test_library_batchcreateifnotexist.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-2009 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 
31  Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
32 
33  var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
34 
35  var databaseGUID = "test_batchcreateifnotexist";
36  var library = createLibrary(databaseGUID);
37 
38  var toAdd = Cc["@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
39  .createInstance(Ci.nsIMutableArray);
40  var propertyArray = Cc["@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
41  .createInstance(Ci.nsIMutableArray);
42  for (var i = 1; i < 101; i++) {
43  toAdd.appendElement(newURI("file:///foo/" + i + ".mp3"), false);
44  var props =
45  Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"]
46  .createInstance(Ci.sbIMutablePropertyArray);
47  props.appendProperty(SBProperties.contentLength, i);
48  props.appendProperty(SBProperties.trackNumber, i);
49  propertyArray.appendElement(props, false);
50  }
51 
52  var libraryListener = new TestMediaListListener();
53  library.addListener(libraryListener);
54 
55  var resultItemArray = {};
56  var itemsCreated = library.batchCreateMediaItemsIfNotExist(toAdd,
57  propertyArray,
58  resultItemArray);
59  resultItemArray = resultItemArray.value;
60 
61  // Check that all items were newly created
62  var e = itemsCreated.enumerate();
63  while(e.hasMoreElements()) {
64  var itemCreated = e.getNext().QueryInterface(Ci.nsIVariant);
65  assertTrue(itemCreated);
66  }
67 
68  e = resultItemArray.enumerate();
69  while(e.hasMoreElements()) {
70  var item = e.getNext();
71 
72  var listener = {
73  _item: null,
74  onEnumerationBegin: function() {
75  },
76  onEnumeratedItem: function(list, item) {
77  this._item = item;
78  return Ci.sbIMediaListEnumerationListener.CANCEL;
79  },
80  onEnumerationEnd: function() {
81  }
82  };
83 
84  library.enumerateItemsByProperty(SBProperties.contentURL,
85  item.contentSrc.spec,
86  listener,
87  Ci.sbIMediaList.ENUMERATIONTYPE_SNAPSHOT);
88 
89  assertEqual(listener._item, item);
90 
91  assertEqual(listener._item.getProperty(SBProperties.contentLength),
92  item.getProperty(SBProperties.contentLength));
93  assertEqual(listener._item.getProperty(SBProperties.trackNumber),
94  item.getProperty(SBProperties.trackNumber));
95  }
96 
97  // Check the order of the notifications
98  assertEqual(libraryListener.added.length, 100);
99  for (var i = 1; i < 101; i++) {
100  assertEqual(libraryListener.added[i - 1].item.contentSrc.spec,
101  "file:///foo/" + i + ".mp3");
102  }
103  libraryListener.reset();
104 
105  // Do it again with duplcate URLs
106  toAdd = Cc["@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
107  .createInstance(Ci.nsIMutableArray);
108  propertyArray = Cc["@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
109  .createInstance(Ci.nsIMutableArray);
110  for (var i = 1; i < 101; i++) {
111  toAdd.appendElement(newURI("file:///foo/duplicate.mp3"), false);
112  var props =
113  Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"]
114  .createInstance(Ci.sbIMutablePropertyArray);
115  props.appendProperty(SBProperties.contentLength, i);
116  props.appendProperty(SBProperties.trackNumber, i);
117  propertyArray.appendElement(props, false);
118  }
119 
120  resultItemArray = {};
121  itemsCreated = library.batchCreateMediaItemsIfNotExist(toAdd,
122  propertyArray,
123  resultItemArray);
124  resultItemArray = resultItemArray.value;
125 
126  // Check that only the first item was newly created
127  var firstItem = true;
128  e = itemsCreated.enumerate();
129  while(e.hasMoreElements()) {
130  var itemCreated = e.getNext().QueryInterface(Ci.nsIVariant);
131  if (firstItem)
132  assertTrue(itemCreated);
133  else
134  assertFalse(itemCreated);
135  firstItem = false;
136  }
137 
138  // Check that all the items were properly returned
139  assertEqual(resultItemArray.length, 100);
140  e = resultItemArray.enumerate();
141  while(e.hasMoreElements()) {
142  var item = e.getNext();
143  assertEqual(item.contentSrc.spec, "file:///foo/duplicate.mp3");
144  }
145 
146  // Check the order of the notifications
147  assertEqual(libraryListener.added.length, 1);
148  assertEqual(libraryListener.added[0].item.contentSrc.spec,
149  "file:///foo/duplicate.mp3");
150 
151  library.removeListener(libraryListener);
152 }
const Cc
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)
const Ci
var ios
Definition: head_feeds.js:5
_getSelectedPageStyle s i
function TestMediaListListener()
function runTest()
Test file.