OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
6 | 6 |
7 import android.hardware.Sensor; | 7 import android.hardware.Sensor; |
8 import android.hardware.SensorEventListener; | 8 import android.hardware.SensorEventListener; |
| 9 import android.hardware.SensorManager; |
9 import android.os.Handler; | 10 import android.os.Handler; |
10 import android.test.AndroidTestCase; | 11 import android.test.AndroidTestCase; |
11 import android.test.suitebuilder.annotation.SmallTest; | 12 import android.test.suitebuilder.annotation.SmallTest; |
12 | 13 |
13 import java.util.HashSet; | 14 import java.util.HashSet; |
14 import java.util.Set; | 15 import java.util.Set; |
15 | 16 |
16 | |
17 /** | 17 /** |
18 * Test suite for DeviceMotionAndOrientation. | 18 * Test suite for DeviceMotionAndOrientation. |
19 */ | 19 */ |
20 public class DeviceMotionAndOrientationTest extends AndroidTestCase { | 20 public class DeviceMotionAndOrientationTest extends AndroidTestCase { |
21 | 21 |
22 private DeviceMotionAndOrientationForTests mDeviceMotionAndOrientation; | 22 private DeviceMotionAndOrientationForTests mDeviceMotionAndOrientation; |
23 private MockSensorManager mMockSensorManager; | 23 private MockSensorManager mMockSensorManager; |
24 | 24 |
25 @Override | 25 @Override |
26 public void setUp() throws Exception { | 26 public void setUp() throws Exception { |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 | 199 |
200 @SmallTest | 200 @SmallTest |
201 public void testSensorChangedmagneticField() { | 201 public void testSensorChangedmagneticField() { |
202 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_O
RIENTATION, 100); | 202 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_O
RIENTATION, 100); |
203 | 203 |
204 float[] values = {1, 2, 3}; | 204 float[] values = {1, 2, 3}; |
205 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_MAGNETIC_FIELD, va
lues); | 205 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_MAGNETIC_FIELD, va
lues); |
206 mDeviceMotionAndOrientation.verifyCalls(""); | 206 mDeviceMotionAndOrientation.verifyCalls(""); |
207 } | 207 } |
208 | 208 |
| 209 // Tests for correct Device Orientation angles. |
| 210 |
| 211 @SmallTest |
| 212 public void testOrientationAnglesFromRotationMatrixIdentity() { |
| 213 float[] gravity = {0, 0, 1}; |
| 214 float[] magnetic = {0, 1, 0}; |
| 215 double[] expectedAngles = {0, 0, 0}; |
| 216 |
| 217 verifyOrientationAngles(gravity, magnetic, expectedAngles); |
| 218 } |
| 219 |
| 220 @SmallTest |
| 221 public void testOrientationAnglesFromRotationMatrix45DegreesX() { |
| 222 float[] gravity = {0, (float)Math.sin(Math.PI / 4), (float)Math.cos(Math
.PI / 4)}; |
| 223 float[] magnetic = {0, 1, 0}; |
| 224 double[] expectedAngles = {0, Math.PI / 4, 0}; |
| 225 |
| 226 verifyOrientationAngles(gravity, magnetic, expectedAngles); |
| 227 } |
| 228 |
| 229 @SmallTest |
| 230 public void testOrientationAnglesFromRotationMatrix45DegreesY() { |
| 231 float[] gravity = {-(float)Math.sin(Math.PI / 4), 0, (float)Math.cos(Mat
h.PI / 4)}; |
| 232 float[] magnetic = {0, 1, 0}; |
| 233 double[] expectedAngles = {0, 0, Math.PI / 4}; |
| 234 |
| 235 verifyOrientationAngles(gravity, magnetic, expectedAngles); |
| 236 } |
| 237 |
| 238 @SmallTest |
| 239 public void testOrientationAnglesFromRotationMatrix45DegreesZ() { |
| 240 float[] gravity = {0, 0, 1}; |
| 241 float[] magnetic = {(float)Math.sin(Math.PI / 4), (float)Math.cos(Math.P
I / 4), 0}; |
| 242 double[] expectedAngles = {Math.PI / 4, 0, 0}; |
| 243 |
| 244 verifyOrientationAngles(gravity, magnetic, expectedAngles); |
| 245 } |
| 246 |
| 247 @SmallTest |
| 248 public void testOrientationAnglesFromRotationMatrixGimbalLock() { |
| 249 float[] gravity = {0, 1, 0}; |
| 250 float[] magnetic = {(float)Math.sin(Math.PI / 4), 0, -(float)Math.cos(Ma
th.PI / 4)}; |
| 251 double[] expectedAngles = {Math.PI / 4, Math.PI / 2, 0}; // favor yaw i
nstead of roll |
| 252 |
| 253 verifyOrientationAngles(gravity, magnetic, expectedAngles); |
| 254 } |
| 255 |
| 256 @SmallTest |
| 257 public void testOrientationAnglesFromRotationMatrixPitchGreaterThan90() { |
| 258 final double largePitchAngle = Math.PI / 2 + Math.PI / 4; |
| 259 float[] gravity = {0, (float)Math.cos(largePitchAngle - Math.PI / 2), |
| 260 -(float)Math.sin(largePitchAngle - Math.PI / 2)}; |
| 261 float[] magnetic = {0, 0, -1}; |
| 262 double[] expectedAngles = {0, largePitchAngle, 0}; |
| 263 |
| 264 verifyOrientationAngles(gravity, magnetic, expectedAngles); |
| 265 } |
| 266 |
| 267 @SmallTest |
| 268 public void testOrientationAnglesFromRotationMatrixRoll90() { |
| 269 float[] gravity = {-1, 0, 0}; |
| 270 float[] magnetic = {0, 1, 0}; |
| 271 double[] expectedAngles = {Math.PI, -Math.PI, -Math.PI / 2}; |
| 272 |
| 273 verifyOrientationAngles(gravity, magnetic, expectedAngles); |
| 274 } |
| 275 |
| 276 /** |
| 277 * Helper method for verifying angles obtained from rotation matrix. |
| 278 * |
| 279 * @param gravity |
| 280 * gravity vector in the device frame |
| 281 * @param magnetic |
| 282 * magnetic field vector in the device frame |
| 283 * @param expectedAngles |
| 284 * expectedAngles[0] rotation angle in radians around the Z-axis |
| 285 * expectedAngles[1] rotation angle in radians around the X-axis |
| 286 * expectedAngles[2] rotation angle in radians around the Y-axis |
| 287 */ |
| 288 private void verifyOrientationAngles(float[] gravity, float[] magnetic, |
| 289 double[] expectedAngles) { |
| 290 float[] R = new float[9]; |
| 291 double[] values = new double[3]; |
| 292 SensorManager.getRotationMatrix(R, null, gravity, magnetic); |
| 293 mDeviceMotionAndOrientation.computeDeviceOrientationFromRotationMatrix(R
, values); |
| 294 |
| 295 assertEquals(expectedAngles.length, values.length); |
| 296 final double epsilon = 0.001; |
| 297 for (int i = 0; i < expectedAngles.length; ++i) { |
| 298 assertEquals(expectedAngles[i], values[i], epsilon); |
| 299 } |
| 300 |
| 301 } |
| 302 |
| 303 // -- End Tests for correct Device Orientation angles. |
| 304 |
209 private static class DeviceMotionAndOrientationForTests extends DeviceMotion
AndOrientation { | 305 private static class DeviceMotionAndOrientationForTests extends DeviceMotion
AndOrientation { |
210 | 306 |
211 private double value1 = 0; | 307 private double value1 = 0; |
212 private double value2 = 0; | 308 private double value2 = 0; |
213 private double value3 = 0; | 309 private double value3 = 0; |
214 private String mCalls = ""; | 310 private String mCalls = ""; |
215 | 311 |
216 private DeviceMotionAndOrientationForTests(){ | 312 private DeviceMotionAndOrientationForTests(){ |
217 } | 313 } |
218 | 314 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
284 numRegistered++; | 380 numRegistered++; |
285 return true; | 381 return true; |
286 } | 382 } |
287 | 383 |
288 @Override | 384 @Override |
289 public void unregisterListener(SensorEventListener listener, int sensorT
ype) { | 385 public void unregisterListener(SensorEventListener listener, int sensorT
ype) { |
290 numUnRegistered++; | 386 numUnRegistered++; |
291 } | 387 } |
292 } | 388 } |
293 } | 389 } |
OLD | NEW |