Brak opisu
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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. if (AdaptivePerformanceGeneralSettings.Instance != null)
  28. return;
  29. testManager = ScriptableObject.CreateInstance<AdaptivePerformanceManagerSettings>();
  30. adaptivePerformanceGeneralSettings = ScriptableObject.CreateInstance<AdaptivePerformanceGeneralSettings>() as AdaptivePerformanceGeneralSettings;
  31. adaptivePerformanceGeneralSettings.Manager = testManager;
  32. testPathToSettings = GetAssetPathForComponents(s_TempSettingsPath);
  33. testPathToSettings = Path.Combine(testPathToSettings, $"Test_{ typeof(AdaptivePerformanceGeneralSettings).Name}.asset");
  34. if (!string.IsNullOrEmpty(testPathToSettings))
  35. {
  36. AssetDatabase.CreateAsset(adaptivePerformanceGeneralSettings, testPathToSettings);
  37. AssetDatabase.AddObjectToAsset(testManager, adaptivePerformanceGeneralSettings);
  38. AssetDatabase.SaveAssets();
  39. }
  40. testPathToGeneralSettings = GetAssetPathForComponents(s_TestGeneralSettings);
  41. testPathToGeneralSettings = Path.Combine(testPathToGeneralSettings, $"Test_{typeof(AdaptivePerformanceGeneralSettingsPerBuildTarget).Name}.asset");
  42. buildTargetSettings = ScriptableObject.CreateInstance<AdaptivePerformanceGeneralSettingsPerBuildTarget>();
  43. buildTargetSettings.SetSettingsForBuildTarget(BuildTargetGroup.Standalone, adaptivePerformanceGeneralSettings);
  44. if (!string.IsNullOrEmpty(testPathToSettings))
  45. {
  46. AssetDatabase.CreateAsset(buildTargetSettings, testPathToGeneralSettings);
  47. AssetDatabase.SaveAssets();
  48. EditorBuildSettings.TryGetConfigObject(AdaptivePerformanceGeneralSettings.k_SettingsKey, out currentSettings);
  49. EditorBuildSettings.AddConfigObject(AdaptivePerformanceGeneralSettings.k_SettingsKey, buildTargetSettings, true);
  50. }
  51. testPathToLoader = GetAssetPathForComponents(s_TempSettingsPath);
  52. // Setup Loader
  53. loader = ScriptableObject.CreateInstance(typeof(SimulatorProviderLoader)) as SimulatorProviderLoader;
  54. AssetDatabase.CreateAsset(loader, Path.Combine(testPathToLoader, $"Test_{typeof(SimulatorProviderLoader).Name}.asset"));
  55. testManager.loaders.Add(loader);
  56. // Setup Settings
  57. settings = ScriptableObject.CreateInstance(typeof(SimulatorProviderSettings)) as SimulatorProviderSettings;
  58. AssetDatabase.CreateAsset(settings, Path.Combine(testPathToLoader, $"Test_{typeof(SimulatorProviderSettings).Name}.asset"));
  59. //settings.logging = false;
  60. EditorBuildSettings.AddConfigObject(SimulatorProviderConstants.k_SettingsKey, settings, true);
  61. // Due to the Settings menu, we have to manually assigned the Simulator loader in tests.
  62. AdaptivePerformancePackageMetadataStore.AssignLoader(AdaptivePerformanceGeneralSettings.Instance.Manager, typeof(SimulatorProviderLoader).Name, BuildTargetGroup.Standalone);
  63. }
  64. public virtual void TearDownTest()
  65. {
  66. testManager.DeinitializeLoader();
  67. adaptivePerformanceGeneralSettings.Manager.loaders.Remove(loader);
  68. loader = null;
  69. EditorBuildSettings.RemoveConfigObject(AdaptivePerformanceGeneralSettings.k_SettingsKey);
  70. if (!string.IsNullOrEmpty(testPathToGeneralSettings))
  71. {
  72. AssetDatabase.DeleteAsset(testPathToGeneralSettings);
  73. }
  74. if (!string.IsNullOrEmpty(testPathToSettings))
  75. {
  76. AssetDatabase.DeleteAsset(testPathToSettings);
  77. }
  78. adaptivePerformanceGeneralSettings.Manager = null;
  79. UnityEngine.Object.DestroyImmediate(adaptivePerformanceGeneralSettings);
  80. adaptivePerformanceGeneralSettings = null;
  81. UnityEngine.Object.DestroyImmediate(testManager);
  82. testManager = null;
  83. UnityEngine.Object.DestroyImmediate(buildTargetSettings);
  84. buildTargetSettings = null;
  85. if (currentSettings != null)
  86. EditorBuildSettings.AddConfigObject(AdaptivePerformanceGeneralSettings.k_SettingsKey, currentSettings, true);
  87. else
  88. EditorBuildSettings.RemoveConfigObject(AdaptivePerformanceGeneralSettings.k_SettingsKey);
  89. AssetDatabase.DeleteAsset(Path.Combine("Assets", "Temp"));
  90. }
  91. public static string GetAssetPathForComponents(string[] pathComponents, string root = "Assets")
  92. {
  93. if (pathComponents.Length <= 0)
  94. return null;
  95. string path = root;
  96. foreach (var pc in pathComponents)
  97. {
  98. string subFolder = Path.Combine(path, pc);
  99. bool shouldCreate = true;
  100. foreach (var f in AssetDatabase.GetSubFolders(path))
  101. {
  102. if (String.Compare(Path.GetFullPath(f), Path.GetFullPath(subFolder), true) == 0)
  103. {
  104. shouldCreate = false;
  105. break;
  106. }
  107. }
  108. if (shouldCreate)
  109. AssetDatabase.CreateFolder(path, pc);
  110. path = subFolder;
  111. }
  112. return path;
  113. }
  114. }
  115. }