NetworkIdentityTests: AddObserver

This commit is contained in:
vis2k 2020-03-01 12:02:25 +01:00
parent 8cd50cf9f2
commit 508e253bb1

View File

@ -938,6 +938,50 @@ public void OnNetworkDestroy()
GameObject.DestroyImmediate(gameObject); GameObject.DestroyImmediate(gameObject);
} }
[Test]
public void AddObserver()
{
// create a networkidentity
GameObject gameObject = new GameObject();
NetworkIdentity identity = gameObject.AddComponent<NetworkIdentity>();
// 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] [Test]
public void ClearObservers() public void ClearObservers()
{ {