| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 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 "chrome/browser/engagement/site_engagement_score.h" | 5 #include "chrome/browser/engagement/site_engagement_score.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/test/simple_test_clock.h" | 10 #include "base/test/simple_test_clock.h" |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 // Accumulate score many times on the same day. Ensure each time the score goes | 107 // Accumulate score many times on the same day. Ensure each time the score goes |
| 108 // up, but not more than the maximum per day. | 108 // up, but not more than the maximum per day. |
| 109 TEST_F(SiteEngagementScoreTest, AccumulateOnSameDay) { | 109 TEST_F(SiteEngagementScoreTest, AccumulateOnSameDay) { |
| 110 base::Time reference_time = GetReferenceTime(); | 110 base::Time reference_time = GetReferenceTime(); |
| 111 | 111 |
| 112 test_clock_.SetNow(reference_time); | 112 test_clock_.SetNow(reference_time); |
| 113 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { | 113 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { |
| 114 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 114 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
| 115 EXPECT_EQ(std::min(SiteEngagementScore::GetMaxPointsPerDay(), | 115 EXPECT_EQ(std::min(SiteEngagementScore::GetMaxPointsPerDay(), |
| 116 (i + 1) * SiteEngagementScore::GetNavigationPoints()), | 116 (i + 1) * SiteEngagementScore::GetNavigationPoints()), |
| 117 score_.GetScore()); | 117 score_.GetTotal()); |
| 118 } | 118 } |
| 119 | 119 |
| 120 EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetScore()); | 120 EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetTotal()); |
| 121 } | 121 } |
| 122 | 122 |
| 123 // Accumulate on the first day to max that day's engagement, then accumulate on | 123 // Accumulate on the first day to max that day's engagement, then accumulate on |
| 124 // a different day. | 124 // a different day. |
| 125 TEST_F(SiteEngagementScoreTest, AccumulateOnTwoDays) { | 125 TEST_F(SiteEngagementScoreTest, AccumulateOnTwoDays) { |
| 126 base::Time reference_time = GetReferenceTime(); | 126 base::Time reference_time = GetReferenceTime(); |
| 127 base::Time later_date = reference_time + base::TimeDelta::FromDays(2); | 127 base::Time later_date = reference_time + base::TimeDelta::FromDays(2); |
| 128 | 128 |
| 129 test_clock_.SetNow(reference_time); | 129 test_clock_.SetNow(reference_time); |
| 130 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) | 130 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) |
| 131 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 131 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
| 132 | 132 |
| 133 EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetScore()); | 133 EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetTotal()); |
| 134 | 134 |
| 135 test_clock_.SetNow(later_date); | 135 test_clock_.SetNow(later_date); |
| 136 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { | 136 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { |
| 137 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 137 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
| 138 double day_score = | 138 double day_score = |
| 139 std::min(SiteEngagementScore::GetMaxPointsPerDay(), | 139 std::min(SiteEngagementScore::GetMaxPointsPerDay(), |
| 140 (i + 1) * SiteEngagementScore::GetNavigationPoints()); | 140 (i + 1) * SiteEngagementScore::GetNavigationPoints()); |
| 141 EXPECT_EQ(day_score + SiteEngagementScore::GetMaxPointsPerDay(), | 141 EXPECT_EQ(day_score + SiteEngagementScore::GetMaxPointsPerDay(), |
| 142 score_.GetScore()); | 142 score_.GetTotal()); |
| 143 } | 143 } |
| 144 | 144 |
| 145 EXPECT_EQ(2 * SiteEngagementScore::GetMaxPointsPerDay(), score_.GetScore()); | 145 EXPECT_EQ(2 * SiteEngagementScore::GetMaxPointsPerDay(), score_.GetTotal()); |
| 146 } | 146 } |
| 147 | 147 |
| 148 // Accumulate score on many consecutive days and ensure the score doesn't exceed | 148 // Accumulate score on many consecutive days and ensure the score doesn't exceed |
| 149 // the maximum allowed. | 149 // the maximum allowed. |
| 150 TEST_F(SiteEngagementScoreTest, AccumulateALotOnManyDays) { | 150 TEST_F(SiteEngagementScoreTest, AccumulateALotOnManyDays) { |
| 151 base::Time current_day = GetReferenceTime(); | 151 base::Time current_day = GetReferenceTime(); |
| 152 | 152 |
| 153 for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) { | 153 for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) { |
| 154 current_day += base::TimeDelta::FromDays(1); | 154 current_day += base::TimeDelta::FromDays(1); |
| 155 test_clock_.SetNow(current_day); | 155 test_clock_.SetNow(current_day); |
| 156 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) | 156 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) |
| 157 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 157 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
| 158 | 158 |
| 159 EXPECT_EQ(std::min(SiteEngagementScore::kMaxPoints, | 159 EXPECT_EQ(std::min(SiteEngagementScore::kMaxPoints, |
| 160 (i + 1) * SiteEngagementScore::GetMaxPointsPerDay()), | 160 (i + 1) * SiteEngagementScore::GetMaxPointsPerDay()), |
| 161 score_.GetScore()); | 161 score_.GetTotal()); |
| 162 } | 162 } |
| 163 | 163 |
| 164 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetScore()); | 164 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetTotal()); |
| 165 } | 165 } |
| 166 | 166 |
| 167 // Accumulate a little on many consecutive days and ensure the score doesn't | 167 // Accumulate a little on many consecutive days and ensure the score doesn't |
| 168 // exceed the maximum allowed. | 168 // exceed the maximum allowed. |
| 169 TEST_F(SiteEngagementScoreTest, AccumulateALittleOnManyDays) { | 169 TEST_F(SiteEngagementScoreTest, AccumulateALittleOnManyDays) { |
| 170 base::Time current_day = GetReferenceTime(); | 170 base::Time current_day = GetReferenceTime(); |
| 171 | 171 |
| 172 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxTotalEngagement; ++i) { | 172 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxTotalEngagement; ++i) { |
| 173 current_day += base::TimeDelta::FromDays(1); | 173 current_day += base::TimeDelta::FromDays(1); |
| 174 test_clock_.SetNow(current_day); | 174 test_clock_.SetNow(current_day); |
| 175 | 175 |
| 176 for (int j = 0; j < kLessAccumulationsThanNeededToMaxDailyEngagement; ++j) | 176 for (int j = 0; j < kLessAccumulationsThanNeededToMaxDailyEngagement; ++j) |
| 177 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 177 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
| 178 | 178 |
| 179 EXPECT_EQ( | 179 EXPECT_EQ( |
| 180 std::min(SiteEngagementScore::kMaxPoints, | 180 std::min(SiteEngagementScore::kMaxPoints, |
| 181 (i + 1) * kLessAccumulationsThanNeededToMaxDailyEngagement * | 181 (i + 1) * kLessAccumulationsThanNeededToMaxDailyEngagement * |
| 182 SiteEngagementScore::GetNavigationPoints()), | 182 SiteEngagementScore::GetNavigationPoints()), |
| 183 score_.GetScore()); | 183 score_.GetTotal()); |
| 184 } | 184 } |
| 185 | 185 |
| 186 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetScore()); | 186 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetTotal()); |
| 187 } | 187 } |
| 188 | 188 |
| 189 // Accumulate a bit, then check the score decays properly for a range of times. | 189 // Accumulate a bit, then check the score decays properly for a range of times. |
| 190 TEST_F(SiteEngagementScoreTest, ScoresDecayOverTime) { | 190 TEST_F(SiteEngagementScoreTest, ScoresDecayOverTime) { |
| 191 base::Time current_day = GetReferenceTime(); | 191 base::Time current_day = GetReferenceTime(); |
| 192 | 192 |
| 193 // First max the score. | 193 // First max the score. |
| 194 for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) { | 194 for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) { |
| 195 current_day += base::TimeDelta::FromDays(1); | 195 current_day += base::TimeDelta::FromDays(1); |
| 196 test_clock_.SetNow(current_day); | 196 test_clock_.SetNow(current_day); |
| 197 | 197 |
| 198 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) | 198 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) |
| 199 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 199 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
| 200 } | 200 } |
| 201 | 201 |
| 202 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetScore()); | 202 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetTotal()); |
| 203 | 203 |
| 204 // The score should not have decayed before the first decay period has | 204 // The score should not have decayed before the first decay period has |
| 205 // elapsed. | 205 // elapsed. |
| 206 test_clock_.SetNow(current_day + | 206 test_clock_.SetNow(current_day + |
| 207 base::TimeDelta::FromHours( | 207 base::TimeDelta::FromHours( |
| 208 SiteEngagementScore::GetDecayPeriodInHours() - 1)); | 208 SiteEngagementScore::GetDecayPeriodInHours() - 1)); |
| 209 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetScore()); | 209 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.GetTotal()); |
| 210 | 210 |
| 211 // The score should have decayed by one chunk after one decay period has | 211 // The score should have decayed by one chunk after one decay period has |
| 212 // elapsed. | 212 // elapsed. |
| 213 test_clock_.SetNow( | 213 test_clock_.SetNow( |
| 214 current_day + | 214 current_day + |
| 215 base::TimeDelta::FromHours(SiteEngagementScore::GetDecayPeriodInHours())); | 215 base::TimeDelta::FromHours(SiteEngagementScore::GetDecayPeriodInHours())); |
| 216 EXPECT_EQ( | 216 EXPECT_EQ( |
| 217 SiteEngagementScore::kMaxPoints - SiteEngagementScore::GetDecayPoints(), | 217 SiteEngagementScore::kMaxPoints - SiteEngagementScore::GetDecayPoints(), |
| 218 score_.GetScore()); | 218 score_.GetTotal()); |
| 219 | 219 |
| 220 // The score should have decayed by the right number of chunks after a few | 220 // The score should have decayed by the right number of chunks after a few |
| 221 // decay periods have elapsed. | 221 // decay periods have elapsed. |
| 222 test_clock_.SetNow( | 222 test_clock_.SetNow( |
| 223 current_day + | 223 current_day + |
| 224 base::TimeDelta::FromHours(kLessPeriodsThanNeededToDecayMaxScore * | 224 base::TimeDelta::FromHours(kLessPeriodsThanNeededToDecayMaxScore * |
| 225 SiteEngagementScore::GetDecayPeriodInHours())); | 225 SiteEngagementScore::GetDecayPeriodInHours())); |
| 226 EXPECT_EQ(SiteEngagementScore::kMaxPoints - | 226 EXPECT_EQ(SiteEngagementScore::kMaxPoints - |
| 227 kLessPeriodsThanNeededToDecayMaxScore * | 227 kLessPeriodsThanNeededToDecayMaxScore * |
| 228 SiteEngagementScore::GetDecayPoints(), | 228 SiteEngagementScore::GetDecayPoints(), |
| 229 score_.GetScore()); | 229 score_.GetTotal()); |
| 230 | 230 |
| 231 // The score should not decay below zero. | 231 // The score should not decay below zero. |
| 232 test_clock_.SetNow( | 232 test_clock_.SetNow( |
| 233 current_day + | 233 current_day + |
| 234 base::TimeDelta::FromHours(kMorePeriodsThanNeededToDecayMaxScore * | 234 base::TimeDelta::FromHours(kMorePeriodsThanNeededToDecayMaxScore * |
| 235 SiteEngagementScore::GetDecayPeriodInHours())); | 235 SiteEngagementScore::GetDecayPeriodInHours())); |
| 236 EXPECT_EQ(0, score_.GetScore()); | 236 EXPECT_EQ(0, score_.GetTotal()); |
| 237 } | 237 } |
| 238 | 238 |
| 239 // Test that any expected decays are applied before adding points. | 239 // Test that any expected decays are applied before adding points. |
| 240 TEST_F(SiteEngagementScoreTest, DecaysAppliedBeforeAdd) { | 240 TEST_F(SiteEngagementScoreTest, DecaysAppliedBeforeAdd) { |
| 241 base::Time current_day = GetReferenceTime(); | 241 base::Time current_day = GetReferenceTime(); |
| 242 | 242 |
| 243 // Get the score up to something that can handle a bit of decay before | 243 // Get the score up to something that can handle a bit of decay before |
| 244 for (int i = 0; i < kLessDaysThanNeededToMaxTotalEngagement; ++i) { | 244 for (int i = 0; i < kLessDaysThanNeededToMaxTotalEngagement; ++i) { |
| 245 current_day += base::TimeDelta::FromDays(1); | 245 current_day += base::TimeDelta::FromDays(1); |
| 246 test_clock_.SetNow(current_day); | 246 test_clock_.SetNow(current_day); |
| 247 | 247 |
| 248 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) | 248 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) |
| 249 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 249 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
| 250 } | 250 } |
| 251 | 251 |
| 252 double initial_score = kLessDaysThanNeededToMaxTotalEngagement * | 252 double initial_score = kLessDaysThanNeededToMaxTotalEngagement * |
| 253 SiteEngagementScore::GetMaxPointsPerDay(); | 253 SiteEngagementScore::GetMaxPointsPerDay(); |
| 254 EXPECT_EQ(initial_score, score_.GetScore()); | 254 EXPECT_EQ(initial_score, score_.GetTotal()); |
| 255 | 255 |
| 256 // Go forward a few decay periods. | 256 // Go forward a few decay periods. |
| 257 test_clock_.SetNow( | 257 test_clock_.SetNow( |
| 258 current_day + | 258 current_day + |
| 259 base::TimeDelta::FromHours(kLessPeriodsThanNeededToDecayMaxScore * | 259 base::TimeDelta::FromHours(kLessPeriodsThanNeededToDecayMaxScore * |
| 260 SiteEngagementScore::GetDecayPeriodInHours())); | 260 SiteEngagementScore::GetDecayPeriodInHours())); |
| 261 | 261 |
| 262 double decayed_score = initial_score - | 262 double decayed_score = initial_score - |
| 263 kLessPeriodsThanNeededToDecayMaxScore * | 263 kLessPeriodsThanNeededToDecayMaxScore * |
| 264 SiteEngagementScore::GetDecayPoints(); | 264 SiteEngagementScore::GetDecayPoints(); |
| 265 EXPECT_EQ(decayed_score, score_.GetScore()); | 265 EXPECT_EQ(decayed_score, score_.GetTotal()); |
| 266 | 266 |
| 267 // Now add some points. | 267 // Now add some points. |
| 268 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 268 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
| 269 EXPECT_EQ(decayed_score + SiteEngagementScore::GetNavigationPoints(), | 269 EXPECT_EQ(decayed_score + SiteEngagementScore::GetNavigationPoints(), |
| 270 score_.GetScore()); | 270 score_.GetTotal()); |
| 271 } | 271 } |
| 272 | 272 |
| 273 // Test that going back in time is handled properly. | 273 // Test that going back in time is handled properly. |
| 274 TEST_F(SiteEngagementScoreTest, GoBackInTime) { | 274 TEST_F(SiteEngagementScoreTest, GoBackInTime) { |
| 275 base::Time current_day = GetReferenceTime(); | 275 base::Time current_day = GetReferenceTime(); |
| 276 | 276 |
| 277 test_clock_.SetNow(current_day); | 277 test_clock_.SetNow(current_day); |
| 278 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) | 278 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) |
| 279 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 279 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
| 280 | 280 |
| 281 EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetScore()); | 281 EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.GetTotal()); |
| 282 | 282 |
| 283 // Adding to the score on an earlier date should be treated like another day, | 283 // Adding to the score on an earlier date should be treated like another day, |
| 284 // and should not cause any decay. | 284 // and should not cause any decay. |
| 285 test_clock_.SetNow(current_day - base::TimeDelta::FromDays( | 285 test_clock_.SetNow(current_day - base::TimeDelta::FromDays( |
| 286 kMorePeriodsThanNeededToDecayMaxScore * | 286 kMorePeriodsThanNeededToDecayMaxScore * |
| 287 SiteEngagementScore::GetDecayPoints())); | 287 SiteEngagementScore::GetDecayPoints())); |
| 288 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { | 288 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { |
| 289 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 289 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
| 290 double day_score = | 290 double day_score = |
| 291 std::min(SiteEngagementScore::GetMaxPointsPerDay(), | 291 std::min(SiteEngagementScore::GetMaxPointsPerDay(), |
| 292 (i + 1) * SiteEngagementScore::GetNavigationPoints()); | 292 (i + 1) * SiteEngagementScore::GetNavigationPoints()); |
| 293 EXPECT_EQ(day_score + SiteEngagementScore::GetMaxPointsPerDay(), | 293 EXPECT_EQ(day_score + SiteEngagementScore::GetMaxPointsPerDay(), |
| 294 score_.GetScore()); | 294 score_.GetTotal()); |
| 295 } | 295 } |
| 296 | 296 |
| 297 EXPECT_EQ(2 * SiteEngagementScore::GetMaxPointsPerDay(), score_.GetScore()); | 297 EXPECT_EQ(2 * SiteEngagementScore::GetMaxPointsPerDay(), score_.GetTotal()); |
| 298 } | 298 } |
| 299 | 299 |
| 300 // Test that scores are read / written correctly from / to empty score | 300 // Test that scores are read / written correctly from / to empty score |
| 301 // dictionaries. | 301 // dictionaries. |
| 302 TEST_F(SiteEngagementScoreTest, EmptyDictionary) { | 302 TEST_F(SiteEngagementScoreTest, EmptyDictionary) { |
| 303 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 303 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 304 TestScoreInitializesAndUpdates(std::move(dict), 0, 0, base::Time()); | 304 TestScoreInitializesAndUpdates(std::move(dict), 0, 0, base::Time()); |
| 305 } | 305 } |
| 306 | 306 |
| 307 // Test that scores are read / written correctly from / to partially empty | 307 // Test that scores are read / written correctly from / to partially empty |
| (...skipping 24 matching lines...) Expand all Loading... |
| 332 SiteEngagementScore score1(&test_clock_, GURL(), | 332 SiteEngagementScore score1(&test_clock_, GURL(), |
| 333 std::unique_ptr<base::DictionaryValue>()); | 333 std::unique_ptr<base::DictionaryValue>()); |
| 334 SiteEngagementScore score2(&test_clock_, GURL(), | 334 SiteEngagementScore score2(&test_clock_, GURL(), |
| 335 std::unique_ptr<base::DictionaryValue>()); | 335 std::unique_ptr<base::DictionaryValue>()); |
| 336 base::Time current_day = GetReferenceTime(); | 336 base::Time current_day = GetReferenceTime(); |
| 337 | 337 |
| 338 test_clock_.SetNow(current_day); | 338 test_clock_.SetNow(current_day); |
| 339 | 339 |
| 340 // The first engagement event gets the bonus. | 340 // The first engagement event gets the bonus. |
| 341 score1.AddPoints(0.5); | 341 score1.AddPoints(0.5); |
| 342 EXPECT_EQ(1.0, score1.GetScore()); | 342 EXPECT_EQ(1.0, score1.GetTotal()); |
| 343 | 343 |
| 344 // Subsequent events do not. | 344 // Subsequent events do not. |
| 345 score1.AddPoints(0.5); | 345 score1.AddPoints(0.5); |
| 346 EXPECT_EQ(1.5, score1.GetScore()); | 346 EXPECT_EQ(1.5, score1.GetTotal()); |
| 347 | 347 |
| 348 // Bonuses are awarded independently between scores. | 348 // Bonuses are awarded independently between scores. |
| 349 score2.AddPoints(1.0); | 349 score2.AddPoints(1.0); |
| 350 EXPECT_EQ(1.5, score2.GetScore()); | 350 EXPECT_EQ(1.5, score2.GetTotal()); |
| 351 score2.AddPoints(1.0); | 351 score2.AddPoints(1.0); |
| 352 EXPECT_EQ(2.5, score2.GetScore()); | 352 EXPECT_EQ(2.5, score2.GetTotal()); |
| 353 | 353 |
| 354 test_clock_.SetNow(current_day + base::TimeDelta::FromDays(1)); | 354 test_clock_.SetNow(current_day + base::TimeDelta::FromDays(1)); |
| 355 | 355 |
| 356 // The first event for the next day gets the bonus. | 356 // The first event for the next day gets the bonus. |
| 357 score1.AddPoints(0.5); | 357 score1.AddPoints(0.5); |
| 358 EXPECT_EQ(2.5, score1.GetScore()); | 358 EXPECT_EQ(2.5, score1.GetTotal()); |
| 359 | 359 |
| 360 // Subsequent events do not. | 360 // Subsequent events do not. |
| 361 score1.AddPoints(0.5); | 361 score1.AddPoints(0.5); |
| 362 EXPECT_EQ(3.0, score1.GetScore()); | 362 EXPECT_EQ(3.0, score1.GetTotal()); |
| 363 | 363 |
| 364 score2.AddPoints(1.0); | 364 score2.AddPoints(1.0); |
| 365 EXPECT_EQ(4.0, score2.GetScore()); | 365 EXPECT_EQ(4.0, score2.GetTotal()); |
| 366 score2.AddPoints(1.0); | 366 score2.AddPoints(1.0); |
| 367 EXPECT_EQ(5.0, score2.GetScore()); | 367 EXPECT_EQ(5.0, score2.GetTotal()); |
| 368 } | 368 } |
| 369 | 369 |
| 370 // Test that resetting a score has the correct properties. | 370 // Test that resetting a score has the correct properties. |
| 371 TEST_F(SiteEngagementScoreTest, Reset) { | 371 TEST_F(SiteEngagementScoreTest, Reset) { |
| 372 base::Time current_day = GetReferenceTime(); | 372 base::Time current_day = GetReferenceTime(); |
| 373 | 373 |
| 374 test_clock_.SetNow(current_day); | 374 test_clock_.SetNow(current_day); |
| 375 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); | 375 score_.AddPoints(SiteEngagementScore::GetNavigationPoints()); |
| 376 EXPECT_EQ(SiteEngagementScore::GetNavigationPoints(), score_.GetScore()); | 376 EXPECT_EQ(SiteEngagementScore::GetNavigationPoints(), score_.GetTotal()); |
| 377 | 377 |
| 378 current_day += base::TimeDelta::FromDays(7); | 378 current_day += base::TimeDelta::FromDays(7); |
| 379 test_clock_.SetNow(current_day); | 379 test_clock_.SetNow(current_day); |
| 380 | 380 |
| 381 score_.Reset(20.0, current_day); | 381 score_.Reset(20.0, current_day); |
| 382 EXPECT_DOUBLE_EQ(20.0, score_.GetScore()); | 382 EXPECT_DOUBLE_EQ(20.0, score_.GetTotal()); |
| 383 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); | 383 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); |
| 384 EXPECT_EQ(current_day, score_.last_engagement_time_); | 384 EXPECT_EQ(current_day, score_.last_engagement_time_); |
| 385 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null()); | 385 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null()); |
| 386 | 386 |
| 387 // Adding points after the reset should work as normal. | 387 // Adding points after the reset should work as normal. |
| 388 score_.AddPoints(5); | 388 score_.AddPoints(5); |
| 389 EXPECT_EQ(25.0, score_.GetScore()); | 389 EXPECT_EQ(25.0, score_.GetTotal()); |
| 390 | 390 |
| 391 // The decay should happen one decay period from the current time. | 391 // The decay should happen one decay period from the current time. |
| 392 test_clock_.SetNow(current_day + | 392 test_clock_.SetNow(current_day + |
| 393 base::TimeDelta::FromHours( | 393 base::TimeDelta::FromHours( |
| 394 SiteEngagementScore::GetDecayPeriodInHours() + 1)); | 394 SiteEngagementScore::GetDecayPeriodInHours() + 1)); |
| 395 EXPECT_EQ(25.0 - SiteEngagementScore::GetDecayPoints(), score_.GetScore()); | 395 EXPECT_EQ(25.0 - SiteEngagementScore::GetDecayPoints(), score_.GetTotal()); |
| 396 | 396 |
| 397 // Ensure that manually setting a time works as expected. | 397 // Ensure that manually setting a time works as expected. |
| 398 score_.AddPoints(5); | 398 score_.AddPoints(5); |
| 399 test_clock_.SetNow(GetReferenceTime()); | 399 test_clock_.SetNow(GetReferenceTime()); |
| 400 base::Time now = test_clock_.Now(); | 400 base::Time now = test_clock_.Now(); |
| 401 score_.Reset(10.0, now); | 401 score_.Reset(10.0, now); |
| 402 | 402 |
| 403 EXPECT_DOUBLE_EQ(10.0, score_.GetScore()); | 403 EXPECT_DOUBLE_EQ(10.0, score_.GetTotal()); |
| 404 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); | 404 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); |
| 405 EXPECT_EQ(now, score_.last_engagement_time_); | 405 EXPECT_EQ(now, score_.last_engagement_time_); |
| 406 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null()); | 406 EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null()); |
| 407 | 407 |
| 408 base::Time old_now = test_clock_.Now(); | 408 base::Time old_now = test_clock_.Now(); |
| 409 | 409 |
| 410 score_.set_last_shortcut_launch_time(test_clock_.Now()); | 410 score_.set_last_shortcut_launch_time(test_clock_.Now()); |
| 411 test_clock_.SetNow(GetReferenceTime() + base::TimeDelta::FromDays(3)); | 411 test_clock_.SetNow(GetReferenceTime() + base::TimeDelta::FromDays(3)); |
| 412 now = test_clock_.Now(); | 412 now = test_clock_.Now(); |
| 413 score_.Reset(15.0, now); | 413 score_.Reset(15.0, now); |
| 414 | 414 |
| 415 // 5 bonus from the last shortcut launch. | 415 // 5 bonus from the last shortcut launch. |
| 416 EXPECT_DOUBLE_EQ(20.0, score_.GetScore()); | 416 EXPECT_DOUBLE_EQ(20.0, score_.GetTotal()); |
| 417 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); | 417 EXPECT_DOUBLE_EQ(0, score_.points_added_today_); |
| 418 EXPECT_EQ(now, score_.last_engagement_time_); | 418 EXPECT_EQ(now, score_.last_engagement_time_); |
| 419 EXPECT_EQ(old_now, score_.last_shortcut_launch_time_); | 419 EXPECT_EQ(old_now, score_.last_shortcut_launch_time_); |
| 420 } | 420 } |
| 421 | 421 |
| 422 // Test proportional decay. | 422 // Test proportional decay. |
| 423 TEST_F(SiteEngagementScoreTest, ProportionalDecay) { | 423 TEST_F(SiteEngagementScoreTest, ProportionalDecay) { |
| 424 SetParamValue(SiteEngagementScore::DECAY_PROPORTION, 0.5); | 424 SetParamValue(SiteEngagementScore::DECAY_PROPORTION, 0.5); |
| 425 SetParamValue(SiteEngagementScore::DECAY_POINTS, 0); | 425 SetParamValue(SiteEngagementScore::DECAY_POINTS, 0); |
| 426 SetParamValue(SiteEngagementScore::MAX_POINTS_PER_DAY, 20); | 426 SetParamValue(SiteEngagementScore::MAX_POINTS_PER_DAY, 20); |
| 427 base::Time current_day = GetReferenceTime(); | 427 base::Time current_day = GetReferenceTime(); |
| 428 test_clock_.SetNow(current_day); | 428 test_clock_.SetNow(current_day); |
| 429 | 429 |
| 430 // Single decay period, expect the score to be halved once. | 430 // Single decay period, expect the score to be halved once. |
| 431 score_.AddPoints(2.0); | 431 score_.AddPoints(2.0); |
| 432 current_day += base::TimeDelta::FromDays(7); | 432 current_day += base::TimeDelta::FromDays(7); |
| 433 test_clock_.SetNow(current_day); | 433 test_clock_.SetNow(current_day); |
| 434 EXPECT_DOUBLE_EQ(1.0, score_.GetScore()); | 434 EXPECT_DOUBLE_EQ(1.0, score_.GetTotal()); |
| 435 | 435 |
| 436 // 3 decay periods, expect the score to be halved 3 times. | 436 // 3 decay periods, expect the score to be halved 3 times. |
| 437 score_.AddPoints(15.0); | 437 score_.AddPoints(15.0); |
| 438 current_day += base::TimeDelta::FromDays(21); | 438 current_day += base::TimeDelta::FromDays(21); |
| 439 test_clock_.SetNow(current_day); | 439 test_clock_.SetNow(current_day); |
| 440 EXPECT_DOUBLE_EQ(2.0, score_.GetScore()); | 440 EXPECT_DOUBLE_EQ(2.0, score_.GetTotal()); |
| 441 | 441 |
| 442 // Ensure point removal happens after proportional decay. | 442 // Ensure point removal happens after proportional decay. |
| 443 score_.AddPoints(4.0); | 443 score_.AddPoints(4.0); |
| 444 EXPECT_DOUBLE_EQ(6.0, score_.GetScore()); | 444 EXPECT_DOUBLE_EQ(6.0, score_.GetTotal()); |
| 445 SetParamValue(SiteEngagementScore::DECAY_POINTS, 2.0); | 445 SetParamValue(SiteEngagementScore::DECAY_POINTS, 2.0); |
| 446 current_day += base::TimeDelta::FromDays(7); | 446 current_day += base::TimeDelta::FromDays(7); |
| 447 test_clock_.SetNow(current_day); | 447 test_clock_.SetNow(current_day); |
| 448 EXPECT_NEAR(1.0, score_.GetScore(), kMaxRoundingDeviation); | 448 EXPECT_NEAR(1.0, score_.GetTotal(), kMaxRoundingDeviation); |
| 449 } | 449 } |
| OLD | NEW |