do_test_removeDataFromDomain_activeDownloads.js
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: sw=2 ts=2 sts=2
3  * ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is Private Browsing Tests.
17  *
18  * The Initial Developer of the Original Code is
19  * Mozilla Corporation.
20  * Portions created by the Initial Developer are Copyright (C) 2008
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
25  *
26  * Alternatively, the contents of this file may be used under the terms of
27  * either of the GNU General Public License Version 2 or later (the "GPL"),
28  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29  * in which case the provisions of the GPL or the LGPL are applicable instead
30  * of those above. If you wish to allow use of your version of this file only
31  * under the terms of either the GPL or the LGPL, and not to allow others to
32  * use your version of this file under the terms of the MPL, indicate your
33  * decision by deleting the provisions above and replace them with the notice
34  * and other provisions required by the GPL or the LGPL. If you do not delete
35  * the provisions above, a recipient may use your version of this file under
36  * the terms of any one of the MPL, the GPL or the LGPL.
37  *
38  * ***** END LICENSE BLOCK ***** */
39 
45 
49  getService(Ci.nsIPrivateBrowsingService);
50 
53 
61 function uri(aFile)
62 {
63  return Cc["@mozilla.org/network/io-service;1"].
64  getService(Ci.nsIIOService).
65  newFileURI(aFile);
66 }
67 
76 function check_active_download(aURIString, aIsActive)
77 {
78  let dm = Cc["@mozilla.org/download-manager;1"].
79  getService(Ci.nsIDownloadManager);
80  let enumerator = dm.activeDownloads;
81  let found = false;
82  while (enumerator.hasMoreElements()) {
83  let dl = enumerator.getNext().QueryInterface(Ci.nsIDownload);
84  if (dl.source.spec == aURIString)
85  found = true;
86  }
87  let checker = aIsActive ? do_check_true : do_check_false;
88  checker(found);
89 }
90 
93 
94 let destFile = dirSvc.get("TmpD", Ci.nsIFile);
95 destFile.append("dm-test-file");
97 let data = [
98  { source: "http://mozilla.org/direct_match",
99  target: destFile.spec,
100  removed: true
101  },
102  { source: "http://www.mozilla.org/subdomain",
103  target: destFile.spec,
104  removed: true
105  },
106  { source: "http://ilovemozilla.org/contains_domain",
107  target: destFile.spec,
108  removed: false
109  },
110 ];
111 
112 function do_test()
113 {
114  // We add this data to the database first, but we cannot instantiate the
115  // download manager service, otherwise these downloads will not be placed in
116  // the active downloads array.
117 
118  // Copy the empty downloads database to our profile directory
119  let downloads = do_get_file("downloads.empty.sqlite");
120  downloads.copyTo(dirSvc.get("ProfD", Ci.nsIFile), "downloads.sqlite");
121 
122  // Open the database
123  let ss = Cc["@mozilla.org/storage/service;1"].
124  getService(Ci.mozIStorageService);
125  let file = dirSvc.get("ProfD", Ci.nsIFile);
126  file.append("downloads.sqlite");
127  let db = ss.openDatabase(file);
128 
129  // Insert the data
130  let stmt = db.createStatement(
131  "INSERT INTO moz_downloads (source, target, state, autoResume, entityID) " +
132  "VALUES (:source, :target, :state, :autoResume, :entityID)"
133  );
134  for (let i = 0; i < data.length; i++) {
135  stmt.params.source = data[i].source;
136  stmt.params.target = data[i].target;
137  stmt.params.state = Ci.nsIDownloadManager.DOWNLOAD_PAUSED;
138  stmt.params.autoResume = 0; // DONT_RESUME is 0
139  stmt.params.entityID = "foo" // just has to be non-null for our test
140  stmt.execute();
141  stmt.reset();
142  }
143  stmt.finalize();
144  stmt = null;
145  db.close();
146  db = null;
147 
148  // Check to make sure it's all there
149  for (let i = 0; i < data.length; i++)
150  check_active_download(data[i].source, true);
151 
152  // Dispatch the remove call
153  pb.removeDataFromDomain("mozilla.org");
154 
155  // And check our data
156  for (let i = 0; i < data.length; i++)
157  check_active_download(data[i].source, !data[i].removed);
158 
159  shutdownPlaces();
160 }
const Cc
var dirSvc
function shutdownPlaces()
getService(Ci.sbIFaceplateManager)
function newFileURI(file)
return null
Definition: FeedWriter.js:1143
const Ci
var PRIVATEBROWSING_CONTRACT_ID
_getSelectedPageStyle s i
function check_active_download(aURIString, aIsActive)
var file