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