Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(815)

Side by Side Diff: content/public/android/javatests/src/org/chromium/content/browser/DeviceSensorsTest.java

Issue 2646093002: Move //content/browser/device_sensor/ into device/sensors (Closed)
Patch Set: gn format & code rebase Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 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;
6
7 import android.content.Context;
8 import android.hardware.Sensor;
9 import android.hardware.SensorEventListener;
10 import android.hardware.SensorManager;
11 import android.os.Handler;
12 import android.support.test.filters.SmallTest;
13 import android.test.AndroidTestCase;
14
15 import java.util.HashSet;
16 import java.util.Set;
17
18 /**
19 * Test suite for DeviceSensors.
20 */
21 public class DeviceSensorsTest extends AndroidTestCase {
22
23 private DeviceSensorsForTests mDeviceSensors;
24 private MockSensorManager mMockSensorManager;
25
26 @Override
27 public void setUp() throws Exception {
28 super.setUp();
29 mMockSensorManager = new MockSensorManager();
30 mDeviceSensors = DeviceSensorsForTests.getInstance(getContext());
31 mDeviceSensors.setSensorManagerProxy(mMockSensorManager);
32 }
33
34 @SmallTest
35 public void testRegisterSensorsDeviceMotion() {
36 boolean start = mDeviceSensors.start(0, ConsumerType.MOTION, 100);
37
38 assertTrue(start);
39 assertTrue("should contain all motion sensors",
40 mDeviceSensors.mActiveSensors.containsAll(
41 DeviceSensors.DEVICE_MOTION_SENSORS));
42 assertTrue(mDeviceSensors.mDeviceMotionIsActive);
43 assertFalse(mDeviceSensors.mDeviceOrientationIsActive);
44 assertFalse(mDeviceSensors.mDeviceLightIsActive);
45
46 assertEquals(DeviceSensors.DEVICE_MOTION_SENSORS.size(),
47 mMockSensorManager.mNumRegistered);
48 assertEquals(0, mMockSensorManager.mNumUnRegistered);
49 assertEquals(DeviceSensors.DEVICE_MOTION_SENSORS.size(),
50 mDeviceSensors.getNumberActiveDeviceMotionSensors());
51 }
52
53 @SmallTest
54 public void testRegisterSensorsDeviceOrientation() {
55 boolean start = mDeviceSensors.start(0, ConsumerType.ORIENTATION, 100);
56
57 assertTrue(start);
58 assertTrue("should contain all orientation sensors",
59 mDeviceSensors.mActiveSensors.containsAll(
60 DeviceSensors.DEVICE_ORIENTATION_SENSORS_A));
61 assertFalse(mDeviceSensors.mDeviceMotionIsActive);
62 assertFalse(mDeviceSensors.mDeviceLightIsActive);
63 assertTrue(mDeviceSensors.mDeviceOrientationIsActive);
64 assertFalse(mDeviceSensors.mDeviceOrientationIsActiveWithBackupSensors);
65 assertEquals(OrientationSensorType.GAME_ROTATION_VECTOR,
66 mDeviceSensors.getOrientationSensorTypeUsed());
67
68 assertEquals(DeviceSensors.DEVICE_ORIENTATION_SENSORS_A.size(),
69 mMockSensorManager.mNumRegistered);
70 assertEquals(0, mMockSensorManager.mNumUnRegistered);
71 }
72
73 @SmallTest
74 public void testRegisterSensorsDeviceOrientationGameRotationVectorNotAvailab le() {
75 MockSensorManager mockSensorManager = new MockSensorManager();
76 mockSensorManager.setGameRotationVectorAvailable(false);
77 mDeviceSensors.setSensorManagerProxy(mockSensorManager);
78 boolean startOrientation = mDeviceSensors.start(0, ConsumerType.ORIENTAT ION, 100);
79
80 assertTrue(startOrientation);
81 assertTrue(mDeviceSensors.mDeviceOrientationIsActive);
82 assertFalse(mDeviceSensors.mDeviceOrientationIsActiveWithBackupSensors);
83 assertTrue("should contain option B orientation sensors",
84 mDeviceSensors.mActiveSensors.containsAll(
85 DeviceSensors.DEVICE_ORIENTATION_SENSORS_B));
86 assertEquals(OrientationSensorType.ROTATION_VECTOR,
87 mDeviceSensors.getOrientationSensorTypeUsed());
88
89 assertEquals(DeviceSensors.DEVICE_ORIENTATION_SENSORS_B.size(),
90 mockSensorManager.mNumRegistered);
91 assertEquals(0, mockSensorManager.mNumUnRegistered);
92 }
93
94 @SmallTest
95 public void testRegisterSensorsDeviceOrientationBothRotationVectorsNotAvaila ble() {
96 MockSensorManager mockSensorManager = new MockSensorManager();
97 mockSensorManager.setGameRotationVectorAvailable(false);
98 mockSensorManager.setRotationVectorAvailable(false);
99 mDeviceSensors.setSensorManagerProxy(mockSensorManager);
100 boolean startOrientation = mDeviceSensors.start(0, ConsumerType.ORIENTAT ION, 100);
101
102 assertTrue(startOrientation);
103 assertTrue(mDeviceSensors.mDeviceOrientationIsActive);
104 assertTrue(mDeviceSensors.mDeviceOrientationIsActiveWithBackupSensors);
105 assertTrue("should contain option C orientation sensors",
106 mDeviceSensors.mActiveSensors.containsAll(
107 DeviceSensors.DEVICE_ORIENTATION_SENSORS_C));
108 assertEquals(OrientationSensorType.ACCELEROMETER_MAGNETIC,
109 mDeviceSensors.getOrientationSensorTypeUsed());
110
111 assertEquals(DeviceSensors.DEVICE_ORIENTATION_SENSORS_C.size(),
112 mockSensorManager.mNumRegistered);
113 assertEquals(0, mockSensorManager.mNumUnRegistered);
114 }
115
116 @SmallTest
117 public void testRegisterSensorsDeviceOrientationNoSensorsAvailable() {
118 MockSensorManager mockSensorManager = new MockSensorManager();
119 mockSensorManager.setGameRotationVectorAvailable(false);
120 mockSensorManager.setRotationVectorAvailable(false);
121 mockSensorManager.setAccelerometerAvailable(false);
122 mDeviceSensors.setSensorManagerProxy(mockSensorManager);
123 boolean startOrientation = mDeviceSensors.start(0, ConsumerType.ORIENTAT ION, 100);
124
125 assertFalse(startOrientation);
126 assertFalse(mDeviceSensors.mDeviceOrientationIsActive);
127 assertFalse(mDeviceSensors.mDeviceOrientationIsActiveWithBackupSensors);
128 assertTrue(mDeviceSensors.mActiveSensors.isEmpty());
129 assertEquals(OrientationSensorType.NOT_AVAILABLE,
130 mDeviceSensors.getOrientationSensorTypeUsed());
131
132 assertEquals(0, mockSensorManager.mNumRegistered);
133 assertEquals(0, mockSensorManager.mNumUnRegistered);
134 }
135
136 @SmallTest
137 public void testRegisterSensorsDeviceMotionAndOrientation() {
138 boolean startOrientation = mDeviceSensors.start(0, ConsumerType.ORIENTAT ION, 100);
139 boolean startMotion = mDeviceSensors.start(0, ConsumerType.MOTION, 100);
140
141 assertTrue(startOrientation);
142 assertTrue(startMotion);
143 assertTrue("should contain all motion sensors",
144 mDeviceSensors.mActiveSensors.containsAll(
145 DeviceSensors.DEVICE_MOTION_SENSORS));
146 assertTrue("should contain all orientation sensors",
147 mDeviceSensors.mActiveSensors.containsAll(
148 DeviceSensors.DEVICE_ORIENTATION_SENSORS_A));
149
150 Set<Integer> union = new HashSet<Integer>(DeviceSensors.DEVICE_ORIENTATI ON_SENSORS_A);
151 union.addAll(DeviceSensors.DEVICE_MOTION_SENSORS);
152
153 assertEquals(union.size(), mDeviceSensors.mActiveSensors.size());
154 assertTrue(mDeviceSensors.mDeviceMotionIsActive);
155 assertTrue(mDeviceSensors.mDeviceOrientationIsActive);
156 assertFalse(mDeviceSensors.mDeviceLightIsActive);
157 assertEquals(union.size(), mMockSensorManager.mNumRegistered);
158 assertEquals(0, mMockSensorManager.mNumUnRegistered);
159 assertEquals(DeviceSensors.DEVICE_MOTION_SENSORS.size(),
160 mDeviceSensors.getNumberActiveDeviceMotionSensors());
161 }
162
163 @SmallTest
164 public void testRegisterSensorsDeviceOrientationAbsolute() {
165 boolean start = mDeviceSensors.start(0, ConsumerType.ORIENTATION_ABSOLUT E, 100);
166
167 assertTrue(start);
168 assertTrue("should contain all absolute orientation sensors",
169 mDeviceSensors.mActiveSensors.containsAll(
170 DeviceSensors.DEVICE_ORIENTATION_ABSOLUTE_SENSORS));
171 assertFalse(mDeviceSensors.mDeviceMotionIsActive);
172 assertFalse(mDeviceSensors.mDeviceLightIsActive);
173 assertFalse(mDeviceSensors.mDeviceOrientationIsActive);
174 assertFalse(mDeviceSensors.mDeviceOrientationIsActiveWithBackupSensors);
175 assertTrue(mDeviceSensors.mDeviceOrientationAbsoluteIsActive);
176
177 assertEquals(DeviceSensors.DEVICE_ORIENTATION_ABSOLUTE_SENSORS.size(),
178 mMockSensorManager.mNumRegistered);
179 assertEquals(0, mMockSensorManager.mNumUnRegistered);
180 }
181
182 @SmallTest
183 public void testUnregisterSensorsDeviceOrientationAbsolute() {
184 mDeviceSensors.start(0, ConsumerType.ORIENTATION_ABSOLUTE, 100);
185 mDeviceSensors.stop(ConsumerType.ORIENTATION_ABSOLUTE);
186
187 assertTrue("should contain no sensors",
188 mDeviceSensors.mActiveSensors.isEmpty());
189 assertFalse(mDeviceSensors.mDeviceMotionIsActive);
190 assertFalse(mDeviceSensors.mDeviceOrientationIsActive);
191 assertFalse(mDeviceSensors.mDeviceOrientationIsActiveWithBackupSensors);
192 assertFalse(mDeviceSensors.mDeviceLightIsActive);
193 assertEquals(DeviceSensors.DEVICE_ORIENTATION_ABSOLUTE_SENSORS.size(),
194 mMockSensorManager.mNumUnRegistered);
195 }
196
197 @SmallTest
198 public void testRegisterSensorsDeviceOrientationAndOrientationAbsolute() {
199 boolean startOrientation = mDeviceSensors.start(0, ConsumerType.ORIENTAT ION, 100);
200 boolean startOrientationAbsolute = mDeviceSensors.start(0,
201 ConsumerType.ORIENTATION_ABSOLUTE, 100);
202
203 assertTrue(startOrientation);
204 assertTrue(startOrientationAbsolute);
205 assertTrue("should contain all orientation sensors",
206 mDeviceSensors.mActiveSensors.containsAll(
207 DeviceSensors.DEVICE_ORIENTATION_SENSORS_A));
208 assertTrue("should contain all absolute orientation sensors",
209 mDeviceSensors.mActiveSensors.containsAll(
210 DeviceSensors.DEVICE_ORIENTATION_ABSOLUTE_SENSORS));
211
212 Set<Integer> union = new HashSet<Integer>(DeviceSensors.DEVICE_ORIENTATI ON_SENSORS_A);
213 union.addAll(DeviceSensors.DEVICE_ORIENTATION_ABSOLUTE_SENSORS);
214
215 assertEquals(union.size(), mDeviceSensors.mActiveSensors.size());
216 assertTrue(mDeviceSensors.mDeviceOrientationIsActive);
217 assertTrue(mDeviceSensors.mDeviceOrientationAbsoluteIsActive);
218 assertFalse(mDeviceSensors.mDeviceMotionIsActive);
219 assertFalse(mDeviceSensors.mDeviceLightIsActive);
220 assertEquals(union.size(), mMockSensorManager.mNumRegistered);
221 assertEquals(0, mMockSensorManager.mNumUnRegistered);
222 }
223
224 @SmallTest
225 public void testRegisterSensorsDeviceLight() {
226 boolean start = mDeviceSensors.start(0, ConsumerType.LIGHT, 100);
227
228 assertTrue(start);
229 assertTrue(mDeviceSensors.mDeviceLightIsActive);
230 assertFalse(mDeviceSensors.mDeviceMotionIsActive);
231 assertFalse(mDeviceSensors.mDeviceOrientationIsActive);
232
233 assertEquals(DeviceSensors.DEVICE_LIGHT_SENSORS.size(),
234 mMockSensorManager.mNumRegistered);
235 assertEquals(0, mMockSensorManager.mNumUnRegistered);
236 }
237
238 @SmallTest
239 public void testUnregisterSensorsDeviceMotion() {
240 mDeviceSensors.start(0, ConsumerType.MOTION, 100);
241 mDeviceSensors.stop(ConsumerType.MOTION);
242
243 assertTrue("should contain no sensors",
244 mDeviceSensors.mActiveSensors.isEmpty());
245 assertFalse(mDeviceSensors.mDeviceMotionIsActive);
246 assertFalse(mDeviceSensors.mDeviceOrientationIsActive);
247 assertFalse(mDeviceSensors.mDeviceLightIsActive);
248 assertEquals(DeviceSensors.DEVICE_MOTION_SENSORS.size(),
249 mMockSensorManager.mNumUnRegistered);
250 assertEquals(0, mDeviceSensors.getNumberActiveDeviceMotionSensors());
251 }
252
253 @SmallTest
254 public void testUnregisterSensorsDeviceOrientation() {
255 mDeviceSensors.start(0, ConsumerType.ORIENTATION, 100);
256 mDeviceSensors.stop(ConsumerType.ORIENTATION);
257
258 assertTrue("should contain no sensors",
259 mDeviceSensors.mActiveSensors.isEmpty());
260 assertFalse(mDeviceSensors.mDeviceMotionIsActive);
261 assertFalse(mDeviceSensors.mDeviceOrientationIsActive);
262 assertFalse(mDeviceSensors.mDeviceOrientationIsActiveWithBackupSensors);
263 assertFalse(mDeviceSensors.mDeviceLightIsActive);
264 assertEquals(DeviceSensors.DEVICE_ORIENTATION_SENSORS_A.size(),
265 mMockSensorManager.mNumUnRegistered);
266 }
267
268 @SmallTest
269 public void testUnregisterSensorsDeviceMotionAndOrientation() {
270 mDeviceSensors.start(0, ConsumerType.ORIENTATION, 100);
271 mDeviceSensors.start(0, ConsumerType.MOTION, 100);
272 mDeviceSensors.stop(ConsumerType.MOTION);
273
274 assertTrue("should contain all orientation sensors",
275 mDeviceSensors.mActiveSensors.containsAll(
276 DeviceSensors.DEVICE_ORIENTATION_SENSORS_A));
277
278 Set<Integer> diff = new HashSet<Integer>(DeviceSensors.DEVICE_MOTION_SEN SORS);
279 diff.removeAll(DeviceSensors.DEVICE_ORIENTATION_SENSORS_A);
280
281 assertEquals(diff.size(), mMockSensorManager.mNumUnRegistered);
282
283 mDeviceSensors.stop(ConsumerType.ORIENTATION);
284
285 assertTrue("should contain no sensors", mDeviceSensors.mActiveSensors.is Empty());
286 assertEquals(diff.size() + DeviceSensors.DEVICE_ORIENTATION_SENSORS_A.si ze(),
287 mMockSensorManager.mNumUnRegistered);
288 assertEquals(0, mDeviceSensors.getNumberActiveDeviceMotionSensors());
289 }
290
291 @SmallTest
292 public void testUnregisterSensorsLight() {
293 mDeviceSensors.start(0, ConsumerType.LIGHT, 100);
294 mDeviceSensors.stop(ConsumerType.LIGHT);
295
296 assertTrue("should contain no sensors", mDeviceSensors.mActiveSensors.is Empty());
297 assertFalse(mDeviceSensors.mDeviceMotionIsActive);
298 assertFalse(mDeviceSensors.mDeviceOrientationIsActive);
299 assertFalse(mDeviceSensors.mDeviceLightIsActive);
300 }
301
302 @SmallTest
303 public void testSensorChangedGotLight() {
304 boolean startLight = mDeviceSensors.start(0, ConsumerType.LIGHT, 100);
305
306 assertTrue(startLight);
307 assertTrue(mDeviceSensors.mDeviceLightIsActive);
308
309 float[] values = {200};
310 mDeviceSensors.sensorChanged(Sensor.TYPE_LIGHT, values);
311 mDeviceSensors.verifyCalls("gotLight");
312 mDeviceSensors.verifyValue(200);
313 }
314
315 /**
316 * Helper method to trigger an orientation change using the given sensorType .
317 */
318 private void changeOrientation(int sensorType, boolean absolute, String expe ctedChange) {
319 boolean startOrientation = mDeviceSensors.start(0,
320 absolute ? ConsumerType.ORIENTATION_ABSOLUTE : ConsumerType.ORIE NTATION, 100);
321
322 assertTrue(startOrientation);
323 assertTrue(absolute ? mDeviceSensors.mDeviceOrientationAbsoluteIsActive
324 : mDeviceSensors.mDeviceOrientationIsActive);
325
326 float alpha = (float) Math.PI / 4;
327 float[] values = {0, 0, (float) Math.sin(alpha / 2), (float) Math.cos(al pha / 2), -1};
328 mDeviceSensors.sensorChanged(sensorType, values);
329
330 mDeviceSensors.verifyCalls(expectedChange);
331 if (!expectedChange.isEmpty()) {
332 mDeviceSensors.verifyValuesEpsilon(Math.toDegrees(alpha), 0, 0);
333 }
334 }
335
336 @SmallTest
337 public void testSensorChangedGotOrientationViaRotationVector() {
338 changeOrientation(Sensor.TYPE_ROTATION_VECTOR, false /* absolute */, "") ;
339 }
340
341 @SmallTest
342 public void testSensorChangedGotOrientationViaGameRotationVector() {
343 changeOrientation(Sensor.TYPE_GAME_ROTATION_VECTOR, false /* absolute */ , "gotOrientation");
344 }
345
346 @SmallTest
347 public void testSensorChangedGotOrientationAbsoluteViaRotationVector() {
348 changeOrientation(Sensor.TYPE_ROTATION_VECTOR, true /* absolute */,
349 "gotOrientationAbsolute");
350 }
351
352 @SmallTest
353 public void testSensorChangedGotOrientationAbsoluteViaGameRotationVector() {
354 changeOrientation(Sensor.TYPE_GAME_ROTATION_VECTOR, true /* absolute */, "");
355 }
356
357 @SmallTest
358 public void testSensorChangedGotOrientationAndOrientationAbsolute() {
359 changeOrientation(Sensor.TYPE_GAME_ROTATION_VECTOR, false /* absolute */ , "gotOrientation");
360 changeOrientation(Sensor.TYPE_ROTATION_VECTOR, true /* absolute */,
361 "gotOrientation" + "gotOrientationAbsolute");
362 }
363
364 @SmallTest
365 public void testSensorChangedGotOrientationViaRotationVectorAndOrientationAb solute() {
366 MockSensorManager mockSensorManager = new MockSensorManager();
367 mockSensorManager.setGameRotationVectorAvailable(false);
368 mDeviceSensors.setSensorManagerProxy(mockSensorManager);
369
370 changeOrientation(Sensor.TYPE_ROTATION_VECTOR, false /* absolute */, "go tOrientation");
371 changeOrientation(Sensor.TYPE_ROTATION_VECTOR, true /* absolute */,
372 "gotOrientation" + "gotOrientationAbsolute" + "gotOrientation");
373 }
374
375 @SmallTest
376 public void testSensorChangedGotAccelerationIncludingGravity() {
377 mDeviceSensors.start(0, ConsumerType.MOTION, 100);
378
379 float[] values = {1, 2, 3};
380 mDeviceSensors.sensorChanged(Sensor.TYPE_ACCELEROMETER, values);
381 mDeviceSensors.verifyCalls("gotAccelerationIncludingGravity");
382 mDeviceSensors.verifyValues(1, 2, 3);
383 }
384
385 @SmallTest
386 public void testSensorChangedGotAcceleration() {
387 mDeviceSensors.start(0, ConsumerType.MOTION, 100);
388
389 float[] values = {1, 2, 3};
390 mDeviceSensors.sensorChanged(Sensor.TYPE_LINEAR_ACCELERATION, values);
391 mDeviceSensors.verifyCalls("gotAcceleration");
392 mDeviceSensors.verifyValues(1, 2, 3);
393 }
394
395 @SmallTest
396 public void testSensorChangedGotRotationRate() {
397 mDeviceSensors.start(0, ConsumerType.MOTION, 100);
398
399 float[] values = {1, 2, 3};
400 mDeviceSensors.sensorChanged(Sensor.TYPE_GYROSCOPE, values);
401 mDeviceSensors.verifyCalls("gotRotationRate");
402 mDeviceSensors.verifyValues(1, 2, 3);
403 }
404
405 @SmallTest
406 public void testSensorChangedGotOrientationAndAcceleration() {
407 boolean startOrientation = mDeviceSensors.start(0, ConsumerType.ORIENTAT ION, 100);
408 boolean startMotion = mDeviceSensors.start(0, ConsumerType.MOTION, 100);
409
410 assertTrue(startOrientation);
411 assertTrue(startMotion);
412 assertTrue(mDeviceSensors.mDeviceMotionIsActive);
413 assertTrue(mDeviceSensors.mDeviceOrientationIsActive);
414
415 float alpha = (float) Math.PI / 4;
416 float[] values = {0, 0, (float) Math.sin(alpha / 2), (float) Math.cos(al pha / 2), -1};
417 mDeviceSensors.sensorChanged(Sensor.TYPE_GAME_ROTATION_VECTOR, values);
418 mDeviceSensors.verifyCalls("gotOrientation");
419 mDeviceSensors.verifyValuesEpsilon(Math.toDegrees(alpha), 0, 0);
420
421 float[] values2 = {1, 2, 3};
422 mDeviceSensors.sensorChanged(Sensor.TYPE_ACCELEROMETER, values2);
423 mDeviceSensors.verifyCalls("gotOrientation" + "gotAccelerationIncludingG ravity");
424 mDeviceSensors.verifyValues(1, 2, 3);
425 }
426
427 // Tests for correct Device Orientation angles.
428
429 @SmallTest
430 public void testOrientationAnglesFromRotationMatrixIdentity() {
431 float[] gravity = {0, 0, 1};
432 float[] magnetic = {0, 1, 0};
433 double[] expectedAngles = {0, 0, 0};
434
435 verifyOrientationAngles(gravity, magnetic, expectedAngles);
436 }
437
438 @SmallTest
439 public void testOrientationAnglesFromRotationMatrix45DegreesX() {
440 float[] gravity = {0, (float) Math.sin(Math.PI / 4), (float) Math.cos(Ma th.PI / 4)};
441 float[] magnetic = {0, 1, 0};
442 double[] expectedAngles = {0, Math.PI / 4, 0};
443
444 verifyOrientationAngles(gravity, magnetic, expectedAngles);
445 }
446
447 @SmallTest
448 public void testOrientationAnglesFromRotationMatrix45DegreesY() {
449 float[] gravity = {-(float) Math.sin(Math.PI / 4), 0, (float) Math.cos(M ath.PI / 4)};
450 float[] magnetic = {0, 1, 0};
451 double[] expectedAngles = {0, 0, Math.PI / 4};
452
453 verifyOrientationAngles(gravity, magnetic, expectedAngles);
454 }
455
456 @SmallTest
457 public void testOrientationAnglesFromRotationMatrix45DegreesZ() {
458 float[] gravity = {0, 0, 1};
459 float[] magnetic = {(float) Math.sin(Math.PI / 4), (float) Math.cos(Math .PI / 4), 0};
460 double[] expectedAngles = {Math.PI / 4, 0, 0};
461
462 verifyOrientationAngles(gravity, magnetic, expectedAngles);
463 }
464
465 @SmallTest
466 public void testOrientationAnglesFromRotationMatrixGimbalLock() {
467 float[] gravity = {0, 1, 0};
468 float[] magnetic = {(float) Math.sin(Math.PI / 4), 0, -(float) Math.cos( Math.PI / 4)};
469 double[] expectedAngles = {Math.PI / 4, Math.PI / 2, 0}; // favor yaw i nstead of roll
470
471 verifyOrientationAngles(gravity, magnetic, expectedAngles);
472 }
473
474 @SmallTest
475 public void testOrientationAnglesFromRotationMatrixPitchGreaterThan90() {
476 final double largePitchAngle = Math.PI / 2 + Math.PI / 4;
477 float[] gravity = {0, (float) Math.cos(largePitchAngle - Math.PI / 2),
478 -(float) Math.sin(largePitchAngle - Math.PI / 2)};
479 float[] magnetic = {0, 0, -1};
480 double[] expectedAngles = {0, largePitchAngle, 0};
481
482 verifyOrientationAngles(gravity, magnetic, expectedAngles);
483 }
484
485 @SmallTest
486 public void testOrientationAnglesFromRotationMatrixRoll90() {
487 float[] gravity = {-1, 0, 0};
488 float[] magnetic = {0, 1, 0};
489 double[] expectedAngles = {Math.PI, -Math.PI, -Math.PI / 2};
490
491 verifyOrientationAngles(gravity, magnetic, expectedAngles);
492 }
493
494 /**
495 * Helper method for verifying angles obtained from rotation matrix.
496 *
497 * @param gravity
498 * gravity vector in the device frame
499 * @param magnetic
500 * magnetic field vector in the device frame
501 * @param expectedAngles
502 * expectedAngles[0] rotation angle in radians around the Z-axis
503 * expectedAngles[1] rotation angle in radians around the X-axis
504 * expectedAngles[2] rotation angle in radians around the Y-axis
505 */
506 private void verifyOrientationAngles(float[] gravity, float[] magnetic,
507 double[] expectedAngles) {
508 float[] r = new float[9];
509 double[] values = new double[3];
510 SensorManager.getRotationMatrix(r, null, gravity, magnetic);
511 DeviceSensors.computeDeviceOrientationFromRotationMatrix(r, values);
512
513 assertEquals(expectedAngles.length, values.length);
514 final double epsilon = 0.001;
515 for (int i = 0; i < expectedAngles.length; ++i) {
516 assertEquals(expectedAngles[i], values[i], epsilon);
517 }
518
519 }
520
521 // -- End Tests for correct Device Orientation angles.
522
523 private static class DeviceSensorsForTests extends DeviceSensors {
524
525 private double mValue1 = 0;
526 private double mValue2 = 0;
527 private double mValue3 = 0;
528 private String mCalls = "";
529
530 private DeviceSensorsForTests(Context context) {
531 super(context);
532 }
533
534 static DeviceSensorsForTests getInstance(Context context) {
535 return new DeviceSensorsForTests(context);
536 }
537
538 private void verifyValue(double v1) {
539 assertEquals(v1, mValue1);
540 }
541
542 private void verifyValues(double v1, double v2, double v3) {
543 assertEquals(v1, mValue1);
544 assertEquals(v2, mValue2);
545 assertEquals(v3, mValue3);
546 }
547
548 private void verifyValuesEpsilon(double v1, double v2, double v3) {
549 assertEquals(v1, mValue1, 0.1);
550 assertEquals(v2, mValue2, 0.1);
551 assertEquals(v3, mValue3, 0.1);
552 }
553
554 private void verifyCalls(String names) {
555 assertEquals(mCalls, names);
556 }
557
558 @Override
559 protected void gotLight(double light) {
560 mValue1 = light;
561 mCalls = mCalls.concat("gotLight");
562 }
563
564 @Override
565 protected void gotOrientation(double alpha, double beta, double gamma) {
566 mValue1 = alpha;
567 mValue2 = beta;
568 mValue3 = gamma;
569 mCalls = mCalls.concat("gotOrientation");
570 }
571
572 @Override
573 protected void gotOrientationAbsolute(double alpha, double beta, double gamma) {
574 mValue1 = alpha;
575 mValue2 = beta;
576 mValue3 = gamma;
577 mCalls = mCalls.concat("gotOrientationAbsolute");
578 }
579
580 @Override
581 protected void gotAcceleration(double x, double y, double z) {
582 mValue1 = x;
583 mValue2 = y;
584 mValue3 = z;
585 mCalls = mCalls.concat("gotAcceleration");
586 }
587
588 @Override
589 protected void gotAccelerationIncludingGravity(double x, double y, doubl e z) {
590 mValue1 = x;
591 mValue2 = y;
592 mValue3 = z;
593 mCalls = mCalls.concat("gotAccelerationIncludingGravity");
594 }
595
596 @Override
597 protected void gotRotationRate(double alpha, double beta, double gamma) {
598 mValue1 = alpha;
599 mValue2 = beta;
600 mValue3 = gamma;
601 mCalls = mCalls.concat("gotRotationRate");
602 }
603 }
604
605 private static class MockSensorManager implements DeviceSensors.SensorManage rProxy {
606
607 private int mNumRegistered = 0;
608 private int mNumUnRegistered = 0;
609 private boolean mRotationVectorAvailable = true;
610 private boolean mGameRotationVectorAvailable = true;
611 private boolean mAccelerometerAvailable = true;
612
613 private MockSensorManager() {
614 }
615
616 public void setGameRotationVectorAvailable(boolean available) {
617 mGameRotationVectorAvailable = available;
618 }
619
620 public void setRotationVectorAvailable(boolean available) {
621 mRotationVectorAvailable = available;
622 }
623
624 public void setAccelerometerAvailable(boolean available) {
625 mAccelerometerAvailable = available;
626 }
627
628 private boolean isSensorTypeAvailable(int sensorType) {
629 switch (sensorType) {
630 case Sensor.TYPE_ROTATION_VECTOR : return mRotationVectorAvailab le;
631 case Sensor.TYPE_GAME_ROTATION_VECTOR : return mGameRotationVect orAvailable;
632 case Sensor.TYPE_ACCELEROMETER : return mAccelerometerAvailable;
633 }
634 return true;
635 }
636
637 @Override
638 public boolean registerListener(SensorEventListener listener, int sensor Type, int rate,
639 Handler handler) {
640 if (isSensorTypeAvailable(sensorType)) {
641 mNumRegistered++;
642 return true;
643 }
644 return false;
645 }
646
647 @Override
648 public void unregisterListener(SensorEventListener listener, int sensorT ype) {
649 mNumUnRegistered++;
650 }
651 }
652 }
OLDNEW
« no previous file with comments | « content/public/android/java/src/org/chromium/content/browser/DeviceSensors.java ('k') | content/renderer/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698