toolkit/mozapps/downloads/tests/chrome/test_close_on_last_window.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/mozapps/downloads/tests/chrome/test_close_on_last_window.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,214 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<!--
     1.6 +/* Any copyright is dedicated to the Public Domain.
     1.7 + * http://creativecommons.org/publicdomain/zero/1.0/
     1.8 + */
     1.9 +
    1.10 +/**
    1.11 + * Bug 544356 - This tests that the Download Manager is closed
    1.12 + * when the last browser window is closed, if there are no active
    1.13 + * downloads.
    1.14 + */
    1.15 +-->
    1.16 +
    1.17 +<window title="Download Manager Test"
    1.18 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    1.19 +        onload="test();">
    1.20 +
    1.21 +  <script type="application/javascript"
    1.22 +          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    1.23 +  <script type="application/javascript"
    1.24 +          src="utils.js"/>
    1.25 +
    1.26 +  <script type="application/javascript">
    1.27 +  <![CDATA[
    1.28 +
    1.29 +const DLMGR_UI_DONE = "download-manager-ui-done";
    1.30 +
    1.31 +Components.utils.import("resource://gre/modules/Services.jsm");
    1.32 +
    1.33 +let gGen, dm, dmui, chromeWindow, testCleanup;
    1.34 +
    1.35 +function test()
    1.36 +{
    1.37 +  if (testSetup()) {
    1.38 +    SimpleTest.waitForExplicitFinish();
    1.39 +
    1.40 +    //Start test
    1.41 +    gGen = doTest();
    1.42 +    gGen.next();
    1.43 +  }
    1.44 +}
    1.45 +
    1.46 +function doTest()
    1.47 +{
    1.48 +  let browserWindow1 = openBrowserWindow(continueTest);
    1.49 +  yield;
    1.50 +  let browserWindow2 = openBrowserWindow(continueTest);
    1.51 +  yield;
    1.52 +  openDownloadManager(continueTest);
    1.53 +  yield;
    1.54 +  let DMWindow = Services.wm.getMostRecentWindow("Download:Manager");
    1.55 +
    1.56 +  addCloseListener([browserWindow1, browserWindow2]);
    1.57 +  browserWindow1.BrowserTryToCloseWindow();
    1.58 +
    1.59 +  yield;
    1.60 +
    1.61 +  // Wait for a moment and make sure the Download Manager didn't close,
    1.62 +  // because there's still one browser window
    1.63 +  setTimeout(continueTest, 500); yield;
    1.64 +  ok(dmui.visible, "Download Manager wasn't closed");
    1.65 +
    1.66 +  let download = addDownload();
    1.67 +  // The download listener will pause the download and continue the test
    1.68 +  yield;
    1.69 +
    1.70 +  browserWindow2.BrowserTryToCloseWindow();
    1.71 +  yield;
    1.72 +
    1.73 +  // Wait for a moment and make sure the Download Manager didn't close,
    1.74 +  // because there's one active download
    1.75 +  setTimeout(continueTest, 500); yield;
    1.76 +  ok(dmui.visible, "Download Manager wasn't closed");
    1.77 +
    1.78 +  dm.cancelDownload(download.id);
    1.79 +  dm.removeDownload(download.id);
    1.80 +
    1.81 +  let browserWindow3 = openBrowserWindow(continueTest);
    1.82 +  yield;
    1.83 +
    1.84 +  // Prepare to close last window
    1.85 +  ok(dmui.visible, "Download Manager wasn't closed");
    1.86 +
    1.87 +  let isMac = /Mac/.test(navigator.platform);
    1.88 +  addCloseListener(isMac ? [browserWindow3] : [browserWindow3, DMWindow]);
    1.89 +  browserWindow3.BrowserTryToCloseWindow();
    1.90 +  if (isMac) {
    1.91 +    // Manually continue the test after a moment to make sure the Download Manager
    1.92 +    // didn't close
    1.93 +    setTimeout(continueTest, 500);
    1.94 +  }
    1.95 +  yield; yield;
    1.96 +
    1.97 +  // Finally we reach the case where the Download Manager should have been closed
    1.98 +  if (isMac) {
    1.99 +    ok(dmui.visible, "Download Manager wasn't closed");
   1.100 +    DMWindow.close();
   1.101 +  } else {
   1.102 +    ok (!dmui.visible, "Download Manager was closed");
   1.103 +  }
   1.104 +
   1.105 +  testCleanup();
   1.106 +  SimpleTest.finish();
   1.107 +  yield;
   1.108 +}
   1.109 +
   1.110 +function continueTest()
   1.111 +{
   1.112 +  SimpleTest.executeSoon(function() {
   1.113 +    gGen.next();
   1.114 +  });
   1.115 +}
   1.116 +
   1.117 +function testSetup()
   1.118 +{
   1.119 +  chromeWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
   1.120 +                  getInterface(Components.interfaces.nsIWebNavigation).
   1.121 +                  QueryInterface(Components.interfaces.nsIDocShellTreeItem).
   1.122 +                  rootTreeItem.
   1.123 +                  QueryInterface(Components.interfaces.nsIInterfaceRequestor).
   1.124 +                  getInterface(Components.interfaces.nsIDOMWindow);
   1.125 +
   1.126 +  dmui = getDMUI();
   1.127 +  if (!dmui) {
   1.128 +    todo(false, "skip test for toolkit download manager UI");
   1.129 +    return false;
   1.130 +  }
   1.131 +
   1.132 +  dm = Cc["@mozilla.org/download-manager;1"].
   1.133 +       getService(Ci.nsIDownloadManager);
   1.134 +
   1.135 +  // Change the window type so this one doesn't count as a browser window
   1.136 +  let oldWinType = chromeWindow.document.documentElement.getAttribute("windowtype");
   1.137 +  chromeWindow.document.documentElement.setAttribute("windowtype", "navigator:testrunner");
   1.138 +
   1.139 +  downloadListener = {
   1.140 +
   1.141 +    onDownloadStateChange: function(aState, aDownload)
   1.142 +    {
   1.143 +      if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_PAUSED) {
   1.144 +        continueTest();
   1.145 +      } else if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING) {
   1.146 +        dm.pauseDownload(aDownload.id);
   1.147 +      }
   1.148 +    },
   1.149 +
   1.150 +    onStateChange: function() { },
   1.151 +    onProgressChange: function() { },
   1.152 +    onSecurityChange: function() { }
   1.153 +  };
   1.154 +
   1.155 +  dm.addListener(downloadListener);
   1.156 +
   1.157 +  testCleanup = function() {
   1.158 +    dm.removeListener(downloadListener);
   1.159 +    chromeWindow.document.documentElement.setAttribute("windowtype", oldWinType);
   1.160 +  };
   1.161 +
   1.162 +  return true;
   1.163 +}
   1.164 +
   1.165 +function openBrowserWindow(callback)
   1.166 +{
   1.167 +  let browserWindow = openDialog(chromeWindow.location, "_blank",
   1.168 +                                 "chrome,all,dialog=no", "about:blank");
   1.169 + 
   1.170 +  let helperFunc = function() {
   1.171 +    callback();
   1.172 +    browserWindow.removeEventListener("load", helperFunc, false);
   1.173 +  }
   1.174 +
   1.175 +  browserWindow.addEventListener("load", helperFunc, false);
   1.176 +
   1.177 +  return browserWindow;
   1.178 +}
   1.179 +
   1.180 +function openDownloadManager(callback)
   1.181 +{
   1.182 +  let testObs = {
   1.183 +    observe: function(aSubject, aTopic, aData) {
   1.184 +      if (aTopic != DLMGR_UI_DONE) {
   1.185 +        return;
   1.186 +      }
   1.187 +
   1.188 +      callback();
   1.189 +      Services.obs.removeObserver(testObs, DLMGR_UI_DONE);
   1.190 +    }
   1.191 +  };
   1.192 +
   1.193 +  Services.obs.addObserver(testObs, DLMGR_UI_DONE, false);
   1.194 +
   1.195 +  dmui.show();
   1.196 +}
   1.197 +
   1.198 +function addCloseListener(aWins)
   1.199 +{
   1.200 +  aWins.forEach(function(win) {
   1.201 +    let listener = function() {
   1.202 +      win.removeEventListener("unload", listener, false);
   1.203 +      continueTest();
   1.204 +    };
   1.205 +    win.addEventListener("unload", listener, false);
   1.206 +  });
   1.207 +}
   1.208 +
   1.209 +  ]]>
   1.210 +  </script>
   1.211 +
   1.212 +  <body xmlns="http://www.w3.org/1999/xhtml">
   1.213 +    <p id="display"></p>
   1.214 +    <div id="content" style="display:none;"></div>
   1.215 +    <pre id="test"></pre>
   1.216 +  </body>
   1.217 +</window>

mercurial