Skip to content

Commit 19ea387

Browse files
authored
[USDU-274] Adds new test cases for ImportHelper functions (#327)
* [USDU-274] Adds new test cases for ImportHelper functions * Adds changes suggested in code review * Adds TearDown step for ImportHelperTests for editor tests
1 parent 87bdba9 commit 19ea387

File tree

5 files changed

+176
-85
lines changed

5 files changed

+176
-85
lines changed

package/com.unity.formats.usd/Tests/Common/BaseFixture.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
using NUnit.Framework;
1717
using UnityEngine;
1818
using UnityEditor;
19-
2019
using USDScene = USD.NET.Scene;
2120
using UnityScene = UnityEngine.SceneManagement.Scene;
21+
using USD.NET;
22+
using USD.NET.Unity;
2223

2324
namespace Unity.Formats.USD.Tests
2425
{
@@ -74,7 +75,7 @@ public string GetPrefabPath(string prefabName = null)
7475
return Path.Combine(ArtifactsDirectoryRelativePath, prefabName);
7576
}
7677

77-
public string CreateTmpUsdFile(string fileName)
78+
public string CreateTmpUsdFile(string fileName = "tempUsd.usda")
7879
{
7980
var usdScenePath = GetUSDScenePath(fileName);
8081
var scene = USDScene.Create(usdScenePath);
@@ -83,6 +84,16 @@ public string CreateTmpUsdFile(string fileName)
8384
return usdScenePath;
8485
}
8586

87+
public Scene CreateTestUsdScene(string fileName = "testUsd.usda")
88+
{
89+
var dummyUsdPath = CreateTmpUsdFile(fileName);
90+
var scene = ImportHelpers.InitForOpen(dummyUsdPath);
91+
scene.Write("/root", new XformSample());
92+
scene.Write("/root/sphere", new SphereSample());
93+
scene.Save();
94+
return scene;
95+
}
96+
8697
[SetUp]
8798
public void InitUSDAndArtifactsDirectory()
8899
{

package/com.unity.formats.usd/Tests/Editor/ImportHelpersTests.cs

Lines changed: 98 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,95 +4,135 @@
44
using UnityEngine;
55
using USD.NET;
66
using USD.NET.Unity;
7+
using System.Linq;
78
using Object = UnityEngine.Object;
9+
using System;
810

911
namespace Unity.Formats.USD.Tests
1012
{
1113
public class ImportHelpersTests : BaseFixtureEditor
1214
{
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()
1439
{
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()));
2149
}
2250

2351
[Test]
2452
public void ImportAsPrefabTest_ContentOk()
2553
{
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());
2955

3056
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);
5258

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);
5763
}
5864

5965
[Test]
6066
public void ImportAsTimelineClipTest_ContentOk()
6167
{
6268
// 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());
6670

6771
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+
70102
bool playableAssetFound = false;
71-
int goCount = 0;
103+
int gameObjectCount = 0;
72104
int materialCount = 0;
73-
foreach (Object thisObject in allObjects)
105+
int usdPrimSourceCount = 0;
106+
107+
foreach (Object childObject in usdAsObjects)
74108
{
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)
83110
{
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;
89129
}
90130
}
91131

92132
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");
96136
}
97137
}
98138
}

package/com.unity.formats.usd/Tests/Runtime/ExportHelpersTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void InitForSave_ValidPath()
4848
[Test]
4949
public void ExportGameObjects_NullScene()
5050
{
51-
var filePath = CreateTmpUsdFile("dummyUsd.usda");
51+
var filePath = CreateTmpUsdFile();
5252
var scene = Scene.Open(filePath);
5353
scene.Close();
5454
var fileInfoBefore = new FileInfo(filePath);
@@ -64,7 +64,7 @@ public void ExportGameObjects_NullScene()
6464
[Test]
6565
public void ExportGameObjects_EmptyList()
6666
{
67-
var filePath = CreateTmpUsdFile("dummyUsd.usda");
67+
var filePath = CreateTmpUsdFile();
6868
var scene = Scene.Open(filePath);
6969
var fileInfoBefore = new FileInfo(filePath);
7070
Assert.DoesNotThrow(delegate ()
@@ -79,7 +79,7 @@ public void ExportGameObjects_EmptyList()
7979
[Test]
8080
public void ExportGameObjects_InvalidGO()
8181
{
82-
var filePath = CreateTmpUsdFile("dummyUsd.usda");
82+
var filePath = CreateTmpUsdFile();
8383
var scene = Scene.Open(filePath);
8484
Assert.DoesNotThrow(delegate ()
8585
{
@@ -92,7 +92,7 @@ public void ExportGameObjects_InvalidGO()
9292
[Test]
9393
public void ExportGameObjects_ValidGO()
9494
{
95-
var filePath = CreateTmpUsdFile("dummyUsd.usda");
95+
var filePath = CreateTmpUsdFile();
9696
var scene = Scene.Open(filePath);
9797
ExportHelpers.ExportGameObjects(new[] { new GameObject("test") }, scene, BasisTransformation.SlowAndSafe);
9898
scene = Scene.Open(filePath);
@@ -105,7 +105,7 @@ public void ExportGameObjects_ValidGO()
105105
[Test]
106106
public void ExportGameObjects_SceneClosedAfterExport()
107107
{
108-
var filePath = CreateTmpUsdFile("dummyUsd.usda");
108+
var filePath = CreateTmpUsdFile();
109109
var scene = Scene.Open(filePath);
110110
ExportHelpers.ExportGameObjects(new[] { new GameObject("test") }, scene, BasisTransformation.SlowAndSafe);
111111
Assert.IsNull(scene.Stage);

package/com.unity.formats.usd/Tests/Runtime/ImportHelpersTests.cs

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,18 @@
33
using UnityEngine;
44
using UnityEngine.SceneManagement;
55
using USD.NET.Unity;
6-
using Scene = USD.NET.Scene;
6+
using System.Collections.Generic;
7+
using USD.NET;
8+
using System.Linq;
79

810
namespace Unity.Formats.USD.Tests
911
{
1012
public class ImportHelpersTests : BaseFixtureRuntime
1113
{
12-
Scene CreateTmpUsdWithData(string fileName)
13-
{
14-
var dummyUsdPath = CreateTmpUsdFile(fileName);
15-
var scene = ImportHelpers.InitForOpen(dummyUsdPath);
16-
scene.Write("/root", new XformSample());
17-
scene.Write("/root/sphere", new SphereSample());
18-
scene.Save();
19-
return scene;
20-
}
21-
2214
[Test]
2315
public void InitForOpenTest_ValidPath_Succeeds()
2416
{
25-
var dummyUsdPath = CreateTmpUsdFile("dummyUsd.usda");
17+
var dummyUsdPath = CreateTmpUsdFile();
2618
var scene = ImportHelpers.InitForOpen(dummyUsdPath);
2719
Assert.NotNull(scene);
2820
Assert.NotNull(scene.Stage);
@@ -48,7 +40,7 @@ public void InitForOpenTest_InvalidPath_ThrowsAndLogs()
4840
[Test]
4941
public void ImportAsGameObjects_ImportAtRoot()
5042
{
51-
var scene = CreateTmpUsdWithData("dummyUsd.usda");
43+
var scene = CreateTestUsdScene();
5244
var root = ImportHelpers.ImportSceneAsGameObject(scene);
5345
bool usdRootIsRoot = Array.Find(SceneManager.GetActiveScene().GetRootGameObjects(), r => r == root);
5446
Assert.IsTrue(usdRootIsRoot, "UsdAsset GameObject is not a root GameObject.");
@@ -58,34 +50,71 @@ public void ImportAsGameObjects_ImportAtRoot()
5850
public void ImportAsGameObjects_ImportUnderParent()
5951
{
6052
var root = new GameObject("thisIsTheRoot");
61-
var scene = CreateTmpUsdWithData("dummyUsd.usda");
53+
var scene = CreateTestUsdScene();
6254
var usdRoot = ImportHelpers.ImportSceneAsGameObject(scene, root);
6355
Assert.AreEqual(root.transform, usdRoot.transform.root, "UsdAsset is not a children of the given parent.");
6456
}
6557

6658
[Test]
6759
public void ImportAsGameObjects_SceneClosedAfterImport()
6860
{
69-
var scene = CreateTmpUsdWithData("dummyUsd.usda");
70-
var root = ImportHelpers.ImportSceneAsGameObject(scene);
61+
var scene = CreateTestUsdScene();
62+
ImportHelpers.ImportSceneAsGameObject(scene);
7163
Assert.IsNull(scene.Stage, "Scene was not closed after import.");
7264
}
7365

7466
[Test]
7567
public void ImportAsGameObjects_ImportClosedScene_LogsError()
7668
{
77-
var rootGOsBefore = SceneManager.GetActiveScene().GetRootGameObjects();
78-
var scene = CreateTmpUsdWithData("dummyUsd.usda");
69+
var scene = CreateTestUsdScene();
7970
scene.Close();
8071
var root = ImportHelpers.ImportSceneAsGameObject(scene);
8172
Assert.IsNull(root);
8273
UnityEngine.TestTools.LogAssert.Expect(LogType.Error, "The USD Scene needs to be opened before being imported.");
8374
}
8475

8576
[Test]
86-
[Ignore("TODO")]
87-
public void ImportAsGameObjects_CleanupAfterError()
77+
public void ImportAsGameObjects_CleanupAfterErrorAtRoot()
8878
{
79+
var scenePath = CreateTmpUsdFile();
80+
var scene = ImportHelpers.InitForOpen(scenePath);
81+
scene.Close();
82+
83+
// This will cause an error as scene is closed
84+
var usdObject = ImportHelpers.ImportSceneAsGameObject(scene);
85+
86+
var rootGameObjects = SceneManager.GetActiveScene().GetRootGameObjects();
87+
Assert.IsEmpty(rootGameObjects.Where(o => o == usdObject), "UsdAsset GameObject was not cleaned up after error at root");
88+
UnityEngine.TestTools.LogAssert.Expect(LogType.Error, "The USD Scene needs to be opened before being imported.");
89+
}
90+
91+
[Test]
92+
public void ImportAsGameObjects_CleanupAfterErrorUnderParent()
93+
{
94+
var parent = new GameObject();
95+
var scenePath = CreateTmpUsdFile();
96+
var scene = ImportHelpers.InitForOpen(scenePath);
97+
scene.Close();
98+
99+
// This will cause an error as scene is closed
100+
ImportHelpers.ImportSceneAsGameObject(scene, parent);
101+
102+
Assert.IsTrue(parent.transform.childCount == 0, "UsdAsset GameObject was not cleaned up after error under parent");
103+
UnityEngine.TestTools.LogAssert.Expect(LogType.Error, "The USD Scene needs to be opened before being imported.");
104+
}
105+
106+
[Test]
107+
public void ImportAsGameObjects_UnderInactiveParent()
108+
{
109+
var parent = new GameObject();
110+
var scenePath = CreateTmpUsdFile();
111+
var scene = ImportHelpers.InitForOpen(scenePath);
112+
113+
parent.SetActive(false);
114+
var usdObject = ImportHelpers.ImportSceneAsGameObject(scene, parent);
115+
116+
Assert.IsFalse(usdObject.activeInHierarchy);
117+
Assert.IsTrue(usdObject.activeSelf, "The USD Scene is self-inactive when imported under an inactive parent");
89118
}
90119
}
91120
}

0 commit comments

Comments
 (0)