OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_service.h" | 5 #include "components/ntp_snippets/ntp_snippets_service.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 using testing::_; | 57 using testing::_; |
58 | 58 |
59 namespace ntp_snippets { | 59 namespace ntp_snippets { |
60 | 60 |
61 namespace { | 61 namespace { |
62 | 62 |
63 MATCHER_P(IdEq, value, "") { | 63 MATCHER_P(IdEq, value, "") { |
64 return arg->id() == value; | 64 return arg->id() == value; |
65 } | 65 } |
66 | 66 |
67 MATCHER_P(IsCategory, id, "") { | |
68 return arg.id() == static_cast<int>(id); | |
Marc Treib
2016/08/22 15:06:47
return arg.IsKnownCategory(id);
?
sfiera
2016/08/24 14:35:57
This way is usable with server-categories (e.g. Is
| |
69 } | |
70 | |
67 const base::Time::Exploded kDefaultCreationTime = {2015, 11, 4, 25, 13, 46, 45}; | 71 const base::Time::Exploded kDefaultCreationTime = {2015, 11, 4, 25, 13, 46, 45}; |
68 const char kTestContentSnippetsServerFormat[] = | 72 const char kTestContentSnippetsServerFormat[] = |
69 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s"; | 73 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s"; |
70 | 74 |
71 const char kSnippetUrl[] = "http://localhost/foobar"; | 75 const char kSnippetUrl[] = "http://localhost/foobar"; |
72 const char kSnippetTitle[] = "Title"; | 76 const char kSnippetTitle[] = "Title"; |
73 const char kSnippetText[] = "Snippet"; | 77 const char kSnippetText[] = "Snippet"; |
74 const char kSnippetSalientImage[] = "http://localhost/salient_image"; | 78 const char kSnippetSalientImage[] = "http://localhost/salient_image"; |
75 const char kSnippetPublisherName[] = "Foo News"; | 79 const char kSnippetPublisherName[] = "Foo News"; |
76 const char kSnippetAmpUrl[] = "http://localhost/amp"; | 80 const char kSnippetAmpUrl[] = "http://localhost/amp"; |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
235 class MockScheduler : public NTPSnippetsScheduler { | 239 class MockScheduler : public NTPSnippetsScheduler { |
236 public: | 240 public: |
237 MOCK_METHOD4(Schedule, | 241 MOCK_METHOD4(Schedule, |
238 bool(base::TimeDelta period_wifi_charging, | 242 bool(base::TimeDelta period_wifi_charging, |
239 base::TimeDelta period_wifi, | 243 base::TimeDelta period_wifi, |
240 base::TimeDelta period_fallback, | 244 base::TimeDelta period_fallback, |
241 base::Time reschedule_time)); | 245 base::Time reschedule_time)); |
242 MOCK_METHOD0(Unschedule, bool()); | 246 MOCK_METHOD0(Unschedule, bool()); |
243 }; | 247 }; |
244 | 248 |
245 class WaitForDBLoad { | 249 void WaitForDBLoad(MockContentSuggestionsProviderObserver* observer, |
246 public: | 250 NTPSnippetsService* service) { |
247 WaitForDBLoad(MockContentSuggestionsProviderObserver* observer, | 251 if (service->ready()) |
248 NTPSnippetsService* service) | 252 return; |
249 : observer_(observer) { | 253 base::RunLoop run_loop; |
250 EXPECT_CALL(*observer_, OnCategoryStatusChanged(_, _, _)) | 254 EXPECT_CALL(*observer, |
251 .WillOnce(Invoke(this, &WaitForDBLoad::OnCategoryStatusChanged)); | 255 OnCategoryStatusChanged(_, IsCategory(KnownCategories::ARTICLES), |
252 if (!service->ready()) | 256 CategoryStatus::AVAILABLE_LOADING)) |
253 run_loop_.Run(); | 257 .WillOnce(testing::InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
Marc Treib
2016/08/22 15:06:47
Wait, did this even work? Looks like if |service|
sfiera
2016/08/24 14:35:57
I doubt it. Probably, this was never used when the
| |
254 } | 258 run_loop.Run(); |
255 | 259 Mock::VerifyAndClearExpectations(observer); |
256 ~WaitForDBLoad() { Mock::VerifyAndClearExpectations(observer_); } | 260 } |
257 | |
258 private: | |
259 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, | |
260 Category category, | |
261 CategoryStatus new_status) { | |
262 EXPECT_EQ(new_status, CategoryStatus::AVAILABLE_LOADING); | |
263 run_loop_.Quit(); | |
264 } | |
265 | |
266 MockContentSuggestionsProviderObserver* observer_; | |
267 base::RunLoop run_loop_; | |
268 | |
269 DISALLOW_COPY_AND_ASSIGN(WaitForDBLoad); | |
270 }; | |
271 | 261 |
272 class MockImageFetcher : public ImageFetcher { | 262 class MockImageFetcher : public ImageFetcher { |
273 public: | 263 public: |
274 MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*)); | 264 MOCK_METHOD1(SetImageFetcherDelegate, void(ImageFetcherDelegate*)); |
275 MOCK_METHOD1(SetDataUseServiceName, void(DataUseServiceName)); | 265 MOCK_METHOD1(SetDataUseServiceName, void(DataUseServiceName)); |
276 MOCK_METHOD3( | 266 MOCK_METHOD3( |
277 StartOrQueueNetworkRequest, | 267 StartOrQueueNetworkRequest, |
278 void(const std::string&, | 268 void(const std::string&, |
279 const GURL&, | 269 const GURL&, |
280 base::Callback<void(const std::string&, const gfx::Image&)>)); | 270 base::Callback<void(const std::string&, const gfx::Image&)>)); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
415 | 405 |
416 // When we have no snippets are all, loading the service initiates a fetch. | 406 // When we have no snippets are all, loading the service initiates a fetch. |
417 base::RunLoop().RunUntilIdle(); | 407 base::RunLoop().RunUntilIdle(); |
418 ASSERT_EQ("OK", service()->snippets_fetcher()->last_status()); | 408 ASSERT_EQ("OK", service()->snippets_fetcher()->last_status()); |
419 } | 409 } |
420 | 410 |
421 TEST_F(NTPSnippetsServiceTest, Full) { | 411 TEST_F(NTPSnippetsServiceTest, Full) { |
422 std::string json_str(GetTestJson({GetSnippet()})); | 412 std::string json_str(GetTestJson({GetSnippet()})); |
423 | 413 |
424 LoadFromJSONString(json_str); | 414 LoadFromJSONString(json_str); |
425 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 415 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
426 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 416 const NTPSnippet& snippet = |
417 *service()->GetSnippetsForTesting(articles_category()).front(); | |
427 | 418 |
428 EXPECT_EQ(snippet.id(), kSnippetUrl); | 419 EXPECT_EQ(snippet.id(), kSnippetUrl); |
429 EXPECT_EQ(snippet.title(), kSnippetTitle); | 420 EXPECT_EQ(snippet.title(), kSnippetTitle); |
430 EXPECT_EQ(snippet.snippet(), kSnippetText); | 421 EXPECT_EQ(snippet.snippet(), kSnippetText); |
431 EXPECT_EQ(snippet.salient_image_url(), GURL(kSnippetSalientImage)); | 422 EXPECT_EQ(snippet.salient_image_url(), GURL(kSnippetSalientImage)); |
432 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date()); | 423 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date()); |
433 EXPECT_EQ(snippet.best_source().publisher_name, kSnippetPublisherName); | 424 EXPECT_EQ(snippet.best_source().publisher_name, kSnippetPublisherName); |
434 EXPECT_EQ(snippet.best_source().amp_url, GURL(kSnippetAmpUrl)); | 425 EXPECT_EQ(snippet.best_source().amp_url, GURL(kSnippetAmpUrl)); |
435 } | 426 } |
436 | 427 |
437 TEST_F(NTPSnippetsServiceTest, Clear) { | 428 TEST_F(NTPSnippetsServiceTest, Clear) { |
438 std::string json_str(GetTestJson({GetSnippet()})); | 429 std::string json_str(GetTestJson({GetSnippet()})); |
439 | 430 |
440 LoadFromJSONString(json_str); | 431 LoadFromJSONString(json_str); |
441 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 432 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
442 | 433 |
443 service()->ClearCachedSuggestionsForDebugging(articles_category()); | 434 service()->ClearCachedSuggestionsForDebugging(articles_category()); |
444 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 435 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
445 } | 436 } |
446 | 437 |
447 TEST_F(NTPSnippetsServiceTest, InsertAtFront) { | 438 TEST_F(NTPSnippetsServiceTest, InsertAtFront) { |
448 std::string first("http://first"); | 439 std::string first("http://first"); |
449 LoadFromJSONString(GetTestJson({GetSnippetWithUrl(first)})); | 440 LoadFromJSONString(GetTestJson({GetSnippetWithUrl(first)})); |
450 EXPECT_THAT(service()->GetSnippetsForTesting(), ElementsAre(IdEq(first))); | 441 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), |
442 ElementsAre(IdEq(first))); | |
451 | 443 |
452 std::string second("http://second"); | 444 std::string second("http://second"); |
453 LoadFromJSONString(GetTestJson({GetSnippetWithUrl(second)})); | 445 LoadFromJSONString(GetTestJson({GetSnippetWithUrl(second)})); |
454 // The snippet loaded last should be at the first position in the list now. | 446 // The snippet loaded last should be at the first position in the list now. |
455 EXPECT_THAT(service()->GetSnippetsForTesting(), | 447 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), |
456 ElementsAre(IdEq(second), IdEq(first))); | 448 ElementsAre(IdEq(second), IdEq(first))); |
457 } | 449 } |
458 | 450 |
459 TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) { | 451 TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) { |
460 int max_snippet_count = NTPSnippetsService::GetMaxSnippetCountForTesting(); | 452 int max_snippet_count = NTPSnippetsService::GetMaxSnippetCountForTesting(); |
461 int snippets_per_load = max_snippet_count / 2 + 1; | 453 int snippets_per_load = max_snippet_count / 2 + 1; |
462 char url_format[] = "http://localhost/%i"; | 454 char url_format[] = "http://localhost/%i"; |
463 | 455 |
464 std::vector<std::string> snippets1; | 456 std::vector<std::string> snippets1; |
465 std::vector<std::string> snippets2; | 457 std::vector<std::string> snippets2; |
466 for (int i = 0; i < snippets_per_load; i++) { | 458 for (int i = 0; i < snippets_per_load; i++) { |
467 snippets1.push_back(GetSnippetWithUrl(base::StringPrintf(url_format, i))); | 459 snippets1.push_back(GetSnippetWithUrl(base::StringPrintf(url_format, i))); |
468 snippets2.push_back(GetSnippetWithUrl( | 460 snippets2.push_back(GetSnippetWithUrl( |
469 base::StringPrintf(url_format, snippets_per_load + i))); | 461 base::StringPrintf(url_format, snippets_per_load + i))); |
470 } | 462 } |
471 | 463 |
472 LoadFromJSONString(GetTestJson(snippets1)); | 464 LoadFromJSONString(GetTestJson(snippets1)); |
473 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(snippets1.size())); | 465 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), |
466 SizeIs(snippets1.size())); | |
474 | 467 |
475 LoadFromJSONString(GetTestJson(snippets2)); | 468 LoadFromJSONString(GetTestJson(snippets2)); |
476 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(max_snippet_count)); | 469 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), |
470 SizeIs(max_snippet_count)); | |
477 } | 471 } |
478 | 472 |
479 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { | 473 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { |
480 LoadFromJSONString(GetTestJson({GetInvalidSnippet()})); | 474 LoadFromJSONString(GetTestJson({GetInvalidSnippet()})); |
481 EXPECT_THAT(service()->snippets_fetcher()->last_status(), | 475 EXPECT_THAT(service()->snippets_fetcher()->last_status(), |
482 StartsWith("Received invalid JSON")); | 476 StartsWith("Received invalid JSON")); |
483 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 477 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
484 } | 478 } |
485 | 479 |
486 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) { | 480 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) { |
487 LoadFromJSONString(GetTestJson({GetSnippet()})); | 481 LoadFromJSONString(GetTestJson({GetSnippet()})); |
488 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 482 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
489 ASSERT_EQ("OK", service()->snippets_fetcher()->last_status()); | 483 ASSERT_EQ("OK", service()->snippets_fetcher()->last_status()); |
490 | 484 |
491 LoadFromJSONString(GetTestJson({GetInvalidSnippet()})); | 485 LoadFromJSONString(GetTestJson({GetInvalidSnippet()})); |
492 EXPECT_THAT(service()->snippets_fetcher()->last_status(), | 486 EXPECT_THAT(service()->snippets_fetcher()->last_status(), |
493 StartsWith("Received invalid JSON")); | 487 StartsWith("Received invalid JSON")); |
494 // This should not have changed the existing snippets. | 488 // This should not have changed the existing snippets. |
495 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 489 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
496 } | 490 } |
497 | 491 |
498 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJson) { | 492 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJson) { |
499 LoadFromJSONString(GetTestJson({GetIncompleteSnippet()})); | 493 LoadFromJSONString(GetTestJson({GetIncompleteSnippet()})); |
500 EXPECT_EQ("Invalid / empty list.", | 494 EXPECT_EQ("Invalid / empty list.", |
501 service()->snippets_fetcher()->last_status()); | 495 service()->snippets_fetcher()->last_status()); |
502 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 496 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
503 } | 497 } |
504 | 498 |
505 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) { | 499 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) { |
506 LoadFromJSONString(GetTestJson({GetSnippet()})); | 500 LoadFromJSONString(GetTestJson({GetSnippet()})); |
507 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 501 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
508 | 502 |
509 LoadFromJSONString(GetTestJson({GetIncompleteSnippet()})); | 503 LoadFromJSONString(GetTestJson({GetIncompleteSnippet()})); |
510 EXPECT_EQ("Invalid / empty list.", | 504 EXPECT_EQ("Invalid / empty list.", |
511 service()->snippets_fetcher()->last_status()); | 505 service()->snippets_fetcher()->last_status()); |
512 // This should not have changed the existing snippets. | 506 // This should not have changed the existing snippets. |
513 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 507 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
514 } | 508 } |
515 | 509 |
516 TEST_F(NTPSnippetsServiceTest, Dismiss) { | 510 TEST_F(NTPSnippetsServiceTest, Dismiss) { |
517 std::vector<std::string> source_urls, publishers, amp_urls; | 511 std::vector<std::string> source_urls, publishers, amp_urls; |
518 source_urls.push_back(std::string("http://site.com")); | 512 source_urls.push_back(std::string("http://site.com")); |
519 publishers.push_back(std::string("Source 1")); | 513 publishers.push_back(std::string("Source 1")); |
520 amp_urls.push_back(std::string()); | 514 amp_urls.push_back(std::string()); |
521 std::string json_str( | 515 std::string json_str( |
522 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 516 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
523 | 517 |
524 LoadFromJSONString(json_str); | 518 LoadFromJSONString(json_str); |
525 | 519 |
526 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 520 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
527 | 521 |
528 // Dismissing a non-existent snippet shouldn't do anything. | 522 // Dismissing a non-existent snippet shouldn't do anything. |
529 service()->DismissSuggestion(MakeUniqueID("http://othersite.com")); | 523 service()->DismissSuggestion(MakeUniqueID("http://othersite.com")); |
530 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 524 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
531 | 525 |
532 // Dismiss the snippet. | 526 // Dismiss the snippet. |
533 service()->DismissSuggestion(MakeUniqueID(kSnippetUrl)); | 527 service()->DismissSuggestion(MakeUniqueID(kSnippetUrl)); |
534 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 528 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
535 | 529 |
536 // Make sure that fetching the same snippet again does not re-add it. | 530 // Make sure that fetching the same snippet again does not re-add it. |
537 LoadFromJSONString(json_str); | 531 LoadFromJSONString(json_str); |
538 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 532 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
539 | 533 |
540 // The snippet should stay dismissed even after re-creating the service. | 534 // The snippet should stay dismissed even after re-creating the service. |
541 RecreateSnippetsService(); | 535 RecreateSnippetsService(); |
542 LoadFromJSONString(json_str); | 536 LoadFromJSONString(json_str); |
543 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 537 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
544 | 538 |
545 // The snippet can be added again after clearing dismissed snippets. | 539 // The snippet can be added again after clearing dismissed snippets. |
546 service()->ClearDismissedSuggestionsForDebugging(articles_category()); | 540 service()->ClearDismissedSuggestionsForDebugging(articles_category()); |
547 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 541 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
548 LoadFromJSONString(json_str); | 542 LoadFromJSONString(json_str); |
549 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 543 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
550 } | 544 } |
551 | 545 |
552 TEST_F(NTPSnippetsServiceTest, GetDismissed) { | 546 TEST_F(NTPSnippetsServiceTest, GetDismissed) { |
553 LoadFromJSONString(GetTestJson({GetSnippet()})); | 547 LoadFromJSONString(GetTestJson({GetSnippet()})); |
554 | 548 |
555 service()->DismissSuggestion(MakeUniqueID(kSnippetUrl)); | 549 service()->DismissSuggestion(MakeUniqueID(kSnippetUrl)); |
556 std::vector<ContentSuggestion> suggestions = | 550 std::vector<ContentSuggestion> suggestions = |
557 service()->GetDismissedSuggestionsForDebugging(articles_category()); | 551 service()->GetDismissedSuggestionsForDebugging(articles_category()); |
558 EXPECT_EQ(1u, suggestions.size()); | 552 EXPECT_EQ(1u, suggestions.size()); |
559 for (auto& suggestion : suggestions) { | 553 for (auto& suggestion : suggestions) { |
560 EXPECT_EQ(MakeUniqueID(kSnippetUrl), suggestion.id()); | 554 EXPECT_EQ(MakeUniqueID(kSnippetUrl), suggestion.id()); |
561 } | 555 } |
562 | 556 |
563 // There should be no dismissed snippet after clearing the list. | 557 // There should be no dismissed snippet after clearing the list. |
564 service()->ClearDismissedSuggestionsForDebugging(articles_category()); | 558 service()->ClearDismissedSuggestionsForDebugging(articles_category()); |
565 EXPECT_EQ(0u, service() | 559 EXPECT_EQ(0u, service() |
566 ->GetDismissedSuggestionsForDebugging(articles_category()) | 560 ->GetDismissedSuggestionsForDebugging(articles_category()) |
567 .size()); | 561 .size()); |
568 } | 562 } |
569 | 563 |
570 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { | 564 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { |
571 std::string json_str(GetTestJson({GetSnippetWithTimes( | 565 std::string json_str(GetTestJson({GetSnippetWithTimes( |
572 "aaa1448459205", | 566 "aaa1448459205", |
573 NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()))})); | 567 NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()))})); |
574 | 568 |
575 LoadFromJSONString(json_str); | 569 LoadFromJSONString(json_str); |
576 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 570 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
577 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 571 const NTPSnippet& snippet = |
572 *service()->GetSnippetsForTesting(articles_category()).front(); | |
578 EXPECT_EQ(snippet.id(), kSnippetUrl); | 573 EXPECT_EQ(snippet.id(), kSnippetUrl); |
579 EXPECT_EQ(snippet.title(), kSnippetTitle); | 574 EXPECT_EQ(snippet.title(), kSnippetTitle); |
580 EXPECT_EQ(snippet.snippet(), kSnippetText); | 575 EXPECT_EQ(snippet.snippet(), kSnippetText); |
581 EXPECT_EQ(base::Time::UnixEpoch(), snippet.publish_date()); | 576 EXPECT_EQ(base::Time::UnixEpoch(), snippet.publish_date()); |
582 } | 577 } |
583 | 578 |
584 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { | 579 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { |
585 std::string json_str(GetTestJson({GetExpiredSnippet()})); | 580 std::string json_str(GetTestJson({GetExpiredSnippet()})); |
586 | 581 |
587 LoadFromJSONString(json_str); | 582 LoadFromJSONString(json_str); |
588 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 583 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
589 } | 584 } |
590 | 585 |
591 TEST_F(NTPSnippetsServiceTest, TestSingleSource) { | 586 TEST_F(NTPSnippetsServiceTest, TestSingleSource) { |
592 std::vector<std::string> source_urls, publishers, amp_urls; | 587 std::vector<std::string> source_urls, publishers, amp_urls; |
593 source_urls.push_back(std::string("http://source1.com")); | 588 source_urls.push_back(std::string("http://source1.com")); |
594 publishers.push_back(std::string("Source 1")); | 589 publishers.push_back(std::string("Source 1")); |
595 amp_urls.push_back(std::string("http://source1.amp.com")); | 590 amp_urls.push_back(std::string("http://source1.amp.com")); |
596 std::string json_str( | 591 std::string json_str( |
597 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 592 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
598 | 593 |
599 LoadFromJSONString(json_str); | 594 LoadFromJSONString(json_str); |
600 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 595 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
601 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 596 const NTPSnippet& snippet = |
597 *service()->GetSnippetsForTesting(articles_category()).front(); | |
602 EXPECT_EQ(snippet.sources().size(), 1u); | 598 EXPECT_EQ(snippet.sources().size(), 1u); |
603 EXPECT_EQ(snippet.id(), kSnippetUrl); | 599 EXPECT_EQ(snippet.id(), kSnippetUrl); |
604 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); | 600 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); |
605 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); | 601 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
606 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); | 602 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); |
607 } | 603 } |
608 | 604 |
609 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMalformedUrl) { | 605 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMalformedUrl) { |
610 std::vector<std::string> source_urls, publishers, amp_urls; | 606 std::vector<std::string> source_urls, publishers, amp_urls; |
611 source_urls.push_back(std::string("aaaa")); | 607 source_urls.push_back(std::string("aaaa")); |
612 publishers.push_back(std::string("Source 1")); | 608 publishers.push_back(std::string("Source 1")); |
613 amp_urls.push_back(std::string("http://source1.amp.com")); | 609 amp_urls.push_back(std::string("http://source1.amp.com")); |
614 std::string json_str( | 610 std::string json_str( |
615 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 611 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
616 | 612 |
617 LoadFromJSONString(json_str); | 613 LoadFromJSONString(json_str); |
618 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 614 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
619 } | 615 } |
620 | 616 |
621 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMissingData) { | 617 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMissingData) { |
622 std::vector<std::string> source_urls, publishers, amp_urls; | 618 std::vector<std::string> source_urls, publishers, amp_urls; |
623 source_urls.push_back(std::string("http://source1.com")); | 619 source_urls.push_back(std::string("http://source1.com")); |
624 publishers.push_back(std::string()); | 620 publishers.push_back(std::string()); |
625 amp_urls.push_back(std::string()); | 621 amp_urls.push_back(std::string()); |
626 std::string json_str( | 622 std::string json_str( |
627 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 623 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
628 | 624 |
629 LoadFromJSONString(json_str); | 625 LoadFromJSONString(json_str); |
630 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 626 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
631 } | 627 } |
632 | 628 |
633 TEST_F(NTPSnippetsServiceTest, TestMultipleSources) { | 629 TEST_F(NTPSnippetsServiceTest, TestMultipleSources) { |
634 std::vector<std::string> source_urls, publishers, amp_urls; | 630 std::vector<std::string> source_urls, publishers, amp_urls; |
635 source_urls.push_back(std::string("http://source1.com")); | 631 source_urls.push_back(std::string("http://source1.com")); |
636 source_urls.push_back(std::string("http://source2.com")); | 632 source_urls.push_back(std::string("http://source2.com")); |
637 publishers.push_back(std::string("Source 1")); | 633 publishers.push_back(std::string("Source 1")); |
638 publishers.push_back(std::string("Source 2")); | 634 publishers.push_back(std::string("Source 2")); |
639 amp_urls.push_back(std::string("http://source1.amp.com")); | 635 amp_urls.push_back(std::string("http://source1.amp.com")); |
640 amp_urls.push_back(std::string("http://source2.amp.com")); | 636 amp_urls.push_back(std::string("http://source2.amp.com")); |
641 std::string json_str( | 637 std::string json_str( |
642 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 638 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
643 | 639 |
644 LoadFromJSONString(json_str); | 640 LoadFromJSONString(json_str); |
645 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 641 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
646 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 642 const NTPSnippet& snippet = |
643 *service()->GetSnippetsForTesting(articles_category()).front(); | |
647 // Expect the first source to be chosen | 644 // Expect the first source to be chosen |
648 EXPECT_EQ(snippet.sources().size(), 2u); | 645 EXPECT_EQ(snippet.sources().size(), 2u); |
649 EXPECT_EQ(snippet.id(), kSnippetUrl); | 646 EXPECT_EQ(snippet.id(), kSnippetUrl); |
650 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); | 647 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); |
651 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); | 648 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
652 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); | 649 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); |
653 } | 650 } |
654 | 651 |
655 TEST_F(NTPSnippetsServiceTest, TestMultipleIncompleteSources) { | 652 TEST_F(NTPSnippetsServiceTest, TestMultipleIncompleteSources) { |
656 // Set Source 2 to have no AMP url, and Source 1 to have no publisher name | 653 // Set Source 2 to have no AMP url, and Source 1 to have no publisher name |
657 // Source 2 should win since we favor publisher name over amp url | 654 // Source 2 should win since we favor publisher name over amp url |
658 std::vector<std::string> source_urls, publishers, amp_urls; | 655 std::vector<std::string> source_urls, publishers, amp_urls; |
659 source_urls.push_back(std::string("http://source1.com")); | 656 source_urls.push_back(std::string("http://source1.com")); |
660 source_urls.push_back(std::string("http://source2.com")); | 657 source_urls.push_back(std::string("http://source2.com")); |
661 publishers.push_back(std::string()); | 658 publishers.push_back(std::string()); |
662 publishers.push_back(std::string("Source 2")); | 659 publishers.push_back(std::string("Source 2")); |
663 amp_urls.push_back(std::string("http://source1.amp.com")); | 660 amp_urls.push_back(std::string("http://source1.amp.com")); |
664 amp_urls.push_back(std::string()); | 661 amp_urls.push_back(std::string()); |
665 std::string json_str( | 662 std::string json_str( |
666 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 663 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
667 | 664 |
668 LoadFromJSONString(json_str); | 665 LoadFromJSONString(json_str); |
669 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 666 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
670 { | 667 { |
671 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 668 const NTPSnippet& snippet = |
669 *service()->GetSnippetsForTesting(articles_category()).front(); | |
672 EXPECT_EQ(snippet.sources().size(), 2u); | 670 EXPECT_EQ(snippet.sources().size(), 2u); |
673 EXPECT_EQ(snippet.id(), kSnippetUrl); | 671 EXPECT_EQ(snippet.id(), kSnippetUrl); |
674 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); | 672 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); |
675 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); | 673 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); |
676 EXPECT_EQ(snippet.best_source().amp_url, GURL()); | 674 EXPECT_EQ(snippet.best_source().amp_url, GURL()); |
677 } | 675 } |
678 | 676 |
679 service()->ClearCachedSuggestionsForDebugging(articles_category()); | 677 service()->ClearCachedSuggestionsForDebugging(articles_category()); |
680 // Set Source 1 to have no AMP url, and Source 2 to have no publisher name | 678 // Set Source 1 to have no AMP url, and Source 2 to have no publisher name |
681 // Source 1 should win in this case since we prefer publisher name to AMP url | 679 // Source 1 should win in this case since we prefer publisher name to AMP url |
682 source_urls.clear(); | 680 source_urls.clear(); |
683 source_urls.push_back(std::string("http://source1.com")); | 681 source_urls.push_back(std::string("http://source1.com")); |
684 source_urls.push_back(std::string("http://source2.com")); | 682 source_urls.push_back(std::string("http://source2.com")); |
685 publishers.clear(); | 683 publishers.clear(); |
686 publishers.push_back(std::string("Source 1")); | 684 publishers.push_back(std::string("Source 1")); |
687 publishers.push_back(std::string()); | 685 publishers.push_back(std::string()); |
688 amp_urls.clear(); | 686 amp_urls.clear(); |
689 amp_urls.push_back(std::string()); | 687 amp_urls.push_back(std::string()); |
690 amp_urls.push_back(std::string("http://source2.amp.com")); | 688 amp_urls.push_back(std::string("http://source2.amp.com")); |
691 json_str = | 689 json_str = |
692 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); | 690 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); |
693 | 691 |
694 LoadFromJSONString(json_str); | 692 LoadFromJSONString(json_str); |
695 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 693 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
696 { | 694 { |
697 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 695 const NTPSnippet& snippet = |
696 *service()->GetSnippetsForTesting(articles_category()).front(); | |
698 EXPECT_EQ(snippet.sources().size(), 2u); | 697 EXPECT_EQ(snippet.sources().size(), 2u); |
699 EXPECT_EQ(snippet.id(), kSnippetUrl); | 698 EXPECT_EQ(snippet.id(), kSnippetUrl); |
700 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); | 699 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); |
701 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); | 700 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
702 EXPECT_EQ(snippet.best_source().amp_url, GURL()); | 701 EXPECT_EQ(snippet.best_source().amp_url, GURL()); |
703 } | 702 } |
704 | 703 |
705 service()->ClearCachedSuggestionsForDebugging(articles_category()); | 704 service()->ClearCachedSuggestionsForDebugging(articles_category()); |
706 // Set source 1 to have no AMP url and no source, and source 2 to only have | 705 // Set source 1 to have no AMP url and no source, and source 2 to only have |
707 // amp url. There should be no snippets since we only add sources we consider | 706 // amp url. There should be no snippets since we only add sources we consider |
708 // complete | 707 // complete |
709 source_urls.clear(); | 708 source_urls.clear(); |
710 source_urls.push_back(std::string("http://source1.com")); | 709 source_urls.push_back(std::string("http://source1.com")); |
711 source_urls.push_back(std::string("http://source2.com")); | 710 source_urls.push_back(std::string("http://source2.com")); |
712 publishers.clear(); | 711 publishers.clear(); |
713 publishers.push_back(std::string()); | 712 publishers.push_back(std::string()); |
714 publishers.push_back(std::string()); | 713 publishers.push_back(std::string()); |
715 amp_urls.clear(); | 714 amp_urls.clear(); |
716 amp_urls.push_back(std::string()); | 715 amp_urls.push_back(std::string()); |
717 amp_urls.push_back(std::string("http://source2.amp.com")); | 716 amp_urls.push_back(std::string("http://source2.amp.com")); |
718 json_str = | 717 json_str = |
719 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); | 718 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); |
720 | 719 |
721 LoadFromJSONString(json_str); | 720 LoadFromJSONString(json_str); |
722 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 721 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
723 } | 722 } |
724 | 723 |
725 TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) { | 724 TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) { |
726 // Test 2 complete sources, we should choose the first complete source | 725 // Test 2 complete sources, we should choose the first complete source |
727 std::vector<std::string> source_urls, publishers, amp_urls; | 726 std::vector<std::string> source_urls, publishers, amp_urls; |
728 source_urls.push_back(std::string("http://source1.com")); | 727 source_urls.push_back(std::string("http://source1.com")); |
729 source_urls.push_back(std::string("http://source2.com")); | 728 source_urls.push_back(std::string("http://source2.com")); |
730 source_urls.push_back(std::string("http://source3.com")); | 729 source_urls.push_back(std::string("http://source3.com")); |
731 publishers.push_back(std::string("Source 1")); | 730 publishers.push_back(std::string("Source 1")); |
732 publishers.push_back(std::string()); | 731 publishers.push_back(std::string()); |
733 publishers.push_back(std::string("Source 3")); | 732 publishers.push_back(std::string("Source 3")); |
734 amp_urls.push_back(std::string("http://source1.amp.com")); | 733 amp_urls.push_back(std::string("http://source1.amp.com")); |
735 amp_urls.push_back(std::string("http://source2.amp.com")); | 734 amp_urls.push_back(std::string("http://source2.amp.com")); |
736 amp_urls.push_back(std::string("http://source3.amp.com")); | 735 amp_urls.push_back(std::string("http://source3.amp.com")); |
737 std::string json_str( | 736 std::string json_str( |
738 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 737 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
739 | 738 |
740 LoadFromJSONString(json_str); | 739 LoadFromJSONString(json_str); |
741 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 740 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
742 { | 741 { |
743 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 742 const NTPSnippet& snippet = |
743 *service()->GetSnippetsForTesting(articles_category()).front(); | |
744 EXPECT_EQ(snippet.sources().size(), 3u); | 744 EXPECT_EQ(snippet.sources().size(), 3u); |
745 EXPECT_EQ(snippet.id(), kSnippetUrl); | 745 EXPECT_EQ(snippet.id(), kSnippetUrl); |
746 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); | 746 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); |
747 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); | 747 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
748 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); | 748 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); |
749 } | 749 } |
750 | 750 |
751 // Test 2 complete sources, we should choose the first complete source | 751 // Test 2 complete sources, we should choose the first complete source |
752 service()->ClearCachedSuggestionsForDebugging(articles_category()); | 752 service()->ClearCachedSuggestionsForDebugging(articles_category()); |
753 source_urls.clear(); | 753 source_urls.clear(); |
754 source_urls.push_back(std::string("http://source1.com")); | 754 source_urls.push_back(std::string("http://source1.com")); |
755 source_urls.push_back(std::string("http://source2.com")); | 755 source_urls.push_back(std::string("http://source2.com")); |
756 source_urls.push_back(std::string("http://source3.com")); | 756 source_urls.push_back(std::string("http://source3.com")); |
757 publishers.clear(); | 757 publishers.clear(); |
758 publishers.push_back(std::string()); | 758 publishers.push_back(std::string()); |
759 publishers.push_back(std::string("Source 2")); | 759 publishers.push_back(std::string("Source 2")); |
760 publishers.push_back(std::string("Source 3")); | 760 publishers.push_back(std::string("Source 3")); |
761 amp_urls.clear(); | 761 amp_urls.clear(); |
762 amp_urls.push_back(std::string("http://source1.amp.com")); | 762 amp_urls.push_back(std::string("http://source1.amp.com")); |
763 amp_urls.push_back(std::string("http://source2.amp.com")); | 763 amp_urls.push_back(std::string("http://source2.amp.com")); |
764 amp_urls.push_back(std::string("http://source3.amp.com")); | 764 amp_urls.push_back(std::string("http://source3.amp.com")); |
765 json_str = | 765 json_str = |
766 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); | 766 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); |
767 | 767 |
768 LoadFromJSONString(json_str); | 768 LoadFromJSONString(json_str); |
769 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 769 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
770 { | 770 { |
771 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 771 const NTPSnippet& snippet = |
772 *service()->GetSnippetsForTesting(articles_category()).front(); | |
772 EXPECT_EQ(snippet.sources().size(), 3u); | 773 EXPECT_EQ(snippet.sources().size(), 3u); |
773 EXPECT_EQ(snippet.id(), kSnippetUrl); | 774 EXPECT_EQ(snippet.id(), kSnippetUrl); |
774 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); | 775 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); |
775 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); | 776 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); |
776 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); | 777 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); |
777 } | 778 } |
778 | 779 |
779 // Test 3 complete sources, we should choose the first complete source | 780 // Test 3 complete sources, we should choose the first complete source |
780 service()->ClearCachedSuggestionsForDebugging(articles_category()); | 781 service()->ClearCachedSuggestionsForDebugging(articles_category()); |
781 source_urls.clear(); | 782 source_urls.clear(); |
782 source_urls.push_back(std::string("http://source1.com")); | 783 source_urls.push_back(std::string("http://source1.com")); |
783 source_urls.push_back(std::string("http://source2.com")); | 784 source_urls.push_back(std::string("http://source2.com")); |
784 source_urls.push_back(std::string("http://source3.com")); | 785 source_urls.push_back(std::string("http://source3.com")); |
785 publishers.clear(); | 786 publishers.clear(); |
786 publishers.push_back(std::string("Source 1")); | 787 publishers.push_back(std::string("Source 1")); |
787 publishers.push_back(std::string("Source 2")); | 788 publishers.push_back(std::string("Source 2")); |
788 publishers.push_back(std::string("Source 3")); | 789 publishers.push_back(std::string("Source 3")); |
789 amp_urls.clear(); | 790 amp_urls.clear(); |
790 amp_urls.push_back(std::string()); | 791 amp_urls.push_back(std::string()); |
791 amp_urls.push_back(std::string("http://source2.amp.com")); | 792 amp_urls.push_back(std::string("http://source2.amp.com")); |
792 amp_urls.push_back(std::string("http://source3.amp.com")); | 793 amp_urls.push_back(std::string("http://source3.amp.com")); |
793 json_str = | 794 json_str = |
794 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); | 795 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); |
795 | 796 |
796 LoadFromJSONString(json_str); | 797 LoadFromJSONString(json_str); |
797 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 798 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
798 { | 799 { |
799 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 800 const NTPSnippet& snippet = |
801 *service()->GetSnippetsForTesting(articles_category()).front(); | |
800 EXPECT_EQ(snippet.sources().size(), 3u); | 802 EXPECT_EQ(snippet.sources().size(), 3u); |
801 EXPECT_EQ(snippet.id(), kSnippetUrl); | 803 EXPECT_EQ(snippet.id(), kSnippetUrl); |
802 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); | 804 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); |
803 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); | 805 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); |
804 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); | 806 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); |
805 } | 807 } |
806 } | 808 } |
807 | 809 |
808 TEST_F(NTPSnippetsServiceTest, LogNumArticlesHistogram) { | 810 TEST_F(NTPSnippetsServiceTest, LogNumArticlesHistogram) { |
809 base::HistogramTester tester; | 811 base::HistogramTester tester; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
870 "http://mashable.com/2016/05/11/stolen?utm_cid=1"}; | 872 "http://mashable.com/2016/05/11/stolen?utm_cid=1"}; |
871 const std::vector<std::string> publishers = {"Mashable", "AOL", "Mashable"}; | 873 const std::vector<std::string> publishers = {"Mashable", "AOL", "Mashable"}; |
872 const std::vector<std::string> amp_urls = { | 874 const std::vector<std::string> amp_urls = { |
873 "http://mashable-amphtml.googleusercontent.com/1", | 875 "http://mashable-amphtml.googleusercontent.com/1", |
874 "http://t2.gstatic.com/images?q=tbn:3", | 876 "http://t2.gstatic.com/images?q=tbn:3", |
875 "http://t2.gstatic.com/images?q=tbn:3"}; | 877 "http://t2.gstatic.com/images?q=tbn:3"}; |
876 | 878 |
877 // Add the snippet from the mashable domain. | 879 // Add the snippet from the mashable domain. |
878 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( | 880 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( |
879 source_urls[0], creation, expiry, source_urls, publishers, amp_urls)})); | 881 source_urls[0], creation, expiry, source_urls, publishers, amp_urls)})); |
880 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 882 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
881 // Dismiss the snippet via the mashable source corpus ID. | 883 // Dismiss the snippet via the mashable source corpus ID. |
882 service()->DismissSuggestion(MakeUniqueID(source_urls[0])); | 884 service()->DismissSuggestion(MakeUniqueID(source_urls[0])); |
883 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 885 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
884 | 886 |
885 // The same article from the AOL domain should now be detected as dismissed. | 887 // The same article from the AOL domain should now be detected as dismissed. |
886 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( | 888 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( |
887 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)})); | 889 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)})); |
888 ASSERT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 890 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
889 } | 891 } |
890 | 892 |
891 TEST_F(NTPSnippetsServiceTest, StatusChanges) { | 893 TEST_F(NTPSnippetsServiceTest, StatusChanges) { |
892 // Simulate user signed out | 894 // Simulate user signed out |
893 SetUpFetchResponse(GetTestJson({GetSnippet()})); | 895 SetUpFetchResponse(GetTestJson({GetSnippet()})); |
894 EXPECT_CALL(observer(), | 896 EXPECT_CALL(observer(), |
895 OnCategoryStatusChanged(_, _, CategoryStatus::SIGNED_OUT)); | 897 OnCategoryStatusChanged(_, _, CategoryStatus::SIGNED_OUT)); |
896 service()->OnDisabledReasonChanged(DisabledReason::SIGNED_OUT); | 898 service()->OnDisabledReasonChanged(DisabledReason::SIGNED_OUT); |
897 base::RunLoop().RunUntilIdle(); | 899 base::RunLoop().RunUntilIdle(); |
898 EXPECT_EQ(NTPSnippetsService::State::DISABLED, service()->state_); | 900 EXPECT_EQ(NTPSnippetsService::State::DISABLED, service()->state_); |
899 EXPECT_THAT(service()->GetSnippetsForTesting(), | 901 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), |
900 IsEmpty()); // No fetch should be made. | 902 IsEmpty()); // No fetch should be made. |
901 | 903 |
902 // Simulate user sign in. The service should be ready again and load snippets. | 904 // Simulate user sign in. The service should be ready again and load snippets. |
903 SetUpFetchResponse(GetTestJson({GetSnippet()})); | 905 SetUpFetchResponse(GetTestJson({GetSnippet()})); |
904 EXPECT_CALL(observer(), | 906 EXPECT_CALL(observer(), |
905 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE_LOADING)); | 907 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE_LOADING)); |
906 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 908 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
907 service()->OnDisabledReasonChanged(DisabledReason::NONE); | 909 service()->OnDisabledReasonChanged(DisabledReason::NONE); |
908 EXPECT_CALL(observer(), | 910 EXPECT_CALL(observer(), |
909 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE)); | 911 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE)); |
910 base::RunLoop().RunUntilIdle(); | 912 base::RunLoop().RunUntilIdle(); |
911 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); | 913 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); |
912 EXPECT_FALSE(service()->GetSnippetsForTesting().empty()); | 914 EXPECT_FALSE(service()->GetSnippetsForTesting(articles_category()).empty()); |
913 } | 915 } |
914 | 916 |
915 TEST_F(NTPSnippetsServiceTest, ImageReturnedWithTheSameId) { | 917 TEST_F(NTPSnippetsServiceTest, ImageReturnedWithTheSameId) { |
916 LoadFromJSONString(GetTestJson({GetSnippet()})); | 918 LoadFromJSONString(GetTestJson({GetSnippet()})); |
917 | 919 |
918 gfx::Image image; | 920 gfx::Image image; |
919 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) | 921 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) |
920 .WillOnce(testing::WithArgs<0, 2>(Invoke(ServeOneByOneImage))); | 922 .WillOnce(testing::WithArgs<0, 2>(Invoke(ServeOneByOneImage))); |
921 testing::MockFunction<void(const std::string&, const gfx::Image&)> | 923 testing::MockFunction<void(const std::string&, const gfx::Image&)> |
922 image_fetched; | 924 image_fetched; |
(...skipping 23 matching lines...) Expand all Loading... | |
946 MakeUniqueID(kSnippetUrl2), | 948 MakeUniqueID(kSnippetUrl2), |
947 base::Bind(&testing::MockFunction<void(const std::string&, | 949 base::Bind(&testing::MockFunction<void(const std::string&, |
948 const gfx::Image&)>::Call, | 950 const gfx::Image&)>::Call, |
949 base::Unretained(&image_fetched))); | 951 base::Unretained(&image_fetched))); |
950 | 952 |
951 base::RunLoop().RunUntilIdle(); | 953 base::RunLoop().RunUntilIdle(); |
952 EXPECT_TRUE(image.IsEmpty()); | 954 EXPECT_TRUE(image.IsEmpty()); |
953 } | 955 } |
954 | 956 |
955 } // namespace ntp_snippets | 957 } // namespace ntp_snippets |
OLD | NEW |