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

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

Issue 12771008: Implement java-side browser sensor polling for device motion and device orientation DOM events. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixed Marcus's comments Created 7 years, 9 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 (c) 2013 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 org.chromium.base.ActivityStatus;
8 import org.chromium.base.test.util.Feature;
9 import org.chromium.content.app.LibraryLoader;
10 import org.chromium.content.common.CommandLine;
11 import org.chromium.content.common.ProcessInitException;
12 import org.chromium.content_shell_apk.ContentShellApplication;
13
14 import android.test.AndroidTestCase;
15 import android.test.suitebuilder.annotation.SmallTest;
16 import android.test.UiThreadTest;
17 import android.hardware.Sensor;
18 import android.hardware.SensorEvent;
19 import android.hardware.SensorEventListener;
20 import android.hardware.SensorManager;
21 import android.os.Handler;
22
23 import com.google.common.collect.Sets;
24
25 import java.util.ArrayList;
26 import java.util.List;
27 import java.util.Set;
28
29
30 /**
31 * Test suite for DeviceMotionAndOrientation.
32 */
33 public class DeviceMotionAndOrientationTest extends AndroidTestCase {
34
35 private DeviceMotionAndOrientationForTests mDeviceMotionAndOrientation;
36 private MockSensorManager mMockSensorManager;
37
38 @Override
39 public void setUp() throws Exception {
40 super.setUp();
41 mMockSensorManager = new MockSensorManager();
42 mDeviceMotionAndOrientation = DeviceMotionAndOrientationForTests.getInst ance();
43 mDeviceMotionAndOrientation.setSensorManagerProxy(mMockSensorManager);
44 }
45
46 @SmallTest
47 public void testRegisterSensorsDeviceMotion() {
48 boolean start = mDeviceMotionAndOrientation.start(0,
49 DeviceMotionAndOrientation.DEVICE_MOTION, 100);
50
51 assertTrue(start);
52 assertTrue("should contain all motion sensors",
53 mDeviceMotionAndOrientation.mActiveSensors.containsAll(
54 DeviceMotionAndOrientation.DEVICE_MOTION_SENSORS));
55 assertTrue(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
56 assertFalse(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
57
58 assertEquals(DeviceMotionAndOrientation.DEVICE_MOTION_SENSORS.size(),
59 mMockSensorManager.numRegistered);
60 assertEquals(0, mMockSensorManager.numUnRegistered);
61 }
62
63 @SmallTest
64 public void testRegisterSensorsDeviceOrientation() {
65 boolean start = mDeviceMotionAndOrientation.start(0,
66 DeviceMotionAndOrientation.DEVICE_ORIENTATION, 100);
67
68 assertTrue(start);
69 assertTrue("should contain all orientation sensors",
70 mDeviceMotionAndOrientation.mActiveSensors.containsAll(
71 DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS));
72 assertFalse(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
73 assertTrue(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
74
75 assertEquals(DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS.size( ),
76 mMockSensorManager.numRegistered);
77 assertEquals(0, mMockSensorManager.numUnRegistered);
78 }
79
80 @SmallTest
81 public void testRegisterSensorsDeviceMotionAndOrientation() {
82 boolean startOrientation = mDeviceMotionAndOrientation.start(0,
83 DeviceMotionAndOrientation.DEVICE_ORIENTATION, 100);
84 boolean startMotion = mDeviceMotionAndOrientation.start(0,
85 DeviceMotionAndOrientation.DEVICE_MOTION, 100);
86
87 assertTrue(startOrientation);
88 assertTrue(startMotion);
89 assertTrue("should contain all motion sensors",
90 mDeviceMotionAndOrientation.mActiveSensors.containsAll(
91 DeviceMotionAndOrientation.DEVICE_MOTION_SENSORS));
92 assertTrue("should contain all orientation sensors",
93 mDeviceMotionAndOrientation.mActiveSensors.containsAll(
94 DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS));
95
96 Set<Integer> union = Sets.newHashSet(DeviceMotionAndOrientation.DEVICE_O RIENTATION_SENSORS);
97 union.addAll(DeviceMotionAndOrientation.DEVICE_MOTION_SENSORS);
98
99 assertEquals(union.size(), mDeviceMotionAndOrientation.mActiveSensors.si ze());
100 assertTrue(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
101 assertTrue(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
102 assertEquals(union.size(), mMockSensorManager.numRegistered);
103 assertEquals(0, mMockSensorManager.numUnRegistered);
104 }
105
106 @SmallTest
107 public void testUnregisterSensorsDeviceMotion() {
108 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_M OTION, 100);
109 mDeviceMotionAndOrientation.stop(DeviceMotionAndOrientation.DEVICE_MOTIO N);
110
111 assertTrue("should contain no sensors",
112 mDeviceMotionAndOrientation.mActiveSensors.isEmpty());
113 assertFalse(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
114 assertFalse(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
115 assertEquals(DeviceMotionAndOrientation.DEVICE_MOTION_SENSORS.size(),
116 mMockSensorManager.numUnRegistered);
117 }
118
119 @SmallTest
120 public void testUnregisterSensorsDeviceOrientation() {
121 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_O RIENTATION, 100);
122 mDeviceMotionAndOrientation.stop(DeviceMotionAndOrientation.DEVICE_ORIEN TATION);
123
124 assertTrue("should contain no sensors",
125 mDeviceMotionAndOrientation.mActiveSensors.isEmpty());
126 assertFalse(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
127 assertFalse(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
128 assertEquals(DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS.size( ),
129 mMockSensorManager.numUnRegistered);
130 }
131
132 @SmallTest
133 public void testUnRegisterSensorsDeviceMotionAndOrientation() {
134 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_O RIENTATION, 100);
135 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_M OTION, 100);
136 mDeviceMotionAndOrientation.stop(DeviceMotionAndOrientation.DEVICE_MOTIO N);
137
138 assertTrue("should contain all orientation sensors",
139 mDeviceMotionAndOrientation.mActiveSensors.containsAll(
140 DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS));
141
142 Set<Integer> diff = Sets.newHashSet(DeviceMotionAndOrientation.DEVICE_MO TION_SENSORS);
143 diff.removeAll(DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS);
144
145 assertEquals(diff.size(), mMockSensorManager.numUnRegistered);
146
147 mDeviceMotionAndOrientation.stop(DeviceMotionAndOrientation.DEVICE_ORIEN TATION);
148
149 assertTrue("should contain no sensors",
150 mDeviceMotionAndOrientation.mActiveSensors.isEmpty());
151 assertEquals(diff.size() + DeviceMotionAndOrientation.DEVICE_ORIENTATION _SENSORS.size(),
152 mMockSensorManager.numUnRegistered);
153 }
154
155 @SmallTest
156 public void testSensorChangedgotAccelerationAndOrientation() {
157 boolean startOrientation = mDeviceMotionAndOrientation.start(0,
158 DeviceMotionAndOrientation.DEVICE_ORIENTATION, 100);
159 boolean startMotion = mDeviceMotionAndOrientation.start(0,
160 DeviceMotionAndOrientation.DEVICE_MOTION, 100);
161
162 assertTrue(startOrientation);
163 assertTrue(startMotion);
164 assertTrue(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
165 assertTrue(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
166
167 float[] values = {0.0f, 0.0f, 9.0f};
168 float[] values2 = {10.0f, 10.0f, 10.0f};
169 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_ACCELEROMETER, val ues);
170 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_MAGNETIC_FIELD, va lues2);
171 mDeviceMotionAndOrientation.verifyCalls("gotAccelerationIncludingGravity " +
172 "gotOrientation");
173 mDeviceMotionAndOrientation.verifyValuesEpsilon(45, 0, 0);
174 }
175
176 @SmallTest
177 public void testSensorChangedgotAccelerationIncludingGravity() {
178 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_M OTION, 100);
179
180 float[] values = {1, 2, 3};
181 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_ACCELEROMETER, val ues);
182 mDeviceMotionAndOrientation.verifyCalls("gotAccelerationIncludingGravity ");
183 mDeviceMotionAndOrientation.verifyValues(1, 2, 3);
184 }
185
186 @SmallTest
187 public void testSensorChangedgotAcceleration() {
188 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_M OTION, 100);
189
190 float[] values = {1, 2, 3};
191 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_LINEAR_ACCELERATIO N, values);
192 mDeviceMotionAndOrientation.verifyCalls("gotAcceleration");
193 mDeviceMotionAndOrientation.verifyValues(1, 2, 3);
194 }
195
196 @SmallTest
197 public void testSensorChangedgotRotationRate() {
198 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_M OTION, 100);
199
200 float[] values = {1, 2, 3};
201 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_GYROSCOPE, values) ;
202 mDeviceMotionAndOrientation.verifyCalls("gotRotationRate");
203 mDeviceMotionAndOrientation.verifyValues(1, 2, 3);
204 }
205
206 @SmallTest
207 public void testSensorChangedmagneticField() {
208 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_O RIENTATION, 100);
209
210 float[] values = {1, 2, 3};
211 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_MAGNETIC_FIELD, va lues);
212 mDeviceMotionAndOrientation.verifyCalls("");
213 }
214
215 private static class DeviceMotionAndOrientationForTests extends DeviceMotion AndOrientation {
216
217 private double value1 = 0;
218 private double value2 = 0;
219 private double value3 = 0;
220 private String mCalls = "";
221
222 private DeviceMotionAndOrientationForTests(){
223 }
224
225 static DeviceMotionAndOrientationForTests getInstance() {
226 return new DeviceMotionAndOrientationForTests();
227 }
228
229 private void verifyValues(double v1, double v2, double v3) {
230 assertEquals(v1, value1);
231 assertEquals(v2, value2);
232 assertEquals(v3, value3);
233 }
234
235 private void verifyValuesEpsilon(double v1, double v2, double v3) {
236 assertEquals(v1, value1, 0.1);
237 assertEquals(v2, value2, 0.1);
238 assertEquals(v3, value3, 0.1);
239 }
240
241 private void verifyCalls(String names) {
242 assertEquals(mCalls, names);
243 }
244
245 @Override
246 protected void gotOrientation(double alpha, double beta, double gamma) {
247 value1 = alpha;
248 value2 = beta;
249 value3 = gamma;
250 mCalls = mCalls.concat("gotOrientation");
251 }
252
253 @Override
254 protected void gotAcceleration(double x, double y, double z) {
255 value1 = x;
256 value2 = y;
257 value3 = z;
258 mCalls = mCalls.concat("gotAcceleration");
259 }
260
261 @Override
262 protected void gotAccelerationIncludingGravity(double x, double y, doubl e z) {
263 value1 = x;
264 value2 = y;
265 value3 = z;
266 mCalls = mCalls.concat("gotAccelerationIncludingGravity");
267 }
268
269 @Override
270 protected void gotRotationRate(double alpha, double beta, double gamma) {
271 value1 = alpha;
272 value2 = beta;
273 value3 = gamma;
274 mCalls = mCalls.concat("gotRotationRate");
275 }
276 }
277
278 private static class MockSensorManager implements
279 DeviceMotionAndOrientation.SensorManagerProxy {
280
281 private int numRegistered = 0;
282 private int numUnRegistered = 0;
283
284 private MockSensorManager() {
285 }
286
287 @Override
288 public boolean registerListener(SensorEventListener listener, int sensor Type, int rate,
289 Handler handler) {
290 numRegistered++;
291 return true;
292 }
293
294 @Override
295 public void unregisterListener(SensorEventListener listener, int sensorT ype) {
296 numUnRegistered++;
297 }
298 }
299 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698