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

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 <?xml version="1.0"?>
     2 <!--
     3 /* Any copyright is dedicated to the Public Domain.
     4  * http://creativecommons.org/publicdomain/zero/1.0/
     5  */
     7 /**
     8  * Bug 544356 - This tests that the Download Manager is closed
     9  * when the last browser window is closed, if there are no active
    10  * downloads.
    11  */
    12 -->
    14 <window title="Download Manager Test"
    15         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    16         onload="test();">
    18   <script type="application/javascript"
    19           src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    20   <script type="application/javascript"
    21           src="utils.js"/>
    23   <script type="application/javascript">
    24   <![CDATA[
    26 const DLMGR_UI_DONE = "download-manager-ui-done";
    28 Components.utils.import("resource://gre/modules/Services.jsm");
    30 let gGen, dm, dmui, chromeWindow, testCleanup;
    32 function test()
    33 {
    34   if (testSetup()) {
    35     SimpleTest.waitForExplicitFinish();
    37     //Start test
    38     gGen = doTest();
    39     gGen.next();
    40   }
    41 }
    43 function doTest()
    44 {
    45   let browserWindow1 = openBrowserWindow(continueTest);
    46   yield;
    47   let browserWindow2 = openBrowserWindow(continueTest);
    48   yield;
    49   openDownloadManager(continueTest);
    50   yield;
    51   let DMWindow = Services.wm.getMostRecentWindow("Download:Manager");
    53   addCloseListener([browserWindow1, browserWindow2]);
    54   browserWindow1.BrowserTryToCloseWindow();
    56   yield;
    58   // Wait for a moment and make sure the Download Manager didn't close,
    59   // because there's still one browser window
    60   setTimeout(continueTest, 500); yield;
    61   ok(dmui.visible, "Download Manager wasn't closed");
    63   let download = addDownload();
    64   // The download listener will pause the download and continue the test
    65   yield;
    67   browserWindow2.BrowserTryToCloseWindow();
    68   yield;
    70   // Wait for a moment and make sure the Download Manager didn't close,
    71   // because there's one active download
    72   setTimeout(continueTest, 500); yield;
    73   ok(dmui.visible, "Download Manager wasn't closed");
    75   dm.cancelDownload(download.id);
    76   dm.removeDownload(download.id);
    78   let browserWindow3 = openBrowserWindow(continueTest);
    79   yield;
    81   // Prepare to close last window
    82   ok(dmui.visible, "Download Manager wasn't closed");
    84   let isMac = /Mac/.test(navigator.platform);
    85   addCloseListener(isMac ? [browserWindow3] : [browserWindow3, DMWindow]);
    86   browserWindow3.BrowserTryToCloseWindow();
    87   if (isMac) {
    88     // Manually continue the test after a moment to make sure the Download Manager
    89     // didn't close
    90     setTimeout(continueTest, 500);
    91   }
    92   yield; yield;
    94   // Finally we reach the case where the Download Manager should have been closed
    95   if (isMac) {
    96     ok(dmui.visible, "Download Manager wasn't closed");
    97     DMWindow.close();
    98   } else {
    99     ok (!dmui.visible, "Download Manager was closed");
   100   }
   102   testCleanup();
   103   SimpleTest.finish();
   104   yield;
   105 }
   107 function continueTest()
   108 {
   109   SimpleTest.executeSoon(function() {
   110     gGen.next();
   111   });
   112 }
   114 function testSetup()
   115 {
   116   chromeWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor).
   117                   getInterface(Components.interfaces.nsIWebNavigation).
   118                   QueryInterface(Components.interfaces.nsIDocShellTreeItem).
   119                   rootTreeItem.
   120                   QueryInterface(Components.interfaces.nsIInterfaceRequestor).
   121                   getInterface(Components.interfaces.nsIDOMWindow);
   123   dmui = getDMUI();
   124   if (!dmui) {
   125     todo(false, "skip test for toolkit download manager UI");
   126     return false;
   127   }
   129   dm = Cc["@mozilla.org/download-manager;1"].
   130        getService(Ci.nsIDownloadManager);
   132   // Change the window type so this one doesn't count as a browser window
   133   let oldWinType = chromeWindow.document.documentElement.getAttribute("windowtype");
   134   chromeWindow.document.documentElement.setAttribute("windowtype", "navigator:testrunner");
   136   downloadListener = {
   138     onDownloadStateChange: function(aState, aDownload)
   139     {
   140       if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_PAUSED) {
   141         continueTest();
   142       } else if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING) {
   143         dm.pauseDownload(aDownload.id);
   144       }
   145     },
   147     onStateChange: function() { },
   148     onProgressChange: function() { },
   149     onSecurityChange: function() { }
   150   };
   152   dm.addListener(downloadListener);
   154   testCleanup = function() {
   155     dm.removeListener(downloadListener);
   156     chromeWindow.document.documentElement.setAttribute("windowtype", oldWinType);
   157   };
   159   return true;
   160 }
   162 function openBrowserWindow(callback)
   163 {
   164   let browserWindow = openDialog(chromeWindow.location, "_blank",
   165                                  "chrome,all,dialog=no", "about:blank");
   167   let helperFunc = function() {
   168     callback();
   169     browserWindow.removeEventListener("load", helperFunc, false);
   170   }
   172   browserWindow.addEventListener("load", helperFunc, false);
   174   return browserWindow;
   175 }
   177 function openDownloadManager(callback)
   178 {
   179   let testObs = {
   180     observe: function(aSubject, aTopic, aData) {
   181       if (aTopic != DLMGR_UI_DONE) {
   182         return;
   183       }
   185       callback();
   186       Services.obs.removeObserver(testObs, DLMGR_UI_DONE);
   187     }
   188   };
   190   Services.obs.addObserver(testObs, DLMGR_UI_DONE, false);
   192   dmui.show();
   193 }
   195 function addCloseListener(aWins)
   196 {
   197   aWins.forEach(function(win) {
   198     let listener = function() {
   199       win.removeEventListener("unload", listener, false);
   200       continueTest();
   201     };
   202     win.addEventListener("unload", listener, false);
   203   });
   204 }
   206   ]]>
   207   </script>
   209   <body xmlns="http://www.w3.org/1999/xhtml">
   210     <p id="display"></p>
   211     <div id="content" style="display:none;"></div>
   212     <pre id="test"></pre>
   213   </body>
   214 </window>

mercurial