| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 #include "base/test/simple_test_clock.h" | 6 #include "base/test/simple_test_clock.h" |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/browser/engagement/site_engagement_helper.h" | 8 #include "chrome/browser/engagement/site_engagement_helper.h" |
| 9 #include "chrome/browser/engagement/site_engagement_service.h" | 9 #include "chrome/browser/engagement/site_engagement_service.h" |
| 10 #include "chrome/browser/engagement/site_engagement_service_factory.h" | 10 #include "chrome/browser/engagement/site_engagement_service_factory.h" |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 EXPECT_TRUE(initial_score.UpdateScoreDict(score_dict)); | 82 EXPECT_TRUE(initial_score.UpdateScoreDict(score_dict)); |
| 83 SiteEngagementScore updated_score(&test_clock_, *score_dict); | 83 SiteEngagementScore updated_score(&test_clock_, *score_dict); |
| 84 VerifyScore(updated_score, 5, 10, different_day); | 84 VerifyScore(updated_score, 5, 10, different_day); |
| 85 } | 85 } |
| 86 | 86 |
| 87 base::SimpleTestClock test_clock_; | 87 base::SimpleTestClock test_clock_; |
| 88 SiteEngagementScore score_; | 88 SiteEngagementScore score_; |
| 89 }; | 89 }; |
| 90 | 90 |
| 91 // Accumulate score many times on the same day. Ensure each time the score goes | 91 // Accumulate score many times on the same day. Ensure each time the score goes |
| 92 // up by kNavigationPoints, but not more than kMaxPointsPerDay. | 92 // up by gNavigationPoints, but not more than gMaxPointsPerDay. |
| 93 TEST_F(SiteEngagementScoreTest, AccumulateOnSameDay) { | 93 TEST_F(SiteEngagementScoreTest, AccumulateOnSameDay) { |
| 94 base::Time reference_time = GetReferenceTime(); | 94 base::Time reference_time = GetReferenceTime(); |
| 95 | 95 |
| 96 test_clock_.SetNow(reference_time); | 96 test_clock_.SetNow(reference_time); |
| 97 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { | 97 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { |
| 98 score_.AddPoints(SiteEngagementScore::kNavigationPoints); | 98 score_.AddPoints(SiteEngagementScore::gNavigationPoints); |
| 99 EXPECT_EQ(std::min(SiteEngagementScore::kMaxPointsPerDay, | 99 EXPECT_EQ(std::min(SiteEngagementScore::gMaxPointsPerDay, |
| 100 (i + 1) * SiteEngagementScore::kNavigationPoints), | 100 (i + 1) * SiteEngagementScore::gNavigationPoints), |
| 101 score_.Score()); | 101 score_.Score()); |
| 102 } | 102 } |
| 103 | 103 |
| 104 EXPECT_EQ(SiteEngagementScore::kMaxPointsPerDay, score_.Score()); | 104 EXPECT_EQ(SiteEngagementScore::gMaxPointsPerDay, score_.Score()); |
| 105 } | 105 } |
| 106 | 106 |
| 107 // Accumulate on the first day to max that day's engagement, then accumulate on | 107 // Accumulate on the first day to max that day's engagement, then accumulate on |
| 108 // a different day. | 108 // a different day. |
| 109 TEST_F(SiteEngagementScoreTest, AccumulateOnTwoDays) { | 109 TEST_F(SiteEngagementScoreTest, AccumulateOnTwoDays) { |
| 110 base::Time reference_time = GetReferenceTime(); | 110 base::Time reference_time = GetReferenceTime(); |
| 111 base::Time later_date = reference_time + base::TimeDelta::FromDays(2); | 111 base::Time later_date = reference_time + base::TimeDelta::FromDays(2); |
| 112 | 112 |
| 113 test_clock_.SetNow(reference_time); | 113 test_clock_.SetNow(reference_time); |
| 114 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) | 114 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) |
| 115 score_.AddPoints(SiteEngagementScore::kNavigationPoints); | 115 score_.AddPoints(SiteEngagementScore::gNavigationPoints); |
| 116 | 116 |
| 117 EXPECT_EQ(SiteEngagementScore::kMaxPointsPerDay, score_.Score()); | 117 EXPECT_EQ(SiteEngagementScore::gMaxPointsPerDay, score_.Score()); |
| 118 | 118 |
| 119 test_clock_.SetNow(later_date); | 119 test_clock_.SetNow(later_date); |
| 120 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { | 120 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { |
| 121 score_.AddPoints(SiteEngagementScore::kNavigationPoints); | 121 score_.AddPoints(SiteEngagementScore::gNavigationPoints); |
| 122 double day_score = | 122 double day_score = |
| 123 std::min(SiteEngagementScore::kMaxPointsPerDay, | 123 std::min(SiteEngagementScore::gMaxPointsPerDay, |
| 124 (i + 1) * SiteEngagementScore::kNavigationPoints); | 124 (i + 1) * SiteEngagementScore::gNavigationPoints); |
| 125 EXPECT_EQ(day_score + SiteEngagementScore::kMaxPointsPerDay, | 125 EXPECT_EQ(day_score + SiteEngagementScore::gMaxPointsPerDay, |
| 126 score_.Score()); | 126 score_.Score()); |
| 127 } | 127 } |
| 128 | 128 |
| 129 EXPECT_EQ(2 * SiteEngagementScore::kMaxPointsPerDay, score_.Score()); | 129 EXPECT_EQ(2 * SiteEngagementScore::gMaxPointsPerDay, score_.Score()); |
| 130 } | 130 } |
| 131 | 131 |
| 132 // Accumulate score on many consecutive days and ensure the score doesn't exceed | 132 // Accumulate score on many consecutive days and ensure the score doesn't exceed |
| 133 // the maximum allowed. | 133 // the maximum allowed. |
| 134 TEST_F(SiteEngagementScoreTest, AccumulateALotOnManyDays) { | 134 TEST_F(SiteEngagementScoreTest, AccumulateALotOnManyDays) { |
| 135 base::Time current_day = GetReferenceTime(); | 135 base::Time current_day = GetReferenceTime(); |
| 136 | 136 |
| 137 for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) { | 137 for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) { |
| 138 current_day += base::TimeDelta::FromDays(1); | 138 current_day += base::TimeDelta::FromDays(1); |
| 139 test_clock_.SetNow(current_day); | 139 test_clock_.SetNow(current_day); |
| 140 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) | 140 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) |
| 141 score_.AddPoints(SiteEngagementScore::kNavigationPoints); | 141 score_.AddPoints(SiteEngagementScore::gNavigationPoints); |
| 142 | 142 |
| 143 EXPECT_EQ(std::min(SiteEngagementScore::kMaxPoints, | 143 EXPECT_EQ(std::min(SiteEngagementScore::kMaxPoints, |
| 144 (i + 1) * SiteEngagementScore::kMaxPointsPerDay), | 144 (i + 1) * SiteEngagementScore::gMaxPointsPerDay), |
| 145 score_.Score()); | 145 score_.Score()); |
| 146 } | 146 } |
| 147 | 147 |
| 148 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.Score()); | 148 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.Score()); |
| 149 } | 149 } |
| 150 | 150 |
| 151 // Accumulate a little on many consecutive days and ensure the score doesn't | 151 // Accumulate a little on many consecutive days and ensure the score doesn't |
| 152 // exceed the maximum allowed. | 152 // exceed the maximum allowed. |
| 153 TEST_F(SiteEngagementScoreTest, AccumulateALittleOnManyDays) { | 153 TEST_F(SiteEngagementScoreTest, AccumulateALittleOnManyDays) { |
| 154 base::Time current_day = GetReferenceTime(); | 154 base::Time current_day = GetReferenceTime(); |
| 155 | 155 |
| 156 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxTotalEngagement; ++i) { | 156 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxTotalEngagement; ++i) { |
| 157 current_day += base::TimeDelta::FromDays(1); | 157 current_day += base::TimeDelta::FromDays(1); |
| 158 test_clock_.SetNow(current_day); | 158 test_clock_.SetNow(current_day); |
| 159 | 159 |
| 160 for (int j = 0; j < kLessAccumulationsThanNeededToMaxDailyEngagement; ++j) | 160 for (int j = 0; j < kLessAccumulationsThanNeededToMaxDailyEngagement; ++j) |
| 161 score_.AddPoints(SiteEngagementScore::kNavigationPoints); | 161 score_.AddPoints(SiteEngagementScore::gNavigationPoints); |
| 162 | 162 |
| 163 EXPECT_EQ( | 163 EXPECT_EQ( |
| 164 std::min(SiteEngagementScore::kMaxPoints, | 164 std::min(SiteEngagementScore::kMaxPoints, |
| 165 (i + 1) * kLessAccumulationsThanNeededToMaxDailyEngagement * | 165 (i + 1) * kLessAccumulationsThanNeededToMaxDailyEngagement * |
| 166 SiteEngagementScore::kNavigationPoints), | 166 SiteEngagementScore::gNavigationPoints), |
| 167 score_.Score()); | 167 score_.Score()); |
| 168 } | 168 } |
| 169 | 169 |
| 170 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.Score()); | 170 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.Score()); |
| 171 } | 171 } |
| 172 | 172 |
| 173 // Accumulate a bit, then check the score decays properly for a range of times. | 173 // Accumulate a bit, then check the score decays properly for a range of times. |
| 174 TEST_F(SiteEngagementScoreTest, ScoresDecayOverTime) { | 174 TEST_F(SiteEngagementScoreTest, ScoresDecayOverTime) { |
| 175 base::Time current_day = GetReferenceTime(); | 175 base::Time current_day = GetReferenceTime(); |
| 176 | 176 |
| 177 // First max the score. | 177 // First max the score. |
| 178 for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) { | 178 for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) { |
| 179 current_day += base::TimeDelta::FromDays(1); | 179 current_day += base::TimeDelta::FromDays(1); |
| 180 test_clock_.SetNow(current_day); | 180 test_clock_.SetNow(current_day); |
| 181 | 181 |
| 182 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) | 182 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) |
| 183 score_.AddPoints(SiteEngagementScore::kNavigationPoints); | 183 score_.AddPoints(SiteEngagementScore::gNavigationPoints); |
| 184 } | 184 } |
| 185 | 185 |
| 186 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.Score()); | 186 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.Score()); |
| 187 | 187 |
| 188 // The score should not have decayed before the first decay period has | 188 // The score should not have decayed before the first decay period has |
| 189 // elapsed. | 189 // elapsed. |
| 190 test_clock_.SetNow( | 190 test_clock_.SetNow( |
| 191 current_day + | 191 current_day + |
| 192 base::TimeDelta::FromDays(SiteEngagementScore::kDecayPeriodInDays - 1)); | 192 base::TimeDelta::FromDays(SiteEngagementScore::gDecayPeriodInDays - 1)); |
| 193 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.Score()); | 193 EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.Score()); |
| 194 | 194 |
| 195 // The score should have decayed by one chunk after one decay period has | 195 // The score should have decayed by one chunk after one decay period has |
| 196 // elapsed. | 196 // elapsed. |
| 197 test_clock_.SetNow( | 197 test_clock_.SetNow( |
| 198 current_day + | 198 current_day + |
| 199 base::TimeDelta::FromDays(SiteEngagementScore::kDecayPeriodInDays)); | 199 base::TimeDelta::FromDays(SiteEngagementScore::gDecayPeriodInDays)); |
| 200 EXPECT_EQ(SiteEngagementScore::kMaxPoints - SiteEngagementScore::kDecayPoints, | 200 EXPECT_EQ(SiteEngagementScore::kMaxPoints - SiteEngagementScore::gDecayPoints, |
| 201 score_.Score()); | 201 score_.Score()); |
| 202 | 202 |
| 203 // The score should have decayed by the right number of chunks after a few | 203 // The score should have decayed by the right number of chunks after a few |
| 204 // decay periods have elapsed. | 204 // decay periods have elapsed. |
| 205 test_clock_.SetNow( | 205 test_clock_.SetNow( |
| 206 current_day + | 206 current_day + |
| 207 base::TimeDelta::FromDays(kLessPeriodsThanNeededToDecayMaxScore * | 207 base::TimeDelta::FromDays(kLessPeriodsThanNeededToDecayMaxScore * |
| 208 SiteEngagementScore::kDecayPeriodInDays)); | 208 SiteEngagementScore::gDecayPeriodInDays)); |
| 209 EXPECT_EQ(SiteEngagementScore::kMaxPoints - | 209 EXPECT_EQ(SiteEngagementScore::kMaxPoints - |
| 210 kLessPeriodsThanNeededToDecayMaxScore * | 210 kLessPeriodsThanNeededToDecayMaxScore * |
| 211 SiteEngagementScore::kDecayPoints, | 211 SiteEngagementScore::gDecayPoints, |
| 212 score_.Score()); | 212 score_.Score()); |
| 213 | 213 |
| 214 // The score should not decay below zero. | 214 // The score should not decay below zero. |
| 215 test_clock_.SetNow( | 215 test_clock_.SetNow( |
| 216 current_day + | 216 current_day + |
| 217 base::TimeDelta::FromDays(kMorePeriodsThanNeededToDecayMaxScore * | 217 base::TimeDelta::FromDays(kMorePeriodsThanNeededToDecayMaxScore * |
| 218 SiteEngagementScore::kDecayPeriodInDays)); | 218 SiteEngagementScore::gDecayPeriodInDays)); |
| 219 EXPECT_EQ(0, score_.Score()); | 219 EXPECT_EQ(0, score_.Score()); |
| 220 } | 220 } |
| 221 | 221 |
| 222 // Test that any expected decays are applied before adding points. | 222 // Test that any expected decays are applied before adding points. |
| 223 TEST_F(SiteEngagementScoreTest, DecaysAppliedBeforeAdd) { | 223 TEST_F(SiteEngagementScoreTest, DecaysAppliedBeforeAdd) { |
| 224 base::Time current_day = GetReferenceTime(); | 224 base::Time current_day = GetReferenceTime(); |
| 225 | 225 |
| 226 // Get the score up to something that can handle a bit of decay before | 226 // Get the score up to something that can handle a bit of decay before |
| 227 for (int i = 0; i < kLessDaysThanNeededToMaxTotalEngagement; ++i) { | 227 for (int i = 0; i < kLessDaysThanNeededToMaxTotalEngagement; ++i) { |
| 228 current_day += base::TimeDelta::FromDays(1); | 228 current_day += base::TimeDelta::FromDays(1); |
| 229 test_clock_.SetNow(current_day); | 229 test_clock_.SetNow(current_day); |
| 230 | 230 |
| 231 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) | 231 for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j) |
| 232 score_.AddPoints(SiteEngagementScore::kNavigationPoints); | 232 score_.AddPoints(SiteEngagementScore::gNavigationPoints); |
| 233 } | 233 } |
| 234 | 234 |
| 235 double initial_score = kLessDaysThanNeededToMaxTotalEngagement * | 235 double initial_score = kLessDaysThanNeededToMaxTotalEngagement * |
| 236 SiteEngagementScore::kMaxPointsPerDay; | 236 SiteEngagementScore::gMaxPointsPerDay; |
| 237 EXPECT_EQ(initial_score, score_.Score()); | 237 EXPECT_EQ(initial_score, score_.Score()); |
| 238 | 238 |
| 239 // Go forward a few decay periods. | 239 // Go forward a few decay periods. |
| 240 test_clock_.SetNow( | 240 test_clock_.SetNow( |
| 241 current_day + | 241 current_day + |
| 242 base::TimeDelta::FromDays(kLessPeriodsThanNeededToDecayMaxScore * | 242 base::TimeDelta::FromDays(kLessPeriodsThanNeededToDecayMaxScore * |
| 243 SiteEngagementScore::kDecayPeriodInDays)); | 243 SiteEngagementScore::gDecayPeriodInDays)); |
| 244 | 244 |
| 245 double decayed_score = | 245 double decayed_score = |
| 246 initial_score - | 246 initial_score - |
| 247 kLessPeriodsThanNeededToDecayMaxScore * SiteEngagementScore::kDecayPoints; | 247 kLessPeriodsThanNeededToDecayMaxScore * SiteEngagementScore::gDecayPoints; |
| 248 EXPECT_EQ(decayed_score, score_.Score()); | 248 EXPECT_EQ(decayed_score, score_.Score()); |
| 249 | 249 |
| 250 // Now add some points. | 250 // Now add some points. |
| 251 score_.AddPoints(SiteEngagementScore::kNavigationPoints); | 251 score_.AddPoints(SiteEngagementScore::gNavigationPoints); |
| 252 EXPECT_EQ(decayed_score + SiteEngagementScore::kNavigationPoints, | 252 EXPECT_EQ(decayed_score + SiteEngagementScore::gNavigationPoints, |
| 253 score_.Score()); | 253 score_.Score()); |
| 254 } | 254 } |
| 255 | 255 |
| 256 // Test that going back in time is handled properly. | 256 // Test that going back in time is handled properly. |
| 257 TEST_F(SiteEngagementScoreTest, GoBackInTime) { | 257 TEST_F(SiteEngagementScoreTest, GoBackInTime) { |
| 258 base::Time current_day = GetReferenceTime(); | 258 base::Time current_day = GetReferenceTime(); |
| 259 | 259 |
| 260 test_clock_.SetNow(current_day); | 260 test_clock_.SetNow(current_day); |
| 261 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) | 261 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) |
| 262 score_.AddPoints(SiteEngagementScore::kNavigationPoints); | 262 score_.AddPoints(SiteEngagementScore::gNavigationPoints); |
| 263 | 263 |
| 264 EXPECT_EQ(SiteEngagementScore::kMaxPointsPerDay, score_.Score()); | 264 EXPECT_EQ(SiteEngagementScore::gMaxPointsPerDay, score_.Score()); |
| 265 | 265 |
| 266 // Adding to the score on an earlier date should be treated like another day, | 266 // Adding to the score on an earlier date should be treated like another day, |
| 267 // and should not cause any decay. | 267 // and should not cause any decay. |
| 268 test_clock_.SetNow(current_day - base::TimeDelta::FromDays( | 268 test_clock_.SetNow(current_day - base::TimeDelta::FromDays( |
| 269 kMorePeriodsThanNeededToDecayMaxScore * | 269 kMorePeriodsThanNeededToDecayMaxScore * |
| 270 SiteEngagementScore::kDecayPoints)); | 270 SiteEngagementScore::gDecayPoints)); |
| 271 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { | 271 for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) { |
| 272 score_.AddPoints(SiteEngagementScore::kNavigationPoints); | 272 score_.AddPoints(SiteEngagementScore::gNavigationPoints); |
| 273 double day_score = | 273 double day_score = |
| 274 std::min(SiteEngagementScore::kMaxPointsPerDay, | 274 std::min(SiteEngagementScore::gMaxPointsPerDay, |
| 275 (i + 1) * SiteEngagementScore::kNavigationPoints); | 275 (i + 1) * SiteEngagementScore::gNavigationPoints); |
| 276 EXPECT_EQ(day_score + SiteEngagementScore::kMaxPointsPerDay, | 276 EXPECT_EQ(day_score + SiteEngagementScore::gMaxPointsPerDay, |
| 277 score_.Score()); | 277 score_.Score()); |
| 278 } | 278 } |
| 279 | 279 |
| 280 EXPECT_EQ(2 * SiteEngagementScore::kMaxPointsPerDay, score_.Score()); | 280 EXPECT_EQ(2 * SiteEngagementScore::gMaxPointsPerDay, score_.Score()); |
| 281 } | 281 } |
| 282 | 282 |
| 283 // Test that scores are read / written correctly from / to empty score | 283 // Test that scores are read / written correctly from / to empty score |
| 284 // dictionaries. | 284 // dictionaries. |
| 285 TEST_F(SiteEngagementScoreTest, EmptyDictionary) { | 285 TEST_F(SiteEngagementScoreTest, EmptyDictionary) { |
| 286 base::DictionaryValue dict; | 286 base::DictionaryValue dict; |
| 287 TestScoreInitializesAndUpdates(&dict, 0, 0, base::Time()); | 287 TestScoreInitializesAndUpdates(&dict, 0, 0, base::Time()); |
| 288 } | 288 } |
| 289 | 289 |
| 290 // Test that scores are read / written correctly from / to partially empty | 290 // Test that scores are read / written correctly from / to partially empty |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 | 402 |
| 403 service->HandleUserInput(url3); | 403 service->HandleUserInput(url3); |
| 404 EXPECT_DOUBLE_EQ(0.05, service->GetScore(url3)); | 404 EXPECT_DOUBLE_EQ(0.05, service->GetScore(url3)); |
| 405 EXPECT_DOUBLE_EQ(0.2, service->GetTotalEngagementPoints()); | 405 EXPECT_DOUBLE_EQ(0.2, service->GetTotalEngagementPoints()); |
| 406 | 406 |
| 407 service->HandleUserInput(url1); | 407 service->HandleUserInput(url1); |
| 408 service->HandleUserInput(url1); | 408 service->HandleUserInput(url1); |
| 409 EXPECT_DOUBLE_EQ(0.15, service->GetScore(url1)); | 409 EXPECT_DOUBLE_EQ(0.15, service->GetScore(url1)); |
| 410 EXPECT_DOUBLE_EQ(0.3, service->GetTotalEngagementPoints()); | 410 EXPECT_DOUBLE_EQ(0.3, service->GetTotalEngagementPoints()); |
| 411 } | 411 } |
| OLD | NEW |