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

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

Issue 1986033002: Implement an observer interface for the site engagement service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@site-engagement-refactor
Patch Set: Rebase to fix leak Created 4 years, 6 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
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 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 std::unique_ptr<KeyedService> BuildTestHistoryService( 90 std::unique_ptr<KeyedService> BuildTestHistoryService(
91 content::BrowserContext* context) { 91 content::BrowserContext* context) {
92 std::unique_ptr<history::HistoryService> service( 92 std::unique_ptr<history::HistoryService> service(
93 new history::HistoryService()); 93 new history::HistoryService());
94 service->Init(history::TestHistoryDatabaseParamsForPath(g_temp_history_dir)); 94 service->Init(history::TestHistoryDatabaseParamsForPath(g_temp_history_dir));
95 return std::move(service); 95 return std::move(service);
96 } 96 }
97 97
98 } // namespace 98 } // namespace
99 99
100 class ObserverTester : public SiteEngagementObserver {
101 public:
102 ObserverTester(SiteEngagementService* service,
103 content::WebContents* web_contents,
104 const GURL& url,
105 double score,
106 bool is_hidden)
107 : SiteEngagementObserver(service),
108 web_contents_(web_contents),
109 url_(url),
110 score_(score),
111 is_hidden_(is_hidden),
112 callback_called_(false),
113 run_loop_() {}
114
115 void OnEngagementIncreased(content::WebContents* web_contents,
116 const GURL& url,
117 double score,
118 bool is_hidden) override {
119 EXPECT_EQ(web_contents_, web_contents);
120 EXPECT_EQ(url_, url);
121 EXPECT_DOUBLE_EQ(score_, score);
122 EXPECT_EQ(is_hidden_, is_hidden);
123 set_callback_called(true);
124 run_loop_.Quit();
125 }
126
127 void Wait() { run_loop_.Run(); }
128
129 bool callback_called() { return callback_called_; }
130 void set_callback_called(bool callback_called) {
131 callback_called_ = callback_called;
132 }
133
134 private:
135 content::WebContents* web_contents_;
136 GURL url_;
137 double score_;
138 bool is_hidden_;
139 bool callback_called_;
140 base::RunLoop run_loop_;
141
142 DISALLOW_COPY_AND_ASSIGN(ObserverTester);
143 };
144
100 class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness { 145 class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness {
101 public: 146 public:
102 void SetUp() override { 147 void SetUp() override {
103 ChromeRenderViewHostTestHarness::SetUp(); 148 ChromeRenderViewHostTestHarness::SetUp();
104 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 149 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
105 g_temp_history_dir = temp_dir_.path(); 150 g_temp_history_dir = temp_dir_.path();
106 HistoryServiceFactory::GetInstance()->SetTestingFactory( 151 HistoryServiceFactory::GetInstance()->SetTestingFactory(
107 profile(), &BuildTestHistoryService); 152 profile(), &BuildTestHistoryService);
108 SiteEngagementScore::SetParamValuesForTesting(); 153 SiteEngagementScore::SetParamValuesForTesting();
109 } 154 }
(...skipping 899 matching lines...) Expand 10 before | Expand all | Expand 10 after
1009 EXPECT_TRUE(service->IsEngagementAtLeast( 1054 EXPECT_TRUE(service->IsEngagementAtLeast(
1010 url2, SiteEngagementService::ENGAGEMENT_LEVEL_LOW)); 1055 url2, SiteEngagementService::ENGAGEMENT_LEVEL_LOW));
1011 EXPECT_TRUE(service->IsEngagementAtLeast( 1056 EXPECT_TRUE(service->IsEngagementAtLeast(
1012 url2, SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM)); 1057 url2, SiteEngagementService::ENGAGEMENT_LEVEL_MEDIUM));
1013 EXPECT_TRUE(service->IsEngagementAtLeast( 1058 EXPECT_TRUE(service->IsEngagementAtLeast(
1014 url2, SiteEngagementService::ENGAGEMENT_LEVEL_HIGH)); 1059 url2, SiteEngagementService::ENGAGEMENT_LEVEL_HIGH));
1015 EXPECT_TRUE(service->IsEngagementAtLeast( 1060 EXPECT_TRUE(service->IsEngagementAtLeast(
1016 url2, SiteEngagementService::ENGAGEMENT_LEVEL_MAX)); 1061 url2, SiteEngagementService::ENGAGEMENT_LEVEL_MAX));
1017 } 1062 }
1018 1063
1064 TEST_F(SiteEngagementServiceTest, Observers) {
1065 SiteEngagementService* service = SiteEngagementService::Get(profile());
1066
1067 GURL url_score_1("http://www.google.com/maps");
1068 GURL url_score_2("http://www.google.com/drive");
1069 GURL url_score_3("http://www.google.com/");
1070 GURL url_not_called("https://www.google.com/");
1071
1072 // Create an observer and Observe(nullptr).
1073 ObserverTester tester_not_called(service, web_contents(), url_not_called, 1,
1074 true);
1075 tester_not_called.Observe(nullptr);
1076
1077 {
1078 // Create an observer for navigation.
1079 ObserverTester tester(service, web_contents(), url_score_1, 0.5, false);
1080 NavigateAndCommit(url_score_1);
1081 service->HandleNavigation(web_contents(), ui::PAGE_TRANSITION_TYPED);
1082 tester.Wait();
1083 EXPECT_TRUE(tester.callback_called());
1084 EXPECT_FALSE(tester_not_called.callback_called());
1085 tester.Observe(nullptr);
1086 }
1087
1088 {
1089 // Update observer for a user input.
1090 ObserverTester tester(service, web_contents(), url_score_2, 0.55, false);
1091 NavigateAndCommit(url_score_2);
1092 service->HandleUserInput(web_contents(),
1093 SiteEngagementMetrics::ENGAGEMENT_MOUSE);
1094 tester.Wait();
1095 EXPECT_TRUE(tester.callback_called());
1096 EXPECT_FALSE(tester_not_called.callback_called());
1097 tester.Observe(nullptr);
1098 }
1099
1100 // Add two observers for media playing in the foreground.
1101 {
1102 ObserverTester tester_1(service, web_contents(), url_score_3, 0.57, false);
1103 ObserverTester tester_2(service, web_contents(), url_score_3, 0.57, false);
1104 NavigateAndCommit(url_score_3);
1105 service->HandleMediaPlaying(web_contents(), false);
1106 tester_1.Wait();
1107 tester_2.Wait();
1108
1109 EXPECT_TRUE(tester_1.callback_called());
1110 EXPECT_TRUE(tester_2.callback_called());
1111 EXPECT_FALSE(tester_not_called.callback_called());
1112 tester_1.Observe(nullptr);
1113 tester_2.Observe(nullptr);
1114 }
1115
1116 // Add an observer for media playing in the background.
1117 {
1118 ObserverTester tester(service, web_contents(), url_score_3, 0.58, true);
1119 service->HandleMediaPlaying(web_contents(), true);
1120 tester.Wait();
1121
1122 EXPECT_TRUE(tester.callback_called());
1123 EXPECT_FALSE(tester_not_called.callback_called());
1124 tester.Observe(nullptr);
1125 }
1126 }
1127
1019 TEST_F(SiteEngagementServiceTest, ScoreDecayHistograms) { 1128 TEST_F(SiteEngagementServiceTest, ScoreDecayHistograms) {
1020 base::SimpleTestClock* clock = new base::SimpleTestClock(); 1129 base::SimpleTestClock* clock = new base::SimpleTestClock();
1021 std::unique_ptr<SiteEngagementService> service( 1130 std::unique_ptr<SiteEngagementService> service(
1022 new SiteEngagementService(profile(), base::WrapUnique(clock))); 1131 new SiteEngagementService(profile(), base::WrapUnique(clock)));
1023 1132
1024 base::Time current_day = GetReferenceTime(); 1133 base::Time current_day = GetReferenceTime();
1025 clock->SetNow(current_day); 1134 clock->SetNow(current_day);
1026 base::HistogramTester histograms; 1135 base::HistogramTester histograms;
1027 GURL origin1("http://www.google.com/"); 1136 GURL origin1("http://www.google.com/");
1028 GURL origin2("http://drive.google.com/"); 1137 GURL origin2("http://drive.google.com/");
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
1098 0, 2); 1207 0, 2);
1099 1208
1100 // Add more points and ensure no more samples are present. 1209 // Add more points and ensure no more samples are present.
1101 service->AddPoints(origin1, 0.01); 1210 service->AddPoints(origin1, 0.01);
1102 service->AddPoints(origin2, 0.01); 1211 service->AddPoints(origin2, 0.01);
1103 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram, 1212 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedFromHistogram,
1104 4); 1213 4);
1105 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram, 1214 histograms.ExpectTotalCount(SiteEngagementMetrics::kScoreDecayedToHistogram,
1106 4); 1215 4);
1107 } 1216 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698