[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / content / browser / device_sensors / sensor_manager_android_unittest.cc
blobdd91c4bcb1c6b1b2ca6bd5c85b1dd1e887c554df
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"
14 namespace content {
16 namespace {
18 class FakeSensorManagerAndroid : public SensorManagerAndroid {
19 public:
20 FakeSensorManagerAndroid() {}
21 ~FakeSensorManagerAndroid() override {}
23 int GetOrientationSensorTypeUsed() override {
24 return 1; // ROTATION_VECTOR
27 int GetNumberActiveDeviceMotionSensors() override {
28 return number_active_sensors_;
31 void SetNumberActiveDeviceMotionSensors(int number_active_sensors) {
32 number_active_sensors_ = number_active_sensors;
35 protected:
36 bool Start(EventType event_type) override { return true; }
37 void Stop(EventType event_type) override {}
39 private:
40 int number_active_sensors_;
43 class AndroidSensorManagerTest : public testing::Test {
44 protected:
45 AndroidSensorManagerTest() {
46 light_buffer_.reset(new DeviceLightHardwareBuffer);
47 motion_buffer_.reset(new DeviceMotionHardwareBuffer);
48 orientation_buffer_.reset(new DeviceOrientationHardwareBuffer);
51 scoped_ptr<DeviceLightHardwareBuffer> light_buffer_;
52 scoped_ptr<DeviceMotionHardwareBuffer> motion_buffer_;
53 scoped_ptr<DeviceOrientationHardwareBuffer> orientation_buffer_;
54 content::TestBrowserThreadBundle thread_bundle_;
57 TEST_F(AndroidSensorManagerTest, ThreeDeviceMotionSensorsActive) {
58 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
59 FakeSensorManagerAndroid sensorManager;
60 sensorManager.SetNumberActiveDeviceMotionSensors(3);
62 sensorManager.StartFetchingDeviceMotionData(motion_buffer_.get());
63 ASSERT_FALSE(motion_buffer_->data.allAvailableSensorsAreActive);
65 sensorManager.GotAcceleration(0, 0, 1, 2, 3);
66 ASSERT_FALSE(motion_buffer_->data.allAvailableSensorsAreActive);
67 ASSERT_EQ(1, motion_buffer_->data.accelerationX);
68 ASSERT_TRUE(motion_buffer_->data.hasAccelerationX);
69 ASSERT_EQ(2, motion_buffer_->data.accelerationY);
70 ASSERT_TRUE(motion_buffer_->data.hasAccelerationY);
71 ASSERT_EQ(3, motion_buffer_->data.accelerationZ);
72 ASSERT_TRUE(motion_buffer_->data.hasAccelerationZ);
74 sensorManager.GotAccelerationIncludingGravity(0, 0, 4, 5, 6);
75 ASSERT_FALSE(motion_buffer_->data.allAvailableSensorsAreActive);
76 ASSERT_EQ(4, motion_buffer_->data.accelerationIncludingGravityX);
77 ASSERT_TRUE(motion_buffer_->data.hasAccelerationIncludingGravityX);
78 ASSERT_EQ(5, motion_buffer_->data.accelerationIncludingGravityY);
79 ASSERT_TRUE(motion_buffer_->data.hasAccelerationIncludingGravityY);
80 ASSERT_EQ(6, motion_buffer_->data.accelerationIncludingGravityZ);
81 ASSERT_TRUE(motion_buffer_->data.hasAccelerationIncludingGravityZ);
83 sensorManager.GotRotationRate(0, 0, 7, 8, 9);
84 ASSERT_TRUE(motion_buffer_->data.allAvailableSensorsAreActive);
85 ASSERT_EQ(7, motion_buffer_->data.rotationRateAlpha);
86 ASSERT_TRUE(motion_buffer_->data.hasRotationRateAlpha);
87 ASSERT_EQ(8, motion_buffer_->data.rotationRateBeta);
88 ASSERT_TRUE(motion_buffer_->data.hasRotationRateBeta);
89 ASSERT_EQ(9, motion_buffer_->data.rotationRateGamma);
90 ASSERT_TRUE(motion_buffer_->data.hasRotationRateGamma);
91 ASSERT_EQ(kInertialSensorIntervalMicroseconds / 1000.,
92 motion_buffer_->data.interval);
94 sensorManager.StopFetchingDeviceMotionData();
95 ASSERT_FALSE(motion_buffer_->data.allAvailableSensorsAreActive);
98 TEST_F(AndroidSensorManagerTest, TwoDeviceMotionSensorsActive) {
99 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
100 FakeSensorManagerAndroid sensorManager;
101 sensorManager.SetNumberActiveDeviceMotionSensors(2);
103 sensorManager.StartFetchingDeviceMotionData(motion_buffer_.get());
104 ASSERT_FALSE(motion_buffer_->data.allAvailableSensorsAreActive);
106 sensorManager.GotAcceleration(0, 0, 1, 2, 3);
107 ASSERT_FALSE(motion_buffer_->data.allAvailableSensorsAreActive);
109 sensorManager.GotAccelerationIncludingGravity(0, 0, 1, 2, 3);
110 ASSERT_TRUE(motion_buffer_->data.allAvailableSensorsAreActive);
111 ASSERT_EQ(kInertialSensorIntervalMicroseconds / 1000.,
112 motion_buffer_->data.interval);
114 sensorManager.StopFetchingDeviceMotionData();
115 ASSERT_FALSE(motion_buffer_->data.allAvailableSensorsAreActive);
118 TEST_F(AndroidSensorManagerTest, ZeroDeviceMotionSensorsActive) {
119 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
120 FakeSensorManagerAndroid sensorManager;
121 sensorManager.SetNumberActiveDeviceMotionSensors(0);
123 sensorManager.StartFetchingDeviceMotionData(motion_buffer_.get());
124 ASSERT_TRUE(motion_buffer_->data.allAvailableSensorsAreActive);
125 ASSERT_EQ(kInertialSensorIntervalMicroseconds / 1000.,
126 motion_buffer_->data.interval);
128 sensorManager.StopFetchingDeviceMotionData();
129 ASSERT_FALSE(motion_buffer_->data.allAvailableSensorsAreActive);
132 TEST_F(AndroidSensorManagerTest, DeviceOrientationSensorsActive) {
133 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
134 FakeSensorManagerAndroid sensorManager;
136 sensorManager.StartFetchingDeviceOrientationData(orientation_buffer_.get());
137 ASSERT_FALSE(orientation_buffer_->data.allAvailableSensorsAreActive);
139 sensorManager.GotOrientation(0, 0, 1, 2, 3);
140 ASSERT_TRUE(orientation_buffer_->data.allAvailableSensorsAreActive);
141 ASSERT_EQ(1, orientation_buffer_->data.alpha);
142 ASSERT_TRUE(orientation_buffer_->data.hasAlpha);
143 ASSERT_EQ(2, orientation_buffer_->data.beta);
144 ASSERT_TRUE(orientation_buffer_->data.hasBeta);
145 ASSERT_EQ(3, orientation_buffer_->data.gamma);
146 ASSERT_TRUE(orientation_buffer_->data.hasGamma);
148 sensorManager.StopFetchingDeviceOrientationData();
149 ASSERT_FALSE(orientation_buffer_->data.allAvailableSensorsAreActive);
152 // DeviceLight
153 TEST_F(AndroidSensorManagerTest, DeviceLightSensorsActive) {
154 FakeSensorManagerAndroid::Register(base::android::AttachCurrentThread());
155 FakeSensorManagerAndroid sensorManager;
157 sensorManager.StartFetchingDeviceLightData(light_buffer_.get());
159 sensorManager.GotLight(0, 0, 100);
160 ASSERT_EQ(100, light_buffer_->data.value);
162 sensorManager.StopFetchingDeviceLightData();
163 ASSERT_EQ(-1, light_buffer_->data.value);
166 } // namespace
168 } // namespace content