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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/base/content/test/general/browser_fullscreen-window-open.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,394 @@
     1.4 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
     1.5 +Components.utils.import("resource://gre/modules/Services.jsm");
     1.6 +
     1.7 +let Cc = Components.classes;
     1.8 +let Ci = Components.interfaces;
     1.9 +
    1.10 +const PREF_DISABLE_OPEN_NEW_WINDOW = "browser.link.open_newwindow.disabled_in_fullscreen";
    1.11 +const isOSX = (Services.appinfo.OS === "Darwin");
    1.12 +
    1.13 +const TEST_FILE = "file_fullscreen-window-open.html";
    1.14 +const gHttpTestRoot = getRootDirectory(gTestPath).replace("chrome://mochitests/content/",
    1.15 +                                                          "http://127.0.0.1:8888/");
    1.16 +
    1.17 +function test () {
    1.18 +  waitForExplicitFinish();
    1.19 +
    1.20 +  Services.prefs.setBoolPref(PREF_DISABLE_OPEN_NEW_WINDOW, true);
    1.21 +
    1.22 +  let newTab = gBrowser.addTab();
    1.23 +  gBrowser.selectedTab = newTab;
    1.24 +
    1.25 +  let gTestBrowser = gBrowser.selectedBrowser;
    1.26 +  gTestBrowser.addEventListener("load", function onLoad(){
    1.27 +    gTestBrowser.removeEventListener("load", onLoad, true, true);
    1.28 +
    1.29 +    // Enter browser fullscreen mode.
    1.30 +    BrowserFullScreen();
    1.31 +
    1.32 +    runNextTest();
    1.33 +  }, true, true);
    1.34 +  gTestBrowser.contentWindow.location.href = gHttpTestRoot + TEST_FILE;
    1.35 +}
    1.36 +
    1.37 +registerCleanupFunction(function(){
    1.38 +  // Exit browser fullscreen mode.
    1.39 +  BrowserFullScreen();
    1.40 +
    1.41 +  gBrowser.removeCurrentTab();
    1.42 +
    1.43 +  Services.prefs.clearUserPref(PREF_DISABLE_OPEN_NEW_WINDOW);
    1.44 +});
    1.45 +
    1.46 +let gTests = [
    1.47 +  test_open,
    1.48 +  test_open_with_size,
    1.49 +  test_open_with_pos,
    1.50 +  test_open_with_outerSize,
    1.51 +  test_open_with_innerSize,
    1.52 +  test_open_with_dialog,
    1.53 +  test_open_when_open_new_window_by_pref,
    1.54 +  test_open_with_pref_to_disable_in_fullscreen,
    1.55 +  test_open_from_chrome,
    1.56 +];
    1.57 +
    1.58 +function runNextTest () {
    1.59 +  let test = gTests.shift();
    1.60 +  if (test) {
    1.61 +    executeSoon(test);
    1.62 +  }
    1.63 +  else {
    1.64 +    finish();
    1.65 +  }
    1.66 +}
    1.67 +
    1.68 +
    1.69 +// Test for window.open() with no feature.
    1.70 +function test_open() {
    1.71 +  waitForTabOpen({
    1.72 +    message: {
    1.73 +      title: "test_open",
    1.74 +      param: "",
    1.75 +    },
    1.76 +    finalizeFn: function () {},
    1.77 +  });
    1.78 +}
    1.79 +
    1.80 +// Test for window.open() with width/height.
    1.81 +function test_open_with_size() {
    1.82 +  waitForTabOpen({
    1.83 +    message: {
    1.84 +      title: "test_open_with_size",
    1.85 +      param: "width=400,height=400",
    1.86 +    },
    1.87 +    finalizeFn: function () {},
    1.88 +  });
    1.89 +}
    1.90 +
    1.91 +// Test for window.open() with top/left.
    1.92 +function test_open_with_pos() {
    1.93 +  waitForTabOpen({
    1.94 +    message: {
    1.95 +      title: "test_open_with_pos",
    1.96 +      param: "top=200,left=200",
    1.97 +    },
    1.98 +    finalizeFn: function () {},
    1.99 +  });
   1.100 +}
   1.101 +
   1.102 +// Test for window.open() with outerWidth/Height.
   1.103 +function test_open_with_outerSize() {
   1.104 +  let [outerWidth, outerHeight] = [window.outerWidth, window.outerHeight];
   1.105 +  waitForTabOpen({
   1.106 +    message: {
   1.107 +      title: "test_open_with_outerSize",
   1.108 +      param: "outerWidth=200,outerHeight=200",
   1.109 +    },
   1.110 +    successFn: function () {
   1.111 +      is(window.outerWidth, outerWidth, "Don't change window.outerWidth.");
   1.112 +      is(window.outerHeight, outerHeight, "Don't change window.outerHeight.");
   1.113 +    },
   1.114 +    finalizeFn: function () {},
   1.115 +  });
   1.116 +}
   1.117 +
   1.118 +// Test for window.open() with innerWidth/Height.
   1.119 +function test_open_with_innerSize() {
   1.120 +  let [innerWidth, innerHeight] = [window.innerWidth, window.innerHeight];
   1.121 +  waitForTabOpen({
   1.122 +    message: {
   1.123 +      title: "test_open_with_innerSize",
   1.124 +      param: "innerWidth=200,innerHeight=200",
   1.125 +    },
   1.126 +    successFn: function () {
   1.127 +      is(window.innerWidth, innerWidth, "Don't change window.innerWidth.");
   1.128 +      is(window.innerHeight, innerHeight, "Don't change window.innerHeight.");
   1.129 +    },
   1.130 +    finalizeFn: function () {},
   1.131 +  });
   1.132 +}
   1.133 +
   1.134 +// Test for window.open() with dialog.
   1.135 +function test_open_with_dialog() {
   1.136 +  waitForTabOpen({
   1.137 +    message: {
   1.138 +      title: "test_open_with_dialog",
   1.139 +      param: "dialog=yes",
   1.140 +    },
   1.141 +    finalizeFn: function () {},
   1.142 +  });
   1.143 +}
   1.144 +
   1.145 +// Test for window.open()
   1.146 +// when "browser.link.open_newwindow" is nsIBrowserDOMWindow.OPEN_NEWWINDOW
   1.147 +function test_open_when_open_new_window_by_pref() {
   1.148 +  const PREF_NAME = "browser.link.open_newwindow";
   1.149 +  Services.prefs.setIntPref(PREF_NAME, Ci.nsIBrowserDOMWindow.OPEN_NEWWINDOW);
   1.150 +  is(Services.prefs.getIntPref(PREF_NAME), Ci.nsIBrowserDOMWindow.OPEN_NEWWINDOW,
   1.151 +     PREF_NAME + " is nsIBrowserDOMWindow.OPEN_NEWWINDOW at this time");
   1.152 +
   1.153 +  waitForTabOpen({
   1.154 +    message: {
   1.155 +      title: "test_open_when_open_new_window_by_pref",
   1.156 +      param: "width=400,height=400",
   1.157 +    },
   1.158 +    finalizeFn: function () {
   1.159 +      Services.prefs.clearUserPref(PREF_NAME);
   1.160 +    },
   1.161 +  });
   1.162 +}
   1.163 +
   1.164 +// Test for the pref, "browser.link.open_newwindow.disabled_in_fullscreen"
   1.165 +function test_open_with_pref_to_disable_in_fullscreen() {
   1.166 +  Services.prefs.setBoolPref(PREF_DISABLE_OPEN_NEW_WINDOW, false);
   1.167 +
   1.168 +  waitForWindowOpen({
   1.169 +    message: {
   1.170 +      title: "test_open_with_pref_disabled_in_fullscreen",
   1.171 +      param: "width=400,height=400",
   1.172 +    },
   1.173 +    finalizeFn: function () {
   1.174 +      Services.prefs.setBoolPref(PREF_DISABLE_OPEN_NEW_WINDOW, true);
   1.175 +    },
   1.176 +  });
   1.177 +}
   1.178 +
   1.179 +
   1.180 +// Test for window.open() called from chrome context.
   1.181 +function test_open_from_chrome() {
   1.182 +  waitForWindowOpenFromChrome({
   1.183 +    message: {
   1.184 +      title: "test_open_from_chrome",
   1.185 +      param: "",
   1.186 +    },
   1.187 +    finalizeFn: function () {},
   1.188 +    timeout: 10000,
   1.189 +  });
   1.190 +}
   1.191 +
   1.192 +function waitForTabOpen(aOptions) {
   1.193 +  let start = Date.now();
   1.194 +  let timeout = aOptions.timeout || 5000;
   1.195 +  let message = aOptions.message;
   1.196 +
   1.197 +  if (!message.title) {
   1.198 +    ok(false, "Can't get message.title.");
   1.199 +    aOptions.finalizeFn();
   1.200 +    runNextTest();
   1.201 +    return;
   1.202 +  }
   1.203 +
   1.204 +  info("Running test: " + message.title);
   1.205 +
   1.206 +  let onTabOpen = function onTabOpen(aEvent) {
   1.207 +    gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen, true);
   1.208 +
   1.209 +    let tab = aEvent.target;
   1.210 +    tab.linkedBrowser.addEventListener("load", function onLoad(ev){
   1.211 +      let browser = ev.currentTarget;
   1.212 +      browser.removeEventListener("load", onLoad, true, true);
   1.213 +      clearTimeout(onTimeout);
   1.214 +
   1.215 +      is(browser.contentWindow.document.title, message.title,
   1.216 +         "Opened Tab is expected: " + message.title);
   1.217 +
   1.218 +      if (aOptions.successFn) {
   1.219 +        aOptions.successFn();
   1.220 +      }
   1.221 +
   1.222 +      gBrowser.removeTab(tab);
   1.223 +      finalize();
   1.224 +    }, true, true);
   1.225 +  }
   1.226 +  gBrowser.tabContainer.addEventListener("TabOpen", onTabOpen, true);
   1.227 +
   1.228 +  let finalize = function () {
   1.229 +    aOptions.finalizeFn();
   1.230 +    info("Finished: " + message.title);
   1.231 +    runNextTest();
   1.232 +  };
   1.233 +
   1.234 +  let onTimeout = setTimeout(function(){
   1.235 +    gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen, true);
   1.236 +
   1.237 +    ok(false, "Timeout: '"+message.title + "'.");
   1.238 +    finalize();
   1.239 +  }, timeout);
   1.240 +
   1.241 +
   1.242 +  const URI = "data:text/html;charset=utf-8,<!DOCTYPE html><html><head><title>"+
   1.243 +              message.title +
   1.244 +              "<%2Ftitle><%2Fhead><body><%2Fbody><%2Fhtml>";
   1.245 +
   1.246 +  executeWindowOpenInContent({
   1.247 +    uri: URI,
   1.248 +    title: message.title,
   1.249 +    option: message.param,
   1.250 +  });
   1.251 +}
   1.252 +
   1.253 +
   1.254 +function waitForWindowOpen(aOptions) {
   1.255 +  let start = Date.now();
   1.256 +  let timeout = aOptions.timeout || 10000;
   1.257 +  let message = aOptions.message;
   1.258 +  let url = aOptions.url || getBrowserURL();
   1.259 +
   1.260 +  if (!message.title) {
   1.261 +    ok(false, "Can't get message.title");
   1.262 +    aOptions.finalizeFn();
   1.263 +    runNextTest();
   1.264 +    return;
   1.265 +  }
   1.266 +
   1.267 +  info("Running test: " + message.title);
   1.268 +
   1.269 +  let onFinalize = function () {
   1.270 +    aOptions.finalizeFn();
   1.271 +
   1.272 +    info("Finished: " + message.title);
   1.273 +    runNextTest();
   1.274 +  };
   1.275 +
   1.276 +  let onTimeout = setTimeout(function(){
   1.277 +    Services.wm.removeListener(listener);
   1.278 +    ok(false, "Fail: '"+message.title + "'.");
   1.279 +
   1.280 +    onFinalize();
   1.281 +  }, timeout);
   1.282 +
   1.283 +  let listener = new WindowListener(message.title, url, {
   1.284 +    onSuccess: aOptions.successFn,
   1.285 +    onTimeout: onTimeout,
   1.286 +    onFinalize: onFinalize,
   1.287 +  });
   1.288 +  Services.wm.addListener(listener);
   1.289 +
   1.290 +  const URI = aOptions.url || "about:blank";
   1.291 +
   1.292 +  executeWindowOpenInContent({
   1.293 +    uri: URI,
   1.294 +    title: message.title,
   1.295 +    option: message.param,
   1.296 +  });
   1.297 +}
   1.298 +
   1.299 +function executeWindowOpenInContent(aParam) {
   1.300 +  var testWindow = gBrowser.selectedBrowser.contentWindow;
   1.301 +  var testElm = testWindow.document.getElementById("test");
   1.302 +
   1.303 +  testElm.setAttribute("data-test-param", JSON.stringify(aParam));
   1.304 +  EventUtils.synthesizeMouseAtCenter(testElm, {}, testWindow);
   1.305 +}
   1.306 +
   1.307 +function waitForWindowOpenFromChrome(aOptions) {
   1.308 +  let start = Date.now();
   1.309 +  let timeout = aOptions.timeout || 10000;
   1.310 +  let message = aOptions.message;
   1.311 +  let url = aOptions.url || getBrowserURL();
   1.312 +
   1.313 +  if (!message.title) {
   1.314 +    ok(false, "Can't get message.title");
   1.315 +    aOptions.finalizeFn();
   1.316 +    runNextTest();
   1.317 +    return;
   1.318 +  }
   1.319 +
   1.320 +  info("Running test: " + message.title);
   1.321 +
   1.322 +  let onFinalize = function () {
   1.323 +    aOptions.finalizeFn();
   1.324 +
   1.325 +    info("Finished: " + message.title);
   1.326 +    runNextTest();
   1.327 +  };
   1.328 +
   1.329 +  let onTimeout = setTimeout(function(){
   1.330 +    Services.wm.removeListener(listener);
   1.331 +    ok(false, "Fail: '"+message.title + "'.");
   1.332 +
   1.333 +    testWindow.close();
   1.334 +    onFinalize();
   1.335 +  }, timeout);
   1.336 +
   1.337 +  let listener = new WindowListener(message.title, url, {
   1.338 +    onSuccess: aOptions.successFn,
   1.339 +    onTimeout: onTimeout,
   1.340 +    onFinalize: onFinalize,
   1.341 +  });
   1.342 +  Services.wm.addListener(listener);
   1.343 +
   1.344 +
   1.345 +  const URI = aOptions.url || "about:blank";
   1.346 +
   1.347 +  let testWindow = window.open(URI, message.title, message.option);
   1.348 +}
   1.349 +
   1.350 +function WindowListener(aTitle, aUrl, aCallBackObj) {
   1.351 +  this.test_title = aTitle;
   1.352 +  this.test_url = aUrl;
   1.353 +  this.callback_onSuccess = aCallBackObj.onSuccess;
   1.354 +  this.callBack_onTimeout = aCallBackObj.onTimeout;
   1.355 +  this.callBack_onFinalize = aCallBackObj.onFinalize;
   1.356 +}
   1.357 +WindowListener.prototype = {
   1.358 +
   1.359 +  test_title: null,
   1.360 +  test_url: null,
   1.361 +  callback_onSuccess: null,
   1.362 +  callBack_onTimeout: null,
   1.363 +  callBack_onFinalize: null,
   1.364 +
   1.365 +  onOpenWindow: function(aXULWindow) {
   1.366 +    Services.wm.removeListener(this);
   1.367 +
   1.368 +    let domwindow = aXULWindow.QueryInterface(Ci.nsIInterfaceRequestor)
   1.369 +                    .getInterface(Ci.nsIDOMWindow);
   1.370 +    domwindow.addEventListener("load", function onLoad(aEvent) {
   1.371 +      is(domwindow.document.location.href, this.test_url,
   1.372 +        "Opened Window is expected: "+ this.test_title);
   1.373 +      if (this.callback_onSuccess) {
   1.374 +        this.callback_onSuccess();
   1.375 +      }
   1.376 +
   1.377 +      domwindow.removeEventListener("load", onLoad, true);
   1.378 +      clearTimeout(this.callBack_onTimeout);
   1.379 +
   1.380 +      // wait for trasition to fullscreen on OSX Lion later
   1.381 +      if (isOSX) {
   1.382 +        setTimeout(function(){
   1.383 +          domwindow.close();
   1.384 +          executeSoon(this.callBack_onFinalize);
   1.385 +        }.bind(this), 3000);
   1.386 +      }
   1.387 +      else {
   1.388 +        domwindow.close();
   1.389 +        executeSoon(this.callBack_onFinalize);
   1.390 +      }
   1.391 +    }.bind(this), true);
   1.392 +  },
   1.393 +  onCloseWindow: function(aXULWindow) {},
   1.394 +  onWindowTitleChange: function(aXULWindow, aNewTitle) {},
   1.395 +  QueryInterface: XPCOMUtils.generateQI([Ci.nsIWindowMediatorListener,
   1.396 +                                         Ci.nsISupports]),
   1.397 +};

mercurial