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

Side by Side Diff: chrome/browser/engagement/site_engagement_helper_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 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_helper.h" 5 #include "chrome/browser/engagement/site_engagement_helper.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/test/histogram_tester.h" 8 #include "base/test/histogram_tester.h"
9 #include "base/timer/mock_timer.h" 9 #include "base/timer/mock_timer.h"
10 #include "base/values.h" 10 #include "base/values.h"
11 #include "chrome/browser/engagement/site_engagement_score.h"
11 #include "chrome/browser/engagement/site_engagement_service.h" 12 #include "chrome/browser/engagement/site_engagement_service.h"
12 #include "chrome/browser/engagement/site_engagement_service_factory.h"
13 #include "chrome/common/chrome_switches.h" 13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 14 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
15 #include "chrome/test/base/testing_profile.h" 15 #include "chrome/test/base/testing_profile.h"
16 #include "content/public/browser/navigation_entry.h" 16 #include "content/public/browser/navigation_entry.h"
17 #include "content/public/browser/page_navigator.h" 17 #include "content/public/browser/page_navigator.h"
18 #include "content/public/browser/web_contents.h" 18 #include "content/public/browser/web_contents.h"
19 #include "content/public/test/web_contents_tester.h" 19 #include "content/public/test/web_contents_tester.h"
20 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
21 21
22 class SiteEngagementHelperTest : public ChromeRenderViewHostTestHarness { 22 class SiteEngagementHelperTest : public ChromeRenderViewHostTestHarness {
23 public: 23 public:
24 void SetUp() override { 24 void SetUp() override {
25 ChromeRenderViewHostTestHarness::SetUp(); 25 ChromeRenderViewHostTestHarness::SetUp();
26 SiteEngagementScore::SetParamValuesForTesting(); 26 SiteEngagementScore::SetParamValuesForTesting();
27 } 27 }
28 28
29 SiteEngagementHelper* GetHelper(content::WebContents* web_contents) { 29 SiteEngagementService::Helper* GetHelper(content::WebContents* web_contents) {
30 SiteEngagementHelper::CreateForWebContents(web_contents); 30 SiteEngagementService::Helper::CreateForWebContents(web_contents);
31 SiteEngagementHelper* helper = 31 SiteEngagementService::Helper* helper =
32 SiteEngagementHelper::FromWebContents(web_contents); 32 SiteEngagementService::Helper::FromWebContents(web_contents);
33 33
34 DCHECK(helper); 34 DCHECK(helper);
35 return helper; 35 return helper;
36 } 36 }
37 37
38 void TrackingStarted(SiteEngagementHelper* helper) { 38 void TrackingStarted(SiteEngagementService::Helper* helper) {
39 helper->input_tracker_.TrackingStarted(); 39 helper->input_tracker_.TrackingStarted();
40 helper->media_tracker_.TrackingStarted(); 40 helper->media_tracker_.TrackingStarted();
41 } 41 }
42 42
43 // Simulate a user interaction event and handle it. 43 // Simulate a user interaction event and handle it.
44 void HandleUserInput(SiteEngagementHelper* helper, 44 void HandleUserInput(SiteEngagementService::Helper* helper,
45 blink::WebInputEvent::Type type) { 45 blink::WebInputEvent::Type type) {
46 helper->input_tracker_.DidGetUserInteraction(type); 46 helper->input_tracker_.DidGetUserInteraction(type);
47 } 47 }
48 48
49 // Simulate a user interaction event and handle it. Reactivates tracking 49 // Simulate a user interaction event and handle it. Reactivates tracking
50 // immediately. 50 // immediately.
51 void HandleUserInputAndRestartTracking(SiteEngagementHelper* helper, 51 void HandleUserInputAndRestartTracking(SiteEngagementService::Helper* helper,
52 blink::WebInputEvent::Type type) { 52 blink::WebInputEvent::Type type) {
53 helper->input_tracker_.DidGetUserInteraction(type); 53 helper->input_tracker_.DidGetUserInteraction(type);
54 helper->input_tracker_.TrackingStarted(); 54 helper->input_tracker_.TrackingStarted();
55 } 55 }
56 56
57 void HandleMediaPlaying(SiteEngagementHelper* helper, bool is_hidden) { 57 void HandleMediaPlaying(SiteEngagementService::Helper* helper,
58 bool is_hidden) {
58 helper->RecordMediaPlaying(is_hidden); 59 helper->RecordMediaPlaying(is_hidden);
59 } 60 }
60 61
61 void MediaStartedPlaying(SiteEngagementHelper* helper) { 62 void MediaStartedPlaying(SiteEngagementService::Helper* helper) {
62 helper->media_tracker_.MediaStartedPlaying( 63 helper->media_tracker_.MediaStartedPlaying(
63 content::WebContentsObserver::MediaPlayerId(nullptr, 1)); 64 content::WebContentsObserver::MediaPlayerId(nullptr, 1));
64 } 65 }
65 66
66 void MediaStoppedPlaying(SiteEngagementHelper* helper) { 67 void MediaStoppedPlaying(SiteEngagementService::Helper* helper) {
67 helper->media_tracker_.MediaStoppedPlaying( 68 helper->media_tracker_.MediaStoppedPlaying(
68 content::WebContentsObserver::MediaPlayerId(nullptr, 1)); 69 content::WebContentsObserver::MediaPlayerId(nullptr, 1));
69 } 70 }
70 71
71 // Set a pause timer on the input tracker for test purposes. 72 // Set a pause timer on the input tracker for test purposes.
72 void SetInputTrackerPauseTimer(SiteEngagementHelper* helper, 73 void SetInputTrackerPauseTimer(SiteEngagementService::Helper* helper,
73 std::unique_ptr<base::Timer> timer) { 74 std::unique_ptr<base::Timer> timer) {
74 helper->input_tracker_.SetPauseTimerForTesting(std::move(timer)); 75 helper->input_tracker_.SetPauseTimerForTesting(std::move(timer));
75 } 76 }
76 77
77 // Set a pause timer on the input tracker for test purposes. 78 // Set a pause timer on the input tracker for test purposes.
78 void SetMediaTrackerPauseTimer(SiteEngagementHelper* helper, 79 void SetMediaTrackerPauseTimer(SiteEngagementService::Helper* helper,
79 std::unique_ptr<base::Timer> timer) { 80 std::unique_ptr<base::Timer> timer) {
80 helper->media_tracker_.SetPauseTimerForTesting(std::move(timer)); 81 helper->media_tracker_.SetPauseTimerForTesting(std::move(timer));
81 } 82 }
82 83
83 bool IsTrackingInput(SiteEngagementHelper* helper) { 84 bool IsTrackingInput(SiteEngagementService::Helper* helper) {
84 return helper->input_tracker_.is_tracking(); 85 return helper->input_tracker_.is_tracking();
85 } 86 }
86 87
87 void Navigate(const GURL& url) { 88 void Navigate(const GURL& url) {
88 controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED, 89 controller().LoadURL(url, content::Referrer(), ui::PAGE_TRANSITION_TYPED,
89 std::string()); 90 std::string());
90 int pending_id = controller().GetPendingEntry()->GetUniqueID(); 91 int pending_id = controller().GetPendingEntry()->GetUniqueID();
91 content::WebContentsTester::For(web_contents()) 92 content::WebContentsTester::For(web_contents())
92 ->TestDidNavigate(web_contents()->GetMainFrame(), 1, pending_id, true, 93 ->TestDidNavigate(web_contents()->GetMainFrame(), 1, pending_id, true,
93 url, ui::PAGE_TRANSITION_TYPED); 94 url, ui::PAGE_TRANSITION_TYPED);
94 } 95 }
95 96
96 void UserInputAccumulation(const blink::WebInputEvent::Type type) { 97 void UserInputAccumulation(const blink::WebInputEvent::Type type) {
97 GURL url1("https://www.google.com/"); 98 GURL url1("https://www.google.com/");
98 GURL url2("http://www.google.com/"); 99 GURL url2("http://www.google.com/");
99 content::WebContents* contents = web_contents(); 100 content::WebContents* contents = web_contents();
100 101
101 SiteEngagementHelper* helper = GetHelper(contents); 102 SiteEngagementService::Helper* helper = GetHelper(contents);
102 SiteEngagementService* service = 103 SiteEngagementService* service = SiteEngagementService::Get(profile());
103 SiteEngagementServiceFactory::GetForProfile(profile());
104 DCHECK(service); 104 DCHECK(service);
105 105
106 // Check that navigation triggers engagement. 106 // Check that navigation triggers engagement.
107 Navigate(url1); 107 Navigate(url1);
108 TrackingStarted(helper); 108 TrackingStarted(helper);
109 109
110 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url1)); 110 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url1));
111 EXPECT_EQ(0, service->GetScore(url2)); 111 EXPECT_EQ(0, service->GetScore(url2));
112 112
113 // Simulate a user input trigger and ensure it is treated correctly. 113 // Simulate a user input trigger and ensure it is treated correctly.
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 153
154 TEST_F(SiteEngagementHelperTest, GestureEngagementAccumulation) { 154 TEST_F(SiteEngagementHelperTest, GestureEngagementAccumulation) {
155 UserInputAccumulation(blink::WebInputEvent::GestureTapDown); 155 UserInputAccumulation(blink::WebInputEvent::GestureTapDown);
156 } 156 }
157 157
158 TEST_F(SiteEngagementHelperTest, MediaEngagementAccumulation) { 158 TEST_F(SiteEngagementHelperTest, MediaEngagementAccumulation) {
159 GURL url1("https://www.google.com/"); 159 GURL url1("https://www.google.com/");
160 GURL url2("http://www.google.com/"); 160 GURL url2("http://www.google.com/");
161 content::WebContents* contents = web_contents(); 161 content::WebContents* contents = web_contents();
162 162
163 SiteEngagementHelper* helper = GetHelper(contents); 163 SiteEngagementService::Helper* helper = GetHelper(contents);
164 SiteEngagementService* service = 164 SiteEngagementService* service = SiteEngagementService::Get(profile());
165 SiteEngagementServiceFactory::GetForProfile(profile());
166 DCHECK(service); 165 DCHECK(service);
167 166
168 Navigate(url1); 167 Navigate(url1);
169 TrackingStarted(helper); 168 TrackingStarted(helper);
170 169
171 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url1)); 170 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url1));
172 EXPECT_EQ(0, service->GetScore(url2)); 171 EXPECT_EQ(0, service->GetScore(url2));
173 172
174 // Simulate a foreground media input and ensure it is treated correctly. 173 // Simulate a foreground media input and ensure it is treated correctly.
175 HandleMediaPlaying(helper, false); 174 HandleMediaPlaying(helper, false);
(...skipping 30 matching lines...) Expand all
206 EXPECT_DOUBLE_EQ(0.54, service->GetScore(url2)); 205 EXPECT_DOUBLE_EQ(0.54, service->GetScore(url2));
207 EXPECT_DOUBLE_EQ(1.14, service->GetTotalEngagementPoints()); 206 EXPECT_DOUBLE_EQ(1.14, service->GetTotalEngagementPoints());
208 } 207 }
209 208
210 TEST_F(SiteEngagementHelperTest, MediaEngagement) { 209 TEST_F(SiteEngagementHelperTest, MediaEngagement) {
211 GURL url1("https://www.google.com/"); 210 GURL url1("https://www.google.com/");
212 GURL url2("http://www.google.com/"); 211 GURL url2("http://www.google.com/");
213 content::WebContents* contents = web_contents(); 212 content::WebContents* contents = web_contents();
214 213
215 base::MockTimer* media_tracker_timer = new base::MockTimer(true, false); 214 base::MockTimer* media_tracker_timer = new base::MockTimer(true, false);
216 SiteEngagementHelper* helper = GetHelper(contents); 215 SiteEngagementService::Helper* helper = GetHelper(contents);
217 SetMediaTrackerPauseTimer(helper, base::WrapUnique(media_tracker_timer)); 216 SetMediaTrackerPauseTimer(helper, base::WrapUnique(media_tracker_timer));
218 SiteEngagementService* service = 217 SiteEngagementService* service = SiteEngagementService::Get(profile());
219 SiteEngagementServiceFactory::GetForProfile(profile());
220 DCHECK(service); 218 DCHECK(service);
221 219
222 Navigate(url1); 220 Navigate(url1);
223 MediaStartedPlaying(helper); 221 MediaStartedPlaying(helper);
224 222
225 EXPECT_DOUBLE_EQ(0.50, service->GetScore(url1)); 223 EXPECT_DOUBLE_EQ(0.50, service->GetScore(url1));
226 EXPECT_EQ(0, service->GetScore(url2)); 224 EXPECT_EQ(0, service->GetScore(url2));
227 EXPECT_TRUE(media_tracker_timer->IsRunning()); 225 EXPECT_TRUE(media_tracker_timer->IsRunning());
228 226
229 media_tracker_timer->Fire(); 227 media_tracker_timer->Fire();
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 EXPECT_DOUBLE_EQ(0.55, service->GetScore(url1)); 276 EXPECT_DOUBLE_EQ(0.55, service->GetScore(url1));
279 EXPECT_EQ(0.53, service->GetScore(url2)); 277 EXPECT_EQ(0.53, service->GetScore(url2));
280 EXPECT_TRUE(media_tracker_timer->IsRunning()); 278 EXPECT_TRUE(media_tracker_timer->IsRunning());
281 } 279 }
282 280
283 TEST_F(SiteEngagementHelperTest, MixedInputEngagementAccumulation) { 281 TEST_F(SiteEngagementHelperTest, MixedInputEngagementAccumulation) {
284 GURL url1("https://www.google.com/"); 282 GURL url1("https://www.google.com/");
285 GURL url2("http://www.google.com/"); 283 GURL url2("http://www.google.com/");
286 content::WebContents* contents = web_contents(); 284 content::WebContents* contents = web_contents();
287 285
288 SiteEngagementHelper* helper = GetHelper(contents); 286 SiteEngagementService::Helper* helper = GetHelper(contents);
289 SiteEngagementService* service = 287 SiteEngagementService* service = SiteEngagementService::Get(profile());
290 SiteEngagementServiceFactory::GetForProfile(profile());
291 DCHECK(service); 288 DCHECK(service);
292 289
293 base::HistogramTester histograms; 290 base::HistogramTester histograms;
294 291
295 // Histograms should start off empty. 292 // Histograms should start off empty.
296 histograms.ExpectTotalCount(SiteEngagementMetrics::kEngagementTypeHistogram, 293 histograms.ExpectTotalCount(SiteEngagementMetrics::kEngagementTypeHistogram,
297 0); 294 0);
298 295
299 Navigate(url1); 296 Navigate(url1);
300 TrackingStarted(helper); 297 TrackingStarted(helper);
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 SiteEngagementMetrics::ENGAGEMENT_FIRST_DAILY_ENGAGEMENT, 2); 388 SiteEngagementMetrics::ENGAGEMENT_FIRST_DAILY_ENGAGEMENT, 2);
392 } 389 }
393 390
394 TEST_F(SiteEngagementHelperTest, CheckTimerAndCallbacks) { 391 TEST_F(SiteEngagementHelperTest, CheckTimerAndCallbacks) {
395 GURL url1("https://www.google.com/"); 392 GURL url1("https://www.google.com/");
396 GURL url2("http://www.google.com/"); 393 GURL url2("http://www.google.com/");
397 content::WebContents* contents = web_contents(); 394 content::WebContents* contents = web_contents();
398 395
399 base::MockTimer* input_tracker_timer = new base::MockTimer(true, false); 396 base::MockTimer* input_tracker_timer = new base::MockTimer(true, false);
400 base::MockTimer* media_tracker_timer = new base::MockTimer(true, false); 397 base::MockTimer* media_tracker_timer = new base::MockTimer(true, false);
401 SiteEngagementHelper* helper = GetHelper(contents); 398 SiteEngagementService::Helper* helper = GetHelper(contents);
402 SetInputTrackerPauseTimer(helper, base::WrapUnique(input_tracker_timer)); 399 SetInputTrackerPauseTimer(helper, base::WrapUnique(input_tracker_timer));
403 SetMediaTrackerPauseTimer(helper, base::WrapUnique(media_tracker_timer)); 400 SetMediaTrackerPauseTimer(helper, base::WrapUnique(media_tracker_timer));
404 401
405 SiteEngagementService* service = 402 SiteEngagementService* service = SiteEngagementService::Get(profile());
406 SiteEngagementServiceFactory::GetForProfile(profile());
407 DCHECK(service); 403 DCHECK(service);
408 404
409 Navigate(url1); 405 Navigate(url1);
410 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url1)); 406 EXPECT_DOUBLE_EQ(0.5, service->GetScore(url1));
411 EXPECT_EQ(0, service->GetScore(url2)); 407 EXPECT_EQ(0, service->GetScore(url2));
412 408
413 // Input timer should be running for navigation delay, but media timer is 409 // Input timer should be running for navigation delay, but media timer is
414 // inactive. 410 // inactive.
415 EXPECT_TRUE(input_tracker_timer->IsRunning()); 411 EXPECT_TRUE(input_tracker_timer->IsRunning());
416 EXPECT_FALSE(IsTrackingInput(helper)); 412 EXPECT_FALSE(IsTrackingInput(helper));
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 // Ensure that navigation and tab activation/hiding does not trigger input 495 // Ensure that navigation and tab activation/hiding does not trigger input
500 // tracking until after a delay. We must manually call WasShown/WasHidden as 496 // tracking until after a delay. We must manually call WasShown/WasHidden as
501 // they are not triggered automatically in this test environment. 497 // they are not triggered automatically in this test environment.
502 TEST_F(SiteEngagementHelperTest, ShowAndHide) { 498 TEST_F(SiteEngagementHelperTest, ShowAndHide) {
503 GURL url1("https://www.google.com/"); 499 GURL url1("https://www.google.com/");
504 GURL url2("http://www.google.com/"); 500 GURL url2("http://www.google.com/");
505 content::WebContents* contents = web_contents(); 501 content::WebContents* contents = web_contents();
506 502
507 base::MockTimer* input_tracker_timer = new base::MockTimer(true, false); 503 base::MockTimer* input_tracker_timer = new base::MockTimer(true, false);
508 base::MockTimer* media_tracker_timer = new base::MockTimer(true, false); 504 base::MockTimer* media_tracker_timer = new base::MockTimer(true, false);
509 SiteEngagementHelper* helper = GetHelper(contents); 505 SiteEngagementService::Helper* helper = GetHelper(contents);
510 SetInputTrackerPauseTimer(helper, base::WrapUnique(input_tracker_timer)); 506 SetInputTrackerPauseTimer(helper, base::WrapUnique(input_tracker_timer));
511 SetMediaTrackerPauseTimer(helper, base::WrapUnique(media_tracker_timer)); 507 SetMediaTrackerPauseTimer(helper, base::WrapUnique(media_tracker_timer));
512 508
513 Navigate(url1); 509 Navigate(url1);
514 input_tracker_timer->Fire(); 510 input_tracker_timer->Fire();
515 511
516 // Hiding the tab should stop input tracking. Media tracking remains inactive. 512 // Hiding the tab should stop input tracking. Media tracking remains inactive.
517 contents->WasHidden(); 513 contents->WasHidden();
518 EXPECT_FALSE(input_tracker_timer->IsRunning()); 514 EXPECT_FALSE(input_tracker_timer->IsRunning());
519 EXPECT_FALSE(media_tracker_timer->IsRunning()); 515 EXPECT_FALSE(media_tracker_timer->IsRunning());
(...skipping 30 matching lines...) Expand all
550 EXPECT_TRUE(IsTrackingInput(helper)); 546 EXPECT_TRUE(IsTrackingInput(helper));
551 } 547 }
552 548
553 // Ensure tracking behavior is correct for multiple navigations in a single tab. 549 // Ensure tracking behavior is correct for multiple navigations in a single tab.
554 TEST_F(SiteEngagementHelperTest, SingleTabNavigation) { 550 TEST_F(SiteEngagementHelperTest, SingleTabNavigation) {
555 GURL url1("https://www.google.com/"); 551 GURL url1("https://www.google.com/");
556 GURL url2("https://www.example.com/"); 552 GURL url2("https://www.example.com/");
557 content::WebContents* contents = web_contents(); 553 content::WebContents* contents = web_contents();
558 554
559 base::MockTimer* input_tracker_timer = new base::MockTimer(true, false); 555 base::MockTimer* input_tracker_timer = new base::MockTimer(true, false);
560 SiteEngagementHelper* helper = GetHelper(contents); 556 SiteEngagementService::Helper* helper = GetHelper(contents);
561 SetInputTrackerPauseTimer(helper, base::WrapUnique(input_tracker_timer)); 557 SetInputTrackerPauseTimer(helper, base::WrapUnique(input_tracker_timer));
562 558
563 // Navigation should start the initial delay timer. 559 // Navigation should start the initial delay timer.
564 Navigate(url1); 560 Navigate(url1);
565 EXPECT_TRUE(input_tracker_timer->IsRunning()); 561 EXPECT_TRUE(input_tracker_timer->IsRunning());
566 EXPECT_FALSE(IsTrackingInput(helper)); 562 EXPECT_FALSE(IsTrackingInput(helper));
567 563
568 // Navigating before the timer fires should simply reset the timer. 564 // Navigating before the timer fires should simply reset the timer.
569 Navigate(url2); 565 Navigate(url2);
570 EXPECT_TRUE(input_tracker_timer->IsRunning()); 566 EXPECT_TRUE(input_tracker_timer->IsRunning());
571 EXPECT_FALSE(IsTrackingInput(helper)); 567 EXPECT_FALSE(IsTrackingInput(helper));
572 568
573 // When the timer fires, callbacks are added. 569 // When the timer fires, callbacks are added.
574 input_tracker_timer->Fire(); 570 input_tracker_timer->Fire();
575 EXPECT_FALSE(input_tracker_timer->IsRunning()); 571 EXPECT_FALSE(input_tracker_timer->IsRunning());
576 EXPECT_TRUE(IsTrackingInput(helper)); 572 EXPECT_TRUE(IsTrackingInput(helper));
577 573
578 // Navigation should start the initial delay timer again. 574 // Navigation should start the initial delay timer again.
579 Navigate(url1); 575 Navigate(url1);
580 EXPECT_TRUE(input_tracker_timer->IsRunning()); 576 EXPECT_TRUE(input_tracker_timer->IsRunning());
581 EXPECT_FALSE(IsTrackingInput(helper)); 577 EXPECT_FALSE(IsTrackingInput(helper));
582 } 578 }
OLDNEW
« no previous file with comments | « chrome/browser/engagement/site_engagement_helper.cc ('k') | chrome/browser/engagement/site_engagement_score.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698