using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using NUnit.Framework; using UnityEngine; using UnityEditor.SceneManagement; using Object = UnityEngine.Object; using UnityEngine.Experimental.Rendering; using UnityEngine.TestTools; namespace UnityEditor.Experimental.Rendering { public class GraphicsTests { // Change the SRP before a full batch of tests public virtual string _SRP_ID { get { return "NONE"; } } [OneTimeSetUp] public void OneTimeSetUp() { BackupSceneManagerSetup(); SetupRenderPipeAsset(); } [OneTimeTearDown] public void OnTimeTearDown() { Debug.Log("OnTimeTearDown"); RestoreRenderPipeAsset(); RestoreSceneManagerSetup(); } public static RenderPipelineAsset GetRenderPipelineAsset(string _SRP_ID) { string absolutePath = TestFrameworkTools.s_Path.Aggregate(TestFrameworkTools.s_RootPath, Path.Combine); string filePath = Path.Combine(absolutePath, TestFrameworkTools.renderPipelineAssets[_SRP_ID] ); filePath = filePath.Replace(Application.dataPath, ""); filePath = filePath.Remove(0, 1); //Debug.Log("Before combine: " + filePath); filePath = Path.Combine("Assets", filePath); //Debug.Log("RP Asset is at : " + filePath); return (RenderPipelineAsset)AssetDatabase.LoadAssetAtPath(filePath, typeof(RenderPipelineAsset)); } public static RenderPipelineAsset beforeTestsRenderPipeAsset; public static RenderPipelineAsset wantedTestsRenderPipeAsset; public void SetupRenderPipeAsset() { //Debug.Log("Set " + _SRP_ID + " render pipeline. Previous was "+ ( (UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset == null)? "null":UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset.name) ); beforeTestsRenderPipeAsset = UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset; wantedTestsRenderPipeAsset = GetRenderPipelineAsset(_SRP_ID); if (wantedTestsRenderPipeAsset != beforeTestsRenderPipeAsset) UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset = wantedTestsRenderPipeAsset; } public void RestoreRenderPipeAsset() { //Debug.Log("RestoreRenderPipeAsset from " + wantedTestsRenderPipeAsset.name + " to " + ((beforeTestsRenderPipeAsset == null)?"null":beforeTestsRenderPipeAsset.name)); if (wantedTestsRenderPipeAsset != beforeTestsRenderPipeAsset) { //Debug.Log("RestoreRenderPipeAsset -> Actual restore"); UnityEngine.Rendering.GraphicsSettings.renderPipelineAsset = beforeTestsRenderPipeAsset; } } public static SceneSetup[] sceneManagerSetupBeforeTest; public void BackupSceneManagerSetup() { sceneManagerSetupBeforeTest = EditorSceneManager.GetSceneManagerSetup(); } public void RestoreSceneManagerSetup() { if ( (sceneManagerSetupBeforeTest == null) || ( sceneManagerSetupBeforeTest.Length == 0 ) ) { EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects, NewSceneMode.Single); } else { EditorSceneManager.RestoreSceneManagerSetup(sceneManagerSetupBeforeTest); } } // 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 (); Assert.IsNotNull(testSetup, "No SetupSceneForRenderPipelineTest in scene " + testInfo.name); Assert.IsNotNull(testSetup.cameraToUse, "No configured camera in "); testSetup.Setup(); 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(); int[] renderIDs = new int[probes.Length]; for (int i=0; i