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
{
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]
public override IEnumerator UnitySetUp()
{
base.SetUp();
yield return base.UnitySetUp();
networkManagerGo = transport.gameObject;
manager = networkManagerGo.AddComponent<NetworkManager>();
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();
NetworkServer.Listen(1);
ConnectHostClientBlockingAuthenticatedAndReady();
}
[UnityTest]
public IEnumerator LocalPlayerIsSetToNullAfterNetworkDestroy()
{
const uint netId = 1000;
CreateNetworked(out GameObject go, out NetworkIdentity identity);
// need spawned local player
CreateNetworkedAndSpawnPlayer(out GameObject go, out NetworkIdentity identity, NetworkServer.localConnection);
SpawnMessage msg = new SpawnMessage
{
netId = netId,
isLocalPlayer = true,
isOwner = true,
};
NetworkIdentity.spawned[msg.netId] = identity;
NetworkClient.OnHostClientSpawn(msg);
// need to have localPlayer set for this test
Assert.That(NetworkClient.localPlayer, !Is.Null);
// unspawn, wait one frame for OnDestroy to be called
NetworkServer.Destroy(identity.gameObject);
// wait a frame for destroy to happen
yield return null;
// use "is null" here to avoid unity == check
@ -93,22 +36,14 @@ public IEnumerator LocalPlayerIsSetToNullAfterNetworkDestroy()
[UnityTest]
public IEnumerator LocalPlayerIsSetToNullAfterNetworkUnspawn()
{
const uint netId = 1000;
CreateNetworked(out GameObject go, out NetworkIdentity identity);
// need spawned local player
CreateNetworkedAndSpawnPlayer(out GameObject go, out NetworkIdentity identity, NetworkServer.localConnection);
SpawnMessage msg = new SpawnMessage
{
netId = netId,
isLocalPlayer = true,
isOwner = true,
};
NetworkIdentity.spawned[msg.netId] = identity;
NetworkClient.OnHostClientSpawn(msg);
// need to have localPlayer set for this test
Assert.That(NetworkClient.localPlayer, !Is.Null);
// unspawn, wait one frame for OnDestroy to be called
NetworkServer.UnSpawn(identity.gameObject);
// wait a frame for destroy to happen
yield return null;
// use "is null" here to avoid unity == check