Skip to content

Commit

Permalink
Fix warning and event triggered on disabled StatefulInteractable afte…
Browse files Browse the repository at this point in the history
…r changing speech settings (MixedRealityToolkit#591)

+ Add tests
+ Make VoiceRequiresFocus public
  • Loading branch information
Anonymous committed Jan 9, 2024
1 parent 3707957 commit 1a61719
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
22 changes: 17 additions & 5 deletions org.mixedrealitytoolkit.core/Interactables/StatefulInteractable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,15 @@ public bool AllowSelectByVoice
if (value != allowSelectByVoice)
{
// Unregister and re-register the interactable to update the speech interactor with latest info
interactionManager.UnregisterInteractable(this as IXRInteractable);
if (Application.isPlaying && isActiveAndEnabled)
{
interactionManager.UnregisterInteractable(this as IXRInteractable);
}
allowSelectByVoice = value;
interactionManager.RegisterInteractable(this as IXRInteractable);
if (Application.isPlaying && isActiveAndEnabled)
{
interactionManager.RegisterInteractable(this as IXRInteractable);
}
}
}
}
Expand All @@ -133,9 +139,15 @@ public string SpeechRecognitionKeyword
if (value != speechRecognitionKeyword)
{
// Unregister and re-register the interactable to update the speech interactor with latest info
interactionManager.UnregisterInteractable(this as IXRInteractable);
if (Application.isPlaying && isActiveAndEnabled)
{
interactionManager.UnregisterInteractable(this as IXRInteractable);
}
speechRecognitionKeyword = value;
interactionManager.RegisterInteractable(this as IXRInteractable);
if (Application.isPlaying && isActiveAndEnabled)
{
interactionManager.RegisterInteractable(this as IXRInteractable);
}
}
}
}
Expand All @@ -146,7 +158,7 @@ public string SpeechRecognitionKeyword
/// </summary>
[field: SerializeField, FormerlySerializedAs("voiceRequiresFocus"),
Tooltip("If true, then the voice command will only respond to voice commands while this Interactable has focus.")]
public bool VoiceRequiresFocus { get; private set; } = true;
public bool VoiceRequiresFocus { get; set; } = true;

/// <summary>
/// Does the interactable require the interactor to hover over it?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,72 @@ public IEnumerator TriggerButtonFarInteractionWithMotionController([ValueSource(
}
*/

/// <summary>
/// Tests if changing the button speech recognition settings is possible.
/// </summary>
[UnityTest]
public IEnumerator ChangeSpeechSettings([ValueSource(nameof(PressableButtonsTestPrefabPaths))] string prefabFilename)
{
// instantiate scene and button
GameObject testButton = InstantiateDefaultPressableButton(prefabFilename);

PressableButton button = testButton.GetComponent<PressableButton>();
Assert.IsNotNull(button);

// check default speech recognition keyword value
Assert.AreEqual("select", button.SpeechRecognitionKeyword);
Assert.IsTrue(button.AllowSelectByVoice);
Assert.IsTrue(button.VoiceRequiresFocus);

button.SpeechRecognitionKeyword = "An other speech";
button.AllowSelectByVoice = false;
button.VoiceRequiresFocus = false;

Assert.IsTrue(button.enabled);
Assert.AreEqual("An other speech", button.SpeechRecognitionKeyword);
Assert.IsFalse(button.AllowSelectByVoice);
Assert.IsFalse(button.VoiceRequiresFocus);

yield return null;

Object.Destroy(testButton);
// Wait for a frame to give Unity a chance to actually destroy the object
yield return null;
}

/// <summary>
/// Tests if changing the button speech recognition settings doesn't trigger warning nor event OnClicked when the button is disabled.
/// </summary>
[UnityTest]
public IEnumerator ChangeSpeechSettingsDisabled([ValueSource(nameof(PressableButtonsTestPrefabPaths))] string prefabFilename)
{
// instantiate scene and button
GameObject testButton = InstantiateDefaultPressableButton(prefabFilename);

PressableButton button = testButton.GetComponent<PressableButton>();
Assert.IsNotNull(button);
button.OnClicked.AddListener(() => { throw new System.Exception("OnClicked shouldn't have been triggered!"); });

yield return null; // wait for the button to register at an InteractionManager

button.enabled = false;
button.SpeechRecognitionKeyword = "An other speech";
button.AllowSelectByVoice = false;

Assert.IsFalse(button.enabled);
Assert.AreEqual("An other speech", button.SpeechRecognitionKeyword);
Assert.IsFalse(button.AllowSelectByVoice);
LogAssert.NoUnexpectedReceived(); // assert that XRI has not triggered a warning

yield return PressAndReleaseButtonWithHand(testButton.transform.position + Vector3.forward * 0.1f); // assert that OnClicked wasn't called

yield return null;

Object.Destroy(testButton);
// Wait for a frame to give Unity a chance to actually destroy the object
yield return null;
}
#endregion Tests

#region Private methods
Expand Down

0 comments on commit 1a61719

Please sign in to comment.