summaryrefslogtreecommitdiff
path: root/chromium/content/browser/screen_orientation/screen_orientation_provider_unittest.cc
blob: 97e3b3ecf6a43c7c5dd7dc83e32416ac727d5030 (plain)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright 2017 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 "content/browser/screen_orientation/screen_orientation_provider.h"

#include "base/optional.h"
#include "base/run_loop.h"
#include "content/common/frame_messages.h"
#include "content/public/browser/screen_orientation_delegate.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/test/test_render_view_host.h"
#include "content/test/test_web_contents.h"
#include "third_party/blink/public/platform/modules/screen_orientation/web_screen_orientation_lock_type.h"

namespace content {

using device::mojom::ScreenOrientationLockResult;

namespace {

class FakeScreenOrientationDelegate : public ScreenOrientationDelegate {
 public:
  FakeScreenOrientationDelegate(bool supported, bool full_screen_required)
      : supported_(supported), full_screen_required_(full_screen_required) {
    ScreenOrientationProvider::SetDelegate(this);
  }

  ~FakeScreenOrientationDelegate() override = default;

  bool FullScreenRequired(WebContents* web_contents) override {
    return full_screen_required_;
  }

  bool ScreenOrientationProviderSupported() override { return supported_; }

  void Lock(WebContents* web_contents,
            blink::WebScreenOrientationLockType lock_orientation) override {
    lock_count_++;
  }

  void Unlock(WebContents* web_contents) override { unlock_count_++; }

  int lock_count() const { return lock_count_; }
  int unlock_count() const { return unlock_count_; }

 private:
  bool supported_ = true;
  bool full_screen_required_ = false;

  int lock_count_ = 0;
  int unlock_count_ = 0;

  DISALLOW_COPY_AND_ASSIGN(FakeScreenOrientationDelegate);
};

class FakeWebContentsDelegate : public WebContentsDelegate {
 public:
  FakeWebContentsDelegate() = default;
  ~FakeWebContentsDelegate() override = default;

  void EnterFullscreenModeForTab(WebContents* web_contents,
                                 const GURL& origin) override {
    fullscreened_contents_ = web_contents;
  }

  void ExitFullscreenModeForTab(WebContents* web_contents) override {
    fullscreened_contents_ = nullptr;
  }

  bool IsFullscreenForTabOrPending(
      const WebContents* web_contents) const override {
    return fullscreened_contents_ && web_contents == fullscreened_contents_;
  }

 private:
  WebContents* fullscreened_contents_ = nullptr;

  DISALLOW_COPY_AND_ASSIGN(FakeWebContentsDelegate);
};

void LockResultCallback(base::Optional<ScreenOrientationLockResult>* out_result,
                        ScreenOrientationLockResult result) {
  *out_result = result;
}

}  // namespace

class ScreenOrientationProviderTest : public RenderViewHostImplTestHarness {
 public:
  ScreenOrientationProviderTest() = default;

  void SetUp() override {
    content::RenderViewHostImplTestHarness::SetUp();

    contents()->SetDelegate(&wc_delegate_);
  }

  // Helpers for testing ScreenOrientationProvider methods.
  void CallLockAndGetResult(
      blink::WebScreenOrientationLockType orientation,
      base::Optional<ScreenOrientationLockResult>* out_result) {
    contents()->GetScreenOrientationProviderForTesting()->LockOrientation(
        orientation, base::BindOnce(&LockResultCallback, out_result));

    base::RunLoop().RunUntilIdle();
  }

  void CallUnlock() {
    contents()->GetScreenOrientationProviderForTesting()->UnlockOrientation();
  }

 private:
  FakeWebContentsDelegate wc_delegate_;
};

// Lock operation is not available.
TEST_F(ScreenOrientationProviderTest, DelegateNotAvailableLockOnce) {
  // No ScreenOrientationDelegate.
  base::Optional<ScreenOrientationLockResult> result_1;
  CallLockAndGetResult(blink::WebScreenOrientationLockType::
                           kWebScreenOrientationLockLandscapeSecondary,
                       &result_1);
  EXPECT_EQ(ScreenOrientationLockResult::
                SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE,
            *result_1);

  // ScreenOrientationDelegate not supported.
  FakeScreenOrientationDelegate delegate(false, false);
  base::Optional<ScreenOrientationLockResult> result_2;
  CallLockAndGetResult(blink::WebScreenOrientationLockType::
                           kWebScreenOrientationLockLandscapeSecondary,
                       &result_2);
  EXPECT_EQ(ScreenOrientationLockResult::
                SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE,
            *result_2);
}

// Full screen is not required by delegate, normally lock once.
TEST_F(ScreenOrientationProviderTest, DelegateLockOnce) {
  // ScreenOrientationDelegate does not require full screen.
  FakeScreenOrientationDelegate delegate(true, false);

  // Navigate to a site.
  const GURL url("http://www.google.com");
  controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED,
                       std::string());

  base::Optional<ScreenOrientationLockResult> result_1;
  CallLockAndGetResult(blink::WebScreenOrientationLockType::
                           kWebScreenOrientationLockLandscapeSecondary,
                       &result_1);
  // Lock request is pending.
  EXPECT_FALSE(result_1.has_value());
  // Delegate did apply lock once.
  EXPECT_EQ(1, delegate.lock_count());
}

// Full screen is required by delegate.
TEST_F(ScreenOrientationProviderTest, DelegateRequireFullScreenLockOnce) {
  // ScreenOrientationDelegate requires full screen.
  FakeScreenOrientationDelegate delegate(true, true);

  // Navigate to a site.
  const GURL url("http://www.google.com");
  controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED,
                       std::string());

  // Current web contents is not in full screen.
  ASSERT_FALSE(contents()->IsFullscreenForCurrentTab());
  base::Optional<ScreenOrientationLockResult> result_1;
  CallLockAndGetResult(blink::WebScreenOrientationLockType::
                           kWebScreenOrientationLockLandscapeSecondary,
                       &result_1);
  EXPECT_EQ(ScreenOrientationLockResult::
                SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED,
            *result_1);
  // Delegate did not apply any lock.
  EXPECT_EQ(0, delegate.lock_count());

  // Simulates entering full screen.
  main_test_rfh()->OnMessageReceived(
      FrameHostMsg_ToggleFullscreen(main_test_rfh()->GetRoutingID(), true));
  ASSERT_TRUE(contents()->IsFullscreenForCurrentTab());

  base::Optional<ScreenOrientationLockResult> result_2;
  CallLockAndGetResult(blink::WebScreenOrientationLockType::
                           kWebScreenOrientationLockLandscapeSecondary,
                       &result_2);
  // Lock request is pending.
  EXPECT_FALSE(result_2.has_value());
  // Delegate did apply lock once.
  EXPECT_EQ(1, delegate.lock_count());
}

// Lock once, then unlock once, the pending lock request will be cancelled.
TEST_F(ScreenOrientationProviderTest, DelegateLockThenUnlock) {
  FakeScreenOrientationDelegate delegate(true, false);

  // Navigate to a site.
  const GURL url("http://www.google.com");
  controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED,
                       std::string());

  base::Optional<ScreenOrientationLockResult> result_1;
  CallLockAndGetResult(blink::WebScreenOrientationLockType::
                           kWebScreenOrientationLockLandscapeSecondary,
                       &result_1);
  // The lock request will be pending.
  EXPECT_FALSE(result_1.has_value());
  // Delegate did apply lock once.
  EXPECT_EQ(1, delegate.lock_count());
  EXPECT_EQ(0, delegate.unlock_count());

  CallUnlock();
  // The pending lock request is cancelled.
  EXPECT_EQ(ScreenOrientationLockResult::
                SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED,
            result_1);
  // Delegate did apply unlock once.
  EXPECT_EQ(1, delegate.unlock_count());
}

// Lock twice, the first lock request will be cancelled by the second one.
TEST_F(ScreenOrientationProviderTest, DelegateLockThenLock) {
  FakeScreenOrientationDelegate delegate(true, false);

  // Navigate to a site.
  const GURL url("http://www.google.com");
  controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED,
                       std::string());

  base::Optional<ScreenOrientationLockResult> result_1;
  CallLockAndGetResult(blink::WebScreenOrientationLockType::
                           kWebScreenOrientationLockLandscapeSecondary,
                       &result_1);
  // The lock request will be pending.
  EXPECT_FALSE(result_1.has_value());
  // Delegate did apply lock once.
  EXPECT_EQ(1, delegate.lock_count());
  EXPECT_EQ(0, delegate.unlock_count());

  base::Optional<ScreenOrientationLockResult> result_2;
  CallLockAndGetResult(blink::WebScreenOrientationLockType::
                           kWebScreenOrientationLockLandscapeSecondary,
                       &result_2);
  // The pending lock request is cancelled.
  EXPECT_EQ(ScreenOrientationLockResult::
                SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED,
            result_1);
  // The second one became pending.
  EXPECT_FALSE(result_2.has_value());
  // Delegate did apply lock once more.
  EXPECT_EQ(2, delegate.lock_count());
  EXPECT_EQ(0, delegate.unlock_count());
}

// Unlock won't be applied if no lock has been applied previously.
TEST_F(ScreenOrientationProviderTest, NoUnlockWithoutLock) {
  FakeScreenOrientationDelegate delegate(true, false);

  // Navigate to a site.
  const GURL url("http://www.google.com");
  controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED,
                       std::string());

  CallUnlock();
  // Delegate did not apply any unlock.
  EXPECT_EQ(0, delegate.unlock_count());
}

// If lock already applied once in full screen, then unlock should be triggered
// once automatically when exiting full screen, and previous pending lock
// request will be cancelled.
TEST_F(ScreenOrientationProviderTest, UnlockWhenExitingFullScreen) {
  // ScreenOrientationDelegate requires full screen.
  FakeScreenOrientationDelegate delegate(true, true);

  // Navigate to a site.
  const GURL url("http://www.google.com");
  controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED,
                       std::string());

  // Simulates entering full screen.
  main_test_rfh()->OnMessageReceived(
      FrameHostMsg_ToggleFullscreen(main_test_rfh()->GetRoutingID(), true));
  ASSERT_TRUE(contents()->IsFullscreenForCurrentTab());

  base::Optional<ScreenOrientationLockResult> result;
  CallLockAndGetResult(blink::WebScreenOrientationLockType::
                           kWebScreenOrientationLockLandscapeSecondary,
                       &result);
  // The lock request will be pending.
  EXPECT_FALSE(result.has_value());
  // Delegate did apply lock once.
  EXPECT_EQ(1, delegate.lock_count());
  EXPECT_EQ(0, delegate.unlock_count());

  // Simulates exiting full screen.
  main_test_rfh()->OnMessageReceived(
      FrameHostMsg_ToggleFullscreen(main_test_rfh()->GetRoutingID(), false));
  ASSERT_FALSE(contents()->IsFullscreenForCurrentTab());
  // The pending lock request is cancelled.
  EXPECT_EQ(ScreenOrientationLockResult::
                SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED,
            result);
  // Delegate did apply unlock once.
  EXPECT_EQ(1, delegate.unlock_count());
}

// If lock already applied once, then unlock should be triggered once
// automatically when navigating to other web page, and previous pending lock
// request will be cancelled.
TEST_F(ScreenOrientationProviderTest, UnlockWhenNavigation) {
  FakeScreenOrientationDelegate delegate(true, false);

  // Navigate to a site.
  const GURL url("http://www.google.com");
  controller().LoadURL(url, Referrer(), ui::PAGE_TRANSITION_TYPED,
                       std::string());

  base::Optional<ScreenOrientationLockResult> result;
  CallLockAndGetResult(blink::WebScreenOrientationLockType::
                           kWebScreenOrientationLockLandscapeSecondary,
                       &result);
  // The lock request will be pending.
  EXPECT_FALSE(result.has_value());
  // Delegate did apply lock once.
  EXPECT_EQ(1, delegate.lock_count());
  EXPECT_EQ(0, delegate.unlock_count());

  // Navigate to another site.
  const GURL another_url("http://www.google.com/abc.html");
  contents()->NavigateAndCommit(another_url);
  // The pending lock request is cancelled.
  EXPECT_EQ(ScreenOrientationLockResult::
                SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED,
            result);
  // Delegate did apply unlock once.
  EXPECT_EQ(1, delegate.unlock_count());
}

}  // namespace content