diff --git a/Assets/Mirror/Tests/Editor/ClientSceneTestsBase.cs b/Assets/Mirror/Tests/Editor/ClientSceneTestsBase.cs new file mode 100644 index 000000000..4d6641922 --- /dev/null +++ b/Assets/Mirror/Tests/Editor/ClientSceneTestsBase.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using NUnit.Framework; +using UnityEditor; +using UnityEngine; + +namespace Mirror.Tests +{ + [TestFixture] + public abstract class ClientSceneTestsBase + { + protected const string NewAssetIdIgnoreMessage = "Ignoring this test till we know how to fix it, see https://github.com/vis2k/Mirror/issues/1831"; + + // use guid to find asset so that the path does not matter + protected const string ValidPrefabAssetGuid = "33169286da0313d45ab5bfccc6cf3775"; + protected const string PrefabWithChildrenAssetGuid = "a78e009e3f2dee44e8859516974ede43"; + protected const string InvalidPrefabAssetGuid = "78f0a3f755d35324e959f3ecdd993fb0"; + // random guid, not used anywhere + protected const string AnotherGuidString = "5794128cdfda04542985151f82990d05"; + + protected GameObject validPrefab; + protected NetworkIdentity validPrefabNetId; + protected GameObject prefabWithChildren; + protected GameObject invalidPrefab; + protected Guid validPrefabGuid; + protected Guid anotherGuid; + + protected Dictionary prefabs => ClientScene.prefabs; + protected Dictionary spawnHandlers => ClientScene.spawnHandlers; + protected Dictionary unspawnHandlers => ClientScene.unspawnHandlers; + + static GameObject LoadPrefab(string guid) + { + return AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid)); + } + + [OneTimeSetUp] + public void OneTimeSetUp() + { + validPrefab = LoadPrefab(ValidPrefabAssetGuid); + validPrefabNetId = validPrefab.GetComponent(); + prefabWithChildren = LoadPrefab(PrefabWithChildrenAssetGuid); + invalidPrefab = LoadPrefab(InvalidPrefabAssetGuid); + validPrefabGuid = new Guid(ValidPrefabAssetGuid); + anotherGuid = new Guid(AnotherGuidString); + } + + [TearDown] + public void TearDown() + { + ClientScene.Shutdown(); + // reset asset id incase they are changed by tests + validPrefabNetId.assetId = validPrefabGuid; + } + + [OneTimeTearDown] + public void OneTimeTearDown() + { + validPrefab = null; + prefabWithChildren = null; + invalidPrefab = null; + } + } +} diff --git a/Assets/Mirror/Tests/Editor/ClientSceneTests.cs.meta b/Assets/Mirror/Tests/Editor/ClientSceneTestsBase.cs.meta similarity index 100% rename from Assets/Mirror/Tests/Editor/ClientSceneTests.cs.meta rename to Assets/Mirror/Tests/Editor/ClientSceneTestsBase.cs.meta diff --git a/Assets/Mirror/Tests/Editor/ClientSceneTests_ClearSpawners.cs b/Assets/Mirror/Tests/Editor/ClientSceneTests_ClearSpawners.cs new file mode 100644 index 000000000..226891228 --- /dev/null +++ b/Assets/Mirror/Tests/Editor/ClientSceneTests_ClearSpawners.cs @@ -0,0 +1,66 @@ +using System; +using NUnit.Framework; + +namespace Mirror.Tests +{ + public class ClientSceneTests_ClearSpawners : ClientSceneTestsBase + { + [Test] + public void RemovesAllPrefabsFromDictionary() + { + prefabs.Add(Guid.NewGuid(), null); + prefabs.Add(Guid.NewGuid(), null); + prefabs.Add(Guid.NewGuid(), null); + + ClientScene.ClearSpawners(); + + Assert.IsEmpty(prefabs); + } + + [Test] + public void RemovesAllSpawnHandlersFromDictionary() + { + spawnHandlers.Add(Guid.NewGuid(), null); + spawnHandlers.Add(Guid.NewGuid(), null); + spawnHandlers.Add(Guid.NewGuid(), null); + + ClientScene.ClearSpawners(); + + Assert.IsEmpty(spawnHandlers); + } + + [Test] + public void RemovesAllUnspawnHandlersFromDictionary() + { + unspawnHandlers.Add(Guid.NewGuid(), null); + unspawnHandlers.Add(Guid.NewGuid(), null); + unspawnHandlers.Add(Guid.NewGuid(), null); + + ClientScene.ClearSpawners(); + + Assert.IsEmpty(unspawnHandlers); + } + + [Test] + public void ClearsAllDictionary() + { + prefabs.Add(Guid.NewGuid(), null); + prefabs.Add(Guid.NewGuid(), null); + prefabs.Add(Guid.NewGuid(), null); + + spawnHandlers.Add(Guid.NewGuid(), null); + spawnHandlers.Add(Guid.NewGuid(), null); + spawnHandlers.Add(Guid.NewGuid(), null); + + unspawnHandlers.Add(Guid.NewGuid(), null); + unspawnHandlers.Add(Guid.NewGuid(), null); + unspawnHandlers.Add(Guid.NewGuid(), null); + + ClientScene.ClearSpawners(); + + Assert.IsEmpty(prefabs); + Assert.IsEmpty(spawnHandlers); + Assert.IsEmpty(unspawnHandlers); + } + } +} diff --git a/Assets/Mirror/Tests/Editor/ClientSceneTests_ClearSpawners.cs.meta b/Assets/Mirror/Tests/Editor/ClientSceneTests_ClearSpawners.cs.meta new file mode 100644 index 000000000..3a955eb07 --- /dev/null +++ b/Assets/Mirror/Tests/Editor/ClientSceneTests_ClearSpawners.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ad2fe5633a9652045a5f7eb2643b6956 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Mirror/Tests/Editor/ClientSceneTests_GetPrefab.cs b/Assets/Mirror/Tests/Editor/ClientSceneTests_GetPrefab.cs new file mode 100644 index 000000000..38d2f8757 --- /dev/null +++ b/Assets/Mirror/Tests/Editor/ClientSceneTests_GetPrefab.cs @@ -0,0 +1,62 @@ +using System; +using NUnit.Framework; +using UnityEngine; + +namespace Mirror.Tests +{ + public class ClientSceneTests_GetPrefab : ClientSceneTestsBase + { + [Test] + public void ReturnsFalseForEmptyGuid() + { + bool result = ClientScene.GetPrefab(new Guid(), out GameObject prefab); + + Assert.IsFalse(result); + Assert.IsNull(prefab); + } + + [Test] + public void ReturnsFalseForPrefabNotFound() + { + Guid guid = Guid.NewGuid(); + bool result = ClientScene.GetPrefab(guid, out GameObject prefab); + + Assert.IsFalse(result); + Assert.IsNull(prefab); + } + + [Test] + public void ReturnsFalseForPrefabIsNull() + { + Guid guid = Guid.NewGuid(); + prefabs.Add(guid, null); + bool result = ClientScene.GetPrefab(guid, out GameObject prefab); + + Assert.IsFalse(result); + Assert.IsNull(prefab); + } + + [Test] + public void ReturnsTrueWhenPrefabIsFound() + { + prefabs.Add(validPrefabGuid, validPrefab); + bool result = ClientScene.GetPrefab(validPrefabGuid, out GameObject prefab); + + Assert.IsTrue(result); + Assert.NotNull(prefab); + } + + [Test] + public void HasOutPrefabWithCorrectGuid() + { + prefabs.Add(validPrefabGuid, validPrefab); + ClientScene.GetPrefab(validPrefabGuid, out GameObject prefab); + + + Assert.NotNull(prefab); + + NetworkIdentity networkID = prefab.GetComponent(); + Assert.AreEqual(networkID.assetId, validPrefabGuid); + } + } +} diff --git a/Assets/Mirror/Tests/Editor/ClientSceneTests_GetPrefab.cs.meta b/Assets/Mirror/Tests/Editor/ClientSceneTests_GetPrefab.cs.meta new file mode 100644 index 000000000..2dde78154 --- /dev/null +++ b/Assets/Mirror/Tests/Editor/ClientSceneTests_GetPrefab.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 27cbff3906928974bb20c3f50eacbbb0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Mirror/Tests/Editor/ClientSceneTests.cs b/Assets/Mirror/Tests/Editor/ClientSceneTests_RegisterPrefab.cs similarity index 59% rename from Assets/Mirror/Tests/Editor/ClientSceneTests.cs rename to Assets/Mirror/Tests/Editor/ClientSceneTests_RegisterPrefab.cs index 2c30556b8..6f97067b8 100644 --- a/Assets/Mirror/Tests/Editor/ClientSceneTests.cs +++ b/Assets/Mirror/Tests/Editor/ClientSceneTests_RegisterPrefab.cs @@ -1,123 +1,11 @@ -using System; -using System.Collections.Generic; +using System; using NUnit.Framework; -using UnityEditor; using UnityEngine; using UnityEngine.TestTools; namespace Mirror.Tests { - [TestFixture] - public abstract class BaseClientSceneTests - { - protected const string NewAssetIdIgnoreMessage = "Ignoring this test till we know how to fix it, see https://github.com/vis2k/Mirror/issues/1831"; - - // use guid to find asset so that the path does not matter - protected const string ValidPrefabAssetGuid = "33169286da0313d45ab5bfccc6cf3775"; - protected const string PrefabWithChildrenAssetGuid = "a78e009e3f2dee44e8859516974ede43"; - protected const string InvalidPrefabAssetGuid = "78f0a3f755d35324e959f3ecdd993fb0"; - // random guid, not used anywhere - protected const string AnotherGuidString = "5794128cdfda04542985151f82990d05"; - - protected GameObject validPrefab; - protected NetworkIdentity validPrefabNetId; - protected GameObject prefabWithChildren; - protected GameObject invalidPrefab; - protected Guid validPrefabGuid; - protected Guid anotherGuid; - - protected Dictionary prefabs => ClientScene.prefabs; - protected Dictionary spawnHandlers => ClientScene.spawnHandlers; - protected Dictionary unspawnHandlers => ClientScene.unspawnHandlers; - - static GameObject LoadPrefab(string guid) - { - return AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid)); - } - - [OneTimeSetUp] - public void OneTimeSetUp() - { - validPrefab = LoadPrefab(ValidPrefabAssetGuid); - validPrefabNetId = validPrefab.GetComponent(); - prefabWithChildren = LoadPrefab(PrefabWithChildrenAssetGuid); - invalidPrefab = LoadPrefab(InvalidPrefabAssetGuid); - validPrefabGuid = new Guid(ValidPrefabAssetGuid); - anotherGuid = new Guid(AnotherGuidString); - } - - [TearDown] - public void TearDown() - { - ClientScene.Shutdown(); - // reset asset id incase they are changed by tests - validPrefabNetId.assetId = validPrefabGuid; - } - - [OneTimeTearDown] - public void OneTimeTearDown() - { - validPrefab = null; - prefabWithChildren = null; - invalidPrefab = null; - } - } - public class ClientSceneTests_GetPrefab : BaseClientSceneTests - { - [Test] - public void ReturnsFalseForEmptyGuid() - { - bool result = ClientScene.GetPrefab(new Guid(), out GameObject prefab); - - Assert.IsFalse(result); - Assert.IsNull(prefab); - } - - [Test] - public void ReturnsFalseForPrefabNotFound() - { - Guid guid = Guid.NewGuid(); - bool result = ClientScene.GetPrefab(guid, out GameObject prefab); - - Assert.IsFalse(result); - Assert.IsNull(prefab); - } - - [Test] - public void ReturnsFalseForPrefabIsNull() - { - Guid guid = Guid.NewGuid(); - prefabs.Add(guid, null); - bool result = ClientScene.GetPrefab(guid, out GameObject prefab); - - Assert.IsFalse(result); - Assert.IsNull(prefab); - } - - [Test] - public void ReturnsTrueWhenPrefabIsFound() - { - prefabs.Add(validPrefabGuid, validPrefab); - bool result = ClientScene.GetPrefab(validPrefabGuid, out GameObject prefab); - - Assert.IsTrue(result); - Assert.NotNull(prefab); - } - - [Test] - public void HasOutPrefabWithCorrectGuid() - { - prefabs.Add(validPrefabGuid, validPrefab); - ClientScene.GetPrefab(validPrefabGuid, out GameObject prefab); - - - Assert.NotNull(prefab); - - NetworkIdentity networkID = prefab.GetComponent(); - Assert.AreEqual(networkID.assetId, validPrefabGuid); - } - } - public class ClientSceneTests_RegisterPrefab : BaseClientSceneTests + public class ClientSceneTests_RegisterPrefab : ClientSceneTestsBase { [Test] [TestCase(RegisterPrefabOverload.Prefab, false)] @@ -663,361 +551,4 @@ public void Handler_ErrorWhenUnSpawnHandlerIsNull(RegisterPrefabOverload overloa } } - public class ClientSceneTests_UnregisterPrefab : BaseClientSceneTests - { - [Test] - public void RemovesPrefabFromDictionary() - { - prefabs.Add(validPrefabGuid, validPrefab); - - ClientScene.UnregisterPrefab(validPrefab); - - Assert.IsFalse(prefabs.ContainsKey(validPrefabGuid)); - } - - [Test] - public void RemovesSpawnHandlerFromDictionary() - { - spawnHandlers.Add(validPrefabGuid, new SpawnHandlerDelegate(x => null)); - - ClientScene.UnregisterPrefab(validPrefab); - - Assert.IsFalse(spawnHandlers.ContainsKey(validPrefabGuid)); - } - - [Test] - public void RemovesUnSpawnHandlerFromDictionary() - { - unspawnHandlers.Add(validPrefabGuid, new UnSpawnDelegate(x => { })); - - ClientScene.UnregisterPrefab(validPrefab); - - Assert.IsFalse(unspawnHandlers.ContainsKey(validPrefabGuid)); - } - - [Test] - public void ErrorWhenPrefabIsNull() - { - LogAssert.Expect(LogType.Error, "Could not unregister prefab because it was null"); - ClientScene.UnregisterPrefab(null); - } - - [Test] - public void ErrorWhenPrefabHasNoNetworkIdentity() - { - LogAssert.Expect(LogType.Error, $"Could not unregister '{invalidPrefab.name}' since it contains no NetworkIdentity component"); - ClientScene.UnregisterPrefab(invalidPrefab); - } - - } - public class ClientSceneTests_RegisterSpawnHandler : BaseClientSceneTests - { - [Test] - public void SpawnDelegate_AddsHandlerToSpawnHandlers() - { - int handlerCalled = 0; - - Guid guid = Guid.NewGuid(); - SpawnDelegate spawnHandler = new SpawnDelegate((pos, rot) => - { - handlerCalled++; - return null; - }); - UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); - - ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); - - Assert.IsTrue(spawnHandlers.ContainsKey(guid)); - - // check spawnHandler above is called - SpawnHandlerDelegate handler = spawnHandlers[guid]; - handler.Invoke(default); - Assert.That(handlerCalled, Is.EqualTo(1)); - } - - [Test] - public void SpawnDelegate_AddsHandlerToSpawnHandlersWithCorrectArguments() - { - int handlerCalled = 0; - Vector3 somePosition = new Vector3(10, 20, 3); - - Guid guid = Guid.NewGuid(); - SpawnDelegate spawnHandler = new SpawnDelegate((pos, assetId) => - { - handlerCalled++; - Assert.That(pos, Is.EqualTo(somePosition)); - Assert.That(assetId, Is.EqualTo(guid)); - return null; - }); - UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); - - ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); - - Assert.IsTrue(spawnHandlers.ContainsKey(guid)); - - // check spawnHandler above is called - SpawnHandlerDelegate handler = spawnHandlers[guid]; - handler.Invoke(new SpawnMessage { position = somePosition, assetId = guid }); - Assert.That(handlerCalled, Is.EqualTo(1)); - } - - [Test] - public void SpawnDelegate_AddsHandlerToUnSpawnHandlers() - { - Guid guid = Guid.NewGuid(); - SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null); - UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); - - ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); - - Assert.IsTrue(unspawnHandlers.ContainsKey(guid)); - Assert.AreEqual(unspawnHandlers[guid], unspawnHandler); - } - - [Test] - public void SpawnDelegate_ErrorWhenSpawnHandlerIsNull() - { - Guid guid = Guid.NewGuid(); - SpawnDelegate spawnHandler = null; - UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); - - LogAssert.Expect(LogType.Error, $"Can not Register null SpawnHandler for {guid}"); - ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); - } - - [Test] - public void SpawnDelegate_ErrorWhenUnSpawnHandlerIsNull() - { - Guid guid = Guid.NewGuid(); - SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null); - UnSpawnDelegate unspawnHandler = null; - - LogAssert.Expect(LogType.Error, $"Can not Register null UnSpawnHandler for {guid}"); - ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); - } - - [Test] - public void SpawnDelegate_ErrorWhenAssetIdIsEmpty() - { - Guid guid = new Guid(); - SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null); - UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); - - LogAssert.Expect(LogType.Error, "Can not Register SpawnHandler for empty Guid"); - ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); - } - - [Test] - public void SpawnDelegate_WarningWhenHandlerForGuidAlreadyExistsInHandlerDictionary() - { - Guid guid = Guid.NewGuid(); - SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null); - UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); - - ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); - - SpawnDelegate spawnHandler2 = new SpawnDelegate((x, y) => new GameObject()); - UnSpawnDelegate unspawnHandler2 = new UnSpawnDelegate(x => UnityEngine.Object.Destroy(x)); - - LogAssert.Expect(LogType.Warning, $"Replacing existing spawnHandlers for {guid}"); - ClientScene.RegisterSpawnHandler(guid, spawnHandler2, unspawnHandler2); - } - - [Test] - public void SpawnDelegate_ErrorWhenHandlerForGuidAlreadyExistsInPrefabDictionary() - { - Guid guid = Guid.NewGuid(); - prefabs.Add(guid, validPrefab); - - SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null); - UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); - - LogAssert.Expect(LogType.Error, $"assetId '{guid}' is already used by prefab '{validPrefab.name}'"); - ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); - } - - - [Test] - public void SpawnHandlerDelegate_AddsHandlerToSpawnHandlers() - { - Guid guid = Guid.NewGuid(); - SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null); - UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); - - ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); - - Assert.IsTrue(spawnHandlers.ContainsKey(guid)); - Assert.AreEqual(spawnHandlers[guid], spawnHandler); - } - - [Test] - public void SpawnHandlerDelegate_AddsHandlerToUnSpawnHandlers() - { - Guid guid = Guid.NewGuid(); - SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null); - UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); - - ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); - - Assert.IsTrue(unspawnHandlers.ContainsKey(guid)); - Assert.AreEqual(unspawnHandlers[guid], unspawnHandler); - } - - [Test] - public void SpawnHandlerDelegate_ErrorWhenSpawnHandlerIsNull() - { - Guid guid = Guid.NewGuid(); - SpawnHandlerDelegate spawnHandler = null; - UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); - - LogAssert.Expect(LogType.Error, $"Can not Register null SpawnHandler for {guid}"); - ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); - } - - [Test] - public void SpawnHandlerDelegate_ErrorWhenUnSpawnHandlerIsNull() - { - Guid guid = Guid.NewGuid(); - SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null); - UnSpawnDelegate unspawnHandler = null; - - LogAssert.Expect(LogType.Error, $"Can not Register null UnSpawnHandler for {guid}"); - ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); - } - - [Test] - public void SpawnHandlerDelegate_ErrorWhenAssetIdIsEmpty() - { - Guid guid = new Guid(); - SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null); - UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); - - LogAssert.Expect(LogType.Error, "Can not Register SpawnHandler for empty Guid"); - ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); - } - - [Test] - public void SpawnHandlerDelegate_WarningWhenHandlerForGuidAlreadyExistsInHandlerDictionary() - { - Guid guid = Guid.NewGuid(); - SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null); - UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); - - ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); - - SpawnHandlerDelegate spawnHandler2 = new SpawnHandlerDelegate(x => new GameObject()); - UnSpawnDelegate unspawnHandler2 = new UnSpawnDelegate(x => UnityEngine.Object.Destroy(x)); - - LogAssert.Expect(LogType.Warning, $"Replacing existing spawnHandlers for {guid}"); - ClientScene.RegisterSpawnHandler(guid, spawnHandler2, unspawnHandler2); - } - - [Test] - public void SpawnHandlerDelegate_ErrorWhenHandlerForGuidAlreadyExistsInPrefabDictionary() - { - Guid guid = Guid.NewGuid(); - prefabs.Add(guid, validPrefab); - - SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => new GameObject()); - UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => UnityEngine.Object.Destroy(x)); - - LogAssert.Expect(LogType.Error, $"assetId '{guid}' is already used by prefab '{validPrefab.name}'"); - ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); - } - - } - public class ClientSceneTests_UnregisterSpawnHandler : BaseClientSceneTests - { - [Test] - public void RemovesSpawnHandlersFromDictionary() - { - spawnHandlers.Add(validPrefabGuid, new SpawnHandlerDelegate(x => null)); - - ClientScene.UnregisterSpawnHandler(validPrefabGuid); - - Assert.IsFalse(unspawnHandlers.ContainsKey(validPrefabGuid)); - } - - [Test] - public void RemovesUnSpawnHandlersFromDictionary() - { - unspawnHandlers.Add(validPrefabGuid, new UnSpawnDelegate(x => { })); - - ClientScene.UnregisterSpawnHandler(validPrefabGuid); - - Assert.IsFalse(unspawnHandlers.ContainsKey(validPrefabGuid)); - } - - [Test] - public void DoesNotRemovePrefabDictionary() - { - prefabs.Add(validPrefabGuid, validPrefab); - - ClientScene.UnregisterSpawnHandler(validPrefabGuid); - - // Should not be removed - Assert.IsTrue(prefabs.ContainsKey(validPrefabGuid)); - } - - } - public class ClientSceneTests_ClearSpawners : BaseClientSceneTests - { - [Test] - public void RemovesAllPrefabsFromDictionary() - { - prefabs.Add(Guid.NewGuid(), null); - prefabs.Add(Guid.NewGuid(), null); - prefabs.Add(Guid.NewGuid(), null); - - ClientScene.ClearSpawners(); - - Assert.IsEmpty(prefabs); - } - - [Test] - public void RemovesAllSpawnHandlersFromDictionary() - { - spawnHandlers.Add(Guid.NewGuid(), null); - spawnHandlers.Add(Guid.NewGuid(), null); - spawnHandlers.Add(Guid.NewGuid(), null); - - ClientScene.ClearSpawners(); - - Assert.IsEmpty(spawnHandlers); - } - - [Test] - public void RemovesAllUnspawnHandlersFromDictionary() - { - unspawnHandlers.Add(Guid.NewGuid(), null); - unspawnHandlers.Add(Guid.NewGuid(), null); - unspawnHandlers.Add(Guid.NewGuid(), null); - - ClientScene.ClearSpawners(); - - Assert.IsEmpty(unspawnHandlers); - } - - [Test] - public void ClearsAllDictionary() - { - prefabs.Add(Guid.NewGuid(), null); - prefabs.Add(Guid.NewGuid(), null); - prefabs.Add(Guid.NewGuid(), null); - - spawnHandlers.Add(Guid.NewGuid(), null); - spawnHandlers.Add(Guid.NewGuid(), null); - spawnHandlers.Add(Guid.NewGuid(), null); - - unspawnHandlers.Add(Guid.NewGuid(), null); - unspawnHandlers.Add(Guid.NewGuid(), null); - unspawnHandlers.Add(Guid.NewGuid(), null); - - ClientScene.ClearSpawners(); - - Assert.IsEmpty(prefabs); - Assert.IsEmpty(spawnHandlers); - Assert.IsEmpty(unspawnHandlers); - } - } } diff --git a/Assets/Mirror/Tests/Editor/ClientSceneTests_RegisterPrefab.cs.meta b/Assets/Mirror/Tests/Editor/ClientSceneTests_RegisterPrefab.cs.meta new file mode 100644 index 000000000..803aa0ab5 --- /dev/null +++ b/Assets/Mirror/Tests/Editor/ClientSceneTests_RegisterPrefab.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: bdfdcfafb85b3be418f2085e38663006 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Mirror/Tests/Editor/ClientSceneTests_RegisterSpawnHandler.cs b/Assets/Mirror/Tests/Editor/ClientSceneTests_RegisterSpawnHandler.cs new file mode 100644 index 000000000..53b7f9404 --- /dev/null +++ b/Assets/Mirror/Tests/Editor/ClientSceneTests_RegisterSpawnHandler.cs @@ -0,0 +1,224 @@ +using System; +using NUnit.Framework; +using UnityEngine; +using UnityEngine.TestTools; + +namespace Mirror.Tests +{ + public class ClientSceneTests_RegisterSpawnHandler : ClientSceneTestsBase + { + [Test] + public void SpawnDelegate_AddsHandlerToSpawnHandlers() + { + int handlerCalled = 0; + + Guid guid = Guid.NewGuid(); + SpawnDelegate spawnHandler = new SpawnDelegate((pos, rot) => + { + handlerCalled++; + return null; + }); + UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); + + ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); + + Assert.IsTrue(spawnHandlers.ContainsKey(guid)); + + // check spawnHandler above is called + SpawnHandlerDelegate handler = spawnHandlers[guid]; + handler.Invoke(default); + Assert.That(handlerCalled, Is.EqualTo(1)); + } + + [Test] + public void SpawnDelegate_AddsHandlerToSpawnHandlersWithCorrectArguments() + { + int handlerCalled = 0; + Vector3 somePosition = new Vector3(10, 20, 3); + + Guid guid = Guid.NewGuid(); + SpawnDelegate spawnHandler = new SpawnDelegate((pos, assetId) => + { + handlerCalled++; + Assert.That(pos, Is.EqualTo(somePosition)); + Assert.That(assetId, Is.EqualTo(guid)); + return null; + }); + UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); + + ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); + + Assert.IsTrue(spawnHandlers.ContainsKey(guid)); + + // check spawnHandler above is called + SpawnHandlerDelegate handler = spawnHandlers[guid]; + handler.Invoke(new SpawnMessage { position = somePosition, assetId = guid }); + Assert.That(handlerCalled, Is.EqualTo(1)); + } + + [Test] + public void SpawnDelegate_AddsHandlerToUnSpawnHandlers() + { + Guid guid = Guid.NewGuid(); + SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null); + UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); + + ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); + + Assert.IsTrue(unspawnHandlers.ContainsKey(guid)); + Assert.AreEqual(unspawnHandlers[guid], unspawnHandler); + } + + [Test] + public void SpawnDelegate_ErrorWhenSpawnHandlerIsNull() + { + Guid guid = Guid.NewGuid(); + SpawnDelegate spawnHandler = null; + UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); + + LogAssert.Expect(LogType.Error, $"Can not Register null SpawnHandler for {guid}"); + ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); + } + + [Test] + public void SpawnDelegate_ErrorWhenUnSpawnHandlerIsNull() + { + Guid guid = Guid.NewGuid(); + SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null); + UnSpawnDelegate unspawnHandler = null; + + LogAssert.Expect(LogType.Error, $"Can not Register null UnSpawnHandler for {guid}"); + ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); + } + + [Test] + public void SpawnDelegate_ErrorWhenAssetIdIsEmpty() + { + Guid guid = new Guid(); + SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null); + UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); + + LogAssert.Expect(LogType.Error, "Can not Register SpawnHandler for empty Guid"); + ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); + } + + [Test] + public void SpawnDelegate_WarningWhenHandlerForGuidAlreadyExistsInHandlerDictionary() + { + Guid guid = Guid.NewGuid(); + SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null); + UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); + + ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); + + SpawnDelegate spawnHandler2 = new SpawnDelegate((x, y) => new GameObject()); + UnSpawnDelegate unspawnHandler2 = new UnSpawnDelegate(x => UnityEngine.Object.Destroy(x)); + + LogAssert.Expect(LogType.Warning, $"Replacing existing spawnHandlers for {guid}"); + ClientScene.RegisterSpawnHandler(guid, spawnHandler2, unspawnHandler2); + } + + [Test] + public void SpawnDelegate_ErrorWhenHandlerForGuidAlreadyExistsInPrefabDictionary() + { + Guid guid = Guid.NewGuid(); + prefabs.Add(guid, validPrefab); + + SpawnDelegate spawnHandler = new SpawnDelegate((x, y) => null); + UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); + + LogAssert.Expect(LogType.Error, $"assetId '{guid}' is already used by prefab '{validPrefab.name}'"); + ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); + } + + + [Test] + public void SpawnHandlerDelegate_AddsHandlerToSpawnHandlers() + { + Guid guid = Guid.NewGuid(); + SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null); + UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); + + ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); + + Assert.IsTrue(spawnHandlers.ContainsKey(guid)); + Assert.AreEqual(spawnHandlers[guid], spawnHandler); + } + + [Test] + public void SpawnHandlerDelegate_AddsHandlerToUnSpawnHandlers() + { + Guid guid = Guid.NewGuid(); + SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null); + UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); + + ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); + + Assert.IsTrue(unspawnHandlers.ContainsKey(guid)); + Assert.AreEqual(unspawnHandlers[guid], unspawnHandler); + } + + [Test] + public void SpawnHandlerDelegate_ErrorWhenSpawnHandlerIsNull() + { + Guid guid = Guid.NewGuid(); + SpawnHandlerDelegate spawnHandler = null; + UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); + + LogAssert.Expect(LogType.Error, $"Can not Register null SpawnHandler for {guid}"); + ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); + } + + [Test] + public void SpawnHandlerDelegate_ErrorWhenUnSpawnHandlerIsNull() + { + Guid guid = Guid.NewGuid(); + SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null); + UnSpawnDelegate unspawnHandler = null; + + LogAssert.Expect(LogType.Error, $"Can not Register null UnSpawnHandler for {guid}"); + ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); + } + + [Test] + public void SpawnHandlerDelegate_ErrorWhenAssetIdIsEmpty() + { + Guid guid = new Guid(); + SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null); + UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); + + LogAssert.Expect(LogType.Error, "Can not Register SpawnHandler for empty Guid"); + ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); + } + + [Test] + public void SpawnHandlerDelegate_WarningWhenHandlerForGuidAlreadyExistsInHandlerDictionary() + { + Guid guid = Guid.NewGuid(); + SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => null); + UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => { }); + + ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); + + SpawnHandlerDelegate spawnHandler2 = new SpawnHandlerDelegate(x => new GameObject()); + UnSpawnDelegate unspawnHandler2 = new UnSpawnDelegate(x => UnityEngine.Object.Destroy(x)); + + LogAssert.Expect(LogType.Warning, $"Replacing existing spawnHandlers for {guid}"); + ClientScene.RegisterSpawnHandler(guid, spawnHandler2, unspawnHandler2); + } + + [Test] + public void SpawnHandlerDelegate_ErrorWhenHandlerForGuidAlreadyExistsInPrefabDictionary() + { + Guid guid = Guid.NewGuid(); + prefabs.Add(guid, validPrefab); + + SpawnHandlerDelegate spawnHandler = new SpawnHandlerDelegate(x => new GameObject()); + UnSpawnDelegate unspawnHandler = new UnSpawnDelegate(x => UnityEngine.Object.Destroy(x)); + + LogAssert.Expect(LogType.Error, $"assetId '{guid}' is already used by prefab '{validPrefab.name}'"); + ClientScene.RegisterSpawnHandler(guid, spawnHandler, unspawnHandler); + } + + } +} diff --git a/Assets/Mirror/Tests/Editor/ClientSceneTests_RegisterSpawnHandler.cs.meta b/Assets/Mirror/Tests/Editor/ClientSceneTests_RegisterSpawnHandler.cs.meta new file mode 100644 index 000000000..ec826b7c5 --- /dev/null +++ b/Assets/Mirror/Tests/Editor/ClientSceneTests_RegisterSpawnHandler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c7e6577e8c2e64e41ae255edc61e91a2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Mirror/Tests/Editor/ClientSceneTests_UnregisterPrefab.cs b/Assets/Mirror/Tests/Editor/ClientSceneTests_UnregisterPrefab.cs new file mode 100644 index 000000000..db501288b --- /dev/null +++ b/Assets/Mirror/Tests/Editor/ClientSceneTests_UnregisterPrefab.cs @@ -0,0 +1,54 @@ +using NUnit.Framework; +using UnityEngine; +using UnityEngine.TestTools; + +namespace Mirror.Tests +{ + public class ClientSceneTests_UnregisterPrefab : ClientSceneTestsBase + { + [Test] + public void RemovesPrefabFromDictionary() + { + prefabs.Add(validPrefabGuid, validPrefab); + + ClientScene.UnregisterPrefab(validPrefab); + + Assert.IsFalse(prefabs.ContainsKey(validPrefabGuid)); + } + + [Test] + public void RemovesSpawnHandlerFromDictionary() + { + spawnHandlers.Add(validPrefabGuid, new SpawnHandlerDelegate(x => null)); + + ClientScene.UnregisterPrefab(validPrefab); + + Assert.IsFalse(spawnHandlers.ContainsKey(validPrefabGuid)); + } + + [Test] + public void RemovesUnSpawnHandlerFromDictionary() + { + unspawnHandlers.Add(validPrefabGuid, new UnSpawnDelegate(x => { })); + + ClientScene.UnregisterPrefab(validPrefab); + + Assert.IsFalse(unspawnHandlers.ContainsKey(validPrefabGuid)); + } + + [Test] + public void ErrorWhenPrefabIsNull() + { + LogAssert.Expect(LogType.Error, "Could not unregister prefab because it was null"); + ClientScene.UnregisterPrefab(null); + } + + [Test] + public void ErrorWhenPrefabHasNoNetworkIdentity() + { + LogAssert.Expect(LogType.Error, $"Could not unregister '{invalidPrefab.name}' since it contains no NetworkIdentity component"); + ClientScene.UnregisterPrefab(invalidPrefab); + } + + } +} diff --git a/Assets/Mirror/Tests/Editor/ClientSceneTests_UnregisterPrefab.cs.meta b/Assets/Mirror/Tests/Editor/ClientSceneTests_UnregisterPrefab.cs.meta new file mode 100644 index 000000000..8478bffbc --- /dev/null +++ b/Assets/Mirror/Tests/Editor/ClientSceneTests_UnregisterPrefab.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: aa6fa692d80eaf8419e559c35034f016 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Mirror/Tests/Editor/ClientSceneTests_UnregisterSpawnHandler.cs b/Assets/Mirror/Tests/Editor/ClientSceneTests_UnregisterSpawnHandler.cs new file mode 100644 index 000000000..69b309abd --- /dev/null +++ b/Assets/Mirror/Tests/Editor/ClientSceneTests_UnregisterSpawnHandler.cs @@ -0,0 +1,39 @@ +using NUnit.Framework; + +namespace Mirror.Tests +{ + public class ClientSceneTests_UnregisterSpawnHandler : ClientSceneTestsBase + { + [Test] + public void RemovesSpawnHandlersFromDictionary() + { + spawnHandlers.Add(validPrefabGuid, new SpawnHandlerDelegate(x => null)); + + ClientScene.UnregisterSpawnHandler(validPrefabGuid); + + Assert.IsFalse(unspawnHandlers.ContainsKey(validPrefabGuid)); + } + + [Test] + public void RemovesUnSpawnHandlersFromDictionary() + { + unspawnHandlers.Add(validPrefabGuid, new UnSpawnDelegate(x => { })); + + ClientScene.UnregisterSpawnHandler(validPrefabGuid); + + Assert.IsFalse(unspawnHandlers.ContainsKey(validPrefabGuid)); + } + + [Test] + public void DoesNotRemovePrefabDictionary() + { + prefabs.Add(validPrefabGuid, validPrefab); + + ClientScene.UnregisterSpawnHandler(validPrefabGuid); + + // Should not be removed + Assert.IsTrue(prefabs.ContainsKey(validPrefabGuid)); + } + + } +} diff --git a/Assets/Mirror/Tests/Editor/ClientSceneTests_UnregisterSpawnHandler.cs.meta b/Assets/Mirror/Tests/Editor/ClientSceneTests_UnregisterSpawnHandler.cs.meta new file mode 100644 index 000000000..006dad00a --- /dev/null +++ b/Assets/Mirror/Tests/Editor/ClientSceneTests_UnregisterSpawnHandler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 49ef76b8883ba3845942503683c9a9b3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: