EditModeRunnerCallback.cs
5.7 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
using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.TestTools.TestRunner;
namespace UnityEditor.TestTools.TestRunner
{
internal class EditModeRunnerCallback : ScriptableObject, ITestRunnerListener
{
private EditModeLauncherContextSettings m_Settings;
public SceneSetup[] previousSceneSetup;
public EditModeRunner runner;
private bool m_Canceled;
private ITest m_CurrentTest;
private int m_TotalTests;
[SerializeField]
private List<string> m_PendingTests;
[SerializeField]
private string m_LastCountedTestName;
[SerializeField]
private bool m_RunRestarted;
public void OnDestroy()
{
CleanUp();
}
public void RunStarted(ITest testsToRun)
{
Setup();
if (m_PendingTests == null)
{
m_PendingTests = GetTestsExpectedToRun(testsToRun, runner.GetFilter());
m_TotalTests = m_PendingTests.Count;
}
}
public void OnEnable()
{
if (m_RunRestarted)
{
Setup();
}
}
private void Setup()
{
m_Settings = new EditModeLauncherContextSettings();
Application.logMessageReceivedThreaded += LogReceived;
EditorApplication.playModeStateChanged += WaitForExitPlaymode;
EditorApplication.update += DisplayProgressBar;
AssemblyReloadEvents.beforeAssemblyReload += BeforeAssemblyReload;
}
private void BeforeAssemblyReload()
{
if (m_CurrentTest != null)
{
m_LastCountedTestName = m_CurrentTest.FullName;
m_RunRestarted = true;
}
}
private void DisplayProgressBar()
{
if (m_CurrentTest == null)
return;
if (!m_Canceled && EditorUtility.DisplayCancelableProgressBar("Test Runner", "Running test " + m_CurrentTest.Name, Math.Min(1.0f, (float)(m_TotalTests - m_PendingTests.Count) / m_TotalTests)))
{
EditorApplication.update -= DisplayProgressBar;
m_Canceled = true;
EditorUtility.ClearProgressBar();
runner.OnRunCancel();
}
}
private static void LogReceived(string message, string stacktrace, LogType type)
{
if (TestContext.Out != null)
TestContext.Out.WriteLine(message);
}
private static void WaitForExitPlaymode(PlayModeStateChange state)
{
if (state == PlayModeStateChange.EnteredEditMode)
{
EditorApplication.playModeStateChanged -= WaitForExitPlaymode;
//because logMessage is reset on Enter EditMode
//we remove and add the callback
//because Unity
Application.logMessageReceivedThreaded -= LogReceived;
Application.logMessageReceivedThreaded += LogReceived;
}
}
public void RunFinished(ITestResult result)
{
if (previousSceneSetup != null && previousSceneSetup.Length > 0)
{
try
{
EditorSceneManager.RestoreSceneManagerSetup(previousSceneSetup);
}
catch (ArgumentException e)
{
Debug.LogWarning(e.Message);
}
}
else
{
foreach (var obj in FindObjectsOfType<GameObject>())
{
if (obj != null && obj.transform.parent != null && (obj.transform.parent.hideFlags & HideFlags.DontSaveInEditor) == HideFlags.DontSaveInEditor && obj.transform.parent.gameObject != null)
{
DestroyImmediate(obj.transform.parent.gameObject);
}
}
EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects, NewSceneMode.Single);
}
CleanUp();
}
private void CleanUp()
{
m_CurrentTest = null;
EditorUtility.ClearProgressBar();
if (m_Settings != null)
{
m_Settings.Dispose();
}
Application.logMessageReceivedThreaded -= LogReceived;
EditorApplication.update -= DisplayProgressBar;
}
public void TestStarted(ITest test)
{
if (test.IsSuite || !(test is TestMethod))
{
return;
}
m_CurrentTest = test;
if (m_RunRestarted)
{
if (test.FullName == m_LastCountedTestName)
m_RunRestarted = false;
}
}
public void TestFinished(ITestResult result)
{
if (result.Test is TestMethod)
{
m_PendingTests.Remove(result.Test.FullName);
}
}
private static List<string> GetTestsExpectedToRun(ITest test, ITestFilter filter)
{
var expectedTests = new List<string>();
if (filter.Pass(test))
{
if (test.IsSuite)
{
expectedTests.AddRange(test.Tests.SelectMany(subTest => GetTestsExpectedToRun(subTest, filter)));
}
else
{
expectedTests.Add(test.FullName);
}
}
return expectedTests;
}
}
}