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

Side by Side Diff: content/renderer/device_sensors/device_motion_event_pump_unittest.cc

Issue 2953263002: Revert of Reland: Refactor DeviceMotionEventPump to use //device/generic_sensor instead of //device/sensors (Closed)
Patch Set: Created 3 years, 5 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
« no previous file with comments | « content/renderer/device_sensors/device_motion_event_pump.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/renderer/device_sensors/device_motion_event_pump.h" 5 #include "content/renderer/device_sensors/device_motion_event_pump.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/location.h" 11 #include "base/location.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ptr_util.h"
15 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
16 #include "base/run_loop.h" 15 #include "base/run_loop.h"
17 #include "base/single_thread_task_runner.h" 16 #include "base/single_thread_task_runner.h"
18 #include "base/threading/thread_task_runner_handle.h" 17 #include "base/threading/thread_task_runner_handle.h"
19 #include "base/time/time.h"
20 #include "content/public/test/test_utils.h" 18 #include "content/public/test/test_utils.h"
21 #include "device/generic_sensor/public/cpp/sensor_reading.h" 19 #include "device/sensors/public/cpp/device_motion_hardware_buffer.h"
22 #include "device/generic_sensor/public/interfaces/sensor.mojom.h"
23 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom.h"
24 #include "device/sensors/public/cpp/motion_data.h"
25 #include "mojo/public/cpp/bindings/interface_request.h"
26 #include "mojo/public/cpp/system/buffer.h" 20 #include "mojo/public/cpp/system/buffer.h"
27 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
28 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic eMotionListener.h" 22 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic eMotionListener.h"
29 23
30 namespace {
31
32 constexpr uint64_t kReadingBufferSize =
33 sizeof(device::SensorReadingSharedBuffer);
34
35 constexpr uint64_t kSharedBufferSizeInBytes =
36 kReadingBufferSize * static_cast<uint64_t>(device::mojom::SensorType::LAST);
37
38 } // namespace
39
40 namespace content { 24 namespace content {
41 25
42 class MockDeviceMotionListener : public blink::WebDeviceMotionListener { 26 class MockDeviceMotionListener : public blink::WebDeviceMotionListener {
43 public: 27 public:
44 MockDeviceMotionListener() 28 MockDeviceMotionListener()
45 : did_change_device_motion_(false), number_of_events_(0) { 29 : did_change_device_motion_(false), number_of_events_(0) {
46 memset(&data_, 0, sizeof(data_)); 30 memset(&data_, 0, sizeof(data_));
47 } 31 }
48 ~MockDeviceMotionListener() override {} 32 ~MockDeviceMotionListener() override {}
49 33
(...skipping 15 matching lines...) Expand all
65 bool did_change_device_motion_; 49 bool did_change_device_motion_;
66 int number_of_events_; 50 int number_of_events_;
67 device::MotionData data_; 51 device::MotionData data_;
68 52
69 DISALLOW_COPY_AND_ASSIGN(MockDeviceMotionListener); 53 DISALLOW_COPY_AND_ASSIGN(MockDeviceMotionListener);
70 }; 54 };
71 55
72 class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump { 56 class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump {
73 public: 57 public:
74 DeviceMotionEventPumpForTesting() 58 DeviceMotionEventPumpForTesting()
75 : DeviceMotionEventPump(nullptr), stop_on_fire_event_(true) {} 59 : DeviceMotionEventPump(0), stop_on_fire_event_(true) {}
76 ~DeviceMotionEventPumpForTesting() override {} 60 ~DeviceMotionEventPumpForTesting() override {}
77 61
78 // DeviceMotionEventPump:
79 void SendStartMessage() override {
80 accelerometer_.mode = device::mojom::ReportingMode::CONTINUOUS;
81 linear_acceleration_sensor_.mode = device::mojom::ReportingMode::ON_CHANGE;
82 gyroscope_.mode = device::mojom::ReportingMode::CONTINUOUS;
83
84 shared_memory_ = mojo::SharedBufferHandle::Create(kSharedBufferSizeInBytes);
85
86 accelerometer_.shared_buffer = shared_memory_->MapAtOffset(
87 kReadingBufferSize,
88 device::SensorReadingSharedBuffer::GetOffset(accelerometer_.type));
89 accelerometer_buffer_ = static_cast<device::SensorReadingSharedBuffer*>(
90 accelerometer_.shared_buffer.get());
91
92 linear_acceleration_sensor_.shared_buffer = shared_memory_->MapAtOffset(
93 kReadingBufferSize, device::SensorReadingSharedBuffer::GetOffset(
94 linear_acceleration_sensor_.type));
95 linear_acceleration_sensor_buffer_ =
96 static_cast<device::SensorReadingSharedBuffer*>(
97 linear_acceleration_sensor_.shared_buffer.get());
98
99 gyroscope_.shared_buffer = shared_memory_->MapAtOffset(
100 kReadingBufferSize,
101 device::SensorReadingSharedBuffer::GetOffset(gyroscope_.type));
102 gyroscope_buffer_ = static_cast<device::SensorReadingSharedBuffer*>(
103 gyroscope_.shared_buffer.get());
104 }
105
106 void StartFireEvent() { DeviceMotionEventPump::DidStart(); }
107
108 void SetAccelerometerSensorData(bool active,
109 double d0,
110 double d1,
111 double d2) {
112 if (active) {
113 mojo::MakeRequest(&accelerometer_.sensor);
114 accelerometer_buffer_->reading.timestamp =
115 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
116 accelerometer_buffer_->reading.values[0].value() = d0;
117 accelerometer_buffer_->reading.values[1].value() = d1;
118 accelerometer_buffer_->reading.values[2].value() = d2;
119 } else {
120 accelerometer_.sensor.reset();
121 }
122 }
123
124 void SetLinearAccelerationSensorData(bool active,
125 double d0,
126 double d1,
127 double d2) {
128 if (active) {
129 mojo::MakeRequest(&linear_acceleration_sensor_.sensor);
130 linear_acceleration_sensor_buffer_->reading.timestamp =
131 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
132 linear_acceleration_sensor_buffer_->reading.values[0].value() = d0;
133 linear_acceleration_sensor_buffer_->reading.values[1].value() = d1;
134 linear_acceleration_sensor_buffer_->reading.values[2].value() = d2;
135 } else {
136 linear_acceleration_sensor_.sensor.reset();
137 }
138 }
139
140 void SetGyroscopeSensorData(bool active, double d0, double d1, double d2) {
141 if (active) {
142 mojo::MakeRequest(&gyroscope_.sensor);
143 gyroscope_buffer_->reading.timestamp =
144 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
145 gyroscope_buffer_->reading.values[0].value() = d0;
146 gyroscope_buffer_->reading.values[1].value() = d1;
147 gyroscope_buffer_->reading.values[2].value() = d2;
148 } else {
149 gyroscope_.sensor.reset();
150 }
151 }
152
153 void set_stop_on_fire_event(bool stop_on_fire_event) { 62 void set_stop_on_fire_event(bool stop_on_fire_event) {
154 stop_on_fire_event_ = stop_on_fire_event; 63 stop_on_fire_event_ = stop_on_fire_event;
155 } 64 }
156 65
157 bool stop_on_fire_event() { return stop_on_fire_event_; } 66 bool stop_on_fire_event() { return stop_on_fire_event_; }
158 67
159 int pump_delay_microseconds() const { return kDefaultPumpDelayMicroseconds; } 68 int pump_delay_microseconds() const { return pump_delay_microseconds_; }
160 69
161 protected: 70 void DidStart(mojo::ScopedSharedBufferHandle renderer_handle) {
162 // DeviceMotionEventPump: 71 DeviceMotionEventPump::DidStart(std::move(renderer_handle));
72 }
73 void SendStartMessage() override {}
74 void SendStopMessage() override {}
163 void FireEvent() override { 75 void FireEvent() override {
164 DeviceMotionEventPump::FireEvent(); 76 DeviceMotionEventPump::FireEvent();
165 if (stop_on_fire_event_) { 77 if (stop_on_fire_event_) {
166 Stop(); 78 Stop();
167 base::MessageLoop::current()->QuitWhenIdle(); 79 base::MessageLoop::current()->QuitWhenIdle();
168 } 80 }
169 } 81 }
170 82
171 private: 83 private:
172 bool stop_on_fire_event_; 84 bool stop_on_fire_event_;
173 mojo::ScopedSharedBufferHandle shared_memory_;
174 device::SensorReadingSharedBuffer* accelerometer_buffer_;
175 device::SensorReadingSharedBuffer* linear_acceleration_sensor_buffer_;
176 device::SensorReadingSharedBuffer* gyroscope_buffer_;
177 85
178 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpForTesting); 86 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpForTesting);
179 }; 87 };
180 88
181 class DeviceMotionEventPumpTest : public testing::Test { 89 class DeviceMotionEventPumpTest : public testing::Test {
182 public: 90 public:
183 DeviceMotionEventPumpTest() = default; 91 DeviceMotionEventPumpTest() = default;
184 92
185 protected: 93 protected:
186 void SetUp() override { 94 void SetUp() override {
187 listener_.reset(new MockDeviceMotionListener); 95 listener_.reset(new MockDeviceMotionListener);
188 motion_pump_.reset(new DeviceMotionEventPumpForTesting()); 96 motion_pump_.reset(new DeviceMotionEventPumpForTesting);
97 shared_memory_ = mojo::SharedBufferHandle::Create(
98 sizeof(device::DeviceMotionHardwareBuffer));
99 mapping_ = shared_memory_->Map(sizeof(device::DeviceMotionHardwareBuffer));
100 ASSERT_TRUE(mapping_);
101 memset(buffer(), 0, sizeof(device::DeviceMotionHardwareBuffer));
102 }
103
104 void InitBuffer(bool allAvailableSensorsActive) {
105 device::MotionData& data = buffer()->data;
106 data.acceleration_x = 1;
107 data.has_acceleration_x = true;
108 data.acceleration_y = 2;
109 data.has_acceleration_y = true;
110 data.acceleration_z = 3;
111 data.has_acceleration_z = true;
112 data.all_available_sensors_are_active = allAvailableSensorsActive;
189 } 113 }
190 114
191 MockDeviceMotionListener* listener() { return listener_.get(); } 115 MockDeviceMotionListener* listener() { return listener_.get(); }
192 DeviceMotionEventPumpForTesting* motion_pump() { return motion_pump_.get(); } 116 DeviceMotionEventPumpForTesting* motion_pump() { return motion_pump_.get(); }
117 mojo::ScopedSharedBufferHandle handle() {
118 return shared_memory_->Clone(
119 mojo::SharedBufferHandle::AccessMode::READ_ONLY);
120 }
121 device::DeviceMotionHardwareBuffer* buffer() {
122 return reinterpret_cast<device::DeviceMotionHardwareBuffer*>(
123 mapping_.get());
124 }
193 125
194 private: 126 private:
195 base::MessageLoop loop_; 127 base::MessageLoop loop_;
196 std::unique_ptr<MockDeviceMotionListener> listener_; 128 std::unique_ptr<MockDeviceMotionListener> listener_;
197 std::unique_ptr<DeviceMotionEventPumpForTesting> motion_pump_; 129 std::unique_ptr<DeviceMotionEventPumpForTesting> motion_pump_;
130 mojo::ScopedSharedBufferHandle shared_memory_;
131 mojo::ScopedSharedBufferMapping mapping_;
198 132
199 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpTest); 133 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpTest);
200 }; 134 };
201 135
202 TEST_F(DeviceMotionEventPumpTest, AllSensorsAreActive) { 136 TEST_F(DeviceMotionEventPumpTest, DidStartPolling) {
137 InitBuffer(true);
138
203 motion_pump()->Start(listener()); 139 motion_pump()->Start(listener());
204 motion_pump()->SetAccelerometerSensorData(true /* active */, 1, 2, 3); 140 motion_pump()->DidStart(handle());
205 motion_pump()->SetLinearAccelerationSensorData(true /* active */, 4, 5, 6);
206 motion_pump()->SetGyroscopeSensorData(true /* active */, 7, 8, 9);
207 motion_pump()->StartFireEvent();
208 141
209 base::RunLoop().Run(); 142 base::RunLoop().Run();
210 143
211 device::MotionData received_data = listener()->data(); 144 const device::MotionData& received_data = listener()->data();
212 EXPECT_TRUE(listener()->did_change_device_motion()); 145 EXPECT_TRUE(listener()->did_change_device_motion());
213
214 EXPECT_TRUE(received_data.has_acceleration_including_gravity_x);
215 EXPECT_EQ(1, received_data.acceleration_including_gravity_x);
216 EXPECT_TRUE(received_data.has_acceleration_including_gravity_y);
217 EXPECT_EQ(2, received_data.acceleration_including_gravity_y);
218 EXPECT_TRUE(received_data.has_acceleration_including_gravity_z);
219 EXPECT_EQ(3, received_data.acceleration_including_gravity_z);
220
221 EXPECT_TRUE(received_data.has_acceleration_x); 146 EXPECT_TRUE(received_data.has_acceleration_x);
222 EXPECT_EQ(4, received_data.acceleration_x); 147 EXPECT_EQ(1, static_cast<double>(received_data.acceleration_x));
148 EXPECT_TRUE(received_data.has_acceleration_x);
149 EXPECT_EQ(2, static_cast<double>(received_data.acceleration_y));
223 EXPECT_TRUE(received_data.has_acceleration_y); 150 EXPECT_TRUE(received_data.has_acceleration_y);
224 EXPECT_EQ(5, received_data.acceleration_y); 151 EXPECT_EQ(3, static_cast<double>(received_data.acceleration_z));
225 EXPECT_TRUE(received_data.has_acceleration_z); 152 EXPECT_TRUE(received_data.has_acceleration_z);
226 EXPECT_EQ(6, received_data.acceleration_z); 153 EXPECT_FALSE(received_data.has_acceleration_including_gravity_x);
227 154 EXPECT_FALSE(received_data.has_acceleration_including_gravity_y);
228 EXPECT_TRUE(received_data.has_rotation_rate_alpha); 155 EXPECT_FALSE(received_data.has_acceleration_including_gravity_z);
229 EXPECT_EQ(7, received_data.rotation_rate_alpha); 156 EXPECT_FALSE(received_data.has_rotation_rate_alpha);
230 EXPECT_TRUE(received_data.has_rotation_rate_beta); 157 EXPECT_FALSE(received_data.has_rotation_rate_beta);
231 EXPECT_EQ(8, received_data.rotation_rate_beta); 158 EXPECT_FALSE(received_data.has_rotation_rate_gamma);
232 EXPECT_TRUE(received_data.has_rotation_rate_gamma);
233 EXPECT_EQ(9, received_data.rotation_rate_gamma);
234 } 159 }
235 160
236 TEST_F(DeviceMotionEventPumpTest, TwoSensorsAreActive) { 161 TEST_F(DeviceMotionEventPumpTest, DidStartPollingNotAllSensorsActive) {
162 InitBuffer(false);
163
237 motion_pump()->Start(listener()); 164 motion_pump()->Start(listener());
238 motion_pump()->SetAccelerometerSensorData(true /* active */, 1, 2, 3); 165 motion_pump()->DidStart(handle());
239 motion_pump()->SetLinearAccelerationSensorData(false /* active */, 4, 5, 6);
240 motion_pump()->SetGyroscopeSensorData(true /* active */, 7, 8, 9);
241 motion_pump()->StartFireEvent();
242 166
243 base::RunLoop().Run(); 167 base::RunLoop().Run();
244 168
245 device::MotionData received_data = listener()->data(); 169 const device::MotionData& received_data = listener()->data();
246 EXPECT_TRUE(listener()->did_change_device_motion()); 170 // No change in device motion because all_available_sensors_are_active is
247 171 // false.
248 EXPECT_TRUE(received_data.has_acceleration_including_gravity_x); 172 EXPECT_FALSE(listener()->did_change_device_motion());
249 EXPECT_EQ(1, received_data.acceleration_including_gravity_x); 173 EXPECT_FALSE(received_data.has_acceleration_x);
250 EXPECT_TRUE(received_data.has_acceleration_including_gravity_y);
251 EXPECT_EQ(2, received_data.acceleration_including_gravity_y);
252 EXPECT_TRUE(received_data.has_acceleration_including_gravity_z);
253 EXPECT_EQ(3, received_data.acceleration_including_gravity_z);
254
255 EXPECT_FALSE(received_data.has_acceleration_x); 174 EXPECT_FALSE(received_data.has_acceleration_x);
256 EXPECT_FALSE(received_data.has_acceleration_y); 175 EXPECT_FALSE(received_data.has_acceleration_y);
257 EXPECT_FALSE(received_data.has_acceleration_z); 176 EXPECT_FALSE(received_data.has_acceleration_z);
258
259 EXPECT_TRUE(received_data.has_rotation_rate_alpha);
260 EXPECT_EQ(7, received_data.rotation_rate_alpha);
261 EXPECT_TRUE(received_data.has_rotation_rate_beta);
262 EXPECT_EQ(8, received_data.rotation_rate_beta);
263 EXPECT_TRUE(received_data.has_rotation_rate_gamma);
264 EXPECT_EQ(9, received_data.rotation_rate_gamma);
265 }
266
267 TEST_F(DeviceMotionEventPumpTest, NoActiveSensors) {
268 motion_pump()->Start(listener());
269 motion_pump()->StartFireEvent();
270
271 base::RunLoop().Run();
272
273 device::MotionData received_data = listener()->data();
274 EXPECT_TRUE(listener()->did_change_device_motion());
275
276 EXPECT_FALSE(received_data.has_acceleration_x);
277 EXPECT_FALSE(received_data.has_acceleration_y);
278 EXPECT_FALSE(received_data.has_acceleration_z);
279
280 EXPECT_FALSE(received_data.has_acceleration_including_gravity_x); 177 EXPECT_FALSE(received_data.has_acceleration_including_gravity_x);
281 EXPECT_FALSE(received_data.has_acceleration_including_gravity_y); 178 EXPECT_FALSE(received_data.has_acceleration_including_gravity_y);
282 EXPECT_FALSE(received_data.has_acceleration_including_gravity_z); 179 EXPECT_FALSE(received_data.has_acceleration_including_gravity_z);
283
284 EXPECT_FALSE(received_data.has_rotation_rate_alpha); 180 EXPECT_FALSE(received_data.has_rotation_rate_alpha);
285 EXPECT_FALSE(received_data.has_rotation_rate_beta); 181 EXPECT_FALSE(received_data.has_rotation_rate_beta);
286 EXPECT_FALSE(received_data.has_rotation_rate_gamma); 182 EXPECT_FALSE(received_data.has_rotation_rate_gamma);
287 } 183 }
288 184
289 // Confirm that the frequency of pumping events is not greater than 60Hz. A rate 185 // Confirm that the frequency of pumping events is not greater than 60Hz. A rate
290 // above 60Hz would allow for the detection of keystrokes (crbug.com/421691) 186 // above 60Hz would allow for the detection of keystrokes (crbug.com/421691)
291 TEST_F(DeviceMotionEventPumpTest, PumpThrottlesEventRate) { 187 TEST_F(DeviceMotionEventPumpTest, PumpThrottlesEventRate) {
292 // Confirm that the delay for pumping events is 60 Hz. 188 // Confirm that the delay for pumping events is 60 Hz.
293 EXPECT_GE(60, base::Time::kMicrosecondsPerSecond / 189 EXPECT_GE(60, base::Time::kMicrosecondsPerSecond /
294 motion_pump()->pump_delay_microseconds()); 190 motion_pump()->pump_delay_microseconds());
295 191
296 motion_pump()->Start(listener()); 192 InitBuffer(true);
297 motion_pump()->SetLinearAccelerationSensorData(true /* active */, 4, 5, 6);
298 193
299 motion_pump()->set_stop_on_fire_event(false); 194 motion_pump()->set_stop_on_fire_event(false);
300 motion_pump()->StartFireEvent(); 195 motion_pump()->Start(listener());
196 motion_pump()->DidStart(handle());
301 197
302 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 198 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
303 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), 199 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(),
304 base::TimeDelta::FromMilliseconds(100)); 200 base::TimeDelta::FromMilliseconds(100));
305 base::RunLoop().Run(); 201 base::RunLoop().Run();
306 motion_pump()->Stop(); 202 motion_pump()->Stop();
307 203
308 // Check that the blink::WebDeviceMotionListener does not receive excess 204 // Check that the blink::WebDeviceMotionListener does not receive excess
309 // events. 205 // events.
310 EXPECT_TRUE(listener()->did_change_device_motion()); 206 EXPECT_TRUE(listener()->did_change_device_motion());
311 EXPECT_GE(6, listener()->number_of_events()); 207 EXPECT_GE(6, listener()->number_of_events());
312 } 208 }
313 209
314 } // namespace content 210 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/device_sensors/device_motion_event_pump.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698