summaryrefslogtreecommitdiffstats
path: root/chrome/browser/resources/bookmark_manager/js/bmm_test.html
blob: 2515b6dbfdf10cd0713d245458564b65154c719a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!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>