michael@0: Cu.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: // Bug 453440 - Test the timespan-based logic of the sanitizer code michael@0: let now_mSec = Date.now(); michael@0: let now_uSec = now_mSec * 1000; michael@0: michael@0: const kMsecPerMin = 60 * 1000; michael@0: const kUsecPerMin = 60 * 1000000; michael@0: michael@0: let tempScope = {}; michael@0: Cc["@mozilla.org/moz/jssubscript-loader;1"].getService(Ci.mozIJSSubScriptLoader) michael@0: .loadSubScript("chrome://browser/content/sanitize.js", tempScope); michael@0: let Sanitizer = tempScope.Sanitizer; michael@0: michael@0: let FormHistory = (Components.utils.import("resource://gre/modules/FormHistory.jsm", {})).FormHistory; michael@0: let Downloads = (Components.utils.import("resource://gre/modules/Downloads.jsm", {})).Downloads; michael@0: michael@0: function promiseFormHistoryRemoved() { michael@0: let deferred = Promise.defer(); michael@0: Services.obs.addObserver(function onfh() { michael@0: Services.obs.removeObserver(onfh, "satchel-storage-changed", false); michael@0: deferred.resolve(); michael@0: }, "satchel-storage-changed", false); michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function promiseDownloadRemoved(list) { michael@0: let deferred = Promise.defer(); michael@0: michael@0: let view = { michael@0: onDownloadRemoved: function(download) { michael@0: list.removeView(view); michael@0: deferred.resolve(); michael@0: } michael@0: }; michael@0: michael@0: list.addView(view); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: michael@0: Task.spawn(function() { michael@0: yield setupDownloads(); michael@0: yield setupFormHistory(); michael@0: yield setupHistory(); michael@0: yield onHistoryReady(); michael@0: }).then(null, ex => ok(false, ex)).then(finish); michael@0: } michael@0: michael@0: function countEntries(name, message, check) { michael@0: let deferred = Promise.defer(); michael@0: michael@0: var obj = {}; michael@0: if (name !== null) michael@0: obj.fieldname = name; michael@0: michael@0: let count; michael@0: FormHistory.count(obj, { handleResult: function (result) count = result, michael@0: handleError: function (error) { michael@0: do_throw("Error occurred searching form history: " + error); michael@0: deferred.reject(error) michael@0: }, michael@0: handleCompletion: function (reason) { michael@0: if (!reason) { michael@0: check(count, message); michael@0: deferred.resolve(); michael@0: } michael@0: }, michael@0: }); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function onHistoryReady() { michael@0: var hoursSinceMidnight = new Date().getHours(); michael@0: var minutesSinceMidnight = hoursSinceMidnight * 60 + new Date().getMinutes(); michael@0: michael@0: // Should test cookies here, but nsICookieManager/nsICookieService michael@0: // doesn't let us fake creation times. bug 463127 michael@0: michael@0: let s = new Sanitizer(); michael@0: s.ignoreTimespan = false; michael@0: s.prefDomain = "privacy.cpd."; michael@0: var itemPrefs = gPrefService.getBranch(s.prefDomain); michael@0: itemPrefs.setBoolPref("history", true); michael@0: itemPrefs.setBoolPref("downloads", true); michael@0: itemPrefs.setBoolPref("cache", false); michael@0: itemPrefs.setBoolPref("cookies", false); michael@0: itemPrefs.setBoolPref("formdata", true); michael@0: itemPrefs.setBoolPref("offlineApps", false); michael@0: itemPrefs.setBoolPref("passwords", false); michael@0: itemPrefs.setBoolPref("sessions", false); michael@0: itemPrefs.setBoolPref("siteSettings", false); michael@0: michael@0: let publicList = yield Downloads.getList(Downloads.PUBLIC); michael@0: let downloadPromise = promiseDownloadRemoved(publicList); michael@0: michael@0: // Clear 10 minutes ago michael@0: s.range = [now_uSec - 10*60*1000000, now_uSec]; michael@0: s.sanitize(); michael@0: s.range = null; michael@0: michael@0: yield promiseFormHistoryRemoved(); michael@0: yield downloadPromise; michael@0: michael@0: ok(!(yield promiseIsURIVisited(makeURI("http://10minutes.com"))), michael@0: "Pretend visit to 10minutes.com should now be deleted"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://1hour.com"))), michael@0: "Pretend visit to 1hour.com should should still exist"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://1hour10minutes.com"))), michael@0: "Pretend visit to 1hour10minutes.com should should still exist"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://2hour.com"))), michael@0: "Pretend visit to 2hour.com should should still exist"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://2hour10minutes.com"))), michael@0: "Pretend visit to 2hour10minutes.com should should still exist"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://4hour.com"))), michael@0: "Pretend visit to 4hour.com should should still exist"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))), michael@0: "Pretend visit to 4hour10minutes.com should should still exist"); michael@0: if (minutesSinceMidnight > 10) { michael@0: ok((yield promiseIsURIVisited(makeURI("http://today.com"))), michael@0: "Pretend visit to today.com should still exist"); michael@0: } michael@0: ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))), michael@0: "Pretend visit to before-today.com should still exist"); michael@0: michael@0: let checkZero = function(num, message) { is(num, 0, message); } michael@0: let checkOne = function(num, message) { is(num, 1, message); } michael@0: michael@0: yield countEntries("10minutes", "10minutes form entry should be deleted", checkZero); michael@0: yield countEntries("1hour", "1hour form entry should still exist", checkOne); michael@0: yield countEntries("1hour10minutes", "1hour10minutes form entry should still exist", checkOne); michael@0: yield countEntries("2hour", "2hour form entry should still exist", checkOne); michael@0: yield countEntries("2hour10minutes", "2hour10minutes form entry should still exist", checkOne); michael@0: yield countEntries("4hour", "4hour form entry should still exist", checkOne); michael@0: yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne); michael@0: if (minutesSinceMidnight > 10) michael@0: yield countEntries("today", "today form entry should still exist", checkOne); michael@0: yield countEntries("b4today", "b4today form entry should still exist", checkOne); michael@0: michael@0: ok(!(yield downloadExists(publicList, "fakefile-10-minutes")), "10 minute download should now be deleted"); michael@0: ok((yield downloadExists(publicList, "fakefile-1-hour")), "<1 hour download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-1-hour-10-minutes")), "1 hour 10 minute download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-2-hour")), "<2 hour old download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "2 hour 10 minute download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present"); michael@0: michael@0: if (minutesSinceMidnight > 10) michael@0: ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present"); michael@0: michael@0: downloadPromise = promiseDownloadRemoved(publicList); michael@0: michael@0: // Clear 1 hour michael@0: Sanitizer.prefs.setIntPref("timeSpan", 1); michael@0: s.sanitize(); michael@0: michael@0: yield promiseFormHistoryRemoved(); michael@0: yield downloadPromise; michael@0: michael@0: ok(!(yield promiseIsURIVisited(makeURI("http://1hour.com"))), michael@0: "Pretend visit to 1hour.com should now be deleted"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://1hour10minutes.com"))), michael@0: "Pretend visit to 1hour10minutes.com should should still exist"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://2hour.com"))), michael@0: "Pretend visit to 2hour.com should should still exist"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://2hour10minutes.com"))), michael@0: "Pretend visit to 2hour10minutes.com should should still exist"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://4hour.com"))), michael@0: "Pretend visit to 4hour.com should should still exist"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))), michael@0: "Pretend visit to 4hour10minutes.com should should still exist"); michael@0: if (hoursSinceMidnight > 1) { michael@0: ok((yield promiseIsURIVisited(makeURI("http://today.com"))), michael@0: "Pretend visit to today.com should still exist"); michael@0: } michael@0: ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))), michael@0: "Pretend visit to before-today.com should still exist"); michael@0: michael@0: yield countEntries("1hour", "1hour form entry should be deleted", checkZero); michael@0: yield countEntries("1hour10minutes", "1hour10minutes form entry should still exist", checkOne); michael@0: yield countEntries("2hour", "2hour form entry should still exist", checkOne); michael@0: yield countEntries("2hour10minutes", "2hour10minutes form entry should still exist", checkOne); michael@0: yield countEntries("4hour", "4hour form entry should still exist", checkOne); michael@0: yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne); michael@0: if (hoursSinceMidnight > 1) michael@0: yield countEntries("today", "today form entry should still exist", checkOne); michael@0: yield countEntries("b4today", "b4today form entry should still exist", checkOne); michael@0: michael@0: ok(!(yield downloadExists(publicList, "fakefile-1-hour")), "<1 hour download should now be deleted"); michael@0: ok((yield downloadExists(publicList, "fakefile-1-hour-10-minutes")), "1 hour 10 minute download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-2-hour")), "<2 hour old download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "2 hour 10 minute download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present"); michael@0: michael@0: if (hoursSinceMidnight > 1) michael@0: ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present"); michael@0: michael@0: downloadPromise = promiseDownloadRemoved(publicList); michael@0: michael@0: // Clear 1 hour 10 minutes michael@0: s.range = [now_uSec - 70*60*1000000, now_uSec]; michael@0: s.sanitize(); michael@0: s.range = null; michael@0: michael@0: yield promiseFormHistoryRemoved(); michael@0: yield downloadPromise; michael@0: michael@0: ok(!(yield promiseIsURIVisited(makeURI("http://1hour10minutes.com"))), michael@0: "Pretend visit to 1hour10minutes.com should now be deleted"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://2hour.com"))), michael@0: "Pretend visit to 2hour.com should should still exist"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://2hour10minutes.com"))), michael@0: "Pretend visit to 2hour10minutes.com should should still exist"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://4hour.com"))), michael@0: "Pretend visit to 4hour.com should should still exist"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))), michael@0: "Pretend visit to 4hour10minutes.com should should still exist"); michael@0: if (minutesSinceMidnight > 70) { michael@0: ok((yield promiseIsURIVisited(makeURI("http://today.com"))), michael@0: "Pretend visit to today.com should still exist"); michael@0: } michael@0: ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))), michael@0: "Pretend visit to before-today.com should still exist"); michael@0: michael@0: yield countEntries("1hour10minutes", "1hour10minutes form entry should be deleted", checkZero); michael@0: yield countEntries("2hour", "2hour form entry should still exist", checkOne); michael@0: yield countEntries("2hour10minutes", "2hour10minutes form entry should still exist", checkOne); michael@0: yield countEntries("4hour", "4hour form entry should still exist", checkOne); michael@0: yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne); michael@0: if (minutesSinceMidnight > 70) michael@0: yield countEntries("today", "today form entry should still exist", checkOne); michael@0: yield countEntries("b4today", "b4today form entry should still exist", checkOne); michael@0: michael@0: ok(!(yield downloadExists(publicList, "fakefile-1-hour-10-minutes")), "1 hour 10 minute old download should now be deleted"); michael@0: ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-2-hour")), "<2 hour old download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "2 hour 10 minute download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present"); michael@0: if (minutesSinceMidnight > 70) michael@0: ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present"); michael@0: michael@0: downloadPromise = promiseDownloadRemoved(publicList); michael@0: michael@0: // Clear 2 hours michael@0: Sanitizer.prefs.setIntPref("timeSpan", 2); michael@0: s.sanitize(); michael@0: michael@0: yield promiseFormHistoryRemoved(); michael@0: yield downloadPromise; michael@0: michael@0: ok(!(yield promiseIsURIVisited(makeURI("http://2hour.com"))), michael@0: "Pretend visit to 2hour.com should now be deleted"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://2hour10minutes.com"))), michael@0: "Pretend visit to 2hour10minutes.com should should still exist"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://4hour.com"))), michael@0: "Pretend visit to 4hour.com should should still exist"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))), michael@0: "Pretend visit to 4hour10minutes.com should should still exist"); michael@0: if (hoursSinceMidnight > 2) { michael@0: ok((yield promiseIsURIVisited(makeURI("http://today.com"))), michael@0: "Pretend visit to today.com should still exist"); michael@0: } michael@0: ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))), michael@0: "Pretend visit to before-today.com should still exist"); michael@0: michael@0: yield countEntries("2hour", "2hour form entry should be deleted", checkZero); michael@0: yield countEntries("2hour10minutes", "2hour10minutes form entry should still exist", checkOne); michael@0: yield countEntries("4hour", "4hour form entry should still exist", checkOne); michael@0: yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne); michael@0: if (hoursSinceMidnight > 2) michael@0: yield countEntries("today", "today form entry should still exist", checkOne); michael@0: yield countEntries("b4today", "b4today form entry should still exist", checkOne); michael@0: michael@0: ok(!(yield downloadExists(publicList, "fakefile-2-hour")), "<2 hour old download should now be deleted"); michael@0: ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "2 hour 10 minute download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present"); michael@0: if (hoursSinceMidnight > 2) michael@0: ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present"); michael@0: michael@0: downloadPromise = promiseDownloadRemoved(publicList); michael@0: michael@0: // Clear 2 hours 10 minutes michael@0: s.range = [now_uSec - 130*60*1000000, now_uSec]; michael@0: s.sanitize(); michael@0: s.range = null; michael@0: michael@0: yield promiseFormHistoryRemoved(); michael@0: yield downloadPromise; michael@0: michael@0: ok(!(yield promiseIsURIVisited(makeURI("http://2hour10minutes.com"))), michael@0: "Pretend visit to 2hour10minutes.com should now be deleted"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://4hour.com"))), michael@0: "Pretend visit to 4hour.com should should still exist"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))), michael@0: "Pretend visit to 4hour10minutes.com should should still exist"); michael@0: if (minutesSinceMidnight > 130) { michael@0: ok((yield promiseIsURIVisited(makeURI("http://today.com"))), michael@0: "Pretend visit to today.com should still exist"); michael@0: } michael@0: ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))), michael@0: "Pretend visit to before-today.com should still exist"); michael@0: michael@0: yield countEntries("2hour10minutes", "2hour10minutes form entry should be deleted", checkZero); michael@0: yield countEntries("4hour", "4hour form entry should still exist", checkOne); michael@0: yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne); michael@0: if (minutesSinceMidnight > 130) michael@0: yield countEntries("today", "today form entry should still exist", checkOne); michael@0: yield countEntries("b4today", "b4today form entry should still exist", checkOne); michael@0: michael@0: ok(!(yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "2 hour 10 minute old download should now be deleted"); michael@0: ok((yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present"); michael@0: if (minutesSinceMidnight > 130) michael@0: ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present"); michael@0: michael@0: downloadPromise = promiseDownloadRemoved(publicList); michael@0: michael@0: // Clear 4 hours michael@0: Sanitizer.prefs.setIntPref("timeSpan", 3); michael@0: s.sanitize(); michael@0: michael@0: yield promiseFormHistoryRemoved(); michael@0: yield downloadPromise; michael@0: michael@0: ok(!(yield promiseIsURIVisited(makeURI("http://4hour.com"))), michael@0: "Pretend visit to 4hour.com should now be deleted"); michael@0: ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))), michael@0: "Pretend visit to 4hour10minutes.com should should still exist"); michael@0: if (hoursSinceMidnight > 4) { michael@0: ok((yield promiseIsURIVisited(makeURI("http://today.com"))), michael@0: "Pretend visit to today.com should still exist"); michael@0: } michael@0: ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))), michael@0: "Pretend visit to before-today.com should still exist"); michael@0: michael@0: yield countEntries("4hour", "4hour form entry should be deleted", checkZero); michael@0: yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne); michael@0: if (hoursSinceMidnight > 4) michael@0: yield countEntries("today", "today form entry should still exist", checkOne); michael@0: yield countEntries("b4today", "b4today form entry should still exist", checkOne); michael@0: michael@0: ok(!(yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should now be deleted"); michael@0: ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present"); michael@0: ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present"); michael@0: if (hoursSinceMidnight > 4) michael@0: ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present"); michael@0: michael@0: downloadPromise = promiseDownloadRemoved(publicList); michael@0: michael@0: // Clear 4 hours 10 minutes michael@0: s.range = [now_uSec - 250*60*1000000, now_uSec]; michael@0: s.sanitize(); michael@0: s.range = null; michael@0: michael@0: yield promiseFormHistoryRemoved(); michael@0: yield downloadPromise; michael@0: michael@0: ok(!(yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))), michael@0: "Pretend visit to 4hour10minutes.com should now be deleted"); michael@0: if (minutesSinceMidnight > 250) { michael@0: ok((yield promiseIsURIVisited(makeURI("http://today.com"))), michael@0: "Pretend visit to today.com should still exist"); michael@0: } michael@0: ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))), michael@0: "Pretend visit to before-today.com should still exist"); michael@0: michael@0: yield countEntries("4hour10minutes", "4hour10minutes form entry should be deleted", checkZero); michael@0: if (minutesSinceMidnight > 250) michael@0: yield countEntries("today", "today form entry should still exist", checkOne); michael@0: yield countEntries("b4today", "b4today form entry should still exist", checkOne); michael@0: michael@0: ok(!(yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should now be deleted"); michael@0: ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present"); michael@0: if (minutesSinceMidnight > 250) michael@0: ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present"); michael@0: michael@0: // The 'Today' download might have been already deleted, in which case we michael@0: // should not wait for a download removal notification. michael@0: if (minutesSinceMidnight > 250) { michael@0: downloadPromise = promiseDownloadRemoved(publicList); michael@0: } else { michael@0: downloadPromise = Promise.resolve(); michael@0: } michael@0: michael@0: // Clear Today michael@0: Sanitizer.prefs.setIntPref("timeSpan", 4); michael@0: s.sanitize(); michael@0: michael@0: yield promiseFormHistoryRemoved(); michael@0: yield downloadPromise; michael@0: michael@0: // Be careful. If we add our objectss just before midnight, and sanitize michael@0: // runs immediately after, they won't be expired. This is expected, but michael@0: // we should not test in that case. We cannot just test for opposite michael@0: // condition because we could cross midnight just one moment after we michael@0: // cache our time, then we would have an even worse random failure. michael@0: var today = isToday(new Date(now_mSec)); michael@0: if (today) { michael@0: ok(!(yield promiseIsURIVisited(makeURI("http://today.com"))), michael@0: "Pretend visit to today.com should now be deleted"); michael@0: michael@0: yield countEntries("today", "today form entry should be deleted", checkZero); michael@0: ok(!(yield downloadExists(publicList, "fakefile-today")), "'Today' download should now be deleted"); michael@0: } michael@0: michael@0: ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))), michael@0: "Pretend visit to before-today.com should still exist"); michael@0: yield countEntries("b4today", "b4today form entry should still exist", checkOne); michael@0: ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present"); michael@0: michael@0: downloadPromise = promiseDownloadRemoved(publicList); michael@0: michael@0: // Choose everything michael@0: Sanitizer.prefs.setIntPref("timeSpan", 0); michael@0: s.sanitize(); michael@0: michael@0: yield promiseFormHistoryRemoved(); michael@0: yield downloadPromise; michael@0: michael@0: ok(!(yield promiseIsURIVisited(makeURI("http://before-today.com"))), michael@0: "Pretend visit to before-today.com should now be deleted"); michael@0: michael@0: yield countEntries("b4today", "b4today form entry should be deleted", checkZero); michael@0: michael@0: ok(!(yield downloadExists(publicList, "fakefile-old")), "Year old download should now be deleted"); michael@0: } michael@0: michael@0: function setupHistory() { michael@0: let deferred = Promise.defer(); michael@0: michael@0: let places = []; michael@0: michael@0: function addPlace(aURI, aTitle, aVisitDate) { michael@0: places.push({ michael@0: uri: aURI, michael@0: title: aTitle, michael@0: visits: [{ michael@0: visitDate: aVisitDate, michael@0: transitionType: Ci.nsINavHistoryService.TRANSITION_LINK michael@0: }] michael@0: }); michael@0: } michael@0: michael@0: addPlace(makeURI("http://10minutes.com/"), "10 minutes ago", now_uSec - 10 * kUsecPerMin); michael@0: addPlace(makeURI("http://1hour.com/"), "Less than 1 hour ago", now_uSec - 45 * kUsecPerMin); michael@0: addPlace(makeURI("http://1hour10minutes.com/"), "1 hour 10 minutes ago", now_uSec - 70 * kUsecPerMin); michael@0: addPlace(makeURI("http://2hour.com/"), "Less than 2 hours ago", now_uSec - 90 * kUsecPerMin); michael@0: addPlace(makeURI("http://2hour10minutes.com/"), "2 hours 10 minutes ago", now_uSec - 130 * kUsecPerMin); michael@0: addPlace(makeURI("http://4hour.com/"), "Less than 4 hours ago", now_uSec - 180 * kUsecPerMin); michael@0: addPlace(makeURI("http://4hour10minutes.com/"), "4 hours 10 minutesago", now_uSec - 250 * kUsecPerMin); michael@0: michael@0: let today = new Date(); michael@0: today.setHours(0); michael@0: today.setMinutes(0); michael@0: today.setSeconds(1); michael@0: addPlace(makeURI("http://today.com/"), "Today", today.getTime() * 1000); michael@0: michael@0: let lastYear = new Date(); michael@0: lastYear.setFullYear(lastYear.getFullYear() - 1); michael@0: addPlace(makeURI("http://before-today.com/"), "Before Today", lastYear.getTime() * 1000); michael@0: michael@0: PlacesUtils.asyncHistory.updatePlaces(places, { michael@0: handleError: function () ok(false, "Unexpected error in adding visit."), michael@0: handleResult: function () { }, michael@0: handleCompletion: function () deferred.resolve() michael@0: }); michael@0: michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function setupFormHistory() { michael@0: michael@0: function searchEntries(terms, params) { michael@0: let deferred = Promise.defer(); michael@0: michael@0: let results = []; michael@0: FormHistory.search(terms, params, { handleResult: function (result) results.push(result), michael@0: handleError: function (error) { michael@0: do_throw("Error occurred searching form history: " + error); michael@0: deferred.reject(error); michael@0: }, michael@0: handleCompletion: function (reason) { deferred.resolve(results); } michael@0: }); michael@0: return deferred.promise; michael@0: } michael@0: michael@0: function update(changes) michael@0: { michael@0: let deferred = Promise.defer(); michael@0: FormHistory.update(changes, { handleError: function (error) { michael@0: do_throw("Error occurred searching form history: " + error); michael@0: deferred.reject(error); michael@0: }, michael@0: handleCompletion: function (reason) { deferred.resolve(); } michael@0: }); michael@0: return deferred.promise; michael@0: } michael@0: michael@0: // Make sure we've got a clean DB to start with, then add the entries we'll be testing. michael@0: yield update( michael@0: [{ michael@0: op: "remove" michael@0: }, michael@0: { michael@0: op : "add", michael@0: fieldname : "10minutes", michael@0: value : "10m" michael@0: }, { michael@0: op : "add", michael@0: fieldname : "1hour", michael@0: value : "1h" michael@0: }, { michael@0: op : "add", michael@0: fieldname : "1hour10minutes", michael@0: value : "1h10m" michael@0: }, { michael@0: op : "add", michael@0: fieldname : "2hour", michael@0: value : "2h" michael@0: }, { michael@0: op : "add", michael@0: fieldname : "2hour10minutes", michael@0: value : "2h10m" michael@0: }, { michael@0: op : "add", michael@0: fieldname : "4hour", michael@0: value : "4h" michael@0: }, { michael@0: op : "add", michael@0: fieldname : "4hour10minutes", michael@0: value : "4h10m" michael@0: }, { michael@0: op : "add", michael@0: fieldname : "today", michael@0: value : "1d" michael@0: }, { michael@0: op : "add", michael@0: fieldname : "b4today", michael@0: value : "1y" michael@0: }]); michael@0: michael@0: // Artifically age the entries to the proper vintage. michael@0: let timestamp = now_uSec - 10 * kUsecPerMin; michael@0: let results = yield searchEntries(["guid"], { fieldname: "10minutes" }); michael@0: yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid }); michael@0: michael@0: timestamp = now_uSec - 45 * kUsecPerMin; michael@0: results = yield searchEntries(["guid"], { fieldname: "1hour" }); michael@0: yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid }); michael@0: michael@0: timestamp = now_uSec - 70 * kUsecPerMin; michael@0: results = yield searchEntries(["guid"], { fieldname: "1hour10minutes" }); michael@0: yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid }); michael@0: michael@0: timestamp = now_uSec - 90 * kUsecPerMin; michael@0: results = yield searchEntries(["guid"], { fieldname: "2hour" }); michael@0: yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid }); michael@0: michael@0: timestamp = now_uSec - 130 * kUsecPerMin; michael@0: results = yield searchEntries(["guid"], { fieldname: "2hour10minutes" }); michael@0: yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid }); michael@0: michael@0: timestamp = now_uSec - 180 * kUsecPerMin; michael@0: results = yield searchEntries(["guid"], { fieldname: "4hour" }); michael@0: yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid }); michael@0: michael@0: timestamp = now_uSec - 250 * kUsecPerMin; michael@0: results = yield searchEntries(["guid"], { fieldname: "4hour10minutes" }); michael@0: yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid }); michael@0: michael@0: let today = new Date(); michael@0: today.setHours(0); michael@0: today.setMinutes(0); michael@0: today.setSeconds(1); michael@0: timestamp = today.getTime() * 1000; michael@0: results = yield searchEntries(["guid"], { fieldname: "today" }); michael@0: yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid }); michael@0: michael@0: let lastYear = new Date(); michael@0: lastYear.setFullYear(lastYear.getFullYear() - 1); michael@0: timestamp = lastYear.getTime() * 1000; michael@0: results = yield searchEntries(["guid"], { fieldname: "b4today" }); michael@0: yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid }); michael@0: michael@0: var checks = 0; michael@0: let checkOne = function(num, message) { is(num, 1, message); checks++; } michael@0: michael@0: // Sanity check. michael@0: yield countEntries("10minutes", "Checking for 10minutes form history entry creation", checkOne); michael@0: yield countEntries("1hour", "Checking for 1hour form history entry creation", checkOne); michael@0: yield countEntries("1hour10minutes", "Checking for 1hour10minutes form history entry creation", checkOne); michael@0: yield countEntries("2hour", "Checking for 2hour form history entry creation", checkOne); michael@0: yield countEntries("2hour10minutes", "Checking for 2hour10minutes form history entry creation", checkOne); michael@0: yield countEntries("4hour", "Checking for 4hour form history entry creation", checkOne); michael@0: yield countEntries("4hour10minutes", "Checking for 4hour10minutes form history entry creation", checkOne); michael@0: yield countEntries("today", "Checking for today form history entry creation", checkOne); michael@0: yield countEntries("b4today", "Checking for b4today form history entry creation", checkOne); michael@0: is(checks, 9, "9 checks made"); michael@0: } michael@0: michael@0: function setupDownloads() { michael@0: michael@0: let publicList = yield Downloads.getList(Downloads.PUBLIC); michael@0: michael@0: let download = yield Downloads.createDownload({ michael@0: source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169", michael@0: target: "fakefile-10-minutes" michael@0: }); michael@0: download.startTime = new Date(now_mSec - 10 * kMsecPerMin), // 10 minutes ago michael@0: download.canceled = true; michael@0: yield publicList.add(download); michael@0: michael@0: download = yield Downloads.createDownload({ michael@0: source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440", michael@0: target: "fakefile-1-hour" michael@0: }); michael@0: download.startTime = new Date(now_mSec - 45 * kMsecPerMin), // 45 minutes ago michael@0: download.canceled = true; michael@0: yield publicList.add(download); michael@0: michael@0: download = yield Downloads.createDownload({ michael@0: source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169", michael@0: target: "fakefile-1-hour-10-minutes" michael@0: }); michael@0: download.startTime = new Date(now_mSec - 70 * kMsecPerMin), // 70 minutes ago michael@0: download.canceled = true; michael@0: yield publicList.add(download); michael@0: michael@0: download = yield Downloads.createDownload({ michael@0: source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440", michael@0: target: "fakefile-2-hour" michael@0: }); michael@0: download.startTime = new Date(now_mSec - 90 * kMsecPerMin), // 90 minutes ago michael@0: download.canceled = true; michael@0: yield publicList.add(download); michael@0: michael@0: download = yield Downloads.createDownload({ michael@0: source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169", michael@0: target: "fakefile-2-hour-10-minutes" michael@0: }); michael@0: download.startTime = new Date(now_mSec - 130 * kMsecPerMin), // 130 minutes ago michael@0: download.canceled = true; michael@0: yield publicList.add(download); michael@0: michael@0: download = yield Downloads.createDownload({ michael@0: source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440", michael@0: target: "fakefile-4-hour" michael@0: }); michael@0: download.startTime = new Date(now_mSec - 180 * kMsecPerMin), // 180 minutes ago michael@0: download.canceled = true; michael@0: yield publicList.add(download); michael@0: michael@0: download = yield Downloads.createDownload({ michael@0: source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169", michael@0: target: "fakefile-4-hour-10-minutes" michael@0: }); michael@0: download.startTime = new Date(now_mSec - 250 * kMsecPerMin), // 250 minutes ago michael@0: download.canceled = true; michael@0: yield publicList.add(download); michael@0: michael@0: // Add "today" download michael@0: let today = new Date(); michael@0: today.setHours(0); michael@0: today.setMinutes(0); michael@0: today.setSeconds(1); michael@0: michael@0: download = yield Downloads.createDownload({ michael@0: source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440", michael@0: target: "fakefile-today" michael@0: }); michael@0: download.startTime = today, // 12:00:01 AM this morning michael@0: download.canceled = true; michael@0: yield publicList.add(download); michael@0: michael@0: // Add "before today" download michael@0: let lastYear = new Date(); michael@0: lastYear.setFullYear(lastYear.getFullYear() - 1); michael@0: michael@0: download = yield Downloads.createDownload({ michael@0: source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440", michael@0: target: "fakefile-old" michael@0: }); michael@0: download.startTime = lastYear, michael@0: download.canceled = true; michael@0: yield publicList.add(download); michael@0: michael@0: // Confirm everything worked michael@0: let downloads = yield publicList.getAll(); michael@0: is(downloads.length, 9, "9 Pretend downloads added"); michael@0: michael@0: ok((yield downloadExists(publicList, "fakefile-old")), "Pretend download for everything case should exist"); michael@0: ok((yield downloadExists(publicList, "fakefile-10-minutes")), "Pretend download for 10-minutes case should exist"); michael@0: ok((yield downloadExists(publicList, "fakefile-1-hour")), "Pretend download for 1-hour case should exist"); michael@0: ok((yield downloadExists(publicList, "fakefile-1-hour-10-minutes")), "Pretend download for 1-hour-10-minutes case should exist"); michael@0: ok((yield downloadExists(publicList, "fakefile-2-hour")), "Pretend download for 2-hour case should exist"); michael@0: ok((yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "Pretend download for 2-hour-10-minutes case should exist"); michael@0: ok((yield downloadExists(publicList, "fakefile-4-hour")), "Pretend download for 4-hour case should exist"); michael@0: ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "Pretend download for 4-hour-10-minutes case should exist"); michael@0: ok((yield downloadExists(publicList, "fakefile-today")), "Pretend download for Today case should exist"); michael@0: } michael@0: michael@0: /** michael@0: * Checks to see if the downloads with the specified id exists. michael@0: * michael@0: * @param aID michael@0: * The ids of the downloads to check. michael@0: */ michael@0: function downloadExists(list, path) michael@0: { michael@0: return Task.spawn(function() { michael@0: let listArray = yield list.getAll(); michael@0: throw new Task.Result(listArray.some(i => i.target.path == path)); michael@0: }); michael@0: } michael@0: michael@0: function isToday(aDate) { michael@0: return aDate.getDate() == new Date().getDate(); michael@0: }