Chromium Code Reviews| 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 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 class WaitForDBLoad { |
| 246 public: | 250 public: |
| 247 WaitForDBLoad(MockContentSuggestionsProviderObserver* observer, | 251 WaitForDBLoad(MockContentSuggestionsProviderObserver* observer, |
| 248 NTPSnippetsService* service) | 252 NTPSnippetsService* service) |
| 249 : observer_(observer) { | 253 : observer_(observer) { |
| 250 EXPECT_CALL(*observer_, OnCategoryStatusChanged(_, _, _)) | 254 EXPECT_CALL(*observer_, OnCategoryStatusChanged(_, _, _)) |
|
Philipp Keck
2016/08/26 12:03:20
If the DB loads before WaitForDBLoad is called, th
Marc Treib
2016/08/26 12:07:11
I guess the first line should read "if (service->r
| |
| 251 .WillOnce(Invoke(this, &WaitForDBLoad::OnCategoryStatusChanged)); | 255 .WillOnce(Invoke(this, &WaitForDBLoad::OnCategoryStatusChanged)); |
| 252 if (!service->ready()) | 256 if (!service->ready()) |
| 253 run_loop_.Run(); | 257 run_loop_.Run(); |
| 254 } | 258 } |
| 255 | 259 |
| 256 ~WaitForDBLoad() { Mock::VerifyAndClearExpectations(observer_); } | 260 ~WaitForDBLoad() { Mock::VerifyAndClearExpectations(observer_); } |
| 257 | 261 |
| 258 private: | 262 private: |
| 259 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, | 263 void OnCategoryStatusChanged(ContentSuggestionsProvider* provider, |
| 260 Category category, | 264 Category category, |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 359 image_fetcher_ = image_fetcher.get(); | 363 image_fetcher_ = image_fetcher.get(); |
| 360 | 364 |
| 361 // Add an initial fetch response, as the service tries to fetch when there | 365 // Add an initial fetch response, as the service tries to fetch when there |
| 362 // is nothing in the DB. | 366 // is nothing in the DB. |
| 363 SetUpFetchResponse(GetTestJson({GetSnippet()})); | 367 SetUpFetchResponse(GetTestJson({GetSnippet()})); |
| 364 | 368 |
| 365 service_.reset(new NTPSnippetsService( | 369 service_.reset(new NTPSnippetsService( |
| 366 &observer_, &category_factory_, pref_service(), nullptr, nullptr, "fr", | 370 &observer_, &category_factory_, pref_service(), nullptr, nullptr, "fr", |
| 367 &scheduler_, std::move(snippets_fetcher), | 371 &scheduler_, std::move(snippets_fetcher), |
| 368 std::move(image_fetcher), /*image_decoder=*/nullptr, | 372 std::move(image_fetcher), /*image_decoder=*/nullptr, |
| 369 base::MakeUnique<NTPSnippetsDatabase>(database_dir_.path(), | 373 base::MakeUnique<NTPSnippetsDatabase>(database_dir_.path(), |
|
Philipp Keck
2016/08/26 12:17:48
If you replace this line with:
base::MakeUnique<NT
Marc Treib
2016/08/26 12:24:42
Huh. I guess we can conclude that the underlying p
| |
| 370 task_runner), | 374 task_runner), |
| 371 base::MakeUnique<NTPSnippetsStatusService>(fake_signin_manager(), | 375 base::MakeUnique<NTPSnippetsStatusService>(fake_signin_manager(), |
| 372 pref_service()))); | 376 pref_service()))); |
| 373 | 377 |
| 374 WaitForDBLoad(&observer_, service_.get()); | 378 WaitForDBLoad(&observer_, service_.get()); |
| 375 } | 379 } |
| 376 | 380 |
| 377 std::string MakeUniqueID(const std::string& within_category_id) { | 381 std::string MakeUniqueID(const std::string& within_category_id) { |
| 378 return service()->MakeUniqueID(articles_category(), within_category_id); | 382 return service()->MakeUniqueID(articles_category(), within_category_id); |
| 379 } | 383 } |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 427 | 431 |
| 428 // When we have no snippets are all, loading the service initiates a fetch. | 432 // When we have no snippets are all, loading the service initiates a fetch. |
| 429 base::RunLoop().RunUntilIdle(); | 433 base::RunLoop().RunUntilIdle(); |
| 430 ASSERT_EQ("OK", service()->snippets_fetcher()->last_status()); | 434 ASSERT_EQ("OK", service()->snippets_fetcher()->last_status()); |
| 431 } | 435 } |
| 432 | 436 |
| 433 TEST_F(NTPSnippetsServiceTest, Full) { | 437 TEST_F(NTPSnippetsServiceTest, Full) { |
| 434 std::string json_str(GetTestJson({GetSnippet()})); | 438 std::string json_str(GetTestJson({GetSnippet()})); |
| 435 | 439 |
| 436 LoadFromJSONString(json_str); | 440 LoadFromJSONString(json_str); |
| 437 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 441 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 438 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 442 const NTPSnippet& snippet = |
| 443 *service()->GetSnippetsForTesting(articles_category()).front(); | |
| 439 | 444 |
| 440 EXPECT_EQ(snippet.id(), kSnippetUrl); | 445 EXPECT_EQ(snippet.id(), kSnippetUrl); |
| 441 EXPECT_EQ(snippet.title(), kSnippetTitle); | 446 EXPECT_EQ(snippet.title(), kSnippetTitle); |
| 442 EXPECT_EQ(snippet.snippet(), kSnippetText); | 447 EXPECT_EQ(snippet.snippet(), kSnippetText); |
| 443 EXPECT_EQ(snippet.salient_image_url(), GURL(kSnippetSalientImage)); | 448 EXPECT_EQ(snippet.salient_image_url(), GURL(kSnippetSalientImage)); |
| 444 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date()); | 449 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date()); |
| 445 EXPECT_EQ(snippet.best_source().publisher_name, kSnippetPublisherName); | 450 EXPECT_EQ(snippet.best_source().publisher_name, kSnippetPublisherName); |
| 446 EXPECT_EQ(snippet.best_source().amp_url, GURL(kSnippetAmpUrl)); | 451 EXPECT_EQ(snippet.best_source().amp_url, GURL(kSnippetAmpUrl)); |
| 447 } | 452 } |
| 448 | 453 |
| 449 TEST_F(NTPSnippetsServiceTest, Clear) { | 454 TEST_F(NTPSnippetsServiceTest, Clear) { |
| 450 std::string json_str(GetTestJson({GetSnippet()})); | 455 std::string json_str(GetTestJson({GetSnippet()})); |
| 451 | 456 |
| 452 LoadFromJSONString(json_str); | 457 LoadFromJSONString(json_str); |
| 453 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 458 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 454 | 459 |
| 455 service()->ClearCachedSuggestions(articles_category()); | 460 service()->ClearCachedSuggestions(articles_category()); |
| 456 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 461 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 457 } | 462 } |
| 458 | 463 |
| 459 TEST_F(NTPSnippetsServiceTest, InsertAtFront) { | 464 TEST_F(NTPSnippetsServiceTest, InsertAtFront) { |
| 460 std::string first("http://first"); | 465 std::string first("http://first"); |
| 461 LoadFromJSONString(GetTestJson({GetSnippetWithUrl(first)})); | 466 LoadFromJSONString(GetTestJson({GetSnippetWithUrl(first)})); |
| 462 EXPECT_THAT(service()->GetSnippetsForTesting(), ElementsAre(IdEq(first))); | 467 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), |
| 468 ElementsAre(IdEq(first))); | |
| 463 | 469 |
| 464 std::string second("http://second"); | 470 std::string second("http://second"); |
| 465 LoadFromJSONString(GetTestJson({GetSnippetWithUrl(second)})); | 471 LoadFromJSONString(GetTestJson({GetSnippetWithUrl(second)})); |
| 466 // The snippet loaded last should be at the first position in the list now. | 472 // The snippet loaded last should be at the first position in the list now. |
| 467 EXPECT_THAT(service()->GetSnippetsForTesting(), | 473 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), |
| 468 ElementsAre(IdEq(second), IdEq(first))); | 474 ElementsAre(IdEq(second), IdEq(first))); |
| 469 } | 475 } |
| 470 | 476 |
| 471 TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) { | 477 TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) { |
| 472 int max_snippet_count = NTPSnippetsService::GetMaxSnippetCountForTesting(); | 478 int max_snippet_count = NTPSnippetsService::GetMaxSnippetCountForTesting(); |
| 473 int snippets_per_load = max_snippet_count / 2 + 1; | 479 int snippets_per_load = max_snippet_count / 2 + 1; |
| 474 char url_format[] = "http://localhost/%i"; | 480 char url_format[] = "http://localhost/%i"; |
| 475 | 481 |
| 476 std::vector<std::string> snippets1; | 482 std::vector<std::string> snippets1; |
| 477 std::vector<std::string> snippets2; | 483 std::vector<std::string> snippets2; |
| 478 for (int i = 0; i < snippets_per_load; i++) { | 484 for (int i = 0; i < snippets_per_load; i++) { |
| 479 snippets1.push_back(GetSnippetWithUrl(base::StringPrintf(url_format, i))); | 485 snippets1.push_back(GetSnippetWithUrl(base::StringPrintf(url_format, i))); |
| 480 snippets2.push_back(GetSnippetWithUrl( | 486 snippets2.push_back(GetSnippetWithUrl( |
| 481 base::StringPrintf(url_format, snippets_per_load + i))); | 487 base::StringPrintf(url_format, snippets_per_load + i))); |
| 482 } | 488 } |
| 483 | 489 |
| 484 LoadFromJSONString(GetTestJson(snippets1)); | 490 LoadFromJSONString(GetTestJson(snippets1)); |
| 485 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(snippets1.size())); | 491 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), |
| 492 SizeIs(snippets1.size())); | |
| 486 | 493 |
| 487 LoadFromJSONString(GetTestJson(snippets2)); | 494 LoadFromJSONString(GetTestJson(snippets2)); |
| 488 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(max_snippet_count)); | 495 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), |
| 496 SizeIs(max_snippet_count)); | |
| 489 } | 497 } |
| 490 | 498 |
| 491 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { | 499 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { |
| 492 LoadFromJSONString(GetTestJson({GetInvalidSnippet()})); | 500 LoadFromJSONString(GetTestJson({GetInvalidSnippet()})); |
| 493 EXPECT_THAT(service()->snippets_fetcher()->last_status(), | 501 EXPECT_THAT(service()->snippets_fetcher()->last_status(), |
| 494 StartsWith("Received invalid JSON")); | 502 StartsWith("Received invalid JSON")); |
| 495 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 503 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 496 } | 504 } |
| 497 | 505 |
| 498 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) { | 506 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) { |
| 499 LoadFromJSONString(GetTestJson({GetSnippet()})); | 507 LoadFromJSONString(GetTestJson({GetSnippet()})); |
| 500 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 508 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 501 ASSERT_EQ("OK", service()->snippets_fetcher()->last_status()); | 509 ASSERT_EQ("OK", service()->snippets_fetcher()->last_status()); |
| 502 | 510 |
| 503 LoadFromJSONString(GetTestJson({GetInvalidSnippet()})); | 511 LoadFromJSONString(GetTestJson({GetInvalidSnippet()})); |
| 504 EXPECT_THAT(service()->snippets_fetcher()->last_status(), | 512 EXPECT_THAT(service()->snippets_fetcher()->last_status(), |
| 505 StartsWith("Received invalid JSON")); | 513 StartsWith("Received invalid JSON")); |
| 506 // This should not have changed the existing snippets. | 514 // This should not have changed the existing snippets. |
| 507 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 515 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 508 } | 516 } |
| 509 | 517 |
| 510 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJson) { | 518 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJson) { |
| 511 LoadFromJSONString(GetTestJson({GetIncompleteSnippet()})); | 519 LoadFromJSONString(GetTestJson({GetIncompleteSnippet()})); |
| 512 EXPECT_EQ("Invalid / empty list.", | 520 EXPECT_EQ("Invalid / empty list.", |
| 513 service()->snippets_fetcher()->last_status()); | 521 service()->snippets_fetcher()->last_status()); |
| 514 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 522 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 515 } | 523 } |
| 516 | 524 |
| 517 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) { | 525 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) { |
| 518 LoadFromJSONString(GetTestJson({GetSnippet()})); | 526 LoadFromJSONString(GetTestJson({GetSnippet()})); |
| 519 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 527 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 520 | 528 |
| 521 LoadFromJSONString(GetTestJson({GetIncompleteSnippet()})); | 529 LoadFromJSONString(GetTestJson({GetIncompleteSnippet()})); |
| 522 EXPECT_EQ("Invalid / empty list.", | 530 EXPECT_EQ("Invalid / empty list.", |
| 523 service()->snippets_fetcher()->last_status()); | 531 service()->snippets_fetcher()->last_status()); |
| 524 // This should not have changed the existing snippets. | 532 // This should not have changed the existing snippets. |
| 525 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 533 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 526 } | 534 } |
| 527 | 535 |
| 528 TEST_F(NTPSnippetsServiceTest, Dismiss) { | 536 TEST_F(NTPSnippetsServiceTest, Dismiss) { |
| 529 std::vector<std::string> source_urls, publishers, amp_urls; | 537 std::vector<std::string> source_urls, publishers, amp_urls; |
| 530 source_urls.push_back(std::string("http://site.com")); | 538 source_urls.push_back(std::string("http://site.com")); |
| 531 publishers.push_back(std::string("Source 1")); | 539 publishers.push_back(std::string("Source 1")); |
| 532 amp_urls.push_back(std::string()); | 540 amp_urls.push_back(std::string()); |
| 533 std::string json_str( | 541 std::string json_str( |
| 534 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 542 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
| 535 | 543 |
| 536 LoadFromJSONString(json_str); | 544 LoadFromJSONString(json_str); |
| 537 | 545 |
| 538 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 546 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 539 | 547 |
| 540 // Dismissing a non-existent snippet shouldn't do anything. | 548 // Dismissing a non-existent snippet shouldn't do anything. |
| 541 service()->DismissSuggestion(MakeUniqueID("http://othersite.com")); | 549 service()->DismissSuggestion(MakeUniqueID("http://othersite.com")); |
| 542 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 550 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 543 | 551 |
| 544 // Dismiss the snippet. | 552 // Dismiss the snippet. |
| 545 service()->DismissSuggestion(MakeUniqueID(kSnippetUrl)); | 553 service()->DismissSuggestion(MakeUniqueID(kSnippetUrl)); |
| 546 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 554 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 547 | 555 |
| 548 // Make sure that fetching the same snippet again does not re-add it. | 556 // Make sure that fetching the same snippet again does not re-add it. |
| 549 LoadFromJSONString(json_str); | 557 LoadFromJSONString(json_str); |
| 550 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 558 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 551 | 559 |
| 552 // The snippet should stay dismissed even after re-creating the service. | 560 // The snippet should stay dismissed even after re-creating the service. |
| 553 RecreateSnippetsService(); | 561 RecreateSnippetsService(); |
| 554 LoadFromJSONString(json_str); | 562 LoadFromJSONString(json_str); |
| 555 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 563 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 556 | 564 |
| 557 // The snippet can be added again after clearing dismissed snippets. | 565 // The snippet can be added again after clearing dismissed snippets. |
| 558 service()->ClearDismissedSuggestionsForDebugging(articles_category()); | 566 service()->ClearDismissedSuggestionsForDebugging(articles_category()); |
| 559 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 567 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 560 LoadFromJSONString(json_str); | 568 LoadFromJSONString(json_str); |
| 561 EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 569 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 562 } | 570 } |
| 563 | 571 |
| 564 TEST_F(NTPSnippetsServiceTest, GetDismissed) { | 572 TEST_F(NTPSnippetsServiceTest, GetDismissed) { |
| 565 LoadFromJSONString(GetTestJson({GetSnippet()})); | 573 LoadFromJSONString(GetTestJson({GetSnippet()})); |
| 566 | 574 |
| 567 service()->DismissSuggestion(MakeUniqueID(kSnippetUrl)); | 575 service()->DismissSuggestion(MakeUniqueID(kSnippetUrl)); |
| 568 | 576 |
| 569 MockDismissedSuggestionsCallback callback; | 577 MockDismissedSuggestionsCallback callback; |
| 570 | 578 |
| 571 EXPECT_CALL(callback, MockRun(_, _)) | 579 EXPECT_CALL(callback, MockRun(_, _)) |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 596 base::Bind(&MockDismissedSuggestionsCallback::Run, | 604 base::Bind(&MockDismissedSuggestionsCallback::Run, |
| 597 base::Unretained(&callback), articles_category())); | 605 base::Unretained(&callback), articles_category())); |
| 598 } | 606 } |
| 599 | 607 |
| 600 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { | 608 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { |
| 601 std::string json_str(GetTestJson({GetSnippetWithTimes( | 609 std::string json_str(GetTestJson({GetSnippetWithTimes( |
| 602 "aaa1448459205", | 610 "aaa1448459205", |
| 603 NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()))})); | 611 NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()))})); |
| 604 | 612 |
| 605 LoadFromJSONString(json_str); | 613 LoadFromJSONString(json_str); |
| 606 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 614 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 607 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 615 const NTPSnippet& snippet = |
| 616 *service()->GetSnippetsForTesting(articles_category()).front(); | |
| 608 EXPECT_EQ(snippet.id(), kSnippetUrl); | 617 EXPECT_EQ(snippet.id(), kSnippetUrl); |
| 609 EXPECT_EQ(snippet.title(), kSnippetTitle); | 618 EXPECT_EQ(snippet.title(), kSnippetTitle); |
| 610 EXPECT_EQ(snippet.snippet(), kSnippetText); | 619 EXPECT_EQ(snippet.snippet(), kSnippetText); |
| 611 EXPECT_EQ(base::Time::UnixEpoch(), snippet.publish_date()); | 620 EXPECT_EQ(base::Time::UnixEpoch(), snippet.publish_date()); |
| 612 } | 621 } |
| 613 | 622 |
| 614 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { | 623 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { |
| 615 std::string json_str(GetTestJson({GetExpiredSnippet()})); | 624 std::string json_str(GetTestJson({GetExpiredSnippet()})); |
| 616 | 625 |
| 617 LoadFromJSONString(json_str); | 626 LoadFromJSONString(json_str); |
| 618 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 627 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 619 } | 628 } |
| 620 | 629 |
| 621 TEST_F(NTPSnippetsServiceTest, TestSingleSource) { | 630 TEST_F(NTPSnippetsServiceTest, TestSingleSource) { |
| 622 std::vector<std::string> source_urls, publishers, amp_urls; | 631 std::vector<std::string> source_urls, publishers, amp_urls; |
| 623 source_urls.push_back(std::string("http://source1.com")); | 632 source_urls.push_back(std::string("http://source1.com")); |
| 624 publishers.push_back(std::string("Source 1")); | 633 publishers.push_back(std::string("Source 1")); |
| 625 amp_urls.push_back(std::string("http://source1.amp.com")); | 634 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 626 std::string json_str( | 635 std::string json_str( |
| 627 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 636 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
| 628 | 637 |
| 629 LoadFromJSONString(json_str); | 638 LoadFromJSONString(json_str); |
| 630 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 639 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 631 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 640 const NTPSnippet& snippet = |
| 641 *service()->GetSnippetsForTesting(articles_category()).front(); | |
| 632 EXPECT_EQ(snippet.sources().size(), 1u); | 642 EXPECT_EQ(snippet.sources().size(), 1u); |
| 633 EXPECT_EQ(snippet.id(), kSnippetUrl); | 643 EXPECT_EQ(snippet.id(), kSnippetUrl); |
| 634 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); | 644 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); |
| 635 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); | 645 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
| 636 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); | 646 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); |
| 637 } | 647 } |
| 638 | 648 |
| 639 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMalformedUrl) { | 649 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMalformedUrl) { |
| 640 std::vector<std::string> source_urls, publishers, amp_urls; | 650 std::vector<std::string> source_urls, publishers, amp_urls; |
| 641 source_urls.push_back(std::string("aaaa")); | 651 source_urls.push_back(std::string("aaaa")); |
| 642 publishers.push_back(std::string("Source 1")); | 652 publishers.push_back(std::string("Source 1")); |
| 643 amp_urls.push_back(std::string("http://source1.amp.com")); | 653 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 644 std::string json_str( | 654 std::string json_str( |
| 645 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 655 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
| 646 | 656 |
| 647 LoadFromJSONString(json_str); | 657 LoadFromJSONString(json_str); |
| 648 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 658 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 649 } | 659 } |
| 650 | 660 |
| 651 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMissingData) { | 661 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMissingData) { |
| 652 std::vector<std::string> source_urls, publishers, amp_urls; | 662 std::vector<std::string> source_urls, publishers, amp_urls; |
| 653 source_urls.push_back(std::string("http://source1.com")); | 663 source_urls.push_back(std::string("http://source1.com")); |
| 654 publishers.push_back(std::string()); | 664 publishers.push_back(std::string()); |
| 655 amp_urls.push_back(std::string()); | 665 amp_urls.push_back(std::string()); |
| 656 std::string json_str( | 666 std::string json_str( |
| 657 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 667 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
| 658 | 668 |
| 659 LoadFromJSONString(json_str); | 669 LoadFromJSONString(json_str); |
| 660 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 670 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 661 } | 671 } |
| 662 | 672 |
| 663 TEST_F(NTPSnippetsServiceTest, TestMultipleSources) { | 673 TEST_F(NTPSnippetsServiceTest, TestMultipleSources) { |
| 664 std::vector<std::string> source_urls, publishers, amp_urls; | 674 std::vector<std::string> source_urls, publishers, amp_urls; |
| 665 source_urls.push_back(std::string("http://source1.com")); | 675 source_urls.push_back(std::string("http://source1.com")); |
| 666 source_urls.push_back(std::string("http://source2.com")); | 676 source_urls.push_back(std::string("http://source2.com")); |
| 667 publishers.push_back(std::string("Source 1")); | 677 publishers.push_back(std::string("Source 1")); |
| 668 publishers.push_back(std::string("Source 2")); | 678 publishers.push_back(std::string("Source 2")); |
| 669 amp_urls.push_back(std::string("http://source1.amp.com")); | 679 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 670 amp_urls.push_back(std::string("http://source2.amp.com")); | 680 amp_urls.push_back(std::string("http://source2.amp.com")); |
| 671 std::string json_str( | 681 std::string json_str( |
| 672 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 682 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
| 673 | 683 |
| 674 LoadFromJSONString(json_str); | 684 LoadFromJSONString(json_str); |
| 675 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 685 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 676 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 686 const NTPSnippet& snippet = |
| 687 *service()->GetSnippetsForTesting(articles_category()).front(); | |
| 677 // Expect the first source to be chosen | 688 // Expect the first source to be chosen |
| 678 EXPECT_EQ(snippet.sources().size(), 2u); | 689 EXPECT_EQ(snippet.sources().size(), 2u); |
| 679 EXPECT_EQ(snippet.id(), kSnippetUrl); | 690 EXPECT_EQ(snippet.id(), kSnippetUrl); |
| 680 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); | 691 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); |
| 681 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); | 692 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
| 682 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); | 693 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); |
| 683 } | 694 } |
| 684 | 695 |
| 685 TEST_F(NTPSnippetsServiceTest, TestMultipleIncompleteSources) { | 696 TEST_F(NTPSnippetsServiceTest, TestMultipleIncompleteSources) { |
| 686 // Set Source 2 to have no AMP url, and Source 1 to have no publisher name | 697 // 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 | 698 // Source 2 should win since we favor publisher name over amp url |
| 688 std::vector<std::string> source_urls, publishers, amp_urls; | 699 std::vector<std::string> source_urls, publishers, amp_urls; |
| 689 source_urls.push_back(std::string("http://source1.com")); | 700 source_urls.push_back(std::string("http://source1.com")); |
| 690 source_urls.push_back(std::string("http://source2.com")); | 701 source_urls.push_back(std::string("http://source2.com")); |
| 691 publishers.push_back(std::string()); | 702 publishers.push_back(std::string()); |
| 692 publishers.push_back(std::string("Source 2")); | 703 publishers.push_back(std::string("Source 2")); |
| 693 amp_urls.push_back(std::string("http://source1.amp.com")); | 704 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 694 amp_urls.push_back(std::string()); | 705 amp_urls.push_back(std::string()); |
| 695 std::string json_str( | 706 std::string json_str( |
| 696 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 707 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
| 697 | 708 |
| 698 LoadFromJSONString(json_str); | 709 LoadFromJSONString(json_str); |
| 699 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 710 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 700 { | 711 { |
| 701 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 712 const NTPSnippet& snippet = |
| 713 *service()->GetSnippetsForTesting(articles_category()).front(); | |
| 702 EXPECT_EQ(snippet.sources().size(), 2u); | 714 EXPECT_EQ(snippet.sources().size(), 2u); |
| 703 EXPECT_EQ(snippet.id(), kSnippetUrl); | 715 EXPECT_EQ(snippet.id(), kSnippetUrl); |
| 704 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); | 716 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); |
| 705 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); | 717 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); |
| 706 EXPECT_EQ(snippet.best_source().amp_url, GURL()); | 718 EXPECT_EQ(snippet.best_source().amp_url, GURL()); |
| 707 } | 719 } |
| 708 | 720 |
| 709 service()->ClearCachedSuggestions(articles_category()); | 721 service()->ClearCachedSuggestions(articles_category()); |
| 710 // Set Source 1 to have no AMP url, and Source 2 to have no publisher name | 722 // 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 | 723 // Source 1 should win in this case since we prefer publisher name to AMP url |
| 712 source_urls.clear(); | 724 source_urls.clear(); |
| 713 source_urls.push_back(std::string("http://source1.com")); | 725 source_urls.push_back(std::string("http://source1.com")); |
| 714 source_urls.push_back(std::string("http://source2.com")); | 726 source_urls.push_back(std::string("http://source2.com")); |
| 715 publishers.clear(); | 727 publishers.clear(); |
| 716 publishers.push_back(std::string("Source 1")); | 728 publishers.push_back(std::string("Source 1")); |
| 717 publishers.push_back(std::string()); | 729 publishers.push_back(std::string()); |
| 718 amp_urls.clear(); | 730 amp_urls.clear(); |
| 719 amp_urls.push_back(std::string()); | 731 amp_urls.push_back(std::string()); |
| 720 amp_urls.push_back(std::string("http://source2.amp.com")); | 732 amp_urls.push_back(std::string("http://source2.amp.com")); |
| 721 json_str = | 733 json_str = |
| 722 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); | 734 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); |
| 723 | 735 |
| 724 LoadFromJSONString(json_str); | 736 LoadFromJSONString(json_str); |
| 725 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 737 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 726 { | 738 { |
| 727 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 739 const NTPSnippet& snippet = |
| 740 *service()->GetSnippetsForTesting(articles_category()).front(); | |
| 728 EXPECT_EQ(snippet.sources().size(), 2u); | 741 EXPECT_EQ(snippet.sources().size(), 2u); |
| 729 EXPECT_EQ(snippet.id(), kSnippetUrl); | 742 EXPECT_EQ(snippet.id(), kSnippetUrl); |
| 730 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); | 743 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); |
| 731 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); | 744 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
| 732 EXPECT_EQ(snippet.best_source().amp_url, GURL()); | 745 EXPECT_EQ(snippet.best_source().amp_url, GURL()); |
| 733 } | 746 } |
| 734 | 747 |
| 735 service()->ClearCachedSuggestions(articles_category()); | 748 service()->ClearCachedSuggestions(articles_category()); |
| 736 // Set source 1 to have no AMP url and no source, and source 2 to only have | 749 // 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 | 750 // amp url. There should be no snippets since we only add sources we consider |
| 738 // complete | 751 // complete |
| 739 source_urls.clear(); | 752 source_urls.clear(); |
| 740 source_urls.push_back(std::string("http://source1.com")); | 753 source_urls.push_back(std::string("http://source1.com")); |
| 741 source_urls.push_back(std::string("http://source2.com")); | 754 source_urls.push_back(std::string("http://source2.com")); |
| 742 publishers.clear(); | 755 publishers.clear(); |
| 743 publishers.push_back(std::string()); | 756 publishers.push_back(std::string()); |
| 744 publishers.push_back(std::string()); | 757 publishers.push_back(std::string()); |
| 745 amp_urls.clear(); | 758 amp_urls.clear(); |
| 746 amp_urls.push_back(std::string()); | 759 amp_urls.push_back(std::string()); |
| 747 amp_urls.push_back(std::string("http://source2.amp.com")); | 760 amp_urls.push_back(std::string("http://source2.amp.com")); |
| 748 json_str = | 761 json_str = |
| 749 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); | 762 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); |
| 750 | 763 |
| 751 LoadFromJSONString(json_str); | 764 LoadFromJSONString(json_str); |
| 752 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 765 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 753 } | 766 } |
| 754 | 767 |
| 755 TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) { | 768 TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) { |
| 756 // Test 2 complete sources, we should choose the first complete source | 769 // Test 2 complete sources, we should choose the first complete source |
| 757 std::vector<std::string> source_urls, publishers, amp_urls; | 770 std::vector<std::string> source_urls, publishers, amp_urls; |
| 758 source_urls.push_back(std::string("http://source1.com")); | 771 source_urls.push_back(std::string("http://source1.com")); |
| 759 source_urls.push_back(std::string("http://source2.com")); | 772 source_urls.push_back(std::string("http://source2.com")); |
| 760 source_urls.push_back(std::string("http://source3.com")); | 773 source_urls.push_back(std::string("http://source3.com")); |
| 761 publishers.push_back(std::string("Source 1")); | 774 publishers.push_back(std::string("Source 1")); |
| 762 publishers.push_back(std::string()); | 775 publishers.push_back(std::string()); |
| 763 publishers.push_back(std::string("Source 3")); | 776 publishers.push_back(std::string("Source 3")); |
| 764 amp_urls.push_back(std::string("http://source1.amp.com")); | 777 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 765 amp_urls.push_back(std::string("http://source2.amp.com")); | 778 amp_urls.push_back(std::string("http://source2.amp.com")); |
| 766 amp_urls.push_back(std::string("http://source3.amp.com")); | 779 amp_urls.push_back(std::string("http://source3.amp.com")); |
| 767 std::string json_str( | 780 std::string json_str( |
| 768 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); | 781 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)})); |
| 769 | 782 |
| 770 LoadFromJSONString(json_str); | 783 LoadFromJSONString(json_str); |
| 771 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 784 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 772 { | 785 { |
| 773 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 786 const NTPSnippet& snippet = |
| 787 *service()->GetSnippetsForTesting(articles_category()).front(); | |
| 774 EXPECT_EQ(snippet.sources().size(), 3u); | 788 EXPECT_EQ(snippet.sources().size(), 3u); |
| 775 EXPECT_EQ(snippet.id(), kSnippetUrl); | 789 EXPECT_EQ(snippet.id(), kSnippetUrl); |
| 776 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); | 790 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); |
| 777 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); | 791 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); |
| 778 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); | 792 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); |
| 779 } | 793 } |
| 780 | 794 |
| 781 // Test 2 complete sources, we should choose the first complete source | 795 // Test 2 complete sources, we should choose the first complete source |
| 782 service()->ClearCachedSuggestions(articles_category()); | 796 service()->ClearCachedSuggestions(articles_category()); |
| 783 source_urls.clear(); | 797 source_urls.clear(); |
| 784 source_urls.push_back(std::string("http://source1.com")); | 798 source_urls.push_back(std::string("http://source1.com")); |
| 785 source_urls.push_back(std::string("http://source2.com")); | 799 source_urls.push_back(std::string("http://source2.com")); |
| 786 source_urls.push_back(std::string("http://source3.com")); | 800 source_urls.push_back(std::string("http://source3.com")); |
| 787 publishers.clear(); | 801 publishers.clear(); |
| 788 publishers.push_back(std::string()); | 802 publishers.push_back(std::string()); |
| 789 publishers.push_back(std::string("Source 2")); | 803 publishers.push_back(std::string("Source 2")); |
| 790 publishers.push_back(std::string("Source 3")); | 804 publishers.push_back(std::string("Source 3")); |
| 791 amp_urls.clear(); | 805 amp_urls.clear(); |
| 792 amp_urls.push_back(std::string("http://source1.amp.com")); | 806 amp_urls.push_back(std::string("http://source1.amp.com")); |
| 793 amp_urls.push_back(std::string("http://source2.amp.com")); | 807 amp_urls.push_back(std::string("http://source2.amp.com")); |
| 794 amp_urls.push_back(std::string("http://source3.amp.com")); | 808 amp_urls.push_back(std::string("http://source3.amp.com")); |
| 795 json_str = | 809 json_str = |
| 796 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); | 810 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); |
| 797 | 811 |
| 798 LoadFromJSONString(json_str); | 812 LoadFromJSONString(json_str); |
| 799 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 813 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 800 { | 814 { |
| 801 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 815 const NTPSnippet& snippet = |
| 816 *service()->GetSnippetsForTesting(articles_category()).front(); | |
| 802 EXPECT_EQ(snippet.sources().size(), 3u); | 817 EXPECT_EQ(snippet.sources().size(), 3u); |
| 803 EXPECT_EQ(snippet.id(), kSnippetUrl); | 818 EXPECT_EQ(snippet.id(), kSnippetUrl); |
| 804 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); | 819 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); |
| 805 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); | 820 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); |
| 806 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); | 821 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); |
| 807 } | 822 } |
| 808 | 823 |
| 809 // Test 3 complete sources, we should choose the first complete source | 824 // Test 3 complete sources, we should choose the first complete source |
| 810 service()->ClearCachedSuggestions(articles_category()); | 825 service()->ClearCachedSuggestions(articles_category()); |
| 811 source_urls.clear(); | 826 source_urls.clear(); |
| 812 source_urls.push_back(std::string("http://source1.com")); | 827 source_urls.push_back(std::string("http://source1.com")); |
| 813 source_urls.push_back(std::string("http://source2.com")); | 828 source_urls.push_back(std::string("http://source2.com")); |
| 814 source_urls.push_back(std::string("http://source3.com")); | 829 source_urls.push_back(std::string("http://source3.com")); |
| 815 publishers.clear(); | 830 publishers.clear(); |
| 816 publishers.push_back(std::string("Source 1")); | 831 publishers.push_back(std::string("Source 1")); |
| 817 publishers.push_back(std::string("Source 2")); | 832 publishers.push_back(std::string("Source 2")); |
| 818 publishers.push_back(std::string("Source 3")); | 833 publishers.push_back(std::string("Source 3")); |
| 819 amp_urls.clear(); | 834 amp_urls.clear(); |
| 820 amp_urls.push_back(std::string()); | 835 amp_urls.push_back(std::string()); |
| 821 amp_urls.push_back(std::string("http://source2.amp.com")); | 836 amp_urls.push_back(std::string("http://source2.amp.com")); |
| 822 amp_urls.push_back(std::string("http://source3.amp.com")); | 837 amp_urls.push_back(std::string("http://source3.amp.com")); |
| 823 json_str = | 838 json_str = |
| 824 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); | 839 GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}); |
| 825 | 840 |
| 826 LoadFromJSONString(json_str); | 841 LoadFromJSONString(json_str); |
| 827 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 842 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 828 { | 843 { |
| 829 const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front(); | 844 const NTPSnippet& snippet = |
| 845 *service()->GetSnippetsForTesting(articles_category()).front(); | |
| 830 EXPECT_EQ(snippet.sources().size(), 3u); | 846 EXPECT_EQ(snippet.sources().size(), 3u); |
| 831 EXPECT_EQ(snippet.id(), kSnippetUrl); | 847 EXPECT_EQ(snippet.id(), kSnippetUrl); |
| 832 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); | 848 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); |
| 833 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); | 849 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); |
| 834 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); | 850 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); |
| 835 } | 851 } |
| 836 } | 852 } |
| 837 | 853 |
| 838 TEST_F(NTPSnippetsServiceTest, LogNumArticlesHistogram) { | 854 TEST_F(NTPSnippetsServiceTest, LogNumArticlesHistogram) { |
| 839 base::HistogramTester tester; | 855 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"}; | 916 "http://mashable.com/2016/05/11/stolen?utm_cid=1"}; |
| 901 const std::vector<std::string> publishers = {"Mashable", "AOL", "Mashable"}; | 917 const std::vector<std::string> publishers = {"Mashable", "AOL", "Mashable"}; |
| 902 const std::vector<std::string> amp_urls = { | 918 const std::vector<std::string> amp_urls = { |
| 903 "http://mashable-amphtml.googleusercontent.com/1", | 919 "http://mashable-amphtml.googleusercontent.com/1", |
| 904 "http://t2.gstatic.com/images?q=tbn:3", | 920 "http://t2.gstatic.com/images?q=tbn:3", |
| 905 "http://t2.gstatic.com/images?q=tbn:3"}; | 921 "http://t2.gstatic.com/images?q=tbn:3"}; |
| 906 | 922 |
| 907 // Add the snippet from the mashable domain. | 923 // Add the snippet from the mashable domain. |
| 908 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( | 924 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( |
| 909 source_urls[0], creation, expiry, source_urls, publishers, amp_urls)})); | 925 source_urls[0], creation, expiry, source_urls, publishers, amp_urls)})); |
| 910 ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1)); | 926 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1)); |
| 911 // Dismiss the snippet via the mashable source corpus ID. | 927 // Dismiss the snippet via the mashable source corpus ID. |
| 912 service()->DismissSuggestion(MakeUniqueID(source_urls[0])); | 928 service()->DismissSuggestion(MakeUniqueID(source_urls[0])); |
| 913 EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 929 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 914 | 930 |
| 915 // The same article from the AOL domain should now be detected as dismissed. | 931 // The same article from the AOL domain should now be detected as dismissed. |
| 916 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( | 932 LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources( |
| 917 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)})); | 933 source_urls[1], creation, expiry, source_urls, publishers, amp_urls)})); |
| 918 ASSERT_THAT(service()->GetSnippetsForTesting(), IsEmpty()); | 934 ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty()); |
| 919 } | 935 } |
| 920 | 936 |
| 921 TEST_F(NTPSnippetsServiceTest, StatusChanges) { | 937 TEST_F(NTPSnippetsServiceTest, StatusChanges) { |
| 922 // Simulate user signed out | 938 // Simulate user signed out |
| 923 SetUpFetchResponse(GetTestJson({GetSnippet()})); | 939 SetUpFetchResponse(GetTestJson({GetSnippet()})); |
| 924 EXPECT_CALL(observer(), | 940 EXPECT_CALL(observer(), |
| 925 OnCategoryStatusChanged(_, _, CategoryStatus::SIGNED_OUT)); | 941 OnCategoryStatusChanged(_, _, CategoryStatus::SIGNED_OUT)); |
| 926 service()->OnDisabledReasonChanged(DisabledReason::SIGNED_OUT); | 942 service()->OnDisabledReasonChanged(DisabledReason::SIGNED_OUT); |
| 927 base::RunLoop().RunUntilIdle(); | 943 base::RunLoop().RunUntilIdle(); |
| 928 EXPECT_EQ(NTPSnippetsService::State::DISABLED, service()->state_); | 944 EXPECT_EQ(NTPSnippetsService::State::DISABLED, service()->state_); |
| 929 EXPECT_THAT(service()->GetSnippetsForTesting(), | 945 EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), |
| 930 IsEmpty()); // No fetch should be made. | 946 IsEmpty()); // No fetch should be made. |
| 931 | 947 |
| 932 // Simulate user sign in. The service should be ready again and load snippets. | 948 // Simulate user sign in. The service should be ready again and load snippets. |
| 933 SetUpFetchResponse(GetTestJson({GetSnippet()})); | 949 SetUpFetchResponse(GetTestJson({GetSnippet()})); |
| 934 EXPECT_CALL(observer(), | 950 EXPECT_CALL(observer(), |
| 935 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE_LOADING)); | 951 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE_LOADING)); |
| 936 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); | 952 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); |
| 937 service()->OnDisabledReasonChanged(DisabledReason::NONE); | 953 service()->OnDisabledReasonChanged(DisabledReason::NONE); |
| 938 EXPECT_CALL(observer(), | 954 EXPECT_CALL(observer(), |
| 939 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE)); | 955 OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE)); |
| 940 base::RunLoop().RunUntilIdle(); | 956 base::RunLoop().RunUntilIdle(); |
| 941 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); | 957 EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_); |
| 942 EXPECT_FALSE(service()->GetSnippetsForTesting().empty()); | 958 EXPECT_FALSE(service()->GetSnippetsForTesting(articles_category()).empty()); |
| 943 } | 959 } |
| 944 | 960 |
| 945 TEST_F(NTPSnippetsServiceTest, ImageReturnedWithTheSameId) { | 961 TEST_F(NTPSnippetsServiceTest, ImageReturnedWithTheSameId) { |
| 946 LoadFromJSONString(GetTestJson({GetSnippet()})); | 962 LoadFromJSONString(GetTestJson({GetSnippet()})); |
| 947 | 963 |
| 948 gfx::Image image; | 964 gfx::Image image; |
| 949 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) | 965 EXPECT_CALL(*image_fetcher(), StartOrQueueNetworkRequest(_, _, _)) |
| 950 .WillOnce(testing::WithArgs<0, 2>(Invoke(ServeOneByOneImage))); | 966 .WillOnce(testing::WithArgs<0, 2>(Invoke(ServeOneByOneImage))); |
| 951 testing::MockFunction<void(const std::string&, const gfx::Image&)> | 967 testing::MockFunction<void(const std::string&, const gfx::Image&)> |
| 952 image_fetched; | 968 image_fetched; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 976 MakeUniqueID(kSnippetUrl2), | 992 MakeUniqueID(kSnippetUrl2), |
| 977 base::Bind(&testing::MockFunction<void(const std::string&, | 993 base::Bind(&testing::MockFunction<void(const std::string&, |
| 978 const gfx::Image&)>::Call, | 994 const gfx::Image&)>::Call, |
| 979 base::Unretained(&image_fetched))); | 995 base::Unretained(&image_fetched))); |
| 980 | 996 |
| 981 base::RunLoop().RunUntilIdle(); | 997 base::RunLoop().RunUntilIdle(); |
| 982 EXPECT_TRUE(image.IsEmpty()); | 998 EXPECT_TRUE(image.IsEmpty()); |
| 983 } | 999 } |
| 984 | 1000 |
| 985 } // namespace ntp_snippets | 1001 } // namespace ntp_snippets |
| OLD | NEW |