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