| 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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 it.Advance()) { | 104 it.Advance()) { |
| 105 std::string value; | 105 std::string value; |
| 106 it.value().GetAsString(&value); | 106 it.value().GetAsString(&value); |
| 107 field_trial_params[it.key()] = value; | 107 field_trial_params[it.key()] = value; |
| 108 } | 108 } |
| 109 | 109 |
| 110 variations::AssociateVariationParams("IOSNTPPromotion", "Group1", | 110 variations::AssociateVariationParams("IOSNTPPromotion", "Group1", |
| 111 field_trial_params); | 111 field_trial_params); |
| 112 base::FieldTrialList::CreateFieldTrial("IOSNTPPromotion", "Group1"); | 112 base::FieldTrialList::CreateFieldTrial("IOSNTPPromotion", "Group1"); |
| 113 | 113 |
| 114 promo_type_ = NotificationPromo::MOBILE_NTP_WHATS_NEW_PROMO; | |
| 115 promo_text_ = promo_text; | 114 promo_text_ = promo_text; |
| 116 | 115 |
| 117 start_ = start; | 116 start_ = start; |
| 118 end_ = year_from_now_epoch; | 117 end_ = year_from_now_epoch; |
| 119 | 118 |
| 120 promo_id_ = promo_id; | 119 promo_id_ = promo_id; |
| 121 max_views_ = max_views; | 120 max_views_ = max_views; |
| 122 max_seconds_ = max_seconds; | 121 max_seconds_ = max_seconds; |
| 123 | 122 |
| 124 closed_ = false; | 123 closed_ = false; |
| 125 received_notification_ = false; | 124 received_notification_ = false; |
| 126 } | 125 } |
| 127 | 126 |
| 128 void InitPromoFromVariations() { | 127 void InitPromoFromVariations() { |
| 129 notification_promo_.InitFromVariations(); | 128 notification_promo_.InitFromVariations(); |
| 130 | 129 |
| 131 // Test the fields. | 130 // Test the fields. |
| 132 TestServerProvidedParameters(); | 131 TestServerProvidedParameters(); |
| 133 } | 132 } |
| 134 | 133 |
| 135 void InitPromoFromJson() { | 134 void InitPromoFromJson() { |
| 136 notification_promo_.InitFromJson(*test_json_, promo_type_); | 135 notification_promo_.InitFromJson(*test_json_); |
| 137 | 136 |
| 138 // Test the fields. | 137 // Test the fields. |
| 139 TestServerProvidedParameters(); | 138 TestServerProvidedParameters(); |
| 140 } | 139 } |
| 141 | 140 |
| 142 void TestServerProvidedParameters() { | 141 void TestServerProvidedParameters() { |
| 143 // Check values. | 142 // Check values. |
| 144 EXPECT_EQ(notification_promo_.promo_text_, promo_text_); | 143 EXPECT_EQ(notification_promo_.promo_text_, promo_text_); |
| 145 | 144 |
| 146 EXPECT_DOUBLE_EQ(notification_promo_.start_, start_); | 145 EXPECT_DOUBLE_EQ(notification_promo_.start_, start_); |
| 147 EXPECT_DOUBLE_EQ(notification_promo_.end_, end_); | 146 EXPECT_DOUBLE_EQ(notification_promo_.end_, end_); |
| 148 | 147 |
| 149 EXPECT_EQ(notification_promo_.promo_id_, promo_id_); | 148 EXPECT_EQ(notification_promo_.promo_id_, promo_id_); |
| 150 EXPECT_EQ(notification_promo_.max_views_, max_views_); | 149 EXPECT_EQ(notification_promo_.max_views_, max_views_); |
| 151 EXPECT_EQ(notification_promo_.max_seconds_, max_seconds_); | 150 EXPECT_EQ(notification_promo_.max_seconds_, max_seconds_); |
| 152 } | 151 } |
| 153 | 152 |
| 154 void TestViews() { | 153 void TestViews() { |
| 155 notification_promo_.views_ = notification_promo_.max_views_ - 2; | 154 notification_promo_.views_ = notification_promo_.max_views_ - 2; |
| 156 notification_promo_.WritePrefs(); | 155 notification_promo_.WritePrefs(); |
| 157 | 156 |
| 158 // Initialize promo from saved prefs and server params. | 157 // Initialize promo from saved prefs and server params. |
| 159 NotificationPromo first_promo(&local_state_); | 158 NotificationPromo first_promo(&local_state_); |
| 160 first_promo.InitFromVariations(); | 159 first_promo.InitFromVariations(); |
| 161 first_promo.InitFromPrefs(promo_type_); | 160 first_promo.InitFromPrefs(); |
| 162 EXPECT_EQ(first_promo.max_views_ - 2, first_promo.views_); | 161 EXPECT_EQ(first_promo.max_views_ - 2, first_promo.views_); |
| 163 EXPECT_TRUE(first_promo.CanShow()); | 162 EXPECT_TRUE(first_promo.CanShow()); |
| 164 first_promo.HandleViewed(); | 163 first_promo.HandleViewed(); |
| 165 | 164 |
| 166 // Initialize another promo to test that the new views were recorded | 165 // Initialize another promo to test that the new views were recorded |
| 167 // correctly in prefs. | 166 // correctly in prefs. |
| 168 NotificationPromo second_promo(&local_state_); | 167 NotificationPromo second_promo(&local_state_); |
| 169 second_promo.InitFromVariations(); | 168 second_promo.InitFromVariations(); |
| 170 second_promo.InitFromPrefs(promo_type_); | 169 second_promo.InitFromPrefs(); |
| 171 EXPECT_EQ(second_promo.max_views_ - 1, second_promo.views_); | 170 EXPECT_EQ(second_promo.max_views_ - 1, second_promo.views_); |
| 172 EXPECT_TRUE(second_promo.CanShow()); | 171 EXPECT_TRUE(second_promo.CanShow()); |
| 173 second_promo.HandleViewed(); | 172 second_promo.HandleViewed(); |
| 174 | 173 |
| 175 NotificationPromo third_promo(&local_state_); | 174 NotificationPromo third_promo(&local_state_); |
| 176 third_promo.InitFromVariations(); | 175 third_promo.InitFromVariations(); |
| 177 third_promo.InitFromPrefs(promo_type_); | 176 third_promo.InitFromPrefs(); |
| 178 EXPECT_EQ(third_promo.max_views_, third_promo.views_); | 177 EXPECT_EQ(third_promo.max_views_, third_promo.views_); |
| 179 EXPECT_FALSE(third_promo.CanShow()); | 178 EXPECT_FALSE(third_promo.CanShow()); |
| 180 | 179 |
| 181 // Test out of range views. | 180 // Test out of range views. |
| 182 for (int i = max_views_; i < max_views_ * 2; ++i) { | 181 for (int i = max_views_; i < max_views_ * 2; ++i) { |
| 183 third_promo.views_ = i; | 182 third_promo.views_ = i; |
| 184 EXPECT_FALSE(third_promo.CanShow()); | 183 EXPECT_FALSE(third_promo.CanShow()); |
| 185 } | 184 } |
| 186 | 185 |
| 187 // Test in range views. | 186 // Test in range views. |
| 188 for (int i = 0; i < max_views_; ++i) { | 187 for (int i = 0; i < max_views_; ++i) { |
| 189 third_promo.views_ = i; | 188 third_promo.views_ = i; |
| 190 EXPECT_TRUE(third_promo.CanShow()); | 189 EXPECT_TRUE(third_promo.CanShow()); |
| 191 } | 190 } |
| 192 | 191 |
| 193 // Reset prefs to default. | 192 // Reset prefs to default. |
| 194 notification_promo_.views_ = 0; | 193 notification_promo_.views_ = 0; |
| 195 notification_promo_.WritePrefs(); | 194 notification_promo_.WritePrefs(); |
| 196 } | 195 } |
| 197 | 196 |
| 198 void TestClosed() { | 197 void TestClosed() { |
| 199 // Initialize promo from saved prefs and server params. | 198 // Initialize promo from saved prefs and server params. |
| 200 NotificationPromo first_promo(&local_state_); | 199 NotificationPromo first_promo(&local_state_); |
| 201 first_promo.InitFromVariations(); | 200 first_promo.InitFromVariations(); |
| 202 first_promo.InitFromPrefs(promo_type_); | 201 first_promo.InitFromPrefs(); |
| 203 EXPECT_FALSE(first_promo.closed_); | 202 EXPECT_FALSE(first_promo.closed_); |
| 204 EXPECT_TRUE(first_promo.CanShow()); | 203 EXPECT_TRUE(first_promo.CanShow()); |
| 205 first_promo.HandleClosed(); | 204 first_promo.HandleClosed(); |
| 206 | 205 |
| 207 // Initialize another promo to test that the the closing of the promo was | 206 // Initialize another promo to test that the the closing of the promo was |
| 208 // recorded correctly in prefs. | 207 // recorded correctly in prefs. |
| 209 NotificationPromo second_promo(&local_state_); | 208 NotificationPromo second_promo(&local_state_); |
| 210 second_promo.InitFromVariations(); | 209 second_promo.InitFromVariations(); |
| 211 second_promo.InitFromPrefs(promo_type_); | 210 second_promo.InitFromPrefs(); |
| 212 EXPECT_TRUE(second_promo.closed_); | 211 EXPECT_TRUE(second_promo.closed_); |
| 213 EXPECT_FALSE(second_promo.CanShow()); | 212 EXPECT_FALSE(second_promo.CanShow()); |
| 214 | 213 |
| 215 // Reset prefs to default. | 214 // Reset prefs to default. |
| 216 second_promo.closed_ = false; | 215 second_promo.closed_ = false; |
| 217 EXPECT_TRUE(second_promo.CanShow()); | 216 EXPECT_TRUE(second_promo.CanShow()); |
| 218 second_promo.WritePrefs(); | 217 second_promo.WritePrefs(); |
| 219 } | 218 } |
| 220 | 219 |
| 221 void TestPromoText() { | 220 void TestPromoText() { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 } | 268 } |
| 270 | 269 |
| 271 // Tests that the first view time is recorded properly in prefs when the | 270 // Tests that the first view time is recorded properly in prefs when the |
| 272 // first view occurs. | 271 // first view occurs. |
| 273 void TestFirstViewTimeRecorded() { | 272 void TestFirstViewTimeRecorded() { |
| 274 EXPECT_DOUBLE_EQ(0, notification_promo_.first_view_time_); | 273 EXPECT_DOUBLE_EQ(0, notification_promo_.first_view_time_); |
| 275 notification_promo_.HandleViewed(); | 274 notification_promo_.HandleViewed(); |
| 276 | 275 |
| 277 NotificationPromo temp_promo(&local_state_); | 276 NotificationPromo temp_promo(&local_state_); |
| 278 temp_promo.InitFromVariations(); | 277 temp_promo.InitFromVariations(); |
| 279 temp_promo.InitFromPrefs(promo_type_); | 278 temp_promo.InitFromPrefs(); |
| 280 EXPECT_NE(0, temp_promo.first_view_time_); | 279 EXPECT_NE(0, temp_promo.first_view_time_); |
| 281 | 280 |
| 282 notification_promo_.views_ = 0; | 281 notification_promo_.views_ = 0; |
| 283 notification_promo_.first_view_time_ = 0; | 282 notification_promo_.first_view_time_ = 0; |
| 284 notification_promo_.WritePrefs(); | 283 notification_promo_.WritePrefs(); |
| 285 } | 284 } |
| 286 | 285 |
| 287 // Tests that if data is saved in the old pref structure, it is successfully | 286 // Tests that if data is saved in the old pref structure, it is successfully |
| 288 // migrated to the new structure that supports saving multiple promos. | 287 // migrated to the new structure that supports saving multiple promos. |
| 289 // TODO(crbug.com/623726) Remove this method when migration is no longer | 288 // TODO(crbug.com/623726) Remove this method when migration is no longer |
| (...skipping 19 matching lines...) Expand all Loading... |
| 309 | 308 |
| 310 std::string promo_list_key = "mobile_ntp_whats_new_promo"; | 309 std::string promo_list_key = "mobile_ntp_whats_new_promo"; |
| 311 std::string promo_dict_key = "ios.ntppromo"; | 310 std::string promo_dict_key = "ios.ntppromo"; |
| 312 | 311 |
| 313 base::DictionaryValue promo_dict; | 312 base::DictionaryValue promo_dict; |
| 314 promo_dict.Set(promo_list_key, promo_list); | 313 promo_dict.Set(promo_list_key, promo_list); |
| 315 local_state_.Set(promo_dict_key, promo_dict); | 314 local_state_.Set(promo_dict_key, promo_dict); |
| 316 | 315 |
| 317 // Initialize promo and verify that its instance variables match the data | 316 // Initialize promo and verify that its instance variables match the data |
| 318 // saved in the old structure. | 317 // saved in the old structure. |
| 319 promo.InitFromPrefs(promo_type_); | 318 promo.InitFromPrefs(); |
| 320 EXPECT_DOUBLE_EQ(first_view_time, promo.first_view_time_); | 319 EXPECT_DOUBLE_EQ(first_view_time, promo.first_view_time_); |
| 321 EXPECT_EQ(views, promo.views_); | 320 EXPECT_EQ(views, promo.views_); |
| 322 EXPECT_EQ(closed, promo.closed_); | 321 EXPECT_EQ(closed, promo.closed_); |
| 323 EXPECT_FALSE(promo.CanShow()); | 322 EXPECT_FALSE(promo.CanShow()); |
| 324 | 323 |
| 325 // Verify that old pref structure was cleared. | 324 // Verify that old pref structure was cleared. |
| 326 const base::DictionaryValue* current_promo_dict = | 325 const base::DictionaryValue* current_promo_dict = |
| 327 local_state_.GetDictionary(promo_dict_key); | 326 local_state_.GetDictionary(promo_dict_key); |
| 328 // Do not continue further if no dictionary was found at the key in prefs. | 327 // Do not continue further if no dictionary was found at the key in prefs. |
| 329 ASSERT_TRUE(current_promo_dict); | 328 ASSERT_TRUE(current_promo_dict); |
| 330 const base::ListValue* current_promo_list = NULL; | 329 const base::ListValue* current_promo_list = NULL; |
| 331 current_promo_dict->GetList(promo_list_key, ¤t_promo_list); | 330 current_promo_dict->GetList(promo_list_key, ¤t_promo_list); |
| 332 EXPECT_FALSE(current_promo_list); | 331 EXPECT_FALSE(current_promo_list); |
| 333 } | 332 } |
| 334 | 333 |
| 335 const NotificationPromo& promo() const { return notification_promo_; } | 334 const NotificationPromo& promo() const { return notification_promo_; } |
| 336 | 335 |
| 337 protected: | 336 protected: |
| 338 TestingPrefServiceSimple local_state_; | 337 TestingPrefServiceSimple local_state_; |
| 339 | 338 |
| 340 private: | 339 private: |
| 341 NotificationPromo notification_promo_; | 340 NotificationPromo notification_promo_; |
| 342 std::unique_ptr<base::FieldTrialList> field_trial_list_; | 341 std::unique_ptr<base::FieldTrialList> field_trial_list_; |
| 343 bool received_notification_; | 342 bool received_notification_; |
| 344 std::unique_ptr<base::DictionaryValue> test_json_; | 343 std::unique_ptr<base::DictionaryValue> test_json_; |
| 345 | 344 |
| 346 NotificationPromo::PromoType promo_type_; | |
| 347 std::string promo_text_; | 345 std::string promo_text_; |
| 348 | 346 |
| 349 double start_; | 347 double start_; |
| 350 double end_; | 348 double end_; |
| 351 int promo_id_; | 349 int promo_id_; |
| 352 int max_views_; | 350 int max_views_; |
| 353 int max_seconds_; | 351 int max_seconds_; |
| 354 | 352 |
| 355 bool closed_; | 353 bool closed_; |
| 356 }; | 354 }; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 " \"promo_id\":0" | 435 " \"promo_id\":0" |
| 438 "}", | 436 "}", |
| 439 "What do you think of Chrome?", | 437 "What do you think of Chrome?", |
| 440 933672366, // unix epoch for 3 Aug 1999 9:26:06 GMT. | 438 933672366, // unix epoch for 3 Aug 1999 9:26:06 GMT. |
| 441 0, 30, 30); | 439 0, 30, 30); |
| 442 InitPromoFromVariations(); | 440 InitPromoFromVariations(); |
| 443 TestMigrationOfOldPrefs(); | 441 TestMigrationOfOldPrefs(); |
| 444 } | 442 } |
| 445 | 443 |
| 446 } // namespace ios | 444 } // namespace ios |
| OLD | NEW |