TestRunnerApi.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Linq;
  3. using System.Threading;
  4. using UnityEditor.TestTools.TestRunner.CommandLineTest;
  5. using UnityEditor.TestTools.TestRunner.TestRun;
  6. using UnityEngine;
  7. using UnityEngine.TestRunner.TestLaunchers;
  8. using UnityEngine.TestTools;
  9. using UnityEngine.TestTools.NUnitExtensions;
  10. namespace UnityEditor.TestTools.TestRunner.Api
  11. {
  12. public class TestRunnerApi : ScriptableObject, ITestRunnerApi
  13. {
  14. internal ICallbacksHolder callbacksHolder;
  15. private ICallbacksHolder m_CallbacksHolder
  16. {
  17. get
  18. {
  19. if (callbacksHolder == null)
  20. {
  21. return CallbacksHolder.instance;
  22. }
  23. return callbacksHolder;
  24. }
  25. }
  26. internal Func<ExecutionSettings,string> ScheduleJob = (executionSettings) =>
  27. {
  28. var runner = new TestJobRunner();
  29. return runner.RunJob(new TestJobData(executionSettings));
  30. };
  31. public string Execute(ExecutionSettings executionSettings)
  32. {
  33. if (executionSettings == null)
  34. {
  35. throw new ArgumentNullException(nameof(executionSettings));
  36. }
  37. if ((executionSettings.filters == null || executionSettings.filters.Length == 0) && executionSettings.filter != null)
  38. {
  39. // Map filter (singular) to filters (plural), for backwards compatibility.
  40. executionSettings.filters = new [] {executionSettings.filter};
  41. }
  42. if (executionSettings.targetPlatform == null && executionSettings.filters != null &&
  43. executionSettings.filters.Length > 0)
  44. {
  45. executionSettings.targetPlatform = executionSettings.filters[0].targetPlatform;
  46. }
  47. return ScheduleJob(executionSettings);
  48. }
  49. public void RegisterCallbacks<T>(T testCallbacks, int priority = 0) where T : ICallbacks
  50. {
  51. if (testCallbacks == null)
  52. {
  53. throw new ArgumentNullException(nameof(testCallbacks));
  54. }
  55. m_CallbacksHolder.Add(testCallbacks, priority);
  56. }
  57. public void UnregisterCallbacks<T>(T testCallbacks) where T : ICallbacks
  58. {
  59. if (testCallbacks == null)
  60. {
  61. throw new ArgumentNullException(nameof(testCallbacks));
  62. }
  63. m_CallbacksHolder.Remove(testCallbacks);
  64. }
  65. internal void RetrieveTestList(ExecutionSettings executionSettings, Action<ITestAdaptor> callback)
  66. {
  67. if (executionSettings == null)
  68. {
  69. throw new ArgumentNullException(nameof(executionSettings));
  70. }
  71. var firstFilter = executionSettings.filters?.FirstOrDefault() ?? executionSettings.filter;
  72. RetrieveTestList(firstFilter.testMode, callback);
  73. }
  74. public void RetrieveTestList(TestMode testMode, Action<ITestAdaptor> callback)
  75. {
  76. if (callback == null)
  77. {
  78. throw new ArgumentNullException(nameof(callback));
  79. }
  80. var platform = ParseTestMode(testMode);
  81. var testAssemblyProvider = new EditorLoadedTestAssemblyProvider(new EditorCompilationInterfaceProxy(), new EditorAssembliesProxy());
  82. var testAdaptorFactory = new TestAdaptorFactory();
  83. var testListCache = new TestListCache(testAdaptorFactory, new RemoteTestResultDataFactory(), TestListCacheData.instance);
  84. var testListProvider = new TestListProvider(testAssemblyProvider, new UnityTestAssemblyBuilder());
  85. var cachedTestListProvider = new CachingTestListProvider(testListProvider, testListCache, testAdaptorFactory);
  86. var job = new TestListJob(cachedTestListProvider, platform, (testRoot) =>
  87. {
  88. callback(testRoot);
  89. });
  90. job.Start();
  91. }
  92. internal static bool IsRunActive()
  93. {
  94. return RunData.instance.isRunning;
  95. }
  96. private static TestPlatform ParseTestMode(TestMode testMode)
  97. {
  98. return (((testMode & TestMode.EditMode) == TestMode.EditMode) ? TestPlatform.EditMode : 0) | (((testMode & TestMode.PlayMode) == TestMode.PlayMode) ? TestPlatform.PlayMode : 0);
  99. }
  100. }
  101. }