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

Side by Side Diff: components/ntp_snippets/ntp_snippets_service_unittest.cc

Issue 1883523002: chrome://snippets-internals page for debugging NTP snippets. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Implementing last suggestions from Bernhard 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.cc ('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 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 EXPECT_EQ(snippet.site_title(), "Site Title"); 177 EXPECT_EQ(snippet.site_title(), "Site Title");
178 EXPECT_EQ(snippet.favicon_url(), GURL("http://localhost/favicon")); 178 EXPECT_EQ(snippet.favicon_url(), GURL("http://localhost/favicon"));
179 EXPECT_EQ(snippet.title(), "Title"); 179 EXPECT_EQ(snippet.title(), "Title");
180 EXPECT_EQ(snippet.snippet(), "Snippet"); 180 EXPECT_EQ(snippet.snippet(), "Snippet");
181 EXPECT_EQ(snippet.salient_image_url(), 181 EXPECT_EQ(snippet.salient_image_url(),
182 GURL("http://localhost/salient_image")); 182 GURL("http://localhost/salient_image"));
183 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date()); 183 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date());
184 } 184 }
185 } 185 }
186 186
187 TEST_F(NTPSnippetsServiceTest, Clear) {
188 std::string json_str(GetTestJson());
189
190 LoadFromJSONString(json_str);
191 EXPECT_EQ(service()->size(), 1u);
192
193 service()->ClearSnippets();
194 EXPECT_EQ(service()->size(), 0u);
195 }
196
187 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { 197 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) {
188 SetExpectJsonParseSuccess(false); 198 SetExpectJsonParseSuccess(false);
189 LoadFromJSONString(GetInvalidJson()); 199 LoadFromJSONString(GetInvalidJson());
190 EXPECT_EQ(service()->size(), 0u); 200 EXPECT_EQ(service()->size(), 0u);
191 } 201 }
192 202
193 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) { 203 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) {
194 LoadFromJSONString(GetTestJson()); 204 LoadFromJSONString(GetTestJson());
195 ASSERT_EQ(service()->size(), 1u); 205 ASSERT_EQ(service()->size(), 1u);
196 206
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 EXPECT_EQ(0u, service()->size()); 240 EXPECT_EQ(0u, service()->size());
231 241
232 // Make sure that fetching the same snippet again does not re-add it. 242 // Make sure that fetching the same snippet again does not re-add it.
233 LoadFromJSONString(json_str); 243 LoadFromJSONString(json_str);
234 EXPECT_EQ(0u, service()->size()); 244 EXPECT_EQ(0u, service()->size());
235 245
236 // The snippet should stay discarded even after re-creating the service. 246 // The snippet should stay discarded even after re-creating the service.
237 CreateSnippetsService(); 247 CreateSnippetsService();
238 LoadFromJSONString(json_str); 248 LoadFromJSONString(json_str);
239 EXPECT_EQ(0u, service()->size()); 249 EXPECT_EQ(0u, service()->size());
250
251 // The snippet can be added again after clearing discarded snippets.
252 service()->ClearDiscardedSnippets();
253 EXPECT_EQ(0u, service()->size());
254 LoadFromJSONString(json_str);
255 EXPECT_EQ(1u, service()->size());
256 }
257
258 TEST_F(NTPSnippetsServiceTest, GetDiscarded) {
259 std::string json_str(
260 "{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}");
261 LoadFromJSONString(json_str);
262
263 // For the test, we need the snippet to get discarded.
264 ASSERT_TRUE(service()->DiscardSnippet(GURL("http://site.com")));
265 const NTPSnippetsService::NTPSnippetStorage& snippets =
266 service()->discarded_snippets();
267 EXPECT_EQ(1u, snippets.size());
268 for (auto& snippet : snippets) {
269 EXPECT_EQ(GURL("http://site.com"), snippet->url());
270 }
271
272 // There should be no discarded snippet after clearing the list.
273 service()->ClearDiscardedSnippets();
274 EXPECT_EQ(0u, service()->discarded_snippets().size());
240 } 275 }
241 276
242 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { 277 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) {
243 std::string json_str(GetTestJson("aaa1448459205")); 278 std::string json_str(GetTestJson("aaa1448459205"));
244 279
245 LoadFromJSONString(json_str); 280 LoadFromJSONString(json_str);
246 EXPECT_EQ(service()->size(), 1u); 281 EXPECT_EQ(service()->size(), 1u);
247 282
248 // The same for loop without the '&' should not compile. 283 // The same for loop without the '&' should not compile.
249 for (auto& snippet : *service()) { 284 for (auto& snippet : *service()) {
250 // Snippet here is a const. 285 // Snippet here is a const.
251 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); 286 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar"));
252 EXPECT_EQ(snippet.title(), "Title"); 287 EXPECT_EQ(snippet.title(), "Title");
253 EXPECT_EQ(snippet.snippet(), "Snippet"); 288 EXPECT_EQ(snippet.snippet(), "Snippet");
254 EXPECT_EQ(base::Time::UnixEpoch(), snippet.publish_date()); 289 EXPECT_EQ(base::Time::UnixEpoch(), snippet.publish_date());
255 } 290 }
256 } 291 }
257 292
258 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { 293 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) {
259 std::string json_str(GetTestExpiredJson()); 294 std::string json_str(GetTestExpiredJson());
260 295
261 LoadFromJSONString(json_str); 296 LoadFromJSONString(json_str);
262 EXPECT_EQ(service()->size(), 0u); 297 EXPECT_EQ(service()->size(), 0u);
263 } 298 }
264 299
265 } // namespace ntp_snippets 300 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698