summaryrefslogtreecommitdiffstats
path: root/browser/components/extensions/test/browser/browser_ext_devtools_panel.js
blob: c9bf12b9fdfc9fba5c98e922699a3bec185a0a93 (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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
"use strict";

// Like most of the mochitest-browser devtools test,
// on debug test machine, it takes about 50s to run the test.
requestLongerTimeout(4);

loadTestSubscript("head_devtools.js");

ChromeUtils.defineESModuleGetters(this, {
  Preferences: "resource://gre/modules/Preferences.sys.mjs",
});

const DEVTOOLS_THEME_PREF = "devtools.theme";

/**
 * This test file ensures that:
 *
 * - devtools.panels.themeName returns the correct value,
 *   both from a page and a panel.
 * - devtools.panels.onThemeChanged fires for theme changes,
 *   both from a page and a panel.
 * - devtools.panels.create is able to create a devtools panel.
 */

function createPage(jsScript, bodyText = "") {
  return `<!DOCTYPE html>
    <html>
       <head>
         <meta charset="utf-8">
       </head>
       <body>
         ${bodyText}
         <script src="${jsScript}"></script>
       </body>
    </html>`;
}

async function test_theme_name(testWithPanel = false) {
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "http://mochi.test:8888/"
  );

  function switchTheme(theme) {
    const waitforThemeChanged = gDevTools.once("theme-changed");
    Preferences.set(DEVTOOLS_THEME_PREF, theme);
    return waitforThemeChanged;
  }

  async function testThemeSwitching(extension, locations = ["page"]) {
    for (let newTheme of ["dark", "light"]) {
      await switchTheme(newTheme);
      for (let location of locations) {
        is(
          await extension.awaitMessage(`devtools_theme_changed_${location}`),
          newTheme,
          `The onThemeChanged event listener fired for the ${location}.`
        );
        is(
          await extension.awaitMessage(`current_theme_${location}`),
          newTheme,
          `The current theme is reported as expected for the ${location}.`
        );
      }
    }
  }

  async function devtools_page(createPanel) {
    if (createPanel) {
      await browser.devtools.panels.create(
        "Test Panel Theme",
        "fake-icon.png",
        "devtools_panel.html"
      );
    }

    browser.devtools.panels.onThemeChanged.addListener(themeName => {
      browser.test.sendMessage("devtools_theme_changed_page", themeName);
      browser.test.sendMessage(
        "current_theme_page",
        browser.devtools.panels.themeName
      );
    });

    browser.test.sendMessage(
      "initial_theme_page",
      browser.devtools.panels.themeName
    );
  }

  async function devtools_panel() {
    browser.devtools.panels.onThemeChanged.addListener(themeName => {
      browser.test.sendMessage("devtools_theme_changed_panel", themeName);
      browser.test.sendMessage(
        "current_theme_panel",
        browser.devtools.panels.themeName
      );
    });

    browser.test.sendMessage(
      "initial_theme_panel",
      browser.devtools.panels.themeName
    );
  }

  let files = {
    "devtools_page.html": createPage("devtools_page.js"),
    "devtools_page.js": `(${devtools_page})(${testWithPanel})`,
  };

  if (testWithPanel) {
    files["devtools_panel.js"] = devtools_panel;
    files["devtools_panel.html"] = createPage(
      "devtools_panel.js",
      "Test Panel Theme"
    );
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      devtools_page: "devtools_page.html",
    },
    files,
  });

  // Ensure that the initial value of the devtools theme is "light".
  await SpecialPowers.pushPrefEnv({ set: [[DEVTOOLS_THEME_PREF, "light"]] });
  registerCleanupFunction(async function () {
    await SpecialPowers.popPrefEnv();
  });

  await extension.startup();

  const toolbox = await openToolboxForTab(tab);

  info("Waiting initial theme from devtools_page");
  is(
    await extension.awaitMessage("initial_theme_page"),
    "light",
    "The initial theme is reported as expected."
  );

  if (testWithPanel) {
    let toolboxAdditionalTools = toolbox.getAdditionalTools();
    is(
      toolboxAdditionalTools.length,
      1,
      "Got the expected number of toolbox specific panel registered."
    );

    let panelId = toolboxAdditionalTools[0].id;

    await gDevTools.showToolboxForTab(tab, { toolId: panelId });
    is(
      await extension.awaitMessage("initial_theme_panel"),
      "light",
      "The initial theme is reported as expected from a devtools panel."
    );

    await testThemeSwitching(extension, ["page", "panel"]);
  } else {
    await testThemeSwitching(extension);
  }

  await closeToolboxForTab(tab);

  await extension.unload();

  BrowserTestUtils.removeTab(tab);
}

add_task(async function test_devtools_page_theme() {
  await test_theme_name(false);
});

add_task(async function test_devtools_panel_theme() {
  await test_theme_name(true);
});

add_task(async function test_devtools_page_panels_create() {
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "http://mochi.test:8888/"
  );

  async function devtools_page() {
    const result = {
      devtoolsPageTabId: browser.devtools.inspectedWindow.tabId,
      panelCreated: 0,
      panelShown: 0,
      panelHidden: 0,
    };

    try {
      const panel = await browser.devtools.panels.create(
        "Test Panel Create",
        "fake-icon.png",
        "devtools_panel.html"
      );

      result.panelCreated++;

      panel.onShown.addListener(contentWindow => {
        result.panelShown++;
        browser.test.assertEq(
          "complete",
          contentWindow.document.readyState,
          "Got the expected 'complete' panel document readyState"
        );
        browser.test.assertEq(
          "test_panel_global",
          contentWindow.TEST_PANEL_GLOBAL,
          "Got the expected global in the panel contentWindow"
        );
        browser.test.sendMessage("devtools_panel_shown", result);
      });

      panel.onHidden.addListener(() => {
        result.panelHidden++;

        browser.test.sendMessage("devtools_panel_hidden", result);
      });

      browser.test.sendMessage("devtools_panel_created");
    } catch (err) {
      // Make the test able to fail fast when it is going to be a failure.
      browser.test.sendMessage("devtools_panel_created");
      throw err;
    }
  }

  function devtools_panel() {
    // Set a property in the global and check that it is defined
    // and accessible from the devtools_page when the panel.onShown
    // event has been received.
    window.TEST_PANEL_GLOBAL = "test_panel_global";

    browser.test.sendMessage(
      "devtools_panel_inspectedWindow_tabId",
      browser.devtools.inspectedWindow.tabId
    );
  }

  const longPrefix = new Array(80).fill("x").join("");
  // Extension ID includes "inspector" to verify Bug 1474379 doesn't regress.
  const EXTENSION_ID = `${longPrefix}-inspector@create-devtools-panel.test`;

  let extension = ExtensionTestUtils.loadExtension({
    useAddonManager: "temporary",
    manifest: {
      devtools_page: "devtools_page.html",
      browser_specific_settings: {
        gecko: { id: EXTENSION_ID },
      },
    },
    files: {
      "devtools_page.html": createPage("devtools_page.js"),
      "devtools_page.js": devtools_page,
      "devtools_panel.html": createPage(
        "devtools_panel.js",
        "Test Panel Create"
      ),
      "devtools_panel.js": devtools_panel,
    },
  });

  await extension.startup();

  const extensionPrefBranch = `devtools.webextensions.${EXTENSION_ID}.`;
  const extensionPrefName = `${extensionPrefBranch}enabled`;

  let prefBranch = Services.prefs.getBranch(extensionPrefBranch);
  ok(
    prefBranch,
    "The preference branch for the extension should have been created"
  );
  is(
    prefBranch.getBoolPref("enabled", false),
    true,
    "The 'enabled' bool preference for the extension should be initially true"
  );

  // Get the devtools panel info for the first item in the toolbox additional tools array.
  const getPanelInfo = toolbox => {
    let toolboxAdditionalTools = toolbox.getAdditionalTools();
    is(
      toolboxAdditionalTools.length,
      1,
      "Got the expected number of toolbox specific panel registered."
    );
    return toolboxAdditionalTools[0];
  };

  // Test the devtools panel shown and hide events.
  const testPanelShowAndHide = async ({
    tab,
    panelId,
    isFirstPanelLoad,
    expectedResults,
  }) => {
    info("Wait Addon Devtools Panel to be shown");

    await gDevTools.showToolboxForTab(tab, { toolId: panelId });
    const { devtoolsPageTabId } = await extension.awaitMessage(
      "devtools_panel_shown"
    );

    // If the panel is loaded for the first time, we expect to also
    // receive the test messages and assert that both the page and the panel
    // have the same devtools.inspectedWindow.tabId value.
    if (isFirstPanelLoad) {
      const devtoolsPanelTabId = await extension.awaitMessage(
        "devtools_panel_inspectedWindow_tabId"
      );
      is(
        devtoolsPanelTabId,
        devtoolsPageTabId,
        "Got the same devtools.inspectedWindow.tabId from devtools page and panel"
      );
    }

    info("Wait Addon Devtools Panel to be shown");

    await gDevTools.showToolboxForTab(tab, { toolId: "testBlankPanel" });
    const results = await extension.awaitMessage("devtools_panel_hidden");

    // We already checked the tabId, remove it from the results, so that we can check
    // the remaining properties using a single Assert.deepEqual.
    delete results.devtoolsPageTabId;

    Assert.deepEqual(
      results,
      expectedResults,
      "Got the expected number of created panels and shown/hidden events"
    );
  };

  // Test the extension devtools_page enabling/disabling through the related
  // about:config preference.
  const testExtensionDevToolsPref = async ({
    prefValue,
    toolbox,
    oldPanelId,
  }) => {
    if (!prefValue) {
      // Test that the extension devtools_page is shutting down when the related
      // about:config preference has been set to false, and the panel on its left
      // is being selected.
      info(
        "Turning off the extension devtools page from its about:config preference"
      );
      let waitToolSelected = toolbox.once("select");
      Services.prefs.setBoolPref(extensionPrefName, false);
      const selectedTool = await waitToolSelected;
      isnot(
        selectedTool,
        oldPanelId,
        "Expect a different panel to be selected"
      );

      let toolboxAdditionalTools = toolbox.getAdditionalTools();
      is(
        toolboxAdditionalTools.length,
        0,
        "Extension devtools panel unregistered"
      );
      is(
        toolbox.visibleAdditionalTools.filter(toolId => toolId == oldPanelId)
          .length,
        0,
        "Removed panel should not be listed in the visible additional tools"
      );
    } else {
      // Test that the extension devtools_page and panel are being created again when
      // the related about:config preference has been set to true.
      info(
        "Turning on the extension devtools page from its about:config preference"
      );
      Services.prefs.setBoolPref(extensionPrefName, true);
      await extension.awaitMessage("devtools_panel_created");

      let toolboxAdditionalTools = toolbox.getAdditionalTools();
      is(
        toolboxAdditionalTools.length,
        1,
        "Got one extension devtools panel registered"
      );

      let newPanelId = getPanelInfo(toolbox).id;
      is(
        toolbox.visibleAdditionalTools.filter(toolId => toolId == newPanelId)
          .length,
        1,
        "Extension panel is listed in the visible additional tools"
      );
    }
  };

  // Wait that the devtools_page has created its devtools panel and retrieve its
  // panel id.
  let toolbox = await openToolboxForTab(tab);
  await extension.awaitMessage("devtools_panel_created");
  let panelId = getPanelInfo(toolbox).id;

  info("Test panel show and hide - first cycle");
  await testPanelShowAndHide({
    tab,
    panelId,
    isFirstPanelLoad: true,
    expectedResults: {
      panelCreated: 1,
      panelShown: 1,
      panelHidden: 1,
    },
  });

  info("Test panel show and hide - second cycle");
  await testPanelShowAndHide({
    tab,
    panelId,
    isFirstPanelLoad: false,
    expectedResults: {
      panelCreated: 1,
      panelShown: 2,
      panelHidden: 2,
    },
  });

  // Go back to the extension devtools panel.
  await gDevTools.showToolboxForTab(tab, { toolId: panelId });
  await extension.awaitMessage("devtools_panel_shown");

  // Check that the aria-label has been set on the devtools panel.
  const panelFrame = toolbox.doc.getElementById(
    `toolbox-panel-iframe-${panelId}`
  );
  const panelInfo = getPanelInfo(toolbox);
  ok(
    panelInfo.panelLabel && !!panelInfo.panelLabel.length,
    "Expect the registered panel to include a non empty panelLabel property"
  );
  is(
    panelFrame && panelFrame.getAttribute("aria-label"),
    panelInfo.panelLabel,
    "Got the expected aria-label on the extension panel frame"
  );

  // Turn off the extension devtools page using the preference that enable/disable the
  // devtools page for a given installed WebExtension.
  await testExtensionDevToolsPref({
    toolbox,
    prefValue: false,
    oldPanelId: panelId,
  });

  // Close and Re-open the toolbox to verify that the toolbox doesn't load the
  // devtools_page and the devtools panel.
  info("Re-open the toolbox and expect no extension devtools panel");
  await closeToolboxForTab(tab);
  toolbox = await openToolboxForTab(tab);

  let toolboxAdditionalTools = toolbox.getAdditionalTools();
  is(
    toolboxAdditionalTools.length,
    0,
    "Got no extension devtools panel on the opened toolbox as expected."
  );

  // Close and Re-open the toolbox to verify that the toolbox does load the
  // devtools_page and the devtools panel again.
  info("Restart the toolbox and enable the extension devtools panel");
  await closeToolboxForTab(tab);
  toolbox = await openToolboxForTab(tab);

  // Turn the addon devtools panel back on using the preference that enable/disable the
  // devtools page for a given installed WebExtension.
  await testExtensionDevToolsPref({
    toolbox,
    prefValue: true,
  });

  // Test devtools panel is loaded correctly after being toggled and
  // devtools panel events has been fired as expected.
  panelId = getPanelInfo(toolbox).id;

  info("Test panel show and hide - after disabling/enabling devtools_page");
  await testPanelShowAndHide({
    tab,
    panelId,
    isFirstPanelLoad: true,
    expectedResults: {
      panelCreated: 1,
      panelShown: 1,
      panelHidden: 1,
    },
  });

  await closeToolboxForTab(tab);

  await extension.unload();

  // Verify that the extension preference branch has been removed once the extension
  // has been uninstalled.
  prefBranch = Services.prefs.getBranch(extensionPrefBranch);
  is(
    prefBranch.getPrefType("enabled"),
    prefBranch.PREF_INVALID,
    "The preference branch for the extension should have been removed"
  );

  BrowserTestUtils.removeTab(tab);
});

add_task(async function test_devtools_page_panels_switch_toolbox_host() {
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "http://mochi.test:8888/"
  );

  function devtools_panel() {
    const hasDevToolsAPINamespace = "devtools" in browser;

    browser.test.sendMessage("devtools_panel_loaded", {
      hasDevToolsAPINamespace,
      panelLoadedURL: window.location.href,
    });
  }

  async function devtools_page() {
    const panel = await browser.devtools.panels.create(
      "Test Panel Switch Host",
      "fake-icon.png",
      "devtools_panel.html"
    );

    panel.onShown.addListener(panelWindow => {
      browser.test.sendMessage(
        "devtools_panel_shown",
        panelWindow.location.href
      );
    });

    panel.onHidden.addListener(() => {
      browser.test.sendMessage("devtools_panel_hidden");
    });

    browser.test.sendMessage("devtools_panel_created");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      devtools_page: "devtools_page.html",
    },
    files: {
      "devtools_page.html": createPage("devtools_page.js"),
      "devtools_page.js": devtools_page,
      "devtools_panel.html": createPage("devtools_panel.js", "DEVTOOLS PANEL"),
      "devtools_panel.js": devtools_panel,
    },
  });

  await extension.startup();

  let toolbox = await openToolboxForTab(tab);
  await extension.awaitMessage("devtools_panel_created");

  const toolboxAdditionalTools = toolbox.getAdditionalTools();

  is(
    toolboxAdditionalTools.length,
    1,
    "Got the expected number of toolbox specific panel registered."
  );

  const panelDef = toolboxAdditionalTools[0];
  const panelId = panelDef.id;

  info("Selecting the addon devtools panel");
  await gDevTools.showToolboxForTab(tab, { toolId: panelId });

  info("Wait for the panel to show and load for the first time");
  const panelShownURL = await extension.awaitMessage("devtools_panel_shown");

  const { panelLoadedURL, hasDevToolsAPINamespace } =
    await extension.awaitMessage("devtools_panel_loaded");

  is(
    panelShownURL,
    panelLoadedURL,
    "Got the expected panel URL on the first load"
  );
  ok(
    hasDevToolsAPINamespace,
    "The devtools panel has the devtools API on the first load"
  );

  const originalToolboxHostType = toolbox.hostType;

  info("Switch the toolbox from docked on bottom to docked on right");
  toolbox.switchHost("right");

  info(
    "Wait for the panel to emit hide, show and load messages once docked on side"
  );
  await extension.awaitMessage("devtools_panel_hidden");
  const dockedOnSideShownURL = await extension.awaitMessage(
    "devtools_panel_shown"
  );

  is(
    dockedOnSideShownURL,
    panelShownURL,
    "Got the expected panel url once the panel shown event has been emitted on toolbox host changed"
  );

  const dockedOnSideLoaded = await extension.awaitMessage(
    "devtools_panel_loaded"
  );

  is(
    dockedOnSideLoaded.panelLoadedURL,
    panelShownURL,
    "Got the expected panel url once the panel has been reloaded on toolbox host changed"
  );
  ok(
    dockedOnSideLoaded.hasDevToolsAPINamespace,
    "The devtools panel has the devtools API once the toolbox host has been changed"
  );

  info("Switch the toolbox from docked on bottom to the original dock mode");
  toolbox.switchHost(originalToolboxHostType);

  info(
    "Wait for the panel test messages once toolbox dock mode has been restored"
  );
  await extension.awaitMessage("devtools_panel_hidden");
  await extension.awaitMessage("devtools_panel_shown");
  await extension.awaitMessage("devtools_panel_loaded");

  await closeToolboxForTab(tab);

  await extension.unload();

  BrowserTestUtils.removeTab(tab);
});

add_task(async function test_devtools_page_invalid_panel_urls() {
  let tab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    "http://mochi.test:8888/"
  );

  async function devtools_page() {
    const matchInvalidPanelURL = /must be a relative URL/;
    const matchInvalidIconURL =
      /be one of \[""\], or match the format "strictRelativeUrl"/;

    // Invalid panel urls (validated by the schema wrappers, throws on invalid urls).
    const invalid_panels = [
      {
        panel: "about:about",
        icon: "icon.png",
        expectError: matchInvalidPanelURL,
      },
      {
        panel: "about:addons",
        icon: "icon.png",
        expectError: matchInvalidPanelURL,
      },
      {
        panel: "http://mochi.test:8888",
        icon: "icon.png",
        expectError: matchInvalidPanelURL,
      },
      // Invalid icon urls (validated inside the API method because of the empty icon string
      // which have to be resolved to the default icon, reject the returned promise).
      {
        panel: "panel.html",
        icon: "about:about",
        expectError: matchInvalidIconURL,
      },
      {
        panel: "panel.html",
        icon: "http://mochi.test:8888",
        expectError: matchInvalidIconURL,
      },
    ];

    const valid_panels = [
      { panel: "panel.html", icon: "icon.png" },
      { panel: "./panel.html", icon: "icon.png" },
      { panel: "/panel.html", icon: "icon.png" },
      { panel: "/panel.html", icon: "" },
    ];

    let valid_panels_length = valid_panels.length;

    const test_cases = [].concat(invalid_panels, valid_panels);

    browser.test.onMessage.addListener(async msg => {
      if (msg !== "start_test_panel_create") {
        return;
      }

      for (let { panel, icon, expectError } of test_cases) {
        browser.test.log(
          `Testing devtools.panels.create for ${JSON.stringify({
            panel,
            icon,
          })}`
        );

        if (expectError) {
          // Verify that invalid panel urls throw.
          browser.test.assertThrows(
            () => browser.devtools.panels.create("Test Panel", icon, panel),
            expectError,
            "Got the expected rejection on creating a devtools panel with " +
              `panel url ${panel} and icon ${icon}`
          );
        } else {
          // Verify that with valid panel and icon urls the panel is created and loaded
          // as expected.
          try {
            const pane = await browser.devtools.panels.create(
              "Test Panel",
              icon,
              panel
            );

            valid_panels_length--;

            // Wait the panel to be loaded.
            const oncePanelLoaded = new Promise(resolve => {
              pane.onShown.addListener(paneWin => {
                browser.test.assertTrue(
                  paneWin.location.href.endsWith("/panel.html"),
                  `The panel has loaded the expected extension URL with ${panel}`
                );
                resolve();
              });
            });

            // Ask the privileged code to select the last created panel.
            const done = valid_panels_length === 0;
            browser.test.sendMessage("select-devtools-panel", done);
            await oncePanelLoaded;
          } catch (err) {
            browser.test.fail(
              "Unexpected failure on creating a devtools panel with " +
                `panel url ${panel} and icon ${icon}`
            );
            throw err;
          }
        }
      }

      browser.test.sendMessage("test_invalid_devtools_panel_urls_done");
    });

    browser.test.sendMessage("devtools_page_ready");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      devtools_page: "devtools_page.html",
      icons: {
        32: "icon.png",
      },
    },
    files: {
      "devtools_page.html": createPage("devtools_page.js"),
      "devtools_page.js": devtools_page,
      "panel.html": createPage("panel.js", "DEVTOOLS PANEL"),
      "panel.js": "",
      "icon.png": imageBuffer,
      "default-icon.png": imageBuffer,
    },
  });

  await extension.startup();

  let toolbox = await openToolboxForTab(tab);
  info("developer toolbox opened");

  await extension.awaitMessage("devtools_page_ready");

  extension.sendMessage("start_test_panel_create");

  let done = false;

  while (!done) {
    info("Waiting test extension request to select the last created panel");
    done = await extension.awaitMessage("select-devtools-panel");

    const toolboxAdditionalTools = toolbox.getAdditionalTools();
    const lastTool = toolboxAdditionalTools[toolboxAdditionalTools.length - 1];

    gDevTools.showToolboxForTab(tab, { toolId: lastTool.id });
    info("Last created panel selected");
  }

  await extension.awaitMessage("test_invalid_devtools_panel_urls_done");

  await closeToolboxForTab(tab);

  await extension.unload();

  BrowserTestUtils.removeTab(tab);
});