Index: chrome/browser/web_resource/promo_resource_service_unittest.cc |
=================================================================== |
--- chrome/browser/web_resource/promo_resource_service_unittest.cc (revision 102893) |
+++ chrome/browser/web_resource/promo_resource_service_unittest.cc (working copy) |
@@ -11,6 +11,7 @@ |
#include "chrome/browser/prefs/browser_prefs.h" |
#include "chrome/browser/prefs/pref_service.h" |
#include "chrome/browser/web_resource/promo_resource_service.h" |
+#include "chrome/browser/web_resource/promo_notification.h" |
Miranda Callahan
2011/09/27 09:18:40
whoops, swap for alphabetical order.
achuithb
2011/09/28 01:40:14
Done.
|
#include "chrome/common/pref_names.h" |
#include "chrome/test/base/testing_browser_process.h" |
#include "chrome/test/base/testing_pref_service.h" |
@@ -111,78 +112,337 @@ |
EXPECT_EQ(logo_end, 0); // date value reset to 0; |
} |
-TEST_F(PromoResourceServiceTest, UnpackNotificationSignal) { |
+class PromoNotificationTestDelegate : public PromoNotification::Delegate { |
+ public: |
+ explicit PromoNotificationTestDelegate(PrefService* prefs) |
+ : prefs_(prefs), |
+ promo_notification_(NULL), |
+ received_notification_(false), |
+ build_targeted_(true), |
+ start_(0.0), |
+ end_(0.0), |
+ build_(PromoResourceService::ALL_BUILDS), |
+ time_slice_(0), |
+ max_group_(0), |
+ max_views_(0), |
+ text_(), |
+ closed_(false) { |
+ } |
+ |
+ void Init(PromoNotification* promo_notification, |
+ const std::string& json, |
+ double start, double end, |
+ int build, int time_slice, int max_group, int max_views, |
+ const std::string& text, bool closed) { |
+ promo_notification_ = promo_notification; |
+ |
+ test_json_.reset(static_cast<DictionaryValue*>( |
+ base::JSONReader::Read(json, false))); |
+ |
+ start_ = start; |
+ end_ = end; |
+ |
+ build_ = build; |
+ time_slice_ = time_slice; |
+ max_group_ = max_group; |
+ max_views_ = max_views; |
+ |
+ text_ = text; |
+ closed_ = closed; |
+ |
+ received_notification_ = false; |
+ } |
+ |
+ // PromoNotification::Delegate implementation. |
+ virtual void OnNewNotification(double start, double end) { |
+ EXPECT_EQ(CalcStart(), start); |
+ EXPECT_EQ(promo_notification_->StartTimeWithOffset(), start); |
+ EXPECT_EQ(promo_notification_->end_, end); |
+ received_notification_ = true; |
+ } |
+ |
+ virtual bool IsBuildAllowed(int builds_targeted) const { |
+ EXPECT_EQ(builds_targeted, build_); |
+ return build_targeted_; |
+ } |
+ |
+ const base::DictionaryValue& TestJson() const { |
+ return *test_json_; |
+ } |
+ |
+ double CalcStart() const { |
+ return start_ + promo_notification_->group_ * time_slice_ * 60.0 * 60.0; |
+ } |
+ |
+ void InitPromoFromJson(bool should_receive_notification) { |
+ received_notification_ = false; |
+ promo_notification_->InitFromJson(TestJson()); |
+ EXPECT_TRUE(received_notification_ == should_receive_notification); |
+ |
+ // Test the fields. |
+ TestNotification(); |
+ TestPrefs(); |
+ } |
+ |
+ void TestNotification() { |
+ // Check values. |
+ EXPECT_EQ(promo_notification_->start_, start_); |
+ EXPECT_EQ(promo_notification_->end_, end_); |
+ EXPECT_EQ(promo_notification_->build_, build_); |
+ EXPECT_EQ(promo_notification_->time_slice_, time_slice_); |
+ EXPECT_EQ(promo_notification_->max_group_, max_group_); |
+ EXPECT_EQ(promo_notification_->max_views_, max_views_); |
+ EXPECT_EQ(promo_notification_->text_, text_); |
+ EXPECT_EQ(promo_notification_->closed_, closed_); |
+ |
+ // Check group within bounds. |
+ EXPECT_GE(promo_notification_->group_, 0); |
+ EXPECT_LT(promo_notification_->group_, 100); |
+ |
+ // Views should be 0 for now. |
+ EXPECT_EQ(promo_notification_->views_, 0); |
+ |
+ // Check calculated time. |
+ EXPECT_EQ(promo_notification_->StartTimeWithOffset(), CalcStart()); |
+ } |
+ |
+ void TestPrefs() { |
+ EXPECT_EQ(prefs_->GetDouble(prefs::kNTPPromoStart), start_); |
+ EXPECT_EQ(prefs_->GetDouble(prefs::kNTPPromoEnd), end_); |
+ |
+ EXPECT_EQ(prefs_->GetInteger(prefs::kNTPPromoBuild), build_); |
+ EXPECT_EQ(prefs_->GetInteger(prefs::kNTPPromoGroupTimeSlice), time_slice_); |
+ EXPECT_EQ(prefs_->GetInteger(prefs::kNTPPromoGroupMax), max_group_); |
+ EXPECT_EQ(prefs_->GetInteger(prefs::kNTPPromoViewsMax), max_views_); |
+ |
+ EXPECT_EQ(prefs_->GetInteger(prefs::kNTPPromoGroup), |
+ promo_notification_ ? promo_notification_->group_: 0); |
+ EXPECT_EQ(prefs_->GetInteger(prefs::kNTPPromoViews), 0); |
+ EXPECT_EQ(prefs_->GetString(prefs::kNTPPromoLine), text_); |
+ EXPECT_EQ(prefs_->GetBoolean(prefs::kNTPPromoClosed), closed_); |
+ } |
+ |
+ // Create a new PromoNotification from prefs and compare to current |
+ // notification. |
+ void TestInitFromPrefs() { |
+ PromoNotification prefs_promo_notification(prefs_, this); |
+ prefs_promo_notification.InitFromPrefs(); |
+ const bool is_equal = *promo_notification_ == prefs_promo_notification; |
+ EXPECT_TRUE(is_equal); |
+ } |
+ |
+ void TestGroup() { |
+ // Test out of range groups. |
+ for (int i = max_group_; i <= PromoNotification::kMaxGroupSize; ++i) { |
+ promo_notification_->group_ = i; |
+ EXPECT_FALSE(promo_notification_->CanShow()); |
+ } |
+ |
+ // Test in-range groups. |
+ for (int i = 0; i < max_group_; ++i) { |
+ promo_notification_->group_ = i; |
+ EXPECT_TRUE(promo_notification_->CanShow()); |
+ } |
+ } |
+ |
+ void TestViews() { |
+ // Test out of range views. |
+ for (int i = max_views_; i < PromoNotification::kMaxViews; ++i) { |
+ promo_notification_->views_ = i; |
+ EXPECT_FALSE(promo_notification_->CanShow()); |
+ } |
+ |
+ // Test in range views. |
+ for (int i = 0; i < max_views_; ++i) { |
+ promo_notification_->views_ = i; |
+ EXPECT_TRUE(promo_notification_->CanShow()); |
+ } |
+ } |
+ |
+ void TestBuild() { |
+ build_targeted_ = false; |
+ EXPECT_FALSE(promo_notification_->CanShow()); |
+ |
+ build_targeted_ = true; |
+ EXPECT_TRUE(promo_notification_->CanShow()); |
+ } |
+ |
+ void TestClosed() { |
+ promo_notification_->closed_ = true; |
+ EXPECT_FALSE(promo_notification_->CanShow()); |
+ |
+ promo_notification_->closed_ = false; |
+ EXPECT_TRUE(promo_notification_->CanShow()); |
+ } |
+ |
+ void TestText() { |
+ promo_notification_->text_.clear(); |
+ EXPECT_FALSE(promo_notification_->CanShow()); |
+ |
+ promo_notification_->text_ = text_; |
+ EXPECT_TRUE(promo_notification_->CanShow()); |
+ } |
+ |
+ void TestTime() { |
+ const double now = base::Time::Now().ToDoubleT(); |
+ const double qhour = 15 * 60; |
+ |
+ promo_notification_->group_ = 0; // For simplicity. |
+ |
+ promo_notification_->start_ = now - qhour; |
+ promo_notification_->end_ = now + qhour; |
+ EXPECT_TRUE(promo_notification_->CanShow()); |
+ |
+ // Start time has not arrived. |
+ promo_notification_->start_ = now + qhour; |
+ promo_notification_->end_ = now + qhour; |
+ EXPECT_FALSE(promo_notification_->CanShow()); |
+ |
+ // End time has past. |
+ promo_notification_->start_ = now - qhour; |
+ promo_notification_->end_ = now - qhour; |
+ EXPECT_FALSE(promo_notification_->CanShow()); |
+ |
+ promo_notification_->start_ = start_; |
+ promo_notification_->end_ = end_; |
+ EXPECT_TRUE(promo_notification_->CanShow()); |
+ } |
+ |
+ private: |
+ PrefService* prefs_; |
+ PromoNotification* promo_notification_; |
+ bool received_notification_; |
+ bool build_targeted_; |
+ scoped_ptr<DictionaryValue> test_json_; |
+ |
+ double start_; |
+ double end_; |
+ |
+ int build_; |
+ int time_slice_; |
+ int max_group_; |
+ int max_views_; |
+ |
+ std::string text_; |
+ bool closed_; |
+}; |
+ |
+TEST_F(PromoResourceServiceTest, PromoNotificationTest) { |
+ // Check that prefs are set correctly. |
+ PrefService* prefs = profile_.GetPrefs(); |
+ ASSERT_TRUE(prefs != NULL); |
+ |
+ PromoNotificationTestDelegate delegate(prefs); |
+ PromoNotification promo_notification(prefs, &delegate); |
+ |
+ // Make sure prefs are unset. |
+ delegate.TestPrefs(); |
+ |
// Set up start and end dates and promo line in a Dictionary as if parsed |
// from the service. |
- std::string json = "{ " |
- " \"topic\": {" |
- " \"answers\": [" |
- " {" |
- " \"name\": \"promo_start\"," |
- " \"question\": \"3:2:5\"," |
- " \"tooltip\": \"Eat more pie!\"," |
- " \"inproduct\": \"31/01/10 01:00 GMT\"" |
- " }," |
- " {" |
- " \"name\": \"promo_end\"," |
- " \"inproduct\": \"31/01/12 01:00 GMT\"" |
- " }" |
- " ]" |
- " }" |
- "}"; |
- scoped_ptr<DictionaryValue> test_json(static_cast<DictionaryValue*>( |
- base::JSONReader::Read(json, false))); |
+ delegate.Init(&promo_notification, |
+ "{ " |
+ " \"topic\": {" |
+ " \"answers\": [" |
+ " {" |
+ " \"name\": \"promo_start\"," |
+ " \"question\": \"3:2:5:10\"," |
+ " \"tooltip\": \"Eat more pie!\"," |
+ " \"inproduct\": \"31/01/10 01:00 GMT\"" |
+ " }," |
+ " {" |
+ " \"name\": \"promo_end\"," |
+ " \"inproduct\": \"31/01/12 01:00 GMT\"" |
+ " }" |
+ " ]" |
+ " }" |
+ "}", |
+ 1264899600, // unix epoch for Jan 31 2010 0100 GMT. |
+ 1327971600, // unix epoch for Jan 31 2012 0100 GMT. |
+ 3, 2, 5, 10, |
+ "Eat more pie!", false); |
+ delegate.InitPromoFromJson(true); |
+ |
+ // Second time should not trigger a notification. |
+ delegate.InitPromoFromJson(false); |
+ |
+ delegate.TestInitFromPrefs(); |
+ |
+ // Test various conditions of CanShow. |
+ // TestGroup Has the side effect of setting us to a passing group. |
+ delegate.TestGroup(); |
+ delegate.TestViews(); |
+ delegate.TestBuild(); |
+ delegate.TestClosed(); |
+ delegate.TestText(); |
+ delegate.TestTime(); |
+} |
+ |
+TEST_F(PromoResourceServiceTest, PromoNotificationTestFail) { |
// Check that prefs are set correctly. |
- web_resource_service_->UnpackNotificationSignal(*(test_json.get())); |
PrefService* prefs = profile_.GetPrefs(); |
ASSERT_TRUE(prefs != NULL); |
- std::string promo_line = prefs->GetString(prefs::kNTPPromoLine); |
- EXPECT_EQ(promo_line, "Eat more pie!"); |
+ PromoNotificationTestDelegate delegate(prefs); |
+ PromoNotification promo_notification(prefs, &delegate); |
- int promo_group = prefs->GetInteger(prefs::kNTPPromoGroup); |
- EXPECT_GE(promo_group, 0); |
- EXPECT_LT(promo_group, 100); |
+ // Set up start and end dates and promo line in a Dictionary as if parsed |
+ // from the service. |
+ delegate.Init(&promo_notification, |
+ "{ " |
+ " \"topic\": {" |
+ " \"answers\": [" |
+ " {" |
+ " \"name\": \"promo_start\"," |
+ " \"question\": \"12:8:10:20\"," |
+ " \"tooltip\": \"Happy 3rd Birthday!\"," |
+ " \"inproduct\": \"09/15/10 05:00 PDT\"" |
+ " }," |
+ " {" |
+ " \"name\": \"promo_end\"," |
+ " \"inproduct\": \"09/30/10 05:00 PDT\"" |
+ " }" |
+ " ]" |
+ " }" |
+ "}", |
+ 1284552000, // unix epoch for Sep 15 2010 0500 PDT. |
+ 1285848000, // unix epoch for Sep 30 2010 0500 PDT. |
+ 12, 8, 10, 20, |
+ "Happy 3rd Birthday!", false); |
- int promo_build_type = prefs->GetInteger(prefs::kNTPPromoBuild); |
- EXPECT_EQ(promo_build_type & PromoResourceService::DEV_BUILD, |
- PromoResourceService::DEV_BUILD); |
- EXPECT_EQ(promo_build_type & PromoResourceService::BETA_BUILD, |
- PromoResourceService::BETA_BUILD); |
- EXPECT_EQ(promo_build_type & PromoResourceService::STABLE_BUILD, 0); |
+ delegate.InitPromoFromJson(true); |
- int promo_time_slice = prefs->GetInteger(prefs::kNTPPromoGroupTimeSlice); |
- EXPECT_EQ(promo_time_slice, 2); |
+ // Second time should not trigger a notification. |
+ delegate.InitPromoFromJson(false); |
- int promo_group_max = prefs->GetInteger(prefs::kNTPPromoGroupMax); |
- EXPECT_EQ(promo_group_max, 5); |
+ delegate.TestInitFromPrefs(); |
- double promo_start = prefs->GetDouble(prefs::kNTPPromoStart); |
- double actual_start = 1264899600; // unix epoch for Jan 31 2010 0100 GMT. |
- EXPECT_EQ(promo_start, actual_start); |
+ // Should fail because out of time bounds. |
+ EXPECT_FALSE(promo_notification.CanShow()); |
+} |
- double modified_start = actual_start + promo_group * 2 * 60 * 60; |
- EXPECT_EQ(PromoResourceService::GetNotificationStartTime(prefs), |
- modified_start); |
+TEST_F(PromoResourceServiceTest, GetNextQuestionValueTest) { |
+ const std::string question("0:-100:2048:0:2a"); |
+ const int question_vec[] = { 0, -100, 2048, 0 }; |
+ size_t index = 0; |
+ bool err = false; |
- double promo_end = prefs->GetDouble(prefs::kNTPPromoEnd); |
- EXPECT_EQ(promo_end, 1327971600); // unix epoch for Jan 31 2012 0100 GMT. |
+ for (size_t i = 0; i < arraysize(question_vec); ++i) { |
+ EXPECT_EQ(question_vec[i], |
+ PromoNotification::GetNextQuestionValue(question, &index, &err)); |
+ EXPECT_FALSE(err); |
+ } |
+ EXPECT_EQ(PromoNotification::GetNextQuestionValue(question, &index, &err), 0); |
+ EXPECT_TRUE(err); |
+} |
- // Unpack the same json a second time. |
- web_resource_service_->UnpackNotificationSignal(*(test_json.get())); |
- |
- // All the data should be unchanged. |
- EXPECT_EQ(promo_line, prefs->GetString(prefs::kNTPPromoLine)); |
- EXPECT_EQ(promo_group, prefs->GetInteger(prefs::kNTPPromoGroup)); |
- EXPECT_EQ(promo_build_type, prefs->GetInteger(prefs::kNTPPromoBuild)); |
- EXPECT_EQ(promo_time_slice, |
- prefs->GetInteger(prefs::kNTPPromoGroupTimeSlice)); |
- EXPECT_EQ(promo_group_max, prefs->GetInteger(prefs::kNTPPromoGroupMax)); |
- EXPECT_EQ(promo_start, prefs->GetDouble(prefs::kNTPPromoStart)); |
- EXPECT_EQ(modified_start, |
- PromoResourceService::GetNotificationStartTime(prefs)); |
- EXPECT_EQ(promo_end, prefs->GetDouble(prefs::kNTPPromoEnd)); |
+TEST_F(PromoResourceServiceTest, NewGroupTest) { |
+ for (size_t i = 0; i < 1000; ++i) { |
+ const int group = PromoNotification::NewGroup(); |
+ EXPECT_GE(group, 0); |
+ EXPECT_LT(group, 100); |
+ } |
} |
TEST_F(PromoResourceServiceTest, UnpackWebStoreSignal) { |
@@ -409,7 +669,7 @@ |
EXPECT_EQ(GURL(""), AppsPromo::GetSourcePromoLogoURL()); |
} |
-TEST_F(PromoResourceServiceTest, IsBuildTargeted) { |
+TEST_F(PromoResourceServiceTest, IsBuildTargetedTest) { |
// canary |
const chrome::VersionInfo::Channel canary = |
chrome::VersionInfo::CHANNEL_CANARY; |