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 download manager UI closes upon download completion when |
michael@0 | 7 | * browser.download.manager.retention is set to 0. This test was added in bug |
michael@0 | 8 | * 413093. |
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="utils.js"/> |
michael@0 | 19 | |
michael@0 | 20 | <script type="application/javascript"> |
michael@0 | 21 | <![CDATA[ |
michael@0 | 22 | |
michael@0 | 23 | function setPref(aDoTest) |
michael@0 | 24 | { |
michael@0 | 25 | let prefs = Cc["@mozilla.org/preferences-service;1"]. |
michael@0 | 26 | getService(Ci.nsIPrefBranch); |
michael@0 | 27 | |
michael@0 | 28 | // If we're testing, set retention to auto-remove and auto-close |
michael@0 | 29 | prefs.setIntPref("browser.download.manager.retention", aDoTest ? 0 : 2); |
michael@0 | 30 | prefs.setBoolPref("browser.download.manager.closeWhenDone", aDoTest); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | function bug413093obs() |
michael@0 | 34 | { |
michael@0 | 35 | this.mDownload = null; |
michael@0 | 36 | this.wasPaused = false; |
michael@0 | 37 | } |
michael@0 | 38 | bug413093obs.prototype = { |
michael@0 | 39 | observe: function(aSubject, aTopic, aData) |
michael@0 | 40 | { |
michael@0 | 41 | if ("domwindowopened" == aTopic) { |
michael@0 | 42 | // If we opened before we were paused, we need to set up our proper state |
michael@0 | 43 | // We also should not try to resume (we weren't paused!) |
michael@0 | 44 | if (!this.wasPaused) { |
michael@0 | 45 | dump("domwindowopened callback - not pausing or resuming\n"); |
michael@0 | 46 | this.wasPaused = true; |
michael@0 | 47 | return; |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | dump("domwindowopened callback - resuming download\n"); |
michael@0 | 51 | |
michael@0 | 52 | // Resume the download now that UI is up and running |
michael@0 | 53 | let dm = Cc["@mozilla.org/download-manager;1"]. |
michael@0 | 54 | getService(Ci.nsIDownloadManager); |
michael@0 | 55 | dm.resumeDownload(this.mDownload.id); |
michael@0 | 56 | } |
michael@0 | 57 | else if("domwindowclosed" == aTopic) { |
michael@0 | 58 | let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"]. |
michael@0 | 59 | getService(Ci.nsIWindowWatcher); |
michael@0 | 60 | ww.unregisterNotification(this); |
michael@0 | 61 | |
michael@0 | 62 | // Let the UI finish doing whatever it needs to do |
michael@0 | 63 | SimpleTest.executeSoon(function() { |
michael@0 | 64 | setPref(false); |
michael@0 | 65 | |
michael@0 | 66 | let dmui = Cc["@mozilla.org/download-manager-ui;1"]. |
michael@0 | 67 | getService(Ci.nsIDownloadManagerUI); |
michael@0 | 68 | ok(!dmui.visible, "Download Manager UI is not showing"); |
michael@0 | 69 | |
michael@0 | 70 | // reset the changed config values to default value |
michael@0 | 71 | let prefs = Cc["@mozilla.org/preferences-service;1"]. |
michael@0 | 72 | getService(Ci.nsIPrefService); |
michael@0 | 73 | prefs.clearUserPref("browser.download.manager.retention"); |
michael@0 | 74 | prefs.clearUserPref("browser.download.manager.closeWhenDone"); |
michael@0 | 75 | |
michael@0 | 76 | SimpleTest.finish(); |
michael@0 | 77 | }); |
michael@0 | 78 | } |
michael@0 | 79 | }, |
michael@0 | 80 | |
michael@0 | 81 | onDownloadStateChange: function(aOldState, aDownload) |
michael@0 | 82 | { |
michael@0 | 83 | if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING && |
michael@0 | 84 | !this.wasPaused) { |
michael@0 | 85 | dump("onDownloadStateChange - pausing download\n"); |
michael@0 | 86 | this.wasPaused = true; |
michael@0 | 87 | |
michael@0 | 88 | // Pause the download until the UI shows up |
michael@0 | 89 | let dm = Cc["@mozilla.org/download-manager;1"]. |
michael@0 | 90 | getService(Ci.nsIDownloadManager); |
michael@0 | 91 | dm.pauseDownload(aDownload.id); |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | if (aDownload.state == Ci.nsIDownloadManager.DOWNLOAD_FINISHED) { |
michael@0 | 95 | aDownload.targetFile.remove(false); |
michael@0 | 96 | |
michael@0 | 97 | let dm = Cc["@mozilla.org/download-manager;1"]. |
michael@0 | 98 | getService(Ci.nsIDownloadManager); |
michael@0 | 99 | dm.removeListener(this); |
michael@0 | 100 | |
michael@0 | 101 | // If this test is going to pass, we'll get a domwindowclosed notification |
michael@0 | 102 | } |
michael@0 | 103 | }, |
michael@0 | 104 | onStateChange: function(a, b, c, d, e) { }, |
michael@0 | 105 | onProgressChange: function(a, b, c, d, e, f, g) { }, |
michael@0 | 106 | onSecurityChange: function(a, b, c, d) { }, |
michael@0 | 107 | }; |
michael@0 | 108 | function test() |
michael@0 | 109 | { |
michael@0 | 110 | function addDownload() { |
michael@0 | 111 | function createURI(aObj) { |
michael@0 | 112 | let ios = Cc["@mozilla.org/network/io-service;1"]. |
michael@0 | 113 | getService(Ci.nsIIOService); |
michael@0 | 114 | return (aObj instanceof Ci.nsIFile) ? ios.newFileURI(aObj) : |
michael@0 | 115 | ios.newURI(aObj, null, null); |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | const nsIWBP = Ci.nsIWebBrowserPersist; |
michael@0 | 119 | let persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"] |
michael@0 | 120 | .createInstance(Ci.nsIWebBrowserPersist); |
michael@0 | 121 | persist.persistFlags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES | |
michael@0 | 122 | nsIWBP.PERSIST_FLAGS_BYPASS_CACHE | |
michael@0 | 123 | nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION; |
michael@0 | 124 | |
michael@0 | 125 | let dirSvc = Cc["@mozilla.org/file/directory_service;1"]. |
michael@0 | 126 | getService(Ci.nsIProperties); |
michael@0 | 127 | let destFile = dirSvc.get("ProfD", Ci.nsIFile); |
michael@0 | 128 | destFile.append("download.result"); |
michael@0 | 129 | if (destFile.exists()) |
michael@0 | 130 | destFile.remove(false); |
michael@0 | 131 | |
michael@0 | 132 | let src = createURI("http://example.com/httpd.js"); |
michael@0 | 133 | let target = createURI(destFile); |
michael@0 | 134 | let tr = Cc["@mozilla.org/transfer;1"].createInstance(Ci.nsITransfer); |
michael@0 | 135 | tr.init(src, target, "test download", null, Math.round(Date.now() * 1000), |
michael@0 | 136 | null, persist, false); |
michael@0 | 137 | persist.progressListener = tr; |
michael@0 | 138 | persist.saveURI(src, null, null, null, null, destFile); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | // First, we clear out the database |
michael@0 | 142 | let dm = Cc["@mozilla.org/download-manager;1"]. |
michael@0 | 143 | getService(Ci.nsIDownloadManager); |
michael@0 | 144 | dm.DBConnection.executeSimpleSQL("DELETE FROM moz_downloads"); |
michael@0 | 145 | |
michael@0 | 146 | // See if the DM is already open, and if it is, close it! |
michael@0 | 147 | let wm = Cc["@mozilla.org/appshell/window-mediator;1"]. |
michael@0 | 148 | getService(Ci.nsIWindowMediator); |
michael@0 | 149 | let win = wm.getMostRecentWindow("Download:Manager"); |
michael@0 | 150 | if (win) |
michael@0 | 151 | win.close(); |
michael@0 | 152 | |
michael@0 | 153 | let obs = new bug413093obs(); |
michael@0 | 154 | dm.addListener(obs); |
michael@0 | 155 | setPref(true); |
michael@0 | 156 | |
michael@0 | 157 | // Start the test when the download manager window loads |
michael@0 | 158 | let ww = Cc["@mozilla.org/embedcomp/window-watcher;1"]. |
michael@0 | 159 | getService(Ci.nsIWindowWatcher); |
michael@0 | 160 | ww.registerNotification(obs); |
michael@0 | 161 | |
michael@0 | 162 | addDownload(); |
michael@0 | 163 | |
michael@0 | 164 | SimpleTest.waitForExplicitFinish(); |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | ]]> |
michael@0 | 168 | </script> |
michael@0 | 169 | |
michael@0 | 170 | <body xmlns="http://www.w3.org/1999/xhtml"> |
michael@0 | 171 | <p id="display"></p> |
michael@0 | 172 | <div id="content" style="display:none;"></div> |
michael@0 | 173 | <pre id="test"></pre> |
michael@0 | 174 | </body> |
michael@0 | 175 | </window> |