OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device_orientation_event_pump.h" | 5 #include "device_orientation_event_pump.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "content/common/device_sensors/device_orientation_hardware_buffer.h" | 9 #include "content/common/device_sensors/device_orientation_hardware_buffer.h" |
10 #include "content/public/test/test_utils.h" | 10 #include "content/public/test/test_utils.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 | 39 |
40 private: | 40 private: |
41 bool did_change_device_orientation_; | 41 bool did_change_device_orientation_; |
42 blink::WebDeviceOrientationData data_; | 42 blink::WebDeviceOrientationData data_; |
43 | 43 |
44 DISALLOW_COPY_AND_ASSIGN(MockDeviceOrientationListener); | 44 DISALLOW_COPY_AND_ASSIGN(MockDeviceOrientationListener); |
45 }; | 45 }; |
46 | 46 |
47 class DeviceOrientationEventPumpForTesting : public DeviceOrientationEventPump { | 47 class DeviceOrientationEventPumpForTesting : public DeviceOrientationEventPump { |
48 public: | 48 public: |
49 DeviceOrientationEventPumpForTesting() { } | 49 DeviceOrientationEventPumpForTesting() |
| 50 : DeviceOrientationEventPump(0) { } |
50 virtual ~DeviceOrientationEventPumpForTesting() { } | 51 virtual ~DeviceOrientationEventPumpForTesting() { } |
51 | 52 |
52 void OnDidStart(base::SharedMemoryHandle renderer_handle) { | 53 void OnDidStart(base::SharedMemoryHandle renderer_handle) { |
53 DeviceOrientationEventPump::OnDidStart(renderer_handle); | 54 DeviceOrientationEventPump::OnDidStart(renderer_handle); |
54 } | 55 } |
55 virtual bool SendStartMessage() OVERRIDE { return true; } | 56 virtual void SendStartMessage() OVERRIDE { } |
56 virtual bool SendStopMessage() OVERRIDE { return true; } | 57 virtual void SendStopMessage() OVERRIDE { } |
57 virtual void FireEvent() OVERRIDE { | 58 virtual void FireEvent() OVERRIDE { |
58 DeviceOrientationEventPump::FireEvent(); | 59 DeviceOrientationEventPump::FireEvent(); |
59 Stop(); | 60 Stop(); |
60 base::MessageLoop::current()->QuitWhenIdle(); | 61 base::MessageLoop::current()->QuitWhenIdle(); |
61 } | 62 } |
62 | 63 |
63 private: | 64 private: |
64 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpForTesting); | 65 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpForTesting); |
65 }; | 66 }; |
66 | 67 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 base::SharedMemory shared_memory_; | 115 base::SharedMemory shared_memory_; |
115 DeviceOrientationHardwareBuffer* buffer_; | 116 DeviceOrientationHardwareBuffer* buffer_; |
116 | 117 |
117 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest); | 118 DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest); |
118 }; | 119 }; |
119 | 120 |
120 TEST_F(DeviceOrientationEventPumpTest, DidStartPolling) { | 121 TEST_F(DeviceOrientationEventPumpTest, DidStartPolling) { |
121 base::MessageLoop loop; | 122 base::MessageLoop loop; |
122 | 123 |
123 InitBuffer(); | 124 InitBuffer(); |
124 orientation_pump()->SetListener(listener()); | 125 orientation_pump()->Start(listener()); |
125 orientation_pump()->OnDidStart(handle()); | 126 orientation_pump()->OnDidStart(handle()); |
126 | 127 |
127 base::MessageLoop::current()->Run(); | 128 base::MessageLoop::current()->Run(); |
128 | 129 |
129 const blink::WebDeviceOrientationData& received_data = listener()->data(); | 130 const blink::WebDeviceOrientationData& received_data = listener()->data(); |
130 EXPECT_TRUE(listener()->did_change_device_orientation()); | 131 EXPECT_TRUE(listener()->did_change_device_orientation()); |
131 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 132 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
132 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 133 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
133 EXPECT_TRUE(received_data.hasAlpha); | 134 EXPECT_TRUE(received_data.hasAlpha); |
134 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 135 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
135 EXPECT_TRUE(received_data.hasBeta); | 136 EXPECT_TRUE(received_data.hasBeta); |
136 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); | 137 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
137 EXPECT_TRUE(received_data.hasGamma); | 138 EXPECT_TRUE(received_data.hasGamma); |
138 } | 139 } |
139 | 140 |
140 TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) { | 141 TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) { |
141 base::MessageLoop loop; | 142 base::MessageLoop loop; |
142 | 143 |
143 InitBufferNoData(); | 144 InitBufferNoData(); |
144 orientation_pump()->SetListener(listener()); | 145 orientation_pump()->Start(listener()); |
145 orientation_pump()->OnDidStart(handle()); | 146 orientation_pump()->OnDidStart(handle()); |
146 | 147 |
147 base::MessageLoop::current()->Run(); | 148 base::MessageLoop::current()->Run(); |
148 | 149 |
149 const blink::WebDeviceOrientationData& received_data = listener()->data(); | 150 const blink::WebDeviceOrientationData& received_data = listener()->data(); |
150 EXPECT_TRUE(listener()->did_change_device_orientation()); | 151 EXPECT_TRUE(listener()->did_change_device_orientation()); |
151 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 152 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
152 EXPECT_FALSE(received_data.hasAlpha); | 153 EXPECT_FALSE(received_data.hasAlpha); |
153 EXPECT_FALSE(received_data.hasBeta); | 154 EXPECT_FALSE(received_data.hasBeta); |
154 EXPECT_FALSE(received_data.hasGamma); | 155 EXPECT_FALSE(received_data.hasGamma); |
155 } | 156 } |
156 | 157 |
157 TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { | 158 TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { |
158 base::MessageLoop loop; | 159 base::MessageLoop loop; |
159 | 160 |
160 InitBuffer(); | 161 InitBuffer(); |
161 orientation_pump()->SetListener(listener()); | 162 orientation_pump()->Start(listener()); |
162 orientation_pump()->OnDidStart(handle()); | 163 orientation_pump()->OnDidStart(handle()); |
163 | 164 |
164 base::MessageLoop::current()->Run(); | 165 base::MessageLoop::current()->Run(); |
165 | 166 |
166 const blink::WebDeviceOrientationData& received_data = listener()->data(); | 167 const blink::WebDeviceOrientationData& received_data = listener()->data(); |
167 EXPECT_TRUE(listener()->did_change_device_orientation()); | 168 EXPECT_TRUE(listener()->did_change_device_orientation()); |
168 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 169 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
169 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 170 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
170 EXPECT_TRUE(received_data.hasAlpha); | 171 EXPECT_TRUE(received_data.hasAlpha); |
171 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 172 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
172 EXPECT_TRUE(received_data.hasBeta); | 173 EXPECT_TRUE(received_data.hasBeta); |
173 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); | 174 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
174 EXPECT_TRUE(received_data.hasGamma); | 175 EXPECT_TRUE(received_data.hasGamma); |
175 | 176 |
176 buffer()->data.alpha = | 177 buffer()->data.alpha = |
177 1 + DeviceOrientationEventPump::kOrientationThreshold / 2.0; | 178 1 + DeviceOrientationEventPump::kOrientationThreshold / 2.0; |
178 listener()->set_did_change_device_orientation(false); | 179 listener()->set_did_change_device_orientation(false); |
179 | 180 |
| 181 // Reset the pump's listener. |
| 182 orientation_pump()->Start(listener()); |
| 183 |
180 base::MessageLoop::current()->PostTask(FROM_HERE, | 184 base::MessageLoop::current()->PostTask(FROM_HERE, |
181 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, | 185 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, |
182 base::Unretained(orientation_pump()))); | 186 base::Unretained(orientation_pump()))); |
183 base::MessageLoop::current()->Run(); | 187 base::MessageLoop::current()->Run(); |
184 | 188 |
185 EXPECT_FALSE(listener()->did_change_device_orientation()); | 189 EXPECT_FALSE(listener()->did_change_device_orientation()); |
186 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); | 190 EXPECT_TRUE(received_data.allAvailableSensorsAreActive); |
187 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); | 191 EXPECT_EQ(1, static_cast<double>(received_data.alpha)); |
188 EXPECT_TRUE(received_data.hasAlpha); | 192 EXPECT_TRUE(received_data.hasAlpha); |
189 EXPECT_EQ(2, static_cast<double>(received_data.beta)); | 193 EXPECT_EQ(2, static_cast<double>(received_data.beta)); |
190 EXPECT_TRUE(received_data.hasBeta); | 194 EXPECT_TRUE(received_data.hasBeta); |
191 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); | 195 EXPECT_EQ(3, static_cast<double>(received_data.gamma)); |
192 EXPECT_TRUE(received_data.hasGamma); | 196 EXPECT_TRUE(received_data.hasGamma); |
193 | 197 |
194 buffer()->data.alpha = | 198 buffer()->data.alpha = |
195 1 + DeviceOrientationEventPump::kOrientationThreshold; | 199 1 + DeviceOrientationEventPump::kOrientationThreshold; |
196 listener()->set_did_change_device_orientation(false); | 200 listener()->set_did_change_device_orientation(false); |
197 | 201 |
| 202 // Reset the pump's listener. |
| 203 orientation_pump()->Start(listener()); |
| 204 |
198 base::MessageLoop::current()->PostTask(FROM_HERE, | 205 base::MessageLoop::current()->PostTask(FROM_HERE, |
199 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, | 206 base::Bind(&DeviceOrientationEventPumpForTesting::FireEvent, |
200 base::Unretained(orientation_pump()))); | 207 base::Unretained(orientation_pump()))); |
201 base::MessageLoop::current()->Run(); | 208 base::MessageLoop::current()->Run(); |
202 | 209 |
203 EXPECT_TRUE(listener()->did_change_device_orientation()); | 210 EXPECT_TRUE(listener()->did_change_device_orientation()); |
204 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, | 211 EXPECT_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, |
205 static_cast<double>(received_data.alpha)); | 212 static_cast<double>(received_data.alpha)); |
206 } | 213 } |
207 | 214 |
208 } // namespace content | 215 } // namespace content |
OLD | NEW |