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 "net/url_request/url_fetcher_impl.h" | 5 #include "net/url_request/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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
54 class ThrottlingTestURLRequestContextGetter | 54 class ThrottlingTestURLRequestContextGetter |
55 : public TestURLRequestContextGetter { | 55 : public TestURLRequestContextGetter { |
56 public: | 56 public: |
57 ThrottlingTestURLRequestContextGetter( | 57 ThrottlingTestURLRequestContextGetter( |
58 base::MessageLoopProxy* io_message_loop_proxy, | 58 base::MessageLoopProxy* io_message_loop_proxy, |
59 TestURLRequestContext* request_context) | 59 TestURLRequestContext* request_context) |
60 : TestURLRequestContextGetter(io_message_loop_proxy), | 60 : TestURLRequestContextGetter(io_message_loop_proxy), |
61 context_(request_context) { | 61 context_(request_context) { |
62 } | 62 } |
63 | 63 |
64 // TestURLRequestContextGetter: | |
64 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { | 65 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { |
65 return context_; | 66 return context_; |
66 } | 67 } |
67 | 68 |
68 protected: | 69 protected: |
69 virtual ~ThrottlingTestURLRequestContextGetter() {} | 70 virtual ~ThrottlingTestURLRequestContextGetter() {} |
70 | 71 |
71 TestURLRequestContext* const context_; | 72 TestURLRequestContext* const context_; |
72 }; | 73 }; |
73 | 74 |
74 } // namespace | 75 } // namespace |
75 | 76 |
76 class URLFetcherTest : public testing::Test, | 77 class URLFetcherTest : public testing::Test, |
77 public URLFetcherDelegate { | 78 public URLFetcherDelegate { |
78 public: | 79 public: |
79 URLFetcherTest() | 80 URLFetcherTest() |
80 : fetcher_(NULL), | 81 : fetcher_(NULL), |
81 context_(new ThrottlingTestURLRequestContext()) { | 82 context_(new ThrottlingTestURLRequestContext()) { |
82 } | 83 } |
83 | 84 |
84 static int GetNumFetcherCores() { | 85 static int GetNumFetcherCores() { |
85 return URLFetcherImpl::GetNumFetcherCores(); | 86 return URLFetcherImpl::GetNumFetcherCores(); |
86 } | 87 } |
87 | 88 |
88 // Creates a URLFetcher, using the program's main thread to do IO. | 89 // Creates a URLFetcher, using the program's main thread to do IO. |
89 virtual void CreateFetcher(const GURL& url); | 90 virtual void CreateFetcher(const GURL& url); |
90 | 91 |
91 // URLFetcherDelegate | 92 // URLFetcherDelegate: |
92 // Subclasses that override this should either call this function or | 93 // Subclasses that override this should either call this function or |
93 // CleanupAfterFetchComplete() at the end of their processing, depending on | 94 // CleanupAfterFetchComplete() at the end of their processing, depending on |
94 // whether they want to check for a non-empty HTTP 200 response or not. | 95 // whether they want to check for a non-empty HTTP 200 response or not. |
95 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 96 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
96 | 97 |
97 // Deletes |fetcher| and terminates the message loop. | 98 // Deletes |fetcher| and terminates the message loop. |
98 void CleanupAfterFetchComplete(); | 99 void CleanupAfterFetchComplete(); |
99 | 100 |
100 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { | 101 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { |
101 return io_message_loop_proxy_; | 102 return io_message_loop_proxy_; |
102 } | 103 } |
103 | 104 |
104 TestURLRequestContext* request_context() { | 105 TestURLRequestContext* request_context() { |
105 return context_.get(); | 106 return context_.get(); |
106 } | 107 } |
107 | 108 |
108 protected: | 109 protected: |
110 // testing::Test: | |
109 virtual void SetUp() OVERRIDE { | 111 virtual void SetUp() OVERRIDE { |
110 testing::Test::SetUp(); | 112 testing::Test::SetUp(); |
111 | 113 |
112 io_message_loop_proxy_ = base::MessageLoopProxy::current(); | 114 io_message_loop_proxy_ = base::MessageLoopProxy::current(); |
113 | 115 |
114 #if defined(USE_NSS) | 116 #if defined(USE_NSS) |
115 crypto::EnsureNSSInit(); | 117 crypto::EnsureNSSInit(); |
116 EnsureNSSHttpIOInit(); | 118 EnsureNSSHttpIOInit(); |
117 #endif | 119 #endif |
118 } | 120 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
159 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 161 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
160 // If the current message loop is not the IO loop, it will be shut down when | 162 // If the current message loop is not the IO loop, it will be shut down when |
161 // the main loop returns and this thread subsequently goes out of scope. | 163 // the main loop returns and this thread subsequently goes out of scope. |
162 } | 164 } |
163 | 165 |
164 namespace { | 166 namespace { |
165 | 167 |
166 // Version of URLFetcherTest that does a POST instead | 168 // Version of URLFetcherTest that does a POST instead |
167 class URLFetcherPostTest : public URLFetcherTest { | 169 class URLFetcherPostTest : public URLFetcherTest { |
168 public: | 170 public: |
169 // URLFetcherTest override. | 171 // URLFetcherTest: |
170 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 172 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
171 | 173 |
172 // URLFetcherDelegate | 174 // URLFetcherDelegate: |
173 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 175 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
174 }; | 176 }; |
175 | 177 |
176 // Version of URLFetcherTest that does a POST instead with empty upload body | 178 // Version of URLFetcherTest that does a POST instead with empty upload body |
177 class URLFetcherEmptyPostTest : public URLFetcherTest { | 179 class URLFetcherEmptyPostTest : public URLFetcherTest { |
178 public: | 180 public: |
179 // URLFetcherTest override. | 181 // URLFetcherTest: |
180 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 182 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
181 | 183 |
182 // URLFetcherDelegate | 184 // URLFetcherDelegate: |
183 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 185 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
184 }; | 186 }; |
185 | 187 |
186 // Version of URLFetcherTest that tests download progress reports. | 188 // Version of URLFetcherTest that tests download progress reports. |
187 class URLFetcherDownloadProgressTest : public URLFetcherTest { | 189 class URLFetcherDownloadProgressTest : public URLFetcherTest { |
188 public: | 190 public: |
189 // URLFetcherTest override. | 191 URLFetcherDownloadProgressTest() |
192 : previous_progress_(0), | |
193 expected_total_(0) { | |
194 } | |
195 | |
196 // URLFetcherTest: | |
190 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 197 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
191 | 198 |
192 // URLFetcherDelegate | 199 // URLFetcherDelegate: |
193 virtual void OnURLFetchDownloadProgress(const URLFetcher* source, | 200 virtual void OnURLFetchDownloadProgress(const URLFetcher* source, |
194 int64 current, int64 total) OVERRIDE; | 201 int64 current, |
202 int64 total) OVERRIDE; | |
203 | |
195 protected: | 204 protected: |
205 // Download progress returned by the previous callback. | |
196 int64 previous_progress_; | 206 int64 previous_progress_; |
207 // Size of the file being downloaded, known in advance (provided by each test | |
208 // case). | |
197 int64 expected_total_; | 209 int64 expected_total_; |
198 }; | 210 }; |
199 | 211 |
200 /// Version of URLFetcherTest that tests progress reports at cancellation. | 212 // Version of URLFetcherTest that tests progress reports at cancellation. |
201 class URLFetcherDownloadProgressCancelTest : public URLFetcherTest { | 213 class URLFetcherDownloadProgressCancelTest : public URLFetcherTest { |
202 public: | 214 public: |
203 // URLFetcherTest override. | 215 // URLFetcherTest: |
204 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 216 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
205 | 217 |
206 // URLFetcherDelegate | 218 // URLFetcherDelegate: |
207 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 219 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
208 virtual void OnURLFetchDownloadProgress(const URLFetcher* source, | 220 virtual void OnURLFetchDownloadProgress(const URLFetcher* source, |
209 int64 current, int64 total) OVERRIDE; | 221 int64 current, |
222 int64 total) OVERRIDE; | |
210 protected: | 223 protected: |
211 bool cancelled_; | 224 bool cancelled_; |
212 }; | 225 }; |
213 | 226 |
214 // Version of URLFetcherTest that tests upload progress reports. | 227 // Version of URLFetcherTest that tests upload progress reports. |
215 class URLFetcherUploadProgressTest : public URLFetcherTest { | 228 class URLFetcherUploadProgressTest : public URLFetcherTest { |
216 public: | 229 public: |
217 virtual void CreateFetcher(const GURL& url); | 230 // URLFetcherTest: |
231 virtual void CreateFetcher(const GURL& url) OVERRIDE; | |
218 | 232 |
219 // URLFetcherDelegate | 233 // URLFetcherDelegate: |
220 virtual void OnURLFetchUploadProgress(const URLFetcher* source, | 234 virtual void OnURLFetchUploadProgress(const URLFetcher* source, |
221 int64 current, int64 total); | 235 int64 current, |
236 int64 total) OVERRIDE; | |
222 protected: | 237 protected: |
223 int64 previous_progress_; | 238 int64 previous_progress_; |
224 std::string chunk_; | 239 std::string chunk_; |
225 int64 number_of_chunks_added_; | 240 int64 number_of_chunks_added_; |
226 }; | 241 }; |
227 | 242 |
228 // Version of URLFetcherTest that tests headers. | 243 // Version of URLFetcherTest that tests headers. |
229 class URLFetcherHeadersTest : public URLFetcherTest { | 244 class URLFetcherHeadersTest : public URLFetcherTest { |
230 public: | 245 public: |
231 // URLFetcherDelegate | 246 // URLFetcherDelegate: |
232 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 247 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
233 }; | 248 }; |
234 | 249 |
235 // Version of URLFetcherTest that tests SocketAddress. | 250 // Version of URLFetcherTest that tests SocketAddress. |
236 class URLFetcherSocketAddressTest : public URLFetcherTest { | 251 class URLFetcherSocketAddressTest : public URLFetcherTest { |
237 public: | 252 public: |
238 // URLFetcherDelegate | 253 // URLFetcherDelegate: |
239 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 254 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
240 protected: | 255 protected: |
241 std::string expected_host_; | 256 std::string expected_host_; |
242 uint16 expected_port_; | 257 uint16 expected_port_; |
243 }; | 258 }; |
244 | 259 |
245 // Version of URLFetcherTest that tests stopping on a redirect. | 260 // Version of URLFetcherTest that tests stopping on a redirect. |
246 class URLFetcherStopOnRedirectTest : public URLFetcherTest { | 261 class URLFetcherStopOnRedirectTest : public URLFetcherTest { |
247 public: | 262 public: |
248 URLFetcherStopOnRedirectTest(); | 263 URLFetcherStopOnRedirectTest(); |
249 virtual ~URLFetcherStopOnRedirectTest(); | 264 virtual ~URLFetcherStopOnRedirectTest(); |
250 | 265 |
251 // URLFetcherTest override. | 266 // URLFetcherTest: |
252 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 267 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
253 // URLFetcherDelegate | 268 |
269 // URLFetcherDelegate: | |
254 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 270 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
255 | 271 |
256 protected: | 272 protected: |
257 // The URL we should be redirected to. | 273 // The URL we should be redirected to. |
258 static const char* kRedirectTarget; | 274 static const char* kRedirectTarget; |
259 | 275 |
260 bool callback_called_; // Set to true in OnURLFetchComplete(). | 276 bool callback_called_; // Set to true in OnURLFetchComplete(). |
261 }; | 277 }; |
262 | 278 |
263 // Version of URLFetcherTest that tests overload protection. | 279 // Version of URLFetcherTest that tests overload protection. |
264 class URLFetcherProtectTest : public URLFetcherTest { | 280 class URLFetcherProtectTest : public URLFetcherTest { |
265 public: | 281 public: |
266 // URLFetcherTest override. | 282 // URLFetcherTest: |
267 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 283 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
268 // URLFetcherDelegate | 284 |
285 // URLFetcherDelegate: | |
269 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 286 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
270 private: | 287 private: |
271 Time start_time_; | 288 Time start_time_; |
272 }; | 289 }; |
273 | 290 |
274 // Version of URLFetcherTest that tests overload protection, when responses | 291 // Version of URLFetcherTest that tests overload protection, when responses |
275 // passed through. | 292 // passed through. |
276 class URLFetcherProtectTestPassedThrough : public URLFetcherTest { | 293 class URLFetcherProtectTestPassedThrough : public URLFetcherTest { |
277 public: | 294 public: |
278 // URLFetcherTest override. | 295 // URLFetcherTest: |
279 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 296 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
280 // URLFetcherDelegate | 297 |
298 // URLFetcherDelegate: | |
281 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 299 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
282 private: | 300 private: |
283 Time start_time_; | 301 Time start_time_; |
284 }; | 302 }; |
285 | 303 |
286 // Version of URLFetcherTest that tests bad HTTPS requests. | 304 // Version of URLFetcherTest that tests bad HTTPS requests. |
287 class URLFetcherBadHTTPSTest : public URLFetcherTest { | 305 class URLFetcherBadHTTPSTest : public URLFetcherTest { |
288 public: | 306 public: |
289 URLFetcherBadHTTPSTest(); | 307 URLFetcherBadHTTPSTest(); |
290 | 308 |
291 // URLFetcherDelegate | 309 // URLFetcherDelegate: |
292 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 310 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
293 | 311 |
294 private: | 312 private: |
295 FilePath cert_dir_; | 313 FilePath cert_dir_; |
296 }; | 314 }; |
297 | 315 |
298 // Version of URLFetcherTest that tests request cancellation on shutdown. | 316 // Version of URLFetcherTest that tests request cancellation on shutdown. |
299 class URLFetcherCancelTest : public URLFetcherTest { | 317 class URLFetcherCancelTest : public URLFetcherTest { |
300 public: | 318 public: |
301 // URLFetcherTest override. | 319 // URLFetcherTest: |
302 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 320 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
303 // URLFetcherDelegate | 321 |
322 // URLFetcherDelegate: | |
304 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 323 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
305 | 324 |
306 void CancelRequest(); | 325 void CancelRequest(); |
307 }; | 326 }; |
308 | 327 |
309 // Version of TestURLRequestContext that posts a Quit task to the IO | 328 // Version of TestURLRequestContext that posts a Quit task to the IO |
310 // thread once it is deleted. | 329 // thread once it is deleted. |
311 class CancelTestURLRequestContext : public ThrottlingTestURLRequestContext { | 330 class CancelTestURLRequestContext : public ThrottlingTestURLRequestContext { |
312 public: | 331 public: |
313 explicit CancelTestURLRequestContext() { | 332 explicit CancelTestURLRequestContext() { |
(...skipping 11 matching lines...) Expand all Loading... | |
325 : public TestURLRequestContextGetter { | 344 : public TestURLRequestContextGetter { |
326 public: | 345 public: |
327 CancelTestURLRequestContextGetter( | 346 CancelTestURLRequestContextGetter( |
328 base::MessageLoopProxy* io_message_loop_proxy, | 347 base::MessageLoopProxy* io_message_loop_proxy, |
329 const GURL& throttle_for_url) | 348 const GURL& throttle_for_url) |
330 : TestURLRequestContextGetter(io_message_loop_proxy), | 349 : TestURLRequestContextGetter(io_message_loop_proxy), |
331 io_message_loop_proxy_(io_message_loop_proxy), | 350 io_message_loop_proxy_(io_message_loop_proxy), |
332 context_created_(false, false), | 351 context_created_(false, false), |
333 throttle_for_url_(throttle_for_url) { | 352 throttle_for_url_(throttle_for_url) { |
334 } | 353 } |
354 | |
355 // TestURLRequestContextGetter: | |
335 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { | 356 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { |
336 if (!context_.get()) { | 357 if (!context_.get()) { |
337 context_.reset(new CancelTestURLRequestContext()); | 358 context_.reset(new CancelTestURLRequestContext()); |
338 DCHECK(context_->throttler_manager()); | 359 DCHECK(context_->throttler_manager()); |
339 | 360 |
340 // Registers an entry for test url. The backoff time is calculated by: | 361 // Registers an entry for test url. The backoff time is calculated by: |
341 // new_backoff = 2.0 * old_backoff + 0 | 362 // new_backoff = 2.0 * old_backoff + 0 |
342 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. | 363 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. |
343 // Maximum retries allowed is set to 2. | 364 // Maximum retries allowed is set to 2. |
344 scoped_refptr<URLRequestThrottlerEntry> entry( | 365 scoped_refptr<URLRequestThrottlerEntry> entry( |
345 new URLRequestThrottlerEntry( | 366 new URLRequestThrottlerEntry( |
346 context_->throttler_manager(), | 367 context_->throttler_manager(), |
347 "", 200, 3, 2000, 2.0, 0.0, 4000)); | 368 "", 200, 3, 2000, 2.0, 0.0, 4000)); |
348 context_->throttler_manager()->OverrideEntryForTests( | 369 context_->throttler_manager()->OverrideEntryForTests( |
349 throttle_for_url_, entry); | 370 throttle_for_url_, entry); |
350 | 371 |
351 context_created_.Signal(); | 372 context_created_.Signal(); |
352 } | 373 } |
353 return context_.get(); | 374 return context_.get(); |
354 } | 375 } |
376 | |
355 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { | 377 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { |
356 return io_message_loop_proxy_; | 378 return io_message_loop_proxy_; |
357 } | 379 } |
380 | |
358 void WaitForContextCreation() { | 381 void WaitForContextCreation() { |
359 context_created_.Wait(); | 382 context_created_.Wait(); |
360 } | 383 } |
361 | 384 |
362 protected: | 385 protected: |
363 virtual ~CancelTestURLRequestContextGetter() {} | 386 virtual ~CancelTestURLRequestContextGetter() {} |
364 | 387 |
365 private: | 388 private: |
366 scoped_ptr<TestURLRequestContext> context_; | 389 scoped_ptr<TestURLRequestContext> context_; |
367 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 390 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
368 base::WaitableEvent context_created_; | 391 base::WaitableEvent context_created_; |
369 GURL throttle_for_url_; | 392 GURL throttle_for_url_; |
370 }; | 393 }; |
371 | 394 |
372 // Version of URLFetcherTest that tests retying the same request twice. | 395 // Version of URLFetcherTest that tests retying the same request twice. |
373 class URLFetcherMultipleAttemptTest : public URLFetcherTest { | 396 class URLFetcherMultipleAttemptTest : public URLFetcherTest { |
374 public: | 397 public: |
375 // URLFetcherDelegate | 398 // URLFetcherDelegate: |
376 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 399 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
377 private: | 400 private: |
378 std::string data_; | 401 std::string data_; |
379 }; | 402 }; |
380 | 403 |
381 class URLFetcherFileTest : public URLFetcherTest { | 404 class URLFetcherFileTest : public URLFetcherTest { |
382 public: | 405 public: |
383 URLFetcherFileTest() : take_ownership_of_file_(false), | 406 URLFetcherFileTest() : take_ownership_of_file_(false), |
384 expected_file_error_(base::PLATFORM_FILE_OK) {} | 407 expected_file_error_(base::PLATFORM_FILE_OK) {} |
385 | 408 |
386 void CreateFetcherForFile(const GURL& url, const FilePath& file_path); | 409 void CreateFetcherForFile(const GURL& url, const FilePath& file_path); |
387 void CreateFetcherForTempFile(const GURL& url); | 410 void CreateFetcherForTempFile(const GURL& url); |
388 | 411 |
389 // URLFetcherDelegate | 412 // URLFetcherDelegate: |
390 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 413 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
391 | 414 |
392 protected: | 415 protected: |
393 FilePath expected_file_; | 416 FilePath expected_file_; |
394 FilePath file_path_; | 417 FilePath file_path_; |
395 | 418 |
396 // Set by the test. Used in OnURLFetchComplete() to decide if | 419 // Set by the test. Used in OnURLFetchComplete() to decide if |
397 // the URLFetcher should own the temp file, so that we can test | 420 // the URLFetcher should own the temp file, so that we can test |
398 // disowning prevents the file from being deleted. | 421 // disowning prevents the file from being deleted. |
399 bool take_ownership_of_file_; | 422 bool take_ownership_of_file_; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
437 | 460 |
438 CleanupAfterFetchComplete(); | 461 CleanupAfterFetchComplete(); |
439 // Do not call the super class method URLFetcherTest::OnURLFetchComplete, | 462 // Do not call the super class method URLFetcherTest::OnURLFetchComplete, |
440 // since it expects a non-empty response. | 463 // since it expects a non-empty response. |
441 } | 464 } |
442 | 465 |
443 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) { | 466 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) { |
444 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 467 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
445 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 468 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
446 io_message_loop_proxy(), request_context())); | 469 io_message_loop_proxy(), request_context())); |
447 previous_progress_ = 0; | |
448 fetcher_->Start(); | 470 fetcher_->Start(); |
449 } | 471 } |
450 | 472 |
451 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress( | 473 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress( |
452 const URLFetcher* source, int64 current, int64 total) { | 474 const URLFetcher* source, int64 progress, int64 total) { |
wtc
2012/11/29 19:35:25
I agree that |progress| seems more informative tha
| |
453 // Increasing between 0 and total. | 475 // Increasing between 0 and total. |
454 EXPECT_LE(0, current); | 476 EXPECT_LE(0, progress); |
455 EXPECT_GE(total, current); | 477 EXPECT_GE(total, progress); |
456 EXPECT_LE(previous_progress_, current); | 478 EXPECT_LE(previous_progress_, progress); |
457 previous_progress_ = current; | |
458 EXPECT_EQ(expected_total_, total); | 479 EXPECT_EQ(expected_total_, total); |
480 previous_progress_ = progress; | |
459 } | 481 } |
460 | 482 |
461 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) { | 483 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) { |
462 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 484 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
463 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 485 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
464 io_message_loop_proxy(), request_context())); | 486 io_message_loop_proxy(), request_context())); |
465 cancelled_ = false; | 487 cancelled_ = false; |
466 fetcher_->Start(); | 488 fetcher_->Start(); |
467 } | 489 } |
468 | 490 |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
848 | 870 |
849 TEST_F(URLFetcherDownloadProgressTest, Basic) { | 871 TEST_F(URLFetcherDownloadProgressTest, Basic) { |
850 TestServer test_server(TestServer::TYPE_HTTP, | 872 TestServer test_server(TestServer::TYPE_HTTP, |
851 TestServer::kLocalhost, | 873 TestServer::kLocalhost, |
852 FilePath(kDocRoot)); | 874 FilePath(kDocRoot)); |
853 ASSERT_TRUE(test_server.Start()); | 875 ASSERT_TRUE(test_server.Start()); |
854 | 876 |
855 // Get a file large enough to require more than one read into | 877 // Get a file large enough to require more than one read into |
856 // URLFetcher::Core's IOBuffer. | 878 // URLFetcher::Core's IOBuffer. |
857 static const char kFileToFetch[] = "animate1.gif"; | 879 static const char kFileToFetch[] = "animate1.gif"; |
858 file_util::GetFileSize(test_server.document_root().AppendASCII(kFileToFetch), | 880 // Hardcoded file size - it cannot be easily fetched when a remote http server |
859 &expected_total_); | 881 // is used for testing. |
882 static const int64 kFileSize = 19021; | |
883 | |
884 expected_total_ = kFileSize; | |
885 | |
860 CreateFetcher(test_server.GetURL( | 886 CreateFetcher(test_server.GetURL( |
861 std::string(kTestServerFilePrefix) + kFileToFetch)); | 887 std::string(kTestServerFilePrefix) + kFileToFetch)); |
862 | 888 |
863 MessageLoop::current()->Run(); | 889 MessageLoop::current()->Run(); |
864 } | 890 } |
865 | 891 |
866 TEST_F(URLFetcherDownloadProgressCancelTest, CancelWhileProgressReport) { | 892 TEST_F(URLFetcherDownloadProgressCancelTest, CancelWhileProgressReport) { |
867 TestServer test_server(TestServer::TYPE_HTTP, | 893 TestServer test_server(TestServer::TYPE_HTTP, |
868 TestServer::kLocalhost, | 894 TestServer::kLocalhost, |
869 FilePath(kDocRoot)); | 895 FilePath(kDocRoot)); |
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1245 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1271 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1246 | 1272 |
1247 MessageLoop::current()->RunUntilIdle(); | 1273 MessageLoop::current()->RunUntilIdle(); |
1248 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1274 ASSERT_FALSE(file_util::PathExists(file_path_)) |
1249 << file_path_.value() << " not removed."; | 1275 << file_path_.value() << " not removed."; |
1250 } | 1276 } |
1251 | 1277 |
1252 } // namespace | 1278 } // namespace |
1253 | 1279 |
1254 } // namespace net | 1280 } // namespace net |
OLD | NEW |