File: webapk_restore_task_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 (257 lines) | stat: -rw-r--r-- 10,355 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
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
// Copyright 2024 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/android/webapk/webapk_restore_task.h"

#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/android/webapk/webapk_install_service_factory.h"
#include "chrome/browser/android/webapk/webapk_restore_manager.h"
#include "chrome/browser/android/webapk/webapk_restore_web_contents_manager.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/test/base/android/android_browser_test.h"
#include "chrome/test/base/chrome_test_utils.h"
#include "components/webapps/browser/android/webapk/webapk_types.h"
#include "components/webapps/browser/web_contents/web_app_url_loader.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace webapk {

class TestWebContentsManager : public WebApkRestoreWebContentsManager {
 public:
  explicit TestWebContentsManager(Profile* profile,
                                  content::WebContents* web_contents)
      : WebApkRestoreWebContentsManager(profile),
        test_web_contents_(web_contents->GetWeakPtr()) {}
  ~TestWebContentsManager() override = default;

  content::WebContents* web_contents() override {
    return test_web_contents_.get();
  }

 private:
  const base::WeakPtr<content::WebContents> test_web_contents_;
};

class WebApkRestoreTaskBrowserTest : public AndroidBrowserTest {
 public:
  WebApkRestoreTaskBrowserTest() = default;

  void SetUpOnMainThread() override {
    embedded_test_server()->ServeFilesFromSourceDirectory(
        "chrome/test/data/banners");
    ASSERT_TRUE(embedded_test_server()->Start());
  }

  Profile* profile() { return chrome_test_utils::GetProfile(this); }
  content::WebContents* web_contents() {
    return chrome_test_utils::GetActiveWebContents(this);
  }
  void OnTaskCompleted(base::OnceClosure done,
                       const GURL& expected_manifest_id,
                       const GURL& manifest_id,
                       webapps::WebApkInstallResult result) {
    EXPECT_EQ(webapps::WebApkInstallResult::SERVER_URL_INVALID, result);
    EXPECT_EQ(expected_manifest_id, manifest_id);
    std::move(done).Run();
  }

  std::unique_ptr<WebApkRestoreWebContentsManager> GetTestWebContentsManager() {
    return std::make_unique<TestWebContentsManager>(profile(), web_contents());
  }
};

IN_PROC_BROWSER_TEST_F(WebApkRestoreTaskBrowserTest, CreateAndRunTasks) {
  base::HistogramTester histogram_tester;
  base::RunLoop run_loop;
  GURL test_url = embedded_test_server()->GetURL("/manifest_test_page.html");

  auto web_contents_manager = GetTestWebContentsManager();

  WebApkRestoreTask task(
      WebApkRestoreManager::PassKeyForTesting(),
      WebApkInstallServiceFactory::GetForBrowserContext(profile()),
      web_contents_manager.get(),
      std::make_unique<webapps::ShortcutInfo>(test_url), base::Time());
  task.Start(base::BindOnce(&WebApkRestoreTaskBrowserTest::OnTaskCompleted,
                            base::Unretained(this), run_loop.QuitClosure(),
                            test_url));
  run_loop.Run();

  EXPECT_EQ(test_url,
            web_contents_manager->web_contents()->GetLastCommittedURL());

  histogram_tester.ExpectUniqueSample(
      "WebApk.Sync.Restore.FetchInstallableData.AppType", 1 /* WEBAPK */, 1);
  histogram_tester.ExpectUniqueSample("WebApk.Sync.Restore.InstallEvent",
                                      0 /* kNone */, 1);
  histogram_tester.ExpectUniqueSample(
      "WebApk.Sync.Restore.InstallResult.Fetched", 4 /* SERVER_URL_INVALID */,
      1);
  histogram_tester.ExpectTotalCount(
      "WebApk.Sync.Restore.InstallResult.Fallback", 0);
}

// TODO(https://crbug.com/370441986): Disabled due to flakiness.
IN_PROC_BROWSER_TEST_F(WebApkRestoreTaskBrowserTest, DISABLED_NoManifestPage) {
  base::HistogramTester histogram_tester;
  base::RunLoop run_loop;
  GURL test_url = embedded_test_server()->GetURL("/no_manifest_test_page.html");

  auto web_contents_manager = GetTestWebContentsManager();

  WebApkRestoreTask task(
      WebApkRestoreManager::PassKeyForTesting(),
      WebApkInstallServiceFactory::GetForBrowserContext(profile()),
      web_contents_manager.get(),
      std::make_unique<webapps::ShortcutInfo>(test_url), base::Time());
  task.Start(base::BindOnce(&WebApkRestoreTaskBrowserTest::OnTaskCompleted,
                            base::Unretained(this), run_loop.QuitClosure(),
                            test_url));
  run_loop.Run();

  EXPECT_EQ(test_url,
            web_contents_manager->web_contents()->GetLastCommittedURL());

  histogram_tester.ExpectUniqueSample(
      "WebApk.Sync.Restore.FetchInstallableData.AppType", 2 /* SHORTCUT */, 1);
  histogram_tester.ExpectUniqueSample("WebApk.Sync.Restore.InstallEvent",
                                      3 /* kNotWebApkCompatible */, 1);
  histogram_tester.ExpectTotalCount("WebApk.Sync.Restore.InstallResult.Fetched",
                                    0);
  histogram_tester.ExpectUniqueSample(
      "WebApk.Sync.Restore.InstallResult.Fallback", 4 /* SERVER_URL_INVALID */,
      1);
}

IN_PROC_BROWSER_TEST_F(WebApkRestoreTaskBrowserTest, StartUrlNotLoadable) {
  base::HistogramTester histogram_tester;
  base::RunLoop run_loop;
  GURL test_url = embedded_test_server()->GetURL("/not_exist.html");

  auto web_contents_manager = GetTestWebContentsManager();

  WebApkRestoreTask task(
      WebApkRestoreManager::PassKeyForTesting(),
      WebApkInstallServiceFactory::GetForBrowserContext(profile()),
      web_contents_manager.get(),
      std::make_unique<webapps::ShortcutInfo>(test_url), base::Time());
  task.Start(base::BindOnce(&WebApkRestoreTaskBrowserTest::OnTaskCompleted,
                            base::Unretained(this), run_loop.QuitClosure(),
                            test_url));
  run_loop.Run();

  EXPECT_EQ(test_url,
            web_contents_manager->web_contents()->GetLastCommittedURL());

  histogram_tester.ExpectUniqueSample("WebApk.Sync.Restore.InstallEvent",
                                      1 /* kLoadUrl */, 1);
  histogram_tester.ExpectTotalCount("WebApk.Sync.Restore.InstallResult.Fetched",
                                    0);
  histogram_tester.ExpectUniqueSample(
      "WebApk.Sync.Restore.InstallResult.Fallback", 4 /* SERVER_URL_INVALID */,
      1);
}

// TODO(crbug.com/379851961): The test is flaky.
IN_PROC_BROWSER_TEST_F(WebApkRestoreTaskBrowserTest,
                       DISABLED_ManifestIdMismatch) {
  base::HistogramTester histogram_tester;
  base::RunLoop run_loop;
  GURL test_url = embedded_test_server()->GetURL("/manifest_test_page.html");
  GURL test_manifest_id = GURL("https://example.com");

  auto test_shortcut_info = std::make_unique<webapps::ShortcutInfo>(test_url);
  test_shortcut_info->manifest_id = test_manifest_id;

  auto web_contents_manager = GetTestWebContentsManager();

  WebApkRestoreTask task(
      WebApkRestoreManager::PassKeyForTesting(),
      WebApkInstallServiceFactory::GetForBrowserContext(profile()),
      web_contents_manager.get(), std::move(test_shortcut_info), base::Time());
  task.Start(base::BindOnce(&WebApkRestoreTaskBrowserTest::OnTaskCompleted,
                            base::Unretained(this), run_loop.QuitClosure(),
                            test_manifest_id));
  run_loop.Run();

  EXPECT_EQ(test_url,
            web_contents_manager->web_contents()->GetLastCommittedURL());

  histogram_tester.ExpectUniqueSample(
      "WebApk.Sync.Restore.FetchInstallableData.AppType", 1 /* WEBAPK */, 1);
  histogram_tester.ExpectUniqueSample("WebApk.Sync.Restore.InstallEvent",
                                      2 /* kManifestIdMismatch */, 1);
  histogram_tester.ExpectTotalCount("WebApk.Sync.Restore.InstallResult.Fetched",
                                    0);
  histogram_tester.ExpectUniqueSample(
      "WebApk.Sync.Restore.InstallResult.Fallback", 4 /* SERVER_URL_INVALID */,
      1);
}

IN_PROC_BROWSER_TEST_F(WebApkRestoreTaskBrowserTest, DownloadIcon) {
  auto web_contents_manager = GetTestWebContentsManager();

  auto test_shortcut_info = std::make_unique<webapps::ShortcutInfo>(
      embedded_test_server()->GetURL("/manifest_test_page.html"));
  test_shortcut_info->best_primary_icon_url =
      embedded_test_server()->GetURL("/256x256-green.png");

  WebApkRestoreTask task(
      WebApkRestoreManager::PassKeyForTesting(),
      WebApkInstallServiceFactory::GetForBrowserContext(profile()),
      web_contents_manager.get(), std::move(test_shortcut_info), base::Time());

  base::RunLoop run_loop;
  task.DownloadIcon(run_loop.QuitClosure());
  run_loop.Run();

  EXPECT_FALSE(task.app_icon().drawsNothing());
  EXPECT_NE(task.app_icon().width(), 256);
}

IN_PROC_BROWSER_TEST_F(WebApkRestoreTaskBrowserTest, DownloadIconNoIconUrl) {
  auto web_contents_manager = GetTestWebContentsManager();

  auto test_shortcut_info = std::make_unique<webapps::ShortcutInfo>(
      embedded_test_server()->GetURL("/manifest_test_page.html"));
  test_shortcut_info->best_primary_icon_url = GURL();

  WebApkRestoreTask task(
      WebApkRestoreManager::PassKeyForTesting(),
      WebApkInstallServiceFactory::GetForBrowserContext(profile()),
      web_contents_manager.get(), std::move(test_shortcut_info), base::Time());

  base::RunLoop run_loop;
  task.DownloadIcon(run_loop.QuitClosure());
  run_loop.Run();

  EXPECT_FALSE(task.app_icon().drawsNothing());
}

IN_PROC_BROWSER_TEST_F(WebApkRestoreTaskBrowserTest, DownloadIconBadIcon) {
  auto web_contents_manager = GetTestWebContentsManager();

  auto test_shortcut_info = std::make_unique<webapps::ShortcutInfo>(
      embedded_test_server()->GetURL("/manifest_test_page.html"));
  test_shortcut_info->best_primary_icon_url =
      embedded_test_server()->GetURL("/bad_icon.png");

  WebApkRestoreTask task(
      WebApkRestoreManager::PassKeyForTesting(),
      WebApkInstallServiceFactory::GetForBrowserContext(profile()),
      web_contents_manager.get(), std::move(test_shortcut_info), base::Time());

  base::RunLoop run_loop;
  task.DownloadIcon(run_loop.QuitClosure());
  run_loop.Run();

  EXPECT_FALSE(task.app_icon().drawsNothing());
}

}  // namespace webapk