| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <vector> |
| 6 |
| 5 #include "base/json/json_reader.h" | 7 #include "base/json/json_reader.h" |
| 6 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 7 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 8 #include "base/time.h" | 10 #include "base/time.h" |
| 9 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 10 #include "base/values.h" | 12 #include "base/values.h" |
| 11 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 12 #include "chrome/browser/prefs/browser_prefs.h" | 14 #include "chrome/browser/prefs/browser_prefs.h" |
| 13 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
| 14 #include "chrome/browser/web_resource/notification_promo.h" | 16 #include "chrome/browser/web_resource/notification_promo.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 received_notification_ = false; | 103 received_notification_ = false; |
| 102 } | 104 } |
| 103 | 105 |
| 104 void InitPromoFromJson(bool should_receive_notification) { | 106 void InitPromoFromJson(bool should_receive_notification) { |
| 105 notification_promo_.InitFromJson(*test_json_); | 107 notification_promo_.InitFromJson(*test_json_); |
| 106 EXPECT_EQ(should_receive_notification, | 108 EXPECT_EQ(should_receive_notification, |
| 107 notification_promo_.new_notification()); | 109 notification_promo_.new_notification()); |
| 108 | 110 |
| 109 // Test the fields. | 111 // Test the fields. |
| 110 TestNotification(); | 112 TestNotification(); |
| 111 TestPrefs(); | |
| 112 } | 113 } |
| 113 | 114 |
| 114 void TestNotification() { | 115 void TestNotification() { |
| 115 // Check values. | 116 // Check values. |
| 116 EXPECT_EQ(notification_promo_.promo_text_, promo_text_); | 117 EXPECT_EQ(notification_promo_.promo_text_, promo_text_); |
| 117 | 118 |
| 118 #if defined(OS_ANDROID) | 119 #if defined(OS_ANDROID) |
| 119 EXPECT_EQ(notification_promo_.promo_text_long_, promo_text_long_); | 120 EXPECT_EQ(notification_promo_.promo_text_long_, promo_text_long_); |
| 120 EXPECT_EQ(notification_promo_.promo_action_type_, promo_action_type_); | 121 EXPECT_EQ(notification_promo_.promo_action_type_, promo_action_type_); |
| 121 EXPECT_TRUE(notification_promo_.promo_action_args_.get() != NULL); | 122 EXPECT_TRUE(notification_promo_.promo_action_args_.get() != NULL); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 144 // Check group within bounds. | 145 // Check group within bounds. |
| 145 EXPECT_GE(notification_promo_.group_, 0); | 146 EXPECT_GE(notification_promo_.group_, 0); |
| 146 EXPECT_LT(notification_promo_.group_, num_groups_); | 147 EXPECT_LT(notification_promo_.group_, num_groups_); |
| 147 | 148 |
| 148 // Views should be 0 for now. | 149 // Views should be 0 for now. |
| 149 EXPECT_EQ(notification_promo_.views_, 0); | 150 EXPECT_EQ(notification_promo_.views_, 0); |
| 150 | 151 |
| 151 EXPECT_EQ(notification_promo_.gplus_required_, gplus_required_); | 152 EXPECT_EQ(notification_promo_.gplus_required_, gplus_required_); |
| 152 } | 153 } |
| 153 | 154 |
| 154 void TestPrefs() { | |
| 155 EXPECT_EQ(prefs_->GetString(prefs::kNtpPromoLine), promo_text_); | |
| 156 #if defined(OS_ANDROID) | |
| 157 EXPECT_EQ(prefs_->GetString(prefs::kNtpPromoLineLong), promo_text_long_); | |
| 158 EXPECT_EQ(prefs_->GetString(prefs::kNtpPromoActionType), | |
| 159 promo_action_type_); | |
| 160 const base::ListValue* lv = prefs_->GetList(prefs::kNtpPromoActionArgs); | |
| 161 EXPECT_TRUE(lv != NULL); | |
| 162 EXPECT_EQ(lv->GetSize(), promo_action_args_.size()); | |
| 163 for (std::size_t i = 0; i < lv->GetSize(); ++i) { | |
| 164 std::string value; | |
| 165 EXPECT_TRUE(lv->GetString(i, &value)); | |
| 166 EXPECT_EQ(value, promo_action_args_[i]); | |
| 167 } | |
| 168 #endif // defined(OS_ANDROID) | |
| 169 | |
| 170 EXPECT_EQ(prefs_->GetDouble(prefs::kNtpPromoStart), start_); | |
| 171 EXPECT_EQ(prefs_->GetDouble(prefs::kNtpPromoEnd), end_); | |
| 172 | |
| 173 EXPECT_EQ(prefs_->GetInteger(prefs::kNtpPromoNumGroups), num_groups_); | |
| 174 EXPECT_EQ(prefs_->GetInteger(prefs::kNtpPromoInitialSegment), | |
| 175 initial_segment_); | |
| 176 EXPECT_EQ(prefs_->GetInteger(prefs::kNtpPromoIncrement), increment_); | |
| 177 EXPECT_EQ(prefs_->GetInteger(prefs::kNtpPromoGroupTimeSlice), time_slice_); | |
| 178 EXPECT_EQ(prefs_->GetInteger(prefs::kNtpPromoGroupMax), max_group_); | |
| 179 | |
| 180 EXPECT_EQ(prefs_->GetInteger(prefs::kNtpPromoViewsMax), max_views_); | |
| 181 EXPECT_EQ(prefs_->GetBoolean(prefs::kNtpPromoClosed), closed_); | |
| 182 | |
| 183 EXPECT_EQ(prefs_->GetInteger(prefs::kNtpPromoGroup), | |
| 184 notification_promo_.group_); | |
| 185 EXPECT_EQ(prefs_->GetInteger(prefs::kNtpPromoViews), 0); | |
| 186 | |
| 187 EXPECT_EQ(prefs_->GetBoolean(prefs::kNtpPromoGplusRequired), | |
| 188 gplus_required_); | |
| 189 } | |
| 190 | |
| 191 // Create a new NotificationPromo from prefs and compare to current | 155 // Create a new NotificationPromo from prefs and compare to current |
| 192 // notification. | 156 // notification. |
| 193 void TestInitFromPrefs() { | 157 void TestInitFromPrefs() { |
| 194 NotificationPromo prefs_notification_promo(profile_); | 158 NotificationPromo prefs_notification_promo(profile_); |
| 195 prefs_notification_promo.InitFromPrefs(); | 159 prefs_notification_promo.InitFromPrefs(); |
| 196 | 160 |
| 197 EXPECT_EQ(notification_promo_.prefs_, | 161 EXPECT_EQ(notification_promo_.prefs_, |
| 198 prefs_notification_promo.prefs_); | 162 prefs_notification_promo.prefs_); |
| 199 EXPECT_EQ(notification_promo_.promo_text_, | 163 EXPECT_EQ(notification_promo_.promo_text_, |
| 200 prefs_notification_promo.promo_text_); | 164 prefs_notification_promo.promo_text_); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 notification_promo_.group_ = i; | 222 notification_promo_.group_ = i; |
| 259 EXPECT_TRUE(notification_promo_.CanShow()); | 223 EXPECT_TRUE(notification_promo_.CanShow()); |
| 260 } | 224 } |
| 261 | 225 |
| 262 // When max_group_ is 0, all groups pass. | 226 // When max_group_ is 0, all groups pass. |
| 263 notification_promo_.max_group_ = 0; | 227 notification_promo_.max_group_ = 0; |
| 264 for (int i = 0; i < num_groups_; i += incr) { | 228 for (int i = 0; i < num_groups_; i += incr) { |
| 265 notification_promo_.group_ = i; | 229 notification_promo_.group_ = i; |
| 266 EXPECT_TRUE(notification_promo_.CanShow()); | 230 EXPECT_TRUE(notification_promo_.CanShow()); |
| 267 } | 231 } |
| 232 notification_promo_.WritePrefs(); |
| 268 } | 233 } |
| 269 | 234 |
| 270 void TestViews() { | 235 void TestViews() { |
| 236 notification_promo_.views_ = notification_promo_.max_views_ - 2; |
| 237 notification_promo_.WritePrefs(); |
| 238 |
| 239 NotificationPromo new_promo(profile_); |
| 240 new_promo.HandleViewed(); |
| 241 EXPECT_TRUE(new_promo.CanShow()); |
| 242 new_promo.HandleViewed(); |
| 243 EXPECT_FALSE(new_promo.CanShow()); |
| 244 |
| 245 notification_promo_.InitFromPrefs(); |
| 246 EXPECT_FALSE(notification_promo_.CanShow()); |
| 247 |
| 271 // Test out of range views. | 248 // Test out of range views. |
| 272 for (int i = max_views_; i < max_views_ * 2; ++i) { | 249 for (int i = max_views_; i < max_views_ * 2; ++i) { |
| 273 notification_promo_.views_ = i; | 250 notification_promo_.views_ = i; |
| 274 EXPECT_FALSE(notification_promo_.CanShow()); | 251 EXPECT_FALSE(notification_promo_.CanShow()); |
| 275 } | 252 } |
| 276 | 253 |
| 277 // Test in range views. | 254 // Test in range views. |
| 278 for (int i = 0; i < max_views_; ++i) { | 255 for (int i = 0; i < max_views_; ++i) { |
| 279 notification_promo_.views_ = i; | 256 notification_promo_.views_ = i; |
| 280 EXPECT_TRUE(notification_promo_.CanShow()); | 257 EXPECT_TRUE(notification_promo_.CanShow()); |
| 281 } | 258 } |
| 259 notification_promo_.WritePrefs(); |
| 282 } | 260 } |
| 283 | 261 |
| 284 void TestClosed() { | 262 void TestClosed() { |
| 263 NotificationPromo new_promo(profile_); |
| 264 new_promo.InitFromPrefs(); |
| 265 EXPECT_TRUE(new_promo.CanShow()); |
| 266 new_promo.HandleClosed(); |
| 267 EXPECT_FALSE(new_promo.CanShow()); |
| 268 new_promo.InitFromPrefs(); |
| 269 EXPECT_FALSE(new_promo.CanShow()); |
| 270 |
| 285 notification_promo_.closed_ = true; | 271 notification_promo_.closed_ = true; |
| 286 EXPECT_FALSE(notification_promo_.CanShow()); | 272 EXPECT_FALSE(notification_promo_.CanShow()); |
| 287 | 273 |
| 288 notification_promo_.closed_ = false; | 274 notification_promo_.closed_ = false; |
| 289 EXPECT_TRUE(notification_promo_.CanShow()); | 275 EXPECT_TRUE(notification_promo_.CanShow()); |
| 276 notification_promo_.WritePrefs(); |
| 290 } | 277 } |
| 291 | 278 |
| 292 void TestPromoText() { | 279 void TestPromoText() { |
| 293 notification_promo_.promo_text_.clear(); | 280 notification_promo_.promo_text_.clear(); |
| 294 EXPECT_FALSE(notification_promo_.CanShow()); | 281 EXPECT_FALSE(notification_promo_.CanShow()); |
| 295 | 282 |
| 296 notification_promo_.promo_text_ = promo_text_; | 283 notification_promo_.promo_text_ = promo_text_; |
| 297 EXPECT_TRUE(notification_promo_.CanShow()); | 284 EXPECT_TRUE(notification_promo_.CanShow()); |
| 298 } | 285 } |
| 299 | 286 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 bool gplus_required_; | 401 bool gplus_required_; |
| 415 }; | 402 }; |
| 416 | 403 |
| 417 TEST_F(PromoResourceServiceTest, NotificationPromoTest) { | 404 TEST_F(PromoResourceServiceTest, NotificationPromoTest) { |
| 418 // Check that prefs are set correctly. | 405 // Check that prefs are set correctly. |
| 419 PrefService* prefs = profile_.GetPrefs(); | 406 PrefService* prefs = profile_.GetPrefs(); |
| 420 ASSERT_TRUE(prefs != NULL); | 407 ASSERT_TRUE(prefs != NULL); |
| 421 | 408 |
| 422 NotificationPromoTest promo_test(&profile_); | 409 NotificationPromoTest promo_test(&profile_); |
| 423 | 410 |
| 424 // Make sure prefs are unset. | |
| 425 promo_test.TestPrefs(); | |
| 426 | |
| 427 // Set up start and end dates and promo line in a Dictionary as if parsed | 411 // Set up start and end dates and promo line in a Dictionary as if parsed |
| 428 // from the service. | 412 // from the service. |
| 429 #if !defined(OS_ANDROID) | 413 #if !defined(OS_ANDROID) |
| 430 promo_test.Init("{" | 414 promo_test.Init("{" |
| 431 " \"ntp_notification_promo\": [" | 415 " \"ntp_notification_promo\": [" |
| 432 " {" | 416 " {" |
| 433 " \"date\":" | 417 " \"date\":" |
| 434 " [" | 418 " [" |
| 435 " {" | 419 " {" |
| 436 " \"start\":\"15 Jan 2012 10:50:85 PST\"," | 420 " \"start\":\"15 Jan 2012 10:50:85 PST\"," |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 promo_test.TestIncrement(); | 518 promo_test.TestIncrement(); |
| 535 promo_test.TestGplus(); | 519 promo_test.TestGplus(); |
| 536 } | 520 } |
| 537 | 521 |
| 538 TEST_F(PromoResourceServiceTest, PromoServerURLTest) { | 522 TEST_F(PromoResourceServiceTest, PromoServerURLTest) { |
| 539 GURL promo_server_url = NotificationPromo::PromoServerURL(); | 523 GURL promo_server_url = NotificationPromo::PromoServerURL(); |
| 540 EXPECT_FALSE(promo_server_url.is_empty()); | 524 EXPECT_FALSE(promo_server_url.is_empty()); |
| 541 EXPECT_TRUE(promo_server_url.SchemeIs("https")); | 525 EXPECT_TRUE(promo_server_url.SchemeIs("https")); |
| 542 // TODO(achuith): Test this better. | 526 // TODO(achuith): Test this better. |
| 543 } | 527 } |
| OLD | NEW |