#include "chrome/browser/ui/views/bookmarks/bookmark_bar_view.h"
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/compiler_specific.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/ranges/algorithm.h"
#include "base/run_loop.h"
#include "base/scoped_multi_source_observation.h"
#include "base/scoped_observation.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "build/build_config.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/bookmarks/managed_bookmark_service_factory.h"
#include "chrome/browser/chrome_content_browser_client.h"
#include "chrome/browser/gcm/gcm_profile_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/actions/chrome_actions.h"
#include "chrome/browser/ui/bookmarks/bookmark_utils.h"
#include "chrome/browser/ui/bookmarks/bookmark_utils_desktop.h"
#include "chrome/browser/ui/bookmarks/test_bookmark_navigation_wrapper.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.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/views/bookmarks/bookmark_bar_view_observer.h"
#include "chrome/browser/ui/views/bookmarks/bookmark_context_menu.h"
#include "chrome/browser/ui/views/bookmarks/bookmark_menu_controller_views.h"
#include "chrome/browser/ui/views/chrome_constrained_window_views_client.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/test/view_event_test_base.h"
#include "chrome/common/chrome_content_client.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/test_browser_window.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/common/bookmark_pref_names.h"
#include "components/bookmarks/test/bookmark_test_helpers.h"
#include "components/constrained_window/constrained_window_views.h"
#include "components/gcm_driver/fake_gcm_profile_service.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/page_navigator.h"
#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/ozone_buildflags.h"
#include "ui/base/test/ui_controls.h"
#include "ui/base/ui_base_features.h"
#include "ui/display/display_switches.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/views/background.h"
#include "ui/views/controls/button/menu_button.h"
#include "ui/views/controls/menu/menu_controller.h"
#include "ui/views/controls/menu/menu_item_view.h"
#include "ui/views/controls/menu/menu_scroll_view_container.h"
#include "ui/views/controls/menu/submenu_view.h"
#include "ui/views/layout/flex_layout.h"
#include "ui/views/layout/flex_layout_types.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/drop_helper.h"
#include "ui/views/widget/widget.h"
#if !BUILDFLAG(IS_MAC)
#include "ui/aura/env.h"
#include "ui/aura/env_observer.h"
#include "ui/aura/window.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "ui/aura/window_tree_host.h"
#endif
ASCIIToUTF16;
BookmarkModel;
BookmarkNode;
BrowserThread;
OpenURLParams;
PageNavigator;
WebContents;
namespace {
#if !BUILDFLAG(IS_MAC)
class DialogWaiter : public aura::EnvObserver, public views::WidgetObserver { … };
class DialogCloseWaiter : public views::WidgetObserver { … };
class TabKeyWaiter : public ui::EventHandler { … };
void MoveMouseAndPress(const gfx::Point& screen_pos,
ui_controls::MouseButton button,
int button_state,
base::OnceClosure closure) { … }
#endif
class TestingPageNavigator : public PageNavigator { … };
}
class BookmarkBarViewEventTestBase : public ViewEventTestBase { … };
class BookmarkBarViewDragTestBase : public BookmarkBarViewEventTestBase,
public BookmarkBarViewObserver,
public views::WidgetObserver { … };
#if !BUILDFLAG(IS_MAC)
class BookmarkBarViewTest1 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_Basic …
#else
#define MAYBE_Basic …
#endif
VIEW_TEST(BookmarkBarViewTest1, MAYBE_Basic)
class BookmarkBarViewTest2 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_HideOnDesktopClick …
#else
#define MAYBE_HideOnDesktopClick …
#endif
VIEW_TEST(BookmarkBarViewTest2, MAYBE_HideOnDesktopClick)
class BookmarkBarViewTest3 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_Submenus …
#else
#define MAYBE_Submenus …
#endif
VIEW_TEST(BookmarkBarViewTest3, MAYBE_Submenus)
class BookmarkContextMenuNotificationObserver { … };
class BookmarkBarViewTest4 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_ContextMenus …
#else
#define MAYBE_ContextMenus …
#endif
VIEW_TEST(BookmarkBarViewTest4, MAYBE_ContextMenus)
class BookmarkBarViewTest5 : public BookmarkBarViewDragTestBase { … };
VIEW_TEST(BookmarkBarViewTest5, DND)
class BookmarkBarViewTest6 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_OZONE_WAYLAND) || BUILDFLAG(IS_WIN)
#define MAYBE_OpenMenuOnClickAndHold …
#else
#define MAYBE_OpenMenuOnClickAndHold …
#endif
VIEW_TEST(BookmarkBarViewTest6, MAYBE_OpenMenuOnClickAndHold)
class BookmarkBarViewTest7 : public BookmarkBarViewDragTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_DNDToDifferentMenu …
#else
#define MAYBE_DNDToDifferentMenu …
#endif
VIEW_TEST(BookmarkBarViewTest7, MAYBE_DNDToDifferentMenu)
class BookmarkBarViewTest8 : public BookmarkBarViewDragTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_DNDBackToOriginatingMenu …
#else
#define MAYBE_DNDBackToOriginatingMenu …
#endif
VIEW_TEST(BookmarkBarViewTest8, MAYBE_DNDBackToOriginatingMenu)
class BookmarkBarViewTest9 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_OZONE_WAYLAND) || BUILDFLAG(IS_WIN)
#define MAYBE_ScrollButtonScrolls …
#else
#define MAYBE_ScrollButtonScrolls …
#endif
VIEW_TEST(BookmarkBarViewTest9, MAYBE_ScrollButtonScrolls)
class BookmarkBarViewTest10 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_KeyEvents …
#else
#define MAYBE_KeyEvents …
#endif
VIEW_TEST(BookmarkBarViewTest10, MAYBE_KeyEvents)
class BookmarkBarViewTest11 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN)
#define MAYBE_CloseMenuAfterClosingContextMenu …
#else
#define MAYBE_CloseMenuAfterClosingContextMenu …
#endif
VIEW_TEST(BookmarkBarViewTest11, MAYBE_CloseMenuAfterClosingContextMenu)
class BookmarkBarViewTest12 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_CloseWithModalDialog …
#else
#define MAYBE_CloseWithModalDialog …
#endif
VIEW_TEST(BookmarkBarViewTest12, MAYBE_CloseWithModalDialog)
class BookmarkBarViewTest13 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_ClickOnContextMenuSeparator …
#else
#define MAYBE_ClickOnContextMenuSeparator …
#endif
VIEW_TEST(BookmarkBarViewTest13, MAYBE_ClickOnContextMenuSeparator)
class BookmarkBarViewTest14 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_ContextMenus2 …
#else
#define MAYBE_ContextMenus2 …
#endif
VIEW_TEST(BookmarkBarViewTest14, MAYBE_ContextMenus2)
class BookmarkBarViewTest15 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_MenuStaysVisibleAfterDelete …
#else
#define MAYBE_MenuStaysVisibleAfterDelete …
#endif
VIEW_TEST(BookmarkBarViewTest15, MAYBE_MenuStaysVisibleAfterDelete)
class BookmarkBarViewTest16 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_DeleteMenu …
#else
#define MAYBE_DeleteMenu …
#endif
VIEW_TEST(BookmarkBarViewTest16, MAYBE_DeleteMenu)
class BookmarkBarViewTest17 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_WIN)
#define MAYBE_ContextMenus3 …
#else
#define MAYBE_ContextMenus3 …
#endif
VIEW_TEST(BookmarkBarViewTest17, MAYBE_ContextMenus3)
class BookmarkBarViewTest18 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_BookmarkBarViewTest18_SiblingMenu …
#else
#define MAYBE_BookmarkBarViewTest18_SiblingMenu …
#endif
VIEW_TEST(BookmarkBarViewTest18, MAYBE_BookmarkBarViewTest18_SiblingMenu)
class BookmarkBarViewTest19 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_BookmarkBarViewTest19_SiblingMenu …
#else
#define MAYBE_BookmarkBarViewTest19_SiblingMenu …
#endif
VIEW_TEST(BookmarkBarViewTest19, MAYBE_BookmarkBarViewTest19_SiblingMenu)
class BookmarkBarViewTest20 : public BookmarkBarViewEventTestBase { … };
BEGIN_METADATA(…)
END_METADATA
#if BUILDFLAG(IS_OZONE_WAYLAND) || BUILDFLAG(IS_WIN)
#define MAYBE_ContextMenuExitTest …
#else
#define MAYBE_ContextMenuExitTest …
#endif
VIEW_TEST(BookmarkBarViewTest20, MAYBE_ContextMenuExitTest)
class BookmarkBarViewTest21 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_ContextMenusForEmptyFolder …
#else
#define MAYBE_ContextMenusForEmptyFolder …
#endif
VIEW_TEST(BookmarkBarViewTest21, MAYBE_ContextMenusForEmptyFolder)
class BookmarkBarViewTest22 : public BookmarkBarViewDragTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_CloseSourceBrowserDuringDrag …
#else
#define MAYBE_CloseSourceBrowserDuringDrag …
#endif
VIEW_TEST(BookmarkBarViewTest22, MAYBE_CloseSourceBrowserDuringDrag)
class BookmarkBarViewTest23 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_ContextMenusKeyboard …
#else
#define MAYBE_ContextMenusKeyboard …
#endif
VIEW_TEST(BookmarkBarViewTest23, MAYBE_ContextMenusKeyboard)
class BookmarkBarViewTest24 : public BookmarkBarViewEventTestBase { … };
VIEW_TEST(BookmarkBarViewTest24, DISABLED_ContextMenusKeyboardEscape)
#if BUILDFLAG(IS_WIN)
template <ui::KeyboardCode KEYCODE>
class BookmarkBarViewTest25 : public BookmarkBarViewEventTestBase {
protected:
void DoTestOnMessageLoop() override {
OpenMenuByClick(GetBookmarkButton(0),
CreateEventTask(this, &BookmarkBarViewTest25::Step2));
}
private:
void Step2() {
ASSERT_TRUE(ui_controls::SendKeyPressNotifyWhenDone(
window()->GetNativeWindow(), KEYCODE, false, false, false, false,
CreateEventTask(this, &BookmarkBarViewTest25::Step3)));
}
void Step3() {
ASSERT_FALSE(MenuIsShowing());
Done();
}
};
using BookmarkBarViewTest25F10 = BookmarkBarViewTest25<ui::VKEY_F10>;
#if BUILDFLAG(IS_WIN)
#define MAYBE_F10ClosesMenu …
#else
#define MAYBE_F10ClosesMenu …
#endif
VIEW_TEST(BookmarkBarViewTest25F10, MAYBE_F10ClosesMenu)
using BookmarkBarViewTest25Alt = BookmarkBarViewTest25<ui::VKEY_MENU>;
#if BUILDFLAG(IS_WIN)
#define MAYBE_AltClosesMenu …
#else
#define MAYBE_AltClosesMenu …
#endif
VIEW_TEST(BookmarkBarViewTest25Alt, MAYBE_AltClosesMenu)
class BookmarkBarViewTest26 : public BookmarkBarViewEventTestBase {
protected:
void DoTestOnMessageLoop() override {
OpenMenuByClick(GetBookmarkButton(0),
CreateEventTask(this, &BookmarkBarViewTest26::Step2));
}
private:
void Step2() {
::SendMessage(window()->GetNativeView()->GetHost()->GetAcceleratedWidget(),
WM_CANCELMODE, 0, 0);
base::SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE,
base::BindOnce(&BookmarkBarViewTest26::Step3, base::Unretained(this)));
}
void Step3() {
ASSERT_FALSE(MenuIsShowing());
Done();
}
};
#if BUILDFLAG(IS_WIN)
#define MAYBE_CancelModeClosesMenu …
#else
#define MAYBE_CancelModeClosesMenu …
#endif
VIEW_TEST(BookmarkBarViewTest26, MAYBE_CancelModeClosesMenu)
#endif
class BookmarkBarViewTest27 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_MiddleClickOnFolderOpensAllBookmarks …
#else
#define MAYBE_MiddleClickOnFolderOpensAllBookmarks …
#endif
VIEW_TEST(BookmarkBarViewTest27, MAYBE_MiddleClickOnFolderOpensAllBookmarks)
#endif
class BookmarkBarViewTest28 : public BookmarkBarViewEventTestBase { … };
#if BUILDFLAG(IS_WIN)
#define MAYBE_ClickWithModifierOnFolderOpensAllBookmarks …
#else
#define MAYBE_ClickWithModifierOnFolderOpensAllBookmarks …
#endif
VIEW_TEST(BookmarkBarViewTest28,
MAYBE_ClickWithModifierOnFolderOpensAllBookmarks)
class BookmarkBarViewTest29 : public BookmarkBarViewDragTestBase { … };
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN)
#define MAYBE_DNDToEmptyMenu …
#else
#define MAYBE_DNDToEmptyMenu …
#endif
VIEW_TEST(BookmarkBarViewTest29, MAYBE_DNDToEmptyMenu)