1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/mozapps/downloads/tests/chrome/test_bug_429247.xul Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,144 @@ 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 429247 to make sure clicking on whitespace in the Download 1.10 + * Manager doesn't run the default action on the selected download. 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="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/> 1.21 + <script type="application/javascript" 1.22 + src="utils.js"/> 1.23 + 1.24 + <script type="application/javascript"> 1.25 + <![CDATA[ 1.26 + 1.27 +function test() 1.28 +{ 1.29 + var dmui = getDMUI(); 1.30 + if (!dmui) { 1.31 + todo(false, "skip test for toolkit download manager UI"); 1.32 + return; 1.33 + } 1.34 + 1.35 + let ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); 1.36 + let dmFile = Cc["@mozilla.org/file/directory_service;1"]. 1.37 + getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile); 1.38 + dmFile.append("dm-ui-test.file"); 1.39 + dmFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); 1.40 + let gTestPath = ios.newFileURI(dmFile).spec; 1.41 + 1.42 + // Data to populate the download manager with 1.43 + const DownloadData = { 1.44 + name: "381603.patch", 1.45 + source: "https://bugzilla.mozilla.org/attachment.cgi?id=266520", 1.46 + target: gTestPath, 1.47 + startTime: 1180493839859230, 1.48 + endTime: 1180493839859239, 1.49 + state: Ci.nsIDownloadManager.DOWNLOAD_FINISHED, 1.50 + currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0 1.51 + } 1.52 + 1.53 + let dm = Cc["@mozilla.org/download-manager;1"]. 1.54 + getService(Ci.nsIDownloadManager); 1.55 + let db = dm.DBConnection; 1.56 + 1.57 + // Empty any old downloads 1.58 + db.executeSimpleSQL("DELETE FROM moz_downloads"); 1.59 + 1.60 + let stmt = db.createStatement( 1.61 + "INSERT INTO moz_downloads (name, source, target, startTime, endTime, " + 1.62 + "state, currBytes, maxBytes, preferredAction, autoResume) " + 1.63 + "VALUES (:name, :source, :target, :startTime, :endTime, :state, " + 1.64 + ":currBytes, :maxBytes, :preferredAction, :autoResume)"); 1.65 + for (let prop in DownloadData) 1.66 + stmt.params[prop] = DownloadData[prop]; 1.67 + 1.68 + stmt.execute(); 1.69 + stmt.finalize(); 1.70 + 1.71 + // Close the UI if necessary 1.72 + let wm = Cc["@mozilla.org/appshell/window-mediator;1"]. 1.73 + getService(Ci.nsIWindowMediator); 1.74 + let win = wm.getMostRecentWindow("Download:Manager"); 1.75 + if (win) win.close(); 1.76 + 1.77 + let obs = Cc["@mozilla.org/observer-service;1"]. 1.78 + getService(Ci.nsIObserverService); 1.79 + const DLMGR_UI_DONE = "download-manager-ui-done"; 1.80 + 1.81 + let testObs = { 1.82 + observe: function(aSubject, aTopic, aData) { 1.83 + if (aTopic != DLMGR_UI_DONE) 1.84 + return; 1.85 + 1.86 + let win = aSubject.QueryInterface(Ci.nsIDOMWindow); 1.87 + win.focus(); 1.88 + let $ = function(aId) win.document.getElementById(aId); 1.89 + let downloadView = $("downloadView"); 1.90 + 1.91 + // Default test/check for invocations 1.92 + let invokeCount = 0; 1.93 + let counter = function() invokeCount++; 1.94 + 1.95 + // Array of tests that consist of the download manager 1.96 + // function to temporarily replace, method to use in its place, value to 1.97 + // use when checking correctness 1.98 + let defaultForSelectedTest = ["doDefaultForSelected", counter, counter]; 1.99 + 1.100 + // All the expected results 1.101 + let allExpected = [0, "The default action was not performed"]; 1.102 + 1.103 + // Select the first download 1.104 + downloadView.selectedIndex = 0; 1.105 + 1.106 + let [func, test, value] = defaultForSelectedTest; 1.107 + // Make a copy of the original function and replace it with a test 1.108 + let copy = win[func]; 1.109 + win[func] = test; 1.110 + 1.111 + // Get the offsetY of the whitespace under the download 1.112 + let downloadItem = downloadView.getItemAtIndex(0); 1.113 + let boxOffset = downloadItem.boxObject.height + 1; 1.114 + // The last download in the download manager + 1 is whitespace 1.115 + synthesizeMouse(downloadItem, 0, boxOffset, {clickCount: 2}, win); 1.116 + 1.117 + // Make sure the value is as expected 1.118 + let [correct, message] = allExpected; 1.119 + ok(value() == correct, message); 1.120 + 1.121 + // Restore original values 1.122 + invokeCount = 0; 1.123 + win[func] = copy; 1.124 + 1.125 + // We're done! 1.126 + win.close(); 1.127 + obs.removeObserver(testObs, DLMGR_UI_DONE); 1.128 + SimpleTest.finish(); 1.129 + } 1.130 + }; 1.131 + obs.addObserver(testObs, DLMGR_UI_DONE, false); 1.132 + 1.133 + // Show the Download Manager UI 1.134 + dmui.show(); 1.135 + 1.136 + SimpleTest.waitForExplicitFinish(); 1.137 +} 1.138 + 1.139 + ]]> 1.140 + </script> 1.141 + 1.142 + <body xmlns="http://www.w3.org/1999/xhtml"> 1.143 + <p id="display"></p> 1.144 + <div id="content" style="display:none;"></div> 1.145 + <pre id="test"></pre> 1.146 + </body> 1.147 +</window>