Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 4 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 5 | |
michael@0 | 6 | /** |
michael@0 | 7 | * Tests the DownloadList object. |
michael@0 | 8 | */ |
michael@0 | 9 | |
michael@0 | 10 | "use strict"; |
michael@0 | 11 | |
michael@0 | 12 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 13 | //// Globals |
michael@0 | 14 | |
michael@0 | 15 | /** |
michael@0 | 16 | * Returns a PRTime in the past usable to add expirable visits. |
michael@0 | 17 | * |
michael@0 | 18 | * @note Expiration ignores any visit added in the last 7 days, but it's |
michael@0 | 19 | * better be safe against DST issues, by going back one day more. |
michael@0 | 20 | */ |
michael@0 | 21 | function getExpirablePRTime() |
michael@0 | 22 | { |
michael@0 | 23 | let dateObj = new Date(); |
michael@0 | 24 | // Normalize to midnight |
michael@0 | 25 | dateObj.setHours(0); |
michael@0 | 26 | dateObj.setMinutes(0); |
michael@0 | 27 | dateObj.setSeconds(0); |
michael@0 | 28 | dateObj.setMilliseconds(0); |
michael@0 | 29 | dateObj = new Date(dateObj.getTime() - 8 * 86400000); |
michael@0 | 30 | return dateObj.getTime() * 1000; |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | /** |
michael@0 | 34 | * Adds an expirable history visit for a download. |
michael@0 | 35 | * |
michael@0 | 36 | * @param aSourceUrl |
michael@0 | 37 | * String containing the URI for the download source, or null to use |
michael@0 | 38 | * httpUrl("source.txt"). |
michael@0 | 39 | * |
michael@0 | 40 | * @return {Promise} |
michael@0 | 41 | * @rejects JavaScript exception. |
michael@0 | 42 | */ |
michael@0 | 43 | function promiseExpirableDownloadVisit(aSourceUrl) |
michael@0 | 44 | { |
michael@0 | 45 | let deferred = Promise.defer(); |
michael@0 | 46 | PlacesUtils.asyncHistory.updatePlaces( |
michael@0 | 47 | { |
michael@0 | 48 | uri: NetUtil.newURI(aSourceUrl || httpUrl("source.txt")), |
michael@0 | 49 | visits: [{ |
michael@0 | 50 | transitionType: Ci.nsINavHistoryService.TRANSITION_DOWNLOAD, |
michael@0 | 51 | visitDate: getExpirablePRTime(), |
michael@0 | 52 | }] |
michael@0 | 53 | }, |
michael@0 | 54 | { |
michael@0 | 55 | handleError: function handleError(aResultCode, aPlaceInfo) { |
michael@0 | 56 | let ex = new Components.Exception("Unexpected error in adding visits.", |
michael@0 | 57 | aResultCode); |
michael@0 | 58 | deferred.reject(ex); |
michael@0 | 59 | }, |
michael@0 | 60 | handleResult: function () {}, |
michael@0 | 61 | handleCompletion: function handleCompletion() { |
michael@0 | 62 | deferred.resolve(); |
michael@0 | 63 | } |
michael@0 | 64 | }); |
michael@0 | 65 | return deferred.promise; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 69 | //// Tests |
michael@0 | 70 | |
michael@0 | 71 | /** |
michael@0 | 72 | * Checks the testing mechanism used to build different download lists. |
michael@0 | 73 | */ |
michael@0 | 74 | add_task(function test_construction() |
michael@0 | 75 | { |
michael@0 | 76 | let downloadListOne = yield promiseNewList(); |
michael@0 | 77 | let downloadListTwo = yield promiseNewList(); |
michael@0 | 78 | let privateDownloadListOne = yield promiseNewList(true); |
michael@0 | 79 | let privateDownloadListTwo = yield promiseNewList(true); |
michael@0 | 80 | |
michael@0 | 81 | do_check_neq(downloadListOne, downloadListTwo); |
michael@0 | 82 | do_check_neq(privateDownloadListOne, privateDownloadListTwo); |
michael@0 | 83 | do_check_neq(downloadListOne, privateDownloadListOne); |
michael@0 | 84 | }); |
michael@0 | 85 | |
michael@0 | 86 | /** |
michael@0 | 87 | * Checks the methods to add and retrieve items from the list. |
michael@0 | 88 | */ |
michael@0 | 89 | add_task(function test_add_getAll() |
michael@0 | 90 | { |
michael@0 | 91 | let list = yield promiseNewList(); |
michael@0 | 92 | |
michael@0 | 93 | let downloadOne = yield promiseNewDownload(); |
michael@0 | 94 | yield list.add(downloadOne); |
michael@0 | 95 | |
michael@0 | 96 | let itemsOne = yield list.getAll(); |
michael@0 | 97 | do_check_eq(itemsOne.length, 1); |
michael@0 | 98 | do_check_eq(itemsOne[0], downloadOne); |
michael@0 | 99 | |
michael@0 | 100 | let downloadTwo = yield promiseNewDownload(); |
michael@0 | 101 | yield list.add(downloadTwo); |
michael@0 | 102 | |
michael@0 | 103 | let itemsTwo = yield list.getAll(); |
michael@0 | 104 | do_check_eq(itemsTwo.length, 2); |
michael@0 | 105 | do_check_eq(itemsTwo[0], downloadOne); |
michael@0 | 106 | do_check_eq(itemsTwo[1], downloadTwo); |
michael@0 | 107 | |
michael@0 | 108 | // The first snapshot should not have been modified. |
michael@0 | 109 | do_check_eq(itemsOne.length, 1); |
michael@0 | 110 | }); |
michael@0 | 111 | |
michael@0 | 112 | /** |
michael@0 | 113 | * Checks the method to remove items from the list. |
michael@0 | 114 | */ |
michael@0 | 115 | add_task(function test_remove() |
michael@0 | 116 | { |
michael@0 | 117 | let list = yield promiseNewList(); |
michael@0 | 118 | |
michael@0 | 119 | yield list.add(yield promiseNewDownload()); |
michael@0 | 120 | yield list.add(yield promiseNewDownload()); |
michael@0 | 121 | |
michael@0 | 122 | let items = yield list.getAll(); |
michael@0 | 123 | yield list.remove(items[0]); |
michael@0 | 124 | |
michael@0 | 125 | // Removing an item that was never added should not raise an error. |
michael@0 | 126 | yield list.remove(yield promiseNewDownload()); |
michael@0 | 127 | |
michael@0 | 128 | items = yield list.getAll(); |
michael@0 | 129 | do_check_eq(items.length, 1); |
michael@0 | 130 | }); |
michael@0 | 131 | |
michael@0 | 132 | /** |
michael@0 | 133 | * Tests that the "add", "remove", and "getAll" methods on the global |
michael@0 | 134 | * DownloadCombinedList object combine the contents of the global DownloadList |
michael@0 | 135 | * objects for public and private downloads. |
michael@0 | 136 | */ |
michael@0 | 137 | add_task(function test_DownloadCombinedList_add_remove_getAll() |
michael@0 | 138 | { |
michael@0 | 139 | let publicList = yield promiseNewList(); |
michael@0 | 140 | let privateList = yield Downloads.getList(Downloads.PRIVATE); |
michael@0 | 141 | let combinedList = yield Downloads.getList(Downloads.ALL); |
michael@0 | 142 | |
michael@0 | 143 | let publicDownload = yield promiseNewDownload(); |
michael@0 | 144 | let privateDownload = yield Downloads.createDownload({ |
michael@0 | 145 | source: { url: httpUrl("source.txt"), isPrivate: true }, |
michael@0 | 146 | target: getTempFile(TEST_TARGET_FILE_NAME).path, |
michael@0 | 147 | }); |
michael@0 | 148 | |
michael@0 | 149 | yield publicList.add(publicDownload); |
michael@0 | 150 | yield privateList.add(privateDownload); |
michael@0 | 151 | |
michael@0 | 152 | do_check_eq((yield combinedList.getAll()).length, 2); |
michael@0 | 153 | |
michael@0 | 154 | yield combinedList.remove(publicDownload); |
michael@0 | 155 | yield combinedList.remove(privateDownload); |
michael@0 | 156 | |
michael@0 | 157 | do_check_eq((yield combinedList.getAll()).length, 0); |
michael@0 | 158 | |
michael@0 | 159 | yield combinedList.add(publicDownload); |
michael@0 | 160 | yield combinedList.add(privateDownload); |
michael@0 | 161 | |
michael@0 | 162 | do_check_eq((yield publicList.getAll()).length, 1); |
michael@0 | 163 | do_check_eq((yield privateList.getAll()).length, 1); |
michael@0 | 164 | do_check_eq((yield combinedList.getAll()).length, 2); |
michael@0 | 165 | |
michael@0 | 166 | yield publicList.remove(publicDownload); |
michael@0 | 167 | yield privateList.remove(privateDownload); |
michael@0 | 168 | |
michael@0 | 169 | do_check_eq((yield combinedList.getAll()).length, 0); |
michael@0 | 170 | }); |
michael@0 | 171 | |
michael@0 | 172 | /** |
michael@0 | 173 | * Checks that views receive the download add and remove notifications, and that |
michael@0 | 174 | * adding and removing views works as expected, both for a normal and a combined |
michael@0 | 175 | * list. |
michael@0 | 176 | */ |
michael@0 | 177 | add_task(function test_notifications_add_remove() |
michael@0 | 178 | { |
michael@0 | 179 | for (let isCombined of [false, true]) { |
michael@0 | 180 | // Force creating a new list for both the public and combined cases. |
michael@0 | 181 | let list = yield promiseNewList(); |
michael@0 | 182 | if (isCombined) { |
michael@0 | 183 | list = yield Downloads.getList(Downloads.ALL); |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | let downloadOne = yield promiseNewDownload(); |
michael@0 | 187 | let downloadTwo = yield Downloads.createDownload({ |
michael@0 | 188 | source: { url: httpUrl("source.txt"), isPrivate: true }, |
michael@0 | 189 | target: getTempFile(TEST_TARGET_FILE_NAME).path, |
michael@0 | 190 | }); |
michael@0 | 191 | yield list.add(downloadOne); |
michael@0 | 192 | yield list.add(downloadTwo); |
michael@0 | 193 | |
michael@0 | 194 | // Check that we receive add notifications for existing elements. |
michael@0 | 195 | let addNotifications = 0; |
michael@0 | 196 | let viewOne = { |
michael@0 | 197 | onDownloadAdded: function (aDownload) { |
michael@0 | 198 | // The first download to be notified should be the first that was added. |
michael@0 | 199 | if (addNotifications == 0) { |
michael@0 | 200 | do_check_eq(aDownload, downloadOne); |
michael@0 | 201 | } else if (addNotifications == 1) { |
michael@0 | 202 | do_check_eq(aDownload, downloadTwo); |
michael@0 | 203 | } |
michael@0 | 204 | addNotifications++; |
michael@0 | 205 | }, |
michael@0 | 206 | }; |
michael@0 | 207 | yield list.addView(viewOne); |
michael@0 | 208 | do_check_eq(addNotifications, 2); |
michael@0 | 209 | |
michael@0 | 210 | // Check that we receive add notifications for new elements. |
michael@0 | 211 | yield list.add(yield promiseNewDownload()); |
michael@0 | 212 | do_check_eq(addNotifications, 3); |
michael@0 | 213 | |
michael@0 | 214 | // Check that we receive remove notifications. |
michael@0 | 215 | let removeNotifications = 0; |
michael@0 | 216 | let viewTwo = { |
michael@0 | 217 | onDownloadRemoved: function (aDownload) { |
michael@0 | 218 | do_check_eq(aDownload, downloadOne); |
michael@0 | 219 | removeNotifications++; |
michael@0 | 220 | }, |
michael@0 | 221 | }; |
michael@0 | 222 | yield list.addView(viewTwo); |
michael@0 | 223 | yield list.remove(downloadOne); |
michael@0 | 224 | do_check_eq(removeNotifications, 1); |
michael@0 | 225 | |
michael@0 | 226 | // We should not receive remove notifications after the view is removed. |
michael@0 | 227 | yield list.removeView(viewTwo); |
michael@0 | 228 | yield list.remove(downloadTwo); |
michael@0 | 229 | do_check_eq(removeNotifications, 1); |
michael@0 | 230 | |
michael@0 | 231 | // We should not receive add notifications after the view is removed. |
michael@0 | 232 | yield list.removeView(viewOne); |
michael@0 | 233 | yield list.add(yield promiseNewDownload()); |
michael@0 | 234 | do_check_eq(addNotifications, 3); |
michael@0 | 235 | } |
michael@0 | 236 | }); |
michael@0 | 237 | |
michael@0 | 238 | /** |
michael@0 | 239 | * Checks that views receive the download change notifications, both for a |
michael@0 | 240 | * normal and a combined list. |
michael@0 | 241 | */ |
michael@0 | 242 | add_task(function test_notifications_change() |
michael@0 | 243 | { |
michael@0 | 244 | for (let isCombined of [false, true]) { |
michael@0 | 245 | // Force creating a new list for both the public and combined cases. |
michael@0 | 246 | let list = yield promiseNewList(); |
michael@0 | 247 | if (isCombined) { |
michael@0 | 248 | list = yield Downloads.getList(Downloads.ALL); |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | let downloadOne = yield promiseNewDownload(); |
michael@0 | 252 | let downloadTwo = yield Downloads.createDownload({ |
michael@0 | 253 | source: { url: httpUrl("source.txt"), isPrivate: true }, |
michael@0 | 254 | target: getTempFile(TEST_TARGET_FILE_NAME).path, |
michael@0 | 255 | }); |
michael@0 | 256 | yield list.add(downloadOne); |
michael@0 | 257 | yield list.add(downloadTwo); |
michael@0 | 258 | |
michael@0 | 259 | // Check that we receive change notifications. |
michael@0 | 260 | let receivedOnDownloadChanged = false; |
michael@0 | 261 | yield list.addView({ |
michael@0 | 262 | onDownloadChanged: function (aDownload) { |
michael@0 | 263 | do_check_eq(aDownload, downloadOne); |
michael@0 | 264 | receivedOnDownloadChanged = true; |
michael@0 | 265 | }, |
michael@0 | 266 | }); |
michael@0 | 267 | yield downloadOne.start(); |
michael@0 | 268 | do_check_true(receivedOnDownloadChanged); |
michael@0 | 269 | |
michael@0 | 270 | // We should not receive change notifications after a download is removed. |
michael@0 | 271 | receivedOnDownloadChanged = false; |
michael@0 | 272 | yield list.remove(downloadTwo); |
michael@0 | 273 | yield downloadTwo.start(); |
michael@0 | 274 | do_check_false(receivedOnDownloadChanged); |
michael@0 | 275 | } |
michael@0 | 276 | }); |
michael@0 | 277 | |
michael@0 | 278 | /** |
michael@0 | 279 | * Checks that the reference to "this" is correct in the view callbacks. |
michael@0 | 280 | */ |
michael@0 | 281 | add_task(function test_notifications_this() |
michael@0 | 282 | { |
michael@0 | 283 | let list = yield promiseNewList(); |
michael@0 | 284 | |
michael@0 | 285 | // Check that we receive change notifications. |
michael@0 | 286 | let receivedOnDownloadAdded = false; |
michael@0 | 287 | let receivedOnDownloadChanged = false; |
michael@0 | 288 | let receivedOnDownloadRemoved = false; |
michael@0 | 289 | let view = { |
michael@0 | 290 | onDownloadAdded: function () { |
michael@0 | 291 | do_check_eq(this, view); |
michael@0 | 292 | receivedOnDownloadAdded = true; |
michael@0 | 293 | }, |
michael@0 | 294 | onDownloadChanged: function () { |
michael@0 | 295 | // Only do this check once. |
michael@0 | 296 | if (!receivedOnDownloadChanged) { |
michael@0 | 297 | do_check_eq(this, view); |
michael@0 | 298 | receivedOnDownloadChanged = true; |
michael@0 | 299 | } |
michael@0 | 300 | }, |
michael@0 | 301 | onDownloadRemoved: function () { |
michael@0 | 302 | do_check_eq(this, view); |
michael@0 | 303 | receivedOnDownloadRemoved = true; |
michael@0 | 304 | }, |
michael@0 | 305 | }; |
michael@0 | 306 | yield list.addView(view); |
michael@0 | 307 | |
michael@0 | 308 | let download = yield promiseNewDownload(); |
michael@0 | 309 | yield list.add(download); |
michael@0 | 310 | yield download.start(); |
michael@0 | 311 | yield list.remove(download); |
michael@0 | 312 | |
michael@0 | 313 | // Verify that we executed the checks. |
michael@0 | 314 | do_check_true(receivedOnDownloadAdded); |
michael@0 | 315 | do_check_true(receivedOnDownloadChanged); |
michael@0 | 316 | do_check_true(receivedOnDownloadRemoved); |
michael@0 | 317 | }); |
michael@0 | 318 | |
michael@0 | 319 | /** |
michael@0 | 320 | * Checks that download is removed on history expiration. |
michael@0 | 321 | */ |
michael@0 | 322 | add_task(function test_history_expiration() |
michael@0 | 323 | { |
michael@0 | 324 | mustInterruptResponses(); |
michael@0 | 325 | |
michael@0 | 326 | function cleanup() { |
michael@0 | 327 | Services.prefs.clearUserPref("places.history.expiration.max_pages"); |
michael@0 | 328 | } |
michael@0 | 329 | do_register_cleanup(cleanup); |
michael@0 | 330 | |
michael@0 | 331 | // Set max pages to 0 to make the download expire. |
michael@0 | 332 | Services.prefs.setIntPref("places.history.expiration.max_pages", 0); |
michael@0 | 333 | |
michael@0 | 334 | let list = yield promiseNewList(); |
michael@0 | 335 | let downloadOne = yield promiseNewDownload(); |
michael@0 | 336 | let downloadTwo = yield promiseNewDownload(httpUrl("interruptible.txt")); |
michael@0 | 337 | |
michael@0 | 338 | let deferred = Promise.defer(); |
michael@0 | 339 | let removeNotifications = 0; |
michael@0 | 340 | let downloadView = { |
michael@0 | 341 | onDownloadRemoved: function (aDownload) { |
michael@0 | 342 | if (++removeNotifications == 2) { |
michael@0 | 343 | deferred.resolve(); |
michael@0 | 344 | } |
michael@0 | 345 | }, |
michael@0 | 346 | }; |
michael@0 | 347 | yield list.addView(downloadView); |
michael@0 | 348 | |
michael@0 | 349 | // Work with one finished download and one canceled download. |
michael@0 | 350 | yield downloadOne.start(); |
michael@0 | 351 | downloadTwo.start(); |
michael@0 | 352 | yield downloadTwo.cancel(); |
michael@0 | 353 | |
michael@0 | 354 | // We must replace the visits added while executing the downloads with visits |
michael@0 | 355 | // that are older than 7 days, otherwise they will not be expired. |
michael@0 | 356 | yield promiseClearHistory(); |
michael@0 | 357 | yield promiseExpirableDownloadVisit(); |
michael@0 | 358 | yield promiseExpirableDownloadVisit(httpUrl("interruptible.txt")); |
michael@0 | 359 | |
michael@0 | 360 | // After clearing history, we can add the downloads to be removed to the list. |
michael@0 | 361 | yield list.add(downloadOne); |
michael@0 | 362 | yield list.add(downloadTwo); |
michael@0 | 363 | |
michael@0 | 364 | // Force a history expiration. |
michael@0 | 365 | Cc["@mozilla.org/places/expiration;1"] |
michael@0 | 366 | .getService(Ci.nsIObserver).observe(null, "places-debug-start-expiration", -1); |
michael@0 | 367 | |
michael@0 | 368 | // Wait for both downloads to be removed. |
michael@0 | 369 | yield deferred.promise; |
michael@0 | 370 | |
michael@0 | 371 | cleanup(); |
michael@0 | 372 | }); |
michael@0 | 373 | |
michael@0 | 374 | /** |
michael@0 | 375 | * Checks all downloads are removed after clearing history. |
michael@0 | 376 | */ |
michael@0 | 377 | add_task(function test_history_clear() |
michael@0 | 378 | { |
michael@0 | 379 | let list = yield promiseNewList(); |
michael@0 | 380 | let downloadOne = yield promiseNewDownload(); |
michael@0 | 381 | let downloadTwo = yield promiseNewDownload(); |
michael@0 | 382 | yield list.add(downloadOne); |
michael@0 | 383 | yield list.add(downloadTwo); |
michael@0 | 384 | |
michael@0 | 385 | let deferred = Promise.defer(); |
michael@0 | 386 | let removeNotifications = 0; |
michael@0 | 387 | let downloadView = { |
michael@0 | 388 | onDownloadRemoved: function (aDownload) { |
michael@0 | 389 | if (++removeNotifications == 2) { |
michael@0 | 390 | deferred.resolve(); |
michael@0 | 391 | } |
michael@0 | 392 | }, |
michael@0 | 393 | }; |
michael@0 | 394 | yield list.addView(downloadView); |
michael@0 | 395 | |
michael@0 | 396 | yield downloadOne.start(); |
michael@0 | 397 | yield downloadTwo.start(); |
michael@0 | 398 | |
michael@0 | 399 | yield promiseClearHistory(); |
michael@0 | 400 | |
michael@0 | 401 | // Wait for the removal notifications that may still be pending. |
michael@0 | 402 | yield deferred.promise; |
michael@0 | 403 | }); |
michael@0 | 404 | |
michael@0 | 405 | /** |
michael@0 | 406 | * Tests the removeFinished method to ensure that it only removes |
michael@0 | 407 | * finished downloads. |
michael@0 | 408 | */ |
michael@0 | 409 | add_task(function test_removeFinished() |
michael@0 | 410 | { |
michael@0 | 411 | let list = yield promiseNewList(); |
michael@0 | 412 | let downloadOne = yield promiseNewDownload(); |
michael@0 | 413 | let downloadTwo = yield promiseNewDownload(); |
michael@0 | 414 | let downloadThree = yield promiseNewDownload(); |
michael@0 | 415 | let downloadFour = yield promiseNewDownload(); |
michael@0 | 416 | yield list.add(downloadOne); |
michael@0 | 417 | yield list.add(downloadTwo); |
michael@0 | 418 | yield list.add(downloadThree); |
michael@0 | 419 | yield list.add(downloadFour); |
michael@0 | 420 | |
michael@0 | 421 | let deferred = Promise.defer(); |
michael@0 | 422 | let removeNotifications = 0; |
michael@0 | 423 | let downloadView = { |
michael@0 | 424 | onDownloadRemoved: function (aDownload) { |
michael@0 | 425 | do_check_true(aDownload == downloadOne || |
michael@0 | 426 | aDownload == downloadTwo || |
michael@0 | 427 | aDownload == downloadThree); |
michael@0 | 428 | do_check_true(removeNotifications < 3); |
michael@0 | 429 | if (++removeNotifications == 3) { |
michael@0 | 430 | deferred.resolve(); |
michael@0 | 431 | } |
michael@0 | 432 | }, |
michael@0 | 433 | }; |
michael@0 | 434 | yield list.addView(downloadView); |
michael@0 | 435 | |
michael@0 | 436 | // Start three of the downloads, but don't start downloadTwo, then set |
michael@0 | 437 | // downloadFour to have partial data. All downloads except downloadFour |
michael@0 | 438 | // should be removed. |
michael@0 | 439 | yield downloadOne.start(); |
michael@0 | 440 | yield downloadThree.start(); |
michael@0 | 441 | yield downloadFour.start(); |
michael@0 | 442 | downloadFour.hasPartialData = true; |
michael@0 | 443 | |
michael@0 | 444 | list.removeFinished(); |
michael@0 | 445 | yield deferred.promise; |
michael@0 | 446 | |
michael@0 | 447 | let downloads = yield list.getAll() |
michael@0 | 448 | do_check_eq(downloads.length, 1); |
michael@0 | 449 | }); |
michael@0 | 450 | |
michael@0 | 451 | /** |
michael@0 | 452 | * Tests the global DownloadSummary objects for the public, private, and |
michael@0 | 453 | * combined download lists. |
michael@0 | 454 | */ |
michael@0 | 455 | add_task(function test_DownloadSummary() |
michael@0 | 456 | { |
michael@0 | 457 | mustInterruptResponses(); |
michael@0 | 458 | |
michael@0 | 459 | let publicList = yield promiseNewList(); |
michael@0 | 460 | let privateList = yield Downloads.getList(Downloads.PRIVATE); |
michael@0 | 461 | |
michael@0 | 462 | let publicSummary = yield Downloads.getSummary(Downloads.PUBLIC); |
michael@0 | 463 | let privateSummary = yield Downloads.getSummary(Downloads.PRIVATE); |
michael@0 | 464 | let combinedSummary = yield Downloads.getSummary(Downloads.ALL); |
michael@0 | 465 | |
michael@0 | 466 | // Add a public download that has succeeded. |
michael@0 | 467 | let succeededPublicDownload = yield promiseNewDownload(); |
michael@0 | 468 | yield succeededPublicDownload.start(); |
michael@0 | 469 | yield publicList.add(succeededPublicDownload); |
michael@0 | 470 | |
michael@0 | 471 | // Add a public download that has been canceled midway. |
michael@0 | 472 | let canceledPublicDownload = |
michael@0 | 473 | yield promiseNewDownload(httpUrl("interruptible.txt")); |
michael@0 | 474 | canceledPublicDownload.start(); |
michael@0 | 475 | yield promiseDownloadMidway(canceledPublicDownload); |
michael@0 | 476 | yield canceledPublicDownload.cancel(); |
michael@0 | 477 | yield publicList.add(canceledPublicDownload); |
michael@0 | 478 | |
michael@0 | 479 | // Add a public download that is in progress. |
michael@0 | 480 | let inProgressPublicDownload = |
michael@0 | 481 | yield promiseNewDownload(httpUrl("interruptible.txt")); |
michael@0 | 482 | inProgressPublicDownload.start(); |
michael@0 | 483 | yield promiseDownloadMidway(inProgressPublicDownload); |
michael@0 | 484 | yield publicList.add(inProgressPublicDownload); |
michael@0 | 485 | |
michael@0 | 486 | // Add a private download that is in progress. |
michael@0 | 487 | let inProgressPrivateDownload = yield Downloads.createDownload({ |
michael@0 | 488 | source: { url: httpUrl("interruptible.txt"), isPrivate: true }, |
michael@0 | 489 | target: getTempFile(TEST_TARGET_FILE_NAME).path, |
michael@0 | 490 | }); |
michael@0 | 491 | inProgressPrivateDownload.start(); |
michael@0 | 492 | yield promiseDownloadMidway(inProgressPrivateDownload); |
michael@0 | 493 | yield privateList.add(inProgressPrivateDownload); |
michael@0 | 494 | |
michael@0 | 495 | // Verify that the summary includes the total number of bytes and the |
michael@0 | 496 | // currently transferred bytes only for the downloads that are not stopped. |
michael@0 | 497 | // For simplicity, we assume that after a download is added to the list, its |
michael@0 | 498 | // current state is immediately propagated to the summary object, which is |
michael@0 | 499 | // true in the current implementation, though it is not guaranteed as all the |
michael@0 | 500 | // download operations may happen asynchronously. |
michael@0 | 501 | do_check_false(publicSummary.allHaveStopped); |
michael@0 | 502 | do_check_eq(publicSummary.progressTotalBytes, TEST_DATA_SHORT.length * 2); |
michael@0 | 503 | do_check_eq(publicSummary.progressCurrentBytes, TEST_DATA_SHORT.length); |
michael@0 | 504 | |
michael@0 | 505 | do_check_false(privateSummary.allHaveStopped); |
michael@0 | 506 | do_check_eq(privateSummary.progressTotalBytes, TEST_DATA_SHORT.length * 2); |
michael@0 | 507 | do_check_eq(privateSummary.progressCurrentBytes, TEST_DATA_SHORT.length); |
michael@0 | 508 | |
michael@0 | 509 | do_check_false(combinedSummary.allHaveStopped); |
michael@0 | 510 | do_check_eq(combinedSummary.progressTotalBytes, TEST_DATA_SHORT.length * 4); |
michael@0 | 511 | do_check_eq(combinedSummary.progressCurrentBytes, TEST_DATA_SHORT.length * 2); |
michael@0 | 512 | |
michael@0 | 513 | yield inProgressPublicDownload.cancel(); |
michael@0 | 514 | |
michael@0 | 515 | // Stopping the download should have excluded it from the summary. |
michael@0 | 516 | do_check_true(publicSummary.allHaveStopped); |
michael@0 | 517 | do_check_eq(publicSummary.progressTotalBytes, 0); |
michael@0 | 518 | do_check_eq(publicSummary.progressCurrentBytes, 0); |
michael@0 | 519 | |
michael@0 | 520 | do_check_false(privateSummary.allHaveStopped); |
michael@0 | 521 | do_check_eq(privateSummary.progressTotalBytes, TEST_DATA_SHORT.length * 2); |
michael@0 | 522 | do_check_eq(privateSummary.progressCurrentBytes, TEST_DATA_SHORT.length); |
michael@0 | 523 | |
michael@0 | 524 | do_check_false(combinedSummary.allHaveStopped); |
michael@0 | 525 | do_check_eq(combinedSummary.progressTotalBytes, TEST_DATA_SHORT.length * 2); |
michael@0 | 526 | do_check_eq(combinedSummary.progressCurrentBytes, TEST_DATA_SHORT.length); |
michael@0 | 527 | |
michael@0 | 528 | yield inProgressPrivateDownload.cancel(); |
michael@0 | 529 | |
michael@0 | 530 | // All the downloads should be stopped now. |
michael@0 | 531 | do_check_true(publicSummary.allHaveStopped); |
michael@0 | 532 | do_check_eq(publicSummary.progressTotalBytes, 0); |
michael@0 | 533 | do_check_eq(publicSummary.progressCurrentBytes, 0); |
michael@0 | 534 | |
michael@0 | 535 | do_check_true(privateSummary.allHaveStopped); |
michael@0 | 536 | do_check_eq(privateSummary.progressTotalBytes, 0); |
michael@0 | 537 | do_check_eq(privateSummary.progressCurrentBytes, 0); |
michael@0 | 538 | |
michael@0 | 539 | do_check_true(combinedSummary.allHaveStopped); |
michael@0 | 540 | do_check_eq(combinedSummary.progressTotalBytes, 0); |
michael@0 | 541 | do_check_eq(combinedSummary.progressCurrentBytes, 0); |
michael@0 | 542 | }); |
michael@0 | 543 | |
michael@0 | 544 | /** |
michael@0 | 545 | * Checks that views receive the summary change notification. This is tested on |
michael@0 | 546 | * the combined summary when adding a public download, as we assume that if we |
michael@0 | 547 | * pass the test in this case we will also pass it in the others. |
michael@0 | 548 | */ |
michael@0 | 549 | add_task(function test_DownloadSummary_notifications() |
michael@0 | 550 | { |
michael@0 | 551 | let list = yield promiseNewList(); |
michael@0 | 552 | let summary = yield Downloads.getSummary(Downloads.ALL); |
michael@0 | 553 | |
michael@0 | 554 | let download = yield promiseNewDownload(); |
michael@0 | 555 | yield list.add(download); |
michael@0 | 556 | |
michael@0 | 557 | // Check that we receive change notifications. |
michael@0 | 558 | let receivedOnSummaryChanged = false; |
michael@0 | 559 | yield summary.addView({ |
michael@0 | 560 | onSummaryChanged: function () { |
michael@0 | 561 | receivedOnSummaryChanged = true; |
michael@0 | 562 | }, |
michael@0 | 563 | }); |
michael@0 | 564 | yield download.start(); |
michael@0 | 565 | do_check_true(receivedOnSummaryChanged); |
michael@0 | 566 | }); |
michael@0 | 567 | |
michael@0 | 568 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 569 | //// Termination |
michael@0 | 570 | |
michael@0 | 571 | let tailFile = do_get_file("tail.js"); |
michael@0 | 572 | Services.scriptloader.loadSubScript(NetUtil.newURI(tailFile).spec); |