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 "content/public/test/test_browser_thread_bundle.h"
12 #include "testing/gtest/include/gtest/gtest.h"
18 class FakeSensorManagerAndroid
: public SensorManagerAndroid
{
20 FakeSensorManagerAndroid() {}
21 ~FakeSensorManagerAndroid() override
{}
23 int GetNumberActiveDeviceMotionSensors() override
{
24 return number_active_sensors_
;
27 void SetNumberActiveDeviceMotionSensors(int number_active_sensors
) {
28 number_active_sensors_
= number_active_sensors
;
32 bool Start(EventType event_type
) override
{ return true; }
33 void Stop(EventType event_type
) override
{}
36 int number_active_sensors_
;
39 class AndroidSensorManagerTest
: public testing::Test
{
41 AndroidSensorManagerTest() {
42 light_buffer_
.reset(new DeviceLightHardwareBuffer
);
43 motion_buffer_
.reset(new DeviceMotionHardwareBuffer
);
44 orientation_buffer_
.reset(new DeviceOrientationHardwareBuffer
);
47 scoped_ptr
<DeviceLightHardwareBuffer
> light_buffer_
;
48 scoped_ptr
<DeviceMotionHardwareBuffer
> motion_buffer_
;
49 scoped_ptr
<DeviceOrientationHardwareBuffer
> orientation_buffer_
;
50 content::TestBrowserThreadBundle thread_bundle_
;
53 TEST_F(AndroidSensorManagerTest
, ThreeDeviceMotionSensorsActive
) {
54 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
55 FakeSensorManagerAndroid sensorManager
;
56 sensorManager
.SetNumberActiveDeviceMotionSensors(3);
58 sensorManager
.StartFetchingDeviceMotionData(motion_buffer_
.get());
59 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
61 sensorManager
.GotAcceleration(0, 0, 1, 2, 3);
62 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
63 ASSERT_EQ(1, motion_buffer_
->data
.accelerationX
);
64 ASSERT_TRUE(motion_buffer_
->data
.hasAccelerationX
);
65 ASSERT_EQ(2, motion_buffer_
->data
.accelerationY
);
66 ASSERT_TRUE(motion_buffer_
->data
.hasAccelerationY
);
67 ASSERT_EQ(3, motion_buffer_
->data
.accelerationZ
);
68 ASSERT_TRUE(motion_buffer_
->data
.hasAccelerationZ
);
70 sensorManager
.GotAccelerationIncludingGravity(0, 0, 4, 5, 6);
71 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
72 ASSERT_EQ(4, motion_buffer_
->data
.accelerationIncludingGravityX
);
73 ASSERT_TRUE(motion_buffer_
->data
.hasAccelerationIncludingGravityX
);
74 ASSERT_EQ(5, motion_buffer_
->data
.accelerationIncludingGravityY
);
75 ASSERT_TRUE(motion_buffer_
->data
.hasAccelerationIncludingGravityY
);
76 ASSERT_EQ(6, motion_buffer_
->data
.accelerationIncludingGravityZ
);
77 ASSERT_TRUE(motion_buffer_
->data
.hasAccelerationIncludingGravityZ
);
79 sensorManager
.GotRotationRate(0, 0, 7, 8, 9);
80 ASSERT_TRUE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
81 ASSERT_EQ(7, motion_buffer_
->data
.rotationRateAlpha
);
82 ASSERT_TRUE(motion_buffer_
->data
.hasRotationRateAlpha
);
83 ASSERT_EQ(8, motion_buffer_
->data
.rotationRateBeta
);
84 ASSERT_TRUE(motion_buffer_
->data
.hasRotationRateBeta
);
85 ASSERT_EQ(9, motion_buffer_
->data
.rotationRateGamma
);
86 ASSERT_TRUE(motion_buffer_
->data
.hasRotationRateGamma
);
87 ASSERT_EQ(kInertialSensorIntervalMicroseconds
/ 1000.,
88 motion_buffer_
->data
.interval
);
90 sensorManager
.StopFetchingDeviceMotionData();
91 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
94 TEST_F(AndroidSensorManagerTest
, TwoDeviceMotionSensorsActive
) {
95 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
96 FakeSensorManagerAndroid sensorManager
;
97 sensorManager
.SetNumberActiveDeviceMotionSensors(2);
99 sensorManager
.StartFetchingDeviceMotionData(motion_buffer_
.get());
100 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
102 sensorManager
.GotAcceleration(0, 0, 1, 2, 3);
103 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
105 sensorManager
.GotAccelerationIncludingGravity(0, 0, 1, 2, 3);
106 ASSERT_TRUE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
107 ASSERT_EQ(kInertialSensorIntervalMicroseconds
/ 1000.,
108 motion_buffer_
->data
.interval
);
110 sensorManager
.StopFetchingDeviceMotionData();
111 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
114 TEST_F(AndroidSensorManagerTest
, ZeroDeviceMotionSensorsActive
) {
115 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
116 FakeSensorManagerAndroid sensorManager
;
117 sensorManager
.SetNumberActiveDeviceMotionSensors(0);
119 sensorManager
.StartFetchingDeviceMotionData(motion_buffer_
.get());
120 ASSERT_TRUE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
121 ASSERT_EQ(kInertialSensorIntervalMicroseconds
/ 1000.,
122 motion_buffer_
->data
.interval
);
124 sensorManager
.StopFetchingDeviceMotionData();
125 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
128 TEST_F(AndroidSensorManagerTest
, DeviceOrientationSensorsActive
) {
129 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
130 FakeSensorManagerAndroid sensorManager
;
132 sensorManager
.StartFetchingDeviceOrientationData(orientation_buffer_
.get());
133 ASSERT_FALSE(orientation_buffer_
->data
.allAvailableSensorsAreActive
);
135 sensorManager
.GotOrientation(0, 0, 1, 2, 3);
136 ASSERT_TRUE(orientation_buffer_
->data
.allAvailableSensorsAreActive
);
137 ASSERT_EQ(1, orientation_buffer_
->data
.alpha
);
138 ASSERT_TRUE(orientation_buffer_
->data
.hasAlpha
);
139 ASSERT_EQ(2, orientation_buffer_
->data
.beta
);
140 ASSERT_TRUE(orientation_buffer_
->data
.hasBeta
);
141 ASSERT_EQ(3, orientation_buffer_
->data
.gamma
);
142 ASSERT_TRUE(orientation_buffer_
->data
.hasGamma
);
144 sensorManager
.StopFetchingDeviceOrientationData();
145 ASSERT_FALSE(orientation_buffer_
->data
.allAvailableSensorsAreActive
);
149 TEST_F(AndroidSensorManagerTest
, DeviceLightSensorsActive
) {
150 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
151 FakeSensorManagerAndroid sensorManager
;
153 sensorManager
.StartFetchingDeviceLightData(light_buffer_
.get());
155 sensorManager
.GotLight(0, 0, 100);
156 ASSERT_EQ(100, light_buffer_
->data
.value
);
158 sensorManager
.StopFetchingDeviceLightData();
159 ASSERT_EQ(-1, light_buffer_
->data
.value
);
164 } // namespace content