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

Side by Side Diff: chrome/browser/web_resource/promo_resource_service_mobile_ntp_unittest.cc

Issue 10860025: Remove promotion-type-specific JSON handling from NotificationPromo (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Comments. Created 8 years, 4 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
(Empty)
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.
4
5 #include <vector>
6
7 #include "base/json/json_reader.h"
8 #include "base/message_loop.h"
9 #include "base/string_number_conversions.h"
10 #include "base/time.h"
11 #include "base/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/prefs/browser_prefs.h"
15 #include "chrome/browser/prefs/pref_service.h"
16 #include "chrome/browser/web_resource/notification_promo.h"
17 #include "chrome/browser/web_resource/promo_resource_service.h"
18 #include "chrome/common/chrome_notification_types.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/test/base/testing_browser_process.h"
21 #include "chrome/test/base/testing_pref_service.h"
22 #include "chrome/test/base/testing_profile.h"
23 #include "content/public/browser/notification_registrar.h"
24 #include "content/public/browser/notification_service.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26
27 #include "chrome/browser/web_resource/notification_promo_mobile_ntp.h"
28
29 class PromoResourceServiceMobileNtpTest : public testing::Test {
30 public:
31 PromoResourceServiceMobileNtpTest()
32 : local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)),
33 web_resource_service_(new PromoResourceService(&profile_)) {
34 }
35
36 protected:
37 TestingProfile profile_;
38 ScopedTestingLocalState local_state_;
39 scoped_refptr<PromoResourceService> web_resource_service_;
40 MessageLoop loop_;
41 };
42
43 class NotificationPromoMobileNtpTest {
44 public:
45 explicit NotificationPromoMobileNtpTest(Profile* profile)
46 : profile_(profile),
47 prefs_(profile->GetPrefs()),
48 notification_promo_(profile),
49 received_notification_(false),
50 start_(0.0),
51 end_(0.0),
52 closed_(false) {
53 }
54
55 void Init(const std::string& json,
56 const std::string& promo_text,
57 const std::string& promo_text_long,
58 const std::string& promo_action_type,
59 const std::string& promo_action_arg0,
60 const std::string& promo_action_arg1,
61 double start, double end) {
62 Value* value(base::JSONReader::Read(json));
63 ASSERT_TRUE(value);
64 DictionaryValue* dict = NULL;
65 value->GetAsDictionary(&dict);
66 ASSERT_TRUE(dict);
67 test_json_.reset(dict);
68
69 promo_type_ = NotificationPromo::MOBILE_NTP_SYNC_PROMO;
70
71 promo_text_ = promo_text;
72
73 promo_text_long_ = promo_text_long;
74 promo_action_type_ = promo_action_type;
75 promo_action_args_.push_back(promo_action_arg0);
76 promo_action_args_.push_back(promo_action_arg1);
77
78 start_ = start;
79 end_ = end;
80
81
82 closed_ = false;
83 received_notification_ = false;
84 }
85
86 void InitPromoFromJson(bool should_receive_notification) {
87 notification_promo_.InitFromJson(*test_json_, promo_type_);
88 EXPECT_EQ(should_receive_notification,
89 notification_promo_.new_notification());
90
91 // Test the fields.
92 TestNotification();
93 }
94
95 void TestNotification() {
96 // Check values.
97 EXPECT_EQ(notification_promo_.promo_text_, promo_text_);
98
99 NotificationPromoMobileNtp mobile_promo(notification_promo_);
100 EXPECT_TRUE(mobile_promo.valid());
101 EXPECT_EQ(mobile_promo.text(), promo_text_);
102 EXPECT_EQ(mobile_promo.text_long(), promo_text_long_);
103 EXPECT_EQ(mobile_promo.action_type(), promo_action_type_);
104 EXPECT_TRUE(mobile_promo.action_args() != NULL);
105 EXPECT_EQ(std::size_t(2), promo_action_args_.size());
106 EXPECT_EQ(mobile_promo.action_args()->GetSize(),
107 promo_action_args_.size());
108 for (std::size_t i = 0; i < promo_action_args_.size(); ++i) {
109 std::string value;
110 EXPECT_TRUE(mobile_promo.action_args()->GetString(i, &value));
111 EXPECT_EQ(value, promo_action_args_[i]);
112 }
113
114 EXPECT_EQ(notification_promo_.start_, start_);
115 EXPECT_EQ(notification_promo_.end_, end_);
116 EXPECT_EQ(notification_promo_.closed_, closed_);
117 }
118
119 // Create a new NotificationPromo from prefs and compare to current
120 // notification.
121 void TestInitFromPrefs() {
122 NotificationPromo prefs_notification_promo(profile_);
123 prefs_notification_promo.InitFromPrefs(promo_type_);
124
125 EXPECT_EQ(notification_promo_.prefs_,
126 prefs_notification_promo.prefs_);
127 EXPECT_EQ(notification_promo_.promo_text_,
128 prefs_notification_promo.promo_text_);
129 EXPECT_EQ(notification_promo_.start_,
130 prefs_notification_promo.start_);
131 EXPECT_EQ(notification_promo_.end_,
132 prefs_notification_promo.end_);
133 EXPECT_EQ(notification_promo_.views_,
134 prefs_notification_promo.views_);
135 EXPECT_EQ(notification_promo_.closed_,
136 prefs_notification_promo.closed_);
137
138 NotificationPromoMobileNtp prefs_mobile_promo(notification_promo_);
139 EXPECT_TRUE(prefs_mobile_promo.valid());
140 NotificationPromoMobileNtp mobile_promo(prefs_notification_promo);
141 EXPECT_TRUE(mobile_promo.valid());
142 EXPECT_EQ(prefs_mobile_promo.text(),
143 mobile_promo.text());
144 EXPECT_EQ(prefs_mobile_promo.text_long(),
145 mobile_promo.text_long());
146 EXPECT_EQ(prefs_mobile_promo.action_type(),
147 mobile_promo.action_type());
148 EXPECT_TRUE(mobile_promo.action_args() != NULL);
149 EXPECT_EQ(prefs_mobile_promo.action_args()->GetSize(),
150 mobile_promo.action_args()->GetSize());
151 for (std::size_t i = 0;
152 i < prefs_mobile_promo.action_args()->GetSize();
153 ++i) {
154 std::string promo_value;
155 std::string prefs_value;
156 EXPECT_TRUE(
157 prefs_mobile_promo.action_args()->GetString(i, &promo_value));
158 EXPECT_TRUE(
159 mobile_promo.action_args()->GetString(
160 i, &prefs_value));
161 EXPECT_EQ(promo_value, prefs_value);
162 }
163 }
164
165 void TestClosed() {
166 NotificationPromo new_promo(profile_);
167 new_promo.InitFromPrefs(promo_type_);
168 EXPECT_FALSE(new_promo.closed_);
169 EXPECT_TRUE(new_promo.CanShow());
170
171 NotificationPromo::HandleClosed(profile_, promo_type_);
172 new_promo.InitFromPrefs(promo_type_);
173 EXPECT_TRUE(new_promo.closed_);
174 EXPECT_FALSE(new_promo.CanShow());
175
176 new_promo.closed_ = false;
177 EXPECT_TRUE(new_promo.CanShow());
178 new_promo.WritePrefs();
179 }
180
181 void TestPromoText() {
182 notification_promo_.promo_text_.clear();
183 EXPECT_FALSE(notification_promo_.CanShow());
184
185 notification_promo_.promo_text_ = promo_text_;
186 EXPECT_TRUE(notification_promo_.CanShow());
187 }
188
189 private:
190 Profile* profile_;
191 PrefService* prefs_;
192 NotificationPromo notification_promo_;
193 bool received_notification_;
194 scoped_ptr<DictionaryValue> test_json_;
195
196 NotificationPromo::PromoType promo_type_;
197 std::string promo_text_;
198 std::string promo_text_long_;
199 std::string promo_action_type_;
200 std::vector<std::string> promo_action_args_;
201
202 double start_;
203 double end_;
204 bool closed_;
205 };
206
207 TEST_F(PromoResourceServiceMobileNtpTest, NotificationPromoMobileNtpTest) {
208 // Check that prefs are set correctly.
209 PrefService* prefs = profile_.GetPrefs();
210 ASSERT_TRUE(prefs != NULL);
211
212 NotificationPromoMobileNtpTest promo_test(&profile_);
213
214 // Set up start and end dates and promo line in a Dictionary as if parsed
215 // from the service.
216 promo_test.Init(
217 "{"
218 " \"mobile_ntp_sync_promo\": ["
219 " {"
220 " \"date\":"
221 " ["
222 " {"
223 " \"start\":\"3 Aug 1999 9:26:06 GMT\","
224 " \"end\":\"7 Jan 2013 5:40:75 PST\""
225 " }"
226 " ],"
227 " \"strings\":"
228 " {"
229 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\":"
230 " \"Like Chrome? Go http://www.google.com/chrome/\","
231 " \"MOBILE_PROMO_CHROME_LONG_TEXT\":"
232 " \"It\'s simple. Go http://www.google.com/chrome/\","
233 " \"MOBILE_PROMO_EMAIL_BODY\":\"This is the body.\","
234 " \"XXX\":\"XXX value\""
235 " },"
236 " \"payload\":"
237 " {"
238 " \"payload_format_version\":3,"
239 " \"gplus_required\":false,"
240 " \"promo_message_long\":"
241 " \"MOBILE_PROMO_CHROME_LONG_TEXT\","
242 " \"promo_message_short\":"
243 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\","
244 " \"promo_action_type\":\"ACTION_EMAIL\","
245 " \"promo_action_args\":[\"MOBILE_PROMO_EMAIL_BODY\",\"XXX\"]"
246 " },"
247 " \"max_views\":30"
248 " }"
249 " ]"
250 "}",
251 "Like Chrome? Go http://www.google.com/chrome/",
252 "It\'s simple. Go http://www.google.com/chrome/",
253 "ACTION_EMAIL", "This is the body.", "XXX value",
254 // We hardcode the unix epoch time to make sure our parsing works.
255 // The starting date is in 1999 to make tests pass on Android devices
256 // with incorrect or unset date/time.
257 933672366, // unix epoch for 3 Aug 1999 9:26:06 GMT.
258 1357566075); // unix epoch for 7 Jan 2013 5:40:75 PST.
259
260 promo_test.InitPromoFromJson(true);
261
262 // Second time should not trigger a notification.
263 promo_test.InitPromoFromJson(false);
264
265 promo_test.TestInitFromPrefs();
266
267 // Test various conditions of CanShow.
268 promo_test.TestClosed();
269 promo_test.TestPromoText();
270 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698