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 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
|
// 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/omnibox/omnibox_view_views.h"
#include <stddef.h>
#include "base/feature_list.h"
#include "base/memory/raw_ptr.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/app/chrome_command_ids.h"
#include "chrome/browser/external_protocol/external_protocol_handler.h"
#include "chrome/browser/interstitials/security_interstitial_page_test_utils.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ssl/typed_navigation_upgrade_throttle.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/location_bar/location_bar.h"
#include "chrome/browser/ui/view_ids.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/location_bar/location_bar_view.h"
#include "chrome/browser/ui/views/omnibox/omnibox_popup_view_views.h"
#include "chrome/browser/ui/views/toolbar/toolbar_view.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/omnibox/browser/autocomplete_match.h"
#include "components/omnibox/browser/omnibox_controller.h"
#include "components/omnibox/browser/omnibox_edit_model.h"
#include "components/omnibox/browser/omnibox_triggered_feature_service.h"
#include "components/omnibox/browser/test_scheme_classifier.h"
#include "components/security_interstitials/core/omnibox_https_upgrade_metrics.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/url_loader_interceptor.h"
#include "third_party/blink/public/common/chrome_debug_urls.h"
#include "ui/accessibility/accessibility_features.h"
#include "ui/accessibility/ax_action_data.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/base/ime/init/input_method_factory.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/ime/mock_input_method.h"
#include "ui/base/ime/text_edit_commands.h"
#include "ui/base/ime/text_input_client.h"
#include "ui/base/test/ui_controls.h"
#include "ui/base/ui_base_switches.h"
#include "ui/events/event_processor.h"
#include "ui/events/event_utils.h"
#include "ui/events/test/event_generator.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/textfield/textfield_test_api.h"
#include "ui/views/views_features.h"
#if defined(USE_AURA)
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "chrome/browser/ui/views/accessibility/uia_accessibility_event_waiter.h"
#endif
namespace {
void SetClipboardText(ui::ClipboardBuffer buffer, const std::u16string& text) {
ui::ScopedClipboardWriter(buffer).WriteText(text);
}
} // namespace
class OmniboxViewViewsTest : public InProcessBrowserTest {
public:
OmniboxViewViewsTest(const OmniboxViewViewsTest&) = delete;
OmniboxViewViewsTest& operator=(const OmniboxViewViewsTest&) = delete;
protected:
OmniboxViewViewsTest() = default;
~OmniboxViewViewsTest() override = default;
OmniboxTriggeredFeatureService* triggered_feature_service() {
return &triggered_feature_service_;
}
static void GetOmniboxViewForBrowser(const Browser* browser,
OmniboxView** omnibox_view) {
BrowserWindow* window = browser->window();
ASSERT_TRUE(window);
LocationBar* location_bar = window->GetLocationBar();
ASSERT_TRUE(location_bar);
*omnibox_view = location_bar->GetOmniboxView();
ASSERT_TRUE(*omnibox_view);
}
// Move the mouse to the center of the browser window and left-click.
void ClickBrowserWindowCenter() {
ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(
BrowserView::GetBrowserViewForBrowser(
browser())->GetBoundsInScreen().CenterPoint()));
ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(ui_controls::LEFT,
ui_controls::DOWN));
ASSERT_TRUE(
ui_test_utils::SendMouseEventsSync(ui_controls::LEFT, ui_controls::UP));
}
// Press and release the mouse at the specified locations. If
// |release_offset| differs from |press_offset|, the mouse will be moved
// between the press and release.
void Click(ui_controls::MouseButton button,
const gfx::Point& press_location,
const gfx::Point& release_location) {
ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(press_location));
ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(button, ui_controls::DOWN));
if (press_location != release_location)
ASSERT_TRUE(ui_test_utils::SendMouseMoveSync(release_location));
ASSERT_TRUE(ui_test_utils::SendMouseEventsSync(button, ui_controls::UP));
}
// Touch down and release at the specified locations.
void Tap(const gfx::Point& press_location,
const gfx::Point& release_location) {
gfx::NativeWindow window = GetRootWindow();
ui::test::EventGenerator generator(window);
if (press_location == release_location) {
generator.GestureTapAt(press_location);
} else {
generator.GestureScrollSequence(press_location, release_location,
base::Milliseconds(10), 1);
}
}
OmniboxView* omnibox() {
return browser()->window()->GetLocationBar()->GetOmniboxView();
}
void PressEnterAndWaitForNavigations(size_t num_expected_navigations) {
content::TestNavigationObserver navigation_observer(
browser()->tab_strip_model()->GetActiveWebContents(),
num_expected_navigations);
EXPECT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_RETURN,
false, false, false, false));
navigation_observer.Wait();
}
private:
// InProcessBrowserTest:
void SetUpOnMainThread() override {
ASSERT_TRUE(ui_test_utils::BringBrowserWindowToFront(browser()));
chrome::FocusLocationBar(browser());
ASSERT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
}
gfx::NativeWindow GetRootWindow() const {
gfx::NativeWindow native_window = browser()->window()->GetNativeWindow();
#if defined(USE_AURA)
native_window = native_window->GetRootWindow();
#endif
return native_window;
}
OmniboxTriggeredFeatureService triggered_feature_service_;
};
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, PasteAndGoDoesNotLeavePopupOpen) {
OmniboxView* view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &view));
OmniboxViewViews* omnibox_view_views = static_cast<OmniboxViewViews*>(view);
// Put a URL on the clipboard.
SetClipboardText(ui::ClipboardBuffer::kCopyPaste, u"http://www.example.com/");
// Paste and go.
omnibox_view_views->ExecuteCommand(IDC_PASTE_AND_GO, ui::EF_NONE);
// The popup should not be open.
EXPECT_FALSE(view->model()->PopupIsOpen());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, DoNotNavigateOnDrop) {
OmniboxView* view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &view));
OmniboxViewViews* omnibox_view_views = static_cast<OmniboxViewViews*>(view);
OSExchangeData data;
std::u16string input = u"Foo bar baz";
EXPECT_FALSE(data.HasString());
data.SetString(input);
EXPECT_TRUE(data.HasString());
ui::DropTargetEvent event(data, gfx::PointF(), gfx::PointF(),
ui::DragDropTypes::DRAG_COPY);
omnibox_view_views->OnDrop(event);
EXPECT_EQ(input, omnibox_view_views->GetText());
EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_TRUE(omnibox_view_views->IsSelectAll());
EXPECT_FALSE(
browser()->tab_strip_model()->GetActiveWebContents()->IsLoading());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, AyncDropCallback) {
OmniboxView* view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &view));
OmniboxViewViews* omnibox_view_views = static_cast<OmniboxViewViews*>(view);
OSExchangeData data;
std::u16string input = u"Foo bar baz";
EXPECT_FALSE(data.HasString());
data.SetString(input);
EXPECT_TRUE(data.HasString());
ui::DropTargetEvent event(data, gfx::PointF(), gfx::PointF(),
ui::DragDropTypes::DRAG_COPY);
auto cb = omnibox_view_views->CreateDropCallback(event);
ui::mojom::DragOperation output_drag_op = ui::mojom::DragOperation::kNone;
std::move(cb).Run(event, output_drag_op, /*drag_image_layer_owner=*/nullptr);
EXPECT_EQ(input, omnibox_view_views->GetText());
EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_TRUE(omnibox_view_views->IsSelectAll());
EXPECT_FALSE(
browser()->tab_strip_model()->GetActiveWebContents()->IsLoading());
}
// Flaky: https://crbug.com/915591.
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, DISABLED_SelectAllOnClick) {
OmniboxView* omnibox_view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view));
omnibox_view->SetUserText(u"http://www.google.com/");
// Take the focus away from the omnibox.
ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_FALSE(omnibox_view->IsSelectAll());
// Clicking in the omnibox should take focus and select all text.
const gfx::Rect omnibox_bounds = BrowserView::GetBrowserViewForBrowser(
browser())->GetViewByID(VIEW_ID_OMNIBOX)->GetBoundsInScreen();
const gfx::Point click_location = omnibox_bounds.CenterPoint();
ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT,
click_location, click_location));
EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_TRUE(omnibox_view->IsSelectAll());
// Clicking in another view should clear focus and the selection.
ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_FALSE(omnibox_view->IsSelectAll());
// Clicking in the omnibox again should take focus and select all text again.
ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT,
click_location, click_location));
EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_TRUE(omnibox_view->IsSelectAll());
// Clicking another omnibox spot should keep focus but clear the selection.
omnibox_view->SelectAll(false);
const gfx::Point click2_location = omnibox_bounds.origin() +
gfx::Vector2d(omnibox_bounds.width() / 4, omnibox_bounds.height() / 4);
ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT,
click2_location, click2_location));
EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_FALSE(omnibox_view->IsSelectAll());
// Take the focus away and click in the omnibox again, but drag a bit before
// releasing. We should focus the omnibox but not select all of its text.
ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
ASSERT_NO_FATAL_FAILURE(Click(ui_controls::LEFT,
click_location, click2_location));
EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_FALSE(omnibox_view->IsSelectAll());
// Middle-click is only handled on Linux, by pasting the selection clipboard
// and moving the cursor after the pasted text instead of selecting-all.
ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
ASSERT_NO_FATAL_FAILURE(Click(ui_controls::MIDDLE,
click_location, click_location));
// TODO(crbug.com/1052397): Revisit the macro expression once build flag switch
// of lacros-chrome is complete.
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)
EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
#else
EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
#endif
EXPECT_FALSE(omnibox_view->IsSelectAll());
}
// TODO(crbug.com/1052397): Revisit the macro expression once build flag switch
// of lacros-chrome is complete.
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, SelectionClipboard) {
OmniboxView* omnibox_view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view));
omnibox_view->SetUserText(u"http://www.google.com/");
OmniboxViewViews* omnibox_view_views =
static_cast<OmniboxViewViews*>(omnibox_view);
ASSERT_TRUE(omnibox_view_views);
gfx::RenderText* render_text = omnibox_view_views->GetRenderText();
// Take the focus away from the omnibox.
ASSERT_NO_FATAL_FAILURE(
ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER));
EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_FALSE(omnibox_view->IsSelectAll());
size_t cursor_position = 14;
int cursor_x = render_text->GetCursorBounds(
gfx::SelectionModel(cursor_position, gfx::CURSOR_FORWARD), false).x();
gfx::Point click_location = omnibox_view_views->GetBoundsInScreen().origin();
click_location.Offset(cursor_x + render_text->display_rect().x(),
omnibox_view_views->height() / 2);
// Middle click focuses the omnibox, pastes, and sets a trailing cursor.
// Select-all on focus shouldn't alter the selection clipboard or cursor.
SetClipboardText(ui::ClipboardBuffer::kSelection, u"123");
ASSERT_NO_FATAL_FAILURE(Click(ui_controls::MIDDLE,
click_location, click_location));
EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_FALSE(omnibox_view->IsSelectAll());
EXPECT_EQ(u"http://www.goo123gle.com/", omnibox_view->GetText());
EXPECT_EQ(17U, omnibox_view_views->GetCursorPosition());
// The omnibox can move when it gains focus, so refresh the click location.
click_location.set_x(omnibox_view_views->GetBoundsInScreen().origin().x() +
cursor_x + render_text->display_rect().x());
// Middle clicking again, with focus, pastes and updates the cursor.
SetClipboardText(ui::ClipboardBuffer::kSelection, u"4567");
ASSERT_NO_FATAL_FAILURE(Click(ui_controls::MIDDLE,
click_location, click_location));
EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_FALSE(omnibox_view->IsSelectAll());
EXPECT_EQ(u"http://www.goo4567123gle.com/", omnibox_view->GetText());
EXPECT_EQ(18U, omnibox_view_views->GetCursorPosition());
}
#endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS)
// No touch on desktop Mac. Tracked in http://crbug.com/445520.
#if !BUILDFLAG(IS_MAC) || defined(USE_AURA)
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, SelectAllOnTap) {
OmniboxView* omnibox_view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view));
omnibox_view->SetUserText(u"http://www.google.com/");
// Take the focus away from the omnibox.
ASSERT_NO_FATAL_FAILURE(
ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER));
EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_FALSE(omnibox_view->IsSelectAll());
// Tapping in the omnibox should take focus and select all text.
const gfx::Rect omnibox_bounds = BrowserView::GetBrowserViewForBrowser(
browser())->GetViewByID(VIEW_ID_OMNIBOX)->GetBoundsInScreen();
const gfx::Point tap_location = omnibox_bounds.CenterPoint();
ASSERT_NO_FATAL_FAILURE(Tap(tap_location, tap_location));
EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_TRUE(omnibox_view->IsSelectAll());
// Tapping in another view should clear focus and the selection.
ASSERT_NO_FATAL_FAILURE(
ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER));
EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_FALSE(omnibox_view->IsSelectAll());
// Tapping in the omnibox again should take focus and select all text again.
ASSERT_NO_FATAL_FAILURE(Tap(tap_location, tap_location));
EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_TRUE(omnibox_view->IsSelectAll());
// Tapping another omnibox spot should keep focus and selection.
omnibox_view->SelectAll(false);
const gfx::Point tap2_location = omnibox_bounds.origin() +
gfx::Vector2d(omnibox_bounds.width() / 4, omnibox_bounds.height() / 4);
ASSERT_NO_FATAL_FAILURE(Tap(tap2_location, tap2_location));
EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
// We don't test if the all text is selected because it depends on whether
// there was text under the tap, which appears to be flaky.
// Take the focus away and tap in the omnibox again, but drag a bit before
// releasing. This shouldn't select text.
ASSERT_NO_FATAL_FAILURE(
ui_test_utils::ClickOnView(browser(), VIEW_ID_TAB_CONTAINER));
ASSERT_NO_FATAL_FAILURE(Tap(tap_location, tap2_location));
EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_FALSE(omnibox_view->IsSelectAll());
}
// Tests if executing a command hides touch editing handles.
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest,
DeactivateTouchEditingOnExecuteCommand) {
OmniboxView* view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &view));
OmniboxViewViews* omnibox_view_views = static_cast<OmniboxViewViews*>(view);
views::TextfieldTestApi textfield_test_api(omnibox_view_views);
// Put a URL on the clipboard.
SetClipboardText(ui::ClipboardBuffer::kCopyPaste, u"http://www.example.com/");
// Tap to activate touch editing.
gfx::Point omnibox_center =
omnibox_view_views->GetBoundsInScreen().CenterPoint();
Tap(omnibox_center, omnibox_center);
EXPECT_TRUE(textfield_test_api.touch_selection_controller());
// Execute a command and check if it deactivates touch editing. Paste & Go is
// chosen since it is specific to Omnibox and its execution wouldn't be
// delegated to the base Textfield class.
omnibox_view_views->ExecuteCommand(IDC_PASTE_AND_GO, ui::EF_NONE);
EXPECT_FALSE(textfield_test_api.touch_selection_controller());
}
#endif // !BUILDFLAG(IS_MAC) || defined(USE_AURA)
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, SelectAllOnTabToFocus) {
OmniboxView* omnibox_view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view));
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GURL("http://www.google.com/")));
// RevertAll after navigation to invalidate the selection range saved on blur.
omnibox_view->RevertAll();
EXPECT_FALSE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_FALSE(omnibox_view->IsSelectAll());
// Pressing tab to focus the omnibox should select all text.
while (!ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX)) {
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_TAB,
false, false, false, false));
}
EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
EXPECT_TRUE(omnibox_view->IsSelectAll());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, CloseOmniboxPopupOnTextDrag) {
OmniboxView* omnibox_view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view));
OmniboxViewViews* omnibox_view_views =
static_cast<OmniboxViewViews*>(omnibox_view);
// Populate suggestions for the omnibox popup.
AutocompleteController* autocomplete_controller =
omnibox_view->controller()->autocomplete_controller();
AutocompleteResult& results = autocomplete_controller->internal_result_;
ACMatches matches;
AutocompleteMatch match(nullptr, 500, false,
AutocompleteMatchType::HISTORY_TITLE);
match.contents = u"http://autocomplete-result/";
match.contents_class.push_back(
ACMatchClassification(0, ACMatchClassification::URL));
match.destination_url = GURL("http://autocomplete-result/");
match.allowed_to_be_default_match = true;
matches.push_back(match);
AutocompleteInput input(u"a", metrics::OmniboxEventProto::OTHER,
TestSchemeClassifier());
results.AppendMatches(matches);
results.SortAndCull(
input, TemplateURLServiceFactory::GetForProfile(browser()->profile()),
triggered_feature_service());
// The omnibox popup should open with suggestions displayed.
autocomplete_controller->NotifyChanged();
EXPECT_TRUE(omnibox_view->model()->PopupIsOpen());
// The omnibox text should be selected.
EXPECT_TRUE(omnibox_view->IsSelectAll());
// Simulate a mouse click before dragging the mouse.
gfx::Point point(omnibox_view_views->origin() + gfx::Vector2d(10, 10));
ui::MouseEvent pressed(ui::ET_MOUSE_PRESSED, point, point,
ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON,
ui::EF_LEFT_MOUSE_BUTTON);
omnibox_view_views->OnMousePressed(pressed);
EXPECT_TRUE(omnibox_view->model()->PopupIsOpen());
// Simulate a mouse drag of the omnibox text, and the omnibox should close.
ui::MouseEvent dragged(ui::ET_MOUSE_DRAGGED, point, point,
ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0);
omnibox_view_views->OnMouseDragged(dragged);
EXPECT_FALSE(omnibox_view->model()->PopupIsOpen());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, MaintainCursorAfterFocusCycle) {
OmniboxView* omnibox_view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view));
// Populate suggestions for the omnibox popup.
AutocompleteController* autocomplete_controller =
omnibox_view->controller()->autocomplete_controller();
AutocompleteResult& results = autocomplete_controller->internal_result_;
ACMatches matches;
AutocompleteMatch match(nullptr, 500, false,
AutocompleteMatchType::HISTORY_TITLE);
match.contents = u"http://autocomplete-result/";
match.contents_class.push_back(
ACMatchClassification(0, ACMatchClassification::URL));
match.destination_url = GURL("http://autocomplete-result/");
match.allowed_to_be_default_match = true;
matches.push_back(match);
AutocompleteInput input(u"autocomplete-result", 19, "autocomplete-result",
metrics::OmniboxEventProto::OTHER,
TestSchemeClassifier());
input.set_current_url(GURL("http://autocomplete-result/"));
results.AppendMatches(matches);
results.SortAndCull(
input, TemplateURLServiceFactory::GetForProfile(browser()->profile()),
triggered_feature_service());
// The omnibox popup should open with suggestions displayed.
autocomplete_controller->NotifyChanged();
EXPECT_TRUE(omnibox_view->model()->PopupIsOpen());
// TODO(krb): For some reason, we need to hit End twice to be registered.
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_END, false,
false, false, false));
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_END, false,
false, false, false));
EXPECT_FALSE(omnibox_view->IsSelectAll());
// Save cursor position, before blur.
size_t prev_start, end;
omnibox_view->GetSelectionBounds(&prev_start, &end);
chrome::FocusAppMenu(browser());
EXPECT_FALSE(omnibox_view->model()->PopupIsOpen());
// Re-focus.
chrome::FocusLocationBar(browser());
// Make sure cursor is restored.
size_t start;
omnibox_view->GetSelectionBounds(&start, &end);
EXPECT_EQ(prev_start, start);
}
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, BackgroundIsOpaque) {
// The omnibox text should be rendered on an opaque background. Otherwise, we
// can't use subpixel rendering.
OmniboxViewViews* view = BrowserView::GetBrowserViewForBrowser(browser())->
toolbar()->location_bar()->omnibox_view();
ASSERT_TRUE(view);
EXPECT_FALSE(view->GetRenderText()->subpixel_rendering_suppressed());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, FocusedTextInputClient) {
chrome::FocusLocationBar(browser());
OmniboxView* view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &view));
OmniboxViewViews* omnibox_view_views = static_cast<OmniboxViewViews*>(view);
ui::InputMethod* input_method =
omnibox_view_views->GetWidget()->GetInputMethod();
EXPECT_EQ(static_cast<ui::TextInputClient*>(omnibox_view_views),
input_method->GetTextInputClient());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, TextElideStatus) {
OmniboxView* view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &view));
OmniboxViewViews* omnibox_view_views = static_cast<OmniboxViewViews*>(view);
ASSERT_NO_FATAL_FAILURE(ClickBrowserWindowCenter());
EXPECT_EQ(omnibox_view_views->GetRenderText()->elide_behavior(),
gfx::ELIDE_TAIL);
chrome::FocusLocationBar(browser());
EXPECT_EQ(omnibox_view_views->GetRenderText()->elide_behavior(),
gfx::NO_ELIDE);
}
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, FragmentUnescapedForDisplay) {
OmniboxView* view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &view));
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(), GURL("http://example.com/#%E2%98%83")));
EXPECT_EQ(view->GetText(), u"example.com/#\u2603");
}
// Ensure that when the user navigates between suggestions, that the accessible
// value of the Omnibox includes helpful information for human comprehension,
// such as the document title. When the user begins to arrow left/right
// within the label or edit it, the value is presented as the pure editable URL.
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, FriendlyAccessibleLabel) {
OmniboxView* omnibox_view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view));
std::u16string match_url = u"https://google.com";
AutocompleteMatch match(nullptr, 500, false,
AutocompleteMatchType::HISTORY_TITLE);
match.contents = match_url;
match.contents_class.push_back(
ACMatchClassification(0, ACMatchClassification::URL));
match.destination_url = GURL(match_url);
match.description = u"Google";
match.description_class = {{0, 0}};
match.allowed_to_be_default_match = true;
// Enter user input mode to prevent spurious unelision.
omnibox_view->model()->SetInputInProgress(true);
// Populate suggestions for the omnibox popup.
AutocompleteController* autocomplete_controller =
omnibox_view->controller()->autocomplete_controller();
AutocompleteResult& results = autocomplete_controller->internal_result_;
ACMatches matches;
matches.push_back(match);
AutocompleteInput input(u"g", metrics::OmniboxEventProto::OTHER,
TestSchemeClassifier());
results.AppendMatches(matches);
results.SortAndCull(
input, TemplateURLServiceFactory::GetForProfile(browser()->profile()),
triggered_feature_service());
// The omnibox popup should open with suggestions displayed.
chrome::FocusLocationBar(browser());
autocomplete_controller->NotifyChanged();
EXPECT_TRUE(omnibox_view->model()->PopupIsOpen());
OmniboxViewViews* omnibox_view_views =
static_cast<OmniboxViewViews*>(omnibox_view);
// Ensure that the friendly accessibility label is updated.
omnibox_view_views->OnTemporaryTextMaybeChanged(match_url, match, false,
false);
omnibox_view->SelectAll(true);
EXPECT_EQ(u"https://google.com", omnibox_view_views->GetText());
// Test friendly label.
const int kFriendlyPrefixLength = match.description.size() + 1;
ui::AXNodeData node_data;
omnibox_view_views->GetAccessibleNodeData(&node_data);
EXPECT_EQ(u"Google https://google.com location from history, 1 of 1",
node_data.GetString16Attribute(ax::mojom::StringAttribute::kValue));
// Selection offsets are moved over by length the inserted descriptive text
// prefix ("Google") + 1 for the space.
EXPECT_EQ(kFriendlyPrefixLength,
node_data.GetIntAttribute(ax::mojom::IntAttribute::kTextSelEnd));
EXPECT_EQ(kFriendlyPrefixLength + static_cast<int>(match_url.size()),
node_data.GetIntAttribute(ax::mojom::IntAttribute::kTextSelStart));
EXPECT_EQ("both", node_data.GetStringAttribute(
ax::mojom::StringAttribute::kAutoComplete));
EXPECT_EQ(ax::mojom::Role::kTextField, node_data.role);
// Select second character -- even though the friendly "Google " prefix is
// part of the exposed accessible text, setting the selection within select
// the intended part of the editable text.
ui::AXActionData set_selection_action_data;
set_selection_action_data.action = ax::mojom::Action::kSetSelection;
set_selection_action_data.anchor_node_id = node_data.id;
set_selection_action_data.focus_node_id = node_data.id;
set_selection_action_data.focus_offset = kFriendlyPrefixLength + 1;
set_selection_action_data.anchor_offset = kFriendlyPrefixLength + 3;
omnibox_view_views->HandleAccessibleAction(set_selection_action_data);
EXPECT_EQ(u"https://google.com", omnibox_view_views->GetText());
// Type "x" to replace the selected "tt" with that character.
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_X, false,
false, false, false));
// Second character should be an "x" now.
EXPECT_EQ(u"hxps://google.com", omnibox_view_views->GetText());
// When editing starts, the accessible value becomes the same as the raw
// edited text.
omnibox_view_views->GetAccessibleNodeData(&node_data);
EXPECT_EQ(u"hxps://google.com",
node_data.GetString16Attribute(ax::mojom::StringAttribute::kValue));
}
// Ensure that the Omnibox popup exposes appropriate accessibility semantics,
// given a current state of open or closed.
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, AccessiblePopup) {
OmniboxView* omnibox_view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view));
OmniboxViewViews* omnibox_view_views =
static_cast<OmniboxViewViews*>(omnibox_view);
chrome::FocusLocationBar(browser());
std::u16string match_url = u"https://google.com";
AutocompleteMatch match(nullptr, 500, false,
AutocompleteMatchType::HISTORY_TITLE);
match.contents = match_url;
match.contents_class.push_back(
ACMatchClassification(0, ACMatchClassification::URL));
match.destination_url = GURL(match_url);
match.description = u"Google";
match.allowed_to_be_default_match = true;
OmniboxPopupView* popup_view = omnibox_view_views->GetPopupViewForTesting();
ui::AXNodeData popup_node_data_1;
popup_view->GetPopupAccessibleNodeData(&popup_node_data_1);
EXPECT_FALSE(popup_node_data_1.HasState(ax::mojom::State::kExpanded));
EXPECT_TRUE(popup_node_data_1.HasState(ax::mojom::State::kCollapsed));
EXPECT_TRUE(popup_node_data_1.HasState(ax::mojom::State::kInvisible));
EXPECT_TRUE(
popup_node_data_1.HasIntAttribute(ax::mojom::IntAttribute::kPopupForId));
EXPECT_EQ(
popup_node_data_1.GetIntAttribute(ax::mojom::IntAttribute::kPopupForId),
omnibox_view_views->GetViewAccessibility().GetUniqueId().Get());
// Populate suggestions for the omnibox popup.
AutocompleteController* autocomplete_controller =
omnibox_view->controller()->autocomplete_controller();
AutocompleteResult& results = autocomplete_controller->internal_result_;
ACMatches matches;
matches.push_back(match);
AutocompleteInput input(u"g", metrics::OmniboxEventProto::OTHER,
TestSchemeClassifier());
results.AppendMatches(matches);
results.SortAndCull(
input, TemplateURLServiceFactory::GetForProfile(browser()->profile()),
triggered_feature_service());
// The omnibox popup should open with suggestions displayed.
autocomplete_controller->NotifyChanged();
EXPECT_TRUE(omnibox_view->model()->PopupIsOpen());
ui::AXNodeData popup_node_data_2;
popup_view->GetPopupAccessibleNodeData(&popup_node_data_2);
EXPECT_TRUE(popup_node_data_2.HasState(ax::mojom::State::kExpanded));
EXPECT_FALSE(popup_node_data_2.HasState(ax::mojom::State::kCollapsed));
EXPECT_FALSE(popup_node_data_2.HasState(ax::mojom::State::kInvisible));
}
// Flaky: https://crbug.com/1143630.
// Omnibox returns to clean state after chrome://kill and reload.
// https://crbug.com/993701 left the URL and icon as chrome://kill after reload.
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, DISABLED_ReloadAfterKill) {
OmniboxView* omnibox_view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view));
OmniboxViewViews* omnibox_view_views =
static_cast<OmniboxViewViews*>(omnibox_view);
// Open new tab page.
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(),
GURL(chrome::kChromeUINewTabURL)));
content::WebContents* tab =
browser()->tab_strip_model()->GetActiveWebContents();
// Kill the tab with chrome://kill
{
content::ScopedAllowRendererCrashes scoped_allow_renderer_crashes;
ASSERT_TRUE(
ui_test_utils::NavigateToURL(browser(), GURL(blink::kChromeUIKillURL)));
EXPECT_TRUE(tab->IsCrashed());
}
// Reload the tab.
tab->GetController().Reload(content::ReloadType::NORMAL, false);
EXPECT_TRUE(content::WaitForLoadStop(tab));
// Verify the omnibox contents, URL and icon.
EXPECT_EQ(u"", omnibox_view_views->GetText());
EXPECT_EQ(GURL(url::kAboutBlankURL),
browser()->location_bar_model()->GetURL());
}
// Omnibox un-elides and elides URL appropriately according to the Always Show
// Full URLs setting.
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, AlwaysShowFullURLs) {
OmniboxView* omnibox_view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view));
OmniboxViewViews* omnibox_view_views =
static_cast<OmniboxViewViews*>(omnibox_view);
ASSERT_TRUE(embedded_test_server()->Start());
// Use a hostname ("a.test") since IP addresses aren't eligible for eliding.
GURL url = embedded_test_server()->GetURL("a.test", "/title1.html");
std::u16string url_text = base::ASCIIToUTF16(url.spec());
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
EXPECT_EQ(url_text, u"http://" + omnibox_view_views->GetText());
// After toggling the setting, the full URL should be shown.
chrome::ToggleShowFullURLs(browser());
EXPECT_EQ(url_text, omnibox_view_views->GetText());
EXPECT_EQ(0,
omnibox_view_views->GetRenderText()->GetUpdatedDisplayOffset().x());
// Toggling the setting again should go back to the elided URL.
chrome::ToggleShowFullURLs(browser());
EXPECT_EQ(url_text, u"http://" + omnibox_view_views->GetText());
}
// The following set of tests require UIA accessibility support, which only
// exists on Windows.
#if BUILDFLAG(IS_WIN)
class OmniboxViewViewsUIATest : public OmniboxViewViewsTest {
public:
OmniboxViewViewsUIATest() {}
private:
base::test::ScopedFeatureList scoped_feature_list_{::features::kUiaProvider};
};
// Omnibox fires the right events when the popup opens/closes with UIA turned
// on.
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsUIATest, AccessibleOmnibox) {
OmniboxView* omnibox_view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view));
chrome::FocusLocationBar(browser());
std::u16string match_url = u"https://example.com";
AutocompleteMatch match(nullptr, 500, false,
AutocompleteMatchType::HISTORY_TITLE);
match.contents = match_url;
match.contents_class.push_back(
ACMatchClassification(0, ACMatchClassification::URL));
match.destination_url = GURL(match_url);
match.description = u"Example";
match.allowed_to_be_default_match = true;
EXPECT_FALSE(omnibox_view->model()->PopupIsOpen());
HWND window_handle =
browser()->window()->GetNativeWindow()->GetHost()->GetAcceleratedWidget();
UiaAccessibilityWaiterInfo info = {window_handle, L"textbox",
L"Address and search bar",
ax::mojom::Event::kControlsChanged};
UiaAccessibilityEventWaiter open_waiter(info);
// Populate suggestions for the omnibox popup.
AutocompleteController* autocomplete_controller =
omnibox_view->controller()->autocomplete_controller();
AutocompleteResult& results = autocomplete_controller->internal_result_;
ACMatches matches;
matches.push_back(match);
AutocompleteInput input(u"e", metrics::OmniboxEventProto::OTHER,
TestSchemeClassifier());
results.AppendMatches(matches);
results.SortAndCull(
input, TemplateURLServiceFactory::GetForProfile(browser()->profile()),
triggered_feature_service());
// The omnibox popup should open with suggestions displayed.
autocomplete_controller->NotifyChanged();
// Wait for ControllerFor property changed event.
open_waiter.Wait();
EXPECT_TRUE(omnibox_view->model()->PopupIsOpen());
UiaAccessibilityEventWaiter close_waiter(info);
// Close the popup. Another property change event is expected.
ClickBrowserWindowCenter();
close_waiter.Wait();
EXPECT_FALSE(omnibox_view->model()->PopupIsOpen());
}
namespace {
class OmniboxMockInputMethod : public ui::MockInputMethod {
public:
OmniboxMockInputMethod() : ui::MockInputMethod(nullptr) {}
// ui::MockInputMethod:
bool IsInputLocaleCJK() const override { return input_locale_cjk_; }
void SetInputLocaleCJK(bool is_cjk) { input_locale_cjk_ = is_cjk; }
private:
bool input_locale_cjk_ = false;
};
} // namespace
class OmniboxViewViewsIMETest : public OmniboxViewViewsTest {
public:
OmniboxViewViewsIMETest() = default;
// OmniboxViewViewsTest:
void SetUp() override {
input_method_ = new OmniboxMockInputMethod();
// Transfers ownership.
ui::SetUpInputMethodForTesting(input_method_);
InProcessBrowserTest::SetUp();
}
protected:
OmniboxMockInputMethod* GetInputMethod() const { return input_method_; }
private:
base::test::ScopedFeatureList scoped_feature_list_{::features::kUiaProvider};
raw_ptr<OmniboxMockInputMethod, AcrossTasksDanglingUntriaged> input_method_ =
nullptr;
};
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsIMETest, TextInputTypeChangedTest) {
chrome::FocusLocationBar(browser());
OmniboxView* view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &view));
OmniboxViewViews* omnibox_view_views = static_cast<OmniboxViewViews*>(view);
ui::InputMethod* input_method =
omnibox_view_views->GetWidget()->GetInputMethod();
EXPECT_EQ(input_method, GetInputMethod());
EXPECT_EQ(ui::TEXT_INPUT_TYPE_URL, omnibox_view_views->GetTextInputType());
GetInputMethod()->SetInputLocaleCJK(/*is_cjk=*/true);
omnibox_view_views->OnInputMethodChanged();
EXPECT_EQ(ui::TEXT_INPUT_TYPE_SEARCH, omnibox_view_views->GetTextInputType());
GetInputMethod()->SetInputLocaleCJK(/*is_cjk=*/false);
omnibox_view_views->OnInputMethodChanged();
EXPECT_EQ(ui::TEXT_INPUT_TYPE_URL, omnibox_view_views->GetTextInputType());
}
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsIMETest, TextInputTypeInitRespectsIME) {
OmniboxMockInputMethod* input_method = new OmniboxMockInputMethod();
ui::SetUpInputMethodForTesting(input_method);
input_method->SetInputLocaleCJK(/*is_cjk=*/true);
Browser* browser_2 = CreateBrowser(browser()->profile());
OmniboxView* view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser_2, &view));
OmniboxViewViews* omnibox_view_views = static_cast<OmniboxViewViews*>(view);
EXPECT_EQ(omnibox_view_views->GetWidget()->GetInputMethod(), input_method);
EXPECT_EQ(ui::TEXT_INPUT_TYPE_SEARCH, omnibox_view_views->GetTextInputType());
input_method->SetInputLocaleCJK(/*is_cjk=*/false);
omnibox_view_views->OnInputMethodChanged();
EXPECT_EQ(ui::TEXT_INPUT_TYPE_URL, omnibox_view_views->GetTextInputType());
}
#endif // BUILDFLAG(IS_WIN)
// ClickOnView(VIEW_ID_OMNIBOX) does not set focus to omnibox on Mac.
// Looks like the same problem as in the SelectAllOnClick().
// Tracked in: https://crbug.com/915591
// Test is also flaky on Linux: https://crbug.com/1157250
// TODO(crbug.com/1052397): Revisit once build flag switch of lacros-chrome is
// complete.
#if BUILDFLAG(IS_MAC) || (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS_LACROS))
#define MAYBE_HandleExternalProtocolURLs DISABLED_HandleExternalProtocolURLs
#else
#define MAYBE_HandleExternalProtocolURLs HandleExternalProtocolURLs
#endif
// Checks that focusing on the omnibox allows the page to open external protocol
// URLs. Regression test for https://crbug.com/1143632
IN_PROC_BROWSER_TEST_F(OmniboxViewViewsTest, MAYBE_HandleExternalProtocolURLs) {
OmniboxView* omnibox_view = nullptr;
ASSERT_NO_FATAL_FAILURE(GetOmniboxViewForBrowser(browser(), &omnibox_view));
AutocompleteController* controller =
omnibox_view->controller()->autocomplete_controller();
ASSERT_TRUE(controller);
auto set_text_and_perform_navigation = [this, omnibox_view, controller]() {
const char fake_protocol[] = "fake";
const char16_t fake_url[] = u"fake://path";
EXPECT_TRUE(ui_test_utils::IsViewFocused(browser(), VIEW_ID_OMNIBOX));
// Set omnibox text and wait for autocomplete.
omnibox_view->SetUserText(fake_url);
if (!controller->done())
ui_test_utils::WaitForAutocompleteDone(browser());
ASSERT_TRUE(controller->done());
ASSERT_TRUE(omnibox_view->model()->PopupIsOpen());
EXPECT_NE(ExternalProtocolHandler::BLOCK,
ExternalProtocolHandler::GetBlockState(fake_protocol, nullptr,
browser()->profile()));
// Check SWYT and UWYT suggestions
const AutocompleteResult& result = controller->result();
ASSERT_EQ(result.size(), 2U);
EXPECT_EQ(result.match_at(0).type,
AutocompleteMatchType::SEARCH_WHAT_YOU_TYPED);
EXPECT_EQ(result.match_at(1).type,
AutocompleteMatchType::URL_WHAT_YOU_TYPED);
// Navigate to UWYT suggestion.
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_DOWN, false,
false, false, false));
ASSERT_TRUE(ui_test_utils::SendKeyPressSync(browser(), ui::VKEY_RETURN,
false, false, false, false));
content::WebContents* tab =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_TRUE(content::WaitForLoadStop(tab));
EXPECT_EQ(ExternalProtocolHandler::BLOCK,
ExternalProtocolHandler::GetBlockState(fake_protocol, nullptr,
browser()->profile()));
};
set_text_and_perform_navigation();
// Set focus to omnibox by click.
ASSERT_NO_FATAL_FAILURE(
ui_test_utils::ClickOnView(browser(), VIEW_ID_OMNIBOX));
ASSERT_NO_FATAL_FAILURE(
ui_test_utils::WaitForViewFocus(browser(), VIEW_ID_OMNIBOX, true));
set_text_and_perform_navigation();
// No touch on desktop Mac. Tracked in http://crbug.com/445520.
#if !BUILDFLAG(IS_MAC) || defined(USE_AURA)
// Set focus to omnibox by tap.
const gfx::Point omnibox_tap_point =
BrowserView::GetBrowserViewForBrowser(browser())
->GetViewByID(VIEW_ID_OMNIBOX)
->GetBoundsInScreen()
.CenterPoint();
ASSERT_NO_FATAL_FAILURE(Tap(omnibox_tap_point, omnibox_tap_point));
ASSERT_NO_FATAL_FAILURE(
ui_test_utils::WaitForViewFocus(browser(), VIEW_ID_OMNIBOX, true));
set_text_and_perform_navigation();
#endif // !BUILDFLAG(IS_MAC) || defined(USE_AURA)
}
// SendKeyPressSync times out on Mac, probably due to https://crbug.com/824418.
#if BUILDFLAG(IS_MAC)
#define MAYBE_DefaultTypedNavigationsToHttps_ZeroSuggest_NoUpgrade \
DISABLED_DefaultTypedNavigationsToHttps_ZeroSuggest_NoUpgrade
#else
#define MAYBE_DefaultTypedNavigationsToHttps_ZeroSuggest_NoUpgrade \
DefaultTypedNavigationsToHttps_ZeroSuggest_NoUpgrade
#endif
// Test that triggers a zero suggest autocomplete request by clicking on the
// omnibox. These should never attempt an HTTPS upgrade or involve the typed
// navigation upgrade throttle.
// This is a regression test for https://crbug.com/1251065
IN_PROC_BROWSER_TEST_F(
OmniboxViewViewsTest,
MAYBE_DefaultTypedNavigationsToHttps_ZeroSuggest_NoUpgrade) {
// Since the embedded test server only works for URLs with non-default ports,
// use a URLLoaderInterceptor to mimic port-free operation. This allows the
// rest of the test to operate as if all URLs are using the default ports.
content::URLLoaderInterceptor interceptor(base::BindLambdaForTesting(
[&](content::URLLoaderInterceptor::RequestParams* params) {
if (params->url_request.url.host() == "site-with-good-https.com") {
std::string headers =
"HTTP/1.1 200 OK\nContent-Type: text/html; charset=utf-8\n";
std::string body = "<html><title>Success</title>Hello world</html>";
content::URLLoaderInterceptor::WriteResponse(headers, body,
params->client.get());
return true;
}
// Not handled by us.
return false;
}));
base::HistogramTester histograms;
const GURL url("https://site-with-good-https.com");
// Type "https://site-with-good-https.com". This should load fine without
// hitting TypedNavigationUpgradeThrottle.
omnibox()->SetUserText(base::UTF8ToUTF16(url.spec()), true);
PressEnterAndWaitForNavigations(1);
content::WebContents* contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(url, contents->GetLastCommittedURL());
EXPECT_FALSE(chrome_browser_interstitials::IsShowingInterstitial(contents));
histograms.ExpectTotalCount(
security_interstitials::omnibox_https_upgrades::kEventHistogram, 0);
ui_test_utils::HistoryEnumerator enumerator(browser()->profile());
EXPECT_TRUE(base::Contains(enumerator.urls(), url));
// Now click the omnibox. This should trigger a zero suggest request with the
// text "site-with-good-https.com" despite the omnibox URL being
// https://site-with-good-https.com. Autocomplete input class shouldn't try
// to upgrade this request.
const gfx::Rect omnibox_bounds =
BrowserView::GetBrowserViewForBrowser(browser())
->GetViewByID(VIEW_ID_OMNIBOX)
->GetBoundsInScreen();
const gfx::Point click_location = omnibox_bounds.CenterPoint();
ASSERT_NO_FATAL_FAILURE(
Click(ui_controls::LEFT, click_location, click_location));
PressEnterAndWaitForNavigations(1);
histograms.ExpectTotalCount(
security_interstitials::omnibox_https_upgrades::kEventHistogram, 0);
}
|