test_mediaexportservice.js
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2009 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
28 Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
29 
30 
31 var TEST_FILES = newAppRelativeFile("testharness/mediaexportservice/files");
32 
33 
34 function runTest()
35 {
36  var controller = new TestController();
37  controller.startTest();
38 }
39 
40 //------------------------------------------------------------------------------
41 // Media export service controller
42 
43 function TestController()
44 {
45 }
46 
47 /*
48  Phases:
49  0 = Add tracks to main library
50  1 = Add playlist, smartplaylist, and add media items to simple playlist
51  2 = Delete playlist and smartplaylist.
52 */
53 
54 TestController.prototype =
55 {
56  _phase : 0,
57  _shutdownService : null,
58  _tracks : [],
59  _tracks2 : [],
60  _trackPaths : [],
61  _track2Paths : [],
62  _mainLibrary : null,
63  _playlist : null,
64  _smartPlaylist : null,
65 
66  // Make the test controller start the testing.
67  startTest: function() {
68  this._log("Starting mediaexport service test!");
69  this._mainLibrary = LibraryUtils.mainLibrary;
70 
71  // Create a the URI's for the tracks to add to the library.
72  this._tracks[0] = newTestFileURI("one.mp3").spec;
73  this._tracks[1] = newTestFileURI("two.mp3").spec;
74  this._tracks[2] = newTestFileURI("three.mp3").spec;
75 
76  // Create another set of fake mediaitems.
77  this._tracks2[0] = newTestFileURI("four.mp3").spec;
78  this._tracks2[1] = newTestFileURI("five.mp3").spec;
79  this._tracks2[2] = newTestFileURI("six.mp3").spec;
80 
81  // Get the file-system paths for the URI's created above.
82  var ioService = Cc["@mozilla.org/network/io-service;1"]
83  .getService(Ci.nsIIOService);
84  var fileProtocolHandler = ioService.getProtocolHandler("file")
85  .QueryInterface(Ci.nsIFileProtocolHandler);
86  for (var i = 0; i < this._tracks.length; i++) {
87  var curTrackURL = this._tracks[i];
88  this._trackPaths[i] =
89  fileProtocolHandler.getFileFromURLSpec(curTrackURL).path;
90  }
91 
92  for (var i = 0; i < this._tracks2.length; i++) {
93  var curTrackURL = this._tracks2[i];
94  this._track2Paths[i] =
95  fileProtocolHandler.getFileFromURLSpec(curTrackURL).path;
96  }
97 
98  // Make sure the tracks, playlist, and smart playlist are not currently
99  // in the library.
100  this._mainLibrary.clear();
101 
102  // Clean up an existing export tasks
104 
105  // Access the shutdown service via the |sbIShutdownJob| interface
106  this._shutdownService = Cc["@songbirdnest.com/media-export-service;1"]
107  .getService(Ci.sbIShutdownJob);
108  this._shutdownService.addJobProgressListener(this);
109 
110  // Finally, kick off the test.
111  this._runCurrentPhase();
112  testPending();
113  },
114 
115  // Run current phase helper
116  _runCurrentPhase: function() {
117  this._log("-> Phase " + this._phase + " BEGIN");
118 
119  // First, set the prefs depending on the export phase
120  switch (this._phase) {
121  case 0:
123  break;
124  case 1:
125  case 2:
127  }
128 
129  // Next, incrementally add items to the library depending on the task to
130  // test that all of the data is getting written out to the task file.
131  switch (this._phase) {
132  case 0:
133  this._addTracks(this._mainLibrary);
134  break;
135 
136  case 1:
137  // Test playlist add and playlist mediaitem add
138  this._addPlaylist();
139  this._addSmartPlaylist();
140  this._addTracks(this._playlist);
141  this._addTracks2(this._mainLibrary);
142  break;
143 
144  case 2:
145  // Test playlist removal
146  this._removePlaylist();
147  this._removeSmartPlaylist();
148  break;
149  }
150 
151  this._invokeExportService();
152  },
153 
154  // Validate the exported data based on the current phase.
155  _validateExportData: function() {
156  this._log("-> Phase " + this._phase + " VALIDATING...");
157 
158  var shouldContinue = true;
159 
160  var taskFile = getExportedTaskFile();
161  var parsedTask = new TaskFileDataParser(taskFile);
162 
163  this._logTaskData(parsedTask);
164 
165  switch (this._phase) {
166  case 0:
167  // There shouldn't be any added media lists.
168  assertEqual(parsedTask.getAddedMediaLists().length, 0);
169 
170  // There shouldn't be any removed media lists.
171  assertEqual(parsedTask.getRemovedMediaLists().length, 0);
172 
173  // Ensure the added tracks to the main library
174  var addedLibraryItems = parsedTask.getAddedMediaItems()["#####SONGBIRD_MAIN_LIBRRAY#####"];
175  assertTrue(addedLibraryItems);
176 
177  // There should only be three added media items.
178  assertEqual(addedLibraryItems.length, 3);
179  assertEqual(addedLibraryItems[0], this._trackPaths[0]);
180  assertEqual(addedLibraryItems[1], this._trackPaths[1]);
181  assertEqual(addedLibraryItems[2], this._trackPaths[2]);
182  break;
183 
184  case 1:
185  // Ensure that the 2 playlists were added and that the 3 mediaitems
186  // have been added to the simple playlist.
187  var addedMediaLists = parsedTask.getAddedMediaLists();
188  assertEqual(addedMediaLists.length, 2);
189  assertEqual(addedMediaLists[0], this._playlist.name);
190  assertEqual(addedMediaLists[1], this._smartPlaylist.name);
191 
192  // Should be no removed playlists.
193  var removedLists = parsedTask.getRemovedMediaLists();
194  assertEqual(removedLists.length, 0);
195 
196  // Should be 3 added mediaitems to |this._playlist|.
197  var addedMediaItems =
198  parsedTask.getAddedMediaItems()[this._playlist.name];
199  assertEqual(addedMediaItems.length, 3);
200 
201  assertEqual(addedMediaItems[0], this._trackPaths[0]);
202  assertEqual(addedMediaItems[1], this._trackPaths[1]);
203  assertEqual(addedMediaItems[2], this._trackPaths[2]);
204 
205  // Should be 3 added mediaitems to |this._mainLibrary|.
206  addedMediaItems =
207  parsedTask.getAddedMediaItems()["#####SONGBIRD_MAIN_LIBRRAY#####"];
208  assertEqual(addedMediaItems.length, 3);
209 
210  assertEqual(addedMediaItems[0], this._track2Paths[0]);
211  assertEqual(addedMediaItems[1], this._track2Paths[1]);
212  assertEqual(addedMediaItems[2], this._track2Paths[2]);
213  break;
214 
215  case 2:
216  // There shouldn't be any added media lists.
217  assertEqual(parsedTask.getAddedMediaLists().length, 0);
218 
219  // There should be 2 removed media lists.
220  var removedMediaLists = parsedTask.getRemovedMediaLists();
221  assertEqual(removedMediaLists.length, 2);
222 
223  assertEqual(removedMediaLists[0], this._playlist.name);
224  assertEqual(removedMediaLists[1], this._smartPlaylist.name);
225 
226  // There shouldn't be any added media items.
227  var hasAddedContent = false;
228  for (var item in parsedTask.getAddedMediaItems()) {
229  hasAddedContent = true;
230  this._log("FOUND " + item + " as an added mediaitem ERROR!!!!");
231  }
232  assertFalse(hasAddedContent);
233 
234  // Shutdown the unit test now.
235  shouldContinue = false;
236  break;
237  }
238 
239  this._log("-> Phase " + this._phase + " FINISHED");
240 
241  // Don't hang on to this file.
242  taskFile.remove(false);
243  parsedTask = null;
244  sleep(1000);
245 
246  if (shouldContinue) {
247  this._phase++;
248  this._runCurrentPhase();
249  }
250  else {
251  // clean up
253 
254  // drop references
255  this._shutdownService.removeJobProgressListener(this);
256  this._shutdownService = null;
257 
258  this._log("Test finished!");
259  testFinished();
260  }
261  },
262 
263  // Start the export service via the shutdown job interface.
264  _invokeExportService: function() {
265  this._log("Export service needs to run = " +
266  this._shutdownService.needsToRunTask);
267  assertTrue(this._shutdownService.needsToRunTask);
268  this._shutdownService.startTask();
269  },
270 
271  // sbIJobProgressListener
272  onJobProgress: function(aJobProgress) {
273  if (aJobProgress.status == Ci.sbIJobProgress.STATUS_SUCCEEDED) {
274  this._log("Current job has STATUS_SUCCEEDED");
275  this._validateExportData();
276  }
277  },
278 
279  // Add/Remove tracks helpers
280  _addTracks: function(aTargetMediaList) {
281  for (var i = 0; i < this._tracks.length; i++) {
282  var mediaItem =
283  this._mainLibrary.createMediaItem(newURI(this._tracks[i]));
284  aTargetMediaList.add(mediaItem);
285  this._log("Added '" + this._tracks[i] + "'' (" +
286  mediaItem.guid + ") to '" + aTargetMediaList.name + "'");
287  }
288  },
289 
290  // Add tracks from |this._tracks2|
291  _addTracks2: function(aTargetMediaList) {
292  for (var i = 0; i < this._tracks2.length; i++) {
293  var mediaItem =
294  this._mainLibrary.createMediaItem(newURI(this._tracks2[i]));
295  aTargetMediaList.add(mediaItem);
296  this._log("Added '" + this._tracks2[i] + "'' (" +
297  mediaItem.guid + ") to '" + aTargetMediaList.name + "'");
298  }
299  },
300 
301  _removeTracks: function() {
302  // |clearItems()| doesn't remove any medialists.
303  this._mainLibrary.clearItems();
304  this._log("Cleared the main library of mediaitems");
305  },
306 
307  // Add/Remove playlists helpers
308  _addPlaylist: function() {
309  this._playlist = this._mainLibrary.createMediaList("simple");
310  this._playlist.name = "TEST PLAYLIST";
311  },
312 
313  _removePlaylist: function() {
314  if (this._playlist) {
315  this._mainLibrary.remove(this._playlist);
316  }
317  },
318 
319  // Add/Remove smart playlists helpers
320  _addSmartPlaylist: function() {
321  this._smartPlaylist = this._mainLibrary.createMediaList("smart");
322  this._smartPlaylist.name = "TEST SMARTPLAYLIST";
323  },
324 
325  _removeSmartPlaylist: function() {
326  if (this._smartPlaylist) {
327  this._mainLibrary.remove(this._smartPlaylist);
328  }
329  },
330 
331  // Task file logging helper
332  _logTaskData: function(aTaskData)
333  {
334  var addedMediaLists = "ADDED MEDIALISTS:";
335  var removedMediaLists = "REMOVED MEDIALISTS";
336  var addedMediaItems = "ADDED MEDIAITEMS";
337 
338  for (var i = 0; i < aTaskData.getAddedMediaLists().length; i++) {
339  addedMediaLists += "\n - " + aTaskData.getAddedMediaLists()[i];
340  }
341  for (var i = 0; i < aTaskData.getRemovedMediaLists().length; i++) {
342  removedMediaLists += "\n - " + aTaskData.getRemovedMediaLists()[i];
343  }
344  for (var curMediaListName in aTaskData.getAddedMediaItems()) {
345  addedMediaItems += "\n - " + curMediaListName;
346 
347  var curListArray = aTaskData.getAddedMediaItems()[curMediaListName];
348  for (var j = 0; j < curListArray.length; j++) {
349  addedMediaItems += "\n * " + curListArray[j];
350  }
351  }
352 
353  this._log(addedMediaLists);
354  this._log(removedMediaLists);
355  this._log(addedMediaItems);
356  },
357 
358  // Logging helper
359  _log: function(aString) {
360  dump("----------------------------------------------------------\n");
361  dump(" " + aString + "\n");
362  dump("----------------------------------------------------------\n");
363  },
364 
365  QueryInterface: XPCOMUtils.generateQI([Ci.sbIJobProgressListener])
366 };
367 
function setExportNothing()
function sleep(ms, suppressOutput)
const Cc
function newAppRelativeFile(path)
function getExportedTaskFile()
function testFinished()
sbOSDControlService prototype QueryInterface
function assertTrue(aTest, aMessage)
var ioService
function assertEqual(aExpected, aActual, aMessage)
function TestController()
function TaskFileDataParser(aDataFile)
var TEST_FILES
function removeAllTaskFiles()
function startTest()
function setExportTracksPlaylistsSmartPlaylists()
return null
Definition: FeedWriter.js:1143
function newURI(aURLString)
function assertFalse(aTest, aMessage)
function _log(aMsg, aMenuID)
function newTestFileURI(aFileName)
function setExportTracksOnly()
function runTest()
Advanced DataRemote unit tests.
const Ci
Javascript wrappers for common library tasks.
_getSelectedPageStyle s i
function testPending()