Home | History | Annotate | Download | only in js
      1 <!DOCTYPE html>
      2 <html>
      3 <head>
      4 <!-- TODO(arv): Check in Closue unit tests and make this run as part of the
      5      tests -->
      6 <script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js"></script>
      7 <script src="cr.js"></script>
      8 <script src="cr/promise.js"></script>
      9 <script src="bmm/treeiterator.js"></script>
     10 <script src="bmm.js"></script>
     11 <script>
     12 
     13 goog.require('goog.testing.jsunit');
     14 
     15 </script>
     16 </head>
     17 <body>
     18 <script>
     19 
     20 var tree = {
     21   id: 0,
     22   children: [
     23     {
     24       id: 1,
     25       children: [
     26         {id: 2},
     27         {id: 3, children: []}
     28       ]
     29     },
     30     {id: 4},
     31     {id: 5}
     32   ]
     33 };
     34 
     35 // Mock chrome.experimental.bookmarkManager.getSubtree
     36 chrome = chrome || {};
     37 chrome.experimental = chrome.experimental || {};
     38 chrome.experimental.bookmarkManager = chrome.experimental.bookmarkManager || {};
     39 
     40 var callbacks = {};
     41 
     42 chrome.experimental.bookmarkManager.getSubtree = function(id, foldersOnly,
     43                                                           callback) {
     44   callbacks[id] = callbacks[id] || [];
     45   callbacks[id].push(callback);
     46   callbacks[id].$calls = callbacks[id].$calls ? callbacks[id].$calls++ : 1;
     47 };
     48 
     49 chrome.experimental.bookmarkManager.getSubtree.load = function(node) {
     50   // getSubtree gets the root tree when id is ''.
     51   var id = node.id;
     52   if (id == tree.id)
     53     id = '';
     54   for (var i = 0; i < callbacks[id].length; i++) {
     55     callbacks[id][i].call(null, [node]);
     56   }
     57 };
     58 
     59 function setUp() {
     60   callbacks = {}
     61 }
     62 
     63 function testLoadSingle() {
     64   var calls = 0;
     65   function f(node) {
     66     calls++;
     67     assertEquals(tree, node);
     68   }
     69   var p = bmm.loadTree();
     70   p.addListener(f);
     71 
     72   chrome.experimental.bookmarkManager.getSubtree.load(tree);
     73 
     74   assertEquals(1, calls);
     75   assertEquals(1, callbacks[''].$calls);
     76 }
     77 
     78 function testLoadMultiple() {
     79   var calls1 = 0;
     80   var calls2 = 0;
     81   function f1(node) {
     82     calls1++;
     83     assertEquals(tree, node);
     84   }
     85   function f2(node) {
     86     calls2++;
     87     assertEquals(tree, node);
     88   }
     89 
     90   var p = bmm.loadTree();
     91   var p2 = bmm.loadTree();
     92   p.addListener(f1);
     93   p2.addListener(f2);
     94 
     95   chrome.experimental.bookmarkManager.getSubtree.load(tree);
     96 
     97   assertEquals(1, calls1);
     98   assertEquals(1, calls2);
     99   assertEquals(1, callbacks[''].$calls);
    100 }
    101 
    102 function testLoadSubtree() {
    103   var calls = 0;
    104   function f(node) {
    105     calls++;
    106     assertEquals(tree.children[0], node);
    107   }
    108   var p = bmm.loadSubtree(1);
    109   p.addListener(f);
    110 
    111   chrome.experimental.bookmarkManager.getSubtree.load(tree.children[0]);
    112 
    113   assertEquals(1, calls);
    114   assertEquals(1, callbacks[1].$calls);
    115 }
    116 
    117 </script>
    118 </body>
    119 </html>
    120