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

mercurial