Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(25)

Side by Side Diff: components/ntp_snippets/ntp_snippets_fetcher_unittest.cc

Issue 2274953002: Inform server of dismissed articles. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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");
tschumann 2016/08/24 16:56:19 nit: ids are now in hex format -- might be nice to
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
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
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
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
354 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) { 363 TEST_F(NTPSnippetsFetcherTest, ShouldNotFetchOnCreation) {
355 // The lack of registered baked in responses would cause any fetch to fail. 364 // The lack of registered baked in responses would cause any fetch to fail.
356 FastForwardUntilNoTasksRemain(); 365 FastForwardUntilNoTasksRemain();
357 EXPECT_THAT(histogram_tester().GetAllSamples( 366 EXPECT_THAT(histogram_tester().GetAllSamples(
358 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 367 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
359 IsEmpty()); 368 IsEmpty());
360 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 369 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
(...skipping 10 matching lines...) Expand all
371 " \"ampUrl\" : \"http://localhost/amp\"," 380 " \"ampUrl\" : \"http://localhost/amp\","
372 " \"corpusId\" : \"http://localhost/foobar\"," 381 " \"corpusId\" : \"http://localhost/foobar\","
373 " \"publisherData\": { \"sourceName\" : \"Foo News\" }" 382 " \"publisherData\": { \"sourceName\" : \"Foo News\" }"
374 " }]" 383 " }]"
375 " }" 384 " }"
376 "}]}"; 385 "}]}";
377 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 386 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
378 net::URLRequestStatus::SUCCESS); 387 net::URLRequestStatus::SUCCESS);
379 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); 388 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar")));
380 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 389 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
390 test_excluded(),
381 /*count=*/1, 391 /*count=*/1,
382 /*interactive_request=*/true); 392 /*interactive_request=*/true);
383 FastForwardUntilNoTasksRemain(); 393 FastForwardUntilNoTasksRemain();
384 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 394 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
385 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 395 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
386 EXPECT_THAT(histogram_tester().GetAllSamples( 396 EXPECT_THAT(histogram_tester().GetAllSamples(
387 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 397 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
388 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 398 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
389 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 399 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
390 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 400 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
(...skipping 15 matching lines...) Expand all
406 " \"attribution\" : \"Foo News\"," 416 " \"attribution\" : \"Foo News\","
407 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," 417 " \"imageUrl\" : \"http://localhost/foobar.jpg\","
408 " \"ampUrl\" : \"http://localhost/amp\"," 418 " \"ampUrl\" : \"http://localhost/amp\","
409 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " 419 " \"faviconUrl\" : \"http://localhost/favicon.ico\" "
410 " }]" 420 " }]"
411 "}]}"; 421 "}]}";
412 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 422 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
413 net::URLRequestStatus::SUCCESS); 423 net::URLRequestStatus::SUCCESS);
414 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar"))); 424 EXPECT_CALL(mock_callback(), Run(IsSingleArticle("http://localhost/foobar")));
415 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 425 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
426 test_excluded(),
416 /*count=*/1, 427 /*count=*/1,
417 /*interactive_request=*/true); 428 /*interactive_request=*/true);
418 FastForwardUntilNoTasksRemain(); 429 FastForwardUntilNoTasksRemain();
419 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 430 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
420 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 431 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
421 EXPECT_THAT(histogram_tester().GetAllSamples( 432 EXPECT_THAT(histogram_tester().GetAllSamples(
422 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 433 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
423 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 434 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
424 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 435 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
425 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 436 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 " \"ampUrl\" : \"http://localhost/amp\"," 469 " \"ampUrl\" : \"http://localhost/amp\","
459 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " 470 " \"faviconUrl\" : \"http://localhost/favicon.ico\" "
460 " }]" 471 " }]"
461 "}]}"; 472 "}]}";
462 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 473 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
463 net::URLRequestStatus::SUCCESS); 474 net::URLRequestStatus::SUCCESS);
464 NTPSnippetsFetcher::OptionalSnippets snippets; 475 NTPSnippetsFetcher::OptionalSnippets snippets;
465 EXPECT_CALL(mock_callback(), Run(_)) 476 EXPECT_CALL(mock_callback(), Run(_))
466 .WillOnce(WithArg<0>(MovePointeeTo(&snippets))); 477 .WillOnce(WithArg<0>(MovePointeeTo(&snippets)));
467 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 478 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
479 test_excluded(),
468 /*count=*/1, 480 /*count=*/1,
469 /*force_request=*/true); 481 /*force_request=*/true);
470 FastForwardUntilNoTasksRemain(); 482 FastForwardUntilNoTasksRemain();
471 483
472 ASSERT_TRUE(snippets); 484 ASSERT_TRUE(snippets);
473 ASSERT_THAT(snippets->size(), Eq(2u)); 485 ASSERT_THAT(snippets->size(), Eq(2u));
474 for (const auto& category : *snippets) { 486 for (const auto& category : *snippets) {
475 const auto& articles = category.second; 487 const auto& articles = category.second;
476 switch (category.first.id()) { 488 switch (category.first.id()) {
477 case static_cast<int>(KnownCategories::ARTICLES): 489 case static_cast<int>(KnownCategories::ARTICLES):
(...skipping 20 matching lines...) Expand all
498 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 510 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
499 /*count=*/1))); 511 /*count=*/1)));
500 } 512 }
501 513
502 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) { 514 TEST_F(NTPSnippetsFetcherTest, ShouldFetchSuccessfullyEmptyList) {
503 const std::string kJsonStr = "{\"recos\": []}"; 515 const std::string kJsonStr = "{\"recos\": []}";
504 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 516 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
505 net::URLRequestStatus::SUCCESS); 517 net::URLRequestStatus::SUCCESS);
506 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); 518 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList()));
507 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 519 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
520 test_excluded(),
508 /*count=*/1, 521 /*count=*/1,
509 /*interactive_request=*/true); 522 /*interactive_request=*/true);
510 FastForwardUntilNoTasksRemain(); 523 FastForwardUntilNoTasksRemain();
511 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK")); 524 EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
512 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 525 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
513 EXPECT_THAT( 526 EXPECT_THAT(
514 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 527 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
515 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); 528 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
516 EXPECT_THAT(histogram_tester().GetAllSamples( 529 EXPECT_THAT(histogram_tester().GetAllSamples(
517 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 530 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
518 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 531 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
519 } 532 }
520 533
521 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldReportEmptyHostsError) { 534 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldReportEmptyHostsError) {
522 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 535 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
523 snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(), 536 snippets_fetcher().FetchSnippetsFromHosts(/*hosts=*/std::set<std::string>(),
524 /*language_code=*/"en-US", 537 /*language_code=*/"en-US",
538 test_excluded(),
525 /*count=*/1, 539 /*count=*/1,
526 /*interactive_request=*/true); 540 /*interactive_request=*/true);
527 FastForwardUntilNoTasksRemain(); 541 FastForwardUntilNoTasksRemain();
528 EXPECT_THAT(snippets_fetcher().last_status(), 542 EXPECT_THAT(snippets_fetcher().last_status(),
529 Eq("Cannot fetch for empty hosts list.")); 543 Eq("Cannot fetch for empty hosts list."));
530 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 544 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
531 EXPECT_THAT( 545 EXPECT_THAT(
532 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 546 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
533 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); 547 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1)));
534 // This particular error gets triggered prior to fetching, so no fetch time 548 // This particular error gets triggered prior to fetching, so no fetch time
535 // or response should get recorded. 549 // or response should get recorded.
536 EXPECT_THAT(histogram_tester().GetAllSamples( 550 EXPECT_THAT(histogram_tester().GetAllSamples(
537 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 551 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
538 IsEmpty()); 552 IsEmpty());
539 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 553 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
540 IsEmpty()); 554 IsEmpty());
541 } 555 }
542 556
543 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldRestrictToHosts) { 557 TEST_F(NTPSnippetsFetcherHostRestrictedTest, ShouldRestrictToHosts) {
544 net::TestURLFetcherFactory test_url_fetcher_factory; 558 net::TestURLFetcherFactory test_url_fetcher_factory;
545 snippets_fetcher().FetchSnippetsFromHosts( 559 snippets_fetcher().FetchSnippetsFromHosts(
546 {"www.somehost1.com", "www.somehost2.com"}, test_lang(), /*count=*/17, 560 {"www.somehost1.com", "www.somehost2.com"}, test_lang(), test_excluded(),
561 /*count=*/17,
547 /*interactive_request=*/true); 562 /*interactive_request=*/true);
548 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0); 563 net::TestURLFetcher* fetcher = test_url_fetcher_factory.GetFetcherByID(0);
549 ASSERT_THAT(fetcher, NotNull()); 564 ASSERT_THAT(fetcher, NotNull());
550 std::unique_ptr<base::Value> value = 565 std::unique_ptr<base::Value> value =
551 base::JSONReader::Read(fetcher->upload_data()); 566 base::JSONReader::Read(fetcher->upload_data());
552 ASSERT_TRUE(value) << " failed to parse JSON: " 567 ASSERT_TRUE(value) << " failed to parse JSON: "
553 << PrintToString(fetcher->upload_data()); 568 << PrintToString(fetcher->upload_data());
554 const base::DictionaryValue* dict = nullptr; 569 const base::DictionaryValue* dict = nullptr;
555 ASSERT_TRUE(value->GetAsDictionary(&dict)); 570 ASSERT_TRUE(value->GetAsDictionary(&dict));
556 const base::DictionaryValue* local_scoring_params = nullptr; 571 const base::DictionaryValue* local_scoring_params = nullptr;
(...skipping 11 matching lines...) Expand all
568 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector)); 583 ASSERT_TRUE(content_selectors->GetDictionary(1, &content_selector));
569 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value)); 584 EXPECT_TRUE(content_selector->GetString("value", &content_selector_value));
570 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com")); 585 EXPECT_THAT(content_selector_value, Eq("www.somehost2.com"));
571 } 586 }
572 587
573 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) { 588 TEST_F(NTPSnippetsFetcherTest, ShouldReportUrlStatusError) {
574 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, 589 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND,
575 net::URLRequestStatus::FAILED); 590 net::URLRequestStatus::FAILED);
576 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 591 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
577 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 592 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
593 test_excluded(),
578 /*count=*/1, 594 /*count=*/1,
579 /*interactive_request=*/true); 595 /*interactive_request=*/true);
580 FastForwardUntilNoTasksRemain(); 596 FastForwardUntilNoTasksRemain();
581 EXPECT_THAT(snippets_fetcher().last_status(), 597 EXPECT_THAT(snippets_fetcher().last_status(),
582 Eq("URLRequestStatus error -2")); 598 Eq("URLRequestStatus error -2"));
583 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 599 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
584 EXPECT_THAT( 600 EXPECT_THAT(
585 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 601 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
586 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1))); 602 ElementsAre(base::Bucket(/*min=*/2, /*count=*/1)));
587 EXPECT_THAT(histogram_tester().GetAllSamples( 603 EXPECT_THAT(histogram_tester().GetAllSamples(
588 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 604 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
589 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1))); 605 ElementsAre(base::Bucket(/*min=*/-2, /*count=*/1)));
590 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 606 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
591 Not(IsEmpty())); 607 Not(IsEmpty()));
592 } 608 }
593 609
594 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) { 610 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpError) {
595 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND, 611 SetFakeResponse(/*data=*/std::string(), net::HTTP_NOT_FOUND,
596 net::URLRequestStatus::SUCCESS); 612 net::URLRequestStatus::SUCCESS);
597 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 613 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
598 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 614 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
615 test_excluded(),
599 /*count=*/1, 616 /*count=*/1,
600 /*interactive_request=*/true); 617 /*interactive_request=*/true);
601 FastForwardUntilNoTasksRemain(); 618 FastForwardUntilNoTasksRemain();
602 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty()); 619 EXPECT_THAT(snippets_fetcher().last_json(), IsEmpty());
603 EXPECT_THAT( 620 EXPECT_THAT(
604 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 621 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
605 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1))); 622 ElementsAre(base::Bucket(/*min=*/3, /*count=*/1)));
606 EXPECT_THAT(histogram_tester().GetAllSamples( 623 EXPECT_THAT(histogram_tester().GetAllSamples(
607 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 624 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
608 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1))); 625 ElementsAre(base::Bucket(/*min=*/404, /*count=*/1)));
609 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 626 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
610 Not(IsEmpty())); 627 Not(IsEmpty()));
611 } 628 }
612 629
613 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) { 630 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonError) {
614 const std::string kInvalidJsonStr = "{ \"recos\": []"; 631 const std::string kInvalidJsonStr = "{ \"recos\": []";
615 SetFakeResponse(/*data=*/kInvalidJsonStr, net::HTTP_OK, 632 SetFakeResponse(/*data=*/kInvalidJsonStr, net::HTTP_OK,
616 net::URLRequestStatus::SUCCESS); 633 net::URLRequestStatus::SUCCESS);
617 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 634 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
618 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 635 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
636 test_excluded(),
619 /*count=*/1, 637 /*count=*/1,
620 /*interactive_request=*/true); 638 /*interactive_request=*/true);
621 FastForwardUntilNoTasksRemain(); 639 FastForwardUntilNoTasksRemain();
622 EXPECT_THAT(snippets_fetcher().last_status(), 640 EXPECT_THAT(snippets_fetcher().last_status(),
623 StartsWith("Received invalid JSON (error ")); 641 StartsWith("Received invalid JSON (error "));
624 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr)); 642 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kInvalidJsonStr));
625 EXPECT_THAT( 643 EXPECT_THAT(
626 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 644 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
627 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); 645 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1)));
628 EXPECT_THAT(histogram_tester().GetAllSamples( 646 EXPECT_THAT(histogram_tester().GetAllSamples(
629 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 647 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
630 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 648 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
631 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 649 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
632 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 650 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
633 /*count=*/1))); 651 /*count=*/1)));
634 } 652 }
635 653
636 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) { 654 TEST_F(NTPSnippetsFetcherTest, ShouldReportJsonErrorForEmptyResponse) {
637 SetFakeResponse(/*data=*/std::string(), net::HTTP_OK, 655 SetFakeResponse(/*data=*/std::string(), net::HTTP_OK,
638 net::URLRequestStatus::SUCCESS); 656 net::URLRequestStatus::SUCCESS);
639 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 657 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
640 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 658 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
659 test_excluded(),
641 /*count=*/1, 660 /*count=*/1,
642 /*interactive_request=*/true); 661 /*interactive_request=*/true);
643 FastForwardUntilNoTasksRemain(); 662 FastForwardUntilNoTasksRemain();
644 EXPECT_THAT(snippets_fetcher().last_json(), std::string()); 663 EXPECT_THAT(snippets_fetcher().last_json(), std::string());
645 EXPECT_THAT( 664 EXPECT_THAT(
646 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 665 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
647 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1))); 666 ElementsAre(base::Bucket(/*min=*/4, /*count=*/1)));
648 EXPECT_THAT(histogram_tester().GetAllSamples( 667 EXPECT_THAT(histogram_tester().GetAllSamples(
649 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 668 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
650 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 669 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
651 } 670 }
652 671
653 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) { 672 TEST_F(NTPSnippetsFetcherTest, ShouldReportInvalidListError) {
654 const std::string kJsonStr = 673 const std::string kJsonStr =
655 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}"; 674 "{\"recos\": [{ \"contentInfo\": { \"foo\" : \"bar\" }}]}";
656 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 675 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
657 net::URLRequestStatus::SUCCESS); 676 net::URLRequestStatus::SUCCESS);
658 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 677 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
659 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 678 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
679 test_excluded(),
660 /*count=*/1, 680 /*count=*/1,
661 /*interactive_request=*/true); 681 /*interactive_request=*/true);
662 FastForwardUntilNoTasksRemain(); 682 FastForwardUntilNoTasksRemain();
663 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr)); 683 EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
664 EXPECT_THAT( 684 EXPECT_THAT(
665 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 685 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
666 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1))); 686 ElementsAre(base::Bucket(/*min=*/5, /*count=*/1)));
667 EXPECT_THAT(histogram_tester().GetAllSamples( 687 EXPECT_THAT(histogram_tester().GetAllSamples(
668 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 688 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
669 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 689 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
670 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 690 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
671 Not(IsEmpty())); 691 Not(IsEmpty()));
672 } 692 }
673 693
674 // This test actually verifies that the test setup itself is sane, to prevent 694 // This test actually verifies that the test setup itself is sane, to prevent
675 // hard-to-reproduce test failures. 695 // hard-to-reproduce test failures.
676 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) { 696 TEST_F(NTPSnippetsFetcherTest, ShouldReportHttpErrorForMissingBakedResponse) {
677 InitFakeURLFetcherFactory(); 697 InitFakeURLFetcherFactory();
678 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1); 698 EXPECT_CALL(mock_callback(), Run(/*snippets=*/Not(HasValue()))).Times(1);
679 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 699 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
700 test_excluded(),
680 /*count=*/1, 701 /*count=*/1,
681 /*interactive_request=*/true); 702 /*interactive_request=*/true);
682 FastForwardUntilNoTasksRemain(); 703 FastForwardUntilNoTasksRemain();
683 } 704 }
684 705
685 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) { 706 TEST_F(NTPSnippetsFetcherTest, ShouldCancelOngoingFetch) {
686 const std::string kJsonStr = "{ \"recos\": [] }"; 707 const std::string kJsonStr = "{ \"recos\": [] }";
687 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK, 708 SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
688 net::URLRequestStatus::SUCCESS); 709 net::URLRequestStatus::SUCCESS);
689 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList())); 710 EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList()));
690 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 711 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
712 test_excluded(),
691 /*count=*/1, 713 /*count=*/1,
692 /*interactive_request=*/true); 714 /*interactive_request=*/true);
693 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous. 715 // Second call to FetchSnippetsFromHosts() overrides/cancels the previous.
694 // Callback is expected to be called once. 716 // Callback is expected to be called once.
695 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(), 717 snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
718 test_excluded(),
696 /*count=*/1, 719 /*count=*/1,
697 /*interactive_request=*/true); 720 /*interactive_request=*/true);
698 FastForwardUntilNoTasksRemain(); 721 FastForwardUntilNoTasksRemain();
699 EXPECT_THAT( 722 EXPECT_THAT(
700 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"), 723 histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchResult"),
701 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1))); 724 ElementsAre(base::Bucket(/*min=*/0, /*count=*/1)));
702 EXPECT_THAT(histogram_tester().GetAllSamples( 725 EXPECT_THAT(histogram_tester().GetAllSamples(
703 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"), 726 "NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
704 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1))); 727 ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
705 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"), 728 EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
706 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs, 729 ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
707 /*count=*/1))); 730 /*count=*/1)));
708 } 731 }
709 732
710 ::std::ostream& operator<<( 733 ::std::ostream& operator<<(
711 ::std::ostream& os, 734 ::std::ostream& os,
712 const NTPSnippetsFetcher::OptionalSnippets& snippets) { 735 const NTPSnippetsFetcher::OptionalSnippets& snippets) {
713 if (snippets) { 736 if (snippets) {
714 // Matchers above aren't any more precise than this, so this is sufficient 737 // Matchers above aren't any more precise than this, so this is sufficient
715 // for test-failure diagnostics. 738 // for test-failure diagnostics.
716 return os << "list with " << snippets->size() << " elements"; 739 return os << "list with " << snippets->size() << " elements";
717 } else { 740 } else {
718 return os << "null"; 741 return os << "null";
719 } 742 }
720 } 743 }
721 744
722 } // namespace ntp_snippets 745 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698