| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 #include "ios/chrome/browser/native_app_launcher/native_app_navigation_util.h" | 5 #include "ios/chrome/browser/native_app_launcher/native_app_navigation_util.h" |
| 6 | 6 |
| 7 #import "ios/web/navigation/crw_session_controller.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #import "ios/web/navigation/navigation_manager_impl.h" | 8 #import "ios/web/public/test/fakes/test_navigation_manager.h" |
| 9 #include "ios/web/public/referrer.h" | 9 #import "ios/web/public/test/fakes/test_web_state.h" |
| 10 #include "ios/web/public/test/web_test.h" | 10 #include "testing/platform_test.h" |
| 11 #import "ios/web/web_state/web_state_impl.h" | |
| 12 #include "ui/base/page_transition_types.h" | 11 #include "ui/base/page_transition_types.h" |
| 13 #include "url/gurl.h" | 12 #include "url/gurl.h" |
| 14 | 13 |
| 15 namespace { | 14 // Tests the implementation of IsLinkNavigation(). The function being tested |
| 16 | 15 // uses public NavigationManager interfaces and can be tested by using |
| 17 class NativeAppNavigationUtilsTest : public web::WebTest { | 16 // TestNavigationManager that implements the same interface. |
| 17 class NativeAppNavigationUtilsTest : public PlatformTest { |
| 18 protected: | 18 protected: |
| 19 void SetUp() override { | 19 void SetUp() override { |
| 20 web::WebTest::SetUp(); | 20 PlatformTest::SetUp(); |
| 21 // WebStateImpl object is needed here to have access to CRWSessionController | 21 std::unique_ptr<web::TestNavigationManager> test_navigation_manager = |
| 22 // for setting up NavigationManager entries. | 22 base::MakeUnique<web::TestNavigationManager>(); |
| 23 std::unique_ptr<web::WebStateImpl> web_state( | 23 test_navigation_manager_ = test_navigation_manager.get(); |
| 24 new web::WebStateImpl(GetBrowserState())); | 24 test_web_state_.SetNavigationManager(std::move(test_navigation_manager)); |
| 25 web_state->GetNavigationManagerImpl().InitializeSession(NO); | |
| 26 web_state->SetWebUsageEnabled(true); | |
| 27 web_state_.reset(web_state.release()); | |
| 28 } | 25 } |
| 29 | 26 |
| 30 void TearDown() override { | 27 web::WebState* web_state() { return &test_web_state_; } |
| 31 web_state_.reset(); | |
| 32 web::WebTest::TearDown(); | |
| 33 } | |
| 34 | 28 |
| 35 web::WebState* web_state() { return web_state_->GetWebState(); } | 29 // Adds a navigation item of |transition| type to current WebState. |
| 36 | |
| 37 void AddItem(const std::string& url_spec, ui::PageTransition transition) { | 30 void AddItem(const std::string& url_spec, ui::PageTransition transition) { |
| 38 CRWSessionController* session_controller = | 31 test_navigation_manager_->AddItem(GURL(url_spec), transition); |
| 39 web_state_->GetNavigationManagerImpl().GetSessionController(); | |
| 40 web_state_->GetNavigationManagerImpl().AddPendingItem( | |
| 41 GURL(url_spec), web::Referrer(), transition, | |
| 42 web::NavigationInitiationType::USER_INITIATED); | |
| 43 [session_controller commitPendingItem]; | |
| 44 } | 32 } |
| 45 | 33 |
| 46 private: | 34 private: |
| 47 std::unique_ptr<web::WebStateImpl> web_state_; | 35 web::TestNavigationManager* test_navigation_manager_; |
| 36 web::TestWebState test_web_state_; |
| 48 }; | 37 }; |
| 49 | 38 |
| 50 // Tests that default state is not a link click. | 39 // Tests that default state is not a link click. |
| 51 TEST_F(NativeAppNavigationUtilsTest, TestEmpty) { | 40 TEST_F(NativeAppNavigationUtilsTest, TestEmpty) { |
| 52 EXPECT_FALSE(native_app_launcher::IsLinkNavigation(web_state())); | 41 EXPECT_FALSE(native_app_launcher::IsLinkNavigation(web_state())); |
| 53 } | 42 } |
| 54 | 43 |
| 55 // URL typed by user is not a link click. | 44 // URL typed by user is not a link click. |
| 56 TEST_F(NativeAppNavigationUtilsTest, TestTypedUrl) { | 45 TEST_F(NativeAppNavigationUtilsTest, TestTypedUrl) { |
| 57 AddItem("http://foo.com/page0", ui::PAGE_TRANSITION_TYPED); | 46 AddItem("http://foo.com/page0", ui::PAGE_TRANSITION_TYPED); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 88 EXPECT_TRUE(native_app_launcher::IsLinkNavigation(web_state())); | 77 EXPECT_TRUE(native_app_launcher::IsLinkNavigation(web_state())); |
| 89 } | 78 } |
| 90 | 79 |
| 91 // The first non-redirect entry is tested. Earlier redirects do not matter. | 80 // The first non-redirect entry is tested. Earlier redirects do not matter. |
| 92 TEST_F(NativeAppNavigationUtilsTest, TestTypedUrlWithRedirectEarlier) { | 81 TEST_F(NativeAppNavigationUtilsTest, TestTypedUrlWithRedirectEarlier) { |
| 93 AddItem("http://foo.com/page0", ui::PAGE_TRANSITION_LINK); | 82 AddItem("http://foo.com/page0", ui::PAGE_TRANSITION_LINK); |
| 94 AddItem("http://bar.com/page1", ui::PAGE_TRANSITION_SERVER_REDIRECT); | 83 AddItem("http://bar.com/page1", ui::PAGE_TRANSITION_SERVER_REDIRECT); |
| 95 AddItem("http://blah.com/page2", ui::PAGE_TRANSITION_TYPED); | 84 AddItem("http://blah.com/page2", ui::PAGE_TRANSITION_TYPED); |
| 96 EXPECT_FALSE(native_app_launcher::IsLinkNavigation(web_state())); | 85 EXPECT_FALSE(native_app_launcher::IsLinkNavigation(web_state())); |
| 97 } | 86 } |
| 98 | |
| 99 } // namespace | |
| OLD | NEW |