Index: ios/chrome/browser/native_app_launcher/native_app_navigation_utils_unittest.mm |
diff --git a/ios/chrome/browser/native_app_launcher/native_app_navigation_utils_unittest.mm b/ios/chrome/browser/native_app_launcher/native_app_navigation_utils_unittest.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..70a187b64d04c54277fdf0f53258fd7f5f4dd09e |
--- /dev/null |
+++ b/ios/chrome/browser/native_app_launcher/native_app_navigation_utils_unittest.mm |
@@ -0,0 +1,100 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ios/chrome/browser/native_app_launcher/native_app_navigation_utils.h" |
+ |
+#import "ios/web/navigation/crw_session_controller.h" |
+#include "ios/web/navigation/navigation_manager_impl.h" |
Eugene But (OOO till 7-30)
2017/01/26 01:04:39
s/include/import
pkl (ping after 24h if needed)
2017/01/26 02:26:29
But navigation_manager_impl defines a C++ class, s
Eugene But (OOO till 7-30)
2017/01/26 03:10:02
Line 19 is Objective-C code.
pkl (ping after 24h if needed)
2017/01/26 19:44:24
Done.
|
+#include "ios/web/public/referrer.h" |
+#include "ios/web/public/test/web_test.h" |
+#import "ios/web/web_state/web_state_impl.h" |
+#include "ui/base/page_transition_types.h" |
+#include "url/gurl.h" |
+ |
+namespace { |
Eugene But (OOO till 7-30)
2017/01/26 01:04:39
Optional nit: There is no need to put tests into a
pkl (ping after 24h if needed)
2017/01/26 02:26:28
There are differing opinions on putting test code
|
+ |
+class NativeAppNavigationUtilsTest : public web::WebTest { |
+ protected: |
+ void SetUp() override { |
+ web::WebTest::SetUp(); |
+ // WebStateImpl object is needed here to have access to CRWSessionController |
+ // for setting up NavigationManager entries. |
+ std::unique_ptr<web::WebStateImpl> web_state( |
+ new web::WebStateImpl(GetBrowserState())); |
+ web_state->GetNavigationManagerImpl().InitializeSession(nil, nil, NO, 0); |
+ web_state->SetWebUsageEnabled(true); |
+ web_state_.reset(web_state.release()); |
+ } |
+ |
+ void TearDown() override { |
+ web_state_.reset(); |
Eugene But (OOO till 7-30)
2017/01/26 01:04:39
Is there a reason for destroying web_state before
pkl (ping after 24h if needed)
2017/01/26 02:26:29
1. WebTestWithWebState does that.
2. It feels righ
|
+ web::WebTest::TearDown(); |
+ } |
+ |
+ web::WebState* web_state() { return web_state_->GetWebState(); } |
Eugene But (OOO till 7-30)
2017/01/26 01:04:39
return web_state_.get();
pkl (ping after 24h if needed)
2017/01/26 02:26:28
web_state_.get() returns a WebStateImpl* which is
|
+ |
+ void AddPendingEntry(const std::string& URL, ui::PageTransition transition) { |
Eugene But (OOO till 7-30)
2017/01/26 01:04:39
s/URL/url_spec ?
Eugene But (OOO till 7-30)
2017/01/26 01:04:39
AddCommittedItem? (s/Pending/Committed, s/Entry/It
pkl (ping after 24h if needed)
2017/01/26 02:26:28
Done.
pkl (ping after 24h if needed)
2017/01/26 02:26:29
Yes, good point! Renamed to just AddItem.
|
+ CRWSessionController* sessionController = |
Eugene But (OOO till 7-30)
2017/01/26 01:04:39
s/sessionController/session_controller
pkl (ping after 24h if needed)
2017/01/26 02:26:28
Done.
|
+ web_state_->GetNavigationManagerImpl().GetSessionController(); |
+ [sessionController addPendingEntry:GURL(URL) |
+ referrer:web::Referrer() |
+ transition:transition |
+ rendererInitiated:NO]; |
+ [sessionController commitPendingEntry]; |
+ } |
+ |
+ private: |
+ std::unique_ptr<web::WebStateImpl> web_state_; |
+}; |
+ |
+// Tests that default state is not a link click. |
+TEST_F(NativeAppNavigationUtilsTest, TestEmpty) { |
+ EXPECT_FALSE(native_app_launcher::IsLinkNavigation(web_state())); |
+} |
+ |
+// URL typed by user is not a link click. |
+TEST_F(NativeAppNavigationUtilsTest, TestTypedURL) { |
Eugene But (OOO till 7-30)
2017/01/26 01:04:39
Optional nit: According to C++ Style Guide this sh
pkl (ping after 24h if needed)
2017/01/26 02:26:29
Done.
|
+ AddPendingEntry("http://foo.com/page0", ui::PAGE_TRANSITION_TYPED); |
+ EXPECT_FALSE(native_app_launcher::IsLinkNavigation(web_state())); |
+} |
+ |
+// Transition state shows that user navigated via a link click. |
+TEST_F(NativeAppNavigationUtilsTest, TestLinkClicked) { |
+ AddPendingEntry("http://foo.com/page0", ui::PAGE_TRANSITION_LINK); |
+ EXPECT_TRUE(native_app_launcher::IsLinkNavigation(web_state())); |
+} |
+ |
+// Transition state shows that user navigated through clicking on a bookmark |
+// is considered *not* a link click. |
+TEST_F(NativeAppNavigationUtilsTest, TestAutoBookmark) { |
+ AddPendingEntry("http://foo.com/page0", ui::PAGE_TRANSITION_AUTO_BOOKMARK); |
+ EXPECT_TRUE(native_app_launcher::IsLinkNavigation(web_state())); |
Eugene But (OOO till 7-30)
2017/01/26 01:04:39
Sorry, missed in early reviews, when we did not ha
pkl (ping after 24h if needed)
2017/01/26 02:26:28
Done.
|
+} |
+ |
+// When there are redirects along the way, redirects are skipped until the |
+// first non-redirect entry. |
+TEST_F(NativeAppNavigationUtilsTest, TestHasTypedURLWithRedirect) { |
+ AddPendingEntry("http://foo.com/page0", ui::PAGE_TRANSITION_TYPED); |
+ AddPendingEntry("http://foo.com/page1", ui::PAGE_TRANSITION_CLIENT_REDIRECT); |
+ EXPECT_FALSE(native_app_launcher::IsLinkNavigation(web_state())); |
+} |
+ |
+// When there are multiple redirects along the way, redirects are skipped until |
+// the first non-redirect entry. |
+TEST_F(NativeAppNavigationUtilsTest, TestHasLinkClickedWithRedirect) { |
+ AddPendingEntry("http://foo.com/page0", ui::PAGE_TRANSITION_LINK); |
+ AddPendingEntry("http://bar.com/page1", ui::PAGE_TRANSITION_SERVER_REDIRECT); |
+ AddPendingEntry("http://zap.com/page2", ui::PAGE_TRANSITION_SERVER_REDIRECT); |
+ EXPECT_TRUE(native_app_launcher::IsLinkNavigation(web_state())); |
+} |
+ |
+// The first non-redirect entry is tested. Earlier redirects do not matter. |
+TEST_F(NativeAppNavigationUtilsTest, TestTypedURLWithRedirectEarlier) { |
+ AddPendingEntry("http://foo.com/page0", ui::PAGE_TRANSITION_LINK); |
+ AddPendingEntry("http://bar.com/page1", ui::PAGE_TRANSITION_SERVER_REDIRECT); |
+ AddPendingEntry("http://blah.com/page2", ui::PAGE_TRANSITION_TYPED); |
+ EXPECT_FALSE(native_app_launcher::IsLinkNavigation(web_state())); |
+} |
+ |
+} // namespace |