private void Reload(GraphicsTestCase _testCase = null)
        {
            testCase = _testCase;

            if (testCase == null)
            {
                return;
            }

            //Debug.Log("Show result for : " + _testCase.ScenePath);

            GetImages();

            if (templateImage == null || resultImage == null)
            {
                testOKOrNotRun = true;
                minDiff        = maxDiff = 1f;
            }
            else
            {
                testOKOrNotRun = false;
                minDiff        = .45f;
                maxDiff        = .55f;
            }

            ApplyValues();

            testResultTreeView.Reload();
        }
 public TestResultViewItem(int id, int depth, string displayName, GraphicsTestCase testCase)
 {
     this.id          = id;
     this.depth       = depth;
     this.displayName = displayName;
     this.testCase    = testCase;
 }
    public IEnumerator Run(GraphicsTestCase testCase)
    {
        SceneManager.LoadScene(testCase.ScenePath);

        // Always wait one frame for scene load
        yield return(null);

        var cameras  = GameObject.FindGameObjectsWithTag("MainCamera").Select(x => x.GetComponent <Camera>());
        var settings = Object.FindObjectOfType <LWGraphicsTestSettings>();

        Assert.IsNotNull(settings, "Invalid test scene, couldn't find LWGraphicsTestSettings");

        // Stereo screen capture on Mac generates monoscopic images and won't be fixed.
        Assume.That((Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.OSXPlayer), "Stereo tests do not run on MacOSX.");

        XRSettings.gameViewRenderMode = GameViewRenderMode.BothEyes;
        yield return(null);

        foreach (var camera in cameras)
        {
            camera.stereoTargetEye = StereoTargetEyeMask.Both;
        }

        for (int i = 0; i < settings.WaitFrames; i++)
        {
            yield return(null);
        }

        ImageAssert.AreEqual(testCase.ReferenceImage, cameras.Where(x => x != null), settings.ImageComparisonSettings);
    }
Beispiel #4
0
    public IEnumerator Run(GraphicsTestCase testCase)
    {
        CleanUp();

        if (!testCase.ScenePath.Contains("GraphicsTest"))
        {
            Assert.Ignore("Ignoring this test because the scene is not under GraphicsTest folder, or not named with GraphicsTest.");
        }

        SceneManager.LoadScene(testCase.ScenePath);

        // Always wait one frame for scene load
        yield return(null);

        //Get Test settings
        //ignore instead of failing, because some scenes might not be used for GraphicsTest
        var settings = Object.FindObjectOfType <GraphicsTestSettingsCustom>();

        if (settings == null)
        {
            Assert.Ignore("Ignoring this test for GraphicsTest because couldn't find GraphicsTestSettingsCustom");
        }

        #if !UNITY_EDITOR
        Screen.SetResolution(settings.ImageComparisonSettings.TargetWidth, settings.ImageComparisonSettings.TargetHeight, false);
        #endif

        var cameras = GameObject.FindGameObjectsWithTag("MainCamera").Select(x => x.GetComponent <Camera>());
        //var settings = Object.FindObjectOfType<UniversalGraphicsTestSettings>();
        //Assert.IsNotNull(settings, "Invalid test scene, couldn't find UniversalGraphicsTestSettings");
        //Scene scene = SceneManager.GetActiveScene();

        yield return(null);

        int waitFrames = settings.WaitFrames;

        if (settings.ImageComparisonSettings.UseBackBuffer && settings.WaitFrames < 1)
        {
            waitFrames = 1;
        }
        for (int i = 0; i < waitFrames; i++)
        {
            yield return(new WaitForEndOfFrame());
        }

        #if UNITY_ANDROID
        // On Android first scene often needs a bit more frames to load all the assets
        // otherwise the screenshot is just a black screen
        if (!wasFirstSceneRan)
        {
            for (int i = 0; i < firstSceneAdditionalFrames; i++)
            {
                yield return(null);
            }
            wasFirstSceneRan = true;
        }
        #endif

        ImageAssert.AreEqual(testCase.ReferenceImage, cameras.Where(x => x != null), settings.ImageComparisonSettings);
    }
Beispiel #5
0
    public IEnumerator Run(GraphicsTestCase testCase)
    {
        SceneManager.LoadScene(testCase.ScenePath);

        // Always wait one frame for scene load
        yield return(null);

        var cameras  = GameObject.FindGameObjectsWithTag("MainCamera").Select(x => x.GetComponent <Camera>());
        var settings = Object.FindObjectOfType <UniversalGraphicsTestSettings>();

        Assert.IsNotNull(settings, "Invalid test scene, couldn't find UniversalGraphicsTestSettings");

        Scene scene = SceneManager.GetActiveScene();

        yield return(null);

        for (int i = 0; i < settings.WaitFrames; i++)
        {
            yield return(null);
        }

#if UNITY_ANDROID
        // On Android first scene often needs a bit more frames to load all the assets
        // otherwise the screenshot is just a black screen
        if (!wasFirstSceneRan)
        {
            for (int i = 0; i < firstSceneAdditionalFrames; i++)
            {
                yield return(null);
            }
            wasFirstSceneRan = true;
        }
#endif

        ImageAssert.AreEqual(testCase.ReferenceImage, cameras.Where(x => x != null), settings.ImageComparisonSettings);

        // Does it allocate memory when it renders what's on the main camera?
        bool allocatesMemory = false;
        var  mainCamera      = GameObject.FindGameObjectWithTag("MainCamera").GetComponent <Camera>();

        // 2D Renderer is currently allocating memory, skip it as it will always fail GC alloc tests.
        var  additionalCameraData = mainCamera.GetUniversalAdditionalCameraData();
        bool is2DRenderer         = additionalCameraData.scriptableRenderer is Renderer2D;

        if (!is2DRenderer)
        {
            try
            {
                ImageAssert.AllocatesMemory(mainCamera, settings?.ImageComparisonSettings);
            }
            catch (AssertionException)
            {
                allocatesMemory = true;
            }
            if (allocatesMemory)
            {
                Assert.Fail("Allocated memory when rendering what is on main camera");
            }
        }
    }
        public static void OpenWindow(GraphicsTestCase _testCase)
        {
            TestResultWindow window = GetWindow <TestResultWindow>();

            window.minSize = new Vector2(800f, 800f);

            window.Reload(_testCase);
        }
Beispiel #7
0
    public IEnumerator Run(GraphicsTestCase testCase)
    {
        SceneManager.LoadScene(testCase.ScenePath);

        // Always wait one frame for scene load
        yield return(null);

        //Get Test settings
        //ignore instead of failing, because some scenes might not be used for GraphicsTest
        var settings = Object.FindObjectOfType <GraphicsTestSettingsCustom>();

        if (settings == null)
        {
            Assert.Ignore("Ignoring this test for GraphicsTest because couldn't find GraphicsTestSettingsCustom");
        }

#if !UNITY_EDITOR
        Screen.SetResolution(settings.ImageComparisonSettings.TargetWidth, settings.ImageComparisonSettings.TargetHeight, false);
#endif

        var cameras = GameObject.FindGameObjectsWithTag("MainCamera").Select(x => x.GetComponent <Camera>());

        yield return(null);

        int waitFrames = settings.WaitFrames;

        if (settings.ImageComparisonSettings.UseBackBuffer && settings.WaitFrames < 1)
        {
            waitFrames = 1;
        }

        if (XRGraphicsAutomatedTests.enabled)
        {
            waitFrames = Unity.Testing.XR.Runtime.ConfigureMockHMD.SetupTest(true, waitFrames, settings.ImageComparisonSettings);
        }

        for (int i = 0; i < waitFrames; i++)
        {
            yield return(new WaitForEndOfFrame());
        }


#if UNITY_ANDROID
        // On Android first scene often needs a bit more frames to load all the assets
        // otherwise the screenshot is just a black screen
        if (!wasFirstSceneRan)
        {
            for (int i = 0; i < firstSceneAdditionalFrames; i++)
            {
                yield return(null);
            }
            wasFirstSceneRan = true;
        }
#endif

        ImageAssert.AreEqual(testCase.ReferenceImage, cameras.Where(x => x != null), settings.ImageComparisonSettings);
    }
Beispiel #8
0
    public IEnumerator Test1(GraphicsTestCase testCase)
    {
        var asyncLoad = SceneManager.LoadSceneAsync(testCase.ScenePath);

        yield return(new WaitUntil(() => asyncLoad.isDone));

        // this pause is here on the first scene load to mitigate an Oculus issue
        // where screenshots would be black for the first couple scenes without it. Bug 1154476.
        if (check)
        {
            yield return(new WaitForSeconds(1));

            check = false;
        }

        XRDevice.DisableAutoXRCameraTracking(Camera.main, true);

        var testSettings = GameObject.FindObjectOfType <GraphicsTestSettings>();

        Assert.IsNotNull(testSettings, "No test settings script found, not a valid test");

        Screen.SetResolution(testSettings.ImageComparisonSettings.TargetWidth, testSettings.ImageComparisonSettings.TargetHeight, false);

        // need a frame to let the resolution change.
        yield return(null);

        yield return(new WaitForEndOfFrame());

        var screenShot = new Texture2D(0, 0, TextureFormat.RGBA32, false);

        screenShot = ScreenCapture.CaptureScreenshotAsTexture(ScreenCapture.StereoScreenCaptureMode.BothEyes);

        try
        {
            ImageAssert.AreEqual(testCase.ReferenceImage, screenShot, testSettings.ImageComparisonSettings);
        }
        catch (AssertionException e)
        {
            // test setup sets the results images directory to the testResults/ResultImages directory
            var testName        = TestContext.CurrentContext.Test.Name;
            var actualImageName = "./ResultsImages/" + testName + ".png";
            TestContext.CurrentContext.Test.Properties.Set("Image", actualImageName);

            // If the exception says there was a null reference image then there isn't a diff or expected images
            if (!e.Message.Contains("But was:  null"))
            {
                var diffImageName = "./ResultsImages/" + testName + ".diff.png";
                TestContext.CurrentContext.Test.Properties.Set("DiffImage", diffImageName);

                var expectedImageName = "./ResultsImages/" + testName + ".expected.png";
                TestContext.CurrentContext.Test.Properties.Set("ExpectedImage", expectedImageName);
            }

            throw;
        }
    }
Beispiel #9
0
        public bool GetImages(GraphicsTestCase _testCase = null)
        {
            GraphicsTestCase tCase = (_testCase == null)? testCase : _testCase;

            if (tCase == null)
            {
                templateImage = null;
                resultImage   = null;
                diffImage     = null;
                return(false);
            }

            if (tCase.ReferenceImage == null)
            {
                resultImage = null;
                diffImage   = null;
                return(false); // No reference image found
            }

            var colorSpace     = UseGraphicsTestCasesAttribute.ColorSpace;
            var platform       = UseGraphicsTestCasesAttribute.Platform;
            var graphicsDevice = UseGraphicsTestCasesAttribute.GraphicsDevice;
            var xrsdk          = UseGraphicsTestCasesAttribute.LoadedXRDevice;

            var actualImagesDir = Path.Combine(ActualImagesRoot, string.Format("{0}/{1}/{2}/{3}", colorSpace, platform.ToUniqueString(), graphicsDevice, xrsdk));

            var sceneName = Path.GetFileNameWithoutExtension(tCase.ScenePath);

            templateImage = tCase.ReferenceImage;
            resultImage   = AssetDatabase.LoadMainAssetAtPath(Path.Combine(actualImagesDir, sceneName + ".png")) as Texture2D;
            diffImage     = AssetDatabase.LoadMainAssetAtPath(Path.Combine(actualImagesDir, sceneName + ".diff.png")) as Texture2D;

            foreach (Texture2D image in new Texture2D[] { templateImage, resultImage, diffImage })
            {
                if (image == null)
                {
                    continue;
                }
                image.filterMode = FilterMode.Point;
                image.mipMapBias = -10;
                image.hideFlags  = HideFlags.HideAndDontSave;
            }

            if (resultImage == null && diffImage == null)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public GraphicsTestCase GetTestCaseFromPath(string scenePath)
        {
            GraphicsTestCase output = null;

            var allImages = CollectReferenceImagePathsFor(string.IsNullOrEmpty(m_ReferenceImagePath) ? ReferenceImagesRoot : m_ReferenceImagePath, QualitySettings.activeColorSpace, Application.platform,
                SystemInfo.graphicsDeviceType);

            Texture2D referenceImage = null;

            string imagePath;
            if (allImages.TryGetValue(Path.GetFileNameWithoutExtension(scenePath), out imagePath))
                referenceImage = AssetDatabase.LoadAssetAtPath<Texture2D>(imagePath);

            output = new GraphicsTestCase(scenePath, referenceImage);

            return output;
        }
    public IEnumerator Test1(GraphicsTestCase testCase)
    {
        SceneManager.LoadScene(testCase.ScenePath);

        yield return(null);

        XRDevice.DisableAutoXRCameraTracking(Camera.main, true);

        var testSettings = GameObject.FindObjectOfType <GraphicsTestSettings>();

        Assert.IsNotNull(testSettings, "No test settings script found, not a valid test");

        Screen.SetResolution(testSettings.ImageComparisonSettings.TargetWidth, testSettings.ImageComparisonSettings.TargetHeight, false);

        yield return(new WaitForSeconds(1));

        yield return(new WaitForEndOfFrame());

        var screenShot = new Texture2D(0, 0, TextureFormat.RGBA32, false);

        screenShot = ScreenCapture.CaptureScreenshotAsTexture(ScreenCapture.StereoScreenCaptureMode.BothEyes);

        try
        {
            ImageAssert.AreEqual(testCase.ReferenceImage, screenShot, testSettings.ImageComparisonSettings);
        }
        catch (AssertionException e)
        {
            // test setup sets the results images directory to the testResults/ResultImages directory
            var testName        = TestContext.CurrentContext.Test.Name;
            var actualImageName = "./ResultsImages/" + testName + ".png";
            TestContext.CurrentContext.Test.Properties.Set("Image", actualImageName);

            // If the exception says there was a null reference image then there isn't a diff or expected images
            if (!e.Message.Contains("But was:  null"))
            {
                var diffImageName = "./ResultsImages/" + testName + ".diff.png";
                TestContext.CurrentContext.Test.Properties.Set("DiffImage", diffImageName);

                var expectedImageName = "./ResultsImages/" + testName + ".expected.png";
                TestContext.CurrentContext.Test.Properties.Set("ExpectedImage", expectedImageName);
            }

            throw;
        }
    }
Beispiel #12
0
    public IEnumerator Run(GraphicsTestCase testCase)
    {
        CleanUp();
        SceneManager.LoadScene(testCase.ScenePath);

        // Always wait one frame for scene load
        yield return(null);

        //Get Test settings
        //ignore instead of failing, because some scenes might not be used for GraphicsTest
        var settings = Object.FindObjectOfType <GraphicsTestSettingsCustom>();

        if (settings == null)
        {
            // CleanUp();
            Assert.Ignore("Ignoring this test for GraphicsTest because couldn't find GraphicsTestSettingsCustom");
        }
        //Assert.IsNotNull(settings, "Invalid test scene, couldn't find GraphicsTestSettingsCustom");

        Screen.SetResolution(settings.ImageComparisonSettings.TargetWidth, settings.ImageComparisonSettings.TargetHeight, false);

        // Get the test camera
        GameObject[] camObjects = GameObject.FindGameObjectsWithTag("MainCamera");
        var          cameras    = camObjects.Select(x => x.GetComponent <Camera>());

        int waitFrames = settings.WaitFrames;

        if (XRGraphicsAutomatedTests.enabled)
        {
            waitFrames = Unity.Testing.XR.Runtime.ConfigureMockHMD.SetupTest(true, waitFrames, settings.ImageComparisonSettings);
        }

        // WaitFrames according to settings
        for (int i = 0; i < waitFrames; i++)
        {
            yield return(new WaitForEndOfFrame());
        }

        // Test Assert
        ImageAssert.AreEqual(testCase.ReferenceImage, cameras.Where(x => x != null), settings.ImageComparisonSettings);

        // Always wait one frame for scene load
        yield return(new WaitForEndOfFrame());
    }
Beispiel #13
0
    public IEnumerator Run(GraphicsTestCase testCase)
    {
        SceneManager.LoadScene(testCase.ScenePath);

        // Always wait one frame for scene load
        yield return(null);

        var ts = Time.timeScale;

        Time.timeScale = 0f;

        var camera   = GameObject.FindGameObjectWithTag("MainCamera").GetComponent <Camera>();
        var settings = Object.FindObjectOfType <BoatAttackGraphicsTestsSettings>();

        if (!settings)
        {
            settings = camera.gameObject.AddComponent <BoatAttackGraphicsTestsSettings>();
        }

        settings.ImageComparisonSettings.UseHDR                   = true;
        settings.ImageComparisonSettings.UseBackBuffer            = true;
        settings.ImageComparisonSettings.ActiveImageTests         = ImageComparisonSettings.ImageTests.IncorrectPixelsCount;
        settings.ImageComparisonSettings.IncorrectPixelsThreshold = 0.001f;
        settings.WaitFrames = 10;

        settings.ImageComparisonSettings.TargetWidth = 1920;
        settings.ImageComparisonSettings.TargetWidth = 1080;

#if STATIC_EVERYTHING
        Debug.Log("hello, this worked");
#endif

        for (int i = 0; i < settings.WaitFrames; i++)
        {
            yield return(new WaitForEndOfFrame());
        }

        ImageAssert.AreEqual(testCase.ReferenceImage, camera, settings.ImageComparisonSettings);

        Time.timeScale = ts;
    }
    public IEnumerator Run(GraphicsTestCase testCase)
    {
        SceneManager.LoadScene(testCase.ScenePath);

        // Arbitrary wait for 5 frames for the scene to load, and other stuff to happen (like Realtime GI to appear ...)
        for (int i = 0; i < 5; ++i)
        {
            yield return(null);
        }

        // Load the test settings
        var settings = GameObject.FindObjectOfType <HDRP_TestSettings>();

        var camera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent <Camera>();

        if (camera == null)
        {
            camera = GameObject.FindObjectOfType <Camera>();
        }
        if (camera == null)
        {
            Assert.Fail("Missing camera for graphic tests.");
        }

        Time.captureFramerate = settings.captureFramerate;

        if (settings.doBeforeTest != null)
        {
            settings.doBeforeTest.Invoke();

            // Wait again one frame, to be sure.
            yield return(null);
        }

        for (int i = 0; i < settings.waitFrames; ++i)
        {
            yield return(null);
        }

        ImageAssert.AreEqual(testCase.ReferenceImage, camera, (settings != null)?settings.ImageComparisonSettings:null);
    }
    public IEnumerator Run(GraphicsTestCase testCase)
    {
        SceneManager.LoadScene(testCase.ScenePath);

        // Always wait one frame for scene load
        yield return(null);

        var camera   = GameObject.FindGameObjectWithTag("MainCamera").GetComponent <Camera>();
        var settings = Object.FindObjectOfType <ShaderGraphGraphicsTestSettings>();

        Assert.IsNotNull(settings, "Invalid test scene, couldn't find ShaderGraphGraphicsTestSettings");
        settings.OnTestBegin();

        for (int i = 0; i < settings.WaitFrames; i++)
        {
            yield return(null);
        }

        ImageAssert.AreEqual(testCase.ReferenceImage, camera, settings.ImageComparisonSettings);
        settings.OnTestComplete();
    }
    public IEnumerator Run(GraphicsTestCase testCase)
    {
        SceneManager.LoadScene(testCase.ScenePath);

        // Always wait one frame for scene load
        yield return(null);

        var cameras  = GameObject.FindGameObjectsWithTag("MainCamera").Select(x => x.GetComponent <Camera>());
        var settings = Object.FindObjectOfType <LWGraphicsTestSettings>();

        Assert.IsNotNull(settings, "Invalid test scene, couldn't find LWGraphicsTestSettings");

        Scene scene = SceneManager.GetActiveScene();

        if (scene.name.Substring(3, 4).Equals("_xr_"))
        {
            Assume.That((Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.OSXPlayer), "Stereo LWRP tests do not run on MacOSX.");

            XRSettings.LoadDeviceByName("MockHMD");
            yield return(null);

            XRSettings.enabled = true;
            yield return(null);

            XRSettings.gameViewRenderMode = GameViewRenderMode.BothEyes;
            yield return(null);

            foreach (var camera in cameras)
            {
                camera.stereoTargetEye = StereoTargetEyeMask.Both;
            }
        }

        for (int i = 0; i < settings.WaitFrames; i++)
        {
            yield return(null);
        }

        ImageAssert.AreEqual(testCase.ReferenceImage, cameras.Where(x => x != null), settings.ImageComparisonSettings);
    }
    [Timeout(300 * 1000)] // Set timeout to 5 minutes to handle complex scenes with many shaders (default timeout is 3 minutes)
    public IEnumerator Run(GraphicsTestCase testCase)
    {
        SceneManager.LoadScene(testCase.ScenePath);

        // Arbitrary wait for 5 frames for the scene to load, and other stuff to happen (like Realtime GI to appear ...)
        for (int i = 0; i < 5; ++i)
        {
            yield return(null);
        }

        // Load the test settings
        var settings = GameObject.FindObjectOfType <HDRP_TestSettings>();

        var camera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent <Camera>();

        if (camera == null)
        {
            camera = GameObject.FindObjectOfType <Camera>();
        }
        if (camera == null)
        {
            Assert.Fail("Missing camera for graphic tests.");
        }

        Time.captureFramerate = settings.captureFramerate;

        if (XRGraphicsAutomatedTests.enabled)
        {
            if (settings.xrCompatible)
            {
                XRGraphicsAutomatedTests.running = true;

                // Increase tolerance to account for slight changes due to float precision
                settings.ImageComparisonSettings.AverageCorrectnessThreshold  *= settings.xrThresholdMultiplier;
                settings.ImageComparisonSettings.PerPixelCorrectnessThreshold *= settings.xrThresholdMultiplier;

                // Increase number of volumetric slices to compensate for initial half-resolution due to XR single-pass optimization
                foreach (var volume in GameObject.FindObjectsOfType <Volume>())
                {
                    if (volume.profile.TryGet <Fog>(out Fog fog))
                    {
                        fog.volumeSliceCount.value *= 2;
                    }
                }
            }
            else
            {
                Assert.Ignore("Test scene is not compatible with XR and will be skipped.");
            }
        }

        if (settings.doBeforeTest != null)
        {
            settings.doBeforeTest.Invoke();

            // Wait again one frame, to be sure.
            yield return(null);
        }

        // Reset temporal effects on hdCamera
        HDCamera.GetOrCreate(camera).Reset();

        for (int i = 0; i < settings.waitFrames; ++i)
        {
            yield return(null);
        }

        var settingsSG = (GameObject.FindObjectOfType <HDRP_TestSettings>() as HDRP_ShaderGraph_TestSettings);

        if (settingsSG == null || !settingsSG.compareSGtoBI)
        {
            // Standard Test
            ImageAssert.AreEqual(testCase.ReferenceImage, camera, settings?.ImageComparisonSettings);

            // For some reason, tests on mac os have started failing with render graph enabled by default.
            // Some tests have 400+ gcalloc in them. Unfortunately it's not reproductible outside of command line so it's impossible to debug.
            // That's why we don't test on macos anymore.
            if (settings.checkMemoryAllocation && SystemInfo.graphicsDeviceType != GraphicsDeviceType.Metal)
            {
                // Does it allocate memory when it renders what's on camera?
                bool allocatesMemory = false;
                try
                {
                    // GC alloc from Camera.CustomRender (case 1206364)
                    int gcAllocThreshold = 2;

                    ImageAssert.AllocatesMemory(camera, settings?.ImageComparisonSettings, gcAllocThreshold);
                }
                catch (AssertionException)
                {
                    allocatesMemory = true;
                }
                if (allocatesMemory)
                {
                    Assert.Fail("Allocated memory when rendering what is on camera");
                }
            }
        }
        else
        {
            if (settingsSG.sgObjs == null)
            {
                Assert.Fail("Missing Shader Graph objects in test scene.");
            }
            if (settingsSG.biObjs == null)
            {
                Assert.Fail("Missing comparison objects in test scene.");
            }

            settingsSG.sgObjs.SetActive(true);
            settingsSG.biObjs.SetActive(false);
            yield return(null); // Wait a frame

            yield return(null);

            bool sgFail = false;
            bool biFail = false;

            // First test: Shader Graph
            try
            {
                ImageAssert.AreEqual(testCase.ReferenceImage, camera, (settings != null)?settings.ImageComparisonSettings:null);
            }
            catch (AssertionException)
            {
                sgFail = true;
            }

            settingsSG.sgObjs.SetActive(false);
            settingsSG.biObjs.SetActive(true);
            settingsSG.biObjs.transform.position = settingsSG.sgObjs.transform.position; // Move to the same location.
            yield return(null);                                                          // Wait a frame

            yield return(null);

            // Second test: HDRP/Lit Materials
            try
            {
                ImageAssert.AreEqual(testCase.ReferenceImage, camera, (settings != null)?settings.ImageComparisonSettings:null);
            }
            catch (AssertionException)
            {
                biFail = true;
            }

            // Informs which ImageAssert failed, if any.
            if (sgFail && biFail)
            {
                Assert.Fail("Both Shader Graph and Non-Shader Graph Objects failed to match the reference image");
            }
            else if (sgFail)
            {
                Assert.Fail("Shader Graph Objects failed.");
            }
            else if (biFail)
            {
                Assert.Fail("Non-Shader Graph Objects failed to match Shader Graph objects.");
            }
        }
    }
Beispiel #18
0
    public IEnumerator Run(GraphicsTestCase testCase)
    {
        SceneManager.LoadScene(testCase.ScenePath);

        // Always wait one frame for scene load
        yield return(null);

        var cameras  = GameObject.FindGameObjectsWithTag("MainCamera").Select(x => x.GetComponent <Camera>());
        var settings = Object.FindObjectOfType <LWGraphicsTestSettings>();

        Assert.IsNotNull(settings, "Invalid test scene, couldn't find LWGraphicsTestSettings");

        // Stereo screen capture on Mac generates monoscopic images and won't be fixed.
        Assume.That((Application.platform != RuntimePlatform.OSXEditor && Application.platform != RuntimePlatform.OSXPlayer), "Stereo tests do not run on MacOSX.");

        var referenceImage = testCase.ReferenceImage;

        // make sure we're rendering in the same size as the reference image, otherwise this is not really comparable.
        Screen.SetResolution(referenceImage.width, referenceImage.height, FullScreenMode.Windowed);

#if UNITY_2020_2_OR_NEWER
        // Ensure a valid XR display is active
        List <XRDisplaySubsystem> xrDisplays = new List <XRDisplaySubsystem>();
        SubsystemManager.GetInstances(xrDisplays);
        Assume.That(xrDisplays.Count > 0 && xrDisplays[0].running, "No XR display active!");

        // Set mirror view to side-by-side (both eyes)
        xrDisplays[0].SetPreferredMirrorBlitMode(XRMirrorViewBlitMode.SideBySide);
#else
        XRSettings.gameViewRenderMode = GameViewRenderMode.BothEyes;
#endif

        yield return(null);

        foreach (var camera in cameras)
        {
            camera.stereoTargetEye = StereoTargetEyeMask.Both;
        }

        var tempScreenshotFile = Path.ChangeExtension(Path.GetTempFileName(), ".png");
        // clean up previous file if it happens to exist at this point
        if (FileAvailable(tempScreenshotFile))
        {
            System.IO.File.Delete(tempScreenshotFile);
        }

        for (int i = 0; i < settings.WaitFrames; i++)
        {
            yield return(null);
        }

        // wait for rendering to complete
        yield return(new WaitForEndOfFrame());

        // we'll take a screenshot here, as what we want to compare is the actual result on-screen.
        // ScreenCapture.CaptureScreenshotAsTexture --> does not work since colorspace is wrong, would need colorspace change and thus color compression
        // ScreenCapture.CaptureScreenshotIntoRenderTexture --> does not work since texture is flipped, would need another pass
        // so we need to capture and reload the resulting file.
        ScreenCapture.CaptureScreenshot(tempScreenshotFile, ScreenCapture.StereoScreenCaptureMode.BothEyes);

        // NOTE: there's discussions around whether Unity has actually documented this correctly.
        // Unity says: next frame MUST have the file ready
        // Community says: not true, file write might take longer, so have to explicitly check the file handle before use
        // https://forum.unity.com/threads/how-to-wait-for-capturescreen-to-complete.172194/
        yield return(null);

        while (!FileAvailable(tempScreenshotFile))
        {
            yield return(null);
        }

        // load the screenshot back into memory and change to the same format as we want to compare with
        var actualImage = new Texture2D(1, 1);
        actualImage.LoadImage(System.IO.File.ReadAllBytes(tempScreenshotFile));

        if (actualImage.width != referenceImage.width || actualImage.height != referenceImage.height)
        {
            Debug.LogWarning("[" + testCase.ScenePath + "] Image size differs (ref: " + referenceImage.width + "x" + referenceImage.height + " vs. actual: " + actualImage.width + "x" + actualImage.height + "). " + (Application.isEditor ? " is your GameView set to a different resolution than the reference images?" : "is your build size different than the reference images?"));
            actualImage = ChangeTextureSize(actualImage, referenceImage.width, referenceImage.height);
        }
        // ref is usually in RGB24 or RGBA32 while actual is in ARGB32, we need to convert formats
        if (referenceImage.format != actualImage.format)
        {
            actualImage = ChangeTextureFormat(actualImage, referenceImage.format);
        }

        // delete temporary file
        File.Delete(tempScreenshotFile);

        // for testing
        // File.WriteAllBytes("reference.png", referenceImage.EncodeToPNG());
        // File.WriteAllBytes("actual.png", actualImage.EncodeToPNG());

        ImageAssert.AreEqual(referenceImage, actualImage, settings.ImageComparisonSettings);
    }
Beispiel #19
0
    public IEnumerator Run(GraphicsTestCase testCase)
    {
        SceneManager.LoadScene(testCase.ScenePath);

        // Always wait one frame for scene load
        yield return(null);

        var cameras = GameObject.FindGameObjectsWithTag("MainCamera").Select(x => x.GetComponent <Camera>());

        Assert.True(cameras != null && cameras.Any(), "Invalid test scene, couldn't find a camera with MainCamera tag.");
        var settings = Object.FindObjectOfType <UniversalGraphicsTestSettings>();

        Assert.IsNotNull(settings, "Invalid test scene, couldn't find UniversalGraphicsTestSettings");

        int waitFrames = Unity.Testing.XR.Runtime.ConfigureMockHMD.SetupTest(settings.XRCompatible, settings.WaitFrames, settings.ImageComparisonSettings);

        Scene scene = SceneManager.GetActiveScene();

        yield return(null);

        if (settings.ImageComparisonSettings.UseBackBuffer && waitFrames < 1)
        {
            waitFrames = 1;
        }

        for (int i = 0; i < waitFrames; i++)
        {
            yield return(new WaitForEndOfFrame());
        }

#if UNITY_ANDROID
        // On Android first scene often needs a bit more frames to load all the assets
        // otherwise the screenshot is just a black screen
        if (!wasFirstSceneRan)
        {
            for (int i = 0; i < firstSceneAdditionalFrames; i++)
            {
                yield return(new WaitForEndOfFrame());
            }
            wasFirstSceneRan = true;
        }
#endif

        ImageAssert.AreEqual(testCase.ReferenceImage, cameras.Where(x => x != null), settings.ImageComparisonSettings);

        // Does it allocate memory when it renders what's on the main camera?
        bool allocatesMemory = false;
        var  mainCamera      = GameObject.FindGameObjectWithTag("MainCamera").GetComponent <Camera>();

        if (settings == null || settings.CheckMemoryAllocation)
        {
            try
            {
                ImageAssert.AllocatesMemory(mainCamera, settings?.ImageComparisonSettings);
            }
            catch (AssertionException)
            {
                allocatesMemory = true;
            }

            if (allocatesMemory)
            {
                Assert.Fail("Allocated memory when rendering what is on main camera");
            }
        }
    }
        public IEnumerator Run(GraphicsTestCase testCase)
        {
#if UNITY_EDITOR
            while (SceneView.sceneViews.Count > 0)
            {
                var sceneView = SceneView.sceneViews[0] as SceneView;
                sceneView.Close();
            }
#endif
            SceneManagement.SceneManager.LoadScene(testCase.ScenePath);

            // Always wait one frame for scene load
            yield return(null);

            var camera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent <Camera>();
            if (camera)
            {
                var vfxComponents = Resources.FindObjectsOfTypeAll <VisualEffect>();
#if UNITY_EDITOR
                var vfxAssets = vfxComponents.Select(o => o.visualEffectAsset).Where(o => o != null).Distinct();
                foreach (var vfx in vfxAssets)
                {
                    //Use Reflection as workaround of the access issue in .net 4 (TODO : Clean this as soon as possible)
                    //var graph = vfx.GetResource().GetOrCreateGraph(); is possible with .net 3.5 but compilation fail with 4.0
                    var visualEffectAssetExt = AppDomain.CurrentDomain.GetAssemblies().Select(o => o.GetType("UnityEditor.VFX.VisualEffectAssetExtensions"))
                                               .Where(o => o != null)
                                               .FirstOrDefault();
                    var fnGetResource = visualEffectAssetExt.GetMethod("GetResource");
                    var resource      = fnGetResource.Invoke(null, new object[] { vfx });
                    var fnGetOrCreate = visualEffectAssetExt.GetMethod("GetOrCreateGraph");
                    var graph         = fnGetOrCreate.Invoke(null, new object[] { resource }) as VFXGraph;
                    graph.RecompileIfNeeded();
                }
#endif

                var rt = RenderTexture.GetTemporary(captureSize, captureSize, 24);
                camera.targetTexture = rt;

                foreach (var component in vfxComponents)
                {
                    component.Reinit();
                }

                int waitFrameCount     = (int)(simulateTime / frequency);
                int startFrameIndex    = Time.frameCount;
                int expectedFrameIndex = startFrameIndex + waitFrameCount;
                while (Time.frameCount != expectedFrameIndex)
                {
                    yield return(null);
                }

                Texture2D actual = null;
                try
                {
                    camera.targetTexture = null;
                    actual = new Texture2D(captureSize, captureSize, TextureFormat.RGB24, false);
                    RenderTexture.active = rt;
                    actual.ReadPixels(new Rect(0, 0, captureSize, captureSize), 0, 0);
                    RenderTexture.active = null;
                    actual.Apply();

                    if (!ExcludedTestsButKeepLoadScene.Any(o => testCase.ScenePath.Contains(o)))
                    {
                        ImageAssert.AreEqual(testCase.ReferenceImage, actual, new ImageComparisonSettings()
                        {
                            AverageCorrectnessThreshold = 10e-5f
                        });
                    }
                    else
                    {
                        Debug.LogFormat("GraphicTest '{0}' result has been ignored", testCase.ReferenceImage);
                    }
                }
                finally
                {
                    RenderTexture.ReleaseTemporary(rt);
                    if (actual != null)
                    {
                        UnityEngine.Object.Destroy(actual);
                    }
                }
            }
        }