| 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 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 } | 155 } |
| 156 | 156 |
| 157 void TestViews() { | 157 void TestViews() { |
| 158 notification_promo_.views_ = notification_promo_.max_views_ - 2; | 158 notification_promo_.views_ = notification_promo_.max_views_ - 2; |
| 159 notification_promo_.WritePrefs(); | 159 notification_promo_.WritePrefs(); |
| 160 | 160 |
| 161 // Initialize promo from saved prefs and server params. | 161 // Initialize promo from saved prefs and server params. |
| 162 NotificationPromo first_promo(&local_state_); | 162 NotificationPromo first_promo(&local_state_); |
| 163 first_promo.InitFromVariations(); | 163 first_promo.InitFromVariations(); |
| 164 first_promo.InitFromPrefs(promo_type_); | 164 first_promo.InitFromPrefs(promo_type_); |
| 165 | 165 EXPECT_EQ(first_promo.max_views_ - 2, first_promo.views_); |
| 166 first_promo.HandleViewed(); | |
| 167 EXPECT_EQ(first_promo.max_views_ - 1, first_promo.views_); | |
| 168 EXPECT_TRUE(first_promo.CanShow()); | 166 EXPECT_TRUE(first_promo.CanShow()); |
| 169 first_promo.HandleViewed(); | 167 first_promo.HandleViewed(); |
| 170 | 168 |
| 171 // Initialize another promo to test that the new views were recorded | 169 // Initialize another promo to test that the new views were recorded |
| 172 // correctly in prefs. | 170 // correctly in prefs. |
| 173 NotificationPromo second_promo(&local_state_); | 171 NotificationPromo second_promo(&local_state_); |
| 174 second_promo.InitFromVariations(); | 172 second_promo.InitFromVariations(); |
| 175 second_promo.InitFromPrefs(promo_type_); | 173 second_promo.InitFromPrefs(promo_type_); |
| 176 EXPECT_EQ(second_promo.max_views_, second_promo.views_); | 174 EXPECT_EQ(second_promo.max_views_ - 1, second_promo.views_); |
| 177 EXPECT_FALSE(second_promo.CanShow()); | 175 EXPECT_TRUE(second_promo.CanShow()); |
| 176 second_promo.HandleViewed(); |
| 177 |
| 178 NotificationPromo third_promo(&local_state_); |
| 179 third_promo.InitFromVariations(); |
| 180 third_promo.InitFromPrefs(promo_type_); |
| 181 EXPECT_EQ(third_promo.max_views_, third_promo.views_); |
| 182 EXPECT_FALSE(third_promo.CanShow()); |
| 178 | 183 |
| 179 // Test out of range views. | 184 // Test out of range views. |
| 180 for (int i = max_views_; i < max_views_ * 2; ++i) { | 185 for (int i = max_views_; i < max_views_ * 2; ++i) { |
| 181 second_promo.views_ = i; | 186 third_promo.views_ = i; |
| 182 EXPECT_FALSE(second_promo.CanShow()); | 187 EXPECT_FALSE(third_promo.CanShow()); |
| 183 } | 188 } |
| 184 | 189 |
| 185 // Test in range views. | 190 // Test in range views. |
| 186 for (int i = 0; i < max_views_; ++i) { | 191 for (int i = 0; i < max_views_; ++i) { |
| 187 second_promo.views_ = i; | 192 third_promo.views_ = i; |
| 188 EXPECT_TRUE(second_promo.CanShow()); | 193 EXPECT_TRUE(third_promo.CanShow()); |
| 189 } | 194 } |
| 190 | 195 |
| 191 // Reset prefs to default. | 196 // Reset prefs to default. |
| 192 notification_promo_.views_ = 0; | 197 notification_promo_.views_ = 0; |
| 193 notification_promo_.WritePrefs(); | 198 notification_promo_.WritePrefs(); |
| 194 } | 199 } |
| 195 | 200 |
| 196 void TestClosed() { | 201 void TestClosed() { |
| 197 // Initialize promo from saved prefs and server params. | 202 // Initialize promo from saved prefs and server params. |
| 198 NotificationPromo first_promo(&local_state_); | 203 NotificationPromo first_promo(&local_state_); |
| 199 first_promo.InitFromVariations(); | 204 first_promo.InitFromVariations(); |
| 200 first_promo.InitFromPrefs(promo_type_); | 205 first_promo.InitFromPrefs(promo_type_); |
| 201 EXPECT_FALSE(first_promo.closed_); | 206 EXPECT_FALSE(first_promo.closed_); |
| 202 EXPECT_TRUE(first_promo.CanShow()); | 207 EXPECT_TRUE(first_promo.CanShow()); |
| 203 | |
| 204 first_promo.HandleClosed(); | 208 first_promo.HandleClosed(); |
| 205 EXPECT_TRUE(first_promo.closed_); | |
| 206 EXPECT_FALSE(first_promo.CanShow()); | |
| 207 | 209 |
| 208 // Initialize another promo to test that the the closing of the promo was | 210 // Initialize another promo to test that the the closing of the promo was |
| 209 // recorded correctly in prefs. | 211 // recorded correctly in prefs. |
| 210 NotificationPromo second_promo(&local_state_); | 212 NotificationPromo second_promo(&local_state_); |
| 211 second_promo.InitFromVariations(); | 213 second_promo.InitFromVariations(); |
| 212 second_promo.InitFromPrefs(promo_type_); | 214 second_promo.InitFromPrefs(promo_type_); |
| 213 EXPECT_TRUE(second_promo.closed_); | 215 EXPECT_TRUE(second_promo.closed_); |
| 214 EXPECT_FALSE(second_promo.CanShow()); | 216 EXPECT_FALSE(second_promo.CanShow()); |
| 215 | 217 |
| 216 // Reset prefs to default. | 218 // Reset prefs to default. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 notification_promo_.first_view_time_ = 0; | 269 notification_promo_.first_view_time_ = 0; |
| 268 notification_promo_.max_seconds_ = max_seconds_; | 270 notification_promo_.max_seconds_ = max_seconds_; |
| 269 EXPECT_TRUE(notification_promo_.CanShow()); | 271 EXPECT_TRUE(notification_promo_.CanShow()); |
| 270 } | 272 } |
| 271 | 273 |
| 272 // Tests that the first view time is recorded properly in prefs when the | 274 // Tests that the first view time is recorded properly in prefs when the |
| 273 // first view occurs. | 275 // first view occurs. |
| 274 void TestFirstViewTimeRecorded() { | 276 void TestFirstViewTimeRecorded() { |
| 275 EXPECT_EQ(0, notification_promo_.first_view_time_); | 277 EXPECT_EQ(0, notification_promo_.first_view_time_); |
| 276 notification_promo_.HandleViewed(); | 278 notification_promo_.HandleViewed(); |
| 277 EXPECT_NE(0, notification_promo_.first_view_time_); | |
| 278 double first_viewed = notification_promo_.first_view_time_; | |
| 279 | 279 |
| 280 NotificationPromo temp_promo(&local_state_); | 280 NotificationPromo temp_promo(&local_state_); |
| 281 temp_promo.InitFromVariations(); | 281 temp_promo.InitFromVariations(); |
| 282 temp_promo.InitFromPrefs(promo_type_); | 282 temp_promo.InitFromPrefs(promo_type_); |
| 283 | 283 EXPECT_NE(0, temp_promo.first_view_time_); |
| 284 EXPECT_EQ(first_viewed, temp_promo.first_view_time_); | |
| 285 | 284 |
| 286 notification_promo_.views_ = 0; | 285 notification_promo_.views_ = 0; |
| 287 notification_promo_.first_view_time_ = 0; | 286 notification_promo_.first_view_time_ = 0; |
| 288 notification_promo_.WritePrefs(); | 287 notification_promo_.WritePrefs(); |
| 289 } | 288 } |
| 290 | 289 |
| 291 // Tests that if data is saved in the old pref structure, it is successfully | 290 // 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. | 291 // migrated to the new structure that supports saving multiple promos. |
| 293 // TODO(crbug.com/623726) Remove this method when migration is no longer | 292 // 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. | 293 // needed as most users have been upgraded to the new pref structure. |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 " \"promo_id\":0" | 440 " \"promo_id\":0" |
| 442 "}", | 441 "}", |
| 443 "What do you think of Chrome?", | 442 "What do you think of Chrome?", |
| 444 933672366, // unix epoch for 3 Aug 1999 9:26:06 GMT. | 443 933672366, // unix epoch for 3 Aug 1999 9:26:06 GMT. |
| 445 0, 30, 30); | 444 0, 30, 30); |
| 446 InitPromoFromVariations(); | 445 InitPromoFromVariations(); |
| 447 TestMigrationOfOldPrefs(); | 446 TestMigrationOfOldPrefs(); |
| 448 } | 447 } |
| 449 | 448 |
| 450 } // namespace ios | 449 } // namespace ios |
| OLD | NEW |