| 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/http/http_response_headers.h" | 
|  | 14 #include "net/http/http_status_code.h" | 
| 14 #include "net/url_request/test_url_fetcher_factory.h" | 15 #include "net/url_request/test_url_fetcher_factory.h" | 
| 15 #include "net/url_request/url_request_status.h" | 16 #include "net/url_request/url_request_status.h" | 
| 16 #include "net/url_request/url_request_test_util.h" | 17 #include "net/url_request/url_request_test_util.h" | 
| 17 #include "testing/gmock/include/gmock/gmock.h" | 18 #include "testing/gmock/include/gmock/gmock.h" | 
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" | 
| 19 | 20 | 
| 20 namespace { | 21 namespace { | 
| 21 | 22 | 
| 22 const char kImageURL[] = "http://www.example.com/image"; | 23 const char kImageURL[] = "http://www.example.com/image"; | 
| 23 const char kURLResponseData[] = "EncodedImageData"; | 24 const char kURLResponseData[] = "EncodedImageData"; | 
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 57 }; | 58 }; | 
| 58 | 59 | 
| 59 TEST_F(ImageDataFetcherTest, FetchImageData) { | 60 TEST_F(ImageDataFetcherTest, FetchImageData) { | 
| 60   image_data_fetcher_.FetchImageData( | 61   image_data_fetcher_.FetchImageData( | 
| 61       GURL(kImageURL), | 62       GURL(kImageURL), | 
| 62       base::Bind(&ImageDataFetcherTest::OnImageDataFetched, | 63       base::Bind(&ImageDataFetcherTest::OnImageDataFetched, | 
| 63                  base::Unretained(this))); | 64                  base::Unretained(this))); | 
| 64 | 65 | 
| 65   RequestMetadata expected_metadata; | 66   RequestMetadata expected_metadata; | 
| 66   expected_metadata.mime_type = std::string("image/png"); | 67   expected_metadata.mime_type = std::string("image/png"); | 
|  | 68   expected_metadata.response_code = net::HTTP_OK; | 
| 67   EXPECT_CALL(*this, OnImageDataFetched(std::string(kURLResponseData), | 69   EXPECT_CALL(*this, OnImageDataFetched(std::string(kURLResponseData), | 
| 68                                         expected_metadata)); | 70                                         expected_metadata)); | 
| 69 | 71 | 
| 70   // Get and configure the TestURLFetcher. | 72   // Get and configure the TestURLFetcher. | 
| 71   net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 73   net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 
| 72   ASSERT_NE(nullptr, test_url_fetcher); | 74   ASSERT_NE(nullptr, test_url_fetcher); | 
| 73   test_url_fetcher->set_status( | 75   test_url_fetcher->set_status( | 
| 74       net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | 76       net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | 
| 75   test_url_fetcher->SetResponseString(kURLResponseData); | 77   test_url_fetcher->SetResponseString(kURLResponseData); | 
|  | 78   test_url_fetcher->set_response_code(net::HTTP_OK); | 
| 76 | 79 | 
| 77   std::string raw_header = | 80   std::string raw_header = | 
| 78       "HTTP/1.1 200 OK\n" | 81       "HTTP/1.1 200 OK\n" | 
| 79       "Content-type: image/png\n\n"; | 82       "Content-type: image/png\n\n"; | 
| 80   std::replace(raw_header.begin(), raw_header.end(), '\n', '\0'); | 83   std::replace(raw_header.begin(), raw_header.end(), '\n', '\0'); | 
| 81   scoped_refptr<net::HttpResponseHeaders> headers( | 84   scoped_refptr<net::HttpResponseHeaders> headers( | 
| 82       new net::HttpResponseHeaders(raw_header)); | 85       new net::HttpResponseHeaders(raw_header)); | 
| 83 | 86 | 
| 84   test_url_fetcher->set_response_headers(headers); | 87   test_url_fetcher->set_response_headers(headers); | 
| 85 | 88 | 
| 86   // Call the URLFetcher delegate to continue the test. | 89   // Call the URLFetcher delegate to continue the test. | 
| 87   test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 90   test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 
| 88 } | 91 } | 
| 89 | 92 | 
| 90 TEST_F(ImageDataFetcherTest, FetchImageData_NotFound) { | 93 TEST_F(ImageDataFetcherTest, FetchImageData_NotFound) { | 
| 91   image_data_fetcher_.FetchImageData( | 94   image_data_fetcher_.FetchImageData( | 
| 92       GURL(kImageURL), base::Bind(&ImageDataFetcherTest::OnImageDataFetched, | 95       GURL(kImageURL), base::Bind(&ImageDataFetcherTest::OnImageDataFetched, | 
| 93                                   base::Unretained(this))); | 96                                   base::Unretained(this))); | 
| 94 | 97 | 
| 95   RequestMetadata expected_metadata; | 98   RequestMetadata expected_metadata; | 
| 96   expected_metadata.mime_type = std::string("image/png"); | 99   expected_metadata.mime_type = std::string("image/png"); | 
|  | 100   expected_metadata.response_code = net::HTTP_NOT_FOUND; | 
| 97   // For 404, expect an empty result even though correct image data is sent. | 101   // For 404, expect an empty result even though correct image data is sent. | 
| 98   EXPECT_CALL(*this, OnImageDataFetched(std::string(), expected_metadata)); | 102   EXPECT_CALL(*this, OnImageDataFetched(std::string(), expected_metadata)); | 
| 99 | 103 | 
| 100   // Get and configure the TestURLFetcher. | 104   // Get and configure the TestURLFetcher. | 
| 101   net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 105   net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 
| 102   ASSERT_NE(nullptr, test_url_fetcher); | 106   ASSERT_NE(nullptr, test_url_fetcher); | 
| 103   test_url_fetcher->set_status( | 107   test_url_fetcher->set_status( | 
| 104       net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | 108       net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | 
| 105   test_url_fetcher->SetResponseString(kURLResponseData); | 109   test_url_fetcher->SetResponseString(kURLResponseData); | 
| 106 | 110 | 
| (...skipping 10 matching lines...) Expand all  Loading... | 
| 117   test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 121   test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 
| 118 } | 122 } | 
| 119 | 123 | 
| 120 TEST_F(ImageDataFetcherTest, FetchImageData_FailedRequest) { | 124 TEST_F(ImageDataFetcherTest, FetchImageData_FailedRequest) { | 
| 121   image_data_fetcher_.FetchImageData( | 125   image_data_fetcher_.FetchImageData( | 
| 122       GURL(kImageURL), | 126       GURL(kImageURL), | 
| 123       base::Bind(&ImageDataFetcherTest::OnImageDataFetchedFailedRequest, | 127       base::Bind(&ImageDataFetcherTest::OnImageDataFetchedFailedRequest, | 
| 124                  base::Unretained(this))); | 128                  base::Unretained(this))); | 
| 125 | 129 | 
| 126   RequestMetadata expected_metadata; | 130   RequestMetadata expected_metadata; | 
|  | 131   expected_metadata.response_code = net::URLFetcher::RESPONSE_CODE_INVALID; | 
| 127   EXPECT_CALL( | 132   EXPECT_CALL( | 
| 128       *this, OnImageDataFetchedFailedRequest(std::string(), expected_metadata)); | 133       *this, OnImageDataFetchedFailedRequest(std::string(), expected_metadata)); | 
| 129 | 134 | 
| 130   // Get and configure the TestURLFetcher. | 135   // Get and configure the TestURLFetcher. | 
| 131   net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 136   net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 
| 132   ASSERT_NE(nullptr, test_url_fetcher); | 137   ASSERT_NE(nullptr, test_url_fetcher); | 
| 133   test_url_fetcher->set_status( | 138   test_url_fetcher->set_status( | 
| 134       net::URLRequestStatus(net::URLRequestStatus::FAILED, | 139       net::URLRequestStatus(net::URLRequestStatus::FAILED, | 
| 135                             net::ERR_INVALID_URL)); | 140                             net::ERR_INVALID_URL)); | 
| 136 | 141 | 
| (...skipping 16 matching lines...) Expand all  Loading... | 
| 153   net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 158   net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 
| 154   ASSERT_NE(nullptr, test_url_fetcher); | 159   ASSERT_NE(nullptr, test_url_fetcher); | 
| 155   test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 160   test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 
| 156 | 161 | 
| 157   test_url_fetcher = fetcher_factory_.GetFetcherByID(1); | 162   test_url_fetcher = fetcher_factory_.GetFetcherByID(1); | 
| 158   ASSERT_NE(nullptr, test_url_fetcher); | 163   ASSERT_NE(nullptr, test_url_fetcher); | 
| 159   test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 164   test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 
| 160 } | 165 } | 
| 161 | 166 | 
| 162 }  // namespace image_fetcher | 167 }  // namespace image_fetcher | 
| OLD | NEW | 
|---|