File: web_app_browsertest_util.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; 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 (216 lines) | stat: -rw-r--r-- 7,742 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
// 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.

#ifndef CHROME_BROWSER_UI_WEB_APPLICATIONS_TEST_WEB_APP_BROWSERTEST_UTIL_H_
#define CHROME_BROWSER_UI_WEB_APPLICATIONS_TEST_WEB_APP_BROWSERTEST_UTIL_H_

#include <string_view>

#include "base/functional/callback_forward.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/web_applications/externally_managed_app_manager.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_install_manager.h"
#include "chrome/browser/web_applications/web_app_install_manager_observer.h"
#include "components/webapps/common/web_app_id.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/base/window_open_disposition.h"

class Browser;
class GURL;
class Profile;

namespace base {
class FilePath;
}

namespace webapps {
enum class InstallResultCode;
}

namespace content {
class WebContents;
}  // namespace content

namespace web_app {

struct ExternalInstallOptions;
class WebAppInstallManager;

// For InstallWebAppFromInfo see web_app_install_test_utils.h

// Navigates to |app_url| and installs app without any installability checks.
// Always selects to open app in its own window.
webapps::AppId InstallWebAppFromPage(Browser* browser, const GURL& app_url);

// Same as InstallWebAppFromPage() but waits for the app browser window to
// appear and closes it.
webapps::AppId InstallWebAppFromPageAndCloseAppBrowser(Browser* browser,
                                                       const GURL& app_url);

// Navigates to |app_url|, verifies WebApp installability, and installs app.
webapps::AppId InstallWebAppFromManifest(Browser* browser, const GURL& app_url);

// Launches a new app window for |app| in |profile| with specified
// |disposition|.
Browser* LaunchWebAppBrowser(
    Profile*,
    const webapps::AppId&,
    WindowOpenDisposition disposition = WindowOpenDisposition::CURRENT_TAB);

// Launches the app, waits for the app url to load.
Browser* LaunchWebAppBrowserAndWait(
    Profile*,
    const webapps::AppId&,
    WindowOpenDisposition disposition = WindowOpenDisposition::CURRENT_TAB);

// Launches a new tab for |app| in |profile|.
Browser* LaunchBrowserForWebAppInTab(
    Profile*,
    const webapps::AppId&,
    WindowOpenDisposition disposition =
        WindowOpenDisposition::NEW_FOREGROUND_TAB);

// Launches the web app to the given URL.
Browser* LaunchWebAppToURL(Profile* profile,
                           const webapps::AppId& app_id,
                           const GURL& url);

// Return |ExternalInstallOptions| with OS shortcut creation disabled.
ExternalInstallOptions CreateInstallOptions(
    const GURL& url,
    const ExternalInstallSource& source =
        ExternalInstallSource::kInternalDefault);

// Synchronous version of ExternallyManagedAppManager::Install.
ExternallyManagedAppManager::InstallResult ExternallyManagedAppManagerInstall(
    Profile*,
    ExternalInstallOptions);

// This function simulates loading a given url via a link click.
// If |proceed_through_interstitial| is true, asserts that a security
// interstitial is shown, and clicks through it, before returning.
// Note - this does NOT wait for the given url to load, it just waits for
// navigation to complete. To ensure the given url is fully loaded, wait for
// that separately.
void NavigateViaLinkClickToURLAndWait(
    Browser* browser,
    const GURL& url,
    bool proceed_through_interstitial = false);

// Performs a navigation and then checks that the toolbar visibility is as
// expected.
void NavigateAndCheckForToolbar(Browser* browser,
                                const GURL& url,
                                bool expected_visibility,
                                bool proceed_through_interstitial = false);

enum AppMenuCommandState {
  kEnabled,
  kDisabled,
  kNotPresent,
};

// For a non-app browser, determines if the command is enabled/disabled/absent.
AppMenuCommandState GetAppMenuCommandState(int command_id, Browser* browser);

// Searches for a Browser window for a given |app_id|. browser->app_name() must
// be defined.
Browser* FindWebAppBrowser(Profile* profile, const webapps::AppId& app_id);

void CloseAndWait(Browser* browser);

bool IsBrowserOpen(const Browser* test_browser);

// Install a web policy app with |url|.
// Returns a valid app ID of the installed app or nullopt.
std::optional<webapps::AppId> ForceInstallWebApp(Profile* profile, GURL url);

// Helper class that lets you await one Browser added and one Browser removed
// event. Optionally filters to a specific Browser with |filter|. Useful for
// closing the web app window that appears after installation from page.
class BrowserWaiter : public BrowserListObserver {
 public:
  explicit BrowserWaiter(Browser* filter = nullptr);
  ~BrowserWaiter() override;

  Browser* AwaitAdded(
      const base::Location& location = base::Location::Current());
  Browser* AwaitRemoved(
      const base::Location& location = base::Location::Current());

  // BrowserListObserver:
  void OnBrowserAdded(Browser* browser) override;
  void OnBrowserRemoved(Browser* browser) override;

 private:
  const raw_ptr<Browser, AcrossTasksDanglingUntriaged> filter_ = nullptr;

  base::RunLoop added_run_loop_;
  raw_ptr<Browser, AcrossTasksDanglingUntriaged> added_browser_ = nullptr;

  base::RunLoop removed_run_loop_;
  raw_ptr<Browser, AcrossTasksDanglingUntriaged> removed_browser_ = nullptr;
};

class UpdateAwaiter : public WebAppInstallManagerObserver {
 public:
  explicit UpdateAwaiter(WebAppInstallManager& install_manager);
  ~UpdateAwaiter() override;
  void AwaitUpdate(const base::Location& location = base::Location::Current());

  // WebAppInstallManagerObserver:
  void OnWebAppManifestUpdated(const webapps::AppId& app_id) override;
  void OnWebAppInstallManagerDestroyed() override;

 private:
  base::RunLoop run_loop_;
  base::ScopedObservation<WebAppInstallManager, WebAppInstallManagerObserver>
      scoped_observation_{this};
};

// Creates a temporary file with the |extension|.
base::FilePath CreateTestFileWithExtension(std::string_view extension);

// Wait for an IPH bubble to show up inside the browser, and return true or
// false based on whether the bubble showed up.
bool WaitForIPHToShowIfAny(Browser* browser);

namespace test {

// Denote ways to simulate click on an element.
enum class ClickMethod {
  kLeftClick,
  kMiddleClick,
  kShiftClick,
  kRightClickLaunchApp
};

// This function simulates a click on the middle of an element matching
// `element_id` based on the type of click passed to it.
void SimulateClickOnElement(content::WebContents* contents,
                            std::string element_id,
                            ClickMethod click);

// Runs `action` for all tabs. This method is resilient to `action` waiting on
// async work, and considers the fresh `BrowserList` and tab model before each
// call. This method ensures that `action` is not called for the same web
// contents twice.
void RunForAllTabs(base::RepeatingCallback<void(content::WebContents&)> action);

// Wait for all available `WebContents` when this is called to finish loading.
// Note: This will hang forever if any web contents purposefully never finishes
// loading, causes reloads, or in any other way doesn't call the 'load' event in
// the page.
void CompletePageLoadForAllWebContents();

}  // namespace test

}  // namespace web_app

#endif  // CHROME_BROWSER_UI_WEB_APPLICATIONS_TEST_WEB_APP_BROWSERTEST_UTIL_H_