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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/ntp_snippets/ntp_snippets_service.h" 5 #include "components/ntp_snippets/ntp_snippets_service.h"
6 6
7 #include "base/json/json_reader.h" 7 #include "base/json/json_reader.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 55
56 std::string GetTestJson() { 56 std::string GetTestJson() {
57 return GetTestJson(NTPSnippet::TimeToJsonString(GetDefaultCreationTime())); 57 return GetTestJson(NTPSnippet::TimeToJsonString(GetDefaultCreationTime()));
58 } 58 }
59 59
60 std::string GetTestExpiredJson() { 60 std::string GetTestExpiredJson() {
61 return GetTestJson(NTPSnippet::TimeToJsonString(GetDefaultCreationTime()), 61 return GetTestJson(NTPSnippet::TimeToJsonString(GetDefaultCreationTime()),
62 NTPSnippet::TimeToJsonString(base::Time::Now())); 62 NTPSnippet::TimeToJsonString(base::Time::Now()));
63 } 63 }
64 64
65 std::string GetInvalidJson() {
66 std::string json_str = GetTestJson();
67 // Make the json invalid by removing the final closing brace.
68 return json_str.substr(0, json_str.size() - 1);
69 }
70
71 std::string GetIncompleteJson() {
72 std::string json_str = GetTestJson();
73 // Rename the "url" entry. The result is syntactically valid json that will
74 // fail to parse as snippets.
75 size_t pos = json_str.find("\"url\"");
76 if (pos == std::string::npos) {
77 NOTREACHED();
78 return std::string();
79 }
80 json_str[pos + 1] = 'x';
81 return json_str;
82 }
83
65 void ParseJson( 84 void ParseJson(
85 bool expect_success,
66 const std::string& json, 86 const std::string& json,
67 const ntp_snippets::NTPSnippetsService::SuccessCallback& success_callback, 87 const ntp_snippets::NTPSnippetsService::SuccessCallback& success_callback,
68 const ntp_snippets::NTPSnippetsService::ErrorCallback& error_callback) { 88 const ntp_snippets::NTPSnippetsService::ErrorCallback& error_callback) {
69 base::JSONReader json_reader; 89 base::JSONReader json_reader;
70 scoped_ptr<base::Value> value = json_reader.ReadToValue(json); 90 scoped_ptr<base::Value> value = json_reader.ReadToValue(json);
71 ASSERT_TRUE(value); 91 bool success = !!value;
92 EXPECT_EQ(expect_success, success);
72 if (value) { 93 if (value) {
73 success_callback.Run(std::move(value)); 94 success_callback.Run(std::move(value));
74 } else { 95 } else {
75 error_callback.Run(json_reader.GetErrorMessage()); 96 error_callback.Run(json_reader.GetErrorMessage());
76 } 97 }
77 } 98 }
78 99
79 } // namespace 100 } // namespace
80 101
81 class NTPSnippetsServiceTest : public testing::Test { 102 class NTPSnippetsServiceTest : public testing::Test {
(...skipping 11 matching lines...) Expand all
93 void CreateSnippetsService() { 114 void CreateSnippetsService() {
94 scoped_refptr<base::SingleThreadTaskRunner> task_runner( 115 scoped_refptr<base::SingleThreadTaskRunner> task_runner(
95 base::ThreadTaskRunnerHandle::Get()); 116 base::ThreadTaskRunnerHandle::Get());
96 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter = 117 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter =
97 new net::TestURLRequestContextGetter(task_runner.get()); 118 new net::TestURLRequestContextGetter(task_runner.get());
98 119
99 service_.reset(new NTPSnippetsService( 120 service_.reset(new NTPSnippetsService(
100 pref_service_.get(), nullptr, task_runner, std::string("fr"), nullptr, 121 pref_service_.get(), nullptr, task_runner, std::string("fr"), nullptr,
101 make_scoped_ptr(new NTPSnippetsFetcher( 122 make_scoped_ptr(new NTPSnippetsFetcher(
102 task_runner, std::move(request_context_getter), true)), 123 task_runner, std::move(request_context_getter), true)),
103 base::Bind(&ParseJson))); 124 base::Bind(&ParseJson, true)));
104 service_->Init(true); 125 service_->Init(true);
105 } 126 }
106 127
107 protected: 128 protected:
108 NTPSnippetsService* service() { 129 NTPSnippetsService* service() {
109 return service_.get(); 130 return service_.get();
110 } 131 }
111 132
112 void LoadFromJSONString(const std::string& json) { 133 void LoadFromJSONString(const std::string& json) {
113 service_->OnSnippetsDownloaded(json); 134 service_->OnSnippetsDownloaded(json);
114 } 135 }
115 136
137 void SetExpectJsonParseSuccess(bool expect_success) {
138 service_->parse_json_callback_ = base::Bind(&ParseJson, expect_success);
139 }
140
116 private: 141 private:
117 base::MessageLoop message_loop_; 142 base::MessageLoop message_loop_;
118 scoped_ptr<TestingPrefServiceSimple> pref_service_; 143 scoped_ptr<TestingPrefServiceSimple> pref_service_;
119 scoped_ptr<NTPSnippetsService> service_; 144 scoped_ptr<NTPSnippetsService> service_;
120 145
121 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest); 146 DISALLOW_COPY_AND_ASSIGN(NTPSnippetsServiceTest);
122 }; 147 };
123 148
124 TEST_F(NTPSnippetsServiceTest, Loop) { 149 TEST_F(NTPSnippetsServiceTest, Loop) {
125 std::string json_str( 150 std::string json_str(
(...skipping 26 matching lines...) Expand all
152 EXPECT_EQ(snippet.site_title(), "Site Title"); 177 EXPECT_EQ(snippet.site_title(), "Site Title");
153 EXPECT_EQ(snippet.favicon_url(), GURL("http://localhost/favicon")); 178 EXPECT_EQ(snippet.favicon_url(), GURL("http://localhost/favicon"));
154 EXPECT_EQ(snippet.title(), "Title"); 179 EXPECT_EQ(snippet.title(), "Title");
155 EXPECT_EQ(snippet.snippet(), "Snippet"); 180 EXPECT_EQ(snippet.snippet(), "Snippet");
156 EXPECT_EQ(snippet.salient_image_url(), 181 EXPECT_EQ(snippet.salient_image_url(),
157 GURL("http://localhost/salient_image")); 182 GURL("http://localhost/salient_image"));
158 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date()); 183 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date());
159 } 184 }
160 } 185 }
161 186
187 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) {
188 SetExpectJsonParseSuccess(false);
189 LoadFromJSONString(GetInvalidJson());
190 EXPECT_EQ(service()->size(), 0u);
191 }
192
193 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) {
194 LoadFromJSONString(GetTestJson());
195 ASSERT_EQ(service()->size(), 1u);
196
197 SetExpectJsonParseSuccess(false);
198 LoadFromJSONString(GetInvalidJson());
199 // This should not have changed the existing snippets.
200 EXPECT_EQ(service()->size(), 1u);
201 }
202
203 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJson) {
204 LoadFromJSONString(GetIncompleteJson());
205 EXPECT_EQ(service()->size(), 0u);
206 }
207
208 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) {
209 LoadFromJSONString(GetTestJson());
210 ASSERT_EQ(service()->size(), 1u);
211
212 LoadFromJSONString(GetIncompleteJson());
213 // This should not have changed the existing snippets.
214 EXPECT_EQ(service()->size(), 1u);
215 }
216
162 TEST_F(NTPSnippetsServiceTest, Discard) { 217 TEST_F(NTPSnippetsServiceTest, Discard) {
163 std::string json_str( 218 std::string json_str(
164 "{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}"); 219 "{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}");
165 LoadFromJSONString(json_str); 220 LoadFromJSONString(json_str);
166 221
167 ASSERT_EQ(1u, service()->size()); 222 ASSERT_EQ(1u, service()->size());
168 223
169 // Discarding a non-existent snippet shouldn't do anything. 224 // Discarding a non-existent snippet shouldn't do anything.
170 EXPECT_FALSE(service()->DiscardSnippet(GURL("http://othersite.com"))); 225 EXPECT_FALSE(service()->DiscardSnippet(GURL("http://othersite.com")));
171 EXPECT_EQ(1u, service()->size()); 226 EXPECT_EQ(1u, service()->size());
(...skipping 29 matching lines...) Expand all
201 } 256 }
202 257
203 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { 258 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) {
204 std::string json_str(GetTestExpiredJson()); 259 std::string json_str(GetTestExpiredJson());
205 260
206 LoadFromJSONString(json_str); 261 LoadFromJSONString(json_str);
207 EXPECT_EQ(service()->size(), 0u); 262 EXPECT_EQ(service()->size(), 0u);
208 } 263 }
209 264
210 } // namespace ntp_snippets 265 } // namespace ntp_snippets
OLDNEW
« 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