File: web_app_navigation_browsertest.cc

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 (350 lines) | stat: -rw-r--r-- 12,481 bytes parent folder | download | duplicates (4)
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// Copyright 2018 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/web_applications/test/web_app_navigation_browsertest.h"

#include <vector>

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/run_loop.h"
#include "base/strings/escape.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "build/build_config.h"
#include "chrome/browser/apps/app_service/app_registry_cache_waiter.h"
#include "chrome/browser/profiles/profile_io_data.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.h"
#include "chrome/browser/web_applications/web_app_install_finalizer.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "components/webapps/browser/uninstall_result_code.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/switches.h"

namespace {

const char kLaunchingPageHost[] = "launching-page.com";
const char kLaunchingPagePath[] = "/index.html";

const char kAppUrlHost[] = "app.com";
const char kOtherAppUrlHost[] = "other-app.com";
const char kAppScopePath[] = "/in_scope/";
const char kAppUrlPath[] = "/in_scope/index.html";
const char kInScopeUrlPath[] = "/in_scope/other.html";
const char kOutOfScopeUrlPath[] = "/out_of_scope/index.html";

const char kAppName[] = "Test app";

}  // anonymous namespace

namespace web_app {

// static
const char* WebAppNavigationBrowserTest::GetLaunchingPageHost() {
  return kLaunchingPageHost;
}

// static
const char* WebAppNavigationBrowserTest::GetLaunchingPagePath() {
  return kLaunchingPagePath;
}

// static
const char* WebAppNavigationBrowserTest::GetAppUrlHost() {
  return kAppUrlHost;
}

// static
const char* WebAppNavigationBrowserTest::GetOtherAppUrlHost() {
  return kOtherAppUrlHost;
}

// static
const char* WebAppNavigationBrowserTest::GetAppScopePath() {
  return kAppScopePath;
}

// static
const char* WebAppNavigationBrowserTest::GetAppUrlPath() {
  return kAppUrlPath;
}

// static
const char* WebAppNavigationBrowserTest::GetInScopeUrlPath() {
  return kInScopeUrlPath;
}

// static
const char* WebAppNavigationBrowserTest::GetOutOfScopeUrlPath() {
  return kOutOfScopeUrlPath;
}

// static
const char* WebAppNavigationBrowserTest::GetAppName() {
  return kAppName;
}

// static
std::string WebAppNavigationBrowserTest::CreateServerRedirect(
    const GURL& target_url) {
  const char* const kServerRedirectBase = "/server-redirect?";
  return kServerRedirectBase +
         base::EscapeQueryParamValue(target_url.spec(), false);
}

// static
std::unique_ptr<content::TestNavigationObserver>
WebAppNavigationBrowserTest::GetTestNavigationObserver(const GURL& target_url) {
  auto observer = std::make_unique<content::TestNavigationObserver>(target_url);
  observer->WatchExistingWebContents();
  observer->StartWatchingNewWebContents();
  return observer;
}

// static
void WebAppNavigationBrowserTest::ClickLink(
    content::WebContents* web_contents,
    const GURL& link_url,
    WebAppNavigationBrowserTest::LinkTarget target,
    const std::string& rel,
    int modifiers,
    blink::WebMouseEvent::Button button) {
  std::string script = content::JsReplace(
      R"(
(() => {
  document.body.innerHTML = '';
  const link = document.createElement('a');
  link.href = $1;
  link.target = $2;
  link.rel = $3;
  // Make a click target that covers the whole viewport.
  const click_target = document.createElement('textarea');
  click_target.style.position = 'absolute';
  click_target.style.top = 0;
  click_target.style.left = 0;
  click_target.style.height = '100vh';
  click_target.style.width = '100vw';
  link.appendChild(click_target);
  document.body.appendChild(link);
})();)",
      link_url.spec().c_str(), target == LinkTarget::SELF ? "_self" : "_blank",
      rel.c_str());
  ASSERT_TRUE(content::ExecJs(web_contents, script));

  content::SimulateEndOfPaintHoldingOnPrimaryMainFrame(web_contents);
  content::SimulateMouseClick(web_contents, modifiers, button);
}

// static
void WebAppNavigationBrowserTest::ClickLinkWithModifiersAndWaitForURL(
    content::WebContents* web_contents,
    const GURL& link_url,
    const GURL& target_url,
    WebAppNavigationBrowserTest::LinkTarget target,
    const std::string& rel,
    int modifiers,
    blink::WebMouseEvent::Button button) {
  auto observer = GetTestNavigationObserver(target_url);
  ClickLink(web_contents, link_url, target, rel, modifiers, button);
  observer->Wait();
}

// static
void WebAppNavigationBrowserTest::ClickLinkAndWaitForURL(
    content::WebContents* web_contents,
    const GURL& link_url,
    const GURL& target_url,
    WebAppNavigationBrowserTest::LinkTarget target,
    const std::string& rel) {
  ClickLinkWithModifiersAndWaitForURL(
      web_contents, link_url, target_url, target, rel,
      blink::WebInputEvent::Modifiers::kNoModifiers);
}

// static
void WebAppNavigationBrowserTest::ClickLinkAndWait(
    content::WebContents* web_contents,
    const GURL& link_url,
    WebAppNavigationBrowserTest::LinkTarget target,
    const std::string& rel) {
  ClickLinkAndWaitForURL(web_contents, link_url, link_url, target, rel);
}

WebAppNavigationBrowserTest::WebAppNavigationBrowserTest()
    : https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {}

WebAppNavigationBrowserTest::~WebAppNavigationBrowserTest() = default;

void WebAppNavigationBrowserTest::SetUp() {
  https_server_.AddDefaultHandlers(GetChromeTestDataDir());
  // Register a request handler that will return empty pages. Tests are
  // responsible for adding elements and firing events on these empty pages.
  https_server_.RegisterRequestHandler(base::BindRepeating(
      [](const net::test_server::HttpRequest& request)
          -> std::unique_ptr<net::test_server::HttpResponse> {
        // Let the default request handlers handle redirections.
        if (request.GetURL().path() == "/server-redirect" ||
            request.GetURL().path() == "/client-redirect") {
          return {};
        }
        auto response = std::make_unique<net::test_server::BasicHttpResponse>();
        response->set_content_type("text/html");
        response->AddCustomHeader("Access-Control-Allow-Origin", "*");
        response->AddCustomHeader("Supports-Loading-Mode", "fenced-frame");
        return response;
      }));

  WebAppBrowserTestBase::SetUp();
}

void WebAppNavigationBrowserTest::SetUpInProcessBrowserTestFixture() {
  WebAppBrowserTestBase::SetUpInProcessBrowserTestFixture();
  cert_verifier_.SetUpInProcessBrowserTestFixture();
}

void WebAppNavigationBrowserTest::TearDownInProcessBrowserTestFixture() {
  WebAppBrowserTestBase::TearDownInProcessBrowserTestFixture();
  cert_verifier_.TearDownInProcessBrowserTestFixture();
}

void WebAppNavigationBrowserTest::SetUpCommandLine(
    base::CommandLine* command_line) {
  // Allow pre-commit input because the content used in the test does not paint
  // anything and relies on script execution to create links, and we do not want
  // to wait for the commit timeout.
  command_line->AppendSwitch(blink::switches::kAllowPreCommitInput);
  cert_verifier_.SetUpCommandLine(command_line);
}

void WebAppNavigationBrowserTest::SetUpOnMainThread() {
  WebAppBrowserTestBase::SetUpOnMainThread();
  host_resolver()->AddRule("*", "127.0.0.1");
  // By default, all SSL cert checks are valid. Can be overridden in tests.
  cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
  profile_ = browser()->profile();
}

void WebAppNavigationBrowserTest::TearDownOnMainThread() {
#if BUILDFLAG(IS_CHROMEOS)
  auto* const provider = WebAppProvider::GetForWebApps(profile());
  const WebAppRegistrar& registrar = provider->registrar_unsafe();
  std::vector<webapps::AppId> app_ids = registrar.GetAppIds();
  for (const auto& app_id : app_ids) {
    if (!registrar.IsInRegistrar(app_id)) {
      continue;
    }
    const WebApp* app = registrar.GetAppById(app_id);
    DCHECK(app->CanUserUninstallWebApp());
    apps::AppReadinessWaiter app_readiness_waiter(
        profile(), app_id, apps::Readiness::kUninstalledByUser);
    base::RunLoop run_loop;
    provider->scheduler().RemoveUserUninstallableManagements(
        app_id, webapps::WebappUninstallSource::kAppsPage,
        base::BindLambdaForTesting([&](webapps::UninstallResultCode code) {
          EXPECT_EQ(code, webapps::UninstallResultCode::kAppRemoved);
          run_loop.Quit();
        }));
    run_loop.Run();
    app_readiness_waiter.Await();
  }
#endif

  WebAppBrowserTestBase::TearDownOnMainThread();
}

Profile* WebAppNavigationBrowserTest::profile() {
  return profile_;
}

void WebAppNavigationBrowserTest::InstallTestWebApp() {
  test_web_app_ = InstallTestWebApp(GetAppUrlHost(), GetAppScopePath());
}

webapps::AppId WebAppNavigationBrowserTest::InstallTestWebApp(
    const std::string& app_host,
    const std::string& app_scope) {
  if (!https_server_.Started()) {
    CHECK(https_server_.Start());
  }

  GURL start_url = https_server_.GetURL(app_host, GetAppUrlPath());
  auto web_app_info =
      WebAppInstallInfo::CreateWithStartUrlForTesting(start_url);
  web_app_info->scope = https_server_.GetURL(app_host, app_scope);
  web_app_info->title = base::UTF8ToUTF16(GetAppName());
  web_app_info->description = u"Test description";
  web_app_info->user_display_mode =
      web_app::mojom::UserDisplayMode::kStandalone;

  webapps::AppId app_id =
      test::InstallWebApp(profile(), std::move(web_app_info));
  DCHECK(!app_id.empty());
  apps::AppReadinessWaiter(profile(), app_id).Await();
  return app_id;
}

Browser* WebAppNavigationBrowserTest::OpenTestWebApp() {
  GURL app_url = https_server_.GetURL(GetAppUrlHost(), GetAppUrlPath());
  auto observer = GetTestNavigationObserver(app_url);
  Browser* app_browser = LaunchWebAppBrowser(test_web_app_);
  observer->Wait();

  return app_browser;
}

void WebAppNavigationBrowserTest::NavigateToLaunchingPage(Browser* browser) {
  ASSERT_TRUE(ui_test_utils::NavigateToURL(
      browser,
      https_server_.GetURL(GetLaunchingPageHost(), GetLaunchingPagePath())));
}

bool WebAppNavigationBrowserTest::ExpectLinkClickNotCapturedIntoAppBrowser(
    Browser* browser,
    const GURL& target_url,
    const std::string& rel) {
  content::WebContents* initial_tab =
      browser->tab_strip_model()->GetActiveWebContents();
  int num_tabs = browser->tab_strip_model()->count();
  size_t num_browsers = chrome::GetBrowserCount(browser->profile());

  ClickLinkAndWait(browser->tab_strip_model()->GetActiveWebContents(),
                   target_url, LinkTarget::SELF, rel);

  EXPECT_EQ(num_tabs, browser->tab_strip_model()->count());
  EXPECT_EQ(num_browsers, chrome::GetBrowserCount(browser->profile()));
  EXPECT_EQ(browser, chrome::FindLastActive());
  EXPECT_EQ(initial_tab, browser->tab_strip_model()->GetActiveWebContents());
  EXPECT_EQ(target_url, initial_tab->GetLastCommittedURL());

  return !HasFailure();
}

const GURL& WebAppNavigationBrowserTest::test_web_app_start_url() {
  auto* const provider = WebAppProvider::GetForWebApps(profile());
  const WebAppRegistrar& registrar = provider->registrar_unsafe();
  return registrar.GetAppStartUrl(test_web_app_);
}

}  // namespace web_app