OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/command_line.h" | |
6 #include "base/macros.h" | |
7 #include "base/message_loop/message_loop.h" | |
8 #include "base/synchronization/waitable_event.h" | |
9 #include "base/threading/platform_thread.h" | |
10 #include "build/build_config.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 #include "content/public/browser/web_contents.h" | |
13 #include "content/public/common/content_switches.h" | |
14 #include "content/public/test/content_browser_test.h" | |
15 #include "content/public/test/content_browser_test_utils.h" | |
16 #include "content/public/test/test_navigation_observer.h" | |
17 #include "content/public/test/test_utils.h" | |
18 #include "content/shell/browser/shell.h" | |
19 #include "content/shell/browser/shell_javascript_dialog_manager.h" | |
20 #include "device/sensors/data_fetcher_shared_memory.h" | |
21 #include "device/sensors/device_sensor_service.h" | |
22 #include "device/sensors/public/cpp/device_motion_hardware_buffer.h" | |
23 #include "device/sensors/public/cpp/device_orientation_hardware_buffer.h" | |
24 | |
25 namespace content { | |
26 | |
27 namespace { | |
28 | |
29 class FakeDataFetcher : public device::DataFetcherSharedMemory { | |
30 public: | |
31 FakeDataFetcher() : sensor_data_available_(true) {} | |
32 ~FakeDataFetcher() override {} | |
33 | |
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( | |
43 base::Closure orientation_started_callback) { | |
44 orientation_started_callback_ = orientation_started_callback; | |
45 } | |
46 | |
47 void SetOrientationStoppedCallback( | |
48 base::Closure orientation_stopped_callback) { | |
49 orientation_stopped_callback_ = orientation_stopped_callback; | |
50 } | |
51 | |
52 void SetOrientationAbsoluteStartedCallback( | |
53 base::Closure orientation_absolute_started_callback) { | |
54 orientation_absolute_started_callback_ = | |
55 orientation_absolute_started_callback; | |
56 } | |
57 | |
58 void SetOrientationAbsoluteStoppedCallback( | |
59 base::Closure orientation_absolute_stopped_callback) { | |
60 orientation_absolute_stopped_callback_ = | |
61 orientation_absolute_stopped_callback; | |
62 } | |
63 | |
64 bool Start(device::ConsumerType consumer_type, void* buffer) override { | |
65 EXPECT_TRUE(buffer); | |
66 | |
67 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: { | |
78 device::DeviceOrientationHardwareBuffer* orientation_buffer = | |
79 static_cast<device::DeviceOrientationHardwareBuffer*>(buffer); | |
80 if (sensor_data_available_) | |
81 UpdateOrientation(orientation_buffer); | |
82 SetOrientationBufferReady(orientation_buffer); | |
83 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
84 orientation_started_callback_); | |
85 } break; | |
86 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: { | |
87 device::DeviceOrientationHardwareBuffer* orientation_buffer = | |
88 static_cast<device::DeviceOrientationHardwareBuffer*>(buffer); | |
89 if (sensor_data_available_) | |
90 UpdateOrientationAbsolute(orientation_buffer); | |
91 SetOrientationBufferReady(orientation_buffer); | |
92 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
93 orientation_absolute_started_callback_); | |
94 } break; | |
95 default: | |
96 return false; | |
97 } | |
98 return true; | |
99 } | |
100 | |
101 bool Stop(device::ConsumerType consumer_type) override { | |
102 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: | |
108 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
109 orientation_stopped_callback_); | |
110 break; | |
111 case device::CONSUMER_TYPE_ORIENTATION_ABSOLUTE: | |
112 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
113 orientation_absolute_stopped_callback_); | |
114 break; | |
115 default: | |
116 return false; | |
117 } | |
118 return true; | |
119 } | |
120 | |
121 void Fetch(unsigned consumer_bitmask) override { | |
122 FAIL() << "fetch should not be called"; | |
123 } | |
124 | |
125 FetcherType GetType() const override { return FETCHER_TYPE_DEFAULT; } | |
126 | |
127 void SetSensorDataAvailable(bool available) { | |
128 sensor_data_available_ = available; | |
129 } | |
130 | |
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( | |
138 device::DeviceOrientationHardwareBuffer* buffer) { | |
139 buffer->seqlock.WriteBegin(); | |
140 buffer->data.all_available_sensors_are_active = true; | |
141 buffer->seqlock.WriteEnd(); | |
142 } | |
143 | |
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) { | |
173 buffer->seqlock.WriteBegin(); | |
174 buffer->data.alpha = 1; | |
175 buffer->data.has_alpha = true; | |
176 buffer->data.beta = 2; | |
177 buffer->data.has_beta = true; | |
178 buffer->data.gamma = 3; | |
179 buffer->data.has_gamma = true; | |
180 buffer->data.all_available_sensors_are_active = true; | |
181 buffer->seqlock.WriteEnd(); | |
182 } | |
183 | |
184 void UpdateOrientationAbsolute( | |
185 device::DeviceOrientationHardwareBuffer* buffer) { | |
186 buffer->seqlock.WriteBegin(); | |
187 buffer->data.alpha = 4; | |
188 buffer->data.has_alpha = true; | |
189 buffer->data.beta = 5; | |
190 buffer->data.has_beta = true; | |
191 buffer->data.gamma = 6; | |
192 buffer->data.has_gamma = true; | |
193 buffer->data.absolute = true; | |
194 buffer->data.all_available_sensors_are_active = true; | |
195 buffer->seqlock.WriteEnd(); | |
196 } | |
197 | |
198 // The below callbacks should be run on the UI thread. | |
199 base::Closure motion_started_callback_; | |
200 base::Closure orientation_started_callback_; | |
201 base::Closure orientation_absolute_started_callback_; | |
202 base::Closure motion_stopped_callback_; | |
203 base::Closure orientation_stopped_callback_; | |
204 base::Closure orientation_absolute_stopped_callback_; | |
205 bool sensor_data_available_; | |
206 | |
207 private: | |
208 DISALLOW_COPY_AND_ASSIGN(FakeDataFetcher); | |
209 }; | |
210 | |
211 class DeviceSensorBrowserTest : public ContentBrowserTest { | |
212 public: | |
213 DeviceSensorBrowserTest() | |
214 : fetcher_(nullptr), | |
215 io_loop_finished_event_( | |
216 base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
217 base::WaitableEvent::InitialState::NOT_SIGNALED) {} | |
218 | |
219 void SetUpOnMainThread() override { | |
220 // 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()); | |
224 orientation_stopped_runloop_.reset(new base::RunLoop()); | |
225 orientation_absolute_started_runloop_.reset(new base::RunLoop()); | |
226 orientation_absolute_stopped_runloop_.reset(new base::RunLoop()); | |
227 #if defined(OS_ANDROID) | |
228 // On Android, the DeviceSensorService lives on the UI thread. | |
229 SetUpFetcher(); | |
230 #else | |
231 // On all other platforms, the DeviceSensorService lives on the IO thread. | |
232 BrowserThread::PostTask( | |
233 BrowserThread::IO, FROM_HERE, | |
234 base::Bind(&DeviceSensorBrowserTest::SetUpOnIOThread, | |
235 base::Unretained(this))); | |
236 io_loop_finished_event_.Wait(); | |
237 #endif | |
238 } | |
239 | |
240 void SetUpFetcher() { | |
241 fetcher_ = new FakeDataFetcher(); | |
242 fetcher_->SetMotionStartedCallback(motion_started_runloop_->QuitClosure()); | |
243 fetcher_->SetMotionStoppedCallback(motion_stopped_runloop_->QuitClosure()); | |
244 fetcher_->SetOrientationStartedCallback( | |
245 orientation_started_runloop_->QuitClosure()); | |
246 fetcher_->SetOrientationStoppedCallback( | |
247 orientation_stopped_runloop_->QuitClosure()); | |
248 fetcher_->SetOrientationAbsoluteStartedCallback( | |
249 orientation_absolute_started_runloop_->QuitClosure()); | |
250 fetcher_->SetOrientationAbsoluteStoppedCallback( | |
251 orientation_absolute_stopped_runloop_->QuitClosure()); | |
252 device::DeviceSensorService::GetInstance()->SetDataFetcherForTesting( | |
253 fetcher_); | |
254 } | |
255 | |
256 void SetUpOnIOThread() { | |
257 SetUpFetcher(); | |
258 io_loop_finished_event_.Signal(); | |
259 } | |
260 | |
261 void DelayAndQuit(base::TimeDelta delay) { | |
262 base::PlatformThread::Sleep(delay); | |
263 base::MessageLoop::current()->QuitWhenIdle(); | |
264 } | |
265 | |
266 void WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta delay) { | |
267 ShellJavaScriptDialogManager* dialog_manager = | |
268 static_cast<ShellJavaScriptDialogManager*>( | |
269 shell()->GetJavaScriptDialogManager(shell()->web_contents())); | |
270 | |
271 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner(); | |
272 dialog_manager->set_dialog_request_callback( | |
273 base::Bind(&DeviceSensorBrowserTest::DelayAndQuit, | |
274 base::Unretained(this), delay)); | |
275 runner->Run(); | |
276 } | |
277 | |
278 FakeDataFetcher* fetcher_; | |
279 | |
280 // NOTE: These can only be initialized once the main thread has been created | |
281 // 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_; | |
285 std::unique_ptr<base::RunLoop> orientation_stopped_runloop_; | |
286 std::unique_ptr<base::RunLoop> orientation_absolute_started_runloop_; | |
287 std::unique_ptr<base::RunLoop> orientation_absolute_stopped_runloop_; | |
288 | |
289 private: | |
290 base::WaitableEvent io_loop_finished_event_; | |
291 }; | |
292 | |
293 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationTest) { | |
294 // The test page will register an event handler for orientation events, | |
295 // expects to get an event with fake values, then removes the event | |
296 // handler and navigates to #pass. | |
297 GURL test_url = GetTestUrl("device_sensors", "device_orientation_test.html"); | |
298 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | |
299 | |
300 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
301 orientation_started_runloop_->Run(); | |
302 orientation_stopped_runloop_->Run(); | |
303 } | |
304 | |
305 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationAbsoluteTest) { | |
306 // The test page will register an event handler for absolute orientation | |
307 // events, expects to get an event with fake values, then removes the event | |
308 // handler and navigates to #pass. | |
309 GURL test_url = | |
310 GetTestUrl("device_sensors", "device_orientation_absolute_test.html"); | |
311 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | |
312 | |
313 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
314 orientation_absolute_started_runloop_->Run(); | |
315 orientation_absolute_stopped_runloop_->Run(); | |
316 } | |
317 | |
318 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionTest) { | |
319 // The test page will register an event handler for motion events, | |
320 // expects to get an event with fake values, then removes the event | |
321 // handler and navigates to #pass. | |
322 GURL test_url = GetTestUrl("device_sensors", "device_motion_test.html"); | |
323 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | |
324 | |
325 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
326 motion_started_runloop_->Run(); | |
327 motion_stopped_runloop_->Run(); | |
328 } | |
329 | |
330 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationNullTest) { | |
331 // 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 | |
333 // provided. | |
334 fetcher_->SetSensorDataAvailable(false); | |
335 GURL test_url = | |
336 GetTestUrl("device_sensors", "device_orientation_null_test.html"); | |
337 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | |
338 | |
339 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
340 orientation_started_runloop_->Run(); | |
341 orientation_stopped_runloop_->Run(); | |
342 } | |
343 | |
344 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, OrientationAbsoluteNullTest) { | |
345 // The test page registers an event handler for absolute orientation events | |
346 // and expects to get an event with null values, because no sensor data can be | |
347 // provided. | |
348 fetcher_->SetSensorDataAvailable(false); | |
349 GURL test_url = GetTestUrl("device_sensors", | |
350 "device_orientation_absolute_null_test.html"); | |
351 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | |
352 | |
353 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
354 orientation_absolute_started_runloop_->Run(); | |
355 orientation_absolute_stopped_runloop_->Run(); | |
356 } | |
357 | |
358 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, MotionNullTest) { | |
359 // 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 | |
361 // provided. | |
362 fetcher_->SetSensorDataAvailable(false); | |
363 GURL test_url = GetTestUrl("device_sensors", "device_motion_null_test.html"); | |
364 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | |
365 | |
366 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
367 motion_started_runloop_->Run(); | |
368 motion_stopped_runloop_->Run(); | |
369 } | |
370 | |
371 IN_PROC_BROWSER_TEST_F(DeviceSensorBrowserTest, NullTestWithAlert) { | |
372 // 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 | |
374 // 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 | |
376 // eventually navigate to #pass. | |
377 fetcher_->SetSensorDataAvailable(false); | |
378 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); | |
379 | |
380 GURL test_url = | |
381 GetTestUrl("device_sensors", "device_sensors_null_test_with_alert.html"); | |
382 shell()->LoadURL(test_url); | |
383 | |
384 // TODO(timvolodine): investigate if it is possible to test this without | |
385 // delay, crbug.com/360044. | |
386 WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta::FromMilliseconds(500)); | |
387 | |
388 motion_started_runloop_->Run(); | |
389 motion_stopped_runloop_->Run(); | |
390 orientation_started_runloop_->Run(); | |
391 orientation_stopped_runloop_->Run(); | |
392 same_tab_observer.Wait(); | |
393 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
394 } | |
395 | |
396 } // namespace | |
397 | |
398 } // namespace content | |
OLD | NEW |