test_playqueue.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 function runTest () {
30  var gLib = null; // test lib
31  var gPQS = null; // play queue service
32  var gMM = null; // mediacore manager
33  var gEventTarget = null;
34 
35  var testFixture = {
36  _testURIArray: [],
37  _testIndex: 0,
38  _prevTestIndex: 0,
39  _indexChange: false,
40  _nextUri: 0,
41 
42  // call this between each test
43  reset: function () {
44  this._testURIArray = [];
45  this._testIndex = 0;
46  this._nextUri = 0;
47  gPQS.clearAll();
48  },
49 
50  onIndexUpdated: function (index) {
51  this._indexChange = true;
52  },
53 
54  onQueueOperationStarted: function() {},
55 
56  onQueueOperationCompleted: function(aCount) {
57  testFinished();
58  },
59 
60  // sbIMediaList methods
61  testAdd: function (num) {
62  var uris = this._generateURIs(num);
63  for (let i = 0; i < uris.length; i++)
64  this._testURIArray.push(uris[i]);
65  var items = this._generateItems(uris);
66  if (num == 1) {
67  gPQS.mediaList.add(items[0]);
68  } else {
69  gPQS.mediaList.addSome(ArrayConverter.enumerator(items));
70  }
71  this._verifyList();
72  },
73 
74  testRemove: function (indexArray) {
75  // go through the list backwards so indexes are valid after splice
76  indexArray.sort();
77  for (let i = indexArray.length - 1; i > -1 ; i--) {
78  if (indexArray[i] < this._testIndex)
79  this._testIndex--;
80  this._testURIArray.splice(indexArray[i], 1);
81  }
82  var items = this._getItemsByIndexes(indexArray);
83  if (indexArray.length == 1) {
84  gPQS.mediaList.remove(items[0]);
85  } else {
86  gPQS.mediaList.removeSome(ArrayConverter.enumerator(items));
87  }
88  this._verifyList();
89  },
90 
91  // play queue service methods
92  testQueueNext: function (num) {
93  var uris = this._generateURIs(num);
94  for (let i = 0; i < uris.length; i++)
95  this._testURIArray.splice(this._testIndex + i, 0, uris[i]);
96  var items = this._generateItems(uris);
97  if (num == 1) {
98  gPQS.queueNext(items[0]);
99  } else {
100  let simpleList = gLib.createMediaList("simple");
101  simpleList.addSome(ArrayConverter.enumerator(items));
102  gPQS.queueNext(simpleList);
103  }
104  this._verifyList();
105  },
106 
107  testQueueLast: function (num) {
108  var uris = this._generateURIs(num);
109  for (let i = 0; i < uris.length; i++)
110  this._testURIArray.push(uris[i]);
111  var items = this._generateItems(uris);
112  if (num == 1) {
113  gPQS.queueLast(items[0]);
114  } else {
115  let simpleList = gLib.createMediaList("simple");
116  simpleList.addSome(ArrayConverter.enumerator(items));
117  gPQS.queueLast(simpleList);
118  }
119  this._verifyList();
120  },
121 
122  testQueueSomeBefore: function(num, pos) {
123  var uris = this._generateURIs(num);
124  for (let i = 0; i < uris.length; i++)
125  this._testURIArray.splice(pos + i, 0, uris[i]);
126  if (pos <= this._testIndex)
127  this._testIndex += num;
128  var items = this._generateItems(uris);
129  gPQS.queueSomeBefore(pos, ArrayConverter.enumerator(items));
130  testPending();
131  this._verifyList();
132  },
133 
134  testQueueSomeNext: function(num) {
135  var uris = this._generateURIs(num);
136  for (let i = 0; i < uris.length; i++)
137  this._testURIArray.splice(this._testIndex + i, 0, uris[i]);
138  var items = this._generateItems(uris);
139  gPQS.queueSomeNext(ArrayConverter.enumerator(items));
140  testPending();
141  this._verifyList();
142  },
143 
144  testQueueSomeLast: function(num) {
145  var uris = this._generateURIs(num);
146  for (let i = 0; i < uris.length; i++)
147  this._testURIArray.push(uris[i]);
148  var items = this._generateItems(uris);
149  gPQS.queueSomeLast(ArrayConverter.enumerator(items));
150  testPending();
151  this._verifyList();
152  },
153 
154  testClearHistory: function () {
155  if (this._testIndex > 0)
156  this._testURIArray.splice(0, this._testIndex);
157  this._testIndex = 0;
158  gPQS.clearHistory();
159  this._verifyList();
160  },
161 
162  testClearAll: function () {
163  this._testIndex = 0;
164  this._testURIArray.length = 0;
165  gPQS.clearAll();
166  assertEqual(gPQS.index, 0);
167  this._verifyList();
168  },
169 
170  testSetIndex: function (pos) {
171  gPQS.index = pos;
172  this._testIndex = Math.min(pos, gPQS.mediaList.length);
173  this._verifyList();
174  },
175 
176  // sequencer methods
177  testSetSequencer: function (pos) {
178  return this.testSetIndex(pos);
179  // initialize the sequencer view
180  gMM.sequencer.view = gPQS.mediaList.createView();
181  while (gMM.sequencer.viewPosition < pos)
182  gMM.sequencer.next();
183  while (gMM.sequencer.viewPosition > pos)
184  gMM.sequencer.previous();
185  gMM.sequencer.stop();
186  this._testIndex = pos;
187  this._verifyList();
188  },
189 
190  // sbIOrderableMediaList methods
191  testInsertBefore: function (num, pos) {
192  var uris = this._generateURIs(num);
193  for (let i = 0; i < uris.length; i++)
194  this._testURIArray.splice(pos + i, 0, uris[i]);
195  if (pos <= this._testIndex)
196  this._testIndex += num;
197  var items = this._generateItems(uris);
198  if (num == 1) {
199  gPQS.mediaList.insertBefore(pos, items[0]);
200  } else {
201  gPQS.mediaList.insertSomeBefore(pos, ArrayConverter.enumerator(items));
202  }
203  this._verifyList();
204  },
205 
206  testMoveBefore: function (indexArray, pos) {
207  indexArray.sort();
208  var uris = new Array(indexArray.length);
209  var movingIndex = -1;
210  var newURIArray = [];
211  var finalIndex = -1;
212  for (let i = 0; i < indexArray.length; i++) {
213  if (indexArray[i] == this._testIndex) {
214  movingIndex = i;
215  this._testIndex = -1;
216  }
217  uris[i] = this._testURIArray[indexArray[i]];
218  this._testURIArray[indexArray[i]] = null;
219  }
220  for (let i = 0; i < pos; i++) {
221  if (i == this._testIndex)
222  finalIndex = newURIArray.length;
223  if (this._testURIArray[i])
224  newURIArray.push(this._testURIArray[i]);
225  }
226  for (let i = 0; i < uris.length; i++) {
227  if (i == movingIndex)
228  finalIndex = newURIArray.length;
229  newURIArray.push(uris[i]);
230  }
231  for (let i = pos; i < this._testURIArray.length; i++) {
232  if (i == this._testIndex)
233  finalIndex = newURIArray.length;
234  if (this._testURIArray[i])
235  newURIArray.push(this._testURIArray[i]);
236  }
237  this._testURIArray = newURIArray;
238  this._testIndex = finalIndex;
239  if (indexArray.length == 1) {
240  gPQS.mediaList.moveBefore(indexArray[0], pos);
241  } else {
242  gPQS.mediaList.moveSomeBefore(indexArray, indexArray.length, pos);
243  }
244  this._verifyList();
245  },
246 
247  testMoveLast: function (indexArray) {
248  indexArray.sort();
249  var uris = new Array(indexArray.length);
250  var movingIndex = -1;
251  var newURIArray = [];
252  var finalIndex = -1;
253  for (let i = 0; i < indexArray.length; i++) {
254  if (indexArray[i] == this._testIndex) {
255  movingIndex = i;
256  this._testIndex = -1;
257  }
258  uris[i] = this._testURIArray[indexArray[i]];
259  this._testURIArray[indexArray[i]] = null;
260  }
261  for (let i = 0; i < this._testURIArray.length; i++) {
262  if (i == this._testIndex)
263  finalIndex = newURIArray.length;
264  if (this._testURIArray[i])
265  newURIArray.push(this._testURIArray[i]);
266  }
267  for (let i = 0; i < uris.length; i++) {
268  if (i == movingIndex)
269  finalIndex = newURIArray.length;
270  newURIArray.push(uris[i]);
271  }
272  this._testURIArray = newURIArray;
273  this._testIndex = finalIndex;
274  if (indexArray.length == 1) {
275  gPQS.mediaList.moveLast(indexArray[0]);
276  } else {
277  gPQS.mediaList.moveSomeLast(indexArray, indexArray.length);
278  }
279  this._verifyList();
280  },
281 
282  _generateItems: function (uris) {
283  var items = new Array(uris.length);
284  for (var i = 0; i < uris.length; i++)
285  items[i] = gLib.createMediaItem(uris[i]);
286  return items;
287  },
288 
289  _generateURIs: function (num) {
290  var uris = new Array(num);
291  for (var i = 0; i < num; i++)
292  uris[i] = newURI("http://test.com/test" + this._nextUri++);
293  return uris;
294  },
295 
296  _getItemsByIndexes: function (indexArray) {
297  var items = new Array(indexArray.length)
298  for (let i = 0; i < indexArray.length; i++)
299  items[i] = gPQS.mediaList.getItemByIndex(indexArray[i]);
300  return items;
301  },
302 
303  _verifyList: function () {
304  assertEqual(gPQS.mediaList.length, this._testURIArray.length);
305  assertEqual(gPQS.index, this._testIndex);
306  if (this._testIndex != this._prevTestIndex) {
307  assertTrue(this._indexChange);
308  this._indexChange = false;
309  this._prevTestIndex = this._testIndex;
310  }
311  for (var i = 0; i < this._testURIArray.length; i++) {
312  assertTrue( this._testURIArray[i].equals(
313  gPQS.mediaList.getItemByIndex(i)["contentSrc"]) );
314  }
315  },
316  };
317 
318  // Import some services.
319  Components.utils.import("resource://app/jsmodules/ArrayConverter.jsm");
320 
321  // register a test library.
322  gLib = createLibrary("test_playqueueservice"); // test library
323  var libraryManager = Cc["@songbirdnest.com/Songbird/library/Manager;1"]
324  .getService(Ci.sbILibraryManager);
325  libraryManager.registerLibrary(gLib, false);
326 
327 
328  gPQS = Cc["@songbirdnest.com/Songbird/playqueue/service;1"]
329  .getService(Ci.sbIPlayQueueService);
330  assertNotEqual(gPQS, null, "Failed to get play queue service");
331 
332  if (!gPQS.mediaList.isEmpty) {
333  var backupList = gLib.copyMediaList("simple",
334  gPQS.mediaList,
335  false);
336  var backupIndex = gPQS.index;
337  }
338 
339  gPQS.addListener(testFixture);
340 
341  gMM = Cc["@songbirdnest.com/Songbird/Mediacore/Manager;1"]
342  .getService(Ci.sbIMediacoreManager);
343 
344  // single item tests
345  testFixture.reset();
346  testFixture.testAdd(1);
347  testFixture.testQueueNext(1);
348  testFixture.testSetSequencer(1);
349 
350  testFixture.testQueueLast(1);
351  testFixture.testClearHistory();
352  testFixture.testSetIndex(gPQS.index + 1);
353  testFixture.testRemove([1]);
354  testFixture.testSetIndex(gPQS.index + 1);
355  testFixture.testQueueNext(1);
356  testFixture.testClearAll();
357 
358  // addSome/removeSome
359  testFixture.reset();
360  testFixture.testAdd(5);
361  testFixture.testSetIndex(2);
362  testFixture.testRemove([0,1,2]);
363 
364  testFixture.reset();
365  testFixture.testAdd(5);
366  testFixture.testSetIndex(2);
367  testFixture.testRemove([0,2,3]);
368 
369  testFixture.reset();
370  testFixture.testAdd(5);
371  testFixture.testSetIndex(2);
372  testFixture.testRemove([1,2,3]);
373 
374  testFixture.reset();
375  testFixture.testAdd(5);
376  testFixture.testSetIndex(2);
377  testFixture.testRemove([2,3,4]);
378 
379  // queueNext/Last with mediaLists
380  testFixture.reset();
381  testFixture.testAdd(5);
382  testFixture.testSetIndex(4);
383  testFixture.testQueueNext(5);
384 
385  testFixture.reset();
386  testFixture.testAdd(5);
387  testFixture.testSetIndex(4);
388  testFixture.testQueueLast(5);
389 
390  // Test queueSomeBefore
391  testFixture.reset();
392  testFixture.testAdd(5);
393  testFixture.testSetIndex(3);
394  testFixture.testQueueSomeBefore(5, 2);
395 
396  testFixture.reset();
397  testFixture.testAdd(5);
398  testFixture.testSetIndex(3);
399  testFixture.testQueueSomeBefore(5, 3);
400 
401  testFixture.reset();
402  testFixture.testAdd(5);
403  testFixture.testSetIndex(3);
404  testFixture.testQueueSomeBefore(5, 4);
405 
406  // Test queueSomeNext/Last
407  testFixture.reset();
408  testFixture.testQueueSomeNext(3); // gPQS.index == gPQS.mediaList.length
409  testFixture.testSetIndex(2);
410  testFixture.testQueueSomeNext(1);
411  testFixture.testQueueSomeLast(3);
412  testFixture.testSetIndex(7);
413  testFixture.testQueueSomeLast(1); // gPQS.index == gPQS.mediaList.length
414 
415  // sbIOrderableMediaList
416  assertTrue(gPQS.mediaList instanceof Ci.sbIOrderableMediaList);
417 
418  testFixture.reset();
419  testFixture.testAdd(5);
420  testFixture.testSetIndex(3);
421  testFixture.testInsertBefore(5, 2);
422 
423  testFixture.reset();
424  testFixture.testAdd(5);
425  testFixture.testSetIndex(3);
426  testFixture.testInsertBefore(5, 3);
427 
428  testFixture.reset();
429  testFixture.testAdd(5);
430  testFixture.testSetIndex(3);
431  testFixture.testInsertBefore(5, 4);
432 
433  testFixture.reset();
434  testFixture.testAdd(5);
435  testFixture.testSetIndex(2);
436  testFixture.testMoveBefore([0,1,2], 2);
437 
438  testFixture.reset();
439  testFixture.testAdd(5);
440  testFixture.testSetIndex(2);
441  testFixture.testMoveBefore([0,1,3], 2);
442 
443  testFixture.reset();
444  testFixture.testAdd(5);
445  testFixture.testSetIndex(2);
446  testFixture.testMoveBefore([2,3,4], 0);
447 
448  testFixture.reset();
449  testFixture.testAdd(5);
450  testFixture.testSetIndex(2);
451  testFixture.testMoveLast([0,1]);
452 
453  testFixture.reset();
454  testFixture.testAdd(5);
455  testFixture.testSetIndex(2);
456  testFixture.testMoveLast([0,1,2,3]);
457 
458  testFixture.reset();
459  testFixture.testAdd(5);
460  testFixture.testSetIndex(2);
461  testFixture.testMoveLast([2,3,4]);
462 
463 
464  // clean up
465  testFixture.reset();
466 
467  gPQS.removeListener(testFixture);
468 
469  if (backupList)
470  {
471  gPQS.mediaList.addAll(backupList);
472  gPQS.index = backupIndex;
473  }
474  libraryManager.unregisterLibrary(gLib);
475 }
476 
const Cc
var gMM
Definition: windowUtils.js:62
_dialogDatepicker pos
function assertNotEqual(aExpected, aActual, aMessage)
var uris
function testFinished()
function assertTrue(aTest, aMessage)
function assertEqual(aExpected, aActual, aMessage)
function runTest()
Test file.
function num(elem, prop)
var libraryManager
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function newURI(aURLString)
const Ci
restoreHistoryPrecursor aCount
_getSelectedPageStyle s i
function testPending()