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

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

Powered by Google App Engine
This is Rietveld 408576698