TestRunnerStateSerializer.cs
5.14 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
using System;
using System.Reflection;
using System.Text;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.TestRunner.NUnitExtensions.Runner;
using UnityEngine.TestTools.NUnitExtensions;
using UnityEngine.TestTools.Logging;
namespace UnityEditor.TestTools.TestRunner
{
[Serializable]
internal class TestRunnerStateSerializer : IStateSerializer
{
private const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy;
[SerializeField]
private HideFlags m_OriginalHideFlags;
[SerializeField]
private bool m_ShouldRestore;
[SerializeField]
private string m_TestObjectTypeName;
[SerializeField]
private ScriptableObject m_TestObject;
[SerializeField]
private string m_TestObjectTxt;
[SerializeField]
private long StartTicks;
[SerializeField]
private double StartTimeOA;
[SerializeField]
private string output;
[SerializeField]
private LogMatch[] m_ExpectedLogs;
public bool ShouldRestore()
{
return m_ShouldRestore;
}
public void SaveContext()
{
var currentContext = UnityTestExecutionContext.CurrentContext;
if (currentContext.TestObject != null)
{
m_TestObjectTypeName = currentContext.TestObject.GetType().AssemblyQualifiedName;
m_TestObject = null;
m_TestObjectTxt = null;
if (currentContext.TestObject is ScriptableObject)
{
m_TestObject = currentContext.TestObject as ScriptableObject;
m_OriginalHideFlags = m_TestObject.hideFlags;
m_TestObject.hideFlags |= HideFlags.DontSave;
}
else
{
m_TestObjectTxt = JsonUtility.ToJson(currentContext.TestObject);
}
}
output = currentContext.CurrentResult.Output;
StartTicks = currentContext.StartTicks;
StartTimeOA = currentContext.StartTime.ToOADate();
if (LogScope.HasCurrentLogScope())
{
m_ExpectedLogs = LogScope.Current.ExpectedLogs.ToArray();
}
m_ShouldRestore = true;
}
public void RestoreContext()
{
var currentContext = UnityTestExecutionContext.CurrentContext;
var outputProp = currentContext.CurrentResult.GetType().BaseType.GetField("_output", Flags);
(outputProp.GetValue(currentContext.CurrentResult) as StringBuilder).Append(output);
currentContext.StartTicks = StartTicks;
currentContext.StartTime = DateTime.FromOADate(StartTimeOA);
if (LogScope.HasCurrentLogScope())
{
LogScope.Current.ExpectedLogs = new Queue<LogMatch>(m_ExpectedLogs);
}
m_ShouldRestore = false;
}
public bool CanRestoreFromScriptableObject(Type requestedType)
{
if (m_TestObject == null)
{
return false;
}
return m_TestObjectTypeName == requestedType.AssemblyQualifiedName;
}
public ScriptableObject RestoreScriptableObjectInstance()
{
if (m_TestObject == null)
{
Debug.LogError("No object to restore");
return null;
}
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
var temp = m_TestObject;
m_TestObject = null;
m_TestObjectTypeName = null;
return temp;
}
public bool CanRestoreFromJson(Type requestedType)
{
if (string.IsNullOrEmpty(m_TestObjectTxt))
{
return false;
}
return m_TestObjectTypeName == requestedType.AssemblyQualifiedName;
}
public void RestoreClassFromJson(ref object instance)
{
if (string.IsNullOrEmpty(m_TestObjectTxt))
{
Debug.LogWarning("No JSON representation to restore");
return;
}
JsonUtility.FromJsonOverwrite(m_TestObjectTxt, instance);
m_TestObjectTxt = null;
m_TestObjectTypeName = null;
}
private void OnPlayModeStateChanged(PlayModeStateChange state)
{
if (m_TestObject == null)
{
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
return;
}
//We set the DontSave flag here because the ScriptableObject would be nulled right before entering EditMode
if (state == PlayModeStateChange.ExitingPlayMode)
{
m_TestObject.hideFlags |= HideFlags.DontSave;
}
else if (state == PlayModeStateChange.EnteredEditMode)
{
m_TestObject.hideFlags = m_OriginalHideFlags;
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
}
}
}
}