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 | |
172 // URLFetcherDelegate | |
173 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 173 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
174 }; | 174 }; |
175 | 175 |
176 // Version of URLFetcherTest that does a POST instead with empty upload body | 176 // Version of URLFetcherTest that does a POST instead with empty upload body |
177 class URLFetcherEmptyPostTest : public URLFetcherTest { | 177 class URLFetcherEmptyPostTest : public URLFetcherTest { |
178 public: | 178 public: |
179 // URLFetcherTest override. | 179 // URLFetcherTest: |
180 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 180 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
181 | |
182 // URLFetcherDelegate | |
183 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 181 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
184 }; | 182 }; |
185 | 183 |
186 // Version of URLFetcherTest that tests download progress reports. | 184 // Version of URLFetcherTest that tests download progress reports. |
187 class URLFetcherDownloadProgressTest : public URLFetcherTest { | 185 class URLFetcherDownloadProgressTest : public URLFetcherTest { |
188 public: | 186 public: |
189 // URLFetcherTest override. | 187 URLFetcherDownloadProgressTest() |
188 : previous_progress_(0), | |
189 previous_total_(0), | |
190 first_call_(true), | |
191 file_size_(0), | |
192 file_size_available_(false) { | |
193 } | |
194 | |
195 // URLFetcherTest: | |
190 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 196 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
197 virtual void OnURLFetchDownloadProgress(const URLFetcher* source, | |
198 int64 current, | |
199 int64 total) OVERRIDE; | |
191 | 200 |
192 // URLFetcherDelegate | |
193 virtual void OnURLFetchDownloadProgress(const URLFetcher* source, | |
194 int64 current, int64 total) OVERRIDE; | |
195 protected: | 201 protected: |
202 // Data returned by the previous callback. | |
196 int64 previous_progress_; | 203 int64 previous_progress_; |
197 int64 expected_total_; | 204 int64 previous_total_; |
205 // Indicates that |previous_progress_| and |previous_total_| are not yet set. | |
206 bool first_call_; | |
207 // File size fetched from the file system. | |
208 int64 file_size_; | |
209 bool file_size_available_; | |
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 | |
206 // URLFetcherDelegate | |
207 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 217 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
208 virtual void OnURLFetchDownloadProgress(const URLFetcher* source, | 218 virtual void OnURLFetchDownloadProgress(const URLFetcher* source, |
209 int64 current, int64 total) OVERRIDE; | 219 int64 current, |
220 int64 total) OVERRIDE; | |
210 protected: | 221 protected: |
211 bool cancelled_; | 222 bool cancelled_; |
212 }; | 223 }; |
213 | 224 |
214 // Version of URLFetcherTest that tests upload progress reports. | 225 // Version of URLFetcherTest that tests upload progress reports. |
215 class URLFetcherUploadProgressTest : public URLFetcherTest { | 226 class URLFetcherUploadProgressTest : public URLFetcherTest { |
216 public: | 227 public: |
217 virtual void CreateFetcher(const GURL& url); | 228 // URLFetcherTest: |
218 | 229 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
219 // URLFetcherDelegate | |
220 virtual void OnURLFetchUploadProgress(const URLFetcher* source, | 230 virtual void OnURLFetchUploadProgress(const URLFetcher* source, |
221 int64 current, int64 total); | 231 int64 current, |
232 int64 total) OVERRIDE; | |
222 protected: | 233 protected: |
223 int64 previous_progress_; | 234 int64 previous_progress_; |
224 std::string chunk_; | 235 std::string chunk_; |
225 int64 number_of_chunks_added_; | 236 int64 number_of_chunks_added_; |
226 }; | 237 }; |
227 | 238 |
228 // Version of URLFetcherTest that tests headers. | 239 // Version of URLFetcherTest that tests headers. |
229 class URLFetcherHeadersTest : public URLFetcherTest { | 240 class URLFetcherHeadersTest : public URLFetcherTest { |
230 public: | 241 public: |
231 // URLFetcherDelegate | 242 // URLFetcherTest: |
232 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 243 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
233 }; | 244 }; |
234 | 245 |
235 // Version of URLFetcherTest that tests SocketAddress. | 246 // Version of URLFetcherTest that tests SocketAddress. |
236 class URLFetcherSocketAddressTest : public URLFetcherTest { | 247 class URLFetcherSocketAddressTest : public URLFetcherTest { |
237 public: | 248 public: |
238 // URLFetcherDelegate | 249 // URLFetcherTest: |
239 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 250 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
240 protected: | 251 protected: |
241 std::string expected_host_; | 252 std::string expected_host_; |
242 uint16 expected_port_; | 253 uint16 expected_port_; |
243 }; | 254 }; |
244 | 255 |
245 // Version of URLFetcherTest that tests stopping on a redirect. | 256 // Version of URLFetcherTest that tests stopping on a redirect. |
246 class URLFetcherStopOnRedirectTest : public URLFetcherTest { | 257 class URLFetcherStopOnRedirectTest : public URLFetcherTest { |
247 public: | 258 public: |
248 URLFetcherStopOnRedirectTest(); | 259 URLFetcherStopOnRedirectTest(); |
249 virtual ~URLFetcherStopOnRedirectTest(); | 260 virtual ~URLFetcherStopOnRedirectTest(); |
250 | 261 |
251 // URLFetcherTest override. | 262 // URLFetcherTest: |
252 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 263 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
253 // URLFetcherDelegate | |
254 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 264 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
255 | 265 |
256 protected: | 266 protected: |
257 // The URL we should be redirected to. | 267 // The URL we should be redirected to. |
258 static const char* kRedirectTarget; | 268 static const char* kRedirectTarget; |
259 | 269 |
260 bool callback_called_; // Set to true in OnURLFetchComplete(). | 270 bool callback_called_; // Set to true in OnURLFetchComplete(). |
261 }; | 271 }; |
262 | 272 |
263 // Version of URLFetcherTest that tests overload protection. | 273 // Version of URLFetcherTest that tests overload protection. |
264 class URLFetcherProtectTest : public URLFetcherTest { | 274 class URLFetcherProtectTest : public URLFetcherTest { |
265 public: | 275 public: |
266 // URLFetcherTest override. | 276 // URLFetcherTest: |
267 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 277 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
268 // URLFetcherDelegate | |
269 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 278 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
270 private: | 279 private: |
271 Time start_time_; | 280 Time start_time_; |
272 }; | 281 }; |
273 | 282 |
274 // Version of URLFetcherTest that tests overload protection, when responses | 283 // Version of URLFetcherTest that tests overload protection, when responses |
275 // passed through. | 284 // passed through. |
276 class URLFetcherProtectTestPassedThrough : public URLFetcherTest { | 285 class URLFetcherProtectTestPassedThrough : public URLFetcherTest { |
277 public: | 286 public: |
278 // URLFetcherTest override. | 287 // URLFetcherTest: |
279 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 288 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
280 // URLFetcherDelegate | |
281 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 289 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
282 private: | 290 private: |
283 Time start_time_; | 291 Time start_time_; |
284 }; | 292 }; |
285 | 293 |
286 // Version of URLFetcherTest that tests bad HTTPS requests. | 294 // Version of URLFetcherTest that tests bad HTTPS requests. |
287 class URLFetcherBadHTTPSTest : public URLFetcherTest { | 295 class URLFetcherBadHTTPSTest : public URLFetcherTest { |
288 public: | 296 public: |
289 URLFetcherBadHTTPSTest(); | 297 URLFetcherBadHTTPSTest(); |
290 | 298 |
291 // URLFetcherDelegate | 299 // URLFetcherTest: |
292 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 300 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
293 | 301 |
294 private: | 302 private: |
295 FilePath cert_dir_; | 303 FilePath cert_dir_; |
296 }; | 304 }; |
297 | 305 |
298 // Version of URLFetcherTest that tests request cancellation on shutdown. | 306 // Version of URLFetcherTest that tests request cancellation on shutdown. |
299 class URLFetcherCancelTest : public URLFetcherTest { | 307 class URLFetcherCancelTest : public URLFetcherTest { |
300 public: | 308 public: |
301 // URLFetcherTest override. | 309 // URLFetcherTest: |
302 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 310 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
303 // URLFetcherDelegate | |
304 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 311 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
305 | 312 |
306 void CancelRequest(); | 313 void CancelRequest(); |
307 }; | 314 }; |
308 | 315 |
309 // Version of TestURLRequestContext that posts a Quit task to the IO | 316 // Version of TestURLRequestContext that posts a Quit task to the IO |
310 // thread once it is deleted. | 317 // thread once it is deleted. |
311 class CancelTestURLRequestContext : public ThrottlingTestURLRequestContext { | 318 class CancelTestURLRequestContext : public ThrottlingTestURLRequestContext { |
312 public: | 319 public: |
313 explicit CancelTestURLRequestContext() { | 320 explicit CancelTestURLRequestContext() { |
(...skipping 11 matching lines...) Expand all Loading... | |
325 : public TestURLRequestContextGetter { | 332 : public TestURLRequestContextGetter { |
326 public: | 333 public: |
327 CancelTestURLRequestContextGetter( | 334 CancelTestURLRequestContextGetter( |
328 base::MessageLoopProxy* io_message_loop_proxy, | 335 base::MessageLoopProxy* io_message_loop_proxy, |
329 const GURL& throttle_for_url) | 336 const GURL& throttle_for_url) |
330 : TestURLRequestContextGetter(io_message_loop_proxy), | 337 : TestURLRequestContextGetter(io_message_loop_proxy), |
331 io_message_loop_proxy_(io_message_loop_proxy), | 338 io_message_loop_proxy_(io_message_loop_proxy), |
332 context_created_(false, false), | 339 context_created_(false, false), |
333 throttle_for_url_(throttle_for_url) { | 340 throttle_for_url_(throttle_for_url) { |
334 } | 341 } |
342 | |
343 // TestURLRequestContextGetter: | |
335 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { | 344 virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { |
336 if (!context_.get()) { | 345 if (!context_.get()) { |
337 context_.reset(new CancelTestURLRequestContext()); | 346 context_.reset(new CancelTestURLRequestContext()); |
338 DCHECK(context_->throttler_manager()); | 347 DCHECK(context_->throttler_manager()); |
339 | 348 |
340 // Registers an entry for test url. The backoff time is calculated by: | 349 // Registers an entry for test url. The backoff time is calculated by: |
341 // new_backoff = 2.0 * old_backoff + 0 | 350 // new_backoff = 2.0 * old_backoff + 0 |
342 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. | 351 // The initial backoff is 2 seconds and maximum backoff is 4 seconds. |
343 // Maximum retries allowed is set to 2. | 352 // Maximum retries allowed is set to 2. |
344 scoped_refptr<URLRequestThrottlerEntry> entry( | 353 scoped_refptr<URLRequestThrottlerEntry> entry( |
345 new URLRequestThrottlerEntry( | 354 new URLRequestThrottlerEntry( |
346 context_->throttler_manager(), | 355 context_->throttler_manager(), |
347 "", 200, 3, 2000, 2.0, 0.0, 4000)); | 356 "", 200, 3, 2000, 2.0, 0.0, 4000)); |
348 context_->throttler_manager()->OverrideEntryForTests( | 357 context_->throttler_manager()->OverrideEntryForTests( |
349 throttle_for_url_, entry); | 358 throttle_for_url_, entry); |
350 | 359 |
351 context_created_.Signal(); | 360 context_created_.Signal(); |
352 } | 361 } |
353 return context_.get(); | 362 return context_.get(); |
354 } | 363 } |
364 | |
355 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { | 365 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { |
356 return io_message_loop_proxy_; | 366 return io_message_loop_proxy_; |
357 } | 367 } |
368 | |
358 void WaitForContextCreation() { | 369 void WaitForContextCreation() { |
359 context_created_.Wait(); | 370 context_created_.Wait(); |
360 } | 371 } |
361 | 372 |
362 protected: | 373 protected: |
363 virtual ~CancelTestURLRequestContextGetter() {} | 374 virtual ~CancelTestURLRequestContextGetter() {} |
364 | 375 |
365 private: | 376 private: |
366 scoped_ptr<TestURLRequestContext> context_; | 377 scoped_ptr<TestURLRequestContext> context_; |
367 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 378 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
368 base::WaitableEvent context_created_; | 379 base::WaitableEvent context_created_; |
369 GURL throttle_for_url_; | 380 GURL throttle_for_url_; |
370 }; | 381 }; |
371 | 382 |
372 // Version of URLFetcherTest that tests retying the same request twice. | 383 // Version of URLFetcherTest that tests retying the same request twice. |
373 class URLFetcherMultipleAttemptTest : public URLFetcherTest { | 384 class URLFetcherMultipleAttemptTest : public URLFetcherTest { |
374 public: | 385 public: |
375 // URLFetcherDelegate | 386 // URLFetcherTest: |
376 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 387 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
377 private: | 388 private: |
378 std::string data_; | 389 std::string data_; |
379 }; | 390 }; |
380 | 391 |
381 class URLFetcherFileTest : public URLFetcherTest { | 392 class URLFetcherFileTest : public URLFetcherTest { |
382 public: | 393 public: |
383 URLFetcherFileTest() : take_ownership_of_file_(false), | 394 URLFetcherFileTest() : take_ownership_of_file_(false), |
384 expected_file_error_(base::PLATFORM_FILE_OK) {} | 395 expected_file_error_(base::PLATFORM_FILE_OK) {} |
385 | 396 |
386 void CreateFetcherForFile(const GURL& url, const FilePath& file_path); | 397 void CreateFetcherForFile(const GURL& url, const FilePath& file_path); |
387 void CreateFetcherForTempFile(const GURL& url); | 398 void CreateFetcherForTempFile(const GURL& url); |
388 | 399 |
389 // URLFetcherDelegate | 400 // URLFetcherTest: |
390 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 401 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
391 | 402 |
392 protected: | 403 protected: |
393 FilePath expected_file_; | 404 FilePath expected_file_; |
394 FilePath file_path_; | 405 FilePath file_path_; |
395 | 406 |
396 // Set by the test. Used in OnURLFetchComplete() to decide if | 407 // Set by the test. Used in OnURLFetchComplete() to decide if |
397 // the URLFetcher should own the temp file, so that we can test | 408 // the URLFetcher should own the temp file, so that we can test |
398 // disowning prevents the file from being deleted. | 409 // disowning prevents the file from being deleted. |
399 bool take_ownership_of_file_; | 410 bool take_ownership_of_file_; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
437 | 448 |
438 CleanupAfterFetchComplete(); | 449 CleanupAfterFetchComplete(); |
439 // Do not call the super class method URLFetcherTest::OnURLFetchComplete, | 450 // Do not call the super class method URLFetcherTest::OnURLFetchComplete, |
440 // since it expects a non-empty response. | 451 // since it expects a non-empty response. |
441 } | 452 } |
442 | 453 |
443 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) { | 454 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) { |
444 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 455 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
445 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 456 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
446 io_message_loop_proxy(), request_context())); | 457 io_message_loop_proxy(), request_context())); |
447 previous_progress_ = 0; | |
448 fetcher_->Start(); | 458 fetcher_->Start(); |
449 } | 459 } |
450 | 460 |
451 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress( | 461 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress( |
452 const URLFetcher* source, int64 current, int64 total) { | 462 const URLFetcher* source, int64 current, int64 total) { |
453 // Increasing between 0 and total. | 463 // Progress has to be between 0 and total size. |
454 EXPECT_LE(0, current); | 464 EXPECT_LE(0, current); |
455 EXPECT_GE(total, current); | 465 EXPECT_GE(total, current); |
456 EXPECT_LE(previous_progress_, current); | 466 |
467 // Progress has to be non-decreasing, total size has to be constant. | |
468 if (!first_call_) { | |
469 EXPECT_LE(previous_progress_, current); | |
470 EXPECT_EQ(previous_total_, total); | |
471 } | |
472 | |
473 // File size is available only when local http server is used. | |
474 if (file_size_available_) | |
475 EXPECT_EQ(file_size_, total); | |
476 | |
457 previous_progress_ = current; | 477 previous_progress_ = current; |
458 EXPECT_EQ(expected_total_, total); | 478 previous_total_ = total; |
479 first_call_ = false; | |
459 } | 480 } |
460 | 481 |
461 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) { | 482 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) { |
462 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 483 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
463 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 484 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
464 io_message_loop_proxy(), request_context())); | 485 io_message_loop_proxy(), request_context())); |
465 cancelled_ = false; | 486 cancelled_ = false; |
466 fetcher_->Start(); | 487 fetcher_->Start(); |
467 } | 488 } |
468 | 489 |
(...skipping 378 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
847 } | 868 } |
848 | 869 |
849 TEST_F(URLFetcherDownloadProgressTest, Basic) { | 870 TEST_F(URLFetcherDownloadProgressTest, Basic) { |
850 TestServer test_server(TestServer::TYPE_HTTP, | 871 TestServer test_server(TestServer::TYPE_HTTP, |
851 TestServer::kLocalhost, | 872 TestServer::kLocalhost, |
852 FilePath(kDocRoot)); | 873 FilePath(kDocRoot)); |
853 ASSERT_TRUE(test_server.Start()); | 874 ASSERT_TRUE(test_server.Start()); |
854 | 875 |
855 // Get a file large enough to require more than one read into | 876 // Get a file large enough to require more than one read into |
856 // URLFetcher::Core's IOBuffer. | 877 // URLFetcher::Core's IOBuffer. |
857 static const char kFileToFetch[] = "animate1.gif"; | 878 static const char kFileToFetch[] = "animate1.gif"; |
wtc
2012/11/22 18:58:02
Since we hardcoded the file name here, I think it
| |
858 file_util::GetFileSize(test_server.document_root().AppendASCII(kFileToFetch), | 879 #if !defined(OS_ANDROID) |
859 &expected_total_); | 880 ASSERT_TRUE( |
881 file_util::GetFileSize( | |
882 test_server.document_root().AppendASCII(kFileToFetch), | |
883 &file_size)); | |
wtc
2012/11/22 18:58:02
I think you missed the trailing '_' for 'file_size
| |
884 file_size_available_ = true; | |
885 #endif | |
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 |