OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ios/chrome/browser/native_app_launcher/native_app_navigation_util.h" |
| 6 |
| 7 #import "ios/web/navigation/crw_session_controller.h" |
| 8 #import "ios/web/navigation/navigation_manager_impl.h" |
| 9 #include "ios/web/public/referrer.h" |
| 10 #include "ios/web/public/test/web_test.h" |
| 11 #import "ios/web/web_state/web_state_impl.h" |
| 12 #include "ui/base/page_transition_types.h" |
| 13 #include "url/gurl.h" |
| 14 |
| 15 namespace { |
| 16 |
| 17 class NativeAppNavigationUtilsTest : public web::WebTest { |
| 18 protected: |
| 19 void SetUp() override { |
| 20 web::WebTest::SetUp(); |
| 21 // WebStateImpl object is needed here to have access to CRWSessionController |
| 22 // for setting up NavigationManager entries. |
| 23 std::unique_ptr<web::WebStateImpl> web_state( |
| 24 new web::WebStateImpl(GetBrowserState())); |
| 25 web_state->GetNavigationManagerImpl().InitializeSession(nil, nil, NO, 0); |
| 26 web_state->SetWebUsageEnabled(true); |
| 27 web_state_.reset(web_state.release()); |
| 28 } |
| 29 |
| 30 void TearDown() override { |
| 31 web_state_.reset(); |
| 32 web::WebTest::TearDown(); |
| 33 } |
| 34 |
| 35 web::WebState* web_state() { return web_state_->GetWebState(); } |
| 36 |
| 37 void AddItem(const std::string& url_spec, ui::PageTransition transition) { |
| 38 CRWSessionController* session_controller = |
| 39 web_state_->GetNavigationManagerImpl().GetSessionController(); |
| 40 [session_controller addPendingEntry:GURL(url_spec) |
| 41 referrer:web::Referrer() |
| 42 transition:transition |
| 43 rendererInitiated:NO]; |
| 44 [session_controller commitPendingEntry]; |
| 45 } |
| 46 |
| 47 private: |
| 48 std::unique_ptr<web::WebStateImpl> web_state_; |
| 49 }; |
| 50 |
| 51 // Tests that default state is not a link click. |
| 52 TEST_F(NativeAppNavigationUtilsTest, TestEmpty) { |
| 53 EXPECT_FALSE(native_app_launcher::IsLinkNavigation(web_state())); |
| 54 } |
| 55 |
| 56 // URL typed by user is not a link click. |
| 57 TEST_F(NativeAppNavigationUtilsTest, TestTypedUrl) { |
| 58 AddItem("http://foo.com/page0", ui::PAGE_TRANSITION_TYPED); |
| 59 EXPECT_FALSE(native_app_launcher::IsLinkNavigation(web_state())); |
| 60 } |
| 61 |
| 62 // Transition state shows that user navigated via a link click. |
| 63 TEST_F(NativeAppNavigationUtilsTest, TestLinkClicked) { |
| 64 AddItem("http://foo.com/page0", ui::PAGE_TRANSITION_LINK); |
| 65 EXPECT_TRUE(native_app_launcher::IsLinkNavigation(web_state())); |
| 66 } |
| 67 |
| 68 // Transition state shows that user navigated through clicking on a bookmark |
| 69 // is considered *not* a link click. |
| 70 TEST_F(NativeAppNavigationUtilsTest, TestAutoBookmark) { |
| 71 AddItem("http://foo.com/page0", ui::PAGE_TRANSITION_AUTO_BOOKMARK); |
| 72 EXPECT_TRUE(native_app_launcher::IsLinkNavigation(web_state())); |
| 73 } |
| 74 |
| 75 // When there are redirects along the way, redirects are skipped until the |
| 76 // first non-redirect entry. |
| 77 TEST_F(NativeAppNavigationUtilsTest, TestHasTypedUrlWithRedirect) { |
| 78 AddItem("http://foo.com/page0", ui::PAGE_TRANSITION_TYPED); |
| 79 AddItem("http://foo.com/page1", ui::PAGE_TRANSITION_CLIENT_REDIRECT); |
| 80 EXPECT_FALSE(native_app_launcher::IsLinkNavigation(web_state())); |
| 81 } |
| 82 |
| 83 // When there are multiple redirects along the way, redirects are skipped until |
| 84 // the first non-redirect entry. |
| 85 TEST_F(NativeAppNavigationUtilsTest, TestHasLinkClickedWithRedirect) { |
| 86 AddItem("http://foo.com/page0", ui::PAGE_TRANSITION_LINK); |
| 87 AddItem("http://bar.com/page1", ui::PAGE_TRANSITION_SERVER_REDIRECT); |
| 88 AddItem("http://zap.com/page2", ui::PAGE_TRANSITION_SERVER_REDIRECT); |
| 89 EXPECT_TRUE(native_app_launcher::IsLinkNavigation(web_state())); |
| 90 } |
| 91 |
| 92 // The first non-redirect entry is tested. Earlier redirects do not matter. |
| 93 TEST_F(NativeAppNavigationUtilsTest, TestTypedUrlWithRedirectEarlier) { |
| 94 AddItem("http://foo.com/page0", ui::PAGE_TRANSITION_LINK); |
| 95 AddItem("http://bar.com/page1", ui::PAGE_TRANSITION_SERVER_REDIRECT); |
| 96 AddItem("http://blah.com/page2", ui::PAGE_TRANSITION_TYPED); |
| 97 EXPECT_FALSE(native_app_launcher::IsLinkNavigation(web_state())); |
| 98 } |
| 99 |
| 100 } // namespace |
OLD | NEW |