From 846e7a63946887a796bd009eb52b2c731c759774 Mon Sep 17 00:00:00 2001 From: Andreas Atteneder Date: Tue, 26 Oct 2021 20:56:34 +0200 Subject: [PATCH] refactor: Moved tests to glTF test framework, since the content (assets, scenes) is also there --- Runtime/Scripts/Export/GltfWriter.cs | 2 +- Tests/Runtime.meta | 8 - Tests/Runtime/GameObjectExportTest.cs | 305 --------------------- Tests/Runtime/GameObjectExportTest.cs.meta | 11 - Tests/Runtime/Utils.cs | 34 --- Tests/Runtime/Utils.cs.meta | 11 - Tests/Runtime/glTFast.Tests.asmdef | 30 -- Tests/Runtime/glTFast.Tests.asmdef.meta | 7 - 8 files changed, 1 insertion(+), 407 deletions(-) delete mode 100644 Tests/Runtime.meta delete mode 100644 Tests/Runtime/GameObjectExportTest.cs delete mode 100644 Tests/Runtime/GameObjectExportTest.cs.meta delete mode 100644 Tests/Runtime/Utils.cs delete mode 100644 Tests/Runtime/Utils.cs.meta delete mode 100644 Tests/Runtime/glTFast.Tests.asmdef delete mode 100644 Tests/Runtime/glTFast.Tests.asmdef.meta diff --git a/Runtime/Scripts/Export/GltfWriter.cs b/Runtime/Scripts/Export/GltfWriter.cs index b59cad13..4028cda0 100644 --- a/Runtime/Scripts/Export/GltfWriter.cs +++ b/Runtime/Scripts/Export/GltfWriter.cs @@ -47,8 +47,8 @@ using UnityEditor; #endif -[assembly: InternalsVisibleTo("glTFast.Tests")] [assembly: InternalsVisibleTo("glTFastEditor")] +[assembly: InternalsVisibleTo("glTF-test-framework.Tests")] namespace GLTFast.Export { diff --git a/Tests/Runtime.meta b/Tests/Runtime.meta deleted file mode 100644 index 42607c00..00000000 --- a/Tests/Runtime.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: ec249f48bc4024561a6ace3465bbe2e0 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Tests/Runtime/GameObjectExportTest.cs b/Tests/Runtime/GameObjectExportTest.cs deleted file mode 100644 index 51c90846..00000000 --- a/Tests/Runtime/GameObjectExportTest.cs +++ /dev/null @@ -1,305 +0,0 @@ -// Copyright 2020-2021 Andreas Atteneder -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -using System; -using System.Collections; -using System.IO; -using System.Linq; -using System.Threading.Tasks; -using GLTFast.Export; -using NUnit.Framework; -using UnityEngine; -using UnityEngine.SceneManagement; -using UnityEngine.TestTools; - -#if GLTF_VALIDATOR && UNITY_EDITOR -using Unity.glTF.Validator; -#endif - -namespace GLTFast.Tests { - - [TestFixture] - public class GameObjectExportTest { - - [OneTimeSetUp] - public void SetupTest() { - SceneManager.LoadScene("ExportScene", LoadSceneMode.Single); - } - - [UnityTest] - public IEnumerator SimpleTree() { - - var root = new GameObject("root"); - var childA = GameObject.CreatePrimitive(PrimitiveType.Cube); - childA.name = "child A"; - var childB = GameObject.CreatePrimitive(PrimitiveType.Sphere); - childB.name = "child B"; - childA.transform.parent = root.transform; - childB.transform.parent = root.transform; - childB.transform.localPosition = new Vector3(1, 0, 0); - - var logger = new CollectingLogger(); - var export = new GameObjectExport(logger:logger); - export.AddScene(new []{root}, "UnityScene"); - var path = Path.Combine(Application.persistentDataPath, "root.gltf"); - var task = export.SaveToFileAndDispose(path); - yield return Utils.WaitForTask(task); - var success = task.Result; - Assert.IsTrue(success); - AssertLogger(logger); -#if GLTF_VALIDATOR && UNITY_EDITOR - ValidateGltf(path, MessageCode.UNUSED_OBJECT); -#endif - } - - [UnityTest] - public IEnumerator ExportSceneGameObjectsJson() { - yield return null; - var task = ExportSceneGameObjects(false); - yield return Utils.WaitForTask(task); - } - - [UnityTest] - public IEnumerator ExportSceneGameObjectsBinary() { - yield return null; - var task = ExportSceneGameObjects(true); - yield return Utils.WaitForTask(task); - } - - [UnityTest] - public IEnumerator ExportSceneAllJson() { - yield return null; - var task = ExportSceneAll(false); - yield return Utils.WaitForTask(task); - } - - [UnityTest] - public IEnumerator ExportSceneAllBinary() { - yield return null; - var task = ExportSceneAll(true); - yield return Utils.WaitForTask(task); - } - - [Test] - public void MeshMaterialCombinationTest() { - - var mc1 = new GltfWriter.MeshMaterialCombination(42,new [] {1,2,3}); - var mc2 = new GltfWriter.MeshMaterialCombination(42,new [] {1,2,3}); - - Assert.AreEqual(mc1,mc2); - - mc1 = new GltfWriter.MeshMaterialCombination(42,new [] {1,2,4}); - Assert.AreNotEqual(mc1,mc2); - - mc1 = new GltfWriter.MeshMaterialCombination(42,new [] {1,2}); - Assert.AreNotEqual(mc1,mc2); - - mc1 = new GltfWriter.MeshMaterialCombination(42,null); - Assert.AreNotEqual(mc1,mc2); - - mc2 = new GltfWriter.MeshMaterialCombination(42,null); - Assert.AreEqual(mc1,mc2); - - mc1 = new GltfWriter.MeshMaterialCombination(13,null); - Assert.AreNotEqual(mc1,mc2); - } - - [UnityTest] - public IEnumerator TwoScenes() { - - var childA = GameObject.CreatePrimitive(PrimitiveType.Cube); - childA.name = "child A"; - - var logger = new CollectingLogger(); - var export = new GameObjectExport(logger:logger); - export.AddScene(new []{childA}, "scene A"); - export.AddScene(new []{childA}, "scene B"); - var path = Path.Combine(Application.persistentDataPath, "TwoScenes.gltf"); - var task = export.SaveToFileAndDispose(path); - yield return Utils.WaitForTask(task); - var success = task.Result; - Assert.IsTrue(success); - AssertLogger(logger); -#if GLTF_VALIDATOR && UNITY_EDITOR - ValidateGltf(path, MessageCode.UNUSED_OBJECT); -#endif - } - - [UnityTest] - public IEnumerator Empty() { - - var logger = new CollectingLogger(); - var export = new GameObjectExport(logger:logger); - var path = Path.Combine(Application.persistentDataPath, "Empty.gltf"); - var task = export.SaveToFileAndDispose(path); - yield return Utils.WaitForTask(task); - var success = task.Result; - Assert.IsTrue(success); - AssertLogger(logger); -#if GLTF_VALIDATOR && UNITY_EDITOR - ValidateGltf(path, MessageCode.UNUSED_OBJECT); -#endif - } - - - [UnityTest] - public IEnumerator SavedTwice() { - - var childA = GameObject.CreatePrimitive(PrimitiveType.Cube); - childA.name = "child A"; - - var logger = new CollectingLogger(); - var export = new GameObjectExport(logger:logger); - export.AddScene(new []{childA}); - var path = Path.Combine(Application.persistentDataPath, "SavedTwice1.gltf"); - var task = export.SaveToFileAndDispose(path); - yield return Utils.WaitForTask(task); - var success = task.Result; - Assert.IsTrue(success); - AssertLogger(logger); -#if GLTF_VALIDATOR && UNITY_EDITOR - ValidateGltf(path, MessageCode.UNUSED_OBJECT); -#endif - Assert.Throws(delegate() - { - export.AddScene(new []{childA}); - }); - path = Path.Combine(Application.persistentDataPath, "SavedTwice2.gltf"); - AssertThrowsAsync(async () => await export.SaveToFileAndDispose(path)); - } - - async Task ExportSceneGameObjects(bool binary) { - var scene = SceneManager.GetActiveScene(); - - var rootObjects = scene.GetRootGameObjects(); - - Assert.AreEqual(21,rootObjects.Length); - foreach (var gameObject in rootObjects) { - var logger = new CollectingLogger(); - var export = new GameObjectExport( - new ExportSettings { - format = binary ? GltfFormat.Binary : GltfFormat.Json, - fileConflictResolution = FileConflictResolution.Overwrite, - }, - logger: logger - ); - export.AddScene(new []{gameObject}, gameObject.name); - var extension = binary ? GltfGlobals.glbExt : GltfGlobals.gltfExt; - var path = Path.Combine(Application.persistentDataPath, $"{gameObject.name}{extension}"); - var success = await export.SaveToFileAndDispose(path); - Assert.IsTrue(success); - AssertLogger(logger); -#if GLTF_VALIDATOR && UNITY_EDITOR - ValidateGltf(path, new [] { - MessageCode.ACCESSOR_MAX_MISMATCH, - MessageCode.ACCESSOR_MIN_MISMATCH, - MessageCode.NODE_EMPTY, - MessageCode.UNUSED_OBJECT, - }); -#endif - } - } - - async Task ExportSceneAll(bool binary) { - SceneManager.LoadScene("ExportScene", LoadSceneMode.Single); - - var scene = SceneManager.GetActiveScene(); - - var rootObjects = scene.GetRootGameObjects(); - - var logger = new CollectingLogger(); - var export = new GameObjectExport( - new ExportSettings { - format = binary ? GltfFormat.Binary : GltfFormat.Json, - fileConflictResolution = FileConflictResolution.Overwrite, - }, - logger: logger - ); - export.AddScene(rootObjects, "ExportScene"); - var extension = binary ? GltfGlobals.glbExt : GltfGlobals.gltfExt; - var path = Path.Combine(Application.persistentDataPath, $"ExportScene{extension}"); - var success = await export.SaveToFileAndDispose(path); - Assert.IsTrue(success); - AssertLogger(logger); -#if GLTF_VALIDATOR && UNITY_EDITOR - ValidateGltf(path, new [] { - MessageCode.ACCESSOR_ELEMENT_OUT_OF_MAX_BOUND, - MessageCode.ACCESSOR_MAX_MISMATCH, - MessageCode.ACCESSOR_MIN_MISMATCH, - MessageCode.NODE_EMPTY, - MessageCode.UNUSED_OBJECT, - }); -#endif - } - - void AssertLogger(CollectingLogger logger) { - logger.LogAll(); - if (logger.items != null) { - foreach (var item in logger.items) { - Assert.AreEqual(LogType.Log, item.type, item.ToString()); - } - } - } - - /// - /// Fill-in for NUnit's Assert.ThrowsAsync - /// Source: https://forum.unity.com/threads/can-i-replace-upgrade-unitys-nunit.488580/#post-6543523 - /// - /// - /// - /// - /// - /// - /// - public static TActual AssertThrowsAsync(AsyncTestDelegate code, string message = "", params object[] args) where TActual : Exception - { - return Assert.Throws(() => - { - try - { - code.Invoke().Wait(); // Will wrap any exceptions in an AggregateException - } - catch (AggregateException e) - { - if (e.InnerException is null) - { - throw; - } - throw e.InnerException; // Throw the unwrapped exception - } - }, message, args); - } - - public delegate Task AsyncTestDelegate(); - - -#if GLTF_VALIDATOR && UNITY_EDITOR - void ValidateGltf(string path, params MessageCode[] expectedMessages) { - var report = Validator.Validate(path); - Assert.NotNull(report, $"Report null for {path}"); - // report.Log(); - if (report.issues != null) { - foreach (var message in report.issues.messages) { - if (expectedMessages.Contains(message.codeEnum)) { - continue; - } - Assert.Less(1, message.severity, $"Error {message} (path {Path.GetFileName(path)})"); - Assert.Less(2, message.severity, $"Warning {message} (path {Path.GetFileName(path)})"); - } - } - } -#endif - } -} diff --git a/Tests/Runtime/GameObjectExportTest.cs.meta b/Tests/Runtime/GameObjectExportTest.cs.meta deleted file mode 100644 index 4dca0797..00000000 --- a/Tests/Runtime/GameObjectExportTest.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 8b69c11f160c74445b71594d538f17f4 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Tests/Runtime/Utils.cs b/Tests/Runtime/Utils.cs deleted file mode 100644 index c789701d..00000000 --- a/Tests/Runtime/Utils.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright 2020-2021 Andreas Atteneder -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -using System.Collections; -using System.Threading.Tasks; -using UnityEngine; - -namespace GLTFast.Tests { - - public static class Utils { - - public static IEnumerator WaitForTask(Task task) { - while(!task.IsCompleted) { - if (task.Exception != null) - throw task.Exception; - yield return null; - } - if (task.Exception != null) - throw task.Exception; - } - } -} diff --git a/Tests/Runtime/Utils.cs.meta b/Tests/Runtime/Utils.cs.meta deleted file mode 100644 index d37530b1..00000000 --- a/Tests/Runtime/Utils.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: c06ae487c51924137ab3c3c7041eb4d2 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Tests/Runtime/glTFast.Tests.asmdef b/Tests/Runtime/glTFast.Tests.asmdef deleted file mode 100644 index d7066b21..00000000 --- a/Tests/Runtime/glTFast.Tests.asmdef +++ /dev/null @@ -1,30 +0,0 @@ -{ - "name": "glTFast.Tests", - "rootNamespace": "", - "references": [ - "glTFast", - "glTFast.Export", - "UnityEngine.TestRunner", - "UnityEditor.TestRunner", - "com.unity.formats.gltf.validator.Editor" - ], - "includePlatforms": [], - "excludePlatforms": [], - "allowUnsafeCode": false, - "overrideReferences": true, - "precompiledReferences": [ - "nunit.framework.dll" - ], - "autoReferenced": false, - "defineConstraints": [ - "UNITY_INCLUDE_TESTS" - ], - "versionDefines": [ - { - "name": "com.unity.formats.gltf.validator", - "expression": "", - "define": "GLTF_VALIDATOR" - } - ], - "noEngineReferences": false -} \ No newline at end of file diff --git a/Tests/Runtime/glTFast.Tests.asmdef.meta b/Tests/Runtime/glTFast.Tests.asmdef.meta deleted file mode 100644 index 6ab5144f..00000000 --- a/Tests/Runtime/glTFast.Tests.asmdef.meta +++ /dev/null @@ -1,7 +0,0 @@ -fileFormatVersion: 2 -guid: 85ec0a874ef9342d48a98a470304e86b -AssemblyDefinitionImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: