toolkit/mozapps/downloads/tests/chrome/test_multi_select.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_multi_select.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,212 @@
     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 228842 to make sure multiple selections work in the download
    1.10 + * manager by making sure commands work as expected for both single and doubly
    1.11 + * selected items.
    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="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
    1.22 +  <script type="application/javascript"
    1.23 +          src="utils.js"/>
    1.24 +
    1.25 +  <script type="application/javascript">
    1.26 +  <![CDATA[
    1.27 +
    1.28 +function test()
    1.29 +{
    1.30 +  var dmui = getDMUI();
    1.31 +  if (!dmui) {
    1.32 +    todo(false, "skip test for toolkit download manager UI");
    1.33 +    return;
    1.34 +  }
    1.35 +
    1.36 +  let dm = Cc["@mozilla.org/download-manager;1"].
    1.37 +           getService(Ci.nsIDownloadManager);
    1.38 +  let db = dm.DBConnection;
    1.39 +
    1.40 +  // Empty any old downloads
    1.41 +  db.executeSimpleSQL("DELETE FROM moz_downloads");
    1.42 +
    1.43 +  let stmt = db.createStatement(
    1.44 +    "INSERT INTO moz_downloads (source, state, target, referrer) " +
    1.45 +    "VALUES (?1, ?2, ?3, ?4)");
    1.46 +
    1.47 +  try {
    1.48 +    for each (let site in ["ed.agadak.net", "mozilla.org", "mozilla.com", "mozilla.net"]) {
    1.49 +      let file = Cc["@mozilla.org/file/directory_service;1"].
    1.50 +        getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
    1.51 +      file.append(site);
    1.52 +      let fileSpec = Cc["@mozilla.org/network/io-service;1"].
    1.53 +        getService(Ci.nsIIOService).newFileURI(file).spec;
    1.54 +
    1.55 +      stmt.bindStringParameter(0, "http://" + site + "/file");
    1.56 +      stmt.bindInt32Parameter(1, dm.DOWNLOAD_FINISHED);
    1.57 +      stmt.bindStringParameter(2, fileSpec);
    1.58 +      stmt.bindStringParameter(3, "http://referrer/");
    1.59 +
    1.60 +      // Add it!
    1.61 +      stmt.execute();
    1.62 +    }
    1.63 +  }
    1.64 +  finally {
    1.65 +    stmt.reset();
    1.66 +    stmt.finalize();
    1.67 +  }
    1.68 +
    1.69 +  // Close the UI if necessary
    1.70 +  let wm = Cc["@mozilla.org/appshell/window-mediator;1"].
    1.71 +           getService(Ci.nsIWindowMediator);
    1.72 +  let win = wm.getMostRecentWindow("Download:Manager");
    1.73 +  if (win) win.close();
    1.74 +
    1.75 +  let obs = Cc["@mozilla.org/observer-service;1"].
    1.76 +            getService(Ci.nsIObserverService);
    1.77 +  const DLMGR_UI_DONE = "download-manager-ui-done";
    1.78 +
    1.79 +  let testPhase = 0;
    1.80 +  let testObs = {
    1.81 +    observe: function(aSubject, aTopic, aData) {
    1.82 +      if (aTopic != DLMGR_UI_DONE)
    1.83 +        return;
    1.84 +
    1.85 +      SimpleTest.waitForFocus(function () { continueTest(aSubject) }, aSubject);
    1.86 +    }
    1.87 +  };
    1.88 +
    1.89 +  function getLoadContext() {
    1.90 +    return window.QueryInterface(Ci.nsIInterfaceRequestor)
    1.91 +                 .getInterface(Ci.nsIWebNavigation)
    1.92 +                 .QueryInterface(Ci.nsILoadContext);
    1.93 +  }
    1.94 +
    1.95 +  function continueTest(win) {
    1.96 +    let $ = function(aId) win.document.getElementById(aId);
    1.97 +    let downloadView = $("downloadView");
    1.98 +
    1.99 +    // Default test/check for invocations
   1.100 +    let invokeCount = 0;
   1.101 +    let counter = function() invokeCount++;
   1.102 +
   1.103 +    // Accessors for returning a value for various properties
   1.104 +    let getItems = function() downloadView.itemCount;
   1.105 +    let getSelected = function() downloadView.selectedCount;
   1.106 +    let getClipboard = function() {
   1.107 +      let clip = Cc["@mozilla.org/widget/clipboard;1"].
   1.108 +                 getService(Ci.nsIClipboard);
   1.109 +      let trans = Cc["@mozilla.org/widget/transferable;1"].
   1.110 +                  createInstance(Ci.nsITransferable);
   1.111 +      trans.init(getLoadContext());
   1.112 +      trans.addDataFlavor("text/unicode");
   1.113 +      clip.getData(trans, clip.kGlobalClipboard);
   1.114 +      let str = {};
   1.115 +      let strLen = {};
   1.116 +      trans.getTransferData("text/unicode", str, strLen);
   1.117 +      return str.value.QueryInterface(Ci.nsISupportsString).data.
   1.118 +        substring(0, strLen.value / 2);
   1.119 +    };
   1.120 +
   1.121 +    // Array of tests that consist of the command name, download manager
   1.122 +    // function to temporarily replace, method to use in its place, value to
   1.123 +    // use when checking correctness
   1.124 +    let commandTests = [
   1.125 +      ["pause", "pauseDownload", counter, counter],
   1.126 +      ["resume", "resumeDownload", counter, counter],
   1.127 +      ["cancel", "cancelDownload", counter, counter],
   1.128 +      ["open", "openDownload", counter, counter],
   1.129 +      ["show", "showDownload", counter, counter],
   1.130 +      ["retry", "retryDownload", counter, counter],
   1.131 +      ["openReferrer", "openReferrer", counter, counter],
   1.132 +      ["copyLocation", null, null, getClipboard],
   1.133 +      ["removeFromList", null, null, getItems],
   1.134 +      ["selectAll", null, null, getSelected],
   1.135 +    ];
   1.136 +
   1.137 +    // All the expected results for both single and double selections
   1.138 +    let allExpected = {
   1.139 +      single: {
   1.140 +        pause: [0, "Paused no downloads"],
   1.141 +        resume: [0, "Resumed no downloads"],
   1.142 +        cancel: [0, "Canceled no downloads"],
   1.143 +        open: [0, "Opened no downloads"],
   1.144 +        show: [0, "Showed no downloads"],
   1.145 +        retry: [1, "Retried one download"],
   1.146 +        openReferrer: [1, "Opened one referrer"],
   1.147 +        copyLocation: ["http://ed.agadak.net/file", "Copied one location"],
   1.148 +        removeFromList: [3, "Removed one downloads, remaining 3"],
   1.149 +        selectAll: [3, "Selected all 3 remaining downloads"],
   1.150 +      },
   1.151 +      double: {
   1.152 +        pause: [0, "Paused neither download"],
   1.153 +        resume: [0, "Resumed neither download"],
   1.154 +        cancel: [0, "Canceled neither download"],
   1.155 +        open: [0, "Opened neither download"],
   1.156 +        show: [0, "Showed neither download"],
   1.157 +        retry: [2, "Retried both downloads"],
   1.158 +        openReferrer: [2, "Opened both referrers"],
   1.159 +        copyLocation: ["http://mozilla.org/file\nhttp://mozilla.com/file", "Copied both locations"],
   1.160 +        removeFromList: [1, "Removed both downloads, remaining 1"],
   1.161 +        selectAll: [1, "Selected the 1 remaining download"],
   1.162 +      },
   1.163 +    };
   1.164 +
   1.165 +    // Run two tests: single selected, double selected
   1.166 +    for each (let whichTest in ["single", "double"]) {
   1.167 +      let expected = allExpected[whichTest];
   1.168 +
   1.169 +      // Select the first download
   1.170 +      downloadView.selectedIndex = 0;
   1.171 +
   1.172 +      // Select 2 downloads for double
   1.173 +      if (whichTest == "double")
   1.174 +        synthesizeKey("VK_DOWN", { shiftKey: true }, win);
   1.175 +
   1.176 +      for each (let [command, func, test, value] in commandTests) {
   1.177 +        // Make a copy of the original function and replace it with a test
   1.178 +        let copy;
   1.179 +        [copy, win[func]] = [win[func], test];
   1.180 +
   1.181 +        // Run the command from the menu
   1.182 +        $("menuitem_" + command).doCommand();
   1.183 +
   1.184 +        // Make sure the value is as expected
   1.185 +        let [correct, message] = expected[command];
   1.186 +        ok(value() == correct, message);
   1.187 +
   1.188 +        // Restore original values
   1.189 +        invokeCount = 0;
   1.190 +        win[func] = copy;
   1.191 +      }
   1.192 +    }
   1.193 +
   1.194 +    // We're done!
   1.195 +    win.close();
   1.196 +    obs.removeObserver(testObs, DLMGR_UI_DONE);
   1.197 +    SimpleTest.finish();
   1.198 +  };
   1.199 +  obs.addObserver(testObs, DLMGR_UI_DONE, false);
   1.200 +
   1.201 +  // Show the Download Manager UI
   1.202 +  dmui.show();
   1.203 +
   1.204 +  SimpleTest.waitForExplicitFinish();
   1.205 +}
   1.206 +
   1.207 +  ]]>
   1.208 +  </script>
   1.209 +
   1.210 +  <body xmlns="http://www.w3.org/1999/xhtml">
   1.211 +    <p id="display"></p>
   1.212 +    <div id="content" style="display:none;"></div>
   1.213 +    <pre id="test"></pre>
   1.214 +  </body>
   1.215 +</window>

mercurial