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