| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/renderer/screen_orientation/screen_orientation_dispatcher.h" | |
| 6 | |
| 7 #include <list> | |
| 8 #include <memory> | |
| 9 #include <tuple> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "base/memory/ptr_util.h" | |
| 13 #include "content/public/test/render_view_test.h" | |
| 14 #include "content/public/test/test_utils.h" | |
| 15 #include "third_party/WebKit/public/platform/modules/screen_orientation/WebLockO
rientationCallback.h" | |
| 16 | |
| 17 namespace content { | |
| 18 | |
| 19 using LockOrientationCallback = | |
| 20 device::mojom::ScreenOrientation::LockOrientationCallback; | |
| 21 using LockResult = device::mojom::ScreenOrientationLockResult; | |
| 22 | |
| 23 // MockLockOrientationCallback is an implementation of | |
| 24 // WebLockOrientationCallback and takes a LockOrientationResultHolder* as a | |
| 25 // parameter when being constructed. The |results_| pointer is owned by the | |
| 26 // caller and not by the callback object. The intent being that as soon as the | |
| 27 // callback is resolved, it will be killed so we use the | |
| 28 // LockOrientationResultHolder to know in which state the callback object is at | |
| 29 // any time. | |
| 30 class MockLockOrientationCallback : public blink::WebLockOrientationCallback { | |
| 31 public: | |
| 32 struct LockOrientationResultHolder { | |
| 33 LockOrientationResultHolder() : succeeded_(false), failed_(false) {} | |
| 34 | |
| 35 bool succeeded_; | |
| 36 bool failed_; | |
| 37 blink::WebLockOrientationError error_; | |
| 38 }; | |
| 39 | |
| 40 explicit MockLockOrientationCallback(LockOrientationResultHolder* results) | |
| 41 : results_(results) {} | |
| 42 | |
| 43 void onSuccess() override { results_->succeeded_ = true; } | |
| 44 | |
| 45 void onError(blink::WebLockOrientationError error) override { | |
| 46 results_->failed_ = true; | |
| 47 results_->error_ = error; | |
| 48 } | |
| 49 | |
| 50 private: | |
| 51 LockOrientationResultHolder* results_; | |
| 52 }; | |
| 53 | |
| 54 // TODO(lunalu): When available, test mojo service without needing a | |
| 55 // RenderViewTest. | |
| 56 class ScreenOrientationDispatcherTest : public RenderViewTest { | |
| 57 protected: | |
| 58 void SetUp() override { | |
| 59 RenderViewTest::SetUp(); | |
| 60 dispatcher_.reset(new ScreenOrientationDispatcher(nullptr)); | |
| 61 ScreenOrientationAssociatedPtr screen_orientation; | |
| 62 mojo::GetIsolatedProxy(&screen_orientation); | |
| 63 dispatcher_->SetScreenOrientationForTests(screen_orientation); | |
| 64 } | |
| 65 | |
| 66 void LockOrientation( | |
| 67 blink::WebScreenOrientationLockType orientation, | |
| 68 std::unique_ptr<blink::WebLockOrientationCallback> callback) { | |
| 69 dispatcher_->lockOrientation(orientation, std::move(callback)); | |
| 70 } | |
| 71 | |
| 72 void UnlockOrientation() { dispatcher_->unlockOrientation(); } | |
| 73 | |
| 74 int GetRequestId() { return dispatcher_->GetRequestIdForTests(); } | |
| 75 | |
| 76 void RunLockResultCallback(int request_id, LockResult result) { | |
| 77 dispatcher_->OnLockOrientationResult(request_id, result); | |
| 78 } | |
| 79 | |
| 80 std::unique_ptr<ScreenOrientationDispatcher> dispatcher_; | |
| 81 }; | |
| 82 | |
| 83 // Test that calling lockOrientation() followed by unlockOrientation() cancel | |
| 84 // the lockOrientation(). | |
| 85 TEST_F(ScreenOrientationDispatcherTest, CancelPending_Unlocking) { | |
| 86 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | |
| 87 | |
| 88 LockOrientation( | |
| 89 blink::WebScreenOrientationLockPortraitPrimary, | |
| 90 base::MakeUnique<MockLockOrientationCallback>(&callback_results)); | |
| 91 UnlockOrientation(); | |
| 92 | |
| 93 EXPECT_FALSE(callback_results.succeeded_); | |
| 94 EXPECT_TRUE(callback_results.failed_); | |
| 95 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results.error_); | |
| 96 } | |
| 97 | |
| 98 // Test that calling lockOrientation() twice cancel the first lockOrientation(). | |
| 99 TEST_F(ScreenOrientationDispatcherTest, CancelPending_DoubleLock) { | |
| 100 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | |
| 101 // We create the object to prevent leaks but never actually use it. | |
| 102 MockLockOrientationCallback::LockOrientationResultHolder callback_results2; | |
| 103 | |
| 104 LockOrientation( | |
| 105 blink::WebScreenOrientationLockPortraitPrimary, | |
| 106 base::MakeUnique<MockLockOrientationCallback>(&callback_results)); | |
| 107 | |
| 108 LockOrientation( | |
| 109 blink::WebScreenOrientationLockPortraitPrimary, | |
| 110 base::MakeUnique<MockLockOrientationCallback>(&callback_results2)); | |
| 111 | |
| 112 EXPECT_FALSE(callback_results.succeeded_); | |
| 113 EXPECT_TRUE(callback_results.failed_); | |
| 114 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results.error_); | |
| 115 } | |
| 116 | |
| 117 // Test that when a LockError message is received, the request is set as failed | |
| 118 // with the correct values. | |
| 119 TEST_F(ScreenOrientationDispatcherTest, LockRequest_Error) { | |
| 120 std::map<LockResult, blink::WebLockOrientationError> errors; | |
| 121 errors[LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE] = | |
| 122 blink::WebLockOrientationErrorNotAvailable; | |
| 123 errors[LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED] = | |
| 124 blink::WebLockOrientationErrorFullscreenRequired; | |
| 125 errors[LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED] = | |
| 126 blink::WebLockOrientationErrorCanceled; | |
| 127 | |
| 128 for (std::map<LockResult, blink::WebLockOrientationError>::const_iterator it = | |
| 129 errors.begin(); | |
| 130 it != errors.end(); ++it) { | |
| 131 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | |
| 132 LockOrientation( | |
| 133 blink::WebScreenOrientationLockPortraitPrimary, | |
| 134 base::MakeUnique<MockLockOrientationCallback>(&callback_results)); | |
| 135 RunLockResultCallback(GetRequestId(), it->first); | |
| 136 EXPECT_FALSE(callback_results.succeeded_); | |
| 137 EXPECT_TRUE(callback_results.failed_); | |
| 138 EXPECT_EQ(it->second, callback_results.error_); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 // Test that when a LockSuccess message is received, the request is set as | |
| 143 // succeeded. | |
| 144 TEST_F(ScreenOrientationDispatcherTest, LockRequest_Success) { | |
| 145 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | |
| 146 LockOrientation( | |
| 147 blink::WebScreenOrientationLockPortraitPrimary, | |
| 148 base::MakeUnique<MockLockOrientationCallback>(&callback_results)); | |
| 149 | |
| 150 RunLockResultCallback(GetRequestId(), | |
| 151 LockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); | |
| 152 | |
| 153 EXPECT_TRUE(callback_results.succeeded_); | |
| 154 EXPECT_FALSE(callback_results.failed_); | |
| 155 } | |
| 156 | |
| 157 // Test the following scenario: | |
| 158 // - request1 is received by the dispatcher; | |
| 159 // - request2 is received by the dispatcher; | |
| 160 // - request1 is rejected; | |
| 161 // - request1 success response is received. | |
| 162 // Expected: request1 is still rejected, request2 has not been set as succeeded. | |
| 163 TEST_F(ScreenOrientationDispatcherTest, RaceScenario) { | |
| 164 MockLockOrientationCallback::LockOrientationResultHolder callback_results1; | |
| 165 MockLockOrientationCallback::LockOrientationResultHolder callback_results2; | |
| 166 | |
| 167 LockOrientation( | |
| 168 blink::WebScreenOrientationLockPortraitPrimary, | |
| 169 base::MakeUnique<MockLockOrientationCallback>(&callback_results1)); | |
| 170 int request_id1 = GetRequestId(); | |
| 171 | |
| 172 LockOrientation( | |
| 173 blink::WebScreenOrientationLockLandscapePrimary, | |
| 174 base::MakeUnique<MockLockOrientationCallback>(&callback_results2)); | |
| 175 | |
| 176 // callback_results1 must be rejected, tested in CancelPending_DoubleLock. | |
| 177 | |
| 178 RunLockResultCallback(request_id1, | |
| 179 LockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); | |
| 180 | |
| 181 // First request is still rejected. | |
| 182 EXPECT_FALSE(callback_results1.succeeded_); | |
| 183 EXPECT_TRUE(callback_results1.failed_); | |
| 184 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results1.error_); | |
| 185 | |
| 186 // Second request is still pending. | |
| 187 EXPECT_FALSE(callback_results2.succeeded_); | |
| 188 EXPECT_FALSE(callback_results2.failed_); | |
| 189 } | |
| 190 | |
| 191 } // namespace content | |
| OLD | NEW |