|
| 1 | +using System.Collections; |
| 2 | +using NUnit.Framework; |
| 3 | +using UnityEngine; |
| 4 | +using UnityEngine.TestTools; |
| 5 | + |
| 6 | +namespace Unity.Netcode.RuntimeTests |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// This class is for testing general fixes or functionality of NetworkBehaviours |
| 10 | + /// </summary> |
| 11 | + public class NetworkBehaviourGenericTests : BaseMultiInstanceTest |
| 12 | + { |
| 13 | + protected override int NbClients => 0; |
| 14 | + public override IEnumerator Setup() |
| 15 | + { |
| 16 | + // Make sure we don't automatically start |
| 17 | + m_BypassStartAndWaitForClients = true; |
| 18 | + |
| 19 | + // Create the Host and add the SimpleNetworkBehaviour component |
| 20 | + yield return base.Setup(); |
| 21 | + } |
| 22 | + |
| 23 | + public class SimpleNetworkBehaviour : NetworkBehaviour |
| 24 | + { |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// This test validates a fix to NetworkBehaviour.NetworkObject when |
| 29 | + /// the NetworkManager.LogLevel is set to Developer |
| 30 | + /// Note: This test does not require any clients, but should not impact this |
| 31 | + /// particular test if new tests are added to this class that do require clients |
| 32 | + /// </summary> |
| 33 | + [Test] |
| 34 | + public void ValidateNoSpam() |
| 35 | + { |
| 36 | + var objectToTest = new GameObject(); |
| 37 | + var simpleNetworkBehaviour = objectToTest.AddComponent<SimpleNetworkBehaviour>(); |
| 38 | + |
| 39 | + // Now just start the Host |
| 40 | + Assert.True(MultiInstanceHelpers.Start(true, m_ServerNetworkManager, new NetworkManager[] { }), "Failed to start the host!"); |
| 41 | + |
| 42 | + // set the log level to developer |
| 43 | + m_ServerNetworkManager.LogLevel = LogLevel.Developer; |
| 44 | + |
| 45 | + // Verify the warning gets logged under normal conditions |
| 46 | + var isNull = simpleNetworkBehaviour.NetworkObject == null; |
| 47 | + LogAssert.Expect(LogType.Warning, $"[Netcode] Could not get {nameof(NetworkObject)} for the {nameof(NetworkBehaviour)}. Are you missing a {nameof(NetworkObject)} component?"); |
| 48 | + |
| 49 | + var networkObjectToTest = objectToTest.AddComponent<NetworkObject>(); |
| 50 | + networkObjectToTest.Spawn(); |
| 51 | + |
| 52 | + // Assure no log messages are logged when they should not be logged |
| 53 | + isNull = simpleNetworkBehaviour.NetworkObject != null; |
| 54 | + LogAssert.NoUnexpectedReceived(); |
| 55 | + |
| 56 | + networkObjectToTest.Despawn(); |
| 57 | + Object.Destroy(networkObjectToTest); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments