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

Unified Diff: components/ntp_snippets/ntp_snippets_service_unittest.cc

Issue 2255783002: Support server categories in NTPSnippetsService. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix after rebase Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/ntp_snippets/ntp_snippets_service_unittest.cc
diff --git a/components/ntp_snippets/ntp_snippets_service_unittest.cc b/components/ntp_snippets/ntp_snippets_service_unittest.cc
index 6c551c44b47b2b26dae18c2a2fddb40023ff2cf2..e578f179c819884813bbfa4c8a28cf3a9d4f3e7d 100644
--- a/components/ntp_snippets/ntp_snippets_service_unittest.cc
+++ b/components/ntp_snippets/ntp_snippets_service_unittest.cc
@@ -64,6 +64,10 @@ MATCHER_P(IdEq, value, "") {
return arg->id() == value;
}
+MATCHER_P(IsCategory, id, "") {
+ return arg.id() == static_cast<int>(id);
+}
+
const base::Time::Exploded kDefaultCreationTime = {2015, 11, 4, 25, 13, 46, 45};
const char kTestContentSnippetsServerFormat[] =
"https://chromereader-pa.googleapis.com/v1/fetch?key=%s";
@@ -242,32 +246,18 @@ class MockScheduler : public NTPSnippetsScheduler {
MOCK_METHOD0(Unschedule, bool());
};
-class WaitForDBLoad {
- public:
- WaitForDBLoad(MockContentSuggestionsProviderObserver* observer,
- NTPSnippetsService* service)
- : observer_(observer) {
- EXPECT_CALL(*observer_, OnCategoryStatusChanged(_, _, _))
- .WillOnce(Invoke(this, &WaitForDBLoad::OnCategoryStatusChanged));
- if (!service->ready())
- run_loop_.Run();
- }
-
- ~WaitForDBLoad() { Mock::VerifyAndClearExpectations(observer_); }
-
- private:
- void OnCategoryStatusChanged(ContentSuggestionsProvider* provider,
- Category category,
- CategoryStatus new_status) {
- EXPECT_EQ(new_status, CategoryStatus::AVAILABLE_LOADING);
- run_loop_.Quit();
- }
-
- MockContentSuggestionsProviderObserver* observer_;
- base::RunLoop run_loop_;
-
- DISALLOW_COPY_AND_ASSIGN(WaitForDBLoad);
-};
+void WaitForDBLoad(MockContentSuggestionsProviderObserver* observer,
+ NTPSnippetsService* service) {
+ if (service->ready())
+ return;
+ base::RunLoop run_loop;
+ EXPECT_CALL(*observer,
+ OnCategoryStatusChanged(_, IsCategory(KnownCategories::ARTICLES),
+ CategoryStatus::AVAILABLE_LOADING))
+ .WillOnce(testing::InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit));
+ run_loop.Run();
+ Mock::VerifyAndClearExpectations(observer);
+}
class MockImageFetcher : public ImageFetcher {
public:
@@ -434,8 +424,9 @@ TEST_F(NTPSnippetsServiceTest, Full) {
std::string json_str(GetTestJson({GetSnippet()}));
LoadFromJSONString(json_str);
- ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
- const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front();
+ ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
+ const NTPSnippet& snippet =
+ *service()->GetSnippetsForTesting(articles_category()).front();
EXPECT_EQ(snippet.id(), kSnippetUrl);
EXPECT_EQ(snippet.title(), kSnippetTitle);
@@ -450,21 +441,22 @@ TEST_F(NTPSnippetsServiceTest, Clear) {
std::string json_str(GetTestJson({GetSnippet()}));
LoadFromJSONString(json_str);
- EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
service()->ClearCachedSuggestions(articles_category());
- EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty());
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty());
}
TEST_F(NTPSnippetsServiceTest, InsertAtFront) {
std::string first("http://first");
LoadFromJSONString(GetTestJson({GetSnippetWithUrl(first)}));
- EXPECT_THAT(service()->GetSnippetsForTesting(), ElementsAre(IdEq(first)));
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()),
+ ElementsAre(IdEq(first)));
std::string second("http://second");
LoadFromJSONString(GetTestJson({GetSnippetWithUrl(second)}));
// The snippet loaded last should be at the first position in the list now.
- EXPECT_THAT(service()->GetSnippetsForTesting(),
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()),
ElementsAre(IdEq(second), IdEq(first)));
}
@@ -482,47 +474,49 @@ TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) {
}
LoadFromJSONString(GetTestJson(snippets1));
- ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(snippets1.size()));
+ ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()),
+ SizeIs(snippets1.size()));
LoadFromJSONString(GetTestJson(snippets2));
- EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(max_snippet_count));
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()),
+ SizeIs(max_snippet_count));
}
TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) {
LoadFromJSONString(GetTestJson({GetInvalidSnippet()}));
EXPECT_THAT(service()->snippets_fetcher()->last_status(),
StartsWith("Received invalid JSON"));
- EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty());
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty());
}
TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) {
LoadFromJSONString(GetTestJson({GetSnippet()}));
- ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
+ ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
ASSERT_EQ("OK", service()->snippets_fetcher()->last_status());
LoadFromJSONString(GetTestJson({GetInvalidSnippet()}));
EXPECT_THAT(service()->snippets_fetcher()->last_status(),
StartsWith("Received invalid JSON"));
// This should not have changed the existing snippets.
- EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
}
TEST_F(NTPSnippetsServiceTest, LoadIncompleteJson) {
LoadFromJSONString(GetTestJson({GetIncompleteSnippet()}));
EXPECT_EQ("Invalid / empty list.",
service()->snippets_fetcher()->last_status());
- EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty());
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty());
}
TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) {
LoadFromJSONString(GetTestJson({GetSnippet()}));
- ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
+ ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
LoadFromJSONString(GetTestJson({GetIncompleteSnippet()}));
EXPECT_EQ("Invalid / empty list.",
service()->snippets_fetcher()->last_status());
// This should not have changed the existing snippets.
- EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
}
TEST_F(NTPSnippetsServiceTest, Dismiss) {
@@ -535,30 +529,30 @@ TEST_F(NTPSnippetsServiceTest, Dismiss) {
LoadFromJSONString(json_str);
- ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
+ ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
// Dismissing a non-existent snippet shouldn't do anything.
service()->DismissSuggestion(MakeUniqueID("http://othersite.com"));
- EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
// Dismiss the snippet.
service()->DismissSuggestion(MakeUniqueID(kSnippetUrl));
- EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty());
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty());
// Make sure that fetching the same snippet again does not re-add it.
LoadFromJSONString(json_str);
- EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty());
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty());
// The snippet should stay dismissed even after re-creating the service.
RecreateSnippetsService();
LoadFromJSONString(json_str);
- EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty());
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty());
// The snippet can be added again after clearing dismissed snippets.
service()->ClearDismissedSuggestionsForDebugging(articles_category());
- EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty());
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty());
LoadFromJSONString(json_str);
- EXPECT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
}
TEST_F(NTPSnippetsServiceTest, GetDismissed) {
@@ -603,8 +597,9 @@ TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) {
NTPSnippet::TimeToJsonString(GetDefaultExpirationTime()))}));
LoadFromJSONString(json_str);
- ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
- const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front();
+ ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
+ const NTPSnippet& snippet =
+ *service()->GetSnippetsForTesting(articles_category()).front();
EXPECT_EQ(snippet.id(), kSnippetUrl);
EXPECT_EQ(snippet.title(), kSnippetTitle);
EXPECT_EQ(snippet.snippet(), kSnippetText);
@@ -615,7 +610,7 @@ TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) {
std::string json_str(GetTestJson({GetExpiredSnippet()}));
LoadFromJSONString(json_str);
- EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty());
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty());
}
TEST_F(NTPSnippetsServiceTest, TestSingleSource) {
@@ -627,8 +622,9 @@ TEST_F(NTPSnippetsServiceTest, TestSingleSource) {
GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}));
LoadFromJSONString(json_str);
- ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
- const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front();
+ ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
+ const NTPSnippet& snippet =
+ *service()->GetSnippetsForTesting(articles_category()).front();
EXPECT_EQ(snippet.sources().size(), 1u);
EXPECT_EQ(snippet.id(), kSnippetUrl);
EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com"));
@@ -645,7 +641,7 @@ TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMalformedUrl) {
GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}));
LoadFromJSONString(json_str);
- EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty());
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty());
}
TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMissingData) {
@@ -657,7 +653,7 @@ TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMissingData) {
GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}));
LoadFromJSONString(json_str);
- EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty());
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty());
}
TEST_F(NTPSnippetsServiceTest, TestMultipleSources) {
@@ -672,8 +668,9 @@ TEST_F(NTPSnippetsServiceTest, TestMultipleSources) {
GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}));
LoadFromJSONString(json_str);
- ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
- const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front();
+ ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
+ const NTPSnippet& snippet =
+ *service()->GetSnippetsForTesting(articles_category()).front();
// Expect the first source to be chosen
EXPECT_EQ(snippet.sources().size(), 2u);
EXPECT_EQ(snippet.id(), kSnippetUrl);
@@ -696,9 +693,10 @@ TEST_F(NTPSnippetsServiceTest, TestMultipleIncompleteSources) {
GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}));
LoadFromJSONString(json_str);
- ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
+ ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
{
- const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front();
+ const NTPSnippet& snippet =
+ *service()->GetSnippetsForTesting(articles_category()).front();
EXPECT_EQ(snippet.sources().size(), 2u);
EXPECT_EQ(snippet.id(), kSnippetUrl);
EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com"));
@@ -722,9 +720,10 @@ TEST_F(NTPSnippetsServiceTest, TestMultipleIncompleteSources) {
GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)});
LoadFromJSONString(json_str);
- ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
+ ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
{
- const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front();
+ const NTPSnippet& snippet =
+ *service()->GetSnippetsForTesting(articles_category()).front();
EXPECT_EQ(snippet.sources().size(), 2u);
EXPECT_EQ(snippet.id(), kSnippetUrl);
EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com"));
@@ -749,7 +748,7 @@ TEST_F(NTPSnippetsServiceTest, TestMultipleIncompleteSources) {
GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)});
LoadFromJSONString(json_str);
- EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty());
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty());
}
TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) {
@@ -768,9 +767,10 @@ TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) {
GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)}));
LoadFromJSONString(json_str);
- ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
+ ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
{
- const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front();
+ const NTPSnippet& snippet =
+ *service()->GetSnippetsForTesting(articles_category()).front();
EXPECT_EQ(snippet.sources().size(), 3u);
EXPECT_EQ(snippet.id(), kSnippetUrl);
EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com"));
@@ -796,9 +796,10 @@ TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) {
GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)});
LoadFromJSONString(json_str);
- ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
+ ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
{
- const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front();
+ const NTPSnippet& snippet =
+ *service()->GetSnippetsForTesting(articles_category()).front();
EXPECT_EQ(snippet.sources().size(), 3u);
EXPECT_EQ(snippet.id(), kSnippetUrl);
EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com"));
@@ -824,9 +825,10 @@ TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) {
GetTestJson({GetSnippetWithSources(source_urls, publishers, amp_urls)});
LoadFromJSONString(json_str);
- ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
+ ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
{
- const NTPSnippet& snippet = *service()->GetSnippetsForTesting().front();
+ const NTPSnippet& snippet =
+ *service()->GetSnippetsForTesting(articles_category()).front();
EXPECT_EQ(snippet.sources().size(), 3u);
EXPECT_EQ(snippet.id(), kSnippetUrl);
EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com"));
@@ -907,15 +909,15 @@ TEST_F(NTPSnippetsServiceTest, DismissShouldRespectAllKnownUrls) {
// Add the snippet from the mashable domain.
LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources(
source_urls[0], creation, expiry, source_urls, publishers, amp_urls)}));
- ASSERT_THAT(service()->GetSnippetsForTesting(), SizeIs(1));
+ ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), SizeIs(1));
// Dismiss the snippet via the mashable source corpus ID.
service()->DismissSuggestion(MakeUniqueID(source_urls[0]));
- EXPECT_THAT(service()->GetSnippetsForTesting(), IsEmpty());
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty());
// The same article from the AOL domain should now be detected as dismissed.
LoadFromJSONString(GetTestJson({GetSnippetWithUrlAndTimesAndSources(
source_urls[1], creation, expiry, source_urls, publishers, amp_urls)}));
- ASSERT_THAT(service()->GetSnippetsForTesting(), IsEmpty());
+ ASSERT_THAT(service()->GetSnippetsForTesting(articles_category()), IsEmpty());
}
TEST_F(NTPSnippetsServiceTest, StatusChanges) {
@@ -926,7 +928,7 @@ TEST_F(NTPSnippetsServiceTest, StatusChanges) {
service()->OnDisabledReasonChanged(DisabledReason::SIGNED_OUT);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(NTPSnippetsService::State::DISABLED, service()->state_);
- EXPECT_THAT(service()->GetSnippetsForTesting(),
+ EXPECT_THAT(service()->GetSnippetsForTesting(articles_category()),
IsEmpty()); // No fetch should be made.
// Simulate user sign in. The service should be ready again and load snippets.
@@ -939,7 +941,7 @@ TEST_F(NTPSnippetsServiceTest, StatusChanges) {
OnCategoryStatusChanged(_, _, CategoryStatus::AVAILABLE));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(NTPSnippetsService::State::READY, service()->state_);
- EXPECT_FALSE(service()->GetSnippetsForTesting().empty());
+ EXPECT_FALSE(service()->GetSnippetsForTesting(articles_category()).empty());
}
TEST_F(NTPSnippetsServiceTest, ImageReturnedWithTheSameId) {

Powered by Google App Engine
This is Rietveld 408576698