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