| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/image_fetcher/image_data_fetcher.h" | 5 #include "components/image_fetcher/image_data_fetcher.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "net/http/http_response_headers.h" |
| 13 #include "net/url_request/test_url_fetcher_factory.h" | 14 #include "net/url_request/test_url_fetcher_factory.h" |
| 14 #include "net/url_request/url_request_status.h" | 15 #include "net/url_request/url_request_status.h" |
| 15 #include "net/url_request/url_request_test_util.h" | 16 #include "net/url_request/url_request_test_util.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 const char kImageURL[] = "http://www.example.com/image"; | 22 const char kImageURL[] = "http://www.example.com/image"; |
| 22 const char kURLResponseData[] = "EncodedImageData"; | 23 const char kURLResponseData[] = "EncodedImageData"; |
| 23 | 24 |
| 24 } // namespace | 25 } // namespace |
| 25 | 26 |
| 26 namespace image_fetcher { | 27 namespace image_fetcher { |
| 27 | 28 |
| 29 TEST(RequestMetadataTest, Equality) { |
| 30 RequestMetadata rhs; |
| 31 RequestMetadata lhs; |
| 32 rhs.mime_type = "testMimeType"; |
| 33 lhs.mime_type = "testMimeType"; |
| 34 |
| 35 EXPECT_EQ(rhs, lhs); |
| 36 } |
| 37 |
| 38 TEST(RequestMetadataTest, NoEquality) { |
| 39 RequestMetadata rhs; |
| 40 RequestMetadata lhs; |
| 41 rhs.mime_type = "testMimeType"; |
| 42 lhs.mime_type = "testOtherMimeType"; |
| 43 |
| 44 EXPECT_NE(rhs, lhs); |
| 45 } |
| 46 |
| 28 class ImageDataFetcherTest : public testing::Test { | 47 class ImageDataFetcherTest : public testing::Test { |
| 29 public: | 48 public: |
| 30 ImageDataFetcherTest() | 49 ImageDataFetcherTest() |
| 31 : test_request_context_getter_( | 50 : test_request_context_getter_( |
| 32 new net::TestURLRequestContextGetter(message_loop_.task_runner())), | 51 new net::TestURLRequestContextGetter(message_loop_.task_runner())), |
| 33 image_data_fetcher_(test_request_context_getter_.get()) {} | 52 image_data_fetcher_(test_request_context_getter_.get()) {} |
| 34 ~ImageDataFetcherTest() override {} | 53 ~ImageDataFetcherTest() override {} |
| 35 | 54 |
| 36 MOCK_METHOD1(OnImageDataFetched, void(const std::string&)); | 55 MOCK_METHOD2(OnImageDataFetched, |
| 56 void(const std::string&, const RequestMetadata&)); |
| 37 | 57 |
| 38 MOCK_METHOD1(OnImageDataFetchedFailedRequest, void(const std::string&)); | 58 MOCK_METHOD2(OnImageDataFetchedFailedRequest, |
| 59 void(const std::string&, const RequestMetadata&)); |
| 39 | 60 |
| 40 MOCK_METHOD1(OnImageDataFetchedMultipleRequests, void(const std::string&)); | 61 MOCK_METHOD2(OnImageDataFetchedMultipleRequests, |
| 62 void(const std::string&, const RequestMetadata&)); |
| 41 | 63 |
| 42 protected: | 64 protected: |
| 43 base::MessageLoop message_loop_; | 65 base::MessageLoop message_loop_; |
| 44 | 66 |
| 45 scoped_refptr<net::URLRequestContextGetter> test_request_context_getter_; | 67 scoped_refptr<net::URLRequestContextGetter> test_request_context_getter_; |
| 46 | 68 |
| 47 ImageDataFetcher image_data_fetcher_; | 69 ImageDataFetcher image_data_fetcher_; |
| 48 | 70 |
| 49 net::TestURLFetcherFactory fetcher_factory_; | 71 net::TestURLFetcherFactory fetcher_factory_; |
| 50 | 72 |
| 51 private: | 73 private: |
| 52 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcherTest); | 74 DISALLOW_COPY_AND_ASSIGN(ImageDataFetcherTest); |
| 53 }; | 75 }; |
| 54 | 76 |
| 55 TEST_F(ImageDataFetcherTest, FetchImageData) { | 77 TEST_F(ImageDataFetcherTest, FetchImageData) { |
| 56 image_data_fetcher_.FetchImageData( | 78 image_data_fetcher_.FetchImageData( |
| 57 GURL(kImageURL), | 79 GURL(kImageURL), |
| 58 base::Bind(&ImageDataFetcherTest::OnImageDataFetched, | 80 base::Bind(&ImageDataFetcherTest::OnImageDataFetched, |
| 59 base::Unretained(this))); | 81 base::Unretained(this))); |
| 60 EXPECT_CALL(*this, OnImageDataFetched(std::string(kURLResponseData))); | 82 |
| 83 RequestMetadata expected_metadata; |
| 84 expected_metadata.mime_type = std::string("image/gif"); |
| 85 EXPECT_CALL(*this, OnImageDataFetched(std::string(kURLResponseData), |
| 86 expected_metadata)); |
| 61 | 87 |
| 62 // Get and configure the TestURLFetcher. | 88 // Get and configure the TestURLFetcher. |
| 63 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 89 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); |
| 64 ASSERT_NE(nullptr, test_url_fetcher); | 90 ASSERT_NE(nullptr, test_url_fetcher); |
| 65 test_url_fetcher->set_status( | 91 test_url_fetcher->set_status( |
| 66 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | 92 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); |
| 67 test_url_fetcher->SetResponseString(kURLResponseData); | 93 test_url_fetcher->SetResponseString(kURLResponseData); |
| 68 | 94 |
| 95 std::string raw_header = |
| 96 "HTTP/1.1 200 OK\n" |
| 97 "Content-type: image/gif\n\n"; |
| 98 std::replace(raw_header.begin(), raw_header.end(), '\n', '\0'); |
| 99 scoped_refptr<net::HttpResponseHeaders> headers( |
| 100 new net::HttpResponseHeaders(raw_header)); |
| 101 |
| 102 test_url_fetcher->set_response_headers(headers); |
| 103 |
| 69 // Call the URLFetcher delegate to continue the test. | 104 // Call the URLFetcher delegate to continue the test. |
| 70 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 105 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); |
| 71 } | 106 } |
| 72 | 107 |
| 73 TEST_F(ImageDataFetcherTest, FetchImageData_FailedRequest) { | 108 TEST_F(ImageDataFetcherTest, FetchImageData_FailedRequest) { |
| 74 image_data_fetcher_.FetchImageData( | 109 image_data_fetcher_.FetchImageData( |
| 75 GURL(kImageURL), | 110 GURL(kImageURL), |
| 76 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedFailedRequest, | 111 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedFailedRequest, |
| 77 base::Unretained(this))); | 112 base::Unretained(this))); |
| 78 EXPECT_CALL(*this, OnImageDataFetchedFailedRequest(std::string())); | 113 |
| 114 RequestMetadata expected_metadata; |
| 115 EXPECT_CALL( |
| 116 *this, OnImageDataFetchedFailedRequest(std::string(), expected_metadata)); |
| 79 | 117 |
| 80 // Get and configure the TestURLFetcher. | 118 // Get and configure the TestURLFetcher. |
| 81 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 119 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); |
| 82 ASSERT_NE(nullptr, test_url_fetcher); | 120 ASSERT_NE(nullptr, test_url_fetcher); |
| 83 test_url_fetcher->set_status( | 121 test_url_fetcher->set_status( |
| 84 net::URLRequestStatus(net::URLRequestStatus::FAILED, | 122 net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 85 net::ERR_INVALID_URL)); | 123 net::ERR_INVALID_URL)); |
| 86 | 124 |
| 87 // Call the URLFetcher delegate to continue the test. | 125 // Call the URLFetcher delegate to continue the test. |
| 88 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 126 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); |
| 89 } | 127 } |
| 90 | 128 |
| 91 TEST_F(ImageDataFetcherTest, FetchImageData_MultipleRequests) { | 129 TEST_F(ImageDataFetcherTest, FetchImageData_MultipleRequests) { |
| 92 ImageDataFetcher::ImageDataFetcherCallback callback = | 130 ImageDataFetcher::ImageDataFetcherCallback callback = |
| 93 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedMultipleRequests, | 131 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedMultipleRequests, |
| 94 base::Unretained(this)); | 132 base::Unretained(this)); |
| 95 EXPECT_CALL(*this, OnImageDataFetchedMultipleRequests(testing::_)).Times(2); | 133 EXPECT_CALL(*this, OnImageDataFetchedMultipleRequests(testing::_, testing::_)) |
| 134 .Times(2); |
| 96 | 135 |
| 97 image_data_fetcher_.FetchImageData(GURL(kImageURL), callback); | 136 image_data_fetcher_.FetchImageData(GURL(kImageURL), callback); |
| 98 image_data_fetcher_.FetchImageData(GURL(kImageURL), callback); | 137 image_data_fetcher_.FetchImageData(GURL(kImageURL), callback); |
| 99 | 138 |
| 100 // Multiple calls to FetchImageData for the same URL will result in | 139 // Multiple calls to FetchImageData for the same URL will result in |
| 101 // multiple URLFetchers being created. | 140 // multiple URLFetchers being created. |
| 102 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 141 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); |
| 103 ASSERT_NE(nullptr, test_url_fetcher); | 142 ASSERT_NE(nullptr, test_url_fetcher); |
| 104 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 143 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); |
| 105 | 144 |
| 106 test_url_fetcher = fetcher_factory_.GetFetcherByID(1); | 145 test_url_fetcher = fetcher_factory_.GetFetcherByID(1); |
| 107 ASSERT_NE(nullptr, test_url_fetcher); | 146 ASSERT_NE(nullptr, test_url_fetcher); |
| 108 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 147 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); |
| 109 } | 148 } |
| 110 | 149 |
| 111 } // namespace image_fetcher | 150 } // namespace image_fetcher |
| OLD | NEW |