LoaderTestSetup.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System.IO;
  2. using NUnit.Framework;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using UnityEngine.TestTools;
  6. using UnityEngine.XR;
  7. using UnityEngine.XR.Management;
  8. namespace Unity.XR.TestTooling
  9. {
  10. public abstract class LoaderTestSetup<L, S> : ManagementTestSetup, IPrebuildSetup, IPostBuildCleanup
  11. where L : XRLoader
  12. where S : ScriptableObject
  13. {
  14. protected abstract string settingsKey { get; }
  15. protected L loader = null;
  16. protected S settings = null;
  17. private bool isRunning = false;
  18. public override void SetupTest()
  19. {
  20. base.SetupTest();
  21. Assert.IsNotNull(xrGeneralSettings);
  22. // Deleted by ManagementSetup - deletes whole Temp folder
  23. // Setup Loader
  24. loader = ScriptableObject.CreateInstance<L>();
  25. var path = GetAssetPathForComponents(s_TempSettingsPath);
  26. AssetDatabase.CreateAsset(loader, Path.Combine(path, $"Test_{typeof(L).Name}.asset"));
  27. xrGeneralSettings.Manager.loaders.Add(loader);
  28. // Setup Settings
  29. settings = ScriptableObject.CreateInstance<S>();
  30. AssetDatabase.CreateAsset(settings, Path.Combine(path, $"Test_{typeof(S).Name}.asset"));
  31. EditorBuildSettings.AddConfigObject(settingsKey, settings, true);
  32. AssetDatabase.SaveAssets();
  33. }
  34. public override void TearDownTest()
  35. {
  36. if (isRunning)
  37. StopAndShutdown();
  38. xrGeneralSettings.Manager.loaders.Remove(loader);
  39. loader = null;
  40. base.TearDownTest();
  41. }
  42. protected void InitializeAndStart()
  43. {
  44. if (loader != null)
  45. {
  46. if (loader.Initialize())
  47. isRunning = loader.Start();
  48. }
  49. }
  50. protected void StopAndShutdown()
  51. {
  52. if (loader != null)
  53. {
  54. loader.Stop();
  55. loader.Deinitialize();
  56. isRunning = false;
  57. }
  58. }
  59. protected void RestartProvider()
  60. {
  61. StopAndShutdown();
  62. InitializeAndStart();
  63. }
  64. // IPrebuildSetup - Build time setup
  65. void IPrebuildSetup.Setup()
  66. {
  67. if (XRGeneralSettings.Instance != null)
  68. XRGeneralSettings.Instance.InitManagerOnStart = false;
  69. }
  70. // IPostBuildCleanup - Build time cleanup
  71. void IPostBuildCleanup.Cleanup()
  72. {
  73. if (XRGeneralSettings.Instance != null)
  74. XRGeneralSettings.Instance.InitManagerOnStart = true;
  75. }
  76. }
  77. }