Apply _RELATIVE relocations ahead of others.
[chromium-blink-merge.git] / content / browser / device_sensors / device_inertial_sensor_browsertest.cc
blob3380d8a2e74ab43ff229f4115e4676f76b525219
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.
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_sensors/data_fetcher_shared_memory.h"
9 #include "content/browser/device_sensors/device_inertial_sensor_service.h"
10 #include "content/common/device_sensors/device_light_hardware_buffer.h"
11 #include "content/common/device_sensors/device_motion_hardware_buffer.h"
12 #include "content/common/device_sensors/device_orientation_hardware_buffer.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/common/content_switches.h"
16 #include "content/public/test/content_browser_test.h"
17 #include "content/public/test/content_browser_test_utils.h"
18 #include "content/public/test/test_navigation_observer.h"
19 #include "content/public/test/test_utils.h"
20 #include "content/shell/browser/shell.h"
21 #include "content/shell/browser/shell_javascript_dialog_manager.h"
23 namespace content {
25 namespace {
27 class FakeDataFetcher : public DataFetcherSharedMemory {
28 public:
29 FakeDataFetcher()
30 : started_orientation_(false, false),
31 stopped_orientation_(false, false),
32 started_motion_(false, false),
33 stopped_motion_(false, false),
34 started_light_(false, false),
35 stopped_light_(false, false),
36 sensor_data_available_(true) {}
37 ~FakeDataFetcher() override {}
39 bool Start(ConsumerType consumer_type, void* buffer) override {
40 EXPECT_TRUE(buffer);
42 switch (consumer_type) {
43 case CONSUMER_TYPE_MOTION:
45 DeviceMotionHardwareBuffer* motion_buffer =
46 static_cast<DeviceMotionHardwareBuffer*>(buffer);
47 if (sensor_data_available_)
48 UpdateMotion(motion_buffer);
49 SetMotionBufferReady(motion_buffer);
50 started_motion_.Signal();
52 break;
53 case CONSUMER_TYPE_ORIENTATION:
55 DeviceOrientationHardwareBuffer* orientation_buffer =
56 static_cast<DeviceOrientationHardwareBuffer*>(buffer);
57 if (sensor_data_available_)
58 UpdateOrientation(orientation_buffer);
59 SetOrientationBufferReady(orientation_buffer);
60 started_orientation_.Signal();
62 break;
63 case CONSUMER_TYPE_LIGHT:
65 DeviceLightHardwareBuffer* light_buffer =
66 static_cast<DeviceLightHardwareBuffer*>(buffer);
67 UpdateLight(light_buffer,
68 sensor_data_available_
69 ? 100
70 : std::numeric_limits<double>::infinity());
71 started_light_.Signal();
73 break;
74 default:
75 return false;
77 return true;
80 bool Stop(ConsumerType consumer_type) override {
81 switch (consumer_type) {
82 case CONSUMER_TYPE_MOTION:
83 stopped_motion_.Signal();
84 break;
85 case CONSUMER_TYPE_ORIENTATION:
86 stopped_orientation_.Signal();
87 break;
88 case CONSUMER_TYPE_LIGHT:
89 stopped_light_.Signal();
90 break;
91 default:
92 return false;
94 return true;
97 void Fetch(unsigned consumer_bitmask) override {
98 FAIL() << "fetch should not be called";
101 FetcherType GetType() const override { return FETCHER_TYPE_DEFAULT; }
103 void SetSensorDataAvailable(bool available) {
104 sensor_data_available_ = available;
107 void SetMotionBufferReady(DeviceMotionHardwareBuffer* buffer) {
108 buffer->seqlock.WriteBegin();
109 buffer->data.allAvailableSensorsAreActive = true;
110 buffer->seqlock.WriteEnd();
113 void SetOrientationBufferReady(DeviceOrientationHardwareBuffer* buffer) {
114 buffer->seqlock.WriteBegin();
115 buffer->data.allAvailableSensorsAreActive = true;
116 buffer->seqlock.WriteEnd();
119 void UpdateMotion(DeviceMotionHardwareBuffer* buffer) {
120 buffer->seqlock.WriteBegin();
121 buffer->data.accelerationX = 1;
122 buffer->data.hasAccelerationX = true;
123 buffer->data.accelerationY = 2;
124 buffer->data.hasAccelerationY = true;
125 buffer->data.accelerationZ = 3;
126 buffer->data.hasAccelerationZ = true;
128 buffer->data.accelerationIncludingGravityX = 4;
129 buffer->data.hasAccelerationIncludingGravityX = true;
130 buffer->data.accelerationIncludingGravityY = 5;
131 buffer->data.hasAccelerationIncludingGravityY = true;
132 buffer->data.accelerationIncludingGravityZ = 6;
133 buffer->data.hasAccelerationIncludingGravityZ = true;
135 buffer->data.rotationRateAlpha = 7;
136 buffer->data.hasRotationRateAlpha = true;
137 buffer->data.rotationRateBeta = 8;
138 buffer->data.hasRotationRateBeta = true;
139 buffer->data.rotationRateGamma = 9;
140 buffer->data.hasRotationRateGamma = true;
142 buffer->data.interval = 100;
143 buffer->data.allAvailableSensorsAreActive = true;
144 buffer->seqlock.WriteEnd();
147 void UpdateOrientation(DeviceOrientationHardwareBuffer* buffer) {
148 buffer->seqlock.WriteBegin();
149 buffer->data.alpha = 1;
150 buffer->data.hasAlpha = true;
151 buffer->data.beta = 2;
152 buffer->data.hasBeta = true;
153 buffer->data.gamma = 3;
154 buffer->data.hasGamma = true;
155 buffer->data.allAvailableSensorsAreActive = true;
156 buffer->seqlock.WriteEnd();
159 void UpdateLight(DeviceLightHardwareBuffer* buffer, double lux) {
160 buffer->seqlock.WriteBegin();
161 buffer->data.value = lux;
162 buffer->seqlock.WriteEnd();
165 base::WaitableEvent started_orientation_;
166 base::WaitableEvent stopped_orientation_;
167 base::WaitableEvent started_motion_;
168 base::WaitableEvent stopped_motion_;
169 base::WaitableEvent started_light_;
170 base::WaitableEvent stopped_light_;
171 bool sensor_data_available_;
173 private:
174 DISALLOW_COPY_AND_ASSIGN(FakeDataFetcher);
178 class DeviceInertialSensorBrowserTest : public ContentBrowserTest {
179 public:
180 DeviceInertialSensorBrowserTest()
181 : fetcher_(NULL),
182 io_loop_finished_event_(false, false) {
185 void SetUpOnMainThread() override {
186 BrowserThread::PostTask(
187 BrowserThread::IO, FROM_HERE,
188 base::Bind(&DeviceInertialSensorBrowserTest::SetUpOnIOThread, this));
189 io_loop_finished_event_.Wait();
192 void SetUpOnIOThread() {
193 fetcher_ = new FakeDataFetcher();
194 DeviceInertialSensorService::GetInstance()->
195 SetDataFetcherForTesting(fetcher_);
196 io_loop_finished_event_.Signal();
199 void DelayAndQuit(base::TimeDelta delay) {
200 base::PlatformThread::Sleep(delay);
201 base::MessageLoop::current()->QuitWhenIdle();
204 void WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta delay) {
205 ShellJavaScriptDialogManager* dialog_manager=
206 static_cast<ShellJavaScriptDialogManager*>(
207 shell()->GetJavaScriptDialogManager());
209 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner();
210 dialog_manager->set_dialog_request_callback(
211 base::Bind(&DeviceInertialSensorBrowserTest::DelayAndQuit, this,
212 delay));
213 runner->Run();
216 FakeDataFetcher* fetcher_;
218 private:
219 base::WaitableEvent io_loop_finished_event_;
222 IN_PROC_BROWSER_TEST_F(DeviceInertialSensorBrowserTest, OrientationTest) {
223 // The test page will register an event handler for orientation events,
224 // expects to get an event with fake values, then removes the event
225 // handler and navigates to #pass.
226 GURL test_url = GetTestUrl("device_sensors", "device_orientation_test.html");
227 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
229 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
230 fetcher_->started_orientation_.Wait();
231 fetcher_->stopped_orientation_.Wait();
234 IN_PROC_BROWSER_TEST_F(DeviceInertialSensorBrowserTest, LightTest) {
235 // The test page will register an event handler for light events,
236 // expects to get an event with fake values, then removes the event
237 // handler and navigates to #pass.
238 GURL test_url = GetTestUrl("device_sensors", "device_light_test.html");
240 // TODO(riju): remove command line args when the feature goes stable.
241 if (!CommandLine::ForCurrentProcess()->HasSwitch(
242 switches::kEnableExperimentalWebPlatformFeatures)) {
243 CommandLine::ForCurrentProcess()->AppendSwitch(
244 switches::kEnableExperimentalWebPlatformFeatures);
247 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
249 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
250 fetcher_->started_light_.Wait();
251 fetcher_->stopped_light_.Wait();
254 IN_PROC_BROWSER_TEST_F(DeviceInertialSensorBrowserTest, MotionTest) {
255 // The test page will register an event handler for motion events,
256 // expects to get an event with fake values, then removes the event
257 // handler and navigates to #pass.
258 GURL test_url = GetTestUrl("device_sensors", "device_motion_test.html");
259 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
261 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
262 fetcher_->started_motion_.Wait();
263 fetcher_->stopped_motion_.Wait();
266 // crbug/416406. The test is flaky.
267 IN_PROC_BROWSER_TEST_F(DeviceInertialSensorBrowserTest,
268 DISABLED_LightOneOffInfintyTest) {
269 // The test page will register an event handler for light events,
270 // expects to get an event with value equal to Infinity. This tests that the
271 // one-off infinity event still propagates to window after the alert is
272 // dismissed and the callback is invoked which navigates to #pass.
273 fetcher_->SetSensorDataAvailable(false);
275 // TODO(riju): remove command line args when the feature goes stable.
276 if (!CommandLine::ForCurrentProcess()->HasSwitch(
277 switches::kEnableExperimentalWebPlatformFeatures)) {
278 CommandLine::ForCurrentProcess()->AppendSwitch(
279 switches::kEnableExperimentalWebPlatformFeatures);
282 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2);
284 GURL test_url =
285 GetTestUrl("device_sensors", "device_light_infinity_test.html");
286 shell()->LoadURL(test_url);
288 WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta::FromMilliseconds(1000));
290 fetcher_->started_light_.Wait();
291 fetcher_->stopped_light_.Wait();
292 same_tab_observer.Wait();
293 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
296 // Flaking in the android try bot. See http://crbug.com/360578.
297 #if defined(OS_ANDROID)
298 #define MAYBE_OrientationNullTestWithAlert DISABLED_OrientationNullTestWithAlert
299 #else
300 #define MAYBE_OrientationNullTestWithAlert OrientationNullTestWithAlert
301 #endif
302 IN_PROC_BROWSER_TEST_F(DeviceInertialSensorBrowserTest,
303 MAYBE_OrientationNullTestWithAlert) {
304 // The test page will register an event handler for orientation events,
305 // expects to get an event with null values. The test raises a modal alert
306 // dialog with a delay to test that the one-off null-event still propagates
307 // to window after the alert is dismissed and the callback is invoked which
308 // navigates to #pass.
309 fetcher_->SetSensorDataAvailable(false);
310 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2);
312 GURL test_url = GetTestUrl("device_sensors",
313 "device_orientation_null_test_with_alert.html");
314 shell()->LoadURL(test_url);
316 // TODO(timvolodine): investigate if it is possible to test this without
317 // delay, crbug.com/360044.
318 WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta::FromMilliseconds(1000));
320 fetcher_->started_orientation_.Wait();
321 fetcher_->stopped_orientation_.Wait();
322 same_tab_observer.Wait();
323 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
326 // Flaking in the android try bot. See http://crbug.com/360578.
327 #if defined(OS_ANDROID) || defined(OS_WIN)
328 #define MAYBE_MotionNullTestWithAlert DISABLED_MotionNullTestWithAlert
329 #else
330 #define MAYBE_MotionNullTestWithAlert MotionNullTestWithAlert
331 #endif
332 IN_PROC_BROWSER_TEST_F(DeviceInertialSensorBrowserTest,
333 MAYBE_MotionNullTestWithAlert) {
334 // The test page will register an event handler for motion events,
335 // expects to get an event with null values. The test raises a modal alert
336 // dialog with a delay to test that the one-off null-event still propagates
337 // to window after the alert is dismissed and the callback is invoked which
338 // navigates to #pass.
339 fetcher_->SetSensorDataAvailable(false);
340 TestNavigationObserver same_tab_observer(shell()->web_contents(), 2);
342 GURL test_url =
343 GetTestUrl("device_sensors", "device_motion_null_test_with_alert.html");
344 shell()->LoadURL(test_url);
346 // TODO(timvolodine): investigate if it is possible to test this without
347 // delay, crbug.com/360044.
348 WaitForAlertDialogAndQuitAfterDelay(base::TimeDelta::FromMilliseconds(1000));
350 fetcher_->started_motion_.Wait();
351 fetcher_->stopped_motion_.Wait();
352 same_tab_observer.Wait();
353 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
356 } // namespace
358 } // namespace content