gecko-dev/toolkit/components/downloads/test/unit/test_removeDownloadsByTimeframe.js
Jim Blandy 7e20285e70 Bug 914753: Make Emacs file variable header lines correct, or at least consistent. DONTBUILD r=ehsan
The -*- file variable lines -*- establish per-file settings that Emacs will
pick up. This patch makes the following changes to those lines (and touches
nothing else):

 - Never set the buffer's mode.

   Years ago, Emacs did not have a good JavaScript mode, so it made sense
   to use Java or C++ mode in .js files. However, Emacs has had js-mode for
   years now; it's perfectly serviceable, and is available and enabled by
   default in all major Emacs packagings.

   Selecting a mode in the -*- file variable line -*- is almost always the
   wrong thing to do anyway. It overrides Emacs's default choice, which is
   (now) reasonable; and even worse, it overrides settings the user might
   have made in their '.emacs' file for that file extension. It's only
   useful when there's something specific about that particular file that
   makes a particular mode appropriate.

 - Correctly propagate settings that establish the correct indentation
   level for this file: c-basic-offset and js2-basic-offset should be
   js-indent-level. Whatever value they're given should be preserved;
   different parts of our tree use different indentation styles.

 - We don't use tabs in Mozilla JS code. Always set indent-tabs-mode: nil.
   Remove tab-width: settings, at least in files that don't contain tab
   characters.

 - Remove js2-mode settings that belong in the user's .emacs file, like
   js2-skip-preprocessor-directives.
2014-06-24 22:12:07 -07:00

167 lines
4.6 KiB
JavaScript

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