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();
20 virtual ~MockDeviceOrientationListener() { }
21 virtual void didChangeDeviceOrientation(
22 const blink::WebDeviceOrientationData&) OVERRIDE;
23 void ResetDidChangeOrientation();
24 bool did_change_device_orientation_;
25 blink::WebDeviceOrientationData data_;
28 MockDeviceOrientationListener::MockDeviceOrientationListener()
29 : did_change_device_orientation_(false) {
30 memset(&data_, 0, sizeof(data_));
33 void MockDeviceOrientationListener::didChangeDeviceOrientation(
34 const blink::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 blink::WebDeviceOrientationData& data = buffer_->data;
85 data.allAvailableSensorsAreActive = true;
88 void InitBufferNoData() {
89 blink::WebDeviceOrientationData& data = buffer_->data;
90 data.allAvailableSensorsAreActive = true;
93 scoped_ptr<MockDeviceOrientationListener> listener_;
94 scoped_ptr<DeviceOrientationEventPumpForTesting> orientation_pump_;
95 base::SharedMemoryHandle handle_;
96 base::SharedMemory shared_memory_;
97 DeviceOrientationHardwareBuffer* buffer_;
100 // Always failing in the win try bot. See http://crbug.com/256782.
102 #define MAYBE_DidStartPolling DISABLED_DidStartPolling
104 #define MAYBE_DidStartPolling DidStartPolling
106 TEST_F(DeviceOrientationEventPumpTest, MAYBE_DidStartPolling) {
107 base::MessageLoop loop;
110 orientation_pump_->SetListener(listener_.get());
111 orientation_pump_->OnDidStart(handle_);
113 base::MessageLoop::current()->Run();
115 blink::WebDeviceOrientationData& received_data = listener_->data_;
116 EXPECT_TRUE(listener_->did_change_device_orientation_);
117 EXPECT_TRUE(received_data.allAvailableSensorsAreActive);
118 EXPECT_EQ(1, static_cast<double>(received_data.alpha));
119 EXPECT_TRUE(received_data.hasAlpha);
120 EXPECT_EQ(2, static_cast<double>(received_data.beta));
121 EXPECT_TRUE(received_data.hasBeta);
122 EXPECT_EQ(3, static_cast<double>(received_data.gamma));
123 EXPECT_TRUE(received_data.hasGamma);
126 // Always failing in the win try bot. See http://crbug.com/256782.
128 #define MAYBE_FireAllNullEvent DISABLED_FireAllNullEvent
130 #define MAYBE_FireAllNullEvent FireAllNullEvent
132 TEST_F(DeviceOrientationEventPumpTest, MAYBE_FireAllNullEvent) {
133 base::MessageLoop loop;
136 orientation_pump_->SetListener(listener_.get());
137 orientation_pump_->OnDidStart(handle_);
139 base::MessageLoop::current()->Run();
141 blink::WebDeviceOrientationData& received_data = listener_->data_;
142 EXPECT_TRUE(listener_->did_change_device_orientation_);
143 EXPECT_TRUE(received_data.allAvailableSensorsAreActive);
144 EXPECT_FALSE(received_data.hasAlpha);
145 EXPECT_FALSE(received_data.hasBeta);
146 EXPECT_FALSE(received_data.hasGamma);
149 // Always failing in the win try bot. See http://crbug.com/256782.
151 #define MAYBE_UpdateRespectsOrientationThreshold \
152 DISABLED_UpdateRespectsOrientationThreshold
154 #define MAYBE_UpdateRespectsOrientationThreshold \
155 UpdateRespectsOrientationThreshold
157 TEST_F(DeviceOrientationEventPumpTest,
158 MAYBE_UpdateRespectsOrientationThreshold) {
159 base::MessageLoop loop;
162 orientation_pump_->SetListener(listener_.get());
163 orientation_pump_->OnDidStart(handle_);
165 base::MessageLoop::current()->Run();
167 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_->ResetDidChangeOrientation();
181 base::MessageLoop::current()->PostTask(FROM_HERE,
182 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent,
183 base::Unretained(orientation_pump_.get())));
184 base::MessageLoop::current()->Run();
186 EXPECT_FALSE(listener_->did_change_device_orientation_);
187 EXPECT_TRUE(received_data.allAvailableSensorsAreActive);
188 EXPECT_EQ(1, static_cast<double>(received_data.alpha));
189 EXPECT_TRUE(received_data.hasAlpha);
190 EXPECT_EQ(2, static_cast<double>(received_data.beta));
191 EXPECT_TRUE(received_data.hasBeta);
192 EXPECT_EQ(3, static_cast<double>(received_data.gamma));
193 EXPECT_TRUE(received_data.hasGamma);
195 buffer_->data.alpha =
196 1 + DeviceOrientationEventPump::kOrientationThreshold;
197 listener_->ResetDidChangeOrientation();
199 base::MessageLoop::current()->PostTask(FROM_HERE,
200 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent,
201 base::Unretained(orientation_pump_.get())));
202 base::MessageLoop::current()->Run();
204 EXPECT_TRUE(listener_->did_change_device_orientation_);
205 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold,
206 static_cast<double>(received_data.alpha));
209 } // namespace content