tor-browser

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

browser_fullscreen-window-open.js (8805B)


      1 /* eslint-disable mozilla/no-arbitrary-setTimeout */
      2 
      3 const PREF_DISABLE_OPEN_NEW_WINDOW =
      4  "browser.link.open_newwindow.disabled_in_fullscreen";
      5 const PREF_BLOCK_TOPLEVEL_DATA =
      6  "security.data_uri.block_toplevel_data_uri_navigations";
      7 const isOSX = Services.appinfo.OS === "Darwin";
      8 
      9 const TEST_FILE = "file_fullscreen-window-open.html";
     10 const gHttpTestRoot = getRootDirectory(gTestPath).replace(
     11  "chrome://mochitests/content/",
     12  "http://127.0.0.1:8888/"
     13 );
     14 
     15 var newWin;
     16 var newBrowser;
     17 
     18 async function test() {
     19  waitForExplicitFinish();
     20 
     21  Services.prefs.setBoolPref(PREF_DISABLE_OPEN_NEW_WINDOW, true);
     22  Services.prefs.setBoolPref(PREF_BLOCK_TOPLEVEL_DATA, false);
     23 
     24  newWin = await BrowserTestUtils.openNewBrowserWindow();
     25  newBrowser = newWin.gBrowser;
     26  await BrowserTestUtils.loadURIString({
     27    browser: newBrowser.selectedTab.linkedBrowser,
     28    uriString: gHttpTestRoot + TEST_FILE,
     29  });
     30 
     31  // Enter browser fullscreen mode.
     32  newWin.BrowserCommands.fullScreen();
     33 
     34  runNextTest();
     35 }
     36 
     37 registerCleanupFunction(async function () {
     38  // Exit browser fullscreen mode.
     39  newWin.BrowserCommands.fullScreen();
     40 
     41  await BrowserTestUtils.closeWindow(newWin);
     42 
     43  Services.prefs.clearUserPref(PREF_DISABLE_OPEN_NEW_WINDOW);
     44  Services.prefs.clearUserPref(PREF_BLOCK_TOPLEVEL_DATA);
     45 });
     46 
     47 var gTests = [
     48  test_open,
     49  test_open_with_size,
     50  test_open_with_pos,
     51  test_open_with_outerSize,
     52  test_open_with_innerSize,
     53  test_open_with_dialog,
     54  test_open_when_open_new_window_by_pref,
     55  test_open_with_pref_to_disable_in_fullscreen,
     56  test_open_from_chrome,
     57 ];
     58 
     59 function runNextTest() {
     60  let testCase = gTests.shift();
     61  if (testCase) {
     62    executeSoon(testCase);
     63  } else {
     64    finish();
     65  }
     66 }
     67 
     68 // Test for window.open() with no feature.
     69 function test_open() {
     70  waitForTabOpen({
     71    message: {
     72      title: "test_open",
     73      param: "",
     74    },
     75    finalizeFn() {},
     76  });
     77 }
     78 
     79 // Test for window.open() with width/height.
     80 function test_open_with_size() {
     81  waitForTabOpen({
     82    message: {
     83      title: "test_open_with_size",
     84      param: "width=400,height=400",
     85    },
     86    finalizeFn() {},
     87  });
     88 }
     89 
     90 // Test for window.open() with top/left.
     91 function test_open_with_pos() {
     92  waitForTabOpen({
     93    message: {
     94      title: "test_open_with_pos",
     95      param: "top=200,left=200",
     96    },
     97    finalizeFn() {},
     98  });
     99 }
    100 
    101 // Test for window.open() with outerWidth/Height.
    102 function test_open_with_outerSize() {
    103  let [outerWidth, outerHeight] = [newWin.outerWidth, newWin.outerHeight];
    104  waitForTabOpen({
    105    message: {
    106      title: "test_open_with_outerSize",
    107      param: "outerWidth=200,outerHeight=200",
    108    },
    109    successFn() {
    110      is(newWin.outerWidth, outerWidth, "Don't change window.outerWidth.");
    111      is(newWin.outerHeight, outerHeight, "Don't change window.outerHeight.");
    112    },
    113    finalizeFn() {},
    114  });
    115 }
    116 
    117 // Test for window.open() with innerWidth/Height.
    118 function test_open_with_innerSize() {
    119  let [innerWidth, innerHeight] = [newWin.innerWidth, newWin.innerHeight];
    120  waitForTabOpen({
    121    message: {
    122      title: "test_open_with_innerSize",
    123      param: "innerWidth=200,innerHeight=200",
    124    },
    125    successFn() {
    126      is(newWin.innerWidth, innerWidth, "Don't change window.innerWidth.");
    127      is(newWin.innerHeight, innerHeight, "Don't change window.innerHeight.");
    128    },
    129    finalizeFn() {},
    130  });
    131 }
    132 
    133 // Test for window.open() with dialog.
    134 function test_open_with_dialog() {
    135  waitForTabOpen({
    136    message: {
    137      title: "test_open_with_dialog",
    138      param: "dialog=yes",
    139    },
    140    finalizeFn() {},
    141  });
    142 }
    143 
    144 // Test for window.open()
    145 // when "browser.link.open_newwindow" is nsIBrowserDOMWindow.OPEN_NEWWINDOW
    146 function test_open_when_open_new_window_by_pref() {
    147  const PREF_NAME = "browser.link.open_newwindow";
    148  Services.prefs.setIntPref(PREF_NAME, Ci.nsIBrowserDOMWindow.OPEN_NEWWINDOW);
    149  is(
    150    Services.prefs.getIntPref(PREF_NAME),
    151    Ci.nsIBrowserDOMWindow.OPEN_NEWWINDOW,
    152    PREF_NAME + " is nsIBrowserDOMWindow.OPEN_NEWWINDOW at this time"
    153  );
    154 
    155  waitForTabOpen({
    156    message: {
    157      title: "test_open_when_open_new_window_by_pref",
    158      param: "width=400,height=400",
    159    },
    160    finalizeFn() {
    161      Services.prefs.clearUserPref(PREF_NAME);
    162    },
    163  });
    164 }
    165 
    166 // Test for the pref, "browser.link.open_newwindow.disabled_in_fullscreen"
    167 function test_open_with_pref_to_disable_in_fullscreen() {
    168  Services.prefs.setBoolPref(PREF_DISABLE_OPEN_NEW_WINDOW, false);
    169 
    170  waitForWindowOpen({
    171    message: {
    172      title: "test_open_with_pref_disabled_in_fullscreen",
    173      param: "width=400,height=400",
    174    },
    175    finalizeFn() {
    176      Services.prefs.setBoolPref(PREF_DISABLE_OPEN_NEW_WINDOW, true);
    177    },
    178  });
    179 }
    180 
    181 // Test for window.open() called from chrome context.
    182 function test_open_from_chrome() {
    183  waitForWindowOpenFromChrome({
    184    message: {
    185      title: "test_open_from_chrome",
    186      param: "",
    187      option: "noopener",
    188    },
    189    finalizeFn() {},
    190  });
    191 }
    192 
    193 function waitForTabOpen(aOptions) {
    194  let message = aOptions.message;
    195 
    196  if (!message.title) {
    197    ok(false, "Can't get message.title.");
    198    aOptions.finalizeFn();
    199    runNextTest();
    200    return;
    201  }
    202 
    203  info("Running test: " + message.title);
    204 
    205  BrowserTestUtils.waitForNewTab(newBrowser, null, true).then(tab => {
    206    is(
    207      tab.linkedBrowser.contentTitle,
    208      message.title,
    209      "Opened Tab is expected: " + message.title
    210    );
    211 
    212    if (aOptions.successFn) {
    213      aOptions.successFn();
    214    }
    215 
    216    newBrowser.removeTab(tab);
    217    finalize();
    218  });
    219 
    220  let finalize = function () {
    221    aOptions.finalizeFn();
    222    info("Finished: " + message.title);
    223    runNextTest();
    224  };
    225 
    226  const URI =
    227    "data:text/html;charset=utf-8,<!DOCTYPE html><html><head><title>" +
    228    message.title +
    229    "<%2Ftitle><%2Fhead><body><%2Fbody><%2Fhtml>";
    230 
    231  executeWindowOpenInContent({
    232    uri: URI,
    233    title: message.title,
    234    option: message.param,
    235  });
    236 }
    237 
    238 function waitForWindowOpen(aOptions) {
    239  let message = aOptions.message;
    240  let url = aOptions.url || "about:blank";
    241 
    242  if (!message.title) {
    243    ok(false, "Can't get message.title");
    244    aOptions.finalizeFn();
    245    runNextTest();
    246    return;
    247  }
    248 
    249  info("Running test: " + message.title);
    250 
    251  let onFinalize = function () {
    252    aOptions.finalizeFn();
    253 
    254    info("Finished: " + message.title);
    255    runNextTest();
    256  };
    257 
    258  let listener = new WindowListener(
    259    message.title,
    260    AppConstants.BROWSER_CHROME_URL,
    261    {
    262      onSuccess: aOptions.successFn,
    263      onFinalize,
    264    }
    265  );
    266  Services.wm.addListener(listener);
    267 
    268  executeWindowOpenInContent({
    269    uri: url,
    270    title: message.title,
    271    option: message.param,
    272  });
    273 }
    274 
    275 function executeWindowOpenInContent(aParam) {
    276  SpecialPowers.spawn(
    277    newBrowser.selectedBrowser,
    278    [JSON.stringify(aParam)],
    279    async function (dataTestParam) {
    280      let testElm = content.document.getElementById("test");
    281      testElm.setAttribute("data-test-param", dataTestParam);
    282      testElm.click();
    283    }
    284  );
    285 }
    286 
    287 function waitForWindowOpenFromChrome(aOptions) {
    288  let message = aOptions.message;
    289  let url = aOptions.url || "about:blank";
    290 
    291  if (!message.title) {
    292    ok(false, "Can't get message.title");
    293    aOptions.finalizeFn();
    294    runNextTest();
    295    return;
    296  }
    297 
    298  info("Running test: " + message.title);
    299 
    300  let onFinalize = function () {
    301    aOptions.finalizeFn();
    302 
    303    info("Finished: " + message.title);
    304    runNextTest();
    305  };
    306 
    307  let listener = new WindowListener(
    308    message.title,
    309    AppConstants.BROWSER_CHROME_URL,
    310    {
    311      onSuccess: aOptions.successFn,
    312      onFinalize,
    313    }
    314  );
    315  Services.wm.addListener(listener);
    316 
    317  newWin.open(url, message.title, message.option);
    318 }
    319 
    320 function WindowListener(aTitle, aUrl, aCallBackObj) {
    321  this.test_title = aTitle;
    322  this.test_url = aUrl;
    323  this.callback_onSuccess = aCallBackObj.onSuccess;
    324  this.callBack_onFinalize = aCallBackObj.onFinalize;
    325 }
    326 WindowListener.prototype = {
    327  test_title: null,
    328  test_url: null,
    329  callback_onSuccess: null,
    330  callBack_onFinalize: null,
    331 
    332  onOpenWindow(aXULWindow) {
    333    Services.wm.removeListener(this);
    334 
    335    let domwindow = aXULWindow.docShell.domWindow;
    336    let onLoad = () => {
    337      is(
    338        domwindow.document.location.href,
    339        this.test_url,
    340        "Opened Window is expected: " + this.test_title
    341      );
    342      if (this.callback_onSuccess) {
    343        this.callback_onSuccess();
    344      }
    345 
    346      domwindow.removeEventListener("load", onLoad, true);
    347 
    348      // wait for trasition to fullscreen on OSX Lion later
    349      if (isOSX) {
    350        setTimeout(() => {
    351          domwindow.close();
    352          executeSoon(this.callBack_onFinalize);
    353        }, 3000);
    354      } else {
    355        domwindow.close();
    356        executeSoon(this.callBack_onFinalize);
    357      }
    358    };
    359    domwindow.addEventListener("load", onLoad, true);
    360  },
    361  onCloseWindow() {},
    362  QueryInterface: ChromeUtils.generateQI(["nsIWindowMediatorListener"]),
    363 };