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

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: Adding the chrome://snippets-internals page in the auto-suggest list. 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 "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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 EXPECT_EQ(snippet.site_title(), "Site Title"); 152 EXPECT_EQ(snippet.site_title(), "Site Title");
153 EXPECT_EQ(snippet.favicon_url(), GURL("http://localhost/favicon")); 153 EXPECT_EQ(snippet.favicon_url(), GURL("http://localhost/favicon"));
154 EXPECT_EQ(snippet.title(), "Title"); 154 EXPECT_EQ(snippet.title(), "Title");
155 EXPECT_EQ(snippet.snippet(), "Snippet"); 155 EXPECT_EQ(snippet.snippet(), "Snippet");
156 EXPECT_EQ(snippet.salient_image_url(), 156 EXPECT_EQ(snippet.salient_image_url(),
157 GURL("http://localhost/salient_image")); 157 GURL("http://localhost/salient_image"));
158 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date()); 158 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date());
159 } 159 }
160 } 160 }
161 161
162 TEST_F(NTPSnippetsServiceTest, Clear) {
163 std::string json_str(GetTestJson());
164
165 LoadFromJSONString(json_str);
166 EXPECT_EQ(service()->size(), 1u);
167
168 service()->ClearSnippets();
169 EXPECT_EQ(service()->size(), 0u);
170 }
171
162 TEST_F(NTPSnippetsServiceTest, Discard) { 172 TEST_F(NTPSnippetsServiceTest, Discard) {
163 std::string json_str( 173 std::string json_str(
164 "{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}"); 174 "{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}");
165 LoadFromJSONString(json_str); 175 LoadFromJSONString(json_str);
166 176
167 ASSERT_EQ(1u, service()->size()); 177 ASSERT_EQ(1u, service()->size());
168 178
169 // Discarding a non-existent snippet shouldn't do anything. 179 // Discarding a non-existent snippet shouldn't do anything.
170 EXPECT_FALSE(service()->DiscardSnippet(GURL("http://othersite.com"))); 180 EXPECT_FALSE(service()->DiscardSnippet(GURL("http://othersite.com")));
171 EXPECT_EQ(1u, service()->size()); 181 EXPECT_EQ(1u, service()->size());
172 182
173 // Discard the snippet. 183 // Discard the snippet.
174 EXPECT_TRUE(service()->DiscardSnippet(GURL("http://site.com"))); 184 EXPECT_TRUE(service()->DiscardSnippet(GURL("http://site.com")));
175 EXPECT_EQ(0u, service()->size()); 185 EXPECT_EQ(0u, service()->size());
176 186
177 // Make sure that fetching the same snippet again does not re-add it. 187 // Make sure that fetching the same snippet again does not re-add it.
178 LoadFromJSONString(json_str); 188 LoadFromJSONString(json_str);
179 EXPECT_EQ(0u, service()->size()); 189 EXPECT_EQ(0u, service()->size());
180 190
181 // The snippet should stay discarded even after re-creating the service. 191 // The snippet should stay discarded even after re-creating the service.
182 CreateSnippetsService(); 192 CreateSnippetsService();
183 LoadFromJSONString(json_str); 193 LoadFromJSONString(json_str);
184 EXPECT_EQ(0u, service()->size()); 194 EXPECT_EQ(0u, service()->size());
195
196 // The snippet can be added again after clearing discarded snippets.
197 service()->ClearDiscardedSnippets();
198 EXPECT_EQ(0u, service()->size());
199 ASSERT_TRUE(LoadFromJSONString(json_str));
200 EXPECT_EQ(1u, service()->size());
201 }
202
203 TEST_F(NTPSnippetsServiceTest, GetDiscarded) {
204 std::string json_str(
205 "{ \"recos\": [ { \"contentInfo\": { \"url\" : \"http://site.com\" }}]}");
206 LoadFromJSONString(json_str);
207
208 // For the test, we need the snippet to get discarded.
209 ASSERT_TRUE(service()->DiscardSnippet(GURL("http://site.com")));
210 const NTPSnippetsService::NTPSnippetStorage& snippets =
211 service()->GetDiscardedSnippets();
212 EXPECT_EQ(1u, snippets.size());
213 for (auto& snippet : snippets) {
214 EXPECT_EQ(GURL("http://site.com"), snippet->url());
215 }
216
217 // There should be no discarded snippet after clearing the list.
218 service()->ClearDiscardedSnippets();
219 EXPECT_EQ(0u, service()->GetDiscardedSnippets().size());
185 } 220 }
186 221
187 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { 222 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) {
188 std::string json_str(GetTestJson("aaa1448459205")); 223 std::string json_str(GetTestJson("aaa1448459205"));
189 224
190 LoadFromJSONString(json_str); 225 LoadFromJSONString(json_str);
191 EXPECT_EQ(service()->size(), 1u); 226 EXPECT_EQ(service()->size(), 1u);
192 227
193 // The same for loop without the '&' should not compile. 228 // The same for loop without the '&' should not compile.
194 for (auto& snippet : *service()) { 229 for (auto& snippet : *service()) {
195 // Snippet here is a const. 230 // Snippet here is a const.
196 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); 231 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar"));
197 EXPECT_EQ(snippet.title(), "Title"); 232 EXPECT_EQ(snippet.title(), "Title");
198 EXPECT_EQ(snippet.snippet(), "Snippet"); 233 EXPECT_EQ(snippet.snippet(), "Snippet");
199 EXPECT_EQ(base::Time::UnixEpoch(), snippet.publish_date()); 234 EXPECT_EQ(base::Time::UnixEpoch(), snippet.publish_date());
200 } 235 }
201 } 236 }
202 237
203 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { 238 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) {
204 std::string json_str(GetTestExpiredJson()); 239 std::string json_str(GetTestExpiredJson());
205 240
206 LoadFromJSONString(json_str); 241 LoadFromJSONString(json_str);
207 EXPECT_EQ(service()->size(), 0u); 242 EXPECT_EQ(service()->size(), 0u);
208 } 243 }
209 244
210 } // namespace ntp_snippets 245 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698