File: site_engagement_helper_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (693 lines) | stat: -rw-r--r-- 28,356 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/site_engagement/content/site_engagement_helper.h"

#include <memory>

#include "base/memory/ptr_util.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/timer/mock_timer.h"
#include "base/values.h"
#include "components/permissions/test/test_permissions_client.h"
#include "components/prefs/testing_pref_service.h"
#include "components/site_engagement/content/engagement_type.h"
#include "components/site_engagement/content/site_engagement_metrics.h"
#include "components/site_engagement/content/site_engagement_score.h"
#include "components/site_engagement/content/site_engagement_service.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/input/web_gesture_event.h"
#include "third_party/blink/public/common/input/web_keyboard_event.h"
#include "third_party/blink/public/common/input/web_mouse_event.h"
#include "third_party/blink/public/common/input/web_touch_event.h"

namespace site_engagement {

using content::NavigationSimulator;

class TestSiteEngagementServiceProvider
    : public SiteEngagementService::ServiceProvider {
 public:
  explicit TestSiteEngagementServiceProvider(
      content::BrowserContext* browser_context)
      : site_engagement_service_(browser_context) {}
  virtual ~TestSiteEngagementServiceProvider() = default;

  SiteEngagementService* GetSiteEngagementService(
      content::BrowserContext* browser_context) override {
    return &site_engagement_service_;
  }

 private:
  SiteEngagementService site_engagement_service_;
};

class SiteEngagementHelperTest : public content::RenderViewHostTestHarness {
 public:
  void SetUp() override {
    content::RenderViewHostTestHarness::SetUp();
    SiteEngagementScore::SetParamValuesForTesting();

    user_prefs::UserPrefs::Set(browser_context(), &pref_service_);
    SiteEngagementService::RegisterProfilePrefs(pref_service_.registry());

    service_provider_ =
        std::make_unique<TestSiteEngagementServiceProvider>(browser_context());

    SiteEngagementService::SetServiceProvider(service_provider_.get());
  }

  void TearDown() override {
    SiteEngagementService::ClearServiceProvider(service_provider_.get());

    content::RenderViewHostTestHarness::TearDown();
  }

  SiteEngagementService::Helper* GetHelper(content::WebContents* web_contents) {
    SiteEngagementService::Helper::CreateForWebContents(web_contents);
    SiteEngagementService::Helper* helper =
        SiteEngagementService::Helper::FromWebContents(web_contents);

    DCHECK(helper);
    return helper;
  }

  void TrackingStarted(SiteEngagementService::Helper* helper) {
    helper->input_tracker_.TrackingStarted();
    helper->media_tracker_.TrackingStarted();
  }

  // Simulate a user interaction event and handle it.
  void HandleUserInput(SiteEngagementService::Helper* helper,
                       blink::WebInputEvent::Type type) {
    std::unique_ptr<blink::WebInputEvent> event;
    switch (type) {
      case blink::WebInputEvent::Type::kRawKeyDown:
        event = std::make_unique<blink::WebKeyboardEvent>();
        break;
      case blink::WebInputEvent::Type::kGestureScrollBegin:
        event = std::make_unique<blink::WebGestureEvent>();
        break;
      case blink::WebInputEvent::Type::kMouseDown:
        event = std::make_unique<blink::WebMouseEvent>();
        break;
      case blink::WebInputEvent::Type::kTouchStart:
        event = std::make_unique<blink::WebTouchEvent>();
        break;
      default:
        NOTREACHED();
    }
    event->SetType(type);
    helper->input_tracker_.DidGetUserInteraction(*event);
  }

  // Simulate a user interaction event and handle it. Reactivates tracking
  // immediately.
  void HandleUserInputAndRestartTracking(SiteEngagementService::Helper* helper,
                                         blink::WebInputEvent::Type type) {
    HandleUserInput(helper, type);
    helper->input_tracker_.TrackingStarted();
  }

  void HandleMediaPlaying(SiteEngagementService::Helper* helper,
                          bool is_hidden) {
    helper->RecordMediaPlaying(is_hidden);
  }

  void MediaStartedPlaying(SiteEngagementService::Helper* helper) {
    helper->media_tracker_.MediaStartedPlaying(
        content::WebContentsObserver::MediaPlayerInfo(false, false),
        content::MediaPlayerId::CreateMediaPlayerIdForTests());
  }

  void MediaStoppedPlaying(SiteEngagementService::Helper* helper) {
    helper->media_tracker_.MediaStoppedPlaying(
        content::WebContentsObserver::MediaPlayerInfo(false, false),
        content::MediaPlayerId::CreateMediaPlayerIdForTests(),
        content::WebContentsObserver::MediaStoppedReason::kUnspecified);
  }

  // Set a pause timer on the input tracker for test purposes.
  void SetInputTrackerPauseTimer(SiteEngagementService::Helper* helper,
                                 std::unique_ptr<base::OneShotTimer> timer) {
    helper->input_tracker_.SetPauseTimerForTesting(std::move(timer));
  }

  // Set a pause timer on the input tracker for test purposes.
  void SetMediaTrackerPauseTimer(SiteEngagementService::Helper* helper,
                                 std::unique_ptr<base::OneShotTimer> timer) {
    helper->media_tracker_.SetPauseTimerForTesting(std::move(timer));
  }

  bool IsTrackingInput(SiteEngagementService::Helper* helper) {
    return helper->input_tracker_.is_tracking();
  }

  void UserInputAccumulation(const blink::WebInputEvent::Type type) {
    GURL url1("https://www.google.com/");
    GURL url2("http://www.google.com/");
    content::WebContents* contents = web_contents();

    SiteEngagementService::Helper* helper = GetHelper(contents);
    SiteEngagementService* service =
        SiteEngagementService::Get(browser_context());
    DCHECK(service);

    // Check that navigation triggers engagement.
    NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url1);
    TrackingStarted(helper);

    EXPECT_DOUBLE_EQ(0.5, service->GetScore(url1));
    EXPECT_EQ(0, service->GetScore(url2));

    // Simulate a user input trigger and ensure it is treated correctly.
    HandleUserInputAndRestartTracking(helper, type);

    EXPECT_DOUBLE_EQ(0.55, service->GetScore(url1));
    EXPECT_EQ(0, service->GetScore(url2));

    // Simulate three inputs , and ensure they are treated correctly.
    HandleUserInputAndRestartTracking(helper, type);
    HandleUserInputAndRestartTracking(helper, type);
    HandleUserInputAndRestartTracking(helper, type);

    EXPECT_DOUBLE_EQ(0.7, service->GetScore(url1));
    EXPECT_EQ(0, service->GetScore(url2));

    // Simulate inputs for a different link.
    NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url2);
    TrackingStarted(helper);

    EXPECT_DOUBLE_EQ(0.7, service->GetScore(url1));
    EXPECT_DOUBLE_EQ(0.5, service->GetScore(url2));
    EXPECT_DOUBLE_EQ(1.2, service->GetTotalEngagementPoints());

    HandleUserInputAndRestartTracking(helper, type);
    EXPECT_DOUBLE_EQ(0.7, service->GetScore(url1));
    EXPECT_DOUBLE_EQ(0.55, service->GetScore(url2));
    EXPECT_DOUBLE_EQ(1.25, service->GetTotalEngagementPoints());
  }

 private:
  std::unique_ptr<TestSiteEngagementServiceProvider> service_provider_;
  TestingPrefServiceSimple pref_service_;
  permissions::TestPermissionsClient permissions_client_;
};

TEST_F(SiteEngagementHelperTest, KeyPressEngagementAccumulation) {
  UserInputAccumulation(blink::WebInputEvent::Type::kRawKeyDown);
}

TEST_F(SiteEngagementHelperTest, MouseDownEventEngagementAccumulation) {
  UserInputAccumulation(blink::WebInputEvent::Type::kMouseDown);
}

TEST_F(SiteEngagementHelperTest, ScrollEventEngagementAccumulation) {
  UserInputAccumulation(blink::WebInputEvent::Type::kGestureScrollBegin);
}

TEST_F(SiteEngagementHelperTest, TouchEngagementAccumulation) {
  UserInputAccumulation(blink::WebInputEvent::Type::kTouchStart);
}

TEST_F(SiteEngagementHelperTest, MediaEngagementAccumulation) {
  GURL url1("https://www.google.com/");
  GURL url2("http://www.google.com/");
  content::WebContents* contents = web_contents();

  SiteEngagementService::Helper* helper = GetHelper(contents);
  SiteEngagementService* service =
      SiteEngagementService::Get(browser_context());
  DCHECK(service);

  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url1);
  TrackingStarted(helper);

  EXPECT_DOUBLE_EQ(0.5, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));

  // Simulate a foreground media input and ensure it is treated correctly.
  HandleMediaPlaying(helper, false);

  EXPECT_DOUBLE_EQ(0.52, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));

  // Simulate continual media playing, and ensure it is treated correctly.
  HandleMediaPlaying(helper, false);
  HandleMediaPlaying(helper, false);
  HandleMediaPlaying(helper, false);

  EXPECT_DOUBLE_EQ(0.58, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));

  // Simulate backgrounding the media.
  HandleMediaPlaying(helper, true);
  HandleMediaPlaying(helper, true);

  EXPECT_DOUBLE_EQ(0.60, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));

  // Simulate inputs for a different link.
  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url2);
  TrackingStarted(helper);

  EXPECT_DOUBLE_EQ(0.6, service->GetScore(url1));
  EXPECT_DOUBLE_EQ(0.5, service->GetScore(url2));
  EXPECT_DOUBLE_EQ(1.1, service->GetTotalEngagementPoints());

  HandleMediaPlaying(helper, false);
  HandleMediaPlaying(helper, false);
  EXPECT_DOUBLE_EQ(0.6, service->GetScore(url1));
  EXPECT_DOUBLE_EQ(0.54, service->GetScore(url2));
  EXPECT_DOUBLE_EQ(1.14, service->GetTotalEngagementPoints());
}

TEST_F(SiteEngagementHelperTest, MediaEngagement) {
  GURL url1("https://www.google.com/");
  GURL url2("http://www.google.com/");
  content::WebContents* contents = web_contents();

  base::MockOneShotTimer* media_tracker_timer = new base::MockOneShotTimer();
  SiteEngagementService::Helper* helper = GetHelper(contents);
  SetMediaTrackerPauseTimer(helper, base::WrapUnique(media_tracker_timer));
  SiteEngagementService* service =
      SiteEngagementService::Get(browser_context());
  DCHECK(service);

  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url1);
  MediaStartedPlaying(helper);

  EXPECT_DOUBLE_EQ(0.50, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));
  EXPECT_TRUE(media_tracker_timer->IsRunning());

  media_tracker_timer->Fire();
  EXPECT_DOUBLE_EQ(0.52, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));
  EXPECT_TRUE(media_tracker_timer->IsRunning());

  contents->WasHidden();
  media_tracker_timer->Fire();
  EXPECT_DOUBLE_EQ(0.53, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));
  EXPECT_TRUE(media_tracker_timer->IsRunning());

  MediaStoppedPlaying(helper);
  media_tracker_timer->Fire();
  EXPECT_DOUBLE_EQ(0.53, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));
  EXPECT_TRUE(media_tracker_timer->IsRunning());

  contents->WasShown();
  media_tracker_timer->Fire();
  EXPECT_DOUBLE_EQ(0.53, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));
  EXPECT_TRUE(media_tracker_timer->IsRunning());

  MediaStartedPlaying(helper);
  media_tracker_timer->Fire();
  EXPECT_DOUBLE_EQ(0.55, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));
  EXPECT_TRUE(media_tracker_timer->IsRunning());

  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url2);
  EXPECT_DOUBLE_EQ(0.55, service->GetScore(url1));
  EXPECT_EQ(0.5, service->GetScore(url2));
  EXPECT_FALSE(media_tracker_timer->IsRunning());

  MediaStartedPlaying(helper);
  media_tracker_timer->Fire();
  EXPECT_DOUBLE_EQ(0.55, service->GetScore(url1));
  EXPECT_EQ(0.52, service->GetScore(url2));
  EXPECT_TRUE(media_tracker_timer->IsRunning());

  contents->WasHidden();
  media_tracker_timer->Fire();
  EXPECT_DOUBLE_EQ(0.55, service->GetScore(url1));
  EXPECT_EQ(0.53, service->GetScore(url2));
  EXPECT_TRUE(media_tracker_timer->IsRunning());

  MediaStoppedPlaying(helper);
  contents->WasShown();
  media_tracker_timer->Fire();
  EXPECT_DOUBLE_EQ(0.55, service->GetScore(url1));
  EXPECT_EQ(0.53, service->GetScore(url2));
  EXPECT_TRUE(media_tracker_timer->IsRunning());
}

TEST_F(SiteEngagementHelperTest, MixedInputEngagementAccumulation) {
  GURL url1("https://www.google.com/");
  GURL url2("http://www.google.com/");
  content::WebContents* contents = web_contents();

  SiteEngagementService::Helper* helper = GetHelper(contents);
  SiteEngagementService* service =
      SiteEngagementService::Get(browser_context());
  DCHECK(service);

  base::HistogramTester histograms;

  // Histograms should start off empty.
  histograms.ExpectTotalCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                              0);

  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url1);
  TrackingStarted(helper);

  EXPECT_DOUBLE_EQ(0.5, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));
  histograms.ExpectTotalCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                              2);
  histograms.ExpectBucketCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                               EngagementType::kNavigation, 1);
  histograms.ExpectBucketCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                               EngagementType::kFirstDailyEngagement, 1);

  HandleUserInputAndRestartTracking(helper,
                                    blink::WebInputEvent::Type::kRawKeyDown);
  HandleUserInputAndRestartTracking(helper,
                                    blink::WebInputEvent::Type::kTouchStart);
  HandleUserInputAndRestartTracking(helper,
                                    blink::WebInputEvent::Type::kTouchStart);
  HandleUserInputAndRestartTracking(helper,
                                    blink::WebInputEvent::Type::kRawKeyDown);
  HandleUserInputAndRestartTracking(helper,
                                    blink::WebInputEvent::Type::kMouseDown);

  EXPECT_DOUBLE_EQ(0.75, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));
  histograms.ExpectTotalCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                              7);
  histograms.ExpectBucketCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                               EngagementType::kNavigation, 1);
  histograms.ExpectBucketCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                               EngagementType::kKeypress, 2);
  histograms.ExpectBucketCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                               EngagementType::kMouse, 1);
  histograms.ExpectBucketCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                               EngagementType::kTouchGesture, 2);
  histograms.ExpectBucketCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                               EngagementType::kFirstDailyEngagement, 1);

  HandleUserInputAndRestartTracking(
      helper, blink::WebInputEvent::Type::kGestureScrollBegin);
  HandleUserInputAndRestartTracking(helper,
                                    blink::WebInputEvent::Type::kMouseDown);
  HandleMediaPlaying(helper, true);
  HandleUserInputAndRestartTracking(helper,
                                    blink::WebInputEvent::Type::kTouchStart);
  HandleMediaPlaying(helper, false);

  EXPECT_DOUBLE_EQ(0.93, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));
  histograms.ExpectTotalCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                              12);
  histograms.ExpectBucketCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                               EngagementType::kMouse, 2);
  histograms.ExpectBucketCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                               EngagementType::kScroll, 1);
  histograms.ExpectBucketCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                               EngagementType::kTouchGesture, 3);
  histograms.ExpectBucketCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                               EngagementType::kMediaVisible, 1);
  histograms.ExpectBucketCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                               EngagementType::kMediaHidden, 1);
  histograms.ExpectBucketCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                               EngagementType::kFirstDailyEngagement, 1);

  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url2);
  TrackingStarted(helper);

  EXPECT_DOUBLE_EQ(0.93, service->GetScore(url1));
  EXPECT_DOUBLE_EQ(0.5, service->GetScore(url2));
  EXPECT_DOUBLE_EQ(1.43, service->GetTotalEngagementPoints());

  HandleUserInputAndRestartTracking(helper,
                                    blink::WebInputEvent::Type::kTouchStart);
  HandleUserInputAndRestartTracking(helper,
                                    blink::WebInputEvent::Type::kRawKeyDown);

  EXPECT_DOUBLE_EQ(0.93, service->GetScore(url1));
  EXPECT_DOUBLE_EQ(0.6, service->GetScore(url2));
  EXPECT_DOUBLE_EQ(1.53, service->GetTotalEngagementPoints());
  histograms.ExpectTotalCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                              16);
  histograms.ExpectBucketCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                               EngagementType::kNavigation, 2);
  histograms.ExpectBucketCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                               EngagementType::kKeypress, 3);
  histograms.ExpectBucketCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                               EngagementType::kTouchGesture, 4);
  histograms.ExpectBucketCount(SiteEngagementMetrics::kEngagementTypeHistogram,
                               EngagementType::kFirstDailyEngagement, 2);
}

TEST_F(SiteEngagementHelperTest, CheckTimerAndCallbacks) {
  GURL url1("https://www.google.com/");
  GURL url2("http://www.google.com/");
  content::WebContents* contents = web_contents();

  base::MockOneShotTimer* input_tracker_timer = new base::MockOneShotTimer;
  base::MockOneShotTimer* media_tracker_timer = new base::MockOneShotTimer;
  SiteEngagementService::Helper* helper = GetHelper(contents);
  SetInputTrackerPauseTimer(helper, base::WrapUnique(input_tracker_timer));
  SetMediaTrackerPauseTimer(helper, base::WrapUnique(media_tracker_timer));

  SiteEngagementService* service =
      SiteEngagementService::Get(browser_context());
  DCHECK(service);

  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url1);
  EXPECT_DOUBLE_EQ(0.5, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));

  // Input timer should be running for navigation delay, but media timer is
  // inactive.
  EXPECT_TRUE(input_tracker_timer->IsRunning());
  EXPECT_FALSE(IsTrackingInput(helper));
  EXPECT_FALSE(media_tracker_timer->IsRunning());

  // Media timer starts once media is detected as playing.
  MediaStartedPlaying(helper);
  EXPECT_TRUE(media_tracker_timer->IsRunning());

  input_tracker_timer->Fire();
  media_tracker_timer->Fire();
  EXPECT_DOUBLE_EQ(0.52, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));

  // Input timer should start running again after input, but the media timer
  // keeps running.
  EXPECT_FALSE(input_tracker_timer->IsRunning());
  EXPECT_TRUE(IsTrackingInput(helper));
  EXPECT_TRUE(media_tracker_timer->IsRunning());

  HandleUserInput(helper, blink::WebInputEvent::Type::kRawKeyDown);
  EXPECT_TRUE(input_tracker_timer->IsRunning());
  EXPECT_FALSE(IsTrackingInput(helper));
  EXPECT_TRUE(media_tracker_timer->IsRunning());

  EXPECT_DOUBLE_EQ(0.57, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));

  input_tracker_timer->Fire();
  EXPECT_FALSE(input_tracker_timer->IsRunning());
  EXPECT_TRUE(IsTrackingInput(helper));
  EXPECT_TRUE(media_tracker_timer->IsRunning());

  // Timer should start running again after input.
  HandleUserInput(helper, blink::WebInputEvent::Type::kTouchStart);
  EXPECT_TRUE(input_tracker_timer->IsRunning());
  EXPECT_FALSE(IsTrackingInput(helper));
  EXPECT_TRUE(media_tracker_timer->IsRunning());

  EXPECT_DOUBLE_EQ(0.62, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));

  input_tracker_timer->Fire();
  EXPECT_FALSE(input_tracker_timer->IsRunning());
  EXPECT_TRUE(IsTrackingInput(helper));

  media_tracker_timer->Fire();
  EXPECT_TRUE(media_tracker_timer->IsRunning());
  EXPECT_DOUBLE_EQ(0.64, service->GetScore(url1));
  EXPECT_EQ(0, service->GetScore(url2));

  // Timer should be running for navigation delay. Media is disabled again.
  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url2);
  EXPECT_TRUE(input_tracker_timer->IsRunning());
  EXPECT_FALSE(IsTrackingInput(helper));
  EXPECT_FALSE(media_tracker_timer->IsRunning());

  EXPECT_DOUBLE_EQ(0.64, service->GetScore(url1));
  EXPECT_DOUBLE_EQ(0.5, service->GetScore(url2));
  EXPECT_DOUBLE_EQ(1.14, service->GetTotalEngagementPoints());

  input_tracker_timer->Fire();
  EXPECT_FALSE(input_tracker_timer->IsRunning());
  EXPECT_TRUE(IsTrackingInput(helper));
  EXPECT_FALSE(media_tracker_timer->IsRunning());

  HandleUserInput(helper, blink::WebInputEvent::Type::kMouseDown);
  EXPECT_TRUE(input_tracker_timer->IsRunning());
  EXPECT_FALSE(IsTrackingInput(helper));
  EXPECT_FALSE(media_tracker_timer->IsRunning());

  MediaStartedPlaying(helper);
  EXPECT_TRUE(media_tracker_timer->IsRunning());

  EXPECT_DOUBLE_EQ(0.64, service->GetScore(url1));
  EXPECT_DOUBLE_EQ(0.55, service->GetScore(url2));
  EXPECT_DOUBLE_EQ(1.19, service->GetTotalEngagementPoints());

  EXPECT_TRUE(media_tracker_timer->IsRunning());
  media_tracker_timer->Fire();
  EXPECT_DOUBLE_EQ(0.64, service->GetScore(url1));
  EXPECT_DOUBLE_EQ(0.57, service->GetScore(url2));
  EXPECT_DOUBLE_EQ(1.21, service->GetTotalEngagementPoints());
}

// Ensure that navigation and tab activation/hiding does not trigger input
// tracking until after a delay. We must manually call WasShown/WasHidden as
// they are not triggered automatically in this test environment.
TEST_F(SiteEngagementHelperTest, ShowAndHide) {
  GURL url1("https://www.google.com/");
  GURL url2("http://www.google.com/");
  content::WebContents* contents = web_contents();

  base::MockOneShotTimer* input_tracker_timer = new base::MockOneShotTimer();
  base::MockOneShotTimer* media_tracker_timer = new base::MockOneShotTimer();
  SiteEngagementService::Helper* helper = GetHelper(contents);
  SetInputTrackerPauseTimer(helper, base::WrapUnique(input_tracker_timer));
  SetMediaTrackerPauseTimer(helper, base::WrapUnique(media_tracker_timer));

  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url1);
  input_tracker_timer->Fire();

  // Hiding the tab should stop input tracking. Media tracking remains inactive.
  contents->WasHidden();
  EXPECT_FALSE(input_tracker_timer->IsRunning());
  EXPECT_FALSE(media_tracker_timer->IsRunning());
  EXPECT_FALSE(IsTrackingInput(helper));

  // Showing the tab should start tracking again after another delay. Media
  // tracking remains inactive.
  contents->WasShown();
  EXPECT_TRUE(input_tracker_timer->IsRunning());
  EXPECT_FALSE(media_tracker_timer->IsRunning());
  EXPECT_FALSE(IsTrackingInput(helper));

  // Start media tracking.
  MediaStartedPlaying(helper);
  EXPECT_TRUE(media_tracker_timer->IsRunning());

  // Hiding the tab should stop input tracking, but not media tracking.
  contents->WasHidden();
  EXPECT_FALSE(input_tracker_timer->IsRunning());
  EXPECT_TRUE(media_tracker_timer->IsRunning());
  EXPECT_FALSE(IsTrackingInput(helper));

  // Showing the tab should start tracking again after another delay. Media
  // tracking continues.
  contents->WasShown();
  EXPECT_TRUE(input_tracker_timer->IsRunning());
  EXPECT_TRUE(media_tracker_timer->IsRunning());
  EXPECT_FALSE(IsTrackingInput(helper));

  input_tracker_timer->Fire();
  media_tracker_timer->Fire();
  EXPECT_FALSE(input_tracker_timer->IsRunning());
  EXPECT_TRUE(media_tracker_timer->IsRunning());
  EXPECT_TRUE(IsTrackingInput(helper));
}

// Verify that the site engagement helper:
// - Doesn't reset input tracking on a visible <-> occluded transition.
// - Handles a hidden <-> occluded transition like a hidden <-> visible
//   transition.
TEST_F(SiteEngagementHelperTest, Occlusion) {
  base::MockOneShotTimer* input_tracker_timer = new base::MockOneShotTimer();
  SiteEngagementService::Helper* helper = GetHelper(web_contents());
  SetInputTrackerPauseTimer(helper, base::WrapUnique(input_tracker_timer));

  NavigationSimulator::NavigateAndCommitFromBrowser(
      web_contents(), GURL("https://www.google.com/"));
  input_tracker_timer->Fire();

  // Visible -> Occluded transition should not affect input tracking.
  EXPECT_EQ(content::Visibility::VISIBLE, web_contents()->GetVisibility());
  web_contents()->WasOccluded();
  EXPECT_EQ(content::Visibility::OCCLUDED, web_contents()->GetVisibility());
  EXPECT_FALSE(input_tracker_timer->IsRunning());
  EXPECT_TRUE(IsTrackingInput(helper));

  // Occluded -> Visible transition should not affect input tracking.
  EXPECT_EQ(content::Visibility::OCCLUDED, web_contents()->GetVisibility());
  web_contents()->WasShown();
  EXPECT_EQ(content::Visibility::VISIBLE, web_contents()->GetVisibility());
  EXPECT_FALSE(input_tracker_timer->IsRunning());
  EXPECT_TRUE(IsTrackingInput(helper));

  // Visible -> Occluded transition should not affect input tracking.
  EXPECT_EQ(content::Visibility::VISIBLE, web_contents()->GetVisibility());
  web_contents()->WasOccluded();
  EXPECT_EQ(content::Visibility::OCCLUDED, web_contents()->GetVisibility());
  EXPECT_FALSE(input_tracker_timer->IsRunning());
  EXPECT_TRUE(IsTrackingInput(helper));

  // Occluded -> Hidden transition should stop input tracking.
  EXPECT_EQ(content::Visibility::OCCLUDED, web_contents()->GetVisibility());
  web_contents()->WasHidden();
  EXPECT_EQ(content::Visibility::HIDDEN, web_contents()->GetVisibility());
  EXPECT_FALSE(input_tracker_timer->IsRunning());
  EXPECT_FALSE(IsTrackingInput(helper));

  // Hidden -> Occluded transition should start a timer to track input.
  EXPECT_EQ(content::Visibility::HIDDEN, web_contents()->GetVisibility());
  web_contents()->WasOccluded();
  EXPECT_EQ(content::Visibility::OCCLUDED, web_contents()->GetVisibility());
  EXPECT_TRUE(input_tracker_timer->IsRunning());
  EXPECT_FALSE(IsTrackingInput(helper));
}

// Ensure tracking behavior is correct for multiple navigations in a single tab.
TEST_F(SiteEngagementHelperTest, SingleTabNavigation) {
  GURL url1("https://www.google.com/");
  GURL url2("https://www.example.com/");
  content::WebContents* contents = web_contents();

  base::MockOneShotTimer* input_tracker_timer = new base::MockOneShotTimer();
  SiteEngagementService::Helper* helper = GetHelper(contents);
  SetInputTrackerPauseTimer(helper, base::WrapUnique(input_tracker_timer));

  // Navigation should start the initial delay timer.
  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url1);
  EXPECT_TRUE(input_tracker_timer->IsRunning());
  EXPECT_FALSE(IsTrackingInput(helper));

  // Navigating before the timer fires should simply reset the timer.
  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url2);
  EXPECT_TRUE(input_tracker_timer->IsRunning());
  EXPECT_FALSE(IsTrackingInput(helper));

  // When the timer fires, callbacks are added.
  input_tracker_timer->Fire();
  EXPECT_FALSE(input_tracker_timer->IsRunning());
  EXPECT_TRUE(IsTrackingInput(helper));

  // Navigation should start the initial delay timer again.
  NavigationSimulator::NavigateAndCommitFromBrowser(web_contents(), url1);
  EXPECT_TRUE(input_tracker_timer->IsRunning());
  EXPECT_FALSE(IsTrackingInput(helper));
}

}  // namespace site_engagement