File: window_management_printing_interactive_uitest.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (146 lines) | stat: -rw-r--r-- 6,228 bytes parent folder | download | duplicates (3)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "build/build_config.h"
#include "chrome/browser/printing/print_preview_dialog_controller.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/permissions/permission_request_manager.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "ui/display/screen_base.h"
#include "ui/display/test/test_screen.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "ash/shell.h"
#include "ui/display/test/display_manager_test_api.h"  // nogncheck
#endif                                                 // BUILDFLAG(IS_CHROMEOS)

class WindowManagementTest : public InProcessBrowserTest {
 public:
  void SetUp() override {
#if !BUILDFLAG(IS_CHROMEOS)
    display::Screen::SetScreenInstance(&screen_);
    screen_.display_list().AddDisplay({1, gfx::Rect(0, 0, 803, 600)},
                                      display::DisplayList::Type::PRIMARY);
#endif
    InProcessBrowserTest::SetUp();
  }

  void SetUpOnMainThread() override {
    // Window management features are only available on secure contexts.
    https_test_server_ = std::make_unique<net::EmbeddedTestServer>(
        net::EmbeddedTestServer::TYPE_HTTPS);
    https_test_server_->ServeFilesFromSourceDirectory(GetChromeTestDataDir());
    ASSERT_TRUE(https_test_server_->Start());
  }

  void TearDown() override {
    InProcessBrowserTest::TearDown();
#if !BUILDFLAG(IS_CHROMEOS)
    display::Screen::SetScreenInstance(nullptr);
#endif
  }

 protected:
  std::unique_ptr<net::EmbeddedTestServer> https_test_server_;
#if !BUILDFLAG(IS_CHROMEOS)
  display::ScreenBase screen_;
#endif
};

// TODO(crbug.com/40115071): Windows crashes static casting to ScreenWin.
#if BUILDFLAG(IS_WIN)
#define MAYBE_NoCrashOnEventsDuringHandlerPrint \
  DISABLED_NoCrashOnEventsDuringHandlerPrint
#else
#define MAYBE_NoCrashOnEventsDuringHandlerPrint \
  NoCrashOnEventsDuringHandlerPrint
#endif
// Test that screen change events occurring while an event handler is running
// a nested event loop (i.e. via window.print()) do not cause a crash.
// Regression test for crbug.com/1273841
IN_PROC_BROWSER_TEST_F(WindowManagementTest,
                       MAYBE_NoCrashOnEventsDuringHandlerPrint) {
  // Update the display configuration to mock display changes.
#if BUILDFLAG(IS_CHROMEOS)
  display::test::DisplayManagerTestApi(ash::Shell::Get()->display_manager())
      .UpdateDisplay("0+0-803x600");
#endif  // BUILDFLAG(IS_CHROMEOS)
  ASSERT_EQ(1, display::Screen::GetScreen()->GetNumDisplays());

  // Navigate in a new tab to observe the test screen instance as needed.
  const GURL url(https_test_server_->GetURL("/simple.html"));
  ui_test_utils::NavigateToURLWithDisposition(
      browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
      ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB |
          ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);

  // Auto-accept the Window Placement permission request.
  auto* tab = browser()->tab_strip_model()->GetActiveWebContents();
  permissions::PermissionRequestManager* permission_request_manager_tab =
      permissions::PermissionRequestManager::FromWebContents(tab);
  permission_request_manager_tab->set_auto_response_for_test(
      permissions::PermissionRequestManager::ACCEPT_ALL);

  // Add a currentscreenchange event handler that will freeze JS via print().
  auto* script = R"(
      function freezeJS(e) {
        self.print();
        screenDetails.removeEventListener('currentscreenchange', freezeJS);
      }

      let screenDetails;
      self.getScreenDetails().then(s => {
        screenDetails = s;
        screenDetails.addEventListener('currentscreenchange', freezeJS);
      });
  )";
  content::WebContentsAddedObserver web_contents_added_observer;
  ASSERT_TRUE(EvalJs(tab, script).error.empty());

  // Alter the display to trigger the currentscreenchange event and print().
#if BUILDFLAG(IS_CHROMEOS)
  display::test::DisplayManagerTestApi(ash::Shell::Get()->display_manager())
      .UpdateDisplay("0+0-807x600");
#else
  screen_.display_list().UpdateDisplay({1, gfx::Rect(0, 0, 807, 600)},
                                       display::DisplayList::Type::PRIMARY);
  EXPECT_EQ(screen_.display_list().displays().size(), 1u);
#endif  // BUILDFLAG(IS_CHROMEOS)
  ASSERT_EQ(1, display::Screen::GetScreen()->GetNumDisplays());

  content::WebContents* print_preview =
      web_contents_added_observer.GetWebContents();
  EXPECT_EQ(print_preview, printing::PrintPreviewDialogController::GetInstance()
                               ->GetPrintPreviewForContents(tab));
  content::AwaitDocumentOnLoadCompleted(print_preview);

  // Add a second display while the print preview dialog is showing.
#if BUILDFLAG(IS_CHROMEOS)
  display::test::DisplayManagerTestApi(ash::Shell::Get()->display_manager())
      .UpdateDisplay("0+0-807x600,1000+0-804x600");
#else
  screen_.display_list().AddDisplay({2, gfx::Rect(1000, 0, 804, 600)},
                                    display::DisplayList::Type::NOT_PRIMARY);
  EXPECT_EQ(screen_.display_list().displays().size(), 2u);
#endif  // BUILDFLAG(IS_CHROMEOS)
  ASSERT_EQ(2, display::Screen::GetScreen()->GetNumDisplays());

  // Cancel print to unfreeze the tab's JS and check that no crash occurred.
  content::WebContentsDestroyedWatcher destroyed_watcher(print_preview);
  // Note: This could be a browser_test if cancelling the print preview dialog
  // was more broadly possible without simulating a key press.
  // PrintViewManager::PrintPreviewDone() worked on Linux, but not Chrome OS.
  EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_ESCAPE, false,
                                              false, false, false));
  destroyed_watcher.Wait();
  ASSERT_FALSE(tab->IsCrashed());
  EXPECT_EQ(true, EvalJs(tab, "true"));
}