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 virtual void SetUpOnMainThread() OVERRIDE {
132 BrowserThread::PostTask(
133 BrowserThread::IO, FROM_HERE,
134 base::Bind(&DeviceInertialSensorBrowserTest::SetUpOnIOThread, this));
135 io_loop_finished_event_.Wait();
138 void SetUpOnIOThread() {
139 fetcher_ = new FakeDataFetcher();
140 DeviceInertialSensorService::GetInstance()->
141 SetDataFetcherForTests(fetcher_);
142 io_loop_finished_event_.Signal();
145 FakeDataFetcher* fetcher_;
148 base::WaitableEvent io_loop_finished_event_;
152 IN_PROC_BROWSER_TEST_F(DeviceInertialSensorBrowserTest, OrientationTest) {
153 // The test page will register an event handler for orientation events,
154 // expects to get an event with fake values, then removes the event
155 // handler and navigates to #pass.
156 GURL test_url = GetTestUrl(
157 "device_orientation", "device_orientation_test.html");
158 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
160 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
161 fetcher_->started_orientation_.Wait();
162 fetcher_->stopped_orientation_.Wait();
165 IN_PROC_BROWSER_TEST_F(DeviceInertialSensorBrowserTest, MotionTest) {
166 // The test page will register an event handler for motion events,
167 // expects to get an event with fake values, then removes the event
168 // handler and navigates to #pass.
169 GURL test_url = GetTestUrl(
170 "device_orientation", "device_motion_test.html");
171 NavigateToURLBlockUntilNavigationsComplete(shell(), test_url, 2);
173 EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
174 fetcher_->started_motion_.Wait();
175 fetcher_->stopped_motion_.Wait();
180 } // namespace content