Chromium Code Reviews| Index: components/ntp_snippets/remote/ntp_snippet_unittest.cc |
| diff --git a/components/ntp_snippets/remote/ntp_snippet_unittest.cc b/components/ntp_snippets/remote/ntp_snippet_unittest.cc |
| index 6df9ec50cafcebb8debbbc667977e4fe3619d00c..77a78e8ba8ea1df33b0c896b756166e1aba49248 100644 |
| --- a/components/ntp_snippets/remote/ntp_snippet_unittest.cc |
| +++ b/components/ntp_snippets/remote/ntp_snippet_unittest.cc |
| @@ -8,6 +8,7 @@ |
| #include "base/json/json_reader.h" |
| #include "base/strings/stringprintf.h" |
| +#include "base/strings/utf_string_conversions.h" |
| #include "base/values.h" |
| #include "components/ntp_snippets/remote/proto/ntp_snippets.pb.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| @@ -17,6 +18,7 @@ namespace ntp_snippets { |
| namespace { |
| using ::testing::ElementsAre; |
| +using ::testing::Eq; |
| using ::testing::IsNull; |
| using ::testing::NotNull; |
| @@ -44,7 +46,11 @@ TEST(NTPSnippetTest, FromChromeContentSuggestionsDictionary) { |
| " \"imageUrl\" : \"http://localhost/foobar.jpg\"," |
| " \"ampUrl\" : \"http://localhost/amp\"," |
| " \"faviconUrl\" : \"http://localhost/favicon.ico\", " |
| - " \"score\": 9001\n" |
| + " \"score\": 9001,\n" |
| + " \"notificationInfo\": {\n" |
| + " \"shouldNotify\": true," |
| + " \"deadline\": \"2016-06-30T13:01:37.000Z\"\n" |
| + " }\n" |
| "}"; |
| auto snippet = SnippetFromContentSuggestionJSON(kJsonStr); |
| ASSERT_THAT(snippet, NotNull()); |
| @@ -62,6 +68,11 @@ TEST(NTPSnippetTest, FromChromeContentSuggestionsDictionary) { |
| EXPECT_EQ(snippet->publisher_name(), "Foo News"); |
| EXPECT_EQ(snippet->url(), GURL("http://localhost/foobar")); |
| EXPECT_EQ(snippet->amp_url(), GURL("http://localhost/amp")); |
| + |
| + EXPECT_TRUE(snippet->should_notify()); |
| + auto notification_duration = |
| + snippet->notification_deadline() - snippet->publish_date(); |
| + EXPECT_EQ(7200.0f, notification_duration.InSecondsF()); |
| } |
| std::unique_ptr<NTPSnippet> SnippetFromChromeReaderDict( |
| @@ -399,5 +410,113 @@ TEST(NTPSnippetTest, CreateFromProtoToProtoRoundtrip) { |
| EXPECT_EQ(proto_serialized, round_tripped_serialized); |
| } |
| +std::unique_ptr<base::DictionaryValue> BaseJSON() { |
|
jkrcal
2017/01/04 07:34:32
nit: I am a bit lost in the various functions here
sfiera
2017/01/05 14:03:56
Multiple sources is a ChromeReader thing. This one
|
| + const std::string kJsonStr = |
| + "{" |
| + " \"ids\" : [\"http://localhost/foobar\"]," |
| + " \"title\" : \"Foo Barred from Baz\"," |
| + " \"snippet\" : \"...\"," |
| + " \"fullPageUrl\" : \"http://localhost/foobar\"," |
| + " \"creationTime\" : \"2016-06-30T11:01:37.000Z\"," |
| + " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," |
| + " \"attribution\" : \"Foo News\"," |
| + " \"imageUrl\" : \"http://localhost/foobar.jpg\"," |
| + " \"ampUrl\" : \"http://localhost/amp\"," |
| + " \"faviconUrl\" : \"http://localhost/favicon.ico\", " |
| + " \"score\": 9001\n" |
| + "}"; |
| + auto json_value = base::JSONReader::Read(kJsonStr); |
| + base::DictionaryValue* json_dict; |
| + CHECK(json_value->GetAsDictionary(&json_dict)); |
| + return json_dict->CreateDeepCopy(); |
| +} |
| + |
| +TEST(NTPSnippetTest, NotifcationInfoAllSpecified) { |
| + auto json = BaseJSON(); |
| + json->SetBoolean("notificationInfo.shouldNotify", true); |
| + json->SetString("notificationInfo.deadline", "2016-06-30T13:01:37.000Z"); |
| + auto snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*json, 0); |
| + EXPECT_TRUE(snippet->should_notify()); |
| + EXPECT_EQ(7200.0f, |
| + (snippet->notification_deadline() - snippet->publish_date()) |
| + .InSecondsF()); |
| +} |
| + |
| +TEST(NTPSnippetTest, NotificationInfoDeadlineInvalid) { |
| + auto json = BaseJSON(); |
| + json->SetBoolean("notificationInfo.shouldNotify", true); |
| + json->SetInteger("notificationInfo.notificationDeadline", 0); |
| + auto snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*json, 0); |
| + EXPECT_TRUE(snippet->should_notify()); |
| + EXPECT_EQ(base::Time::Max(), snippet->notification_deadline()); |
| +} |
| + |
| +TEST(NTPSnippetTest, NotificationInfoDeadlineAbsent) { |
| + auto json = BaseJSON(); |
| + json->SetBoolean("notificationInfo.shouldNotify", true); |
| + auto snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*json, 0); |
| + EXPECT_TRUE(snippet->should_notify()); |
| + EXPECT_EQ(base::Time::Max(), snippet->notification_deadline()); |
| +} |
| + |
| +TEST(NTPSnippetTest, NotificationInfoShouldNotifyInvalid) { |
| + auto json = BaseJSON(); |
| + json->SetString("notificationInfo.shouldNotify", "non-bool"); |
| + auto snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*json, 0); |
| + EXPECT_FALSE(snippet->should_notify()); |
| +} |
| + |
| +TEST(NTPSnippetTest, NotificationInfoAbsent) { |
| + auto json = BaseJSON(); |
| + auto snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*json, 0); |
| + EXPECT_FALSE(snippet->should_notify()); |
| +} |
| + |
| +TEST(NTPSnippetTest, ToContentSuggestion) { |
|
jkrcal
2017/01/04 07:34:32
Thanks for increasing test coverage! :)
|
| + auto json = BaseJSON(); |
| + auto snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*json, 0); |
| + ASSERT_THAT(snippet, NotNull()); |
| + ContentSuggestion sugg = snippet->ToContentSuggestion( |
| + Category::FromKnownCategory(KnownCategories::ARTICLES)); |
| + |
| + EXPECT_THAT(sugg.id().category(), |
| + Eq(Category::FromKnownCategory(KnownCategories::ARTICLES))); |
| + EXPECT_THAT(sugg.id().id_within_category(), Eq("http://localhost/foobar")); |
| + EXPECT_THAT(sugg.url(), Eq(GURL("http://localhost/amp"))); |
| + EXPECT_THAT(sugg.title(), Eq(base::UTF8ToUTF16("Foo Barred from Baz"))); |
| + EXPECT_THAT(sugg.snippet_text(), Eq(base::UTF8ToUTF16("..."))); |
| + EXPECT_THAT(sugg.publish_date().ToJavaTime(), Eq(1467284497000)); |
| + EXPECT_THAT(sugg.publisher_name(), Eq(base::UTF8ToUTF16("Foo News"))); |
| + EXPECT_THAT(sugg.score(), Eq(9001)); |
| + EXPECT_THAT(sugg.download_suggestion_extra(), IsNull()); |
| + EXPECT_THAT(sugg.recent_tab_suggestion_extra(), IsNull()); |
| + EXPECT_THAT(sugg.notification_extra(), IsNull()); |
| +} |
| + |
| +TEST(NTPSnippetTest, ToContentSuggestionWithNotificationInfo) { |
| + auto json = BaseJSON(); |
| + json->SetBoolean("notificationInfo.shouldNotify", true); |
| + json->SetString("notificationInfo.deadline", "2016-06-30T13:01:37.000Z"); |
| + auto snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*json, 0); |
| + ASSERT_THAT(snippet, NotNull()); |
| + ContentSuggestion sugg = snippet->ToContentSuggestion( |
| + Category::FromKnownCategory(KnownCategories::ARTICLES)); |
| + |
| + EXPECT_THAT(sugg.id().category(), |
| + Eq(Category::FromKnownCategory(KnownCategories::ARTICLES))); |
| + EXPECT_THAT(sugg.id().id_within_category(), Eq("http://localhost/foobar")); |
| + EXPECT_THAT(sugg.url(), Eq(GURL("http://localhost/amp"))); |
| + EXPECT_THAT(sugg.title(), Eq(base::UTF8ToUTF16("Foo Barred from Baz"))); |
| + EXPECT_THAT(sugg.snippet_text(), Eq(base::UTF8ToUTF16("..."))); |
| + EXPECT_THAT(sugg.publish_date().ToJavaTime(), Eq(1467284497000)); |
| + EXPECT_THAT(sugg.publisher_name(), Eq(base::UTF8ToUTF16("Foo News"))); |
| + EXPECT_THAT(sugg.score(), Eq(9001)); |
| + EXPECT_THAT(sugg.download_suggestion_extra(), IsNull()); |
| + EXPECT_THAT(sugg.recent_tab_suggestion_extra(), IsNull()); |
| + ASSERT_THAT(sugg.notification_extra(), NotNull()); |
| + EXPECT_THAT(sugg.notification_extra()->deadline.ToJavaTime(), |
| + Eq(1467291697000)); |
| +} |
| + |
| } // namespace |
| } // namespace ntp_snippets |