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 promo_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> promo_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 NotificationPromoMobileNtpTest() : received_notification_(false) { |
46 : profile_(profile), | |
47 prefs_(profile->GetPrefs()), | |
48 mobile_promo_(profile), | |
49 received_notification_(false) { | |
50 } | 44 } |
51 | 45 |
52 void Init(const std::string& json, | 46 void Init(const std::string& json, |
53 const std::string& promo_text, | 47 const std::string& promo_text, |
54 const std::string& promo_text_long, | 48 const std::string& promo_text_long, |
55 const std::string& promo_action_type, | 49 const std::string& promo_action_type, |
56 const std::string& promo_action_arg0, | 50 const std::string& promo_action_arg0, |
57 const std::string& promo_action_arg1) { | 51 const std::string& promo_action_arg1) { |
58 Value* value(base::JSONReader::Read(json)); | 52 Value* value(base::JSONReader::Read(json)); |
59 ASSERT_TRUE(value); | 53 ASSERT_TRUE(value); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 for (std::size_t i = 0; i < promo_action_args_.size(); ++i) { | 89 for (std::size_t i = 0; i < promo_action_args_.size(); ++i) { |
96 std::string value; | 90 std::string value; |
97 EXPECT_TRUE(mobile_promo_.action_args()->GetString(i, &value)); | 91 EXPECT_TRUE(mobile_promo_.action_args()->GetString(i, &value)); |
98 EXPECT_EQ(value, promo_action_args_[i]); | 92 EXPECT_EQ(value, promo_action_args_[i]); |
99 } | 93 } |
100 } | 94 } |
101 | 95 |
102 // Create a new NotificationPromo from prefs and compare to current | 96 // Create a new NotificationPromo from prefs and compare to current |
103 // notification. | 97 // notification. |
104 void TestInitFromPrefs() { | 98 void TestInitFromPrefs() { |
105 NotificationPromoMobileNtp prefs_mobile_promo(profile_); | 99 NotificationPromoMobileNtp prefs_mobile_promo; |
106 const bool rv = prefs_mobile_promo.InitFromPrefs(); | 100 const bool rv = prefs_mobile_promo.InitFromPrefs(); |
107 EXPECT_TRUE(rv); | 101 EXPECT_TRUE(rv); |
108 EXPECT_TRUE(prefs_mobile_promo.valid()); | 102 EXPECT_TRUE(prefs_mobile_promo.valid()); |
109 EXPECT_TRUE(mobile_promo_.valid()); | 103 EXPECT_TRUE(mobile_promo_.valid()); |
110 | 104 |
111 EXPECT_EQ(prefs_mobile_promo.text(), | 105 EXPECT_EQ(prefs_mobile_promo.text(), |
112 mobile_promo_.text()); | 106 mobile_promo_.text()); |
113 EXPECT_EQ(prefs_mobile_promo.text_long(), | 107 EXPECT_EQ(prefs_mobile_promo.text_long(), |
114 mobile_promo_.text_long()); | 108 mobile_promo_.text_long()); |
115 EXPECT_EQ(prefs_mobile_promo.action_type(), | 109 EXPECT_EQ(prefs_mobile_promo.action_type(), |
116 mobile_promo_.action_type()); | 110 mobile_promo_.action_type()); |
117 EXPECT_TRUE(mobile_promo_.action_args() != NULL); | 111 EXPECT_TRUE(mobile_promo_.action_args() != NULL); |
118 EXPECT_EQ(prefs_mobile_promo.action_args()->GetSize(), | 112 EXPECT_EQ(prefs_mobile_promo.action_args()->GetSize(), |
119 mobile_promo_.action_args()->GetSize()); | 113 mobile_promo_.action_args()->GetSize()); |
120 for (std::size_t i = 0; | 114 for (std::size_t i = 0; |
121 i < prefs_mobile_promo.action_args()->GetSize(); | 115 i < prefs_mobile_promo.action_args()->GetSize(); |
122 ++i) { | 116 ++i) { |
123 std::string promo_value; | 117 std::string promo_value; |
124 std::string prefs_value; | 118 std::string prefs_value; |
125 EXPECT_TRUE( | 119 EXPECT_TRUE( |
126 prefs_mobile_promo.action_args()->GetString(i, &prefs_value)); | 120 prefs_mobile_promo.action_args()->GetString(i, &prefs_value)); |
127 EXPECT_TRUE( | 121 EXPECT_TRUE( |
128 mobile_promo_.action_args()->GetString(i, &promo_value)); | 122 mobile_promo_.action_args()->GetString(i, &promo_value)); |
129 EXPECT_EQ(promo_value, prefs_value); | 123 EXPECT_EQ(promo_value, prefs_value); |
130 } | 124 } |
131 } | 125 } |
132 | 126 |
133 private: | 127 private: |
134 Profile* profile_; | |
135 PrefService* prefs_; | |
136 NotificationPromoMobileNtp mobile_promo_; | 128 NotificationPromoMobileNtp mobile_promo_; |
137 bool received_notification_; | 129 bool received_notification_; |
138 scoped_ptr<DictionaryValue> test_json_; | 130 scoped_ptr<DictionaryValue> test_json_; |
139 | 131 |
140 std::string promo_text_; | 132 std::string promo_text_; |
141 std::string promo_text_long_; | 133 std::string promo_text_long_; |
142 std::string promo_action_type_; | 134 std::string promo_action_type_; |
143 std::vector<std::string> promo_action_args_; | 135 std::vector<std::string> promo_action_args_; |
144 }; | 136 }; |
145 | 137 |
146 TEST_F(PromoResourceServiceMobileNtpTest, NotificationPromoMobileNtpTest) { | 138 TEST_F(PromoResourceServiceMobileNtpTest, NotificationPromoMobileNtpTest) { |
147 // Check that prefs are set correctly. | 139 NotificationPromoMobileNtpTest promo_test; |
148 PrefService* prefs = profile_.GetPrefs(); | |
149 ASSERT_TRUE(prefs); | |
150 | |
151 NotificationPromoMobileNtpTest promo_test(&profile_); | |
152 | 140 |
153 // Set up start and end dates and promo line in a Dictionary as if parsed | 141 // Set up start and end dates and promo line in a Dictionary as if parsed |
154 // from the service. | 142 // from the service. |
155 promo_test.Init( | 143 promo_test.Init( |
156 "{" | 144 "{" |
157 " \"mobile_ntp_sync_promo\": [" | 145 " \"mobile_ntp_sync_promo\": [" |
158 " {" | 146 " {" |
159 " \"date\":" | 147 " \"date\":" |
160 " [" | 148 " [" |
161 " {" | 149 " {" |
162 " \"start\":\"3 Aug 1999 9:26:06 GMT\"," | 150 " \"start\":\"3 Aug 1999 9:26:06 GMT\"," |
163 " \"end\":\"7 Jan 2013 5:40:75 PST\"" | 151 " \"end\":\"7 Jan 2013 5:40:75 PST\"" |
164 " }" | 152 " }" |
165 " ]," | 153 " ]," |
166 " \"strings\":" | 154 " \"strings\":" |
167 " {" | 155 " {" |
168 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\":" | 156 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\":" |
169 " \"Like Chrome? Go http://www.google.com/chrome/\"," | 157 " \"Like Chrome? Go http://www.google.com/chrome/\"," |
170 " \"MOBILE_PROMO_CHROME_LONG_TEXT\":" | 158 " \"MOBILE_PROMO_CHROME_LONG_TEXT\":" |
171 " \"It's simple. Go http://www.google.com/chrome/\"," | 159 " \"It's simple. Go http://www.google.com/chrome/\"," |
172 " \"MOBILE_PROMO_EMAIL_BODY\":\"This is the body.\"," | 160 " \"MOBILE_PROMO_EMAIL_BODY\":\"This is the body.\"," |
173 " \"XXX\":\"XXX value\"" | 161 " \"XXX\":\"XXX value\"" |
174 " }," | 162 " }," |
175 " \"payload\":" | 163 " \"payload\":" |
176 " {" | 164 " {" |
177 " \"payload_format_version\":3," | 165 " \"payload_format_version\":3," |
178 " \"gplus_required\":false," | |
179 " \"promo_message_long\":" | 166 " \"promo_message_long\":" |
180 " \"MOBILE_PROMO_CHROME_LONG_TEXT\"," | 167 " \"MOBILE_PROMO_CHROME_LONG_TEXT\"," |
181 " \"promo_message_short\":" | 168 " \"promo_message_short\":" |
182 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\"," | 169 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\"," |
183 " \"promo_action_type\":\"ACTION_EMAIL\"," | 170 " \"promo_action_type\":\"ACTION_EMAIL\"," |
184 " \"promo_action_args\":[\"MOBILE_PROMO_EMAIL_BODY\",\"XXX\"]" | 171 " \"promo_action_args\":[\"MOBILE_PROMO_EMAIL_BODY\",\"XXX\"]" |
185 " }," | 172 " }," |
186 " \"max_views\":30" | 173 " \"max_views\":30" |
187 " }" | 174 " }" |
188 " ]" | 175 " ]" |
189 "}", | 176 "}", |
190 "Like Chrome? Go http://www.google.com/chrome/", | 177 "Like Chrome? Go http://www.google.com/chrome/", |
191 "It\'s simple. Go http://www.google.com/chrome/", | 178 "It\'s simple. Go http://www.google.com/chrome/", |
192 "ACTION_EMAIL", "This is the body.", "XXX value"); | 179 "ACTION_EMAIL", "This is the body.", "XXX value"); |
193 | 180 |
194 promo_test.InitPromoFromJson(true); | 181 promo_test.InitPromoFromJson(true); |
195 | 182 |
196 // Second time should not trigger a notification. | 183 // Second time should not trigger a notification. |
197 promo_test.InitPromoFromJson(false); | 184 promo_test.InitPromoFromJson(false); |
198 | 185 |
199 promo_test.TestInitFromPrefs(); | 186 promo_test.TestInitFromPrefs(); |
200 } | 187 } |
OLD | NEW |