Skip to content

Commit 317857c

Browse files
committed
add reflection class to fix convert to model unit tests
1 parent 0a8c098 commit 317857c

File tree

3 files changed

+66
-9
lines changed

3 files changed

+66
-9
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Reflection;
4+
using UnityEngine;
5+
6+
namespace UnityEditor.Formats.Fbx.Exporter.UnitTests
7+
{
8+
/// <summary>
9+
// This class allows accessing the ConvertToModel methods that this
10+
// unit test needs to validate
11+
/// </summary>
12+
public class ConvertToModelReflection
13+
{
14+
public static GameObject GetOrCreateFbxAsset(GameObject toConvert,
15+
string fbxDirectoryFullPath = null,
16+
string fbxFullPath = null,
17+
ConvertToPrefabSettingsSerialize exportOptions = null)
18+
{
19+
return (GameObject)InvokeStaticMethod("GetOrCreateFbxAsset", new object[] { toConvert, fbxDirectoryFullPath, fbxFullPath, exportOptions });
20+
}
21+
22+
public static void CopyComponents(GameObject to, GameObject from)
23+
{
24+
InvokeStaticMethod("CopyComponents", new object[] { to, from });
25+
}
26+
27+
public static void UpdateFromSourceRecursive(GameObject dest, GameObject source)
28+
{
29+
InvokeStaticMethod("UpdateFromSourceRecursive", new object[] { dest, source });
30+
}
31+
32+
public static Dictionary<string, GameObject> MapNameToSourceRecursive(GameObject dest, GameObject source)
33+
{
34+
return (Dictionary<string, GameObject>)InvokeStaticMethod("MapNameToSourceRecursive", new object[] { dest, source });
35+
}
36+
37+
/////////// Helpers ///////////
38+
private static object InvokeStaticMethod(string methodName, object[] argsToPass)
39+
{
40+
// Use reflection to call the internal ModelExporter.GetGameObject static method
41+
var internalMethod = typeof(ConvertToModel).GetMethod(methodName,
42+
BindingFlags.Static | BindingFlags.NonPublic);
43+
return internalMethod.Invoke(null, argsToPass);
44+
}
45+
}
46+
}

Assets/com.unity.formats.fbx.tests/ConvertToModelReflection.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/com.unity.formats.fbx.tests/ConvertToModelTest.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,20 @@ public void TestStaticHelpers()
6262

6363
// Test on an object in the scene
6464
Assert.That(ConvertToModel.WillExportFbx(a));
65-
var aAsset = ConvertToModel.GetOrCreateFbxAsset(a, fbxFullPath: GetRandomFbxFilePath());
65+
var aAsset = ConvertToModelReflection.GetOrCreateFbxAsset(a, fbxFullPath: GetRandomFbxFilePath());
6666
Assert.AreNotEqual(a, aAsset);
6767
AssertSameHierarchy(a, aAsset, ignoreRootName: true);
6868
Assert.AreEqual(PrefabType.ModelPrefab, PrefabUtility.GetPrefabType(aAsset));
6969

7070
// Test on an fbx asset
7171
Assert.That(!ConvertToModel.WillExportFbx(aAsset));
72-
var aAssetAsset = ConvertToModel.GetOrCreateFbxAsset(aAsset, fbxFullPath: GetRandomFbxFilePath());
72+
var aAssetAsset = ConvertToModelReflection.GetOrCreateFbxAsset(aAsset, fbxFullPath: GetRandomFbxFilePath());
7373
Assert.AreEqual(aAsset, aAssetAsset);
7474

7575
// Test on an fbx instance
7676
var aAssetInstance = PrefabUtility.InstantiatePrefab(aAsset) as GameObject;
7777
Assert.That(!ConvertToModel.WillExportFbx(aAssetInstance));
78-
var aAssetInstanceAsset = ConvertToModel.GetOrCreateFbxAsset(aAssetInstance, fbxFullPath: GetRandomFbxFilePath());
78+
var aAssetInstanceAsset = ConvertToModelReflection.GetOrCreateFbxAsset(aAssetInstance, fbxFullPath: GetRandomFbxFilePath());
7979
Assert.AreEqual(aAsset, aAssetInstanceAsset);
8080
}
8181

@@ -88,7 +88,7 @@ public void TestStaticHelpers()
8888
Assert.AreEqual(a, b);
8989

9090
// Test on an fbx asset
91-
var aFbx = ConvertToModel.GetOrCreateFbxAsset(a, fbxFullPath: GetRandomFbxFilePath());
91+
var aFbx = ConvertToModelReflection.GetOrCreateFbxAsset(a, fbxFullPath: GetRandomFbxFilePath());
9292
var bFbx = ConvertToModel.GetOrCreateInstance(aFbx);
9393
Assert.AreNotEqual(aFbx, bFbx);
9494
Assert.AreEqual(aFbx, PrefabUtility.GetCorrespondingObjectFromSource(bFbx));
@@ -103,7 +103,7 @@ public void TestStaticHelpers()
103103
// Test SetupFbxPrefab
104104
{
105105
var a = CreateHierarchy();
106-
var aFbx = ConvertToModel.GetOrCreateFbxAsset(a, fbxFullPath: GetRandomFbxFilePath());
106+
var aFbx = ConvertToModelReflection.GetOrCreateFbxAsset(a, fbxFullPath: GetRandomFbxFilePath());
107107

108108
// We don't have an FbxPrefab; after setup we do; after a second setup we still only have one.
109109
Assert.AreEqual(0, a.GetComponents<FbxPrefab>().Length);
@@ -166,7 +166,7 @@ public void TestStaticHelpers()
166166
Assert.IsFalse(b.GetComponent<BoxCollider>());
167167
Assert.AreEqual(Vector3.zero, b.transform.localPosition);
168168
Assert.AreNotEqual (a.GetComponent<MeshFilter>().sharedMesh, b.GetComponent<MeshFilter> ().sharedMesh);
169-
ConvertToModel.CopyComponents(b, a);
169+
ConvertToModelReflection.CopyComponents(b, a);
170170
Assert.IsFalse(b.GetComponent<BoxCollider>());
171171
Assert.AreEqual(Vector3.zero, b.transform.localPosition);
172172
Assert.AreEqual (a.GetComponent<MeshFilter>().sharedMesh, b.GetComponent<MeshFilter> ().sharedMesh);
@@ -198,7 +198,7 @@ public void TestStaticHelpers()
198198
Assert.AreEqual ("BB", b.transform.GetChild (1).name);
199199
Assert.AreEqual (Vector3.zero, b1.transform.localPosition);
200200

201-
ConvertToModel.UpdateFromSourceRecursive (b, a);
201+
ConvertToModelReflection.UpdateFromSourceRecursive (b, a);
202202

203203
// only the mesh + materials should change
204204
Assert.AreEqual(b.GetComponent<MeshFilter>().sharedMesh, a.GetComponent<MeshFilter>().sharedMesh);
@@ -391,7 +391,7 @@ public void MapNameToSourceTest()
391391
quad2.transform.parent = cube2.transform;
392392
capsule.transform.SetSiblingIndex(1);
393393

394-
var dictionary = ConvertToModel.MapNameToSourceRecursive(cube, cube2);
394+
var dictionary = ConvertToModelReflection.MapNameToSourceRecursive(cube, cube2);
395395

396396
//We expect these to pass because we've given it an identical game object, as it would have after a normal export.
397397
Assert.AreSame(capsule2, dictionary[capsule.name]);
@@ -407,7 +407,7 @@ public void MapNameToSourceTest()
407407
capsule3.transform.parent = cube3.transform;
408408
sphere3.transform.parent = cube3.transform;
409409

410-
var dictionaryBroken = ConvertToModel.MapNameToSourceRecursive(cube, cube3);
410+
var dictionaryBroken = ConvertToModelReflection.MapNameToSourceRecursive(cube, cube3);
411411

412412
//the dictionary size should be equal to the amount of children + the parent
413413
Assert.True(dictionaryBroken.Count == 4);

0 commit comments

Comments
 (0)