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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: content/public/android/javatests/src/org/chromium/content/browser/DeviceMotionAndOrientationTest.java
diff --git a/content/public/android/javatests/src/org/chromium/content/browser/DeviceMotionAndOrientationTest.java b/content/public/android/javatests/src/org/chromium/content/browser/DeviceMotionAndOrientationTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..ca680b11f4f3b3ceb26a81c04cc4a653cad88389
--- /dev/null
+++ b/content/public/android/javatests/src/org/chromium/content/browser/DeviceMotionAndOrientationTest.java
@@ -0,0 +1,303 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.content.browser;
+
+import org.chromium.base.ActivityStatus;
+import org.chromium.base.test.util.Feature;
+import org.chromium.content.app.LibraryLoader;
+import org.chromium.content.common.CommandLine;
+import org.chromium.content.common.ProcessInitException;
+import org.chromium.content_shell_apk.ContentShellApplication;
+
+import android.test.UiThreadTest;
+import android.test.InstrumentationTestCase;
+import android.test.suitebuilder.annotation.SmallTest;
+import android.hardware.Sensor;
+import android.hardware.SensorEvent;
+import android.hardware.SensorEventListener;
+import android.hardware.SensorManager;
+import android.os.Handler;
+
+import com.google.common.collect.Sets;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+
+/**
+ * Test suite for DeviceMotionAndOrientation.
+ */
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:
+public class DeviceMotionAndOrientationTest extends InstrumentationTestCase {
+
+ private MockDeviceMotionAndOrientation mDeviceMotionAndOrientation;
+ private MockSensorManager mMockSensorManager;
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ mMockSensorManager = new MockSensorManager();
+ 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.
+ mDeviceMotionAndOrientation.setSensorManagerProxy(mMockSensorManager);
+ }
+
+ @SmallTest
+ 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
+ boolean start = mDeviceMotionAndOrientation.start(0,
+ DeviceMotionAndOrientation.DEVICE_MOTION, 100);
+ assertTrue("should contain all motion sensors",
+ mDeviceMotionAndOrientation.mActiveSensors.containsAll(
+ DeviceMotionAndOrientation.DEVICE_MOTION_SENSORS));
+ assertTrue(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
+ assertFalse(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
+
+ assertEquals(DeviceMotionAndOrientation.DEVICE_MOTION_SENSORS.size(),
+ mMockSensorManager.numRegistered);
+ assertEquals(0, mMockSensorManager.numUnRegistered);
+ }
+
+ @SmallTest
+ public void testRegisterSensors_DeviceOrientation() {
+ boolean start = mDeviceMotionAndOrientation.start(0,
+ DeviceMotionAndOrientation.DEVICE_ORIENTATION, 100);
+ assertTrue("should contain all orientation sensors",
+ mDeviceMotionAndOrientation.mActiveSensors.containsAll(
+ DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS));
+ assertFalse(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
+ assertTrue(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
+
+ assertEquals(DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS.size(),
+ mMockSensorManager.numRegistered);
+ assertEquals(0, mMockSensorManager.numUnRegistered);
+ }
+
+ @SmallTest
+ public void testRegisterSensors_DeviceMotionAndOrientation() {
+ boolean start = mDeviceMotionAndOrientation.start(0,
+ DeviceMotionAndOrientation.DEVICE_ORIENTATION, 100);
+ boolean start2 = mDeviceMotionAndOrientation.start(0,
+ DeviceMotionAndOrientation.DEVICE_MOTION, 100);
+
+ assertTrue("should contain all motion sensors",
+ mDeviceMotionAndOrientation.mActiveSensors.containsAll(
+ DeviceMotionAndOrientation.DEVICE_MOTION_SENSORS));
+ assertTrue("should contain all orientation sensors",
+ mDeviceMotionAndOrientation.mActiveSensors.containsAll(
+ DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS));
+
+ Set<Integer> union = Sets.newHashSet(DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS);
+ union.addAll(DeviceMotionAndOrientation.DEVICE_MOTION_SENSORS);
+
+ assertEquals(union.size(), mDeviceMotionAndOrientation.mActiveSensors.size());
+ assertTrue(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
+ assertTrue(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
+ assertEquals(union.size(), mMockSensorManager.numRegistered);
+ assertEquals(0, mMockSensorManager.numUnRegistered);
+ }
+
+ @SmallTest
+ public void testUnregisterSensors_DeviceMotion() {
+ boolean start = mDeviceMotionAndOrientation.start(0,
+ DeviceMotionAndOrientation.DEVICE_MOTION, 100);
+ mDeviceMotionAndOrientation.stop(DeviceMotionAndOrientation.DEVICE_MOTION);
+
+ assertTrue("should contain no sensors",
+ mDeviceMotionAndOrientation.mActiveSensors.isEmpty());
+ assertFalse(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
+ assertFalse(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
+ assertEquals(DeviceMotionAndOrientation.DEVICE_MOTION_SENSORS.size(),
+ mMockSensorManager.numUnRegistered);
+ }
+
+ @SmallTest
+ public void testUnregisterSensors_DeviceOrientation() {
+ boolean start = mDeviceMotionAndOrientation.start(0,
+ DeviceMotionAndOrientation.DEVICE_ORIENTATION, 100);
+ mDeviceMotionAndOrientation.stop(DeviceMotionAndOrientation.DEVICE_ORIENTATION);
+
+ assertTrue("should contain no sensors",
+ mDeviceMotionAndOrientation.mActiveSensors.isEmpty());
+ assertFalse(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
+ assertFalse(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
+ assertEquals(DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS.size(),
+ mMockSensorManager.numUnRegistered);
+ }
+
+ @SmallTest
+ public void testUnRegisterSensors_DeviceMotionAndOrientation() {
+ boolean start = mDeviceMotionAndOrientation.start(0,
+ DeviceMotionAndOrientation.DEVICE_ORIENTATION, 100);
+ boolean start2 = mDeviceMotionAndOrientation.start(0,
+ DeviceMotionAndOrientation.DEVICE_MOTION, 100);
+
+ mDeviceMotionAndOrientation.stop(DeviceMotionAndOrientation.DEVICE_MOTION);
+
+ assertTrue("should contain all orientation sensors",
+ mDeviceMotionAndOrientation.mActiveSensors.containsAll(
+ DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS));
+
+ Set<Integer> diff = Sets.newHashSet(DeviceMotionAndOrientation.DEVICE_MOTION_SENSORS);
+ diff.removeAll(DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS);
+
+ assertEquals(diff.size(), mMockSensorManager.numUnRegistered);
+
+ mDeviceMotionAndOrientation.stop(DeviceMotionAndOrientation.DEVICE_ORIENTATION);
+
+ assertTrue("should contain no sensors",
+ mDeviceMotionAndOrientation.mActiveSensors.isEmpty());
+ assertEquals(diff.size() + DeviceMotionAndOrientation.DEVICE_ORIENTATION_SENSORS.size(),
+ mMockSensorManager.numUnRegistered);
+ }
+
+ @SmallTest
+ public void testSensorChanged_gotAccelerationAndOrientation() {
+ boolean startOrientation = mDeviceMotionAndOrientation.start(0,
+ DeviceMotionAndOrientation.DEVICE_ORIENTATION, 100);
+ boolean startMotion = mDeviceMotionAndOrientation.start(0,
+ DeviceMotionAndOrientation.DEVICE_MOTION, 100);
+
+ assertTrue(startOrientation);
+ assertTrue(startMotion);
+ assertTrue(mDeviceMotionAndOrientation.mDeviceMotionIsActive);
+ assertTrue(mDeviceMotionAndOrientation.mDeviceOrientationIsActive);
+
+ float[] values = {0.0f, 0.0f, 9.0f};
+ float[] values2 = {10.0f, 10.0f, 10.0f};
+ mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_ACCELEROMETER, values);
+ mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_MAGNETIC_FIELD, values2);
+ mDeviceMotionAndOrientation.verifyCalls("gotAccelerationIncludingGravity"+"gotOrientation");
+ mDeviceMotionAndOrientation.verifyValuesEpsilon(45, 0, 0);
+ }
+
+ @SmallTest
+ public void testSensorChanged_gotAccelerationIncludingGravity() {
+ mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_MOTION, 100);
+
+ float[] values = {1, 2, 3};
+ mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_ACCELEROMETER, values);
+ mDeviceMotionAndOrientation.verifyCalls("gotAccelerationIncludingGravity");
+ mDeviceMotionAndOrientation.verifyValues(1, 2, 3);
+ }
+
+ @SmallTest
+ public void testSensorChanged_gotAcceleration() {
+ mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_MOTION, 100);
+
+ float[] values = {1, 2, 3};
+ mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_LINEAR_ACCELERATION, values);
+ mDeviceMotionAndOrientation.verifyCalls("gotAcceleration");
+ mDeviceMotionAndOrientation.verifyValues(1, 2, 3);
+ }
+
+ @SmallTest
+ public void testSensorChanged_gotRotationRate() {
+ mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_MOTION, 100);
+
+ float[] values = {1, 2, 3};
+ mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_GYROSCOPE, values);
+ mDeviceMotionAndOrientation.verifyCalls("gotRotationRate");
+ mDeviceMotionAndOrientation.verifyValues(1, 2, 3);
+ }
+
+ @SmallTest
+ public void testSensorChanged_magneticField() {
+ mDeviceMotionAndOrientation.start(0, DeviceMotionAndOrientation.DEVICE_ORIENTATION, 100);
+
+ float[] values = {1, 2, 3};
+ mDeviceMotionAndOrientation.sensorChanged(Sensor.TYPE_MAGNETIC_FIELD, values);
+ mDeviceMotionAndOrientation.verifyCalls("");
+ }
+
+ static class MockDeviceMotionAndOrientation extends DeviceMotionAndOrientation {
+
+ double value1 = 0;
+ double value2 = 0;
+ double value3 = 0;
+ String mCalls = "";
+
+ private MockDeviceMotionAndOrientation(){
+ }
+
+ static MockDeviceMotionAndOrientation getInstance() {
+ return new MockDeviceMotionAndOrientation();
+ }
+
+ void verifyValues(double v1, double v2, double v3) {
+ assertEquals(v1, value1);
+ assertEquals(v2, value2);
+ assertEquals(v3, value3);
+ }
+
+ void verifyValuesEpsilon(double v1, double v2, double v3) {
+ assertEquals(v1, value1, 0.1);
+ assertEquals(v2, value2, 0.1);
+ assertEquals(v3, value3, 0.1);
+ }
+
+ void verifyCalls(String names) {
+ assertEquals(mCalls, names);
+ }
+
+ @Override
+ protected void gotOrientation(double alpha, double beta, double gamma) {
+ value1 = alpha;
+ value2 = beta;
+ value3 = gamma;
+ mCalls = mCalls.concat("gotOrientation");
+ }
+
+ @Override
+ protected void gotAcceleration(double x, double y, double z) {
+ value1 = x;
+ value2 = y;
+ value3 = z;
+ mCalls = mCalls.concat("gotAcceleration");
+ }
+
+ @Override
+ protected void gotAccelerationIncludingGravity(double x, double y, double z) {
+ value1 = x;
+ value2 = y;
+ value3 = z;
+ mCalls = mCalls.concat("gotAccelerationIncludingGravity");
+ }
+
+ @Override
+ protected void gotRotationRate(double alpha, double beta, double gamma) {
+ value1 = alpha;
+ value2 = beta;
+ value3 = gamma;
+ mCalls = mCalls.concat("gotRotationRate");
+ }
+ }
+
+ static class MockSensorManager implements DeviceMotionAndOrientation.SensorManagerProxy {
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
+
+ int numRegistered = 0;
+ int numUnRegistered = 0;
+
+ MockSensorManager() {
+ }
+
+ @Override
+ public List<Sensor> getSensorList(int type) {
+ List<Sensor> s = new ArrayList<Sensor>();
+ s.add(null);
+ return s;
+ }
+
+ @Override
+ public boolean registerListener(SensorEventListener listener, Sensor sensor, int rate,
+ Handler handler) {
+ numRegistered++;
+ return true;
+ }
+
+ @Override
+ public void unregisterListener(SensorEventListener listener, Sensor sensor) {
+ numUnRegistered++;
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698