From 508e253bb1d0590d36ca356abf2d12651d95795a Mon Sep 17 00:00:00 2001 From: vis2k Date: Sun, 1 Mar 2020 12:02:25 +0100 Subject: [PATCH] NetworkIdentityTests: AddObserver --- .../Tests/Editor/NetworkIdentityTests.cs | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/Assets/Mirror/Tests/Editor/NetworkIdentityTests.cs b/Assets/Mirror/Tests/Editor/NetworkIdentityTests.cs index e358b9fdd..d53dc47c9 100644 --- a/Assets/Mirror/Tests/Editor/NetworkIdentityTests.cs +++ b/Assets/Mirror/Tests/Editor/NetworkIdentityTests.cs @@ -938,6 +938,50 @@ public void OnNetworkDestroy() GameObject.DestroyImmediate(gameObject); } + [Test] + public void AddObserver() + { + // create a networkidentity + GameObject gameObject = new GameObject(); + NetworkIdentity identity = gameObject.AddComponent(); + + // create some connections + NetworkConnectionToClient connection1 = new NetworkConnectionToClient(42); + NetworkConnectionToClient connection2 = new NetworkConnectionToClient(43); + + // AddObserver should return early if called before .observers was + // created + Assert.That(identity.observers, Is.Null); + LogAssert.ignoreFailingMessages = true; // error log is expected + identity.AddObserver(connection1); + LogAssert.ignoreFailingMessages = false; + Assert.That(identity.observers, Is.Null); + + // call OnStartServer so that observers dict is created + identity.OnStartServer(); + + // call AddObservers + identity.AddObserver(connection1); + identity.AddObserver(connection2); + Assert.That(identity.observers.Count, Is.EqualTo(2)); + Assert.That(identity.observers.ContainsKey(connection1.connectionId)); + Assert.That(identity.observers[connection1.connectionId], Is.EqualTo(connection1)); + Assert.That(identity.observers.ContainsKey(connection2.connectionId)); + Assert.That(identity.observers[connection2.connectionId], Is.EqualTo(connection2)); + + // adding a duplicate connectionId shouldn't overwrite the original + NetworkConnectionToClient duplicate = new NetworkConnectionToClient(connection1.connectionId); + identity.AddObserver(duplicate); + Assert.That(identity.observers.Count, Is.EqualTo(2)); + Assert.That(identity.observers.ContainsKey(connection1.connectionId)); + Assert.That(identity.observers[connection1.connectionId], Is.EqualTo(connection1)); + Assert.That(identity.observers.ContainsKey(connection2.connectionId)); + Assert.That(identity.observers[connection2.connectionId], Is.EqualTo(connection2)); + + // clean up + GameObject.DestroyImmediate(gameObject); + } + [Test] public void ClearObservers() {