1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/places/tests/expiration/head_expiration.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,119 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ : 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +const Ci = Components.interfaces; 1.11 +const Cc = Components.classes; 1.12 +const Cr = Components.results; 1.13 +const Cu = Components.utils; 1.14 + 1.15 +Cu.import("resource://gre/modules/Services.jsm"); 1.16 + 1.17 +// Import common head. 1.18 +let (commonFile = do_get_file("../head_common.js", false)) { 1.19 + let uri = Services.io.newFileURI(commonFile); 1.20 + Services.scriptloader.loadSubScript(uri.spec, this); 1.21 +} 1.22 + 1.23 +// Put any other stuff relative to this test folder below. 1.24 + 1.25 + 1.26 +// Simulates an expiration at shutdown. 1.27 +function shutdownExpiration() 1.28 +{ 1.29 + let expire = Cc["@mozilla.org/places/expiration;1"].getService(Ci.nsIObserver); 1.30 + expire.observe(null, "places-will-close-connection", null); 1.31 +} 1.32 + 1.33 + 1.34 +/** 1.35 + * Causes expiration component to start, otherwise it would wait for the first 1.36 + * history notification. 1.37 + */ 1.38 +function force_expiration_start() { 1.39 + Cc["@mozilla.org/places/expiration;1"].getService(Ci.nsISupports); 1.40 +} 1.41 + 1.42 + 1.43 +/** 1.44 + * Forces an expiration run. 1.45 + * 1.46 + * @param [optional] aLimit 1.47 + * Limit for the expiration. Pass -1 for unlimited. 1.48 + * Any other non-positive value will just expire orphans. 1.49 + * 1.50 + * @return {Promise} 1.51 + * @resolves When expiration finishes. 1.52 + * @rejects Never. 1.53 + */ 1.54 +function promiseForceExpirationStep(aLimit) { 1.55 + let promise = promiseTopicObserved(PlacesUtils.TOPIC_EXPIRATION_FINISHED); 1.56 + let expire = Cc["@mozilla.org/places/expiration;1"].getService(Ci.nsIObserver); 1.57 + expire.observe(null, "places-debug-start-expiration", aLimit); 1.58 + return promise; 1.59 +} 1.60 + 1.61 + 1.62 +/** 1.63 + * Expiration preferences helpers. 1.64 + */ 1.65 + 1.66 +function setInterval(aNewInterval) { 1.67 + Services.prefs.setIntPref("places.history.expiration.interval_seconds", aNewInterval); 1.68 +} 1.69 +function getInterval() { 1.70 + return Services.prefs.getIntPref("places.history.expiration.interval_seconds"); 1.71 +} 1.72 +function clearInterval() { 1.73 + try { 1.74 + Services.prefs.clearUserPref("places.history.expiration.interval_seconds"); 1.75 + } 1.76 + catch(ex) {} 1.77 +} 1.78 + 1.79 + 1.80 +function setMaxPages(aNewMaxPages) { 1.81 + Services.prefs.setIntPref("places.history.expiration.max_pages", aNewMaxPages); 1.82 +} 1.83 +function getMaxPages() { 1.84 + return Services.prefs.getIntPref("places.history.expiration.max_pages"); 1.85 +} 1.86 +function clearMaxPages() { 1.87 + try { 1.88 + Services.prefs.clearUserPref("places.history.expiration.max_pages"); 1.89 + } 1.90 + catch(ex) {} 1.91 +} 1.92 + 1.93 + 1.94 +function setHistoryEnabled(aHistoryEnabled) { 1.95 + Services.prefs.setBoolPref("places.history.enabled", aHistoryEnabled); 1.96 +} 1.97 +function getHistoryEnabled() { 1.98 + return Services.prefs.getBoolPref("places.history.enabled"); 1.99 +} 1.100 +function clearHistoryEnabled() { 1.101 + try { 1.102 + Services.prefs.clearUserPref("places.history.enabled"); 1.103 + } 1.104 + catch(ex) {} 1.105 +} 1.106 + 1.107 +/** 1.108 + * Returns a PRTime in the past usable to add expirable visits. 1.109 + * 1.110 + * @note Expiration ignores any visit added in the last 7 days, but it's 1.111 + * better be safe against DST issues, by going back one day more. 1.112 + */ 1.113 +function getExpirablePRTime() { 1.114 + let dateObj = new Date(); 1.115 + // Normalize to midnight 1.116 + dateObj.setHours(0); 1.117 + dateObj.setMinutes(0); 1.118 + dateObj.setSeconds(0); 1.119 + dateObj.setMilliseconds(0); 1.120 + dateObj = new Date(dateObj.getTime() - 8 * 86400000); 1.121 + return dateObj.getTime() * 1000; 1.122 +}