1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "base/json/json_reader.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/prefs/testing_pref_service.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "base/values.h"
14 #include "components/version_info/version_info.h"
15 #include "components/web_resource/notification_promo.h"
16 #include "components/web_resource/notification_promo_mobile_ntp.h"
17 #include "components/web_resource/promo_resource_service.h"
18 #include "testing/gtest/include/gtest/gtest.h"
21 version_info::Channel kChannel
= version_info::Channel::UNKNOWN
;
24 namespace web_resource
{
26 class PromoResourceServiceMobileNtpTest
: public testing::Test
{
28 PromoResourceServiceMobileNtpTest() {
29 NotificationPromo::RegisterPrefs(local_state_
.registry());
30 promo_resource_service_
.reset(new PromoResourceService(
31 &local_state_
, kChannel
, "en", nullptr, nullptr,
32 web_resource::WebResourceService::ParseJSONCallback()));
36 TestingPrefServiceSimple local_state_
;
37 // |promo_resource_service_| must be created after |local_state_|.
38 scoped_ptr
<PromoResourceService
> promo_resource_service_
;
39 base::MessageLoop loop_
;
42 class NotificationPromoMobileNtpTest
{
44 explicit NotificationPromoMobileNtpTest(PrefService
* local_state
)
45 : local_state_(local_state
),
46 mobile_promo_(local_state
),
47 received_notification_(false) {}
49 void Init(const std::string
& json
,
50 const std::string
& promo_text
,
51 const std::string
& promo_text_long
,
52 const std::string
& promo_action_type
,
53 const std::string
& promo_action_arg0
,
54 const std::string
& promo_action_arg1
) {
55 scoped_ptr
<base::Value
> value
= base::JSONReader::Read(json
);
57 base::DictionaryValue
* dict
= NULL
;
58 value
.release()->GetAsDictionary(&dict
);
60 test_json_
.reset(dict
);
62 promo_text_
= promo_text
;
63 promo_text_long_
= promo_text_long
;
64 promo_action_type_
= promo_action_type
;
65 promo_action_args_
.push_back(promo_action_arg0
);
66 promo_action_args_
.push_back(promo_action_arg1
);
68 received_notification_
= false;
71 void InitPromoFromJson(bool should_receive_notification
) {
72 EXPECT_TRUE(mobile_promo_
.InitFromJson(*test_json_
));
73 EXPECT_TRUE(mobile_promo_
.valid());
74 EXPECT_EQ(should_receive_notification
,
75 mobile_promo_
.notification_promo().new_notification());
81 void TestNotification() {
83 EXPECT_TRUE(mobile_promo_
.valid());
84 EXPECT_EQ(mobile_promo_
.text(), promo_text_
);
85 EXPECT_EQ(mobile_promo_
.text_long(), promo_text_long_
);
86 EXPECT_EQ(mobile_promo_
.action_type(), promo_action_type_
);
87 EXPECT_TRUE(mobile_promo_
.action_args() != NULL
);
88 EXPECT_EQ(2u, promo_action_args_
.size());
89 EXPECT_EQ(mobile_promo_
.action_args()->GetSize(),
90 promo_action_args_
.size());
91 for (std::size_t i
= 0; i
< promo_action_args_
.size(); ++i
) {
93 EXPECT_TRUE(mobile_promo_
.action_args()->GetString(i
, &value
));
94 EXPECT_EQ(value
, promo_action_args_
[i
]);
98 // Create a new NotificationPromo from prefs and compare to current
100 void TestInitFromPrefs() {
101 NotificationPromoMobileNtp
prefs_mobile_promo(local_state_
);
102 EXPECT_TRUE(prefs_mobile_promo
.InitFromPrefs());
103 EXPECT_TRUE(prefs_mobile_promo
.valid());
104 EXPECT_TRUE(mobile_promo_
.valid());
106 EXPECT_EQ(prefs_mobile_promo
.text(), mobile_promo_
.text());
107 EXPECT_EQ(prefs_mobile_promo
.text_long(), mobile_promo_
.text_long());
108 EXPECT_EQ(prefs_mobile_promo
.action_type(), mobile_promo_
.action_type());
109 EXPECT_TRUE(mobile_promo_
.action_args() != NULL
);
110 EXPECT_EQ(prefs_mobile_promo
.action_args()->GetSize(),
111 mobile_promo_
.action_args()->GetSize());
112 for (std::size_t i
= 0; i
< prefs_mobile_promo
.action_args()->GetSize();
114 std::string promo_value
;
115 std::string prefs_value
;
116 EXPECT_TRUE(prefs_mobile_promo
.action_args()->GetString(i
, &prefs_value
));
117 EXPECT_TRUE(mobile_promo_
.action_args()->GetString(i
, &promo_value
));
118 EXPECT_EQ(promo_value
, prefs_value
);
123 PrefService
* local_state_
;
124 NotificationPromoMobileNtp mobile_promo_
;
125 bool received_notification_
;
126 scoped_ptr
<base::DictionaryValue
> test_json_
;
128 std::string promo_text_
;
129 std::string promo_text_long_
;
130 std::string promo_action_type_
;
131 std::vector
<std::string
> promo_action_args_
;
134 TEST_F(PromoResourceServiceMobileNtpTest
, NotificationPromoMobileNtpTest
) {
135 NotificationPromoMobileNtpTest
promo_test(&local_state_
);
137 // Set up start and end dates and promo line in a Dictionary as if parsed
141 " \"mobile_ntp_sync_promo\": ["
146 " \"start\":\"3 Aug 1999 9:26:06 GMT\","
147 " \"end\":\"7 Jan 2013 5:40:75 PST\""
152 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\":"
153 " \"Like Chrome? Go http://www.google.com/chrome/\","
154 " \"MOBILE_PROMO_CHROME_LONG_TEXT\":"
155 " \"It's simple. Go http://www.google.com/chrome/\","
156 " \"MOBILE_PROMO_EMAIL_BODY\":\"This is the body.\","
157 " \"XXX\":\"XXX value\""
161 " \"payload_format_version\":3,"
162 " \"promo_message_long\":"
163 " \"MOBILE_PROMO_CHROME_LONG_TEXT\","
164 " \"promo_message_short\":"
165 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\","
166 " \"promo_action_type\":\"ACTION_EMAIL\","
167 " \"promo_action_args\":[\"MOBILE_PROMO_EMAIL_BODY\",\"XXX\"]"
173 "Like Chrome? Go http://www.google.com/chrome/",
174 "It\'s simple. Go http://www.google.com/chrome/", "ACTION_EMAIL",
175 "This is the body.", "XXX value");
177 promo_test
.InitPromoFromJson(true);
179 // Second time should not trigger a notification.
180 promo_test
.InitPromoFromJson(false);
182 promo_test
.TestInitFromPrefs();
185 } // namespace web_resource