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 |
(...skipping 29 matching lines...) Expand all Loading... |
40 : results_(results) {} | 40 : results_(results) {} |
41 | 41 |
42 void onSuccess() override { results_->succeeded_ = true; } | 42 void onSuccess() override { results_->succeeded_ = true; } |
43 | 43 |
44 void onError(blink::WebLockOrientationError error) override { | 44 void onError(blink::WebLockOrientationError error) override { |
45 results_->failed_ = true; | 45 results_->failed_ = true; |
46 results_->error_ = error; | 46 results_->error_ = error; |
47 } | 47 } |
48 | 48 |
49 private: | 49 private: |
50 ~MockLockOrientationCallback() override {} | |
51 | |
52 LockOrientationResultHolder* results_; | 50 LockOrientationResultHolder* results_; |
53 }; | 51 }; |
54 | 52 |
55 // TODO(lunalu): When available, test mojo service without needing a | 53 // TODO(lunalu): When available, test mojo service without needing a |
56 // RenderViewTest. | 54 // RenderViewTest. |
57 class ScreenOrientationDispatcherTest : public RenderViewTest { | 55 class ScreenOrientationDispatcherTest : public RenderViewTest { |
58 protected: | 56 protected: |
59 void SetUp() override { | 57 void SetUp() override { |
60 RenderViewTest::SetUp(); | 58 RenderViewTest::SetUp(); |
61 dispatcher_.reset(new ScreenOrientationDispatcher(nullptr)); | 59 dispatcher_.reset(new ScreenOrientationDispatcher(nullptr)); |
62 ScreenOrientationAssociatedPtr screen_orientation; | 60 ScreenOrientationAssociatedPtr screen_orientation; |
63 mojo::GetDummyProxyForTesting(&screen_orientation); | 61 mojo::GetDummyProxyForTesting(&screen_orientation); |
64 dispatcher_->SetScreenOrientationForTests(screen_orientation); | 62 dispatcher_->SetScreenOrientationForTests(screen_orientation); |
65 } | 63 } |
66 | 64 |
67 void LockOrientation(blink::WebScreenOrientationLockType orientation, | 65 void LockOrientation( |
68 blink::WebLockOrientationCallback* callback) { | 66 blink::WebScreenOrientationLockType orientation, |
69 dispatcher_->lockOrientation(orientation, callback); | 67 std::unique_ptr<blink::WebLockOrientationCallback> callback) { |
| 68 dispatcher_->lockOrientation(orientation, std::move(callback)); |
70 } | 69 } |
71 | 70 |
72 void UnlockOrientation() { dispatcher_->unlockOrientation(); } | 71 void UnlockOrientation() { dispatcher_->unlockOrientation(); } |
73 | 72 |
74 int GetRequestId() { return dispatcher_->GetRequestIdForTests(); } | 73 int GetRequestId() { return dispatcher_->GetRequestIdForTests(); } |
75 | 74 |
76 void RunLockResultCallback(int request_id, LockResult result) { | 75 void RunLockResultCallback(int request_id, LockResult result) { |
77 dispatcher_->OnLockOrientationResult(request_id, result); | 76 dispatcher_->OnLockOrientationResult(request_id, result); |
78 } | 77 } |
79 | 78 |
80 std::unique_ptr<ScreenOrientationDispatcher> dispatcher_; | 79 std::unique_ptr<ScreenOrientationDispatcher> dispatcher_; |
81 }; | 80 }; |
82 | 81 |
83 // Test that calling lockOrientation() followed by unlockOrientation() cancel | 82 // Test that calling lockOrientation() followed by unlockOrientation() cancel |
84 // the lockOrientation(). | 83 // the lockOrientation(). |
85 TEST_F(ScreenOrientationDispatcherTest, CancelPending_Unlocking) { | 84 TEST_F(ScreenOrientationDispatcherTest, CancelPending_Unlocking) { |
86 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 85 MockLockOrientationCallback::LockOrientationResultHolder callback_results; |
87 | 86 |
88 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 87 LockOrientation( |
89 new MockLockOrientationCallback(&callback_results)); | 88 blink::WebScreenOrientationLockPortraitPrimary, |
| 89 base::MakeUnique<MockLockOrientationCallback>(&callback_results)); |
90 UnlockOrientation(); | 90 UnlockOrientation(); |
91 | 91 |
92 EXPECT_FALSE(callback_results.succeeded_); | 92 EXPECT_FALSE(callback_results.succeeded_); |
93 EXPECT_TRUE(callback_results.failed_); | 93 EXPECT_TRUE(callback_results.failed_); |
94 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results.error_); | 94 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results.error_); |
95 } | 95 } |
96 | 96 |
97 // Test that calling lockOrientation() twice cancel the first lockOrientation(). | 97 // Test that calling lockOrientation() twice cancel the first lockOrientation(). |
98 TEST_F(ScreenOrientationDispatcherTest, CancelPending_DoubleLock) { | 98 TEST_F(ScreenOrientationDispatcherTest, CancelPending_DoubleLock) { |
99 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 99 MockLockOrientationCallback::LockOrientationResultHolder callback_results; |
100 // 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. |
101 MockLockOrientationCallback::LockOrientationResultHolder callback_results2; | 101 MockLockOrientationCallback::LockOrientationResultHolder callback_results2; |
102 | 102 |
103 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 103 LockOrientation( |
104 new MockLockOrientationCallback(&callback_results)); | 104 blink::WebScreenOrientationLockPortraitPrimary, |
| 105 base::MakeUnique<MockLockOrientationCallback>(&callback_results)); |
105 | 106 |
106 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 107 LockOrientation( |
107 new MockLockOrientationCallback(&callback_results2)); | 108 blink::WebScreenOrientationLockPortraitPrimary, |
| 109 base::MakeUnique<MockLockOrientationCallback>(&callback_results2)); |
108 | 110 |
109 EXPECT_FALSE(callback_results.succeeded_); | 111 EXPECT_FALSE(callback_results.succeeded_); |
110 EXPECT_TRUE(callback_results.failed_); | 112 EXPECT_TRUE(callback_results.failed_); |
111 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results.error_); | 113 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results.error_); |
112 } | 114 } |
113 | 115 |
114 // Test that when a LockError message is received, the request is set as failed | 116 // Test that when a LockError message is received, the request is set as failed |
115 // with the correct values. | 117 // with the correct values. |
116 TEST_F(ScreenOrientationDispatcherTest, LockRequest_Error) { | 118 TEST_F(ScreenOrientationDispatcherTest, LockRequest_Error) { |
117 std::map<LockResult, blink::WebLockOrientationError> errors; | 119 std::map<LockResult, blink::WebLockOrientationError> errors; |
118 errors[LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE] = | 120 errors[LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE] = |
119 blink::WebLockOrientationErrorNotAvailable; | 121 blink::WebLockOrientationErrorNotAvailable; |
120 errors[LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED] = | 122 errors[LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED] = |
121 blink::WebLockOrientationErrorFullscreenRequired; | 123 blink::WebLockOrientationErrorFullscreenRequired; |
122 errors[LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED] = | 124 errors[LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED] = |
123 blink::WebLockOrientationErrorCanceled; | 125 blink::WebLockOrientationErrorCanceled; |
124 | 126 |
125 for (std::map<LockResult, blink::WebLockOrientationError>::const_iterator it = | 127 for (std::map<LockResult, blink::WebLockOrientationError>::const_iterator it = |
126 errors.begin(); | 128 errors.begin(); |
127 it != errors.end(); ++it) { | 129 it != errors.end(); ++it) { |
128 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 130 MockLockOrientationCallback::LockOrientationResultHolder callback_results; |
129 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 131 LockOrientation( |
130 new MockLockOrientationCallback(&callback_results)); | 132 blink::WebScreenOrientationLockPortraitPrimary, |
| 133 base::MakeUnique<MockLockOrientationCallback>(&callback_results)); |
131 RunLockResultCallback(GetRequestId(), it->first); | 134 RunLockResultCallback(GetRequestId(), it->first); |
132 EXPECT_FALSE(callback_results.succeeded_); | 135 EXPECT_FALSE(callback_results.succeeded_); |
133 EXPECT_TRUE(callback_results.failed_); | 136 EXPECT_TRUE(callback_results.failed_); |
134 EXPECT_EQ(it->second, callback_results.error_); | 137 EXPECT_EQ(it->second, callback_results.error_); |
135 } | 138 } |
136 } | 139 } |
137 | 140 |
138 // Test that when a LockSuccess message is received, the request is set as | 141 // Test that when a LockSuccess message is received, the request is set as |
139 // succeeded. | 142 // succeeded. |
140 TEST_F(ScreenOrientationDispatcherTest, LockRequest_Success) { | 143 TEST_F(ScreenOrientationDispatcherTest, LockRequest_Success) { |
141 MockLockOrientationCallback::LockOrientationResultHolder callback_results; | 144 MockLockOrientationCallback::LockOrientationResultHolder callback_results; |
142 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 145 LockOrientation( |
143 new MockLockOrientationCallback(&callback_results)); | 146 blink::WebScreenOrientationLockPortraitPrimary, |
| 147 base::MakeUnique<MockLockOrientationCallback>(&callback_results)); |
144 | 148 |
145 RunLockResultCallback(GetRequestId(), | 149 RunLockResultCallback(GetRequestId(), |
146 LockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); | 150 LockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); |
147 | 151 |
148 EXPECT_TRUE(callback_results.succeeded_); | 152 EXPECT_TRUE(callback_results.succeeded_); |
149 EXPECT_FALSE(callback_results.failed_); | 153 EXPECT_FALSE(callback_results.failed_); |
150 } | 154 } |
151 | 155 |
152 // Test the following scenario: | 156 // Test the following scenario: |
153 // - request1 is received by the dispatcher; | 157 // - request1 is received by the dispatcher; |
154 // - request2 is received by the dispatcher; | 158 // - request2 is received by the dispatcher; |
155 // - request1 is rejected; | 159 // - request1 is rejected; |
156 // - request1 success response is received. | 160 // - request1 success response is received. |
157 // Expected: request1 is still rejected, request2 has not been set as succeeded. | 161 // Expected: request1 is still rejected, request2 has not been set as succeeded. |
158 TEST_F(ScreenOrientationDispatcherTest, RaceScenario) { | 162 TEST_F(ScreenOrientationDispatcherTest, RaceScenario) { |
159 MockLockOrientationCallback::LockOrientationResultHolder callback_results1; | 163 MockLockOrientationCallback::LockOrientationResultHolder callback_results1; |
160 MockLockOrientationCallback::LockOrientationResultHolder callback_results2; | 164 MockLockOrientationCallback::LockOrientationResultHolder callback_results2; |
161 | 165 |
162 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary, | 166 LockOrientation( |
163 new MockLockOrientationCallback(&callback_results1)); | 167 blink::WebScreenOrientationLockPortraitPrimary, |
| 168 base::MakeUnique<MockLockOrientationCallback>(&callback_results1)); |
164 int request_id1 = GetRequestId(); | 169 int request_id1 = GetRequestId(); |
165 | 170 |
166 LockOrientation(blink::WebScreenOrientationLockLandscapePrimary, | 171 LockOrientation( |
167 new MockLockOrientationCallback(&callback_results2)); | 172 blink::WebScreenOrientationLockLandscapePrimary, |
| 173 base::MakeUnique<MockLockOrientationCallback>(&callback_results2)); |
168 | 174 |
169 // callback_results1 must be rejected, tested in CancelPending_DoubleLock. | 175 // callback_results1 must be rejected, tested in CancelPending_DoubleLock. |
170 | 176 |
171 RunLockResultCallback(request_id1, | 177 RunLockResultCallback(request_id1, |
172 LockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); | 178 LockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS); |
173 | 179 |
174 // First request is still rejected. | 180 // First request is still rejected. |
175 EXPECT_FALSE(callback_results1.succeeded_); | 181 EXPECT_FALSE(callback_results1.succeeded_); |
176 EXPECT_TRUE(callback_results1.failed_); | 182 EXPECT_TRUE(callback_results1.failed_); |
177 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results1.error_); | 183 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results1.error_); |
178 | 184 |
179 // Second request is still pending. | 185 // Second request is still pending. |
180 EXPECT_FALSE(callback_results2.succeeded_); | 186 EXPECT_FALSE(callback_results2.succeeded_); |
181 EXPECT_FALSE(callback_results2.failed_); | 187 EXPECT_FALSE(callback_results2.failed_); |
182 } | 188 } |
183 | 189 |
184 } // namespace content | 190 } // namespace content |
OLD | NEW |