48 testFileDownloader.start();
56 var testFileDownloader = {
67 succeedingURISpec:
"chrome://songbird/locale/songbird.properties",
68 failingURISpec:
"http://nonexistent_file",
69 destinationFileExt:
"tst",
83 _testPendingIndicated:
false,
84 _allTestsComplete:
false,
91 start:
function testFileDownloader_start() {
97 func =
function() {
return _this._testExistence(); };
98 this._testList.push(func);
99 func =
function() {
return _this._testDownloadSucceeds(); };
100 this._testList.push(func);
101 func =
function() {
return _this._testDownloadWithURISpec(); };
102 this._testList.push(func);
103 func =
function() {
return _this._testDownloadWithDstExt(); };
104 this._testList.push(func);
105 func =
function() {
return _this._testDownloadFails(); };
106 this._testList.push(func);
107 func =
function() {
return _this._testDownloadCancel(); };
108 this._testList.push(func);
111 this._nextTestIndex = 0;
120 _runNextTest:
function testFileDownloader__runNextTest() {
124 if (this._nextTestIndex >= this._testList.length) {
125 this._allTestsComplete =
true;
126 if (this._testPendingIndicated) {
133 if (!this._testList[this._nextTestIndex++]()) {
136 if (!this._allTestsComplete && !this._testPendingIndicated) {
137 this._testPendingIndicated =
true;
150 _testExistence:
function testFileDownloader__testExistence() {
154 fileDownloader =
Cc[
"@songbirdnest.com/Songbird/FileDownloader;1"]
155 .createInstance(
Ci.sbIFileDownloader);
158 "File downloader component is not available.");
169 _testDownloadSucceeds:
function testFileDownloader__testDownloadSucceeds() {
171 var fileDownloader =
Cc[
"@songbirdnest.com/Songbird/FileDownloader;1"]
172 .createInstance(
Ci.sbIFileDownloader);
176 var fileDownloaderListener = {
177 onProgress:
function() {},
181 assertTrue(fileDownloader.succeeded,
"File download failed.");
183 "Destination file not available.");
184 assertTrue(fileDownloader.destinationFile.exists(),
185 "Destination file does not exist.");
187 fileDownloader.bytesDownloaded);
191 fileDownloader.listener =
null;
192 fileDownloader =
null;
195 tester._runNextTest();
200 var
ioService =
Cc[
"@mozilla.org/network/io-service;1"]
201 .getService(
Ci.nsIIOService);
202 var sourceURI = ioService.newURI(this.succeedingURISpec,
null,
null);
205 fileDownloader.sourceURI = sourceURI;
206 fileDownloader.listener = fileDownloaderListener;
209 fileDownloader.start();
220 _testDownloadWithURISpec:
221 function testFileDownloader__testDownloadWithURISpec() {
223 var fileDownloader =
Cc[
"@songbirdnest.com/Songbird/FileDownloader;1"]
224 .createInstance(
Ci.sbIFileDownloader);
228 var fileDownloaderListener = {
229 onProgress:
function() {},
233 assertTrue(fileDownloader.succeeded,
"File download failed.");
236 fileDownloader.listener =
null;
237 fileDownloader =
null;
240 tester._runNextTest();
245 fileDownloader.sourceURISpec = this.succeedingURISpec;
246 fileDownloader.listener = fileDownloaderListener;
249 fileDownloader.start();
260 _testDownloadWithDstExt:
261 function testFileDownloader__testDownloadWithDstExt() {
263 var fileDownloader =
Cc[
"@songbirdnest.com/Songbird/FileDownloader;1"]
264 .createInstance(
Ci.sbIFileDownloader);
268 var fileDownloaderListener = {
269 onProgress:
function() {},
273 assertTrue(fileDownloader.succeeded,
"File download failed.");
276 var ioService =
Cc[
"@mozilla.org/network/io-service;1"]
277 .getService(
Ci.nsIIOService);
278 var fileProtocolHandler = ioService.getProtocolHandler(
"file")
279 .QueryInterface(
Ci.nsIFileProtocolHandler);
280 var destinationFileURL =
281 fileProtocolHandler.newFileURI(fileDownloader.destinationFile);
282 destinationFileURL = destinationFileURL.QueryInterface(
Ci.nsIURL);
286 tester.destinationFileExt);
289 fileDownloader.listener =
null;
290 fileDownloader =
null;
293 tester._runNextTest();
298 fileDownloader.sourceURISpec = this.succeedingURISpec;
299 fileDownloader.listener = fileDownloaderListener;
300 fileDownloader.destinationFileExtension = this.destinationFileExt;
303 fileDownloader.start();
314 _testDownloadFails:
function testFileDownloader__testDownloadFails() {
316 var fileDownloader =
Cc[
"@songbirdnest.com/Songbird/FileDownloader;1"]
317 .createInstance(
Ci.sbIFileDownloader);
321 var fileDownloaderListener = {
322 onProgress:
function() {},
326 assertTrue(!fileDownloader.succeeded,
"File download succeeded.");
329 fileDownloader.listener =
null;
330 fileDownloader =
null;
333 tester._runNextTest();
338 var ioService =
Cc[
"@mozilla.org/network/io-service;1"]
339 .getService(
Ci.nsIIOService);
340 var sourceURI = ioService.newURI(this.failingURISpec,
null,
null);
343 fileDownloader.sourceURI = sourceURI;
344 fileDownloader.listener = fileDownloaderListener;
347 fileDownloader.start();
358 _testDownloadCancel:
function testFileDownloader__testDownloadCancel() {
360 var fileDownloader =
Cc[
"@songbirdnest.com/Songbird/FileDownloader;1"]
361 .createInstance(
Ci.sbIFileDownloader);
365 var fileDownloaderListener = {
366 onProgress:
function() {},
370 assertTrue(!fileDownloader.succeeded,
"File download succeeded.");
372 "Destination file not deleted.");
375 fileDownloader.listener =
null;
376 fileDownloader =
null;
379 tester._runNextTest();
384 var ioService =
Cc[
"@mozilla.org/network/io-service;1"]
385 .getService(
Ci.nsIIOService);
386 var sourceURI = ioService.newURI(this.succeedingURISpec,
null,
null);
389 fileDownloader.sourceURI = sourceURI;
390 fileDownloader.listener = fileDownloaderListener;
393 fileDownloader.start();
394 fileDownloader.cancel();
function runTest()
Advanced DataRemote unit tests.
function assertTrue(aTest, aMessage)
function assertEqual(aExpected, aActual, aMessage)