| Index: chrome/browser/safe_browsing/safe_browsing_navigation_observer_unittest.cc | 
| diff --git a/chrome/browser/safe_browsing/safe_browsing_navigation_observer_unittest.cc b/chrome/browser/safe_browsing/safe_browsing_navigation_observer_unittest.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..a06961c0c9af71ecf498070a0a979460cb867f47 | 
| --- /dev/null | 
| +++ b/chrome/browser/safe_browsing/safe_browsing_navigation_observer_unittest.cc | 
| @@ -0,0 +1,111 @@ | 
| +// Copyright 2016 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 "chrome/browser/safe_browsing/safe_browsing_navigation_observer.h" | 
| +#include "chrome/browser/sessions/session_tab_helper.h" | 
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" | 
| +#include "chrome/test/base/browser_with_test_window_test.h" | 
| +#include "content/public/test/test_renderer_host.h" | 
| +#include "testing/gtest/include/gtest/gtest.h" | 
| +#include "ui/base/window_open_disposition.h" | 
| + | 
| +namespace safe_browsing { | 
| + | 
| +class SBNavigationObserverTest : public BrowserWithTestWindowTest { | 
| + public: | 
| +  SBNavigationObserverTest() {} | 
| +  void SetUp() override { | 
| +    BrowserWithTestWindowTest::SetUp(); | 
| +    AddTab(browser(), GURL("chrome://blank")); | 
| +    navigation_observer_ = new SafeBrowsingNavigationObserver(); | 
| +  } | 
| +  void TearDown() override { | 
| +    delete navigation_observer_; | 
| +    BrowserWithTestWindowTest::TearDown(); | 
| +  } | 
| +  void VerifyNavigationEvent( | 
| +      const GURL& expected_source_url, | 
| +      int expected_source_tab, | 
| +      const GURL& expected_target_url, | 
| +      int expected_target_tab, | 
| +      const GURL& expected_main_frame_url, | 
| +      bool expected_is_user_initiated, | 
| +      bool expected_has_committed, | 
| +      bool expected_is_server_redirect, | 
| +      bool expected_is_finished, | 
| +      const GURL& expected_redirect_url, | 
| +      const SafeBrowsingNavigationObserver::NavigationEvent& actual_nav_event) { | 
| +    EXPECT_EQ(expected_source_url, actual_nav_event.source_url); | 
| +    EXPECT_EQ(expected_source_tab, actual_nav_event.source_tab_id); | 
| +    EXPECT_EQ(expected_target_url, actual_nav_event.target_url); | 
| +    EXPECT_EQ(expected_target_tab, actual_nav_event.target_tab_id); | 
| +    EXPECT_EQ(expected_main_frame_url, actual_nav_event.main_frame_url); | 
| +    EXPECT_EQ(expected_is_user_initiated, actual_nav_event.is_user_initiated); | 
| +    EXPECT_EQ(expected_has_committed, actual_nav_event.has_committed); | 
| +    EXPECT_EQ(expected_is_server_redirect, actual_nav_event.is_server_redirect); | 
| +    EXPECT_EQ(expected_is_finished, actual_nav_event.is_finished); | 
| +    EXPECT_EQ(expected_redirect_url, actual_nav_event.server_redirect_url); | 
| +  } | 
| + | 
| + protected: | 
| +  SafeBrowsingNavigationObserver* navigation_observer_; | 
| + | 
| + private: | 
| +  DISALLOW_COPY_AND_ASSIGN(SBNavigationObserverTest); | 
| +}; | 
| + | 
| +TEST_F(SBNavigationObserverTest, BasicNavigationAndCommit) { | 
| +  // Navigation in current tab | 
| +  content::NavigationController* controller = | 
| +      &browser()->tab_strip_model()->GetWebContentsAt(0)->GetController(); | 
| +  browser()->OpenURL( | 
| +      content::OpenURLParams(GURL("http://foo/1"), content::Referrer(), | 
| +                             WindowOpenDisposition::CURRENT_TAB, | 
| +                             ui::PAGE_TRANSITION_AUTO_BOOKMARK, false)); | 
| +  CommitPendingLoad(controller); | 
| +  int tab_id = SessionTabHelper::IdForTab(controller->GetWebContents()); | 
| +  SafeBrowsingNavigationObserver::NavigationMap* nav_map = | 
| +      navigation_observer_->navigation_map(); | 
| +  ASSERT_EQ(std::size_t(1), nav_map->size()); | 
| +  ASSERT_EQ(std::size_t(1), nav_map->at(GURL("http://foo/1")).size()); | 
| +  VerifyNavigationEvent(GURL("chrome://blank/"), tab_id, GURL("http://foo/1"), | 
| +                        tab_id, GURL("chrome://blank/"), true, true, false, | 
| +                        true, GURL(), nav_map->at(GURL("http://foo/1")).at(0)); | 
| +  // Open a new tab and navigate | 
| +  browser()->OpenURL( | 
| +      content::OpenURLParams(GURL("http://foo/2"), content::Referrer(), | 
| +                             WindowOpenDisposition::NEW_FOREGROUND_TAB, | 
| +                             ui::PAGE_TRANSITION_AUTO_BOOKMARK, false)); | 
| +  controller = | 
| +      &browser()->tab_strip_model()->GetWebContentsAt(1)->GetController(); | 
| +  CommitPendingLoad(controller); | 
| +  int new_tab_id = SessionTabHelper::IdForTab( | 
| +      browser()->tab_strip_model()->GetActiveWebContents()); | 
| +  ASSERT_EQ(std::size_t(2), nav_map->size()); | 
| +  ASSERT_EQ(std::size_t(1), nav_map->at(GURL("http://foo/2")).size()); | 
| +  VerifyNavigationEvent(GURL("http://foo/2"), new_tab_id, GURL("http://foo/2"), | 
| +                        new_tab_id, GURL("http://foo/2"), true, true, false, | 
| +                        true, GURL(), nav_map->at(GURL("http://foo/2")).at(0)); | 
| +} | 
| + | 
| +TEST_F(SBNavigationObserverTest, ServerRedirect) { | 
| +  content::RenderFrameHostTester* rfh_tester = | 
| +      content::RenderFrameHostTester::For( | 
| +          browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame()); | 
| +  rfh_tester->SimulateNavigationStart(GURL("http://foo/3")); | 
| +  GURL redirect("http://redirect/1"); | 
| +  rfh_tester->SimulateRedirect(redirect); | 
| +  rfh_tester->SimulateNavigationCommit(redirect); | 
| +  int tab_id = SessionTabHelper::IdForTab( | 
| +      browser()->tab_strip_model()->GetWebContentsAt(0)); | 
| +  SafeBrowsingNavigationObserver::NavigationMap* nav_map = | 
| +      navigation_observer_->navigation_map(); | 
| +  ASSERT_EQ(std::size_t(1), nav_map->size()); | 
| +  ASSERT_EQ(std::size_t(1), nav_map->at(redirect).size()); | 
| +  VerifyNavigationEvent(GURL("chrome://blank/"), tab_id, GURL("http://foo/3"), | 
| +                        tab_id, GURL("chrome://blank/"), true, true, true, true, | 
| +                        GURL("http://redirect/1"), | 
| +                        nav_map->at(GURL("http://redirect/1")).at(0)); | 
| +} | 
| + | 
| +}  // namespace safe_browsing | 
|  |