File: popup_test_base.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 (160 lines) | stat: -rw-r--r-- 6,348 bytes parent folder | download | duplicates (7)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/test/popup_test_base.h"

#include <string>

#include "base/memory/raw_ptr.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/embedder_support/switches.h"
#include "components/permissions/permission_request_manager.h"
#include "content/public/test/browser_test_utils.h"
#include "ui/display/display.h"
#include "ui/display/screen.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"
#include "url/gurl.h"

namespace {

// A helper to wait for Browser window bounds changes beyond given thresholds.
class BoundsChangeWaiter final : public views::WidgetObserver {
 public:
  BoundsChangeWaiter(Browser* browser, int move_by, int resize_by)
      : widget_(views::Widget::GetWidgetForNativeWindow(
            browser->window()->GetNativeWindow())),
        move_by_(move_by),
        resize_by_(resize_by),
        initial_bounds_(widget_->GetWindowBoundsInScreen()) {}

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

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

  // Wait for changes to occur, or return immediately if they already have.
  void Wait() {
    if (!BoundsChangeMeetsThreshold(widget_->GetWindowBoundsInScreen())) {
      widget_->AddObserver(this);
      run_loop_.Run();
    }
  }

 private:
  bool BoundsChangeMeetsThreshold(const gfx::Rect& rect) const {
    return (std::abs(rect.x() - initial_bounds_.x()) >= move_by_ ||
            std::abs(rect.y() - initial_bounds_.y()) >= move_by_) &&
           (std::abs(rect.width() - initial_bounds_.width()) >= resize_by_ ||
            std::abs(rect.height() - initial_bounds_.height()) >= resize_by_);
  }

  const raw_ptr<views::Widget> widget_;
  const int move_by_, resize_by_;
  const gfx::Rect initial_bounds_;
  base::RunLoop run_loop_;
};

}  // namespace

void PopupTestBase::SetUpCommandLine(base::CommandLine* command_line) {
  command_line->AppendSwitch(embedder_support::kDisablePopupBlocking);
}

// static
Browser* PopupTestBase::OpenPopup(Browser* browser,
                                  const std::string& script,
                                  bool user_gesture) {
  return OpenPopup(browser->tab_strip_model()->GetActiveWebContents(), script,
                   user_gesture);
}

// static
Browser* PopupTestBase::OpenPopup(const content::ToRenderFrameHost& adapter,
                                  const std::string& script,
                                  bool user_gesture) {
  if (user_gesture) {
    content::ExecuteScriptAsync(adapter, script);
  } else {
    content::ExecuteScriptAsyncWithoutUserGesture(adapter, script);
  }
  Browser* popup = ui_test_utils::WaitForBrowserToOpen();
  content::WebContents* popup_contents =
      popup->tab_strip_model()->GetActiveWebContents();
  // The popup's bounds are initialized after the synchronous window.open().
  // Ideally, this might wait for browser->renderer window bounds init via:
  // blink::mojom::Widget.UpdateVisualProperties, but it seems sufficient to
  // wait for WebContents to load the URL after the initial about:blank doc,
  // and then for that Document's readyState to be 'complete'. Anecdotally,
  // initial bounds seem settled once outerWidth and outerHeight are non-zero.
  EXPECT_TRUE(WaitForLoadStop(popup_contents));
  EXPECT_TRUE(WaitForRenderFrameReady(popup_contents->GetPrimaryMainFrame()));
  EXPECT_NE("0x0", EvalJs(popup_contents, "outerWidth + 'x' + outerHeight"));
  return popup;
}

// static
void PopupTestBase::WaitForBoundsChange(Browser* browser,
                                        int move_by,
                                        int resize_by) {
  BoundsChangeWaiter(browser, move_by, resize_by).Wait();
}

// static
void PopupTestBase::SetUpWindowManagement(Browser* browser) {
  content::WebContents* web_contents =
      browser->tab_strip_model()->GetActiveWebContents();
  // Request and auto-accept the permission request.
  permissions::PermissionRequestManager* permission_request_manager =
      permissions::PermissionRequestManager::FromWebContents(web_contents);
  permission_request_manager->set_auto_response_for_test(
      permissions::PermissionRequestManager::ACCEPT_ALL);
  content::RenderFrameHost* rfh = web_contents->GetPrimaryMainFrame();
  ASSERT_TRUE(content::WaitForLoadStop(web_contents));
  ASSERT_TRUE(content::WaitForRenderFrameReady(rfh));
  ASSERT_EQ("complete", EvalJs(web_contents, "document.readyState"));
  ASSERT_EQ("visible", EvalJs(web_contents, "document.visibilityState"));
  ASSERT_GT(EvalJs(web_contents,
                   R"JS(getScreenDetails().then(s => {
                          window.screenDetails = s;
                          return s.screens.length; }))JS"),
            0);
  // Do not auto-accept any other permission requests.
  permission_request_manager->set_auto_response_for_test(
      permissions::PermissionRequestManager::NONE);
}

// static
display::Display PopupTestBase::GetDisplayNearestBrowser(
    const Browser* browser) {
  return display::Screen::GetScreen()->GetDisplayNearestWindow(
      browser->window()->GetNativeWindow());
}

// static
void PopupTestBase::WaitForUserActivationExpiry(Browser* browser) {
  const std::string await_activation_expiry_script = R"(
    (async () => {
      while (navigator.userActivation.isActive)
        await new Promise(resolve => setTimeout(resolve, 1000));
      return navigator.userActivation.isActive;
    })();
  )";
  auto* tab = browser->tab_strip_model()->GetActiveWebContents();
  EXPECT_EQ(false, EvalJs(tab, await_activation_expiry_script,
                          content::EXECUTE_SCRIPT_NO_USER_GESTURE));
  EXPECT_FALSE(tab->HasRecentInteraction());
}