Chromium Code Reviews| 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 "chrome/browser/engagement/site_engagement_service.h" | 5 #include "chrome/browser/engagement/site_engagement_service.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 1483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1494 // Add some more points and ensure the value is persisted. | 1494 // Add some more points and ensure the value is persisted. |
| 1495 service->AddPoints(origin, 3); | 1495 service->AddPoints(origin, 3); |
| 1496 | 1496 |
| 1497 last_engagement_time = base::Time::FromInternalValue( | 1497 last_engagement_time = base::Time::FromInternalValue( |
| 1498 profile()->GetPrefs()->GetInt64(prefs::kSiteEngagementLastUpdateTime)); | 1498 profile()->GetPrefs()->GetInt64(prefs::kSiteEngagementLastUpdateTime)); |
| 1499 | 1499 |
| 1500 EXPECT_EQ(later_in_day, last_engagement_time); | 1500 EXPECT_EQ(later_in_day, last_engagement_time); |
| 1501 EXPECT_EQ(later_in_day, service->GetLastEngagementTime()); | 1501 EXPECT_EQ(later_in_day, service->GetLastEngagementTime()); |
| 1502 } | 1502 } |
| 1503 | 1503 |
| 1504 TEST_F(SiteEngagementServiceTest, CleanupMovesScoreBackToNow) { | |
| 1505 base::SimpleTestClock* clock = new base::SimpleTestClock(); | |
| 1506 std::unique_ptr<SiteEngagementService> service( | |
| 1507 new SiteEngagementService(profile(), base::WrapUnique(clock))); | |
| 1508 base::Time last_engagement_time; | |
| 1509 | |
| 1510 base::Time current_day = GetReferenceTime(); | |
| 1511 clock->SetNow(current_day); | |
| 1512 | |
| 1513 GURL origin("http://www.google.com/"); | |
| 1514 service->AddPoints(origin, 1); | |
| 1515 EXPECT_EQ(1, service->GetScore(origin)); | |
| 1516 EXPECT_EQ(current_day, service->GetLastEngagementTime()); | |
| 1517 | |
| 1518 // Send the clock back in time before the stale period and add engagement for | |
| 1519 // a new origin. Go forward in time so that the original origin is between | |
| 1520 // rebase_time and now. Ensure that the original origin has its last | |
| 1521 // engagement time updated to now. | |
| 1522 base::Time before_stale_period = | |
| 1523 clock->Now() - service->GetStalePeriod() - service->GetMaxDecayPeriod(); | |
| 1524 clock->SetNow(before_stale_period); | |
|
benwells
2016/10/19 23:54:06
What would happen if CleanupEngagementScores(true)
| |
| 1525 | |
| 1526 GURL origin1("http://maps.google.com/"); | |
| 1527 service->AddPoints(origin1, 1); | |
| 1528 EXPECT_EQ(before_stale_period, service->GetLastEngagementTime()); | |
| 1529 | |
| 1530 // Set the clock in the future, just under the rebase limit from | |
| 1531 // |later_in_day|. | |
| 1532 clock->SetNow(current_day + service->GetMaxDecayPeriod() - | |
| 1533 base::TimeDelta::FromSeconds(30)); | |
| 1534 service->CleanupEngagementScores(true); | |
| 1535 EXPECT_EQ(clock->Now(), | |
| 1536 service->CreateEngagementScore(origin).last_engagement_time()); | |
| 1537 EXPECT_EQ(clock->Now(), service->GetLastEngagementTime()); | |
| 1538 EXPECT_EQ(1, service->GetScore(origin)); | |
| 1539 EXPECT_EQ(0, service->GetScore(origin1)); | |
| 1540 } | |
| 1541 | |
| 1542 TEST_F(SiteEngagementServiceTest, CleanupMovesScoreBackToRebase) { | |
| 1543 base::SimpleTestClock* clock = new base::SimpleTestClock(); | |
| 1544 std::unique_ptr<SiteEngagementService> service( | |
| 1545 new SiteEngagementService(profile(), base::WrapUnique(clock))); | |
| 1546 base::Time last_engagement_time; | |
| 1547 | |
| 1548 base::Time current_day = GetReferenceTime(); | |
| 1549 clock->SetNow(current_day); | |
| 1550 | |
| 1551 GURL origin("http://www.google.com/"); | |
| 1552 service->ResetScoreForURL(origin, 5); | |
| 1553 service->AddPoints(origin, 5); | |
| 1554 EXPECT_EQ(10, service->GetScore(origin)); | |
| 1555 EXPECT_EQ(current_day, service->GetLastEngagementTime()); | |
| 1556 | |
| 1557 // Send the clock back in time before the stale period and add engagement for | |
| 1558 // a new origin. Go forward in time so that the original origin is between | |
| 1559 // last_engagement_time and rebase_time. Ensure that the original origin has | |
| 1560 // its last engagement time updated to rebase_time, and it has decayed when we | |
| 1561 // access the score. | |
| 1562 base::Time before_stale_period = | |
| 1563 clock->Now() - service->GetStalePeriod() - service->GetMaxDecayPeriod(); | |
| 1564 clock->SetNow(before_stale_period); | |
| 1565 | |
| 1566 GURL origin1("http://maps.google.com/"); | |
| 1567 service->AddPoints(origin1, 1); | |
| 1568 | |
| 1569 EXPECT_EQ(before_stale_period, service->GetLastEngagementTime()); | |
| 1570 | |
| 1571 // Set the clock such that |origin|'s last engagement time is between | |
| 1572 // last_engagement_time and rebase_time. | |
| 1573 clock->SetNow(current_day + service->GetStalePeriod() + | |
| 1574 service->GetMaxDecayPeriod() - | |
| 1575 base::TimeDelta::FromSeconds((30))); | |
| 1576 base::Time rebased_time = clock->Now() - service->GetMaxDecayPeriod(); | |
| 1577 service->CleanupEngagementScores(true); | |
| 1578 EXPECT_EQ(rebased_time, | |
| 1579 service->CreateEngagementScore(origin).last_engagement_time()); | |
| 1580 EXPECT_EQ(rebased_time, service->GetLastEngagementTime()); | |
| 1581 EXPECT_EQ(5, service->GetScore(origin)); | |
| 1582 EXPECT_EQ(0, service->GetScore(origin1)); | |
| 1583 } | |
| 1584 | |
| 1504 TEST_F(SiteEngagementServiceTest, IncognitoEngagementService) { | 1585 TEST_F(SiteEngagementServiceTest, IncognitoEngagementService) { |
| 1505 SiteEngagementService* service = SiteEngagementService::Get(profile()); | 1586 SiteEngagementService* service = SiteEngagementService::Get(profile()); |
| 1506 ASSERT_TRUE(service); | 1587 ASSERT_TRUE(service); |
| 1507 | 1588 |
| 1508 GURL url1("http://www.google.com/"); | 1589 GURL url1("http://www.google.com/"); |
| 1509 GURL url2("https://www.google.com/"); | 1590 GURL url2("https://www.google.com/"); |
| 1510 GURL url3("https://drive.google.com/"); | 1591 GURL url3("https://drive.google.com/"); |
| 1511 GURL url4("https://maps.google.com/"); | 1592 GURL url4("https://maps.google.com/"); |
| 1512 | 1593 |
| 1513 service->AddPoints(url1, 1); | 1594 service->AddPoints(url1, 1); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1591 | 1672 |
| 1592 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, | 1673 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1593 settings_map, url1)); | 1674 settings_map, url1)); |
| 1594 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, | 1675 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1595 settings_map, url2)); | 1676 settings_map, url2)); |
| 1596 EXPECT_EQ(4, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, | 1677 EXPECT_EQ(4, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, |
| 1597 incognito_settings_map, url1)); | 1678 incognito_settings_map, url1)); |
| 1598 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, | 1679 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, |
| 1599 incognito_settings_map, url2)); | 1680 incognito_settings_map, url2)); |
| 1600 } | 1681 } |
| OLD | NEW |