chromium/chrome/browser/ui/views/tabs/tab_hover_card_controller_interactive_uitest.cc

// 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/tab_enums.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/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/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 "content/public/browser/web_contents.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_ASH)
#include "chrome/browser/ash/system_web_apps/test_support/test_system_web_app_installation.h"
#endif

namespace {
constexpr char16_t kTabTitle[] =;
constexpr char16_t kTabDomain[] =;
constexpr char kTabUrl[] =;

DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE();
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE();

TabRendererData MakeTabRendererData() {}

}  // namespace

class TabHoverCardInteractiveUiTest
    : public MemorySaverInteractiveTestMixin<InteractiveBrowserTest>,
      public test::TabHoverCardTestUtil {};

#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) {}

#endif

IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       HoverCardHidesOnMouseExit) {}

#if BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_HoverCardShownOnTabFocus
#else
#define MAYBE_HoverCardShownOnTabFocus
#endif
IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       MAYBE_HoverCardShownOnTabFocus) {}

IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       HoverCardVisibleOnTabCloseButtonFocusAfterTabFocus) {}

// Verify hover card is visible when tab is focused and a key is pressed.
IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       WidgetVisibleOnKeyPressAfterTabFocus) {}

// Verify hover card thumbnail is not visible on active tabs.
IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       ThumbnailNotVisibileOnActiveTabs) {}

// Verify hover card is not visible when tab is focused and the mouse is
// pressed.
IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       WidgetNotVisibleOnMousePressAfterTabFocus) {}

IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       WidgetNotVisibleOnMousePressAfterHover) {}

IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       HoverCardVisibleOnTabFocusFromKeyboardAccelerator) {}

IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       InactiveWindowStaysInactiveOnHover) {}

IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       UpdatesHoverCardOnHoverDifferentTab) {}

IN_PROC_BROWSER_TEST_F(TabHoverCardInteractiveUiTest,
                       HoverCardDoesNotHaveFooterView) {}

TabHoverCardBubbleViewMetricsTest;

IN_PROC_BROWSER_TEST_F(TabHoverCardBubbleViewMetricsTest,
                       HoverCardsSeenRatioMetric) {}

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

// 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) {}

// 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) {}

class TabHoverCardFadeFooterInteractiveUiTest
    : public TabHoverCardInteractiveUiTest {};

// 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) {}

// 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_F(TabHoverCardFadeFooterInteractiveUiTest,
                       HoverCardFooterShowsDiscardStatus) {}

// 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) {}

// 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) {}

IN_PROC_BROWSER_TEST_F(TabHoverCardFadeFooterInteractiveUiTest,
                       ActiveMemoryUsageHidesOnDiscard) {}

// The discarded status in the hover card footer should disappear after a
// discarded tab is reloaded
IN_PROC_BROWSER_TEST_F(TabHoverCardFadeFooterInteractiveUiTest,
                       DiscardStatusHidesOnReload) {}

// 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) {}

// 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) {}

IN_PROC_BROWSER_TEST_F(
    TabHoverCardFadeFooterInteractiveUiTest,
    BackgroundTabHoverCardContentsHaveCorrectDimensions) {}

#if BUILDFLAG(IS_CHROMEOS_ASH)
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