michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 sts=2 michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * Test added with bug 460086 to test the behavior of the new API that was added michael@0: * to remove all traces of visiting a site. michael@0: */ michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Globals michael@0: michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: Cu.import("resource://gre/modules/PlacesUtils.jsm"); michael@0: Cu.import("resource://gre/modules/ForgetAboutSite.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyModuleGetter(this, "Promise", michael@0: "resource://gre/modules/Promise.jsm"); michael@0: michael@0: const COOKIE_EXPIRY = Math.round(Date.now() / 1000) + 60; michael@0: const COOKIE_NAME = "testcookie"; michael@0: const COOKIE_PATH = "/"; michael@0: michael@0: const LOGIN_USERNAME = "username"; michael@0: const LOGIN_PASSWORD = "password"; michael@0: const LOGIN_USERNAME_FIELD = "username_field"; michael@0: const LOGIN_PASSWORD_FIELD = "password_field"; michael@0: michael@0: const PERMISSION_TYPE = "test-perm"; michael@0: const PERMISSION_VALUE = Ci.nsIPermissionManager.ALLOW_ACTION; michael@0: michael@0: const PREFERENCE_NAME = "test-pref"; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Utility Functions michael@0: michael@0: /** michael@0: * Creates an nsIURI object for the given string representation of a URI. michael@0: * michael@0: * @param aURIString michael@0: * The spec of the URI to create. michael@0: * @returns an nsIURI representing aURIString. michael@0: */ michael@0: function uri(aURIString) michael@0: { michael@0: return Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService). michael@0: newURI(aURIString, null, null); michael@0: } michael@0: michael@0: /** michael@0: * Asynchronously adds visits to a page. michael@0: * michael@0: * @param aPlaceInfo michael@0: * Can be an nsIURI, in such a case a single LINK visit will be added. michael@0: * Otherwise can be an object describing the visit to add, or an array michael@0: * of these objects: michael@0: * { uri: nsIURI of the page, michael@0: * transition: one of the TRANSITION_* from nsINavHistoryService, michael@0: * [optional] title: title of the page, michael@0: * [optional] visitDate: visit date in microseconds from the epoch michael@0: * [optional] referrer: nsIURI of the referrer for this visit michael@0: * } michael@0: * michael@0: * @return {Promise} michael@0: * @resolves When all visits have been added successfully. michael@0: * @rejects JavaScript exception. michael@0: */ michael@0: function promiseAddVisits(aPlaceInfo) michael@0: { michael@0: let deferred = Promise.defer(); michael@0: let places = []; michael@0: if (aPlaceInfo instanceof Ci.nsIURI) { michael@0: places.push({ uri: aPlaceInfo }); michael@0: } michael@0: else if (Array.isArray(aPlaceInfo)) { michael@0: places = places.concat(aPlaceInfo); michael@0: } else { michael@0: places.push(aPlaceInfo) michael@0: } michael@0: michael@0: // Create mozIVisitInfo for each entry. michael@0: let now = Date.now(); michael@0: for (let i = 0; i < places.length; i++) { michael@0: if (!places[i].title) { michael@0: places[i].title = "test visit for " + places[i].uri.spec; michael@0: } michael@0: places[i].visits = [{ michael@0: transitionType: places[i].transition === undefined ? Ci.nsINavHistoryService.TRANSITION_LINK michael@0: : places[i].transition, michael@0: visitDate: places[i].visitDate || (now++) * 1000, michael@0: referrerURI: places[i].referrer michael@0: }]; michael@0: } michael@0: michael@0: PlacesUtils.asyncHistory.updatePlaces( michael@0: places, michael@0: { michael@0: handleError: function handleError(aResultCode, aPlaceInfo) { michael@0: let ex = new Components.Exception("Unexpected error in adding visits.", michael@0: aResultCode); michael@0: deferred.reject(ex); michael@0: }, michael@0: handleResult: function () {}, michael@0: handleCompletion: function handleCompletion() { michael@0: deferred.resolve(); michael@0: } michael@0: } michael@0: ); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Asynchronously check a url is visited. michael@0: * michael@0: * @param aURI michael@0: * The URI. michael@0: * michael@0: * @return {Promise} michael@0: * @resolves When the check has been added successfully. michael@0: * @rejects JavaScript exception. michael@0: */ michael@0: function promiseIsURIVisited(aURI) michael@0: { michael@0: let deferred = Promise.defer(); michael@0: PlacesUtils.asyncHistory.isURIVisited(aURI, function(aURI, aIsVisited) { michael@0: deferred.resolve(aIsVisited); michael@0: }); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: /** michael@0: * Add a cookie to the cookie service. michael@0: * michael@0: * @param aDomain michael@0: */ michael@0: function add_cookie(aDomain) michael@0: { michael@0: check_cookie_exists(aDomain, false); michael@0: let cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2); michael@0: cm.add(aDomain, COOKIE_PATH, COOKIE_NAME, "", false, false, false, michael@0: COOKIE_EXPIRY); michael@0: check_cookie_exists(aDomain, true); michael@0: } michael@0: michael@0: /** michael@0: * Checks to ensure that a cookie exists or not for a domain. michael@0: * michael@0: * @param aDomain michael@0: * The domain to check for the cookie. michael@0: * @param aExists michael@0: * True if the cookie should exist, false otherwise. michael@0: */ michael@0: function check_cookie_exists(aDomain, aExists) michael@0: { michael@0: let cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2); michael@0: let cookie = { michael@0: host: aDomain, michael@0: name: COOKIE_NAME, michael@0: path: COOKIE_PATH michael@0: } michael@0: let checker = aExists ? do_check_true : do_check_false; michael@0: checker(cm.cookieExists(cookie)); michael@0: } michael@0: michael@0: /** michael@0: * Adds a download to download history. michael@0: * michael@0: * @param aURIString michael@0: * The string of the URI to add. michael@0: * @param aIsActive michael@0: * If it should be set to an active state in the database. This does not michael@0: * make it show up in the list of active downloads however! michael@0: */ michael@0: function add_download(aURIString, aIsActive) michael@0: { michael@0: function makeGUID() { michael@0: let guid = ""; michael@0: for (var i = 0; i < 12; i++) michael@0: guid += Math.floor(Math.random() * 10); michael@0: return guid; michael@0: } michael@0: michael@0: check_downloaded(aURIString, false); michael@0: let db = Cc["@mozilla.org/download-manager;1"]. michael@0: getService(Ci.nsIDownloadManager). michael@0: DBConnection; michael@0: let stmt = db.createStatement( michael@0: "INSERT INTO moz_downloads (source, state, guid) " + michael@0: "VALUES (:source, :state, :guid)" michael@0: ); michael@0: stmt.params.source = aURIString; michael@0: stmt.params.state = aIsActive ? Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING : michael@0: Ci.nsIDownloadManager.DOWNLOAD_FINISHED; michael@0: stmt.params.guid = makeGUID(); michael@0: try { michael@0: stmt.execute(); michael@0: } michael@0: finally { michael@0: stmt.finalize(); michael@0: } michael@0: check_downloaded(aURIString, true); michael@0: } michael@0: michael@0: /** michael@0: * Checks to ensure a URI string is in download history or not. michael@0: * michael@0: * @param aURIString michael@0: * The string of the URI to check. michael@0: * @param aIsDownloaded michael@0: * True if the URI should be downloaded, false otherwise. michael@0: */ michael@0: function check_downloaded(aURIString, aIsDownloaded) michael@0: { michael@0: let db = Cc["@mozilla.org/download-manager;1"]. michael@0: getService(Ci.nsIDownloadManager). michael@0: DBConnection; michael@0: let stmt = db.createStatement( michael@0: "SELECT * " + michael@0: "FROM moz_downloads " + michael@0: "WHERE source = :source" michael@0: ); michael@0: stmt.params.source = aURIString; michael@0: michael@0: let checker = aIsDownloaded ? do_check_true : do_check_false; michael@0: try { michael@0: checker(stmt.executeStep()); michael@0: } michael@0: finally { michael@0: stmt.finalize(); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Adds a disabled host to the login manager. michael@0: * michael@0: * @param aHost michael@0: * The host to add to the list of disabled hosts. michael@0: */ michael@0: function add_disabled_host(aHost) michael@0: { michael@0: check_disabled_host(aHost, false); michael@0: let lm = Cc["@mozilla.org/login-manager;1"]. michael@0: getService(Ci.nsILoginManager); michael@0: lm.setLoginSavingEnabled(aHost, false); michael@0: check_disabled_host(aHost, true); michael@0: } michael@0: michael@0: /** michael@0: * Checks to see if a host is disabled for storing logins or not. michael@0: * michael@0: * @param aHost michael@0: * The host to check if it is disabled. michael@0: * @param aIsDisabled michael@0: * True if the host should be disabled, false otherwise. michael@0: */ michael@0: function check_disabled_host(aHost, aIsDisabled) michael@0: { michael@0: let lm = Cc["@mozilla.org/login-manager;1"]. michael@0: getService(Ci.nsILoginManager); michael@0: let checker = aIsDisabled ? do_check_false : do_check_true; michael@0: checker(lm.getLoginSavingEnabled(aHost)); michael@0: } michael@0: michael@0: /** michael@0: * Adds a login for the specified host to the login manager. michael@0: * michael@0: * @param aHost michael@0: * The host to add the login for. michael@0: */ michael@0: function add_login(aHost) michael@0: { michael@0: check_login_exists(aHost, false); michael@0: let login = Cc["@mozilla.org/login-manager/loginInfo;1"]. michael@0: createInstance(Ci.nsILoginInfo); michael@0: login.init(aHost, "", null, LOGIN_USERNAME, LOGIN_PASSWORD, michael@0: LOGIN_USERNAME_FIELD, LOGIN_PASSWORD_FIELD); michael@0: let lm = Cc["@mozilla.org/login-manager;1"]. michael@0: getService(Ci.nsILoginManager); michael@0: lm.addLogin(login); michael@0: check_login_exists(aHost, true); michael@0: } michael@0: michael@0: /** michael@0: * Checks to see if a login exists for a host. michael@0: * michael@0: * @param aHost michael@0: * The host to check for the test login. michael@0: * @param aExists michael@0: * True if the login should exist, false otherwise. michael@0: */ michael@0: function check_login_exists(aHost, aExists) michael@0: { michael@0: let lm = Cc["@mozilla.org/login-manager;1"]. michael@0: getService(Ci.nsILoginManager); michael@0: let count = { value: 0 }; michael@0: lm.findLogins(count, aHost, "", null); michael@0: do_check_eq(count.value, aExists ? 1 : 0); michael@0: } michael@0: michael@0: /** michael@0: * Adds a permission for the specified URI to the permission manager. michael@0: * michael@0: * @param aURI michael@0: * The URI to add the test permission for. michael@0: */ michael@0: function add_permission(aURI) michael@0: { michael@0: check_permission_exists(aURI, false); michael@0: let pm = Cc["@mozilla.org/permissionmanager;1"]. michael@0: getService(Ci.nsIPermissionManager); michael@0: let principal = Cc["@mozilla.org/scriptsecuritymanager;1"] michael@0: .getService(Ci.nsIScriptSecurityManager) michael@0: .getNoAppCodebasePrincipal(aURI); michael@0: michael@0: pm.addFromPrincipal(principal, PERMISSION_TYPE, PERMISSION_VALUE); michael@0: check_permission_exists(aURI, true); michael@0: } michael@0: michael@0: /** michael@0: * Checks to see if a permission exists for the given URI. michael@0: * michael@0: * @param aURI michael@0: * The URI to check if a permission exists. michael@0: * @param aExists michael@0: * True if the permission should exist, false otherwise. michael@0: */ michael@0: function check_permission_exists(aURI, aExists) michael@0: { michael@0: let pm = Cc["@mozilla.org/permissionmanager;1"]. michael@0: getService(Ci.nsIPermissionManager); michael@0: let principal = Cc["@mozilla.org/scriptsecuritymanager;1"] michael@0: .getService(Ci.nsIScriptSecurityManager) michael@0: .getNoAppCodebasePrincipal(aURI); michael@0: michael@0: let perm = pm.testExactPermissionFromPrincipal(principal, PERMISSION_TYPE); michael@0: let checker = aExists ? do_check_eq : do_check_neq; michael@0: checker(perm, PERMISSION_VALUE); michael@0: } michael@0: michael@0: /** michael@0: * Adds a content preference for the specified URI. michael@0: * michael@0: * @param aURI michael@0: * The URI to add a preference for. michael@0: */ michael@0: function add_preference(aURI) michael@0: { michael@0: let deferred = Promise.defer(); michael@0: let cp = Cc["@mozilla.org/content-pref/service;1"]. michael@0: getService(Ci.nsIContentPrefService2); michael@0: cp.set(aURI.spec, PREFERENCE_NAME, "foo", null, { michael@0: handleCompletion: function() deferred.resolve() michael@0: }); michael@0: return deferred.promise; michael@0: } michael@0: michael@0: /** michael@0: * Checks to see if a content preference exists for the given URI. michael@0: * michael@0: * @param aURI michael@0: * The URI to check if a preference exists. michael@0: */ michael@0: function preference_exists(aURI) michael@0: { michael@0: let deferred = Promise.defer(); michael@0: let cp = Cc["@mozilla.org/content-pref/service;1"]. michael@0: getService(Ci.nsIContentPrefService2); michael@0: let exists = false; michael@0: cp.getByDomainAndName(aURI.spec, PREFERENCE_NAME, null, { michael@0: handleResult: function() exists = true, michael@0: handleCompletion: function() deferred.resolve(exists) michael@0: }); michael@0: return deferred.promise; michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Test Functions michael@0: michael@0: // History michael@0: function test_history_cleared_with_direct_match() michael@0: { michael@0: const TEST_URI = uri("http://mozilla.org/foo"); michael@0: do_check_false(yield promiseIsURIVisited(TEST_URI)); michael@0: yield promiseAddVisits(TEST_URI); michael@0: do_check_true(yield promiseIsURIVisited(TEST_URI)); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: do_check_false(yield promiseIsURIVisited(TEST_URI)); michael@0: } michael@0: michael@0: function test_history_cleared_with_subdomain() michael@0: { michael@0: const TEST_URI = uri("http://www.mozilla.org/foo"); michael@0: do_check_false(yield promiseIsURIVisited(TEST_URI)); michael@0: yield promiseAddVisits(TEST_URI); michael@0: do_check_true(yield promiseIsURIVisited(TEST_URI)); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: do_check_false(yield promiseIsURIVisited(TEST_URI)); michael@0: } michael@0: michael@0: function test_history_not_cleared_with_uri_contains_domain() michael@0: { michael@0: const TEST_URI = uri("http://ilovemozilla.org/foo"); michael@0: do_check_false(yield promiseIsURIVisited(TEST_URI)); michael@0: yield promiseAddVisits(TEST_URI); michael@0: do_check_true(yield promiseIsURIVisited(TEST_URI)); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: do_check_true(yield promiseIsURIVisited(TEST_URI)); michael@0: michael@0: // Clear history since we left something there from this test. michael@0: PlacesUtils.bhistory.removeAllPages(); michael@0: } michael@0: michael@0: // Cookie Service michael@0: function test_cookie_cleared_with_direct_match() michael@0: { michael@0: const TEST_DOMAIN = "mozilla.org"; michael@0: add_cookie(TEST_DOMAIN); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: check_cookie_exists(TEST_DOMAIN, false); michael@0: } michael@0: michael@0: function test_cookie_cleared_with_subdomain() michael@0: { michael@0: const TEST_DOMAIN = "www.mozilla.org"; michael@0: add_cookie(TEST_DOMAIN); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: check_cookie_exists(TEST_DOMAIN, false); michael@0: } michael@0: michael@0: function test_cookie_not_cleared_with_uri_contains_domain() michael@0: { michael@0: const TEST_DOMAIN = "ilovemozilla.org"; michael@0: add_cookie(TEST_DOMAIN); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: check_cookie_exists(TEST_DOMAIN, true); michael@0: } michael@0: michael@0: // Download Manager michael@0: function test_download_history_cleared_with_direct_match() michael@0: { michael@0: if (oldDownloadManagerDisabled()) { michael@0: return; michael@0: } michael@0: michael@0: const TEST_URI = "http://mozilla.org/foo"; michael@0: add_download(TEST_URI, false); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: check_downloaded(TEST_URI, false); michael@0: } michael@0: michael@0: function test_download_history_cleared_with_subdomain() michael@0: { michael@0: if (oldDownloadManagerDisabled()) { michael@0: return; michael@0: } michael@0: michael@0: const TEST_URI = "http://www.mozilla.org/foo"; michael@0: add_download(TEST_URI, false); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: check_downloaded(TEST_URI, false); michael@0: } michael@0: michael@0: function test_download_history_not_cleared_with_active_direct_match() michael@0: { michael@0: if (oldDownloadManagerDisabled()) { michael@0: return; michael@0: } michael@0: michael@0: // Tests that downloads marked as active in the db are not deleted from the db michael@0: const TEST_URI = "http://mozilla.org/foo"; michael@0: add_download(TEST_URI, true); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: check_downloaded(TEST_URI, true); michael@0: michael@0: // Reset state michael@0: let db = Cc["@mozilla.org/download-manager;1"]. michael@0: getService(Ci.nsIDownloadManager). michael@0: DBConnection; michael@0: db.executeSimpleSQL("DELETE FROM moz_downloads"); michael@0: check_downloaded(TEST_URI, false); michael@0: } michael@0: michael@0: // Login Manager michael@0: function test_login_manager_disabled_hosts_cleared_with_direct_match() michael@0: { michael@0: const TEST_HOST = "http://mozilla.org"; michael@0: add_disabled_host(TEST_HOST); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: check_disabled_host(TEST_HOST, false); michael@0: } michael@0: michael@0: function test_login_manager_disabled_hosts_cleared_with_subdomain() michael@0: { michael@0: const TEST_HOST = "http://www.mozilla.org"; michael@0: add_disabled_host(TEST_HOST); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: check_disabled_host(TEST_HOST, false); michael@0: } michael@0: michael@0: function test_login_manager_disabled_hosts_not_cleared_with_uri_contains_domain() michael@0: { michael@0: const TEST_HOST = "http://ilovemozilla.org"; michael@0: add_disabled_host(TEST_HOST); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: check_disabled_host(TEST_HOST, true); michael@0: michael@0: // Reset state michael@0: let lm = Cc["@mozilla.org/login-manager;1"]. michael@0: getService(Ci.nsILoginManager); michael@0: lm.setLoginSavingEnabled(TEST_HOST, true); michael@0: check_disabled_host(TEST_HOST, false); michael@0: } michael@0: michael@0: function test_login_manager_logins_cleared_with_direct_match() michael@0: { michael@0: const TEST_HOST = "http://mozilla.org"; michael@0: add_login(TEST_HOST); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: check_login_exists(TEST_HOST, false); michael@0: } michael@0: michael@0: function test_login_manager_logins_cleared_with_subdomain() michael@0: { michael@0: const TEST_HOST = "http://www.mozilla.org"; michael@0: add_login(TEST_HOST); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: check_login_exists(TEST_HOST, false); michael@0: } michael@0: michael@0: function tets_login_manager_logins_not_cleared_with_uri_contains_domain() michael@0: { michael@0: const TEST_HOST = "http://ilovemozilla.org"; michael@0: add_login(TEST_HOST); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: check_login_exists(TEST_HOST, true); michael@0: michael@0: let lm = Cc["@mozilla.org/login-manager;1"]. michael@0: getService(Ci.nsILoginManager); michael@0: lm.removeAllLogins(); michael@0: check_login_exists(TEST_HOST, false); michael@0: } michael@0: michael@0: // Permission Manager michael@0: function test_permission_manager_cleared_with_direct_match() michael@0: { michael@0: const TEST_URI = uri("http://mozilla.org"); michael@0: add_permission(TEST_URI); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: check_permission_exists(TEST_URI, false); michael@0: } michael@0: michael@0: function test_permission_manager_cleared_with_subdomain() michael@0: { michael@0: const TEST_URI = uri("http://www.mozilla.org"); michael@0: add_permission(TEST_URI); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: check_permission_exists(TEST_URI, false); michael@0: } michael@0: michael@0: function test_permission_manager_not_cleared_with_uri_contains_domain() michael@0: { michael@0: const TEST_URI = uri("http://ilovemozilla.org"); michael@0: add_permission(TEST_URI); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: check_permission_exists(TEST_URI, true); michael@0: michael@0: // Reset state michael@0: let pm = Cc["@mozilla.org/permissionmanager;1"]. michael@0: getService(Ci.nsIPermissionManager); michael@0: pm.removeAll(); michael@0: check_permission_exists(TEST_URI, false); michael@0: } michael@0: michael@0: function waitForPurgeNotification() { michael@0: let deferred = Promise.defer(); michael@0: michael@0: let observer = { michael@0: observe: function(aSubject, aTopic, aData) michael@0: { michael@0: Services.obs.removeObserver(observer, "browser:purge-domain-data"); michael@0: // test_storage_cleared needs this extra executeSoon because michael@0: // the DOMStorage clean-up is also listening to this same observer michael@0: // which is run synchronously. michael@0: Services.tm.mainThread.dispatch(function() { michael@0: deferred.resolve(); michael@0: }, Components.interfaces.nsIThread.DISPATCH_NORMAL); michael@0: } michael@0: }; michael@0: Services.obs.addObserver(observer, "browser:purge-domain-data", false); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: // Content Preferences michael@0: function test_content_preferences_cleared_with_direct_match() michael@0: { michael@0: const TEST_URI = uri("http://mozilla.org"); michael@0: do_check_false(yield preference_exists(TEST_URI)); michael@0: yield add_preference(TEST_URI); michael@0: do_check_true(yield preference_exists(TEST_URI)); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: yield waitForPurgeNotification(); michael@0: do_check_false(yield preference_exists(TEST_URI)); michael@0: } michael@0: michael@0: function test_content_preferences_cleared_with_subdomain() michael@0: { michael@0: const TEST_URI = uri("http://www.mozilla.org"); michael@0: do_check_false(yield preference_exists(TEST_URI)); michael@0: yield add_preference(TEST_URI); michael@0: do_check_true(yield preference_exists(TEST_URI)); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: yield waitForPurgeNotification(); michael@0: do_check_false(yield preference_exists(TEST_URI)); michael@0: } michael@0: michael@0: function test_content_preferences_not_cleared_with_uri_contains_domain() michael@0: { michael@0: const TEST_URI = uri("http://ilovemozilla.org"); michael@0: do_check_false(yield preference_exists(TEST_URI)); michael@0: yield add_preference(TEST_URI); michael@0: do_check_true(yield preference_exists(TEST_URI)); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: yield waitForPurgeNotification(); michael@0: do_check_true(yield preference_exists(TEST_URI)); michael@0: michael@0: // Reset state michael@0: ForgetAboutSite.removeDataFromDomain("ilovemozilla.org"); michael@0: yield waitForPurgeNotification(); michael@0: do_check_false(yield preference_exists(TEST_URI)); michael@0: } michael@0: michael@0: // Cache michael@0: function test_cache_cleared() michael@0: { michael@0: // Because this test is asynchronous, it should be the last test michael@0: do_check_true(tests[tests.length - 1] == arguments.callee); michael@0: michael@0: // NOTE: We could be more extensive with this test and actually add an entry michael@0: // to the cache, and then make sure it is gone. However, we trust that michael@0: // the API is well tested, and that when we get the observer michael@0: // notification, we have actually cleared the cache. michael@0: // This seems to happen asynchronously... michael@0: let os = Cc["@mozilla.org/observer-service;1"]. michael@0: getService(Ci.nsIObserverService); michael@0: let observer = { michael@0: observe: function(aSubject, aTopic, aData) michael@0: { michael@0: os.removeObserver(observer, "cacheservice:empty-cache"); michael@0: // Shutdown the download manager. michael@0: Services.obs.notifyObservers(null, "quit-application", null); michael@0: do_test_finished(); michael@0: } michael@0: }; michael@0: os.addObserver(observer, "cacheservice:empty-cache", false); michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: do_test_pending(); michael@0: } michael@0: michael@0: function test_storage_cleared() michael@0: { michael@0: function getStorageForURI(aURI) michael@0: { michael@0: let principal = Cc["@mozilla.org/scriptsecuritymanager;1"]. michael@0: getService(Ci.nsIScriptSecurityManager). michael@0: getNoAppCodebasePrincipal(aURI); michael@0: let dsm = Cc["@mozilla.org/dom/localStorage-manager;1"]. michael@0: getService(Ci.nsIDOMStorageManager); michael@0: return dsm.createStorage(principal, ""); michael@0: } michael@0: michael@0: let s = [ michael@0: getStorageForURI(uri("http://mozilla.org")), michael@0: getStorageForURI(uri("http://my.mozilla.org")), michael@0: getStorageForURI(uri("http://ilovemozilla.org")), michael@0: ]; michael@0: michael@0: for (let i = 0; i < s.length; ++i) { michael@0: let storage = s[i]; michael@0: storage.setItem("test", "value" + i); michael@0: do_check_eq(storage.length, 1); michael@0: do_check_eq(storage.key(0), "test"); michael@0: do_check_eq(storage.getItem("test"), "value" + i); michael@0: } michael@0: michael@0: ForgetAboutSite.removeDataFromDomain("mozilla.org"); michael@0: yield waitForPurgeNotification(); michael@0: michael@0: do_check_eq(s[0].getItem("test"), null); michael@0: do_check_eq(s[0].length, 0); michael@0: do_check_eq(s[1].getItem("test"), null); michael@0: do_check_eq(s[1].length, 0); michael@0: do_check_eq(s[2].getItem("test"), "value2"); michael@0: do_check_eq(s[2].length, 1); michael@0: } michael@0: michael@0: let tests = [ michael@0: // History michael@0: test_history_cleared_with_direct_match, michael@0: test_history_cleared_with_subdomain, michael@0: test_history_not_cleared_with_uri_contains_domain, michael@0: michael@0: // Cookie Service michael@0: test_cookie_cleared_with_direct_match, michael@0: test_cookie_cleared_with_subdomain, michael@0: test_cookie_not_cleared_with_uri_contains_domain, michael@0: michael@0: // Download Manager michael@0: // Note: active downloads tested in test_removeDataFromDomain_activeDownloads.js michael@0: test_download_history_cleared_with_direct_match, michael@0: test_download_history_cleared_with_subdomain, michael@0: test_download_history_not_cleared_with_active_direct_match, michael@0: michael@0: // Login Manager michael@0: test_login_manager_disabled_hosts_cleared_with_direct_match, michael@0: test_login_manager_disabled_hosts_cleared_with_subdomain, michael@0: test_login_manager_disabled_hosts_not_cleared_with_uri_contains_domain, michael@0: test_login_manager_logins_cleared_with_direct_match, michael@0: test_login_manager_logins_cleared_with_subdomain, michael@0: tets_login_manager_logins_not_cleared_with_uri_contains_domain, michael@0: michael@0: // Permission Manager michael@0: test_permission_manager_cleared_with_direct_match, michael@0: test_permission_manager_cleared_with_subdomain, michael@0: test_permission_manager_not_cleared_with_uri_contains_domain, michael@0: michael@0: // Content Preferences michael@0: test_content_preferences_cleared_with_direct_match, michael@0: test_content_preferences_cleared_with_subdomain, michael@0: test_content_preferences_not_cleared_with_uri_contains_domain, michael@0: michael@0: // Storage michael@0: test_storage_cleared, michael@0: michael@0: // Cache michael@0: test_cache_cleared, michael@0: ]; michael@0: michael@0: function run_test() michael@0: { michael@0: for (let i = 0; i < tests.length; i++) michael@0: add_task(tests[i]); michael@0: michael@0: run_next_test(); michael@0: }