browser/base/content/test/general/browser_sanitize-timespans.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/base/content/test/general/browser_sanitize-timespans.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,730 @@
     1.4 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
     1.5 +Cu.import("resource://gre/modules/Services.jsm");
     1.6 +
     1.7 +// Bug 453440 - Test the timespan-based logic of the sanitizer code
     1.8 +let now_mSec = Date.now();
     1.9 +let now_uSec = now_mSec * 1000;
    1.10 +
    1.11 +const kMsecPerMin = 60 * 1000;
    1.12 +const kUsecPerMin = 60 * 1000000;
    1.13 +
    1.14 +let tempScope = {};
    1.15 +Cc["@mozilla.org/moz/jssubscript-loader;1"].getService(Ci.mozIJSSubScriptLoader)
    1.16 +                                           .loadSubScript("chrome://browser/content/sanitize.js", tempScope);
    1.17 +let Sanitizer = tempScope.Sanitizer;
    1.18 +
    1.19 +let FormHistory = (Components.utils.import("resource://gre/modules/FormHistory.jsm", {})).FormHistory;
    1.20 +let Downloads = (Components.utils.import("resource://gre/modules/Downloads.jsm", {})).Downloads;
    1.21 +
    1.22 +function promiseFormHistoryRemoved() {
    1.23 +  let deferred = Promise.defer();
    1.24 +  Services.obs.addObserver(function onfh() {
    1.25 +    Services.obs.removeObserver(onfh, "satchel-storage-changed", false);
    1.26 +    deferred.resolve();
    1.27 +  }, "satchel-storage-changed", false);
    1.28 +  return deferred.promise;
    1.29 +}
    1.30 +
    1.31 +function promiseDownloadRemoved(list) {
    1.32 +  let deferred = Promise.defer();
    1.33 +
    1.34 +  let view = {
    1.35 +    onDownloadRemoved: function(download) {
    1.36 +      list.removeView(view);
    1.37 +      deferred.resolve();
    1.38 +    }
    1.39 +  };
    1.40 +
    1.41 +  list.addView(view);
    1.42 +
    1.43 +  return deferred.promise;
    1.44 +}
    1.45 +
    1.46 +function test() {
    1.47 +  waitForExplicitFinish();
    1.48 +
    1.49 +  Task.spawn(function() {
    1.50 +    yield setupDownloads();
    1.51 +    yield setupFormHistory();
    1.52 +    yield setupHistory();
    1.53 +    yield onHistoryReady();
    1.54 +  }).then(null, ex => ok(false, ex)).then(finish);
    1.55 +}
    1.56 +
    1.57 +function countEntries(name, message, check) {
    1.58 +  let deferred = Promise.defer();
    1.59 +
    1.60 +  var obj = {};
    1.61 +  if (name !== null)
    1.62 +    obj.fieldname = name;
    1.63 +
    1.64 +  let count;
    1.65 +  FormHistory.count(obj, { handleResult: function (result) count = result,
    1.66 +                           handleError: function (error) {
    1.67 +                             do_throw("Error occurred searching form history: " + error);
    1.68 +                             deferred.reject(error)
    1.69 +                           },
    1.70 +                           handleCompletion: function (reason) {
    1.71 +                             if (!reason) {
    1.72 +                               check(count, message);
    1.73 +                               deferred.resolve();
    1.74 +                             }
    1.75 +                           },
    1.76 +                         });
    1.77 +
    1.78 +  return deferred.promise;
    1.79 +}
    1.80 +
    1.81 +function onHistoryReady() {
    1.82 +  var hoursSinceMidnight = new Date().getHours();
    1.83 +  var minutesSinceMidnight = hoursSinceMidnight * 60 + new Date().getMinutes();
    1.84 +
    1.85 +  // Should test cookies here, but nsICookieManager/nsICookieService
    1.86 +  // doesn't let us fake creation times.  bug 463127
    1.87 +  
    1.88 +  let s = new Sanitizer();
    1.89 +  s.ignoreTimespan = false;
    1.90 +  s.prefDomain = "privacy.cpd.";
    1.91 +  var itemPrefs = gPrefService.getBranch(s.prefDomain);
    1.92 +  itemPrefs.setBoolPref("history", true);
    1.93 +  itemPrefs.setBoolPref("downloads", true);
    1.94 +  itemPrefs.setBoolPref("cache", false);
    1.95 +  itemPrefs.setBoolPref("cookies", false);
    1.96 +  itemPrefs.setBoolPref("formdata", true);
    1.97 +  itemPrefs.setBoolPref("offlineApps", false);
    1.98 +  itemPrefs.setBoolPref("passwords", false);
    1.99 +  itemPrefs.setBoolPref("sessions", false);
   1.100 +  itemPrefs.setBoolPref("siteSettings", false);
   1.101 +
   1.102 +  let publicList = yield Downloads.getList(Downloads.PUBLIC);
   1.103 +  let downloadPromise = promiseDownloadRemoved(publicList);
   1.104 +
   1.105 +  // Clear 10 minutes ago
   1.106 +  s.range = [now_uSec - 10*60*1000000, now_uSec];
   1.107 +  s.sanitize();
   1.108 +  s.range = null;
   1.109 +
   1.110 +  yield promiseFormHistoryRemoved();
   1.111 +  yield downloadPromise;
   1.112 +
   1.113 +  ok(!(yield promiseIsURIVisited(makeURI("http://10minutes.com"))),
   1.114 +     "Pretend visit to 10minutes.com should now be deleted");
   1.115 +  ok((yield promiseIsURIVisited(makeURI("http://1hour.com"))),
   1.116 +     "Pretend visit to 1hour.com should should still exist");
   1.117 +  ok((yield promiseIsURIVisited(makeURI("http://1hour10minutes.com"))),
   1.118 +     "Pretend visit to 1hour10minutes.com should should still exist");
   1.119 +  ok((yield promiseIsURIVisited(makeURI("http://2hour.com"))),
   1.120 +     "Pretend visit to 2hour.com should should still exist");
   1.121 +  ok((yield promiseIsURIVisited(makeURI("http://2hour10minutes.com"))),
   1.122 +     "Pretend visit to 2hour10minutes.com should should still exist");
   1.123 +  ok((yield promiseIsURIVisited(makeURI("http://4hour.com"))),
   1.124 +     "Pretend visit to 4hour.com should should still exist");
   1.125 +  ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))),
   1.126 +     "Pretend visit to 4hour10minutes.com should should still exist");
   1.127 +  if (minutesSinceMidnight > 10) {
   1.128 +    ok((yield promiseIsURIVisited(makeURI("http://today.com"))),
   1.129 +       "Pretend visit to today.com should still exist");
   1.130 +  }
   1.131 +  ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))),
   1.132 +    "Pretend visit to before-today.com should still exist");
   1.133 +
   1.134 +  let checkZero = function(num, message) { is(num, 0, message); }
   1.135 +  let checkOne = function(num, message) { is(num, 1, message); }
   1.136 +
   1.137 +  yield countEntries("10minutes", "10minutes form entry should be deleted", checkZero);
   1.138 +  yield countEntries("1hour", "1hour form entry should still exist", checkOne);
   1.139 +  yield countEntries("1hour10minutes", "1hour10minutes form entry should still exist", checkOne);
   1.140 +  yield countEntries("2hour", "2hour form entry should still exist", checkOne);
   1.141 +  yield countEntries("2hour10minutes", "2hour10minutes form entry should still exist", checkOne);
   1.142 +  yield countEntries("4hour", "4hour form entry should still exist", checkOne);
   1.143 +  yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne);
   1.144 +  if (minutesSinceMidnight > 10)
   1.145 +    yield countEntries("today", "today form entry should still exist", checkOne);
   1.146 +  yield countEntries("b4today", "b4today form entry should still exist", checkOne);
   1.147 +
   1.148 +  ok(!(yield downloadExists(publicList, "fakefile-10-minutes")), "10 minute download should now be deleted");
   1.149 +  ok((yield downloadExists(publicList, "fakefile-1-hour")), "<1 hour download should still be present");
   1.150 +  ok((yield downloadExists(publicList, "fakefile-1-hour-10-minutes")), "1 hour 10 minute download should still be present");
   1.151 +  ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present");
   1.152 +  ok((yield downloadExists(publicList, "fakefile-2-hour")), "<2 hour old download should still be present");
   1.153 +  ok((yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "2 hour 10 minute download should still be present");
   1.154 +  ok((yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should still be present");
   1.155 +  ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present");
   1.156 +
   1.157 +  if (minutesSinceMidnight > 10)
   1.158 +    ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present");
   1.159 +
   1.160 +  downloadPromise = promiseDownloadRemoved(publicList);
   1.161 +
   1.162 +  // Clear 1 hour
   1.163 +  Sanitizer.prefs.setIntPref("timeSpan", 1);
   1.164 +  s.sanitize();
   1.165 +
   1.166 +  yield promiseFormHistoryRemoved();
   1.167 +  yield downloadPromise;
   1.168 +
   1.169 +  ok(!(yield promiseIsURIVisited(makeURI("http://1hour.com"))),
   1.170 +     "Pretend visit to 1hour.com should now be deleted");
   1.171 +  ok((yield promiseIsURIVisited(makeURI("http://1hour10minutes.com"))),
   1.172 +     "Pretend visit to 1hour10minutes.com should should still exist");
   1.173 +  ok((yield promiseIsURIVisited(makeURI("http://2hour.com"))),
   1.174 +     "Pretend visit to 2hour.com should should still exist");
   1.175 +  ok((yield promiseIsURIVisited(makeURI("http://2hour10minutes.com"))),
   1.176 +     "Pretend visit to 2hour10minutes.com should should still exist");
   1.177 +  ok((yield promiseIsURIVisited(makeURI("http://4hour.com"))),
   1.178 +     "Pretend visit to 4hour.com should should still exist");
   1.179 +  ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))),
   1.180 +     "Pretend visit to 4hour10minutes.com should should still exist");
   1.181 +  if (hoursSinceMidnight > 1) {
   1.182 +    ok((yield promiseIsURIVisited(makeURI("http://today.com"))),
   1.183 +       "Pretend visit to today.com should still exist");
   1.184 +  }
   1.185 +  ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))),
   1.186 +    "Pretend visit to before-today.com should still exist");
   1.187 +
   1.188 +  yield countEntries("1hour", "1hour form entry should be deleted", checkZero);
   1.189 +  yield countEntries("1hour10minutes", "1hour10minutes form entry should still exist", checkOne);
   1.190 +  yield countEntries("2hour", "2hour form entry should still exist", checkOne);
   1.191 +  yield countEntries("2hour10minutes", "2hour10minutes form entry should still exist", checkOne);
   1.192 +  yield countEntries("4hour", "4hour form entry should still exist", checkOne);
   1.193 +  yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne);
   1.194 +  if (hoursSinceMidnight > 1)
   1.195 +    yield countEntries("today", "today form entry should still exist", checkOne);
   1.196 +  yield countEntries("b4today", "b4today form entry should still exist", checkOne);
   1.197 +
   1.198 +  ok(!(yield downloadExists(publicList, "fakefile-1-hour")), "<1 hour download should now be deleted");
   1.199 +  ok((yield downloadExists(publicList, "fakefile-1-hour-10-minutes")), "1 hour 10 minute download should still be present");
   1.200 +  ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present");
   1.201 +  ok((yield downloadExists(publicList, "fakefile-2-hour")), "<2 hour old download should still be present");
   1.202 +  ok((yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "2 hour 10 minute download should still be present");
   1.203 +  ok((yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should still be present");
   1.204 +  ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present");
   1.205 +
   1.206 +  if (hoursSinceMidnight > 1)
   1.207 +    ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present");
   1.208 +  
   1.209 +  downloadPromise = promiseDownloadRemoved(publicList);
   1.210 +
   1.211 +  // Clear 1 hour 10 minutes
   1.212 +  s.range = [now_uSec - 70*60*1000000, now_uSec];
   1.213 +  s.sanitize();
   1.214 +  s.range = null;
   1.215 +
   1.216 +  yield promiseFormHistoryRemoved();
   1.217 +  yield downloadPromise;
   1.218 +
   1.219 +  ok(!(yield promiseIsURIVisited(makeURI("http://1hour10minutes.com"))),
   1.220 +     "Pretend visit to 1hour10minutes.com should now be deleted");
   1.221 +  ok((yield promiseIsURIVisited(makeURI("http://2hour.com"))),
   1.222 +     "Pretend visit to 2hour.com should should still exist");
   1.223 +  ok((yield promiseIsURIVisited(makeURI("http://2hour10minutes.com"))),
   1.224 +     "Pretend visit to 2hour10minutes.com should should still exist");
   1.225 +  ok((yield promiseIsURIVisited(makeURI("http://4hour.com"))),
   1.226 +     "Pretend visit to 4hour.com should should still exist");
   1.227 +  ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))),
   1.228 +     "Pretend visit to 4hour10minutes.com should should still exist");
   1.229 +  if (minutesSinceMidnight > 70) {
   1.230 +    ok((yield promiseIsURIVisited(makeURI("http://today.com"))),
   1.231 +       "Pretend visit to today.com should still exist");
   1.232 +  }
   1.233 +  ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))),
   1.234 +    "Pretend visit to before-today.com should still exist");
   1.235 +
   1.236 +  yield countEntries("1hour10minutes", "1hour10minutes form entry should be deleted", checkZero);
   1.237 +  yield countEntries("2hour", "2hour form entry should still exist", checkOne);
   1.238 +  yield countEntries("2hour10minutes", "2hour10minutes form entry should still exist", checkOne);
   1.239 +  yield countEntries("4hour", "4hour form entry should still exist", checkOne);
   1.240 +  yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne);
   1.241 +  if (minutesSinceMidnight > 70)
   1.242 +    yield countEntries("today", "today form entry should still exist", checkOne);
   1.243 +  yield countEntries("b4today", "b4today form entry should still exist", checkOne);
   1.244 +
   1.245 +  ok(!(yield downloadExists(publicList, "fakefile-1-hour-10-minutes")), "1 hour 10 minute old download should now be deleted");
   1.246 +  ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present");
   1.247 +  ok((yield downloadExists(publicList, "fakefile-2-hour")), "<2 hour old download should still be present");
   1.248 +  ok((yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "2 hour 10 minute download should still be present");
   1.249 +  ok((yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should still be present");
   1.250 +  ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present");
   1.251 +  if (minutesSinceMidnight > 70)
   1.252 +    ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present");
   1.253 +
   1.254 +  downloadPromise = promiseDownloadRemoved(publicList);
   1.255 +
   1.256 +  // Clear 2 hours
   1.257 +  Sanitizer.prefs.setIntPref("timeSpan", 2);
   1.258 +  s.sanitize();
   1.259 +
   1.260 +  yield promiseFormHistoryRemoved();
   1.261 +  yield downloadPromise;
   1.262 +
   1.263 +  ok(!(yield promiseIsURIVisited(makeURI("http://2hour.com"))),
   1.264 +     "Pretend visit to 2hour.com should now be deleted");
   1.265 +  ok((yield promiseIsURIVisited(makeURI("http://2hour10minutes.com"))),
   1.266 +     "Pretend visit to 2hour10minutes.com should should still exist");
   1.267 +  ok((yield promiseIsURIVisited(makeURI("http://4hour.com"))),
   1.268 +     "Pretend visit to 4hour.com should should still exist");
   1.269 +  ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))),
   1.270 +     "Pretend visit to 4hour10minutes.com should should still exist");
   1.271 +  if (hoursSinceMidnight > 2) {
   1.272 +    ok((yield promiseIsURIVisited(makeURI("http://today.com"))),
   1.273 +       "Pretend visit to today.com should still exist");
   1.274 +  }
   1.275 +  ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))),
   1.276 +    "Pretend visit to before-today.com should still exist");
   1.277 +
   1.278 +  yield countEntries("2hour", "2hour form entry should be deleted", checkZero);
   1.279 +  yield countEntries("2hour10minutes", "2hour10minutes form entry should still exist", checkOne);
   1.280 +  yield countEntries("4hour", "4hour form entry should still exist", checkOne);
   1.281 +  yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne);
   1.282 +  if (hoursSinceMidnight > 2)
   1.283 +    yield countEntries("today", "today form entry should still exist", checkOne);
   1.284 +  yield countEntries("b4today", "b4today form entry should still exist", checkOne);
   1.285 +
   1.286 +  ok(!(yield downloadExists(publicList, "fakefile-2-hour")), "<2 hour old download should now be deleted");
   1.287 +  ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present");
   1.288 +  ok((yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "2 hour 10 minute download should still be present");
   1.289 +  ok((yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should still be present");
   1.290 +  ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present");
   1.291 +  if (hoursSinceMidnight > 2)
   1.292 +    ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present");
   1.293 +  
   1.294 +  downloadPromise = promiseDownloadRemoved(publicList);
   1.295 +
   1.296 +  // Clear 2 hours 10 minutes
   1.297 +  s.range = [now_uSec - 130*60*1000000, now_uSec];
   1.298 +  s.sanitize();
   1.299 +  s.range = null;
   1.300 +
   1.301 +  yield promiseFormHistoryRemoved();
   1.302 +  yield downloadPromise;
   1.303 +
   1.304 +  ok(!(yield promiseIsURIVisited(makeURI("http://2hour10minutes.com"))),
   1.305 +     "Pretend visit to 2hour10minutes.com should now be deleted");
   1.306 +  ok((yield promiseIsURIVisited(makeURI("http://4hour.com"))),
   1.307 +     "Pretend visit to 4hour.com should should still exist");
   1.308 +  ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))),
   1.309 +     "Pretend visit to 4hour10minutes.com should should still exist");
   1.310 +  if (minutesSinceMidnight > 130) {
   1.311 +    ok((yield promiseIsURIVisited(makeURI("http://today.com"))),
   1.312 +       "Pretend visit to today.com should still exist");
   1.313 +  }
   1.314 +  ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))),
   1.315 +    "Pretend visit to before-today.com should still exist");
   1.316 +
   1.317 +  yield countEntries("2hour10minutes", "2hour10minutes form entry should be deleted", checkZero);
   1.318 +  yield countEntries("4hour", "4hour form entry should still exist", checkOne);
   1.319 +  yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne);
   1.320 +  if (minutesSinceMidnight > 130)
   1.321 +    yield countEntries("today", "today form entry should still exist", checkOne);
   1.322 +  yield countEntries("b4today", "b4today form entry should still exist", checkOne);
   1.323 +
   1.324 +  ok(!(yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "2 hour 10 minute old download should now be deleted");
   1.325 +  ok((yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should still be present");
   1.326 +  ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present");
   1.327 +  ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present");
   1.328 +  if (minutesSinceMidnight > 130)
   1.329 +    ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present");
   1.330 +
   1.331 +  downloadPromise = promiseDownloadRemoved(publicList);
   1.332 +
   1.333 +  // Clear 4 hours
   1.334 +  Sanitizer.prefs.setIntPref("timeSpan", 3);
   1.335 +  s.sanitize();
   1.336 +
   1.337 +  yield promiseFormHistoryRemoved();
   1.338 +  yield downloadPromise;
   1.339 +
   1.340 +  ok(!(yield promiseIsURIVisited(makeURI("http://4hour.com"))),
   1.341 +     "Pretend visit to 4hour.com should now be deleted");
   1.342 +  ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))),
   1.343 +     "Pretend visit to 4hour10minutes.com should should still exist");
   1.344 +  if (hoursSinceMidnight > 4) {
   1.345 +    ok((yield promiseIsURIVisited(makeURI("http://today.com"))),
   1.346 +       "Pretend visit to today.com should still exist");
   1.347 +  }
   1.348 +  ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))),
   1.349 +    "Pretend visit to before-today.com should still exist");
   1.350 +
   1.351 +  yield countEntries("4hour", "4hour form entry should be deleted", checkZero);
   1.352 +  yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne);
   1.353 +  if (hoursSinceMidnight > 4)
   1.354 +    yield countEntries("today", "today form entry should still exist", checkOne);
   1.355 +  yield countEntries("b4today", "b4today form entry should still exist", checkOne);
   1.356 +
   1.357 +  ok(!(yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should now be deleted");
   1.358 +  ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present");
   1.359 +  ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present");
   1.360 +  if (hoursSinceMidnight > 4)
   1.361 +    ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present");
   1.362 +
   1.363 +  downloadPromise = promiseDownloadRemoved(publicList);
   1.364 +
   1.365 +  // Clear 4 hours 10 minutes
   1.366 +  s.range = [now_uSec - 250*60*1000000, now_uSec];
   1.367 +  s.sanitize();
   1.368 +  s.range = null;
   1.369 +
   1.370 +  yield promiseFormHistoryRemoved();
   1.371 +  yield downloadPromise;
   1.372 +
   1.373 +  ok(!(yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))),
   1.374 +     "Pretend visit to 4hour10minutes.com should now be deleted");
   1.375 +  if (minutesSinceMidnight > 250) {
   1.376 +    ok((yield promiseIsURIVisited(makeURI("http://today.com"))),
   1.377 +       "Pretend visit to today.com should still exist");
   1.378 +  }
   1.379 +  ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))),
   1.380 +    "Pretend visit to before-today.com should still exist");
   1.381 +
   1.382 +  yield countEntries("4hour10minutes", "4hour10minutes form entry should be deleted", checkZero);
   1.383 +  if (minutesSinceMidnight > 250)
   1.384 +    yield countEntries("today", "today form entry should still exist", checkOne);
   1.385 +  yield countEntries("b4today", "b4today form entry should still exist", checkOne);
   1.386 +  
   1.387 +  ok(!(yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should now be deleted");
   1.388 +  ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present");
   1.389 +  if (minutesSinceMidnight > 250)
   1.390 +    ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present");
   1.391 +
   1.392 +  // The 'Today' download might have been already deleted, in which case we
   1.393 +  // should not wait for a download removal notification.
   1.394 +  if (minutesSinceMidnight > 250) {
   1.395 +    downloadPromise = promiseDownloadRemoved(publicList);
   1.396 +  } else {
   1.397 +    downloadPromise = Promise.resolve();
   1.398 +  }
   1.399 +
   1.400 +  // Clear Today
   1.401 +  Sanitizer.prefs.setIntPref("timeSpan", 4);
   1.402 +  s.sanitize();
   1.403 +
   1.404 +  yield promiseFormHistoryRemoved();
   1.405 +  yield downloadPromise;
   1.406 +
   1.407 +  // Be careful.  If we add our objectss just before midnight, and sanitize
   1.408 +  // runs immediately after, they won't be expired.  This is expected, but
   1.409 +  // we should not test in that case.  We cannot just test for opposite
   1.410 +  // condition because we could cross midnight just one moment after we
   1.411 +  // cache our time, then we would have an even worse random failure.
   1.412 +  var today = isToday(new Date(now_mSec));
   1.413 +  if (today) {
   1.414 +    ok(!(yield promiseIsURIVisited(makeURI("http://today.com"))),
   1.415 +       "Pretend visit to today.com should now be deleted");
   1.416 +
   1.417 +    yield countEntries("today", "today form entry should be deleted", checkZero);
   1.418 +    ok(!(yield downloadExists(publicList, "fakefile-today")), "'Today' download should now be deleted");
   1.419 +  }
   1.420 +
   1.421 +  ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))),
   1.422 +     "Pretend visit to before-today.com should still exist");
   1.423 +  yield countEntries("b4today", "b4today form entry should still exist", checkOne);
   1.424 +  ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present");
   1.425 +
   1.426 +  downloadPromise = promiseDownloadRemoved(publicList);
   1.427 +
   1.428 +  // Choose everything
   1.429 +  Sanitizer.prefs.setIntPref("timeSpan", 0);
   1.430 +  s.sanitize();
   1.431 +
   1.432 +  yield promiseFormHistoryRemoved();
   1.433 +  yield downloadPromise;
   1.434 +
   1.435 +  ok(!(yield promiseIsURIVisited(makeURI("http://before-today.com"))),
   1.436 +     "Pretend visit to before-today.com should now be deleted");
   1.437 +
   1.438 +  yield countEntries("b4today", "b4today form entry should be deleted", checkZero);
   1.439 +
   1.440 +  ok(!(yield downloadExists(publicList, "fakefile-old")), "Year old download should now be deleted");
   1.441 +}
   1.442 +
   1.443 +function setupHistory() {
   1.444 +  let deferred = Promise.defer();
   1.445 +
   1.446 +  let places = [];
   1.447 +
   1.448 +  function addPlace(aURI, aTitle, aVisitDate) {
   1.449 +    places.push({
   1.450 +      uri: aURI,
   1.451 +      title: aTitle,
   1.452 +      visits: [{
   1.453 +        visitDate: aVisitDate,
   1.454 +        transitionType: Ci.nsINavHistoryService.TRANSITION_LINK
   1.455 +      }]
   1.456 +    });
   1.457 +  }
   1.458 +
   1.459 +  addPlace(makeURI("http://10minutes.com/"), "10 minutes ago", now_uSec - 10 * kUsecPerMin);
   1.460 +  addPlace(makeURI("http://1hour.com/"), "Less than 1 hour ago", now_uSec - 45 * kUsecPerMin);
   1.461 +  addPlace(makeURI("http://1hour10minutes.com/"), "1 hour 10 minutes ago", now_uSec - 70 * kUsecPerMin);
   1.462 +  addPlace(makeURI("http://2hour.com/"), "Less than 2 hours ago", now_uSec - 90 * kUsecPerMin);
   1.463 +  addPlace(makeURI("http://2hour10minutes.com/"), "2 hours 10 minutes ago", now_uSec - 130 * kUsecPerMin);
   1.464 +  addPlace(makeURI("http://4hour.com/"), "Less than 4 hours ago", now_uSec - 180 * kUsecPerMin);
   1.465 +  addPlace(makeURI("http://4hour10minutes.com/"), "4 hours 10 minutesago", now_uSec - 250 * kUsecPerMin);
   1.466 +
   1.467 +  let today = new Date();
   1.468 +  today.setHours(0);
   1.469 +  today.setMinutes(0);
   1.470 +  today.setSeconds(1);
   1.471 +  addPlace(makeURI("http://today.com/"), "Today", today.getTime() * 1000);
   1.472 +
   1.473 +  let lastYear = new Date();
   1.474 +  lastYear.setFullYear(lastYear.getFullYear() - 1);
   1.475 +  addPlace(makeURI("http://before-today.com/"), "Before Today", lastYear.getTime() * 1000);
   1.476 +
   1.477 +  PlacesUtils.asyncHistory.updatePlaces(places, {
   1.478 +    handleError: function () ok(false, "Unexpected error in adding visit."),
   1.479 +    handleResult: function () { },
   1.480 +    handleCompletion: function () deferred.resolve()
   1.481 +  });
   1.482 +
   1.483 +  return deferred.promise;
   1.484 +}
   1.485 +
   1.486 +function setupFormHistory() {
   1.487 +
   1.488 +  function searchEntries(terms, params) {
   1.489 +    let deferred = Promise.defer();
   1.490 +
   1.491 +    let results = [];
   1.492 +    FormHistory.search(terms, params, { handleResult: function (result) results.push(result),
   1.493 +                                        handleError: function (error) {
   1.494 +                                          do_throw("Error occurred searching form history: " + error);
   1.495 +                                          deferred.reject(error);
   1.496 +                                        },
   1.497 +                                        handleCompletion: function (reason) { deferred.resolve(results); }
   1.498 +                                      });
   1.499 +    return deferred.promise;
   1.500 +  }
   1.501 +
   1.502 +  function update(changes)
   1.503 +  {
   1.504 +    let deferred = Promise.defer();
   1.505 +    FormHistory.update(changes, { handleError: function (error) {
   1.506 +                                    do_throw("Error occurred searching form history: " + error);
   1.507 +                                    deferred.reject(error);
   1.508 +                                  },
   1.509 +                                  handleCompletion: function (reason) { deferred.resolve(); }
   1.510 +                                });
   1.511 +    return deferred.promise;
   1.512 +  }
   1.513 +
   1.514 +  // Make sure we've got a clean DB to start with, then add the entries we'll be testing.
   1.515 +  yield update(
   1.516 +    [{
   1.517 +        op: "remove"
   1.518 +     },
   1.519 +     {
   1.520 +        op : "add",
   1.521 +        fieldname : "10minutes",
   1.522 +        value : "10m"
   1.523 +      }, {
   1.524 +        op : "add",
   1.525 +        fieldname : "1hour",
   1.526 +        value : "1h"
   1.527 +      }, {
   1.528 +        op : "add",
   1.529 +        fieldname : "1hour10minutes",
   1.530 +        value : "1h10m"
   1.531 +      }, {
   1.532 +        op : "add",
   1.533 +        fieldname : "2hour",
   1.534 +        value : "2h"
   1.535 +      }, {
   1.536 +        op : "add",
   1.537 +        fieldname : "2hour10minutes",
   1.538 +        value : "2h10m"
   1.539 +      }, {
   1.540 +        op : "add",
   1.541 +        fieldname : "4hour",
   1.542 +        value : "4h"
   1.543 +      }, {
   1.544 +        op : "add",
   1.545 +        fieldname : "4hour10minutes",
   1.546 +        value : "4h10m"
   1.547 +      }, {
   1.548 +        op : "add",
   1.549 +        fieldname : "today",
   1.550 +        value : "1d"
   1.551 +      }, {
   1.552 +        op : "add",
   1.553 +        fieldname : "b4today",
   1.554 +        value : "1y"
   1.555 +      }]);
   1.556 +
   1.557 +  // Artifically age the entries to the proper vintage.
   1.558 +  let timestamp = now_uSec - 10 * kUsecPerMin;
   1.559 +  let results = yield searchEntries(["guid"], { fieldname: "10minutes" });
   1.560 +  yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
   1.561 +
   1.562 +  timestamp = now_uSec - 45 * kUsecPerMin;
   1.563 +  results = yield searchEntries(["guid"], { fieldname: "1hour" });
   1.564 +  yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
   1.565 +
   1.566 +  timestamp = now_uSec - 70 * kUsecPerMin;
   1.567 +  results = yield searchEntries(["guid"], { fieldname: "1hour10minutes" });
   1.568 +  yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
   1.569 +
   1.570 +  timestamp = now_uSec - 90 * kUsecPerMin;
   1.571 +  results = yield searchEntries(["guid"], { fieldname: "2hour" });
   1.572 +  yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
   1.573 +
   1.574 +  timestamp = now_uSec - 130 * kUsecPerMin;
   1.575 +  results = yield searchEntries(["guid"], { fieldname: "2hour10minutes" });
   1.576 +  yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
   1.577 +
   1.578 +  timestamp = now_uSec - 180 * kUsecPerMin;
   1.579 +  results = yield searchEntries(["guid"], { fieldname: "4hour" });
   1.580 +  yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
   1.581 +
   1.582 +  timestamp = now_uSec - 250 * kUsecPerMin;
   1.583 +  results = yield searchEntries(["guid"], { fieldname: "4hour10minutes" });
   1.584 +  yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
   1.585 +
   1.586 +  let today = new Date();
   1.587 +  today.setHours(0);
   1.588 +  today.setMinutes(0);
   1.589 +  today.setSeconds(1);
   1.590 +  timestamp = today.getTime() * 1000;
   1.591 +  results = yield searchEntries(["guid"], { fieldname: "today" });
   1.592 +  yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
   1.593 +
   1.594 +  let lastYear = new Date();
   1.595 +  lastYear.setFullYear(lastYear.getFullYear() - 1);
   1.596 +  timestamp = lastYear.getTime() * 1000;
   1.597 +  results = yield searchEntries(["guid"], { fieldname: "b4today" });
   1.598 +  yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
   1.599 +
   1.600 +  var checks = 0;
   1.601 +  let checkOne = function(num, message) { is(num, 1, message); checks++; }
   1.602 +
   1.603 +  // Sanity check.
   1.604 +  yield countEntries("10minutes", "Checking for 10minutes form history entry creation", checkOne);
   1.605 +  yield countEntries("1hour", "Checking for 1hour form history entry creation", checkOne);
   1.606 +  yield countEntries("1hour10minutes", "Checking for 1hour10minutes form history entry creation", checkOne);
   1.607 +  yield countEntries("2hour", "Checking for 2hour form history entry creation", checkOne);
   1.608 +  yield countEntries("2hour10minutes", "Checking for 2hour10minutes form history entry creation", checkOne);
   1.609 +  yield countEntries("4hour", "Checking for 4hour form history entry creation", checkOne);
   1.610 +  yield countEntries("4hour10minutes", "Checking for 4hour10minutes form history entry creation", checkOne);
   1.611 +  yield countEntries("today", "Checking for today form history entry creation", checkOne);
   1.612 +  yield countEntries("b4today", "Checking for b4today form history entry creation", checkOne);
   1.613 +  is(checks, 9, "9 checks made");
   1.614 +}
   1.615 +
   1.616 +function setupDownloads() {
   1.617 +
   1.618 +  let publicList = yield Downloads.getList(Downloads.PUBLIC);
   1.619 +
   1.620 +  let download = yield Downloads.createDownload({
   1.621 +    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
   1.622 +    target: "fakefile-10-minutes"
   1.623 +  });
   1.624 +  download.startTime = new Date(now_mSec - 10 * kMsecPerMin), // 10 minutes ago
   1.625 +  download.canceled = true;
   1.626 +  yield publicList.add(download);
   1.627 +
   1.628 +  download = yield Downloads.createDownload({
   1.629 +    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
   1.630 +    target: "fakefile-1-hour"
   1.631 +  });
   1.632 +  download.startTime = new Date(now_mSec - 45 * kMsecPerMin), // 45 minutes ago
   1.633 +  download.canceled = true;
   1.634 +  yield publicList.add(download);
   1.635 +
   1.636 +  download = yield Downloads.createDownload({
   1.637 +    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
   1.638 +    target: "fakefile-1-hour-10-minutes"
   1.639 +  });
   1.640 +  download.startTime = new Date(now_mSec - 70 * kMsecPerMin), // 70 minutes ago
   1.641 +  download.canceled = true;
   1.642 +  yield publicList.add(download);
   1.643 +
   1.644 +  download = yield Downloads.createDownload({
   1.645 +    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
   1.646 +    target: "fakefile-2-hour"
   1.647 +  });
   1.648 +  download.startTime = new Date(now_mSec - 90 * kMsecPerMin), // 90 minutes ago
   1.649 +  download.canceled = true;
   1.650 +  yield publicList.add(download);
   1.651 +
   1.652 +  download = yield Downloads.createDownload({
   1.653 +    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
   1.654 +    target: "fakefile-2-hour-10-minutes"
   1.655 +  });
   1.656 +  download.startTime = new Date(now_mSec - 130 * kMsecPerMin), // 130 minutes ago
   1.657 +  download.canceled = true;
   1.658 +  yield publicList.add(download);
   1.659 +
   1.660 +  download = yield Downloads.createDownload({
   1.661 +    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
   1.662 +    target: "fakefile-4-hour"
   1.663 +  });
   1.664 +  download.startTime = new Date(now_mSec - 180 * kMsecPerMin), // 180 minutes ago
   1.665 +  download.canceled = true;
   1.666 +  yield publicList.add(download);
   1.667 +
   1.668 +  download = yield Downloads.createDownload({
   1.669 +    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
   1.670 +    target: "fakefile-4-hour-10-minutes"
   1.671 +  });
   1.672 +  download.startTime = new Date(now_mSec - 250 * kMsecPerMin), // 250 minutes ago
   1.673 +  download.canceled = true;
   1.674 +  yield publicList.add(download);
   1.675 +
   1.676 +  // Add "today" download
   1.677 +  let today = new Date();
   1.678 +  today.setHours(0);
   1.679 +  today.setMinutes(0);
   1.680 +  today.setSeconds(1);
   1.681 +
   1.682 +  download = yield Downloads.createDownload({
   1.683 +    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
   1.684 +    target: "fakefile-today"
   1.685 +  });
   1.686 +  download.startTime = today, // 12:00:01 AM this morning
   1.687 +  download.canceled = true;
   1.688 +  yield publicList.add(download);
   1.689 +  
   1.690 +  // Add "before today" download
   1.691 +  let lastYear = new Date();
   1.692 +  lastYear.setFullYear(lastYear.getFullYear() - 1);
   1.693 +
   1.694 +  download = yield Downloads.createDownload({
   1.695 +    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
   1.696 +    target: "fakefile-old"
   1.697 +  });
   1.698 +  download.startTime = lastYear,
   1.699 +  download.canceled = true;
   1.700 +  yield publicList.add(download);
   1.701 +  
   1.702 +  // Confirm everything worked
   1.703 +  let downloads = yield publicList.getAll();
   1.704 +  is(downloads.length, 9, "9 Pretend downloads added");
   1.705 +
   1.706 +  ok((yield downloadExists(publicList, "fakefile-old")), "Pretend download for everything case should exist");
   1.707 +  ok((yield downloadExists(publicList, "fakefile-10-minutes")), "Pretend download for 10-minutes case should exist");
   1.708 +  ok((yield downloadExists(publicList, "fakefile-1-hour")), "Pretend download for 1-hour case should exist");
   1.709 +  ok((yield downloadExists(publicList, "fakefile-1-hour-10-minutes")), "Pretend download for 1-hour-10-minutes case should exist");
   1.710 +  ok((yield downloadExists(publicList, "fakefile-2-hour")), "Pretend download for 2-hour case should exist");
   1.711 +  ok((yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "Pretend download for 2-hour-10-minutes case should exist");
   1.712 +  ok((yield downloadExists(publicList, "fakefile-4-hour")), "Pretend download for 4-hour case should exist");
   1.713 +  ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "Pretend download for 4-hour-10-minutes case should exist");
   1.714 +  ok((yield downloadExists(publicList, "fakefile-today")), "Pretend download for Today case should exist");
   1.715 +}
   1.716 +
   1.717 +/**
   1.718 + * Checks to see if the downloads with the specified id exists.
   1.719 + *
   1.720 + * @param aID
   1.721 + *        The ids of the downloads to check.
   1.722 + */
   1.723 +function downloadExists(list, path)
   1.724 +{
   1.725 +  return Task.spawn(function() {
   1.726 +    let listArray = yield list.getAll();
   1.727 +    throw new Task.Result(listArray.some(i => i.target.path == path));
   1.728 +  });
   1.729 +}
   1.730 +
   1.731 +function isToday(aDate) {
   1.732 +  return aDate.getDate() == new Date().getDate();
   1.733 +}

mercurial