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

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