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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 <?xml version="1.0"?>
     2 <!-- This Source Code Form is subject to the terms of the Mozilla Public
     3    - License, v. 2.0. If a copy of the MPL was not distributed with this
     4    - file, You can obtain one at http://mozilla.org/MPL/2.0/.  -->
     5 <!--
     6  * This test ensures that the delete key removes a download.  This was added by
     7  * bug 411172.
     8 -->
    10 <window title="Download Manager Test"
    11         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    12         onload="test();">
    14   <script type="application/javascript"
    15           src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
    16   <script type="application/javascript"
    17           src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
    18   <script type="application/javascript"
    19           src="utils.js"/>
    21   <script type="application/javascript">
    22   <![CDATA[
    24 var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
    25 var dmFile = Cc["@mozilla.org/file/directory_service;1"].
    26              getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
    27 dmFile.append("dm-ui-test.file");
    28 dmFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
    29 var gTestPath = ios.newFileURI(dmFile).spec;
    31 // Downloads are sorted by endTime, so make sure the end times are distinct
    32 const DownloadData = [
    33   { name: "381603.patch",
    34     source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
    35     target: gTestPath,
    36     startTime: 1180493839859230,
    37     endTime: 1180493839859239,
    38     state: Ci.nsIDownloadManager.DOWNLOAD_FINISHED,
    39     currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 },
    40   { name: "381603.patch",
    41     source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
    42     target: gTestPath,
    43     startTime: 1180493839859230,
    44     endTime: 1180493839859236,
    45     state: Ci.nsIDownloadManager.DOWNLOAD_FAILED,
    46     currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 },
    47   { name: "381603.patch",
    48     source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
    49     target: gTestPath,
    50     startTime: 1180493839859230,
    51     endTime: 1180493839859234,
    52     state: Ci.nsIDownloadManager.DOWNLOAD_CANCELED,
    53     currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 },
    54   { name: "381603.patch",
    55     source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
    56     target: gTestPath,
    57     startTime: 1180493839859230,
    58     endTime: 1180493839859232,
    59     state: Ci.nsIDownloadManager.DOWNLOAD_BLOCKED_PARENTAL,
    60     currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 },
    61   { name: "381603.patch",
    62     source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
    63     target: gTestPath,
    64     startTime: 1180493839859230,
    65     endTime: 1180493839859230,
    66     state: Ci.nsIDownloadManager.DOWNLOAD_DIRTY,
    67     currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 },
    68   { name: "381603.patch",
    69     source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520",
    70     target: gTestPath,
    71     startTime: 1180493839859229,
    72     endTime: 1180493839859229,
    73     state: Ci.nsIDownloadManager.DOWNLOAD_BLOCKED_POLICY,
    74     currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 }
    75 ];
    78 function test()
    79 {
    80   var dmui = getDMUI();
    81   if (!dmui) {
    82     todo(false, "skip test for toolkit download manager UI");
    83     return;
    84   }
    86   var dm = Cc["@mozilla.org/download-manager;1"].
    87            getService(Ci.nsIDownloadManager);
    88   var db = dm.DBConnection;
    90   // First, we populate the database with some fake data
    91   db.executeSimpleSQL("DELETE FROM moz_downloads");
    92   var stmt = db.createStatement(
    93     "INSERT INTO moz_downloads (name, source, target, startTime, endTime, " +
    94       "state, currBytes, maxBytes, preferredAction, autoResume) " +
    95     "VALUES (:name, :source, :target, :startTime, :endTime, :state, " +
    96       ":currBytes, :maxBytes, :preferredAction, :autoResume)");
    97   for each (var dl in DownloadData) {
    98     for (var prop in dl)
    99       stmt.params[prop] = dl[prop];
   101     stmt.execute();
   102   }
   103   stmt.finalize();
   105   // See if the DM is already open, and if it is, close it!
   106   var wm = Cc["@mozilla.org/appshell/window-mediator;1"].
   107            getService(Ci.nsIWindowMediator);
   108   var win = wm.getMostRecentWindow("Download:Manager");
   109   if (win)
   110     win.close();
   112   let os = Cc["@mozilla.org/observer-service;1"].
   113            getService(Ci.nsIObserverService);
   114   const DLMGR_UI_DONE = "download-manager-ui-done";
   116   let testObs = {
   117     observe: function(aSubject, aTopic, aData)
   118     {
   119       if (aTopic != DLMGR_UI_DONE)
   120         return;
   122       SimpleTest.waitForFocus(function () { deleteDownload(aSubject) }, aSubject);
   123     }
   124   };
   126   function deleteDownload(win) {
   127     let doc = win.document;
   129     let stmt = db.createStatement("SELECT COUNT(*) FROM moz_downloads");
   130     try {
   131       stmt.executeStep();
   132       let richlistbox = doc.getElementById("downloadView");
   133       richlistbox.selectedIndex = 0;
   134       is(stmt.getInt32(0), richlistbox.children.length,
   135          "The database and the number of downloads display matches");
   136       stmt.reset();
   138       let len = DownloadData.length;
   139       for (let i = 0; i < len; i++) {
   140         synthesizeKey("VK_BACK_SPACE", {}, win);
   142         stmt.executeStep();
   143         is(stmt.getInt32(0), len - (i + 1),
   144            "The download was properly removed");
   145         stmt.reset();
   146       }
   147     }
   148     finally {
   149       stmt.reset();
   150       stmt.finalize();
   151     }
   153     win.close();
   154     dmFile.remove(false);
   155     os.removeObserver(testObs, DLMGR_UI_DONE);
   156     SimpleTest.finish();
   157   }
   159   // Register with the observer service
   160   os.addObserver(testObs, DLMGR_UI_DONE, false);
   162   // Show the Download Manager UI
   163   dmui.show();
   165   SimpleTest.waitForExplicitFinish();
   166 }
   168   ]]>
   169   </script>
   171   <body xmlns="http://www.w3.org/1999/xhtml">
   172     <p id="display"></p>
   173     <div id="content" style="display:none;"></div>
   174     <pre id="test"></pre>
   175   </body>
   176 </window>

mercurial