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

http://github.com/zpao/v8monkey · Unknown · 212 lines · 187 code · 25 blank · 0 comment · 0 complexity · f51016b51397c9bc1422ec12554f1d16 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 test ensures that the delete key removes a download. This was added by
  41. * bug 411172.
  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. var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
  56. var dmFile = Cc["@mozilla.org/file/directory_service;1"].
  57. getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
  58. dmFile.append("dm-ui-test.file");
  59. dmFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
  60. var gTestPath = ios.newFileURI(dmFile).spec;
  61. // Downloads are sorted by endTime, so make sure the end times are distinct
  62. const DownloadData = [
  63. { name: "381603.patch",
  64. source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
  65. target: gTestPath,
  66. startTime: 1180493839859230,
  67. endTime: 1180493839859239,
  68. state: Ci.nsIDownloadManager.DOWNLOAD_FINISHED,
  69. currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 },
  70. { name: "381603.patch",
  71. source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
  72. target: gTestPath,
  73. startTime: 1180493839859230,
  74. endTime: 1180493839859236,
  75. state: Ci.nsIDownloadManager.DOWNLOAD_FAILED,
  76. currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 },
  77. { name: "381603.patch",
  78. source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
  79. target: gTestPath,
  80. startTime: 1180493839859230,
  81. endTime: 1180493839859234,
  82. state: Ci.nsIDownloadManager.DOWNLOAD_CANCELED,
  83. currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 },
  84. { name: "381603.patch",
  85. source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
  86. target: gTestPath,
  87. startTime: 1180493839859230,
  88. endTime: 1180493839859232,
  89. state: Ci.nsIDownloadManager.DOWNLOAD_BLOCKED_PARENTAL,
  90. currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 },
  91. { name: "381603.patch",
  92. source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
  93. target: gTestPath,
  94. startTime: 1180493839859230,
  95. endTime: 1180493839859230,
  96. state: Ci.nsIDownloadManager.DOWNLOAD_DIRTY,
  97. currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 },
  98. { name: "381603.patch",
  99. source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
  100. target: gTestPath,
  101. startTime: 1180493839859229,
  102. endTime: 1180493839859229,
  103. state: Ci.nsIDownloadManager.DOWNLOAD_BLOCKED_POLICY,
  104. currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 }
  105. ];
  106. function test()
  107. {
  108. var dmui = getDMUI();
  109. if (!dmui) {
  110. todo(false, "skip test for toolkit download manager UI");
  111. return;
  112. }
  113. var dm = Cc["@mozilla.org/download-manager;1"].
  114. getService(Ci.nsIDownloadManager);
  115. var db = dm.DBConnection;
  116. // First, we populate the database with some fake data
  117. db.executeSimpleSQL("DELETE FROM moz_downloads");
  118. var stmt = db.createStatement(
  119. "INSERT INTO moz_downloads (name, source, target, startTime, endTime, " +
  120. "state, currBytes, maxBytes, preferredAction, autoResume) " +
  121. "VALUES (:name, :source, :target, :startTime, :endTime, :state, " +
  122. ":currBytes, :maxBytes, :preferredAction, :autoResume)");
  123. for each (var dl in DownloadData) {
  124. for (var prop in dl)
  125. stmt.params[prop] = dl[prop];
  126. stmt.execute();
  127. }
  128. stmt.finalize();
  129. // See if the DM is already open, and if it is, close it!
  130. var wm = Cc["@mozilla.org/appshell/window-mediator;1"].
  131. getService(Ci.nsIWindowMediator);
  132. var win = wm.getMostRecentWindow("Download:Manager");
  133. if (win)
  134. win.close();
  135. let os = Cc["@mozilla.org/observer-service;1"].
  136. getService(Ci.nsIObserverService);
  137. const DLMGR_UI_DONE = "download-manager-ui-done";
  138. let testObs = {
  139. observe: function(aSubject, aTopic, aData)
  140. {
  141. if (aTopic != DLMGR_UI_DONE)
  142. return;
  143. SimpleTest.waitForFocus(function () { deleteDownload(aSubject) }, aSubject);
  144. }
  145. };
  146. function deleteDownload(win) {
  147. let doc = win.document;
  148. let stmt = db.createStatement("SELECT COUNT(*) FROM moz_downloads");
  149. try {
  150. stmt.executeStep();
  151. let richlistbox = doc.getElementById("downloadView");
  152. richlistbox.selectedIndex = 0;
  153. is(stmt.getInt32(0), richlistbox.children.length,
  154. "The database and the number of downloads display matches");
  155. stmt.reset();
  156. let len = DownloadData.length;
  157. for (let i = 0; i < len; i++) {
  158. synthesizeKey("VK_BACK_SPACE", {}, win);
  159. stmt.executeStep();
  160. is(stmt.getInt32(0), len - (i + 1),
  161. "The download was properly removed");
  162. stmt.reset();
  163. }
  164. }
  165. finally {
  166. stmt.reset();
  167. stmt.finalize();
  168. }
  169. win.close();
  170. dmFile.remove(false);
  171. os.removeObserver(testObs, DLMGR_UI_DONE);
  172. SimpleTest.finish();
  173. }
  174. // Register with the observer service
  175. os.addObserver(testObs, DLMGR_UI_DONE, false);
  176. // Show the Download Manager UI
  177. dmui.show();
  178. SimpleTest.waitForExplicitFinish();
  179. }
  180. ]]>
  181. </script>
  182. <body xmlns="http://www.w3.org/1999/xhtml">
  183. <p id="display"></p>
  184. <div id="content" style="display:none;"></div>
  185. <pre id="test"></pre>
  186. </body>
  187. </window>