1 // Copyright 2014 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.
6 #include "base/command_line.h"
7 #include "content/browser/loader/cross_site_resource_handler.h"
8 #include "content/browser/loader/resource_dispatcher_host_impl.h"
9 #include "content/browser/loader/resource_request_info_impl.h"
10 #include "content/browser/transition_request_manager.h"
11 #include "content/browser/web_contents/web_contents_impl.h"
12 #include "content/public/browser/web_contents_observer.h"
13 #include "content/public/common/content_switches.h"
14 #include "content/public/test/browser_test_utils.h"
15 #include "content/public/test/content_browser_test.h"
16 #include "content/public/test/content_browser_test_utils.h"
17 #include "content/public/test/test_utils.h"
18 #include "content/shell/browser/shell.h"
19 #include "content/shell/browser/shell_resource_dispatcher_host_delegate.h"
20 #include "net/test/embedded_test_server/embedded_test_server.h"
21 #include "net/url_request/url_request.h"
25 class TransitionBrowserTest
: public ContentBrowserTest
{
27 TransitionBrowserTest() {}
29 void SetUpCommandLine(CommandLine
* command_line
) override
{
30 command_line
->AppendSwitch(
31 switches::kEnableExperimentalWebPlatformFeatures
);
35 DISALLOW_COPY_AND_ASSIGN(TransitionBrowserTest
);
38 class TransitionBrowserTestObserver
39 : public WebContentsObserver
,
40 public ShellResourceDispatcherHostDelegate
{
42 TransitionBrowserTestObserver(WebContents
* web_contents
)
43 : WebContentsObserver(web_contents
),
45 did_defer_response_(false),
46 is_transition_request_(false) {
49 void RequestBeginning(net::URLRequest
* request
,
50 ResourceContext
* resource_context
,
51 AppCacheService
* appcache_service
,
52 ResourceType resource_type
,
53 ScopedVector
<ResourceThrottle
>* throttles
) override
{
54 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
55 ShellResourceDispatcherHostDelegate::RequestBeginning(request
,
62 ResourceRequestInfoImpl
* info
=
63 ResourceRequestInfoImpl::ForRequest(request_
);
65 if (is_transition_request_
) {
66 TransitionRequestManager::GetInstance(
67 )->AddPendingTransitionRequestDataForTesting(
68 info
->GetChildID(), info
->GetRenderFrameID());
72 void OnResponseStarted(net::URLRequest
* request
,
73 ResourceContext
* resource_context
,
74 ResourceResponse
* response
,
75 IPC::Sender
* sender
) override
{
76 ResourceRequestInfoImpl
* info
=
77 ResourceRequestInfoImpl::ForRequest(request_
);
79 did_defer_response_
= info
->cross_site_handler()->did_defer_for_testing();
82 void set_pending_transition_request(bool is_transition_request
) {
83 is_transition_request_
= is_transition_request
;
86 bool did_defer_response() const { return did_defer_response_
; }
89 net::URLRequest
* request_
;
90 bool did_defer_response_
;
91 bool is_transition_request_
;
94 // This tests that normal navigations don't defer at first response.
95 IN_PROC_BROWSER_TEST_F(TransitionBrowserTest
,
96 NormalNavigationNotDeferred
) {
97 // This test shouldn't run in --site-per-process mode, where normal
98 // navigations are also deferred.
99 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
100 switches::kSitePerProcess
))
103 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
104 scoped_ptr
<TransitionBrowserTestObserver
> observer(
105 new TransitionBrowserTestObserver(shell()->web_contents()));
107 ResourceDispatcherHost::Get()->SetDelegate(observer
.get());
109 NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"));
111 EXPECT_FALSE(observer
->did_defer_response());
114 // This tests that when a navigation transition is detected, the response is
116 IN_PROC_BROWSER_TEST_F(TransitionBrowserTest
,
117 TransitionNavigationIsDeferred
) {
118 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
119 scoped_ptr
<TransitionBrowserTestObserver
> observer(
120 new TransitionBrowserTestObserver(shell()->web_contents()));
122 ResourceDispatcherHost::Get()->SetDelegate(observer
.get());
123 observer
->set_pending_transition_request(true);
125 NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"));
127 EXPECT_TRUE(observer
->did_defer_response());
130 // This tests that the renderer is reused between the outgoing and transition.
131 IN_PROC_BROWSER_TEST_F(TransitionBrowserTest
,
132 TransitionNavigationSharesRenderer
) {
133 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
135 NavigateToURL(shell(), embedded_test_server()->GetURL("/title1.html"));
137 int outgoing_process_id
=
138 shell()->web_contents()->GetRenderProcessHost()->GetID();
140 WebContents::CreateParams
create_params(
141 shell()->web_contents()->GetBrowserContext(),
142 shell()->web_contents()->GetSiteInstance());
143 scoped_ptr
<WebContents
> transition_web_contents(
144 WebContents::Create(create_params
));
146 GURL
about_blank(url::kAboutBlankURL
);
147 NavigationController::LoadURLParams
params(about_blank
);
148 transition_web_contents
->GetController().LoadURLWithParams(params
);
149 transition_web_contents
->Focus();
151 WaitForLoadStop(transition_web_contents
.get());
153 int transition_process_id
=
154 transition_web_contents
->GetRenderProcessHost()->GetID();
156 EXPECT_EQ(outgoing_process_id
, transition_process_id
);
159 } // namespace content