OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "content/browser/device_sensors/sensor_manager_chromeos.h" | |
6 | |
7 #include <memory> | |
8 | |
9 #include "base/macros.h" | |
10 #include "chromeos/accelerometer/accelerometer_types.h" | |
11 #include "device/sensors/public/cpp/device_motion_hardware_buffer.h" | |
12 #include "device/sensors/public/cpp/device_orientation_hardware_buffer.h" | |
13 #include "testing/gtest/include/gtest/gtest.h" | |
14 | |
15 namespace { | |
16 | |
17 const double kMeanGravity = -9.80665; | |
18 | |
19 // Isolated content::SensorManagerChromeOS from the active | |
20 // chromeos::AccelerometerReader. This allows for direct control over which | |
21 // accelerometer events are provided to the sensor manager. | |
22 class TestSensorManagerChromeOS : public content::SensorManagerChromeOS { | |
23 public: | |
24 TestSensorManagerChromeOS() {} | |
25 ~TestSensorManagerChromeOS() override {}; | |
26 | |
27 protected: | |
28 void StartObservingAccelerometer() override {} | |
29 void StopObservingAccelerometer() override {} | |
30 | |
31 private: | |
32 DISALLOW_COPY_AND_ASSIGN(TestSensorManagerChromeOS); | |
33 }; | |
34 | |
35 } // namespace | |
36 | |
37 namespace content { | |
38 | |
39 class SensorManagerChromeOSTest : public testing::Test { | |
40 public: | |
41 SensorManagerChromeOSTest() { | |
42 motion_buffer_.reset(new DeviceMotionHardwareBuffer); | |
43 orientation_buffer_.reset(new DeviceOrientationHardwareBuffer); | |
44 } | |
45 | |
46 ~SensorManagerChromeOSTest() override {} | |
47 | |
48 void OnAccelerationIncludingGravity(double x, double y, double z) { | |
49 scoped_refptr<chromeos::AccelerometerUpdate> update( | |
50 new chromeos::AccelerometerUpdate()); | |
51 update->Set(chromeos::ACCELEROMETER_SOURCE_SCREEN, x, y, z); | |
52 sensor_manager_->OnAccelerometerUpdated(update); | |
53 } | |
54 | |
55 DeviceMotionHardwareBuffer* motion_buffer() { return motion_buffer_.get(); } | |
56 | |
57 DeviceOrientationHardwareBuffer* orientation_buffer() { | |
58 return orientation_buffer_.get(); | |
59 } | |
60 | |
61 SensorManagerChromeOS* sensor_manager() { return sensor_manager_.get(); } | |
62 | |
63 // testing::Test: | |
64 void SetUp() override { | |
65 testing::Test::SetUp(); | |
66 sensor_manager_.reset(new TestSensorManagerChromeOS); | |
67 sensor_manager_->StartFetchingDeviceMotionData(motion_buffer_.get()); | |
68 sensor_manager_->StartFetchingDeviceOrientationData( | |
69 orientation_buffer_.get()); | |
70 } | |
71 | |
72 void TearDown() override { | |
73 sensor_manager_->StopFetchingDeviceMotionData(); | |
74 sensor_manager_->StopFetchingDeviceOrientationData(); | |
75 testing::Test::TearDown(); | |
76 } | |
77 | |
78 private: | |
79 std::unique_ptr<TestSensorManagerChromeOS> sensor_manager_; | |
80 std::unique_ptr<DeviceMotionHardwareBuffer> motion_buffer_; | |
81 std::unique_ptr<DeviceOrientationHardwareBuffer> orientation_buffer_; | |
82 | |
83 DISALLOW_COPY_AND_ASSIGN(SensorManagerChromeOSTest); | |
84 }; | |
85 | |
86 // Tests that starting to process motion data will update the associated buffer. | |
87 TEST_F(SensorManagerChromeOSTest, MotionBuffer) { | |
88 DeviceMotionHardwareBuffer* buffer = motion_buffer(); | |
89 EXPECT_FLOAT_EQ(100.0f, buffer->data.interval); | |
90 EXPECT_FALSE(buffer->data.hasAccelerationIncludingGravityX); | |
91 EXPECT_FALSE(buffer->data.hasAccelerationIncludingGravityY); | |
92 EXPECT_FALSE(buffer->data.hasAccelerationIncludingGravityZ); | |
93 EXPECT_FALSE(buffer->data.hasAccelerationX); | |
94 EXPECT_FALSE(buffer->data.hasAccelerationY); | |
95 EXPECT_FALSE(buffer->data.hasAccelerationZ); | |
96 EXPECT_FALSE(buffer->data.hasRotationRateAlpha); | |
97 EXPECT_FALSE(buffer->data.hasRotationRateBeta); | |
98 EXPECT_FALSE(buffer->data.hasRotationRateGamma); | |
99 | |
100 OnAccelerationIncludingGravity(0.0f, 0.0f, 1.0f); | |
101 EXPECT_TRUE(buffer->data.hasAccelerationIncludingGravityX); | |
102 EXPECT_TRUE(buffer->data.hasAccelerationIncludingGravityY); | |
103 EXPECT_TRUE(buffer->data.hasAccelerationIncludingGravityZ); | |
104 EXPECT_FALSE(buffer->data.hasAccelerationX); | |
105 EXPECT_FALSE(buffer->data.hasAccelerationY); | |
106 EXPECT_FALSE(buffer->data.hasAccelerationZ); | |
107 EXPECT_FALSE(buffer->data.hasRotationRateAlpha); | |
108 EXPECT_FALSE(buffer->data.hasRotationRateBeta); | |
109 EXPECT_FALSE(buffer->data.hasRotationRateGamma); | |
110 EXPECT_TRUE(buffer->data.allAvailableSensorsAreActive); | |
111 | |
112 sensor_manager()->StopFetchingDeviceMotionData(); | |
113 EXPECT_FALSE(buffer->data.allAvailableSensorsAreActive); | |
114 } | |
115 | |
116 // Tests that starting to process orientation data will update the associated | |
117 // buffer. | |
118 TEST_F(SensorManagerChromeOSTest, OrientationBuffer) { | |
119 DeviceOrientationHardwareBuffer* buffer = orientation_buffer(); | |
120 EXPECT_FALSE(buffer->data.hasAlpha); | |
121 EXPECT_FALSE(buffer->data.hasBeta); | |
122 EXPECT_FALSE(buffer->data.hasGamma); | |
123 EXPECT_FALSE(buffer->data.allAvailableSensorsAreActive); | |
124 | |
125 OnAccelerationIncludingGravity(0.0f, 0.0f, 1.0f); | |
126 EXPECT_FLOAT_EQ(0.0f, buffer->data.alpha); | |
127 EXPECT_FALSE(buffer->data.hasAlpha); | |
128 EXPECT_TRUE(buffer->data.hasBeta); | |
129 EXPECT_TRUE(buffer->data.hasGamma); | |
130 EXPECT_TRUE(buffer->data.allAvailableSensorsAreActive); | |
131 | |
132 sensor_manager()->StopFetchingDeviceOrientationData(); | |
133 EXPECT_FALSE(buffer->data.allAvailableSensorsAreActive); | |
134 } | |
135 | |
136 // Tests a device resting flat. | |
137 TEST_F(SensorManagerChromeOSTest, NeutralOrientation) { | |
138 OnAccelerationIncludingGravity(0.0f, 0.0f, -kMeanGravity); | |
139 | |
140 DeviceMotionHardwareBuffer* motion = motion_buffer(); | |
141 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityX); | |
142 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY); | |
143 EXPECT_FLOAT_EQ(-kMeanGravity, motion->data.accelerationIncludingGravityZ); | |
144 | |
145 DeviceOrientationHardwareBuffer* orientation = orientation_buffer(); | |
146 EXPECT_FLOAT_EQ(0.0f, orientation->data.beta); | |
147 EXPECT_FLOAT_EQ(0.0f, orientation->data.gamma); | |
148 } | |
149 | |
150 // Tests an upside-down device, such that the W3C boundary [-180,180) causes the | |
151 // beta value to become negative. | |
152 TEST_F(SensorManagerChromeOSTest, UpsideDown) { | |
153 OnAccelerationIncludingGravity(0.0f, 0.0f, kMeanGravity); | |
154 | |
155 DeviceMotionHardwareBuffer* motion = motion_buffer(); | |
156 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityX); | |
157 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY); | |
158 EXPECT_FLOAT_EQ(kMeanGravity, motion->data.accelerationIncludingGravityZ); | |
159 | |
160 DeviceOrientationHardwareBuffer* orientation = orientation_buffer(); | |
161 EXPECT_FLOAT_EQ(-180.0f, orientation->data.beta); | |
162 EXPECT_FLOAT_EQ(0.0f, orientation->data.gamma); | |
163 } | |
164 | |
165 // Tests for positive beta value before the device is completely upside-down | |
166 TEST_F(SensorManagerChromeOSTest, BeforeUpsideDownBoundary) { | |
167 OnAccelerationIncludingGravity(0.0f, -kMeanGravity / 2.0f, | |
168 kMeanGravity / 2.0f); | |
169 | |
170 DeviceMotionHardwareBuffer* motion = motion_buffer(); | |
171 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityX); | |
172 EXPECT_FLOAT_EQ(-kMeanGravity / 2.0f, | |
173 motion->data.accelerationIncludingGravityY); | |
174 EXPECT_FLOAT_EQ(kMeanGravity / 2.0f, | |
175 motion->data.accelerationIncludingGravityZ); | |
176 | |
177 DeviceOrientationHardwareBuffer* orientation = orientation_buffer(); | |
178 EXPECT_FLOAT_EQ(135.0f, orientation->data.beta); | |
179 EXPECT_FLOAT_EQ(0.0f, orientation->data.gamma); | |
180 } | |
181 | |
182 // Tests a device lying on its left-edge. | |
183 TEST_F(SensorManagerChromeOSTest, LeftEdge) { | |
184 OnAccelerationIncludingGravity(-kMeanGravity, 0.0f, 0.0f); | |
185 | |
186 DeviceMotionHardwareBuffer* motion = motion_buffer(); | |
187 EXPECT_FLOAT_EQ(-kMeanGravity, motion->data.accelerationIncludingGravityX); | |
188 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY); | |
189 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityZ); | |
190 | |
191 DeviceOrientationHardwareBuffer* orientation = orientation_buffer(); | |
192 EXPECT_FLOAT_EQ(0.0f, orientation->data.beta); | |
193 EXPECT_FLOAT_EQ(-90.0f, orientation->data.gamma); | |
194 } | |
195 | |
196 // Tests a device lying on its right-edge, such that the W3C boundary [-90,90) | |
197 // causes the gamma value to become negative. | |
198 TEST_F(SensorManagerChromeOSTest, RightEdge) { | |
199 OnAccelerationIncludingGravity(kMeanGravity, 0.0f, 0.0f); | |
200 | |
201 DeviceMotionHardwareBuffer* motion = motion_buffer(); | |
202 EXPECT_FLOAT_EQ(kMeanGravity, motion->data.accelerationIncludingGravityX); | |
203 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY); | |
204 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityZ); | |
205 | |
206 DeviceOrientationHardwareBuffer* orientation = orientation_buffer(); | |
207 EXPECT_FLOAT_EQ(0.0f, orientation->data.beta); | |
208 EXPECT_FLOAT_EQ(-90.0f, orientation->data.gamma); | |
209 } | |
210 | |
211 // Tests for positive gamma value before the device is completely on its right | |
212 // side. | |
213 TEST_F(SensorManagerChromeOSTest, BeforeRightEdgeBoundary) { | |
214 OnAccelerationIncludingGravity(kMeanGravity / 2.0f, 0.0f, | |
215 -kMeanGravity / 2.0f); | |
216 | |
217 DeviceMotionHardwareBuffer* motion = motion_buffer(); | |
218 EXPECT_FLOAT_EQ(kMeanGravity / 2.0f, | |
219 motion->data.accelerationIncludingGravityX); | |
220 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY); | |
221 EXPECT_FLOAT_EQ(-kMeanGravity / 2.0f, | |
222 motion->data.accelerationIncludingGravityZ); | |
223 | |
224 DeviceOrientationHardwareBuffer* orientation = orientation_buffer(); | |
225 EXPECT_FLOAT_EQ(0.0f, orientation->data.beta); | |
226 EXPECT_FLOAT_EQ(45.0f, orientation->data.gamma); | |
227 } | |
228 | |
229 } // namespace content | |
OLD | NEW |