OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // bookmarks api test | 5 // bookmarks api test |
6 // browser_tests.exe --gtest_filter=ExtensionApiTest.Bookmarks | 6 // browser_tests.exe --gtest_filter=ExtensionApiTest.Bookmarks |
7 | 7 |
8 // This is global state that is maintained across tests as a reference | 8 // This is global state that is maintained across tests as a reference |
9 // to compare against what's fetched from the browser (using compareTrees). | 9 // to compare against what's fetched from the browser (using compareTrees). |
10 // TODO(erikkay) It would be better if each test was self-contained and | 10 // TODO(erikkay) It would be better if each test was self-contained and |
11 // didn't depend on global state. | 11 // didn't depend on global state. |
12 var expected = [ | 12 var expected = [ |
13 {"children": [ | 13 {"children": [ |
14 {children:[], id:"1", parentId:"0", index:0, title:"Bookmarks bar"}, | 14 {children:[], id:"1", parentId:"0", index:0, title:"Bookmarks bar"}, |
15 {children:[], id:"2", parentId:"0", index:1, title:"Other bookmarks"} | 15 {children:[], id:"2", parentId:"0", index:1, title:"Other bookmarks"}, |
16 {id:"4", parentId:"0", index:3, title:"Managed bookmarks", children:[ | |
17 {id:"5", parentId:"4", index:0, title:"Managed Bookmark", | |
18 url: "http://www.chromium.org/"}, | |
19 {id:"6", parentId:"4", index:1, title:"Managed Folder", children:[]} | |
20 ] | |
21 } | |
16 ], | 22 ], |
17 id:"0", title:"" | 23 id:"0", title:"" |
18 } | 24 } |
19 ]; | 25 ]; |
20 | 26 |
21 function bookmarksBar() { return expected[0].children[0]; } | 27 function bookmarksBar() { return expected[0].children[0]; } |
22 function otherBookmarks() { return expected[0].children[1]; } | 28 function otherBookmarks() { return expected[0].children[1]; } |
23 | 29 |
24 // Some variables that are used across multiple tests. | 30 // Some variables that are used across multiple tests. |
25 var node1 = {parentId:"1", title:"bar baz", | 31 var node1 = {parentId:"1", title:"bar baz", |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 function run() { | 129 function run() { |
124 chrome.test.runTests([ | 130 chrome.test.runTests([ |
125 function getTree() { | 131 function getTree() { |
126 verifyTreeIsExpected(pass()); | 132 verifyTreeIsExpected(pass()); |
127 }, | 133 }, |
128 | 134 |
129 function get() { | 135 function get() { |
130 chrome.bookmarks.get("1", pass(function(results) { | 136 chrome.bookmarks.get("1", pass(function(results) { |
131 chrome.test.assertTrue(compareNode(results[0], expected[0].children[0])); | 137 chrome.test.assertTrue(compareNode(results[0], expected[0].children[0])); |
132 })); | 138 })); |
139 chrome.bookmarks.get("5", pass(function(results) { | |
140 chrome.test.assertTrue(compareNode( | |
141 results[0], expected[0].children[2].children[0])); | |
142 })); | |
133 chrome.bookmarks.get("42", fail("Can't find bookmark for id.")); | 143 chrome.bookmarks.get("42", fail("Can't find bookmark for id.")); |
134 }, | 144 }, |
135 | 145 |
136 function getArray() { | 146 function getArray() { |
137 chrome.bookmarks.get(["1", "2"], pass(function(results) { | 147 chrome.bookmarks.get(["1", "2"], pass(function(results) { |
138 chrome.test.assertTrue(compareNode(results[0], expected[0].children[0]), | 148 chrome.test.assertTrue(compareNode(results[0], expected[0].children[0]), |
139 "get() result != expected"); | 149 "get() result != expected"); |
140 chrome.test.assertTrue(compareNode(results[1], expected[0].children[1]), | 150 chrome.test.assertTrue(compareNode(results[1], expected[0].children[1]), |
141 "get() result != expected"); | 151 "get() result != expected"); |
142 })); | 152 })); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
186 "created node != source"); | 196 "created node != source"); |
187 })); | 197 })); |
188 }, | 198 }, |
189 | 199 |
190 function createInRoot() { | 200 function createInRoot() { |
191 const error = "Can't modify the root bookmark folders."; | 201 const error = "Can't modify the root bookmark folders."; |
192 var node = {parentId:"0", title:"g404", url:"http://www.google.com/404"}; | 202 var node = {parentId:"0", title:"g404", url:"http://www.google.com/404"}; |
193 chrome.bookmarks.create(node, fail(error)); | 203 chrome.bookmarks.create(node, fail(error)); |
194 }, | 204 }, |
195 | 205 |
206 function createInManaged() { | |
207 const error = "Can't modify managed bookmarks."; | |
208 var node = {parentId:"4", title:"g404", url:"http://www.google.com/404"}; | |
209 chrome.bookmarks.create(node, fail(error)); | |
210 }, | |
211 | |
196 function createFolder() { | 212 function createFolder() { |
197 var node = {parentId:"1", title:"foo bar"}; // folder | 213 var node = {parentId:"1", title:"foo bar"}; // folder |
198 chrome.test.listenOnce(chrome.bookmarks.onCreated, function(id, created) { | 214 chrome.test.listenOnce(chrome.bookmarks.onCreated, function(id, created) { |
199 node.id = created.id; | 215 node.id = created.id; |
200 node.index = 1; | 216 node.index = 1; |
201 node.children = []; | 217 node.children = []; |
202 chrome.test.assertTrue(compareNode(node, created)); | 218 chrome.test.assertTrue(compareNode(node, created)); |
203 }); | 219 }); |
204 chrome.bookmarks.create(node, pass(function(results) { | 220 chrome.bookmarks.create(node, pass(function(results) { |
205 node.id = results.id; // since we couldn't know this going in | 221 node.id = results.id; // since we couldn't know this going in |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
279 folder.parentId = results.parentId; | 295 folder.parentId = results.parentId; |
280 folder.index = results.index; | 296 folder.index = results.index; |
281 | 297 |
282 var folder2 = expected[0].children[0].children.pop(); | 298 var folder2 = expected[0].children[0].children.pop(); |
283 chrome.test.assertEq(folder2.id, folder.id); | 299 chrome.test.assertEq(folder2.id, folder.id); |
284 expected[0].children[1].children.push(folder2); | 300 expected[0].children[1].children.push(folder2); |
285 verifyTreeIsExpected(pass()); | 301 verifyTreeIsExpected(pass()); |
286 })); | 302 })); |
287 }, | 303 }, |
288 | 304 |
305 function moveToManaged() { | |
306 var managed_node = expected[0].children[2]; | |
307 chrome.test.assertEq("4", managed_node.id); | |
308 const error = "Can't modify managed bookmarks."; | |
309 chrome.bookmarks.move(node1.id, {parentId:managed_node.id}, fail(error)); | |
310 verifyTreeIsExpected(pass()); | |
311 }, | |
312 | |
313 function moveFromManaged() { | |
314 var managed_node = expected[0].children[2]; | |
315 var moving_node = managed_node.children[0]; | |
316 var other = expected[0].children[1]; | |
317 const error = "Can't modify managed bookmarks."; | |
318 chrome.bookmarks.move(moving_node.id, {parentId:other.id}, fail(error)); | |
319 verifyTreeIsExpected(pass()); | |
320 }, | |
321 | |
289 function search() { | 322 function search() { |
290 chrome.bookmarks.search("baz bar", pass(function(results) { | 323 chrome.bookmarks.search("baz bar", pass(function(results) { |
291 // matches node1 & node3 | 324 // matches node1 & node3 |
292 chrome.test.assertEq(2, results.length); | 325 chrome.test.assertEq(2, results.length); |
293 })); | 326 })); |
294 chrome.bookmarks.search("www hello", pass(function(results) { | 327 chrome.bookmarks.search("www hello", pass(function(results) { |
295 // matches node1 & node3 | 328 // matches node1 & node3 |
296 chrome.test.assertEq(2, results.length); | 329 chrome.test.assertEq(2, results.length); |
297 })); | 330 })); |
298 chrome.bookmarks.search("bar example", | 331 chrome.bookmarks.search("bar example", |
299 pass(function(results) { | 332 pass(function(results) { |
300 // matches node2 | 333 // matches node2 |
301 chrome.test.assertEq(1, results.length); | 334 chrome.test.assertEq(1, results.length); |
302 })); | 335 })); |
303 chrome.bookmarks.search("foo bar", pass(function(results) { | 336 chrome.bookmarks.search("foo bar", pass(function(results) { |
304 // matches node3 & folder "foo bar" from createFolder | 337 // matches node3 & folder "foo bar" from createFolder |
305 chrome.test.assertEq(2, results.length); | 338 chrome.test.assertEq(2, results.length); |
306 })); | 339 })); |
307 chrome.bookmarks.search("quux", pass(function(results) { | 340 chrome.bookmarks.search("quux", pass(function(results) { |
308 // matches node2 & node1 | 341 // matches node2 & node1 |
309 chrome.test.assertEq(2, results.length); | 342 chrome.test.assertEq(2, results.length); |
310 })); | 343 })); |
311 chrome.bookmarks.search("Bookmark Bar", pass(function(results) { | 344 chrome.bookmarks.search("Bookmark Bar", pass(function(results) { |
312 // Does not match any node since permanent nodes are stripped from search | 345 // Does not match any node since permanent nodes are stripped from search |
313 chrome.test.assertEq(0, results.length); | 346 chrome.test.assertEq(0, results.length); |
314 })); | 347 })); |
348 chrome.bookmarks.search("Managed", pass(function(results) { | |
349 // Matches the Managed Bookmark and the Managed Folder but not the | |
350 // managed_node. | |
351 chrome.test.assertEq(2, results.length); | |
352 })); | |
315 }, | 353 }, |
316 | 354 |
317 function update() { | 355 function update() { |
318 var title = "hello world"; | 356 var title = "hello world"; |
319 chrome.test.listenOnce(chrome.bookmarks.onChanged, function(id, changes) { | 357 chrome.test.listenOnce(chrome.bookmarks.onChanged, function(id, changes) { |
320 chrome.test.assertEq(title, changes.title); | 358 chrome.test.assertEq(title, changes.title); |
321 }); | 359 }); |
322 chrome.bookmarks.update(node1.id, {"title": title}, pass(function(results) { | 360 chrome.bookmarks.update(node1.id, {"title": title}, pass(function(results) { |
323 chrome.test.assertEq(title, results.title); | 361 chrome.test.assertEq(title, results.title); |
324 })); | 362 })); |
(...skipping 21 matching lines...) Expand all Loading... | |
346 function(results) { | 384 function(results) { |
347 chrome.bookmarks.get(node1.id, pass(function(results) { | 385 chrome.bookmarks.get(node1.id, pass(function(results) { |
348 chrome.test.assertEq(url, results[0].url); | 386 chrome.test.assertEq(url, results[0].url); |
349 chrome.test.log("URL UNCHANGED"); | 387 chrome.test.log("URL UNCHANGED"); |
350 })); | 388 })); |
351 }) | 389 }) |
352 ); | 390 ); |
353 })); | 391 })); |
354 }, | 392 }, |
355 | 393 |
394 function updateManaged() { | |
395 var managed_node = expected[0].children[2]; | |
396 var updating_node = managed_node.children[0]; | |
397 const error = "Can't modify managed bookmarks."; | |
398 chrome.bookmarks.update(updating_node.id, {"title": "New"}, fail(error)); | |
399 }, | |
400 | |
356 function remove() { | 401 function remove() { |
357 var parentId = node1.parentId; | 402 var parentId = node1.parentId; |
358 chrome.test.listenOnce(chrome.bookmarks.onRemoved, | 403 chrome.test.listenOnce(chrome.bookmarks.onRemoved, |
359 function(id, removeInfo) { | 404 function(id, removeInfo) { |
360 chrome.test.assertEq(id, node1.id); | 405 chrome.test.assertEq(id, node1.id); |
361 chrome.test.assertEq(removeInfo.parentId, parentId); | 406 chrome.test.assertEq(removeInfo.parentId, parentId); |
362 chrome.test.assertEq(removeInfo.index, node1.index); | 407 chrome.test.assertEq(removeInfo.index, node1.index); |
363 }); | 408 }); |
364 chrome.bookmarks.remove(node1.id, pass(function() { | 409 chrome.bookmarks.remove(node1.id, pass(function() { |
365 // Update expected to match. | 410 // Update expected to match. |
366 // We removed node1, which means that the index of the other two nodes | 411 // We removed node1, which means that the index of the other two nodes |
367 // changes as well. | 412 // changes as well. |
368 expected[0].children[1].children[1].children.shift(); | 413 expected[0].children[1].children[1].children.shift(); |
369 expected[0].children[1].children[1].children[0].index = 0; | 414 expected[0].children[1].children[1].children[0].index = 0; |
370 expected[0].children[1].children[1].children[1].index = 1; | 415 expected[0].children[1].children[1].children[1].index = 1; |
371 verifyTreeIsExpected(pass()); | 416 verifyTreeIsExpected(pass()); |
372 })); | 417 })); |
373 }, | 418 }, |
374 | 419 |
420 function removeManaged() { | |
421 var managed_node = expected[0].children[2]; | |
422 var removing_node = managed_node.children[0]; | |
423 const error = "Can't modify managed bookmarks."; | |
424 chrome.bookmarks.remove(removing_node.id, fail(error)); | |
425 }, | |
426 | |
375 function searchRemoved() { | 427 function searchRemoved() { |
376 // Search for deleted node | 428 // Search for deleted node |
377 chrome.bookmarks.search("baz bar", pass(function(results) { | 429 chrome.bookmarks.search("baz bar", pass(function(results) { |
378 // matches only node3 since node1 was removed | 430 // matches only node3 since node1 was removed |
379 chrome.test.assertEq(1, results.length); | 431 chrome.test.assertEq(1, results.length); |
380 })); | 432 })); |
381 }, | 433 }, |
382 | 434 |
383 function removeTree() { | 435 function removeTree() { |
not at google - send to devlin
2014/06/02 21:49:29
have you tested removeTree? it's the only function
Joao da Silva
2014/06/03 12:54:00
Added a new test for that.
| |
384 var parentId = node2.parentId; | 436 var parentId = node2.parentId; |
385 var folder = expected[0].children[1].children[1]; | 437 var folder = expected[0].children[1].children[1]; |
386 chrome.test.listenOnce(chrome.bookmarks.onRemoved, | 438 chrome.test.listenOnce(chrome.bookmarks.onRemoved, |
387 function(id, removeInfo) { | 439 function(id, removeInfo) { |
388 chrome.test.assertEq(id, folder.id); | 440 chrome.test.assertEq(id, folder.id); |
389 chrome.test.assertEq(removeInfo.parentId, folder.parentId); | 441 chrome.test.assertEq(removeInfo.parentId, folder.parentId); |
390 chrome.test.assertEq(removeInfo.index, folder.index); | 442 chrome.test.assertEq(removeInfo.index, folder.index); |
391 }); | 443 }); |
392 chrome.bookmarks.removeTree(parentId, pass(function(){ | 444 chrome.bookmarks.removeTree(parentId, pass(function(){ |
393 // Update expected to match. | 445 // Update expected to match. |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
486 function getRecent() { | 538 function getRecent() { |
487 var failed = false; | 539 var failed = false; |
488 try { | 540 try { |
489 chrome.bookmarks.getRecent(0, function() {}); | 541 chrome.bookmarks.getRecent(0, function() {}); |
490 } catch (ex) { | 542 } catch (ex) { |
491 failed = true; | 543 failed = true; |
492 } | 544 } |
493 chrome.test.assertTrue(failed, "Calling with 0 should fail"); | 545 chrome.test.assertTrue(failed, "Calling with 0 should fail"); |
494 | 546 |
495 chrome.bookmarks.getRecent(10000, pass(function(results) { | 547 chrome.bookmarks.getRecent(10000, pass(function(results) { |
496 chrome.test.assertEq(3, results.length, | 548 // Should include the "Managed Bookmark". |
549 chrome.test.assertEq(4, results.length, | |
497 "Should have gotten all recent bookmarks"); | 550 "Should have gotten all recent bookmarks"); |
498 })); | 551 })); |
499 | 552 |
500 chrome.bookmarks.getRecent(2, pass(function(results) { | 553 chrome.bookmarks.getRecent(2, pass(function(results) { |
501 chrome.test.assertEq(2, results.length, | 554 chrome.test.assertEq(2, results.length, |
502 "Should only get the last 2 bookmarks"); | 555 "Should only get the last 2 bookmarks"); |
503 | 556 |
504 chrome.test.assertTrue(compareNode(node3, results[0])); | 557 chrome.test.assertTrue(compareNode(node3, results[0])); |
505 chrome.test.assertTrue(compareNode(node2, results[1])); | 558 chrome.test.assertTrue(compareNode(node2, results[1])); |
506 })); | 559 })); |
(...skipping 13 matching lines...) Expand all Loading... | |
520 function(result) { | 573 function(result) { |
521 chrome.test.assertEq(newTitle, result.title); | 574 chrome.test.assertEq(newTitle, result.title); |
522 chrome.test.assertFalse('url' in result) | 575 chrome.test.assertFalse('url' in result) |
523 })); | 576 })); |
524 }); | 577 }); |
525 } | 578 } |
526 ]); | 579 ]); |
527 } | 580 } |
528 | 581 |
529 run(); | 582 run(); |
OLD | NEW |