|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
|
2 * vim: sw=2 ts=2 sts=2 |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 /** |
|
8 * Test added with bug 460086 to test the behavior of the new API that was added |
|
9 * to remove all traces of visiting a site. |
|
10 */ |
|
11 |
|
12 //////////////////////////////////////////////////////////////////////////////// |
|
13 //// Globals |
|
14 |
|
15 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
|
16 Cu.import("resource://gre/modules/Services.jsm"); |
|
17 Cu.import("resource://gre/modules/PlacesUtils.jsm"); |
|
18 Cu.import("resource://gre/modules/ForgetAboutSite.jsm"); |
|
19 |
|
20 XPCOMUtils.defineLazyModuleGetter(this, "Promise", |
|
21 "resource://gre/modules/Promise.jsm"); |
|
22 |
|
23 const COOKIE_EXPIRY = Math.round(Date.now() / 1000) + 60; |
|
24 const COOKIE_NAME = "testcookie"; |
|
25 const COOKIE_PATH = "/"; |
|
26 |
|
27 const LOGIN_USERNAME = "username"; |
|
28 const LOGIN_PASSWORD = "password"; |
|
29 const LOGIN_USERNAME_FIELD = "username_field"; |
|
30 const LOGIN_PASSWORD_FIELD = "password_field"; |
|
31 |
|
32 const PERMISSION_TYPE = "test-perm"; |
|
33 const PERMISSION_VALUE = Ci.nsIPermissionManager.ALLOW_ACTION; |
|
34 |
|
35 const PREFERENCE_NAME = "test-pref"; |
|
36 |
|
37 //////////////////////////////////////////////////////////////////////////////// |
|
38 //// Utility Functions |
|
39 |
|
40 /** |
|
41 * Creates an nsIURI object for the given string representation of a URI. |
|
42 * |
|
43 * @param aURIString |
|
44 * The spec of the URI to create. |
|
45 * @returns an nsIURI representing aURIString. |
|
46 */ |
|
47 function uri(aURIString) |
|
48 { |
|
49 return Cc["@mozilla.org/network/io-service;1"]. |
|
50 getService(Ci.nsIIOService). |
|
51 newURI(aURIString, null, null); |
|
52 } |
|
53 |
|
54 /** |
|
55 * Asynchronously adds visits to a page. |
|
56 * |
|
57 * @param aPlaceInfo |
|
58 * Can be an nsIURI, in such a case a single LINK visit will be added. |
|
59 * Otherwise can be an object describing the visit to add, or an array |
|
60 * of these objects: |
|
61 * { uri: nsIURI of the page, |
|
62 * transition: one of the TRANSITION_* from nsINavHistoryService, |
|
63 * [optional] title: title of the page, |
|
64 * [optional] visitDate: visit date in microseconds from the epoch |
|
65 * [optional] referrer: nsIURI of the referrer for this visit |
|
66 * } |
|
67 * |
|
68 * @return {Promise} |
|
69 * @resolves When all visits have been added successfully. |
|
70 * @rejects JavaScript exception. |
|
71 */ |
|
72 function promiseAddVisits(aPlaceInfo) |
|
73 { |
|
74 let deferred = Promise.defer(); |
|
75 let places = []; |
|
76 if (aPlaceInfo instanceof Ci.nsIURI) { |
|
77 places.push({ uri: aPlaceInfo }); |
|
78 } |
|
79 else if (Array.isArray(aPlaceInfo)) { |
|
80 places = places.concat(aPlaceInfo); |
|
81 } else { |
|
82 places.push(aPlaceInfo) |
|
83 } |
|
84 |
|
85 // Create mozIVisitInfo for each entry. |
|
86 let now = Date.now(); |
|
87 for (let i = 0; i < places.length; i++) { |
|
88 if (!places[i].title) { |
|
89 places[i].title = "test visit for " + places[i].uri.spec; |
|
90 } |
|
91 places[i].visits = [{ |
|
92 transitionType: places[i].transition === undefined ? Ci.nsINavHistoryService.TRANSITION_LINK |
|
93 : places[i].transition, |
|
94 visitDate: places[i].visitDate || (now++) * 1000, |
|
95 referrerURI: places[i].referrer |
|
96 }]; |
|
97 } |
|
98 |
|
99 PlacesUtils.asyncHistory.updatePlaces( |
|
100 places, |
|
101 { |
|
102 handleError: function handleError(aResultCode, aPlaceInfo) { |
|
103 let ex = new Components.Exception("Unexpected error in adding visits.", |
|
104 aResultCode); |
|
105 deferred.reject(ex); |
|
106 }, |
|
107 handleResult: function () {}, |
|
108 handleCompletion: function handleCompletion() { |
|
109 deferred.resolve(); |
|
110 } |
|
111 } |
|
112 ); |
|
113 |
|
114 return deferred.promise; |
|
115 } |
|
116 |
|
117 |
|
118 /** |
|
119 * Asynchronously check a url is visited. |
|
120 * |
|
121 * @param aURI |
|
122 * The URI. |
|
123 * |
|
124 * @return {Promise} |
|
125 * @resolves When the check has been added successfully. |
|
126 * @rejects JavaScript exception. |
|
127 */ |
|
128 function promiseIsURIVisited(aURI) |
|
129 { |
|
130 let deferred = Promise.defer(); |
|
131 PlacesUtils.asyncHistory.isURIVisited(aURI, function(aURI, aIsVisited) { |
|
132 deferred.resolve(aIsVisited); |
|
133 }); |
|
134 |
|
135 return deferred.promise; |
|
136 } |
|
137 |
|
138 /** |
|
139 * Add a cookie to the cookie service. |
|
140 * |
|
141 * @param aDomain |
|
142 */ |
|
143 function add_cookie(aDomain) |
|
144 { |
|
145 check_cookie_exists(aDomain, false); |
|
146 let cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2); |
|
147 cm.add(aDomain, COOKIE_PATH, COOKIE_NAME, "", false, false, false, |
|
148 COOKIE_EXPIRY); |
|
149 check_cookie_exists(aDomain, true); |
|
150 } |
|
151 |
|
152 /** |
|
153 * Checks to ensure that a cookie exists or not for a domain. |
|
154 * |
|
155 * @param aDomain |
|
156 * The domain to check for the cookie. |
|
157 * @param aExists |
|
158 * True if the cookie should exist, false otherwise. |
|
159 */ |
|
160 function check_cookie_exists(aDomain, aExists) |
|
161 { |
|
162 let cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2); |
|
163 let cookie = { |
|
164 host: aDomain, |
|
165 name: COOKIE_NAME, |
|
166 path: COOKIE_PATH |
|
167 } |
|
168 let checker = aExists ? do_check_true : do_check_false; |
|
169 checker(cm.cookieExists(cookie)); |
|
170 } |
|
171 |
|
172 /** |
|
173 * Adds a download to download history. |
|
174 * |
|
175 * @param aURIString |
|
176 * The string of the URI to add. |
|
177 * @param aIsActive |
|
178 * If it should be set to an active state in the database. This does not |
|
179 * make it show up in the list of active downloads however! |
|
180 */ |
|
181 function add_download(aURIString, aIsActive) |
|
182 { |
|
183 function makeGUID() { |
|
184 let guid = ""; |
|
185 for (var i = 0; i < 12; i++) |
|
186 guid += Math.floor(Math.random() * 10); |
|
187 return guid; |
|
188 } |
|
189 |
|
190 check_downloaded(aURIString, false); |
|
191 let db = Cc["@mozilla.org/download-manager;1"]. |
|
192 getService(Ci.nsIDownloadManager). |
|
193 DBConnection; |
|
194 let stmt = db.createStatement( |
|
195 "INSERT INTO moz_downloads (source, state, guid) " + |
|
196 "VALUES (:source, :state, :guid)" |
|
197 ); |
|
198 stmt.params.source = aURIString; |
|
199 stmt.params.state = aIsActive ? Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING : |
|
200 Ci.nsIDownloadManager.DOWNLOAD_FINISHED; |
|
201 stmt.params.guid = makeGUID(); |
|
202 try { |
|
203 stmt.execute(); |
|
204 } |
|
205 finally { |
|
206 stmt.finalize(); |
|
207 } |
|
208 check_downloaded(aURIString, true); |
|
209 } |
|
210 |
|
211 /** |
|
212 * Checks to ensure a URI string is in download history or not. |
|
213 * |
|
214 * @param aURIString |
|
215 * The string of the URI to check. |
|
216 * @param aIsDownloaded |
|
217 * True if the URI should be downloaded, false otherwise. |
|
218 */ |
|
219 function check_downloaded(aURIString, aIsDownloaded) |
|
220 { |
|
221 let db = Cc["@mozilla.org/download-manager;1"]. |
|
222 getService(Ci.nsIDownloadManager). |
|
223 DBConnection; |
|
224 let stmt = db.createStatement( |
|
225 "SELECT * " + |
|
226 "FROM moz_downloads " + |
|
227 "WHERE source = :source" |
|
228 ); |
|
229 stmt.params.source = aURIString; |
|
230 |
|
231 let checker = aIsDownloaded ? do_check_true : do_check_false; |
|
232 try { |
|
233 checker(stmt.executeStep()); |
|
234 } |
|
235 finally { |
|
236 stmt.finalize(); |
|
237 } |
|
238 } |
|
239 |
|
240 /** |
|
241 * Adds a disabled host to the login manager. |
|
242 * |
|
243 * @param aHost |
|
244 * The host to add to the list of disabled hosts. |
|
245 */ |
|
246 function add_disabled_host(aHost) |
|
247 { |
|
248 check_disabled_host(aHost, false); |
|
249 let lm = Cc["@mozilla.org/login-manager;1"]. |
|
250 getService(Ci.nsILoginManager); |
|
251 lm.setLoginSavingEnabled(aHost, false); |
|
252 check_disabled_host(aHost, true); |
|
253 } |
|
254 |
|
255 /** |
|
256 * Checks to see if a host is disabled for storing logins or not. |
|
257 * |
|
258 * @param aHost |
|
259 * The host to check if it is disabled. |
|
260 * @param aIsDisabled |
|
261 * True if the host should be disabled, false otherwise. |
|
262 */ |
|
263 function check_disabled_host(aHost, aIsDisabled) |
|
264 { |
|
265 let lm = Cc["@mozilla.org/login-manager;1"]. |
|
266 getService(Ci.nsILoginManager); |
|
267 let checker = aIsDisabled ? do_check_false : do_check_true; |
|
268 checker(lm.getLoginSavingEnabled(aHost)); |
|
269 } |
|
270 |
|
271 /** |
|
272 * Adds a login for the specified host to the login manager. |
|
273 * |
|
274 * @param aHost |
|
275 * The host to add the login for. |
|
276 */ |
|
277 function add_login(aHost) |
|
278 { |
|
279 check_login_exists(aHost, false); |
|
280 let login = Cc["@mozilla.org/login-manager/loginInfo;1"]. |
|
281 createInstance(Ci.nsILoginInfo); |
|
282 login.init(aHost, "", null, LOGIN_USERNAME, LOGIN_PASSWORD, |
|
283 LOGIN_USERNAME_FIELD, LOGIN_PASSWORD_FIELD); |
|
284 let lm = Cc["@mozilla.org/login-manager;1"]. |
|
285 getService(Ci.nsILoginManager); |
|
286 lm.addLogin(login); |
|
287 check_login_exists(aHost, true); |
|
288 } |
|
289 |
|
290 /** |
|
291 * Checks to see if a login exists for a host. |
|
292 * |
|
293 * @param aHost |
|
294 * The host to check for the test login. |
|
295 * @param aExists |
|
296 * True if the login should exist, false otherwise. |
|
297 */ |
|
298 function check_login_exists(aHost, aExists) |
|
299 { |
|
300 let lm = Cc["@mozilla.org/login-manager;1"]. |
|
301 getService(Ci.nsILoginManager); |
|
302 let count = { value: 0 }; |
|
303 lm.findLogins(count, aHost, "", null); |
|
304 do_check_eq(count.value, aExists ? 1 : 0); |
|
305 } |
|
306 |
|
307 /** |
|
308 * Adds a permission for the specified URI to the permission manager. |
|
309 * |
|
310 * @param aURI |
|
311 * The URI to add the test permission for. |
|
312 */ |
|
313 function add_permission(aURI) |
|
314 { |
|
315 check_permission_exists(aURI, false); |
|
316 let pm = Cc["@mozilla.org/permissionmanager;1"]. |
|
317 getService(Ci.nsIPermissionManager); |
|
318 let principal = Cc["@mozilla.org/scriptsecuritymanager;1"] |
|
319 .getService(Ci.nsIScriptSecurityManager) |
|
320 .getNoAppCodebasePrincipal(aURI); |
|
321 |
|
322 pm.addFromPrincipal(principal, PERMISSION_TYPE, PERMISSION_VALUE); |
|
323 check_permission_exists(aURI, true); |
|
324 } |
|
325 |
|
326 /** |
|
327 * Checks to see if a permission exists for the given URI. |
|
328 * |
|
329 * @param aURI |
|
330 * The URI to check if a permission exists. |
|
331 * @param aExists |
|
332 * True if the permission should exist, false otherwise. |
|
333 */ |
|
334 function check_permission_exists(aURI, aExists) |
|
335 { |
|
336 let pm = Cc["@mozilla.org/permissionmanager;1"]. |
|
337 getService(Ci.nsIPermissionManager); |
|
338 let principal = Cc["@mozilla.org/scriptsecuritymanager;1"] |
|
339 .getService(Ci.nsIScriptSecurityManager) |
|
340 .getNoAppCodebasePrincipal(aURI); |
|
341 |
|
342 let perm = pm.testExactPermissionFromPrincipal(principal, PERMISSION_TYPE); |
|
343 let checker = aExists ? do_check_eq : do_check_neq; |
|
344 checker(perm, PERMISSION_VALUE); |
|
345 } |
|
346 |
|
347 /** |
|
348 * Adds a content preference for the specified URI. |
|
349 * |
|
350 * @param aURI |
|
351 * The URI to add a preference for. |
|
352 */ |
|
353 function add_preference(aURI) |
|
354 { |
|
355 let deferred = Promise.defer(); |
|
356 let cp = Cc["@mozilla.org/content-pref/service;1"]. |
|
357 getService(Ci.nsIContentPrefService2); |
|
358 cp.set(aURI.spec, PREFERENCE_NAME, "foo", null, { |
|
359 handleCompletion: function() deferred.resolve() |
|
360 }); |
|
361 return deferred.promise; |
|
362 } |
|
363 |
|
364 /** |
|
365 * Checks to see if a content preference exists for the given URI. |
|
366 * |
|
367 * @param aURI |
|
368 * The URI to check if a preference exists. |
|
369 */ |
|
370 function preference_exists(aURI) |
|
371 { |
|
372 let deferred = Promise.defer(); |
|
373 let cp = Cc["@mozilla.org/content-pref/service;1"]. |
|
374 getService(Ci.nsIContentPrefService2); |
|
375 let exists = false; |
|
376 cp.getByDomainAndName(aURI.spec, PREFERENCE_NAME, null, { |
|
377 handleResult: function() exists = true, |
|
378 handleCompletion: function() deferred.resolve(exists) |
|
379 }); |
|
380 return deferred.promise; |
|
381 } |
|
382 |
|
383 //////////////////////////////////////////////////////////////////////////////// |
|
384 //// Test Functions |
|
385 |
|
386 // History |
|
387 function test_history_cleared_with_direct_match() |
|
388 { |
|
389 const TEST_URI = uri("http://mozilla.org/foo"); |
|
390 do_check_false(yield promiseIsURIVisited(TEST_URI)); |
|
391 yield promiseAddVisits(TEST_URI); |
|
392 do_check_true(yield promiseIsURIVisited(TEST_URI)); |
|
393 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
394 do_check_false(yield promiseIsURIVisited(TEST_URI)); |
|
395 } |
|
396 |
|
397 function test_history_cleared_with_subdomain() |
|
398 { |
|
399 const TEST_URI = uri("http://www.mozilla.org/foo"); |
|
400 do_check_false(yield promiseIsURIVisited(TEST_URI)); |
|
401 yield promiseAddVisits(TEST_URI); |
|
402 do_check_true(yield promiseIsURIVisited(TEST_URI)); |
|
403 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
404 do_check_false(yield promiseIsURIVisited(TEST_URI)); |
|
405 } |
|
406 |
|
407 function test_history_not_cleared_with_uri_contains_domain() |
|
408 { |
|
409 const TEST_URI = uri("http://ilovemozilla.org/foo"); |
|
410 do_check_false(yield promiseIsURIVisited(TEST_URI)); |
|
411 yield promiseAddVisits(TEST_URI); |
|
412 do_check_true(yield promiseIsURIVisited(TEST_URI)); |
|
413 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
414 do_check_true(yield promiseIsURIVisited(TEST_URI)); |
|
415 |
|
416 // Clear history since we left something there from this test. |
|
417 PlacesUtils.bhistory.removeAllPages(); |
|
418 } |
|
419 |
|
420 // Cookie Service |
|
421 function test_cookie_cleared_with_direct_match() |
|
422 { |
|
423 const TEST_DOMAIN = "mozilla.org"; |
|
424 add_cookie(TEST_DOMAIN); |
|
425 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
426 check_cookie_exists(TEST_DOMAIN, false); |
|
427 } |
|
428 |
|
429 function test_cookie_cleared_with_subdomain() |
|
430 { |
|
431 const TEST_DOMAIN = "www.mozilla.org"; |
|
432 add_cookie(TEST_DOMAIN); |
|
433 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
434 check_cookie_exists(TEST_DOMAIN, false); |
|
435 } |
|
436 |
|
437 function test_cookie_not_cleared_with_uri_contains_domain() |
|
438 { |
|
439 const TEST_DOMAIN = "ilovemozilla.org"; |
|
440 add_cookie(TEST_DOMAIN); |
|
441 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
442 check_cookie_exists(TEST_DOMAIN, true); |
|
443 } |
|
444 |
|
445 // Download Manager |
|
446 function test_download_history_cleared_with_direct_match() |
|
447 { |
|
448 if (oldDownloadManagerDisabled()) { |
|
449 return; |
|
450 } |
|
451 |
|
452 const TEST_URI = "http://mozilla.org/foo"; |
|
453 add_download(TEST_URI, false); |
|
454 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
455 check_downloaded(TEST_URI, false); |
|
456 } |
|
457 |
|
458 function test_download_history_cleared_with_subdomain() |
|
459 { |
|
460 if (oldDownloadManagerDisabled()) { |
|
461 return; |
|
462 } |
|
463 |
|
464 const TEST_URI = "http://www.mozilla.org/foo"; |
|
465 add_download(TEST_URI, false); |
|
466 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
467 check_downloaded(TEST_URI, false); |
|
468 } |
|
469 |
|
470 function test_download_history_not_cleared_with_active_direct_match() |
|
471 { |
|
472 if (oldDownloadManagerDisabled()) { |
|
473 return; |
|
474 } |
|
475 |
|
476 // Tests that downloads marked as active in the db are not deleted from the db |
|
477 const TEST_URI = "http://mozilla.org/foo"; |
|
478 add_download(TEST_URI, true); |
|
479 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
480 check_downloaded(TEST_URI, true); |
|
481 |
|
482 // Reset state |
|
483 let db = Cc["@mozilla.org/download-manager;1"]. |
|
484 getService(Ci.nsIDownloadManager). |
|
485 DBConnection; |
|
486 db.executeSimpleSQL("DELETE FROM moz_downloads"); |
|
487 check_downloaded(TEST_URI, false); |
|
488 } |
|
489 |
|
490 // Login Manager |
|
491 function test_login_manager_disabled_hosts_cleared_with_direct_match() |
|
492 { |
|
493 const TEST_HOST = "http://mozilla.org"; |
|
494 add_disabled_host(TEST_HOST); |
|
495 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
496 check_disabled_host(TEST_HOST, false); |
|
497 } |
|
498 |
|
499 function test_login_manager_disabled_hosts_cleared_with_subdomain() |
|
500 { |
|
501 const TEST_HOST = "http://www.mozilla.org"; |
|
502 add_disabled_host(TEST_HOST); |
|
503 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
504 check_disabled_host(TEST_HOST, false); |
|
505 } |
|
506 |
|
507 function test_login_manager_disabled_hosts_not_cleared_with_uri_contains_domain() |
|
508 { |
|
509 const TEST_HOST = "http://ilovemozilla.org"; |
|
510 add_disabled_host(TEST_HOST); |
|
511 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
512 check_disabled_host(TEST_HOST, true); |
|
513 |
|
514 // Reset state |
|
515 let lm = Cc["@mozilla.org/login-manager;1"]. |
|
516 getService(Ci.nsILoginManager); |
|
517 lm.setLoginSavingEnabled(TEST_HOST, true); |
|
518 check_disabled_host(TEST_HOST, false); |
|
519 } |
|
520 |
|
521 function test_login_manager_logins_cleared_with_direct_match() |
|
522 { |
|
523 const TEST_HOST = "http://mozilla.org"; |
|
524 add_login(TEST_HOST); |
|
525 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
526 check_login_exists(TEST_HOST, false); |
|
527 } |
|
528 |
|
529 function test_login_manager_logins_cleared_with_subdomain() |
|
530 { |
|
531 const TEST_HOST = "http://www.mozilla.org"; |
|
532 add_login(TEST_HOST); |
|
533 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
534 check_login_exists(TEST_HOST, false); |
|
535 } |
|
536 |
|
537 function tets_login_manager_logins_not_cleared_with_uri_contains_domain() |
|
538 { |
|
539 const TEST_HOST = "http://ilovemozilla.org"; |
|
540 add_login(TEST_HOST); |
|
541 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
542 check_login_exists(TEST_HOST, true); |
|
543 |
|
544 let lm = Cc["@mozilla.org/login-manager;1"]. |
|
545 getService(Ci.nsILoginManager); |
|
546 lm.removeAllLogins(); |
|
547 check_login_exists(TEST_HOST, false); |
|
548 } |
|
549 |
|
550 // Permission Manager |
|
551 function test_permission_manager_cleared_with_direct_match() |
|
552 { |
|
553 const TEST_URI = uri("http://mozilla.org"); |
|
554 add_permission(TEST_URI); |
|
555 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
556 check_permission_exists(TEST_URI, false); |
|
557 } |
|
558 |
|
559 function test_permission_manager_cleared_with_subdomain() |
|
560 { |
|
561 const TEST_URI = uri("http://www.mozilla.org"); |
|
562 add_permission(TEST_URI); |
|
563 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
564 check_permission_exists(TEST_URI, false); |
|
565 } |
|
566 |
|
567 function test_permission_manager_not_cleared_with_uri_contains_domain() |
|
568 { |
|
569 const TEST_URI = uri("http://ilovemozilla.org"); |
|
570 add_permission(TEST_URI); |
|
571 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
572 check_permission_exists(TEST_URI, true); |
|
573 |
|
574 // Reset state |
|
575 let pm = Cc["@mozilla.org/permissionmanager;1"]. |
|
576 getService(Ci.nsIPermissionManager); |
|
577 pm.removeAll(); |
|
578 check_permission_exists(TEST_URI, false); |
|
579 } |
|
580 |
|
581 function waitForPurgeNotification() { |
|
582 let deferred = Promise.defer(); |
|
583 |
|
584 let observer = { |
|
585 observe: function(aSubject, aTopic, aData) |
|
586 { |
|
587 Services.obs.removeObserver(observer, "browser:purge-domain-data"); |
|
588 // test_storage_cleared needs this extra executeSoon because |
|
589 // the DOMStorage clean-up is also listening to this same observer |
|
590 // which is run synchronously. |
|
591 Services.tm.mainThread.dispatch(function() { |
|
592 deferred.resolve(); |
|
593 }, Components.interfaces.nsIThread.DISPATCH_NORMAL); |
|
594 } |
|
595 }; |
|
596 Services.obs.addObserver(observer, "browser:purge-domain-data", false); |
|
597 |
|
598 return deferred.promise; |
|
599 } |
|
600 |
|
601 // Content Preferences |
|
602 function test_content_preferences_cleared_with_direct_match() |
|
603 { |
|
604 const TEST_URI = uri("http://mozilla.org"); |
|
605 do_check_false(yield preference_exists(TEST_URI)); |
|
606 yield add_preference(TEST_URI); |
|
607 do_check_true(yield preference_exists(TEST_URI)); |
|
608 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
609 yield waitForPurgeNotification(); |
|
610 do_check_false(yield preference_exists(TEST_URI)); |
|
611 } |
|
612 |
|
613 function test_content_preferences_cleared_with_subdomain() |
|
614 { |
|
615 const TEST_URI = uri("http://www.mozilla.org"); |
|
616 do_check_false(yield preference_exists(TEST_URI)); |
|
617 yield add_preference(TEST_URI); |
|
618 do_check_true(yield preference_exists(TEST_URI)); |
|
619 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
620 yield waitForPurgeNotification(); |
|
621 do_check_false(yield preference_exists(TEST_URI)); |
|
622 } |
|
623 |
|
624 function test_content_preferences_not_cleared_with_uri_contains_domain() |
|
625 { |
|
626 const TEST_URI = uri("http://ilovemozilla.org"); |
|
627 do_check_false(yield preference_exists(TEST_URI)); |
|
628 yield add_preference(TEST_URI); |
|
629 do_check_true(yield preference_exists(TEST_URI)); |
|
630 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
631 yield waitForPurgeNotification(); |
|
632 do_check_true(yield preference_exists(TEST_URI)); |
|
633 |
|
634 // Reset state |
|
635 ForgetAboutSite.removeDataFromDomain("ilovemozilla.org"); |
|
636 yield waitForPurgeNotification(); |
|
637 do_check_false(yield preference_exists(TEST_URI)); |
|
638 } |
|
639 |
|
640 // Cache |
|
641 function test_cache_cleared() |
|
642 { |
|
643 // Because this test is asynchronous, it should be the last test |
|
644 do_check_true(tests[tests.length - 1] == arguments.callee); |
|
645 |
|
646 // NOTE: We could be more extensive with this test and actually add an entry |
|
647 // to the cache, and then make sure it is gone. However, we trust that |
|
648 // the API is well tested, and that when we get the observer |
|
649 // notification, we have actually cleared the cache. |
|
650 // This seems to happen asynchronously... |
|
651 let os = Cc["@mozilla.org/observer-service;1"]. |
|
652 getService(Ci.nsIObserverService); |
|
653 let observer = { |
|
654 observe: function(aSubject, aTopic, aData) |
|
655 { |
|
656 os.removeObserver(observer, "cacheservice:empty-cache"); |
|
657 // Shutdown the download manager. |
|
658 Services.obs.notifyObservers(null, "quit-application", null); |
|
659 do_test_finished(); |
|
660 } |
|
661 }; |
|
662 os.addObserver(observer, "cacheservice:empty-cache", false); |
|
663 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
664 do_test_pending(); |
|
665 } |
|
666 |
|
667 function test_storage_cleared() |
|
668 { |
|
669 function getStorageForURI(aURI) |
|
670 { |
|
671 let principal = Cc["@mozilla.org/scriptsecuritymanager;1"]. |
|
672 getService(Ci.nsIScriptSecurityManager). |
|
673 getNoAppCodebasePrincipal(aURI); |
|
674 let dsm = Cc["@mozilla.org/dom/localStorage-manager;1"]. |
|
675 getService(Ci.nsIDOMStorageManager); |
|
676 return dsm.createStorage(principal, ""); |
|
677 } |
|
678 |
|
679 let s = [ |
|
680 getStorageForURI(uri("http://mozilla.org")), |
|
681 getStorageForURI(uri("http://my.mozilla.org")), |
|
682 getStorageForURI(uri("http://ilovemozilla.org")), |
|
683 ]; |
|
684 |
|
685 for (let i = 0; i < s.length; ++i) { |
|
686 let storage = s[i]; |
|
687 storage.setItem("test", "value" + i); |
|
688 do_check_eq(storage.length, 1); |
|
689 do_check_eq(storage.key(0), "test"); |
|
690 do_check_eq(storage.getItem("test"), "value" + i); |
|
691 } |
|
692 |
|
693 ForgetAboutSite.removeDataFromDomain("mozilla.org"); |
|
694 yield waitForPurgeNotification(); |
|
695 |
|
696 do_check_eq(s[0].getItem("test"), null); |
|
697 do_check_eq(s[0].length, 0); |
|
698 do_check_eq(s[1].getItem("test"), null); |
|
699 do_check_eq(s[1].length, 0); |
|
700 do_check_eq(s[2].getItem("test"), "value2"); |
|
701 do_check_eq(s[2].length, 1); |
|
702 } |
|
703 |
|
704 let tests = [ |
|
705 // History |
|
706 test_history_cleared_with_direct_match, |
|
707 test_history_cleared_with_subdomain, |
|
708 test_history_not_cleared_with_uri_contains_domain, |
|
709 |
|
710 // Cookie Service |
|
711 test_cookie_cleared_with_direct_match, |
|
712 test_cookie_cleared_with_subdomain, |
|
713 test_cookie_not_cleared_with_uri_contains_domain, |
|
714 |
|
715 // Download Manager |
|
716 // Note: active downloads tested in test_removeDataFromDomain_activeDownloads.js |
|
717 test_download_history_cleared_with_direct_match, |
|
718 test_download_history_cleared_with_subdomain, |
|
719 test_download_history_not_cleared_with_active_direct_match, |
|
720 |
|
721 // Login Manager |
|
722 test_login_manager_disabled_hosts_cleared_with_direct_match, |
|
723 test_login_manager_disabled_hosts_cleared_with_subdomain, |
|
724 test_login_manager_disabled_hosts_not_cleared_with_uri_contains_domain, |
|
725 test_login_manager_logins_cleared_with_direct_match, |
|
726 test_login_manager_logins_cleared_with_subdomain, |
|
727 tets_login_manager_logins_not_cleared_with_uri_contains_domain, |
|
728 |
|
729 // Permission Manager |
|
730 test_permission_manager_cleared_with_direct_match, |
|
731 test_permission_manager_cleared_with_subdomain, |
|
732 test_permission_manager_not_cleared_with_uri_contains_domain, |
|
733 |
|
734 // Content Preferences |
|
735 test_content_preferences_cleared_with_direct_match, |
|
736 test_content_preferences_cleared_with_subdomain, |
|
737 test_content_preferences_not_cleared_with_uri_contains_domain, |
|
738 |
|
739 // Storage |
|
740 test_storage_cleared, |
|
741 |
|
742 // Cache |
|
743 test_cache_cleared, |
|
744 ]; |
|
745 |
|
746 function run_test() |
|
747 { |
|
748 for (let i = 0; i < tests.length; i++) |
|
749 add_task(tests[i]); |
|
750 |
|
751 run_next_test(); |
|
752 } |