Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Side by Side Diff: chrome/browser/engagement/site_engagement_service_unittest.cc

Issue 2427343002: Make the site engagement service more robust to clock changes and time inconsistencies. (Closed)
Patch Set: Address nit Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/engagement/site_engagement_service.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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. Ensure that the original origin has its last engagement time
1520 // updated to now as a result.
1521 base::Time before_stale_period =
1522 clock->Now() - service->GetStalePeriod() - service->GetMaxDecayPeriod();
1523 clock->SetNow(before_stale_period);
1524
1525 GURL origin1("http://maps.google.com/");
1526 service->AddPoints(origin1, 1);
1527
1528 EXPECT_EQ(before_stale_period,
1529 service->CreateEngagementScore(origin).last_engagement_time());
1530 EXPECT_EQ(before_stale_period, service->GetLastEngagementTime());
1531 EXPECT_EQ(1, service->GetScore(origin));
1532 EXPECT_EQ(1, service->GetScore(origin1));
1533
1534 // Advance within a decay period and add points.
1535 base::TimeDelta less_than_decay_period =
1536 base::TimeDelta::FromHours(SiteEngagementScore::GetDecayPeriodInHours()) -
1537 base::TimeDelta::FromSeconds(30);
1538 base::Time origin1_last_updated = clock->Now() + less_than_decay_period;
1539 clock->SetNow(origin1_last_updated);
1540 service->AddPoints(origin, 1);
1541 service->AddPoints(origin1, 5);
1542 EXPECT_EQ(2, service->GetScore(origin));
1543 EXPECT_EQ(6, service->GetScore(origin1));
1544
1545 clock->SetNow(clock->Now() + less_than_decay_period);
1546 service->AddPoints(origin, 5);
1547 EXPECT_EQ(7, service->GetScore(origin));
1548
1549 // Move forward to the max number of decays per score. This is within the
1550 // stale period so no cleanup should be run.
1551 for (int i = 0; i < SiteEngagementScore::GetMaxDecaysPerScore(); ++i) {
1552 clock->SetNow(clock->Now() + less_than_decay_period);
1553 service->AddPoints(origin, 5);
1554 EXPECT_EQ(clock->Now(), service->GetLastEngagementTime());
1555 }
1556 EXPECT_EQ(12, service->GetScore(origin));
1557 EXPECT_EQ(clock->Now(), service->GetLastEngagementTime());
1558
1559 // Move the clock back to precisely 1 decay period after origin1's last
1560 // updated time. |last_engagement_time| is in the future, so AddPoints
1561 // triggers a cleanup. Ensure that |last_engagement_time| is moved back
1562 // appropriately, while origin1 is decayed correctly (once).
1563 clock->SetNow(origin1_last_updated + less_than_decay_period +
1564 base::TimeDelta::FromSeconds(30));
1565 service->AddPoints(origin1, 1);
1566
1567 EXPECT_EQ(clock->Now(),
1568 service->CreateEngagementScore(origin).last_engagement_time());
1569 EXPECT_EQ(clock->Now(), service->GetLastEngagementTime());
1570 EXPECT_EQ(12, service->GetScore(origin));
1571 EXPECT_EQ(1, service->GetScore(origin1));
1572 }
1573
1574 TEST_F(SiteEngagementServiceTest, CleanupMovesScoreBackToRebase) {
1575 base::SimpleTestClock* clock = new base::SimpleTestClock();
1576 std::unique_ptr<SiteEngagementService> service(
1577 new SiteEngagementService(profile(), base::WrapUnique(clock)));
1578 base::Time last_engagement_time;
1579
1580 base::Time current_day = GetReferenceTime();
1581 clock->SetNow(current_day);
1582
1583 GURL origin("http://www.google.com/");
1584 service->ResetScoreForURL(origin, 5);
1585 service->AddPoints(origin, 5);
1586 EXPECT_EQ(10, service->GetScore(origin));
1587 EXPECT_EQ(current_day, service->GetLastEngagementTime());
1588
1589 // Send the clock back in time before the stale period and add engagement for
1590 // a new origin.
1591 base::Time before_stale_period =
1592 clock->Now() - service->GetStalePeriod() - service->GetMaxDecayPeriod();
1593 clock->SetNow(before_stale_period);
1594
1595 GURL origin1("http://maps.google.com/");
1596 service->AddPoints(origin1, 1);
1597
1598 EXPECT_EQ(before_stale_period, service->GetLastEngagementTime());
1599
1600 // Set the clock such that |origin|'s last engagement time is between
1601 // last_engagement_time and rebase_time.
1602 clock->SetNow(current_day + service->GetStalePeriod() +
1603 service->GetMaxDecayPeriod() -
1604 base::TimeDelta::FromSeconds((30)));
1605 base::Time rebased_time = clock->Now() - service->GetMaxDecayPeriod();
1606 service->CleanupEngagementScores(true);
1607
1608 // Ensure that the original origin has its last engagement time updated to
1609 // rebase_time, and it has decayed when we access the score.
1610 EXPECT_EQ(rebased_time,
1611 service->CreateEngagementScore(origin).last_engagement_time());
1612 EXPECT_EQ(rebased_time, service->GetLastEngagementTime());
1613 EXPECT_EQ(5, service->GetScore(origin));
1614 EXPECT_EQ(0, service->GetScore(origin1));
1615 }
1616
1504 TEST_F(SiteEngagementServiceTest, IncognitoEngagementService) { 1617 TEST_F(SiteEngagementServiceTest, IncognitoEngagementService) {
1505 SiteEngagementService* service = SiteEngagementService::Get(profile()); 1618 SiteEngagementService* service = SiteEngagementService::Get(profile());
1506 ASSERT_TRUE(service); 1619 ASSERT_TRUE(service);
1507 1620
1508 GURL url1("http://www.google.com/"); 1621 GURL url1("http://www.google.com/");
1509 GURL url2("https://www.google.com/"); 1622 GURL url2("https://www.google.com/");
1510 GURL url3("https://drive.google.com/"); 1623 GURL url3("https://drive.google.com/");
1511 GURL url4("https://maps.google.com/"); 1624 GURL url4("https://maps.google.com/");
1512 1625
1513 service->AddPoints(url1, 1); 1626 service->AddPoints(url1, 1);
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
1591 1704
1592 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, 1705 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::IO,
1593 settings_map, url1)); 1706 settings_map, url1));
1594 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, 1707 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE,
1595 settings_map, url2)); 1708 settings_map, url2));
1596 EXPECT_EQ(4, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE, 1709 EXPECT_EQ(4, CheckScoreFromSettingsOnThread(content::BrowserThread::FILE,
1597 incognito_settings_map, url1)); 1710 incognito_settings_map, url1));
1598 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::IO, 1711 EXPECT_EQ(3, CheckScoreFromSettingsOnThread(content::BrowserThread::IO,
1599 incognito_settings_map, url2)); 1712 incognito_settings_map, url2));
1600 } 1713 }
OLDNEW
« no previous file with comments | « chrome/browser/engagement/site_engagement_service.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698