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

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

Issue 2421463002: FetchMore functionality backend (Closed)
Patch Set: ID set reference, Optional callback, ... (2466863003 comments). 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..5073f07493ddb85a2eb286e55ac818be9ab3f2e0 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,23 @@ class NTPSnippetsServiceTest : public ::testing::Test {
base::RunLoop().RunUntilIdle();
}
+ void LoadMoreFromJSONString(NTPSnippetsService* service,
+ const std::string& json,
+ const std::set<std::string>& known_ids,
+ NTPSnippetsService::FetchingCallback callback) {
+ SetUpFetchResponse(json);
+ service->Fetch(articles_category(), known_ids, callback);
+ base::RunLoop().RunUntilIdle();
+ }
+
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 +537,7 @@ class NTPSnippetsServiceTest : public ::testing::Test {
FakeImageDecoder* image_decoder_;
base::ScopedTempDir database_dir_;
+ MockCaller caller_;
DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest);
};
@@ -832,6 +850,59 @@ 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)}),
+ std::set<std::string>(),
+ base::Bind([](std::vector<ContentSuggestion>) {}));
+ // The snippets loaded last are added to the previously loaded.
+ EXPECT_THAT(service->GetSnippetsForTesting(articles_category()),
+ ElementsAre(IdEq(first), IdEq(second)));
+}
+
+namespace {
+
+// Workaround for gMock's lack of support for movable types.
+void SuggestionsLoaded(
+ MockFunction<void(const std::vector<ContentSuggestion>& v)>* loaded,
+ std::vector<ContentSuggestion> v) {
+ loaded->Call(v);
+}
+
+} // namespace
+
+TEST_F(NTPSnippetsServiceTest, InvokesOnlyCallbackOnFetchingMore) {
+ auto service = MakeSnippetsService();
+
+ MockFunction<void(const std::vector<ContentSuggestion>&)> loaded;
+ EXPECT_CALL(loaded, Call(SizeIs(1)));
+
+ LoadMoreFromJSONString(
+ service.get(), GetTestJson({GetSnippetWithUrl("http://some")}),
+ std::set<std::string>(), base::Bind(&SuggestionsLoaded, &loaded));
+
+ // The observer shouldn't have been triggered.
+ EXPECT_THAT(observer().SuggestionsForCategory(articles_category()),
+ IsEmpty());
+}
+
+TEST_F(NTPSnippetsServiceTest, ReturnFetchRequestEmptyBeforeInit) {
+ auto service = MakeSnippetsServiceWithoutInitialization();
+ MockFunction<void(const std::vector<ContentSuggestion>&)> loaded;
+ EXPECT_CALL(loaded, Call(SizeIs(0)));
+ service->Fetch(articles_category(), std::set<std::string>(),
+ base::Bind(&SuggestionsLoaded, &loaded));
+ base::RunLoop().RunUntilIdle();
+}
+
TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) {
auto service = MakeSnippetsService();

Powered by Google App Engine
This is Rietveld 408576698