File: webapk_manager_unittest.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; 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 (367 lines) | stat: -rw-r--r-- 13,334 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// 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 "chrome/browser/apps/app_service/webapk/webapk_manager.h"

#include <memory>
#include <vector>

#include "ash/constants/ash_features.h"
#include "base/strings/strcat.h"
#include "base/test/metrics/histogram_tester.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/app_service/app_service_test.h"
#include "chrome/browser/apps/app_service/publishers/arc_apps.h"
#include "chrome/browser/apps/app_service/webapk/webapk_install_queue.h"
#include "chrome/browser/apps/app_service/webapk/webapk_install_task.h"
#include "chrome/browser/apps/app_service/webapk/webapk_metrics.h"
#include "chrome/browser/apps/app_service/webapk/webapk_prefs.h"
#include "chrome/browser/ash/app_list/arc/arc_app_list_prefs.h"
#include "chrome/browser/ash/app_list/arc/arc_app_test.h"
#include "chrome/browser/ash/arc/arc_util.h"
#include "chrome/browser/web_applications/test/fake_web_app_provider.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/test/base/testing_profile.h"
#include "chromeos/ash/experiences/arc/mojom/app.mojom.h"
#include "chromeos/ash/experiences/arc/test/fake_app_instance.h"
#include "components/prefs/pref_service.h"
#include "components/services/app_service/public/cpp/app_registry_cache.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/app_update.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace {

constexpr char kTestAppUrl[] = "https://www.example.com/";
constexpr char kTestAppActionUrl[] = "https://www.example.com/share";
constexpr char kTestManifestUrl[] = "https://www.example.com/manifest.json";
constexpr char kTestShareTextParam[] = "share_text";
constexpr char kTestWebApkPackageName[] = "org.chromium.webapk.some_package";
const std::u16string kTestAppTitle = u"Test App";

std::unique_ptr<web_app::WebAppInstallInfo> BuildDefaultWebAppInfo(
    GURL app_url = GURL(kTestAppUrl)) {
  auto app_info =
      web_app::WebAppInstallInfo::CreateWithStartUrlForTesting(app_url);
  app_info->scope = GURL(kTestAppUrl);
  app_info->title = kTestAppTitle;
  app_info->manifest_url = GURL(kTestManifestUrl);

  apps::ShareTarget target;
  target.action = GURL(kTestAppActionUrl);
  target.method = apps::ShareTarget::Method::kPost;
  target.enctype = apps::ShareTarget::Enctype::kMultipartFormData;
  target.params.text = kTestShareTextParam;
  app_info->share_target = target;

  return app_info;
}

arc::mojom::ArcPackageInfoPtr GetArcPackage(const std::string& package_name) {
  auto package = arc::mojom::ArcPackageInfo::New();
  package->package_name = package_name;
  return package;
}

}  // namespace

class WebApkManagerTest : public apps::AppRegistryCache::Observer,
                          public testing::Test {
 public:
  WebApkManagerTest() = default;

  void SetUp() override {
    testing::Test::SetUp();
    web_app::test::AwaitStartWebAppProviderAndSubsystems(profile());
  }

  void TearDown() override { arc_test_.TearDown(); }

  void StartWebApkManager() {
    app_service_test_.SetUp(&profile_);
    // This starts the ArcApps publisher, which owns the WebApkManager.
    arc_test_.SetUp(&profile_);
  }

  void AssertNoPendingInstalls() {
    ASSERT_FALSE(webapk_manager()->GetInstallQueueForTest()->PopTaskForTest());
  }

  bool IsAppInstalled(const std::string& app_id) {
    bool installed = false;
    app_service_proxy()->AppRegistryCache().ForOneApp(
        app_id, [&](const apps::AppUpdate& app) {
          installed = app.Readiness() == apps::Readiness::kReady;
        });
    return installed;
  }

  void WaitForAppUninstall(const std::string& app_id) {
    app_registry_cache_observer_.Observe(
        &(app_service_proxy()->AppRegistryCache()));
    app_id_ = app_id;
    base::RunLoop run_loop;
    quit_callback_ = run_loop.QuitClosure();
    run_loop.Run();
  }

  // apps::AppRegistryCache::Observer overrides.
  void OnAppUpdate(const apps::AppUpdate& update) override {
    if (app_id_.empty()) {
      return;
    }

    if (app_id_ == update.AppId() &&
        update.Readiness() == apps::Readiness::kUninstalledByUser &&
        !quit_callback_.is_null()) {
      std::move(quit_callback_).Run();
      app_registry_cache_observer_.Reset();
    }
  }

  void OnAppRegistryCacheWillBeDestroyed(
      apps::AppRegistryCache* cache) override {
    app_registry_cache_observer_.Reset();
  }

  TestingProfile* profile() { return &profile_; }
  apps::AppServiceTest* app_service_test() { return &app_service_test_; }
  apps::WebApkManager* webapk_manager() {
    return apps::ArcApps::Get(profile())->GetWebApkManagerForTesting();
  }
  ArcAppTest* arc_test() { return &arc_test_; }
  apps::AppServiceProxyBase* app_service_proxy() {
    return apps::AppServiceProxyFactory::GetForProfile(profile());
  }

 private:
  content::BrowserTaskEnvironment task_environment_;
  TestingProfile profile_;
  ArcAppTest arc_test_;
  apps::AppServiceTest app_service_test_;
  std::string app_id_;
  base::OnceClosure quit_callback_;

  base::ScopedObservation<apps::AppRegistryCache,
                          apps::AppRegistryCache::Observer>
      app_registry_cache_observer_{this};
};

TEST_F(WebApkManagerTest, InstallsWebApkOnStartup) {
  auto app_id =
      web_app::test::InstallWebApp(profile(), BuildDefaultWebAppInfo());

  StartWebApkManager();

  auto install_task =
      webapk_manager()->GetInstallQueueForTest()->PopTaskForTest();
  ASSERT_TRUE(install_task);
  ASSERT_EQ(install_task->app_id(), app_id);
  AssertNoPendingInstalls();
}

TEST_F(WebApkManagerTest, InstallWebApkAfterStartup) {
  StartWebApkManager();
  AssertNoPendingInstalls();

  auto app_id =
      web_app::test::InstallWebApp(profile(), BuildDefaultWebAppInfo());

  auto install_task =
      webapk_manager()->GetInstallQueueForTest()->PopTaskForTest();
  ASSERT_TRUE(install_task);
  ASSERT_EQ(install_task->app_id(), app_id);
  AssertNoPendingInstalls();
}

// Does not install web apps without a Share Target definition.
TEST_F(WebApkManagerTest, NoShareTarget) {
  auto app_info = web_app::WebAppInstallInfo::CreateWithStartUrlForTesting(
      GURL(kTestAppUrl));
  app_info->title = kTestAppTitle;
  auto app_id = web_app::test::InstallWebApp(profile(), std::move(app_info));

  StartWebApkManager();

  AssertNoPendingInstalls();
}

// When two eligible apps are available during startup, but one of them already
// has a WebAPK installed, only install a new WebAPK for the other app.
TEST_F(WebApkManagerTest, IgnoresAlreadyInstalledWebApkOnStartup) {
  auto app_info_1 = BuildDefaultWebAppInfo();
  // Change the manifest_id so that the two apps have different IDs.
  auto app_info_2 =
      BuildDefaultWebAppInfo(GURL(base::StrCat({kTestAppUrl, "/app_2"})));

  auto app_id_1 =
      web_app::test::InstallWebApp(profile(), std::move(app_info_1));
  auto app_id_2 =
      web_app::test::InstallWebApp(profile(), std::move(app_info_2));
  apps::webapk_prefs::AddWebApk(profile(), app_id_1,
                                "org.chromium.webapk.some_package");

  StartWebApkManager();

  auto install_task =
      webapk_manager()->GetInstallQueueForTest()->PopTaskForTest();
  ASSERT_TRUE(install_task);
  ASSERT_EQ(install_task->app_id(), app_id_2);
  AssertNoPendingInstalls();
}

TEST_F(WebApkManagerTest, RemovesIneligibleWebApkOnStartup) {
  auto app_info = web_app::WebAppInstallInfo::CreateWithStartUrlForTesting(
      GURL(kTestAppUrl));
  app_info->title = kTestAppTitle;
  auto app_id = web_app::test::InstallWebApp(profile(), std::move(app_info));

  // Add the app to prefs manually, as if this app was previously installed as a
  // webapk.
  apps::webapk_prefs::AddWebApk(profile(), app_id, kTestWebApkPackageName);

  StartWebApkManager();
  arc_test()->app_instance()->SendRefreshPackageList({});

  // The WebAPK should have been uninstalled, but the app itself is still
  // installed.
  ASSERT_FALSE(apps::webapk_prefs::GetWebApkPackageName(profile(), app_id));
  ASSERT_TRUE(IsAppInstalled(app_id));
}

TEST_F(WebApkManagerTest, RemovesUninstalledAppOnStartup) {
  std::string app_id = "foobar";
  apps::webapk_prefs::AddWebApk(profile(), app_id, kTestWebApkPackageName);
  StartWebApkManager();
  arc_test()->app_instance()->SendRefreshPackageList({});
  ASSERT_FALSE(apps::webapk_prefs::GetWebApkPackageName(profile(), app_id));
}

TEST_F(WebApkManagerTest, RemovesAppUninstalledFromChrome) {
  auto app_id =
      web_app::test::InstallWebApp(profile(), BuildDefaultWebAppInfo());
  apps::webapk_prefs::AddWebApk(profile(), app_id, kTestWebApkPackageName);
  StartWebApkManager();
  arc_test()->app_instance()->SendRefreshPackageList({});

  app_service_proxy()->UninstallSilently(app_id,
                                         apps::UninstallSource::kUnknown);

  // Wait for the async web app uninstallation.
  WaitForAppUninstall(app_id);

  ASSERT_FALSE(apps::webapk_prefs::GetWebApkPackageName(profile(), app_id));
}

TEST_F(WebApkManagerTest, QueuesUpdatedApp) {
  auto app_id =
      web_app::test::InstallWebApp(profile(), BuildDefaultWebAppInfo());
  apps::webapk_prefs::AddWebApk(profile(), app_id, kTestWebApkPackageName);

  StartWebApkManager();

  // Mimic updating an app by reinstalling it with a different WebAppInfo.
  auto updated_app_info = BuildDefaultWebAppInfo();
  updated_app_info->title = u"Some new title";
  auto updated_app_id =
      web_app::test::InstallWebApp(profile(), std::move(updated_app_info),
                                   /*overwrite_existing_manifest_fields=*/true);
  EXPECT_EQ(app_id, updated_app_id);

  auto install_task =
      webapk_manager()->GetInstallQueueForTest()->PopTaskForTest();
  ASSERT_TRUE(install_task);
  ASSERT_EQ(install_task->app_id(), app_id);
  AssertNoPendingInstalls();

  base::flat_set<std::string> updating_apps =
      apps::webapk_prefs::GetUpdateNeededAppIds(profile());
  ASSERT_THAT(updating_apps, testing::ElementsAre(app_id));
}

TEST_F(WebApkManagerTest, QueuesPendingUpdateOnStartup) {
  auto app_info_1 = BuildDefaultWebAppInfo();
  // Change the manifest_id so that the two apps have different IDs.
  auto app_info_2 =
      BuildDefaultWebAppInfo(GURL(base::StrCat({kTestAppUrl, "/app_2"})));

  auto app_id_1 =
      web_app::test::InstallWebApp(profile(), std::move(app_info_1));
  apps::webapk_prefs::AddWebApk(profile(), app_id_1, kTestWebApkPackageName);
  apps::webapk_prefs::SetUpdateNeededForApp(profile(), app_id_1,
                                            /* update_needed= */ true);
  auto app_id_2 =
      web_app::test::InstallWebApp(profile(), std::move(app_info_2));
  apps::webapk_prefs::AddWebApk(profile(), app_id_2, kTestWebApkPackageName);
  apps::webapk_prefs::SetUpdateNeededForApp(profile(), app_id_2,
                                            /* update_needed= */ false);

  StartWebApkManager();

  // App 1 has a pending update, app 2 does not. Only app 1 should be queued.
  auto install_task =
      webapk_manager()->GetInstallQueueForTest()->PopTaskForTest();
  ASSERT_TRUE(install_task);
  ASSERT_EQ(install_task->app_id(), app_id_1);
  AssertNoPendingInstalls();
}

TEST_F(WebApkManagerTest, IgnoresInstallsWhilePlayStoreDisabled) {
  StartWebApkManager();

  arc::SetArcPlayStoreEnabledForProfile(profile(), /*enabled=*/false);

  auto app_id =
      web_app::test::InstallWebApp(profile(), BuildDefaultWebAppInfo());

  AssertNoPendingInstalls();
}

TEST_F(WebApkManagerTest, IgnoresInstallsWhilePolicyDisabled) {
  StartWebApkManager();
  profile()->GetPrefs()->SetBoolean(
      apps::webapk_prefs::kGeneratedWebApksEnabled, false);

  auto app_id =
      web_app::test::InstallWebApp(profile(), BuildDefaultWebAppInfo());

  AssertNoPendingInstalls();
}

TEST_F(WebApkManagerTest, RemovesWebApksWhenPolicyDisabled) {
  auto app_id =
      web_app::test::InstallWebApp(profile(), BuildDefaultWebAppInfo());
  apps::webapk_prefs::AddWebApk(profile(), app_id, kTestWebApkPackageName);

  StartWebApkManager();
  arc_test()->app_instance()->SendRefreshPackageList({});

  profile()->GetPrefs()->SetBoolean(
      apps::webapk_prefs::kGeneratedWebApksEnabled, false);

  ASSERT_THAT(apps::webapk_prefs::GetWebApkAppIds(profile()),
              testing::IsEmpty());
}

TEST_F(WebApkManagerTest, RemovesUntrackedInstalledWebApk) {
  std::vector<arc::mojom::ArcPackageInfoPtr> packages;
  packages.push_back(GetArcPackage("org.chromium.webapk.package1"));
  packages.push_back(GetArcPackage("org.chromium.webapk.package2"));

  auto app_id =
      web_app::test::InstallWebApp(profile(), BuildDefaultWebAppInfo());
  apps::webapk_prefs::AddWebApk(profile(), app_id,
                                "org.chromium.webapk.package1");
  StartWebApkManager();

  arc_test()->app_instance()->SendRefreshPackageList(std::move(packages));

  ASSERT_TRUE(ArcAppListPrefs::Get(profile())->GetPackage(
      "org.chromium.webapk.package1"));
  ASSERT_FALSE(ArcAppListPrefs::Get(profile())->GetPackage(
      "org.chromium.webapk.package2"));
}