File: popup_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (255 lines) | stat: -rw-r--r-- 11,224 bytes parent folder | download | duplicates (5)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string>

#include "base/memory/raw_ptr.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/test/popup_test_base.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "third_party/blink/public/common/features.h"
#include "ui/display/display.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_observer.h"

namespace {

// Tests of window placement for popup browser windows.
using PopupTest = PopupTestBase;

// A helper class to wait for the bounds of two widgets to become equal.
class WidgetBoundsEqualWaiter final : public views::WidgetObserver {
 public:
  WidgetBoundsEqualWaiter(views::Widget* widget, views::Widget* widget_cmp)
      : widget_(widget), widget_cmp_(widget_cmp) {
    widget_->AddObserver(this);
    widget_cmp_->AddObserver(this);
  }

  WidgetBoundsEqualWaiter(const WidgetBoundsEqualWaiter&) = delete;
  WidgetBoundsEqualWaiter& operator=(const WidgetBoundsEqualWaiter&) = delete;
  ~WidgetBoundsEqualWaiter() final {
    widget_->RemoveObserver(this);
    widget_cmp_->RemoveObserver(this);
  }

  // views::WidgetObserver:
  void OnWidgetBoundsChanged(views::Widget* widget,
                             const gfx::Rect& rect) final {
    if (WidgetsBoundsEqual()) {
      widget_->RemoveObserver(this);
      widget_cmp_->RemoveObserver(this);
      run_loop_.Quit();
    }
  }

  // Wait for changes to occur, or return immediately if they already have.
  void Wait() {
    if (!WidgetsBoundsEqual()) {
      run_loop_.Run();
    }
  }

 private:
  bool WidgetsBoundsEqual() {
    return widget_->GetWindowBoundsInScreen() ==
           widget_cmp_->GetWindowBoundsInScreen();
  }
  const raw_ptr<views::Widget> widget_ = nullptr;
  const raw_ptr<views::Widget> widget_cmp_ = nullptr;
  base::RunLoop run_loop_;
};

// Ensure `left=0,top=0` popup window feature coordinates are respected.
IN_PROC_BROWSER_TEST_F(PopupTest, OpenLeftAndTopZeroCoordinates) {
  // Attempt to open a popup at (0,0). Its bounds should match the request, but
  // be adjusted to meet minimum size and available display area constraints.
  Browser* popup =
      OpenPopup(browser(), "open('.', '', 'left=0,top=0,width=50,height=50')");
  const display::Display display = GetDisplayNearestBrowser(popup);
  gfx::Rect expected(popup->window()->GetBounds().size());
  expected.AdjustToFit(display.work_area());
#if BUILDFLAG(IS_LINUX)
  // TODO(crbug.com/40815883) Desktop Linux window bounds are inaccurate.
  expected.Outset(50);
  EXPECT_TRUE(expected.Contains(popup->window()->GetBounds()))
      << " expected: " << expected.ToString()
      << " popup: " << popup->window()->GetBounds().ToString()
      << " work_area: " << display.work_area().ToString();
#else
  EXPECT_EQ(expected.ToString(), popup->window()->GetBounds().ToString())
      << " work_area: " << display.work_area().ToString();
#endif
}

// Ensure popups are opened in the available space of the opener's display.
IN_PROC_BROWSER_TEST_F(PopupTest, OpenClampedToCurrentDisplay) {
  // Attempt to open popups outside the bounds of the opener's display.
  const char* const open_features[] = {
      ("left=${screen.availLeft-50},top=${screen.availTop-50}"
       ",width=200,height=200"),
      ("left=${screen.availLeft+screen.availWidth+50}"
       ",top=${screen.availTop+screen.availHeight+50},width=200,height=200"),
      ("left=${screen.availLeft+screen.availWidth-50}"
       ",top=${screen.availTop+screen.availHeight-50},width=500,height=500,"),
      "width=${screen.availWidth+300},height=${screen.availHeight+300}",
  };
  const display::Display display = GetDisplayNearestBrowser(browser());
  for (const char* const features : open_features) {
    const std::string script = "open('.', '', `" + std::string(features) + "`)";
    Browser* popup = OpenPopup(browser(), script);
    // The popup should be constrained to the opener's available display space.
    EXPECT_EQ(display, GetDisplayNearestBrowser(popup));
    gfx::Rect work_area(display.work_area());
#if BUILDFLAG(IS_LINUX)
    // TODO(crbug.com/40815883) Desktop Linux bounds flakily extend outside the
    // work area on trybots, when opening with excessive width and height, e.g.:
    // width=${screen.availWidth+300},height=${screen.availHeight+300} yields:
    // work_area: 0,0 1280x800 popup: 1,20 1280x800
    work_area.Outset(50);
#endif
    EXPECT_TRUE(work_area.Contains(popup->window()->GetBounds()))
        << " script: " << script
        << " work_area: " << display.work_area().ToString()
        << " popup: " << popup->window()->GetBounds().ToString();
  }
}

// Ensure popups cannot be moved beyond the available display space by script.
IN_PROC_BROWSER_TEST_F(PopupTest, MoveClampedToCurrentDisplay) {
  const char kOpenPopup[] =
      ("open('.', '', `left=${screen.availLeft+screen.availWidth/2}"
       ",top=${screen.availTop+screen.availHeight/2},width=200,height=200`)");
  const char* const kMoveScripts[] = {
      "moveBy(screen.availWidth*2, screen.availHeight* 2)",
      "moveBy(screen.availWidth*-2, screen.availHeight*-2)",
      ("moveTo(screen.availLeft+screen.availWidth+50,"
       "screen.availTop+screen.availHeight+50)"),
      "moveTo(screen.availLeft-50, screen.availTop-50)",
  };
  const display::Display display = GetDisplayNearestBrowser(browser());
  for (const char* const script : kMoveScripts) {
    Browser* popup = OpenPopup(browser(), kOpenPopup);
    gfx::Rect popup_bounds = popup->window()->GetBounds();
    content::WebContents* popup_contents =
        popup->tab_strip_model()->GetActiveWebContents();
    SCOPED_TRACE(testing::Message()
                 << " script: " << script
                 << " work_area: " << display.work_area().ToString()
                 << " popup-before: " << popup_bounds.ToString());
    content::ExecuteScriptAsync(popup_contents, script);
    // Wait for a substantial move, bounds change during init.
    WaitForBoundsChange(popup, /*move_by=*/40, /*resize_by=*/0);
    EXPECT_NE(popup_bounds.origin(), popup->window()->GetBounds().origin());
    EXPECT_EQ(popup_bounds.size(), popup->window()->GetBounds().size());
    EXPECT_TRUE(display.work_area().Contains(popup->window()->GetBounds()))
        << " popup-after: " << popup->window()->GetBounds().ToString();
  }
}

// Ensure popups cannot be resized beyond the available display space by script.
IN_PROC_BROWSER_TEST_F(PopupTest, ResizeClampedToCurrentDisplay) {
  const char kOpenPopup[] =
      ("open('.', '', `left=${screen.availLeft},top=${screen.availTop}"
       ",width=200,height=200`)");
  const char* const kResizeScripts[] = {
      "resizeBy(screen.availWidth*2, screen.availHeight*2)",
      "resizeTo(screen.availWidth+200, screen.availHeight+200)",
  };
  const display::Display display = GetDisplayNearestBrowser(browser());
  for (const char* const script : kResizeScripts) {
    Browser* popup = OpenPopup(browser(), kOpenPopup);
    gfx::Rect popup_bounds = popup->window()->GetBounds();
    content::WebContents* popup_contents =
        popup->tab_strip_model()->GetActiveWebContents();
    SCOPED_TRACE(testing::Message()
                 << " script: " << script
                 << " work_area: " << display.work_area().ToString()
                 << " popup-before: " << popup_bounds.ToString());
    content::ExecuteScriptAsync(popup_contents, script);
    // Wait for a substantial resize, bounds change during init.
    WaitForBoundsChange(popup, /*move_by=*/0, /*resize_by=*/99);
    EXPECT_NE(popup_bounds.size(), popup->window()->GetBounds().size());
    EXPECT_TRUE(display.work_area().Contains(popup->window()->GetBounds()))
        << " popup-after: " << popup->window()->GetBounds().ToString();
  }
}

// Opens two popups with custom position and size, but one has noopener. They
// should both have the same position and size. http://crbug.com/1011688
IN_PROC_BROWSER_TEST_F(PopupTest, NoopenerPositioning) {
  const char kFeatures[] =
      "left=${screen.availLeft},top=${screen.availTop},width=200,height=200";
  Browser* noopener_popup = OpenPopup(
      browser(), "open('.', '', `noopener=1," + std::string(kFeatures) + "`)");
  Browser* opener_popup =
      OpenPopup(browser(), "open('.', '', `" + std::string(kFeatures) + "`)");

  WidgetBoundsEqualWaiter(views::Widget::GetWidgetForNativeWindow(
                              noopener_popup->window()->GetNativeWindow()),
                          views::Widget::GetWidgetForNativeWindow(
                              opener_popup->window()->GetNativeWindow()))
      .Wait();

  EXPECT_EQ(noopener_popup->window()->GetBounds().ToString(),
            opener_popup->window()->GetBounds().ToString());
}

// Tests for Additional Windowing Controls on popup windows.
// https://chromestatus.com/feature/5201832664629248
// For PWA tests see WebAppFrameToolbarBrowserTest_AdditionalWindowingControls
class PopupTest_AdditionalWindowingControls : public PopupTest {
 private:
  base::test::ScopedFeatureList feature_list{
      blink::features::kDesktopPWAsAdditionalWindowingControls};
};

// Ensure that moving a popup by moveTo/moveBy generates a `move` event.
// Note: window.moveTo/moveBy API is enabled only for popups and web apps.
#if BUILDFLAG(IS_LINUX)
#define MAYBE_MoveCallFiresMoveEvent DISABLED_MoveCallFiresMoveEvent
#else
#define MAYBE_MoveCallFiresMoveEvent MoveCallFiresMoveEvent
#endif
IN_PROC_BROWSER_TEST_F(PopupTest_AdditionalWindowingControls,
                       MAYBE_MoveCallFiresMoveEvent) {
  const char popup_script[] =
      R"(var command = "%s";
      var coordString = (x, y) => `(X: ${x}, Y: ${y})`;
      new Promise((resolve, reject) => {
        const coord_before = coordString(screenX, screenY);
        addEventListener('move', e => resolve(`move fired`));
        setTimeout(() => {
          const coord_after = coordString(screenX, screenY);
          reject(`move not fired by ${command}; window position: `
               + `${coord_before} -> ${coord_after}`); }, 1000);
        %s;
        }).finally(()=>close()); )";

  for (const char* const move_command : {"moveBy(10, 10)", "moveTo(50, 50)"}) {
    std::string script =
        base::StringPrintf(popup_script, move_command, move_command);

    Browser* popup = OpenPopup(
        browser(), "open('.', '', 'left=0,top=0,width=50,height=50')");
    content::WebContents* popup_contents =
        popup->tab_strip_model()->GetActiveWebContents();

    gfx::Rect bounds_before = popup->window()->GetBounds();
    SCOPED_TRACE(testing::Message()
                 << " move-command: " << move_command
                 << " popup-before: " << bounds_before.ToString());
    EXPECT_EQ(content::EvalJs(popup_contents, script), "move fired");
    gfx::Rect bounds_after = popup->window()->GetBounds();
    EXPECT_NE(bounds_before.ToString(), bounds_after.ToString());
  }
}

}  // namespace