ManagementTestSetup.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEditor.XR.Management;
  5. using UnityEngine;
  6. using UnityEngine.XR.Management;
  7. namespace Unity.XR.TestTooling
  8. {
  9. // Mostly borrowed from XRManagement - this should probably live in that package.
  10. public abstract class ManagementTestSetup
  11. {
  12. protected static readonly string[] s_TestGeneralSettings = { "Temp", "Test" };
  13. protected static readonly string[] s_TempSettingsPath = {"Temp", "Test", "Settings" };
  14. /// <summary>
  15. /// When true, AssetDatabase.AddObjectToAsset will not be called to add XRManagerSettings to XRGeneralSettings.
  16. /// </summary>
  17. protected virtual bool TestManagerUpgradePath => false;
  18. protected string testPathToGeneralSettings;
  19. protected string testPathToSettings;
  20. private UnityEngine.Object currentSettings = null;
  21. protected XRManagerSettings testManager = null;
  22. protected XRGeneralSettings xrGeneralSettings = null;
  23. protected XRGeneralSettingsPerBuildTarget buildTargetSettings = null;
  24. public virtual void SetupTest()
  25. {
  26. testManager = ScriptableObject.CreateInstance<XRManagerSettings>();
  27. xrGeneralSettings = ScriptableObject.CreateInstance<XRGeneralSettings>() as XRGeneralSettings;
  28. xrGeneralSettings.Manager = testManager;
  29. testPathToSettings = GetAssetPathForComponents(s_TempSettingsPath);
  30. testPathToSettings = Path.Combine(testPathToSettings, "Test_XRGeneralSettings.asset");
  31. if (!string.IsNullOrEmpty(testPathToSettings))
  32. {
  33. AssetDatabase.CreateAsset(xrGeneralSettings, testPathToSettings);
  34. if (!TestManagerUpgradePath)
  35. {
  36. AssetDatabase.AddObjectToAsset(testManager, xrGeneralSettings);
  37. }
  38. AssetDatabase.SaveAssets();
  39. }
  40. testPathToGeneralSettings = GetAssetPathForComponents(s_TestGeneralSettings);
  41. testPathToGeneralSettings = Path.Combine(testPathToGeneralSettings, "Test_XRGeneralSettingsPerBuildTarget.asset");
  42. buildTargetSettings = ScriptableObject.CreateInstance<XRGeneralSettingsPerBuildTarget>();
  43. buildTargetSettings.SetSettingsForBuildTarget(BuildTargetGroup.Standalone, xrGeneralSettings);
  44. if (!string.IsNullOrEmpty(testPathToSettings))
  45. {
  46. AssetDatabase.CreateAsset(buildTargetSettings, testPathToGeneralSettings);
  47. AssetDatabase.SaveAssets();
  48. EditorBuildSettings.TryGetConfigObject(XRGeneralSettings.k_SettingsKey, out currentSettings);
  49. EditorBuildSettings.AddConfigObject(XRGeneralSettings.k_SettingsKey, buildTargetSettings, true);
  50. }
  51. }
  52. public virtual void TearDownTest()
  53. {
  54. EditorBuildSettings.RemoveConfigObject(XRGeneralSettings.k_SettingsKey);
  55. if (!string.IsNullOrEmpty(testPathToGeneralSettings))
  56. {
  57. AssetDatabase.DeleteAsset(testPathToGeneralSettings);
  58. }
  59. if (!string.IsNullOrEmpty(testPathToSettings))
  60. {
  61. AssetDatabase.DeleteAsset(testPathToSettings);
  62. }
  63. xrGeneralSettings.Manager = null;
  64. UnityEngine.Object.DestroyImmediate(xrGeneralSettings);
  65. xrGeneralSettings = null;
  66. UnityEngine.Object.DestroyImmediate(testManager);
  67. testManager = null;
  68. UnityEngine.Object.DestroyImmediate(buildTargetSettings);
  69. buildTargetSettings = null;
  70. if (currentSettings != null)
  71. EditorBuildSettings.AddConfigObject(XRGeneralSettings.k_SettingsKey, currentSettings, true);
  72. else
  73. EditorBuildSettings.RemoveConfigObject(XRGeneralSettings.k_SettingsKey);
  74. AssetDatabase.DeleteAsset(Path.Combine("Assets","Temp"));
  75. }
  76. public static string GetAssetPathForComponents(string[] pathComponents, string root = "Assets")
  77. {
  78. if (pathComponents.Length <= 0)
  79. return null;
  80. string path = root;
  81. foreach( var pc in pathComponents)
  82. {
  83. string subFolder = Path.Combine(path, pc);
  84. bool shouldCreate = true;
  85. foreach (var f in AssetDatabase.GetSubFolders(path))
  86. {
  87. if (String.Compare(Path.GetFullPath(f), Path.GetFullPath(subFolder), true) == 0)
  88. {
  89. shouldCreate = false;
  90. break;
  91. }
  92. }
  93. if (shouldCreate)
  94. AssetDatabase.CreateFolder(path, pc);
  95. path = subFolder;
  96. }
  97. return path;
  98. }
  99. }
  100. }