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