Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* Any copyright is dedicated to the Public Domain.
4 * http://creativecommons.org/publicdomain/zero/1.0/ */
6 /**
7 * Tests the functions located directly in the "Downloads" object.
8 */
10 "use strict";
12 ////////////////////////////////////////////////////////////////////////////////
13 //// Tests
15 /**
16 * Tests that the createDownload function exists and can be called. More
17 * detailed tests are implemented separately for the DownloadCore module.
18 */
19 add_task(function test_createDownload()
20 {
21 // Creates a simple Download object without starting the download.
22 yield Downloads.createDownload({
23 source: { url: "about:blank" },
24 target: { path: getTempFile(TEST_TARGET_FILE_NAME).path },
25 saver: { type: "copy" },
26 });
27 });
29 /**
30 * Tests createDownload for private download.
31 */
32 add_task(function test_createDownload_private()
33 {
34 let download = yield Downloads.createDownload({
35 source: { url: "about:blank", isPrivate: true },
36 target: { path: getTempFile(TEST_TARGET_FILE_NAME).path },
37 saver: { type: "copy" }
38 });
39 do_check_true(download.source.isPrivate);
40 });
42 /**
43 * Tests createDownload for normal (public) download.
44 */
45 add_task(function test_createDownload_public()
46 {
47 let tempPath = getTempFile(TEST_TARGET_FILE_NAME).path;
48 let download = yield Downloads.createDownload({
49 source: { url: "about:blank", isPrivate: false },
50 target: { path: tempPath },
51 saver: { type: "copy" }
52 });
53 do_check_false(download.source.isPrivate);
55 download = yield Downloads.createDownload({
56 source: { url: "about:blank" },
57 target: { path: tempPath },
58 saver: { type: "copy" }
59 });
60 do_check_false(download.source.isPrivate);
61 });
63 /**
64 * Tests "fetch" with nsIURI and nsIFile as arguments.
65 */
66 add_task(function test_fetch_uri_file_arguments()
67 {
68 let targetFile = getTempFile(TEST_TARGET_FILE_NAME);
69 yield Downloads.fetch(NetUtil.newURI(httpUrl("source.txt")), targetFile);
70 yield promiseVerifyContents(targetFile.path, TEST_DATA_SHORT);
71 });
73 /**
74 * Tests "fetch" with DownloadSource and DownloadTarget as arguments.
75 */
76 add_task(function test_fetch_object_arguments()
77 {
78 let targetPath = getTempFile(TEST_TARGET_FILE_NAME).path;
79 yield Downloads.fetch({ url: httpUrl("source.txt") }, { path: targetPath });
80 yield promiseVerifyContents(targetPath, TEST_DATA_SHORT);
81 });
83 /**
84 * Tests "fetch" with string arguments.
85 */
86 add_task(function test_fetch_string_arguments()
87 {
88 let targetPath = getTempFile(TEST_TARGET_FILE_NAME).path;
89 yield Downloads.fetch(httpUrl("source.txt"), targetPath);
90 yield promiseVerifyContents(targetPath, TEST_DATA_SHORT);
92 targetPath = getTempFile(TEST_TARGET_FILE_NAME).path;
93 yield Downloads.fetch(new String(httpUrl("source.txt")),
94 new String(targetPath));
95 yield promiseVerifyContents(targetPath, TEST_DATA_SHORT);
96 });
98 /**
99 * Tests that the getList function returns the same list when called multiple
100 * times with the same argument, but returns different lists when called with
101 * different arguments. More detailed tests are implemented separately for the
102 * DownloadList module.
103 */
104 add_task(function test_getList()
105 {
106 let publicListOne = yield Downloads.getList(Downloads.PUBLIC);
107 let privateListOne = yield Downloads.getList(Downloads.PRIVATE);
109 let publicListTwo = yield Downloads.getList(Downloads.PUBLIC);
110 let privateListTwo = yield Downloads.getList(Downloads.PRIVATE);
112 do_check_eq(publicListOne, publicListTwo);
113 do_check_eq(privateListOne, privateListTwo);
115 do_check_neq(publicListOne, privateListOne);
116 });
118 /**
119 * Tests that the getSummary function returns the same summary when called
120 * multiple times with the same argument, but returns different summaries when
121 * called with different arguments. More detailed tests are implemented
122 * separately for the DownloadSummary object in the DownloadList module.
123 */
124 add_task(function test_getSummary()
125 {
126 let publicSummaryOne = yield Downloads.getSummary(Downloads.PUBLIC);
127 let privateSummaryOne = yield Downloads.getSummary(Downloads.PRIVATE);
129 let publicSummaryTwo = yield Downloads.getSummary(Downloads.PUBLIC);
130 let privateSummaryTwo = yield Downloads.getSummary(Downloads.PRIVATE);
132 do_check_eq(publicSummaryOne, publicSummaryTwo);
133 do_check_eq(privateSummaryOne, privateSummaryTwo);
135 do_check_neq(publicSummaryOne, privateSummaryOne);
136 });
138 /**
139 * Tests that the getSystemDownloadsDirectory returns a non-empty download
140 * directory string.
141 */
142 add_task(function test_getSystemDownloadsDirectory()
143 {
144 let downloadDir = yield Downloads.getSystemDownloadsDirectory();
145 do_check_neq(downloadDir, "");
146 });
148 /**
149 * Tests that the getPreferredDownloadsDirectory returns a non-empty download
150 * directory string.
151 */
152 add_task(function test_getPreferredDownloadsDirectory()
153 {
154 let downloadDir = yield Downloads.getPreferredDownloadsDirectory();
155 do_check_neq(downloadDir, "");
156 });
158 /**
159 * Tests that the getTemporaryDownloadsDirectory returns a non-empty download
160 * directory string.
161 */
162 add_task(function test_getTemporaryDownloadsDirectory()
163 {
164 let downloadDir = yield Downloads.getTemporaryDownloadsDirectory();
165 do_check_neq(downloadDir, "");
166 });
168 ////////////////////////////////////////////////////////////////////////////////
169 //// Termination
171 let tailFile = do_get_file("tail.js");
172 Services.scriptloader.loadSubScript(NetUtil.newURI(tailFile).spec);