test_smartmedialist.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-2008 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 
31 function runTest() {
32 
33  var databaseGUID = "test_smartmedialist";
34  var library = createLibrary(databaseGUID);
35 
36  testProperties(library);
37  testConditions(library);
38  testAll(library);
39  testAny(library);
40  testOperators(library);
41  testItemLimit(library);
42  testSerialize(library);
43  testUsecsLimit(library);
44  testBytesLimit(library);
45  testRandom(library);
49  testMatchTypeNoneRandom(library);
50 }
51 
52 function testProperties(library) {
53 
54  var albumProp = SB_NS + "albumName";
55  var list = library.createMediaList("smart");
56 
57  assertEqual(list.matchType, Ci.sbILocalDatabaseSmartMediaList.MATCH_TYPE_ANY);
58  list.matchType = Ci.sbILocalDatabaseSmartMediaList.MATCH_TYPE_ALL;
59  assertEqual(list.matchType, Ci.sbILocalDatabaseSmartMediaList.MATCH_TYPE_ALL);
60 
61  assertEqual(list.limitType, Ci.sbILocalDatabaseSmartMediaList.LIMIT_TYPE_NONE);
62  list.limitType = Ci.sbILocalDatabaseSmartMediaList.LIMIT_TYPE_ITEMS;
63  assertEqual(list.limitType, Ci.sbILocalDatabaseSmartMediaList.LIMIT_TYPE_ITEMS);
64 
65  assertEqual(list.limit, 0);
66  list.limit = 20;
67  assertEqual(list.limit, 20);
68 
69  assertEqual(list.selectPropertyID, "");
70  list.selectPropertyID = albumProp;
71  assertEqual(list.selectPropertyID, albumProp);
72 
73  assertEqual(list.selectDirection, true);
74  list.selectDirection = false;
75  assertEqual(list.selectDirection, false);
76 
77  assertEqual(list.randomSelection, false);
78  list.randomSelection = true;
79  assertEqual(list.randomSelection, true);
80 
81  assertEqual(list.autoUpdate, false);
82  list.autoUpdate = true;
83  assertEqual(list.autoUpdate, true);
84 }
85 
86 function testConditions(library) {
87 
88  var albumProp = SB_NS + "albumName";
89 
90  var list = library.createMediaList("smart");
91 
92  assertEqual(list.conditionCount, 0);
93 
94  var op = getOperatorForProperty(albumProp, "=");
95  var condition1 = list.appendCondition(albumProp, op, "Back In Black", null, "a");
96  assertEqual(list.conditionCount, 1);
97 
98  assertEqual(condition1.propertyID, albumProp);
99  assertEqual(condition1.operator, op);
100  assertEqual(condition1.leftValue, "Back In Black");
101  assertEqual(condition1.rightValue, null);
102  assertEqual(condition1.displayUnit, "a");
103 
104  var condition2 = list.appendCondition(albumProp, op, "The Life of Riley", null, "b");
105  assertEqual(list.conditionCount, 2);
106 
107  assertEqual(list.getConditionAt(0), condition1);
108  assertEqual(list.getConditionAt(1), condition2);
109 
110  list.removeConditionAt(0);
111  assertEqual(list.conditionCount, 1);
112  assertEqual(list.getConditionAt(0), condition2);
113 
114  list.removeConditionAt(0);
115  assertEqual(list.conditionCount, 0);
116 }
117 
118 function testAll(library) {
119 
120  var albumProp = SB_NS + "albumName";
121  var artistProp = SB_NS + "artistName";
122  var contentLengthProp = SB_NS + "contentLength";
123 
124  var list = library.createMediaList("smart");
125  list.matchType = Ci.sbILocalDatabaseSmartMediaList.MATCH_TYPE_ALL;
126 
127  assertEqual(list.length, 0);
128 
129  list.appendCondition(albumProp,
130  getOperatorForProperty(albumProp, "="),
131  "Back In Black",
132  null,
133  "unit");
134  list.rebuild();
135 
136  assertEqual(list.length, 10);
137  assertUnique(list);
138 
139  // Adding this condition should not change the result since
140  // it overlaps with the first condition
141  list.appendCondition(artistProp,
142  getOperatorForProperty(artistProp, "="),
143  "AC/DC",
144  null,
145  "unit");
146  list.rebuild();
147  assertEqual(list.length, 10);
148  assertUnique(list);
149 
150  // Should result in 0 items
151  list.appendCondition(artistProp,
152  getOperatorForProperty(artistProp, "="),
153  "a-ha",
154  null,
155  "unit");
156  list.rebuild();
157 
158  assertEqual(list.length, 0);
159 
160  list.removeConditionAt(2);
161  list.rebuild();
162  assertEqual(list.length, 10);
163  assertUnique(list);
164 
165  // Contrain the list on contnet length
166  list.appendCondition(contentLengthProp,
167  getOperatorForProperty(contentLengthProp, "<"),
168  "1000",
169  null,
170  "unit");
171  list.rebuild();
172  assertEqual(list.length, 6);
173  assertUnique(list);
174 }
175 
176 function testAny(library) {
177 
178  var albumProp = SB_NS + "albumName";
179  var artistProp = SB_NS + "artistName";
180  var contentLengthProp = SB_NS + "contentLength";
181 
182  var list = library.createMediaList("smart");
183  list.matchType = Ci.sbILocalDatabaseSmartMediaList.MATCH_TYPE_ANY;
184 
185  list.appendCondition(albumProp,
186  getOperatorForProperty(albumProp, "="),
187  "Back In Black",
188  null,
189  "unit");
190  list.rebuild();
191 
192  assertEqual(list.length, 10);
193  assertUnique(list);
194 
195  // Adding this condition should not change the result since
196  // it overlaps with the first condition
197  list.appendCondition(artistProp,
198  getOperatorForProperty(artistProp, "="),
199  "AC/DC",
200  null,
201  "unit");
202  list.rebuild();
203 
204  assertEqual(list.length, 10);
205  assertUnique(list);
206 
207  // Add another artist
208  list.appendCondition(artistProp,
209  getOperatorForProperty(artistProp, "="),
210  "a-ha",
211  null,
212  "unit");
213  list.rebuild();
214 
215  assertEqual(list.length, 20);
216  assertUnique(list);
217 }
218 
219 function testOperators(library) {
220 
221  var list = library.createMediaList("smart");
222  var lastPlayTimeProp = SB_NS + "lastPlayTime";
223  var albumProp = SB_NS + "albumName";
224 
225  function setConditions(prop, op, value) {
226  list.clearConditions();
227  list.appendCondition(prop,
228  getOperatorForProperty(prop, op),
229  value,
230  null,
231  "unit");
232  list.rebuild();
233  }
234 
235  var value = "1166399962000";
236 
237  list.notExistsMode = list.NOTEXISTS_ASNULL;
238 
239  setConditions(lastPlayTimeProp, "=", value);
240  assertEqual(list.length, 4);
241  assertUnique(list);
242 
243  list.notExistsMode = list.NOTEXISTS_ASZERO;
244 
245  setConditions(lastPlayTimeProp, "=", value);
246  assertEqual(list.length, 4);
247  assertUnique(list);
248 
249  list.notExistsMode = list.NOTEXISTS_ASNULL;
250 
251  setConditions(lastPlayTimeProp, "!=", value);
252  assertEqual(list.length, 45);
253  assertUnique(list);
254 
255  list.notExistsMode = list.NOTEXISTS_ASZERO;
256 
257  setConditions(lastPlayTimeProp, "!=", value);
258  assertEqual(list.length, 96);
259  assertUnique(list);
260 
261  list.notExistsMode = list.NOTEXISTS_ASNULL;
262 
263  setConditions(lastPlayTimeProp, ">", value);
264  assertEqual(list.length, 29);
265  assertUnique(list);
266 
267  list.notExistsMode = list.NOTEXISTS_ASZERO;
268 
269  setConditions(lastPlayTimeProp, ">", value);
270  assertEqual(list.length, 29);
271  assertUnique(list);
272 
273  list.notExistsMode = list.NOTEXISTS_ASNULL;
274 
275  setConditions(lastPlayTimeProp, ">=", value);
276  assertEqual(list.length, 33);
277  assertUnique(list);
278 
279  list.notExistsMode = list.NOTEXISTS_ASZERO;
280 
281  setConditions(lastPlayTimeProp, ">=", value);
282  assertEqual(list.length, 33);
283  assertUnique(list);
284 
285  list.notExistsMode = list.NOTEXISTS_ASNULL;
286 
287  setConditions(lastPlayTimeProp, "<", value);
288  assertEqual(list.length, 16);
289  assertUnique(list);
290 
291  list.notExistsMode = list.NOTEXISTS_ASZERO;
292 
293  setConditions(lastPlayTimeProp, "<", value);
294  assertEqual(list.length, 67);
295  assertUnique(list);
296 
297  list.notExistsMode = list.NOTEXISTS_ASNULL;
298 
299  setConditions(lastPlayTimeProp, "<=", value);
300  assertEqual(list.length, 20);
301  assertUnique(list);
302 
303  list.notExistsMode = list.NOTEXISTS_ASZERO;
304 
305  setConditions(lastPlayTimeProp, "<=", value);
306  assertEqual(list.length, 71);
307  assertUnique(list);
308 
309  list.notExistsMode = list.NOTEXISTS_ASNULL;
310 
311  var value = "1166399960000";
312  setConditions(lastPlayTimeProp, "#=", value);
313  assertEqual(list.length, 4);
314  assertUnique(list);
315 
316  list.notExistsMode = list.NOTEXISTS_ASZERO;
317 
318  setConditions(lastPlayTimeProp, "#=", value);
319  assertEqual(list.length, 4);
320  assertUnique(list);
321 
322  list.notExistsMode = list.NOTEXISTS_ASNULL;
323 
324  setConditions(lastPlayTimeProp, "#!=", value);
325  assertEqual(list.length, 45);
326  assertUnique(list);
327 
328  list.notExistsMode = list.NOTEXISTS_ASZERO;
329 
330  setConditions(lastPlayTimeProp, "#!=", value);
331  assertEqual(list.length, 96);
332  assertUnique(list);
333 
334  list.notExistsMode = list.NOTEXISTS_ASNULL;
335 
336  setConditions(lastPlayTimeProp, "#>", value);
337  assertEqual(list.length, 29);
338  assertUnique(list);
339 
340  list.notExistsMode = list.NOTEXISTS_ASZERO;
341 
342  setConditions(lastPlayTimeProp, "#>", value);
343  assertEqual(list.length, 29);
344  assertUnique(list);
345 
346  list.notExistsMode = list.NOTEXISTS_ASNULL;
347 
348  setConditions(lastPlayTimeProp, "#>=", value);
349  assertEqual(list.length, 33);
350  assertUnique(list);
351 
352  list.notExistsMode = list.NOTEXISTS_ASZERO;
353 
354  setConditions(lastPlayTimeProp, "#>=", value);
355  assertEqual(list.length, 33);
356  assertUnique(list);
357 
358  list.notExistsMode = list.NOTEXISTS_ASNULL;
359 
360  setConditions(lastPlayTimeProp, "#<", value);
361  assertEqual(list.length, 16);
362  assertUnique(list);
363 
364  list.notExistsMode = list.NOTEXISTS_ASZERO;
365 
366  setConditions(lastPlayTimeProp, "#<", value);
367  assertEqual(list.length, 16);
368  assertUnique(list);
369 
370  list.notExistsMode = list.NOTEXISTS_ASNULL;
371 
372  setConditions(lastPlayTimeProp, "#<=", value);
373  assertEqual(list.length, 20);
374  assertUnique(list);
375 
376  list.notExistsMode = list.NOTEXISTS_ASZERO;
377 
378  setConditions(lastPlayTimeProp, "#<=", value);
379  assertEqual(list.length, 20);
380  assertUnique(list);
381 
382  list.notExistsMode = list.NOTEXISTS_ASNULL;
383 
384 
385  list.clearConditions();
386  list.appendCondition(lastPlayTimeProp,
387  getOperatorForProperty(lastPlayTimeProp, "^"),
388  "1164844762000",
389  "1169855962000",
390  "unit");
391  list.rebuild();
392  assertEqual(list.length, 49);
393  assertUnique(list);
394 
395  list.notExistsMode = list.NOTEXISTS_ASZERO;
396 
397  list.clearConditions();
398  list.appendCondition(lastPlayTimeProp,
399  getOperatorForProperty(lastPlayTimeProp, "^"),
400  "1164844764000",
401  "1169855960000",
402  "unit");
403  list.rebuild();
404  assertEqual(list.length, 45);
405  assertUnique(list);
406 
407  list.notExistsMode = list.NOTEXISTS_ASNULL;
408 
409  list.clearConditions();
410  list.appendCondition(lastPlayTimeProp,
411  getOperatorForProperty(lastPlayTimeProp, "#^"),
412  "1164844764000",
413  "1169855960000",
414  "unit");
415  list.rebuild();
416  assertEqual(list.length, 49);
417  assertUnique(list);
418 
419  list.notExistsMode = list.NOTEXISTS_ASNULL;
420 
421  list.clearConditions();
422  list.appendCondition(albumProp,
423  getOperatorForProperty(albumProp, "?%"),
424  "On",
425  null,
426  "unit");
427  list.rebuild();
428  assertEqual(list.length, 12);
429  assertUnique(list);
430 
431  list.notExistsMode = list.NOTEXISTS_ASZERO;
432 
433  list.clearConditions();
434  list.appendCondition(albumProp,
435  getOperatorForProperty(albumProp, "?%"),
436  "On",
437  null,
438  "unit");
439  list.rebuild();
440  assertEqual(list.length, 12);
441  assertUnique(list);
442 
443  list.notExistsMode = list.NOTEXISTS_ASNULL;
444 
445  list.clearConditions();
446  list.appendCondition(albumProp,
447  getOperatorForProperty(albumProp, "%?"),
448  "Black",
449  null,
450  "unit");
451  list.rebuild();
452  assertEqual(list.length, 22);
453  assertUnique(list);
454 
455  list.notExistsMode = list.NOTEXISTS_ASZERO;
456 
457  list.clearConditions();
458  list.appendCondition(albumProp,
459  getOperatorForProperty(albumProp, "%?"),
460  "Black",
461  null,
462  false);
463  list.rebuild();
464  assertEqual(list.length, 22);
465  assertUnique(list);
466 
467  list.notExistsMode = list.NOTEXISTS_ASNULL;
468 
469  list.clearConditions();
470  list.appendCondition(albumProp,
471  getOperatorForProperty(albumProp, "%?%"),
472  "fat",
473  null,
474  "unit");
475  list.rebuild();
476  assertEqual(list.length, 12);
477  assertUnique(list);
478 
479  list.notExistsMode = list.NOTEXISTS_ASZERO;
480 
481  list.clearConditions();
482  list.appendCondition(albumProp,
483  getOperatorForProperty(albumProp, "%?%"),
484  "fat",
485  null,
486  false);
487  list.rebuild();
488  assertEqual(list.length, 12);
489  assertUnique(list);
490 
491 }
492 
493 function testItemLimit(library) {
494 
495  var albumProp = SB_NS + "albumName";
496  var trackProp = SB_NS + "trackNumber";
497 
498  var list = library.createMediaList("smart");
499 
500  list.appendCondition(albumProp,
501  getOperatorForProperty(albumProp, "="),
502  "Back In Black",
503  null,
504  "unit");
505  list.rebuild();
506  assertEqual(list.length, 10);
507  assertUnique(list);
508 
509  list.limitType = Ci.sbILocalDatabaseSmartMediaList.LIMIT_TYPE_ITEMS;
510  list.limit = 5;
511  list.selectPropertyID = trackProp;
512  list.selectDirection = true;
513 
514  list.rebuild();
515  assertEqual(list.length, 5);
516 
517  // Should have tracks 1 to 5
518  assertTrackNumbers(list, [1, 2, 3, 4, 5]);
519 
520  list.selectDirection = false;
521  list.rebuild();
522 
523  // Should have tracks 6 to 10
524  assertTrackNumbers(list, [10, 9, 8, 7, 6]);
525 
526  list.limitType = Ci.sbILocalDatabaseSmartMediaList.LIMIT_TYPE_NONE;
527  list.rebuild();
528  assertEqual(list.length, 10);
529 }
530 
531 function testUsecsLimit(library) {
532 
533  var albumProp = SB_NS + "albumName";
534  var trackProp = SB_NS + "trackNumber";
535 
536  var list = library.createMediaList("smart");
537 
538  list.appendCondition(albumProp,
539  getOperatorForProperty(albumProp, "="),
540  "Back In Black",
541  null,
542  "unit");
543 
544  list.limitType = Ci.sbILocalDatabaseSmartMediaList.LIMIT_TYPE_USECS;
545  list.limit = 30 * 60 * 1000 * 1000;
546  list.selectPropertyID = trackProp;
547  list.selectDirection = true;
548 
549  list.rebuild();
550  assertEqual(list.length, 3);
551  assertUnique(list);
552 
553  // Should have tracks 1 to 3
554  assertTrackNumbers(list, [1, 2, 3]);
555 
556  list.selectDirection = false;
557  list.rebuild();
558  assertEqual(list.length, 3);
559 
560  // Should have tracks 8 to 10
561  assertTrackNumbers(list, [10, 9, 8]);
562 }
563 
564 function testBytesLimit(library) {
565 
566  var albumProp = SB_NS + "albumName";
567  var trackProp = SB_NS + "trackNumber";
568 
569  var list = library.createMediaList("smart");
570 
571  list.appendCondition(albumProp,
572  getOperatorForProperty(albumProp, "="),
573  "Back In Black",
574  null,
575  "unit");
576 
577  list.limitType = Ci.sbILocalDatabaseSmartMediaList.LIMIT_TYPE_BYTES;
578  list.limit = 1500;
579  list.selectPropertyID = trackProp;
580  list.selectDirection = true;
581 
582  list.rebuild();
583  assertEqual(list.length, 3);
584  assertUnique(list);
585 
586  // Should have tracks 1 to 3
587  assertTrackNumbers(list, [1, 2, 3]);
588 
589  list.selectDirection = false;
590  list.rebuild();
591  assertEqual(list.length, 3);
592 
593  // Should have tracks 8 to 10
594  assertTrackNumbers(list, [10, 9, 8]);
595 }
596 
597 function testMatchTypeNoneItemLimit(library) {
598 
599  var artistProp = SB_NS + "artistName";
600 
601  var list = library.createMediaList("smart");
602  list.matchType = Ci.sbILocalDatabaseSmartMediaList.MATCH_TYPE_NONE;
603  list.limitType = Ci.sbILocalDatabaseSmartMediaList.LIMIT_TYPE_ITEMS;
604  list.limit = 25;
605  list.selectPropertyID = artistProp;
606  list.selectDirection = true;
607 
608  list.rebuild();
609  assertEqual(list.length, 25);
610  assertUnique(list);
611 
612  // First 12 tracks are "A House"
613  assertPropertyRange(list, 0, 12, artistProp, "A House");
614 
615  // Next 13 tracks are "A Split Second"
616  assertPropertyRange(list, 12, 13, artistProp, "A Split Second");
617 
618 }
619 
620 function testMatchTypeNoneUsecLimit(library) {
621 
622  var artistProp = SB_NS + "artistName";
623  var durationProp = SB_NS + "duration";
624 
625  var list = library.createMediaList("smart");
626  list.matchType = Ci.sbILocalDatabaseSmartMediaList.MATCH_TYPE_NONE;
627  list.limitType = Ci.sbILocalDatabaseSmartMediaList.LIMIT_TYPE_USECS;
628 
629  // 2 hours of music
630  list.limit = 120 * 60 * 1000 * 1000;
631  list.selectPropertyID = artistProp;
632  list.selectDirection = true;
633 
634  list.rebuild();
635  assertEqual(list.length, 15);
636  assertUnique(list);
637 
638  // Make sure the list's limit is correct
639  var sum = sumProperty(list, durationProp);
640  assertTrue(sum >= list.limit);
641 
642  // First 12 tracks are "A House"
643  assertPropertyRange(list, 0, 12, artistProp, "A House");
644 
645  // Next 3 tracks are "A Split Second"
646  assertPropertyRange(list, 12, 3, artistProp, "A Split Second");
647 }
648 
649 function testMatchTypeNoneBytesLimit(library) {
650 
651  var artistProp = SB_NS + "artistName";
652  var contentLengthProp = SB_NS + "contentLength";
653 
654  var list = library.createMediaList("smart");
655  list.matchType = Ci.sbILocalDatabaseSmartMediaList.MATCH_TYPE_NONE;
656  list.limitType = Ci.sbILocalDatabaseSmartMediaList.LIMIT_TYPE_BYTES;
657 
658  list.limit = 40000;
659  list.selectPropertyID = artistProp;
660  list.selectDirection = true;
661 
662  list.rebuild();
663  assertEqual(list.length, 30);
664  assertUnique(list);
665 
666  // Make sure the list's limit is correct
667  var sum = sumProperty(list, contentLengthProp);
668  assertTrue(sum >= list.limit);
669 
670  // First 12 tracks are "A House"
671  assertPropertyRange(list, 0, 12, artistProp, "A House");
672 
673  // Next 15 tracks are "A Split Second"
674  assertPropertyRange(list, 12, 15, artistProp, "A Split Second");
675 
676  // Next 3 tracks are "A Split Second"
677  assertPropertyRange(list, 27, 3, artistProp, "a-ha");
678 }
679 
680 function testRandom(library) {
681 
682  var albumProp = SB_NS + "albumName";
683  var trackProp = SB_NS + "trackNumber";
684 
685  var list = library.createMediaList("smart");
686 
687  list.appendCondition(albumProp,
688  getOperatorForProperty(albumProp, "="),
689  "Back In Black",
690  null,
691  "unit");
692 
693  list.limitType = Ci.sbILocalDatabaseSmartMediaList.LIMIT_TYPE_ITEMS;
694  list.limit = 5;
695  list.randomSelection = true;
696 
697  list.rebuild();
698  assertEqual(list.length, 5);
699  assertUnique(list);
700 
701  // Test various high limits, should return all tracks in back in black
702  list.limit = 200;
703  list.rebuild();
704  assertEqual(list.length, 10);
705  assertUnique(list);
706 
707  list.limitType = Ci.sbILocalDatabaseSmartMediaList.LIMIT_TYPE_USECS;
708  list.limit = 10 * 60 * 1000 * 1000;
709  list.rebuild();
710  assertUnique(list);
711 
712  list.limit = 240 * 60 * 1000 * 1000;
713  list.rebuild();
714  assertEqual(list.length, 10);
715  assertUnique(list);
716 
717  list.limitType = Ci.sbILocalDatabaseSmartMediaList.LIMIT_TYPE_BYTES;
718  list.limit = 10000;
719  list.rebuild();
720  assertUnique(list);
721 
722  list.limit = 1000000000;
723  list.rebuild();
724  assertEqual(list.length, 10);
725  assertUnique(list);
726 }
727 
728 function testMatchTypeNoneRandom(library) {
729 
730  var list = library.createMediaList("smart");
731  var libraryMediaItems = countMediaItems(library);
732 
733  list.matchType = Ci.sbILocalDatabaseSmartMediaList.MATCH_TYPE_NONE;
734  list.limitType = Ci.sbILocalDatabaseSmartMediaList.LIMIT_TYPE_ITEMS;
735  list.limit = 5;
736  list.randomSelection = true;
737 
738  list.rebuild();
739  assertEqual(list.length, 5);
740  assertUnique(list);
741 
742  // a really high limit should select the entire library
743  list.limit = 100000;
744  list.rebuild();
745  assertEqual(list.length, libraryMediaItems);
746  assertUnique(list);
747 
748  list.limitType = Ci.sbILocalDatabaseSmartMediaList.LIMIT_TYPE_USECS;
749  list.limit = 10 * 60 * 1000 * 1000;
750  list.rebuild();
751  assertUnique(list);
752 
753  list.limit = 100 * 60 * 60 * 1000 * 1000;
754  list.rebuild();
755  assertEqual(list.length, libraryMediaItems);
756  assertUnique(list);
757 
758  list.limitType = Ci.sbILocalDatabaseSmartMediaList.LIMIT_TYPE_BYTES;
759  list.limit = 10000;
760  list.rebuild();
761  assertUnique(list);
762 
763  list.limit = 1000000000;
764  list.rebuild();
765  assertEqual(list.length, libraryMediaItems);
766  assertUnique(list);
767 }
768 
769 function testSerialize(library) {
770 
771  var lastPlayTimeProp = SB_NS + "lastPlayTime";
772  var albumProp = SB_NS + "albumName";
773 
774  var list = library.createMediaList("smart");
775  var guid = list.guid;
776  list.matchType = Ci.sbILocalDatabaseSmartMediaList.MATCH_TYPE_ALL;
777  list.limitType = Ci.sbILocalDatabaseSmartMediaList.LIMIT_TYPE_ITEMS;
778  list.selectPropertyID = albumProp;
779  list.selectDirection = false;
780  list.limit = 123;
781  list.randomSelection = true;
782  list.autoUpdate = true;
783  list.appendCondition(albumProp,
784  getOperatorForProperty(albumProp, "%?%"),
785  "fat",
786  "",
787  "unit");
788  list.appendCondition(lastPlayTimeProp,
789  getOperatorForProperty(lastPlayTimeProp, ">"),
790  "1164844762000",
791  "",
792  "unit");
793  library.flush();
794 
795  // Create a second instance of the library and get the same list and see
796  // if they match
797  var databaseGUID = "test_smartmedialist";
798  var library2 = createLibrary(databaseGUID, null, false);
799  var restoredList = library2.getMediaItem(guid);
800 
801  assertEqual(list.matchType, restoredList.matchType);
802  assertEqual(list.limitType, restoredList.limitType);
803  assertEqual(list.selectPropertyID, restoredList.selectPropertyID);
804  assertEqual(list.selectDirection, restoredList.selectDirection);
805  assertEqual(list.limit, restoredList.limit);
806  assertEqual(list.randomSelection, restoredList.randomSelection);
807  assertEqual(list.autoUpdate, restoredList.autoUpdate);
808  assertEqual(list.conditionCount, restoredList.conditionCount);
809 
810  for (var i = 0; i < list.conditionCount; i++) {
811  assertCondition(list.getConditionAt(i), restoredList.getConditionAt(i));
812  }
813 }
814 
815 function getOperatorForProperty(propertyID, operator) {
816 
817  var propMan = Cc["@songbirdnest.com/Songbird/Properties/PropertyManager;1"]
818  .getService(Ci.sbIPropertyManager);
819  var info = propMan.getPropertyInfo(propertyID);
820  var op = info.getOperator(operator);
821  assertNotEqual(op, null);
822  return op;
823 }
824 
825 function assertCondition(a, b) {
826 
827  assertEqual(a.propertyID, b.propertyID);
828  assertEqual(a.operator.operator, b.operator.operator);
829  assertEqual(a.leftValue, b.leftValue);
830  assertEqual(a.rightValue, b.rightValue);
831  assertEqual(a.displayUnit, b.displayUnit);
832 }
833 
834 function assertTrackNumbers(list, a) {
835 
836  for (var i = 0; i < a.length; i++) {
837  assertEqual(list.getItemByIndex(i).getProperty(SB_NS + "trackNumber"), a[i]);
838  }
839 }
840 
841 function assertPropertyRange(list, start, length, prop, value) {
842 
843  for (var i = start; i < length; i++) {
844  var item = list.getItemByIndex(i);
845  assertEqual(item.getProperty(prop), value);
846  }
847 }
848 
849 function dumpList(list, sumprop) {
850  var sum = 0;
851  for (var i = 0; i < list.length; i++) {
852  var item = list.getItemByIndex(i);
853  var artist = item.getProperty(SB_NS + "artistName");
854  var album = item.getProperty(SB_NS + "albumName");
855  var track = item.getProperty(SB_NS + "trackNumber");
856  var size = item.getProperty(SB_NS + "contentLength");
857  if (sumprop) {
858  var v = parseInt(item.getProperty(sumprop));
859  sum += v;
860  }
861  log("" + i + ": " + artist + " " + album + " " + track + " " + size + (sumprop ? (" -> " + sum ) : "" ));
862  }
863 }
864 
865 function assertUnique(list) {
866 
867  var guids = {};
868 
869  for (var i = 0; i < list.length; i++) {
870  var item = list.getItemByIndex(i);
871  if (item.guid in guids) {
872  fail("list not unique, gud '" + item.guid + "' appears more than once");
873  }
874  guids[item.guid] = 1;
875  }
876 }
877 
878 function countMediaItems(library) {
879 
880  var PROP_ISLIST = "http://songbirdnest.com/data/1.0#isList";
881 
882  var listener = {
883  length: 0,
884  onEnumerationBegin: function() {
885  },
886  onEnumeratedItem: function(list, item) {
887  this.length++;
888  },
889  onEnumerationEnd: function() {
890  }
891  };
892 
893  library.enumerateItemsByProperty(PROP_ISLIST, "0",
894  listener,
895  Ci.sbIMediaList.ENUMERATIONTYPE_LOCKING);
896 
897  return listener.length;
898 }
899 
900 function sumProperty(list, prop) {
901 
902  var sum = 0;
903 
904  for (var i = 0; i < list.length; i++) {
905  var item = list.getItemByIndex(i);
906  sum += parseInt(item.getProperty(prop));
907  }
908 
909  return sum;
910 }
911 
function start(ch)
function testRandom(library)
function testBytesLimit(library)
function testAny(library)
const Cc
function fail(aMessage)
function testMatchTypeNoneRandom(library)
function testMatchTypeNoneBytesLimit(library)
function assertTrackNumbers(list, a)
function assertNotEqual(aExpected, aActual, aMessage)
function log(s)
function assertPropertyRange(list, start, length, prop, value)
function testUsecsLimit(library)
function dumpList(list, sumprop)
function assertTrue(aTest, aMessage)
const PROP_ISLIST
function testConditions(library)
function countMediaItems(library)
function runTest()
Test file.
function assertEqual(aExpected, aActual, aMessage)
function assertUnique(list)
function sumProperty(list, prop)
var getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow('Songbird SBProperties artist
Definition: tuner2.js:40
function testMatchTypeNoneUsecLimit(library)
function getOperatorForProperty(propertyID, operator)
const SB_NS
function testItemLimit(library)
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function testProperties(library)
function testOperators(library)
function assertCondition(a, b)
countRef value
Definition: FeedWriter.js:1423
const Ci
function testMatchTypeNoneItemLimit(library)
function testAll(library)
_getSelectedPageStyle s i
function testSerialize(library)