PageRenderTime 27ms CodeModel.GetById 24ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/zpao/v8monkey
Unknown | 206 lines | 177 code | 29 blank | 0 comment | 0 complexity | 0b067fe53c3b5a959553c89c9504d215 MD5 | raw file
  1<?xml version="1.0"?>
  2<!--
  3/* ***** BEGIN LICENSE BLOCK *****
  4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5 *
  6 * The contents of this file are subject to the Mozilla Public License Version
  7 * 1.1 (the "License"); you may not use this file except in compliance with
  8 * the License. You may obtain a copy of the License at
  9 * http://www.mozilla.org/MPL/
 10 *
 11 * Software distributed under the License is distributed on an "AS IS" basis,
 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 13 * for the specific language governing rights and limitations under the
 14 * License.
 15 *
 16 * The Original Code is mozilla.org code.
 17 *
 18 * The Initial Developer of the Original Code is
 19 * Mozilla Corporation.
 20 * Portions created by the Initial Developer are Copyright (C) 2008
 21 * the Initial Developer. All Rights Reserved.
 22 *
 23 * Contributor(s):
 24 *   Shawn Wilsher <me@shawnwilsher.com> (Original Author)
 25 *
 26 * Alternatively, the contents of this file may be used under the terms of
 27 * either the GNU General Public License Version 2 or later (the "GPL"), or
 28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 29 * in which case the provisions of the GPL or the LGPL are applicable instead
 30 * of those above. If you wish to allow use of your version of this file only
 31 * under the terms of either the GPL or the LGPL, and not to allow others to
 32 * use your version of this file under the terms of the MPL, indicate your
 33 * decision by deleting the provisions above and replace them with the notice
 34 * and other provisions required by the GPL or the LGPL. If you do not delete
 35 * the provisions above, a recipient may use your version of this file under
 36 * the terms of any one of the MPL, the GPL or the LGPL.
 37 *
 38 * ***** END LICENSE BLOCK ***** */
 39
 40/**
 41 * This tests that the space key will pause and resume a download in the UI.
 42 * This test was added in bug 413985.
 43 */
 44-->
 45
 46<window title="Download Manager Test"
 47        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
 48        onload="test();">
 49
 50  <script type="application/javascript"
 51          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
 52  <script type="application/javascript"
 53          src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
 54  <script type="application/javascript"
 55          src="utils.js"/>
 56
 57  <script type="application/javascript">
 58  <![CDATA[
 59
 60function bug413985obs(aWin)
 61{
 62  this.mWin = aWin;
 63  this.wasPaused = false;
 64  this.wasResumed = false;
 65  this.timer = null; // timer declared here to prevent premature GC 
 66}
 67bug413985obs.prototype = {
 68  observe: function(aSubject, aTopic, aData)
 69  {
 70    if ("timer-callback" == aTopic) {
 71      // dispatch a space keypress to resume the download
 72      synthesizeKey(" ", {}, this.mWin);
 73    }
 74  },
 75
 76  onDownloadStateChange: function(aState, aDownload)
 77  {
 78    if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING &&
 79        !this.wasPaused) {
 80      this.wasPaused = true;
 81      // dispatch a space keypress to pause the download
 82      synthesizeKey(" ", {}, this.mWin);
 83    }
 84
 85    if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_PAUSED &&
 86        !this.wasResumed) {
 87      this.wasResumed = true;
 88      // We have to do this on a timer so other JS stuff that handles the UI
 89      // can actually catch up to us...
 90      this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
 91      this.timer.init(this, 0, Ci.nsITimer.TYPE_ONE_SHOT);
 92    }
 93
 94    if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) {
 95      ok(this.wasPaused && this.wasResumed,
 96         "The download was paused, and then resumed to completion");
 97      aDownload.targetFile.remove(false);
 98
 99      var dm = Cc["@mozilla.org/download-manager;1"].
100                getService(Ci.nsIDownloadManager);
101      dm.removeListener(this);
102
103      SimpleTest.finish();
104    }
105  },
106  onStateChange: function(a, b, c, d, e) { },
107  onProgressChange: function(a, b, c, d, e, f, g) { },
108  onSecurityChange: function(a, b, c, d) { }
109};
110function test()
111{
112  var dmui = getDMUI();
113  if (!dmui) {
114    todo(false, "skip test for toolkit download manager UI");
115    return;
116  }
117
118  var dm = Cc["@mozilla.org/download-manager;1"].
119           getService(Ci.nsIDownloadManager);
120
121  function addDownload() {
122    function createURI(aObj) {
123      var ios = Cc["@mozilla.org/network/io-service;1"].
124                getService(Ci.nsIIOService);
125      return (aObj instanceof Ci.nsIFile) ? ios.newFileURI(aObj) :
126                                            ios.newURI(aObj, null, null);
127    }
128
129    const nsIWBP = Ci.nsIWebBrowserPersist;
130    var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
131                  .createInstance(Ci.nsIWebBrowserPersist);
132    persist.persistFlags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
133                           nsIWBP.PERSIST_FLAGS_BYPASS_CACHE |
134                           nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
135
136    var dirSvc = Cc["@mozilla.org/file/directory_service;1"].
137                 getService(Ci.nsIProperties);
138    var destFile = dirSvc.get("ProfD", Ci.nsIFile);
139    destFile.append("download.result");
140    if (destFile.exists())
141      destFile.remove(false);
142
143    var dl = dm.addDownload(Ci.nsIDownloadManager.DOWNLOAD_TYPE_DOWNLOAD,
144                            createURI("http://example.com/httpd.js"),
145                            createURI(destFile), null, null,
146                            Math.round(Date.now() * 1000), null, persist);
147
148    persist.progressListener = dl.QueryInterface(Ci.nsIWebProgressListener);
149    persist.saveURI(dl.source, null, null, null, null, dl.targetFile);
150
151    return dl;
152  }
153
154  // First, we clear out the database
155  dm.DBConnection.executeSimpleSQL("DELETE FROM moz_downloads");
156
157  // See if the DM is already open, and if it is, close it!
158  var wm = Cc["@mozilla.org/appshell/window-mediator;1"].
159           getService(Ci.nsIWindowMediator);
160  var win = wm.getMostRecentWindow("Download:Manager");
161  if (win)
162    win.close();
163
164  let os = Cc["@mozilla.org/observer-service;1"].
165           getService(Ci.nsIObserverService);
166  const DLMGR_UI_DONE = "download-manager-ui-done";
167
168  let testObs = {
169    observe: function(aSubject, aTopic, aData)
170    {
171      if (aTopic != DLMGR_UI_DONE)
172        return;
173
174      SimpleTest.waitForFocus(function () { continueTest(aSubject) }, aSubject);
175    }
176  };
177
178  var continueTest = function(win)
179  {
180    let doc = win.document;
181    dm.addListener(new bug413985obs(win));
182
183    let dl = addDownload();
184    // we need to focus the download as well
185    doc.getElementById("downloadView").selectedIndex = 0;
186    os.removeObserver(testObs, DLMGR_UI_DONE);
187  };
188
189  // Register with the observer service
190  os.addObserver(testObs, DLMGR_UI_DONE, false);
191
192  // Show the Download Manager UI
193  dmui.show();
194
195  SimpleTest.waitForExplicitFinish();
196}
197
198  ]]>
199  </script>
200
201  <body xmlns="http://www.w3.org/1999/xhtml">
202    <p id="display"></p>
203    <div id="content" style="display:none;"></div>
204    <pre id="test"></pre>
205  </body>
206</window>