browser_sanitize-timespans.js
Go to the documentation of this file.
1 // Bug 453440 - Test the timespan-based logic of the sanitizer code
2 var now_uSec = Date.now() * 1000;
3 
4 const dm = Cc["@mozilla.org/download-manager;1"].getService(Ci.nsIDownloadManager);
5 const bhist = Cc["@mozilla.org/browser/global-history;2"].getService(Ci.nsIBrowserHistory);
6 const formhist = Cc["@mozilla.org/satchel/form-history;1"].getService(Ci.nsIFormHistory2);
7 
8 Cc["@mozilla.org/moz/jssubscript-loader;1"].getService(Ci.mozIJSSubScriptLoader)
9  .loadSubScript("chrome://browser/content/sanitize.js");
10 
11 function test() {
12 
13  var hoursSinceMidnight = new Date().getHours();
14  var minutesSinceMidnight = new Date().getMinutes();
15 
16  setupHistory();
19 
20  // Should test cookies here, but nsICookieManager/nsICookieService
21  // doesn't let us fake creation times. bug 463127
22 
23  let s = new Sanitizer();
24  s.ignoreTimespan = false;
25  s.prefDomain = "privacy.cpd.";
26  var itemPrefs = gPrefService.getBranch(s.prefDomain);
27  itemPrefs.setBoolPref("history", true);
28  itemPrefs.setBoolPref("downloads", true);
29  itemPrefs.setBoolPref("cache", false);
30  itemPrefs.setBoolPref("cookies", false);
31  itemPrefs.setBoolPref("formdata", true);
32  itemPrefs.setBoolPref("offlineApps", false);
33  itemPrefs.setBoolPref("passwords", false);
34  itemPrefs.setBoolPref("sessions", false);
35  itemPrefs.setBoolPref("siteSettings", false);
36 
37  // Clear 10 minutes ago
38  s.range = [now_uSec - 10*60*1000000, now_uSec];
39  s.sanitize();
40  s.range = null;
41 
42  ok(!bhist.isVisited(makeURI("http://10minutes.com")), "10minutes.com should now be deleted");
43  ok(bhist.isVisited(makeURI("http://1hour.com")), "Pretend visit to 1hour.com should still exist");
44  ok(bhist.isVisited(makeURI("http://1hour10minutes.com/")), "Pretend visit to 1hour10minutes.com should still exist");
45  ok(bhist.isVisited(makeURI("http://2hour.com")), "Pretend visit to 2hour.com should still exist");
46  ok(bhist.isVisited(makeURI("http://2hour10minutes.com/")), "Pretend visit to 2hour10minutes.com should still exist");
47  ok(bhist.isVisited(makeURI("http://4hour.com")), "Pretend visit to 4hour.com should still exist");
48  ok(bhist.isVisited(makeURI("http://4hour10minutes.com/")), "Pretend visit to 4hour10minutes.com should still exist");
49 
50  if(minutesSinceMidnight > 10)
51  ok(bhist.isVisited(makeURI("http://today.com")), "Pretend visit to today.com should still exist");
52  ok(bhist.isVisited(makeURI("http://before-today.com")), "Pretend visit to before-today.com should still exist");
53 
54  ok(!formhist.nameExists("10minutes"), "10minutes form entry should be deleted");
55  ok(formhist.nameExists("1hour"), "1hour form entry should still exist");
56  ok(formhist.nameExists("1hour10minutes"), "1hour10minutes form entry should still exist");
57  ok(formhist.nameExists("2hour"), "2hour form entry should still exist");
58  ok(formhist.nameExists("2hour10minutes"), "2hour10minutes form entry should still exist");
59  ok(formhist.nameExists("4hour"), "4hour form entry should still exist");
60  ok(formhist.nameExists("4hour10minutes"), "4hour10minutes form entry should still exist");
61  if(minutesSinceMidnight > 10)
62  ok(formhist.nameExists("today"), "today form entry should still exist");
63  ok(formhist.nameExists("b4today"), "b4today form entry should still exist");
64 
65  ok(!downloadExists(5555555), "10 minute download should now be deleted");
66  ok(downloadExists(5555551), "<1 hour download should still be present");
67  ok(downloadExists(5555556), "1 hour 10 minute download should still be present");
68  ok(downloadExists(5555550), "Year old download should still be present");
69  ok(downloadExists(5555552), "<2 hour old download should still be present");
70  ok(downloadExists(5555557), "2 hour 10 minute download should still be present");
71  ok(downloadExists(5555553), "<4 hour old download should still be present");
72  ok(downloadExists(5555558), "4 hour 10 minute download should still be present");
73 
74  if(minutesSinceMidnight > 10)
75  ok(downloadExists(5555554), "'Today' download should still be present");
76 
77  // Clear 1 hour
78  Sanitizer.prefs.setIntPref("timeSpan", 1);
79  s.sanitize();
80 
81  ok(!bhist.isVisited(makeURI("http://1hour.com")), "1hour.com should now be deleted");
82  ok(bhist.isVisited(makeURI("http://1hour10minutes.com/")), "Pretend visit to 1hour10minutes.com should still exist");
83  ok(bhist.isVisited(makeURI("http://2hour.com")), "Pretend visit to 2hour.com should still exist");
84  ok(bhist.isVisited(makeURI("http://2hour10minutes.com/")), "Pretend visit to 2hour10minutes.com should still exist");
85  ok(bhist.isVisited(makeURI("http://4hour.com")), "Pretend visit to 4hour.com should still exist");
86  ok(bhist.isVisited(makeURI("http://4hour10minutes.com/")), "Pretend visit to 4hour10minutes.com should still exist");
87 
88  if(hoursSinceMidnight > 1)
89  ok(bhist.isVisited(makeURI("http://today.com")), "Pretend visit to today.com should still exist");
90  ok(bhist.isVisited(makeURI("http://before-today.com")), "Pretend visit to before-today.com should still exist");
91 
92  ok(!formhist.nameExists("1hour"), "1hour form entry should be deleted");
93  ok(formhist.nameExists("1hour10minutes"), "1hour10minutes form entry should still exist");
94  ok(formhist.nameExists("2hour"), "2hour form entry should still exist");
95  ok(formhist.nameExists("2hour10minutes"), "2hour10minutes form entry should still exist");
96  ok(formhist.nameExists("4hour"), "4hour form entry should still exist");
97  ok(formhist.nameExists("4hour10minutes"), "4hour10minutes form entry should still exist");
98  if(hoursSinceMidnight > 1)
99  ok(formhist.nameExists("today"), "today form entry should still exist");
100  ok(formhist.nameExists("b4today"), "b4today form entry should still exist");
101 
102  ok(!downloadExists(5555551), "<1 hour download should now be deleted");
103  ok(downloadExists(5555556), "1 hour 10 minute download should still be present");
104  ok(downloadExists(5555550), "Year old download should still be present");
105  ok(downloadExists(5555552), "<2 hour old download should still be present");
106  ok(downloadExists(5555557), "2 hour 10 minute download should still be present");
107  ok(downloadExists(5555553), "<4 hour old download should still be present");
108  ok(downloadExists(5555558), "4 hour 10 minute download should still be present");
109 
110  if(hoursSinceMidnight > 1)
111  ok(downloadExists(5555554), "'Today' download should still be present");
112 
113  // Clear 1 hour 10 minutes
114  s.range = [now_uSec - 70*60*1000000, now_uSec];
115  s.sanitize();
116  s.range = null;
117 
118  ok(!bhist.isVisited(makeURI("http://1hour10minutes.com")), "Pretend visit to 1hour10minutes.com should now be deleted");
119  ok(bhist.isVisited(makeURI("http://2hour.com")), "Pretend visit to 2hour.com should still exist");
120  ok(bhist.isVisited(makeURI("http://2hour10minutes.com/")), "Pretend visit to 2hour10minutes.com should still exist");
121  ok(bhist.isVisited(makeURI("http://4hour.com")), "Pretend visit to 4hour.com should still exist");
122  ok(bhist.isVisited(makeURI("http://4hour10minutes.com/")), "Pretend visit to 4hour10minutes.com should still exist");
123  if(minutesSinceMidnight > 70)
124  ok(bhist.isVisited(makeURI("http://today.com")), "Pretend visit to today.com should still exist");
125  ok(bhist.isVisited(makeURI("http://before-today.com")), "Pretend visit to before-today.com should still exist");
126 
127  ok(!formhist.nameExists("1hour10minutes"), "1hour10minutes form entry should be deleted");
128  ok(formhist.nameExists("2hour"), "2hour form entry should still exist");
129  ok(formhist.nameExists("2hour10minutes"), "2hour10minutes form entry should still exist");
130  ok(formhist.nameExists("4hour"), "4hour form entry should still exist");
131  ok(formhist.nameExists("4hour10minutes"), "4hour10minutes form entry should still exist");
132  if(minutesSinceMidnight > 70)
133  ok(formhist.nameExists("today"), "today form entry should still exist");
134  ok(formhist.nameExists("b4today"), "b4today form entry should still exist");
135 
136  ok(!downloadExists(5555556), "1 hour 10 minute old download should now be deleted");
137  ok(downloadExists(5555550), "Year old download should still be present");
138  ok(downloadExists(5555552), "<2 hour old download should still be present");
139  ok(downloadExists(5555557), "2 hour 10 minute download should still be present");
140  ok(downloadExists(5555553), "<4 hour old download should still be present");
141  ok(downloadExists(5555558), "4 hour 10 minute download should still be present");
142  if(minutesSinceMidnight > 70)
143  ok(downloadExists(5555554), "'Today' download should still be present");
144 
145  // Clear 2 hours
146  Sanitizer.prefs.setIntPref("timeSpan", 2);
147  s.sanitize();
148 
149  ok(!bhist.isVisited(makeURI("http://2hour.com")), "Pretend visit to 2hour.com should now be deleted");
150  ok(bhist.isVisited(makeURI("http://2hour10minutes.com/")), "Pretend visit to 2hour10minutes.com should still exist");
151  ok(bhist.isVisited(makeURI("http://4hour.com")), "Pretend visit to 4hour.com should still exist");
152  ok(bhist.isVisited(makeURI("http://4hour10minutes.com/")), "Pretend visit to 4hour10minutes.com should still exist");
153  if(hoursSinceMidnight > 2)
154  ok(bhist.isVisited(makeURI("http://today.com")), "Pretend visit to today.com should still exist");
155  ok(bhist.isVisited(makeURI("http://before-today.com")), "Pretend visit to before-today.com should still exist");
156 
157  ok(!formhist.nameExists("2hour"), "2hour form entry should be deleted");
158  ok(formhist.nameExists("2hour10minutes"), "2hour10minutes form entry should still exist");
159  ok(formhist.nameExists("4hour"), "4hour form entry should still exist");
160  ok(formhist.nameExists("4hour10minutes"), "4hour10minutes form entry should still exist");
161  if(hoursSinceMidnight > 2)
162  ok(formhist.nameExists("today"), "today form entry should still exist");
163  ok(formhist.nameExists("b4today"), "b4today form entry should still exist");
164 
165  ok(formhist.nameExists("b4today"), "b4today form entry should still exist");
166  ok(!downloadExists(5555552), "<2 hour old download should now be deleted");
167  ok(downloadExists(5555550), "Year old download should still be present");
168  ok(downloadExists(5555557), "2 hour 10 minute download should still be present");
169  ok(downloadExists(5555553), "<4 hour old download should still be present");
170  ok(downloadExists(5555558), "4 hour 10 minute download should still be present");
171  if(hoursSinceMidnight > 2)
172  ok(downloadExists(5555554), "'Today' download should still be present");
173 
174  // Clear 2 hours 10 minutes
175  s.range = [now_uSec - 130*60*1000000, now_uSec];
176  s.sanitize();
177  s.range = null;
178 
179  ok(!bhist.isVisited(makeURI("http://2hour10minutes.com")), "Pretend visit to 2hour10minutes.com should now be deleted");
180  ok(bhist.isVisited(makeURI("http://4hour.com")), "Pretend visit to 4hour.com should still exist");
181  ok(bhist.isVisited(makeURI("http://4hour10minutes.com/")), "Pretend visit to 4hour10minutes.com should still exist");
182  if(minutesSinceMidnight > 130)
183  ok(bhist.isVisited(makeURI("http://today.com")), "Pretend visit to today.com should still exist");
184  ok(bhist.isVisited(makeURI("http://before-today.com")), "Pretend visit to before-today.com should still exist");
185 
186  ok(!formhist.nameExists("2hour10minutes"), "2hour10minutes form entry should be deleted");
187  ok(formhist.nameExists("4hour"), "4hour form entry should still exist");
188  ok(formhist.nameExists("4hour10minutes"), "4hour10minutes form entry should still exist");
189  if(minutesSinceMidnight > 130)
190  ok(formhist.nameExists("today"), "today form entry should still exist");
191  ok(formhist.nameExists("b4today"), "b4today form entry should still exist");
192 
193  ok(!downloadExists(5555557), "2 hour 10 minute old download should now be deleted");
194  ok(downloadExists(5555553), "<4 hour old download should still be present");
195  ok(downloadExists(5555558), "4 hour 10 minute download should still be present");
196  ok(downloadExists(5555550), "Year old download should still be present");
197  if(minutesSinceMidnight > 130)
198  ok(downloadExists(5555554), "'Today' download should still be present");
199 
200  // Clear 4 hours
201  Sanitizer.prefs.setIntPref("timeSpan", 3);
202  s.sanitize();
203 
204  ok(!bhist.isVisited(makeURI("http://4hour.com")), "Pretend visit to 4hour.com should now be deleted");
205  ok(bhist.isVisited(makeURI("http://4hour10minutes.com/")), "Pretend visit to 4hour10minutes.com should still exist");
206  if(hoursSinceMidnight > 4)
207  ok(bhist.isVisited(makeURI("http://today.com")), "Pretend visit to today.com should still exist");
208  ok(bhist.isVisited(makeURI("http://before-today.com")), "Pretend visit to before-today.com should still exist");
209 
210  ok(!formhist.nameExists("4hour"), "4hour form entry should be deleted");
211  ok(formhist.nameExists("4hour10minutes"), "4hour10minutes form entry should still exist");
212  if(hoursSinceMidnight > 4)
213  ok(formhist.nameExists("today"), "today form entry should still exist");
214  ok(formhist.nameExists("b4today"), "b4today form entry should still exist");
215 
216  ok(!downloadExists(5555553), "<4 hour old download should now be deleted");
217  ok(downloadExists(5555558), "4 hour 10 minute download should still be present");
218  ok(downloadExists(5555550), "Year old download should still be present");
219  if(hoursSinceMidnight > 4)
220  ok(downloadExists(5555554), "'Today' download should still be present");
221 
222  // Clear 4 hours 10 minutes
223  s.range = [now_uSec - 250*60*1000000, now_uSec];
224  s.sanitize();
225  s.range = null;
226 
227  ok(!bhist.isVisited(makeURI("http://4hour10minutes.com/")), "Pretend visit to 4hour10minutes.com should now be deleted");
228  if(minutesSinceMidnight > 250)
229  ok(bhist.isVisited(makeURI("http://today.com")), "Pretend visit to today.com should still exist");
230  ok(bhist.isVisited(makeURI("http://before-today.com")), "Pretend visit to before-today.com should still exist");
231 
232  ok(!formhist.nameExists("4hour10minutes"), "4hour10minutes form entry should be deleted");
233  if(minutesSinceMidnight > 250)
234  ok(formhist.nameExists("today"), "today form entry should still exist");
235  ok(formhist.nameExists("b4today"), "b4today form entry should still exist");
236 
237  ok(!downloadExists(5555558), "4 hour 10 minute download should now be deleted");
238  ok(downloadExists(5555550), "Year old download should still be present");
239  if(minutesSinceMidnight > 250)
240  ok(downloadExists(5555554), "'Today' download should still be present");
241 
242  // Clear Today
243  Sanitizer.prefs.setIntPref("timeSpan", 4);
244  s.sanitize();
245 
246  ok(!bhist.isVisited(makeURI("http://today.com")), "Pretend visit to today.com should now be deleted");
247  ok(bhist.isVisited(makeURI("http://before-today.com")), "Pretend visit to before-today.com should still exist");
248 
249  ok(!formhist.nameExists("today"), "today form entry should be deleted");
250  ok(formhist.nameExists("b4today"), "b4today form entry should still exist");
251 
252  ok(!downloadExists(5555554), "'Today' download should now be deleted");
253  ok(downloadExists(5555550), "Year old download should still be present");
254 
255  // Choose everything
256  Sanitizer.prefs.setIntPref("timeSpan", 0);
257  s.sanitize();
258 
259  ok(!bhist.isVisited(makeURI("http://before-today.com")), "Pretend visit to before-today.com should now be deleted");
260 
261  ok(!formhist.nameExists("b4today"), "b4today form entry should be deleted");
262 
263  ok(!downloadExists(5555550), "Year old download should now be deleted");
264 
265 }
266 
267 function setupHistory() {
268  bhist.addPageWithDetails(makeURI("http://10minutes.com/"), "10 minutes ago", now_uSec - 10*60*1000000);
269  bhist.addPageWithDetails(makeURI("http://1hour.com/"), "Less than 1 hour ago", now_uSec - 45*60*1000000);
270  bhist.addPageWithDetails(makeURI("http://1hour10minutes.com/"), "1 hour 10 minutes ago", now_uSec - 70*60*1000000);
271  bhist.addPageWithDetails(makeURI("http://2hour.com/"), "Less than 2 hours ago", now_uSec - 90*60*1000000);
272  bhist.addPageWithDetails(makeURI("http://2hour10minutes.com/"), "2 hours 10 minutes ago", now_uSec - 130*60*1000000);
273  bhist.addPageWithDetails(makeURI("http://4hour.com/"), "Less than 4 hours ago", now_uSec - 180*60*1000000);
274  bhist.addPageWithDetails(makeURI("http://4hour10minutes.com/"), "4 hours 10 minutesago", now_uSec - 250*60*1000000);
275 
276  let today = new Date();
277  today.setHours(0);
278  today.setMinutes(0);
279  today.setSeconds(30);
280  bhist.addPageWithDetails(makeURI("http://today.com/"), "Today", today.valueOf() * 1000);
281 
282  let lastYear = new Date();
283  lastYear.setFullYear(lastYear.getFullYear() - 1);
284  bhist.addPageWithDetails(makeURI("http://before-today.com/"), "Before Today", lastYear.valueOf() * 1000);
285 
286  // Confirm everything worked
287  ok(bhist.isVisited(makeURI("http://10minutes.com/")), "Pretend visit to 10minutes.com should exist");
288  ok(bhist.isVisited(makeURI("http://1hour.com")), "Pretend visit to 1hour.com should exist");
289  ok(bhist.isVisited(makeURI("http://1hour10minutes.com/")), "Pretend visit to 1hour10minutes.com should exist");
290  ok(bhist.isVisited(makeURI("http://2hour.com")), "Pretend visit to 2hour.com should exist");
291  ok(bhist.isVisited(makeURI("http://2hour10minutes.com/")), "Pretend visit to 2hour10minutes.com should exist");
292  ok(bhist.isVisited(makeURI("http://4hour.com")), "Pretend visit to 4hour.com should exist");
293  ok(bhist.isVisited(makeURI("http://4hour10minutes.com/")), "Pretend visit to 4hour10minutes.com should exist");
294  ok(bhist.isVisited(makeURI("http://today.com")), "Pretend visit to today.com should exist");
295  ok(bhist.isVisited(makeURI("http://before-today.com")), "Pretend visit to before-today.com should exist");
296 }
297 
298 function setupFormHistory() {
299  // Make sure we've got a clean DB to start with.
300  formhist.removeAllEntries();
301 
302  // Add the entries we'll be testing.
303  formhist.addEntry("10minutes", "10m");
304  formhist.addEntry("1hour", "1h");
305  formhist.addEntry("1hour10minutes", "1h10m");
306  formhist.addEntry("2hour", "2h");
307  formhist.addEntry("2hour10minutes", "2h10m");
308  formhist.addEntry("4hour", "4h");
309  formhist.addEntry("4hour10minutes", "4h10m");
310  formhist.addEntry("today", "1d");
311  formhist.addEntry("b4today", "1y");
312 
313  // Artifically age the entries to the proper vintage.
314  let db = formhist.DBConnection;
315  let timestamp = now_uSec - 10*60*1000000;
316  db.executeSimpleSQL("UPDATE moz_formhistory SET firstUsed = " +
317  timestamp + " WHERE fieldname = '10minutes'");
318  timestamp = now_uSec - 45*60*1000000;
319  db.executeSimpleSQL("UPDATE moz_formhistory SET firstUsed = " +
320  timestamp + " WHERE fieldname = '1hour'");
321  timestamp = now_uSec - 70*60*1000000;
322  db.executeSimpleSQL("UPDATE moz_formhistory SET firstUsed = " +
323  timestamp + " WHERE fieldname = '1hour10minutes'");
324  timestamp = now_uSec - 90*60*1000000;
325  db.executeSimpleSQL("UPDATE moz_formhistory SET firstUsed = " +
326  timestamp + " WHERE fieldname = '2hour'");
327  timestamp = now_uSec - 130*60*1000000;
328  db.executeSimpleSQL("UPDATE moz_formhistory SET firstUsed = " +
329  timestamp + " WHERE fieldname = '2hour10minutes'");
330  timestamp = now_uSec - 180*60*1000000;
331  db.executeSimpleSQL("UPDATE moz_formhistory SET firstUsed = " +
332  timestamp + " WHERE fieldname = '4hour'");
333  timestamp = now_uSec - 250*60*1000000;
334  db.executeSimpleSQL("UPDATE moz_formhistory SET firstUsed = " +
335  timestamp + " WHERE fieldname = '4hour10minutes'");
336 
337  let today = new Date();
338  today.setHours(0);
339  today.setMinutes(0);
340  today.setSeconds(1);
341  timestamp = today.valueOf() * 1000;
342  db.executeSimpleSQL("UPDATE moz_formhistory SET firstUsed = " +
343  timestamp + " WHERE fieldname = 'today'");
344 
345  let lastYear = new Date();
346  lastYear.setFullYear(lastYear.getFullYear() - 1);
347  timestamp = lastYear.valueOf() * 1000;
348  db.executeSimpleSQL("UPDATE moz_formhistory SET firstUsed = " +
349  timestamp + " WHERE fieldname = 'b4today'");
350 
351  // Sanity check.
352  ok(formhist.nameExists("10minutes"), "Checking for 10minutes form history entry creation");
353  ok(formhist.nameExists("1hour"), "Checking for 1hour form history entry creation");
354  ok(formhist.nameExists("1hour10minutes"), "Checking for 1hour10minutes form history entry creation");
355  ok(formhist.nameExists("2hour"), "Checking for 2hour form history entry creation");
356  ok(formhist.nameExists("2hour10minutes"), "Checking for 2hour10minutes form history entry creation");
357  ok(formhist.nameExists("4hour"), "Checking for 4hour form history entry creation");
358  ok(formhist.nameExists("4hour10minutes"), "Checking for 4hour10minutes form history entry creation");
359  ok(formhist.nameExists("today"), "Checking for today form history entry creation");
360  ok(formhist.nameExists("b4today"), "Checking for b4today form history entry creation");
361 }
362 
363 function setupDownloads() {
364 
365  // Add 10-minutes download to DB
366  let data = {
367  id: "5555555",
368  name: "fakefile-10-minutes",
369  source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
370  target: "fakefile-10-minutes",
371  startTime: now_uSec - 10*60*1000000, // 10 minutes ago, in uSec
372  endTime: now_uSec - 11*60*1000000, // 1 minute later
373  state: Ci.nsIDownloadManager.DOWNLOAD_FINISHED,
374  currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0
375  };
376 
377  let db = dm.DBConnection;
378  let stmt = db.createStatement(
379  "INSERT INTO moz_downloads (id, name, source, target, startTime, endTime, " +
380  "state, currBytes, maxBytes, preferredAction, autoResume) " +
381  "VALUES (:id, :name, :source, :target, :startTime, :endTime, :state, " +
382  ":currBytes, :maxBytes, :preferredAction, :autoResume)");
383  try {
384  for (let prop in data)
385  stmt.params[prop] = data[prop];
386  stmt.execute();
387  }
388  finally {
389  stmt.reset();
390  }
391 
392  // Add within-1-hour download to DB
393  data = {
394  id: "5555551",
395  name: "fakefile-1-hour",
396  source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
397  target: "fakefile-1-hour",
398  startTime: now_uSec - 45*60*1000000, // 45 minutes ago, in uSec
399  endTime: now_uSec - 44*60*1000000, // 1 minute later
400  state: Ci.nsIDownloadManager.DOWNLOAD_FINISHED,
401  currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0
402  };
403 
404  try {
405  for (let prop in data)
406  stmt.params[prop] = data[prop];
407  stmt.execute();
408  }
409  finally {
410  stmt.reset();
411  }
412 
413  // Add 1-hour-10-minutes download to DB
414  data = {
415  id: "5555556",
416  name: "fakefile-1-hour-10-minutes",
417  source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
418  target: "fakefile-1-hour-10-minutes",
419  startTime: now_uSec - 70*60*1000000, // 70 minutes ago, in uSec
420  endTime: now_uSec - 71*60*1000000, // 1 minute later
421  state: Ci.nsIDownloadManager.DOWNLOAD_FINISHED,
422  currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0
423  };
424 
425  try {
426  for (let prop in data)
427  stmt.params[prop] = data[prop];
428  stmt.execute();
429  }
430  finally {
431  stmt.reset();
432  }
433 
434  // Add within-2-hour download
435  data = {
436  id: "5555552",
437  name: "fakefile-2-hour",
438  source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
439  target: "fakefile-2-hour",
440  startTime: now_uSec - 90*60*1000000, // 90 minutes ago, in uSec
441  endTime: now_uSec - 89*60*1000000, // 1 minute later
442  state: Ci.nsIDownloadManager.DOWNLOAD_FINISHED,
443  currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0
444  };
445 
446  try {
447  for (let prop in data)
448  stmt.params[prop] = data[prop];
449  stmt.execute();
450  }
451  finally {
452  stmt.reset();
453  }
454 
455  // Add 2-hour-10-minutes download
456  data = {
457  id: "5555557",
458  name: "fakefile-2-hour-10-minutes",
459  source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
460  target: "fakefile-2-hour-10-minutes",
461  startTime: now_uSec - 130*60*1000000, // 130 minutes ago, in uSec
462  endTime: now_uSec - 131*60*1000000, // 1 minute later
463  state: Ci.nsIDownloadManager.DOWNLOAD_FINISHED,
464  currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0
465  };
466 
467  try {
468  for (let prop in data)
469  stmt.params[prop] = data[prop];
470  stmt.execute();
471  }
472  finally {
473  stmt.reset();
474  }
475 
476  // Add within-4-hour download
477  data = {
478  id: "5555553",
479  name: "fakefile-4-hour",
480  source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
481  target: "fakefile-4-hour",
482  startTime: now_uSec - 180*60*1000000, // 180 minutes ago, in uSec
483  endTime: now_uSec - 179*60*1000000, // 1 minute later
484  state: Ci.nsIDownloadManager.DOWNLOAD_FINISHED,
485  currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0
486  };
487 
488  try {
489  for (let prop in data)
490  stmt.params[prop] = data[prop];
491  stmt.execute();
492  }
493  finally {
494  stmt.reset();
495  }
496 
497  // Add 4-hour-10-minutes download
498  data = {
499  id: "5555558",
500  name: "fakefile-4-hour-10-minutes",
501  source: "https://bugzilla.mozilla.org/show_bug.cgi?id=480169",
502  target: "fakefile-4-hour-10-minutes",
503  startTime: now_uSec - 250*60*1000000, // 250 minutes ago, in uSec
504  endTime: now_uSec - 251*60*1000000, // 1 minute later
505  state: Ci.nsIDownloadManager.DOWNLOAD_FINISHED,
506  currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0
507  };
508 
509  try {
510  for (let prop in data)
511  stmt.params[prop] = data[prop];
512  stmt.execute();
513  }
514  finally {
515  stmt.reset();
516  }
517 
518  // Add "today" download
519  let today = new Date();
520  today.setHours(0);
521  today.setMinutes(0);
522  today.setSeconds(30);
523 
524  data = {
525  id: "5555554",
526  name: "fakefile-today",
527  source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
528  target: "fakefile-today",
529  startTime: today.valueOf() * 1000, // 12:00:30am this morning, in uSec
530  endTime: (today.valueOf() + 1000) * 1000, // 1 second later
531  state: Ci.nsIDownloadManager.DOWNLOAD_FINISHED,
532  currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0
533  };
534 
535  try {
536  for (let prop in data)
537  stmt.params[prop] = data[prop];
538  stmt.execute();
539  }
540  finally {
541  stmt.reset();
542  }
543 
544  // Add "before today" download
545  let lastYear = new Date();
546  lastYear.setFullYear(lastYear.getFullYear() - 1);
547  data = {
548  id: "5555550",
549  name: "fakefile-old",
550  source: "https://bugzilla.mozilla.org/show_bug.cgi?id=453440",
551  target: "fakefile-old",
552  startTime: lastYear.valueOf() * 1000, // 1 year ago, in uSec
553  endTime: (lastYear.valueOf() + 1000) * 1000, // 1 second later
554  state: Ci.nsIDownloadManager.DOWNLOAD_FINISHED,
555  currBytes: 0, maxBytes: -1, preferredAction: 0, autoResume: 0
556  };
557 
558  try {
559  for (let prop in data)
560  stmt.params[prop] = data[prop];
561  stmt.execute();
562  }
563  finally {
564  stmt.finalize();
565  }
566 
567  // Confirm everything worked
568  ok(downloadExists(5555550), "Pretend download for everything case should exist");
569  ok(downloadExists(5555555), "Pretend download for 10-minutes case should exist");
570  ok(downloadExists(5555551), "Pretend download for 1-hour case should exist");
571  ok(downloadExists(5555556), "Pretend download for 1-hour-10-minutes case should exist");
572  ok(downloadExists(5555552), "Pretend download for 2-hour case should exist");
573  ok(downloadExists(5555557), "Pretend download for 2-hour-10-minutes case should exist");
574  ok(downloadExists(5555553), "Pretend download for 4-hour case should exist");
575  ok(downloadExists(5555558), "Pretend download for 4-hour-10-minutes case should exist");
576  ok(downloadExists(5555554), "Pretend download for Today case should exist");
577 }
578 
585 function downloadExists(aID)
586 {
587  let db = dm.DBConnection;
588  let stmt = db.createStatement(
589  "SELECT * " +
590  "FROM moz_downloads " +
591  "WHERE id = :id"
592  );
593  stmt.params.id = aID;
594  var rows = stmt.step();
595  stmt.finalize();
596  return rows;
597 }
const Cc
const formhist
function setupHistory()
function downloadExists(aID)
function makeURI(aURLSpec, aCharset)
Definition: FeedWriter.js:71
function setupDownloads()
return null
Definition: FeedWriter.js:1143
var gPrefService
Definition: overlay.js:34
function setupFormHistory()
const Ci
function Sanitizer()
Definition: sanitize.js:42
observe data
Definition: FeedWriter.js:1329