toolkit/components/downloads/test/unit/test_removeDownloadsByTimeframe.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * vim: sw=2 ts=2 sts=2
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /**
michael@0 8 * Test added with bug 462964 to test the behavior of the new API that was added
michael@0 9 * to remove downloads by a given time frame.
michael@0 10 */
michael@0 11
michael@0 12 ////////////////////////////////////////////////////////////////////////////////
michael@0 13 //// Constants
michael@0 14
michael@0 15 let dm = Cc["@mozilla.org/download-manager;1"].
michael@0 16 getService(Ci.nsIDownloadManager);
michael@0 17
michael@0 18 const START_TIME = Date.now() * 1000;
michael@0 19 const END_TIME = START_TIME + (60 * 1000000); // one minute range
michael@0 20
michael@0 21 const DOWNLOAD_FINISHED = Ci.nsIDownloadManager.DOWNLOAD_FINISHED;
michael@0 22 const DOWNLOAD_DOWNLOADING = Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING;
michael@0 23
michael@0 24 const REMOVED_TOPIC = "download-manager-remove-download";
michael@0 25
michael@0 26 ////////////////////////////////////////////////////////////////////////////////
michael@0 27 //// Utility Functions
michael@0 28
michael@0 29 /**
michael@0 30 * Adds a download to the database.
michael@0 31 *
michael@0 32 * @param aStartTimeInRange
michael@0 33 * Indicates if the new download's start time should be in range.
michael@0 34 * @param aEndTimeInRange
michael@0 35 * Indicates if the new download's end time should be in range.
michael@0 36 * @returns the inserted id.
michael@0 37 */
michael@0 38 let id = 1;
michael@0 39 function add_download_to_db(aStartTimeInRange, aEndTimeInRange, aState)
michael@0 40 {
michael@0 41 let db = dm.DBConnection;
michael@0 42 let stmt = db.createStatement(
michael@0 43 "INSERT INTO moz_downloads (id, startTime, endTime, state) " +
michael@0 44 "VALUES (:id, :startTime, :endTime, :state)"
michael@0 45 );
michael@0 46 stmt.params.id = id;
michael@0 47 stmt.params.startTime = aStartTimeInRange ? START_TIME + 1 : START_TIME - 1;
michael@0 48 stmt.params.endTime = aEndTimeInRange ? END_TIME - 1 : END_TIME + 1;
michael@0 49 stmt.params.state = aState;
michael@0 50 stmt.execute();
michael@0 51 stmt.finalize();
michael@0 52
michael@0 53 return id++;
michael@0 54 }
michael@0 55
michael@0 56 /**
michael@0 57 * Checks to see the downloads with the specified id exist or not.
michael@0 58 *
michael@0 59 * @param aIDs
michael@0 60 * The ids of the downloads to check.
michael@0 61 * @param aExpected
michael@0 62 * True if we expect the download to exist, false if we do not.
michael@0 63 */
michael@0 64 function check_existence(aIDs, aExpected)
michael@0 65 {
michael@0 66 let db = dm.DBConnection;
michael@0 67 let stmt = db.createStatement(
michael@0 68 "SELECT * " +
michael@0 69 "FROM moz_downloads " +
michael@0 70 "WHERE id = :id"
michael@0 71 );
michael@0 72
michael@0 73 let checkFunc = aExpected ? do_check_true : do_check_false;
michael@0 74 for (let i = 0; i < aIDs.length; i++) {
michael@0 75 stmt.params.id = aIDs[i];
michael@0 76 checkFunc(stmt.executeStep());
michael@0 77 stmt.reset();
michael@0 78 }
michael@0 79 stmt.finalize();
michael@0 80 }
michael@0 81
michael@0 82 ////////////////////////////////////////////////////////////////////////////////
michael@0 83 //// Test Functions
michael@0 84
michael@0 85 function test_download_start_in_range()
michael@0 86 {
michael@0 87 let id = add_download_to_db(true, false, DOWNLOAD_FINISHED);
michael@0 88 dm.removeDownloadsByTimeframe(START_TIME, END_TIME);
michael@0 89 check_existence([id], false);
michael@0 90 }
michael@0 91
michael@0 92 function test_download_end_in_range()
michael@0 93 {
michael@0 94 let id = add_download_to_db(false, true, DOWNLOAD_FINISHED);
michael@0 95 dm.removeDownloadsByTimeframe(START_TIME, END_TIME);
michael@0 96 check_existence([id], true);
michael@0 97 }
michael@0 98
michael@0 99 function test_multiple_downloads_in_range()
michael@0 100 {
michael@0 101 let ids = [];
michael@0 102 ids.push(add_download_to_db(true, false, DOWNLOAD_FINISHED));
michael@0 103 ids.push(add_download_to_db(true, false, DOWNLOAD_FINISHED));
michael@0 104 dm.removeDownloadsByTimeframe(START_TIME, END_TIME);
michael@0 105 check_existence(ids, false);
michael@0 106 }
michael@0 107
michael@0 108 function test_no_downloads_in_range()
michael@0 109 {
michael@0 110 let ids = [];
michael@0 111 ids.push(add_download_to_db(false, true, DOWNLOAD_FINISHED));
michael@0 112 ids.push(add_download_to_db(false, true, DOWNLOAD_FINISHED));
michael@0 113 dm.removeDownloadsByTimeframe(START_TIME, END_TIME);
michael@0 114 check_existence(ids, true);
michael@0 115 }
michael@0 116
michael@0 117 function test_active_download_in_range()
michael@0 118 {
michael@0 119 let id = add_download_to_db(true, false, DOWNLOAD_DOWNLOADING);
michael@0 120 dm.removeDownloadsByTimeframe(START_TIME, END_TIME);
michael@0 121 check_existence([id], true);
michael@0 122 }
michael@0 123
michael@0 124 function test_observer_dispatched()
michael@0 125 {
michael@0 126 let observer = {
michael@0 127 notified: false,
michael@0 128 observe: function(aSubject, aTopic, aData)
michael@0 129 {
michael@0 130 this.notified = true;
michael@0 131 do_check_eq(aSubject, null);
michael@0 132 do_check_eq(aTopic, REMOVED_TOPIC);
michael@0 133 do_check_eq(aData, null);
michael@0 134 }
michael@0 135 };
michael@0 136 let os = Cc["@mozilla.org/observer-service;1"].
michael@0 137 getService(Ci.nsIObserverService);
michael@0 138 os.addObserver(observer, REMOVED_TOPIC, false);
michael@0 139
michael@0 140 add_download_to_db(true, false, DOWNLOAD_FINISHED);
michael@0 141 dm.removeDownloadsByTimeframe(START_TIME, END_TIME);
michael@0 142 do_check_true(observer.notified);
michael@0 143
michael@0 144 os.removeObserver(observer, REMOVED_TOPIC);
michael@0 145 }
michael@0 146
michael@0 147 let tests = [
michael@0 148 test_download_start_in_range,
michael@0 149 test_download_end_in_range,
michael@0 150 test_multiple_downloads_in_range,
michael@0 151 test_no_downloads_in_range,
michael@0 152 test_active_download_in_range,
michael@0 153 test_observer_dispatched,
michael@0 154 ];
michael@0 155
michael@0 156 function run_test()
michael@0 157 {
michael@0 158 if (oldDownloadManagerDisabled()) {
michael@0 159 return;
michael@0 160 }
michael@0 161
michael@0 162 for (let i = 0; i < tests.length; i++) {
michael@0 163 dm.cleanUp();
michael@0 164 tests[i]();
michael@0 165 }
michael@0 166 }

mercurial