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

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

Issue 1958163002: [NTP Snippets] Refactor home-grown container API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Avoided auto as suggested. Created 4 years, 7 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 <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 13 matching lines...) Expand all
24 #include "components/ntp_snippets/ntp_snippets_scheduler.h" 24 #include "components/ntp_snippets/ntp_snippets_scheduler.h"
25 #include "components/ntp_snippets/switches.h" 25 #include "components/ntp_snippets/switches.h"
26 #include "components/prefs/testing_pref_service.h" 26 #include "components/prefs/testing_pref_service.h"
27 #include "google_apis/google_api_keys.h" 27 #include "google_apis/google_api_keys.h"
28 #include "net/url_request/test_url_fetcher_factory.h" 28 #include "net/url_request/test_url_fetcher_factory.h"
29 #include "net/url_request/url_request_test_util.h" 29 #include "net/url_request/url_request_test_util.h"
30 #include "testing/gmock/include/gmock/gmock.h" 30 #include "testing/gmock/include/gmock/gmock.h"
31 #include "testing/gtest/include/gtest/gtest.h" 31 #include "testing/gtest/include/gtest/gtest.h"
32 32
33 using testing::ElementsAre; 33 using testing::ElementsAre;
34 using testing::Eq;
34 using testing::IsEmpty; 35 using testing::IsEmpty;
36 using testing::SizeIs;
35 using testing::StartsWith; 37 using testing::StartsWith;
36 using testing::_; 38 using testing::_;
37 39
38 namespace ntp_snippets { 40 namespace ntp_snippets {
39 41
40 namespace { 42 namespace {
41 43
44 MATCHER_P(UrlEq, value, "") {
45 return arg->url() == GURL(value);
46 }
47
42 const base::Time::Exploded kDefaultCreationTime = {2015, 11, 4, 25, 13, 46, 45}; 48 const base::Time::Exploded kDefaultCreationTime = {2015, 11, 4, 25, 13, 46, 45};
43 const char kTestContentSnippetsServerFormat[] = 49 const char kTestContentSnippetsServerFormat[] =
44 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s"; 50 "https://chromereader-pa.googleapis.com/v1/fetch?key=%s";
45 51
46 base::Time GetDefaultCreationTime() { 52 base::Time GetDefaultCreationTime() {
47 return base::Time::FromUTCExploded(kDefaultCreationTime); 53 return base::Time::FromUTCExploded(kDefaultCreationTime);
48 } 54 }
49 55
50 std::string GetTestJson(const std::string& content_creation_time_str, 56 std::string GetTestJson(const std::string& content_creation_time_str,
51 const std::string& expiry_time_str) { 57 const std::string& expiry_time_str) {
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 }; 269 };
264 270
265 TEST_F(NTPSnippetsServiceTest, ScheduleIfEnabled) { 271 TEST_F(NTPSnippetsServiceTest, ScheduleIfEnabled) {
266 // SetUp() checks that Schedule is called. 272 // SetUp() checks that Schedule is called.
267 } 273 }
268 274
269 TEST_F(NTPSnippetsServiceDisabledTest, Unschedule) { 275 TEST_F(NTPSnippetsServiceDisabledTest, Unschedule) {
270 // SetUp() checks that Unschedule is called. 276 // SetUp() checks that Unschedule is called.
271 } 277 }
272 278
273 TEST_F(NTPSnippetsServiceTest, Loop) {
274 std::string json_str(
275 "{ \"recos\": [ "
276 " { \"contentInfo\": { \"url\" : \"http://localhost/foobar\" }}"
277 "]}");
278 LoadFromJSONString(json_str);
279
280 // The same for loop without the '&' should not compile.
281 for (auto& snippet : *service()) {
282 // Snippet here is a const.
283 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar"));
284 }
285 // Without the const, this should not compile.
286 for (const NTPSnippet& snippet : *service()) {
287 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar"));
288 }
289 }
290
291 TEST_F(NTPSnippetsServiceTest, Full) { 279 TEST_F(NTPSnippetsServiceTest, Full) {
292 std::string json_str(GetTestJson()); 280 std::string json_str(GetTestJson());
293 281
294 LoadFromJSONString(json_str); 282 LoadFromJSONString(json_str);
295 EXPECT_EQ(service()->size(), 1u); 283 ASSERT_THAT(service()->snippets(), SizeIs(1));
296 284 const NTPSnippet& snippet = *service()->snippets().front();
297 // The same for loop without the '&' should not compile. 285 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar"));
298 for (auto& snippet : *service()) { 286 EXPECT_EQ(snippet.best_source().publisher_name, "Foo News");
299 // Snippet here is a const. 287 EXPECT_EQ(snippet.title(), "Title");
300 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); 288 EXPECT_EQ(snippet.snippet(), "Snippet");
301 EXPECT_EQ(snippet.best_source().publisher_name, "Foo News"); 289 EXPECT_EQ(snippet.salient_image_url(),
302 EXPECT_EQ(snippet.title(), "Title"); 290 GURL("http://localhost/salient_image"));
303 EXPECT_EQ(snippet.snippet(), "Snippet"); 291 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date());
304 EXPECT_EQ(snippet.salient_image_url(), 292 EXPECT_EQ(snippet.best_source().amp_url.spec(),
305 GURL("http://localhost/salient_image")); 293 GURL("http://localhost/amp").spec());
306 EXPECT_EQ(GetDefaultCreationTime(), snippet.publish_date());
307 EXPECT_EQ(snippet.best_source().amp_url.spec(),
308 GURL("http://localhost/amp").spec());
309 }
310 } 294 }
311 295
312 TEST_F(NTPSnippetsServiceTest, Clear) { 296 TEST_F(NTPSnippetsServiceTest, Clear) {
313 std::string json_str(GetTestJson()); 297 std::string json_str(GetTestJson());
314 298
315 LoadFromJSONString(json_str); 299 LoadFromJSONString(json_str);
316 EXPECT_EQ(service()->size(), 1u); 300 EXPECT_THAT(service()->snippets(), SizeIs(1));
317 301
318 service()->ClearSnippets(); 302 service()->ClearSnippets();
319 EXPECT_EQ(service()->size(), 0u); 303 EXPECT_THAT(service()->snippets(), IsEmpty());
320 } 304 }
321 305
322 TEST_F(NTPSnippetsServiceTest, InsertAtFront) { 306 TEST_F(NTPSnippetsServiceTest, InsertAtFront) {
323 base::Time expiry_time = base::Time::Now() + base::TimeDelta::FromHours(1); 307 base::Time expiry_time = base::Time::Now() + base::TimeDelta::FromHours(1);
324 char json_str_format[] = 308 char json_str_format[] =
325 "{ \"recos\": [ " 309 "{ \"recos\": [ "
326 "{ \"contentInfo\": {" 310 "{ \"contentInfo\": {"
327 "\"url\" : \"%s\"," 311 "\"url\" : \"%s\","
328 "\"title\" : \"Title\"," 312 "\"title\" : \"Title\","
329 "\"snippet\" : \"Snippet\"," 313 "\"snippet\" : \"Snippet\","
330 "\"thumbnailUrl\" : \"http://localhost/salient_image\"," 314 "\"thumbnailUrl\" : \"http://localhost/salient_image\","
331 "\"creationTimestampSec\" : \"%s\"," 315 "\"creationTimestampSec\" : \"%s\","
332 "\"expiryTimestampSec\" : \"%s\"," 316 "\"expiryTimestampSec\" : \"%s\","
333 "\"sourceCorpusInfo\" : [{\"corpusId\": \"http://first\"," 317 "\"sourceCorpusInfo\" : [{\"corpusId\": \"http://first\","
334 "\"publisherData\": {" 318 "\"publisherData\": {"
335 "\"sourceName\": \"Source 1\"" 319 "\"sourceName\": \"Source 1\""
336 "}," 320 "},"
337 "\"ampUrl\": \"\"}]" 321 "\"ampUrl\": \"\"}]"
338 "}}" 322 "}}"
339 "]}"; 323 "]}";
340 std::string json_str(base::StringPrintf( 324 std::string json_str(base::StringPrintf(
341 json_str_format, "http://first", 325 json_str_format, "http://first",
342 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()).c_str(), 326 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()).c_str(),
343 NTPSnippet::TimeToJsonString(expiry_time).c_str())); 327 NTPSnippet::TimeToJsonString(expiry_time).c_str()));
344 328
345 LoadFromJSONString(json_str); 329 LoadFromJSONString(json_str);
346 330
347 ASSERT_EQ(service()->size(), 1u); 331 EXPECT_THAT(service()->snippets(), ElementsAre(UrlEq("http://first")));
348 332
349 json_str = base::StringPrintf( 333 json_str = base::StringPrintf(
350 json_str_format, "http://second", 334 json_str_format, "http://second",
351 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()).c_str(), 335 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()).c_str(),
352 NTPSnippet::TimeToJsonString(expiry_time).c_str()); 336 NTPSnippet::TimeToJsonString(expiry_time).c_str());
353 337
354 LoadFromJSONString(json_str); 338 LoadFromJSONString(json_str);
355 ASSERT_EQ(service()->size(), 2u);
356 339
357 // The snippet loaded last should be at the first position in the list now. 340 // The snippet loaded last should be at the first position in the list now.
358 const NTPSnippet& first_snippet = *service()->begin(); 341 EXPECT_THAT(service()->snippets(),
359 EXPECT_EQ(first_snippet.url(), GURL("http://second")); 342 ElementsAre(UrlEq("http://second"), UrlEq("http://first")));
360 } 343 }
361 344
362 TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) { 345 TEST_F(NTPSnippetsServiceTest, LimitNumSnippets) {
363 int max_snippet_count = NTPSnippetsService::GetMaxSnippetCountForTesting(); 346 int max_snippet_count = NTPSnippetsService::GetMaxSnippetCountForTesting();
364 int snippets_per_load = max_snippet_count / 2 + 1; 347 int snippets_per_load = max_snippet_count / 2 + 1;
365 348
366 base::Time expiry_time = base::Time::Now() + base::TimeDelta::FromHours(1); 349 base::Time expiry_time = base::Time::Now() + base::TimeDelta::FromHours(1);
367 char json_str_format[] = 350 char json_str_format[] =
368 "{ \"contentInfo\": {" 351 "{ \"contentInfo\": {"
369 "\"url\" : \"http://localhost/%i\"," 352 "\"url\" : \"http://localhost/%i\","
(...skipping 17 matching lines...) Expand all
387 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()).c_str(), 370 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()).c_str(),
388 NTPSnippet::TimeToJsonString(expiry_time).c_str())); 371 NTPSnippet::TimeToJsonString(expiry_time).c_str()));
389 snippets2.push_back(base::StringPrintf( 372 snippets2.push_back(base::StringPrintf(
390 json_str_format, snippets_per_load + i, 373 json_str_format, snippets_per_load + i,
391 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()).c_str(), 374 NTPSnippet::TimeToJsonString(GetDefaultCreationTime()).c_str(),
392 NTPSnippet::TimeToJsonString(expiry_time).c_str())); 375 NTPSnippet::TimeToJsonString(expiry_time).c_str()));
393 } 376 }
394 377
395 LoadFromJSONString( 378 LoadFromJSONString(
396 "{ \"recos\": [ " + base::JoinString(snippets1, ", ") + "]}"); 379 "{ \"recos\": [ " + base::JoinString(snippets1, ", ") + "]}");
397 ASSERT_EQ(snippets1.size(), service()->size()); 380 ASSERT_THAT(service()->snippets(), SizeIs(snippets1.size()));
398 381
399 LoadFromJSONString( 382 LoadFromJSONString(
400 "{ \"recos\": [ " + base::JoinString(snippets2, ", ") + "]}"); 383 "{ \"recos\": [ " + base::JoinString(snippets2, ", ") + "]}");
401 EXPECT_EQ(max_snippet_count, (int)service()->size()); 384 EXPECT_THAT(service()->snippets(), SizeIs(max_snippet_count));
402 } 385 }
403 386
404 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) { 387 TEST_F(NTPSnippetsServiceTest, LoadInvalidJson) {
405 LoadFromJSONString(GetInvalidJson()); 388 LoadFromJSONString(GetInvalidJson());
406 EXPECT_THAT(service()->last_status(), StartsWith("Received invalid JSON")); 389 EXPECT_THAT(service()->last_status(), StartsWith("Received invalid JSON"));
407 EXPECT_EQ(service()->size(), 0u); 390 EXPECT_THAT(service()->snippets(), IsEmpty());
408 } 391 }
409 392
410 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) { 393 TEST_F(NTPSnippetsServiceTest, LoadInvalidJsonWithExistingSnippets) {
411 LoadFromJSONString(GetTestJson()); 394 LoadFromJSONString(GetTestJson());
412 ASSERT_EQ(service()->size(), 1u); 395 ASSERT_THAT(service()->snippets(), SizeIs(1));
413 ASSERT_EQ("OK", service()->last_status()); 396 ASSERT_EQ("OK", service()->last_status());
414 397
415 LoadFromJSONString(GetInvalidJson()); 398 LoadFromJSONString(GetInvalidJson());
416 EXPECT_THAT(service()->last_status(), StartsWith("Received invalid JSON")); 399 EXPECT_THAT(service()->last_status(), StartsWith("Received invalid JSON"));
417 // This should not have changed the existing snippets. 400 // This should not have changed the existing snippets.
418 EXPECT_EQ(service()->size(), 1u); 401 EXPECT_THAT(service()->snippets(), SizeIs(1));
419 } 402 }
420 403
421 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJson) { 404 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJson) {
422 LoadFromJSONString(GetIncompleteJson()); 405 LoadFromJSONString(GetIncompleteJson());
423 EXPECT_EQ("Invalid / empty list.", service()->last_status()); 406 EXPECT_EQ("Invalid / empty list.", service()->last_status());
424 EXPECT_EQ(service()->size(), 0u); 407 EXPECT_THAT(service()->snippets(), IsEmpty());
425 } 408 }
426 409
427 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) { 410 TEST_F(NTPSnippetsServiceTest, LoadIncompleteJsonWithExistingSnippets) {
428 LoadFromJSONString(GetTestJson()); 411 LoadFromJSONString(GetTestJson());
429 ASSERT_EQ(service()->size(), 1u); 412 ASSERT_THAT(service()->snippets(), SizeIs(1));
430 413
431 LoadFromJSONString(GetIncompleteJson()); 414 LoadFromJSONString(GetIncompleteJson());
432 EXPECT_EQ("Invalid / empty list.", service()->last_status()); 415 EXPECT_EQ("Invalid / empty list.", service()->last_status());
433 // This should not have changed the existing snippets. 416 // This should not have changed the existing snippets.
434 EXPECT_EQ(service()->size(), 1u); 417 EXPECT_THAT(service()->snippets(), SizeIs(1));
435 } 418 }
436 419
437 TEST_F(NTPSnippetsServiceTest, Discard) { 420 TEST_F(NTPSnippetsServiceTest, Discard) {
438 std::vector<std::string> source_urls, publishers, amp_urls; 421 std::vector<std::string> source_urls, publishers, amp_urls;
439 source_urls.push_back(std::string("http://site.com")); 422 source_urls.push_back(std::string("http://site.com"));
440 publishers.push_back(std::string("Source 1")); 423 publishers.push_back(std::string("Source 1"));
441 amp_urls.push_back(std::string()); 424 amp_urls.push_back(std::string());
442 std::string json_str( 425 std::string json_str(
443 GetTestJsonWithSources(source_urls, publishers, amp_urls)); 426 GetTestJsonWithSources(source_urls, publishers, amp_urls));
444 427
445 LoadFromJSONString(json_str); 428 LoadFromJSONString(json_str);
446 429
447 ASSERT_EQ(1u, service()->size()); 430 ASSERT_THAT(service()->snippets(), SizeIs(1));
448 431
449 // Discarding a non-existent snippet shouldn't do anything. 432 // Discarding a non-existent snippet shouldn't do anything.
450 EXPECT_FALSE(service()->DiscardSnippet(GURL("http://othersite.com"))); 433 EXPECT_FALSE(service()->DiscardSnippet(GURL("http://othersite.com")));
451 EXPECT_EQ(1u, service()->size()); 434 EXPECT_THAT(service()->snippets(), SizeIs(1));
452 435
453 // Discard the snippet. 436 // Discard the snippet.
454 EXPECT_TRUE(service()->DiscardSnippet(GURL("http://localhost/foobar"))); 437 EXPECT_TRUE(service()->DiscardSnippet(GURL("http://localhost/foobar")));
455 EXPECT_EQ(0u, service()->size()); 438 EXPECT_THAT(service()->snippets(), IsEmpty());
456 439
457 // Make sure that fetching the same snippet again does not re-add it. 440 // Make sure that fetching the same snippet again does not re-add it.
458 LoadFromJSONString(json_str); 441 LoadFromJSONString(json_str);
459 EXPECT_EQ(0u, service()->size()); 442 EXPECT_THAT(service()->snippets(), IsEmpty());
460 443
461 // The snippet should stay discarded even after re-creating the service. 444 // The snippet should stay discarded even after re-creating the service.
462 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); 445 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
463 CreateSnippetsService(/*enabled=*/true); 446 CreateSnippetsService(/*enabled=*/true);
464 LoadFromJSONString(json_str); 447 LoadFromJSONString(json_str);
465 EXPECT_EQ(0u, service()->size()); 448 EXPECT_THAT(service()->snippets(), IsEmpty());
466 449
467 // The snippet can be added again after clearing discarded snippets. 450 // The snippet can be added again after clearing discarded snippets.
468 service()->ClearDiscardedSnippets(); 451 service()->ClearDiscardedSnippets();
469 EXPECT_EQ(0u, service()->size()); 452 EXPECT_THAT(service()->snippets(), IsEmpty());
470 LoadFromJSONString(json_str); 453 LoadFromJSONString(json_str);
471 EXPECT_EQ(1u, service()->size()); 454 EXPECT_THAT(service()->snippets(), SizeIs(1));
472 } 455 }
473 456
474 TEST_F(NTPSnippetsServiceTest, GetDiscarded) { 457 TEST_F(NTPSnippetsServiceTest, GetDiscarded) {
475 LoadFromJSONString(GetTestJson()); 458 LoadFromJSONString(GetTestJson());
476 459
477 // For the test, we need the snippet to get discarded. 460 // For the test, we need the snippet to get discarded.
478 ASSERT_TRUE(service()->DiscardSnippet(GURL("http://localhost/foobar"))); 461 ASSERT_TRUE(service()->DiscardSnippet(GURL("http://localhost/foobar")));
479 const NTPSnippetsService::NTPSnippetStorage& snippets = 462 const NTPSnippetsService::NTPSnippetStorage& snippets =
480 service()->discarded_snippets(); 463 service()->discarded_snippets();
481 EXPECT_EQ(1u, snippets.size()); 464 EXPECT_EQ(1u, snippets.size());
482 for (auto& snippet : snippets) { 465 for (auto& snippet : snippets) {
483 EXPECT_EQ(GURL("http://localhost/foobar"), snippet->url()); 466 EXPECT_EQ(GURL("http://localhost/foobar"), snippet->url());
484 } 467 }
485 468
486 // There should be no discarded snippet after clearing the list. 469 // There should be no discarded snippet after clearing the list.
487 service()->ClearDiscardedSnippets(); 470 service()->ClearDiscardedSnippets();
488 EXPECT_EQ(0u, service()->discarded_snippets().size()); 471 EXPECT_EQ(0u, service()->discarded_snippets().size());
489 } 472 }
490 473
491 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) { 474 TEST_F(NTPSnippetsServiceTest, CreationTimestampParseFail) {
492 std::string json_str(GetTestJson("aaa1448459205")); 475 std::string json_str(GetTestJson("aaa1448459205"));
493 476
494 LoadFromJSONString(json_str); 477 LoadFromJSONString(json_str);
495 EXPECT_EQ(service()->size(), 1u); 478 ASSERT_THAT(service()->snippets(), SizeIs(1));
496 479 const NTPSnippet& snippet = *service()->snippets().front();
497 // The same for loop without the '&' should not compile. 480 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar"));
498 for (auto& snippet : *service()) { 481 EXPECT_EQ(snippet.title(), "Title");
499 // Snippet here is a const. 482 EXPECT_EQ(snippet.snippet(), "Snippet");
500 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); 483 EXPECT_EQ(base::Time::UnixEpoch(), snippet.publish_date());
501 EXPECT_EQ(snippet.title(), "Title");
502 EXPECT_EQ(snippet.snippet(), "Snippet");
503 EXPECT_EQ(base::Time::UnixEpoch(), snippet.publish_date());
504 }
505 } 484 }
506 485
507 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) { 486 TEST_F(NTPSnippetsServiceTest, RemoveExpiredContent) {
508 std::string json_str(GetTestExpiredJson()); 487 std::string json_str(GetTestExpiredJson());
509 488
510 LoadFromJSONString(json_str); 489 LoadFromJSONString(json_str);
511 EXPECT_EQ(service()->size(), 0u); 490 EXPECT_THAT(service()->snippets(), IsEmpty());
512 } 491 }
513 492
514 TEST_F(NTPSnippetsServiceTest, TestSingleSource) { 493 TEST_F(NTPSnippetsServiceTest, TestSingleSource) {
515 std::vector<std::string> source_urls, publishers, amp_urls; 494 std::vector<std::string> source_urls, publishers, amp_urls;
516 source_urls.push_back(std::string("http://source1.com")); 495 source_urls.push_back(std::string("http://source1.com"));
517 publishers.push_back(std::string("Source 1")); 496 publishers.push_back(std::string("Source 1"));
518 amp_urls.push_back(std::string("http://source1.amp.com")); 497 amp_urls.push_back(std::string("http://source1.amp.com"));
519 std::string json_str( 498 std::string json_str(
520 GetTestJsonWithSources(source_urls, publishers, amp_urls)); 499 GetTestJsonWithSources(source_urls, publishers, amp_urls));
521 500
522 LoadFromJSONString(json_str); 501 LoadFromJSONString(json_str);
523 502 ASSERT_THAT(service()->snippets(), SizeIs(1));
524 EXPECT_EQ(service()->size(), 1u); 503 const NTPSnippet& snippet = *service()->snippets().front();
525 504 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar"));
526 for (auto& snippet : *service()) { 505 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com"));
527 EXPECT_EQ(snippet.sources().size(), 1u); 506 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1"));
528 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); 507 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com"));
529 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com"));
530 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1"));
531 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com"));
532 }
533 } 508 }
534 509
535 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMalformedUrl) { 510 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMalformedUrl) {
536 std::vector<std::string> source_urls, publishers, amp_urls; 511 std::vector<std::string> source_urls, publishers, amp_urls;
537 source_urls.push_back(std::string("aaaa")); 512 source_urls.push_back(std::string("aaaa"));
538 publishers.push_back(std::string("Source 1")); 513 publishers.push_back(std::string("Source 1"));
539 amp_urls.push_back(std::string("http://source1.amp.com")); 514 amp_urls.push_back(std::string("http://source1.amp.com"));
540 std::string json_str( 515 std::string json_str(
541 GetTestJsonWithSources(source_urls, publishers, amp_urls)); 516 GetTestJsonWithSources(source_urls, publishers, amp_urls));
542 517
543 LoadFromJSONString(json_str); 518 LoadFromJSONString(json_str);
544 EXPECT_EQ(service()->size(), 0u); 519 EXPECT_THAT(service()->snippets(), IsEmpty());
545 } 520 }
546 521
547 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMissingData) { 522 TEST_F(NTPSnippetsServiceTest, TestSingleSourceWithMissingData) {
548 std::vector<std::string> source_urls, publishers, amp_urls; 523 std::vector<std::string> source_urls, publishers, amp_urls;
549 source_urls.push_back(std::string("http://source1.com")); 524 source_urls.push_back(std::string("http://source1.com"));
550 publishers.push_back(std::string()); 525 publishers.push_back(std::string());
551 amp_urls.push_back(std::string()); 526 amp_urls.push_back(std::string());
552 std::string json_str( 527 std::string json_str(
553 GetTestJsonWithSources(source_urls, publishers, amp_urls)); 528 GetTestJsonWithSources(source_urls, publishers, amp_urls));
554 529
555 LoadFromJSONString(json_str); 530 LoadFromJSONString(json_str);
556 EXPECT_EQ(service()->size(), 0u); 531 EXPECT_THAT(service()->snippets(), IsEmpty());
557 } 532 }
558 533
559 TEST_F(NTPSnippetsServiceTest, TestMultipleSources) { 534 TEST_F(NTPSnippetsServiceTest, TestMultipleSources) {
560 std::vector<std::string> source_urls, publishers, amp_urls; 535 std::vector<std::string> source_urls, publishers, amp_urls;
561 source_urls.push_back(std::string("http://source1.com")); 536 source_urls.push_back(std::string("http://source1.com"));
562 source_urls.push_back(std::string("http://source2.com")); 537 source_urls.push_back(std::string("http://source2.com"));
563 publishers.push_back(std::string("Source 1")); 538 publishers.push_back(std::string("Source 1"));
564 publishers.push_back(std::string("Source 2")); 539 publishers.push_back(std::string("Source 2"));
565 amp_urls.push_back(std::string("http://source1.amp.com")); 540 amp_urls.push_back(std::string("http://source1.amp.com"));
566 amp_urls.push_back(std::string("http://source2.amp.com")); 541 amp_urls.push_back(std::string("http://source2.amp.com"));
567 std::string json_str( 542 std::string json_str(
568 GetTestJsonWithSources(source_urls, publishers, amp_urls)); 543 GetTestJsonWithSources(source_urls, publishers, amp_urls));
569 544
570 LoadFromJSONString(json_str); 545 LoadFromJSONString(json_str);
571 EXPECT_EQ(service()->size(), 1u); 546 ASSERT_THAT(service()->snippets(), SizeIs(1));
572 547 const NTPSnippet& snippet = *service()->snippets().front();
573 // Expect the first source to be chosen 548 // Expect the first source to be chosen
574 for (auto& snippet : *service()) { 549 EXPECT_EQ(snippet.sources().size(), 2u);
575 EXPECT_EQ(snippet.sources().size(), 2u); 550 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar"));
576 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); 551 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com"));
577 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); 552 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1"));
578 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); 553 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com"));
579 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com"));
580 }
581 } 554 }
582 555
583 TEST_F(NTPSnippetsServiceTest, TestMultipleIncompleteSources) { 556 TEST_F(NTPSnippetsServiceTest, TestMultipleIncompleteSources) {
584 // Set Source 2 to have no AMP url, and Source 1 to have no publisher name 557 // Set Source 2 to have no AMP url, and Source 1 to have no publisher name
585 // Source 2 should win since we favor publisher name over amp url 558 // Source 2 should win since we favor publisher name over amp url
586 std::vector<std::string> source_urls, publishers, amp_urls; 559 std::vector<std::string> source_urls, publishers, amp_urls;
587 source_urls.push_back(std::string("http://source1.com")); 560 source_urls.push_back(std::string("http://source1.com"));
588 source_urls.push_back(std::string("http://source2.com")); 561 source_urls.push_back(std::string("http://source2.com"));
589 publishers.push_back(std::string()); 562 publishers.push_back(std::string());
590 publishers.push_back(std::string("Source 2")); 563 publishers.push_back(std::string("Source 2"));
591 amp_urls.push_back(std::string("http://source1.amp.com")); 564 amp_urls.push_back(std::string("http://source1.amp.com"));
592 amp_urls.push_back(std::string()); 565 amp_urls.push_back(std::string());
593 std::string json_str( 566 std::string json_str(
594 GetTestJsonWithSources(source_urls, publishers, amp_urls)); 567 GetTestJsonWithSources(source_urls, publishers, amp_urls));
595 568
596 LoadFromJSONString(json_str); 569 LoadFromJSONString(json_str);
597 EXPECT_EQ(service()->size(), 1u); 570 ASSERT_THAT(service()->snippets(), SizeIs(1));
598 571 {
599 for (auto& snippet : *service()) { 572 const NTPSnippet& snippet = *service()->snippets().front();
600 EXPECT_EQ(snippet.sources().size(), 2u); 573 EXPECT_EQ(snippet.sources().size(), 2u);
601 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); 574 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar"));
602 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); 575 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com"));
603 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); 576 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2"));
604 EXPECT_EQ(snippet.best_source().amp_url, GURL()); 577 EXPECT_EQ(snippet.best_source().amp_url, GURL());
605 } 578 }
606 579
607 service()->ClearSnippets(); 580 service()->ClearSnippets();
608 // Set Source 1 to have no AMP url, and Source 2 to have no publisher name 581 // Set Source 1 to have no AMP url, and Source 2 to have no publisher name
609 // Source 1 should win in this case since we prefer publisher name to AMP url 582 // Source 1 should win in this case since we prefer publisher name to AMP url
610 source_urls.clear(); 583 source_urls.clear();
611 source_urls.push_back(std::string("http://source1.com")); 584 source_urls.push_back(std::string("http://source1.com"));
612 source_urls.push_back(std::string("http://source2.com")); 585 source_urls.push_back(std::string("http://source2.com"));
613 publishers.clear(); 586 publishers.clear();
614 publishers.push_back(std::string("Source 1")); 587 publishers.push_back(std::string("Source 1"));
615 publishers.push_back(std::string()); 588 publishers.push_back(std::string());
616 amp_urls.clear(); 589 amp_urls.clear();
617 amp_urls.push_back(std::string()); 590 amp_urls.push_back(std::string());
618 amp_urls.push_back(std::string("http://source2.amp.com")); 591 amp_urls.push_back(std::string("http://source2.amp.com"));
619 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls); 592 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls);
620 593
621 LoadFromJSONString(json_str); 594 LoadFromJSONString(json_str);
622 EXPECT_EQ(service()->size(), 1u); 595 ASSERT_THAT(service()->snippets(), SizeIs(1));
623 596 {
624 for (auto& snippet : *service()) { 597 const NTPSnippet& snippet = *service()->snippets().front();
625 EXPECT_EQ(snippet.sources().size(), 2u); 598 EXPECT_EQ(snippet.sources().size(), 2u);
626 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); 599 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar"));
627 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); 600 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com"));
628 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); 601 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1"));
629 EXPECT_EQ(snippet.best_source().amp_url, GURL()); 602 EXPECT_EQ(snippet.best_source().amp_url, GURL());
630 } 603 }
631 604
632 service()->ClearSnippets(); 605 service()->ClearSnippets();
633 // Set source 1 to have no AMP url and no source, and source 2 to only have 606 // Set source 1 to have no AMP url and no source, and source 2 to only have
634 // amp url. There should be no snippets since we only add sources we consider 607 // amp url. There should be no snippets since we only add sources we consider
635 // complete 608 // complete
636 source_urls.clear(); 609 source_urls.clear();
637 source_urls.push_back(std::string("http://source1.com")); 610 source_urls.push_back(std::string("http://source1.com"));
638 source_urls.push_back(std::string("http://source2.com")); 611 source_urls.push_back(std::string("http://source2.com"));
639 publishers.clear(); 612 publishers.clear();
640 publishers.push_back(std::string()); 613 publishers.push_back(std::string());
641 publishers.push_back(std::string()); 614 publishers.push_back(std::string());
642 amp_urls.clear(); 615 amp_urls.clear();
643 amp_urls.push_back(std::string()); 616 amp_urls.push_back(std::string());
644 amp_urls.push_back(std::string("http://source2.amp.com")); 617 amp_urls.push_back(std::string("http://source2.amp.com"));
645 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls); 618 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls);
646 619
647 LoadFromJSONString(json_str); 620 LoadFromJSONString(json_str);
648 EXPECT_EQ(service()->size(), 0u); 621 EXPECT_THAT(service()->snippets(), IsEmpty());
649 } 622 }
650 623
651 TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) { 624 TEST_F(NTPSnippetsServiceTest, TestMultipleCompleteSources) {
652 // Test 2 complete sources, we should choose the first complete source 625 // Test 2 complete sources, we should choose the first complete source
653 std::vector<std::string> source_urls, publishers, amp_urls; 626 std::vector<std::string> source_urls, publishers, amp_urls;
654 source_urls.push_back(std::string("http://source1.com")); 627 source_urls.push_back(std::string("http://source1.com"));
655 source_urls.push_back(std::string("http://source2.com")); 628 source_urls.push_back(std::string("http://source2.com"));
656 source_urls.push_back(std::string("http://source3.com")); 629 source_urls.push_back(std::string("http://source3.com"));
657 publishers.push_back(std::string("Source 1")); 630 publishers.push_back(std::string("Source 1"));
658 publishers.push_back(std::string()); 631 publishers.push_back(std::string());
659 publishers.push_back(std::string("Source 3")); 632 publishers.push_back(std::string("Source 3"));
660 amp_urls.push_back(std::string("http://source1.amp.com")); 633 amp_urls.push_back(std::string("http://source1.amp.com"));
661 amp_urls.push_back(std::string("http://source2.amp.com")); 634 amp_urls.push_back(std::string("http://source2.amp.com"));
662 amp_urls.push_back(std::string("http://source3.amp.com")); 635 amp_urls.push_back(std::string("http://source3.amp.com"));
663 std::string json_str( 636 std::string json_str(
664 GetTestJsonWithSources(source_urls, publishers, amp_urls)); 637 GetTestJsonWithSources(source_urls, publishers, amp_urls));
665 638
666 LoadFromJSONString(json_str); 639 LoadFromJSONString(json_str);
667 EXPECT_EQ(service()->size(), 1u); 640 ASSERT_THAT(service()->snippets(), SizeIs(1));
668 641 {
669 for (auto& snippet : *service()) { 642 const NTPSnippet& snippet = *service()->snippets().front();
670 EXPECT_EQ(snippet.sources().size(), 3u); 643 EXPECT_EQ(snippet.sources().size(), 3u);
671 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); 644 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar"));
672 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com")); 645 EXPECT_EQ(snippet.best_source().url, GURL("http://source1.com"));
673 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1")); 646 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 1"));
674 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com")); 647 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source1.amp.com"));
675 } 648 }
676 649
677 // Test 2 complete sources, we should choose the first complete source 650 // Test 2 complete sources, we should choose the first complete source
678 service()->ClearSnippets(); 651 service()->ClearSnippets();
679 source_urls.clear(); 652 source_urls.clear();
680 source_urls.push_back(std::string("http://source1.com")); 653 source_urls.push_back(std::string("http://source1.com"));
681 source_urls.push_back(std::string("http://source2.com")); 654 source_urls.push_back(std::string("http://source2.com"));
682 source_urls.push_back(std::string("http://source3.com")); 655 source_urls.push_back(std::string("http://source3.com"));
683 publishers.clear(); 656 publishers.clear();
684 publishers.push_back(std::string()); 657 publishers.push_back(std::string());
685 publishers.push_back(std::string("Source 2")); 658 publishers.push_back(std::string("Source 2"));
686 publishers.push_back(std::string("Source 3")); 659 publishers.push_back(std::string("Source 3"));
687 amp_urls.clear(); 660 amp_urls.clear();
688 amp_urls.push_back(std::string("http://source1.amp.com")); 661 amp_urls.push_back(std::string("http://source1.amp.com"));
689 amp_urls.push_back(std::string("http://source2.amp.com")); 662 amp_urls.push_back(std::string("http://source2.amp.com"));
690 amp_urls.push_back(std::string("http://source3.amp.com")); 663 amp_urls.push_back(std::string("http://source3.amp.com"));
691 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls); 664 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls);
692 665
693 LoadFromJSONString(json_str); 666 LoadFromJSONString(json_str);
694 EXPECT_EQ(service()->size(), 1u); 667 ASSERT_THAT(service()->snippets(), SizeIs(1));
695 668 {
696 for (auto& snippet : *service()) { 669 const NTPSnippet& snippet = *service()->snippets().front();
697 EXPECT_EQ(snippet.sources().size(), 3u); 670 EXPECT_EQ(snippet.sources().size(), 3u);
698 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); 671 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar"));
699 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); 672 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com"));
700 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); 673 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2"));
701 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); 674 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com"));
702 } 675 }
703 676
704 // Test 3 complete sources, we should choose the first complete source 677 // Test 3 complete sources, we should choose the first complete source
705 service()->ClearSnippets(); 678 service()->ClearSnippets();
706 source_urls.clear(); 679 source_urls.clear();
707 source_urls.push_back(std::string("http://source1.com")); 680 source_urls.push_back(std::string("http://source1.com"));
708 source_urls.push_back(std::string("http://source2.com")); 681 source_urls.push_back(std::string("http://source2.com"));
709 source_urls.push_back(std::string("http://source3.com")); 682 source_urls.push_back(std::string("http://source3.com"));
710 publishers.clear(); 683 publishers.clear();
711 publishers.push_back(std::string("Source 1")); 684 publishers.push_back(std::string("Source 1"));
712 publishers.push_back(std::string("Source 2")); 685 publishers.push_back(std::string("Source 2"));
713 publishers.push_back(std::string("Source 3")); 686 publishers.push_back(std::string("Source 3"));
714 amp_urls.clear(); 687 amp_urls.clear();
715 amp_urls.push_back(std::string()); 688 amp_urls.push_back(std::string());
716 amp_urls.push_back(std::string("http://source2.amp.com")); 689 amp_urls.push_back(std::string("http://source2.amp.com"));
717 amp_urls.push_back(std::string("http://source3.amp.com")); 690 amp_urls.push_back(std::string("http://source3.amp.com"));
718 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls); 691 json_str = GetTestJsonWithSources(source_urls, publishers, amp_urls);
719 692
720 LoadFromJSONString(json_str); 693 LoadFromJSONString(json_str);
721 EXPECT_EQ(service()->size(), 1u); 694 ASSERT_THAT(service()->snippets(), SizeIs(1));
722 695 {
723 for (auto& snippet : *service()) { 696 const NTPSnippet& snippet = *service()->snippets().front();
724 EXPECT_EQ(snippet.sources().size(), 3u); 697 EXPECT_EQ(snippet.sources().size(), 3u);
725 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar")); 698 EXPECT_EQ(snippet.url(), GURL("http://localhost/foobar"));
726 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com")); 699 EXPECT_EQ(snippet.best_source().url, GURL("http://source2.com"));
727 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2")); 700 EXPECT_EQ(snippet.best_source().publisher_name, std::string("Source 2"));
728 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com")); 701 EXPECT_EQ(snippet.best_source().amp_url, GURL("http://source2.amp.com"));
729 } 702 }
730 } 703 }
731 704
732 TEST_F(NTPSnippetsServiceTest, LogNumArticlesHistogram) { 705 TEST_F(NTPSnippetsServiceTest, LogNumArticlesHistogram) {
733 base::HistogramTester tester; 706 base::HistogramTester tester;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
776 EXPECT_THAT( 749 EXPECT_THAT(
777 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"), 750 tester.GetAllSamples("NewTabPage.Snippets.NumArticlesZeroDueToDiscarded"),
778 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1))); 751 ElementsAre(base::Bucket(/*min=*/1, /*count=*/1)));
779 // Recreating the service and loading from prefs shouldn't count as fetched 752 // Recreating the service and loading from prefs shouldn't count as fetched
780 // articles. 753 // articles.
781 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1); 754 EXPECT_CALL(mock_scheduler(), Schedule(_, _, _, _)).Times(1);
782 CreateSnippetsService(/*enabled=*/true); 755 CreateSnippetsService(/*enabled=*/true);
783 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4); 756 tester.ExpectTotalCount("NewTabPage.Snippets.NumArticlesFetched", 4);
784 } 757 }
785 } // namespace ntp_snippets 758 } // 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