Chromium Code Reviews| Index: content/public/test/browser_test_utils.cc |
| diff --git a/content/public/test/browser_test_utils.cc b/content/public/test/browser_test_utils.cc |
| index ca51b0575028171c30990b686532a6c518739d2a..b155835ff7c32fabb7c864375e4f3accbfe42fe8 100644 |
| --- a/content/public/test/browser_test_utils.cc |
| +++ b/content/public/test/browser_test_utils.cc |
| @@ -43,6 +43,8 @@ |
| #include "content/public/browser/browser_plugin_guest_manager.h" |
| #include "content/public/browser/histogram_fetcher.h" |
| #include "content/public/browser/navigation_entry.h" |
| +#include "content/public/browser/navigation_handle.h" |
| +#include "content/public/browser/navigation_throttle.h" |
| #include "content/public/browser/notification_service.h" |
| #include "content/public/browser/notification_types.h" |
| #include "content/public/browser/render_frame_host.h" |
| @@ -346,6 +348,40 @@ CrossSiteRedirectResponseHandler(const GURL& server_base_url, |
| return std::move(http_response); |
| } |
| +// Helper class used by the TestNavigationManager to pause navigations. |
| +// Note: the throttle should be added to the *end* of the list of throttles, |
| +// so all NavigationThrottles that should be attached observe the |
| +// WillStartRequest callback. This throttle is added, and will re-add itself |
| +// to the end of the list on WillStartRequest. |
| +class TestNavigationManagerThrottle : public NavigationThrottle { |
| + public: |
| + TestNavigationManagerThrottle(NavigationHandle* handle, |
| + base::Closure on_will_start_request_closure, |
| + bool attached_in_did_start) |
| + : NavigationThrottle(handle), |
| + on_will_start_request_closure_(on_will_start_request_closure), |
| + attached_in_did_start_(attached_in_did_start) {} |
| + ~TestNavigationManagerThrottle() override {} |
| + |
| + private: |
| + // NavigationThrottle implementation. |
| + NavigationThrottle::ThrottleCheckResult WillStartRequest() override { |
|
clamy
2016/07/26 17:09:22
I'm not terribly fond of this. Attaching a throttl
Charlie Harrison
2016/07/26 19:56:06
Hm okay. I've modified NavigationHandleImpl::Regis
|
| + if (attached_in_did_start_) { |
| + navigation_handle()->RegisterThrottleForTesting( |
| + base::WrapUnique(new TestNavigationManagerThrottle( |
| + navigation_handle(), on_will_start_request_closure_, false))); |
| + return NavigationThrottle::PROCEED; |
| + } else { |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + on_will_start_request_closure_); |
| + return NavigationThrottle::DEFER; |
| + } |
| + } |
| + |
| + base::Closure on_will_start_request_closure_; |
| + bool attached_in_did_start_; |
| +}; |
| + |
| } // namespace |
| bool NavigateIframeToURL(WebContents* web_contents, |
| @@ -1431,4 +1467,94 @@ uint32_t InputMsgWatcher::WaitForAck() { |
| return ack_result_; |
| } |
| +TestNavigationManager::TestNavigationManager(int filtering_frame_tree_node_id, |
| + WebContents* web_contents, |
| + const GURL& url) |
| + : WebContentsObserver(web_contents), |
| + filtering_frame_tree_node_id_(filtering_frame_tree_node_id), |
| + url_(url), |
| + navigation_paused_(false), |
| + handle_(nullptr), |
| + handled_navigation_(false), |
| + weak_factory_(this) {} |
| + |
| +TestNavigationManager::TestNavigationManager(WebContents* web_contents, |
| + const GURL& url) |
| + : TestNavigationManager(FrameTreeNode::kFrameTreeNodeInvalidId, |
| + web_contents, |
| + url) {} |
| + |
| +TestNavigationManager::~TestNavigationManager() { |
| + ResumeNavigation(); |
| +} |
| + |
| +void TestNavigationManager::WaitForWillStartRequest() { |
| + DCHECK(!did_finish_loop_runner_); |
| + if (!handle_ && handled_navigation_) |
| + return; |
| + if (navigation_paused_) |
| + return; |
| + will_start_loop_runner_ = new MessageLoopRunner(); |
| + will_start_loop_runner_->Run(); |
| + will_start_loop_runner_ = nullptr; |
| +} |
| + |
| +void TestNavigationManager::WaitForNavigationFinished() { |
| + DCHECK(!will_start_loop_runner_); |
| + if (!handle_ && handled_navigation_) |
| + return; |
| + // Ensure the navigation is resumed if the manager paused it previously. |
| + if (navigation_paused_) |
| + ResumeNavigation(); |
| + did_finish_loop_runner_ = new MessageLoopRunner(); |
| + did_finish_loop_runner_->Run(); |
| + did_finish_loop_runner_ = nullptr; |
| +} |
| + |
| +void TestNavigationManager::DidStartNavigation(NavigationHandle* handle) { |
| + if (handle_ || handle->GetURL() != url_) |
| + return; |
| + if (handled_navigation_) |
| + return; |
| + |
| + if (filtering_frame_tree_node_id_ != FrameTreeNode::kFrameTreeNodeInvalidId && |
| + handle->GetFrameTreeNodeId() != filtering_frame_tree_node_id_) { |
| + return; |
| + } |
| + |
| + handle_ = handle; |
| + std::unique_ptr<NavigationThrottle> throttle( |
| + new TestNavigationManagerThrottle( |
| + handle_, base::Bind(&TestNavigationManager::OnWillStartRequest, |
| + weak_factory_.GetWeakPtr()), true)); |
| + handle_->RegisterThrottleForTesting(std::move(throttle)); |
| +} |
| + |
| +void TestNavigationManager::DidFinishNavigation(NavigationHandle* handle) { |
| + if (handle != handle_) |
| + return; |
| + handle_ = nullptr; |
| + handled_navigation_ = true; |
| + navigation_paused_ = false; |
| + if (did_finish_loop_runner_) |
|
clamy
2016/07/26 17:09:22
We may want to also quit the will_start_loop_runne
Charlie Harrison
2016/07/26 19:56:07
Done.
|
| + did_finish_loop_runner_->Quit(); |
| +} |
| + |
| +void TestNavigationManager::OnWillStartRequest() { |
| + navigation_paused_ = true; |
| + if (will_start_loop_runner_) |
| + will_start_loop_runner_->Quit(); |
| + |
| + // If waiting for the navigation to finish, resume the navigation. |
| + if (did_finish_loop_runner_) |
| + ResumeNavigation(); |
| +} |
| + |
| +void TestNavigationManager::ResumeNavigation() { |
| + if (!navigation_paused_ || !handle_) |
| + return; |
| + navigation_paused_ = false; |
| + handle_->Resume(); |
| +} |
| + |
| } // namespace content |