File: alternative_error_page_override_info_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 (193 lines) | stat: -rw-r--r-- 8,325 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
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <string_view>

#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/ui/web_applications/web_app_browsertest_base.h"
#include "chrome/browser/web_applications/proto/web_app_install_state.pb.h"
#include "chrome/browser/web_applications/test/web_app_icon_waiter.h"
#include "chrome/browser/web_applications/test/web_app_test_utils.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registrar.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/url_formatter/url_formatter.h"
#include "components/webapps/common/web_app_id.h"
#include "content/public/common/alternative_error_page_override_info.mojom.h"
#include "content/public/common/content_client.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_utils.h"
#include "skia/ext/skia_utils_base.h"
#include "ui/native_theme/native_theme.h"
#include "url/gurl.h"

// Class to test browser error page display info.
class AlternativeErrorPageOverrideInfoBrowserTest
    : public web_app::WebAppBrowserTestBase {
 public:
  // Helper function to prepare PWA and retrieve information from the
  // alternative error page function.
  content::mojom::AlternativeErrorPageOverrideInfoPtr GetErrorPageInfo(
      std::string_view html) {

    const GURL app_url = embedded_test_server()->GetURL(html);
    web_app::NavigateViaLinkClickToURLAndWait(browser(), app_url);
    web_app::test::InstallPwaForCurrentUrl(browser());
    content::BrowserContext* context = browser()->profile();

    return content::GetContentClientForTesting()
        ->browser()
        ->GetAlternativeErrorPageOverrideInfo(
            app_url, /*render_frame_host=*/nullptr, context,
            net::ERR_INTERNET_DISCONNECTED);
  }

 protected:
  const std::string yellow_ = skia::SkColorToHexString(SK_ColorYELLOW);
  const std::string green_ = skia::SkColorToHexString(SK_ColorGREEN);
  const std::string red_ = skia::SkColorToHexString(SK_ColorRED);
  const std::string blue_ = skia::SkColorToHexString(SK_ColorBLUE);
  const std::string black_ = skia::SkColorToHexString(SK_ColorBLACK);
  const std::string white_ = skia::SkColorToHexString(SK_ColorWHITE);

 private:
  void SetUpOnMainThread() override {
    WebAppBrowserTestBase::SetUpOnMainThread();
  }

  void TearDownOnMainThread() override {
    WebAppBrowserTestBase::TearDownOnMainThread();
  }
};

// Testing url outside the scope of an installed app.
IN_PROC_BROWSER_TEST_F(AlternativeErrorPageOverrideInfoBrowserTest,
                       NoManifest) {
  ASSERT_TRUE(embedded_test_server()->Start());
  const GURL app_url = embedded_test_server()->GetURL("/simple.html");
  content::BrowserContext* context = browser()->profile();

  content::mojom::AlternativeErrorPageOverrideInfoPtr info =
      content::GetContentClientForTesting()
          ->browser()
          ->GetAlternativeErrorPageOverrideInfo(
              app_url, /*render_frame_host=*/nullptr, context,
              net::ERR_INTERNET_DISCONNECTED);

  // Expect mojom struct to be null.
  EXPECT_FALSE(info);
}

// Testing manifest with app short name.
IN_PROC_BROWSER_TEST_F(AlternativeErrorPageOverrideInfoBrowserTest,
                       ManifestWithAppShortName) {
  ASSERT_TRUE(embedded_test_server()->Start());
  content::mojom::AlternativeErrorPageOverrideInfoPtr info = GetErrorPageInfo(
      "/banners/"
      "manifest_test_page.html?manifest=manifest_short_name_only.json");

  // Expect mojom struct with custom app short name.
  EXPECT_TRUE(info);
  EXPECT_EQ(*info->alternative_error_page_params.Find("app_short_name"),
            "Manifest");
}

// Testing app manifest with no app short name.
IN_PROC_BROWSER_TEST_F(AlternativeErrorPageOverrideInfoBrowserTest,
                       ManifestWithNoAppShortName) {
  ASSERT_TRUE(embedded_test_server()->Start());
  content::mojom::AlternativeErrorPageOverrideInfoPtr info = GetErrorPageInfo(
      "/banners/"
      "manifest_test_page.html?manifest=manifest.json");

  // Expect mojom struct with customized with app name.
  EXPECT_TRUE(info);
  EXPECT_EQ(*info->alternative_error_page_params.Find("app_short_name"),
            "Manifest test app");
}

// Testing app manifest with no app short name or app name.
IN_PROC_BROWSER_TEST_F(AlternativeErrorPageOverrideInfoBrowserTest,
                       ManifestWithNoAppShortNameOrAppName) {
  ASSERT_TRUE(embedded_test_server()->Start());
  content::mojom::AlternativeErrorPageOverrideInfoPtr info = GetErrorPageInfo(
      "/banners/"
      "manifest_test_page.html?manifest=manifest_empty_name_short_name.json");

  // Expect mojom struct customized with HTML page title.
  EXPECT_TRUE(info);
  EXPECT_EQ(*info->alternative_error_page_params.Find("app_short_name"),
            "Web app banner test page");
}

// Testing app manifest with no app short name or app name, and HTML page
// has no title
IN_PROC_BROWSER_TEST_F(AlternativeErrorPageOverrideInfoBrowserTest,
                       ManifestWithNoAppShortNameOrAppNameOrTitle) {
  ASSERT_TRUE(embedded_test_server()->Start());
  const GURL app_url = embedded_test_server()->GetURL("/title1.html");
  web_app::NavigateViaLinkClickToURLAndWait(browser(), app_url);
  web_app::test::InstallPwaForCurrentUrl(browser());
  content::BrowserContext* context = browser()->profile();

  content::mojom::AlternativeErrorPageOverrideInfoPtr info =
      content::GetContentClientForTesting()
          ->browser()
          ->GetAlternativeErrorPageOverrideInfo(
              app_url, /*render_frame_host=*/nullptr, context,
              net::ERR_INTERNET_DISCONNECTED);

  // Expect mojom struct customized with HTML page title.
  EXPECT_TRUE(info);
  EXPECT_EQ(*info->alternative_error_page_params.Find("app_short_name"),
            base::UTF16ToUTF8(url_formatter::FormatUrl(app_url)));
}

// Testing manifest with icon.
IN_PROC_BROWSER_TEST_F(AlternativeErrorPageOverrideInfoBrowserTest,
                       ManifestWithIcon) {
  ASSERT_TRUE(embedded_test_server()->Start());
  const GURL app_url = embedded_test_server()->GetURL(
      "/banners/"
      "manifest_test_page.html?manifest=manifest_one_icon.json");
  web_app::NavigateViaLinkClickToURLAndWait(browser(), app_url);
  web_app::test::InstallPwaForCurrentUrl(browser());
  Profile* profile = browser()->profile();
  web_app::WebAppProvider* web_app_provider =
      web_app::WebAppProvider::GetForTest(profile);
  const std::optional<webapps::AppId> app_id =
      web_app_provider->registrar_unsafe().FindBestAppWithUrlInScope(
          app_url,
          web_app::WebAppFilter::InstalledInOperatingSystemForTesting());
  WebAppIconWaiter(profile, app_id.value()).Wait();
  content::mojom::AlternativeErrorPageOverrideInfoPtr info =
      content::GetContentClientForTesting()
          ->browser()
          ->GetAlternativeErrorPageOverrideInfo(
              app_url, /*render_frame_host=*/nullptr, profile,
              net::ERR_INTERNET_DISCONNECTED);

  // Expect mojom struct with everything (except the icon) filled out.
  EXPECT_TRUE(info);
  EXPECT_EQ(*info->alternative_error_page_params.Find("app_short_name"),
            "Manifest test app");
  // This test may at first glance seem like an end-to-end test of the default
  // offline experience, but should be considered more of a unit test for just
  // the initial values provided to the default offline page. For a proper
  // end-to-end test, see WebAppOfflinePageIconShowing in the
  // WebAppOfflinePageTest suite.
  EXPECT_EQ(
      *info->alternative_error_page_params.Find("icon_url"),
      "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACk"
      "lEQVR42mMAAQAABQABoIJXOQAAAABJRU5ErkJggg==");
  EXPECT_EQ(
      *info->alternative_error_page_params.Find("web_app_error_page_message"),
      "You're offline");
  EXPECT_EQ(*info->alternative_error_page_params.Find("supplementary_icon"),
            "offlineIcon");
}