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.

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

mercurial