OLD | NEW |
| (Empty) |
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 "device_orientation_event_pump.h" | |
6 | |
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" | |
13 | |
14 namespace content { | |
15 | |
16 class MockDeviceOrientationListener | |
17 : public blink::WebDeviceOrientationListener { | |
18 public: | |
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_; | |
26 }; | |
27 | |
28 MockDeviceOrientationListener::MockDeviceOrientationListener() | |
29 : did_change_device_orientation_(false) { | |
30 memset(&data_, 0, sizeof(data_)); | |
31 } | |
32 | |
33 void MockDeviceOrientationListener::didChangeDeviceOrientation( | |
34 const blink::WebDeviceOrientationData& data) { | |
35 memcpy(&data_, &data, sizeof(data)); | |
36 did_change_device_orientation_ = true; | |
37 } | |
38 | |
39 void MockDeviceOrientationListener::ResetDidChangeOrientation() { | |
40 did_change_device_orientation_ = false; | |
41 } | |
42 | |
43 class DeviceOrientationEventPumpForTesting : public DeviceOrientationEventPump { | |
44 public: | |
45 DeviceOrientationEventPumpForTesting() { } | |
46 virtual ~DeviceOrientationEventPumpForTesting() { } | |
47 | |
48 void OnDidStart(base::SharedMemoryHandle renderer_handle) { | |
49 DeviceOrientationEventPump::OnDidStart(renderer_handle); | |
50 } | |
51 virtual bool SendStartMessage() OVERRIDE { return true; } | |
52 virtual bool SendStopMessage() OVERRIDE { return true; } | |
53 virtual void FireEvent() OVERRIDE { | |
54 DeviceOrientationEventPump::FireEvent(); | |
55 Stop(); | |
56 base::MessageLoop::current()->QuitWhenIdle(); | |
57 } | |
58 }; | |
59 | |
60 class DeviceOrientationEventPumpTest : public testing::Test { | |
61 public: | |
62 DeviceOrientationEventPumpTest() { | |
63 EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( | |
64 sizeof(DeviceOrientationHardwareBuffer))); | |
65 } | |
66 | |
67 protected: | |
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_); | |
75 } | |
76 | |
77 void InitBuffer() { | |
78 blink::WebDeviceOrientationData& data = buffer_->data; | |
79 data.alpha = 1; | |
80 data.hasAlpha = true; | |
81 data.beta = 2; | |
82 data.hasBeta = true; | |
83 data.gamma = 3; | |
84 data.hasGamma = true; | |
85 data.allAvailableSensorsAreActive = true; | |
86 } | |
87 | |
88 void InitBufferNoData() { | |
89 blink::WebDeviceOrientationData& data = buffer_->data; | |
90 data.allAvailableSensorsAreActive = true; | |
91 } | |
92 | |
93 scoped_ptr<MockDeviceOrientationListener> listener_; | |
94 scoped_ptr<DeviceOrientationEventPumpForTesting> orientation_pump_; | |
95 base::SharedMemoryHandle handle_; | |
96 base::SharedMemory shared_memory_; | |
97 DeviceOrientationHardwareBuffer* buffer_; | |
98 }; | |
99 | |
100 // Always failing in the win try bot. See http://crbug.com/256782. | |
101 #if defined(OS_WIN) | |
102 #define MAYBE_DidStartPolling DISABLED_DidStartPolling | |
103 #else | |
104 #define MAYBE_DidStartPolling DidStartPolling | |
105 #endif | |
106 TEST_F(DeviceOrientationEventPumpTest, MAYBE_DidStartPolling) { | |
107 base::MessageLoop loop; | |
108 | |
109 InitBuffer(); | |
110 orientation_pump_->SetListener(listener_.get()); | |
111 orientation_pump_->OnDidStart(handle_); | |
112 | |
113 base::MessageLoop::current()->Run(); | |
114 | |
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, (double)received_data.alpha); | |
119 EXPECT_TRUE(received_data.hasAlpha); | |
120 EXPECT_EQ(2, (double)received_data.beta); | |
121 EXPECT_TRUE(received_data.hasBeta); | |
122 EXPECT_EQ(3, (double)received_data.gamma); | |
123 EXPECT_TRUE(received_data.hasGamma); | |
124 } | |
125 | |
126 // Always failing in the win try bot. See http://crbug.com/256782. | |
127 #if defined(OS_WIN) | |
128 #define MAYBE_FireAllNullEvent DISABLED_FireAllNullEvent | |
129 #else | |
130 #define MAYBE_FireAllNullEvent FireAllNullEvent | |
131 #endif | |
132 TEST_F(DeviceOrientationEventPumpTest, MAYBE_FireAllNullEvent) { | |
133 base::MessageLoop loop; | |
134 | |
135 InitBufferNoData(); | |
136 orientation_pump_->SetListener(listener_.get()); | |
137 orientation_pump_->OnDidStart(handle_); | |
138 | |
139 base::MessageLoop::current()->Run(); | |
140 | |
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); | |
147 } | |
148 | |
149 // Always failing in the win try bot. See http://crbug.com/256782. | |
150 #if defined(OS_WIN) | |
151 #define MAYBE_UpdateRespectsOrientationThreshold \ | |
152 DISABLED_UpdateRespectsOrientationThreshold | |
153 #else | |
154 #define MAYBE_UpdateRespectsOrientationThreshold \ | |
155 UpdateRespectsOrientationThreshold | |
156 #endif | |
157 TEST_F(DeviceOrientationEventPumpTest, | |
158 MAYBE_UpdateRespectsOrientationThreshold) { | |
159 base::MessageLoop loop; | |
160 | |
161 InitBuffer(); | |
162 orientation_pump_->SetListener(listener_.get()); | |
163 orientation_pump_->OnDidStart(handle_); | |
164 | |
165 base::MessageLoop::current()->Run(); | |
166 | |
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, (double)received_data.alpha); | |
171 EXPECT_TRUE(received_data.hasAlpha); | |
172 EXPECT_EQ(2, (double)received_data.beta); | |
173 EXPECT_TRUE(received_data.hasBeta); | |
174 EXPECT_EQ(3, (double)received_data.gamma); | |
175 EXPECT_TRUE(received_data.hasGamma); | |
176 | |
177 buffer_->data.alpha = | |
178 1 + DeviceOrientationEventPump::kOrientationThreshold / 2.0; | |
179 listener_->ResetDidChangeOrientation(); | |
180 | |
181 base::MessageLoop::current()->PostTask(FROM_HERE, | |
182 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, | |
183 base::Unretained(orientation_pump_.get()))); | |
184 base::MessageLoop::current()->Run(); | |
185 | |
186 EXPECT_FALSE(listener_->did_change_device_orientation_); | |
187 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | |
188 EXPECT_EQ(1, (double)received_data.alpha); | |
189 EXPECT_TRUE(received_data.hasAlpha); | |
190 EXPECT_EQ(2, (double)received_data.beta); | |
191 EXPECT_TRUE(received_data.hasBeta); | |
192 EXPECT_EQ(3, (double)received_data.gamma); | |
193 EXPECT_TRUE(received_data.hasGamma); | |
194 | |
195 buffer_->data.alpha = | |
196 1 + DeviceOrientationEventPump::kOrientationThreshold; | |
197 listener_->ResetDidChangeOrientation(); | |
198 | |
199 base::MessageLoop::current()->PostTask(FROM_HERE, | |
200 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, | |
201 base::Unretained(orientation_pump_.get()))); | |
202 base::MessageLoop::current()->Run(); | |
203 | |
204 EXPECT_TRUE(listener_->did_change_device_orientation_); | |
205 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, | |
206 (double)received_data.alpha); | |
207 } | |
208 | |
209 } // namespace content | |
OLD | NEW |