Tanoda
InteractionButtonTests.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9#if LEAP_TESTS
10
11using NUnit.Framework;
12using System.Collections;
13using UnityEngine;
14using UnityEngine.TestTools;
15
17
18 public class InteractionButtonTests : InteractionEngineTestBase {
19
20 public enum ButtonEventType {
21 OnPress,
22 OnUnpress
23 }
24
25 public enum ButtonActionType {
26 DisableIt,
27 DestroyIt
28 }
29
30 [UnityTest]
31 public IEnumerator TestButtonActionsDuringEvents(
32 [Values(ButtonEventType.OnPress,
33 ButtonEventType.OnUnpress)] ButtonEventType buttonEventType,
34 [Values(ButtonActionType.DisableIt,
35 ButtonActionType.DestroyIt)] ButtonActionType buttonActionType
36 ) {
37
38 yield return wait(beginningTestWait);
39
40 InitTest(PRESS_BUTTON_RIG, DEFAULT_STAGE);
41 recording.Stop(); // Don't play the recording until we're ready!
42
43 // Wait before starting the test.
44 yield return wait(aWhile);
45
46 // Play the button-pressing animation.
47 recording.Play();
48
49 // Create the test action to perform when the event is fired.
50 System.Action buttonAction;
51 switch (buttonActionType) {
52 case ButtonActionType.DestroyIt:
53 buttonAction = () => { GameObject.Destroy(button); }; break;
54 case ButtonActionType.DisableIt:
55 buttonAction = () => { button.gameObject.SetActive(false); }; break;
56 default:
57 throw new System.NotImplementedException("This action is not implemented.");
58 }
59
60 // Schedule the test action when the specified button event fires.
61 bool eventFired = false;
62 System.Action doOnEvent = () => {
63 eventFired = true;
64
65 buttonAction();
66 };
67 switch (buttonEventType) {
68 case ButtonEventType.OnPress:
69 button.OnPress += doOnEvent; break;
70 case ButtonEventType.OnUnpress:
71 button.OnUnpress += doOnEvent; break;
72 default:
73 throw new System.NotImplementedException("This button event is not implemented.");
74 }
75
76 // Wait until the button is pressed.
77 int framesWaited = 0;
78 while (!eventFired && framesWaited < WAIT_FOR_INTERACTION_FRAME_LIMIT) {
79 yield return null;
80 framesWaited++;
81 }
82 Assert.That(framesWaited != WAIT_FOR_INTERACTION_FRAME_LIMIT,
83 "Test recording failed to press the button (fire the event).");
84
85 // If the button was disabled or destroyed, the primary hover lock should not
86 // be engaged.
87 if (buttonActionType == ButtonActionType.DestroyIt
88 || buttonActionType == ButtonActionType.DisableIt) {
89
90 Assert.That(rightHand.primaryHoverLocked == false,
91 "Primary hover lock was active even after the button was disabled "
92 + "or destroyed.");
93 }
94
95 yield return wait(endingTestWait);
96 }
97
98 }
99
100}
101
102#endif // LEAP_TESTS