TestRunnerWindow.cs 9.0 KB

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