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" | |
15 #include "chrome/browser/prefs/pref_service.h" | |
16 #include "chrome/browser/web_resource/notification_promo.h" | 14 #include "chrome/browser/web_resource/notification_promo.h" |
17 #include "chrome/browser/web_resource/promo_resource_service.h" | 15 #include "chrome/browser/web_resource/promo_resource_service.h" |
18 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
19 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
20 #include "chrome/test/base/testing_browser_process.h" | 18 #include "chrome/test/base/testing_browser_process.h" |
21 #include "chrome/test/base/testing_pref_service.h" | 19 #include "chrome/test/base/testing_pref_service.h" |
22 #include "chrome/test/base/testing_profile.h" | |
23 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
24 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
26 | 23 |
27 #include "chrome/browser/web_resource/notification_promo_mobile_ntp.h" | 24 #include "chrome/browser/web_resource/notification_promo_mobile_ntp.h" |
28 | 25 |
29 class PromoResourceServiceMobileNtpTest : public testing::Test { | 26 class PromoResourceServiceMobileNtpTest : public testing::Test { |
30 public: | 27 public: |
31 PromoResourceServiceMobileNtpTest() | 28 PromoResourceServiceMobileNtpTest() |
32 : local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)), | 29 : local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)) { |
33 web_resource_service_(new PromoResourceService(&profile_)) { | 30 static_cast<TestingBrowserProcess*>(g_browser_process)->SetLocalState( |
| 31 local_state_.Get()); |
| 32 // |promo_resource_service_| must be created after local state is set. |
| 33 promo_resource_service_ = new PromoResourceService; |
34 } | 34 } |
35 | 35 |
36 protected: | 36 protected: |
37 TestingProfile profile_; | |
38 ScopedTestingLocalState local_state_; | 37 ScopedTestingLocalState local_state_; |
39 scoped_refptr<PromoResourceService> web_resource_service_; | 38 scoped_refptr<PromoResourceService> promo_resource_service_; |
40 MessageLoop loop_; | 39 MessageLoop loop_; |
41 }; | 40 }; |
42 | 41 |
43 class NotificationPromoMobileNtpTest { | 42 class NotificationPromoMobileNtpTest { |
44 public: | 43 public: |
45 explicit NotificationPromoMobileNtpTest(Profile* profile) | 44 NotificationPromoMobileNtpTest() : received_notification_(false) { |
46 : profile_(profile), | |
47 prefs_(profile->GetPrefs()), | |
48 mobile_promo_(profile), | |
49 received_notification_(false) { | |
50 } | 45 } |
51 | 46 |
52 void Init(const std::string& json, | 47 void Init(const std::string& json, |
53 const std::string& promo_text, | 48 const std::string& promo_text, |
54 const std::string& promo_text_long, | 49 const std::string& promo_text_long, |
55 const std::string& promo_action_type, | 50 const std::string& promo_action_type, |
56 const std::string& promo_action_arg0, | 51 const std::string& promo_action_arg0, |
57 const std::string& promo_action_arg1) { | 52 const std::string& promo_action_arg1) { |
58 Value* value(base::JSONReader::Read(json)); | 53 Value* value(base::JSONReader::Read(json)); |
59 ASSERT_TRUE(value); | 54 ASSERT_TRUE(value); |
60 DictionaryValue* dict = NULL; | 55 DictionaryValue* dict = NULL; |
61 value->GetAsDictionary(&dict); | 56 value->GetAsDictionary(&dict); |
62 ASSERT_TRUE(dict); | 57 ASSERT_TRUE(dict); |
63 test_json_.reset(dict); | 58 test_json_.reset(dict); |
64 | 59 |
65 promo_text_ = promo_text; | 60 promo_text_ = promo_text; |
66 promo_text_long_ = promo_text_long; | 61 promo_text_long_ = promo_text_long; |
67 promo_action_type_ = promo_action_type; | 62 promo_action_type_ = promo_action_type; |
68 promo_action_args_.push_back(promo_action_arg0); | 63 promo_action_args_.push_back(promo_action_arg0); |
69 promo_action_args_.push_back(promo_action_arg1); | 64 promo_action_args_.push_back(promo_action_arg1); |
70 | 65 |
71 received_notification_ = false; | 66 received_notification_ = false; |
72 } | 67 } |
73 | 68 |
74 void InitPromoFromJson(bool should_receive_notification) { | 69 void InitPromoFromJson(bool should_receive_notification) { |
75 const bool rv = mobile_promo_.InitFromJson(*test_json_); | 70 EXPECT_TRUE(mobile_promo_.InitFromJson(*test_json_)); |
76 EXPECT_TRUE(rv); | |
77 EXPECT_TRUE(mobile_promo_.valid()); | 71 EXPECT_TRUE(mobile_promo_.valid()); |
78 EXPECT_EQ(should_receive_notification, | 72 EXPECT_EQ(should_receive_notification, |
79 mobile_promo_.notification_promo().new_notification()); | 73 mobile_promo_.notification_promo().new_notification()); |
80 | 74 |
81 // Test the fields. | 75 // Test the fields. |
82 TestNotification(); | 76 TestNotification(); |
83 } | 77 } |
84 | 78 |
85 void TestNotification() { | 79 void TestNotification() { |
86 // Check values. | 80 // Check values. |
87 EXPECT_TRUE(mobile_promo_.valid()); | 81 EXPECT_TRUE(mobile_promo_.valid()); |
88 EXPECT_EQ(mobile_promo_.text(), promo_text_); | 82 EXPECT_EQ(mobile_promo_.text(), promo_text_); |
89 EXPECT_EQ(mobile_promo_.text_long(), promo_text_long_); | 83 EXPECT_EQ(mobile_promo_.text_long(), promo_text_long_); |
90 EXPECT_EQ(mobile_promo_.action_type(), promo_action_type_); | 84 EXPECT_EQ(mobile_promo_.action_type(), promo_action_type_); |
91 EXPECT_TRUE(mobile_promo_.action_args() != NULL); | 85 EXPECT_TRUE(mobile_promo_.action_args() != NULL); |
92 EXPECT_EQ(2u, promo_action_args_.size()); | 86 EXPECT_EQ(2u, promo_action_args_.size()); |
93 EXPECT_EQ(mobile_promo_.action_args()->GetSize(), | 87 EXPECT_EQ(mobile_promo_.action_args()->GetSize(), |
94 promo_action_args_.size()); | 88 promo_action_args_.size()); |
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 EXPECT_TRUE(prefs_mobile_promo.InitFromPrefs()); |
107 EXPECT_TRUE(rv); | |
108 EXPECT_TRUE(prefs_mobile_promo.valid()); | 101 EXPECT_TRUE(prefs_mobile_promo.valid()); |
109 EXPECT_TRUE(mobile_promo_.valid()); | 102 EXPECT_TRUE(mobile_promo_.valid()); |
110 | 103 |
111 EXPECT_EQ(prefs_mobile_promo.text(), | 104 EXPECT_EQ(prefs_mobile_promo.text(), |
112 mobile_promo_.text()); | 105 mobile_promo_.text()); |
113 EXPECT_EQ(prefs_mobile_promo.text_long(), | 106 EXPECT_EQ(prefs_mobile_promo.text_long(), |
114 mobile_promo_.text_long()); | 107 mobile_promo_.text_long()); |
115 EXPECT_EQ(prefs_mobile_promo.action_type(), | 108 EXPECT_EQ(prefs_mobile_promo.action_type(), |
116 mobile_promo_.action_type()); | 109 mobile_promo_.action_type()); |
117 EXPECT_TRUE(mobile_promo_.action_args() != NULL); | 110 EXPECT_TRUE(mobile_promo_.action_args() != NULL); |
118 EXPECT_EQ(prefs_mobile_promo.action_args()->GetSize(), | 111 EXPECT_EQ(prefs_mobile_promo.action_args()->GetSize(), |
119 mobile_promo_.action_args()->GetSize()); | 112 mobile_promo_.action_args()->GetSize()); |
120 for (std::size_t i = 0; | 113 for (std::size_t i = 0; |
121 i < prefs_mobile_promo.action_args()->GetSize(); | 114 i < prefs_mobile_promo.action_args()->GetSize(); |
122 ++i) { | 115 ++i) { |
123 std::string promo_value; | 116 std::string promo_value; |
124 std::string prefs_value; | 117 std::string prefs_value; |
125 EXPECT_TRUE( | 118 EXPECT_TRUE( |
126 prefs_mobile_promo.action_args()->GetString(i, &prefs_value)); | 119 prefs_mobile_promo.action_args()->GetString(i, &prefs_value)); |
127 EXPECT_TRUE( | 120 EXPECT_TRUE( |
128 mobile_promo_.action_args()->GetString(i, &promo_value)); | 121 mobile_promo_.action_args()->GetString(i, &promo_value)); |
129 EXPECT_EQ(promo_value, prefs_value); | 122 EXPECT_EQ(promo_value, prefs_value); |
130 } | 123 } |
131 } | 124 } |
132 | 125 |
133 private: | 126 private: |
134 Profile* profile_; | |
135 PrefService* prefs_; | |
136 NotificationPromoMobileNtp mobile_promo_; | 127 NotificationPromoMobileNtp mobile_promo_; |
137 bool received_notification_; | 128 bool received_notification_; |
138 scoped_ptr<DictionaryValue> test_json_; | 129 scoped_ptr<DictionaryValue> test_json_; |
139 | 130 |
140 std::string promo_text_; | 131 std::string promo_text_; |
141 std::string promo_text_long_; | 132 std::string promo_text_long_; |
142 std::string promo_action_type_; | 133 std::string promo_action_type_; |
143 std::vector<std::string> promo_action_args_; | 134 std::vector<std::string> promo_action_args_; |
144 }; | 135 }; |
145 | 136 |
146 TEST_F(PromoResourceServiceMobileNtpTest, NotificationPromoMobileNtpTest) { | 137 TEST_F(PromoResourceServiceMobileNtpTest, NotificationPromoMobileNtpTest) { |
147 // Check that prefs are set correctly. | 138 NotificationPromoMobileNtpTest promo_test; |
148 PrefService* prefs = profile_.GetPrefs(); | |
149 ASSERT_TRUE(prefs); | |
150 | |
151 NotificationPromoMobileNtpTest promo_test(&profile_); | |
152 | 139 |
153 // Set up start and end dates and promo line in a Dictionary as if parsed | 140 // Set up start and end dates and promo line in a Dictionary as if parsed |
154 // from the service. | 141 // from the service. |
155 promo_test.Init( | 142 promo_test.Init( |
156 "{" | 143 "{" |
157 " \"mobile_ntp_sync_promo\": [" | 144 " \"mobile_ntp_sync_promo\": [" |
158 " {" | 145 " {" |
159 " \"date\":" | 146 " \"date\":" |
160 " [" | 147 " [" |
161 " {" | 148 " {" |
162 " \"start\":\"3 Aug 1999 9:26:06 GMT\"," | 149 " \"start\":\"3 Aug 1999 9:26:06 GMT\"," |
163 " \"end\":\"7 Jan 2013 5:40:75 PST\"" | 150 " \"end\":\"7 Jan 2013 5:40:75 PST\"" |
164 " }" | 151 " }" |
165 " ]," | 152 " ]," |
166 " \"strings\":" | 153 " \"strings\":" |
167 " {" | 154 " {" |
168 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\":" | 155 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\":" |
169 " \"Like Chrome? Go http://www.google.com/chrome/\"," | 156 " \"Like Chrome? Go http://www.google.com/chrome/\"," |
170 " \"MOBILE_PROMO_CHROME_LONG_TEXT\":" | 157 " \"MOBILE_PROMO_CHROME_LONG_TEXT\":" |
171 " \"It's simple. Go http://www.google.com/chrome/\"," | 158 " \"It's simple. Go http://www.google.com/chrome/\"," |
172 " \"MOBILE_PROMO_EMAIL_BODY\":\"This is the body.\"," | 159 " \"MOBILE_PROMO_EMAIL_BODY\":\"This is the body.\"," |
173 " \"XXX\":\"XXX value\"" | 160 " \"XXX\":\"XXX value\"" |
174 " }," | 161 " }," |
175 " \"payload\":" | 162 " \"payload\":" |
176 " {" | 163 " {" |
177 " \"payload_format_version\":3," | 164 " \"payload_format_version\":3," |
178 " \"gplus_required\":false," | |
179 " \"promo_message_long\":" | 165 " \"promo_message_long\":" |
180 " \"MOBILE_PROMO_CHROME_LONG_TEXT\"," | 166 " \"MOBILE_PROMO_CHROME_LONG_TEXT\"," |
181 " \"promo_message_short\":" | 167 " \"promo_message_short\":" |
182 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\"," | 168 " \"MOBILE_PROMO_CHROME_SHORT_TEXT\"," |
183 " \"promo_action_type\":\"ACTION_EMAIL\"," | 169 " \"promo_action_type\":\"ACTION_EMAIL\"," |
184 " \"promo_action_args\":[\"MOBILE_PROMO_EMAIL_BODY\",\"XXX\"]" | 170 " \"promo_action_args\":[\"MOBILE_PROMO_EMAIL_BODY\",\"XXX\"]" |
185 " }," | 171 " }," |
186 " \"max_views\":30" | 172 " \"max_views\":30" |
187 " }" | 173 " }" |
188 " ]" | 174 " ]" |
189 "}", | 175 "}", |
190 "Like Chrome? Go http://www.google.com/chrome/", | 176 "Like Chrome? Go http://www.google.com/chrome/", |
191 "It\'s simple. Go http://www.google.com/chrome/", | 177 "It\'s simple. Go http://www.google.com/chrome/", |
192 "ACTION_EMAIL", "This is the body.", "XXX value"); | 178 "ACTION_EMAIL", "This is the body.", "XXX value"); |
193 | 179 |
194 promo_test.InitPromoFromJson(true); | 180 promo_test.InitPromoFromJson(true); |
195 | 181 |
196 // Second time should not trigger a notification. | 182 // Second time should not trigger a notification. |
197 promo_test.InitPromoFromJson(false); | 183 promo_test.InitPromoFromJson(false); |
198 | 184 |
199 promo_test.TestInitFromPrefs(); | 185 promo_test.TestInitFromPrefs(); |
200 } | 186 } |
OLD | NEW |