File: web_app_helpers_unittest.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 (134 lines) | stat: -rw-r--r-- 5,845 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// 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/web_applications/web_app_helpers.h"

#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace web_app {

TEST(WebAppHelpers, GenerateApplicationNameFromURL) {
  EXPECT_EQ("_", GenerateApplicationNameFromURL(GURL()));

  EXPECT_EQ("example.com_/",
            GenerateApplicationNameFromURL(GURL("http://example.com")));

  EXPECT_EQ("example.com_/path",
            GenerateApplicationNameFromURL(GURL("https://example.com/path")));
}

TEST(WebAppHelpers, GenerateAppId) {
  EXPECT_EQ("fedbieoalmbobgfjapopkghdmhgncnaa",
            GenerateAppId(/*manifest_id=*/std::nullopt,
                          GURL("https://www.chromestatus.com/features")));

  // The io2016 example is also walked through at
  // https://play.golang.org/p/VrIq_QKFjiV
  EXPECT_EQ("mjgafbdfajpigcjmkgmeokfbodbcfijl",
            GenerateAppId(/*manifest_id=*/std::nullopt,
                          GURL("https://events.google.com/io2016/"
                               "?utm_source=web_app_manifest")));
}

TEST(WebAppHelpers, GenerateAppIdForSubApps) {
  const std::string subapp_starturl = "https://example.com/subapp";
  const webapps::ManifestId parent_manifest_id = GURL("https://example.com");

  EXPECT_EQ("emdpgjhffapdncpmnindbhiapcohmjga",
            GenerateAppId(/*manifest_id_path=*/std::nullopt,
                          GURL(subapp_starturl), parent_manifest_id));

  EXPECT_EQ("jaadilplijgkeakjaoplplaeceoommee",
            GenerateAppId("manifest.webmanifest", GURL(subapp_starturl),
                          parent_manifest_id));
}

TEST(WebAppHelpers, GenerateManifestIdFromStartUrlOnly) {
  EXPECT_EQ(GURL("https://example.com/"),
            GenerateManifestIdFromStartUrlOnly(GURL("https://example.com/")));
  EXPECT_EQ(GURL("https://example.com"),
            GenerateManifestIdFromStartUrlOnly(GURL("https://example.com")));
  EXPECT_EQ(GURL("https://example.com/start?a=b"),
            GenerateManifestIdFromStartUrlOnly(
                GURL("https://example.com/start?a=b")));
  EXPECT_EQ(GURL("https://example.com/start"),
            GenerateManifestIdFromStartUrlOnly(
                GURL("https://example.com/start#fragment")));
}

TEST(WebAppHelpers, IsValidWebAppUrl) {
  // TODO(crbug.com/40793595): Remove chrome-extension scheme from being
  // installed as PWAs on ChromeOS.
  bool is_chrome_extension_valid_web_app = true;
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
  // chrome-extension:// URLs can no longer be PWAs, but they can be shortcuts.
  is_chrome_extension_valid_web_app = false;
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)

  EXPECT_EQ(IsValidWebAppUrl(
                GURL("chrome-extension://oafaagfgbdpldilgjjfjocjglfbolmac")),
            is_chrome_extension_valid_web_app);

  EXPECT_TRUE(IsValidWebAppUrl(GURL("https://chromium.org")));
  EXPECT_TRUE(IsValidWebAppUrl(GURL("https://www.chromium.org")));
  EXPECT_TRUE(
      IsValidWebAppUrl(GURL("https://www.chromium.org/path/to/page.html")));
  EXPECT_TRUE(IsValidWebAppUrl(GURL("http://chromium.org")));
  EXPECT_TRUE(IsValidWebAppUrl(GURL("http://www.chromium.org")));
  EXPECT_TRUE(
      IsValidWebAppUrl(GURL("http://www.chromium.org/path/to/page.html")));
  EXPECT_TRUE(IsValidWebAppUrl(GURL("https://examle.com/foo?bar")));
  EXPECT_TRUE(IsValidWebAppUrl(GURL("https://examle.com/foo#bar")));

  EXPECT_FALSE(IsValidWebAppUrl(GURL()));
  EXPECT_FALSE(IsValidWebAppUrl(GURL("ftp://www.chromium.org")));
  EXPECT_FALSE(IsValidWebAppUrl(GURL("chrome://flags")));
  EXPECT_FALSE(IsValidWebAppUrl(GURL("about:blank")));
  EXPECT_FALSE(
      IsValidWebAppUrl(GURL("file://mhjfbmdgcfjbbpaeojofohoefgiehjai")));
  EXPECT_FALSE(IsValidWebAppUrl(GURL("chrome://extensions")));
  EXPECT_FALSE(
      IsValidWebAppUrl(GURL("filesystem:http://example.com/path/file.html")));
  EXPECT_TRUE(IsValidWebAppUrl(GURL("chrome://password-manager")));
}

TEST(WebAppHelpers, ManifestIdEncoding) {
  GURL start_url("https://example.com/abc");
  // ASCII character. URL parser no longer unescapes percent encoded ASCII
  // characters. See https://crbug.com/1252531.
  EXPECT_EQ(GenerateAppId("j", start_url), GenerateAppId("j", start_url));
  EXPECT_EQ(GenerateAppId("%6Ax", start_url), GenerateAppId("%6Ax", start_url));

  // Special characters.
  EXPECT_EQ(GenerateAppId("a😀b", start_url),
            GenerateAppId("a%F0%9F%98%80b", start_url));
  EXPECT_EQ(GenerateAppId("a b", start_url), GenerateAppId("a%20b", start_url));

  // "/"" is excluded from encoding according to url spec.
  EXPECT_NE(GenerateAppId("a/b", start_url), GenerateAppId("a%2Fb", start_url));
}

TEST(WebAppHelpers, ManifestIdWithQueriesAndFragments) {
  GURL start_url_long = GURL("https://example.com/start_url/long/path.html");
  GURL url = GURL("https://example.com/test");
  GURL url_with_query = GURL("https://example.com/test?id");
  GURL url_with_fragment = GURL("https://example.com/test#id");
  GURL url_with_query_and_fragment =
      GURL("https://example.com/test?id#fragment");

  EXPECT_EQ(url, GenerateManifestIdFromStartUrlOnly(url));
  EXPECT_EQ(url, GenerateManifestIdFromStartUrlOnly(url_with_fragment));
  EXPECT_EQ(url_with_query, GenerateManifestIdFromStartUrlOnly(url_with_query));
  EXPECT_EQ(url_with_query,
            GenerateManifestIdFromStartUrlOnly(url_with_query_and_fragment));

  EXPECT_EQ(url, GenerateManifestId("test", start_url_long));
  EXPECT_EQ(url, GenerateManifestId("test#id", start_url_long));
  EXPECT_EQ(url_with_query, GenerateManifestId("test?id", start_url_long));
  EXPECT_EQ(url_with_query,
            GenerateManifestId("test?id#fragment", start_url_long));
}

}  // namespace web_app