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

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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 Test download manager's interaction with the private browsing service.
michael@0 7
michael@0 8 An overview of what this test does follows:
michael@0 9
michael@0 10 * Create a download (Download-A) with specific details.
michael@0 11 * Check that Download-A is retrievable.
michael@0 12 * Enter private browsing mode.
michael@0 13 * Check that Download-A is not accessible.
michael@0 14 * Create another download (Download-B) with specific and different details.
michael@0 15 * Check that Download-B is retrievable.
michael@0 16 * Exit private browsing mode.
michael@0 17 * Check that Download-A is retrievable.
michael@0 18 * Check that Download-B is not accessible.
michael@0 19 * Create a new download (Download-C) with specific details.
michael@0 20 Start it and enter private browsing mode immediately after.
michael@0 21 * Check that Download-C has been paused.
michael@0 22 * Exit the private browsing mode.
michael@0 23 * Verify that Download-C resumes and finishes correctly now.
michael@0 24 **/
michael@0 25
michael@0 26 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 27
michael@0 28 this.__defineGetter__("dm", function() {
michael@0 29 delete this.dm;
michael@0 30 return this.dm = Cc["@mozilla.org/download-manager;1"].
michael@0 31 getService(Ci.nsIDownloadManager);
michael@0 32 });
michael@0 33
michael@0 34 /**
michael@0 35 * Try to see if an active download is available using the |activeDownloads|
michael@0 36 * property.
michael@0 37 */
michael@0 38 function is_active_download_available(aGUID, aSrc, aDst, aName, aPrivate) {
michael@0 39 let enumerator = aPrivate ? dm.activePrivateDownloads : dm.activeDownloads;
michael@0 40 while (enumerator.hasMoreElements()) {
michael@0 41 let download = enumerator.getNext();
michael@0 42 if (download.guid == aGUID &&
michael@0 43 download.source.spec == aSrc &&
michael@0 44 download.targetFile.path == aDst.path &&
michael@0 45 download.displayName == aName &&
michael@0 46 download.isPrivate == aPrivate)
michael@0 47 return true;
michael@0 48 }
michael@0 49 return false;
michael@0 50 }
michael@0 51
michael@0 52 function expect_download_present(aGUID, aSrc, aDst, aName, aPrivate) {
michael@0 53 is_download_available(aGUID, aSrc, aDst, aName, aPrivate, true);
michael@0 54 }
michael@0 55
michael@0 56 function expect_download_absent(aGUID, aSrc, aDst, aName, aPrivate) {
michael@0 57 is_download_available(aGUID, aSrc, aDst, aName, aPrivate, false);
michael@0 58 }
michael@0 59
michael@0 60 /**
michael@0 61 * Try to see if a download is available using the |getDownloadByGUID| method. The
michael@0 62 * download can both be active or inactive.
michael@0 63 */
michael@0 64 function is_download_available(aGUID, aSrc, aDst, aName, aPrivate, present) {
michael@0 65 do_test_pending();
michael@0 66 dm.getDownloadByGUID(aGUID, function(status, download) {
michael@0 67 if (!present) {
michael@0 68 do_check_eq(download, null);
michael@0 69 } else {
michael@0 70 do_check_neq(download, null);
michael@0 71 do_check_eq(download.guid, aGUID);
michael@0 72 do_check_eq(download.source.spec, aSrc);
michael@0 73 do_check_eq(download.targetFile.path, aDst.path);
michael@0 74 do_check_eq(download.displayName, aName);
michael@0 75 do_check_eq(download.isPrivate, aPrivate);
michael@0 76 }
michael@0 77 do_test_finished();
michael@0 78 });
michael@0 79 }
michael@0 80
michael@0 81 function run_test() {
michael@0 82 if (oldDownloadManagerDisabled()) {
michael@0 83 return;
michael@0 84 }
michael@0 85
michael@0 86 let prefBranch = Cc["@mozilla.org/preferences-service;1"].
michael@0 87 getService(Ci.nsIPrefBranch);
michael@0 88
michael@0 89 do_test_pending();
michael@0 90 let httpserv = new HttpServer();
michael@0 91 httpserv.registerDirectory("/", do_get_cwd());
michael@0 92 httpserv.start(-1);
michael@0 93
michael@0 94 let tmpDir = do_get_tempdir();
michael@0 95 const nsIWBP = Ci.nsIWebBrowserPersist;
michael@0 96
michael@0 97 // make sure we're starting with an empty DB
michael@0 98 do_check_eq(dm.activeDownloadCount, 0);
michael@0 99 do_check_eq(dm.activePrivateDownloadCount, 0);
michael@0 100
michael@0 101 let listener = {
michael@0 102 phase: 1,
michael@0 103 handledC: false,
michael@0 104 onDownloadStateChange: function(aState, aDownload)
michael@0 105 {
michael@0 106 switch (aDownload.state) {
michael@0 107 case dm.DOWNLOAD_FAILED:
michael@0 108 case dm.DOWNLOAD_CANCELED:
michael@0 109 case dm.DOWNLOAD_DIRTY:
michael@0 110 case dm.DOWNLOAD_BLOCKED_POLICY:
michael@0 111 // Fail!
michael@0 112 if (aDownload.targetFile.exists())
michael@0 113 aDownload.targetFile.remove(false);
michael@0 114 dm.removeListener(this);
michael@0 115 do_throw("Download failed (name: " + aDownload.displayName + ", state: " + aDownload.state + ")");
michael@0 116 do_test_finished();
michael@0 117 break;
michael@0 118
michael@0 119 // We need to wait until Download-C has started, because otherwise it
michael@0 120 // won't be resumable, so the private browsing mode won't correctly pause it.
michael@0 121 case dm.DOWNLOAD_DOWNLOADING:
michael@0 122 if (aDownload.guid == downloadC && !this.handledC && this.phase == 2) {
michael@0 123 // Sanity check: Download-C must be resumable
michael@0 124 do_check_true(dlC.resumable);
michael@0 125
michael@0 126 // Check that Download-A is accessible
michael@0 127 expect_download_present(downloadA, downloadASource,
michael@0 128 fileA, downloadAName, false);
michael@0 129
michael@0 130 // Check that Download-B is not accessible
michael@0 131 expect_download_absent(downloadB, downloadBSource,
michael@0 132 fileB, downloadBName, true);
michael@0 133
michael@0 134 // Check that Download-C is accessible
michael@0 135 expect_download_present(downloadC, downloadCSource,
michael@0 136 fileC, downloadCName, false);
michael@0 137
michael@0 138 // only perform these checks the first time that Download-C is started
michael@0 139 this.handledC = true;
michael@0 140 }
michael@0 141 break;
michael@0 142
michael@0 143 case dm.DOWNLOAD_FINISHED:
michael@0 144 do_check_true(aDownload.targetFile.exists());
michael@0 145 aDownload.targetFile.remove(false);
michael@0 146 this.onDownloadFinished();
michael@0 147 break;
michael@0 148 }
michael@0 149 },
michael@0 150 onStateChange: function(a, b, c, d, e) { },
michael@0 151 onProgressChange: function(a, b, c, d, e, f, g) { },
michael@0 152 onSecurityChange: function(a, b, c, d) { },
michael@0 153 onDownloadFinished: function () {
michael@0 154 switch (this.phase) {
michael@0 155 case 1: {
michael@0 156 do_check_eq(dm.activeDownloadCount, 0);
michael@0 157
michael@0 158 // Create Download-B
michael@0 159 let dlB = addDownload(httpserv, {
michael@0 160 isPrivate: true,
michael@0 161 targetFile: fileB,
michael@0 162 sourceURI: downloadBSource,
michael@0 163 downloadName: downloadBName,
michael@0 164 runBeforeStart: function (aDownload) {
michael@0 165 // Check that Download-B is retrievable
michael@0 166 do_check_eq(dm.activePrivateDownloadCount, 1);
michael@0 167 do_check_eq(dm.activeDownloadCount, 0);
michael@0 168 do_check_true(is_active_download_available(aDownload.guid,
michael@0 169 downloadBSource, fileB, downloadBName, true));
michael@0 170 expect_download_present(aDownload.guid,
michael@0 171 downloadBSource, fileB, downloadBName, true);
michael@0 172 }
michael@0 173 });
michael@0 174 downloadB = dlB.guid;
michael@0 175
michael@0 176 // wait for Download-B to finish
michael@0 177 ++this.phase;
michael@0 178 }
michael@0 179 break;
michael@0 180
michael@0 181 case 2: {
michael@0 182 do_check_eq(dm.activeDownloadCount, 0);
michael@0 183
michael@0 184 // Simulate last private browsing context triggering cleanup
michael@0 185 Services.obs.notifyObservers(null, "last-pb-context-exited", null);
michael@0 186
michael@0 187 // Create Download-C
michael@0 188 dlC = addDownload(httpserv, {
michael@0 189 isPrivate: false,
michael@0 190 targetFile: fileC,
michael@0 191 sourceURI: downloadCSource,
michael@0 192 downloadName: downloadCName,
michael@0 193 runBeforeStart: function (aDownload) {
michael@0 194 // Check that Download-C is retrievable
michael@0 195 do_check_eq(dm.activePrivateDownloadCount, 0);
michael@0 196 do_check_true(is_active_download_available(aDownload.guid,
michael@0 197 downloadCSource, fileC, downloadCName, false));
michael@0 198 expect_download_present(aDownload.guid,
michael@0 199 downloadCSource, fileC, downloadCName, false);
michael@0 200 }
michael@0 201 });
michael@0 202 downloadC = dlC.guid;
michael@0 203
michael@0 204 // wait for Download-C to finish
michael@0 205 ++this.phase;
michael@0 206
michael@0 207 // Check that Download-A is accessible
michael@0 208 expect_download_present(downloadA, downloadASource,
michael@0 209 fileA, downloadAName, false);
michael@0 210
michael@0 211 // Check that Download-B is not accessible
michael@0 212 expect_download_absent(downloadB, downloadBSource,
michael@0 213 fileB, downloadBName, true);
michael@0 214 }
michael@0 215 break;
michael@0 216
michael@0 217 case 3: {
michael@0 218 do_check_eq(dm.activePrivateDownloadCount, 0);
michael@0 219
michael@0 220 // Check that Download-A is accessible
michael@0 221 expect_download_present(downloadA, downloadASource,
michael@0 222 fileA, downloadAName, false);
michael@0 223
michael@0 224 // Check that Download-B is not accessible
michael@0 225 expect_download_absent(downloadB, downloadBSource,
michael@0 226 fileB, downloadBName, true);
michael@0 227
michael@0 228 // Check that Download-C is accessible
michael@0 229 expect_download_present(downloadC, downloadCSource,
michael@0 230 fileC, downloadCName, false);
michael@0 231
michael@0 232 // cleanup
michael@0 233 dm.removeListener(this);
michael@0 234 httpserv.stop(do_test_finished);
michael@0 235 }
michael@0 236 break;
michael@0 237
michael@0 238 default:
michael@0 239 do_throw("Unexpected phase: " + this.phase);
michael@0 240 break;
michael@0 241 }
michael@0 242 }
michael@0 243 };
michael@0 244
michael@0 245 dm.addPrivacyAwareListener(listener);
michael@0 246 dm.addPrivacyAwareListener(getDownloadListener());
michael@0 247
michael@0 248 // properties of Download-A
michael@0 249 let downloadA = -1;
michael@0 250 const downloadASource = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9gKHhQaLFEVkXsAAAAIdEVYdENvbW1lbnQA9syWvwAABXZJRE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/cAiwUxwvgH+CNPDdhKHAnMAHYl8YqnzXKOxFirsj1DVksagcw3epgfzY7EFmzUkRwLjADWKVM6k2rg3lplhgQNWSd0g/KkZ8zAtoCrwCjrQ6+zHVTrA46rQ52iD35SKZfVCZVH+OdDgT6hZjDEzgs4G9Md3Tpdq8IbZnjfc6RqNBtwx3MPSewV/pRfcD5dFX5HTG/17iYkxEjNIG+1S6NmRvvYk5GrFtdHwBd44x/i/l9sos5ZGCT9DcY5Y0pMwOuPVkXucBXSqzegzkpurVDgmeAhlIjViY1UJnUXcqkWkSNIq710qgZEA20Icxsu3agRURojlHeEm39E0UE3JWF5FfgEauDQ87uJ5yIseW8gEZS3O2iTp8s8SGcpDujvU4CmRqrg2hU+IBY/XY3HZ+ICepfk8VGauuf7AuqyCivQtRrNfCSm4aPxp2Nko8cLoz0lTZPwLdFawhxeaHFYYbCKK+2D+z+bU4+aHHW1KJkvppEvNYY5VWVOSv3mSibprjCRyLDw1Z07i5gkrb+6RKDvwTYDNwNbNPWV3F0mbLTDXIfbges5O1LBf4K4FsB35bJNiUzpPMOAPWywETgJ6O860sA/lpxE8bxf4EjbZUm1xLTn8CD2vpbiwA8IdpmKdCfQpSYIi9wi3yfA89q6/9RIPC3Ah9IOAmFLPJFXuSWWbskenpbW39HnsZpGvC4k04pXpk1xmK7he6DdKckNwI/AAejJSkJBWvorn/dI35XaQvdMYxk+tTgEHBKsgeDRa6jrTyfGsQwUraPPS769h+G3Ox+KOb9iAAAAABJRU5ErkJggg==";
michael@0 251 const downloadADest = "download-file-A";
michael@0 252 const downloadAName = "download-A";
michael@0 253
michael@0 254 // properties of Download-B
michael@0 255 let downloadB = -1;
michael@0 256 const downloadBSource = "data:application/octet-stream;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAYAAABXAvmHAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9gKHhQaLFEVkXsAAAAIdE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0epgXxqaDhG1YS4DlY5hIofd6w/cAiwUxwvgH+CNPDdhKHAnMAHYl8YqnzXKOxFirsj1DVksagcw3epgfzY7EFmzUkRwLjADWKVM6k2rg3lplhgQNWSd0g/KkZ8zAtoCrwCjrQ6+zHVTrA46rQ52iD35SKZfVCZVH+OdDgT6hZjDEzgs4G9Md3Tpdq8IbZnjfc6RqNBtwx3MPSewV/pRfcD5dFX5HTG/17iYkxEjNIG+1S6NmRvvYk5GrFtdHwBd44x/i/l9sos5ZGCT9DcY5Y0pMwOuPVkXucBXSqzegzkpurVDgmeAhlIjViY1UJnUXcqkWkSNIq710qgZEA20Icxsu3agRURojlHeEm39E0UE3JWF5FfgEauDQ87uJ5yIseW8gEZS3O2iTp8s8SGcpDujvU4CmRqrg2hU+IBY/XY3HZ+ICepfk8VGauuf7AuqyCivQtRrNfCSm4aPxp2Nko8cLoz0lTZPwLdFawhxeaHFYYbCKK+2D+z+bU4+aHHW1KJkvppEvNYY5VWVOSv3mSibprjCRyLDw1Z07i5gkrb+6RKDvwTYDNwNbNPWV3F0mbLTDXIfbges5O1LBf4K4FsB35bJNiUzpPMOAPWywETgJ6O860sA/lpxE8bxf4EjbZUm1xLTn8CD2vpbiwA8IdpmKdCfQpSYIi9wi3yfA89q6/9RIPC3Ah9IOAmFLPJFXuSWWbskenpbW39HnsZpGvC4k04pXpk1xmK7he6DdKckNwI/AAejJSkJBWvorn/dI35XaQvdMYxk+tTgEHBKsgeDRa6jrTyfGsQwUraPPS769h+G3Ox+KOb9iAAAAABJRU5ErkJggg==";
michael@0 257 const downloadBDest = "download-file-B";
michael@0 258 const downloadBName = "download-B";
michael@0 259
michael@0 260 // properties of Download-C
michael@0 261 let downloadC = -1;
michael@0 262 const downloadCSource = "http://localhost:" +
michael@0 263 httpserv.identity.primaryPort +
michael@0 264 "/head_download_manager.js";
michael@0 265 const downloadCDest = "download-file-C";
michael@0 266 const downloadCName = "download-C";
michael@0 267
michael@0 268 // Create all target files
michael@0 269 let fileA = tmpDir.clone();
michael@0 270 fileA.append(downloadADest);
michael@0 271 fileA.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
michael@0 272 let fileB = tmpDir.clone();
michael@0 273 fileB.append(downloadBDest);
michael@0 274 fileB.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
michael@0 275 let fileC = tmpDir.clone();
michael@0 276 fileC.append(downloadCDest);
michael@0 277 fileC.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
michael@0 278
michael@0 279 // use js closures to access dlC
michael@0 280 let dlC;
michael@0 281
michael@0 282 // Create Download-A
michael@0 283 let dlA = addDownload(httpserv, {
michael@0 284 isPrivate: false,
michael@0 285 targetFile: fileA,
michael@0 286 sourceURI: downloadASource,
michael@0 287 downloadName: downloadAName,
michael@0 288 runBeforeStart: function (aDownload) {
michael@0 289 // Check that Download-A is retrievable
michael@0 290 do_check_eq(dm.activePrivateDownloadCount, 0);
michael@0 291 do_check_true(is_active_download_available(aDownload.guid, downloadASource, fileA, downloadAName, false));
michael@0 292 expect_download_present(aDownload.guid, downloadASource, fileA, downloadAName, false);
michael@0 293 }
michael@0 294 });
michael@0 295 downloadA = dlA.guid;
michael@0 296
michael@0 297 // wait for Download-A to finish
michael@0 298 }

mercurial