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 virtual ~TestPrerenderContents() {
50 if (final_status() == FINAL_STATUS_MAX
)
51 SetFinalStatus(FINAL_STATUS_USED
);
52 PrerenderResourceThrottle::OverridePrerenderContentsForTesting(NULL
);
55 virtual bool GetChildId(int* child_id
) const override
{
56 *child_id
= child_id_
;
60 virtual 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 virtual void MoveEntryToPendingDelete(PrerenderContents
* entry
,
93 FinalStatus final_status
) override
{
97 class DeferredRedirectDelegate
: public net::URLRequest::Delegate
,
98 public content::ResourceController
{
100 DeferredRedirectDelegate()
102 was_deferred_(false),
103 cancel_called_(false),
104 resume_called_(false) {
107 void SetThrottle(PrerenderResourceThrottle
* throttle
) {
108 throttle_
= throttle
;
109 throttle_
->set_controller_for_testing(this);
113 run_loop_
.reset(new base::RunLoop());
117 bool was_deferred() const { return was_deferred_
; }
118 bool cancel_called() const { return cancel_called_
; }
119 bool resume_called() const { return resume_called_
; }
121 // net::URLRequest::Delegate implementation:
122 virtual void OnReceivedRedirect(net::URLRequest
* request
,
123 const net::RedirectInfo
& redirect_info
,
124 bool* defer_redirect
) override
{
125 // Defer the redirect either way.
126 *defer_redirect
= true;
128 // Find out what the throttle would have done.
129 throttle_
->WillRedirectRequest(redirect_info
.new_url
, &was_deferred_
);
132 virtual void OnResponseStarted(net::URLRequest
* request
) override
{ }
133 virtual void OnReadCompleted(net::URLRequest
* request
,
134 int bytes_read
) override
{
137 // content::ResourceController implementation:
138 virtual void Cancel() override
{
139 EXPECT_FALSE(cancel_called_
);
140 EXPECT_FALSE(resume_called_
);
142 cancel_called_
= true;
145 virtual void CancelAndIgnore() override
{ Cancel(); }
146 virtual void CancelWithError(int error_code
) override
{ Cancel(); }
147 virtual void Resume() override
{
148 EXPECT_TRUE(was_deferred_
);
149 EXPECT_FALSE(cancel_called_
);
150 EXPECT_FALSE(resume_called_
);
152 resume_called_
= true;
157 scoped_ptr
<base::RunLoop
> run_loop_
;
158 PrerenderResourceThrottle
* throttle_
;
163 DISALLOW_COPY_AND_ASSIGN(DeferredRedirectDelegate
);
168 class PrerenderTrackerTest
: public testing::Test
{
170 static const int kDefaultChildId
= 0;
171 static const int kDefaultRouteId
= 100;
173 PrerenderTrackerTest() :
174 ui_thread_(BrowserThread::UI
, &message_loop_
),
175 io_thread_(BrowserThread::IO
, &message_loop_
),
176 prerender_manager_(prerender_tracker()),
177 test_contents_(&prerender_manager_
, kDefaultChildId
, kDefaultRouteId
) {
178 chrome_browser_net::SetUrlRequestMocksEnabled(true);
181 virtual ~PrerenderTrackerTest() {
182 chrome_browser_net::SetUrlRequestMocksEnabled(false);
184 // Cleanup work so the file IO tasks from URLRequestMockHTTPJob
186 content::BrowserThread::GetBlockingPool()->FlushForTesting();
190 PrerenderTracker
* prerender_tracker() {
191 return g_browser_process
->prerender_tracker();
194 TestPrerenderManager
* prerender_manager() {
195 return &prerender_manager_
;
198 TestPrerenderContents
* test_contents() {
199 return &test_contents_
;
202 // Runs any tasks queued on either thread.
204 message_loop_
.RunUntilIdle();
208 base::MessageLoopForIO message_loop_
;
209 content::TestBrowserThread ui_thread_
;
210 content::TestBrowserThread io_thread_
;
212 TestPrerenderManager prerender_manager_
;
213 TestPrerenderContents test_contents_
;
216 // Checks that deferred redirects are throttled and resumed correctly.
217 TEST_F(PrerenderTrackerTest
, PrerenderThrottledRedirectResume
) {
218 const base::FilePath::CharType kRedirectPath
[] =
219 FILE_PATH_LITERAL("prerender/image-deferred.png");
221 test_contents()->Start();
225 net::TestURLRequestContext url_request_context
;
226 DeferredRedirectDelegate delegate
;
227 scoped_ptr
<net::URLRequest
> request(url_request_context
.CreateRequest(
228 net::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath
)),
229 net::DEFAULT_PRIORITY
,
232 content::ResourceRequestInfo::AllocateForTesting(request
.get(),
233 content::RESOURCE_TYPE_IMAGE
,
240 // Install a prerender throttle.
241 PrerenderResourceThrottle
throttle(request
.get());
242 delegate
.SetThrottle(&throttle
);
244 // Start the request and wait for a redirect.
247 EXPECT_TRUE(delegate
.was_deferred());
248 // This calls WillRedirectRequestOnUI().
251 // Display the prerendered RenderView and wait for the throttle to
253 test_contents()->Use();
255 EXPECT_TRUE(delegate
.resume_called());
256 EXPECT_FALSE(delegate
.cancel_called());
259 // Checks that redirects in main frame loads are not deferred.
260 TEST_F(PrerenderTrackerTest
, PrerenderThrottledRedirectMainFrame
) {
261 const base::FilePath::CharType kRedirectPath
[] =
262 FILE_PATH_LITERAL("prerender/image-deferred.png");
264 test_contents()->Start();
268 net::TestURLRequestContext url_request_context
;
269 DeferredRedirectDelegate delegate
;
270 scoped_ptr
<net::URLRequest
> request(url_request_context
.CreateRequest(
271 net::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath
)),
272 net::DEFAULT_PRIORITY
,
275 content::ResourceRequestInfo::AllocateForTesting(
277 content::RESOURCE_TYPE_MAIN_FRAME
,
284 // Install a prerender throttle.
285 PrerenderResourceThrottle
throttle(request
.get());
286 delegate
.SetThrottle(&throttle
);
288 // Start the request and wait for a redirect. This time, it should
292 // This calls WillRedirectRequestOnUI().
295 // Cleanup work so the prerender is gone.
296 test_contents()->Cancel();
300 // Checks that attempting to defer a synchronous request aborts the
302 TEST_F(PrerenderTrackerTest
, PrerenderThrottledRedirectSyncXHR
) {
303 const base::FilePath::CharType kRedirectPath
[] =
304 FILE_PATH_LITERAL("prerender/image-deferred.png");
306 test_contents()->Start();
310 net::TestURLRequestContext url_request_context
;
311 DeferredRedirectDelegate delegate
;
312 scoped_ptr
<net::URLRequest
> request(url_request_context
.CreateRequest(
313 net::URLRequestMockHTTPJob::GetMockUrl(base::FilePath(kRedirectPath
)),
314 net::DEFAULT_PRIORITY
,
317 content::ResourceRequestInfo::AllocateForTesting(request
.get(),
318 content::RESOURCE_TYPE_XHR
,
325 // Install a prerender throttle.
326 PrerenderResourceThrottle
throttle(request
.get());
327 delegate
.SetThrottle(&throttle
);
329 // Start the request and wait for a redirect.
332 // This calls WillRedirectRequestOnUI().
335 // We should have cancelled the prerender.
336 EXPECT_EQ(FINAL_STATUS_BAD_DEFERRED_REDIRECT
,
337 test_contents()->final_status());
339 // Cleanup work so the prerender is gone.
340 test_contents()->Cancel();
344 } // namespace prerender