browser_privatebrowsing_downloadmonitor.js
Go to the documentation of this file.
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Private Browsing Tests.
15  *
16  * The Initial Developer of the Original Code is
17  * Ehsan Akhgari.
18  * Portions created by the Initial Developer are Copyright (C) 2008
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  * Ehsan Akhgari <ehsan.akhgari@gmail.com> (Original Author)
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either of the GNU General Public License Version 2 or later (the "GPL"),
26  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37 
38 // This test makes sure that the download monitor status bar panel is correctly
39 // cleared when switching the private browsing mode on or off.
40 
41 function test() {
42  // initialization
43  let pb = Cc["@mozilla.org/privatebrowsing;1"].
44  getService(Ci.nsIPrivateBrowsingService);
45  let dm = Cc["@mozilla.org/download-manager;1"].
46  getService(Ci.nsIDownloadManager);
47  if (!gDownloadMgr)
48  gDownloadMgr = dm;
49  let iosvc = Cc["@mozilla.org/network/io-service;1"].
50  getService(Ci.nsIIOService);
51  let panel = document.getElementById("download-monitor");
53 
54  // Add a new download
55  addDownload(dm, {
56  resultFileName: "pbtest-1",
57  downloadName: "PB Test 1"
58  });
59 
60  // Make sure that the download is being displayed in the monitor panel
61  if (!DownloadMonitorPanel.inited())
62  DownloadMonitorPanel.init();
63  else
64  DownloadMonitorPanel.updateStatus();
65  ok(!panel.hidden, "The download panel should be successfully added initially");
66 
67  // Enter the private browsing mode
68  pb.privateBrowsingEnabled = true;
69 
70  setTimeout(function () {
71  ok(panel.hidden, "The download panel should be hidden when entering the private browsing mode");
72 
73  // Add a new download
74  let file = addDownload(dm, {
75  resultFileName: "pbtest-2",
76  downloadName: "PB Test 2"
77  }).targetFile;
78 
79  // Update the panel
80  DownloadMonitorPanel.updateStatus();
81 
82  // Make sure that the panel is visible
83  ok(!panel.hidden, "The download panel should show up when a new download is added");
84 
85  // Exit the private browsing mode
86  pb.privateBrowsingEnabled = false;
87 
88  setTimeout(function () {
89  ok(panel.hidden, "The download panel should be hidden when leaving the private browsing mode");
90 
91  // cleanup
92  let dls = dm.activeDownloads;
93  while (dls.hasMoreElements()) {
94  let dl = dls.getNext().QueryInterface(Ci.nsIDownload);
95  dm.removeDownload(dl.id);
96  let file = dl.targetFile;
97  if (file.exists())
98  file.remove(false);
99  }
100  if (file.exists())
101  file.remove(false);
102 
103  finish();
104  }, 0);
105  }, 0);
106 }
107 
119 function addDownload(dm, aParams)
120 {
121  if (!aParams)
122  aParams = {};
123  if (!("resultFileName" in aParams))
124  aParams.resultFileName = "download.result";
125  if (!("targetFile" in aParams)) {
126  let dirSvc = Cc["@mozilla.org/file/directory_service;1"].
127  getService(Ci.nsIProperties);
128  aParams.targetFile = dirSvc.get("ProfD", Ci.nsIFile);
129  aParams.targetFile.append(aParams.resultFileName);
130  }
131  if (!("sourceURI" in aParams))
132  aParams.sourceURI = "http://localhost:8888/browser/browser/components/privatebrowsing/test/browser/staller.sjs";
133  if (!("downloadName" in aParams))
134  aParams.downloadName = null;
135  if (!("runBeforeStart" in aParams))
136  aParams.runBeforeStart = function () {};
137 
138  const nsIWBP = Ci.nsIWebBrowserPersist;
139  let persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
140  .createInstance(Ci.nsIWebBrowserPersist);
141  persist.persistFlags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
142  nsIWBP.PERSIST_FLAGS_BYPASS_CACHE |
143  nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
144 
145  let dl = dm.addDownload(Ci.nsIDownloadManager.DOWNLOAD_TYPE_DOWNLOAD,
146  createURI(aParams.sourceURI),
147  createURI(aParams.targetFile), aParams.downloadName, null,
148  Math.round(Date.now() * 1000), null, persist);
149 
150  // This will throw if it isn't found, and that would mean test failure, so no
151  // try catch block
152  let test = dm.getDownload(dl.id);
153 
154  aParams.runBeforeStart.call(undefined, dl);
155 
156  persist.progressListener = dl.QueryInterface(Ci.nsIWebProgressListener);
157  persist.saveURI(dl.source, null, null, null, null, dl.targetFile);
158 
159  return dl;
160 }
161 
162 function createURI(aObj)
163 {
164  let ios = Cc["@mozilla.org/network/io-service;1"].
165  getService(Ci.nsIIOService);
166  return (aObj instanceof Ci.nsIFile) ? ios.newFileURI(aObj) :
167  ios.newURI(aObj, null, null);
168 }
const Cc
var dirSvc
getService(Ci.sbIFaceplateManager)
let gDownloadMgr
Definition: browser.js:81
waitForExplicitFinish()
aWindow setTimeout(function(){_this.restoreHistory(aWindow, aTabs, aTabData, aIdMap);}, 0)
return null
Definition: FeedWriter.js:1143
let DownloadMonitorPanel
Definition: browser.js:6808
const Ci
var iosvc
var ios
Definition: head_feeds.js:5
function addDownload(dm, aParams)
var file