tor-browser

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

browser_toolbox_frames_list.js (4714B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Check that the frames list gets updated as iframes are added/removed from the document,
      7 // and during navigation.
      8 
      9 const TEST_COM_URL =
     10  "https://example.com/document-builder.sjs?html=<div id=com>com";
     11 const TEST_ORG_URL =
     12  `https://example.org/document-builder.sjs?html=<div id=org>org</div>` +
     13  `<iframe src="https://example.org/document-builder.sjs?html=example.org iframe"></iframe>` +
     14  `<iframe src="https://example.com/document-builder.sjs?html=example.com iframe"></iframe>`;
     15 
     16 add_task(async function () {
     17  // Enable the frames button.
     18  await pushPref("devtools.command-button-frames.enabled", true);
     19 
     20  const tab = await addTab(TEST_COM_URL);
     21 
     22  const toolbox = await gDevTools.showToolboxForTab(tab, {
     23    toolId: "webconsole",
     24  });
     25 
     26  ok(
     27    !getFramesButton(toolbox),
     28    "Frames button is not rendered when there's no iframes in the page"
     29  );
     30  await checkFramesList(toolbox, []);
     31 
     32  info("Create a same origin (example.com) iframe");
     33  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
     34    const comIframe = content.document.createElement("iframe");
     35    comIframe.src =
     36      "https://example.com/document-builder.sjs?html=example.com iframe";
     37    content.document.body.appendChild(comIframe);
     38  });
     39 
     40  await waitFor(() => getFramesButton(toolbox));
     41  ok(true, "Button is displayed when adding an iframe");
     42 
     43  info("Check the content of the frames list");
     44  await checkFramesList(toolbox, [
     45    TEST_COM_URL,
     46    "https://example.com/document-builder.sjs?html=example.com iframe",
     47  ]);
     48 
     49  info("Create a cross-process origin (example.org) iframe");
     50  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
     51    const orgIframe = content.document.createElement("iframe");
     52    orgIframe.src =
     53      "https://example.org/document-builder.sjs?html=example.org iframe";
     54    content.document.body.appendChild(orgIframe);
     55  });
     56 
     57  info("Check that the content of the frames list was updated");
     58  await checkFramesList(toolbox, [
     59    TEST_COM_URL,
     60    "https://example.com/document-builder.sjs?html=example.com iframe",
     61    "https://example.org/document-builder.sjs?html=example.org iframe",
     62  ]);
     63 
     64  info("Reload and check that the frames list is cleared");
     65  await reloadBrowser();
     66  await waitFor(() => !getFramesButton(toolbox));
     67  ok(
     68    true,
     69    "The button was hidden when reloading as the page does not have iframes"
     70  );
     71  await checkFramesList(toolbox, []);
     72 
     73  info("Navigate to a different origin, on a page with iframes");
     74  await navigateTo(TEST_ORG_URL);
     75  await checkFramesList(toolbox, [
     76    TEST_ORG_URL,
     77    "https://example.org/document-builder.sjs?html=example.org iframe",
     78    "https://example.com/document-builder.sjs?html=example.com iframe",
     79  ]);
     80 
     81  info("Check that frames list is updated when removing same-origin iframe");
     82  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
     83    content.document.querySelector("iframe").remove();
     84  });
     85  await checkFramesList(toolbox, [
     86    TEST_ORG_URL,
     87    "https://example.com/document-builder.sjs?html=example.com iframe",
     88  ]);
     89 
     90  info("Check that frames list is updated when removing cross-origin iframe");
     91  await SpecialPowers.spawn(tab.linkedBrowser, [], async function () {
     92    content.document.querySelector("iframe").remove();
     93  });
     94  await waitFor(() => !getFramesButton(toolbox));
     95  ok(true, "The button was hidden when removing the last iframe on the page");
     96  await checkFramesList(toolbox, []);
     97 
     98  info("Check that the list does have expected items after reloading");
     99  await reloadBrowser();
    100  await waitFor(() => getFramesButton(toolbox));
    101  ok(true, "button is displayed after reloading");
    102  await checkFramesList(toolbox, [
    103    TEST_ORG_URL,
    104    "https://example.org/document-builder.sjs?html=example.org iframe",
    105    "https://example.com/document-builder.sjs?html=example.com iframe",
    106  ]);
    107 });
    108 
    109 function getFramesButton(toolbox) {
    110  return toolbox.doc.getElementById("command-button-frames");
    111 }
    112 
    113 async function checkFramesList(toolbox, expectedFrames) {
    114  const frames = await waitFor(() => {
    115    // items might be added in the list before their url is known, so exclude empty items.
    116    const f = getFramesLabels(toolbox).filter(t => t !== "");
    117    if (f.length !== expectedFrames.length) {
    118      return false;
    119    }
    120 
    121    return f;
    122  });
    123 
    124  is(
    125    JSON.stringify(frames.sort()),
    126    JSON.stringify(expectedFrames.sort()),
    127    "The expected frames are displayed"
    128  );
    129 }
    130 
    131 function getFramesLabels(toolbox) {
    132  return Array.from(
    133    toolbox.doc.querySelectorAll("#toolbox-frame-menu .command .label")
    134  ).map(el => el.textContent);
    135 }