File: tab_hover_card_controller_interactive_uitest.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 (882 lines) | stat: -rw-r--r-- 36,760 bytes parent folder | download | duplicates (3)
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>

#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/interstitials/security_interstitial_page_test_utils.h"
#include "chrome/browser/ui/browser_command_controller.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/performance_controls/memory_saver_utils.h"
#include "chrome/browser/ui/performance_controls/tab_resource_usage_tab_helper.h"
#include "chrome/browser/ui/performance_controls/test_support/memory_metrics_refresh_waiter.h"
#include "chrome/browser/ui/performance_controls/test_support/memory_saver_interactive_test_mixin.h"
#include "chrome/browser/ui/tabs/alert/tab_alert.h"
#include "chrome/browser/ui/tabs/saved_tab_groups/collaboration_messaging_tab_data.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/tabs/fade_footer_view.h"
#include "chrome/browser/ui/views/tabs/fade_label_view.h"
#include "chrome/browser/ui/views/tabs/tab.h"
#include "chrome/browser/ui/views/tabs/tab_close_button.h"
#include "chrome/browser/ui/views/tabs/tab_hover_card_bubble_view.h"
#include "chrome/browser/ui/views/tabs/tab_hover_card_controller.h"
#include "chrome/browser/ui/views/tabs/tab_hover_card_test_util.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "chrome/browser/ui/views/test/tab_strip_interactive_test_mixin.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chrome/test/interaction/interactive_browser_test.h"
#include "components/collaboration/public/messaging/message.h"
#include "components/data_sharing/public/group_data.h"
#include "components/lookalikes/core/safety_tip_test_utils.h"
#include "components/performance_manager/public/decorators/process_metrics_decorator.h"
#include "components/performance_manager/public/performance_manager.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_features.h"
#include "content/public/test/browser_test.h"
#include "net/base/url_util.h"
#include "net/dns/mock_host_resolver.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/image_model.h"
#include "ui/base/models/list_selection_model.h"
#include "ui/base/test/ui_controls.h"
#include "ui/base/text/bytes_formatting.h"
#include "ui/display/display.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/geometry/point.h"
#include "ui/views/controls/label.h"
#include "ui/views/test/widget_activation_waiter.h"
#include "ui/views/test/widget_test.h"
#include "url/gurl.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ash/system_web_apps/test_support/test_system_web_app_installation.h"
#endif

namespace {
constexpr char16_t kTabTitle[] = u"Test Tab 2";
constexpr char16_t kTabDomain[] = u"example.com";
constexpr char kTabUrl[] = "http://example.com/path/to/document.html";

DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kFirstTabContents);
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kSecondTabContents);

TabRendererData MakeTabRendererData() {
  TabRendererData new_tab_data = TabRendererData();
  new_tab_data.title = kTabTitle;
  new_tab_data.last_committed_url = GURL(kTabUrl);
  new_tab_data.alert_state = {tabs::TabAlert::AUDIO_PLAYING};
  return new_tab_data;
}

collaboration::messaging::PersistentMessage CreateMessage(
    std::string given_name,
    std::string avatar_url,
    collaboration::messaging::CollaborationEvent event) {
  data_sharing::GroupMember member;
  member.given_name = given_name;
  member.avatar_url = GURL(avatar_url);

  collaboration::messaging::MessageAttribution attribution;
  attribution.triggering_user = member;

  collaboration::messaging::PersistentMessage message;
  message.type = collaboration::messaging::PersistentNotificationType::CHIP;
  message.attribution = attribution;
  message.collaboration_event = event;

  return message;
}

}  // namespace

class TabHoverCardInteractiveUiTest
    : public TabStripInteractiveTestMixin<
          MemorySaverInteractiveTestMixin<InteractiveBrowserTest>>,
      public test::TabHoverCardTestUtil {
 public:
  ~TabHoverCardInteractiveUiTest() override = default;

  void SetUp() override {
    set_open_about_blank_on_browser_launch(true);
    scoped_feature_list_.InitAndEnableFeature(features::kTabHoverCardImages);
    MemorySaverInteractiveTestMixin::SetUp();
  }

  // Start the test by moving the mouse to a location where it will not be
  // hovering the tabstrip. All subsequent interactions will be simulated.
  void SetUpOnMainThread() override {
    MemorySaverInteractiveTestMixin::SetUpOnMainThread();
    Tab::SetShowHoverCardOnMouseHoverForTesting(true);
    base::RunLoop run_loop(base::RunLoop::Type::kNestableTasksAllowed);
    gfx::Point upper_left;
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
    // Because Ozone makes it impossible to target a point not in a window in
    // tests, instead target the extreme upper left of the browser window.
    upper_left = browser()->window()->GetBounds().origin();
#endif
    ui_controls::SendMouseMoveNotifyWhenDone(upper_left.x(), upper_left.y(),
                                             run_loop.QuitClosure());
    run_loop.Run();
  }

  void TearDownOnMainThread() override {
    MemorySaverInteractiveTestMixin::TearDownOnMainThread();
  }

  auto UnhoverTab() {
    return Steps(MoveMouseTo(kNewTabButtonElementId));
  }

  StepBuilder CheckHovercardIsOpen() {
    return WaitForShow(TabHoverCardBubbleView::kHoverCardBubbleElementId);
  }

  StepBuilder CheckHovercardIsClosed() {
    return WaitForHide(TabHoverCardBubbleView::kHoverCardBubbleElementId);
  }

  TabResourceUsageTabHelper* GetResourceUsageAt(int index) {
    return browser()
        ->tab_strip_model()
        ->GetTabAtIndex(index)
        ->GetTabFeatures()
        ->resource_usage_helper();
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

#if defined(USE_AURA)

// Verify that the hover card is not visible when any key is pressed.
// Because this test depends on Aura event handling, it is not performed on Mac.
IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       HoverCardHidesOnAnyKeyPressInSameWindow) {
  RunTestSequence(
      InstrumentTab(kFirstTabContents, 0),
      NavigateWebContents(kFirstTabContents, GURL(chrome::kChromeUINewTabURL)),
      HoverTabAt(0), CheckHovercardIsOpen(),
      Check(base::BindLambdaForTesting([=, this]() {
        return ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_DOWN, false,
                                               false, false, false);
      })),
      CheckHovercardIsClosed());
}

#endif

IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       HoverCardHidesOnMouseExit) {
  RunTestSequence(
      InstrumentTab(kFirstTabContents, 0),
      NavigateWebContents(kFirstTabContents, GURL(chrome::kChromeUINewTabURL)),
      HoverTabAt(0), CheckHovercardIsOpen(), UnhoverTab(),
      CheckHovercardIsClosed());
}

IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       HoverCardShownOnTabFocus) {
  TabStrip* const tab_strip = GetTabStrip(browser());
  Tab* const tab = tab_strip->tab_at(0);
  tab_strip->GetFocusManager()->SetFocusedView(tab);
  WaitForHoverCardVisible(tab_strip);
}

IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       HoverCardVisibleOnTabCloseButtonFocusAfterTabFocus) {
  TabStrip* const tab_strip = GetTabStrip(browser());
  Tab* const tab = tab_strip->tab_at(0);
  tab_strip->GetFocusManager()->SetFocusedView(tab);
  WaitForHoverCardVisible(tab_strip);
  tab_strip->GetFocusManager()->SetFocusedView(tab->close_button_);
  EXPECT_TRUE(IsHoverCardVisible(tab_strip));
}

// Verify hover card is visible when tab is focused and a key is pressed.
IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       WidgetVisibleOnKeyPressAfterTabFocus) {
  TabStrip* const tab_strip = GetTabStrip(browser());
  Tab* const tab = tab_strip->tab_at(0);
  tab_strip->GetFocusManager()->SetFocusedView(tab);
  WaitForHoverCardVisible(tab_strip);

  ui::KeyEvent key_event(ui::EventType::kKeyPressed, ui::VKEY_SPACE, 0);
  tab->OnKeyPressed(key_event);
  EXPECT_TRUE(IsHoverCardVisible(tab_strip));
}

// Verify hover card thumbnail is not visible on active tabs.
IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       ThumbnailNotVisibileOnActiveTabs) {
  TabStrip* const tab_strip = GetTabStrip(browser());
  Tab* const tab = tab_strip->tab_at(0);
  tab_strip->GetFocusManager()->SetFocusedView(tab);
  EXPECT_TRUE(tab->IsActive());
  WaitForHoverCardVisible(tab_strip);
  views::View* const thumbnail_view_ =
      GetHoverCard(tab_strip)->GetThumbnailViewForTesting();
  CHECK(thumbnail_view_);
  EXPECT_FALSE(thumbnail_view_->GetVisible());
}

// Verify hover card is not visible when tab is focused and the mouse is
// pressed.
IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       WidgetNotVisibleOnMousePressAfterTabFocus) {
  TabStrip* const tab_strip = GetTabStrip(browser());
  Tab* const tab = tab_strip->tab_at(0);
  tab_strip->GetFocusManager()->SetFocusedView(tab);
  WaitForHoverCardVisible(tab_strip);

  ui::MouseEvent click_event(ui::EventType::kMousePressed, gfx::Point(),
                             gfx::Point(), base::TimeTicks(), ui::EF_NONE, 0);
  tab->OnMousePressed(click_event);
  EXPECT_FALSE(IsHoverCardVisible(tab_strip));
}

IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       WidgetNotVisibleOnMousePressAfterHover) {
  RunTestSequence(
      InstrumentTab(kFirstTabContents, 0),
      NavigateWebContents(kFirstTabContents, GURL(chrome::kChromeUINewTabURL)),
      HoverTabAt(0), CheckHovercardIsOpen(), SelectTab(kTabStripElementId, 0),
      CheckHovercardIsClosed());
}

IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       HoverCardVisibleOnTabFocusFromKeyboardAccelerator) {
  TabStrip* const tab_strip = GetTabStrip(browser());

  ASSERT_TRUE(
      AddTabAtIndex(1, GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_TYPED));

  // Cycle focus until it reaches a tab.
  while (!tab_strip->IsFocusInTabs()) {
    browser()->command_controller()->ExecuteCommand(IDC_FOCUS_NEXT_PANE);
  }

  WaitForHoverCardVisible(tab_strip);

  // Move focus forward to the close button or next tab dependent on window
  // size.
  tab_strip->AcceleratorPressed(ui::Accelerator(ui::VKEY_RIGHT, ui::EF_NONE));
  EXPECT_TRUE(IsHoverCardVisible(tab_strip));
}

IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       InactiveWindowStaysInactiveOnHover) {
  resource_coordinator::GetTabLifecycleUnitSource()
      ->SetFocusedTabStripModelForTesting(nullptr);
  const BrowserList* active_browser_list = BrowserList::GetInstance();

  // Open a second browser window.
  ui_test_utils::NavigateToURLWithDisposition(
      browser(), GURL(url::kAboutBlankURL), WindowOpenDisposition::NEW_WINDOW,
      ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
  ASSERT_EQ(2u, active_browser_list->size());

  // Choose one browser to be active; the other to be inactive.
  Browser* active_window = active_browser_list->get(0);
  Browser* inactive_window = active_browser_list->get(1);

  // Activate the active browser and wait for the inactive browser to be
  // inactive.
  BrowserView::GetBrowserViewForBrowser(active_window)->Activate();
  views::test::WaitForWidgetActive(
      BrowserView::GetBrowserViewForBrowser(inactive_window)->frame(), false);
  ASSERT_FALSE(
      BrowserView::GetBrowserViewForBrowser(inactive_window)->IsActive());

  // Simulate hovering the inactive tabstrip and wait for the hover card to
  // appear. The inactive browser should remain inactive.
  SimulateHoverTab(inactive_window, 0);
  ASSERT_FALSE(
      BrowserView::GetBrowserViewForBrowser(inactive_window)->IsActive());
}

IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       UpdatesHoverCardOnHoverDifferentTab) {
  TabStrip* const tab_strip = GetTabStrip(browser());
  ASSERT_TRUE(
      AddTabAtIndex(1, GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_TYPED));
  tab_strip->SetTabData(1, MakeTabRendererData());

  SimulateHoverTab(browser(), 0);

  auto* const hover_card = SimulateHoverTab(browser(), 1);
  EXPECT_EQ(kTabTitle, hover_card->GetTitleTextForTesting());
  EXPECT_EQ(kTabDomain, hover_card->GetDomainTextForTesting());
  EXPECT_EQ(tab_strip->tab_at(1), hover_card->GetAnchorView());
}

IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       HoverCardDoesNotHaveFooterView) {
  TabStrip* const tab_strip = GetTabStrip(browser());
  ASSERT_TRUE(
      AddTabAtIndex(1, GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_TYPED));
  TabRendererData tab_data = TabRendererData();
  tab_data.title = kTabTitle;
  tab_data.last_committed_url = GURL(kTabUrl);
  tab_strip->SetTabData(1, tab_data);

  auto* const hover_card = SimulateHoverTab(browser(), 1);
  EXPECT_FALSE(hover_card->GetFooterViewForTesting()->GetVisible());
}

using TabHoverCardBubbleViewMetricsTest = TabHoverCardInteractiveUiTest;

IN_PROC_BROWSER_TEST_F(TabHoverCardBubbleViewMetricsTest,
                       HoverCardsSeenRatioMetric) {
  TabStrip* const tab_strip = GetTabStrip(browser());
  ASSERT_TRUE(
      AddTabAtIndex(1, GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_TYPED));
  ASSERT_TRUE(
      AddTabAtIndex(2, GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_TYPED));

  SimulateHoverTab(browser(), 0);

  EXPECT_EQ(1, GetHoverCardsSeenCount(browser()));

  SimulateHoverTab(browser(), 1);

  EXPECT_EQ(2, GetHoverCardsSeenCount(browser()));

  ui::ListSelectionModel selection;
  selection.SetSelectedIndex(1);
  tab_strip->SetSelection(selection);

  TabHoverCardBubbleView* const hover_card = GetHoverCard(tab_strip);
  EXPECT_FALSE(hover_card && hover_card->GetWidget()->IsVisible());
  EXPECT_EQ(0, GetHoverCardsSeenCount(browser()));
}

// Tests for tabs showing interstitials to check whether the URL in the hover
// card is displayed or hidden as appropriate.
class TabHoverCardBubbleViewInterstitialBrowserTest
    : public TabHoverCardInteractiveUiTest {
 public:
  ~TabHoverCardBubbleViewInterstitialBrowserTest() override = default;

  void SetUpOnMainThread() override {
    host_resolver()->AddRule("*", "127.0.0.1");

    https_server_mismatched_ = std::make_unique<net::EmbeddedTestServer>(
        net::EmbeddedTestServer::TYPE_HTTPS);
    https_server_mismatched_->SetSSLConfig(
        net::EmbeddedTestServer::CERT_MISMATCHED_NAME);
    https_server_mismatched_->AddDefaultHandlers(GetChromeTestDataDir());

    TabHoverCardInteractiveUiTest::SetUpOnMainThread();
    lookalikes::InitializeSafetyTipConfig();
  }

  net::EmbeddedTestServer* https_server_mismatched() {
    return https_server_mismatched_.get();
  }

 private:
  std::unique_ptr<net::EmbeddedTestServer> https_server_mismatched_;
};

// Verify that the domain field of tab's hover card is empty if the tab is
// showing a lookalike interstitial is ("Did you mean google.com?").
IN_PROC_BROWSER_TEST_F(TabHoverCardBubbleViewInterstitialBrowserTest,
                       LookalikeInterstitial_ShouldHideHoverCardUrl) {
  //  Navigate the tab to a lookalike URL and check the hover card. The domain
  //  field must be empty.
  static constexpr char kLookalikeDomain[] = "googlé.com";
  static constexpr char kUrlPath[] = "/empty.html";
  const GURL url = embedded_test_server()->GetURL(kLookalikeDomain, kUrlPath);
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

  content::WebContents* const tab =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_TRUE(chrome_browser_interstitials::IsShowingInterstitial(tab));

  // Open another tab.
  chrome::NewTab(browser());
  auto* const hover_card = SimulateHoverTab(browser(), 0);

  EXPECT_TRUE(hover_card->GetDomainTextForTesting().empty());
  EXPECT_EQ(1, GetHoverCardsSeenCount(browser()));
}

// Verify that the domain field of tab's hover card is not empty on other types
// of interstitials (here, SSL).
IN_PROC_BROWSER_TEST_F(TabHoverCardBubbleViewInterstitialBrowserTest,
                       SSLInterstitial_ShouldShowHoverCardUrl) {
  ASSERT_TRUE(https_server_mismatched()->Start());
  // Navigate the tab to an SSL error.
  static constexpr char kBadSSLDomain[] = "site.test";
  static constexpr char kUrlPath[] = "/empty.html";
  const GURL url = https_server_mismatched()->GetURL(kBadSSLDomain, kUrlPath);
  ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));

  content::WebContents* const tab =
      browser()->tab_strip_model()->GetActiveWebContents();
  ASSERT_TRUE(chrome_browser_interstitials::IsShowingInterstitial(tab));

  // Open another tab.
  chrome::NewTab(browser());
  auto* const hover_card = SimulateHoverTab(browser(), 0);

  EXPECT_EQ(base::UTF8ToUTF16(net::GetHostAndPort(url)),
            hover_card->GetDomainTextForTesting());
  EXPECT_EQ(1, GetHoverCardsSeenCount(browser()));
}

class TabHoverCardFadeFooterInteractiveUiTest
    : public TabHoverCardInteractiveUiTest {
 public:
  FadeAlertFooterRow* GetPrimaryAlertRowFromHoverCard(
      TabHoverCardBubbleView* bubble) {
    return bubble->GetFooterViewForTesting()
        ->GetAlertRowForTesting()
        ->GetPrimaryViewForTesting();
  }

  FadePerformanceFooterRow* GetPrimaryPerformanceRowFromHoverCard(
      TabHoverCardBubbleView* bubble) {
    return bubble->GetFooterViewForTesting()
        ->GetPerformanceRowForTesting()
        ->GetPrimaryViewForTesting();
  }

  FadeCollaborationMessagingFooterRow*
  GetPrimaryCollaborationMessagingRowFromHoverCard(
      TabHoverCardBubbleView* bubble) {
    return bubble->GetFooterViewForTesting()
        ->GetCollaborationMessagingRowForTesting()
        ->GetPrimaryViewForTesting();
  }

  auto CheckAlertRowLabel(std::u16string expected_text, bool has_position) {
    return Steps(
        WaitForShow(TabHoverCardBubbleView::kHoverCardBubbleElementId),
        CheckView(
            TabHoverCardBubbleView::kHoverCardBubbleElementId,
            [=, this](TabHoverCardBubbleView* bubble) {
              views::Label* const alert_label =
                  GetPrimaryAlertRowFromHoverCard(bubble)->footer_label();
              return alert_label->GetText().find(expected_text) !=
                     std::string::npos;
            },
            has_position));
  }

  auto ForceTabDataRefreshMemoryMetrics() {
    return Do([]() {
      TabResourceUsageRefreshWaiter waiter;
      waiter.Wait();
    });
  }
};

// Mocks the hover card footer is playing audio and verifies that
// the correct string is shown for the corresponding tab alert.
IN_PROC_BROWSER_TEST_F(TabHoverCardFadeFooterInteractiveUiTest,
                       HoverCardFooterUpdatesTabAlertStatus) {
  TabStrip* const tab_strip = GetTabStrip(browser());
  ASSERT_TRUE(
      AddTabAtIndex(1, GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_TYPED));
  tab_strip->SetTabData(1, MakeTabRendererData());

  FadeAlertFooterRow* const alert_row =
      GetPrimaryAlertRowFromHoverCard(SimulateHoverTab(browser(), 1));
  EXPECT_EQ(
      l10n_util::GetStringUTF16(IDS_TOOLTIP_TAB_ALERT_STATE_AUDIO_PLAYING),
      alert_row->footer_label()->GetText());
  EXPECT_FALSE(alert_row->icon()->GetImageModel().IsEmpty());

  // Hover card footer should update when we hover over another tab that is
  // not playing audio
  SimulateHoverTab(browser(), 0);
  EXPECT_TRUE(alert_row->footer_label()->GetText().empty());
  EXPECT_TRUE(alert_row->icon()->GetImageModel().IsEmpty());
}

class TabHoverCardFadeFooterWithDiscardInteractiveUiTest
    : public TabHoverCardFadeFooterInteractiveUiTest,
      public ::testing::WithParamInterface<bool> {
 public:
  void SetUp() override {
    TabHoverCardFadeFooterInteractiveUiTest::SetUp();
    scoped_feature_list_.InitWithFeatureState(features::kWebContentsDiscard,
                                              GetParam());
  }

 private:
  base::test::ScopedFeatureList scoped_feature_list_;
};

// Mocks that a tab is discarded and verifies that the correct string
// for a discarded tab and discarded tab with memory usage is displayed
// on the hover card
IN_PROC_BROWSER_TEST_P(TabHoverCardFadeFooterWithDiscardInteractiveUiTest,
                       HoverCardFooterShowsDiscardStatus) {
  TabStrip* const tab_strip = GetTabStrip(browser());
  ASSERT_TRUE(
      AddTabAtIndex(1, GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_TYPED));
  TabRendererData tab_renderer_data = MakeTabRendererData();
  tab_renderer_data.should_show_discard_status = true;
  tab_strip->SetTabData(1, tab_renderer_data);

  FadeAlertFooterRow* const alert_row =
      GetPrimaryAlertRowFromHoverCard(SimulateHoverTab(browser(), 1));
  EXPECT_EQ(l10n_util::GetStringUTF16(IDS_HOVERCARD_INACTIVE_TAB),
            alert_row->footer_label()->GetText());
  EXPECT_FALSE(alert_row->icon()->GetImageModel().IsEmpty());

  // Clear the memory usage data from tab 0 if it was set, otherwise the
  // performance row won't be empty.
  TabRendererData tab_0_data = tab_strip->tab_at(0)->data();
  tab_0_data.tab_resource_usage = nullptr;
  tab_strip->SetTabData(0, tab_0_data);

  // Hover card footer should update when we hover over another tab that is
  // not discarded
  SimulateHoverTab(browser(), 0);
  EXPECT_TRUE(alert_row->footer_label()->GetText().empty());
  EXPECT_TRUE(alert_row->icon()->GetImageModel().IsEmpty());

  // Show discard status with memory savings
  tab_renderer_data.discarded_memory_savings_in_bytes = 1000;
  tab_strip->SetTabData(1, tab_renderer_data);
  SimulateHoverTab(browser(), 1);
  EXPECT_EQ(
      l10n_util::FormatString(
          l10n_util::GetStringUTF16(IDS_HOVERCARD_INACTIVE_TAB_MEMORY_SAVINGS),
          {ui::FormatBytes(
              tab_renderer_data.discarded_memory_savings_in_bytes)},
          nullptr),
      alert_row->footer_label()->GetText());
}

// With memory usage in hovercards pref set to enabled, mocks a tab with normal
// memory usage and verifies that the string for normal memory usage is shown on
// the hover card. Also tests that the hover card updates this string and use
// the high memory usage string instead when the tab uses memory above the
// threshold.
IN_PROC_BROWSER_TEST_F(TabHoverCardFadeFooterInteractiveUiTest,
                       HoverCardFooterMemoryUsagePrefEnabled) {
  g_browser_process->local_state()->SetBoolean(
      prefs::kHoverCardMemoryUsageEnabled, true);
  ASSERT_TRUE(
      AddTabAtIndex(1, GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_TYPED));

  uint64_t bytes_used = 1000;
  auto* const tab_resource_usage_tab_helper = GetResourceUsageAt(1);
  tab_resource_usage_tab_helper->SetMemoryUsageInBytes(bytes_used);

  // Show memory usage without savings
  FadePerformanceFooterRow* const performance_row =
      GetPrimaryPerformanceRowFromHoverCard(SimulateHoverTab(browser(), 1));
  EXPECT_EQ(l10n_util::FormatString(
                l10n_util::GetStringUTF16(IDS_HOVERCARD_TAB_MEMORY_USAGE),
                {ui::FormatBytes(bytes_used)}, nullptr),
            performance_row->footer_label()->GetText());
  EXPECT_FALSE(performance_row->icon()->GetImageModel().IsEmpty());

  // Hover card updates and shows high memory usage when card is still open
  bytes_used = TabResourceUsage::kHighMemoryUsageThresholdBytes + 100;
  tab_resource_usage_tab_helper->SetMemoryUsageInBytes(bytes_used);
  GetTabStrip(browser())
      ->hover_card_controller_for_testing()
      ->OnTabResourceMetricsRefreshed();
  EXPECT_EQ(l10n_util::FormatString(
                l10n_util::GetStringUTF16(IDS_HOVERCARD_TAB_HIGH_MEMORY_USAGE),
                {ui::FormatBytes(bytes_used)}, nullptr),
            performance_row->footer_label()->GetText());
}

// With memory usage in hovercards pref set to disabled, mocks a tab with normal
// memory usage and verifies that the string for normal memory usage is not
// shown on the hover card. Also tests that the hover card does show the high
// memory usage string when the tab uses memory above the threshold.
IN_PROC_BROWSER_TEST_F(TabHoverCardFadeFooterInteractiveUiTest,
                       HoverCardFooterMemoryUsagePrefDisabled) {
  g_browser_process->local_state()->SetBoolean(
      prefs::kHoverCardMemoryUsageEnabled, false);
  ASSERT_TRUE(
      AddTabAtIndex(1, GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_TYPED));

  uint64_t bytes_used = 1000;
  auto* const tab_resource_usage_tab_helper = GetResourceUsageAt(1);
  tab_resource_usage_tab_helper->SetMemoryUsageInBytes(bytes_used);

  // Don't show memory usage
  FadePerformanceFooterRow* const performance_row =
      GetPrimaryPerformanceRowFromHoverCard(SimulateHoverTab(browser(), 1));
  EXPECT_TRUE(performance_row->footer_label()->GetText().empty());
  EXPECT_TRUE(performance_row->icon()->GetImageModel().IsEmpty());

  // Hover card updates and shows high memory usage when card is still open
  bytes_used = TabResourceUsage::kHighMemoryUsageThresholdBytes + 100;
  tab_resource_usage_tab_helper->SetMemoryUsageInBytes(bytes_used);
  GetTabStrip(browser())
      ->hover_card_controller_for_testing()
      ->OnTabResourceMetricsRefreshed();
  EXPECT_EQ(l10n_util::FormatString(
                l10n_util::GetStringUTF16(IDS_HOVERCARD_TAB_HIGH_MEMORY_USAGE),
                {ui::FormatBytes(bytes_used)}, nullptr),
            performance_row->footer_label()->GetText());
}

IN_PROC_BROWSER_TEST_F(TabHoverCardFadeFooterInteractiveUiTest,
                       ActiveMemoryUsageHidesOnDiscard) {
  const uint64_t bytes_used = 1;
  GetResourceUsageAt(0)->SetMemoryUsageInBytes(bytes_used);

  RunTestSequence(InstrumentTab(kFirstTabContents, 0),
                  NavigateWebContents(kFirstTabContents, GetURL("a.com")),
                  AddInstrumentedTab(kSecondTabContents, GetURL("b.com")),
                  ForceTabDataRefreshMemoryMetrics(), TryDiscardTab(0),
                  HoverTabAt(0),
                  WaitForShow(FooterView::kHoverCardFooterElementId),
                  CheckAlertRowLabel(u"Inactive tab", true),
                  CheckView(
                      TabHoverCardBubbleView::kHoverCardBubbleElementId,
                      [=, this](TabHoverCardBubbleView* bubble) {
                        return GetPrimaryPerformanceRowFromHoverCard(bubble)
                            ->footer_label()
                            ->GetText()
                            .empty();
                      },
                      true));
}

// The discarded status in the hover card footer should disappear after a
// discarded tab is reloaded
IN_PROC_BROWSER_TEST_P(TabHoverCardFadeFooterWithDiscardInteractiveUiTest,
                       DiscardStatusHidesOnReload) {
  RunTestSequence(
      InstrumentTab(kFirstTabContents, 0),
      NavigateWebContents(kFirstTabContents, GetURL("a.com")),
      AddInstrumentedTab(kSecondTabContents, GetURL("b.com")), UnhoverTab(),
      ForceTabDataRefreshMemoryMetrics(), HoverTabAt(0), CheckHovercardIsOpen(),
      CheckAlertRowLabel(u"Inactive tab", false), UnhoverTab(),
      CheckHovercardIsClosed(),
      // Check that the discarded tab should update its contents to show discard
      // status
      TryDiscardTab(0), HoverTabAt(0),
      WaitForShow(FooterView::kHoverCardFooterElementId),
      CheckAlertRowLabel(u"Inactive tab", true), UnhoverTab(),
      CheckHovercardIsClosed(),
      // Ensure that hover card shows footer only for the discarded tab
      HoverTabAt(1), CheckHovercardIsOpen(),
      CheckAlertRowLabel(u"Inactive tab", false),
      // Select discarded tab
      UnhoverTab(), CheckHovercardIsClosed(), SelectTab(kTabStripElementId, 0),
      WaitForWebContentsReady(kFirstTabContents), HoverTabAt(0),
      CheckHovercardIsOpen(), CheckAlertRowLabel(u"Inactive tab", false));
}

// The hover card should stop showing memory usage data after navigating to
// another site since the data is now out of date
IN_PROC_BROWSER_TEST_F(TabHoverCardFadeFooterInteractiveUiTest,
                       MemoryUpdatesOnNavigation) {
  const uint64_t bytes_used = 1;
  GetResourceUsageAt(0)->SetMemoryUsageInBytes(bytes_used);

  RunTestSequence(
      InstrumentTab(kFirstTabContents, 0), UnhoverTab(), HoverTabAt(0),
      CheckHovercardIsOpen(),
      WaitForShow(FooterView::kHoverCardFooterElementId), UnhoverTab(),
      CheckHovercardIsClosed(),
      NavigateWebContents(kFirstTabContents, GetURL("a.com")), HoverTabAt(0),
      CheckHovercardIsOpen(),
      CheckView(
          TabHoverCardBubbleView::kHoverCardBubbleElementId,
          [=, this](TabHoverCardBubbleView* bubble) {
            views::Label* const performance_label =
                GetPrimaryPerformanceRowFromHoverCard(bubble)->footer_label();
            return performance_label->GetText().find(l10n_util::GetStringFUTF16(
                       IDS_HOVERCARD_TAB_MEMORY_USAGE,
                       ui::FormatBytes(bytes_used))) != std::string::npos;
          },
          false));
}

// The hover card should display tab memory usage in the footer. When the user
// hovers over a tab without memory data available, the footer should update
// accordingly and stop showing memory usage
IN_PROC_BROWSER_TEST_F(TabHoverCardFadeFooterInteractiveUiTest,
                       FooterHidesOnTabWithoutMemoryUsage) {
  ASSERT_TRUE(
      AddTabAtIndex(1, GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_TYPED));

  GetResourceUsageAt(0)->SetMemoryUsageInBytes(1000);

  // Footer should show when hovering over tab with memory usage
  views::View* const footer_view =
      SimulateHoverTab(browser(), 0)->GetFooterViewForTesting();
  EXPECT_TRUE(footer_view->GetVisible());

  // Hover over a tab without memory usage
  GetResourceUsageAt(1)->SetMemoryUsageInBytes(0);
  SimulateHoverTab(browser(), 1);

  // Footer should no longer be visible because there is no memory data
  EXPECT_FALSE(footer_view->GetVisible());
}

IN_PROC_BROWSER_TEST_F(TabHoverCardFadeFooterInteractiveUiTest,
                       BackgroundTabHoverCardContentsHaveCorrectDimensions) {
  TabStrip* const tab_strip = GetTabStrip(browser());
  ASSERT_TRUE(
      AddTabAtIndex(1, GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_TYPED));
  browser()->tab_strip_model()->ActivateTabAt(0);
  Tab* const tab = tab_strip->tab_at(1);
  TabRendererData data = tab->data();
  data.alert_state = {tabs::TabAlert::AUDIO_PLAYING};
  tab->SetData(data);
  tab_strip->GetFocusManager()->SetFocusedView(tab);
  WaitForHoverCardVisible(tab_strip);

  auto* const hover_card =
      tab_strip->hover_card_controller_for_testing()->hover_card_for_testing();
  gfx::Size hover_card_size = hover_card->size();

  int total_children_height = 0;

  // Verify that all children of the hovercard can fit within the hovercard
  for (views::View* child : hover_card->children()) {
    EXPECT_TRUE(child->GetVisible());
    gfx::Size child_size = child->size();
    EXPECT_GT(child_size.width(), 0);
    EXPECT_LE(child_size.width(), hover_card_size.width());
    EXPECT_GT(child_size.height(), 0);
    EXPECT_LE(child_size.height(), hover_card_size.height());
    total_children_height += child_size.height();
  }

  // Verify that stacking the children within the hovercard takes up the entire
  // hover card space
  total_children_height +=
      hover_card->title_label_->GetProperty(views::kMarginsKey)->height() +
      hover_card->domain_label_->GetProperty(views::kMarginsKey)->height();
  EXPECT_EQ(hover_card_size.height(), total_children_height);
}

// Mocks that a tab has collaboration messaging and verifies that the correct
// string is displayed on the hover card.
IN_PROC_BROWSER_TEST_F(TabHoverCardFadeFooterInteractiveUiTest,
                       HoverCardFooterShowsCollaborationMessaging) {
  TabStrip* const tab_strip = GetTabStrip(browser());
  ASSERT_TRUE(
      AddTabAtIndex(1, GURL(url::kAboutBlankURL), ui::PAGE_TRANSITION_TYPED));
  TabRendererData tab_renderer_data = MakeTabRendererData();

  // Clear alert state. Alerts take precedence over all other footers.
  tab_renderer_data.alert_state = {};
  tab_groups::CollaborationMessagingTabData data(browser()->profile());
  tab_renderer_data.collaboration_messaging = data.GetWeakPtr();

  // Do not make a network request for the user's avatar.
  data.set_mocked_avatar_for_testing(gfx::Image());

  // Create a mock PersistentMessage
  // Show collaboration messaging status with TAB_ADDED event.
  std::string given_name = "User";
  std::string avatar_url = "https://google.com/chrome/1";
  data.SetMessage(
      CreateMessage(given_name, avatar_url,
                    collaboration::messaging::CollaborationEvent::TAB_ADDED));

  tab_strip->SetTabData(1, tab_renderer_data);
  FadeCollaborationMessagingFooterRow* const collaboration_messaging_row =
      GetPrimaryCollaborationMessagingRowFromHoverCard(
          SimulateHoverTab(browser(), 1));
  EXPECT_EQ(u"User added this tab",
            collaboration_messaging_row->footer_label()->GetText());
  EXPECT_FALSE(collaboration_messaging_row->icon()->GetImageModel().IsEmpty());

  // Hover card footer should update when we hover over another tab that is
  // not discarded
  SimulateHoverTab(browser(), 0);
  EXPECT_TRUE(collaboration_messaging_row->footer_label()->GetText().empty());
  EXPECT_TRUE(collaboration_messaging_row->icon()->GetImageModel().IsEmpty());

  // Reset tab data by setting intermediate object. Without this, the new
  // tab_data is ignored because it is the same object.
  tab_strip->SetTabData(1, MakeTabRendererData());

  // Change username and action to show collaboration messaging with TAB_UPDATED
  // event.
  std::string given_name2 = "Another User";
  std::string avatar_url2 = "https://google.com/chrome/2";
  data.SetMessage(
      CreateMessage(given_name2, avatar_url2,
                    collaboration::messaging::CollaborationEvent::TAB_UPDATED));

  tab_strip->SetTabData(1, tab_renderer_data);
  SimulateHoverTab(browser(), 1);
  EXPECT_EQ(u"Another User changed this tab",
            collaboration_messaging_row->footer_label()->GetText());
  EXPECT_FALSE(collaboration_messaging_row->icon()->GetImageModel().IsEmpty());
}

#if BUILDFLAG(IS_CHROMEOS)
class TabHoverCardSystemWebAppTest : public InteractiveBrowserTest {
 public:
  TabHoverCardSystemWebAppTest()
      : test_system_web_app_installation_(
            ash::TestSystemWebAppInstallation::SetUpTabbedMultiWindowApp()) {}

  void SetUpOnMainThread() override {
    InteractiveBrowserTest::SetUpOnMainThread();
    Tab::SetShowHoverCardOnMouseHoverForTesting(true);
  }

 protected:
  std::unique_ptr<ash::TestSystemWebAppInstallation>
      test_system_web_app_installation_;
};

IN_PROC_BROWSER_TEST_F(TabHoverCardSystemWebAppTest,
                       HideDomainNameFromHoverCard) {
  test_system_web_app_installation_->WaitForAppInstall();
  const auto* const app_browser = web_app::LaunchWebAppBrowser(
      browser()->profile(), test_system_web_app_installation_->GetAppId());
  const char kTabToHover[] = "Tab to hover";

  RunTestSequenceInContext(
      app_browser->window()->GetElementContext(),
      WithView(kTabStripElementId,
               [](TabStrip* tab_strip) { tab_strip->StopAnimating(true); }),
      NameDescendantViewByType<Tab>(kBrowserViewElementId, kTabToHover, 0),
      MoveMouseTo(kTabToHover),
      WaitForShow(TabHoverCardBubbleView::kHoverCardBubbleElementId),
      EnsureNotPresent(TabHoverCardBubbleView::kHoverCardDomainLabelElementId),
      MoveMouseTo(kNewTabButtonElementId),
      WaitForHide(TabHoverCardBubbleView::kHoverCardBubbleElementId));
}
#endif

INSTANTIATE_TEST_SUITE_P(
    ,
    TabHoverCardFadeFooterWithDiscardInteractiveUiTest,
    ::testing::Values(false, true),
    [](const ::testing::TestParamInfo<
        TabHoverCardFadeFooterWithDiscardInteractiveUiTest::ParamType>& info) {
      return info.param ? "RetainedWebContents" : "UnretainedWebContents";
    });