Skip to content

Commit fcaa2b5

Browse files
committed
added unit test class for default selection
1 parent 329cfd3 commit fcaa2b5

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
using UnityEngine.TestTools;
4+
using NUnit.Framework;
5+
using System.IO;
6+
using System.Collections.Generic;
7+
8+
namespace FbxExporters.UnitTests
9+
{
10+
/// <summary>
11+
/// Tests the default selection export behavior.
12+
/// Tests that the right GameObjects are exported and
13+
/// that they have the expected transforms.
14+
/// </summary>
15+
public class DefaultSelectionTest
16+
{
17+
private string _filePath;
18+
protected string filePath { get { return string.IsNullOrEmpty(_filePath) ? Application.dataPath : _filePath; } set { _filePath = value; } }
19+
20+
private string _fileNamePrefix;
21+
protected string fileNamePrefix { get { return string.IsNullOrEmpty(_fileNamePrefix) ? "_safe_to_delete__" : _fileNamePrefix; }
22+
set { _fileNamePrefix = value; } }
23+
24+
private string _fileNameExt;
25+
protected string fileNameExt { get { return string.IsNullOrEmpty(_fileNameExt) ? ".fbx" : _fileNameExt; } set { _fileNameExt = value; } }
26+
27+
private string MakeFileName(string baseName = null, string prefixName = null, string extName = null)
28+
{
29+
if (baseName==null)
30+
baseName = Path.GetRandomFileName();
31+
32+
if (prefixName==null)
33+
prefixName = this.fileNamePrefix;
34+
35+
if (extName==null)
36+
extName = this.fileNameExt;
37+
38+
return prefixName + baseName + extName;
39+
}
40+
41+
protected string GetRandomFileNamePath(string pathName = null, string prefixName = null, string extName = null)
42+
{
43+
string temp;
44+
45+
if (pathName==null)
46+
pathName = this.filePath;
47+
48+
if (prefixName==null)
49+
prefixName = this.fileNamePrefix;
50+
51+
if (extName==null)
52+
extName = this.fileNameExt;
53+
54+
// repeat until you find a file that does not already exist
55+
do {
56+
temp = Path.Combine (pathName, MakeFileName(prefixName: prefixName, extName: extName));
57+
58+
} while(File.Exists (temp));
59+
60+
return temp;
61+
}
62+
63+
[TearDown]
64+
public void Term ()
65+
{
66+
foreach (string file in Directory.GetFiles (this.filePath, MakeFileName("*"))) {
67+
File.Delete (file);
68+
}
69+
}
70+
71+
[Test]
72+
public void TestDefaultSelection ()
73+
{
74+
var root = CreateHierarchy ();
75+
Assert.IsNotNull (root);
76+
77+
var exportedRoot = ExportSelection (root, new Object[]{root});
78+
79+
// test Export Root
80+
// Expected result: everything gets exported
81+
82+
// test Export Parent1, Child1
83+
// Expected result: Parent1, Child1, Child2
84+
85+
// test Export Child2
86+
// Expected result: Child2
87+
88+
// test Export Child2, Parent2
89+
// Expected result: Parent2, Child3, Child2
90+
91+
//UnityEngine.Object.DestroyImmediate (root);
92+
}
93+
94+
private GameObject CreateHierarchy ()
95+
{
96+
// Create the following hierarchy:
97+
// Root
98+
// -> Parent1
99+
// ----> Child1
100+
// ----> Child2
101+
// -> Parent2
102+
// ----> Child3
103+
104+
var root = CreateGameObject ("Root");
105+
106+
var parent1 = CreateGameObject ("Parent1", root.transform);
107+
var parent2 = CreateGameObject ("Parent2", root.transform);
108+
parent1.transform.SetAsFirstSibling ();
109+
110+
CreateGameObject ("Child1", parent1.transform);
111+
CreateGameObject ("Child2", parent1.transform);
112+
CreateGameObject ("Child3", parent2.transform);
113+
114+
return root;
115+
}
116+
117+
private GameObject CreateGameObject(string name, Transform parent = null)
118+
{
119+
var go = new GameObject (name);
120+
go.transform.SetParent (parent);
121+
return go;
122+
}
123+
124+
private void CompareHierarchies(GameObject expectedHierarchy, GameObject actualHierarchy, bool ignoreRoot = false)
125+
{
126+
if (!ignoreRoot) {
127+
Assert.AreEqual (expectedHierarchy.name, actualHierarchy.name);
128+
Assert.AreEqual (expectedHierarchy.transform.childCount, actualHierarchy.transform.childCount);
129+
}
130+
}
131+
132+
private GameObject ExportSelection(GameObject origRoot, Object[] selected)
133+
{
134+
// export selected to a file, then return the root
135+
var filename = GetRandomFileNamePath();
136+
137+
Debug.unityLogger.logEnabled = false;
138+
var fbxFileName = FbxExporters.Editor.ModelExporter.ExportObjects (filename, selected) as string;
139+
Debug.unityLogger.logEnabled = true;
140+
141+
Debug.LogWarning (filename + ", " + fbxFileName);
142+
Assert.IsNotNull (fbxFileName);
143+
144+
// make filepath relative to project folder
145+
if (fbxFileName.StartsWith (Application.dataPath, System.StringComparison.CurrentCulture))
146+
{
147+
fbxFileName = "Assets" + fbxFileName.Substring (Application.dataPath.Length);
148+
}
149+
// refresh the assetdata base so that we can query for the model
150+
AssetDatabase.Refresh ();
151+
152+
Object unityMainAsset = AssetDatabase.LoadMainAssetAtPath (fbxFileName);
153+
var fbxRoot = unityMainAsset as GameObject;
154+
155+
Assert.IsNotNull (fbxRoot);
156+
return fbxRoot;
157+
}
158+
}
159+
}

0 commit comments

Comments
 (0)