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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 const std::map<std::string, std::string>& params) | 154 const std::map<std::string, std::string>& params) |
155 : params_manager_(ntp_snippets::kStudyName, params), | 155 : params_manager_(ntp_snippets::kStudyName, params), |
156 mock_task_runner_(new base::TestMockTimeTaskRunner()), | 156 mock_task_runner_(new base::TestMockTimeTaskRunner()), |
157 mock_task_runner_handle_(mock_task_runner_), | 157 mock_task_runner_handle_(mock_task_runner_), |
158 signin_client_(new TestSigninClient(nullptr)), | 158 signin_client_(new TestSigninClient(nullptr)), |
159 account_tracker_(new AccountTrackerService()), | 159 account_tracker_(new AccountTrackerService()), |
160 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(), | 160 fake_signin_manager_(new FakeSigninManagerBase(signin_client_.get(), |
161 account_tracker_.get())), | 161 account_tracker_.get())), |
162 fake_token_service_(new FakeProfileOAuth2TokenService()), | 162 fake_token_service_(new FakeProfileOAuth2TokenService()), |
163 pref_service_(new TestingPrefServiceSimple()), | 163 pref_service_(new TestingPrefServiceSimple()), |
164 test_lang_("en-US"), | |
165 test_url_(gurl) { | 164 test_url_(gurl) { |
166 RequestThrottler::RegisterProfilePrefs(pref_service_->registry()); | 165 RequestThrottler::RegisterProfilePrefs(pref_service_->registry()); |
167 UserClassifier::RegisterProfilePrefs(pref_service_->registry()); | 166 UserClassifier::RegisterProfilePrefs(pref_service_->registry()); |
168 user_classifier_ = base::MakeUnique<UserClassifier>(pref_service_.get()); | 167 user_classifier_ = base::MakeUnique<UserClassifier>(pref_service_.get()); |
169 | 168 |
170 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( | 169 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( |
171 fake_signin_manager_.get(), fake_token_service_.get(), | 170 fake_signin_manager_.get(), fake_token_service_.get(), |
172 scoped_refptr<net::TestURLRequestContextGetter>( | 171 scoped_refptr<net::TestURLRequestContextGetter>( |
173 new net::TestURLRequestContextGetter(mock_task_runner_.get())), | 172 new net::TestURLRequestContextGetter(mock_task_runner_.get())), |
174 pref_service_.get(), &category_factory_, nullptr, | 173 pref_service_.get(), &category_factory_, nullptr, |
175 base::Bind(&ParseJsonDelayed), kAPIKey, user_classifier_.get()); | 174 base::Bind(&ParseJsonDelayed), kAPIKey, user_classifier_.get()); |
176 | 175 |
177 snippets_fetcher_->SetCallback( | 176 snippets_fetcher_->SetCallback( |
178 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, | 177 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, |
179 base::Unretained(&mock_callback_))); | 178 base::Unretained(&mock_callback_))); |
180 snippets_fetcher_->SetTickClockForTesting( | 179 snippets_fetcher_->SetTickClockForTesting( |
181 mock_task_runner_->GetMockTickClock()); | 180 mock_task_runner_->GetMockTickClock()); |
182 test_excluded_.insert("1234567890"); | |
183 // Increase initial time such that ticks are non-zero. | 181 // Increase initial time such that ticks are non-zero. |
184 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); | 182 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); |
185 } | 183 } |
186 | 184 |
187 NTPSnippetsFetcher& snippets_fetcher() { return *snippets_fetcher_; } | 185 NTPSnippetsFetcher& snippets_fetcher() { return *snippets_fetcher_; } |
188 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; } | 186 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; } |
189 void FastForwardUntilNoTasksRemain() { | 187 void FastForwardUntilNoTasksRemain() { |
190 mock_task_runner_->FastForwardUntilNoTasksRemain(); | 188 mock_task_runner_->FastForwardUntilNoTasksRemain(); |
191 } | 189 } |
192 const std::string& test_lang() const { return test_lang_; } | |
193 const GURL& test_url() { return test_url_; } | |
194 const std::set<std::string>& test_hosts() const { return test_hosts_; } | |
195 const std::set<std::string>& test_excluded() const { return test_excluded_; } | |
196 base::HistogramTester& histogram_tester() { return histogram_tester_; } | 190 base::HistogramTester& histogram_tester() { return histogram_tester_; } |
197 | 191 |
| 192 NTPSnippetsFetcher::Params test_params() { |
| 193 NTPSnippetsFetcher::Params result; |
| 194 result.count_to_fetch = 1; |
| 195 result.interactive_request = true; |
| 196 return result; |
| 197 } |
| 198 |
198 void InitFakeURLFetcherFactory() { | 199 void InitFakeURLFetcherFactory() { |
199 if (fake_url_fetcher_factory_) | 200 if (fake_url_fetcher_factory_) |
200 return; | 201 return; |
201 // Instantiation of factory automatically sets itself as URLFetcher's | 202 // Instantiation of factory automatically sets itself as URLFetcher's |
202 // factory. | 203 // factory. |
203 fake_url_fetcher_factory_.reset(new net::FakeURLFetcherFactory( | 204 fake_url_fetcher_factory_.reset(new net::FakeURLFetcherFactory( |
204 /*default_factory=*/&failing_url_fetcher_factory_)); | 205 /*default_factory=*/&failing_url_fetcher_factory_)); |
205 } | 206 } |
206 | 207 |
207 void SetFakeResponse(const std::string& response_data, | 208 void SetFakeResponse(const std::string& response_data, |
(...skipping 13 matching lines...) Expand all Loading... |
221 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; | 222 std::unique_ptr<net::FakeURLFetcherFactory> fake_url_fetcher_factory_; |
222 std::unique_ptr<TestSigninClient> signin_client_; | 223 std::unique_ptr<TestSigninClient> signin_client_; |
223 std::unique_ptr<AccountTrackerService> account_tracker_; | 224 std::unique_ptr<AccountTrackerService> account_tracker_; |
224 std::unique_ptr<SigninManagerBase> fake_signin_manager_; | 225 std::unique_ptr<SigninManagerBase> fake_signin_manager_; |
225 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 226 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
226 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 227 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
227 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | 228 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
228 std::unique_ptr<UserClassifier> user_classifier_; | 229 std::unique_ptr<UserClassifier> user_classifier_; |
229 CategoryFactory category_factory_; | 230 CategoryFactory category_factory_; |
230 MockSnippetsAvailableCallback mock_callback_; | 231 MockSnippetsAvailableCallback mock_callback_; |
231 const std::string test_lang_; | |
232 const GURL test_url_; | 232 const GURL test_url_; |
233 std::set<std::string> test_hosts_; | |
234 std::set<std::string> test_excluded_; | |
235 base::HistogramTester histogram_tester_; | 233 base::HistogramTester histogram_tester_; |
236 | 234 |
237 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); | 235 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); |
238 }; | 236 }; |
239 | 237 |
240 class NTPSnippetsContentSuggestionsFetcherTest : public NTPSnippetsFetcherTest { | 238 class NTPSnippetsContentSuggestionsFetcherTest : public NTPSnippetsFetcherTest { |
241 public: | 239 public: |
242 NTPSnippetsContentSuggestionsFetcherTest() | 240 NTPSnippetsContentSuggestionsFetcherTest() |
243 : NTPSnippetsFetcherTest( | 241 : NTPSnippetsFetcherTest( |
244 GURL(kTestChromeContentSuggestionsUrl), | 242 GURL(kTestChromeContentSuggestionsUrl), |
245 {{"content_suggestions_backend", kContentSuggestionsServer}}) {} | 243 {{"content_suggestions_backend", kContentSuggestionsServer}}) {} |
246 }; | 244 }; |
247 | 245 |
248 TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) { | 246 TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) { |
249 NTPSnippetsFetcher::RequestParams params; | 247 NTPSnippetsFetcher::RequestBuilder builder; |
250 params.obfuscated_gaia_id = "0BFUSGAIA"; | 248 builder.params.hosts = {"chromium.org"}; |
251 params.only_return_personalized_results = true; | 249 builder.params.excluded_ids = {"1234567890"}; |
252 params.user_locale = "en"; | 250 builder.params.count_to_fetch = 25; |
253 params.host_restricts = {"chromium.org"}; | 251 builder.params.language_code = "en"; |
254 params.excluded_ids = {"1234567890"}; | 252 builder.params.interactive_request = false; |
255 params.count_to_fetch = 25; | 253 builder.obfuscated_gaia_id = "0BFUSGAIA"; |
256 params.interactive_request = false; | 254 builder.only_return_personalized_results = true; |
257 params.user_class = "ACTIVE_NTP_USER"; | 255 builder.user_class = "ACTIVE_NTP_USER"; |
258 | 256 |
259 | 257 builder.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; |
260 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; | 258 EXPECT_THAT(builder.BuildRequest(), |
261 EXPECT_THAT(params.BuildRequest(), | |
262 EqualsJSON("{" | 259 EqualsJSON("{" |
263 " \"response_detail_level\": \"STANDARD\"," | 260 " \"response_detail_level\": \"STANDARD\"," |
264 " \"obfuscated_gaia_id\": \"0BFUSGAIA\"," | 261 " \"obfuscated_gaia_id\": \"0BFUSGAIA\"," |
265 " \"user_locale\": \"en\"," | 262 " \"user_locale\": \"en\"," |
266 " \"advanced_options\": {" | 263 " \"advanced_options\": {" |
267 " \"local_scoring_params\": {" | 264 " \"local_scoring_params\": {" |
268 " \"content_params\": {" | 265 " \"content_params\": {" |
269 " \"only_return_personalized_results\": true" | 266 " \"only_return_personalized_results\": true" |
270 " }," | 267 " }," |
271 " \"content_restricts\": [" | 268 " \"content_restricts\": [" |
(...skipping 17 matching lines...) Expand all Loading... |
289 " }" | 286 " }" |
290 " ]" | 287 " ]" |
291 " }," | 288 " }," |
292 " \"global_scoring_params\": {" | 289 " \"global_scoring_params\": {" |
293 " \"num_to_return\": 25," | 290 " \"num_to_return\": 25," |
294 " \"sort_type\": 1" | 291 " \"sort_type\": 1" |
295 " }" | 292 " }" |
296 " }" | 293 " }" |
297 "}")); | 294 "}")); |
298 | 295 |
299 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; | 296 builder.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
300 EXPECT_THAT(params.BuildRequest(), | 297 EXPECT_THAT(builder.BuildRequest(), |
301 EqualsJSON("{" | 298 EqualsJSON("{" |
302 " \"uiLanguage\": \"en\"," | 299 " \"uiLanguage\": \"en\"," |
303 " \"priority\": \"BACKGROUND_PREFETCH\"," | 300 " \"priority\": \"BACKGROUND_PREFETCH\"," |
304 " \"regularlyVisitedHostNames\": [" | 301 " \"regularlyVisitedHostNames\": [" |
305 " \"chromium.org\"" | 302 " \"chromium.org\"" |
306 " ]," | 303 " ]," |
307 " \"excludedSuggestionIds\": [" | 304 " \"excludedSuggestionIds\": [" |
308 " \"1234567890\"" | 305 " \"1234567890\"" |
309 " ]," | 306 " ]," |
310 " \"userActivenessClass\": \"ACTIVE_NTP_USER\"" | 307 " \"userActivenessClass\": \"ACTIVE_NTP_USER\"" |
311 "}")); | 308 "}")); |
312 } | 309 } |
313 | 310 |
314 TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) { | 311 TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) { |
315 NTPSnippetsFetcher::RequestParams params; | 312 NTPSnippetsFetcher::RequestBuilder builder; |
316 params.only_return_personalized_results = false; | 313 builder.params = test_params(); |
317 params.host_restricts = {}; | 314 builder.params.count_to_fetch = 10; |
318 params.count_to_fetch = 10; | 315 builder.only_return_personalized_results = false; |
319 params.excluded_ids = {}; | 316 builder.user_class = "ACTIVE_NTP_USER"; |
320 params.interactive_request = true; | |
321 params.user_class = "ACTIVE_NTP_USER"; | |
322 | 317 |
323 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; | 318 builder.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; |
324 EXPECT_THAT(params.BuildRequest(), | 319 EXPECT_THAT(builder.BuildRequest(), |
325 EqualsJSON("{" | 320 EqualsJSON("{" |
326 " \"response_detail_level\": \"STANDARD\"," | 321 " \"response_detail_level\": \"STANDARD\"," |
327 " \"advanced_options\": {" | 322 " \"advanced_options\": {" |
328 " \"local_scoring_params\": {" | 323 " \"local_scoring_params\": {" |
329 " \"content_params\": {" | 324 " \"content_params\": {" |
330 " \"only_return_personalized_results\": false" | 325 " \"only_return_personalized_results\": false" |
331 " }," | 326 " }," |
332 " \"content_restricts\": [" | 327 " \"content_restricts\": [" |
333 " {" | 328 " {" |
334 " \"type\": \"METADATA\"," | 329 " \"type\": \"METADATA\"," |
(...skipping 10 matching lines...) Expand all Loading... |
345 " ]," | 340 " ]," |
346 " \"content_selectors\": []" | 341 " \"content_selectors\": []" |
347 " }," | 342 " }," |
348 " \"global_scoring_params\": {" | 343 " \"global_scoring_params\": {" |
349 " \"num_to_return\": 10," | 344 " \"num_to_return\": 10," |
350 " \"sort_type\": 1" | 345 " \"sort_type\": 1" |
351 " }" | 346 " }" |
352 " }" | 347 " }" |
353 "}")); | 348 "}")); |
354 | 349 |
355 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; | 350 builder.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
356 EXPECT_THAT(params.BuildRequest(), | 351 EXPECT_THAT(builder.BuildRequest(), |
357 EqualsJSON("{" | 352 EqualsJSON("{" |
358 " \"regularlyVisitedHostNames\": []," | 353 " \"regularlyVisitedHostNames\": []," |
359 " \"priority\": \"USER_ACTION\"," | 354 " \"priority\": \"USER_ACTION\"," |
360 " \"excludedSuggestionIds\": []," | 355 " \"excludedSuggestionIds\": []," |
361 " \"userActivenessClass\": \"ACTIVE_NTP_USER\"" | 356 " \"userActivenessClass\": \"ACTIVE_NTP_USER\"" |
362 "}")); | 357 "}")); |
363 } | 358 } |
364 | 359 |
365 TEST_F(NTPSnippetsFetcherTest, BuildRequestExcludedIds) { | 360 TEST_F(NTPSnippetsFetcherTest, BuildRequestExcludedIds) { |
366 NTPSnippetsFetcher::RequestParams params; | 361 NTPSnippetsFetcher::RequestBuilder builder; |
367 params.only_return_personalized_results = false; | 362 builder.params = test_params(); |
368 params.host_restricts = {}; | 363 builder.params.interactive_request = false; |
369 params.count_to_fetch = 10; | 364 for (int i = 0; i < 200; ++i) |
370 params.interactive_request = false; | 365 builder.params.excluded_ids.insert(base::StringPrintf("%03d", i)); |
371 for (int i = 0; i < 200; ++i) { | 366 builder.only_return_personalized_results = false; |
372 params.excluded_ids.insert(base::StringPrintf("%03d", i)); | 367 builder.user_class = "ACTIVE_NTP_USER"; |
373 } | |
374 params.user_class = "ACTIVE_NTP_USER"; | |
375 | 368 |
376 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; | 369 builder.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
377 EXPECT_THAT(params.BuildRequest(), | 370 EXPECT_THAT(builder.BuildRequest(), |
378 EqualsJSON("{" | 371 EqualsJSON("{" |
379 " \"regularlyVisitedHostNames\": []," | 372 " \"regularlyVisitedHostNames\": []," |
380 " \"priority\": \"BACKGROUND_PREFETCH\"," | 373 " \"priority\": \"BACKGROUND_PREFETCH\"," |
381 " \"excludedSuggestionIds\": [" | 374 " \"excludedSuggestionIds\": [" |
382 " \"000\", \"001\", \"002\", \"003\", \"004\"," | 375 " \"000\", \"001\", \"002\", \"003\", \"004\"," |
383 " \"005\", \"006\", \"007\", \"008\", \"009\"," | 376 " \"005\", \"006\", \"007\", \"008\", \"009\"," |
384 " \"010\", \"011\", \"012\", \"013\", \"014\"," | 377 " \"010\", \"011\", \"012\", \"013\", \"014\"," |
385 " \"015\", \"016\", \"017\", \"018\", \"019\"," | 378 " \"015\", \"016\", \"017\", \"018\", \"019\"," |
386 " \"020\", \"021\", \"022\", \"023\", \"024\"," | 379 " \"020\", \"021\", \"022\", \"023\", \"024\"," |
387 " \"025\", \"026\", \"027\", \"028\", \"029\"," | 380 " \"025\", \"026\", \"027\", \"028\", \"029\"," |
(...skipping 12 matching lines...) Expand all Loading... |
400 " \"090\", \"091\", \"092\", \"093\", \"094\"," | 393 " \"090\", \"091\", \"092\", \"093\", \"094\"," |
401 " \"095\", \"096\", \"097\", \"098\", \"099\"" | 394 " \"095\", \"096\", \"097\", \"098\", \"099\"" |
402 // Truncated to 100 entries. Currently, they happen to | 395 // Truncated to 100 entries. Currently, they happen to |
403 // be those lexically first. | 396 // be those lexically first. |
404 " ]," | 397 " ]," |
405 " \"userActivenessClass\": \"ACTIVE_NTP_USER\"" | 398 " \"userActivenessClass\": \"ACTIVE_NTP_USER\"" |
406 "}")); | 399 "}")); |
407 } | 400 } |
408 | 401 |
409 TEST_F(NTPSnippetsFetcherTest, BuildRequestNoUserClass) { | 402 TEST_F(NTPSnippetsFetcherTest, BuildRequestNoUserClass) { |
410 NTPSnippetsFetcher::RequestParams params; | 403 NTPSnippetsFetcher::RequestBuilder builder; |
411 params.only_return_personalized_results = false; | 404 builder.params = test_params(); |
412 params.host_restricts = {}; | 405 builder.params.interactive_request = false; |
413 params.count_to_fetch = 10; | 406 builder.only_return_personalized_results = false; |
414 params.interactive_request = false; | |
415 | 407 |
416 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; | 408 builder.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
417 EXPECT_THAT(params.BuildRequest(), | 409 EXPECT_THAT(builder.BuildRequest(), |
418 EqualsJSON("{" | 410 EqualsJSON("{" |
419 " \"regularlyVisitedHostNames\": []," | 411 " \"regularlyVisitedHostNames\": []," |
420 " \"priority\": \"BACKGROUND_PREFETCH\"," | 412 " \"priority\": \"BACKGROUND_PREFETCH\"," |
421 " \"excludedSuggestionIds\": []" | 413 " \"excludedSuggestionIds\": []" |
422 "}")); | 414 "}")); |
423 } | 415 } |
424 | 416 |
425 TEST_F(NTPSnippetsFetcherTest, BuildRequestWithTwoLanguages) { | 417 TEST_F(NTPSnippetsFetcherTest, BuildRequestWithTwoLanguages) { |
426 NTPSnippetsFetcher::RequestParams params; | 418 NTPSnippetsFetcher::RequestBuilder builder; |
427 params.only_return_personalized_results = false; | 419 builder.params = test_params(); |
428 params.host_restricts = {}; | 420 builder.only_return_personalized_results = false; |
429 params.count_to_fetch = 10; | 421 builder.ui_language.language_code = "en"; |
430 params.interactive_request = true; | 422 builder.ui_language.frequency = 0.5f; |
431 params.ui_language.language_code = "en"; | 423 builder.other_top_language.language_code = "de"; |
432 params.ui_language.frequency = 0.5f; | 424 builder.other_top_language.frequency = 0.5f; |
433 params.other_top_language.language_code = "de"; | |
434 params.other_top_language.frequency = 0.5f; | |
435 | 425 |
436 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; | 426 builder.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
437 EXPECT_THAT(params.BuildRequest(), | 427 EXPECT_THAT(builder.BuildRequest(), |
438 EqualsJSON("{" | 428 EqualsJSON("{" |
439 " \"regularlyVisitedHostNames\": []," | 429 " \"regularlyVisitedHostNames\": []," |
440 " \"priority\": \"USER_ACTION\"," | 430 " \"priority\": \"USER_ACTION\"," |
441 " \"excludedSuggestionIds\": []," | 431 " \"excludedSuggestionIds\": []," |
442 " \"topLanguages\": [" | 432 " \"topLanguages\": [" |
443 " {" | 433 " {" |
444 " \"language\" : \"en\"," | 434 " \"language\" : \"en\"," |
445 " \"frequency\" : 0.5" | 435 " \"frequency\" : 0.5" |
446 " }," | 436 " }," |
447 " {" | 437 " {" |
448 " \"language\" : \"de\"," | 438 " \"language\" : \"de\"," |
449 " \"frequency\" : 0.5" | 439 " \"frequency\" : 0.5" |
450 " }" | 440 " }" |
451 " ]" | 441 " ]" |
452 "}")); | 442 "}")); |
453 } | 443 } |
454 | 444 |
455 TEST_F(NTPSnippetsFetcherTest, BuildRequestWithUILanguageOnly) { | 445 TEST_F(NTPSnippetsFetcherTest, BuildRequestWithUILanguageOnly) { |
456 NTPSnippetsFetcher::RequestParams params; | 446 NTPSnippetsFetcher::RequestBuilder builder; |
457 params.only_return_personalized_results = false; | 447 builder.params = test_params(); |
458 params.host_restricts = {}; | 448 builder.only_return_personalized_results = false; |
459 params.count_to_fetch = 10; | 449 builder.ui_language.language_code = "en"; |
460 params.interactive_request = true; | 450 builder.ui_language.frequency = 0.5f; |
461 params.ui_language.language_code = "en"; | |
462 params.ui_language.frequency = 0.5f; | |
463 | 451 |
464 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; | 452 builder.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
465 EXPECT_THAT(params.BuildRequest(), | 453 EXPECT_THAT(builder.BuildRequest(), |
466 EqualsJSON("{" | 454 EqualsJSON("{" |
467 " \"regularlyVisitedHostNames\": []," | 455 " \"regularlyVisitedHostNames\": []," |
468 " \"priority\": \"USER_ACTION\"," | 456 " \"priority\": \"USER_ACTION\"," |
469 " \"excludedSuggestionIds\": []," | 457 " \"excludedSuggestionIds\": []," |
470 " \"topLanguages\": [{" | 458 " \"topLanguages\": [{" |
471 " \"language\" : \"en\"," | 459 " \"language\" : \"en\"," |
472 " \"frequency\" : 0.5" | 460 " \"frequency\" : 0.5" |
473 " }]" | 461 " }]" |
474 "}")); | 462 "}")); |
475 } | 463 } |
476 | 464 |
477 TEST_F(NTPSnippetsFetcherTest, BuildRequestWithOtherLanguageOnly) { | 465 TEST_F(NTPSnippetsFetcherTest, BuildRequestWithOtherLanguageOnly) { |
478 NTPSnippetsFetcher::RequestParams params; | 466 NTPSnippetsFetcher::RequestBuilder builder; |
479 params.only_return_personalized_results = false; | 467 builder.params = test_params(); |
480 params.host_restricts = {}; | 468 builder.only_return_personalized_results = false; |
481 params.count_to_fetch = 10; | 469 builder.other_top_language.language_code = "de"; |
482 params.interactive_request = true; | 470 builder.other_top_language.frequency = 0.5f; |
483 params.other_top_language.language_code = "de"; | |
484 params.other_top_language.frequency = 0.5f; | |
485 | 471 |
486 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; | 472 builder.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
487 EXPECT_THAT(params.BuildRequest(), | 473 EXPECT_THAT(builder.BuildRequest(), |
488 EqualsJSON("{" | 474 EqualsJSON("{" |
489 " \"regularlyVisitedHostNames\": []," | 475 " \"regularlyVisitedHostNames\": []," |
490 " \"priority\": \"USER_ACTION\"," | 476 " \"priority\": \"USER_ACTION\"," |
491 " \"excludedSuggestionIds\": []," | 477 " \"excludedSuggestionIds\": []," |
492 " \"topLanguages\": [{" | 478 " \"topLanguages\": [{" |
493 " \"language\" : \"de\"," | 479 " \"language\" : \"de\"," |
494 " \"frequency\" : 0.5" | 480 " \"frequency\" : 0.5" |
495 " }]" | 481 " }]" |
496 "}")); | 482 "}")); |
497 } | 483 } |
(...skipping 17 matching lines...) Expand all Loading... |
515 " \"sourceCorpusInfo\" : [{" | 501 " \"sourceCorpusInfo\" : [{" |
516 " \"ampUrl\" : \"http://localhost/amp\"," | 502 " \"ampUrl\" : \"http://localhost/amp\"," |
517 " \"corpusId\" : \"http://localhost/foobar\"," | 503 " \"corpusId\" : \"http://localhost/foobar\"," |
518 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" | 504 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" |
519 " }]" | 505 " }]" |
520 " }" | 506 " }" |
521 "}]}"; | 507 "}]}"; |
522 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 508 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
523 net::URLRequestStatus::SUCCESS); | 509 net::URLRequestStatus::SUCCESS); |
524 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); | 510 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); |
525 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 511 snippets_fetcher().FetchSnippets(test_params()); |
526 test_excluded(), | |
527 /*count=*/1, | |
528 /*interactive_request=*/true); | |
529 FastForwardUntilNoTasksRemain(); | 512 FastForwardUntilNoTasksRemain(); |
530 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 513 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
531 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 514 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
532 EXPECT_THAT(histogram_tester().GetAllSamples( | 515 EXPECT_THAT(histogram_tester().GetAllSamples( |
533 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 516 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
534 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 517 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
535 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 518 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
536 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 519 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
537 /*count=*/1))); | 520 /*count=*/1))); |
538 } | 521 } |
(...skipping 12 matching lines...) Expand all Loading... |
551 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," | 534 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," |
552 " \"attribution\" : \"Foo News\"," | 535 " \"attribution\" : \"Foo News\"," |
553 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," | 536 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," |
554 " \"ampUrl\" : \"http://localhost/amp\"," | 537 " \"ampUrl\" : \"http://localhost/amp\"," |
555 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " | 538 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " |
556 " }]" | 539 " }]" |
557 "}]}"; | 540 "}]}"; |
558 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 541 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
559 net::URLRequestStatus::SUCCESS); | 542 net::URLRequestStatus::SUCCESS); |
560 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); | 543 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); |
561 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 544 snippets_fetcher().FetchSnippets(test_params()); |
562 test_excluded(), | |
563 /*count=*/1, | |
564 /*interactive_request=*/true); | |
565 FastForwardUntilNoTasksRemain(); | 545 FastForwardUntilNoTasksRemain(); |
566 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 546 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
567 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 547 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
568 EXPECT_THAT(histogram_tester().GetAllSamples( | 548 EXPECT_THAT(histogram_tester().GetAllSamples( |
569 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 549 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
570 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 550 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
571 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 551 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
572 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 552 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
573 /*count=*/1))); | 553 /*count=*/1))); |
574 } | 554 } |
575 | 555 |
576 TEST_F(NTPSnippetsContentSuggestionsFetcherTest, EmptyCategoryIsOK) { | 556 TEST_F(NTPSnippetsContentSuggestionsFetcherTest, EmptyCategoryIsOK) { |
577 const std::string kJsonStr = | 557 const std::string kJsonStr = |
578 "{\"categories\" : [{" | 558 "{\"categories\" : [{" |
579 " \"id\": 1," | 559 " \"id\": 1," |
580 " \"localizedTitle\": \"Articles for You\"" | 560 " \"localizedTitle\": \"Articles for You\"" |
581 "}]}"; | 561 "}]}"; |
582 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 562 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
583 net::URLRequestStatus::SUCCESS); | 563 net::URLRequestStatus::SUCCESS); |
584 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); | 564 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
585 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 565 snippets_fetcher().FetchSnippets(test_params()); |
586 test_excluded(), | |
587 /*count=*/1, | |
588 /*interactive_request=*/true); | |
589 FastForwardUntilNoTasksRemain(); | 566 FastForwardUntilNoTasksRemain(); |
590 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 567 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
591 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 568 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
592 EXPECT_THAT(histogram_tester().GetAllSamples( | 569 EXPECT_THAT(histogram_tester().GetAllSamples( |
593 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 570 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
594 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 571 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
595 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 572 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
596 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 573 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
597 /*count=*/1))); | 574 /*count=*/1))); |
598 } | 575 } |
(...skipping 29 matching lines...) Expand all Loading... |
628 " \"imageUrl\" : \"http://localhost/foo2.jpg\"," | 605 " \"imageUrl\" : \"http://localhost/foo2.jpg\"," |
629 " \"ampUrl\" : \"http://localhost/amp\"," | 606 " \"ampUrl\" : \"http://localhost/amp\"," |
630 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " | 607 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " |
631 " }]" | 608 " }]" |
632 "}]}"; | 609 "}]}"; |
633 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 610 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
634 net::URLRequestStatus::SUCCESS); | 611 net::URLRequestStatus::SUCCESS); |
635 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories; | 612 NTPSnippetsFetcher::OptionalFetchedCategories fetched_categories; |
636 EXPECT_CALL(mock_callback(), Run(_)) | 613 EXPECT_CALL(mock_callback(), Run(_)) |
637 .WillOnce(WithArg<0>(MovePointeeTo(&fetched_categories))); | 614 .WillOnce(WithArg<0>(MovePointeeTo(&fetched_categories))); |
638 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 615 snippets_fetcher().FetchSnippets(test_params()); |
639 test_excluded(), | |
640 /*count=*/1, | |
641 /*interactive_request=*/true); | |
642 FastForwardUntilNoTasksRemain(); | 616 FastForwardUntilNoTasksRemain(); |
643 | 617 |
644 ASSERT_TRUE(fetched_categories); | 618 ASSERT_TRUE(fetched_categories); |
645 ASSERT_THAT(fetched_categories->size(), Eq(2u)); | 619 ASSERT_THAT(fetched_categories->size(), Eq(2u)); |
646 for (const auto& category : *fetched_categories) { | 620 for (const auto& category : *fetched_categories) { |
647 const auto& articles = category.snippets; | 621 const auto& articles = category.snippets; |
648 switch (category.category.id()) { | 622 switch (category.category.id()) { |
649 case static_cast<int>(KnownCategories::ARTICLES): | 623 case static_cast<int>(KnownCategories::ARTICLES): |
650 ASSERT_THAT(articles.size(), Eq(1u)); | 624 ASSERT_THAT(articles.size(), Eq(1u)); |
651 EXPECT_THAT(articles[0]->best_source().url.spec(), | 625 EXPECT_THAT(articles[0]->best_source().url.spec(), |
(...skipping 17 matching lines...) Expand all Loading... |
669 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 643 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
670 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 644 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
671 /*count=*/1))); | 645 /*count=*/1))); |
672 } | 646 } |
673 | 647 |
674 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { | 648 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { |
675 const std::string kJsonStr = "{\"recos\": []}"; | 649 const std::string kJsonStr = "{\"recos\": []}"; |
676 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 650 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
677 net::URLRequestStatus::SUCCESS); | 651 net::URLRequestStatus::SUCCESS); |
678 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); | 652 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
679 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 653 snippets_fetcher().FetchSnippets(test_params()); |
680 test_excluded(), | |
681 /*count=*/1, | |
682 /*interactive_request=*/true); | |
683 FastForwardUntilNoTasksRemain(); | 654 FastForwardUntilNoTasksRemain(); |
684 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 655 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
685 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 656 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
686 EXPECT_THAT( | 657 EXPECT_THAT( |
687 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 658 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
688 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); | 659 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); |
689 EXPECT_THAT(histogram_tester().GetAllSamples( | 660 EXPECT_THAT(histogram_tester().GetAllSamples( |
690 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 661 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
691 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 662 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
692 } | 663 } |
693 | 664 |
694 TEST_F(NTPSnippetsFetcherTest, ShouldRestrictToHosts) { | 665 TEST_F(NTPSnippetsFetcherTest, ShouldRestrictToHosts) { |
695 net::TestURLFetcherFactory test_url_fetcher_factory; | 666 net::TestURLFetcherFactory test_url_fetcher_factory; |
696 snippets_fetcher().FetchSnippetsFromHosts( | 667 NTPSnippetsFetcher::Params params = test_params(); |
697 {"www.somehost1.com", "www.somehost2.com"}, test_lang(), test_excluded(), | 668 params.hosts = {"www.somehost1.com", "www.somehost2.com"}; |
698 /*count=*/17, | 669 params.count_to_fetch = 17; |
699 /*interactive_request=*/true); | 670 snippets_fetcher().FetchSnippets(params); |
700 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); | 671 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); |
701 ASSERT_THAT(fetcher, NotNull()); | 672 ASSERT_THAT(fetcher, NotNull()); |
702 std::unique_ptr<base::Value> value = | 673 std::unique_ptr<base::Value> value = |
703 base::JSONReader::Read(fetcher->upload_data()); | 674 base::JSONReader::Read(fetcher->upload_data()); |
704 ASSERT_TRUE(value) << " failed to parse JSON: " | 675 ASSERT_TRUE(value) << " failed to parse JSON: " |
705 << PrintToString(fetcher->upload_data()); | 676 << PrintToString(fetcher->upload_data()); |
706 const base::DictionaryValue* dict = nullptr; | 677 const base::DictionaryValue* dict = nullptr; |
707 ASSERT_TRUE(value->GetAsDictionary(&dict)); | 678 ASSERT_TRUE(value->GetAsDictionary(&dict)); |
708 const base::DictionaryValue* local_scoring_params = nullptr; | 679 const base::DictionaryValue* local_scoring_params = nullptr; |
709 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params", | 680 ASSERT_TRUE(dict->GetDictionary("advanced_options.local_scoring_params", |
710 &local_scoring_params)); | 681 &local_scoring_params)); |
711 const base::ListValue* content_selectors = nullptr; | 682 const base::ListValue* content_selectors = nullptr; |
712 ASSERT_TRUE( | 683 ASSERT_TRUE( |
713 local_scoring_params->GetList("content_selectors", &content_selectors)); | 684 local_scoring_params->GetList("content_selectors", &content_selectors)); |
714 ASSERT_THAT(content_selectors->GetSize(), Eq(static_cast<size_t>(2))); | 685 ASSERT_THAT(content_selectors->GetSize(), Eq(static_cast<size_t>(2))); |
715 const base::DictionaryValue* content_selector = nullptr; | 686 const base::DictionaryValue* content_selector = nullptr; |
716 ASSERT_TRUE(content_selectors->GetDictionary(0, &content_selector)); | 687 ASSERT_TRUE(content_selectors->GetDictionary(0, &content_selector)); |
717 std::string content_selector_value; | 688 std::string content_selector_value; |
718 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); | 689 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); |
719 EXPECT_THAT(content_selector_value, Eq("www.somehost1.com")); | 690 EXPECT_THAT(content_selector_value, Eq("www.somehost1.com")); |
720 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector)); | 691 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector)); |
721 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); | 692 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); |
722 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com")); | 693 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com")); |
723 } | 694 } |
724 | 695 |
725 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { | 696 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { |
726 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND, | 697 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND, |
727 net::URLRequestStatus::FAILED); | 698 net::URLRequestStatus::FAILED); |
728 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 699 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
729 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 700 snippets_fetcher().FetchSnippets(test_params()); |
730 test_excluded(), | |
731 /*count=*/1, | |
732 /*interactive_request=*/true); | |
733 FastForwardUntilNoTasksRemain(); | 701 FastForwardUntilNoTasksRemain(); |
734 EXPECT_THAT(snippets_fetcher().last_status(), | 702 EXPECT_THAT(snippets_fetcher().last_status(), |
735 Eq("URLRequestStatus error -2")); | 703 Eq("URLRequestStatus error -2")); |
736 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); | 704 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
737 EXPECT_THAT( | 705 EXPECT_THAT( |
738 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 706 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
739 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); | 707 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); |
740 EXPECT_THAT(histogram_tester().GetAllSamples( | 708 EXPECT_THAT(histogram_tester().GetAllSamples( |
741 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 709 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
742 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); | 710 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); |
743 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 711 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
744 Not(IsEmpty())); | 712 Not(IsEmpty())); |
745 } | 713 } |
746 | 714 |
747 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { | 715 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { |
748 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND, | 716 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_NOT_FOUND, |
749 net::URLRequestStatus::SUCCESS); | 717 net::URLRequestStatus::SUCCESS); |
750 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 718 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
751 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 719 snippets_fetcher().FetchSnippets(test_params()); |
752 test_excluded(), | |
753 /*count=*/1, | |
754 /*interactive_request=*/true); | |
755 FastForwardUntilNoTasksRemain(); | 720 FastForwardUntilNoTasksRemain(); |
756 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); | 721 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
757 EXPECT_THAT( | 722 EXPECT_THAT( |
758 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 723 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
759 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); | 724 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); |
760 EXPECT_THAT(histogram_tester().GetAllSamples( | 725 EXPECT_THAT(histogram_tester().GetAllSamples( |
761 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 726 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
762 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); | 727 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); |
763 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 728 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
764 Not(IsEmpty())); | 729 Not(IsEmpty())); |
765 } | 730 } |
766 | 731 |
767 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { | 732 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { |
768 const std::string kInvalidJsonStr = "{ \"recos\": []"; | 733 const std::string kInvalidJsonStr = "{ \"recos\": []"; |
769 SetFakeResponse(/*response_data=*/kInvalidJsonStr, net::HTTP_OK, | 734 SetFakeResponse(/*response_data=*/kInvalidJsonStr, net::HTTP_OK, |
770 net::URLRequestStatus::SUCCESS); | 735 net::URLRequestStatus::SUCCESS); |
771 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 736 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
772 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 737 snippets_fetcher().FetchSnippets(test_params()); |
773 test_excluded(), | |
774 /*count=*/1, | |
775 /*interactive_request=*/true); | |
776 FastForwardUntilNoTasksRemain(); | 738 FastForwardUntilNoTasksRemain(); |
777 EXPECT_THAT(snippets_fetcher().last_status(), | 739 EXPECT_THAT(snippets_fetcher().last_status(), |
778 StartsWith("Received invalid JSON (error ")); | 740 StartsWith("Received invalid JSON (error ")); |
779 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); | 741 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); |
780 EXPECT_THAT( | 742 EXPECT_THAT( |
781 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 743 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
782 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); | 744 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); |
783 EXPECT_THAT(histogram_tester().GetAllSamples( | 745 EXPECT_THAT(histogram_tester().GetAllSamples( |
784 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 746 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
785 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 747 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
786 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 748 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
787 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 749 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
788 /*count=*/1))); | 750 /*count=*/1))); |
789 } | 751 } |
790 | 752 |
791 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { | 753 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { |
792 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_OK, | 754 SetFakeResponse(/*response_data=*/std::string(), net::HTTP_OK, |
793 net::URLRequestStatus::SUCCESS); | 755 net::URLRequestStatus::SUCCESS); |
794 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 756 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
795 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 757 snippets_fetcher().FetchSnippets(test_params()); |
796 test_excluded(), | |
797 /*count=*/1, | |
798 /*interactive_request=*/true); | |
799 FastForwardUntilNoTasksRemain(); | 758 FastForwardUntilNoTasksRemain(); |
800 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); | 759 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); |
801 EXPECT_THAT( | 760 EXPECT_THAT( |
802 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 761 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
803 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); | 762 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); |
804 EXPECT_THAT(histogram_tester().GetAllSamples( | 763 EXPECT_THAT(histogram_tester().GetAllSamples( |
805 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 764 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
806 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 765 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
807 } | 766 } |
808 | 767 |
809 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { | 768 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { |
810 const std::string kJsonStr = | 769 const std::string kJsonStr = |
811 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; | 770 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; |
812 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 771 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
813 net::URLRequestStatus::SUCCESS); | 772 net::URLRequestStatus::SUCCESS); |
814 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 773 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
815 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 774 snippets_fetcher().FetchSnippets(test_params()); |
816 test_excluded(), | |
817 /*count=*/1, | |
818 /*interactive_request=*/true); | |
819 FastForwardUntilNoTasksRemain(); | 775 FastForwardUntilNoTasksRemain(); |
820 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 776 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
821 EXPECT_THAT( | 777 EXPECT_THAT( |
822 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 778 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
823 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1))); | 779 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1))); |
824 EXPECT_THAT(histogram_tester().GetAllSamples( | 780 EXPECT_THAT(histogram_tester().GetAllSamples( |
825 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 781 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
826 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 782 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
827 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 783 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
828 Not(IsEmpty())); | 784 Not(IsEmpty())); |
829 } | 785 } |
830 | 786 |
831 // This test actually verifies that the test setup itself is sane, to prevent | 787 // This test actually verifies that the test setup itself is sane, to prevent |
832 // hard-to-reproduce test failures. | 788 // hard-to-reproduce test failures. |
833 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { | 789 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { |
834 InitFakeURLFetcherFactory(); | 790 InitFakeURLFetcherFactory(); |
835 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 791 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
836 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 792 snippets_fetcher().FetchSnippets(test_params()); |
837 test_excluded(), | |
838 /*count=*/1, | |
839 /*interactive_request=*/true); | |
840 FastForwardUntilNoTasksRemain(); | 793 FastForwardUntilNoTasksRemain(); |
841 } | 794 } |
842 | 795 |
843 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { | 796 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { |
844 const std::string kJsonStr = "{ \"recos\": [] }"; | 797 const std::string kJsonStr = "{ \"recos\": [] }"; |
845 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, | 798 SetFakeResponse(/*response_data=*/kJsonStr, net::HTTP_OK, |
846 net::URLRequestStatus::SUCCESS); | 799 net::URLRequestStatus::SUCCESS); |
847 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); | 800 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
848 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 801 snippets_fetcher().FetchSnippets(test_params()); |
849 test_excluded(), | 802 // Second call to FetchSnippets() overrides/cancels the previous. |
850 /*count=*/1, | |
851 /*interactive_request=*/true); | |
852 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous. | |
853 // Callback is expected to be called once. | 803 // Callback is expected to be called once. |
854 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 804 snippets_fetcher().FetchSnippets(test_params()); |
855 test_excluded(), | |
856 /*count=*/1, | |
857 /*interactive_request=*/true); | |
858 FastForwardUntilNoTasksRemain(); | 805 FastForwardUntilNoTasksRemain(); |
859 EXPECT_THAT( | 806 EXPECT_THAT( |
860 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 807 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
861 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); | 808 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); |
862 EXPECT_THAT(histogram_tester().GetAllSamples( | 809 EXPECT_THAT(histogram_tester().GetAllSamples( |
863 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 810 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
864 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 811 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
865 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 812 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
866 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 813 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
867 /*count=*/1))); | 814 /*count=*/1))); |
868 } | 815 } |
869 | 816 |
870 ::std::ostream& operator<<( | 817 ::std::ostream& operator<<( |
871 ::std::ostream& os, | 818 ::std::ostream& os, |
872 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { | 819 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { |
873 if (fetched_categories) { | 820 if (fetched_categories) { |
874 // Matchers above aren't any more precise than this, so this is sufficient | 821 // Matchers above aren't any more precise than this, so this is sufficient |
875 // for test-failure diagnostics. | 822 // for test-failure diagnostics. |
876 return os << "list with " << fetched_categories->size() << " elements"; | 823 return os << "list with " << fetched_categories->size() << " elements"; |
877 } | 824 } |
878 return os << "null"; | 825 return os << "null"; |
879 } | 826 } |
880 | 827 |
881 } // namespace ntp_snippets | 828 } // namespace ntp_snippets |
OLD | NEW |