tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_sanitize-timespans_v2.js (33157B)


      1 requestLongerTimeout(2);
      2 
      3 const { PlacesTestUtils } = ChromeUtils.importESModule(
      4  "resource://testing-common/PlacesTestUtils.sys.mjs"
      5 );
      6 
      7 // Bug 453440 - Test the timespan-based logic of the sanitizer code
      8 var now_mSec = Date.now();
      9 var now_uSec = now_mSec * 1000;
     10 
     11 const kMsecPerMin = 60 * 1000;
     12 const kUsecPerMin = 60 * 1000000;
     13 
     14 function promiseFormHistoryRemoved() {
     15  return new Promise(resolve => {
     16    Services.obs.addObserver(function onfh() {
     17      Services.obs.removeObserver(onfh, "satchel-storage-changed");
     18      resolve();
     19    }, "satchel-storage-changed");
     20  });
     21 }
     22 
     23 function promiseDownloadRemoved(list) {
     24  return new Promise(resolve => {
     25    let view = {
     26      onDownloadRemoved() {
     27        list.removeView(view);
     28        resolve();
     29      },
     30    };
     31 
     32    list.addView(view);
     33  });
     34 }
     35 
     36 add_task(async function test() {
     37  await setupDownloads();
     38  await setupFormHistory();
     39  await setupHistory();
     40  await onHistoryReady();
     41 });
     42 
     43 async function countEntries(name, message, check) {
     44  var obj = {};
     45  if (name !== null) {
     46    obj.fieldname = name;
     47  }
     48  let count = await FormHistory.count(obj);
     49  check(count, message);
     50 }
     51 
     52 async function onHistoryReady() {
     53  var hoursSinceMidnight = new Date().getHours();
     54  var minutesSinceMidnight = hoursSinceMidnight * 60 + new Date().getMinutes();
     55 
     56  // Should test cookies here, but nsICookieManager/nsICookieService
     57  // doesn't let us fake creation times.  bug 463127
     58 
     59  await SpecialPowers.pushPrefEnv({
     60    set: [["privacy.sanitize.useOldClearHistoryDialog", false]],
     61  });
     62 
     63  let itemsToClear = ["historyAndFormData", "downloads"];
     64 
     65  let publicList = await Downloads.getList(Downloads.PUBLIC);
     66  let downloadPromise = promiseDownloadRemoved(publicList);
     67  let formHistoryPromise = promiseFormHistoryRemoved();
     68 
     69  // Clear 10 minutes ago
     70  let range = [now_uSec - 10 * 60 * 1000000, now_uSec];
     71  await Sanitizer.sanitize(itemsToClear, { range, ignoreTimespan: false });
     72 
     73  await formHistoryPromise;
     74  await downloadPromise;
     75 
     76  ok(
     77    !(await PlacesUtils.history.hasVisits("https://10minutes.com")),
     78    "Pretend visit to 10minutes.com should now be deleted"
     79  );
     80  ok(
     81    await PlacesUtils.history.hasVisits("https://1hour.com"),
     82    "Pretend visit to 1hour.com should should still exist"
     83  );
     84  ok(
     85    await PlacesUtils.history.hasVisits("https://1hour10minutes.com"),
     86    "Pretend visit to 1hour10minutes.com should should still exist"
     87  );
     88  ok(
     89    await PlacesUtils.history.hasVisits("https://2hour.com"),
     90    "Pretend visit to 2hour.com should should still exist"
     91  );
     92  ok(
     93    await PlacesUtils.history.hasVisits("https://2hour10minutes.com"),
     94    "Pretend visit to 2hour10minutes.com should should still exist"
     95  );
     96  ok(
     97    await PlacesUtils.history.hasVisits("https://4hour.com"),
     98    "Pretend visit to 4hour.com should should still exist"
     99  );
    100  ok(
    101    await PlacesUtils.history.hasVisits("https://4hour10minutes.com"),
    102    "Pretend visit to 4hour10minutes.com should should still exist"
    103  );
    104  if (minutesSinceMidnight > 10) {
    105    ok(
    106      await PlacesUtils.history.hasVisits("https://today.com"),
    107      "Pretend visit to today.com should still exist"
    108    );
    109  }
    110  ok(
    111    await PlacesUtils.history.hasVisits("https://before-today.com"),
    112    "Pretend visit to before-today.com should still exist"
    113  );
    114 
    115  let checkZero = function (num, message) {
    116    is(num, 0, message);
    117  };
    118  let checkOne = function (num, message) {
    119    is(num, 1, message);
    120  };
    121 
    122  await countEntries(
    123    "10minutes",
    124    "10minutes form entry should be deleted",
    125    checkZero
    126  );
    127  await countEntries("1hour", "1hour form entry should still exist", checkOne);
    128  await countEntries(
    129    "1hour10minutes",
    130    "1hour10minutes form entry should still exist",
    131    checkOne
    132  );
    133  await countEntries("2hour", "2hour form entry should still exist", checkOne);
    134  await countEntries(
    135    "2hour10minutes",
    136    "2hour10minutes form entry should still exist",
    137    checkOne
    138  );
    139  await countEntries("4hour", "4hour form entry should still exist", checkOne);
    140  await countEntries(
    141    "4hour10minutes",
    142    "4hour10minutes form entry should still exist",
    143    checkOne
    144  );
    145  if (minutesSinceMidnight > 10) {
    146    await countEntries(
    147      "today",
    148      "today form entry should still exist",
    149      checkOne
    150    );
    151  }
    152  await countEntries(
    153    "b4today",
    154    "b4today form entry should still exist",
    155    checkOne
    156  );
    157 
    158  ok(
    159    !(await downloadExists(publicList, "fakefile-10-minutes")),
    160    "10 minute download should now be deleted"
    161  );
    162  ok(
    163    await downloadExists(publicList, "fakefile-1-hour"),
    164    "<1 hour download should still be present"
    165  );
    166  ok(
    167    await downloadExists(publicList, "fakefile-1-hour-10-minutes"),
    168    "1 hour 10 minute download should still be present"
    169  );
    170  ok(
    171    await downloadExists(publicList, "fakefile-old"),
    172    "Year old download should still be present"
    173  );
    174  ok(
    175    await downloadExists(publicList, "fakefile-2-hour"),
    176    "<2 hour old download should still be present"
    177  );
    178  ok(
    179    await downloadExists(publicList, "fakefile-2-hour-10-minutes"),
    180    "2 hour 10 minute download should still be present"
    181  );
    182  ok(
    183    await downloadExists(publicList, "fakefile-4-hour"),
    184    "<4 hour old download should still be present"
    185  );
    186  ok(
    187    await downloadExists(publicList, "fakefile-4-hour-10-minutes"),
    188    "4 hour 10 minute download should still be present"
    189  );
    190 
    191  if (minutesSinceMidnight > 10) {
    192    ok(
    193      await downloadExists(publicList, "fakefile-today"),
    194      "'Today' download should still be present"
    195    );
    196  }
    197 
    198  downloadPromise = promiseDownloadRemoved(publicList);
    199  formHistoryPromise = promiseFormHistoryRemoved();
    200 
    201  // Clear 1 hour
    202  Services.prefs.setIntPref(Sanitizer.PREF_TIMESPAN, 1);
    203  await Sanitizer.sanitize(itemsToClear, { ignoreTimespan: false });
    204 
    205  await formHistoryPromise;
    206  await downloadPromise;
    207 
    208  ok(
    209    !(await PlacesUtils.history.hasVisits("https://1hour.com")),
    210    "Pretend visit to 1hour.com should now be deleted"
    211  );
    212  ok(
    213    await PlacesUtils.history.hasVisits("https://1hour10minutes.com"),
    214    "Pretend visit to 1hour10minutes.com should should still exist"
    215  );
    216  ok(
    217    await PlacesUtils.history.hasVisits("https://2hour.com"),
    218    "Pretend visit to 2hour.com should should still exist"
    219  );
    220  ok(
    221    await PlacesUtils.history.hasVisits("https://2hour10minutes.com"),
    222    "Pretend visit to 2hour10minutes.com should should still exist"
    223  );
    224  ok(
    225    await PlacesUtils.history.hasVisits("https://4hour.com"),
    226    "Pretend visit to 4hour.com should should still exist"
    227  );
    228  ok(
    229    await PlacesUtils.history.hasVisits("https://4hour10minutes.com"),
    230    "Pretend visit to 4hour10minutes.com should should still exist"
    231  );
    232  if (hoursSinceMidnight > 1) {
    233    ok(
    234      await PlacesUtils.history.hasVisits("https://today.com"),
    235      "Pretend visit to today.com should still exist"
    236    );
    237  }
    238  ok(
    239    await PlacesUtils.history.hasVisits("https://before-today.com"),
    240    "Pretend visit to before-today.com should still exist"
    241  );
    242 
    243  await countEntries("1hour", "1hour form entry should be deleted", checkZero);
    244  await countEntries(
    245    "1hour10minutes",
    246    "1hour10minutes form entry should still exist",
    247    checkOne
    248  );
    249  await countEntries("2hour", "2hour form entry should still exist", checkOne);
    250  await countEntries(
    251    "2hour10minutes",
    252    "2hour10minutes form entry should still exist",
    253    checkOne
    254  );
    255  await countEntries("4hour", "4hour form entry should still exist", checkOne);
    256  await countEntries(
    257    "4hour10minutes",
    258    "4hour10minutes form entry should still exist",
    259    checkOne
    260  );
    261  if (hoursSinceMidnight > 1) {
    262    await countEntries(
    263      "today",
    264      "today form entry should still exist",
    265      checkOne
    266    );
    267  }
    268  await countEntries(
    269    "b4today",
    270    "b4today form entry should still exist",
    271    checkOne
    272  );
    273 
    274  ok(
    275    !(await downloadExists(publicList, "fakefile-1-hour")),
    276    "<1 hour download should now be deleted"
    277  );
    278  ok(
    279    await downloadExists(publicList, "fakefile-1-hour-10-minutes"),
    280    "1 hour 10 minute download should still be present"
    281  );
    282  ok(
    283    await downloadExists(publicList, "fakefile-old"),
    284    "Year old download should still be present"
    285  );
    286  ok(
    287    await downloadExists(publicList, "fakefile-2-hour"),
    288    "<2 hour old download should still be present"
    289  );
    290  ok(
    291    await downloadExists(publicList, "fakefile-2-hour-10-minutes"),
    292    "2 hour 10 minute download should still be present"
    293  );
    294  ok(
    295    await downloadExists(publicList, "fakefile-4-hour"),
    296    "<4 hour old download should still be present"
    297  );
    298  ok(
    299    await downloadExists(publicList, "fakefile-4-hour-10-minutes"),
    300    "4 hour 10 minute download should still be present"
    301  );
    302 
    303  if (hoursSinceMidnight > 1) {
    304    ok(
    305      await downloadExists(publicList, "fakefile-today"),
    306      "'Today' download should still be present"
    307    );
    308  }
    309 
    310  downloadPromise = promiseDownloadRemoved(publicList);
    311  formHistoryPromise = promiseFormHistoryRemoved();
    312 
    313  // Clear 1 hour 10 minutes
    314  range = [now_uSec - 70 * 60 * 1000000, now_uSec];
    315  await Sanitizer.sanitize(itemsToClear, { range, ignoreTimespan: false });
    316 
    317  await formHistoryPromise;
    318  await downloadPromise;
    319 
    320  ok(
    321    !(await PlacesUtils.history.hasVisits("https://1hour10minutes.com")),
    322    "Pretend visit to 1hour10minutes.com should now be deleted"
    323  );
    324  ok(
    325    await PlacesUtils.history.hasVisits("https://2hour.com"),
    326    "Pretend visit to 2hour.com should should still exist"
    327  );
    328  ok(
    329    await PlacesUtils.history.hasVisits("https://2hour10minutes.com"),
    330    "Pretend visit to 2hour10minutes.com should should still exist"
    331  );
    332  ok(
    333    await PlacesUtils.history.hasVisits("https://4hour.com"),
    334    "Pretend visit to 4hour.com should should still exist"
    335  );
    336  ok(
    337    await PlacesUtils.history.hasVisits("https://4hour10minutes.com"),
    338    "Pretend visit to 4hour10minutes.com should should still exist"
    339  );
    340  if (minutesSinceMidnight > 70) {
    341    ok(
    342      await PlacesUtils.history.hasVisits("https://today.com"),
    343      "Pretend visit to today.com should still exist"
    344    );
    345  }
    346  ok(
    347    await PlacesUtils.history.hasVisits("https://before-today.com"),
    348    "Pretend visit to before-today.com should still exist"
    349  );
    350 
    351  await countEntries(
    352    "1hour10minutes",
    353    "1hour10minutes form entry should be deleted",
    354    checkZero
    355  );
    356  await countEntries("2hour", "2hour form entry should still exist", checkOne);
    357  await countEntries(
    358    "2hour10minutes",
    359    "2hour10minutes form entry should still exist",
    360    checkOne
    361  );
    362  await countEntries("4hour", "4hour form entry should still exist", checkOne);
    363  await countEntries(
    364    "4hour10minutes",
    365    "4hour10minutes form entry should still exist",
    366    checkOne
    367  );
    368  if (minutesSinceMidnight > 70) {
    369    await countEntries(
    370      "today",
    371      "today form entry should still exist",
    372      checkOne
    373    );
    374  }
    375  await countEntries(
    376    "b4today",
    377    "b4today form entry should still exist",
    378    checkOne
    379  );
    380 
    381  ok(
    382    !(await downloadExists(publicList, "fakefile-1-hour-10-minutes")),
    383    "1 hour 10 minute old download should now be deleted"
    384  );
    385  ok(
    386    await downloadExists(publicList, "fakefile-old"),
    387    "Year old download should still be present"
    388  );
    389  ok(
    390    await downloadExists(publicList, "fakefile-2-hour"),
    391    "<2 hour old download should still be present"
    392  );
    393  ok(
    394    await downloadExists(publicList, "fakefile-2-hour-10-minutes"),
    395    "2 hour 10 minute download should still be present"
    396  );
    397  ok(
    398    await downloadExists(publicList, "fakefile-4-hour"),
    399    "<4 hour old download should still be present"
    400  );
    401  ok(
    402    await downloadExists(publicList, "fakefile-4-hour-10-minutes"),
    403    "4 hour 10 minute download should still be present"
    404  );
    405  if (minutesSinceMidnight > 70) {
    406    ok(
    407      await downloadExists(publicList, "fakefile-today"),
    408      "'Today' download should still be present"
    409    );
    410  }
    411 
    412  downloadPromise = promiseDownloadRemoved(publicList);
    413  formHistoryPromise = promiseFormHistoryRemoved();
    414 
    415  // Clear 2 hours
    416  Services.prefs.setIntPref(Sanitizer.PREF_TIMESPAN, 2);
    417  await Sanitizer.sanitize(itemsToClear, { ignoreTimespan: false });
    418 
    419  await formHistoryPromise;
    420  await downloadPromise;
    421 
    422  ok(
    423    !(await PlacesUtils.history.hasVisits("https://2hour.com")),
    424    "Pretend visit to 2hour.com should now be deleted"
    425  );
    426  ok(
    427    await PlacesUtils.history.hasVisits("https://2hour10minutes.com"),
    428    "Pretend visit to 2hour10minutes.com should should still exist"
    429  );
    430  ok(
    431    await PlacesUtils.history.hasVisits("https://4hour.com"),
    432    "Pretend visit to 4hour.com should should still exist"
    433  );
    434  ok(
    435    await PlacesUtils.history.hasVisits("https://4hour10minutes.com"),
    436    "Pretend visit to 4hour10minutes.com should should still exist"
    437  );
    438  if (hoursSinceMidnight > 2) {
    439    ok(
    440      await PlacesUtils.history.hasVisits("https://today.com"),
    441      "Pretend visit to today.com should still exist"
    442    );
    443  }
    444  ok(
    445    await PlacesUtils.history.hasVisits("https://before-today.com"),
    446    "Pretend visit to before-today.com should still exist"
    447  );
    448 
    449  await countEntries("2hour", "2hour form entry should be deleted", checkZero);
    450  await countEntries(
    451    "2hour10minutes",
    452    "2hour10minutes form entry should still exist",
    453    checkOne
    454  );
    455  await countEntries("4hour", "4hour form entry should still exist", checkOne);
    456  await countEntries(
    457    "4hour10minutes",
    458    "4hour10minutes form entry should still exist",
    459    checkOne
    460  );
    461  if (hoursSinceMidnight > 2) {
    462    await countEntries(
    463      "today",
    464      "today form entry should still exist",
    465      checkOne
    466    );
    467  }
    468  await countEntries(
    469    "b4today",
    470    "b4today form entry should still exist",
    471    checkOne
    472  );
    473 
    474  ok(
    475    !(await downloadExists(publicList, "fakefile-2-hour")),
    476    "<2 hour old download should now be deleted"
    477  );
    478  ok(
    479    await downloadExists(publicList, "fakefile-old"),
    480    "Year old download should still be present"
    481  );
    482  ok(
    483    await downloadExists(publicList, "fakefile-2-hour-10-minutes"),
    484    "2 hour 10 minute download should still be present"
    485  );
    486  ok(
    487    await downloadExists(publicList, "fakefile-4-hour"),
    488    "<4 hour old download should still be present"
    489  );
    490  ok(
    491    await downloadExists(publicList, "fakefile-4-hour-10-minutes"),
    492    "4 hour 10 minute download should still be present"
    493  );
    494  if (hoursSinceMidnight > 2) {
    495    ok(
    496      await downloadExists(publicList, "fakefile-today"),
    497      "'Today' download should still be present"
    498    );
    499  }
    500 
    501  downloadPromise = promiseDownloadRemoved(publicList);
    502  formHistoryPromise = promiseFormHistoryRemoved();
    503 
    504  // Clear 2 hours 10 minutes
    505  range = [now_uSec - 130 * 60 * 1000000, now_uSec];
    506  await Sanitizer.sanitize(itemsToClear, { range, ignoreTimespan: false });
    507 
    508  await formHistoryPromise;
    509  await downloadPromise;
    510 
    511  ok(
    512    !(await PlacesUtils.history.hasVisits("https://2hour10minutes.com")),
    513    "Pretend visit to 2hour10minutes.com should now be deleted"
    514  );
    515  ok(
    516    await PlacesUtils.history.hasVisits("https://4hour.com"),
    517    "Pretend visit to 4hour.com should should still exist"
    518  );
    519  ok(
    520    await PlacesUtils.history.hasVisits("https://4hour10minutes.com"),
    521    "Pretend visit to 4hour10minutes.com should should still exist"
    522  );
    523  if (minutesSinceMidnight > 130) {
    524    ok(
    525      await PlacesUtils.history.hasVisits("https://today.com"),
    526      "Pretend visit to today.com should still exist"
    527    );
    528  }
    529  ok(
    530    await PlacesUtils.history.hasVisits("https://before-today.com"),
    531    "Pretend visit to before-today.com should still exist"
    532  );
    533 
    534  await countEntries(
    535    "2hour10minutes",
    536    "2hour10minutes form entry should be deleted",
    537    checkZero
    538  );
    539  await countEntries("4hour", "4hour form entry should still exist", checkOne);
    540  await countEntries(
    541    "4hour10minutes",
    542    "4hour10minutes form entry should still exist",
    543    checkOne
    544  );
    545  if (minutesSinceMidnight > 130) {
    546    await countEntries(
    547      "today",
    548      "today form entry should still exist",
    549      checkOne
    550    );
    551  }
    552  await countEntries(
    553    "b4today",
    554    "b4today form entry should still exist",
    555    checkOne
    556  );
    557 
    558  ok(
    559    !(await downloadExists(publicList, "fakefile-2-hour-10-minutes")),
    560    "2 hour 10 minute old download should now be deleted"
    561  );
    562  ok(
    563    await downloadExists(publicList, "fakefile-4-hour"),
    564    "<4 hour old download should still be present"
    565  );
    566  ok(
    567    await downloadExists(publicList, "fakefile-4-hour-10-minutes"),
    568    "4 hour 10 minute download should still be present"
    569  );
    570  ok(
    571    await downloadExists(publicList, "fakefile-old"),
    572    "Year old download should still be present"
    573  );
    574  if (minutesSinceMidnight > 130) {
    575    ok(
    576      await downloadExists(publicList, "fakefile-today"),
    577      "'Today' download should still be present"
    578    );
    579  }
    580 
    581  downloadPromise = promiseDownloadRemoved(publicList);
    582  formHistoryPromise = promiseFormHistoryRemoved();
    583 
    584  // Clear 4 hours
    585  Services.prefs.setIntPref(Sanitizer.PREF_TIMESPAN, 3);
    586  await Sanitizer.sanitize(itemsToClear, { ignoreTimespan: false });
    587 
    588  await formHistoryPromise;
    589  await downloadPromise;
    590 
    591  ok(
    592    !(await PlacesUtils.history.hasVisits("https://4hour.com")),
    593    "Pretend visit to 4hour.com should now be deleted"
    594  );
    595  ok(
    596    await PlacesUtils.history.hasVisits("https://4hour10minutes.com"),
    597    "Pretend visit to 4hour10minutes.com should should still exist"
    598  );
    599  if (hoursSinceMidnight > 4) {
    600    ok(
    601      await PlacesUtils.history.hasVisits("https://today.com"),
    602      "Pretend visit to today.com should still exist"
    603    );
    604  }
    605  ok(
    606    await PlacesUtils.history.hasVisits("https://before-today.com"),
    607    "Pretend visit to before-today.com should still exist"
    608  );
    609 
    610  await countEntries("4hour", "4hour form entry should be deleted", checkZero);
    611  await countEntries(
    612    "4hour10minutes",
    613    "4hour10minutes form entry should still exist",
    614    checkOne
    615  );
    616  if (hoursSinceMidnight > 4) {
    617    await countEntries(
    618      "today",
    619      "today form entry should still exist",
    620      checkOne
    621    );
    622  }
    623  await countEntries(
    624    "b4today",
    625    "b4today form entry should still exist",
    626    checkOne
    627  );
    628 
    629  ok(
    630    !(await downloadExists(publicList, "fakefile-4-hour")),
    631    "<4 hour old download should now be deleted"
    632  );
    633  ok(
    634    await downloadExists(publicList, "fakefile-4-hour-10-minutes"),
    635    "4 hour 10 minute download should still be present"
    636  );
    637  ok(
    638    await downloadExists(publicList, "fakefile-old"),
    639    "Year old download should still be present"
    640  );
    641  if (hoursSinceMidnight > 4) {
    642    ok(
    643      await downloadExists(publicList, "fakefile-today"),
    644      "'Today' download should still be present"
    645    );
    646  }
    647 
    648  downloadPromise = promiseDownloadRemoved(publicList);
    649  formHistoryPromise = promiseFormHistoryRemoved();
    650 
    651  // Clear 4 hours 10 minutes
    652  range = [now_uSec - 250 * 60 * 1000000, now_uSec];
    653  await Sanitizer.sanitize(itemsToClear, { range, ignoreTimespan: false });
    654 
    655  await formHistoryPromise;
    656  await downloadPromise;
    657 
    658  ok(
    659    !(await PlacesUtils.history.hasVisits("https://4hour10minutes.com")),
    660    "Pretend visit to 4hour10minutes.com should now be deleted"
    661  );
    662  if (minutesSinceMidnight > 250) {
    663    ok(
    664      await PlacesUtils.history.hasVisits("https://today.com"),
    665      "Pretend visit to today.com should still exist"
    666    );
    667  }
    668  ok(
    669    await PlacesUtils.history.hasVisits("https://before-today.com"),
    670    "Pretend visit to before-today.com should still exist"
    671  );
    672 
    673  await countEntries(
    674    "4hour10minutes",
    675    "4hour10minutes form entry should be deleted",
    676    checkZero
    677  );
    678  if (minutesSinceMidnight > 250) {
    679    await countEntries(
    680      "today",
    681      "today form entry should still exist",
    682      checkOne
    683    );
    684  }
    685  await countEntries(
    686    "b4today",
    687    "b4today form entry should still exist",
    688    checkOne
    689  );
    690 
    691  ok(
    692    !(await downloadExists(publicList, "fakefile-4-hour-10-minutes")),
    693    "4 hour 10 minute download should now be deleted"
    694  );
    695  ok(
    696    await downloadExists(publicList, "fakefile-old"),
    697    "Year old download should still be present"
    698  );
    699  if (minutesSinceMidnight > 250) {
    700    ok(
    701      await downloadExists(publicList, "fakefile-today"),
    702      "'Today' download should still be present"
    703    );
    704  }
    705 
    706  // The 'Today' download might have been already deleted, in which case we
    707  // should not wait for a download removal notification.
    708  if (minutesSinceMidnight > 250) {
    709    downloadPromise = promiseDownloadRemoved(publicList);
    710    formHistoryPromise = promiseFormHistoryRemoved();
    711  } else {
    712    downloadPromise = formHistoryPromise = Promise.resolve();
    713  }
    714 
    715  // Clear Today
    716  Services.prefs.setIntPref(Sanitizer.PREF_TIMESPAN, 4);
    717  let progress = await Sanitizer.sanitize(itemsToClear, {
    718    ignoreTimespan: false,
    719  });
    720  Assert.deepEqual(progress, {
    721    historyAndFormData: "cleared",
    722    downloads: "cleared",
    723  });
    724 
    725  await formHistoryPromise;
    726  await downloadPromise;
    727 
    728  // Be careful.  If we add our objectss just before midnight, and sanitize
    729  // runs immediately after, they won't be expired.  This is expected, but
    730  // we should not test in that case.  We cannot just test for opposite
    731  // condition because we could cross midnight just one moment after we
    732  // cache our time, then we would have an even worse random failure.
    733  var today = isToday(new Date(now_mSec));
    734  if (today) {
    735    ok(
    736      !(await PlacesUtils.history.hasVisits("https://today.com")),
    737      "Pretend visit to today.com should now be deleted"
    738    );
    739 
    740    await countEntries(
    741      "today",
    742      "today form entry should be deleted",
    743      checkZero
    744    );
    745    ok(
    746      !(await downloadExists(publicList, "fakefile-today")),
    747      "'Today' download should now be deleted"
    748    );
    749  }
    750 
    751  ok(
    752    await PlacesUtils.history.hasVisits("https://before-today.com"),
    753    "Pretend visit to before-today.com should still exist"
    754  );
    755  await countEntries(
    756    "b4today",
    757    "b4today form entry should still exist",
    758    checkOne
    759  );
    760  ok(
    761    await downloadExists(publicList, "fakefile-old"),
    762    "Year old download should still be present"
    763  );
    764 
    765  downloadPromise = promiseDownloadRemoved(publicList);
    766  formHistoryPromise = promiseFormHistoryRemoved();
    767 
    768  // Choose everything
    769  Services.prefs.setIntPref(Sanitizer.PREF_TIMESPAN, 0);
    770  await Sanitizer.sanitize(itemsToClear, { ignoreTimespan: false });
    771 
    772  await formHistoryPromise;
    773  await downloadPromise;
    774 
    775  ok(
    776    !(await PlacesUtils.history.hasVisits("https://before-today.com")),
    777    "Pretend visit to before-today.com should now be deleted"
    778  );
    779 
    780  await countEntries(
    781    "b4today",
    782    "b4today form entry should be deleted",
    783    checkZero
    784  );
    785 
    786  ok(
    787    !(await downloadExists(publicList, "fakefile-old")),
    788    "Year old download should now be deleted"
    789  );
    790 }
    791 
    792 async function setupHistory() {
    793  let places = [];
    794 
    795  function addPlace(aURI, aTitle, aVisitDate) {
    796    places.push({
    797      uri: aURI,
    798      title: aTitle,
    799      visitDate: aVisitDate,
    800      transition: Ci.nsINavHistoryService.TRANSITION_LINK,
    801    });
    802  }
    803 
    804  addPlace(
    805    "https://10minutes.com/",
    806    "10 minutes ago",
    807    now_uSec - 10 * kUsecPerMin
    808  );
    809  addPlace(
    810    "https://1hour.com/",
    811    "Less than 1 hour ago",
    812    now_uSec - 45 * kUsecPerMin
    813  );
    814  addPlace(
    815    "https://1hour10minutes.com/",
    816    "1 hour 10 minutes ago",
    817    now_uSec - 70 * kUsecPerMin
    818  );
    819  addPlace(
    820    "https://2hour.com/",
    821    "Less than 2 hours ago",
    822    now_uSec - 90 * kUsecPerMin
    823  );
    824  addPlace(
    825    "https://2hour10minutes.com/",
    826    "2 hours 10 minutes ago",
    827    now_uSec - 130 * kUsecPerMin
    828  );
    829  addPlace(
    830    "https://4hour.com/",
    831    "Less than 4 hours ago",
    832    now_uSec - 180 * kUsecPerMin
    833  );
    834  addPlace(
    835    "https://4hour10minutes.com/",
    836    "4 hours 10 minutesago",
    837    now_uSec - 250 * kUsecPerMin
    838  );
    839 
    840  let today = new Date();
    841  today.setHours(0);
    842  today.setMinutes(0);
    843  today.setSeconds(0);
    844  today.setMilliseconds(1);
    845  addPlace("https://today.com/", "Today", today.getTime() * 1000);
    846 
    847  let lastYear = new Date();
    848  lastYear.setFullYear(lastYear.getFullYear() - 1);
    849  addPlace(
    850    "https://before-today.com/",
    851    "Before Today",
    852    lastYear.getTime() * 1000
    853  );
    854  await PlacesTestUtils.addVisits(places);
    855 }
    856 
    857 async function setupFormHistory() {
    858  function searchEntries(terms, params) {
    859    return FormHistory.search(terms, params);
    860  }
    861 
    862  // Make sure we've got a clean DB to start with, then add the entries we'll be testing.
    863  await FormHistory.update([
    864    {
    865      op: "remove",
    866    },
    867    {
    868      op: "add",
    869      fieldname: "10minutes",
    870      value: "10m",
    871    },
    872    {
    873      op: "add",
    874      fieldname: "1hour",
    875      value: "1h",
    876    },
    877    {
    878      op: "add",
    879      fieldname: "1hour10minutes",
    880      value: "1h10m",
    881    },
    882    {
    883      op: "add",
    884      fieldname: "2hour",
    885      value: "2h",
    886    },
    887    {
    888      op: "add",
    889      fieldname: "2hour10minutes",
    890      value: "2h10m",
    891    },
    892    {
    893      op: "add",
    894      fieldname: "4hour",
    895      value: "4h",
    896    },
    897    {
    898      op: "add",
    899      fieldname: "4hour10minutes",
    900      value: "4h10m",
    901    },
    902    {
    903      op: "add",
    904      fieldname: "today",
    905      value: "1d",
    906    },
    907    {
    908      op: "add",
    909      fieldname: "b4today",
    910      value: "1y",
    911    },
    912  ]);
    913 
    914  // Artifically age the entries to the proper vintage.
    915  let timestamp = now_uSec - 10 * kUsecPerMin;
    916  let results = await searchEntries(["guid"], { fieldname: "10minutes" });
    917  await FormHistory.update({
    918    op: "update",
    919    firstUsed: timestamp,
    920    guid: results[0].guid,
    921  });
    922 
    923  timestamp = now_uSec - 45 * kUsecPerMin;
    924  results = await searchEntries(["guid"], { fieldname: "1hour" });
    925  await FormHistory.update({
    926    op: "update",
    927    firstUsed: timestamp,
    928    guid: results[0].guid,
    929  });
    930 
    931  timestamp = now_uSec - 70 * kUsecPerMin;
    932  results = await searchEntries(["guid"], { fieldname: "1hour10minutes" });
    933  await FormHistory.update({
    934    op: "update",
    935    firstUsed: timestamp,
    936    guid: results[0].guid,
    937  });
    938 
    939  timestamp = now_uSec - 90 * kUsecPerMin;
    940  results = await searchEntries(["guid"], { fieldname: "2hour" });
    941  await FormHistory.update({
    942    op: "update",
    943    firstUsed: timestamp,
    944    guid: results[0].guid,
    945  });
    946 
    947  timestamp = now_uSec - 130 * kUsecPerMin;
    948  results = await searchEntries(["guid"], { fieldname: "2hour10minutes" });
    949  await FormHistory.update({
    950    op: "update",
    951    firstUsed: timestamp,
    952    guid: results[0].guid,
    953  });
    954 
    955  timestamp = now_uSec - 180 * kUsecPerMin;
    956  results = await searchEntries(["guid"], { fieldname: "4hour" });
    957  await FormHistory.update({
    958    op: "update",
    959    firstUsed: timestamp,
    960    guid: results[0].guid,
    961  });
    962 
    963  timestamp = now_uSec - 250 * kUsecPerMin;
    964  results = await searchEntries(["guid"], { fieldname: "4hour10minutes" });
    965  await FormHistory.update({
    966    op: "update",
    967    firstUsed: timestamp,
    968    guid: results[0].guid,
    969  });
    970 
    971  let today = new Date();
    972  today.setHours(0);
    973  today.setMinutes(0);
    974  today.setSeconds(0);
    975  today.setMilliseconds(1);
    976  timestamp = today.getTime() * 1000;
    977  results = await searchEntries(["guid"], { fieldname: "today" });
    978  await FormHistory.update({
    979    op: "update",
    980    firstUsed: timestamp,
    981    guid: results[0].guid,
    982  });
    983 
    984  let lastYear = new Date();
    985  lastYear.setFullYear(lastYear.getFullYear() - 1);
    986  timestamp = lastYear.getTime() * 1000;
    987  results = await searchEntries(["guid"], { fieldname: "b4today" });
    988  await FormHistory.update({
    989    op: "update",
    990    firstUsed: timestamp,
    991    guid: results[0].guid,
    992  });
    993 
    994  var checks = 0;
    995  let checkOne = function (num, message) {
    996    is(num, 1, message);
    997    checks++;
    998  };
    999 
   1000  // Sanity check.
   1001  await countEntries(
   1002    "10minutes",
   1003    "Checking for 10minutes form history entry creation",
   1004    checkOne
   1005  );
   1006  await countEntries(
   1007    "1hour",
   1008    "Checking for 1hour form history entry creation",
   1009    checkOne
   1010  );
   1011  await countEntries(
   1012    "1hour10minutes",
   1013    "Checking for 1hour10minutes form history entry creation",
   1014    checkOne
   1015  );
   1016  await countEntries(
   1017    "2hour",
   1018    "Checking for 2hour form history entry creation",
   1019    checkOne
   1020  );
   1021  await countEntries(
   1022    "2hour10minutes",
   1023    "Checking for 2hour10minutes form history entry creation",
   1024    checkOne
   1025  );
   1026  await countEntries(
   1027    "4hour",
   1028    "Checking for 4hour form history entry creation",
   1029    checkOne
   1030  );
   1031  await countEntries(
   1032    "4hour10minutes",
   1033    "Checking for 4hour10minutes form history entry creation",
   1034    checkOne
   1035  );
   1036  await countEntries(
   1037    "today",
   1038    "Checking for today form history entry creation",
   1039    checkOne
   1040  );
   1041  await countEntries(
   1042    "b4today",
   1043    "Checking for b4today form history entry creation",
   1044    checkOne
   1045  );
   1046  is(checks, 9, "9 checks made");
   1047 }
   1048 
   1049 async function setupDownloads() {
   1050  let publicList = await Downloads.getList(Downloads.PUBLIC);
   1051 
   1052  let download = await Downloads.createDownload({
   1053    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
   1054    target: "fakefile-10-minutes",
   1055  });
   1056  download.startTime = new Date(now_mSec - 10 * kMsecPerMin); // 10 minutes ago
   1057  download.canceled = true;
   1058  await publicList.add(download);
   1059 
   1060  download = await Downloads.createDownload({
   1061    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
   1062    target: "fakefile-1-hour",
   1063  });
   1064  download.startTime = new Date(now_mSec - 45 * kMsecPerMin); // 45 minutes ago
   1065  download.canceled = true;
   1066  await publicList.add(download);
   1067 
   1068  download = await Downloads.createDownload({
   1069    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
   1070    target: "fakefile-1-hour-10-minutes",
   1071  });
   1072  download.startTime = new Date(now_mSec - 70 * kMsecPerMin); // 70 minutes ago
   1073  download.canceled = true;
   1074  await publicList.add(download);
   1075 
   1076  download = await Downloads.createDownload({
   1077    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
   1078    target: "fakefile-2-hour",
   1079  });
   1080  download.startTime = new Date(now_mSec - 90 * kMsecPerMin); // 90 minutes ago
   1081  download.canceled = true;
   1082  await publicList.add(download);
   1083 
   1084  download = await Downloads.createDownload({
   1085    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
   1086    target: "fakefile-2-hour-10-minutes",
   1087  });
   1088  download.startTime = new Date(now_mSec - 130 * kMsecPerMin); // 130 minutes ago
   1089  download.canceled = true;
   1090  await publicList.add(download);
   1091 
   1092  download = await Downloads.createDownload({
   1093    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
   1094    target: "fakefile-4-hour",
   1095  });
   1096  download.startTime = new Date(now_mSec - 180 * kMsecPerMin); // 180 minutes ago
   1097  download.canceled = true;
   1098  await publicList.add(download);
   1099 
   1100  download = await Downloads.createDownload({
   1101    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
   1102    target: "fakefile-4-hour-10-minutes",
   1103  });
   1104  download.startTime = new Date(now_mSec - 250 * kMsecPerMin); // 250 minutes ago
   1105  download.canceled = true;
   1106  await publicList.add(download);
   1107 
   1108  // Add "today" download
   1109  let today = new Date();
   1110  today.setHours(0);
   1111  today.setMinutes(0);
   1112  today.setSeconds(0);
   1113  today.setMilliseconds(1);
   1114 
   1115  download = await Downloads.createDownload({
   1116    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
   1117    target: "fakefile-today",
   1118  });
   1119  download.startTime = today; // 12:00:01 AM this morning
   1120  download.canceled = true;
   1121  await publicList.add(download);
   1122 
   1123  // Add "before today" download
   1124  let lastYear = new Date();
   1125  lastYear.setFullYear(lastYear.getFullYear() - 1);
   1126 
   1127  download = await Downloads.createDownload({
   1128    source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
   1129    target: "fakefile-old",
   1130  });
   1131  download.startTime = lastYear;
   1132  download.canceled = true;
   1133  await publicList.add(download);
   1134 
   1135  // Confirm everything worked
   1136  let downloads = await publicList.getAll();
   1137  is(downloads.length, 9, "9 Pretend downloads added");
   1138 
   1139  ok(
   1140    await downloadExists(publicList, "fakefile-old"),
   1141    "Pretend download for everything case should exist"
   1142  );
   1143  ok(
   1144    await downloadExists(publicList, "fakefile-10-minutes"),
   1145    "Pretend download for 10-minutes case should exist"
   1146  );
   1147  ok(
   1148    await downloadExists(publicList, "fakefile-1-hour"),
   1149    "Pretend download for 1-hour case should exist"
   1150  );
   1151  ok(
   1152    await downloadExists(publicList, "fakefile-1-hour-10-minutes"),
   1153    "Pretend download for 1-hour-10-minutes case should exist"
   1154  );
   1155  ok(
   1156    await downloadExists(publicList, "fakefile-2-hour"),
   1157    "Pretend download for 2-hour case should exist"
   1158  );
   1159  ok(
   1160    await downloadExists(publicList, "fakefile-2-hour-10-minutes"),
   1161    "Pretend download for 2-hour-10-minutes case should exist"
   1162  );
   1163  ok(
   1164    await downloadExists(publicList, "fakefile-4-hour"),
   1165    "Pretend download for 4-hour case should exist"
   1166  );
   1167  ok(
   1168    await downloadExists(publicList, "fakefile-4-hour-10-minutes"),
   1169    "Pretend download for 4-hour-10-minutes case should exist"
   1170  );
   1171  ok(
   1172    await downloadExists(publicList, "fakefile-today"),
   1173    "Pretend download for Today case should exist"
   1174  );
   1175 }
   1176 
   1177 /**
   1178 * Checks to see if the downloads with the specified id exists.
   1179 *
   1180 * @param aID
   1181 *        The ids of the downloads to check.
   1182 */
   1183 let downloadExists = async function (list, path) {
   1184  let listArray = await list.getAll();
   1185  return listArray.some(i => i.target.path == path);
   1186 };
   1187 
   1188 function isToday(aDate) {
   1189  return aDate.getDate() == new Date().getDate();
   1190 }