File: publisher_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 (675 lines) | stat: -rw-r--r-- 28,150 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
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
// 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 <utility>
#include <vector>

#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation_traits.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "base/values.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/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_service_test_base.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/web_applications/externally_managed_app_manager.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom.h"
#include "chrome/browser/web_applications/test/fake_web_app_provider.h"
#include "chrome/browser/web_applications/test/test_web_app_url_loader.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/common/chrome_features.h"
#include "chrome/test/base/testing_profile.h"
#include "components/account_id/account_id.h"
#include "components/app_constants/constants.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/features.h"
#include "components/services/app_service/public/cpp/icon_types.h"
#include "components/services/app_service/public/cpp/intent_util.h"
#include "components/services/app_service/public/cpp/permission.h"
#include "extensions/browser/extension_registrar.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/gurl.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "ash/constants/ash_features.h"
#include "chrome/browser/apps/app_service/publishers/arc_apps.h"
#include "chrome/browser/apps/app_service/publishers/arc_apps_factory.h"
#include "chrome/browser/ash/app_list/arc/arc_app_test.h"
#include "chrome/browser/ash/borealis/borealis_util.h"
#include "chrome/browser/ash/login/users/fake_chrome_user_manager.h"
#include "chrome/common/chrome_features.h"
#include "chrome/grit/branded_strings.h"
#include "chromeos/ash/components/login/login_state/login_state.h"
#include "chromeos/ash/experiences/arc/test/fake_app_instance.h"
#include "components/services/app_service/public/cpp/app_capability_access_cache.h"
#include "components/services/app_service/public/cpp/capability_access_update.h"
#include "components/user_manager/scoped_user_manager.h"
#include "ui/base/l10n/l10n_util.h"

#endif  // BUILDFLAG(IS_CHROMEOS)

namespace {

const base::Time kLastLaunchTime = base::Time::Now();
const base::Time kInstallTime = base::Time::Now();
const char kUrl[] = "https://example.com/";

scoped_refptr<extensions::Extension> MakeExtensionApp(
    const std::string& name,
    const std::string& version,
    const std::string& url,
    const std::string& id) {
  std::string err;
  base::Value::Dict value;
  value.Set("name", name);
  value.Set("version", version);
  base::Value::List scripts;
  scripts.Append("script.js");
  value.SetByDottedPath("app.background.scripts", std::move(scripts));
  scoped_refptr<extensions::Extension> app = extensions::Extension::Create(
      base::FilePath(), extensions::mojom::ManifestLocation::kInternal, value,
      extensions::Extension::WAS_INSTALLED_BY_DEFAULT, id, &err);
  EXPECT_EQ(err, "");
  return app;
}

#if BUILDFLAG(IS_CHROMEOS)
void AddArcPackage(ArcAppTest& arc_test,
                   const std::vector<arc::mojom::AppInfoPtr>& fake_apps) {
  for (const auto& fake_app : fake_apps) {
    base::flat_map<arc::mojom::AppPermission, arc::mojom::PermissionStatePtr>
        permissions;
    permissions.emplace(arc::mojom::AppPermission::CAMERA,
                        arc::mojom::PermissionState::New(/*granted=*/false,
                                                         /*managed=*/false));
    permissions.emplace(arc::mojom::AppPermission::LOCATION,
                        arc::mojom::PermissionState::New(/*granted=*/true,
                                                         /*managed=*/false));
    arc::mojom::ArcPackageInfoPtr package = arc::mojom::ArcPackageInfo::New(
        fake_app->package_name, /*package_version=*/1,
        /*last_backup_android_id=*/1,
        /*last_backup_time=*/1, /*sync=*/true, /*system=*/false,
        /*vpn_provider=*/false, /*web_app_info=*/nullptr, std::nullopt,
        std::move(permissions));
    arc_test.AddPackage(package->Clone());
    arc_test.app_instance()->SendPackageAdded(package->Clone());
  }
}

apps::Permissions MakeFakePermissions() {
  apps::Permissions permissions;
  permissions.push_back(std::make_unique<apps::Permission>(
      apps::PermissionType::kCamera, apps::TriState::kBlock,
      /*is_managed*/ false));
  permissions.push_back(std::make_unique<apps::Permission>(
      apps::PermissionType::kLocation, apps::TriState::kAllow,
      /*is_managed*/ false));
  return permissions;
}
#endif  // BUILDFLAG(IS_CHROMEOS)

apps::IntentFilters CreateIntentFilters() {
  const GURL url(kUrl);
  apps::IntentFilters filters;
  apps::IntentFilterPtr filter = std::make_unique<apps::IntentFilter>();

  apps::ConditionValues values1;
  values1.push_back(std::make_unique<apps::ConditionValue>(
      apps_util::kIntentActionView, apps::PatternMatchType::kLiteral));
  filter->conditions.push_back(std::make_unique<apps::Condition>(
      apps::ConditionType::kAction, std::move(values1)));

  apps::ConditionValues values2;
  values2.push_back(std::make_unique<apps::ConditionValue>(
      url.scheme(), apps::PatternMatchType::kLiteral));
  filter->conditions.push_back(std::make_unique<apps::Condition>(
      apps::ConditionType::kScheme, std::move(values2)));

  apps::ConditionValues values3;
  values3.push_back(std::make_unique<apps::ConditionValue>(
      apps_util::AuthorityView::Encode(url), apps::PatternMatchType::kLiteral));
  filter->conditions.push_back(std::make_unique<apps::Condition>(
      apps::ConditionType::kAuthority, std::move(values3)));

  apps::ConditionValues values4;
  values4.push_back(std::make_unique<apps::ConditionValue>(
      url.path(), apps::PatternMatchType::kPrefix));
  filter->conditions.push_back(std::make_unique<apps::Condition>(
      apps::ConditionType::kPath, std::move(values4)));

  filters.push_back(std::move(filter));

  return filters;
}

MATCHER(Ready, "App has readiness=\"kReady\"") {
  return arg.readiness == apps::Readiness::kReady;
}

MATCHER_P(ShownInShelf, shown, "App shown on the shelf") {
  return arg.show_in_shelf.has_value() && arg.show_in_shelf == shown;
}

MATCHER_P(ShownInLauncher, shown, "App shown in the launcher") {
  return arg.show_in_launcher.has_value() && arg.show_in_launcher == shown;
}

#if BUILDFLAG(IS_CHROMEOS)
arc::mojom::PrivacyItemPtr CreateArcPrivacyItem(
    arc::mojom::AppPermissionGroup permission,
    const std::string& package_name) {
  arc::mojom::PrivacyItemPtr item = arc::mojom::PrivacyItem::New();
  item->permission_group = permission;
  item->privacy_application = arc::mojom::PrivacyApplication::New();
  item->privacy_application->package_name = package_name;
  return item;
}
#endif  // BUILDFLAG(IS_CHROMEOS)

// AppRegistryCacheObserver is used to test the OnAppTypeInitialized and
// OnAppUpdate interfaces for AppRegistryCache::Observer.
class AppRegistryCacheObserver : public apps::AppRegistryCache::Observer {
 public:
  explicit AppRegistryCacheObserver(apps::AppRegistryCache* cache) {
    app_registry_cache_observer_.Observe(cache);
  }

  ~AppRegistryCacheObserver() override = default;

  // apps::AppRegistryCache::Observer overrides.
  void OnAppUpdate(const apps::AppUpdate& update) override {
    updated_ids_.push_back(update.AppId());
  }

  void OnAppTypeInitialized(apps::AppType app_type) override {
    app_types_.push_back(app_type);
  }

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

  std::vector<std::string> updated_ids() const { return updated_ids_; }
  std::vector<apps::AppType> app_types() const { return app_types_; }

 private:
  std::vector<std::string> updated_ids_;
  std::vector<apps::AppType> app_types_;

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

}  // namespace

namespace apps {

class PublisherTest : public extensions::ExtensionServiceTestBase {
 public:
  PublisherTest() = default;
  PublisherTest(const PublisherTest&) = delete;
  PublisherTest& operator=(const PublisherTest&) = delete;

  ~PublisherTest() override = default;

  // ExtensionServiceTestBase:
  void SetUp() override {
    extensions::ExtensionServiceTestBase::SetUp();
    InitializeExtensionService(ExtensionServiceInitParams());
    service()->Init();
    ConfigureWebAppProvider();
#if BUILDFLAG(IS_CHROMEOS)
    ash::LoginState::Initialize();
#endif  // BUILDFLAG(IS_CHROMEOS)
  }

  void TearDown() override {
    extensions::ExtensionServiceTestBase::TearDown();
    profile_.reset();
#if BUILDFLAG(IS_CHROMEOS)
    ash::LoginState::Shutdown();
#endif  // BUILDFLAG(IS_CHROMEOS)
  }

  void ConfigureWebAppProvider() {
    auto url_loader = std::make_unique<web_app::TestWebAppUrlLoader>();

    auto externally_managed_app_manager =
        std::make_unique<web_app::ExternallyManagedAppManager>(profile());
    externally_managed_app_manager->SetUrlLoaderForTesting(
        std::move(url_loader));

    auto* const provider = web_app::FakeWebAppProvider::Get(profile());
    provider->SetExternallyManagedAppManager(
        std::move(externally_managed_app_manager));
    web_app::test::AwaitStartWebAppProviderAndSubsystems(profile());
    base::RunLoop().RunUntilIdle();
  }

  std::string CreateWebApp(const std::string& app_name) {
    const GURL kAppUrl(kUrl);

    auto web_app_info =
        web_app::WebAppInstallInfo::CreateWithStartUrlForTesting(kAppUrl);
    web_app_info->title = base::UTF8ToUTF16(app_name);
    web_app_info->scope = kAppUrl;
    web_app_info->user_display_mode =
        web_app::mojom::UserDisplayMode::kStandalone;

    return web_app::test::InstallWebApp(profile(), std::move(web_app_info));
  }

#if BUILDFLAG(IS_CHROMEOS)
  void RemoveArcApp(const std::string& app_id) {
    ArcApps* arc_apps = ArcAppsFactory::GetForProfile(profile());
    ASSERT_TRUE(arc_apps);
    arc_apps->OnAppRemoved(app_id);
  }
#endif  // BUILDFLAG(IS_CHROMEOS)

  void VerifyOptionalBool(std::optional<bool> source,
                          std::optional<bool> target) {
    if (source.has_value()) {
      EXPECT_EQ(source, target);
    }
  }

  const AppPtr& GetApp(const std::string& app_id) {
    AppRegistryCache& cache =
        AppServiceProxyFactory::GetForProfile(profile())->AppRegistryCache();
    return cache.states_[app_id];
  }

  void VerifyNoApp(const std::string& app_id) {
    AppRegistryCache& cache =
        AppServiceProxyFactory::GetForProfile(profile())->AppRegistryCache();

    ASSERT_EQ(cache.states_.end(), cache.states_.find(app_id));
  }

  void VerifyApp(AppType app_type,
                 const std::string& app_id,
                 const std::string& name,
                 apps::Readiness readiness,
                 InstallReason install_reason,
                 InstallSource install_source,
                 const std::vector<std::string>& additional_search_terms,
                 base::Time last_launch_time,
                 base::Time install_time,
                 const apps::Permissions& permissions,
                 std::optional<bool> is_platform_app = std::nullopt,
                 std::optional<bool> recommendable = std::nullopt,
                 std::optional<bool> searchable = std::nullopt,
                 std::optional<bool> show_in_launcher = std::nullopt,
                 std::optional<bool> show_in_shelf = std::nullopt,
                 std::optional<bool> show_in_search = std::nullopt,
                 std::optional<bool> show_in_management = std::nullopt,
                 std::optional<bool> handles_intents = std::nullopt,
                 std::optional<bool> allow_uninstall = std::nullopt,
                 std::optional<bool> allow_close = std::nullopt,
                 std::optional<bool> has_badge = std::nullopt,
                 std::optional<bool> paused = std::nullopt,
                 std::optional<bool> allow_window_mode_selection = std::nullopt,
                 WindowMode window_mode = WindowMode::kUnknown) {
    AppRegistryCache& cache =
        AppServiceProxyFactory::GetForProfile(profile())->AppRegistryCache();

    ASSERT_NE(cache.states_.end(), cache.states_.find(app_id));
    EXPECT_EQ(app_type, cache.states_[app_id]->app_type);
    ASSERT_TRUE(cache.states_[app_id]->name.has_value());
    EXPECT_EQ(name, cache.states_[app_id]->name.value());
    EXPECT_EQ(readiness, cache.states_[app_id]->readiness);
    ASSERT_TRUE(cache.states_[app_id]->icon_key.has_value());
    EXPECT_EQ(install_reason, cache.states_[app_id]->install_reason);
    EXPECT_EQ(install_source, cache.states_[app_id]->install_source);
    EXPECT_EQ(additional_search_terms,
              cache.states_[app_id]->additional_search_terms);
    if (!last_launch_time.is_null()) {
      EXPECT_EQ(last_launch_time, cache.states_[app_id]->last_launch_time);
    }
    if (!install_time.is_null()) {
      EXPECT_EQ(install_time, cache.states_[app_id]->install_time);
    }
    if (!permissions.empty()) {
      EXPECT_TRUE(IsEqual(permissions, cache.states_[app_id]->permissions));
    }

    VerifyOptionalBool(is_platform_app, cache.states_[app_id]->is_platform_app);
    VerifyOptionalBool(recommendable, cache.states_[app_id]->recommendable);
    VerifyOptionalBool(searchable, cache.states_[app_id]->searchable);
    VerifyOptionalBool(show_in_launcher,
                       cache.states_[app_id]->show_in_launcher);
    VerifyOptionalBool(show_in_shelf, cache.states_[app_id]->show_in_shelf);
    VerifyOptionalBool(show_in_search, cache.states_[app_id]->show_in_search);
    VerifyOptionalBool(show_in_management,
                       cache.states_[app_id]->show_in_management);
    VerifyOptionalBool(handles_intents, cache.states_[app_id]->handles_intents);
    VerifyOptionalBool(allow_uninstall, cache.states_[app_id]->allow_uninstall);
    VerifyOptionalBool(allow_close, cache.states_[app_id]->allow_close);
    VerifyOptionalBool(has_badge, cache.states_[app_id]->has_badge);
    VerifyOptionalBool(paused, cache.states_[app_id]->paused);
    VerifyOptionalBool(allow_window_mode_selection,
                       cache.states_[app_id]->allow_window_mode_selection);
    if (window_mode != WindowMode::kUnknown) {
      EXPECT_EQ(window_mode, cache.states_[app_id]->window_mode);
    }
  }

  void VerifyAppIsRemoved(const std::string& app_id) {
    AppRegistryCache& cache =
        AppServiceProxyFactory::GetForProfile(profile())->AppRegistryCache();
    ASSERT_NE(cache.states_.end(), cache.states_.find(app_id));
    EXPECT_EQ(apps::Readiness::kUninstalledByUser,
              cache.states_[app_id]->readiness);
  }

  void VerifyIntentFilters(const std::string& app_id) {
    apps::IntentFilters source = CreateIntentFilters();

    apps::IntentFilters target;
    apps::AppServiceProxyFactory::GetForProfile(profile())
        ->AppRegistryCache()
        .ForOneApp(app_id, [&target](const apps::AppUpdate& update) {
          target = update.IntentFilters();
        });

    EXPECT_EQ(source.size(), target.size());
    for (int i = 0; i < static_cast<int>(source.size()); i++) {
      EXPECT_EQ(*source[i], *target[i]);
    }
  }

  void VerifyAppTypeIsInitialized(AppType app_type) {
    AppRegistryCache& cache =
        AppServiceProxyFactory::GetForProfile(profile())->AppRegistryCache();
    ASSERT_TRUE(cache.IsAppTypeInitialized(app_type));
    ASSERT_TRUE(base::Contains(cache.InitializedAppTypes(), app_type));
  }

  void VerifyCapabilityAccess(const std::string& app_id,
                              std::optional<bool> accessing_camera,
                              std::optional<bool> accessing_microphone) {
    std::optional<bool> camera;
    std::optional<bool> microphone;
    apps::AppServiceProxyFactory::GetForProfile(profile())
        ->AppCapabilityAccessCache()
        .ForOneApp(app_id, [&camera, &microphone](
                               const apps::CapabilityAccessUpdate& update) {
          camera = update.Camera();
          microphone = update.Microphone();
        });
    EXPECT_EQ(camera, accessing_camera);
    EXPECT_EQ(microphone, accessing_microphone);
  }

  void VerifyNoCapabilityAccess(const std::string& app_id) {
    ASSERT_FALSE(
        apps::AppServiceProxyFactory::GetForProfile(profile())
            ->AppCapabilityAccessCache()
            .ForOneApp(app_id, [](const apps::CapabilityAccessUpdate& update) {
              NOTREACHED();
            }));
  }
};

#if BUILDFLAG(IS_CHROMEOS)
TEST_F(PublisherTest, ArcAppsOnApps) {
  ArcAppTest arc_test;
  arc_test.SetUp(profile());

  // Install fake apps.
  arc_test.app_instance()->SendRefreshAppList(arc_test.fake_apps());
  AddArcPackage(arc_test, arc_test.fake_apps());

  // Verify ARC apps are added to AppRegistryCache.
  ArcAppListPrefs* prefs = ArcAppListPrefs::Get(profile());
  ASSERT_TRUE(prefs);
  for (const auto& app_id : prefs->GetAppIds()) {
    std::unique_ptr<ArcAppListPrefs::AppInfo> app_info = prefs->GetApp(app_id);
    if (app_info) {
      VerifyApp(
          AppType::kArc, app_id, app_info->name, Readiness::kReady,
          app_info->sticky ? InstallReason::kSystem : InstallReason::kUser,
          app_info->sticky ? InstallSource::kSystem : InstallSource::kPlayStore,
          {}, app_info->last_launch_time, app_info->install_time,
          apps::Permissions(),
          /*is_platform_app=*/false,
          /*recommendable=*/true, /*searchable=*/true,
          /*show_in_launcher=*/true, /*show_in_shelf=*/true,
          /*show_in_search=*/true, /*show_in_management=*/true,
          /*handles_intents=*/true,
          /*allow_uninstall=*/app_info->ready && !app_info->sticky,
          /*allow_close=*/true,
          /*has_badge=*/false, /*paused=*/false,
          /*allow_window_mode_selection=*/std::nullopt);
      // Simulate the app is removed.
      RemoveArcApp(app_id);
      VerifyAppIsRemoved(app_id);
    }
  }
  VerifyAppTypeIsInitialized(AppType::kArc);

  // Verify the initialization process again with a new ArcApps object.
  std::unique_ptr<ArcApps> arc_apps = std::make_unique<ArcApps>(
      AppServiceProxyFactory::GetForProfile(profile()));
  ASSERT_TRUE(arc_apps.get());
  arc_apps->Initialize();

  for (const auto& app_id : prefs->GetAppIds()) {
    std::unique_ptr<ArcAppListPrefs::AppInfo> app_info = prefs->GetApp(app_id);
    if (app_info) {
      VerifyApp(
          AppType::kArc, app_id, app_info->name, Readiness::kReady,
          app_info->sticky ? InstallReason::kSystem : InstallReason::kUser,
          app_info->sticky ? InstallSource::kSystem : InstallSource::kPlayStore,
          {}, app_info->last_launch_time, app_info->install_time,
          MakeFakePermissions(),
          /*is_platform_app=*/false,
          /*recommendable=*/true, /*searchable=*/true,
          /*show_in_launcher=*/true, /*show_in_shelf=*/true,
          /*show_in_search=*/true, /*show_in_management=*/true,
          /*handles_intents=*/true,
          /*allow_uninstall=*/app_info->ready && !app_info->sticky,
          /*allow_close=*/true,
          /*has_badge=*/false, /*paused=*/false,
          /*allow_window_mode_selection=*/std::nullopt);

      // Test OnAppLastLaunchTimeUpdated.
      const base::Time before_time = base::Time::Now();
      prefs->SetLastLaunchTime(app_id);
      app_info = prefs->GetApp(app_id);
      EXPECT_GE(app_info->last_launch_time, before_time);
      VerifyApp(
          AppType::kArc, app_id, app_info->name, Readiness::kReady,
          app_info->sticky ? InstallReason::kSystem : InstallReason::kUser,
          app_info->sticky ? InstallSource::kSystem : InstallSource::kPlayStore,
          {}, app_info->last_launch_time, app_info->install_time,
          MakeFakePermissions());
    }
  }

  arc_apps->Shutdown();
}

TEST_F(PublisherTest, ArcApps_CapabilityAccess) {
  ArcAppTest arc_test;
  arc_test.SetUp(profile());
  ArcApps* arc_apps = apps::ArcAppsFactory::GetForProfile(profile());
  ASSERT_TRUE(arc_apps);

  const auto& fake_apps = arc_test.fake_apps();
  std::string package_name1 = fake_apps[0]->package_name;
  std::string package_name2 = fake_apps[1]->package_name;

  // Install fake apps.
  arc_test.app_instance()->SendRefreshAppList(arc_test.fake_apps());

  // Set accessing Camera for `package_name1`.
  {
    std::vector<arc::mojom::PrivacyItemPtr> privacy_items;
    privacy_items.push_back(CreateArcPrivacyItem(
        arc::mojom::AppPermissionGroup::CAMERA, package_name1));
    arc_apps->OnPrivacyItemsChanged(std::move(privacy_items));
    VerifyCapabilityAccess(ArcAppTest::GetAppId(*fake_apps[0]),
                           /*accessing_camera=*/true,
                           /*accessing_microphone=*/std::nullopt);
  }

  // Cancel accessing Camera for `package_name1`.
  {
    std::vector<arc::mojom::PrivacyItemPtr> privacy_items;
    arc_apps->OnPrivacyItemsChanged(std::move(privacy_items));
    VerifyCapabilityAccess(ArcAppTest::GetAppId(*fake_apps[0]),
                           /*accessing_camera=*/false,
                           /*accessing_microphone=*/false);
  }

  // Set accessing Camera and Microphone for `package_name1`, and accessing
  // Camera for `package_name2`.
  {
    std::vector<arc::mojom::PrivacyItemPtr> privacy_items;
    privacy_items.push_back(CreateArcPrivacyItem(
        arc::mojom::AppPermissionGroup::CAMERA, package_name1));
    privacy_items.push_back(CreateArcPrivacyItem(
        arc::mojom::AppPermissionGroup::MICROPHONE, package_name1));
    privacy_items.push_back(CreateArcPrivacyItem(
        arc::mojom::AppPermissionGroup::CAMERA, package_name2));
    arc_apps->OnPrivacyItemsChanged(std::move(privacy_items));
    VerifyCapabilityAccess(ArcAppTest::GetAppId(*fake_apps[0]),
                           /*accessing_camera=*/true,
                           /*accessing_microphone=*/true);
    VerifyCapabilityAccess(ArcAppTest::GetAppId(*fake_apps[1]),
                           /*accessing_camera=*/true,
                           /*accessing_microphone=*/std::nullopt);
  }

  // Cancel accessing Microphone for `package_name1`.
  {
    std::vector<arc::mojom::PrivacyItemPtr> privacy_items;
    privacy_items.push_back(CreateArcPrivacyItem(
        arc::mojom::AppPermissionGroup::CAMERA, package_name1));
    privacy_items.push_back(CreateArcPrivacyItem(
        arc::mojom::AppPermissionGroup::CAMERA, package_name2));
    arc_apps->OnPrivacyItemsChanged(std::move(privacy_items));
    VerifyCapabilityAccess(ArcAppTest::GetAppId(*fake_apps[0]),
                           /*accessing_camera=*/true,
                           /*accessing_microphone=*/false);
    VerifyCapabilityAccess(ArcAppTest::GetAppId(*fake_apps[1]),
                           /*accessing_camera=*/true,
                           /*accessing_microphone=*/false);
  }

  // Cancel accessing CAMERA for `package_name1` and `package_name2`.
  {
    std::vector<arc::mojom::PrivacyItemPtr> privacy_items;
    arc_apps->OnPrivacyItemsChanged(std::move(privacy_items));
    VerifyCapabilityAccess(ArcAppTest::GetAppId(*fake_apps[0]),
                           /*accessing_camera=*/false,
                           /*accessing_microphone=*/false);
    VerifyCapabilityAccess(ArcAppTest::GetAppId(*fake_apps[1]),
                           /*accessing_camera=*/false,
                           /*accessing_microphone=*/false);
  }

  arc_apps->Shutdown();
}
#endif  // BUILDFLAG(IS_CHROMEOS)

TEST_F(PublisherTest, ExtensionAppsOnApps) {
  // Re-init AppService to verify the init process.
  AppServiceTest app_service_test;
  app_service_test.SetUp(profile());

  // Install a "web store" app.
  scoped_refptr<extensions::Extension> store =
      MakeExtensionApp("webstore", "0.0", "http://google.com",
                       std::string(extensions::kWebStoreAppId));
  registrar()->AddExtension(store.get());

  VerifyApp(AppType::kChromeApp, store->id(), store->name(), Readiness::kReady,
            InstallReason::kDefault, InstallSource::kChromeWebStore, {},
            base::Time(), base::Time(), apps::Permissions(),
            /*is_platform_app=*/true, /*recommendable=*/true,
            /*searchable=*/true,
            /*show_in_launcher=*/true, /*show_in_shelf=*/true,
            /*show_in_search=*/true, /*show_in_management=*/true,
            /*handles_intents=*/true, /*allow_uninstall=*/true,
            /*allow_close=*/true,
            /*has_badge=*/false, /*paused=*/false,
            /*allow_window_mode_selection=*/std::nullopt);
  VerifyAppTypeIsInitialized(AppType::kChromeApp);

  // Uninstall the Chrome app.
  registrar()->UninstallExtension(
      store->id(), extensions::UNINSTALL_REASON_FOR_TESTING, nullptr);
  VerifyApp(AppType::kChromeApp, store->id(), store->name(),
            Readiness::kUninstalledByUser, InstallReason::kDefault,
            InstallSource::kChromeWebStore, {}, base::Time(), base::Time(),
            apps::Permissions(),
            /*is_platform_app=*/true,
            /*recommendable=*/true,
            /*searchable=*/true,
            /*show_in_launcher=*/true, /*show_in_shelf=*/true,
            /*show_in_search=*/true, /*show_in_management=*/true,
            /*handles_intents=*/true, /*allow_uninstall=*/true,
            /*allow_close=*/true,
            /*has_badge=*/false, /*paused=*/false,
            /*allow_window_mode_selection=*/std::nullopt);

  // Reinstall the Chrome app.
  registrar()->AddExtension(store.get());
  VerifyApp(AppType::kChromeApp, store->id(), store->name(), Readiness::kReady,
            InstallReason::kDefault, InstallSource::kChromeWebStore, {},
            base::Time(), base::Time(), apps::Permissions(),
            /*is_platform_app=*/true, /*recommendable=*/true,
            /*searchable=*/true,
            /*show_in_launcher=*/true, /*show_in_shelf=*/true,
            /*show_in_search=*/true, /*show_in_management=*/true,
            /*handles_intents=*/true, /*allow_uninstall=*/true,
            /*allow_close=*/true,
            /*has_badge=*/false, /*paused=*/false,
            /*allow_window_mode_selection=*/std::nullopt);

  // Test OnExtensionLastLaunchTimeChanged.
  extensions::ExtensionPrefs::Get(profile())->SetLastLaunchTime(
      store->id(), kLastLaunchTime);
  VerifyApp(AppType::kChromeApp, store->id(), store->name(), Readiness::kReady,
            InstallReason::kDefault, InstallSource::kChromeWebStore, {},
            kLastLaunchTime, base::Time(), apps::Permissions(),
            /*is_platform_app=*/true);
}

TEST_F(PublisherTest, WebAppsOnApps) {
  const std::string kAppName = "Web App";
  AppServiceTest app_service_test_;
  app_service_test_.SetUp(profile());
  auto app_id = CreateWebApp(kAppName);

  InstallReason expected_install_reason = InstallReason::kUser;

  VerifyApp(AppType::kWeb, app_id, kAppName, Readiness::kReady,
            expected_install_reason, InstallSource::kBrowser, {}, base::Time(),
            base::Time(), apps::Permissions(),
            /*is_platform_app=*/false,
            /*recommendable=*/true,
            /*searchable=*/true,
            /*show_in_launcher=*/true, /*show_in_shelf=*/true,
            /*show_in_search=*/true, /*show_in_management=*/true,
            /*handles_intents=*/true, /*allow_uninstall=*/true,
            /*allow_close=*/true,
            /*has_badge=*/false, /*paused=*/false,
            /*allow_window_mode_selection=*/true, WindowMode::kWindow);
  VerifyIntentFilters(app_id);
  VerifyAppTypeIsInitialized(AppType::kWeb);
}

}  // namespace apps