-
Notifications
You must be signed in to change notification settings - Fork 459
Expand file tree
/
Copy pathNetworkAnimatorTests.cs
More file actions
1039 lines (888 loc) · 53.8 KB
/
NetworkAnimatorTests.cs
File metadata and controls
1039 lines (888 loc) · 53.8 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Unity.Netcode;
using Unity.Netcode.TestHelpers.Runtime;
using UnityEngine;
using UnityEngine.TestTools;
namespace TestProject.RuntimeTests
{
/// <summary>
/// Tests Various Features of The NetworkAnimator
/// !! NOTE !!
/// This test depends upon the following assets:
/// Assets\Tests\Animation\Resources\AnimatorObject.prefab
/// Assets\Tests\Manual\NetworkAnimatorTests\CubeAnimatorController.controller (referenced in AnimatorObject)
/// Possibly we could build this at runtime, but for now it uses the same animator controller as the manual
/// test does.
/// </summary>
[TestFixture(HostOrServer.Host)]
[TestFixture(HostOrServer.Server)]
public class NetworkAnimatorTests : NetcodeIntegrationTest
{
private const string k_AnimatorObjectName = "AnimatorObject";
private const string k_OwnerAnimatorObjectName = "OwnerAnimatorObject";
private const string k_OwnerPlayerCheerName = "PlayerCheerOwnerAuth";
private const string k_PlayerCheerName = "PlayerCheerServerAuth";
protected override bool m_EnableTimeTravel => true;
protected override bool m_SetupIsACoroutine => false;
protected override bool m_TearDownIsACoroutine => false;
protected override int NumberOfClients => 3;
private GameObject m_AnimationTestPrefab => m_AnimatorObjectPrefab ? m_AnimatorObjectPrefab as GameObject : null;
private GameObject m_AnimationOwnerTestPrefab => m_OwnerAnimatorObjectPrefab ? m_OwnerAnimatorObjectPrefab as GameObject : null;
private GameObject m_AnimationCheerTestPrefab => m_PlayerCheerPrefab ? m_PlayerCheerPrefab as GameObject : null;
private GameObject m_AnimationCheerOwnerTestPrefab => m_OwnerPlayerCheerPrefab ? m_OwnerPlayerCheerPrefab as GameObject : null;
private AnimatorTestHelper.ParameterValues m_ParameterValues;
private Object m_AnimatorObjectPrefab;
private Object m_OwnerAnimatorObjectPrefab;
private Object m_PlayerCheerPrefab;
private Object m_OwnerPlayerCheerPrefab;
public NetworkAnimatorTests(HostOrServer hostOrServer)
{
m_UseHost = hostOrServer == HostOrServer.Host;
}
protected override void OnOneTimeSetup()
{
m_AnimatorObjectPrefab = Resources.Load(k_AnimatorObjectName);
Assert.NotNull(m_AnimatorObjectPrefab, $"Failed to load resource {k_AnimatorObjectName}!");
m_OwnerAnimatorObjectPrefab = Resources.Load(k_OwnerAnimatorObjectName);
Assert.NotNull(m_OwnerAnimatorObjectPrefab, $"Failed to load resource {k_OwnerAnimatorObjectName}!");
m_OwnerPlayerCheerPrefab = Resources.Load(k_OwnerPlayerCheerName);
Assert.NotNull(m_OwnerPlayerCheerPrefab, $"Failed to load resource {k_OwnerPlayerCheerName}!");
m_PlayerCheerPrefab = Resources.Load(k_PlayerCheerName);
Assert.NotNull(m_PlayerCheerPrefab, $"Failed to load resource {k_PlayerCheerName}!");
base.OnOneTimeSetup();
}
protected override void OnInlineSetup()
{
AnimatorTestHelper.Initialize();
CheckStateEnterCount.ResetTest();
TriggerTest.ResetTest();
StateSyncTest.ResetTest();
}
protected override void OnInlineTearDown()
{
m_EnableVerboseDebug = false;
}
protected override void OnServerAndClientsCreated()
{
// Server authority prefab
var networkObjectServer = (m_AnimatorObjectPrefab as GameObject).GetComponent<NetworkObject>();
networkObjectServer.NetworkManagerOwner = m_ServerNetworkManager;
networkObjectServer.name = "ServerAuth";
NetcodeIntegrationTestHelpers.MakeNetworkObjectTestPrefab(networkObjectServer);
var networkAnimatorServerAuthPrefab = new NetworkPrefab() { Prefab = networkObjectServer.gameObject };
m_ServerNetworkManager.NetworkConfig.Prefabs.Add(networkAnimatorServerAuthPrefab);
// Owner authority prefab
var networkObjectOwner = (m_OwnerAnimatorObjectPrefab as GameObject).GetComponent<NetworkObject>();
networkObjectOwner.NetworkManagerOwner = m_ServerNetworkManager;
networkObjectOwner.name = "OwnerAuthority";
NetcodeIntegrationTestHelpers.MakeNetworkObjectTestPrefab(networkObjectOwner);
var networkAnimatorOwnerAuthPrefab = new NetworkPrefab() { Prefab = networkObjectOwner.gameObject };
m_ServerNetworkManager.NetworkConfig.Prefabs.Add(networkAnimatorOwnerAuthPrefab);
// Server authority player cheer prefab
var networkObjectPlayerCheerServer = (m_PlayerCheerPrefab as GameObject).GetComponent<NetworkObject>();
networkObjectPlayerCheerServer.NetworkManagerOwner = m_ServerNetworkManager;
networkObjectPlayerCheerServer.name = "PCServerAuth";
NetcodeIntegrationTestHelpers.MakeNetworkObjectTestPrefab(networkObjectPlayerCheerServer);
var networkAnimatorPlayerCheerServerAuthPrefab = new NetworkPrefab() { Prefab = networkObjectPlayerCheerServer.gameObject };
m_ServerNetworkManager.NetworkConfig.Prefabs.Add(networkAnimatorPlayerCheerServerAuthPrefab);
var networkObjectPlayerCheerOwner = (m_OwnerPlayerCheerPrefab as GameObject).GetComponent<NetworkObject>();
networkObjectPlayerCheerOwner.NetworkManagerOwner = m_ServerNetworkManager;
networkObjectPlayerCheerOwner.name = "PCOwnerAuth";
NetcodeIntegrationTestHelpers.MakeNetworkObjectTestPrefab(networkObjectPlayerCheerOwner);
var networkAnimatorPlayerCheerOwnerAuthPrefab = new NetworkPrefab() { Prefab = networkObjectPlayerCheerOwner.gameObject };
m_ServerNetworkManager.NetworkConfig.Prefabs.Add(networkAnimatorPlayerCheerOwnerAuthPrefab);
foreach (var clientNetworkManager in m_ClientNetworkManagers)
{
clientNetworkManager.NetworkConfig.Prefabs.Add(networkAnimatorServerAuthPrefab);
clientNetworkManager.NetworkConfig.Prefabs.Add(networkAnimatorOwnerAuthPrefab);
clientNetworkManager.NetworkConfig.Prefabs.Add(networkAnimatorPlayerCheerServerAuthPrefab);
clientNetworkManager.NetworkConfig.Prefabs.Add(networkAnimatorPlayerCheerOwnerAuthPrefab);
}
base.OnServerAndClientsCreated();
}
private bool ParameterValuesMatch(OwnerShipMode ownerShipMode, AuthoritativeMode authoritativeMode, bool debugInfo = false)
{
var serverParameters = AnimatorTestHelper.ServerSideInstance.GetParameterValues();
if (!serverParameters.ValuesMatch(m_ParameterValues, debugInfo))
{
return false;
}
foreach (var animatorTestHelper in AnimatorTestHelper.ClientSideInstances)
{
var clientParameters = animatorTestHelper.Value.GetParameterValues();
if (!clientParameters.ValuesMatch(m_ParameterValues, debugInfo))
{
return false;
}
}
return true;
}
public enum OwnerShipMode
{
ServerOwner,
ClientOwner
}
public enum AuthoritativeMode
{
ServerAuth,
OwnerAuth,
PCServerAuth,
PCOwnerAuth
}
private GameObject SpawnPrefab(bool isClientOwner, AuthoritativeMode authoritativeMode)
{
var networkManager = isClientOwner ? m_ClientNetworkManagers[0] : m_ServerNetworkManager;
var gameObject = (GameObject)null;
switch (authoritativeMode)
{
case AuthoritativeMode.ServerAuth:
{
Assert.NotNull(m_AnimatorObjectPrefab);
gameObject = SpawnObject(m_AnimatorObjectPrefab as GameObject, networkManager);
break;
}
case AuthoritativeMode.OwnerAuth:
{
Assert.NotNull(m_OwnerAnimatorObjectPrefab);
gameObject = SpawnObject(m_OwnerAnimatorObjectPrefab as GameObject, networkManager);
break;
}
case AuthoritativeMode.PCServerAuth:
{
Assert.NotNull(m_PlayerCheerPrefab);
gameObject = SpawnObject(m_PlayerCheerPrefab as GameObject, networkManager);
break;
}
case AuthoritativeMode.PCOwnerAuth:
{
Assert.NotNull(m_OwnerPlayerCheerPrefab);
gameObject = SpawnObject(m_OwnerPlayerCheerPrefab as GameObject, networkManager);
break;
}
}
return gameObject;
}
private string GetNetworkAnimatorName(AuthoritativeMode authoritativeMode)
{
var name = string.Empty;
switch (authoritativeMode)
{
case AuthoritativeMode.ServerAuth:
{
name = m_AnimationTestPrefab.name;
break;
}
case AuthoritativeMode.OwnerAuth:
{
name = m_AnimationOwnerTestPrefab.name;
break;
}
case AuthoritativeMode.PCServerAuth:
{
name = m_PlayerCheerPrefab.name;
break;
}
case AuthoritativeMode.PCOwnerAuth:
{
name = m_OwnerPlayerCheerPrefab.name;
break;
}
}
return name;
}
/// <summary>
/// Verifies that parameters are synchronized with currently connected clients
/// when no transition or layer change has occurred.
/// </summary>
/// <param name="authoritativeMode">Server or Owner authoritative</param>
[Test]
public void ParameterUpdateTests([Values] OwnerShipMode ownerShipMode, [Values(AuthoritativeMode.ServerAuth, AuthoritativeMode.OwnerAuth)] AuthoritativeMode authoritativeMode)
{
VerboseDebug($" ++++++++++++++++++ Parameter Test [{ownerShipMode}] Starting ++++++++++++++++++ ");
// Spawn our test animator object
var objectInstance = SpawnPrefab(ownerShipMode == OwnerShipMode.ClientOwner, authoritativeMode);
// Wait for it to spawn server-side
var success = WaitForConditionOrTimeOutWithTimeTravel(() => AnimatorTestHelper.ServerSideInstance != null);
Assert.True(success, $"Timed out waiting for the server-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");
// Wait for it to spawn client-side
success = WaitForConditionOrTimeOutWithTimeTravel(() => AnimatorTestHelper.ClientSideInstances.ContainsKey(m_ClientNetworkManagers[0].LocalClientId));
Assert.True(success, $"Timed out waiting for the client-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");
// Create new parameter values
m_ParameterValues = new AnimatorTestHelper.ParameterValues() { FloatValue = 1.0f, IntValue = 5, BoolValue = true };
if (authoritativeMode == AuthoritativeMode.OwnerAuth)
{
var objectToUpdate = ownerShipMode == OwnerShipMode.ClientOwner ? AnimatorTestHelper.ClientSideInstances[m_ClientNetworkManagers[0].LocalClientId] : AnimatorTestHelper.ServerSideInstance;
// Set the new parameter values via the owner
objectToUpdate.UpdateParameters(m_ParameterValues);
}
else
{
// Set the new parameter values via the server
AnimatorTestHelper.ServerSideInstance.UpdateParameters(m_ParameterValues);
}
// Wait for the client side to update to the new parameter values
success = WaitForConditionOrTimeOutWithTimeTravel(() => ParameterValuesMatch(ownerShipMode, authoritativeMode, m_EnableVerboseDebug));
Assert.True(success, $"Timed out waiting for the client-side parameters to match {m_ParameterValues}!");
VerboseDebug($" ------------------ Parameter Test [{ownerShipMode}] Stopping ------------------ ");
}
private bool AllTriggersDetected(OwnerShipMode ownerShipMode)
{
if (ownerShipMode == OwnerShipMode.ClientOwner)
{
if (!TriggerTest.ClientsThatTriggered.Contains(m_ServerNetworkManager.LocalClientId))
{
return false;
}
}
foreach (var animatorTestHelper in AnimatorTestHelper.ClientSideInstances)
{
if (ownerShipMode == OwnerShipMode.ClientOwner && animatorTestHelper.Value.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId)
{
continue;
}
if (!TriggerTest.ClientsThatTriggered.Contains(animatorTestHelper.Value.NetworkManager.LocalClientId))
{
return false;
}
}
return true;
}
private bool AllInstancesSameLayerWeight(OwnerShipMode ownerShipMode, int layer, float targetWeight)
{
if (ownerShipMode == OwnerShipMode.ClientOwner)
{
if (AnimatorTestHelper.ServerSideInstance.GetLayerWeight(layer) != targetWeight)
{
return false;
}
}
foreach (var animatorTestHelper in AnimatorTestHelper.ClientSideInstances)
{
if (ownerShipMode == OwnerShipMode.ClientOwner && animatorTestHelper.Value.OwnerClientId == m_ClientNetworkManagers[0].LocalClientId)
{
continue;
}
if (animatorTestHelper.Value.GetLayerWeight(layer) != targetWeight)
{
return false;
}
}
return true;
}
private bool WaitForClientsToInitialize()
{
foreach (var networkManager in m_ClientNetworkManagers)
{
var clientId = networkManager.LocalClientId;
if (!AnimatorTestHelper.ClientSideInstances.ContainsKey(clientId))
{
return false;
}
if (!AnimatorTestHelper.ClientSideInstances[clientId].GetComponent<Animator>().isInitialized)
{
return false;
}
VerboseDebug($"{networkManager.name} initialized and spawned {AnimatorTestHelper.ClientSideInstances[clientId]}.");
}
return true;
}
private bool AllClientsTransitioningAnyState()
{
foreach (var networkManager in m_ClientNetworkManagers)
{
var clientId = networkManager.LocalClientId;
if (!AnimatorTestHelper.ClientSideInstances.ContainsKey(clientId))
{
return false;
}
var animator = AnimatorTestHelper.ClientSideInstances[clientId].GetComponent<Animator>();
if (!animator.isInitialized)
{
return false;
}
var transitionInfo = animator.GetAnimatorTransitionInfo(0);
if (!transitionInfo.anyState)
{
return false;
}
VerboseDebug($"{networkManager.name} transitioning from AnyState or CrossFade.");
}
return true;
}
/// <summary>
/// Verifies that cross fading is synchronized with currently connected clients
/// </summary>
[UnityTest]
public IEnumerator CrossFadeTransitionTests([Values] OwnerShipMode ownerShipMode, [Values(AuthoritativeMode.ServerAuth, AuthoritativeMode.OwnerAuth)] AuthoritativeMode authoritativeMode)
{
CrossFadeTransitionDetect.ResetTest();
CrossFadeTransitionDetect.SetTargetAnimationState(AnimatorTestHelper.TargetCrossFadeState);
VerboseDebug($" ++++++++++++++++++ Cross Fade Transition Test [{ownerShipMode}] Starting ++++++++++++++++++ ");
CrossFadeTransitionDetect.IsVerboseDebug = m_EnableVerboseDebug;
// Spawn our test animator object
var objectInstance = SpawnPrefab(ownerShipMode == OwnerShipMode.ClientOwner, authoritativeMode);
// Wait for it to spawn server-side
var success = WaitForConditionOrTimeOutWithTimeTravel(() => AnimatorTestHelper.ServerSideInstance != null);
Assert.True(success, $"Timed out waiting for the server-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");
// Wait for it to spawn client-side
success = WaitForConditionOrTimeOutWithTimeTravel(WaitForClientsToInitialize);
Assert.True(success, $"Timed out waiting for the client-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");
var animatorTestHelper = ownerShipMode == OwnerShipMode.ClientOwner ? AnimatorTestHelper.ClientSideInstances[m_ClientNetworkManagers[0].LocalClientId] : AnimatorTestHelper.ServerSideInstance;
var layerCount = animatorTestHelper.GetAnimator().layerCount;
var animationStateCount = animatorTestHelper.GetAnimatorStateCount();
Assert.True(layerCount == animationStateCount, $"AnimationState count {animationStateCount} does not equal the layer count {layerCount}!");
if (authoritativeMode == AuthoritativeMode.ServerAuth)
{
animatorTestHelper = AnimatorTestHelper.ServerSideInstance;
}
CrossFadeTransitionDetect.ClientIds.Add(m_ServerNetworkManager.LocalClientId);
foreach (var client in m_ClientNetworkManagers)
{
CrossFadeTransitionDetect.ClientIds.Add(client.LocalClientId);
}
animatorTestHelper.TestCrossFade();
// Verify the host and all clients performed a cross fade transition
yield return WaitForConditionOrTimeOut(CrossFadeTransitionDetect.AllClientsTransitioned);
AssertOnTimeout($"Timed out waiting for all clients to transition from synchronized cross fade!");
}
private bool AllTriggersDetectedOnObserversOnly(OwnerShipMode ownerShipMode, ulong nonObserverId)
{
if (ownerShipMode == OwnerShipMode.ClientOwner)
{
if (!TriggerTest.ClientsThatTriggered.Contains(m_ServerNetworkManager.LocalClientId))
{
return false;
}
}
foreach (var animatorTestHelper in AnimatorTestHelper.ClientSideInstances)
{
var currentClientId = animatorTestHelper.Value.NetworkManager.LocalClientId;
if (currentClientId == nonObserverId || (ownerShipMode == OwnerShipMode.ClientOwner && currentClientId == animatorTestHelper.Value.OwnerClientId))
{
continue;
}
if (!TriggerTest.ClientsThatTriggered.Contains(currentClientId))
{
return false;
}
}
// Should return false always
return !TriggerTest.ClientsThatTriggered.Contains(nonObserverId);
}
private bool AllObserversSameLayerWeight(OwnerShipMode ownerShipMode, int layer, float targetWeight, ulong nonObserverId)
{
if (ownerShipMode == OwnerShipMode.ClientOwner)
{
if (AnimatorTestHelper.ServerSideInstance.GetLayerWeight(layer) != targetWeight)
{
return false;
}
}
foreach (var animatorTestHelper in AnimatorTestHelper.ClientSideInstances)
{
var currentClientId = animatorTestHelper.Value.NetworkManager.LocalClientId;
if (ownerShipMode == OwnerShipMode.ClientOwner && animatorTestHelper.Value.OwnerClientId == currentClientId)
{
continue;
}
if (currentClientId == nonObserverId)
{
if (animatorTestHelper.Value.GetLayerWeight(layer) == targetWeight)
{
return false;
}
}
else
if (animatorTestHelper.Value.GetLayerWeight(layer) != targetWeight)
{
return false;
}
}
return true;
}
[UnityTest]
public IEnumerator OnlyObserversAnimateTest([Values] OwnerShipMode ownerShipMode, [Values(AuthoritativeMode.ServerAuth, AuthoritativeMode.OwnerAuth)] AuthoritativeMode authoritativeMode)
{
// Spawn our test animator object
var objectInstance = SpawnPrefab(ownerShipMode == OwnerShipMode.ClientOwner, authoritativeMode);
var networkObject = objectInstance.GetComponent<NetworkObject>();
// Wait for it to spawn server-side
var success = WaitForConditionOrTimeOutWithTimeTravel(() => AnimatorTestHelper.ServerSideInstance != null);
Assert.True(success, $"Timed out waiting for the server-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");
// Wait for it to spawn client-side
success = WaitForConditionOrTimeOutWithTimeTravel(WaitForClientsToInitialize);
Assert.True(success, $"Timed out waiting for the server-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");
var animatorTestHelper = ownerShipMode == OwnerShipMode.ClientOwner ? AnimatorTestHelper.ClientSideInstances[m_ClientNetworkManagers[0].LocalClientId] : AnimatorTestHelper.ServerSideInstance;
networkObject.NetworkHide(m_ClientNetworkManagers[1].LocalClientId);
yield return WaitForConditionOrTimeOut(() => !m_ClientNetworkManagers[1].SpawnManager.SpawnedObjects.ContainsKey(networkObject.NetworkObjectId));
AssertOnTimeout($"Client-{m_ClientNetworkManagers[1].LocalClientId} timed out waiting to hide {networkObject.name}!");
if (authoritativeMode == AuthoritativeMode.ServerAuth)
{
animatorTestHelper = AnimatorTestHelper.ServerSideInstance;
}
animatorTestHelper.SetTrigger();
// Wait for all triggers to fire
yield return WaitForConditionOrTimeOut(() => AllTriggersDetectedOnObserversOnly(ownerShipMode, m_ClientNetworkManagers[1].LocalClientId));
AssertOnTimeout($"Timed out waiting for all triggers to match!");
animatorTestHelper.SetLayerWeight(1, 0.75f);
// Wait for all instances to update their weight value for layer 1
success = WaitForConditionOrTimeOutWithTimeTravel(() => AllObserversSameLayerWeight(ownerShipMode, 1, 0.75f, m_ClientNetworkManagers[1].LocalClientId));
Assert.True(success, $"Timed out waiting for all instances to match weight 0.75 on layer 1!");
}
/// <summary>
/// Verifies that triggers are synchronized with currently connected clients
/// </summary>
/// <param name="authoritativeMode">Server or Owner authoritative</param>
[UnityTest]
public IEnumerator TriggerUpdateTests([Values] OwnerShipMode ownerShipMode, [Values(AuthoritativeMode.ServerAuth, AuthoritativeMode.OwnerAuth)] AuthoritativeMode authoritativeMode)
{
CheckStateEnterCount.ResetTest();
VerboseDebug($" ++++++++++++++++++ Trigger Test [{TriggerTest.Iteration}][{ownerShipMode}] Starting ++++++++++++++++++ ");
TriggerTest.IsVerboseDebug = m_EnableVerboseDebug;
AnimatorTestHelper.IsTriggerTest = m_EnableVerboseDebug;
// Spawn our test animator object
var objectInstance = SpawnPrefab(ownerShipMode == OwnerShipMode.ClientOwner, authoritativeMode);
// Wait for it to spawn server-side
var success = WaitForConditionOrTimeOutWithTimeTravel(() => AnimatorTestHelper.ServerSideInstance != null);
Assert.True(success, $"Timed out waiting for the server-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");
// Wait for it to spawn client-side
success = WaitForConditionOrTimeOutWithTimeTravel(WaitForClientsToInitialize);
Assert.True(success, $"Timed out waiting for the client-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");
var animatorTestHelper = ownerShipMode == OwnerShipMode.ClientOwner ? AnimatorTestHelper.ClientSideInstances[m_ClientNetworkManagers[0].LocalClientId] : AnimatorTestHelper.ServerSideInstance;
var layerCount = animatorTestHelper.GetAnimator().layerCount;
var animationStateCount = animatorTestHelper.GetAnimatorStateCount();
Assert.True(layerCount == animationStateCount, $"AnimationState count {animationStateCount} does not equal the layer count {layerCount}!");
if (authoritativeMode == AuthoritativeMode.ServerAuth)
{
animatorTestHelper = AnimatorTestHelper.ServerSideInstance;
}
if (m_EnableVerboseDebug)
{
var retryTrigger = true;
var timeOutHelper = new TimeoutHelper(1.0f);
var count = 0;
while (retryTrigger)
{
VerboseDebug($"Current Trigger State: {animatorTestHelper.GetCurrentTriggerState()}");
VerboseDebug($"Setting Trigger");
animatorTestHelper.SetTrigger();
VerboseDebug($"New Trigger State: {animatorTestHelper.GetCurrentTriggerState()}");
// Wait for all triggers to fire
yield return WaitForConditionOrTimeOut(() => AllTriggersDetected(ownerShipMode), timeOutHelper);
retryTrigger = timeOutHelper.TimedOut;
if (retryTrigger)
{
count++;
Debug.LogWarning($"[{ownerShipMode}][{count}] Resending trigger!");
}
}
}
else
{
animatorTestHelper.SetTrigger();
// Wait for all triggers to fire
yield return WaitForConditionOrTimeOut(() => AllTriggersDetected(ownerShipMode));
AssertOnTimeout($"Timed out waiting for all triggers to match!");
}
TimeTravelToNextTick();
var clientIdList = new List<ulong>();
foreach (var client in m_ClientNetworkManagers)
{
clientIdList.Add(client.LocalClientId);
}
// Verify we only entered each state once
success = WaitForConditionOrTimeOutWithTimeTravel(() => CheckStateEnterCount.AllStatesEnteredMatch(clientIdList));
Assert.True(success, $"Timed out waiting for all states entered to match!");
// Now, update some states for several seconds to assure the AnimationState count does not grow
var waitForSeconds = new WaitForSeconds(0.25f);
bool rotateToggle = true;
for (int i = 0; i < 10; i++)
{
animatorTestHelper.SetBool("Rotate", rotateToggle);
animatorTestHelper.SetTrigger("Pulse");
animationStateCount = animatorTestHelper.GetAnimatorStateCount();
Assert.True(layerCount == animationStateCount, $"AnimationState count {animationStateCount} does not equal the layer count {layerCount}!");
yield return waitForSeconds;
rotateToggle = !rotateToggle;
}
CheckStateEnterCount.ResetTest();
if (m_EnableVerboseDebug)
{
var retryTrigger = true;
var timeOutHelper = new TimeoutHelper(1.0f);
var count = 0;
while (retryTrigger)
{
VerboseDebug($"Current Trigger State: {animatorTestHelper.GetCurrentTriggerState()}");
VerboseDebug($"Setting Attack Trigger ");
var animator = animatorTestHelper.GetAnimator();
animator.SetInteger("WeaponType", 1);
animatorTestHelper.SetTrigger("Attack");
VerboseDebug($"New Trigger State: {animatorTestHelper.GetCurrentTriggerState()}");
// Wait for all triggers to fire
yield return WaitForConditionOrTimeOut(() => AllTriggersDetected(ownerShipMode), timeOutHelper);
retryTrigger = timeOutHelper.TimedOut;
if (retryTrigger)
{
count++;
Debug.LogWarning($"[{ownerShipMode}][{count}] Resending trigger!");
}
}
}
else
{
var animator = animatorTestHelper.GetAnimator();
animator.SetInteger("WeaponType", 1);
animatorTestHelper.SetTrigger("Attack");
// Wait for all triggers to fire
yield return WaitForConditionOrTimeOut(() => AllTriggersDetected(ownerShipMode));
AssertOnTimeout($"Timed out waiting for all triggers to match!");
}
AnimatorTestHelper.IsTriggerTest = false;
VerboseDebug($" ------------------ Trigger Test [{TriggerTest.Iteration}][{ownerShipMode}] Stopping ------------------ ");
}
/// <summary>
/// Verifies that triggers are synchronized with currently connected clients
/// </summary>
/// <param name="authoritativeMode">Server or Owner authoritative</param>
[Test]
public void WeightUpdateTests([Values] OwnerShipMode ownerShipMode, [Values(AuthoritativeMode.ServerAuth, AuthoritativeMode.OwnerAuth)] AuthoritativeMode authoritativeMode)
{
CheckStateEnterCount.ResetTest();
TriggerTest.ResetTest();
VerboseDebug($" ++++++++++++++++++ Weight Test [{ownerShipMode}] Starting ++++++++++++++++++ ");
TriggerTest.IsVerboseDebug = m_EnableVerboseDebug;
AnimatorTestHelper.IsTriggerTest = m_EnableVerboseDebug;
// Spawn our test animator object
var objectInstance = SpawnPrefab(ownerShipMode == OwnerShipMode.ClientOwner, authoritativeMode);
// Wait for it to spawn server-side
var success = WaitForConditionOrTimeOutWithTimeTravel(() => AnimatorTestHelper.ServerSideInstance != null);
Assert.True(success, $"Timed out waiting for the server-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");
// Wait for it to spawn client-side
success = WaitForConditionOrTimeOutWithTimeTravel(WaitForClientsToInitialize);
Assert.True(success, $"Timed out waiting for the client-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");
var animatorTestHelper = ownerShipMode == OwnerShipMode.ClientOwner ? AnimatorTestHelper.ClientSideInstances[m_ClientNetworkManagers[0].LocalClientId] : AnimatorTestHelper.ServerSideInstance;
var layerCount = animatorTestHelper.GetAnimator().layerCount;
var animationStateCount = animatorTestHelper.GetAnimatorStateCount();
Assert.True(layerCount == animationStateCount, $"AnimationState count {animationStateCount} does not equal the layer count {layerCount}!");
if (authoritativeMode == AuthoritativeMode.ServerAuth)
{
animatorTestHelper = AnimatorTestHelper.ServerSideInstance;
}
var originalWeight = animatorTestHelper.GetLayerWeight(1);
animatorTestHelper.SetLayerWeight(1, 0.75f);
// Wait for all instances to update their weight value for layer 1
success = WaitForConditionOrTimeOutWithTimeTravel(() => AllInstancesSameLayerWeight(ownerShipMode, 1, 0.75f));
Assert.True(success, $"Timed out waiting for all instances to match weight 0.75 on layer 1!");
animatorTestHelper.SetLayerWeight(1, originalWeight);
// Wait for all instances to update their weight value for layer 1
success = WaitForConditionOrTimeOutWithTimeTravel(() => AllInstancesSameLayerWeight(ownerShipMode, 1, originalWeight));
Assert.True(success, $"Timed out waiting for all instances to match weight {originalWeight} on layer 1!");
// Now set the layer weight to 0
animatorTestHelper.SetLayerWeight(1, 0.0f);
// Now late join a client
CreateAndStartNewClientWithTimeTravel();
// Verify the late joined client is synchronized to the changed weight
success = WaitForConditionOrTimeOutWithTimeTravel(() => AllInstancesSameLayerWeight(ownerShipMode, 1, 0.0f));
Assert.True(success, $"[Late-Join] Timed out waiting for all instances to match weight 0 on layer 1!");
animatorTestHelper.SetLayerWeight(1, originalWeight);
// Wait for all instances to update their weight value for layer 1
success = WaitForConditionOrTimeOutWithTimeTravel(() => AllInstancesSameLayerWeight(ownerShipMode, 1, originalWeight));
Assert.True(success, $"Timed out waiting for all instances to match weight {originalWeight} on layer 1!");
AnimatorTestHelper.IsTriggerTest = false;
VerboseDebug($" ------------------ Weight Test [{ownerShipMode}] Stopping ------------------ ");
}
/// <summary>
/// Verifies that late joining clients are synchronized to an
/// animator's trigger state.
/// </summary>
/// <param name="authoritativeMode">Server or Owner authoritative</param>
[UnityTest]
public IEnumerator LateJoinTriggerSynchronizationTest([Values] OwnerShipMode ownerShipMode, [Values] AuthoritativeMode authoritativeMode)
{
VerboseDebug($" ++++++++++++++++++ Late Join Trigger Test [{TriggerTest.Iteration}][{ownerShipMode}] Starting ++++++++++++++++++ ");
TriggerTest.IsVerboseDebug = m_EnableVerboseDebug;
CheckStateEnterCount.IsVerboseDebug = m_EnableVerboseDebug;
AnimatorTestHelper.IsTriggerTest = m_EnableVerboseDebug;
bool isClientOwner = ownerShipMode == OwnerShipMode.ClientOwner;
// Spawn our test animator object
var objectInstance = SpawnPrefab(ownerShipMode == OwnerShipMode.ClientOwner, authoritativeMode);
// Wait for it to spawn server-side
var success = WaitForConditionOrTimeOutWithTimeTravel(() => AnimatorTestHelper.ServerSideInstance != null);
Assert.True(success, $"Timed out waiting for the server-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");
// Wait for it to spawn client-side
success = WaitForConditionOrTimeOutWithTimeTravel(WaitForClientsToInitialize);
Assert.True(success, $"Timed out waiting for the client-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");
var triggerName = authoritativeMode == AuthoritativeMode.OwnerAuth || authoritativeMode == AuthoritativeMode.ServerAuth ? "TestTrigger" : "Cheer";
// Set the trigger based on the type of test
if (authoritativeMode == AuthoritativeMode.OwnerAuth)
{
var objectToUpdate = ownerShipMode == OwnerShipMode.ClientOwner ? AnimatorTestHelper.ClientSideInstances[m_ClientNetworkManagers[0].LocalClientId] : AnimatorTestHelper.ServerSideInstance;
// Set the animation trigger via the owner
objectToUpdate.SetTrigger(triggerName);
}
else
{
// Set the animation trigger via the server
AnimatorTestHelper.ServerSideInstance.SetTrigger(triggerName, m_EnableVerboseDebug);
}
// Wait for all triggers to fire
yield return WaitForConditionOrTimeOut(() => AllTriggersDetected(ownerShipMode));
AssertOnTimeout($"Timed out waiting for all triggers to match!");
if (authoritativeMode == AuthoritativeMode.OwnerAuth || authoritativeMode == AuthoritativeMode.ServerAuth)
{
// Create new parameter values
m_ParameterValues = new AnimatorTestHelper.ParameterValues() { FloatValue = 1.0f, IntValue = 5, BoolValue = true };
if (authoritativeMode == AuthoritativeMode.OwnerAuth)
{
var objectToUpdate = ownerShipMode == OwnerShipMode.ClientOwner ? AnimatorTestHelper.ClientSideInstances[m_ClientNetworkManagers[0].LocalClientId] : AnimatorTestHelper.ServerSideInstance;
// Set the new parameter values
objectToUpdate.UpdateParameters(m_ParameterValues);
}
else
{
// Set the new parameter values
AnimatorTestHelper.ServerSideInstance.UpdateParameters(m_ParameterValues);
}
// Wait for the client side to update to the new parameter values
success = WaitForConditionOrTimeOutWithTimeTravel(() => ParameterValuesMatch(ownerShipMode, authoritativeMode, m_EnableVerboseDebug));
Assert.True(success, $"Timed out waiting for the client-side parameters to match {m_ParameterValues.ValuesToString()}!");
}
CreateAndStartNewClientWithTimeTravel();
Assert.IsTrue(m_ClientNetworkManagers.Length == NumberOfClients + 1, $"Newly created and connected client was not added to {nameof(m_ClientNetworkManagers)}!");
// Wait for it to spawn client-side
success = WaitForConditionOrTimeOutWithTimeTravel(WaitForClientsToInitialize);
Assert.True(success, $"Timed out waiting for the late joining client-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");
// Make sure the AnimatorTestHelper client side instances is the same as the TotalClients
var calculatedClients = (AnimatorTestHelper.ClientSideInstances.Count + (m_UseHost ? 1 : 0));
Assert.True(calculatedClients == TotalClients, $"Number of client");
// Now check that the late joining client and all other clients are synchronized to the trigger
yield return WaitForConditionOrTimeOut(() => AllTriggersDetected(ownerShipMode));
var message = string.Empty;
if (s_GlobalTimeoutHelper.TimedOut)
{
message = "\n Clients that triggered:";
foreach (var id in TriggerTest.ClientsThatTriggered)
{
message += $" ({id})";
}
}
AssertOnTimeout($"Timed out waiting for the late joining client's triggers to match!{message}", s_GlobalTimeoutHelper);
// Now check that the late joining client and all other clients are synchronized to the updated parameter values
if (authoritativeMode == AuthoritativeMode.OwnerAuth || authoritativeMode == AuthoritativeMode.ServerAuth)
{
success = WaitForConditionOrTimeOutWithTimeTravel(() => ParameterValuesMatch(ownerShipMode, authoritativeMode, m_EnableVerboseDebug));
Assert.True(success, $"Timed out waiting for the client-side parameters to match {m_ParameterValues.ValuesToString()}!");
}
var newlyJoinedClient = m_ClientNetworkManagers[NumberOfClients];
StopOneClientWithTimeTravel(newlyJoinedClient);
VerboseDebug($" ------------------ Late Join Trigger Test [{TriggerTest.Iteration}][{ownerShipMode}] Stopping ------------------ ");
}
/// <summary>
/// Verifies that late joining clients are synchronized to all of the
/// states of an animator.
/// </summary>
/// <param name="authoritativeMode">Server or Owner authoritative</param>
[UnityTest]
public IEnumerator LateJoinSynchronizationTest([Values] OwnerShipMode ownerShipMode, [Values(AuthoritativeMode.ServerAuth, AuthoritativeMode.OwnerAuth)] AuthoritativeMode authoritativeMode)
{
VerboseDebug($" ++++++++++++++++++ Late Join Synchronization Test [{TriggerTest.Iteration}][{ownerShipMode}] Starting ++++++++++++++++++ ");
StateSyncTest.IsVerboseDebug = m_EnableVerboseDebug;
TriggerTest.IsVerboseDebug = m_EnableVerboseDebug;
AnimatorTestHelper.IsTriggerTest = m_EnableVerboseDebug;
bool isClientOwner = ownerShipMode == OwnerShipMode.ClientOwner;
// Spawn our test animator object
var objectInstance = SpawnPrefab(ownerShipMode == OwnerShipMode.ClientOwner, authoritativeMode);
// Wait for it to spawn server-side
var success = WaitForConditionOrTimeOutWithTimeTravel(() => AnimatorTestHelper.ServerSideInstance != null);
Assert.True(success, $"Timed out waiting for the server-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");
// Wait for it to spawn client-side
success = WaitForConditionOrTimeOutWithTimeTravel(WaitForClientsToInitialize);
Assert.True(success, $"Timed out waiting for the client-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");
// Set the late join parameter based on the type of test
if (authoritativeMode == AuthoritativeMode.OwnerAuth)
{
var objectToUpdate = ownerShipMode == OwnerShipMode.ClientOwner ? AnimatorTestHelper.ClientSideInstances[m_ClientNetworkManagers[0].LocalClientId] : AnimatorTestHelper.ServerSideInstance;
// Set the late join parameter via the owner
objectToUpdate.SetLateJoinParam(true);
}
else
{
// Set the late join parameter to kick off the late join synchronization state
// (it rotates to 180 degrees and then stops animating until the value is reset)
AnimatorTestHelper.ServerSideInstance.SetLateJoinParam(true);
}
var firstClientAnimatorTestHelper = AnimatorTestHelper.ClientSideInstances[m_ClientNetworkManagers[0].LocalClientId];
// Wait for the 1st client to rotate to the 180.0f degree point
yield return WaitForConditionOrTimeOut(() => Mathf.Approximately(firstClientAnimatorTestHelper.transform.rotation.eulerAngles.y, 180.0f));
AssertOnTimeout($"Timed out waiting for client-side cube to reach 180.0f!");
AnimatorTestHelper.ServerSideInstance.GetNetworkAnimator().SynchronizationStateInfo = new List<AnimatorStateInfo>();
// Create and join a new client (late joining client)
CreateAndStartNewClientWithTimeTravel();
Assert.IsTrue(m_ClientNetworkManagers.Length == NumberOfClients + 1, $"Newly created and connected client was not added to {nameof(m_ClientNetworkManagers)}!");
// Wait for the client to have spawned and the spawned prefab to be instantiated
success = WaitForConditionOrTimeOutWithTimeTravel(WaitForClientsToInitialize);
Assert.True(success, $"Timed out waiting for the late joining client-side instance of {GetNetworkAnimatorName(authoritativeMode)} to be spawned!");
// Make sure the AnimatorTestHelper client side instances is the same as the TotalClients
var calculatedClients = (AnimatorTestHelper.ClientSideInstances.Count + (m_UseHost ? 1 : 0));
Assert.True(calculatedClients == TotalClients, $"Number of client");
var lateJoinObjectInstance = AnimatorTestHelper.ClientSideInstances[m_ClientNetworkManagers[NumberOfClients].LocalClientId];
yield return WaitForConditionOrTimeOut(() => Mathf.Approximately(lateJoinObjectInstance.transform.rotation.eulerAngles.y, 180.0f));
AssertOnTimeout($"[Late Join] Timed out waiting for cube to reach 180.0f!");
// Validate the fix by making sure the late joining client was synchronized to the server's Animator's states
success = WaitForConditionOrTimeOutWithTimeTravel(LateJoinClientSynchronized);
Assert.True(success, "[Late Join] Timed out waiting for newly joined client to have expected state synchronized!");
var newlyJoinedClient = m_ClientNetworkManagers[NumberOfClients];
StopOneClientWithTimeTravel(newlyJoinedClient);
VerboseDebug($" ------------------ Late Join Synchronization Test [{TriggerTest.Iteration}][{ownerShipMode}] Stopping ------------------ ");
}
/// <summary>
/// Used by: LateJoinSynchronizationTest
/// Wait condition method that compares the states of the late joined client
/// and the server.
/// </summary>
private bool LateJoinClientSynchronized()
{
if (!StateSyncTest.StatesEntered.ContainsKey(m_ClientNetworkManagers[NumberOfClients].LocalClientId))
{
VerboseDebug($"Late join client has not had any states synchronized yet!");
return false;
}
var serverStates = StateSyncTest.StatesEntered[m_ServerNetworkManager.LocalClientId];
var clientStates = StateSyncTest.StatesEntered[m_ClientNetworkManagers[NumberOfClients].LocalClientId];
if (serverStates.Count() != clientStates.Count())
{
VerboseDebug($"[Count][Server] {serverStates.Count} | [Client-{m_ClientNetworkManagers[NumberOfClients].LocalClientId}]{clientStates.Count}");
return false;
}
// We only check the last layer for this test as the other layers will have their normalized time slightly out of sync
var index = 2;
var serverAnimState = AnimatorTestHelper.ServerSideInstance.GetNetworkAnimator().SynchronizationStateInfo[index];// serverStates[index];
if (clientStates[index].shortNameHash != serverAnimState.shortNameHash)
{
VerboseDebug($"[Hash Fail] Server({serverAnimState.shortNameHash}) | Client({clientStates[index].shortNameHash}) ");
return false;
}
var clientNormalizedTime = clientStates[index].normalizedTime;
var serverNormalizedTime = serverAnimState.normalizedTime;
if (!Mathf.Approximately(clientNormalizedTime, serverNormalizedTime))
{
VerboseDebug($"[NormalizedTime Fail][{index}][{serverStates.Count}:{clientStates.Count}] Server({serverNormalizedTime}) | Client-{m_ClientNetworkManagers[1].LocalClientId}({clientNormalizedTime})");
return false;
}
VerboseDebug($"[NormalizedTime][{index}][{serverStates.Count}:{clientStates.Count}] Server({serverNormalizedTime}) | Client-{m_ClientNetworkManagers[1].LocalClientId}({clientNormalizedTime})");
return true;
}
private bool m_ClientDisconnected;
/// <summary>
/// This validates that NetworkAnimator properly removes its subscription to the
/// OnClientConnectedCallback when it is despawned and destroyed during the
/// shutdown sequence on both the server and the client.
/// </summary>
[Test]
public void ShutdownWhileSpawnedAndStartBackUpTest()
{
VerboseDebug($" ++++++++++++++++++ Disconnect-Reconnect Server Test Starting ++++++++++++++++++ ");
// Spawn our test animator object
var objectInstance = SpawnPrefab(false, AuthoritativeMode.ServerAuth);
var networkObjectInstance = objectInstance.GetComponent<NetworkObject>();
var serverAnimatorTestHelper = objectInstance.GetComponent<AnimatorTestHelper>();
// Wait for it to spawn server-side
var success = WaitForConditionOrTimeOutWithTimeTravel(() => AnimatorTestHelper.ServerSideInstance != null);
Assert.True(success, $"Timed out waiting for the server-side instance of {GetNetworkAnimatorName(AuthoritativeMode.ServerAuth)} to be spawned!");
// Wait for it to spawn client-side
success = WaitForConditionOrTimeOutWithTimeTravel(WaitForClientsToInitialize);
Assert.True(success, $"Timed out waiting for the client-side instance of {GetNetworkAnimatorName(AuthoritativeMode.ServerAuth)} to be spawned!");
var clientAnimatorTestHelper = s_GlobalNetworkObjects[m_ClientNetworkManagers[0].LocalClientId].Values.Where((c) => c.GetComponent<AnimatorTestHelper>() != null).First().GetComponent<AnimatorTestHelper>();
Assert.IsNotNull(clientAnimatorTestHelper, $"Could not find the client side {nameof(AnimatorTestHelper)}!");
VerboseDebug($" ++++++++++++++++++ Disconnect-Reconnect Shutting Down Clients and Server ++++++++++++++++++ ");
clientAnimatorTestHelper.OnCheckIsServerIsClient += Client_OnCheckIsServerIsClient;
// Now shutdown the clients to verify this fix.
// The client-side spawned NetworkObject should get despawned
// and invoke the Client_OnCheckIsServerIsClient action.
foreach (var clientNetworkManager in m_ClientNetworkManagers)
{
clientNetworkManager.Shutdown(true);
}
// Wait for all clients to be disconnected
success = WaitForConditionOrTimeOutWithTimeTravel(AllClientsDisconnected);
Assert.True(success, $"Timed out waiting for the client to disconnect!");
Assert.IsTrue(m_ClientTestHelperDespawned, $"Client-Side {nameof(AnimatorTestHelper)} did not have a valid IsClient setting!");
serverAnimatorTestHelper.OnCheckIsServerIsClient += Server_OnCheckIsServerIsClient;
// Now shutdown the server-side to verify this fix.
// The server-side spawned NetworkObject should get despawned
// and invoke the Server_OnCheckIsServerIsClient action.
var playerPrefabIndex = 0;
for (int i = 0; i < m_ServerNetworkManager.NetworkConfig.Prefabs.Prefabs.Count; i++)
{
if (m_ServerNetworkManager.NetworkConfig.Prefabs.Prefabs[i].Prefab.name.Contains("Player"))
{
playerPrefabIndex = i;
break;
}
}
m_ServerNetworkManager.Shutdown();
TimeTravelToNextTick();
WaitForConditionOrTimeOutWithTimeTravel(() => !m_ServerNetworkManager.ShutdownInProgress);
Assert.IsTrue(m_ServerTestHelperDespawned, $"Server-Side {nameof(AnimatorTestHelper)} did not have a valid IsServer setting!");
AssertOnTimeout($"Timed out waiting for the server to shutdown!");
VerboseDebug($" ++++++++++++++++++ Disconnect-Reconnect Restarting Server and Client ++++++++++++++++++ ");
// Since the dynamically generated PlayerPrefab is destroyed when the server shuts down,
// we need to create a new one and assign it to NetworkPrefab index 0
m_PlayerPrefab = new GameObject("Player");
NetworkObject networkObject = m_PlayerPrefab.AddComponent<NetworkObject>();
NetcodeIntegrationTestHelpers.MakeNetworkObjectTestPrefab(networkObject);
m_ServerNetworkManager.NetworkConfig.Prefabs.Prefabs[playerPrefabIndex].Prefab = m_PlayerPrefab;
// Now, restart the server and the client
m_ServerNetworkManager.StartHost();
foreach (var clientNetworkManager in m_ClientNetworkManagers)
{
clientNetworkManager.NetworkConfig.Prefabs.Prefabs[playerPrefabIndex].Prefab = m_PlayerPrefab;
clientNetworkManager.StartClient();
}
// Wait for the server and clients to start and connect
success = WaitForClientsConnectedOrTimeOutWithTimeTravel();
Assert.True(success, $"Client Failed to Connect!");
VerboseDebug($" ++++++++++++++++++ Disconnect-Reconnect Server Test Stopping ++++++++++++++++++ ");
}
private bool m_ServerTestHelperDespawned;