toolkit/mozapps/downloads/tests/chrome/test_multi_select.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.

michael@0 1 <?xml version="1.0"?>
michael@0 2 <!-- This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 - License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
michael@0 5 <!--
michael@0 6 * Test bug 228842 to make sure multiple selections work in the download
michael@0 7 * manager by making sure commands work as expected for both single and doubly
michael@0 8 * selected items.
michael@0 9 -->
michael@0 10
michael@0 11 <window title="Download Manager Test"
michael@0 12 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
michael@0 13 onload="test();">
michael@0 14
michael@0 15 <script type="application/javascript"
michael@0 16 src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
michael@0 17 <script type="application/javascript"
michael@0 18 src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
michael@0 19 <script type="application/javascript"
michael@0 20 src="utils.js"/>
michael@0 21
michael@0 22 <script type="application/javascript">
michael@0 23 <![CDATA[
michael@0 24
michael@0 25 function test()
michael@0 26 {
michael@0 27 var dmui = getDMUI();
michael@0 28 if (!dmui) {
michael@0 29 todo(false, "skip test for toolkit download manager UI");
michael@0 30 return;
michael@0 31 }
michael@0 32
michael@0 33 let dm = Cc["@mozilla.org/download-manager;1"].
michael@0 34 getService(Ci.nsIDownloadManager);
michael@0 35 let db = dm.DBConnection;
michael@0 36
michael@0 37 // Empty any old downloads
michael@0 38 db.executeSimpleSQL("DELETE FROM moz_downloads");
michael@0 39
michael@0 40 let stmt = db.createStatement(
michael@0 41 "INSERT INTO moz_downloads (source, state, target, referrer) " +
michael@0 42 "VALUES (?1, ?2, ?3, ?4)");
michael@0 43
michael@0 44 try {
michael@0 45 for each (let site in ["ed.agadak.net", "mozilla.org", "mozilla.com", "mozilla.net"]) {
michael@0 46 let file = Cc["@mozilla.org/file/directory_service;1"].
michael@0 47 getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile);
michael@0 48 file.append(site);
michael@0 49 let fileSpec = Cc["@mozilla.org/network/io-service;1"].
michael@0 50 getService(Ci.nsIIOService).newFileURI(file).spec;
michael@0 51
michael@0 52 stmt.bindStringParameter(0, "http://" + site + "/file");
michael@0 53 stmt.bindInt32Parameter(1, dm.DOWNLOAD_FINISHED);
michael@0 54 stmt.bindStringParameter(2, fileSpec);
michael@0 55 stmt.bindStringParameter(3, "http://referrer/");
michael@0 56
michael@0 57 // Add it!
michael@0 58 stmt.execute();
michael@0 59 }
michael@0 60 }
michael@0 61 finally {
michael@0 62 stmt.reset();
michael@0 63 stmt.finalize();
michael@0 64 }
michael@0 65
michael@0 66 // Close the UI if necessary
michael@0 67 let wm = Cc["@mozilla.org/appshell/window-mediator;1"].
michael@0 68 getService(Ci.nsIWindowMediator);
michael@0 69 let win = wm.getMostRecentWindow("Download:Manager");
michael@0 70 if (win) win.close();
michael@0 71
michael@0 72 let obs = Cc["@mozilla.org/observer-service;1"].
michael@0 73 getService(Ci.nsIObserverService);
michael@0 74 const DLMGR_UI_DONE = "download-manager-ui-done";
michael@0 75
michael@0 76 let testPhase = 0;
michael@0 77 let testObs = {
michael@0 78 observe: function(aSubject, aTopic, aData) {
michael@0 79 if (aTopic != DLMGR_UI_DONE)
michael@0 80 return;
michael@0 81
michael@0 82 SimpleTest.waitForFocus(function () { continueTest(aSubject) }, aSubject);
michael@0 83 }
michael@0 84 };
michael@0 85
michael@0 86 function getLoadContext() {
michael@0 87 return window.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 88 .getInterface(Ci.nsIWebNavigation)
michael@0 89 .QueryInterface(Ci.nsILoadContext);
michael@0 90 }
michael@0 91
michael@0 92 function continueTest(win) {
michael@0 93 let $ = function(aId) win.document.getElementById(aId);
michael@0 94 let downloadView = $("downloadView");
michael@0 95
michael@0 96 // Default test/check for invocations
michael@0 97 let invokeCount = 0;
michael@0 98 let counter = function() invokeCount++;
michael@0 99
michael@0 100 // Accessors for returning a value for various properties
michael@0 101 let getItems = function() downloadView.itemCount;
michael@0 102 let getSelected = function() downloadView.selectedCount;
michael@0 103 let getClipboard = function() {
michael@0 104 let clip = Cc["@mozilla.org/widget/clipboard;1"].
michael@0 105 getService(Ci.nsIClipboard);
michael@0 106 let trans = Cc["@mozilla.org/widget/transferable;1"].
michael@0 107 createInstance(Ci.nsITransferable);
michael@0 108 trans.init(getLoadContext());
michael@0 109 trans.addDataFlavor("text/unicode");
michael@0 110 clip.getData(trans, clip.kGlobalClipboard);
michael@0 111 let str = {};
michael@0 112 let strLen = {};
michael@0 113 trans.getTransferData("text/unicode", str, strLen);
michael@0 114 return str.value.QueryInterface(Ci.nsISupportsString).data.
michael@0 115 substring(0, strLen.value / 2);
michael@0 116 };
michael@0 117
michael@0 118 // Array of tests that consist of the command name, download manager
michael@0 119 // function to temporarily replace, method to use in its place, value to
michael@0 120 // use when checking correctness
michael@0 121 let commandTests = [
michael@0 122 ["pause", "pauseDownload", counter, counter],
michael@0 123 ["resume", "resumeDownload", counter, counter],
michael@0 124 ["cancel", "cancelDownload", counter, counter],
michael@0 125 ["open", "openDownload", counter, counter],
michael@0 126 ["show", "showDownload", counter, counter],
michael@0 127 ["retry", "retryDownload", counter, counter],
michael@0 128 ["openReferrer", "openReferrer", counter, counter],
michael@0 129 ["copyLocation", null, null, getClipboard],
michael@0 130 ["removeFromList", null, null, getItems],
michael@0 131 ["selectAll", null, null, getSelected],
michael@0 132 ];
michael@0 133
michael@0 134 // All the expected results for both single and double selections
michael@0 135 let allExpected = {
michael@0 136 single: {
michael@0 137 pause: [0, "Paused no downloads"],
michael@0 138 resume: [0, "Resumed no downloads"],
michael@0 139 cancel: [0, "Canceled no downloads"],
michael@0 140 open: [0, "Opened no downloads"],
michael@0 141 show: [0, "Showed no downloads"],
michael@0 142 retry: [1, "Retried one download"],
michael@0 143 openReferrer: [1, "Opened one referrer"],
michael@0 144 copyLocation: ["http://ed.agadak.net/file", "Copied one location"],
michael@0 145 removeFromList: [3, "Removed one downloads, remaining 3"],
michael@0 146 selectAll: [3, "Selected all 3 remaining downloads"],
michael@0 147 },
michael@0 148 double: {
michael@0 149 pause: [0, "Paused neither download"],
michael@0 150 resume: [0, "Resumed neither download"],
michael@0 151 cancel: [0, "Canceled neither download"],
michael@0 152 open: [0, "Opened neither download"],
michael@0 153 show: [0, "Showed neither download"],
michael@0 154 retry: [2, "Retried both downloads"],
michael@0 155 openReferrer: [2, "Opened both referrers"],
michael@0 156 copyLocation: ["http://mozilla.org/file\nhttp://mozilla.com/file", "Copied both locations"],
michael@0 157 removeFromList: [1, "Removed both downloads, remaining 1"],
michael@0 158 selectAll: [1, "Selected the 1 remaining download"],
michael@0 159 },
michael@0 160 };
michael@0 161
michael@0 162 // Run two tests: single selected, double selected
michael@0 163 for each (let whichTest in ["single", "double"]) {
michael@0 164 let expected = allExpected[whichTest];
michael@0 165
michael@0 166 // Select the first download
michael@0 167 downloadView.selectedIndex = 0;
michael@0 168
michael@0 169 // Select 2 downloads for double
michael@0 170 if (whichTest == "double")
michael@0 171 synthesizeKey("VK_DOWN", { shiftKey: true }, win);
michael@0 172
michael@0 173 for each (let [command, func, test, value] in commandTests) {
michael@0 174 // Make a copy of the original function and replace it with a test
michael@0 175 let copy;
michael@0 176 [copy, win[func]] = [win[func], test];
michael@0 177
michael@0 178 // Run the command from the menu
michael@0 179 $("menuitem_" + command).doCommand();
michael@0 180
michael@0 181 // Make sure the value is as expected
michael@0 182 let [correct, message] = expected[command];
michael@0 183 ok(value() == correct, message);
michael@0 184
michael@0 185 // Restore original values
michael@0 186 invokeCount = 0;
michael@0 187 win[func] = copy;
michael@0 188 }
michael@0 189 }
michael@0 190
michael@0 191 // We're done!
michael@0 192 win.close();
michael@0 193 obs.removeObserver(testObs, DLMGR_UI_DONE);
michael@0 194 SimpleTest.finish();
michael@0 195 };
michael@0 196 obs.addObserver(testObs, DLMGR_UI_DONE, false);
michael@0 197
michael@0 198 // Show the Download Manager UI
michael@0 199 dmui.show();
michael@0 200
michael@0 201 SimpleTest.waitForExplicitFinish();
michael@0 202 }
michael@0 203
michael@0 204 ]]>
michael@0 205 </script>
michael@0 206
michael@0 207 <body xmlns="http://www.w3.org/1999/xhtml">
michael@0 208 <p id="display"></p>
michael@0 209 <div id="content" style="display:none;"></div>
michael@0 210 <pre id="test"></pre>
michael@0 211 </body>
michael@0 212 </window>

mercurial