OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <vector> | 5 #include <vector> |
6 | 6 |
7 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/time.h" | 10 #include "base/time.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/prefs/browser_prefs.h" | 14 #include "chrome/browser/prefs/browser_prefs.h" |
15 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
16 #include "chrome/browser/web_resource/notification_promo.h" | 16 #include "chrome/browser/web_resource/notification_promo.h" |
17 #include "chrome/browser/web_resource/promo_resource_service.h" | 17 #include "chrome/browser/web_resource/promo_resource_service.h" |
18 #include "chrome/common/chrome_notification_types.h" | 18 #include "chrome/common/chrome_notification_types.h" |
19 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
20 #include "chrome/test/base/testing_browser_process.h" | 20 #include "chrome/test/base/testing_browser_process.h" |
21 #include "chrome/test/base/testing_pref_service.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" | 22 #include "content/public/browser/notification_registrar.h" |
24 #include "content/public/browser/notification_service.h" | 23 #include "content/public/browser/notification_service.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
26 | 25 |
27 #include "chrome/browser/web_resource/notification_promo_mobile_ntp.h" | 26 #include "chrome/browser/web_resource/notification_promo_mobile_ntp.h" |
28 | 27 |
29 class PromoResourceServiceMobileNtpTest : public testing::Test { | 28 class PromoResourceServiceMobileNtpTest : public testing::Test { |
30 public: | 29 public: |
31 PromoResourceServiceMobileNtpTest() | 30 PromoResourceServiceMobileNtpTest() |
32 : local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)), | 31 : local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)), |
33 web_resource_service_(new PromoResourceService(&profile_)) { | 32 web_resource_service_(new PromoResourceService(local_state_.Get())) { |
34 } | 33 } |
35 | 34 |
36 protected: | 35 protected: |
37 TestingProfile profile_; | |
38 ScopedTestingLocalState local_state_; | 36 ScopedTestingLocalState local_state_; |
39 scoped_refptr<PromoResourceService> web_resource_service_; | 37 scoped_refptr<PromoResourceService> web_resource_service_; |
40 MessageLoop loop_; | 38 MessageLoop loop_; |
41 }; | 39 }; |
42 | 40 |
43 class NotificationPromoMobileNtpTest { | 41 class NotificationPromoMobileNtpTest { |
44 public: | 42 public: |
45 explicit NotificationPromoMobileNtpTest(Profile* profile) | 43 explicit NotificationPromoMobileNtpTest(PrefServiceSimple* prefs) |
46 : profile_(profile), | 44 : prefs_(prefs), |
47 prefs_(profile->GetPrefs()), | 45 mobile_promo_(prefs), |
48 mobile_promo_(profile), | |
49 received_notification_(false) { | 46 received_notification_(false) { |
50 } | 47 } |
51 | 48 |
52 void Init(const std::string& json, | 49 void Init(const std::string& json, |
53 const std::string& promo_text, | 50 const std::string& promo_text, |
54 const std::string& promo_text_long, | 51 const std::string& promo_text_long, |
55 const std::string& promo_action_type, | 52 const std::string& promo_action_type, |
56 const std::string& promo_action_arg0, | 53 const std::string& promo_action_arg0, |
57 const std::string& promo_action_arg1) { | 54 const std::string& promo_action_arg1) { |
58 Value* value(base::JSONReader::Read(json)); | 55 Value* value(base::JSONReader::Read(json)); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 for (std::size_t i = 0; i < promo_action_args_.size(); ++i) { | 92 for (std::size_t i = 0; i < promo_action_args_.size(); ++i) { |
96 std::string value; | 93 std::string value; |
97 EXPECT_TRUE(mobile_promo_.action_args()->GetString(i, &value)); | 94 EXPECT_TRUE(mobile_promo_.action_args()->GetString(i, &value)); |
98 EXPECT_EQ(value, promo_action_args_[i]); | 95 EXPECT_EQ(value, promo_action_args_[i]); |
99 } | 96 } |
100 } | 97 } |
101 | 98 |
102 // Create a new NotificationPromo from prefs and compare to current | 99 // Create a new NotificationPromo from prefs and compare to current |
103 // notification. | 100 // notification. |
104 void TestInitFromPrefs() { | 101 void TestInitFromPrefs() { |
105 NotificationPromoMobileNtp prefs_mobile_promo(profile_); | 102 NotificationPromoMobileNtp prefs_mobile_promo(prefs_); |
106 const bool rv = prefs_mobile_promo.InitFromPrefs(); | 103 const bool rv = prefs_mobile_promo.InitFromPrefs(); |
107 EXPECT_TRUE(rv); | 104 EXPECT_TRUE(rv); |
108 EXPECT_TRUE(prefs_mobile_promo.valid()); | 105 EXPECT_TRUE(prefs_mobile_promo.valid()); |
109 EXPECT_TRUE(mobile_promo_.valid()); | 106 EXPECT_TRUE(mobile_promo_.valid()); |
110 | 107 |
111 EXPECT_EQ(prefs_mobile_promo.text(), | 108 EXPECT_EQ(prefs_mobile_promo.text(), |
112 mobile_promo_.text()); | 109 mobile_promo_.text()); |
113 EXPECT_EQ(prefs_mobile_promo.text_long(), | 110 EXPECT_EQ(prefs_mobile_promo.text_long(), |
114 mobile_promo_.text_long()); | 111 mobile_promo_.text_long()); |
115 EXPECT_EQ(prefs_mobile_promo.action_type(), | 112 EXPECT_EQ(prefs_mobile_promo.action_type(), |
116 mobile_promo_.action_type()); | 113 mobile_promo_.action_type()); |
117 EXPECT_TRUE(mobile_promo_.action_args() != NULL); | 114 EXPECT_TRUE(mobile_promo_.action_args() != NULL); |
118 EXPECT_EQ(prefs_mobile_promo.action_args()->GetSize(), | 115 EXPECT_EQ(prefs_mobile_promo.action_args()->GetSize(), |
119 mobile_promo_.action_args()->GetSize()); | 116 mobile_promo_.action_args()->GetSize()); |
120 for (std::size_t i = 0; | 117 for (std::size_t i = 0; |
121 i < prefs_mobile_promo.action_args()->GetSize(); | 118 i < prefs_mobile_promo.action_args()->GetSize(); |
122 ++i) { | 119 ++i) { |
123 std::string promo_value; | 120 std::string promo_value; |
124 std::string prefs_value; | 121 std::string prefs_value; |
125 EXPECT_TRUE( | 122 EXPECT_TRUE( |
126 prefs_mobile_promo.action_args()->GetString(i, &prefs_value)); | 123 prefs_mobile_promo.action_args()->GetString(i, &prefs_value)); |
127 EXPECT_TRUE( | 124 EXPECT_TRUE( |
128 mobile_promo_.action_args()->GetString(i, &promo_value)); | 125 mobile_promo_.action_args()->GetString(i, &promo_value)); |
129 EXPECT_EQ(promo_value, prefs_value); | 126 EXPECT_EQ(promo_value, prefs_value); |
130 } | 127 } |
131 } | 128 } |
132 | 129 |
133 private: | 130 private: |
134 Profile* profile_; | 131 PrefServiceSimple* prefs_; |
135 PrefService* prefs_; | |
136 NotificationPromoMobileNtp mobile_promo_; | 132 NotificationPromoMobileNtp mobile_promo_; |
137 bool received_notification_; | 133 bool received_notification_; |
138 scoped_ptr<DictionaryValue> test_json_; | 134 scoped_ptr<DictionaryValue> test_json_; |
139 | 135 |
140 std::string promo_text_; | 136 std::string promo_text_; |
141 std::string promo_text_long_; | 137 std::string promo_text_long_; |
142 std::string promo_action_type_; | 138 std::string promo_action_type_; |
143 std::vector<std::string> promo_action_args_; | 139 std::vector<std::string> promo_action_args_; |
144 }; | 140 }; |
145 | 141 |
146 TEST_F(PromoResourceServiceMobileNtpTest, NotificationPromoMobileNtpTest) { | 142 TEST_F(PromoResourceServiceMobileNtpTest, NotificationPromoMobileNtpTest) { |
147 // Check that prefs are set correctly. | 143 NotificationPromoMobileNtpTest promo_test(local_state_.Get()); |
148 PrefService* prefs = profile_.GetPrefs(); | |
149 ASSERT_TRUE(prefs); | |
150 | |
151 NotificationPromoMobileNtpTest promo_test(&profile_); | |
152 | 144 |
153 // Set up start and end dates and promo line in a Dictionary as if parsed | 145 // Set up start and end dates and promo line in a Dictionary as if parsed |
154 // from the service. | 146 // from the service. |
155 promo_test.Init( | 147 promo_test.Init( |
156 "{" | 148 "{" |
157 " \"mobile_ntp_sync_promo\": [" | 149 " \"mobile_ntp_sync_promo\": [" |
158 " {" | 150 " {" |
159 " \"date\":" | 151 " \"date\":" |
160 " [" | 152 " [" |
161 " {" | 153 " {" |
162 " \"start\":\"3 Aug 1999 9:26:06 GMT\"," | 154 " \"start\":\"3 Aug 1999 9:26:06 GMT\"," |
163 " \"end\":\"7 Jan 2013 5:40:75 PST\"" | 155 " \"end\":\"7 Jan 2013 5:40:75 PST\"" |
164 " }" | 156 " }" |
165 " ]," | 157 " ]," |
166 " \"strings\":" | 158 " \"strings\":" |
167 " {" | 159 " {" |
168 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\":" | 160 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\":" |
169 " \"Like Chrome? Go http://www.google.com/chrome/\"," | 161 " \"Like Chrome? Go http://www.google.com/chrome/\"," |
170 " \"MOBILE_PROMO_CHROME_LONG_TEXT\":" | 162 " \"MOBILE_PROMO_CHROME_LONG_TEXT\":" |
171 " \"It's simple. Go http://www.google.com/chrome/\"," | 163 " \"It's simple. Go http://www.google.com/chrome/\"," |
172 " \"MOBILE_PROMO_EMAIL_BODY\":\"This is the body.\"," | 164 " \"MOBILE_PROMO_EMAIL_BODY\":\"This is the body.\"," |
173 " \"XXX\":\"XXX value\"" | 165 " \"XXX\":\"XXX value\"" |
174 " }," | 166 " }," |
175 " \"payload\":" | 167 " \"payload\":" |
176 " {" | 168 " {" |
177 " \"payload_format_version\":3," | 169 " \"payload_format_version\":3," |
178 " \"gplus_required\":false," | |
179 " \"promo_message_long\":" | 170 " \"promo_message_long\":" |
180 " \"MOBILE_PROMO_CHROME_LONG_TEXT\"," | 171 " \"MOBILE_PROMO_CHROME_LONG_TEXT\"," |
181 " \"promo_message_short\":" | 172 " \"promo_message_short\":" |
182 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\"," | 173 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\"," |
183 " \"promo_action_type\":\"ACTION_EMAIL\"," | 174 " \"promo_action_type\":\"ACTION_EMAIL\"," |
184 " \"promo_action_args\":[\"MOBILE_PROMO_EMAIL_BODY\",\"XXX\"]" | 175 " \"promo_action_args\":[\"MOBILE_PROMO_EMAIL_BODY\",\"XXX\"]" |
185 " }," | 176 " }," |
186 " \"max_views\":30" | 177 " \"max_views\":30" |
187 " }" | 178 " }" |
188 " ]" | 179 " ]" |
189 "}", | 180 "}", |
190 "Like Chrome? Go http://www.google.com/chrome/", | 181 "Like Chrome? Go http://www.google.com/chrome/", |
191 "It\'s simple. Go http://www.google.com/chrome/", | 182 "It\'s simple. Go http://www.google.com/chrome/", |
192 "ACTION_EMAIL", "This is the body.", "XXX value"); | 183 "ACTION_EMAIL", "This is the body.", "XXX value"); |
193 | 184 |
194 promo_test.InitPromoFromJson(true); | 185 promo_test.InitPromoFromJson(true); |
195 | 186 |
196 // Second time should not trigger a notification. | 187 // Second time should not trigger a notification. |
197 promo_test.InitPromoFromJson(false); | 188 promo_test.InitPromoFromJson(false); |
198 | 189 |
199 promo_test.TestInitFromPrefs(); | 190 promo_test.TestInitFromPrefs(); |
200 } | 191 } |
OLD | NEW |