test_libraryservicepane.js
Go to the documentation of this file.
1 
31 const DEBUG_OUTPUT = false;
32 
33 const PROP_ISLIST = "http://songbirdnest.com/data/1.0#isList";
34 const LSP='http://songbirdnest.com/rdf/library-servicepane#';
35 
36 var servicePane;
39 
40 // Used to track things added using the tests
41 var listsToRemove = [];
43 var nodesToRemove = [];
44 
45 
46 
47 function runTest () {
48  return;
49  setup();
50 
52 
54 
56 
58 
60 
62 
63  testRenaming();
64 
66 
68 
70 
72 
73  servicePane = null;
76 }
77 
78 
79 function setup() {
80  servicePane = Components.classes['@songbirdnest.com/servicepane/service;1']
81  .getService(Components.interfaces.sbIServicePaneService);
82  assertNotEqual(servicePane, null);
83 
84  libraryServicePane = Components.classes['@songbirdnest.com/servicepane/library;1']
85  .getService(Components.interfaces.sbILibraryServicePaneService);
87 
88  libraryManager = Components.classes['@songbirdnest.com/Songbird/library/Manager;1']
89  .getService(Components.interfaces.sbILibraryManager);
91 
92  // Start with an empty library
93  libraryManager.mainLibrary.clear();
94 }
95 
96 
97 
101 function removeAddedItems() {
102  DBG("About to remove all test items from tree");
103  showTree(servicePane.root, "tree-before-delete: ");
104 
105  // Remove all media lists added
106  for (var i = 0; i < listsToRemove.length; i++) {
107  listsToRemove[i].library.remove(listsToRemove[i]);
108  }
109 
110  // Remove all libraries added
111  for (var i = 0; i < librariesToRemove.length; i++) {
112  libraryManager.unregisterLibrary(librariesToRemove[i]);
113  }
114 
115  DBG("Finished removing test items from tree");
116  showTree(servicePane.root, "tree-after-delete: ");
117 }
118 
119 
120 
122 // Helpers //
124 
128 function DBG(s) {
129  if (DEBUG_OUTPUT) {
130  // Madness. If I don't use this intermediate var
131  // it warns me that DBG.caller has no properties,
132  // but then works just fine anyway.
133  var myCaller = DBG.caller;
134  //dump('DBG:test_libraryservicepane:' + myCaller.name + ": " +s+'\n');
135  log('DBG:test_libraryservicepane:' + myCaller.name + ": " +s+'\n');
136  }
137 }
138 
142 function showTree(root, prefix) {
143  var node = root.firstChild;
144  while(node) {
145  DBG(prefix + node.name + " [" + node.id + "]");
146  showTree(node, prefix + " ");
147  node = node.nextSibling;
148  }
149 }
150 
151 
153 // Tests //
155 
156 
162  var libraries = libraryManager.getLibraries();
163  var count = 0;
164 
165  DBG("About to check library manager against the service tree:");
166  showTree(servicePane.root, "before-checking-libraries: ");
167  assertNotEqual(servicePane.root, null);
168 
169  while (libraries.hasMoreElements()) {
170  var library = libraries.getNext();
171  count++;
172 
173  var node = libraryServicePane.getNodeForLibraryResource(library);
174  assertNotEqual(node, null);
175 
176  // Test that we can get from the node back to the library
177  var libraryFromSP = libraryServicePane.getLibraryResourceForNode(node)
178  assertEqual(library, libraryFromSP);
179 
180  // All libraries should be at the top level
181  assertNotEqual(node.parentNode, null);
182  assertEqual(node.parentNode, servicePane.root);
183 
184  // All libraries should be visible
185  assertEqual(node.hidden, false);
186 
187  // The name should match
188  assertEqual(node.name, library.name);
189 
190  // Make sure the playlists of this library are reflected in the tree
191  testLibraryMediaLists(library);
192 
193  // Make sure createNodeForLibrary returns same node as getNodeForLibraryResource.
194  var createNode = libraryServicePane.createNodeForLibrary(library);
195  assertEqual(node, createNode);
196  }
197 
198  var library = createLibrary("test-createNodeForLibrary");
199  library.setProperty(SBProperties.hidden, true);
200  library.name = "Testing createNodeForLibrary";
201  libraryManager.registerLibrary(library, false);
202 
203  var node = libraryServicePane.createNodeForLibrary(library);
204 
205  assertNotEqual(node, null);
206  assertEqual(node.hidden, true);
207  assertEqual(node.name, library.name);
208 
209  libraryManager.unregisterLibrary(library);
210 
211  // We should have seen at least the main and web libs
212  DBG("processed " + count + " libraries");
213  assertEqual(count >= 2, true);
214 
215  DBG("done");
216 }
217 
218 
223 function testLibraryMediaLists(aLibrary) {
224  // Listener to receive enumerated items and store then in an array
225  var listener = {
226  items: [],
227  onEnumerationBegin: function() { },
228  onEnumerationEnd: function() { },
229  onEnumeratedItem: function(list, item) {
230  this.items.push(item);
231  }
232  };
233 
234  // Enumerate all lists in this library
235  aLibrary.enumerateItemsByProperty(PROP_ISLIST, "1",
236  listener,
237  Components.interfaces.sbIMediaList.ENUMERATIONTYPE_LOCKING);
238 
239  // Make sure we have a node for each list
240  for (var i = 0; i < listener.items.length; i++) {
241  var node = libraryServicePane.getNodeForLibraryResource(listener.items[i]);
242  assertNotEqual(node, null);
243  assertNotEqual(node.parentNode, null);
244 
245  var medialistFromSP = libraryServicePane.getNodeForLibraryResource(listener.items[i]);
246  assertNotEqual(listener.items[i], medialistFromSP);
247 
248  // Test that we can get from the node back to the library
249  var medialistFromSP = libraryServicePane.getLibraryResourceForNode(node)
250  assertEqual(listener.items[i], medialistFromSP);
251 
252  // All libraries should be visible
253  //assertEqual(node.hidden, false);
254 
255  // The name should match
256  assertEqual(node.name, listener.items[i].name);
257 
258  // Main library playlists should be at the top level
259  if (aLibrary == libraryManager.mainLibrary) {
260  assertEqual(node.parentNode, servicePane.root);
261 
262  // All others should be nested
263  } else {
264  var libraryNode = libraryServicePane.getNodeForLibraryResource(aLibrary);
265  assertEqual(node.parentNode, libraryNode);
266  }
267  }
268 }
269 
270 
271 
277 
278  var library = libraryManager.mainLibrary;
279 
280  // Create the playlist
281  var mediaList = library.createMediaList("simple");
282  mediaList.name = "test playlist";
283 
284  // Make sure the playlist appears in the tree
285  var node = libraryServicePane.getNodeForLibraryResource(mediaList);
286  assertNotEqual(node, null);
287 
288  // Make sure the name is the same
289  assertEqual(node.name, mediaList.name);
290 
291  // Make sure this will get cleaned up
292  listsToRemove.push(mediaList);
293 }
294 
295 
296 
304 function testInsertionLogic() {
305 
306  // Insert media lists into the main library
307  var list1 = libraryManager.mainLibrary.createMediaList("simple");
308  var list2 = libraryManager.mainLibrary.createMediaList("simple");
309  list1.name = "Test1";
310  list2.name = "Test2";
311 
312  // Make sure they appeared in the tree
313  var node1 = libraryServicePane.getNodeForLibraryResource(list1);
314  var node2 = libraryServicePane.getNodeForLibraryResource(list2);
315  assertNotEqual(node1, null);
316  assertNotEqual(node2, null);
317 
318  // Should show up after the main library but before any bookmarks
319  // or other content
320  var mainLibraryNode = libraryServicePane.getNodeForLibraryResource(
321  libraryManager.mainLibrary);
322  var node = mainLibraryNode.nextSibling;
323  while (node && node.contractid == mainLibraryNode.contractid) {
324  if (node == node1) {
325  break;
326  }
327  node = node.nextSibling;
328  }
329  assertNotEqual(node, null);
330  assertEqual(node, node1);
331 
332  // The second playlist should appear after the first
333  assertEqual(node1.nextSibling, node2);
334 
335  // Now pretend the user moves their playlists in between some
336  // of their bookmark folders
337  var bmNode1 = servicePane.createNode();
338  bmNode1.id = "urn:test:1";
339  servicePane.root.appendChild(bmNode1);
340 
341  var bmNode2 = servicePane.createNode();
342  bmNode1.id = "urn:test:2";
343  servicePane.root.appendChild(bmNode2);
344 
345  DBG("About to move all playlists between some bookmarks");
346  showTree(servicePane.root, "tree-before-move: ");
347  var type = node1.getAttributeNS(LSP, "ListType");
348  node = mainLibraryNode;
349  // Search for playlists between the library and the first fake bookmark
350  DBG("About to iterate");
351  while (node && node != bmNode1) {
352  // If this is a playlist then move it in between our fake bookmarks
353  DBG("checking node " + node.id);
354  if (node.getAttributeNS(LSP, "ListType") == type) {
355  DBG("moving node " + node.id);
356  node = node.nextSibling;
357  servicePane.root.insertBefore(node.previousSibling, bmNode2);
358  } else {
359  node = node.nextSibling;
360  }
361  }
362  DBG("Finished moving playlists");
363  showTree(servicePane.root, "tree-after-move: ");
364  assertEqual(bmNode1, node);
365 
366  // Newly inserted playlists should appear directly below other playlists
367  // in the middle of the bookmarks
368  var list3 = libraryManager.mainLibrary.createMediaList("simple");
369  list3.name = "Test3";
370 
371  DBG("After adding a new playlist:");
372  showTree(servicePane.root, "tree-after-add: ");
373 
374  var node3 = libraryServicePane.getNodeForLibraryResource(list3);
375  assertNotEqual(node3, null);
376  assertEqual(node2.nextSibling, node3);
377  assertEqual(node3.nextSibling, bmNode2);
378 
379  // Now add a new library so that we can test nesting
380  var newLibrary = createLibrary("test1");
381  newLibrary.name = "Test1";
382  libraryManager.registerLibrary(newLibrary, false);
383 
384  DBG("After adding a new library:");
385  showTree(servicePane.root, "tree-after-newlibrary: ");
386 
387  // New library should appear below the other libraries
388  var newLibraryNode = libraryServicePane.getNodeForLibraryResource(newLibrary);
389  assertNotEqual(newLibraryNode, null);
390  assertEqual(newLibraryNode.name, newLibrary.name);
391  type = newLibraryNode.getAttributeNS(LSP, "ListType");
392  node = newLibraryNode.previousSibling;
393  while (node && node.getAttributeNS(LSP, "ListType") == type &&
394  node != mainLibraryNode)
395  {
396  DBG("Scanning backwards from new library to main library: " + node.id);
397  node = node.previousSibling;
398  }
399  assertNotEqual(node, null);
400  assertEqual(node, mainLibraryNode);
401 
402  // Insert a playlist into the new library
403  var list4 = newLibrary.createMediaList("simple");
404  list4.name = "Test4";
405 
406  DBG("After adding a playlist into the new library:");
407  showTree(servicePane.root, "tree-after-newlibrary-playlist: ");
408 
409  // Should appear nested under the library
410  var node4 = libraryServicePane.getNodeForLibraryResource(list4);
411  assertNotEqual(node4, null);
412  assertEqual(node4.name, list4.name);
413  assertEqual(node4.parentNode, newLibraryNode);
414 
415  // Make sure the things we added will get removed
416  listsToRemove = listsToRemove.concat(list1, list2, list3, list4);
417  librariesToRemove = librariesToRemove.concat(newLibrary);
418  nodesToRemove = nodesToRemove.concat(node1, node2, bmNode1, bmNode2, node3, newLibraryNode, node4);
419 }
420 
421 
427  DBG("About to test library suggestion functionality");
428 
429  // No selection should give the main library
430  var suggestedLibrary = libraryServicePane.suggestLibraryForNewList(
431  "simple", null);
432  assertEqual(suggestedLibrary, libraryManager.mainLibrary);
433 
434  // Add a new library so that we can test nesting
435  var newLibrary = createLibrary("suggestion-test");
436  newLibrary.name = "Test2";
437  libraryManager.registerLibrary(newLibrary, false);
438 
439  // New library should appear below the other libraries
440  var newLibraryNode = libraryServicePane.getNodeForLibraryResource(newLibrary);
441 
442  // Selection of the new library should return the new library
443  suggestedLibrary = libraryServicePane.suggestLibraryForNewList(
444  "simple", newLibraryNode);
445  assertEqual(suggestedLibrary, newLibrary);
446 
447 
448  // Selection of a playlist in the new library should
449  // return the new library
450  var list1 = newLibrary.createMediaList("simple");
451  list1.name = "Test1";
452  var node1 = libraryServicePane.getNodeForLibraryResource(list1);
453  suggestedLibrary = libraryServicePane.suggestLibraryForNewList(
454  "simple", node1);
455  assertEqual(suggestedLibrary, newLibrary);
456 
457  // Selection of a new library playlist placed at the top
458  // level should still return the new library
459  var list2 = newLibrary.createMediaList("simple");
460  list2.name = "Test2";
461  var node2 = libraryServicePane.getNodeForLibraryResource(list2);
462  suggestedLibrary = libraryServicePane.suggestLibraryForNewList(
463  "simple", node2);
464  assertEqual(suggestedLibrary, newLibrary);
465 
466  // Selection of a bookmark under the new library should
467  // return the new library
468  var bmNode1 = servicePane.createNode();
469  bmNode1.id = "urn:select-test:1";
470  newLibraryNode.appendChild(bmNode1);
471 
472  var suggestedLibrary = libraryServicePane.suggestLibraryForNewList(
473  "simple", bmNode1);
474  assertEqual(suggestedLibrary, newLibrary);
475 
476  // Selection of a bookmark at the top level should
477  // return the main library
478  var bmNode2 = servicePane.createNode();
479  bmNode2.id = "urn:select-test:2";
480  servicePane.root.appendChild(bmNode2);
481 
482  var suggestedLibrary = libraryServicePane.suggestLibraryForNewList(
483  "simple", bmNode2);
484  assertEqual(suggestedLibrary, libraryManager.mainLibrary);
485 
486  // Requesting an unsupported type should give null
487  var suggestedLibrary = libraryServicePane.suggestLibraryForNewList(
488  "fakemedialisttype", null);
489  assertEqual(suggestedLibrary, null);
490 
491  DBG("Tree after suggestion testing:");
492  showTree(servicePane.root, "tree-after-newlibrary: ");
493 
494 
495  // Make sure the things we added will get removed
496  listsToRemove = listsToRemove.concat(list1, list2);
497  librariesToRemove = librariesToRemove.concat(newLibrary);
498  nodesToRemove = nodesToRemove.concat(node1, node2, bmNode1, bmNode2, newLibraryNode);
499 }
500 
501 
502 
507 function testRenaming() {
508  DBG("About to test library renaming functionality");
509  // Add a new library to rename
510  var newLibrary = createLibrary("renaming-test");
511  newLibrary.name = "Test2";
512  libraryManager.registerLibrary(newLibrary, false);
513  var newLibraryNode = libraryServicePane.getNodeForLibraryResource(newLibrary);
514 
515  // Add a list to the library
516  var list1 = newLibrary.createMediaList("simple");
517  list1.name = "Test1";
518  var node1 = libraryServicePane.getNodeForLibraryResource(list1);
519 
520  // Fake a rename on the library
521  var newName = "RenameTest1";
522  servicePane.onRename(newLibraryNode, newName);
523  // Make sure the name was changed correctly
524  assertEqual(newLibraryNode.name, newName);
525 
526  // Fake a rename on the playlist
527  newName = "RenameTest2";
528  servicePane.onRename(node1, newName);
529  // Make sure the name was changed correctly
530  assertEqual(node1.name, newName);
531 
532  // Make sure that renaming to an empty string is not accepted
533  servicePane.onRename(node1, "");
534  assertEqual(node1.name, newName);
535 
536  // Make sure the things we added will get removed
537  listsToRemove = listsToRemove.concat(list1);
538  librariesToRemove = librariesToRemove.concat(newLibrary);
539  nodesToRemove = nodesToRemove.concat(node1, newLibraryNode);
540 }
541 
542 
548 
549  // Remove all service tree nodes added
550  for (var i = 0; i < nodesToRemove.length; i++) {
551  DBG("Confirming removal of: " + nodesToRemove[i].id );
552  var id = nodesToRemove[i].id;
553  // Playlist nodes should be removed
554  if (id.indexOf("urn:item") == 0) {
555  assertEqual(nodesToRemove[i].parentNode, null);
556  // Library nodes should be hidden
557  } else if (id.indexOf("urn:library") == 0) {
559  nodesToRemove[i].parentNode.removeChild(nodesToRemove[i]);
560  } else {
561  nodesToRemove[i].parentNode.removeChild(nodesToRemove[i]);
562  }
563  }
564 }
565 
566 
567 
568 
569 
570 
function testLibrarySuggestion()
function runTest()
Advanced DataRemote unit tests.
function testAllItemsRemoved()
function assertNotEqual(aExpected, aActual, aMessage)
function setup()
var libraryServicePane
function log(s)
function testRenaming()
const PROP_ISLIST
function DBG(s)
function assertEqual(aExpected, aActual, aMessage)
return elem filter &&elem filter indexOf("opacity=")>=0?(parseFloat(elem.filter.match(/opacity
function testInsertionLogic()
function testLibraryMediaLists(aLibrary)
function testLibrariesAndContents()
function showTree(root, prefix)
var count
Definition: test_bug7406.js:32
var nodesToRemove
var libraryManager
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
let node
var librariesToRemove
var hidden
var listsToRemove
function testAddingSimplePlaylists()
_getSelectedPageStyle s i
function removeAddedItems()
const DEBUG_OUTPUT
Basic library service pane unit tests.