Skip to content

Commit 6684d67

Browse files
author
Benoit Hudson
committed
Working on unit tests for fbxprefab...
1 parent aef18b1 commit 6684d67

File tree

2 files changed

+86
-41
lines changed

2 files changed

+86
-41
lines changed

Assets/FbxExporters/Editor/UnitTests/ExporterTestBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ namespace FbxExporters.UnitTests
1717
public abstract class ExporterTestBase
1818
{
1919
/// <summary>
20-
/// Sleep an amount of time (in ms) so we can safely assume that the timestamp on an fbx will change.
20+
/// Sleep an amount of time (in ms) so we can safely assume that the
21+
/// timestamp on an fbx will change.
2122
/// </summary>
2223
public void SleepForFileTimestamp() {
23-
System.Threading.Thread.Sleep(1000);
24+
System.Threading.Thread.Sleep(100);
2425
}
2526

2627
private string _testDirectory;

Assets/FbxExporters/Editor/UnitTests/FbxPrefabTest.cs

Lines changed: 83 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ public class FbxPrefabTest : ExporterTestBase
1212
FbxPrefab.FbxRepresentation m_originalRep;
1313

1414
GameObject m_source; // the fbx model
15+
FbxPrefab.FbxRepresentation m_originalHistory;
16+
1517
GameObject m_autoPrefab; // prefab that auto-updates
1618
GameObject m_manualPrefab; // prefab that doesn't auto-update
1719

@@ -49,61 +51,86 @@ void OnUpdate(FbxPrefab prefabInstance, IEnumerable<GameObject> updated)
4951
}
5052
}
5153

54+
static KeyValuePair<string, FbxPrefab.FbxRepresentation> StackItem(
55+
string name, FbxPrefab.FbxRepresentation rep)
56+
{
57+
return new KeyValuePair<string, FbxPrefab.FbxRepresentation>(name, rep);
58+
}
59+
5260
public static void AssertAreIdentical(
5361
FbxPrefab.FbxRepresentation a,
5462
FbxPrefab.FbxRepresentation b) {
5563
// A bit of a laborious comparison scheme. This is due to the
5664
// round-trip through FBX causing tiny errors in the transforms.
57-
var astack = new List<FbxPrefab.FbxRepresentation> ();
58-
astack.Add(a);
59-
var bstack = new List<FbxPrefab.FbxRepresentation> ();
60-
bstack.Add(b);
65+
var astack = new List<KeyValuePair<string, FbxPrefab.FbxRepresentation>> ();
66+
astack.Add(StackItem("(root)", a));
67+
var bstack = new List<KeyValuePair<string, FbxPrefab.FbxRepresentation>> ();
68+
bstack.Add(StackItem("(root)", b));
6169

6270
var aDummy = new GameObject("aDummy").transform;
6371
var bDummy = new GameObject("bDummy").transform;
6472
while (astack.Count > 0) {
6573
Assert.AreEqual(astack.Count, bstack.Count); // should never fail
66-
a = astack[astack.Count - 1]; astack.RemoveAt(astack.Count - 1);
67-
b = bstack[bstack.Count - 1]; bstack.RemoveAt(bstack.Count - 1);
74+
var aKvp = astack[astack.Count - 1]; astack.RemoveAt(astack.Count - 1);
75+
var bKvp = bstack[bstack.Count - 1]; bstack.RemoveAt(bstack.Count - 1);
76+
77+
var aName = aKvp.Key;
78+
var bName = bKvp.Key;
79+
Assert.AreEqual(aName, bName);
80+
81+
a = aKvp.Value;
82+
b = bKvp.Value;
6883

6984
// Verify that they have the same children (by name).
7085
var achildren = a.ChildNames;
7186
var bchildren = b.ChildNames;
72-
Assert.That(achildren, Is.EquivalentTo(bchildren));
87+
Assert.That(achildren, Is.EquivalentTo(bchildren), aName + " children");
7388

74-
// Add the children to each stack.
89+
// Add the children to each stack. It's important to get the
90+
// same order for both stacks.
7591
foreach(var child in achildren) {
76-
astack.Add(a.GetChild(child));
77-
bstack.Add(b.GetChild(child));
92+
astack.Add(StackItem(child, a.GetChild(child)));
93+
bstack.Add(StackItem(child, b.GetChild(child)));
7894
}
7995

8096
// Verify that they have the same components.
8197
var atypes = a.ComponentTypes;
8298
var btypes = b.ComponentTypes;
83-
Assert.That(atypes, Is.EquivalentTo(btypes));
99+
Assert.That(atypes, Is.EquivalentTo(btypes), aName + " component types");
84100

85101
foreach(var t in atypes) {
86102
var avalues = a.GetComponentValues(t);
87103
var bvalues = b.GetComponentValues(t);
88-
Assert.AreEqual(avalues.Count, bvalues.Count);
89-
90-
if (t != "UnityEngine.Transform") {
91-
Assert.AreEqual(avalues, bvalues);
92-
} else {
104+
Assert.AreEqual(avalues.Count, bvalues.Count, aName + " component multiplicity");
105+
106+
//
107+
// TODO: test that the values match up.
108+
// Exceptions:
109+
// - Transforms we expect small changes.
110+
// - MeshFilter and MeshRenderer we expect guids to change
111+
//
112+
if (t == "UnityEngine.MeshFilter") {
113+
// TODO: test that everything but the guid matches
114+
} else if (t == "UnityEngine.MeshRenderer") {
115+
// TODO: test that everything but the guid matches
116+
} else if (t == "UnityEngine.Transform") {
93117
// Verify that the transforms are nearly (but don't require bitwise) equal.
94118
EditorJsonUtility.FromJsonOverwrite(avalues[0], aDummy);
95119
EditorJsonUtility.FromJsonOverwrite(bvalues[0], bDummy);
96120
var dist = Vector3.Distance(aDummy.localPosition, bDummy.localPosition);
97-
Assert.That(dist, Is.LessThan(1e-6), () => string.Format("position {0} vs {1} dist {2}",
98-
aDummy.localPosition, bDummy.localPosition, dist));
121+
Assert.That(dist, Is.LessThan(1e-6), () => string.Format("{3}: position {0} vs {1} dist {2}",
122+
aDummy.localPosition, bDummy.localPosition, dist, aName));
99123

100124
dist = Vector3.Distance(aDummy.localScale, bDummy.localScale);
101-
Assert.That(dist, Is.LessThan(1e-6), () => string.Format("scale {0} vs {1} dist {2}",
102-
aDummy.localScale, bDummy.localScale, dist));
125+
Assert.That(dist, Is.LessThan(1e-6), () => string.Format("{3}: scale {0} vs {1} dist {2}",
126+
aDummy.localScale, bDummy.localScale, dist, aName));
103127

104128
dist = Quaternion.Angle(aDummy.localRotation, bDummy.localRotation);
105-
Assert.That(dist, Is.LessThan(1e-6), () => string.Format("rotation {0} vs {1} angle {2}",
106-
aDummy.localRotation.eulerAngles, bDummy.localRotation.eulerAngles, dist));
129+
Assert.That(dist, Is.LessThan(1e-6), () => string.Format("{3}: rotation {0} vs {1} angle {2}",
130+
aDummy.localRotation.eulerAngles, bDummy.localRotation.eulerAngles, dist, aName));
131+
} else {
132+
// Default: test that the components are precisely identical.
133+
Assert.AreEqual(avalues, bvalues, string.Format("{0}: type {1}", aName, t));
107134
}
108135
}
109136
}
@@ -126,6 +153,7 @@ public void Init() {
126153
var fbxAsset = FbxExporters.Editor.ModelExporter.ExportObject(
127154
GetRandomFbxFilePath(), m_original);
128155
m_source = AssetDatabase.LoadMainAssetAtPath(fbxAsset) as GameObject;
156+
m_originalHistory = Rep(m_source);
129157
Assert.IsTrue(m_source);
130158

131159
// Create an FbxPrefab linked to the Fbx file. Make it auto-update.
@@ -166,7 +194,11 @@ GameObject ModifySourceFbx()
166194
// - add parent3
167195
var newModel = PrefabUtility.InstantiatePrefab(m_source) as GameObject;
168196
GameObject.DestroyImmediate(newModel.transform.Find("Parent1").gameObject);
169-
CreateGameObject("Parent3", newModel.transform);
197+
var parent3 = CreateGameObject("Parent3", newModel.transform);
198+
199+
// We're not doing an apply operation, so the collider isn't
200+
// supposed to get to the prefab, so don't test that it does.
201+
Object.DestroyImmediate(parent3.GetComponent<BoxCollider>());
170202

171203
// Export it to clobber the old FBX file.
172204
// Sleep one second first to make sure the timestamp differs
@@ -183,29 +215,35 @@ GameObject ModifySourceFbx()
183215

184216
[Test]
185217
public void BasicTest() {
186-
// Check the history is good at the start
218+
// Verify we start in the right place.
187219
AssertAreIdentical(m_originalRep, Rep(m_manualPrefab));
188220
AssertAreIdentical(m_originalRep, Rep(m_autoPrefab));
189-
AssertAreIdentical(m_originalRep, History(m_manualPrefab));
190-
AssertAreIdentical(m_originalRep, History(m_autoPrefab));
221+
222+
// Verify history is as we expect. It differs from the originalRep
223+
// in that it doesn't have box colliders.
224+
AssertAreIdentical(m_originalHistory, History(m_manualPrefab));
225+
AssertAreIdentical(m_originalHistory, History(m_autoPrefab));
191226

192227
FbxPrefab.FbxRepresentation newHierarchy;
228+
FbxPrefab.FbxRepresentation newHistory;
193229
using(var updateSet = new UpdateListener(m_autoPrefab)) {
194230
Debug.Log("Testing auto update");
195231
newHierarchy = Rep(ModifySourceFbx());
232+
newHistory = Rep(m_source);
196233
AssertAreDifferent(m_originalRep, newHierarchy);
197234

198-
// Make sure the fbx source changed.
199-
AssertAreDifferent(m_originalRep, Rep(m_source));
200-
AssertAreIdentical(newHierarchy, Rep(m_source));
235+
// Make sure the fbx source changed (testing the test).
236+
AssertAreDifferent(m_originalHistory, Rep(m_source));
201237

202238
// Make sure the auto-update prefab changed.
239+
Debug.Log(string.Format("source: {0}\nprefab: {1}",
240+
newHierarchy.ToJson(), Rep(m_autoPrefab).ToJson()));
203241
AssertAreIdentical(newHierarchy, Rep(m_autoPrefab));
204-
AssertAreIdentical(newHierarchy, History(m_autoPrefab));
242+
AssertAreIdentical(newHierarchy, Rep(m_source));
205243

206244
// Make sure the manual-update prefab didn't.
207245
AssertAreIdentical(m_originalRep, Rep(m_manualPrefab));
208-
AssertAreIdentical(m_originalRep, History(m_manualPrefab));
246+
AssertAreIdentical(m_originalHistory, History(m_manualPrefab));
209247

210248
// Make sure we got the right changes.
211249
Assert.AreEqual (1, updateSet.NumUpdates);
@@ -223,7 +261,7 @@ public void BasicTest() {
223261
var manualPrefabComponent = m_manualPrefab.GetComponent<FbxPrefab>();
224262
manualPrefabComponent.SyncPrefab();
225263
AssertAreIdentical(newHierarchy, Rep(m_manualPrefab));
226-
AssertAreIdentical(newHierarchy, History(m_manualPrefab));
264+
AssertAreIdentical(newHistory, History(m_manualPrefab));
227265

228266
// Check some corner cases.
229267
Assert.AreEqual(m_source, manualPrefabComponent.GetFbxAsset());
@@ -245,38 +283,44 @@ public void BasicTest() {
245283
Assert.That( () => manualPrefabComponent.SetSourceModel(null), Throws.Nothing );
246284
Assert.IsNull(manualPrefabComponent.GetFbxAsset());
247285
AssertAreIdentical(newHierarchy, Rep(m_manualPrefab));
248-
AssertAreIdentical(newHierarchy, History(m_manualPrefab));
286+
AssertAreIdentical(newHistory, History(m_manualPrefab));
249287
Assert.That( () => manualPrefabComponent.SyncPrefab(), Throws.Nothing );
250288
AssertAreIdentical(newHierarchy, Rep(m_manualPrefab));
251-
AssertAreIdentical(newHierarchy, History(m_manualPrefab));
289+
AssertAreIdentical(newHistory, History(m_manualPrefab));
252290

253291
// Switch to some other model, which looks like the original model
254292
// (but is a totally different file). This will cause an update
255-
// immediately.
293+
// immediately. We expect to have lost the colliders on the objects
294+
// that were deleted in the interim.
256295
var fbxAsset = FbxExporters.Editor.ModelExporter.ExportObject(
257296
GetRandomFbxFilePath(), m_original);
258297
var newSource = AssetDatabase.LoadMainAssetAtPath(fbxAsset) as GameObject;
259298
Assert.IsTrue(newSource);
260299
Debug.Log("Testing SetSourceModel relink");
261300
manualPrefabComponent.SetSourceModel(newSource);
262301
AssertAreIdentical(m_originalRep, Rep(m_manualPrefab));
263-
AssertAreIdentical(m_originalRep, History(m_manualPrefab));
302+
AssertAreIdentical(m_originalHistory, History(m_manualPrefab));
264303
}
265304

266305
[Test]
267306
public void ManualToAuto() {
268307
// Check what happens when we go from manual to auto-update.
308+
Debug.Log("ManualToAuto: modifying source");
269309
var newHierarchy = Rep(ModifySourceFbx());
310+
var newHistory = Rep(m_source);
311+
270312
AssertAreIdentical(m_originalRep, Rep(m_manualPrefab));
271-
AssertAreIdentical(m_originalRep, History(m_manualPrefab));
313+
AssertAreIdentical(m_originalHistory, History(m_manualPrefab));
272314

315+
Debug.Log("ManualToAuto: setting manual to manual");
273316
m_manualPrefab.GetComponent<FbxPrefab>().SetAutoUpdate(false);
274317
AssertAreIdentical(m_originalRep, Rep(m_manualPrefab));
275-
AssertAreIdentical(m_originalRep, History(m_manualPrefab));
318+
AssertAreIdentical(m_originalHistory, History(m_manualPrefab));
276319

320+
Debug.Log("ManualToAuto: setting manual to auto");
277321
m_manualPrefab.GetComponent<FbxPrefab>().SetAutoUpdate(true);
278322
AssertAreIdentical(newHierarchy, Rep(m_manualPrefab));
279-
AssertAreIdentical(newHierarchy, History(m_manualPrefab));
323+
AssertAreIdentical(newHistory, History(m_manualPrefab));
280324
}
281325
}
282326

0 commit comments

Comments
 (0)