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

Unified Diff: components/ntp_snippets/remote/ntp_snippets_service_unittest.cc

Issue 2421463002: FetchMore functionality backend (Closed)
Patch Set: NTBR. Rebasing a lot. Created 4 years, 1 month 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/remote/ntp_snippets_service_unittest.cc
diff --git a/components/ntp_snippets/remote/ntp_snippets_service_unittest.cc b/components/ntp_snippets/remote/ntp_snippets_service_unittest.cc
index e79d79ca5cb4aa48b17018157061004d7ca987ea..05dd1ca347c2abeceb1b03e72babdef2bd70ef24 100644
--- a/components/ntp_snippets/remote/ntp_snippets_service_unittest.cc
+++ b/components/ntp_snippets/remote/ntp_snippets_service_unittest.cc
@@ -388,7 +388,8 @@ class NTPSnippetsServiceTest : public ::testing::Test {
test_url_(kTestContentSuggestionsServerWithAPIKey),
user_classifier_(/*pref_service=*/nullptr),
image_fetcher_(nullptr),
- image_decoder_(nullptr) {
+ image_decoder_(nullptr),
+ caller_() {
NTPSnippetsService::RegisterProfilePrefs(utils_.pref_service()->registry());
RequestThrottler::RegisterProfilePrefs(utils_.pref_service()->registry());
@@ -503,7 +504,34 @@ class NTPSnippetsServiceTest : public ::testing::Test {
base::RunLoop().RunUntilIdle();
}
+ void LoadMoreFromJSONString(
+ NTPSnippetsService* service,
+ const std::string& json,
+ const NTPSnippetsService::FetchedMoreCallback& callback) {
+ SetUpFetchResponse(json);
+ service->FetchMore(articles_category(), callback);
+ base::RunLoop().RunUntilIdle();
+ }
+
+ NTPSnippetsService::FetchedMoreCallback MakeDummyCallback() {
+ return base::Bind(&MockCaller::EmptyCallback,
+ base::Unretained(&this->caller_));
+ }
+
+ NTPSnippetsService::FetchedMoreCallback MakeMustCallCallback() {
+ EXPECT_CALL(this->caller_, MustCallback()).Times(1);
+ return base::Bind(&MockCaller::CheckCallback,
+ base::Unretained(&this->caller_));
+ }
+
private:
+ class MockCaller {
+ public:
+ void EmptyCallback(std::vector<ContentSuggestion>) {}
+ void CheckCallback(std::vector<ContentSuggestion> v) { MustCallback(); }
+ MOCK_METHOD0(MustCallback, void());
+ };
+
variations::testing::VariationParamsManager params_manager_;
test::NTPSnippetsTestUtils utils_;
base::MessageLoop message_loop_;
@@ -520,6 +548,7 @@ class NTPSnippetsServiceTest : public ::testing::Test {
FakeImageDecoder* image_decoder_;
base::ScopedTempDir database_dir_;
+ MockCaller caller_;
DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest);
};
@@ -832,6 +861,33 @@ TEST_F(NTPSnippetsServiceTest, ReplaceSnippets) {
ElementsAre(IdEq(second)));
}
+TEST_F(NTPSnippetsServiceTest, LoadsAdditionalSnippets) {
+ auto service = MakeSnippetsService();
+
+ std::string first("http://first");
+ LoadFromJSONString(service.get(), GetTestJson({GetSnippetWithUrl(first)}));
+ EXPECT_THAT(service->GetSnippetsForTesting(articles_category()),
+ ElementsAre(IdEq(first)));
+
+ std::string second("http://second");
+ LoadMoreFromJSONString(service.get(),
+ GetTestJson({GetSnippetWithUrl(second)}),
+ MakeDummyCallback());
+ // The snippets loaded last are added to the previously loaded.
+ EXPECT_THAT(service->GetSnippetsForTesting(articles_category()),
+ ElementsAre(IdEq(first), IdEq(second)));
+}
+
+TEST_F(NTPSnippetsServiceTest, InvokesOnlyCallbackOnFetchingMore) {
+ LoadMoreFromJSONString(MakeSnippetsService().get(),
+ GetTestJson({GetSnippetWithUrl("http://some")}),
+ MakeMustCallCallback());
+
+ // The observer shouldn't have been triggered.
+ EXPECT_THAT(observer().SuggestionsForCategory(articles_category()),
+ SizeIs(0));
+}
+
TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) {
auto service = MakeSnippetsService();

Powered by Google App Engine
This is Rietveld 408576698