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 #include "base/memory/ptr_util.h" | |
8 #import "ios/web/public/test/fakes/test_navigation_manager.h" | |
9 #import "ios/web/public/test/fakes/test_web_state.h" | |
10 #include "testing/platform_test.h" | |
11 #include "ui/base/page_transition_types.h" | |
12 #include "url/gurl.h" | |
13 | |
14 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
15 #error "This file requires ARC support." | |
16 #endif | |
17 | |
18 // Tests the implementation of IsLinkNavigation(). The function being tested | |
19 // uses public NavigationManager interfaces and can be tested by using | |
20 // TestNavigationManager that implements the same interface. | |
21 class NativeAppNavigationUtilsTest : public PlatformTest { | |
22 protected: | |
23 void SetUp() override { | |
24 PlatformTest::SetUp(); | |
25 std::unique_ptr<web::TestNavigationManager> test_navigation_manager = | |
26 base::MakeUnique<web::TestNavigationManager>(); | |
27 test_navigation_manager_ = test_navigation_manager.get(); | |
28 test_web_state_.SetNavigationManager(std::move(test_navigation_manager)); | |
29 } | |
30 | |
31 web::WebState* web_state() { return &test_web_state_; } | |
32 | |
33 // Adds a navigation item of |transition| type to current WebState. | |
34 void AddItem(const std::string& url_spec, ui::PageTransition transition) { | |
35 test_navigation_manager_->AddItem(GURL(url_spec), transition); | |
36 } | |
37 | |
38 private: | |
39 web::TestNavigationManager* test_navigation_manager_; | |
40 web::TestWebState test_web_state_; | |
41 }; | |
42 | |
43 // Tests that default state is not a link click. | |
44 TEST_F(NativeAppNavigationUtilsTest, TestEmpty) { | |
45 EXPECT_FALSE(native_app_launcher::IsLinkNavigation(web_state())); | |
46 } | |
47 | |
48 // URL typed by user is not a link click. | |
49 TEST_F(NativeAppNavigationUtilsTest, TestTypedUrl) { | |
50 AddItem("http://foo.com/page0", ui::PAGE_TRANSITION_TYPED); | |
51 EXPECT_FALSE(native_app_launcher::IsLinkNavigation(web_state())); | |
52 } | |
53 | |
54 // Transition state shows that user navigated via a link click. | |
55 TEST_F(NativeAppNavigationUtilsTest, TestLinkClicked) { | |
56 AddItem("http://foo.com/page0", ui::PAGE_TRANSITION_LINK); | |
57 EXPECT_TRUE(native_app_launcher::IsLinkNavigation(web_state())); | |
58 } | |
59 | |
60 // Transition state shows that user navigated through clicking on a bookmark | |
61 // is considered *not* a link click. | |
62 TEST_F(NativeAppNavigationUtilsTest, TestAutoBookmark) { | |
63 AddItem("http://foo.com/page0", ui::PAGE_TRANSITION_AUTO_BOOKMARK); | |
64 EXPECT_TRUE(native_app_launcher::IsLinkNavigation(web_state())); | |
65 } | |
66 | |
67 // When there are redirects along the way, redirects are skipped until the | |
68 // first non-redirect entry. | |
69 TEST_F(NativeAppNavigationUtilsTest, TestHasTypedUrlWithRedirect) { | |
70 AddItem("http://foo.com/page0", ui::PAGE_TRANSITION_TYPED); | |
71 AddItem("http://foo.com/page1", ui::PAGE_TRANSITION_CLIENT_REDIRECT); | |
72 EXPECT_FALSE(native_app_launcher::IsLinkNavigation(web_state())); | |
73 } | |
74 | |
75 // When there are multiple redirects along the way, redirects are skipped until | |
76 // the first non-redirect entry. | |
77 TEST_F(NativeAppNavigationUtilsTest, TestHasLinkClickedWithRedirect) { | |
78 AddItem("http://foo.com/page0", ui::PAGE_TRANSITION_LINK); | |
79 AddItem("http://bar.com/page1", ui::PAGE_TRANSITION_CLIENT_REDIRECT); | |
80 AddItem("http://zap.com/page2", ui::PAGE_TRANSITION_CLIENT_REDIRECT); | |
81 EXPECT_TRUE(native_app_launcher::IsLinkNavigation(web_state())); | |
82 } | |
83 | |
84 // The first non-redirect entry is tested. Earlier redirects do not matter. | |
85 TEST_F(NativeAppNavigationUtilsTest, TestTypedUrlWithRedirectEarlier) { | |
86 AddItem("http://foo.com/page0", ui::PAGE_TRANSITION_LINK); | |
87 AddItem("http://bar.com/page1", ui::PAGE_TRANSITION_CLIENT_REDIRECT); | |
88 AddItem("http://blah.com/page2", ui::PAGE_TRANSITION_TYPED); | |
89 EXPECT_FALSE(native_app_launcher::IsLinkNavigation(web_state())); | |
90 } | |
OLD | NEW |