File: tabs_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 (562 lines) | stat: -rw-r--r-- 21,666 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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/strings/stringprintf.h"
#include "base/strings/to_string.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/api/tabs/tabs_api.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/prefs/incognito_mode_prefs.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "components/prefs/pref_service.h"
#include "content/public/common/content_features.h"
#include "content/public/test/back_forward_cache_util.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/prerender_test_util.h"
#include "extensions/test/result_catcher.h"
#include "extensions/test/test_extension_dir.h"
#include "net/dns/mock_host_resolver.h"

#if BUILDFLAG(IS_WIN)
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#endif

using ContextType = extensions::browser_test_util::ContextType;

class ExtensionApiTabTest : public extensions::ExtensionApiTest {
 public:
  explicit ExtensionApiTabTest(ContextType context_type = ContextType::kNone)
      : ExtensionApiTest(context_type) {}
  ~ExtensionApiTabTest() override = default;
  ExtensionApiTabTest(const ExtensionApiTabTest&) = delete;
  ExtensionApiTabTest& operator=(const ExtensionApiTabTest&) = delete;

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

class ExtensionApiTabTestWithContextType
    : public ExtensionApiTabTest,
      public testing::WithParamInterface<ContextType> {
 public:
  ExtensionApiTabTestWithContextType() : ExtensionApiTabTest(GetParam()) {}
  ExtensionApiTabTestWithContextType(
      const ExtensionApiTabTestWithContextType&) = delete;
  ExtensionApiTabTestWithContextType& operator=(
      const ExtensionApiTabTestWithContextType&) = delete;
  ~ExtensionApiTabTestWithContextType() override = default;
};

INSTANTIATE_TEST_SUITE_P(PersistentBackground,
                         ExtensionApiTabTestWithContextType,
                         ::testing::Values(ContextType::kPersistentBackground));

INSTANTIATE_TEST_SUITE_P(ServiceWorker,
                         ExtensionApiTabTestWithContextType,
                         ::testing::Values(ContextType::kServiceWorker));

class ExtensionApiTabBackForwardCacheTest
    : public ExtensionApiTabTestWithContextType {
 public:
  ExtensionApiTabBackForwardCacheTest() {
    feature_list_.InitWithFeaturesAndParameters(
        content::GetBasicBackForwardCacheFeatureForTesting(
            {{features::kBackForwardCache, {}}}),
        content::GetDefaultDisabledBackForwardCacheFeaturesForTesting());
  }
  ~ExtensionApiTabBackForwardCacheTest() override = default;

 private:
  base::test::ScopedFeatureList feature_list_;
};

INSTANTIATE_TEST_SUITE_P(PersistentBackground,
                         ExtensionApiTabBackForwardCacheTest,
                         ::testing::Values(ContextType::kPersistentBackground));

INSTANTIATE_TEST_SUITE_P(ServiceWorker,
                         ExtensionApiTabBackForwardCacheTest,
                         ::testing::Values(ContextType::kServiceWorker));

class ExtensionApiNewTabTest : public ExtensionApiTabTestWithContextType {
 public:
  ExtensionApiNewTabTest() = default;
  void SetUpCommandLine(base::CommandLine* command_line) override {
    ExtensionApiTabTest::SetUpCommandLine(command_line);
    // Override the default which InProcessBrowserTest adds if it doesn't see a
    // homepage.
    command_line->AppendSwitchASCII(
        switches::kHomePage, chrome::kChromeUINewTabURL);
  }
};

INSTANTIATE_TEST_SUITE_P(PersistentBackground,
                         ExtensionApiNewTabTest,
                         ::testing::Values(ContextType::kPersistentBackground));

INSTANTIATE_TEST_SUITE_P(ServiceWorker,
                         ExtensionApiNewTabTest,
                         ::testing::Values(ContextType::kServiceWorker));

IN_PROC_BROWSER_TEST_P(ExtensionApiNewTabTest, Tabs) {
  // The test creates a tab and checks that the URL of the new tab
  // is that of the new tab page.  Make sure the pref that controls
  // this is set.
  browser()->profile()->GetPrefs()->SetBoolean(
      prefs::kHomePageIsNewTabPage, true);

  ASSERT_TRUE(RunExtensionTest("tabs/basics/crud")) << message_;
}

IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, TabAudible) {
  ASSERT_TRUE(
      RunExtensionTest("tabs/basics", {.extension_url = "audible.html"}))
      << message_;
}

// TODO(crbug.com/40925613): Re-enable this test
#if BUILDFLAG(IS_MAC)
#define MAYBE_Muted DISABLED_Muted
#else
#define MAYBE_Muted Muted
#endif
IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, MAYBE_Muted) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics/muted")) << message_;
}

IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, Tabs2) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics", {.extension_url = "crud2.html"}))
      << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Duplicate) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics/duplicate")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Size) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics/tab_size")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Update) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics/update")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Pinned) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics/pinned")) << message_;
}

// Flakes reported on Linux debug and Mac, see crbug.com/40936001.
#if (BUILDFLAG(IS_LINUX) && !defined(NDEBUG)) || BUILDFLAG(IS_MAC)
#define MAYBE_Move DISABLED_Move
#else
#define MAYBE_Move Move
#endif
IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, MAYBE_Move) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics/move")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Events) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics/events")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, RelativeURLs) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics/relative_urls")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Query) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics/query")) << message_;
}

// TODO(crbug.com/40254426): Move to tabs_interactive_test.cc
// TODO(crbug.com/40890826): Re-enable once flakiness is fixed.
IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, DISABLED_Highlight) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics/highlight")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, LastAccessed) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics/last_accessed")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, CrashBrowser) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics/crash")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Opener) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics/opener")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Remove) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics/remove")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, RemoveMultiple) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics/remove_multiple")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, GetCurrent) {
  ASSERT_TRUE(RunExtensionTest("tabs/get_current")) << message_;
}

// Disabled for being flaky. See crbug.com/1472144
IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, DISABLED_Connect) {
  ASSERT_TRUE(RunExtensionTest("tabs/connect")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, OnRemoved) {
  ASSERT_TRUE(RunExtensionTest("tabs/on_removed")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Reload) {
  ASSERT_TRUE(RunExtensionTest("tabs/reload")) << message_;
}

class ExtensionApiCaptureTest
    : public ExtensionApiTabTest,
      public testing::WithParamInterface<ContextType> {
 public:
  ExtensionApiCaptureTest() : ExtensionApiTabTest(GetParam()) {}
  ~ExtensionApiCaptureTest() override = default;
  ExtensionApiCaptureTest(const ExtensionApiCaptureTest&) = delete;
  ExtensionApiCaptureTest& operator=(const ExtensionApiCaptureTest&) = delete;

  void SetUp() override {
    extensions::TabsCaptureVisibleTabFunction::set_disable_throttling_for_tests(
        true);
    EnablePixelOutput();
    ExtensionApiTabTest::SetUp();
  }
};

INSTANTIATE_TEST_SUITE_P(PersistentBackground,
                         ExtensionApiCaptureTest,
                         ::testing::Values(ContextType::kPersistentBackground));
INSTANTIATE_TEST_SUITE_P(ServiceWorker,
                         ExtensionApiCaptureTest,
                         ::testing::Values(ContextType::kServiceWorker));

// https://crbug.com/1450747 Flaky on Mac.
// TODO(crbug.com/381214152): Re-enable this test
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
#define MAYBE_CaptureVisibleTabJpeg DISABLED_CaptureVisibleTabJpeg
#else
#define MAYBE_CaptureVisibleTabJpeg CaptureVisibleTabJpeg
#endif
IN_PROC_BROWSER_TEST_P(ExtensionApiCaptureTest, MAYBE_CaptureVisibleTabJpeg) {
  ASSERT_TRUE(RunExtensionTest("tabs/capture_visible_tab/test_jpeg"))
      << message_;
}

// https://crbug.com/1450933 Flaky on Mac.
// TODO(crbug.com/381277829): Flaky on ASAN and MSAN builds.
#if BUILDFLAG(IS_MAC) || defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER)
#define MAYBE_CaptureVisibleTabPng DISABLED_CaptureVisibleTabPng
#else
#define MAYBE_CaptureVisibleTabPng CaptureVisibleTabPng
#endif
IN_PROC_BROWSER_TEST_P(ExtensionApiCaptureTest, MAYBE_CaptureVisibleTabPng) {
  ASSERT_TRUE(RunExtensionTest("tabs/capture_visible_tab/test_png"))
      << message_;
}

// TODO(crbug.com/40168659) Re-enable test
IN_PROC_BROWSER_TEST_P(ExtensionApiCaptureTest,
                       DISABLED_CaptureVisibleTabRace) {
  ASSERT_TRUE(RunExtensionTest("tabs/capture_visible_tab/test_race"))
      << message_;
}

// https://crbug.com/1107934 Flaky on Windows, Linux, ChromeOS.
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_CHROMEOS)
#define MAYBE_CaptureVisibleFile DISABLED_CaptureVisibleFile
#else
#define MAYBE_CaptureVisibleFile CaptureVisibleFile
#endif
IN_PROC_BROWSER_TEST_P(ExtensionApiCaptureTest, MAYBE_CaptureVisibleFile) {
  ASSERT_TRUE(RunExtensionTest("tabs/capture_visible_tab/test_file", {},
                               {.allow_file_access = true}))
      << message_;
}

// TODO(crbug.com/40803947): Fix flakiness on Linux then reenable.
#if BUILDFLAG(IS_LINUX)
#define MAYBE_CaptureVisibleDisabled DISABLED_CaptureVisibleDisabled
#else
#define MAYBE_CaptureVisibleDisabled CaptureVisibleDisabled
#endif
IN_PROC_BROWSER_TEST_P(ExtensionApiCaptureTest, MAYBE_CaptureVisibleDisabled) {
  browser()->profile()->GetPrefs()->SetBoolean(prefs::kDisableScreenshots,
                                               true);
  ASSERT_TRUE(RunExtensionTest("tabs/capture_visible_tab/test_disabled"))
      << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiCaptureTest, CaptureNullWindow) {
  ASSERT_TRUE(RunExtensionTest("tabs/capture_visible_tab_null_window"))
      << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, OnCreated) {
  ASSERT_TRUE(RunExtensionTest("tabs/on_created")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType,
                       LazyBackgroundTabsOnCreated) {
  ASSERT_TRUE(RunExtensionTest("tabs/lazy_background_on_created")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, OnUpdated) {
  ASSERT_TRUE(RunExtensionTest("tabs/on_updated")) << message_;
}

// TODO(crbug.com/378027647) Failing on ChromeOS and Linux
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
#define MAYBE_OnUpdated DISABLED_OnUpdated
#else
#define MAYBE_OnUpdated OnUpdated
#endif
IN_PROC_BROWSER_TEST_P(ExtensionApiTabBackForwardCacheTest, MAYBE_OnUpdated) {
  ASSERT_TRUE(RunExtensionTest("tabs/backForwardCache/on_updated")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, NoPermissions) {
  ASSERT_TRUE(RunExtensionTest("tabs/no_permissions")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType,
                       DISABLED_HostPermission) {
  ASSERT_TRUE(RunExtensionTest("tabs/host_permission")) << message_;
}

// Flaky on Windows, Mac and Linux. http://crbug.com/820110.
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX) || \
    BUILDFLAG(IS_CHROMEOS)
#define MAYBE_UpdateWindowResize DISABLED_UpdateWindowResize
#else
#define MAYBE_UpdateWindowResize UpdateWindowResize
#endif
IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, MAYBE_UpdateWindowResize) {
  ASSERT_TRUE(RunExtensionTest("window_update/resize")) << message_;
}

#if BUILDFLAG(IS_WIN)
IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, FocusWindowDoesNotUnmaximize) {
  HWND window =
      browser()->window()->GetNativeWindow()->GetHost()->GetAcceleratedWidget();
  ::SendMessage(window, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
  ASSERT_TRUE(RunExtensionTest("window_update/focus")) << message_;
  ASSERT_TRUE(::IsZoomed(window));
}
#endif  // BUILDFLAG(IS_WIN)

#if defined(USE_AURA) || BUILDFLAG(IS_MAC)
// Maximizing/fullscreen popup window doesn't work on aura's managed mode.
// See bug crbug.com/116305.
// Mac: http://crbug.com/103912
#define MAYBE_UpdateWindowShowState DISABLED_UpdateWindowShowState
#else
#define MAYBE_UpdateWindowShowState UpdateWindowShowState
#endif  // defined(USE_AURA) || BUILDFLAG(IS_MAC)
IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, MAYBE_UpdateWindowShowState) {
  ASSERT_TRUE(RunExtensionTest("window_update/show_state")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType,
                       IncognitoDisabledByPref) {
  IncognitoModePrefs::SetAvailability(
      browser()->profile()->GetPrefs(),
      policy::IncognitoModeAvailability::kDisabled);

  // This makes sure that creating an incognito window fails due to pref
  // (policy) being set.
  ASSERT_TRUE(RunExtensionTest("tabs/incognito_disabled")) << message_;
}

IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, GetViewsOfCreatedPopup) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics",
                               {.extension_url = "get_views_popup.html"}))
      << message_;
}

IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, GetViewsOfCreatedWindow) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics",
                               {.extension_url = "get_views_window.html"}))
      << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType,
                       OnUpdatedDiscardedState) {
  ASSERT_TRUE(RunExtensionTest("tabs/basics/discarded")) << message_;
}

IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, OpenerCraziness) {
  ASSERT_TRUE(RunExtensionTest("tabs/tab_opener_id")) << message_;
}

// Tests sending messages from an extension's service worker using
// chrome.tabs.sendMessage to a webpage in the extension listening for them
// using chrome.runtime.OnMessage.
IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, SendMessage) {
  ASSERT_TRUE(RunExtensionTest("tabs/send_message"));
}

// Tests that extension with "tabs" permission does not leak tab info to another
// extension without "tabs" permission.
//
// Regression test for https://crbug.com/1302959
IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType,
                       TabsPermissionDoesNotLeakTabInfo) {
  constexpr char kManifestWithTabsPermission[] =
      R"({
        "name": "test", "version": "1", "manifest_version": 2,
        "background": {"scripts": ["background.js"], "persistent": true},
        "permissions": ["tabs"]
      })";
  constexpr char kBackgroundJSWithTabsPermission[] =
      "chrome.tabs.onUpdated.addListener(() => {});";

  constexpr char kManifestWithoutTabsPermission[] =
      R"({
        "name": "test", "version": "1", "manifest_version": 2,
        "background": {"scripts": ["background.js"], "persistent": true}
      })";
  constexpr char kBackgroundJSWithoutTabsPermission[] =
      R"(
        let urlStr = '%s';
        chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
          chrome.test.assertEq(3, Array.from(arguments).length);
          // Note: we'll search within all of the arguments, just to make sure
          // we don't miss any inadvertently added ones. See
          // https://crbug.com/1302959 for details.
          let argumentsStr = JSON.stringify(arguments);
          let containsUrlStr = argumentsStr.indexOf(urlStr) != -1;
          chrome.test.assertFalse(containsUrlStr);
          if (tab.status == 'complete') {
            chrome.test.notifyPass();
          }
        });
      )";

  GURL url = embedded_test_server()->GetURL("/title1.html");

  // First load the extension with "tabs" permission.
  // Note that order is important for this regression test.
  extensions::TestExtensionDir ext_dir1;
  ext_dir1.WriteManifest(kManifestWithTabsPermission);
  ext_dir1.WriteFile(FILE_PATH_LITERAL("background.js"),
                     kBackgroundJSWithTabsPermission);
  ASSERT_TRUE(LoadExtension(ext_dir1.UnpackedPath()));

  // Then load the extension without "tabs" permission.
  extensions::ResultCatcher catcher;
  extensions::TestExtensionDir ext_dir2;
  ext_dir2.WriteManifest(kManifestWithoutTabsPermission);
  ext_dir2.WriteFile(FILE_PATH_LITERAL("background.js"),
                     base::StringPrintf(kBackgroundJSWithoutTabsPermission,
                                        url.spec().c_str()));
  ASSERT_TRUE(LoadExtension(ext_dir2.UnpackedPath()));

  // Now open a tab and ensure the extension in |ext_dir2| does not see any info
  // that is guarded by "tabs" permission.
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
  EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
}

struct IncognitoTestParam {
  IncognitoTestParam(bool is_incognito_enabled, ContextType context_type)
      : is_incognito_enabled(is_incognito_enabled),
        context_type(context_type) {}

  bool is_incognito_enabled;
  ContextType context_type;
};

class IncognitoExtensionApiTabTest
    : public ExtensionApiTabTest,
      public testing::WithParamInterface<IncognitoTestParam> {
 public:
  IncognitoExtensionApiTabTest()
      : ExtensionApiTabTest(GetParam().context_type) {}
  IncognitoExtensionApiTabTest(const IncognitoExtensionApiTabTest&) = delete;
  IncognitoExtensionApiTabTest& operator=(const IncognitoExtensionApiTabTest&) =
      delete;
  ~IncognitoExtensionApiTabTest() override = default;
};

IN_PROC_BROWSER_TEST_P(IncognitoExtensionApiTabTest, Tabs) {
  bool is_incognito_enabled = GetParam().is_incognito_enabled;
  Browser* incognito_browser =
      OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
  std::string args = base::StringPrintf(
      R"({"isIncognito": %s, "windowId": %d})",
      base::ToString(is_incognito_enabled),
      extensions::ExtensionTabUtil::GetWindowId(incognito_browser));

  EXPECT_TRUE(RunExtensionTest("tabs/basics/incognito",
                               {.custom_arg = args.c_str()},
                               {.allow_in_incognito = is_incognito_enabled}))
      << message_;
}

INSTANTIATE_TEST_SUITE_P(
    PB_IncognitoEnabled,
    IncognitoExtensionApiTabTest,
    testing::Values(IncognitoTestParam(true,
                                       ContextType::kPersistentBackground)));
INSTANTIATE_TEST_SUITE_P(
    PB_IncognitoDisabled,
    IncognitoExtensionApiTabTest,
    testing::Values(IncognitoTestParam(false,
                                       ContextType::kPersistentBackground)));
INSTANTIATE_TEST_SUITE_P(
    SW_IncognitoEnabled,
    IncognitoExtensionApiTabTest,
    testing::Values(IncognitoTestParam(true, ContextType::kServiceWorker)));
INSTANTIATE_TEST_SUITE_P(
    SW_IncognitoDisabled,
    IncognitoExtensionApiTabTest,
    testing::Values(IncognitoTestParam(false, ContextType::kServiceWorker)));

class ExtensionApiTabPrerenderingTest : public ExtensionApiTabTest {
 public:
  ExtensionApiTabPrerenderingTest()
      : prerender_helper_(base::BindRepeating(
            &ExtensionApiTabPrerenderingTest::GetWebContents,
            base::Unretained(this))) {}
  ~ExtensionApiTabPrerenderingTest() override = default;

  content::WebContents* GetWebContents() {
    return browser()->tab_strip_model()->GetWebContentsAt(0);
  }

 private:
  content::test::PrerenderTestHelper prerender_helper_;
};

// TODO(crbug.com/40235049): Flaky on multiple platforms.
IN_PROC_BROWSER_TEST_F(ExtensionApiTabPrerenderingTest, DISABLED_Prerendering) {
  ASSERT_TRUE(RunExtensionTest("tabs/prerendering")) << message_;
}

IN_PROC_BROWSER_TEST_F(ExtensionApiTabPrerenderingTest,
                       PrerenderingIntoANewTab) {
  ASSERT_TRUE(RunExtensionTest("tabs/prerendering_into_new_tab")) << message_;
}

// Adding a new test? Awesome. But API tests are the old hotness. The new
// hotness is api_test_utils. See tabs_test.cc for an example.
// We are trying to phase out many uses of API tests as they tend to be flaky.