1 // Copyright 2014 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.
5 #include "content/browser/device_sensors/sensor_manager_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "content/browser/device_sensors/inertial_sensor_consts.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 class FakeSensorManagerAndroid
: public SensorManagerAndroid
{
19 FakeSensorManagerAndroid() { }
20 virtual ~FakeSensorManagerAndroid() { }
22 virtual int GetNumberActiveDeviceMotionSensors() override
{
23 return number_active_sensors_
;
26 void SetNumberActiveDeviceMotionSensors(int number_active_sensors
) {
27 number_active_sensors_
= number_active_sensors
;
31 virtual bool Start(EventType event_type
) override
{
35 virtual void Stop(EventType event_type
) override
{
39 int number_active_sensors_
;
42 class AndroidSensorManagerTest
: public testing::Test
{
44 AndroidSensorManagerTest() {
45 light_buffer_
.reset(new DeviceLightHardwareBuffer
);
46 motion_buffer_
.reset(new DeviceMotionHardwareBuffer
);
47 orientation_buffer_
.reset(new DeviceOrientationHardwareBuffer
);
50 scoped_ptr
<DeviceLightHardwareBuffer
> light_buffer_
;
51 scoped_ptr
<DeviceMotionHardwareBuffer
> motion_buffer_
;
52 scoped_ptr
<DeviceOrientationHardwareBuffer
> orientation_buffer_
;
55 TEST_F(AndroidSensorManagerTest
, ThreeDeviceMotionSensorsActive
) {
56 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
57 FakeSensorManagerAndroid sensorManager
;
58 sensorManager
.SetNumberActiveDeviceMotionSensors(3);
60 sensorManager
.StartFetchingDeviceMotionData(motion_buffer_
.get());
61 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
63 sensorManager
.GotAcceleration(0, 0, 1, 2, 3);
64 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
65 ASSERT_EQ(1, motion_buffer_
->data
.accelerationX
);
66 ASSERT_TRUE(motion_buffer_
->data
.hasAccelerationX
);
67 ASSERT_EQ(2, motion_buffer_
->data
.accelerationY
);
68 ASSERT_TRUE(motion_buffer_
->data
.hasAccelerationY
);
69 ASSERT_EQ(3, motion_buffer_
->data
.accelerationZ
);
70 ASSERT_TRUE(motion_buffer_
->data
.hasAccelerationZ
);
72 sensorManager
.GotAccelerationIncludingGravity(0, 0, 4, 5, 6);
73 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
74 ASSERT_EQ(4, motion_buffer_
->data
.accelerationIncludingGravityX
);
75 ASSERT_TRUE(motion_buffer_
->data
.hasAccelerationIncludingGravityX
);
76 ASSERT_EQ(5, motion_buffer_
->data
.accelerationIncludingGravityY
);
77 ASSERT_TRUE(motion_buffer_
->data
.hasAccelerationIncludingGravityY
);
78 ASSERT_EQ(6, motion_buffer_
->data
.accelerationIncludingGravityZ
);
79 ASSERT_TRUE(motion_buffer_
->data
.hasAccelerationIncludingGravityZ
);
81 sensorManager
.GotRotationRate(0, 0, 7, 8, 9);
82 ASSERT_TRUE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
83 ASSERT_EQ(7, motion_buffer_
->data
.rotationRateAlpha
);
84 ASSERT_TRUE(motion_buffer_
->data
.hasRotationRateAlpha
);
85 ASSERT_EQ(8, motion_buffer_
->data
.rotationRateBeta
);
86 ASSERT_TRUE(motion_buffer_
->data
.hasRotationRateBeta
);
87 ASSERT_EQ(9, motion_buffer_
->data
.rotationRateGamma
);
88 ASSERT_TRUE(motion_buffer_
->data
.hasRotationRateGamma
);
89 ASSERT_EQ(kInertialSensorIntervalMicroseconds
/ 1000.,
90 motion_buffer_
->data
.interval
);
92 sensorManager
.StopFetchingDeviceMotionData();
93 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
96 TEST_F(AndroidSensorManagerTest
, TwoDeviceMotionSensorsActive
) {
97 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
98 FakeSensorManagerAndroid sensorManager
;
99 sensorManager
.SetNumberActiveDeviceMotionSensors(2);
101 sensorManager
.StartFetchingDeviceMotionData(motion_buffer_
.get());
102 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
104 sensorManager
.GotAcceleration(0, 0, 1, 2, 3);
105 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
107 sensorManager
.GotAccelerationIncludingGravity(0, 0, 1, 2, 3);
108 ASSERT_TRUE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
109 ASSERT_EQ(kInertialSensorIntervalMicroseconds
/ 1000.,
110 motion_buffer_
->data
.interval
);
112 sensorManager
.StopFetchingDeviceMotionData();
113 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
116 TEST_F(AndroidSensorManagerTest
, ZeroDeviceMotionSensorsActive
) {
117 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
118 FakeSensorManagerAndroid sensorManager
;
119 sensorManager
.SetNumberActiveDeviceMotionSensors(0);
121 sensorManager
.StartFetchingDeviceMotionData(motion_buffer_
.get());
122 ASSERT_TRUE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
123 ASSERT_EQ(kInertialSensorIntervalMicroseconds
/ 1000.,
124 motion_buffer_
->data
.interval
);
126 sensorManager
.StopFetchingDeviceMotionData();
127 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
130 TEST_F(AndroidSensorManagerTest
, DeviceOrientationSensorsActive
) {
131 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
132 FakeSensorManagerAndroid sensorManager
;
134 sensorManager
.StartFetchingDeviceOrientationData(orientation_buffer_
.get());
135 ASSERT_FALSE(orientation_buffer_
->data
.allAvailableSensorsAreActive
);
137 sensorManager
.GotOrientation(0, 0, 1, 2, 3);
138 ASSERT_TRUE(orientation_buffer_
->data
.allAvailableSensorsAreActive
);
139 ASSERT_EQ(1, orientation_buffer_
->data
.alpha
);
140 ASSERT_TRUE(orientation_buffer_
->data
.hasAlpha
);
141 ASSERT_EQ(2, orientation_buffer_
->data
.beta
);
142 ASSERT_TRUE(orientation_buffer_
->data
.hasBeta
);
143 ASSERT_EQ(3, orientation_buffer_
->data
.gamma
);
144 ASSERT_TRUE(orientation_buffer_
->data
.hasGamma
);
146 sensorManager
.StopFetchingDeviceOrientationData();
147 ASSERT_FALSE(orientation_buffer_
->data
.allAvailableSensorsAreActive
);
151 TEST_F(AndroidSensorManagerTest
, DeviceLightSensorsActive
) {
152 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
153 FakeSensorManagerAndroid sensorManager
;
155 sensorManager
.StartFetchingDeviceLightData(light_buffer_
.get());
157 sensorManager
.GotLight(0, 0, 100);
158 ASSERT_EQ(100, light_buffer_
->data
.value
);
160 sensorManager
.StopFetchingDeviceLightData();
161 ASSERT_EQ(-1, light_buffer_
->data
.value
);
166 } // namespace content