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/synchronization/waitable_event.h" | 7 #include "base/synchronization/waitable_event.h" |
8 #include "base/threading/platform_thread.h" | 8 #include "base/threading/platform_thread.h" |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
11 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
12 #include "content/public/common/content_switches.h" | 12 #include "content/public/common/content_switches.h" |
13 #include "content/public/test/content_browser_test.h" | 13 #include "content/public/test/content_browser_test.h" |
14 #include "content/public/test/content_browser_test_utils.h" | 14 #include "content/public/test/content_browser_test_utils.h" |
15 #include "content/public/test/test_navigation_observer.h" | 15 #include "content/public/test/test_navigation_observer.h" |
16 #include "content/public/test/test_utils.h" | 16 #include "content/public/test/test_utils.h" |
17 #include "content/shell/browser/shell.h" | 17 #include "content/shell/browser/shell.h" |
18 #include "content/shell/browser/shell_javascript_dialog_manager.h" | 18 #include "content/shell/browser/shell_javascript_dialog_manager.h" |
19 #include "device/sensors/data_fetcher_shared_memory.h" | 19 #include "device/sensors/data_fetcher_shared_memory.h" |
20 #include "device/sensors/device_sensor_service.h" | 20 #include "device/sensors/device_sensor_service.h" |
21 #include "device/sensors/public/cpp/device_light_hardware_buffer.h" | |
22 #include "device/sensors/public/cpp/device_motion_hardware_buffer.h" | 21 #include "device/sensors/public/cpp/device_motion_hardware_buffer.h" |
23 #include "device/sensors/public/cpp/device_orientation_hardware_buffer.h" | 22 #include "device/sensors/public/cpp/device_orientation_hardware_buffer.h" |
24 | 23 |
25 namespace content { | 24 namespace content { |
26 | 25 |
27 namespace { | 26 namespace { |
28 | 27 |
29 class FakeDataFetcher : public device::DataFetcherSharedMemory { | 28 class FakeDataFetcher : public device::DataFetcherSharedMemory { |
30 public: | 29 public: |
31 FakeDataFetcher() : sensor_data_available_(true) {} | 30 FakeDataFetcher() : sensor_data_available_(true) {} |
32 ~FakeDataFetcher() override {} | 31 ~FakeDataFetcher() override {} |
33 | 32 |
34 void SetMotionStartedCallback(base::Closure motion_started_callback) { | 33 void SetMotionStartedCallback(base::Closure motion_started_callback) { |
35 motion_started_callback_ = motion_started_callback; | 34 motion_started_callback_ = motion_started_callback; |
36 } | 35 } |
37 | 36 |
38 void SetMotionStoppedCallback(base::Closure motion_stopped_callback) { | 37 void SetMotionStoppedCallback(base::Closure motion_stopped_callback) { |
39 motion_stopped_callback_ = motion_stopped_callback; | 38 motion_stopped_callback_ = motion_stopped_callback; |
40 } | 39 } |
41 | 40 |
42 void SetLightStartedCallback(base::Closure light_started_callback) { | |
43 light_started_callback_ = light_started_callback; | |
44 } | |
45 | |
46 void SetLightStoppedCallback(base::Closure light_stopped_callback) { | |
47 light_stopped_callback_ = light_stopped_callback; | |
48 } | |
49 | |
50 void SetOrientationStartedCallback( | 41 void SetOrientationStartedCallback( |
51 base::Closure orientation_started_callback) { | 42 base::Closure orientation_started_callback) { |
52 orientation_started_callback_ = orientation_started_callback; | 43 orientation_started_callback_ = orientation_started_callback; |
53 } | 44 } |
54 | 45 |
55 void SetOrientationStoppedCallback( | 46 void SetOrientationStoppedCallback( |
56 base::Closure orientation_stopped_callback) { | 47 base::Closure orientation_stopped_callback) { |
57 orientation_stopped_callback_ = orientation_stopped_callback; | 48 orientation_stopped_callback_ = orientation_stopped_callback; |
58 } | 49 } |
59 | 50 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 } break; | 84 } break; |
94 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: { | 85 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: { |
95 device::DeviceOrientationHardwareBuffer* orientation_buffer = | 86 device::DeviceOrientationHardwareBuffer* orientation_buffer = |
96 static_cast<device::DeviceOrientationHardwareBuffer*>(buffer); | 87 static_cast<device::DeviceOrientationHardwareBuffer*>(buffer); |
97 if (sensor_data_available_) | 88 if (sensor_data_available_) |
98 UpdateOrientationAbsolute(orientation_buffer); | 89 UpdateOrientationAbsolute(orientation_buffer); |
99 SetOrientationBufferReady(orientation_buffer); | 90 SetOrientationBufferReady(orientation_buffer); |
100 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 91 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
101 orientation_absolute_started_callback_); | 92 orientation_absolute_started_callback_); |
102 } break; | 93 } break; |
103 case device::CONSUMER_TYPE_LIGHT: { | |
104 device::DeviceLightHardwareBuffer* light_buffer = | |
105 static_cast<device::DeviceLightHardwareBuffer*>(buffer); | |
106 UpdateLight(light_buffer, | |
107 sensor_data_available_ | |
108 ? 100 | |
109 : std::numeric_limits<double>::infinity()); | |
110 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
111 light_started_callback_); | |
112 } break; | |
113 default: | 94 default: |
114 return false; | 95 return false; |
115 } | 96 } |
116 return true; | 97 return true; |
117 } | 98 } |
118 | 99 |
119 bool Stop(device::ConsumerType consumer_type) override { | 100 bool Stop(device::ConsumerType consumer_type) override { |
120 switch (consumer_type) { | 101 switch (consumer_type) { |
121 case device::CONSUMER_TYPE_MOTION: | 102 case device::CONSUMER_TYPE_MOTION: |
122 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 103 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
123 motion_stopped_callback_); | 104 motion_stopped_callback_); |
124 break; | 105 break; |
125 case device::CONSUMER_TYPE_ORIENTATION: | 106 case device::CONSUMER_TYPE_ORIENTATION: |
126 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 107 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
127 orientation_stopped_callback_); | 108 orientation_stopped_callback_); |
128 break; | 109 break; |
129 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: | 110 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: |
130 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 111 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
131 orientation_absolute_stopped_callback_); | 112 orientation_absolute_stopped_callback_); |
132 break; | 113 break; |
133 case device::CONSUMER_TYPE_LIGHT: | |
134 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
135 light_stopped_callback_); | |
136 break; | |
137 default: | 114 default: |
138 return false; | 115 return false; |
139 } | 116 } |
140 return true; | 117 return true; |
141 } | 118 } |
142 | 119 |
143 void Fetch(unsigned consumer_bitmask) override { | 120 void Fetch(unsigned consumer_bitmask) override { |
144 FAIL() << "fetch should not be called"; | 121 FAIL() << "fetch should not be called"; |
145 } | 122 } |
146 | 123 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 buffer->data.has_alpha = true; | 187 buffer->data.has_alpha = true; |
211 buffer->data.beta = 5; | 188 buffer->data.beta = 5; |
212 buffer->data.has_beta = true; | 189 buffer->data.has_beta = true; |
213 buffer->data.gamma = 6; | 190 buffer->data.gamma = 6; |
214 buffer->data.has_gamma = true; | 191 buffer->data.has_gamma = true; |
215 buffer->data.absolute = true; | 192 buffer->data.absolute = true; |
216 buffer->data.all_available_sensors_are_active = true; | 193 buffer->data.all_available_sensors_are_active = true; |
217 buffer->seqlock.WriteEnd(); | 194 buffer->seqlock.WriteEnd(); |
218 } | 195 } |
219 | 196 |
220 void UpdateLight(device::DeviceLightHardwareBuffer* buffer, double lux) { | |
221 buffer->seqlock.WriteBegin(); | |
222 buffer->data.value = lux; | |
223 buffer->seqlock.WriteEnd(); | |
224 } | |
225 | |
226 // The below callbacks should be run on the UI thread. | 197 // The below callbacks should be run on the UI thread. |
227 base::Closure motion_started_callback_; | 198 base::Closure motion_started_callback_; |
228 base::Closure orientation_started_callback_; | 199 base::Closure orientation_started_callback_; |
229 base::Closure orientation_absolute_started_callback_; | 200 base::Closure orientation_absolute_started_callback_; |
230 base::Closure light_started_callback_; | |
231 base::Closure motion_stopped_callback_; | 201 base::Closure motion_stopped_callback_; |
232 base::Closure orientation_stopped_callback_; | 202 base::Closure orientation_stopped_callback_; |
233 base::Closure orientation_absolute_stopped_callback_; | 203 base::Closure orientation_absolute_stopped_callback_; |
234 base::Closure light_stopped_callback_; | |
235 bool sensor_data_available_; | 204 bool sensor_data_available_; |
236 | 205 |
237 private: | 206 private: |
238 DISALLOW_COPY_AND_ASSIGN(FakeDataFetcher); | 207 DISALLOW_COPY_AND_ASSIGN(FakeDataFetcher); |
239 }; | 208 }; |
240 | 209 |
241 class DeviceSensorBrowserTest : public ContentBrowserTest { | 210 class DeviceSensorBrowserTest : public ContentBrowserTest { |
242 public: | 211 public: |
243 DeviceSensorBrowserTest() | 212 DeviceSensorBrowserTest() |
244 : fetcher_(nullptr), | 213 : fetcher_(nullptr), |
245 io_loop_finished_event_( | 214 io_loop_finished_event_( |
246 base::WaitableEvent::ResetPolicy::AUTOMATIC, | 215 base::WaitableEvent::ResetPolicy::AUTOMATIC, |
247 base::WaitableEvent::InitialState::NOT_SIGNALED) {} | 216 base::WaitableEvent::InitialState::NOT_SIGNALED) {} |
248 | 217 |
249 void SetUpOnMainThread() override { | 218 void SetUpOnMainThread() override { |
250 // Initialize the RunLoops now that the main thread has been created. | 219 // Initialize the RunLoops now that the main thread has been created. |
251 light_started_runloop_.reset(new base::RunLoop()); | |
252 light_stopped_runloop_.reset(new base::RunLoop()); | |
253 motion_started_runloop_.reset(new base::RunLoop()); | 220 motion_started_runloop_.reset(new base::RunLoop()); |
254 motion_stopped_runloop_.reset(new base::RunLoop()); | 221 motion_stopped_runloop_.reset(new base::RunLoop()); |
255 orientation_started_runloop_.reset(new base::RunLoop()); | 222 orientation_started_runloop_.reset(new base::RunLoop()); |
256 orientation_stopped_runloop_.reset(new base::RunLoop()); | 223 orientation_stopped_runloop_.reset(new base::RunLoop()); |
257 orientation_absolute_started_runloop_.reset(new base::RunLoop()); | 224 orientation_absolute_started_runloop_.reset(new base::RunLoop()); |
258 orientation_absolute_stopped_runloop_.reset(new base::RunLoop()); | 225 orientation_absolute_stopped_runloop_.reset(new base::RunLoop()); |
259 #if defined(OS_ANDROID) | 226 #if defined(OS_ANDROID) |
260 // On Android, the DeviceSensorService lives on the UI thread. | 227 // On Android, the DeviceSensorService lives on the UI thread. |
261 SetUpFetcher(); | 228 SetUpFetcher(); |
262 #else | 229 #else |
263 // On all other platforms, the DeviceSensorService lives on the IO thread. | 230 // On all other platforms, the DeviceSensorService lives on the IO thread. |
264 BrowserThread::PostTask( | 231 BrowserThread::PostTask( |
265 BrowserThread::IO, FROM_HERE, | 232 BrowserThread::IO, FROM_HERE, |
266 base::Bind(&DeviceSensorBrowserTest::SetUpOnIOThread, | 233 base::Bind(&DeviceSensorBrowserTest::SetUpOnIOThread, |
267 base::Unretained(this))); | 234 base::Unretained(this))); |
268 io_loop_finished_event_.Wait(); | 235 io_loop_finished_event_.Wait(); |
269 #endif | 236 #endif |
270 } | 237 } |
271 | 238 |
272 void SetUpFetcher() { | 239 void SetUpFetcher() { |
273 fetcher_ = new FakeDataFetcher(); | 240 fetcher_ = new FakeDataFetcher(); |
274 fetcher_->SetLightStartedCallback(light_started_runloop_->QuitClosure()); | |
275 fetcher_->SetLightStoppedCallback(light_stopped_runloop_->QuitClosure()); | |
276 fetcher_->SetMotionStartedCallback(motion_started_runloop_->QuitClosure()); | 241 fetcher_->SetMotionStartedCallback(motion_started_runloop_->QuitClosure()); |
277 fetcher_->SetMotionStoppedCallback(motion_stopped_runloop_->QuitClosure()); | 242 fetcher_->SetMotionStoppedCallback(motion_stopped_runloop_->QuitClosure()); |
278 fetcher_->SetOrientationStartedCallback( | 243 fetcher_->SetOrientationStartedCallback( |
279 orientation_started_runloop_->QuitClosure()); | 244 orientation_started_runloop_->QuitClosure()); |
280 fetcher_->SetOrientationStoppedCallback( | 245 fetcher_->SetOrientationStoppedCallback( |
281 orientation_stopped_runloop_->QuitClosure()); | 246 orientation_stopped_runloop_->QuitClosure()); |
282 fetcher_->SetOrientationAbsoluteStartedCallback( | 247 fetcher_->SetOrientationAbsoluteStartedCallback( |
283 orientation_absolute_started_runloop_->QuitClosure()); | 248 orientation_absolute_started_runloop_->QuitClosure()); |
284 fetcher_->SetOrientationAbsoluteStoppedCallback( | 249 fetcher_->SetOrientationAbsoluteStoppedCallback( |
285 orientation_absolute_stopped_runloop_->QuitClosure()); | 250 orientation_absolute_stopped_runloop_->QuitClosure()); |
(...skipping 16 matching lines...) Expand all Loading... |
302 static_cast<ShellJavaScriptDialogManager*>( | 267 static_cast<ShellJavaScriptDialogManager*>( |
303 shell()->GetJavaScriptDialogManager(shell()->web_contents())); | 268 shell()->GetJavaScriptDialogManager(shell()->web_contents())); |
304 | 269 |
305 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner(); | 270 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner(); |
306 dialog_manager->set_dialog_request_callback( | 271 dialog_manager->set_dialog_request_callback( |
307 base::Bind(&DeviceSensorBrowserTest::DelayAndQuit, | 272 base::Bind(&DeviceSensorBrowserTest::DelayAndQuit, |
308 base::Unretained(this), delay)); | 273 base::Unretained(this), delay)); |
309 runner->Run(); | 274 runner->Run(); |
310 } | 275 } |
311 | 276 |
312 void EnableExperimentalFeatures() { | |
313 // TODO(riju): remove when the DeviceLight feature goes stable. | |
314 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess(); | |
315 if (!cmd_line->HasSwitch(switches::kEnableExperimentalWebPlatformFeatures)) | |
316 cmd_line->AppendSwitch(switches::kEnableExperimentalWebPlatformFeatures); | |
317 } | |
318 | |
319 FakeDataFetcher* fetcher_; | 277 FakeDataFetcher* fetcher_; |
320 | 278 |
321 // NOTE: These can only be initialized once the main thread has been created | 279 // NOTE: These can only be initialized once the main thread has been created |
322 // and so must be pointers instead of plain objects. | 280 // and so must be pointers instead of plain objects. |
323 std::unique_ptr<base::RunLoop> light_started_runloop_; | |
324 std::unique_ptr<base::RunLoop> light_stopped_runloop_; | |
325 std::unique_ptr<base::RunLoop> motion_started_runloop_; | 281 std::unique_ptr<base::RunLoop> motion_started_runloop_; |
326 std::unique_ptr<base::RunLoop> motion_stopped_runloop_; | 282 std::unique_ptr<base::RunLoop> motion_stopped_runloop_; |
327 std::unique_ptr<base::RunLoop> orientation_started_runloop_; | 283 std::unique_ptr<base::RunLoop> orientation_started_runloop_; |
328 std::unique_ptr<base::RunLoop> orientation_stopped_runloop_; | 284 std::unique_ptr<base::RunLoop> orientation_stopped_runloop_; |
329 std::unique_ptr<base::RunLoop> orientation_absolute_started_runloop_; | 285 std::unique_ptr<base::RunLoop> orientation_absolute_started_runloop_; |
330 std::unique_ptr<base::RunLoop> orientation_absolute_stopped_runloop_; | 286 std::unique_ptr<base::RunLoop> orientation_absolute_stopped_runloop_; |
331 | 287 |
332 private: | 288 private: |
333 base::WaitableEvent io_loop_finished_event_; | 289 base::WaitableEvent io_loop_finished_event_; |
334 }; | 290 }; |
(...skipping 16 matching lines...) Expand all Loading... |
351 // handler and navigates to #pass. | 307 // handler and navigates to #pass. |
352 GURL test_url = | 308 GURL test_url = |
353 GetTestUrl("device_sensors", "device_orientation_absolute_test.html"); | 309 GetTestUrl("device_sensors", "device_orientation_absolute_test.html"); |
354 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | 310 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
355 | 311 |
356 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 312 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
357 orientation_absolute_started_runloop_->Run(); | 313 orientation_absolute_started_runloop_->Run(); |
358 orientation_absolute_stopped_runloop_->Run(); | 314 orientation_absolute_stopped_runloop_->Run(); |
359 } | 315 } |
360 | 316 |
361 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, LightTest) { | |
362 // The test page will register an event handler for light events, | |
363 // expects to get an event with fake values, then removes the event | |
364 // handler and navigates to #pass. | |
365 EnableExperimentalFeatures(); | |
366 GURL test_url = GetTestUrl("device_sensors", "device_light_test.html"); | |
367 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | |
368 | |
369 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
370 light_started_runloop_->Run(); | |
371 light_stopped_runloop_->Run(); | |
372 } | |
373 | |
374 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionTest) { | 317 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionTest) { |
375 // The test page will register an event handler for motion events, | 318 // The test page will register an event handler for motion events, |
376 // expects to get an event with fake values, then removes the event | 319 // expects to get an event with fake values, then removes the event |
377 // handler and navigates to #pass. | 320 // handler and navigates to #pass. |
378 GURL test_url = GetTestUrl("device_sensors", "device_motion_test.html"); | 321 GURL test_url = GetTestUrl("device_sensors", "device_motion_test.html"); |
379 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | 322 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
380 | 323 |
381 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 324 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
382 motion_started_runloop_->Run(); | 325 motion_started_runloop_->Run(); |
383 motion_stopped_runloop_->Run(); | 326 motion_stopped_runloop_->Run(); |
384 } | 327 } |
385 | 328 |
386 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, LightOneOffInfintyTest) { | |
387 // The test page registers an event handler for light events and expects | |
388 // to get an event with value equal to infinity, because no sensor data can | |
389 // be provided. | |
390 EnableExperimentalFeatures(); | |
391 fetcher_->SetSensorDataAvailable(false); | |
392 GURL test_url = | |
393 GetTestUrl("device_sensors", "device_light_infinity_test.html"); | |
394 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | |
395 | |
396 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
397 light_started_runloop_->Run(); | |
398 light_stopped_runloop_->Run(); | |
399 } | |
400 | |
401 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationNullTest) { | 329 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationNullTest) { |
402 // The test page registers an event handler for orientation events and | 330 // The test page registers an event handler for orientation events and |
403 // expects to get an event with null values, because no sensor data can be | 331 // expects to get an event with null values, because no sensor data can be |
404 // provided. | 332 // provided. |
405 fetcher_->SetSensorDataAvailable(false); | 333 fetcher_->SetSensorDataAvailable(false); |
406 GURL test_url = | 334 GURL test_url = |
407 GetTestUrl("device_sensors", "device_orientation_null_test.html"); | 335 GetTestUrl("device_sensors", "device_orientation_null_test.html"); |
408 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | 336 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); |
409 | 337 |
410 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 338 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
460 motion_stopped_runloop_->Run(); | 388 motion_stopped_runloop_->Run(); |
461 orientation_started_runloop_->Run(); | 389 orientation_started_runloop_->Run(); |
462 orientation_stopped_runloop_->Run(); | 390 orientation_stopped_runloop_->Run(); |
463 same_tab_observer.Wait(); | 391 same_tab_observer.Wait(); |
464 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | 392 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); |
465 } | 393 } |
466 | 394 |
467 } // namespace | 395 } // namespace |
468 | 396 |
469 } // namespace content | 397 } // namespace content |
OLD | NEW |