| 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 web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); |
| 263 |
| 264 loop_.RunAllPending(); |
| 265 |
| 266 // Reset this scoped_ptr to prevent a DCHECK. |
| 267 web_resource_service_->apps_promo_logo_downloader_.reset(); |
| 268 |
| 269 AppsPromo::PromoData actual_data = AppsPromo::GetPromo(); |
| 270 EXPECT_EQ("340252", actual_data.id); |
| 271 EXPECT_EQ("Header!", actual_data.header); |
| 272 EXPECT_EQ("The button label!", actual_data.button); |
| 273 EXPECT_EQ(GURL("http://link.com"), actual_data.link); |
| 274 EXPECT_EQ("No thanks, hide this.", actual_data.expire); |
| 275 EXPECT_EQ(AppsPromo::USERS_NEW, actual_data.user_group); |
| 276 |
| 277 // The logo should now be a base64 DATA URL. |
| 278 EXPECT_EQ(GURL(png_base64), actual_data.logo); |
| 279 |
| 280 // And the source pref should hold the source HTTPS URL. |
| 281 EXPECT_EQ(GURL(logo_url), AppsPromo::GetSourcePromoLogoURL()); |
| 282 } |
| 283 |
| 284 // Tests that we revert to the default logo when the fetch fails. |
| 285 TEST_F(PromoResourceServiceTest, UnpackWebStoreSignalHttpsLogoError) { |
| 286 web_resource_service_->set_channel(chrome::VersionInfo::CHANNEL_DEV); |
| 287 |
| 288 std::string logo_url = "https://www.google.com/image/test.png"; |
| 289 std::string png_data = "!$#%,./nvl;iadh9oh82"; |
| 290 std::string png_base64 = "ISQjJSwuL252bDtpYWRoOW9oODI="; |
| 291 |
| 292 FakeURLFetcherFactory factory; |
| 293 |
| 294 // Have URLFetcher return a 500 error. |
| 295 factory.SetFakeResponse(logo_url, png_data, false); |
| 296 |
| 297 std::string json = |
| 298 "{ " |
| 299 " \"topic\": {" |
| 300 " \"answers\": [" |
| 301 " {" |
| 302 " \"answer_id\": \"340252\"," |
| 303 " \"name\": \"webstore_promo:15:1:" + logo_url + "\"," |
| 304 " \"question\": \"Header!\"," |
| 305 " \"inproduct_target\": \"The button label!\"," |
| 306 " \"inproduct\": \"http://link.com\"," |
| 307 " \"tooltip\": \"No thanks, hide this.\"" |
| 308 " }" |
| 309 " ]" |
| 310 " }" |
| 311 "}"; |
| 312 |
| 313 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( |
| 314 base::JSONReader::Read(json, false))); |
| 315 |
| 316 web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); |
| 317 |
| 318 loop_.RunAllPending(); |
| 319 |
| 320 // Reset this scoped_ptr to prevent a DCHECK. |
| 321 web_resource_service_->apps_promo_logo_downloader_.reset(); |
| 322 |
| 323 AppsPromo::PromoData actual_data = AppsPromo::GetPromo(); |
| 324 EXPECT_EQ("340252", actual_data.id); |
| 325 EXPECT_EQ("Header!", actual_data.header); |
| 326 EXPECT_EQ("The button label!", actual_data.button); |
| 327 EXPECT_EQ(GURL("http://link.com"), actual_data.link); |
| 328 EXPECT_EQ("No thanks, hide this.", actual_data.expire); |
| 329 EXPECT_EQ(AppsPromo::USERS_NEW, actual_data.user_group); |
| 330 |
| 331 // Logos are the default values. |
| 332 EXPECT_EQ(GURL("chrome://theme/IDR_WEBSTORE_ICON"), actual_data.logo); |
| 333 EXPECT_EQ(GURL(""), AppsPromo::GetSourcePromoLogoURL()); |
| 334 } |
| 335 |
| 336 // Tests that we don't download images over HTTP. |
| 337 TEST_F(PromoResourceServiceTest, UnpackWebStoreSignalHttpLogo) { |
| 338 web_resource_service_->set_channel(chrome::VersionInfo::CHANNEL_DEV); |
| 339 |
| 340 // Use an HTTP URL. |
| 341 std::string logo_url = "http://www.google.com/image/test.png"; |
| 342 std::string png_data = "!$#%,./nvl;iadh9oh82"; |
| 343 std::string png_base64 = "ISQjJSwuL252bDtpYWRoOW9oODI="; |
| 344 |
| 345 FakeURLFetcherFactory factory; |
| 346 factory.SetFakeResponse(logo_url, png_data, true); |
| 347 |
| 348 std::string json = |
| 349 "{ " |
| 350 " \"topic\": {" |
| 351 " \"answers\": [" |
| 352 " {" |
| 353 " \"answer_id\": \"340252\"," |
| 354 " \"name\": \"webstore_promo:15:1:" + logo_url + "\"," |
| 355 " \"question\": \"Header!\"," |
| 356 " \"inproduct_target\": \"The button label!\"," |
| 357 " \"inproduct\": \"http://link.com\"," |
| 358 " \"tooltip\": \"No thanks, hide this.\"" |
| 359 " }" |
| 360 " ]" |
| 361 " }" |
| 362 "}"; |
| 363 |
| 364 scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( |
| 365 base::JSONReader::Read(json, false))); |
| 366 |
| 367 web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); |
| 368 |
| 369 loop_.RunAllPending(); |
| 370 |
| 371 // Reset this scoped_ptr to prevent a DCHECK. |
| 372 web_resource_service_->apps_promo_logo_downloader_.reset(); |
| 373 |
| 374 AppsPromo::PromoData actual_data = AppsPromo::GetPromo(); |
| 375 EXPECT_EQ("340252", actual_data.id); |
| 376 EXPECT_EQ("Header!", actual_data.header); |
| 377 EXPECT_EQ("The button label!", actual_data.button); |
| 378 EXPECT_EQ(GURL("http://link.com"), actual_data.link); |
| 379 EXPECT_EQ("No thanks, hide this.", actual_data.expire); |
| 380 EXPECT_EQ(AppsPromo::USERS_NEW, actual_data.user_group); |
| 381 |
| 382 // Logos should be the default values because HTTP URLs are not valid. |
| 383 EXPECT_EQ(GURL("chrome://theme/IDR_WEBSTORE_ICON"), actual_data.logo); |
| 384 EXPECT_EQ(GURL(""), AppsPromo::GetSourcePromoLogoURL()); |
| 385 } |
| 386 |
| 233 TEST_F(PromoResourceServiceTest, IsBuildTargeted) { | 387 TEST_F(PromoResourceServiceTest, IsBuildTargeted) { |
| 234 // canary | 388 // canary |
| 235 const chrome::VersionInfo::Channel canary = | 389 const chrome::VersionInfo::Channel canary = |
| 236 chrome::VersionInfo::CHANNEL_CANARY; | 390 chrome::VersionInfo::CHANNEL_CANARY; |
| 237 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 1)); | 391 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 1)); |
| 238 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 3)); | 392 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 3)); |
| 239 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 7)); | 393 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 7)); |
| 240 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 15)); | 394 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 15)); |
| 241 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 8)); | 395 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 8)); |
| 242 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 11)); | 396 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 11)); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 265 const chrome::VersionInfo::Channel stable = | 419 const chrome::VersionInfo::Channel stable = |
| 266 chrome::VersionInfo::CHANNEL_STABLE; | 420 chrome::VersionInfo::CHANNEL_STABLE; |
| 267 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 1)); | 421 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 1)); |
| 268 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 3)); | 422 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 3)); |
| 269 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 7)); | 423 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 7)); |
| 270 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 15)); | 424 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 15)); |
| 271 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 8)); | 425 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 8)); |
| 272 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 11)); | 426 EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 11)); |
| 273 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 12)); | 427 EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 12)); |
| 274 } | 428 } |
| OLD | NEW |