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

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

Powered by Google App Engine
This is Rietveld 408576698