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 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <optional>
#include "base/callback_list.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/test/bind.h"
#include "build/build_config.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/browser_window.h"
#include "chrome/browser/ui/toolbar/app_menu_model.h"
#include "chrome/browser/ui/views/bubble/webui_bubble_dialog_view.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/omnibox/omnibox_view_views.h"
#include "chrome/browser/ui/views/side_panel/side_panel_entry.h"
#include "chrome/browser/ui/views/toolbar/browser_app_menu_button.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/interaction/interactive_browser_test.h"
#include "chrome/test/interaction/tracked_element_webcontents.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/test/browser_test.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/interaction/element_tracker.h"
#include "ui/base/interaction/interaction_sequence.h"
#include "ui/base/interaction/state_observer.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/page_transition_types.h"
#include "ui/base/window_open_disposition.h"
#include "ui/display/screen.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/views/bubble/bubble_border.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/controls/webview/webview.h"
#include "ui/views/event_monitor.h"
#include "ui/views/interaction/element_tracker_views.h"
#include "ui/views/interaction/interaction_sequence_views.h"
#include "ui/views/interaction/widget_focus_observer.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/layout/flex_layout_view.h"
#include "ui/views/layout/layout_types.h"
#include "ui/views/test/widget_activation_waiter.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/widget/widget_observer.h"
#include "url/gurl.h"
#if BUILDFLAG(IS_WIN)
#include "base/win/windows_version.h"
#endif
namespace {
constexpr char kDocumentWithNamedElement[] = "/select.html";
constexpr char kDocumentWithTitle[] = "/title3.html";
constexpr char kDocumentWithTextField[] = "/form_interaction.html";
}
class InteractiveBrowserTestUiTest : public InteractiveBrowserTest {
public:
InteractiveBrowserTestUiTest() = default;
~InteractiveBrowserTestUiTest() override = default;
void SetUp() override {
set_open_about_blank_on_browser_launch(true);
ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
InteractiveBrowserTest::SetUp();
}
void SetUpOnMainThread() override {
InteractiveBrowserTest::SetUpOnMainThread();
embedded_test_server()->StartAcceptingConnections();
}
void TearDownOnMainThread() override {
EXPECT_TRUE(embedded_test_server()->ShutdownAndWaitUntilComplete());
InteractiveBrowserTest::TearDownOnMainThread();
}
};
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest,
PressButtonAndMouseMoveClick) {
RelativePositionSpecifier pos = CenterPoint();
#if BUILDFLAG(IS_WIN)
if (base::win::OSInfo::GetInstance()->version() < base::win::Version::WIN11) {
// Handler for http://crbug.com/392854216 (menu may overlap button).
pos = base::BindOnce([](ui::TrackedElement* el) {
gfx::Rect bounds = el->GetScreenBounds();
auto* const menu_item =
ui::ElementTracker::GetElementTracker()->GetElementInAnyContext(
AppMenuModel::kMoreToolsMenuItem);
const gfx::Rect widget_bounds =
menu_item->AsA<views::TrackedElementViews>()
->view()
->GetWidget()
->GetWindowBoundsInScreen();
// Create a rectangle where all points are strictly inside the original
// bounds.
bounds.Inset(gfx::Insets::TLBR(1, 1, 2, 2));
// Test points around the rectangle to find one that does not intersect
// the menu widget.
for (const auto& point :
{bounds.CenterPoint(), bounds.bottom_center(), bounds.left_center(),
bounds.right_center(), bounds.origin(), bounds.top_right(),
bounds.bottom_right(), bounds.bottom_left()}) {
if (!widget_bounds.Contains(point)) {
return point;
}
}
NOTREACHED() << "Menu widget ()" << widget_bounds.ToString()
<< ") significantly overlaps menu button ("
<< bounds.ToString() << ") cannot target button.";
});
}
#endif
RunTestSequence(
// Ensure the mouse isn't over the app menu button.
MoveMouseTo(kTabStripElementId),
// Simulate press of the menu button and ensure the button activates and
// the menu appears.
PressButton(kToolbarAppMenuButtonElementId),
WaitForShow(AppMenuModel::kMoreToolsMenuItem),
// Move the mouse to the button and click it. This will hide the menu.
MoveMouseTo(kToolbarAppMenuButtonElementId, std::move(pos)), ClickMouse(),
WaitForHide(AppMenuModel::kMoreToolsMenuItem));
}
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest, TestNameAndDrag) {
const char kWebContentsName[] = "WebContents";
gfx::Point p1;
gfx::Point p2;
auto p2gen = base::BindLambdaForTesting([&](ui::TrackedElement* el) {
p2 = el->AsA<views::TrackedElementViews>()
->view()
->GetBoundsInScreen()
.bottom_right() -
gfx::Vector2d(5, 5);
return p2;
});
RunTestSequence(
// Name the browser's primary webview and calculate a point in its upper
// left.
NameViewRelative(
kBrowserViewElementId, kWebContentsName,
base::BindOnce([](BrowserView* browser_view) -> views::View* {
return browser_view->contents_web_view();
})),
WithView(kWebContentsName,
base::BindLambdaForTesting([&p1](views::View* view) {
p1 = view->GetBoundsInScreen().origin() + gfx::Vector2d(5, 5);
})),
// Move the mouse to the point. Use the gfx::Point* version so we can
// dynamically receive the value calculated in the previous step.
MoveMouseTo(std::ref(p1)),
// Verify that the mouse has been moved to the correct point.
Check(base::BindLambdaForTesting([&]() {
gfx::Rect rect(p1, gfx::Size());
rect.Inset(-1);
const gfx::Point point =
display::Screen::GetScreen()->GetCursorScreenPoint();
if (!rect.Contains(point)) {
LOG(ERROR) << "Expected cursor pos " << point.ToString()
<< " to be roughly " << p1.ToString();
return false;
}
return true;
})),
// Drag the mouse to a point returned from a generator function. The
// function also stores the result in |p2|.
DragMouseTo(kWebContentsName, std::move(p2gen), false),
// Verify that the mouse moved to the correct point.
Check(base::BindLambdaForTesting([&]() {
gfx::Rect rect(p2, gfx::Size());
rect.Inset(-1);
const gfx::Point point =
display::Screen::GetScreen()->GetCursorScreenPoint();
if (!rect.Contains(point)) {
LOG(ERROR) << "Expected cursor pos " << point.ToString()
<< " to be roughly " << p2.ToString();
return false;
}
return true;
})),
// Release the mouse button.
ReleaseMouse());
}
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest,
MouseToNewWindowAndDoActionsInSameContext) {
Browser* const incognito = CreateIncognitoBrowser();
RunTestSequence(
InContext(incognito->window()->GetElementContext(),
WaitForShow(kBrowserViewElementId)),
InSameContext(
ActivateSurface(kBrowserViewElementId),
MoveMouseTo(kToolbarAppMenuButtonElementId), ClickMouse(),
SelectMenuItem(AppMenuModel::kDownloadsMenuItem),
WaitForHide(AppMenuModel::kDownloadsMenuItem),
// These two types of actions use PostTask() internally and bounce off
// the pivot element. Make sure they still work in a "InSameContext".
EnsureNotPresent(AppMenuModel::kDownloadsMenuItem),
// Make sure this picks up the correct button, since it was after a
// string of non-element-specific actions.
WithElement(kToolbarAppMenuButtonElementId,
base::BindOnce(base::BindLambdaForTesting(
[incognito](ui::TrackedElement* el) {
EXPECT_EQ(incognito->window()->GetElementContext(),
el->context());
})))));
}
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest,
MouseToNewWindowAndDoActionsInSpecificContext) {
auto* const incognito = CreateIncognitoBrowser();
RunTestSequence(InContext(
incognito->window()->GetElementContext(),
ActivateSurface(kBrowserViewElementId),
MoveMouseTo(kToolbarAppMenuButtonElementId), ClickMouse(),
SelectMenuItem(AppMenuModel::kDownloadsMenuItem),
WaitForHide(AppMenuModel::kDownloadsMenuItem),
// These two types of actions use PostTask() internally and
// bounce off the pivot element. Make sure they still work in a
// "InSameContext".
EnsureNotPresent(AppMenuModel::kDownloadsMenuItem),
// Make sure this picks up the correct button, since it was
// after a string of non-element-specific actions.
WithElement(kToolbarAppMenuButtonElementId,
base::BindOnce(base::BindLambdaForTesting(
[incognito](ui::TrackedElement* el) {
EXPECT_EQ(incognito->window()->GetElementContext(),
el->context());
})))));
}
// Tests whether ActivateSurface() can correctly bring a browser window to the
// front so that mouse input can be sent to it.
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest, ActivateMultipleSurfaces) {
auto* const incognito = CreateIncognitoBrowser();
RunTestSequence(
SetOnIncompatibleAction(OnIncompatibleAction::kHaltTest,
"Some Linux window managers do not allow "
"programmatically raising/activating windows. "
"This invalidates the rest of the test."),
InContext(incognito->window()->GetElementContext(),
ActivateSurface(kBrowserViewElementId),
MoveMouseTo(kToolbarAppMenuButtonElementId), ClickMouse(),
SelectMenuItem(AppMenuModel::kDownloadsMenuItem),
WaitForHide(AppMenuModel::kDownloadsMenuItem)),
ActivateSurface(kBrowserViewElementId),
MoveMouseTo(kToolbarAppMenuButtonElementId), ClickMouse(),
WaitForShow(AppMenuModel::kDownloadsMenuItem));
}
// Tests whether ActivateSurface() results in kCurrentWidgetFocus updating
// correctly.
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest,
WatchForBrowserActivation) {
auto* const incognito = CreateIncognitoBrowser();
RunTestSequence(
SetOnIncompatibleAction(OnIncompatibleAction::kHaltTest,
"Some Linux window managers do not allow "
"programmatically raising/activating windows. "
"This invalidates the rest of the test."),
ObserveState(views::test::kCurrentWidgetFocus),
InContext(incognito->window()->GetElementContext(),
ActivateSurface(kBrowserViewElementId),
MoveMouseTo(kToolbarAppMenuButtonElementId), ClickMouse(),
SelectMenuItem(AppMenuModel::kDownloadsMenuItem),
WaitForHide(AppMenuModel::kDownloadsMenuItem)),
ActivateSurface(kBrowserViewElementId),
WaitForState(views::test::kCurrentWidgetFocus, [this]() {
return BrowserView::GetBrowserViewForBrowser(browser())
->GetWidget()
->GetNativeView();
}));
}
// Tests whether ActivateSurface() results in kCurrentWidgetFocus updating
// correctly when targeting a tab's web contents.
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest,
WatchForTabWebContentsActivation) {
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kWebContentsElementId);
auto* const incognito = CreateIncognitoBrowser();
RunTestSequence(
SetOnIncompatibleAction(OnIncompatibleAction::kHaltTest,
"Some Linux window managers do not allow "
"programmatically raising/activating windows. "
"This invalidates the rest of the test."),
ObserveState(views::test::kCurrentWidgetFocus),
InContext(incognito->window()->GetElementContext(),
ActivateSurface(kBrowserViewElementId),
MoveMouseTo(kToolbarAppMenuButtonElementId), ClickMouse(),
SelectMenuItem(AppMenuModel::kDownloadsMenuItem),
WaitForHide(AppMenuModel::kDownloadsMenuItem)),
InstrumentTab(kWebContentsElementId),
ActivateSurface(kWebContentsElementId),
WaitForState(views::test::kCurrentWidgetFocus, [this]() {
return BrowserView::GetBrowserViewForBrowser(browser())
->GetWidget()
->GetNativeView();
}));
}
// TODO(crbug.com/330095872): Flaky on linux-chromeos-rel and Linux ChromiumOS
// MSan.
#if BUILDFLAG(IS_CHROMEOS)
#define MAYBE_WatchForNonTabWebContentsActivation \
DISABLED_WatchForNonTabWebContentsActivation
#else
#define MAYBE_WatchForNonTabWebContentsActivation \
WatchForNonTabWebContentsActivation
#endif
// Tests whether ActivateSurface() results in kCurrentWidgetFocus updating
// correctly when targeting a non-tab web contents.
//
// TODO(crbug.com/40069026): These tests can be kind of hairy and we're working
// on making sure these primitives play nice together and do not flake. If you
// see a flake, first, note that these are edge case tests for new test
// infrastructure and do not directly affect Chrome stability. Next, please:
// - Reopen or add to the attached bug.
// - Make sure it is assigned to dfried@chromium.org or another
// chrome/test/interaction owner.
// - [Selectively] disable the test on the offending platforms.
//
// Thank you for working with us to make Chrome test infrastructure better!
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest,
MAYBE_WatchForNonTabWebContentsActivation) {
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kWebContentsElementId);
constexpr char kWebViewName[] = "Web View";
auto* const incognito = CreateIncognitoBrowser();
gfx::NativeView expected_view = gfx::NativeView();
RunTestSequence(
SetOnIncompatibleAction(OnIncompatibleAction::kHaltTest,
"Some Linux window managers do not allow "
"programmatically raising/activating windows. "
"This invalidates the rest of the test."),
ObserveState(views::test::kCurrentWidgetFocus),
InContext(incognito->window()->GetElementContext(),
ActivateSurface(kBrowserViewElementId),
MoveMouseTo(kToolbarAppMenuButtonElementId), ClickMouse(),
SelectMenuItem(AppMenuModel::kDownloadsMenuItem),
WaitForHide(AppMenuModel::kDownloadsMenuItem)),
PressButton(kTabSearchButtonElementId),
WaitForShow(kTabSearchBubbleElementId),
NameDescendantViewByType<views::WebView>(kTabSearchBubbleElementId,
kWebViewName),
InstrumentNonTabWebView(kWebContentsElementId, kWebViewName),
ActivateSurface(kWebContentsElementId),
WithView(kTabSearchBubbleElementId,
[&expected_view](views::View* view) {
expected_view = view->GetWidget()->GetNativeView();
}),
WaitForState(views::test::kCurrentWidgetFocus, std::ref(expected_view)));
}
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest,
WebPageNavigateStateAndLocation) {
const GURL url = embedded_test_server()->GetURL(kDocumentWithNamedElement);
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kWebPageId);
DEFINE_LOCAL_CUSTOM_ELEMENT_EVENT_TYPE(kElementReadyEvent);
const DeepQuery kDeepQuery{"#select"};
StateChange state_change;
state_change.event = kElementReadyEvent;
state_change.type = StateChange::Type::kExists;
state_change.where = kDeepQuery;
RunTestSequence(
InstrumentTab(kWebPageId),
// Load a different page. We could use NavigateWebContents() but that's
// tested elsewhere and this test will test WaitForWebContentsNavigation()
// instead.
WithElement(kWebPageId, base::BindOnce(
[](GURL url, ui::TrackedElement* el) {
// This also provides an opportunity to test
// AsInstrumentedWebContents().
auto* const tab =
AsInstrumentedWebContents(el);
tab->LoadPage(url);
},
url)),
WaitForWebContentsNavigation(kWebPageId, url),
// Wait for an expected element to be present and move the mouse to that
// element.
WaitForStateChange(kWebPageId, state_change),
MoveMouseTo(kWebPageId, kDeepQuery),
// Verify that the mouse cursor is now in the web contents.
Check(base::BindLambdaForTesting([&]() {
BrowserView* const browser_view =
BrowserView::GetBrowserViewForBrowser(browser());
const gfx::Rect web_contents_bounds =
browser_view->contents_web_view()->GetBoundsInScreen();
const gfx::Point point =
display::Screen::GetScreen()->GetCursorScreenPoint();
if (!web_contents_bounds.Contains(point)) {
LOG(ERROR) << "Expected cursor pos " << point.ToString() << " to in "
<< web_contents_bounds.ToString();
return false;
}
return true;
})));
}
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest,
InAnyContextAndEnsureNotPresent) {
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kBrowserPageId);
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kIncognitoPageId);
Browser* const incognito_browser = this->CreateIncognitoBrowser();
// Run the test in the context of the incognito browser.
RunTestSequenceInContext(
incognito_browser->window()->GetElementContext(),
// Instrument the tabs but do not force them to load.
InstrumentTab(kIncognitoPageId, std::nullopt, CurrentBrowser(),
/* wait_for_ready =*/false),
InstrumentTab(kBrowserPageId, std::nullopt, browser(),
/* wait_for_ready =*/false),
// Wait for the pages to load. Manually specify that the incognito page
// must be in the default context (otherwise, this verb defaults to being
// context-agnostic).
WaitForWebContentsReady(kIncognitoPageId)
.SetContext(ui::InteractionSequence::ContextMode::kInitial),
WaitForWebContentsReady(kBrowserPageId),
// The regular browser page is not present if we do not specify
// InAnyContext().
EnsureNotPresent(kBrowserPageId),
// But we can find a page in the correct context even if we specify
// InAnyContext().
InAnyContext(EnsurePresent(kIncognitoPageId)));
}
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest,
InstrumentNonTabAsTestStep) {
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kWebContentsId);
const char kTabSearchWebViewName[] = "Tab Search WebView";
RunTestSequence(
PressButton(kTabSearchButtonElementId),
WaitForShow(kTabSearchBubbleElementId),
NameChildViewByType<views::WebView>(kTabSearchBubbleElementId,
kTabSearchWebViewName),
InstrumentNonTabWebView(kWebContentsId, kTabSearchWebViewName));
}
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest,
SendAcceleratorToWebContents) {
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kWebContentsId);
DEFINE_LOCAL_CUSTOM_ELEMENT_EVENT_TYPE(kClearAllClickEvent);
const DeepQuery kClearAllDownloadsButton = {"downloads-manager",
"downloads-toolbar", "#clearAll"};
const ui::Accelerator kClickWebButtonAccelerator(ui::KeyboardCode::VKEY_SPACE,
ui::EF_NONE);
StateChange clear_all_downloads_click;
clear_all_downloads_click.type = StateChange::Type::kExists;
clear_all_downloads_click.where = kClearAllDownloadsButton;
clear_all_downloads_click.event = kClearAllClickEvent;
RunTestSequence(
InstrumentTab(kWebContentsId),
PressButton(kToolbarAppMenuButtonElementId),
SelectMenuItem(AppMenuModel::kDownloadsMenuItem),
WaitForWebContentsNavigation(kWebContentsId,
GURL(chrome::kChromeUIDownloadsURL)),
FocusElement(kWebContentsId),
ExecuteJsAt(kWebContentsId, kClearAllDownloadsButton, "el => el.focus()"),
SendAccelerator(kWebContentsId, kClickWebButtonAccelerator),
WaitForStateChange(kWebContentsId, clear_all_downloads_click));
}
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest, SendKeyToWebContents) {
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kWebContentsId);
const GURL url = embedded_test_server()->GetURL(kDocumentWithTextField);
const DeepQuery kTextField = {"#value"};
RunTestSequence(
InstrumentTab(kWebContentsId), NavigateWebContents(kWebContentsId, url),
FocusWebContents(kWebContentsId),
ExecuteJsAt(kWebContentsId, kTextField,
"el => { el.focus(); el.value = ''; }"),
SendKeyPress(kWebContentsId, ui::VKEY_A),
SendKeyPress(kWebContentsId, ui::VKEY_B, ui::EF_SHIFT_DOWN),
CheckJsResultAt(kWebContentsId, kTextField, "el => el.value", u"aB"));
}
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest, FocusElement) {
RunTestSequence(
FocusElement(kToolbarAppMenuButtonElementId),
CheckViewProperty(kToolbarAppMenuButtonElementId, &views::View::HasFocus,
true),
FocusElement(kOmniboxElementId),
CheckViewProperty(kOmniboxElementId, &views::View::HasFocus, true));
}
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest, SendKeyPress) {
RunTestSequence(
FocusElement(kOmniboxElementId),
SendKeyPress(kOmniboxElementId, ui::VKEY_A),
SendKeyPress(kOmniboxElementId, ui::VKEY_B, ui::EF_SHIFT_DOWN),
CheckViewProperty(kOmniboxElementId, &OmniboxViewViews::GetText, u"aB"));
}
// Simple bubble containing a WebView. Allows us to simulate swapping out one
// WebContents for another.
class WebBubbleView : public views::BubbleDialogDelegateView {
METADATA_HEADER(WebBubbleView, views::BubbleDialogDelegateView)
public:
~WebBubbleView() override = default;
// Creates a bubble with a WebView and loads `url` in the view.
static WebBubbleView* CreateBubble(Browser* browser, GURL url) {
BrowserView* const browser_view =
BrowserView::GetBrowserViewForBrowser(browser);
auto bubble_ptr = base::WrapUnique(
new WebBubbleView(browser_view->toolbar(), browser->profile(), url));
auto* const bubble = bubble_ptr.get();
views::BubbleDialogDelegateView::CreateBubble(std::move(bubble_ptr))
->Show();
return bubble;
}
// Swaps out the current WebContents for a new one and loads `url` into that
// new WebContents.
void SwapWebContents(GURL url) {
owned_web_contents_ = content::WebContents::Create(
content::WebContents::CreateParams(profile_));
web_view_->SetWebContents(owned_web_contents_.get());
web_view_->LoadInitialURL(url);
}
// Gets the WebView displayed by this bubble.
views::WebView* web_view() { return web_view_; }
private:
WebBubbleView(views::View* anchor_view, Profile* profile, GURL url)
: BubbleDialogDelegateView(anchor_view, views::BubbleBorder::TOP_LEFT),
profile_(profile) {
SetLayoutManager(std::make_unique<views::FillLayout>());
web_view_ = AddChildView(std::make_unique<views::WebView>(profile));
web_view_->LoadInitialURL(url);
}
// views::BubbleDialogDelegateView:
gfx::Size CalculatePreferredSize(
const views::SizeBounds& available_size) const override {
// Need a large enough bubble that the WebView has size to render.
return gfx::Size(300, 400);
}
const raw_ptr<Profile> profile_;
raw_ptr<views::WebView> web_view_;
std::unique_ptr<content::WebContents> owned_web_contents_;
};
BEGIN_METADATA(WebBubbleView)
END_METADATA
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest,
SwappingWebViewWebContentsTreatedAsNavigation) {
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kWebContentsId);
const GURL url = embedded_test_server()->GetURL(kDocumentWithNamedElement);
const GURL url2 = embedded_test_server()->GetURL(kDocumentWithTitle);
auto* const bubble = WebBubbleView::CreateBubble(browser(), url);
RunTestSequence(InstrumentNonTabWebView(kWebContentsId, bubble->web_view()),
// Need to flush here because we're still responding to the
// original WebContents being shown, so we can't destroy the
// WebContents until the call resolves.
Do([&]() { bubble->SwapWebContents(url2); }),
WaitForWebContentsNavigation(kWebContentsId, url2));
bubble->GetWidget()->CloseNow();
}
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest,
WaitForWebContentsPainted) {
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kWebContentsId);
const GURL url = embedded_test_server()->GetURL(kDocumentWithNamedElement);
auto* const bubble = WebBubbleView::CreateBubble(browser(), url);
RunTestSequence(
InstrumentNonTabWebView(kWebContentsId, bubble->web_view(), false),
// This should wait for the element to appear and then paint.
WaitForWebContentsPainted(kWebContentsId),
// This should be more or less a no-op.
WaitForWebContentsPainted(kWebContentsId));
bubble->GetWidget()->CloseNow();
}
// Ensure that the initial active window is detected by the focus observer.
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestUiTest, InitialWindowActive) {
auto* const widget =
BrowserView::GetBrowserViewForBrowser(browser())->GetWidget();
views::test::WaitForWidgetActive(widget, true);
RunTestSequence(ObserveState(views::test::kCurrentWidgetFocus),
WaitForState(views::test::kCurrentWidgetFocus,
[widget]() { return widget->GetNativeView(); }));
}
namespace {
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kHoverView1Id);
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kHoverView2Id);
DEFINE_LOCAL_ELEMENT_IDENTIFIER_VALUE(kHoverView3Id);
class HoverDetectionView : public views::View {
METADATA_HEADER(HoverDetectionView, views::View)
public:
~HoverDetectionView() override = default;
// views::View:
gfx::Size CalculatePreferredSize(const views::SizeBounds&) const override {
return gfx::Size(200, 50);
}
void OnMouseMoved(const ui::MouseEvent& event) override {
on_mouse_move_callbacks_.Notify(event);
}
void OnMouseEntered(const ui::MouseEvent& event) override {
on_mouse_move_callbacks_.Notify(event);
}
void OnMouseExited(const ui::MouseEvent& event) override {
on_mouse_move_callbacks_.Notify(event);
}
using MouseMoveCallback =
base::RepeatingCallback<void(const ui::MouseEvent&)>;
auto AddOnMouseMoveCallback(MouseMoveCallback callback) {
return on_mouse_move_callbacks_.Add(callback);
}
private:
base::RepeatingCallbackList<void(const ui::MouseEvent&)>
on_mouse_move_callbacks_;
};
class LastHoverEventObserver
: public ui::test::StateObserver<std::set<ui::EventType>> {
public:
explicit LastHoverEventObserver(HoverDetectionView* view)
: subscription_(view->AddOnMouseMoveCallback(
base::BindRepeating(&LastHoverEventObserver::OnHoverEvent,
base::Unretained(this)))) {}
~LastHoverEventObserver() override = default;
void OnHoverEvent(const ui::MouseEvent& event) {
observed_events_.insert(event.type());
OnStateObserverStateChanged(observed_events_);
}
private:
base::CallbackListSubscription subscription_;
std::set<ui::EventType> observed_events_;
};
DEFINE_LOCAL_STATE_IDENTIFIER_VALUE(LastHoverEventObserver, kHoverView1State);
DEFINE_LOCAL_STATE_IDENTIFIER_VALUE(LastHoverEventObserver, kHoverView2State);
DEFINE_LOCAL_STATE_IDENTIFIER_VALUE(LastHoverEventObserver, kHoverView3State);
BEGIN_METADATA(HoverDetectionView)
END_METADATA
} // namespace
class HoverDetectionBubbleView : public views::FlexLayoutView,
public views::BubbleDialogDelegate {
METADATA_HEADER(HoverDetectionBubbleView, views::FlexLayoutView)
public:
explicit HoverDetectionBubbleView(views::View* anchor_view)
: BubbleDialogDelegate(anchor_view, views::BubbleBorder::TOP_RIGHT) {
SetOwnedByWidget(OwnedByWidgetPassKey());
auto* view = AddChildView(std::make_unique<HoverDetectionView>());
view->SetProperty(views::kElementIdentifierKey, kHoverView1Id);
views_.push_back(view);
view = AddChildView(std::make_unique<HoverDetectionView>());
view->SetProperty(views::kElementIdentifierKey, kHoverView2Id);
views_.push_back(view);
view = AddChildView(std::make_unique<HoverDetectionView>());
view->SetProperty(views::kElementIdentifierKey, kHoverView3Id);
views_.push_back(view);
SetOrientation(views::LayoutOrientation::kVertical);
}
~HoverDetectionBubbleView() override { views_.clear(); }
views::View* GetContentsView() override { return this; }
HoverDetectionView* view(size_t idx) { return views_[idx].get(); }
private:
std::vector<raw_ptr<HoverDetectionView>> views_;
};
BEGIN_METADATA(HoverDetectionBubbleView)
END_METADATA
class InteractiveBrowserTestHoverUiTest : public InteractiveBrowserTestUiTest {
public:
InteractiveBrowserTestHoverUiTest() = default;
~InteractiveBrowserTestHoverUiTest() override = default;
void SetUpOnMainThread() override {
InteractiveBrowserTestUiTest::SetUpOnMainThread();
// Move the mouse somewhere completely outside where the dialog will show.
auto* const browser_view = BrowserView::GetBrowserViewForBrowser(browser());
mouse_util().PerformGestures(
{browser_view->GetNativeWindow(), /*force_async=*/false},
views::test::InteractionTestUtilMouse::MoveTo(
browser_view->GetBoundsInScreen().origin() +
gfx::Vector2d(10, 10)));
// Create and show the bubble.
auto* const anchor_view =
views::ElementTrackerViews::GetInstance()->GetFirstMatchingView(
kToolbarAppMenuButtonElementId,
browser()->window()->GetElementContext());
CHECK(anchor_view);
auto bubble_view = std::make_unique<HoverDetectionBubbleView>(anchor_view);
bubble_view_ = bubble_view.get();
bubble_widget_ = base::WrapUnique(views::BubbleDialogDelegate::CreateBubble(
std::move(bubble_view), views::Widget::InitParams::CLIENT_OWNS_WIDGET));
bubble_widget_->Show();
bubble_view_->SizeToContents();
}
void TearDownOnMainThread() override {
bubble_view_ = nullptr;
bubble_widget_.reset();
InteractiveBrowserTestUiTest::TearDownOnMainThread();
}
protected:
raw_ptr<HoverDetectionBubbleView> bubble_view_ = nullptr;
std::unique_ptr<views::Widget> bubble_widget_;
};
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestHoverUiTest, MoveMouseHoversView) {
RunTestSequence(
WaitForShow(kHoverView1Id),
ObserveState(kHoverView1State, bubble_view_->view(0)),
MoveMouseTo(kHoverView1Id),
WaitForState(kHoverView1State,
testing::UnorderedElementsAre(ui::EventType::kMouseEntered,
ui::EventType::kMouseMoved)));
}
IN_PROC_BROWSER_TEST_F(InteractiveBrowserTestHoverUiTest,
MoveMouseHoverMultipleViews) {
RunTestSequence(
WaitForShow(kHoverView1Id),
ObserveState(kHoverView1State, bubble_view_->view(0)),
ObserveState(kHoverView2State, bubble_view_->view(1)),
ObserveState(kHoverView3State, bubble_view_->view(2)),
MoveMouseTo(kHoverView1Id),
WaitForState(kHoverView1State,
testing::UnorderedElementsAre(ui::EventType::kMouseEntered,
ui::EventType::kMouseMoved)),
MoveMouseTo(kHoverView2Id),
WaitForState(kHoverView1State,
testing::Contains(ui::EventType::kMouseExited)),
WaitForState(kHoverView2State,
testing::UnorderedElementsAre(ui::EventType::kMouseEntered,
ui::EventType::kMouseMoved)),
MoveMouseTo(kHoverView3Id),
WaitForState(kHoverView2State,
testing::Contains(ui::EventType::kMouseExited)),
WaitForState(kHoverView3State,
testing::UnorderedElementsAre(ui::EventType::kMouseEntered,
ui::EventType::kMouseMoved)),
MoveMouseTo(
kBrowserViewElementId, base::BindOnce([](ui::TrackedElement* el) {
return el->GetScreenBounds().origin() + gfx::Vector2d(10, 10);
})),
WaitForState(kHoverView3State,
testing::Contains(ui::EventType::kMouseExited)));
}
|