tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_webchannel-open-script-in-debugger.js (2986B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 /* eslint-disable max-nested-callbacks */
      8 
      9 const ASSET_BASE_URL =
     10  "https://example.com/browser/devtools/client/performance-new/test/browser/webchannel-open-script-in-debugger_assets/";
     11 const TEST_PAGE_URL = ASSET_BASE_URL + "test-page.html";
     12 const TEST_SCRIPT_URL = ASSET_BASE_URL + "test-script.js";
     13 const FRONTEND_URL = ASSET_BASE_URL + "webchannel.html";
     14 
     15 const {
     16  gDevTools,
     17 } = require("resource://devtools/client/framework/devtools.js");
     18 
     19 add_task(async function test() {
     20  info(
     21    "Test the WebChannel mechanism works for opening the devtools debugger with the script"
     22  );
     23 
     24  // First open the test page so it can load a simple script.
     25  await BrowserTestUtils.withNewTab(
     26    {
     27      gBrowser,
     28      url: TEST_PAGE_URL,
     29    },
     30    async browser => {
     31      const testPageTabId = browser.browsingContext.browserId;
     32      const request = {
     33        tabId: testPageTabId,
     34        scriptUrl: TEST_SCRIPT_URL,
     35        line: 1,
     36        column: 1,
     37      };
     38 
     39      // Make sure that the current active tab is the test page for a sanity check.
     40      Assert.strictEqual(
     41        gBrowser.selectedBrowser.browsingContext.browserId,
     42        testPageTabId,
     43        "Current selected tab is the test page."
     44      );
     45 
     46      const onToolboxReady = gDevTools.once("toolbox-ready");
     47      // Now open the second tab that will send the webchannel request.
     48      // After the request it must switch to the first tab, and then
     49      await BrowserTestUtils.withNewTab(
     50        {
     51          gBrowser,
     52          url:
     53            FRONTEND_URL +
     54            "?request=" +
     55            encodeURIComponent(JSON.stringify(request)),
     56        },
     57        async () => {
     58          // Note that we could do a sanity check here for the selected tab, but
     59          // it will be racy in case the code inside the html page gets executed
     60          // quickly. So it's better to not do it.
     61          await onToolboxReady;
     62          ok(true, "Toolbox is successfully loaded");
     63 
     64          // Check that the active tab is back to the first tab again.
     65          Assert.strictEqual(
     66            gBrowser.selectedBrowser.browsingContext.browserId,
     67            testPageTabId,
     68            "Selected tab should be switched back to the test page again."
     69          );
     70 
     71          // Now check if the debugger successfully loaded the source.
     72          const toolbox = gDevTools.getToolboxForTab(gBrowser.selectedTab);
     73          ok(!!toolbox, "Toolbox is opened successfully");
     74 
     75          const dbg = toolbox.getPanel("jsdebugger");
     76          await waitUntil(() => {
     77            const source = dbg._selectors.getSelectedSource(dbg._getState());
     78            return source && source.url === TEST_SCRIPT_URL;
     79          });
     80 
     81          ok(true, "Source is successfully loaded.");
     82        }
     83      );
     84    }
     85  );
     86 });