| 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/ntp_snippets_fetcher.h" | 5 #include "components/ntp_snippets/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 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 new net::TestURLRequestContextGetter(mock_task_runner_.get())), | 172 new net::TestURLRequestContextGetter(mock_task_runner_.get())), |
| 173 pref_service_.get(), &category_factory_, base::Bind(&ParseJsonDelayed), | 173 pref_service_.get(), &category_factory_, base::Bind(&ParseJsonDelayed), |
| 174 /*is_stable_channel=*/true); | 174 /*is_stable_channel=*/true); |
| 175 | 175 |
| 176 snippets_fetcher_->SetCallback( | 176 snippets_fetcher_->SetCallback( |
| 177 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, | 177 base::Bind(&MockSnippetsAvailableCallback::WrappedRun, |
| 178 base::Unretained(&mock_callback_))); | 178 base::Unretained(&mock_callback_))); |
| 179 snippets_fetcher_->SetTickClockForTesting( | 179 snippets_fetcher_->SetTickClockForTesting( |
| 180 mock_task_runner_->GetMockTickClock()); | 180 mock_task_runner_->GetMockTickClock()); |
| 181 test_hosts_.insert("www.somehost.com"); | 181 test_hosts_.insert("www.somehost.com"); |
| 182 test_excluded_.insert("1234567890"); |
| 182 // Increase initial time such that ticks are non-zero. | 183 // Increase initial time such that ticks are non-zero. |
| 183 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); | 184 mock_task_runner_->FastForwardBy(base::TimeDelta::FromMilliseconds(1234)); |
| 184 } | 185 } |
| 185 | 186 |
| 186 NTPSnippetsFetcher& snippets_fetcher() { return *snippets_fetcher_; } | 187 NTPSnippetsFetcher& snippets_fetcher() { return *snippets_fetcher_; } |
| 187 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; } | 188 MockSnippetsAvailableCallback& mock_callback() { return mock_callback_; } |
| 188 void FastForwardUntilNoTasksRemain() { | 189 void FastForwardUntilNoTasksRemain() { |
| 189 mock_task_runner_->FastForwardUntilNoTasksRemain(); | 190 mock_task_runner_->FastForwardUntilNoTasksRemain(); |
| 190 } | 191 } |
| 191 const std::string& test_lang() const { return test_lang_; } | 192 const std::string& test_lang() const { return test_lang_; } |
| 192 const GURL& test_url() { return test_url_; } | 193 const GURL& test_url() { return test_url_; } |
| 193 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_; } |
| 194 base::HistogramTester& histogram_tester() { return histogram_tester_; } | 196 base::HistogramTester& histogram_tester() { return histogram_tester_; } |
| 195 | 197 |
| 196 void InitFakeURLFetcherFactory() { | 198 void InitFakeURLFetcherFactory() { |
| 197 if (fake_url_fetcher_factory_) | 199 if (fake_url_fetcher_factory_) |
| 198 return; | 200 return; |
| 199 // Instantiation of factory automatically sets itself as URLFetcher's | 201 // Instantiation of factory automatically sets itself as URLFetcher's |
| 200 // factory. | 202 // factory. |
| 201 fake_url_fetcher_factory_.reset(new net::FakeURLFetcherFactory( | 203 fake_url_fetcher_factory_.reset(new net::FakeURLFetcherFactory( |
| 202 /*default_factory=*/&failing_url_fetcher_factory_)); | 204 /*default_factory=*/&failing_url_fetcher_factory_)); |
| 203 } | 205 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 221 std::unique_ptr<AccountTrackerService> account_tracker_; | 223 std::unique_ptr<AccountTrackerService> account_tracker_; |
| 222 std::unique_ptr<SigninManagerBase> fake_signin_manager_; | 224 std::unique_ptr<SigninManagerBase> fake_signin_manager_; |
| 223 std::unique_ptr<OAuth2TokenService> fake_token_service_; | 225 std::unique_ptr<OAuth2TokenService> fake_token_service_; |
| 224 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; | 226 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher_; |
| 225 std::unique_ptr<TestingPrefServiceSimple> pref_service_; | 227 std::unique_ptr<TestingPrefServiceSimple> pref_service_; |
| 226 CategoryFactory category_factory_; | 228 CategoryFactory category_factory_; |
| 227 MockSnippetsAvailableCallback mock_callback_; | 229 MockSnippetsAvailableCallback mock_callback_; |
| 228 const std::string test_lang_; | 230 const std::string test_lang_; |
| 229 const GURL test_url_; | 231 const GURL test_url_; |
| 230 std::set<std::string> test_hosts_; | 232 std::set<std::string> test_hosts_; |
| 233 std::set<std::string> test_excluded_; |
| 231 base::HistogramTester histogram_tester_; | 234 base::HistogramTester histogram_tester_; |
| 232 | 235 |
| 233 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); | 236 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsFetcherTest); |
| 234 }; | 237 }; |
| 235 | 238 |
| 236 class NTPSnippetsContentSuggestionsFetcherTest : public NTPSnippetsFetcherTest { | 239 class NTPSnippetsContentSuggestionsFetcherTest : public NTPSnippetsFetcherTest { |
| 237 public: | 240 public: |
| 238 NTPSnippetsContentSuggestionsFetcherTest() | 241 NTPSnippetsContentSuggestionsFetcherTest() |
| 239 : NTPSnippetsFetcherTest( | 242 : NTPSnippetsFetcherTest( |
| 240 GetFetcherUrl(kTestChromeContentSuggestionsUrlFormat), | 243 GetFetcherUrl(kTestChromeContentSuggestionsUrlFormat), |
| 241 {{"content_suggestions_backend", kContentSuggestionsServer}}) {} | 244 {{"content_suggestions_backend", kContentSuggestionsServer}}) {} |
| 242 }; | 245 }; |
| 243 | 246 |
| 244 class NTPSnippetsFetcherHostRestrictedTest : public NTPSnippetsFetcherTest { | 247 class NTPSnippetsFetcherHostRestrictedTest : public NTPSnippetsFetcherTest { |
| 245 public: | 248 public: |
| 246 NTPSnippetsFetcherHostRestrictedTest() | 249 NTPSnippetsFetcherHostRestrictedTest() |
| 247 : NTPSnippetsFetcherTest( | 250 : NTPSnippetsFetcherTest( |
| 248 GetFetcherUrl(kTestChromeReaderUrlFormat), | 251 GetFetcherUrl(kTestChromeReaderUrlFormat), |
| 249 {{"fetching_host_restrict", "on"}}) {} | 252 {{"fetching_host_restrict", "on"}}) {} |
| 250 }; | 253 }; |
| 251 | 254 |
| 252 TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) { | 255 TEST_F(NTPSnippetsFetcherTest, BuildRequestAuthenticated) { |
| 253 NTPSnippetsFetcher::RequestParams params; | 256 NTPSnippetsFetcher::RequestParams params; |
| 254 params.obfuscated_gaia_id = "0BFUSGAIA"; | 257 params.obfuscated_gaia_id = "0BFUSGAIA"; |
| 255 params.only_return_personalized_results = true; | 258 params.only_return_personalized_results = true; |
| 256 params.user_locale = "en"; | 259 params.user_locale = "en"; |
| 257 params.host_restricts = {"chromium.org"}; | 260 params.host_restricts = {"chromium.org"}; |
| 261 params.excluded_ids = {"1234567890"}; |
| 258 params.count_to_fetch = 25; | 262 params.count_to_fetch = 25; |
| 259 | 263 |
| 260 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; | 264 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; |
| 261 EXPECT_THAT(params.BuildRequest(), | 265 EXPECT_THAT(params.BuildRequest(), |
| 262 EqualsJSON("{" | 266 EqualsJSON("{" |
| 263 " \"response_detail_level\": \"STANDARD\"," | 267 " \"response_detail_level\": \"STANDARD\"," |
| 264 " \"obfuscated_gaia_id\": \"0BFUSGAIA\"," | 268 " \"obfuscated_gaia_id\": \"0BFUSGAIA\"," |
| 265 " \"user_locale\": \"en\"," | 269 " \"user_locale\": \"en\"," |
| 266 " \"advanced_options\": {" | 270 " \"advanced_options\": {" |
| 267 " \"local_scoring_params\": {" | 271 " \"local_scoring_params\": {" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 295 " }" | 299 " }" |
| 296 " }" | 300 " }" |
| 297 "}")); | 301 "}")); |
| 298 | 302 |
| 299 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; | 303 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
| 300 EXPECT_THAT(params.BuildRequest(), | 304 EXPECT_THAT(params.BuildRequest(), |
| 301 EqualsJSON("{" | 305 EqualsJSON("{" |
| 302 " \"uiLanguage\": \"en\"," | 306 " \"uiLanguage\": \"en\"," |
| 303 " \"regularlyVisitedHostNames\": [" | 307 " \"regularlyVisitedHostNames\": [" |
| 304 " \"chromium.org\"" | 308 " \"chromium.org\"" |
| 309 " ]," |
| 310 " \"excludedSuggestionIds\": [" |
| 311 " \"1234567890\"" |
| 305 " ]" | 312 " ]" |
| 306 "}")); | 313 "}")); |
| 307 } | 314 } |
| 308 | 315 |
| 309 TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) { | 316 TEST_F(NTPSnippetsFetcherTest, BuildRequestUnauthenticated) { |
| 310 NTPSnippetsFetcher::RequestParams params; | 317 NTPSnippetsFetcher::RequestParams params; |
| 311 params.only_return_personalized_results = false; | 318 params.only_return_personalized_results = false; |
| 312 params.host_restricts = {}; | 319 params.host_restricts = {}; |
| 313 params.count_to_fetch = 10; | 320 params.count_to_fetch = 10; |
| 321 params.excluded_ids = {}; |
| 314 | 322 |
| 315 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; | 323 params.fetch_api = NTPSnippetsFetcher::CHROME_READER_API; |
| 316 EXPECT_THAT(params.BuildRequest(), | 324 EXPECT_THAT(params.BuildRequest(), |
| 317 EqualsJSON("{" | 325 EqualsJSON("{" |
| 318 " \"response_detail_level\": \"STANDARD\"," | 326 " \"response_detail_level\": \"STANDARD\"," |
| 319 " \"advanced_options\": {" | 327 " \"advanced_options\": {" |
| 320 " \"local_scoring_params\": {" | 328 " \"local_scoring_params\": {" |
| 321 " \"content_params\": {" | 329 " \"content_params\": {" |
| 322 " \"only_return_personalized_results\": false" | 330 " \"only_return_personalized_results\": false" |
| 323 " }," | 331 " }," |
| (...skipping 16 matching lines...) Expand all Loading... |
| 340 " \"global_scoring_params\": {" | 348 " \"global_scoring_params\": {" |
| 341 " \"num_to_return\": 10," | 349 " \"num_to_return\": 10," |
| 342 " \"sort_type\": 1" | 350 " \"sort_type\": 1" |
| 343 " }" | 351 " }" |
| 344 " }" | 352 " }" |
| 345 "}")); | 353 "}")); |
| 346 | 354 |
| 347 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; | 355 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
| 348 EXPECT_THAT(params.BuildRequest(), | 356 EXPECT_THAT(params.BuildRequest(), |
| 349 EqualsJSON("{" | 357 EqualsJSON("{" |
| 350 " \"regularlyVisitedHostNames\": []" | 358 " \"regularlyVisitedHostNames\": []," |
| 359 " \"excludedSuggestionIds\": []" |
| 351 "}")); | 360 "}")); |
| 352 } | 361 } |
| 353 | 362 |
| 363 TEST_F(NTPSnippetsFetcherTest, BuildRequestExcludedIds) { |
| 364 NTPSnippetsFetcher::RequestParams params; |
| 365 params.only_return_personalized_results = false; |
| 366 params.host_restricts = {}; |
| 367 params.count_to_fetch = 10; |
| 368 for (int i = 0; i < 200; ++i) { |
| 369 params.excluded_ids.insert(base::StringPrintf("%03d", i)); |
| 370 } |
| 371 |
| 372 params.fetch_api = NTPSnippetsFetcher::CHROME_CONTENT_SUGGESTIONS_API; |
| 373 EXPECT_THAT(params.BuildRequest(), |
| 374 EqualsJSON("{" |
| 375 " \"regularlyVisitedHostNames\": []," |
| 376 " \"excludedSuggestionIds\": [" |
| 377 " \"000\", \"001\", \"002\", \"003\", \"004\"," |
| 378 " \"005\", \"006\", \"007\", \"008\", \"009\"," |
| 379 " \"010\", \"011\", \"012\", \"013\", \"014\"," |
| 380 " \"015\", \"016\", \"017\", \"018\", \"019\"," |
| 381 " \"020\", \"021\", \"022\", \"023\", \"024\"," |
| 382 " \"025\", \"026\", \"027\", \"028\", \"029\"," |
| 383 " \"030\", \"031\", \"032\", \"033\", \"034\"," |
| 384 " \"035\", \"036\", \"037\", \"038\", \"039\"," |
| 385 " \"040\", \"041\", \"042\", \"043\", \"044\"," |
| 386 " \"045\", \"046\", \"047\", \"048\", \"049\"," |
| 387 " \"050\", \"051\", \"052\", \"053\", \"054\"," |
| 388 " \"055\", \"056\", \"057\", \"058\", \"059\"," |
| 389 " \"060\", \"061\", \"062\", \"063\", \"064\"," |
| 390 " \"065\", \"066\", \"067\", \"068\", \"069\"," |
| 391 " \"070\", \"071\", \"072\", \"073\", \"074\"," |
| 392 " \"075\", \"076\", \"077\", \"078\", \"079\"," |
| 393 " \"080\", \"081\", \"082\", \"083\", \"084\"," |
| 394 " \"085\", \"086\", \"087\", \"088\", \"089\"," |
| 395 " \"090\", \"091\", \"092\", \"093\", \"094\"," |
| 396 " \"095\", \"096\", \"097\", \"098\", \"099\"" |
| 397 // Truncated to 100 entries. Currently, they happen to |
| 398 // be those lexically first. |
| 399 " ]" |
| 400 "}")); |
| 401 } |
| 402 |
| 354 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) { | 403 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) { |
| 355 // The lack of registered baked in responses would cause any fetch to fail. | 404 // The lack of registered baked in responses would cause any fetch to fail. |
| 356 FastForwardUntilNoTasksRemain(); | 405 FastForwardUntilNoTasksRemain(); |
| 357 EXPECT_THAT(histogram_tester().GetAllSamples( | 406 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 358 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 407 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 359 IsEmpty()); | 408 IsEmpty()); |
| 360 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 409 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 361 IsEmpty()); | 410 IsEmpty()); |
| 362 EXPECT_THAT(snippets_fetcher().last_status(), IsEmpty()); | 411 EXPECT_THAT(snippets_fetcher().last_status(), IsEmpty()); |
| 363 } | 412 } |
| 364 | 413 |
| 365 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfully) { | 414 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfully) { |
| 366 const std::string kJsonStr = | 415 const std::string kJsonStr = |
| 367 "{\"recos\": [{" | 416 "{\"recos\": [{" |
| 368 " \"contentInfo\": {" | 417 " \"contentInfo\": {" |
| 369 " \"url\" : \"http://localhost/foobar\"," | 418 " \"url\" : \"http://localhost/foobar\"," |
| 370 " \"sourceCorpusInfo\" : [{" | 419 " \"sourceCorpusInfo\" : [{" |
| 371 " \"ampUrl\" : \"http://localhost/amp\"," | 420 " \"ampUrl\" : \"http://localhost/amp\"," |
| 372 " \"corpusId\" : \"http://localhost/foobar\"," | 421 " \"corpusId\" : \"http://localhost/foobar\"," |
| 373 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" | 422 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" |
| 374 " }]" | 423 " }]" |
| 375 " }" | 424 " }" |
| 376 "}]}"; | 425 "}]}"; |
| 377 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 426 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, |
| 378 net::URLRequestStatus::SUCCESS); | 427 net::URLRequestStatus::SUCCESS); |
| 379 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); | 428 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); |
| 380 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 429 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 430 test_excluded(), |
| 381 /*count=*/1, | 431 /*count=*/1, |
| 382 /*interactive_request=*/true); | 432 /*interactive_request=*/true); |
| 383 FastForwardUntilNoTasksRemain(); | 433 FastForwardUntilNoTasksRemain(); |
| 384 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 434 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
| 385 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 435 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 386 EXPECT_THAT(histogram_tester().GetAllSamples( | 436 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 387 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 437 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 388 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 438 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 389 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 439 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 390 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 440 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 406 " \"attribution\" : \"Foo News\"," | 456 " \"attribution\" : \"Foo News\"," |
| 407 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," | 457 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," |
| 408 " \"ampUrl\" : \"http://localhost/amp\"," | 458 " \"ampUrl\" : \"http://localhost/amp\"," |
| 409 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " | 459 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " |
| 410 " }]" | 460 " }]" |
| 411 "}]}"; | 461 "}]}"; |
| 412 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 462 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, |
| 413 net::URLRequestStatus::SUCCESS); | 463 net::URLRequestStatus::SUCCESS); |
| 414 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); | 464 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); |
| 415 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 465 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 466 test_excluded(), |
| 416 /*count=*/1, | 467 /*count=*/1, |
| 417 /*interactive_request=*/true); | 468 /*interactive_request=*/true); |
| 418 FastForwardUntilNoTasksRemain(); | 469 FastForwardUntilNoTasksRemain(); |
| 419 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 470 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
| 420 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 471 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 421 EXPECT_THAT(histogram_tester().GetAllSamples( | 472 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 422 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 473 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 423 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 474 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 424 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 475 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 425 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 476 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
| 426 /*count=*/1))); | 477 /*count=*/1))); |
| 427 } | 478 } |
| 428 | 479 |
| 429 TEST_F(NTPSnippetsContentSuggestionsFetcherTest, EmptyCategoryIsOK) { | 480 TEST_F(NTPSnippetsContentSuggestionsFetcherTest, EmptyCategoryIsOK) { |
| 430 const std::string kJsonStr = | 481 const std::string kJsonStr = |
| 431 "{\"categories\" : [{" | 482 "{\"categories\" : [{" |
| 432 " \"id\": 1," | 483 " \"id\": 1," |
| 433 " \"localizedTitle\": \"Articles for You\"" | 484 " \"localizedTitle\": \"Articles for You\"" |
| 434 "}]}"; | 485 "}]}"; |
| 435 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 486 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, |
| 436 net::URLRequestStatus::SUCCESS); | 487 net::URLRequestStatus::SUCCESS); |
| 437 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); | 488 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
| 438 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 489 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 490 test_excluded(), |
| 439 /*count=*/1, | 491 /*count=*/1, |
| 440 /*interactive_request=*/true); | 492 /*interactive_request=*/true); |
| 441 FastForwardUntilNoTasksRemain(); | 493 FastForwardUntilNoTasksRemain(); |
| 442 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 494 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
| 443 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 495 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 444 EXPECT_THAT(histogram_tester().GetAllSamples( | 496 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 445 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 497 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 446 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 498 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 447 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 499 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 448 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 500 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 " \"ampUrl\" : \"http://localhost/amp\"," | 533 " \"ampUrl\" : \"http://localhost/amp\"," |
| 482 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " | 534 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " |
| 483 " }]" | 535 " }]" |
| 484 "}]}"; | 536 "}]}"; |
| 485 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 537 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, |
| 486 net::URLRequestStatus::SUCCESS); | 538 net::URLRequestStatus::SUCCESS); |
| 487 NTPSnippetsFetcher::OptionalSnippets snippets; | 539 NTPSnippetsFetcher::OptionalSnippets snippets; |
| 488 EXPECT_CALL(mock_callback(), Run(_)) | 540 EXPECT_CALL(mock_callback(), Run(_)) |
| 489 .WillOnce(WithArg<0>(MovePointeeTo(&snippets))); | 541 .WillOnce(WithArg<0>(MovePointeeTo(&snippets))); |
| 490 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 542 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 543 test_excluded(), |
| 491 /*count=*/1, | 544 /*count=*/1, |
| 492 /*force_request=*/true); | 545 /*force_request=*/true); |
| 493 FastForwardUntilNoTasksRemain(); | 546 FastForwardUntilNoTasksRemain(); |
| 494 | 547 |
| 495 ASSERT_TRUE(snippets); | 548 ASSERT_TRUE(snippets); |
| 496 ASSERT_THAT(snippets->size(), Eq(2u)); | 549 ASSERT_THAT(snippets->size(), Eq(2u)); |
| 497 for (const auto& category : *snippets) { | 550 for (const auto& category : *snippets) { |
| 498 const auto& articles = category.second; | 551 const auto& articles = category.second; |
| 499 switch (category.first.id()) { | 552 switch (category.first.id()) { |
| 500 case static_cast<int>(KnownCategories::ARTICLES): | 553 case static_cast<int>(KnownCategories::ARTICLES): |
| (...skipping 20 matching lines...) Expand all Loading... |
| 521 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 574 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
| 522 /*count=*/1))); | 575 /*count=*/1))); |
| 523 } | 576 } |
| 524 | 577 |
| 525 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { | 578 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { |
| 526 const std::string kJsonStr = "{\"recos\": []}"; | 579 const std::string kJsonStr = "{\"recos\": []}"; |
| 527 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 580 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, |
| 528 net::URLRequestStatus::SUCCESS); | 581 net::URLRequestStatus::SUCCESS); |
| 529 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); | 582 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
| 530 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 583 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 584 test_excluded(), |
| 531 /*count=*/1, | 585 /*count=*/1, |
| 532 /*interactive_request=*/true); | 586 /*interactive_request=*/true); |
| 533 FastForwardUntilNoTasksRemain(); | 587 FastForwardUntilNoTasksRemain(); |
| 534 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); | 588 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); |
| 535 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 589 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 536 EXPECT_THAT( | 590 EXPECT_THAT( |
| 537 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 591 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 538 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); | 592 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); |
| 539 EXPECT_THAT(histogram_tester().GetAllSamples( | 593 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 540 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 594 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 541 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 595 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 542 } | 596 } |
| 543 | 597 |
| 544 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldReportEmptyHostsError) { | 598 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldReportEmptyHostsError) { |
| 545 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 599 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 546 snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(), | 600 snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(), |
| 547 /*language_code=*/"en-US", | 601 /*language_code=*/"en-US", |
| 602 test_excluded(), |
| 548 /*count=*/1, | 603 /*count=*/1, |
| 549 /*interactive_request=*/true); | 604 /*interactive_request=*/true); |
| 550 FastForwardUntilNoTasksRemain(); | 605 FastForwardUntilNoTasksRemain(); |
| 551 EXPECT_THAT(snippets_fetcher().last_status(), | 606 EXPECT_THAT(snippets_fetcher().last_status(), |
| 552 Eq("Cannot fetch for empty hosts list.")); | 607 Eq("Cannot fetch for empty hosts list.")); |
| 553 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); | 608 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
| 554 EXPECT_THAT( | 609 EXPECT_THAT( |
| 555 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 610 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 556 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); | 611 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); |
| 557 // This particular error gets triggered prior to fetching, so no fetch time | 612 // This particular error gets triggered prior to fetching, so no fetch time |
| 558 // or response should get recorded. | 613 // or response should get recorded. |
| 559 EXPECT_THAT(histogram_tester().GetAllSamples( | 614 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 560 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 615 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 561 IsEmpty()); | 616 IsEmpty()); |
| 562 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 617 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 563 IsEmpty()); | 618 IsEmpty()); |
| 564 } | 619 } |
| 565 | 620 |
| 566 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldRestrictToHosts) { | 621 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldRestrictToHosts) { |
| 567 net::TestURLFetcherFactory test_url_fetcher_factory; | 622 net::TestURLFetcherFactory test_url_fetcher_factory; |
| 568 snippets_fetcher().FetchSnippetsFromHosts( | 623 snippets_fetcher().FetchSnippetsFromHosts( |
| 569 {"www.somehost1.com", "www.somehost2.com"}, test_lang(), /*count=*/17, | 624 {"www.somehost1.com", "www.somehost2.com"}, test_lang(), test_excluded(), |
| 625 /*count=*/17, |
| 570 /*interactive_request=*/true); | 626 /*interactive_request=*/true); |
| 571 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); | 627 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); |
| 572 ASSERT_THAT(fetcher, NotNull()); | 628 ASSERT_THAT(fetcher, NotNull()); |
| 573 std::unique_ptr<base::Value> value = | 629 std::unique_ptr<base::Value> value = |
| 574 base::JSONReader::Read(fetcher->upload_data()); | 630 base::JSONReader::Read(fetcher->upload_data()); |
| 575 ASSERT_TRUE(value) << " failed to parse JSON: " | 631 ASSERT_TRUE(value) << " failed to parse JSON: " |
| 576 << PrintToString(fetcher->upload_data()); | 632 << PrintToString(fetcher->upload_data()); |
| 577 const base::DictionaryValue* dict = nullptr; | 633 const base::DictionaryValue* dict = nullptr; |
| 578 ASSERT_TRUE(value->GetAsDictionary(&dict)); | 634 ASSERT_TRUE(value->GetAsDictionary(&dict)); |
| 579 const base::DictionaryValue* local_scoring_params = nullptr; | 635 const base::DictionaryValue* local_scoring_params = nullptr; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 591 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector)); | 647 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector)); |
| 592 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); | 648 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); |
| 593 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com")); | 649 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com")); |
| 594 } | 650 } |
| 595 | 651 |
| 596 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { | 652 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { |
| 597 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, | 653 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, |
| 598 net::URLRequestStatus::FAILED); | 654 net::URLRequestStatus::FAILED); |
| 599 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 655 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 600 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 656 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 657 test_excluded(), |
| 601 /*count=*/1, | 658 /*count=*/1, |
| 602 /*interactive_request=*/true); | 659 /*interactive_request=*/true); |
| 603 FastForwardUntilNoTasksRemain(); | 660 FastForwardUntilNoTasksRemain(); |
| 604 EXPECT_THAT(snippets_fetcher().last_status(), | 661 EXPECT_THAT(snippets_fetcher().last_status(), |
| 605 Eq("URLRequestStatus error -2")); | 662 Eq("URLRequestStatus error -2")); |
| 606 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); | 663 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
| 607 EXPECT_THAT( | 664 EXPECT_THAT( |
| 608 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 665 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 609 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); | 666 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); |
| 610 EXPECT_THAT(histogram_tester().GetAllSamples( | 667 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 611 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 668 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 612 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); | 669 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); |
| 613 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 670 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 614 Not(IsEmpty())); | 671 Not(IsEmpty())); |
| 615 } | 672 } |
| 616 | 673 |
| 617 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { | 674 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { |
| 618 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, | 675 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, |
| 619 net::URLRequestStatus::SUCCESS); | 676 net::URLRequestStatus::SUCCESS); |
| 620 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 677 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 621 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 678 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 679 test_excluded(), |
| 622 /*count=*/1, | 680 /*count=*/1, |
| 623 /*interactive_request=*/true); | 681 /*interactive_request=*/true); |
| 624 FastForwardUntilNoTasksRemain(); | 682 FastForwardUntilNoTasksRemain(); |
| 625 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); | 683 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); |
| 626 EXPECT_THAT( | 684 EXPECT_THAT( |
| 627 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 685 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 628 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); | 686 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); |
| 629 EXPECT_THAT(histogram_tester().GetAllSamples( | 687 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 630 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 688 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 631 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); | 689 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); |
| 632 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 690 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 633 Not(IsEmpty())); | 691 Not(IsEmpty())); |
| 634 } | 692 } |
| 635 | 693 |
| 636 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { | 694 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { |
| 637 const std::string kInvalidJsonStr = "{ \"recos\": []"; | 695 const std::string kInvalidJsonStr = "{ \"recos\": []"; |
| 638 SetFakeResponse(/*data=*/kInvalidJsonStr, net::HTTP_OK, | 696 SetFakeResponse(/*data=*/kInvalidJsonStr, net::HTTP_OK, |
| 639 net::URLRequestStatus::SUCCESS); | 697 net::URLRequestStatus::SUCCESS); |
| 640 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 698 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 641 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 699 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 700 test_excluded(), |
| 642 /*count=*/1, | 701 /*count=*/1, |
| 643 /*interactive_request=*/true); | 702 /*interactive_request=*/true); |
| 644 FastForwardUntilNoTasksRemain(); | 703 FastForwardUntilNoTasksRemain(); |
| 645 EXPECT_THAT(snippets_fetcher().last_status(), | 704 EXPECT_THAT(snippets_fetcher().last_status(), |
| 646 StartsWith("Received invalid JSON (error ")); | 705 StartsWith("Received invalid JSON (error ")); |
| 647 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); | 706 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); |
| 648 EXPECT_THAT( | 707 EXPECT_THAT( |
| 649 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 708 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 650 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); | 709 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); |
| 651 EXPECT_THAT(histogram_tester().GetAllSamples( | 710 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 652 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 711 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 653 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 712 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 654 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 713 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 655 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 714 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
| 656 /*count=*/1))); | 715 /*count=*/1))); |
| 657 } | 716 } |
| 658 | 717 |
| 659 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { | 718 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { |
| 660 SetFakeResponse(/*data=*/std::string(), net::HTTP_OK, | 719 SetFakeResponse(/*data=*/std::string(), net::HTTP_OK, |
| 661 net::URLRequestStatus::SUCCESS); | 720 net::URLRequestStatus::SUCCESS); |
| 662 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 721 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 663 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 722 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 723 test_excluded(), |
| 664 /*count=*/1, | 724 /*count=*/1, |
| 665 /*interactive_request=*/true); | 725 /*interactive_request=*/true); |
| 666 FastForwardUntilNoTasksRemain(); | 726 FastForwardUntilNoTasksRemain(); |
| 667 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); | 727 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); |
| 668 EXPECT_THAT( | 728 EXPECT_THAT( |
| 669 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 729 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 670 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); | 730 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); |
| 671 EXPECT_THAT(histogram_tester().GetAllSamples( | 731 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 672 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 732 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 673 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 733 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 674 } | 734 } |
| 675 | 735 |
| 676 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { | 736 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { |
| 677 const std::string kJsonStr = | 737 const std::string kJsonStr = |
| 678 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; | 738 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; |
| 679 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 739 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, |
| 680 net::URLRequestStatus::SUCCESS); | 740 net::URLRequestStatus::SUCCESS); |
| 681 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 741 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 682 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 742 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 743 test_excluded(), |
| 683 /*count=*/1, | 744 /*count=*/1, |
| 684 /*interactive_request=*/true); | 745 /*interactive_request=*/true); |
| 685 FastForwardUntilNoTasksRemain(); | 746 FastForwardUntilNoTasksRemain(); |
| 686 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); | 747 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); |
| 687 EXPECT_THAT( | 748 EXPECT_THAT( |
| 688 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 749 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 689 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1))); | 750 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1))); |
| 690 EXPECT_THAT(histogram_tester().GetAllSamples( | 751 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 691 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 752 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 692 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 753 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 693 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 754 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 694 Not(IsEmpty())); | 755 Not(IsEmpty())); |
| 695 } | 756 } |
| 696 | 757 |
| 697 // This test actually verifies that the test setup itself is sane, to prevent | 758 // This test actually verifies that the test setup itself is sane, to prevent |
| 698 // hard-to-reproduce test failures. | 759 // hard-to-reproduce test failures. |
| 699 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { | 760 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { |
| 700 InitFakeURLFetcherFactory(); | 761 InitFakeURLFetcherFactory(); |
| 701 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); | 762 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); |
| 702 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 763 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 764 test_excluded(), |
| 703 /*count=*/1, | 765 /*count=*/1, |
| 704 /*interactive_request=*/true); | 766 /*interactive_request=*/true); |
| 705 FastForwardUntilNoTasksRemain(); | 767 FastForwardUntilNoTasksRemain(); |
| 706 } | 768 } |
| 707 | 769 |
| 708 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { | 770 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { |
| 709 const std::string kJsonStr = "{ \"recos\": [] }"; | 771 const std::string kJsonStr = "{ \"recos\": [] }"; |
| 710 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, | 772 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, |
| 711 net::URLRequestStatus::SUCCESS); | 773 net::URLRequestStatus::SUCCESS); |
| 712 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); | 774 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); |
| 713 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 775 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 776 test_excluded(), |
| 714 /*count=*/1, | 777 /*count=*/1, |
| 715 /*interactive_request=*/true); | 778 /*interactive_request=*/true); |
| 716 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous. | 779 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous. |
| 717 // Callback is expected to be called once. | 780 // Callback is expected to be called once. |
| 718 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), | 781 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), |
| 782 test_excluded(), |
| 719 /*count=*/1, | 783 /*count=*/1, |
| 720 /*interactive_request=*/true); | 784 /*interactive_request=*/true); |
| 721 FastForwardUntilNoTasksRemain(); | 785 FastForwardUntilNoTasksRemain(); |
| 722 EXPECT_THAT( | 786 EXPECT_THAT( |
| 723 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), | 787 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), |
| 724 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); | 788 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); |
| 725 EXPECT_THAT(histogram_tester().GetAllSamples( | 789 EXPECT_THAT(histogram_tester().GetAllSamples( |
| 726 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), | 790 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), |
| 727 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); | 791 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); |
| 728 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), | 792 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), |
| 729 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, | 793 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, |
| 730 /*count=*/1))); | 794 /*count=*/1))); |
| 731 } | 795 } |
| 732 | 796 |
| 733 ::std::ostream& operator<<( | 797 ::std::ostream& operator<<( |
| 734 ::std::ostream& os, | 798 ::std::ostream& os, |
| 735 const NTPSnippetsFetcher::OptionalSnippets& snippets) { | 799 const NTPSnippetsFetcher::OptionalSnippets& snippets) { |
| 736 if (snippets) { | 800 if (snippets) { |
| 737 // Matchers above aren't any more precise than this, so this is sufficient | 801 // Matchers above aren't any more precise than this, so this is sufficient |
| 738 // for test-failure diagnostics. | 802 // for test-failure diagnostics. |
| 739 return os << "list with " << snippets->size() << " elements"; | 803 return os << "list with " << snippets->size() << " elements"; |
| 740 } else { | 804 } else { |
| 741 return os << "null"; | 805 return os << "null"; |
| 742 } | 806 } |
| 743 } | 807 } |
| 744 | 808 |
| 745 } // namespace ntp_snippets | 809 } // namespace ntp_snippets |
| OLD | NEW |