Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / content / browser / device_sensors / sensor_manager_chromeos_unittest.cc
blob184a315615de624e6f1bed6391452142b22e6b81
1 // Copyright 2015 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_chromeos.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "chromeos/accelerometer/accelerometer_types.h"
9 #include "content/common/device_sensors/device_motion_hardware_buffer.h"
10 #include "content/common/device_sensors/device_orientation_hardware_buffer.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace {
15 const double kMeanGravity = -9.80665;
17 // Isolated content::SensorManagerChromeOS from the active
18 // chromeos::AccelerometerReader. This allows for direct control over which
19 // accelerometer events are provided to the sensor manager.
20 class TestSensorManagerChromeOS : public content::SensorManagerChromeOS {
21 public:
22 TestSensorManagerChromeOS() {}
23 ~TestSensorManagerChromeOS() override {};
25 protected:
26 void StartObservingAccelerometer() override {}
27 void StopObservingAccelerometer() override {}
29 private:
30 DISALLOW_COPY_AND_ASSIGN(TestSensorManagerChromeOS);
33 } // namespace
35 namespace content {
37 class SensorManagerChromeOSTest : public testing::Test {
38 public:
39 SensorManagerChromeOSTest() {
40 motion_buffer_.reset(new DeviceMotionHardwareBuffer);
41 orientation_buffer_.reset(new DeviceOrientationHardwareBuffer);
44 ~SensorManagerChromeOSTest() override {}
46 void OnAccelerationIncludingGravity(double x, double y, double z) {
47 scoped_refptr<chromeos::AccelerometerUpdate> update(
48 new chromeos::AccelerometerUpdate());
49 update->Set(chromeos::ACCELEROMETER_SOURCE_SCREEN, x, y, z);
50 sensor_manager_->OnAccelerometerUpdated(update);
53 DeviceMotionHardwareBuffer* motion_buffer() { return motion_buffer_.get(); }
55 DeviceOrientationHardwareBuffer* orientation_buffer() {
56 return orientation_buffer_.get();
59 SensorManagerChromeOS* sensor_manager() { return sensor_manager_.get(); }
61 // testing::Test:
62 void SetUp() override {
63 testing::Test::SetUp();
64 sensor_manager_.reset(new TestSensorManagerChromeOS);
65 sensor_manager_->StartFetchingDeviceMotionData(motion_buffer_.get());
66 sensor_manager_->StartFetchingDeviceOrientationData(
67 orientation_buffer_.get());
70 void TearDown() override {
71 sensor_manager_->StopFetchingDeviceMotionData();
72 sensor_manager_->StopFetchingDeviceOrientationData();
73 testing::Test::TearDown();
76 private:
77 scoped_ptr<TestSensorManagerChromeOS> sensor_manager_;
78 scoped_ptr<DeviceMotionHardwareBuffer> motion_buffer_;
79 scoped_ptr<DeviceOrientationHardwareBuffer> orientation_buffer_;
81 DISALLOW_COPY_AND_ASSIGN(SensorManagerChromeOSTest);
84 // Tests that starting to process motion data will update the associated buffer.
85 TEST_F(SensorManagerChromeOSTest, MotionBuffer) {
86 DeviceMotionHardwareBuffer* buffer = motion_buffer();
87 EXPECT_FLOAT_EQ(100.0f, buffer->data.interval);
88 EXPECT_FALSE(buffer->data.hasAccelerationIncludingGravityX);
89 EXPECT_FALSE(buffer->data.hasAccelerationIncludingGravityY);
90 EXPECT_FALSE(buffer->data.hasAccelerationIncludingGravityZ);
91 EXPECT_FALSE(buffer->data.hasAccelerationX);
92 EXPECT_FALSE(buffer->data.hasAccelerationY);
93 EXPECT_FALSE(buffer->data.hasAccelerationZ);
94 EXPECT_FALSE(buffer->data.hasRotationRateAlpha);
95 EXPECT_FALSE(buffer->data.hasRotationRateBeta);
96 EXPECT_FALSE(buffer->data.hasRotationRateGamma);
98 OnAccelerationIncludingGravity(0.0f, 0.0f, 1.0f);
99 EXPECT_TRUE(buffer->data.hasAccelerationIncludingGravityX);
100 EXPECT_TRUE(buffer->data.hasAccelerationIncludingGravityY);
101 EXPECT_TRUE(buffer->data.hasAccelerationIncludingGravityZ);
102 EXPECT_FALSE(buffer->data.hasAccelerationX);
103 EXPECT_FALSE(buffer->data.hasAccelerationY);
104 EXPECT_FALSE(buffer->data.hasAccelerationZ);
105 EXPECT_FALSE(buffer->data.hasRotationRateAlpha);
106 EXPECT_FALSE(buffer->data.hasRotationRateBeta);
107 EXPECT_FALSE(buffer->data.hasRotationRateGamma);
108 EXPECT_TRUE(buffer->data.allAvailableSensorsAreActive);
110 sensor_manager()->StopFetchingDeviceMotionData();
111 EXPECT_FALSE(buffer->data.allAvailableSensorsAreActive);
114 // Tests that starting to process orientation data will update the associated
115 // buffer.
116 TEST_F(SensorManagerChromeOSTest, OrientationBuffer) {
117 DeviceOrientationHardwareBuffer* buffer = orientation_buffer();
118 EXPECT_TRUE(buffer->data.hasAbsolute);
119 EXPECT_FALSE(buffer->data.hasAlpha);
120 EXPECT_FALSE(buffer->data.hasBeta);
121 EXPECT_FALSE(buffer->data.hasGamma);
122 EXPECT_FALSE(buffer->data.allAvailableSensorsAreActive);
124 OnAccelerationIncludingGravity(0.0f, 0.0f, 1.0f);
125 EXPECT_FLOAT_EQ(0.0f, buffer->data.alpha);
126 EXPECT_FALSE(buffer->data.hasAlpha);
127 EXPECT_TRUE(buffer->data.hasBeta);
128 EXPECT_TRUE(buffer->data.hasGamma);
129 EXPECT_TRUE(buffer->data.allAvailableSensorsAreActive);
131 sensor_manager()->StopFetchingDeviceOrientationData();
132 EXPECT_FALSE(buffer->data.allAvailableSensorsAreActive);
135 // Tests a device resting flat.
136 TEST_F(SensorManagerChromeOSTest, NeutralOrientation) {
137 OnAccelerationIncludingGravity(0.0f, 0.0f, -kMeanGravity);
139 DeviceMotionHardwareBuffer* motion = motion_buffer();
140 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityX);
141 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY);
142 EXPECT_FLOAT_EQ(-kMeanGravity, motion->data.accelerationIncludingGravityZ);
144 DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
145 EXPECT_FLOAT_EQ(0.0f, orientation->data.beta);
146 EXPECT_FLOAT_EQ(0.0f, orientation->data.gamma);
149 // Tests an upside-down device, such that the W3C boundary [-180,180) causes the
150 // beta value to become negative.
151 TEST_F(SensorManagerChromeOSTest, UpsideDown) {
152 OnAccelerationIncludingGravity(0.0f, 0.0f, kMeanGravity);
154 DeviceMotionHardwareBuffer* motion = motion_buffer();
155 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityX);
156 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY);
157 EXPECT_FLOAT_EQ(kMeanGravity, motion->data.accelerationIncludingGravityZ);
159 DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
160 EXPECT_FLOAT_EQ(-180.0f, orientation->data.beta);
161 EXPECT_FLOAT_EQ(0.0f, orientation->data.gamma);
164 // Tests for positive beta value before the device is completely upside-down
165 TEST_F(SensorManagerChromeOSTest, BeforeUpsideDownBoundary) {
166 OnAccelerationIncludingGravity(0.0f, -kMeanGravity / 2.0f,
167 kMeanGravity / 2.0f);
169 DeviceMotionHardwareBuffer* motion = motion_buffer();
170 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityX);
171 EXPECT_FLOAT_EQ(-kMeanGravity / 2.0f,
172 motion->data.accelerationIncludingGravityY);
173 EXPECT_FLOAT_EQ(kMeanGravity / 2.0f,
174 motion->data.accelerationIncludingGravityZ);
176 DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
177 EXPECT_FLOAT_EQ(135.0f, orientation->data.beta);
178 EXPECT_FLOAT_EQ(0.0f, orientation->data.gamma);
181 // Tests a device lying on its left-edge.
182 TEST_F(SensorManagerChromeOSTest, LeftEdge) {
183 OnAccelerationIncludingGravity(-kMeanGravity, 0.0f, 0.0f);
185 DeviceMotionHardwareBuffer* motion = motion_buffer();
186 EXPECT_FLOAT_EQ(-kMeanGravity, motion->data.accelerationIncludingGravityX);
187 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY);
188 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityZ);
190 DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
191 EXPECT_FLOAT_EQ(0.0f, orientation->data.beta);
192 EXPECT_FLOAT_EQ(-90.0f, orientation->data.gamma);
195 // Tests a device lying on its right-edge, such that the W3C boundary [-90,90)
196 // causes the gamma value to become negative.
197 TEST_F(SensorManagerChromeOSTest, RightEdge) {
198 OnAccelerationIncludingGravity(kMeanGravity, 0.0f, 0.0f);
200 DeviceMotionHardwareBuffer* motion = motion_buffer();
201 EXPECT_FLOAT_EQ(kMeanGravity, motion->data.accelerationIncludingGravityX);
202 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY);
203 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityZ);
205 DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
206 EXPECT_FLOAT_EQ(0.0f, orientation->data.beta);
207 EXPECT_FLOAT_EQ(-90.0f, orientation->data.gamma);
210 // Tests for positive gamma value before the device is completely on its right
211 // side.
212 TEST_F(SensorManagerChromeOSTest, BeforeRightEdgeBoundary) {
213 OnAccelerationIncludingGravity(kMeanGravity / 2.0f, 0.0f,
214 -kMeanGravity / 2.0f);
216 DeviceMotionHardwareBuffer* motion = motion_buffer();
217 EXPECT_FLOAT_EQ(kMeanGravity / 2.0f,
218 motion->data.accelerationIncludingGravityX);
219 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY);
220 EXPECT_FLOAT_EQ(-kMeanGravity / 2.0f,
221 motion->data.accelerationIncludingGravityZ);
223 DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
224 EXPECT_FLOAT_EQ(0.0f, orientation->data.beta);
225 EXPECT_FLOAT_EQ(45.0f, orientation->data.gamma);
228 } // namespace content