Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(434)

Side by Side Diff: chrome/browser/prerender/prerender_resource_handler_unittest.cc

Issue 5912001: Add PrerenderResourceHandler and hook it into the ResourceDispatcherHost.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Remove extra comment. Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2010 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.
4
5 #include "chrome/browser/prerender/prerender_resource_handler.h"
6 #include "chrome/common/resource_response.h"
7 #include "net/http/http_response_headers.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace {
11
12 class MockResourceHandler : public ResourceHandler {
13 public:
14 MockResourceHandler() {}
15
16 virtual bool OnUploadProgress(int request_id,
17 uint64 position,
18 uint64 size) {
19 return true;
20 }
21
22 virtual bool OnRequestRedirected(int request_id, const GURL& url,
23 ResourceResponse* response,
24 bool* defer) {
25 *defer = false;
26 return true;
27 }
28
29 virtual bool OnResponseStarted(int request_id,
30 ResourceResponse* response) {
31 return true;
32 }
33
34 virtual bool OnWillStart(int request_id, const GURL& url, bool* defer) {
35 *defer = false;
36 return true;
37 }
38
39 virtual bool OnWillRead(int request_id,
40 net::IOBuffer** buf,
41 int* buf_size,
42 int min_size) {
43 return true;
44 }
45
46 virtual bool OnReadCompleted(int request_id, int* bytes_read) {
47 return true;
48 }
49
50 virtual bool OnResponseCompleted(int request_id,
51 const URLRequestStatus& status,
52 const std::string& security_info) {
53 return true;
54 }
55
56 virtual void OnRequestClosed() {
57 }
58
59 virtual void OnDataDownloaded(int request_id, int bytes_downloaded) {}
60 };
61
62 } // namespace
63
64 class PrerenderResourceHandlerTest : public testing::Test {
65 protected:
66 PrerenderResourceHandlerTest()
67 : mock_handler_(new MockResourceHandler()),
68 ALLOW_THIS_IN_INITIALIZER_LIST(
69 pre_handler_(new PrerenderResourceHandler(
70 mock_handler_,
71 NewCallback(
72 this,
73 &PrerenderResourceHandlerTest::SetLastHandledURL)))),
74 ui_thread_(BrowserThread::UI, &loop_),
75 default_url_("http://www.prerender.com") {
76 }
77
78 void SetLastHandledURL(const GURL& url) {
79 last_handled_url_ = url;
80 }
81
82 // Common logic shared by many of the tests
83 void StartPrerendering(const std::string& mime_type,
84 const std::string& headers) {
85 int request_id = 1;
86 bool defer = false;
87 EXPECT_TRUE(pre_handler_->OnWillStart(request_id, default_url_, &defer));
88 EXPECT_FALSE(defer);
89 scoped_refptr<ResourceResponse> response(new ResourceResponse);
90 response->response_head.mime_type = mime_type;
91 response->response_head.headers = new net::HttpResponseHeaders(headers);
92 EXPECT_TRUE(last_handled_url_.is_empty());
93
94 // Start the response. If it is able to prerender, a task will
95 // be posted to loop_ (masquerading as the UI thread), and
96 // |SetLastHandledURL| will be called.
97 EXPECT_TRUE(pre_handler_->OnResponseStarted(request_id, response));
98 loop_.RunAllPending();
99 }
100
101 scoped_refptr<MockResourceHandler> mock_handler_;
102 scoped_refptr<PrerenderResourceHandler> pre_handler_;
103 MessageLoop loop_;
104 BrowserThread ui_thread_;
105 GURL last_handled_url_;
106 GURL default_url_;
107 };
108
109 namespace {
110
111 TEST_F(PrerenderResourceHandlerTest, NoOp) {
112 }
113
114 // Tests that a valid HTML resource will correctly get diverted
115 // to the PrerenderManager.
116 TEST_F(PrerenderResourceHandlerTest, Prerender) {
117 StartPrerendering("text/html", "HTTP/1.1 200 OK\r\n\r\n");
118 EXPECT_EQ(default_url_, last_handled_url_);
119 }
120
121 // Tests that a resource with the wrong MIME type (a GIF in this example)
122 // will not be diverted to the PrerenderManager.
123 TEST_F(PrerenderResourceHandlerTest, PrerenderWrongMimeType) {
124 StartPrerendering("image/gif", "HTTP/1.1 200 OK\r\n\r\n");
125 EXPECT_TRUE(last_handled_url_.is_empty());
126 }
127
128 // Tests that a resource with a non-200 response will not be diverted
129 // to the PrerenderManager
130 TEST_F(PrerenderResourceHandlerTest, PrerenderBadResponseCode) {
131 StartPrerendering("text/html", "HTTP/1.1 403 Forbidden\r\n\r\n");
132 EXPECT_TRUE(last_handled_url_.is_empty());
133 }
134
135 // Tests that the final request in a redirect chain will
136 // get diverted to the PrerenderManager.
137 TEST_F(PrerenderResourceHandlerTest, PrerenderRedirect) {
138 int request_id = 1;
139 GURL url_redirect("http://www.redirect.com");
140 bool defer = false;
141 EXPECT_TRUE(pre_handler_->OnWillStart(request_id, default_url_, &defer));
142 EXPECT_FALSE(defer);
143 EXPECT_TRUE(pre_handler_->OnRequestRedirected(request_id,
144 url_redirect,
145 NULL,
146 &defer));
147 EXPECT_FALSE(defer);
148 scoped_refptr<ResourceResponse> response(new ResourceResponse);
149 response->response_head.mime_type = "text/html";
150 response->response_head.headers = new net::HttpResponseHeaders(
151 "HTTP/1.1 200 OK\r\n\r\n");
152 EXPECT_TRUE(pre_handler_->OnResponseStarted(request_id, response));
153 EXPECT_TRUE(last_handled_url_.is_empty());
154 loop_.RunAllPending();
155 EXPECT_EQ(url_redirect, last_handled_url_);
156 }
157
158 }
willchan no longer on Chromium 2010/12/17 21:45:17 // namespace
159
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698