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

#include "base/containers/contains.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_tabstrip.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/ui/web_applications/test/web_app_navigation_browsertest.h"
#include "chrome/browser/ui/web_applications/web_app_browsertest_base.h"
#include "chrome/browser/web_applications/os_integration/web_app_protocol_handler_manager.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "components/page_load_metrics/browser/page_load_metrics_test_waiter.h"
#include "components/services/app_service/public/cpp/protocol_handler_info.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_navigation_observer.h"
#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom.h"

namespace {

constexpr char kUseCounterHistogram[] = "Blink.UseCounter.Features";

blink::mojom::WebFeature protocol_handling_feature =
    blink::mojom::WebFeature::kWebAppManifestProtocolHandlers;

}  // namespace

namespace web_app {

class WebAppProtocolHandlingBrowserTest : public WebAppNavigationBrowserTest {
 public:
  WebAppProtocolHandlingBrowserTest() = default;
  ~WebAppProtocolHandlingBrowserTest() override = default;

  void SetUpOnMainThread() override {
    WebAppNavigationBrowserTest::SetUpOnMainThread();
    ASSERT_TRUE(embedded_test_server()->Start());
  }

  webapps::AppId InstallTestApp(const char* path, bool await_metric) {
    GURL start_url = embedded_test_server()->GetURL(path);
    page_load_metrics::PageLoadMetricsTestWaiter metrics_waiter(
        browser()->tab_strip_model()->GetActiveWebContents());
    if (await_metric) {
      metrics_waiter.AddWebFeatureExpectation(protocol_handling_feature);
    }

    webapps::AppId app_id =
        web_app::InstallWebAppFromPage(browser(), start_url);
    if (await_metric) {
      metrics_waiter.Wait();
    }

    return app_id;
  }

  web_app::WebAppProvider* provider() {
    return WebAppProvider::GetForTest(browser()->profile());
  }

  web_app::WebAppProtocolHandlerManager& protocol_handler_manager() {
    return provider()
        ->os_integration_manager()
        .protocol_handler_manager_for_testing();
  }

 protected:
  base::HistogramTester histogram_tester_;
};

IN_PROC_BROWSER_TEST_F(WebAppProtocolHandlingBrowserTest,
                       BasicProtocolHandlers) {
  webapps::AppId app_id = InstallTestApp(
      "/banners/"
      "manifest_test_page.html?manifest=manifest_protocol_handlers.json",
      /*await_metric=*/true);
  std::vector<apps::ProtocolHandlerInfo> protocol_handlers =
      protocol_handler_manager().GetAppProtocolHandlerInfos(app_id);

  // Two handlers have invalid properties so they shouldn't be in the result.
  apps::ProtocolHandlerInfo protocol_handler1;
  protocol_handler1.protocol = "mailto";
  protocol_handler1.url = GURL(embedded_test_server()->GetURL(
      "/banners/manifest_protocol_handlers.json?mailto=%s"));

  apps::ProtocolHandlerInfo protocol_handler2;
  protocol_handler2.protocol = "web+testing";
  protocol_handler2.url = GURL(embedded_test_server()->GetURL(
      "/banners/manifest_protocol_handlers.json?testing=%s"));

  ASSERT_EQ(2u, protocol_handlers.size());
  EXPECT_TRUE(base::Contains(protocol_handlers, protocol_handler1));
  EXPECT_TRUE(base::Contains(protocol_handlers, protocol_handler2));

  histogram_tester_.ExpectBucketCount(kUseCounterHistogram,
                                      protocol_handling_feature, 1);
}

IN_PROC_BROWSER_TEST_F(WebAppProtocolHandlingBrowserTest, NoProtocolHandlers) {
  webapps::AppId app_id =
      InstallTestApp("/banners/manifest_test_page.html?manifest=manifest.json",
                     /*await_metric=*/false);
  std::vector<apps::ProtocolHandlerInfo> protocol_handlers =
      protocol_handler_manager().GetAppProtocolHandlerInfos(app_id);
  ASSERT_EQ(0u, protocol_handlers.size());

  histogram_tester_.ExpectBucketCount(kUseCounterHistogram,
                                      protocol_handling_feature, 0);
}

}  // namespace web_app