| 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 "content/renderer/screen_orientation/screen_orientation_dispatcher.h" | 5 #include "content/renderer/screen_orientation/screen_orientation_dispatcher.h" |
| 6 | 6 |
| 7 #include <list> | 7 #include <list> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <tuple> | 9 #include <tuple> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "content/common/screen_orientation_messages.h" | 12 #include "content/public/test/render_view_test.h" |
| 13 #include "content/public/test/test_utils.h" | 13 #include "content/public/test/test_utils.h" |
| 14 #include "ipc/ipc_test_sink.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebLockO
rientationCallback.h" | 14 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebLockO
rientationCallback.h" |
| 17 | 15 |
| 18 namespace content { | 16 namespace content { |
| 19 | 17 |
| 18 using LockOrientationCallback = |
| 19 mojom::ScreenOrientation::LockOrientationCallback; |
| 20 using LockResult = ::blink::mojom::ScreenOrientationLockResult; |
| 21 |
| 20 // MockLockOrientationCallback is an implementation of | 22 // MockLockOrientationCallback is an implementation of |
| 21 // WebLockOrientationCallback and takes a LockOrientationResultHolder* as a | 23 // WebLockOrientationCallback and takes a LockOrientationResultHolder* as a |
| 22 // parameter when being constructed. The |results_| pointer is owned by the | 24 // parameter when being constructed. The |results_| pointer is owned by the |
| 23 // caller and not by the callback object. The intent being that as soon as the | 25 // caller and not by the callback object. The intent being that as soon as the |
| 24 // callback is resolved, it will be killed so we use the | 26 // callback is resolved, it will be killed so we use the |
| 25 // LockOrientationResultHolder to know in which state the callback object is at | 27 // LockOrientationResultHolder to know in which state the callback object is at |
| 26 // any time. | 28 // any time. |
| 27 class MockLockOrientationCallback : public blink::WebLockOrientationCallback { | 29 class MockLockOrientationCallback : public blink::WebLockOrientationCallback { |
| 28 public: | 30 public: |
| 29 struct LockOrientationResultHolder { | 31 struct LockOrientationResultHolder { |
| 30 LockOrientationResultHolder() | 32 LockOrientationResultHolder() : succeeded_(false), failed_(false) {} |
| 31 : succeeded_(false), failed_(false) {} | |
| 32 | 33 |
| 33 bool succeeded_; | 34 bool succeeded_; |
| 34 bool failed_; | 35 bool failed_; |
| 35 blink::WebLockOrientationError error_; | 36 blink::WebLockOrientationError error_; |
| 36 }; | 37 }; |
| 37 | 38 |
| 38 explicit MockLockOrientationCallback(LockOrientationResultHolder* results) | 39 explicit MockLockOrientationCallback(LockOrientationResultHolder* results) |
| 39 : results_(results) {} | 40 : results_(results) {} |
| 40 | 41 |
| 41 void onSuccess() override { results_->succeeded_ = true; } | 42 void onSuccess() override { results_->succeeded_ = true; } |
| 42 | 43 |
| 43 void onError(blink::WebLockOrientationError error) override { | 44 void onError(blink::WebLockOrientationError error) override { |
| 44 results_->failed_ = true; | 45 results_->failed_ = true; |
| 45 results_->error_ = error; | 46 results_->error_ = error; |
| 46 } | 47 } |
| 47 | 48 |
| 48 private: | 49 private: |
| 49 ~MockLockOrientationCallback() override {} | 50 ~MockLockOrientationCallback() override {} |
| 50 | 51 |
| 51 LockOrientationResultHolder* results_; | 52 LockOrientationResultHolder* results_; |
| 52 }; | 53 }; |
| 53 | 54 |
| 54 class ScreenOrientationDispatcherWithSink : public ScreenOrientationDispatcher { | 55 // TODO(lunalu): When available, test mojo service without needing a |
| 55 public: | 56 // RenderViewTest. |
| 56 explicit ScreenOrientationDispatcherWithSink(IPC::TestSink* sink) | 57 class ScreenOrientationDispatcherTest : public RenderViewTest { |
| 57 :ScreenOrientationDispatcher(NULL) , sink_(sink) { | |
| 58 } | |
| 59 | |
| 60 bool Send(IPC::Message* message) override { return sink_->Send(message); } | |
| 61 | |
| 62 IPC::TestSink* sink_; | |
| 63 }; | |
| 64 | |
| 65 class ScreenOrientationDispatcherTest : public testing::Test { | |
| 66 protected: | 58 protected: |
| 67 void SetUp() override { | 59 void SetUp() override { |
| 68 dispatcher_.reset(new ScreenOrientationDispatcherWithSink(&sink_)); | 60 RenderViewTest::SetUp(); |
| 69 } | 61 dispatcher_.reset(new ScreenOrientationDispatcher(nullptr)); |
| 70 | 62 ScreenOrientationAssociatedPtr screen_orientation; |
| 71 int GetFirstLockRequestIdFromSink() { | 63 mojo::GetDummyProxyForTesting(&screen_orientation); |
| 72 const IPC::Message* msg = sink().GetFirstMessageMatching( | 64 dispatcher_->SetScreenOrientationForTests(screen_orientation); |
| 73 ScreenOrientationHostMsg_LockRequest::ID); | |
| 74 EXPECT_TRUE(msg != NULL); | |
| 75 | |
| 76 std::tuple<blink::WebScreenOrientationLockType, int> params; | |
| 77 ScreenOrientationHostMsg_LockRequest::Read(msg, ¶ms); | |
| 78 return std::get<1>(params); | |
| 79 } | |
| 80 | |
| 81 IPC::TestSink& sink() { | |
| 82 return sink_; | |
| 83 } | 65 } |
| 84 | 66 |
| 85 void LockOrientation(blink::WebScreenOrientationLockType orientation, | 67 void LockOrientation(blink::WebScreenOrientationLockType orientation, |
| 86 blink::WebLockOrientationCallback* callback) { | 68 blink::WebLockOrientationCallback* callback) { |
| 87 dispatcher_->lockOrientation(orientation, callback); | 69 dispatcher_->lockOrientation(orientation, callback); |
| 88 } | 70 } |
| 89 | 71 |
| 90 void UnlockOrientation() { | 72 void UnlockOrientation() { dispatcher_->unlockOrientation(); } |
| 91 dispatcher_->unlockOrientation(); | 73 |
| 74 int GetRequestId() { return dispatcher_->GetRequestIdForTests(); } |
| 75 |
| 76 void RunLockResultCallback(int request_id, LockResult result) { |
| 77 dispatcher_->OnLockOrientationResult(request_id, result); |
| 92 } | 78 } |
| 93 | 79 |
| 94 void OnMessageReceived(const IPC::Message& message) { | |
| 95 dispatcher_->OnMessageReceived(message); | |
| 96 } | |
| 97 | |
| 98 int routing_id() const { | |
| 99 // We return a fake routing_id() in the context of this test. | |
| 100 return 0; | |
| 101 } | |
| 102 | |
| 103 IPC::TestSink sink_; | |
| 104 std::unique_ptr<ScreenOrientationDispatcher> dispatcher_; | 80 std::unique_ptr<ScreenOrientationDispatcher> dispatcher_; |
| 105 }; | 81 }; |
| 106 | 82 |
| 107 // Test that calling lockOrientation() followed by unlockOrientation() cancel | 83 // Test that calling lockOrientation() followed by unlockOrientation() cancel |
| 108 // the lockOrientation(). | 84 // the lockOrientation(). |
| 109 TEST_F(ScreenOrientationDispatcherTest, CancelPending_Unlocking) { | 85 TEST_F(ScreenOrientationDispatcherTest, CancelPending_Unlocking) { |
| 110 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 86 MockLockOrientationCallback::LockOrientationResultHolder callback_results; |
| 87 |
| 111 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 88 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, |
| 112 new MockLockOrientationCallback(&callback_results)); | 89 new MockLockOrientationCallback(&callback_results)); |
| 113 UnlockOrientation(); | 90 UnlockOrientation(); |
| 114 | 91 |
| 115 EXPECT_FALSE(callback_results.succeeded_); | 92 EXPECT_FALSE(callback_results.succeeded_); |
| 116 EXPECT_TRUE(callback_results.failed_); | 93 EXPECT_TRUE(callback_results.failed_); |
| 117 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results.error_); | 94 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results.error_); |
| 118 } | 95 } |
| 119 | 96 |
| 120 // Test that calling lockOrientation() twice cancel the first lockOrientation(). | 97 // Test that calling lockOrientation() twice cancel the first lockOrientation(). |
| 121 TEST_F(ScreenOrientationDispatcherTest, CancelPending_DoubleLock) { | 98 TEST_F(ScreenOrientationDispatcherTest, CancelPending_DoubleLock) { |
| 122 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 99 MockLockOrientationCallback::LockOrientationResultHolder callback_results; |
| 123 // We create the object to prevent leaks but never actually use it. | 100 // We create the object to prevent leaks but never actually use it. |
| 124 MockLockOrientationCallback::LockOrientationResultHolder callback_results2; | 101 MockLockOrientationCallback::LockOrientationResultHolder callback_results2; |
| 125 | 102 |
| 126 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 103 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, |
| 127 new MockLockOrientationCallback(&callback_results)); | 104 new MockLockOrientationCallback(&callback_results)); |
| 105 |
| 128 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 106 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, |
| 129 new MockLockOrientationCallback(&callback_results2)); | 107 new MockLockOrientationCallback(&callback_results2)); |
| 130 | 108 |
| 131 EXPECT_FALSE(callback_results.succeeded_); | 109 EXPECT_FALSE(callback_results.succeeded_); |
| 132 EXPECT_TRUE(callback_results.failed_); | 110 EXPECT_TRUE(callback_results.failed_); |
| 133 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results.error_); | 111 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results.error_); |
| 134 } | 112 } |
| 135 | 113 |
| 136 // Test that when a LockError message is received, the request is set as failed | 114 // Test that when a LockError message is received, the request is set as failed |
| 137 // with the correct values. | 115 // with the correct values. |
| 138 TEST_F(ScreenOrientationDispatcherTest, LockRequest_Error) { | 116 TEST_F(ScreenOrientationDispatcherTest, LockRequest_Error) { |
| 139 std::list<blink::WebLockOrientationError> errors; | 117 std::map<LockResult, blink::WebLockOrientationError> errors; |
| 140 errors.push_back(blink::WebLockOrientationErrorNotAvailable); | 118 errors[LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE] = |
| 141 errors.push_back( | 119 blink::WebLockOrientationErrorNotAvailable; |
| 142 blink::WebLockOrientationErrorFullscreenRequired); | 120 errors[LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED] = |
| 143 errors.push_back(blink::WebLockOrientationErrorCanceled); | 121 blink::WebLockOrientationErrorFullscreenRequired; |
| 122 errors[LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED] = |
| 123 blink::WebLockOrientationErrorCanceled; |
| 144 | 124 |
| 145 for (std::list<blink::WebLockOrientationError>::const_iterator | 125 for (std::map<LockResult, blink::WebLockOrientationError>::const_iterator it = |
| 146 it = errors.begin(); it != errors.end(); ++it) { | 126 errors.begin(); |
| 127 it != errors.end(); ++it) { |
| 147 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 128 MockLockOrientationCallback::LockOrientationResultHolder callback_results; |
| 148 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 129 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, |
| 149 new MockLockOrientationCallback(&callback_results)); | 130 new MockLockOrientationCallback(&callback_results)); |
| 150 | 131 RunLockResultCallback(GetRequestId(), it->first); |
| 151 int request_id = GetFirstLockRequestIdFromSink(); | |
| 152 OnMessageReceived( | |
| 153 ScreenOrientationMsg_LockError(routing_id(), request_id, *it)); | |
| 154 | |
| 155 EXPECT_FALSE(callback_results.succeeded_); | 132 EXPECT_FALSE(callback_results.succeeded_); |
| 156 EXPECT_TRUE(callback_results.failed_); | 133 EXPECT_TRUE(callback_results.failed_); |
| 157 EXPECT_EQ(*it, callback_results.error_); | 134 EXPECT_EQ(it->second, callback_results.error_); |
| 158 | |
| 159 sink().ClearMessages(); | |
| 160 } | 135 } |
| 161 } | 136 } |
| 162 | 137 |
| 163 // Test that when a LockSuccess message is received, the request is set as | 138 // Test that when a LockSuccess message is received, the request is set as |
| 164 // succeeded. | 139 // succeeded. |
| 165 TEST_F(ScreenOrientationDispatcherTest, LockRequest_Success) { | 140 TEST_F(ScreenOrientationDispatcherTest, LockRequest_Success) { |
| 166 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 141 MockLockOrientationCallback::LockOrientationResultHolder callback_results; |
| 167 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 142 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, |
| 168 new MockLockOrientationCallback(&callback_results)); | 143 new MockLockOrientationCallback(&callback_results)); |
| 169 | 144 |
| 170 int request_id = GetFirstLockRequestIdFromSink(); | 145 RunLockResultCallback(GetRequestId(), |
| 171 OnMessageReceived(ScreenOrientationMsg_LockSuccess(routing_id(), | 146 LockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); |
| 172 request_id)); | |
| 173 | 147 |
| 174 EXPECT_TRUE(callback_results.succeeded_); | 148 EXPECT_TRUE(callback_results.succeeded_); |
| 175 EXPECT_FALSE(callback_results.failed_); | 149 EXPECT_FALSE(callback_results.failed_); |
| 176 | |
| 177 sink().ClearMessages(); | |
| 178 } | |
| 179 | |
| 180 // Test an edge case: a LockSuccess is received but it matches no pending | |
| 181 // callback. | |
| 182 TEST_F(ScreenOrientationDispatcherTest, SuccessForUnknownRequest) { | |
| 183 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | |
| 184 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | |
| 185 new MockLockOrientationCallback(&callback_results)); | |
| 186 | |
| 187 int request_id = GetFirstLockRequestIdFromSink(); | |
| 188 OnMessageReceived(ScreenOrientationMsg_LockSuccess(routing_id(), | |
| 189 request_id + 1)); | |
| 190 | |
| 191 EXPECT_FALSE(callback_results.succeeded_); | |
| 192 EXPECT_FALSE(callback_results.failed_); | |
| 193 } | |
| 194 | |
| 195 // Test an edge case: a LockError is received but it matches no pending | |
| 196 // callback. | |
| 197 TEST_F(ScreenOrientationDispatcherTest, ErrorForUnknownRequest) { | |
| 198 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | |
| 199 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | |
| 200 new MockLockOrientationCallback(&callback_results)); | |
| 201 | |
| 202 int request_id = GetFirstLockRequestIdFromSink(); | |
| 203 OnMessageReceived(ScreenOrientationMsg_LockError( | |
| 204 routing_id(), request_id + 1, blink::WebLockOrientationErrorCanceled)); | |
| 205 | |
| 206 EXPECT_FALSE(callback_results.succeeded_); | |
| 207 EXPECT_FALSE(callback_results.failed_); | |
| 208 } | 150 } |
| 209 | 151 |
| 210 // Test the following scenario: | 152 // Test the following scenario: |
| 211 // - request1 is received by the dispatcher; | 153 // - request1 is received by the dispatcher; |
| 212 // - request2 is received by the dispatcher; | 154 // - request2 is received by the dispatcher; |
| 213 // - request1 is rejected; | 155 // - request1 is rejected; |
| 214 // - request1 success response is received. | 156 // - request1 success response is received. |
| 215 // Expected: request1 is still rejected, request2 has not been set as succeeded. | 157 // Expected: request1 is still rejected, request2 has not been set as succeeded. |
| 216 TEST_F(ScreenOrientationDispatcherTest, RaceScenario) { | 158 TEST_F(ScreenOrientationDispatcherTest, RaceScenario) { |
| 217 MockLockOrientationCallback::LockOrientationResultHolder callback_results1; | 159 MockLockOrientationCallback::LockOrientationResultHolder callback_results1; |
| 218 MockLockOrientationCallback::LockOrientationResultHolder callback_results2; | 160 MockLockOrientationCallback::LockOrientationResultHolder callback_results2; |
| 219 | 161 |
| 220 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 162 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, |
| 221 new MockLockOrientationCallback(&callback_results1)); | 163 new MockLockOrientationCallback(&callback_results1)); |
| 222 int request_id1 = GetFirstLockRequestIdFromSink(); | 164 int request_id1 = GetRequestId(); |
| 223 | 165 |
| 224 LockOrientation(blink::WebScreenOrientationLockLandscapePrimary, | 166 LockOrientation(blink::WebScreenOrientationLockLandscapePrimary, |
| 225 new MockLockOrientationCallback(&callback_results2)); | 167 new MockLockOrientationCallback(&callback_results2)); |
| 226 | 168 |
| 227 // callback_results1 must be rejected, tested in CancelPending_DoubleLock. | 169 // callback_results1 must be rejected, tested in CancelPending_DoubleLock. |
| 228 | 170 |
| 229 OnMessageReceived(ScreenOrientationMsg_LockSuccess(routing_id(), | 171 RunLockResultCallback(request_id1, |
| 230 request_id1)); | 172 LockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); |
| 231 | 173 |
| 232 // First request is still rejected. | 174 // First request is still rejected. |
| 233 EXPECT_FALSE(callback_results1.succeeded_); | 175 EXPECT_FALSE(callback_results1.succeeded_); |
| 234 EXPECT_TRUE(callback_results1.failed_); | 176 EXPECT_TRUE(callback_results1.failed_); |
| 235 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results1.error_); | 177 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results1.error_); |
| 236 | 178 |
| 237 // Second request is still pending. | 179 // Second request is still pending. |
| 238 EXPECT_FALSE(callback_results2.succeeded_); | 180 EXPECT_FALSE(callback_results2.succeeded_); |
| 239 EXPECT_FALSE(callback_results2.failed_); | 181 EXPECT_FALSE(callback_results2.failed_); |
| 240 } | 182 } |
| 241 | 183 |
| 242 } // namespace content | 184 } // namespace content |
| OLD | NEW |