暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TestRunnerWindow.cs 9.1KB

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