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

Side by Side Diff: components/ntp_snippets/remote/remote_suggestion_unittest.cc

Issue 2686063003: [remote suggestions] Attach the fetch time to RemoteSnippets, ContentSnippets and SnippetArticle (Closed)
Patch Set: Update comments Created 3 years, 10 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/remote/remote_suggestion.h" 5 #include "components/ntp_snippets/remote/remote_suggestion.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
12 #include "base/values.h" 13 #include "base/values.h"
13 #include "components/ntp_snippets/remote/proto/ntp_snippets.pb.h" 14 #include "components/ntp_snippets/remote/proto/ntp_snippets.pb.h"
14 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 17
17 namespace ntp_snippets { 18 namespace ntp_snippets {
18 namespace { 19 namespace {
19 20
20 using ::testing::ElementsAre; 21 using ::testing::ElementsAre;
21 using ::testing::Eq; 22 using ::testing::Eq;
22 using ::testing::IsNull; 23 using ::testing::IsNull;
23 using ::testing::NotNull; 24 using ::testing::NotNull;
24 25
25 std::unique_ptr<RemoteSuggestion> SnippetFromContentSuggestionJSON( 26 std::unique_ptr<RemoteSuggestion> SnippetFromContentSuggestionJSON(
26 const std::string& json) { 27 const std::string& json,
28 const base::Time fetch_time) {
27 auto json_value = base::JSONReader::Read(json); 29 auto json_value = base::JSONReader::Read(json);
28 base::DictionaryValue* json_dict; 30 base::DictionaryValue* json_dict;
29 if (!json_value->GetAsDictionary(&json_dict)) { 31 if (!json_value->GetAsDictionary(&json_dict)) {
30 return nullptr; 32 return nullptr;
31 } 33 }
32 return RemoteSuggestion::CreateFromContentSuggestionsDictionary( 34 return RemoteSuggestion::CreateFromContentSuggestionsDictionary(
33 *json_dict, kArticlesRemoteId); 35 *json_dict, kArticlesRemoteId, fetch_time);
34 } 36 }
35 37
36 TEST(RemoteSuggestionTest, FromChromeContentSuggestionsDictionary) { 38 TEST(RemoteSuggestionTest, FromChromeContentSuggestionsDictionary) {
37 const std::string kJsonStr = 39 const std::string kJsonStr =
38 "{" 40 "{"
39 " \"ids\" : [\"http://localhost/foobar\"]," 41 " \"ids\" : [\"http://localhost/foobar\"],"
40 " \"title\" : \"Foo Barred from Baz\"," 42 " \"title\" : \"Foo Barred from Baz\","
41 " \"snippet\" : \"...\"," 43 " \"snippet\" : \"...\","
42 " \"fullPageUrl\" : \"http://localhost/foobar\"," 44 " \"fullPageUrl\" : \"http://localhost/foobar\","
43 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\"," 45 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\","
44 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," 46 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\","
45 " \"attribution\" : \"Foo News\"," 47 " \"attribution\" : \"Foo News\","
46 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," 48 " \"imageUrl\" : \"http://localhost/foobar.jpg\","
47 " \"ampUrl\" : \"http://localhost/amp\"," 49 " \"ampUrl\" : \"http://localhost/amp\","
48 " \"faviconUrl\" : \"http://localhost/favicon.ico\", " 50 " \"faviconUrl\" : \"http://localhost/favicon.ico\", "
49 " \"score\": 9001,\n" 51 " \"score\": 9001,\n"
50 " \"notificationInfo\": {\n" 52 " \"notificationInfo\": {\n"
51 " \"shouldNotify\": true," 53 " \"shouldNotify\": true,"
52 " \"deadline\": \"2016-06-30T13:01:37.000Z\"\n" 54 " \"deadline\": \"2016-06-30T13:01:37.000Z\"\n"
53 " }\n" 55 " }\n"
54 "}"; 56 "}";
55 auto snippet = SnippetFromContentSuggestionJSON(kJsonStr); 57 const base::Time fetch_time = base::Time::Now();
58 auto snippet = SnippetFromContentSuggestionJSON(kJsonStr, fetch_time);
56 ASSERT_THAT(snippet, NotNull()); 59 ASSERT_THAT(snippet, NotNull());
57 60
58 EXPECT_EQ(snippet->id(), "http://localhost/foobar"); 61 EXPECT_EQ(snippet->id(), "http://localhost/foobar");
59 EXPECT_EQ(snippet->title(), "Foo Barred from Baz"); 62 EXPECT_EQ(snippet->title(), "Foo Barred from Baz");
60 EXPECT_EQ(snippet->snippet(), "..."); 63 EXPECT_EQ(snippet->snippet(), "...");
61 EXPECT_EQ(snippet->salient_image_url(), GURL("http://localhost/foobar.jpg")); 64 EXPECT_EQ(snippet->salient_image_url(), GURL("http://localhost/foobar.jpg"));
62 EXPECT_EQ(snippet->score(), 9001); 65 EXPECT_EQ(snippet->score(), 9001);
63 auto unix_publish_date = snippet->publish_date() - base::Time::UnixEpoch(); 66 auto unix_publish_date = snippet->publish_date() - base::Time::UnixEpoch();
64 auto expiry_duration = snippet->expiry_date() - snippet->publish_date(); 67 auto expiry_duration = snippet->expiry_date() - snippet->publish_date();
65 EXPECT_FLOAT_EQ(unix_publish_date.InSecondsF(), 1467284497.000000f); 68 EXPECT_FLOAT_EQ(unix_publish_date.InSecondsF(), 1467284497.000000f);
66 EXPECT_FLOAT_EQ(expiry_duration.InSecondsF(), 86400.000000f); 69 EXPECT_FLOAT_EQ(expiry_duration.InSecondsF(), 86400.000000f);
67 70
68 EXPECT_EQ(snippet->publisher_name(), "Foo News"); 71 EXPECT_EQ(snippet->publisher_name(), "Foo News");
69 EXPECT_EQ(snippet->url(), GURL("http://localhost/foobar")); 72 EXPECT_EQ(snippet->url(), GURL("http://localhost/foobar"));
70 EXPECT_EQ(snippet->amp_url(), GURL("http://localhost/amp")); 73 EXPECT_EQ(snippet->amp_url(), GURL("http://localhost/amp"));
71 74
72 EXPECT_TRUE(snippet->should_notify()); 75 EXPECT_TRUE(snippet->should_notify());
73 auto notification_duration = 76 auto notification_duration =
74 snippet->notification_deadline() - snippet->publish_date(); 77 snippet->notification_deadline() - snippet->publish_date();
75 EXPECT_EQ(7200.0f, notification_duration.InSecondsF()); 78 EXPECT_EQ(7200.0f, notification_duration.InSecondsF());
76 } 79 }
77 80
78 std::unique_ptr<RemoteSuggestion> SnippetFromChromeReaderDict( 81 std::unique_ptr<RemoteSuggestion> SnippetFromChromeReaderDict(
79 std::unique_ptr<base::DictionaryValue> dict) { 82 std::unique_ptr<base::DictionaryValue> dict) {
80 if (!dict) { 83 if (!dict) {
81 return nullptr; 84 return nullptr;
82 } 85 }
83 return RemoteSuggestion::CreateFromChromeReaderDictionary(*dict); 86 // TODO
87 return RemoteSuggestion::CreateFromChromeReaderDictionary(*dict,
88 base::Time::Now());
84 } 89 }
85 90
86 const char kChromeReaderCreationTimestamp[] = "1234567890"; 91 const char kChromeReaderCreationTimestamp[] = "1234567890";
87 const char kChromeReaderExpiryTimestamp[] = "2345678901"; 92 const char kChromeReaderExpiryTimestamp[] = "2345678901";
88 93
89 // Old form, from chromereader-pa.googleapis.com. Two sources. 94 // Old form, from chromereader-pa.googleapis.com. Two sources.
90 std::unique_ptr<base::DictionaryValue> ChromeReaderSnippetWithTwoSources() { 95 std::unique_ptr<base::DictionaryValue> ChromeReaderSnippetWithTwoSources() {
91 const std::string kJsonStr = base::StringPrintf( 96 const std::string kJsonStr = base::StringPrintf(
92 "{\n" 97 "{\n"
93 " \"contentInfo\": {\n" 98 " \"contentInfo\": {\n"
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 " \"title\" : \"Foo Barred from Baz\"," 372 " \"title\" : \"Foo Barred from Baz\","
368 " \"snippet\" : \"...\"," 373 " \"snippet\" : \"...\","
369 " \"fullPageUrl\" : \"http://localhost/foobar\"," 374 " \"fullPageUrl\" : \"http://localhost/foobar\","
370 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\"," 375 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\","
371 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," 376 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\","
372 " \"attribution\" : \"Foo News\"," 377 " \"attribution\" : \"Foo News\","
373 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," 378 " \"imageUrl\" : \"http://localhost/foobar.jpg\","
374 " \"ampUrl\" : \"http://localhost/amp\"," 379 " \"ampUrl\" : \"http://localhost/amp\","
375 " \"faviconUrl\" : \"http://localhost/favicon.ico\" " 380 " \"faviconUrl\" : \"http://localhost/favicon.ico\" "
376 "}"; 381 "}";
377 auto snippet = SnippetFromContentSuggestionJSON(kJsonStr); 382 const base::Time fetch_time = base::Time::Now();
383 auto snippet = SnippetFromContentSuggestionJSON(kJsonStr, fetch_time);
378 ASSERT_THAT(snippet, NotNull()); 384 ASSERT_THAT(snippet, NotNull());
379 385
380 EXPECT_EQ(snippet->id(), "http://localhost/foobar"); 386 EXPECT_EQ(snippet->id(), "http://localhost/foobar");
381 EXPECT_THAT(snippet->GetAllIDs(), 387 EXPECT_THAT(snippet->GetAllIDs(),
382 ElementsAre("http://localhost/foobar", "012345")); 388 ElementsAre("http://localhost/foobar", "012345"));
383 } 389 }
384 390
385 TEST(RemoteSuggestionTest, CreateFromProtoToProtoRoundtrip) { 391 TEST(RemoteSuggestionTest, CreateFromProtoToProtoRoundtrip) {
386 SnippetProto proto; 392 SnippetProto proto;
387 proto.add_ids("foo"); 393 proto.add_ids("foo");
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 auto json_value = base::JSONReader::Read(kJsonStr); 440 auto json_value = base::JSONReader::Read(kJsonStr);
435 base::DictionaryValue* json_dict; 441 base::DictionaryValue* json_dict;
436 CHECK(json_value->GetAsDictionary(&json_dict)); 442 CHECK(json_value->GetAsDictionary(&json_dict));
437 return json_dict->CreateDeepCopy(); 443 return json_dict->CreateDeepCopy();
438 } 444 }
439 445
440 TEST(RemoteSuggestionTest, NotifcationInfoAllSpecified) { 446 TEST(RemoteSuggestionTest, NotifcationInfoAllSpecified) {
441 auto json = ContentSuggestionSnippet(); 447 auto json = ContentSuggestionSnippet();
442 json->SetBoolean("notificationInfo.shouldNotify", true); 448 json->SetBoolean("notificationInfo.shouldNotify", true);
443 json->SetString("notificationInfo.deadline", "2016-06-30T13:01:37.000Z"); 449 json->SetString("notificationInfo.deadline", "2016-06-30T13:01:37.000Z");
444 auto snippet = 450 auto snippet = RemoteSuggestion::CreateFromContentSuggestionsDictionary(
445 RemoteSuggestion::CreateFromContentSuggestionsDictionary(*json, 0); 451 *json, 0, base::Time::Now());
446 EXPECT_TRUE(snippet->should_notify()); 452 EXPECT_TRUE(snippet->should_notify());
447 EXPECT_EQ(7200.0f, 453 EXPECT_EQ(7200.0f,
448 (snippet->notification_deadline() - snippet->publish_date()) 454 (snippet->notification_deadline() - snippet->publish_date())
449 .InSecondsF()); 455 .InSecondsF());
450 } 456 }
451 457
452 TEST(RemoteSuggestionTest, NotificationInfoDeadlineInvalid) { 458 TEST(RemoteSuggestionTest, NotificationInfoDeadlineInvalid) {
453 auto json = ContentSuggestionSnippet(); 459 auto json = ContentSuggestionSnippet();
454 json->SetBoolean("notificationInfo.shouldNotify", true); 460 json->SetBoolean("notificationInfo.shouldNotify", true);
455 json->SetInteger("notificationInfo.notificationDeadline", 0); 461 json->SetInteger("notificationInfo.notificationDeadline", 0);
456 auto snippet = 462 auto snippet = RemoteSuggestion::CreateFromContentSuggestionsDictionary(
457 RemoteSuggestion::CreateFromContentSuggestionsDictionary(*json, 0); 463 *json, 0, base::Time::Now());
458 EXPECT_TRUE(snippet->should_notify()); 464 EXPECT_TRUE(snippet->should_notify());
459 EXPECT_EQ(base::Time::Max(), snippet->notification_deadline()); 465 EXPECT_EQ(base::Time::Max(), snippet->notification_deadline());
460 } 466 }
461 467
462 TEST(RemoteSuggestionTest, NotificationInfoDeadlineAbsent) { 468 TEST(RemoteSuggestionTest, NotificationInfoDeadlineAbsent) {
463 auto json = ContentSuggestionSnippet(); 469 auto json = ContentSuggestionSnippet();
464 json->SetBoolean("notificationInfo.shouldNotify", true); 470 json->SetBoolean("notificationInfo.shouldNotify", true);
465 auto snippet = 471 auto snippet = RemoteSuggestion::CreateFromContentSuggestionsDictionary(
466 RemoteSuggestion::CreateFromContentSuggestionsDictionary(*json, 0); 472 *json, 0, base::Time::Now());
467 EXPECT_TRUE(snippet->should_notify()); 473 EXPECT_TRUE(snippet->should_notify());
468 EXPECT_EQ(base::Time::Max(), snippet->notification_deadline()); 474 EXPECT_EQ(base::Time::Max(), snippet->notification_deadline());
469 } 475 }
470 476
471 TEST(RemoteSuggestionTest, NotificationInfoShouldNotifyInvalid) { 477 TEST(RemoteSuggestionTest, NotificationInfoShouldNotifyInvalid) {
472 auto json = ContentSuggestionSnippet(); 478 auto json = ContentSuggestionSnippet();
473 json->SetString("notificationInfo.shouldNotify", "non-bool"); 479 json->SetString("notificationInfo.shouldNotify", "non-bool");
474 auto snippet = 480 auto snippet = RemoteSuggestion::CreateFromContentSuggestionsDictionary(
475 RemoteSuggestion::CreateFromContentSuggestionsDictionary(*json, 0); 481 *json, 0, base::Time::Now());
476 EXPECT_FALSE(snippet->should_notify()); 482 EXPECT_FALSE(snippet->should_notify());
477 } 483 }
478 484
479 TEST(RemoteSuggestionTest, NotificationInfoAbsent) { 485 TEST(RemoteSuggestionTest, NotificationInfoAbsent) {
480 auto json = ContentSuggestionSnippet(); 486 auto json = ContentSuggestionSnippet();
481 auto snippet = 487 auto snippet = RemoteSuggestion::CreateFromContentSuggestionsDictionary(
482 RemoteSuggestion::CreateFromContentSuggestionsDictionary(*json, 0); 488 *json, 0, base::Time::Now());
483 EXPECT_FALSE(snippet->should_notify()); 489 EXPECT_FALSE(snippet->should_notify());
484 } 490 }
485 491
486 TEST(RemoteSuggestionTest, ToContentSuggestion) { 492 TEST(RemoteSuggestionTest, ToContentSuggestion) {
487 auto json = ContentSuggestionSnippet(); 493 auto json = ContentSuggestionSnippet();
488 auto snippet = 494 auto snippet = RemoteSuggestion::CreateFromContentSuggestionsDictionary(
489 RemoteSuggestion::CreateFromContentSuggestionsDictionary(*json, 0); 495 *json, 0, base::Time::Now());
490 ASSERT_THAT(snippet, NotNull()); 496 ASSERT_THAT(snippet, NotNull());
491 ContentSuggestion sugg = snippet->ToContentSuggestion( 497 ContentSuggestion sugg = snippet->ToContentSuggestion(
492 Category::FromKnownCategory(KnownCategories::ARTICLES)); 498 Category::FromKnownCategory(KnownCategories::ARTICLES));
493 499
494 EXPECT_THAT(sugg.id().category(), 500 EXPECT_THAT(sugg.id().category(),
495 Eq(Category::FromKnownCategory(KnownCategories::ARTICLES))); 501 Eq(Category::FromKnownCategory(KnownCategories::ARTICLES)));
496 EXPECT_THAT(sugg.id().id_within_category(), Eq("http://localhost/foobar")); 502 EXPECT_THAT(sugg.id().id_within_category(), Eq("http://localhost/foobar"));
497 EXPECT_THAT(sugg.url(), Eq(GURL("http://localhost/amp"))); 503 EXPECT_THAT(sugg.url(), Eq(GURL("http://localhost/amp")));
498 EXPECT_THAT(sugg.title(), Eq(base::UTF8ToUTF16("Foo Barred from Baz"))); 504 EXPECT_THAT(sugg.title(), Eq(base::UTF8ToUTF16("Foo Barred from Baz")));
499 EXPECT_THAT(sugg.snippet_text(), Eq(base::UTF8ToUTF16("..."))); 505 EXPECT_THAT(sugg.snippet_text(), Eq(base::UTF8ToUTF16("...")));
500 EXPECT_THAT(sugg.publish_date().ToJavaTime(), Eq(1467284497000)); 506 EXPECT_THAT(sugg.publish_date().ToJavaTime(), Eq(1467284497000));
501 EXPECT_THAT(sugg.publisher_name(), Eq(base::UTF8ToUTF16("Foo News"))); 507 EXPECT_THAT(sugg.publisher_name(), Eq(base::UTF8ToUTF16("Foo News")));
502 EXPECT_THAT(sugg.score(), Eq(9001)); 508 EXPECT_THAT(sugg.score(), Eq(9001));
503 EXPECT_THAT(sugg.download_suggestion_extra(), IsNull()); 509 EXPECT_THAT(sugg.download_suggestion_extra(), IsNull());
504 EXPECT_THAT(sugg.recent_tab_suggestion_extra(), IsNull()); 510 EXPECT_THAT(sugg.recent_tab_suggestion_extra(), IsNull());
505 EXPECT_THAT(sugg.notification_extra(), IsNull()); 511 EXPECT_THAT(sugg.notification_extra(), IsNull());
506 } 512 }
507 513
508 TEST(RemoteSuggestionTest, ToContentSuggestionWithNotificationInfo) { 514 TEST(RemoteSuggestionTest, ToContentSuggestionWithNotificationInfo) {
509 auto json = ContentSuggestionSnippet(); 515 auto json = ContentSuggestionSnippet();
510 json->SetBoolean("notificationInfo.shouldNotify", true); 516 json->SetBoolean("notificationInfo.shouldNotify", true);
511 json->SetString("notificationInfo.deadline", "2016-06-30T13:01:37.000Z"); 517 json->SetString("notificationInfo.deadline", "2016-06-30T13:01:37.000Z");
512 auto snippet = 518 auto snippet = RemoteSuggestion::CreateFromContentSuggestionsDictionary(
513 RemoteSuggestion::CreateFromContentSuggestionsDictionary(*json, 0); 519 *json, 0, base::Time::Now());
514 ASSERT_THAT(snippet, NotNull()); 520 ASSERT_THAT(snippet, NotNull());
515 ContentSuggestion sugg = snippet->ToContentSuggestion( 521 ContentSuggestion sugg = snippet->ToContentSuggestion(
516 Category::FromKnownCategory(KnownCategories::ARTICLES)); 522 Category::FromKnownCategory(KnownCategories::ARTICLES));
517 523
518 EXPECT_THAT(sugg.id().category(), 524 EXPECT_THAT(sugg.id().category(),
519 Eq(Category::FromKnownCategory(KnownCategories::ARTICLES))); 525 Eq(Category::FromKnownCategory(KnownCategories::ARTICLES)));
520 EXPECT_THAT(sugg.id().id_within_category(), Eq("http://localhost/foobar")); 526 EXPECT_THAT(sugg.id().id_within_category(), Eq("http://localhost/foobar"));
521 EXPECT_THAT(sugg.url(), Eq(GURL("http://localhost/amp"))); 527 EXPECT_THAT(sugg.url(), Eq(GURL("http://localhost/amp")));
522 EXPECT_THAT(sugg.title(), Eq(base::UTF8ToUTF16("Foo Barred from Baz"))); 528 EXPECT_THAT(sugg.title(), Eq(base::UTF8ToUTF16("Foo Barred from Baz")));
523 EXPECT_THAT(sugg.snippet_text(), Eq(base::UTF8ToUTF16("..."))); 529 EXPECT_THAT(sugg.snippet_text(), Eq(base::UTF8ToUTF16("...")));
524 EXPECT_THAT(sugg.publish_date().ToJavaTime(), Eq(1467284497000)); 530 EXPECT_THAT(sugg.publish_date().ToJavaTime(), Eq(1467284497000));
525 EXPECT_THAT(sugg.publisher_name(), Eq(base::UTF8ToUTF16("Foo News"))); 531 EXPECT_THAT(sugg.publisher_name(), Eq(base::UTF8ToUTF16("Foo News")));
526 EXPECT_THAT(sugg.score(), Eq(9001)); 532 EXPECT_THAT(sugg.score(), Eq(9001));
527 EXPECT_THAT(sugg.download_suggestion_extra(), IsNull()); 533 EXPECT_THAT(sugg.download_suggestion_extra(), IsNull());
528 EXPECT_THAT(sugg.recent_tab_suggestion_extra(), IsNull()); 534 EXPECT_THAT(sugg.recent_tab_suggestion_extra(), IsNull());
529 ASSERT_THAT(sugg.notification_extra(), NotNull()); 535 ASSERT_THAT(sugg.notification_extra(), NotNull());
530 EXPECT_THAT(sugg.notification_extra()->deadline.ToJavaTime(), 536 EXPECT_THAT(sugg.notification_extra()->deadline.ToJavaTime(),
531 Eq(1467291697000)); 537 Eq(1467291697000));
532 } 538 }
533 539
534 } // namespace 540 } // namespace
535 } // namespace ntp_snippets 541 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698