Bug 1503050 - ensure we fully open the new window we intend to close before doing so, r=Felipe, a=test-only
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Tue, 30 Oct 2018 13:00:07 +0000
changeset 501155 a2ea2d92a10b09dedffb11e75a030fb5646936a6
parent 501154 9a5723abbaf07e35ce0eb394db045249d4a54b8b
child 501156 0501846e98cbcdacfc662e50764401cb802fd8ba
push id1864
push userffxbld-merge
push dateMon, 03 Dec 2018 15:51:40 +0000
treeherdermozilla-release@f040763d99ad [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersFelipe, test-only
bugs1503050
milestone64.0
Bug 1503050 - ensure we fully open the new window we intend to close before doing so, r=Felipe, a=test-only Differential Revision: https://phabricator.services.mozilla.com/D10134
uriloader/exthandler/tests/mochitest/browser.ini
uriloader/exthandler/tests/mochitest/browser_auto_close_window.js
uriloader/exthandler/tests/mochitest/download.sjs
uriloader/exthandler/tests/mochitest/download_page.html
--- a/uriloader/exthandler/tests/mochitest/browser.ini
+++ b/uriloader/exthandler/tests/mochitest/browser.ini
@@ -1,13 +1,15 @@
 [DEFAULT]
 head = head.js
 support-files =
-  download_page.html
-  download.bin
   protocolHandler.html
 
 [browser_auto_close_window.js]
 run-if = e10s # test relies on e10s behavior
+support-files =
+  download_page.html
+  download.bin
+  download.sjs
 [browser_download_always_ask_preferred_app.js]
 [browser_download_privatebrowsing.js]
 [browser_remember_download_option.js]
 [browser_web_protocol_handlers.js]
--- a/uriloader/exthandler/tests/mochitest/browser_auto_close_window.js
+++ b/uriloader/exthandler/tests/mochitest/browser_auto_close_window.js
@@ -1,15 +1,15 @@
 /* Any copyright is dedicated to the Public Domain.
  * http://creativecommons.org/publicdomain/zero/1.0/ */
 
-const URL =
-  getRootDirectory(gTestPath).replace("chrome://mochitests/content",
-                                      "https://example.com") +
-    "download_page.html";
+const ROOT = getRootDirectory(gTestPath).replace("chrome://mochitests/content",
+                                                 "https://example.com");
+const PAGE_URL = ROOT + "download_page.html";
+const SJS_URL = ROOT + "download.sjs";
 
 const HELPERAPP_DIALOG_CONTRACT_ID = "@mozilla.org/helperapplauncherdialog;1";
 const HELPERAPP_DIALOG_CID =
         Components.ID(Cc[HELPERAPP_DIALOG_CONTRACT_ID].number);
 const MOCK_HELPERAPP_DIALOG_CID =
         Components.ID("{2f372b6f-56c9-46d5-af0d-9f09bb69860c}");
 
 let registrar = Components.manager
@@ -42,22 +42,22 @@ add_task(async function setup() {
   registrar.registerFactory(MOCK_HELPERAPP_DIALOG_CID, "",
                             HELPERAPP_DIALOG_CONTRACT_ID,
                             mockHelperAppService);
 });
 
 add_task(async function simple_navigation() {
   // Tests that simple navigation gives us the right windowContext (that is,
   // the window that we're using).
-  await BrowserTestUtils.withNewTab({ gBrowser, url: URL }, async function(browser) {
+  await BrowserTestUtils.withNewTab({ gBrowser, url: PAGE_URL }, async function(browser) {
     let dialogAppeared = promiseHelperAppDialog();
     await BrowserTestUtils.synthesizeMouseAtCenter("#regular_load", {}, browser);
     let windowContext = await dialogAppeared;
 
-    is(windowContext.gBrowser.selectedBrowser.currentURI.spec, URL,
+    is(windowContext.gBrowser.selectedBrowser.currentURI.spec, PAGE_URL,
        "got the right windowContext");
   });
 });
 
 // Given a browser pointing to download_page.html, clicks on the link that
 // opens with target="_blank" (i.e. a new tab) and ensures that we
 // automatically open and close that tab.
 async function testNewTab(browser) {
@@ -75,48 +75,51 @@ async function testNewTab(browser) {
   let [ tab, closingPromise ] = await tabOpened;
   await closingPromise;
   is(tab.linkedBrowser, null, "tab was opened and closed");
 }
 
 add_task(async function target_blank() {
   // Tests that a link with target=_blank opens a new tab and closes it,
   // returning the window that we're using for navigation.
-  await BrowserTestUtils.withNewTab({ gBrowser, url: URL }, async function(browser) {
+  await BrowserTestUtils.withNewTab({ gBrowser, url: PAGE_URL }, async function(browser) {
     await testNewTab(browser);
   });
 });
 
 add_task(async function new_window() {
   // Tests that a link that forces us to open a new window (by specifying a
   // width and a height in window.open) opens a new window for the load,
   // realizes that we need to close that window and returns the *original*
   // window as the window context.
-  await BrowserTestUtils.withNewTab({ gBrowser, url: URL }, async function(browser) {
+  await BrowserTestUtils.withNewTab({ gBrowser, url: PAGE_URL }, async function(browser) {
     let dialogAppeared = promiseHelperAppDialog();
     let windowOpened = BrowserTestUtils.waitForNewWindow();
 
     await BrowserTestUtils.synthesizeMouseAtCenter("#new_window", {}, browser);
+    let win = await windowOpened;
+    // Now allow request to complete:
+    fetch(SJS_URL + "?finish");
+
 
     let windowContext = await dialogAppeared;
-    is(windowContext.gBrowser.selectedBrowser.currentURI.spec, URL,
+    is(windowContext.gBrowser.selectedBrowser.currentURI.spec, PAGE_URL,
        "got the right windowContext");
-    let win = await windowOpened;
 
     // The window should close on its own. If not, this test will time out.
     await BrowserTestUtils.domWindowClosed(win);
     ok(win.closed, "window was opened and closed");
   });
 });
 
 add_task(async function nested_window_opens() {
   // Tests that the window auto-closing feature works if the download is
   // initiated by a window that, itself, has an opener (see bug 1373109).
-  await BrowserTestUtils.withNewTab({ gBrowser, url: URL }, async function(outerBrowser) {
-    let secondTabPromise = BrowserTestUtils.waitForNewTab(gBrowser, `${URL}?newwin`, true);
+  await BrowserTestUtils.withNewTab({ gBrowser, url: PAGE_URL }, async function(outerBrowser) {
+    let secondTabPromise = BrowserTestUtils.waitForNewTab(gBrowser, `${PAGE_URL}?newwin`, true);
     BrowserTestUtils.synthesizeMouseAtCenter("#open_in_new_tab", {}, outerBrowser);
     let secondTab = await secondTabPromise;
     let nestedBrowser = secondTab.linkedBrowser;
 
     await ContentTask.spawn(nestedBrowser, null, function() {
       ok(content.opener, "this window has an opener");
     });
 
new file mode 100644
--- /dev/null
+++ b/uriloader/exthandler/tests/mochitest/download.sjs
@@ -0,0 +1,34 @@
+"use strict";
+
+Cu.import("resource://gre/modules/Timer.jsm");
+
+function actuallyHandleRequest(req, res) {
+  res.setHeader("Content-Type", "application/octet-stream", false);
+  res.write("abc123");
+  res.finish();
+}
+
+function handleRequest(req, res) {
+  if (req.queryString.includes('finish')) {
+    res.write("OK");
+    let downloadReq = null;
+    getObjectState("downloadReq", o => { downloadReq = o });
+    // Two possibilities: either the download request has already reached us, or not.
+    if (downloadReq) {
+      downloadReq.wrappedJSObject.callback();
+    } else {
+      // Set a variable to allow the request to complete immediately:
+      setState("finishReq", "true");
+    }
+  } else {
+    res.processAsync();
+    if (getState("finishReq")) {
+      actuallyHandleRequest(req, res);
+    } else {
+      let o = {callback() { actuallyHandleRequest(req, res) }};
+      o.wrappedJSObject = o;
+      o.QueryInterface = () => o;
+      setObjectState("downloadReq", o);
+    }
+  }
+}
--- a/uriloader/exthandler/tests/mochitest/download_page.html
+++ b/uriloader/exthandler/tests/mochitest/download_page.html
@@ -2,17 +2,17 @@
    - http://creativecommons.org/publicdomain/zero/1.0/ -->
 <!DOCTYPE html>
 <html>
 <head>
   <meta charset=UTF-8>
   <title>Test page for link clicking</title>
   <script type="text/javascript">
     function launch_download() {
-      window.open("download.bin", "_blank", "height=100,width=100");
+      window.open("download.sjs", "_blank", "height=100,width=100");
     }
   </script>
 </head>
 <body>
   <a href="download.bin" id="regular_load">regular load</a>
   <a href="download.bin" id="target_blank" target="_blank">target blank</a>
   <a href="#" onclick="launch_download(); return false" id="new_window">new window</a>
   <a href="#" onclick="window.open('download_page.html?newwin'); return false" id="open_in_new_tab">click to reopen</a>