From 2c11cc83939f26a01dd0c786abd7725c622adb04 Mon Sep 17 00:00:00 2001 From: James Frowen Date: Mon, 4 May 2020 12:54:43 +0100 Subject: [PATCH] tests for ClientScene.PrepareToSpawnSceneObjects (#1846) * tests for ClientScene.PrepareToSpawnSceneObjects * removing line --- .../Tests/Editor/ClientSceneTestsBase.cs | 2 +- ...ntSceneTests_PrepareToSpawnSceneObjects.cs | 146 ++++++++++++++++++ ...neTests_PrepareToSpawnSceneObjects.cs.meta | 11 ++ 3 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 Assets/Mirror/Tests/Editor/ClientSceneTests_PrepareToSpawnSceneObjects.cs create mode 100644 Assets/Mirror/Tests/Editor/ClientSceneTests_PrepareToSpawnSceneObjects.cs.meta diff --git a/Assets/Mirror/Tests/Editor/ClientSceneTestsBase.cs b/Assets/Mirror/Tests/Editor/ClientSceneTestsBase.cs index 4d6641922..f1286aa5d 100644 --- a/Assets/Mirror/Tests/Editor/ClientSceneTestsBase.cs +++ b/Assets/Mirror/Tests/Editor/ClientSceneTestsBase.cs @@ -46,7 +46,7 @@ public void OneTimeSetUp() } [TearDown] - public void TearDown() + public virtual void TearDown() { ClientScene.Shutdown(); // reset asset id incase they are changed by tests diff --git a/Assets/Mirror/Tests/Editor/ClientSceneTests_PrepareToSpawnSceneObjects.cs b/Assets/Mirror/Tests/Editor/ClientSceneTests_PrepareToSpawnSceneObjects.cs new file mode 100644 index 000000000..d7681a161 --- /dev/null +++ b/Assets/Mirror/Tests/Editor/ClientSceneTests_PrepareToSpawnSceneObjects.cs @@ -0,0 +1,146 @@ +using System.Collections.Generic; +using NUnit.Framework; +using UnityEngine; + +namespace Mirror.Tests +{ + public class ClientSceneTests_PrepareToSpawnSceneObjects : ClientSceneTestsBase + { + readonly List createdObjects = new List(); + + protected Dictionary spawnableObjects => ClientScene.spawnableObjects; + + + [TearDown] + public override void TearDown() + { + base.TearDown(); + foreach (NetworkIdentity item in createdObjects) + { + Object.DestroyImmediate(item.gameObject); + } + createdObjects.Clear(); + } + + NetworkIdentity CreateSceneObject(ulong sceneId) + { + GameObject runtimeObject = new GameObject("Runtime GameObject"); + NetworkIdentity networkIdentity = runtimeObject.AddComponent(); + // set sceneId to zero as it is set in onvalidate (does not set id at runtime) + networkIdentity.sceneId = sceneId; + + createdObjects.Add(networkIdentity); + + return networkIdentity; + } + + + [Test] + public void AddsAllInactiveIdentitiesInSceneWithSceneIdToDictionary() + { + NetworkIdentity obj1 = CreateSceneObject(10); + NetworkIdentity obj2 = CreateSceneObject(11); + NetworkIdentity obj3 = CreateSceneObject(12); + NetworkIdentity obj4 = CreateSceneObject(13); + + obj1.gameObject.SetActive(false); + obj2.gameObject.SetActive(false); + obj3.gameObject.SetActive(false); + obj4.gameObject.SetActive(false); + + ClientScene.PrepareToSpawnSceneObjects(); + + Assert.That(spawnableObjects, Has.Count.EqualTo(4)); + + Assert.IsTrue(spawnableObjects.ContainsValue(obj1)); + Assert.IsTrue(spawnableObjects.ContainsValue(obj2)); + Assert.IsTrue(spawnableObjects.ContainsValue(obj3)); + Assert.IsTrue(spawnableObjects.ContainsValue(obj4)); + } + + [Test] + public void DoesNotAddActiveObjectsToDictionary() + { + NetworkIdentity active1 = CreateSceneObject(30); + NetworkIdentity active2 = CreateSceneObject(31); + NetworkIdentity inactive1 = CreateSceneObject(32); + NetworkIdentity inactive2 = CreateSceneObject(33); + + active1.gameObject.SetActive(true); + active2.gameObject.SetActive(true); + inactive1.gameObject.SetActive(false); + inactive2.gameObject.SetActive(false); + + ClientScene.PrepareToSpawnSceneObjects(); + + Assert.That(spawnableObjects, Has.Count.EqualTo(2)); + + Assert.IsTrue(spawnableObjects.ContainsValue(inactive1)); + Assert.IsTrue(spawnableObjects.ContainsValue(inactive2)); + + Assert.IsFalse(spawnableObjects.ContainsValue(active1)); + Assert.IsFalse(spawnableObjects.ContainsValue(active2)); + } + + [Test] + public void DoesNotAddObjectsWithNoSceneId() + { + NetworkIdentity noId1 = CreateSceneObject(0); + NetworkIdentity noId2 = CreateSceneObject(0); + NetworkIdentity hasId1 = CreateSceneObject(40); + NetworkIdentity hasId2 = CreateSceneObject(41); + + noId1.gameObject.SetActive(false); + noId2.gameObject.SetActive(false); + hasId1.gameObject.SetActive(false); + hasId2.gameObject.SetActive(false); + + ClientScene.PrepareToSpawnSceneObjects(); + + Assert.IsTrue(spawnableObjects.ContainsValue(hasId1)); + Assert.IsTrue(spawnableObjects.ContainsValue(hasId2)); + + Assert.IsFalse(spawnableObjects.ContainsValue(noId1)); + Assert.IsFalse(spawnableObjects.ContainsValue(noId2)); + } + + [Test] + public void AddsIdentitiesToDictionaryUsingSceneId() + { + NetworkIdentity obj1 = CreateSceneObject(20); + NetworkIdentity obj2 = CreateSceneObject(21); + obj1.gameObject.SetActive(false); + obj2.gameObject.SetActive(false); + + ClientScene.PrepareToSpawnSceneObjects(); + + Assert.IsTrue(spawnableObjects.ContainsKey(20)); + Assert.That(spawnableObjects[20], Is.EqualTo(obj1)); + + Assert.IsTrue(spawnableObjects.ContainsKey(21)); + Assert.That(spawnableObjects[21], Is.EqualTo(obj2)); + } + + [Test] + public void ClearsExistingItemsFromDictionary() + { + // destroyed objects from old scene + spawnableObjects.Add(60, null); + spawnableObjects.Add(62, null); + + // active object + NetworkIdentity obj1 = CreateSceneObject(61); + spawnableObjects.Add(61, obj1); + + // new disabled object + NetworkIdentity obj2 = CreateSceneObject(63); + obj2.gameObject.SetActive(false); + + ClientScene.PrepareToSpawnSceneObjects(); + + Assert.That(spawnableObjects, Has.Count.EqualTo(1)); + Assert.IsFalse(spawnableObjects.ContainsValue(null)); + Assert.IsTrue(spawnableObjects.ContainsValue(obj2)); + } + } +} diff --git a/Assets/Mirror/Tests/Editor/ClientSceneTests_PrepareToSpawnSceneObjects.cs.meta b/Assets/Mirror/Tests/Editor/ClientSceneTests_PrepareToSpawnSceneObjects.cs.meta new file mode 100644 index 000000000..9652845b1 --- /dev/null +++ b/Assets/Mirror/Tests/Editor/ClientSceneTests_PrepareToSpawnSceneObjects.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 546931b1b1c38d5498f2c189e68b63aa +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: