test_allowduplicates.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  Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
34 
35  var databaseGUID = "test_allowduplicates";
36  var library = createLibrary(databaseGUID, null, false);
37 
38  var uri = newURI("http://example.com/foo.mp3");
39 
40  // Default behavior is to not allow dups
41  var item1 = library.createMediaItem(uri);
42  var item2 = library.createMediaItem(uri);
43 
44  assertTrue(item1.equals(item2));
45 
46  var item3 = library.createMediaItem(uri, null, true);
47  assertFalse(item1.equals(item3));
48 
49  library.clear();
50 
51  var toAdd = Cc["@songbirdnest.com/moz/xpcom/threadsafe-array;1"].createInstance(Ci.nsIMutableArray);
52  toAdd.appendElement(newURI("http://example.com/foo.mp3"), false);
53  toAdd.appendElement(newURI("http://example.com/foo.mp3"), false);
54  toAdd.appendElement(newURI("http://example.com/foo1.mp3"), false);
55  toAdd.appendElement(newURI("http://example.com/foo1.mp3"), false);
56 
57  var added = library.batchCreateMediaItems(toAdd);
58  assertEqual(added.length, 2);
59  assertEqual(library.length, 2);
60 
61  var added = library.batchCreateMediaItems(toAdd);
62  assertEqual(added.length, 0);
63  assertEqual(library.length, 2);
64 
65  var added = library.batchCreateMediaItems(toAdd, null, true);
66  assertEqual(added.length, 4);
67  assertEqual(library.length, 6);
68 
69  library.clear();
70 
71  var listener = {
72  arrayLength: 0,
73  libraryLength: 0,
74  that: this,
75  onProgress: function(index) {
76  },
77  onComplete: function(array, result) {
78  assertEqual(result, Cr.NS_OK);
79  assertEqual(array.length, this.arrayLength);
80  assertEqual(library.length, this.libraryLength);
81 
82  // Verify that the media item content src matches the origin URL
83  for (var i = 0; i < array.length; i++) {
84  var mediaItem = array.queryElementAt(i, Ci.sbIMediaItem);
85  var originURL = mediaItem.getProperty(SBProperties.originURL);
86  assertEqual(mediaItem.contentSrc.spec, originURL);
87  }
88 
89  this.that.testFinished();
90  test = null;
91  }
92  };
93 
94  // Create media item properties to match media items to content src URI's.
95  var toAddProps =
96  Cc["@songbirdnest.com/moz/xpcom/threadsafe-array;1"].createInstance(Ci.nsIMutableArray);
97  for (var i = 0; i < toAdd.length; i++) {
98  var uri = toAdd.queryElementAt(i, Ci.nsIURI);
99  var properties = [ [ SBProperties.originURL, uri.spec ] ];
100  toAddProps.appendElement(SBProperties.createArray(properties), false);
101  }
102 
103  // 2 tracks added, library will have 2 items, no duplicates
104  listener.arrayLength = 2;
105  listener.libraryLength = 2;
106  library.batchCreateMediaItemsAsync(listener, toAdd, toAddProps);
107  testPending();
108 
109  // 0 tracks added, library will have 2 items, no duplicates
110  listener.arrayLength = 0;
111  listener.libraryLength = 2;
112  library.batchCreateMediaItemsAsync(listener, toAdd, toAddProps);
113  testPending();
114 
115  // 4 tracks added, library will have 6 items, duplicates allowed
116  listener.arrayLength = 4;
117  listener.libraryLength = 6;
118  library.batchCreateMediaItemsAsync(listener, toAdd, toAddProps, true);
119  testPending();
120 
121 }
122 
const Cc
inArray array
function onComplete(job)
Definition: test_bug7406.js:85
function runTest()
Test file.
function assertTrue(aTest, aMessage)
function assertEqual(aExpected, aActual, aMessage)
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function newURI(aURLString)
function assertFalse(aTest, aMessage)
var uri
Definition: FeedWriter.js:1135
const Cr
const Ci
_getSelectedPageStyle s i
function testPending()