summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/screen_orientation/screen_orientation_controller_test.cc
blob: 9aab24e0f954a35f2bf26e863595ea0141b4c13f (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
// 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 "third_party/blink/renderer/modules/screen_orientation/screen_orientation_controller.h"

#include <memory>

#include "mojo/public/cpp/bindings/associated_remote.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/web/web_local_frame.h"
#include "third_party/blink/renderer/core/frame/frame_test_helpers.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/web_frame_widget_impl.h"
#include "third_party/blink/renderer/core/frame/web_local_frame_impl.h"
#include "third_party/blink/renderer/core/testing/page_test_base.h"
#include "third_party/blink/renderer/modules/screen_orientation/screen_orientation.h"
#include "third_party/blink/renderer/modules/screen_orientation/web_lock_orientation_callback.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_associated_remote.h"
#include "third_party/blink/renderer/platform/testing/unit_test_helpers.h"
#include "third_party/blink/renderer/platform/testing/url_test_helpers.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"

namespace blink {

using LockOrientationCallback =
    device::mojom::blink::ScreenOrientation::LockOrientationCallback;
using LockResult = device::mojom::blink::ScreenOrientationLockResult;

// 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) {}

  void OnSuccess() override { results_->succeeded_ = true; }

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

 private:
  LockOrientationResultHolder* results_;
};

class ScreenOrientationControllerTest : public PageTestBase {
 protected:
  void SetUp() override {
    PageTestBase::SetUp(IntSize());
    HeapMojoAssociatedRemote<device::mojom::blink::ScreenOrientation>
        screen_orientation(GetFrame().DomWindow());
    ignore_result(screen_orientation.BindNewEndpointAndPassDedicatedReceiver());
    Controller()->SetScreenOrientationAssociatedRemoteForTests(
        std::move(screen_orientation));
  }

  void TearDown() override {
    HeapMojoAssociatedRemote<device::mojom::blink::ScreenOrientation>
        screen_orientation(GetFrame().DomWindow());
    Controller()->SetScreenOrientationAssociatedRemoteForTests(
        std::move(screen_orientation));
  }

  ScreenOrientationController* Controller() {
    return ScreenOrientationController::From(*GetFrame().DomWindow());
  }

  void LockOrientation(
      device::mojom::ScreenOrientationLockType orientation,
      std::unique_ptr<blink::WebLockOrientationCallback> callback) {
    Controller()->lock(orientation, std::move(callback));
  }

  void UnlockOrientation() { Controller()->unlock(); }

  int GetRequestId() { return Controller()->GetRequestIdForTests(); }

  void RunLockResultCallback(int request_id, LockResult result) {
    Controller()->OnLockOrientationResult(request_id, result);
  }
};

// Test that calling lockOrientation() followed by unlockOrientation() cancel
// the lockOrientation().
TEST_F(ScreenOrientationControllerTest, CancelPending_Unlocking) {
  MockLockOrientationCallback::LockOrientationResultHolder callback_results;

  LockOrientation(
      device::mojom::ScreenOrientationLockType::PORTRAIT_PRIMARY,
      std::make_unique<MockLockOrientationCallback>(&callback_results));
  UnlockOrientation();

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

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

  LockOrientation(
      device::mojom::ScreenOrientationLockType::PORTRAIT_PRIMARY,
      std::make_unique<MockLockOrientationCallback>(&callback_results));

  LockOrientation(
      device::mojom::ScreenOrientationLockType::PORTRAIT_PRIMARY,
      std::make_unique<MockLockOrientationCallback>(&callback_results2));

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

// Test that when a LockError message is received, the request is set as failed
// with the correct values.
TEST_F(ScreenOrientationControllerTest, LockRequest_Error) {
  HashMap<LockResult, blink::WebLockOrientationError, WTF::IntHash<LockResult>>
      errors;
  errors.insert(LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_NOT_AVAILABLE,
                blink::kWebLockOrientationErrorNotAvailable);
  errors.insert(
      LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_FULLSCREEN_REQUIRED,
      blink::kWebLockOrientationErrorFullscreenRequired);
  errors.insert(LockResult::SCREEN_ORIENTATION_LOCK_RESULT_ERROR_CANCELED,
                blink::kWebLockOrientationErrorCanceled);

  for (auto it = errors.begin(); it != errors.end(); ++it) {
    MockLockOrientationCallback::LockOrientationResultHolder callback_results;
    LockOrientation(
        device::mojom::ScreenOrientationLockType::PORTRAIT_PRIMARY,
        std::make_unique<MockLockOrientationCallback>(&callback_results));
    RunLockResultCallback(GetRequestId(), it->key);
    EXPECT_FALSE(callback_results.succeeded_);
    EXPECT_TRUE(callback_results.failed_);
    EXPECT_EQ(it->value, callback_results.error_);
  }
}

// Test that when a LockSuccess message is received, the request is set as
// succeeded.
TEST_F(ScreenOrientationControllerTest, LockRequest_Success) {
  MockLockOrientationCallback::LockOrientationResultHolder callback_results;
  LockOrientation(
      device::mojom::ScreenOrientationLockType::PORTRAIT_PRIMARY,
      std::make_unique<MockLockOrientationCallback>(&callback_results));

  RunLockResultCallback(GetRequestId(),
                        LockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS);

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

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

  LockOrientation(
      device::mojom::ScreenOrientationLockType::PORTRAIT_PRIMARY,
      std::make_unique<MockLockOrientationCallback>(&callback_results1));
  int request_id1 = GetRequestId();

  LockOrientation(
      device::mojom::ScreenOrientationLockType::LANDSCAPE_PRIMARY,
      std::make_unique<MockLockOrientationCallback>(&callback_results2));

  // callback_results1 must be rejected, tested in CancelPending_DoubleLock.

  RunLockResultCallback(request_id1,
                        LockResult::SCREEN_ORIENTATION_LOCK_RESULT_SUCCESS);

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

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

class ScreenInfoWebFrameWidget : public frame_test_helpers::TestWebFrameWidget {
 public:
  template <typename... Args>
  explicit ScreenInfoWebFrameWidget(Args&&... args)
      : frame_test_helpers::TestWebFrameWidget(std::forward<Args>(args)...) {
    screen_info_.orientation_angle = 1234;
  }
  ~ScreenInfoWebFrameWidget() override = default;

  // frame_test_helpers::TestWebFrameWidget overrides.
  ScreenInfo GetInitialScreenInfo() override { return screen_info_; }

 private:
  ScreenInfo screen_info_;
};

TEST_F(ScreenOrientationControllerTest, PageVisibilityCrash) {
  std::string base_url("http://internal.test/");
  std::string test_url("single_iframe.html");
  url_test_helpers::RegisterMockedURLLoadFromBase(
      WebString::FromUTF8(base_url), test::CoreTestDataPath(),
      WebString::FromUTF8(test_url));
  url_test_helpers::RegisterMockedURLLoadFromBase(
      WebString::FromUTF8(base_url), test::CoreTestDataPath(),
      WebString::FromUTF8("visible_iframe.html"));

  frame_test_helpers::CreateTestWebFrameWidgetCallback create_widget_callback =
      base::BindRepeating(
          &frame_test_helpers::WebViewHelper::CreateTestWebFrameWidget<
              ScreenInfoWebFrameWidget>);
  frame_test_helpers::WebViewHelper web_view_helper(create_widget_callback);
  web_view_helper.InitializeAndLoad(base_url + test_url, nullptr, nullptr);

  Page* page = web_view_helper.GetWebView()->GetPage();
  LocalFrame* frame = To<LocalFrame>(page->MainFrame());

  // Fully set up on an orientation and a controller in the main frame, but not
  // the iframe. Prepare an orientation change, then toggle visibility. When
  // set to visible, propagating the orientation change events shouldn't crash
  // just because the ScreenOrientationController in the iframe was never
  // referenced before this.
  ScreenOrientation::Create(frame->DomWindow());
  page->SetVisibilityState(mojom::blink::PageVisibilityState::kHidden, false);
  web_view_helper.LocalMainFrame()->SendOrientationChangeEvent();
  page->SetVisibilityState(mojom::blink::PageVisibilityState::kVisible, false);

  // When the iframe's orientation is initialized, it should be properly synced.
  auto* child_orientation = ScreenOrientation::Create(
      To<LocalFrame>(frame->Tree().FirstChild())->DomWindow());
  EXPECT_EQ(child_orientation->angle(), 1234);

  url_test_helpers::UnregisterAllURLsAndClearMemoryCache();
  web_view_helper.Reset();
}

TEST_F(ScreenOrientationControllerTest,
       OrientationChangePropagationToGrandchild) {
  std::string base_url("http://internal.test/");
  std::string test_url("page_with_grandchild.html");
  url_test_helpers::RegisterMockedURLLoadFromBase(
      WebString::FromUTF8(base_url), test::CoreTestDataPath(),
      WebString::FromUTF8(test_url));
  url_test_helpers::RegisterMockedURLLoadFromBase(
      WebString::FromUTF8(base_url), test::CoreTestDataPath(),
      WebString::FromUTF8("single_iframe.html"));
  url_test_helpers::RegisterMockedURLLoadFromBase(
      WebString::FromUTF8(base_url), test::CoreTestDataPath(),
      WebString::FromUTF8("visible_iframe.html"));

  frame_test_helpers::WebViewHelper web_view_helper;
  web_view_helper.InitializeAndLoad(base_url + test_url, nullptr, nullptr);

  Page* page = web_view_helper.GetWebView()->GetPage();
  LocalFrame* frame = To<LocalFrame>(page->MainFrame());

  // Fully set up on an orientation and a controller in the main frame and
  // the grandchild, but not the child.
  ScreenOrientation::Create(frame->DomWindow());
  Frame* grandchild = frame->Tree().FirstChild()->Tree().FirstChild();
  auto* grandchild_orientation =
      ScreenOrientation::Create(To<LocalFrame>(grandchild)->DomWindow());

  // Update the screen info and ensure it propagated to the grandchild.
  blink::ScreenInfo screen_info;
  screen_info.orientation_angle = 90;
  auto* web_frame_widget_base =
      static_cast<WebFrameWidgetImpl*>(frame->GetWidgetForLocalRoot());
  web_frame_widget_base->UpdateScreenInfo(screen_info);
  EXPECT_EQ(grandchild_orientation->angle(), 90);

  url_test_helpers::UnregisterAllURLsAndClearMemoryCache();
  web_view_helper.Reset();
}

}  // namespace blink