michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim: sw=2 ts=2 sts=2 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 added with bug 462964 to test the behavior of the new API that was added michael@0: * to remove downloads by a given time frame. michael@0: */ michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Constants michael@0: michael@0: let dm = Cc["@mozilla.org/download-manager;1"]. michael@0: getService(Ci.nsIDownloadManager); michael@0: michael@0: const START_TIME = Date.now() * 1000; michael@0: const END_TIME = START_TIME + (60 * 1000000); // one minute range michael@0: michael@0: const DOWNLOAD_FINISHED = Ci.nsIDownloadManager.DOWNLOAD_FINISHED; michael@0: const DOWNLOAD_DOWNLOADING = Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING; michael@0: michael@0: const REMOVED_TOPIC = "download-manager-remove-download"; michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Utility Functions michael@0: michael@0: /** michael@0: * Adds a download to the database. michael@0: * michael@0: * @param aStartTimeInRange michael@0: * Indicates if the new download's start time should be in range. michael@0: * @param aEndTimeInRange michael@0: * Indicates if the new download's end time should be in range. michael@0: * @returns the inserted id. michael@0: */ michael@0: let id = 1; michael@0: function add_download_to_db(aStartTimeInRange, aEndTimeInRange, aState) michael@0: { michael@0: let db = dm.DBConnection; michael@0: let stmt = db.createStatement( michael@0: "INSERT INTO moz_downloads (id, startTime, endTime, state) " + michael@0: "VALUES (:id, :startTime, :endTime, :state)" michael@0: ); michael@0: stmt.params.id = id; michael@0: stmt.params.startTime = aStartTimeInRange ? START_TIME + 1 : START_TIME - 1; michael@0: stmt.params.endTime = aEndTimeInRange ? END_TIME - 1 : END_TIME + 1; michael@0: stmt.params.state = aState; michael@0: stmt.execute(); michael@0: stmt.finalize(); michael@0: michael@0: return id++; michael@0: } michael@0: michael@0: /** michael@0: * Checks to see the downloads with the specified id exist or not. michael@0: * michael@0: * @param aIDs michael@0: * The ids of the downloads to check. michael@0: * @param aExpected michael@0: * True if we expect the download to exist, false if we do not. michael@0: */ michael@0: function check_existence(aIDs, aExpected) michael@0: { michael@0: let db = dm.DBConnection; michael@0: let stmt = db.createStatement( michael@0: "SELECT * " + michael@0: "FROM moz_downloads " + michael@0: "WHERE id = :id" michael@0: ); michael@0: michael@0: let checkFunc = aExpected ? do_check_true : do_check_false; michael@0: for (let i = 0; i < aIDs.length; i++) { michael@0: stmt.params.id = aIDs[i]; michael@0: checkFunc(stmt.executeStep()); michael@0: stmt.reset(); michael@0: } michael@0: stmt.finalize(); michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: //// Test Functions michael@0: michael@0: function test_download_start_in_range() michael@0: { michael@0: let id = add_download_to_db(true, false, DOWNLOAD_FINISHED); michael@0: dm.removeDownloadsByTimeframe(START_TIME, END_TIME); michael@0: check_existence([id], false); michael@0: } michael@0: michael@0: function test_download_end_in_range() michael@0: { michael@0: let id = add_download_to_db(false, true, DOWNLOAD_FINISHED); michael@0: dm.removeDownloadsByTimeframe(START_TIME, END_TIME); michael@0: check_existence([id], true); michael@0: } michael@0: michael@0: function test_multiple_downloads_in_range() michael@0: { michael@0: let ids = []; michael@0: ids.push(add_download_to_db(true, false, DOWNLOAD_FINISHED)); michael@0: ids.push(add_download_to_db(true, false, DOWNLOAD_FINISHED)); michael@0: dm.removeDownloadsByTimeframe(START_TIME, END_TIME); michael@0: check_existence(ids, false); michael@0: } michael@0: michael@0: function test_no_downloads_in_range() michael@0: { michael@0: let ids = []; michael@0: ids.push(add_download_to_db(false, true, DOWNLOAD_FINISHED)); michael@0: ids.push(add_download_to_db(false, true, DOWNLOAD_FINISHED)); michael@0: dm.removeDownloadsByTimeframe(START_TIME, END_TIME); michael@0: check_existence(ids, true); michael@0: } michael@0: michael@0: function test_active_download_in_range() michael@0: { michael@0: let id = add_download_to_db(true, false, DOWNLOAD_DOWNLOADING); michael@0: dm.removeDownloadsByTimeframe(START_TIME, END_TIME); michael@0: check_existence([id], true); michael@0: } michael@0: michael@0: function test_observer_dispatched() michael@0: { michael@0: let observer = { michael@0: notified: false, michael@0: observe: function(aSubject, aTopic, aData) michael@0: { michael@0: this.notified = true; michael@0: do_check_eq(aSubject, null); michael@0: do_check_eq(aTopic, REMOVED_TOPIC); michael@0: do_check_eq(aData, null); michael@0: } michael@0: }; michael@0: let os = Cc["@mozilla.org/observer-service;1"]. michael@0: getService(Ci.nsIObserverService); michael@0: os.addObserver(observer, REMOVED_TOPIC, false); michael@0: michael@0: add_download_to_db(true, false, DOWNLOAD_FINISHED); michael@0: dm.removeDownloadsByTimeframe(START_TIME, END_TIME); michael@0: do_check_true(observer.notified); michael@0: michael@0: os.removeObserver(observer, REMOVED_TOPIC); michael@0: } michael@0: michael@0: let tests = [ michael@0: test_download_start_in_range, michael@0: test_download_end_in_range, michael@0: test_multiple_downloads_in_range, michael@0: test_no_downloads_in_range, michael@0: test_active_download_in_range, michael@0: test_observer_dispatched, michael@0: ]; michael@0: michael@0: function run_test() michael@0: { michael@0: if (oldDownloadManagerDisabled()) { michael@0: return; michael@0: } michael@0: michael@0: for (let i = 0; i < tests.length; i++) { michael@0: dm.cleanUp(); michael@0: tests[i](); michael@0: } michael@0: }