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 "device_orientation_event_pump.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
9 #include "content/common/device_orientation/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 WebKit::WebDeviceOrientationListener
{
19 MockDeviceOrientationListener();
20 virtual ~MockDeviceOrientationListener() { }
21 virtual void didChangeDeviceOrientation(
22 const WebKit::WebDeviceOrientationData
&) OVERRIDE
;
23 void ResetDidChangeOrientation();
24 bool did_change_device_orientation_
;
25 WebKit::WebDeviceOrientationData data_
;
28 MockDeviceOrientationListener::MockDeviceOrientationListener()
29 : did_change_device_orientation_(false) {
30 memset(&data_
, 0, sizeof(data_
));
33 void MockDeviceOrientationListener::didChangeDeviceOrientation(
34 const WebKit::WebDeviceOrientationData
& data
) {
35 memcpy(&data_
, &data
, sizeof(data
));
36 did_change_device_orientation_
= true;
39 void MockDeviceOrientationListener::ResetDidChangeOrientation() {
40 did_change_device_orientation_
= false;
43 class DeviceOrientationEventPumpForTesting
: public DeviceOrientationEventPump
{
45 DeviceOrientationEventPumpForTesting() { }
46 virtual ~DeviceOrientationEventPumpForTesting() { }
48 void OnDidStart(base::SharedMemoryHandle renderer_handle
) {
49 DeviceOrientationEventPump::OnDidStart(renderer_handle
);
51 virtual bool SendStartMessage() OVERRIDE
{ return true; }
52 virtual bool SendStopMessage() OVERRIDE
{ return true; }
53 virtual void FireEvent() OVERRIDE
{
54 DeviceOrientationEventPump::FireEvent();
56 base::MessageLoop::current()->QuitWhenIdle();
60 class DeviceOrientationEventPumpTest
: public testing::Test
{
62 DeviceOrientationEventPumpTest() {
63 EXPECT_TRUE(shared_memory_
.CreateAndMapAnonymous(
64 sizeof(DeviceOrientationHardwareBuffer
)));
68 virtual void SetUp() OVERRIDE
{
69 listener_
.reset(new MockDeviceOrientationListener
);
70 orientation_pump_
.reset(new DeviceOrientationEventPumpForTesting
);
71 buffer_
= static_cast<DeviceOrientationHardwareBuffer
*>(
72 shared_memory_
.memory());
73 memset(buffer_
, 0, sizeof(DeviceOrientationHardwareBuffer
));
74 shared_memory_
.ShareToProcess(base::kNullProcessHandle
, &handle_
);
78 WebKit::WebDeviceOrientationData
& data
= buffer_
->data
;
85 data
.allAvailableSensorsAreActive
= true;
88 scoped_ptr
<MockDeviceOrientationListener
> listener_
;
89 scoped_ptr
<DeviceOrientationEventPumpForTesting
> orientation_pump_
;
90 base::SharedMemoryHandle handle_
;
91 base::SharedMemory shared_memory_
;
92 DeviceOrientationHardwareBuffer
* buffer_
;
95 // Always failing in the win try bot. See http://crbug.com/256782.
97 #define MAYBE_DidStartPolling DISABLED_DidStartPolling
99 #define MAYBE_DidStartPolling DidStartPolling
101 TEST_F(DeviceOrientationEventPumpTest
, MAYBE_DidStartPolling
) {
102 base::MessageLoop loop
;
105 orientation_pump_
->SetListener(listener_
.get());
106 orientation_pump_
->OnDidStart(handle_
);
108 base::MessageLoop::current()->Run();
110 WebKit::WebDeviceOrientationData
& received_data
= listener_
->data_
;
111 EXPECT_TRUE(listener_
->did_change_device_orientation_
);
112 EXPECT_TRUE(received_data
.allAvailableSensorsAreActive
);
113 EXPECT_EQ(1, (double)received_data
.alpha
);
114 EXPECT_TRUE(received_data
.hasAlpha
);
115 EXPECT_EQ(2, (double)received_data
.beta
);
116 EXPECT_TRUE(received_data
.hasBeta
);
117 EXPECT_EQ(3, (double)received_data
.gamma
);
118 EXPECT_TRUE(received_data
.hasGamma
);
121 // Always failing in the win try bot. See http://crbug.com/256782.
123 #define MAYBE_UpdateRespectsOrientationThreshold \
124 DISABLED_UpdateRespectsOrientationThreshold
126 #define MAYBE_UpdateRespectsOrientationThreshold \
127 UpdateRespectsOrientationThreshold
129 TEST_F(DeviceOrientationEventPumpTest
,
130 MAYBE_UpdateRespectsOrientationThreshold
) {
131 base::MessageLoop loop
;
134 orientation_pump_
->SetListener(listener_
.get());
135 orientation_pump_
->OnDidStart(handle_
);
137 base::MessageLoop::current()->Run();
139 WebKit::WebDeviceOrientationData
& received_data
= listener_
->data_
;
140 EXPECT_TRUE(listener_
->did_change_device_orientation_
);
141 EXPECT_TRUE(received_data
.allAvailableSensorsAreActive
);
142 EXPECT_EQ(1, (double)received_data
.alpha
);
143 EXPECT_TRUE(received_data
.hasAlpha
);
144 EXPECT_EQ(2, (double)received_data
.beta
);
145 EXPECT_TRUE(received_data
.hasBeta
);
146 EXPECT_EQ(3, (double)received_data
.gamma
);
147 EXPECT_TRUE(received_data
.hasGamma
);
149 buffer_
->data
.alpha
=
150 1 + DeviceOrientationEventPump::kOrientationThreshold
/ 2.0;
151 listener_
->ResetDidChangeOrientation();
153 base::MessageLoop::current()->PostTask(FROM_HERE
,
154 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent
,
155 base::Unretained(orientation_pump_
.get())));
156 base::MessageLoop::current()->Run();
158 EXPECT_FALSE(listener_
->did_change_device_orientation_
);
159 EXPECT_TRUE(received_data
.allAvailableSensorsAreActive
);
160 EXPECT_EQ(1, (double)received_data
.alpha
);
161 EXPECT_TRUE(received_data
.hasAlpha
);
162 EXPECT_EQ(2, (double)received_data
.beta
);
163 EXPECT_TRUE(received_data
.hasBeta
);
164 EXPECT_EQ(3, (double)received_data
.gamma
);
165 EXPECT_TRUE(received_data
.hasGamma
);
167 buffer_
->data
.alpha
=
168 1 + DeviceOrientationEventPump::kOrientationThreshold
;
169 listener_
->ResetDidChangeOrientation();
171 base::MessageLoop::current()->PostTask(FROM_HERE
,
172 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent
,
173 base::Unretained(orientation_pump_
.get())));
174 base::MessageLoop::current()->Run();
176 EXPECT_TRUE(listener_
->did_change_device_orientation_
);
177 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold
,
178 (double)received_data
.alpha
);
181 } // namespace content