File: supervised_user_extensions_manager_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (566 lines) | stat: -rw-r--r-- 23,213 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
// 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/supervised_user/supervised_user_extensions_manager.h"

#include <string>
#include <vector>

#include "base/test/gtest_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/values.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/supervised_user/supervised_user_extensions_metrics_recorder.h"
#include "chrome/browser/supervised_user/supervised_user_test_util.h"
#include "chrome/test/base/testing_profile.h"
#include "components/prefs/pref_service.h"
#include "components/supervised_user/core/browser/supervised_user_utils.h"
#include "components/supervised_user/core/common/pref_names.h"
#include "components/version_info/version_info.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/features/feature_channel.h"
#include "extensions/common/manifest_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

using extensions::Extension;

class SupervisedUserExtensionsManagerTestBase
    : public extensions::ExtensionServiceTestBase {
 public:
  SupervisedUserExtensionsManagerTestBase()
      : channel_(version_info::Channel::DEV) {}
  ~SupervisedUserExtensionsManagerTestBase() override = default;

  void SetUp() override {
    ExtensionServiceTestBase::SetUp();
    ExtensionServiceInitParams params;
    params.profile_is_supervised = true;
    InitializeExtensionService(std::move(params));
    // Flush the message loop, to ensure that credentials have been loaded in
    // Identity Manager.
    base::RunLoop().RunUntilIdle();
  }

  void TearDown() override {
    // Flush the message loop, to ensure all posted tasks run.
    base::RunLoop().RunUntilIdle();
    ExtensionServiceTestBase::TearDown();
  }

  void CheckLocalApprovalMigrationForDesktopState(
      supervised_user::LocallyParentApprovedExtensionsMigrationState
          expected_migration_state) {
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
    auto* prefs = profile()->GetPrefs();
    CHECK(prefs);
    EXPECT_EQ(static_cast<int>(expected_migration_state),
              prefs->GetInteger(
                  prefs::kLocallyParentApprovedExtensionsMigrationState));
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
  }

  scoped_refptr<const extensions::Extension> MakeThemeExtension() {
    base::Value::Dict source;
    source.Set(extensions::manifest_keys::kName, "Theme");
    source.Set(extensions::manifest_keys::kTheme, base::Value::Dict());
    source.Set(extensions::manifest_keys::kVersion, "1.0");
    extensions::ExtensionBuilder builder;
    scoped_refptr<const extensions::Extension> extension =
        builder.SetManifest(std::move(source)).Build();
    return extension;
  }

  scoped_refptr<const extensions::Extension> MakeExtension(
      std::string name = "Extension") {
    scoped_refptr<const extensions::Extension> extension =
        extensions::ExtensionBuilder(name).Build();
    return extension;
  }

  void MakeSupervisedUserExtensionsManager() {
    manager_ = std::make_unique<extensions::SupervisedUserExtensionsManager>(
        profile());
  }

  extensions::ScopedCurrentChannel channel_;
  std::unique_ptr<extensions::SupervisedUserExtensionsManager> manager_;
};

class SupervisedUserExtensionsManagerTest
    : public SupervisedUserExtensionsManagerTestBase {};

TEST_F(SupervisedUserExtensionsManagerTest,
       ExtensionManagementPolicyProviderWithoutSUInitiatedInstalls) {
  MakeSupervisedUserExtensionsManager();
  supervised_user_test_util::
      SetSupervisedUserExtensionsMayRequestPermissionsPref(profile_.get(),
                                                           false);
  ASSERT_TRUE(profile_->IsChild());

  // Check that a supervised user can install and uninstall a theme even if
  // they are not allowed to install extensions.
  {
    scoped_refptr<const extensions::Extension> theme = MakeThemeExtension();

    std::u16string error_1;
    EXPECT_TRUE(manager_->UserMayLoad(theme.get(), &error_1));
    EXPECT_TRUE(error_1.empty());

    std::u16string error_2;
    EXPECT_FALSE(manager_->MustRemainInstalled(theme.get(), &error_2));
    EXPECT_TRUE(error_2.empty());
  }

  scoped_refptr<const extensions::Extension> extension = MakeExtension();
  // Installations are always allowed.
  std::u16string error_1;
  EXPECT_TRUE(manager_->UserMayLoad(extension.get(), &error_1));
  EXPECT_TRUE(error_1.empty());

  std::u16string error_2;
  EXPECT_TRUE(manager_->UserMayInstall(extension.get(), &error_2));
  EXPECT_TRUE(error_2.empty());

  std::u16string error_3;
  EXPECT_FALSE(manager_->MustRemainInstalled(extension.get(), &error_3));
  EXPECT_TRUE(error_3.empty());

#if DCHECK_IS_ON()
  EXPECT_FALSE(manager_->GetDebugPolicyProviderName().empty());
#endif
}

TEST_F(SupervisedUserExtensionsManagerTest,
       ExtensionManagementPolicyProviderWithSUInitiatedInstalls) {
  MakeSupervisedUserExtensionsManager();
    // Enable child users to initiate extension installs by simulating the
    // toggling of "Skip parent approval to install extensions" to disabled.
    supervised_user_test_util::SetSkipParentApprovalToInstallExtensionsPref(
        profile(), false);

  ASSERT_TRUE(profile_->IsChild());

  // The supervised user should be able to load and uninstall the extensions
  // they install.
  {
    scoped_refptr<const extensions::Extension> extension = MakeExtension();

    std::u16string error;
    EXPECT_TRUE(manager_->UserMayLoad(extension.get(), &error));
    EXPECT_TRUE(error.empty());

    std::u16string error_2;
    EXPECT_FALSE(manager_->MustRemainInstalled(extension.get(), &error_2));
    EXPECT_TRUE(error_2.empty());

    extensions::disable_reason::DisableReason reason =
        extensions::disable_reason::DISABLE_NONE;
    EXPECT_TRUE(manager_->MustRemainDisabled(extension.get(), &reason));
    EXPECT_EQ(extensions::disable_reason::DISABLE_CUSTODIAN_APPROVAL_REQUIRED,
              reason);

    std::u16string error_3;
    EXPECT_TRUE(manager_->UserMayModifySettings(extension.get(), &error_3));
    EXPECT_TRUE(error_3.empty());

    std::u16string error_4;
    EXPECT_TRUE(manager_->UserMayInstall(extension.get(), &error_4));
    EXPECT_TRUE(error_4.empty());
  }

#if DCHECK_IS_ON()
  EXPECT_FALSE(manager_->GetDebugPolicyProviderName().empty());
#endif
}

// Tests that on Desktop (Win/Linux/Mac) platforms,
// present extensions will be marked as locally parent-approved
// when the SupervisedUserExtensionsManager is created for a supervised user.
TEST_F(SupervisedUserExtensionsManagerTest,
       MigrateExtensionsToLocallyApproved) {
  ASSERT_TRUE(profile_->IsChild());
  base::HistogramTester histogram_tester;

  // Register the extensions.
  scoped_refptr<const Extension> approved_extn =
      MakeExtension("extension_test_1");
  scoped_refptr<const Extension> locally_approved_extn =
      MakeExtension("local_extension_test_1");
  registrar()->AddExtension(approved_extn);
  registrar()->AddExtension(locally_approved_extn);

  // Mark one extension as already parent-approved in the corresponding
  // preference.
  auto* prefs = profile()->GetPrefs();
  CHECK(prefs);
  base::Value::Dict approved_extensions;
  approved_extensions.Set(approved_extn->id(), true);
  prefs->SetDict(prefs::kSupervisedUserApprovedExtensions,
                 std::move(approved_extensions));

  // Create the object under test.
  MakeSupervisedUserExtensionsManager();

  bool has_local_approval_migration_run = false;
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
  auto expected_migration_state =
      supervised_user::LocallyParentApprovedExtensionsMigrationState::kComplete;
  has_local_approval_migration_run = true;
  EXPECT_EQ(
      static_cast<int>(expected_migration_state),
      prefs->GetInteger(prefs::kLocallyParentApprovedExtensionsMigrationState));
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)

  // The already approved extension should be allowed and not part of the
  // local-approved list.
  const base::Value::Dict& local_approved_extensions_pref =
      prefs->GetDict(prefs::kSupervisedUserLocallyParentApprovedExtensions);
  EXPECT_FALSE(local_approved_extensions_pref.contains(approved_extn->id()));
  EXPECT_TRUE(manager_->IsExtensionAllowed(*approved_extn));

  // The extensions approved in the migration should be allowed and part
  // of the local-approved list.
  int approved_extensions_count = has_local_approval_migration_run ? 1 : 0;
  EXPECT_EQ(
      has_local_approval_migration_run,
      local_approved_extensions_pref.contains(locally_approved_extn->id()));
  EXPECT_EQ(has_local_approval_migration_run,
            manager_->IsExtensionAllowed(*locally_approved_extn));
  histogram_tester.ExpectBucketCount(
      SupervisedUserExtensionsMetricsRecorder::kExtensionsHistogramName,
      SupervisedUserExtensionsMetricsRecorder::UmaExtensionState::
          kLocalApprovalGranted,
      approved_extensions_count);
  histogram_tester.ExpectTotalCount(
      extensions::kInitialLocallyApprovedExtensionCountWinLinuxMacHistogramName,
      approved_extensions_count);
}

// Tests that extensions missing parent approval are granted parent approval
// on their installation, when the "Extensions" Family Link toggle is ON.
TEST_F(SupervisedUserExtensionsManagerTest,
       GrantParentApprovalOnInstallationWhenExtensionsToggleOn) {
  ASSERT_TRUE(profile_->IsChild());
  base::HistogramTester histogram_tester;

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
  // Mark the migration done to avoid any interference with the one-off
  // migration.
  auto* prefs = profile()->GetPrefs();
  CHECK(prefs);
  prefs->SetInteger(
      prefs::kLocallyParentApprovedExtensionsMigrationState,
      static_cast<int>(
          supervised_user::LocallyParentApprovedExtensionsMigrationState::
              kComplete));
#endif

  // Create the object under test.
  MakeSupervisedUserExtensionsManager();

  // Set the Extensions switch to OFF. The extensions installed on this mode,
  // should be pending approval and disabled.
  supervised_user_test_util::SetSkipParentApprovalToInstallExtensionsPref(
      profile(), false);

  // Install an extension.
  scoped_refptr<const Extension> extn_with_switch_off =
      MakeExtension("extension_test_1");
  registrar()->OnExtensionInstalled(extn_with_switch_off.get(),
                                    /*page_ordinal=*/syncer::StringOrdinal());

  extensions::disable_reason::DisableReason reason;
  EXPECT_FALSE(manager_->IsExtensionAllowed(*extn_with_switch_off.get()));
  EXPECT_TRUE(
      manager_->MustRemainDisabled(extn_with_switch_off.get(), &reason));

  histogram_tester.ExpectTotalCount(
      extensions::kExtensionApprovalsCountOnExtensionToggleHistogramName, 0);
  // Set the Extensions switch to ON. Install another extension which should be
  // granted parental approval by the end of the installation.
  supervised_user_test_util::SetSkipParentApprovalToInstallExtensionsPref(
      profile(), true);

  // Toggling the "Extensions" results in granting approval to the existing
  // extension.
  int approved_extensions_count = 1;
  histogram_tester.ExpectBucketCount(
      SupervisedUserExtensionsMetricsRecorder::kExtensionsHistogramName,
      SupervisedUserExtensionsMetricsRecorder::UmaExtensionState::
          kApprovalGrantedByDefault,
      approved_extensions_count);
  // The entry point of the implicit approval is recorded.
  histogram_tester.ExpectBucketCount(
      SupervisedUserExtensionsMetricsRecorder::
          kImplicitParentApprovalGrantEntryPointHistogramName,
      SupervisedUserExtensionsMetricsRecorder::
          ImplicitExtensionApprovalEntryPoint::
              kOnExtensionsSwitchFlippedToEnabled,
      approved_extensions_count);
  // The number of auto-approved extensions is recorded.
  histogram_tester.ExpectTotalCount(
      extensions::kExtensionApprovalsCountOnExtensionToggleHistogramName,
      approved_extensions_count);

  // Install an extension.
  scoped_refptr<const Extension> extn_with_switch_on =
      MakeExtension("extension_test_2");
  registrar()->OnExtensionInstalled(extn_with_switch_on.get(),
                                    /*page_ordinal=*/syncer::StringOrdinal());

  EXPECT_TRUE(manager_->IsExtensionAllowed(*extn_with_switch_on.get()));
  EXPECT_TRUE(
      !manager_->MustRemainDisabled(extn_with_switch_on.get(), &reason));
  EXPECT_TRUE(profile()
                  ->GetPrefs()
                  ->GetDict(prefs::kSupervisedUserApprovedExtensions)
                  .contains(extn_with_switch_on->id()));

  histogram_tester.ExpectBucketCount(
      SupervisedUserExtensionsMetricsRecorder::kExtensionsHistogramName,
      SupervisedUserExtensionsMetricsRecorder::UmaExtensionState::
          kApprovalGrantedByDefault,
      approved_extensions_count + 1);
  // The migration to locally approved extensions has occurred before we
  // installed any extensions, so not local approvals have been granted.
  histogram_tester.ExpectBucketCount(
      SupervisedUserExtensionsMetricsRecorder::kExtensionsHistogramName,
      SupervisedUserExtensionsMetricsRecorder::UmaExtensionState::
          kLocalApprovalGranted,
      0);
  histogram_tester.ExpectTotalCount(
      extensions::kInitialLocallyApprovedExtensionCountWinLinuxMacHistogramName,
      0);
  // The entry point of the implicit approval is recorded.
  histogram_tester.ExpectBucketCount(
      SupervisedUserExtensionsMetricsRecorder::
          kImplicitParentApprovalGrantEntryPointHistogramName,
      SupervisedUserExtensionsMetricsRecorder::
          ImplicitExtensionApprovalEntryPoint::
              OnExtensionInstallationWithExtensionsSwitchEnabled,
      1);
  histogram_tester.ExpectTotalCount(
      SupervisedUserExtensionsMetricsRecorder::
          kImplicitParentApprovalGrantEntryPointHistogramName,
      approved_extensions_count + 1);
}

// Tests that extensions missing parent approval are granted parent approval
// when the "Extensions" toggle is flipped to ON.
TEST_F(SupervisedUserExtensionsManagerTest,
       GrantParentApprovalOnExtensionsWhenExtensionsToggleSetToOn) {
  ASSERT_TRUE(profile_->IsChild());

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
  // Mark the migration done to avoid any interference with the one-off
  // migration.
  auto* prefs = profile()->GetPrefs();
  CHECK(prefs);
  prefs->SetInteger(
      prefs::kLocallyParentApprovedExtensionsMigrationState,
      static_cast<int>(
          supervised_user::LocallyParentApprovedExtensionsMigrationState::
              kComplete));
#endif

  // Create the object under test.
  MakeSupervisedUserExtensionsManager();

  // Set the Extensions switch to OFF. The extensions installed on this mode,
  // should be pending approval and disabled.
  supervised_user_test_util::SetSkipParentApprovalToInstallExtensionsPref(
      profile(), false);

  // Install an extension.
  scoped_refptr<const Extension> extn_with_switch_off =
      MakeExtension("extension_test_1");
  registrar()->OnExtensionInstalled(extn_with_switch_off.get(),
                                    /*page_ordinal=*/syncer::StringOrdinal());

  extensions::disable_reason::DisableReason reason;
  EXPECT_FALSE(manager_->IsExtensionAllowed(*extn_with_switch_off.get()));
  EXPECT_TRUE(
      manager_->MustRemainDisabled(extn_with_switch_off.get(), &reason));

  // Set the Extensions switch to ON. The extension should have been granted
  // parent approval when the SkipParentApprovalToInstallExtension preference is
  // flipped.
  supervised_user_test_util::SetSkipParentApprovalToInstallExtensionsPref(
      profile(), true);

  EXPECT_TRUE(manager_->IsExtensionAllowed(*extn_with_switch_off.get()));
  EXPECT_TRUE(
      !manager_->MustRemainDisabled(extn_with_switch_off.get(), &reason));
  EXPECT_TRUE(profile()
                  ->GetPrefs()
                  ->GetDict(prefs::kSupervisedUserApprovedExtensions)
                  .contains(extn_with_switch_off->id()));
}

// Tests the local approval is revoked on uninstalling the extension or
// when the extension gains normal parental approval.
TEST_F(SupervisedUserExtensionsManagerTest, RevokeLocalApproval) {
  ASSERT_TRUE(profile_->IsChild());

  scoped_refptr<const Extension> locally_approved_extn1 =
      MakeExtension("extension_test_1");
  registrar()->AddExtension(locally_approved_extn1);
  scoped_refptr<const Extension> locally_approved_extn2 =
      MakeExtension("extension_test_2");
  registrar()->AddExtension(locally_approved_extn2);

  // Create the object under test.
  MakeSupervisedUserExtensionsManager();

  bool has_local_approval_migration_run = false;
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
  has_local_approval_migration_run = true;
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)

  auto* prefs = profile()->GetPrefs();
  CHECK(prefs);
  const base::Value::Dict& local_approved_extensions_pref =
      prefs->GetDict(prefs::kSupervisedUserLocallyParentApprovedExtensions);
  EXPECT_EQ(
      has_local_approval_migration_run,
      local_approved_extensions_pref.contains(locally_approved_extn1->id()));
  EXPECT_EQ(
      has_local_approval_migration_run,
      registry()->enabled_extensions().Contains(locally_approved_extn1->id()));

  // Uninstalling the extension also removes the local approval.
  ASSERT_TRUE(registrar()->UninstallExtension(
      locally_approved_extn1->id(), extensions::UNINSTALL_REASON_FOR_TESTING,
      nullptr));
  EXPECT_FALSE(
      local_approved_extensions_pref.contains(locally_approved_extn1->id()));

  // Granting parent approval (typically from another client) removes the local
  // approval. The extension remains allowed.
  manager_->AddExtensionApproval(*locally_approved_extn2);
  EXPECT_FALSE(
      local_approved_extensions_pref.contains(locally_approved_extn2->id()));
  EXPECT_TRUE(manager_->IsExtensionAllowed(*locally_approved_extn2));
}

// Whether the local approval extension migration for
// Win/Mac/Linux platforms has already run at the beginning of
// each test case.
enum class LocalApprovalMigrationForDesktopState : int {
  kExecuted = 0,
  kPending = 1
};

// Tests the managed extensions' state when an existing profile becomes
// supervised.
class AddingSupervisionTest : public SupervisedUserExtensionsManagerTestBase,
                              public ::testing::WithParamInterface<
                                  LocalApprovalMigrationForDesktopState> {
 public:
  AddingSupervisionTest() = default;

  LocalApprovalMigrationForDesktopState
  GetInitialLocalApprovalMigrationForDesktopState() {
    return GetParam();
  }

  void MaybeMarkLocalApprovalMigrationDone() {
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
    if (GetInitialLocalApprovalMigrationForDesktopState() ==
        LocalApprovalMigrationForDesktopState::kExecuted) {
      auto* prefs = profile()->GetPrefs();
      CHECK(prefs);
      prefs->SetInteger(
          prefs::kLocallyParentApprovedExtensionsMigrationState,
          static_cast<int>(
              supervised_user::LocallyParentApprovedExtensionsMigrationState::
                  kComplete));
    }
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
  }
};

// Tests that on Desktop (Win/Linux/Mac) platforms, when:
// 1) parental controls on extensions apply for the first time (solution
// release) and
// 2) a supervised user sings-in on an existing unsupervised profile or
// the existing profile becomes supervised (Gellerized) then
// the existing extensions remain disabled and pending approval.
// The states of the present extensions should not be impacted by the local
// extension approval migration that is executed on feature release for Desktop
// platforms.
TEST_P(AddingSupervisionTest,
       DisableExistingExtensionsOnProfileBecomingSupervised) {
  MaybeMarkLocalApprovalMigrationDone();
  profile()->AsTestingProfile()->SetIsSupervisedProfile(false);
  ASSERT_TRUE(!profile_->IsChild());

  scoped_refptr<const Extension> existing_extension =
      MakeExtension("extension_test_2");
  registrar()->AddExtension(existing_extension);

  supervised_user::LocallyParentApprovedExtensionsMigrationState
      expected_migragtion_state = supervised_user::
          LocallyParentApprovedExtensionsMigrationState::kNeedToRun;
  if (GetInitialLocalApprovalMigrationForDesktopState() ==
      LocalApprovalMigrationForDesktopState::kExecuted) {
    expected_migragtion_state = supervised_user::
        LocallyParentApprovedExtensionsMigrationState::kComplete;
  }
  CheckLocalApprovalMigrationForDesktopState(expected_migragtion_state);

  // Create the object under test.
  MakeSupervisedUserExtensionsManager();

  expected_migragtion_state =
      supervised_user::LocallyParentApprovedExtensionsMigrationState::kComplete;
  CheckLocalApprovalMigrationForDesktopState(expected_migragtion_state);

  auto* prefs = profile()->GetPrefs();
  CHECK(prefs);
  const base::Value::Dict& local_approved_extensions_pref =
      prefs->GetDict(prefs::kSupervisedUserLocallyParentApprovedExtensions);
  EXPECT_FALSE(
      local_approved_extensions_pref.contains(existing_extension->id()));
  EXPECT_TRUE(
      registry()->enabled_extensions().Contains(existing_extension->id()));

  // Make the user supervised.
  profile()->AsTestingProfile()->SetIsSupervisedProfile(true);
  ASSERT_TRUE(profile_->IsChild());

  const base::Value::Dict& local_approved_extensions_pref_post_migr =
      prefs->GetDict(prefs::kSupervisedUserLocallyParentApprovedExtensions);
  // Check that the pre-existing extensions should have been be excluded from
  // the local approval migration.
  EXPECT_FALSE(local_approved_extensions_pref_post_migr.contains(
      existing_extension->id()));
  EXPECT_FALSE(manager_->IsExtensionAllowed(*existing_extension));

  EXPECT_TRUE(
      registry()->disabled_extensions().Contains(existing_extension->id()));
}

INSTANTIATE_TEST_SUITE_P(
    All,
    AddingSupervisionTest,
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
    testing::Values(LocalApprovalMigrationForDesktopState::kExecuted,
                    LocalApprovalMigrationForDesktopState::kPending),
#else   // ChromeOS case
        // The local approval migration is not applicable for ChromeOS.
        // The test just needs to be executed once, the value of
        // LocalApprovalMigrationForDesktopState does not matter.
    testing::Values(LocalApprovalMigrationForDesktopState::kExecuted),
#endif  // BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
    [](const auto& info) {
      return std::string(
          info.param == LocalApprovalMigrationForDesktopState::kExecuted
              ? "PostReleaseSkipParentApprovalFeature"
              : "PreReleaseSkipParentApprovalFeature");
    });