OLD | NEW |
| (Empty) |
1 // Copyright 2013 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/synchronization/waitable_event.h" | |
7 #include "base/threading/platform_thread.h" | |
8 #include "content/browser/device_orientation/data_fetcher_shared_memory.h" | |
9 #include "content/browser/device_orientation/device_inertial_sensor_service.h" | |
10 #include "content/common/device_orientation/device_motion_hardware_buffer.h" | |
11 #include "content/common/device_orientation/device_orientation_hardware_buffer.h
" | |
12 #include "content/public/browser/browser_thread.h" | |
13 #include "content/public/browser/web_contents.h" | |
14 #include "content/public/common/content_switches.h" | |
15 #include "content/public/test/content_browser_test.h" | |
16 #include "content/public/test/content_browser_test_utils.h" | |
17 #include "content/public/test/test_navigation_observer.h" | |
18 #include "content/public/test/test_utils.h" | |
19 #include "content/shell/browser/shell.h" | |
20 #include "content/shell/browser/shell_javascript_dialog_manager.h" | |
21 | |
22 namespace content { | |
23 | |
24 namespace { | |
25 | |
26 class FakeDataFetcher : public DataFetcherSharedMemory { | |
27 public: | |
28 FakeDataFetcher() | |
29 : started_orientation_(false, false), | |
30 stopped_orientation_(false, false), | |
31 started_motion_(false, false), | |
32 stopped_motion_(false, false), | |
33 sensor_data_available_(true) { | |
34 } | |
35 virtual ~FakeDataFetcher() { } | |
36 | |
37 virtual bool Start(ConsumerType consumer_type, void* buffer) OVERRIDE { | |
38 EXPECT_TRUE(buffer); | |
39 | |
40 switch (consumer_type) { | |
41 case CONSUMER_TYPE_MOTION: | |
42 { | |
43 DeviceMotionHardwareBuffer* motion_buffer = | |
44 static_cast<DeviceMotionHardwareBuffer*>(buffer); | |
45 if (sensor_data_available_) | |
46 UpdateMotion(motion_buffer); | |
47 SetMotionBufferReady(motion_buffer); | |
48 started_motion_.Signal(); | |
49 } | |
50 break; | |
51 case CONSUMER_TYPE_ORIENTATION: | |
52 { | |
53 DeviceOrientationHardwareBuffer* orientation_buffer = | |
54 static_cast<DeviceOrientationHardwareBuffer*>(buffer); | |
55 if (sensor_data_available_) | |
56 UpdateOrientation(orientation_buffer); | |
57 SetOrientationBufferReady(orientation_buffer); | |
58 started_orientation_.Signal(); | |
59 } | |
60 break; | |
61 default: | |
62 return false; | |
63 } | |
64 return true; | |
65 } | |
66 | |
67 virtual bool Stop(ConsumerType consumer_type) OVERRIDE { | |
68 switch (consumer_type) { | |
69 case CONSUMER_TYPE_MOTION: | |
70 stopped_motion_.Signal(); | |
71 break; | |
72 case CONSUMER_TYPE_ORIENTATION: | |
73 stopped_orientation_.Signal(); | |
74 break; | |
75 default: | |
76 return false; | |
77 } | |
78 return true; | |
79 } | |
80 | |
81 virtual void Fetch(unsigned consumer_bitmask) OVERRIDE { | |
82 FAIL() << "fetch should not be called"; | |
83 } | |
84 | |
85 virtual FetcherType GetType() const OVERRIDE { | |
86 return FETCHER_TYPE_DEFAULT; | |
87 } | |
88 | |
89 void SetSensorDataAvailable(bool available) { | |
90 sensor_data_available_ = available; | |
91 } | |
92 | |
93 void SetMotionBufferReady(DeviceMotionHardwareBuffer* buffer) { | |
94 buffer->seqlock.WriteBegin(); | |
95 buffer->data.allAvailableSensorsAreActive = true; | |
96 buffer->seqlock.WriteEnd(); | |
97 } | |
98 | |
99 void SetOrientationBufferReady(DeviceOrientationHardwareBuffer* buffer) { | |
100 buffer->seqlock.WriteBegin(); | |
101 buffer->data.allAvailableSensorsAreActive = true; | |
102 buffer->seqlock.WriteEnd(); | |
103 } | |
104 | |
105 void UpdateMotion(DeviceMotionHardwareBuffer* buffer) { | |
106 buffer->seqlock.WriteBegin(); | |
107 buffer->data.accelerationX = 1; | |
108 buffer->data.hasAccelerationX = true; | |
109 buffer->data.accelerationY = 2; | |
110 buffer->data.hasAccelerationY = true; | |
111 buffer->data.accelerationZ = 3; | |
112 buffer->data.hasAccelerationZ = true; | |
113 | |
114 buffer->data.accelerationIncludingGravityX = 4; | |
115 buffer->data.hasAccelerationIncludingGravityX = true; | |
116 buffer->data.accelerationIncludingGravityY = 5; | |
117 buffer->data.hasAccelerationIncludingGravityY = true; | |
118 buffer->data.accelerationIncludingGravityZ = 6; | |
119 buffer->data.hasAccelerationIncludingGravityZ = true; | |
120 | |
121 buffer->data.rotationRateAlpha = 7; | |
122 buffer->data.hasRotationRateAlpha = true; | |
123 buffer->data.rotationRateBeta = 8; | |
124 buffer->data.hasRotationRateBeta = true; | |
125 buffer->data.rotationRateGamma = 9; | |
126 buffer->data.hasRotationRateGamma = true; | |
127 | |
128 buffer->data.interval = 100; | |
129 buffer->data.allAvailableSensorsAreActive = true; | |
130 buffer->seqlock.WriteEnd(); | |
131 } | |
132 | |
133 void UpdateOrientation(DeviceOrientationHardwareBuffer* buffer) { | |
134 buffer->seqlock.WriteBegin(); | |
135 buffer->data.alpha = 1; | |
136 buffer->data.hasAlpha = true; | |
137 buffer->data.beta = 2; | |
138 buffer->data.hasBeta = true; | |
139 buffer->data.gamma = 3; | |
140 buffer->data.hasGamma = true; | |
141 buffer->data.allAvailableSensorsAreActive = true; | |
142 buffer->seqlock.WriteEnd(); | |
143 } | |
144 | |
145 base::WaitableEvent started_orientation_; | |
146 base::WaitableEvent stopped_orientation_; | |
147 base::WaitableEvent started_motion_; | |
148 base::WaitableEvent stopped_motion_; | |
149 bool sensor_data_available_; | |
150 | |
151 private: | |
152 | |
153 DISALLOW_COPY_AND_ASSIGN(FakeDataFetcher); | |
154 }; | |
155 | |
156 | |
157 class DeviceInertialSensorBrowserTest : public ContentBrowserTest { | |
158 public: | |
159 DeviceInertialSensorBrowserTest() | |
160 : fetcher_(NULL), | |
161 io_loop_finished_event_(false, false) { | |
162 } | |
163 | |
164 virtual void SetUpOnMainThread() OVERRIDE { | |
165 BrowserThread::PostTask( | |
166 BrowserThread::IO, FROM_HERE, | |
167 base::Bind(&DeviceInertialSensorBrowserTest::SetUpOnIOThread, this)); | |
168 io_loop_finished_event_.Wait(); | |
169 } | |
170 | |
171 void SetUpOnIOThread() { | |
172 fetcher_ = new FakeDataFetcher(); | |
173 DeviceInertialSensorService::GetInstance()-> | |
174 SetDataFetcherForTests(fetcher_); | |
175 io_loop_finished_event_.Signal(); | |
176 } | |
177 | |
178 void DelayAndQuit(base::TimeDelta delay) { | |
179 base::PlatformThread::Sleep(delay); | |
180 base::MessageLoop::current()->QuitWhenIdle(); | |
181 } | |
182 | |
183 void WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta delay) { | |
184 ShellJavaScriptDialogManager* dialog_manager= | |
185 static_cast<ShellJavaScriptDialogManager*>( | |
186 shell()->GetJavaScriptDialogManager()); | |
187 | |
188 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner(); | |
189 dialog_manager->set_dialog_request_callback( | |
190 base::Bind(&DeviceInertialSensorBrowserTest::DelayAndQuit, this, | |
191 delay)); | |
192 runner->Run(); | |
193 } | |
194 | |
195 FakeDataFetcher* fetcher_; | |
196 | |
197 private: | |
198 base::WaitableEvent io_loop_finished_event_; | |
199 }; | |
200 | |
201 | |
202 IN_PROC_BROWSER_TEST_F(DeviceInertialSensorBrowserTest, OrientationTest) { | |
203 // The test page will register an event handler for orientation events, | |
204 // expects to get an event with fake values, then removes the event | |
205 // handler and navigates to #pass. | |
206 GURL test_url = GetTestUrl( | |
207 "device_orientation", "device_orientation_test.html"); | |
208 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | |
209 | |
210 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
211 fetcher_->started_orientation_.Wait(); | |
212 fetcher_->stopped_orientation_.Wait(); | |
213 } | |
214 | |
215 IN_PROC_BROWSER_TEST_F(DeviceInertialSensorBrowserTest, MotionTest) { | |
216 // The test page will register an event handler for motion events, | |
217 // expects to get an event with fake values, then removes the event | |
218 // handler and navigates to #pass. | |
219 GURL test_url = GetTestUrl( | |
220 "device_orientation", "device_motion_test.html"); | |
221 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2); | |
222 | |
223 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
224 fetcher_->started_motion_.Wait(); | |
225 fetcher_->stopped_motion_.Wait(); | |
226 } | |
227 | |
228 // Flaking in the android try bot. See http://crbug.com/360578. | |
229 #if defined(OS_ANDROID) | |
230 #define MAYBE_OrientationNullTestWithAlert DISABLED_OrientationNullTestWithAlert | |
231 #else | |
232 #define MAYBE_OrientationNullTestWithAlert OrientationNullTestWithAlert | |
233 #endif | |
234 IN_PROC_BROWSER_TEST_F(DeviceInertialSensorBrowserTest, | |
235 MAYBE_OrientationNullTestWithAlert) { | |
236 // The test page will register an event handler for orientation events, | |
237 // expects to get an event with null values. The test raises a modal alert | |
238 // dialog with a delay to test that the one-off null-event still propagates | |
239 // to window after the alert is dismissed and the callback is invoked which | |
240 // navigates to #pass. | |
241 fetcher_->SetSensorDataAvailable(false); | |
242 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); | |
243 | |
244 GURL test_url = GetTestUrl( | |
245 "device_orientation", "device_orientation_null_test_with_alert.html"); | |
246 shell()->LoadURL(test_url); | |
247 | |
248 // TODO(timvolodine): investigate if it is possible to test this without | |
249 // delay, crbug.com/360044. | |
250 WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta::FromMilliseconds(1000)); | |
251 | |
252 fetcher_->started_orientation_.Wait(); | |
253 fetcher_->stopped_orientation_.Wait(); | |
254 same_tab_observer.Wait(); | |
255 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
256 } | |
257 | |
258 // Flaking in the android try bot. See http://crbug.com/360578. | |
259 #if defined(OS_ANDROID) | |
260 #define MAYBE_MotionNullTestWithAlert DISABLED_MotionNullTestWithAlert | |
261 #else | |
262 #define MAYBE_MotionNullTestWithAlert MotionNullTestWithAlert | |
263 #endif | |
264 IN_PROC_BROWSER_TEST_F(DeviceInertialSensorBrowserTest, | |
265 MAYBE_MotionNullTestWithAlert) { | |
266 // The test page will register an event handler for motion events, | |
267 // expects to get an event with null values. The test raises a modal alert | |
268 // dialog with a delay to test that the one-off null-event still propagates | |
269 // to window after the alert is dismissed and the callback is invoked which | |
270 // navigates to #pass. | |
271 fetcher_->SetSensorDataAvailable(false); | |
272 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2); | |
273 | |
274 GURL test_url = GetTestUrl( | |
275 "device_orientation", "device_motion_null_test_with_alert.html"); | |
276 shell()->LoadURL(test_url); | |
277 | |
278 // TODO(timvolodine): investigate if it is possible to test this without | |
279 // delay, crbug.com/360044. | |
280 WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta::FromMilliseconds(1000)); | |
281 | |
282 fetcher_->started_motion_.Wait(); | |
283 fetcher_->stopped_motion_.Wait(); | |
284 same_tab_observer.Wait(); | |
285 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref()); | |
286 } | |
287 | |
288 } // namespace | |
289 | |
290 } // namespace content | |
OLD | NEW |