123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- 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
- {
- foreach (var obj in FindObjectsOfType<GameObject>())
- {
- if (obj != null && obj.transform.parent != null && (obj.transform.parent.hideFlags & HideFlags.DontSaveInEditor) == HideFlags.DontSaveInEditor && obj.transform.parent.gameObject != null)
- {
- DestroyImmediate(obj.transform.parent.gameObject);
- }
- }
-
- 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;
- }
- }
- }
|