PageRenderTime 22ms CodeModel.GetById 14ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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