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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
179 // URLFetcherTest override. | 179 // URLFetcherTest override. |
180 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 180 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
181 | 181 |
182 // URLFetcherDelegate | 182 // URLFetcherDelegate |
183 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 183 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
184 }; | 184 }; |
185 | 185 |
186 // Version of URLFetcherTest that tests download progress reports. | 186 // Version of URLFetcherTest that tests download progress reports. |
187 class URLFetcherDownloadProgressTest : public URLFetcherTest { | 187 class URLFetcherDownloadProgressTest : public URLFetcherTest { |
188 public: | 188 public: |
189 // URLFetcherTest override. | 189 URLFetcherDownloadProgressTest() |
190 : previous_progress_(0), | |
191 previous_total_(0), | |
192 first_call_(true) { | |
193 } | |
194 | |
195 // URLFetcherTest: | |
190 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 196 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
191 | |
192 // URLFetcherDelegate | |
wtc
2012/11/21 00:48:01
We should either leave these comments unchanged, o
ppi
2012/11/22 09:57:18
Thanks, I have fixed the comments throughout the e
| |
193 virtual void OnURLFetchDownloadProgress(const URLFetcher* source, | 197 virtual void OnURLFetchDownloadProgress(const URLFetcher* source, |
194 int64 current, int64 total) OVERRIDE; | 198 int64 current, int64 total) OVERRIDE; |
199 | |
195 protected: | 200 protected: |
196 int64 previous_progress_; | 201 int64 previous_progress_; |
197 int64 expected_total_; | 202 int64 previous_total_; |
203 // Indicates that |previous_progress_| and |previous_total_| are not yet set. | |
204 bool first_call_; | |
198 }; | 205 }; |
199 | 206 |
200 /// Version of URLFetcherTest that tests progress reports at cancellation. | 207 // Version of URLFetcherTest that tests progress reports at cancellation. |
201 class URLFetcherDownloadProgressCancelTest : public URLFetcherTest { | 208 class URLFetcherDownloadProgressCancelTest : public URLFetcherTest { |
202 public: | 209 public: |
203 // URLFetcherTest override. | 210 // URLFetcherTest override. |
204 virtual void CreateFetcher(const GURL& url) OVERRIDE; | 211 virtual void CreateFetcher(const GURL& url) OVERRIDE; |
205 | 212 |
206 // URLFetcherDelegate | 213 // URLFetcherDelegate |
207 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; | 214 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
208 virtual void OnURLFetchDownloadProgress(const URLFetcher* source, | 215 virtual void OnURLFetchDownloadProgress(const URLFetcher* source, |
209 int64 current, int64 total) OVERRIDE; | 216 int64 current, int64 total) OVERRIDE; |
210 protected: | 217 protected: |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
437 | 444 |
438 CleanupAfterFetchComplete(); | 445 CleanupAfterFetchComplete(); |
439 // Do not call the super class method URLFetcherTest::OnURLFetchComplete, | 446 // Do not call the super class method URLFetcherTest::OnURLFetchComplete, |
440 // since it expects a non-empty response. | 447 // since it expects a non-empty response. |
441 } | 448 } |
442 | 449 |
443 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) { | 450 void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) { |
444 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 451 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
445 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 452 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
446 io_message_loop_proxy(), request_context())); | 453 io_message_loop_proxy(), request_context())); |
447 previous_progress_ = 0; | |
448 fetcher_->Start(); | 454 fetcher_->Start(); |
449 } | 455 } |
450 | 456 |
451 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress( | 457 void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress( |
452 const URLFetcher* source, int64 current, int64 total) { | 458 const URLFetcher* source, int64 current, int64 total) { |
453 // Increasing between 0 and total. | 459 // Progress has to be between 0 and total size. |
454 EXPECT_LE(0, current); | 460 EXPECT_LE(0, current); |
455 EXPECT_GE(total, current); | 461 EXPECT_GE(total, current); |
456 EXPECT_LE(previous_progress_, current); | 462 |
463 // Progress has to be non-decreasing, total size has to be constant. | |
464 if (!first_call_) { | |
465 EXPECT_LE(previous_progress_, current); | |
466 EXPECT_EQ(previous_total_, total); | |
467 } | |
468 | |
457 previous_progress_ = current; | 469 previous_progress_ = current; |
458 EXPECT_EQ(expected_total_, total); | 470 previous_total_ = total; |
471 first_call_ = false; | |
459 } | 472 } |
460 | 473 |
461 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) { | 474 void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) { |
462 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); | 475 fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
463 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( | 476 fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
464 io_message_loop_proxy(), request_context())); | 477 io_message_loop_proxy(), request_context())); |
465 cancelled_ = false; | 478 cancelled_ = false; |
466 fetcher_->Start(); | 479 fetcher_->Start(); |
467 } | 480 } |
468 | 481 |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
848 | 861 |
849 TEST_F(URLFetcherDownloadProgressTest, Basic) { | 862 TEST_F(URLFetcherDownloadProgressTest, Basic) { |
850 TestServer test_server(TestServer::TYPE_HTTP, | 863 TestServer test_server(TestServer::TYPE_HTTP, |
851 TestServer::kLocalhost, | 864 TestServer::kLocalhost, |
852 FilePath(kDocRoot)); | 865 FilePath(kDocRoot)); |
853 ASSERT_TRUE(test_server.Start()); | 866 ASSERT_TRUE(test_server.Start()); |
854 | 867 |
855 // Get a file large enough to require more than one read into | 868 // Get a file large enough to require more than one read into |
856 // URLFetcher::Core's IOBuffer. | 869 // URLFetcher::Core's IOBuffer. |
857 static const char kFileToFetch[] = "animate1.gif"; | 870 static const char kFileToFetch[] = "animate1.gif"; |
858 file_util::GetFileSize(test_server.document_root().AppendASCII(kFileToFetch), | |
859 &expected_total_); | |
wtc
2012/11/21 00:48:01
Removing this file_util::GetFileSize call weakens
ppi
2012/11/22 09:57:18
You are right, thanks. I didn't feel comfortable a
| |
860 CreateFetcher(test_server.GetURL( | 871 CreateFetcher(test_server.GetURL( |
861 std::string(kTestServerFilePrefix) + kFileToFetch)); | 872 std::string(kTestServerFilePrefix) + kFileToFetch)); |
862 | 873 |
863 MessageLoop::current()->Run(); | 874 MessageLoop::current()->Run(); |
864 } | 875 } |
865 | 876 |
866 TEST_F(URLFetcherDownloadProgressCancelTest, CancelWhileProgressReport) { | 877 TEST_F(URLFetcherDownloadProgressCancelTest, CancelWhileProgressReport) { |
867 TestServer test_server(TestServer::TYPE_HTTP, | 878 TestServer test_server(TestServer::TYPE_HTTP, |
868 TestServer::kLocalhost, | 879 TestServer::kLocalhost, |
869 FilePath(kDocRoot)); | 880 FilePath(kDocRoot)); |
(...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1245 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). | 1256 MessageLoop::current()->Run(); // OnURLFetchComplete() will Quit(). |
1246 | 1257 |
1247 MessageLoop::current()->RunAllPending(); | 1258 MessageLoop::current()->RunAllPending(); |
1248 ASSERT_FALSE(file_util::PathExists(file_path_)) | 1259 ASSERT_FALSE(file_util::PathExists(file_path_)) |
1249 << file_path_.value() << " not removed."; | 1260 << file_path_.value() << " not removed."; |
1250 } | 1261 } |
1251 | 1262 |
1252 } // namespace | 1263 } // namespace |
1253 | 1264 |
1254 } // namespace net | 1265 } // namespace net |
OLD | NEW |