| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 temp_promo.InitFromVariations(); | 281 temp_promo.InitFromVariations(); |
| 282 temp_promo.InitFromPrefs(promo_type_); | 282 temp_promo.InitFromPrefs(promo_type_); |
| 283 | 283 |
| 284 EXPECT_EQ(first_viewed, temp_promo.first_view_time_); | 284 EXPECT_EQ(first_viewed, temp_promo.first_view_time_); |
| 285 | 285 |
| 286 notification_promo_.views_ = 0; | 286 notification_promo_.views_ = 0; |
| 287 notification_promo_.first_view_time_ = 0; | 287 notification_promo_.first_view_time_ = 0; |
| 288 notification_promo_.WritePrefs(); | 288 notification_promo_.WritePrefs(); |
| 289 } | 289 } |
| 290 | 290 |
| 291 // Tests that if data is saved in the old pref structure, it is successfully |
| 292 // migrated to the new structure that supports saving multiple promos. |
| 293 // TODO(crbug.com/623726) Remove this method when migration is no longer |
| 294 // needed as most users have been upgraded to the new pref structure. |
| 295 void TestMigrationOfOldPrefs() { |
| 296 NotificationPromo promo(&local_state_); |
| 297 promo.InitFromVariations(); |
| 298 |
| 299 // Pick values for each variable that is saved into old prefs structure. |
| 300 double first_view_time = 2.0; |
| 301 int views = max_views_ + 1; |
| 302 bool closed = true; |
| 303 |
| 304 // Save data into old prefs structure. |
| 305 base::DictionaryValue* ntp_promo = new base::DictionaryValue; |
| 306 ntp_promo->SetInteger("id", promo.promo_id_); |
| 307 ntp_promo->SetDouble("first_view_time", first_view_time); |
| 308 ntp_promo->SetInteger("views", views); |
| 309 ntp_promo->SetBoolean("closed", true); |
| 310 |
| 311 base::ListValue* promo_list = new base::ListValue; |
| 312 promo_list->Set(0, ntp_promo); |
| 313 |
| 314 std::string promo_list_key = "mobile_ntp_whats_new_promo"; |
| 315 std::string promo_dict_key = "ios.ntppromo"; |
| 316 |
| 317 base::DictionaryValue promo_dict; |
| 318 promo_dict.Set(promo_list_key, promo_list); |
| 319 local_state_.Set(promo_dict_key, promo_dict); |
| 320 |
| 321 // Initialize promo and verify that its instance variables match the data |
| 322 // saved in the old structure. |
| 323 promo.InitFromPrefs(promo_type_); |
| 324 EXPECT_EQ(first_view_time, promo.first_view_time_); |
| 325 EXPECT_EQ(views, promo.views_); |
| 326 EXPECT_EQ(closed, promo.closed_); |
| 327 EXPECT_FALSE(promo.CanShow()); |
| 328 |
| 329 // Verify that old pref structure was cleared. |
| 330 const base::DictionaryValue* current_promo_dict = |
| 331 local_state_.GetDictionary(promo_dict_key); |
| 332 // Do not continue further if no dictionary was found at the key in prefs. |
| 333 ASSERT_TRUE(current_promo_dict); |
| 334 const base::ListValue* current_promo_list = NULL; |
| 335 current_promo_dict->GetList(promo_list_key, ¤t_promo_list); |
| 336 EXPECT_FALSE(current_promo_list); |
| 337 } |
| 338 |
| 291 const NotificationPromo& promo() const { return notification_promo_; } | 339 const NotificationPromo& promo() const { return notification_promo_; } |
| 292 | 340 |
| 293 protected: | 341 protected: |
| 294 TestingPrefServiceSimple local_state_; | 342 TestingPrefServiceSimple local_state_; |
| 295 | 343 |
| 296 private: | 344 private: |
| 297 NotificationPromo notification_promo_; | 345 NotificationPromo notification_promo_; |
| 298 std::unique_ptr<base::FieldTrialList> field_trial_list_; | 346 std::unique_ptr<base::FieldTrialList> field_trial_list_; |
| 299 bool received_notification_; | 347 bool received_notification_; |
| 300 std::unique_ptr<base::DictionaryValue> test_json_; | 348 std::unique_ptr<base::DictionaryValue> test_json_; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 // Test various conditions of CanShow. | 416 // Test various conditions of CanShow. |
| 369 TestViews(); | 417 TestViews(); |
| 370 TestClosed(); | 418 TestClosed(); |
| 371 TestPromoText(); | 419 TestPromoText(); |
| 372 TestTime(); | 420 TestTime(); |
| 373 TestMaxTime(); | 421 TestMaxTime(); |
| 374 | 422 |
| 375 TestFirstViewTimeRecorded(); | 423 TestFirstViewTimeRecorded(); |
| 376 } | 424 } |
| 377 | 425 |
| 426 // TODO(crbug.com/623726) Remove this test case when migration is no longer |
| 427 // needed as most users have been upgraded to the new pref structure. |
| 428 TEST_F(NotificationPromoTest, NotificationPromoPrefMigrationTest) { |
| 429 Init( |
| 430 "{" |
| 431 " \"start\":\"3 Aug 1999 9:26:06 GMT\"," |
| 432 " \"end\":\"$1\"," |
| 433 " \"promo_text\":\"What do you think of Chrome?\"," |
| 434 " \"payload\":" |
| 435 " {" |
| 436 " \"days_active\":7," |
| 437 " \"install_age_days\":21" |
| 438 " }," |
| 439 " \"max_views\":30," |
| 440 " \"max_seconds\":30," |
| 441 " \"promo_id\":0" |
| 442 "}", |
| 443 "What do you think of Chrome?", |
| 444 933672366, // unix epoch for 3 Aug 1999 9:26:06 GMT. |
| 445 0, 30, 30); |
| 446 InitPromoFromVariations(); |
| 447 TestMigrationOfOldPrefs(); |
| 448 } |
| 449 |
| 378 } // namespace ios | 450 } // namespace ios |
| OLD | NEW |