| 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 ], | 16 ], |
| 17 id:"0", title:"" | 17 id:"0", title:"" |
| 18 } | 18 } |
| 19 ]; | 19 ]; |
| 20 | 20 |
| 21 function bookmarksBar() { return expected[0].children[0]; } | 21 function bookmarksBar() { return expected[0].children[0]; } |
| 22 function otherBookmarks() { return expected[0].children[1]; } | 22 function otherBookmarks() { return expected[0].children[1]; } |
| 23 | 23 |
| 24 // Some variables that are used across multiple tests. | 24 // Some variables that are used across multiple tests. |
| 25 var node1 = {parentId:"1", title:"Foo bar baz", | 25 var node1 = {parentId:"1", title:"bar baz", |
| 26 url:"http://www.example.com/hello"}; | 26 url:"http://www.example.com/hello"}; |
| 27 var node2 = {parentId:"1", title:"foo quux", | 27 var node2 = {parentId:"1", title:"foo quux", |
| 28 url:"http://www.example.com/bar"}; | 28 url:"http://www.example.com/bar"}; |
| 29 var node3 = {parentId:"1", title:"bar baz", | 29 var node3 = {parentId:"1", title:"Foo bar baz", |
| 30 url:"http://www.google.com/hello/quux"}; | 30 url:"http://www.google.com/hello/quux"}; |
| 31 var quota_node1 = {parentId:"1", title:"Dave", | 31 var quota_node1 = {parentId:"1", title:"Dave", |
| 32 url:"http://www.dmband.com/"}; | 32 url:"http://www.dmband.com/"}; |
| 33 var quota_node2 = {parentId:"1", title:"UW", | 33 var quota_node2 = {parentId:"1", title:"UW", |
| 34 url:"http://www.uwaterloo.ca/"}; | 34 url:"http://www.uwaterloo.ca/"}; |
| 35 var quota_node3 = {parentId:"1", title:"Whistler", | 35 var quota_node3 = {parentId:"1", title:"Whistler", |
| 36 url:"http://www.whistlerblackcomb.com/"}; | 36 url:"http://www.whistlerblackcomb.com/"}; |
| 37 | 37 |
| 38 var pass = chrome.test.callbackPass; | 38 var pass = chrome.test.callbackPass; |
| 39 var fail = chrome.test.callbackFail; | 39 var fail = chrome.test.callbackFail; |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 chrome.bookmarks.search("www hello", pass(function(results) { | 294 chrome.bookmarks.search("www hello", pass(function(results) { |
| 295 // matches node1 & node3 | 295 // matches node1 & node3 |
| 296 chrome.test.assertEq(2, results.length); | 296 chrome.test.assertEq(2, results.length); |
| 297 })); | 297 })); |
| 298 chrome.bookmarks.search("bar example", | 298 chrome.bookmarks.search("bar example", |
| 299 pass(function(results) { | 299 pass(function(results) { |
| 300 // matches node2 | 300 // matches node2 |
| 301 chrome.test.assertEq(1, results.length); | 301 chrome.test.assertEq(1, results.length); |
| 302 })); | 302 })); |
| 303 chrome.bookmarks.search("foo bar", pass(function(results) { | 303 chrome.bookmarks.search("foo bar", pass(function(results) { |
| 304 // matches node1 & folder "foo bar" from createFolder | 304 // matches node3 & folder "foo bar" from createFolder |
| 305 chrome.test.assertEq(2, results.length); | 305 chrome.test.assertEq(2, results.length); |
| 306 })); | 306 })); |
| 307 chrome.bookmarks.search("quux", pass(function(results) { | 307 chrome.bookmarks.search("quux", pass(function(results) { |
| 308 // matches node2 & node3 | 308 // matches node2 & node1 |
| 309 chrome.test.assertEq(2, results.length); | 309 chrome.test.assertEq(2, results.length); |
| 310 })); | 310 })); |
| 311 chrome.bookmarks.search("Bookmark Bar", pass(function(results) { |
| 312 // Does not match any node since permanent nodes are stripped from search |
| 313 chrome.test.assertEq(0, results.length); |
| 314 })); |
| 311 }, | 315 }, |
| 312 | 316 |
| 313 function update() { | 317 function update() { |
| 314 var title = "hello world"; | 318 var title = "hello world"; |
| 315 chrome.test.listenOnce(chrome.bookmarks.onChanged, function(id, changes) { | 319 chrome.test.listenOnce(chrome.bookmarks.onChanged, function(id, changes) { |
| 316 chrome.test.assertEq(title, changes.title); | 320 chrome.test.assertEq(title, changes.title); |
| 317 }); | 321 }); |
| 318 chrome.bookmarks.update(node1.id, {"title": title}, pass(function(results) { | 322 chrome.bookmarks.update(node1.id, {"title": title}, pass(function(results) { |
| 319 chrome.test.assertEq(title, results.title); | 323 chrome.test.assertEq(title, results.title); |
| 320 })); | 324 })); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 357 chrome.test.assertEq(removeInfo.parentId, parentId); | 361 chrome.test.assertEq(removeInfo.parentId, parentId); |
| 358 chrome.test.assertEq(removeInfo.index, node1.index); | 362 chrome.test.assertEq(removeInfo.index, node1.index); |
| 359 }); | 363 }); |
| 360 chrome.bookmarks.remove(node1.id, pass(function() { | 364 chrome.bookmarks.remove(node1.id, pass(function() { |
| 361 // Update expected to match. | 365 // Update expected to match. |
| 362 // We removed node1, which means that the index of the other two nodes | 366 // We removed node1, which means that the index of the other two nodes |
| 363 // changes as well. | 367 // changes as well. |
| 364 expected[0].children[1].children[1].children.shift(); | 368 expected[0].children[1].children[1].children.shift(); |
| 365 expected[0].children[1].children[1].children[0].index = 0; | 369 expected[0].children[1].children[1].children[0].index = 0; |
| 366 expected[0].children[1].children[1].children[1].index = 1; | 370 expected[0].children[1].children[1].children[1].index = 1; |
| 367 | |
| 368 verifyTreeIsExpected(pass()); | 371 verifyTreeIsExpected(pass()); |
| 369 })); | 372 })); |
| 370 }, | 373 }, |
| 371 | 374 |
| 375 function searchRemoved() { |
| 376 // Search for deleted node |
| 377 chrome.bookmarks.search("baz bar", pass(function(results) { |
| 378 // matches only node3 since node1 was removed |
| 379 chrome.test.assertEq(1, results.length); |
| 380 })); |
| 381 }, |
| 382 |
| 372 function removeTree() { | 383 function removeTree() { |
| 373 var parentId = node2.parentId; | 384 var parentId = node2.parentId; |
| 374 var folder = expected[0].children[1].children[1]; | 385 var folder = expected[0].children[1].children[1]; |
| 375 chrome.test.listenOnce(chrome.bookmarks.onRemoved, | 386 chrome.test.listenOnce(chrome.bookmarks.onRemoved, |
| 376 function(id, removeInfo) { | 387 function(id, removeInfo) { |
| 377 chrome.test.assertEq(id, folder.id); | 388 chrome.test.assertEq(id, folder.id); |
| 378 chrome.test.assertEq(removeInfo.parentId, folder.parentId); | 389 chrome.test.assertEq(removeInfo.parentId, folder.parentId); |
| 379 chrome.test.assertEq(removeInfo.index, folder.index); | 390 chrome.test.assertEq(removeInfo.index, folder.index); |
| 380 }); | 391 }); |
| 381 chrome.bookmarks.removeTree(parentId, pass(function(){ | 392 chrome.bookmarks.removeTree(parentId, pass(function(){ |
| 382 // Update expected to match. | 393 // Update expected to match. |
| 383 expected[0].children[1].children.pop(); | 394 expected[0].children[1].children.pop(); |
| 384 verifyTreeIsExpected(pass()); | 395 verifyTreeIsExpected(pass()); |
| 385 })); | 396 })); |
| 386 }, | 397 }, |
| 387 | 398 |
| 399 function searchRemovedTree() { |
| 400 // Search for deleted folder and enclosed node3 |
| 401 chrome.bookmarks.search("foo bar", pass(function(results) { |
| 402 // Does not match anything since folder was removed with node3 in it |
| 403 chrome.test.assertEq(0, results.length); |
| 404 })); |
| 405 }, |
| 406 |
| 388 function quotaLimitedCreate() { | 407 function quotaLimitedCreate() { |
| 389 var node = {parentId:"1", title:"quotacreate", url:"http://www.quota.com/"}; | 408 var node = {parentId:"1", title:"quotacreate", url:"http://www.quota.com/"}; |
| 390 for (i = 0; i < 100; i++) { | 409 for (i = 0; i < 100; i++) { |
| 391 chrome.bookmarks.create(node, pass(function(results) { | 410 chrome.bookmarks.create(node, pass(function(results) { |
| 392 expected[0].children[0].children.push(results); | 411 expected[0].children[0].children.push(results); |
| 393 })); | 412 })); |
| 394 } | 413 } |
| 395 chrome.bookmarks.create( | 414 chrome.bookmarks.create( |
| 396 node, | 415 node, |
| 397 fail("This request exceeds the MAX_WRITE_OPERATIONS_PER_HOUR quota.")); | 416 fail("This request exceeds the MAX_WRITE_OPERATIONS_PER_HOUR quota.")); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 444 }); | 463 }); |
| 445 | 464 |
| 446 function afterRemove() { | 465 function afterRemove() { |
| 447 // Once done add 3 nodes | 466 // Once done add 3 nodes |
| 448 chrome.bookmarks.getTree(pass(function(results) { | 467 chrome.bookmarks.getTree(pass(function(results) { |
| 449 chrome.test.assertEq(0, results[0].children[0].children.length); | 468 chrome.test.assertEq(0, results[0].children[0].children.length); |
| 450 chrome.test.assertEq(0, results[0].children[1].children.length); | 469 chrome.test.assertEq(0, results[0].children[1].children.length); |
| 451 expected = results; | 470 expected = results; |
| 452 | 471 |
| 453 // Reset the nodes | 472 // Reset the nodes |
| 454 node1 = {parentId:"1", title:"Foo bar baz", | 473 node1 = {parentId:"1", title:"bar baz", |
| 455 url:"http://www.example.com/hello"}; | 474 url:"http://www.example.com/hello"}; |
| 456 node2 = {parentId:"1", title:"foo quux", | 475 node2 = {parentId:"1", title:"foo quux", |
| 457 url:"http://www.example.com/bar"}; | 476 url:"http://www.example.com/bar"}; |
| 458 node3 = {parentId:"1", title:"bar baz", | 477 node3 = {parentId:"1", title:"Foo bar baz", |
| 459 url:"http://www.google.com/hello/quux"}; | 478 url:"http://www.google.com/hello/quux"}; |
| 460 createNodes(bookmarksBar(), [node1, node2, node3], pass(function() { | 479 createNodes(bookmarksBar(), [node1, node2, node3], pass(function() { |
| 461 verifyTreeIsExpected(pass()); | 480 verifyTreeIsExpected(pass()); |
| 462 })); | 481 })); |
| 463 })); | 482 })); |
| 464 } | 483 } |
| 465 }, | 484 }, |
| 466 | 485 |
| 467 function getRecent() { | 486 function getRecent() { |
| 468 var failed = false; | 487 var failed = false; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 483 "Should only get the last 2 bookmarks"); | 502 "Should only get the last 2 bookmarks"); |
| 484 | 503 |
| 485 chrome.test.assertTrue(compareNode(node3, results[0])); | 504 chrome.test.assertTrue(compareNode(node3, results[0])); |
| 486 chrome.test.assertTrue(compareNode(node2, results[1])); | 505 chrome.test.assertTrue(compareNode(node2, results[1])); |
| 487 })); | 506 })); |
| 488 } | 507 } |
| 489 ]); | 508 ]); |
| 490 } | 509 } |
| 491 | 510 |
| 492 run(); | 511 run(); |
| OLD | NEW |