toolkit/mozapps/downloads/tests/chrome/test_basic_functionality.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_basic_functionality.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,219 @@
     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 + * Make sure the download manager can display downloads in the right order and
    1.10 + * contains the expected data. The list has one of each download state ordered
    1.11 + * by the start/end times.
    1.12 +-->
    1.13 +
    1.14 +<window title="Download Manager Test"
    1.15 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    1.16 +        onload="test();">
    1.17 +
    1.18 +  <script type="application/javascript"
    1.19 +          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    1.20 +  <script type="application/javascript"
    1.21 +          src="utils.js"/>
    1.22 +
    1.23 +  <script type="application/javascript">
    1.24 +  <![CDATA[
    1.25 +
    1.26 +var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    1.27 +var dmFile = Cc["@mozilla.org/file/directory_service;1"].
    1.28 +             getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
    1.29 +dmFile.append("dm-ui-test.file");
    1.30 +dmFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
    1.31 +var gTestPath = ios.newFileURI(dmFile).spec;
    1.32 +
    1.33 +// Downloads are sorted by endTime, so make sure the end times are distinct
    1.34 +const DownloadData = [
    1.35 +  /* Active states first */
    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_NOTSTARTED,
    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: 1180493839859238,
    1.48 +    state: Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING,
    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: 1180493839859237,
    1.55 +    state: Ci.nsIDownloadManager.DOWNLOAD_PAUSED,
    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: 1180493839859236,
    1.62 +    state: Ci.nsIDownloadManager.DOWNLOAD_SCANNING,
    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: 1180493839859235,
    1.69 +    state: Ci.nsIDownloadManager.DOWNLOAD_QUEUED,
    1.70 +    currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 },
    1.71 +  /* Finished states */
    1.72 +  { name: "381603.patch",
    1.73 +    source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
    1.74 +    target: gTestPath,
    1.75 +    startTime: 1180493839859230,
    1.76 +    endTime: 1180493839859234,
    1.77 +    state: Ci.nsIDownloadManager.DOWNLOAD_FINISHED,
    1.78 +    currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 },
    1.79 +  { name: "381603.patch",
    1.80 +    source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
    1.81 +    target: gTestPath,
    1.82 +    startTime: 1180493839859230,
    1.83 +    endTime: 1180493839859233,
    1.84 +    state: Ci.nsIDownloadManager.DOWNLOAD_FAILED,
    1.85 +    currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 },
    1.86 +  { name: "381603.patch",
    1.87 +    source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
    1.88 +    target: gTestPath,
    1.89 +    startTime: 1180493839859230,
    1.90 +    endTime: 1180493839859232,
    1.91 +    state: Ci.nsIDownloadManager.DOWNLOAD_CANCELED,
    1.92 +    currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 },
    1.93 +  { name: "381603.patch",
    1.94 +    source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
    1.95 +    target: gTestPath,
    1.96 +    startTime: 1180493839859230,
    1.97 +    endTime: 1180493839859231,
    1.98 +    state: Ci.nsIDownloadManager.DOWNLOAD_BLOCKED_PARENTAL,
    1.99 +    currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 },
   1.100 +  { name: "381603.patch",
   1.101 +    source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
   1.102 +    target: gTestPath,
   1.103 +    startTime: 1180493839859230,
   1.104 +    endTime: 1180493839859230,
   1.105 +    state: Ci.nsIDownloadManager.DOWNLOAD_DIRTY,
   1.106 +    currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 },
   1.107 +  { name: "381603.patch",
   1.108 +    source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
   1.109 +    target: gTestPath,
   1.110 +    startTime: 1180493839859229,
   1.111 +    endTime: 1180493839859229,
   1.112 +    state: Ci.nsIDownloadManager.DOWNLOAD_BLOCKED_POLICY,
   1.113 +    currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 }
   1.114 +];
   1.115 +
   1.116 +function test_numberOfRichlistitems(aWin)
   1.117 +{
   1.118 +  var doc = aWin.document;
   1.119 +  var richlistbox = doc.getElementById("downloadView");
   1.120 +  is(richlistbox.children.length, DownloadData.length,
   1.121 +     "There is the correct number of richlistitems");
   1.122 +}
   1.123 +
   1.124 +function test_properDownloadData(aWin)
   1.125 +{
   1.126 +  // This also tests the ordering of the display
   1.127 +  var doc = aWin.document;
   1.128 +  var richlistbox = doc.getElementById("downloadView");
   1.129 +  for (var i = 0; i < richlistbox.children.length; i++) {
   1.130 +    var elm = richlistbox.children[i];
   1.131 +    is(elm.getAttribute("target"), DownloadData[i].name,
   1.132 +       "Download names match up");
   1.133 +    is(elm.getAttribute("state"), DownloadData[i].state,
   1.134 +       "Download states match up");
   1.135 +    is(elm.getAttribute("file"), DownloadData[i].target,
   1.136 +       "Download targets match up");
   1.137 +    is(elm.getAttribute("uri"), DownloadData[i].source,
   1.138 +       "Download sources match up");
   1.139 +  }
   1.140 +}
   1.141 +
   1.142 +var testFuncs = [
   1.143 +    test_numberOfRichlistitems
   1.144 +  , test_properDownloadData
   1.145 +];
   1.146 +
   1.147 +function test()
   1.148 +{
   1.149 +  var dmui = getDMUI();
   1.150 +  if (!dmui) {
   1.151 +    todo(false, "skip test for toolkit download manager UI");
   1.152 +    return;
   1.153 +  }
   1.154 +
   1.155 +  var dm = Cc["@mozilla.org/download-manager;1"].
   1.156 +           getService(Ci.nsIDownloadManager);
   1.157 +  var db = dm.DBConnection;
   1.158 +
   1.159 +  // First, we populate the database with some fake data
   1.160 +  db.executeSimpleSQL("DELETE FROM moz_downloads");
   1.161 +  var stmt = db.createStatement(
   1.162 +    "INSERT INTO moz_downloads (name, source, target, startTime, endTime, " +
   1.163 +      "state, currBytes, maxBytes, preferredAction, autoResume) " +
   1.164 +    "VALUES (:name, :source, :target, :startTime, :endTime, :state, " +
   1.165 +      ":currBytes, :maxBytes, :preferredAction, :autoResume)");
   1.166 +  for each (var dl in DownloadData) {
   1.167 +    for (var prop in dl)
   1.168 +      stmt.params[prop] = dl[prop];
   1.169 +
   1.170 +    stmt.execute();
   1.171 +  }
   1.172 +  stmt.finalize();
   1.173 +
   1.174 +  // See if the DM is already open, and if it is, close it!
   1.175 +  var wm = Cc["@mozilla.org/appshell/window-mediator;1"].
   1.176 +           getService(Ci.nsIWindowMediator);
   1.177 +  var win = wm.getMostRecentWindow("Download:Manager");
   1.178 +  if (win)
   1.179 +    win.close();
   1.180 +
   1.181 +  let os = Cc["@mozilla.org/observer-service;1"].
   1.182 +           getService(Ci.nsIObserverService);
   1.183 +  const DLMGR_UI_DONE = "download-manager-ui-done";
   1.184 +
   1.185 +  let testObs = {
   1.186 +    observe: function(aSubject, aTopic, aData)
   1.187 +    {
   1.188 +      if (aTopic != DLMGR_UI_DONE)
   1.189 +        return;
   1.190 +
   1.191 +      let win = aSubject.QueryInterface(Ci.nsIDOMWindow);
   1.192 +      win.focus();
   1.193 +
   1.194 +      // Now we can run our tests
   1.195 +      for each (let t in testFuncs)
   1.196 +        t(win);
   1.197 +
   1.198 +      win.close();
   1.199 +      dmFile.remove(false);
   1.200 +      os.removeObserver(testObs, DLMGR_UI_DONE);
   1.201 +      SimpleTest.finish();
   1.202 +    }
   1.203 +  };
   1.204 +
   1.205 +  // Register with the observer service
   1.206 +  os.addObserver(testObs, DLMGR_UI_DONE, false);
   1.207 +
   1.208 +  // Show the Download Manager UI
   1.209 +  dmui.show();
   1.210 +
   1.211 +  SimpleTest.waitForExplicitFinish();
   1.212 +}
   1.213 +
   1.214 +  ]]>
   1.215 +  </script>
   1.216 +
   1.217 +  <body xmlns="http://www.w3.org/1999/xhtml">
   1.218 +    <p id="display"></p>
   1.219 +    <div id="content" style="display:none;"></div>
   1.220 +    <pre id="test"></pre>
   1.221 +  </body>
   1.222 +</window>

mercurial