Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
2 Cu.import("resource://gre/modules/Services.jsm");
4 // Bug 453440 - Test the timespan-based logic of the sanitizer code
5 let now_mSec = Date.now();
6 let now_uSec = now_mSec * 1000;
8 const kMsecPerMin = 60 * 1000;
9 const kUsecPerMin = 60 * 1000000;
11 let tempScope = {};
12 Cc["@mozilla.org/moz/jssubscript-loader;1"].getService(Ci.mozIJSSubScriptLoader)
13 .loadSubScript("chrome://browser/content/sanitize.js", tempScope);
14 let Sanitizer = tempScope.Sanitizer;
16 let FormHistory = (Components.utils.import("resource://gre/modules/FormHistory.jsm", {})).FormHistory;
17 let Downloads = (Components.utils.import("resource://gre/modules/Downloads.jsm", {})).Downloads;
19 function promiseFormHistoryRemoved() {
20 let deferred = Promise.defer();
21 Services.obs.addObserver(function onfh() {
22 Services.obs.removeObserver(onfh, "satchel-storage-changed", false);
23 deferred.resolve();
24 }, "satchel-storage-changed", false);
25 return deferred.promise;
26 }
28 function promiseDownloadRemoved(list) {
29 let deferred = Promise.defer();
31 let view = {
32 onDownloadRemoved: function(download) {
33 list.removeView(view);
34 deferred.resolve();
35 }
36 };
38 list.addView(view);
40 return deferred.promise;
41 }
43 function test() {
44 waitForExplicitFinish();
46 Task.spawn(function() {
47 yield setupDownloads();
48 yield setupFormHistory();
49 yield setupHistory();
50 yield onHistoryReady();
51 }).then(null, ex => ok(false, ex)).then(finish);
52 }
54 function countEntries(name, message, check) {
55 let deferred = Promise.defer();
57 var obj = {};
58 if (name !== null)
59 obj.fieldname = name;
61 let count;
62 FormHistory.count(obj, { handleResult: function (result) count = result,
63 handleError: function (error) {
64 do_throw("Error occurred searching form history: " + error);
65 deferred.reject(error)
66 },
67 handleCompletion: function (reason) {
68 if (!reason) {
69 check(count, message);
70 deferred.resolve();
71 }
72 },
73 });
75 return deferred.promise;
76 }
78 function onHistoryReady() {
79 var hoursSinceMidnight = new Date().getHours();
80 var minutesSinceMidnight = hoursSinceMidnight * 60 + new Date().getMinutes();
82 // Should test cookies here, but nsICookieManager/nsICookieService
83 // doesn't let us fake creation times. bug 463127
85 let s = new Sanitizer();
86 s.ignoreTimespan = false;
87 s.prefDomain = "privacy.cpd.";
88 var itemPrefs = gPrefService.getBranch(s.prefDomain);
89 itemPrefs.setBoolPref("history", true);
90 itemPrefs.setBoolPref("downloads", true);
91 itemPrefs.setBoolPref("cache", false);
92 itemPrefs.setBoolPref("cookies", false);
93 itemPrefs.setBoolPref("formdata", true);
94 itemPrefs.setBoolPref("offlineApps", false);
95 itemPrefs.setBoolPref("passwords", false);
96 itemPrefs.setBoolPref("sessions", false);
97 itemPrefs.setBoolPref("siteSettings", false);
99 let publicList = yield Downloads.getList(Downloads.PUBLIC);
100 let downloadPromise = promiseDownloadRemoved(publicList);
102 // Clear 10 minutes ago
103 s.range = [now_uSec - 10*60*1000000, now_uSec];
104 s.sanitize();
105 s.range = null;
107 yield promiseFormHistoryRemoved();
108 yield downloadPromise;
110 ok(!(yield promiseIsURIVisited(makeURI("http://10minutes.com"))),
111 "Pretend visit to 10minutes.com should now be deleted");
112 ok((yield promiseIsURIVisited(makeURI("http://1hour.com"))),
113 "Pretend visit to 1hour.com should should still exist");
114 ok((yield promiseIsURIVisited(makeURI("http://1hour10minutes.com"))),
115 "Pretend visit to 1hour10minutes.com should should still exist");
116 ok((yield promiseIsURIVisited(makeURI("http://2hour.com"))),
117 "Pretend visit to 2hour.com should should still exist");
118 ok((yield promiseIsURIVisited(makeURI("http://2hour10minutes.com"))),
119 "Pretend visit to 2hour10minutes.com should should still exist");
120 ok((yield promiseIsURIVisited(makeURI("http://4hour.com"))),
121 "Pretend visit to 4hour.com should should still exist");
122 ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))),
123 "Pretend visit to 4hour10minutes.com should should still exist");
124 if (minutesSinceMidnight > 10) {
125 ok((yield promiseIsURIVisited(makeURI("http://today.com"))),
126 "Pretend visit to today.com should still exist");
127 }
128 ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))),
129 "Pretend visit to before-today.com should still exist");
131 let checkZero = function(num, message) { is(num, 0, message); }
132 let checkOne = function(num, message) { is(num, 1, message); }
134 yield countEntries("10minutes", "10minutes form entry should be deleted", checkZero);
135 yield countEntries("1hour", "1hour form entry should still exist", checkOne);
136 yield countEntries("1hour10minutes", "1hour10minutes form entry should still exist", checkOne);
137 yield countEntries("2hour", "2hour form entry should still exist", checkOne);
138 yield countEntries("2hour10minutes", "2hour10minutes form entry should still exist", checkOne);
139 yield countEntries("4hour", "4hour form entry should still exist", checkOne);
140 yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne);
141 if (minutesSinceMidnight > 10)
142 yield countEntries("today", "today form entry should still exist", checkOne);
143 yield countEntries("b4today", "b4today form entry should still exist", checkOne);
145 ok(!(yield downloadExists(publicList, "fakefile-10-minutes")), "10 minute download should now be deleted");
146 ok((yield downloadExists(publicList, "fakefile-1-hour")), "<1 hour download should still be present");
147 ok((yield downloadExists(publicList, "fakefile-1-hour-10-minutes")), "1 hour 10 minute download should still be present");
148 ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present");
149 ok((yield downloadExists(publicList, "fakefile-2-hour")), "<2 hour old download should still be present");
150 ok((yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "2 hour 10 minute download should still be present");
151 ok((yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should still be present");
152 ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present");
154 if (minutesSinceMidnight > 10)
155 ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present");
157 downloadPromise = promiseDownloadRemoved(publicList);
159 // Clear 1 hour
160 Sanitizer.prefs.setIntPref("timeSpan", 1);
161 s.sanitize();
163 yield promiseFormHistoryRemoved();
164 yield downloadPromise;
166 ok(!(yield promiseIsURIVisited(makeURI("http://1hour.com"))),
167 "Pretend visit to 1hour.com should now be deleted");
168 ok((yield promiseIsURIVisited(makeURI("http://1hour10minutes.com"))),
169 "Pretend visit to 1hour10minutes.com should should still exist");
170 ok((yield promiseIsURIVisited(makeURI("http://2hour.com"))),
171 "Pretend visit to 2hour.com should should still exist");
172 ok((yield promiseIsURIVisited(makeURI("http://2hour10minutes.com"))),
173 "Pretend visit to 2hour10minutes.com should should still exist");
174 ok((yield promiseIsURIVisited(makeURI("http://4hour.com"))),
175 "Pretend visit to 4hour.com should should still exist");
176 ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))),
177 "Pretend visit to 4hour10minutes.com should should still exist");
178 if (hoursSinceMidnight > 1) {
179 ok((yield promiseIsURIVisited(makeURI("http://today.com"))),
180 "Pretend visit to today.com should still exist");
181 }
182 ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))),
183 "Pretend visit to before-today.com should still exist");
185 yield countEntries("1hour", "1hour form entry should be deleted", checkZero);
186 yield countEntries("1hour10minutes", "1hour10minutes form entry should still exist", checkOne);
187 yield countEntries("2hour", "2hour form entry should still exist", checkOne);
188 yield countEntries("2hour10minutes", "2hour10minutes form entry should still exist", checkOne);
189 yield countEntries("4hour", "4hour form entry should still exist", checkOne);
190 yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne);
191 if (hoursSinceMidnight > 1)
192 yield countEntries("today", "today form entry should still exist", checkOne);
193 yield countEntries("b4today", "b4today form entry should still exist", checkOne);
195 ok(!(yield downloadExists(publicList, "fakefile-1-hour")), "<1 hour download should now be deleted");
196 ok((yield downloadExists(publicList, "fakefile-1-hour-10-minutes")), "1 hour 10 minute download should still be present");
197 ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present");
198 ok((yield downloadExists(publicList, "fakefile-2-hour")), "<2 hour old download should still be present");
199 ok((yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "2 hour 10 minute download should still be present");
200 ok((yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should still be present");
201 ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present");
203 if (hoursSinceMidnight > 1)
204 ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present");
206 downloadPromise = promiseDownloadRemoved(publicList);
208 // Clear 1 hour 10 minutes
209 s.range = [now_uSec - 70*60*1000000, now_uSec];
210 s.sanitize();
211 s.range = null;
213 yield promiseFormHistoryRemoved();
214 yield downloadPromise;
216 ok(!(yield promiseIsURIVisited(makeURI("http://1hour10minutes.com"))),
217 "Pretend visit to 1hour10minutes.com should now be deleted");
218 ok((yield promiseIsURIVisited(makeURI("http://2hour.com"))),
219 "Pretend visit to 2hour.com should should still exist");
220 ok((yield promiseIsURIVisited(makeURI("http://2hour10minutes.com"))),
221 "Pretend visit to 2hour10minutes.com should should still exist");
222 ok((yield promiseIsURIVisited(makeURI("http://4hour.com"))),
223 "Pretend visit to 4hour.com should should still exist");
224 ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))),
225 "Pretend visit to 4hour10minutes.com should should still exist");
226 if (minutesSinceMidnight > 70) {
227 ok((yield promiseIsURIVisited(makeURI("http://today.com"))),
228 "Pretend visit to today.com should still exist");
229 }
230 ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))),
231 "Pretend visit to before-today.com should still exist");
233 yield countEntries("1hour10minutes", "1hour10minutes form entry should be deleted", checkZero);
234 yield countEntries("2hour", "2hour form entry should still exist", checkOne);
235 yield countEntries("2hour10minutes", "2hour10minutes form entry should still exist", checkOne);
236 yield countEntries("4hour", "4hour form entry should still exist", checkOne);
237 yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne);
238 if (minutesSinceMidnight > 70)
239 yield countEntries("today", "today form entry should still exist", checkOne);
240 yield countEntries("b4today", "b4today form entry should still exist", checkOne);
242 ok(!(yield downloadExists(publicList, "fakefile-1-hour-10-minutes")), "1 hour 10 minute old download should now be deleted");
243 ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present");
244 ok((yield downloadExists(publicList, "fakefile-2-hour")), "<2 hour old download should still be present");
245 ok((yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "2 hour 10 minute download should still be present");
246 ok((yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should still be present");
247 ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present");
248 if (minutesSinceMidnight > 70)
249 ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present");
251 downloadPromise = promiseDownloadRemoved(publicList);
253 // Clear 2 hours
254 Sanitizer.prefs.setIntPref("timeSpan", 2);
255 s.sanitize();
257 yield promiseFormHistoryRemoved();
258 yield downloadPromise;
260 ok(!(yield promiseIsURIVisited(makeURI("http://2hour.com"))),
261 "Pretend visit to 2hour.com should now be deleted");
262 ok((yield promiseIsURIVisited(makeURI("http://2hour10minutes.com"))),
263 "Pretend visit to 2hour10minutes.com should should still exist");
264 ok((yield promiseIsURIVisited(makeURI("http://4hour.com"))),
265 "Pretend visit to 4hour.com should should still exist");
266 ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))),
267 "Pretend visit to 4hour10minutes.com should should still exist");
268 if (hoursSinceMidnight > 2) {
269 ok((yield promiseIsURIVisited(makeURI("http://today.com"))),
270 "Pretend visit to today.com should still exist");
271 }
272 ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))),
273 "Pretend visit to before-today.com should still exist");
275 yield countEntries("2hour", "2hour form entry should be deleted", checkZero);
276 yield countEntries("2hour10minutes", "2hour10minutes form entry should still exist", checkOne);
277 yield countEntries("4hour", "4hour form entry should still exist", checkOne);
278 yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne);
279 if (hoursSinceMidnight > 2)
280 yield countEntries("today", "today form entry should still exist", checkOne);
281 yield countEntries("b4today", "b4today form entry should still exist", checkOne);
283 ok(!(yield downloadExists(publicList, "fakefile-2-hour")), "<2 hour old download should now be deleted");
284 ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present");
285 ok((yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "2 hour 10 minute download should still be present");
286 ok((yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should still be present");
287 ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present");
288 if (hoursSinceMidnight > 2)
289 ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present");
291 downloadPromise = promiseDownloadRemoved(publicList);
293 // Clear 2 hours 10 minutes
294 s.range = [now_uSec - 130*60*1000000, now_uSec];
295 s.sanitize();
296 s.range = null;
298 yield promiseFormHistoryRemoved();
299 yield downloadPromise;
301 ok(!(yield promiseIsURIVisited(makeURI("http://2hour10minutes.com"))),
302 "Pretend visit to 2hour10minutes.com should now be deleted");
303 ok((yield promiseIsURIVisited(makeURI("http://4hour.com"))),
304 "Pretend visit to 4hour.com should should still exist");
305 ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))),
306 "Pretend visit to 4hour10minutes.com should should still exist");
307 if (minutesSinceMidnight > 130) {
308 ok((yield promiseIsURIVisited(makeURI("http://today.com"))),
309 "Pretend visit to today.com should still exist");
310 }
311 ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))),
312 "Pretend visit to before-today.com should still exist");
314 yield countEntries("2hour10minutes", "2hour10minutes form entry should be deleted", checkZero);
315 yield countEntries("4hour", "4hour form entry should still exist", checkOne);
316 yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne);
317 if (minutesSinceMidnight > 130)
318 yield countEntries("today", "today form entry should still exist", checkOne);
319 yield countEntries("b4today", "b4today form entry should still exist", checkOne);
321 ok(!(yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "2 hour 10 minute old download should now be deleted");
322 ok((yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should still be present");
323 ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present");
324 ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present");
325 if (minutesSinceMidnight > 130)
326 ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present");
328 downloadPromise = promiseDownloadRemoved(publicList);
330 // Clear 4 hours
331 Sanitizer.prefs.setIntPref("timeSpan", 3);
332 s.sanitize();
334 yield promiseFormHistoryRemoved();
335 yield downloadPromise;
337 ok(!(yield promiseIsURIVisited(makeURI("http://4hour.com"))),
338 "Pretend visit to 4hour.com should now be deleted");
339 ok((yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))),
340 "Pretend visit to 4hour10minutes.com should should still exist");
341 if (hoursSinceMidnight > 4) {
342 ok((yield promiseIsURIVisited(makeURI("http://today.com"))),
343 "Pretend visit to today.com should still exist");
344 }
345 ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))),
346 "Pretend visit to before-today.com should still exist");
348 yield countEntries("4hour", "4hour form entry should be deleted", checkZero);
349 yield countEntries("4hour10minutes", "4hour10minutes form entry should still exist", checkOne);
350 if (hoursSinceMidnight > 4)
351 yield countEntries("today", "today form entry should still exist", checkOne);
352 yield countEntries("b4today", "b4today form entry should still exist", checkOne);
354 ok(!(yield downloadExists(publicList, "fakefile-4-hour")), "<4 hour old download should now be deleted");
355 ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should still be present");
356 ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present");
357 if (hoursSinceMidnight > 4)
358 ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present");
360 downloadPromise = promiseDownloadRemoved(publicList);
362 // Clear 4 hours 10 minutes
363 s.range = [now_uSec - 250*60*1000000, now_uSec];
364 s.sanitize();
365 s.range = null;
367 yield promiseFormHistoryRemoved();
368 yield downloadPromise;
370 ok(!(yield promiseIsURIVisited(makeURI("http://4hour10minutes.com"))),
371 "Pretend visit to 4hour10minutes.com should now be deleted");
372 if (minutesSinceMidnight > 250) {
373 ok((yield promiseIsURIVisited(makeURI("http://today.com"))),
374 "Pretend visit to today.com should still exist");
375 }
376 ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))),
377 "Pretend visit to before-today.com should still exist");
379 yield countEntries("4hour10minutes", "4hour10minutes form entry should be deleted", checkZero);
380 if (minutesSinceMidnight > 250)
381 yield countEntries("today", "today form entry should still exist", checkOne);
382 yield countEntries("b4today", "b4today form entry should still exist", checkOne);
384 ok(!(yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "4 hour 10 minute download should now be deleted");
385 ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present");
386 if (minutesSinceMidnight > 250)
387 ok((yield downloadExists(publicList, "fakefile-today")), "'Today' download should still be present");
389 // The 'Today' download might have been already deleted, in which case we
390 // should not wait for a download removal notification.
391 if (minutesSinceMidnight > 250) {
392 downloadPromise = promiseDownloadRemoved(publicList);
393 } else {
394 downloadPromise = Promise.resolve();
395 }
397 // Clear Today
398 Sanitizer.prefs.setIntPref("timeSpan", 4);
399 s.sanitize();
401 yield promiseFormHistoryRemoved();
402 yield downloadPromise;
404 // Be careful. If we add our objectss just before midnight, and sanitize
405 // runs immediately after, they won't be expired. This is expected, but
406 // we should not test in that case. We cannot just test for opposite
407 // condition because we could cross midnight just one moment after we
408 // cache our time, then we would have an even worse random failure.
409 var today = isToday(new Date(now_mSec));
410 if (today) {
411 ok(!(yield promiseIsURIVisited(makeURI("http://today.com"))),
412 "Pretend visit to today.com should now be deleted");
414 yield countEntries("today", "today form entry should be deleted", checkZero);
415 ok(!(yield downloadExists(publicList, "fakefile-today")), "'Today' download should now be deleted");
416 }
418 ok((yield promiseIsURIVisited(makeURI("http://before-today.com"))),
419 "Pretend visit to before-today.com should still exist");
420 yield countEntries("b4today", "b4today form entry should still exist", checkOne);
421 ok((yield downloadExists(publicList, "fakefile-old")), "Year old download should still be present");
423 downloadPromise = promiseDownloadRemoved(publicList);
425 // Choose everything
426 Sanitizer.prefs.setIntPref("timeSpan", 0);
427 s.sanitize();
429 yield promiseFormHistoryRemoved();
430 yield downloadPromise;
432 ok(!(yield promiseIsURIVisited(makeURI("http://before-today.com"))),
433 "Pretend visit to before-today.com should now be deleted");
435 yield countEntries("b4today", "b4today form entry should be deleted", checkZero);
437 ok(!(yield downloadExists(publicList, "fakefile-old")), "Year old download should now be deleted");
438 }
440 function setupHistory() {
441 let deferred = Promise.defer();
443 let places = [];
445 function addPlace(aURI, aTitle, aVisitDate) {
446 places.push({
447 uri: aURI,
448 title: aTitle,
449 visits: [{
450 visitDate: aVisitDate,
451 transitionType: Ci.nsINavHistoryService.TRANSITION_LINK
452 }]
453 });
454 }
456 addPlace(makeURI("http://10minutes.com/"), "10 minutes ago", now_uSec - 10 * kUsecPerMin);
457 addPlace(makeURI("http://1hour.com/"), "Less than 1 hour ago", now_uSec - 45 * kUsecPerMin);
458 addPlace(makeURI("http://1hour10minutes.com/"), "1 hour 10 minutes ago", now_uSec - 70 * kUsecPerMin);
459 addPlace(makeURI("http://2hour.com/"), "Less than 2 hours ago", now_uSec - 90 * kUsecPerMin);
460 addPlace(makeURI("http://2hour10minutes.com/"), "2 hours 10 minutes ago", now_uSec - 130 * kUsecPerMin);
461 addPlace(makeURI("http://4hour.com/"), "Less than 4 hours ago", now_uSec - 180 * kUsecPerMin);
462 addPlace(makeURI("http://4hour10minutes.com/"), "4 hours 10 minutesago", now_uSec - 250 * kUsecPerMin);
464 let today = new Date();
465 today.setHours(0);
466 today.setMinutes(0);
467 today.setSeconds(1);
468 addPlace(makeURI("http://today.com/"), "Today", today.getTime() * 1000);
470 let lastYear = new Date();
471 lastYear.setFullYear(lastYear.getFullYear() - 1);
472 addPlace(makeURI("http://before-today.com/"), "Before Today", lastYear.getTime() * 1000);
474 PlacesUtils.asyncHistory.updatePlaces(places, {
475 handleError: function () ok(false, "Unexpected error in adding visit."),
476 handleResult: function () { },
477 handleCompletion: function () deferred.resolve()
478 });
480 return deferred.promise;
481 }
483 function setupFormHistory() {
485 function searchEntries(terms, params) {
486 let deferred = Promise.defer();
488 let results = [];
489 FormHistory.search(terms, params, { handleResult: function (result) results.push(result),
490 handleError: function (error) {
491 do_throw("Error occurred searching form history: " + error);
492 deferred.reject(error);
493 },
494 handleCompletion: function (reason) { deferred.resolve(results); }
495 });
496 return deferred.promise;
497 }
499 function update(changes)
500 {
501 let deferred = Promise.defer();
502 FormHistory.update(changes, { handleError: function (error) {
503 do_throw("Error occurred searching form history: " + error);
504 deferred.reject(error);
505 },
506 handleCompletion: function (reason) { deferred.resolve(); }
507 });
508 return deferred.promise;
509 }
511 // Make sure we've got a clean DB to start with, then add the entries we'll be testing.
512 yield update(
513 [{
514 op: "remove"
515 },
516 {
517 op : "add",
518 fieldname : "10minutes",
519 value : "10m"
520 }, {
521 op : "add",
522 fieldname : "1hour",
523 value : "1h"
524 }, {
525 op : "add",
526 fieldname : "1hour10minutes",
527 value : "1h10m"
528 }, {
529 op : "add",
530 fieldname : "2hour",
531 value : "2h"
532 }, {
533 op : "add",
534 fieldname : "2hour10minutes",
535 value : "2h10m"
536 }, {
537 op : "add",
538 fieldname : "4hour",
539 value : "4h"
540 }, {
541 op : "add",
542 fieldname : "4hour10minutes",
543 value : "4h10m"
544 }, {
545 op : "add",
546 fieldname : "today",
547 value : "1d"
548 }, {
549 op : "add",
550 fieldname : "b4today",
551 value : "1y"
552 }]);
554 // Artifically age the entries to the proper vintage.
555 let timestamp = now_uSec - 10 * kUsecPerMin;
556 let results = yield searchEntries(["guid"], { fieldname: "10minutes" });
557 yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
559 timestamp = now_uSec - 45 * kUsecPerMin;
560 results = yield searchEntries(["guid"], { fieldname: "1hour" });
561 yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
563 timestamp = now_uSec - 70 * kUsecPerMin;
564 results = yield searchEntries(["guid"], { fieldname: "1hour10minutes" });
565 yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
567 timestamp = now_uSec - 90 * kUsecPerMin;
568 results = yield searchEntries(["guid"], { fieldname: "2hour" });
569 yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
571 timestamp = now_uSec - 130 * kUsecPerMin;
572 results = yield searchEntries(["guid"], { fieldname: "2hour10minutes" });
573 yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
575 timestamp = now_uSec - 180 * kUsecPerMin;
576 results = yield searchEntries(["guid"], { fieldname: "4hour" });
577 yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
579 timestamp = now_uSec - 250 * kUsecPerMin;
580 results = yield searchEntries(["guid"], { fieldname: "4hour10minutes" });
581 yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
583 let today = new Date();
584 today.setHours(0);
585 today.setMinutes(0);
586 today.setSeconds(1);
587 timestamp = today.getTime() * 1000;
588 results = yield searchEntries(["guid"], { fieldname: "today" });
589 yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
591 let lastYear = new Date();
592 lastYear.setFullYear(lastYear.getFullYear() - 1);
593 timestamp = lastYear.getTime() * 1000;
594 results = yield searchEntries(["guid"], { fieldname: "b4today" });
595 yield update({ op: "update", firstUsed: timestamp, guid: results[0].guid });
597 var checks = 0;
598 let checkOne = function(num, message) { is(num, 1, message); checks++; }
600 // Sanity check.
601 yield countEntries("10minutes", "Checking for 10minutes form history entry creation", checkOne);
602 yield countEntries("1hour", "Checking for 1hour form history entry creation", checkOne);
603 yield countEntries("1hour10minutes", "Checking for 1hour10minutes form history entry creation", checkOne);
604 yield countEntries("2hour", "Checking for 2hour form history entry creation", checkOne);
605 yield countEntries("2hour10minutes", "Checking for 2hour10minutes form history entry creation", checkOne);
606 yield countEntries("4hour", "Checking for 4hour form history entry creation", checkOne);
607 yield countEntries("4hour10minutes", "Checking for 4hour10minutes form history entry creation", checkOne);
608 yield countEntries("today", "Checking for today form history entry creation", checkOne);
609 yield countEntries("b4today", "Checking for b4today form history entry creation", checkOne);
610 is(checks, 9, "9 checks made");
611 }
613 function setupDownloads() {
615 let publicList = yield Downloads.getList(Downloads.PUBLIC);
617 let download = yield Downloads.createDownload({
618 source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
619 target: "fakefile-10-minutes"
620 });
621 download.startTime = new Date(now_mSec - 10 * kMsecPerMin), // 10 minutes ago
622 download.canceled = true;
623 yield publicList.add(download);
625 download = yield Downloads.createDownload({
626 source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
627 target: "fakefile-1-hour"
628 });
629 download.startTime = new Date(now_mSec - 45 * kMsecPerMin), // 45 minutes ago
630 download.canceled = true;
631 yield publicList.add(download);
633 download = yield Downloads.createDownload({
634 source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
635 target: "fakefile-1-hour-10-minutes"
636 });
637 download.startTime = new Date(now_mSec - 70 * kMsecPerMin), // 70 minutes ago
638 download.canceled = true;
639 yield publicList.add(download);
641 download = yield Downloads.createDownload({
642 source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
643 target: "fakefile-2-hour"
644 });
645 download.startTime = new Date(now_mSec - 90 * kMsecPerMin), // 90 minutes ago
646 download.canceled = true;
647 yield publicList.add(download);
649 download = yield Downloads.createDownload({
650 source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
651 target: "fakefile-2-hour-10-minutes"
652 });
653 download.startTime = new Date(now_mSec - 130 * kMsecPerMin), // 130 minutes ago
654 download.canceled = true;
655 yield publicList.add(download);
657 download = yield Downloads.createDownload({
658 source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
659 target: "fakefile-4-hour"
660 });
661 download.startTime = new Date(now_mSec - 180 * kMsecPerMin), // 180 minutes ago
662 download.canceled = true;
663 yield publicList.add(download);
665 download = yield Downloads.createDownload({
666 source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
667 target: "fakefile-4-hour-10-minutes"
668 });
669 download.startTime = new Date(now_mSec - 250 * kMsecPerMin), // 250 minutes ago
670 download.canceled = true;
671 yield publicList.add(download);
673 // Add "today" download
674 let today = new Date();
675 today.setHours(0);
676 today.setMinutes(0);
677 today.setSeconds(1);
679 download = yield Downloads.createDownload({
680 source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
681 target: "fakefile-today"
682 });
683 download.startTime = today, // 12:00:01 AM this morning
684 download.canceled = true;
685 yield publicList.add(download);
687 // Add "before today" download
688 let lastYear = new Date();
689 lastYear.setFullYear(lastYear.getFullYear() - 1);
691 download = yield Downloads.createDownload({
692 source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
693 target: "fakefile-old"
694 });
695 download.startTime = lastYear,
696 download.canceled = true;
697 yield publicList.add(download);
699 // Confirm everything worked
700 let downloads = yield publicList.getAll();
701 is(downloads.length, 9, "9 Pretend downloads added");
703 ok((yield downloadExists(publicList, "fakefile-old")), "Pretend download for everything case should exist");
704 ok((yield downloadExists(publicList, "fakefile-10-minutes")), "Pretend download for 10-minutes case should exist");
705 ok((yield downloadExists(publicList, "fakefile-1-hour")), "Pretend download for 1-hour case should exist");
706 ok((yield downloadExists(publicList, "fakefile-1-hour-10-minutes")), "Pretend download for 1-hour-10-minutes case should exist");
707 ok((yield downloadExists(publicList, "fakefile-2-hour")), "Pretend download for 2-hour case should exist");
708 ok((yield downloadExists(publicList, "fakefile-2-hour-10-minutes")), "Pretend download for 2-hour-10-minutes case should exist");
709 ok((yield downloadExists(publicList, "fakefile-4-hour")), "Pretend download for 4-hour case should exist");
710 ok((yield downloadExists(publicList, "fakefile-4-hour-10-minutes")), "Pretend download for 4-hour-10-minutes case should exist");
711 ok((yield downloadExists(publicList, "fakefile-today")), "Pretend download for Today case should exist");
712 }
714 /**
715 * Checks to see if the downloads with the specified id exists.
716 *
717 * @param aID
718 * The ids of the downloads to check.
719 */
720 function downloadExists(list, path)
721 {
722 return Task.spawn(function() {
723 let listArray = yield list.getAll();
724 throw new Task.Result(listArray.some(i => i.target.path == path));
725 });
726 }
728 function isToday(aDate) {
729 return aDate.getDate() == new Date().getDate();
730 }