tor-browser

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

browser_target_command_frames_popups.js (4659B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test that we create targets for popups
      7 
      8 const TEST_URL = "https://example.org/document-builder.sjs?html=main page";
      9 const POPUP_URL = "https://example.com/document-builder.sjs?html=popup";
     10 const POPUP_SECOND_URL =
     11  "https://example.com/document-builder.sjs?html=popup-navigated";
     12 
     13 add_task(async function () {
     14  await pushPref("devtools.popups.debug", true);
     15 
     16  // Create a TargetCommand for a given test tab
     17  const tab = await addTab(TEST_URL);
     18  const commands = await CommandsFactory.forTab(tab);
     19  const targetCommand = commands.targetCommand;
     20  const { TYPES } = targetCommand;
     21 
     22  await targetCommand.startListening();
     23 
     24  // Assert that watchTargets will call the create callback for all existing frames
     25  const targets = [];
     26  const destroyedTargets = [];
     27  const onAvailable = ({ targetFront }) => {
     28    targets.push(targetFront);
     29  };
     30  const onDestroyed = ({ targetFront }) => {
     31    destroyedTargets.push(targetFront);
     32  };
     33  await targetCommand.watchTargets({
     34    types: [TYPES.FRAME],
     35    onAvailable,
     36    onDestroyed,
     37  });
     38 
     39  is(targets.length, 1, "At first, we only get one target");
     40  is(
     41    targets[0],
     42    targetCommand.targetFront,
     43    "And this target is the top level one"
     44  );
     45 
     46  info("Open a popup");
     47  const firstPopupBrowsingContext = await SpecialPowers.spawn(
     48    tab.linkedBrowser,
     49    [POPUP_URL],
     50    url => {
     51      const win = content.open(url);
     52      return win.browsingContext;
     53    }
     54  );
     55 
     56  await waitFor(() => targets.length === 2);
     57  ok(true, "We are notified about the first popup's target");
     58 
     59  is(
     60    targets[1].browsingContextID,
     61    firstPopupBrowsingContext.id,
     62    "the new target is for the popup"
     63  );
     64  is(targets[1].url, POPUP_URL, "the new target has the right url");
     65 
     66  info("Navigate the popup to a second location");
     67  await SpecialPowers.spawn(
     68    firstPopupBrowsingContext,
     69    [POPUP_SECOND_URL],
     70    url => {
     71      content.location.href = url;
     72    }
     73  );
     74 
     75  await waitFor(() => targets.length === 3);
     76  ok(true, "We are notified about the new location popup's target");
     77 
     78  await waitFor(() => destroyedTargets.length === 1);
     79  ok(true, "The first popup's target is destroyed");
     80  is(
     81    destroyedTargets[0],
     82    targets[1],
     83    "The destroyed target is the popup's one"
     84  );
     85 
     86  is(
     87    targets[2].browsingContextID,
     88    firstPopupBrowsingContext.id,
     89    "the new location target is for the popup"
     90  );
     91  is(
     92    targets[2].url,
     93    POPUP_SECOND_URL,
     94    "the new location target has the right url"
     95  );
     96 
     97  info("Close the popup");
     98  await SpecialPowers.spawn(firstPopupBrowsingContext, [], () => {
     99    content.close();
    100  });
    101 
    102  await waitFor(() => destroyedTargets.length === 2);
    103  ok(true, "The popup's target is destroyed");
    104  is(
    105    destroyedTargets[1],
    106    targets[2],
    107    "The destroyed target is the popup's one"
    108  );
    109 
    110  info("Open a about:blank popup");
    111  const aboutBlankPopupBrowsingContext = await SpecialPowers.spawn(
    112    tab.linkedBrowser,
    113    [],
    114    () => {
    115      const win = content.open("about:blank");
    116      return win.browsingContext;
    117    }
    118  );
    119 
    120  await waitFor(() => targets.length === 4);
    121  ok(true, "We are notified about the about:blank popup's target");
    122 
    123  is(
    124    targets[3].browsingContextID,
    125    aboutBlankPopupBrowsingContext.id,
    126    "the new target is for the popup"
    127  );
    128  is(targets[3].url, "about:blank", "the new target has the right url");
    129 
    130  info("Select the original tab and reload it");
    131  gBrowser.selectedTab = tab;
    132  await BrowserTestUtils.reloadTab(tab);
    133 
    134  await waitFor(() => targets.length === 5);
    135  is(targets[4], targetCommand.targetFront, "We get a new top level target");
    136  ok(!targets[3].isDestroyed(), "The about:blank popup target is still alive");
    137 
    138  info("Call about:blank popup method to ensure it really is functional");
    139  await targets[3].logInPage("foo");
    140 
    141  info(
    142    "Ensure that iframe using window.open to load their document aren't considered as popups"
    143  );
    144  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], async () => {
    145    const iframe = content.document.createElement("iframe");
    146    iframe.setAttribute("name", "test-iframe");
    147    content.document.documentElement.appendChild(iframe);
    148    content.open("data:text/html,iframe", "test-iframe");
    149  });
    150  await waitFor(() => targets.length === 6);
    151  is(
    152    targets[5].targetForm.isPopup,
    153    false,
    154    "The iframe target isn't considered as a popup"
    155  );
    156 
    157  targetCommand.unwatchTargets({
    158    types: [TYPES.FRAME],
    159    onAvailable,
    160    onDestroyed,
    161  });
    162  targetCommand.destroy();
    163  BrowserTestUtils.removeTab(tab);
    164  await commands.destroy();
    165 });