Inject JSON parsing in WebResource as a callback rather than inheritance
[chromium-blink-merge.git] / chrome / browser / web_resource / promo_resource_service_mobile_ntp_unittest.cc
blobc88d9e7a95bcf0eb2f8339a0a09191be6597f188
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.
5 #include <vector>
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"
20 namespace {
21 version_info::Channel kChannel = version_info::Channel::UNKNOWN;
24 class PromoResourceServiceMobileNtpTest : public testing::Test {
25 public:
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()));
33 protected:
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 {
41 public:
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));
54 ASSERT_TRUE(value);
55 base::DictionaryValue* dict = NULL;
56 value->GetAsDictionary(&dict);
57 ASSERT_TRUE(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());
75 // Test the fields.
76 TestNotification();
79 void TestNotification() {
80 // Check values.
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) {
90 std::string value;
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
97 // notification.
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();
115 ++i) {
116 std::string promo_value;
117 std::string prefs_value;
118 EXPECT_TRUE(
119 prefs_mobile_promo.action_args()->GetString(i, &prefs_value));
120 EXPECT_TRUE(
121 mobile_promo_.action_args()->GetString(i, &promo_value));
122 EXPECT_EQ(promo_value, prefs_value);
126 private:
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
142 // from the service.
143 promo_test.Init(
145 " \"mobile_ntp_sync_promo\": ["
146 " {"
147 " \"date\":"
148 " ["
149 " {"
150 " \"start\":\"3 Aug 1999 9:26:06 GMT\","
151 " \"end\":\"7 Jan 2013 5:40:75 PST\""
152 " }"
153 " ],"
154 " \"strings\":"
155 " {"
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\""
162 " },"
163 " \"payload\":"
164 " {"
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\"]"
172 " },"
173 " \"max_views\":30"
174 " }"
175 " ]"
176 "}",
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();