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 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 fake_token_service_(new FakeProfileOAuth2TokenService()), | 161 fake_token_service_(new FakeProfileOAuth2TokenService()), |
162 pref_service_(new TestingPrefServiceSimple()), | 162 pref_service_(new TestingPrefServiceSimple()), |
163 test_lang_("en-US"), | 163 test_lang_("en-US"), |
164 test_url_(gurl) { | 164 test_url_(gurl) { |
165 RequestThrottler::RegisterProfilePrefs(pref_service_->registry()); | 165 RequestThrottler::RegisterProfilePrefs(pref_service_->registry()); |
166 | 166 |
167 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( | 167 snippets_fetcher_ = base::MakeUnique<NTPSnippetsFetcher>( |
168 fake_signin_manager_.get(), fake_token_service_.get(), | 168 fake_signin_manager_.get(), fake_token_service_.get(), |
169 scoped_refptr<net::TestURLRequestContextGetter>( | 169 scoped_refptr<net::TestURLRequestContextGetter>( |
170 new net::TestURLRequestContextGetter(mock_task_runner_.get())), | 170 new net::TestURLRequestContextGetter(mock_task_runner_.get())), |
171 pref_service_.get(), &category_factory_, base::Bind(&ParseJsonDelayed), | 171 pref_service_.get(), &category_factory_, nullptr, |
172 kAPIKey); | 172 base::Bind(&ParseJsonDelayed), kAPIKey); |
173 | 173 |
174 snippets_fetcher_->SetCallback( | 174 snippets_fetcher_->SetCallback( |
175 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, | 175 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, |
176 base::Unretained(&mock_callback_))); | 176 base::Unretained(&mock_callback_))); |
177 snippets_fetcher_->SetTickClockForTesting( | 177 snippets_fetcher_->SetTickClockForTesting( |
178 mock_task_runner_->GetMockTickClock()); | 178 mock_task_runner_->GetMockTickClock()); |
179 test_excluded_.insert("1234567890"); | 179 test_excluded_.insert("1234567890"); |
180 // Increase initial time such that ticks are non-zero. | 180 // Increase initial time such that ticks are non-zero. |
181 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); | 181 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); |
182 } | 182 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 | 243 |
244 TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) { | 244 TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) { |
245 NTPSnippetsFetcher::RequestParams params; | 245 NTPSnippetsFetcher::RequestParams params; |
246 params.obfuscated_gaia_id = "0BFUSGAIA"; | 246 params.obfuscated_gaia_id = "0BFUSGAIA"; |
247 params.only_return_personalized_results = true; | 247 params.only_return_personalized_results = true; |
248 params.user_locale = "en"; | 248 params.user_locale = "en"; |
249 params.host_restricts = {"chromium.org"}; | 249 params.host_restricts = {"chromium.org"}; |
250 params.excluded_ids = {"1234567890"}; | 250 params.excluded_ids = {"1234567890"}; |
251 params.count_to_fetch = 25; | 251 params.count_to_fetch = 25; |
252 params.interactive_request = false; | 252 params.interactive_request = false; |
| 253 params.ui_language.frequency = 0.0f; |
| 254 params.other_top_language.frequency = 0.0f; |
| 255 |
253 | 256 |
254 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; | 257 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; |
255 EXPECT_THAT(params.BuildRequest(), | 258 EXPECT_THAT(params.BuildRequest(), |
256 EqualsJSON("{" | 259 EqualsJSON("{" |
257 " \"response_detail_level\": \"STANDARD\"," | 260 " \"response_detail_level\": \"STANDARD\"," |
258 " \"obfuscated_gaia_id\": \"0BFUSGAIA\"," | 261 " \"obfuscated_gaia_id\": \"0BFUSGAIA\"," |
259 " \"user_locale\": \"en\"," | 262 " \"user_locale\": \"en\"," |
260 " \"advanced_options\": {" | 263 " \"advanced_options\": {" |
261 " \"local_scoring_params\": {" | 264 " \"local_scoring_params\": {" |
262 " \"content_params\": {" | 265 " \"content_params\": {" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 "}")); | 307 "}")); |
305 } | 308 } |
306 | 309 |
307 TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) { | 310 TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) { |
308 NTPSnippetsFetcher::RequestParams params; | 311 NTPSnippetsFetcher::RequestParams params; |
309 params.only_return_personalized_results = false; | 312 params.only_return_personalized_results = false; |
310 params.host_restricts = {}; | 313 params.host_restricts = {}; |
311 params.count_to_fetch = 10; | 314 params.count_to_fetch = 10; |
312 params.excluded_ids = {}; | 315 params.excluded_ids = {}; |
313 params.interactive_request = true; | 316 params.interactive_request = true; |
314 | 317 params.ui_language.frequency = 0.0f; |
| 318 params.other_top_language.frequency = 0.0f; |
315 | 319 |
316 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; | 320 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; |
317 EXPECT_THAT(params.BuildRequest(), | 321 EXPECT_THAT(params.BuildRequest(), |
318 EqualsJSON("{" | 322 EqualsJSON("{" |
319 " \"response_detail_level\": \"STANDARD\"," | 323 " \"response_detail_level\": \"STANDARD\"," |
320 " \"advanced_options\": {" | 324 " \"advanced_options\": {" |
321 " \"local_scoring_params\": {" | 325 " \"local_scoring_params\": {" |
322 " \"content_params\": {" | 326 " \"content_params\": {" |
323 " \"only_return_personalized_results\": false" | 327 " \"only_return_personalized_results\": false" |
324 " }," | 328 " }," |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 | 360 |
357 TEST_F(NTPSnippetsFetcherTest, BuildRequestExcludedIds) { | 361 TEST_F(NTPSnippetsFetcherTest, BuildRequestExcludedIds) { |
358 NTPSnippetsFetcher::RequestParams params; | 362 NTPSnippetsFetcher::RequestParams params; |
359 params.only_return_personalized_results = false; | 363 params.only_return_personalized_results = false; |
360 params.host_restricts = {}; | 364 params.host_restricts = {}; |
361 params.count_to_fetch = 10; | 365 params.count_to_fetch = 10; |
362 params.interactive_request = false; | 366 params.interactive_request = false; |
363 for (int i = 0; i < 200; ++i) { | 367 for (int i = 0; i < 200; ++i) { |
364 params.excluded_ids.insert(base::StringPrintf("%03d", i)); | 368 params.excluded_ids.insert(base::StringPrintf("%03d", i)); |
365 } | 369 } |
| 370 params.ui_language.frequency = 0.0f; |
| 371 params.other_top_language.frequency = 0.0f; |
366 | 372 |
367 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; | 373 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
368 EXPECT_THAT(params.BuildRequest(), | 374 EXPECT_THAT(params.BuildRequest(), |
369 EqualsJSON("{" | 375 EqualsJSON("{" |
370 " \"regularlyVisitedHostNames\": []," | 376 " \"regularlyVisitedHostNames\": []," |
371 " \"priority\": \"BACKGROUND_PREFETCH\"," | 377 " \"priority\": \"BACKGROUND_PREFETCH\"," |
372 " \"excludedSuggestionIds\": [" | 378 " \"excludedSuggestionIds\": [" |
373 " \"000\", \"001\", \"002\", \"003\", \"004\"," | 379 " \"000\", \"001\", \"002\", \"003\", \"004\"," |
374 " \"005\", \"006\", \"007\", \"008\", \"009\"," | 380 " \"005\", \"006\", \"007\", \"008\", \"009\"," |
375 " \"010\", \"011\", \"012\", \"013\", \"014\"," | 381 " \"010\", \"011\", \"012\", \"013\", \"014\"," |
(...skipping 13 matching lines...) Expand all Loading... |
389 " \"080\", \"081\", \"082\", \"083\", \"084\"," | 395 " \"080\", \"081\", \"082\", \"083\", \"084\"," |
390 " \"085\", \"086\", \"087\", \"088\", \"089\"," | 396 " \"085\", \"086\", \"087\", \"088\", \"089\"," |
391 " \"090\", \"091\", \"092\", \"093\", \"094\"," | 397 " \"090\", \"091\", \"092\", \"093\", \"094\"," |
392 " \"095\", \"096\", \"097\", \"098\", \"099\"" | 398 " \"095\", \"096\", \"097\", \"098\", \"099\"" |
393 // Truncated to 100 entries. Currently, they happen to | 399 // Truncated to 100 entries. Currently, they happen to |
394 // be those lexically first. | 400 // be those lexically first. |
395 " ]" | 401 " ]" |
396 "}")); | 402 "}")); |
397 } | 403 } |
398 | 404 |
| 405 TEST_F(NTPSnippetsFetcherTest, BuildRequestWithTwoLanguages) { |
| 406 NTPSnippetsFetcher::RequestParams params; |
| 407 params.only_return_personalized_results = false; |
| 408 params.host_restricts = {}; |
| 409 params.count_to_fetch = 10; |
| 410 params.interactive_request = true; |
| 411 params.ui_language.language_code = "en"; |
| 412 params.ui_language.frequency = 0.5f; |
| 413 params.other_top_language.language_code = "de"; |
| 414 params.other_top_language.frequency = 0.5f; |
| 415 |
| 416 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
| 417 EXPECT_THAT(params.BuildRequest(), |
| 418 EqualsJSON("{" |
| 419 " \"regularlyVisitedHostNames\": []," |
| 420 " \"priority\": \"USER_ACTION\"," |
| 421 " \"excludedSuggestionIds\": []," |
| 422 " \"top_languages\": [" |
| 423 " {" |
| 424 " \"language\" : \"en\"," |
| 425 " \"frequency\" : 0.5" |
| 426 " }," |
| 427 " {" |
| 428 " \"language\" : \"de\"," |
| 429 " \"frequency\" : 0.5" |
| 430 " }" |
| 431 " ]" |
| 432 "}")); |
| 433 } |
| 434 |
| 435 TEST_F(NTPSnippetsFetcherTest, BuildRequestWithUILanguageOnly) { |
| 436 NTPSnippetsFetcher::RequestParams params; |
| 437 params.only_return_personalized_results = false; |
| 438 params.host_restricts = {}; |
| 439 params.count_to_fetch = 10; |
| 440 params.interactive_request = true; |
| 441 params.ui_language.language_code = "en"; |
| 442 params.ui_language.frequency = 0.5f; |
| 443 params.other_top_language.frequency = 0.0f; |
| 444 |
| 445 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
| 446 EXPECT_THAT(params.BuildRequest(), |
| 447 EqualsJSON("{" |
| 448 " \"regularlyVisitedHostNames\": []," |
| 449 " \"priority\": \"USER_ACTION\"," |
| 450 " \"excludedSuggestionIds\": []," |
| 451 " \"top_languages\": [{" |
| 452 " \"language\" : \"en\"," |
| 453 " \"frequency\" : 0.5" |
| 454 " }]" |
| 455 "}")); |
| 456 } |
| 457 |
| 458 TEST_F(NTPSnippetsFetcherTest, BuildRequestWithOtherLanguageOnly) { |
| 459 NTPSnippetsFetcher::RequestParams params; |
| 460 params.only_return_personalized_results = false; |
| 461 params.host_restricts = {}; |
| 462 params.count_to_fetch = 10; |
| 463 params.interactive_request = true; |
| 464 params.ui_language.frequency = 0.0f; |
| 465 params.other_top_language.language_code = "de"; |
| 466 params.other_top_language.frequency = 0.5f; |
| 467 |
| 468 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
| 469 EXPECT_THAT(params.BuildRequest(), |
| 470 EqualsJSON("{" |
| 471 " \"regularlyVisitedHostNames\": []," |
| 472 " \"priority\": \"USER_ACTION\"," |
| 473 " \"excludedSuggestionIds\": []," |
| 474 " \"top_languages\": [{" |
| 475 " \"language\" : \"de\"," |
| 476 " \"frequency\" : 0.5" |
| 477 " }]" |
| 478 "}")); |
| 479 } |
| 480 |
399 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) { | 481 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) { |
400 // The lack of registered baked in responses would cause any fetch to fail. | 482 // The lack of registered baked in responses would cause any fetch to fail. |
401 FastForwardUntilNoTasksRemain(); | 483 FastForwardUntilNoTasksRemain(); |
402 EXPECT_THAT(histogram_tester().GetAllSamples( | 484 EXPECT_THAT(histogram_tester().GetAllSamples( |
403 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 485 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
404 IsEmpty()); | 486 IsEmpty()); |
405 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 487 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
406 IsEmpty()); | 488 IsEmpty()); |
407 EXPECT_THAT(snippets_fetcher().last_status(), IsEmpty()); | 489 EXPECT_THAT(snippets_fetcher().last_status(), IsEmpty()); |
408 } | 490 } |
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
772 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { | 854 const NTPSnippetsFetcher::OptionalFetchedCategories& fetched_categories) { |
773 if (fetched_categories) { | 855 if (fetched_categories) { |
774 // Matchers above aren't any more precise than this, so this is sufficient | 856 // Matchers above aren't any more precise than this, so this is sufficient |
775 // for test-failure diagnostics. | 857 // for test-failure diagnostics. |
776 return os << "list with " << fetched_categories->size() << " elements"; | 858 return os << "list with " << fetched_categories->size() << " elements"; |
777 } | 859 } |
778 return os << "null"; | 860 return os << "null"; |
779 } | 861 } |
780 | 862 |
781 } // namespace ntp_snippets | 863 } // namespace ntp_snippets |
OLD | NEW |