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

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

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

Powered by Google App Engine
This is Rietveld 408576698