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.
5 #include "base/command_line.h"
6 #include "base/synchronization/waitable_event.h"
7 #include "content/browser/device_orientation/data_fetcher_shared_memory.h"
8 #include "content/browser/device_orientation/device_inertial_sensor_service.h"
9 #include "content/common/device_orientation/device_motion_hardware_buffer.h"
10 #include "content/common/device_orientation/device_orientation_hardware_buffer.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/shell/browser/shell.h"
15 #include "content/test/content_browser_test.h"
16 #include "content/test/content_browser_test_utils.h"
22 class FakeDataFetcher
: public DataFetcherSharedMemory
{
25 : started_orientation_(false, false),
26 stopped_orientation_(false, false),
27 started_motion_(false, false),
28 stopped_motion_(false, false) {
30 virtual ~FakeDataFetcher() { }
32 virtual bool Start(ConsumerType consumer_type
, void* buffer
) OVERRIDE
{
35 switch (consumer_type
) {
36 case CONSUMER_TYPE_MOTION
:
37 UpdateMotion(static_cast<DeviceMotionHardwareBuffer
*>(buffer
));
38 started_motion_
.Signal();
40 case CONSUMER_TYPE_ORIENTATION
:
42 static_cast<DeviceOrientationHardwareBuffer
*>(buffer
));
43 started_orientation_
.Signal();
51 virtual bool Stop(ConsumerType consumer_type
) OVERRIDE
{
52 switch (consumer_type
) {
53 case CONSUMER_TYPE_MOTION
:
54 stopped_motion_
.Signal();
56 case CONSUMER_TYPE_ORIENTATION
:
57 stopped_orientation_
.Signal();
65 virtual void Fetch(unsigned consumer_bitmask
) OVERRIDE
{
66 FAIL() << "fetch should not be called";
69 virtual FetcherType
GetType() const OVERRIDE
{
70 return FETCHER_TYPE_DEFAULT
;
73 void UpdateMotion(DeviceMotionHardwareBuffer
* buffer
) {
74 buffer
->seqlock
.WriteBegin();
75 buffer
->data
.accelerationX
= 1;
76 buffer
->data
.hasAccelerationX
= true;
77 buffer
->data
.accelerationY
= 2;
78 buffer
->data
.hasAccelerationY
= true;
79 buffer
->data
.accelerationZ
= 3;
80 buffer
->data
.hasAccelerationZ
= true;
82 buffer
->data
.accelerationIncludingGravityX
= 4;
83 buffer
->data
.hasAccelerationIncludingGravityX
= true;
84 buffer
->data
.accelerationIncludingGravityY
= 5;
85 buffer
->data
.hasAccelerationIncludingGravityY
= true;
86 buffer
->data
.accelerationIncludingGravityZ
= 6;
87 buffer
->data
.hasAccelerationIncludingGravityZ
= true;
89 buffer
->data
.rotationRateAlpha
= 7;
90 buffer
->data
.hasRotationRateAlpha
= true;
91 buffer
->data
.rotationRateBeta
= 8;
92 buffer
->data
.hasRotationRateBeta
= true;
93 buffer
->data
.rotationRateGamma
= 9;
94 buffer
->data
.hasRotationRateGamma
= true;
96 buffer
->data
.interval
= 100;
97 buffer
->data
.allAvailableSensorsAreActive
= true;
98 buffer
->seqlock
.WriteEnd();
101 void UpdateOrientation(DeviceOrientationHardwareBuffer
* buffer
) {
102 buffer
->seqlock
.WriteBegin();
103 buffer
->data
.alpha
= 1;
104 buffer
->data
.hasAlpha
= true;
105 buffer
->data
.beta
= 2;
106 buffer
->data
.hasBeta
= true;
107 buffer
->data
.gamma
= 3;
108 buffer
->data
.hasGamma
= true;
109 buffer
->data
.allAvailableSensorsAreActive
= true;
110 buffer
->seqlock
.WriteEnd();
113 base::WaitableEvent started_orientation_
;
114 base::WaitableEvent stopped_orientation_
;
115 base::WaitableEvent started_motion_
;
116 base::WaitableEvent stopped_motion_
;
120 DISALLOW_COPY_AND_ASSIGN(FakeDataFetcher
);
124 class DeviceInertialSensorBrowserTest
: public ContentBrowserTest
{
126 DeviceInertialSensorBrowserTest()
128 io_loop_finished_event_(false, false) {
131 // From ContentBrowserTest.
132 virtual void SetUpCommandLine(CommandLine
* command_line
) OVERRIDE
{
133 EXPECT_TRUE(!command_line
->HasSwitch(switches::kDisableDeviceOrientation
));
134 EXPECT_TRUE(!command_line
->HasSwitch(switches::kDisableDeviceMotion
));
137 virtual void SetUpOnMainThread() OVERRIDE
{
138 BrowserThread::PostTask(
139 BrowserThread::IO
, FROM_HERE
,
140 base::Bind(&DeviceInertialSensorBrowserTest::SetUpOnIOThread
, this));
141 io_loop_finished_event_
.Wait();
144 void SetUpOnIOThread() {
145 fetcher_
= new FakeDataFetcher();
146 DeviceInertialSensorService::GetInstance()->
147 SetDataFetcherForTests(fetcher_
);
148 io_loop_finished_event_
.Signal();
151 FakeDataFetcher
* fetcher_
;
154 base::WaitableEvent io_loop_finished_event_
;
158 IN_PROC_BROWSER_TEST_F(DeviceInertialSensorBrowserTest
, OrientationTest
) {
159 // The test page will register an event handler for orientation events,
160 // expects to get an event with fake values, then removes the event
161 // handler and navigates to #pass.
162 GURL test_url
= GetTestUrl(
163 "device_orientation", "device_orientation_test.html");
164 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url
, 2);
166 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
167 fetcher_
->started_orientation_
.Wait();
168 fetcher_
->stopped_orientation_
.Wait();
171 IN_PROC_BROWSER_TEST_F(DeviceInertialSensorBrowserTest
, MotionTest
) {
172 // The test page will register an event handler for motion events,
173 // expects to get an event with fake values, then removes the event
174 // handler and navigates to #pass.
175 GURL test_url
= GetTestUrl(
176 "device_orientation", "device_motion_test.html");
177 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url
, 2);
179 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
180 fetcher_
->started_motion_
.Wait();
181 fetcher_
->stopped_motion_
.Wait();
186 } // namespace content