toolkit/mozapps/downloads/tests/chrome/test_search_clearlist.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_search_clearlist.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,145 @@
     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 + * Test bug 431188 to make sure the Clear list button is enabled after doing a
    1.10 + * search and finding results.
    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="utils.js"/>
    1.21 +
    1.22 +  <script type="application/javascript">
    1.23 +  <![CDATA[
    1.24 +
    1.25 +function test()
    1.26 +{
    1.27 +  var dmui = getDMUI();
    1.28 +  if (!dmui) {
    1.29 +    todo(false, "skip test for toolkit download manager UI");
    1.30 +    return;
    1.31 +  }
    1.32 +
    1.33 +  let dm = Cc["@mozilla.org/download-manager;1"].
    1.34 +           getService(Ci.nsIDownloadManager);
    1.35 +  let db = dm.DBConnection;
    1.36 +
    1.37 +  // Empty any old downloads
    1.38 +  db.executeSimpleSQL("DELETE FROM moz_downloads");
    1.39 +
    1.40 +  // Make a file name for the downloads
    1.41 +  let file = Cc["@mozilla.org/file/directory_service;1"].
    1.42 +             getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
    1.43 +  file.append("cleanUp");
    1.44 +  let filePath = Cc["@mozilla.org/network/io-service;1"].
    1.45 +                 getService(Ci.nsIIOService).newFileURI(file).spec;
    1.46 +
    1.47 +  let stmt = db.createStatement(
    1.48 +    "INSERT INTO moz_downloads (target, source, state, endTime) " +
    1.49 +    "VALUES (?1, ?2, ?3, ?4)");
    1.50 +
    1.51 +  // Add a bunch of downloads that don't match the search
    1.52 +  let sites = [];
    1.53 +  for (let i = 0; i < 50; i++)
    1.54 +    sites.push("i-hate.clear-list-" + i);
    1.55 +
    1.56 +  // Add one download that matches the search
    1.57 +  let searchTerm = "one-download.match-search";
    1.58 +  sites.push(searchTerm);
    1.59 +
    1.60 +  try {
    1.61 +    for each (let site in sites) {
    1.62 +      stmt.bindStringParameter(0, filePath);
    1.63 +      stmt.bindStringParameter(1, "http://" + site + "/file");
    1.64 +      stmt.bindInt32Parameter(2, dm.DOWNLOAD_FINISHED);
    1.65 +      // Make the one that matches slightly older so it appears last
    1.66 +      stmt.bindInt64Parameter(3, 1112223334445556 - (site == searchTerm));
    1.67 +
    1.68 +      // Add it!
    1.69 +      stmt.execute();
    1.70 +    }
    1.71 +  }
    1.72 +  finally {
    1.73 +    stmt.reset();
    1.74 +    stmt.finalize();
    1.75 +  }
    1.76 +
    1.77 +  // Close the UI if necessary
    1.78 +  let wm = Cc["@mozilla.org/appshell/window-mediator;1"].
    1.79 +           getService(Ci.nsIWindowMediator);
    1.80 +  let win = wm.getMostRecentWindow("Download:Manager");
    1.81 +  if (win) win.close();
    1.82 +
    1.83 +  let obs = Cc["@mozilla.org/observer-service;1"].
    1.84 +            getService(Ci.nsIObserverService);
    1.85 +  const DLMGR_UI_DONE = "download-manager-ui-done";
    1.86 +
    1.87 +  let testPhase = 0;
    1.88 +  let testObs = {
    1.89 +    observe: function(aSubject, aTopic, aData) {
    1.90 +      if (aTopic != DLMGR_UI_DONE)
    1.91 +        return;
    1.92 +
    1.93 +      let win = aSubject.QueryInterface(Ci.nsIDOMWindow);
    1.94 +      win.focus();
    1.95 +      let $ = function(aId) win.document.getElementById(aId);
    1.96 +      let downloadView = $("downloadView");
    1.97 +      let searchbox = $("searchbox");
    1.98 +      let clearList = $("clearListButton");
    1.99 +
   1.100 +      // The list must have built, so figure out what test to do
   1.101 +      switch (testPhase++) {
   1.102 +        case 0:
   1.103 +        case 2:
   1.104 +          // Search for the one download
   1.105 +          searchbox.value = searchTerm;
   1.106 +          searchbox.doCommand();
   1.107 +
   1.108 +          break;
   1.109 +        case 1:
   1.110 +          // Search came back with 1 item
   1.111 +          is(downloadView.itemCount, 1, "Search found the item to delete");
   1.112 +          is(clearList.disabled, false, "Clear list is enabled for search matching 1 item");
   1.113 +
   1.114 +          // Clear the list that has the single matched item
   1.115 +          clearList.doCommand();
   1.116 +
   1.117 +          break;
   1.118 +        case 3:
   1.119 +          // There's nothing that matches the search
   1.120 +          is(downloadView.itemCount, 0, "Clear list killed the one matching download");
   1.121 +          is(clearList.disabled, true, "Clear list is disabled for no items");
   1.122 +
   1.123 +          // We're done!
   1.124 +          win.close();
   1.125 +          obs.removeObserver(testObs, DLMGR_UI_DONE);
   1.126 +          SimpleTest.finish();
   1.127 +
   1.128 +          break;
   1.129 +      }
   1.130 +    }
   1.131 +  };
   1.132 +  obs.addObserver(testObs, DLMGR_UI_DONE, false);
   1.133 +
   1.134 +  // Show the Download Manager UI
   1.135 +  dmui.show();
   1.136 +
   1.137 +  SimpleTest.waitForExplicitFinish();
   1.138 +}
   1.139 +
   1.140 +  ]]>
   1.141 +  </script>
   1.142 +
   1.143 +  <body xmlns="http://www.w3.org/1999/xhtml">
   1.144 +    <p id="display"></p>
   1.145 +    <div id="content" style="display:none;"></div>
   1.146 +    <pre id="test"></pre>
   1.147 +  </body>
   1.148 +</window>

mercurial