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 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 | 60 |
61 function compareTrees(left, right, verbose) { | 61 function compareTrees(left, right, verbose) { |
62 if (verbose) { | 62 if (verbose) { |
63 chrome.test.log(JSON.stringify(left) || "<null>"); | 63 chrome.test.log(JSON.stringify(left) || "<null>"); |
64 chrome.test.log(JSON.stringify(right) || "<null>"); | 64 chrome.test.log(JSON.stringify(right) || "<null>"); |
65 } | 65 } |
66 if (left == null && right == null) { | 66 if (left == null && right == null) { |
67 return true; | 67 return true; |
68 } | 68 } |
69 if (left == null || right == null) | 69 if (left == null || right == null) |
70 return left + " !+ " + right; | 70 return left + " != " + right; |
71 if (left.length != right.length) | 71 if (left.length != right.length) |
72 return "count mismatch: " + left.length + " != " + right.length; | 72 return "count mismatch: " + left.length + " != " + right.length; |
73 for (var i = 0; i < left.length; i++) { | 73 for (var i = 0; i < left.length; i++) { |
74 var result = compareNode(left[i], right[i]); | 74 var result = compareNode(left[i], right[i]); |
75 if (result !== true) { | 75 if (result !== true) { |
76 chrome.test.log(result); | 76 chrome.test.log(result); |
77 chrome.test.log(JSON.stringify(left, null, 2)); | 77 chrome.test.log(JSON.stringify(left, null, 2)); |
78 chrome.test.log(JSON.stringify(right, null, 2)); | 78 chrome.test.log(JSON.stringify(right, null, 2)); |
79 return result; | 79 return result; |
80 } | 80 } |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 | 170 |
171 function createNoParentId() { | 171 function createNoParentId() { |
172 var node = {title:"google", url:"http://www.google.com/"}; | 172 var node = {title:"google", url:"http://www.google.com/"}; |
173 chrome.test.listenOnce(chrome.bookmarks.onCreated, function(id, created) { | 173 chrome.test.listenOnce(chrome.bookmarks.onCreated, function(id, created) { |
174 node.id = created.id; | 174 node.id = created.id; |
175 node.index = 0; | 175 node.index = 0; |
176 chrome.test.assertEq(id, node.id); | 176 chrome.test.assertEq(id, node.id); |
177 // Make sure the parentId defaults to the Other Bookmarks folder. | 177 // Make sure the parentId defaults to the Other Bookmarks folder. |
178 chrome.test.assertEq(expected[0].children[1].id, created.parentId); | 178 chrome.test.assertEq(expected[0].children[1].id, created.parentId); |
179 chrome.test.assertTrue(compareNode(node, created)); | 179 chrome.test.assertTrue(compareNode(node, created)); |
| 180 expected[0].children[1].children.push(node); |
180 }); | 181 }); |
181 chrome.bookmarks.create(node, pass(function(results) { | 182 chrome.bookmarks.create(node, pass(function(results) { |
182 node.id = results.id; // since we couldn't know this going in | 183 node.id = results.id; // since we couldn't know this going in |
183 node.index = 0; | 184 node.index = 0; |
184 chrome.test.assertTrue(compareNode(node, results), | 185 chrome.test.assertTrue(compareNode(node, results), |
185 "created node != source"); | 186 "created node != source"); |
186 })); | 187 })); |
187 }, | 188 }, |
188 | 189 |
189 function createInRoot() { | 190 function createInRoot() { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 | 270 |
270 verifyTreeIsExpected(pass()); | 271 verifyTreeIsExpected(pass()); |
271 })); | 272 })); |
272 | 273 |
273 // Move folder (and its children) to be a child of Other Bookmarks. | 274 // Move folder (and its children) to be a child of Other Bookmarks. |
274 var other = expected[0].children[1]; | 275 var other = expected[0].children[1]; |
275 chrome.bookmarks.move(folder.id, {parentId:other.id}, | 276 chrome.bookmarks.move(folder.id, {parentId:other.id}, |
276 pass(function(results) { | 277 pass(function(results) { |
277 chrome.test.assertEq(results.parentId, other.id); | 278 chrome.test.assertEq(results.parentId, other.id); |
278 folder.parentId = results.parentId; | 279 folder.parentId = results.parentId; |
| 280 folder.index = results.index; |
279 | 281 |
280 var folder2 = expected[0].children[0].children.pop(); | 282 var folder2 = expected[0].children[0].children.pop(); |
281 folder2.parentId = other.parentId; | 283 chrome.test.assertEq(folder2.id, folder.id); |
282 folder2.index = 0; | |
283 expected[0].children[1].children.push(folder2); | 284 expected[0].children[1].children.push(folder2); |
284 | |
285 verifyTreeIsExpected(pass()); | 285 verifyTreeIsExpected(pass()); |
286 })); | 286 })); |
287 }, | 287 }, |
288 | 288 |
289 function search() { | 289 function search() { |
290 chrome.bookmarks.search("baz bar", pass(function(results) { | 290 chrome.bookmarks.search("baz bar", pass(function(results) { |
291 // matches node1 & node3 | 291 // matches node1 & node3 |
292 chrome.test.assertEq(2, results.length); | 292 chrome.test.assertEq(2, results.length); |
293 })); | 293 })); |
294 chrome.bookmarks.search("www hello", pass(function(results) { | 294 chrome.bookmarks.search("www hello", pass(function(results) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 chrome.test.listenOnce(chrome.bookmarks.onRemoved, | 354 chrome.test.listenOnce(chrome.bookmarks.onRemoved, |
355 function(id, removeInfo) { | 355 function(id, removeInfo) { |
356 chrome.test.assertEq(id, node1.id); | 356 chrome.test.assertEq(id, node1.id); |
357 chrome.test.assertEq(removeInfo.parentId, parentId); | 357 chrome.test.assertEq(removeInfo.parentId, parentId); |
358 chrome.test.assertEq(removeInfo.index, node1.index); | 358 chrome.test.assertEq(removeInfo.index, node1.index); |
359 }); | 359 }); |
360 chrome.bookmarks.remove(node1.id, pass(function() { | 360 chrome.bookmarks.remove(node1.id, pass(function() { |
361 // Update expected to match. | 361 // Update expected to match. |
362 // We removed node1, which means that the index of the other two nodes | 362 // We removed node1, which means that the index of the other two nodes |
363 // changes as well. | 363 // changes as well. |
364 expected[0].children[1].children[0].children.shift(); | 364 expected[0].children[1].children[1].children.shift(); |
365 expected[0].children[1].children[0].children[0].index = 0; | 365 expected[0].children[1].children[1].children[0].index = 0; |
366 expected[0].children[1].children[0].children[1].index = 1; | 366 expected[0].children[1].children[1].children[1].index = 1; |
367 | 367 |
368 verifyTreeIsExpected(pass()); | 368 verifyTreeIsExpected(pass()); |
369 })); | 369 })); |
370 }, | 370 }, |
371 | 371 |
372 function removeTree() { | 372 function removeTree() { |
373 var parentId = node2.parentId; | 373 var parentId = node2.parentId; |
374 var folder = expected[0].children[1].children[0]; | 374 var folder = expected[0].children[1].children[1]; |
375 chrome.test.listenOnce(chrome.bookmarks.onRemoved, | 375 chrome.test.listenOnce(chrome.bookmarks.onRemoved, |
376 function(id, removeInfo) { | 376 function(id, removeInfo) { |
377 chrome.test.assertEq(id, folder.id); | 377 chrome.test.assertEq(id, folder.id); |
378 chrome.test.assertEq(removeInfo.parentId, folder.parentId); | 378 chrome.test.assertEq(removeInfo.parentId, folder.parentId); |
379 chrome.test.assertEq(removeInfo.index, folder.index); | 379 chrome.test.assertEq(removeInfo.index, folder.index); |
380 }); | 380 }); |
381 chrome.bookmarks.removeTree(parentId, pass(function(){ | 381 chrome.bookmarks.removeTree(parentId, pass(function(){ |
382 // Update expected to match. | 382 // Update expected to match. |
383 expected[0].children[1].children.shift(); | 383 expected[0].children[1].children.pop(); |
384 | |
385 verifyTreeIsExpected(pass()); | 384 verifyTreeIsExpected(pass()); |
386 })); | 385 })); |
387 }, | 386 }, |
388 | 387 |
389 function quotaLimitedCreate() { | 388 function quotaLimitedCreate() { |
390 var node = {parentId:"1", title:"quotacreate", url:"http://www.quota.com/"}; | 389 var node = {parentId:"1", title:"quotacreate", url:"http://www.quota.com/"}; |
391 for (i = 0; i < 100; i++) { | 390 for (i = 0; i < 100; i++) { |
392 chrome.bookmarks.create(node, pass(function(results) { | 391 chrome.bookmarks.create(node, pass(function(results) { |
393 expected[0].children[0].children.push(results); | 392 expected[0].children[0].children.push(results); |
394 })); | 393 })); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
429 | 428 |
430 chrome.test.resetQuota(); | 429 chrome.test.resetQuota(); |
431 }, | 430 }, |
432 | 431 |
433 function getRecentSetup() { | 432 function getRecentSetup() { |
434 // Clean up tree | 433 // Clean up tree |
435 ["1", "2"].forEach(function(id) { | 434 ["1", "2"].forEach(function(id) { |
436 chrome.bookmarks.getChildren(id, pass(function(children) { | 435 chrome.bookmarks.getChildren(id, pass(function(children) { |
437 children.forEach(function(child, i) { | 436 children.forEach(function(child, i) { |
438 chrome.bookmarks.removeTree(child.id, pass(function() {})); | 437 chrome.bookmarks.removeTree(child.id, pass(function() {})); |
439 | |
440 // When we have removed the last child we can continue. | 438 // When we have removed the last child we can continue. |
441 if (i == children.length - 1) | 439 if (id == "2" && i == children.length - 1) |
442 afterRemove(); | 440 afterRemove(); |
443 }); | 441 }); |
444 })); | 442 })); |
445 }); | 443 }); |
446 | 444 |
447 function afterRemove() { | 445 function afterRemove() { |
448 // Once done add 3 nodes | 446 // Once done add 3 nodes |
449 chrome.bookmarks.getTree(pass(function(results) { | 447 chrome.bookmarks.getTree(pass(function(results) { |
450 chrome.test.assertEq(0, results[0].children[0].children.length); | 448 chrome.test.assertEq(0, results[0].children[0].children.length); |
451 chrome.test.assertEq(0, results[0].children[1].children.length); | 449 chrome.test.assertEq(0, results[0].children[1].children.length); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 "Should only get the last 2 bookmarks"); | 482 "Should only get the last 2 bookmarks"); |
485 | 483 |
486 chrome.test.assertTrue(compareNode(node3, results[0])); | 484 chrome.test.assertTrue(compareNode(node3, results[0])); |
487 chrome.test.assertTrue(compareNode(node2, results[1])); | 485 chrome.test.assertTrue(compareNode(node2, results[1])); |
488 })); | 486 })); |
489 } | 487 } |
490 ]); | 488 ]); |
491 } | 489 } |
492 | 490 |
493 run(); | 491 run(); |
OLD | NEW |