Chromium Code Reviews| Index: components/image_fetcher/image_data_fetcher_unittest.cc |
| diff --git a/components/image_fetcher/image_data_fetcher_unittest.cc b/components/image_fetcher/image_data_fetcher_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..18ebdd61e0d4472c99891a88d3be3d70b7d6aeac |
| --- /dev/null |
| +++ b/components/image_fetcher/image_data_fetcher_unittest.cc |
| @@ -0,0 +1,127 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/image_fetcher/image_data_fetcher.h" |
| + |
| +#include <memory> |
| + |
| +#include "base/callback.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "net/url_request/test_url_fetcher_factory.h" |
| +#include "net/url_request/url_request_status.h" |
| +#include "net/url_request/url_request_test_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace { |
| + |
| +const GURL kImageURL("http://www.example.com/image1"); |
|
Bernhard Bauer
2016/06/20 17:28:38
This will still create a static initializer. The o
markusheintz_
2016/06/21 15:56:08
sry forgot. Done
|
| +const char kURLResponseData[] = "EncodedImageData"; |
| + |
| +} // namespace |
| + |
| +namespace image_fetcher { |
| + |
| +class ImageDataFetcherTest : public testing::Test { |
| + public: |
| + ImageDataFetcherTest() |
| + : message_loop_(), |
| + test_request_context_getter_( |
| + new net::TestURLRequestContextGetter(message_loop_.task_runner())), |
| + image_data_fetcher_(test_request_context_getter_.get()), |
| + num_callbacks_run_(0) {} |
| + ~ImageDataFetcherTest() override {} |
| + |
| + // Callback that is called by the FetchImageData test. |
| + void OnImageDataFetched(const std::string& image_data) { |
| + EXPECT_EQ(std::string(kURLResponseData), image_data); |
| + } |
| + |
| + // Callback that is called by the FetchImageData_FailedRequest test. |
| + void OnImageDataFetchedFailedRequest(const std::string& image_data) { |
| + EXPECT_EQ(std::string(), image_data); |
| + } |
| + |
| + // Callback that is called by the FetchImageData_MultipleRequests test. |
| + void OnImageDataFetchedMultipleRequests(const std::string& image_data) { |
| + ++num_callbacks_run_; |
| + } |
| + |
| + protected: |
| + base::MessageLoop message_loop_; |
| + |
| + scoped_refptr<net::URLRequestContextGetter> test_request_context_getter_; |
| + |
| + ImageDataFetcher image_data_fetcher_; |
| + |
| + net::TestURLFetcherFactory fetcher_factory_; |
| + |
| + // The number of times the OnImageDataFetchedMultipleRequests callback was |
| + // called during a test run. |
| + int num_callbacks_run_; |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(ImageDataFetcherTest); |
| +}; |
| + |
| +TEST_F(ImageDataFetcherTest, FetchImageData) { |
| + image_data_fetcher_.FetchImageData( |
| + kImageURL, |
| + base::Bind(&ImageDataFetcherTest::OnImageDataFetched, |
| + base::Unretained(this)a)); |
|
Bernhard Bauer
2016/06/20 17:28:38
typo?
markusheintz_
2016/06/21 15:56:08
Done.
|
| + |
| + // Get and configure the TestURLFetcher. |
| + net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); |
| + ASSERT_NE(nullptr, test_url_fetcher); |
| + test_url_fetcher->set_status( |
| + net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); |
| + test_url_fetcher->SetResponseString(std::string(kURLResponseData)); |
|
Bernhard Bauer
2016/06/20 17:28:38
std::string has an implicit constructor that takes
markusheintz_
2016/06/21 15:56:08
Done.
|
| + |
| + // Call the URLFetcher delegate to continue the test. |
| + test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); |
| +} |
| + |
| +TEST_F(ImageDataFetcherTest, FetchImageData_FailedRequest) { |
| + image_data_fetcher_.FetchImageData( |
| + kImageURL, |
| + base::Bind(&ImageDataFetcherTest::OnImageDataFetchedFailedRequest, |
| + base::Unretained(this))); |
| + |
| + // Get and configure the TestURLFetcher. |
| + net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); |
| + ASSERT_NE(nullptr, test_url_fetcher); |
| + test_url_fetcher->set_status( |
| + net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| + net::ERR_INVALID_URL)); |
| + |
| + // Call the URLFetcher delegate to continue the test. |
| + test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); |
| +} |
| + |
| +TEST_F(ImageDataFetcherTest, FetchImageData_MultipleRequests) { |
| + ImageDataFetcher::ImageDataFetcherCallback callback1 = |
| + base::Bind(&ImageDataFetcherTest::OnImageDataFetchedMultipleRequests, |
| + base::Unretained(this)); |
| + ImageDataFetcher::ImageDataFetcherCallback callback2 = |
| + base::Bind(&ImageDataFetcherTest::OnImageDataFetchedMultipleRequests, |
| + base::Unretained(this)); |
| + |
| + image_data_fetcher_.FetchImageData(kImageURL, callback1); |
| + image_data_fetcher_.FetchImageData(kImageURL, callback2); |
| + |
| + // Multiple calls to the FetchImageData for the same URL will result in |
|
Bernhard Bauer
2016/06/20 17:28:38
No "the" before FetchImageData
markusheintz_
2016/06/21 15:56:08
Done.
|
| + // multiple URLFetcher beeing created. |
|
Marc Treib
2016/06/20 15:21:42
nit: being
Bernhard Bauer
2016/06/20 17:28:38
URLFetchers
markusheintz_
2016/06/21 15:56:08
Done.
markusheintz_
2016/06/21 15:56:08
Done.
|
| + net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); |
| + ASSERT_NE(nullptr, test_url_fetcher); |
| + test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); |
| + |
| + test_url_fetcher = fetcher_factory_.GetFetcherByID(1); |
| + ASSERT_NE(nullptr, test_url_fetcher); |
| + test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); |
| + |
| + EXPECT_EQ(num_callbacks_run_, 2); |
| +} |
| + |
| +} // namespace image_fetcher |