ClientSceneTest_LocalPlayer_AsHost: huge cleanup

This commit is contained in:
vis2k 2021-08-10 19:32:17 +08:00
parent 4925f1397a
commit 494484ef78

View File

@ -7,83 +7,26 @@ namespace Mirror.Tests.Runtime
{ {
public class ClientSceneTest_LocalPlayer_AsHost : MirrorPlayModeTest public class ClientSceneTest_LocalPlayer_AsHost : MirrorPlayModeTest
{ {
protected GameObject networkManagerGo;
protected NetworkManager manager;
protected GameObject playerGO;
protected NetworkIdentity identity;
protected virtual bool AutoAddPlayer => true;
protected virtual void afterStartHost() {}
protected virtual void beforeStopHost() {}
[UnitySetUp] [UnitySetUp]
public override IEnumerator UnitySetUp() public override IEnumerator UnitySetUp()
{ {
base.SetUp(); yield return base.UnitySetUp();
networkManagerGo = transport.gameObject; NetworkServer.Listen(1);
manager = networkManagerGo.AddComponent<NetworkManager>(); ConnectHostClientBlockingAuthenticatedAndReady();
Transport.activeTransport = transport;
// create a tracked prefab (not spawned)
CreateGameObject(out playerGO);
identity = playerGO.AddComponent<NetworkIdentity>();
identity.assetId = System.Guid.NewGuid();
manager.playerPrefab = playerGO;
manager.autoStartServerBuild = false;
manager.autoCreatePlayer = AutoAddPlayer;
if (Application.isBatchMode)
{
Application.targetFrameRate = 60;
}
yield return null;
manager.StartHost();
yield return null;
afterStartHost();
}
[UnityTearDown]
public override IEnumerator UnityTearDown()
{
beforeStopHost();
yield return null;
// needed for stophost
Transport.activeTransport = transport;
manager.StopHost();
base.TearDown();
} }
[UnityTest] [UnityTest]
public IEnumerator LocalPlayerIsSetToNullAfterNetworkDestroy() public IEnumerator LocalPlayerIsSetToNullAfterNetworkDestroy()
{ {
const uint netId = 1000; // need spawned local player
CreateNetworked(out GameObject go, out NetworkIdentity identity); CreateNetworkedAndSpawnPlayer(out GameObject go, out NetworkIdentity identity, NetworkServer.localConnection);
SpawnMessage msg = new SpawnMessage // need to have localPlayer set for this test
{ Assert.That(NetworkClient.localPlayer, !Is.Null);
netId = netId,
isLocalPlayer = true,
isOwner = true,
};
NetworkIdentity.spawned[msg.netId] = identity;
NetworkClient.OnHostClientSpawn(msg);
// unspawn, wait one frame for OnDestroy to be called
NetworkServer.Destroy(identity.gameObject); NetworkServer.Destroy(identity.gameObject);
// wait a frame for destroy to happen
yield return null; yield return null;
// use "is null" here to avoid unity == check // use "is null" here to avoid unity == check
@ -93,22 +36,14 @@ public IEnumerator LocalPlayerIsSetToNullAfterNetworkDestroy()
[UnityTest] [UnityTest]
public IEnumerator LocalPlayerIsSetToNullAfterNetworkUnspawn() public IEnumerator LocalPlayerIsSetToNullAfterNetworkUnspawn()
{ {
const uint netId = 1000; // need spawned local player
CreateNetworked(out GameObject go, out NetworkIdentity identity); CreateNetworkedAndSpawnPlayer(out GameObject go, out NetworkIdentity identity, NetworkServer.localConnection);
SpawnMessage msg = new SpawnMessage // need to have localPlayer set for this test
{ Assert.That(NetworkClient.localPlayer, !Is.Null);
netId = netId,
isLocalPlayer = true,
isOwner = true,
};
NetworkIdentity.spawned[msg.netId] = identity;
NetworkClient.OnHostClientSpawn(msg);
// unspawn, wait one frame for OnDestroy to be called
NetworkServer.UnSpawn(identity.gameObject); NetworkServer.UnSpawn(identity.gameObject);
// wait a frame for destroy to happen
yield return null; yield return null;
// use "is null" here to avoid unity == check // use "is null" here to avoid unity == check