Index: device/generic_sensor/android/junit/src/org/chromium/device/sensors/PlatformSensorProviderTest.java |
diff --git a/device/generic_sensor/android/junit/src/org/chromium/device/sensors/PlatformSensorProviderTest.java b/device/generic_sensor/android/junit/src/org/chromium/device/sensors/PlatformSensorProviderTest.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..22e73afcb9cfc3a37351f8dfc34067279dcb6c1b |
--- /dev/null |
+++ b/device/generic_sensor/android/junit/src/org/chromium/device/sensors/PlatformSensorProviderTest.java |
@@ -0,0 +1,288 @@ |
+// Copyright 2016 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.device.sensors; |
+ |
+import static org.junit.Assert.assertEquals; |
+import static org.junit.Assert.assertNotNull; |
+import static org.junit.Assert.assertNull; |
+import static org.mockito.ArgumentMatchers.any; |
+import static org.mockito.ArgumentMatchers.anyInt; |
+import static org.mockito.Mockito.doNothing; |
+import static org.mockito.Mockito.doReturn; |
+import static org.mockito.Mockito.mock; |
+import static org.mockito.Mockito.never; |
+import static org.mockito.Mockito.times; |
+import static org.mockito.Mockito.verify; |
+ |
+import android.content.Context; |
+import android.hardware.Sensor; |
+import android.hardware.SensorEvent; |
+import android.hardware.SensorEventListener; |
+import android.hardware.SensorManager; |
+import android.os.Handler; |
+ |
+import org.chromium.base.test.util.Feature; |
+import org.chromium.mojom.device.mojom.ReportingMode; |
+import org.chromium.mojom.device.mojom.SensorReadBuffer; |
+import org.chromium.mojom.device.mojom.SensorType; |
+import org.chromium.testing.local.LocalRobolectricTestRunner; |
+ |
+import org.junit.Before; |
+import org.junit.Test; |
+import org.junit.runner.RunWith; |
+import org.mockito.Mock; |
+import org.mockito.MockitoAnnotations; |
+import org.robolectric.annotation.Config; |
+ |
+import java.lang.reflect.Field; |
+import java.lang.reflect.Modifier; |
+import java.nio.ByteBuffer; |
+import java.nio.ByteOrder; |
+import java.util.ArrayList; |
+import java.util.List; |
+ |
+/** |
+ * Unit tests for PlatformSensorProvider |
+ */ |
+@RunWith(LocalRobolectricTestRunner.class) |
+@Config(manifest = Config.NONE) |
+public class PlatformSensorProviderTest { |
+ @Mock |
+ private Context mContext; |
+ @Mock |
+ private SensorManager mSensorManager; |
+ @Mock |
+ private PlatformSensorProvider mPlatformSensorProvider; |
+ private ByteBuffer mSharedBuffer; |
+ private final List<Sensor> mMockSensors = new ArrayList<Sensor>(); |
+ private static final long PLATFORM_SENSOR_ANDROID = 123456789L; |
+ private static final long PLATFORM_SENSOR_TIMESTAMP = 314159265358979L; |
+ private static final double MILLISECONDS_IN_NANOSECOND = 0.000001d; |
+ |
+ @Before |
+ public void setUp() { |
+ MockitoAnnotations.initMocks(this); |
+ doReturn(mSensorManager).when(mContext).getSystemService(Context.SENSOR_SERVICE); |
+ doReturn(mMockSensors).when(mSensorManager).getSensorList(anyInt()); |
+ doReturn(mSensorManager).when(mPlatformSensorProvider).getSensorManager(); |
+ doReturn(new Handler()).when(mPlatformSensorProvider).getHandler(); |
+ // By default, allow successful registration of SensorEventListeners. |
+ doReturn(true) |
+ .when(mSensorManager) |
+ .registerListener(any(SensorEventListener.class), any(Sensor.class), anyInt(), |
+ any(Handler.class)); |
+ doNothing().when(mPlatformSensorProvider).sensorStarted(any(PlatformSensor.class)); |
+ doNothing().when(mPlatformSensorProvider).sensorStopped(any(PlatformSensor.class)); |
+ } |
+ |
+ @Test |
+ @Feature({"PlatformSensorProvider"}) |
+ public void testNullSensorManager() { |
+ doReturn(null).when(mContext).getSystemService(Context.SENSOR_SERVICE); |
+ PlatformSensorProvider provider = PlatformSensorProvider.create(mContext); |
+ PlatformSensor sensor = provider.createSensor(SensorType.AMBIENT_LIGHT); |
+ assertNull(sensor); |
+ } |
+ |
+ @Test |
+ @Feature({"PlatformSensorProvider"}) |
+ public void testSensorNotSupported() { |
+ PlatformSensorProvider provider = PlatformSensorProvider.create(mContext); |
+ PlatformSensor sensor = provider.createSensor(SensorType.AMBIENT_LIGHT); |
+ assertNull(sensor); |
+ } |
+ |
+ @Test |
+ @Feature({"PlatformSensorProvider"}) |
+ public void testSensorTypeMappings() { |
+ PlatformSensorProvider provider = PlatformSensorProvider.create(mContext); |
+ provider.createSensor(SensorType.AMBIENT_LIGHT); |
+ verify(mSensorManager).getSensorList(Sensor.TYPE_LIGHT); |
+ provider.createSensor(SensorType.ACCELEROMETER); |
+ verify(mSensorManager).getSensorList(Sensor.TYPE_ACCELEROMETER); |
+ provider.createSensor(SensorType.LINEAR_ACCELERATION); |
+ verify(mSensorManager).getSensorList(Sensor.TYPE_LINEAR_ACCELERATION); |
+ provider.createSensor(SensorType.GYROSCOPE); |
+ verify(mSensorManager).getSensorList(Sensor.TYPE_GYROSCOPE); |
+ provider.createSensor(SensorType.MAGNETOMETER); |
+ verify(mSensorManager).getSensorList(Sensor.TYPE_MAGNETIC_FIELD); |
+ } |
+ |
+ @Test |
+ @Feature({"PlatformSensorProvider"}) |
+ public void testSensorSupported() { |
+ PlatformSensor sensor = createPlatformSensor(50000, Sensor.TYPE_LIGHT, |
+ SensorType.AMBIENT_LIGHT, Sensor.REPORTING_MODE_ON_CHANGE); |
+ assertNotNull(sensor); |
+ } |
+ |
+ @Test |
+ @Feature({"PlatformSensor"}) |
+ public void testSensorStartStop() { |
timvolodine
2016/09/06 17:44:37
- can we have a test with multiple sensors?
- e.g.
shalamov
2016/09/07 13:53:42
Done.
|
+ addMockSensor(50000, Sensor.TYPE_ACCELEROMETER, Sensor.REPORTING_MODE_CONTINUOUS); |
+ PlatformSensor sensor = |
+ PlatformSensor.create(Sensor.TYPE_ACCELEROMETER, 3, mPlatformSensorProvider); |
+ assertNotNull(sensor); |
+ |
+ sensor.startSensor(5); |
+ sensor.stopSensor(); |
+ |
+ // Multiple start invocations. |
+ sensor.startSensor(1); |
+ sensor.startSensor(2); |
+ sensor.startSensor(3); |
+ // Same frequency, should not restart sensor |
+ sensor.startSensor(3); |
+ |
+ // Started polling with 5, 1, 2 and 3 Hz frequency. |
+ verify(mPlatformSensorProvider, times(4)).getHandler(); |
+ verify(mPlatformSensorProvider, times(4)).sensorStarted(sensor); |
+ verify(mSensorManager, times(4)) |
+ .registerListener(any(SensorEventListener.class), any(Sensor.class), anyInt(), |
timvolodine
2016/09/06 17:44:37
is it also possible to test that unregisterListene
shalamov
2016/09/07 13:53:42
Done.
|
+ any(Handler.class)); |
+ |
+ sensor.stopSensor(); |
+ sensor.stopSensor(); |
+ verify(mPlatformSensorProvider, times(3)).sensorStopped(sensor); |
+ } |
+ |
+ @Test |
+ @Feature({"PlatformSensor"}) |
+ public void testSensorStartFails() { |
+ addMockSensor(50000, Sensor.TYPE_ACCELEROMETER, Sensor.REPORTING_MODE_CONTINUOUS); |
+ PlatformSensor sensor = |
+ PlatformSensor.create(Sensor.TYPE_ACCELEROMETER, 3, mPlatformSensorProvider); |
+ assertNotNull(sensor); |
+ |
+ doReturn(false) |
+ .when(mSensorManager) |
+ .registerListener(any(SensorEventListener.class), any(Sensor.class), anyInt(), |
+ any(Handler.class)); |
+ |
+ sensor.startSensor(5); |
+ verify(mPlatformSensorProvider, times(1)).sensorStarted(sensor); |
+ verify(mPlatformSensorProvider, times(1)).sensorStopped(sensor); |
+ verify(mPlatformSensorProvider, times(1)).getHandler(); |
+ } |
+ |
+ @Test |
+ @Feature({"PlatformSensor"}) |
+ public void testSensorConfiguration() { |
+ // 5Hz min delay |
+ PlatformSensor sensor = createPlatformSensor(200000, Sensor.TYPE_ACCELEROMETER, |
+ SensorType.ACCELEROMETER, Sensor.REPORTING_MODE_CONTINUOUS); |
+ assertEquals(true, sensor.checkSensorConfiguration(5)); |
+ assertEquals(false, sensor.checkSensorConfiguration(6)); |
+ } |
+ |
+ @Test |
+ @Feature({"PlatformSensor"}) |
+ public void testSensorOnChangeReportingMode() { |
+ PlatformSensor sensor = createPlatformSensor(50000, Sensor.TYPE_LIGHT, |
+ SensorType.AMBIENT_LIGHT, Sensor.REPORTING_MODE_ON_CHANGE); |
+ assertEquals(ReportingMode.ON_CHANGE, sensor.getReportingMode()); |
+ } |
+ |
+ @Test |
+ @Feature({"PlatformSensor"}) |
+ public void testSensorNotifierIsNotCalled() |
+ throws NoSuchFieldException, IllegalAccessException { |
+ PlatformSensor sensor = createPlatformSensor(50000, Sensor.TYPE_ACCELEROMETER, |
+ SensorType.ACCELEROMETER, Sensor.REPORTING_MODE_CONTINUOUS); |
+ PlatformSensorNotifier mockNotifier = mock(PlatformSensorNotifier.class); |
+ initPlatformSensor(sensor, SensorReadBuffer.READ_BUFFER_SIZE, mockNotifier); |
+ SensorEvent event = createFakeEvent(3); |
+ sensor.onSensorChanged(event); |
+ verify(mockNotifier, never()).sensorReadingChanged(); |
+ } |
+ |
+ @Test |
+ @Feature({"PlatformSensor"}) |
+ public void testSensorBufferFromEvent() throws NoSuchFieldException, IllegalAccessException { |
timvolodine
2016/09/06 17:44:37
what does this test? maybe add a comment?
shalamov
2016/09/07 13:53:42
Added comments.
|
+ PlatformSensor sensor = createPlatformSensor(50000, Sensor.TYPE_LIGHT, |
+ SensorType.AMBIENT_LIGHT, Sensor.REPORTING_MODE_ON_CHANGE); |
+ PlatformSensorNotifier mockNotifier = mock(PlatformSensorNotifier.class); |
+ initPlatformSensor(sensor, SensorReadBuffer.READ_BUFFER_SIZE, mockNotifier); |
+ SensorEvent event = createFakeEvent(1); |
+ sensor.onSensorChanged(event); |
+ // System.out.format("Event values[0] %f", event.values[0]); |
timvolodine
2016/09/06 17:44:37
should this be here?
shalamov
2016/09/07 13:53:42
Done.
|
+ verify(mockNotifier, times(1)).sensorReadingChanged(); |
+ // verify timestamp |
+ assertEquals(MILLISECONDS_IN_NANOSECOND * PLATFORM_SENSOR_TIMESTAMP, |
+ mSharedBuffer.getDouble(), MILLISECONDS_IN_NANOSECOND); |
+ // verify illuminance |
+ assertEquals(1.0d + MILLISECONDS_IN_NANOSECOND, mSharedBuffer.getDouble(), |
+ MILLISECONDS_IN_NANOSECOND); |
+ } |
+ |
+ @Test |
+ @Feature({"PlatformSensor"}) |
+ public void testSensorInvalidReadingSize() throws NoSuchFieldException, IllegalAccessException { |
+ PlatformSensor sensor = createPlatformSensor(50000, Sensor.TYPE_ACCELEROMETER, |
+ SensorType.ACCELEROMETER, Sensor.REPORTING_MODE_CONTINUOUS); |
+ PlatformSensorNotifier mockNotifier = mock(PlatformSensorNotifier.class); |
+ initPlatformSensor(sensor, SensorReadBuffer.READ_BUFFER_SIZE, mockNotifier); |
+ // Accelerometer requires 3 reading values x,y and z, create fake event with 1 reading |
+ // value. |
+ SensorEvent event = createFakeEvent(1); |
+ sensor.onSensorChanged(event); |
+ verify(mockNotifier, times(1)).sensorError(); |
+ } |
+ |
+ @Test |
+ @Feature({"PlatformSensor"}) |
+ public void testSensorInvalidBufferSize() throws NoSuchFieldException, IllegalAccessException { |
+ PlatformSensor sensor = createPlatformSensor(50000, Sensor.TYPE_ACCELEROMETER, |
+ SensorType.ACCELEROMETER, Sensor.REPORTING_MODE_CONTINUOUS); |
+ PlatformSensorNotifier mockNotifier = mock(PlatformSensorNotifier.class); |
+ // Create buffer that doesn't have enough capacity to hold sensor reading values. |
+ initPlatformSensor(sensor, 2, mockNotifier); |
+ SensorEvent event = createFakeEvent(3); |
+ sensor.onSensorChanged(event); |
+ verify(mockNotifier, times(1)).sensorError(); |
+ } |
+ |
+ private SensorEvent createFakeEvent(int readingValuesNum) |
+ throws NoSuchFieldException, IllegalAccessException { |
+ SensorEvent mockEvent = mock(SensorEvent.class); |
+ mockEvent.timestamp = PLATFORM_SENSOR_TIMESTAMP; |
+ float values[] = new float[readingValuesNum]; |
+ for (int i = 0; i < readingValuesNum; ++i) { |
+ values[i] = (float) (i + 1.0f + MILLISECONDS_IN_NANOSECOND); |
+ } |
+ |
+ Field valuesField = SensorEvent.class.getDeclaredField("values"); |
+ valuesField.setAccessible(true); |
+ Field modifiersField = Field.class.getDeclaredField("modifiers"); |
+ modifiersField.setAccessible(true); |
+ modifiersField.setInt(valuesField, valuesField.getModifiers() & ~Modifier.FINAL); |
+ valuesField.set(mockEvent, values); |
+ return mockEvent; |
+ } |
+ |
+ private void initPlatformSensor( |
+ PlatformSensor sensor, long readingSize, PlatformSensorNotifier notifier) { |
+ mSharedBuffer = ByteBuffer.allocate((int) readingSize); |
+ mSharedBuffer.order(ByteOrder.nativeOrder()); |
+ sensor.initPlatformSensorAndroid(notifier, PLATFORM_SENSOR_ANDROID, mSharedBuffer); |
+ } |
+ |
+ private void addMockSensor(long minDelayUsec, int sensorType, int reportingMode) { |
+ mMockSensors.clear(); |
+ Sensor mockSensor = mock(Sensor.class); |
+ doReturn((int) minDelayUsec).when(mockSensor).getMinDelay(); |
+ doReturn(reportingMode).when(mockSensor).getReportingMode(); |
+ doReturn(sensorType).when(mockSensor).getType(); |
+ doReturn(mMockSensors).when(mSensorManager).getSensorList(sensorType); |
+ mMockSensors.add(mockSensor); |
+ } |
+ |
+ private PlatformSensor createPlatformSensor( |
+ long minDelayUsec, int androidSensorType, int mojoSensorType, int reportingMode) { |
+ addMockSensor(minDelayUsec, androidSensorType, reportingMode); |
+ PlatformSensorProvider provider = PlatformSensorProvider.create(mContext); |
+ return provider.createSensor(mojoSensorType); |
+ } |
+} |