public IEnumerator TestScene(TestFrameworkTools.TestInfo testInfo) { // open the scene UnityEngine.SceneManagement.SceneManager.LoadScene(testInfo.sceneListIndex, UnityEngine.SceneManagement.LoadSceneMode.Single); yield return(null); // wait one "frame" to let the scene load SetupSceneForRenderPipelineTest testSetup = GameObject.FindObjectOfType <SetupSceneForRenderPipelineTest>(); TestFrameworkTools.AssertFix.TestWithMessages(testSetup != null, "No SetupSceneForRenderPipelineTest in scene " + testInfo.name); TestFrameworkTools.AssertFix.TestWithMessages(testSetup.cameraToUse != null, "No configured camera in <SetupSceneForRenderPipelineTest> "); Time.captureFramerate = testSetup.forcedFrameRate; // Initialize testSetup.Setup(); yield return(null); // Wait one frame in case we changed the render pipeline testSetup.thingToDoBeforeTest.Invoke(); // Setup Render Target Camera testCamera = testSetup.cameraToUse; var rtDesc = new RenderTextureDescriptor( testSetup.width, testSetup.height, (testSetup.hdr && testCamera.allowHDR) ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.ARGB32, 24); //rtDesc.sRGB = PlayerSettings.colorSpace == ColorSpace.Linear; rtDesc.msaaSamples = testSetup.msaaSamples; var tempTarget = RenderTexture.GetTemporary(rtDesc); var oldTarget = testSetup.cameraToUse.targetTexture; testSetup.cameraToUse.targetTexture = tempTarget; for (int f = 0; f < testSetup.waitForFrames; ++f) { yield return(null); } // render the scene testSetup.cameraToUse.Render(); testSetup.cameraToUse.targetTexture = oldTarget; // Readback the rendered texture var oldActive = RenderTexture.active; RenderTexture.active = tempTarget; var captured = new Texture2D(tempTarget.width, tempTarget.height, TextureFormat.RGB24, false); captured.ReadPixels(new Rect(0, 0, testSetup.width, testSetup.height), 0, 0); RenderTexture.active = oldActive; // Load the template Texture2D fromDisk = new Texture2D(2, 2); string dumpFileLocation = ""; if (!TestFrameworkTools.FindReferenceImage(testInfo, ref fromDisk, captured, ref dumpFileLocation)) { throw new System.Exception(string.Format("Template file not found for {0}, creating it at {1}.", testInfo.name, dumpFileLocation)); } // Compare var areEqual = TestFrameworkTools.CompareTextures(fromDisk, captured, testInfo.threshold); if (!areEqual) { var failedPath = Path.Combine(Directory.GetParent(Application.dataPath).ToString(), "SRP_Failed"); Directory.CreateDirectory(failedPath); var misMatchLocationResult = Path.Combine(failedPath, string.Format("{0}.{1}", testInfo.name, "png")); var misMatchLocationTemplate = Path.Combine(failedPath, string.Format("{0}.template.{1}", testInfo.name, "png")); var generated = captured.EncodeToPNG(); File.WriteAllBytes(misMatchLocationResult, generated); File.Copy(dumpFileLocation, misMatchLocationTemplate, true); throw new System.Exception(string.Format("Scene from {0}, did not match .template file.", testInfo.relativePath)); } else { Assert.IsTrue(true); } testSetup.TearDown(); yield return(null); }
// the actual test public static IEnumerator TestScene(TestFrameworkTools.TestInfo testInfo) { var prjRelativeGraphsPath = TestFrameworkTools.s_Path.Aggregate(TestFrameworkTools.s_RootPath, Path.Combine); var filePath = Path.Combine(prjRelativeGraphsPath, testInfo.relativePath); // open the scene EditorSceneManager.OpenScene(filePath); SetupSceneForRenderPipelineTest testSetup = Object.FindObjectOfType <SetupSceneForRenderPipelineTest> (); Assert.IsNotNull(testSetup, "No SetupSceneForRenderPipelineTest in scene " + testInfo.name); Assert.IsNotNull(testSetup.cameraToUse, "No configured camera in <SetupSceneForRenderPipelineTest>"); if (testSetup.renderPipelines == null || testSetup.renderPipelines.Length == 0) { yield break; } for (int r = 0; r < (doOnlyFirstRenderPipelineAsset?1:testSetup.renderPipelines.Length); ++r) { if (r == 0) { testSetup.Setup(); } else { testSetup.Setup(r); } for (int i = 0; i < testInfo.frameWait; ++i) { yield return(null); } while (UnityEditor.Lightmapping.isRunning) { yield return(null); } // Force rendering of all realtime reflection probes ReflectionProbe[] probes = GameObject.FindObjectsOfType <ReflectionProbe>(); int[] renderIDs = new int[probes.Length]; for (int i = 0; i < probes.Length; ++i) { if (probes[i].mode == UnityEngine.Rendering.ReflectionProbeMode.Realtime) { renderIDs[i] = probes[i].RenderProbe(); } else { renderIDs[i] = -1; } } for (int i = 0; i < probes.Length; ++i) { if (renderIDs[i] != -1) { while (!probes[i].IsFinishedRendering(renderIDs[i])) { yield return(null); } } } testSetup.thingToDoBeforeTest.Invoke(); // Render the camera Texture2D captured = TestFrameworkTools.RenderSetupToTexture(testSetup); // Load the template Texture2D fromDisk = new Texture2D(2, 2); string dumpFileLocation = ""; if (!TestFrameworkTools.FindReferenceImage(testInfo, ref fromDisk, captured, ref dumpFileLocation)) { Assert.Fail("Template file not found for {0}, creating it at {1}.", testInfo.name, dumpFileLocation); } // Compare var areEqual = TestFrameworkTools.CompareTextures(fromDisk, captured, testInfo.threshold); if (!areEqual) { var failedPath = Path.Combine(Directory.GetParent(Application.dataPath).ToString(), "SRP_Failed"); Directory.CreateDirectory(failedPath); var misMatchLocationResult = Path.Combine(failedPath, string.Format("{0}.{1}", testInfo.name, "png")); var misMatchLocationTemplate = Path.Combine(failedPath, string.Format("{0}.template.{1}", testInfo.name, "png")); // Add associated renderpipeline label image if it exists string rpLabelPath = AssetDatabase.GetAssetPath(testSetup.renderPipelines[r]); Texture2D rpLabel = AssetDatabase.LoadAssetAtPath <Texture2D>(rpLabelPath.Remove(rpLabelPath.Length - 5) + "png"); if (rpLabel != null) { Color[] rpLabelPixels = rpLabel.GetPixels(); Color[] targetPixels = captured.GetPixels(0, 0, rpLabel.width, rpLabel.height); for (int p = 0; p < rpLabelPixels.Length; ++p) { targetPixels[p] = Color.Lerp(targetPixels[p], rpLabelPixels[p], rpLabelPixels[p].a); targetPixels[p].a = 1f; } captured.SetPixels(0, 0, rpLabel.width, rpLabel.height, targetPixels); captured.Apply(); } var generated = captured.EncodeToPNG(); File.WriteAllBytes(misMatchLocationResult, generated); File.Copy(dumpFileLocation, misMatchLocationTemplate, true); Object.DestroyImmediate(captured); } Assert.IsTrue(areEqual, "Scene from {0}, did not match .template file.", testInfo.relativePath); if (!areEqual) // No need to continue the test on other renderpipelines, it would overwrite the fail capture { testSetup.TearDown(); yield break; } } testSetup.TearDown(); }
// the actual test public static IEnumerator TestScene(TestFrameworkTools.TestInfo testInfo) { var prjRelativeGraphsPath = TestFrameworkTools.s_Path.Aggregate(TestFrameworkTools.s_RootPath, Path.Combine); var filePath = Path.Combine(prjRelativeGraphsPath, testInfo.relativePath); // open the scene EditorSceneManager.OpenScene(filePath); SetupSceneForRenderPipelineTest testSetup = Object.FindObjectOfType <SetupSceneForRenderPipelineTest> (); Assert.IsNotNull(testSetup, "No SetupSceneForRenderPipelineTest in scene " + testInfo.name); Assert.IsNotNull(testSetup.cameraToUse, "No configured camera in <SetupSceneForRenderPipelineTest>"); testSetup.Setup(); for (int i = 0; i < testInfo.frameWait; ++i) { yield return(null); } while (UnityEditor.Lightmapping.isRunning) { yield return(null); } // Handle play mode and things to do before if (testSetup.testInPlayMode) { //if (!testSetup.invokeAtStart) testSetup.thingToDoBeforeTest.Invoke(); //EditorApplication.isPlaying = true; } else { testSetup.thingToDoBeforeTest.Invoke(); } // Render the camera Texture2D captured = TestFrameworkTools.RenderSetupToTexture(testSetup); // Load the template Texture2D fromDisk = new Texture2D(2, 2); string dumpFileLocation = ""; if (!TestFrameworkTools.FindReferenceImage(testInfo, ref fromDisk, captured, ref dumpFileLocation)) { Assert.Fail("Template file not found for {0}, creating it at {1}.", testInfo.name, dumpFileLocation); } // Compare var areEqual = TestFrameworkTools.CompareTextures(fromDisk, captured, testInfo.threshold); if (!areEqual) { var failedPath = Path.Combine(Directory.GetParent(Application.dataPath).ToString(), "SRP_Failed"); Directory.CreateDirectory(failedPath); var misMatchLocationResult = Path.Combine(failedPath, string.Format("{0}.{1}", testInfo.name, "png")); var misMatchLocationTemplate = Path.Combine(failedPath, string.Format("{0}.template.{1}", testInfo.name, "png")); var generated = captured.EncodeToPNG(); File.WriteAllBytes(misMatchLocationResult, generated); File.Copy(dumpFileLocation, misMatchLocationTemplate); } Assert.IsTrue(areEqual, "Scene from {0}, did not match .template file.", testInfo.relativePath); testSetup.TearDown(); }