| OLD | NEW |
| 1 // History api test for Chrome. | 1 // History api test for Chrome. |
| 2 // browser_tests.exe --gtest_filter=ExtensionApiTest.History | 2 // browser_tests.exe --gtest_filter=ExtensionApiTest.History |
| 3 | 3 |
| 4 var pass = chrome.test.callbackPass; | 4 var pass = chrome.test.callbackPass; |
| 5 var fail = chrome.test.callbackFail; | 5 var fail = chrome.test.callbackFail; |
| 6 var assertEq = chrome.test.assertEq; | 6 var assertEq = chrome.test.assertEq; |
| 7 | 7 |
| 8 var GOOGLE_URL = 'http://www.google.com/'; | 8 var GOOGLE_URL = 'http://www.google.com/'; |
| 9 var PICASA_URL = 'http://www.picasa.com/'; | 9 var PICASA_URL = 'http://www.picasa.com/'; |
| 10 var A_RELATIVE_URL = | 10 var A_RELATIVE_URL = |
| (...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 function() { }); | 480 function() { }); |
| 481 }; | 481 }; |
| 482 | 482 |
| 483 // deletRange entry point. | 483 // deletRange entry point. |
| 484 chrome.experimental.history.deleteAll(function() { | 484 chrome.experimental.history.deleteAll(function() { |
| 485 setItemVisitedListener(onAddedItem); | 485 setItemVisitedListener(onAddedItem); |
| 486 setItemRemovedListener(deleteRange2TestVerification); | 486 setItemRemovedListener(deleteRange2TestVerification); |
| 487 populateHistory([urls[itemsAdded]], function() { }); | 487 populateHistory([urls[itemsAdded]], function() { }); |
| 488 }); | 488 }); |
| 489 }, | 489 }, |
| 490 | |
| 491 // This test has the following format. From the main entry point we | |
| 492 // attach a listener and give that listener a state to callback on. After | |
| 493 // receipt of the first message we validate it is as excepted in | |
| 494 // 'firsPageVisit.' We update the callback to 'secondPageVisit' and modify | |
| 495 //the history. | |
| 496 function listenForAdd() { | |
| 497 function secondPageVisit(visited) { | |
| 498 // Update state machine. | |
| 499 itemVisitedCallback = null; | |
| 500 chrome.experimental.history.onVisited.removeListener(itemVisitedListener); | |
| 501 | |
| 502 // Validate the state. | |
| 503 countItemsInHistory(function(count) { | |
| 504 assertEq(2, count); | |
| 505 assertEq(PICASA_URL, visited.url); | |
| 506 | |
| 507 // The test has succeeded. | |
| 508 chrome.test.succeed(); | |
| 509 }); | |
| 510 }; | |
| 511 | |
| 512 function firstPageVisit(visited) { | |
| 513 // Update state machine. | |
| 514 itemVisitedCallback = secondPageVisit; | |
| 515 | |
| 516 // Validate the state. | |
| 517 countItemsInHistory(function(count) { | |
| 518 assertEq(1, count); | |
| 519 assertEq(GOOGLE_URL, visited.url); | |
| 520 | |
| 521 // Transistion to next state. | |
| 522 populateHistory([PICASA_URL], function() { | |
| 523 }); | |
| 524 }); | |
| 525 }; | |
| 526 | |
| 527 // Populate the history one item at a time. | |
| 528 itemVisitedCallback = firstPageVisit; | |
| 529 chrome.experimental.history.onVisited.addListener(itemVisitedListener); | |
| 530 | |
| 531 chrome.experimental.history.deleteAll(function() { | |
| 532 populateHistory([GOOGLE_URL], function() { | |
| 533 }); | |
| 534 }); | |
| 535 }, | |
| 536 | |
| 537 function listenForDelete() { | |
| 538 var urls = [GOOGLE_URL, PICASA_URL]; | |
| 539 var visited_urls = {}; | |
| 540 | |
| 541 // Callback for deletions. | |
| 542 function deleteRangeCallback(removed) { | |
| 543 // We will no longer need to listen to deletions. | |
| 544 itemRemovedCallback = null; | |
| 545 chrome.experimental.history.onVisitRemoved.removeListener( | |
| 546 itemRemovedListener); | |
| 547 | |
| 548 // The two added urls should have been deleted together. | |
| 549 assertEq(false, removed.allHistory); | |
| 550 assertEq(2, removed.urls.length); | |
| 551 | |
| 552 // History should be empty. | |
| 553 countItemsInHistory(function(count) { | |
| 554 assertEq(0, count); | |
| 555 | |
| 556 // The test has succeeded. | |
| 557 chrome.test.succeed(); | |
| 558 }); | |
| 559 }; | |
| 560 | |
| 561 // Callback for page visits. | |
| 562 function historyPopulatedCallback(visited) { | |
| 563 visited_urls[visited_urls.length] = visited.url; | |
| 564 | |
| 565 if (visited_urls.length < urls.length) { | |
| 566 return; | |
| 567 } | |
| 568 | |
| 569 // We will no longer need to listen to visits. | |
| 570 itemVisitedCallback = null; | |
| 571 chrome.experimental.history.onVisited.removeListener(itemVisitedListener); | |
| 572 countItemsInHistory(function(count) { | |
| 573 assertEq(2, count); | |
| 574 | |
| 575 var startDate = new Date(); | |
| 576 startDate.setDate(startDate.getDate() - 1); | |
| 577 var endDate = new Date(); | |
| 578 chrome.experimental.history.deleteRange( | |
| 579 { 'startTime': startDate.getTime(), 'endTime': endDate.getTime() }, | |
| 580 function() { }); | |
| 581 }); | |
| 582 }; | |
| 583 | |
| 584 // We must be called back after the initial | |
| 585 itemVisitedCallback = historyPopulatedCallback; | |
| 586 chrome.experimental.history.onVisited.addListener(itemVisitedListener); | |
| 587 | |
| 588 chrome.experimental.history.deleteAll(function() { | |
| 589 // Do not add the listener until after we have reset the state of history. | |
| 590 itemRemovedCallback = deleteRangeCallback; | |
| 591 chrome.experimental.history.onVisitRemoved.addListener( | |
| 592 itemRemovedListener); | |
| 593 | |
| 594 // This call triggers the onVisited event handler. | |
| 595 populateHistory(urls, function() { }); | |
| 596 }); | |
| 597 }, | |
| 598 ]); | 490 ]); |
| OLD | NEW |