diff options
| author | achuith@chromium.org <achuith@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-08 08:04:40 +0000 |
|---|---|---|
| committer | achuith@chromium.org <achuith@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-06-08 08:04:40 +0000 |
| commit | dcea9dc8c475908bc6432912ee4229fa78bb6113 (patch) | |
| tree | da2390f7056f2d6c463899c2e00b53bf3559311b | |
| parent | 583b4fc28d0dabb780105f8c68840edd0280523e (diff) | |
| download | chromium_src-dcea9dc8c475908bc6432912ee4229fa78bb6113.zip chromium_src-dcea9dc8c475908bc6432912ee4229fa78bb6113.tar.gz chromium_src-dcea9dc8c475908bc6432912ee4229fa78bb6113.tar.bz2 | |
Purge Logo/WebstoreSignal.
BUG=NONE
TEST=unit tests pass.
TBR=dbeam@chromium.org
Review URL: https://chromiumcodereview.appspot.com/10539045
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@141183 0039d316-1c4b-4281-b951-d872f2087c98
4 files changed, 17 insertions, 675 deletions
diff --git a/chrome/browser/web_resource/notification_promo.cc b/chrome/browser/web_resource/notification_promo.cc index 204c871..d5633a1 100644 --- a/chrome/browser/web_resource/notification_promo.cc +++ b/chrome/browser/web_resource/notification_promo.cc @@ -50,8 +50,10 @@ const char* PlatformString() { // Returns a string suitable for the Promo Server URL 'dist' value. const char* ChannelString() { + // GetChannel hits the registry on Windows. See http://crbug.com/70898. + base::ThreadRestrictions::ScopedAllowIO allow_io; const chrome::VersionInfo::Channel channel = - PromoResourceService::GetChannel(); + chrome::VersionInfo::GetChannel(); switch (channel) { case chrome::VersionInfo::CHANNEL_CANARY: return "canary"; diff --git a/chrome/browser/web_resource/promo_resource_service.cc b/chrome/browser/web_resource/promo_resource_service.cc index a1113a1..348e438 100644 --- a/chrome/browser/web_resource/promo_resource_service.cc +++ b/chrome/browser/web_resource/promo_resource_service.cc @@ -5,20 +5,14 @@ #include "chrome/browser/web_resource/promo_resource_service.h" #include "base/command_line.h" -#include "base/rand_util.h" -#include "base/string_number_conversions.h" +#include "base/message_loop.h" #include "base/threading/thread_restrictions.h" -#include "base/time.h" -#include "base/utf_string_conversions.h" #include "base/values.h" #include "chrome/browser/browser_process.h" -#include "chrome/browser/extensions/apps_promo.h" #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/profiles/profile.h" -#include "chrome/browser/sync/sync_ui_util.h" #include "chrome/common/chrome_notification_types.h" #include "chrome/common/chrome_switches.h" -#include "chrome/common/chrome_version_info.h" #include "chrome/common/pref_names.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/notification_service.h" @@ -37,13 +31,6 @@ static const int kTestCacheUpdateDelay = 3 * 60 * 1000; // to versions with different types of promos). static const int kPromoServiceVersion = 7; -// Properties used by the server. -static const char kAnswerIdProperty[] = "answer_id"; -static const char kWebStoreHeaderProperty[] = "question"; -static const char kWebStoreButtonProperty[] = "inproduct_target"; -static const char kWebStoreLinkProperty[] = "inproduct"; -static const char kWebStoreExpireProperty[] = "tooltip"; - GURL GetPromoResourceURL() { const std::string promo_server_url = CommandLine::ForCurrentProcess()-> GetSwitchValueASCII(switches::kPromoServerURL); @@ -72,44 +59,17 @@ void PromoResourceService::RegisterUserPrefs(PrefService* prefs) { prefs->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate, "0", PrefService::UNSYNCABLE_PREF); + NotificationPromo::RegisterUserPrefs(prefs); + + // TODO(achuith): Delete this in M21 prefs->RegisterDoublePref(prefs::kNtpCustomLogoStart, 0, PrefService::UNSYNCABLE_PREF); prefs->RegisterDoublePref(prefs::kNtpCustomLogoEnd, 0, PrefService::UNSYNCABLE_PREF); - NotificationPromo::RegisterUserPrefs(prefs); -} - -// static -chrome::VersionInfo::Channel PromoResourceService::GetChannel() { - // GetChannel hits the registry on Windows. See http://crbug.com/70898. - base::ThreadRestrictions::ScopedAllowIO allow_io; - return chrome::VersionInfo::GetChannel(); -} - -// static -bool PromoResourceService::IsBuildTargeted(chrome::VersionInfo::Channel channel, - int builds_allowed) { - if (builds_allowed == NO_BUILD || - builds_allowed < 0 || - builds_allowed > ALL_BUILDS) { - return false; - } - switch (channel) { - case chrome::VersionInfo::CHANNEL_CANARY: - return (CANARY_BUILD & builds_allowed) != 0; - case chrome::VersionInfo::CHANNEL_DEV: - return (DEV_BUILD & builds_allowed) != 0; - case chrome::VersionInfo::CHANNEL_BETA: - return (BETA_BUILD & builds_allowed) != 0; - case chrome::VersionInfo::CHANNEL_STABLE: - return (STABLE_BUILD & builds_allowed) != 0; - default: - // Show promos for local builds when using a custom promo URL. - return CommandLine::ForCurrentProcess()->HasSwitch( - switches::kPromoServerURL); - } + prefs->ClearPref(prefs::kNtpCustomLogoStart); + prefs->ClearPref(prefs::kNtpCustomLogoEnd); } PromoResourceService::PromoResourceService(Profile* profile) @@ -120,34 +80,22 @@ PromoResourceService::PromoResourceService(Profile* profile) kStartResourceFetchDelay, GetCacheUpdateDelay()), profile_(profile), - channel_(chrome::VersionInfo::CHANNEL_UNKNOWN), - ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)), - web_resource_update_scheduled_(false) { + ALLOW_THIS_IN_INITIALIZER_LIST( + weak_ptr_factory_(this)), + web_resource_update_scheduled_(false) { ScheduleNotificationOnInit(); } -PromoResourceService::~PromoResourceService() { } - -bool PromoResourceService::IsBuildTargeted(int builds_targeted) { - if (channel_ == chrome::VersionInfo::CHANNEL_UNKNOWN) - channel_ = GetChannel(); - - return IsBuildTargeted(channel_, builds_targeted); -} - -void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { - UnpackLogoSignal(parsed_json); - UnpackNotificationSignal(parsed_json); - UnpackWebStoreSignal(parsed_json); +PromoResourceService::~PromoResourceService() { } void PromoResourceService::ScheduleNotification(double promo_start, double promo_end) { if (promo_start > 0 && promo_end > 0) { - int64 ms_until_start = + const int64 ms_until_start = static_cast<int64>((base::Time::FromDoubleT( promo_start) - base::Time::Now()).InMilliseconds()); - int64 ms_until_end = + const int64 ms_until_end = static_cast<int64>((base::Time::FromDoubleT( promo_end) - base::Time::Now()).InMilliseconds()); if (ms_until_start > 0) { @@ -177,7 +125,6 @@ void PromoResourceService::ScheduleNotificationOnInit() { local_state->SetInteger(prefs::kNtpPromoVersion, kPromoServiceVersion); local_state->SetString(prefs::kNtpPromoLocale, locale); prefs_->ClearPref(prefs::kNtpPromoResourceCacheUpdate); - AppsPromo::ClearPromo(); PostNotification(0); } else { // If the promo start is in the future, set a notification task to @@ -224,8 +171,7 @@ std::string PromoResourceService::GetPromoLocale() { return local_state->GetString(prefs::kNtpPromoLocale); } -void PromoResourceService::UnpackNotificationSignal( - const DictionaryValue& parsed_json) { +void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { NotificationPromo notification_promo(profile_); notification_promo.InitFromJson(parsed_json); @@ -240,154 +186,3 @@ bool PromoResourceService::CanShowNotificationPromo(Profile* profile) { notification_promo.InitFromPrefs(); return notification_promo.CanShow(); } - -void PromoResourceService::UnpackWebStoreSignal( - const DictionaryValue& parsed_json) { - DictionaryValue* topic_dict; - ListValue* answer_list; - - bool is_webstore_active = false; - bool signal_found = false; - AppsPromo::PromoData promo_data; - std::string promo_link = ""; - std::string promo_logo = ""; - int target_builds = 0; - - if (!parsed_json.GetDictionary("topic", &topic_dict) || - !topic_dict->GetList("answers", &answer_list)) - return; - - for (ListValue::const_iterator answer_iter = answer_list->begin(); - answer_iter != answer_list->end(); ++answer_iter) { - if (!(*answer_iter)->IsType(Value::TYPE_DICTIONARY)) - continue; - DictionaryValue* a_dic = - static_cast<DictionaryValue*>(*answer_iter); - - // The "name" field has three different values packed into it, each - // separated by a ':'. - std::string name; - if (!a_dic->GetString("name", &name)) - continue; - - // (1) the string "webstore_promo" - size_t split = name.find(":"); - if (split == std::string::npos || name.substr(0, split) != "webstore_promo") - continue; - - // If the "webstore_promo" string was found, that's enough to activate the - // apps section even if the rest of the promo fails parsing. - is_webstore_active = true; - - // (2) an integer specifying which builds the promo targets - name = name.substr(split+1); - split = name.find(':'); - if (split == std::string::npos || - !base::StringToInt(name.substr(0, split), &target_builds)) - continue; - - // (3) an integer specifying what users should maximize the promo - name = name.substr(split+1); - split = name.find(':'); - if (split == std::string::npos || - !base::StringToInt(name.substr(0, split), &promo_data.user_group)) - continue; - - // (4) optional text that specifies a URL of a logo image - promo_logo = name.substr(split+1); - - if (!a_dic->GetString(kAnswerIdProperty, &promo_data.id) || - !a_dic->GetString(kWebStoreHeaderProperty, &promo_data.header) || - !a_dic->GetString(kWebStoreButtonProperty, &promo_data.button) || - !a_dic->GetString(kWebStoreLinkProperty, &promo_link) || - !a_dic->GetString(kWebStoreExpireProperty, &promo_data.expire)) - continue; - - if (IsBuildTargeted(target_builds)) { - // The downloader will set the promo prefs and send the - // NOTIFICATION_WEB_STORE_PROMO_LOADED notification. - promo_data.link = GURL(promo_link); - promo_data.logo = GURL(promo_logo); - apps_promo_logo_fetcher_.reset( - new AppsPromoLogoFetcher(profile_, promo_data)); - signal_found = true; - break; - } - } - - if (!signal_found) { - // If no web store promos target this build, then clear all the prefs. - AppsPromo::ClearPromo(); - } - - AppsPromo::SetWebStoreSupportedForLocale(is_webstore_active); - - return; -} - -void PromoResourceService::UnpackLogoSignal( - const DictionaryValue& parsed_json) { - DictionaryValue* topic_dict; - ListValue* answer_list; - double old_logo_start = 0; - double old_logo_end = 0; - double logo_start = 0; - double logo_end = 0; - - // Check for preexisting start and end values. - if (prefs_->HasPrefPath(prefs::kNtpCustomLogoStart) && - prefs_->HasPrefPath(prefs::kNtpCustomLogoEnd)) { - old_logo_start = prefs_->GetDouble(prefs::kNtpCustomLogoStart); - old_logo_end = prefs_->GetDouble(prefs::kNtpCustomLogoEnd); - } - - // Check for newly received start and end values. - if (parsed_json.GetDictionary("topic", &topic_dict)) { - if (topic_dict->GetList("answers", &answer_list)) { - std::string logo_start_string = ""; - std::string logo_end_string = ""; - for (ListValue::const_iterator answer_iter = answer_list->begin(); - answer_iter != answer_list->end(); ++answer_iter) { - if (!(*answer_iter)->IsType(Value::TYPE_DICTIONARY)) - continue; - DictionaryValue* a_dic = - static_cast<DictionaryValue*>(*answer_iter); - std::string logo_signal; - if (a_dic->GetString("name", &logo_signal)) { - if (logo_signal == "custom_logo_start") { - a_dic->GetString("inproduct", &logo_start_string); - } else if (logo_signal == "custom_logo_end") { - a_dic->GetString("inproduct", &logo_end_string); - } - } - } - if (!logo_start_string.empty() && - logo_start_string.length() > 0 && - !logo_end_string.empty() && - logo_end_string.length() > 0) { - base::Time start_time; - base::Time end_time; - if (base::Time::FromString(logo_start_string.c_str(), &start_time) && - base::Time::FromString(logo_end_string.c_str(), &end_time)) { - logo_start = start_time.ToDoubleT(); - logo_end = end_time.ToDoubleT(); - } - } - } - } - - // If logo start or end times have changed, trigger a new web resource - // notification, so that the logo on the NTP is updated. This check is - // outside the reading of the web resource data, because the absence of - // dates counts as a triggering change if there were dates before. - if (!(old_logo_start == logo_start) || - !(old_logo_end == logo_end)) { - prefs_->SetDouble(prefs::kNtpCustomLogoStart, logo_start); - prefs_->SetDouble(prefs::kNtpCustomLogoEnd, logo_end); - content::NotificationService* service = - content::NotificationService::current(); - service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, - content::Source<WebResourceService>(this), - content::NotificationService::NoDetails()); - } -} diff --git a/chrome/browser/web_resource/promo_resource_service.h b/chrome/browser/web_resource/promo_resource_service.h index e4db630..8812cdb 100644 --- a/chrome/browser/web_resource/promo_resource_service.h +++ b/chrome/browser/web_resource/promo_resource_service.h @@ -11,13 +11,11 @@ #include "base/memory/weak_ptr.h" #include "chrome/browser/web_resource/notification_promo.h" #include "chrome/browser/web_resource/web_resource_service.h" -#include "chrome/common/chrome_version_info.h" namespace base { class DictionaryValue; } -class AppsPromoLogoFetcher; class PrefService; class Profile; @@ -27,17 +25,7 @@ class Profile; // promotional messages to certain groups of Chrome users. class PromoResourceService : public WebResourceService { public: - // Identifies types of Chrome builds for promo targeting. - enum BuildType { - NO_BUILD = 0, - DEV_BUILD = 1, - BETA_BUILD = 1 << 1, - STABLE_BUILD = 1 << 2, - CANARY_BUILD = 1 << 3, - ALL_BUILDS = (1 << 4) - 1, - }; - - // Checks for conditions to show promo: start/end times, channel, etc. + // Checks for conditions to show promo. static bool CanShowNotificationPromo(Profile* profile); static void RegisterPrefs(PrefService* local_state); @@ -46,22 +34,7 @@ class PromoResourceService : public WebResourceService { explicit PromoResourceService(Profile* profile); - static chrome::VersionInfo::Channel GetChannel(); - static bool IsBuildTargeted(chrome::VersionInfo::Channel, int builds_allowed); - private: - FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, IsBuildTargetedTest); - FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, UnpackLogoSignal); - FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, UnpackWebStoreSignal); - FRIEND_TEST_ALL_PREFIXES( - PromoResourceServiceTest, UnpackPartialWebStoreSignal); - FRIEND_TEST_ALL_PREFIXES( - PromoResourceServiceTest, UnpackWebStoreSignalHttpsLogo); - FRIEND_TEST_ALL_PREFIXES( - PromoResourceServiceTest, UnpackWebStoreSignalHttpsLogoError); - FRIEND_TEST_ALL_PREFIXES( - PromoResourceServiceTest, UnpackWebStoreSignalHttpLogo); - virtual ~PromoResourceService(); int GetPromoServiceVersion(); @@ -70,11 +43,6 @@ class PromoResourceService : public WebResourceService { // so we can fetch new data if the locale changes. std::string GetPromoLocale(); - // Returns true if |builds_targeted| includes the release channel Chrome - // belongs to. For testing purposes, you can override the current channel - // with set_channel. - bool IsBuildTargeted(int builds_targeted); - // Schedule a notification that a web resource is either going to become // available or be no longer valid. void ScheduleNotification(double start, double end); @@ -93,81 +61,12 @@ class PromoResourceService : public WebResourceService { // Notify listeners that the state of a web resource has changed. void PromoResourceStateChange(); - // Overrides the current Chrome release channel for testing purposes. - void set_channel(chrome::VersionInfo::Channel channel) { channel_ = channel; } - // WebResourceService override. virtual void Unpack(const base::DictionaryValue& parsed_json) OVERRIDE; - // Unpack the web resource as a custom notification signal. - void UnpackNotificationSignal(const base::DictionaryValue& parsed_json); - - // Unpack the promo resource as a custom logo signal. Expects a start and end - // signal. Delivery will be in json in the form of: - // { - // "topic": { - // "answers": [ - // { - // "answer_id": "107366", - // "name": "custom_logo_start", - // "question": "", - // "tooltip": "", - // "inproduct": "10/8/09 12:00", - // "inproduct_target": null - // }, - // { - // "answer_id": "107366", - // "name": "custom_logo_end", - // "question": "", - // "tooltip": "", - // "inproduct": "10/8/09 12:00", - // "inproduct_target": null - // }, - // ... - // ] - // } - // } - // - void UnpackLogoSignal(const base::DictionaryValue& parsed_json); - - // Unpack the web store promo. Expects JSON delivery in the following format: - // { - // "topic": { - // "answers": [ - // { - // "answer_id": "1143011", - // "name": "webstore_promo:15:1:https://www.google.com/logo.png", - // "question": "Browse thousands of apps and games for Chrome.", - // "inproduct_target": "Visit the Chrome Web Store", - // "inproduct": "https://chrome.google.com/webstore?hl=en", - // "tooltip": "No thanks, hide this" - // }, - // ... - // ] - // } - // } - // The properties are defined as follows: - // inproduct: the release channels targeted (bitwise or of BuildTypes) - // question: the promo header text - // inproduct_target: the promo button text - // inproduct: the promo button link - // tooltip: the text for the "hide this" link on the promo - // name: starts with "webstore_promo" to identify the signal. The second - // part contains the release channels targeted (bitwise or of - // BuildTypes). The third part specifies what users should maximize - // the apps section of the NTP when first loading the promo (bitwise - // or of AppsPromo::UserGroup). The forth part is optional and - // specifies the URL of the logo image. If left out, the default - // webstore logo will be used. The logo can be an HTTPS or DATA URL. - // answer_id: the promo's id - void UnpackWebStoreSignal(const base::DictionaryValue& parsed_json); - // The profile this service belongs to. Profile* profile_; - // Overrides the current Chrome release channel for testing purposes. - chrome::VersionInfo::Channel channel_; - // Allows the creation of tasks to send a notification. // This allows the PromoResourceService to notify the New Tab Page immediately // when a new web resource should be shown or removed. @@ -180,9 +79,6 @@ class PromoResourceService : public WebResourceService { // becomes available. bool web_resource_update_scheduled_; - // A helper that downloads the promo logo. - scoped_ptr<AppsPromoLogoFetcher> apps_promo_logo_fetcher_; - DISALLOW_COPY_AND_ASSIGN(PromoResourceService); }; diff --git a/chrome/browser/web_resource/promo_resource_service_unittest.cc b/chrome/browser/web_resource/promo_resource_service_unittest.cc index f9821b6..0f3fda1 100644 --- a/chrome/browser/web_resource/promo_resource_service_unittest.cc +++ b/chrome/browser/web_resource/promo_resource_service_unittest.cc @@ -9,7 +9,6 @@ #include "base/utf_string_conversions.h" #include "base/values.h" #include "chrome/browser/browser_process.h" -#include "chrome/browser/extensions/apps_promo.h" #include "chrome/browser/prefs/browser_prefs.h" #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/web_resource/notification_promo.h" @@ -38,85 +37,6 @@ class PromoResourceServiceTest : public testing::Test { MessageLoop loop_; }; -// Verifies that custom dates read from a web resource server are written to -// the preferences file. -TEST_F(PromoResourceServiceTest, UnpackLogoSignal) { - // Set up start and end dates in a Dictionary as if parsed from the service. - std::string json = "{ " - " \"topic\": {" - " \"answers\": [" - " {" - " \"name\": \"custom_logo_start\"," - " \"inproduct\": \"31/01/10 01:00 GMT\"" - " }," - " {" - " \"name\": \"custom_logo_end\"," - " \"inproduct\": \"31/01/12 01:00 GMT\"" - " }" - " ]" - " }" - "}"; - scoped_ptr<DictionaryValue> test_json( - static_cast<DictionaryValue*>(base::JSONReader::Read(json))); - - // Check that prefs are set correctly. - web_resource_service_->UnpackLogoSignal(*(test_json.get())); - PrefService* prefs = profile_.GetPrefs(); - ASSERT_TRUE(prefs != NULL); - - double logo_start = - prefs->GetDouble(prefs::kNtpCustomLogoStart); - EXPECT_EQ(logo_start, 1264899600); // unix epoch for Jan 31 2010 0100 GMT. - double logo_end = - prefs->GetDouble(prefs::kNtpCustomLogoEnd); - EXPECT_EQ(logo_end, 1327971600); // unix epoch for Jan 31 2012 0100 GMT. - - // Change the start only and recheck. - json = "{ " - " \"topic\": {" - " \"answers\": [" - " {" - " \"name\": \"custom_logo_start\"," - " \"inproduct\": \"28/02/10 14:00 GMT\"" - " }," - " {" - " \"name\": \"custom_logo_end\"," - " \"inproduct\": \"31/01/12 01:00 GMT\"" - " }" - " ]" - " }" - "}"; - test_json->Clear(); - test_json.reset(static_cast<DictionaryValue*>( - base::JSONReader::Read(json))); - - // Check that prefs are set correctly. - web_resource_service_->UnpackLogoSignal(*(test_json.get())); - - logo_start = prefs->GetDouble(prefs::kNtpCustomLogoStart); - EXPECT_EQ(logo_start, 1267365600); // date changes to Feb 28 2010 1400 GMT. - - // If no date is included in the prefs, reset custom logo dates to 0. - json = "{ " - " \"topic\": {" - " \"answers\": [" - " {" - " }" - " ]" - " }" - "}"; - test_json->Clear(); - test_json.reset(static_cast<DictionaryValue*>( - base::JSONReader::Read(json))); - - // Check that prefs are set correctly. - web_resource_service_->UnpackLogoSignal(*(test_json.get())); - logo_start = prefs->GetDouble(prefs::kNtpCustomLogoStart); - EXPECT_EQ(logo_start, 0); // date value reset to 0; - logo_end = prefs->GetDouble(prefs::kNtpCustomLogoEnd); - EXPECT_EQ(logo_end, 0); // date value reset to 0; -} - class NotificationPromoTest { public: explicit NotificationPromoTest(Profile* profile) @@ -494,274 +414,3 @@ TEST_F(PromoResourceServiceTest, PromoServerURLTest) { EXPECT_TRUE(promo_server_url.SchemeIs("https")); // TODO(achuith): Test this better. } - -TEST_F(PromoResourceServiceTest, UnpackWebStoreSignal) { - web_resource_service_->set_channel(chrome::VersionInfo::CHANNEL_DEV); - - std::string json = "{ " - " \"topic\": {" - " \"answers\": [" - " {" - " \"answer_id\": \"341252\"," - " \"name\": \"webstore_promo:15:1:\"," - " \"question\": \"The header!\"," - " \"inproduct_target\": \"The button label!\"," - " \"inproduct\": \"http://link.com\"," - " \"tooltip\": \"No thanks, hide this.\"" - " }" - " ]" - " }" - "}"; - scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( - base::JSONReader::Read(json))); - - // Set the source logo URL to verify that it gets cleared. - AppsPromo::SetSourcePromoLogoURL(GURL("https://www.google.com/test.png")); - - // Check that prefs are set correctly. - web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); - - AppsPromo::PromoData actual_data = AppsPromo::GetPromo(); - EXPECT_EQ("341252", actual_data.id); - EXPECT_EQ("The header!", actual_data.header); - EXPECT_EQ("The button label!", actual_data.button); - EXPECT_EQ(GURL("http://link.com"), actual_data.link); - EXPECT_EQ("No thanks, hide this.", actual_data.expire); - EXPECT_EQ(AppsPromo::USERS_NEW, actual_data.user_group); - - // When we don't download a logo, we revert to the default and clear the - // source pref. - EXPECT_EQ(GURL("chrome://theme/IDR_WEBSTORE_ICON"), actual_data.logo); - EXPECT_EQ(GURL(""), AppsPromo::GetSourcePromoLogoURL()); -} - -// Tests that the "web store active" flag is set even when the web store promo -// fails parsing. -TEST_F(PromoResourceServiceTest, UnpackPartialWebStoreSignal) { - std::string json = "{ " - " \"topic\": {" - " \"answers\": [" - " {" - " \"answer_id\": \"sdlfj32\"," - " \"name\": \"webstore_promo:#klsdjlfSD\"" - " }" - " ]" - " }" - "}"; - scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( - base::JSONReader::Read(json))); - - // Check that prefs are set correctly. - web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); - EXPECT_FALSE(AppsPromo::IsPromoSupportedForLocale()); - EXPECT_TRUE(AppsPromo::IsWebStoreSupportedForLocale()); -} - -// Tests that we can successfully unpack web store signals with HTTPS -// logos. -TEST_F(PromoResourceServiceTest, UnpackWebStoreSignalHttpsLogo) { - web_resource_service_->set_channel(chrome::VersionInfo::CHANNEL_DEV); - - std::string logo_url = "https://www.google.com/image/test.png"; - std::string png_data = "!$#%,./nvl;iadh9oh82"; - std::string png_base64 = "data:image/png;base64,ISQjJSwuL252bDtpYWRoOW9oODI="; - - FakeURLFetcherFactory factory; - factory.SetFakeResponse(logo_url, png_data, true); - - std::string json = - "{ " - " \"topic\": {" - " \"answers\": [" - " {" - " \"answer_id\": \"340252\"," - " \"name\": \"webstore_promo:15:1:" + logo_url + "\"," - " \"question\": \"Header!\"," - " \"inproduct_target\": \"The button label!\"," - " \"inproduct\": \"http://link.com\"," - " \"tooltip\": \"No thanks, hide this.\"" - " }" - " ]" - " }" - "}"; - - scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( - base::JSONReader::Read(json))); - - // Update the promo multiple times to verify the logo is cached correctly. - for (size_t i = 0; i < 2; ++i) { - web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); - - // We should only need to run the message loop the first time since the - // image is then cached. - if (i == 0) - loop_.RunAllPending(); - - // Reset this scoped_ptr to prevent a DCHECK. - web_resource_service_->apps_promo_logo_fetcher_.reset(); - - AppsPromo::PromoData actual_data = AppsPromo::GetPromo(); - EXPECT_EQ("340252", actual_data.id); - EXPECT_EQ("Header!", actual_data.header); - EXPECT_EQ("The button label!", actual_data.button); - EXPECT_EQ(GURL("http://link.com"), actual_data.link); - EXPECT_EQ("No thanks, hide this.", actual_data.expire); - EXPECT_EQ(AppsPromo::USERS_NEW, actual_data.user_group); - - // The logo should now be a base64 DATA URL. - EXPECT_EQ(GURL(png_base64), actual_data.logo); - - // And the source pref should hold the source HTTPS URL. - EXPECT_EQ(GURL(logo_url), AppsPromo::GetSourcePromoLogoURL()); - } -} - -// Tests that we revert to the default logo when the fetch fails. -TEST_F(PromoResourceServiceTest, UnpackWebStoreSignalHttpsLogoError) { - web_resource_service_->set_channel(chrome::VersionInfo::CHANNEL_DEV); - - std::string logo_url = "https://www.google.com/image/test.png"; - std::string png_data = "!$#%,./nvl;iadh9oh82"; - std::string png_base64 = "ISQjJSwuL252bDtpYWRoOW9oODI="; - - FakeURLFetcherFactory factory; - - // Have URLFetcher return a 500 error. - factory.SetFakeResponse(logo_url, png_data, false); - - std::string json = - "{ " - " \"topic\": {" - " \"answers\": [" - " {" - " \"answer_id\": \"340252\"," - " \"name\": \"webstore_promo:15:1:" + logo_url + "\"," - " \"question\": \"Header!\"," - " \"inproduct_target\": \"The button label!\"," - " \"inproduct\": \"http://link.com\"," - " \"tooltip\": \"No thanks, hide this.\"" - " }" - " ]" - " }" - "}"; - - scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( - base::JSONReader::Read(json))); - - web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); - - loop_.RunAllPending(); - - // Reset this scoped_ptr to prevent a DCHECK. - web_resource_service_->apps_promo_logo_fetcher_.reset(); - - AppsPromo::PromoData actual_data = AppsPromo::GetPromo(); - EXPECT_EQ("340252", actual_data.id); - EXPECT_EQ("Header!", actual_data.header); - EXPECT_EQ("The button label!", actual_data.button); - EXPECT_EQ(GURL("http://link.com"), actual_data.link); - EXPECT_EQ("No thanks, hide this.", actual_data.expire); - EXPECT_EQ(AppsPromo::USERS_NEW, actual_data.user_group); - - // Logos are the default values. - EXPECT_EQ(GURL("chrome://theme/IDR_WEBSTORE_ICON"), actual_data.logo); - EXPECT_EQ(GURL(""), AppsPromo::GetSourcePromoLogoURL()); -} - -// Tests that we don't download images over HTTP. -TEST_F(PromoResourceServiceTest, UnpackWebStoreSignalHttpLogo) { - web_resource_service_->set_channel(chrome::VersionInfo::CHANNEL_DEV); - - // Use an HTTP URL. - std::string logo_url = "http://www.google.com/image/test.png"; - std::string png_data = "!$#%,./nvl;iadh9oh82"; - std::string png_base64 = "ISQjJSwuL252bDtpYWRoOW9oODI="; - - FakeURLFetcherFactory factory; - factory.SetFakeResponse(logo_url, png_data, true); - - std::string json = - "{ " - " \"topic\": {" - " \"answers\": [" - " {" - " \"answer_id\": \"340252\"," - " \"name\": \"webstore_promo:15:1:" + logo_url + "\"," - " \"question\": \"Header!\"," - " \"inproduct_target\": \"The button label!\"," - " \"inproduct\": \"http://link.com\"," - " \"tooltip\": \"No thanks, hide this.\"" - " }" - " ]" - " }" - "}"; - - scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( - base::JSONReader::Read(json))); - - web_resource_service_->UnpackWebStoreSignal(*(test_json.get())); - - loop_.RunAllPending(); - - // Reset this scoped_ptr to prevent a DCHECK. - web_resource_service_->apps_promo_logo_fetcher_.reset(); - - AppsPromo::PromoData actual_data = AppsPromo::GetPromo(); - EXPECT_EQ("340252", actual_data.id); - EXPECT_EQ("Header!", actual_data.header); - EXPECT_EQ("The button label!", actual_data.button); - EXPECT_EQ(GURL("http://link.com"), actual_data.link); - EXPECT_EQ("No thanks, hide this.", actual_data.expire); - EXPECT_EQ(AppsPromo::USERS_NEW, actual_data.user_group); - - // Logos should be the default values because HTTP URLs are not valid. - EXPECT_EQ(GURL("chrome://theme/IDR_WEBSTORE_ICON"), actual_data.logo); - EXPECT_EQ(GURL(""), AppsPromo::GetSourcePromoLogoURL()); -} - -TEST_F(PromoResourceServiceTest, IsBuildTargetedTest) { - // canary - const chrome::VersionInfo::Channel canary = - chrome::VersionInfo::CHANNEL_CANARY; - EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 1)); - EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 3)); - EXPECT_FALSE(PromoResourceService::IsBuildTargeted(canary, 7)); - EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 15)); - EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 8)); - EXPECT_TRUE(PromoResourceService::IsBuildTargeted(canary, 11)); - - // dev - const chrome::VersionInfo::Channel dev = - chrome::VersionInfo::CHANNEL_DEV; - EXPECT_TRUE(PromoResourceService::IsBuildTargeted(dev, 1)); - EXPECT_TRUE(PromoResourceService::IsBuildTargeted(dev, 3)); - EXPECT_TRUE(PromoResourceService::IsBuildTargeted(dev, 7)); - EXPECT_TRUE(PromoResourceService::IsBuildTargeted(dev, 15)); - EXPECT_FALSE(PromoResourceService::IsBuildTargeted(dev, 8)); - EXPECT_TRUE(PromoResourceService::IsBuildTargeted(dev, 11)); - - // beta - const chrome::VersionInfo::Channel beta = - chrome::VersionInfo::CHANNEL_BETA; - EXPECT_FALSE(PromoResourceService::IsBuildTargeted(beta, 1)); - EXPECT_TRUE(PromoResourceService::IsBuildTargeted(beta, 3)); - EXPECT_TRUE(PromoResourceService::IsBuildTargeted(beta, 7)); - EXPECT_TRUE(PromoResourceService::IsBuildTargeted(beta, 15)); - EXPECT_FALSE(PromoResourceService::IsBuildTargeted(beta, 8)); - EXPECT_TRUE(PromoResourceService::IsBuildTargeted(beta, 11)); - - // stable - const chrome::VersionInfo::Channel stable = - chrome::VersionInfo::CHANNEL_STABLE; - EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 1)); - EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 3)); - EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 7)); - EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 15)); - EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 8)); - EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, 11)); - EXPECT_TRUE(PromoResourceService::IsBuildTargeted(stable, 12)); - - // invalid - EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, -1)); - EXPECT_FALSE(PromoResourceService::IsBuildTargeted(stable, INT_MAX)); -} |
