public static IEnumerator MakeScreenShotReference(string name) { #if UNITY_EDITOR var path = TestScreenshotTools.ReferenceScreenshotDirectoryToSaveByFileSystem; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var fullPath = path + '/' + TestScreenshotTools.GenerateReferenceScreenshotNameToSave(name); yield return(CaptureScreenshot(fullPath)); int index = fullPath.IndexOf("Assets", StringComparison.Ordinal); var assetPath = fullPath.Substring(index); UnityEditor.AssetDatabase.Refresh(); var textureImporter = (UnityEditor.TextureImporter)UnityEditor.AssetImporter.GetAtPath(assetPath); textureImporter.isReadable = true; textureImporter.mipmapEnabled = false; textureImporter.textureType = UnityEditor.TextureImporterType.Default; textureImporter.textureCompression = UnityEditor.TextureImporterCompression.Uncompressed; textureImporter.npotScale = TextureImporterNPOTScale.None; UnityEditor.AssetDatabase.ImportAsset(assetPath, UnityEditor.ImportAssetOptions.ForceUpdate); #else yield return(null); Debug.LogWarning("You can use Make referece screenshot only in editor."); #endif }
public static IEnumerator MakeScreenShot(string name) { var screenshotPath = TestScreenshotTools.ScreenshotDirectoryToCaptureByUnity; if (!Directory.Exists(screenshotPath)) { Directory.CreateDirectory(screenshotPath); } screenshotPath += '/' + TestScreenshotTools.GenerateScreenshotNameWithTime(name); yield return(CaptureScreenshot(screenshotPath)); }
public static IEnumerator MakeScreenshotAndCompare(string screenShotName, string referenceName, float percentOfCorrectPixels = 0.9f, bool dontFail = false) { #if UNITY_EDITOR if (PlayModeTestRunner.ForceMakeReferenceScreenshot) { yield return(MakeScreenShotReference(referenceName)); yield break; } #endif var screenshotPathToCapture = TestScreenshotTools.ScreenshotDirectoryToCaptureByUnity; if (!Directory.Exists(screenshotPathToCapture)) { Directory.CreateDirectory(screenshotPathToCapture); } var screenShotNameWithTime = TestScreenshotTools.GenerateScreenshotNameWithTime(screenShotName); yield return(CaptureScreenshot(screenshotPathToCapture + '/' + screenShotNameWithTime)); var screenshotPathToLoad = TestScreenshotTools.ScreenshotDirectoryToLoadByFileSystem + '/' + screenShotNameWithTime; var referenceFullPath = TestScreenshotTools.ReferenceScreenshotDirectoryToLoadFromResources + '/' + referenceName; var screenshotCompareError = "screenshot: " + screenShotName + " doesn't match reference: " + referenceFullPath; var screenshotBytes = File.ReadAllBytes(screenshotPathToLoad); var camera = UITestUtils.FindAnyGameObject <Camera>(); var screenshotTexture = new Texture2D(camera.pixelWidth, camera.pixelHeight, TextureFormat.RGB24, false); screenshotTexture.LoadImage(screenshotBytes); screenshotTexture.Apply(); var referenceTex = Resources.Load <Texture2D>(referenceFullPath); var referenceNotFoundMessage = "can't find reference screen shot with path: " + referenceFullPath + " to compare it with screen shot: " + screenShotName; if (!referenceTex) { if (dontFail) { MakeScreenshotAndCompareClass.AddScreenshotErrorLog(referenceNotFoundMessage); yield break; } Assert.Fail(referenceNotFoundMessage); } var pixelsRef = referenceTex.GetPixels32(); var screenShot = screenshotTexture.GetPixels32(); if (dontFail) { if (pixelsRef.Length != screenShot.Length) { screenshotCompareError += " reason: resolution missmatch"; MakeScreenshotAndCompareClass.AddScreenshotErrorLog(screenshotCompareError); yield break; } } Assert.AreEqual(pixelsRef.Length, screenShot.Length, screenshotCompareError); var matchedPixels = 0f; var epsilon = 20f; for (int i = 0; i < pixelsRef.Length; i++) { if (Mathf.Abs(pixelsRef[i].r - screenShot[i].r) < epsilon && Mathf.Abs(pixelsRef[i].g - screenShot[i].g) < epsilon && Mathf.Abs(pixelsRef[i].b - screenShot[i].b) < epsilon) { matchedPixels++; } } var actualPercentOfCorrectPixels = matchedPixels / pixelsRef.Length; if (dontFail) { if (actualPercentOfCorrectPixels < percentOfCorrectPixels) { var errorMessage = screenshotCompareError + " actual treshold: " + actualPercentOfCorrectPixels + " need treshold: " + percentOfCorrectPixels; MakeScreenshotAndCompareClass.AddScreenshotErrorLog(errorMessage); yield break; } } Assert.GreaterOrEqual(actualPercentOfCorrectPixels, percentOfCorrectPixels, screenshotCompareError); GameObject.DestroyImmediate(screenshotTexture, true); //GameObject.DestroyImmediate(referenceTex, true); //todo find out how to unload texture carefully File.Delete(screenshotPathToLoad); #if UNITY_EDITOR TestScreenshotTools.ClearScreenshotsEmptyFolders(); #endif }