test_media_item_download_service.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  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2010 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
32 //------------------------------------------------------------------------------
33 //------------------------------------------------------------------------------
34 //
35 // Media item download service unit tests.
36 //
37 //------------------------------------------------------------------------------
38 //------------------------------------------------------------------------------
39 
44 function runTest() {
45  // Start running the tests.
47 }
48 
49 
55  //
56  // Media item download service tests configuration.
57  //
58  // succeedingURISpec A URI spec for a file that should succeed at
59  // downloading. This spec must be relative to the
60  // test harness directory.
61  // failingURISpec A URI spec for a file that should fail at
62  // downloading. This spec must be relative to the
63  // test harness directory.
64  // noDownloaderURISpec An absolute URI spec that should not have a
65  // matching downloader.
66  //
67 
68  succeedingURISpec: "test1.mp3",
69  failingURISpec: "nonexistent_file",
70  noDownloaderURISpec: "about:config",
71 
72 
73  //
74  // Media item download service tests fields.
75  //
76  // _testList List of tests to run.
77  // _nextTestIndex Index of next test to run.
78  // _testPendingIndicated True if a pending test has been indicated.
79  // _allTestsComplete True if all tests have completed.
80  // _server HTTP server.
81  // _serverPort HTTP server port.
82  //
83 
84  _testList: null,
85  _nextTestIndex: 0,
86  _testPendingIndicated: false,
87  _allTestsComplete: false,
88  _server: null,
89  _serverPort: -1,
90 
91 
96  start: function testMediaItemDownloadService_start() {
97  let self = this;
98 
99  // Initialize the list of tests.
100  this._testList = [];
101  this._testList.push(function() { return self._testDownload(); });
102  this._testList.push(function() { return self._testDownloadFailure(); });
103  this._testList.push(function()
104  { return self._testNoMatchingDownloader(); });
105 
106  // Set up a test server.
107  this._serverPort = getTestServerPortNumber();
108  this._server = Cc["@mozilla.org/server/jshttp;1"]
109  .createInstance(Ci.nsIHttpServer);
110  this._server.start(this._serverPort);
111  this._server.registerDirectory("/", getTestFile("."));
112 
113  // Start running the tests.
114  this._nextTestIndex = 0;
115  this._runNextTest();
116  },
117 
118 
123  _finish: function testMediaItemDownloadService__finish() {
124  // Mark all tests complete.
125  this._allTestsComplete = true;
126 
127  // Indicate that the test has finished if a pending test has been indicated.
128  if (this._testPendingIndicated) {
129  testFinished();
130  }
131 
132  // Stop test server.
133  this._server.stop(function() {});
134  },
135 
136 
141  _runNextTest: function testMediaItemDownloadService__runNextTest() {
142  // Run tests until complete or test is pending.
143  while (1) {
144  // Finish test if no more tests to run.
145  if (this._nextTestIndex >= this._testList.length) {
146  this._finish();
147  break;
148  }
149 
150  // Run the next test. Indicate if tests are pending.
151  if (!this._testList[this._nextTestIndex++]()) {
152  // If not all tests have completed and a pending test has not been
153  // indicated, indicate a pending test.
154  if (!this._allTestsComplete && !this._testPendingIndicated) {
155  this._testPendingIndicated = true;
156  testPending();
157  }
158  break;
159  }
160  }
161  },
162 
163 
168  _testDownload: function testMediaItemDownloadService__testDownload() {
169  // Get the media item download service.
170  let mediaItemDownloadService =
171  Cc["@songbirdnest.com/Songbird/MediaItemDownloadService;1"]
172  .getService(Ci.sbIMediaItemDownloadService);
173 
174  // Create a test media item to download.
175  let library = createLibrary("test_mediaitemdownload", null, false);
176  let uri = newURI("http://localhost:" + this._serverPort +
177  "/" + this.succeedingURISpec);
178  let mediaItem = library.createMediaItem(uri);
179 
180  // Get a downloader for the media item.
181  let downloader;
182  try {
183  downloader = mediaItemDownloadService.getDownloader(mediaItem, null);
184  } catch(ex) {
185  doFail("Could not get a media item downloader: " + ex);
186  }
187 
188  // Get the download size.
189  let downloadSize = downloader.getDownloadSize(mediaItem, null);
190  assertEqual(downloadSize, 17392);
191 
192  // Create download job.
193  let downloadJob;
194  try {
195  downloadJob = downloader.createDownloadJob(mediaItem, null);
196  } catch(ex) {
197  doFail("Could not create media item download job: " + ex);
198  }
199 
200  // Add a download job progress listener.
201  let tester = this;
202  let listener = {
203  onJobProgress: function() {
204  if ((downloadJob.status == Ci.sbIJobProgress.STATUS_FAILED) ||
205  (downloadJob.status == Ci.sbIJobProgress.STATUS_SUCCEEDED)) {
206  assertEqual(downloadJob.status, Ci.sbIJobProgress.STATUS_SUCCEEDED);
207  assertEqual(downloadJob.downloadedFile.fileSize, downloadSize);
208  downloadJob.removeJobProgressListener(this);
209  tester._runNextTest();
210  }
211  }
212  };
213  downloadJob.addJobProgressListener(listener);
214 
215  // Start downloading.
216  try {
217  downloadJob.start();
218  } catch(ex) {
219  doFail("Could not start media item download: " + ex);
220  }
221 
222  // Test is pending.
223  return false;
224  },
225 
226 
231  _testDownloadFailure:
232  function testMediaItemDownloadService__testDownloadFailure() {
233  // Get the media item download service.
234  let mediaItemDownloadService =
235  Cc["@songbirdnest.com/Songbird/MediaItemDownloadService;1"]
236  .getService(Ci.sbIMediaItemDownloadService);
237 
238  // Create a test media item to download.
239  let library = createLibrary("test_mediaitemdownload", null, false);
240  let uri = newURI("http://localhost:" + this._serverPort +
241  "/" + this.failingURISpec);
242  let mediaItem = library.createMediaItem(uri);
243 
244  // Get a downloader for the media item.
245  let downloader;
246  try {
247  downloader = mediaItemDownloadService.getDownloader(mediaItem, null);
248  } catch(ex) {
249  doFail("Could not get a media item downloader: " + ex);
250  }
251 
252  // Create download job.
253  let downloadJob;
254  try {
255  downloadJob = downloader.createDownloadJob(mediaItem, null);
256  } catch(ex) {
257  doFail("Could not create media item download job: " + ex);
258  }
259 
260  // Add a download job progress listener. Test that the download fails.
261  let tester = this;
262  let listener = {
263  onJobProgress: function() {
264  if ((downloadJob.status == Ci.sbIJobProgress.STATUS_FAILED) ||
265  (downloadJob.status == Ci.sbIJobProgress.STATUS_SUCCEEDED)) {
266  assertEqual(downloadJob.status, Ci.sbIJobProgress.STATUS_FAILED);
267  downloadJob.removeJobProgressListener(this);
268  tester._runNextTest();
269  }
270  }
271  };
272  downloadJob.addJobProgressListener(listener);
273 
274  // Start downloading.
275  try {
276  downloadJob.start();
277  } catch(ex) {
278  doFail("Could not start media item download: " + ex);
279  }
280 
281  // Test is pending.
282  return false;
283  },
284 
285 
290  _testNoMatchingDownloader:
291  function testMediaItemDownloadService__testNoMatchingDownloader() {
292  // Get the media item download service.
293  let mediaItemDownloadService =
294  Cc["@songbirdnest.com/Songbird/MediaItemDownloadService;1"]
295  .getService(Ci.sbIMediaItemDownloadService);
296 
297  // Create a test media item to download.
298  let library = createLibrary("test_mediaitemdownload", null, false);
299  let uri = newURI(this.noDownloaderURISpec);
300  let mediaItem = library.createMediaItem(uri);
301 
302  // Get a downloader for the media item. Test that no matching downloaders
303  // were found.
304  let downloader;
305  try {
306  downloader = mediaItemDownloadService.getDownloader(mediaItem, null);
307  } catch(ex) {
308  doFail("Get media item downloader exception: " + ex);
309  }
310  assertTrue(!downloader);
311 
312  // Test is not pending.
313  return true;
314  }
315 };
316 
function start(ch)
const Cc
function testFinished()
function assertTrue(aTest, aMessage)
function assertEqual(aExpected, aActual, aMessage)
function getTestFile(aFileName)
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function newURI(aURLString)
var uri
Definition: FeedWriter.js:1135
let testMediaItemDownloadService
function runTest()
Advanced DataRemote unit tests.
const Ci
function doFail(text)
function getTestServerPortNumber()
function testPending()