OLD | NEW |
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 "device/sensors/public/cpp/motion_data.h" |
| 25 #include "mojo/public/cpp/bindings/interface_request.h" |
20 #include "mojo/public/cpp/system/buffer.h" | 26 #include "mojo/public/cpp/system/buffer.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.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 |
(...skipping 15 matching lines...) Expand all Loading... |
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 device::MotionData 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_.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 |
62 void set_stop_on_fire_event(bool stop_on_fire_event) { | 153 void set_stop_on_fire_event(bool stop_on_fire_event) { |
63 stop_on_fire_event_ = stop_on_fire_event; | 154 stop_on_fire_event_ = stop_on_fire_event; |
64 } | 155 } |
65 | 156 |
66 bool stop_on_fire_event() { return stop_on_fire_event_; } | 157 bool stop_on_fire_event() { return stop_on_fire_event_; } |
67 | 158 |
68 int pump_delay_microseconds() const { return pump_delay_microseconds_; } | 159 int pump_delay_microseconds() const { return kDefaultPumpDelayMicroseconds; } |
69 | 160 |
70 void DidStart(mojo::ScopedSharedBufferHandle renderer_handle) { | 161 protected: |
71 DeviceMotionEventPump::DidStart(std::move(renderer_handle)); | 162 // DeviceMotionEventPump: |
72 } | |
73 void SendStartMessage() override {} | |
74 void SendStopMessage() override {} | |
75 void FireEvent() override { | 163 void FireEvent() override { |
76 DeviceMotionEventPump::FireEvent(); | 164 DeviceMotionEventPump::FireEvent(); |
77 if (stop_on_fire_event_) { | 165 if (stop_on_fire_event_) { |
78 Stop(); | 166 Stop(); |
79 base::MessageLoop::current()->QuitWhenIdle(); | 167 base::MessageLoop::current()->QuitWhenIdle(); |
80 } | 168 } |
81 } | 169 } |
82 | 170 |
83 private: | 171 private: |
84 bool stop_on_fire_event_; | 172 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_; |
85 | 177 |
86 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpForTesting); | 178 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpForTesting); |
87 }; | 179 }; |
88 | 180 |
89 class DeviceMotionEventPumpTest : public testing::Test { | 181 class DeviceMotionEventPumpTest : public testing::Test { |
90 public: | 182 public: |
91 DeviceMotionEventPumpTest() = default; | 183 DeviceMotionEventPumpTest() = default; |
92 | 184 |
93 protected: | 185 protected: |
94 void SetUp() override { | 186 void SetUp() override { |
95 listener_.reset(new MockDeviceMotionListener); | 187 listener_.reset(new MockDeviceMotionListener); |
96 motion_pump_.reset(new DeviceMotionEventPumpForTesting); | 188 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 } | 189 } |
114 | 190 |
115 MockDeviceMotionListener* listener() { return listener_.get(); } | 191 MockDeviceMotionListener* listener() { return listener_.get(); } |
116 DeviceMotionEventPumpForTesting* motion_pump() { return motion_pump_.get(); } | 192 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 | 193 |
126 private: | 194 private: |
127 base::MessageLoop loop_; | 195 base::MessageLoop loop_; |
128 std::unique_ptr<MockDeviceMotionListener> listener_; | 196 std::unique_ptr<MockDeviceMotionListener> listener_; |
129 std::unique_ptr<DeviceMotionEventPumpForTesting> motion_pump_; | 197 std::unique_ptr<DeviceMotionEventPumpForTesting> motion_pump_; |
130 mojo::ScopedSharedBufferHandle shared_memory_; | |
131 mojo::ScopedSharedBufferMapping mapping_; | |
132 | 198 |
133 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpTest); | 199 DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpTest); |
134 }; | 200 }; |
135 | 201 |
136 TEST_F(DeviceMotionEventPumpTest, DidStartPolling) { | 202 TEST_F(DeviceMotionEventPumpTest, AllSensorsAreActive) { |
137 InitBuffer(true); | |
138 | |
139 motion_pump()->Start(listener()); | 203 motion_pump()->Start(listener()); |
140 motion_pump()->DidStart(handle()); | 204 motion_pump()->SetAccelerometerSensorData(true /* active */, 1, 2, 3); |
| 205 motion_pump()->SetLinearAccelerationSensorData(true /* active */, 4, 5, 6); |
| 206 motion_pump()->SetGyroscopeSensorData(true /* active */, 7, 8, 9); |
| 207 motion_pump()->StartFireEvent(); |
141 | 208 |
142 base::RunLoop().Run(); | 209 base::RunLoop().Run(); |
143 | 210 |
144 const device::MotionData& received_data = listener()->data(); | 211 device::MotionData received_data = listener()->data(); |
145 EXPECT_TRUE(listener()->did_change_device_motion()); | 212 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 |
146 EXPECT_TRUE(received_data.has_acceleration_x); | 221 EXPECT_TRUE(received_data.has_acceleration_x); |
147 EXPECT_EQ(1, static_cast<double>(received_data.acceleration_x)); | 222 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); | 223 EXPECT_TRUE(received_data.has_acceleration_y); |
151 EXPECT_EQ(3, static_cast<double>(received_data.acceleration_z)); | 224 EXPECT_EQ(5, received_data.acceleration_y); |
152 EXPECT_TRUE(received_data.has_acceleration_z); | 225 EXPECT_TRUE(received_data.has_acceleration_z); |
| 226 EXPECT_EQ(6, received_data.acceleration_z); |
| 227 |
| 228 EXPECT_TRUE(received_data.has_rotation_rate_alpha); |
| 229 EXPECT_EQ(7, received_data.rotation_rate_alpha); |
| 230 EXPECT_TRUE(received_data.has_rotation_rate_beta); |
| 231 EXPECT_EQ(8, received_data.rotation_rate_beta); |
| 232 EXPECT_TRUE(received_data.has_rotation_rate_gamma); |
| 233 EXPECT_EQ(9, received_data.rotation_rate_gamma); |
| 234 } |
| 235 |
| 236 TEST_F(DeviceMotionEventPumpTest, TwoSensorsAreActive) { |
| 237 motion_pump()->Start(listener()); |
| 238 motion_pump()->SetAccelerometerSensorData(true /* active */, 1, 2, 3); |
| 239 motion_pump()->SetLinearAccelerationSensorData(false /* active */, 4, 5, 6); |
| 240 motion_pump()->SetGyroscopeSensorData(true /* active */, 7, 8, 9); |
| 241 motion_pump()->StartFireEvent(); |
| 242 |
| 243 base::RunLoop().Run(); |
| 244 |
| 245 device::MotionData received_data = listener()->data(); |
| 246 EXPECT_TRUE(listener()->did_change_device_motion()); |
| 247 |
| 248 EXPECT_TRUE(received_data.has_acceleration_including_gravity_x); |
| 249 EXPECT_EQ(1, received_data.acceleration_including_gravity_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); |
| 256 EXPECT_FALSE(received_data.has_acceleration_y); |
| 257 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 |
153 EXPECT_FALSE(received_data.has_acceleration_including_gravity_x); | 280 EXPECT_FALSE(received_data.has_acceleration_including_gravity_x); |
154 EXPECT_FALSE(received_data.has_acceleration_including_gravity_y); | 281 EXPECT_FALSE(received_data.has_acceleration_including_gravity_y); |
155 EXPECT_FALSE(received_data.has_acceleration_including_gravity_z); | 282 EXPECT_FALSE(received_data.has_acceleration_including_gravity_z); |
| 283 |
156 EXPECT_FALSE(received_data.has_rotation_rate_alpha); | 284 EXPECT_FALSE(received_data.has_rotation_rate_alpha); |
157 EXPECT_FALSE(received_data.has_rotation_rate_beta); | 285 EXPECT_FALSE(received_data.has_rotation_rate_beta); |
158 EXPECT_FALSE(received_data.has_rotation_rate_gamma); | 286 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 } | 287 } |
184 | 288 |
185 // Confirm that the frequency of pumping events is not greater than 60Hz. A rate | 289 // 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) | 290 // above 60Hz would allow for the detection of keystrokes (crbug.com/421691) |
187 TEST_F(DeviceMotionEventPumpTest, PumpThrottlesEventRate) { | 291 TEST_F(DeviceMotionEventPumpTest, PumpThrottlesEventRate) { |
188 // Confirm that the delay for pumping events is 60 Hz. | 292 // Confirm that the delay for pumping events is 60 Hz. |
189 EXPECT_GE(60, base::Time::kMicrosecondsPerSecond / | 293 EXPECT_GE(60, base::Time::kMicrosecondsPerSecond / |
190 motion_pump()->pump_delay_microseconds()); | 294 motion_pump()->pump_delay_microseconds()); |
191 | 295 |
192 InitBuffer(true); | 296 motion_pump()->Start(listener()); |
| 297 motion_pump()->SetLinearAccelerationSensorData(true /* active */, 4, 5, 6); |
193 | 298 |
194 motion_pump()->set_stop_on_fire_event(false); | 299 motion_pump()->set_stop_on_fire_event(false); |
195 motion_pump()->Start(listener()); | 300 motion_pump()->StartFireEvent(); |
196 motion_pump()->DidStart(handle()); | |
197 | 301 |
198 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 302 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
199 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), | 303 FROM_HERE, base::MessageLoop::QuitWhenIdleClosure(), |
200 base::TimeDelta::FromMilliseconds(100)); | 304 base::TimeDelta::FromMilliseconds(100)); |
201 base::RunLoop().Run(); | 305 base::RunLoop().Run(); |
202 motion_pump()->Stop(); | 306 motion_pump()->Stop(); |
203 | 307 |
204 // Check that the blink::WebDeviceMotionListener does not receive excess | 308 // Check that the blink::WebDeviceMotionListener does not receive excess |
205 // events. | 309 // events. |
206 EXPECT_TRUE(listener()->did_change_device_motion()); | 310 EXPECT_TRUE(listener()->did_change_device_motion()); |
207 EXPECT_GE(6, listener()->number_of_events()); | 311 EXPECT_GE(6, listener()->number_of_events()); |
208 } | 312 } |
209 | 313 |
210 } // namespace content | 314 } // namespace content |
OLD | NEW |