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 "screen_orientation_dispatcher.h" | 5 #include "screen_orientation_dispatcher.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 } | 98 } |
99 | 99 |
100 void UnlockOrientation() { | 100 void UnlockOrientation() { |
101 dispatcher_->unlockOrientation(); | 101 dispatcher_->unlockOrientation(); |
102 } | 102 } |
103 | 103 |
104 void OnMessageReceived(const IPC::Message& message) { | 104 void OnMessageReceived(const IPC::Message& message) { |
105 dispatcher_->OnMessageReceived(message); | 105 dispatcher_->OnMessageReceived(message); |
106 } | 106 } |
107 | 107 |
| 108 int routing_id() const { |
| 109 // We return a fake routing_id() in the context of this test. |
| 110 return 0; |
| 111 } |
| 112 |
108 IPC::TestSink sink_; | 113 IPC::TestSink sink_; |
109 scoped_ptr<ScreenOrientationDispatcher> dispatcher_; | 114 scoped_ptr<ScreenOrientationDispatcher> dispatcher_; |
110 }; | 115 }; |
111 | 116 |
112 // Test that calling lockOrientation() followed by unlockOrientation() cancel | 117 // Test that calling lockOrientation() followed by unlockOrientation() cancel |
113 // the lockOrientation(). | 118 // the lockOrientation(). |
114 TEST_F(ScreenOrientationDispatcherTest, CancelPending_Unlocking) { | 119 TEST_F(ScreenOrientationDispatcherTest, CancelPending_Unlocking) { |
115 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 120 MockLockOrientationCallback::LockOrientationResultHolder callback_results; |
116 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 121 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, |
117 new MockLockOrientationCallback(&callback_results)); | 122 new MockLockOrientationCallback(&callback_results)); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 blink::WebLockOrientationCallback::ErrorTypeFullScreenRequired); | 154 blink::WebLockOrientationCallback::ErrorTypeFullScreenRequired); |
150 errors.push_back(blink::WebLockOrientationCallback::ErrorTypeCanceled); | 155 errors.push_back(blink::WebLockOrientationCallback::ErrorTypeCanceled); |
151 | 156 |
152 for (std::list<blink::WebLockOrientationCallback::ErrorType>::const_iterator | 157 for (std::list<blink::WebLockOrientationCallback::ErrorType>::const_iterator |
153 it = errors.begin(); it != errors.end(); ++it) { | 158 it = errors.begin(); it != errors.end(); ++it) { |
154 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 159 MockLockOrientationCallback::LockOrientationResultHolder callback_results; |
155 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 160 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, |
156 new MockLockOrientationCallback(&callback_results)); | 161 new MockLockOrientationCallback(&callback_results)); |
157 | 162 |
158 int request_id = GetFirstLockRequestIdFromSink(); | 163 int request_id = GetFirstLockRequestIdFromSink(); |
159 OnMessageReceived(ScreenOrientationMsg_LockError(request_id, *it)); | 164 OnMessageReceived( |
| 165 ScreenOrientationMsg_LockError(routing_id(), request_id, *it)); |
160 | 166 |
161 EXPECT_FALSE(callback_results.succeeded_); | 167 EXPECT_FALSE(callback_results.succeeded_); |
162 EXPECT_TRUE(callback_results.failed_); | 168 EXPECT_TRUE(callback_results.failed_); |
163 EXPECT_EQ(*it, callback_results.error_); | 169 EXPECT_EQ(*it, callback_results.error_); |
164 | 170 |
165 sink().ClearMessages(); | 171 sink().ClearMessages(); |
166 } | 172 } |
167 } | 173 } |
168 | 174 |
169 // Test that when a LockSuccess message is received, the request is set as | 175 // Test that when a LockSuccess message is received, the request is set as |
(...skipping 10 matching lines...) Expand all Loading... |
180 }; | 186 }; |
181 | 187 |
182 int orientationsCount = 4; | 188 int orientationsCount = 4; |
183 | 189 |
184 for (int i = 0; i < orientationsCount; ++i) { | 190 for (int i = 0; i < orientationsCount; ++i) { |
185 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 191 MockLockOrientationCallback::LockOrientationResultHolder callback_results; |
186 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 192 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, |
187 new MockLockOrientationCallback(&callback_results)); | 193 new MockLockOrientationCallback(&callback_results)); |
188 | 194 |
189 int request_id = GetFirstLockRequestIdFromSink(); | 195 int request_id = GetFirstLockRequestIdFromSink(); |
190 OnMessageReceived(ScreenOrientationMsg_LockSuccess(request_id, | 196 OnMessageReceived(ScreenOrientationMsg_LockSuccess(routing_id(), |
| 197 request_id, |
191 orientations[i].angle, | 198 orientations[i].angle, |
192 orientations[i].type)); | 199 orientations[i].type)); |
193 | 200 |
194 EXPECT_TRUE(callback_results.succeeded_); | 201 EXPECT_TRUE(callback_results.succeeded_); |
195 EXPECT_FALSE(callback_results.failed_); | 202 EXPECT_FALSE(callback_results.failed_); |
196 EXPECT_EQ(orientations[i].angle, callback_results.angle_); | 203 EXPECT_EQ(orientations[i].angle, callback_results.angle_); |
197 EXPECT_EQ(orientations[i].type, callback_results.orientation_); | 204 EXPECT_EQ(orientations[i].type, callback_results.orientation_); |
198 | 205 |
199 sink().ClearMessages(); | 206 sink().ClearMessages(); |
200 } | 207 } |
201 } | 208 } |
202 | 209 |
203 // Test an edge case: a LockSuccess is received but it matches no pending | 210 // Test an edge case: a LockSuccess is received but it matches no pending |
204 // callback. | 211 // callback. |
205 TEST_F(ScreenOrientationDispatcherTest, SuccessForUnknownRequest) { | 212 TEST_F(ScreenOrientationDispatcherTest, SuccessForUnknownRequest) { |
206 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 213 MockLockOrientationCallback::LockOrientationResultHolder callback_results; |
207 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 214 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, |
208 new MockLockOrientationCallback(&callback_results)); | 215 new MockLockOrientationCallback(&callback_results)); |
209 | 216 |
210 int request_id = GetFirstLockRequestIdFromSink(); | 217 int request_id = GetFirstLockRequestIdFromSink(); |
211 OnMessageReceived(ScreenOrientationMsg_LockSuccess( | 218 OnMessageReceived(ScreenOrientationMsg_LockSuccess( |
| 219 routing_id(), |
212 request_id + 1, | 220 request_id + 1, |
213 90, | 221 90, |
214 blink::WebScreenOrientationLandscapePrimary)); | 222 blink::WebScreenOrientationLandscapePrimary)); |
215 | 223 |
216 EXPECT_FALSE(callback_results.succeeded_); | 224 EXPECT_FALSE(callback_results.succeeded_); |
217 EXPECT_FALSE(callback_results.failed_); | 225 EXPECT_FALSE(callback_results.failed_); |
218 } | 226 } |
219 | 227 |
220 // Test an edge case: a LockError is received but it matches no pending | 228 // Test an edge case: a LockError is received but it matches no pending |
221 // callback. | 229 // callback. |
222 TEST_F(ScreenOrientationDispatcherTest, ErrorForUnknownRequest) { | 230 TEST_F(ScreenOrientationDispatcherTest, ErrorForUnknownRequest) { |
223 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 231 MockLockOrientationCallback::LockOrientationResultHolder callback_results; |
224 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 232 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, |
225 new MockLockOrientationCallback(&callback_results)); | 233 new MockLockOrientationCallback(&callback_results)); |
226 | 234 |
227 int request_id = GetFirstLockRequestIdFromSink(); | 235 int request_id = GetFirstLockRequestIdFromSink(); |
228 OnMessageReceived(ScreenOrientationMsg_LockError( | 236 OnMessageReceived(ScreenOrientationMsg_LockError( |
| 237 routing_id(), |
229 request_id + 1, | 238 request_id + 1, |
230 blink::WebLockOrientationCallback::ErrorTypeCanceled)); | 239 blink::WebLockOrientationCallback::ErrorTypeCanceled)); |
231 | 240 |
232 EXPECT_FALSE(callback_results.succeeded_); | 241 EXPECT_FALSE(callback_results.succeeded_); |
233 EXPECT_FALSE(callback_results.failed_); | 242 EXPECT_FALSE(callback_results.failed_); |
234 } | 243 } |
235 | 244 |
236 // Test the following scenario: | 245 // Test the following scenario: |
237 // - request1 is received by the dispatcher; | 246 // - request1 is received by the dispatcher; |
238 // - request2 is received by the dispatcher; | 247 // - request2 is received by the dispatcher; |
239 // - request1 is rejected; | 248 // - request1 is rejected; |
240 // - request1 success response is received. | 249 // - request1 success response is received. |
241 // Expected: request1 is still rejected, request2 has not been set as succeeded. | 250 // Expected: request1 is still rejected, request2 has not been set as succeeded. |
242 TEST_F(ScreenOrientationDispatcherTest, RaceScenario) { | 251 TEST_F(ScreenOrientationDispatcherTest, RaceScenario) { |
243 MockLockOrientationCallback::LockOrientationResultHolder callback_results1; | 252 MockLockOrientationCallback::LockOrientationResultHolder callback_results1; |
244 MockLockOrientationCallback::LockOrientationResultHolder callback_results2; | 253 MockLockOrientationCallback::LockOrientationResultHolder callback_results2; |
245 | 254 |
246 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 255 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, |
247 new MockLockOrientationCallback(&callback_results1)); | 256 new MockLockOrientationCallback(&callback_results1)); |
248 int request_id1 = GetFirstLockRequestIdFromSink(); | 257 int request_id1 = GetFirstLockRequestIdFromSink(); |
249 | 258 |
250 LockOrientation(blink::WebScreenOrientationLockLandscapePrimary, | 259 LockOrientation(blink::WebScreenOrientationLockLandscapePrimary, |
251 new MockLockOrientationCallback(&callback_results2)); | 260 new MockLockOrientationCallback(&callback_results2)); |
252 | 261 |
253 // callback_results1 must be rejected, tested in CancelPending_DoubleLock. | 262 // callback_results1 must be rejected, tested in CancelPending_DoubleLock. |
254 | 263 |
255 OnMessageReceived(ScreenOrientationMsg_LockSuccess( | 264 OnMessageReceived(ScreenOrientationMsg_LockSuccess( |
| 265 routing_id(), |
256 request_id1, | 266 request_id1, |
257 0, | 267 0, |
258 blink::WebScreenOrientationPortraitPrimary)); | 268 blink::WebScreenOrientationPortraitPrimary)); |
259 | 269 |
260 // First request is still rejected. | 270 // First request is still rejected. |
261 EXPECT_FALSE(callback_results1.succeeded_); | 271 EXPECT_FALSE(callback_results1.succeeded_); |
262 EXPECT_TRUE(callback_results1.failed_); | 272 EXPECT_TRUE(callback_results1.failed_); |
263 EXPECT_EQ(blink::WebLockOrientationCallback::ErrorTypeCanceled, | 273 EXPECT_EQ(blink::WebLockOrientationCallback::ErrorTypeCanceled, |
264 callback_results1.error_); | 274 callback_results1.error_); |
265 | 275 |
266 // Second request is still pending. | 276 // Second request is still pending. |
267 EXPECT_FALSE(callback_results2.succeeded_); | 277 EXPECT_FALSE(callback_results2.succeeded_); |
268 EXPECT_FALSE(callback_results2.failed_); | 278 EXPECT_FALSE(callback_results2.failed_); |
269 } | 279 } |
270 | 280 |
271 } // namespace content | 281 } // namespace content |
OLD | NEW |