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/ntp_snippets/remote/ntp_snippets_fetcher.h" | 5 #include "components/ntp_snippets/remote/ntp_snippets_fetcher.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 net::URLRequestStatus::FAILED); | 167 net::URLRequestStatus::FAILED); |
168 } | 168 } |
169 }; | 169 }; |
170 | 170 |
171 void ParseJson( | 171 void ParseJson( |
172 const std::string& json, | 172 const std::string& json, |
173 const ntp_snippets::NTPSnippetsFetcher::SuccessCallback& success_callback, | 173 const ntp_snippets::NTPSnippetsFetcher::SuccessCallback& success_callback, |
174 const ntp_snippets::NTPSnippetsFetcher::ErrorCallback& error_callback) { | 174 const ntp_snippets::NTPSnippetsFetcher::ErrorCallback& error_callback) { |
175 base::JSONReader json_reader; | 175 base::JSONReader json_reader; |
176 std::unique_ptr<base::Value> value = json_reader.ReadToValue(json); | 176 std::unique_ptr<base::Value> value = json_reader.ReadToValue(json); |
177 if (value) | 177 if (value) { |
178 success_callback.Run(std::move(value)); | 178 success_callback.Run(std::move(value)); |
179 else | 179 } else { |
180 error_callback.Run(json_reader.GetErrorMessage()); | 180 error_callback.Run(json_reader.GetErrorMessage()); |
| 181 } |
181 } | 182 } |
182 | 183 |
183 void ParseJsonDelayed( | 184 void ParseJsonDelayed( |
184 const std::string& json, | 185 const std::string& json, |
185 const ntp_snippets::NTPSnippetsFetcher::SuccessCallback& success_callback, | 186 const ntp_snippets::NTPSnippetsFetcher::SuccessCallback& success_callback, |
186 const ntp_snippets::NTPSnippetsFetcher::ErrorCallback& error_callback) { | 187 const ntp_snippets::NTPSnippetsFetcher::ErrorCallback& error_callback) { |
187 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 188 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
188 FROM_HERE, base::Bind(&ParseJson, json, success_callback, error_callback), | 189 FROM_HERE, base::Bind(&ParseJson, json, success_callback, error_callback), |
189 base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs)); | 190 base::TimeDelta::FromMilliseconds(kTestJsonParsingLatencyMs)); |
190 } | 191 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 base::HistogramTester& histogram_tester() { return histogram_tester_; } | 241 base::HistogramTester& histogram_tester() { return histogram_tester_; } |
241 | 242 |
242 NTPSnippetsFetcher::Params test_params() { | 243 NTPSnippetsFetcher::Params test_params() { |
243 NTPSnippetsFetcher::Params result; | 244 NTPSnippetsFetcher::Params result; |
244 result.count_to_fetch = 1; | 245 result.count_to_fetch = 1; |
245 result.interactive_request = true; | 246 result.interactive_request = true; |
246 return result; | 247 return result; |
247 } | 248 } |
248 | 249 |
249 void InitFakeURLFetcherFactory() { | 250 void InitFakeURLFetcherFactory() { |
250 if (fake_url_fetcher_factory_) | 251 if (fake_url_fetcher_factory_) { |
251 return; | 252 return; |
| 253 } |
252 // Instantiation of factory automatically sets itself as URLFetcher's | 254 // Instantiation of factory automatically sets itself as URLFetcher's |
253 // factory. | 255 // factory. |
254 fake_url_fetcher_factory_.reset(new net::FakeURLFetcherFactory( | 256 fake_url_fetcher_factory_.reset(new net::FakeURLFetcherFactory( |
255 /*default_factory=*/&failing_url_fetcher_factory_)); | 257 /*default_factory=*/&failing_url_fetcher_factory_)); |
256 } | 258 } |
257 | 259 |
258 void SetFakeResponse(const std::string& response_data, | 260 void SetFakeResponse(const std::string& response_data, |
259 net::HttpStatusCode response_code, | 261 net::HttpStatusCode response_code, |
260 net::URLRequestStatus::Status status) { | 262 net::URLRequestStatus::Status status) { |
261 InitFakeURLFetcherFactory(); | 263 InitFakeURLFetcherFactory(); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
404 " \"priority\": \"USER_ACTION\"," | 406 " \"priority\": \"USER_ACTION\"," |
405 " \"excludedSuggestionIds\": []," | 407 " \"excludedSuggestionIds\": []," |
406 " \"userActivenessClass\": \"ACTIVE_NTP_USER\"" | 408 " \"userActivenessClass\": \"ACTIVE_NTP_USER\"" |
407 "}")); | 409 "}")); |
408 } | 410 } |
409 | 411 |
410 TEST_F(NTPSnippetsFetcherTest, BuildRequestExcludedIds) { | 412 TEST_F(NTPSnippetsFetcherTest, BuildRequestExcludedIds) { |
411 NTPSnippetsFetcher::RequestBuilder builder; | 413 NTPSnippetsFetcher::RequestBuilder builder; |
412 builder.params = test_params(); | 414 builder.params = test_params(); |
413 builder.params.interactive_request = false; | 415 builder.params.interactive_request = false; |
414 for (int i = 0; i < 200; ++i) | 416 for (int i = 0; i < 200; ++i) { |
415 builder.params.excluded_ids.insert(base::StringPrintf("%03d", i)); | 417 builder.params.excluded_ids.insert(base::StringPrintf("%03d", i)); |
| 418 } |
416 builder.only_return_personalized_results = false; | 419 builder.only_return_personalized_results = false; |
417 builder.user_class = "ACTIVE_NTP_USER"; | 420 builder.user_class = "ACTIVE_NTP_USER"; |
418 | 421 |
419 builder.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; | 422 builder.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
420 EXPECT_THAT(builder.BuildRequest(), | 423 EXPECT_THAT(builder.BuildRequest(), |
421 EqualsJSON("{" | 424 EqualsJSON("{" |
422 " \"regularlyVisitedHostNames\": []," | 425 " \"regularlyVisitedHostNames\": []," |
423 " \"priority\": \"BACKGROUND_PREFETCH\"," | 426 " \"priority\": \"BACKGROUND_PREFETCH\"," |
424 " \"excludedSuggestionIds\": [" | 427 " \"excludedSuggestionIds\": [" |
425 " \"000\", \"001\", \"002\", \"003\", \"004\"," | 428 " \"000\", \"001\", \"002\", \"003\", \"004\"," |
(...skipping 575 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1001 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { | 1004 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { |
1002 if (fetched_categories) { | 1005 if (fetched_categories) { |
1003 // Matchers above aren't any more precise than this, so this is sufficient | 1006 // Matchers above aren't any more precise than this, so this is sufficient |
1004 // for test-failure diagnostics. | 1007 // for test-failure diagnostics. |
1005 return os << "list with " << fetched_categories->size() << " elements"; | 1008 return os << "list with " << fetched_categories->size() << " elements"; |
1006 } | 1009 } |
1007 return os << "null"; | 1010 return os << "null"; |
1008 } | 1011 } |
1009 | 1012 |
1010 } // namespace ntp_snippets | 1013 } // namespace ntp_snippets |
OLD | NEW |