-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathBasicInputTests.cs
More file actions
900 lines (734 loc) · 43.2 KB
/
Copy pathBasicInputTests.cs
File metadata and controls
900 lines (734 loc) · 43.2 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
// Copyright (c) Mixed Reality Toolkit Contributors
// Licensed under the BSD 3-Clause
// Disable "missing XML comment" warning for tests. While nice to have, this documentation is not required.
#pragma warning disable CS1591
using MixedReality.Toolkit.Core.Tests;
using NUnit.Framework;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.TestTools;
using UnityEngine.XR;
using UnityEngine.XR.Interaction.Toolkit;
using MixedReality.Toolkit.Input;
using MixedReality.Toolkit.Input.Simulation;
using MixedReality.Toolkit;
using MixedReality.Toolkit.Subsystems;
using HandshapeId = MixedReality.Toolkit.Input.HandshapeTypes.HandshapeId;
namespace MixedReality.Toolkit.Input.Tests
{
/// <summary>
/// Basic tests for verifying user input and basic interactions.
/// </summary>
public class BasicInputTests : BaseRuntimeInputTests
{
/// <summary>
/// Ensure the simulated input devices are registered and present.
/// </summary>
[UnityTest]
public IEnumerator InputDeviceSmoketest()
{
foreach (var device in InputSystem.devices)
{
Debug.Log(device);
}
Assert.That(InputSystem.devices, Has.Exactly(2).TypeOf<MRTKSimulatedController>());
yield return null;
}
/// <summary>
/// Ensure the simulated input devices bind to the controllers on the rig.
/// </summary>
[UnityTest]
public IEnumerator InputBindingSmoketest()
{
var controllers = new[] {
CachedLookup.LeftHandController,
CachedLookup.RightHandController,
CachedLookup.GazeController
};
foreach (var controller in controllers)
{
Assert.That(controller, Is.Not.Null);
Assert.That(controller, Is.AssignableTo(typeof(ActionBasedController)));
ActionBasedController actionBasedController = controller as ActionBasedController;
Assert.That(actionBasedController.positionAction.action.controls, Has.Count.GreaterThanOrEqualTo(1));
}
yield return null;
}
/// <summary>
/// Ensure the simulated input device actually makes the rig's controllers move/actuate.
/// </summary>
[UnityTest]
public IEnumerator HandMovingSmoketest()
{
var controller = CachedLookup.RightHandController as ActionBasedController;
var testHand = new TestHand(Handedness.Right);
InputTestUtilities.SetHandAnchorPoint(Handedness.Right, ControllerAnchorPoint.Device);
yield return testHand.Show(Vector3.forward);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.That(controller.transform.position.x, Is.EqualTo(0.0f).Within(0.01f));
yield return testHand.Move(Vector3.right * 0.5f, 60);
yield return RuntimeTestUtilities.WaitForUpdates();
Debug.Log("Input system update mode: " + InputSystem.settings.updateMode);
Assert.That(controller.positionAction.action.controls, Has.Count.GreaterThanOrEqualTo(1));
Assert.That(controller.positionAction.action.activeControl, Is.Not.Null);
Assert.That(controller.positionAction.action.ReadValue<Vector3>().x, Is.EqualTo(0.5f).Within(0.01f));
Assert.That(controller.transform.position.x, Is.EqualTo(0.5f).Within(0.01f));
yield return null;
}
/// <summary>
/// Test that anchoring the test hands on the grab point actually results in the grab interactor
/// being located where we want it to be.
/// </summary>
[UnityTest]
public IEnumerator GrabAnchorTest()
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
var interactable = cube.AddComponent<MRTKBaseInteractable>();
Vector3 cubePos = InputTestUtilities.InFrontOfUser();
cube.transform.position = cubePos;
cube.transform.localScale = Vector3.one * 1.0f;
var testHand = new TestHand(Handedness.Right);
InputTestUtilities.SetHandAnchorPoint(Handedness.Right, ControllerAnchorPoint.Grab);
yield return testHand.Show(Vector3.zero);
yield return RuntimeTestUtilities.WaitForUpdates();
yield return testHand.MoveTo(cubePos);
yield return RuntimeTestUtilities.WaitForUpdates();
var hands = XRSubsystemHelpers.GetFirstRunningSubsystem<HandsAggregatorSubsystem>();
bool gotJoint = hands.TryGetPinchingPoint(XRNode.RightHand, out HandJointPose jointPose);
Assert.IsTrue(interactable.IsGrabHovered, "Interactable wasn't grab hovered!");
Assert.IsTrue((interactable.HoveringGrabInteractors[0] as GrabInteractor).enabled, "Interactor wasn't enabled");
TestUtilities.AssertAboutEqual(interactable.HoveringGrabInteractors[0].GetAttachTransform(interactable).position,
cubePos, "Grab interactor's attachTransform wasn't where we wanted it to go!", 0.001f);
}
/// <summary>
/// Very basic test of StatefulInteractable's poke hovering, grab selecting,
/// and toggling mechanics. Does not test rays or gaze interactions.
/// </summary>
[UnityTest]
public IEnumerator StatefulInteractableSmokeTest()
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.AddComponent<StatefulInteractable>();
cube.transform.position = InputTestUtilities.InFrontOfUser(new Vector3(0.2f, 0.2f, 0.5f));
cube.transform.localScale = Vector3.one * 0.1f;
// For this test, we won't use poke selection.
cube.GetComponent<StatefulInteractable>().DisableInteractorType(typeof(PokeInteractor));
GameObject cube2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube2.AddComponent<StatefulInteractable>();
cube2.transform.position = InputTestUtilities.InFrontOfUser(new Vector3(-0.2f, -0.2f, 0.5f));
cube2.transform.localScale = Vector3.one * 0.1f;
// For this test, we won't use poke selection.
cube2.GetComponent<StatefulInteractable>().DisableInteractorType(typeof(PokeInteractor));
var rightHand = new TestHand(Handedness.Right);
yield return rightHand.Show(InputTestUtilities.InFrontOfUser(0.5f));
yield return RuntimeTestUtilities.WaitForUpdates();
bool shouldTestToggle = false;
StatefulInteractable firstCubeInteractable = cube.GetComponent<StatefulInteractable>();
StatefulInteractable secondCubeInteractable = cube2.GetComponent<StatefulInteractable>();
for (int i = 0; i < 5; i++)
{
// Flip this back and forth to test both toggleability and un-toggleability
shouldTestToggle = !shouldTestToggle;
yield return rightHand.RotateTo(Quaternion.Euler(0, 45, 0));
yield return RuntimeTestUtilities.WaitForUpdates();
// Test the first cube.
firstCubeInteractable.ForceSetToggled(false);
firstCubeInteractable.ToggleMode = shouldTestToggle ? StatefulInteractable.ToggleType.Toggle : StatefulInteractable.ToggleType.Button;
firstCubeInteractable.TriggerOnRelease = (i % 2) == 0;
Assert.IsFalse(firstCubeInteractable.IsGrabHovered,
"StatefulInteractable was already hovered.");
yield return rightHand.MoveTo(cube.transform.position);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsTrue(firstCubeInteractable.IsGrabHovered,
"StatefulInteractable did not get hovered.");
yield return rightHand.SetHandshape(HandshapeId.Pinch);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsTrue(firstCubeInteractable.IsGrabSelected,
"StatefulInteractable did not get GrabSelected.");
if (shouldTestToggle)
{
if (secondCubeInteractable.TriggerOnRelease)
{
Assert.IsFalse(secondCubeInteractable.IsToggled, "StatefulInteractable toggled on press, when it was set to be toggled on release.");
}
else
{
Assert.IsFalse(secondCubeInteractable.IsToggled, "StatefulInteractable didn't toggled on press, when it was set to be toggled on press.");
}
}
yield return rightHand.SetHandshape(HandshapeId.Open);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsFalse(firstCubeInteractable.IsGrabSelected,
"StatefulInteractable did not get un-GrabSelected.");
if (shouldTestToggle)
{
Assert.IsTrue(firstCubeInteractable.IsToggled, "StatefulInteractable did not get toggled.");
}
else
{
Assert.IsFalse(firstCubeInteractable.IsToggled, "StatefulInteractable shouldn't have been toggled, but it was.");
}
// Test the second cube.
secondCubeInteractable.ForceSetToggled(false);
secondCubeInteractable.ToggleMode = shouldTestToggle ? StatefulInteractable.ToggleType.Toggle : StatefulInteractable.ToggleType.Button;
secondCubeInteractable.TriggerOnRelease = (i % 2) == 0;
yield return rightHand.MoveTo(InputTestUtilities.InFrontOfUser(0.5f));
yield return RuntimeTestUtilities.WaitForUpdates();
yield return rightHand.RotateTo(Quaternion.Euler(0, -45, 0));
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsFalse(secondCubeInteractable.IsGrabHovered,
"StatefulInteractable was already hovered.");
yield return rightHand.MoveTo(secondCubeInteractable.transform.position);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsTrue(secondCubeInteractable.IsGrabHovered,
"StatefulInteractable did not get hovered.");
yield return rightHand.SetHandshape(HandshapeId.Pinch);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsTrue(secondCubeInteractable.IsGrabSelected,
"StatefulInteractable did not get GrabSelected.");
if (shouldTestToggle)
{
if (secondCubeInteractable.TriggerOnRelease)
{
Assert.IsFalse(secondCubeInteractable.IsToggled, "StatefulInteractable toggled on press, when it was set to be toggled on release.");
}
else
{
Assert.IsFalse(secondCubeInteractable.IsToggled, "StatefulInteractable didn't toggled on press, when it was set to be toggled on press.");
}
}
yield return rightHand.SetHandshape(HandshapeId.Open);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsFalse(secondCubeInteractable.IsGrabSelected,
"StatefulInteractable did not get un-GrabSelected.");
if (shouldTestToggle)
{
Assert.IsTrue(secondCubeInteractable.IsToggled, "StatefulInteractable did not get toggled.");
}
else
{
Assert.IsFalse(secondCubeInteractable.IsToggled, "StatefulInteractable shouldn't have been toggled, but it was.");
}
yield return rightHand.MoveTo(InputTestUtilities.InFrontOfUser(0.5f));
yield return RuntimeTestUtilities.WaitForUpdates();
}
yield return null;
}
/// <summary>
/// Simple smoke test to ensure basic gaze-pinch selection functionality.
/// </summary>
[UnityTest]
public IEnumerator GazePinchSmokeTest()
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
StatefulInteractable interactable = cube.AddComponent<StatefulInteractable>();
cube.transform.position = InputTestUtilities.InFrontOfUser();
cube.transform.localScale = Vector3.one * 0.1f;
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsTrue(interactable.isHovered);
Assert.IsTrue(interactable.IsGazeHovered);
Assert.IsFalse(interactable.IsGazePinchHovered);
var rightHand = new TestHand(Handedness.Right);
yield return rightHand.Show(InputTestUtilities.InFrontOfUser() + new Vector3(0.2f, 0, 0f));
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsTrue(interactable.isHovered);
Assert.IsTrue(interactable.IsGazePinchHovered);
yield return rightHand.SetHandshape(HandshapeId.Pinch);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsTrue(interactable.isSelected);
Assert.IsTrue(interactable.IsGazePinchSelected);
yield return rightHand.SetHandshape(HandshapeId.Open);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsFalse(interactable.isSelected);
Assert.IsFalse(interactable.IsGazePinchSelected);
Assert.IsTrue(interactable.isHovered);
Assert.IsTrue(interactable.IsGazePinchHovered);
}
[UnityTest]
public IEnumerator TestStatefulInteractableSelectMode(
[Values(InteractableSelectMode.Single, InteractableSelectMode.Multiple)] InteractableSelectMode selectMode,
[Values(true, false)] bool triggerOnRelease,
[Values(true, false)] bool releaseInSelectOrder)
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
StatefulInteractable interactable = cube.AddComponent<StatefulInteractable>();
cube.transform.position = InputTestUtilities.InFrontOfUser(new Vector3(0.2f, 0.2f, 0.5f));
cube.transform.localScale = Vector3.one * 0.1f;
bool isSelected = false;
bool selectEntered = false;
bool selectExited = false;
int clickCount = 0;
void ResetState()
{
selectEntered = false;
selectExited = false;
clickCount = 0;
}
// For this test, we won't use poke or grab selection
interactable.DisableInteractorType(typeof(PokeInteractor));
interactable.DisableInteractorType(typeof(GrabInteractor));
interactable.selectMode = selectMode;
interactable.TriggerOnRelease = triggerOnRelease;
interactable.firstSelectEntered.AddListener((_) => isSelected = true);
interactable.lastSelectExited.AddListener((_) => isSelected = false);
interactable.selectEntered.AddListener((_) => selectEntered = true);
interactable.selectExited.AddListener((_) => selectExited = true);
interactable.OnClicked.AddListener(() => clickCount++);
// Introduce the first hand
var rightHand = new TestHand(Handedness.Right);
yield return rightHand.Show(InputTestUtilities.InFrontOfUser(0.4f));
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsFalse(interactable.IsRayHovered,
"StatefulInteractable was already RayHovered.");
Assert.IsFalse(interactable.isHovered,
"StatefulInteractable was already hovered.");
// Aim the first hand to hover the cube
yield return rightHand.AimAt(cube.transform.position);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsTrue(interactable.IsRayHovered,
"StatefulInteractable did not get RayHovered.");
Assert.IsTrue(interactable.isHovered,
"StatefulInteractable did not get hovered.");
Assert.IsTrue(interactable.HoveringRayInteractors.Count == 1,
"StatefulInteractable should only have 1 hovering RayInteractor.");
Assert.IsTrue(interactable.interactorsHovering.Count == 1,
"StatefulInteractable should only have 1 hovering interactor.");
Assert.IsFalse(isSelected,
"StatefulInteractable should not be selected.");
Assert.IsFalse(selectEntered,
"StatefulInteractable should not have had a select enter.");
Assert.IsFalse(selectExited,
"StatefulInteractable should not have had a select exit.");
// Pinch the first hand to select the cube
yield return rightHand.SetHandshape(HandshapeId.Pinch);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsTrue(interactable.IsRaySelected,
"StatefulInteractable did not get RaySelected.");
Assert.IsTrue(interactable.isSelected,
"StatefulInteractable did not get selected.");
Assert.IsTrue(interactable.interactorsSelecting.Count == 1,
"StatefulInteractable should only have 1 selecting interactor.");
Assert.IsTrue(isSelected,
"StatefulInteractable did not get selected.");
Assert.IsTrue(selectEntered,
"StatefulInteractable should have had a select enter.");
Assert.IsFalse(selectExited,
"StatefulInteractable should not have had a select exit.");
Assert.AreEqual(triggerOnRelease ? 0 : 1, clickCount);
// Reset to continue testing
ResetState();
// Release the first hand to deselect the cube
yield return rightHand.SetHandshape(HandshapeId.Open);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsFalse(interactable.IsRaySelected,
"StatefulInteractable did not get de-RaySelected.");
Assert.IsFalse(interactable.isSelected,
"StatefulInteractable did not get deselected.");
Assert.IsTrue(interactable.interactorsSelecting.Count == 0,
"StatefulInteractable should not have any selecting interactors.");
Assert.IsFalse(isSelected,
"StatefulInteractable should not be selected.");
Assert.IsFalse(selectEntered,
"StatefulInteractable should not have had a select enter.");
Assert.IsTrue(selectExited,
"StatefulInteractable should have had a select exit.");
Assert.AreEqual(triggerOnRelease ? 1 : 0, clickCount);
// Reset to continue testing
ResetState();
// Introduce the second hand
var leftHand = new TestHand(Handedness.Left);
yield return leftHand.Show(InputTestUtilities.InFrontOfUser(0.4f));
yield return RuntimeTestUtilities.WaitForUpdates();
// Aim the second hand to hover the cube
yield return leftHand.AimAt(cube.transform.position);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsTrue(interactable.IsRayHovered,
"StatefulInteractable did not stay RayHovered.");
Assert.IsTrue(interactable.isHovered,
"StatefulInteractable did not stay hovered.");
Assert.IsTrue(interactable.HoveringRayInteractors.Count == 2,
"StatefulInteractable should have 2 hovering RayInteractors.");
Assert.IsTrue(interactable.interactorsHovering.Count == 2,
"StatefulInteractable should have 2 hovering interactors.");
Assert.IsFalse(isSelected,
"StatefulInteractable should not be selected.");
Assert.IsFalse(selectEntered,
"StatefulInteractable should not have had a select enter.");
Assert.IsFalse(selectExited,
"StatefulInteractable should not have had a select exit.");
// Pinch the first hand to select the cube
yield return rightHand.SetHandshape(HandshapeId.Pinch);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsTrue(interactable.IsRaySelected,
"StatefulInteractable did not get RaySelected.");
Assert.IsTrue(interactable.isSelected,
"StatefulInteractable did not get selected.");
Assert.IsTrue(interactable.interactorsSelecting.Count == 1,
"StatefulInteractable should only have 1 selecting interactor.");
Assert.IsTrue(isSelected,
"StatefulInteractable did not get selected.");
Assert.IsTrue(selectEntered,
"StatefulInteractable should have had a select enter.");
Assert.IsFalse(selectExited,
"StatefulInteractable should not have had a select exit.");
Assert.AreEqual(triggerOnRelease ? 0 : 1, clickCount);
// Reset to continue testing
ResetState();
// Pinch the second hand to select the cube
yield return leftHand.SetHandshape(HandshapeId.Pinch);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsTrue(interactable.IsRaySelected,
"StatefulInteractable did not stay RaySelected.");
Assert.IsTrue(interactable.isSelected,
"StatefulInteractable did not stay selected.");
Assert.IsTrue(isSelected,
"StatefulInteractable did not stay selected.");
Assert.IsTrue(selectEntered,
"StatefulInteractable should have had a select enter.");
// Both hands are pinching, so we check the select state based on the mode
switch (selectMode)
{
case InteractableSelectMode.Single:
Assert.IsTrue(interactable.interactorsSelecting.Count == 1,
"StatefulInteractable should only have 1 selecting interactor.");
Assert.IsTrue(selectExited,
"StatefulInteractable should have had a select exit.");
Assert.AreEqual(1, clickCount);
break;
case InteractableSelectMode.Multiple:
Assert.IsTrue(interactable.interactorsSelecting.Count == 2,
"StatefulInteractable should have 2 selecting interactors.");
Assert.IsFalse(selectExited,
"StatefulInteractable should not have had a select exit.");
Assert.AreEqual(0, clickCount);
break;
default:
Assert.Fail($"Unhandled {nameof(InteractableSelectMode)}={selectMode}");
break;
}
// Reset to continue testing
ResetState();
TestHand firstReleasedHand;
TestHand secondReleasedHand;
if (releaseInSelectOrder)
{
firstReleasedHand = rightHand;
secondReleasedHand = leftHand;
}
else
{
firstReleasedHand = leftHand;
secondReleasedHand = rightHand;
}
// Release a hand to deselect the cube
yield return firstReleasedHand.SetHandshape(HandshapeId.Open);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsFalse(selectEntered,
"StatefulInteractable should not have had a select enter.");
// The first hand was no longer selecting in Single mode
// If we're releasing in the reverse order we selected,
// releasing the second pinch should release the select fully
if (!releaseInSelectOrder && selectMode == InteractableSelectMode.Single)
{
Assert.IsFalse(interactable.IsRaySelected,
"StatefulInteractable did not get de-RaySelected.");
Assert.IsFalse(interactable.isSelected,
"StatefulInteractable did not get deselected.");
Assert.IsTrue(interactable.interactorsSelecting.Count == 0,
"StatefulInteractable should not have any selecting interactors.");
Assert.IsFalse(isSelected,
"StatefulInteractable should not be selected.");
Assert.IsTrue(selectExited,
"StatefulInteractable should have had a select exit.");
Assert.AreEqual(triggerOnRelease ? 1 : 0, clickCount);
}
// The first hand was no longer selecting in Single mode
// If we're releasing in the same order we selected,
// we should still be selected regardless of the mode
else
{
Assert.IsTrue(interactable.IsRaySelected,
"StatefulInteractable did not stay RaySelected.");
Assert.IsTrue(interactable.isSelected,
"StatefulInteractable did not stay selected.");
Assert.IsTrue(interactable.interactorsSelecting.Count == 1,
"StatefulInteractable should only have 1 selecting interactor.");
Assert.IsTrue(isSelected,
"StatefulInteractable should be selected.");
Assert.AreEqual(0, clickCount);
if (selectMode == InteractableSelectMode.Multiple)
{
Assert.IsTrue(selectExited,
"StatefulInteractable should have had a select exit.");
}
else
{
// This select exit happened when the second hand pinched, releasing the first
Assert.IsFalse(selectExited,
"StatefulInteractable should not have had a select exit.");
}
}
// Reset to continue testing
ResetState();
// Release the last hand to deselect the cube
yield return secondReleasedHand.SetHandshape(HandshapeId.Open);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsFalse(interactable.IsRaySelected,
"StatefulInteractable did not get de-RaySelected.");
Assert.IsFalse(interactable.isSelected,
"StatefulInteractable did not get deselected.");
Assert.IsTrue(interactable.interactorsSelecting.Count == 0,
"StatefulInteractable should not have any selecting interactors.");
Assert.IsFalse(isSelected,
"StatefulInteractable should not be selected.");
Assert.IsFalse(selectEntered,
"StatefulInteractable should not have had a select enter.");
if (!releaseInSelectOrder && selectMode == InteractableSelectMode.Single)
{
Assert.IsFalse(selectExited,
"StatefulInteractable should not have had a select exit.");
}
else
{
Assert.IsTrue(selectExited,
"StatefulInteractable should have had a select exit.");
Assert.AreEqual(triggerOnRelease ? 1 : 0, clickCount);
}
// Reset to continue testing
ResetState();
yield return RuntimeTestUtilities.WaitForUpdates();
}
/// <summary>
/// A dummy interactor used to test basic selection/toggle logic.
/// </summary>
private class TestInteractor : XRBaseInteractor { }
/// <summary>
/// Test that the correct toggle state should be readable after receiving an OnClicked event.
/// </summary>
[UnityTest]
public IEnumerator TestToggleEventOrdering()
{
var gameObject = new GameObject();
var interactable = gameObject.AddComponent<StatefulInteractable>();
var interactor = gameObject.AddComponent<TestInteractor>();
bool receivedOnClicked = false;
bool expectedToggleState = false;
interactable.OnClicked.AddListener(() =>
{
receivedOnClicked = true;
Assert.IsTrue(interactable.IsToggled == expectedToggleState, "Toggle state had an unexpected value");
});
interactor.StartManualInteraction(interactable as IXRSelectInteractable);
yield return null;
interactor.EndManualInteraction();
yield return null;
Assert.IsTrue(receivedOnClicked, "Didn't receive click event");
receivedOnClicked = false;
interactable.ToggleMode = StatefulInteractable.ToggleType.Toggle;
expectedToggleState = true;
interactor.StartManualInteraction(interactable as IXRSelectInteractable);
yield return null;
interactor.EndManualInteraction();
yield return null;
Assert.IsTrue(receivedOnClicked, "Didn't receive click event");
receivedOnClicked = false;
expectedToggleState = false;
interactor.StartManualInteraction(interactable as IXRSelectInteractable);
yield return null;
interactor.EndManualInteraction();
yield return null;
Assert.IsTrue(receivedOnClicked, "Didn't receive click event");
}
/// <summary>
/// Test whether toggle state can be hydrated without firing events.
/// </summary>
[UnityTest]
public IEnumerator ToggleHydrationTest()
{
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
var interactable = cube.AddComponent<StatefulInteractable>();
bool didFireEvent = false;
interactable.IsToggled.OnEntered.AddListener((_) => didFireEvent = true);
interactable.IsToggled.OnExited.AddListener((_) => didFireEvent = true);
interactable.ForceSetToggled(true);
Assert.IsTrue(interactable.IsToggled, "Interactable didn't get toggled.");
Assert.IsTrue(didFireEvent, "ForceSetToggled(true) should have fired the event.");
didFireEvent = false;
interactable.ForceSetToggled(false);
Assert.IsFalse(interactable.IsToggled, "Interactable didn't get detoggled.");
Assert.IsTrue(didFireEvent, "ForceSetToggled(false) should have fired the event.");
didFireEvent = false;
interactable.ForceSetToggled(true, fireEvents: false);
Assert.IsTrue(interactable.IsToggled, "Interactable didn't get toggled.");
Assert.IsFalse(didFireEvent, "ForceSetToggled(true, fireEvents:false) should NOT have fired the event.");
interactable.ForceSetToggled(false, fireEvents: false);
Assert.IsFalse(interactable.IsToggled, "Interactable didn't get detoggled.");
Assert.IsFalse(didFireEvent, "ForceSetToggled(false, fireEvents:false) should NOT have fired the event.");
yield return null;
}
/// <summary>
/// Tests whether disabling an interactable mid-interaction will
/// break XRDirectInteractor. Repro test for ADO#1582/1581.
/// </summary>
[UnityTest]
public IEnumerator InteractableDisabledDuringInteraction()
{
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = InputTestUtilities.InFrontOfUser(new Vector3(1.0f, 0.1f, 1.0f));
cube.transform.localScale = Vector3.one * 0.1f;
cube.AddComponent<StatefulInteractable>();
// Otherwise, poke will conflict with grab.
cube.GetComponent<StatefulInteractable>().selectMode = InteractableSelectMode.Multiple;
var rightHand = new TestHand(Handedness.Right);
yield return rightHand.Show(InputTestUtilities.InFrontOfUser());
yield return RuntimeTestUtilities.WaitForUpdates();
yield return rightHand.MoveTo(cube.transform.position);
yield return RuntimeTestUtilities.WaitForUpdates();
yield return rightHand.SetHandshape(HandshapeId.Pinch);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsTrue(cube.GetComponent<StatefulInteractable>().IsGrabSelected,
"StatefulInteractable did not get GrabSelected.");
cube.SetActive(false);
yield return rightHand.SetHandshape(HandshapeId.Open);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsFalse(cube.GetComponent<StatefulInteractable>().IsGrabSelected,
"StatefulInteractable did not get un-GrabSelected.");
yield return rightHand.MoveTo(Vector3.zero);
yield return RuntimeTestUtilities.WaitForUpdates();
cube.SetActive(true);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsFalse(AnyProximityDetectorsTriggered(),
"ProximityInteractor was still hovering after re-enabling faraway object.");
XRBaseController rightHandController = CachedLookup.RightHandController;
Assert.IsTrue(rightHandController != null, "No controllers found for right hand.");
Assert.IsTrue(rightHandController.GetComponentInChildren<MRTKRayInteractor>().enabled, "Ray didn't reactivate");
Assert.IsTrue(rightHandController.GetComponentInChildren<GazePinchInteractor>().enabled, "GazePinch didn't reactivate");
Assert.IsFalse(rightHandController.GetComponentInChildren<PokeInteractor>().enabled, "Poke didn't deactivate");
Assert.IsFalse(rightHandController.GetComponentInChildren<GrabInteractor>().enabled, "Grab didn't deactivate");
yield return null;
}
/// <summary>
/// Tests whether spawning an interactable on top of a hand will cause problems with the proximity detector.
/// </summary>
[UnityTest]
public IEnumerator SpawnInteractableOnHand()
{
// Spawn our hand.
var rightHand = new TestHand(Handedness.Right);
yield return rightHand.Show(InputTestUtilities.InFrontOfUser());
yield return RuntimeTestUtilities.WaitForUpdates();
// Prox detector should start out un-triggered.
Assert.IsFalse(AnyProximityDetectorsTriggered(), "Prox detector started out triggered, when it shouldn't be (no cube yet!)");
// Rays should start enabled
XRBaseController rightHandController = CachedLookup.RightHandController;
Assert.IsTrue(rightHandController != null, "No controllers found for right hand.");
Assert.IsTrue(rightHandController.GetComponentInChildren<MRTKRayInteractor>().enabled, "Ray didn't start active");
Assert.IsTrue(rightHandController.GetComponentInChildren<GazePinchInteractor>().enabled, "GazePinch didn't start active");
Assert.IsFalse(rightHandController.GetComponentInChildren<PokeInteractor>().enabled, "Poke started active, when it shouldn't");
Assert.IsFalse(rightHandController.GetComponentInChildren<GrabInteractor>().enabled, "Grab started active, when it shouldn't");
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = InputTestUtilities.InFrontOfUser();
cube.transform.localScale = Vector3.one * 0.1f;
cube.AddComponent<StatefulInteractable>();
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsTrue(AnyProximityDetectorsTriggered(), "Prox detector should see it!");
Assert.IsFalse(rightHandController.GetComponentInChildren<MRTKRayInteractor>().enabled, "Ray didn't disable on proximity");
Assert.IsFalse(rightHandController.GetComponentInChildren<GazePinchInteractor>().enabled, "GazePinch disable on proximity");
Assert.IsTrue(rightHandController.GetComponentInChildren<PokeInteractor>().enabled, "Poke didn't activate on proximity");
Assert.IsTrue(rightHandController.GetComponentInChildren<GrabInteractor>().enabled, "Grab didn't activate on proximity");
// Move hand far away.
yield return rightHand.MoveTo(new Vector3(2, 2, 2));
yield return RuntimeTestUtilities.WaitForUpdates(frameCount:240);
Assert.IsFalse(AnyProximityDetectorsTriggered(), "Prox detectors should no longer be triggered.");
Assert.IsTrue(rightHandController.GetComponentInChildren<MRTKRayInteractor>().enabled, "Ray didn't reactivate");
Assert.IsTrue(rightHandController.GetComponentInChildren<GazePinchInteractor>().enabled, "GazePinch didn't reactivate");
Assert.IsFalse(rightHandController.GetComponentInChildren<PokeInteractor>().enabled, "Poke didn't deactivate");
Assert.IsFalse(rightHandController.GetComponentInChildren<GrabInteractor>().enabled, "Grab didn't deactivate");
yield return null;
}
/// <summary>
/// Tests to make sure that untracked controllers can't initiate any new interactions and that their interactors can no longer hover.
/// However, the interactions should still maintaining any original selected states, as the loss of tracking is usually just temporary
/// i.e. we don't want to immediately let go of a gripped object due to a momentary loss in tracking
/// </summary>
[UnityTest]
public IEnumerator UntrackedControllerNearInteractions()
{
var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = InputTestUtilities.InFrontOfUser(new Vector3(1.0f, 0.1f, 1.0f));
cube.transform.localScale = Vector3.one * 0.1f;
cube.AddComponent<StatefulInteractable>();
// Otherwise, poke will conflict with grab.
cube.GetComponent<StatefulInteractable>().selectMode = InteractableSelectMode.Multiple;
var rightHand = new TestHand(Handedness.Right);
yield return RuntimeTestUtilities.WaitForUpdates();
yield return rightHand.Show(InputTestUtilities.InFrontOfUser(0.5f));
yield return RuntimeTestUtilities.WaitForUpdates();
// First ensure that the interactor can interact with a cube normally
yield return rightHand.MoveTo(cube.transform.position);
yield return RuntimeTestUtilities.WaitForUpdates();
yield return rightHand.SetHandshape(HandshapeId.Pinch);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsTrue(cube.GetComponent<StatefulInteractable>().IsGrabSelected,
"StatefulInteractable did not get GrabSelected.");
// Now check that all hovers are disabled while selection is maintained after we "lose tracking", which is done by hiding the hand
yield return rightHand.Hide();
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsTrue(cube.GetComponent<StatefulInteractable>().IsGrabSelected,
"StatefulInteractable is no longer GrabSelected.");
// Make sure state is maintained even if the hand gameobject moves
yield return rightHand.Move(Vector3.left);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsTrue(cube.GetComponent<StatefulInteractable>().IsGrabSelected,
"StatefulInteractable is no longer GrabSelected.");
yield return rightHand.SetHandshape(HandshapeId.Open);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsFalse(cube.GetComponent<StatefulInteractable>().IsGrabSelected,
"StatefulInteractable did not get un-GrabSelected.");
// Check that the hand cannot interact with any new interactables
var newCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
newCube.transform.position = InputTestUtilities.InFrontOfUser(new Vector3(-3.0f, 0.1f, 1.0f));
newCube.transform.localScale = Vector3.one * 0.1f;
newCube.AddComponent<StatefulInteractable>();
yield return rightHand.MoveTo(newCube.transform.position);
yield return RuntimeTestUtilities.WaitForUpdates();
yield return rightHand.SetHandshape(HandshapeId.Pinch);
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsFalse(newCube.GetComponent<StatefulInteractable>().IsGrabSelected,
"The interactor somehow grabbed the new cube");
yield return rightHand.SetHandshape(HandshapeId.Open);
yield return RuntimeTestUtilities.WaitForUpdates();
// Finish
yield return RuntimeTestUtilities.WaitForUpdates();
yield return rightHand.MoveTo(Vector3.zero);
yield return RuntimeTestUtilities.WaitForUpdates();
yield return rightHand.Show();
yield return RuntimeTestUtilities.WaitForUpdates();
Assert.IsFalse(AnyProximityDetectorsTriggered(),
"ProximityInteractor was still hovering after re-enabling faraway object.");
XRBaseController rightHandController = CachedLookup.RightHandController;
Assert.IsTrue(rightHandController != null, "No controllers found for right hand.");
Assert.IsTrue(rightHandController.GetComponentInChildren<MRTKRayInteractor>().enabled, "Ray didn't reactivate");
Assert.IsTrue(rightHandController.GetComponentInChildren<GazePinchInteractor>().enabled, "GazePinch didn't reactivate");
Assert.IsFalse(rightHandController.GetComponentInChildren<PokeInteractor>().enabled, "Poke didn't deactivate");
Assert.IsFalse(rightHandController.GetComponentInChildren<GrabInteractor>().enabled, "Grab didn't deactivate");
yield return null;
}
// Returns true iff any of the ProximityDetectors in the scene are currently triggered.
private bool AnyProximityDetectorsTriggered()
{
ProximityDetector[] detectors = FindObjectUtility.FindObjectsByType<ProximityDetector>();
foreach (var detector in detectors)
{
if (detector.IsModeDetected())
{
return true;
}
}
return false;
}
}
}
#pragma warning restore CS1591