| 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 void UpdateScore(SiteEngagementScore* score, | 55 void UpdateScore(SiteEngagementScore* score, |
| 56 double raw_score, | 56 double raw_score, |
| 57 double points_added_today, | 57 double points_added_today, |
| 58 base::Time last_engagement_time) { | 58 base::Time last_engagement_time) { |
| 59 score->raw_score_ = raw_score; | 59 score->raw_score_ = raw_score; |
| 60 score->points_added_today_ = points_added_today; | 60 score->points_added_today_ = points_added_today; |
| 61 score->last_engagement_time_ = last_engagement_time; | 61 score->last_engagement_time_ = last_engagement_time; |
| 62 } | 62 } |
| 63 | 63 |
| 64 void TestScoreInitializesAndUpdates( | 64 void TestScoreInitializesAndUpdates( |
| 65 base::DictionaryValue* settings_dict, | 65 base::DictionaryValue* score_dict, |
| 66 double expected_raw_score, | 66 double expected_raw_score, |
| 67 double expected_points_added_today, | 67 double expected_points_added_today, |
| 68 base::Time expected_last_engagement_time) { | 68 base::Time expected_last_engagement_time) { |
| 69 SiteEngagementScore initial_score(&test_clock_, *settings_dict); | 69 SiteEngagementScore initial_score(&test_clock_, *score_dict); |
| 70 VerifyScore(initial_score, expected_raw_score, expected_points_added_today, | 70 VerifyScore(initial_score, expected_raw_score, expected_points_added_today, |
| 71 expected_last_engagement_time); | 71 expected_last_engagement_time); |
| 72 | 72 |
| 73 // Updating the settings dict should return false, as the score shouldn't | 73 // Updating the score dict should return false, as the score shouldn't |
| 74 // have changed at this point. | 74 // have changed at this point. |
| 75 EXPECT_FALSE(initial_score.UpdateSettings(settings_dict)); | 75 EXPECT_FALSE(initial_score.UpdateScoreDict(score_dict)); |
| 76 | 76 |
| 77 // Update the score to new values and verify it updates the settings dict | 77 // Update the score to new values and verify it updates the score dict |
| 78 // correctly. | 78 // correctly. |
| 79 base::Time different_day = | 79 base::Time different_day = |
| 80 GetReferenceTime() + base::TimeDelta::FromDays(1); | 80 GetReferenceTime() + base::TimeDelta::FromDays(1); |
| 81 UpdateScore(&initial_score, 5, 10, different_day); | 81 UpdateScore(&initial_score, 5, 10, different_day); |
| 82 EXPECT_TRUE(initial_score.UpdateSettings(settings_dict)); | 82 EXPECT_TRUE(initial_score.UpdateScoreDict(score_dict)); |
| 83 SiteEngagementScore updated_score(&test_clock_, *settings_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 // Navigate many times on the same day. Ensure each time the score goes up by | 91 // Navigate many times on the same day. Ensure each time the score goes up by |
| 92 // kNavigationPoints, but not more than kMaxPointsPerDay. | 92 // kNavigationPoints, but not more than kMaxPointsPerDay. |
| 93 TEST_F(SiteEngagementScoreTest, NavigateOnSameDay) { | 93 TEST_F(SiteEngagementScoreTest, NavigateOnSameDay) { |
| (...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 double day_score = | 273 double day_score = |
| 274 std::min(SiteEngagementScore::kMaxPointsPerDay, | 274 std::min(SiteEngagementScore::kMaxPointsPerDay, |
| 275 (i + 1) * SiteEngagementScore::kNavigationPoints); | 275 (i + 1) * SiteEngagementScore::kNavigationPoints); |
| 276 EXPECT_EQ(day_score + SiteEngagementScore::kMaxPointsPerDay, | 276 EXPECT_EQ(day_score + SiteEngagementScore::kMaxPointsPerDay, |
| 277 score_.Score()); | 277 score_.Score()); |
| 278 } | 278 } |
| 279 | 279 |
| 280 EXPECT_EQ(2 * SiteEngagementScore::kMaxPointsPerDay, score_.Score()); | 280 EXPECT_EQ(2 * SiteEngagementScore::kMaxPointsPerDay, score_.Score()); |
| 281 } | 281 } |
| 282 | 282 |
| 283 // Test that scores are read / written correctly from / to empty settings | 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 |
| 291 // settings dictionaries. | 291 // score dictionaries. |
| 292 TEST_F(SiteEngagementScoreTest, PartiallyEmptyDictionary) { | 292 TEST_F(SiteEngagementScoreTest, PartiallyEmptyDictionary) { |
| 293 base::DictionaryValue dict; | 293 base::DictionaryValue dict; |
| 294 dict.SetDouble(SiteEngagementScore::kPointsAddedTodayKey, 2); | 294 dict.SetDouble(SiteEngagementScore::kPointsAddedTodayKey, 2); |
| 295 | 295 |
| 296 TestScoreInitializesAndUpdates(&dict, 0, 2, base::Time()); | 296 TestScoreInitializesAndUpdates(&dict, 0, 2, base::Time()); |
| 297 } | 297 } |
| 298 | 298 |
| 299 // Test that scores are read / written correctly from / to populated settings | 299 // Test that scores are read / written correctly from / to populated score |
| 300 // dictionaries. | 300 // dictionaries. |
| 301 TEST_F(SiteEngagementScoreTest, PopulatedDictionary) { | 301 TEST_F(SiteEngagementScoreTest, PopulatedDictionary) { |
| 302 base::DictionaryValue dict; | 302 base::DictionaryValue dict; |
| 303 dict.SetDouble(SiteEngagementScore::kRawScoreKey, 1); | 303 dict.SetDouble(SiteEngagementScore::kRawScoreKey, 1); |
| 304 dict.SetDouble(SiteEngagementScore::kPointsAddedTodayKey, 2); | 304 dict.SetDouble(SiteEngagementScore::kPointsAddedTodayKey, 2); |
| 305 dict.SetDouble(SiteEngagementScore::kLastEngagementTimeKey, | 305 dict.SetDouble(SiteEngagementScore::kLastEngagementTimeKey, |
| 306 GetReferenceTime().ToInternalValue()); | 306 GetReferenceTime().ToInternalValue()); |
| 307 | 307 |
| 308 TestScoreInitializesAndUpdates(&dict, 1, 2, GetReferenceTime()); | 308 TestScoreInitializesAndUpdates(&dict, 1, 2, GetReferenceTime()); |
| 309 } | 309 } |
| 310 | 310 |
| 311 using SiteEngagementServiceTest = BrowserWithTestWindowTest; | 311 |
| 312 class SiteEngagementServiceTest : public BrowserWithTestWindowTest { |
| 313 public: |
| 314 SiteEngagementServiceTest() {} |
| 315 |
| 316 void SetUp() override { |
| 317 BrowserWithTestWindowTest::SetUp(); |
| 318 base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 319 switches::kEnableSiteEngagementService); |
| 320 } |
| 321 }; |
| 322 |
| 312 | 323 |
| 313 // Tests that the Site Engagement service is hooked up properly to navigations | 324 // Tests that the Site Engagement service is hooked up properly to navigations |
| 314 // by performing two navigations and checking the engagement score increases | 325 // by performing two navigations and checking the engagement score increases |
| 315 // both times. | 326 // both times. |
| 316 TEST_F(SiteEngagementServiceTest, ScoreIncrementsOnPageRequest) { | 327 TEST_F(SiteEngagementServiceTest, ScoreIncrementsOnPageRequest) { |
| 317 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 318 switches::kEnableSiteEngagementService); | |
| 319 | |
| 320 SiteEngagementService* service = | 328 SiteEngagementService* service = |
| 321 SiteEngagementServiceFactory::GetForProfile(profile()); | 329 SiteEngagementServiceFactory::GetForProfile(profile()); |
| 322 DCHECK(service); | 330 DCHECK(service); |
| 323 | 331 |
| 324 GURL url("http://www.google.com/"); | 332 GURL url("http://www.google.com/"); |
| 325 | 333 |
| 326 AddTab(browser(), GURL("about:blank")); | 334 AddTab(browser(), GURL("about:blank")); |
| 327 EXPECT_EQ(0, service->GetScore(url)); | 335 EXPECT_EQ(0, service->GetScore(url)); |
| 328 int prev_score = service->GetScore(url); | 336 int prev_score = service->GetScore(url); |
| 329 | 337 |
| 330 NavigateAndCommitActiveTab(url); | 338 NavigateAndCommitActiveTab(url); |
| 331 EXPECT_LT(prev_score, service->GetScore(url)); | 339 EXPECT_LT(prev_score, service->GetScore(url)); |
| 332 prev_score = service->GetScore(url); | 340 prev_score = service->GetScore(url); |
| 333 | 341 |
| 334 NavigateAndCommitActiveTab(url); | 342 NavigateAndCommitActiveTab(url); |
| 335 EXPECT_LT(prev_score, service->GetScore(url)); | 343 EXPECT_LT(prev_score, service->GetScore(url)); |
| 336 } | 344 } |
| 345 |
| 346 // Expect that site engagement scores for several sites are correctly aggregated |
| 347 // by GetTotalEngagementPoints(). |
| 348 TEST_F(SiteEngagementServiceTest, GetTotalEngagementPoints) { |
| 349 SiteEngagementService* service = |
| 350 SiteEngagementServiceFactory::GetForProfile(profile()); |
| 351 DCHECK(service); |
| 352 |
| 353 // The https and http versions of www.google.com should be separate. |
| 354 GURL url1("https://www.google.com/"); |
| 355 GURL url2("http://www.google.com/"); |
| 356 GURL url3("http://drive.google.com/"); |
| 357 |
| 358 EXPECT_EQ(0, service->GetScore(url1)); |
| 359 EXPECT_EQ(0, service->GetScore(url2)); |
| 360 EXPECT_EQ(0, service->GetScore(url3)); |
| 361 |
| 362 service->HandleNavigation(url1); |
| 363 EXPECT_EQ(1, service->GetScore(url1)); |
| 364 EXPECT_EQ(1, service->GetTotalEngagementPoints()); |
| 365 |
| 366 service->HandleNavigation(url2); |
| 367 service->HandleNavigation(url2); |
| 368 EXPECT_EQ(2, service->GetScore(url2)); |
| 369 EXPECT_EQ(3, service->GetTotalEngagementPoints()); |
| 370 |
| 371 service->HandleNavigation(url3); |
| 372 EXPECT_EQ(1, service->GetScore(url3)); |
| 373 EXPECT_EQ(4, service->GetTotalEngagementPoints()); |
| 374 |
| 375 service->HandleNavigation(url1); |
| 376 service->HandleNavigation(url1); |
| 377 EXPECT_EQ(3, service->GetScore(url1)); |
| 378 EXPECT_EQ(6, service->GetTotalEngagementPoints()); |
| 379 } |
| OLD | NEW |