Skip to content

Commit f423ac4

Browse files
committed
added transform comparison
1 parent 16be718 commit f423ac4

File tree

1 file changed

+71
-11
lines changed

1 file changed

+71
-11
lines changed

Assets/FbxExporters/Editor/UnitTests/DefaultSelectionTest.cs

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,50 +60,80 @@ protected string GetRandomFileNamePath(string pathName = null, string prefixName
6060
return temp;
6161
}
6262

63+
protected GameObject m_root;
64+
6365
[TearDown]
6466
public void Term ()
6567
{
6668
foreach (string file in Directory.GetFiles (this.filePath, MakeFileName("*"))) {
6769
File.Delete (file);
6870
}
71+
if (m_root) {
72+
UnityEngine.Object.DestroyImmediate (m_root);
73+
}
6974
}
7075

7176
[Test]
7277
public void TestDefaultSelection ()
7378
{
74-
var root = CreateHierarchy ();
75-
Assert.IsNotNull (root);
79+
m_root = CreateHierarchy ();
80+
Assert.IsNotNull (m_root);
7681

7782
// test Export Root
7883
// Expected result: everything gets exported
79-
var exportedRoot = ExportSelection (new Object[]{root});
80-
CompareHierarchies(root, exportedRoot, true);
84+
// Expected transform: root is zeroed out, all other transforms unchanged
85+
var exportedRoot = ExportSelection (new Object[]{m_root});
86+
CompareHierarchies(m_root, exportedRoot, true, false);
87+
CompareGlobalTransform (exportedRoot.transform);
8188

8289
// test Export Parent1, Child1
8390
// Expected result: Parent1, Child1, Child2
84-
var parent1 = root.transform.Find("Parent1");
91+
// Expected transform: Parent1 zeroed out, all other transforms unchanged
92+
var parent1 = m_root.transform.Find("Parent1");
8593
var child1 = parent1.Find ("Child1");
8694
exportedRoot = ExportSelection (new Object[]{parent1.gameObject, child1.gameObject});
87-
CompareHierarchies(parent1.gameObject, exportedRoot, true);
95+
CompareHierarchies(parent1.gameObject, exportedRoot, true, false);
96+
CompareGlobalTransform (exportedRoot.transform);
8897

8998
// test Export Child2
9099
// Expected result: Child2
100+
// Expected transform: Child2 zeroed out
91101
var child2 = parent1.Find("Child2").gameObject;
92102
exportedRoot = ExportSelection (new Object[]{child2});
93-
CompareHierarchies(child2, exportedRoot, true);
103+
CompareHierarchies(child2, exportedRoot, true, false);
104+
CompareGlobalTransform (exportedRoot.transform);
94105

95106
// test Export Child2, Parent2
96107
// Expected result: Parent2, Child3, Child2
97-
var parent2 = root.transform.Find("Parent2");
108+
// Expected transform: Child2 and Parent2 maintain global transform
109+
var parent2 = m_root.transform.Find("Parent2");
98110
exportedRoot = ExportSelection (new Object[]{child2, parent2});
99111

100112
List<GameObject> children = new List<GameObject> ();
101113
foreach (Transform child in exportedRoot.transform) {
102114
children.Add (child.gameObject);
103115
}
104116
CompareHierarchies(new GameObject[]{child2, parent2.gameObject}, children.ToArray());
117+
}
105118

106-
UnityEngine.Object.DestroyImmediate (root);
119+
/// <summary>
120+
/// Compares the global transform of expected
121+
/// to the local transform of actual.
122+
/// </summary>
123+
/// <param name="actual">Actual.</param>
124+
/// <param name="expected">Expected.</param>
125+
private void CompareGlobalTransform(Transform actual, Transform expected=null){
126+
if (!expected) {
127+
// test that actual is zeroed out
128+
Assert.AreEqual(Vector3.zero, actual.localPosition);
129+
Assert.AreEqual (Vector3.zero, actual.localEulerAngles);
130+
Assert.AreEqual (Vector3.one, actual.localScale);
131+
return;
132+
}
133+
float epsilon = 0.0001f;
134+
Assert.IsTrue (Vector3.SqrMagnitude(expected.position - actual.localPosition) < epsilon);
135+
Assert.IsTrue (Vector3.SqrMagnitude(expected.rotation.eulerAngles - actual.localEulerAngles) < epsilon);
136+
Assert.IsTrue (Vector3.SqrMagnitude(expected.lossyScale - actual.localScale) < epsilon);
107137
}
108138

109139
private GameObject CreateHierarchy ()
@@ -117,9 +147,23 @@ private GameObject CreateHierarchy ()
117147
// ----> Child3
118148

119149
var root = CreateGameObject ("Root");
150+
SetTransform (root.transform,
151+
new Vector3 (3, 4, -6),
152+
new Vector3 (45, 10, 34),
153+
new Vector3 (2, 1, 3));
120154

121155
var parent1 = CreateGameObject ("Parent1", root.transform);
156+
SetTransform (parent1.transform,
157+
new Vector3 (53, 0, -1),
158+
new Vector3 (0, 5, 0),
159+
new Vector3 (1, 1, 1));
160+
122161
var parent2 = CreateGameObject ("Parent2", root.transform);
162+
SetTransform (parent2.transform,
163+
new Vector3 (0, 0, 0),
164+
new Vector3 (90, 1, 3),
165+
new Vector3 (1, 0.3f, 0.5f));
166+
123167
parent1.transform.SetAsFirstSibling ();
124168

125169
CreateGameObject ("Child1", parent1.transform);
@@ -129,21 +173,34 @@ private GameObject CreateHierarchy ()
129173
return root;
130174
}
131175

176+
private void SetTransform(Transform t, Vector3 pos, Vector3 rot, Vector3 scale){
177+
t.localPosition = pos;
178+
t.localEulerAngles = rot;
179+
t.localScale = scale;
180+
}
181+
132182
private GameObject CreateGameObject(string name, Transform parent = null)
133183
{
134184
var go = new GameObject (name);
135185
go.transform.SetParent (parent);
136186
return go;
137187
}
138188

139-
private void CompareHierarchies(GameObject expectedHierarchy, GameObject actualHierarchy, bool ignoreName = false)
189+
private void CompareHierarchies(
190+
GameObject expectedHierarchy, GameObject actualHierarchy,
191+
bool ignoreName = false, bool compareTransform = true)
140192
{
141193
if (!ignoreName) {
142194
Assert.AreEqual (expectedHierarchy.name, actualHierarchy.name);
143195
}
144196

145197
var expectedTransform = expectedHierarchy.transform;
146198
var actualTransform = actualHierarchy.transform;
199+
200+
if (compareTransform) {
201+
Assert.AreEqual (expectedTransform, actualTransform);
202+
}
203+
147204
Assert.AreEqual (expectedTransform.childCount, actualTransform.childCount);
148205

149206
foreach (Transform expectedChild in expectedTransform) {
@@ -165,7 +222,10 @@ private void CompareHierarchies(GameObject[] expectedHierarchy, GameObject[] act
165222
});
166223

167224
for (int i = 0; i < expectedHierarchy.Length; i++) {
168-
CompareHierarchies (expectedHierarchy [i], actualHierarchy [i]);
225+
CompareHierarchies (expectedHierarchy [i], actualHierarchy [i], false, false);
226+
// if we are Comparing lists of hierarchies, that means that the transforms
227+
// should be the global transform of expected, as there is no zeroed out root
228+
CompareGlobalTransform (actualHierarchy [i].transform, expectedHierarchy [i].transform);
169229
}
170230
}
171231

0 commit comments

Comments
 (0)