test_library.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 SB_NS = "http://songbirdnest.com/data/1.0#";
34  var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
35 
36  var databaseGUID = "test_library";
37  var library = createLibrary(databaseGUID);
38 
39  // test create
40  var uriSpec = "file:///foo";
41  var uri = ios.newURI(uriSpec, null, null);
42 
43  var item1 = library.createMediaItem(uri);
44  var now = new Date();
45  assertEqual(item1.getProperty(SB_NS + "contentURL"), uriSpec);
46 
47  var created = new Date(parseInt(item1.getProperty(SB_NS + "created")));
48  var updated = new Date(parseInt(item1.getProperty(SB_NS + "updated")));
49 
50  assertEqual(created.getTime() == updated.getTime(), true);
51  // Compare the dates with a little slack -- they should be very close
52  assertEqual(now - created < 5000, true);
53  assertEqual(now - updated < 5000, true);
54 
55  // Make sure we are getting different guids
56  var item2 = library.createMediaItem(uri, null, true);
57  assertNotEqual(item1.guid, item2.guid);
58 
59  // Test that they items were added to the library view the view list
60  assertEqual(library.getItemByGuid(item1.guid).guid, item1.guid);
61  assertEqual(library.getItemByGuid(item2.guid).guid, item2.guid);
62 
63  var listListener = new TestMediaListListener();
64  library.addListener(listListener, false);
65 
66  var uri2 = ios.newURI("file:///bar", null, null);
67  var item3 = library.createMediaItem(uri2, null, true);
68 
69  assertTrue(item3.equals(listListener.added[0].item));
70 
71  library.remove(item1);
72  assertTrue(item1.equals(listListener.removedBefore[0].item));
73  assertTrue(item1.equals(listListener.removedAfter[0].item));
74 
75  // test createMediaItemIfNotExist
76  var item4 = {};
77  var hasItem4 = library.createMediaItemIfNotExist(uri2, null, item4);
78  assertFalse(hasItem4);
79  assertTrue(item4.value.equals(item3));
80  var uri3 = ios.newURI("file:///quux", null, null);
81  hasItem4 = library.createMediaItemIfNotExist(uri3, null, item4);
82  assertTrue(hasItem4);
83  assertFalse(item4.value.equals(item3));
84  library.remove(item4.value);
85 
86  // Test if removing items from the library also remove items from the
87  // playlist.
88  var enumerationListener = new TestMediaListEnumerationListener();
89 
90  var list = library.getMediaItem("7e8dcc95-7a1d-4bb3-9b14-d4906a9952cb");
91  assertEqual(list.length, 20);
92 
93  list.enumerateAllItems(enumerationListener,
94  Ci.sbIMediaList.ENUMERATIONTYPE_SNAPSHOT);
95 
96  var listCount = enumerationListener.count;
97  assertTrue(listCount > 0);
98 
99  listListener.reset();
100  var removedItemCount = 0;
101  listListener.onAfterItemRemoved = function onAfterItemRemoved(list, item) {
102  removedItemCount++;
103  }
104 
105  library.removeSome(enumerationListener.QueryInterface(Ci.nsISimpleEnumerator));
106 
107  assertEqual(removedItemCount, listCount);
108 
109  assertEqual(list.length, 0);
110 
111  library.removeListener(listListener);
112 
113  // Now test if clearing the library also clears the playlist.
114  library = createLibrary(databaseGUID);
115  list = library.getMediaItem("7e8dcc95-7a1d-4bb3-9b14-d4906a9952cb");
116 
117  assertEqual(list.length, 20);
118 
119  library.clear();
120 
121  assertEqual(list.length, 0);
122  // Test that modifying a media item notifies the library and playlist.
123  library = createLibrary(databaseGUID);
124  list = library.getMediaItem("7e8dcc95-7a1d-4bb3-9b14-d4906a9952cb");
125  var item = list.getItemByIndex(8);
126 
127  listListener = new TestMediaListListener();
128  list.addListener(listListener, false);
129 
130  var libraryListener = new TestMediaListListener();
131  library.addListener(libraryListener, false);
132 
133  item.contentType = "foo/foo";
134 
135  assertEqual(listListener.updatedItem, item);
136  assertEqual(libraryListener.updatedItem, item);
137 
138  list.removeListener(listListener);
139  library.removeListener(libraryListener);
140 
141  // Test that modifying an item doesn't notify lists that don't contain the
142  // item.
143  library = createLibrary(databaseGUID);
144  list = library.getMediaItem("7e8dcc95-7a1d-4bb3-9b14-d4906a9952cb");
145 
146  listListener = new TestMediaListListener();
147  list.addListener(listListener, false);
148 
149  item = library.getItemByGuid("3E63F4C2-AD99-11DB-9321-C22AB7121F49");
150 
151  libraryListener = new TestMediaListListener();
152  library.addListener(libraryListener, false);
153 
154  item.contentType = "foo/foo";
155 
156  assertNotEqual(listListener.updatedItem, item);
157  assertEqual(libraryListener.updatedItem, item);
158 
159  list.removeListener(listListener);
160  library.removeListener(libraryListener);
161 
162  // Test create with properties
163  var props = createPropertyArray();
164  props.appendProperty(SB_NS + "artistName", "a-ha");
165  props.appendProperty(SB_NS + "albumName", "Back in Black");
166  props.appendProperty(SB_NS + "contentLength", "123");
167  props.appendProperty(SB_NS + "contentURL", "http://www.foo.com/foo.mp3");
168  var item4 = library.createMediaItem(uri, props);
169 
170  // contentURL should not have been affected by the property list
171  assertEqual(item4.getProperty(SB_NS + "contentURL"), uriSpec);
172  assertEqual(item4.getProperty(SB_NS + "artistName"), "a-ha");
173  assertEqual(item4.getProperty(SB_NS + "albumName"), "Back in Black");
174  assertEqual(item4.getProperty(SB_NS + "contentLength"), "123");
175 
176  // Make sure that items can't be resurrected once removed.
177  var newItem = library.createMediaItem(uri, null, true);
178  var guid = newItem.guid;
179 
180  library.remove(newItem);
181 
182  var exceptionResult;
183  try {
184  var item = library.getItemByGuid(guid);
185  }
186  catch (e) {
187  exceptionResult = e.result;
188  }
189  assertEqual(exceptionResult, Cr.NS_ERROR_NOT_AVAILABLE);
190 
191  // Removing a removed item should do nothing
192  library.remove(newItem);
193 
194  var databaseGUID = "test_library_clearItems";
195  var library = createLibrary(databaseGUID);
196 
197  var list = library.createMediaList("simple");
198  var itemFoo = library.createMediaItem(uri, null, true);
199  var itemBar = library.createMediaItem(uri2, null, true);
200 
201  var itemFooGUID = itemFoo.guid;
202 
203  list.add(itemFoo);
204  list.add(itemBar);
205  assertEqual(list.length, 2);
206  library.clearItems();
207  assertEqual(list.length, 0);
208 
209  try {
210  let deadItemFoo = library.getMediaItem(itemFooGUID);
211  throw "ERROR! Dead Item in mMediaItemInfoTable!";
212  }
213  catch(e) {
214  // Everything is fine if we get NS_ERROR_NOT_AVAILABLE.
215  // Other exceptions are bad.
216  if(e.result != Cr.NS_ERROR_NOT_AVAILABLE) {
217  throw e;
218  }
219  }
220 
221  var itemFoo2 = library.createMediaItem(uri, null, true);
222  var itemBar2 = library.createMediaItem(uri2, null, true);
223 
224  itemFoo2.setProperty(SB_NS + "contentType", "video");
225  itemBar2.setProperty(SB_NS + "contentType", "audio");
226 
227  list.add(itemFoo2);
228  list.add(itemBar2);
229  assertEqual(list.length, 2);
230  library.clearItemsByType("audio");
231  assertEqual(list.length, 1);
232 }
233 
235  return Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"]
236  .createInstance(Ci.sbIMutablePropertyArray);
237 }
238 
const Cc
function assertNotEqual(aExpected, aActual, aMessage)
function assertTrue(aTest, aMessage)
function assertEqual(aExpected, aActual, aMessage)
const SB_NS
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function assertFalse(aTest, aMessage)
var uri
Definition: FeedWriter.js:1135
function runTest()
Test file.
Definition: test_library.js:31
const Cr
const Ci
function now()
var ios
Definition: head_feeds.js:5
function TestMediaListListener()
function createPropertyArray()