test_mediaitemwatcher.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  var mediaItem1;
33  var mediaItem2;
34  var mediaItemWatcher;
35  var filter;
36  var func;
37  var batchListener = new BatchEndListener();
38 
39  // Import some services.
40  Components.utils.import("resource://app/jsmodules/sbLibraryUtils.jsm");
41  Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
42 
43  // Make a test library.
44  var library = createLibrary("test_mediaitemwatcher");
45  var libraryManager = Cc["@songbirdnest.com/Songbird/library/Manager;1"]
46  .getService(Ci.sbILibraryManager);
47  libraryManager.registerLibrary(library, false);
48 
49  // Set up a test media item listener.
50  var listener = {
51  itemRemovedCount: 0,
52  itemUpdatedCount: 0,
53 
54  onItemRemoved: function(aMediaItem) {
55  this.itemRemovedCount++;
56  },
57  onItemUpdated: function(aMediaItem) {
58  log(aMediaItem.contentSrc.spec);
59  this.itemUpdatedCount++;
60  },
61 
62  reset: function() {
63  this.itemRemovedCount = 0;
64  this.itemUpdatedCount = 0;
65  }
66  };
67 
68 
69  //----------------------------------------------------------------------------
70  //
71  // Test basic update and removal notification.
72  //
73 
74  // Reset the listener.
75  listener.reset();
76  library.addListener(batchListener,
77  false,
78  Ci.sbIMediaList.LISTENER_FLAGS_BATCHBEGIN |
79  Ci.sbIMediaList.LISTENER_FLAGS_BATCHEND);
80  while (batchListener.depth > 0) {
81  sleep(100);
82  }
83 
84  // Create a test media item.
85  mediaItem1 = library.createMediaItem(newURI("http://test.com/test1"));
86  assertTrue(mediaItem1);
87  mediaItem2 = library.createMediaItem(newURI("http://test.com/test2"));
88  assertTrue(mediaItem2);
89 
90  // Set up a test media item watcher.
91  mediaItemWatcher = Cc["@songbirdnest.com/Songbird/Library/MediaItemWatcher;1"]
92  .createInstance(Ci.sbIMediaItemWatcher);
93  assertTrue(mediaItemWatcher);
94  mediaItemWatcher.watch(mediaItem1, listener);
95  while (batchListener.depth > 0) {
96  sleep(100);
97  }
98 
99  // Test item updated notification.
100  batchListener.waitForCompletion(function()
101  mediaItem1.setProperty(SBProperties.trackName, "test1"));
102  assertEqual(listener.itemRemovedCount, 0, "incorrect number of removed items");
103  assertEqual(listener.itemUpdatedCount, 1, "incorrect number of updated items");
104  batchListener.waitForCompletion(function()
105  mediaItem1.setProperty(SBProperties.albumName, "test1"));
106  assertEqual(listener.itemRemovedCount, 0, "incorrect number of removed items");
107  assertEqual(listener.itemUpdatedCount, 2, "incorrect number of updated items");
108  batchListener.waitForCompletion(function()
109  mediaItem2.setProperty(SBProperties.trackName, "test2"));
110  assertEqual(listener.itemRemovedCount, 0, "incorrect number of removed items");
111  assertEqual(listener.itemUpdatedCount, 2, "incorrect number of updated items");
112 
113  // Test item removed notification.
114  listener.reset();
115  batchListener.waitForCompletion(function() {
116  library.remove(mediaItem1);
117  library.remove(mediaItem2);
118  });
119  assertEqual(listener.itemRemovedCount, 1, "incorrect number of removed items");
120  assertEqual(listener.itemUpdatedCount, 0, "incorrect number of updated items");
121 
122  // Clean up.
123  mediaItemWatcher.cancel();
124  library.remove(mediaItem1);
125  library.remove(mediaItem2);
126 
127 
128  //----------------------------------------------------------------------------
129  //
130  // Test watcher cancel.
131  //
132 
133  // Reset the listener.
134  listener.reset();
135 
136  // Create a test media item.
137  mediaItem1 = library.createMediaItem(newURI("http://test.com/test1"));
138  assertTrue(mediaItem1);
139 
140  // Set up a test media item watcher.
141  mediaItemWatcher = Cc["@songbirdnest.com/Songbird/Library/MediaItemWatcher;1"]
142  .createInstance(Ci.sbIMediaItemWatcher);
143  assertTrue(mediaItemWatcher);
144  mediaItemWatcher.watch(mediaItem1, listener);
145 
146  // Test media item watcher cancel.
147  batchListener.waitForCompletion(function()
148  mediaItem1.setProperty(SBProperties.trackName, "test1"));
149  assertEqual(listener.itemRemovedCount, 0, "incorrect number of removed items");
150  assertEqual(listener.itemUpdatedCount, 1, "incorrect number of updated items");
151  mediaItemWatcher.cancel();
152  batchListener.waitForCompletion(function()
153  mediaItem1.setProperty(SBProperties.trackName, "test2"));
154  assertEqual(listener.itemRemovedCount, 0, "incorrect number of removed items");
155  assertEqual(listener.itemUpdatedCount, 1, "incorrect number of updated items");
156 
157  // Clean up.
158  mediaItemWatcher.cancel();
159  library.remove(mediaItem1);
160 
161 
162  //----------------------------------------------------------------------------
163  //
164  // Test item updated notification with filter.
165  //
166 
167  // Reset the listener.
168  listener.reset();
169 
170  // Create a test media item.
171  mediaItem1 = library.createMediaItem(newURI("http://test.com/test"));
172  assertTrue(mediaItem1);
173 
174  // Set up a test media item watcher to just watch for track name changes.
175  mediaItemWatcher = Cc["@songbirdnest.com/Songbird/Library/MediaItemWatcher;1"]
176  .createInstance(Ci.sbIMediaItemWatcher);
177  assertTrue(mediaItemWatcher);
178  filter = SBProperties.createArray([ [ SBProperties.trackName, null ] ]);
179  mediaItemWatcher.watch(mediaItem1, listener, filter);
180 
181  // Test item updated notification with filter.
182  batchListener.waitForCompletion(function()
183  mediaItem1.setProperty(SBProperties.trackName, "test"));
184  assertEqual(listener.itemRemovedCount, 0, "incorrect number of removed items");
185  assertEqual(listener.itemUpdatedCount, 1, "incorrect number of updated items");
186  batchListener.waitForCompletion(function()
187  mediaItem1.setProperty(SBProperties.albumName, "test"));
188  assertEqual(listener.itemRemovedCount, 0, "incorrect number of removed items");
189  assertEqual(listener.itemUpdatedCount, 1, "incorrect number of updated items");
190 
191  // Clean up.
192  mediaItemWatcher.cancel();
193  library.remove(mediaItem1);
194 
195 
196  //----------------------------------------------------------------------------
197  //
198  // Test item updated notification in a batch.
199  //
200 
201  // Reset the listener.
202  listener.reset();
203 
204  // Create two test media items.
205  mediaItem1 = library.createMediaItem(newURI("http://test.com/test"));
206  assertTrue(mediaItem1);
207  mediaItem2 = library.createMediaItem(newURI("http://test.com/test"));
208  assertTrue(mediaItem2);
209 
210  // Set up a test media item watcher to just watch for track name changes.
211  mediaItemWatcher = Cc["@songbirdnest.com/Songbird/Library/MediaItemWatcher;1"]
212  .createInstance(Ci.sbIMediaItemWatcher);
213  assertTrue(mediaItemWatcher);
214  mediaItemWatcher.watch(mediaItem1, listener);
215 
216  // Test item updated notification in a batch.
217  func = function() {
218  mediaItem1.setProperty(SBProperties.trackName, "test");
219  mediaItem2.setProperty(SBProperties.trackName, "test");
220  assertEqual(listener.itemRemovedCount, 0, "incorrect number of removed items");
221  assertEqual(listener.itemUpdatedCount, 0, "incorrect number of updated items");
222  };
223  library.runInBatchMode(func);
224  assertEqual(listener.itemRemovedCount, 0, "incorrect number of removed items");
225  assertEqual(listener.itemUpdatedCount, 1, "incorrect number of updated items");
226 
227  // Clean up.
228  mediaItemWatcher.cancel();
229  library.remove(mediaItem1);
230  library.remove(mediaItem2);
231 
232 
233  //----------------------------------------------------------------------------
234  //
235  // Test item removal notification in a batch.
236  //
237 
238  // Reset the listener.
239  listener.reset();
240 
241  // Create two test media items.
242  mediaItem1 = library.createMediaItem(newURI("http://test.com/test"));
243  assertTrue(mediaItem1);
244  mediaItem2 = library.createMediaItem(newURI("http://test.com/test"));
245  assertTrue(mediaItem2);
246 
247  // Set up a test media item watcher.
248  mediaItemWatcher = Cc["@songbirdnest.com/Songbird/Library/MediaItemWatcher;1"]
249  .createInstance(Ci.sbIMediaItemWatcher);
250  assertTrue(mediaItemWatcher);
251  mediaItemWatcher.watch(mediaItem1, listener);
252 
253  // Test item updated notification in a batch.
254  func = function() {
255  library.remove(mediaItem1);
256  library.remove(mediaItem2);
257  assertEqual(listener.itemRemovedCount, 0, "incorrect number of removed items");
258  assertEqual(listener.itemUpdatedCount, 0, "incorrect number of updated items");
259  };
260  library.runInBatchMode(func);
261  assertEqual(listener.itemRemovedCount, 1, "incorrect number of removed items");
262  assertEqual(listener.itemUpdatedCount, 0, "incorrect number of updated items");
263 
264  // Clean up.
265  mediaItemWatcher.cancel();
266  library.remove(mediaItem1);
267  library.remove(mediaItem2);
268  while (batchListener.depth > 0) {
269  sleep(100);
270  }
271  library.removeListener(batchListener);
272 
273  // Final clean up.
274  libraryManager.unregisterLibrary(library);
275 }
276 
function sleep(ms, suppressOutput)
const Cc
function log(s)
function assertTrue(aTest, aMessage)
function assertEqual(aExpected, aActual, aMessage)
function runTest()
Test file.
function BatchEndListener()
var libraryManager
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function newURI(aURLString)
const Ci
Array filter(tab.attributes, function(aAttr){return(_this.xulAttributes.indexOf(aAttr.name) >-1);}).forEach(tab.removeAttribute