Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #import "ios/web/navigation/navigation_manager_impl.h" | 5 #import "ios/web/navigation/navigation_manager_impl.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #import "base/mac/scoped_nsobject.h" | 8 #import "base/mac/scoped_nsobject.h" |
| 9 #import "ios/web/navigation/crw_session_controller+private_constructors.h" | 9 #import "ios/web/navigation/crw_session_controller+private_constructors.h" |
| 10 #import "ios/web/navigation/navigation_manager_delegate.h" | 10 #import "ios/web/navigation/navigation_manager_delegate.h" |
| 11 #include "ios/web/public/navigation_item.h" | 11 #include "ios/web/public/navigation_item.h" |
| 12 #include "ios/web/public/test/fakes/test_browser_state.h" | 12 #include "ios/web/public/test/fakes/test_browser_state.h" |
| 13 #include "ios/web/test/test_url_constants.h" | 13 #include "ios/web/test/test_url_constants.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "testing/platform_test.h" | 15 #include "testing/platform_test.h" |
| 16 | 16 |
| 17 namespace web { | 17 namespace web { |
| 18 namespace { | 18 namespace { |
| 19 // Stub class for NavigationManagerDelegate. | 19 // Stub class for NavigationManagerDelegate. |
| 20 class TestNavigationManagerDelegate : public NavigationManagerDelegate { | 20 class TestNavigationManagerDelegate : public NavigationManagerDelegate { |
| 21 public: | |
|
Eugene But (OOO till 7-30)
2017/03/28 01:52:56
How about this?
public:
bool reload_called() {
liaoyuke
2017/03/28 16:22:48
Done.
| |
| 21 void GoToIndex(int index) override {} | 22 void GoToIndex(int index) override {} |
| 22 void LoadURLWithParams(const NavigationManager::WebLoadParams&) override {} | 23 void LoadURLWithParams(const NavigationManager::WebLoadParams&) override {} |
| 23 void Reload() override {} | 24 void Reload() override { reload_called_ = true; } |
| 24 void OnNavigationItemsPruned(size_t pruned_item_count) override {} | 25 void OnNavigationItemsPruned(size_t pruned_item_count) override {} |
| 25 void OnNavigationItemChanged() override{}; | 26 void OnNavigationItemChanged() override{}; |
| 26 void OnNavigationItemCommitted(const LoadCommittedDetails&) override {} | 27 void OnNavigationItemCommitted(const LoadCommittedDetails&) override {} |
| 27 WebState* GetWebState() override { return nullptr; } | 28 WebState* GetWebState() override { return nullptr; } |
| 29 | |
| 30 bool reload_called() { return reload_called_; } | |
| 31 | |
| 32 private: | |
| 33 bool reload_called_ = false; | |
| 28 }; | 34 }; |
| 29 } // namespace | 35 } // namespace |
| 30 | 36 |
| 31 // Test fixture for NavigationManagerImpl testing. | 37 // Test fixture for NavigationManagerImpl testing. |
| 32 class NavigationManagerTest : public PlatformTest { | 38 class NavigationManagerTest : public PlatformTest { |
| 33 protected: | 39 protected: |
| 34 NavigationManagerTest() : manager_(new NavigationManagerImpl()) { | 40 NavigationManagerTest() : manager_(new NavigationManagerImpl()) { |
| 35 manager_->SetDelegate(&delegate_); | 41 manager_->SetDelegate(&delegate_); |
| 36 manager_->SetBrowserState(&browser_state_); | 42 manager_->SetBrowserState(&browser_state_); |
| 37 controller_.reset( | 43 controller_.reset( |
| 38 [[CRWSessionController alloc] initWithBrowserState:&browser_state_]); | 44 [[CRWSessionController alloc] initWithBrowserState:&browser_state_]); |
| 39 manager_->SetSessionController(controller_.get()); | 45 manager_->SetSessionController(controller_.get()); |
| 40 } | 46 } |
| 41 CRWSessionController* session_controller() { return controller_.get(); } | 47 CRWSessionController* session_controller() { return controller_.get(); } |
| 42 NavigationManagerImpl* navigation_manager() { return manager_.get(); } | 48 NavigationManagerImpl* navigation_manager() { return manager_.get(); } |
| 49 TestNavigationManagerDelegate navigation_manager_delegate() { | |
| 50 return delegate_; | |
| 51 } | |
| 43 | 52 |
| 44 private: | 53 private: |
| 45 TestBrowserState browser_state_; | 54 TestBrowserState browser_state_; |
| 46 TestNavigationManagerDelegate delegate_; | 55 TestNavigationManagerDelegate delegate_; |
| 47 std::unique_ptr<NavigationManagerImpl> manager_; | 56 std::unique_ptr<NavigationManagerImpl> manager_; |
| 48 base::scoped_nsobject<CRWSessionController> controller_; | 57 base::scoped_nsobject<CRWSessionController> controller_; |
| 49 }; | 58 }; |
| 50 | 59 |
| 51 // Tests state of an empty navigation manager. | 60 // Tests state of an empty navigation manager. |
| 52 TEST_F(NavigationManagerTest, EmptyManager) { | 61 TEST_F(NavigationManagerTest, EmptyManager) { |
| (...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 591 navigation_manager()->AddPendingItem( | 600 navigation_manager()->AddPendingItem( |
| 592 GURL("http://www.3.com"), Referrer(), ui::PAGE_TRANSITION_TYPED, | 601 GURL("http://www.3.com"), Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 593 web::NavigationInitiationType::USER_INITIATED); | 602 web::NavigationInitiationType::USER_INITIATED); |
| 594 [session_controller() commitPendingItem]; | 603 [session_controller() commitPendingItem]; |
| 595 web::NavigationItem* item3 = navigation_manager()->GetLastCommittedItem(); | 604 web::NavigationItem* item3 = navigation_manager()->GetLastCommittedItem(); |
| 596 | 605 |
| 597 // Verify that |item2|'s UserAgentType is propagated to |item3|. | 606 // Verify that |item2|'s UserAgentType is propagated to |item3|. |
| 598 EXPECT_EQ(item2->GetUserAgentType(), item3->GetUserAgentType()); | 607 EXPECT_EQ(item2->GetUserAgentType(), item3->GetUserAgentType()); |
| 599 } | 608 } |
| 600 | 609 |
| 601 // Tests that calling |Reload| on NavigationManager leaves the Url of the | 610 // Tests that calling |Reload| with web::ReloadType::NORMAL will not crash when |
|
Eugene But (OOO till 7-30)
2017/03/28 01:52:56
s/will not crash/is no op
Same comment for other
liaoyuke
2017/03/28 16:22:49
Done.
| |
| 602 // visible item unchanged. | 611 // there are no transient, pending and committed items. |
| 603 TEST_F(NavigationManagerTest, ReloadWithNormalReloadType) { | 612 TEST_F(NavigationManagerTest, ReloadEmptyWithNormalType) { |
| 613 ASSERT_FALSE(navigation_manager()->GetTransientItem()); | |
| 614 ASSERT_FALSE(navigation_manager()->GetPendingItem()); | |
| 615 ASSERT_EQ(0, navigation_manager()->GetItemCount()); | |
| 616 navigation_manager()->Reload(web::ReloadType::NORMAL, | |
| 617 false /* check_for_repost */); | |
| 618 EXPECT_TRUE(navigation_manager_delegate().reload_called()); | |
|
Eugene But (OOO till 7-30)
2017/03/28 01:52:56
Should this be EXPECT_FALSE?
liaoyuke
2017/03/28 16:22:48
Done.
| |
| 619 } | |
| 620 | |
| 621 // Tests that calling |Reload| with web::ReloadType::NORMAL leaves the url of | |
| 622 // the renderer initiated pending item unchanged when there is one. | |
| 623 TEST_F(NavigationManagerTest, ReloadRendererPendingItemWithNormalType) { | |
| 624 GURL url_before_reload = GURL("http://www.url.com"); | |
| 625 navigation_manager()->AddPendingItem( | |
| 626 url_before_reload, Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 627 web::NavigationInitiationType::RENDERER_INITIATED); | |
| 628 | |
| 629 navigation_manager()->Reload(web::ReloadType::NORMAL, | |
| 630 false /* check_for_repost */); | |
| 631 EXPECT_TRUE(navigation_manager_delegate().reload_called()); | |
| 632 | |
| 633 ASSERT_TRUE(navigation_manager()->GetPendingItem()); | |
| 634 EXPECT_EQ(url_before_reload, | |
| 635 navigation_manager()->GetPendingItem()->GetURL()); | |
| 636 } | |
| 637 | |
| 638 // Tests that calling |Reload| with web::ReloadType::NORMAL leaves the url of | |
| 639 // the user initiated pending item unchanged when there is one. | |
| 640 TEST_F(NavigationManagerTest, ReloadUserPendingItemWithNormalType) { | |
| 641 GURL url_before_reload = GURL("http://www.url.com"); | |
| 642 navigation_manager()->AddPendingItem( | |
| 643 url_before_reload, Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 644 web::NavigationInitiationType::USER_INITIATED); | |
| 645 | |
| 646 navigation_manager()->Reload(web::ReloadType::NORMAL, | |
| 647 false /* check_for_repost */); | |
| 648 EXPECT_TRUE(navigation_manager_delegate().reload_called()); | |
| 649 | |
| 650 ASSERT_TRUE(navigation_manager()->GetPendingItem()); | |
| 651 EXPECT_EQ(url_before_reload, | |
| 652 navigation_manager()->GetPendingItem()->GetURL()); | |
| 653 } | |
| 654 | |
| 655 // Tests that calling |Reload| with web::ReloadType::NORMAL leaves the url of | |
| 656 // the last committed item unchanged when there is no pending item. | |
| 657 TEST_F(NavigationManagerTest, ReloadLastCommittedItemWithNormalType) { | |
| 658 navigation_manager()->AddPendingItem( | |
| 659 GURL("http://www.url.com/0"), Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 660 web::NavigationInitiationType::USER_INITIATED); | |
| 661 [session_controller() commitPendingItem]; | |
| 662 | |
| 663 GURL url_before_reload = GURL("http://www.url.com/1"); | |
| 664 navigation_manager()->AddPendingItem( | |
| 665 url_before_reload, Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 666 web::NavigationInitiationType::USER_INITIATED); | |
| 667 [session_controller() commitPendingItem]; | |
| 668 | |
| 669 navigation_manager()->Reload(web::ReloadType::NORMAL, | |
| 670 false /* check_for_repost */); | |
| 671 EXPECT_TRUE(navigation_manager_delegate().reload_called()); | |
| 672 | |
| 673 ASSERT_TRUE(navigation_manager()->GetLastCommittedItem()); | |
| 674 EXPECT_EQ(url_before_reload, | |
| 675 navigation_manager()->GetLastCommittedItem()->GetURL()); | |
| 676 } | |
| 677 | |
| 678 // Tests that calling |Reload| with web::ReloadType::NORMAL leaves the url of | |
| 679 // the last committed item unchanged when there is no pending item, but there | |
| 680 // forward items after last committed item. | |
| 681 TEST_F(NavigationManagerTest, | |
| 682 ReloadLastCommittedItemWithNormalTypeWithForwardItems) { | |
| 683 navigation_manager()->AddPendingItem( | |
| 684 GURL("http://www.url.com/0"), Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 685 web::NavigationInitiationType::USER_INITIATED); | |
| 686 [session_controller() commitPendingItem]; | |
| 687 | |
| 688 GURL url_before_reload = GURL("http://www.url.com/1"); | |
| 689 navigation_manager()->AddPendingItem( | |
| 690 url_before_reload, Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 691 web::NavigationInitiationType::USER_INITIATED); | |
| 692 [session_controller() commitPendingItem]; | |
| 693 | |
| 694 navigation_manager()->AddPendingItem( | |
| 695 GURL("http://www.url.com/2"), Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 696 web::NavigationInitiationType::USER_INITIATED); | |
| 697 [session_controller() commitPendingItem]; | |
| 698 | |
| 699 [session_controller() goToItemAtIndex:1]; | |
| 700 EXPECT_EQ(1, navigation_manager()->GetLastCommittedItemIndex()); | |
| 701 | |
| 702 navigation_manager()->Reload(web::ReloadType::NORMAL, | |
| 703 false /* check_for_repost */); | |
| 704 EXPECT_TRUE(navigation_manager_delegate().reload_called()); | |
| 705 | |
| 706 ASSERT_TRUE(navigation_manager()->GetLastCommittedItem()); | |
| 707 EXPECT_EQ(url_before_reload, | |
| 708 navigation_manager()->GetLastCommittedItem()->GetURL()); | |
| 709 } | |
| 710 | |
| 711 // Tests that calling |Reload| with web::ReloadType::ORIGINAL_REQUEST_URL | |
| 712 // changes the renderer initiated pending item's url to its original request url | |
| 713 // when there is one. | |
| 714 TEST_F(NavigationManagerTest, ReloadRendererPendingItemWithOriginalType) { | |
| 604 navigation_manager()->AddPendingItem( | 715 navigation_manager()->AddPendingItem( |
| 605 GURL("http://www.url.com"), Referrer(), ui::PAGE_TRANSITION_TYPED, | 716 GURL("http://www.url.com"), Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 606 web::NavigationInitiationType::USER_INITIATED); | 717 web::NavigationInitiationType::RENDERER_INITIATED); |
| 607 ASSERT_TRUE(navigation_manager()->GetVisibleItem()); | 718 ASSERT_TRUE(navigation_manager()->GetPendingItem()); |
| 608 | 719 GURL expected_original_url = GURL("http://www.url.com/original"); |
| 609 GURL url_before_reload = navigation_manager()->GetVisibleItem()->GetURL(); | 720 navigation_manager()->GetPendingItem()->SetOriginalRequestURL( |
| 610 navigation_manager()->Reload(web::ReloadType::NORMAL, | 721 expected_original_url); |
| 611 false /* check_for_repost */); | 722 |
| 612 | 723 navigation_manager()->Reload(web::ReloadType::ORIGINAL_REQUEST_URL, |
| 613 EXPECT_EQ(url_before_reload, | 724 false /* check_for_repost */); |
| 614 navigation_manager()->GetVisibleItem()->GetURL()); | 725 EXPECT_TRUE(navigation_manager_delegate().reload_called()); |
| 726 | |
| 727 ASSERT_TRUE(navigation_manager()->GetPendingItem()); | |
| 728 EXPECT_EQ(expected_original_url, | |
| 729 navigation_manager()->GetPendingItem()->GetURL()); | |
| 730 } | |
| 731 | |
| 732 // Tests that calling |Reload| with web::ReloadType::ORIGINAL_REQUEST_URL | |
| 733 // changes the user initiated pending item's url to its original request url | |
| 734 // when there is one. | |
| 735 TEST_F(NavigationManagerTest, ReloadUserPendingItemWithOriginalType) { | |
| 736 navigation_manager()->AddPendingItem( | |
| 737 GURL("http://www.url.com"), Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 738 web::NavigationInitiationType::USER_INITIATED); | |
| 739 ASSERT_TRUE(navigation_manager()->GetPendingItem()); | |
| 740 GURL expected_original_url = GURL("http://www.url.com/original"); | |
| 741 navigation_manager()->GetPendingItem()->SetOriginalRequestURL( | |
| 742 expected_original_url); | |
| 743 | |
| 744 navigation_manager()->Reload(web::ReloadType::ORIGINAL_REQUEST_URL, | |
| 745 false /* check_for_repost */); | |
| 746 EXPECT_TRUE(navigation_manager_delegate().reload_called()); | |
| 747 | |
| 748 ASSERT_TRUE(navigation_manager()->GetPendingItem()); | |
| 749 EXPECT_EQ(expected_original_url, | |
| 750 navigation_manager()->GetPendingItem()->GetURL()); | |
| 751 } | |
| 752 | |
| 753 // Tests that calling |Reload| with web::ReloadType::ORIGINAL_REQUEST_URL | |
| 754 // changes the last committed item's url to its original request url when there | |
| 755 // is no pending item. | |
| 756 TEST_F(NavigationManagerTest, ReloadLastCommittedItemWithOriginalType) { | |
| 757 navigation_manager()->AddPendingItem( | |
| 758 GURL("http://www.url.com/0"), Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 759 web::NavigationInitiationType::USER_INITIATED); | |
| 760 [session_controller() commitPendingItem]; | |
| 761 | |
| 762 navigation_manager()->AddPendingItem( | |
| 763 GURL("http://www.url.com/1"), Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 764 web::NavigationInitiationType::USER_INITIATED); | |
| 765 GURL expected_original_url = GURL("http://www.url.com/1/original"); | |
| 766 ASSERT_TRUE(navigation_manager()->GetPendingItem()); | |
| 767 navigation_manager()->GetPendingItem()->SetOriginalRequestURL( | |
| 768 expected_original_url); | |
| 769 [session_controller() commitPendingItem]; | |
| 770 | |
| 771 navigation_manager()->Reload(web::ReloadType::ORIGINAL_REQUEST_URL, | |
| 772 false /* check_for_repost */); | |
| 773 EXPECT_TRUE(navigation_manager_delegate().reload_called()); | |
| 774 | |
| 775 ASSERT_TRUE(navigation_manager()->GetLastCommittedItem()); | |
| 776 EXPECT_EQ(expected_original_url, | |
| 777 navigation_manager()->GetLastCommittedItem()->GetURL()); | |
| 778 } | |
| 779 | |
| 780 // Tests that calling |Reload| with web::ReloadType::ORIGINAL_REQUEST_URL | |
| 781 // changes the last committed item's url to its original request url when there | |
| 782 // is no pending item, but there are forward items after last committed item. | |
| 783 TEST_F(NavigationManagerTest, | |
| 784 ReloadLastCommittedItemWithOriginalTypeWithForwardItems) { | |
| 785 navigation_manager()->AddPendingItem( | |
| 786 GURL("http://www.url.com/0"), Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 787 web::NavigationInitiationType::USER_INITIATED); | |
| 788 [session_controller() commitPendingItem]; | |
| 789 | |
| 790 navigation_manager()->AddPendingItem( | |
| 791 GURL("http://www.url.com/1"), Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 792 web::NavigationInitiationType::USER_INITIATED); | |
| 793 GURL expected_original_url = GURL("http://www.url.com/1/original"); | |
| 794 ASSERT_TRUE(navigation_manager()->GetPendingItem()); | |
| 795 navigation_manager()->GetPendingItem()->SetOriginalRequestURL( | |
| 796 expected_original_url); | |
| 797 [session_controller() commitPendingItem]; | |
| 798 | |
| 799 navigation_manager()->AddPendingItem( | |
| 800 GURL("http://www.url.com/2"), Referrer(), ui::PAGE_TRANSITION_TYPED, | |
| 801 web::NavigationInitiationType::USER_INITIATED); | |
| 802 [session_controller() commitPendingItem]; | |
| 803 | |
| 804 [session_controller() goToItemAtIndex:1]; | |
| 805 EXPECT_EQ(1, navigation_manager()->GetLastCommittedItemIndex()); | |
| 806 | |
| 807 navigation_manager()->Reload(web::ReloadType::ORIGINAL_REQUEST_URL, | |
| 808 false /* check_for_repost */); | |
| 809 EXPECT_TRUE(navigation_manager_delegate().reload_called()); | |
| 810 | |
| 811 ASSERT_TRUE(navigation_manager()->GetLastCommittedItem()); | |
| 812 EXPECT_EQ(expected_original_url, | |
| 813 navigation_manager()->GetLastCommittedItem()->GetURL()); | |
| 615 } | 814 } |
| 616 | 815 |
| 617 } // namespace web | 816 } // namespace web |
| OLD | NEW |