Commit 90f20b32 authored by sfiera's avatar sfiera Committed by Commit bot

Allow category["suggestions"] to be absent.

APIs do not return empty lists, apparently. If a repeated field is
empty, then it will be absent in the JSON. Allow this, but make sure
that the empty category is created first.

Review-Url: https://codereview.chromium.org/2279483002
Cr-Commit-Position: refs/heads/master@{#414402}
parent 0b70c48a
...@@ -537,15 +537,19 @@ bool NTPSnippetsFetcher::JsonToSnippets(const base::Value& parsed, ...@@ -537,15 +537,19 @@ bool NTPSnippetsFetcher::JsonToSnippets(const base::Value& parsed,
for (const auto& v : *categories) { for (const auto& v : *categories) {
int category_id = -1; int category_id = -1;
const base::DictionaryValue* category_value = nullptr; const base::DictionaryValue* category_value = nullptr;
const base::ListValue* suggestions = nullptr;
if (!(v->GetAsDictionary(&category_value) && if (!(v->GetAsDictionary(&category_value) &&
category_value->GetInteger("id", &category_id) && category_value->GetInteger("id", &category_id) &&
(category_id > 0) && (category_id > 0))) {
category_value->GetList("suggestions", &suggestions))) {
return false; return false;
} }
Category category = category_factory_->FromRemoteCategory(category_id); Category category = category_factory_->FromRemoteCategory(category_id);
const base::ListValue* suggestions = nullptr;
NTPSnippet::PtrVector* articles = &(*snippets)[category]; NTPSnippet::PtrVector* articles = &(*snippets)[category];
if (!category_value->GetList("suggestions", &suggestions)) {
// Absence of a list of suggestions is treated as an empty list, which
// is permissible.
continue;
}
if (!AddSnippetsFromListValue( if (!AddSnippetsFromListValue(
/* content_suggestions_api = */ true, *suggestions, articles)) { /* content_suggestions_api = */ true, *suggestions, articles)) {
return false; return false;
......
...@@ -426,6 +426,29 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ShouldFetchSuccessfully) { ...@@ -426,6 +426,29 @@ TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ShouldFetchSuccessfully) {
/*count=*/1))); /*count=*/1)));
} }
TEST_F(NTPSnippetsContentSuggestionsFetcherTest, EmptyCategoryIsOK) {
const std::string kJsonStr =
"{\"categories\" : [{"
" \"id\": 1,"
" \"localizedTitle\": \"Articles for You\""
"}]}";
SetFakeResponse(/*data=*/kJsonStr, net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
EXPECT_CALL(mock_callback(), Run(IsEmptyArticleList()));
snippets_fetcher().FetchSnippetsFromHosts(test_hosts(), test_lang(),
/*count=*/1,
/*interactive_request=*/true);
FastForwardUntilNoTasksRemain();
EXPECT_THAT(snippets_fetcher().last_status(), Eq("OK"));
EXPECT_THAT(snippets_fetcher().last_json(), Eq(kJsonStr));
EXPECT_THAT(histogram_tester().GetAllSamples(
"NewTabPage.Snippets.FetchHttpResponseOrErrorCode"),
ElementsAre(base::Bucket(/*min=*/200, /*count=*/1)));
EXPECT_THAT(histogram_tester().GetAllSamples("NewTabPage.Snippets.FetchTime"),
ElementsAre(base::Bucket(/*min=*/kTestJsonParsingLatencyMs,
/*count=*/1)));
}
TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ServerCategories) { TEST_F(NTPSnippetsContentSuggestionsFetcherTest, ServerCategories) {
const std::string kJsonStr = const std::string kJsonStr =
"{\"categories\" : [{" "{\"categories\" : [{"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment