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

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 comments and trybots 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 testRegisterSensors_DeviceMotion() {
bulach 2013/03/19 18:51:06 ahn, I see the need for the suffix... however, in
timvolodine 2013/03/19 19:35:04 Done.
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 testRegisterSensors_DeviceOrientation() {
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 testRegisterSensors_DeviceMotionAndOrientation() {
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 testUnregisterSensors_DeviceMotion() {
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 testUnregisterSensors_DeviceOrientation() {
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 testUnRegisterSensors_DeviceMotionAndOrientation() {
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 testSensorChanged_gotAccelerationAndOrientation() {
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 "+"gotOrientation");
bulach 2013/03/19 18:51:06 nit: spaces between +
timvolodine 2013/03/19 19:35:04 Done.
172 mDeviceMotionAndOrientation.verifyValuesEpsilon(45, 0, 0);
173 }
174
175 @SmallTest
176 public void testSensorChanged_gotAccelerationIncludingGravity() {
177 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_M OTION, 100);
178
179 float[] values = {1, 2, 3};
180 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_ACCELEROMETER, val ues);
181 mDeviceMotionAndOrientation.verifyCalls("gotAccelerationIncludingGravity ");
182 mDeviceMotionAndOrientation.verifyValues(1, 2, 3);
183 }
184
185 @SmallTest
186 public void testSensorChanged_gotAcceleration() {
187 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_M OTION, 100);
188
189 float[] values = {1, 2, 3};
190 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_LINEAR_ACCELERATIO N, values);
191 mDeviceMotionAndOrientation.verifyCalls("gotAcceleration");
192 mDeviceMotionAndOrientation.verifyValues(1, 2, 3);
193 }
194
195 @SmallTest
196 public void testSensorChanged_gotRotationRate() {
197 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_M OTION, 100);
198
199 float[] values = {1, 2, 3};
200 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_GYROSCOPE, values) ;
201 mDeviceMotionAndOrientation.verifyCalls("gotRotationRate");
202 mDeviceMotionAndOrientation.verifyValues(1, 2, 3);
203 }
204
205 @SmallTest
206 public void testSensorChanged_magneticField() {
207 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_O RIENTATION, 100);
208
209 float[] values = {1, 2, 3};
210 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_MAGNETIC_FIELD, va lues);
211 mDeviceMotionAndOrientation.verifyCalls("");
212 }
213
214 static class DeviceMotionAndOrientationForTests extends DeviceMotionAndOrien tation {
bulach 2013/03/19 18:51:06 won't make much difference, but could mark this an
timvolodine 2013/03/19 19:35:04 Done. However this was not possible in some places
215
216 double value1 = 0;
217 double value2 = 0;
218 double value3 = 0;
219 String mCalls = "";
220
221 private DeviceMotionAndOrientationForTests(){
222 }
223
224 static DeviceMotionAndOrientationForTests getInstance() {
225 return new DeviceMotionAndOrientationForTests();
226 }
227
228 void verifyValues(double v1, double v2, double v3) {
229 assertEquals(v1, value1);
230 assertEquals(v2, value2);
231 assertEquals(v3, value3);
232 }
233
234 void verifyValuesEpsilon(double v1, double v2, double v3) {
235 assertEquals(v1, value1, 0.1);
236 assertEquals(v2, value2, 0.1);
237 assertEquals(v3, value3, 0.1);
238 }
239
240 void verifyCalls(String names) {
241 assertEquals(mCalls, names);
242 }
243
244 @Override
245 protected void gotOrientation(double alpha, double beta, double gamma) {
246 value1 = alpha;
247 value2 = beta;
248 value3 = gamma;
249 mCalls = mCalls.concat("gotOrientation");
250 }
251
252 @Override
253 protected void gotAcceleration(double x, double y, double z) {
254 value1 = x;
255 value2 = y;
256 value3 = z;
257 mCalls = mCalls.concat("gotAcceleration");
258 }
259
260 @Override
261 protected void gotAccelerationIncludingGravity(double x, double y, doubl e z) {
262 value1 = x;
263 value2 = y;
264 value3 = z;
265 mCalls = mCalls.concat("gotAccelerationIncludingGravity");
266 }
267
268 @Override
269 protected void gotRotationRate(double alpha, double beta, double gamma) {
270 value1 = alpha;
271 value2 = beta;
272 value3 = gamma;
273 mCalls = mCalls.concat("gotRotationRate");
274 }
275 }
276
277 static class MockSensorManager implements DeviceMotionAndOrientation.SensorM anagerProxy {
278
279 int numRegistered = 0;
280 int numUnRegistered = 0;
281
282 MockSensorManager() {
283 }
284
285 @Override
286 public boolean registerListener(SensorEventListener listener, int sensor Type, int rate,
287 Handler handler) {
288 numRegistered++;
289 return true;
290 }
291
292 @Override
293 public void unregisterListener(SensorEventListener listener, int sensorT ype) {
294 numUnRegistered++;
295 }
296 }
297 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698