Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / browser / device_orientation / device_inertial_sensor_browsertest.cc
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 "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"
17
18 namespace content {
19
20 namespace {
21
22 class FakeDataFetcher : public DataFetcherSharedMemory {
23  public:
24   FakeDataFetcher()
25       : started_orientation_(false, false),
26         stopped_orientation_(false, false),
27         started_motion_(false, false),
28         stopped_motion_(false, false) {
29   }
30   virtual ~FakeDataFetcher() { }
31
32   virtual bool Start(ConsumerType consumer_type, void* buffer) OVERRIDE {
33     EXPECT_TRUE(buffer);
34
35     switch (consumer_type) {
36       case CONSUMER_TYPE_MOTION:
37         UpdateMotion(static_cast<DeviceMotionHardwareBuffer*>(buffer));
38         started_motion_.Signal();
39         break;
40       case CONSUMER_TYPE_ORIENTATION:
41         UpdateOrientation(
42             static_cast<DeviceOrientationHardwareBuffer*>(buffer));
43         started_orientation_.Signal();
44         break;
45       default:
46         return false;
47     }
48     return true;
49   }
50
51   virtual bool Stop(ConsumerType consumer_type) OVERRIDE {
52     switch (consumer_type) {
53       case CONSUMER_TYPE_MOTION:
54         stopped_motion_.Signal();
55         break;
56       case CONSUMER_TYPE_ORIENTATION:
57         stopped_orientation_.Signal();
58         break;
59       default:
60         return false;
61     }
62     return true;
63   }
64
65   virtual void Fetch(unsigned consumer_bitmask) OVERRIDE {
66     FAIL() << "fetch should not be called";
67   }
68
69   virtual FetcherType GetType() const OVERRIDE {
70     return FETCHER_TYPE_DEFAULT;
71   }
72
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;
81
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;
88
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;
95
96     buffer->data.interval = 100;
97     buffer->data.allAvailableSensorsAreActive = true;
98     buffer->seqlock.WriteEnd();
99   }
100
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();
111   }
112
113   base::WaitableEvent started_orientation_;
114   base::WaitableEvent stopped_orientation_;
115   base::WaitableEvent started_motion_;
116   base::WaitableEvent stopped_motion_;
117
118  private:
119
120   DISALLOW_COPY_AND_ASSIGN(FakeDataFetcher);
121 };
122
123
124 class DeviceInertialSensorBrowserTest : public ContentBrowserTest  {
125  public:
126   DeviceInertialSensorBrowserTest()
127       : fetcher_(NULL),
128         io_loop_finished_event_(false, false) {
129   }
130
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();
136   }
137
138   void SetUpOnIOThread() {
139     fetcher_ = new FakeDataFetcher();
140     DeviceInertialSensorService::GetInstance()->
141         SetDataFetcherForTests(fetcher_);
142     io_loop_finished_event_.Signal();
143   }
144
145   FakeDataFetcher* fetcher_;
146
147  private:
148   base::WaitableEvent io_loop_finished_event_;
149 };
150
151
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);
159
160   EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
161   fetcher_->started_orientation_.Wait();
162   fetcher_->stopped_orientation_.Wait();
163 }
164
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);
172
173   EXPECT_EQ("pass", shell()->web_contents()->GetLastCommittedURL().ref());
174   fetcher_->started_motion_.Wait();
175   fetcher_->stopped_motion_.Wait();
176 }
177
178 } //  namespace
179
180 } //  namespace content