Commit 5ffb8e26 authored by achuith@chromium.org's avatar achuith@chromium.org

Support for promo_type.

* Create an enum PromoType and a data member promo_type_ of NotificationPromo.
* InitFromJson, InitFromPrefs, HandleClosed and HandleViewed all take a PromoType now.
* HandleClosed and HandleViewed are static.
* Add an anon function PromoTypeToString for conversion from PromoType to string.
* Improve unit tests TestViewed() and TestClosed().

BUG=123061
TEST=unit tests. Manual.

Review URL: https://chromiumcodereview.appspot.com/10836099

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@150235 0039d316-1c4b-4281-b951-d872f2087c98
parent f5a05dc4
...@@ -63,15 +63,16 @@ void NewTabPageHandler::RegisterMessages() { ...@@ -63,15 +63,16 @@ void NewTabPageHandler::RegisterMessages() {
} }
void NewTabPageHandler::HandleCloseNotificationPromo(const ListValue* args) { void NewTabPageHandler::HandleCloseNotificationPromo(const ListValue* args) {
NotificationPromo notification_promo(Profile::FromWebUI(web_ui())); NotificationPromo::HandleClosed(Profile::FromWebUI(web_ui()),
notification_promo.HandleClosed(); NotificationPromo::NTP_NOTIFICATION_PROMO);
Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED); Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED);
} }
void NewTabPageHandler::HandleNotificationPromoViewed(const ListValue* args) { void NewTabPageHandler::HandleNotificationPromoViewed(const ListValue* args) {
NotificationPromo notification_promo(Profile::FromWebUI(web_ui())); if (NotificationPromo::HandleViewed(Profile::FromWebUI(web_ui()),
if (notification_promo.HandleViewed()) NotificationPromo::NTP_NOTIFICATION_PROMO)) {
Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED); Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED);
}
} }
void NewTabPageHandler::HandlePageSelected(const ListValue* args) { void NewTabPageHandler::HandlePageSelected(const ListValue* args) {
......
...@@ -410,7 +410,7 @@ void NTPResourceCache::CreateNewTabHTML() { ...@@ -410,7 +410,7 @@ void NTPResourceCache::CreateNewTabHTML() {
// Set the promo string for display if there is a valid outstanding promo. // Set the promo string for display if there is a valid outstanding promo.
NotificationPromo notification_promo(profile_); NotificationPromo notification_promo(profile_);
notification_promo.InitFromPrefs(); notification_promo.InitFromPrefs(NotificationPromo::NTP_NOTIFICATION_PROMO);
if (notification_promo.CanShow()) if (notification_promo.CanShow())
load_time_data.SetString("serverpromo", notification_promo.promo_text()); load_time_data.SetString("serverpromo", notification_promo.promo_text());
......
...@@ -105,6 +105,28 @@ const char* ChannelString() { ...@@ -105,6 +105,28 @@ const char* ChannelString() {
} }
} }
struct PromoMapEntry {
NotificationPromo::PromoType promo_type;
const char* promo_type_str;
};
const PromoMapEntry kPromoMap[] = {
{ NotificationPromo::NO_PROMO, "" },
{ NotificationPromo::NTP_NOTIFICATION_PROMO, "ntp_notification_promo" },
{ NotificationPromo::BUBBLE_PROMO, "bubble_promo" },
{ NotificationPromo::MOBILE_NTP_SYNC_PROMO, "mobile_ntp_sync_promo" },
};
// Convert PromoType to appropriate string.
const char* PromoTypeToString(NotificationPromo::PromoType promo_type) {
for (size_t i = 0; i < arraysize(kPromoMap); ++i) {
if (kPromoMap[i].promo_type == promo_type)
return kPromoMap[i].promo_type_str;
}
NOTREACHED();
return "";
}
// TODO(achuith): remove this in m23. // TODO(achuith): remove this in m23.
void ClearDeprecatedPrefs(PrefService* prefs) { void ClearDeprecatedPrefs(PrefService* prefs) {
prefs->RegisterStringPref(prefs::kNtpPromoLine, prefs->RegisterStringPref(prefs::kNtpPromoLine,
...@@ -183,17 +205,13 @@ void ClearDeprecatedPrefs(PrefService* prefs) { ...@@ -183,17 +205,13 @@ void ClearDeprecatedPrefs(PrefService* prefs) {
} // namespace } // namespace
const char NotificationPromo::kNtpNotificationPromoType[] =
"ntp_notification_promo";
const char NotificationPromo::kBubblePromoType[] = "bubble_promo";
NotificationPromo::NotificationPromo(Profile* profile) NotificationPromo::NotificationPromo(Profile* profile)
: profile_(profile), : profile_(profile),
prefs_(profile_->GetPrefs()), prefs_(profile_->GetPrefs()),
promo_type_(kNtpNotificationPromoType), promo_type_(NO_PROMO),
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
promo_action_args_(new base::ListValue), promo_action_args_(new base::ListValue),
#endif // defined(OS_ANDROID) #endif
start_(0.0), start_(0.0),
end_(0.0), end_(0.0),
num_groups_(kDefaultGroupSize), num_groups_(kDefaultGroupSize),
...@@ -213,17 +231,14 @@ NotificationPromo::NotificationPromo(Profile* profile) ...@@ -213,17 +231,14 @@ NotificationPromo::NotificationPromo(Profile* profile)
NotificationPromo::~NotificationPromo() {} NotificationPromo::~NotificationPromo() {}
void NotificationPromo::InitFromJson(const DictionaryValue& json) { void NotificationPromo::InitFromJson(const DictionaryValue& json,
PromoType promo_type) {
promo_type_ = promo_type;
const ListValue* promo_list = NULL; const ListValue* promo_list = NULL;
#if !defined(OS_ANDROID) if (!json.GetList(PromoTypeToString(promo_type_), &promo_list)) {
if (!json.GetList(promo_type_, &promo_list)) LOG(ERROR) << "Malformed JSON: not " << PromoTypeToString(promo_type_);
return;
#else
if (!json.GetList("mobile_ntp_sync_promo", &promo_list)) {
LOG(ERROR) << "Malfromed JSON: not a mobile_ntp_sync_promo";
return; return;
} }
#endif // !defined(OS_ANDROID)
// No support for multiple promos yet. Only consider the first one. // No support for multiple promos yet. Only consider the first one.
const DictionaryValue* promo = NULL; const DictionaryValue* promo = NULL;
...@@ -343,7 +358,7 @@ void NotificationPromo::InitFromJson(const DictionaryValue& json) { ...@@ -343,7 +358,7 @@ void NotificationPromo::InitFromJson(const DictionaryValue& json) {
void NotificationPromo::CheckForNewNotification() { void NotificationPromo::CheckForNewNotification() {
NotificationPromo old_promo(profile_); NotificationPromo old_promo(profile_);
old_promo.InitFromPrefs(); old_promo.InitFromPrefs(promo_type_);
const double old_start = old_promo.start_; const double old_start = old_promo.start_;
const double old_end = old_promo.end_; const double old_end = old_promo.end_;
const std::string old_promo_text = old_promo.promo_text_; const std::string old_promo_text = old_promo.promo_text_;
...@@ -400,18 +415,19 @@ void NotificationPromo::WritePrefs() { ...@@ -400,18 +415,19 @@ void NotificationPromo::WritePrefs() {
promo_list->Set(0, ntp_promo); // Only support 1 promo for now. promo_list->Set(0, ntp_promo); // Only support 1 promo for now.
base::DictionaryValue promo_dict; base::DictionaryValue promo_dict;
promo_dict.Set(promo_type_, promo_list); promo_dict.Set(PromoTypeToString(promo_type_), promo_list);
prefs_->Set(kPrefPromoObject, promo_dict); prefs_->Set(kPrefPromoObject, promo_dict);
} }
void NotificationPromo::InitFromPrefs() { void NotificationPromo::InitFromPrefs(PromoType promo_type) {
promo_type_ = promo_type;
const base::DictionaryValue* promo_dict = const base::DictionaryValue* promo_dict =
prefs_->GetDictionary(kPrefPromoObject); prefs_->GetDictionary(kPrefPromoObject);
if (!promo_dict) if (!promo_dict)
return; return;
const base::ListValue* promo_list(NULL); const base::ListValue* promo_list(NULL);
promo_dict->GetList(promo_type_, &promo_list); promo_dict->GetList(PromoTypeToString(promo_type_), &promo_list);
if (!promo_list) if (!promo_list)
return; return;
...@@ -458,21 +474,25 @@ bool NotificationPromo::CanShow() const { ...@@ -458,21 +474,25 @@ bool NotificationPromo::CanShow() const {
IsGPlusRequired(); IsGPlusRequired();
} }
void NotificationPromo::HandleClosed() { // static
void NotificationPromo::HandleClosed(Profile* profile, PromoType promo_type) {
content::RecordAction(UserMetricsAction("NTPPromoClosed")); content::RecordAction(UserMetricsAction("NTPPromoClosed"));
InitFromPrefs(); NotificationPromo promo(profile);
if (!closed_) { promo.InitFromPrefs(promo_type);
closed_ = true; if (!promo.closed_) {
WritePrefs(); promo.closed_ = true;
promo.WritePrefs();
} }
} }
bool NotificationPromo::HandleViewed() { // static
bool NotificationPromo::HandleViewed(Profile* profile, PromoType promo_type) {
content::RecordAction(UserMetricsAction("NTPPromoShown")); content::RecordAction(UserMetricsAction("NTPPromoShown"));
InitFromPrefs(); NotificationPromo promo(profile);
++views_; promo.InitFromPrefs(promo_type);
WritePrefs(); ++promo.views_;
return ExceedsMaxViews(); promo.WritePrefs();
return promo.ExceedsMaxViews();
} }
bool NotificationPromo::ExceedsMaxGroup() const { bool NotificationPromo::ExceedsMaxGroup() const {
......
...@@ -27,15 +27,19 @@ class NotificationPromo { ...@@ -27,15 +27,19 @@ class NotificationPromo {
public: public:
static GURL PromoServerURL(); static GURL PromoServerURL();
static const char kNtpNotificationPromoType[]; enum PromoType {
static const char kBubblePromoType[]; NO_PROMO,
NTP_NOTIFICATION_PROMO,
BUBBLE_PROMO,
MOBILE_NTP_SYNC_PROMO,
};
explicit NotificationPromo(Profile* profile); explicit NotificationPromo(Profile* profile);
~NotificationPromo(); ~NotificationPromo();
// Initialize from json/prefs. // Initialize from json/prefs.
void InitFromJson(const base::DictionaryValue& json); void InitFromJson(const base::DictionaryValue& json, PromoType promo_type);
void InitFromPrefs(); void InitFromPrefs(PromoType promo_type);
// Can this promo be shown? // Can this promo be shown?
bool CanShow() const; bool CanShow() const;
...@@ -46,8 +50,11 @@ class NotificationPromo { ...@@ -46,8 +50,11 @@ class NotificationPromo {
double EndTime() const; double EndTime() const;
// Helpers for NewTabPageHandler. // Helpers for NewTabPageHandler.
void HandleClosed(); // Mark the promo as closed when the user dismisses it.
bool HandleViewed(); // returns true if views exceeds maximum allowed. static void HandleClosed(Profile* profile, PromoType promo_type);
// Mark the promo has having been viewed. This returns true if views
// exceeds the maximum allowed.
static bool HandleViewed(Profile* profile, PromoType promo_type);
bool new_notification() const { return new_notification_; } bool new_notification() const { return new_notification_; }
...@@ -91,7 +98,7 @@ class NotificationPromo { ...@@ -91,7 +98,7 @@ class NotificationPromo {
Profile* profile_; Profile* profile_;
PrefService* prefs_; PrefService* prefs_;
std::string promo_type_; PromoType promo_type_;
std::string promo_text_; std::string promo_text_;
#if defined(OS_ANDROID) || defined(OS_IOS) #if defined(OS_ANDROID) || defined(OS_IOS)
std::string promo_text_long_; std::string promo_text_long_;
......
...@@ -179,7 +179,13 @@ std::string PromoResourceService::GetPromoLocale() { ...@@ -179,7 +179,13 @@ std::string PromoResourceService::GetPromoLocale() {
void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { void PromoResourceService::Unpack(const DictionaryValue& parsed_json) {
NotificationPromo notification_promo(profile_); NotificationPromo notification_promo(profile_);
notification_promo.InitFromJson(parsed_json); NotificationPromo::PromoType promo_type =
#if !defined(OS_ANDROID)
NotificationPromo::NTP_NOTIFICATION_PROMO;
#else
NotificationPromo::MOBILE_NTP_SYNC_PROMO;
#endif
notification_promo.InitFromJson(parsed_json, promo_type);
if (notification_promo.new_notification()) { if (notification_promo.new_notification()) {
ScheduleNotification(notification_promo.StartTimeForGroup(), ScheduleNotification(notification_promo.StartTimeForGroup(),
......
...@@ -77,6 +77,13 @@ class NotificationPromoTest { ...@@ -77,6 +77,13 @@ class NotificationPromoTest {
ASSERT_TRUE(dict); ASSERT_TRUE(dict);
test_json_.reset(dict); test_json_.reset(dict);
promo_type_ =
#if !defined(OS_ANDROID)
NotificationPromo::NTP_NOTIFICATION_PROMO;
#else
NotificationPromo::MOBILE_NTP_SYNC_PROMO;
#endif
promo_text_ = promo_text; promo_text_ = promo_text;
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
...@@ -104,7 +111,7 @@ class NotificationPromoTest { ...@@ -104,7 +111,7 @@ class NotificationPromoTest {
} }
void InitPromoFromJson(bool should_receive_notification) { void InitPromoFromJson(bool should_receive_notification) {
notification_promo_.InitFromJson(*test_json_); notification_promo_.InitFromJson(*test_json_, promo_type_);
EXPECT_EQ(should_receive_notification, EXPECT_EQ(should_receive_notification,
notification_promo_.new_notification()); notification_promo_.new_notification());
...@@ -156,7 +163,7 @@ class NotificationPromoTest { ...@@ -156,7 +163,7 @@ class NotificationPromoTest {
// notification. // notification.
void TestInitFromPrefs() { void TestInitFromPrefs() {
NotificationPromo prefs_notification_promo(profile_); NotificationPromo prefs_notification_promo(profile_);
prefs_notification_promo.InitFromPrefs(); prefs_notification_promo.InitFromPrefs(promo_type_);
EXPECT_EQ(notification_promo_.prefs_, EXPECT_EQ(notification_promo_.prefs_,
prefs_notification_promo.prefs_); prefs_notification_promo.prefs_);
...@@ -236,44 +243,44 @@ class NotificationPromoTest { ...@@ -236,44 +243,44 @@ class NotificationPromoTest {
notification_promo_.views_ = notification_promo_.max_views_ - 2; notification_promo_.views_ = notification_promo_.max_views_ - 2;
notification_promo_.WritePrefs(); notification_promo_.WritePrefs();
NotificationPromo::HandleViewed(profile_, promo_type_);
NotificationPromo new_promo(profile_); NotificationPromo new_promo(profile_);
new_promo.HandleViewed(); new_promo.InitFromPrefs(promo_type_);
EXPECT_EQ(new_promo.max_views_ - 1, new_promo.views_);
EXPECT_TRUE(new_promo.CanShow()); EXPECT_TRUE(new_promo.CanShow());
new_promo.HandleViewed(); NotificationPromo::HandleViewed(profile_, promo_type_);
new_promo.InitFromPrefs(promo_type_);
EXPECT_EQ(new_promo.max_views_, new_promo.views_);
EXPECT_FALSE(new_promo.CanShow()); EXPECT_FALSE(new_promo.CanShow());
notification_promo_.InitFromPrefs();
EXPECT_FALSE(notification_promo_.CanShow());
// Test out of range views. // Test out of range views.
for (int i = max_views_; i < max_views_ * 2; ++i) { for (int i = max_views_; i < max_views_ * 2; ++i) {
notification_promo_.views_ = i; new_promo.views_ = i;
EXPECT_FALSE(notification_promo_.CanShow()); EXPECT_FALSE(new_promo.CanShow());
} }
// Test in range views. // Test in range views.
for (int i = 0; i < max_views_; ++i) { for (int i = 0; i < max_views_; ++i) {
notification_promo_.views_ = i; new_promo.views_ = i;
EXPECT_TRUE(notification_promo_.CanShow()); EXPECT_TRUE(new_promo.CanShow());
} }
notification_promo_.WritePrefs(); new_promo.WritePrefs();
} }
void TestClosed() { void TestClosed() {
NotificationPromo new_promo(profile_); NotificationPromo new_promo(profile_);
new_promo.InitFromPrefs(); new_promo.InitFromPrefs(promo_type_);
EXPECT_FALSE(new_promo.closed_);
EXPECT_TRUE(new_promo.CanShow()); EXPECT_TRUE(new_promo.CanShow());
new_promo.HandleClosed();
EXPECT_FALSE(new_promo.CanShow());
new_promo.InitFromPrefs();
EXPECT_FALSE(new_promo.CanShow());
notification_promo_.closed_ = true; NotificationPromo::HandleClosed(profile_, promo_type_);
EXPECT_FALSE(notification_promo_.CanShow()); new_promo.InitFromPrefs(promo_type_);
EXPECT_TRUE(new_promo.closed_);
EXPECT_FALSE(new_promo.CanShow());
notification_promo_.closed_ = false; new_promo.closed_ = false;
EXPECT_TRUE(notification_promo_.CanShow()); EXPECT_TRUE(new_promo.CanShow());
notification_promo_.WritePrefs(); new_promo.WritePrefs();
} }
void TestPromoText() { void TestPromoText() {
...@@ -378,6 +385,7 @@ class NotificationPromoTest { ...@@ -378,6 +385,7 @@ class NotificationPromoTest {
bool received_notification_; bool received_notification_;
scoped_ptr<DictionaryValue> test_json_; scoped_ptr<DictionaryValue> test_json_;
NotificationPromo::PromoType promo_type_;
std::string promo_text_; std::string promo_text_;
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
std::string promo_text_long_; std::string promo_text_long_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment