browser/components/downloads/test/browser/head.js

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

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 * Provides infrastructure for automated download components tests.
michael@0 8 */
michael@0 9
michael@0 10 ////////////////////////////////////////////////////////////////////////////////
michael@0 11 //// Globals
michael@0 12
michael@0 13 XPCOMUtils.defineLazyModuleGetter(this, "Downloads",
michael@0 14 "resource://gre/modules/Downloads.jsm");
michael@0 15 XPCOMUtils.defineLazyModuleGetter(this, "DownloadsCommon",
michael@0 16 "resource:///modules/DownloadsCommon.jsm");
michael@0 17 XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
michael@0 18 "resource://gre/modules/FileUtils.jsm");
michael@0 19 XPCOMUtils.defineLazyModuleGetter(this, "Promise",
michael@0 20 "resource://gre/modules/Promise.jsm");
michael@0 21 XPCOMUtils.defineLazyModuleGetter(this, "Task",
michael@0 22 "resource://gre/modules/Task.jsm");
michael@0 23 const nsIDM = Ci.nsIDownloadManager;
michael@0 24
michael@0 25 let gTestTargetFile = FileUtils.getFile("TmpD", ["dm-ui-test.file"]);
michael@0 26 gTestTargetFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
michael@0 27 registerCleanupFunction(function () {
michael@0 28 gTestTargetFile.remove(false);
michael@0 29 });
michael@0 30
michael@0 31 ////////////////////////////////////////////////////////////////////////////////
michael@0 32 //// Infrastructure
michael@0 33
michael@0 34 function test()
michael@0 35 {
michael@0 36 waitForExplicitFinish();
michael@0 37 Task.spawn(test_task).then(null, ex => ok(false, ex)).then(finish);
michael@0 38 }
michael@0 39
michael@0 40 ////////////////////////////////////////////////////////////////////////////////
michael@0 41 //// Asynchronous support subroutines
michael@0 42
michael@0 43 function promiseFocus()
michael@0 44 {
michael@0 45 let deferred = Promise.defer();
michael@0 46 waitForFocus(deferred.resolve);
michael@0 47 return deferred.promise;
michael@0 48 }
michael@0 49
michael@0 50 function promisePanelOpened()
michael@0 51 {
michael@0 52 let deferred = Promise.defer();
michael@0 53
michael@0 54 if (DownloadsPanel.panel && DownloadsPanel.panel.state == "open") {
michael@0 55 return deferred.resolve();
michael@0 56 }
michael@0 57
michael@0 58 // Hook to wait until the panel is shown.
michael@0 59 let originalOnPopupShown = DownloadsPanel.onPopupShown;
michael@0 60 DownloadsPanel.onPopupShown = function () {
michael@0 61 DownloadsPanel.onPopupShown = originalOnPopupShown;
michael@0 62 originalOnPopupShown.apply(this, arguments);
michael@0 63
michael@0 64 // Defer to the next tick of the event loop so that we don't continue
michael@0 65 // processing during the DOM event handler itself.
michael@0 66 setTimeout(deferred.resolve, 0);
michael@0 67 };
michael@0 68
michael@0 69 return deferred.promise;
michael@0 70 }
michael@0 71
michael@0 72 function task_resetState()
michael@0 73 {
michael@0 74 // Remove all downloads.
michael@0 75 let publicList = yield Downloads.getList(Downloads.PUBLIC);
michael@0 76 let downloads = yield publicList.getAll();
michael@0 77 for (let download of downloads) {
michael@0 78 publicList.remove(download);
michael@0 79 yield download.finalize(true);
michael@0 80 }
michael@0 81
michael@0 82 DownloadsPanel.hidePanel();
michael@0 83
michael@0 84 yield promiseFocus();
michael@0 85 }
michael@0 86
michael@0 87 function task_addDownloads(aItems)
michael@0 88 {
michael@0 89 let startTimeMs = Date.now();
michael@0 90
michael@0 91 let publicList = yield Downloads.getList(Downloads.PUBLIC);
michael@0 92 for (let item of aItems) {
michael@0 93 publicList.add(yield Downloads.createDownload({
michael@0 94 source: "http://www.example.com/test-download.txt",
michael@0 95 target: gTestTargetFile,
michael@0 96 succeeded: item.state == nsIDM.DOWNLOAD_FINISHED,
michael@0 97 canceled: item.state == nsIDM.DOWNLOAD_CANCELED ||
michael@0 98 item.state == nsIDM.DOWNLOAD_PAUSED,
michael@0 99 error: item.state == nsIDM.DOWNLOAD_FAILED ? new Error("Failed.") : null,
michael@0 100 hasPartialData: item.state == nsIDM.DOWNLOAD_PAUSED,
michael@0 101 startTime: new Date(startTimeMs++),
michael@0 102 }));
michael@0 103 }
michael@0 104 }
michael@0 105
michael@0 106 function task_openPanel()
michael@0 107 {
michael@0 108 yield promiseFocus();
michael@0 109
michael@0 110 let promise = promisePanelOpened();
michael@0 111 DownloadsPanel.showPanel();
michael@0 112 yield promise;
michael@0 113 }

mercurial