test_file_downloader.js
Go to the documentation of this file.
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4 //
5 // BEGIN SONGBIRD GPL
6 //
7 // This file is part of the Songbird web player.
8 //
9 // Copyright(c) 2005-2008 POTI, Inc.
10 // http://songbirdnest.com
11 //
12 // This file may be licensed under the terms of of the
13 // GNU General Public License Version 2 (the "GPL").
14 //
15 // Software distributed under the License is distributed
16 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
17 // express or implied. See the GPL for the specific language
18 // governing rights and limitations.
19 //
20 // You should have received a copy of the GPL along with this
21 // program. If not, go to http://www.gnu.org/licenses/gpl.html
22 // or write to the Free Software Foundation, Inc.,
23 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 // END SONGBIRD GPL
26 //
27  */
28 
34 //------------------------------------------------------------------------------
35 //------------------------------------------------------------------------------
36 //
37 // File downloader service unit tests.
38 //
39 //------------------------------------------------------------------------------
40 //------------------------------------------------------------------------------
41 
46 function runTest() {
47  // Start running the tests.
48  testFileDownloader.start();
49 }
50 
51 
56 var testFileDownloader = {
57  //
58  // File downloader tests configuration.
59  //
60  // succeedingURISpec A URI spec for a file that should succeed at
61  // downloading.
62  // failingURISpec A URI spec for a file that should fail at
63  // downloading.
64  // destinationFileExt Destination file extension.
65  //
66 
67  succeedingURISpec: "chrome://songbird/locale/songbird.properties",
68  failingURISpec: "http://nonexistent_file",
69  destinationFileExt: "tst",
70 
71 
72  //
73  // File downloader tests fields.
74  //
75  // _testList List of tests to run.
76  // _nextTestIndex Index of next test to run.
77  // _testPendingIndicated True if a pending test has been indicated.
78  // _allTestsComplete True if all tests have completed.
79  //
80 
81  _testList: null,
82  _nextTestIndex: 0,
83  _testPendingIndicated: false,
84  _allTestsComplete: false,
85 
86 
91  start: function testFileDownloader_start() {
92  var _this = this;
93  var func;
94 
95  // Initialize the list of tests.
96  this._testList = [];
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);
109 
110  // Start running the tests.
111  this._nextTestIndex = 0;
112  this._runNextTest();
113  },
114 
115 
120  _runNextTest: function testFileDownloader__runNextTest() {
121  // Run tests until complete or test is pending.
122  while (1) {
123  // Finish test if no more tests to run.
124  if (this._nextTestIndex >= this._testList.length) {
125  this._allTestsComplete = true;
126  if (this._testPendingIndicated) {
127  testFinished();
128  }
129  break;
130  }
131 
132  // Run the next test. Indicate if tests are pending.
133  if (!this._testList[this._nextTestIndex++]()) {
134  // If not all tests have completed and a pending test has not been
135  // indicated, indicate a pending test.
136  if (!this._allTestsComplete && !this._testPendingIndicated) {
137  this._testPendingIndicated = true;
138  testPending();
139  }
140  break;
141  }
142  }
143  },
144 
145 
150  _testExistence: function testFileDownloader__testExistence() {
151  // Test that the file downloader component is available.
152  var fileDownloader;
153  try {
154  fileDownloader = Cc["@songbirdnest.com/Songbird/FileDownloader;1"]
155  .createInstance(Ci.sbIFileDownloader);
156  } catch (ex) {}
157  assertTrue(fileDownloader,
158  "File downloader component is not available.");
159 
160  // Test is complete.
161  return true;
162  },
163 
164 
169  _testDownloadSucceeds: function testFileDownloader__testDownloadSucceeds() {
170  // Create a file downloader.
171  var fileDownloader = Cc["@songbirdnest.com/Songbird/FileDownloader;1"]
172  .createInstance(Ci.sbIFileDownloader);
173 
174  // Create a file download listener.
175  var tester = this;
176  var fileDownloaderListener = {
177  onProgress: function() {},
178 
179  onComplete: function() {
180  // Validate success.
181  assertTrue(fileDownloader.succeeded, "File download failed.");
182  assertTrue(fileDownloader.destinationFile,
183  "Destination file not available.");
184  assertTrue(fileDownloader.destinationFile.exists(),
185  "Destination file does not exist.");
186  assertEqual(fileDownloader.bytesToDownload,
187  fileDownloader.bytesDownloaded);
188  assertEqual(fileDownloader.percentComplete, 100);
189 
190  // Release file downloader.
191  fileDownloader.listener = null;
192  fileDownloader = null;
193 
194  // Run the next test.
195  tester._runNextTest();
196  }
197  };
198 
199  // Create the source URI.
200  var ioService = Cc["@mozilla.org/network/io-service;1"]
201  .getService(Ci.nsIIOService);
202  var sourceURI = ioService.newURI(this.succeedingURISpec, null, null);
203 
204  // Set up the file downloader.
205  fileDownloader.sourceURI = sourceURI;
206  fileDownloader.listener = fileDownloaderListener;
207 
208  // Start the file download.
209  fileDownloader.start();
210 
211  // Test is not complete.
212  return false;
213  },
214 
215 
220  _testDownloadWithURISpec:
221  function testFileDownloader__testDownloadWithURISpec() {
222  // Create a file downloader.
223  var fileDownloader = Cc["@songbirdnest.com/Songbird/FileDownloader;1"]
224  .createInstance(Ci.sbIFileDownloader);
225 
226  // Create a file download listener.
227  var tester = this;
228  var fileDownloaderListener = {
229  onProgress: function() {},
230 
231  onComplete: function() {
232  // Validate success.
233  assertTrue(fileDownloader.succeeded, "File download failed.");
234 
235  // Release file downloader.
236  fileDownloader.listener = null;
237  fileDownloader = null;
238 
239  // Run the next test.
240  tester._runNextTest();
241  }
242  };
243 
244  // Set up the file downloader.
245  fileDownloader.sourceURISpec = this.succeedingURISpec;
246  fileDownloader.listener = fileDownloaderListener;
247 
248  // Start the file download.
249  fileDownloader.start();
250 
251  // Test is not complete.
252  return false;
253  },
254 
255 
260  _testDownloadWithDstExt:
261  function testFileDownloader__testDownloadWithDstExt() {
262  // Create a file downloader.
263  var fileDownloader = Cc["@songbirdnest.com/Songbird/FileDownloader;1"]
264  .createInstance(Ci.sbIFileDownloader);
265 
266  // Create a file download listener.
267  var tester = this;
268  var fileDownloaderListener = {
269  onProgress: function() {},
270 
271  onComplete: function() {
272  // Validate success.
273  assertTrue(fileDownloader.succeeded, "File download failed.");
274 
275  // Get destination file extension.
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);
283 
284  // Validate file extension.
285  assertEqual(destinationFileURL.fileExtension,
286  tester.destinationFileExt);
287 
288  // Release file downloader.
289  fileDownloader.listener = null;
290  fileDownloader = null;
291 
292  // Run the next test.
293  tester._runNextTest();
294  }
295  };
296 
297  // Set up the file downloader.
298  fileDownloader.sourceURISpec = this.succeedingURISpec;
299  fileDownloader.listener = fileDownloaderListener;
300  fileDownloader.destinationFileExtension = this.destinationFileExt;
301 
302  // Start the file download.
303  fileDownloader.start();
304 
305  // Test is not complete.
306  return false;
307  },
308 
309 
314  _testDownloadFails: function testFileDownloader__testDownloadFails() {
315  // Create a file downloader.
316  var fileDownloader = Cc["@songbirdnest.com/Songbird/FileDownloader;1"]
317  .createInstance(Ci.sbIFileDownloader);
318 
319  // Create a file download listener.
320  var tester = this;
321  var fileDownloaderListener = {
322  onProgress: function() {},
323 
324  onComplete: function() {
325  // Validate failure.
326  assertTrue(!fileDownloader.succeeded, "File download succeeded.");
327 
328  // Release file downloader.
329  fileDownloader.listener = null;
330  fileDownloader = null;
331 
332  // Run the next test.
333  tester._runNextTest();
334  }
335  };
336 
337  // Create the source URI.
338  var ioService = Cc["@mozilla.org/network/io-service;1"]
339  .getService(Ci.nsIIOService);
340  var sourceURI = ioService.newURI(this.failingURISpec, null, null);
341 
342  // Set up the file downloader.
343  fileDownloader.sourceURI = sourceURI;
344  fileDownloader.listener = fileDownloaderListener;
345 
346  // Start the file download.
347  fileDownloader.start();
348 
349  // Test is not complete.
350  return false;
351  },
352 
353 
358  _testDownloadCancel: function testFileDownloader__testDownloadCancel() {
359  // Create a file downloader.
360  var fileDownloader = Cc["@songbirdnest.com/Songbird/FileDownloader;1"]
361  .createInstance(Ci.sbIFileDownloader);
362 
363  // Create a file download listener.
364  var tester = this;
365  var fileDownloaderListener = {
366  onProgress: function() {},
367 
368  onComplete: function() {
369  // Validate failure from cancel.
370  assertTrue(!fileDownloader.succeeded, "File download succeeded.");
371  assertTrue(!fileDownloader.destinationFile,
372  "Destination file not deleted.");
373 
374  // Release file downloader.
375  fileDownloader.listener = null;
376  fileDownloader = null;
377 
378  // Run the next test.
379  tester._runNextTest();
380  }
381  };
382 
383  // Create the source URI.
384  var ioService = Cc["@mozilla.org/network/io-service;1"]
385  .getService(Ci.nsIIOService);
386  var sourceURI = ioService.newURI(this.succeedingURISpec, null, null);
387 
388  // Set up the file downloader.
389  fileDownloader.sourceURI = sourceURI;
390  fileDownloader.listener = fileDownloaderListener;
391 
392  // Start and cancel the file download.
393  fileDownloader.start();
394  fileDownloader.cancel();
395 
396  // Test is not complete.
397  return false;
398  }
399 }
400 
function start(ch)
const Cc
function runTest()
Advanced DataRemote unit tests.
function onComplete(job)
Definition: test_bug7406.js:85
function testFinished()
function assertTrue(aTest, aMessage)
var ioService
function assertEqual(aExpected, aActual, aMessage)
var _this
return null
Definition: FeedWriter.js:1143
const Ci
function testPending()