toolkit/mozapps/downloads/tests/chrome/test_space_key_pauses_resumes.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 <!-- This Source Code Form is subject to the terms of the Mozilla Public
     3    - License, v. 2.0. If a copy of the MPL was not distributed with this
     4    - file, You can obtain one at http://mozilla.org/MPL/2.0/.  -->
     5 <!--
     6  * This tests that the space key will pause and resume a download in the UI.
     7  * This test was added in bug 413985.
     8 -->
    10 <window title="Download Manager Test"
    11         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    12         onload="test();">
    14   <script type="application/javascript"
    15           src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    16   <script type="application/javascript"
    17           src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
    18   <script type="application/javascript"
    19           src="utils.js"/>
    21   <script type="application/javascript">
    22   <![CDATA[
    24 function bug413985obs(aWin)
    25 {
    26   this.mWin = aWin;
    27   this.wasPaused = false;
    28   this.wasResumed = false;
    29   this.timer = null; // timer declared here to prevent premature GC 
    30 }
    31 bug413985obs.prototype = {
    32   observe: function(aSubject, aTopic, aData)
    33   {
    34     if ("timer-callback" == aTopic) {
    35       // dispatch a space keypress to resume the download
    36       synthesizeKey(" ", {}, this.mWin);
    37     }
    38   },
    40   onDownloadStateChange: function(aState, aDownload)
    41   {
    42     if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING &&
    43         !this.wasPaused) {
    44       this.wasPaused = true;
    45       // dispatch a space keypress to pause the download
    46       synthesizeKey(" ", {}, this.mWin);
    47     }
    49     if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_PAUSED &&
    50         !this.wasResumed) {
    51       this.wasResumed = true;
    52       // We have to do this on a timer so other JS stuff that handles the UI
    53       // can actually catch up to us...
    54       this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    55       this.timer.init(this, 0, Ci.nsITimer.TYPE_ONE_SHOT);
    56     }
    58     if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) {
    59       ok(this.wasPaused && this.wasResumed,
    60          "The download was paused, and then resumed to completion");
    61       aDownload.targetFile.remove(false);
    63       var dm = Cc["@mozilla.org/download-manager;1"].
    64                 getService(Ci.nsIDownloadManager);
    65       dm.removeListener(this);
    67       SimpleTest.finish();
    68     }
    69   },
    70   onStateChange: function(a, b, c, d, e) { },
    71   onProgressChange: function(a, b, c, d, e, f, g) { },
    72   onSecurityChange: function(a, b, c, d) { }
    73 };
    74 function test()
    75 {
    76   var dmui = getDMUI();
    77   if (!dmui) {
    78     todo(false, "skip test for toolkit download manager UI");
    79     return;
    80   }
    82   var dm = Cc["@mozilla.org/download-manager;1"].
    83            getService(Ci.nsIDownloadManager);
    85   function addDownload() {
    86     function createURI(aObj) {
    87       var ios = Cc["@mozilla.org/network/io-service;1"].
    88                 getService(Ci.nsIIOService);
    89       return (aObj instanceof Ci.nsIFile) ? ios.newFileURI(aObj) :
    90                                             ios.newURI(aObj, null, null);
    91     }
    93     const nsIWBP = Ci.nsIWebBrowserPersist;
    94     var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
    95                   .createInstance(Ci.nsIWebBrowserPersist);
    96     persist.persistFlags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
    97                            nsIWBP.PERSIST_FLAGS_BYPASS_CACHE |
    98                            nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
   100     var dirSvc = Cc["@mozilla.org/file/directory_service;1"].
   101                  getService(Ci.nsIProperties);
   102     var destFile = dirSvc.get("ProfD", Ci.nsIFile);
   103     destFile.append("download.result");
   104     if (destFile.exists())
   105       destFile.remove(false);
   107     var dl = dm.addDownload(Ci.nsIDownloadManager.DOWNLOAD_TYPE_DOWNLOAD,
   108                             createURI("http://example.com/httpd.js"),
   109                             createURI(destFile), null, null,
   110                             Math.round(Date.now() * 1000), null, persist, false);
   112     var privacyContext = window.QueryInterface(Ci.nsIInterfaceRequestor)
   113                                .getInterface(Ci.nsIWebNavigation)
   114                                .QueryInterface(Ci.nsILoadContext);
   116     persist.progressListener = dl.QueryInterface(Ci.nsIWebProgressListener);
   117     persist.saveURI(dl.source, null, null, null, null, dl.targetFile, privacyContext);
   119     return dl;
   120   }
   122   // First, we clear out the database
   123   dm.DBConnection.executeSimpleSQL("DELETE FROM moz_downloads");
   125   // See if the DM is already open, and if it is, close it!
   126   var wm = Cc["@mozilla.org/appshell/window-mediator;1"].
   127            getService(Ci.nsIWindowMediator);
   128   var win = wm.getMostRecentWindow("Download:Manager");
   129   if (win)
   130     win.close();
   132   let os = Cc["@mozilla.org/observer-service;1"].
   133            getService(Ci.nsIObserverService);
   134   const DLMGR_UI_DONE = "download-manager-ui-done";
   136   let testObs = {
   137     observe: function(aSubject, aTopic, aData)
   138     {
   139       if (aTopic != DLMGR_UI_DONE)
   140         return;
   142       SimpleTest.waitForFocus(function () { continueTest(aSubject) }, aSubject);
   143     }
   144   };
   146   var continueTest = function(win)
   147   {
   148     let doc = win.document;
   149     dm.addListener(new bug413985obs(win));
   151     let dl = addDownload();
   152     // we need to focus the download as well
   153     doc.getElementById("downloadView").selectedIndex = 0;
   154     os.removeObserver(testObs, DLMGR_UI_DONE);
   155   };
   157   // Register with the observer service
   158   os.addObserver(testObs, DLMGR_UI_DONE, false);
   160   // Show the Download Manager UI
   161   dmui.show();
   163   SimpleTest.waitForExplicitFinish();
   164 }
   166   ]]>
   167   </script>
   169   <body xmlns="http://www.w3.org/1999/xhtml">
   170     <p id="display"></p>
   171     <div id="content" style="display:none;"></div>
   172     <pre id="test"></pre>
   173   </body>
   174 </window>

mercurial