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" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 }; | 58 }; |
59 | 59 |
60 TEST_F(ImageDataFetcherTest, FetchImageData) { | 60 TEST_F(ImageDataFetcherTest, FetchImageData) { |
61 image_data_fetcher_.FetchImageData( | 61 image_data_fetcher_.FetchImageData( |
62 GURL(kImageURL), | 62 GURL(kImageURL), |
63 base::Bind(&ImageDataFetcherTest::OnImageDataFetched, | 63 base::Bind(&ImageDataFetcherTest::OnImageDataFetched, |
64 base::Unretained(this))); | 64 base::Unretained(this))); |
65 | 65 |
66 RequestMetadata expected_metadata; | 66 RequestMetadata expected_metadata; |
67 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; | 68 expected_metadata.http_response_code = net::HTTP_OK; |
69 EXPECT_CALL(*this, OnImageDataFetched(std::string(kURLResponseData), | 69 EXPECT_CALL(*this, OnImageDataFetched(std::string(kURLResponseData), |
70 expected_metadata)); | 70 expected_metadata)); |
71 | 71 |
72 // Get and configure the TestURLFetcher. | 72 // Get and configure the TestURLFetcher. |
73 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 73 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); |
74 ASSERT_NE(nullptr, test_url_fetcher); | 74 ASSERT_NE(nullptr, test_url_fetcher); |
75 test_url_fetcher->set_status( | 75 test_url_fetcher->set_status( |
76 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | 76 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); |
77 test_url_fetcher->SetResponseString(kURLResponseData); | 77 test_url_fetcher->SetResponseString(kURLResponseData); |
78 test_url_fetcher->set_response_code(net::HTTP_OK); | 78 test_url_fetcher->set_response_code(net::HTTP_OK); |
79 | 79 |
80 std::string raw_header = | 80 std::string raw_header = |
81 "HTTP/1.1 200 OK\n" | 81 "HTTP/1.1 200 OK\n" |
82 "Content-type: image/png\n\n"; | 82 "Content-type: image/png\n\n"; |
83 std::replace(raw_header.begin(), raw_header.end(), '\n', '\0'); | 83 std::replace(raw_header.begin(), raw_header.end(), '\n', '\0'); |
84 scoped_refptr<net::HttpResponseHeaders> headers( | 84 scoped_refptr<net::HttpResponseHeaders> headers( |
85 new net::HttpResponseHeaders(raw_header)); | 85 new net::HttpResponseHeaders(raw_header)); |
86 | 86 |
87 test_url_fetcher->set_response_headers(headers); | 87 test_url_fetcher->set_response_headers(headers); |
88 | 88 |
89 // Call the URLFetcher delegate to continue the test. | 89 // Call the URLFetcher delegate to continue the test. |
90 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 90 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); |
91 } | 91 } |
92 | 92 |
| 93 TEST_F(ImageDataFetcherTest, FetchImageData_FromCache) { |
| 94 image_data_fetcher_.FetchImageData( |
| 95 GURL(kImageURL), base::Bind(&ImageDataFetcherTest::OnImageDataFetched, |
| 96 base::Unretained(this))); |
| 97 |
| 98 RequestMetadata expected_metadata; |
| 99 expected_metadata.mime_type = std::string("image/png"); |
| 100 expected_metadata.http_response_code = net::HTTP_OK; |
| 101 expected_metadata.from_http_cache = true; |
| 102 EXPECT_CALL(*this, OnImageDataFetched(std::string(kURLResponseData), |
| 103 expected_metadata)); |
| 104 |
| 105 // Get and configure the TestURLFetcher. |
| 106 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); |
| 107 ASSERT_NE(nullptr, test_url_fetcher); |
| 108 test_url_fetcher->set_status( |
| 109 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); |
| 110 test_url_fetcher->SetResponseString(kURLResponseData); |
| 111 test_url_fetcher->set_response_code(net::HTTP_OK); |
| 112 test_url_fetcher->set_was_cached(true); |
| 113 |
| 114 std::string raw_header = |
| 115 "HTTP/1.1 200 OK\n" |
| 116 "Content-type: image/png\n\n"; |
| 117 std::replace(raw_header.begin(), raw_header.end(), '\n', '\0'); |
| 118 scoped_refptr<net::HttpResponseHeaders> headers( |
| 119 new net::HttpResponseHeaders(raw_header)); |
| 120 |
| 121 test_url_fetcher->set_response_headers(headers); |
| 122 |
| 123 // Call the URLFetcher delegate to continue the test. |
| 124 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); |
| 125 } |
| 126 |
93 TEST_F(ImageDataFetcherTest, FetchImageData_NotFound) { | 127 TEST_F(ImageDataFetcherTest, FetchImageData_NotFound) { |
94 image_data_fetcher_.FetchImageData( | 128 image_data_fetcher_.FetchImageData( |
95 GURL(kImageURL), base::Bind(&ImageDataFetcherTest::OnImageDataFetched, | 129 GURL(kImageURL), base::Bind(&ImageDataFetcherTest::OnImageDataFetched, |
96 base::Unretained(this))); | 130 base::Unretained(this))); |
97 | 131 |
98 RequestMetadata expected_metadata; | 132 RequestMetadata expected_metadata; |
99 expected_metadata.mime_type = std::string("image/png"); | 133 expected_metadata.mime_type = std::string("image/png"); |
100 expected_metadata.response_code = net::HTTP_NOT_FOUND; | 134 expected_metadata.http_response_code = net::HTTP_NOT_FOUND; |
101 // For 404, expect an empty result even though correct image data is sent. | 135 // For 404, expect an empty result even though correct image data is sent. |
102 EXPECT_CALL(*this, OnImageDataFetched(std::string(), expected_metadata)); | 136 EXPECT_CALL(*this, OnImageDataFetched(std::string(), expected_metadata)); |
103 | 137 |
104 // Get and configure the TestURLFetcher. | 138 // Get and configure the TestURLFetcher. |
105 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 139 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); |
106 ASSERT_NE(nullptr, test_url_fetcher); | 140 ASSERT_NE(nullptr, test_url_fetcher); |
107 test_url_fetcher->set_status( | 141 test_url_fetcher->set_status( |
108 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | 142 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); |
109 test_url_fetcher->SetResponseString(kURLResponseData); | 143 test_url_fetcher->SetResponseString(kURLResponseData); |
110 | 144 |
(...skipping 10 matching lines...) Expand all Loading... |
121 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 155 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); |
122 } | 156 } |
123 | 157 |
124 TEST_F(ImageDataFetcherTest, FetchImageData_FailedRequest) { | 158 TEST_F(ImageDataFetcherTest, FetchImageData_FailedRequest) { |
125 image_data_fetcher_.FetchImageData( | 159 image_data_fetcher_.FetchImageData( |
126 GURL(kImageURL), | 160 GURL(kImageURL), |
127 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedFailedRequest, | 161 base::Bind(&ImageDataFetcherTest::OnImageDataFetchedFailedRequest, |
128 base::Unretained(this))); | 162 base::Unretained(this))); |
129 | 163 |
130 RequestMetadata expected_metadata; | 164 RequestMetadata expected_metadata; |
131 expected_metadata.response_code = net::URLFetcher::RESPONSE_CODE_INVALID; | 165 expected_metadata.http_response_code = net::URLFetcher::RESPONSE_CODE_INVALID; |
132 EXPECT_CALL( | 166 EXPECT_CALL( |
133 *this, OnImageDataFetchedFailedRequest(std::string(), expected_metadata)); | 167 *this, OnImageDataFetchedFailedRequest(std::string(), expected_metadata)); |
134 | 168 |
135 // Get and configure the TestURLFetcher. | 169 // Get and configure the TestURLFetcher. |
136 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 170 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); |
137 ASSERT_NE(nullptr, test_url_fetcher); | 171 ASSERT_NE(nullptr, test_url_fetcher); |
138 test_url_fetcher->set_status( | 172 test_url_fetcher->set_status( |
139 net::URLRequestStatus(net::URLRequestStatus::FAILED, | 173 net::URLRequestStatus(net::URLRequestStatus::FAILED, |
140 net::ERR_INVALID_URL)); | 174 net::ERR_INVALID_URL)); |
141 | 175 |
(...skipping 16 matching lines...) Expand all Loading... |
158 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); | 192 net::TestURLFetcher* test_url_fetcher = fetcher_factory_.GetFetcherByID(0); |
159 ASSERT_NE(nullptr, test_url_fetcher); | 193 ASSERT_NE(nullptr, test_url_fetcher); |
160 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 194 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); |
161 | 195 |
162 test_url_fetcher = fetcher_factory_.GetFetcherByID(1); | 196 test_url_fetcher = fetcher_factory_.GetFetcherByID(1); |
163 ASSERT_NE(nullptr, test_url_fetcher); | 197 ASSERT_NE(nullptr, test_url_fetcher); |
164 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); | 198 test_url_fetcher->delegate()->OnURLFetchComplete(test_url_fetcher); |
165 } | 199 } |
166 | 200 |
167 } // namespace image_fetcher | 201 } // namespace image_fetcher |
OLD | NEW |