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

content / browser / screen_details / screen_details_browsertest.cc [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "content/browser/permissions/permission_controller_impl.h"
#include "content/browser/screen_details/screen_details_test_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/test_utils.h"
#include "content/shell/browser/shell.h"
#include "third_party/blink/public/common/permissions/permission_utils.h"
#include "ui/display/screen_base.h"

namespace content {

using ScreenDetailsTest = ContentBrowserTest;

// Test ScreenDetails API promise rejection without permission.
IN_PROC_BROWSER_TEST_F(ScreenDetailsTest, GetScreensNoPermission) {
  ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(nullptr, "empty.html")));
  ASSERT_EQ(true, EvalJs(shell(), "'getScreenDetails' in self"));
  // getScreenDetails() rejects its promise without permission.
  EXPECT_THAT(EvalJs(shell(), "await getScreenDetails()"),
              EvalJsResult::IsError());
}

// TODO(crbug.com/40145721): Test ScreenDetails API values with permission.
IN_PROC_BROWSER_TEST_F(ScreenDetailsTest, DISABLED_GetScreensBasic) {
  ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(nullptr, "empty.html")));
  ASSERT_EQ(true, EvalJs(shell(), "'getScreenDetails' in self"));
  auto result = EvalJs(shell(), content::test::kGetScreenDetailsScript);
  EXPECT_EQ(content::test::GetExpectedScreenDetails(), result.value);
}

// Test that screen.isExtended matches the availability of multiple displays.
IN_PROC_BROWSER_TEST_F(ScreenDetailsTest, IsExtendedBasic) {
  ASSERT_TRUE(NavigateToURL(shell(), GetTestUrl(nullptr, "empty.html")));
  ASSERT_EQ(true, EvalJs(shell(), "'isExtended' in screen"));
  EXPECT_EQ("boolean", EvalJs(shell(), "typeof screen.isExtended"));
  EXPECT_EQ(display::Screen::GetScreen()->GetNumDisplays() > 1,
            EvalJs(shell(), "screen.isExtended"));
}

// Test ScreenDetails functionality with a fake display::Screen object.
class FakeScreenDetailsTest : public ScreenDetailsTest {
 public:
  FakeScreenDetailsTest() = default;
  ~FakeScreenDetailsTest() override = default;
  FakeScreenDetailsTest(const FakeScreenDetailsTest&) = delete;
  void operator=(const FakeScreenDetailsTest&) = delete;

 protected:
  // ScreenDetailsTest:
  void SetUp() override {
    display::Screen::SetScreenInstance(&screen_);
    screen()->display_list().AddDisplay({0, gfx::Rect(100, 100, 801, 802)},
                                        display::DisplayList::Type::PRIMARY);

    ScreenDetailsTest::SetUp();
  }
  void TearDown() override {
    ScreenDetailsTest::TearDown();
    display::Screen::SetScreenInstance(nullptr);
  }

  void SetUpOnMainThread() override {
    ScreenDetailsTest::SetUpOnMainThread();
    // Create a shell that observes the fake screen.
    test_shell_ = CreateBrowser();
  }

  void TearDownOnMainThread() override {
    test_shell_ = nullptr;
    ScreenDetailsTest::TearDownOnMainThread();
  }

  display::ScreenBase* screen() { return &screen_; }
  Shell* test_shell() { return test_shell_; }

 private:
  display::ScreenBase screen_;
  raw_ptr<Shell> test_shell_ = nullptr;
};

// TODO(crbug.com/40115071): Windows crashes static casting to ScreenWin.
// TODO(crbug.com/40115071): Android requires a GetDisplayNearestView overload.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN)
#define MAYBE_GetScreensFaked DISABLED_GetScreensFaked
#else
#define MAYBE_GetScreensFaked GetScreensFaked
#endif
IN_PROC_BROWSER_TEST_F(FakeScreenDetailsTest, MAYBE_GetScreensFaked) {
  ASSERT_TRUE(NavigateToURL(test_shell(), GetTestUrl(nullptr, "empty.html")));
  ASSERT_EQ(true, EvalJs(test_shell(), "'getScreenDetails' in self"));

  content::WebContents* contents = test_shell()->web_contents();
  PermissionControllerImpl* permission_controller =
      PermissionControllerImpl::FromBrowserContext(
          contents->GetBrowserContext());
  permission_controller->GrantPermissionOverrides(
      contents->GetPrimaryMainFrame()->GetLastCommittedOrigin(),
      {blink::PermissionType::WINDOW_MANAGEMENT});

  screen()->display_list().AddDisplay({1, gfx::Rect(100, 100, 801, 802)},
                                      display::DisplayList::Type::NOT_PRIMARY);
  display::Display display(2, gfx::Rect(901, 100, 801, 802));
  // Test that UTF-8 characters are correctly reflected in the JS output.
  display.set_label("Inbyggd skärm");
  screen()->display_list().AddDisplay(display,
                                      display::DisplayList::Type::NOT_PRIMARY);

  EvalJsResult result =
      EvalJs(test_shell(), content::test::kGetScreenDetailsScript);
  EXPECT_EQ(content::test::GetExpectedScreenDetails(), result.value);
}

// TODO(crbug.com/40115071): Windows crashes static casting to ScreenWin.
// TODO(crbug.com/40115071): Android requires a GetDisplayNearestView overload.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN)
#define MAYBE_IsExtendedFaked DISABLED_IsExtendedFaked
#else
#define MAYBE_IsExtendedFaked IsExtendedFaked
#endif
IN_PROC_BROWSER_TEST_F(FakeScreenDetailsTest, MAYBE_IsExtendedFaked) {
  ASSERT_TRUE(NavigateToURL(test_shell(), GetTestUrl(nullptr, "empty.html")));
  EXPECT_FALSE(EvalJs(test_shell(), "screen.isExtended").ExtractBool());

  screen()->display_list().AddDisplay({1, gfx::Rect(100, 100, 801, 802)},
                                      display::DisplayList::Type::NOT_PRIMARY);
  EXPECT_TRUE(EvalJs(test_shell(), "screen.isExtended").ExtractBool());

  screen()->display_list().RemoveDisplay(1);
  EXPECT_FALSE(EvalJs(test_shell(), "screen.isExtended").ExtractBool());
}

// TODO(crbug.com/40115071): Windows crashes static casting to ScreenWin.
// TODO(crbug.com/40115071): Android requires a GetDisplayNearestView overload.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN)
#define MAYBE_ScreenOnchangeNoPermission DISABLED_ScreenOnchangeNoPermission
#else
#define MAYBE_ScreenOnchangeNoPermission ScreenOnchangeNoPermission
#endif
// Sites with no permission only get an event if screen.isExtended changes.
// TODO(crbug.com/40145721): Need content_browsertests permission controls.
IN_PROC_BROWSER_TEST_F(FakeScreenDetailsTest,
                       MAYBE_ScreenOnchangeNoPermission) {
  ASSERT_TRUE(NavigateToURL(test_shell(), GetTestUrl(nullptr, "empty.html")));
  ASSERT_EQ(true, EvalJs(test_shell(), "'onchange' in screen"));
  constexpr char kSetScreenOnchange[] = R"(
    window.screen.onchange = function() { ++document.title; };
    document.title = 0;
  )";
  EXPECT_EQ(0, EvalJs(test_shell(), kSetScreenOnchange));
  EXPECT_FALSE(EvalJs(test_shell(), "screen.isExtended").ExtractBool());
  EXPECT_EQ("0", EvalJs(test_shell(), "document.title"));

  // screen.isExtended changes from false to true here, so an event is sent.
  EXPECT_FALSE(EvalJs(test_shell(), "screen.isExtended").ExtractBool());
  screen()->display_list().AddDisplay({1, gfx::Rect(100, 100, 801, 802)},
                                      display::DisplayList::Type::NOT_PRIMARY);
  EXPECT_TRUE(EvalJs(test_shell(), "screen.isExtended").ExtractBool());
  EXPECT_EQ("1", EvalJs(test_shell(), "document.title"));

  // screen.isExtended remains unchanged, so no event is sent.
  screen()->display_list().AddDisplay({2, gfx::Rect(901, 100, 801, 802)},
                                      display::DisplayList::Type::NOT_PRIMARY);
  EXPECT_TRUE(EvalJs(test_shell(), "screen.isExtended").ExtractBool());
  EXPECT_EQ("1", EvalJs(test_shell(), "document.title"));

  // screen.isExtended remains unchanged, so no event is sent.
  EXPECT_NE(0u, screen()->display_list().UpdateDisplay(
                    {2, gfx::Rect(902, 100, 801, 802)}));
  EXPECT_TRUE(EvalJs(test_shell(), "screen.isExtended").ExtractBool());
  EXPECT_EQ("1", EvalJs(test_shell(), "document.title"));

  // screen.isExtended remains unchanged, so no event is sent.
  screen()->display_list().RemoveDisplay(2);
  EXPECT_TRUE(EvalJs(test_shell(), "screen.isExtended").ExtractBool());
  EXPECT_EQ("1", EvalJs(test_shell(), "document.title"));

  // screen.isExtended changes from true to false here, so an event is sent.
  screen()->display_list().RemoveDisplay(1);
  EXPECT_FALSE(EvalJs(test_shell(), "screen.isExtended").ExtractBool());
  EXPECT_EQ("2", EvalJs(test_shell(), "document.title"));
}

// TODO(crbug.com/40115071): Windows crashes static casting to ScreenWin.
// TODO(crbug.com/40115071): Android requires a GetDisplayNearestView overload.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN)
#define MAYBE_ScreenOnChangeForIsExtended DISABLED_ScreenOnChangeForIsExtended
#else
#define MAYBE_ScreenOnChangeForIsExtended ScreenOnChangeForIsExtended
#endif
// Sites should get Screen.change events anytime Screen.isExtended changes.
IN_PROC_BROWSER_TEST_F(FakeScreenDetailsTest,
                       MAYBE_ScreenOnChangeForIsExtended) {
  ASSERT_TRUE(NavigateToURL(test_shell(), GetTestUrl(nullptr, "empty.html")));
  ASSERT_EQ(true, EvalJs(test_shell(), "'onchange' in screen"));
  constexpr char kSetScreenOnChange[] = R"(
    screen.onchange = function() { ++document.title; };
    document.title = 0;
  )";
  EXPECT_EQ(0, EvalJs(test_shell(), kSetScreenOnChange));
  EXPECT_EQ("0", EvalJs(test_shell(), "document.title"));

  // Screen.isExtended changes from false to true here, so an event is sent.
  EXPECT_FALSE(EvalJs(test_shell(), "screen.isExtended").ExtractBool());
  screen()->display_list().AddDisplay({1, gfx::Rect(100, 100, 801, 802)},
                                      display::DisplayList::Type::NOT_PRIMARY);
  EXPECT_TRUE(EvalJs(test_shell(), "screen.isExtended").ExtractBool());
  EXPECT_EQ("1", EvalJs(test_shell(), "document.title"));

  // The current Screen remains unchanged, so no event is sent.
  screen()->display_list().AddDisplay({2, gfx::Rect(901, 100, 801, 802)},
                                      display::DisplayList::Type::NOT_PRIMARY);
  EXPECT_TRUE(EvalJs(test_shell(), "screen.isExtended").ExtractBool());
  EXPECT_EQ("1", EvalJs(test_shell(), "document.title"));

  // The current Screen remains unchanged, so no event is sent.
  EXPECT_NE(0u, screen()->display_list().UpdateDisplay(
                    {2, gfx::Rect(902, 100, 801, 802)}));
  EXPECT_TRUE(EvalJs(test_shell(), "screen.isExtended").ExtractBool());
  EXPECT_EQ("1", EvalJs(test_shell(), "document.title"));

  // The current Screen remains unchanged, so no event is sent.
  screen()->display_list().RemoveDisplay(2);
  EXPECT_TRUE(EvalJs(test_shell(), "screen.isExtended").ExtractBool());
  EXPECT_EQ("1", EvalJs(test_shell(), "document.title"));

  // Screen.isExtended changes from true to false here, so an event is sent.
  screen()->display_list().RemoveDisplay(1);
  EXPECT_FALSE(EvalJs(test_shell(), "screen.isExtended").ExtractBool());
  EXPECT_EQ("2", EvalJs(test_shell(), "document.title"));
}

// TODO(crbug.com/40115071): Windows crashes static casting to ScreenWin.
// TODO(crbug.com/40115071): Android requires a GetDisplayNearestView overload.
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN)
#define MAYBE_ScreenOnChangeForAttributes DISABLED_ScreenOnChangeForAttributes
#else
#define MAYBE_ScreenOnChangeForAttributes ScreenOnChangeForAttributes
#endif
// Sites should get Screen.change events anytime other Screen attributes change.
IN_PROC_BROWSER_TEST_F(FakeScreenDetailsTest,
                       MAYBE_ScreenOnChangeForAttributes) {
  ASSERT_TRUE(NavigateToURL(test_shell(), GetTestUrl(nullptr, "empty.html")));
  ASSERT_EQ(true, EvalJs(test_shell(), "'onchange' in screen"));
  constexpr char kSetScreenOnChange[] = R"(
    screen.onchange = function() { ++document.title; };
    document.title = 0;
  )";
  EXPECT_EQ(0, EvalJs(test_shell(), kSetScreenOnChange));
  EXPECT_EQ("0", EvalJs(test_shell(), "document.title"));

  // An event is sent when Screen work area changes.
  // work_area translates into Screen.available_rect.
  display::Display display = screen()->display_list().displays()[0];
  display.set_work_area(gfx::Rect(101, 102, 903, 904));
  EXPECT_NE(0u, screen()->display_list().UpdateDisplay(display));
  EXPECT_EQ("1", EvalJs(test_shell(), "document.title"));

  // An event is sent when Screen scaling changes.
  display.set_device_scale_factor(display.device_scale_factor() * 2);
  EXPECT_NE(0u, screen()->display_list().UpdateDisplay(display));
  EXPECT_EQ("2", EvalJs(test_shell(), "document.title"));
}

}  // namespace content