OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.content.browser.input; | |
6 | |
7 import android.view.KeyEvent; | |
8 import android.view.MotionEvent; | |
9 | |
10 import org.chromium.base.test.util.Feature; | |
11 import org.chromium.testing.local.LocalRobolectricTestRunner; | |
12 | |
13 import org.junit.Assert; | |
14 import org.junit.Before; | |
15 import org.junit.Test; | |
16 import org.junit.runner.RunWith; | |
17 | |
18 import org.robolectric.annotation.Config; | |
19 | |
20 import java.util.Arrays; | |
21 import java.util.BitSet; | |
22 | |
23 /** | |
24 * Verify no regressions in gamepad mappings. | |
25 */ | |
26 @RunWith(LocalRobolectricTestRunner.class) | |
27 @Config(manifest = Config.NONE) | |
28 public class GamepadMappingsTest { | |
29 private static final float ERROR_TOLERANCE = 0.000001f; | |
30 /** | |
31 * Set bits indicate that we don't expect the button at mMappedButtons[index
] to be mapped. | |
32 */ | |
33 private BitSet mUnmappedButtons = new BitSet(CanonicalButtonIndex.COUNT); | |
34 /** | |
35 * Set bits indicate that we don't expect the axis at mMappedAxes[index] to
be mapped. | |
36 */ | |
37 private BitSet mUnmappedAxes = new BitSet(CanonicalAxisIndex.COUNT); | |
38 private float[] mMappedButtons = new float[CanonicalButtonIndex.COUNT]; | |
39 private float[] mMappedAxes = new float[CanonicalAxisIndex.COUNT]; | |
40 private float[] mRawButtons = new float[GamepadDevice.MAX_RAW_BUTTON_VALUES]
; | |
41 private float[] mRawAxes = new float[GamepadDevice.MAX_RAW_AXIS_VALUES]; | |
42 | |
43 @Before | |
44 public void setUp() throws Exception { | |
45 | |
46 // By default, we expect every button and axis to be mapped. | |
47 mUnmappedButtons.clear(); | |
48 mUnmappedAxes.clear(); | |
49 | |
50 // Start with all the mapped values as unmapped. | |
51 Arrays.fill(mMappedButtons, Float.NaN); | |
52 Arrays.fill(mMappedAxes, Float.NaN); | |
53 | |
54 // Set each raw value to something unique. | |
55 for (int i = 0; i < GamepadDevice.MAX_RAW_AXIS_VALUES; i++) { | |
56 mRawAxes[i] = -i - 1.0f; | |
57 } | |
58 for (int i = 0; i < GamepadDevice.MAX_RAW_BUTTON_VALUES; i++) { | |
59 mRawButtons[i] = i + 1.0f; | |
60 } | |
61 } | |
62 | |
63 @Test | |
64 @Feature({"Gamepad"}) | |
65 public void testShieldGamepadMappings() throws Exception { | |
66 GamepadMappings.mapToStandardGamepad(mMappedAxes, mMappedButtons, mRawAx
es, mRawButtons, | |
67 GamepadMappings.NVIDIA_SHIELD_DEVICE_NAME_PREFIX); | |
68 | |
69 assertShieldGamepadMappings(); | |
70 } | |
71 | |
72 @Test | |
73 @Feature({"Gamepad"}) | |
74 public void testXBox360GamepadMappings() throws Exception { | |
75 GamepadMappings.mapToStandardGamepad(mMappedAxes, mMappedButtons, mRawAx
es, mRawButtons, | |
76 GamepadMappings.MICROSOFT_XBOX_PAD_DEVICE_NAME); | |
77 | |
78 assertShieldGamepadMappings(); | |
79 } | |
80 | |
81 @Test | |
82 @Feature({"Gamepad"}) | |
83 public void testPS3SixAxisGamepadMappings() throws Exception { | |
84 GamepadMappings.mapToStandardGamepad(mMappedAxes, mMappedButtons, mRawAx
es, mRawButtons, | |
85 GamepadMappings.PS3_SIXAXIS_DEVICE_NAME); | |
86 | |
87 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.PRIMARY], | |
88 mRawButtons[KeyEvent.KEYCODE_BUTTON_X], ERROR_TOLERANCE); | |
89 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.SECONDARY], | |
90 mRawButtons[KeyEvent.KEYCODE_BUTTON_Y], ERROR_TOLERANCE); | |
91 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.TERTIARY], | |
92 mRawButtons[KeyEvent.KEYCODE_BUTTON_A], ERROR_TOLERANCE); | |
93 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.QUATERNARY], | |
94 mRawButtons[KeyEvent.KEYCODE_BUTTON_B], ERROR_TOLERANCE); | |
95 | |
96 assertMappedCommonTriggerButtons(); | |
97 assertMappedCommonThumbstickButtons(); | |
98 assertMappedCommonDpadButtons(); | |
99 assertMappedCommonStartSelectMetaButtons(); | |
100 assertMappedTriggerAxexToShoulderButtons(); | |
101 assertMappedXYAxes(); | |
102 assertMappedZAndRZAxesToRightStick(); | |
103 | |
104 assertMapping(); | |
105 } | |
106 | |
107 @Test | |
108 @Feature({"Gamepad"}) | |
109 public void testSamsungEIGP20GamepadMappings() throws Exception { | |
110 GamepadMappings.mapToStandardGamepad(mMappedAxes, mMappedButtons, mRawAx
es, mRawButtons, | |
111 GamepadMappings.SAMSUNG_EI_GP20_DEVICE_NAME); | |
112 | |
113 assertMappedCommonXYABButtons(); | |
114 assertMappedCommonTriggerButtons(); | |
115 assertMappedCommonThumbstickButtons(); | |
116 assertMappedCommonStartSelectMetaButtons(); | |
117 assertMappedHatAxisToDpadButtons(); | |
118 assertMappedXYAxes(); | |
119 assertMappedRXAndRYAxesToRightStick(); | |
120 | |
121 expectNoShoulderButtons(); | |
122 assertMapping(); | |
123 } | |
124 | |
125 @Test | |
126 @Feature({"Gamepad"}) | |
127 public void testAmazonFireGamepadMappings() throws Exception { | |
128 GamepadMappings.mapToStandardGamepad(mMappedAxes, mMappedButtons, mRawAx
es, mRawButtons, | |
129 GamepadMappings.AMAZON_FIRE_DEVICE_NAME); | |
130 | |
131 assertMappedCommonXYABButtons(); | |
132 assertMappedPedalAxesToBottomShoulder(); | |
133 assertMappedCommonThumbstickButtons(); | |
134 assertMappedCommonStartSelectMetaButtons(); | |
135 assertMappedTriggerButtonsToTopShoulder(); | |
136 assertMappedHatAxisToDpadButtons(); | |
137 assertMappedXYAxes(); | |
138 assertMappedZAndRZAxesToRightStick(); | |
139 | |
140 assertMapping(); | |
141 } | |
142 | |
143 @Test | |
144 @Feature({"Gamepad"}) | |
145 public void testUnknownGamepadMappings() throws Exception { | |
146 GamepadMappings.mapToStandardGamepad( | |
147 mMappedAxes, mMappedButtons, mRawAxes, mRawButtons, ""); | |
148 | |
149 assertMappedCommonXYABButtons(); | |
150 assertMappedCommonTriggerButtons(); | |
151 assertMappedCommonThumbstickButtons(); | |
152 assertMappedCommonStartSelectMetaButtons(); | |
153 assertMappedTriggerAxexToShoulderButtons(); | |
154 assertMappedCommonDpadButtons(); | |
155 assertMappedXYAxes(); | |
156 assertMappedRXAndRYAxesToRightStick(); | |
157 | |
158 assertMapping(); | |
159 } | |
160 | |
161 /** | |
162 * Asserts that the current gamepad mapping being tested matches the shield
mappings. | |
163 */ | |
164 public void assertShieldGamepadMappings() { | |
165 assertMappedCommonXYABButtons(); | |
166 assertMappedTriggerButtonsToTopShoulder(); | |
167 assertMappedCommonThumbstickButtons(); | |
168 assertMappedCommonStartSelectMetaButtons(); | |
169 assertMappedTriggerAxesToBottomShoulder(); | |
170 assertMappedHatAxisToDpadButtons(); | |
171 assertMappedXYAxes(); | |
172 assertMappedZAndRZAxesToRightStick(); | |
173 | |
174 assertMapping(); | |
175 } | |
176 | |
177 public void expectNoShoulderButtons() { | |
178 mUnmappedButtons.set(CanonicalButtonIndex.LEFT_SHOULDER); | |
179 mUnmappedButtons.set(CanonicalButtonIndex.RIGHT_SHOULDER); | |
180 } | |
181 | |
182 public void assertMapping() { | |
183 for (int i = 0; i < mMappedAxes.length; i++) { | |
184 if (mUnmappedAxes.get(i)) { | |
185 Assert.assertTrue( | |
186 "An unexpected axis was mapped at index " + i, Float.isN
aN(mMappedAxes[i])); | |
187 } else { | |
188 Assert.assertFalse( | |
189 "An axis was not mapped at index " + i, Float.isNaN(mMap
pedAxes[i])); | |
190 } | |
191 } | |
192 for (int i = 0; i < mMappedButtons.length; i++) { | |
193 if (mUnmappedButtons.get(i)) { | |
194 Assert.assertTrue("An unexpected button was mapped at index " +
i, | |
195 Float.isNaN(mMappedButtons[i])); | |
196 } else { | |
197 Assert.assertFalse( | |
198 "A button was not mapped at index " + i, Float.isNaN(mMa
ppedButtons[i])); | |
199 } | |
200 } | |
201 } | |
202 | |
203 private void assertMappedCommonTriggerButtons() { | |
204 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.LEFT_TRIGGER], | |
205 mRawButtons[KeyEvent.KEYCODE_BUTTON_L1], ERROR_TOLERANCE); | |
206 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.RIGHT_TRIGGER], | |
207 mRawButtons[KeyEvent.KEYCODE_BUTTON_R1], ERROR_TOLERANCE); | |
208 } | |
209 | |
210 private void assertMappedCommonDpadButtons() { | |
211 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.DPAD_DOWN], | |
212 mRawButtons[KeyEvent.KEYCODE_DPAD_DOWN], ERROR_TOLERANCE); | |
213 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.DPAD_UP], | |
214 mRawButtons[KeyEvent.KEYCODE_DPAD_UP], ERROR_TOLERANCE); | |
215 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.DPAD_LEFT], | |
216 mRawButtons[KeyEvent.KEYCODE_DPAD_LEFT], ERROR_TOLERANCE); | |
217 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.DPAD_RIGHT], | |
218 mRawButtons[KeyEvent.KEYCODE_DPAD_RIGHT], ERROR_TOLERANCE); | |
219 } | |
220 | |
221 private void assertMappedTriggerAxexToShoulderButtons() { | |
222 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.LEFT_SHOULDER], | |
223 mRawAxes[MotionEvent.AXIS_LTRIGGER], ERROR_TOLERANCE); | |
224 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.RIGHT_SHOULDER], | |
225 mRawAxes[MotionEvent.AXIS_RTRIGGER], ERROR_TOLERANCE); | |
226 } | |
227 | |
228 private void assertMappedTriggerButtonsToTopShoulder() { | |
229 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.LEFT_SHOULDER], | |
230 mRawButtons[KeyEvent.KEYCODE_BUTTON_L1], ERROR_TOLERANCE); | |
231 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.RIGHT_SHOULDER], | |
232 mRawButtons[KeyEvent.KEYCODE_BUTTON_R1], ERROR_TOLERANCE); | |
233 } | |
234 | |
235 private void assertMappedCommonXYABButtons() { | |
236 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.PRIMARY], | |
237 mRawButtons[KeyEvent.KEYCODE_BUTTON_A], ERROR_TOLERANCE); | |
238 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.SECONDARY], | |
239 mRawButtons[KeyEvent.KEYCODE_BUTTON_B], ERROR_TOLERANCE); | |
240 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.TERTIARY], | |
241 mRawButtons[KeyEvent.KEYCODE_BUTTON_X], ERROR_TOLERANCE); | |
242 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.QUATERNARY], | |
243 mRawButtons[KeyEvent.KEYCODE_BUTTON_Y], ERROR_TOLERANCE); | |
244 } | |
245 | |
246 private void assertMappedCommonThumbstickButtons() { | |
247 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.LEFT_THUMBSTICK]
, | |
248 mRawButtons[KeyEvent.KEYCODE_BUTTON_THUMBL], ERROR_TOLERANCE); | |
249 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.RIGHT_THUMBSTICK
], | |
250 mRawButtons[KeyEvent.KEYCODE_BUTTON_THUMBR], ERROR_TOLERANCE); | |
251 } | |
252 | |
253 private void assertMappedCommonStartSelectMetaButtons() { | |
254 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.START], | |
255 mRawButtons[KeyEvent.KEYCODE_BUTTON_START], ERROR_TOLERANCE); | |
256 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.BACK_SELECT], | |
257 mRawButtons[KeyEvent.KEYCODE_BUTTON_SELECT], ERROR_TOLERANCE); | |
258 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.META], | |
259 mRawButtons[KeyEvent.KEYCODE_BUTTON_MODE], ERROR_TOLERANCE); | |
260 } | |
261 | |
262 private void assertMappedPedalAxesToBottomShoulder() { | |
263 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.LEFT_TRIGGER], | |
264 mRawAxes[MotionEvent.AXIS_BRAKE], ERROR_TOLERANCE); | |
265 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.RIGHT_TRIGGER], | |
266 mRawAxes[MotionEvent.AXIS_GAS], ERROR_TOLERANCE); | |
267 } | |
268 | |
269 private void assertMappedTriggerAxesToBottomShoulder() { | |
270 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.LEFT_TRIGGER], | |
271 mRawAxes[MotionEvent.AXIS_LTRIGGER], ERROR_TOLERANCE); | |
272 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.RIGHT_TRIGGER], | |
273 mRawAxes[MotionEvent.AXIS_RTRIGGER], ERROR_TOLERANCE); | |
274 } | |
275 | |
276 private void assertMappedHatAxisToDpadButtons() { | |
277 float hatX = mRawAxes[MotionEvent.AXIS_HAT_X]; | |
278 float hatY = mRawAxes[MotionEvent.AXIS_HAT_Y]; | |
279 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.DPAD_LEFT], | |
280 GamepadMappings.negativeAxisValueAsButton(hatX), ERROR_TOLERANCE
); | |
281 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.DPAD_RIGHT], | |
282 GamepadMappings.positiveAxisValueAsButton(hatX), ERROR_TOLERANCE
); | |
283 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.DPAD_UP], | |
284 GamepadMappings.negativeAxisValueAsButton(hatY), ERROR_TOLERANCE
); | |
285 Assert.assertEquals(mMappedButtons[CanonicalButtonIndex.DPAD_DOWN], | |
286 GamepadMappings.positiveAxisValueAsButton(hatY), ERROR_TOLERANCE
); | |
287 } | |
288 | |
289 private void assertMappedXYAxes() { | |
290 Assert.assertEquals(mMappedAxes[CanonicalAxisIndex.LEFT_STICK_X], | |
291 mRawAxes[MotionEvent.AXIS_X], ERROR_TOLERANCE); | |
292 Assert.assertEquals(mMappedAxes[CanonicalAxisIndex.LEFT_STICK_Y], | |
293 mRawAxes[MotionEvent.AXIS_Y], ERROR_TOLERANCE); | |
294 } | |
295 | |
296 private void assertMappedRXAndRYAxesToRightStick() { | |
297 Assert.assertEquals(mMappedAxes[CanonicalAxisIndex.RIGHT_STICK_X], | |
298 mRawAxes[MotionEvent.AXIS_RX], ERROR_TOLERANCE); | |
299 Assert.assertEquals(mMappedAxes[CanonicalAxisIndex.RIGHT_STICK_Y], | |
300 mRawAxes[MotionEvent.AXIS_RY], ERROR_TOLERANCE); | |
301 } | |
302 | |
303 private void assertMappedZAndRZAxesToRightStick() { | |
304 Assert.assertEquals(mMappedAxes[CanonicalAxisIndex.RIGHT_STICK_X], | |
305 mRawAxes[MotionEvent.AXIS_Z], ERROR_TOLERANCE); | |
306 Assert.assertEquals(mMappedAxes[CanonicalAxisIndex.RIGHT_STICK_Y], | |
307 mRawAxes[MotionEvent.AXIS_RZ], ERROR_TOLERANCE); | |
308 } | |
309 } | |
OLD | NEW |