OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "content/common/net/url_fetcher_impl.h" | 5 #include "content/common/net/url_fetcher_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 : public TestURLRequestContextGetter { | 52 : public TestURLRequestContextGetter { |
53 public: | 53 public: |
54 ThrottlingTestURLRequestContextGetter( | 54 ThrottlingTestURLRequestContextGetter( |
55 base::MessageLoopProxy* io_message_loop_proxy, | 55 base::MessageLoopProxy* io_message_loop_proxy, |
56 TestURLRequestContext* request_context) | 56 TestURLRequestContext* request_context) |
57 : TestURLRequestContextGetter(io_message_loop_proxy), | 57 : TestURLRequestContextGetter(io_message_loop_proxy), |
58 context_(request_context) { | 58 context_(request_context) { |
59 } | 59 } |
60 | 60 |
61 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { | 61 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { |
62 return context_.get(); | 62 return context_; |
63 } | 63 } |
64 | 64 |
65 protected: | 65 protected: |
66 virtual ~ThrottlingTestURLRequestContextGetter() {} | 66 virtual ~ThrottlingTestURLRequestContextGetter() {} |
67 | 67 |
68 scoped_refptr<TestURLRequestContext> context_; | 68 TestURLRequestContext* const context_; |
69 }; | 69 }; |
70 | 70 |
71 } // namespace | 71 } // namespace |
72 | 72 |
73 class URLFetcherTest : public testing::Test, | 73 class URLFetcherTest : public testing::Test, |
74 public content::URLFetcherDelegate { | 74 public content::URLFetcherDelegate { |
75 public: | 75 public: |
76 URLFetcherTest() | 76 URLFetcherTest() |
77 : fetcher_(NULL), | 77 : fetcher_(NULL), |
78 context_(new ThrottlingTestURLRequestContext()) { | 78 context_(new ThrottlingTestURLRequestContext()) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 } | 115 } |
116 | 116 |
117 // URLFetcher is designed to run on the main UI thread, but in our tests | 117 // URLFetcher is designed to run on the main UI thread, but in our tests |
118 // we assume that the current thread is the IO thread where the URLFetcher | 118 // we assume that the current thread is the IO thread where the URLFetcher |
119 // dispatches its requests to. When we wish to simulate being used from | 119 // dispatches its requests to. When we wish to simulate being used from |
120 // a UI thread, we dispatch a worker thread to do so. | 120 // a UI thread, we dispatch a worker thread to do so. |
121 MessageLoopForIO io_loop_; | 121 MessageLoopForIO io_loop_; |
122 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 122 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
123 | 123 |
124 URLFetcherImpl* fetcher_; | 124 URLFetcherImpl* fetcher_; |
125 scoped_refptr<TestURLRequestContext> context_; | 125 const scoped_ptr<TestURLRequestContext> context_; |
126 }; | 126 }; |
127 | 127 |
128 void URLFetcherTest::CreateFetcher(const GURL& url) { | 128 void URLFetcherTest::CreateFetcher(const GURL& url) { |
129 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); | 129 fetcher_ = new URLFetcherImpl(url, content::URLFetcher::GET, this); |
130 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 130 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
131 io_message_loop_proxy(), request_context())); | 131 io_message_loop_proxy(), request_context())); |
132 fetcher_->Start(); | 132 fetcher_->Start(); |
133 } | 133 } |
134 | 134 |
135 void URLFetcherTest::OnURLFetchComplete(const content::URLFetcher* source) { | 135 void URLFetcherTest::OnURLFetchComplete(const content::URLFetcher* source) { |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 public: | 286 public: |
287 CancelTestURLRequestContextGetter( | 287 CancelTestURLRequestContextGetter( |
288 base::MessageLoopProxy* io_message_loop_proxy, | 288 base::MessageLoopProxy* io_message_loop_proxy, |
289 const GURL& throttle_for_url) | 289 const GURL& throttle_for_url) |
290 : TestURLRequestContextGetter(io_message_loop_proxy), | 290 : TestURLRequestContextGetter(io_message_loop_proxy), |
291 io_message_loop_proxy_(io_message_loop_proxy), | 291 io_message_loop_proxy_(io_message_loop_proxy), |
292 context_created_(false, false), | 292 context_created_(false, false), |
293 throttle_for_url_(throttle_for_url) { | 293 throttle_for_url_(throttle_for_url) { |
294 } | 294 } |
295 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { | 295 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { |
296 if (!context_) { | 296 if (!context_.get()) { |
297 context_ = new CancelTestURLRequestContext(); | 297 context_.reset(new CancelTestURLRequestContext()); |
298 DCHECK(context_->throttler_manager()); | 298 DCHECK(context_->throttler_manager()); |
299 | 299 |
300 // Registers an entry for test url. The backoff time is calculated by: | 300 // Registers an entry for test url. The backoff time is calculated by: |
301 // new_backoff = 2.0 * old_backoff + 0 | 301 // new_backoff = 2.0 * old_backoff + 0 |
302 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. | 302 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. |
303 // Maximum retries allowed is set to 2. | 303 // Maximum retries allowed is set to 2. |
304 scoped_refptr<net::URLRequestThrottlerEntry> entry( | 304 scoped_refptr<net::URLRequestThrottlerEntry> entry( |
305 new net::URLRequestThrottlerEntry( | 305 new net::URLRequestThrottlerEntry( |
306 context_->throttler_manager(), | 306 context_->throttler_manager(), |
307 "", 200, 3, 2000, 2.0, 0.0, 4000)); | 307 "", 200, 3, 2000, 2.0, 0.0, 4000)); |
308 context_->throttler_manager()->OverrideEntryForTests( | 308 context_->throttler_manager()->OverrideEntryForTests( |
309 throttle_for_url_, entry); | 309 throttle_for_url_, entry); |
310 | 310 |
311 context_created_.Signal(); | 311 context_created_.Signal(); |
312 } | 312 } |
313 return context_; | 313 return context_.get(); |
314 } | 314 } |
315 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { | 315 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { |
316 return io_message_loop_proxy_; | 316 return io_message_loop_proxy_; |
317 } | 317 } |
318 void WaitForContextCreation() { | 318 void WaitForContextCreation() { |
319 context_created_.Wait(); | 319 context_created_.Wait(); |
320 } | 320 } |
321 | 321 |
322 protected: | 322 protected: |
323 virtual ~CancelTestURLRequestContextGetter() {} | 323 virtual ~CancelTestURLRequestContextGetter() {} |
324 | 324 |
325 private: | 325 private: |
326 scoped_refptr<ThrottlingTestURLRequestContext> context_; | 326 scoped_ptr<TestURLRequestContext> context_; |
327 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 327 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
328 base::WaitableEvent context_created_; | 328 base::WaitableEvent context_created_; |
329 GURL throttle_for_url_; | 329 GURL throttle_for_url_; |
330 }; | 330 }; |
331 | 331 |
332 // Version of URLFetcherTest that tests retying the same request twice. | 332 // Version of URLFetcherTest that tests retying the same request twice. |
333 class URLFetcherMultipleAttemptTest : public URLFetcherTest { | 333 class URLFetcherMultipleAttemptTest : public URLFetcherTest { |
334 public: | 334 public: |
335 // content::URLFetcherDelegate | 335 // content::URLFetcherDelegate |
336 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; | 336 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
(...skipping 809 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1146 std::string(kTestServerFilePrefix) + kFileToFetch)); | 1146 std::string(kTestServerFilePrefix) + kFileToFetch)); |
1147 | 1147 |
1148 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1148 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1149 | 1149 |
1150 MessageLoop::current()->RunAllPending(); | 1150 MessageLoop::current()->RunAllPending(); |
1151 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1151 ASSERT_FALSE(file_util::PathExists(file_path_)) |
1152 << file_path_.value() << " not removed."; | 1152 << file_path_.value() << " not removed."; |
1153 } | 1153 } |
1154 | 1154 |
1155 } // namespace. | 1155 } // namespace. |
OLD | NEW |