SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

263 lines
9.2 KiB

4 years ago
  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. if (m_testRunnerApi != null)
  86. {
  87. m_testRunnerApi.UnregisterCallbacks(m_WindowResultUpdater);
  88. }
  89. }
  90. private void OnEnable()
  91. {
  92. s_Instance = this;
  93. SelectTestListGUI(m_TestTypeToolbarIndex);
  94. m_testRunnerApi = ScriptableObject.CreateInstance<TestRunnerApi>();
  95. m_WindowResultUpdater = new WindowResultUpdater();
  96. m_testRunnerApi.RegisterCallbacks(m_WindowResultUpdater);
  97. }
  98. private void Enable()
  99. {
  100. m_Settings = new TestRunnerWindowSettings("UnityEditor.PlaymodeTestsRunnerWindow");
  101. filterSettings = new TestFilterSettings("UnityTest.IntegrationTestsRunnerWindow");
  102. if (m_SelectedTestTypes == null)
  103. {
  104. SelectTestListGUI(m_TestTypeToolbarIndex);
  105. }
  106. StartRetrieveTestList();
  107. m_SelectedTestTypes.Reload();
  108. m_Enabled = true;
  109. }
  110. private void SelectTestListGUI(int testTypeToolbarIndex)
  111. {
  112. if (testTypeToolbarIndex == (int)TestRunnerMenuLabels.PlayMode)
  113. {
  114. if (m_PlayModeTestListGUI == null)
  115. {
  116. m_PlayModeTestListGUI = new PlayModeTestListGUI();
  117. }
  118. m_SelectedTestTypes = m_PlayModeTestListGUI;
  119. }
  120. else if (testTypeToolbarIndex == (int)TestRunnerMenuLabels.EditMode)
  121. {
  122. if (m_EditModeTestListGUI == null)
  123. {
  124. m_EditModeTestListGUI = new EditModeTestListGUI();
  125. }
  126. m_SelectedTestTypes = m_EditModeTestListGUI;
  127. }
  128. }
  129. private void StartRetrieveTestList()
  130. {
  131. if (!m_SelectedTestTypes.HasTreeData())
  132. {
  133. m_testRunnerApi.RetrieveTestList(m_SelectedTestTypes.TestMode, (rootTest) =>
  134. {
  135. m_SelectedTestTypes.Init(this, rootTest);
  136. m_SelectedTestTypes.Reload();
  137. });
  138. }
  139. }
  140. public void OnGUI()
  141. {
  142. if (!m_Enabled)
  143. {
  144. Enable();
  145. }
  146. if (BuildPipeline.isBuildingPlayer)
  147. {
  148. m_IsBuilding = true;
  149. }
  150. else if (m_IsBuilding)
  151. {
  152. m_IsBuilding = false;
  153. Repaint();
  154. }
  155. EditorGUILayout.BeginHorizontal();
  156. GUILayout.FlexibleSpace();
  157. var selectedIndex = m_TestTypeToolbarIndex;
  158. m_TestTypeToolbarIndex = GUILayout.Toolbar(m_TestTypeToolbarIndex, Enum.GetNames(typeof(TestRunnerMenuLabels)), "LargeButton", UnityEngine.GUI.ToolbarButtonSize.FitToContents);
  159. GUILayout.FlexibleSpace();
  160. EditorGUILayout.EndHorizontal();
  161. if (selectedIndex != m_TestTypeToolbarIndex)
  162. {
  163. SelectTestListGUI(m_TestTypeToolbarIndex);
  164. StartRetrieveTestList();
  165. }
  166. EditorGUILayout.BeginVertical();
  167. using (new EditorGUI.DisabledScope(EditorApplication.isPlayingOrWillChangePlaymode))
  168. {
  169. m_SelectedTestTypes.PrintHeadPanel();
  170. }
  171. EditorGUILayout.EndVertical();
  172. if (m_Settings.verticalSplit)
  173. SplitterGUILayout.BeginVerticalSplit(m_Spl);
  174. else
  175. SplitterGUILayout.BeginHorizontalSplit(m_Spl);
  176. EditorGUILayout.BeginVertical();
  177. EditorGUILayout.BeginVertical(Styles.testList);
  178. m_SelectedTestTypes.RenderTestList();
  179. EditorGUILayout.EndVertical();
  180. EditorGUILayout.EndVertical();
  181. m_SelectedTestTypes.RenderDetails();
  182. if (m_Settings.verticalSplit)
  183. SplitterGUILayout.EndVerticalSplit();
  184. else
  185. SplitterGUILayout.EndHorizontalSplit();
  186. }
  187. public void AddItemsToMenu(GenericMenu menu)
  188. {
  189. menu.AddItem(m_GUIVerticalSplit, m_Settings.verticalSplit, m_Settings.ToggleVerticalSplit);
  190. menu.AddItem(m_GUIHorizontalSplit, !m_Settings.verticalSplit, m_Settings.ToggleVerticalSplit);
  191. menu.AddSeparator(null);
  192. var playModeTestRunnerEnabled = PlayerSettings.playModeTestRunnerEnabled;
  193. var currentActive = playModeTestRunnerEnabled ? m_GUIDisablePlaymodeTestsRunner : m_GUIEnableaPlaymodeTestsRunner;
  194. if (EditorPrefs.GetBool("InternalMode", false))
  195. {
  196. menu.AddItem(m_GUIRunPlayModeTestAsEditModeTests, PlayerSettings.runPlayModeTestAsEditModeTest, () =>
  197. {
  198. PlayerSettings.runPlayModeTestAsEditModeTest = !PlayerSettings.runPlayModeTestAsEditModeTest;
  199. });
  200. }
  201. menu.AddItem(currentActive, false, () =>
  202. {
  203. PlayerSettings.playModeTestRunnerEnabled = !playModeTestRunnerEnabled;
  204. EditorUtility.DisplayDialog(currentActive.text, "You need to restart the editor now", "Ok");
  205. });
  206. }
  207. public void RebuildUIFilter()
  208. {
  209. if (m_SelectedTestTypes != null && m_SelectedTestTypes.HasTreeData())
  210. {
  211. m_SelectedTestTypes.RebuildUIFilter();
  212. }
  213. }
  214. public static void UpdateWindow()
  215. {
  216. if (s_Instance != null && s_Instance.m_SelectedTestTypes != null)
  217. {
  218. s_Instance.m_SelectedTestTypes.Repaint();
  219. s_Instance.Repaint();
  220. }
  221. }
  222. }
  223. }