tor-browser

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

browser_fullscreen-contextmenu-esc.js (3909B)


      1 "use strict";
      2 
      3 function captureUnexpectedFullscreenChange() {
      4  ok(false, "Caught an unexpected fullscreen change");
      5 }
      6 
      7 const kPage =
      8  "https://example.org/browser/dom/base/test/fullscreen/dummy_page.html";
      9 
     10 function waitForDocActivated(aBrowser) {
     11  return SpecialPowers.spawn(aBrowser, [], () => {
     12    return ContentTaskUtils.waitForCondition(
     13      () => content.browsingContext.isActive && content.document.hasFocus()
     14    );
     15  });
     16 }
     17 
     18 add_task(async function () {
     19  await pushPrefs(
     20    ["full-screen-api.transition-duration.enter", "0 0"],
     21    ["full-screen-api.transition-duration.leave", "0 0"]
     22  );
     23 
     24  let tab = await BrowserTestUtils.openNewForegroundTab({
     25    gBrowser,
     26    opening: kPage,
     27    waitForStateStop: true,
     28  });
     29  let browser = tab.linkedBrowser;
     30 
     31  // As requestFullscreen checks the active state of the docshell,
     32  // wait for the document to be activated, just to be sure that
     33  // the fullscreen request won't be denied.
     34  await SpecialPowers.spawn(browser, [], () => {
     35    return ContentTaskUtils.waitForCondition(
     36      () => content.browsingContext.isActive && content.document.hasFocus()
     37    );
     38  });
     39 
     40  let contextMenu = document.getElementById("contentAreaContextMenu");
     41  ok(contextMenu, "Got context menu");
     42 
     43  let state;
     44  info("Enter DOM fullscreen");
     45  let fullScreenChangedPromise = BrowserTestUtils.waitForContentEvent(
     46    browser,
     47    "fullscreenchange"
     48  );
     49  await SpecialPowers.spawn(browser, [], () => {
     50    content.document.body.requestFullscreen();
     51  });
     52 
     53  await fullScreenChangedPromise;
     54  state = await SpecialPowers.spawn(browser, [], () => {
     55    return !!content.document.fullscreenElement;
     56  });
     57  ok(state, "The content should have entered fullscreen");
     58  ok(document.fullscreenElement, "The chrome should also be in fullscreen");
     59 
     60  let removeContentEventListener = BrowserTestUtils.addContentEventListener(
     61    browser,
     62    "fullscreenchange",
     63    captureUnexpectedFullscreenChange
     64  );
     65 
     66  info("Open context menu");
     67  is(contextMenu.state, "closed", "Should not have opened context menu");
     68 
     69  let popupShownPromise = promiseWaitForEvent(window, "popupshown");
     70 
     71  EventUtils.synthesizeMouse(
     72    browser,
     73    screen.width / 2,
     74    screen.height / 2,
     75    { type: "contextmenu", button: 2 },
     76    window
     77  );
     78  await popupShownPromise;
     79  is(contextMenu.state, "open", "Should have opened context menu");
     80 
     81  let popupHidePromise = promiseWaitForEvent(window, "popuphidden");
     82 
     83  if (
     84    !AppConstants.platform == "macosx" ||
     85    !Services.prefs.getBoolPref("widget.macos.native-context-menus", false)
     86  ) {
     87    info("Send the first escape");
     88    EventUtils.synthesizeKey("KEY_Escape");
     89  } else {
     90    // We cannot synthesize key events at native macOS menus.
     91    // We also do not see key events that are processed by native macOS menus,
     92    // so we cannot accidentally exit fullscreen when the user closes a native
     93    // menu with Escape.
     94    // Close the menu normally.
     95    info("Close the context menu");
     96    contextMenu.hidePopup();
     97  }
     98  await popupHidePromise;
     99  is(contextMenu.state, "closed", "Should have closed context menu");
    100 
    101  // Wait a small time to confirm that the first ESC key
    102  // does not exit fullscreen.
    103  // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
    104  await new Promise(resolve => setTimeout(resolve, 1000));
    105  state = await SpecialPowers.spawn(browser, [], () => {
    106    return !!content.document.fullscreenElement;
    107  });
    108  ok(state, "The content should still be in fullscreen");
    109  ok(document.fullscreenElement, "The chrome should still be in fullscreen");
    110 
    111  removeContentEventListener();
    112  info("Send the second escape");
    113  let fullscreenExitPromise = BrowserTestUtils.waitForContentEvent(
    114    browser,
    115    "fullscreenchange"
    116  );
    117  EventUtils.synthesizeKey("KEY_Escape");
    118  await fullscreenExitPromise;
    119  ok(!document.fullscreenElement, "The chrome should have exited fullscreen");
    120 
    121  gBrowser.removeTab(tab);
    122 });