EditorEnumeratorTestWorkItem.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using System;
  2. using System.Collections;
  3. using NUnit.Framework.Interfaces;
  4. using NUnit.Framework.Internal;
  5. using NUnit.Framework.Internal.Commands;
  6. using NUnit.Framework.Internal.Execution;
  7. using UnityEngine;
  8. using UnityEngine.TestRunner.NUnitExtensions.Runner;
  9. using UnityEngine.TestTools;
  10. namespace UnityEditor.TestTools.TestRunner
  11. {
  12. internal class EditorEnumeratorTestWorkItem : UnityWorkItem
  13. {
  14. private TestCommand m_Command;
  15. public EditorEnumeratorTestWorkItem(TestMethod test, ITestFilter filter)
  16. : base(test, null)
  17. {
  18. m_Command = TestCommandBuilder.BuildTestCommand(test, filter);
  19. }
  20. private static IEnumerableTestMethodCommand FindFirstIEnumerableTestMethodCommand(TestCommand command)
  21. {
  22. if (command == null)
  23. {
  24. return null;
  25. }
  26. if (command is IEnumerableTestMethodCommand)
  27. {
  28. return (IEnumerableTestMethodCommand)command;
  29. }
  30. if (command is DelegatingTestCommand)
  31. {
  32. var delegatingTestCommand = (DelegatingTestCommand)command;
  33. return FindFirstIEnumerableTestMethodCommand(delegatingTestCommand.GetInnerCommand());
  34. }
  35. return null;
  36. }
  37. protected override IEnumerable PerformWork()
  38. {
  39. if (IsCancelledRun())
  40. {
  41. yield break;
  42. }
  43. if (m_DontRunRestoringResult)
  44. {
  45. if (EditModeTestCallbacks.RestoringTestContext == null)
  46. {
  47. throw new NullReferenceException("RestoringTestContext is not set");
  48. }
  49. EditModeTestCallbacks.RestoringTestContext();
  50. Result = Context.CurrentResult;
  51. yield break;
  52. }
  53. try
  54. {
  55. if (IsCancelledRun())
  56. {
  57. yield break;
  58. }
  59. if (m_Command is SkipCommand)
  60. {
  61. m_Command.Execute(Context);
  62. Result = Context.CurrentResult;
  63. yield break;
  64. }
  65. //Check if we can execute this test
  66. var firstEnumerableCommand = FindFirstIEnumerableTestMethodCommand(m_Command);
  67. if (firstEnumerableCommand == null)
  68. {
  69. Context.CurrentResult.SetResult(ResultState.Error, "Returning IEnumerator but not using test attribute supporting this");
  70. yield break;
  71. }
  72. if (m_Command.Test.Method.ReturnType.IsType(typeof(IEnumerator)))
  73. {
  74. if (m_Command is ApplyChangesToContextCommand)
  75. {
  76. var applyChangesToContextCommand = ((ApplyChangesToContextCommand)m_Command);
  77. applyChangesToContextCommand.ApplyChanges(Context);
  78. m_Command = applyChangesToContextCommand.GetInnerCommand();
  79. }
  80. var innerCommand = m_Command as IEnumerableTestMethodCommand;
  81. if (innerCommand == null)
  82. {
  83. Debug.Log("failed getting innerCommand");
  84. throw new Exception("Tests returning IEnumerator can only use test attributes handling those");
  85. }
  86. foreach (var workItemStep in innerCommand.ExecuteEnumerable(Context))
  87. {
  88. if (IsCancelledRun())
  89. {
  90. yield break;
  91. }
  92. if (workItemStep is TestEnumerator)
  93. {
  94. if (EnumeratorStepHelper.UpdateEnumeratorPcIfNeeded(TestEnumerator.Enumerator))
  95. {
  96. yield return new RestoreTestContextAfterDomainReload();
  97. }
  98. continue;
  99. }
  100. if (workItemStep is AsyncOperation)
  101. {
  102. var asyncOperation = (AsyncOperation)workItemStep;
  103. while (!asyncOperation.isDone)
  104. {
  105. if (IsCancelledRun())
  106. {
  107. yield break;
  108. }
  109. yield return null;
  110. }
  111. continue;
  112. }
  113. ResultedInDomainReload = false;
  114. if (workItemStep is IEditModeTestYieldInstruction)
  115. {
  116. var editModeTestYieldInstruction = (IEditModeTestYieldInstruction)workItemStep;
  117. yield return editModeTestYieldInstruction;
  118. var enumerator = editModeTestYieldInstruction.Perform();
  119. while (true)
  120. {
  121. bool moveNext;
  122. try
  123. {
  124. moveNext = enumerator.MoveNext();
  125. }
  126. catch (Exception e)
  127. {
  128. Context.CurrentResult.RecordException(e);
  129. break;
  130. }
  131. if (!moveNext)
  132. {
  133. break;
  134. }
  135. yield return null;
  136. }
  137. }
  138. else
  139. {
  140. yield return workItemStep;
  141. }
  142. }
  143. Result = Context.CurrentResult;
  144. EditorApplication.isPlaying = false;
  145. yield return null;
  146. }
  147. }
  148. finally
  149. {
  150. WorkItemComplete();
  151. }
  152. }
  153. private bool IsCancelledRun()
  154. {
  155. return Context.ExecutionStatus == TestExecutionStatus.AbortRequested || Context.ExecutionStatus == TestExecutionStatus.StopRequested;
  156. }
  157. }
  158. }