toolkit/components/downloads/test/unit/test_privatebrowsing_cancel.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /**
michael@0 6 Make sure that the download manager service is given a chance to cancel the
michael@0 7 private browsing mode transition.
michael@0 8 **/
michael@0 9
michael@0 10 const Cm = Components.manager;
michael@0 11
michael@0 12 const kPromptServiceUUID = "{6cc9c9fe-bc0b-432b-a410-253ef8bcc699}";
michael@0 13 const kPromptServiceContractID = "@mozilla.org/embedcomp/prompt-service;1";
michael@0 14
michael@0 15 // Save original prompt service factory
michael@0 16 const kPromptServiceFactory = Cm.getClassObject(Cc[kPromptServiceContractID],
michael@0 17 Ci.nsIFactory);
michael@0 18
michael@0 19 let fakePromptServiceFactory = {
michael@0 20 createInstance: function(aOuter, aIid) {
michael@0 21 if (aOuter != null)
michael@0 22 throw Cr.NS_ERROR_NO_AGGREGATION;
michael@0 23 return promptService.QueryInterface(aIid);
michael@0 24 }
michael@0 25 };
michael@0 26
michael@0 27 let promptService = {
michael@0 28 _buttonChoice: 0,
michael@0 29 _called: false,
michael@0 30 wasCalled: function() {
michael@0 31 let called = this._called;
michael@0 32 this._called = false;
michael@0 33 return called;
michael@0 34 },
michael@0 35 sayCancel: function() {
michael@0 36 this._buttonChoice = 1;
michael@0 37 this._called = false;
michael@0 38 },
michael@0 39 sayProceed: function() {
michael@0 40 this._buttonChoice = 0;
michael@0 41 this._called = false;
michael@0 42 },
michael@0 43 QueryInterface: function(aIID) {
michael@0 44 if (aIID.equals(Ci.nsIPromptService) ||
michael@0 45 aIID.equals(Ci.nsISupports)) {
michael@0 46 return this;
michael@0 47 }
michael@0 48 throw Cr.NS_ERROR_NO_INTERFACE;
michael@0 49 },
michael@0 50 confirmEx: function(parent, title, text, buttonFlags,
michael@0 51 button0Title, button1Title, button2Title,
michael@0 52 checkMsg, checkState) {
michael@0 53 this._called = true;
michael@0 54 return this._buttonChoice;
michael@0 55 }
michael@0 56 };
michael@0 57
michael@0 58 Cm.QueryInterface(Ci.nsIComponentRegistrar)
michael@0 59 .registerFactory(Components.ID(kPromptServiceUUID), "Prompt Service",
michael@0 60 kPromptServiceContractID, fakePromptServiceFactory);
michael@0 61
michael@0 62 this.__defineGetter__("dm", function() {
michael@0 63 delete this.dm;
michael@0 64 return this.dm = Cc["@mozilla.org/download-manager;1"].
michael@0 65 getService(Ci.nsIDownloadManager);
michael@0 66 });
michael@0 67
michael@0 68 function trigger_pb_cleanup(expected)
michael@0 69 {
michael@0 70 var obs = Cc["@mozilla.org/observer-service;1"].getService(Ci.nsIObserverService);
michael@0 71 var cancel = Cc["@mozilla.org/supports-PRBool;1"].createInstance(Ci.nsISupportsPRBool);
michael@0 72 cancel.data = false;
michael@0 73 obs.notifyObservers(cancel, "last-pb-context-exiting", null);
michael@0 74 do_check_eq(expected, cancel.data);
michael@0 75 if (!expected)
michael@0 76 obs.notifyObservers(cancel, "last-pb-context-exited", null);
michael@0 77 }
michael@0 78
michael@0 79 function run_test() {
michael@0 80 if (oldDownloadManagerDisabled()) {
michael@0 81 return;
michael@0 82 }
michael@0 83
michael@0 84 function finishTest() {
michael@0 85 // Cancel Download-G
michael@0 86 dlG.cancel();
michael@0 87 dlG.remove();
michael@0 88 dm.cleanUp();
michael@0 89 dm.cleanUpPrivate();
michael@0 90 do_check_eq(dm.activeDownloadCount, 0);
michael@0 91 do_check_eq(dm.activePrivateDownloadCount, 0);
michael@0 92
michael@0 93 dm.removeListener(listener);
michael@0 94 httpserv.stop(do_test_finished);
michael@0 95
michael@0 96 // Unregister the factory so we do not leak
michael@0 97 Cm.QueryInterface(Ci.nsIComponentRegistrar)
michael@0 98 .unregisterFactory(Components.ID(kPromptServiceUUID),
michael@0 99 fakePromptServiceFactory);
michael@0 100
michael@0 101 // Restore the original factory
michael@0 102 Cm.QueryInterface(Ci.nsIComponentRegistrar)
michael@0 103 .registerFactory(Components.ID(kPromptServiceUUID), "Prompt Service",
michael@0 104 kPromptServiceContractID, kPromptServiceFactory);
michael@0 105 }
michael@0 106
michael@0 107 do_test_pending();
michael@0 108 let httpserv = new HttpServer();
michael@0 109 httpserv.registerDirectory("/file/", do_get_cwd());
michael@0 110 httpserv.registerPathHandler("/noresume", function (meta, response) {
michael@0 111 response.setHeader("Content-Type", "text/html", false);
michael@0 112 response.setHeader("Accept-Ranges", "none", false);
michael@0 113 response.write("foo");
michael@0 114 });
michael@0 115 httpserv.start(-1);
michael@0 116
michael@0 117 let tmpDir = Cc["@mozilla.org/file/directory_service;1"].
michael@0 118 getService(Ci.nsIProperties).
michael@0 119 get("TmpD", Ci.nsIFile);
michael@0 120
michael@0 121 // make sure we're starting with an empty DB
michael@0 122 do_check_eq(dm.activeDownloadCount, 0);
michael@0 123
michael@0 124 let listener = {
michael@0 125 onDownloadStateChange: function(aState, aDownload)
michael@0 126 {
michael@0 127 switch (aDownload.state) {
michael@0 128 case dm.DOWNLOAD_QUEUED:
michael@0 129 case dm.DOWNLOAD_DOWNLOADING:
michael@0 130 if (aDownload.targetFile.equals(dlD.targetFile)) {
michael@0 131 // Sanity check: Download-D must not be resumable
michael@0 132 do_check_false(dlD.resumable);
michael@0 133
michael@0 134 // Cancel the transition
michael@0 135 promptService.sayCancel();
michael@0 136 trigger_pb_cleanup(true);
michael@0 137 do_check_true(promptService.wasCalled());
michael@0 138 do_check_eq(dm.activePrivateDownloadCount, 1);
michael@0 139
michael@0 140 promptService.sayProceed();
michael@0 141 trigger_pb_cleanup(false);
michael@0 142 do_check_true(promptService.wasCalled());
michael@0 143 do_check_eq(dm.activePrivateDownloadCount, 0);
michael@0 144 do_check_eq(dlD.state, dm.DOWNLOAD_CANCELED);
michael@0 145
michael@0 146 // Create Download-E
michael@0 147 dlE = addDownload(httpserv, {
michael@0 148 isPrivate: true,
michael@0 149 targetFile: fileE,
michael@0 150 sourceURI: downloadESource,
michael@0 151 downloadName: downloadEName
michael@0 152 });
michael@0 153
michael@0 154 // Wait for Download-E to start
michael@0 155 } else if (aDownload.targetFile.equals(dlE.targetFile)) {
michael@0 156 // Sanity check: Download-E must be resumable
michael@0 157 do_check_true(dlE.resumable);
michael@0 158
michael@0 159 promptService.sayCancel();
michael@0 160 trigger_pb_cleanup(true);
michael@0 161 do_check_true(promptService.wasCalled());
michael@0 162 do_check_eq(dm.activePrivateDownloadCount, 1);
michael@0 163
michael@0 164 promptService.sayProceed();
michael@0 165 trigger_pb_cleanup(false);
michael@0 166 do_check_true(promptService.wasCalled());
michael@0 167 do_check_eq(dm.activePrivateDownloadCount, 0);
michael@0 168 do_check_eq(dlE.state, dm.DOWNLOAD_CANCELED);
michael@0 169
michael@0 170 // Create Download-F
michael@0 171 dlF = addDownload(httpserv, {
michael@0 172 isPrivate: true,
michael@0 173 targetFile: fileF,
michael@0 174 sourceURI: downloadFSource,
michael@0 175 downloadName: downloadFName
michael@0 176 });
michael@0 177
michael@0 178 // Wait for Download-F to start
michael@0 179 } else if (aDownload.targetFile.equals(dlF.targetFile)) {
michael@0 180 // Sanity check: Download-F must be resumable
michael@0 181 do_check_true(dlF.resumable);
michael@0 182 dlF.pause();
michael@0 183
michael@0 184 } else if (aDownload.targetFile.equals(dlG.targetFile)) {
michael@0 185 // Sanity check: Download-G must not be resumable
michael@0 186 do_check_false(dlG.resumable);
michael@0 187
michael@0 188 promptService.sayCancel();
michael@0 189 trigger_pb_cleanup(false);
michael@0 190 do_check_false(promptService.wasCalled());
michael@0 191 do_check_eq(dm.activeDownloadCount, 1);
michael@0 192 do_check_eq(dlG.state, dm.DOWNLOAD_DOWNLOADING);
michael@0 193 finishTest();
michael@0 194 }
michael@0 195 break;
michael@0 196
michael@0 197 case dm.DOWNLOAD_PAUSED:
michael@0 198 if (aDownload.targetFile.equals(dlF.targetFile)) {
michael@0 199 promptService.sayProceed();
michael@0 200 trigger_pb_cleanup(false);
michael@0 201 do_check_true(promptService.wasCalled());
michael@0 202 do_check_eq(dm.activePrivateDownloadCount, 0);
michael@0 203 do_check_eq(dlF.state, dm.DOWNLOAD_CANCELED);
michael@0 204
michael@0 205 // Create Download-G
michael@0 206 dlG = addDownload(httpserv, {
michael@0 207 isPrivate: false,
michael@0 208 targetFile: fileG,
michael@0 209 sourceURI: downloadGSource,
michael@0 210 downloadName: downloadGName
michael@0 211 });
michael@0 212
michael@0 213 // Wait for Download-G to start
michael@0 214 }
michael@0 215 break;
michael@0 216 }
michael@0 217 },
michael@0 218 onStateChange: function(a, b, c, d, e) { },
michael@0 219 onProgressChange: function(a, b, c, d, e, f, g) { },
michael@0 220 onSecurityChange: function(a, b, c, d) { }
michael@0 221 };
michael@0 222
michael@0 223 dm.addPrivacyAwareListener(listener);
michael@0 224
michael@0 225 const PORT = httpserv.identity.primaryPort;
michael@0 226
michael@0 227 // properties of Download-D
michael@0 228 const downloadDSource = "http://localhost:" + PORT + "/noresume";
michael@0 229 const downloadDDest = "download-file-D";
michael@0 230 const downloadDName = "download-D";
michael@0 231
michael@0 232 // properties of Download-E
michael@0 233 const downloadESource = "http://localhost:" + PORT + "/file/head_download_manager.js";
michael@0 234 const downloadEDest = "download-file-E";
michael@0 235 const downloadEName = "download-E";
michael@0 236
michael@0 237 // properties of Download-F
michael@0 238 const downloadFSource = "http://localhost:" + PORT + "/file/head_download_manager.js";
michael@0 239 const downloadFDest = "download-file-F";
michael@0 240 const downloadFName = "download-F";
michael@0 241
michael@0 242 // properties of Download-G
michael@0 243 const downloadGSource = "http://localhost:" + PORT + "/noresume";
michael@0 244 const downloadGDest = "download-file-G";
michael@0 245 const downloadGName = "download-G";
michael@0 246
michael@0 247 // Create all target files
michael@0 248 let fileD = tmpDir.clone();
michael@0 249 fileD.append(downloadDDest);
michael@0 250 fileD.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
michael@0 251 let fileE = tmpDir.clone();
michael@0 252 fileE.append(downloadEDest);
michael@0 253 fileE.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
michael@0 254 let fileF = tmpDir.clone();
michael@0 255 fileF.append(downloadFDest);
michael@0 256 fileF.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
michael@0 257 let fileG = tmpDir.clone();
michael@0 258 fileG.append(downloadGDest);
michael@0 259 fileG.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
michael@0 260
michael@0 261 // Create Download-D
michael@0 262 let dlD = addDownload(httpserv, {
michael@0 263 isPrivate: true,
michael@0 264 targetFile: fileD,
michael@0 265 sourceURI: downloadDSource,
michael@0 266 downloadName: downloadDName
michael@0 267 });
michael@0 268
michael@0 269 let dlE, dlF, dlG;
michael@0 270
michael@0 271 // wait for Download-D to start
michael@0 272 }

mercurial