|
| 1 | +// Copyright (c) Mixed Reality Toolkit Contributors |
| 2 | +// Licensed under the BSD 3-Clause |
| 3 | + |
| 4 | +// Disable "missing XML comment" warning for tests. While nice to have, this documentation is not required. |
| 5 | +#pragma warning disable CS1591 |
| 6 | + |
| 7 | +using MixedReality.Toolkit.Core.Tests; |
| 8 | +using MixedReality.Toolkit.Input; |
| 9 | +using MixedReality.Toolkit.Input.Tests; |
| 10 | +using NUnit.Framework; |
| 11 | +using System.Collections; |
| 12 | +using UnityEditor; |
| 13 | +using UnityEngine; |
| 14 | +using UnityEngine.TestTools; |
| 15 | +using UnityEngine.UI; |
| 16 | +using Object = UnityEngine.Object; |
| 17 | + |
| 18 | +namespace MixedReality.Toolkit.UX.Runtime.Tests |
| 19 | +{ |
| 20 | + public class SliderTests : BaseRuntimeInputTests |
| 21 | + { |
| 22 | + // Slider/CanvasSlider.prefab |
| 23 | + private const string DefaultSliderPrefabGuid = "f64620d502cdf0f429efa27703913cb7"; |
| 24 | + private static readonly string DefaultSliderPrefabPath = AssetDatabase.GUIDToAssetPath(DefaultSliderPrefabGuid); |
| 25 | + |
| 26 | + private const int HandMovementFrames = 10; |
| 27 | + |
| 28 | + private TestHand hand; |
| 29 | + |
| 30 | + [SetUp] |
| 31 | + public void SetUp() |
| 32 | + { |
| 33 | + hand = new TestHand(Handedness.Right); |
| 34 | + } |
| 35 | + |
| 36 | + [UnityTest] |
| 37 | + public IEnumerator TouchSlider_MoveRight_ValueIncreasesCorrectly([ValueSource(nameof(MoveRightTestCases))] TestCase testCase) |
| 38 | + { |
| 39 | + InputTestUtilities.InitializeCameraToOriginAndForward(); |
| 40 | + |
| 41 | + var testPrefab = InstantiateSlider(DefaultSliderPrefabPath); |
| 42 | + testPrefab.transform.position = InputTestUtilities.InFrontOfUser(new Vector3(0, 0, 1)); |
| 43 | + |
| 44 | + var slider = testPrefab.GetComponentInChildren<Slider>(); |
| 45 | + slider.MinValue = testCase.MinValue; |
| 46 | + slider.MaxValue = testCase.MaxValue; |
| 47 | + slider.Value = ((testCase.MaxValue - testCase.MinValue) / 2) + testCase.MinValue; |
| 48 | + |
| 49 | + yield return ShowHand(); |
| 50 | + yield return hand.MoveTo(testPrefab.transform.position - new Vector3(0, 0, 0.00f), HandMovementFrames); |
| 51 | + yield return RuntimeTestUtilities.WaitForUpdates(); |
| 52 | + yield return hand.MoveTo(testPrefab.transform.position - new Vector3(-0.04f, 0f, 0.00f), HandMovementFrames); |
| 53 | + yield return RuntimeTestUtilities.WaitForUpdates(); |
| 54 | + Assert.That(slider.Value, Is.EqualTo(testCase.Expected).Within(0.00001f)); |
| 55 | + |
| 56 | + Object.Destroy(testPrefab); |
| 57 | + } |
| 58 | + |
| 59 | + [UnityTest] |
| 60 | + public IEnumerator GrabSlider_MoveRight_ValueIncreasesCorrectly([ValueSource(nameof(MoveRightTestCases))] TestCase testCase) |
| 61 | + { |
| 62 | + InputTestUtilities.InitializeCameraToOriginAndForward(); |
| 63 | + |
| 64 | + var testPrefab = InstantiateSlider(DefaultSliderPrefabPath); |
| 65 | + testPrefab.transform.position = InputTestUtilities.InFrontOfUser(new Vector3(0, 0, 1)); |
| 66 | + |
| 67 | + var slider = testPrefab.GetComponentInChildren<Slider>(); |
| 68 | + slider.IsTouchable = false; |
| 69 | + slider.MinValue = testCase.MinValue; |
| 70 | + slider.MaxValue = testCase.MaxValue; |
| 71 | + slider.Value = ((testCase.MaxValue - testCase.MinValue) / 2) + testCase.MinValue; |
| 72 | + |
| 73 | + yield return ShowHand(); |
| 74 | + yield return hand.MoveTo(testPrefab.transform.position - new Vector3(0, 0, 0.00f), HandMovementFrames); |
| 75 | + yield return RuntimeTestUtilities.WaitForUpdates(); |
| 76 | + yield return hand.SetHandshape(HandshapeTypes.HandshapeId.Grab); |
| 77 | + yield return RuntimeTestUtilities.WaitForUpdates(); |
| 78 | + yield return hand.MoveTo(testPrefab.transform.position - new Vector3(-0.04f, 0f, 0.00f), HandMovementFrames); |
| 79 | + yield return RuntimeTestUtilities.WaitForUpdates(); |
| 80 | + yield return hand.SetHandshape(HandshapeTypes.HandshapeId.Open); |
| 81 | + yield return RuntimeTestUtilities.WaitForUpdates(); |
| 82 | + |
| 83 | + Assert.That(slider.Value, Is.EqualTo(testCase.Expected).Within(0.00001f)); |
| 84 | + |
| 85 | + Object.Destroy(testPrefab); |
| 86 | + } |
| 87 | + [UnityTest] |
| 88 | + public IEnumerator TouchSlider_MoveLeft_ValueIncreasesCorrectly([ValueSource(nameof(MoveLeftTestCases))] TestCase testCase) |
| 89 | + { |
| 90 | + InputTestUtilities.InitializeCameraToOriginAndForward(); |
| 91 | + |
| 92 | + var testPrefab = InstantiateSlider(DefaultSliderPrefabPath); |
| 93 | + testPrefab.transform.position = InputTestUtilities.InFrontOfUser(new Vector3(0, 0, 1)); |
| 94 | + |
| 95 | + var slider = testPrefab.GetComponentInChildren<Slider>(); |
| 96 | + slider.MinValue = testCase.MinValue; |
| 97 | + slider.MaxValue = testCase.MaxValue; |
| 98 | + slider.Value = ((testCase.MaxValue - testCase.MinValue) / 2) + testCase.MinValue; |
| 99 | + |
| 100 | + yield return ShowHand(); |
| 101 | + yield return hand.MoveTo(testPrefab.transform.position - new Vector3(0, 0, 0.00f), HandMovementFrames); |
| 102 | + yield return RuntimeTestUtilities.WaitForUpdates(); |
| 103 | + yield return hand.MoveTo(testPrefab.transform.position + new Vector3(-0.04f, 0f, 0.00f), HandMovementFrames); |
| 104 | + yield return RuntimeTestUtilities.WaitForUpdates(); |
| 105 | + Assert.That(slider.Value, Is.EqualTo(testCase.Expected).Within(0.00001f)); |
| 106 | + |
| 107 | + Object.Destroy(testPrefab); |
| 108 | + } |
| 109 | + |
| 110 | + [UnityTest] |
| 111 | + public IEnumerator GrabSlider_MoveLeft_ValueIncreasesCorrectly([ValueSource(nameof(MoveLeftTestCases))] TestCase testCase) |
| 112 | + { |
| 113 | + InputTestUtilities.InitializeCameraToOriginAndForward(); |
| 114 | + |
| 115 | + var testPrefab = InstantiateSlider(DefaultSliderPrefabPath); |
| 116 | + testPrefab.transform.position = InputTestUtilities.InFrontOfUser(new Vector3(0, 0, 1)); |
| 117 | + |
| 118 | + var slider = testPrefab.GetComponentInChildren<Slider>(); |
| 119 | + slider.IsTouchable = false; |
| 120 | + slider.MinValue = testCase.MinValue; |
| 121 | + slider.MaxValue = testCase.MaxValue; |
| 122 | + slider.Value = ((testCase.MaxValue - testCase.MinValue) / 2) + testCase.MinValue; |
| 123 | + |
| 124 | + yield return ShowHand(); |
| 125 | + yield return hand.MoveTo(testPrefab.transform.position - new Vector3(0, 0, 0.00f), HandMovementFrames); |
| 126 | + yield return RuntimeTestUtilities.WaitForUpdates(); |
| 127 | + yield return hand.SetHandshape(HandshapeTypes.HandshapeId.Grab); |
| 128 | + yield return RuntimeTestUtilities.WaitForUpdates(); |
| 129 | + yield return hand.MoveTo(testPrefab.transform.position + new Vector3(-0.04f, 0f, 0.00f), HandMovementFrames); |
| 130 | + yield return RuntimeTestUtilities.WaitForUpdates(); |
| 131 | + yield return hand.SetHandshape(HandshapeTypes.HandshapeId.Open); |
| 132 | + yield return RuntimeTestUtilities.WaitForUpdates(); |
| 133 | + |
| 134 | + Assert.That(slider.Value, Is.EqualTo(testCase.Expected).Within(0.00001f)); |
| 135 | + |
| 136 | + Object.Destroy(testPrefab); |
| 137 | + } |
| 138 | + |
| 139 | + public readonly struct TestCase |
| 140 | + { |
| 141 | + public float MinValue { get; } |
| 142 | + public float MaxValue { get; } |
| 143 | + public float Expected { get; } |
| 144 | + |
| 145 | + public TestCase(float minValue, float maxValue, float expected) |
| 146 | + { |
| 147 | + MinValue = minValue; |
| 148 | + MaxValue = maxValue; |
| 149 | + Expected = expected; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private static IEnumerable MoveRightTestCases() |
| 154 | + { |
| 155 | + yield return new TestCase(minValue: 0, maxValue: 1, expected: 0.7f); |
| 156 | + yield return new TestCase(minValue: 0, maxValue: 10, expected: 7); |
| 157 | + yield return new TestCase(minValue: 0, maxValue: 0.1f, expected: 0.07f); |
| 158 | + yield return new TestCase(minValue: -1, maxValue: 1, expected: 0.4f); |
| 159 | + yield return new TestCase(minValue: -1, maxValue: 0, expected: -0.3f); |
| 160 | + } |
| 161 | + |
| 162 | + private static IEnumerable MoveLeftTestCases() |
| 163 | + { |
| 164 | + yield return new TestCase(minValue: 0, maxValue: 1, expected: 0.3f); |
| 165 | + yield return new TestCase(minValue: 0, maxValue: 10, expected: 3); |
| 166 | + yield return new TestCase(minValue: 0, maxValue: 0.1f, expected: 0.03f); |
| 167 | + yield return new TestCase(minValue: -1, maxValue: 1, expected: -0.4f); |
| 168 | + yield return new TestCase(minValue: -1, maxValue: 0, expected: -0.7f); |
| 169 | + } |
| 170 | + |
| 171 | + private IEnumerator ShowHand() |
| 172 | + { |
| 173 | + Vector3 initialHandPosition = InputTestUtilities.InFrontOfUser(new Vector3(0.05f, -0.05f, 0.3f)); |
| 174 | + yield return hand.Show(initialHandPosition); |
| 175 | + yield return RuntimeTestUtilities.WaitForUpdates(); |
| 176 | + } |
| 177 | + |
| 178 | + private GameObject InstantiateSlider(string prefabPath) |
| 179 | + { |
| 180 | + GameObject canvas = new GameObject("SliderParent", typeof(RectTransform), typeof(Canvas), typeof(CanvasScaler)); |
| 181 | + canvas.transform.localScale = Vector3.one * 0.001f; |
| 182 | + (canvas.transform as RectTransform).sizeDelta = Vector2.one * 200; |
| 183 | + GameObject slider = Object.Instantiate(AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath)); |
| 184 | + slider.transform.SetParent(canvas.transform, worldPositionStays: false); |
| 185 | + slider.transform.position = Vector3.zero; |
| 186 | + if (slider.transform is RectTransform rectTransform) |
| 187 | + { |
| 188 | + rectTransform.sizeDelta = new Vector2(200f, rectTransform.sizeDelta.y); |
| 189 | + } |
| 190 | + return canvas; |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | +#pragma warning restore CS1591 |
0 commit comments