1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/downloads/tests/chrome/test_backspace_key_removes.xul Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,176 @@ 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 test ensures that the delete key removes a download. This was added by 1.10 + * bug 411172. 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 +var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); 1.28 +var dmFile = Cc["@mozilla.org/file/directory_service;1"]. 1.29 + getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile); 1.30 +dmFile.append("dm-ui-test.file"); 1.31 +dmFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); 1.32 +var gTestPath = ios.newFileURI(dmFile).spec; 1.33 + 1.34 +// Downloads are sorted by endTime, so make sure the end times are distinct 1.35 +const DownloadData = [ 1.36 + { name: "381603.patch", 1.37 + source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520", 1.38 + target: gTestPath, 1.39 + startTime: 1180493839859230, 1.40 + endTime: 1180493839859239, 1.41 + state: Ci.nsIDownloadManager.DOWNLOAD_FINISHED, 1.42 + currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 }, 1.43 + { name: "381603.patch", 1.44 + source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520", 1.45 + target: gTestPath, 1.46 + startTime: 1180493839859230, 1.47 + endTime: 1180493839859236, 1.48 + state: Ci.nsIDownloadManager.DOWNLOAD_FAILED, 1.49 + currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 }, 1.50 + { name: "381603.patch", 1.51 + source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520", 1.52 + target: gTestPath, 1.53 + startTime: 1180493839859230, 1.54 + endTime: 1180493839859234, 1.55 + state: Ci.nsIDownloadManager.DOWNLOAD_CANCELED, 1.56 + currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 }, 1.57 + { name: "381603.patch", 1.58 + source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520", 1.59 + target: gTestPath, 1.60 + startTime: 1180493839859230, 1.61 + endTime: 1180493839859232, 1.62 + state: Ci.nsIDownloadManager.DOWNLOAD_BLOCKED_PARENTAL, 1.63 + currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 }, 1.64 + { name: "381603.patch", 1.65 + source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520", 1.66 + target: gTestPath, 1.67 + startTime: 1180493839859230, 1.68 + endTime: 1180493839859230, 1.69 + state: Ci.nsIDownloadManager.DOWNLOAD_DIRTY, 1.70 + currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 }, 1.71 + { name: "381603.patch", 1.72 + source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520", 1.73 + target: gTestPath, 1.74 + startTime: 1180493839859229, 1.75 + endTime: 1180493839859229, 1.76 + state: Ci.nsIDownloadManager.DOWNLOAD_BLOCKED_POLICY, 1.77 + currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 } 1.78 +]; 1.79 + 1.80 + 1.81 +function test() 1.82 +{ 1.83 + var dmui = getDMUI(); 1.84 + if (!dmui) { 1.85 + todo(false, "skip test for toolkit download manager UI"); 1.86 + return; 1.87 + } 1.88 + 1.89 + var dm = Cc["@mozilla.org/download-manager;1"]. 1.90 + getService(Ci.nsIDownloadManager); 1.91 + var db = dm.DBConnection; 1.92 + 1.93 + // First, we populate the database with some fake data 1.94 + db.executeSimpleSQL("DELETE FROM moz_downloads"); 1.95 + var stmt = db.createStatement( 1.96 + "INSERT INTO moz_downloads (name, source, target, startTime, endTime, " + 1.97 + "state, currBytes, maxBytes, preferredAction, autoResume) " + 1.98 + "VALUES (:name, :source, :target, :startTime, :endTime, :state, " + 1.99 + ":currBytes, :maxBytes, :preferredAction, :autoResume)"); 1.100 + for each (var dl in DownloadData) { 1.101 + for (var prop in dl) 1.102 + stmt.params[prop] = dl[prop]; 1.103 + 1.104 + stmt.execute(); 1.105 + } 1.106 + stmt.finalize(); 1.107 + 1.108 + // See if the DM is already open, and if it is, close it! 1.109 + var wm = Cc["@mozilla.org/appshell/window-mediator;1"]. 1.110 + getService(Ci.nsIWindowMediator); 1.111 + var win = wm.getMostRecentWindow("Download:Manager"); 1.112 + if (win) 1.113 + win.close(); 1.114 + 1.115 + let os = Cc["@mozilla.org/observer-service;1"]. 1.116 + getService(Ci.nsIObserverService); 1.117 + const DLMGR_UI_DONE = "download-manager-ui-done"; 1.118 + 1.119 + let testObs = { 1.120 + observe: function(aSubject, aTopic, aData) 1.121 + { 1.122 + if (aTopic != DLMGR_UI_DONE) 1.123 + return; 1.124 + 1.125 + SimpleTest.waitForFocus(function () { deleteDownload(aSubject) }, aSubject); 1.126 + } 1.127 + }; 1.128 + 1.129 + function deleteDownload(win) { 1.130 + let doc = win.document; 1.131 + 1.132 + let stmt = db.createStatement("SELECT COUNT(*) FROM moz_downloads"); 1.133 + try { 1.134 + stmt.executeStep(); 1.135 + let richlistbox = doc.getElementById("downloadView"); 1.136 + richlistbox.selectedIndex = 0; 1.137 + is(stmt.getInt32(0), richlistbox.children.length, 1.138 + "The database and the number of downloads display matches"); 1.139 + stmt.reset(); 1.140 + 1.141 + let len = DownloadData.length; 1.142 + for (let i = 0; i < len; i++) { 1.143 + synthesizeKey("VK_BACK_SPACE", {}, win); 1.144 + 1.145 + stmt.executeStep(); 1.146 + is(stmt.getInt32(0), len - (i + 1), 1.147 + "The download was properly removed"); 1.148 + stmt.reset(); 1.149 + } 1.150 + } 1.151 + finally { 1.152 + stmt.reset(); 1.153 + stmt.finalize(); 1.154 + } 1.155 + 1.156 + win.close(); 1.157 + dmFile.remove(false); 1.158 + os.removeObserver(testObs, DLMGR_UI_DONE); 1.159 + SimpleTest.finish(); 1.160 + } 1.161 + 1.162 + // Register with the observer service 1.163 + os.addObserver(testObs, DLMGR_UI_DONE, false); 1.164 + 1.165 + // Show the Download Manager UI 1.166 + dmui.show(); 1.167 + 1.168 + SimpleTest.waitForExplicitFinish(); 1.169 +} 1.170 + 1.171 + ]]> 1.172 + </script> 1.173 + 1.174 + <body xmlns="http://www.w3.org/1999/xhtml"> 1.175 + <p id="display"></p> 1.176 + <div id="content" style="display:none;"></div> 1.177 + <pre id="test"></pre> 1.178 + </body> 1.179 +</window>