michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /** michael@0: * Test bug 251337 to make sure old downloads are expired and removed. michael@0: * Make sure bug 431346 and bug 431597 don't cause crashes when batching. michael@0: */ michael@0: michael@0: /** michael@0: * Returns a PRTime in the past usable to add expirable visits. michael@0: * michael@0: * @note Expiration ignores any visit added in the last 7 days, but it's michael@0: * better be safe against DST issues, by going back one day more. michael@0: */ michael@0: function getExpirablePRTime() { michael@0: let dateObj = new Date(); michael@0: // Normalize to midnight michael@0: dateObj.setHours(0); michael@0: dateObj.setMinutes(0); michael@0: dateObj.setSeconds(0); michael@0: dateObj.setMilliseconds(0); michael@0: dateObj = new Date(dateObj.getTime() - 8 * 86400000); michael@0: return dateObj.getTime() * 1000; michael@0: } michael@0: michael@0: function run_test() michael@0: { michael@0: if (oldDownloadManagerDisabled()) { michael@0: return; michael@0: } michael@0: michael@0: run_next_test(); michael@0: } michael@0: michael@0: add_task(function test_execute() michael@0: { michael@0: // Like the code, we check to see if nav-history-service exists michael@0: // (i.e MOZ_PLACES is enabled), so that we don't run this test if it doesn't. michael@0: if (!("@mozilla.org/browser/nav-history-service;1" in Cc)) michael@0: return; michael@0: michael@0: let dm = Cc["@mozilla.org/download-manager;1"]. michael@0: getService(Ci.nsIDownloadManager); michael@0: let db = dm.DBConnection; michael@0: michael@0: // Empty any old downloads michael@0: db.executeSimpleSQL("DELETE FROM moz_downloads"); michael@0: michael@0: let stmt = db.createStatement( michael@0: "INSERT INTO moz_downloads (id, source, target, state, guid) " + michael@0: "VALUES (?1, ?2, ?3, ?4, ?5)"); michael@0: michael@0: let iosvc = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService); michael@0: let theId = 1337; michael@0: let theURI = iosvc.newURI("http://expireme/please", null, null); michael@0: let theGUID = "a1bcD23eF4g5"; michael@0: michael@0: try { michael@0: // Add a download from the URI michael@0: stmt.bindByIndex(0, theId); michael@0: stmt.bindByIndex(1, theURI.spec); michael@0: michael@0: // Give a dummy file name michael@0: let file = Cc["@mozilla.org/file/directory_service;1"]. michael@0: getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile); michael@0: file.append("expireTest"); michael@0: stmt.bindByIndex(2, Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService).newFileURI(file).spec); michael@0: michael@0: // Give it some state michael@0: stmt.bindByIndex(3, dm.DOWNLOAD_FINISHED); michael@0: michael@0: stmt.bindByIndex(4, theGUID); michael@0: michael@0: // Add it! michael@0: stmt.execute(); michael@0: } michael@0: finally { michael@0: stmt.reset(); michael@0: stmt.finalize(); michael@0: } michael@0: michael@0: // Add an expirable visit to this download. michael@0: let histsvc = Cc["@mozilla.org/browser/nav-history-service;1"]. michael@0: getService(Ci.nsINavHistoryService); michael@0: yield promiseAddVisits({uri: theURI, visitDate: getExpirablePRTime(), michael@0: transition: histsvc.TRANSITION_DOWNLOAD}); michael@0: michael@0: // Get the download manager as history observer and batch expirations michael@0: let histobs = dm.QueryInterface(Ci.nsINavHistoryObserver); michael@0: histobs.onBeginUpdateBatch(); michael@0: michael@0: // Look for the removed download notification michael@0: let obs = Cc["@mozilla.org/observer-service;1"]. michael@0: getService(Ci.nsIObserverService); michael@0: const kRemoveTopic = "download-manager-remove-download-guid"; michael@0: let testObs = { michael@0: observe: function(aSubject, aTopic, aData) { michael@0: if (aTopic != kRemoveTopic) michael@0: return; michael@0: michael@0: // Make sure the removed/expired download was the one we added michael@0: let id = aSubject.QueryInterface(Ci.nsISupportsCString); michael@0: do_check_eq(id.data, theGUID); michael@0: michael@0: // We're done! michael@0: histobs.onEndUpdateBatch(); michael@0: obs.removeObserver(testObs, kRemoveTopic); michael@0: do_test_finished(); michael@0: } michael@0: }; michael@0: obs.addObserver(testObs, kRemoveTopic, false); michael@0: michael@0: // Set expiration stuff to 0 to make the download expire michael@0: Services.prefs.setIntPref("places.history.expiration.max_pages", 0); michael@0: michael@0: // Force a history expiration. michael@0: let expire = Cc["@mozilla.org/places/expiration;1"].getService(Ci.nsIObserver); michael@0: expire.observe(null, "places-debug-start-expiration", -1); michael@0: michael@0: // Expiration happens on a timeout, about 3.5s after we set the pref michael@0: do_test_pending(); michael@0: }); michael@0: