| 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 #include "chrome/browser/safe_browsing/safe_browsing_navigation_observer.h" | 
|  | 5 #include "chrome/browser/sessions/session_tab_helper.h" | 
|  | 6 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 
|  | 7 #include "chrome/test/base/browser_with_test_window_test.h" | 
|  | 8 #include "content/public/test/test_renderer_host.h" | 
|  | 9 #include "testing/gtest/include/gtest/gtest.h" | 
|  | 10 #include "ui/base/window_open_disposition.h" | 
|  | 11 | 
|  | 12 namespace safe_browsing { | 
|  | 13 | 
|  | 14 class SBNavigationObserverTest : public BrowserWithTestWindowTest { | 
|  | 15  public: | 
|  | 16   SBNavigationObserverTest() {} | 
|  | 17   void SetUp() override { | 
|  | 18     BrowserWithTestWindowTest::SetUp(); | 
|  | 19     AddTab(browser(), GURL("chrome://blank")); | 
|  | 20     navigation_observer_ = new SafeBrowsingNavigationObserver(); | 
|  | 21   } | 
|  | 22   void TearDown() override { | 
|  | 23     delete navigation_observer_; | 
|  | 24     BrowserWithTestWindowTest::TearDown(); | 
|  | 25   } | 
|  | 26   void VerifyNavigationEvent( | 
|  | 27       const GURL& expected_source_url, | 
|  | 28       int expected_source_tab, | 
|  | 29       const GURL& expected_target_url, | 
|  | 30       int expected_target_tab, | 
|  | 31       const GURL& expected_main_frame_url, | 
|  | 32       bool expected_is_user_initiated, | 
|  | 33       bool expected_has_committed, | 
|  | 34       bool expected_is_server_redirect, | 
|  | 35       bool expected_is_finished, | 
|  | 36       const GURL& expected_redirect_url, | 
|  | 37       const SafeBrowsingNavigationObserver::NavigationEvent& actual_nav_event) { | 
|  | 38     EXPECT_EQ(expected_source_url, actual_nav_event.source_url); | 
|  | 39     EXPECT_EQ(expected_source_tab, actual_nav_event.source_tab_id); | 
|  | 40     EXPECT_EQ(expected_target_url, actual_nav_event.target_url); | 
|  | 41     EXPECT_EQ(expected_target_tab, actual_nav_event.target_tab_id); | 
|  | 42     EXPECT_EQ(expected_main_frame_url, actual_nav_event.main_frame_url); | 
|  | 43     EXPECT_EQ(expected_is_user_initiated, actual_nav_event.is_user_initiated); | 
|  | 44     EXPECT_EQ(expected_has_committed, actual_nav_event.has_committed); | 
|  | 45     EXPECT_EQ(expected_is_server_redirect, actual_nav_event.is_server_redirect); | 
|  | 46     EXPECT_EQ(expected_is_finished, actual_nav_event.is_finished); | 
|  | 47     EXPECT_EQ(expected_redirect_url, actual_nav_event.server_redirect_url); | 
|  | 48   } | 
|  | 49 | 
|  | 50  protected: | 
|  | 51   SafeBrowsingNavigationObserver* navigation_observer_; | 
|  | 52 | 
|  | 53  private: | 
|  | 54   DISALLOW_COPY_AND_ASSIGN(SBNavigationObserverTest); | 
|  | 55 }; | 
|  | 56 | 
|  | 57 TEST_F(SBNavigationObserverTest, BasicNavigationAndCommit) { | 
|  | 58   // Navigation in current tab | 
|  | 59   content::NavigationController* controller = | 
|  | 60       &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); | 
|  | 61   browser()->OpenURL( | 
|  | 62       content::OpenURLParams(GURL("http://foo/1"), content::Referrer(), | 
|  | 63                              WindowOpenDisposition::CURRENT_TAB, | 
|  | 64                              ui::PAGE_TRANSITION_AUTO_BOOKMARK, false)); | 
|  | 65   CommitPendingLoad(controller); | 
|  | 66   int tab_id = SessionTabHelper::IdForTab(controller->GetWebContents()); | 
|  | 67   SafeBrowsingNavigationObserver::NavigationMap* nav_map = | 
|  | 68       navigation_observer_->navigation_map(); | 
|  | 69   ASSERT_EQ(std::size_t(1), nav_map->size()); | 
|  | 70   ASSERT_EQ(std::size_t(1), nav_map->at(GURL("http://foo/1")).size()); | 
|  | 71   VerifyNavigationEvent(GURL("chrome://blank/"), tab_id, GURL("http://foo/1"), | 
|  | 72                         tab_id, GURL("chrome://blank/"), true, true, false, | 
|  | 73                         true, GURL(), nav_map->at(GURL("http://foo/1")).at(0)); | 
|  | 74   // Open a new tab and navigate | 
|  | 75   browser()->OpenURL( | 
|  | 76       content::OpenURLParams(GURL("http://foo/2"), content::Referrer(), | 
|  | 77                              WindowOpenDisposition::NEW_FOREGROUND_TAB, | 
|  | 78                              ui::PAGE_TRANSITION_AUTO_BOOKMARK, false)); | 
|  | 79   controller = | 
|  | 80       &browser()->tab_strip_model()->GetWebContentsAt(1)->GetController(); | 
|  | 81   CommitPendingLoad(controller); | 
|  | 82   int new_tab_id = SessionTabHelper::IdForTab( | 
|  | 83       browser()->tab_strip_model()->GetActiveWebContents()); | 
|  | 84   ASSERT_EQ(std::size_t(2), nav_map->size()); | 
|  | 85   ASSERT_EQ(std::size_t(1), nav_map->at(GURL("http://foo/2")).size()); | 
|  | 86   VerifyNavigationEvent(GURL("http://foo/2"), new_tab_id, GURL("http://foo/2"), | 
|  | 87                         new_tab_id, GURL("http://foo/2"), true, true, false, | 
|  | 88                         true, GURL(), nav_map->at(GURL("http://foo/2")).at(0)); | 
|  | 89 } | 
|  | 90 | 
|  | 91 TEST_F(SBNavigationObserverTest, ServerRedirect) { | 
|  | 92   content::RenderFrameHostTester* rfh_tester = | 
|  | 93       content::RenderFrameHostTester::For( | 
|  | 94           browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()); | 
|  | 95   rfh_tester->SimulateNavigationStart(GURL("http://foo/3")); | 
|  | 96   GURL redirect("http://redirect/1"); | 
|  | 97   rfh_tester->SimulateRedirect(redirect); | 
|  | 98   rfh_tester->SimulateNavigationCommit(redirect); | 
|  | 99   int tab_id = SessionTabHelper::IdForTab( | 
|  | 100       browser()->tab_strip_model()->GetWebContentsAt(0)); | 
|  | 101   SafeBrowsingNavigationObserver::NavigationMap* nav_map = | 
|  | 102       navigation_observer_->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("chrome://blank/"), tab_id, GURL("http://foo/3"), | 
|  | 106                         tab_id, GURL("chrome://blank/"), true, true, true, true, | 
|  | 107                         GURL("http://redirect/1"), | 
|  | 108                         nav_map->at(GURL("http://redirect/1")).at(0)); | 
|  | 109 } | 
|  | 110 | 
|  | 111 }  // namespace safe_browsing | 
| OLD | NEW | 
|---|