|
| 1 | +using UnityEngine; |
| 2 | +using UnityEditor; |
| 3 | +using UnityEngine.TestTools; |
| 4 | +using NUnit.Framework; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.IO; |
| 7 | +using FbxExporters.Editor; |
| 8 | + |
| 9 | +namespace FbxExporters.UnitTests |
| 10 | +{ |
| 11 | + public class ConvertToModelTest : ExporterTestBase |
| 12 | + { |
| 13 | + public static List<string> ChildNames(Transform a) { |
| 14 | + var names = new List<string>(); |
| 15 | + foreach(Transform child in a) { |
| 16 | + names.Add(child.name); |
| 17 | + } |
| 18 | + return names; |
| 19 | + } |
| 20 | + |
| 21 | + [Test] |
| 22 | + public void TestStaticHelpers() |
| 23 | + { |
| 24 | + // Test IncrementFileName |
| 25 | + { |
| 26 | + var tempPath = Path.GetTempPath (); |
| 27 | + var basename = Path.GetFileNameWithoutExtension (Path.GetRandomFileName ()); |
| 28 | + |
| 29 | + var filename1 = basename + " 1.fbx"; |
| 30 | + var filename2 = Path.Combine(tempPath, basename + " 2.fbx"); |
| 31 | + Assert.AreEqual (filename2, ConvertToModel.IncrementFileName (tempPath, filename1)); |
| 32 | + |
| 33 | + filename1 = basename + "1.fbx"; |
| 34 | + filename2 = Path.Combine(tempPath, basename + "2.fbx"); |
| 35 | + Assert.AreEqual (filename2, ConvertToModel.IncrementFileName (tempPath, filename1)); |
| 36 | + |
| 37 | + filename1 = basename + "k.fbx"; |
| 38 | + filename2 = Path.Combine(tempPath, basename + "k 1.fbx"); |
| 39 | + Assert.AreEqual (filename2, ConvertToModel.IncrementFileName (tempPath, filename1)); |
| 40 | + } |
| 41 | + |
| 42 | + // Test EnforceUniqueNames |
| 43 | + { |
| 44 | + var a = new GameObject("a"); |
| 45 | + var b = new GameObject("b"); |
| 46 | + var a1 = new GameObject("a"); |
| 47 | + var a2 = new GameObject("a"); |
| 48 | + ConvertToModel.EnforceUniqueNames(new GameObject[] { a, b, a1, a2 }); |
| 49 | + Assert.AreEqual("a", a.name); |
| 50 | + Assert.AreEqual("b", b.name); |
| 51 | + Assert.AreEqual("a 1", a1.name); |
| 52 | + Assert.AreEqual("a 2", a2.name); |
| 53 | + } |
| 54 | + |
| 55 | + // Test FixSiblingOrder |
| 56 | + { |
| 57 | + var a = new GameObject("a").transform; |
| 58 | + var b = new GameObject("a").transform; |
| 59 | + |
| 60 | + var a1 = new GameObject("a1").transform; |
| 61 | + var a2 = new GameObject("a2").transform; |
| 62 | + var a3 = new GameObject("a3").transform; |
| 63 | + |
| 64 | + var b1 = new GameObject("a1").transform; |
| 65 | + var b2 = new GameObject("a2").transform; |
| 66 | + var b3 = new GameObject("a3").transform; |
| 67 | + |
| 68 | + a1.parent = a; |
| 69 | + a2.parent = a; |
| 70 | + a3.parent = a; |
| 71 | + |
| 72 | + b3.parent = b; |
| 73 | + b1.parent = b; |
| 74 | + b2.parent = b; |
| 75 | + |
| 76 | + // Assert same set, different order. |
| 77 | + Assert.That(ChildNames(b), Is.EquivalentTo(ChildNames(a))); |
| 78 | + Assert.That(ChildNames(b), Is.Not.EqualTo(ChildNames(a))); |
| 79 | + |
| 80 | + // Fix the sibling order. Now we have same set, same order! |
| 81 | + ConvertToModel.FixSiblingOrder(a, b); |
| 82 | + Assert.That(ChildNames(b), Is.EquivalentTo(ChildNames(a))); |
| 83 | + Assert.That(ChildNames(b), Is.EqualTo(ChildNames(a))); |
| 84 | + } |
| 85 | + |
| 86 | + // Test CopyComponents |
| 87 | + { |
| 88 | + var a = new GameObject("a"); |
| 89 | + var b = new GameObject("b"); |
| 90 | + a.AddComponent<BoxCollider>(); |
| 91 | + a.transform.localPosition += new Vector3(1,2,3); |
| 92 | + Assert.IsFalse(b.GetComponent<BoxCollider>()); |
| 93 | + Assert.AreEqual(Vector3.zero, b.transform.localPosition); |
| 94 | + ConvertToModel.CopyComponents(a, b); |
| 95 | + Assert.IsTrue(b.GetComponent<BoxCollider>()); |
| 96 | + Assert.AreEqual(new Vector3(1,2,3), b.transform.localPosition); |
| 97 | + } |
| 98 | + |
| 99 | + // Test SetupImportedGameObject. Very similar but recursive. |
| 100 | + { |
| 101 | + var a = new GameObject ("a"); |
| 102 | + var a1 = new GameObject ("AA"); |
| 103 | + var a2 = new GameObject ("BB"); |
| 104 | + a2.transform.parent = a.transform; |
| 105 | + a1.transform.parent = a.transform; // out of alpha order! |
| 106 | + var b = new GameObject ("b"); |
| 107 | + var b1 = new GameObject ("AA"); |
| 108 | + var b2 = new GameObject ("BB"); |
| 109 | + b1.transform.parent = b.transform; |
| 110 | + b2.transform.parent = b.transform; // in alpha order |
| 111 | + a.AddComponent<BoxCollider> (); |
| 112 | + a1.transform.localPosition = new Vector3 (1, 2, 3); |
| 113 | + |
| 114 | + Assert.IsFalse (b.GetComponent<BoxCollider> ()); |
| 115 | + Assert.AreEqual ("BB", b.transform.GetChild (1).name); |
| 116 | + Assert.AreEqual (Vector3.zero, b1.transform.localPosition); |
| 117 | + |
| 118 | + ConvertToModel.SetupImportedGameObject (a, b); |
| 119 | + |
| 120 | + Assert.IsTrue (b.GetComponent<BoxCollider> ()); |
| 121 | + Assert.AreEqual ("AA", b.transform.GetChild (1).name); |
| 122 | + Assert.AreEqual (new Vector3 (1, 2, 3), b1.transform.localPosition); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + [Test] |
| 127 | + public void BasicTest() |
| 128 | + { |
| 129 | + // Get a random directory. |
| 130 | + var path = GetRandomFileNamePath(extName: ""); |
| 131 | + |
| 132 | + // Create a cube. |
| 133 | + var cube = GameObject.CreatePrimitive(PrimitiveType.Cube); |
| 134 | + |
| 135 | + // Convert it to a prefab. |
| 136 | + var cubePrefabInstance = ConvertToModel.Convert(cube, |
| 137 | + directoryFullPath: path, keepOriginal: true); |
| 138 | + |
| 139 | + // Make sure it's what we expect. |
| 140 | + Assert.That(cube); // we kept the original |
| 141 | + Assert.That(cubePrefabInstance); // we got the new |
| 142 | + Assert.AreEqual("Cube", cubePrefabInstance.name); // it has the right name |
| 143 | + Assert.That(!EditorUtility.IsPersistent(cubePrefabInstance)); |
| 144 | + var cubePrefabAsset = PrefabUtility.GetPrefabParent(cubePrefabInstance); |
| 145 | + Assert.That(cubePrefabAsset); |
| 146 | + |
| 147 | + // it's a different mesh instance, but the same mesh |
| 148 | + Assert.AreNotEqual( |
| 149 | + cube.GetComponent<MeshFilter>().sharedMesh, |
| 150 | + cubePrefabInstance.GetComponent<MeshFilter>().sharedMesh); |
| 151 | + Assert.That(cubePrefabInstance.GetComponent<FbxPrefab>()); |
| 152 | + |
| 153 | + // Should be all the same triangles. But it isn't. TODO. |
| 154 | + // At least the indices should match in multiplicity. |
| 155 | + var cubeMesh = cube.GetComponent<MeshFilter>().sharedMesh; |
| 156 | + var cubePrefabMesh = cubePrefabInstance.GetComponent<MeshFilter>().sharedMesh; |
| 157 | + //Assert.That( |
| 158 | + // cubeMesh.triangles, |
| 159 | + // Is.EqualTo(cubePrefabMesh.triangles) |
| 160 | + //); |
| 161 | + Assert.That(cubeMesh.triangles, Is.EquivalentTo(cubeMesh.triangles)); |
| 162 | + |
| 163 | + // Make sure it's where we expect. |
| 164 | + var assetRelativePath = AssetDatabase.GetAssetPath(cubePrefabAsset); |
| 165 | + var assetFullPath = Path.GetFullPath(Path.Combine(Application.dataPath, |
| 166 | + "../" + assetRelativePath)); |
| 167 | + Assert.AreEqual(Path.GetFullPath(path), Path.GetDirectoryName(assetFullPath)); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments