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

Unified 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: Addressing reviewer comments 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/engagement/site_engagement_service.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 85e709da56e3038918725d5fd517713441c19ae2..3af889d0182068ff610808f72cac801f9b2e4252 100644
--- a/chrome/browser/engagement/site_engagement_service_unittest.cc
+++ b/chrome/browser/engagement/site_engagement_service_unittest.cc
@@ -97,6 +97,46 @@ std::unique_ptr<KeyedService> BuildTestHistoryService(
} // namespace
+class ObserverTester : public SiteEngagementObserver {
+ public:
+ ObserverTester(SiteEngagementService* service,
+ content::WebContents* web_contents,
+ const GURL& url,
+ double score)
+ : SiteEngagementObserver(service),
+ web_contents_(web_contents),
+ url_(url),
+ score_(score),
+ callback_called_(false),
+ run_loop_() {}
+
+ void OnEngagementIncreased(content::WebContents* web_contents,
+ const GURL& url,
+ double score) override {
+ EXPECT_EQ(web_contents_, web_contents);
+ EXPECT_EQ(url_, url);
+ EXPECT_DOUBLE_EQ(score_, score);
+ set_callback_called(true);
+ run_loop_.Quit();
+ }
+
+ void Wait() { run_loop_.Run(); }
+
+ bool callback_called() { return callback_called_; }
+ void set_callback_called(bool callback_called) {
+ callback_called_ = callback_called;
+ }
+
+ private:
+ content::WebContents* web_contents_;
+ GURL url_;
+ double score_;
+ bool callback_called_;
+ base::RunLoop run_loop_;
+
+ DISALLOW_COPY_AND_ASSIGN(ObserverTester);
+};
+
class SiteEngagementServiceTest : public ChromeRenderViewHostTestHarness {
public:
void SetUp() override {
@@ -1016,6 +1056,69 @@ TEST_F(SiteEngagementServiceTest, EngagementLevel) {
url2, SiteEngagementService::ENGAGEMENT_LEVEL_MAX));
}
+TEST_F(SiteEngagementServiceTest, Observers) {
+ SiteEngagementService* service = SiteEngagementService::Get(profile());
+
+ GURL url_score_1("http://www.google.com/maps");
+ GURL url_score_2("http://www.google.com/drive");
+ GURL url_score_3("http://www.google.com/");
+ GURL url_not_called("https://www.google.com/");
+
+ // Create an observer and Observe(nullptr).
+ ObserverTester tester_not_called(service, web_contents(), url_not_called, 1);
+ tester_not_called.Observe(nullptr);
+
+ {
+ // Create an observer for navigation.
+ ObserverTester tester(service, web_contents(), url_score_1, 0.5);
+ NavigateAndCommit(url_score_1);
+ service->HandleNavigation(web_contents(), ui::PAGE_TRANSITION_TYPED);
+ tester.Wait();
+ EXPECT_TRUE(tester.callback_called());
+ EXPECT_FALSE(tester_not_called.callback_called());
+ tester.Observe(nullptr);
+ }
+
+ {
+ // Update observer for a user input.
+ ObserverTester tester(service, web_contents(), url_score_2, 0.55);
+ NavigateAndCommit(url_score_2);
+ service->HandleUserInput(web_contents(),
+ SiteEngagementMetrics::ENGAGEMENT_MOUSE);
+ tester.Wait();
+ EXPECT_TRUE(tester.callback_called());
+ EXPECT_FALSE(tester_not_called.callback_called());
+ tester.Observe(nullptr);
+ }
+
+ // Add two observers for media playing in the foreground.
+ {
+ ObserverTester tester_1(service, web_contents(), url_score_3, 0.57);
+ ObserverTester tester_2(service, web_contents(), url_score_3, 0.57);
+ NavigateAndCommit(url_score_3);
+ service->HandleMediaPlaying(web_contents(), false);
+ tester_1.Wait();
+ tester_2.Wait();
+
+ EXPECT_TRUE(tester_1.callback_called());
+ EXPECT_TRUE(tester_2.callback_called());
+ EXPECT_FALSE(tester_not_called.callback_called());
+ tester_1.Observe(nullptr);
+ tester_2.Observe(nullptr);
+ }
+
+ // Add an observer for media playing in the background.
+ {
+ ObserverTester tester(service, web_contents(), url_score_3, 0.58);
+ service->HandleMediaPlaying(web_contents(), true);
+ tester.Wait();
+
+ EXPECT_TRUE(tester.callback_called());
+ EXPECT_FALSE(tester_not_called.callback_called());
+ tester.Observe(nullptr);
+ }
+}
+
TEST_F(SiteEngagementServiceTest, ScoreDecayHistograms) {
base::SimpleTestClock* clock = new base::SimpleTestClock();
std::unique_ptr<SiteEngagementService> service(
« no previous file with comments | « chrome/browser/engagement/site_engagement_service.cc ('k') | chrome/chrome_browser.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698