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: added tests and mocks 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.UiThreadTest;
15 import android.test.InstrumentationTestCase;
16 import android.test.suitebuilder.annotation.SmallTest;
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 */
Miguel Garcia 2013/03/19 15:12:00 Do you need a full instrumentation test for this?
timvolodine 2013/03/19 16:50:08 not sure what else to use in this case, was basing
Miguel Garcia 2013/03/19 17:06:13 It feels that a plain AndroidTestCase would do. O
timvolodine 2013/03/19 17:31:55 ok, that works also -- done. On 2013/03/19 17:06:
33 public class DeviceMotionAndOrientationTest extends InstrumentationTestCase {
34
35 private MockDeviceMotionAndOrientation mDeviceMotionAndOrientation;
36 private MockSensorManager mMockSensorManager;
37
38 @Override
39 public void setUp() throws Exception {
40 super.setUp();
41 mMockSensorManager = new MockSensorManager();
42 mDeviceMotionAndOrientation = MockDeviceMotionAndOrientation.getInstance ();
Miguel Garcia 2013/03/19 15:12:00 line over 80, some more below, please run the clan
bulach 2013/03/19 15:22:36 java is 100cols, so it's fine. :) however, please
timvolodine 2013/03/19 16:50:08 Done.
timvolodine 2013/03/19 16:50:08 Done.
Miguel Garcia 2013/03/19 17:06:13 Ups, my bad On 2013/03/19 15:22:36, bulach wrote:
timvolodine 2013/03/19 17:31:55 Done.
43 mDeviceMotionAndOrientation.setSensorManagerProxy(mMockSensorManager);
44 }
45
46 @SmallTest
47 public void testRegisterSensors_DeviceMotion() {
bulach 2013/03/19 15:22:36 nit: remove the "_DeviceMotion" everywhere, the na
timvolodine 2013/03/19 16:50:08 this refers to testing of sensors related to "devi
48 boolean start = mDeviceMotionAndOrientation.start(0,
49 DeviceMotionAndOrientation.DEVICE_MOTION, 100);
50 assertTrue("should contain all motion sensors",
51 mDeviceMotionAndOrientation.mActiveSensors.containsAll(
52 DeviceMotionAndOrientation.DEVICE_MOTION_SENSORS));
53 assertTrue(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
54 assertFalse(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
55
56 assertEquals(DeviceMotionAndOrientation.DEVICE_MOTION_SENSORS.size(),
57 mMockSensorManager.numRegistered);
58 assertEquals(0, mMockSensorManager.numUnRegistered);
59 }
60
61 @SmallTest
62 public void testRegisterSensors_DeviceOrientation() {
63 boolean start = mDeviceMotionAndOrientation.start(0,
64 DeviceMotionAndOrientation.DEVICE_ORIENTATION, 100);
65 assertTrue("should contain all orientation sensors",
66 mDeviceMotionAndOrientation.mActiveSensors.containsAll(
67 DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS));
68 assertFalse(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
69 assertTrue(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
70
71 assertEquals(DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS.size( ),
72 mMockSensorManager.numRegistered);
73 assertEquals(0, mMockSensorManager.numUnRegistered);
74 }
75
76 @SmallTest
77 public void testRegisterSensors_DeviceMotionAndOrientation() {
78 boolean start = mDeviceMotionAndOrientation.start(0,
79 DeviceMotionAndOrientation.DEVICE_ORIENTATION, 100);
80 boolean start2 = mDeviceMotionAndOrientation.start(0,
81 DeviceMotionAndOrientation.DEVICE_MOTION, 100);
82
83 assertTrue("should contain all motion sensors",
84 mDeviceMotionAndOrientation.mActiveSensors.containsAll(
85 DeviceMotionAndOrientation.DEVICE_MOTION_SENSORS));
86 assertTrue("should contain all orientation sensors",
87 mDeviceMotionAndOrientation.mActiveSensors.containsAll(
88 DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS));
89
90 Set<Integer> union = Sets.newHashSet(DeviceMotionAndOrientation.DEVICE_O RIENTATION_SENSORS);
91 union.addAll(DeviceMotionAndOrientation.DEVICE_MOTION_SENSORS);
92
93 assertEquals(union.size(), mDeviceMotionAndOrientation.mActiveSensors.si ze());
94 assertTrue(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
95 assertTrue(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
96 assertEquals(union.size(), mMockSensorManager.numRegistered);
97 assertEquals(0, mMockSensorManager.numUnRegistered);
98 }
99
100 @SmallTest
101 public void testUnregisterSensors_DeviceMotion() {
102 boolean start = mDeviceMotionAndOrientation.start(0,
103 DeviceMotionAndOrientation.DEVICE_MOTION, 100);
104 mDeviceMotionAndOrientation.stop(DeviceMotionAndOrientation.DEVICE_MOTIO N);
105
106 assertTrue("should contain no sensors",
107 mDeviceMotionAndOrientation.mActiveSensors.isEmpty());
108 assertFalse(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
109 assertFalse(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
110 assertEquals(DeviceMotionAndOrientation.DEVICE_MOTION_SENSORS.size(),
111 mMockSensorManager.numUnRegistered);
112 }
113
114 @SmallTest
115 public void testUnregisterSensors_DeviceOrientation() {
116 boolean start = mDeviceMotionAndOrientation.start(0,
117 DeviceMotionAndOrientation.DEVICE_ORIENTATION, 100);
118 mDeviceMotionAndOrientation.stop(DeviceMotionAndOrientation.DEVICE_ORIEN TATION);
119
120 assertTrue("should contain no sensors",
121 mDeviceMotionAndOrientation.mActiveSensors.isEmpty());
122 assertFalse(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
123 assertFalse(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
124 assertEquals(DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS.size( ),
125 mMockSensorManager.numUnRegistered);
126 }
127
128 @SmallTest
129 public void testUnRegisterSensors_DeviceMotionAndOrientation() {
130 boolean start = mDeviceMotionAndOrientation.start(0,
131 DeviceMotionAndOrientation.DEVICE_ORIENTATION, 100);
132 boolean start2 = mDeviceMotionAndOrientation.start(0,
133 DeviceMotionAndOrientation.DEVICE_MOTION, 100);
134
135 mDeviceMotionAndOrientation.stop(DeviceMotionAndOrientation.DEVICE_MOTIO N);
136
137 assertTrue("should contain all orientation sensors",
138 mDeviceMotionAndOrientation.mActiveSensors.containsAll(
139 DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS));
140
141 Set<Integer> diff = Sets.newHashSet(DeviceMotionAndOrientation.DEVICE_MO TION_SENSORS);
142 diff.removeAll(DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS);
143
144 assertEquals(diff.size(), mMockSensorManager.numUnRegistered);
145
146 mDeviceMotionAndOrientation.stop(DeviceMotionAndOrientation.DEVICE_ORIEN TATION);
147
148 assertTrue("should contain no sensors",
149 mDeviceMotionAndOrientation.mActiveSensors.isEmpty());
150 assertEquals(diff.size() + DeviceMotionAndOrientation.DEVICE_ORIENTATION _SENSORS.size(),
151 mMockSensorManager.numUnRegistered);
152 }
153
154 @SmallTest
155 public void testSensorChanged_gotAccelerationAndOrientation() {
156 boolean startOrientation = mDeviceMotionAndOrientation.start(0,
157 DeviceMotionAndOrientation.DEVICE_ORIENTATION, 100);
158 boolean startMotion = mDeviceMotionAndOrientation.start(0,
159 DeviceMotionAndOrientation.DEVICE_MOTION, 100);
160
161 assertTrue(startOrientation);
162 assertTrue(startMotion);
163 assertTrue(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
164 assertTrue(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
165
166 float[] values = {0.0f, 0.0f, 9.0f};
167 float[] values2 = {10.0f, 10.0f, 10.0f};
168 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_ACCELEROMETER, val ues);
169 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_MAGNETIC_FIELD, va lues2);
170 mDeviceMotionAndOrientation.verifyCalls("gotAccelerationIncludingGravity "+"gotOrientation");
171 mDeviceMotionAndOrientation.verifyValuesEpsilon(45, 0, 0);
172 }
173
174 @SmallTest
175 public void testSensorChanged_gotAccelerationIncludingGravity() {
176 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_M OTION, 100);
177
178 float[] values = {1, 2, 3};
179 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_ACCELEROMETER, val ues);
180 mDeviceMotionAndOrientation.verifyCalls("gotAccelerationIncludingGravity ");
181 mDeviceMotionAndOrientation.verifyValues(1, 2, 3);
182 }
183
184 @SmallTest
185 public void testSensorChanged_gotAcceleration() {
186 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_M OTION, 100);
187
188 float[] values = {1, 2, 3};
189 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_LINEAR_ACCELERATIO N, values);
190 mDeviceMotionAndOrientation.verifyCalls("gotAcceleration");
191 mDeviceMotionAndOrientation.verifyValues(1, 2, 3);
192 }
193
194 @SmallTest
195 public void testSensorChanged_gotRotationRate() {
196 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_M OTION, 100);
197
198 float[] values = {1, 2, 3};
199 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_GYROSCOPE, values) ;
200 mDeviceMotionAndOrientation.verifyCalls("gotRotationRate");
201 mDeviceMotionAndOrientation.verifyValues(1, 2, 3);
202 }
203
204 @SmallTest
205 public void testSensorChanged_magneticField() {
206 mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_O RIENTATION, 100);
207
208 float[] values = {1, 2, 3};
209 mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_MAGNETIC_FIELD, va lues);
210 mDeviceMotionAndOrientation.verifyCalls("");
211 }
212
213 static class MockDeviceMotionAndOrientation extends DeviceMotionAndOrientati on {
214
215 double value1 = 0;
216 double value2 = 0;
217 double value3 = 0;
218 String mCalls = "";
219
220 private MockDeviceMotionAndOrientation(){
221 }
222
223 static MockDeviceMotionAndOrientation getInstance() {
224 return new MockDeviceMotionAndOrientation();
225 }
226
227 void verifyValues(double v1, double v2, double v3) {
228 assertEquals(v1, value1);
229 assertEquals(v2, value2);
230 assertEquals(v3, value3);
231 }
232
233 void verifyValuesEpsilon(double v1, double v2, double v3) {
234 assertEquals(v1, value1, 0.1);
235 assertEquals(v2, value2, 0.1);
236 assertEquals(v3, value3, 0.1);
237 }
238
239 void verifyCalls(String names) {
240 assertEquals(mCalls, names);
241 }
242
243 @Override
244 protected void gotOrientation(double alpha, double beta, double gamma) {
245 value1 = alpha;
246 value2 = beta;
247 value3 = gamma;
248 mCalls = mCalls.concat("gotOrientation");
249 }
250
251 @Override
252 protected void gotAcceleration(double x, double y, double z) {
253 value1 = x;
254 value2 = y;
255 value3 = z;
256 mCalls = mCalls.concat("gotAcceleration");
257 }
258
259 @Override
260 protected void gotAccelerationIncludingGravity(double x, double y, doubl e z) {
261 value1 = x;
262 value2 = y;
263 value3 = z;
264 mCalls = mCalls.concat("gotAccelerationIncludingGravity");
265 }
266
267 @Override
268 protected void gotRotationRate(double alpha, double beta, double gamma) {
269 value1 = alpha;
270 value2 = beta;
271 value3 = gamma;
272 mCalls = mCalls.concat("gotRotationRate");
273 }
274 }
275
276 static class MockSensorManager implements DeviceMotionAndOrientation.SensorM anagerProxy {
Miguel Garcia 2013/03/19 15:12:00 please move this to a separate class. We will end
timvolodine 2013/03/19 16:50:08 not sure if moving to a separate class will simpli
Miguel Garcia 2013/03/19 17:06:13 It's a pity that no mocking system is supported.
timvolodine 2013/03/19 17:31:55 If we move the MockSensorManager to a separate fil
Miguel Garcia 2013/03/19 17:57:22 Alright, I don't feel too strongly about it. On 2
277
278 int numRegistered = 0;
279 int numUnRegistered = 0;
280
281 MockSensorManager() {
282 }
283
284 @Override
285 public List<Sensor> getSensorList(int type) {
286 List<Sensor> s = new ArrayList<Sensor>();
287 s.add(null);
288 return s;
289 }
290
291 @Override
292 public boolean registerListener(SensorEventListener listener, Sensor sen sor, int rate,
293 Handler handler) {
294 numRegistered++;
295 return true;
296 }
297
298 @Override
299 public void unregisterListener(SensorEventListener listener, Sensor sens or) {
300 numUnRegistered++;
301 }
302 }
303 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698