File: user_scripts_apitest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (782 lines) | stat: -rw-r--r-- 31,746 bytes parent folder | download | duplicates (2)
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
// 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/extensions/api/user_scripts/user_scripts_apitest.h"

#include "base/feature_list.h"
#include "base/one_shot_event.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/scoped_run_loop_timeout.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_util.h"
#include "chrome/browser/extensions/user_scripts_test_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_navigation_observer.h"
#include "extensions/browser/background_script_executor.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_util.h"
#include "extensions/browser/renderer_startup_helper.h"
#include "extensions/browser/user_script_manager.h"
#include "extensions/common/extension_features.h"
#include "extensions/common/features/feature_developer_mode_only.h"
#include "extensions/common/user_scripts_allowed_state.h"
#include "extensions/common/utils/content_script_utils.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "extensions/test/test_extension_dir.h"
#include "net/dns/mock_host_resolver.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace extensions {

void UserScriptsAPITest::SetUpOnMainThread() {
  ExtensionApiTest::SetUpOnMainThread();

  host_resolver()->AddRule("*", "127.0.0.1");
  ASSERT_TRUE(StartEmbeddedTestServer());
}

void UserScriptsAPITest::OpenInCurrentTab(const GURL& url) {
  content::WebContents* web_contents =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_TRUE(web_contents);

  content::TestNavigationObserver nav_observer(web_contents);
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  nav_observer.Wait();

  EXPECT_TRUE(nav_observer.last_navigation_succeeded());
  EXPECT_EQ(url, web_contents->GetLastCommittedURL());
}

content::RenderFrameHost* UserScriptsAPITest::OpenInNewTab(const GURL& url) {
  content::TestNavigationObserver nav_observer(url);
  nav_observer.StartWatchingNewWebContents();
  content::RenderFrameHost* tab = ui_test_utils::NavigateToURLWithDisposition(
      browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
      ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
  nav_observer.Wait();

  EXPECT_TRUE(nav_observer.last_navigation_succeeded());
  EXPECT_EQ(url, browser()
                     ->tab_strip_model()
                     ->GetActiveWebContents()
                     ->GetLastCommittedURL());

  return tab;
}

content::EvalJsResult UserScriptsAPITest::GetInjectedElements(
    content::RenderFrameHost* host) {
  static constexpr char kGetInjectedScripts[] =
      R"(const divs = document.body.getElementsByTagName('div');
           JSON.stringify(Array.from(divs).map(div => div.id).sort());)";
  return content::EvalJs(host, kGetInjectedScripts);
}

  // Loads the extension and pauses in-between loading and running the tests to
  // enable the userScripts API.
testing::AssertionResult UserScriptsAPITest::RunUserScriptsExtensionTestImpl(
    const base::FilePath& extension_path,
    bool allow_api) {
  // Load the extension.
  ExtensionTestMessageListener test_ready_listener(
      "ready",
      allow_api ? ReplyBehavior::kWillReply : ReplyBehavior::kWontReply);
  ResultCatcher catcher;
  const Extension* extension = LoadExtension(extension_path);
  if (!extension) {
    return testing::AssertionFailure() << "Failed to load extension";
  }

  if (allow_api) {
    // Wait until extension tests are ready to run, then allow the
    // userScripts API, then continue on with the API testing.
    bool extension_ready = test_ready_listener.WaitUntilSatisfied();
    if (!extension_ready) {
      testing::AssertionFailure()
          << "extension did not signal that it was ready after loading";
    }
    user_scripts_test_util::SetUserScriptsAPIAllowed(profile(), extension->id(),
                                                     /*allowed=*/true);
    test_ready_listener.Reply("");
  }

  // Observe each test result.
  {
    base::test::ScopedRunLoopTimeout timeout(
        FROM_HERE, std::nullopt,
        base::BindRepeating(
            [](const base::FilePath& extension_path) {
              return "GetNextResult timeout while "
                     "RunUserScriptsExtensionTest: " +
                     extension_path.MaybeAsASCII();
            },
            extension_path));
    if (!catcher.GetNextResult()) {
      return testing::AssertionFailure() << catcher.message();
    }
  }

  return testing::AssertionSuccess();
  ;
}

testing::AssertionResult UserScriptsAPITest::RunUserScriptsExtensionTest(
    const char* extension_sub_path) {
  const base::FilePath& root_path = test_data_dir_;
  base::FilePath extension_path = root_path.AppendASCII(extension_sub_path);
  return RunUserScriptsExtensionTestImpl(extension_path, /*allow_api=*/true);
}

testing::AssertionResult
UserScriptsAPITest::RunUserScriptsExtensionTestNotAllowed(
    const base::FilePath& extension_path) {
  return RunUserScriptsExtensionTestImpl(extension_path, /*allow_api=*/false);
}

UserScriptsAPITest::UserScriptsAPITest() {
  if (GetParam()) {
    scoped_feature_list_.InitWithFeatures(
        /*enabled_features=*/
        {extensions_features::kApiUserScriptsMultipleWorlds,
         extensions_features::kApiUserScriptsExecute,
         extensions_features::kUserScriptUserExtensionToggle},
        /*disabled_features=*/{});
  } else {
    scoped_feature_list_.InitWithFeatures(
        /*enabled_features=*/{extensions_features::
                                  kApiUserScriptsMultipleWorlds,
                              extensions_features::kApiUserScriptsExecute},
        /*disabled_features=*/{
            extensions_features::kUserScriptUserExtensionToggle});
  }
}

// TODO(crbug.com/40935741, crbug.com/335421977): Flaky on Linux debug and on
// "Linux ChromiumOS MSan Tests".
#if (BUILDFLAG(IS_LINUX) && !defined(NDEBUG)) || \
    (BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER))
#define MAYBE_RegisterUserScripts DISABLED_RegisterUserScripts
#else
#define MAYBE_RegisterUserScripts RegisterUserScripts
#endif
IN_PROC_BROWSER_TEST_P(UserScriptsAPITest, MAYBE_RegisterUserScripts) {
  ASSERT_TRUE(RunUserScriptsExtensionTest("user_scripts/register")) << message_;
}

IN_PROC_BROWSER_TEST_P(UserScriptsAPITest, GetUserScripts) {
  ASSERT_TRUE(RunUserScriptsExtensionTest("user_scripts/get_scripts"))
      << message_;
}

IN_PROC_BROWSER_TEST_P(UserScriptsAPITest, UnregisterUserScripts) {
  ASSERT_TRUE(RunUserScriptsExtensionTest("user_scripts/unregister"))
      << message_;
}

IN_PROC_BROWSER_TEST_P(UserScriptsAPITest, UpdateUserScripts) {
  ASSERT_TRUE(RunUserScriptsExtensionTest("user_scripts/update")) << message_;
}

IN_PROC_BROWSER_TEST_P(UserScriptsAPITest, ExecuteUserScripts) {
  ASSERT_TRUE(RunUserScriptsExtensionTest("user_scripts/execute")) << message_;
}

IN_PROC_BROWSER_TEST_P(UserScriptsAPITest, ExecuteUserScripts_Subframes) {
  // Open up two tabs, each with cross-site iframes, one at a.com and one at
  // d.com. In both cases, the cross-site iframes point to b.com and c.com.
  OpenInCurrentTab(
      embedded_test_server()->GetURL("a.com", "/iframe_cross_site.html"));
  OpenInNewTab(
      embedded_test_server()->GetURL("d.com", "/iframe_cross_site.html"));

  ASSERT_TRUE(
      RunUserScriptsExtensionTest("user_scripts/execute_with_subframes"))
      << message_;
}

IN_PROC_BROWSER_TEST_P(UserScriptsAPITest, ExecuteUserScripts_SizeLimit) {
  auto single_scripts_limit_reset =
      script_parsing::CreateScopedMaxScriptLengthForTesting(700u);
  ASSERT_TRUE(RunUserScriptsExtensionTest("user_scripts/execute_size_limit"))
      << message_;
}

// TODO(crbug.com/335421977): Flaky on "Linux ChromiumOS MSan Tests".
#if BUILDFLAG(IS_CHROMEOS) && defined(MEMORY_SANITIZER)
#define MAYBE_ConfigureWorld DISABLED_ConfigureWorld
#else
#define MAYBE_ConfigureWorld ConfigureWorld
#endif
IN_PROC_BROWSER_TEST_P(UserScriptsAPITest, MAYBE_ConfigureWorld) {
  ASSERT_TRUE(RunUserScriptsExtensionTest("user_scripts/configure_world"))
      << message_;
}

IN_PROC_BROWSER_TEST_P(UserScriptsAPITest, GetAndRemoveWorlds) {
  ASSERT_TRUE(RunUserScriptsExtensionTest("user_scripts/get_and_remove_worlds"))
      << message_;
}

IN_PROC_BROWSER_TEST_P(UserScriptsAPITest,
                       UserScriptInjectionOrderIsAlphabetical) {
  ASSERT_TRUE(RunUserScriptsExtensionTest("user_scripts/injection_order"))
      << message_;
}

// Tests that registered user scripts are disabled when the userScripts API is
// not allowed and are re-enabled if when the API is allowed again.
IN_PROC_BROWSER_TEST_P(UserScriptsAPITest,
                       UserScriptsAreDisabledWhenAPIIsNotAllowed) {
  const Extension* extension =
      LoadExtension(test_data_dir_.AppendASCII("user_scripts/allowed_tests"));
  ASSERT_TRUE(extension);
  user_scripts_test_util::SetUserScriptsAPIAllowed(profile(), extension->id(),
                                                   /*allowed=*/true);

  // Register a user script and a content script.
  EXPECT_EQ("success",
            BackgroundScriptExecutor::ExecuteScript(
                profile(), extension->id(), "registerUserScripts();",
                BackgroundScriptExecutor::ResultCapture::kSendScriptResult));
  EXPECT_EQ("success",
            BackgroundScriptExecutor::ExecuteScript(
                profile(), extension->id(), "registerContentScript();",
                BackgroundScriptExecutor::ResultCapture::kSendScriptResult));

  const GURL url =
      embedded_test_server()->GetURL("example.com", "/simple.html");

  // Open a new tab.
  content::RenderFrameHost* new_tab = OpenInNewTab(url);

  // Since the userScript API is available (as part of this test suite's setup),
  // both the user script and the content script should inject.
  EXPECT_EQ(R"(["content-script","user-script-code","user-script-file"])",
            GetInjectedElements(new_tab));

  user_scripts_test_util::SetUserScriptsAPIAllowed(profile(), extension->id(),
                                                   /*allowed=*/false);

  // Open a new tab. Now, user scripts should be disabled. However, content
  // scripts should still inject.
  new_tab = OpenInNewTab(url);
  EXPECT_EQ(R"(["content-script"])", GetInjectedElements(new_tab));

  user_scripts_test_util::SetUserScriptsAPIAllowed(profile(), extension->id(),
                                                   /*allowed=*/true);

  // Open a new tab. The user script should inject again.
  new_tab = OpenInNewTab(url);
  EXPECT_EQ(R"(["content-script","user-script-code","user-script-file"])",
            GetInjectedElements(new_tab));
}

// Tests that unregisterContentScripts unregisters only content scripts and
// not user scripts.
IN_PROC_BROWSER_TEST_P(UserScriptsAPITest,
                       ScriptingAPIDoesNotAffectUserScripts) {
  ASSERT_TRUE(RunUserScriptsExtensionTest("scripting/dynamic_user_scripts"))
      << message_;
}

INSTANTIATE_TEST_SUITE_P(All,
                         UserScriptsAPITest,
                         // extensions_features::kUserScriptUserExtensionToggle
                         testing::Bool());

// Base test fixture for tests spanning multiple sessions where a custom arg
// is set before the test is run.
class PersistentUserScriptsAPITest : public UserScriptsAPITest {
 public:
  PersistentUserScriptsAPITest() = default;

  // UserScriptsAPITest override.
  void SetUp() override {
    // Initialize the listener object here before calling SetUp. This avoids a
    // race condition where the extension loads (as part of browser startup) and
    // sends a message before a message listener in C++ has been initialized.
    listener_ = std::make_unique<ExtensionTestMessageListener>(
        "ready", ReplyBehavior::kWillReply);

    UserScriptsAPITest::SetUp();
  }

  // Reset listener before the browser gets torn down.
  void TearDownOnMainThread() override {
    listener_.reset();
    UserScriptsAPITest::TearDownOnMainThread();
  }

 protected:
  // Used to wait for results from extension tests. This is initialized before
  // the test is run which avoids a race condition where the extension is loaded
  // (as part of startup) and finishes its tests before the ResultCatcher is
  // created.
  ResultCatcher result_catcher_;

  // Used to wait for the extension to load and send a ready message so the test
  // can reply which the extension waits for to start its testing functions.
  // This ensures that the testing functions will run after the browser has
  // finished initializing.
  std::unique_ptr<ExtensionTestMessageListener> listener_;
};

// Tests that registered user scripts persist across sessions. The test is run
// across three sessions.
IN_PROC_BROWSER_TEST_P(PersistentUserScriptsAPITest,
                       PRE_PRE_PersistentScripts) {
  const Extension* extension = LoadExtension(
      test_data_dir_.AppendASCII("user_scripts/persistent_scripts"));
  ASSERT_TRUE(extension);
  ASSERT_TRUE(listener_->WaitUntilSatisfied());
  user_scripts_test_util::SetUserScriptsAPIAllowed(profile(), extension->id(),
                                                   /*allowed=*/true);
  listener_->Reply(
      testing::UnitTest::GetInstance()->current_test_info()->name());
  EXPECT_TRUE(result_catcher_.GetNextResult()) << result_catcher_.message();
}

IN_PROC_BROWSER_TEST_P(PersistentUserScriptsAPITest, PRE_PersistentScripts) {
  ASSERT_TRUE(listener_->WaitUntilSatisfied());
  listener_->Reply(
      testing::UnitTest::GetInstance()->current_test_info()->name());
  EXPECT_TRUE(result_catcher_.GetNextResult()) << result_catcher_.message();
}

IN_PROC_BROWSER_TEST_P(PersistentUserScriptsAPITest, PersistentScripts) {
  ASSERT_TRUE(listener_->WaitUntilSatisfied());
  listener_->Reply(
      testing::UnitTest::GetInstance()->current_test_info()->name());
  EXPECT_TRUE(result_catcher_.GetNextResult()) << result_catcher_.message();
}

// Tests that the world configuration of a registered user script is persisted
// across sessions. The test is run across three sessions.
IN_PROC_BROWSER_TEST_P(PersistentUserScriptsAPITest,
                       PRE_PRE_PersistentWorldConfiguration) {
  const Extension* extension = LoadExtension(
      test_data_dir_.AppendASCII("user_scripts/persistent_configure_world"));
  ASSERT_TRUE(extension);
  ASSERT_TRUE(listener_->WaitUntilSatisfied());
  user_scripts_test_util::SetUserScriptsAPIAllowed(profile(), extension->id(),
                                                   /*allowed=*/true);
  listener_->Reply(
      testing::UnitTest::GetInstance()->current_test_info()->name());
  EXPECT_TRUE(result_catcher_.GetNextResult()) << result_catcher_.message();
}

IN_PROC_BROWSER_TEST_P(PersistentUserScriptsAPITest,
                       PRE_PersistentWorldConfiguration) {
  ASSERT_TRUE(listener_->WaitUntilSatisfied());
  listener_->Reply(
      testing::UnitTest::GetInstance()->current_test_info()->name());
  EXPECT_TRUE(result_catcher_.GetNextResult()) << result_catcher_.message();
}

IN_PROC_BROWSER_TEST_P(PersistentUserScriptsAPITest,
                       PersistentWorldConfiguration) {
  ASSERT_TRUE(listener_->WaitUntilSatisfied());
  listener_->Reply(
      testing::UnitTest::GetInstance()->current_test_info()->name());
  EXPECT_TRUE(result_catcher_.GetNextResult()) << result_catcher_.message();
}

INSTANTIATE_TEST_SUITE_P(All,
                         PersistentUserScriptsAPITest,
                         // extensions_features::kUserScriptUserExtensionToggle
                         testing::Bool());

// A test suite that runs without developer mode enabled.
class UserScriptsAPITestWithoutDeveloperMode : public UserScriptsAPITest {
 public:
  UserScriptsAPITestWithoutDeveloperMode() = default;
  UserScriptsAPITestWithoutDeveloperMode(
      const UserScriptsAPITestWithoutDeveloperMode&) = delete;
  UserScriptsAPITestWithoutDeveloperMode& operator=(
      const UserScriptsAPITestWithoutDeveloperMode&) = delete;
  ~UserScriptsAPITestWithoutDeveloperMode() override = default;
};

// TODO(crbug.com/390138269): Remove this test once the per-extension toggle is
// enabled by default since the API will no longer be controlled by dev mode.
// Verifies that the `chrome.userScripts` API is unavailable if the user doesn't
// have dev mode turned on.
IN_PROC_BROWSER_TEST_P(UserScriptsAPITestWithoutDeveloperMode,
                       UserScriptsAPIIsUnavailableWithoutDeveloperMode) {
  static constexpr char kManifest[] =
      R"({
           "name": "user scripts",
           "manifest_version": 3,
           "version": "0.1",
           "background": {"service_worker": "background.js"},
           "permissions": ["userScripts"]
         })";
  static constexpr char kBackgroundJs[] =
      R"(chrome.test.runTests([
           function userScriptsIsUnavailable() {
             let caught = false;
             try {
               chrome.userScripts;
             } catch (e) {
               caught = true;
               const expectedError =
                   `Failed to read the 'userScripts' property from 'Object': ` +
                   `The 'userScripts' API is only available for ` +
                   `users in developer mode.`;
               chrome.test.assertEq(expectedError, e.message);
             }
             chrome.test.assertTrue(caught);
             chrome.test.succeed();
           },
         ]);)";

  TestExtensionDir test_dir;
  test_dir.WriteManifest(kManifest);
  test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundJs);

  ASSERT_TRUE(RunUserScriptsExtensionTestNotAllowed(test_dir.UnpackedPath()))
      << message_;
}

// This tests inherits from a test that uses GetParam() so the test must be
// parameterized, even if there's only one test case to run).
INSTANTIATE_TEST_SUITE_P(All,
                         UserScriptsAPITestWithoutDeveloperMode,
                         // extensions_features::kUserScriptUserExtensionToggle
                         testing::Values(false));

using UserScriptsAPITestWithoutUserAllowed =
    UserScriptsAPITestWithoutDeveloperMode;

// Verifies that the `chrome.userScripts` API is undefined if the API is not
// allowed yet.
IN_PROC_BROWSER_TEST_P(UserScriptsAPITestWithoutUserAllowed,
                       UserScriptsAPIIsUndefinedWithoutAPIAllowed) {
  static constexpr char kManifest[] =
      R"({
           "name": "user scripts",
           "manifest_version": 3,
           "version": "0.1",
           "background": {"service_worker": "background.js"},
           "permissions": ["userScripts"]
         })";
  static constexpr char kBackgroundJs[] =
      R"(chrome.test.runTests([
           function userScriptsIsUndefined() {
             chrome.test.assertTrue(chrome.userScripts === undefined);
             chrome.test.succeed();
           },
         ]);)";

  TestExtensionDir test_dir;
  test_dir.WriteManifest(kManifest);
  test_dir.WriteFile(FILE_PATH_LITERAL("background.js"), kBackgroundJs);
  ASSERT_TRUE(RunUserScriptsExtensionTestNotAllowed(test_dir.UnpackedPath()))
      << message_;
}

// This tests inherits from a test that uses GetParam() so the test must be
// parameterized, even if there's only one test case to run).
INSTANTIATE_TEST_SUITE_P(All,
                         UserScriptsAPITestWithoutUserAllowed,
                         // extensions_features::kUserScriptUserExtensionToggle
                         testing::Values(true));

class UserScriptsAPITestWithoutAPIAllowed : public UserScriptsAPITest {
 public:
  UserScriptsAPITestWithoutAPIAllowed() = default;

  // UserScriptsAPITest override.
  void SetUp() override {
    // Initialize the listener object here before calling SetUp. This avoids a
    // race condition where the extension loads (as part of browser startup) and
    // sends a message before a message listener in C++ has been initialized.
    background_started_listener_ =
        std::make_unique<ExtensionTestMessageListener>("started");

    UserScriptsAPITest::SetUp();
  }

  // Reset listener before the browser gets torn down.
  void TearDown() override {
    background_started_listener_.reset();
    UserScriptsAPITest::TearDown();
  }

 protected:
  std::unique_ptr<ExtensionTestMessageListener> background_started_listener_;
};

// Tests that registered user scripts are properly ignored when loading
// stored dynamic scripts if the API is not allowed.
IN_PROC_BROWSER_TEST_P(UserScriptsAPITestWithoutAPIAllowed,
                       PRE_UserScriptsDisabledOnStartupIfAPINotAllowed) {
  // Load an extension and register user scripts and a dynamic content script.
  const Extension* extension =
      LoadExtension(test_data_dir_.AppendASCII("user_scripts/allowed_tests"));
  ASSERT_TRUE(extension);
  ASSERT_TRUE(background_started_listener_->WaitUntilSatisfied());
  user_scripts_test_util::SetUserScriptsAPIAllowed(profile(), extension->id(),
                                                   /*allowed=*/true);

  EXPECT_EQ("success",
            BackgroundScriptExecutor::ExecuteScript(
                profile(), extension->id(), "registerUserScripts();",
                BackgroundScriptExecutor::ResultCapture::kSendScriptResult));
  EXPECT_EQ("success",
            BackgroundScriptExecutor::ExecuteScript(
                profile(), extension->id(), "registerContentScript();",
                BackgroundScriptExecutor::ResultCapture::kSendScriptResult));

  const GURL url =
      embedded_test_server()->GetURL("example.com", "/simple.html");

  // To start, all scripts should inject.
  content::RenderFrameHost* new_tab = OpenInNewTab(url);
  EXPECT_EQ(R"(["content-script","user-script-code","user-script-file"])",
            GetInjectedElements(new_tab));

  // Disallow userScript API, and then re-open the browser...
  user_scripts_test_util::SetUserScriptsAPIAllowed(profile(), extension->id(),
                                                   /*allowed=*/false);
}

IN_PROC_BROWSER_TEST_P(UserScriptsAPITestWithoutAPIAllowed,
                       UserScriptsDisabledOnStartupIfAPINotAllowed) {
  // Wait until the extension loads so we can get it's ID.
  ASSERT_TRUE(background_started_listener_->WaitUntilSatisfied());

  // Find the extension's ID so we can make some assertions.
  ExtensionId extension_id;
  for (const auto& extension :
       ExtensionRegistry::Get(profile())->enabled_extensions()) {
    if (extension->name() == "Test") {
      extension_id = extension->id();
    }
  }
  ASSERT_TRUE(!extension_id.empty());

  // userScripts should remain disallowed after browser restart.
  if (GetParam()) {
    EXPECT_FALSE(GetCurrentUserScriptAllowedState(
                     util::GetBrowserContextId(profile()), extension_id)
                     .value_or(false));
  } else {
    EXPECT_FALSE(GetCurrentDeveloperMode(util::GetBrowserContextId(profile())));
  }

  const GURL url =
      embedded_test_server()->GetURL("example.com", "/simple.html");

  // And, to start, only the content script should inject.
  content::RenderFrameHost* new_tab = OpenInNewTab(url);
  EXPECT_EQ(R"(["content-script"])", GetInjectedElements(new_tab));

  user_scripts_test_util::SetUserScriptsAPIAllowed(profile(), extension_id,
                                                   /*allowed=*/true);

  // All scripts should once again inject.
  new_tab = OpenInNewTab(url);
  EXPECT_EQ(R"(["content-script","user-script-code","user-script-file"])",
            GetInjectedElements(new_tab));
}

INSTANTIATE_TEST_SUITE_P(All,
                         UserScriptsAPITestWithoutAPIAllowed,
                         // extensions_features::kUserScriptUserExtensionToggle
                         testing::Bool());

// TODO(crbug.com/390138269): Write a test that confirms that enabling the API
// for an extension in one profile doesn't enable it for the same extension in
// another profile. Also write tests to confirm incognito split/span mode
// behavior.

class MigrateUserScriptsAPITest : public ExtensionApiTest {
 public:
  MigrateUserScriptsAPITest() {
    // Tests the migration capability by disabling the feature in only the
    // PRE_<test_name> setup methods so the extension can be installed without
    // the migration. Then the primary test method restarts the browser with the
    // migration enabled.
    scoped_feature_list_.InitWithFeatureState(
        extensions_features::kUserScriptUserExtensionToggle,
        /*enabled=*/!GetTestPreCount());
  }

  void SetUpOnMainThread() override {
    ExtensionApiTest::SetUpOnMainThread();
    host_resolver()->AddRule("*", "127.0.0.1");
    ASSERT_TRUE(StartEmbeddedTestServer());
  }

  bool ExtensionPrefEnabled(const ExtensionId& extension_id,
                            const PrefMap& pref) {
    bool user_scripts_allowed = false;
    ExtensionPrefs::Get(profile())->ReadPrefAsBoolean(extension_id, pref,
                                                      &user_scripts_allowed);
    return user_scripts_allowed;
  }

  bool PrefEnabled(const PrefMap& pref) {
    return ExtensionPrefs::Get(profile())->GetPrefAsBoolean(pref);
  }

  void WaitForAllExtensionsToLoad() {
    // Wait for all extensions to load.
    SCOPED_TRACE("waiting for all extensions to load");
    ExtensionSystem* extension_system = ExtensionSystem::Get(profile());
    ASSERT_TRUE(extension_system);
    base::RunLoop run_loop;
    extension_system->ready().Post(FROM_HERE, run_loop.QuitClosure());
    run_loop.Run();
  }

  const ExtensionId GetTestExtensionId(const char* extension_name) {
    ExtensionId extension_id;
    const auto extensions =
        ExtensionRegistry::Get(profile())->GenerateInstalledExtensionsSet();
    for (const auto& extension : extensions) {
      if (extension->name() == extension_name) {
        extension_id = extension->id();
        break;
      }
    }
    return extension_id;
  }

  void SetDevMode(bool enabled) {
    util::SetDeveloperModeForProfile(profile(),
                                     /*in_developer_mode=*/enabled);

    // Wait for the above IPC(s) to send.
    RendererStartupHelper* renderer_startup_helper =
        RendererStartupHelperFactory::GetForBrowserContext(profile());
    renderer_startup_helper->FlushAllForTesting();
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

// Installs an extension without the user script permission prior to the
// migration.
IN_PROC_BROWSER_TEST_F(MigrateUserScriptsAPITest,
                       PRE_ExtensionWithoutPermission_Allowed_AfterMigration) {
  const Extension* extension = LoadExtension(test_data_dir_.AppendASCII(
      "user_scripts/migration_tests/extension_without_userscript_permission"));
  ASSERT_TRUE(extension);

  // Confirm no migration has occurred.
  ASSERT_FALSE(PrefEnabled(UserScriptManager::kUserScriptsToggleMigratedPref));
}

// Tests that extensions that do not have permission to use the user scripts API
// have the allowed preference set to false after migration.
IN_PROC_BROWSER_TEST_F(MigrateUserScriptsAPITest,
                       ExtensionWithoutPermission_Allowed_AfterMigration) {
  WaitForAllExtensionsToLoad();

  // Find the extension's ID so we can make some assertions.
  ExtensionId extension_id =
      GetTestExtensionId("extension_without_userscript_permission");
  ASSERT_FALSE(extension_id.empty());

  // Confirm migration occurred and extension disallowed.
  EXPECT_TRUE(PrefEnabled(UserScriptManager::kUserScriptsToggleMigratedPref));
  EXPECT_FALSE(ExtensionPrefEnabled(
      extension_id, UserScriptManager::kUserScriptsAllowedPref));
}

// Installs two extensions (one enabled and one disabled) and disables dev mode
// prior to the migration.
IN_PROC_BROWSER_TEST_F(MigrateUserScriptsAPITest,
                       PRE_DevModeOff_Disallowed_AfterMigration) {
  const Extension* enabled_extension = LoadExtension(test_data_dir_.AppendASCII(
      "user_scripts/migration_tests/extension_with_userscript_permission"));
  ASSERT_TRUE(enabled_extension);
  const Extension* disabled_extension =
      LoadExtension(test_data_dir_.AppendASCII("user_scripts/allowed_tests"));
  ASSERT_TRUE(disabled_extension);
  // Confirm no migration has occurred.
  ASSERT_FALSE(PrefEnabled(UserScriptManager::kUserScriptsToggleMigratedPref));

  // Disable dev mode so it is off during the migration.
  SetDevMode(/*enabled=*/false);

  DisableExtension(disabled_extension->id());
}

// Tests that user script API extensions where dev mode is off have the allowed
// preference set to false after restart (regardless if the extension is enabled
// or disabled).
IN_PROC_BROWSER_TEST_F(MigrateUserScriptsAPITest,
                       DevModeOff_Disallowed_AfterMigration) {
  WaitForAllExtensionsToLoad();

  // Find the extension's ID so we can make some assertions.
  ExtensionId enabled_extension_id =
      GetTestExtensionId("extension_with_userscript_permission");
  ASSERT_FALSE(enabled_extension_id.empty());
  ExtensionId disabled_extension_id = GetTestExtensionId("Test");
  ASSERT_FALSE(disabled_extension_id.empty());

  // Confirm extension is migrated and disallowed.
  EXPECT_TRUE(PrefEnabled(UserScriptManager::kUserScriptsToggleMigratedPref));
  EXPECT_FALSE(ExtensionPrefEnabled(
      enabled_extension_id, UserScriptManager::kUserScriptsAllowedPref));
  EXPECT_FALSE(ExtensionPrefEnabled(
      disabled_extension_id, UserScriptManager::kUserScriptsAllowedPref));
}

// Installs two extensions (one enabled and one disabled) and enables dev mode
// prior to the migration.
IN_PROC_BROWSER_TEST_F(MigrateUserScriptsAPITest,
                       PRE_DevModeOn_Allowed_AfterMigration) {
  const Extension* enabled_extension = LoadExtension(test_data_dir_.AppendASCII(
      "user_scripts/migration_tests/extension_with_userscript_permission"));
  ASSERT_TRUE(enabled_extension);
  const Extension* disabled_extension =
      LoadExtension(test_data_dir_.AppendASCII("user_scripts/allowed_tests"));
  ASSERT_TRUE(disabled_extension);

  // Confirm no migration has occurred.
  ASSERT_FALSE(PrefEnabled(UserScriptManager::kUserScriptsToggleMigratedPref));

  // Enable dev mode so it is on during the migration.
  SetDevMode(/*enabled=*/true);

  DisableExtension(disabled_extension->id());
}

// Tests that user script API extensions where dev mode is on have the allowed
// preference set to true after restart (regardless if the extension is enabled
// or disabled).
IN_PROC_BROWSER_TEST_F(MigrateUserScriptsAPITest,
                       DevModeOn_Allowed_AfterMigration) {
  WaitForAllExtensionsToLoad();

  // Find the extension's ID so we can make some assertions.
  ExtensionId enabled_extension_id =
      GetTestExtensionId("extension_with_userscript_permission");
  ASSERT_FALSE(enabled_extension_id.empty());
  ExtensionId disabled_extension_id = GetTestExtensionId("Test");
  ASSERT_FALSE(disabled_extension_id.empty());

  // Confirm extension is migrated and disallowed.
  EXPECT_TRUE(PrefEnabled(UserScriptManager::kUserScriptsToggleMigratedPref));
  EXPECT_TRUE(ExtensionPrefEnabled(enabled_extension_id,
                                   UserScriptManager::kUserScriptsAllowedPref));
  EXPECT_TRUE(ExtensionPrefEnabled(disabled_extension_id,
                                   UserScriptManager::kUserScriptsAllowedPref));
}

}  // namespace extensions