< Summary

Class:GDX.Developer.Reports.BuildVerification.TestRunner
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Developer/Reports/BuildVerification/TestRunner.cs
Covered lines:0
Uncovered lines:122
Coverable lines:122
Total lines:188
Line coverage:0% (0 of 122)
Covered branches:0
Total branches:0
Covered methods:0
Total methods:7
Method coverage:0% (0 of 7)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
TestRunner()0%2100%
AddTest(...)0%20400%
Execute()0%20400%
Execute()0%9303000%
RemoveTest(...)0%12300%
HasRemainingTests()0%6200%
Reset()0%6200%

File(s)

./Packages/com.dotbunny.gdx/GDX/Developer/Reports/BuildVerification/TestRunner.cs

#LineLine coverage
 1// Copyright (c) 2020-2024 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System.Diagnostics;
 6using System.Globalization;
 7using System.Threading.Tasks;
 8using GDX.Collections.Generic;
 9using UnityEngine;
 10using UnityEngine.SceneManagement;
 11
 12namespace GDX.Developer.Reports.BuildVerification
 13{
 14    public static class TestRunner
 15    {
 16        /// <summary>
 17        /// </summary>
 18        const int SafeDelayTime = 1;
 19
 020        static readonly object s_lockKnownTests = new object();
 021        static SimpleList<ITestBehaviour> s_KnownTest = new SimpleList<ITestBehaviour>(10);
 22
 23        public static void AddTest(SimpleTestBehaviour simpleTest)
 024        {
 025            if (simpleTest == null)
 026            {
 027                return;
 28            }
 29
 030            lock (s_lockKnownTests)
 031            {
 032                if (!s_KnownTest.ContainsReference(simpleTest))
 033                {
 034                    s_KnownTest.AddWithExpandCheck(simpleTest);
 035                }
 036            }
 037        }
 38
 39        public static async Task Execute(TestScene[] scenes)
 040        {
 041            for (int testSceneIndex = 0; testSceneIndex < scenes.Length; testSceneIndex++)
 042            {
 043                await Execute(scenes[testSceneIndex]);
 044            }
 045        }
 46
 47        public static async Task Execute(TestScene testScene)
 048        {
 049            if (!testScene.IsValid())
 050            {
 051                UnityEngine.Debug.LogWarning($"Invalid scene {testScene.BuildIndex.ToString()}.");
 052                Reset();
 053                return;
 54            }
 55
 056            Stopwatch timeoutTimer = new Stopwatch();
 57
 058            UnityEngine.Debug.Log($"Load {testScene.ScenePath} ({testScene.BuildIndex.ToString()})");
 059            AsyncOperation loadOperation = SceneManager.LoadSceneAsync(testScene.BuildIndex, LoadSceneMode.Additive);
 060            timeoutTimer.Restart();
 061            if (loadOperation != null)
 062            {
 063                while (!loadOperation.isDone)
 064                {
 065                    if (timeoutTimer.ElapsedMilliseconds < testScene.LoadTimeout)
 066                    {
 067                        await Task.Delay(SafeDelayTime);
 068                    }
 69                    else
 070                    {
 071                        UnityEngine.Debug.LogError(
 72                            $"Failed to load {testScene.ScenePath} ({testScene.BuildIndex.ToString()}).");
 073                        Reset();
 074                        return;
 75                    }
 076                }
 077            }
 78
 79            // Wait for all scenes to load and activate
 080            while (SceneExtensions.IsSceneManagerBusy())
 081            {
 082                await Task.Delay(SafeDelayTime);
 083            }
 84
 85
 86            // Wait for next update - super important around integration of loaded content
 087            UnityEngine.Debug.Log("Waiting at least frame ...");
 088            float loadCurrentTime = Time.time;
 089            while (Time.time == loadCurrentTime)
 090            {
 091                await Task.Delay(SafeDelayTime);
 092            }
 93
 94            // Restart timer for timeout
 095            timeoutTimer.Restart();
 096            while (HasRemainingTests())
 097            {
 098                if (timeoutTimer.ElapsedMilliseconds < testScene.TestTimeout)
 099                {
 0100                    await Task.Delay(SafeDelayTime);
 0101                }
 102                else
 0103                {
 0104                    UnityEngine.Debug.LogWarning(
 105                        $"Test run timed out after {(timeoutTimer.ElapsedMilliseconds / 1000f).ToString(CultureInfo.Curr
 0106                    for (int i = 0; i < s_KnownTest.Count; i++)
 0107                    {
 0108                        BuildVerificationReport.Assert(s_KnownTest.Array[i].GetIdentifier(), false, "Test timed out.");
 0109                    }
 110
 0111                    Reset();
 0112                    break;
 113                }
 0114            }
 115
 0116            UnityEngine.Debug.Log( "Waiting at least frame ...");
 0117            float testCurrentTime = Time.time;
 0118            while (Time.time == testCurrentTime)
 0119            {
 0120                await Task.Delay(SafeDelayTime);
 0121            }
 122
 0123            UnityEngine.Debug.Log($"Unload {testScene.ScenePath} ({testScene.BuildIndex.ToString()})");
 0124            AsyncOperation unloadOperation = SceneManager.UnloadSceneAsync(testScene.BuildIndex,
 125                UnloadSceneOptions.UnloadAllEmbeddedSceneObjects);
 0126            timeoutTimer.Restart();
 0127            if (unloadOperation != null)
 0128            {
 0129                while (!unloadOperation.isDone)
 0130                {
 0131                    if (timeoutTimer.ElapsedMilliseconds < testScene.UnloadTimeout)
 0132                    {
 0133                        await Task.Delay(SafeDelayTime);
 0134                    }
 135                    else
 0136                    {
 0137                        UnityEngine.Debug.LogError($"Failed to unload {testScene.ScenePath} ({testScene.BuildIndex.ToStr
 0138                        Reset();
 0139                        return;
 140                    }
 0141                }
 0142            }
 143
 144            // Wait for next update - super important around unloading
 0145            UnityEngine.Debug.Log("Waiting at least frame ...");
 0146            float unloadCurrentTime = Time.time;
 0147            while (Time.time == unloadCurrentTime)
 0148            {
 0149                await Task.Delay(SafeDelayTime);
 0150            }
 151
 152            // Make sure we remove all registered as a safety precaution / will also stop the timer
 0153            Reset();
 154
 0155            UnityEngine.Debug.Log($"Test scene {testScene.ScenePath} ({testScene.BuildIndex.ToString()}) execution finis
 0156        }
 157
 158
 159        public static void RemoveTest(SimpleTestBehaviour simpleTest)
 0160        {
 0161            if (simpleTest == null)
 0162            {
 0163                return;
 164            }
 165
 0166            lock (s_lockKnownTests)
 0167            {
 0168                s_KnownTest.RemoveFirstItem(simpleTest);
 0169            }
 0170        }
 171
 172        static bool HasRemainingTests()
 0173        {
 0174            lock (s_lockKnownTests)
 0175            {
 0176                return s_KnownTest.Count > 0;
 177            }
 0178        }
 179
 180        static void Reset()
 0181        {
 0182            lock (s_lockKnownTests)
 0183            {
 0184                s_KnownTest.Clear();
 0185            }
 0186        }
 187    }
 188}