OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/json/json_reader.h" | 5 #include "base/json/json_reader.h" |
6 #include "base/time.h" | 6 #include "base/time.h" |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/extensions/apps_promo.h" | 10 #include "chrome/browser/extensions/apps_promo.h" |
11 #include "chrome/browser/prefs/browser_prefs.h" | 11 #include "chrome/browser/prefs/browser_prefs.h" |
12 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
13 #include "chrome/browser/web_resource/promo_resource_service.h" | 13 #include "chrome/browser/web_resource/promo_resource_service.h" |
14 #include "chrome/common/pref_names.h" | 14 #include "chrome/common/pref_names.h" |
15 #include "chrome/test/base/testing_browser_process.h" | 15 #include "chrome/test/base/testing_browser_process.h" |
16 #include "chrome/test/base/testing_pref_service.h" | 16 #include "chrome/test/base/testing_pref_service.h" |
17 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 18 #include "content/test/test_url_fetcher_factory.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 class PromoResourceServiceTest : public testing::Test { | 21 class PromoResourceServiceTest : public testing::Test { |
21 public: | 22 public: |
22 PromoResourceServiceTest() | 23 PromoResourceServiceTest() |
23 : local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)), | 24 : local_state_(static_cast<TestingBrowserProcess*>(g_browser_process)), |
24 web_resource_service_(new PromoResourceService(&profile_)) { | 25 web_resource_service_(new PromoResourceService(&profile_)) { |
25 } | 26 } |
26 | 27 |
27 protected: | 28 protected: |
28 TestingProfile profile_; | 29 TestingProfile profile_; |
29 ScopedTestingLocalState local_state_; | 30 ScopedTestingLocalState local_state_; |
30 scoped_refptr<PromoResourceService> web_resource_service_; | 31 scoped_refptr<PromoResourceService> web_resource_service_; |
| 32 MessageLoop loop_; |
31 }; | 33 }; |
32 | 34 |
33 // Verifies that custom dates read from a web resource server are written to | 35 // Verifies that custom dates read from a web resource server are written to |
34 // the preferences file. | 36 // the preferences file. |
35 TEST_F(PromoResourceServiceTest, UnpackLogoSignal) { | 37 TEST_F(PromoResourceServiceTest, UnpackLogoSignal) { |
36 // Set up start and end dates in a Dictionary as if parsed from the service. | 38 // Set up start and end dates in a Dictionary as if parsed from the service. |
37 std::string json = "{ " | 39 std::string json = "{ " |
38 " \"topic\": {" | 40 " \"topic\": {" |
39 " \"answers\": [" | 41 " \"answers\": [" |
40 " {" | 42 " {" |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 " {" | 126 " {" |
125 " \"name\": \"promo_end\"," | 127 " \"name\": \"promo_end\"," |
126 " \"inproduct\": \"31/01/12 01:00 GMT\"" | 128 " \"inproduct\": \"31/01/12 01:00 GMT\"" |
127 " }" | 129 " }" |
128 " ]" | 130 " ]" |
129 " }" | 131 " }" |
130 "}"; | 132 "}"; |
131 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( | 133 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( |
132 base::JSONReader::Read(json, false))); | 134 base::JSONReader::Read(json, false))); |
133 | 135 |
134 // Initialize a message loop for this to run on. | |
135 MessageLoop loop; | |
136 | |
137 // Check that prefs are set correctly. | 136 // Check that prefs are set correctly. |
138 web_resource_service_->UnpackPromoSignal(*(test_json.get())); | 137 web_resource_service_->UnpackPromoSignal(*(test_json.get())); |
139 PrefService* prefs = profile_.GetPrefs(); | 138 PrefService* prefs = profile_.GetPrefs(); |
140 ASSERT_TRUE(prefs != NULL); | 139 ASSERT_TRUE(prefs != NULL); |
141 | 140 |
142 std::string promo_line = prefs->GetString(prefs::kNTPPromoLine); | 141 std::string promo_line = prefs->GetString(prefs::kNTPPromoLine); |
143 EXPECT_EQ(promo_line, "Eat more pie!"); | 142 EXPECT_EQ(promo_line, "Eat more pie!"); |
144 | 143 |
145 int promo_group = prefs->GetInteger(prefs::kNTPPromoGroup); | 144 int promo_group = prefs->GetInteger(prefs::kNTPPromoGroup); |
146 EXPECT_GE(promo_group, 0); | 145 EXPECT_GE(promo_group, 0); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 " \"inproduct_target\": \"The button label!\"," | 179 " \"inproduct_target\": \"The button label!\"," |
181 " \"inproduct\": \"http://link.com\"," | 180 " \"inproduct\": \"http://link.com\"," |
182 " \"tooltip\": \"No thanks, hide this.\"" | 181 " \"tooltip\": \"No thanks, hide this.\"" |
183 " }" | 182 " }" |
184 " ]" | 183 " ]" |
185 " }" | 184 " }" |
186 "}"; | 185 "}"; |
187 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( | 186 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( |
188 base::JSONReader::Read(json, false))); | 187 base::JSONReader::Read(json, false))); |
189 | 188 |
190 // Initialize a message loop for this to run on. | 189 // Set the source logo URL to verify that it gets cleared. |
191 MessageLoop loop; | 190 AppsPromo::SetSourcePromoLogoURL(GURL("https://www.google.com/test.png")); |
192 | 191 |
193 // Check that prefs are set correctly. | 192 // Check that prefs are set correctly. |
194 web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); | 193 web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); |
195 PrefService* prefs = profile_.GetPrefs(); | |
196 ASSERT_TRUE(prefs != NULL); | |
197 | 194 |
198 EXPECT_EQ("341252", AppsPromo::GetPromoId()); | 195 AppsPromo::PromoData actual_data = AppsPromo::GetPromo(); |
199 EXPECT_EQ("The header!", AppsPromo::GetPromoHeaderText()); | 196 EXPECT_EQ("341252", actual_data.id); |
200 EXPECT_EQ("The button label!", AppsPromo::GetPromoButtonText()); | 197 EXPECT_EQ("The header!", actual_data.header); |
201 EXPECT_EQ(GURL("http://link.com"), AppsPromo::GetPromoLink()); | 198 EXPECT_EQ("The button label!", actual_data.button); |
202 EXPECT_EQ("No thanks, hide this.", AppsPromo::GetPromoExpireText()); | 199 EXPECT_EQ(GURL("http://link.com"), actual_data.link); |
203 EXPECT_EQ(AppsPromo::USERS_NEW, AppsPromo::GetPromoUserGroup()); | 200 EXPECT_EQ("No thanks, hide this.", actual_data.expire); |
204 EXPECT_EQ(GURL("chrome://theme/IDR_WEBSTORE_ICON"), | 201 EXPECT_EQ(AppsPromo::USERS_NEW, actual_data.user_group); |
205 AppsPromo::GetPromoLogo()); | 202 |
| 203 // When we don't download a logo, we revert to the default and clear the |
| 204 // source pref. |
| 205 EXPECT_EQ(GURL("chrome://theme/IDR_WEBSTORE_ICON"), actual_data.logo); |
| 206 EXPECT_EQ(GURL(""), AppsPromo::GetSourcePromoLogoURL()); |
206 } | 207 } |
207 | 208 |
208 // Tests that the "web store active" flag is set even when the web store promo | 209 // Tests that the "web store active" flag is set even when the web store promo |
209 // fails parsing. | 210 // fails parsing. |
210 TEST_F(PromoResourceServiceTest, UnpackPartialWebStoreSignal) { | 211 TEST_F(PromoResourceServiceTest, UnpackPartialWebStoreSignal) { |
211 std::string json = "{ " | 212 std::string json = "{ " |
212 " \"topic\": {" | 213 " \"topic\": {" |
213 " \"answers\": [" | 214 " \"answers\": [" |
214 " {" | 215 " {" |
215 " \"answer_id\": \"sdlfj32\"," | 216 " \"answer_id\": \"sdlfj32\"," |
216 " \"name\": \"webstore_promo:#klsdjlfSD\"" | 217 " \"name\": \"webstore_promo:#klsdjlfSD\"" |
217 " }" | 218 " }" |
218 " ]" | 219 " ]" |
219 " }" | 220 " }" |
220 "}"; | 221 "}"; |
221 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( | 222 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( |
222 base::JSONReader::Read(json, false))); | 223 base::JSONReader::Read(json, false))); |
223 | 224 |
224 // Initialize a message loop for this to run on. | |
225 MessageLoop loop; | |
226 | |
227 // Check that prefs are set correctly. | 225 // Check that prefs are set correctly. |
228 web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); | 226 web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); |
229 EXPECT_FALSE(AppsPromo::IsPromoSupportedForLocale()); | 227 EXPECT_FALSE(AppsPromo::IsPromoSupportedForLocale()); |
230 EXPECT_TRUE(AppsPromo::IsWebStoreSupportedForLocale()); | 228 EXPECT_TRUE(AppsPromo::IsWebStoreSupportedForLocale()); |
231 } | 229 } |
232 | 230 |
| 231 // Tests that we can successfully unpack web store signals with HTTPS |
| 232 // logos. |
| 233 TEST_F(PromoResourceServiceTest, UnpackWebStoreSignalHttpsLogo) { |
| 234 web_resource_service_->set_channel(chrome::VersionInfo::CHANNEL_DEV); |
| 235 |
| 236 std::string logo_url = "https://www.google.com/image/test.png"; |
| 237 std::string png_data = "!$#%,./nvl;iadh9oh82"; |
| 238 std::string png_base64 = "data:image/png;base64,ISQjJSwuL252bDtpYWRoOW9oODI="; |
| 239 |
| 240 FakeURLFetcherFactory factory; |
| 241 factory.SetFakeResponse(logo_url, png_data, true); |
| 242 |
| 243 std::string json = |
| 244 "{ " |
| 245 " \"topic\": {" |
| 246 " \"answers\": [" |
| 247 " {" |
| 248 " \"answer_id\": \"340252\"," |
| 249 " \"name\": \"webstore_promo:15:1:" + logo_url + "\"," |
| 250 " \"question\": \"Header!\"," |
| 251 " \"inproduct_target\": \"The button label!\"," |
| 252 " \"inproduct\": \"http://link.com\"," |
| 253 " \"tooltip\": \"No thanks, hide this.\"" |
| 254 " }" |
| 255 " ]" |
| 256 " }" |
| 257 "}"; |
| 258 |
| 259 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( |
| 260 base::JSONReader::Read(json, false))); |
| 261 |
| 262 // Update the promo multiple times to verify the logo is cached correctly. |
| 263 for (size_t i = 0; i < 2; ++i) { |
| 264 web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); |
| 265 |
| 266 // We should only need to run the message loop the first time since the |
| 267 // image is then cached. |
| 268 if (i == 0) |
| 269 loop_.RunAllPending(); |
| 270 |
| 271 // Reset this scoped_ptr to prevent a DCHECK. |
| 272 web_resource_service_->apps_promo_logo_fetcher_.reset(); |
| 273 |
| 274 AppsPromo::PromoData actual_data = AppsPromo::GetPromo(); |
| 275 EXPECT_EQ("340252", actual_data.id); |
| 276 EXPECT_EQ("Header!", actual_data.header); |
| 277 EXPECT_EQ("The button label!", actual_data.button); |
| 278 EXPECT_EQ(GURL("http://link.com"), actual_data.link); |
| 279 EXPECT_EQ("No thanks, hide this.", actual_data.expire); |
| 280 EXPECT_EQ(AppsPromo::USERS_NEW, actual_data.user_group); |
| 281 |
| 282 // The logo should now be a base64 DATA URL. |
| 283 EXPECT_EQ(GURL(png_base64), actual_data.logo); |
| 284 |
| 285 // And the source pref should hold the source HTTPS URL. |
| 286 EXPECT_EQ(GURL(logo_url), AppsPromo::GetSourcePromoLogoURL()); |
| 287 } |
| 288 } |
| 289 |
| 290 // Tests that we revert to the default logo when the fetch fails. |
| 291 TEST_F(PromoResourceServiceTest, UnpackWebStoreSignalHttpsLogoError) { |
| 292 web_resource_service_->set_channel(chrome::VersionInfo::CHANNEL_DEV); |
| 293 |
| 294 std::string logo_url = "https://www.google.com/image/test.png"; |
| 295 std::string png_data = "!$#%,./nvl;iadh9oh82"; |
| 296 std::string png_base64 = "ISQjJSwuL252bDtpYWRoOW9oODI="; |
| 297 |
| 298 FakeURLFetcherFactory factory; |
| 299 |
| 300 // Have URLFetcher return a 500 error. |
| 301 factory.SetFakeResponse(logo_url, png_data, false); |
| 302 |
| 303 std::string json = |
| 304 "{ " |
| 305 " \"topic\": {" |
| 306 " \"answers\": [" |
| 307 " {" |
| 308 " \"answer_id\": \"340252\"," |
| 309 " \"name\": \"webstore_promo:15:1:" + logo_url + "\"," |
| 310 " \"question\": \"Header!\"," |
| 311 " \"inproduct_target\": \"The button label!\"," |
| 312 " \"inproduct\": \"http://link.com\"," |
| 313 " \"tooltip\": \"No thanks, hide this.\"" |
| 314 " }" |
| 315 " ]" |
| 316 " }" |
| 317 "}"; |
| 318 |
| 319 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( |
| 320 base::JSONReader::Read(json, false))); |
| 321 |
| 322 web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); |
| 323 |
| 324 loop_.RunAllPending(); |
| 325 |
| 326 // Reset this scoped_ptr to prevent a DCHECK. |
| 327 web_resource_service_->apps_promo_logo_fetcher_.reset(); |
| 328 |
| 329 AppsPromo::PromoData actual_data = AppsPromo::GetPromo(); |
| 330 EXPECT_EQ("340252", actual_data.id); |
| 331 EXPECT_EQ("Header!", actual_data.header); |
| 332 EXPECT_EQ("The button label!", actual_data.button); |
| 333 EXPECT_EQ(GURL("http://link.com"), actual_data.link); |
| 334 EXPECT_EQ("No thanks, hide this.", actual_data.expire); |
| 335 EXPECT_EQ(AppsPromo::USERS_NEW, actual_data.user_group); |
| 336 |
| 337 // Logos are the default values. |
| 338 EXPECT_EQ(GURL("chrome://theme/IDR_WEBSTORE_ICON"), actual_data.logo); |
| 339 EXPECT_EQ(GURL(""), AppsPromo::GetSourcePromoLogoURL()); |
| 340 } |
| 341 |
| 342 // Tests that we don't download images over HTTP. |
| 343 TEST_F(PromoResourceServiceTest, UnpackWebStoreSignalHttpLogo) { |
| 344 web_resource_service_->set_channel(chrome::VersionInfo::CHANNEL_DEV); |
| 345 |
| 346 // Use an HTTP URL. |
| 347 std::string logo_url = "http://www.google.com/image/test.png"; |
| 348 std::string png_data = "!$#%,./nvl;iadh9oh82"; |
| 349 std::string png_base64 = "ISQjJSwuL252bDtpYWRoOW9oODI="; |
| 350 |
| 351 FakeURLFetcherFactory factory; |
| 352 factory.SetFakeResponse(logo_url, png_data, true); |
| 353 |
| 354 std::string json = |
| 355 "{ " |
| 356 " \"topic\": {" |
| 357 " \"answers\": [" |
| 358 " {" |
| 359 " \"answer_id\": \"340252\"," |
| 360 " \"name\": \"webstore_promo:15:1:" + logo_url + "\"," |
| 361 " \"question\": \"Header!\"," |
| 362 " \"inproduct_target\": \"The button label!\"," |
| 363 " \"inproduct\": \"http://link.com\"," |
| 364 " \"tooltip\": \"No thanks, hide this.\"" |
| 365 " }" |
| 366 " ]" |
| 367 " }" |
| 368 "}"; |
| 369 |
| 370 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( |
| 371 base::JSONReader::Read(json, false))); |
| 372 |
| 373 web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); |
| 374 |
| 375 loop_.RunAllPending(); |
| 376 |
| 377 // Reset this scoped_ptr to prevent a DCHECK. |
| 378 web_resource_service_->apps_promo_logo_fetcher_.reset(); |
| 379 |
| 380 AppsPromo::PromoData actual_data = AppsPromo::GetPromo(); |
| 381 EXPECT_EQ("340252", actual_data.id); |
| 382 EXPECT_EQ("Header!", actual_data.header); |
| 383 EXPECT_EQ("The button label!", actual_data.button); |
| 384 EXPECT_EQ(GURL("http://link.com"), actual_data.link); |
| 385 EXPECT_EQ("No thanks, hide this.", actual_data.expire); |
| 386 EXPECT_EQ(AppsPromo::USERS_NEW, actual_data.user_group); |
| 387 |
| 388 // Logos should be the default values because HTTP URLs are not valid. |
| 389 EXPECT_EQ(GURL("chrome://theme/IDR_WEBSTORE_ICON"), actual_data.logo); |
| 390 EXPECT_EQ(GURL(""), AppsPromo::GetSourcePromoLogoURL()); |
| 391 } |
| 392 |
233 TEST_F(PromoResourceServiceTest, IsBuildTargeted) { | 393 TEST_F(PromoResourceServiceTest, IsBuildTargeted) { |
234 // canary | 394 // canary |
235 const chrome::VersionInfo::Channel canary = | 395 const chrome::VersionInfo::Channel canary = |
236 chrome::VersionInfo::CHANNEL_CANARY; | 396 chrome::VersionInfo::CHANNEL_CANARY; |
237 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 1)); | 397 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 1)); |
238 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 3)); | 398 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 3)); |
239 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 7)); | 399 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 7)); |
240 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 15)); | 400 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 15)); |
241 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 8)); | 401 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 8)); |
242 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 11)); | 402 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 11)); |
(...skipping 22 matching lines...) Expand all Loading... |
265 const chrome::VersionInfo::Channel stable = | 425 const chrome::VersionInfo::Channel stable = |
266 chrome::VersionInfo::CHANNEL_STABLE; | 426 chrome::VersionInfo::CHANNEL_STABLE; |
267 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 1)); | 427 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 1)); |
268 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 3)); | 428 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 3)); |
269 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 7)); | 429 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 7)); |
270 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 15)); | 430 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 15)); |
271 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 8)); | 431 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 8)); |
272 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 11)); | 432 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 11)); |
273 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 12)); | 433 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 12)); |
274 } | 434 } |
OLD | NEW |