Skip to content

Commit f4b2c9b

Browse files
committed
Update InteractionModeManagerTests.cs
1 parent ce20d98 commit f4b2c9b

File tree

1 file changed

+41
-24
lines changed

1 file changed

+41
-24
lines changed

org.mixedrealitytoolkit.input/Tests/Runtime/InteractionModeManagerTests.cs

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public IEnumerator ProximityDetectorTest()
6868
}
6969

7070
/// <summary>
71-
/// Tests the basic Interaction detector. The controller should enter one mode during hover, another during select, and fall back to the default mode during neither
71+
/// Tests the basic Interaction detector. The controller should enter one mode during hover, another during select, and fall back to the default mode during neither.
7272
/// </summary>
7373
[UnityTest]
7474
public IEnumerator InteractionDetectorTest()
@@ -89,28 +89,42 @@ public IEnumerator InteractionDetectorTest()
8989
yield return rightHand.AimAt(cube.transform.position);
9090
yield return RuntimeTestUtilities.WaitForUpdates();
9191

92-
InteractionMode currentMode = rightHandController.GetComponentInChildren<MRTKRayInteractor>().GetComponent<InteractionDetector>().ModeOnHover;
93-
Assert.AreEqual(currentMode, rightHandController.GetComponentInChildren<MRTKRayInteractor>().GetComponent<InteractionDetector>().ModeOnDetection);
94-
ValidateInteractionModeActive(rightHandController, currentMode);
92+
InteractionDetector interactionDetector = rightHandController.GetComponentInChildren<MRTKRayInteractor>().GetComponent<InteractionDetector>();
93+
94+
InteractionMode expectedMode = interactionDetector.ModeOnHover;
95+
Assert.AreEqual(expectedMode, interactionDetector.ModeOnDetection);
96+
ValidateInteractionModeActive(rightHandController, expectedMode);
9597

98+
// Select the cube and check that we're in the correct mode
9699
yield return rightHand.SetHandshape(HandshapeTypes.HandshapeId.Grab);
97100
yield return RuntimeTestUtilities.WaitForUpdates();
98-
currentMode = rightHandController.GetComponentInChildren<MRTKRayInteractor>().GetComponent<InteractionDetector>().ModeOnSelect;
99-
Assert.AreEqual(currentMode, rightHandController.GetComponentInChildren<MRTKRayInteractor>().GetComponent<InteractionDetector>().ModeOnDetection);
100-
ValidateInteractionModeActive(rightHandController, currentMode);
101+
expectedMode = interactionDetector.ModeOnSelect;
102+
Assert.AreEqual(expectedMode, interactionDetector.ModeOnDetection);
103+
ValidateInteractionModeActive(rightHandController, expectedMode);
101104

102-
// move the hand far away and validate that we are in the default mode
105+
// Release the selection and move the hand far away and validate that we are in the default mode
103106
yield return rightHand.SetHandshape(HandshapeTypes.HandshapeId.Open);
104107
yield return RuntimeTestUtilities.WaitForUpdates();
105-
yield return rightHand.MoveTo(cube.transform.position + new Vector3(3.0f,0,0));
108+
yield return rightHand.MoveTo(cube.transform.position + new Vector3(3.0f, 0, 0));
106109
yield return RuntimeTestUtilities.WaitForUpdates();
110+
expectedMode = InteractionModeManager.Instance.DefaultMode;
111+
ValidateInteractionModeActive(rightHandController, expectedMode);
107112

108-
currentMode = InteractionModeManager.Instance.DefaultMode;
109-
ValidateInteractionModeActive(rightHandController, currentMode);
113+
// Put the hand into a grab state and validate that we are in the default mode, since we're not selecting an object
114+
yield return rightHand.SetHandshape(HandshapeTypes.HandshapeId.Grab);
115+
yield return RuntimeTestUtilities.WaitForUpdates();
116+
expectedMode = InteractionModeManager.Instance.DefaultMode;
117+
ValidateInteractionModeActive(rightHandController, expectedMode);
118+
119+
// Release the grab state and validate that we are in the default mode
120+
yield return rightHand.SetHandshape(HandshapeTypes.HandshapeId.Open);
121+
yield return RuntimeTestUtilities.WaitForUpdates();
122+
expectedMode = InteractionModeManager.Instance.DefaultMode;
123+
ValidateInteractionModeActive(rightHandController, expectedMode);
110124
}
111125

112126
/// <summary>
113-
/// Tests that mode mediation works properly.
127+
/// Tests that mode mediation works properly.
114128
/// </summary>
115129
/// <remarks>
116130
/// The interaction mode with the higher priority should be the valid one which affects the controller.
@@ -136,12 +150,14 @@ public IEnumerator ModeMediationTest()
136150
InputTestUtilities.SetHandAnchorPoint(Handedness.Right, ControllerAnchorPoint.Grab);
137151
yield return RuntimeTestUtilities.WaitForUpdates();
138152

139-
// Moving the hand to a position where it's far ray is hovering over the cube
153+
InteractionDetector rayInteractionDetector = rightHandController.GetComponentInChildren<MRTKRayInteractor>().GetComponent<InteractionDetector>();
154+
155+
// Moving the hand to a position where its far ray is hovering over the cube
140156
yield return rightHand.AimAt(cube.transform.position);
141157
yield return RuntimeTestUtilities.WaitForUpdates();
142-
InteractionMode farRayMode = rightHandController.GetComponentInChildren<MRTKRayInteractor>().GetComponent<InteractionDetector>().ModeOnHover;
158+
InteractionMode farRayMode = rayInteractionDetector.ModeOnHover;
143159
yield return RuntimeTestUtilities.WaitForUpdates();
144-
Assert.AreEqual(farRayMode, rightHandController.GetComponentInChildren<MRTKRayInteractor>().GetComponent<InteractionDetector>().ModeOnDetection);
160+
Assert.AreEqual(farRayMode, rayInteractionDetector.ModeOnDetection);
145161
ValidateInteractionModeActive(rightHandController, farRayMode);
146162

147163
// Now move the hand in range for the proximity detector
@@ -159,8 +175,10 @@ public IEnumerator ModeMediationTest()
159175
yield return rightHand.SetHandshape(HandshapeTypes.HandshapeId.Grab);
160176
yield return RuntimeTestUtilities.WaitForUpdates();
161177

162-
InteractionMode grabMode = rightHandController.GetComponentInChildren<GrabInteractor>().GetComponent<InteractionDetector>().ModeOnSelect;
163-
Assert.AreEqual(grabMode, rightHandController.GetComponentInChildren<GrabInteractor>().GetComponent<InteractionDetector>().ModeOnDetection);
178+
InteractionDetector grabInteractionDetector = rightHandController.GetComponentInChildren<GrabInteractor>().GetComponent<InteractionDetector>();
179+
180+
InteractionMode grabMode = grabInteractionDetector.ModeOnSelect;
181+
Assert.AreEqual(grabMode, grabInteractionDetector.ModeOnDetection);
164182
yield return RuntimeTestUtilities.WaitForUpdates();
165183
ValidateInteractionModeActive(rightHandController, grabMode);
166184
Assert.IsTrue(grabMode.Priority > nearMode.Priority);
@@ -176,7 +194,7 @@ public IEnumerator ModeMediationTest()
176194

177195
// Moving the hand to a position where it's far ray is hovering over the cube
178196
yield return rightHand.MoveTo(cube.transform.position + new Vector3(0.02f, -0.1f, -0.8f));
179-
yield return RuntimeTestUtilities.WaitForUpdates(frameCount:120);
197+
yield return RuntimeTestUtilities.WaitForUpdates(frameCount: 120);
180198

181199
ValidateInteractionModeActive(rightHandController, farRayMode);
182200
}
@@ -189,15 +207,14 @@ public IEnumerator ModeMediationTest()
189207
private void ValidateInteractionModeActive(XRBaseController controller, InteractionMode currentMode)
190208
{
191209
// We construct the list of managed interactor types manually because we don't want to expose the internal controller mapping implementation to even internal use, since
192-
// we don't want any other class to be able to modify those collections without going through the Mode Manager or it's in-editor inspector.
193-
HashSet<System.Type> managedInteractorTypes = new HashSet<System.Type>(InteractionModeManager.Instance.PrioritizedInteractionModes.SelectMany(x => x.AssociatedTypes));
194-
HashSet<System.Type> activeInteractorTypes = InteractionModeManager.Instance.PrioritizedInteractionModes.Find(x => x.ModeName == currentMode.Name).AssociatedTypes;
210+
// we don't want any other class to be able to modify those collections without going through the Mode Manager or its in-editor inspector.
211+
HashSet<Type> managedInteractorTypes = new HashSet<System.Type>(InteractionModeManager.Instance.PrioritizedInteractionModes.SelectMany(x => x.AssociatedTypes));
212+
HashSet<Type> activeInteractorTypes = InteractionModeManager.Instance.PrioritizedInteractionModes.Find(x => x.ModeName == currentMode.Name).AssociatedTypes;
195213

196214
// Ensure the prox detector has actually had the desired effect of enabling/disabling interactors.
197-
foreach (System.Type interactorType in managedInteractorTypes)
215+
foreach (Type interactorType in managedInteractorTypes)
198216
{
199-
XRBaseInteractor interactor = controller.GetComponentInChildren(interactorType) as XRBaseInputInteractor;
200-
if (interactor != null)
217+
if (controller.GetComponentInChildren(interactorType) is XRBaseInputInteractor interactor && interactor != null)
201218
{
202219
Assert.AreEqual(activeInteractorTypes.Contains(interactorType), interactor.enabled);
203220
}

0 commit comments

Comments
 (0)