Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(46)

Side by Side Diff: content/renderer/screen_orientation/screen_orientation_dispatcher_unittest.cc

Issue 2391883006: Mojo-ify implementation of screen orientation locking/unlocking. (Closed)
Patch Set: clup Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "content/common/screen_orientation_messages.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"
17
18 namespace content {
19
20 // MockLockOrientationCallback is an implementation of
21 // WebLockOrientationCallback and takes a LockOrientationResultHolder* as a
22 // 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
24 // callback is resolved, it will be killed so we use the
25 // LockOrientationResultHolder to know in which state the callback object is at
26 // any time.
27 class MockLockOrientationCallback : public blink::WebLockOrientationCallback {
28 public:
29 struct LockOrientationResultHolder {
30 LockOrientationResultHolder()
31 : succeeded_(false), failed_(false) {}
32
33 bool succeeded_;
34 bool failed_;
35 blink::WebLockOrientationError error_;
36 };
37
38 explicit MockLockOrientationCallback(LockOrientationResultHolder* results)
39 : results_(results) {}
40
41 void onSuccess() override { results_->succeeded_ = true; }
42
43 void onError(blink::WebLockOrientationError error) override {
44 results_->failed_ = true;
45 results_->error_ = error;
46 }
47
48 private:
49 ~MockLockOrientationCallback() override {}
50
51 LockOrientationResultHolder* results_;
52 };
53
54 class ScreenOrientationDispatcherWithSink : public ScreenOrientationDispatcher {
55 public:
56 explicit ScreenOrientationDispatcherWithSink(IPC::TestSink* sink)
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:
67 void SetUp() override {
68 dispatcher_.reset(new ScreenOrientationDispatcherWithSink(&sink_));
69 }
70
71 int GetFirstLockRequestIdFromSink() {
72 const IPC::Message* msg = sink().GetFirstMessageMatching(
73 ScreenOrientationHostMsg_LockRequest::ID);
74 EXPECT_TRUE(msg != NULL);
75
76 std::tuple<blink::WebScreenOrientationLockType, int> params;
77 ScreenOrientationHostMsg_LockRequest::Read(msg, &params);
78 return std::get<1>(params);
79 }
80
81 IPC::TestSink& sink() {
82 return sink_;
83 }
84
85 void LockOrientation(blink::WebScreenOrientationLockType orientation,
86 blink::WebLockOrientationCallback* callback) {
87 dispatcher_->lockOrientation(orientation, callback);
88 }
89
90 void UnlockOrientation() {
91 dispatcher_->unlockOrientation();
92 }
93
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_;
105 };
106
107 // Test that calling lockOrientation() followed by unlockOrientation() cancel
108 // the lockOrientation().
109 TEST_F(ScreenOrientationDispatcherTest, CancelPending_Unlocking) {
110 MockLockOrientationCallback::LockOrientationResultHolder callback_results;
111 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
112 new MockLockOrientationCallback(&callback_results));
113 UnlockOrientation();
114
115 EXPECT_FALSE(callback_results.succeeded_);
116 EXPECT_TRUE(callback_results.failed_);
117 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results.error_);
118 }
119
120 // Test that calling lockOrientation() twice cancel the first lockOrientation().
121 TEST_F(ScreenOrientationDispatcherTest, CancelPending_DoubleLock) {
122 MockLockOrientationCallback::LockOrientationResultHolder callback_results;
123 // We create the object to prevent leaks but never actually use it.
124 MockLockOrientationCallback::LockOrientationResultHolder callback_results2;
125
126 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
127 new MockLockOrientationCallback(&callback_results));
128 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
129 new MockLockOrientationCallback(&callback_results2));
130
131 EXPECT_FALSE(callback_results.succeeded_);
132 EXPECT_TRUE(callback_results.failed_);
133 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results.error_);
134 }
135
136 // Test that when a LockError message is received, the request is set as failed
137 // with the correct values.
138 TEST_F(ScreenOrientationDispatcherTest, LockRequest_Error) {
139 std::list<blink::WebLockOrientationError> errors;
140 errors.push_back(blink::WebLockOrientationErrorNotAvailable);
141 errors.push_back(
142 blink::WebLockOrientationErrorFullscreenRequired);
143 errors.push_back(blink::WebLockOrientationErrorCanceled);
144
145 for (std::list<blink::WebLockOrientationError>::const_iterator
146 it = errors.begin(); it != errors.end(); ++it) {
147 MockLockOrientationCallback::LockOrientationResultHolder callback_results;
148 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
149 new MockLockOrientationCallback(&callback_results));
150
151 int request_id = GetFirstLockRequestIdFromSink();
152 OnMessageReceived(
153 ScreenOrientationMsg_LockError(routing_id(), request_id, *it));
154
155 EXPECT_FALSE(callback_results.succeeded_);
156 EXPECT_TRUE(callback_results.failed_);
157 EXPECT_EQ(*it, callback_results.error_);
158
159 sink().ClearMessages();
160 }
161 }
162
163 // Test that when a LockSuccess message is received, the request is set as
164 // succeeded.
165 TEST_F(ScreenOrientationDispatcherTest, LockRequest_Success) {
166 MockLockOrientationCallback::LockOrientationResultHolder callback_results;
167 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
168 new MockLockOrientationCallback(&callback_results));
169
170 int request_id = GetFirstLockRequestIdFromSink();
171 OnMessageReceived(ScreenOrientationMsg_LockSuccess(routing_id(),
172 request_id));
173
174 EXPECT_TRUE(callback_results.succeeded_);
175 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 }
209
210 // Test the following scenario:
211 // - request1 is received by the dispatcher;
212 // - request2 is received by the dispatcher;
213 // - request1 is rejected;
214 // - request1 success response is received.
215 // Expected: request1 is still rejected, request2 has not been set as succeeded.
216 TEST_F(ScreenOrientationDispatcherTest, RaceScenario) {
217 MockLockOrientationCallback::LockOrientationResultHolder callback_results1;
218 MockLockOrientationCallback::LockOrientationResultHolder callback_results2;
219
220 LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
221 new MockLockOrientationCallback(&callback_results1));
222 int request_id1 = GetFirstLockRequestIdFromSink();
223
224 LockOrientation(blink::WebScreenOrientationLockLandscapePrimary,
225 new MockLockOrientationCallback(&callback_results2));
226
227 // callback_results1 must be rejected, tested in CancelPending_DoubleLock.
228
229 OnMessageReceived(ScreenOrientationMsg_LockSuccess(routing_id(),
230 request_id1));
231
232 // First request is still rejected.
233 EXPECT_FALSE(callback_results1.succeeded_);
234 EXPECT_TRUE(callback_results1.failed_);
235 EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results1.error_);
236
237 // Second request is still pending.
238 EXPECT_FALSE(callback_results2.succeeded_);
239 EXPECT_FALSE(callback_results2.failed_);
240 }
241
242 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/screen_orientation/screen_orientation_dispatcher_browsertest.cc ('k') | content/test/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698