Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(19)

Side by Side Diff: content/browser/device_sensors/sensor_manager_chromeos_unittest.cc

Issue 934843002: Implement DeviceMotionEvent API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/device_sensors/sensor_manager_chromeos.h" 5 #include "content/browser/device_sensors/sensor_manager_chromeos.h"
6 6
7 #include "base/memory/scoped_ptr.h" 7 #include "base/memory/scoped_ptr.h"
8 #include "chromeos/accelerometer/accelerometer_types.h" 8 #include "chromeos/accelerometer/accelerometer_types.h"
9 #include "content/common/device_sensors/device_motion_hardware_buffer.h"
9 #include "content/common/device_sensors/device_orientation_hardware_buffer.h" 10 #include "content/common/device_sensors/device_orientation_hardware_buffer.h"
10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "ui/gfx/geometry/vector3d_f.h"
11 13
12 namespace { 14 namespace {
13 15
14 const double kMeanGravity = 9.80665; 16 const double kMeanGravity = 9.80665;
15 17
18 const double kGravityFilterRatio = 0.8;
19
16 // Isolated content::SensorManagerChromeOS from the active 20 // Isolated content::SensorManagerChromeOS from the active
17 // chromeos::AccelerometerReader. This allows for direct control over which 21 // chromeos::AccelerometerReader. This allows for direct control over which
18 // accelerometer events are provided to the sensor manager. 22 // accelerometer events are provided to the sensor manager.
19 class TestSensorManagerChromeOS : public content::SensorManagerChromeOS { 23 class TestSensorManagerChromeOS : public content::SensorManagerChromeOS {
20 public: 24 public:
21 TestSensorManagerChromeOS() {} 25 TestSensorManagerChromeOS() {}
22 ~TestSensorManagerChromeOS() override {}; 26 ~TestSensorManagerChromeOS() override {};
23 27
24 protected: 28 protected:
25 void StartObservingAccelerometer() override {} 29 void StartObservingAccelerometer() override {}
26 void StopObservingAccelerometer() override {} 30 void StopObservingAccelerometer() override {}
27 31
28 private: 32 private:
29 DISALLOW_COPY_AND_ASSIGN(TestSensorManagerChromeOS); 33 DISALLOW_COPY_AND_ASSIGN(TestSensorManagerChromeOS);
30 }; 34 };
31 35
32 } // namespace 36 } // namespace
33 37
34 namespace content { 38 namespace content {
35 39
36 class SensorManagerChromeOSTest : public testing::Test { 40 class SensorManagerChromeOSTest : public testing::Test {
37 public: 41 public:
38 SensorManagerChromeOSTest() { 42 SensorManagerChromeOSTest() {
43 motion_buffer_.reset(new DeviceMotionHardwareBuffer);
39 orientation_buffer_.reset(new DeviceOrientationHardwareBuffer); 44 orientation_buffer_.reset(new DeviceOrientationHardwareBuffer);
40 } 45 }
41 46
42 ~SensorManagerChromeOSTest() override {} 47 ~SensorManagerChromeOSTest() override {}
43 48
44 void OnAccelerationIncludingGravity(double x, double y, double z) { 49 void OnAccelerationIncludingGravity(double x, double y, double z) {
45 chromeos::AccelerometerUpdate update; 50 chromeos::AccelerometerUpdate update;
46 update.Set(chromeos::ACCELEROMETER_SOURCE_SCREEN, x, y, z); 51 update.Set(chromeos::ACCELEROMETER_SOURCE_SCREEN, x, y, z);
47 sensor_manager_->OnAccelerometerUpdated(update); 52 sensor_manager_->OnAccelerometerUpdated(update);
48 } 53 }
49 54
55 DeviceMotionHardwareBuffer* motion_buffer() { return motion_buffer_.get(); }
56
50 DeviceOrientationHardwareBuffer* orientation_buffer() { 57 DeviceOrientationHardwareBuffer* orientation_buffer() {
51 return orientation_buffer_.get(); 58 return orientation_buffer_.get();
52 } 59 }
53 60
54 SensorManagerChromeOS* sensor_manager() { return sensor_manager_.get(); } 61 SensorManagerChromeOS* sensor_manager() { return sensor_manager_.get(); }
55 62
56 // testing::Test: 63 // testing::Test:
57 void SetUp() override { 64 void SetUp() override {
58 testing::Test::SetUp(); 65 testing::Test::SetUp();
59 sensor_manager_.reset(new TestSensorManagerChromeOS); 66 sensor_manager_.reset(new TestSensorManagerChromeOS);
67 sensor_manager_->StartFetchingDeviceMotionData(motion_buffer_.get());
60 sensor_manager_->StartFetchingDeviceOrientationData( 68 sensor_manager_->StartFetchingDeviceOrientationData(
61 orientation_buffer_.get()); 69 orientation_buffer_.get());
62 } 70 }
63 71
64 void TearDown() override { 72 void TearDown() override {
73 sensor_manager_->StopFetchingDeviceMotionData();
65 sensor_manager_->StopFetchingDeviceOrientationData(); 74 sensor_manager_->StopFetchingDeviceOrientationData();
66 testing::Test::TearDown(); 75 testing::Test::TearDown();
67 } 76 }
68 77
69 private: 78 private:
70 scoped_ptr<TestSensorManagerChromeOS> sensor_manager_; 79 scoped_ptr<TestSensorManagerChromeOS> sensor_manager_;
71 80
81 scoped_ptr<DeviceMotionHardwareBuffer> motion_buffer_;
82
flackr 2015/02/18 22:34:09 nit: No need to separate these.
jonross 2015/02/25 00:09:57 Done.
72 scoped_ptr<DeviceOrientationHardwareBuffer> orientation_buffer_; 83 scoped_ptr<DeviceOrientationHardwareBuffer> orientation_buffer_;
73 84
74 DISALLOW_COPY_AND_ASSIGN(SensorManagerChromeOSTest); 85 DISALLOW_COPY_AND_ASSIGN(SensorManagerChromeOSTest);
75 }; 86 };
76 87
88 // Tests that starting to process motion data will update the associated buffer.
89 TEST_F(SensorManagerChromeOSTest, MotionBuffer) {
90 DeviceMotionHardwareBuffer* buffer = motion_buffer();
91 EXPECT_FLOAT_EQ(16.0f, buffer->data.interval);
92 EXPECT_FALSE(buffer->data.hasAccelerationIncludingGravityX);
93 EXPECT_FALSE(buffer->data.hasAccelerationIncludingGravityY);
94 EXPECT_FALSE(buffer->data.hasAccelerationIncludingGravityZ);
95 EXPECT_FALSE(buffer->data.hasAccelerationX);
96 EXPECT_FALSE(buffer->data.hasAccelerationY);
97 EXPECT_FALSE(buffer->data.hasAccelerationZ);
98 EXPECT_FALSE(buffer->data.hasRotationRateAlpha);
99 EXPECT_FALSE(buffer->data.hasRotationRateBeta);
100 EXPECT_FALSE(buffer->data.hasRotationRateGamma);
101
102 OnAccelerationIncludingGravity(0.0f, 0.0f, 1.0f);
103 EXPECT_TRUE(buffer->data.hasAccelerationIncludingGravityX);
104 EXPECT_TRUE(buffer->data.hasAccelerationIncludingGravityY);
105 EXPECT_TRUE(buffer->data.hasAccelerationIncludingGravityZ);
106 EXPECT_TRUE(buffer->data.hasAccelerationX);
107 EXPECT_TRUE(buffer->data.hasAccelerationY);
108 EXPECT_TRUE(buffer->data.hasAccelerationZ);
109 EXPECT_FALSE(buffer->data.hasRotationRateAlpha);
110 EXPECT_FALSE(buffer->data.hasRotationRateBeta);
111 EXPECT_FALSE(buffer->data.hasRotationRateGamma);
112 EXPECT_TRUE(buffer->data.allAvailableSensorsAreActive);
113
114 sensor_manager()->StopFetchingDeviceMotionData();
115 EXPECT_FALSE(buffer->data.allAvailableSensorsAreActive);
116 }
117
77 // Tests that starting to process orientation data will update the associated 118 // Tests that starting to process orientation data will update the associated
78 // buffer. 119 // buffer.
79 TEST_F(SensorManagerChromeOSTest, OrientationBuffer) { 120 TEST_F(SensorManagerChromeOSTest, OrientationBuffer) {
80 DeviceOrientationHardwareBuffer* buffer = orientation_buffer(); 121 DeviceOrientationHardwareBuffer* buffer = orientation_buffer();
81 EXPECT_TRUE(buffer->data.hasAbsolute); 122 EXPECT_TRUE(buffer->data.hasAbsolute);
82 EXPECT_FALSE(buffer->data.hasAlpha); 123 EXPECT_FALSE(buffer->data.hasAlpha);
83 EXPECT_FALSE(buffer->data.hasBeta); 124 EXPECT_FALSE(buffer->data.hasBeta);
84 EXPECT_FALSE(buffer->data.hasGamma); 125 EXPECT_FALSE(buffer->data.hasGamma);
85 EXPECT_FALSE(buffer->data.allAvailableSensorsAreActive); 126 EXPECT_FALSE(buffer->data.allAvailableSensorsAreActive);
86 127
87 OnAccelerationIncludingGravity(0.0f, 0.0f, 1.0f); 128 OnAccelerationIncludingGravity(0.0f, 0.0f, 1.0f);
88 EXPECT_FLOAT_EQ(0.0f, buffer->data.alpha); 129 EXPECT_FLOAT_EQ(0.0f, buffer->data.alpha);
89 EXPECT_FALSE(buffer->data.hasAlpha); 130 EXPECT_FALSE(buffer->data.hasAlpha);
90 EXPECT_TRUE(buffer->data.hasBeta); 131 EXPECT_TRUE(buffer->data.hasBeta);
91 EXPECT_TRUE(buffer->data.hasGamma); 132 EXPECT_TRUE(buffer->data.hasGamma);
92 EXPECT_TRUE(buffer->data.allAvailableSensorsAreActive); 133 EXPECT_TRUE(buffer->data.allAvailableSensorsAreActive);
93 134
94 sensor_manager()->StopFetchingDeviceOrientationData(); 135 sensor_manager()->StopFetchingDeviceOrientationData();
95 EXPECT_FALSE(buffer->data.allAvailableSensorsAreActive); 136 EXPECT_FALSE(buffer->data.allAvailableSensorsAreActive);
96 } 137 }
97 138
98 // Tests a device resting flat. 139 // Tests a device resting flat.
99 TEST_F(SensorManagerChromeOSTest, NeutralOrientation) { 140 TEST_F(SensorManagerChromeOSTest, NeutralOrientation) {
100 OnAccelerationIncludingGravity(0.0f, 0.0f, -kMeanGravity); 141 OnAccelerationIncludingGravity(0.0f, 0.0f, -kMeanGravity);
101 DeviceOrientationHardwareBuffer* buffer = orientation_buffer(); 142
102 EXPECT_FLOAT_EQ(0.0f, buffer->data.beta); 143 DeviceMotionHardwareBuffer* motion = motion_buffer();
103 EXPECT_FLOAT_EQ(0.0f, buffer->data.gamma); 144 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityX);
145 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY);
146 EXPECT_FLOAT_EQ(-kMeanGravity, motion->data.accelerationIncludingGravityZ);
147
148 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationX);
149 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationY);
150 EXPECT_FLOAT_EQ(-kMeanGravity * kGravityFilterRatio,
151 motion->data.accelerationZ);
152
153 DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
154 EXPECT_FLOAT_EQ(0.0f, orientation->data.beta);
155 EXPECT_FLOAT_EQ(0.0f, orientation->data.gamma);
104 } 156 }
105 157
106 // Tests an upside-down device, such that the W3C boundary [-180,180) causes the 158 // Tests an upside-down device, such that the W3C boundary [-180,180) causes the
107 // beta value to become negative. 159 // beta value to become negative.
108 TEST_F(SensorManagerChromeOSTest, UpsideDown) { 160 TEST_F(SensorManagerChromeOSTest, UpsideDown) {
109 OnAccelerationIncludingGravity(0.0f, 0.0f, kMeanGravity); 161 OnAccelerationIncludingGravity(0.0f, 0.0f, kMeanGravity);
110 DeviceOrientationHardwareBuffer* buffer = orientation_buffer(); 162
111 EXPECT_FLOAT_EQ(-180.0f, buffer->data.beta); 163 DeviceMotionHardwareBuffer* motion = motion_buffer();
112 EXPECT_FLOAT_EQ(0.0f, buffer->data.gamma); 164 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityX);
165 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY);
166 EXPECT_FLOAT_EQ(kMeanGravity, motion->data.accelerationIncludingGravityZ);
167
168 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationX);
169 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationY);
170 EXPECT_FLOAT_EQ(kMeanGravity * kGravityFilterRatio,
171 motion->data.accelerationZ);
172
173 DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
174 EXPECT_FLOAT_EQ(-180.0f, orientation->data.beta);
175 EXPECT_FLOAT_EQ(0.0f, orientation->data.gamma);
113 } 176 }
114 177
115 // Tests for positive beta value before the device is completely upside-down 178 // Tests for positive beta value before the device is completely upside-down
116 TEST_F(SensorManagerChromeOSTest, BeforeUpsideDownBoundary) { 179 TEST_F(SensorManagerChromeOSTest, BeforeUpsideDownBoundary) {
117 OnAccelerationIncludingGravity(0.0f, -kMeanGravity / 2.0f, 180 OnAccelerationIncludingGravity(0.0f, -kMeanGravity / 2.0f,
118 kMeanGravity / 2.0f); 181 kMeanGravity / 2.0f);
119 DeviceOrientationHardwareBuffer* buffer = orientation_buffer(); 182
120 EXPECT_FLOAT_EQ(135.0f, buffer->data.beta); 183 DeviceMotionHardwareBuffer* motion = motion_buffer();
121 EXPECT_FLOAT_EQ(0.0f, buffer->data.gamma); 184 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityX);
185 EXPECT_FLOAT_EQ(-kMeanGravity / 2.0f,
186 motion->data.accelerationIncludingGravityY);
187 EXPECT_FLOAT_EQ(kMeanGravity / 2.0f,
188 motion->data.accelerationIncludingGravityZ);
189
190 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationX);
191 EXPECT_FLOAT_EQ(-kMeanGravity / 2.0f * kGravityFilterRatio,
192 motion->data.accelerationY);
193 EXPECT_FLOAT_EQ(kMeanGravity / 2.0f * kGravityFilterRatio,
194 motion->data.accelerationZ);
195
196 DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
197 EXPECT_FLOAT_EQ(135.0f, orientation->data.beta);
198 EXPECT_FLOAT_EQ(0.0f, orientation->data.gamma);
122 } 199 }
123 200
124 // Tests a device lying on its left-edge. 201 // Tests a device lying on its left-edge.
125 TEST_F(SensorManagerChromeOSTest, LeftEdge) { 202 TEST_F(SensorManagerChromeOSTest, LeftEdge) {
126 OnAccelerationIncludingGravity(-kMeanGravity, 0.0f, 0.0f); 203 OnAccelerationIncludingGravity(-kMeanGravity, 0.0f, 0.0f);
127 DeviceOrientationHardwareBuffer* buffer = orientation_buffer(); 204
128 EXPECT_FLOAT_EQ(0.0f, buffer->data.beta); 205 DeviceMotionHardwareBuffer* motion = motion_buffer();
129 EXPECT_FLOAT_EQ(-90.0f, buffer->data.gamma); 206 EXPECT_FLOAT_EQ(-kMeanGravity, motion->data.accelerationIncludingGravityX);
207 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY);
208 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityZ);
209
210 EXPECT_FLOAT_EQ(-kMeanGravity * kGravityFilterRatio,
211 motion->data.accelerationX);
212 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationY);
213 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationZ);
214
215 DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
216 EXPECT_FLOAT_EQ(0.0f, orientation->data.beta);
217 EXPECT_FLOAT_EQ(-90.0f, orientation->data.gamma);
130 } 218 }
131 219
132 // Tests a device lying on its right-edge, such that the W3C boundary [-90,90) 220 // Tests a device lying on its right-edge, such that the W3C boundary [-90,90)
133 // causes the gamma value to become negative. 221 // causes the gamma value to become negative.
134 TEST_F(SensorManagerChromeOSTest, RightEdge) { 222 TEST_F(SensorManagerChromeOSTest, RightEdge) {
135 OnAccelerationIncludingGravity(kMeanGravity, 0.0f, 0.0f); 223 OnAccelerationIncludingGravity(kMeanGravity, 0.0f, 0.0f);
136 DeviceOrientationHardwareBuffer* buffer = orientation_buffer(); 224
137 EXPECT_FLOAT_EQ(0.0f, buffer->data.beta); 225 DeviceMotionHardwareBuffer* motion = motion_buffer();
138 EXPECT_FLOAT_EQ(-90.0f, buffer->data.gamma); 226 EXPECT_FLOAT_EQ(kMeanGravity, motion->data.accelerationIncludingGravityX);
227 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY);
228 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityZ);
229
230 EXPECT_FLOAT_EQ(kMeanGravity * kGravityFilterRatio,
231 motion->data.accelerationX);
232 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationY);
233 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationZ);
234
235 DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
236 EXPECT_FLOAT_EQ(0.0f, orientation->data.beta);
237 EXPECT_FLOAT_EQ(-90.0f, orientation->data.gamma);
139 } 238 }
140 239
141 // Tests for positive gamma value before the device is completely on its right 240 // Tests for positive gamma value before the device is completely on its right
142 // side. 241 // side.
143 TEST_F(SensorManagerChromeOSTest, BeforeRightEdgeBoundary) { 242 TEST_F(SensorManagerChromeOSTest, BeforeRightEdgeBoundary) {
144 OnAccelerationIncludingGravity(kMeanGravity / 2.0f, 0.0f, 243 OnAccelerationIncludingGravity(kMeanGravity / 2.0f, 0.0f,
145 -kMeanGravity / 2.0f); 244 -kMeanGravity / 2.0f);
146 DeviceOrientationHardwareBuffer* buffer = orientation_buffer(); 245
147 EXPECT_FLOAT_EQ(0.0f, buffer->data.beta); 246 DeviceMotionHardwareBuffer* motion = motion_buffer();
148 EXPECT_FLOAT_EQ(45.0f, buffer->data.gamma); 247 EXPECT_FLOAT_EQ(kMeanGravity / 2.0f,
248 motion->data.accelerationIncludingGravityX);
249 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationIncludingGravityY);
250 EXPECT_FLOAT_EQ(-kMeanGravity / 2.0f,
251 motion->data.accelerationIncludingGravityZ);
252
253 EXPECT_FLOAT_EQ(kMeanGravity / 2.0f * kGravityFilterRatio,
254 motion->data.accelerationX);
255 EXPECT_FLOAT_EQ(0.0f, motion->data.accelerationY);
256 EXPECT_FLOAT_EQ(-kMeanGravity / 2.0f * kGravityFilterRatio,
257 motion->data.accelerationZ);
258
259 DeviceOrientationHardwareBuffer* orientation = orientation_buffer();
260 EXPECT_FLOAT_EQ(0.0f, orientation->data.beta);
261 EXPECT_FLOAT_EQ(45.0f, orientation->data.gamma);
262 }
263
264 // Tests that the gravity component of gravity is increasingly filtered out of
265 // the acceleration data as the device remains stationary.
266 TEST_F(SensorManagerChromeOSTest, GravityFilterComletesForStationaryDevice) {
267 OnAccelerationIncludingGravity(0.0f, 0.0f, -kMeanGravity);
268
269 DeviceMotionHardwareBuffer* motion = motion_buffer();
270 EXPECT_FLOAT_EQ(-kMeanGravity, motion->data.accelerationIncludingGravityZ);
271 EXPECT_FLOAT_EQ(-kMeanGravity * kGravityFilterRatio,
272 motion->data.accelerationZ);
273
274 double previous_acceleration = motion->data.accelerationZ;
275 double acceleration;
276 // A third of a second, the filter will have removed enough gravity to have
277 // approached the asymptote of 0.
278 for (int i = 0; i < 20; ++i) {
279 OnAccelerationIncludingGravity(0.0f, 0.0f, -kMeanGravity);
280 acceleration = motion->data.accelerationZ;
281 EXPECT_LT(previous_acceleration, acceleration);
282 previous_acceleration = acceleration;
283 }
284 }
285
286 // Tests that after gravity has been filtered out, that other acceleration
287 // sources as reported.
288 TEST_F(SensorManagerChromeOSTest, NonGravityAcceleration) {
289 for (int i = 0; i < 20; ++i)
290 OnAccelerationIncludingGravity(0.0f, 0.0f, -kMeanGravity);
291 DeviceMotionHardwareBuffer* motion = motion_buffer();
292 gfx::Vector3dF stable(motion->data.accelerationX, motion->data.accelerationY,
293 motion->data.accelerationZ);
294
295 OnAccelerationIncludingGravity(10.0f, 0.0f, -kMeanGravity);
296 EXPECT_FLOAT_EQ(8.0f, motion->data.accelerationX);
297 gfx::Vector3dF accelerating(motion->data.accelerationX,
298 motion->data.accelerationY,
299 motion->data.accelerationZ);
300 EXPECT_GT(accelerating.Length(), stable.Length());
301 }
302
303 // Tests that after gravity has been filtered out, that acceleration opposing
304 // gravity is reported, even though the instantaneous acceleration with gravity
305 // is 0.
306 TEST_F(SensorManagerChromeOSTest, AccelerationAgainstGravity) {
307 for (int i = 0; i < 20; ++i)
308 OnAccelerationIncludingGravity(0.0f, 0.0f, -kMeanGravity);
309 DeviceMotionHardwareBuffer* motion = motion_buffer();
310 double stable_gravity = motion->data.accelerationZ;
311
312 OnAccelerationIncludingGravity(0.0f, 0.0f, 0.0f);
313 EXPECT_LT(stable_gravity, motion->data.accelerationZ);
149 } 314 }
150 315
151 } // namespace content 316 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698