File: webapk_restore_manager_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 (243 lines) | stat: -rw-r--r-- 9,595 bytes parent folder | download | duplicates (5)
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
// 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_manager.h"

#include "base/functional/callback.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/task_environment.h"
#include "base/types/pass_key.h"
#include "chrome/browser/android/webapk/webapk_helpers.h"
#include "chrome/browser/android/webapk/webapk_install_service_factory.h"
#include "chrome/browser/android/webapk/webapk_restore_task.h"
#include "chrome/browser/android/webapk/webapk_restore_web_contents_manager.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/webapps/browser/android/webapk/webapk_types.h"
#include "components/webapps/browser/web_contents/web_app_url_loader.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace webapk {

std::vector<WebApkRestoreData> CreateRestoreAppsList(
    const std::vector<GURL>& manifest_ids) {
  std::vector<WebApkRestoreData> apps;
  for (const auto& manifest_id : manifest_ids) {
    auto shortcut_info = std::make_unique<webapps::ShortcutInfo>(manifest_id);
    apps.emplace_back(
        WebApkRestoreData(GenerateAppIdFromManifestId(manifest_id),
                          std::move(shortcut_info), base::Time()));
  }
  return apps;
}

// A mock WebApkRestoreTask that does nothing but run the complete callback when
// starts.
class MockWebApkRestoreTask : public WebApkRestoreTask {
 public:
  explicit MockWebApkRestoreTask(
      base::PassKey<WebApkRestoreManager> pass_key,
      WebApkInstallService* web_apk_install_service,
      WebApkRestoreWebContentsManager* web_contents_manager,
      std::unique_ptr<webapps::ShortcutInfo> shortcut_info,
      base::Time last_used_time,
      std::vector<std::pair<GURL, std::string>>* task_log)
      : WebApkRestoreTask(pass_key,
                          web_apk_install_service,
                          web_contents_manager,
                          std::move(shortcut_info),
                          last_used_time),
        task_log_(task_log) {}
  ~MockWebApkRestoreTask() override = default;

  void DownloadIcon(base::OnceClosure done_closure) {
    std::move(done_closure).Run();
  }

  void Start(CompleteCallback complete_callback) override {
    task_log_->emplace_back(manifest_id(), "Start");

    base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, base::BindOnce(std::move(complete_callback), manifest_id(),
                                  webapps::WebApkInstallResult::SUCCESS));
  }

 private:
  raw_ptr<std::vector<std::pair<GURL, std::string>>> task_log_;
};

class TestWebContentsManager : public WebApkRestoreWebContentsManager {
 public:
  explicit TestWebContentsManager(Profile* profile)
      : WebApkRestoreWebContentsManager(profile) {}
  ~TestWebContentsManager() override = default;

  void LoadUrl(const GURL& url,
               webapps::WebAppUrlLoader::ResultCallback callback) override {
    std::move(callback).Run(webapps::WebAppUrlLoaderResult::kUrlLoaded);
  }
};

class TestWebApkRestoreManager : public WebApkRestoreManager {
 public:
  explicit TestWebApkRestoreManager(Profile* profile)
      : WebApkRestoreManager(
            WebApkInstallServiceFactory::GetForBrowserContext(profile),
            std::make_unique<TestWebContentsManager>(profile)),
        web_apk_install_service_(
            WebApkInstallServiceFactory::GetForBrowserContext(profile)) {}
  ~TestWebApkRestoreManager() override = default;

  void PrepareTasksFinish(base::OnceClosure on_task_finish) {
    on_task_finish_ = std::move(on_task_finish);
  }

  void OnTaskFinished(const GURL& manifest_id,
                      webapps::WebApkInstallResult result) override {
    task_log_.emplace_back(manifest_id, "Finish");
    WebApkRestoreManager::OnTaskFinished(manifest_id, result);

    if (GetTasksCountForTesting() == 0u) {
      std::move(on_task_finish_).Run();
    }
  }

  std::vector<std::pair<GURL, std::string>> task_log() { return task_log_; }

 private:
  // Mock CreateNewTask to create the Mock task instead.
  std::unique_ptr<WebApkRestoreTask> CreateNewTask(
      std::unique_ptr<webapps::ShortcutInfo> shortcut_info,
      base::Time last_used_time) override {
    task_log_.emplace_back(shortcut_info->manifest_id, "Create");
    return std::make_unique<MockWebApkRestoreTask>(
        WebApkRestoreManager::PassKeyForTesting(), web_apk_install_service_,
        web_contents_manager(), std::move(shortcut_info), last_used_time,
        &task_log_);
  }

  const raw_ptr<WebApkInstallService> web_apk_install_service_;
  base::OnceClosure on_task_finish_;
  std::vector<std::pair<GURL, std::string>> task_log_;
};

class WebApkRestoreManagerTest : public ::testing::Test {
 public:
  WebApkRestoreManagerTest()
      : task_environment_(content::BrowserTaskEnvironment::IO_MAINLOOP) {}

  void SetUp() override {
    ASSERT_TRUE(testing_profile_manager_.SetUp());
    profile_ = testing_profile_manager_.CreateTestingProfile(
        TestingProfile::kDefaultProfileUserName);
  }

  TestingProfile* profile() { return profile_.get(); }

 protected:
  const GURL kManifestId1 = GURL("https://example.com/app1");
  const GURL kManifestId2 = GURL("https://example.com/app2");
  const GURL kManifestId3 = GURL("https://example.com/app3");
  const webapps::AppId kAppId1 = GenerateAppIdFromManifestId(kManifestId1);
  const webapps::AppId kAppId2 = GenerateAppIdFromManifestId(kManifestId2);
  const webapps::AppId kAppId3 = GenerateAppIdFromManifestId(kManifestId3);

 private:
  content::BrowserTaskEnvironment task_environment_;
  TestingProfileManager testing_profile_manager_{
      TestingBrowserProcess::GetGlobal()};
  raw_ptr<TestingProfile> profile_;
};

TEST_F(WebApkRestoreManagerTest, GetAppResults) {
  std::unique_ptr<TestWebApkRestoreManager> manager =
      std::make_unique<TestWebApkRestoreManager>(profile());

  std::vector<WebApkRestoreData> apps;
  auto shortcut_info_1 = std::make_unique<webapps::ShortcutInfo>(kManifestId1);
  shortcut_info_1->name = u"app1";
  auto shortcut_info_2 = std::make_unique<webapps::ShortcutInfo>(kManifestId2);
  shortcut_info_2->name = u"app2";
  apps.emplace_back(
      WebApkRestoreData(kAppId1, std::move(shortcut_info_1), base::Time()));
  apps.emplace_back(
      WebApkRestoreData(kAppId2, std::move(shortcut_info_2), base::Time()));

  base::RunLoop run_loop;
  manager->PrepareRestorableApps(
      std::move(apps),
      base::BindLambdaForTesting([&](const std::vector<std::string>& ids,
                                     const std::vector<std::u16string>& names,
                                     const std::vector<int>& last_used_in_days,
                                     const std::vector<SkBitmap>& icons) {
        EXPECT_THAT(ids, testing::ElementsAre(
                             GenerateAppIdFromManifestId(kManifestId1),
                             GenerateAppIdFromManifestId(kManifestId2)));
        EXPECT_THAT(names, testing::ElementsAre(u"app1", u"app2"));
        run_loop.Quit();
      }));
  run_loop.Run();

}

TEST_F(WebApkRestoreManagerTest, RunOneTasks) {
  std::unique_ptr<TestWebApkRestoreManager> manager =
      std::make_unique<TestWebApkRestoreManager>(profile());

  auto apps = CreateRestoreAppsList({kManifestId1});
  manager->PrepareRestorableApps(std::move(apps), base::DoNothing());

  base::RunLoop run_loop;
  manager->PrepareTasksFinish(run_loop.QuitClosure());
  manager->ScheduleRestoreTasks({kAppId1});
  run_loop.Run();

  EXPECT_THAT(manager->task_log(),
              testing::ElementsAre(std::pair(kManifestId1, "Create"),
                                   std::pair(kManifestId1, "Start"),
                                   std::pair(kManifestId1, "Finish")));
}

// Schedule multiple tasks and they run in sequence.
TEST_F(WebApkRestoreManagerTest, ScheduleMultipleTasks) {
  std::unique_ptr<TestWebApkRestoreManager> manager =
      std::make_unique<TestWebApkRestoreManager>(profile());

  auto apps = CreateRestoreAppsList({kManifestId1, kManifestId2, kManifestId3});
  manager->PrepareRestorableApps(std::move(apps), base::DoNothing());

  base::RunLoop run_loop;
  manager->PrepareTasksFinish(run_loop.QuitClosure());
  manager->ScheduleRestoreTasks({kAppId1, kAppId2});
  run_loop.Run();

  // Tests are created when added to the queue. The second task only start until
  // the first one finished.
  EXPECT_THAT(
      manager->task_log(),
      testing::ElementsAre(
          std::pair(kManifestId1, "Create"), std::pair(kManifestId2, "Create"),
          std::pair(kManifestId3, "Create"), std::pair(kManifestId1, "Start"),
          std::pair(kManifestId1, "Finish"), std::pair(kManifestId2, "Start"),
          std::pair(kManifestId2, "Finish")));

  base::RunLoop run_loop2;
  manager->PrepareTasksFinish(run_loop2.QuitClosure());
  manager->ScheduleRestoreTasks({kAppId3});
  run_loop2.Run();

  EXPECT_THAT(
      manager->task_log(),
      testing::ElementsAre(
          std::pair(kManifestId1, "Create"), std::pair(kManifestId2, "Create"),
          std::pair(kManifestId3, "Create"), std::pair(kManifestId1, "Start"),
          std::pair(kManifestId1, "Finish"), std::pair(kManifestId2, "Start"),
          std::pair(kManifestId2, "Finish"), std::pair(kManifestId3, "Start"),
          std::pair(kManifestId3, "Finish")));
}

}  // namespace webapk