TestRunnerStateSerializer.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Reflection;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.TestRunner.NUnitExtensions.Runner;
  7. using UnityEngine.TestTools.NUnitExtensions;
  8. using UnityEngine.TestTools.Logging;
  9. namespace UnityEditor.TestTools.TestRunner
  10. {
  11. [Serializable]
  12. internal class TestRunnerStateSerializer : IStateSerializer
  13. {
  14. private const BindingFlags Flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy;
  15. [SerializeField]
  16. private HideFlags m_OriginalHideFlags;
  17. [SerializeField]
  18. private bool m_ShouldRestore;
  19. [SerializeField]
  20. private string m_TestObjectTypeName;
  21. [SerializeField]
  22. private ScriptableObject m_TestObject;
  23. [SerializeField]
  24. private string m_TestObjectTxt;
  25. [SerializeField]
  26. private long StartTicks;
  27. [SerializeField]
  28. private double StartTimeOA;
  29. [SerializeField]
  30. private string output;
  31. [SerializeField]
  32. private LogMatch[] m_ExpectedLogs;
  33. public bool ShouldRestore()
  34. {
  35. return m_ShouldRestore;
  36. }
  37. public void SaveContext()
  38. {
  39. var currentContext = UnityTestExecutionContext.CurrentContext;
  40. if (currentContext.TestObject != null)
  41. {
  42. m_TestObjectTypeName = currentContext.TestObject.GetType().AssemblyQualifiedName;
  43. m_TestObject = null;
  44. m_TestObjectTxt = null;
  45. if (currentContext.TestObject is ScriptableObject)
  46. {
  47. m_TestObject = currentContext.TestObject as ScriptableObject;
  48. m_OriginalHideFlags = m_TestObject.hideFlags;
  49. m_TestObject.hideFlags |= HideFlags.DontSave;
  50. }
  51. else
  52. {
  53. m_TestObjectTxt = JsonUtility.ToJson(currentContext.TestObject);
  54. }
  55. }
  56. output = currentContext.CurrentResult.Output;
  57. StartTicks = currentContext.StartTicks;
  58. StartTimeOA = currentContext.StartTime.ToOADate();
  59. if (LogScope.HasCurrentLogScope())
  60. {
  61. m_ExpectedLogs = LogScope.Current.ExpectedLogs.ToArray();
  62. }
  63. m_ShouldRestore = true;
  64. }
  65. public void RestoreContext()
  66. {
  67. var currentContext = UnityTestExecutionContext.CurrentContext;
  68. var outputProp = currentContext.CurrentResult.GetType().BaseType.GetField("_output", Flags);
  69. (outputProp.GetValue(currentContext.CurrentResult) as StringBuilder).Append(output);
  70. currentContext.StartTicks = StartTicks;
  71. currentContext.StartTime = DateTime.FromOADate(StartTimeOA);
  72. if (LogScope.HasCurrentLogScope())
  73. {
  74. LogScope.Current.ExpectedLogs = new Queue<LogMatch>(m_ExpectedLogs);
  75. }
  76. m_ShouldRestore = false;
  77. }
  78. public bool CanRestoreFromScriptableObject(Type requestedType)
  79. {
  80. if (m_TestObject == null)
  81. {
  82. return false;
  83. }
  84. return m_TestObjectTypeName == requestedType.AssemblyQualifiedName;
  85. }
  86. public ScriptableObject RestoreScriptableObjectInstance()
  87. {
  88. if (m_TestObject == null)
  89. {
  90. Debug.LogError("No object to restore");
  91. return null;
  92. }
  93. EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
  94. var temp = m_TestObject;
  95. m_TestObject = null;
  96. m_TestObjectTypeName = null;
  97. return temp;
  98. }
  99. public bool CanRestoreFromJson(Type requestedType)
  100. {
  101. if (string.IsNullOrEmpty(m_TestObjectTxt))
  102. {
  103. return false;
  104. }
  105. return m_TestObjectTypeName == requestedType.AssemblyQualifiedName;
  106. }
  107. public void RestoreClassFromJson(ref object instance)
  108. {
  109. if (string.IsNullOrEmpty(m_TestObjectTxt))
  110. {
  111. Debug.LogWarning("No JSON representation to restore");
  112. return;
  113. }
  114. JsonUtility.FromJsonOverwrite(m_TestObjectTxt, instance);
  115. m_TestObjectTxt = null;
  116. m_TestObjectTypeName = null;
  117. }
  118. private void OnPlayModeStateChanged(PlayModeStateChange state)
  119. {
  120. if (m_TestObject == null)
  121. {
  122. EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
  123. return;
  124. }
  125. //We set the DontSave flag here because the ScriptableObject would be nulled right before entering EditMode
  126. if (state == PlayModeStateChange.ExitingPlayMode)
  127. {
  128. m_TestObject.hideFlags |= HideFlags.DontSave;
  129. }
  130. else if (state == PlayModeStateChange.EnteredEditMode)
  131. {
  132. m_TestObject.hideFlags = m_OriginalHideFlags;
  133. EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
  134. }
  135. }
  136. }
  137. }