TestRunnerWindow.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using UnityEditor.Callbacks;
  3. using UnityEditor.TestTools.TestRunner.Api;
  4. using UnityEditor.TestTools.TestRunner.GUI;
  5. using UnityEngine;
  6. namespace UnityEditor.TestTools.TestRunner
  7. {
  8. [Serializable]
  9. /// <summary>
  10. /// The TestRunnerWindow class is repsonsible for drawing the Test Runner window.
  11. /// </summary>
  12. public class TestRunnerWindow : EditorWindow, IHasCustomMenu
  13. {
  14. internal static class Styles
  15. {
  16. public static GUIStyle info;
  17. public static GUIStyle testList;
  18. static Styles()
  19. {
  20. info = new GUIStyle(EditorStyles.wordWrappedLabel);
  21. info.wordWrap = false;
  22. info.stretchHeight = true;
  23. info.margin.right = 15;
  24. testList = new GUIStyle("CN Box");
  25. testList.margin.top = 0;
  26. testList.padding.left = 3;
  27. }
  28. }
  29. private readonly GUIContent m_GUIHorizontalSplit = EditorGUIUtility.TrTextContent("Horizontal layout");
  30. private readonly GUIContent m_GUIVerticalSplit = EditorGUIUtility.TrTextContent("Vertical layout");
  31. private readonly GUIContent m_GUIEnableaPlaymodeTestsRunner = EditorGUIUtility.TrTextContent("Enable playmode tests for all assemblies");
  32. private readonly GUIContent m_GUIDisablePlaymodeTestsRunner = EditorGUIUtility.TrTextContent("Disable playmode tests for all assemblies");
  33. private readonly GUIContent m_GUIRunPlayModeTestAsEditModeTests = EditorGUIUtility.TrTextContent("Run playmode tests as editmode tests");
  34. internal static TestRunnerWindow s_Instance;
  35. private bool m_IsBuilding;
  36. [NonSerialized]
  37. private bool m_Enabled;
  38. internal TestFilterSettings filterSettings;
  39. [SerializeField]
  40. private SplitterState m_Spl = new SplitterState(new float[] { 75, 25 }, new[] { 32, 32 }, null);
  41. private TestRunnerWindowSettings m_Settings;
  42. private enum TestRunnerMenuLabels
  43. {
  44. PlayMode = 0,
  45. EditMode = 1
  46. }
  47. [SerializeField]
  48. private int m_TestTypeToolbarIndex = (int)TestRunnerMenuLabels.EditMode;
  49. [SerializeField]
  50. private PlayModeTestListGUI m_PlayModeTestListGUI;
  51. [SerializeField]
  52. private EditModeTestListGUI m_EditModeTestListGUI;
  53. internal TestListGUI m_SelectedTestTypes;
  54. private ITestRunnerApi m_testRunnerApi;
  55. private WindowResultUpdater m_WindowResultUpdater;
  56. [MenuItem("Window/General/Test Runner", false, 201, false)]
  57. /// <summary>
  58. /// Launches the Test Runner window.
  59. /// </summary>
  60. public static void ShowWindow()
  61. {
  62. s_Instance = GetWindow<TestRunnerWindow>("Test Runner");
  63. s_Instance.Show();
  64. }
  65. internal static void ShowPlaymodeTestsRunnerWindowCodeBased()
  66. {
  67. ShowWindow();
  68. }
  69. static TestRunnerWindow()
  70. {
  71. InitBackgroundRunners();
  72. }
  73. private static void InitBackgroundRunners()
  74. {
  75. EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
  76. EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
  77. }
  78. [DidReloadScripts]
  79. private static void CompilationCallback()
  80. {
  81. UpdateWindow();
  82. }
  83. private static void OnPlayModeStateChanged(PlayModeStateChange state)
  84. {
  85. if (s_Instance && state == PlayModeStateChange.EnteredEditMode && s_Instance.m_SelectedTestTypes.HasTreeData())
  86. {
  87. //repaint message details after exit playmode
  88. s_Instance.m_SelectedTestTypes.TestSelectionCallback(s_Instance.m_SelectedTestTypes.m_TestListState.selectedIDs.ToArray());
  89. s_Instance.Repaint();
  90. }
  91. }
  92. internal void OnDestroy()
  93. {
  94. EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
  95. }
  96. private void OnEnable()
  97. {
  98. s_Instance = this;
  99. SelectTestListGUI(m_TestTypeToolbarIndex);
  100. m_testRunnerApi = ScriptableObject.CreateInstance<TestRunnerApi>();
  101. m_WindowResultUpdater = new WindowResultUpdater();
  102. m_testRunnerApi.RegisterCallbacks(m_WindowResultUpdater);
  103. }
  104. private void Enable()
  105. {
  106. m_Settings = new TestRunnerWindowSettings("UnityEditor.PlaymodeTestsRunnerWindow");
  107. filterSettings = new TestFilterSettings("UnityTest.IntegrationTestsRunnerWindow");
  108. if (m_SelectedTestTypes == null)
  109. {
  110. SelectTestListGUI(m_TestTypeToolbarIndex);
  111. }
  112. StartRetrieveTestList();
  113. m_SelectedTestTypes.Reload();
  114. m_Enabled = true;
  115. }
  116. private void SelectTestListGUI(int testTypeToolbarIndex)
  117. {
  118. if (testTypeToolbarIndex == (int)TestRunnerMenuLabels.PlayMode)
  119. {
  120. if (m_PlayModeTestListGUI == null)
  121. {
  122. m_PlayModeTestListGUI = new PlayModeTestListGUI();
  123. }
  124. m_SelectedTestTypes = m_PlayModeTestListGUI;
  125. }
  126. else if (testTypeToolbarIndex == (int)TestRunnerMenuLabels.EditMode)
  127. {
  128. if (m_EditModeTestListGUI == null)
  129. {
  130. m_EditModeTestListGUI = new EditModeTestListGUI();
  131. }
  132. m_SelectedTestTypes = m_EditModeTestListGUI;
  133. }
  134. }
  135. private void StartRetrieveTestList()
  136. {
  137. if (!m_SelectedTestTypes.HasTreeData())
  138. {
  139. var listToInit = m_SelectedTestTypes;
  140. m_testRunnerApi.RetrieveTestList(m_SelectedTestTypes.TestMode, (rootTest) =>
  141. {
  142. listToInit.Init(this, rootTest);
  143. listToInit.Reload();
  144. });
  145. }
  146. }
  147. internal void OnGUI()
  148. {
  149. if (!m_Enabled)
  150. {
  151. Enable();
  152. }
  153. if (BuildPipeline.isBuildingPlayer)
  154. {
  155. m_IsBuilding = true;
  156. }
  157. else if (m_IsBuilding)
  158. {
  159. m_IsBuilding = false;
  160. Repaint();
  161. }
  162. EditorGUILayout.BeginHorizontal();
  163. GUILayout.FlexibleSpace();
  164. var selectedIndex = m_TestTypeToolbarIndex;
  165. m_TestTypeToolbarIndex = GUILayout.Toolbar(m_TestTypeToolbarIndex, Enum.GetNames(typeof(TestRunnerMenuLabels)), "LargeButton", UnityEngine.GUI.ToolbarButtonSize.FitToContents);
  166. GUILayout.FlexibleSpace();
  167. EditorGUILayout.EndHorizontal();
  168. if (selectedIndex != m_TestTypeToolbarIndex)
  169. {
  170. SelectTestListGUI(m_TestTypeToolbarIndex);
  171. StartRetrieveTestList();
  172. }
  173. EditorGUILayout.BeginVertical();
  174. using (new EditorGUI.DisabledScope(EditorApplication.isPlayingOrWillChangePlaymode))
  175. {
  176. m_SelectedTestTypes.PrintHeadPanel();
  177. }
  178. EditorGUILayout.EndVertical();
  179. if (m_Settings.verticalSplit)
  180. SplitterGUILayout.BeginVerticalSplit(m_Spl);
  181. else
  182. SplitterGUILayout.BeginHorizontalSplit(m_Spl);
  183. EditorGUILayout.BeginVertical();
  184. EditorGUILayout.BeginVertical(Styles.testList);
  185. m_SelectedTestTypes.RenderTestList();
  186. EditorGUILayout.EndVertical();
  187. EditorGUILayout.EndVertical();
  188. m_SelectedTestTypes.RenderDetails();
  189. if (m_Settings.verticalSplit)
  190. SplitterGUILayout.EndVerticalSplit();
  191. else
  192. SplitterGUILayout.EndHorizontalSplit();
  193. }
  194. /// <summary>
  195. /// Adds additional menu items to the Test Runner window.
  196. /// </summary>
  197. /// <param name="menu">The <see cref="GenericMenu"/></param>
  198. public void AddItemsToMenu(GenericMenu menu)
  199. {
  200. menu.AddItem(m_GUIVerticalSplit, m_Settings.verticalSplit, m_Settings.ToggleVerticalSplit);
  201. menu.AddItem(m_GUIHorizontalSplit, !m_Settings.verticalSplit, m_Settings.ToggleVerticalSplit);
  202. menu.AddSeparator(null);
  203. var playModeTestRunnerEnabled = PlayerSettings.playModeTestRunnerEnabled;
  204. var currentActive = playModeTestRunnerEnabled ? m_GUIDisablePlaymodeTestsRunner : m_GUIEnableaPlaymodeTestsRunner;
  205. if (EditorPrefs.GetBool("InternalMode", false))
  206. {
  207. menu.AddItem(m_GUIRunPlayModeTestAsEditModeTests, PlayerSettings.runPlayModeTestAsEditModeTest, () =>
  208. {
  209. PlayerSettings.runPlayModeTestAsEditModeTest = !PlayerSettings.runPlayModeTestAsEditModeTest;
  210. });
  211. }
  212. menu.AddItem(currentActive, false, () =>
  213. {
  214. PlayerSettings.playModeTestRunnerEnabled = !playModeTestRunnerEnabled;
  215. EditorUtility.DisplayDialog(currentActive.text, "You need to restart the editor now", "Ok");
  216. });
  217. }
  218. internal void RebuildUIFilter()
  219. {
  220. if (m_SelectedTestTypes != null && m_SelectedTestTypes.HasTreeData())
  221. {
  222. m_SelectedTestTypes.RebuildUIFilter();
  223. }
  224. }
  225. internal static void UpdateWindow()
  226. {
  227. if (s_Instance != null && s_Instance.m_SelectedTestTypes != null)
  228. {
  229. s_Instance.m_SelectedTestTypes.Repaint();
  230. s_Instance.Repaint();
  231. }
  232. }
  233. }
  234. }