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

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: Unit tests 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
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 "base/json/json_reader.h" 5 #include "base/json/json_reader.h"
6 #include "base/macros.h" 6 #include "base/macros.h"
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/string_number_conversions.h" 8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "base/thread_task_runner_handle.h" 10 #include "base/thread_task_runner_handle.h"
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
160 EXPECT_EQ(snippet.title(), "Title"); 160 EXPECT_EQ(snippet.title(), "Title");
161 EXPECT_EQ(snippet.snippet(), "Snippet"); 161 EXPECT_EQ(snippet.snippet(), "Snippet");
162 EXPECT_EQ(snippet.salient_image_url(), 162 EXPECT_EQ(snippet.salient_image_url(),
163 GURL("http://localhost/salient_image")); 163 GURL("http://localhost/salient_image"));
164 base::Time then = 164 base::Time then =
165 base::Time::FromUTCExploded({2015, 11, 4, 25, 13, 46, 45}); 165 base::Time::FromUTCExploded({2015, 11, 4, 25, 13, 46, 45});
166 EXPECT_EQ(then, snippet.publish_date()); 166 EXPECT_EQ(then, snippet.publish_date());
167 } 167 }
168 } 168 }
169 169
170 TEST_F(NTPSnippetsServiceTest, Clear) {
171 std::string json_str(GetTestJson(1448459205));
172
173 ASSERT_TRUE(LoadFromJSONString(json_str));
Marc Treib 2016/04/13 08:45:52 Heads-up: I changed the setup of these tests sligh
jkrcal 2016/04/14 15:27:01 Done.
174 EXPECT_EQ(service()->size(), 1u);
175
176 service()->ClearSnippets();
177 EXPECT_EQ(service()->size(), 0u);
178 }
179
170 TEST_F(NTPSnippetsServiceTest, Discard) { 180 TEST_F(NTPSnippetsServiceTest, Discard) {
171 std::string json_str( 181 std::string json_str(
172 "{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}"); 182 "{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}");
173 ASSERT_TRUE(LoadFromJSONString(json_str)); 183 ASSERT_TRUE(LoadFromJSONString(json_str));
174 184
175 ASSERT_EQ(1u, service()->size()); 185 ASSERT_EQ(1u, service()->size());
176 186
177 // Discarding a non-existent snippet shouldn't do anything. 187 // Discarding a non-existent snippet shouldn't do anything.
178 EXPECT_FALSE(service()->DiscardSnippet(GURL("http://othersite.com"))); 188 EXPECT_FALSE(service()->DiscardSnippet(GURL("http://othersite.com")));
179 EXPECT_EQ(1u, service()->size()); 189 EXPECT_EQ(1u, service()->size());
180 190
181 // Discard the snippet. 191 // Discard the snippet.
182 EXPECT_TRUE(service()->DiscardSnippet(GURL("http://site.com"))); 192 EXPECT_TRUE(service()->DiscardSnippet(GURL("http://site.com")));
183 EXPECT_EQ(0u, service()->size()); 193 EXPECT_EQ(0u, service()->size());
184 194
185 // Make sure that fetching the same snippet again does not re-add it. 195 // Make sure that fetching the same snippet again does not re-add it.
186 ASSERT_TRUE(LoadFromJSONString(json_str)); 196 ASSERT_TRUE(LoadFromJSONString(json_str));
187 EXPECT_EQ(0u, service()->size()); 197 EXPECT_EQ(0u, service()->size());
188 198
189 // The snippet should stay discarded even after re-creating the service. 199 // The snippet should stay discarded even after re-creating the service.
190 CreateSnippetsService(); 200 CreateSnippetsService();
191 // Init the service, so the prefs get loaded. 201 // Init the service, so the prefs get loaded.
192 // TODO(treib): This should happen in CreateSnippetsService. 202 // TODO(treib): This should happen in CreateSnippetsService.
193 service()->Init(true); 203 service()->Init(true);
194 ASSERT_TRUE(LoadFromJSONString(json_str)); 204 ASSERT_TRUE(LoadFromJSONString(json_str));
195 EXPECT_EQ(0u, service()->size()); 205 EXPECT_EQ(0u, service()->size());
206
207 // The snippet can be added again after clearing discarded snippets.
208 service()->ClearDiscardedSnippets();
209 EXPECT_EQ(0u, service()->size());
210 ASSERT_TRUE(LoadFromJSONString(json_str));
211 EXPECT_EQ(1u, service()->size());
212 }
213
214 TEST_F(NTPSnippetsServiceTest, GetDiscarded) {
215 std::string json_str(
216 "{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}");
217 ASSERT_TRUE(LoadFromJSONString(json_str));
218
219 // For the test, we need the snippet to get discarded.
220 ASSERT_TRUE(service()->DiscardSnippet(GURL("http://site.com")));
221 const NTPSnippetsService::NTPSnippetStorage& snippets =
222 service()->GetDiscardedSnippets();
223 EXPECT_EQ(1u, snippets.size());
224 for (auto& snippet : snippets) {
225 EXPECT_EQ(GURL("http://site.com"), snippet->url());
226 }
227
228 // There should be no discarded snippet after clearing the list.
229 service()->ClearDiscardedSnippets();
230 EXPECT_EQ(0u, snippets.size());
Marc Treib 2016/04/13 08:45:52 Ah, this works because you have a reference to the
jkrcal 2016/04/14 15:27:01 Done.
196 } 231 }
197 232
198 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { 233 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) {
199 std::string json_str(GetTestJson("aaa1448459205")); 234 std::string json_str(GetTestJson("aaa1448459205"));
200 235
201 ASSERT_TRUE(LoadFromJSONString(json_str)); 236 ASSERT_TRUE(LoadFromJSONString(json_str));
202 EXPECT_EQ(service()->size(), 1u); 237 EXPECT_EQ(service()->size(), 1u);
203 238
204 // The same for loop without the '&' should not compile. 239 // The same for loop without the '&' should not compile.
205 for (auto& snippet : *service()) { 240 for (auto& snippet : *service()) {
206 // Snippet here is a const. 241 // Snippet here is a const.
207 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); 242 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar"));
208 EXPECT_EQ(snippet.title(), "Title"); 243 EXPECT_EQ(snippet.title(), "Title");
209 EXPECT_EQ(snippet.snippet(), "Snippet"); 244 EXPECT_EQ(snippet.snippet(), "Snippet");
210 EXPECT_EQ(base::Time::UnixEpoch(), snippet.publish_date()); 245 EXPECT_EQ(base::Time::UnixEpoch(), snippet.publish_date());
211 } 246 }
212 } 247 }
213 248
214 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { 249 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) {
215 std::string json_str(GetTestExpiredJson(1448459205)); 250 std::string json_str(GetTestExpiredJson(1448459205));
216 251
217 ASSERT_TRUE(LoadFromJSONString(json_str)); 252 ASSERT_TRUE(LoadFromJSONString(json_str));
218 EXPECT_EQ(service()->size(), 0u); 253 EXPECT_EQ(service()->size(), 0u);
219 } 254 }
220 255
221 } // namespace ntp_snippets 256 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698