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 "device_orientation_event_pump.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
9 #include "content/common/device_sensors/device_orientation_hardware_buffer.h"
10 #include "content/public/test/test_utils.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/WebKit/public/platform/WebDeviceOrientationListener.h"
16 class MockDeviceOrientationListener
17 : public blink::WebDeviceOrientationListener
{
19 MockDeviceOrientationListener() : did_change_device_orientation_(false) {
20 memset(&data_
, 0, sizeof(data_
));
22 virtual ~MockDeviceOrientationListener() { }
24 virtual void didChangeDeviceOrientation(
25 const blink::WebDeviceOrientationData
& data
) OVERRIDE
{
26 memcpy(&data_
, &data
, sizeof(data
));
27 did_change_device_orientation_
= true;
30 bool did_change_device_orientation() const {
31 return did_change_device_orientation_
;
33 void set_did_change_device_orientation(bool value
) {
34 did_change_device_orientation_
= value
;
36 const blink::WebDeviceOrientationData
& data() const {
41 bool did_change_device_orientation_
;
42 blink::WebDeviceOrientationData data_
;
44 DISALLOW_COPY_AND_ASSIGN(MockDeviceOrientationListener
);
47 class DeviceOrientationEventPumpForTesting
: public DeviceOrientationEventPump
{
49 DeviceOrientationEventPumpForTesting()
50 : DeviceOrientationEventPump(0) { }
51 virtual ~DeviceOrientationEventPumpForTesting() { }
53 void OnDidStart(base::SharedMemoryHandle renderer_handle
) {
54 DeviceOrientationEventPump::OnDidStart(renderer_handle
);
56 virtual void SendStartMessage() OVERRIDE
{ }
57 virtual void SendStopMessage() OVERRIDE
{ }
58 virtual void FireEvent() OVERRIDE
{
59 DeviceOrientationEventPump::FireEvent();
61 base::MessageLoop::current()->QuitWhenIdle();
65 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpForTesting
);
68 class DeviceOrientationEventPumpTest
: public testing::Test
{
70 DeviceOrientationEventPumpTest() {
71 EXPECT_TRUE(shared_memory_
.CreateAndMapAnonymous(
72 sizeof(DeviceOrientationHardwareBuffer
)));
76 virtual void SetUp() OVERRIDE
{
77 const DeviceOrientationHardwareBuffer
* null_buffer
= NULL
;
78 listener_
.reset(new MockDeviceOrientationListener
);
79 orientation_pump_
.reset(new DeviceOrientationEventPumpForTesting
);
80 buffer_
= static_cast<DeviceOrientationHardwareBuffer
*>(
81 shared_memory_
.memory());
82 ASSERT_NE(null_buffer
, buffer_
);
83 memset(buffer_
, 0, sizeof(DeviceOrientationHardwareBuffer
));
84 ASSERT_TRUE(shared_memory_
.ShareToProcess(base::GetCurrentProcessHandle(),
89 blink::WebDeviceOrientationData
& data
= buffer_
->data
;
96 data
.allAvailableSensorsAreActive
= true;
99 void InitBufferNoData() {
100 blink::WebDeviceOrientationData
& data
= buffer_
->data
;
101 data
.allAvailableSensorsAreActive
= true;
104 MockDeviceOrientationListener
* listener() { return listener_
.get(); }
105 DeviceOrientationEventPumpForTesting
* orientation_pump() {
106 return orientation_pump_
.get();
108 base::SharedMemoryHandle
handle() { return handle_
; }
109 DeviceOrientationHardwareBuffer
* buffer() { return buffer_
; }
112 scoped_ptr
<MockDeviceOrientationListener
> listener_
;
113 scoped_ptr
<DeviceOrientationEventPumpForTesting
> orientation_pump_
;
114 base::SharedMemoryHandle handle_
;
115 base::SharedMemory shared_memory_
;
116 DeviceOrientationHardwareBuffer
* buffer_
;
118 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest
);
121 TEST_F(DeviceOrientationEventPumpTest
, DidStartPolling
) {
122 base::MessageLoop loop
;
125 orientation_pump()->Start(listener());
126 orientation_pump()->OnDidStart(handle());
128 base::MessageLoop::current()->Run();
130 const blink::WebDeviceOrientationData
& received_data
= listener()->data();
131 EXPECT_TRUE(listener()->did_change_device_orientation());
132 EXPECT_TRUE(received_data
.allAvailableSensorsAreActive
);
133 EXPECT_EQ(1, static_cast<double>(received_data
.alpha
));
134 EXPECT_TRUE(received_data
.hasAlpha
);
135 EXPECT_EQ(2, static_cast<double>(received_data
.beta
));
136 EXPECT_TRUE(received_data
.hasBeta
);
137 EXPECT_EQ(3, static_cast<double>(received_data
.gamma
));
138 EXPECT_TRUE(received_data
.hasGamma
);
141 TEST_F(DeviceOrientationEventPumpTest
, FireAllNullEvent
) {
142 base::MessageLoop loop
;
145 orientation_pump()->Start(listener());
146 orientation_pump()->OnDidStart(handle());
148 base::MessageLoop::current()->Run();
150 const blink::WebDeviceOrientationData
& received_data
= listener()->data();
151 EXPECT_TRUE(listener()->did_change_device_orientation());
152 EXPECT_TRUE(received_data
.allAvailableSensorsAreActive
);
153 EXPECT_FALSE(received_data
.hasAlpha
);
154 EXPECT_FALSE(received_data
.hasBeta
);
155 EXPECT_FALSE(received_data
.hasGamma
);
158 TEST_F(DeviceOrientationEventPumpTest
, UpdateRespectsOrientationThreshold
) {
159 base::MessageLoop loop
;
162 orientation_pump()->Start(listener());
163 orientation_pump()->OnDidStart(handle());
165 base::MessageLoop::current()->Run();
167 const blink::WebDeviceOrientationData
& received_data
= listener()->data();
168 EXPECT_TRUE(listener()->did_change_device_orientation());
169 EXPECT_TRUE(received_data
.allAvailableSensorsAreActive
);
170 EXPECT_EQ(1, static_cast<double>(received_data
.alpha
));
171 EXPECT_TRUE(received_data
.hasAlpha
);
172 EXPECT_EQ(2, static_cast<double>(received_data
.beta
));
173 EXPECT_TRUE(received_data
.hasBeta
);
174 EXPECT_EQ(3, static_cast<double>(received_data
.gamma
));
175 EXPECT_TRUE(received_data
.hasGamma
);
177 buffer()->data
.alpha
=
178 1 + DeviceOrientationEventPump::kOrientationThreshold
/ 2.0;
179 listener()->set_did_change_device_orientation(false);
181 // Reset the pump's listener.
182 orientation_pump()->Start(listener());
184 base::MessageLoop::current()->PostTask(FROM_HERE
,
185 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent
,
186 base::Unretained(orientation_pump())));
187 base::MessageLoop::current()->Run();
189 EXPECT_FALSE(listener()->did_change_device_orientation());
190 EXPECT_TRUE(received_data
.allAvailableSensorsAreActive
);
191 EXPECT_EQ(1, static_cast<double>(received_data
.alpha
));
192 EXPECT_TRUE(received_data
.hasAlpha
);
193 EXPECT_EQ(2, static_cast<double>(received_data
.beta
));
194 EXPECT_TRUE(received_data
.hasBeta
);
195 EXPECT_EQ(3, static_cast<double>(received_data
.gamma
));
196 EXPECT_TRUE(received_data
.hasGamma
);
198 buffer()->data
.alpha
=
199 1 + DeviceOrientationEventPump::kOrientationThreshold
;
200 listener()->set_did_change_device_orientation(false);
202 // Reset the pump's listener.
203 orientation_pump()->Start(listener());
205 base::MessageLoop::current()->PostTask(FROM_HERE
,
206 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent
,
207 base::Unretained(orientation_pump())));
208 base::MessageLoop::current()->Run();
210 EXPECT_TRUE(listener()->did_change_device_orientation());
211 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold
,
212 static_cast<double>(received_data
.alpha
));
215 } // namespace content