tor-browser

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

browser_target_command_switchToTarget.js (4421B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test the TargetCommand API switchToTarget function
      7 
      8 add_task(async function testSwitchToTarget() {
      9  info("Test TargetCommand.switchToTarget method");
     10 
     11  // Create a first target to switch from, a new tab with an iframe
     12  const firstTab = await addTab(
     13    `data:text/html,<iframe src="data:text/html,foo"></iframe>`
     14  );
     15  const commands = await CommandsFactory.forTab(firstTab);
     16  const targetCommand = commands.targetCommand;
     17  const { TYPES } = targetCommand;
     18  await targetCommand.startListening();
     19 
     20  // Create a second target to switch to, a new tab with an iframe
     21  const secondTab = await addTab(
     22    `data:text/html,<iframe src="data:text/html,bar"></iframe>`
     23  );
     24  // We have to spawn a new distinct `commands` object for this new tab,
     25  // but we will otherwise consider the first one as the main one.
     26  // From this second one, we will only retrieve a new target.
     27  const secondCommands = await CommandsFactory.forTab(secondTab, {
     28    client: commands.client,
     29  });
     30  await secondCommands.targetCommand.startListening();
     31  const secondTarget = secondCommands.targetCommand.targetFront;
     32 
     33  const frameTargets = [];
     34  const firstTarget = targetCommand.targetFront;
     35  let currentTarget = targetCommand.targetFront;
     36  const onFrameAvailable = ({ targetFront, isTargetSwitching }) => {
     37    is(
     38      targetFront.targetType,
     39      TYPES.FRAME,
     40      "We are only notified about frame targets"
     41    );
     42    ok(
     43      targetFront == currentTarget
     44        ? targetFront.isTopLevel
     45        : !targetFront.isTopLevel,
     46      "isTopLevel property is correct"
     47    );
     48    if (targetFront.isTopLevel) {
     49      // When calling watchTargets, this will be false, but it will be true when calling switchToTarget
     50      is(
     51        isTargetSwitching,
     52        currentTarget == secondTarget,
     53        "target switching boolean is correct"
     54      );
     55    } else {
     56      ok(!isTargetSwitching, "for now, only top level target can be switched");
     57    }
     58    frameTargets.push(targetFront);
     59  };
     60  const destroyedTargets = [];
     61  const onFrameDestroyed = ({ targetFront, isTargetSwitching }) => {
     62    is(
     63      targetFront.targetType,
     64      TYPES.FRAME,
     65      "target-destroyed: We are only notified about frame targets"
     66    );
     67    ok(
     68      targetFront == firstTarget
     69        ? targetFront.isTopLevel
     70        : !targetFront.isTopLevel,
     71      "target-destroyed: isTopLevel property is correct"
     72    );
     73    if (targetFront.isTopLevel) {
     74      is(
     75        isTargetSwitching,
     76        true,
     77        "target-destroyed: target switching boolean is correct"
     78      );
     79    } else {
     80      ok(
     81        !isTargetSwitching,
     82        "target-destroyed: for now, only top level target can be switched"
     83      );
     84    }
     85    destroyedTargets.push(targetFront);
     86  };
     87  await targetCommand.watchTargets({
     88    types: [TYPES.FRAME],
     89    onAvailable: onFrameAvailable,
     90    onDestroyed: onFrameDestroyed,
     91  });
     92 
     93  // Save the original list of targets
     94  const createdTargets = [...frameTargets];
     95  // Clear the recorded target list of all existing targets
     96  frameTargets.length = 0;
     97 
     98  currentTarget = secondTarget;
     99  await targetCommand.switchToTarget(secondTarget);
    100 
    101  is(
    102    targetCommand.targetFront,
    103    currentTarget,
    104    "After the switch, the top level target has been updated"
    105  );
    106  // Because JS Window Actor API isn't used yet, FrameDescriptor.getTarget returns null
    107  // And there is no target being created for the iframe, yet.
    108  // As soon as bug 1565200 is resolved, this should return two frames, including the iframe.
    109  is(
    110    frameTargets.length,
    111    1,
    112    "We get the report of the top level iframe when switching to the new target"
    113  );
    114  is(frameTargets[0], currentTarget);
    115  //is(frameTargets[1].url, "data:text/html,foo");
    116 
    117  // Ensure that all the targets reported before the call to switchToTarget
    118  // are reported as destroyed while calling switchToTarget.
    119  is(
    120    destroyedTargets.length,
    121    createdTargets.length,
    122    "All targets original reported are destroyed"
    123  );
    124  for (const newTarget of createdTargets) {
    125    ok(
    126      destroyedTargets.includes(newTarget),
    127      "Each originally target is reported as destroyed"
    128    );
    129  }
    130 
    131  targetCommand.destroy();
    132 
    133  await commands.destroy();
    134  await secondCommands.destroy();
    135 
    136  BrowserTestUtils.removeTab(firstTab);
    137  BrowserTestUtils.removeTab(secondTab);
    138 });