tor-browser

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

browser_controller.js (4123B)


      1 add_setup(async function () {
      2  await SpecialPowers.pushPrefEnv({
      3    set: [["test.wait300msAfterTabSwitch", true]],
      4  });
      5 });
      6 
      7 function checkCommandState(testid, undoEnabled, copyEnabled, deleteEnabled) {
      8  is(
      9    !document.getElementById("cmd_undo").hasAttribute("disabled"),
     10    undoEnabled,
     11    testid + " undo"
     12  );
     13  is(
     14    !document.getElementById("cmd_copy").hasAttribute("disabled"),
     15    copyEnabled,
     16    testid + " copy"
     17  );
     18  is(
     19    !document.getElementById("cmd_delete").hasAttribute("disabled"),
     20    deleteEnabled,
     21    testid + " delete"
     22  );
     23 }
     24 
     25 function keyAndUpdate(key, eventDetails, updateEventsCount) {
     26  let updatePromise = BrowserTestUtils.waitForEvent(
     27    window,
     28    "commandupdate",
     29    false,
     30    () => {
     31      return --updateEventsCount == 0;
     32    }
     33  );
     34  EventUtils.synthesizeKey(key, eventDetails);
     35  return updatePromise;
     36 }
     37 
     38 add_task(async function test_controllers_subframes() {
     39  let tab = await BrowserTestUtils.openNewForegroundTab(
     40    gBrowser,
     41    OOP_BASE_PAGE_URI
     42  );
     43  let browser = tab.linkedBrowser;
     44  let browsingContexts = await initChildFrames(
     45    browser,
     46    "<input id='input'><br><br>"
     47  );
     48 
     49  gURLBar.focus();
     50 
     51  for (let stepNum = 0; stepNum < browsingContexts.length; stepNum++) {
     52    let useTab = stepNum > 0;
     53    await keyAndUpdate(useTab ? "VK_TAB" : "VK_F6", {}, 4);
     54 
     55    // Since focus may be switching into a separate process here,
     56    // need to wait for the focus to have been updated.
     57    await SpecialPowers.spawn(browsingContexts[stepNum], [], () => {
     58      return ContentTaskUtils.waitForCondition(
     59        () => content.browsingContext.isActive && content.document.hasFocus()
     60      );
     61    });
     62 
     63    // Force the UI to update on platforms that don't
     64    // normally do so until menus are opened.
     65    goUpdateGlobalEditMenuItems(true);
     66 
     67    await SpecialPowers.spawn(browsingContexts[stepNum], [{ useTab }], args => {
     68      // Both the tab key and document navigation with F6 will focus
     69      // the root of the document within the frame.
     70      // When dom.disable_tab_focus_to_root_element is true, only F6 will do this.
     71      let document = content.document;
     72      let expectedElement = !args.useTab
     73        ? document.documentElement
     74        : document.getElementById("input");
     75      Assert.equal(document.activeElement, expectedElement, "root focused");
     76    });
     77 
     78    if (!useTab) {
     79      // XXX Currently, Copy is always enabled when the root (not an editor element)
     80      // is focused. Possibly that should only be true if a listener is present?
     81      checkCommandState(
     82        "step " + stepNum + " root focused",
     83        false,
     84        true,
     85        false
     86      );
     87 
     88      // Tab to the textbox.
     89      await keyAndUpdate("VK_TAB", {}, 1);
     90    }
     91 
     92    goUpdateGlobalEditMenuItems(true);
     93 
     94    await SpecialPowers.spawn(browsingContexts[stepNum], [], () => {
     95      Assert.equal(
     96        content.document.activeElement,
     97        content.document.getElementById("input"),
     98        "input focused"
     99      );
    100    });
    101    checkCommandState(
    102      "step " + stepNum + " input focused",
    103      false,
    104      false,
    105      false
    106    );
    107 
    108    // Type into the textbox.
    109    await keyAndUpdate("a", {}, 1);
    110    checkCommandState("step " + stepNum + " typed", true, false, false);
    111 
    112    await SpecialPowers.spawn(browsingContexts[stepNum], [], () => {
    113      Assert.equal(
    114        content.document.activeElement,
    115        content.document.getElementById("input"),
    116        "input focused"
    117      );
    118    });
    119 
    120    // Select all text; this causes the Copy and Delete commands to be enabled.
    121    await keyAndUpdate("a", { accelKey: true }, 1);
    122    goUpdateGlobalEditMenuItems(true);
    123 
    124    checkCommandState("step " + stepNum + " selected", true, true, true);
    125 
    126    // Now make sure that the text is selected.
    127    await SpecialPowers.spawn(browsingContexts[stepNum], [], () => {
    128      let input = content.document.getElementById("input");
    129      Assert.equal(input.value, "a", "text matches");
    130      Assert.equal(input.selectionStart, 0, "selectionStart matches");
    131      Assert.equal(input.selectionEnd, 1, "selectionEnd matches");
    132    });
    133  }
    134 
    135  BrowserTestUtils.removeTab(tab);
    136 });