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