|
| 1 | +using System.Collections; |
| 2 | +using UnityEngine; |
| 3 | +using UnityEngine.TestTools; |
| 4 | +using Unity.Netcode.TestHelpers.Runtime; |
| 5 | + |
| 6 | +namespace Unity.Netcode.RuntimeTests |
| 7 | +{ |
| 8 | + public class NetworkListChangedTestComponent : NetworkBehaviour |
| 9 | + { |
| 10 | + |
| 11 | + } |
| 12 | + |
| 13 | + public class ListChangedObject : NetworkBehaviour |
| 14 | + { |
| 15 | + public int ExpectedPreviousValue = 0; |
| 16 | + public int ExpectedValue = 0; |
| 17 | + public bool AddDone = false; |
| 18 | + |
| 19 | + public NetworkList<int> MyNetworkList = new NetworkList<int>(); |
| 20 | + |
| 21 | + public override void OnNetworkSpawn() |
| 22 | + { |
| 23 | + MyNetworkList.OnListChanged += Changed; |
| 24 | + base.OnNetworkSpawn(); |
| 25 | + } |
| 26 | + |
| 27 | + public void Changed(NetworkListEvent<int> listEvent) |
| 28 | + { |
| 29 | + if (listEvent.Type == NetworkListEvent<int>.EventType.Value) |
| 30 | + { |
| 31 | + if (listEvent.PreviousValue != ExpectedPreviousValue) |
| 32 | + { |
| 33 | + Debug.Log($"Expected previous value mismatch {listEvent.PreviousValue} versus {ExpectedPreviousValue}"); |
| 34 | + Debug.Assert(listEvent.PreviousValue == ExpectedPreviousValue); |
| 35 | + } |
| 36 | + |
| 37 | + if (listEvent.Value != ExpectedValue) |
| 38 | + { |
| 39 | + Debug.Log($"Expected value mismatch {listEvent.Value} versus {ExpectedValue}"); |
| 40 | + Debug.Assert(listEvent.Value == ExpectedValue); |
| 41 | + } |
| 42 | + |
| 43 | + AddDone = true; |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public class NetworkListChangedTests : NetcodeIntegrationTest |
| 49 | + { |
| 50 | + protected override int NumberOfClients => 2; |
| 51 | + |
| 52 | + private ulong m_ClientId0; |
| 53 | + private GameObject m_PrefabToSpawn; |
| 54 | + |
| 55 | + private NetworkObject m_NetSpawnedObject1; |
| 56 | + |
| 57 | + protected override void OnServerAndClientsCreated() |
| 58 | + { |
| 59 | + m_PrefabToSpawn = CreateNetworkObjectPrefab("ListChangedObject"); |
| 60 | + m_PrefabToSpawn.AddComponent<ListChangedObject>(); |
| 61 | + } |
| 62 | + |
| 63 | + [UnityTest] |
| 64 | + public IEnumerator NetworkListChangedTest() |
| 65 | + { |
| 66 | + m_ClientId0 = m_ClientNetworkManagers[0].LocalClientId; |
| 67 | + |
| 68 | + // create 3 objects |
| 69 | + var spawnedObject1 = SpawnObject(m_PrefabToSpawn, m_ServerNetworkManager); |
| 70 | + m_NetSpawnedObject1 = spawnedObject1.GetComponent<NetworkObject>(); |
| 71 | + |
| 72 | + m_NetSpawnedObject1.GetComponent<ListChangedObject>().MyNetworkList.Add(42); |
| 73 | + m_NetSpawnedObject1.GetComponent<ListChangedObject>().ExpectedPreviousValue = 42; |
| 74 | + m_NetSpawnedObject1.GetComponent<ListChangedObject>().ExpectedValue = 44; |
| 75 | + m_NetSpawnedObject1.GetComponent<ListChangedObject>().MyNetworkList[0] = 44; |
| 76 | + |
| 77 | + Debug.Assert(m_NetSpawnedObject1.GetComponent<ListChangedObject>().AddDone); |
| 78 | + |
| 79 | + return null; |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments