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

Unified Diff: chrome/browser/engagement/site_engagement_service_unittest.cc

Issue 1975723002: Reduce the site engagement service public interface. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Push messaging now uses engagement Created 4 years, 7 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/engagement/site_engagement_service_unittest.cc
diff --git a/chrome/browser/engagement/site_engagement_service_unittest.cc b/chrome/browser/engagement/site_engagement_service_unittest.cc
index f88fbf1e64bc690e3e71502341f1b8590b70b929..e43d6d63f38d409f1452d918097426f4ac572b93 100644
--- a/chrome/browser/engagement/site_engagement_service_unittest.cc
+++ b/chrome/browser/engagement/site_engagement_service_unittest.cc
@@ -16,7 +16,7 @@
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/engagement/site_engagement_helper.h"
#include "chrome/browser/engagement/site_engagement_metrics.h"
-#include "chrome/browser/engagement/site_engagement_service_factory.h"
+#include "chrome/browser/engagement/site_engagement_score.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_switches.h"
@@ -37,10 +37,7 @@ namespace {
base::FilePath g_temp_history_dir;
-const int kLessAccumulationsThanNeededToMaxDailyEngagement = 2;
const int kMoreAccumulationsThanNeededToMaxDailyEngagement = 40;
-const int kMoreAccumulationsThanNeededToMaxTotalEngagement = 200;
-const int kLessDaysThanNeededToMaxTotalEngagement = 4;
const int kMoreDaysThanNeededToMaxTotalEngagement = 40;
const int kLessPeriodsThanNeededToDecayMaxScore = 2;
const int kMorePeriodsThanNeededToDecayMaxScore = 40;
@@ -52,8 +49,8 @@ class SiteEngagementChangeWaiter : public content_settings::Observer {
HostContentSettingsMapFactory::GetForProfile(profile)->AddObserver(this);
}
~SiteEngagementChangeWaiter() override {
- HostContentSettingsMapFactory::GetForProfile(profile_)
- ->RemoveObserver(this);
+ HostContentSettingsMapFactory::GetForProfile(profile_)->RemoveObserver(
+ this);
}
// Overridden from content_settings::Observer:
@@ -100,378 +97,6 @@ std::unique_ptr<KeyedService> BuildTestHistoryService(
} // namespace
-class SiteEngagementScoreTest : public testing::Test {
- public:
- SiteEngagementScoreTest() : score_(&test_clock_) {}
-
- void SetUp() override {
- testing::Test::SetUp();
- // Disable the first engagement bonus for tests.
- SiteEngagementScore::SetParamValuesForTesting();
- }
-
- protected:
- void VerifyScore(const SiteEngagementScore& score,
- double expected_raw_score,
- double expected_points_added_today,
- base::Time expected_last_engagement_time) {
- EXPECT_EQ(expected_raw_score, score.raw_score_);
- EXPECT_EQ(expected_points_added_today, score.points_added_today_);
- EXPECT_EQ(expected_last_engagement_time, score.last_engagement_time_);
- }
-
- void UpdateScore(SiteEngagementScore* score,
- double raw_score,
- double points_added_today,
- base::Time last_engagement_time) {
- score->raw_score_ = raw_score;
- score->points_added_today_ = points_added_today;
- score->last_engagement_time_ = last_engagement_time;
- }
-
- void TestScoreInitializesAndUpdates(
- base::DictionaryValue* score_dict,
- double expected_raw_score,
- double expected_points_added_today,
- base::Time expected_last_engagement_time) {
- SiteEngagementScore initial_score(&test_clock_, *score_dict);
- VerifyScore(initial_score, expected_raw_score, expected_points_added_today,
- expected_last_engagement_time);
-
- // Updating the score dict should return false, as the score shouldn't
- // have changed at this point.
- EXPECT_FALSE(initial_score.UpdateScoreDict(score_dict));
-
- // Update the score to new values and verify it updates the score dict
- // correctly.
- base::Time different_day =
- GetReferenceTime() + base::TimeDelta::FromDays(1);
- UpdateScore(&initial_score, 5, 10, different_day);
- EXPECT_TRUE(initial_score.UpdateScoreDict(score_dict));
- SiteEngagementScore updated_score(&test_clock_, *score_dict);
- VerifyScore(updated_score, 5, 10, different_day);
- }
-
- void SetFirstDailyEngagementPointsForTesting(double points) {
- SiteEngagementScore::param_values
- [SiteEngagementScore::FIRST_DAILY_ENGAGEMENT] = points;
- }
-
- base::SimpleTestClock test_clock_;
- SiteEngagementScore score_;
-};
-
-// Accumulate score many times on the same day. Ensure each time the score goes
-// up, but not more than the maximum per day.
-TEST_F(SiteEngagementScoreTest, AccumulateOnSameDay) {
- base::Time reference_time = GetReferenceTime();
-
- test_clock_.SetNow(reference_time);
- for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) {
- score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
- EXPECT_EQ(std::min(SiteEngagementScore::GetMaxPointsPerDay(),
- (i + 1) * SiteEngagementScore::GetNavigationPoints()),
- score_.Score());
- }
-
- EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.Score());
-}
-
-// Accumulate on the first day to max that day's engagement, then accumulate on
-// a different day.
-TEST_F(SiteEngagementScoreTest, AccumulateOnTwoDays) {
- base::Time reference_time = GetReferenceTime();
- base::Time later_date = reference_time + base::TimeDelta::FromDays(2);
-
- test_clock_.SetNow(reference_time);
- for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i)
- score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
-
- EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.Score());
-
- test_clock_.SetNow(later_date);
- for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) {
- score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
- double day_score =
- std::min(SiteEngagementScore::GetMaxPointsPerDay(),
- (i + 1) * SiteEngagementScore::GetNavigationPoints());
- EXPECT_EQ(day_score + SiteEngagementScore::GetMaxPointsPerDay(),
- score_.Score());
- }
-
- EXPECT_EQ(2 * SiteEngagementScore::GetMaxPointsPerDay(), score_.Score());
-}
-
-// Accumulate score on many consecutive days and ensure the score doesn't exceed
-// the maximum allowed.
-TEST_F(SiteEngagementScoreTest, AccumulateALotOnManyDays) {
- base::Time current_day = GetReferenceTime();
-
- for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) {
- current_day += base::TimeDelta::FromDays(1);
- test_clock_.SetNow(current_day);
- for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j)
- score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
-
- EXPECT_EQ(std::min(SiteEngagementScore::kMaxPoints,
- (i + 1) * SiteEngagementScore::GetMaxPointsPerDay()),
- score_.Score());
- }
-
- EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.Score());
-}
-
-// Accumulate a little on many consecutive days and ensure the score doesn't
-// exceed the maximum allowed.
-TEST_F(SiteEngagementScoreTest, AccumulateALittleOnManyDays) {
- base::Time current_day = GetReferenceTime();
-
- for (int i = 0; i < kMoreAccumulationsThanNeededToMaxTotalEngagement; ++i) {
- current_day += base::TimeDelta::FromDays(1);
- test_clock_.SetNow(current_day);
-
- for (int j = 0; j < kLessAccumulationsThanNeededToMaxDailyEngagement; ++j)
- score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
-
- EXPECT_EQ(
- std::min(SiteEngagementScore::kMaxPoints,
- (i + 1) * kLessAccumulationsThanNeededToMaxDailyEngagement *
- SiteEngagementScore::GetNavigationPoints()),
- score_.Score());
- }
-
- EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.Score());
-}
-
-// Accumulate a bit, then check the score decays properly for a range of times.
-TEST_F(SiteEngagementScoreTest, ScoresDecayOverTime) {
- base::Time current_day = GetReferenceTime();
-
- // First max the score.
- for (int i = 0; i < kMoreDaysThanNeededToMaxTotalEngagement; ++i) {
- current_day += base::TimeDelta::FromDays(1);
- test_clock_.SetNow(current_day);
-
- for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j)
- score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
- }
-
- EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.Score());
-
- // The score should not have decayed before the first decay period has
- // elapsed.
- test_clock_.SetNow(current_day +
- base::TimeDelta::FromDays(
- SiteEngagementScore::GetDecayPeriodInDays() - 1));
- EXPECT_EQ(SiteEngagementScore::kMaxPoints, score_.Score());
-
- // The score should have decayed by one chunk after one decay period has
- // elapsed.
- test_clock_.SetNow(
- current_day +
- base::TimeDelta::FromDays(SiteEngagementScore::GetDecayPeriodInDays()));
- EXPECT_EQ(
- SiteEngagementScore::kMaxPoints - SiteEngagementScore::GetDecayPoints(),
- score_.Score());
-
- // The score should have decayed by the right number of chunks after a few
- // decay periods have elapsed.
- test_clock_.SetNow(
- current_day +
- base::TimeDelta::FromDays(kLessPeriodsThanNeededToDecayMaxScore *
- SiteEngagementScore::GetDecayPeriodInDays()));
- EXPECT_EQ(SiteEngagementScore::kMaxPoints -
- kLessPeriodsThanNeededToDecayMaxScore *
- SiteEngagementScore::GetDecayPoints(),
- score_.Score());
-
- // The score should not decay below zero.
- test_clock_.SetNow(
- current_day +
- base::TimeDelta::FromDays(kMorePeriodsThanNeededToDecayMaxScore *
- SiteEngagementScore::GetDecayPeriodInDays()));
- EXPECT_EQ(0, score_.Score());
-}
-
-// Test that any expected decays are applied before adding points.
-TEST_F(SiteEngagementScoreTest, DecaysAppliedBeforeAdd) {
- base::Time current_day = GetReferenceTime();
-
- // Get the score up to something that can handle a bit of decay before
- for (int i = 0; i < kLessDaysThanNeededToMaxTotalEngagement; ++i) {
- current_day += base::TimeDelta::FromDays(1);
- test_clock_.SetNow(current_day);
-
- for (int j = 0; j < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++j)
- score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
- }
-
- double initial_score = kLessDaysThanNeededToMaxTotalEngagement *
- SiteEngagementScore::GetMaxPointsPerDay();
- EXPECT_EQ(initial_score, score_.Score());
-
- // Go forward a few decay periods.
- test_clock_.SetNow(
- current_day +
- base::TimeDelta::FromDays(kLessPeriodsThanNeededToDecayMaxScore *
- SiteEngagementScore::GetDecayPeriodInDays()));
-
- double decayed_score = initial_score -
- kLessPeriodsThanNeededToDecayMaxScore *
- SiteEngagementScore::GetDecayPoints();
- EXPECT_EQ(decayed_score, score_.Score());
-
- // Now add some points.
- score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
- EXPECT_EQ(decayed_score + SiteEngagementScore::GetNavigationPoints(),
- score_.Score());
-}
-
-// Test that going back in time is handled properly.
-TEST_F(SiteEngagementScoreTest, GoBackInTime) {
- base::Time current_day = GetReferenceTime();
-
- test_clock_.SetNow(current_day);
- for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i)
- score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
-
- EXPECT_EQ(SiteEngagementScore::GetMaxPointsPerDay(), score_.Score());
-
- // Adding to the score on an earlier date should be treated like another day,
- // and should not cause any decay.
- test_clock_.SetNow(current_day - base::TimeDelta::FromDays(
- kMorePeriodsThanNeededToDecayMaxScore *
- SiteEngagementScore::GetDecayPoints()));
- for (int i = 0; i < kMoreAccumulationsThanNeededToMaxDailyEngagement; ++i) {
- score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
- double day_score =
- std::min(SiteEngagementScore::GetMaxPointsPerDay(),
- (i + 1) * SiteEngagementScore::GetNavigationPoints());
- EXPECT_EQ(day_score + SiteEngagementScore::GetMaxPointsPerDay(),
- score_.Score());
- }
-
- EXPECT_EQ(2 * SiteEngagementScore::GetMaxPointsPerDay(), score_.Score());
-}
-
-// Test that scores are read / written correctly from / to empty score
-// dictionaries.
-TEST_F(SiteEngagementScoreTest, EmptyDictionary) {
- base::DictionaryValue dict;
- TestScoreInitializesAndUpdates(&dict, 0, 0, base::Time());
-}
-
-// Test that scores are read / written correctly from / to partially empty
-// score dictionaries.
-TEST_F(SiteEngagementScoreTest, PartiallyEmptyDictionary) {
- base::DictionaryValue dict;
- dict.SetDouble(SiteEngagementScore::kPointsAddedTodayKey, 2);
-
- TestScoreInitializesAndUpdates(&dict, 0, 2, base::Time());
-}
-
-// Test that scores are read / written correctly from / to populated score
-// dictionaries.
-TEST_F(SiteEngagementScoreTest, PopulatedDictionary) {
- base::DictionaryValue dict;
- dict.SetDouble(SiteEngagementScore::kRawScoreKey, 1);
- dict.SetDouble(SiteEngagementScore::kPointsAddedTodayKey, 2);
- dict.SetDouble(SiteEngagementScore::kLastEngagementTimeKey,
- GetReferenceTime().ToInternalValue());
-
- TestScoreInitializesAndUpdates(&dict, 1, 2, GetReferenceTime());
-}
-
-// Ensure bonus engagement is awarded for the first engagement of a day.
-TEST_F(SiteEngagementScoreTest, FirstDailyEngagementBonus) {
- SetFirstDailyEngagementPointsForTesting(0.5);
-
- SiteEngagementScore score1(&test_clock_);
- SiteEngagementScore score2(&test_clock_);
- base::Time current_day = GetReferenceTime();
-
- test_clock_.SetNow(current_day);
-
- // The first engagement event gets the bonus.
- score1.AddPoints(0.5);
- EXPECT_EQ(1.0, score1.Score());
-
- // Subsequent events do not.
- score1.AddPoints(0.5);
- EXPECT_EQ(1.5, score1.Score());
-
- // Bonuses are awarded independently between scores.
- score2.AddPoints(1.0);
- EXPECT_EQ(1.5, score2.Score());
- score2.AddPoints(1.0);
- EXPECT_EQ(2.5, score2.Score());
-
- test_clock_.SetNow(current_day + base::TimeDelta::FromDays(1));
-
- // The first event for the next day gets the bonus.
- score1.AddPoints(0.5);
- EXPECT_EQ(2.5, score1.Score());
-
- // Subsequent events do not.
- score1.AddPoints(0.5);
- EXPECT_EQ(3.0, score1.Score());
-
- score2.AddPoints(1.0);
- EXPECT_EQ(4.0, score2.Score());
- score2.AddPoints(1.0);
- EXPECT_EQ(5.0, score2.Score());
-}
-
-// Test that resetting a score has the correct properties.
-TEST_F(SiteEngagementScoreTest, Reset) {
- base::Time current_day = GetReferenceTime();
-
- test_clock_.SetNow(current_day);
- score_.AddPoints(SiteEngagementScore::GetNavigationPoints());
- EXPECT_EQ(SiteEngagementScore::GetNavigationPoints(), score_.Score());
-
- current_day += base::TimeDelta::FromDays(7);
- test_clock_.SetNow(current_day);
-
- score_.Reset(20.0, nullptr);
- EXPECT_DOUBLE_EQ(20.0, score_.Score());
- EXPECT_DOUBLE_EQ(0, score_.points_added_today_);
- EXPECT_EQ(current_day, score_.last_engagement_time_);
- EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null());
-
- // Adding points after the reset should work as normal.
- score_.AddPoints(5);
- EXPECT_EQ(25.0, score_.Score());
-
- // The decay should happen one decay period from the current time.
- test_clock_.SetNow(current_day +
- base::TimeDelta::FromDays(
- SiteEngagementScore::GetDecayPeriodInDays() + 1));
- EXPECT_EQ(25.0 - SiteEngagementScore::GetDecayPoints(), score_.Score());
-
- // Ensure that manually setting a time works as expected.
- score_.AddPoints(5);
- test_clock_.SetNow(GetReferenceTime());
- base::Time now = test_clock_.Now();
- score_.Reset(10.0, &now);
-
- EXPECT_DOUBLE_EQ(10.0, score_.Score());
- EXPECT_DOUBLE_EQ(0, score_.points_added_today_);
- EXPECT_EQ(now, score_.last_engagement_time_);
- EXPECT_TRUE(score_.last_shortcut_launch_time_.is_null());
-
- score_.set_last_shortcut_launch_time(test_clock_.Now());
- test_clock_.SetNow(GetReferenceTime() + base::TimeDelta::FromDays(3));
- now = test_clock_.Now();
- score_.Reset(15.0, &now);
-
- // 5 bonus from the last shortcut launch.
- EXPECT_DOUBLE_EQ(20.0, score_.Score());
- EXPECT_DOUBLE_EQ(0, score_.points_added_today_);
- EXPECT_EQ(now, score_.last_engagement_time_);
- EXPECT_EQ(now, score_.last_shortcut_launch_time_);
-}
-
class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness {
public:
void SetUp() override {
@@ -514,8 +139,7 @@ class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness {
};
TEST_F(SiteEngagementServiceTest, GetMedianEngagement) {
- SiteEngagementService* service =
- SiteEngagementServiceFactory::GetForProfile(profile());
+ SiteEngagementService* service = SiteEngagementService::Get(profile());
ASSERT_TRUE(service);
GURL url1("http://www.google.com/");
@@ -581,12 +205,11 @@ TEST_F(SiteEngagementServiceTest, GetMedianEngagement) {
// by performing two navigations and checking the engagement score increases
// both times.
TEST_F(SiteEngagementServiceTest, ScoreIncrementsOnPageRequest) {
- SiteEngagementService* service =
- SiteEngagementServiceFactory::GetForProfile(profile());
+ SiteEngagementService* service = SiteEngagementService::Get(profile());
ASSERT_TRUE(service);
// Create the helper manually since it isn't present when a tab isn't created.
- SiteEngagementHelper::CreateForWebContents(web_contents());
+ SiteEngagementService::Helper::CreateForWebContents(web_contents());
GURL url("http://www.google.com/");
EXPECT_EQ(0, service->GetScore(url));
@@ -599,8 +222,7 @@ TEST_F(SiteEngagementServiceTest, ScoreIncrementsOnPageRequest) {
// Expect that site engagement scores for several sites are correctly
// aggregated during navigation events.
TEST_F(SiteEngagementServiceTest, GetTotalNavigationPoints) {
- SiteEngagementService* service =
- SiteEngagementServiceFactory::GetForProfile(profile());
+ SiteEngagementService* service = SiteEngagementService::Get(profile());
ASSERT_TRUE(service);
// The https and http versions of www.google.com should be separate.
@@ -636,8 +258,7 @@ TEST_F(SiteEngagementServiceTest, GetTotalNavigationPoints) {
}
TEST_F(SiteEngagementServiceTest, GetTotalUserInputPoints) {
- SiteEngagementService* service =
- SiteEngagementServiceFactory::GetForProfile(profile());
+ SiteEngagementService* service = SiteEngagementService::Get(profile());
ASSERT_TRUE(service);
// The https and http versions of www.google.com should be separate.
@@ -709,8 +330,8 @@ TEST_F(SiteEngagementServiceTest, LastShortcutLaunch) {
histograms.ExpectTotalCount(
SiteEngagementMetrics::kDaysSinceLastShortcutLaunchHistogram, 1);
- histograms.ExpectTotalCount(
- SiteEngagementMetrics::kEngagementTypeHistogram, 4);
+ histograms.ExpectTotalCount(SiteEngagementMetrics::kEngagementTypeHistogram,
+ 4);
histograms.ExpectBucketCount(
SiteEngagementMetrics::kEngagementTypeHistogram,
SiteEngagementMetrics::ENGAGEMENT_WEBAPP_SHORTCUT_LAUNCH, 2);
@@ -1083,12 +704,11 @@ TEST_F(SiteEngagementServiceTest, CleanupEngagementScores) {
TEST_F(SiteEngagementServiceTest, NavigationAccumulation) {
GURL url("https://www.google.com/");
- SiteEngagementService* service =
- SiteEngagementServiceFactory::GetForProfile(profile());
+ SiteEngagementService* service = SiteEngagementService::Get(profile());
ASSERT_TRUE(service);
// Create the helper manually since it isn't present when a tab isn't created.
- SiteEngagementHelper::CreateForWebContents(web_contents());
+ SiteEngagementService::Helper::CreateForWebContents(web_contents());
// Only direct navigation should trigger engagement.
NavigateWithTransitionAndExpectHigherScore(service, url,
« no previous file with comments | « chrome/browser/engagement/site_engagement_service.cc ('k') | chrome/browser/push_messaging/background_budget_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698