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

Side by Side Diff: chrome/browser/net/url_fetcher_unittest.cc

Issue 258008: Move initialization of ChromeURLRequestContexts to the IO thread. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: sync again, just in case Created 11 years, 2 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
« no previous file with comments | « chrome/browser/net/url_fetcher.cc ('k') | chrome/browser/net/url_request_context_getter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/thread.h" 5 #include "base/thread.h"
6 #include "base/time.h" 6 #include "base/time.h"
7 #include "base/timer.h" 7 #include "base/timer.h"
8 #include "chrome/browser/net/url_fetcher.h" 8 #include "chrome/browser/net/url_fetcher.h"
9 #include "chrome/browser/net/url_fetcher_protect.h" 9 #include "chrome/browser/net/url_fetcher_protect.h"
10 #include "chrome/browser/net/url_request_context_getter.h"
10 #include "chrome/common/chrome_plugin_lib.h" 11 #include "chrome/common/chrome_plugin_lib.h"
11 #include "net/http/http_response_headers.h" 12 #include "net/http/http_response_headers.h"
12 #include "net/socket/ssl_test_util.h" 13 #include "net/socket/ssl_test_util.h"
13 #include "net/url_request/url_request_unittest.h" 14 #include "net/url_request/url_request_unittest.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 16
16 using base::Time; 17 using base::Time;
17 using base::TimeDelta; 18 using base::TimeDelta;
18 19
19 namespace { 20 namespace {
20 21
21 const wchar_t kDocRoot[] = L"chrome/test/data"; 22 const wchar_t kDocRoot[] = L"chrome/test/data";
22 23
24 class TestURLRequestContextGetter : public URLRequestContextGetter {
25 public:
26 virtual URLRequestContext* GetURLRequestContext() {
27 if (!context_)
28 context_ = new TestURLRequestContext();
29 return context_;
30 }
31 private:
32 scoped_refptr<URLRequestContext> context_;
33 };
34
23 class URLFetcherTest : public testing::Test, public URLFetcher::Delegate { 35 class URLFetcherTest : public testing::Test, public URLFetcher::Delegate {
24 public: 36 public:
25 URLFetcherTest() : fetcher_(NULL) { } 37 URLFetcherTest() : fetcher_(NULL) { }
26 38
27 // Creates a URLFetcher, using the program's main thread to do IO. 39 // Creates a URLFetcher, using the program's main thread to do IO.
28 virtual void CreateFetcher(const GURL& url); 40 virtual void CreateFetcher(const GURL& url);
29 41
30 // URLFetcher::Delegate 42 // URLFetcher::Delegate
31 virtual void OnURLFetchComplete(const URLFetcher* source, 43 virtual void OnURLFetchComplete(const URLFetcher* source,
32 const GURL& url, 44 const GURL& url,
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 } 152 }
141 153
142 virtual ~CancelTestURLRequestContext() { 154 virtual ~CancelTestURLRequestContext() {
143 *destructor_called_ = true; 155 *destructor_called_ = true;
144 } 156 }
145 157
146 private: 158 private:
147 bool* destructor_called_; 159 bool* destructor_called_;
148 }; 160 };
149 161
162 class CancelTestURLRequestContextGetter : public URLRequestContextGetter {
163 public:
164 CancelTestURLRequestContextGetter(bool* destructor_called)
165 : destructor_called_(destructor_called) {
166 }
167
168 virtual URLRequestContext* GetURLRequestContext() {
169 if (!context_)
170 context_ = new CancelTestURLRequestContext(destructor_called_);
171 return context_;
172 }
173
174 private:
175 scoped_refptr<URLRequestContext> context_;
176 bool* destructor_called_;
177 };
178
150 // Wrapper that lets us call CreateFetcher() on a thread of our choice. We 179 // Wrapper that lets us call CreateFetcher() on a thread of our choice. We
151 // could make URLFetcherTest refcounted and use PostTask(FROM_HERE.. ) to call 180 // could make URLFetcherTest refcounted and use PostTask(FROM_HERE.. ) to call
152 // CreateFetcher() directly, but the ownership of the URLFetcherTest is a bit 181 // CreateFetcher() directly, but the ownership of the URLFetcherTest is a bit
153 // confusing in that case because GTest doesn't know about the refcounting. 182 // confusing in that case because GTest doesn't know about the refcounting.
154 // It's less confusing to just do it this way. 183 // It's less confusing to just do it this way.
155 class FetcherWrapperTask : public Task { 184 class FetcherWrapperTask : public Task {
156 public: 185 public:
157 FetcherWrapperTask(URLFetcherTest* test, const GURL& url) 186 FetcherWrapperTask(URLFetcherTest* test, const GURL& url)
158 : test_(test), url_(url) { } 187 : test_(test), url_(url) { }
159 virtual void Run() { 188 virtual void Run() {
160 test_->CreateFetcher(url_); 189 test_->CreateFetcher(url_);
161 }; 190 };
162 191
163 private: 192 private:
164 URLFetcherTest* test_; 193 URLFetcherTest* test_;
165 GURL url_; 194 GURL url_;
166 }; 195 };
167 196
168 void URLFetcherTest::CreateFetcher(const GURL& url) { 197 void URLFetcherTest::CreateFetcher(const GURL& url) {
169 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); 198 fetcher_ = new URLFetcher(url, URLFetcher::GET, this);
170 fetcher_->set_request_context(new TestURLRequestContext()); 199 fetcher_->set_request_context(new TestURLRequestContextGetter());
171 fetcher_->set_io_loop(&io_loop_); 200 fetcher_->set_io_loop(&io_loop_);
172 fetcher_->Start(); 201 fetcher_->Start();
173 } 202 }
174 203
175 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source, 204 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source,
176 const GURL& url, 205 const GURL& url,
177 const URLRequestStatus& status, 206 const URLRequestStatus& status,
178 int response_code, 207 int response_code,
179 const ResponseCookies& cookies, 208 const ResponseCookies& cookies,
180 const std::string& data) { 209 const std::string& data) {
181 EXPECT_TRUE(status.is_success()); 210 EXPECT_TRUE(status.is_success());
182 EXPECT_EQ(200, response_code); // HTTP OK 211 EXPECT_EQ(200, response_code); // HTTP OK
183 EXPECT_FALSE(data.empty()); 212 EXPECT_FALSE(data.empty());
184 213
185 delete fetcher_; // Have to delete this here and not in the destructor, 214 delete fetcher_; // Have to delete this here and not in the destructor,
186 // because the destructor won't necessarily run on the 215 // because the destructor won't necessarily run on the
187 // same thread that CreateFetcher() did. 216 // same thread that CreateFetcher() did.
188 217
189 io_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask()); 218 io_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask());
190 // If MessageLoop::current() != io_loop_, it will be shut down when the 219 // If MessageLoop::current() != io_loop_, it will be shut down when the
191 // main loop returns and this thread subsequently goes out of scope. 220 // main loop returns and this thread subsequently goes out of scope.
192 } 221 }
193 222
194 void URLFetcherPostTest::CreateFetcher(const GURL& url) { 223 void URLFetcherPostTest::CreateFetcher(const GURL& url) {
195 fetcher_ = new URLFetcher(url, URLFetcher::POST, this); 224 fetcher_ = new URLFetcher(url, URLFetcher::POST, this);
196 fetcher_->set_request_context(new TestURLRequestContext()); 225 fetcher_->set_request_context(new TestURLRequestContextGetter());
197 fetcher_->set_io_loop(&io_loop_); 226 fetcher_->set_io_loop(&io_loop_);
198 fetcher_->set_upload_data("application/x-www-form-urlencoded", 227 fetcher_->set_upload_data("application/x-www-form-urlencoded",
199 "bobsyeruncle"); 228 "bobsyeruncle");
200 fetcher_->Start(); 229 fetcher_->Start();
201 } 230 }
202 231
203 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source, 232 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source,
204 const GURL& url, 233 const GURL& url,
205 const URLRequestStatus& status, 234 const URLRequestStatus& status,
206 int response_code, 235 int response_code,
(...skipping 14 matching lines...) Expand all
221 std::string header; 250 std::string header;
222 EXPECT_TRUE(source->response_headers()->GetNormalizedHeader("cache-control", 251 EXPECT_TRUE(source->response_headers()->GetNormalizedHeader("cache-control",
223 &header)); 252 &header));
224 EXPECT_EQ("private", header); 253 EXPECT_EQ("private", header);
225 URLFetcherTest::OnURLFetchComplete(source, url, status, response_code, 254 URLFetcherTest::OnURLFetchComplete(source, url, status, response_code,
226 cookies, data); 255 cookies, data);
227 } 256 }
228 257
229 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { 258 void URLFetcherProtectTest::CreateFetcher(const GURL& url) {
230 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); 259 fetcher_ = new URLFetcher(url, URLFetcher::GET, this);
231 fetcher_->set_request_context(new TestURLRequestContext()); 260 fetcher_->set_request_context(new TestURLRequestContextGetter());
232 fetcher_->set_io_loop(&io_loop_); 261 fetcher_->set_io_loop(&io_loop_);
233 start_time_ = Time::Now(); 262 start_time_ = Time::Now();
234 fetcher_->Start(); 263 fetcher_->Start();
235 } 264 }
236 265
237 void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher* source, 266 void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher* source,
238 const GURL& url, 267 const GURL& url,
239 const URLRequestStatus& status, 268 const URLRequestStatus& status,
240 int response_code, 269 int response_code,
241 const ResponseCookies& cookies, 270 const ResponseCookies& cookies,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 EXPECT_TRUE(data.empty()); 322 EXPECT_TRUE(data.empty());
294 323
295 // The rest is the same as URLFetcherTest::OnURLFetchComplete. 324 // The rest is the same as URLFetcherTest::OnURLFetchComplete.
296 delete fetcher_; 325 delete fetcher_;
297 io_loop_.Quit(); 326 io_loop_.Quit();
298 } 327 }
299 328
300 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { 329 void URLFetcherCancelTest::CreateFetcher(const GURL& url) {
301 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); 330 fetcher_ = new URLFetcher(url, URLFetcher::GET, this);
302 fetcher_->set_request_context( 331 fetcher_->set_request_context(
303 new CancelTestURLRequestContext(&context_released_)); 332 new CancelTestURLRequestContextGetter(&context_released_));
304 fetcher_->set_io_loop(&io_loop_); 333 fetcher_->set_io_loop(&io_loop_);
305 fetcher_->Start(); 334 fetcher_->Start();
306 // Make sure we give the IO thread a chance to run. 335 // Make sure we give the IO thread a chance to run.
307 timer_.Start(TimeDelta::FromMilliseconds(300), this, 336 timer_.Start(TimeDelta::FromMilliseconds(300), this,
308 &URLFetcherCancelTest::CancelRequest); 337 &URLFetcherCancelTest::CancelRequest);
309 } 338 }
310 339
311 void URLFetcherCancelTest::OnURLFetchComplete(const URLFetcher* source, 340 void URLFetcherCancelTest::OnURLFetchComplete(const URLFetcher* source,
312 const GURL& url, 341 const GURL& url,
313 const URLRequestStatus& status, 342 const URLRequestStatus& status,
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
456 // message loop will be shut down automatically as the thread goes out of 485 // message loop will be shut down automatically as the thread goes out of
457 // scope. 486 // scope.
458 base::Thread t("URLFetcher test thread"); 487 base::Thread t("URLFetcher test thread");
459 ASSERT_TRUE(t.Start()); 488 ASSERT_TRUE(t.Start());
460 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url)); 489 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url));
461 490
462 MessageLoop::current()->Run(); 491 MessageLoop::current()->Run();
463 } 492 }
464 493
465 } // namespace. 494 } // namespace.
OLDNEW
« no previous file with comments | « chrome/browser/net/url_fetcher.cc ('k') | chrome/browser/net/url_request_context_getter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698