summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_url_overrides_newtab.js
blob: 7d3342044b6e2485d377c4e1b28ecca5c93c186b (plain)
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
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */

"use strict";

requestLongerTimeout(4);

ChromeUtils.defineESModuleGetters(this, {
  ExtensionControlledPopup:
    "resource:///modules/ExtensionControlledPopup.sys.mjs",
  ExtensionSettingsStore:
    "resource://gre/modules/ExtensionSettingsStore.sys.mjs",
});
ChromeUtils.defineModuleGetter(
  this,
  "AboutNewTab",
  "resource:///modules/AboutNewTab.jsm"
);

function getNotificationSetting(extensionId) {
  return ExtensionSettingsStore.getSetting("newTabNotification", extensionId);
}

function getNewTabDoorhanger() {
  ExtensionControlledPopup._getAndMaybeCreatePanel(document);
  return document.getElementById("extension-new-tab-notification");
}

function clickKeepChanges(notification) {
  notification.button.click();
}

function clickManage(notification) {
  notification.secondaryButton.click();
}

async function promiseNewTab(expectUrl = AboutNewTab.newTabURL, win = window) {
  let eventName = "browser-open-newtab-start";
  let newTabStartPromise = new Promise(resolve => {
    async function observer(subject) {
      Services.obs.removeObserver(observer, eventName);
      resolve(subject.wrappedJSObject);
    }
    Services.obs.addObserver(observer, eventName);
  });

  let newtabShown = TestUtils.waitForCondition(
    () => win.gBrowser.currentURI.spec == expectUrl,
    `Should open correct new tab url ${expectUrl}.`
  );

  win.BrowserOpenTab();
  const newTabCreatedPromise = newTabStartPromise;
  const browser = await newTabCreatedPromise;
  await newtabShown;
  const tab = win.gBrowser.selectedTab;

  Assert.deepEqual(
    browser,
    tab.linkedBrowser,
    "browser-open-newtab-start notified with the created browser"
  );
  return tab;
}

function waitForAddonDisabled(addon) {
  return new Promise(resolve => {
    let listener = {
      onDisabled(disabledAddon) {
        if (disabledAddon.id == addon.id) {
          resolve();
          AddonManager.removeAddonListener(listener);
        }
      },
    };
    AddonManager.addAddonListener(listener);
  });
}

function waitForAddonEnabled(addon) {
  return new Promise(resolve => {
    let listener = {
      onEnabled(enabledAddon) {
        if (enabledAddon.id == addon.id) {
          AddonManager.removeAddonListener(listener);
          resolve();
        }
      },
    };
    AddonManager.addAddonListener(listener);
  });
}

// Default test extension data for newtab.
const extensionData = {
  manifest: {
    browser_specific_settings: {
      gecko: {
        id: "newtaburl@mochi.test",
      },
    },
    chrome_url_overrides: {
      newtab: "newtab.html",
    },
  },
  files: {
    "newtab.html": "<h1>New tab!</h1>",
  },
  useAddonManager: "temporary",
};

add_task(async function test_new_tab_opens() {
  let panel = getNewTabDoorhanger().closest("panel");
  let extension = ExtensionTestUtils.loadExtension(extensionData);

  await extension.startup();
  let extensionNewTabUrl = `moz-extension://${extension.uuid}/newtab.html`;

  // Simulate opening the newtab open as a user would.
  let popupShown = promisePopupShown(panel);
  let tab = await promiseNewTab(extensionNewTabUrl);
  await popupShown;

  // This will show a confirmation doorhanger, make sure we don't leave it open.
  let popupHidden = promisePopupHidden(panel);
  panel.hidePopup();
  await popupHidden;

  BrowserTestUtils.removeTab(tab);
  await extension.unload();
});

add_task(async function test_new_tab_ignore_settings() {
  await ExtensionSettingsStore.initialize();
  let notification = getNewTabDoorhanger();
  let panel = notification.closest("panel");
  let extensionId = "newtabignore@mochi.test";
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      browser_specific_settings: { gecko: { id: extensionId } },
      browser_action: {
        default_popup: "ignore.html",
        default_area: "navbar",
      },
      chrome_url_overrides: { newtab: "ignore.html" },
    },
    files: { "ignore.html": '<h1 id="extension-new-tab">New Tab!</h1>' },
    useAddonManager: "temporary",
  });

  ok(
    panel.getAttribute("panelopen") != "true",
    "The notification panel is initially closed"
  );

  await extension.startup();

  // Simulate opening the New Tab as a user would.
  let popupShown = promisePopupShown(panel);
  let tab = await promiseNewTab();
  await popupShown;

  // Ensure the doorhanger is shown and the setting isn't set yet.
  is(
    panel.getAttribute("panelopen"),
    "true",
    "The notification panel is open after opening New Tab"
  );
  is(gURLBar.focused, false, "The URL bar is not focused with a doorhanger");
  is(
    getNotificationSetting(extensionId),
    null,
    "The New Tab notification is not set for this extension"
  );
  is(
    panel.anchorNode.closest("toolbarbutton").id,
    "newtabignore_mochi_test-BAP",
    "The doorhanger is anchored to the browser action"
  );

  // Manually close the panel, as if the user ignored it.
  let popupHidden = promisePopupHidden(panel);
  panel.hidePopup();
  await popupHidden;

  // Ensure panel is closed and the setting still isn't set.
  ok(
    panel.getAttribute("panelopen") != "true",
    "The notification panel is closed"
  );
  is(
    getNotificationSetting(extensionId),
    null,
    "The New Tab notification is not set after ignoring the doorhanger"
  );

  // Close the first tab and open another new tab.
  BrowserTestUtils.removeTab(tab);
  tab = await promiseNewTab();

  // Verify the doorhanger is not shown a second time.
  ok(
    panel.getAttribute("panelopen") != "true",
    "The notification panel doesn't open after ignoring the doorhanger"
  );
  is(gURLBar.focused, true, "The URL bar is focused with no doorhanger");

  BrowserTestUtils.removeTab(tab);
  await extension.unload();
});

add_task(async function test_new_tab_keep_settings() {
  await ExtensionSettingsStore.initialize();
  let notification = getNewTabDoorhanger();
  let panel = notification.closest("panel");
  let extensionId = "newtabkeep@mochi.test";
  let manifest = {
    version: "1.0",
    name: "New Tab Add-on",
    browser_specific_settings: { gecko: { id: extensionId } },
    chrome_url_overrides: { newtab: "newtab.html" },
  };
  let extension = ExtensionTestUtils.loadExtension({
    ...extensionData,
    manifest,
    useAddonManager: "permanent",
  });

  ok(
    panel.getAttribute("panelopen") != "true",
    "The notification panel is initially closed"
  );

  await extension.startup();
  let extensionNewTabUrl = `moz-extension://${extension.uuid}/newtab.html`;

  // Simulate opening the New Tab as a user would.
  let popupShown = promisePopupShown(panel);
  let tab = await promiseNewTab(extensionNewTabUrl);
  await popupShown;

  // Ensure the panel is open and the setting isn't saved yet.
  is(
    panel.getAttribute("panelopen"),
    "true",
    "The notification panel is open after opening New Tab"
  );
  is(
    getNotificationSetting(extensionId),
    null,
    "The New Tab notification is not set for this extension"
  );
  is(
    panel.anchorNode.closest("toolbarbutton").id,
    "PanelUI-menu-button",
    "The doorhanger is anchored to the menu icon"
  );
  is(
    panel.querySelector("#extension-new-tab-notification-description")
      .textContent,
    "An extension,  New Tab Add-on, changed the page you see when you open a new tab.Learn more",
    "The description includes the add-on name"
  );

  // Click the Keep Changes button.
  let confirmationSaved = TestUtils.waitForCondition(() => {
    return ExtensionSettingsStore.getSetting(
      "newTabNotification",
      extensionId,
      extensionId
    ).value;
  });
  clickKeepChanges(notification);
  await confirmationSaved;

  // Ensure panel is closed and setting is updated.
  ok(
    panel.getAttribute("panelopen") != "true",
    "The notification panel is closed after click"
  );
  is(
    getNotificationSetting(extensionId).value,
    true,
    "The New Tab notification is set after keeping the changes"
  );

  // Close the first tab and open another new tab.
  BrowserTestUtils.removeTab(tab);
  tab = await promiseNewTab(extensionNewTabUrl);

  // Verify the doorhanger is not shown a second time.
  ok(
    panel.getAttribute("panelopen") != "true",
    "The notification panel is not opened after keeping the changes"
  );

  BrowserTestUtils.removeTab(tab);

  let upgradedExtension = ExtensionTestUtils.loadExtension({
    ...extensionData,
    manifest: Object.assign({}, manifest, { version: "2.0" }),
    useAddonManager: "permanent",
  });

  await upgradedExtension.startup();
  extensionNewTabUrl = `moz-extension://${upgradedExtension.uuid}/newtab.html`;

  tab = await promiseNewTab(extensionNewTabUrl);

  // Ensure panel is closed and setting is still set.
  ok(
    panel.getAttribute("panelopen") != "true",
    "The notification panel is closed after click"
  );
  is(
    getNotificationSetting(extensionId).value,
    true,
    "The New Tab notification is set after keeping the changes"
  );

  BrowserTestUtils.removeTab(tab);
  await upgradedExtension.unload();
  await extension.unload();

  let confirmation = ExtensionSettingsStore.getSetting(
    "newTabNotification",
    extensionId,
    extensionId
  );
  is(confirmation, null, "The confirmation has been cleaned up");
});

add_task(async function test_new_tab_restore_settings() {
  await ExtensionSettingsStore.initialize();
  let notification = getNewTabDoorhanger();
  let panel = notification.closest("panel");
  let extensionId = "newtabrestore@mochi.test";
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      browser_specific_settings: { gecko: { id: extensionId } },
      chrome_url_overrides: { newtab: "restore.html" },
    },
    files: { "restore.html": '<h1 id="extension-new-tab">New Tab!</h1>' },
    useAddonManager: "temporary",
  });

  ok(
    panel.getAttribute("panelopen") != "true",
    "The notification panel is initially closed"
  );
  is(
    getNotificationSetting(extensionId),
    null,
    "The New Tab notification is not initially set for this extension"
  );

  await extension.startup();

  // Simulate opening the newtab open as a user would.
  let popupShown = promisePopupShown(panel);
  let tab = await promiseNewTab();
  await popupShown;

  // Verify that the panel is open and add-on is enabled.
  let addon = await AddonManager.getAddonByID(extensionId);
  is(addon.userDisabled, false, "The add-on is enabled at first");
  is(
    panel.getAttribute("panelopen"),
    "true",
    "The notification panel is open after opening New Tab"
  );
  is(
    getNotificationSetting(extensionId),
    null,
    "The New Tab notification is not set for this extension"
  );

  // Click the Manage button.
  let preferencesShown = TestUtils.waitForCondition(
    () => gBrowser.currentURI.spec == "about:preferences#home",
    "Should open about:preferences."
  );

  let popupHidden = promisePopupHidden(panel);
  clickManage(notification);
  await popupHidden;
  await preferencesShown;

  // Ensure panel is closed, settings haven't changed and add-on is disabled.
  ok(
    panel.getAttribute("panelopen") != "true",
    "The notification panel is closed after click"
  );

  is(
    getNotificationSetting(extensionId),
    null,
    "The New Tab notification is not set after clicking manage"
  );

  // Reopen a browser tab and verify that there's no doorhanger.
  BrowserTestUtils.removeTab(tab);
  tab = await promiseNewTab();

  ok(
    panel.getAttribute("panelopen") != "true",
    "The notification panel is not opened after keeping the changes"
  );

  BrowserTestUtils.removeTab(tab);
  await extension.unload();
});

add_task(async function test_new_tab_restore_settings_multiple() {
  await ExtensionSettingsStore.initialize();
  let notification = getNewTabDoorhanger();
  let panel = notification.closest("panel");
  let extensionOneId = "newtabrestoreone@mochi.test";
  let extensionOne = ExtensionTestUtils.loadExtension({
    manifest: {
      browser_specific_settings: { gecko: { id: extensionOneId } },
      chrome_url_overrides: { newtab: "restore-one.html" },
    },
    files: {
      "restore-one.html": `
        <h1 id="extension-new-tab">New Tab!</h1>
      `,
    },
    useAddonManager: "temporary",
  });
  let extensionTwoId = "newtabrestoretwo@mochi.test";
  let extensionTwo = ExtensionTestUtils.loadExtension({
    manifest: {
      browser_specific_settings: { gecko: { id: extensionTwoId } },
      chrome_url_overrides: { newtab: "restore-two.html" },
    },
    files: { "restore-two.html": '<h1 id="extension-new-tab">New Tab!</h1>' },
    useAddonManager: "temporary",
  });

  ok(
    panel.getAttribute("panelopen") != "true",
    "The notification panel is initially closed"
  );
  is(
    getNotificationSetting(extensionOneId),
    null,
    "The New Tab notification is not initially set for this extension"
  );
  is(
    getNotificationSetting(extensionTwoId),
    null,
    "The New Tab notification is not initially set for this extension"
  );

  await extensionOne.startup();
  await extensionTwo.startup();

  // Simulate opening the newtab open as a user would.
  let popupShown = promisePopupShown(panel);
  let tab1 = await promiseNewTab();
  await popupShown;

  // Verify that the panel is open and add-on is enabled.
  let addonTwo = await AddonManager.getAddonByID(extensionTwoId);
  is(addonTwo.userDisabled, false, "The add-on is enabled at first");
  is(
    panel.getAttribute("panelopen"),
    "true",
    "The notification panel is open after opening New Tab"
  );
  is(
    getNotificationSetting(extensionTwoId),
    null,
    "The New Tab notification is not set for this extension"
  );

  // Click the Manage button.
  let popupHidden = promisePopupHidden(panel);
  let preferencesShown = TestUtils.waitForCondition(
    () => gBrowser.currentURI.spec == "about:preferences#home",
    "Should open about:preferences."
  );
  clickManage(notification);
  await popupHidden;
  await preferencesShown;

  // Disable the second addon then refresh the new tab expect to see a new addon dropdown.
  let addonDisabled = waitForAddonDisabled(addonTwo);
  addonTwo.disable();
  await addonDisabled;

  // Ensure the panel opens again for the next add-on.
  popupShown = promisePopupShown(panel);
  let newtabShown = TestUtils.waitForCondition(
    () => gBrowser.currentURI.spec == AboutNewTab.newTabURL,
    "Should open correct new tab url."
  );
  let tab2 = await promiseNewTab();
  await newtabShown;
  await popupShown;

  is(
    getNotificationSetting(extensionTwoId),
    null,
    "The New Tab notification is not set after restoring the settings"
  );
  let addonOne = await AddonManager.getAddonByID(extensionOneId);
  is(
    addonOne.userDisabled,
    false,
    "The extension is enabled before making a choice"
  );
  is(
    getNotificationSetting(extensionOneId),
    null,
    "The New Tab notification is not set before making a choice"
  );
  is(
    panel.getAttribute("panelopen"),
    "true",
    "The notification panel is open after opening New Tab"
  );
  is(
    gBrowser.currentURI.spec,
    AboutNewTab.newTabURL,
    "The user is now on the next extension's New Tab page"
  );

  preferencesShown = TestUtils.waitForCondition(
    () => gBrowser.currentURI.spec == "about:preferences#home",
    "Should open about:preferences."
  );
  popupHidden = promisePopupHidden(panel);
  clickManage(notification);
  await popupHidden;
  await preferencesShown;
  // remove the extra preferences tab.
  BrowserTestUtils.removeTab(tab2);

  addonDisabled = waitForAddonDisabled(addonOne);
  addonOne.disable();
  await addonDisabled;
  tab2 = await promiseNewTab();

  ok(
    panel.getAttribute("panelopen") != "true",
    "The notification panel is closed after restoring the second time"
  );
  is(
    getNotificationSetting(extensionOneId),
    null,
    "The New Tab notification is not set after restoring the settings"
  );
  is(
    gBrowser.currentURI.spec,
    "about:newtab",
    "The user is now on the original New Tab URL since all extensions are disabled"
  );

  // Reopen a browser tab and verify that there's no doorhanger.
  BrowserTestUtils.removeTab(tab2);
  tab2 = await promiseNewTab();

  ok(
    panel.getAttribute("panelopen") != "true",
    "The notification panel is not opened after keeping the changes"
  );

  // FIXME: We need to enable the add-on so it gets cleared from the
  // ExtensionSettingsStore for now. See bug 1408226.
  let addonsEnabled = Promise.all([
    waitForAddonEnabled(addonOne),
    waitForAddonEnabled(addonTwo),
  ]);
  await addonOne.enable();
  await addonTwo.enable();
  await addonsEnabled;
  BrowserTestUtils.removeTab(tab1);
  BrowserTestUtils.removeTab(tab2);

  await extensionOne.unload();
  await extensionTwo.unload();
});

/**
 * Ensure we don't show the extension URL in the URL bar temporarily in new tabs
 * while we're switching remoteness (when the URL we're loading and the
 * default content principal are different).
 */
add_task(async function dontTemporarilyShowAboutExtensionPath() {
  await ExtensionSettingsStore.initialize();
  let extension = ExtensionTestUtils.loadExtension(extensionData);

  await extension.startup();
  let extensionNewTabUrl = `moz-extension://${extension.uuid}/newtab.html`;

  let wpl = {
    onLocationChange() {
      is(gURLBar.value, "", "URL bar value should stay empty.");
    },
  };
  gBrowser.addProgressListener(wpl);

  let tab = await BrowserTestUtils.openNewForegroundTab({
    gBrowser,
    url: extensionNewTabUrl,
  });

  gBrowser.removeProgressListener(wpl);
  is(gURLBar.value, "", "URL bar value should be empty.");
  await SpecialPowers.spawn(tab.linkedBrowser, [], function () {
    is(
      content.document.body.textContent,
      "New tab!",
      "New tab page is loaded."
    );
  });

  BrowserTestUtils.removeTab(tab);
  await extension.unload();
});

add_task(async function test_overriding_newtab_incognito_not_allowed() {
  let panel = getNewTabDoorhanger().closest("panel");

  let extension = ExtensionTestUtils.loadExtension({
    ...extensionData,
    useAddonManager: "permanent",
  });

  await extension.startup();
  let extensionNewTabUrl = `moz-extension://${extension.uuid}/newtab.html`;

  let popupShown = promisePopupShown(panel);
  let tab = await promiseNewTab(extensionNewTabUrl);
  await popupShown;

  // This will show a confirmation doorhanger, make sure we don't leave it open.
  let popupHidden = promisePopupHidden(panel);
  panel.hidePopup();
  await popupHidden;

  BrowserTestUtils.removeTab(tab);

  // Verify a private window does not open the extension page.  We would
  // get an extra notification that we don't listen for if it gets loaded.
  let windowOpenedPromise = BrowserTestUtils.waitForNewWindow();
  let win = OpenBrowserWindow({ private: true });
  await windowOpenedPromise;

  await promiseNewTab("about:privatebrowsing", win);

  is(win.gURLBar.value, "", "newtab not used in private window");

  // Verify setting the pref directly doesn't bypass permissions.
  let origUrl = AboutNewTab.newTabURL;
  AboutNewTab.newTabURL = extensionNewTabUrl;
  await promiseNewTab("about:privatebrowsing", win);

  is(win.gURLBar.value, "", "directly set newtab not used in private window");

  AboutNewTab.newTabURL = origUrl;

  await extension.unload();
  await BrowserTestUtils.closeWindow(win);
});

add_task(async function test_overriding_newtab_incognito_spanning() {
  let extension = ExtensionTestUtils.loadExtension({
    ...extensionData,
    useAddonManager: "permanent",
    incognitoOverride: "spanning",
  });

  await extension.startup();
  let extensionNewTabUrl = `moz-extension://${extension.uuid}/newtab.html`;

  let windowOpenedPromise = BrowserTestUtils.waitForNewWindow();
  let win = OpenBrowserWindow({ private: true });
  await windowOpenedPromise;
  let panel = ExtensionControlledPopup._getAndMaybeCreatePanel(win.document);
  let popupShown = promisePopupShown(panel);
  await promiseNewTab(extensionNewTabUrl, win);
  await popupShown;

  // This will show a confirmation doorhanger, make sure we don't leave it open.
  let popupHidden = promisePopupHidden(panel);
  panel.hidePopup();
  await popupHidden;

  await extension.unload();
  await BrowserTestUtils.closeWindow(win);
});

// Test that prefs set by the newtab override code are
// properly unset when all newtab extensions are gone.
add_task(async function testNewTabPrefsReset() {
  function isUndefinedPref(pref) {
    try {
      Services.prefs.getBoolPref(pref);
      return false;
    } catch (e) {
      return true;
    }
  }

  ok(
    isUndefinedPref("browser.newtab.extensionControlled"),
    "extensionControlled pref is not set"
  );
  ok(
    isUndefinedPref("browser.newtab.privateAllowed"),
    "privateAllowed pref is not set"
  );
});

// This test ensures that an extension provided newtab
// can be opened by another extension (e.g. tab manager)
// regardless of whether the newtab url is made available
// in web_accessible_resources.
add_task(async function test_newtab_from_extension() {
  let panel = getNewTabDoorhanger().closest("panel");

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      browser_specific_settings: {
        gecko: {
          id: "newtaburl@mochi.test",
        },
      },
      chrome_url_overrides: {
        newtab: "newtab.html",
      },
    },
    files: {
      "newtab.html": `<h1>New tab!</h1><script src="newtab.js"></script>`,
      "newtab.js": () => {
        browser.test.sendMessage("newtab-loaded");
      },
    },
    useAddonManager: "temporary",
  });

  await extension.startup();
  let extensionNewTabUrl = `moz-extension://${extension.uuid}/newtab.html`;

  let popupShown = promisePopupShown(panel);
  let tab = await promiseNewTab(extensionNewTabUrl);
  await popupShown;

  // This will show a confirmation doorhanger, make sure we don't leave it open.
  let popupHidden = promisePopupHidden(panel);
  panel.hidePopup();
  await popupHidden;

  BrowserTestUtils.removeTab(tab);

  // extension to open the newtab
  let opener = ExtensionTestUtils.loadExtension({
    async background() {
      let newtab = await browser.tabs.create({});
      browser.test.assertTrue(
        newtab.id !== browser.tabs.TAB_ID_NONE,
        "New tab was created."
      );
      await browser.tabs.remove(newtab.id);
      browser.test.sendMessage("complete");
    },
  });

  function listener(msg) {
    Assert.ok(!/may not load or link to moz-extension/.test(msg.message));
  }
  Services.console.registerListener(listener);
  registerCleanupFunction(() => {
    Services.console.unregisterListener(listener);
  });

  await opener.startup();
  await opener.awaitMessage("complete");
  await extension.awaitMessage("newtab-loaded");
  await opener.unload();
  await extension.unload();
});