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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/downloads/test/unit/test_removeDownloadsByTimeframe.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,166 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     1.5 + * vim: sw=2 ts=2 sts=2
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/**
    1.11 + * Test added with bug 462964 to test the behavior of the new API that was added
    1.12 + * to remove downloads by a given time frame.
    1.13 + */
    1.14 +
    1.15 +////////////////////////////////////////////////////////////////////////////////
    1.16 +//// Constants
    1.17 +
    1.18 +let dm = Cc["@mozilla.org/download-manager;1"].
    1.19 +         getService(Ci.nsIDownloadManager);
    1.20 +
    1.21 +const START_TIME = Date.now() * 1000;
    1.22 +const END_TIME = START_TIME + (60 * 1000000); // one minute range
    1.23 +
    1.24 +const DOWNLOAD_FINISHED = Ci.nsIDownloadManager.DOWNLOAD_FINISHED;
    1.25 +const DOWNLOAD_DOWNLOADING = Ci.nsIDownloadManager.DOWNLOAD_DOWNLOADING;
    1.26 +
    1.27 +const REMOVED_TOPIC = "download-manager-remove-download";
    1.28 +
    1.29 +////////////////////////////////////////////////////////////////////////////////
    1.30 +//// Utility Functions
    1.31 +
    1.32 +/**
    1.33 + * Adds a download to the database.
    1.34 + *
    1.35 + * @param aStartTimeInRange
    1.36 + *        Indicates if the new download's start time should be in range.
    1.37 + * @param aEndTimeInRange
    1.38 + *        Indicates if the new download's end time should be in range.
    1.39 + * @returns the inserted id.
    1.40 + */
    1.41 +let id = 1;
    1.42 +function add_download_to_db(aStartTimeInRange, aEndTimeInRange, aState)
    1.43 +{
    1.44 +  let db = dm.DBConnection;
    1.45 +  let stmt = db.createStatement(
    1.46 +    "INSERT INTO moz_downloads (id, startTime, endTime, state) " +
    1.47 +    "VALUES (:id, :startTime, :endTime, :state)"
    1.48 +  );
    1.49 +  stmt.params.id = id;
    1.50 +  stmt.params.startTime = aStartTimeInRange ? START_TIME + 1 : START_TIME - 1;
    1.51 +  stmt.params.endTime = aEndTimeInRange ? END_TIME - 1 : END_TIME + 1;
    1.52 +  stmt.params.state = aState;
    1.53 +  stmt.execute();
    1.54 +  stmt.finalize();
    1.55 +
    1.56 +  return id++;
    1.57 +}
    1.58 +
    1.59 +/**
    1.60 + * Checks to see the downloads with the specified id exist or not.
    1.61 + *
    1.62 + * @param aIDs
    1.63 + *        The ids of the downloads to check.
    1.64 + * @param aExpected
    1.65 + *        True if we expect the download to exist, false if we do not.
    1.66 + */
    1.67 +function check_existence(aIDs, aExpected)
    1.68 +{
    1.69 +  let db = dm.DBConnection;
    1.70 +  let stmt = db.createStatement(
    1.71 +    "SELECT * " +
    1.72 +    "FROM moz_downloads " +
    1.73 +    "WHERE id = :id"
    1.74 +  );
    1.75 +
    1.76 +  let checkFunc = aExpected ? do_check_true : do_check_false;
    1.77 +  for (let i = 0; i < aIDs.length; i++) {
    1.78 +    stmt.params.id = aIDs[i];
    1.79 +    checkFunc(stmt.executeStep());
    1.80 +    stmt.reset();
    1.81 +  }
    1.82 +  stmt.finalize();
    1.83 +}
    1.84 +
    1.85 +////////////////////////////////////////////////////////////////////////////////
    1.86 +//// Test Functions
    1.87 +
    1.88 +function test_download_start_in_range()
    1.89 +{
    1.90 +  let id = add_download_to_db(true, false, DOWNLOAD_FINISHED);
    1.91 +  dm.removeDownloadsByTimeframe(START_TIME, END_TIME);
    1.92 +  check_existence([id], false);
    1.93 +}
    1.94 +
    1.95 +function test_download_end_in_range()
    1.96 +{
    1.97 +  let id = add_download_to_db(false, true, DOWNLOAD_FINISHED);
    1.98 +  dm.removeDownloadsByTimeframe(START_TIME, END_TIME);
    1.99 +  check_existence([id], true);
   1.100 +}
   1.101 +
   1.102 +function test_multiple_downloads_in_range()
   1.103 +{
   1.104 +  let ids = [];
   1.105 +  ids.push(add_download_to_db(true, false, DOWNLOAD_FINISHED));
   1.106 +  ids.push(add_download_to_db(true, false, DOWNLOAD_FINISHED));
   1.107 +  dm.removeDownloadsByTimeframe(START_TIME, END_TIME);
   1.108 +  check_existence(ids, false);
   1.109 +}
   1.110 +
   1.111 +function test_no_downloads_in_range()
   1.112 +{
   1.113 +  let ids = [];
   1.114 +  ids.push(add_download_to_db(false, true, DOWNLOAD_FINISHED));
   1.115 +  ids.push(add_download_to_db(false, true, DOWNLOAD_FINISHED));
   1.116 +  dm.removeDownloadsByTimeframe(START_TIME, END_TIME);
   1.117 +  check_existence(ids, true);
   1.118 +}
   1.119 +
   1.120 +function test_active_download_in_range()
   1.121 +{
   1.122 +  let id = add_download_to_db(true, false, DOWNLOAD_DOWNLOADING);
   1.123 +  dm.removeDownloadsByTimeframe(START_TIME, END_TIME);
   1.124 +  check_existence([id], true);
   1.125 +}
   1.126 +
   1.127 +function test_observer_dispatched()
   1.128 +{
   1.129 +  let observer = {
   1.130 +    notified: false,
   1.131 +    observe: function(aSubject, aTopic, aData)
   1.132 +    {
   1.133 +      this.notified = true;
   1.134 +      do_check_eq(aSubject, null);
   1.135 +      do_check_eq(aTopic, REMOVED_TOPIC);
   1.136 +      do_check_eq(aData, null);
   1.137 +    }
   1.138 +  };
   1.139 +  let os = Cc["@mozilla.org/observer-service;1"].
   1.140 +           getService(Ci.nsIObserverService);
   1.141 +  os.addObserver(observer, REMOVED_TOPIC, false);
   1.142 +
   1.143 +  add_download_to_db(true, false, DOWNLOAD_FINISHED);
   1.144 +  dm.removeDownloadsByTimeframe(START_TIME, END_TIME);
   1.145 +  do_check_true(observer.notified);
   1.146 +
   1.147 +  os.removeObserver(observer, REMOVED_TOPIC);
   1.148 +}
   1.149 +
   1.150 +let tests = [
   1.151 +  test_download_start_in_range,
   1.152 +  test_download_end_in_range,
   1.153 +  test_multiple_downloads_in_range,
   1.154 +  test_no_downloads_in_range,
   1.155 +  test_active_download_in_range,
   1.156 +  test_observer_dispatched,
   1.157 +];
   1.158 +
   1.159 +function run_test()
   1.160 +{
   1.161 +  if (oldDownloadManagerDisabled()) {
   1.162 +    return;
   1.163 +  }
   1.164 +
   1.165 +  for (let i = 0; i < tests.length; i++) {
   1.166 +    dm.cleanUp();
   1.167 +    tests[i]();
   1.168 +  }
   1.169 +}

mercurial