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_orientation/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_orientation/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 motion_buffer_
.reset(new DeviceMotionHardwareBuffer
);
46 orientation_buffer_
.reset(new DeviceOrientationHardwareBuffer
);
49 scoped_ptr
<DeviceMotionHardwareBuffer
> motion_buffer_
;
50 scoped_ptr
<DeviceOrientationHardwareBuffer
> orientation_buffer_
;
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(kInertialSensorIntervalMillis
, motion_buffer_
->data
.interval
);
89 sensorManager
.StopFetchingDeviceMotionData();
90 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
93 TEST_F(AndroidSensorManagerTest
, TwoDeviceMotionSensorsActive
) {
94 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
95 FakeSensorManagerAndroid sensorManager
;
96 sensorManager
.SetNumberActiveDeviceMotionSensors(2);
98 sensorManager
.StartFetchingDeviceMotionData(motion_buffer_
.get());
99 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
101 sensorManager
.GotAcceleration(0, 0, 1, 2, 3);
102 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
104 sensorManager
.GotAccelerationIncludingGravity(0, 0, 1, 2, 3);
105 ASSERT_TRUE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
106 ASSERT_EQ(kInertialSensorIntervalMillis
, motion_buffer_
->data
.interval
);
108 sensorManager
.StopFetchingDeviceMotionData();
109 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
112 TEST_F(AndroidSensorManagerTest
, ZeroDeviceMotionSensorsActive
) {
113 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
114 FakeSensorManagerAndroid sensorManager
;
115 sensorManager
.SetNumberActiveDeviceMotionSensors(0);
117 sensorManager
.StartFetchingDeviceMotionData(motion_buffer_
.get());
118 ASSERT_TRUE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
119 ASSERT_EQ(kInertialSensorIntervalMillis
, motion_buffer_
->data
.interval
);
121 sensorManager
.StopFetchingDeviceMotionData();
122 ASSERT_FALSE(motion_buffer_
->data
.allAvailableSensorsAreActive
);
125 TEST_F(AndroidSensorManagerTest
, DeviceOrientationSensorsActive
) {
126 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
127 FakeSensorManagerAndroid sensorManager
;
129 sensorManager
.StartFetchingDeviceOrientationData(orientation_buffer_
.get());
130 ASSERT_FALSE(orientation_buffer_
->data
.allAvailableSensorsAreActive
);
132 sensorManager
.GotOrientation(0, 0, 1, 2, 3);
133 ASSERT_TRUE(orientation_buffer_
->data
.allAvailableSensorsAreActive
);
134 ASSERT_EQ(1, orientation_buffer_
->data
.alpha
);
135 ASSERT_TRUE(orientation_buffer_
->data
.hasAlpha
);
136 ASSERT_EQ(2, orientation_buffer_
->data
.beta
);
137 ASSERT_TRUE(orientation_buffer_
->data
.hasBeta
);
138 ASSERT_EQ(3, orientation_buffer_
->data
.gamma
);
139 ASSERT_TRUE(orientation_buffer_
->data
.hasGamma
);
141 sensorManager
.StopFetchingDeviceOrientationData();
142 ASSERT_FALSE(orientation_buffer_
->data
.allAvailableSensorsAreActive
);
148 } // namespace content