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 "chrome/browser/web_resource/notification_promo.h"
15 #include "chrome/browser/web_resource/notification_promo_mobile_ntp.h"
16 #include "chrome/browser/web_resource/promo_resource_service.h"
17 #include "components/version_info/version_info.h"
18 #include "testing/gtest/include/gtest/gtest.h"
21 version_info::Channel kChannel
= version_info::Channel::UNKNOWN
;
24 class PromoResourceServiceMobileNtpTest
: public testing::Test
{
26 PromoResourceServiceMobileNtpTest() {
27 NotificationPromo::RegisterPrefs(local_state_
.registry());
28 promo_resource_service_
.reset(new PromoResourceService(
29 &local_state_
, kChannel
, "en", nullptr, nullptr,
30 web_resource::WebResourceService::ParseJSONCallback()));
34 TestingPrefServiceSimple local_state_
;
35 // |promo_resource_service_| must be created after |local_state_|.
36 scoped_ptr
<PromoResourceService
> promo_resource_service_
;
37 base::MessageLoop loop_
;
40 class NotificationPromoMobileNtpTest
{
42 explicit NotificationPromoMobileNtpTest(PrefService
* local_state
)
43 : local_state_(local_state
),
44 mobile_promo_(local_state
),
45 received_notification_(false) {}
47 void Init(const std::string
& json
,
48 const std::string
& promo_text
,
49 const std::string
& promo_text_long
,
50 const std::string
& promo_action_type
,
51 const std::string
& promo_action_arg0
,
52 const std::string
& promo_action_arg1
) {
53 base::Value
* value(base::JSONReader::DeprecatedRead(json
));
55 base::DictionaryValue
* dict
= NULL
;
56 value
->GetAsDictionary(&dict
);
58 test_json_
.reset(dict
);
60 promo_text_
= promo_text
;
61 promo_text_long_
= promo_text_long
;
62 promo_action_type_
= promo_action_type
;
63 promo_action_args_
.push_back(promo_action_arg0
);
64 promo_action_args_
.push_back(promo_action_arg1
);
66 received_notification_
= false;
69 void InitPromoFromJson(bool should_receive_notification
) {
70 EXPECT_TRUE(mobile_promo_
.InitFromJson(*test_json_
));
71 EXPECT_TRUE(mobile_promo_
.valid());
72 EXPECT_EQ(should_receive_notification
,
73 mobile_promo_
.notification_promo().new_notification());
79 void TestNotification() {
81 EXPECT_TRUE(mobile_promo_
.valid());
82 EXPECT_EQ(mobile_promo_
.text(), promo_text_
);
83 EXPECT_EQ(mobile_promo_
.text_long(), promo_text_long_
);
84 EXPECT_EQ(mobile_promo_
.action_type(), promo_action_type_
);
85 EXPECT_TRUE(mobile_promo_
.action_args() != NULL
);
86 EXPECT_EQ(2u, promo_action_args_
.size());
87 EXPECT_EQ(mobile_promo_
.action_args()->GetSize(),
88 promo_action_args_
.size());
89 for (std::size_t i
= 0; i
< promo_action_args_
.size(); ++i
) {
91 EXPECT_TRUE(mobile_promo_
.action_args()->GetString(i
, &value
));
92 EXPECT_EQ(value
, promo_action_args_
[i
]);
96 // Create a new NotificationPromo from prefs and compare to current
98 void TestInitFromPrefs() {
99 NotificationPromoMobileNtp
prefs_mobile_promo(local_state_
);
100 EXPECT_TRUE(prefs_mobile_promo
.InitFromPrefs());
101 EXPECT_TRUE(prefs_mobile_promo
.valid());
102 EXPECT_TRUE(mobile_promo_
.valid());
104 EXPECT_EQ(prefs_mobile_promo
.text(),
105 mobile_promo_
.text());
106 EXPECT_EQ(prefs_mobile_promo
.text_long(),
107 mobile_promo_
.text_long());
108 EXPECT_EQ(prefs_mobile_promo
.action_type(),
109 mobile_promo_
.action_type());
110 EXPECT_TRUE(mobile_promo_
.action_args() != NULL
);
111 EXPECT_EQ(prefs_mobile_promo
.action_args()->GetSize(),
112 mobile_promo_
.action_args()->GetSize());
113 for (std::size_t i
= 0;
114 i
< prefs_mobile_promo
.action_args()->GetSize();
116 std::string promo_value
;
117 std::string prefs_value
;
119 prefs_mobile_promo
.action_args()->GetString(i
, &prefs_value
));
121 mobile_promo_
.action_args()->GetString(i
, &promo_value
));
122 EXPECT_EQ(promo_value
, prefs_value
);
127 PrefService
* local_state_
;
128 NotificationPromoMobileNtp mobile_promo_
;
129 bool received_notification_
;
130 scoped_ptr
<base::DictionaryValue
> test_json_
;
132 std::string promo_text_
;
133 std::string promo_text_long_
;
134 std::string promo_action_type_
;
135 std::vector
<std::string
> promo_action_args_
;
138 TEST_F(PromoResourceServiceMobileNtpTest
, NotificationPromoMobileNtpTest
) {
139 NotificationPromoMobileNtpTest
promo_test(&local_state_
);
141 // Set up start and end dates and promo line in a Dictionary as if parsed
145 " \"mobile_ntp_sync_promo\": ["
150 " \"start\":\"3 Aug 1999 9:26:06 GMT\","
151 " \"end\":\"7 Jan 2013 5:40:75 PST\""
156 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\":"
157 " \"Like Chrome? Go http://www.google.com/chrome/\","
158 " \"MOBILE_PROMO_CHROME_LONG_TEXT\":"
159 " \"It's simple. Go http://www.google.com/chrome/\","
160 " \"MOBILE_PROMO_EMAIL_BODY\":\"This is the body.\","
161 " \"XXX\":\"XXX value\""
165 " \"payload_format_version\":3,"
166 " \"promo_message_long\":"
167 " \"MOBILE_PROMO_CHROME_LONG_TEXT\","
168 " \"promo_message_short\":"
169 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\","
170 " \"promo_action_type\":\"ACTION_EMAIL\","
171 " \"promo_action_args\":[\"MOBILE_PROMO_EMAIL_BODY\",\"XXX\"]"
177 "Like Chrome? Go http://www.google.com/chrome/",
178 "It\'s simple. Go http://www.google.com/chrome/",
179 "ACTION_EMAIL", "This is the body.", "XXX value");
181 promo_test
.InitPromoFromJson(true);
183 // Second time should not trigger a notification.
184 promo_test
.InitPromoFromJson(false);
186 promo_test
.TestInitFromPrefs();