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
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include "base/test/bind.h"
#include "build/buildflag.h"
#include "chrome/browser/bookmarks/bookmark_model_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_element_identifiers.h"
#include "chrome/browser/ui/toolbar/bookmark_sub_menu_model.h"
#include "chrome/browser/ui/views/toolbar/browser_app_menu_button.h"
#include "chrome/test/interaction/interactive_browser_test.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_model_observer.h"
#include "components/commerce/core/commerce_feature_list.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/ozone_buildflags.h"
#include "ui/base/test/ui_controls.h"
#include "ui/views/controls/menu/menu_item_view.h"
#include "ui/views/controls/menu/submenu_view.h"
#include "ui/views/view_utils.h"
#if BUILDFLAG(IS_MAC)
#include "base/mac/mac_util.h"
#endif
// Observes a widget to track when a drag is complete.
class DragWaiter : public views::WidgetObserver {
public:
explicit DragWaiter(views::Widget* widget) : widget_(widget) {
widget_->AddObserver(this);
}
~DragWaiter() override {
if (widget_) {
widget_->RemoveObserver(this);
}
}
DragWaiter(const DragWaiter&) = delete;
void operator=(const DragWaiter&) = delete;
void OnWidgetDestroying(views::Widget* widget) override {
if (drag_loop_ && drag_loop_->running()) {
drag_loop_->Quit();
}
widget_->RemoveObserver(this);
widget_ = nullptr;
}
void OnWidgetDragComplete(views::Widget* widget) override {
drag_complete_ = true;
if (drag_loop_ && drag_loop_->running()) {
drag_loop_->Quit();
}
}
void Wait() {
if (!drag_complete_) {
drag_loop_ = std::make_unique<base::RunLoop>(
base::RunLoop::Type::kNestableTasksAllowed);
drag_loop_->Run();
}
}
private:
raw_ptr<views::Widget> widget_ = nullptr;
bool drag_complete_ = false;
std::unique_ptr<base::RunLoop> drag_loop_;
};
class AppMenuDragAndDropInteractiveTest : public InteractiveBrowserTest {
public:
using DragObserver =
views::test::PollingViewObserver<bool, views::MenuItemView>;
AppMenuDragAndDropInteractiveTest() {
// Disabled to hide the comparison tables submenu.
scoped_feature_list_.InitAndDisableFeature(
commerce::kProductSpecifications);
}
~AppMenuDragAndDropInteractiveTest() override = default;
AppMenuDragAndDropInteractiveTest(const AppMenuDragAndDropInteractiveTest&) =
delete;
void operator=(const AppMenuDragAndDropInteractiveTest&) = delete;
// Names the menu item at a given index of `target_view`'s submenu.
// The usual `NameChildView` doesn't work for this case because submenus
// have their own widgets and are not part of the parent menu item's view
// hierarchy.
auto NameSubmenuChild(ElementSpecifier target_view,
std::string name,
size_t index) {
return Steps(NameViewRelative(
target_view, name,
base::BindLambdaForTesting([=](views::View* view) -> views::View* {
views::MenuItemView* const menu =
views::AsViewClass<views::MenuItemView>(view);
CHECK(menu);
CHECK(menu->HasSubmenu());
return menu->GetSubmenu()->GetMenuItemAt(index);
})));
}
// Returns a relative-position callback that gets the top-center point of
// a view. The value is slightly adjusted to prevent rounding errors on
// scaled devices.
auto TopCenter() {
return base::BindLambdaForTesting([](views::View* el) {
return el->GetBoundsInScreen().top_center() + gfx::Vector2d(0, 2);
});
}
auto CenterPoint() {
return base::BindLambdaForTesting([](views::View* view) {
return view->GetBoundsInScreen().CenterPoint();
});
}
auto RegisterDragWaiter(ElementSpecifier target_view) {
return WithView(target_view, [&](views::View* view) {
auto* menu = static_cast<views::MenuItemView*>(view);
drag_waiter_ = std::make_unique<DragWaiter>(menu->GetWidget());
});
}
auto RemoveDragWaiter() {
return Do([&]() { drag_waiter_.reset(); });
}
// The original "DragMouseTo" method has issues on various platforms. This
// method uses custom logic to perform the drag.
auto DragMouseTo(ElementSpecifier dragged_view,
ElementSpecifier target_view,
base::RepeatingCallback<gfx::Point(views::View*)> pos) {
return Steps(
RegisterDragWaiter(dragged_view),
WithView(target_view,
[this, pos = std::move(pos)](views::View* view) {
base::RunLoop press_loop(
base::RunLoop::Type::kNestableTasksAllowed);
EXPECT_TRUE(ui_controls::SendMouseEventsNotifyWhenDone(
ui_controls::MouseButton::LEFT,
ui_controls::MouseButtonState::DOWN,
press_loop.QuitClosure()));
press_loop.Run();
gfx::Rect bounds = view->GetBoundsInScreen();
gfx::Point start_location(bounds.width() / 2,
bounds.height() / 2);
gfx::Point target_location =
start_location + gfx::Vector2d(10, 10);
// Send an initial mouse movement to start the drag.
EXPECT_TRUE(ui_controls::SendMouseMove(target_location.x(),
target_location.y()));
// Send another mouse movement to the target desitnation.
target_location = std::move(pos).Run(view);
EXPECT_TRUE(ui_controls::SendMouseMove(target_location.x(),
target_location.y()));
// Release the mouse movement.
EXPECT_TRUE(ui_controls::SendMouseEvents(
ui_controls::MouseButton::LEFT,
ui_controls::MouseButtonState::UP));
// Await drag completion.
drag_waiter_->Wait();
}),
RemoveDragWaiter());
}
private:
std::unique_ptr<DragWaiter> drag_waiter_;
base::test::ScopedFeatureList scoped_feature_list_;
};
// TODO(crbug.com/375959961): For X11, the menu is always closed on drag
// completion because the native widget's state is not properly updated.
// TODO(crbug.com/388531778): DND tests are flaky on Windows. This should be
// re-enabled once de-flaked.
#if BUILDFLAG(IS_OZONE_X11) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_OZONE_WAYLAND)
#define MAYBE_DISABLED(test_name) DISABLED_##test_name
#else
#define MAYBE_DISABLED(test_name) test_name
#endif
// TODO(crbug.com/391735476) Deflake on Mac11.
#if BUILDFLAG(IS_MAC)
#define SKIP_IF_MAC11() \
if (base::mac::MacOSMajorVersion() == 11) { \
GTEST_SKIP() << "Test is flaky on Mac11 (crbug.com/391735476)"; \
}
#else
#define SKIP_IF_MAC11()
#endif
IN_PROC_BROWSER_TEST_F(AppMenuDragAndDropInteractiveTest,
MAYBE_DISABLED(BookmarksDragAndDrop)) {
SKIP_IF_MAC11();
// Add two bookmarks nodes to the bookmarks bar.
bookmarks::BookmarkModel* const model =
BookmarkModelFactory::GetForBrowserContext(browser()->profile());
const bookmarks::BookmarkNode* const bb_node = model->bookmark_bar_node();
model->AddFolder(bb_node, 0, u"a");
model->AddFolder(bb_node, 1, u"b");
static constexpr std::string kANodeMenuId = "a_node";
static constexpr std::string kBNodeMenuId = "b_node";
RunTestSequence(
// Open the bookmarks menu in the App menu.
PressButton(kToolbarAppMenuButtonElementId),
SelectMenuItem(AppMenuModel::kBookmarksMenuItem),
// The 9th and 10th menu items of the bookmarks menu should be the two
// bookmarks added to the bookmarks bar.
NameSubmenuChild(AppMenuModel::kBookmarksMenuItem, kANodeMenuId, 8u),
CheckViewProperty(kANodeMenuId, &views::MenuItemView::title, u"a"),
NameSubmenuChild(AppMenuModel::kBookmarksMenuItem, kBNodeMenuId, 9u),
CheckViewProperty(kBNodeMenuId, &views::MenuItemView::title, u"b"),
// Drag bookmark "b" above bookmark "a".
MoveMouseTo(kBNodeMenuId),
DragMouseTo(kBNodeMenuId, kANodeMenuId, TopCenter()),
// The order of the bookmarks should now be swapped.
NameSubmenuChild(AppMenuModel::kBookmarksMenuItem, kANodeMenuId, 9u),
CheckViewProperty(kANodeMenuId, &views::MenuItemView::title, u"a"),
NameSubmenuChild(AppMenuModel::kBookmarksMenuItem, kBNodeMenuId, 8u),
CheckViewProperty(kBNodeMenuId, &views::MenuItemView::title, u"b"));
}
IN_PROC_BROWSER_TEST_F(AppMenuDragAndDropInteractiveTest,
MAYBE_DISABLED(BookmarksDragAndDropToNestedFolder)) {
SKIP_IF_MAC11();
// Add two bookmarks nodes to the bookmarks bar.
bookmarks::BookmarkModel* const model =
BookmarkModelFactory::GetForBrowserContext(browser()->profile());
const bookmarks::BookmarkNode* const bb_node = model->bookmark_bar_node();
model->AddFolder(bb_node, 0, u"a");
model->AddFolder(bb_node, 1, u"b");
static constexpr std::string kANodeMenuId = "a_node";
static constexpr std::string kBNodeMenuId = "b_node";
RunTestSequence(
// Open the bookmarks menu in the App menu.
PressButton(kToolbarAppMenuButtonElementId),
SelectMenuItem(AppMenuModel::kBookmarksMenuItem),
// The 9th and 10th menu items of the bookmarks menu should be the two
// bookmarks added to the bookmarks bar.
NameSubmenuChild(AppMenuModel::kBookmarksMenuItem, kANodeMenuId, 8u),
CheckViewProperty(kANodeMenuId, &views::MenuItemView::title, u"a"),
NameSubmenuChild(AppMenuModel::kBookmarksMenuItem, kBNodeMenuId, 9u),
CheckViewProperty(kBNodeMenuId, &views::MenuItemView::title, u"b"),
// Drag bookmark "a" onto bookmark folder "b".
MoveMouseTo(kBNodeMenuId),
DragMouseTo(kBNodeMenuId, kANodeMenuId, CenterPoint()),
// Bookmark folder "b" should now be the 9th menu item, and bookmark "a"
// should be in its submenu.
NameSubmenuChild(AppMenuModel::kBookmarksMenuItem, kANodeMenuId, 8u),
CheckViewProperty(kANodeMenuId, &views::MenuItemView::title, u"a"),
NameSubmenuChild(kANodeMenuId, kBNodeMenuId, 0u),
CheckViewProperty(kBNodeMenuId, &views::MenuItemView::title, u"b"));
}
IN_PROC_BROWSER_TEST_F(AppMenuDragAndDropInteractiveTest,
MAYBE_DISABLED(BookmarksDragAndDropFromNestedFolder)) {
SKIP_IF_MAC11();
// Add one bookmark folder to the bookmarks bar, and add a bookmark node to
// the new folder.
bookmarks::BookmarkModel* const model =
BookmarkModelFactory::GetForBrowserContext(browser()->profile());
const bookmarks::BookmarkNode* const bb_node = model->bookmark_bar_node();
model->AddFolder(bb_node, 0, u"a");
model->AddFolder(bb_node->children()[0].get(), 0, u"b");
static constexpr std::string kANodeMenuId = "a_node";
static constexpr std::string kBNodeMenuId = "b_node";
RunTestSequence(
// Open the bookmarks menu in the App menu.
PressButton(kToolbarAppMenuButtonElementId),
SelectMenuItem(AppMenuModel::kBookmarksMenuItem),
// The 9th menu item of the bookmarks menu should be the bookmark "a".
// The 1st menu item of bookmark folder "a" should be bookmark "b".
NameSubmenuChild(AppMenuModel::kBookmarksMenuItem, kANodeMenuId, 8u),
CheckViewProperty(kANodeMenuId, &views::MenuItemView::title, u"a"),
SelectMenuItem(kANodeMenuId), // Open bookmark folder "a".
NameSubmenuChild(kANodeMenuId, kBNodeMenuId, 0u),
CheckViewProperty(kBNodeMenuId, &views::MenuItemView::title, u"b"),
// Drag bookmark "b" out of bookmark folder "a".
MoveMouseTo(kBNodeMenuId),
DragMouseTo(kBNodeMenuId, kANodeMenuId, TopCenter()),
// Bookmark "b" should now be the 9th menu item and bookmark folder "a"
// should be the 10th.
NameSubmenuChild(AppMenuModel::kBookmarksMenuItem, kBNodeMenuId, 8u),
CheckViewProperty(kBNodeMenuId, &views::MenuItemView::title, u"b"),
NameSubmenuChild(AppMenuModel::kBookmarksMenuItem, kBNodeMenuId, 9u),
CheckViewProperty(kBNodeMenuId, &views::MenuItemView::title, u"a"));
}
|