Repository navigation
Expand file tree
/
Copy pathUnifiedNetworkTransformTest.cs
More file actions
123 lines (104 loc) · 4.87 KB
/
Copy pathUnifiedNetworkTransformTest.cs
File metadata and controls
123 lines (104 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#if UNIFIED_NETCODE
using System.Collections;
using System.Text;
using NUnit.Framework;
using Unity.Netcode.Components;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine;
using UnityEngine.TestTools;
namespace Unity.Netcode.RuntimeTests
{
/// <summary>
/// Test class that deliberately removes some functionality from NetworkTransform that is conditionally disabled
/// by the presence of ghost objects in the base class. This is to help be certain that the network transform
/// is not doing the work, but that the work is being done by N4E's snapshots.
/// </summary>
internal class DoNothingNetworkTransform : NetworkTransform
{
public override void OnNetworkSpawn()
{
// Deliberately left empty
}
internal override void InternalInitialization(bool isOwnershipChange = false)
{
// Deliberately left empty
}
}
[TestFixture(HostOrServer.UnifiedHost)]
internal class UnifiedNetworkTransformTest : IntegrationTestWithApproximation
{
protected override int NumberOfClients => 2;
private GameObject m_Prefab;
private NetworkObject m_Instance;
public UnifiedNetworkTransformTest(HostOrServer hostOrServer) : base(hostOrServer)
{
}
// This is the validation test for the unified API, so it always runs during a unified test pass.
protected override bool UseUnifiedTests()
{
return true;
}
protected override bool OnSetVerboseDebug()
{
return false;
}
protected override IEnumerator OnSetup()
{
// Creates the hybrid prefab
m_Prefab = CreateNetworkObjectPrefab("HybridPrefab");
m_Prefab.AddComponent<DoNothingNetworkTransform>();
return base.OnSetup();
}
private bool ValidatePosition(StringBuilder errorLog)
{
var authorityPosition = m_AuthorityInstanceTransform.position;
foreach (var client in m_ClientNetworkManagers)
{
var instancePosition = client.SpawnManager.SpawnedObjects[m_Instance.NetworkObjectId].transform.position;
if (!Approximately(authorityPosition, instancePosition))
{
errorLog.AppendLine($"[Client-{client.LocalClientId}] Clone instance position {GetVector3Values(instancePosition)} " +
$"does not equal the authority's {GetVector3Values(authorityPosition)}");
}
}
return errorLog.Length == 0;
}
private Transform m_AuthorityInstanceTransform;
[UnityTest]
public IEnumerator BasicMovementTest()
{
var authority = GetAuthorityNetworkManager();
m_Instance = SpawnObject(m_Prefab, m_ServerNetworkManager).GetComponent<NetworkObject>();
m_AuthorityInstanceTransform = m_Instance.transform;
yield return WaitForSpawnedOnAllOrTimeOut(m_Instance);
AssertOnTimeout($"Failed to spawn {m_Instance.name} on all clients!");
VerboseDebug("All clients spawned instance!");
yield return WaitForConditionOrTimeOut(ValidatePosition);
AssertOnTimeout($"A client failed to synchronize the position on the initial spawn!");
var originalPos = authority.LocalClient.PlayerObject.transform.position;
var newPos = originalPos + new Vector3(1, 1, 1);
m_Instance.transform.position = newPos;
yield return WaitForConditionOrTimeOut(ValidatePosition);
AssertOnTimeout($"A client failed to synchronize the changed position: {GetVector3Values(newPos)}!");
VerboseDebug("Test Passed!");
}
/// <summary>
/// Validates that each <see cref="NetworkManager"/>'s world only updates that <see cref="NetworkManager"/>'s connections.
/// </summary>
[Test]
public void EachWorldUpdatesConnectionsForItsOwnNetworkManager()
{
foreach (var networkManager in m_NetworkManagers)
{
var world = networkManager.NetcodeWorld;
Assert.IsNotNull(world, $"[Client-{networkManager.LocalClientId}] Has no world!");
Assert.IsNotNull(world.GetExistingSystemManaged<UnifiedUpdateConnections>(), $"[{world.Name}] Has no {nameof(UnifiedUpdateConnections)} system!");
using var query = world.EntityManager.CreateEntityQuery(typeof(HybridNetworkManager));
Assert.AreEqual(1, query.CalculateEntityCount(), $"[{world.Name}] Does not have exactly one {nameof(HybridNetworkManager)}!");
var bound = query.GetSingleton<HybridNetworkManager>().NetworkManager.Value;
Assert.AreSame(networkManager, bound, $"[{world.Name}] Is bound to another {nameof(NetworkManager)}!");
}
}
}
}
#endif