test_simplemedialist.js
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
29 Components.utils.import("resource://app/jsmodules/ArrayConverter.jsm");
30 
31 function countItems(enumerator) {
32  var count = 0;
33  while (enumerator.hasMoreElements()) {
34  var item = enumerator.getNext().QueryInterface(Ci.sbIMediaItem);
35  assertNotEqual(item, null);
36  count++;
37  }
38  return count;
39 }
40 
41 function runTest () {
42 
43  var databaseGUID = "test_simplemedialist";
44  var library = createLibrary(databaseGUID);
45 
46  var list = library.getMediaItem("7e8dcc95-7a1d-4bb3-9b14-d4906a9952cb");
47  assertList(list, "data_sort_sml101_ordinal_asc.txt");
48 
49  var item = list.getItemByIndex(0);
50  assertEqual(item.guid, "3E586C1A-AD99-11DB-9321-C22AB7121F49");
51 
52  // Test contains
53  item = library.getMediaItem("3E586C1A-AD99-11DB-9321-C22AB7121F49");
54  var contains = list.contains(item);
55  assertEqual(contains, true);
56 
57  item = library.getMediaItem("3E6DD1C2-AD99-11DB-9321-C22AB7121F49");
58  contains = list.contains(item);
59  assertEqual(contains, false);
60 
61  var titleProperty = "http://songbirdnest.com/data/1.0#trackName";
62  var albumProperty = "http://songbirdnest.com/data/1.0#albumName";
63  var genreProperty = "http://songbirdnest.com/data/1.0#genre";
64 
65  // Test getItemsByProperty(s)
66  var enumerationListener = new TestMediaListEnumerationListener();
67 
68  list.enumerateItemsByProperty(titleProperty, "Train of Thought",
69  enumerationListener,
70  Ci.sbIMediaList.ENUMERATIONTYPE_LOCKING);
71  assertEqual(enumerationListener.count, 1);
72  enumerationListener.reset();
73 
74  list.enumerateItemsByProperty(albumProperty, "Back in Black",
75  enumerationListener,
76  Ci.sbIMediaList.ENUMERATIONTYPE_LOCKING);
77  assertEqual(enumerationListener.count, 10);
78  enumerationListener.reset();
79 
80  list.enumerateItemsByProperty(genreProperty, "KJaskjjbfjJDBs",
81  enumerationListener,
82  Ci.sbIMediaList.ENUMERATIONTYPE_LOCKING);
83  assertEqual(enumerationListener.count, 0);
84  enumerationListener.reset();
85 
86  var propertyArray =
87  Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"].
88  createInstance(Ci.sbIMutablePropertyArray);
89  propertyArray.appendProperty(albumProperty, "Back in Black");
90 
91  list.enumerateItemsByProperties(propertyArray, enumerationListener,
92  Ci.sbIMediaList.ENUMERATIONTYPE_LOCKING);
93  assertEqual(enumerationListener.count, 10);
94  enumerationListener.reset();
95 
96  propertyArray.appendProperty(titleProperty, "Rock and Roll Ain't Noise Pollution");
97  propertyArray.appendProperty(titleProperty, "Shake a Leg");
98 
99  list.enumerateItemsByProperties(propertyArray, enumerationListener,
100  Ci.sbIMediaList.ENUMERATIONTYPE_LOCKING);
101  assertEqual(enumerationListener.count, 2);
102  enumerationListener.reset();
103 
104  propertyArray.removeElementAt(1);
105 
106  list.enumerateItemsByProperties(propertyArray, enumerationListener,
107  Ci.sbIMediaList.ENUMERATIONTYPE_LOCKING);
108  assertEqual(enumerationListener.count, 1);
109  enumerationListener.reset();
110 
111  //Test getIemByIndex, indexOf, lastIndexOf.
112  var mediaItem = list.getItemByIndex(8);
113  assertNotEqual(mediaItem, null);
114 
115  var mediaItemIndex = list.indexOf(mediaItem, 0);
116  assertEqual(mediaItemIndex, 8);
117 
118  var indexOfException;
119  try {
120  mediaItemIndex = list.indexOf(mediaItem, 10);
121  } catch (e) {
122  indexOfException = e;
123  }
124  assertEqual(indexOfException.result, Cr.NS_ERROR_NOT_AVAILABLE);
125 
126  var indexOfException2;
127  try {
128  mediaItemIndex = list.indexOf(mediaItem, 45);
129  } catch (e) {
130  indexOfException2 = e;
131  }
132  assertEqual(indexOfException2.result, Cr.NS_ERROR_INVALID_ARG);
133 
134  var indexOfException3;
135  try {
136  mediaItemIndex = list.indexOf(mediaItem);
137  } catch (e) {
138  indexOfException3 = e;
139  assertEqual(indexOfException3.result, Cr.NS_ERROR_INVALID_ARG);
140  }
141 
142  mediaItemIndex = list.lastIndexOf(mediaItem, 0);
143  assertEqual(mediaItemIndex, 8);
144 
145  var lastIndexOfException;
146  try {
147  mediaItemIndex = list.lastIndexOf(mediaItem, 10);
148  } catch (e) {
149  lastIndexOfException = e;
150  }
151  assertEqual(lastIndexOfException.result, Cr.NS_ERROR_NOT_AVAILABLE);
152 
153  var lastIndexOfException2;
154  try {
155  mediaItemIndex = list.lastIndexOf(mediaItem, 45);
156  } catch (e) {
157  lastIndexOfException2 = e;
158  }
159  assertEqual(lastIndexOfException2.result, Cr.NS_ERROR_INVALID_ARG);
160 
161  //Test add, addSome, addAll
162  var data = readList("data_sort_sml101_ordinal_asc.txt");
163 
164  list = library.getMediaItem("7e8dcc95-7a1d-4bb3-9b14-d4906a9952cb");
165  item = library.getMediaItem("3E6DD1C2-AD99-11DB-9321-C22AB7121F49");
166  var oldlength = list.length;
167 
168  list.add(item);
169  data.push(item.guid);
170  assertEqual(list.contains(item), true);
171  assertEqual(list.length, oldlength + 1);
172  assertList(list, data);
173 
174  list.add(item);
175  data.push(item.guid);
176  assertEqual(list.contains(item), true);
177  assertEqual(list.length, oldlength + 2);
178  assertList(list, data);
179 
180  list.add(list);
181  data.push(list.guid);
182  assertEqual(list.contains(item), true);
183  assertEqual(list.length, oldlength + 3);
184  assertList(list, data);
185 
186  // Add the entire database to the list
187  var view = library;
188  var a = readList("data_sort_created_asc.txt");
189 
190  list.addAll(view);
191  a.forEach(function(e) { data.push(e); });
192  assertList(list, data);
193  assertEqual(list.length, oldlength + 3 + view.length);
194 
195  // And add it again
196  var simpleEnumerator = new TestMediaListEnumerationListener();
197  view.enumerateAllItems(simpleEnumerator,
198  Ci.sbIMediaList.ENUMERATIONTYPE_LOCKING);
199  list.addSome(simpleEnumerator);
200 
201  a.forEach(function(e) { data.push(e); });
202  assertList(list, data);
203  assertEqual(list.length, oldlength + 3 + view.length + view.length);
204 
205  var asyncListener = {
206  _itemsProcessed: 0,
207  _complete: false,
208  onProgress: function(aItemsProcessed, aComplete) {
209  if(aComplete) {
210  this._itemsProcessed = aItemsProcessed;
211  this._complete = true;
212  testFinished();
213  }
214  },
215  onItemAdded: function(aMediaItem) {},
216  onComplete: function() {}
217  };
218 
219  simpleEnumerator.reset();
220  view.enumerateAllItems(simpleEnumerator,
221  Ci.sbIMediaList.ENUMERATIONTYPE_SNAPSHOT);
222  {
223  // This is kind of annoying but necessary because XPC shell only has
224  // one JS context. If we don't do this XPC shell will lock up.
225  var tempArray = Cc["@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
226  .createInstance(Ci.nsIMutableArray);
227  while(simpleEnumerator.hasMoreElements()) {
228  tempArray.appendElement(simpleEnumerator.getNext(), false);
229  }
230 
231  var tempEnumerator = tempArray.enumerate();
232  log("Testing addSomeAsync");
233  list.addMediaItems(tempEnumerator, asyncListener, true);
234  testPending();
235  }
236 
237  log("Processed " + asyncListener._itemsProcessed + " items.");
238  a.forEach(function(e) { data.push(e); });
239  assertList(list, data);
240  assertEqual(list.length, oldlength + 3 + (view.length * 3));
241 
242  // Test insertBefore. These tests seem a bit random but they are testing
243  // all the code paths in sbLocalDatabaseSimpleMediaList::GetBeforeOrdinal
244  library = createLibrary(databaseGUID);
245  list = library.getMediaItem("7e8dcc95-7a1d-4bb3-9b14-d4906a9952cb");
246  a = readList("data_sort_sml101_ordinal_asc.txt");
247 
248  // insert at top
249  item = library.getMediaItem("3E6DD1C2-AD99-11DB-9321-C22AB7121F49");
250  list.insertBefore(0, item);
251  a.unshift(item.guid);
252  assertList(list, a);
253 
254  // insert before last
255  item = library.getMediaItem("3E6D8050-AD99-11DB-9321-C22AB7121F49");
256  list.insertBefore(list.length - 1, item);
257  a.splice(a.length - 1, 0, item.guid);
258  assertList(list, a);
259 
260  // insert above the previous insert
261  item = library.getMediaItem("3E6D3050-AD99-11DB-9321-C22AB7121F49");
262  list.insertBefore(list.length - 2, item);
263  a.splice(a.length - 2, 0, item.guid);
264  assertList(list, a);
265 
266  // insert befoe last again
267  item = library.getMediaItem("3E6CDB1E-AD99-11DB-9321-C22AB7121F49");
268  list.insertBefore(list.length - 1, item);
269  a.splice(a.length - 1, 0, item.guid);
270  assertList(list, a);
271 
272  // insert above the previous insert
273  item = library.getMediaItem("3E6C8D80-AD99-11DB-9321-C22AB7121F49");
274  list.insertBefore(list.length - 2, item);
275  a.splice(a.length - 2, 0, item.guid);
276  assertList(list, a);
277 
278  // test bad index
279  try {
280  list.insertBefore(list.length, item);
281  fail("NS_ERROR_INVALID_ARG not thrown");
282  }
283  catch(e) {
284  assertEqual(e.result, Cr.NS_ERROR_INVALID_ARG);
285  }
286 
287  // Test insertSomeBefore
288  var items = [];
289  items[0] = library.getMediaItem("3E6DD1C2-AD99-11DB-9321-C22AB7121F49");
290  items[1] = library.getMediaItem("3E6D8050-AD99-11DB-9321-C22AB7121F49");
291  items[2] = library.getMediaItem("3E6D3050-AD99-11DB-9321-C22AB7121F49");
292  list.insertSomeBefore(0, new SimpleArrayEnumerator(items));
293  a.unshift(items[2].guid);
294  a.unshift(items[1].guid);
295  a.unshift(items[0].guid);
296  assertList(list, a);
297 
298  list.insertSomeBeforeAsync(0,
299  new SimpleArrayEnumerator(items),
300  asyncListener);
301  testPending();
302  a.unshift(items[2].guid);
303  a.unshift(items[1].guid);
304  a.unshift(items[0].guid);
305  assertList(list, a);
306 
307  // Test moveBefore
308  library = createLibrary(databaseGUID);
309  list = library.getMediaItem("7e8dcc95-7a1d-4bb3-9b14-d4906a9952cb");
310  a = readList("data_sort_sml101_ordinal_asc.txt");
311 
312  // move from second to first
313  list.moveBefore(1, 0);
314  a.unshift(a.splice(1, 1)[0]);
315  assertList(list, a);
316 
317  // move from first to before last
318  list.moveBefore(0, list.length - 1);
319  var guid = a.splice(0, 1)[0];
320  a.splice(a.length - 1, 0, guid);
321  assertList(list, a);
322 
323  // Test moveLast
324  list.moveLast(0);
325  guid = a.splice(0, 1)[0];
326  a.push(guid);
327  assertList(list, a);
328 
329  // Test moveSomeBefore
330  var indexes = [5, 6, 7, 8, 9];
331  list.moveSomeBefore(indexes, indexes.length, 0);
332  var b = a.splice(5, 5);
333  a.unshift(b[0], b[1], b[2], b[3], b[4]);
334  assertList(list, a);
335 
336  // Test moveSomeLast
337  list.moveSomeLast(indexes, indexes.length);
338  var b = a.splice(5, 5);
339  a = a.concat(b);
340  assertList(list, a);
341 
342  // Test insertAllBefore.
343  var insertItems = [];
344  insertItems[0] = library.getMediaItem("3E6DD1C2-AD99-11DB-9321-C22AB7121F49");
345  insertItems[1] = library.getMediaItem("3E6D8050-AD99-11DB-9321-C22AB7121F49");
346  insertItems[2] = library.getMediaItem("3E6D3050-AD99-11DB-9321-C22AB7121F49");
347  var insertList = library.createMediaList("simple");
348  insertList.addSome(new SimpleArrayEnumerator(insertItems));
349  list.insertAllBefore(0, insertList);
350  a.unshift(insertItems[2].guid);
351  a.unshift(insertItems[1].guid);
352  a.unshift(insertItems[0].guid);
353  assertList(list, a);
354 
355  // Test insertAllBefore again to ensure ordinals are set properly.
356  list.insertAllBefore(0, insertList);
357  a.unshift(insertItems[2].guid);
358  a.unshift(insertItems[1].guid);
359  a.unshift(insertItems[0].guid);
360  assertList(list, a);
361 
362  // Move items added with insertAllBefore to the end of the list. Otherwise,
363  // removal tests will fail because they require the items removed from the
364  // beginning of the list to only exist once on the list.
365  var indexes = [0, 1, 2, 3, 4, 5];
366  list.moveSomeLast(indexes, indexes.length);
367  b = a.splice(0, 6);
368  a = a.concat(b);
369  assertList(list, a);
370 
371  // Test remove
372  item = library.getMediaItem("3E586C1A-AD99-11DB-9321-C22AB7121F49");
373  assertEqual(list.contains(item), true);
374  oldlength = list.length;
375  list.remove(item);
376  assertEqual(list.contains(item), false);
377  assertEqual(list.length, oldlength - 1);
378 
379  item = list.getItemByIndex(0);
380  oldlength = list.length;
381  assertEqual(list.contains(item), true);
382  list.removeByIndex(0);
383  assertEqual(list.contains(item), false);
384  assertEqual(list.length, oldlength - 1);
385 
386  // Remove an item that is not in the list should do nothing
387  var notinlist = library.createMediaItem(newURI("http://foo.com/blah"));
388  list.remove(notinlist);
389 
390  // test bad index
391  try {
392  list.removeByIndex(list.length);
393  fail("NS_ERROR_INVALID_ARG not thrown");
394  }
395  catch(e) {
396  assertEqual(e.result, Cr.NS_ERROR_INVALID_ARG);
397  }
398 
399  var toRemove = [
400  list.getItemByIndex(0),
401  list.getItemByIndex(1),
402  list.getItemByIndex(2),
403  list.getItemByIndex(3),
404  list.getItemByIndex(4)
405  ];
406  oldlength = list.length;
407  toRemove.forEach(function(item) { assertEqual(list.contains(item), true); });
408  list.removeSome(new SimpleArrayEnumerator(toRemove));
409  toRemove.forEach(function(item) { assertEqual(list.contains(item), false); });
410  assertEqual(list.length, oldlength - toRemove.length);
411 
412  // Test clear
413  list.clear();
414  assertEqual(list.length, 0);
415 
416  // Test duplicate items in list
417  library = createLibrary(databaseGUID);
418  var item1 = library.createMediaItem(newURI("file:///foo"));
419  var item2 = library.createMediaItem(newURI("file:///foo"));
420  list = library.createMediaList("simple");
421 
422  a = [item1.guid, item2.guid, item1.guid, item2.guid];
423  list.add(item1);
424  list.add(item2);
425  list.add(item1);
426  list.add(item2);
427  assertList(list, a);
428 
429  list.removeByIndex(2);
430  a = [item1.guid, item2.guid, item2.guid];
431  assertList(list, a);
432 
433  list.removeByIndex(2);
434  a = [item1.guid, item2.guid];
435  assertList(list, a);
436 
437  // Test that removal actually deletes the first instance
438  list = library.createMediaList("simple");
439  list.add(item1);
440  list.add(item2);
441  list.add(item1);
442  list.add(item2);
443  list.add(item2);
444  list.add(item1);
445  list.add(item2);
446  list.add(item2);
447  list.add(item2);
448 
449  a = [item1.guid, item2.guid, item1.guid, item2.guid, item2.guid, item1.guid, item2.guid, item2.guid, item2.guid];
450  assertList(list, a);
451 
452  list.remove(item1);
453  a = [item2.guid, item1.guid, item2.guid, item2.guid, item1.guid, item2.guid, item2.guid, item2.guid];
454  assertList(list, a);
455 
456  list.remove(item1);
457  a = [item2.guid, item2.guid, item2.guid, item1.guid, item2.guid, item2.guid, item2.guid];
458  assertList(list, a);
459 
460  list.remove(item1);
461  a = [item2.guid, item2.guid, item2.guid, item2.guid, item2.guid, item2.guid];
462  assertList(list, a);
463 
464  // Test adding foreign and already existing items to the list
465  localLibrary = createLibrary("test_simplemedialist_local");
466  let localLibraryLength = localLibrary.length;
467  foreignLibrary = createLibrary("test_simplemedialist_foreign");
468 
469  let items = [
470  foreignLibrary.createMediaItem(newURI("file:///foo1")),
471  foreignLibrary.createMediaItem(newURI("file:///foo2")),
472  localLibrary.createMediaItem(newURI("file:///foo3"))
473  ];
474  localLibrary.add(items[1]);
475  list = localLibrary.createMediaList("simple");
476  list.addSome(ArrayConverter.enumerator(items));
477 
478  // new items should be 4, 3 new items and the playlist
479  assertEqual(localLibrary.length - localLibraryLength, 4);
480 }
481 
function countItems(enumerator)
const Cc
function fail(aMessage)
function runTest()
Advanced DataRemote unit tests.
function assertNotEqual(aExpected, aActual, aMessage)
function onComplete(job)
Definition: test_bug7406.js:85
function log(s)
function testFinished()
function assertList(list, data)
sidebarFactory createInstance
Definition: nsSidebar.js:351
function assertEqual(aExpected, aActual, aMessage)
var count
Definition: test_bug7406.js:32
return null
Definition: FeedWriter.js:1143
function SimpleArrayEnumerator(aArray)
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function newURI(aURLString)
const Cr
function readList(dataFile)
const Ci
observe data
Definition: FeedWriter.js:1329
function testPending()