| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/public/test/test_navigation_observer.h" | 5 #include "content/public/test/test_navigation_observer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/ptr_util.h" |
| 9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
| 11 #include "base/stl_util.h" | |
| 12 #include "content/browser/web_contents/web_contents_impl.h" | 12 #include "content/browser/web_contents/web_contents_impl.h" |
| 13 #include "content/public/browser/web_contents_observer.h" | 13 #include "content/public/browser/web_contents_observer.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 | 17 |
| 18 class TestNavigationObserver::TestWebContentsObserver | 18 class TestNavigationObserver::TestWebContentsObserver |
| 19 : public WebContentsObserver { | 19 : public WebContentsObserver { |
| 20 public: | 20 public: |
| 21 TestWebContentsObserver(TestNavigationObserver* parent, | 21 TestWebContentsObserver(TestNavigationObserver* parent, |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 web_contents_created_callback_( | 102 web_contents_created_callback_( |
| 103 base::Bind( | 103 base::Bind( |
| 104 &TestNavigationObserver::OnWebContentsCreated, | 104 &TestNavigationObserver::OnWebContentsCreated, |
| 105 base::Unretained(this))) { | 105 base::Unretained(this))) { |
| 106 if (web_contents) | 106 if (web_contents) |
| 107 RegisterAsObserver(web_contents); | 107 RegisterAsObserver(web_contents); |
| 108 } | 108 } |
| 109 | 109 |
| 110 TestNavigationObserver::~TestNavigationObserver() { | 110 TestNavigationObserver::~TestNavigationObserver() { |
| 111 StopWatchingNewWebContents(); | 111 StopWatchingNewWebContents(); |
| 112 | |
| 113 base::STLDeleteContainerPointers(web_contents_observers_.begin(), | |
| 114 web_contents_observers_.end()); | |
| 115 } | 112 } |
| 116 | 113 |
| 117 void TestNavigationObserver::Wait() { | 114 void TestNavigationObserver::Wait() { |
| 118 message_loop_runner_->Run(); | 115 message_loop_runner_->Run(); |
| 119 } | 116 } |
| 120 | 117 |
| 121 void TestNavigationObserver::StartWatchingNewWebContents() { | 118 void TestNavigationObserver::StartWatchingNewWebContents() { |
| 122 WebContentsImpl::FriendZone::AddCreatedCallbackForTesting( | 119 WebContentsImpl::FriendZone::AddCreatedCallbackForTesting( |
| 123 web_contents_created_callback_); | 120 web_contents_created_callback_); |
| 124 } | 121 } |
| 125 | 122 |
| 126 void TestNavigationObserver::StopWatchingNewWebContents() { | 123 void TestNavigationObserver::StopWatchingNewWebContents() { |
| 127 WebContentsImpl::FriendZone::RemoveCreatedCallbackForTesting( | 124 WebContentsImpl::FriendZone::RemoveCreatedCallbackForTesting( |
| 128 web_contents_created_callback_); | 125 web_contents_created_callback_); |
| 129 } | 126 } |
| 130 | 127 |
| 131 void TestNavigationObserver::RegisterAsObserver(WebContents* web_contents) { | 128 void TestNavigationObserver::RegisterAsObserver(WebContents* web_contents) { |
| 132 web_contents_observers_.insert( | 129 web_contents_observers_.insert( |
| 133 new TestWebContentsObserver(this, web_contents)); | 130 base::MakeUnique<TestWebContentsObserver>(this, web_contents)); |
| 134 } | 131 } |
| 135 | 132 |
| 136 void TestNavigationObserver::OnWebContentsCreated(WebContents* web_contents) { | 133 void TestNavigationObserver::OnWebContentsCreated(WebContents* web_contents) { |
| 137 RegisterAsObserver(web_contents); | 134 RegisterAsObserver(web_contents); |
| 138 } | 135 } |
| 139 | 136 |
| 140 void TestNavigationObserver::OnWebContentsDestroyed( | 137 void TestNavigationObserver::OnWebContentsDestroyed( |
| 141 TestWebContentsObserver* observer, | 138 TestWebContentsObserver* observer, |
| 142 WebContents* web_contents) { | 139 WebContents* web_contents) { |
| 143 web_contents_observers_.erase(observer); | 140 web_contents_observers_.erase(std::find_if( |
| 144 delete observer; | 141 web_contents_observers_.begin(), web_contents_observers_.end(), |
| 142 [observer](const std::unique_ptr<TestWebContentsObserver>& ptr) { |
| 143 return ptr.get() == observer; |
| 144 })); |
| 145 } | 145 } |
| 146 | 146 |
| 147 void TestNavigationObserver::OnNavigationEntryCommitted( | 147 void TestNavigationObserver::OnNavigationEntryCommitted( |
| 148 TestWebContentsObserver* observer, | 148 TestWebContentsObserver* observer, |
| 149 WebContents* web_contents, | 149 WebContents* web_contents, |
| 150 const LoadCommittedDetails& load_details) { | 150 const LoadCommittedDetails& load_details) { |
| 151 navigation_started_ = true; | 151 navigation_started_ = true; |
| 152 } | 152 } |
| 153 | 153 |
| 154 void TestNavigationObserver::OnDidAttachInterstitialPage( | 154 void TestNavigationObserver::OnDidAttachInterstitialPage( |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 | 192 |
| 193 void TestNavigationObserver::OnDidCommitProvisionalLoadForFrame( | 193 void TestNavigationObserver::OnDidCommitProvisionalLoadForFrame( |
| 194 RenderFrameHost* render_frame_host, | 194 RenderFrameHost* render_frame_host, |
| 195 const GURL& url, | 195 const GURL& url, |
| 196 ui::PageTransition transition_type) { | 196 ui::PageTransition transition_type) { |
| 197 last_navigation_url_ = url; | 197 last_navigation_url_ = url; |
| 198 last_navigation_succeeded_ = true; | 198 last_navigation_succeeded_ = true; |
| 199 } | 199 } |
| 200 | 200 |
| 201 } // namespace content | 201 } // namespace content |
| OLD | NEW |