|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 /** |
|
6 * Test bug 251337 to make sure old downloads are expired and removed. |
|
7 * Make sure bug 431346 and bug 431597 don't cause crashes when batching. |
|
8 */ |
|
9 |
|
10 /** |
|
11 * Returns a PRTime in the past usable to add expirable visits. |
|
12 * |
|
13 * @note Expiration ignores any visit added in the last 7 days, but it's |
|
14 * better be safe against DST issues, by going back one day more. |
|
15 */ |
|
16 function getExpirablePRTime() { |
|
17 let dateObj = new Date(); |
|
18 // Normalize to midnight |
|
19 dateObj.setHours(0); |
|
20 dateObj.setMinutes(0); |
|
21 dateObj.setSeconds(0); |
|
22 dateObj.setMilliseconds(0); |
|
23 dateObj = new Date(dateObj.getTime() - 8 * 86400000); |
|
24 return dateObj.getTime() * 1000; |
|
25 } |
|
26 |
|
27 function run_test() |
|
28 { |
|
29 if (oldDownloadManagerDisabled()) { |
|
30 return; |
|
31 } |
|
32 |
|
33 run_next_test(); |
|
34 } |
|
35 |
|
36 add_task(function test_execute() |
|
37 { |
|
38 // Like the code, we check to see if nav-history-service exists |
|
39 // (i.e MOZ_PLACES is enabled), so that we don't run this test if it doesn't. |
|
40 if (!("@mozilla.org/browser/nav-history-service;1" in Cc)) |
|
41 return; |
|
42 |
|
43 let dm = Cc["@mozilla.org/download-manager;1"]. |
|
44 getService(Ci.nsIDownloadManager); |
|
45 let db = dm.DBConnection; |
|
46 |
|
47 // Empty any old downloads |
|
48 db.executeSimpleSQL("DELETE FROM moz_downloads"); |
|
49 |
|
50 let stmt = db.createStatement( |
|
51 "INSERT INTO moz_downloads (id, source, target, state, guid) " + |
|
52 "VALUES (?1, ?2, ?3, ?4, ?5)"); |
|
53 |
|
54 let iosvc = Cc["@mozilla.org/network/io-service;1"]. |
|
55 getService(Ci.nsIIOService); |
|
56 let theId = 1337; |
|
57 let theURI = iosvc.newURI("http://expireme/please", null, null); |
|
58 let theGUID = "a1bcD23eF4g5"; |
|
59 |
|
60 try { |
|
61 // Add a download from the URI |
|
62 stmt.bindByIndex(0, theId); |
|
63 stmt.bindByIndex(1, theURI.spec); |
|
64 |
|
65 // Give a dummy file name |
|
66 let file = Cc["@mozilla.org/file/directory_service;1"]. |
|
67 getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile); |
|
68 file.append("expireTest"); |
|
69 stmt.bindByIndex(2, Cc["@mozilla.org/network/io-service;1"]. |
|
70 getService(Ci.nsIIOService).newFileURI(file).spec); |
|
71 |
|
72 // Give it some state |
|
73 stmt.bindByIndex(3, dm.DOWNLOAD_FINISHED); |
|
74 |
|
75 stmt.bindByIndex(4, theGUID); |
|
76 |
|
77 // Add it! |
|
78 stmt.execute(); |
|
79 } |
|
80 finally { |
|
81 stmt.reset(); |
|
82 stmt.finalize(); |
|
83 } |
|
84 |
|
85 // Add an expirable visit to this download. |
|
86 let histsvc = Cc["@mozilla.org/browser/nav-history-service;1"]. |
|
87 getService(Ci.nsINavHistoryService); |
|
88 yield promiseAddVisits({uri: theURI, visitDate: getExpirablePRTime(), |
|
89 transition: histsvc.TRANSITION_DOWNLOAD}); |
|
90 |
|
91 // Get the download manager as history observer and batch expirations |
|
92 let histobs = dm.QueryInterface(Ci.nsINavHistoryObserver); |
|
93 histobs.onBeginUpdateBatch(); |
|
94 |
|
95 // Look for the removed download notification |
|
96 let obs = Cc["@mozilla.org/observer-service;1"]. |
|
97 getService(Ci.nsIObserverService); |
|
98 const kRemoveTopic = "download-manager-remove-download-guid"; |
|
99 let testObs = { |
|
100 observe: function(aSubject, aTopic, aData) { |
|
101 if (aTopic != kRemoveTopic) |
|
102 return; |
|
103 |
|
104 // Make sure the removed/expired download was the one we added |
|
105 let id = aSubject.QueryInterface(Ci.nsISupportsCString); |
|
106 do_check_eq(id.data, theGUID); |
|
107 |
|
108 // We're done! |
|
109 histobs.onEndUpdateBatch(); |
|
110 obs.removeObserver(testObs, kRemoveTopic); |
|
111 do_test_finished(); |
|
112 } |
|
113 }; |
|
114 obs.addObserver(testObs, kRemoveTopic, false); |
|
115 |
|
116 // Set expiration stuff to 0 to make the download expire |
|
117 Services.prefs.setIntPref("places.history.expiration.max_pages", 0); |
|
118 |
|
119 // Force a history expiration. |
|
120 let expire = Cc["@mozilla.org/places/expiration;1"].getService(Ci.nsIObserver); |
|
121 expire.observe(null, "places-debug-start-expiration", -1); |
|
122 |
|
123 // Expiration happens on a timeout, about 3.5s after we set the pref |
|
124 do_test_pending(); |
|
125 }); |
|
126 |