| | 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 | |
|
| | 5 | | using System.Diagnostics; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using GDX.Collections.Generic; |
| | 9 | | using UnityEngine; |
| | 10 | | using UnityEngine.SceneManagement; |
| | 11 | |
|
| | 12 | | namespace GDX.Developer.Reports.BuildVerification |
| | 13 | | { |
| | 14 | | public static class TestRunner |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// </summary> |
| | 18 | | const int SafeDelayTime = 1; |
| | 19 | |
|
| 0 | 20 | | static readonly object s_lockKnownTests = new object(); |
| 0 | 21 | | static SimpleList<ITestBehaviour> s_KnownTest = new SimpleList<ITestBehaviour>(10); |
| | 22 | |
|
| | 23 | | public static void AddTest(SimpleTestBehaviour simpleTest) |
| 0 | 24 | | { |
| 0 | 25 | | if (simpleTest == null) |
| 0 | 26 | | { |
| 0 | 27 | | return; |
| | 28 | | } |
| | 29 | |
|
| 0 | 30 | | lock (s_lockKnownTests) |
| 0 | 31 | | { |
| 0 | 32 | | if (!s_KnownTest.ContainsReference(simpleTest)) |
| 0 | 33 | | { |
| 0 | 34 | | s_KnownTest.AddWithExpandCheck(simpleTest); |
| 0 | 35 | | } |
| 0 | 36 | | } |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | public static async Task Execute(TestScene[] scenes) |
| 0 | 40 | | { |
| 0 | 41 | | for (int testSceneIndex = 0; testSceneIndex < scenes.Length; testSceneIndex++) |
| 0 | 42 | | { |
| 0 | 43 | | await Execute(scenes[testSceneIndex]); |
| 0 | 44 | | } |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | public static async Task Execute(TestScene testScene) |
| 0 | 48 | | { |
| 0 | 49 | | if (!testScene.IsValid()) |
| 0 | 50 | | { |
| 0 | 51 | | UnityEngine.Debug.LogWarning($"Invalid scene {testScene.BuildIndex.ToString()}."); |
| 0 | 52 | | Reset(); |
| 0 | 53 | | return; |
| | 54 | | } |
| | 55 | |
|
| 0 | 56 | | Stopwatch timeoutTimer = new Stopwatch(); |
| | 57 | |
|
| 0 | 58 | | UnityEngine.Debug.Log($"Load {testScene.ScenePath} ({testScene.BuildIndex.ToString()})"); |
| 0 | 59 | | AsyncOperation loadOperation = SceneManager.LoadSceneAsync(testScene.BuildIndex, LoadSceneMode.Additive); |
| 0 | 60 | | timeoutTimer.Restart(); |
| 0 | 61 | | if (loadOperation != null) |
| 0 | 62 | | { |
| 0 | 63 | | while (!loadOperation.isDone) |
| 0 | 64 | | { |
| 0 | 65 | | if (timeoutTimer.ElapsedMilliseconds < testScene.LoadTimeout) |
| 0 | 66 | | { |
| 0 | 67 | | await Task.Delay(SafeDelayTime); |
| 0 | 68 | | } |
| | 69 | | else |
| 0 | 70 | | { |
| 0 | 71 | | UnityEngine.Debug.LogError( |
| | 72 | | $"Failed to load {testScene.ScenePath} ({testScene.BuildIndex.ToString()})."); |
| 0 | 73 | | Reset(); |
| 0 | 74 | | return; |
| | 75 | | } |
| 0 | 76 | | } |
| 0 | 77 | | } |
| | 78 | |
|
| | 79 | | // Wait for all scenes to load and activate |
| 0 | 80 | | while (SceneExtensions.IsSceneManagerBusy()) |
| 0 | 81 | | { |
| 0 | 82 | | await Task.Delay(SafeDelayTime); |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | |
|
| | 86 | | // Wait for next update - super important around integration of loaded content |
| 0 | 87 | | UnityEngine.Debug.Log("Waiting at least frame ..."); |
| 0 | 88 | | float loadCurrentTime = Time.time; |
| 0 | 89 | | while (Time.time == loadCurrentTime) |
| 0 | 90 | | { |
| 0 | 91 | | await Task.Delay(SafeDelayTime); |
| 0 | 92 | | } |
| | 93 | |
|
| | 94 | | // Restart timer for timeout |
| 0 | 95 | | timeoutTimer.Restart(); |
| 0 | 96 | | while (HasRemainingTests()) |
| 0 | 97 | | { |
| 0 | 98 | | if (timeoutTimer.ElapsedMilliseconds < testScene.TestTimeout) |
| 0 | 99 | | { |
| 0 | 100 | | await Task.Delay(SafeDelayTime); |
| 0 | 101 | | } |
| | 102 | | else |
| 0 | 103 | | { |
| 0 | 104 | | UnityEngine.Debug.LogWarning( |
| | 105 | | $"Test run timed out after {(timeoutTimer.ElapsedMilliseconds / 1000f).ToString(CultureInfo.Curr |
| 0 | 106 | | for (int i = 0; i < s_KnownTest.Count; i++) |
| 0 | 107 | | { |
| 0 | 108 | | BuildVerificationReport.Assert(s_KnownTest.Array[i].GetIdentifier(), false, "Test timed out."); |
| 0 | 109 | | } |
| | 110 | |
|
| 0 | 111 | | Reset(); |
| 0 | 112 | | break; |
| | 113 | | } |
| 0 | 114 | | } |
| | 115 | |
|
| 0 | 116 | | UnityEngine.Debug.Log( "Waiting at least frame ..."); |
| 0 | 117 | | float testCurrentTime = Time.time; |
| 0 | 118 | | while (Time.time == testCurrentTime) |
| 0 | 119 | | { |
| 0 | 120 | | await Task.Delay(SafeDelayTime); |
| 0 | 121 | | } |
| | 122 | |
|
| 0 | 123 | | UnityEngine.Debug.Log($"Unload {testScene.ScenePath} ({testScene.BuildIndex.ToString()})"); |
| 0 | 124 | | AsyncOperation unloadOperation = SceneManager.UnloadSceneAsync(testScene.BuildIndex, |
| | 125 | | UnloadSceneOptions.UnloadAllEmbeddedSceneObjects); |
| 0 | 126 | | timeoutTimer.Restart(); |
| 0 | 127 | | if (unloadOperation != null) |
| 0 | 128 | | { |
| 0 | 129 | | while (!unloadOperation.isDone) |
| 0 | 130 | | { |
| 0 | 131 | | if (timeoutTimer.ElapsedMilliseconds < testScene.UnloadTimeout) |
| 0 | 132 | | { |
| 0 | 133 | | await Task.Delay(SafeDelayTime); |
| 0 | 134 | | } |
| | 135 | | else |
| 0 | 136 | | { |
| 0 | 137 | | UnityEngine.Debug.LogError($"Failed to unload {testScene.ScenePath} ({testScene.BuildIndex.ToStr |
| 0 | 138 | | Reset(); |
| 0 | 139 | | return; |
| | 140 | | } |
| 0 | 141 | | } |
| 0 | 142 | | } |
| | 143 | |
|
| | 144 | | // Wait for next update - super important around unloading |
| 0 | 145 | | UnityEngine.Debug.Log("Waiting at least frame ..."); |
| 0 | 146 | | float unloadCurrentTime = Time.time; |
| 0 | 147 | | while (Time.time == unloadCurrentTime) |
| 0 | 148 | | { |
| 0 | 149 | | await Task.Delay(SafeDelayTime); |
| 0 | 150 | | } |
| | 151 | |
|
| | 152 | | // Make sure we remove all registered as a safety precaution / will also stop the timer |
| 0 | 153 | | Reset(); |
| | 154 | |
|
| 0 | 155 | | UnityEngine.Debug.Log($"Test scene {testScene.ScenePath} ({testScene.BuildIndex.ToString()}) execution finis |
| 0 | 156 | | } |
| | 157 | |
|
| | 158 | |
|
| | 159 | | public static void RemoveTest(SimpleTestBehaviour simpleTest) |
| 0 | 160 | | { |
| 0 | 161 | | if (simpleTest == null) |
| 0 | 162 | | { |
| 0 | 163 | | return; |
| | 164 | | } |
| | 165 | |
|
| 0 | 166 | | lock (s_lockKnownTests) |
| 0 | 167 | | { |
| 0 | 168 | | s_KnownTest.RemoveFirstItem(simpleTest); |
| 0 | 169 | | } |
| 0 | 170 | | } |
| | 171 | |
|
| | 172 | | static bool HasRemainingTests() |
| 0 | 173 | | { |
| 0 | 174 | | lock (s_lockKnownTests) |
| 0 | 175 | | { |
| 0 | 176 | | return s_KnownTest.Count > 0; |
| | 177 | | } |
| 0 | 178 | | } |
| | 179 | |
|
| | 180 | | static void Reset() |
| 0 | 181 | | { |
| 0 | 182 | | lock (s_lockKnownTests) |
| 0 | 183 | | { |
| 0 | 184 | | s_KnownTest.Clear(); |
| 0 | 185 | | } |
| 0 | 186 | | } |
| | 187 | | } |
| | 188 | | } |