toolkit/mozapps/downloads/tests/chrome/test_space_key_pauses_resumes.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_space_key_pauses_resumes.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,174 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<!-- This Source Code Form is subject to the terms of the Mozilla Public
     1.6 +   - License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 +   - file, You can obtain one at http://mozilla.org/MPL/2.0/.  -->
     1.8 +<!--
     1.9 + * This tests that the space key will pause and resume a download in the UI.
    1.10 + * This test was added in bug 413985.
    1.11 +-->
    1.12 +
    1.13 +<window title="Download Manager Test"
    1.14 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    1.15 +        onload="test();">
    1.16 +
    1.17 +  <script type="application/javascript"
    1.18 +          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    1.19 +  <script type="application/javascript"
    1.20 +          src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
    1.21 +  <script type="application/javascript"
    1.22 +          src="utils.js"/>
    1.23 +
    1.24 +  <script type="application/javascript">
    1.25 +  <![CDATA[
    1.26 +
    1.27 +function bug413985obs(aWin)
    1.28 +{
    1.29 +  this.mWin = aWin;
    1.30 +  this.wasPaused = false;
    1.31 +  this.wasResumed = false;
    1.32 +  this.timer = null; // timer declared here to prevent premature GC 
    1.33 +}
    1.34 +bug413985obs.prototype = {
    1.35 +  observe: function(aSubject, aTopic, aData)
    1.36 +  {
    1.37 +    if ("timer-callback" == aTopic) {
    1.38 +      // dispatch a space keypress to resume the download
    1.39 +      synthesizeKey(" ", {}, this.mWin);
    1.40 +    }
    1.41 +  },
    1.42 +
    1.43 +  onDownloadStateChange: function(aState, aDownload)
    1.44 +  {
    1.45 +    if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING &&
    1.46 +        !this.wasPaused) {
    1.47 +      this.wasPaused = true;
    1.48 +      // dispatch a space keypress to pause the download
    1.49 +      synthesizeKey(" ", {}, this.mWin);
    1.50 +    }
    1.51 +
    1.52 +    if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_PAUSED &&
    1.53 +        !this.wasResumed) {
    1.54 +      this.wasResumed = true;
    1.55 +      // We have to do this on a timer so other JS stuff that handles the UI
    1.56 +      // can actually catch up to us...
    1.57 +      this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
    1.58 +      this.timer.init(this, 0, Ci.nsITimer.TYPE_ONE_SHOT);
    1.59 +    }
    1.60 +
    1.61 +    if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) {
    1.62 +      ok(this.wasPaused && this.wasResumed,
    1.63 +         "The download was paused, and then resumed to completion");
    1.64 +      aDownload.targetFile.remove(false);
    1.65 +
    1.66 +      var dm = Cc["@mozilla.org/download-manager;1"].
    1.67 +                getService(Ci.nsIDownloadManager);
    1.68 +      dm.removeListener(this);
    1.69 +
    1.70 +      SimpleTest.finish();
    1.71 +    }
    1.72 +  },
    1.73 +  onStateChange: function(a, b, c, d, e) { },
    1.74 +  onProgressChange: function(a, b, c, d, e, f, g) { },
    1.75 +  onSecurityChange: function(a, b, c, d) { }
    1.76 +};
    1.77 +function test()
    1.78 +{
    1.79 +  var dmui = getDMUI();
    1.80 +  if (!dmui) {
    1.81 +    todo(false, "skip test for toolkit download manager UI");
    1.82 +    return;
    1.83 +  }
    1.84 +
    1.85 +  var dm = Cc["@mozilla.org/download-manager;1"].
    1.86 +           getService(Ci.nsIDownloadManager);
    1.87 +
    1.88 +  function addDownload() {
    1.89 +    function createURI(aObj) {
    1.90 +      var ios = Cc["@mozilla.org/network/io-service;1"].
    1.91 +                getService(Ci.nsIIOService);
    1.92 +      return (aObj instanceof Ci.nsIFile) ? ios.newFileURI(aObj) :
    1.93 +                                            ios.newURI(aObj, null, null);
    1.94 +    }
    1.95 +
    1.96 +    const nsIWBP = Ci.nsIWebBrowserPersist;
    1.97 +    var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
    1.98 +                  .createInstance(Ci.nsIWebBrowserPersist);
    1.99 +    persist.persistFlags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
   1.100 +                           nsIWBP.PERSIST_FLAGS_BYPASS_CACHE |
   1.101 +                           nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
   1.102 +
   1.103 +    var dirSvc = Cc["@mozilla.org/file/directory_service;1"].
   1.104 +                 getService(Ci.nsIProperties);
   1.105 +    var destFile = dirSvc.get("ProfD", Ci.nsIFile);
   1.106 +    destFile.append("download.result");
   1.107 +    if (destFile.exists())
   1.108 +      destFile.remove(false);
   1.109 +
   1.110 +    var dl = dm.addDownload(Ci.nsIDownloadManager.DOWNLOAD_TYPE_DOWNLOAD,
   1.111 +                            createURI("http://example.com/httpd.js"),
   1.112 +                            createURI(destFile), null, null,
   1.113 +                            Math.round(Date.now() * 1000), null, persist, false);
   1.114 +
   1.115 +    var privacyContext = window.QueryInterface(Ci.nsIInterfaceRequestor)
   1.116 +                               .getInterface(Ci.nsIWebNavigation)
   1.117 +                               .QueryInterface(Ci.nsILoadContext);
   1.118 +
   1.119 +    persist.progressListener = dl.QueryInterface(Ci.nsIWebProgressListener);
   1.120 +    persist.saveURI(dl.source, null, null, null, null, dl.targetFile, privacyContext);
   1.121 +
   1.122 +    return dl;
   1.123 +  }
   1.124 +
   1.125 +  // First, we clear out the database
   1.126 +  dm.DBConnection.executeSimpleSQL("DELETE FROM moz_downloads");
   1.127 +
   1.128 +  // See if the DM is already open, and if it is, close it!
   1.129 +  var wm = Cc["@mozilla.org/appshell/window-mediator;1"].
   1.130 +           getService(Ci.nsIWindowMediator);
   1.131 +  var win = wm.getMostRecentWindow("Download:Manager");
   1.132 +  if (win)
   1.133 +    win.close();
   1.134 +
   1.135 +  let os = Cc["@mozilla.org/observer-service;1"].
   1.136 +           getService(Ci.nsIObserverService);
   1.137 +  const DLMGR_UI_DONE = "download-manager-ui-done";
   1.138 +
   1.139 +  let testObs = {
   1.140 +    observe: function(aSubject, aTopic, aData)
   1.141 +    {
   1.142 +      if (aTopic != DLMGR_UI_DONE)
   1.143 +        return;
   1.144 +
   1.145 +      SimpleTest.waitForFocus(function () { continueTest(aSubject) }, aSubject);
   1.146 +    }
   1.147 +  };
   1.148 +
   1.149 +  var continueTest = function(win)
   1.150 +  {
   1.151 +    let doc = win.document;
   1.152 +    dm.addListener(new bug413985obs(win));
   1.153 +
   1.154 +    let dl = addDownload();
   1.155 +    // we need to focus the download as well
   1.156 +    doc.getElementById("downloadView").selectedIndex = 0;
   1.157 +    os.removeObserver(testObs, DLMGR_UI_DONE);
   1.158 +  };
   1.159 +
   1.160 +  // Register with the observer service
   1.161 +  os.addObserver(testObs, DLMGR_UI_DONE, false);
   1.162 +
   1.163 +  // Show the Download Manager UI
   1.164 +  dmui.show();
   1.165 +
   1.166 +  SimpleTest.waitForExplicitFinish();
   1.167 +}
   1.168 +
   1.169 +  ]]>
   1.170 +  </script>
   1.171 +
   1.172 +  <body xmlns="http://www.w3.org/1999/xhtml">
   1.173 +    <p id="display"></p>
   1.174 +    <div id="content" style="display:none;"></div>
   1.175 +    <pre id="test"></pre>
   1.176 +  </body>
   1.177 +</window>

mercurial