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

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

Powered by Google App Engine
This is Rietveld 408576698