您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

153 行
5.8 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NUnit.Framework;
using UnityEngine.TestTools;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
using UnityEngine.Events;
using System.IO;
using System.Linq;
public class GraphicTestsPlayMode : IPrebuildSetup, IPostBuildCleanup
{
public virtual string pipelineID { get { return "NoPipeline"; } }
#if UNITY_EDITOR
UnityEditor.EditorBuildSettingsScene[] oldScenes;
#endif
public void Setup()
{
Debug.Log("Setup for the test");
#if UNITY_EDITOR
oldScenes = UnityEditor.EditorBuildSettings.scenes;
List<UnityEditor.EditorBuildSettingsScene> sceneSetups = new List<UnityEditor.EditorBuildSettingsScene>();
foreach ( TestFrameworkTools.TestInfo testInfo in TestFrameworkTools.CollectScenesPlayMode.GetScenesForPipelineID(pipelineID))
{
sceneSetups.Add(new UnityEditor.EditorBuildSettingsScene {
path = testInfo.relativePath,
enabled = true
});
}
UnityEditor.EditorBuildSettings.scenes = sceneSetups.ToArray();
#endif
}
public void Cleanup()
{
#if UNITY_EDITOR
//UnityEditor.EditorBuildSettings.scenes = oldScenes;
#endif
}
//public IEnumerator TestScene([ValueSource(typeof(TestFrameworkTools.CollectScenesPlayMode), "HDRP")]TestFrameworkTools.TestInfo testInfo)
public 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
UnityEngine.SceneManagement.SceneManager.LoadScene( testInfo.relativePath , UnityEngine.SceneManagement.LoadSceneMode.Single);
yield return null; // wait one "frame" to let the scene load
SetupSceneForRenderPipelineTest testSetup = GameObject.FindObjectOfType<SetupSceneForRenderPipelineTest>();
//Assert.IsNotNull(testSetup, "No SetupSceneForRenderPipelineTest in scene " + testInfo.name);
//Assert.IsNotNull(testSetup.cameraToUse, "No configured camera in <SetupSceneForRenderPipelineTest>");
TestFrameworkTools.AssertFix.TestWithMessages(testSetup != null, "No SetupSceneForRenderPipelineTest in scene " + testInfo.name);
TestFrameworkTools.AssertFix.TestWithMessages(testSetup.cameraToUse != null, "No configured camera in <SetupSceneForRenderPipelineTest> ");
// 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;
while (!testSetup.IsTestFinished) yield return null;
// Pause
float prevTimeScale = Time.timeScale;
Time.timeScale = 0f;
// 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;
//Unpause
Time.timeScale = prevTimeScale;
// 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;
}
public class HDRP : GraphicTestsPlayMode
{
public override string pipelineID { get { return "HDRP"; } }
[UnityTest]
new public IEnumerator TestScene([ValueSource(typeof(TestFrameworkTools.CollectScenesPlayMode), "HDRP")]TestFrameworkTools.TestInfo testInfo)
{
return base.TestScene(testInfo);
}
}
}