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 "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" | |
12 | |
13 namespace { | |
14 | |
15 const double kMeanGravity = 9.80665; | |
16 | |
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 {}; | |
24 | |
25 protected: | |
26 void StartObservingAccelerometer() override {} | |
27 void StopObservingAccelerometer() override {} | |
28 | |
29 private: | |
30 DISALLOW_COPY_AND_ASSIGN(TestSensorManagerChromeOS); | |
31 }; | |
32 | |
33 } // namespace | |
34 | |
35 namespace content { | |
36 | |
37 class SensorManagerChromeOSTest : public testing::Test { | |
38 public: | |
39 SensorManagerChromeOSTest() { | |
40 motion_buffer_.reset(new DeviceMotionHardwareBuffer); | |
41 orientation_buffer_.reset(new DeviceOrientationHardwareBuffer); | |
42 } | |
43 | |
44 ~SensorManagerChromeOSTest() override {} | |
45 | |
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); | |
51 } | |
52 | |
53 DeviceMotionHardwareBuffer* motion_buffer() { return motion_buffer_.get(); } | |
54 | |
55 DeviceOrientationHardwareBuffer* orientation_buffer() { | |
56 return orientation_buffer_.get(); | |
57 } | |
58 | |
59 SensorManagerChromeOS* sensor_manager() { return sensor_manager_.get(); } | |
60 | |
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()); | |
68 } | |
69 | |
70 void TearDown() override { | |
71 sensor_manager_->StopFetchingDeviceMotionData(); | |
72 sensor_manager_->StopFetchingDeviceOrientationData(); | |
73 testing::Test::TearDown(); | |
74 } | |
75 | |
76 private: | |
77 scoped_ptr<TestSensorManagerChromeOS> sensor_manager_; | |
78 scoped_ptr<DeviceMotionHardwareBuffer> motion_buffer_; | |
79 scoped_ptr<DeviceOrientationHardwareBuffer> orientation_buffer_; | |
80 | |
81 DISALLOW_COPY_AND_ASSIGN(SensorManagerChromeOSTest); | |
82 }; | |
83 | |
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); | |
97 | |
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); | |
109 | |
110 sensor_manager()->StopFetchingDeviceMotionData(); | |
111 EXPECT_FALSE(buffer->data.allAvailableSensorsAreActive); | |
112 } | |
113 | |
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); | |
123 | |
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); | |
130 | |
131 sensor_manager()->StopFetchingDeviceOrientationData(); | |
132 EXPECT_FALSE(buffer->data.allAvailableSensorsAreActive); | |
133 } | |
134 | |
135 // Tests a device resting flat. | |
136 TEST_F(SensorManagerChromeOSTest, NeutralOrientation) { | |
137 OnAccelerationIncludingGravity(0.0f, 0.0f, -kMeanGravity); | |
138 | |
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); | |
143 | |
144 DeviceOrientationHardwareBuffer* orientation = orientation_buffer(); | |
145 EXPECT_FLOAT_EQ(0.0f, orientation->data.beta); | |
146 EXPECT_FLOAT_EQ(0.0f, orientation->data.gamma); | |
147 } | |
148 | |
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); | |
153 | |
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); | |
158 | |
159 DeviceOrientationHardwareBuffer* orientation = orientation_buffer(); | |
160 EXPECT_FLOAT_EQ(-180.0f, orientation->data.beta); | |
161 EXPECT_FLOAT_EQ(0.0f, orientation->data.gamma); | |
162 } | |
163 | |
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); | |
168 | |
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); | |
175 | |
176 DeviceOrientationHardwareBuffer* orientation = orientation_buffer(); | |
177 EXPECT_FLOAT_EQ(135.0f, orientation->data.beta); | |
178 EXPECT_FLOAT_EQ(0.0f, orientation->data.gamma); | |
179 } | |
180 | |
181 // Tests a device lying on its left-edge. | |
182 TEST_F(SensorManagerChromeOSTest, LeftEdge) { | |
183 OnAccelerationIncludingGravity(-kMeanGravity, 0.0f, 0.0f); | |
184 | |
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); | |
189 | |
190 DeviceOrientationHardwareBuffer* orientation = orientation_buffer(); | |
191 EXPECT_FLOAT_EQ(0.0f, orientation->data.beta); | |
192 EXPECT_FLOAT_EQ(-90.0f, orientation->data.gamma); | |
193 } | |
194 | |
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); | |
199 | |
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); | |
204 | |
205 DeviceOrientationHardwareBuffer* orientation = orientation_buffer(); | |
206 EXPECT_FLOAT_EQ(0.0f, orientation->data.beta); | |
207 EXPECT_FLOAT_EQ(-90.0f, orientation->data.gamma); | |
208 } | |
209 | |
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); | |
215 | |
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); | |
222 | |
223 DeviceOrientationHardwareBuffer* orientation = orientation_buffer(); | |
224 EXPECT_FLOAT_EQ(0.0f, orientation->data.beta); | |
225 EXPECT_FLOAT_EQ(45.0f, orientation->data.gamma); | |
226 } | |
227 | |
228 } // namespace content | |
OLD | NEW |