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
3 // found in the LICENSE file.
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/threading/sequenced_worker_pool.h"
13 #include "chrome/browser/net/url_request_mock_util.h"
14 #include "chrome/browser/prerender/prerender_contents.h"
15 #include "chrome/browser/prerender/prerender_manager.h"
16 #include "chrome/browser/prerender/prerender_resource_throttle.h"
17 #include "chrome/browser/prerender/prerender_tracker.h"
18 #include "chrome/test/base/testing_browser_process.h"
19 #include "content/public/browser/resource_controller.h"
20 #include "content/public/browser/resource_request_info.h"
21 #include "content/public/test/test_browser_thread.h"
22 #include "ipc/ipc_message.h"
23 #include "net/base/request_priority.h"
24 #include "net/test/url_request/url_request_mock_http_job.h"
25 #include "net/url_request/redirect_info.h"
26 #include "net/url_request/url_request.h"
27 #include "net/url_request/url_request_test_util.h"
28 #include "testing/gtest/include/gtest/gtest.h"
30 using content::BrowserThread
;
31 using content::ResourceType
;
37 class TestPrerenderContents
: public PrerenderContents
{
39 TestPrerenderContents(PrerenderManager
* prerender_manager
,
40 int child_id
, int route_id
)
41 : PrerenderContents(prerender_manager
, static_cast<Profile
*>(NULL
),
42 GURL(), content::Referrer(), ORIGIN_NONE
,
43 PrerenderManager::kNoExperiment
),
46 PrerenderResourceThrottle::OverridePrerenderContentsForTesting(this);
49 ~TestPrerenderContents() override
{
50 if (final_status() == FINAL_STATUS_MAX
)
51 SetFinalStatus(FINAL_STATUS_USED
);
52 PrerenderResourceThrottle::OverridePrerenderContentsForTesting(NULL
);
55 bool GetChildId(int* child_id
) const override
{
56 *child_id
= child_id_
;
60 bool GetRouteId(int* route_id
) const override
{
61 *route_id
= route_id_
;
66 prerendering_has_started_
= true;
67 NotifyPrerenderStart();
71 Destroy(FINAL_STATUS_CANCELLED
);
83 class TestPrerenderManager
: public PrerenderManager
{
85 explicit TestPrerenderManager(PrerenderTracker
* prerender_tracker
) :
86 PrerenderManager(NULL
, prerender_tracker
) {
87 mutable_config().rate_limit_enabled
= false;
90 // We never allocate our PrerenderContents in PrerenderManager, so we don't
91 // ever want the default pending delete behaviour.
92 void MoveEntryToPendingDelete(PrerenderContents
* entry
,
93 FinalStatus final_status
) override
{}
96 class DeferredRedirectDelegate
: public net::URLRequest::Delegate
,
97 public content::ResourceController
{
99 DeferredRedirectDelegate()
101 was_deferred_(false),
102 cancel_called_(false),
103 resume_called_(false) {
106 void SetThrottle(PrerenderResourceThrottle
* throttle
) {
107 throttle_
= throttle
;
108 throttle_
->set_controller_for_testing(this);
112 run_loop_
.reset(new base::RunLoop());
116 bool was_deferred() const { return was_deferred_
; }
117 bool cancel_called() const { return cancel_called_
; }
118 bool resume_called() const { return resume_called_
; }
120 // net::URLRequest::Delegate implementation:
121 void OnReceivedRedirect(net::URLRequest
* request
,
122 const net::RedirectInfo
& redirect_info
,
123 bool* defer_redirect
) override
{
124 // Defer the redirect either way.
125 *defer_redirect
= true;
127 // Find out what the throttle would have done.
128 throttle_
->WillRedirectRequest(redirect_info
, &was_deferred_
);
131 void OnResponseStarted(net::URLRequest
* request
) override
{}
132 void OnReadCompleted(net::URLRequest
* request
, int bytes_read
) override
{}
134 // content::ResourceController implementation:
135 void Cancel() override
{
136 EXPECT_FALSE(cancel_called_
);
137 EXPECT_FALSE(resume_called_
);
139 cancel_called_
= true;
142 void CancelAndIgnore() override
{ Cancel(); }
143 void CancelWithError(int error_code
) override
{ Cancel(); }
144 void Resume() override
{
145 EXPECT_TRUE(was_deferred_
);
146 EXPECT_FALSE(cancel_called_
);
147 EXPECT_FALSE(resume_called_
);
149 resume_called_
= true;
154 scoped_ptr
<base::RunLoop
> run_loop_
;
155 PrerenderResourceThrottle
* throttle_
;
160 DISALLOW_COPY_AND_ASSIGN(DeferredRedirectDelegate
);
165 class PrerenderTrackerTest
: public testing::Test
{
167 static const int kDefaultChildId
= 0;
168 static const int kDefaultRouteId
= 100;
170 PrerenderTrackerTest() :
171 ui_thread_(BrowserThread::UI
, &message_loop_
),
172 io_thread_(BrowserThread::IO
, &message_loop_
),
173 prerender_manager_(prerender_tracker()),
174 test_contents_(&prerender_manager_
, kDefaultChildId
, kDefaultRouteId
) {
175 chrome_browser_net::SetUrlRequestMocksEnabled(true);
178 ~PrerenderTrackerTest() override
{
179 chrome_browser_net::SetUrlRequestMocksEnabled(false);
181 // Cleanup work so the file IO tasks from URLRequestMockHTTPJob
183 content::BrowserThread::GetBlockingPool()->FlushForTesting();
187 PrerenderTracker
* prerender_tracker() {
188 return g_browser_process
->prerender_tracker();
191 TestPrerenderManager
* prerender_manager() {
192 return &prerender_manager_
;
195 TestPrerenderContents
* test_contents() {
196 return &test_contents_
;
199 // Runs any tasks queued on either thread.
201 message_loop_
.RunUntilIdle();
205 base::MessageLoopForIO message_loop_
;
206 content::TestBrowserThread ui_thread_
;
207 content::TestBrowserThread io_thread_
;
209 TestPrerenderManager prerender_manager_
;
210 TestPrerenderContents test_contents_
;
213 // Checks that deferred redirects are throttled and resumed correctly.
214 TEST_F(PrerenderTrackerTest
, PrerenderThrottledRedirectResume
) {
215 const base::FilePath::CharType kRedirectPath
[] =
216 FILE_PATH_LITERAL("prerender/image-deferred.png");
218 test_contents()->Start();
222 net::TestURLRequestContext url_request_context
;
223 DeferredRedirectDelegate delegate
;
224 scoped_ptr
<net::URLRequest
> request(url_request_context
.CreateRequest(
225 net::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath
)),
226 net::DEFAULT_PRIORITY
,
229 content::ResourceRequestInfo::AllocateForTesting(
231 content::RESOURCE_TYPE_IMAGE
,
236 false, // is_main_frame
237 false, // parent_is_main_frame
238 true, // allow_download
241 // Install a prerender throttle.
242 PrerenderResourceThrottle
throttle(request
.get());
243 delegate
.SetThrottle(&throttle
);
245 // Start the request and wait for a redirect.
248 EXPECT_TRUE(delegate
.was_deferred());
249 // This calls WillRedirectRequestOnUI().
252 // Display the prerendered RenderView and wait for the throttle to
254 test_contents()->Use();
256 EXPECT_TRUE(delegate
.resume_called());
257 EXPECT_FALSE(delegate
.cancel_called());
260 // Checks that redirects in main frame loads are not deferred.
261 TEST_F(PrerenderTrackerTest
, PrerenderThrottledRedirectMainFrame
) {
262 const base::FilePath::CharType kRedirectPath
[] =
263 FILE_PATH_LITERAL("prerender/image-deferred.png");
265 test_contents()->Start();
269 net::TestURLRequestContext url_request_context
;
270 DeferredRedirectDelegate delegate
;
271 scoped_ptr
<net::URLRequest
> request(url_request_context
.CreateRequest(
272 net::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath
)),
273 net::DEFAULT_PRIORITY
,
276 content::ResourceRequestInfo::AllocateForTesting(
278 content::RESOURCE_TYPE_MAIN_FRAME
,
283 true, // is_main_frame
284 false, // parent_is_main_frame
285 true, // allow_download
288 // Install a prerender throttle.
289 PrerenderResourceThrottle
throttle(request
.get());
290 delegate
.SetThrottle(&throttle
);
292 // Start the request and wait for a redirect. This time, it should
296 // This calls WillRedirectRequestOnUI().
299 // Cleanup work so the prerender is gone.
300 test_contents()->Cancel();
304 // Checks that attempting to defer a synchronous request aborts the
306 TEST_F(PrerenderTrackerTest
, PrerenderThrottledRedirectSyncXHR
) {
307 const base::FilePath::CharType kRedirectPath
[] =
308 FILE_PATH_LITERAL("prerender/image-deferred.png");
310 test_contents()->Start();
314 net::TestURLRequestContext url_request_context
;
315 DeferredRedirectDelegate delegate
;
316 scoped_ptr
<net::URLRequest
> request(url_request_context
.CreateRequest(
317 net::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath
)),
318 net::DEFAULT_PRIORITY
,
321 content::ResourceRequestInfo::AllocateForTesting(
323 content::RESOURCE_TYPE_XHR
,
328 false, // is_main_frame
329 false, // parent_is_main_frame
330 true, // allow_download
333 // Install a prerender throttle.
334 PrerenderResourceThrottle
throttle(request
.get());
335 delegate
.SetThrottle(&throttle
);
337 // Start the request and wait for a redirect.
340 // This calls WillRedirectRequestOnUI().
343 // We should have cancelled the prerender.
344 EXPECT_EQ(FINAL_STATUS_BAD_DEFERRED_REDIRECT
,
345 test_contents()->final_status());
347 // Cleanup work so the prerender is gone.
348 test_contents()->Cancel();
352 } // namespace prerender