File: dedupe_install_urls_command_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 (572 lines) | stat: -rw-r--r-- 25,814 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
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
// Copyright 2023 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/web_applications/commands/dedupe_install_urls_command.h"

#include "base/test/metrics/histogram_tester.h"
#include "base/test/test_future.h"
#include "build/build_config.h"
#include "chrome/browser/web_applications/externally_managed_app_manager.h"
#include "chrome/browser/web_applications/policy/web_app_policy_manager.h"
#include "chrome/browser/web_applications/preinstalled_web_app_manager.h"
#include "chrome/browser/web_applications/preinstalled_web_apps/preinstalled_web_apps.h"
#include "chrome/browser/web_applications/test/fake_web_contents_manager.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/test/web_app_test.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/browser/web_applications/web_app_command_manager.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_management_type.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registry_update.h"
#include "chrome/browser/web_applications/web_app_sync_bridge.h"
#include "chrome/common/pref_names.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chromeos/ash/components/system/fake_statistics_provider.h"
#include "chromeos/ash/components/system/statistics_provider.h"
#endif

namespace web_app {

class DedupeInstallUrlsCommandTest : public WebAppTest {
 public:
  DedupeInstallUrlsCommandTest()
      : bypass_dependencies_(
            PreinstalledWebAppManager::BypassAwaitingDependenciesForTesting()),
        skip_preinstalled_web_app_startup_(
            PreinstalledWebAppManager::SkipStartupForTesting()),
        bypass_offline_manifest_requirement_(
            PreinstalledWebAppManager::
                BypassOfflineManifestRequirementForTesting()) {}

  void SetUp() override {
    WebAppTest::SetUp();

    fake_web_contents_manager_ = static_cast<FakeWebContentsManager*>(
        &provider().web_contents_manager());

    test::AwaitStartWebAppProviderAndSubsystems(profile());

#if BUILDFLAG(IS_CHROMEOS)
    // Mocking the StatisticsProvider for testing.
    ash::system::StatisticsProvider::SetTestProvider(&statistics_provider);
    statistics_provider.SetMachineStatistic(ash::system::kActivateDateKey,
                                            "2023-18");
#endif
  }

  void TearDown() override {
#if BUILDFLAG(IS_CHROMEOS)
    ash::system::StatisticsProvider::SetTestProvider(nullptr);
#endif
    WebAppTest::TearDown();
  }

  WebAppProvider& provider() {
    return *WebAppProvider::GetForWebApps(profile());
  }

  void SetPolicyInstallUrlAndSynchronize(const GURL& url) {
    base::test::TestFuture<void> future;
    provider()
        .policy_manager()
        .SetOnAppsSynchronizedCompletedCallbackForTesting(future.GetCallback());
    profile()->GetPrefs()->SetList(
        prefs::kWebAppInstallForceList,
        base::Value::List().Append(base::Value::Dict().Set("url", url.spec())));
    CHECK(future.Wait());
  }

  void AddBuggyDefaultInstallToApp(const webapps::AppId& app_id,
                                   const GURL& install_url) {
    ScopedRegistryUpdate update = provider().sync_bridge_unsafe().BeginUpdate();
    WebApp& placeholder_app = *update->UpdateApp(app_id);
    placeholder_app.AddSource(WebAppManagement::Type::kDefault);
    placeholder_app.AddInstallURLToManagementExternalConfigMap(
        WebAppManagement::Type::kDefault, install_url);
  }

  void SynchronizePreinstalledWebAppManagerWithInstallUrl(
      const GURL& install_url) {
    ScopedTestingPreinstalledAppData scope;
    ExternalInstallOptions options(install_url,
                                   mojom::UserDisplayMode::kStandalone,
                                   ExternalInstallSource::kExternalDefault);
    options.user_type_allowlist = {"unmanaged"};
    scope.apps.push_back(std::move(options));

    base::test::TestFuture<
        std::map<GURL /*install_url*/,
                 ExternallyManagedAppManager::InstallResult>,
        std::map<GURL /*install_url*/, webapps::UninstallResultCode>>
        future;
    provider().preinstalled_web_app_manager().LoadAndSynchronizeForTesting(
        future.GetCallback());
    CHECK(future.Wait());
  }

  void SynchronizePolicyWebAppManager() {
    base::test::TestFuture<void> future;
    provider()
        .policy_manager()
        .SetOnAppsSynchronizedCompletedCallbackForTesting(future.GetCallback());
    provider().policy_manager().RefreshPolicyInstalledAppsForTesting();
    CHECK(future.Wait());
  }

  webapps::AppId ExternallyInstallWebApp(
      webapps::WebappInstallSource install_surface,
      const GURL& install_url,
      const GURL& start_url) {
    auto web_app_info =
        WebAppInstallInfo::CreateWithStartUrlForTesting(start_url);
    web_app_info->title = u"Test app";
    web_app_info->install_url = install_url;

    return test::InstallWebApp(profile(), std::move(web_app_info),
                               /*overwrite_existing_manifest_fields=*/true,
                               install_surface);
  }

 protected:
  raw_ptr<FakeWebContentsManager, DisableDanglingPtrDetection>
      fake_web_contents_manager_ = nullptr;
  base::AutoReset<bool> bypass_dependencies_;
  base::AutoReset<bool> skip_preinstalled_web_app_startup_;
  base::AutoReset<bool> bypass_offline_manifest_requirement_;

#if BUILDFLAG(IS_CHROMEOS)
  ash::system::FakeStatisticsProvider statistics_provider;
#endif  // BUILDFLAG(IS_CHROMEOS)
};

TEST_F(DedupeInstallUrlsCommandTest,
       PolicyUpgradePlaceholderWithTwoInstallSources) {
  // This tests for users affected by crbug.com/1427340, specifically those left
  // with a placeholder web app installed with kPolicy and kDefault install
  // sources.
  // They got into this state by the following steps:
  // - A web app policy installed install URL A, was unsuccessful and created
  //   placeholder P for install URL A.
  // - A web app preinstall installed install URL A, saw the placeholder P and
  //   added itself as another install source to it.
  // This test checks that the placeholder is removed with a successful policy
  // install of install URL A.

  base::HistogramTester histogram_tester;
  GURL install_url("https://example.com/install_url");
  GURL manifest_url("https://example.com/manifest.json");
  GURL start_url("https://example.com/start_url");
  webapps::AppId placeholder_app_id = GenerateAppIdFromManifestId(
      GenerateManifestIdFromStartUrlOnly(install_url));
  webapps::AppId real_app_id = GenerateAppIdFromManifestId(
      GenerateManifestIdFromStartUrlOnly(start_url));

  // Set up buggy state.
  {
    // Set up failure at install_url.
    // Default FakePageState is kFailedErrorPageLoaded.
    fake_web_contents_manager_->GetOrCreatePageState(install_url);

    // Install install_url via policy leading to placeholder installation.
    SetPolicyInstallUrlAndSynchronize(install_url);
    EXPECT_TRUE(provider().registrar_unsafe().IsPlaceholderApp(
        placeholder_app_id, WebAppManagement::Type::kPolicy));

    // Recreate preinstall bug by adding its config to the same placeholder.
    AddBuggyDefaultInstallToApp(placeholder_app_id, install_url);
  }

  // Perform successful policy install.
  {
    // Set up successful load at install_url.
    ASSERT_EQ(fake_web_contents_manager_->CreateBasicInstallPageState(
                  install_url, manifest_url, start_url),
              real_app_id);

    // Rerun existing policy install_url installation.
    SynchronizePolicyWebAppManager();
  }

  // Placeholder app should no longer be present.
  EXPECT_FALSE(provider().registrar_unsafe().IsInRegistrar(placeholder_app_id));

  // Real app should be installed
  const WebApp* real_app =
      provider().registrar_unsafe().GetAppById(real_app_id);
  EXPECT_TRUE(real_app);

  // Real app should contain both the policy and preinstall install_url
  // association configs.
  EXPECT_EQ(
      real_app->management_to_external_config_map(),
      (WebApp::ExternalConfigMap{
          {WebAppManagement::Type::kPolicy, {false, {install_url}, {}}},
          {WebAppManagement::Type::kDefault, {false, {install_url}, {}}}}));

  EXPECT_THAT(histogram_tester.GetAllSamples(
                  "WebApp.DedupeInstallUrls.SessionRunCount"),
              base::BucketsAre(base::Bucket(/*runs=*/1, /*count=*/1),
                               base::Bucket(/*runs=*/2, /*count=*/1)));
  EXPECT_THAT(histogram_tester.GetAllSamples(
                  "WebApp.DedupeInstallUrls.InstallUrlsDeduped"),
              base::BucketsAre(base::Bucket(/*install_urls=*/0, /*count=*/1),
                               base::Bucket(/*install_urls=*/1, /*count=*/1)));
  EXPECT_THAT(
      histogram_tester.GetAllSamples("WebApp.DedupeInstallUrls.AppsDeduped"),
      base::BucketsAre(base::Bucket(/*apps=*/2, /*count=*/1)));
}

TEST_F(DedupeInstallUrlsCommandTest,
       PreinstallUpgradePlaceholderWithTwoInstallSources) {
  // This tests for users affected by crbug.com/1427340, specifically those left
  // with a placeholder web app installed with kPolicy and kDefault install
  // sources.
  // They got into this state by the following steps:
  // - A web app policy installed install URL A, was unsuccessful and created
  //   placeholder P for install URL A.
  // - A web app preinstall installed install URL A, saw the placeholder P and
  //   added itself as another install source to it.
  // This test checks that the placeholder is removed with a force reinstall of
  // install URL A via PreinstalledWebAppManager.

  base::HistogramTester histogram_tester;
  GURL install_url("https://example.com/install_url");
  GURL manifest_url("https://example.com/manifest.json");
  GURL start_url("https://example.com/start_url");
  webapps::AppId placeholder_app_id = GenerateAppIdFromManifestId(
      GenerateManifestIdFromStartUrlOnly(install_url));
  webapps::AppId real_app_id = GenerateAppIdFromManifestId(
      GenerateManifestIdFromStartUrlOnly(start_url));

  // Set up buggy state.
  {
    // Set up failure at install_url.
    // Default FakePageState is kFailedErrorPageLoaded.
    fake_web_contents_manager_->GetOrCreatePageState(install_url);

    // Install install_url via policy leading to placeholder installation.
    SetPolicyInstallUrlAndSynchronize(install_url);
    EXPECT_TRUE(provider().registrar_unsafe().IsPlaceholderApp(
        placeholder_app_id, WebAppManagement::Type::kPolicy));

    // Recreate preinstall bug by adding its config to the same placeholder.
    AddBuggyDefaultInstallToApp(placeholder_app_id, install_url);
  }

  // Run PreinstalledWebAppManager with a working install_url.
  ASSERT_EQ(fake_web_contents_manager_->CreateBasicInstallPageState(
                install_url, manifest_url, start_url),
            real_app_id);
  SynchronizePreinstalledWebAppManagerWithInstallUrl(install_url);

  // Placeholder app should no longer be present.
  EXPECT_FALSE(provider().registrar_unsafe().IsInRegistrar(placeholder_app_id));

  // Real app should be installed
  const WebApp* real_app =
      provider().registrar_unsafe().GetAppById(real_app_id);
  EXPECT_TRUE(real_app);

  // Real app should contain both the policy and preinstall install_url
  // association configs.
  EXPECT_EQ(
      real_app->management_to_external_config_map(),
      (WebApp::ExternalConfigMap{
          {WebAppManagement::Type::kPolicy, {false, {install_url}, {}}},
          {WebAppManagement::Type::kDefault, {false, {install_url}, {}}}}));

  EXPECT_THAT(histogram_tester.GetAllSamples(
                  "WebApp.DedupeInstallUrls.SessionRunCount"),
              base::BucketsAre(base::Bucket(/*runs=*/1, /*count=*/1),
                               base::Bucket(/*runs=*/2, /*count=*/1)));
  EXPECT_THAT(histogram_tester.GetAllSamples(
                  "WebApp.DedupeInstallUrls.InstallUrlsDeduped"),
              base::BucketsAre(base::Bucket(/*install_urls=*/0, /*count=*/1),
                               base::Bucket(/*install_urls=*/1, /*count=*/1)));
  EXPECT_THAT(
      histogram_tester.GetAllSamples("WebApp.DedupeInstallUrls.AppsDeduped"),
      base::BucketsAre(base::Bucket(/*apps=*/2, /*count=*/1)));
}

TEST_F(DedupeInstallUrlsCommandTest, SameInstallUrlForRealAndPlaceholder) {
  // This tests for users affected by crbug.com/1427340, specifically those left
  // with a kDefault placeholder-like app (placeholder in appearance but not in
  // configuration) and kPolicy real app.
  // They got into this state by the following steps:
  // - A web app policy installed install URL A, was unsuccessful and created
  //   placeholder P for install URL A.
  // - A web app preinstall installed install URL A, saw the placeholder P and
  //   added itself as another install source to it.
  // - The web app policy later retried installing install URL A and was
  //   successful, installed real app R and removed the policy install source
  //   from placeholder P. This did not uninstall placeholder P as the default
  //   install source remained.
  // - The user is left with placeholder P and real app R both associated with
  //   install URL A.
  // This test checks that the placeholder is deduped into the real app after a
  // policy synchronisation run that's already satisfied.

  base::HistogramTester histogram_tester;
  GURL install_url("https://example.com/install_url");
  GURL manifest_url("https://example.com/manifest.json");
  GURL start_url("https://example.com/start_url");
  webapps::AppId placeholder_app_id = GenerateAppIdFromManifestId(
      GenerateManifestIdFromStartUrlOnly(install_url));
  webapps::AppId real_app_id = GenerateAppIdFromManifestId(
      GenerateManifestIdFromStartUrlOnly(start_url));

  // Set up buggy state.
  {
    // Set up failure at install_url.
    // Default FakePageState is kFailedErrorPageLoaded.
    fake_web_contents_manager_->GetOrCreatePageState(install_url);

    // Install install_url via policy leading to placeholder installation.
    SetPolicyInstallUrlAndSynchronize(install_url);
    EXPECT_TRUE(provider().registrar_unsafe().IsPlaceholderApp(
        placeholder_app_id, WebAppManagement::Type::kPolicy));

    // Recreate preinstall bug by adding its config to the same placeholder.
    AddBuggyDefaultInstallToApp(placeholder_app_id, install_url);

    // Run policy install successfully without any dedupe logic.
    {
      base::AutoReset<bool> scope =
          DedupeInstallUrlsCommand::ScopedSuppressForTesting();

      // Set up successful load at install_url.
      ASSERT_EQ(fake_web_contents_manager_->CreateBasicInstallPageState(
                    install_url, manifest_url, start_url),
                real_app_id);

      SynchronizePolicyWebAppManager();

      // The placeholder app remains.
      const WebApp* placeholder_app =
          provider().registrar_unsafe().GetAppById(placeholder_app_id);
      ASSERT_TRUE(placeholder_app);
      EXPECT_EQ(placeholder_app->management_to_external_config_map(),
                (WebApp::ExternalConfigMap{{WebAppManagement::Type::kDefault,
                                            {false, {install_url}, {}}}}));

      // The placeholder is no longer marked as a placeholder despite still
      // looking like one.
      EXPECT_FALSE(provider().registrar_unsafe().IsPlaceholderApp(
          placeholder_app_id, WebAppManagement::Type::kPolicy));
      EXPECT_TRUE(LooksLikePlaceholder(*placeholder_app));
    }
  }

  // Rerun policy synchronize with deduping enabled.
  SynchronizePolicyWebAppManager();

  // Placeholder app should no longer be present.
  EXPECT_FALSE(provider().registrar_unsafe().IsInRegistrar(placeholder_app_id));

  // Real app should be installed
  const WebApp* real_app =
      provider().registrar_unsafe().GetAppById(real_app_id);
  EXPECT_TRUE(real_app);

  // Real app should contain both the policy and preinstall install_url
  // association configs.
  EXPECT_EQ(
      real_app->management_to_external_config_map(),
      (WebApp::ExternalConfigMap{
          {WebAppManagement::Type::kPolicy, {false, {install_url}, {}}},
          {WebAppManagement::Type::kDefault, {false, {install_url}, {}}}}));

  EXPECT_THAT(histogram_tester.GetAllSamples(
                  "WebApp.DedupeInstallUrls.SessionRunCount"),
              base::BucketsAre(base::Bucket(/*runs=*/1, /*count=*/1),
                               base::Bucket(/*runs=*/2, /*count=*/1),
                               base::Bucket(/*runs=*/3, /*count=*/1)));
  EXPECT_THAT(histogram_tester.GetAllSamples(
                  "WebApp.DedupeInstallUrls.InstallUrlsDeduped"),
              base::BucketsAre(base::Bucket(/*install_urls=*/0, /*count=*/1),
                               base::Bucket(/*install_urls=*/1, /*count=*/1)));
  EXPECT_THAT(
      histogram_tester.GetAllSamples("WebApp.DedupeInstallUrls.AppsDeduped"),
      base::BucketsAre(base::Bucket(/*apps=*/2, /*count=*/1)));
}

TEST_F(DedupeInstallUrlsCommandTest, DefaultPlaceholderForceReinstalled) {
  // This tests for users affected by crbug.com/1427340, specifically those left
  // with a kDefault placeholder-like app (placeholder in appearance but not in
  // configuration) and kPolicy real app for a different install URL.
  // They got into this state by the following steps:
  // - A web app policy installed install URL A, was unsuccessful and created
  //   placeholder P for install URL A.
  // - A web app preinstall installed install URL A, saw the placeholder P and
  //   added itself as another install source to it.
  // - The web app policy later retried installing install URL A and was
  //   successful, installed real app R and removed the policy install source
  //   from placeholder P. This did not uninstall placeholder P as the default
  //   install source remained.
  // - The web app policy is later later updated to install a similar install
  //   URL B with a slight tweak to the query params so it still installs the
  //   same app. Real app R is uninstalled then reinstalled again this time
  //   associated with install URL B.
  // - The user is left with placeholder P associated with install URL A via
  //   the kDefault install source and real app R associated with install URL B
  //   via the kPolicy install source.
  // This test checks that the placeholder is removed with a force reinstall of
  // install URL A via PreinstalledWebAppManager.

  base::HistogramTester histogram_tester;
  GURL install_url("https://example.com/install_url");
  GURL alternate_install_url(
      "https://example.com/install_url?with_query_param");
  GURL manifest_url("https://example.com/manifest.json");
  GURL start_url("https://example.com/start_url");
  webapps::AppId placeholder_app_id = GenerateAppIdFromManifestId(
      GenerateManifestIdFromStartUrlOnly(install_url));
  webapps::AppId real_app_id = GenerateAppIdFromManifestId(
      GenerateManifestIdFromStartUrlOnly(start_url));

  // Set up buggy state.
  {
    // Set up failure at install_url.
    // Default FakePageState is kFailedErrorPageLoaded.
    fake_web_contents_manager_->GetOrCreatePageState(install_url);

    // Install install_url via policy leading to placeholder installation.
    SetPolicyInstallUrlAndSynchronize(install_url);
    EXPECT_TRUE(provider().registrar_unsafe().IsPlaceholderApp(
        placeholder_app_id, WebAppManagement::Type::kPolicy));

    // Recreate preinstall bug by adding its config to the same placeholder.
    AddBuggyDefaultInstallToApp(placeholder_app_id, install_url);

    // Run policy install successfully on a slightly different install_url that
    // installs the same web app.
    ASSERT_EQ(fake_web_contents_manager_->CreateBasicInstallPageState(
                  alternate_install_url, manifest_url, start_url),
              real_app_id);
    SetPolicyInstallUrlAndSynchronize(alternate_install_url);

    // The placeholder app remains.
    const WebApp* placeholder_app =
        provider().registrar_unsafe().GetAppById(placeholder_app_id);
    ASSERT_TRUE(placeholder_app);
    EXPECT_EQ(placeholder_app->management_to_external_config_map(),
              (WebApp::ExternalConfigMap{{WebAppManagement::Type::kDefault,
                                          {false, {install_url}, {}}}}));

    // The placeholder is no longer marked as a placeholder despite still
    // looking like one.
    EXPECT_FALSE(provider().registrar_unsafe().IsPlaceholderApp(
        placeholder_app_id, WebAppManagement::Type::kPolicy));
    EXPECT_TRUE(LooksLikePlaceholder(*placeholder_app));
  }

  // Run PreinstalledWebAppManager with a working install_url.
  ASSERT_EQ(fake_web_contents_manager_->CreateBasicInstallPageState(
                install_url, manifest_url, start_url),
            real_app_id);
  SynchronizePreinstalledWebAppManagerWithInstallUrl(install_url);

  // Placeholder app should no longer be present.
  EXPECT_FALSE(provider().registrar_unsafe().IsInRegistrar(placeholder_app_id));

  // Real app should be installed
  const WebApp* real_app =
      provider().registrar_unsafe().GetAppById(real_app_id);
  EXPECT_TRUE(real_app);

  // Real app should contain both the policy and preinstall configs for their
  // respective install URLs.
  EXPECT_EQ(real_app->management_to_external_config_map(),
            (WebApp::ExternalConfigMap{{WebAppManagement::Type::kPolicy,
                                        {false, {alternate_install_url}, {}}},
                                       {WebAppManagement::Type::kDefault,
                                        {false, {install_url}, {}}}}));

  EXPECT_THAT(histogram_tester.GetAllSamples(
                  "WebApp.DedupeInstallUrls.SessionRunCount"),
              base::BucketsAre(base::Bucket(/*runs=*/1, /*count=*/1),
                               base::Bucket(/*runs=*/2, /*count=*/1),
                               base::Bucket(/*runs=*/3, /*count=*/1)));
  EXPECT_THAT(histogram_tester.GetAllSamples(
                  "WebApp.DedupeInstallUrls.InstallUrlsDeduped"),
              base::BucketsAre(base::Bucket(/*install_urls=*/0, /*count=*/2),
                               base::Bucket(/*install_urls=*/1, /*count=*/1)));
  EXPECT_THAT(
      histogram_tester.GetAllSamples("WebApp.DedupeInstallUrls.AppsDeduped"),
      base::BucketsAre(base::Bucket(/*apps=*/2, /*count=*/1)));
}

// Other tests just cover two apps getting deduped, this tests multiple
// install_urls getting deduped with more than two apps in each.
TEST_F(DedupeInstallUrlsCommandTest, MoreThanTwoDuplicates) {
  base::HistogramTester histogram_tester;

  // Set up duplicate apps.
  GURL install_url_a("https://www.a.com/");
  webapps::AppId app_id_a1 =
      ExternallyInstallWebApp(webapps::WebappInstallSource::EXTERNAL_DEFAULT,
                              install_url_a, install_url_a.Resolve("default"));
  webapps::AppId app_id_a2 =
      ExternallyInstallWebApp(webapps::WebappInstallSource::KIOSK,
                              install_url_a, install_url_a.Resolve("kiosk"));
  webapps::AppId app_id_a3 =
      ExternallyInstallWebApp(webapps::WebappInstallSource::EXTERNAL_POLICY,
                              install_url_a, install_url_a.Resolve("policy"));

  GURL install_url_b("https://www.b.com/");
  webapps::AppId app_id_b1 =
      ExternallyInstallWebApp(webapps::WebappInstallSource::ARC, install_url_b,
                              install_url_b.Resolve("arc"));
  webapps::AppId app_id_b2 =
      ExternallyInstallWebApp(webapps::WebappInstallSource::PRELOADED_OEM,
                              install_url_b, install_url_b.Resolve("oem"));
  webapps::AppId app_id_b3 =
      ExternallyInstallWebApp(webapps::WebappInstallSource::MICROSOFT_365_SETUP,
                              install_url_b, install_url_b.Resolve("ms365"));

  // All app IDs must be unique.
  ASSERT_EQ(base::flat_set<webapps::AppId>({app_id_a1, app_id_a2, app_id_a3,
                                            app_id_b1, app_id_b2, app_id_b3})
                .size(),
            6u);

  // Run dedupe scan.
  base::test::TestFuture<void> future;
  provider().scheduler().ScheduleDedupeInstallUrls(future.GetCallback());
  ASSERT_TRUE(future.Wait());

  // The most recently installed web app is chosen as the dedupe into target.
  const WebAppRegistrar& registrar = provider().registrar_unsafe();
  EXPECT_FALSE(registrar.IsInRegistrar(app_id_a1));
  EXPECT_FALSE(registrar.IsInRegistrar(app_id_a2));
  const WebApp* app_a = registrar.GetAppById(app_id_a3);
  ASSERT_TRUE(app_a);
  EXPECT_EQ(app_a->GetSources(),
            WebAppManagementTypes({WebAppManagement::Type::kDefault,
                                   WebAppManagement::Type::kKiosk,
                                   WebAppManagement::Type::kPolicy}));

  EXPECT_FALSE(registrar.IsInRegistrar(app_id_b1));
  EXPECT_FALSE(registrar.IsInRegistrar(app_id_b2));
  const WebApp* app_b = registrar.GetAppById(app_id_b3);
  ASSERT_TRUE(app_b);
  EXPECT_EQ(
      app_b->GetSources(),
      WebAppManagementTypes({WebAppManagement::Type::kWebAppStore,
                             WebAppManagement::Type::kOem,
                             WebAppManagement::Type::kOneDriveIntegration}));

  histogram_tester.ExpectUniqueSample(
      "WebApp.DedupeInstallUrls.SessionRunCount", /*runs=*/1, /*count=*/1);
  histogram_tester.ExpectUniqueSample(
      "WebApp.DedupeInstallUrls.InstallUrlsDeduped", /*install_urls=*/2,
      /*count=*/1);
  histogram_tester.ExpectUniqueSample("WebApp.DedupeInstallUrls.AppsDeduped",
                                      /*apps=*/3,
                                      /*count=*/2);
}

}  // namespace web_app