OLD | NEW |
1 // bookmarks api test | 1 // bookmarks api test |
2 // browser_tests.exe --gtest_filter=ExtensionApiTest.Bookmarks | 2 // browser_tests.exe --gtest_filter=ExtensionApiTest.Bookmarks |
3 | 3 |
4 var expected = [ | 4 var expected = [ |
5 {"children": [ | 5 {"children": [ |
6 {children:[], id:"1", parentId:"0", index:0, title:"Bookmarks bar"}, | 6 {children:[], id:"1", parentId:"0", index:0, title:"Bookmarks bar"}, |
7 {children:[], id:"2", parentId:"0", index:1, title:"Other bookmarks"} | 7 {children:[], id:"2", parentId:"0", index:1, title:"Other bookmarks"} |
8 ], | 8 ], |
9 id:"0", title:"" | 9 id:"0", title:"" |
10 } | 10 } |
11 ]; | 11 ]; |
12 | 12 |
13 // Some variables that are used across multiple tests. | 13 // Some variables that are used across multiple tests. |
14 var node1 = {parentId:"1", title:"Foo bar baz", | 14 var node1 = {parentId:"1", title:"Foo bar baz", |
15 url:"http://www.example.com/hello"}; | 15 url:"http://www.example.com/hello"}; |
16 var node2 = {parentId:"1", title:"foo quux", | 16 var node2 = {parentId:"1", title:"foo quux", |
17 url:"http://www.example.com/bar"}; | 17 url:"http://www.example.com/bar"}; |
18 var node3 = {parentId:"1", title:"bar baz", | 18 var node3 = {parentId:"1", title:"bar baz", |
19 url:"http://www.google.com/hello/quux"}; | 19 url:"http://www.google.com/hello/quux"}; |
20 | 20 |
21 var testCallback = chrome.test.testCallback; | 21 var pass = chrome.test.callbackPass; |
| 22 var fail = chrome.test.callbackFail; |
22 | 23 |
23 function compareNode(left, right) { | 24 function compareNode(left, right) { |
24 //chrome.test.log(JSON.stringify(left)); | 25 //chrome.test.log("compareNode()"); |
25 //chrome.test.log(JSON.stringify(right)); | 26 //chrome.test.log(JSON.stringify(left, null, 2)); |
| 27 //chrome.test.log(JSON.stringify(right, null, 2)); |
26 // TODO(erikkay): do some comparison of dateAdded | 28 // TODO(erikkay): do some comparison of dateAdded |
27 if (left.id != right.id) | 29 if (left.id != right.id) |
28 return "id mismatch: " + left.id + " != " + right.id; | 30 return "id mismatch: " + left.id + " != " + right.id; |
29 if (left.title != right.title) { | 31 if (left.title != right.title) { |
30 // TODO(erikkay): This resource dependency still isn't working reliably. | 32 // TODO(erikkay): This resource dependency still isn't working reliably. |
31 // See bug 19866. | 33 // See bug 19866. |
32 // return "title mismatch: " + left.title + " != " + right.title; | 34 // return "title mismatch: " + left.title + " != " + right.title; |
33 console.log("title mismatch: " + left.title + " != " + right.title); | 35 chrome.test.log("title mismatch: " + left.title + " != " + right.title); |
34 } | 36 } |
35 if (left.url != right.url) | 37 if (left.url != right.url) |
36 return "url mismatch: " + left.url + " != " + right.url; | 38 return "url mismatch: " + left.url + " != " + right.url; |
37 if (left.index != right.index) | 39 if (left.index != right.index) |
38 return "index mismatch: " + left.index + " != " + right.index; | 40 return "index mismatch: " + left.index + " != " + right.index; |
39 return true; | 41 return true; |
40 } | 42 } |
41 | 43 |
42 function compareTrees(left, right) { | 44 function compareTrees(left, right) { |
43 //chrome.test.log(JSON.stringify(left) || "<null>"); | 45 //chrome.test.log(JSON.stringify(left) || "<null>"); |
44 //chrome.test.log(JSON.stringify(right) || "<null>"); | 46 //chrome.test.log(JSON.stringify(right) || "<null>"); |
45 if (left == null && right == null) { | 47 if (left == null && right == null) { |
46 console.log("both left and right are NULL"); | |
47 return true; | 48 return true; |
48 } | 49 } |
49 if (left == null || right == null) | 50 if (left == null || right == null) |
50 return left + " !+ " + right; | 51 return left + " !+ " + right; |
51 if (left.length != right.length) | 52 if (left.length != right.length) |
52 return "count mismatch: " + left.length + " != " + right.length; | 53 return "count mismatch: " + left.length + " != " + right.length; |
53 for (var i = 0; i < left.length; i++) { | 54 for (var i = 0; i < left.length; i++) { |
54 var result = compareNode(left[i], right[i]); | 55 var result = compareNode(left[i], right[i]); |
55 if (result !== true) { | 56 if (result !== true) { |
56 console.log(JSON.stringify(left)); | 57 chrome.test.log(result); |
57 console.log(JSON.stringify(right)); | 58 chrome.test.log(JSON.stringify(left, null, 2)); |
| 59 chrome.test.log(JSON.stringify(right, null, 2)); |
58 return result; | 60 return result; |
59 } | 61 } |
60 result = compareTrees(left[i].children, right[i].children); | 62 result = compareTrees(left[i].children, right[i].children); |
61 if (result !== true) | 63 if (result !== true) |
62 return result; | 64 return result; |
63 } | 65 } |
64 return true; | 66 return true; |
65 } | 67 } |
66 | 68 |
67 chrome.test.runTests([ | 69 chrome.test.runTests([ |
68 function getTree() { | 70 function getTree() { |
69 chrome.bookmarks.getTree(testCallback(true, function(results) { | 71 chrome.bookmarks.getTree(pass(function(results) { |
70 chrome.test.assertTrue(compareTrees(results, expected), | 72 chrome.test.assertTrue(compareTrees(results, expected), |
71 "getTree() result != expected"); | 73 "getTree() result != expected"); |
72 expected = results; | 74 expected = results; |
73 })); | 75 })); |
74 }, | 76 }, |
75 | 77 |
76 function get() { | 78 function get() { |
77 chrome.bookmarks.get("1", testCallback(true, function(results) { | 79 chrome.bookmarks.get("1", pass(function(results) { |
78 chrome.test.assertTrue(compareNode(results[0], expected[0].children[0])); | 80 chrome.test.assertTrue(compareNode(results[0], expected[0].children[0])); |
79 })); | 81 })); |
| 82 chrome.bookmarks.get("42", fail(function(){}, "Can't find bookmark for id.")
); |
80 }, | 83 }, |
81 | 84 |
82 function getArray() { | 85 function getArray() { |
83 chrome.bookmarks.get(["1", "2"], testCallback(true, function(results) { | 86 chrome.bookmarks.get(["1", "2"], pass(function(results) { |
84 chrome.test.assertTrue(compareNode(results[0], expected[0].children[0]), | 87 chrome.test.assertTrue(compareNode(results[0], expected[0].children[0]), |
85 "get() result != expected"); | 88 "get() result != expected"); |
86 chrome.test.assertTrue(compareNode(results[1], expected[0].children[1]), | 89 chrome.test.assertTrue(compareNode(results[1], expected[0].children[1]), |
87 "get() result != expected"); | 90 "get() result != expected"); |
88 })); | 91 })); |
89 }, | 92 }, |
90 | 93 |
91 function getChildren() { | 94 function getChildren() { |
92 chrome.bookmarks.getChildren("0", testCallback(true, function(results) { | 95 chrome.bookmarks.getChildren("0", pass(function(results) { |
93 chrome.test.assertTrue(compareNode(results[0], expected[0].children[0]), | 96 chrome.test.assertTrue(compareNode(results[0], expected[0].children[0]), |
94 "getChildren() result != expected"); | 97 "getChildren() result != expected"); |
95 chrome.test.assertTrue(compareNode(results[1], expected[0].children[1]), | 98 chrome.test.assertTrue(compareNode(results[1], expected[0].children[1]), |
96 "getChildren() result != expected"); | 99 "getChildren() result != expected"); |
97 })); | 100 })); |
98 }, | 101 }, |
99 | 102 |
100 function create() { | 103 function create() { |
101 var node = {parentId:"1", title:"google", url:"http://www.google.com/"}; | 104 var node = {parentId:"1", title:"google", url:"http://www.google.com/"}; |
102 chrome.bookmarks.create(node, testCallback(true, function(results) { | 105 chrome.test.listenOnce(chrome.bookmarks.onCreated, function(id, created) { |
| 106 node.id = created.id; |
| 107 node.index = 0; |
| 108 chrome.test.assertEq(id, node.id); |
| 109 chrome.test.assertTrue(compareNode(node, created)); |
| 110 }); |
| 111 chrome.bookmarks.create(node, pass(function(results) { |
103 node.id = results.id; // since we couldn't know this going in | 112 node.id = results.id; // since we couldn't know this going in |
104 node.index = 0; | 113 node.index = 0; |
105 chrome.test.assertTrue(compareNode(node, results), | 114 chrome.test.assertTrue(compareNode(node, results), |
106 "created node != source"); | 115 "created node != source"); |
107 expected[0].children[0].children.push(node); | 116 expected[0].children[0].children.push(node); |
108 })); | 117 })); |
109 }, | 118 }, |
110 | 119 |
111 function createFolder() { | 120 function createFolder() { |
112 var node = {parentId:"1", title:"foo bar"}; // folder | 121 var node = {parentId:"1", title:"foo bar"}; // folder |
113 chrome.bookmarks.create(node, testCallback(true, function(results) { | 122 chrome.test.listenOnce(chrome.bookmarks.onCreated, function(id, created) { |
| 123 node.id = created.id; |
| 124 node.index = 1; |
| 125 node.children = []; |
| 126 chrome.test.assertTrue(compareNode(node, created)); |
| 127 }); |
| 128 chrome.bookmarks.create(node, pass(function(results) { |
114 node.id = results.id; // since we couldn't know this going in | 129 node.id = results.id; // since we couldn't know this going in |
115 node.index = 1; | 130 node.index = 1; |
116 node.children = []; | 131 node.children = []; |
117 chrome.test.assertTrue(compareNode(node, results), | 132 chrome.test.assertTrue(compareNode(node, results), |
118 "created node != source"); | 133 "created node != source"); |
119 expected[0].children[0].children.push(node); | 134 expected[0].children[0].children.push(node); |
120 })); | 135 })); |
121 }, | 136 }, |
122 | 137 |
123 function move_setup() { | 138 function move_setup() { |
124 chrome.bookmarks.create(node1, testCallback(false, function(results) { | 139 var bookmarks_bar = expected[0].children[0]; |
| 140 chrome.bookmarks.create(node1, pass(function(results) { |
125 node1.id = results.id; | 141 node1.id = results.id; |
126 node1.index = results.index; | 142 node1.index = results.index; |
127 expected[0].children[0].children.push(node1); | 143 bookmarks_bar.children.push(node1); |
128 })); | 144 })); |
129 chrome.bookmarks.create(node2, testCallback(false, function(results) { | 145 chrome.bookmarks.create(node2, pass(function(results) { |
130 node2.id = results.id; | 146 node2.id = results.id; |
131 node2.index = results.index; | 147 node2.index = results.index; |
132 expected[0].children[0].children.push(node2); | 148 bookmarks_bar.children.push(node2); |
133 })); | 149 })); |
134 chrome.bookmarks.create(node3, testCallback(false, function(results) { | 150 chrome.bookmarks.create(node3, pass(function(results) { |
135 node3.id = results.id; | 151 node3.id = results.id; |
136 node3.index = results.index; | 152 node3.index = results.index; |
137 expected[0].children[0].children.push(node3); | 153 bookmarks_bar.children.push(node3); |
138 })); | 154 })); |
139 chrome.bookmarks.getTree(testCallback(true, function(results) { | 155 chrome.bookmarks.getTree(pass(function(results) { |
| 156 chrome.test.assertTrue(compareTrees(expected, results), |
| 157 "getTree() result != expected"); |
| 158 expected = results; |
| 159 })); |
| 160 }, |
| 161 |
| 162 function move() { |
| 163 // Move node1, node2, and node3 from their current location (the bookmark |
| 164 // bar) to be under the "foo bar" folder (created in createFolder()). |
| 165 // Then move that folder to be under the "other bookmarks" folder. |
| 166 var folder = expected[0].children[0].children[1]; |
| 167 var old_node1 = expected[0].children[0].children[2]; |
| 168 chrome.test.listenOnce(chrome.bookmarks.onMoved, function(id, moveInfo) { |
| 169 chrome.test.assertEq(node1.id, id); |
| 170 chrome.test.assertEq(moveInfo.parentId, folder.id); |
| 171 chrome.test.assertEq(moveInfo.index, 0); |
| 172 chrome.test.assertEq(moveInfo.oldParentId, old_node1.parentId); |
| 173 chrome.test.assertEq(moveInfo.oldIndex, old_node1.index); |
| 174 }); |
| 175 chrome.bookmarks.move(node1.id, {parentId:folder.id}, |
| 176 pass(function(results) { |
| 177 chrome.test.assertEq(results.parentId, folder.id); |
| 178 node1.parentId = results.parentId; |
| 179 node1.index = 0; |
| 180 })); |
| 181 chrome.bookmarks.move(node2.id, {parentId:folder.id}, |
| 182 pass(function(results) { |
| 183 chrome.test.assertEq(results.parentId, folder.id); |
| 184 node2.parentId = results.parentId; |
| 185 node2.index = 1; |
| 186 })); |
| 187 // Insert node3 at index 1 rather than at the end. This should result in |
| 188 // an order of node1, node3, node2. |
| 189 chrome.bookmarks.move(node3.id, {parentId:folder.id, index:1}, |
| 190 pass(function(results) { |
| 191 chrome.test.assertEq(results.parentId, folder.id); |
| 192 chrome.test.assertEq(results.index, 1); |
| 193 node3.parentId = results.parentId; |
| 194 node3.index = 1; |
| 195 node2.index = 2; |
| 196 })); |
| 197 |
| 198 chrome.bookmarks.getTree(pass(function(results) { |
| 199 expected[0].children[0].children.pop(); |
| 200 expected[0].children[0].children.pop(); |
| 201 expected[0].children[0].children.pop(); |
| 202 folder.children.push(node1); |
| 203 folder.children.push(node3); |
| 204 folder.children.push(node2); |
| 205 chrome.test.assertTrue(compareTrees(expected, results), |
| 206 "getTree() result != expected"); |
| 207 expected = results; |
| 208 })); |
| 209 |
| 210 // Move folder (and its children) to be a child of Other Bookmarks. |
| 211 var other = expected[0].children[1]; |
| 212 chrome.bookmarks.move(folder.id, {parentId:other.id}, |
| 213 pass(function(results) { |
| 214 chrome.test.assertEq(results.parentId, other.id); |
| 215 folder.parentId = results.parentId; |
| 216 })); |
| 217 |
| 218 chrome.bookmarks.getTree(pass(function(results) { |
| 219 var folder = expected[0].children[0].children.pop(); |
| 220 folder.parentId = other.parentId; |
| 221 folder.index = 0; |
| 222 expected[0].children[1].children.push(folder); |
| 223 chrome.test.assertTrue(compareTrees(expected, results), |
| 224 "getTree() result != expected"); |
| 225 expected = results; |
| 226 })); |
| 227 }, |
| 228 |
| 229 function search() { |
| 230 chrome.bookmarks.search("baz bar", pass(function(results) { |
| 231 // matches node1 & node3 |
| 232 chrome.test.assertEq(2, results.length); |
| 233 })); |
| 234 chrome.bookmarks.search("www hello", pass(function(results) { |
| 235 // matches node1 & node3 |
| 236 chrome.test.assertEq(2, results.length); |
| 237 })); |
| 238 chrome.bookmarks.search("bar example", |
| 239 pass(function(results) { |
| 240 // matches node2 |
| 241 chrome.test.assertEq(1, results.length); |
| 242 })); |
| 243 chrome.bookmarks.search("foo bar", pass(function(results) { |
| 244 // matches node1 |
| 245 chrome.test.assertEq(1, results.length); |
| 246 })); |
| 247 chrome.bookmarks.search("quux", pass(function(results) { |
| 248 // matches node2 & node3 |
| 249 chrome.test.assertEq(2, results.length); |
| 250 })); |
| 251 }, |
| 252 |
| 253 function update() { |
| 254 var title = "hello world"; |
| 255 chrome.test.listenOnce(chrome.bookmarks.onChanged, function(id, changes) { |
| 256 chrome.test.assertEq(title, changes.title); |
| 257 }); |
| 258 chrome.bookmarks.update(node1.id, {"title": title}, pass(function(results) { |
| 259 chrome.test.assertEq(title, results.title); |
| 260 })); |
| 261 }, |
| 262 |
| 263 function remove() { |
| 264 var parentId = node1.parentId; |
| 265 chrome.test.listenOnce(chrome.bookmarks.onRemoved, |
| 266 function(id, removeInfo) { |
| 267 chrome.test.assertEq(id, node1.id); |
| 268 chrome.test.assertEq(removeInfo.parentId, parentId); |
| 269 chrome.test.assertEq(removeInfo.index, node1.index); |
| 270 }); |
| 271 chrome.bookmarks.remove(node1.id, pass(function() {})); |
| 272 chrome.bookmarks.getTree(pass(function(results) { |
| 273 // We removed node1, which means that the index of the other two nodes |
| 274 // changes as well. |
| 275 expected[0].children[1].children[0].children.shift(); |
| 276 expected[0].children[1].children[0].children[0].index = 0; |
| 277 expected[0].children[1].children[0].children[1].index = 1; |
140 chrome.test.assertTrue(compareTrees(expected, results), | 278 chrome.test.assertTrue(compareTrees(expected, results), |
141 "getTree() result != expected"); | 279 "getTree() result != expected"); |
142 expected = results; | 280 expected = results; |
143 })); | 281 })); |
144 }, | 282 }, |
145 | 283 |
146 function move() { | 284 function removeTree() { |
147 // Move node1, node2, and node3 from their current location (the bookmark | 285 var parentId = node2.parentId; |
148 // bar) to be under the "other bookmarks" folder instead. | 286 var folder = expected[0].children[1].children[0]; |
149 var other = expected[0].children[1]; | 287 chrome.test.listenOnce(chrome.bookmarks.onRemoved, |
150 //var folder = expected[0].children[0].children[1]; | 288 function(id, removeInfo) { |
151 //console.log(JSON.stringify(node1)); | 289 chrome.test.assertEq(id, folder.id); |
152 chrome.bookmarks.move(node1.id, {parentId:other.id}, | 290 chrome.test.assertEq(removeInfo.parentId, folder.parentId); |
153 testCallback(false, function(results) { | 291 chrome.test.assertEq(removeInfo.index, folder.index); |
154 chrome.test.assertEq(results.parentId, other.id); | 292 }); |
155 node1.parentId = results.parentId; | 293 chrome.bookmarks.removeTree(parentId, pass(function(){})); |
156 })); | 294 chrome.bookmarks.getTree(pass(function(results) { |
157 chrome.bookmarks.move(node2.id, {parentId:other.id}, | 295 expected[0].children[1].children.shift(); |
158 testCallback(false, function(results) { | 296 chrome.test.assertTrue(compareTrees(expected, results), |
159 chrome.test.assertEq(results.parentId, other.id); | 297 "getTree() result != expected"); |
160 node3.parentId = results.parentId; | 298 expected = results; |
161 })); | |
162 // Insert node3 at index 1 rather than at the end. This should result in | |
163 // an order of node1, node3, node2. | |
164 chrome.bookmarks.move(node3.id, {parentId:other.id, index:1}, | |
165 testCallback(true, function(results) { | |
166 chrome.test.assertEq(results.parentId, other.id); | |
167 chrome.test.assertEq(results.index, 1); | |
168 node3.parentId = results.parentId; | |
169 node3.index = 1; | |
170 })); | 299 })); |
171 }, | 300 }, |
172 | |
173 function search() { | |
174 chrome.bookmarks.search("baz bar", testCallback(false, function(results) { | |
175 // matches node1 & node3 | |
176 console.log(JSON.stringify(results)); | |
177 chrome.test.assertEq(2, results.length); | |
178 })); | |
179 chrome.bookmarks.search("www hello", testCallback(false, function(results) { | |
180 // matches node1 & node3 | |
181 chrome.test.assertEq(2, results.length); | |
182 })); | |
183 chrome.bookmarks.search("bar example", | |
184 testCallback(false, function(results) { | |
185 // matches node2 | |
186 chrome.test.assertEq(1, results.length); | |
187 })); | |
188 chrome.bookmarks.search("foo bar", testCallback(false, function(results) { | |
189 // matches node1 | |
190 chrome.test.assertEq(1, results.length); | |
191 })); | |
192 chrome.bookmarks.search("quux", testCallback(true, function(results) { | |
193 // matches node2 & node3 | |
194 chrome.test.assertEq(2, results.length); | |
195 })); | |
196 }, | |
197 | |
198 function update() { | |
199 chrome.bookmarks.update(node1.id, {"title": "hello world"}, | |
200 testCallback(true, function(results) { | |
201 chrome.test.assertEq("hello world", results.title); | |
202 })); | |
203 }, | |
204 | |
205 function remove() { | |
206 chrome.test.succeed(); | |
207 }, | |
208 | |
209 function removeTree() { | |
210 chrome.test.succeed(); | |
211 }, | |
212 ]); | 301 ]); |
OLD | NEW |