No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ManagementTestSetup.cs 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.IO;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using UnityEngine.AdaptivePerformance;
  6. using UnityEditor.AdaptivePerformance.Simulator.Editor;
  7. using UnityEditor.AdaptivePerformance.Editor;
  8. using UnityEditor.AdaptivePerformance.Editor.Metadata;
  9. namespace UnityEditor.AdaptivePerformance.Tests
  10. {
  11. public abstract class ManagementTestSetup
  12. {
  13. protected static readonly string[] s_TestGeneralSettings = { "Temp", "Test" };
  14. protected static readonly string[] s_TempSettingsPath = { "Temp", "Test", "Settings" };
  15. static protected string testPathToGeneralSettings;
  16. static protected string testPathToSettings;
  17. static protected string testPathToLoader;
  18. static private UnityEngine.Object currentSettings = null;
  19. static protected AdaptivePerformanceManagerSettings testManager = null;
  20. static protected AdaptivePerformanceGeneralSettings adaptivePerformanceGeneralSettings = null;
  21. static protected AdaptivePerformanceGeneralSettingsPerBuildTarget buildTargetSettings = null;
  22. static protected SimulatorProviderLoader loader = null;
  23. static protected SimulatorProviderSettings settings = null;
  24. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
  25. public static void SetupTest()
  26. {
  27. testManager = ScriptableObject.CreateInstance<AdaptivePerformanceManagerSettings>();
  28. adaptivePerformanceGeneralSettings = ScriptableObject.CreateInstance<AdaptivePerformanceGeneralSettings>() as AdaptivePerformanceGeneralSettings;
  29. adaptivePerformanceGeneralSettings.Manager = testManager;
  30. testPathToSettings = GetAssetPathForComponents(s_TempSettingsPath);
  31. testPathToSettings = Path.Combine(testPathToSettings, $"Test_{ typeof(AdaptivePerformanceGeneralSettings).Name}.asset");
  32. if (!string.IsNullOrEmpty(testPathToSettings))
  33. {
  34. AssetDatabase.CreateAsset(adaptivePerformanceGeneralSettings, testPathToSettings);
  35. AssetDatabase.AddObjectToAsset(testManager, adaptivePerformanceGeneralSettings);
  36. AssetDatabase.SaveAssets();
  37. }
  38. testPathToGeneralSettings = GetAssetPathForComponents(s_TestGeneralSettings);
  39. testPathToGeneralSettings = Path.Combine(testPathToGeneralSettings, $"Test_{typeof(AdaptivePerformanceGeneralSettingsPerBuildTarget).Name}.asset");
  40. buildTargetSettings = ScriptableObject.CreateInstance<AdaptivePerformanceGeneralSettingsPerBuildTarget>();
  41. buildTargetSettings.SetSettingsForBuildTarget(BuildTargetGroup.Standalone, adaptivePerformanceGeneralSettings);
  42. if (!string.IsNullOrEmpty(testPathToSettings))
  43. {
  44. AssetDatabase.CreateAsset(buildTargetSettings, testPathToGeneralSettings);
  45. AssetDatabase.SaveAssets();
  46. EditorBuildSettings.TryGetConfigObject(AdaptivePerformanceGeneralSettings.k_SettingsKey, out currentSettings);
  47. EditorBuildSettings.AddConfigObject(AdaptivePerformanceGeneralSettings.k_SettingsKey, buildTargetSettings, true);
  48. }
  49. testPathToLoader = GetAssetPathForComponents(s_TempSettingsPath);
  50. // Setup Loader
  51. loader = ScriptableObject.CreateInstance(typeof(SimulatorProviderLoader)) as SimulatorProviderLoader;
  52. AssetDatabase.CreateAsset(loader, Path.Combine(testPathToLoader, $"Test_{typeof(SimulatorProviderLoader).Name}.asset"));
  53. testManager.loaders.Add(loader);
  54. // Setup Settings
  55. settings = ScriptableObject.CreateInstance(typeof(SimulatorProviderSettings)) as SimulatorProviderSettings;
  56. AssetDatabase.CreateAsset(settings, Path.Combine(testPathToLoader, $"Test_{typeof(SimulatorProviderSettings).Name}.asset"));
  57. //settings.logging = false;
  58. EditorBuildSettings.AddConfigObject(SimulatorProviderConstants.k_SettingsKey, settings, true);
  59. // Due to the Settings menu, we have to manually assigned the Simulator loader in tests.
  60. AdaptivePerformancePackageMetadataStore.AssignLoader(AdaptivePerformanceGeneralSettings.Instance.Manager, typeof(SimulatorProviderLoader).Name, BuildTargetGroup.Standalone);
  61. }
  62. public virtual void TearDownTest()
  63. {
  64. testManager.DeinitializeLoader();
  65. adaptivePerformanceGeneralSettings.Manager.loaders.Remove(loader);
  66. loader = null;
  67. EditorBuildSettings.RemoveConfigObject(AdaptivePerformanceGeneralSettings.k_SettingsKey);
  68. if (!string.IsNullOrEmpty(testPathToGeneralSettings))
  69. {
  70. AssetDatabase.DeleteAsset(testPathToGeneralSettings);
  71. }
  72. if (!string.IsNullOrEmpty(testPathToSettings))
  73. {
  74. AssetDatabase.DeleteAsset(testPathToSettings);
  75. }
  76. adaptivePerformanceGeneralSettings.Manager = null;
  77. UnityEngine.Object.DestroyImmediate(adaptivePerformanceGeneralSettings);
  78. adaptivePerformanceGeneralSettings = null;
  79. UnityEngine.Object.DestroyImmediate(testManager);
  80. testManager = null;
  81. UnityEngine.Object.DestroyImmediate(buildTargetSettings);
  82. buildTargetSettings = null;
  83. if (currentSettings != null)
  84. EditorBuildSettings.AddConfigObject(AdaptivePerformanceGeneralSettings.k_SettingsKey, currentSettings, true);
  85. else
  86. EditorBuildSettings.RemoveConfigObject(AdaptivePerformanceGeneralSettings.k_SettingsKey);
  87. AssetDatabase.DeleteAsset(Path.Combine("Assets", "Temp"));
  88. }
  89. public static string GetAssetPathForComponents(string[] pathComponents, string root = "Assets")
  90. {
  91. if (pathComponents.Length <= 0)
  92. return null;
  93. string path = root;
  94. foreach (var pc in pathComponents)
  95. {
  96. string subFolder = Path.Combine(path, pc);
  97. bool shouldCreate = true;
  98. foreach (var f in AssetDatabase.GetSubFolders(path))
  99. {
  100. if (String.Compare(Path.GetFullPath(f), Path.GetFullPath(subFolder), true) == 0)
  101. {
  102. shouldCreate = false;
  103. break;
  104. }
  105. }
  106. if (shouldCreate)
  107. AssetDatabase.CreateFolder(path, pc);
  108. path = subFolder;
  109. }
  110. return path;
  111. }
  112. }
  113. }