| 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" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 NavigationManagerTest() : manager_(new NavigationManagerImpl()) { | 34 NavigationManagerTest() : manager_(new NavigationManagerImpl()) { |
| 35 manager_->SetDelegate(&delegate_); | 35 manager_->SetDelegate(&delegate_); |
| 36 manager_->SetBrowserState(&browser_state_); | 36 manager_->SetBrowserState(&browser_state_); |
| 37 controller_.reset( | 37 controller_.reset( |
| 38 [[CRWSessionController alloc] initWithBrowserState:&browser_state_]); | 38 [[CRWSessionController alloc] initWithBrowserState:&browser_state_]); |
| 39 manager_->SetSessionController(controller_.get()); | 39 manager_->SetSessionController(controller_.get()); |
| 40 } | 40 } |
| 41 CRWSessionController* session_controller() { return controller_.get(); } | 41 CRWSessionController* session_controller() { return controller_.get(); } |
| 42 NavigationManagerImpl* navigation_manager() { return manager_.get(); } | 42 NavigationManagerImpl* navigation_manager() { return manager_.get(); } |
| 43 | 43 |
| 44 // Adds a pending item by calling: |
| 45 // AddPendingItem(url, Referrer(), ui::PAGE_TRANSITION_IS_REDIRECT_MASK, |
| 46 // web::NavigationInitiationType::USER_INITIATED); |
| 47 // NOTE: use with discretion only when other fields don't matter. |
| 48 void AddRedirectPendingItem(GURL url) { |
| 49 manager_->AddPendingItem(url, Referrer(), |
| 50 ui::PAGE_TRANSITION_IS_REDIRECT_MASK, |
| 51 web::NavigationInitiationType::USER_INITIATED); |
| 52 } |
| 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) { |
| 53 EXPECT_EQ(0, navigation_manager()->GetItemCount()); | 63 EXPECT_EQ(0, navigation_manager()->GetItemCount()); |
| (...skipping 537 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 will not crash when |
| 602 // visible item unchanged. | 612 // there is no item. |
| 603 TEST_F(NavigationManagerTest, ReloadWithNormalReloadType) { | 613 TEST_F(NavigationManagerTest, ReloadEmptyWithNormal) { |
| 614 ASSERT_EQ(0, navigation_manager()->GetItemCount()); |
| 615 navigation_manager()->Reload(web::ReloadType::NORMAL, |
| 616 false /* check_for_repost */); |
| 617 } |
| 618 |
| 619 // Tests that calling |Reload| with web::ReloadType::NORMAL leaves the url of |
| 620 // the pending item unchanged when there is one. |
| 621 TEST_F(NavigationManagerTest, ReloadPendingItemWithNormal) { |
| 604 navigation_manager()->AddPendingItem( | 622 navigation_manager()->AddPendingItem( |
| 605 GURL("http://www.url.com"), Referrer(), ui::PAGE_TRANSITION_TYPED, | 623 GURL("http://www.url.com"), Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 606 web::NavigationInitiationType::USER_INITIATED); | 624 web::NavigationInitiationType::USER_INITIATED); |
| 607 ASSERT_TRUE(navigation_manager()->GetVisibleItem()); | 625 ASSERT_TRUE(navigation_manager()->GetPendingItem()); |
| 608 | 626 |
| 609 GURL url_before_reload = navigation_manager()->GetVisibleItem()->GetURL(); | 627 GURL url_before_reload = navigation_manager()->GetPendingItem()->GetURL(); |
| 610 navigation_manager()->Reload(web::ReloadType::NORMAL, | 628 navigation_manager()->Reload(web::ReloadType::NORMAL, |
| 611 false /* check_for_repost */); | 629 false /* check_for_repost */); |
| 612 | 630 |
| 631 ASSERT_TRUE(navigation_manager()->GetPendingItem()); |
| 613 EXPECT_EQ(url_before_reload, | 632 EXPECT_EQ(url_before_reload, |
| 614 navigation_manager()->GetVisibleItem()->GetURL()); | 633 navigation_manager()->GetPendingItem()->GetURL()); |
| 634 } |
| 635 |
| 636 // Tests that calling |Reload| with web::ReloadType::NORMAL leaves the url of |
| 637 // the last committed item unchanged when there is no pending item. |
| 638 TEST_F(NavigationManagerTest, ReloadLastCommittedItemWithNormal) { |
| 639 navigation_manager()->AddPendingItem( |
| 640 GURL("http://www.url.com"), Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 641 web::NavigationInitiationType::USER_INITIATED); |
| 642 [session_controller() commitPendingItem]; |
| 643 ASSERT_TRUE(navigation_manager()->GetLastCommittedItem()); |
| 644 |
| 645 GURL url_before_reload = |
| 646 navigation_manager()->GetLastCommittedItem()->GetURL(); |
| 647 navigation_manager()->Reload(web::ReloadType::NORMAL, |
| 648 false /* check_for_repost */); |
| 649 |
| 650 ASSERT_TRUE(navigation_manager()->GetLastCommittedItem()); |
| 651 EXPECT_EQ(url_before_reload, |
| 652 navigation_manager()->GetLastCommittedItem()->GetURL()); |
| 653 } |
| 654 |
| 655 // Tests that calling |Reload| with web::ReloadType::ORIGINAL_REQUEST_URL |
| 656 // changes the current item's url to its original request url when there is a |
| 657 // non-redirect pending item. |
| 658 TEST_F(NavigationManagerTest, ReloadNonRedirectPendingItemWithOriginal) { |
| 659 navigation_manager()->AddPendingItem( |
| 660 GURL("http://www.url.com"), Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 661 web::NavigationInitiationType::USER_INITIATED); |
| 662 ASSERT_TRUE(navigation_manager()->GetPendingItem()); |
| 663 GURL expected_original_url = GURL("http://www.url.com/original"); |
| 664 navigation_manager()->GetPendingItem()->SetOriginalRequestURL( |
| 665 expected_original_url); |
| 666 |
| 667 navigation_manager()->Reload(web::ReloadType::ORIGINAL_REQUEST_URL, |
| 668 false /* check_for_repost */); |
| 669 |
| 670 ASSERT_TRUE(navigation_manager()->GetPendingItem()); |
| 671 EXPECT_EQ(expected_original_url, |
| 672 navigation_manager()->GetPendingItem()->GetURL()); |
| 673 } |
| 674 |
| 675 // Tests that calling |Reload| with web::ReloadType::ORIGINAL_REQUEST_URL skips |
| 676 // discards redirect pending items and sets the current item as the last |
| 677 // non-redirect item and changes its url to its original request url when there |
| 678 // is a redirect pending item. |
| 679 TEST_F(NavigationManagerTest, ReloadRedirectPendingItemWithOriginal) { |
| 680 navigation_manager()->AddPendingItem( |
| 681 GURL("http://www.url.com"), Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 682 web::NavigationInitiationType::USER_INITIATED); |
| 683 ASSERT_TRUE(navigation_manager()->GetPendingItem()); |
| 684 GURL expected_original_url = GURL("http://www.url.com/original"); |
| 685 navigation_manager()->GetPendingItem()->SetOriginalRequestURL( |
| 686 expected_original_url); |
| 687 [session_controller() commitPendingItem]; |
| 688 |
| 689 AddRedirectPendingItem(GURL("http://www.url.com/pending")); |
| 690 |
| 691 navigation_manager()->Reload(web::ReloadType::ORIGINAL_REQUEST_URL, |
| 692 false /* check_for_repost */); |
| 693 |
| 694 ASSERT_TRUE(navigation_manager()->GetLastCommittedItem()); |
| 695 EXPECT_FALSE(navigation_manager()->GetPendingItem()); |
| 696 EXPECT_EQ(1, navigation_manager()->GetItemCount()); |
| 697 EXPECT_EQ(expected_original_url, |
| 698 navigation_manager()->GetLastCommittedItem()->GetURL()); |
| 699 } |
| 700 |
| 701 // Tests that calling |Reload| with web::ReloadType::ORIGINAL_REQUEST_URL |
| 702 // skips redirect items and sets the current item as the last non-redirect item |
| 703 // and changes its url to its original request url when there are multiple |
| 704 // redirect items. |
| 705 TEST_F(NavigationManagerTest, ReloadMultiRedirectItemsWithOriginal) { |
| 706 navigation_manager()->AddPendingItem( |
| 707 GURL("http://www.url.com"), Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 708 web::NavigationInitiationType::USER_INITIATED); |
| 709 ASSERT_TRUE(navigation_manager()->GetPendingItem()); |
| 710 GURL expected_original_url = GURL("http://www.url.com/original"); |
| 711 navigation_manager()->GetPendingItem()->SetOriginalRequestURL( |
| 712 expected_original_url); |
| 713 [session_controller() commitPendingItem]; |
| 714 |
| 715 AddRedirectPendingItem(GURL("http://www.url.com/1")); |
| 716 [session_controller() commitPendingItem]; |
| 717 AddRedirectPendingItem(GURL("http://www.url.com/2")); |
| 718 [session_controller() commitPendingItem]; |
| 719 AddRedirectPendingItem(GURL("http://www.url.com/3")); |
| 720 |
| 721 navigation_manager()->Reload(web::ReloadType::ORIGINAL_REQUEST_URL, |
| 722 false /* check_for_repost */); |
| 723 |
| 724 ASSERT_TRUE(navigation_manager()->GetLastCommittedItem()); |
| 725 EXPECT_FALSE(navigation_manager()->GetPendingItem()); |
| 726 EXPECT_EQ(1, navigation_manager()->GetItemCount()); |
| 727 EXPECT_EQ(expected_original_url, |
| 728 navigation_manager()->GetLastCommittedItem()->GetURL()); |
| 729 } |
| 730 |
| 731 // Tests that calling |Reload| with web::ReloadType::ORIGINAL_REQUEST_URL |
| 732 // skips redirect items and sets the current item as the last (not earlies) |
| 733 // non-redirect item and changes its url to its original request url when there |
| 734 // are multiple non-redirect items. |
| 735 TEST_F(NavigationManagerTest, ReloadItemsWithOriginalNotEarlies) { |
| 736 navigation_manager()->AddPendingItem( |
| 737 GURL("http://www.url.com/1"), Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 738 web::NavigationInitiationType::USER_INITIATED); |
| 739 ASSERT_TRUE(navigation_manager()->GetPendingItem()); |
| 740 navigation_manager()->GetPendingItem()->SetOriginalRequestURL( |
| 741 GURL("http://www.url.com/original/1")); |
| 742 [session_controller() commitPendingItem]; |
| 743 |
| 744 navigation_manager()->AddPendingItem( |
| 745 GURL("http://www.url.com/2"), Referrer(), ui::PAGE_TRANSITION_TYPED, |
| 746 web::NavigationInitiationType::USER_INITIATED); |
| 747 ASSERT_TRUE(navigation_manager()->GetPendingItem()); |
| 748 GURL expected_original_url = GURL("http://www.url.com/original/2"); |
| 749 navigation_manager()->GetPendingItem()->SetOriginalRequestURL( |
| 750 expected_original_url); |
| 751 [session_controller() commitPendingItem]; |
| 752 |
| 753 AddRedirectPendingItem(GURL("http://www.url.com/3")); |
| 754 |
| 755 navigation_manager()->Reload(web::ReloadType::ORIGINAL_REQUEST_URL, |
| 756 false /* check_for_repost */); |
| 757 |
| 758 ASSERT_TRUE(navigation_manager()->GetLastCommittedItem()); |
| 759 EXPECT_FALSE(navigation_manager()->GetPendingItem()); |
| 760 EXPECT_EQ(2, navigation_manager()->GetItemCount()); |
| 761 EXPECT_EQ(expected_original_url, |
| 762 navigation_manager()->GetLastCommittedItem()->GetURL()); |
| 615 } | 763 } |
| 616 | 764 |
| 617 } // namespace web | 765 } // namespace web |
| OLD | NEW |