summaryrefslogtreecommitdiff
path: root/Library/PackageCache/com.unity.test-framework@1.1.11/UnityEditor.TestRunner/TestRunner/Callbacks/EditModeRunnerCallback.cs
diff options
context:
space:
mode:
authorAndrew Lee <alee14498@protonmail.com>2020-04-19 17:19:32 -0400
committerAndrew Lee <alee14498@protonmail.com>2020-04-19 17:19:32 -0400
commitc55fba8ab2a1c9d3df65eda4a5a1e957f4aa1f78 (patch)
treeee4d51c7c1d633e11f46453ef1edd3c77c4ef9f7 /Library/PackageCache/com.unity.test-framework@1.1.11/UnityEditor.TestRunner/TestRunner/Callbacks/EditModeRunnerCallback.cs
downloadProject-Sandbox-c55fba8ab2a1c9d3df65eda4a5a1e957f4aa1f78.tar.gz
Project-Sandbox-c55fba8ab2a1c9d3df65eda4a5a1e957f4aa1f78.tar.bz2
Project-Sandbox-c55fba8ab2a1c9d3df65eda4a5a1e957f4aa1f78.zip
Inital commit
Diffstat (limited to 'Library/PackageCache/com.unity.test-framework@1.1.11/UnityEditor.TestRunner/TestRunner/Callbacks/EditModeRunnerCallback.cs')
-rw-r--r--Library/PackageCache/com.unity.test-framework@1.1.11/UnityEditor.TestRunner/TestRunner/Callbacks/EditModeRunnerCallback.cs178
1 files changed, 178 insertions, 0 deletions
diff --git a/Library/PackageCache/com.unity.test-framework@1.1.11/UnityEditor.TestRunner/TestRunner/Callbacks/EditModeRunnerCallback.cs b/Library/PackageCache/com.unity.test-framework@1.1.11/UnityEditor.TestRunner/TestRunner/Callbacks/EditModeRunnerCallback.cs
new file mode 100644
index 0000000..7d026fa
--- /dev/null
+++ b/Library/PackageCache/com.unity.test-framework@1.1.11/UnityEditor.TestRunner/TestRunner/Callbacks/EditModeRunnerCallback.cs
@@ -0,0 +1,178 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using NUnit.Framework;
+using NUnit.Framework.Interfaces;
+using NUnit.Framework.Internal;
+using UnityEditor.SceneManagement;
+using UnityEngine;
+using UnityEngine.TestTools.TestRunner;
+
+namespace UnityEditor.TestTools.TestRunner
+{
+ internal class EditModeRunnerCallback : ScriptableObject, ITestRunnerListener
+ {
+ private EditModeLauncherContextSettings m_Settings;
+ public SceneSetup[] previousSceneSetup;
+ public EditModeRunner runner;
+
+ private bool m_Canceled;
+ private ITest m_CurrentTest;
+ private int m_TotalTests;
+
+ [SerializeField]
+ private List<string> m_PendingTests;
+ [SerializeField]
+ private string m_LastCountedTestName;
+ [SerializeField]
+ private bool m_RunRestarted;
+
+ public void OnDestroy()
+ {
+ CleanUp();
+ }
+
+ public void RunStarted(ITest testsToRun)
+ {
+ Setup();
+ if (m_PendingTests == null)
+ {
+ m_PendingTests = GetTestsExpectedToRun(testsToRun, runner.GetFilter());
+ m_TotalTests = m_PendingTests.Count;
+ }
+ }
+
+ public void OnEnable()
+ {
+ if (m_RunRestarted)
+ {
+ Setup();
+ }
+ }
+
+ private void Setup()
+ {
+ m_Settings = new EditModeLauncherContextSettings();
+ Application.logMessageReceivedThreaded += LogReceived;
+ EditorApplication.playModeStateChanged += WaitForExitPlaymode;
+ EditorApplication.update += DisplayProgressBar;
+ AssemblyReloadEvents.beforeAssemblyReload += BeforeAssemblyReload;
+ }
+
+ private void BeforeAssemblyReload()
+ {
+ if (m_CurrentTest != null)
+ {
+ m_LastCountedTestName = m_CurrentTest.FullName;
+ m_RunRestarted = true;
+ }
+ }
+
+ private void DisplayProgressBar()
+ {
+ if (m_CurrentTest == null)
+ return;
+ if (!m_Canceled && EditorUtility.DisplayCancelableProgressBar("Test Runner", "Running test " + m_CurrentTest.Name, Math.Min(1.0f, (float)(m_TotalTests - m_PendingTests.Count) / m_TotalTests)))
+ {
+ EditorApplication.update -= DisplayProgressBar;
+ m_Canceled = true;
+ EditorUtility.ClearProgressBar();
+ runner.OnRunCancel();
+ }
+ }
+
+ private static void LogReceived(string message, string stacktrace, LogType type)
+ {
+ if (TestContext.Out != null)
+ TestContext.Out.WriteLine(message);
+ }
+
+ private static void WaitForExitPlaymode(PlayModeStateChange state)
+ {
+ if (state == PlayModeStateChange.EnteredEditMode)
+ {
+ EditorApplication.playModeStateChanged -= WaitForExitPlaymode;
+ //because logMessage is reset on Enter EditMode
+ //we remove and add the callback
+ //because Unity
+ Application.logMessageReceivedThreaded -= LogReceived;
+ Application.logMessageReceivedThreaded += LogReceived;
+ }
+ }
+
+ public void RunFinished(ITestResult result)
+ {
+ if (previousSceneSetup != null && previousSceneSetup.Length > 0)
+ {
+ try
+ {
+ EditorSceneManager.RestoreSceneManagerSetup(previousSceneSetup);
+ }
+ catch (ArgumentException e)
+ {
+ Debug.LogWarning(e.Message);
+ }
+ }
+ else
+ {
+ EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects, NewSceneMode.Single);
+ }
+ CleanUp();
+ }
+
+ private void CleanUp()
+ {
+ m_CurrentTest = null;
+ EditorUtility.ClearProgressBar();
+ if (m_Settings != null)
+ {
+ m_Settings.Dispose();
+ }
+ Application.logMessageReceivedThreaded -= LogReceived;
+ EditorApplication.update -= DisplayProgressBar;
+ }
+
+ public void TestStarted(ITest test)
+ {
+ if (test.IsSuite || !(test is TestMethod))
+ {
+ return;
+ }
+
+ m_CurrentTest = test;
+
+ if (m_RunRestarted)
+ {
+ if (test.FullName == m_LastCountedTestName)
+ m_RunRestarted = false;
+ }
+ }
+
+ public void TestFinished(ITestResult result)
+ {
+ if (result.Test is TestMethod)
+ {
+ m_PendingTests.Remove(result.Test.FullName);
+ }
+ }
+
+ private static List<string> GetTestsExpectedToRun(ITest test, ITestFilter filter)
+ {
+ var expectedTests = new List<string>();
+
+ if (filter.Pass(test))
+ {
+ if (test.IsSuite)
+ {
+ expectedTests.AddRange(test.Tests.SelectMany(subTest => GetTestsExpectedToRun(subTest, filter)));
+ }
+ else
+ {
+ expectedTests.Add(test.FullName);
+ }
+ }
+
+ return expectedTests;
+ }
+ }
+}