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

Unified Diff: components/ntp_snippets/ntp_snippets_service_unittest.cc

Issue 1889783002: [NTP Snippets] Add tests for invalid or incomplete json (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@snippets_tests_cleanup
Patch Set: Created 4 years, 8 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
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 dd58c1b6a2717b1839201336e59a0d1d856858ce..b4700c5d11bf84309e49c370d8c5fc251a969826 100644
--- a/components/ntp_snippets/ntp_snippets_service_unittest.cc
+++ b/components/ntp_snippets/ntp_snippets_service_unittest.cc
@@ -62,13 +62,34 @@ std::string GetTestExpiredJson() {
NTPSnippet::TimeToJsonString(base::Time::Now()));
}
+std::string GetInvalidJson() {
+ std::string json_str = GetTestJson();
+ // Make the json invalid by removing the final closing brace.
+ return json_str.substr(0, json_str.size() - 1);
+}
+
+std::string GetIncompleteJson() {
+ std::string json_str = GetTestJson();
+ // Rename the "url" entry. The result is syntactically valid json that will
+ // fail to parse as snippets.
+ size_t pos = json_str.find("\"url\"");
+ if (pos == std::string::npos) {
+ NOTREACHED();
+ return std::string();
+ }
+ json_str[pos + 1] = 'x';
+ return json_str;
+}
+
void ParseJson(
+ bool expect_success,
const std::string& json,
const ntp_snippets::NTPSnippetsService::SuccessCallback& success_callback,
const ntp_snippets::NTPSnippetsService::ErrorCallback& error_callback) {
base::JSONReader json_reader;
scoped_ptr<base::Value> value = json_reader.ReadToValue(json);
- ASSERT_TRUE(value);
+ bool success = !!value;
+ EXPECT_EQ(expect_success, success);
if (value) {
success_callback.Run(std::move(value));
} else {
@@ -100,7 +121,7 @@ class NTPSnippetsServiceTest : public testing::Test {
pref_service_.get(), nullptr, task_runner, std::string("fr"), nullptr,
make_scoped_ptr(new NTPSnippetsFetcher(
task_runner, std::move(request_context_getter), true)),
- base::Bind(&ParseJson)));
+ base::Bind(&ParseJson, true)));
service_->Init(true);
}
@@ -113,6 +134,10 @@ class NTPSnippetsServiceTest : public testing::Test {
service_->OnSnippetsDownloaded(json);
}
+ void SetExpectJsonParseSuccess(bool expect_success) {
+ service_->parse_json_callback_ = base::Bind(&ParseJson, expect_success);
+ }
+
private:
base::MessageLoop message_loop_;
scoped_ptr<TestingPrefServiceSimple> pref_service_;
@@ -159,6 +184,36 @@ TEST_F(NTPSnippetsServiceTest, Full) {
}
}
+TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) {
+ SetExpectJsonParseSuccess(false);
+ LoadFromJSONString(GetInvalidJson());
+ EXPECT_EQ(service()->size(), 0u);
+}
+
+TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) {
+ LoadFromJSONString(GetTestJson());
+ ASSERT_EQ(service()->size(), 1u);
+
+ SetExpectJsonParseSuccess(false);
+ LoadFromJSONString(GetInvalidJson());
+ // This should not have changed the existing snippets.
+ EXPECT_EQ(service()->size(), 1u);
+}
+
+TEST_F(NTPSnippetsServiceTest, LoadIncompleteJson) {
+ LoadFromJSONString(GetIncompleteJson());
+ EXPECT_EQ(service()->size(), 0u);
+}
+
+TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) {
+ LoadFromJSONString(GetTestJson());
+ ASSERT_EQ(service()->size(), 1u);
+
+ LoadFromJSONString(GetIncompleteJson());
+ // This should not have changed the existing snippets.
+ EXPECT_EQ(service()->size(), 1u);
+}
+
TEST_F(NTPSnippetsServiceTest, Discard) {
std::string json_str(
"{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}");
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698