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

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: Another merge with trunk Created 9 years, 11 months 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) 2011 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 base::Time FixedGetCurrentTime() {
63 return base::Time();
64 }
65
66 // HttpResponseHeaders expects the raw input for it's constructor
67 // to be a NUL ('\0') separated string for each line. This is a little
68 // difficult to do for string literals, so this helper function accepts
69 // newline-separated string literals and does the substitution. The
70 // returned object is expected to be deleted by the caller.
71 net::HttpResponseHeaders* CreateResponseHeaders(
72 const char* newline_separated_headers) {
73 std::string headers(newline_separated_headers);
74 std::string::iterator i = headers.begin();
75 std::string::iterator end = headers.end();
76 while (i != end) {
77 if (*i == '\n')
78 *i = '\0';
79 ++i;
80 }
81 return new net::HttpResponseHeaders(headers);
82 }
83
84 } // namespace
85
86 class PrerenderResourceHandlerTest : public testing::Test {
87 protected:
88 PrerenderResourceHandlerTest()
89 : prerender_duration_(base::TimeDelta::FromSeconds(10)),
90 mock_handler_(new MockResourceHandler()),
91 ALLOW_THIS_IN_INITIALIZER_LIST(
92 pre_handler_(new PrerenderResourceHandler(
93 mock_handler_,
94 NewCallback(
95 this,
96 &PrerenderResourceHandlerTest::SetLastHandledURL)))),
97 ui_thread_(BrowserThread::UI, &loop_),
98 default_url_("http://www.prerender.com") {
99 pre_handler_->set_prerender_duration(prerender_duration_);
100 pre_handler_->set_get_current_time_function(&FixedGetCurrentTime);
101 }
102
103 void SetLastHandledURL(const GURL& url) {
104 last_handled_url_ = url;
105 }
106
107 // Common logic shared by many of the tests
108 void StartPrerendering(const std::string& mime_type,
109 const char* headers) {
110 int request_id = 1;
111 bool defer = false;
112 EXPECT_TRUE(pre_handler_->OnWillStart(request_id, default_url_, &defer));
113 EXPECT_FALSE(defer);
114 scoped_refptr<ResourceResponse> response(new ResourceResponse);
115 response->response_head.request_time = FixedGetCurrentTime();
116 response->response_head.response_time = FixedGetCurrentTime();
117 response->response_head.mime_type = mime_type;
118 response->response_head.headers = CreateResponseHeaders(headers);
119 EXPECT_TRUE(last_handled_url_.is_empty());
120
121 // Start the response. If it is able to prerender, a task will
122 // be posted to loop_ (masquerading as the UI thread), and
123 // |SetLastHandledURL| will be called.
124 EXPECT_TRUE(pre_handler_->OnResponseStarted(request_id, response));
125 loop_.RunAllPending();
126 }
127
128 base::TimeDelta prerender_duration_;
129 scoped_refptr<MockResourceHandler> mock_handler_;
130 scoped_refptr<PrerenderResourceHandler> pre_handler_;
131 MessageLoop loop_;
132 BrowserThread ui_thread_;
133 GURL last_handled_url_;
134 GURL default_url_;
135 };
136
137 namespace {
138
139 TEST_F(PrerenderResourceHandlerTest, NoOp) {
140 }
141
142 // Tests that a valid HTML resource will correctly get diverted
143 // to the PrerenderManager.
144 TEST_F(PrerenderResourceHandlerTest, Prerender) {
145 StartPrerendering("text/html",
146 "HTTP/1.1 200 OK\n"
147 "cache-control: max-age=86400\n");
148 EXPECT_EQ(default_url_, last_handled_url_);
149 }
150
151 // Tests that a no-cache HTML resource will not get diverted
152 // to the PrerenderManager.
153 TEST_F(PrerenderResourceHandlerTest, PrerenderNoCache) {
154 StartPrerendering("text/html",
155 "HTTP/1.1 200 OK\n"
156 "cache-control: no-cache\n");
157 EXPECT_TRUE(last_handled_url_.is_empty());
158 }
159
160 // Tests that a cacheable HTML resource which needs to be revalidated
161 // shortly will not be prerendered.
162 TEST_F(PrerenderResourceHandlerTest, PrerenderShortMaxAge) {
163 StartPrerendering("text/html",
164 "HTTP/1.1 200 OK\n"
165 "cache-control: max-age=5\n");
166 EXPECT_TRUE(last_handled_url_.is_empty());
167 }
168
169 // Tests that a resource with the wrong MIME type (a GIF in this example)
170 // will not be diverted to the PrerenderManager.
171 TEST_F(PrerenderResourceHandlerTest, PrerenderWrongMimeType) {
172 StartPrerendering("image/gif",
173 "HTTP/1.1 200 OK\n"
174 "cache-control: max-age=86400\n");
175 EXPECT_TRUE(last_handled_url_.is_empty());
176 }
177
178 // Tests that a resource with a non-200 response will not be diverted
179 // to the PrerenderManager
180 TEST_F(PrerenderResourceHandlerTest, PrerenderBadResponseCode) {
181 StartPrerendering("text/html",
182 "HTTP/1.1 403 Forbidden\n"
183 "cache-control: max-age=86400\n");
184 EXPECT_TRUE(last_handled_url_.is_empty());
185 }
186
187 // Tests that the final request in a redirect chain will
188 // get diverted to the PrerenderManager.
189 TEST_F(PrerenderResourceHandlerTest, PrerenderRedirect) {
190 int request_id = 1;
191 GURL url_redirect("http://www.redirect.com");
192 bool defer = false;
193 EXPECT_TRUE(pre_handler_->OnWillStart(request_id, default_url_, &defer));
194 EXPECT_FALSE(defer);
195 EXPECT_TRUE(pre_handler_->OnRequestRedirected(request_id,
196 url_redirect,
197 NULL,
198 &defer));
199 EXPECT_FALSE(defer);
200 scoped_refptr<ResourceResponse> response(new ResourceResponse);
201 response->response_head.mime_type = "text/html";
202 response->response_head.request_time = FixedGetCurrentTime();
203 response->response_head.response_time = FixedGetCurrentTime();
204 response->response_head.headers = CreateResponseHeaders(
205 "HTTP/1.1 200 OK\n"
206 "cache-control: max-age=86400\n");
207 EXPECT_TRUE(pre_handler_->OnResponseStarted(request_id, response));
208 EXPECT_TRUE(last_handled_url_.is_empty());
209 loop_.RunAllPending();
210 EXPECT_EQ(url_redirect, last_handled_url_);
211 }
212
213 }
214
OLDNEW
« no previous file with comments | « chrome/browser/prerender/prerender_resource_handler.cc ('k') | chrome/browser/profiles/profile_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698