File: tab_usage_scenario_tracker_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 (655 lines) | stat: -rw-r--r-- 27,767 bytes parent folder | download | duplicates (6)
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
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/metrics/usage_scenario/tab_usage_scenario_tracker.h"

#include <memory>

#include "base/memory/raw_ptr.h"
#include "base/time/time.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/ukm/content/source_url_recorder.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/browser/visibility.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/web_contents_tester.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace metrics {

namespace {

constexpr base::TimeDelta kInterval = base::Minutes(2);

class MockTabUsageScenarioTracker : public TabUsageScenarioTracker {
 public:
  explicit MockTabUsageScenarioTracker(
      UsageScenarioDataStoreImpl* usage_scenario_data_store)
      : TabUsageScenarioTracker(usage_scenario_data_store) {}

  void SetNumDisplays(int num_displays) {
    num_displays_ = num_displays;
    OnNumDisplaysChanged();
  }

  void SetNumDisplaysWithoutNotification(int num_displays) {
    num_displays_ = num_displays;
  }

  int GetNumDisplays() override { return num_displays_; }

 private:
  int num_displays_ = 1;
};

// Inherit from ChromeRenderViewHostTestHarness for access to test profile.
class TabUsageScenarioTrackerTest : public ChromeRenderViewHostTestHarness {
 public:
  TabUsageScenarioTrackerTest()
      : ChromeRenderViewHostTestHarness(
            base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  ~TabUsageScenarioTrackerTest() override = default;
  TabUsageScenarioTrackerTest(const TabUsageScenarioTrackerTest& other) =
      delete;
  TabUsageScenarioTrackerTest& operator=(const TabUsageScenarioTrackerTest&) =
      delete;

  void SetUp() override {
    ChromeRenderViewHostTestHarness::SetUp();
    tab_usage_scenario_tracker_ = std::make_unique<MockTabUsageScenarioTracker>(
        &usage_scenario_data_store_);
  }

  void TearDown() override {
    tab_usage_scenario_tracker_.reset();
    ChromeRenderViewHostTestHarness::TearDown();
  }

  std::unique_ptr<content::WebContents> CreateWebContents() {
    std::unique_ptr<content::WebContents> contents(
        content::WebContentsTester::CreateTestWebContents(profile(), nullptr));
    ukm::InitializeSourceUrlRecorderForWebContents(contents.get());
    return contents;
  }

  void MakeTabVisible(content::WebContents* contents) {
    contents->WasShown();
    tab_usage_scenario_tracker_->OnTabVisibilityChanged(contents);
  }

  void MakeTabHidden(content::WebContents* contents) {
    contents->WasHidden();
    tab_usage_scenario_tracker_->OnTabVisibilityChanged(contents);
  }

  void MakeTabOccluded(content::WebContents* contents) {
    contents->WasOccluded();
    tab_usage_scenario_tracker_->OnTabVisibilityChanged(contents);
  }

  void NavigateAndCommitTab(content::WebContents* contents, const GURL& gurl) {
    content::NavigationSimulator::NavigateAndCommitFromBrowser(contents, gurl);
    tab_usage_scenario_tracker_->OnPrimaryMainFrameNavigationCommitted(
        contents);
  }

 protected:
  UsageScenarioDataStoreImpl usage_scenario_data_store_;
  std::unique_ptr<MockTabUsageScenarioTracker> tab_usage_scenario_tracker_;
  ukm::TestAutoSetUkmRecorder ukm_recorder_;
};

}  // namespace

TEST_F(TabUsageScenarioTrackerTest, NewVisibleTabMeansOneVisibleWindow) {
  auto contents = CreateWebContents();
  tab_usage_scenario_tracker_->OnTabAdded(contents.get());

  // Only one WebContent was shown which means only one visible window.
  UsageScenarioDataStore::IntervalData interval_data =
      usage_scenario_data_store_.GetIntervalDataForTesting();
  EXPECT_EQ(interval_data.max_visible_window_count, 1);
}

TEST_F(TabUsageScenarioTrackerTest, VisibilityUpdateOnVisibleWindowIsNoop) {
  auto contents = CreateWebContents();
  tab_usage_scenario_tracker_->OnTabAdded(contents.get());

  // Only one WebContent was shown which means only one visible window.
  // The call to OnVisibilityChanged should not create a visible window count
  // higher than the number of windows.
  UsageScenarioDataStore::IntervalData interval_data =
      usage_scenario_data_store_.GetIntervalDataForTesting();
  EXPECT_EQ(interval_data.max_visible_window_count, 1);
}

TEST_F(TabUsageScenarioTrackerTest, HidingWebContentsMakesWindowInvisible) {
  // WebContents starts out visible.
  auto contents = CreateWebContents();
  tab_usage_scenario_tracker_->OnTabAdded(contents.get());

  // WebContents is hidden.
  MakeTabHidden(contents.get());

  // Grab the interval data.
  UsageScenarioDataStore::IntervalData interval_data =
      usage_scenario_data_store_.ResetIntervalData();

  // One WebContents was shown for part of the interval so one visible window.
  EXPECT_EQ(interval_data.max_visible_window_count, 1);

  // End a new interval, no WebContents was shown for the duration so no visible
  // window.
  interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(interval_data.max_visible_window_count, 0);
}

TEST_F(TabUsageScenarioTrackerTest, TrackingOfVisibleWebContents) {
  // Start with 2 hidden WebContents.
  auto contents1 = CreateWebContents();
  contents1->WasHidden();
  auto contents2 = CreateWebContents();
  contents2->WasHidden();
  tab_usage_scenario_tracker_->OnTabAdded(contents1.get());
  tab_usage_scenario_tracker_->OnTabAdded(contents2.get());
  EXPECT_EQ(usage_scenario_data_store_.current_tab_count_for_testing(), 2);
  EXPECT_EQ(
      usage_scenario_data_store_.current_visible_window_count_for_testing(), 0);

  // Make one contents visible.
  MakeTabVisible(contents1.get());
  EXPECT_EQ(usage_scenario_data_store_.current_tab_count_for_testing(), 2);
  EXPECT_EQ(
      usage_scenario_data_store_.current_visible_window_count_for_testing(), 1);

  // |contents1| is visible, removing it should update the number of currently
  // visible windows.
  tab_usage_scenario_tracker_->OnTabRemoved(contents1.get());
  EXPECT_EQ(usage_scenario_data_store_.current_tab_count_for_testing(), 1);
  EXPECT_EQ(
      usage_scenario_data_store_.current_visible_window_count_for_testing(), 0);

  tab_usage_scenario_tracker_->OnTabRemoved(contents2.get());
  EXPECT_EQ(usage_scenario_data_store_.current_tab_count_for_testing(), 0);
  EXPECT_EQ(
      usage_scenario_data_store_.current_visible_window_count_for_testing(), 0);
}

TEST_F(TabUsageScenarioTrackerTest, TrackingOfOccludedWebContents) {
  // Start with 2 hidden WebContents.
  auto contents1 = CreateWebContents();
  contents1->WasHidden();
  auto contents2 = CreateWebContents();
  contents2->WasHidden();
  tab_usage_scenario_tracker_->OnTabAdded(contents1.get());
  tab_usage_scenario_tracker_->OnTabAdded(contents2.get());
  EXPECT_EQ(usage_scenario_data_store_.current_tab_count_for_testing(), 2);
  EXPECT_EQ(
      usage_scenario_data_store_.current_visible_window_count_for_testing(), 0);

  // Make one contents occluded.
  MakeTabOccluded(contents1.get());
  EXPECT_EQ(usage_scenario_data_store_.current_tab_count_for_testing(), 2);
  EXPECT_EQ(
      usage_scenario_data_store_.current_visible_window_count_for_testing(), 0);

  // Make one content visible.
  MakeTabVisible(contents2.get());
  EXPECT_EQ(usage_scenario_data_store_.current_tab_count_for_testing(), 2);
  EXPECT_EQ(
      usage_scenario_data_store_.current_visible_window_count_for_testing(), 1);

  // Then make it occluded.
  MakeTabOccluded(contents2.get());
  EXPECT_EQ(usage_scenario_data_store_.current_tab_count_for_testing(), 2);
  EXPECT_EQ(
      usage_scenario_data_store_.current_visible_window_count_for_testing(), 0);
}

TEST_F(TabUsageScenarioTrackerTest, FullScreenVideoSingleMonitor) {
  // WebContents starts out visible.
  auto contents = CreateWebContents();
  tab_usage_scenario_tracker_->OnTabAdded(contents.get());

  // WebContents is playing video fullscreen.
  tab_usage_scenario_tracker_->OnMediaEffectivelyFullscreenChanged(
      contents.get(), true);

  task_environment()->FastForwardBy(kInterval);

  // Grab the interval data.
  UsageScenarioDataStore::IntervalData interval_data =
      usage_scenario_data_store_.ResetIntervalData();

  // Ensure that the time playing a video fullscreen is properly recorded.
  EXPECT_EQ(interval_data.time_playing_video_full_screen_single_monitor,
            kInterval);

  // Add a second display, this should stop the fullscreen video on single
  // monitor session.
  task_environment()->FastForwardBy(kInterval);
  tab_usage_scenario_tracker_->SetNumDisplays(2);
  task_environment()->FastForwardBy(kInterval);

  interval_data = usage_scenario_data_store_.ResetIntervalData();

  // Ensure that the time playing a video fullscreen is properly recorded.
  EXPECT_EQ(interval_data.time_playing_video_full_screen_single_monitor,
            kInterval);

  task_environment()->FastForwardBy(kInterval);
  interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_TRUE(
      interval_data.time_playing_video_full_screen_single_monitor.is_zero());

  // Removing the secondary display while there's still some video playing
  // fullscreen should resume the session.
  tab_usage_scenario_tracker_->SetNumDisplays(1);
  task_environment()->FastForwardBy(kInterval);
  interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(interval_data.time_playing_video_full_screen_single_monitor,
            kInterval);
}

// Regression test for crbug.com/1273251.
TEST_F(TabUsageScenarioTrackerTest,
       FullScreenVideoSingleMonitor_StopPlayingWithTwoMonitors) {
  auto contents = CreateWebContents();
  tab_usage_scenario_tracker_->OnTabAdded(contents.get());

  // `contents` plays video in fullscreen.
  tab_usage_scenario_tracker_->OnMediaEffectivelyFullscreenChanged(
      contents.get(), true);
  task_environment()->FastForwardBy(kInterval);

  // Add a second display, this should stop the fullscreen video on single
  // monitor session.
  tab_usage_scenario_tracker_->SetNumDisplays(2);
  task_environment()->FastForwardBy(kInterval);

  // Stop playing video in fullscreen in `contents` while there are 2 displays.
  tab_usage_scenario_tracker_->OnMediaEffectivelyFullscreenChanged(
      contents.get(), false);
  task_environment()->FastForwardBy(kInterval);

  // Remove the second display.
  tab_usage_scenario_tracker_->SetNumDisplays(1);
  task_environment()->FastForwardBy(kInterval);

  // `contents2` starts playing video in fullscreen.
  auto contents2 = CreateWebContents();
  tab_usage_scenario_tracker_->OnTabAdded(contents2.get());

  tab_usage_scenario_tracker_->OnMediaEffectivelyFullscreenChanged(
      contents2.get(), true);
  task_environment()->FastForwardBy(kInterval);

  // Expect 2 * kInterval of video playback (kInterval for each WebContents).
  auto interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(interval_data.time_playing_video_full_screen_single_monitor,
            2 * kInterval);
}

// Regression test for crbug.com/341488142.
TEST_F(TabUsageScenarioTrackerTest,
       FullScreenVideoSingleMonitor_NumDisplaysChangeWithoutNotification) {
  auto contents = CreateWebContents();
  tab_usage_scenario_tracker_->OnTabAdded(contents.get());

  // Add a second display.
  tab_usage_scenario_tracker_->SetNumDisplays(2);

  // `contents` plays video in fullscreen. There are 2 displays so time isn't
  // accumulated in `time_playing_video_full_screen_single_monitor`.
  tab_usage_scenario_tracker_->OnMediaEffectivelyFullscreenChanged(
      contents.get(), true);
  task_environment()->FastForwardBy(kInterval);

  // Remove the 2nd display without dispatching a notification yet, to mimic
  // `XDisplayManager`'s delayed notification
  // (https://source.chromium.org/chromium/chromium/src/+/main:ui/base/x/x11_display_manager.cc;l=130-135;drc=90cac1911508d3d682a67c97aa62483eb712f69a).
  tab_usage_scenario_tracker_->SetNumDisplaysWithoutNotification(1);
  task_environment()->FastForwardBy(kInterval);

  // Stop playing video in fullscreen in `contents` while there is 1 display,
  // but the display removal notification hasn't been dispatched yet. This
  // shouldn't crash.
  tab_usage_scenario_tracker_->OnMediaEffectivelyFullscreenChanged(
      contents.get(), false);

  // Expect no time playing video in fullscreen, since the display removal
  // notification was never dispatched.
  auto interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(interval_data.time_playing_video_full_screen_single_monitor,
            base::TimeDelta());
}

TEST_F(TabUsageScenarioTrackerTest, VideoInVisibleTab) {
  // Create 2 tabs, one visible and one hidden.
  auto contents1 = CreateWebContents();
  auto contents2 = CreateWebContents();
  contents2->WasHidden();
  tab_usage_scenario_tracker_->OnTabAdded(contents1.get());
  tab_usage_scenario_tracker_->OnTabAdded(contents2.get());

  // Pretend that |content1| is playing a video while being visible.
  tab_usage_scenario_tracker_->OnVideoStartedPlaying(contents1.get());

  task_environment()->FastForwardBy(kInterval);

  // Grab the interval data and ensure that the time playing a video in the
  // visible tab is properly recorded.
  UsageScenarioDataStore::IntervalData interval_data =
      usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(interval_data.time_playing_video_in_visible_tab, kInterval);

  // Start a video in the hidden tab, while another one is still playing in the
  // visible tab, this shouldn't change anything.
  tab_usage_scenario_tracker_->OnVideoStartedPlaying(contents2.get());
  task_environment()->FastForwardBy(kInterval);
  interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(interval_data.time_playing_video_in_visible_tab, kInterval);

  // Stop the video playing in the visible tab.
  tab_usage_scenario_tracker_->OnVideoStoppedPlaying(contents1.get());
  task_environment()->FastForwardBy(kInterval);
  interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_TRUE(interval_data.time_playing_video_in_visible_tab.is_zero());

  // There's still a video playing in the second tab, make it visible and ensure
  // that things are reported properly.
  contents2->WasShown();
  tab_usage_scenario_tracker_->OnTabVisibilityChanged(contents2.get());
  task_environment()->FastForwardBy(kInterval);
  interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(interval_data.time_playing_video_in_visible_tab, kInterval);

  // Mark the tab as playing video for only half of the interval.
  task_environment()->FastForwardBy(kInterval);
  tab_usage_scenario_tracker_->OnVideoStoppedPlaying(contents2.get());
  task_environment()->FastForwardBy(kInterval);
  interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(interval_data.time_playing_video_in_visible_tab, kInterval);
}

TEST_F(TabUsageScenarioTrackerTest, VisibleTabPlayingVideoRemoved) {
  auto contents1 = CreateWebContents();
  tab_usage_scenario_tracker_->OnTabAdded(contents1.get());

  // Pretend that |content1| is playing a video while being visible.
  tab_usage_scenario_tracker_->OnVideoStartedPlaying(contents1.get());
  task_environment()->FastForwardBy(kInterval);
  UsageScenarioDataStore::IntervalData interval_data =
      usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(kInterval, interval_data.time_playing_video_in_visible_tab);

  tab_usage_scenario_tracker_->OnTabRemoved(contents1.get());
  task_environment()->FastForwardBy(kInterval);
  interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_TRUE(interval_data.time_playing_video_in_visible_tab.is_zero());
}

TEST_F(TabUsageScenarioTrackerTest, TabPlayingAudio) {
  // Create a tab and add it.
  auto web_contents = CreateWebContents();
  tab_usage_scenario_tracker_->OnTabAdded(web_contents.get());

  // Pretend that web_contents is playing audio.
  content::WebContentsTester::For(web_contents.get())
      ->SetIsCurrentlyAudible(true);
  tab_usage_scenario_tracker_->OnTabIsAudibleChanged(web_contents.get());

  task_environment()->FastForwardBy(kInterval);

  // Grab the interval data and ensure that the time playing a video in the
  // visible tab is properly recorded.
  UsageScenarioDataStore::IntervalData interval_data =
      usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(interval_data.time_playing_audio, kInterval);
}

TEST_F(TabUsageScenarioTrackerTest, UKMVisibility1tab) {
  const GURL kUrl1("https://foo.com/subfoo");
  const GURL kUrl2("https://bar.com/subbar");

  // Test case with only one tab with a navigation that has already been
  // committed when it starts being tracked.
  auto contents1 = CreateWebContents();
  EXPECT_EQ(content::Visibility::VISIBLE, contents1->GetVisibility());
  content::NavigationSimulator::NavigateAndCommitFromBrowser(contents1.get(),
                                                             GURL(kUrl1));
  auto source_id_1 = contents1->GetPrimaryMainFrame()->GetPageUkmSourceId();
  EXPECT_NE(ukm::kInvalidSourceId, source_id_1);

  tab_usage_scenario_tracker_->OnTabAdded(contents1.get());
  task_environment()->FastForwardBy(kInterval);
  auto interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(source_id_1, interval_data.source_id_for_longest_visible_origin);
  EXPECT_EQ(kInterval,
            interval_data.source_id_for_longest_visible_origin_duration);
  EXPECT_EQ(1U,
            usage_scenario_data_store_.GetVisibleSourceIdsForTesting().size());

  task_environment()->FastForwardBy(kInterval);
  interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(source_id_1, interval_data.source_id_for_longest_visible_origin);
  EXPECT_EQ(kInterval,
            interval_data.source_id_for_longest_visible_origin_duration);
  EXPECT_EQ(1U,
            usage_scenario_data_store_.GetVisibleSourceIdsForTesting().size());

  MakeTabHidden(contents1.get());
  task_environment()->FastForwardBy(kInterval);
  interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(ukm::kInvalidSourceId,
            interval_data.source_id_for_longest_visible_origin);
  EXPECT_EQ(0U,
            usage_scenario_data_store_.GetVisibleSourceIdsForTesting().size());

  // Reloading the tab while it's not visible shouldn't change anything.
  content::NavigationSimulator::Reload(contents1.get());
  task_environment()->FastForwardBy(kInterval);
  interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(ukm::kInvalidSourceId,
            interval_data.source_id_for_longest_visible_origin);
  EXPECT_EQ(0U,
            usage_scenario_data_store_.GetVisibleSourceIdsForTesting().size());

  // Make the tab visible and navigate to a different URL.
  MakeTabVisible(contents1.get());
  NavigateAndCommitTab(contents1.get(), kUrl2);
  auto source_id_2 = contents1->GetPrimaryMainFrame()->GetPageUkmSourceId();
  EXPECT_NE(source_id_1, source_id_2);
  task_environment()->FastForwardBy(kInterval);
  interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(source_id_2, interval_data.source_id_for_longest_visible_origin);
  EXPECT_EQ(kInterval,
            interval_data.source_id_for_longest_visible_origin_duration);
  EXPECT_EQ(1U,
            usage_scenario_data_store_.GetVisibleSourceIdsForTesting().size());

  // Reloading the tab while it's visible shouldn't change anything.
  content::NavigationSimulator::Reload(contents1.get());
  task_environment()->FastForwardBy(kInterval);
  interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(source_id_2, interval_data.source_id_for_longest_visible_origin);
  EXPECT_EQ(kInterval,
            interval_data.source_id_for_longest_visible_origin_duration);
  EXPECT_EQ(1U,
            usage_scenario_data_store_.GetVisibleSourceIdsForTesting().size());
}

TEST_F(TabUsageScenarioTrackerTest, UKMVisibility1tabLateNavigation) {
  const GURL kUrl1("https://foo.com/subfoo");
  // Test case with only one tab with a navigation that gets committed after
  // starting to track the tab.
  auto contents1 = CreateWebContents();
  EXPECT_EQ(content::Visibility::VISIBLE, contents1->GetVisibility());
  tab_usage_scenario_tracker_->OnTabAdded(contents1.get());

  task_environment()->FastForwardBy(kInterval);
  auto interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(ukm::kInvalidSourceId,
            interval_data.source_id_for_longest_visible_origin);
  EXPECT_TRUE(
      interval_data.source_id_for_longest_visible_origin_duration.is_zero());
  EXPECT_EQ(0U,
            usage_scenario_data_store_.GetVisibleSourceIdsForTesting().size());

  NavigateAndCommitTab(contents1.get(), kUrl1);
  auto source_id_1 = contents1->GetPrimaryMainFrame()->GetPageUkmSourceId();
  EXPECT_NE(ukm::kInvalidSourceId, source_id_1);

  task_environment()->FastForwardBy(kInterval);
  interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(source_id_1, interval_data.source_id_for_longest_visible_origin);
  EXPECT_EQ(kInterval,
            interval_data.source_id_for_longest_visible_origin_duration);
  EXPECT_EQ(1U,
            usage_scenario_data_store_.GetVisibleSourceIdsForTesting().size());
}

TEST_F(TabUsageScenarioTrackerTest, UKMVisibilityMultipleTabs) {
  const GURL kUrl1("https://foo.com/subfoo");
  const GURL kUrl2("https://bar.com/subbar");
  const GURL kUrl3("https://foo.com/foo2");

  // Create 3 tabs: one is visible, one is hidden and one is occluded.
  auto contents1 = CreateWebContents();
  auto contents2 = CreateWebContents();
  auto contents3 = CreateWebContents();
  tab_usage_scenario_tracker_->OnTabAdded(contents1.get());
  tab_usage_scenario_tracker_->OnTabAdded(contents2.get());
  tab_usage_scenario_tracker_->OnTabAdded(contents3.get());
  EXPECT_EQ(content::Visibility::VISIBLE, contents1->GetVisibility());
  MakeTabHidden(contents2.get());
  MakeTabOccluded(contents3.get());

  NavigateAndCommitTab(contents1.get(), kUrl1);
  NavigateAndCommitTab(contents2.get(), kUrl2);
  NavigateAndCommitTab(contents3.get(), kUrl3);

  task_environment()->FastForwardBy(kInterval);
  auto interval_data = usage_scenario_data_store_.ResetIntervalData();
  auto source_id_1 = contents1->GetPrimaryMainFrame()->GetPageUkmSourceId();
  EXPECT_EQ(source_id_1, interval_data.source_id_for_longest_visible_origin);
  EXPECT_EQ(kInterval,
            interval_data.source_id_for_longest_visible_origin_duration);
  EXPECT_EQ(1U,
            usage_scenario_data_store_.GetVisibleSourceIdsForTesting().size());

  // Make a second tab visible and make the hidden one occluded.
  task_environment()->FastForwardBy(kInterval);
  MakeTabVisible(contents2.get());
  MakeTabHidden(contents3.get());
  task_environment()->FastForwardBy(kInterval);
  interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(source_id_1, interval_data.source_id_for_longest_visible_origin);
  EXPECT_EQ(2 * kInterval,
            interval_data.source_id_for_longest_visible_origin_duration);
  EXPECT_EQ(2U,
            usage_scenario_data_store_.GetVisibleSourceIdsForTesting().size());

  // Mark the first tab as hidden.
  MakeTabOccluded(contents1.get());
  task_environment()->FastForwardBy(kInterval);
  interval_data = usage_scenario_data_store_.ResetIntervalData();
  auto source_id_2 = contents2->GetPrimaryMainFrame()->GetPageUkmSourceId();
  EXPECT_EQ(source_id_2, interval_data.source_id_for_longest_visible_origin);
  EXPECT_EQ(kInterval,
            interval_data.source_id_for_longest_visible_origin_duration);
  EXPECT_EQ(1U,
            usage_scenario_data_store_.GetVisibleSourceIdsForTesting().size());

  // Finally, mark the third tab as visible.
  MakeTabHidden(contents2.get());
  MakeTabVisible(contents3.get());
  task_environment()->FastForwardBy(kInterval);
  interval_data = usage_scenario_data_store_.ResetIntervalData();
  auto source_id_3 = contents3->GetPrimaryMainFrame()->GetPageUkmSourceId();
  EXPECT_EQ(source_id_3, interval_data.source_id_for_longest_visible_origin);
  EXPECT_EQ(kInterval,
            interval_data.source_id_for_longest_visible_origin_duration);
  EXPECT_EQ(1U,
            usage_scenario_data_store_.GetVisibleSourceIdsForTesting().size());
}

TEST_F(TabUsageScenarioTrackerTest, UKMVisibilityMultipleVisibilityEvents) {
  const GURL kUrl1("https://foo.com/subfoo");

  auto contents1 = CreateWebContents();
  EXPECT_EQ(content::Visibility::VISIBLE, contents1->GetVisibility());
  content::NavigationSimulator::NavigateAndCommitFromBrowser(contents1.get(),
                                                             GURL(kUrl1));
  auto source_id_1 = contents1->GetPrimaryMainFrame()->GetPageUkmSourceId();
  EXPECT_NE(ukm::kInvalidSourceId, source_id_1);
  tab_usage_scenario_tracker_->OnTabAdded(contents1.get());

  task_environment()->FastForwardBy(kInterval);
  MakeTabHidden(contents1.get());
  task_environment()->FastForwardBy(kInterval);
  MakeTabVisible(contents1.get());
  task_environment()->FastForwardBy(kInterval);
  MakeTabHidden(contents1.get());
  task_environment()->FastForwardBy(kInterval);

  auto interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(source_id_1, interval_data.source_id_for_longest_visible_origin);
  EXPECT_EQ(2 * kInterval,
            interval_data.source_id_for_longest_visible_origin_duration);
}

TEST_F(TabUsageScenarioTrackerTest,
       UKMVisibilityMultipleVisibleTabsSameOrigin) {
  const GURL kUrl1("https://foo.com/subfoo");
  const GURL kUrl2("https://bar.com/subbar");
  const GURL kUrl3("https://foo.com/foo2");

  // Create 3 tabs: one is visible, one is hidden and one is occluded.
  auto contents1 = CreateWebContents();
  auto contents2 = CreateWebContents();
  auto contents3 = CreateWebContents();
  tab_usage_scenario_tracker_->OnTabAdded(contents1.get());
  tab_usage_scenario_tracker_->OnTabAdded(contents2.get());
  tab_usage_scenario_tracker_->OnTabAdded(contents3.get());
  EXPECT_EQ(content::Visibility::VISIBLE, contents1->GetVisibility());
  EXPECT_EQ(content::Visibility::VISIBLE, contents2->GetVisibility());
  EXPECT_EQ(content::Visibility::VISIBLE, contents3->GetVisibility());

  NavigateAndCommitTab(contents1.get(), kUrl1);
  NavigateAndCommitTab(contents2.get(), kUrl2);
  NavigateAndCommitTab(contents3.get(), kUrl3);

  auto source_id_1 = contents1->GetPrimaryMainFrame()->GetPageUkmSourceId();
  auto source_id_2 = contents2->GetPrimaryMainFrame()->GetPageUkmSourceId();
  auto source_id_3 = contents3->GetPrimaryMainFrame()->GetPageUkmSourceId();
  EXPECT_NE(source_id_1, source_id_2);
  EXPECT_NE(source_id_1, source_id_3);
  EXPECT_NE(source_id_2, source_id_3);

  task_environment()->FastForwardBy(kInterval * 2);
  MakeTabHidden(contents1.get());
  task_environment()->FastForwardBy(kInterval);
  MakeTabHidden(contents3.get());
  task_environment()->FastForwardBy(kInterval);
  MakeTabHidden(contents2.get());

  // contents1 visible for 2*kInterval
  // contents2 visible for 4*kInterval
  // contents3 visible for 3*kInterval
  // The origin foo.com was visible for the longest time (5*kInterval).
  // source_id_3 is the source id that was visible for the longest time for
  // origin foo.com. Therefore, expect:
  //     source_id_for_longest_visible_origin = source_id_3
  //     source_id_for_longest_visible_origin_duration = 3*kInterval

  auto interval_data = usage_scenario_data_store_.ResetIntervalData();
  EXPECT_EQ(source_id_3, interval_data.source_id_for_longest_visible_origin);
  EXPECT_EQ(3 * kInterval,
            interval_data.source_id_for_longest_visible_origin_duration);
}

}  // namespace metrics