File: manifest_v2_experiment_manager_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 (981 lines) | stat: -rw-r--r-- 41,854 bytes parent folder | download | duplicates (3)
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
// 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/extensions/manifest_v2_experiment_manager.h"

#include "base/one_shot_event.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/extension_management_internal.h"
#include "chrome/browser/extensions/external_provider_manager.h"
#include "chrome/browser/extensions/mv2_experiment_stage.h"
#include "chrome/browser/extensions/unpacked_installer.h"
#include "chrome/browser/profiles/profile.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/test/browser_test.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/mock_external_provider.h"
#include "extensions/browser/pref_names.h"
#include "extensions/browser/test_extension_registry_observer.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_features.h"
#include "extensions/common/feature_switch.h"
#include "extensions/common/mojom/manifest.mojom.h"
#include "extensions/test/test_extension_dir.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"

namespace extensions {
namespace {

const Extension* GetExtensionByName(std::string_view name,
                                    const ExtensionSet& extensions) {
  const Extension* extension = nullptr;
  for (const auto& e : extensions) {
    if (e->name() == name) {
      extension = e.get();
      break;
    }
  }

  return extension;
}

// Each test may have a different desired stage. Store them here so the test
// harness properly instantiates them.
MV2ExperimentStage GetExperimentStageForTest(std::string_view test_name) {
  struct {
    const char* test_name;
    MV2ExperimentStage stage;
  } test_stages[] = {
      {"PRE_PRE_ExtensionsAreDisabledOnStartup", MV2ExperimentStage::kWarning},
      {"PRE_ExtensionsAreDisabledOnStartup", MV2ExperimentStage::kWarning},
      {"ExtensionsAreDisabledOnStartup",
       MV2ExperimentStage::kDisableWithReEnable},
      {"PRE_PRE_ExtensionsCanBeReEnabledByUsers", MV2ExperimentStage::kWarning},
      {"PRE_ExtensionsCanBeReEnabledByUsers",
       MV2ExperimentStage::kDisableWithReEnable},
      {"ExtensionsCanBeReEnabledByUsers",
       MV2ExperimentStage::kDisableWithReEnable},
      {"ExtensionsAreReEnabledWhenUpdatedToMV3",
       MV2ExperimentStage::kDisableWithReEnable},
      {"PRE_MarkingNoticeAsAcknowledged", MV2ExperimentStage::kWarning},
      {"MarkingNoticeAsAcknowledged", MV2ExperimentStage::kDisableWithReEnable},
      {"PRE_MarkingGlobalNoticeAsAcknowledged", MV2ExperimentStage::kWarning},
      {"MarkingGlobalNoticeAsAcknowledged",
       MV2ExperimentStage::kDisableWithReEnable},
      {"PRE_PRE_ExtensionsAreReEnabledIfExperimentDisabled",
       MV2ExperimentStage::kWarning},
      {"PRE_ExtensionsAreReEnabledIfExperimentDisabled",
       MV2ExperimentStage::kDisableWithReEnable},
      {"ExtensionsAreReEnabledIfExperimentDisabled",
       MV2ExperimentStage::kWarning},
      {"ExternalExtensionsCanBeInstalledButAreAlsoDisabled",
       MV2ExperimentStage::kDisableWithReEnable},
      {"UkmIsEmittedForExtensionWhenUninstalled",
       MV2ExperimentStage::kDisableWithReEnable},
      {"UkmIsNotEmittedForOtherUninstallations",
       MV2ExperimentStage::kDisableWithReEnable},
      {"PRE_MV2ExtensionsAreNotDisabledIfLegacyExtensionSwitchIsApplied",
       MV2ExperimentStage::kWarning},
      {"MV2ExtensionsAreNotDisabledIfLegacyExtensionSwitchIsApplied",
       MV2ExperimentStage::kUnsupported},
      {"PRE_PRE_FlowFromWarningToUnsupported", MV2ExperimentStage::kWarning},
      {"PRE_FlowFromWarningToUnsupported",
       MV2ExperimentStage::kDisableWithReEnable},
      {"FlowFromWarningToUnsupported", MV2ExperimentStage::kUnsupported},
      {"UnpackedExtensionsCanBeInstalledInDisabledPhase",
       MV2ExperimentStage::kDisableWithReEnable},
      {"UnpackedExtensionsCannotBeInstalledInUnsupportedPhase",
       MV2ExperimentStage::kUnsupported},
  };

  for (const auto& test_stage : test_stages) {
    if (test_stage.test_name == test_name) {
      return test_stage.stage;
    }
  }

  NOTREACHED()
      << "Unknown test name '" << test_name << "'. "
      << "You need to add a new test stage entry into this collection.";
}

}  // namespace

class ManifestV2ExperimentManagerBrowserTest : public ExtensionBrowserTest {
 public:
  ManifestV2ExperimentManagerBrowserTest() = default;
  ~ManifestV2ExperimentManagerBrowserTest() override = default;

  void SetUp() override {
    std::vector<base::test::FeatureRef> enabled_features;
    std::vector<base::test::FeatureRef> disabled_features;

    // Each test may need a different value for the experiment stages, since
    // many need some kind of pre-experiment set up, then test the behavior on
    // subsequent startups. Initialize each test according to its preferred
    // stage.
    MV2ExperimentStage experiment_stage = GetExperimentStageForTest(
        testing::UnitTest::GetInstance()->current_test_info()->name());

    switch (experiment_stage) {
      case MV2ExperimentStage::kWarning:
        enabled_features.push_back(
            extensions_features::kExtensionManifestV2DeprecationWarning);
        disabled_features.push_back(
            extensions_features::kExtensionManifestV2Disabled);
        disabled_features.push_back(
            extensions_features::kExtensionManifestV2Unsupported);
        break;
      case MV2ExperimentStage::kDisableWithReEnable:
        enabled_features.push_back(
            extensions_features::kExtensionManifestV2Disabled);
        disabled_features.push_back(
            extensions_features::kExtensionManifestV2DeprecationWarning);
        disabled_features.push_back(
            extensions_features::kExtensionManifestV2Unsupported);
        break;
      case MV2ExperimentStage::kUnsupported:
        enabled_features.push_back(
            extensions_features::kExtensionManifestV2Unsupported);
        disabled_features.push_back(
            extensions_features::kExtensionManifestV2DeprecationWarning);
        disabled_features.push_back(
            extensions_features::kExtensionManifestV2Disabled);
        break;
      case MV2ExperimentStage::kNone:
        NOTREACHED() << "Unhandled stage.";
    }

    PopulateAdditionalFeatures(enabled_features, disabled_features);

    feature_list_.InitWithFeatures(enabled_features, disabled_features);

    ExtensionBrowserTest::SetUp();
  }

  void TearDown() override {
    ExtensionBrowserTest::TearDown();
  }

  void SetUpCommandLine(base::CommandLine* command_line) override {
    ExtensionBrowserTest::SetUpCommandLine(command_line);
  }

  void SetUpOnMainThread() override {
    ExtensionBrowserTest::SetUpOnMainThread();

    ukm_recorder_.emplace();
    // UKM only emits for webstore extensions. Pretend any extension is a store
    // extension for this test.
    ukm_recorder_->SetIsWebstoreExtensionCallback(
        base::BindRepeating([](std::string_view) { return true; }));
  }

  // Since this is testing the MV2 deprecation experiments, we don't want to
  // bypass their disabling for testing.
  bool ShouldAllowMV2Extensions() override { return false; }

  void WaitForExtensionSystemReady() {
    base::RunLoop run_loop;
    ExtensionSystem::Get(profile())->ready().Post(
        FROM_HERE, run_loop.QuitWhenIdleClosure());
    run_loop.Run();
  }

  // Uninstalls the extension with the given `extension_id` and for the given
  // `uninstall_reason`, waiting until uninstallation has finished.
  void UninstallExtension(const ExtensionId& extension_id,
                          UninstallReason uninstall_reason) {
    base::RunLoop run_loop;
    extension_registrar()->UninstallExtension(extension_id, uninstall_reason,
                                              /*error=*/nullptr,
                                              run_loop.QuitWhenIdleClosure());
    run_loop.Run();
  }

  // Adds a new MV2 extension with the given `name` to the profile, returning
  // it afterwards.
  const Extension* AddMV2Extension(std::string_view name) {
    return AddExtensionWithManifestVersion(name, 2);
  }

  const Extension* AddExtensionWithManifestVersion(std::string_view name,
                                                   int manifest_version) {
    static constexpr char kManifest[] =
        R"({
             "name": "%s",
             "manifest_version": %d,
             "version": "0.1"
           })";
    TestExtensionDir test_dir;
    test_dir.WriteManifest(
        base::StringPrintf(kManifest, name.data(), manifest_version));
    return InstallExtension(test_dir.UnpackedPath(), /*expected_change=*/1,
                            mojom::ManifestLocation::kInternal);
  }

  // Returns true if the extension was explicitly re-enabled by the user after
  // being disabled by the MV2 experiment.
  bool WasExtensionReEnabledByUser(const ExtensionId& extension_id) {
    return experiment_manager()->DidUserReEnableExtensionForTesting(
        extension_id);
  }

  // Returns the UKM entries for the Extensions.MV2ExtensionHandledInSoftDisable
  // event.
  std::vector<raw_ptr<const ukm::mojom::UkmEntry, VectorExperimental>>
  GetUkmEntries() {
    return ukm_recorder().GetEntriesByName(
        ukm::builders::Extensions_MV2ExtensionHandledInSoftDisable::kEntryName);
  }

  MV2ExperimentStage GetActiveExperimentStage() {
    return experiment_manager()->GetCurrentExperimentStage();
  }

  ExtensionPrefs* extension_prefs() { return ExtensionPrefs::Get(profile()); }

  ManifestV2ExperimentManager* experiment_manager() {
    return ManifestV2ExperimentManager::Get(profile());
  }

  base::HistogramTester& histogram_tester() { return histogram_tester_; }
  ukm::TestAutoSetUkmRecorder& ukm_recorder() { return *ukm_recorder_; }

 private:
  // Allows subclasses to add additional features to enable / disable.
  virtual void PopulateAdditionalFeatures(
      std::vector<base::test::FeatureRef>& enabled_features,
      std::vector<base::test::FeatureRef>& disabled_features) {}

  base::test::ScopedFeatureList feature_list_;
  base::HistogramTester histogram_tester_;
  std::optional<ukm::TestAutoSetUkmRecorder> ukm_recorder_;
};

// A test series to verify MV2 extensions are disabled on startup.
// Step 1 (Warning Only Stage): Install an MV2 extension.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       PRE_PRE_ExtensionsAreDisabledOnStartup) {
  EXPECT_EQ(MV2ExperimentStage::kWarning, GetActiveExperimentStage());

  const Extension* extension = AddMV2Extension("Test MV2 Extension");
  ASSERT_TRUE(extension);
}
// Step 2 (Warning Only Stage): Verify the MV2 extension is still enabled after
// restarting the browser. Since this is still a PRE_ stage, the disabling
// experiment isn't active, and MV2 extensions should be unaffected.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       PRE_ExtensionsAreDisabledOnStartup) {
  EXPECT_EQ(MV2ExperimentStage::kWarning, GetActiveExperimentStage());

  WaitForExtensionSystemReady();

  const Extension* extension = GetExtensionByName(
      "Test MV2 Extension", extension_registry()->enabled_extensions());

  ASSERT_TRUE(extension);
  const ExtensionId extension_id = extension->id();
  EXPECT_TRUE(
      extension_registry()->enabled_extensions().Contains(extension_id));

  EXPECT_TRUE(extension_prefs()->GetDisableReasons(extension_id).empty());

  histogram_tester().ExpectTotalCount(
      "Extensions.MV2Deprecation.MV2ExtensionState.Internal", 0);
}
// Step 3 (Disable Stage): Verify the extension is disabled. Now the disabling
// experiment is active, and any old MV2 extensions are disabled.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       ExtensionsAreDisabledOnStartup) {
  EXPECT_EQ(MV2ExperimentStage::kDisableWithReEnable,
            GetActiveExperimentStage());

  WaitForExtensionSystemReady();

  const Extension* extension = GetExtensionByName(
      "Test MV2 Extension",
      extension_registry()->GenerateInstalledExtensionsSet());

  ASSERT_TRUE(extension);
  const ExtensionId extension_id = extension->id();

  EXPECT_FALSE(
      extension_registry()->enabled_extensions().Contains(extension_id));
  EXPECT_TRUE(
      extension_registry()->disabled_extensions().Contains(extension_id));

  EXPECT_THAT(extension_prefs()->GetDisableReasons(extension_id),
              testing::UnorderedElementsAre(
                  disable_reason::DISABLE_UNSUPPORTED_MANIFEST_VERSION));

  // The extension is recorded as "soft disabled".
  histogram_tester().ExpectTotalCount(
      "Extensions.MV2Deprecation.MV2ExtensionState.Internal", 1);
  histogram_tester().ExpectBucketCount(
      "Extensions.MV2Deprecation.MV2ExtensionState.Internal",
      ManifestV2ExperimentManager::MV2ExtensionState::kSoftDisabled, 1);
}

// A test series to verify extensions that are re-enabled by the user do not
// get re-disabled on subsequent starts.
// Step 1 (Warning Only Stage): Install an MV2 extension.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       PRE_PRE_ExtensionsCanBeReEnabledByUsers) {
  EXPECT_EQ(MV2ExperimentStage::kWarning, GetActiveExperimentStage());

  const Extension* extension = AddMV2Extension("Test MV2 Extension");
  ASSERT_TRUE(extension);
}
// Step 2 (Disable Stage): The extension will be disabled by the experiment.
// Re-enable the extension.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       PRE_ExtensionsCanBeReEnabledByUsers) {
  EXPECT_EQ(MV2ExperimentStage::kDisableWithReEnable,
            GetActiveExperimentStage());

  WaitForExtensionSystemReady();

  const Extension* extension = GetExtensionByName(
      "Test MV2 Extension", extension_registry()->disabled_extensions());
  ASSERT_TRUE(extension);
  const ExtensionId extension_id = extension->id();

  // Before re-enabling the extension, there should be no UKM entries.
  EXPECT_TRUE(GetUkmEntries().empty());

  // Re-enable the disabled extension.
  extension_registrar()->EnableExtension(extension_id);

  // The extension should be properly re-enabled, the disable reasons cleared,
  // and the extension should be marked as explicitly re-enabled.
  EXPECT_TRUE(
      extension_registry()->enabled_extensions().Contains(extension_id));
  EXPECT_TRUE(extension_prefs()->GetDisableReasons(extension_id).empty());
  EXPECT_TRUE(WasExtensionReEnabledByUser(extension_id));

  // We should emit a UKM record for the re-enabling.
  auto entries = GetUkmEntries();
  ASSERT_EQ(1u, entries.size());
  auto* entry = entries.front().get();
  ukm_recorder().ExpectEntrySourceHasUrl(entry, extension->url());
  ukm_recorder().ExpectEntryMetric(
      entry,
      ukm::builders::Extensions_MV2ExtensionHandledInSoftDisable::kActionName,
      static_cast<int64_t>(ManifestV2ExperimentManager::
                               ExtensionMV2DeprecationAction::kReEnabled));
}
// Step 3 (Disable Stage): The extension should still be enabled on a subsequent
// start since the user explicitly chose to re-enable it.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       ExtensionsCanBeReEnabledByUsers) {
  EXPECT_EQ(MV2ExperimentStage::kDisableWithReEnable,
            GetActiveExperimentStage());

  WaitForExtensionSystemReady();

  const Extension* extension = GetExtensionByName(
      "Test MV2 Extension", extension_registry()->enabled_extensions());
  ASSERT_TRUE(extension);
  const ExtensionId extension_id = extension->id();

  EXPECT_TRUE(extension_prefs()->GetDisableReasons(extension_id).empty());
  EXPECT_TRUE(WasExtensionReEnabledByUser(extension_id));

  // The extension is reported as re-enabled by the user.
  histogram_tester().ExpectTotalCount(
      "Extensions.MV2Deprecation.MV2ExtensionState.Internal", 1);
  histogram_tester().ExpectBucketCount(
      "Extensions.MV2Deprecation.MV2ExtensionState.Internal",
      ManifestV2ExperimentManager::MV2ExtensionState::kUserReEnabled, 1);
}

// Tests that extensions are re-enabled automatically if they update to MV3.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       ExtensionsAreReEnabledWhenUpdatedToMV3) {
  EXPECT_EQ(MV2ExperimentStage::kDisableWithReEnable,
            GetActiveExperimentStage());

  WaitForExtensionSystemReady();

  static constexpr char kManifestTemplate[] =
      R"({
           "name": "Test Extension",
           "manifest_version": %d,
           "version": "%s"
         })";
  std::string mv2_manifest = base::StringPrintf(kManifestTemplate, 2, "1.0");
  std::string mv3_manifest = base::StringPrintf(kManifestTemplate, 3, "2.0");

  TestExtensionDir test_dir;
  test_dir.WriteManifest(mv2_manifest);
  base::FilePath mv2_crx = test_dir.Pack("mv2.crx");
  test_dir.WriteManifest(mv3_manifest);
  base::FilePath mv3_crx = test_dir.Pack("mv3.crx");

  const Extension* extension = InstallExtension(
      mv2_crx, /*expected_change=*/1, mojom::ManifestLocation::kInternal);
  ASSERT_TRUE(extension);
  const ExtensionId extension_id = extension->id();

  // Technically, this could be accomplished using a PRE_ test, similar to
  // other browser tests in this file. However, that makes it much more
  // difficult to update the extension to an MV3 version, since we couldn't
  // construct the extension dynamically.
  experiment_manager()->DisableAffectedExtensionsForTesting();

  // The MV2 extension is disabled.
  EXPECT_TRUE(
      extension_registry()->disabled_extensions().Contains(extension_id));
  EXPECT_THAT(extension_prefs()->GetDisableReasons(extension_id),
              testing::UnorderedElementsAre(
                  disable_reason::DISABLE_UNSUPPORTED_MANIFEST_VERSION));

  // Update the extension to MV3. Note: Even though this doesn't result in a
  // _new_ extension, the `expected_change` is 1 here because this results in
  // the extension being added to the enabled set (so the enabled extension
  // count is 1 higher than it was before).
  const Extension* updated_extension = UpdateExtension(extension_id, mv3_crx,
                                                       /*expected_change=*/1);
  ASSERT_TRUE(updated_extension);
  EXPECT_EQ(updated_extension->id(), extension_id);

  // The new MV3 extension should be enabled.
  EXPECT_EQ(3, updated_extension->manifest_version());
  EXPECT_TRUE(
      extension_registry()->enabled_extensions().Contains(extension_id));
  EXPECT_TRUE(extension_prefs()->GetDisableReasons(extension_id).empty());
  // The user didn't re-enable the extension, so it shouldn't be marked as such.
  EXPECT_FALSE(WasExtensionReEnabledByUser(extension_id));
}

// Tests that the MV2 deprecation notice for an extension is only acknowledged
// for the current stage.
// Step 1 (Warning Stage): Mark an extension's notice as acknowledged on this
// stage.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       PRE_MarkingNoticeAsAcknowledged) {
  EXPECT_EQ(MV2ExperimentStage::kWarning, GetActiveExperimentStage());

  WaitForExtensionSystemReady();

  // Add an extension and verify it's notice is not marked as acknowledged on
  // this stage.
  const Extension* extension = AddMV2Extension("Test MV2 Extension");
  ASSERT_TRUE(extension);
  EXPECT_FALSE(experiment_manager()->DidUserAcknowledgeNotice(extension->id()));

  // Mark the notice as acknowledged for this stage. Verify it's acknowledged.
  experiment_manager()->MarkNoticeAsAcknowledged(extension->id());
  EXPECT_TRUE(experiment_manager()->DidUserAcknowledgeNotice(extension->id()));
}
// Step 2 (Disable Stage): Verify extension's notice is not acknowledged on this
// stage. Mark notice as acknowledged on this stage.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       MarkingNoticeAsAcknowledged) {
  EXPECT_EQ(MV2ExperimentStage::kDisableWithReEnable,
            GetActiveExperimentStage());

  WaitForExtensionSystemReady();

  // Verify extension's notice is not marked as acknowledged on this stage, even
  // if it was acknowledged on the previous stage.
  const Extension* extension = GetExtensionByName(
      "Test MV2 Extension", extension_registry()->disabled_extensions());
  ASSERT_TRUE(extension);
  EXPECT_FALSE(experiment_manager()->DidUserAcknowledgeNotice(extension->id()));

  // Mark the notice as acknowledged for this stage. Verify it's acknowledged.
  experiment_manager()->MarkNoticeAsAcknowledged(extension->id());
  EXPECT_TRUE(experiment_manager()->DidUserAcknowledgeNotice(extension->id()));
}

// Tests that the MV2 deprecation global notice is only acknowledged for the
// current stage.
// Step 1 (Warning Stage): Mark the global notice as acknowledged
// on this stage.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       PRE_MarkingGlobalNoticeAsAcknowledged) {
  EXPECT_EQ(MV2ExperimentStage::kWarning, GetActiveExperimentStage());

  WaitForExtensionSystemReady();

  // Add an extension that should make the MV2 deprecation notice visible.
  // Verify global notice is not marked as acknowledged on this stage.
  const Extension* extension = AddMV2Extension("Test MV2 Extension");
  ASSERT_TRUE(extension);
  EXPECT_FALSE(experiment_manager()->DidUserAcknowledgeNoticeGlobally());

  // Mark the global notice as acknowledged for this stage. Verify it's
  // acknowledged.
  experiment_manager()->MarkNoticeAsAcknowledgedGlobally();
  EXPECT_TRUE(experiment_manager()->DidUserAcknowledgeNoticeGlobally());
}
// Step 2 (Disable Stage): Verify global notice is not acknowledged on this
// stage. Mark notice as acknowledged on this stage.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       MarkingGlobalNoticeAsAcknowledged) {
  EXPECT_EQ(MV2ExperimentStage::kDisableWithReEnable,
            GetActiveExperimentStage());

  WaitForExtensionSystemReady();

  // Verify global notice is not marked as acknowledged on this stage, even if
  // it was acknowledged on the previous stage.
  const Extension* extension = GetExtensionByName(
      "Test MV2 Extension", extension_registry()->disabled_extensions());
  ASSERT_TRUE(extension);
  EXPECT_FALSE(experiment_manager()->DidUserAcknowledgeNoticeGlobally());

  // Mark the global notice as acknowledged for this stage. Verify it's
  // acknowledged.
  experiment_manager()->MarkNoticeAsAcknowledgedGlobally();
  EXPECT_TRUE(experiment_manager()->DidUserAcknowledgeNoticeGlobally());
}

// Tests that if a user moves from a later experiment stage (disable with
// re-enable) to an earlier one (warning), any disabled extensions will be
// automatically re-enabled.
// First stage: install an MV2 extension.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       PRE_PRE_ExtensionsAreReEnabledIfExperimentDisabled) {
  EXPECT_EQ(MV2ExperimentStage::kWarning, GetActiveExperimentStage());

  const Extension* extension = AddMV2Extension("Test MV2 Extension");
  ASSERT_TRUE(extension);
}
// Second stage: MV2 deprecation experiment takes effect; extension is disabled.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       PRE_ExtensionsAreReEnabledIfExperimentDisabled) {
  EXPECT_EQ(MV2ExperimentStage::kDisableWithReEnable,
            GetActiveExperimentStage());

  WaitForExtensionSystemReady();

  const Extension* extension = GetExtensionByName(
      "Test MV2 Extension", extension_registry()->disabled_extensions());
  ASSERT_TRUE(extension);
  const ExtensionId extension_id = extension->id();
  EXPECT_THAT(extension_prefs()->GetDisableReasons(extension_id),
              testing::UnorderedElementsAre(
                  disable_reason::DISABLE_UNSUPPORTED_MANIFEST_VERSION));
}
// Third stage: Move the user back to the warning stage. The extension should be
// re-enabled.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       ExtensionsAreReEnabledIfExperimentDisabled) {
  EXPECT_EQ(MV2ExperimentStage::kWarning, GetActiveExperimentStage());

  WaitForExtensionSystemReady();

  const Extension* extension = GetExtensionByName(
      "Test MV2 Extension", extension_registry()->enabled_extensions());
  ASSERT_TRUE(extension);
  const ExtensionId extension_id = extension->id();

  EXPECT_TRUE(extension_prefs()->GetDisableReasons(extension_id).empty());
  // The user didn't re-enable the extension, so it shouldn't be marked as such.
  EXPECT_FALSE(WasExtensionReEnabledByUser(extension_id));

  // Since the user is no longer in the disable phase, no metrics should be
  // reported.
  histogram_tester().ExpectTotalCount(
      "Extensions.MV2Deprecation.MV2ExtensionState.Internal", 0);
}

// Tests that externally-installed extensions are allowed to be installed, but
// will still be disabled by the MV2 experiments.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       ExternalExtensionsCanBeInstalledButAreAlsoDisabled) {
  // External extensions are default-disabled on Windows and Mac. This won't
  // be affected by the MV2 deprecation, but for consistency of testing, we
  // disable this prompting in the test.
  FeatureSwitch::ScopedOverride feature_override(
      FeatureSwitch::prompt_for_external_extensions(), false);

  // TODO(devlin): Update this to a different extension so we use one dedicated
  // to this test ("good.crx" should likely be updated to MV3).
  static constexpr char kExtensionId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
  base::FilePath crx_path = test_data_dir_.AppendASCII("good.crx");

  // Install a new external extension.
  ExternalProviderManager* external_provider_manager =
      ExternalProviderManager::Get(profile());
  TestExtensionRegistryObserver observer(extension_registry());
  auto provider = std::make_unique<MockExternalProvider>(
      external_provider_manager, mojom::ManifestLocation::kExternalPref);
  provider->UpdateOrAddExtension(kExtensionId, "1.0.0.0", crx_path);
  external_provider_manager->AddProviderForTesting(std::move(provider));
  external_provider_manager->CheckForExternalUpdates();

  auto extension = observer.WaitForExtensionInstalled();
  EXPECT_EQ(extension->id(), kExtensionId);

  // The extension should install and be enabled. We allow installation of
  // external extensions (unlike webstore extensions) because we can't know if
  // the extension is MV2 or MV3 until we install it.
  // We could theoretically disable it immediately if it's MV2, but it'll get
  // disabled on the next run of Chrome.
  EXPECT_TRUE(
      extension_registry()->enabled_extensions().Contains(kExtensionId));
  EXPECT_TRUE(extension_prefs()->GetDisableReasons(kExtensionId).empty());

  // The extension should still be counted as "affected" by the MV2 deprecation.
  EXPECT_TRUE(experiment_manager()->IsExtensionAffected(*extension));

  // And should also be disabled when we check again.
  experiment_manager()->DisableAffectedExtensionsForTesting();
  EXPECT_TRUE(
      extension_registry()->disabled_extensions().Contains(kExtensionId));
  EXPECT_THAT(extension_prefs()->GetDisableReasons(kExtensionId),
              testing::UnorderedElementsAre(
                  disable_reason::DISABLE_UNSUPPORTED_MANIFEST_VERSION));
}

// Tests that a UKM event is emitted when the user uninstalls a disabled
// extension.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       UkmIsEmittedForExtensionWhenUninstalled) {
  EXPECT_EQ(MV2ExperimentStage::kDisableWithReEnable,
            GetActiveExperimentStage());

  WaitForExtensionSystemReady();

  const Extension* extension = AddMV2Extension("Test MV2 Extension");
  ASSERT_TRUE(extension);

  experiment_manager()->DisableAffectedExtensionsForTesting();

  EXPECT_TRUE(GetUkmEntries().empty());

  // Since the extension will be uninstalled (and the pointer will become unsafe
  // to use), cache its URL.
  const GURL extension_url = extension->url();
  UninstallExtension(extension->id(),
                     UninstallReason::UNINSTALL_REASON_USER_INITIATED);

  auto entries = GetUkmEntries();
  ASSERT_EQ(1u, entries.size());
  auto* entry = entries.front().get();
  ukm_recorder().ExpectEntrySourceHasUrl(entry, extension_url);
  ukm_recorder().ExpectEntryMetric(
      entry,
      ukm::builders::Extensions_MV2ExtensionHandledInSoftDisable::kActionName,
      static_cast<int64_t>(ManifestV2ExperimentManager::
                               ExtensionMV2DeprecationAction::kRemoved));
}

// Tests that UKM events are not emitted for unrelated uninstallations.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       UkmIsNotEmittedForOtherUninstallations) {
  EXPECT_EQ(MV2ExperimentStage::kDisableWithReEnable,
            GetActiveExperimentStage());

  WaitForExtensionSystemReady();

  const Extension* mv2_extension = AddMV2Extension("Test MV2 Extension");
  ASSERT_TRUE(mv2_extension);
  const Extension* mv3_extension =
      AddExtensionWithManifestVersion("Test MV3 Extension", 3);
  ASSERT_TRUE(mv3_extension);

  experiment_manager()->DisableAffectedExtensionsForTesting();

  EXPECT_TRUE(GetUkmEntries().empty());

  // Uninstalling an MV2 extension for a reason other than user uninstallation
  // should not trigger a UKM event.
  UninstallExtension(mv2_extension->id(),
                     UninstallReason::UNINSTALL_REASON_MANAGEMENT_API);
  EXPECT_TRUE(GetUkmEntries().empty());

  // Uninstalling extensions that aren't affected by the MV2 experiments should
  // not trigger a UKM event.
  UninstallExtension(mv3_extension->id(),
                     UninstallReason::UNINSTALL_REASON_USER_INITIATED);
  EXPECT_TRUE(GetUkmEntries().empty());
}

// Tests the flow from the "warning" phase to the "unsupported" phase.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       PRE_PRE_FlowFromWarningToUnsupported) {
  EXPECT_EQ(MV2ExperimentStage::kWarning, GetActiveExperimentStage());
  // Install two MV2 extensions.
  const Extension* extension1 = AddMV2Extension("Test MV2 Extension 1");
  ASSERT_TRUE(extension1);

  const Extension* extension2 = AddMV2Extension("Test MV2 Extension 2");
  ASSERT_TRUE(extension2);
}
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       PRE_FlowFromWarningToUnsupported) {
  EXPECT_EQ(MV2ExperimentStage::kDisableWithReEnable,
            GetActiveExperimentStage());

  WaitForExtensionSystemReady();

  // Both extensions should be disabled.
  const Extension* extension1 = GetExtensionByName(
      "Test MV2 Extension 1", extension_registry()->disabled_extensions());
  ASSERT_TRUE(extension1);
  const ExtensionId extension_id1 = extension1->id();
  EXPECT_THAT(extension_prefs()->GetDisableReasons(extension_id1),
              testing::UnorderedElementsAre(
                  disable_reason::DISABLE_UNSUPPORTED_MANIFEST_VERSION));
  const Extension* extension2 = GetExtensionByName(
      "Test MV2 Extension 2", extension_registry()->disabled_extensions());
  ASSERT_TRUE(extension1);
  const ExtensionId extension_id2 = extension2->id();
  EXPECT_THAT(extension_prefs()->GetDisableReasons(extension_id2),
              testing::UnorderedElementsAre(
                  disable_reason::DISABLE_UNSUPPORTED_MANIFEST_VERSION));

  // The extensions should be recorded as "soft disabled".
  histogram_tester().ExpectTotalCount(
      "Extensions.MV2Deprecation.MV2ExtensionState.Internal", 2);
  histogram_tester().ExpectBucketCount(
      "Extensions.MV2Deprecation.MV2ExtensionState.Internal",
      ManifestV2ExperimentManager::MV2ExtensionState::kSoftDisabled, 2);

  // The user should be allowed to re-enable the extensions.
  ExtensionSystem* system = ExtensionSystem::Get(profile());
  {
    disable_reason::DisableReason disable_reason = disable_reason::DISABLE_NONE;
    EXPECT_FALSE(system->management_policy()->MustRemainDisabled(
        extension1, &disable_reason));
    EXPECT_EQ(disable_reason::DISABLE_NONE, disable_reason);
  }
  {
    disable_reason::DisableReason disable_reason = disable_reason::DISABLE_NONE;
    EXPECT_FALSE(system->management_policy()->MustRemainDisabled(
        extension2, &disable_reason));
    EXPECT_EQ(disable_reason::DISABLE_NONE, disable_reason);
  }

  // Re-enable the first MV2 extension (this is allowed in this phase).
  extension_registrar()->EnableExtension(extension_id1);

  // The first extension should be properly re-enabled, the disable reasons
  // cleared, and the extension should be marked as explicitly re-enabled.
  EXPECT_TRUE(
      extension_registry()->enabled_extensions().Contains(extension_id1));
  EXPECT_TRUE(extension_prefs()->GetDisableReasons(extension_id1).empty());
  EXPECT_TRUE(WasExtensionReEnabledByUser(extension_id1));
}
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       FlowFromWarningToUnsupported) {
  EXPECT_EQ(MV2ExperimentStage::kUnsupported, GetActiveExperimentStage());

  WaitForExtensionSystemReady();

  // Both extensions should be disabled.
  // In the "unsupported" phase, both extensions should be disabled again, even
  // though the first was re-enabled in a previous phase.
  const Extension* extension1 = GetExtensionByName(
      "Test MV2 Extension 1", extension_registry()->disabled_extensions());
  ASSERT_TRUE(extension1);
  const ExtensionId extension_id1 = extension1->id();

  EXPECT_TRUE(WasExtensionReEnabledByUser(extension_id1));
  EXPECT_THAT(extension_prefs()->GetDisableReasons(extension_id1),
              testing::UnorderedElementsAre(
                  disable_reason::DISABLE_UNSUPPORTED_MANIFEST_VERSION));

  const Extension* extension2 = GetExtensionByName(
      "Test MV2 Extension 2", extension_registry()->disabled_extensions());
  ASSERT_TRUE(extension2);
  const ExtensionId extension_id2 = extension2->id();

  EXPECT_FALSE(WasExtensionReEnabledByUser(extension_id2));
  EXPECT_THAT(extension_prefs()->GetDisableReasons(extension_id2),
              testing::UnorderedElementsAre(
                  disable_reason::DISABLE_UNSUPPORTED_MANIFEST_VERSION));

  // The extensions should now be recorded as "hard disabled".
  histogram_tester().ExpectTotalCount(
      "Extensions.MV2Deprecation.MV2ExtensionState.Internal", 2);
  histogram_tester().ExpectBucketCount(
      "Extensions.MV2Deprecation.MV2ExtensionState.Internal",
      ManifestV2ExperimentManager::MV2ExtensionState::kHardDisabled, 2);

  // The user should no longer be allowed to re-enable the extensions.
  ExtensionSystem* system = ExtensionSystem::Get(profile());
  {
    disable_reason::DisableReason disable_reason = disable_reason::DISABLE_NONE;
    EXPECT_TRUE(system->management_policy()->MustRemainDisabled(
        extension1, &disable_reason));
    EXPECT_EQ(disable_reason::DISABLE_UNSUPPORTED_MANIFEST_VERSION,
              disable_reason);
  }
  {
    disable_reason::DisableReason disable_reason = disable_reason::DISABLE_NONE;
    EXPECT_TRUE(system->management_policy()->MustRemainDisabled(
        extension2, &disable_reason));
    EXPECT_EQ(disable_reason::DISABLE_UNSUPPORTED_MANIFEST_VERSION,
              disable_reason);
  }
}

// Tests that unpacked extensions can be installed in the disabled experiment
// phase.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       UnpackedExtensionsCanBeInstalledInDisabledPhase) {
  EXPECT_EQ(MV2ExperimentStage::kDisableWithReEnable,
            GetActiveExperimentStage());
  WaitForExtensionSystemReady();

  static constexpr char kMv2Manifest[] =
      R"({
           "name": "Simple MV2",
           "manifest_version": 2,
           "version": "0.1"
         })";
  TestExtensionDir test_dir;
  test_dir.WriteManifest(kMv2Manifest);

  base::RunLoop run_loop;
  std::string id;
  scoped_refptr<UnpackedInstaller> installer =
      UnpackedInstaller::Create(profile());
  auto on_complete = [&run_loop, &id](const Extension* extension,
                                      const base::FilePath& file_path,
                                      const std::string& error) {
    EXPECT_TRUE(extension);
    EXPECT_EQ("", error);
    id = extension->id();
    run_loop.Quit();
  };
  installer->set_completion_callback(base::BindLambdaForTesting(on_complete));
  installer->set_be_noisy_on_failure(false);
  installer->Load(test_dir.UnpackedPath());
  run_loop.Run();

  EXPECT_TRUE(extension_registry()->enabled_extensions().Contains(id));
}

// Tests that unpacked extensions cannot be installed in the unsupported
// experiment phase.
IN_PROC_BROWSER_TEST_F(ManifestV2ExperimentManagerBrowserTest,
                       UnpackedExtensionsCannotBeInstalledInUnsupportedPhase) {
  EXPECT_EQ(MV2ExperimentStage::kUnsupported, GetActiveExperimentStage());
  WaitForExtensionSystemReady();

  static constexpr char kMv2Manifest[] =
      R"({
           "name": "Simple MV2",
           "manifest_version": 2,
           "version": "0.1"
         })";
  TestExtensionDir test_dir;
  test_dir.WriteManifest(kMv2Manifest);

  base::RunLoop run_loop;
  std::string install_error;
  scoped_refptr<UnpackedInstaller> installer =
      UnpackedInstaller::Create(profile());
  auto on_complete = [&run_loop, &install_error](
                         const Extension* extension,
                         const base::FilePath& file_path,
                         const std::string& error) {
    install_error = error;
    run_loop.Quit();
  };
  installer->set_completion_callback(base::BindLambdaForTesting(on_complete));
  installer->set_be_noisy_on_failure(false);
  installer->Load(test_dir.UnpackedPath());
  run_loop.Run();

  EXPECT_EQ(
      "Cannot install extension because it uses an unsupported "
      "manifest version.",
      install_error);
}

class ManifestV2ExperimentWithLegacyExtensionSupportTest
    : public ManifestV2ExperimentManagerBrowserTest {
 public:
  ManifestV2ExperimentWithLegacyExtensionSupportTest() = default;
  ~ManifestV2ExperimentWithLegacyExtensionSupportTest() override = default;

 private:
  void PopulateAdditionalFeatures(
      std::vector<base::test::FeatureRef>& enabled_features,
      std::vector<base::test::FeatureRef>& disabled_features) override {
    enabled_features.push_back(extensions_features::kAllowLegacyMV2Extensions);
  }
};

// Tests that legacy unpacked MV2 extensions are still allowed (and aren't
// auto-disabled) if the kAllowLegacyMV2Extensions feature is enabled.
IN_PROC_BROWSER_TEST_F(
    ManifestV2ExperimentWithLegacyExtensionSupportTest,
    PRE_MV2ExtensionsAreNotDisabledIfLegacyExtensionSwitchIsApplied) {
  EXPECT_EQ(MV2ExperimentStage::kWarning, GetActiveExperimentStage());

  // Load two extensions: a packed extension and an unpacked extension.
  const Extension* packed_extension =
      AddMV2Extension("Test Packed MV2 Extension");
  ASSERT_TRUE(packed_extension);
  EXPECT_EQ(mojom::ManifestLocation::kInternal, packed_extension->location());

  const Extension* unpacked_extension =
      LoadExtension(test_data_dir_.AppendASCII("simple_mv2"));
  ASSERT_TRUE(unpacked_extension);
  EXPECT_EQ(mojom::ManifestLocation::kUnpacked, unpacked_extension->location());
}
IN_PROC_BROWSER_TEST_F(
    ManifestV2ExperimentWithLegacyExtensionSupportTest,
    MV2ExtensionsAreNotDisabledIfLegacyExtensionSwitchIsApplied) {
  EXPECT_EQ(MV2ExperimentStage::kUnsupported, GetActiveExperimentStage());

  WaitForExtensionSystemReady();

  // The packed extension should have been disabled.
  const Extension* packed_extension = GetExtensionByName(
      "Test Packed MV2 Extension", extension_registry()->disabled_extensions());
  ASSERT_TRUE(packed_extension);
  const ExtensionId packed_extension_id = packed_extension->id();

  EXPECT_THAT(extension_prefs()->GetDisableReasons(packed_extension_id),
              testing::UnorderedElementsAre(
                  disable_reason::DISABLE_UNSUPPORTED_MANIFEST_VERSION));
  // The user didn't re-enable the extension, so it shouldn't be marked as such.
  EXPECT_FALSE(WasExtensionReEnabledByUser(packed_extension_id));

  // The user is not allowed to re-enable the packed extension; the flag only
  // applies to unpacked extensions.
  ExtensionSystem* system = ExtensionSystem::Get(profile());
  disable_reason::DisableReason disable_reason = disable_reason::DISABLE_NONE;
  EXPECT_TRUE(system->management_policy()->MustRemainDisabled(packed_extension,
                                                              &disable_reason));
  EXPECT_EQ(disable_reason::DISABLE_UNSUPPORTED_MANIFEST_VERSION,
            disable_reason);

  // The unpacked extension should still be enabled.
  const Extension* unpacked_extension = GetExtensionByName(
      "Simple MV2 Extension", extension_registry()->enabled_extensions());
  ASSERT_TRUE(unpacked_extension);
  const ExtensionId unpacked_extension_id = unpacked_extension->id();

  EXPECT_TRUE(
      extension_prefs()->GetDisableReasons(unpacked_extension_id).empty());
  // The user didn't re-enable the extension, so it shouldn't be marked as such.
  EXPECT_FALSE(WasExtensionReEnabledByUser(unpacked_extension_id));

  // The user is allowed to re-enable the unpacked extension.
  disable_reason = disable_reason::DISABLE_NONE;
  EXPECT_FALSE(system->management_policy()->MustRemainDisabled(
      unpacked_extension, &disable_reason));
  EXPECT_EQ(disable_reason::DISABLE_NONE, disable_reason);
}

}  // namespace extensions