File: screen_orientation_dispatcher_unittest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (244 lines) | stat: -rw-r--r-- 9,005 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "screen_orientation_dispatcher.h"

#include <list>

#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "content/common/screen_orientation_messages.h"
#include "content/public/test/test_utils.h"
#include "ipc/ipc_test_sink.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/public/platform/WebLockOrientationCallback.h"

namespace content {

// MockLockOrientationCallback is an implementation of
// WebLockOrientationCallback and takes a LockOrientationResultHolder* as a
// parameter when being constructed. The |results_| pointer is owned by the
// caller and not by the callback object. The intent being that as soon as the
// callback is resolved, it will be killed so we use the
// LockOrientationResultHolder to know in which state the callback object is at
// any time.
class MockLockOrientationCallback :
    public blink::WebLockOrientationCallback {
 public:
  struct LockOrientationResultHolder {
    LockOrientationResultHolder()
        : succeeded_(false), failed_(false) {}

    bool succeeded_;
    bool failed_;
    blink::WebLockOrientationError error_;
  };

  explicit MockLockOrientationCallback(LockOrientationResultHolder* results)
      : results_(results) {}

  virtual void onSuccess() {
    results_->succeeded_ = true;
  }

  virtual void onError(blink::WebLockOrientationError error) {
    results_->failed_ = true;
    results_->error_ = error;
  }

 private:
  virtual ~MockLockOrientationCallback() {}

  LockOrientationResultHolder* results_;
};

class ScreenOrientationDispatcherWithSink : public ScreenOrientationDispatcher {
 public:
  explicit ScreenOrientationDispatcherWithSink(IPC::TestSink* sink)
      :ScreenOrientationDispatcher(NULL) , sink_(sink) {
  }

  bool Send(IPC::Message* message) override { return sink_->Send(message); }

  IPC::TestSink* sink_;
};

class ScreenOrientationDispatcherTest : public testing::Test {
 protected:
  void SetUp() override {
    dispatcher_.reset(new ScreenOrientationDispatcherWithSink(&sink_));
  }

  int GetFirstLockRequestIdFromSink() {
    const IPC::Message* msg = sink().GetFirstMessageMatching(
        ScreenOrientationHostMsg_LockRequest::ID);
    EXPECT_TRUE(msg != NULL);

    Tuple<blink::WebScreenOrientationLockType,int> params;
    ScreenOrientationHostMsg_LockRequest::Read(msg, &params);
    return get<1>(params);
  }

  IPC::TestSink& sink() {
    return sink_;
  }

  void LockOrientation(blink::WebScreenOrientationLockType orientation,
                       blink::WebLockOrientationCallback* callback) {
    dispatcher_->lockOrientation(orientation, callback);
  }

  void UnlockOrientation() {
    dispatcher_->unlockOrientation();
  }

  void OnMessageReceived(const IPC::Message& message) {
    dispatcher_->OnMessageReceived(message);
  }

  int routing_id() const {
    // We return a fake routing_id() in the context of this test.
    return 0;
  }

  IPC::TestSink sink_;
  scoped_ptr<ScreenOrientationDispatcher> dispatcher_;
};

// Test that calling lockOrientation() followed by unlockOrientation() cancel
// the lockOrientation().
TEST_F(ScreenOrientationDispatcherTest, CancelPending_Unlocking) {
  MockLockOrientationCallback::LockOrientationResultHolder callback_results;
  LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
                  new MockLockOrientationCallback(&callback_results));
  UnlockOrientation();

  EXPECT_FALSE(callback_results.succeeded_);
  EXPECT_TRUE(callback_results.failed_);
  EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results.error_);
}

// Test that calling lockOrientation() twice cancel the first lockOrientation().
TEST_F(ScreenOrientationDispatcherTest, CancelPending_DoubleLock) {
  MockLockOrientationCallback::LockOrientationResultHolder callback_results;
  // We create the object to prevent leaks but never actually use it.
  MockLockOrientationCallback::LockOrientationResultHolder callback_results2;

  LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
                  new MockLockOrientationCallback(&callback_results));
  LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
                  new MockLockOrientationCallback(&callback_results2));

  EXPECT_FALSE(callback_results.succeeded_);
  EXPECT_TRUE(callback_results.failed_);
  EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results.error_);
}

// Test that when a LockError message is received, the request is set as failed
// with the correct values.
TEST_F(ScreenOrientationDispatcherTest, LockRequest_Error) {
  std::list<blink::WebLockOrientationError> errors;
  errors.push_back(blink::WebLockOrientationErrorNotAvailable);
  errors.push_back(
      blink::WebLockOrientationErrorFullScreenRequired);
  errors.push_back(blink::WebLockOrientationErrorCanceled);

  for (std::list<blink::WebLockOrientationError>::const_iterator
          it = errors.begin(); it != errors.end(); ++it) {
    MockLockOrientationCallback::LockOrientationResultHolder callback_results;
    LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
                    new MockLockOrientationCallback(&callback_results));

    int request_id = GetFirstLockRequestIdFromSink();
    OnMessageReceived(
        ScreenOrientationMsg_LockError(routing_id(), request_id, *it));

    EXPECT_FALSE(callback_results.succeeded_);
    EXPECT_TRUE(callback_results.failed_);
    EXPECT_EQ(*it, callback_results.error_);

    sink().ClearMessages();
  }
}

// Test that when a LockSuccess message is received, the request is set as
// succeeded.
TEST_F(ScreenOrientationDispatcherTest, LockRequest_Success) {
  MockLockOrientationCallback::LockOrientationResultHolder callback_results;
  LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
                  new MockLockOrientationCallback(&callback_results));

  int request_id = GetFirstLockRequestIdFromSink();
  OnMessageReceived(ScreenOrientationMsg_LockSuccess(routing_id(),
                                                     request_id));

  EXPECT_TRUE(callback_results.succeeded_);
  EXPECT_FALSE(callback_results.failed_);

  sink().ClearMessages();
}

// Test an edge case: a LockSuccess is received but it matches no pending
// callback.
TEST_F(ScreenOrientationDispatcherTest, SuccessForUnknownRequest) {
  MockLockOrientationCallback::LockOrientationResultHolder callback_results;
  LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
                  new MockLockOrientationCallback(&callback_results));

  int request_id = GetFirstLockRequestIdFromSink();
  OnMessageReceived(ScreenOrientationMsg_LockSuccess(routing_id(),
                                                     request_id + 1));

  EXPECT_FALSE(callback_results.succeeded_);
  EXPECT_FALSE(callback_results.failed_);
}

// Test an edge case: a LockError is received but it matches no pending
// callback.
TEST_F(ScreenOrientationDispatcherTest, ErrorForUnknownRequest) {
  MockLockOrientationCallback::LockOrientationResultHolder callback_results;
  LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
                  new MockLockOrientationCallback(&callback_results));

  int request_id = GetFirstLockRequestIdFromSink();
  OnMessageReceived(ScreenOrientationMsg_LockError(
      routing_id(), request_id + 1, blink::WebLockOrientationErrorCanceled));

  EXPECT_FALSE(callback_results.succeeded_);
  EXPECT_FALSE(callback_results.failed_);
}

// Test the following scenario:
// - request1 is received by the dispatcher;
// - request2 is received by the dispatcher;
// - request1 is rejected;
// - request1 success response is received.
// Expected: request1 is still rejected, request2 has not been set as succeeded.
TEST_F(ScreenOrientationDispatcherTest, RaceScenario) {
  MockLockOrientationCallback::LockOrientationResultHolder callback_results1;
  MockLockOrientationCallback::LockOrientationResultHolder callback_results2;

  LockOrientation(blink::WebScreenOrientationLockPortraitPrimary,
                  new MockLockOrientationCallback(&callback_results1));
  int request_id1 = GetFirstLockRequestIdFromSink();

  LockOrientation(blink::WebScreenOrientationLockLandscapePrimary,
                  new MockLockOrientationCallback(&callback_results2));

  // callback_results1 must be rejected, tested in CancelPending_DoubleLock.

  OnMessageReceived(ScreenOrientationMsg_LockSuccess(routing_id(),
                                                     request_id1));

  // First request is still rejected.
  EXPECT_FALSE(callback_results1.succeeded_);
  EXPECT_TRUE(callback_results1.failed_);
  EXPECT_EQ(blink::WebLockOrientationErrorCanceled, callback_results1.error_);

  // Second request is still pending.
  EXPECT_FALSE(callback_results2.succeeded_);
  EXPECT_FALSE(callback_results2.failed_);
}

}  // namespace content