Wed, 31 Dec 2014 06:09:35 +0100
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 | * This tests that the space key will pause and resume a download in the UI. |
michael@0 | 7 | * This test was added in bug 413985. |
michael@0 | 8 | --> |
michael@0 | 9 | |
michael@0 | 10 | <window title="Download Manager Test" |
michael@0 | 11 | xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" |
michael@0 | 12 | onload="test();"> |
michael@0 | 13 | |
michael@0 | 14 | <script type="application/javascript" |
michael@0 | 15 | src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/> |
michael@0 | 16 | <script type="application/javascript" |
michael@0 | 17 | src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/> |
michael@0 | 18 | <script type="application/javascript" |
michael@0 | 19 | src="utils.js"/> |
michael@0 | 20 | |
michael@0 | 21 | <script type="application/javascript"> |
michael@0 | 22 | <![CDATA[ |
michael@0 | 23 | |
michael@0 | 24 | function bug413985obs(aWin) |
michael@0 | 25 | { |
michael@0 | 26 | this.mWin = aWin; |
michael@0 | 27 | this.wasPaused = false; |
michael@0 | 28 | this.wasResumed = false; |
michael@0 | 29 | this.timer = null; // timer declared here to prevent premature GC |
michael@0 | 30 | } |
michael@0 | 31 | bug413985obs.prototype = { |
michael@0 | 32 | observe: function(aSubject, aTopic, aData) |
michael@0 | 33 | { |
michael@0 | 34 | if ("timer-callback" == aTopic) { |
michael@0 | 35 | // dispatch a space keypress to resume the download |
michael@0 | 36 | synthesizeKey(" ", {}, this.mWin); |
michael@0 | 37 | } |
michael@0 | 38 | }, |
michael@0 | 39 | |
michael@0 | 40 | onDownloadStateChange: function(aState, aDownload) |
michael@0 | 41 | { |
michael@0 | 42 | if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING && |
michael@0 | 43 | !this.wasPaused) { |
michael@0 | 44 | this.wasPaused = true; |
michael@0 | 45 | // dispatch a space keypress to pause the download |
michael@0 | 46 | synthesizeKey(" ", {}, this.mWin); |
michael@0 | 47 | } |
michael@0 | 48 | |
michael@0 | 49 | if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_PAUSED && |
michael@0 | 50 | !this.wasResumed) { |
michael@0 | 51 | this.wasResumed = true; |
michael@0 | 52 | // We have to do this on a timer so other JS stuff that handles the UI |
michael@0 | 53 | // can actually catch up to us... |
michael@0 | 54 | this.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); |
michael@0 | 55 | this.timer.init(this, 0, Ci.nsITimer.TYPE_ONE_SHOT); |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) { |
michael@0 | 59 | ok(this.wasPaused && this.wasResumed, |
michael@0 | 60 | "The download was paused, and then resumed to completion"); |
michael@0 | 61 | aDownload.targetFile.remove(false); |
michael@0 | 62 | |
michael@0 | 63 | var dm = Cc["@mozilla.org/download-manager;1"]. |
michael@0 | 64 | getService(Ci.nsIDownloadManager); |
michael@0 | 65 | dm.removeListener(this); |
michael@0 | 66 | |
michael@0 | 67 | SimpleTest.finish(); |
michael@0 | 68 | } |
michael@0 | 69 | }, |
michael@0 | 70 | onStateChange: function(a, b, c, d, e) { }, |
michael@0 | 71 | onProgressChange: function(a, b, c, d, e, f, g) { }, |
michael@0 | 72 | onSecurityChange: function(a, b, c, d) { } |
michael@0 | 73 | }; |
michael@0 | 74 | function test() |
michael@0 | 75 | { |
michael@0 | 76 | var dmui = getDMUI(); |
michael@0 | 77 | if (!dmui) { |
michael@0 | 78 | todo(false, "skip test for toolkit download manager UI"); |
michael@0 | 79 | return; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | var dm = Cc["@mozilla.org/download-manager;1"]. |
michael@0 | 83 | getService(Ci.nsIDownloadManager); |
michael@0 | 84 | |
michael@0 | 85 | function addDownload() { |
michael@0 | 86 | function createURI(aObj) { |
michael@0 | 87 | var ios = Cc["@mozilla.org/network/io-service;1"]. |
michael@0 | 88 | getService(Ci.nsIIOService); |
michael@0 | 89 | return (aObj instanceof Ci.nsIFile) ? ios.newFileURI(aObj) : |
michael@0 | 90 | ios.newURI(aObj, null, null); |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | const nsIWBP = Ci.nsIWebBrowserPersist; |
michael@0 | 94 | var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"] |
michael@0 | 95 | .createInstance(Ci.nsIWebBrowserPersist); |
michael@0 | 96 | persist.persistFlags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES | |
michael@0 | 97 | nsIWBP.PERSIST_FLAGS_BYPASS_CACHE | |
michael@0 | 98 | nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION; |
michael@0 | 99 | |
michael@0 | 100 | var dirSvc = Cc["@mozilla.org/file/directory_service;1"]. |
michael@0 | 101 | getService(Ci.nsIProperties); |
michael@0 | 102 | var destFile = dirSvc.get("ProfD", Ci.nsIFile); |
michael@0 | 103 | destFile.append("download.result"); |
michael@0 | 104 | if (destFile.exists()) |
michael@0 | 105 | destFile.remove(false); |
michael@0 | 106 | |
michael@0 | 107 | var dl = dm.addDownload(Ci.nsIDownloadManager.DOWNLOAD_TYPE_DOWNLOAD, |
michael@0 | 108 | createURI("http://example.com/httpd.js"), |
michael@0 | 109 | createURI(destFile), null, null, |
michael@0 | 110 | Math.round(Date.now() * 1000), null, persist, false); |
michael@0 | 111 | |
michael@0 | 112 | var privacyContext = window.QueryInterface(Ci.nsIInterfaceRequestor) |
michael@0 | 113 | .getInterface(Ci.nsIWebNavigation) |
michael@0 | 114 | .QueryInterface(Ci.nsILoadContext); |
michael@0 | 115 | |
michael@0 | 116 | persist.progressListener = dl.QueryInterface(Ci.nsIWebProgressListener); |
michael@0 | 117 | persist.saveURI(dl.source, null, null, null, null, dl.targetFile, privacyContext); |
michael@0 | 118 | |
michael@0 | 119 | return dl; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | // First, we clear out the database |
michael@0 | 123 | dm.DBConnection.executeSimpleSQL("DELETE FROM moz_downloads"); |
michael@0 | 124 | |
michael@0 | 125 | // See if the DM is already open, and if it is, close it! |
michael@0 | 126 | var wm = Cc["@mozilla.org/appshell/window-mediator;1"]. |
michael@0 | 127 | getService(Ci.nsIWindowMediator); |
michael@0 | 128 | var win = wm.getMostRecentWindow("Download:Manager"); |
michael@0 | 129 | if (win) |
michael@0 | 130 | win.close(); |
michael@0 | 131 | |
michael@0 | 132 | let os = Cc["@mozilla.org/observer-service;1"]. |
michael@0 | 133 | getService(Ci.nsIObserverService); |
michael@0 | 134 | const DLMGR_UI_DONE = "download-manager-ui-done"; |
michael@0 | 135 | |
michael@0 | 136 | let testObs = { |
michael@0 | 137 | observe: function(aSubject, aTopic, aData) |
michael@0 | 138 | { |
michael@0 | 139 | if (aTopic != DLMGR_UI_DONE) |
michael@0 | 140 | return; |
michael@0 | 141 | |
michael@0 | 142 | SimpleTest.waitForFocus(function () { continueTest(aSubject) }, aSubject); |
michael@0 | 143 | } |
michael@0 | 144 | }; |
michael@0 | 145 | |
michael@0 | 146 | var continueTest = function(win) |
michael@0 | 147 | { |
michael@0 | 148 | let doc = win.document; |
michael@0 | 149 | dm.addListener(new bug413985obs(win)); |
michael@0 | 150 | |
michael@0 | 151 | let dl = addDownload(); |
michael@0 | 152 | // we need to focus the download as well |
michael@0 | 153 | doc.getElementById("downloadView").selectedIndex = 0; |
michael@0 | 154 | os.removeObserver(testObs, DLMGR_UI_DONE); |
michael@0 | 155 | }; |
michael@0 | 156 | |
michael@0 | 157 | // Register with the observer service |
michael@0 | 158 | os.addObserver(testObs, DLMGR_UI_DONE, false); |
michael@0 | 159 | |
michael@0 | 160 | // Show the Download Manager UI |
michael@0 | 161 | dmui.show(); |
michael@0 | 162 | |
michael@0 | 163 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | ]]> |
michael@0 | 167 | </script> |
michael@0 | 168 | |
michael@0 | 169 | <body xmlns="http://www.w3.org/1999/xhtml"> |
michael@0 | 170 | <p id="display"></p> |
michael@0 | 171 | <div id="content" style="display:none;"></div> |
michael@0 | 172 | <pre id="test"></pre> |
michael@0 | 173 | </body> |
michael@0 | 174 | </window> |