Index: content/browser/transition_browsertest.cc |
diff --git a/content/browser/transition_browsertest.cc b/content/browser/transition_browsertest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..e0a66bbbde86b064760f7ab0b30cfb217630a532 |
--- /dev/null |
+++ b/content/browser/transition_browsertest.cc |
@@ -0,0 +1,153 @@ |
+// Copyright 2014 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 "base/bind.h" |
+#include "content/browser/loader/cross_site_resource_handler.h" |
+#include "content/browser/loader/resource_dispatcher_host_impl.h" |
+#include "content/browser/loader/resource_request_info_impl.h" |
+#include "content/browser/transition_request_manager.h" |
+#include "content/browser/web_contents/web_contents_impl.h" |
+#include "content/public/browser/web_contents_observer.h" |
+#include "content/public/test/content_browser_test.h" |
+#include "content/public/test/content_browser_test_utils.h" |
+#include "content/public/test/test_utils.h" |
+#include "content/shell/browser/shell.h" |
+#include "content/shell/browser/shell_resource_dispatcher_host_delegate.h" |
+#include "net/test/embedded_test_server/embedded_test_server.h" |
+#include "net/url_request/url_request.h" |
+ |
+namespace content { |
+ |
+class TransitionBrowserTest : public ContentBrowserTest { |
+ public: |
+ TransitionBrowserTest() {} |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(TransitionBrowserTest); |
+}; |
+ |
+class TransitionBrowserTestObserver |
+ : public WebContentsObserver, |
+ public ShellResourceDispatcherHostDelegate { |
+ public: |
+ TransitionBrowserTestObserver(Shell* shell) |
+ : WebContentsObserver(shell->web_contents()), |
+ request_(NULL), |
+ shell_(shell), |
+ provisional_load_started_(false), |
+ provisional_load_committed_(false), |
+ did_defer_response_(false), |
+ is_transition_request_(false) {} |
+ |
+ virtual void RequestBeginning( |
+ net::URLRequest* request, |
+ ResourceContext* resource_context, |
+ appcache::AppCacheService* appcache_service, |
+ ResourceType::Type resource_type, |
+ int child_id, |
+ int route_id, |
+ ScopedVector<ResourceThrottle>* throttles) OVERRIDE { |
+ CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
+ ShellResourceDispatcherHostDelegate::RequestBeginning(request, |
+ resource_context, |
+ appcache_service, |
+ resource_type, |
+ child_id, |
+ route_id, |
+ throttles); |
+ request_ = request; |
+ |
+ ResourceRequestInfoImpl* info = |
+ ResourceRequestInfoImpl::ForRequest(request_); |
+ |
+ TransitionRequestManager::GetInstance()->SetHasPendingTransitionRequest( |
+ child_id, info->GetRenderFrameID(), is_transition_request_); |
+ |
+ static_cast<WebContentsImpl*>(shell_->web_contents())-> |
jam
2014/06/16 03:46:10
WebContents isn't thread safe, so it shouldn't be
shatch
2014/06/16 22:32:19
Done.
|
+ set_did_defer_callback_for_testing( |
+ base::Bind(&TransitionBrowserTestObserver::OnDidDeferCallback, |
+ base::Unretained(this))); |
+ } |
+ |
+ virtual void DidStartProvisionalLoadForFrame( |
+ int64 frame_id, |
+ int64 parent_frame_id, |
+ bool is_main_frame, |
+ const GURL& validated_url, |
+ bool is_error_page, |
+ bool is_iframe_srcdoc, |
+ RenderViewHost* render_view_host) OVERRIDE { |
+ provisional_load_started_ = true; |
+ } |
+ |
+ virtual void DidCommitProvisionalLoadForFrame( |
+ int64 frame_id, |
+ const base::string16& frame_unique_name, |
+ bool is_main_frame, |
+ const GURL& url, |
+ PageTransition transition_type, |
+ RenderViewHost* render_view_host) { |
+ provisional_load_committed_ = true; |
+ } |
+ |
+ void OnDidDeferCallback() { |
+ ASSERT_TRUE(provisional_load_started_); |
+ ASSERT_FALSE(provisional_load_committed_); |
+ |
+ // Check that the request is currently blocked |
+ ResourceRequestInfoImpl* info = |
+ ResourceRequestInfoImpl::ForRequest(request_); |
+ |
+ ASSERT_TRUE(info->cross_site_handler()->is_deferred_for_testing()); |
+ |
+ static_cast<WebContentsImpl*>(shell_->web_contents())-> |
+ ResumeResponseDeferredAtStart(); |
+ |
+ did_defer_response_ = true; |
+ } |
+ |
+ void set_pending_transition_request(bool is_transition_request) { |
+ is_transition_request_ = is_transition_request; |
+ } |
+ |
+ bool did_defer_response() const { return did_defer_response_; } |
+ |
+ private: |
+ net::URLRequest* request_; |
+ Shell* shell_; |
+ bool provisional_load_started_; |
+ bool provisional_load_committed_; |
+ bool did_defer_response_; |
+ bool is_transition_request_; |
+}; |
+ |
+// This tests that DidDeferAfterResponseStarted is not called on normal |
+// navigations. |
+IN_PROC_BROWSER_TEST_F(TransitionBrowserTest, |
+ DidDeferAfterResponseStartedNotCalled) { |
+ ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
+ scoped_ptr<TransitionBrowserTestObserver> observer( |
+ new TransitionBrowserTestObserver(shell())); |
+ |
+ NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html")); |
+ |
+ ASSERT_FALSE(observer->did_defer_response()); |
+} |
+ |
+// This tests that DidDeferAfterResponseStarted is called on a transition |
+// navigation, and that the request is deferred. |
+IN_PROC_BROWSER_TEST_F(TransitionBrowserTest, ResponseDeferredIfHandled) { |
+ ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
+ scoped_ptr<TransitionBrowserTestObserver> observer( |
+ new TransitionBrowserTestObserver(shell())); |
+ |
+ ResourceDispatcherHost::Get()->SetDelegate(observer.get()); |
+ observer->set_pending_transition_request(true); |
+ |
+ NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html")); |
+ |
+ ASSERT_TRUE(observer->did_defer_response()); |
+} |
+ |
+} // namespace content |