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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/macros.h" | 6 #include "base/macros.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
9 #include "base/threading/platform_thread.h" | 9 #include "base/threading/platform_thread.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
12 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
13 #include "content/public/common/content_switches.h" | 13 #include "content/public/common/content_switches.h" |
14 #include "content/public/test/content_browser_test.h" | 14 #include "content/public/test/content_browser_test.h" |
15 #include "content/public/test/content_browser_test_utils.h" | 15 #include "content/public/test/content_browser_test_utils.h" |
16 #include "content/public/test/test_navigation_observer.h" | 16 #include "content/public/test/test_navigation_observer.h" |
17 #include "content/public/test/test_utils.h" | 17 #include "content/public/test/test_utils.h" |
18 #include "content/shell/browser/shell.h" | 18 #include "content/shell/browser/shell.h" |
19 #include "content/shell/browser/shell_javascript_dialog_manager.h" | 19 #include "content/shell/browser/shell_javascript_dialog_manager.h" |
20 #include "device/generic_sensor/platform_sensor.h" | |
21 #include "device/generic_sensor/platform_sensor_provider.h" | |
20 #include "device/sensors/data_fetcher_shared_memory.h" | 22 #include "device/sensors/data_fetcher_shared_memory.h" |
21 #include "device/sensors/device_sensor_service.h" | 23 #include "device/sensors/device_sensor_service.h" |
22 #include "device/sensors/public/cpp/device_motion_hardware_buffer.h" | 24 #include "device/sensors/public/cpp/device_motion_hardware_buffer.h" |
23 #include "device/sensors/public/cpp/device_orientation_hardware_buffer.h" | 25 #include "device/sensors/public/cpp/device_orientation_hardware_buffer.h" |
24 | 26 |
25 namespace content { | 27 namespace content { |
26 | 28 |
27 namespace { | 29 namespace { |
28 | 30 |
29 class FakeDataFetcher : public device::DataFetcherSharedMemory { | 31 class FakeDataFetcher : public device::DataFetcherSharedMemory { |
30 public: | 32 public: |
31 FakeDataFetcher() : sensor_data_available_(true) {} | 33 FakeDataFetcher() : sensor_data_available_(true) {} |
32 ~FakeDataFetcher() override {} | 34 ~FakeDataFetcher() override {} |
33 | 35 |
34 void SetMotionStartedCallback(base::Closure motion_started_callback) { | |
35 motion_started_callback_ = motion_started_callback; | |
36 } | |
37 | |
38 void SetMotionStoppedCallback(base::Closure motion_stopped_callback) { | |
39 motion_stopped_callback_ = motion_stopped_callback; | |
40 } | |
41 | |
42 void SetOrientationStartedCallback( | 36 void SetOrientationStartedCallback( |
43 base::Closure orientation_started_callback) { | 37 base::Closure orientation_started_callback) { |
44 orientation_started_callback_ = orientation_started_callback; | 38 orientation_started_callback_ = orientation_started_callback; |
45 } | 39 } |
46 | 40 |
47 void SetOrientationStoppedCallback( | 41 void SetOrientationStoppedCallback( |
48 base::Closure orientation_stopped_callback) { | 42 base::Closure orientation_stopped_callback) { |
49 orientation_stopped_callback_ = orientation_stopped_callback; | 43 orientation_stopped_callback_ = orientation_stopped_callback; |
50 } | 44 } |
51 | 45 |
52 void SetOrientationAbsoluteStartedCallback( | 46 void SetOrientationAbsoluteStartedCallback( |
53 base::Closure orientation_absolute_started_callback) { | 47 base::Closure orientation_absolute_started_callback) { |
54 orientation_absolute_started_callback_ = | 48 orientation_absolute_started_callback_ = |
55 orientation_absolute_started_callback; | 49 orientation_absolute_started_callback; |
56 } | 50 } |
57 | 51 |
58 void SetOrientationAbsoluteStoppedCallback( | 52 void SetOrientationAbsoluteStoppedCallback( |
59 base::Closure orientation_absolute_stopped_callback) { | 53 base::Closure orientation_absolute_stopped_callback) { |
60 orientation_absolute_stopped_callback_ = | 54 orientation_absolute_stopped_callback_ = |
61 orientation_absolute_stopped_callback; | 55 orientation_absolute_stopped_callback; |
62 } | 56 } |
63 | 57 |
64 bool Start(device::ConsumerType consumer_type, void* buffer) override { | 58 bool Start(device::ConsumerType consumer_type, void* buffer) override { |
65 EXPECT_TRUE(buffer); | 59 EXPECT_TRUE(buffer); |
66 | 60 |
67 switch (consumer_type) { | 61 switch (consumer_type) { |
68 case device::CONSUMER_TYPE_MOTION: { | |
69 device::DeviceMotionHardwareBuffer* motion_buffer = | |
70 static_cast<device::DeviceMotionHardwareBuffer*>(buffer); | |
71 if (sensor_data_available_) | |
72 UpdateMotion(motion_buffer); | |
73 SetMotionBufferReady(motion_buffer); | |
74 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
75 motion_started_callback_); | |
76 } break; | |
77 case device::CONSUMER_TYPE_ORIENTATION: { | 62 case device::CONSUMER_TYPE_ORIENTATION: { |
78 device::DeviceOrientationHardwareBuffer* orientation_buffer = | 63 device::DeviceOrientationHardwareBuffer* orientation_buffer = |
79 static_cast<device::DeviceOrientationHardwareBuffer*>(buffer); | 64 static_cast<device::DeviceOrientationHardwareBuffer*>(buffer); |
80 if (sensor_data_available_) | 65 if (sensor_data_available_) |
81 UpdateOrientation(orientation_buffer); | 66 UpdateOrientation(orientation_buffer); |
82 SetOrientationBufferReady(orientation_buffer); | 67 SetOrientationBufferReady(orientation_buffer); |
83 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 68 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
84 orientation_started_callback_); | 69 orientation_started_callback_); |
85 } break; | 70 } break; |
86 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: { | 71 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: { |
87 device::DeviceOrientationHardwareBuffer* orientation_buffer = | 72 device::DeviceOrientationHardwareBuffer* orientation_buffer = |
88 static_cast<device::DeviceOrientationHardwareBuffer*>(buffer); | 73 static_cast<device::DeviceOrientationHardwareBuffer*>(buffer); |
89 if (sensor_data_available_) | 74 if (sensor_data_available_) |
90 UpdateOrientationAbsolute(orientation_buffer); | 75 UpdateOrientationAbsolute(orientation_buffer); |
91 SetOrientationBufferReady(orientation_buffer); | 76 SetOrientationBufferReady(orientation_buffer); |
92 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 77 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
93 orientation_absolute_started_callback_); | 78 orientation_absolute_started_callback_); |
94 } break; | 79 } break; |
95 default: | 80 default: |
96 return false; | 81 return false; |
97 } | 82 } |
98 return true; | 83 return true; |
99 } | 84 } |
100 | 85 |
101 bool Stop(device::ConsumerType consumer_type) override { | 86 bool Stop(device::ConsumerType consumer_type) override { |
102 switch (consumer_type) { | 87 switch (consumer_type) { |
103 case device::CONSUMER_TYPE_MOTION: | |
104 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
105 motion_stopped_callback_); | |
106 break; | |
107 case device::CONSUMER_TYPE_ORIENTATION: | 88 case device::CONSUMER_TYPE_ORIENTATION: |
108 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 89 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
109 orientation_stopped_callback_); | 90 orientation_stopped_callback_); |
110 break; | 91 break; |
111 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: | 92 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: |
112 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 93 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
113 orientation_absolute_stopped_callback_); | 94 orientation_absolute_stopped_callback_); |
114 break; | 95 break; |
115 default: | 96 default: |
116 return false; | 97 return false; |
117 } | 98 } |
118 return true; | 99 return true; |
119 } | 100 } |
120 | 101 |
121 void Fetch(unsigned consumer_bitmask) override { | 102 void Fetch(unsigned consumer_bitmask) override { |
122 FAIL() << "fetch should not be called"; | 103 FAIL() << "fetch should not be called"; |
123 } | 104 } |
124 | 105 |
125 FetcherType GetType() const override { return FETCHER_TYPE_DEFAULT; } | 106 FetcherType GetType() const override { return FETCHER_TYPE_DEFAULT; } |
126 | 107 |
127 void SetSensorDataAvailable(bool available) { | 108 void SetSensorDataAvailable(bool available) { |
128 sensor_data_available_ = available; | 109 sensor_data_available_ = available; |
129 } | 110 } |
130 | 111 |
131 void SetMotionBufferReady(device::DeviceMotionHardwareBuffer* buffer) { | |
132 buffer->seqlock.WriteBegin(); | |
133 buffer->data.all_available_sensors_are_active = true; | |
134 buffer->seqlock.WriteEnd(); | |
135 } | |
136 | |
137 void SetOrientationBufferReady( | 112 void SetOrientationBufferReady( |
138 device::DeviceOrientationHardwareBuffer* buffer) { | 113 device::DeviceOrientationHardwareBuffer* buffer) { |
139 buffer->seqlock.WriteBegin(); | 114 buffer->seqlock.WriteBegin(); |
140 buffer->data.all_available_sensors_are_active = true; | 115 buffer->data.all_available_sensors_are_active = true; |
141 buffer->seqlock.WriteEnd(); | 116 buffer->seqlock.WriteEnd(); |
142 } | 117 } |
143 | 118 |
144 void UpdateMotion(device::DeviceMotionHardwareBuffer* buffer) { | |
145 buffer->seqlock.WriteBegin(); | |
146 buffer->data.acceleration_x = 1; | |
147 buffer->data.has_acceleration_x = true; | |
148 buffer->data.acceleration_y = 2; | |
149 buffer->data.has_acceleration_y = true; | |
150 buffer->data.acceleration_z = 3; | |
151 buffer->data.has_acceleration_z = true; | |
152 | |
153 buffer->data.acceleration_including_gravity_x = 4; | |
154 buffer->data.has_acceleration_including_gravity_x = true; | |
155 buffer->data.acceleration_including_gravity_y = 5; | |
156 buffer->data.has_acceleration_including_gravity_y = true; | |
157 buffer->data.acceleration_including_gravity_z = 6; | |
158 buffer->data.has_acceleration_including_gravity_z = true; | |
159 | |
160 buffer->data.rotation_rate_alpha = 7; | |
161 buffer->data.has_rotation_rate_alpha = true; | |
162 buffer->data.rotation_rate_beta = 8; | |
163 buffer->data.has_rotation_rate_beta = true; | |
164 buffer->data.rotation_rate_gamma = 9; | |
165 buffer->data.has_rotation_rate_gamma = true; | |
166 | |
167 buffer->data.interval = 100; | |
168 buffer->data.all_available_sensors_are_active = true; | |
169 buffer->seqlock.WriteEnd(); | |
170 } | |
171 | |
172 void UpdateOrientation(device::DeviceOrientationHardwareBuffer* buffer) { | 119 void UpdateOrientation(device::DeviceOrientationHardwareBuffer* buffer) { |
173 buffer->seqlock.WriteBegin(); | 120 buffer->seqlock.WriteBegin(); |
174 buffer->data.alpha = 1; | 121 buffer->data.alpha = 1; |
175 buffer->data.has_alpha = true; | 122 buffer->data.has_alpha = true; |
176 buffer->data.beta = 2; | 123 buffer->data.beta = 2; |
177 buffer->data.has_beta = true; | 124 buffer->data.has_beta = true; |
178 buffer->data.gamma = 3; | 125 buffer->data.gamma = 3; |
179 buffer->data.has_gamma = true; | 126 buffer->data.has_gamma = true; |
180 buffer->data.all_available_sensors_are_active = true; | 127 buffer->data.all_available_sensors_are_active = true; |
181 buffer->seqlock.WriteEnd(); | 128 buffer->seqlock.WriteEnd(); |
182 } | 129 } |
183 | 130 |
184 void UpdateOrientationAbsolute( | 131 void UpdateOrientationAbsolute( |
185 device::DeviceOrientationHardwareBuffer* buffer) { | 132 device::DeviceOrientationHardwareBuffer* buffer) { |
186 buffer->seqlock.WriteBegin(); | 133 buffer->seqlock.WriteBegin(); |
187 buffer->data.alpha = 4; | 134 buffer->data.alpha = 4; |
188 buffer->data.has_alpha = true; | 135 buffer->data.has_alpha = true; |
189 buffer->data.beta = 5; | 136 buffer->data.beta = 5; |
190 buffer->data.has_beta = true; | 137 buffer->data.has_beta = true; |
191 buffer->data.gamma = 6; | 138 buffer->data.gamma = 6; |
192 buffer->data.has_gamma = true; | 139 buffer->data.has_gamma = true; |
193 buffer->data.absolute = true; | 140 buffer->data.absolute = true; |
194 buffer->data.all_available_sensors_are_active = true; | 141 buffer->data.all_available_sensors_are_active = true; |
195 buffer->seqlock.WriteEnd(); | 142 buffer->seqlock.WriteEnd(); |
196 } | 143 } |
197 | 144 |
198 // The below callbacks should be run on the UI thread. | 145 // The below callbacks should be run on the UI thread. |
199 base::Closure motion_started_callback_; | |
200 base::Closure orientation_started_callback_; | 146 base::Closure orientation_started_callback_; |
201 base::Closure orientation_absolute_started_callback_; | 147 base::Closure orientation_absolute_started_callback_; |
202 base::Closure motion_stopped_callback_; | |
203 base::Closure orientation_stopped_callback_; | 148 base::Closure orientation_stopped_callback_; |
204 base::Closure orientation_absolute_stopped_callback_; | 149 base::Closure orientation_absolute_stopped_callback_; |
205 bool sensor_data_available_; | 150 bool sensor_data_available_; |
206 | 151 |
207 private: | 152 private: |
208 DISALLOW_COPY_AND_ASSIGN(FakeDataFetcher); | 153 DISALLOW_COPY_AND_ASSIGN(FakeDataFetcher); |
209 }; | 154 }; |
210 | 155 |
156 class FakeAccelerometer : public device::PlatformSensor { | |
157 public: | |
158 FakeAccelerometer(mojo::ScopedSharedBufferMapping mapping, | |
159 device::PlatformSensorProvider* provider) | |
160 : PlatformSensor(device::mojom::SensorType::ACCELEROMETER, | |
161 std::move(mapping), | |
162 provider) {} | |
163 | |
164 device::mojom::ReportingMode GetReportingMode() override { | |
165 return device::mojom::ReportingMode::ON_CHANGE; | |
166 } | |
167 | |
168 bool StartSensor( | |
169 const device::PlatformSensorConfiguration& configuration) override { | |
170 device::SensorReading reading; | |
171 reading.timestamp = | |
172 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); | |
173 reading.values[0] = 4; | |
174 reading.values[1] = 5; | |
175 reading.values[2] = 6; | |
176 UpdateSensorReading(reading, true); | |
177 return true; | |
178 } | |
179 | |
180 void StopSensor() override {}; | |
181 | |
182 protected: | |
183 ~FakeAccelerometer() override = default; | |
184 | |
185 bool CheckSensorConfiguration( | |
186 const device::PlatformSensorConfiguration& configuration) override { | |
187 return true; | |
188 } | |
189 | |
190 device::PlatformSensorConfiguration GetDefaultConfiguration() override { | |
191 return device::PlatformSensorConfiguration(60 /* frequency */); | |
192 } | |
193 | |
194 private: | |
195 DISALLOW_COPY_AND_ASSIGN(FakeAccelerometer); | |
196 }; | |
197 | |
198 class FakeLinearAccelerationSensor : public device::PlatformSensor { | |
199 public: | |
200 FakeLinearAccelerationSensor(mojo::ScopedSharedBufferMapping mapping, | |
201 device::PlatformSensorProvider* provider) | |
202 : PlatformSensor(device::mojom::SensorType::LINEAR_ACCELERATION, | |
203 std::move(mapping), | |
204 provider) {} | |
205 | |
206 device::mojom::ReportingMode GetReportingMode() override { | |
207 return device::mojom::ReportingMode::CONTINUOUS; | |
208 } | |
209 | |
210 bool StartSensor( | |
211 const device::PlatformSensorConfiguration& configuration) override { | |
212 device::SensorReading reading; | |
213 reading.timestamp = | |
214 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); | |
215 reading.values[0] = 1; | |
216 reading.values[1] = 2; | |
217 reading.values[2] = 3; | |
218 UpdateSensorReading(reading, true); | |
219 return true; | |
220 } | |
221 | |
222 void StopSensor() override {}; | |
223 | |
224 protected: | |
225 ~FakeLinearAccelerationSensor() override = default; | |
226 | |
227 bool CheckSensorConfiguration( | |
228 const device::PlatformSensorConfiguration& configuration) override { | |
229 return true; | |
230 } | |
231 | |
232 device::PlatformSensorConfiguration GetDefaultConfiguration() override { | |
233 return device::PlatformSensorConfiguration(60 /* frequency */); | |
234 } | |
235 | |
236 private: | |
237 DISALLOW_COPY_AND_ASSIGN(FakeLinearAccelerationSensor); | |
238 }; | |
239 | |
240 class FakeGyroscope : public device::PlatformSensor { | |
241 public: | |
242 FakeGyroscope(mojo::ScopedSharedBufferMapping mapping, | |
243 device::PlatformSensorProvider* provider) | |
244 : PlatformSensor(device::mojom::SensorType::GYROSCOPE, | |
245 std::move(mapping), | |
246 provider) {} | |
247 | |
248 device::mojom::ReportingMode GetReportingMode() override { | |
249 return device::mojom::ReportingMode::ON_CHANGE; | |
250 } | |
251 | |
252 bool StartSensor( | |
253 const device::PlatformSensorConfiguration& configuration) override { | |
254 device::SensorReading reading; | |
255 reading.timestamp = | |
256 (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); | |
257 reading.values[0] = 7; | |
258 reading.values[1] = 8; | |
259 reading.values[2] = 9; | |
260 UpdateSensorReading(reading, true); | |
261 return true; | |
262 } | |
263 | |
264 void StopSensor() override {}; | |
265 | |
266 protected: | |
267 ~FakeGyroscope() override = default; | |
268 | |
269 bool CheckSensorConfiguration( | |
270 const device::PlatformSensorConfiguration& configuration) override { | |
271 return true; | |
272 } | |
273 | |
274 device::PlatformSensorConfiguration GetDefaultConfiguration() override { | |
275 return device::PlatformSensorConfiguration(60 /* frequency */); | |
276 } | |
277 | |
278 private: | |
279 DISALLOW_COPY_AND_ASSIGN(FakeGyroscope); | |
280 }; | |
281 | |
282 class FakeSensorProvider : public device::PlatformSensorProvider { | |
283 public: | |
284 static FakeSensorProvider* GetInstance() { | |
285 return base::Singleton<FakeSensorProvider, base::LeakySingletonTraits< | |
286 FakeSensorProvider>>::get(); | |
287 } | |
288 FakeSensorProvider() | |
289 : accelerometer_is_available_(true), | |
290 linear_acceleration_sensor_is_available_(true), | |
291 gyroscope_is_available_(true) {} | |
Reilly Grant (use Gerrit)
2017/06/06 20:58:55
nit: It's cleaner to do these default initializati
juncai
2017/06/06 23:36:25
Done.
| |
292 ~FakeSensorProvider() override = default; | |
293 | |
294 void set_accelerometer_is_available(bool accelerometer_is_available) { | |
295 accelerometer_is_available_ = accelerometer_is_available; | |
296 } | |
297 | |
298 void set_linear_acceleration_sensor_is_available( | |
299 bool linear_acceleration_sensor_is_available) { | |
300 linear_acceleration_sensor_is_available_ = | |
301 linear_acceleration_sensor_is_available; | |
302 } | |
303 | |
304 void set_gyroscope_is_available(bool gyroscope_is_available) { | |
305 gyroscope_is_available_ = gyroscope_is_available; | |
306 } | |
307 | |
308 protected: | |
309 void CreateSensorInternal(device::mojom::SensorType type, | |
310 mojo::ScopedSharedBufferMapping mapping, | |
311 const CreateSensorCallback& callback) override { | |
312 // Create Sensors here. | |
313 scoped_refptr<device::PlatformSensor> sensor; | |
314 | |
315 switch (type) { | |
316 case device::mojom::SensorType::ACCELEROMETER: | |
317 if (accelerometer_is_available_) | |
318 sensor = new FakeAccelerometer(std::move(mapping), this); | |
319 break; | |
320 case device::mojom::SensorType::LINEAR_ACCELERATION: | |
321 if (linear_acceleration_sensor_is_available_) | |
322 sensor = new FakeLinearAccelerationSensor(std::move(mapping), this); | |
323 break; | |
324 case device::mojom::SensorType::GYROSCOPE: | |
325 if (gyroscope_is_available_) | |
326 sensor = new FakeGyroscope(std::move(mapping), this); | |
327 break; | |
328 default: | |
329 NOTIMPLEMENTED(); | |
330 } | |
331 | |
332 callback.Run(std::move(sensor)); | |
333 } | |
334 | |
335 bool accelerometer_is_available_; | |
336 bool linear_acceleration_sensor_is_available_; | |
337 bool gyroscope_is_available_; | |
338 }; | |
339 | |
211 class DeviceSensorBrowserTest : public ContentBrowserTest { | 340 class DeviceSensorBrowserTest : public ContentBrowserTest { |
212 public: | 341 public: |
213 DeviceSensorBrowserTest() | 342 DeviceSensorBrowserTest() |
214 : fetcher_(nullptr), | 343 : fetcher_(nullptr), |
215 io_loop_finished_event_( | 344 io_loop_finished_event_( |
216 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 345 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
217 base::WaitableEvent::InitialState::NOT_SIGNALED) {} | 346 base::WaitableEvent::InitialState::NOT_SIGNALED) {} |
218 | 347 |
219 void SetUpOnMainThread() override { | 348 void SetUpOnMainThread() override { |
220 // Initialize the RunLoops now that the main thread has been created. | 349 // Initialize the RunLoops now that the main thread has been created. |
221 motion_started_runloop_.reset(new base::RunLoop()); | |
222 motion_stopped_runloop_.reset(new base::RunLoop()); | |
223 orientation_started_runloop_.reset(new base::RunLoop()); | 350 orientation_started_runloop_.reset(new base::RunLoop()); |
224 orientation_stopped_runloop_.reset(new base::RunLoop()); | 351 orientation_stopped_runloop_.reset(new base::RunLoop()); |
225 orientation_absolute_started_runloop_.reset(new base::RunLoop()); | 352 orientation_absolute_started_runloop_.reset(new base::RunLoop()); |
226 orientation_absolute_stopped_runloop_.reset(new base::RunLoop()); | 353 orientation_absolute_stopped_runloop_.reset(new base::RunLoop()); |
227 #if defined(OS_ANDROID) | 354 #if defined(OS_ANDROID) |
228 // On Android, the DeviceSensorService lives on the UI thread. | 355 // On Android, the DeviceSensorService lives on the UI thread. |
229 SetUpFetcher(); | 356 SetUpFetcher(); |
357 BrowserThread::PostTask( | |
358 BrowserThread::IO, FROM_HERE, | |
359 base::Bind(&DeviceSensorBrowserTest::SetUpOnIOThreadForAndroid, | |
360 base::Unretained(this))); | |
361 io_loop_finished_event_.Wait(); | |
230 #else | 362 #else |
231 // On all other platforms, the DeviceSensorService lives on the IO thread. | 363 // On all other platforms, the DeviceSensorService lives on the IO thread. |
232 BrowserThread::PostTask( | 364 BrowserThread::PostTask( |
233 BrowserThread::IO, FROM_HERE, | 365 BrowserThread::IO, FROM_HERE, |
234 base::Bind(&DeviceSensorBrowserTest::SetUpOnIOThread, | 366 base::Bind(&DeviceSensorBrowserTest::SetUpOnIOThread, |
235 base::Unretained(this))); | 367 base::Unretained(this))); |
236 io_loop_finished_event_.Wait(); | 368 io_loop_finished_event_.Wait(); |
237 #endif | 369 #endif |
238 } | 370 } |
239 | 371 |
240 void SetUpFetcher() { | 372 void SetUpFetcher() { |
241 fetcher_ = new FakeDataFetcher(); | 373 fetcher_ = new FakeDataFetcher(); |
242 fetcher_->SetMotionStartedCallback(motion_started_runloop_->QuitClosure()); | |
243 fetcher_->SetMotionStoppedCallback(motion_stopped_runloop_->QuitClosure()); | |
244 fetcher_->SetOrientationStartedCallback( | 374 fetcher_->SetOrientationStartedCallback( |
245 orientation_started_runloop_->QuitClosure()); | 375 orientation_started_runloop_->QuitClosure()); |
246 fetcher_->SetOrientationStoppedCallback( | 376 fetcher_->SetOrientationStoppedCallback( |
247 orientation_stopped_runloop_->QuitClosure()); | 377 orientation_stopped_runloop_->QuitClosure()); |
248 fetcher_->SetOrientationAbsoluteStartedCallback( | 378 fetcher_->SetOrientationAbsoluteStartedCallback( |
249 orientation_absolute_started_runloop_->QuitClosure()); | 379 orientation_absolute_started_runloop_->QuitClosure()); |
250 fetcher_->SetOrientationAbsoluteStoppedCallback( | 380 fetcher_->SetOrientationAbsoluteStoppedCallback( |
251 orientation_absolute_stopped_runloop_->QuitClosure()); | 381 orientation_absolute_stopped_runloop_->QuitClosure()); |
252 device::DeviceSensorService::GetInstance()->SetDataFetcherForTesting( | 382 device::DeviceSensorService::GetInstance()->SetDataFetcherForTesting( |
253 fetcher_); | 383 fetcher_); |
254 } | 384 } |
255 | 385 |
386 void SetUpOnIOThreadForAndroid() { | |
387 sensor_provider_ = FakeSensorProvider::GetInstance(); | |
388 device::PlatformSensorProvider::SetProviderForTesting(sensor_provider_); | |
389 io_loop_finished_event_.Signal(); | |
390 } | |
391 | |
256 void SetUpOnIOThread() { | 392 void SetUpOnIOThread() { |
257 SetUpFetcher(); | 393 SetUpFetcher(); |
394 sensor_provider_ = FakeSensorProvider::GetInstance(); | |
395 device::PlatformSensorProvider::SetProviderForTesting(sensor_provider_); | |
258 io_loop_finished_event_.Signal(); | 396 io_loop_finished_event_.Signal(); |
259 } | 397 } |
260 | 398 |
399 void TearDown() override { | |
400 device::PlatformSensorProvider::SetProviderForTesting(nullptr); | |
401 } | |
402 | |
261 void DelayAndQuit(base::TimeDelta delay) { | 403 void DelayAndQuit(base::TimeDelta delay) { |
262 base::PlatformThread::Sleep(delay); | 404 base::PlatformThread::Sleep(delay); |
263 base::MessageLoop::current()->QuitWhenIdle(); | 405 base::MessageLoop::current()->QuitWhenIdle(); |
264 } | 406 } |
265 | 407 |
266 void WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta delay) { | 408 void WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta delay) { |
267 ShellJavaScriptDialogManager* dialog_manager = | 409 ShellJavaScriptDialogManager* dialog_manager = |
268 static_cast<ShellJavaScriptDialogManager*>( | 410 static_cast<ShellJavaScriptDialogManager*>( |
269 shell()->GetJavaScriptDialogManager(shell()->web_contents())); | 411 shell()->GetJavaScriptDialogManager(shell()->web_contents())); |
270 | 412 |
271 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner(); | 413 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner(); |
272 dialog_manager->set_dialog_request_callback( | 414 dialog_manager->set_dialog_request_callback( |
273 base::Bind(&DeviceSensorBrowserTest::DelayAndQuit, | 415 base::Bind(&DeviceSensorBrowserTest::DelayAndQuit, |
274 base::Unretained(this), delay)); | 416 base::Unretained(this), delay)); |
275 runner->Run(); | 417 runner->Run(); |
276 } | 418 } |
277 | 419 |
278 FakeDataFetcher* fetcher_; | 420 FakeDataFetcher* fetcher_; |
421 FakeSensorProvider* sensor_provider_; | |
279 | 422 |
280 // NOTE: These can only be initialized once the main thread has been created | 423 // NOTE: These can only be initialized once the main thread has been created |
281 // and so must be pointers instead of plain objects. | 424 // and so must be pointers instead of plain objects. |
282 std::unique_ptr<base::RunLoop> motion_started_runloop_; | |
283 std::unique_ptr<base::RunLoop> motion_stopped_runloop_; | |
284 std::unique_ptr<base::RunLoop> orientation_started_runloop_; | 425 std::unique_ptr<base::RunLoop> orientation_started_runloop_; |
285 std::unique_ptr<base::RunLoop> orientation_stopped_runloop_; | 426 std::unique_ptr<base::RunLoop> orientation_stopped_runloop_; |
286 std::unique_ptr<base::RunLoop> orientation_absolute_started_runloop_; | 427 std::unique_ptr<base::RunLoop> orientation_absolute_started_runloop_; |
287 std::unique_ptr<base::RunLoop> orientation_absolute_stopped_runloop_; | 428 std::unique_ptr<base::RunLoop> orientation_absolute_stopped_runloop_; |
288 | 429 |
289 private: | 430 private: |
290 base::WaitableEvent io_loop_finished_event_; | 431 base::WaitableEvent io_loop_finished_event_; |
291 }; | 432 }; |
292 | 433 |
293 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationTest) { | 434 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationTest) { |
(...skipping 22 matching lines...) Expand all Loading... | |
316 } | 457 } |
317 | 458 |
318 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionTest) { | 459 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionTest) { |
319 // The test page will register an event handler for motion events, | 460 // The test page will register an event handler for motion events, |
320 // expects to get an event with fake values, then removes the event | 461 // expects to get an event with fake values, then removes the event |
321 // handler and navigates to #pass. | 462 // handler and navigates to #pass. |
322 GURL test_url = GetTestUrl("device_sensors", "device_motion_test.html"); | 463 GURL test_url = GetTestUrl("device_sensors", "device_motion_test.html"); |
323 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | 464 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
324 | 465 |
325 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 466 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
326 motion_started_runloop_->Run(); | |
327 motion_stopped_runloop_->Run(); | |
328 } | 467 } |
329 | 468 |
330 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationNullTest) { | 469 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationNullTest) { |
331 // The test page registers an event handler for orientation events and | 470 // The test page registers an event handler for orientation events and |
332 // expects to get an event with null values, because no sensor data can be | 471 // expects to get an event with null values, because no sensor data can be |
333 // provided. | 472 // provided. |
334 fetcher_->SetSensorDataAvailable(false); | 473 fetcher_->SetSensorDataAvailable(false); |
335 GURL test_url = | 474 GURL test_url = |
336 GetTestUrl("device_sensors", "device_orientation_null_test.html"); | 475 GetTestUrl("device_sensors", "device_orientation_null_test.html"); |
337 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | 476 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
(...skipping 14 matching lines...) Expand all Loading... | |
352 | 491 |
353 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 492 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
354 orientation_absolute_started_runloop_->Run(); | 493 orientation_absolute_started_runloop_->Run(); |
355 orientation_absolute_stopped_runloop_->Run(); | 494 orientation_absolute_stopped_runloop_->Run(); |
356 } | 495 } |
357 | 496 |
358 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionNullTest) { | 497 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionNullTest) { |
359 // The test page registers an event handler for motion events and | 498 // The test page registers an event handler for motion events and |
360 // expects to get an event with null values, because no sensor data can be | 499 // expects to get an event with null values, because no sensor data can be |
361 // provided. | 500 // provided. |
362 fetcher_->SetSensorDataAvailable(false); | 501 sensor_provider_->set_accelerometer_is_available(false); |
502 sensor_provider_->set_linear_acceleration_sensor_is_available(false); | |
503 sensor_provider_->set_gyroscope_is_available(false); | |
363 GURL test_url = GetTestUrl("device_sensors", "device_motion_null_test.html"); | 504 GURL test_url = GetTestUrl("device_sensors", "device_motion_null_test.html"); |
364 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | 505 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
365 | 506 |
366 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 507 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
367 motion_started_runloop_->Run(); | 508 } |
368 motion_stopped_runloop_->Run(); | 509 |
510 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, | |
511 MotionOnlySomeSensorsAreAvailableTest) { | |
512 // The test page registers an event handler for motion events and | |
513 // expects to get an event with only some fake values, because only | |
514 // some sensor data can be provided. | |
515 sensor_provider_->set_accelerometer_is_available(false); | |
516 GURL test_url = | |
517 GetTestUrl("device_sensors", | |
518 "device_motion_only_some_sensors_are_available_test.html"); | |
519 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | |
520 | |
521 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
369 } | 522 } |
370 | 523 |
371 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, NullTestWithAlert) { | 524 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, NullTestWithAlert) { |
372 // The test page registers an event handlers for motion/orientation events and | 525 // The test page registers an event handlers for motion/orientation events and |
373 // expects to get events with null values. The test raises a modal alert | 526 // expects to get events with null values. The test raises a modal alert |
374 // dialog with a delay to test that the one-off null-events still propagate to | 527 // dialog with a delay to test that the one-off null-events still propagate to |
375 // window after the alert is dismissed and the callbacks are invoked which | 528 // window after the alert is dismissed and the callbacks are invoked which |
376 // eventually navigate to #pass. | 529 // eventually navigate to #pass. |
377 fetcher_->SetSensorDataAvailable(false); | 530 fetcher_->SetSensorDataAvailable(false); |
531 sensor_provider_->set_accelerometer_is_available(false); | |
532 sensor_provider_->set_linear_acceleration_sensor_is_available(false); | |
533 sensor_provider_->set_gyroscope_is_available(false); | |
378 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); | 534 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); |
379 | 535 |
380 GURL test_url = | 536 GURL test_url = |
381 GetTestUrl("device_sensors", "device_sensors_null_test_with_alert.html"); | 537 GetTestUrl("device_sensors", "device_sensors_null_test_with_alert.html"); |
382 shell()->LoadURL(test_url); | 538 shell()->LoadURL(test_url); |
383 | 539 |
384 // TODO(timvolodine): investigate if it is possible to test this without | 540 // TODO(timvolodine): investigate if it is possible to test this without |
385 // delay, crbug.com/360044. | 541 // delay, crbug.com/360044. |
386 WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta::FromMilliseconds(500)); | 542 WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta::FromMilliseconds(500)); |
387 | 543 |
388 motion_started_runloop_->Run(); | |
389 motion_stopped_runloop_->Run(); | |
390 orientation_started_runloop_->Run(); | 544 orientation_started_runloop_->Run(); |
391 orientation_stopped_runloop_->Run(); | 545 orientation_stopped_runloop_->Run(); |
392 same_tab_observer.Wait(); | 546 same_tab_observer.Wait(); |
393 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 547 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
394 } | 548 } |
395 | 549 |
396 } // namespace | 550 } // namespace |
397 | 551 |
398 } // namespace content | 552 } // namespace content |
OLD | NEW |