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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
// 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 "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/app/vector_icons/vector_icons.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/tabs/split_tab_menu_model.h"
#include "chrome/browser/ui/tabs/tab_menu_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/tabs/tab_strip_model_observer.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/tabs/tab.h"
#include "chrome/browser/ui/views/tabs/tab_strip.h"
#include "chrome/browser/ui/views/test/split_tabs_interactive_test_mixin.h"
#include "chrome/browser/ui/views/toolbar/split_tabs_button.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/generated_resources.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/prefs/pref_service.h"
#include "components/tabs/public/split_tab_collection.h"
#include "components/tabs/public/split_tab_visual_data.h"
#include "content/public/test/browser_test.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/interaction/interactive_test.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/test/ui_controls.h"
#include "ui/base/ui_base_types.h"
#include "ui/views/controls/menu/menu_item_view.h"
#include "ui/views/controls/menu/submenu_view.h"
#include "url/gurl.h"
namespace {
class ActiveTabObserver : public TabStripModelObserver,
public ui::test::StateObserver<bool> {
public:
explicit ActiveTabObserver(TabStripModel* tab_strip_model)
: tab_strip_model_(tab_strip_model) {
tab_strip_model_->AddObserver(this);
}
~ActiveTabObserver() override { tab_strip_model_->RemoveObserver(this); }
void OnTabStripModelChanged(
TabStripModel* tab_strip_model,
const TabStripModelChange& change,
const TabStripSelectionChange& selection) override {
if (change.type() == TabStripModelChange::kMoved) {
OnStateObserverStateChanged(true);
}
}
private:
raw_ptr<TabStripModel> tab_strip_model_;
};
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kWebContents1Id);
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kWebContents2Id);
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kWebContents3Id);
DEFINE_LOCAL_STATE_IDENTIFIER_VALUE(ActiveTabObserver, kActiveTabChanged);
} // namespace
class SplitTabButtonInteractiveTest
: public SplitTabsInteractiveTestMixin<InteractiveBrowserTest> {
public:
void SetUpOnMainThread() override {
SplitTabsInteractiveTestMixin::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(embedded_test_server()->Start());
}
GURL GetTestUrl(std::string relative_url = "/title1.html") const {
return embedded_test_server()->GetURL(relative_url);
}
auto UpdateSplitTabButtonPinState(bool should_pin) {
return Do([=, this]() {
browser()->profile()->GetPrefs()->SetBoolean(prefs::kPinSplitTabButton,
should_pin);
});
}
auto CheckSplitTabButtonIcon(const gfx::VectorIcon& expected_icon) {
return CheckView(
kToolbarSplitTabsToolbarButtonElementId,
[](SplitTabsToolbarButton* button) {
auto vector_icons = button->GetIconsForTesting();
CHECK(vector_icons.has_value());
return vector_icons->icon.name;
},
expected_icon.name);
}
auto CheckTabCount(int expected_count) {
return CheckResult(
[this]() { return browser()->tab_strip_model()->count(); },
expected_count);
}
auto CheckTabInSplit(int tab_index, bool expected_split_state) {
return CheckResult(
[=, this]() {
tabs::TabInterface* const tab =
browser()->tab_strip_model()->GetTabAtIndex(tab_index);
return tab->IsSplit();
},
expected_split_state);
}
auto CheckMenuString(ElementSpecifier identifier, int expected_string_id) {
return CheckView(
identifier,
[](views::MenuItemView* menu_item_view) {
return menu_item_view->title();
},
l10n_util::GetStringUTF16(expected_string_id));
}
auto CheckMenuIcon(ElementSpecifier identifier, const gfx::VectorIcon& icon) {
return CheckView(
identifier,
[](views::MenuItemView* menu_item_view) {
return menu_item_view->GetIcon().GetVectorIcon().vector_icon()->name;
},
icon.name);
}
auto ClickActiveTabInSplit() {
return Steps(MoveMouseTo(base::BindLambdaForTesting([this]() {
return multi_contents_view()
->GetActiveContentsView()
->GetBoundsInScreen()
.CenterPoint();
})),
ClickMouse());
}
auto CheckSplitTabButtonPinState(bool should_pin) {
return CheckResult(
[=, this]() {
return browser()->profile()->GetPrefs()->GetBoolean(
prefs::kPinSplitTabButton);
},
should_pin);
}
auto RightClickSplitTabsButton() {
return Steps(MoveMouseTo(kToolbarSplitTabsToolbarButtonElementId),
ClickMouse(ui_controls::RIGHT));
}
auto NamePinStateMenuItem(std::string_view name, int menu_index) {
return NameViewRelative(SplitTabsToolbarButton::kUpdatePinStateMenu, name,
[=](views::SubmenuView* root_item) {
return root_item->GetMenuItemAt(menu_index);
});
}
};
IN_PROC_BROWSER_TEST_F(SplitTabButtonInteractiveTest, PinSplitTabButton) {
RunTestSequence(EnsureNotPresent(kToolbarSplitTabsToolbarButtonElementId),
UpdateSplitTabButtonPinState(true),
WaitForShow(kToolbarSplitTabsToolbarButtonElementId),
UpdateSplitTabButtonPinState(false),
WaitForHide(kToolbarSplitTabsToolbarButtonElementId));
}
IN_PROC_BROWSER_TEST_F(SplitTabButtonInteractiveTest,
UnpinSplitTabWhileActive) {
const GURL url1 = embedded_test_server()->GetURL("/title1.html");
RunTestSequence(InstrumentTab(kWebContents1Id),
NavigateWebContents(kWebContents1Id, url1),
AddInstrumentedTab(kWebContents2Id, url1),
AddInstrumentedTab(kWebContents3Id, url1),
UpdateSplitTabButtonPinState(true),
WaitForShow(kToolbarSplitTabsToolbarButtonElementId),
SelectTab(kTabStripElementId, 0), EnterSplitView(0, 1),
EnsurePresent(kToolbarSplitTabsToolbarButtonElementId),
UpdateSplitTabButtonPinState(false),
EnsurePresent(kToolbarSplitTabsToolbarButtonElementId));
}
IN_PROC_BROWSER_TEST_F(SplitTabButtonInteractiveTest, PinButtonWithMenu) {
const GURL url1 = embedded_test_server()->GetURL("/title1.html");
constexpr char kPinMenuItem[] = "pin_menu_item";
constexpr char kUnpinMenuItem[] = "unpin_menu_item";
RunTestSequence(
AddInstrumentedTab(kWebContents2Id, url1),
SelectTab(kTabStripElementId, 0), EnterSplitView(0, 1),
WaitForShow(kToolbarSplitTabsToolbarButtonElementId),
// The split tab button should be unpinned and the menu should reflect
// that.
MayInvolveNativeContextMenu(
RightClickSplitTabsButton(),
WaitForShow(SplitTabsToolbarButton::kUpdatePinStateMenu),
CheckSplitTabButtonPinState(false),
NamePinStateMenuItem(kPinMenuItem, 0),
CheckMenuString(kPinMenuItem,
IDS_SIDE_PANEL_TOOLBAR_BUTTON_CXMENU_PIN),
CheckMenuIcon(kPinMenuItem, kKeepIcon), SelectMenuItem(kPinMenuItem)),
WaitForHide(kPinMenuItem),
// Verify that the split tab button is pinned.
MayInvolveNativeContextMenu(
RightClickSplitTabsButton(),
WaitForShow(SplitTabsToolbarButton::kUpdatePinStateMenu),
CheckSplitTabButtonPinState(true),
NamePinStateMenuItem(kUnpinMenuItem, 1),
CheckMenuString(kUnpinMenuItem,
IDS_SIDE_PANEL_TOOLBAR_BUTTON_CXMENU_UNPIN),
CheckMenuIcon(kUnpinMenuItem, kKeepOffIcon)));
}
IN_PROC_BROWSER_TEST_F(SplitTabButtonInteractiveTest, UnpinButtonWithMenu) {
const GURL url1 = embedded_test_server()->GetURL("/title1.html");
constexpr char kPinMenuItem[] = "pin_menu_item";
constexpr char kUnpinMenuItem[] = "unpin_menu_item";
RunTestSequence(AddInstrumentedTab(kWebContents2Id, url1),
UpdateSplitTabButtonPinState(true),
WaitForShow(kToolbarSplitTabsToolbarButtonElementId),
SelectTab(kTabStripElementId, 0), EnterSplitView(0, 1),
// Unpin the split tab button with the menu item
MayInvolveNativeContextMenu(
RightClickSplitTabsButton(),
WaitForShow(SplitTabsToolbarButton::kUpdatePinStateMenu),
NamePinStateMenuItem(kUnpinMenuItem, 1),
SelectMenuItem(kUnpinMenuItem)),
WaitForHide(kUnpinMenuItem),
// Verify the button is now unpinned and the menu should have
// the pin option
MayInvolveNativeContextMenu(
RightClickSplitTabsButton(),
WaitForShow(SplitTabsToolbarButton::kUpdatePinStateMenu),
NamePinStateMenuItem(kPinMenuItem, 0),
CheckSplitTabButtonPinState(false),
CheckMenuString(kPinMenuItem,
IDS_SIDE_PANEL_TOOLBAR_BUTTON_CXMENU_PIN),
CheckMenuIcon(kPinMenuItem, kKeepIcon)));
}
IN_PROC_BROWSER_TEST_F(SplitTabButtonInteractiveTest,
OpenCustomizedChromeSidePanel) {
const GURL url1 = embedded_test_server()->GetURL("/title1.html");
constexpr char kCustomizeChromeMenuItem[] = "customize_chrome_menu_item";
RunTestSequence(
AddInstrumentedTab(kWebContents2Id, url1),
SelectTab(kTabStripElementId, 0), EnterSplitView(0, 1),
WaitForShow(kToolbarSplitTabsToolbarButtonElementId),
MayInvolveNativeContextMenu(
RightClickSplitTabsButton(),
WaitForShow(SplitTabsToolbarButton::kUpdatePinStateMenu),
NamePinStateMenuItem(kCustomizeChromeMenuItem, 2),
EnsureNotPresent(kCustomizeChromeSidePanelWebViewElementId),
SelectMenuItem(kCustomizeChromeMenuItem)),
WaitForHide(SplitTabsToolbarButton::kUpdatePinStateMenu),
WaitForShow(kCustomizeChromeSidePanelWebViewElementId));
}
IN_PROC_BROWSER_TEST_F(SplitTabButtonInteractiveTest, DefaultButtonIcon) {
RunTestSequence(UpdateSplitTabButtonPinState(true),
WaitForShow(kToolbarSplitTabsToolbarButtonElementId),
CheckSplitTabButtonIcon(kSplitSceneIcon));
}
IN_PROC_BROWSER_TEST_F(SplitTabButtonInteractiveTest, ButtonIconUpdates) {
const GURL url1 = embedded_test_server()->GetURL("/title1.html");
RunTestSequence(InstrumentTab(kWebContents1Id),
NavigateWebContents(kWebContents1Id, url1),
AddInstrumentedTab(kWebContents2Id, url1),
SelectTab(kTabStripElementId, 0), EnterSplitView(0, 1),
WaitForShow(kToolbarSplitTabsToolbarButtonElementId),
CheckSplitTabButtonIcon(kSplitSceneLeftIcon),
FocusInactiveTabInSplit(),
EnsurePresent(kToolbarSplitTabsToolbarButtonElementId),
CheckSplitTabButtonIcon(kSplitSceneRightIcon));
}
IN_PROC_BROWSER_TEST_F(SplitTabButtonInteractiveTest, EnterSplitView) {
RunTestSequence(
UpdateSplitTabButtonPinState(true),
WaitForShow(kToolbarSplitTabsToolbarButtonElementId), CheckTabCount(1),
PressButton(kToolbarSplitTabsToolbarButtonElementId), CheckTabCount(2),
CheckTabInSplit(0, true), CheckTabInSplit(1, true));
}
IN_PROC_BROWSER_TEST_F(SplitTabButtonInteractiveTest, ToggleMenu) {
RunTestSequence(
UpdateSplitTabButtonPinState(true),
WaitForShow(kToolbarSplitTabsToolbarButtonElementId),
CheckTabInSplit(0, false),
// Since the active tab isn't in a split, the button press
// should create an empty split tab.
PressButton(kToolbarSplitTabsToolbarButtonElementId), CheckTabCount(2),
// Pressing the button while we are in a split should open the
// menu instead.
PressButton(kToolbarSplitTabsToolbarButtonElementId),
WaitForShow(SplitTabMenuModel::kReversePositionMenuItem),
CheckTabCount(2),
// Click on the button again while the menu for the split
// button is open and confirm it hides the menu.
MoveMouseTo(kToolbarSplitTabsToolbarButtonElementId), ClickMouse(),
WaitForHide(SplitTabMenuModel::kReversePositionMenuItem),
CheckTabCount(2));
}
IN_PROC_BROWSER_TEST_F(SplitTabButtonInteractiveTest,
ReversePositionMenuItemUpdates) {
const GURL url1 = embedded_test_server()->GetURL("/title1.html");
RunTestSequence(AddInstrumentedTab(kWebContents2Id, url1),
SelectTab(kTabStripElementId, 0), EnterSplitView(0, 1),
WaitForShow(kToolbarSplitTabsToolbarButtonElementId),
PressButton(kToolbarSplitTabsToolbarButtonElementId),
WaitForShow(SplitTabMenuModel::kReversePositionMenuItem),
CheckMenuString(SplitTabMenuModel::kReversePositionMenuItem,
IDS_SPLIT_TAB_REVERSE_VIEWS),
CheckMenuIcon(SplitTabMenuModel::kReversePositionMenuItem,
kSplitSceneRightIcon),
ClickActiveTabInSplit(),
WaitForHide(SplitTabMenuModel::kReversePositionMenuItem),
// Change the focus and reopen the menu
FocusInactiveTabInSplit(),
PressButton(kToolbarSplitTabsToolbarButtonElementId),
WaitForShow(SplitTabMenuModel::kReversePositionMenuItem),
CheckMenuString(SplitTabMenuModel::kReversePositionMenuItem,
IDS_SPLIT_TAB_REVERSE_VIEWS),
CheckMenuIcon(SplitTabMenuModel::kReversePositionMenuItem,
kSplitSceneLeftIcon));
}
IN_PROC_BROWSER_TEST_F(SplitTabButtonInteractiveTest, ReverseSplitTabPosition) {
RunTestSequence(
InstrumentTab(kWebContents1Id),
AddInstrumentedTab(kWebContents2Id, GetTestUrl("/links.html")),
SelectTab(kTabStripElementId, 0), EnterSplitView(0, 1),
WaitForShow(kToolbarSplitTabsToolbarButtonElementId),
// The newly created split tab should be active
CheckSplitTabButtonIcon(kSplitSceneLeftIcon),
NavigateWebContents(kWebContents1Id, GetTestUrl()),
// Reversing the tab positions should move the active tab to the left.
PressButton(kToolbarSplitTabsToolbarButtonElementId),
WaitForShow(SplitTabMenuModel::kReversePositionMenuItem),
ObserveState(kActiveTabChanged, browser()->tab_strip_model()),
SelectMenuItem(SplitTabMenuModel::kReversePositionMenuItem),
WaitForState(kActiveTabChanged, true),
CheckSplitTabButtonIcon(kSplitSceneRightIcon),
CheckResult(
[this]() {
TabStripModel* const tab_strip_model = browser()->tab_strip_model();
return tab_strip_model
->GetWebContentsAt(tab_strip_model->active_index())
->GetURL();
},
GetTestUrl()));
}
IN_PROC_BROWSER_TEST_F(SplitTabButtonInteractiveTest, CloseLeftRightTabs) {
RunTestSequence(InstrumentTab(kWebContents1Id),
AddInstrumentedTab(kWebContents2Id, GetTestUrl()),
SelectTab(kTabStripElementId, 0), EnterSplitView(0, 1),
WaitForShow(kToolbarSplitTabsToolbarButtonElementId),
// Open the button's context menu.
PressButton(kToolbarSplitTabsToolbarButtonElementId),
WaitForShow(SplitTabMenuModel::kCloseStartTabMenuItem),
// Selecting close left menu item should close the left tab
EnsureNotPresent(SplitTabMenuModel::kCloseMenuItem),
SelectMenuItem(SplitTabMenuModel::kCloseStartTabMenuItem),
WaitForHide(kWebContents1Id),
WaitForHide(kToolbarSplitTabsToolbarButtonElementId),
CheckTabCount(1), EnsurePresent(kWebContents2Id),
// Create a new split with a third tab.
AddInstrumentedTab(kWebContents3Id, GetTestUrl()),
SelectTab(kTabStripElementId, 0), EnterSplitView(0, 1),
WaitForShow(kToolbarSplitTabsToolbarButtonElementId),
PressButton(kToolbarSplitTabsToolbarButtonElementId),
WaitForShow(SplitTabMenuModel::kCloseEndTabMenuItem),
// Selecting close right menu item should close the right tab
SelectMenuItem(SplitTabMenuModel::kCloseEndTabMenuItem),
WaitForHide(kWebContents3Id),
WaitForHide(kToolbarSplitTabsToolbarButtonElementId),
CheckTabCount(1), EnsurePresent(kWebContents2Id));
}
IN_PROC_BROWSER_TEST_F(SplitTabButtonInteractiveTest, ExitSplit) {
RunTestSequence(
AddInstrumentedTab(kWebContents2Id, GetTestUrl()),
SelectTab(kTabStripElementId, 0), EnterSplitView(0, 1),
WaitForShow(kToolbarSplitTabsToolbarButtonElementId),
// Open the button's context menu.
PressButton(kToolbarSplitTabsToolbarButtonElementId),
WaitForShow(SplitTabMenuModel::kReversePositionMenuItem),
CheckTabInSplit(0, true), CheckTabInSplit(1, true),
// The split tabs should be separated after selecting the menu item.
SelectMenuItem(SplitTabMenuModel::kExitSplitMenuItem),
WaitForHide(kToolbarSplitTabsToolbarButtonElementId),
CheckTabInSplit(0, false), CheckTabInSplit(1, false),
CheckResult(
[this]() { return browser()->tab_strip_model()->active_index(); },
0));
}
IN_PROC_BROWSER_TEST_F(SplitTabButtonInteractiveTest, ButtonUpdatesOnSplit) {
RunTestSequence(AddInstrumentedTab(kWebContents2Id, GetTestUrl()),
SelectTab(kTabStripElementId, 0), EnterSplitView(0, 1),
WaitForShow(kToolbarSplitTabsToolbarButtonElementId),
SetOnIncompatibleAction(
OnIncompatibleAction::kIgnoreAndContinue,
"Screenshot can only run in pixel_tests on Windows."),
Screenshot(kToolbarSplitTabsToolbarButtonElementId,
"SplitTabButton", "6628632"));
}
|