browser/base/content/test/general/browser_fullscreen-window-open.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
     2 Components.utils.import("resource://gre/modules/Services.jsm");
     4 let Cc = Components.classes;
     5 let Ci = Components.interfaces;
     7 const PREF_DISABLE_OPEN_NEW_WINDOW = "browser.link.open_newwindow.disabled_in_fullscreen";
     8 const isOSX = (Services.appinfo.OS === "Darwin");
    10 const TEST_FILE = "file_fullscreen-window-open.html";
    11 const gHttpTestRoot = getRootDirectory(gTestPath).replace("chrome://mochitests/content/",
    12                                                           "http://127.0.0.1:8888/");
    14 function test () {
    15   waitForExplicitFinish();
    17   Services.prefs.setBoolPref(PREF_DISABLE_OPEN_NEW_WINDOW, true);
    19   let newTab = gBrowser.addTab();
    20   gBrowser.selectedTab = newTab;
    22   let gTestBrowser = gBrowser.selectedBrowser;
    23   gTestBrowser.addEventListener("load", function onLoad(){
    24     gTestBrowser.removeEventListener("load", onLoad, true, true);
    26     // Enter browser fullscreen mode.
    27     BrowserFullScreen();
    29     runNextTest();
    30   }, true, true);
    31   gTestBrowser.contentWindow.location.href = gHttpTestRoot + TEST_FILE;
    32 }
    34 registerCleanupFunction(function(){
    35   // Exit browser fullscreen mode.
    36   BrowserFullScreen();
    38   gBrowser.removeCurrentTab();
    40   Services.prefs.clearUserPref(PREF_DISABLE_OPEN_NEW_WINDOW);
    41 });
    43 let gTests = [
    44   test_open,
    45   test_open_with_size,
    46   test_open_with_pos,
    47   test_open_with_outerSize,
    48   test_open_with_innerSize,
    49   test_open_with_dialog,
    50   test_open_when_open_new_window_by_pref,
    51   test_open_with_pref_to_disable_in_fullscreen,
    52   test_open_from_chrome,
    53 ];
    55 function runNextTest () {
    56   let test = gTests.shift();
    57   if (test) {
    58     executeSoon(test);
    59   }
    60   else {
    61     finish();
    62   }
    63 }
    66 // Test for window.open() with no feature.
    67 function test_open() {
    68   waitForTabOpen({
    69     message: {
    70       title: "test_open",
    71       param: "",
    72     },
    73     finalizeFn: function () {},
    74   });
    75 }
    77 // Test for window.open() with width/height.
    78 function test_open_with_size() {
    79   waitForTabOpen({
    80     message: {
    81       title: "test_open_with_size",
    82       param: "width=400,height=400",
    83     },
    84     finalizeFn: function () {},
    85   });
    86 }
    88 // Test for window.open() with top/left.
    89 function test_open_with_pos() {
    90   waitForTabOpen({
    91     message: {
    92       title: "test_open_with_pos",
    93       param: "top=200,left=200",
    94     },
    95     finalizeFn: function () {},
    96   });
    97 }
    99 // Test for window.open() with outerWidth/Height.
   100 function test_open_with_outerSize() {
   101   let [outerWidth, outerHeight] = [window.outerWidth, window.outerHeight];
   102   waitForTabOpen({
   103     message: {
   104       title: "test_open_with_outerSize",
   105       param: "outerWidth=200,outerHeight=200",
   106     },
   107     successFn: function () {
   108       is(window.outerWidth, outerWidth, "Don't change window.outerWidth.");
   109       is(window.outerHeight, outerHeight, "Don't change window.outerHeight.");
   110     },
   111     finalizeFn: function () {},
   112   });
   113 }
   115 // Test for window.open() with innerWidth/Height.
   116 function test_open_with_innerSize() {
   117   let [innerWidth, innerHeight] = [window.innerWidth, window.innerHeight];
   118   waitForTabOpen({
   119     message: {
   120       title: "test_open_with_innerSize",
   121       param: "innerWidth=200,innerHeight=200",
   122     },
   123     successFn: function () {
   124       is(window.innerWidth, innerWidth, "Don't change window.innerWidth.");
   125       is(window.innerHeight, innerHeight, "Don't change window.innerHeight.");
   126     },
   127     finalizeFn: function () {},
   128   });
   129 }
   131 // Test for window.open() with dialog.
   132 function test_open_with_dialog() {
   133   waitForTabOpen({
   134     message: {
   135       title: "test_open_with_dialog",
   136       param: "dialog=yes",
   137     },
   138     finalizeFn: function () {},
   139   });
   140 }
   142 // Test for window.open()
   143 // when "browser.link.open_newwindow" is nsIBrowserDOMWindow.OPEN_NEWWINDOW
   144 function test_open_when_open_new_window_by_pref() {
   145   const PREF_NAME = "browser.link.open_newwindow";
   146   Services.prefs.setIntPref(PREF_NAME, Ci.nsIBrowserDOMWindow.OPEN_NEWWINDOW);
   147   is(Services.prefs.getIntPref(PREF_NAME), Ci.nsIBrowserDOMWindow.OPEN_NEWWINDOW,
   148      PREF_NAME + " is nsIBrowserDOMWindow.OPEN_NEWWINDOW at this time");
   150   waitForTabOpen({
   151     message: {
   152       title: "test_open_when_open_new_window_by_pref",
   153       param: "width=400,height=400",
   154     },
   155     finalizeFn: function () {
   156       Services.prefs.clearUserPref(PREF_NAME);
   157     },
   158   });
   159 }
   161 // Test for the pref, "browser.link.open_newwindow.disabled_in_fullscreen"
   162 function test_open_with_pref_to_disable_in_fullscreen() {
   163   Services.prefs.setBoolPref(PREF_DISABLE_OPEN_NEW_WINDOW, false);
   165   waitForWindowOpen({
   166     message: {
   167       title: "test_open_with_pref_disabled_in_fullscreen",
   168       param: "width=400,height=400",
   169     },
   170     finalizeFn: function () {
   171       Services.prefs.setBoolPref(PREF_DISABLE_OPEN_NEW_WINDOW, true);
   172     },
   173   });
   174 }
   177 // Test for window.open() called from chrome context.
   178 function test_open_from_chrome() {
   179   waitForWindowOpenFromChrome({
   180     message: {
   181       title: "test_open_from_chrome",
   182       param: "",
   183     },
   184     finalizeFn: function () {},
   185     timeout: 10000,
   186   });
   187 }
   189 function waitForTabOpen(aOptions) {
   190   let start = Date.now();
   191   let timeout = aOptions.timeout || 5000;
   192   let message = aOptions.message;
   194   if (!message.title) {
   195     ok(false, "Can't get message.title.");
   196     aOptions.finalizeFn();
   197     runNextTest();
   198     return;
   199   }
   201   info("Running test: " + message.title);
   203   let onTabOpen = function onTabOpen(aEvent) {
   204     gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen, true);
   206     let tab = aEvent.target;
   207     tab.linkedBrowser.addEventListener("load", function onLoad(ev){
   208       let browser = ev.currentTarget;
   209       browser.removeEventListener("load", onLoad, true, true);
   210       clearTimeout(onTimeout);
   212       is(browser.contentWindow.document.title, message.title,
   213          "Opened Tab is expected: " + message.title);
   215       if (aOptions.successFn) {
   216         aOptions.successFn();
   217       }
   219       gBrowser.removeTab(tab);
   220       finalize();
   221     }, true, true);
   222   }
   223   gBrowser.tabContainer.addEventListener("TabOpen", onTabOpen, true);
   225   let finalize = function () {
   226     aOptions.finalizeFn();
   227     info("Finished: " + message.title);
   228     runNextTest();
   229   };
   231   let onTimeout = setTimeout(function(){
   232     gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen, true);
   234     ok(false, "Timeout: '"+message.title + "'.");
   235     finalize();
   236   }, timeout);
   239   const URI = "data:text/html;charset=utf-8,<!DOCTYPE html><html><head><title>"+
   240               message.title +
   241               "<%2Ftitle><%2Fhead><body><%2Fbody><%2Fhtml>";
   243   executeWindowOpenInContent({
   244     uri: URI,
   245     title: message.title,
   246     option: message.param,
   247   });
   248 }
   251 function waitForWindowOpen(aOptions) {
   252   let start = Date.now();
   253   let timeout = aOptions.timeout || 10000;
   254   let message = aOptions.message;
   255   let url = aOptions.url || getBrowserURL();
   257   if (!message.title) {
   258     ok(false, "Can't get message.title");
   259     aOptions.finalizeFn();
   260     runNextTest();
   261     return;
   262   }
   264   info("Running test: " + message.title);
   266   let onFinalize = function () {
   267     aOptions.finalizeFn();
   269     info("Finished: " + message.title);
   270     runNextTest();
   271   };
   273   let onTimeout = setTimeout(function(){
   274     Services.wm.removeListener(listener);
   275     ok(false, "Fail: '"+message.title + "'.");
   277     onFinalize();
   278   }, timeout);
   280   let listener = new WindowListener(message.title, url, {
   281     onSuccess: aOptions.successFn,
   282     onTimeout: onTimeout,
   283     onFinalize: onFinalize,
   284   });
   285   Services.wm.addListener(listener);
   287   const URI = aOptions.url || "about:blank";
   289   executeWindowOpenInContent({
   290     uri: URI,
   291     title: message.title,
   292     option: message.param,
   293   });
   294 }
   296 function executeWindowOpenInContent(aParam) {
   297   var testWindow = gBrowser.selectedBrowser.contentWindow;
   298   var testElm = testWindow.document.getElementById("test");
   300   testElm.setAttribute("data-test-param", JSON.stringify(aParam));
   301   EventUtils.synthesizeMouseAtCenter(testElm, {}, testWindow);
   302 }
   304 function waitForWindowOpenFromChrome(aOptions) {
   305   let start = Date.now();
   306   let timeout = aOptions.timeout || 10000;
   307   let message = aOptions.message;
   308   let url = aOptions.url || getBrowserURL();
   310   if (!message.title) {
   311     ok(false, "Can't get message.title");
   312     aOptions.finalizeFn();
   313     runNextTest();
   314     return;
   315   }
   317   info("Running test: " + message.title);
   319   let onFinalize = function () {
   320     aOptions.finalizeFn();
   322     info("Finished: " + message.title);
   323     runNextTest();
   324   };
   326   let onTimeout = setTimeout(function(){
   327     Services.wm.removeListener(listener);
   328     ok(false, "Fail: '"+message.title + "'.");
   330     testWindow.close();
   331     onFinalize();
   332   }, timeout);
   334   let listener = new WindowListener(message.title, url, {
   335     onSuccess: aOptions.successFn,
   336     onTimeout: onTimeout,
   337     onFinalize: onFinalize,
   338   });
   339   Services.wm.addListener(listener);
   342   const URI = aOptions.url || "about:blank";
   344   let testWindow = window.open(URI, message.title, message.option);
   345 }
   347 function WindowListener(aTitle, aUrl, aCallBackObj) {
   348   this.test_title = aTitle;
   349   this.test_url = aUrl;
   350   this.callback_onSuccess = aCallBackObj.onSuccess;
   351   this.callBack_onTimeout = aCallBackObj.onTimeout;
   352   this.callBack_onFinalize = aCallBackObj.onFinalize;
   353 }
   354 WindowListener.prototype = {
   356   test_title: null,
   357   test_url: null,
   358   callback_onSuccess: null,
   359   callBack_onTimeout: null,
   360   callBack_onFinalize: null,
   362   onOpenWindow: function(aXULWindow) {
   363     Services.wm.removeListener(this);
   365     let domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor)
   366                     .getInterface(Ci.nsIDOMWindow);
   367     domwindow.addEventListener("load", function onLoad(aEvent) {
   368       is(domwindow.document.location.href, this.test_url,
   369         "Opened Window is expected: "+ this.test_title);
   370       if (this.callback_onSuccess) {
   371         this.callback_onSuccess();
   372       }
   374       domwindow.removeEventListener("load", onLoad, true);
   375       clearTimeout(this.callBack_onTimeout);
   377       // wait for trasition to fullscreen on OSX Lion later
   378       if (isOSX) {
   379         setTimeout(function(){
   380           domwindow.close();
   381           executeSoon(this.callBack_onFinalize);
   382         }.bind(this), 3000);
   383       }
   384       else {
   385         domwindow.close();
   386         executeSoon(this.callBack_onFinalize);
   387       }
   388     }.bind(this), true);
   389   },
   390   onCloseWindow: function(aXULWindow) {},
   391   onWindowTitleChange: function(aXULWindow, aNewTitle) {},
   392   QueryInterface: XPCOMUtils.generateQI([Ci.nsIWindowMediatorListener,
   393                                          Ci.nsISupports]),
   394 };

mercurial