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
|
// bookmarks api test
// browser_tests.exe --gtest_filter=ExtensionApiTest.Bookmarks
var expected = [
{"children": [
{"children": [], "id": "1", "parentId": "0", "index": 0, "title":"Bookmarks bar"},
{"children": [], "id": "2", "parentId": "0", "index": 1, "title":"Other bookmarks"}
],
"id": "0", "title": ""
}
];
var testCallback = chrome.test.testCallback;
function compareNode(left, right) {
//console.log(JSON.stringify(left));
//console.log(JSON.stringify(right));
// TODO(erikkay): do some comparison of dateAdded
if (left.id != right.id)
return "id mismatch: " + left.id + " != " + right.id;
if (left.title != right.title)
return "title mismatch: " + left.title + " != " + right.title;
if (left.url != right.url)
return "url mismatch: " + left.url + " != " + right.url;
if (left.index != right.index)
return "index mismatch: " + left.index + " != " + right.index;
return true;
}
function compareTrees(left, right) {
//console.log(JSON.stringify(left));
//console.log(JSON.stringify(right));
if (left == null && right == null) {
console.log("both left and right are NULL");
return true;
}
if (left == null || right == null)
return left + " !+ " + right;
if (left.length != right.length)
return "count mismatch: " + left.length + " != " + right.length;
for (var i = 0; i < left.length; i++) {
var result = compareNode(left[i], right[i]);
if (result !== true) {
console.log(JSON.stringify(left));
console.log(JSON.stringify(right));
return result;
}
result = compareTrees(left[i].children, right[i].children);
if (result !== true)
return result;
}
return true;
}
chrome.test.runTests([
function getTree() {
chrome.bookmarks.getTree(testCallback(true, function(results) {
chrome.test.assertTrue(compareTrees(results, expected),
"getTree() result != expected");
expected = results;
}));
},
function get() {
chrome.bookmarks.get("1", testCallback(true, function(results) {
chrome.test.assertTrue(compareNode(results[0], expected[0].children[0]));
}));
},
function getArray() {
chrome.bookmarks.get(["1", "2"], testCallback(true, function(results) {
chrome.test.assertTrue(compareNode(results[0], expected[0].children[0]),
"get() result != expected");
chrome.test.assertTrue(compareNode(results[1], expected[0].children[1]),
"get() result != expected");
}));
},
function getChildren() {
chrome.bookmarks.getChildren("0", testCallback(true, function(results) {
chrome.test.assertTrue(compareNode(results[0], expected[0].children[0]),
"getChildren() result != expected");
chrome.test.assertTrue(compareNode(results[1], expected[0].children[1]),
"getChildren() result != expected");
}));
},
function create() {
var node = {parentId: "1", title:"google", url:"http://www.google.com/"};
chrome.bookmarks.create(node, testCallback(true, function(results) {
node.id = results.id; // since we couldn't know this going in
node.index = 0;
chrome.test.assertTrue(compareNode(node, results),
"created node != source");
}));
},
]);
|