EditModeRunnerCallback.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using NUnit.Framework;
  5. using NUnit.Framework.Interfaces;
  6. using NUnit.Framework.Internal;
  7. using UnityEditor.SceneManagement;
  8. using UnityEngine;
  9. using UnityEngine.TestTools.TestRunner;
  10. namespace UnityEditor.TestTools.TestRunner
  11. {
  12. internal class EditModeRunnerCallback : ScriptableObject, ITestRunnerListener
  13. {
  14. private EditModeLauncherContextSettings m_Settings;
  15. public SceneSetup[] previousSceneSetup;
  16. public EditModeRunner runner;
  17. private bool m_Canceled;
  18. private ITest m_CurrentTest;
  19. private int m_TotalTests;
  20. [SerializeField]
  21. private List<string> m_PendingTests;
  22. [SerializeField]
  23. private string m_LastCountedTestName;
  24. [SerializeField]
  25. private bool m_RunRestarted;
  26. public void OnDestroy()
  27. {
  28. CleanUp();
  29. }
  30. public void RunStarted(ITest testsToRun)
  31. {
  32. Setup();
  33. if (m_PendingTests == null)
  34. {
  35. m_PendingTests = GetTestsExpectedToRun(testsToRun, runner.GetFilter());
  36. m_TotalTests = m_PendingTests.Count;
  37. }
  38. }
  39. public void OnEnable()
  40. {
  41. if (m_RunRestarted)
  42. {
  43. Setup();
  44. }
  45. }
  46. private void Setup()
  47. {
  48. m_Settings = new EditModeLauncherContextSettings();
  49. Application.logMessageReceivedThreaded += LogReceived;
  50. EditorApplication.playModeStateChanged += WaitForExitPlaymode;
  51. EditorApplication.update += DisplayProgressBar;
  52. AssemblyReloadEvents.beforeAssemblyReload += BeforeAssemblyReload;
  53. }
  54. private void BeforeAssemblyReload()
  55. {
  56. if (m_CurrentTest != null)
  57. {
  58. m_LastCountedTestName = m_CurrentTest.FullName;
  59. m_RunRestarted = true;
  60. }
  61. }
  62. private void DisplayProgressBar()
  63. {
  64. if (m_CurrentTest == null)
  65. return;
  66. if (!m_Canceled && EditorUtility.DisplayCancelableProgressBar("Test Runner", "Running test " + m_CurrentTest.Name, Math.Min(1.0f, (float)(m_TotalTests - m_PendingTests.Count) / m_TotalTests)))
  67. {
  68. EditorApplication.update -= DisplayProgressBar;
  69. m_Canceled = true;
  70. EditorUtility.ClearProgressBar();
  71. runner.OnRunCancel();
  72. }
  73. }
  74. private static void LogReceived(string message, string stacktrace, LogType type)
  75. {
  76. if (TestContext.Out != null)
  77. TestContext.Out.WriteLine(message);
  78. }
  79. private static void WaitForExitPlaymode(PlayModeStateChange state)
  80. {
  81. if (state == PlayModeStateChange.EnteredEditMode)
  82. {
  83. EditorApplication.playModeStateChanged -= WaitForExitPlaymode;
  84. //because logMessage is reset on Enter EditMode
  85. //we remove and add the callback
  86. //because Unity
  87. Application.logMessageReceivedThreaded -= LogReceived;
  88. Application.logMessageReceivedThreaded += LogReceived;
  89. }
  90. }
  91. public void RunFinished(ITestResult result)
  92. {
  93. if (previousSceneSetup != null && previousSceneSetup.Length > 0)
  94. {
  95. try
  96. {
  97. EditorSceneManager.RestoreSceneManagerSetup(previousSceneSetup);
  98. }
  99. catch (ArgumentException e)
  100. {
  101. Debug.LogWarning(e.Message);
  102. }
  103. }
  104. else
  105. {
  106. foreach (var obj in FindObjectsOfType<GameObject>())
  107. {
  108. if (obj != null && obj.transform.parent != null && (obj.transform.parent.hideFlags & HideFlags.DontSaveInEditor) == HideFlags.DontSaveInEditor && obj.transform.parent.gameObject != null)
  109. {
  110. DestroyImmediate(obj.transform.parent.gameObject);
  111. }
  112. }
  113. EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects, NewSceneMode.Single);
  114. }
  115. CleanUp();
  116. }
  117. private void CleanUp()
  118. {
  119. m_CurrentTest = null;
  120. EditorUtility.ClearProgressBar();
  121. if (m_Settings != null)
  122. {
  123. m_Settings.Dispose();
  124. }
  125. Application.logMessageReceivedThreaded -= LogReceived;
  126. EditorApplication.update -= DisplayProgressBar;
  127. }
  128. public void TestStarted(ITest test)
  129. {
  130. if (test.IsSuite || !(test is TestMethod))
  131. {
  132. return;
  133. }
  134. m_CurrentTest = test;
  135. if (m_RunRestarted)
  136. {
  137. if (test.FullName == m_LastCountedTestName)
  138. m_RunRestarted = false;
  139. }
  140. }
  141. public void TestFinished(ITestResult result)
  142. {
  143. if (result.Test is TestMethod)
  144. {
  145. m_PendingTests.Remove(result.Test.FullName);
  146. }
  147. }
  148. private static List<string> GetTestsExpectedToRun(ITest test, ITestFilter filter)
  149. {
  150. var expectedTests = new List<string>();
  151. if (filter.Pass(test))
  152. {
  153. if (test.IsSuite)
  154. {
  155. expectedTests.AddRange(test.Tests.SelectMany(subTest => GetTestsExpectedToRun(subTest, filter)));
  156. }
  157. else
  158. {
  159. expectedTests.Add(test.FullName);
  160. }
  161. }
  162. return expectedTests;
  163. }
  164. }
  165. }