1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/downloads/test/unit/test_history_expiration.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,126 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +/** 1.9 + * Test bug 251337 to make sure old downloads are expired and removed. 1.10 + * Make sure bug 431346 and bug 431597 don't cause crashes when batching. 1.11 + */ 1.12 + 1.13 +/** 1.14 + * Returns a PRTime in the past usable to add expirable visits. 1.15 + * 1.16 + * @note Expiration ignores any visit added in the last 7 days, but it's 1.17 + * better be safe against DST issues, by going back one day more. 1.18 + */ 1.19 +function getExpirablePRTime() { 1.20 + let dateObj = new Date(); 1.21 + // Normalize to midnight 1.22 + dateObj.setHours(0); 1.23 + dateObj.setMinutes(0); 1.24 + dateObj.setSeconds(0); 1.25 + dateObj.setMilliseconds(0); 1.26 + dateObj = new Date(dateObj.getTime() - 8 * 86400000); 1.27 + return dateObj.getTime() * 1000; 1.28 +} 1.29 + 1.30 +function run_test() 1.31 +{ 1.32 + if (oldDownloadManagerDisabled()) { 1.33 + return; 1.34 + } 1.35 + 1.36 + run_next_test(); 1.37 +} 1.38 + 1.39 +add_task(function test_execute() 1.40 +{ 1.41 + // Like the code, we check to see if nav-history-service exists 1.42 + // (i.e MOZ_PLACES is enabled), so that we don't run this test if it doesn't. 1.43 + if (!("@mozilla.org/browser/nav-history-service;1" in Cc)) 1.44 + return; 1.45 + 1.46 + let dm = Cc["@mozilla.org/download-manager;1"]. 1.47 + getService(Ci.nsIDownloadManager); 1.48 + let db = dm.DBConnection; 1.49 + 1.50 + // Empty any old downloads 1.51 + db.executeSimpleSQL("DELETE FROM moz_downloads"); 1.52 + 1.53 + let stmt = db.createStatement( 1.54 + "INSERT INTO moz_downloads (id, source, target, state, guid) " + 1.55 + "VALUES (?1, ?2, ?3, ?4, ?5)"); 1.56 + 1.57 + let iosvc = Cc["@mozilla.org/network/io-service;1"]. 1.58 + getService(Ci.nsIIOService); 1.59 + let theId = 1337; 1.60 + let theURI = iosvc.newURI("http://expireme/please", null, null); 1.61 + let theGUID = "a1bcD23eF4g5"; 1.62 + 1.63 + try { 1.64 + // Add a download from the URI 1.65 + stmt.bindByIndex(0, theId); 1.66 + stmt.bindByIndex(1, theURI.spec); 1.67 + 1.68 + // Give a dummy file name 1.69 + let file = Cc["@mozilla.org/file/directory_service;1"]. 1.70 + getService(Ci.nsIProperties).get("TmpD", Ci.nsIFile); 1.71 + file.append("expireTest"); 1.72 + stmt.bindByIndex(2, Cc["@mozilla.org/network/io-service;1"]. 1.73 + getService(Ci.nsIIOService).newFileURI(file).spec); 1.74 + 1.75 + // Give it some state 1.76 + stmt.bindByIndex(3, dm.DOWNLOAD_FINISHED); 1.77 + 1.78 + stmt.bindByIndex(4, theGUID); 1.79 + 1.80 + // Add it! 1.81 + stmt.execute(); 1.82 + } 1.83 + finally { 1.84 + stmt.reset(); 1.85 + stmt.finalize(); 1.86 + } 1.87 + 1.88 + // Add an expirable visit to this download. 1.89 + let histsvc = Cc["@mozilla.org/browser/nav-history-service;1"]. 1.90 + getService(Ci.nsINavHistoryService); 1.91 + yield promiseAddVisits({uri: theURI, visitDate: getExpirablePRTime(), 1.92 + transition: histsvc.TRANSITION_DOWNLOAD}); 1.93 + 1.94 + // Get the download manager as history observer and batch expirations 1.95 + let histobs = dm.QueryInterface(Ci.nsINavHistoryObserver); 1.96 + histobs.onBeginUpdateBatch(); 1.97 + 1.98 + // Look for the removed download notification 1.99 + let obs = Cc["@mozilla.org/observer-service;1"]. 1.100 + getService(Ci.nsIObserverService); 1.101 + const kRemoveTopic = "download-manager-remove-download-guid"; 1.102 + let testObs = { 1.103 + observe: function(aSubject, aTopic, aData) { 1.104 + if (aTopic != kRemoveTopic) 1.105 + return; 1.106 + 1.107 + // Make sure the removed/expired download was the one we added 1.108 + let id = aSubject.QueryInterface(Ci.nsISupportsCString); 1.109 + do_check_eq(id.data, theGUID); 1.110 + 1.111 + // We're done! 1.112 + histobs.onEndUpdateBatch(); 1.113 + obs.removeObserver(testObs, kRemoveTopic); 1.114 + do_test_finished(); 1.115 + } 1.116 + }; 1.117 + obs.addObserver(testObs, kRemoveTopic, false); 1.118 + 1.119 + // Set expiration stuff to 0 to make the download expire 1.120 + Services.prefs.setIntPref("places.history.expiration.max_pages", 0); 1.121 + 1.122 + // Force a history expiration. 1.123 + let expire = Cc["@mozilla.org/places/expiration;1"].getService(Ci.nsIObserver); 1.124 + expire.observe(null, "places-debug-start-expiration", -1); 1.125 + 1.126 + // Expiration happens on a timeout, about 3.5s after we set the pref 1.127 + do_test_pending(); 1.128 +}); 1.129 +