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