toolkit/components/jsdownloads/test/unit/test_Downloads.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/jsdownloads/test/unit/test_Downloads.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,172 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=2 et sw=2 tw=80: */
     1.6 +/* Any copyright is dedicated to the Public Domain.
     1.7 + * http://creativecommons.org/publicdomain/zero/1.0/ */
     1.8 +
     1.9 +/**
    1.10 + * Tests the functions located directly in the "Downloads" object.
    1.11 + */
    1.12 +
    1.13 +"use strict";
    1.14 +
    1.15 +////////////////////////////////////////////////////////////////////////////////
    1.16 +//// Tests
    1.17 +
    1.18 +/**
    1.19 + * Tests that the createDownload function exists and can be called.  More
    1.20 + * detailed tests are implemented separately for the DownloadCore module.
    1.21 + */
    1.22 +add_task(function test_createDownload()
    1.23 +{
    1.24 +  // Creates a simple Download object without starting the download.
    1.25 +  yield Downloads.createDownload({
    1.26 +    source: { url: "about:blank" },
    1.27 +    target: { path: getTempFile(TEST_TARGET_FILE_NAME).path },
    1.28 +    saver: { type: "copy" },
    1.29 +  });
    1.30 +});
    1.31 +
    1.32 +/**
    1.33 + * Tests createDownload for private download.
    1.34 + */
    1.35 +add_task(function test_createDownload_private()
    1.36 +{
    1.37 +  let download = yield Downloads.createDownload({
    1.38 +    source: { url: "about:blank", isPrivate: true },
    1.39 +    target: { path: getTempFile(TEST_TARGET_FILE_NAME).path },
    1.40 +    saver: { type: "copy" }
    1.41 +  });
    1.42 +  do_check_true(download.source.isPrivate);
    1.43 +});
    1.44 +
    1.45 +/**
    1.46 + * Tests createDownload for normal (public) download.
    1.47 + */
    1.48 +add_task(function test_createDownload_public()
    1.49 +{
    1.50 +  let tempPath = getTempFile(TEST_TARGET_FILE_NAME).path;
    1.51 +  let download = yield Downloads.createDownload({
    1.52 +    source: { url: "about:blank", isPrivate: false },
    1.53 +    target: { path: tempPath },
    1.54 +    saver: { type: "copy" }
    1.55 +  });
    1.56 +  do_check_false(download.source.isPrivate);
    1.57 +
    1.58 +  download = yield Downloads.createDownload({
    1.59 +    source: { url: "about:blank" },
    1.60 +    target: { path: tempPath },
    1.61 +    saver: { type: "copy" }
    1.62 +  });
    1.63 +  do_check_false(download.source.isPrivate);
    1.64 +});
    1.65 +
    1.66 +/**
    1.67 + * Tests "fetch" with nsIURI and nsIFile as arguments.
    1.68 + */
    1.69 +add_task(function test_fetch_uri_file_arguments()
    1.70 +{
    1.71 +  let targetFile = getTempFile(TEST_TARGET_FILE_NAME);
    1.72 +  yield Downloads.fetch(NetUtil.newURI(httpUrl("source.txt")), targetFile);
    1.73 +  yield promiseVerifyContents(targetFile.path, TEST_DATA_SHORT);
    1.74 +});
    1.75 +
    1.76 +/**
    1.77 + * Tests "fetch" with DownloadSource and DownloadTarget as arguments.
    1.78 + */
    1.79 +add_task(function test_fetch_object_arguments()
    1.80 +{
    1.81 +  let targetPath = getTempFile(TEST_TARGET_FILE_NAME).path;
    1.82 +  yield Downloads.fetch({ url: httpUrl("source.txt") }, { path: targetPath });
    1.83 +  yield promiseVerifyContents(targetPath, TEST_DATA_SHORT);
    1.84 +});
    1.85 +
    1.86 +/**
    1.87 + * Tests "fetch" with string arguments.
    1.88 + */
    1.89 +add_task(function test_fetch_string_arguments()
    1.90 +{
    1.91 +  let targetPath = getTempFile(TEST_TARGET_FILE_NAME).path;
    1.92 +  yield Downloads.fetch(httpUrl("source.txt"), targetPath);
    1.93 +  yield promiseVerifyContents(targetPath, TEST_DATA_SHORT);
    1.94 +
    1.95 +  targetPath = getTempFile(TEST_TARGET_FILE_NAME).path;
    1.96 +  yield Downloads.fetch(new String(httpUrl("source.txt")),
    1.97 +                        new String(targetPath));
    1.98 +  yield promiseVerifyContents(targetPath, TEST_DATA_SHORT);
    1.99 +});
   1.100 +
   1.101 +/**
   1.102 + * Tests that the getList function returns the same list when called multiple
   1.103 + * times with the same argument, but returns different lists when called with
   1.104 + * different arguments.  More detailed tests are implemented separately for the
   1.105 + * DownloadList module.
   1.106 + */
   1.107 +add_task(function test_getList()
   1.108 +{
   1.109 +  let publicListOne = yield Downloads.getList(Downloads.PUBLIC);
   1.110 +  let privateListOne = yield Downloads.getList(Downloads.PRIVATE);
   1.111 +
   1.112 +  let publicListTwo = yield Downloads.getList(Downloads.PUBLIC);
   1.113 +  let privateListTwo = yield Downloads.getList(Downloads.PRIVATE);
   1.114 +
   1.115 +  do_check_eq(publicListOne, publicListTwo);
   1.116 +  do_check_eq(privateListOne, privateListTwo);
   1.117 +
   1.118 +  do_check_neq(publicListOne, privateListOne);
   1.119 +});
   1.120 +
   1.121 +/**
   1.122 + * Tests that the getSummary function returns the same summary when called
   1.123 + * multiple times with the same argument, but returns different summaries when
   1.124 + * called with different arguments.  More detailed tests are implemented
   1.125 + * separately for the DownloadSummary object in the DownloadList module.
   1.126 + */
   1.127 +add_task(function test_getSummary()
   1.128 +{
   1.129 +  let publicSummaryOne = yield Downloads.getSummary(Downloads.PUBLIC);
   1.130 +  let privateSummaryOne = yield Downloads.getSummary(Downloads.PRIVATE);
   1.131 +
   1.132 +  let publicSummaryTwo = yield Downloads.getSummary(Downloads.PUBLIC);
   1.133 +  let privateSummaryTwo = yield Downloads.getSummary(Downloads.PRIVATE);
   1.134 +
   1.135 +  do_check_eq(publicSummaryOne, publicSummaryTwo);
   1.136 +  do_check_eq(privateSummaryOne, privateSummaryTwo);
   1.137 +
   1.138 +  do_check_neq(publicSummaryOne, privateSummaryOne);
   1.139 +});
   1.140 +
   1.141 +/**
   1.142 + * Tests that the getSystemDownloadsDirectory returns a non-empty download
   1.143 + * directory string.
   1.144 + */
   1.145 +add_task(function test_getSystemDownloadsDirectory()
   1.146 +{
   1.147 +  let downloadDir = yield Downloads.getSystemDownloadsDirectory();
   1.148 +  do_check_neq(downloadDir, "");
   1.149 +});
   1.150 +
   1.151 +/**
   1.152 + * Tests that the getPreferredDownloadsDirectory returns a non-empty download
   1.153 + * directory string.
   1.154 + */
   1.155 +add_task(function test_getPreferredDownloadsDirectory()
   1.156 +{
   1.157 +  let downloadDir = yield Downloads.getPreferredDownloadsDirectory();
   1.158 +  do_check_neq(downloadDir, "");
   1.159 +});
   1.160 +
   1.161 +/**
   1.162 + * Tests that the getTemporaryDownloadsDirectory returns a non-empty download
   1.163 + * directory string.
   1.164 + */
   1.165 +add_task(function test_getTemporaryDownloadsDirectory()
   1.166 +{
   1.167 +  let downloadDir = yield Downloads.getTemporaryDownloadsDirectory();
   1.168 +  do_check_neq(downloadDir, "");
   1.169 +});
   1.170 +
   1.171 +////////////////////////////////////////////////////////////////////////////////
   1.172 +//// Termination
   1.173 +
   1.174 +let tailFile = do_get_file("tail.js");
   1.175 +Services.scriptloader.loadSubScript(NetUtil.newURI(tailFile).spec);

mercurial