Chromium Code Reviews| Index: net/url_request/url_fetcher_impl_unittest.cc |
| diff --git a/net/url_request/url_fetcher_impl_unittest.cc b/net/url_request/url_fetcher_impl_unittest.cc |
| index f3f06595321c451b3eca7a5f052896f5b2dbcddd..bce48d3439dee7911e78d989d517b1051bc58d9b 100644 |
| --- a/net/url_request/url_fetcher_impl_unittest.cc |
| +++ b/net/url_request/url_fetcher_impl_unittest.cc |
| @@ -61,6 +61,7 @@ class ThrottlingTestURLRequestContextGetter |
| context_(request_context) { |
| } |
| + // TestURLRequestContextGetter: |
| virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { |
| return context_; |
| } |
| @@ -88,7 +89,7 @@ class URLFetcherTest : public testing::Test, |
| // Creates a URLFetcher, using the program's main thread to do IO. |
| virtual void CreateFetcher(const GURL& url); |
| - // URLFetcherDelegate |
| + // URLFetcherDelegate: |
| // Subclasses that override this should either call this function or |
| // CleanupAfterFetchComplete() at the end of their processing, depending on |
| // whether they want to check for a non-empty HTTP 200 response or not. |
| @@ -106,6 +107,7 @@ class URLFetcherTest : public testing::Test, |
| } |
| protected: |
| + // testing::Test: |
| virtual void SetUp() OVERRIDE { |
| testing::Test::SetUp(); |
| @@ -166,47 +168,56 @@ namespace { |
| // Version of URLFetcherTest that does a POST instead |
| class URLFetcherPostTest : public URLFetcherTest { |
| public: |
| - // URLFetcherTest override. |
| + // URLFetcherTest: |
| virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| - |
| - // URLFetcherDelegate |
| virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| }; |
| // Version of URLFetcherTest that does a POST instead with empty upload body |
| class URLFetcherEmptyPostTest : public URLFetcherTest { |
| public: |
| - // URLFetcherTest override. |
| + // URLFetcherTest: |
| virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| - |
| - // URLFetcherDelegate |
| virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| }; |
| // Version of URLFetcherTest that tests download progress reports. |
| class URLFetcherDownloadProgressTest : public URLFetcherTest { |
| public: |
| - // URLFetcherTest override. |
| - virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| + URLFetcherDownloadProgressTest() |
| + : previous_progress_(0), |
| + previous_total_(0), |
| + first_call_(true), |
| + file_size_(0), |
| + file_size_available_(false) { |
| + } |
| - // URLFetcherDelegate |
| + // URLFetcherTest: |
| + virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| virtual void OnURLFetchDownloadProgress(const URLFetcher* source, |
| - int64 current, int64 total) OVERRIDE; |
| + int64 current, |
| + int64 total) OVERRIDE; |
| + |
| protected: |
| + // Data returned by the previous callback. |
| int64 previous_progress_; |
| - int64 expected_total_; |
| + int64 previous_total_; |
| + // Indicates that |previous_progress_| and |previous_total_| are not yet set. |
| + bool first_call_; |
| + // File size fetched from the file system. |
| + int64 file_size_; |
| + bool file_size_available_; |
| }; |
| -/// Version of URLFetcherTest that tests progress reports at cancellation. |
| +// Version of URLFetcherTest that tests progress reports at cancellation. |
| class URLFetcherDownloadProgressCancelTest : public URLFetcherTest { |
| public: |
| - // URLFetcherTest override. |
| + // URLFetcherTest: |
| virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| - |
| - // URLFetcherDelegate |
| virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| virtual void OnURLFetchDownloadProgress(const URLFetcher* source, |
| - int64 current, int64 total) OVERRIDE; |
| + int64 current, |
| + int64 total) OVERRIDE; |
| protected: |
| bool cancelled_; |
| }; |
| @@ -214,11 +225,11 @@ class URLFetcherDownloadProgressCancelTest : public URLFetcherTest { |
| // Version of URLFetcherTest that tests upload progress reports. |
| class URLFetcherUploadProgressTest : public URLFetcherTest { |
| public: |
| - virtual void CreateFetcher(const GURL& url); |
| - |
| - // URLFetcherDelegate |
| + // URLFetcherTest: |
| + virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| virtual void OnURLFetchUploadProgress(const URLFetcher* source, |
| - int64 current, int64 total); |
| + int64 current, |
| + int64 total) OVERRIDE; |
| protected: |
| int64 previous_progress_; |
| std::string chunk_; |
| @@ -228,14 +239,14 @@ class URLFetcherUploadProgressTest : public URLFetcherTest { |
| // Version of URLFetcherTest that tests headers. |
| class URLFetcherHeadersTest : public URLFetcherTest { |
| public: |
| - // URLFetcherDelegate |
| + // URLFetcherTest: |
| virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| }; |
| // Version of URLFetcherTest that tests SocketAddress. |
| class URLFetcherSocketAddressTest : public URLFetcherTest { |
| public: |
| - // URLFetcherDelegate |
| + // URLFetcherTest: |
| virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| protected: |
| std::string expected_host_; |
| @@ -248,9 +259,8 @@ class URLFetcherStopOnRedirectTest : public URLFetcherTest { |
| URLFetcherStopOnRedirectTest(); |
| virtual ~URLFetcherStopOnRedirectTest(); |
| - // URLFetcherTest override. |
| + // URLFetcherTest: |
| virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| - // URLFetcherDelegate |
| virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| protected: |
| @@ -263,9 +273,8 @@ class URLFetcherStopOnRedirectTest : public URLFetcherTest { |
| // Version of URLFetcherTest that tests overload protection. |
| class URLFetcherProtectTest : public URLFetcherTest { |
| public: |
| - // URLFetcherTest override. |
| + // URLFetcherTest: |
| virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| - // URLFetcherDelegate |
| virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| private: |
| Time start_time_; |
| @@ -275,9 +284,8 @@ class URLFetcherProtectTest : public URLFetcherTest { |
| // passed through. |
| class URLFetcherProtectTestPassedThrough : public URLFetcherTest { |
| public: |
| - // URLFetcherTest override. |
| + // URLFetcherTest: |
| virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| - // URLFetcherDelegate |
| virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| private: |
| Time start_time_; |
| @@ -288,7 +296,7 @@ class URLFetcherBadHTTPSTest : public URLFetcherTest { |
| public: |
| URLFetcherBadHTTPSTest(); |
| - // URLFetcherDelegate |
| + // URLFetcherTest: |
| virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| private: |
| @@ -298,9 +306,8 @@ class URLFetcherBadHTTPSTest : public URLFetcherTest { |
| // Version of URLFetcherTest that tests request cancellation on shutdown. |
| class URLFetcherCancelTest : public URLFetcherTest { |
| public: |
| - // URLFetcherTest override. |
| + // URLFetcherTest: |
| virtual void CreateFetcher(const GURL& url) OVERRIDE; |
| - // URLFetcherDelegate |
| virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| void CancelRequest(); |
| @@ -332,6 +339,8 @@ class CancelTestURLRequestContextGetter |
| context_created_(false, false), |
| throttle_for_url_(throttle_for_url) { |
| } |
| + |
| + // TestURLRequestContextGetter: |
| virtual TestURLRequestContext* GetURLRequestContext() OVERRIDE { |
| if (!context_.get()) { |
| context_.reset(new CancelTestURLRequestContext()); |
| @@ -352,9 +361,11 @@ class CancelTestURLRequestContextGetter |
| } |
| return context_.get(); |
| } |
| + |
| virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const { |
| return io_message_loop_proxy_; |
| } |
| + |
| void WaitForContextCreation() { |
| context_created_.Wait(); |
| } |
| @@ -372,7 +383,7 @@ class CancelTestURLRequestContextGetter |
| // Version of URLFetcherTest that tests retying the same request twice. |
| class URLFetcherMultipleAttemptTest : public URLFetcherTest { |
| public: |
| - // URLFetcherDelegate |
| + // URLFetcherTest: |
| virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| private: |
| std::string data_; |
| @@ -386,7 +397,7 @@ class URLFetcherFileTest : public URLFetcherTest { |
| void CreateFetcherForFile(const GURL& url, const FilePath& file_path); |
| void CreateFetcherForTempFile(const GURL& url); |
| - // URLFetcherDelegate |
| + // URLFetcherTest: |
| virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
| protected: |
| @@ -444,18 +455,28 @@ void URLFetcherDownloadProgressTest::CreateFetcher(const GURL& url) { |
| fetcher_ = new URLFetcherImpl(url, URLFetcher::GET, this); |
| fetcher_->SetRequestContext(new ThrottlingTestURLRequestContextGetter( |
| io_message_loop_proxy(), request_context())); |
| - previous_progress_ = 0; |
| fetcher_->Start(); |
| } |
| void URLFetcherDownloadProgressTest::OnURLFetchDownloadProgress( |
| const URLFetcher* source, int64 current, int64 total) { |
| - // Increasing between 0 and total. |
| + // Progress has to be between 0 and total size. |
| EXPECT_LE(0, current); |
| EXPECT_GE(total, current); |
| - EXPECT_LE(previous_progress_, current); |
| + |
| + // Progress has to be non-decreasing, total size has to be constant. |
| + if (!first_call_) { |
| + EXPECT_LE(previous_progress_, current); |
| + EXPECT_EQ(previous_total_, total); |
| + } |
| + |
| + // File size is available only when local http server is used. |
| + if (file_size_available_) |
| + EXPECT_EQ(file_size_, total); |
| + |
| previous_progress_ = current; |
| - EXPECT_EQ(expected_total_, total); |
| + previous_total_ = total; |
| + first_call_ = false; |
| } |
| void URLFetcherDownloadProgressCancelTest::CreateFetcher(const GURL& url) { |
| @@ -855,8 +876,13 @@ TEST_F(URLFetcherDownloadProgressTest, Basic) { |
| // Get a file large enough to require more than one read into |
| // URLFetcher::Core's IOBuffer. |
| static const char kFileToFetch[] = "animate1.gif"; |
|
wtc
2012/11/22 18:58:02
Since we hardcoded the file name here, I think it
|
| - file_util::GetFileSize(test_server.document_root().AppendASCII(kFileToFetch), |
| - &expected_total_); |
| +#if !defined(OS_ANDROID) |
| + ASSERT_TRUE( |
| + file_util::GetFileSize( |
| + test_server.document_root().AppendASCII(kFileToFetch), |
| + &file_size)); |
|
wtc
2012/11/22 18:58:02
I think you missed the trailing '_' for 'file_size
|
| + file_size_available_ = true; |
| +#endif |
| CreateFetcher(test_server.GetURL( |
| std::string(kTestServerFilePrefix) + kFileToFetch)); |