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: |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 " }" | 184 " }" |
184 " ]" | 185 " ]" |
185 " }" | 186 " }" |
186 "}"; | 187 "}"; |
187 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( | 188 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( |
188 base::JSONReader::Read(json, false))); | 189 base::JSONReader::Read(json, false))); |
189 | 190 |
190 // Initialize a message loop for this to run on. | 191 // Initialize a message loop for this to run on. |
191 MessageLoop loop; | 192 MessageLoop loop; |
192 | 193 |
| 194 // Set the original logo URL to verify that it gets cleared. |
| 195 AppsPromo::SetPromoLogoOriginal(GURL("https://www.google.com/test.png")); |
| 196 |
193 // Check that prefs are set correctly. | 197 // Check that prefs are set correctly. |
194 web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); | 198 web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); |
195 PrefService* prefs = profile_.GetPrefs(); | |
196 ASSERT_TRUE(prefs != NULL); | |
197 | 199 |
198 EXPECT_EQ("341252", AppsPromo::GetPromoId()); | 200 AppsPromo::PromoData actual_data = AppsPromo::GetPromo(); |
199 EXPECT_EQ("The header!", AppsPromo::GetPromoHeaderText()); | 201 EXPECT_EQ("341252", actual_data.id); |
200 EXPECT_EQ("The button label!", AppsPromo::GetPromoButtonText()); | 202 EXPECT_EQ("The header!", actual_data.header); |
201 EXPECT_EQ(GURL("http://link.com"), AppsPromo::GetPromoLink()); | 203 EXPECT_EQ("The button label!", actual_data.button); |
202 EXPECT_EQ("No thanks, hide this.", AppsPromo::GetPromoExpireText()); | 204 EXPECT_EQ(GURL("http://link.com"), actual_data.link); |
203 EXPECT_EQ(AppsPromo::USERS_NEW, AppsPromo::GetPromoUserGroup()); | 205 EXPECT_EQ("No thanks, hide this.", actual_data.expire); |
204 EXPECT_EQ(GURL("chrome://theme/IDR_WEBSTORE_ICON"), | 206 EXPECT_EQ(AppsPromo::USERS_NEW, actual_data.user_group); |
205 AppsPromo::GetPromoLogo()); | 207 |
| 208 // When we don't download a logo, we revert to the default and clear the |
| 209 // original pref. |
| 210 EXPECT_EQ(GURL("chrome://theme/IDR_WEBSTORE_ICON"), actual_data.logo); |
| 211 EXPECT_EQ(GURL(""), AppsPromo::GetPromoLogoOriginal()); |
206 } | 212 } |
207 | 213 |
208 // Tests that the "web store active" flag is set even when the web store promo | 214 // Tests that the "web store active" flag is set even when the web store promo |
209 // fails parsing. | 215 // fails parsing. |
210 TEST_F(PromoResourceServiceTest, UnpackPartialWebStoreSignal) { | 216 TEST_F(PromoResourceServiceTest, UnpackPartialWebStoreSignal) { |
211 std::string json = "{ " | 217 std::string json = "{ " |
212 " \"topic\": {" | 218 " \"topic\": {" |
213 " \"answers\": [" | 219 " \"answers\": [" |
214 " {" | 220 " {" |
215 " \"answer_id\": \"sdlfj32\"," | 221 " \"answer_id\": \"sdlfj32\"," |
216 " \"name\": \"webstore_promo:#klsdjlfSD\"" | 222 " \"name\": \"webstore_promo:#klsdjlfSD\"" |
217 " }" | 223 " }" |
218 " ]" | 224 " ]" |
219 " }" | 225 " }" |
220 "}"; | 226 "}"; |
221 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( | 227 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( |
222 base::JSONReader::Read(json, false))); | 228 base::JSONReader::Read(json, false))); |
223 | 229 |
224 // Initialize a message loop for this to run on. | 230 // Initialize a message loop for this to run on. |
225 MessageLoop loop; | 231 MessageLoop loop; |
226 | 232 |
227 // Check that prefs are set correctly. | 233 // Check that prefs are set correctly. |
228 web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); | 234 web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); |
229 EXPECT_FALSE(AppsPromo::IsPromoSupportedForLocale()); | 235 EXPECT_FALSE(AppsPromo::IsPromoSupportedForLocale()); |
230 EXPECT_TRUE(AppsPromo::IsWebStoreSupportedForLocale()); | 236 EXPECT_TRUE(AppsPromo::IsWebStoreSupportedForLocale()); |
231 } | 237 } |
232 | 238 |
| 239 // Tests that we can successfully unpack web store signals with https |
| 240 // logos. |
| 241 TEST_F(PromoResourceServiceTest, UnpackWebStoreSignalHttpsLogo) { |
| 242 web_resource_service_->set_channel(chrome::VersionInfo::CHANNEL_DEV); |
| 243 |
| 244 std::string logo_url = "https://www.google.com/image/test.png"; |
| 245 std::string png_data = "!$#%,./nvl;iadh9oh82"; |
| 246 std::string png_base64 = "data:image/png;base64,ISQjJSwuL252bDtpYWRoOW9oODI="; |
| 247 |
| 248 MessageLoop message_loop; |
| 249 FakeURLFetcherFactory factory; |
| 250 factory.SetFakeResponse(logo_url, png_data, true); |
| 251 |
| 252 std::string json = |
| 253 "{ " |
| 254 " \"topic\": {" |
| 255 " \"answers\": [" |
| 256 " {" |
| 257 " \"answer_id\": \"340252\"," |
| 258 " \"name\": \"webstore_promo:15:1:" + logo_url + "\"," |
| 259 " \"question\": \"Header!\"," |
| 260 " \"inproduct_target\": \"The button label!\"," |
| 261 " \"inproduct\": \"http://link.com\"," |
| 262 " \"tooltip\": \"No thanks, hide this.\"" |
| 263 " }" |
| 264 " ]" |
| 265 " }" |
| 266 "}"; |
| 267 |
| 268 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( |
| 269 base::JSONReader::Read(json, false))); |
| 270 |
| 271 web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); |
| 272 |
| 273 message_loop.RunAllPending(); |
| 274 |
| 275 AppsPromo::PromoData actual_data = AppsPromo::GetPromo(); |
| 276 EXPECT_EQ("340252", actual_data.id); |
| 277 EXPECT_EQ("Header!", actual_data.header); |
| 278 EXPECT_EQ("The button label!", actual_data.button); |
| 279 EXPECT_EQ(GURL("http://link.com"), actual_data.link); |
| 280 EXPECT_EQ("No thanks, hide this.", actual_data.expire); |
| 281 EXPECT_EQ(AppsPromo::USERS_NEW, actual_data.user_group); |
| 282 |
| 283 // The logo should now be a base64 data URL. |
| 284 EXPECT_EQ(GURL(png_base64), actual_data.logo); |
| 285 |
| 286 // And the original pref should hold the original https URL. |
| 287 EXPECT_EQ(GURL(logo_url), AppsPromo::GetPromoLogoOriginal()); |
| 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 MessageLoop message_loop; |
| 299 FakeURLFetcherFactory factory; |
| 300 |
| 301 // Have URLFetcher return a 500 error. |
| 302 factory.SetFakeResponse(logo_url, png_data, false); |
| 303 |
| 304 std::string json = |
| 305 "{ " |
| 306 " \"topic\": {" |
| 307 " \"answers\": [" |
| 308 " {" |
| 309 " \"answer_id\": \"340252\"," |
| 310 " \"name\": \"webstore_promo:15:1:" + logo_url + "\"," |
| 311 " \"question\": \"Header!\"," |
| 312 " \"inproduct_target\": \"The button label!\"," |
| 313 " \"inproduct\": \"http://link.com\"," |
| 314 " \"tooltip\": \"No thanks, hide this.\"" |
| 315 " }" |
| 316 " ]" |
| 317 " }" |
| 318 "}"; |
| 319 |
| 320 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( |
| 321 base::JSONReader::Read(json, false))); |
| 322 |
| 323 web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); |
| 324 |
| 325 message_loop.RunAllPending(); |
| 326 |
| 327 AppsPromo::PromoData actual_data = AppsPromo::GetPromo(); |
| 328 EXPECT_EQ("340252", actual_data.id); |
| 329 EXPECT_EQ("Header!", actual_data.header); |
| 330 EXPECT_EQ("The button label!", actual_data.button); |
| 331 EXPECT_EQ(GURL("http://link.com"), actual_data.link); |
| 332 EXPECT_EQ("No thanks, hide this.", actual_data.expire); |
| 333 EXPECT_EQ(AppsPromo::USERS_NEW, actual_data.user_group); |
| 334 |
| 335 // Logos are the default values. |
| 336 EXPECT_EQ(GURL("chrome://theme/IDR_WEBSTORE_ICON"), actual_data.logo); |
| 337 EXPECT_EQ(GURL(""), AppsPromo::GetPromoLogoOriginal()); |
| 338 } |
| 339 |
| 340 // Tests that we don't download images over http. |
| 341 TEST_F(PromoResourceServiceTest, UnpackWebStoreSignalHttpLogo) { |
| 342 web_resource_service_->set_channel(chrome::VersionInfo::CHANNEL_DEV); |
| 343 |
| 344 // Use an http URL. |
| 345 std::string logo_url = "http://www.google.com/image/test.png"; |
| 346 std::string png_data = "!$#%,./nvl;iadh9oh82"; |
| 347 std::string png_base64 = "ISQjJSwuL252bDtpYWRoOW9oODI="; |
| 348 |
| 349 MessageLoop message_loop; |
| 350 FakeURLFetcherFactory factory; |
| 351 factory.SetFakeResponse(logo_url, png_data, true); |
| 352 |
| 353 std::string json = |
| 354 "{ " |
| 355 " \"topic\": {" |
| 356 " \"answers\": [" |
| 357 " {" |
| 358 " \"answer_id\": \"340252\"," |
| 359 " \"name\": \"webstore_promo:15:1:" + logo_url + "\"," |
| 360 " \"question\": \"Header!\"," |
| 361 " \"inproduct_target\": \"The button label!\"," |
| 362 " \"inproduct\": \"http://link.com\"," |
| 363 " \"tooltip\": \"No thanks, hide this.\"" |
| 364 " }" |
| 365 " ]" |
| 366 " }" |
| 367 "}"; |
| 368 |
| 369 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( |
| 370 base::JSONReader::Read(json, false))); |
| 371 |
| 372 web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); |
| 373 |
| 374 message_loop.RunAllPending(); |
| 375 |
| 376 AppsPromo::PromoData actual_data = AppsPromo::GetPromo(); |
| 377 EXPECT_EQ("340252", actual_data.id); |
| 378 EXPECT_EQ("Header!", actual_data.header); |
| 379 EXPECT_EQ("The button label!", actual_data.button); |
| 380 EXPECT_EQ(GURL("http://link.com"), actual_data.link); |
| 381 EXPECT_EQ("No thanks, hide this.", actual_data.expire); |
| 382 EXPECT_EQ(AppsPromo::USERS_NEW, actual_data.user_group); |
| 383 |
| 384 // Logos should be the default values because http URLs are not valid. |
| 385 EXPECT_EQ(GURL("chrome://theme/IDR_WEBSTORE_ICON"), actual_data.logo); |
| 386 EXPECT_EQ(GURL(""), AppsPromo::GetPromoLogoOriginal()); |
| 387 } |
| 388 |
233 TEST_F(PromoResourceServiceTest, IsBuildTargeted) { | 389 TEST_F(PromoResourceServiceTest, IsBuildTargeted) { |
234 // canary | 390 // canary |
235 const chrome::VersionInfo::Channel canary = | 391 const chrome::VersionInfo::Channel canary = |
236 chrome::VersionInfo::CHANNEL_CANARY; | 392 chrome::VersionInfo::CHANNEL_CANARY; |
237 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 1)); | 393 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 1)); |
238 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 3)); | 394 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 3)); |
239 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 7)); | 395 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 7)); |
240 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 15)); | 396 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 15)); |
241 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 8)); | 397 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 8)); |
242 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 11)); | 398 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 11)); |
(...skipping 22 matching lines...) Expand all Loading... |
265 const chrome::VersionInfo::Channel stable = | 421 const chrome::VersionInfo::Channel stable = |
266 chrome::VersionInfo::CHANNEL_STABLE; | 422 chrome::VersionInfo::CHANNEL_STABLE; |
267 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 1)); | 423 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 1)); |
268 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 3)); | 424 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 3)); |
269 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 7)); | 425 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 7)); |
270 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 15)); | 426 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 15)); |
271 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 8)); | 427 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 8)); |
272 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 11)); | 428 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 11)); |
273 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 12)); | 429 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 12)); |
274 } | 430 } |
OLD | NEW |