|
4 | 4 | using UnityEngine; |
5 | 5 | using USD.NET; |
6 | 6 | using USD.NET.Unity; |
| 7 | +using System.Linq; |
7 | 8 | using Object = UnityEngine.Object; |
| 9 | +using System; |
8 | 10 |
|
9 | 11 | namespace Unity.Formats.USD.Tests |
10 | 12 | { |
11 | 13 | public class ImportHelpersTests : BaseFixtureEditor |
12 | 14 | { |
13 | | - Scene CreateTestAsset(string fileName) |
| 15 | + Scene m_scene; |
| 16 | + |
| 17 | + [SetUp] |
| 18 | + public void CreateTestScene() |
| 19 | + { |
| 20 | + m_scene = CreateTestUsdScene(); |
| 21 | + } |
| 22 | + |
| 23 | + [Test] |
| 24 | + public void ImportAsPrefabTest_SceneClosedAfterImport() |
| 25 | + { |
| 26 | + ImportHelpers.ImportAsPrefab(m_scene, GetPrefabPath()); |
| 27 | + Assert.IsNull(m_scene.Stage, "Scene was not closed after import."); |
| 28 | + } |
| 29 | + |
| 30 | + [Test] |
| 31 | + public void ImportAsTimelineClipTest_SceneClosedAfterImport() |
| 32 | + { |
| 33 | + ImportHelpers.ImportAsTimelineClip(m_scene, GetPrefabPath()); |
| 34 | + Assert.IsNull(m_scene.Stage, "Scene was not closed after import."); |
| 35 | + } |
| 36 | + |
| 37 | + [Test] |
| 38 | + public void ImportAsPrefabTest_ImportClosedScene_Error() |
14 | 39 | { |
15 | | - var dummyUsdPath = CreateTmpUsdFile(fileName); |
16 | | - var scene = ImportHelpers.InitForOpen(dummyUsdPath); |
17 | | - scene.Write("/root", new XformSample()); |
18 | | - scene.Write("/root/sphere", new SphereSample()); |
19 | | - scene.Save(); |
20 | | - return scene; |
| 40 | + m_scene.Close(); |
| 41 | + Assert.Throws<System.NullReferenceException>(() => ImportHelpers.ImportAsPrefab(m_scene, GetPrefabPath())); |
| 42 | + } |
| 43 | + |
| 44 | + [Test] |
| 45 | + public void ImportAsTimelineClipTest_ImportClosedScene_Error() |
| 46 | + { |
| 47 | + m_scene.Close(); |
| 48 | + Assert.Throws<System.NullReferenceException>(() => ImportHelpers.ImportAsTimelineClip(m_scene, GetPrefabPath())); |
21 | 49 | } |
22 | 50 |
|
23 | 51 | [Test] |
24 | 52 | public void ImportAsPrefabTest_ContentOk() |
25 | 53 | { |
26 | | - var scene = CreateTestAsset("dummyUsd.usda"); |
27 | | - var assetPath = ImportHelpers.ImportAsPrefab(scene, GetPrefabPath()); |
28 | | - Assert.IsNull(scene.Stage, "Scene was not closed after import."); |
| 54 | + var assetPath = ImportHelpers.ImportAsPrefab(m_scene, GetPrefabPath()); |
29 | 55 |
|
30 | 56 | Assert.IsTrue(File.Exists(assetPath)); |
31 | | - var allObjects = AssetDatabase.LoadAllAssetsAtPath(assetPath); |
32 | | - Assert.NotZero(allObjects.Length); |
33 | | - bool playableAssetFound = false; |
34 | | - int goCount = 0; |
35 | | - int materialCount = 0; |
36 | | - foreach (Object thisObject in allObjects) |
37 | | - { |
38 | | - string myType = thisObject.GetType().Name; |
39 | | - if (myType == "UsdPlayableAsset") |
40 | | - { |
41 | | - playableAssetFound = true; |
42 | | - } |
43 | | - else if (myType == "GameObject") |
44 | | - { |
45 | | - goCount += 1; |
46 | | - } |
47 | | - else if (myType == "Material") |
48 | | - { |
49 | | - materialCount += 1; |
50 | | - } |
51 | | - } |
| 57 | + var usdAsObjects = AssetDatabase.LoadAllAssetsAtPath(assetPath); |
52 | 58 |
|
53 | | - Assert.IsTrue(playableAssetFound, "No PlayableAssset was found in the prefab."); |
54 | | - Assert.AreEqual(2, goCount, "Wrong GameObjects count in the prefab."); |
55 | | - // The 3 default materials + 1 material per meshRender |
56 | | - Assert.AreEqual(4, materialCount, "Wrong Materials count in the prefab"); |
| 59 | + // ExpectedGameObjectCount: The Root GameObject + Sphere GameObject added |
| 60 | + // ExpectedPrimSourceCount: Root Source + Sphere Source |
| 61 | + // ExpectedMaterialCount: The 3 default materials + 1 material from Sphere meshRender |
| 62 | + EditorImportAssert.IsValidImport(usdAsObjects, expectedGameObjectCount: 2, expectedPrimSourceCount: 2, expectedMaterialCount: 4); |
57 | 63 | } |
58 | 64 |
|
59 | 65 | [Test] |
60 | 66 | public void ImportAsTimelineClipTest_ContentOk() |
61 | 67 | { |
62 | 68 | // Import as timeline clip should not create a hierarchy, only the root and the playable |
63 | | - var scene = CreateTestAsset("dummyUsd.usda"); |
64 | | - var assetPath = ImportHelpers.ImportAsTimelineClip(scene, GetPrefabPath()); |
65 | | - Assert.IsNull(scene.Stage, "Scene was not closed after import."); |
| 69 | + var assetPath = ImportHelpers.ImportAsTimelineClip(m_scene, GetPrefabPath()); |
66 | 70 |
|
67 | 71 | Assert.IsTrue(File.Exists(assetPath)); |
68 | | - var allObjects = AssetDatabase.LoadAllAssetsAtPath(assetPath); |
69 | | - Assert.NotZero(allObjects.Length); |
| 72 | + var usdAsObjects = AssetDatabase.LoadAllAssetsAtPath(assetPath); |
| 73 | + |
| 74 | + // ExpectedGameObjectCount: The Root GameObject |
| 75 | + // ExpectedPrimSourceCount: 0 TODO: Shouldnt there be a prim source object for the root object? |
| 76 | + // ExpectedMaterialCount: The 3 default materials |
| 77 | + EditorImportAssert.IsValidImport(usdAsObjects, expectedGameObjectCount: 1, expectedPrimSourceCount: 0, expectedMaterialCount: 3); |
| 78 | + } |
| 79 | + |
| 80 | + [TearDown] |
| 81 | + public void ClearTestScene() |
| 82 | + { |
| 83 | + m_scene = null; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + public static class EditorImportAssert |
| 88 | + { |
| 89 | + enum ObjectTypeName |
| 90 | + { |
| 91 | + UsdPlayableAsset = 0, |
| 92 | + GameObject = 1, |
| 93 | + Material = 2, |
| 94 | + UsdPrimSource = 3 |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | + public static void IsValidImport(Object[] usdAsObjects, int expectedGameObjectCount, int expectedPrimSourceCount, int expectedMaterialCount) |
| 99 | + { |
| 100 | + Assert.NotZero(usdAsObjects.Length); |
| 101 | + |
70 | 102 | bool playableAssetFound = false; |
71 | | - int goCount = 0; |
| 103 | + int gameObjectCount = 0; |
72 | 104 | int materialCount = 0; |
73 | | - foreach (Object thisObject in allObjects) |
| 105 | + int usdPrimSourceCount = 0; |
| 106 | + |
| 107 | + foreach (Object childObject in usdAsObjects) |
74 | 108 | { |
75 | | - Debug.Log(thisObject.name); |
76 | | - Debug.Log(thisObject.GetType().Name); |
77 | | - string myType = thisObject.GetType().Name; |
78 | | - if (myType == "UsdPlayableAsset") |
79 | | - { |
80 | | - playableAssetFound = true; |
81 | | - } |
82 | | - else if (myType == "GameObject") |
| 109 | + switch (childObject.GetType().Name) |
83 | 110 | { |
84 | | - goCount += 1; |
85 | | - } |
86 | | - else if (myType == "Material") |
87 | | - { |
88 | | - materialCount += 1; |
| 111 | + case nameof(ObjectTypeName.UsdPlayableAsset): |
| 112 | + playableAssetFound = true; |
| 113 | + break; |
| 114 | + |
| 115 | + case nameof(ObjectTypeName.GameObject): |
| 116 | + gameObjectCount++; |
| 117 | + break; |
| 118 | + |
| 119 | + case nameof(ObjectTypeName.Material): |
| 120 | + materialCount++; |
| 121 | + break; |
| 122 | + |
| 123 | + case nameof(ObjectTypeName.UsdPrimSource): |
| 124 | + usdPrimSourceCount++; |
| 125 | + break; |
| 126 | + |
| 127 | + default: |
| 128 | + break; |
89 | 129 | } |
90 | 130 | } |
91 | 131 |
|
92 | 132 | Assert.IsTrue(playableAssetFound, "No PlayableAssset was found in the prefab."); |
93 | | - Assert.AreEqual(1, goCount, "Wrong GameObjects count in the prefab."); |
94 | | - // Only 3 default materials and no meshRenderer |
95 | | - Assert.AreEqual(3, materialCount, "Wrong Materials count in the prefab"); |
| 133 | + Assert.AreEqual(expectedGameObjectCount, gameObjectCount, "Wrong GameObjects count in the prefab."); |
| 134 | + Assert.AreEqual(expectedPrimSourceCount, usdPrimSourceCount, "Wrong USD Prim Source object count in the prefab"); |
| 135 | + Assert.AreEqual(expectedMaterialCount, materialCount, "Wrong Materials count in the prefab"); |
96 | 136 | } |
97 | 137 | } |
98 | 138 | } |
0 commit comments