Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "chrome/browser/safe_browsing/safe_browsing_navigation_observer.h" | |
| 6 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer_manager .h" | |
| 7 #include "chrome/browser/sessions/session_tab_helper.h" | |
| 8 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 9 #include "chrome/test/base/browser_with_test_window_test.h" | |
| 10 #include "content/public/test/test_renderer_host.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 #include "ui/base/window_open_disposition.h" | |
| 13 | |
| 14 namespace safe_browsing { | |
| 15 | |
| 16 class SBNavigationObserverTest : public BrowserWithTestWindowTest { | |
| 17 public: | |
| 18 SBNavigationObserverTest() {} | |
| 19 void SetUp() override { | |
| 20 BrowserWithTestWindowTest::SetUp(); | |
| 21 AddTab(browser(), GURL("http://foo/0")); | |
| 22 navigation_observer_manager_ = new SafeBrowsingNavigationObserverManager(); | |
| 23 navigation_observer_ = new SafeBrowsingNavigationObserver( | |
| 24 browser()->tab_strip_model()->GetWebContentsAt(0), | |
| 25 navigation_observer_manager_); | |
| 26 } | |
| 27 void TearDown() override { | |
| 28 delete navigation_observer_; | |
| 29 BrowserWithTestWindowTest::TearDown(); | |
| 30 } | |
| 31 void VerifyNavigationEvent(const GURL& expected_source_url, | |
| 32 int expected_source_tab, | |
| 33 const GURL& expected_original_request_url, | |
| 34 int expected_target_tab, | |
| 35 const GURL& expected_source_main_frame_url, | |
| 36 bool expected_is_user_initiated, | |
| 37 bool expected_has_committed, | |
| 38 bool expected_has_server_redirect, | |
| 39 const GURL& expected_destination_url, | |
| 40 const NavigationEvent& actual_nav_event) { | |
| 41 EXPECT_EQ(expected_source_url, actual_nav_event.source_url); | |
| 42 EXPECT_EQ(expected_source_tab, actual_nav_event.source_tab_id); | |
| 43 EXPECT_EQ(expected_original_request_url, | |
| 44 actual_nav_event.original_request_url); | |
| 45 EXPECT_EQ(expected_target_tab, actual_nav_event.target_tab_id); | |
| 46 EXPECT_EQ(expected_source_main_frame_url, | |
| 47 actual_nav_event.source_main_frame_url); | |
| 48 EXPECT_EQ(expected_is_user_initiated, actual_nav_event.is_user_initiated); | |
| 49 EXPECT_EQ(expected_has_committed, actual_nav_event.has_committed); | |
| 50 EXPECT_EQ(expected_has_server_redirect, | |
| 51 actual_nav_event.has_server_redirect); | |
| 52 EXPECT_EQ(expected_destination_url, actual_nav_event.destination_url); | |
| 53 } | |
| 54 | |
| 55 SafeBrowsingNavigationObserverManager::NavigationMap* navigation_map() { | |
| 56 return navigation_observer_manager_->navigation_map(); | |
| 57 } | |
| 58 | |
| 59 protected: | |
| 60 SafeBrowsingNavigationObserverManager* navigation_observer_manager_; | |
| 61 SafeBrowsingNavigationObserver* navigation_observer_; | |
| 62 | |
| 63 private: | |
| 64 DISALLOW_COPY_AND_ASSIGN(SBNavigationObserverTest); | |
| 65 }; | |
| 66 | |
| 67 TEST_F(SBNavigationObserverTest, BasicNavigationAndCommit) { | |
| 68 // Navigation in current tab | |
|
Charlie Reis
2016/10/25 04:52:27
nit: End comment with period.
Jialiu Lin
2016/10/27 02:15:21
Done.
| |
| 69 content::NavigationController* controller = | |
| 70 &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); | |
| 71 browser()->OpenURL( | |
| 72 content::OpenURLParams(GURL("http://foo/1"), content::Referrer(), | |
| 73 WindowOpenDisposition::CURRENT_TAB, | |
| 74 ui::PAGE_TRANSITION_AUTO_BOOKMARK, false)); | |
| 75 CommitPendingLoad(controller); | |
| 76 int tab_id = SessionTabHelper::IdForTab(controller->GetWebContents()); | |
| 77 auto nav_map = navigation_map(); | |
| 78 ASSERT_EQ(std::size_t(1), nav_map->size()); | |
| 79 ASSERT_EQ(std::size_t(1), nav_map->at(GURL("http://foo/1")).size()); | |
| 80 VerifyNavigationEvent(GURL("http://foo/0"), // source_url | |
| 81 tab_id, // source_tab_id | |
| 82 GURL("http://foo/1"), // original_request_url | |
| 83 tab_id, // target_tab_id | |
| 84 GURL("http://foo/0"), // source_main_frame_url | |
| 85 true, // is_user_initiated | |
| 86 true, // has_committed | |
| 87 false, // has_server_redirect | |
| 88 GURL("http://foo/1"), // destination_url | |
| 89 nav_map->at(GURL("http://foo/1")).at(0)); | |
| 90 } | |
| 91 | |
| 92 TEST_F(SBNavigationObserverTest, ServerRedirect) { | |
| 93 content::RenderFrameHostTester* rfh_tester = | |
| 94 content::RenderFrameHostTester::For( | |
| 95 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()); | |
| 96 rfh_tester->SimulateNavigationStart(GURL("http://foo/3")); | |
| 97 GURL redirect("http://redirect/1"); | |
| 98 rfh_tester->SimulateRedirect(redirect); | |
| 99 rfh_tester->SimulateNavigationCommit(redirect); | |
| 100 int tab_id = SessionTabHelper::IdForTab( | |
| 101 browser()->tab_strip_model()->GetWebContentsAt(0)); | |
| 102 auto nav_map = navigation_map(); | |
| 103 ASSERT_EQ(std::size_t(1), nav_map->size()); | |
| 104 ASSERT_EQ(std::size_t(1), nav_map->at(redirect).size()); | |
| 105 VerifyNavigationEvent(GURL("http://foo/0"), // source_url | |
| 106 tab_id, // source_tab_id | |
| 107 GURL("http://foo/3"), // original_request_url | |
| 108 tab_id, // target_tab_id | |
| 109 GURL("http://foo/0"), // source_main_frame_url | |
| 110 false, // is_user_initiated | |
| 111 true, // has_committed | |
| 112 true, // has_server_redirect | |
| 113 GURL("http://redirect/1"), // destination_url | |
| 114 nav_map->at(GURL("http://redirect/1")).at(0)); | |
| 115 } | |
| 116 | |
| 117 } // namespace safe_browsing | |
| OLD | NEW |