chromium/chrome/browser/ui/views/frame/browser_view_unittest.cc

// Copyright 2013 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/ui/views/frame/browser_view.h"

#include <memory>

#include "base/memory/scoped_refptr.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/ui/actions/chrome_action_id.h"
#include "chrome/browser/ui/browser_actions.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_navigator.h"
#include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/performance_controls/tab_resource_usage_tab_helper.h"
#include "chrome/browser/ui/tabs/tab_activity_simulator.h"
#include "chrome/browser/ui/tabs/tab_enums.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/toolbar/pinned_toolbar/pinned_toolbar_actions_model.h"
#include "chrome/browser/ui/toolbar/pinned_toolbar/pinned_toolbar_actions_model_factory.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
#include "chrome/browser/ui/views/frame/browser_view_layout.h"
#include "chrome/browser/ui/views/frame/tab_strip_region_view.h"
#include "chrome/browser/ui/views/frame/test_with_browser_view.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/tabs/tab_strip.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "components/vector_icons/vector_icons.h"
#include "components/version_info/channel.h"
#include "content/public/test/navigation_simulator.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/web_contents_tester.h"
#include "ui/actions/actions.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/dialog_model.h"
#include "ui/base/text/bytes_formatting.h"
#include "ui/gfx/scrollbar_size.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/bubble/bubble_dialog_model_host.h"
#include "ui/views/controls/webview/webview.h"

#if BUILDFLAG(IS_MAC)
#include "chrome/browser/ui/recently_audible_helper.h"
#endif

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

namespace {

// Class for BrowserView unit tests for the loading animation feature.
// Creates a Browser with a |features_list| where
// kStopLoadingAnimationForHiddenWindow is enabled before setting GPU thread.
class BrowserViewTestWithStopLoadingAnimationForHiddenWindow
    : public TestWithBrowserView {};

// Tab strip bounds depend on the window frame sizes.
gfx::Point ExpectedTabStripRegionOrigin(BrowserView* browser_view) {}

// Helper function to take a printf-style format string and substitute the
// browser name (like "Chromium" or "Google Chrome") for %s, and return the
// result as a std::u16string.
std::u16string SubBrowserName(const char* fmt) {}

}  // namespace

class BrowserViewTest : public TestWithBrowserView {};

// Test basic construction and initialization.
TEST_F(BrowserViewTest, BrowserView) {}

#if BUILDFLAG(IS_CHROMEOS_ASH)
TEST_F(BrowserViewTest, OnTaskLockedBrowserView) {
  ASSERT_TRUE(browser_view()->browser());
  browser_view()->browser()->SetLockedForOnTask(true);
  EXPECT_FALSE(browser_view()->CanMinimize());
  EXPECT_FALSE(browser_view()->ShouldShowCloseButton());
}

TEST_F(BrowserViewTest, OnTaskUnlockedBrowserView) {
  ASSERT_TRUE(browser_view()->browser());
  browser_view()->browser()->SetLockedForOnTask(false);
  EXPECT_TRUE(browser_view()->CanMinimize());
  EXPECT_TRUE(browser_view()->ShouldShowCloseButton());
}
#endif

namespace {
// A thin wrapper around `Browser` to ensure that it's destructed in the right
// order.
class ScopedBrowser {};
}  // namespace

// TODO(crbug.com/326199292): Flaky on Linux.
#if BUILDFLAG(IS_LINUX)
#define MAYBE_UpdateActiveBrowser
#else
#define MAYBE_UpdateActiveBrowser
#endif

// Test that calling `BrowserView::Activate()` or `BrowserView::Show()` sets
// the last active browser synchronously.
TEST_F(BrowserViewTest, MAYBE_UpdateActiveBrowser) {}

// Test layout of the top-of-window UI.
TEST_F(BrowserViewTest, DISABLED_BrowserViewLayout) {}

// TODO(crbug.com/40656637): Flaky on Linux.
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#define MAYBE_FindBarBoundingBoxLocationBar
#else
#define MAYBE_FindBarBoundingBoxLocationBar
#endif
// Test the find bar's bounding box when the location bar is visible.
TEST_F(BrowserViewTest, MAYBE_FindBarBoundingBoxLocationBar) {}

// Test the find bar's bounding box when the location bar is not visible.
TEST_F(BrowserViewTest, FindBarBoundingBoxNoLocationBar) {}

// Tests that a browser window is correctly associated to a WebContents that
// belongs to that window's UI hierarchy.
TEST_F(BrowserViewTest, FindBrowserWindowWithWebContents) {}

// Tests that tab contents are correctly associated with their browser window,
// even when non-active.
TEST_F(BrowserViewTest, FindBrowserWindowWithWebContentsTabSwitch) {}

// On macOS, most accelerators are handled by CommandDispatcher.
#if !BUILDFLAG(IS_MAC)
// Test that repeated accelerators are processed or ignored depending on the
// commands that they refer to. The behavior for different commands is dictated
// by IsCommandRepeatable() in chrome/browser/ui/views/accelerator_table.h.
TEST_F(BrowserViewTest, DISABLED_RepeatedAccelerators) {}
#endif  // !BUILDFLAG(IS_MAC)

// Test that bookmark bar view becomes invisible when closing the browser.
// TODO(crbug.com/40097152): Flaky on Linux.
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#define MAYBE_BookmarkBarInvisibleOnShutdown
#else
#define MAYBE_BookmarkBarInvisibleOnShutdown
#endif
TEST_F(BrowserViewTest, MAYBE_BookmarkBarInvisibleOnShutdown) {}

TEST_F(BrowserViewTest, DISABLED_AccessibleWindowTitle) {}

TEST_F(BrowserViewTest, WindowTitleOmitsLowMemoryUsage) {}

#if BUILDFLAG(IS_MAC)
// Tests that audio playing state is reflected in the "Window" menu on Mac.
TEST_F(BrowserViewTest, TitleAudioIndicators) {
  std::u16string playing_icon = u"\U0001F50A";
  std::u16string muted_icon = u"\U0001F507";

  AddTab(browser_view()->browser(), GURL("about:blank"));
  content::WebContents* contents = browser_view()->GetActiveWebContents();
  RecentlyAudibleHelper* audible_helper =
      RecentlyAudibleHelper::FromWebContents(contents);

  audible_helper->SetNotRecentlyAudibleForTesting();
  EXPECT_EQ(browser_view()->GetWindowTitle().find(playing_icon),
            std::u16string::npos);
  EXPECT_EQ(browser_view()->GetWindowTitle().find(muted_icon),
            std::u16string::npos);

  audible_helper->SetCurrentlyAudibleForTesting();
  EXPECT_NE(browser_view()->GetWindowTitle().find(playing_icon),
            std::u16string::npos);
  EXPECT_EQ(browser_view()->GetWindowTitle().find(muted_icon),
            std::u16string::npos);

  audible_helper->SetRecentlyAudibleForTesting();
  contents->SetAudioMuted(true);
  EXPECT_EQ(browser_view()->GetWindowTitle().find(playing_icon),
            std::u16string::npos);
  EXPECT_NE(browser_view()->GetWindowTitle().find(muted_icon),
            std::u16string::npos);
}
#endif

TEST_F(BrowserViewTest, RotatePaneFocusFromView) {}

TEST_F(BrowserViewTest, AccessibleProperties) {}

//  Macs do not have fullscreen policy.
#if !BUILDFLAG(IS_MAC)

TEST_F(BrowserViewTest, CanFullscreenPolicyWatcher) {}

class BrowserViewPipTest : public TestWithBrowserView {};

// Pip is used to test reverting back to not allowed to fullscreen state.
TEST_F(BrowserViewPipTest, CanFullscreenPolicyDoesNotEnableFullscreen) {}

#endif  // !BUILDFLAG(IS_MAC)

class BrowserViewHostedAppTest : public TestWithBrowserView {};

// Test basic layout for hosted apps.
TEST_F(BrowserViewHostedAppTest, Layout) {}

BrowserViewWindowTypeTest;

TEST_F(BrowserViewWindowTypeTest, TestWindowIsNotReturned) {}

// Tests Feature to ensure that the loading animation is not rendered after the
// window changes to hidden.
TEST_F(BrowserViewTestWithStopLoadingAnimationForHiddenWindow,
       LoadingAnimationNotRenderedWhenWindowHidden) {}