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

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

Issue 2616543002: Expose notification info in ContentSuggestion (Closed)
Patch Set: Created 3 years, 11 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/ntp_snippet.h" 5 #include "components/ntp_snippets/remote/ntp_snippet.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/values.h" 12 #include "base/values.h"
12 #include "components/ntp_snippets/remote/proto/ntp_snippets.pb.h" 13 #include "components/ntp_snippets/remote/proto/ntp_snippets.pb.h"
13 #include "testing/gmock/include/gmock/gmock.h" 14 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
15 16
16 namespace ntp_snippets { 17 namespace ntp_snippets {
17 namespace { 18 namespace {
18 19
19 using ::testing::ElementsAre; 20 using ::testing::ElementsAre;
21 using ::testing::Eq;
20 using ::testing::IsNull; 22 using ::testing::IsNull;
21 using ::testing::NotNull; 23 using ::testing::NotNull;
22 24
23 std::unique_ptr<NTPSnippet> SnippetFromContentSuggestionJSON( 25 std::unique_ptr<NTPSnippet> SnippetFromContentSuggestionJSON(
24 const std::string& json) { 26 const std::string& json) {
25 auto json_value = base::JSONReader::Read(json); 27 auto json_value = base::JSONReader::Read(json);
26 base::DictionaryValue* json_dict; 28 base::DictionaryValue* json_dict;
27 if (!json_value->GetAsDictionary(&json_dict)) { 29 if (!json_value->GetAsDictionary(&json_dict)) {
28 return nullptr; 30 return nullptr;
29 } 31 }
30 return NTPSnippet::CreateFromContentSuggestionsDictionary(*json_dict, 32 return NTPSnippet::CreateFromContentSuggestionsDictionary(*json_dict,
31 kArticlesRemoteId); 33 kArticlesRemoteId);
32 } 34 }
33 35
34 TEST(NTPSnippetTest, FromChromeContentSuggestionsDictionary) { 36 TEST(NTPSnippetTest, FromChromeContentSuggestionsDictionary) {
35 const std::string kJsonStr = 37 const std::string kJsonStr =
36 "{" 38 "{"
37 " \"ids\" : [\"http://localhost/foobar\"]," 39 " \"ids\" : [\"http://localhost/foobar\"],"
38 " \"title\" : \"Foo Barred from Baz\"," 40 " \"title\" : \"Foo Barred from Baz\","
39 " \"snippet\" : \"...\"," 41 " \"snippet\" : \"...\","
40 " \"fullPageUrl\" : \"http://localhost/foobar\"," 42 " \"fullPageUrl\" : \"http://localhost/foobar\","
41 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\"," 43 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\","
42 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\"," 44 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\","
43 " \"attribution\" : \"Foo News\"," 45 " \"attribution\" : \"Foo News\","
44 " \"imageUrl\" : \"http://localhost/foobar.jpg\"," 46 " \"imageUrl\" : \"http://localhost/foobar.jpg\","
45 " \"ampUrl\" : \"http://localhost/amp\"," 47 " \"ampUrl\" : \"http://localhost/amp\","
46 " \"faviconUrl\" : \"http://localhost/favicon.ico\", " 48 " \"faviconUrl\" : \"http://localhost/favicon.ico\", "
47 " \"score\": 9001\n" 49 " \"score\": 9001,\n"
50 " \"notificationInfo\": {\n"
51 " \"shouldNotify\": true,"
52 " \"deadline\": \"2016-06-30T13:01:37.000Z\"\n"
53 " }\n"
48 "}"; 54 "}";
49 auto snippet = SnippetFromContentSuggestionJSON(kJsonStr); 55 auto snippet = SnippetFromContentSuggestionJSON(kJsonStr);
50 ASSERT_THAT(snippet, NotNull()); 56 ASSERT_THAT(snippet, NotNull());
51 57
52 EXPECT_EQ(snippet->id(), "http://localhost/foobar"); 58 EXPECT_EQ(snippet->id(), "http://localhost/foobar");
53 EXPECT_EQ(snippet->title(), "Foo Barred from Baz"); 59 EXPECT_EQ(snippet->title(), "Foo Barred from Baz");
54 EXPECT_EQ(snippet->snippet(), "..."); 60 EXPECT_EQ(snippet->snippet(), "...");
55 EXPECT_EQ(snippet->salient_image_url(), GURL("http://localhost/foobar.jpg")); 61 EXPECT_EQ(snippet->salient_image_url(), GURL("http://localhost/foobar.jpg"));
56 EXPECT_EQ(snippet->score(), 9001); 62 EXPECT_EQ(snippet->score(), 9001);
57 auto unix_publish_date = snippet->publish_date() - base::Time::UnixEpoch(); 63 auto unix_publish_date = snippet->publish_date() - base::Time::UnixEpoch();
58 auto expiry_duration = snippet->expiry_date() - snippet->publish_date(); 64 auto expiry_duration = snippet->expiry_date() - snippet->publish_date();
59 EXPECT_FLOAT_EQ(unix_publish_date.InSecondsF(), 1467284497.000000f); 65 EXPECT_FLOAT_EQ(unix_publish_date.InSecondsF(), 1467284497.000000f);
60 EXPECT_FLOAT_EQ(expiry_duration.InSecondsF(), 86400.000000f); 66 EXPECT_FLOAT_EQ(expiry_duration.InSecondsF(), 86400.000000f);
61 67
62 EXPECT_EQ(snippet->publisher_name(), "Foo News"); 68 EXPECT_EQ(snippet->publisher_name(), "Foo News");
63 EXPECT_EQ(snippet->url(), GURL("http://localhost/foobar")); 69 EXPECT_EQ(snippet->url(), GURL("http://localhost/foobar"));
64 EXPECT_EQ(snippet->amp_url(), GURL("http://localhost/amp")); 70 EXPECT_EQ(snippet->amp_url(), GURL("http://localhost/amp"));
71
72 EXPECT_TRUE(snippet->should_notify());
73 auto notification_duration =
74 snippet->notification_deadline() - snippet->publish_date();
75 EXPECT_EQ(7200.0f, notification_duration.InSecondsF());
65 } 76 }
66 77
67 std::unique_ptr<NTPSnippet> SnippetFromChromeReaderDict( 78 std::unique_ptr<NTPSnippet> SnippetFromChromeReaderDict(
68 std::unique_ptr<base::DictionaryValue> dict) { 79 std::unique_ptr<base::DictionaryValue> dict) {
69 if (!dict) { 80 if (!dict) {
70 return nullptr; 81 return nullptr;
71 } 82 }
72 return NTPSnippet::CreateFromChromeReaderDictionary(*dict); 83 return NTPSnippet::CreateFromChromeReaderDictionary(*dict);
73 } 84 }
74 85
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 // Unfortunately, we only have MessageLite protocol buffers in Chrome, so 403 // Unfortunately, we only have MessageLite protocol buffers in Chrome, so
393 // comparing via DebugString() or MessageDifferencer is not working. 404 // comparing via DebugString() or MessageDifferencer is not working.
394 // So we either need to compare field-by-field (maintenance heavy) or 405 // So we either need to compare field-by-field (maintenance heavy) or
395 // compare the binary version (unusable diagnostic). Deciding for the latter. 406 // compare the binary version (unusable diagnostic). Deciding for the latter.
396 std::string proto_serialized, round_tripped_serialized; 407 std::string proto_serialized, round_tripped_serialized;
397 proto.SerializeToString(&proto_serialized); 408 proto.SerializeToString(&proto_serialized);
398 snippet->ToProto().SerializeToString(&round_tripped_serialized); 409 snippet->ToProto().SerializeToString(&round_tripped_serialized);
399 EXPECT_EQ(proto_serialized, round_tripped_serialized); 410 EXPECT_EQ(proto_serialized, round_tripped_serialized);
400 } 411 }
401 412
413 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
414 const std::string kJsonStr =
415 "{"
416 " \"ids\" : [\"http://localhost/foobar\"],"
417 " \"title\" : \"Foo Barred from Baz\","
418 " \"snippet\" : \"...\","
419 " \"fullPageUrl\" : \"http://localhost/foobar\","
420 " \"creationTime\" : \"2016-06-30T11:01:37.000Z\","
421 " \"expirationTime\" : \"2016-07-01T11:01:37.000Z\","
422 " \"attribution\" : \"Foo News\","
423 " \"imageUrl\" : \"http://localhost/foobar.jpg\","
424 " \"ampUrl\" : \"http://localhost/amp\","
425 " \"faviconUrl\" : \"http://localhost/favicon.ico\", "
426 " \"score\": 9001\n"
427 "}";
428 auto json_value = base::JSONReader::Read(kJsonStr);
429 base::DictionaryValue* json_dict;
430 CHECK(json_value->GetAsDictionary(&json_dict));
431 return json_dict->CreateDeepCopy();
432 }
433
434 TEST(NTPSnippetTest, NotifcationInfoAllSpecified) {
435 auto json = BaseJSON();
436 json->SetBoolean("notificationInfo.shouldNotify", true);
437 json->SetString("notificationInfo.deadline", "2016-06-30T13:01:37.000Z");
438 auto snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*json, 0);
439 EXPECT_TRUE(snippet->should_notify());
440 EXPECT_EQ(7200.0f,
441 (snippet->notification_deadline() - snippet->publish_date())
442 .InSecondsF());
443 }
444
445 TEST(NTPSnippetTest, NotificationInfoDeadlineInvalid) {
446 auto json = BaseJSON();
447 json->SetBoolean("notificationInfo.shouldNotify", true);
448 json->SetInteger("notificationInfo.notificationDeadline", 0);
449 auto snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*json, 0);
450 EXPECT_TRUE(snippet->should_notify());
451 EXPECT_EQ(base::Time::Max(), snippet->notification_deadline());
452 }
453
454 TEST(NTPSnippetTest, NotificationInfoDeadlineAbsent) {
455 auto json = BaseJSON();
456 json->SetBoolean("notificationInfo.shouldNotify", true);
457 auto snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*json, 0);
458 EXPECT_TRUE(snippet->should_notify());
459 EXPECT_EQ(base::Time::Max(), snippet->notification_deadline());
460 }
461
462 TEST(NTPSnippetTest, NotificationInfoShouldNotifyInvalid) {
463 auto json = BaseJSON();
464 json->SetString("notificationInfo.shouldNotify", "non-bool");
465 auto snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*json, 0);
466 EXPECT_FALSE(snippet->should_notify());
467 }
468
469 TEST(NTPSnippetTest, NotificationInfoAbsent) {
470 auto json = BaseJSON();
471 auto snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*json, 0);
472 EXPECT_FALSE(snippet->should_notify());
473 }
474
475 TEST(NTPSnippetTest, ToContentSuggestion) {
jkrcal 2017/01/04 07:34:32 Thanks for increasing test coverage! :)
476 auto json = BaseJSON();
477 auto snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*json, 0);
478 ASSERT_THAT(snippet, NotNull());
479 ContentSuggestion sugg = snippet->ToContentSuggestion(
480 Category::FromKnownCategory(KnownCategories::ARTICLES));
481
482 EXPECT_THAT(sugg.id().category(),
483 Eq(Category::FromKnownCategory(KnownCategories::ARTICLES)));
484 EXPECT_THAT(sugg.id().id_within_category(), Eq("http://localhost/foobar"));
485 EXPECT_THAT(sugg.url(), Eq(GURL("http://localhost/amp")));
486 EXPECT_THAT(sugg.title(), Eq(base::UTF8ToUTF16("Foo Barred from Baz")));
487 EXPECT_THAT(sugg.snippet_text(), Eq(base::UTF8ToUTF16("...")));
488 EXPECT_THAT(sugg.publish_date().ToJavaTime(), Eq(1467284497000));
489 EXPECT_THAT(sugg.publisher_name(), Eq(base::UTF8ToUTF16("Foo News")));
490 EXPECT_THAT(sugg.score(), Eq(9001));
491 EXPECT_THAT(sugg.download_suggestion_extra(), IsNull());
492 EXPECT_THAT(sugg.recent_tab_suggestion_extra(), IsNull());
493 EXPECT_THAT(sugg.notification_extra(), IsNull());
494 }
495
496 TEST(NTPSnippetTest, ToContentSuggestionWithNotificationInfo) {
497 auto json = BaseJSON();
498 json->SetBoolean("notificationInfo.shouldNotify", true);
499 json->SetString("notificationInfo.deadline", "2016-06-30T13:01:37.000Z");
500 auto snippet = NTPSnippet::CreateFromContentSuggestionsDictionary(*json, 0);
501 ASSERT_THAT(snippet, NotNull());
502 ContentSuggestion sugg = snippet->ToContentSuggestion(
503 Category::FromKnownCategory(KnownCategories::ARTICLES));
504
505 EXPECT_THAT(sugg.id().category(),
506 Eq(Category::FromKnownCategory(KnownCategories::ARTICLES)));
507 EXPECT_THAT(sugg.id().id_within_category(), Eq("http://localhost/foobar"));
508 EXPECT_THAT(sugg.url(), Eq(GURL("http://localhost/amp")));
509 EXPECT_THAT(sugg.title(), Eq(base::UTF8ToUTF16("Foo Barred from Baz")));
510 EXPECT_THAT(sugg.snippet_text(), Eq(base::UTF8ToUTF16("...")));
511 EXPECT_THAT(sugg.publish_date().ToJavaTime(), Eq(1467284497000));
512 EXPECT_THAT(sugg.publisher_name(), Eq(base::UTF8ToUTF16("Foo News")));
513 EXPECT_THAT(sugg.score(), Eq(9001));
514 EXPECT_THAT(sugg.download_suggestion_extra(), IsNull());
515 EXPECT_THAT(sugg.recent_tab_suggestion_extra(), IsNull());
516 ASSERT_THAT(sugg.notification_extra(), NotNull());
517 EXPECT_THAT(sugg.notification_extra()->deadline.ToJavaTime(),
518 Eq(1467291697000));
519 }
520
402 } // namespace 521 } // namespace
403 } // namespace ntp_snippets 522 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698