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