chromium/chrome/browser/ui/views/web_apps/frame_toolbar/web_app_frame_toolbar_browsertest.cc

// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <cmath>
#include <optional>
#include <string_view>
#include <tuple>

#include "base/memory/raw_ptr.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/icu_test_util.h"
#include "base/test/run_until.h"
#include "base/test/test_future.h"
#include "base/test/test_timeouts.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/download/bubble/download_bubble_ui_controller.h"
#include "chrome/browser/download/bubble/download_display_controller.h"
#include "chrome/browser/extensions/chrome_test_extension_loader.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/custom_theme_supplier.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/download/download_display.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/browser/ui/views/extensions/extensions_menu_view.h"
#include "chrome/browser/ui/views/extensions/extensions_toolbar_button.h"
#include "chrome/browser/ui/views/extensions/extensions_toolbar_container.h"
#include "chrome/browser/ui/views/frame/app_menu_button.h"
#include "chrome/browser/ui/views/frame/browser_non_client_frame_view.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/browser_view_layout.h"
#include "chrome/browser/ui/views/frame/toolbar_button_provider.h"
#include "chrome/browser/ui/views/frame/top_container_view.h"
#include "chrome/browser/ui/views/infobars/infobar_container_view.h"
#include "chrome/browser/ui/views/infobars/infobar_view.h"
#include "chrome/browser/ui/views/page_action/page_action_icon_controller.h"
#include "chrome/browser/ui/views/web_apps/frame_toolbar/web_app_frame_toolbar_test_helper.h"
#include "chrome/browser/ui/views/web_apps/frame_toolbar/web_app_frame_toolbar_view.h"
#include "chrome/browser/ui/views/web_apps/frame_toolbar/web_app_navigation_button_container.h"
#include "chrome/browser/ui/views/web_apps/frame_toolbar/web_app_origin_text.h"
#include "chrome/browser/ui/views/web_apps/frame_toolbar/web_app_toolbar_button_container.h"
#include "chrome/browser/ui/views/web_apps/frame_toolbar/window_controls_overlay_toggle_button.h"
#include "chrome/browser/ui/web_applications/app_browser_controller.h"
#include "chrome/browser/ui/web_applications/test/isolated_web_app_test_utils.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/ui/web_applications/web_app_browsertest_base.h"
#include "chrome/browser/ui/web_applications/web_app_menu_model.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_url_info.h"
#include "chrome/browser/web_applications/mojom/user_display_mode.mojom.h"
#include "chrome/browser/web_applications/scope_extension_info.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_origin_association_manager.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_registry_update.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "chromeos/constants/chromeos_features.h"
#include "chromeos/ui/frame/caption_buttons/frame_caption_button_container_view.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/permissions/permission_request_manager.h"
#include "components/webapps/services/web_app_origin_association/test/test_web_app_origin_association_fetcher.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/common/page_zoom.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/theme_change_waiter.h"
#include "extensions/test/test_extension_dir.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/widget/constants.h"
#include "third_party/skia/include/core/SkColor.h"
#include "third_party/skia/include/core/SkRect.h"
#include "ui/base/hit_test.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/gfx/geometry/size.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/animation/ink_drop.h"
#include "ui/views/controls/label.h"
#include "ui/views/test/views_test_utils.h"
#include "ui/views/view.h"
#include "ui/views/view_observer.h"
#include "ui/views/widget/any_widget_observer.h"
#include "url/gurl.h"
#include "url/origin.h"

#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ui/views/frame/browser_non_client_frame_view_chromeos.h"
#endif

#if BUILDFLAG(IS_LINUX)
#include "chrome/browser/ui/views/frame/opaque_browser_frame_view_layout.h"
#endif

#if defined(USE_AURA)
#include "ui/aura/client/aura_constants.h"
#endif

namespace {

#if BUILDFLAG(IS_MAC)
// Keep in sync with browser_non_client_frame_view_mac.mm
constexpr double kTitlePaddingWidthFraction = 0.1;
#endif

template <typename T>
T* GetLastVisible(const std::vector<T*>& views) {}

void LoadTestPopUpExtension(Profile* profile) {}

SkColor GetFrameColor(Browser* browser) {}

}  // namespace

class WebAppFrameToolbarBrowserTest : public web_app::WebAppBrowserTestBase {};

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest, SpaceConstrained) {}

// TODO(crbug.com/40940526): Re-enable this test
#if BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_ThemeChange
#else
#define MAYBE_ThemeChange
#endif
IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest, MAYBE_ThemeChange) {}

// Test that a tooltip is shown when hovering over a truncated title.
IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest, TitleHover) {}

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest,
                       MenuButtonAccessibleName) {}

class WebAppFrameToolbarBrowserTest_ElidedExtensionsMenu
    : public WebAppFrameToolbarBrowserTest {};

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_ElidedExtensionsMenu,
                       Test) {}

class WebAppFrameToolbarBrowserTest_NoElidedExtensionsMenu
    : public WebAppFrameToolbarBrowserTest {};

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_NoElidedExtensionsMenu,
                       Test) {}

// Borderless has not been implemented for win/mac.
#if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS))
class BorderlessIsolatedWebAppBrowserTest
    : public web_app::IsolatedWebAppBrowserTestHarness {};

IN_PROC_BROWSER_TEST_F(BorderlessIsolatedWebAppBrowserTest,
                       AppUsesBorderlessModeAndHasWindowManagementPermission) {}

// Regression test for b/321784833.
IN_PROC_BROWSER_TEST_F(BorderlessIsolatedWebAppBrowserTest,
                       BorderlessModeHidesTitlebarAndWindowingControls) {}

IN_PROC_BROWSER_TEST_F(BorderlessIsolatedWebAppBrowserTest,
                       DisplayModeMediaCSS) {}

IN_PROC_BROWSER_TEST_F(
    BorderlessIsolatedWebAppBrowserTest,
    AppUsesBorderlessModeAndDoesNotHaveWindowManagementPermission) {}

IN_PROC_BROWSER_TEST_F(BorderlessIsolatedWebAppBrowserTest,
                       AppDoesntUseBorderlessMode) {}

IN_PROC_BROWSER_TEST_F(BorderlessIsolatedWebAppBrowserTest,
                       PopupToItselfIsBorderless) {}

IN_PROC_BROWSER_TEST_F(BorderlessIsolatedWebAppBrowserTest,
                       PopupToAnyOtherOriginIsNotBorderless) {}

IN_PROC_BROWSER_TEST_F(
    BorderlessIsolatedWebAppBrowserTest,
    PopupSize_CanSubceedMinimumWindowSize_And_InnerAndOuterSizesAreCorrect) {}

IN_PROC_BROWSER_TEST_F(
    BorderlessIsolatedWebAppBrowserTest,
    PopupResize_CanSubceedMinimumWindowSize_And_InnerAndOuterSizesAreCorrect) {}

// Test to ensure that the minimum size for a borderless app is as small as
// possible. To test the fix for b/265935069.
IN_PROC_BROWSER_TEST_F(BorderlessIsolatedWebAppBrowserTest, FrameMinimumSize) {}

class BorderlessIsolatedWebAppBrowserTestDisabledFlag
    : public BorderlessIsolatedWebAppBrowserTest {};

IN_PROC_BROWSER_TEST_F(BorderlessIsolatedWebAppBrowserTestDisabledFlag,
                       AppCannotUseFeatureWhenBorderlessFlagIsDisabled) {}
#endif  // (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS))

class WebAppFrameToolbarBrowserTest_WindowControlsOverlay
    : public WebAppFrameToolbarBrowserTest {};

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       WindowControlsOverlay) {}

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       GeometryChangeEvent) {}

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       NoGeometryChangeEventIfOverlayIsOff) {}

// TODO(crbug.com/40827841): Enable for mac/win when flakiness has been fixed.
#if !BUILDFLAG(IS_MAC) && !BUILDFLAG(IS_WIN)
// Test to ensure crbug.com/1298226 won't reproduce.
IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       PopupFromWcoAppToItself) {}

// Test to ensure crbug.com/1298237 won't reproduce.
IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       PopupFromWcoAppToAnyOtherWebsite) {}
#endif  // !BUILDFLAG(IS_MAC) && !BUILDFLAG(IS_WIN)

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       WindowControlsOverlayRTL) {}

// Test to ensure crbug.com/1353133 won't reproduce. It casts the frame_view to
// the ChromeOS's frame_view to have access to the caption_button_container_ so
// it cannot be run on any other platform.
#if BUILDFLAG(IS_CHROMEOS)
IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       WindowControlsOverlayFrameViewHeight) {
  InstallAndLaunchWebApp();
  ToggleWindowControlsOverlayAndWait();
  EXPECT_TRUE(GetWindowControlOverlayVisibility());

  BrowserNonClientFrameViewChromeOS* frame_view_cros =
      static_cast<BrowserNonClientFrameViewChromeOS*>(helper()->frame_view());

  int frame_view_height = frame_view_cros->GetMinimumSize().height();
  int caption_container_height =
      frame_view_cros->caption_button_container()->size().height();
  int client_view_height =
      frame_view_cros->frame()->client_view()->GetMinimumSize().height();

  // Frame view minimum height also includes radius of window to ensure correct
  // rounding of window. See b/294588040.
  int window_radius = chromeos::features::RoundedWindowsRadius();

  EXPECT_EQ(frame_view_height,
            caption_container_height + client_view_height + window_radius);
}
#endif  // BUILDFLAG(IS_CHROMEOS)

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       CSSRectTestLTR) {}

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       CSSRectTestRTL) {}

// TODO(crbug.com/40809857): Flaky. Also enable for borderless mode when
// fixed.
IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       DISABLED_WindowControlsOverlayDraggableRegions) {}

// Regression test for https://crbug.com/1448878.
IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       DraggableRegionsIgnoredForOwnedWidgets) {}

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       ToggleWindowControlsOverlay) {}

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       OpenInChrome) {}

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       HideToggleButtonWhenCCTIsVisible) {}

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       HideToggleButtonWhenInfoBarIsVisible) {}

// Regression test for https://crbug.com/1239443.
IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       OpenWithOverlayEnabled) {}

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       DraggableRegionNotResetByFencedFrameNavigation) {}

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       FencedFrame) {}

// Extensions in  ChromeOS are not in the titlebar.
#if !BUILDFLAG(IS_CHROMEOS)
// Regression test for https://crbug.com/1351566.
IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       ExtensionsIconVisibility) {}
#endif  // !BUILDFLAG(IS_CHROMEOS)

#if !BUILDFLAG(IS_CHROMEOS_ASH)
// Test that a download by a web app browser only shows the download UI in that
// app's window.
IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       DownloadIconVisibilityForAppDownload) {}

// Test that a download by a regular browser does not show the download UI in an
// app's window.
IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       DownloadIconVisibilityForRegularDownload) {}
#endif  // !BUILDFLAG(IS_CHROMEOS_ASH)

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       DisplayModeMediaCSS) {}

// Verifies that draggable and non draggable regions defined by the app-region
// CSS property are collected.
IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       DraggableRegionsEnabled) {}

// Regression test for https://crbug.com/1516830.
IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_WindowControlsOverlay,
                       DragAfterNavigation) {}

// Tests for Additional Windowing Controls on web app windows.
// https://chromestatus.com/feature/5201832664629248
// For popup tests see PopupTest_AdditionalWindowingControls
#if !BUILDFLAG(IS_ANDROID)
class WebAppFrameToolbarBrowserTest_AdditionalWindowingControls
    : public WebAppFrameToolbarBrowserTest {};

IN_PROC_BROWSER_TEST_F(
    WebAppFrameToolbarBrowserTest_AdditionalWindowingControls,
    WindowSetResizableMatches) {}

IN_PROC_BROWSER_TEST_F(
    WebAppFrameToolbarBrowserTest_AdditionalWindowingControls,
    NavigatingBetweenTwoPagesWithUnsetResizability) {}

IN_PROC_BROWSER_TEST_F(
    WebAppFrameToolbarBrowserTest_AdditionalWindowingControls,
    NavigatingBetweenTwoPagesWithNonNullResizability) {}

IN_PROC_BROWSER_TEST_F(
    WebAppFrameToolbarBrowserTest_AdditionalWindowingControls,
    NavigatingOutsideTheAppScopeAndBackResetsAndThenRestoresResizability) {}

// TODO(crbug.com/333641972): Re-enable this test on Mac.
#if BUILDFLAG(IS_MAC)
#define MAYBE_WindowSetResizableBlocksResizeToAndResizeByApis
#else
#define MAYBE_WindowSetResizableBlocksResizeToAndResizeByApis
#endif
IN_PROC_BROWSER_TEST_F(
    WebAppFrameToolbarBrowserTest_AdditionalWindowingControls,
    MAYBE_WindowSetResizableBlocksResizeToAndResizeByApis) {}

// Test to ensure crbug.com/1513330 won't reproduce.
// TODO(b/41492287, b/336264927): Flaky on Linux, Mac, and Lacros.
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_CHROMEOS_LACROS)
#define MAYBE_WindowSetResizableDoesntBlockMoveToAndMoveByApis
#else
#define MAYBE_WindowSetResizableDoesntBlockMoveToAndMoveByApis
#endif
IN_PROC_BROWSER_TEST_F(
    WebAppFrameToolbarBrowserTest_AdditionalWindowingControls,
    MAYBE_WindowSetResizableDoesntBlockMoveToAndMoveByApis) {}

IN_PROC_BROWSER_TEST_F(
    WebAppFrameToolbarBrowserTest_AdditionalWindowingControls,
    MinimizeWindowWithApi) {}

IN_PROC_BROWSER_TEST_F(
    WebAppFrameToolbarBrowserTest_AdditionalWindowingControls,
    MaximizeAndRestoreWindowWithApi) {}

#if !BUILDFLAG(IS_MAC)
IN_PROC_BROWSER_TEST_F(
    WebAppFrameToolbarBrowserTest_AdditionalWindowingControls,
    WindowSetResizableBlocksMaximizingNormalWindow) {}

IN_PROC_BROWSER_TEST_F(
    WebAppFrameToolbarBrowserTest_AdditionalWindowingControls,
    WindowSetResizableBlocksRestoringMaximizedWindow) {}
#endif  // !BUILDFLAG(IS_MAC)

IN_PROC_BROWSER_TEST_F(
    WebAppFrameToolbarBrowserTest_AdditionalWindowingControls,
    MoveCallFiresMoveEvent) {}

#endif  // !BUILDFLAG(IS_ANDROID)

class OriginTextVisibilityWaiter : public views::ViewObserver {};

class WebAppFrameToolbarBrowserTest_OriginText
    : public WebAppFrameToolbarBrowserTest,
      public testing::WithParamInterface<bool> {};

IN_PROC_BROWSER_TEST_P(WebAppFrameToolbarBrowserTest_OriginText,
                       InScopeNavigation) {}

IN_PROC_BROWSER_TEST_P(WebAppFrameToolbarBrowserTest_OriginText,
                       OutOfScopeBarShown) {}

IN_PROC_BROWSER_TEST_P(WebAppFrameToolbarBrowserTest_OriginText,
                       ThemeColorChange) {}

IN_PROC_BROWSER_TEST_P(WebAppFrameToolbarBrowserTest_OriginText,
                       OutOfScopeBarWithThemeColorChange) {}

IN_PROC_BROWSER_TEST_P(WebAppFrameToolbarBrowserTest_OriginText,
                       WebAppOriginTextAccessibleProperties) {}

INSTANTIATE_TEST_SUITE_P();

class WebAppFrameToolbarBrowserTest_ScopeExtensionsOriginText
    : public WebAppFrameToolbarBrowserTest {};

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_ScopeExtensionsOriginText,
                       ExtendedScope) {}

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_ScopeExtensionsOriginText,
                       ExtendedScopeToOutOfScope) {}

IN_PROC_BROWSER_TEST_F(WebAppFrameToolbarBrowserTest_ScopeExtensionsOriginText,
                       ExtendedScopeThemeColorChange) {}