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

Powered by Google App Engine
This is Rietveld 408576698