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