Html程序  |  120行  |  2.49 KB

<!DOCTYPE html>
<html>
<head>
<!-- TODO(arv): Check in Closue unit tests and make this run as part of the
     tests -->
<script src="http://closure-library.googlecode.com/svn/trunk/closure/goog/base.js"></script>
<script src="cr.js"></script>
<script src="cr/promise.js"></script>
<script src="bmm/treeiterator.js"></script>
<script src="bmm.js"></script>
<script>

goog.require('goog.testing.jsunit');

</script>
</head>
<body>
<script>

var tree = {
  id: 0,
  children: [
    {
      id: 1,
      children: [
        {id: 2},
        {id: 3, children: []}
      ]
    },
    {id: 4},
    {id: 5}
  ]
};

// Mock chrome.experimental.bookmarkManager.getSubtree
chrome = chrome || {};
chrome.experimental = chrome.experimental || {};
chrome.experimental.bookmarkManager = chrome.experimental.bookmarkManager || {};

var callbacks = {};

chrome.experimental.bookmarkManager.getSubtree = function(id, foldersOnly,
                                                          callback) {
  callbacks[id] = callbacks[id] || [];
  callbacks[id].push(callback);
  callbacks[id].$calls = callbacks[id].$calls ? callbacks[id].$calls++ : 1;
};

chrome.experimental.bookmarkManager.getSubtree.load = function(node) {
  // getSubtree gets the root tree when id is ''.
  var id = node.id;
  if (id == tree.id)
    id = '';
  for (var i = 0; i < callbacks[id].length; i++) {
    callbacks[id][i].call(null, [node]);
  }
};

function setUp() {
  callbacks = {}
}

function testLoadSingle() {
  var calls = 0;
  function f(node) {
    calls++;
    assertEquals(tree, node);
  }
  var p = bmm.loadTree();
  p.addListener(f);

  chrome.experimental.bookmarkManager.getSubtree.load(tree);

  assertEquals(1, calls);
  assertEquals(1, callbacks[''].$calls);
}

function testLoadMultiple() {
  var calls1 = 0;
  var calls2 = 0;
  function f1(node) {
    calls1++;
    assertEquals(tree, node);
  }
  function f2(node) {
    calls2++;
    assertEquals(tree, node);
  }

  var p = bmm.loadTree();
  var p2 = bmm.loadTree();
  p.addListener(f1);
  p2.addListener(f2);

  chrome.experimental.bookmarkManager.getSubtree.load(tree);

  assertEquals(1, calls1);
  assertEquals(1, calls2);
  assertEquals(1, callbacks[''].$calls);
}

function testLoadSubtree() {
  var calls = 0;
  function f(node) {
    calls++;
    assertEquals(tree.children[0], node);
  }
  var p = bmm.loadSubtree(1);
  p.addListener(f);

  chrome.experimental.bookmarkManager.getSubtree.load(tree.children[0]);

  assertEquals(1, calls);
  assertEquals(1, callbacks[1].$calls);
}

</script>
</body>
</html>