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 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/renderer_host/input/touch_selection_controller_client_aura.h"
#include <memory>
#include "base/command_line.h"
#include "base/json/json_reader.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_timeouts.h"
#include "content/browser/renderer_host/render_widget_host_view_aura.h"
#include "content/browser/renderer_host/render_widget_host_view_child_frame.h"
#include "content/browser/renderer_host/render_widget_host_view_event_handler.h"
#include "content/browser/renderer_host/text_input_manager.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/public/test/hit_test_region_observer.h"
#include "content/public/test/synchronize_visual_properties_interceptor.h"
#include "content/public/test/test_navigation_observer.h"
#include "content/public/test/test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "third_party/blink/public/common/switches.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/ui_base_features.h"
#include "ui/display/display_switches.h"
#include "ui/events/event_sink.h"
#include "ui/events/event_utils.h"
#include "ui/events/gesture_detection/gesture_configuration.h"
#include "ui/events/test/event_generator.h"
#include "ui/events/test/motion_event_test_utils.h"
#include "ui/touch_selection/touch_editing_controller.h"
#include "ui/touch_selection/touch_selection_controller_test_api.h"
#include "ui/touch_selection/touch_selection_metrics.h"
namespace content {
namespace {
// Character dimensions in px, from the font size in `touch_selection.html`.
constexpr int kCharacterWidth = 15;
constexpr int kCharacterHeight = 15;
bool JSONToPoint(const std::string& str, gfx::PointF* point) {
std::optional<base::Value> value = base::JSONReader::Read(str);
if (!value)
return false;
base::Value::Dict* root = value->GetIfDict();
if (!root)
return false;
std::optional<double> x = root->FindDouble("x");
std::optional<double> y = root->FindDouble("y");
if (!x || !y)
return false;
point->set_x(*x);
point->set_y(*y);
return true;
}
gfx::RectF ConvertRectFToChildCoords(RenderWidgetHostViewAura* parent,
RenderWidgetHostViewChildFrame* child,
const gfx::RectF& rect) {
return gfx::BoundingRect(
child->TransformRootPointToViewCoordSpace(rect.origin()),
child->TransformRootPointToViewCoordSpace(rect.bottom_right()));
}
// Converts a point from `view` coordinates to the coordinate system used by
// `generator_delegate`.
gfx::Point ConvertPointFromView(
RenderWidgetHostViewAura* view,
const ui::test::EventGeneratorDelegate* generator_delegate,
const gfx::PointF& point_in_view) {
gfx::Point point_in_generator = gfx::ToRoundedPoint(point_in_view);
generator_delegate->ConvertPointFromTarget(view->GetNativeView(),
&point_in_generator);
return gfx::ScaleToRoundedPoint(point_in_generator,
view->GetDeviceScaleFactor());
}
// A mock touch selection menu runner to use whenever a default one is not
// installed.
class TestTouchSelectionMenuRunner : public ui::TouchSelectionMenuRunner {
public:
TestTouchSelectionMenuRunner() : menu_opened_(false) {}
TestTouchSelectionMenuRunner(const TestTouchSelectionMenuRunner&) = delete;
TestTouchSelectionMenuRunner& operator=(const TestTouchSelectionMenuRunner&) =
delete;
~TestTouchSelectionMenuRunner() override {}
private:
bool IsMenuAvailable(
const ui::TouchSelectionMenuClient* client) const override {
return true;
}
void OpenMenu(base::WeakPtr<ui::TouchSelectionMenuClient> client,
const gfx::Rect& anchor_rect,
const gfx::Size& handle_image_size,
aura::Window* context) override {
menu_opened_ = true;
}
void CloseMenu() override { menu_opened_ = false; }
bool IsRunning() const override { return menu_opened_; }
bool menu_opened_;
};
} // namespace
class TestTouchSelectionControllerClientAura
: public TouchSelectionControllerClientAura,
public TextInputManager::Observer {
public:
TestTouchSelectionControllerClientAura(RenderWidgetHostViewAura* rwhva,
bool enable_all_menu_commands)
: TouchSelectionControllerClientAura(rwhva),
expected_event_(ui::SELECTION_HANDLES_SHOWN),
enable_all_menu_commands_(enable_all_menu_commands) {
show_quick_menu_immediately_for_test_ = true;
}
TestTouchSelectionControllerClientAura(
const TestTouchSelectionControllerClientAura&) = delete;
TestTouchSelectionControllerClientAura& operator=(
const TestTouchSelectionControllerClientAura&) = delete;
~TestTouchSelectionControllerClientAura() override {}
void InitWaitForSelectionEvent(ui::SelectionEventType expected_event) {
DCHECK(!run_loop_);
expected_event_ = expected_event;
run_loop_ = std::make_unique<base::RunLoop>();
}
void InitWaitForHandleContextMenu() {
DCHECK(!menu_run_loop_);
waiting_for_handle_context_menu_ = true;
menu_run_loop_ = std::make_unique<base::RunLoop>();
}
bool HandleContextMenu(const ContextMenuParams& params) override {
bool handled =
TouchSelectionControllerClientAura::HandleContextMenu(params);
if (menu_run_loop_ && waiting_for_handle_context_menu_) {
waiting_for_handle_context_menu_ = false;
menu_run_loop_->Quit();
}
return handled;
}
void InitWaitForSelectionUpdate() {
DCHECK(!run_loop_);
// Wait for selection change to ensure that the selected text is updated.
waiting_for_selection_change_ = true;
// Wait for bounds update to ensure that the TouchSelectionController has
// processed the selection update (since it uses these bounds e.g. for
// handle placement and to initiate long press drag selection).
waiting_for_selection_bounds_update_ = true;
rwhva_->GetTextInputManager()->AddObserver(this);
run_loop_ = std::make_unique<base::RunLoop>();
}
void UpdateClientSelectionBounds(const gfx::SelectionBound& start,
const gfx::SelectionBound& end) override {
TouchSelectionControllerClientAura::UpdateClientSelectionBounds(start, end);
if (run_loop_ && waiting_for_selection_bounds_update_) {
waiting_for_selection_bounds_update_ = false;
// Only quit the run loop once the selection and bounds have both updated.
if (!waiting_for_selection_change_) {
run_loop_->Quit();
}
}
}
void OnTextSelectionChanged(TextInputManager* text_input_manager,
RenderWidgetHostViewBase* updated_view) override {
if (run_loop_ && waiting_for_selection_change_) {
text_input_manager->RemoveObserver(this);
waiting_for_selection_change_ = false;
// Only quit the run loop once the selection and bounds have both updated.
if (!waiting_for_selection_bounds_update_) {
run_loop_->Quit();
}
}
}
void Wait() {
DCHECK(run_loop_);
run_loop_->Run();
run_loop_.reset();
}
void WaitForHandleContextMenu() {
DCHECK(menu_run_loop_);
menu_run_loop_->Run();
menu_run_loop_.reset();
}
ui::TouchSelectionMenuClient* GetActiveMenuClient() {
return active_menu_client_;
}
bool IsMagnifierVisible() const {
return touch_selection_magnifier_ != nullptr;
}
bool IsHandlingSelectionDrag() const { return handle_drag_in_progress_; }
private:
// TouchSelectionControllerClientAura:
void OnSelectionEvent(ui::SelectionEventType event) override {
TouchSelectionControllerClientAura::OnSelectionEvent(event);
if (run_loop_ && event == expected_event_)
run_loop_->Quit();
}
bool IsCommandIdEnabled(int command_id) const override {
if (enable_all_menu_commands_) {
return true;
}
return TouchSelectionControllerClientAura::IsCommandIdEnabled(command_id);
}
bool waiting_for_handle_context_menu_ = false;
bool waiting_for_selection_change_ = false;
bool waiting_for_selection_bounds_update_ = false;
ui::SelectionEventType expected_event_;
std::unique_ptr<base::RunLoop> run_loop_;
std::unique_ptr<base::RunLoop> menu_run_loop_;
// When set to true, the TouchSelectionControllerClientAura criteria for
// enabling menu commands is overridden and we instead enable all menu
// commands. This is so that we can test behaviour related to showing/hiding
// the menu without worrying about whether commands will be shown or not.
bool enable_all_menu_commands_ = false;
};
class TouchSelectionControllerClientAuraTest : public ContentBrowserTest {
public:
TouchSelectionControllerClientAuraTest() {
scoped_feature_list_.InitAndEnableFeature(
features::kTouchTextEditingRedesign);
}
TouchSelectionControllerClientAuraTest(
const TouchSelectionControllerClientAuraTest&) = delete;
TouchSelectionControllerClientAuraTest& operator=(
const TouchSelectionControllerClientAuraTest&) = delete;
~TouchSelectionControllerClientAuraTest() override {}
protected:
// Starts the test server and navigates to the given url. Sets a large enough
// size to the root window. Returns after the navigation to the url is
// complete.
void StartTestWithPage(const std::string& url) {
ASSERT_TRUE(embedded_test_server()->Start());
GURL test_url(embedded_test_server()->GetURL(url));
EXPECT_TRUE(NavigateToURL(shell(), test_url));
aura::Window* content = shell()->web_contents()->GetContentNativeView();
content->GetHost()->SetBoundsInPixels(gfx::Rect(800, 600));
SimulateEndOfPaintHoldingOnPrimaryMainFrame(shell()->web_contents());
}
gfx::PointF GetPointInText(int cursor_index) const {
gfx::PointF point;
JSONToPoint(EvalJs(shell(), "get_top_left_of_text()").ExtractString(),
&point);
point.Offset(cursor_index * kCharacterWidth, 0.5f * kCharacterHeight);
return point;
}
gfx::PointF GetPointInTextfield(int cursor_index) const {
gfx::PointF point;
JSONToPoint(EvalJs(shell(), "get_top_left_of_textfield()").ExtractString(),
&point);
point.Offset(cursor_index * kCharacterWidth, 0.5f * kCharacterHeight);
return point;
}
gfx::PointF GetPointInsideEmptyTextfield() const {
gfx::PointF point;
JSONToPoint(
EvalJs(shell(), "get_top_left_of_empty_textfield()").ExtractString(),
&point);
// Offset the point so that it is within the textfield.
point.Offset(0.5f * kCharacterWidth, 0.5f * kCharacterHeight);
return point;
}
RenderWidgetHostViewAura* GetRenderWidgetHostViewAura() {
return static_cast<RenderWidgetHostViewAura*>(
shell()->web_contents()->GetRenderWidgetHostView());
}
TestTouchSelectionControllerClientAura* selection_controller_client() {
return static_cast<TestTouchSelectionControllerClientAura*>(
GetRenderWidgetHostViewAura()->selection_controller_client());
}
// Performs a tap to place the cursor at `point`.
void TapAndWaitForCursor(ui::test::EventGenerator& generator,
const gfx::Point& point) {
selection_controller_client()->InitWaitForSelectionUpdate();
generator.GestureTapAt(point);
selection_controller_client()->Wait();
}
// Performs a long press to select the word at `point`.
void SelectWithLongPress(ui::test::EventGenerator& generator,
const gfx::Point& point) {
selection_controller_client()->InitWaitForSelectionUpdate();
generator.PressTouch(point);
selection_controller_client()->Wait();
}
// Performs a double press to select the word at `point`.
void SelectWithDoublePress(ui::test::EventGenerator& generator,
const gfx::Point& point) {
// Perform the first press and release, then wait for a selection update
// before pressing again. This is to ensure that the next selection update
// corresponds to the second press (otherwise, we might return too early if
// the first selection update only happens after the second press). Note
// that the event generator uses its own mock clock to set the touch event
// times, so there shouldn't be an issue with double press timing even if
// the first selection update is delayed.
selection_controller_client()->InitWaitForSelectionUpdate();
generator.PressTouch(point);
generator.ReleaseTouch();
selection_controller_client()->Wait();
// Perform a second press to select the word at `point`.
selection_controller_client()->InitWaitForSelectionUpdate();
generator.PressTouch(point);
selection_controller_client()->Wait();
}
// Performs touch moves to initiate selection dragging after a long press or
// double press gesture. Note that the first two touch moves after the
// initiating long press or double press don't produce selection updates,
// since they are instead used to set up the selection drag (e.g. set the
// selection base and extent).
void InitiateTouchSelectionDragging(ui::test::EventGenerator& generator) {
generator.MoveTouchBy(10, 0);
generator.MoveTouchBy(10, 0);
}
// Performs a touch move to adjust the selection and waits for the
// corresponding selection update. This assumes that there is currently a
// selection drag in progress.
void DragAndWaitForSelectionUpdate(ui::test::EventGenerator& generator,
int x,
int y) {
CHECK(selection_controller_client()->IsHandlingSelectionDrag());
selection_controller_client()->InitWaitForSelectionUpdate();
generator.MoveTouchBy(x, y);
selection_controller_client()->Wait();
}
void InitSelectionController(bool enable_all_menu_commands) {
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
rwhva->SetSelectionControllerClientForTest(
std::make_unique<TestTouchSelectionControllerClientAura>(
rwhva, enable_all_menu_commands));
// Simulate the start of a motion event sequence, since the tests assume it.
rwhva->selection_controller()->WillHandleTouchEvent(
ui::test::MockMotionEvent(ui::MotionEvent::Action::DOWN));
}
void SetUpOnMainThread() override {
ContentBrowserTest::SetUpOnMainThread();
if (!ui::TouchSelectionMenuRunner::GetInstance())
menu_runner_ = std::make_unique<TestTouchSelectionMenuRunner>();
// Set a small tap slop.
ui::GestureConfiguration::GetInstance()
->set_max_touch_move_in_pixels_for_click(5);
}
void SetUpCommandLine(base::CommandLine* command_line) override {
command_line->AppendSwitch(blink::switches::kAllowPreCommitInput);
}
private:
void TearDownOnMainThread() override {
menu_runner_ = nullptr;
ContentBrowserTest::TearDownOnMainThread();
}
std::unique_ptr<TestTouchSelectionMenuRunner> menu_runner_;
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraTest,
InitiallyInactive) {
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(true);
EXPECT_EQ(
GetRenderWidgetHostViewAura()->selection_controller()->active_status(),
ui::TouchSelectionController::INACTIVE);
EXPECT_EQ(GetRenderWidgetHostViewAura()
->selection_controller()
->GetVisibleRectBetweenBounds(),
gfx::RectF());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
}
// Tests that long-pressing on a text brings up selection handles and the quick
// menu properly.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraTest,
LongPressSelection) {
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(true);
// Long-press to select some text.
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
ui::test::EventGenerator generator(rwhva->GetNativeView()->GetRootWindow());
SelectWithLongPress(generator,
ConvertPointFromView(rwhva, generator.delegate(),
GetPointInText(/*cursor_index=*/2)));
// Touch selection should be active.
EXPECT_EQ(rwhva->selection_controller()->active_status(),
ui::TouchSelectionController::SELECTION_ACTIVE);
EXPECT_EQ(rwhva->GetSelectedText(), u"Some");
EXPECT_EQ(rwhva->selection_controller()->GetVisibleRectBetweenBounds().size(),
gfx::SizeF(4 * kCharacterWidth, kCharacterHeight));
// Selection handles and menu should not be shown while the long press is
// still being held down.
ui::TouchSelectionControllerTestApi selection_controller_test_api(
rwhva->selection_controller());
EXPECT_FALSE(selection_controller_test_api.GetStartVisible());
EXPECT_FALSE(selection_controller_test_api.GetEndVisible());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// Release the long press.
generator.ReleaseTouch();
// Touch selection handles and menu should now be showing.
EXPECT_EQ(rwhva->selection_controller()->active_status(),
ui::TouchSelectionController::SELECTION_ACTIVE);
EXPECT_EQ(rwhva->GetSelectedText(), u"Some");
EXPECT_EQ(rwhva->selection_controller()->GetVisibleRectBetweenBounds().size(),
gfx::SizeF(4 * kCharacterWidth, kCharacterHeight));
EXPECT_TRUE(selection_controller_test_api.GetStartVisible());
EXPECT_TRUE(selection_controller_test_api.GetEndVisible());
EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
}
class TouchSelectionControllerClientAuraSiteIsolationTest
: public TouchSelectionControllerClientAuraTest,
public testing::WithParamInterface<bool> {
public:
void SetUpCommandLine(base::CommandLine* command_line) override {
TouchSelectionControllerClientAuraTest::SetUpCommandLine(command_line);
IsolateAllSitesForTesting(command_line);
}
void SetUpOnMainThread() override {
TouchSelectionControllerClientAuraTest::SetUpOnMainThread();
host_resolver()->AddRule("*", "127.0.0.1");
SetupCrossSiteRedirector(embedded_test_server());
ASSERT_TRUE(embedded_test_server()->Start());
}
gfx::PointF GetPointInTextInFrame(RenderFrameHostImpl* frame,
int cursor_index) {
gfx::PointF point_in_text;
JSONToPoint(EvalJs(frame, "get_top_left_of_text()").ExtractString(),
&point_in_text);
point_in_text.Offset(cursor_index * kCharacterWidth,
0.5f * kCharacterHeight);
return point_in_text;
}
gfx::Point ConvertPointFromChildFrame(
RenderFrameHostImpl* child_frame,
const ui::test::EventGeneratorDelegate* generator_delegate,
const gfx::PointF& point_in_child_view) {
return ConvertPointFromView(
GetRenderWidgetHostViewAura(), generator_delegate,
static_cast<RenderWidgetHostViewChildFrame*>(
child_frame->GetRenderWidgetHost()->GetView())
->TransformPointToRootCoordSpaceF(point_in_child_view));
}
};
INSTANTIATE_TEST_SUITE_P(TouchSelectionForCrossProcessFramesTests,
TouchSelectionControllerClientAuraSiteIsolationTest,
testing::Bool());
IN_PROC_BROWSER_TEST_P(TouchSelectionControllerClientAuraSiteIsolationTest,
BasicSelectionIsolatedIframe) {
GURL test_url(embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(a)"));
EXPECT_TRUE(NavigateToURL(shell(), test_url));
FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents())
->GetPrimaryFrameTree()
.root();
EXPECT_EQ(
" Site A\n"
" +--Site A\n"
"Where A = http://a.com/",
DepictFrameTree(*root));
TestNavigationObserver observer(shell()->web_contents());
EXPECT_EQ(1u, root->child_count());
FrameTreeNode* child = root->child_at(0);
InitSelectionController(true);
// We need to load the desired subframe and then wait until it's stable, i.e.
// generates no new frames for some reasonable time period: a stray frame
// between touch selection's pre-handling of GestureLongPress and the
// expected frame containing the selected region can confuse the
// TouchSelectionController, causing it to fail to show selection handles.
// Note this is an issue with the TouchSelectionController in general, and
// not a property of this test.
GURL child_url(
embedded_test_server()->GetURL("b.com", "/touch_selection.html"));
EXPECT_TRUE(NavigateToURLFromRenderer(child, child_url));
EXPECT_EQ(
" Site A ------------ proxies for B\n"
" +--Site B ------- proxies for A\n"
"Where A = http://a.com/\n"
" B = http://b.com/",
DepictFrameTree(*root));
// The child will change with the cross-site navigation. It shouldn't change
// after this.
child = root->child_at(0);
WaitForHitTestData(child->current_frame_host());
RenderWidgetHostViewChildFrame* child_view =
static_cast<RenderWidgetHostViewChildFrame*>(
child->current_frame_host()->GetRenderWidgetHost()->GetView());
EXPECT_EQ(child_url, observer.last_navigation_url());
EXPECT_TRUE(observer.last_navigation_succeeded());
RenderWidgetHostViewAura* parent_view = GetRenderWidgetHostViewAura();
EXPECT_EQ(ui::TouchSelectionController::INACTIVE,
parent_view->selection_controller()->active_status());
EXPECT_EQ(gfx::RectF(),
parent_view->selection_controller()->GetVisibleRectBetweenBounds());
// Long press some text in the iframe to show selection handles.
ui::test::EventGenerator generator(
GetRenderWidgetHostViewAura()->GetNativeView()->GetRootWindow());
const gfx::Point point_in_text = ConvertPointFromChildFrame(
child->current_frame_host(), generator.delegate(),
GetPointInTextInFrame(child->current_frame_host(), /*cursor_index=*/2));
SelectWithLongPress(generator, point_in_text);
generator.ReleaseTouch();
// Check that selection is active and the quick menu is showing.
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
parent_view->selection_controller()->active_status());
EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
EXPECT_NE(gfx::RectF(),
parent_view->selection_controller()->GetVisibleRectBetweenBounds());
// Check that selection handles are cleared after tapping inside/outside the
// iframe.
selection_controller_client()->InitWaitForSelectionEvent(
ui::SELECTION_HANDLES_CLEARED);
const gfx::RectF rect_between_selection_bounds =
parent_view->selection_controller()->GetRectBetweenBounds();
if (GetParam()) {
// Tap a point outside the iframe that doesn't overlap with the selection
// handles or menu.
const gfx::PointF point_outside_iframe(
child_view->TransformPointToRootCoordSpaceF(gfx::PointF(-20.f, 0)).x(),
rect_between_selection_bounds.left_center().y());
generator.GestureTapAt(ConvertPointFromView(
parent_view, generator.delegate(), point_outside_iframe));
} else {
// Tap a point inside the iframe that doesn't overlap with the selection
// handles or menu.
const gfx::PointF point_inside_iframe(
rect_between_selection_bounds.right_center().x() + 10,
rect_between_selection_bounds.right_center().y());
generator.GestureTapAt(ConvertPointFromView(
parent_view, generator.delegate(), point_inside_iframe));
}
selection_controller_client()->Wait();
EXPECT_EQ(ui::TouchSelectionController::INACTIVE,
parent_view->selection_controller()->active_status());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
EXPECT_EQ(gfx::RectF(),
parent_view->selection_controller()->GetVisibleRectBetweenBounds());
}
// Failing in sanitizer runs: https://crbug.com/1405296
IN_PROC_BROWSER_TEST_P(TouchSelectionControllerClientAuraSiteIsolationTest,
DISABLED_BasicSelectionIsolatedScrollMainframe) {
GURL test_url(embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(a)"));
EXPECT_TRUE(NavigateToURL(shell(), test_url));
FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents())
->GetPrimaryFrameTree()
.root();
EXPECT_EQ(
" Site A\n"
" +--Site A\n"
"Where A = http://a.com/",
DepictFrameTree(*root));
TestNavigationObserver observer(shell()->web_contents());
EXPECT_EQ(1u, root->child_count());
FrameTreeNode* child = root->child_at(0);
// Make sure mainframe can scroll.
EXPECT_TRUE(ExecJs(shell()->web_contents(),
"document.body.style.height = '900px'; "
"document.body.style.overFlowY = 'scroll';"));
InitSelectionController(true);
// We need to load the desired subframe and then wait until it's stable, i.e.
// generates no new frames for some reasonable time period: a stray frame
// between touch selection's pre-handling of GestureLongPress and the
// expected frame containing the selected region can confuse the
// TouchSelectionController, causing it to fail to show selection handles.
// Note this is an issue with the TouchSelectionController in general, and
// not a property of this test.
GURL child_url(
embedded_test_server()->GetURL("b.com", "/touch_selection.html"));
EXPECT_TRUE(NavigateToURLFromRenderer(child, child_url));
EXPECT_EQ(
" Site A ------------ proxies for B\n"
" +--Site B ------- proxies for A\n"
"Where A = http://a.com/\n"
" B = http://b.com/",
DepictFrameTree(*root));
// The child will change with the cross-site navigation. It shouldn't change
// after this.
child = root->child_at(0);
WaitForHitTestData(child->current_frame_host());
EXPECT_EQ(child_url, observer.last_navigation_url());
EXPECT_TRUE(observer.last_navigation_succeeded());
RenderWidgetHostViewAura* parent_view = GetRenderWidgetHostViewAura();
ui::TouchSelectionController* selection_controller =
parent_view->selection_controller();
EXPECT_EQ(ui::TouchSelectionController::INACTIVE,
selection_controller->active_status());
EXPECT_EQ(gfx::RectF(), selection_controller->GetVisibleRectBetweenBounds());
ui::TouchSelectionControllerTestApi selection_controller_test_api(
selection_controller);
RenderFrameProxyHost* child_proxy_host =
child->render_manager()->GetProxyToParent();
auto interceptor = std::make_unique<SynchronizeVisualPropertiesInterceptor>(
child_proxy_host);
// Long press some text in the iframe to show selection handles.
ui::test::EventGenerator generator(
GetRenderWidgetHostViewAura()->GetNativeView()->GetRootWindow());
const gfx::Point point_in_text = ConvertPointFromChildFrame(
child->current_frame_host(), generator.delegate(),
GetPointInTextInFrame(child->current_frame_host(), /*cursor_index=*/2));
SelectWithLongPress(generator, point_in_text);
generator.ReleaseTouch();
// Check that selection is active and the quick menu is showing.
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
selection_controller->active_status());
EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
EXPECT_NE(gfx::RectF(), selection_controller->GetVisibleRectBetweenBounds());
gfx::Point scroll_start_position(10, 10);
gfx::Point scroll_end_position(10, 0);
// Initiate a touch scroll of the main frame, and make sure when the selection
// handles re-appear make sure they have the correct location.
// 1) Send touch-down.
ui::TouchEvent touch_down(
ui::EventType::kTouchPressed, scroll_start_position,
ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::kTouch, 0));
parent_view->OnTouchEvent(&touch_down);
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
selection_controller->active_status());
EXPECT_FALSE(selection_controller_test_api.temporarily_hidden());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
gfx::PointF initial_start_handle_position =
selection_controller->GetStartPosition();
// 2) Send touch-move.
ui::TouchEvent touch_move(
ui::EventType::kTouchMoved, scroll_end_position, ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::kTouch, 0));
parent_view->OnTouchEvent(&touch_move);
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
selection_controller->active_status());
EXPECT_FALSE(selection_controller_test_api.temporarily_hidden());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// Start scrolling: touch handles should get hidden, while touch selection is
// still active.
ui::GestureEventDetails scroll_begin_details(
ui::EventType::kGestureScrollBegin);
scroll_begin_details.set_device_type(
ui::GestureDeviceType::DEVICE_TOUCHSCREEN);
ui::GestureEvent scroll_begin(scroll_start_position.x(),
scroll_start_position.y(), 0,
ui::EventTimeForNow(), scroll_begin_details);
parent_view->OnGestureEvent(&scroll_begin);
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
parent_view->selection_controller()->active_status());
EXPECT_TRUE(selection_controller_test_api.temporarily_hidden());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// GestureScrollUpdate
gfx::Vector2dF scroll_delta = scroll_end_position - scroll_start_position;
ui::GestureEventDetails scroll_update_details(
ui::EventType::kGestureScrollUpdate, scroll_delta.x(), scroll_delta.y());
scroll_update_details.set_device_type(
ui::GestureDeviceType::DEVICE_TOUCHSCREEN);
ui::GestureEvent scroll_update(scroll_start_position.x(),
scroll_start_position.y(), 0,
ui::EventTimeForNow(), scroll_update_details);
parent_view->OnGestureEvent(&scroll_update);
EXPECT_TRUE(selection_controller_test_api.temporarily_hidden());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// Make sure we wait for the scroll to actually happen.
interceptor->WaitForRect();
// Since the check below that compares the scroll_delta to the actual handle
// movement requires use of TransformPointToRootCoordSpaceF() in
// TouchSelectionControllerClientChildFrame::DidScroll(), we need to
// make sure the post-scroll frames have rendered before the transform
// can be trusted. This may point out a general concern with the timing
// of the main-frame's did-stop-flinging IPC and the rendering of the
// child frame's compositor frame.
{
base::RunLoop loop;
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE, loop.QuitClosure(), TestTimeouts::tiny_timeout());
loop.Run();
}
// End scrolling: touch handles should re-appear.
ui::GestureEventDetails scroll_end_details(ui::EventType::kGestureScrollEnd);
scroll_end_details.set_device_type(ui::GestureDeviceType::DEVICE_TOUCHSCREEN);
ui::GestureEvent scroll_end(scroll_end_position.x(), scroll_end_position.y(),
0, ui::EventTimeForNow(), scroll_end_details);
parent_view->OnGestureEvent(&scroll_end);
EXPECT_FALSE(selection_controller_test_api.temporarily_hidden());
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
parent_view->selection_controller()->active_status());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// 3) Send touch-end.
ui::TouchEvent touch_up(ui::EventType::kTouchReleased, scroll_end_position,
ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::kTouch, 0));
parent_view->OnTouchEvent(&touch_up);
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
selection_controller->active_status());
EXPECT_FALSE(selection_controller_test_api.temporarily_hidden());
EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
EXPECT_NE(gfx::RectF(), selection_controller->GetVisibleRectBetweenBounds());
// Make sure handles have moved.
gfx::PointF final_start_handle_position =
selection_controller->GetStartPosition();
EXPECT_EQ(scroll_delta,
final_start_handle_position - initial_start_handle_position);
}
// Tests that the selection handles in a child view have their bounds updated
// when the main view is resized.
IN_PROC_BROWSER_TEST_P(TouchSelectionControllerClientAuraSiteIsolationTest,
SelectionHandlesIsolatedIframeMainViewResized) {
// Set the test page up.
const GURL test_url(embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(a)"));
EXPECT_TRUE(NavigateToURL(shell(), test_url));
FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents())
->GetPrimaryFrameTree()
.root();
EXPECT_EQ(DepictFrameTree(*root),
" Site A\n"
" +--Site A\n"
"Where A = http://a.com/");
TestNavigationObserver observer(shell()->web_contents());
EXPECT_EQ(root->child_count(), 1u);
FrameTreeNode* child = root->child_at(0);
InitSelectionController(true);
RenderWidgetHostViewAura* parent_view = GetRenderWidgetHostViewAura();
parent_view->SetSize(gfx::Size(600, 500));
WaitForHitTestData(root->current_frame_host());
const GURL child_url(
embedded_test_server()->GetURL("b.com", "/touch_selection.html"));
EXPECT_TRUE(NavigateToURLFromRenderer(child, child_url));
EXPECT_EQ(DepictFrameTree(*root),
" Site A ------------ proxies for B\n"
" +--Site B ------- proxies for A\n"
"Where A = http://a.com/\n"
" B = http://b.com/");
// The child will change with the cross-site navigation. It shouldn't change
// after this.
child = root->child_at(0);
WaitForHitTestData(child->current_frame_host());
RenderWidgetHostViewChildFrame* child_view =
static_cast<RenderWidgetHostViewChildFrame*>(
child->current_frame_host()->GetRenderWidgetHost()->GetView());
EXPECT_EQ(child_url, observer.last_navigation_url());
EXPECT_TRUE(observer.last_navigation_succeeded());
EXPECT_EQ(ui::TouchSelectionController::INACTIVE,
parent_view->selection_controller()->active_status());
EXPECT_EQ(gfx::RectF(),
parent_view->selection_controller()->GetVisibleRectBetweenBounds());
// Long press some text in the iframe to show selection handles.
ui::test::EventGenerator generator(
GetRenderWidgetHostViewAura()->GetNativeView()->GetRootWindow());
const gfx::Point point_in_text = ConvertPointFromChildFrame(
child->current_frame_host(), generator.delegate(),
GetPointInTextInFrame(child->current_frame_host(), /*cursor_index=*/2));
SelectWithLongPress(generator, point_in_text);
generator.ReleaseTouch();
// Selection handles should be shown.
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
parent_view->selection_controller()->active_status());
const gfx::RectF initial_selection_bounds_in_parent_coords =
parent_view->selection_controller()->GetRectBetweenBounds();
EXPECT_NE(initial_selection_bounds_in_parent_coords, gfx::RectF());
// Compute selection bounds in child view coordinates, to help determine the
// expected selection bounds after resizing the parent view.
const gfx::RectF initial_selection_bounds_in_child_coords =
ConvertRectFToChildCoords(parent_view, child_view,
initial_selection_bounds_in_parent_coords);
// Resize the parent view.
selection_controller_client()->InitWaitForSelectionEvent(
ui::SELECTION_HANDLES_MOVED);
parent_view->SetSize(gfx::Size(200, 200));
selection_controller_client()->Wait();
// Resizing the parent view changes the position of the child view within this
// parent, so we expect the selection handles to have moved and the selection
// bounds in the parent view to have changed.
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
parent_view->selection_controller()->active_status());
const gfx::RectF new_selection_bounds_in_parent_coords =
parent_view->selection_controller()->GetRectBetweenBounds();
EXPECT_NE(new_selection_bounds_in_parent_coords, gfx::RectF());
EXPECT_NE(new_selection_bounds_in_parent_coords,
initial_selection_bounds_in_parent_coords);
// The selection bounds should remain the same in child view coordinates.
EXPECT_EQ(ConvertRectFToChildCoords(parent_view, child_view,
new_selection_bounds_in_parent_coords),
initial_selection_bounds_in_child_coords);
}
IN_PROC_BROWSER_TEST_P(TouchSelectionControllerClientAuraSiteIsolationTest,
TouchSelectionDeactivatedAfterReload) {
const GURL main_url(embedded_test_server()->GetURL(
"a.com", "/cross_site_iframe_factory.html?a(a)"));
EXPECT_TRUE(NavigateToURL(shell(), main_url));
FrameTreeNode* root = static_cast<WebContentsImpl*>(shell()->web_contents())
->GetPrimaryFrameTree()
.root();
WaitForHitTestData(root->current_frame_host());
InitSelectionController(true);
{
// Load touch selection test page into iframe.
const GURL child_url(
embedded_test_server()->GetURL("b.com", "/touch_selection.html"));
EXPECT_TRUE(NavigateToURLFromRenderer(root->child_at(0), child_url));
RenderFrameHostImpl* iframe = root->child_at(0)->current_frame_host();
WaitForHitTestData(iframe);
// Long press some text in the iframe to show selection handles.
ui::test::EventGenerator generator(
GetRenderWidgetHostViewAura()->GetNativeView()->GetRootWindow());
const gfx::Point point_in_text = ConvertPointFromChildFrame(
iframe, generator.delegate(),
GetPointInTextInFrame(iframe, /*cursor_index=*/2));
SelectWithLongPress(generator, point_in_text);
generator.ReleaseTouch();
}
// Touch selection handles and menu should be active.
EXPECT_EQ(
ui::TouchSelectionController::SELECTION_ACTIVE,
GetRenderWidgetHostViewAura()->selection_controller()->active_status());
EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
{
// Reload web contents.
TestNavigationObserver reload_observer(shell()->web_contents());
shell()->web_contents()->GetController().Reload(
content::ReloadType::NORMAL, false /*check_for_repost=*/);
reload_observer.Wait();
}
InitSelectionController(true);
// Touch selection handles and menu should be deactivated.
EXPECT_EQ(
ui::TouchSelectionController::INACTIVE,
GetRenderWidgetHostViewAura()->selection_controller()->active_status());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
}
// Tests that tapping in a textfield brings up the insertion handle, but not the
// quick menu, initially. Then, successive taps on the insertion handle toggle
// the quick menu visibility.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraTest,
BasicInsertionFollowedByTapsOnHandle) {
// Set the test page up.
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(true);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
EXPECT_EQ(ui::TouchSelectionController::INACTIVE,
rwhva->selection_controller()->active_status());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
EXPECT_EQ(gfx::RectF(),
rwhva->selection_controller()->GetVisibleRectBetweenBounds());
gfx::NativeView native_view = rwhva->GetNativeView();
ui::test::EventGenerator generator(native_view->GetRootWindow());
// Tap inside the textfield and wait for the insertion handle to appear.
selection_controller_client()->InitWaitForSelectionEvent(
ui::INSERTION_HANDLE_SHOWN);
gfx::Point point = gfx::ToRoundedPoint(GetPointInTextfield(2));
generator.delegate()->ConvertPointFromTarget(native_view, &point);
generator.GestureTapAt(point);
selection_controller_client()->Wait();
// Check that insertion is active, but the quick menu is not showing.
EXPECT_EQ(ui::TouchSelectionController::INSERTION_ACTIVE,
rwhva->selection_controller()->active_status());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// Tap on the insertion handle; the quick menu should appear.
gfx::Point handle_center = gfx::ToRoundedPoint(
rwhva->selection_controller()->GetStartHandleRect().CenterPoint());
generator.delegate()->ConvertPointFromTarget(native_view, &handle_center);
generator.GestureTapAt(handle_center);
EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
EXPECT_NE(gfx::RectF(),
rwhva->selection_controller()->GetVisibleRectBetweenBounds());
// Tap once more on the insertion handle; the quick menu should disappear.
generator.GestureTapAt(handle_center);
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
}
#if BUILDFLAG(IS_CHROMEOS)
// Tests that the text selection can be adjusted by touch dragging after a long
// press gesture on readable text.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraTest,
LongPressDragSelectionReadableText) {
// Set the test page up.
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(false);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
gfx::NativeView native_view = rwhva->GetNativeView();
ui::test::EventGenerator generator(native_view->GetRootWindow());
// Long pressing on readable text should select the closest word.
gfx::Point point_in_readable_text = gfx::ToRoundedPoint(GetPointInText(2));
generator.delegate()->ConvertPointFromTarget(native_view,
&point_in_readable_text);
SelectWithLongPress(generator, point_in_readable_text);
EXPECT_EQ(rwhva->GetSelectedText(), u"Some");
EXPECT_EQ(rwhva->selection_controller()->GetRectBetweenBounds().size(),
gfx::SizeF(4 * kCharacterWidth, kCharacterHeight));
// Drag after the long press to adjust the selection.
InitiateTouchSelectionDragging(generator);
// Move the end of the selection from "Some|" to "Some text|".
DragAndWaitForSelectionUpdate(generator, 5 * kCharacterWidth, 0);
EXPECT_EQ(rwhva->GetSelectedText(), u"Some text");
EXPECT_EQ(rwhva->selection_controller()->GetRectBetweenBounds().size(),
gfx::SizeF(9 * kCharacterWidth, kCharacterHeight));
// Move the end of the selection from "Some text|" to "Some t|".
DragAndWaitForSelectionUpdate(generator, -3 * kCharacterWidth, 0);
EXPECT_EQ(rwhva->GetSelectedText(), u"Some t");
EXPECT_EQ(rwhva->selection_controller()->GetRectBetweenBounds().size(),
gfx::SizeF(6 * kCharacterWidth, kCharacterHeight));
}
// Tests that the text selection can be adjusted by touch dragging after a long
// press gesture on editable text.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraTest,
LongPressDragSelectionEditableText) {
// Set the test page up.
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(false);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
gfx::NativeView native_view = rwhva->GetNativeView();
ui::test::EventGenerator generator(native_view->GetRootWindow());
// Long pressing on editable text should select the closest word.
gfx::Point point_in_textfield = gfx::ToRoundedPoint(GetPointInTextfield(2));
generator.delegate()->ConvertPointFromTarget(native_view,
&point_in_textfield);
SelectWithLongPress(generator, point_in_textfield);
EXPECT_EQ(rwhva->GetSelectedText(), u"Some");
EXPECT_EQ(rwhva->selection_controller()->GetRectBetweenBounds().size(),
gfx::SizeF(4 * kCharacterWidth, kCharacterHeight));
// Drag after the long press to adjust the selection.
InitiateTouchSelectionDragging(generator);
// Move the end of the selection from "Some|" to "Some editable|".
DragAndWaitForSelectionUpdate(generator, 9 * kCharacterWidth, 0);
EXPECT_EQ(rwhva->GetSelectedText(), u"Some editable");
EXPECT_EQ(rwhva->selection_controller()->GetRectBetweenBounds().size(),
gfx::SizeF(13 * kCharacterWidth, kCharacterHeight));
// Drag the end of the selection from "Some editable|" to "Some editabl|".
DragAndWaitForSelectionUpdate(generator, -kCharacterWidth, 0);
EXPECT_EQ(rwhva->GetSelectedText(), u"Some editabl");
EXPECT_EQ(rwhva->selection_controller()->GetRectBetweenBounds().size(),
gfx::SizeF(12 * kCharacterWidth, kCharacterHeight));
}
// Tests that the text selection can be adjusted by touch dragging after a
// double press gesture on editable text.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraTest,
DoublePressDragSelection) {
// Set the test page up.
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(false);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
gfx::NativeView native_view = rwhva->GetNativeView();
ui::test::EventGenerator generator(native_view->GetRootWindow());
// Double pressing on editable text should select the closest word.
gfx::Point point_in_textfield = gfx::ToRoundedPoint(GetPointInTextfield(2));
generator.delegate()->ConvertPointFromTarget(native_view,
&point_in_textfield);
SelectWithDoublePress(generator, point_in_textfield);
EXPECT_EQ(rwhva->GetSelectedText(), u"Some");
EXPECT_EQ(rwhva->selection_controller()->GetRectBetweenBounds().size(),
gfx::SizeF(4 * kCharacterWidth, kCharacterHeight));
// Drag after the double press to adjust the selection.
InitiateTouchSelectionDragging(generator);
// Move the end of the selection from "Some|" to "Some editable|".
DragAndWaitForSelectionUpdate(generator, 9 * kCharacterWidth, 0);
EXPECT_EQ(rwhva->GetSelectedText(), u"Some editable");
EXPECT_EQ(rwhva->selection_controller()->GetRectBetweenBounds().size(),
gfx::SizeF(13 * kCharacterWidth, kCharacterHeight));
// Drag the end of the selection from "Some editable|" to "Some editabl|".
DragAndWaitForSelectionUpdate(generator, -kCharacterWidth, 0);
EXPECT_EQ(rwhva->GetSelectedText(), u"Some editabl");
EXPECT_EQ(rwhva->selection_controller()->GetRectBetweenBounds().size(),
gfx::SizeF(12 * kCharacterWidth, kCharacterHeight));
}
// Tests that touch selection dragging adjusts the selection using a direction
// strategy (roughly, expands by word and shrinks by character).
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraTest,
SelectionDraggingDirectionStrategy) {
// Set the test page up.
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(false);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
gfx::NativeView native_view = rwhva->GetNativeView();
ui::test::EventGenerator generator(native_view->GetRootWindow());
// Double press in editable text to select the closest word.
gfx::Point point_in_textfield = gfx::ToRoundedPoint(GetPointInTextfield(2));
generator.delegate()->ConvertPointFromTarget(native_view,
&point_in_textfield);
SelectWithDoublePress(generator, point_in_textfield);
EXPECT_EQ(rwhva->GetSelectedText(), u"Some");
EXPECT_EQ(rwhva->selection_controller()->GetRectBetweenBounds().size(),
gfx::SizeF(4 * kCharacterWidth, kCharacterHeight));
// Drag the end of the selection from "Some|" to "Some edita|". The selection
// should expand to the closest word boundary. Note that this creates an
// offset of 3 characters between the drag position and end of the selection.
InitiateTouchSelectionDragging(generator);
DragAndWaitForSelectionUpdate(generator, 6 * kCharacterWidth, 0);
EXPECT_EQ(rwhva->GetSelectedText(), u"Some editable");
EXPECT_EQ(rwhva->selection_controller()->GetRectBetweenBounds().size(),
gfx::SizeF(13 * kCharacterWidth, kCharacterHeight));
// Drag the end of the selection from "Some editable|" to "Some ed|". The
// selection should shrink to the nearest character boundary, preserving the
// offset between the drag position and end of the selection.
DragAndWaitForSelectionUpdate(generator, -6 * kCharacterWidth, 0);
EXPECT_EQ(rwhva->GetSelectedText(), u"Some ed");
EXPECT_EQ(rwhva->selection_controller()->GetRectBetweenBounds().size(),
gfx::SizeF(7 * kCharacterWidth, kCharacterHeight));
// Drag to remove the 3 character offset and move the end of the selection
// from "Some ed|" to "Some edit|". Since we are adjusting within a word, the
// selection expands with character granularity.
DragAndWaitForSelectionUpdate(generator, 5 * kCharacterWidth, 0);
EXPECT_EQ(rwhva->GetSelectedText(), u"Some edit");
EXPECT_EQ(rwhva->selection_controller()->GetRectBetweenBounds().size(),
gfx::SizeF(9 * kCharacterWidth, kCharacterHeight));
// Drag the end of the selection from "Some edit|" to "Some editable tex|".
// Since we have expanded past a word boundary, the selection should expand
// with word granularity again.
DragAndWaitForSelectionUpdate(generator, 8 * kCharacterWidth, 0);
EXPECT_EQ(rwhva->GetSelectedText(), u"Some editable text");
EXPECT_EQ(rwhva->selection_controller()->GetRectBetweenBounds().size(),
gfx::SizeF(18 * kCharacterWidth, kCharacterHeight));
}
// Tests that a magnifier is shown when touch selection dragging.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraTest,
SelectionDraggingShowsMagnifier) {
// Set the test page up.
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(false);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
gfx::NativeView native_view = rwhva->GetNativeView();
ui::test::EventGenerator generator(native_view->GetRootWindow());
// Double press in textfield then start touch selection dragging.
gfx::Point point_in_textfield = gfx::ToRoundedPoint(GetPointInTextfield(2));
generator.delegate()->ConvertPointFromTarget(native_view,
&point_in_textfield);
SelectWithDoublePress(generator, point_in_textfield);
InitiateTouchSelectionDragging(generator);
// Drag to move the selection.
selection_controller_client()->InitWaitForSelectionEvent(
ui::SELECTION_HANDLES_MOVED);
generator.MoveTouchBy(8 * kCharacterWidth, 0);
selection_controller_client()->Wait();
// Magnifier should be shown while dragging.
EXPECT_TRUE(selection_controller_client()->IsMagnifierVisible());
// Release touch to end the drag.
selection_controller_client()->InitWaitForSelectionEvent(
ui::SELECTION_HANDLE_DRAG_STOPPED);
generator.ReleaseTouch();
selection_controller_client()->Wait();
// Magnifier should be hidden after dragging stops.
EXPECT_FALSE(selection_controller_client()->IsMagnifierVisible());
}
// Tests that tapping the caret toggles showing and hiding the quick menu.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraTest, TapOnCaret) {
// Set the test page up.
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(true);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
const gfx::NativeView native_view = rwhva->GetNativeView();
ui::test::EventGenerator generator(native_view->GetRootWindow());
// Mouse click inside the textfield to make a caret appear.
selection_controller_client()->InitWaitForSelectionUpdate();
gfx::Point point = gfx::ToRoundedPoint(GetPointInTextfield(2));
generator.delegate()->ConvertPointFromTarget(native_view, &point);
generator.MoveMouseTo(point);
generator.PressLeftButton();
selection_controller_client()->Wait();
EXPECT_EQ(rwhva->selection_controller()->active_status(),
ui::TouchSelectionController::INACTIVE);
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// Tap the caret to show an insertion handle and quick menu.
selection_controller_client()->InitWaitForSelectionEvent(
ui::INSERTION_HANDLE_SHOWN);
selection_controller_client()->InitWaitForHandleContextMenu();
generator.GestureTapAt(point);
// Wait for both the insertion handle to be shown and for the context menu
// event to be handled, since these might not occur in a fixed order.
selection_controller_client()->Wait();
selection_controller_client()->WaitForHandleContextMenu();
EXPECT_EQ(rwhva->selection_controller()->active_status(),
ui::TouchSelectionController::INSERTION_ACTIVE);
EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// Tap the caret again to hide the quick menu. We advance the clock before
// tapping again to avoid the tap being treated as a double tap.
generator.AdvanceClock(base::Milliseconds(1000));
selection_controller_client()->InitWaitForHandleContextMenu();
generator.GestureTapAt(point);
selection_controller_client()->WaitForHandleContextMenu();
// The insertion handle should still be shown but menu should be hidden.
EXPECT_EQ(rwhva->selection_controller()->active_status(),
ui::TouchSelectionController::INSERTION_ACTIVE);
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
}
// Tests that the insertion handle and menu are hidden when a mouse event
// occurs, then can be shown again by tapping on the caret.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraTest,
HandleVisibilityAfterMouseEvent) {
// Set the test page up.
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(true);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
const gfx::NativeView native_view = rwhva->GetNativeView();
ui::test::EventGenerator generator(native_view->GetRootWindow());
// Tap inside the textfield to place a caret and show an insertion handle.
selection_controller_client()->InitWaitForSelectionEvent(
ui::INSERTION_HANDLE_SHOWN);
gfx::Point caret_location = gfx::ToRoundedPoint(GetPointInTextfield(2));
generator.delegate()->ConvertPointFromTarget(native_view, &caret_location);
generator.GestureTapAt(caret_location);
selection_controller_client()->Wait();
const gfx::RectF caret_bounds =
rwhva->selection_controller()->GetVisibleRectBetweenBounds();
EXPECT_NE(caret_bounds, gfx::RectF());
EXPECT_EQ(rwhva->selection_controller()->active_status(),
ui::TouchSelectionController::INSERTION_ACTIVE);
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// Move the mouse to hide the insertion handle.
selection_controller_client()->InitWaitForSelectionEvent(
ui::INSERTION_HANDLE_CLEARED);
generator.SendMouseEnter();
generator.MoveMouseBy(100, 100);
selection_controller_client()->Wait();
EXPECT_EQ(rwhva->selection_controller()->GetVisibleRectBetweenBounds(),
gfx::RectF());
EXPECT_EQ(rwhva->selection_controller()->active_status(),
ui::TouchSelectionController::INACTIVE);
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// Tap the caret to show the menu and make the insertion handle reappear. We
// advance the clock before tapping to avoid it being treated as a double tap.
generator.AdvanceClock(base::Milliseconds(1000));
selection_controller_client()->InitWaitForSelectionEvent(
ui::INSERTION_HANDLE_SHOWN);
generator.GestureTapAt(caret_location);
selection_controller_client()->Wait();
EXPECT_EQ(rwhva->selection_controller()->GetVisibleRectBetweenBounds(),
caret_bounds);
EXPECT_EQ(rwhva->selection_controller()->active_status(),
ui::TouchSelectionController::INSERTION_ACTIVE);
EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
}
#endif // BUILDFLAG(IS_CHROMEOS)
#if BUILDFLAG(IS_CHROMEOS)
// Tests that touch selection dragging records a histogram entry.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraTest,
SelectionDraggingMetrics) {
// Set the test page up.
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
base::HistogramTester histogram_tester;
InitSelectionController(false);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
gfx::NativeView native_view = rwhva->GetNativeView();
ui::test::EventGenerator generator(native_view->GetRootWindow());
gfx::Point point_in_textfield = gfx::ToRoundedPoint(GetPointInTextfield(2));
generator.delegate()->ConvertPointFromTarget(native_view,
&point_in_textfield);
// Long press drag selection.
SelectWithLongPress(generator, point_in_textfield);
InitiateTouchSelectionDragging(generator);
DragAndWaitForSelectionUpdate(generator, 9 * kCharacterWidth, 0);
generator.ReleaseTouch();
histogram_tester.ExpectBucketCount(ui::kTouchSelectionDragTypeHistogramName,
ui::TouchSelectionDragType::kLongPressDrag,
1);
histogram_tester.ExpectTotalCount(ui::kTouchSelectionDragTypeHistogramName,
1);
// Double press drag selection. Close the menu if needed so that it doesn't
// get in the way of the double press.
ui::TouchSelectionMenuRunner::GetInstance()->CloseMenu();
SelectWithDoublePress(generator, point_in_textfield);
InitiateTouchSelectionDragging(generator);
DragAndWaitForSelectionUpdate(generator, 10 * kCharacterWidth, 0);
generator.ReleaseTouch();
histogram_tester.ExpectBucketCount(
ui::kTouchSelectionDragTypeHistogramName,
ui::TouchSelectionDragType::kDoublePressDrag, 1);
histogram_tester.ExpectTotalCount(ui::kTouchSelectionDragTypeHistogramName,
2);
}
#endif // BUILDFLAG(IS_CHROMEOS)
// Tests that the quick menu is hidden whenever a touch point is active.
// Flaky: https://crbug.com/803576
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraTest,
DISABLED_QuickMenuHiddenOnTouch) {
// Set the test page up.
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(true);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
EXPECT_EQ(ui::TouchSelectionController::INACTIVE,
rwhva->selection_controller()->active_status());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
EXPECT_EQ(gfx::RectF(),
rwhva->selection_controller()->GetVisibleRectBetweenBounds());
// Long-press on the text and wait for selection handles to appear.
selection_controller_client()->InitWaitForSelectionEvent(
ui::SELECTION_HANDLES_SHOWN);
gfx::PointF point = GetPointInText(2);
ui::GestureEventDetails long_press_details(ui::EventType::kGestureLongPress);
long_press_details.set_device_type(ui::GestureDeviceType::DEVICE_TOUCHSCREEN);
ui::GestureEvent long_press(point.x(), point.y(), 0, ui::EventTimeForNow(),
long_press_details);
rwhva->OnGestureEvent(&long_press);
selection_controller_client()->Wait();
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
rwhva->selection_controller()->active_status());
EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
EXPECT_NE(gfx::RectF(),
rwhva->selection_controller()->GetVisibleRectBetweenBounds());
ui::test::EventGenerator generator(rwhva->GetNativeView()->GetRootWindow(),
rwhva->GetNativeView());
// Put the first finger down: the quick menu should get hidden.
generator.PressTouchId(0);
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
rwhva->selection_controller()->active_status());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// Put a second finger down: the quick menu should remain hidden.
generator.PressTouchId(1);
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
rwhva->selection_controller()->active_status());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// Lift the first finger up: the quick menu should still remain hidden.
generator.ReleaseTouchId(0);
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
rwhva->selection_controller()->active_status());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// Lift the second finger up: the quick menu should re-appear.
generator.ReleaseTouchId(1);
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
rwhva->selection_controller()->active_status());
EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
EXPECT_NE(gfx::RectF(),
rwhva->selection_controller()->GetVisibleRectBetweenBounds());
}
// Tests that the quick menu and touch handles are hidden during an scroll.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraTest, HiddenOnScroll) {
// Set the test page up.
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(true);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
ui::TouchSelectionControllerTestApi selection_controller_test_api(
rwhva->selection_controller());
EXPECT_EQ(ui::TouchSelectionController::INACTIVE,
rwhva->selection_controller()->active_status());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
EXPECT_EQ(gfx::RectF(),
rwhva->selection_controller()->GetVisibleRectBetweenBounds());
// Long-press on the text and wait for selection handles to appear.
selection_controller_client()->InitWaitForSelectionEvent(
ui::SELECTION_HANDLES_SHOWN);
gfx::PointF point = GetPointInText(2);
ui::GestureEventDetails long_press_details(ui::EventType::kGestureLongPress);
long_press_details.set_device_type(ui::GestureDeviceType::DEVICE_TOUCHSCREEN);
ui::GestureEvent long_press(point.x(), point.y(), 0, ui::EventTimeForNow(),
long_press_details);
rwhva->OnGestureEvent(&long_press);
selection_controller_client()->Wait();
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
rwhva->selection_controller()->active_status());
EXPECT_FALSE(selection_controller_test_api.temporarily_hidden());
EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
EXPECT_NE(gfx::RectF(),
rwhva->selection_controller()->GetVisibleRectBetweenBounds());
// Put a finger down: the quick menu should go away, while touch handles stay
// there.
ui::TouchEvent touch_down(
ui::EventType::kTouchPressed, gfx::Point(10, 10), ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::kTouch, 0));
rwhva->OnTouchEvent(&touch_down);
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
rwhva->selection_controller()->active_status());
EXPECT_FALSE(selection_controller_test_api.temporarily_hidden());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// Start scrolling: touch handles should get hidden, while touch selection is
// still active.
ui::GestureEventDetails scroll_begin_details(
ui::EventType::kGestureScrollBegin);
scroll_begin_details.set_device_type(
ui::GestureDeviceType::DEVICE_TOUCHSCREEN);
ui::GestureEvent scroll_begin(10, 10, 0, ui::EventTimeForNow(),
scroll_begin_details);
rwhva->OnGestureEvent(&scroll_begin);
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
rwhva->selection_controller()->active_status());
EXPECT_TRUE(selection_controller_test_api.temporarily_hidden());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// End scrolling: touch handles should re-appear.
ui::GestureEventDetails scroll_end_details(ui::EventType::kGestureScrollEnd);
scroll_end_details.set_device_type(ui::GestureDeviceType::DEVICE_TOUCHSCREEN);
ui::GestureEvent scroll_end(10, 10, 0, ui::EventTimeForNow(),
scroll_end_details);
rwhva->OnGestureEvent(&scroll_end);
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
rwhva->selection_controller()->active_status());
EXPECT_FALSE(selection_controller_test_api.temporarily_hidden());
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// Lift the finger up: the quick menu should re-appear.
ui::TouchEvent touch_up(ui::EventType::kTouchReleased, gfx::Point(10, 10),
ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::kTouch, 0));
rwhva->OnTouchEvent(&touch_up);
EXPECT_EQ(ui::TouchSelectionController::SELECTION_ACTIVE,
rwhva->selection_controller()->active_status());
EXPECT_FALSE(selection_controller_test_api.temporarily_hidden());
EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
EXPECT_NE(gfx::RectF(),
rwhva->selection_controller()->GetVisibleRectBetweenBounds());
}
// Tests that the magnifier is correctly shown for a swipe-to-move-cursor
// gesture.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraTest,
SwipeToMoveCursorMagnifier) {
// Set the test page up.
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(true);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
gfx::NativeView native_view = rwhva->GetNativeView();
ui::test::EventGenerator generator(native_view->GetRootWindow());
EXPECT_FALSE(selection_controller_client()->IsMagnifierVisible());
// Tap to focus the textfield.
selection_controller_client()->InitWaitForSelectionEvent(
ui::INSERTION_HANDLE_SHOWN);
gfx::Point start = gfx::ToRoundedPoint(GetPointInTextfield(2));
generator.delegate()->ConvertPointFromTarget(native_view, &start);
generator.GestureTapAt(start);
selection_controller_client()->Wait();
// Swipe to move the cursor. We advance the clock before swiping to avoid the
// start of the gesture being interpreted as a double press.
generator.AdvanceClock(base::Milliseconds(1000));
generator.GestureScrollSequenceWithCallback(
start, start + gfx::Vector2d(100, 0), /*duration=*/base::Milliseconds(50),
/*steps=*/5,
base::BindLambdaForTesting([&](ui::EventType event_type,
const gfx::Vector2dF& offset) {
if (event_type == ui::EventType::kGestureScrollBegin) {
selection_controller_client()->InitWaitForSelectionEvent(
ui::INSERTION_HANDLE_MOVED);
} else if (event_type == ui::EventType::kGestureScrollUpdate) {
selection_controller_client()->Wait();
EXPECT_TRUE(selection_controller_client()->IsMagnifierVisible());
selection_controller_client()->InitWaitForSelectionEvent(
ui::INSERTION_HANDLE_MOVED);
}
}));
EXPECT_FALSE(selection_controller_client()->IsMagnifierVisible());
}
// Tests that the select all menu command works correctly.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraTest,
SelectAllCommand) {
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(false);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
ui::TouchSelectionControllerTestApi selection_controller_test_api(
rwhva->selection_controller());
ui::test::EventGenerator generator(rwhva->GetNativeView()->GetRootWindow());
// Tap inside the textfield and wait for the insertion cursor to appear.
TapAndWaitForCursor(generator,
ConvertPointFromView(rwhva, generator.delegate(),
GetPointInTextfield(2)));
// Select all command should be enabled.
EXPECT_TRUE(
selection_controller_client()->GetActiveMenuClient()->IsCommandIdEnabled(
ui::TouchEditable::kSelectAll));
// Execute select all command.
selection_controller_client()->InitWaitForSelectionUpdate();
selection_controller_client()->GetActiveMenuClient()->ExecuteCommand(
ui::TouchEditable::kSelectAll, 0);
selection_controller_client()->Wait();
// All text in the textfield should be selected. Touch handles and quick menu
// should be shown and the select all command should now be disabled since all
// text is already selected.
EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
EXPECT_TRUE(selection_controller_test_api.GetStartVisible());
EXPECT_TRUE(selection_controller_test_api.GetEndVisible());
EXPECT_EQ(
selection_controller_client()->GetActiveMenuClient()->GetSelectedText(),
u"Some editable text");
EXPECT_FALSE(
selection_controller_client()->GetActiveMenuClient()->IsCommandIdEnabled(
ui::TouchEditable::kSelectAll));
}
// Tests that the select word menu command works correctly.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraTest,
SelectWordCommand) {
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(false);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
ui::TouchSelectionControllerTestApi selection_controller_test_api(
rwhva->selection_controller());
ui::test::EventGenerator generator(rwhva->GetNativeView()->GetRootWindow());
// Tap inside the textfield and wait for the insertion cursor to appear.
TapAndWaitForCursor(generator,
ConvertPointFromView(rwhva, generator.delegate(),
GetPointInTextfield(2)));
// Select word command should be enabled.
EXPECT_TRUE(
selection_controller_client()->GetActiveMenuClient()->IsCommandIdEnabled(
ui::TouchEditable::kSelectWord));
// Execute select word command.
selection_controller_client()->InitWaitForSelectionUpdate();
selection_controller_client()->GetActiveMenuClient()->ExecuteCommand(
ui::TouchEditable::kSelectWord, 0);
selection_controller_client()->Wait();
// The closest word should be selected. Touch handles and quick menu should be
// shown and the select word command should now be disabled since there is a
// non-empty selection.
EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
EXPECT_TRUE(selection_controller_test_api.GetStartVisible());
EXPECT_TRUE(selection_controller_test_api.GetEndVisible());
EXPECT_EQ(
selection_controller_client()->GetActiveMenuClient()->GetSelectedText(),
u"Some");
EXPECT_FALSE(
selection_controller_client()->GetActiveMenuClient()->IsCommandIdEnabled(
ui::TouchEditable::kSelectWord));
}
// Tests that the select all and select word commands in the quick menu are
// disabled for empty textfields.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraTest,
SelectCommandsEmptyTextfield) {
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(false);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
ui::test::EventGenerator generator(rwhva->GetNativeView()->GetRootWindow());
// Long-press on an empty textfield, then release to make an insertion handle
// appear.
SelectWithLongPress(generator,
ConvertPointFromView(rwhva, generator.delegate(),
GetPointInsideEmptyTextfield()));
generator.ReleaseTouch();
// Select all and select word commands should be disabled.
EXPECT_FALSE(
selection_controller_client()->GetActiveMenuClient()->IsCommandIdEnabled(
ui::TouchEditable::kSelectAll));
EXPECT_FALSE(
selection_controller_client()->GetActiveMenuClient()->IsCommandIdEnabled(
ui::TouchEditable::kSelectWord));
}
class TouchSelectionControllerClientAuraScaleFactorTest
: public TouchSelectionControllerClientAuraTest {
public:
static constexpr float kScaleFactor = 2.0f;
void SetUpCommandLine(base::CommandLine* command_line) override {
TouchSelectionControllerClientAuraTest::SetUpCommandLine(command_line);
command_line->AppendSwitchASCII(switches::kForceDeviceScaleFactor, "2");
}
};
// Tests that selection handles are properly positioned at 2x DSF.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraScaleFactorTest,
SelectionHandleCoordinates) {
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(true);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
const ui::TouchSelectionController* controller =
rwhva->selection_controller();
ui::test::EventGenerator generator(rwhva->GetNativeView()->GetRootWindow());
// Long-press to select some text, then release to make selection handles
// appear.
SelectWithLongPress(
generator,
ConvertPointFromView(rwhva, generator.delegate(), GetPointInText(2)));
generator.ReleaseTouch();
// Selection bounds should be non-empty.
const gfx::RectF initial_selection_bounds =
controller->GetVisibleRectBetweenBounds();
EXPECT_GT(initial_selection_bounds.width(), 0);
EXPECT_EQ(initial_selection_bounds.height(), kCharacterHeight);
// Handles should be shown just below the selection.
const float start_handle_top = controller->GetStartHandleRect().y();
const float end_handle_top = controller->GetEndHandleRect().y();
EXPECT_EQ(start_handle_top, end_handle_top);
EXPECT_LE(initial_selection_bounds.bottom(), end_handle_top);
EXPECT_LE(end_handle_top, initial_selection_bounds.bottom() + 10);
}
// Tests that selection handle coordinates are updated after dragging at 2x DSF.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraScaleFactorTest,
SelectionHandleCoordinatesAfterDrag) {
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(true);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
const ui::TouchSelectionController* controller =
rwhva->selection_controller();
ui::test::EventGenerator generator(rwhva->GetNativeView()->GetRootWindow());
// Long-press to select some text, then release to make selection handles
// appear.
SelectWithLongPress(
generator,
ConvertPointFromView(rwhva, generator.delegate(), GetPointInText(2)));
generator.ReleaseTouch();
const gfx::RectF start_handle_rect = controller->GetStartHandleRect();
const gfx::RectF end_handle_rect = controller->GetEndHandleRect();
// Drag to move the end handle one character left. Close the menu if needed
// before dragging so that it doesn't get in the way.
ui::TouchSelectionMenuRunner::GetInstance()->CloseMenu();
generator.PressTouch(ConvertPointFromView(rwhva, generator.delegate(),
end_handle_rect.CenterPoint()));
DragAndWaitForSelectionUpdate(generator, -kScaleFactor * kCharacterWidth, 0);
// The start handle should have remained in its initial position while the end
// handle should have been dragged one character left.
EXPECT_EQ(controller->GetStartHandleRect(), start_handle_rect);
EXPECT_EQ(controller->GetEndHandleRect(),
end_handle_rect - gfx::Vector2dF(kCharacterWidth, 0));
}
// Tests that the menu is correctly shown after dragging a selection handle.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraScaleFactorTest,
SelectionHandleDragShowsMenu) {
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(true);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
const ui::TouchSelectionController* controller =
rwhva->selection_controller();
ui::test::EventGenerator generator(rwhva->GetNativeView()->GetRootWindow());
// Long-press to select some text, then release to make selection handles
// appear.
SelectWithLongPress(
generator,
ConvertPointFromView(rwhva, generator.delegate(), GetPointInText(2)));
generator.ReleaseTouch();
// Menu should be shown if no drag is in progress.
EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// Drag to move the end handle one character left. Close the menu before
// dragging so that it doesn't get in the way.
ui::TouchSelectionMenuRunner::GetInstance()->CloseMenu();
generator.PressTouch(
ConvertPointFromView(rwhva, generator.delegate(),
controller->GetEndHandleRect().CenterPoint()));
DragAndWaitForSelectionUpdate(generator, -kScaleFactor * kCharacterWidth, 0);
// Menu should remain hidden while dragging.
EXPECT_FALSE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
// Release touch to end the drag.
generator.ReleaseTouch();
// Menu should be shown after drag finishes.
EXPECT_TRUE(ui::TouchSelectionMenuRunner::GetInstance()->IsRunning());
}
// Tests that the magnifier is correctly shown when dragging a selection handle.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraScaleFactorTest,
SelectionHandleDragShowsMagnifier) {
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(true);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
const ui::TouchSelectionController* controller =
rwhva->selection_controller();
ui::test::EventGenerator generator(rwhva->GetNativeView()->GetRootWindow());
// Long-press to select some text, then release to make selection handles
// appear.
SelectWithLongPress(
generator,
ConvertPointFromView(rwhva, generator.delegate(), GetPointInText(2)));
generator.ReleaseTouch();
// Magnifier should be hidden if no drag is in progress.
EXPECT_FALSE(selection_controller_client()->IsMagnifierVisible());
// Drag to move the end handle one character left. Close the menu before
// dragging so that it doesn't get in the way.
ui::TouchSelectionMenuRunner::GetInstance()->CloseMenu();
generator.PressTouch(
ConvertPointFromView(rwhva, generator.delegate(),
controller->GetEndHandleRect().CenterPoint()));
DragAndWaitForSelectionUpdate(generator, -kScaleFactor * kCharacterWidth, 0);
// Magnifier should be shown while dragging the handle.
EXPECT_TRUE(selection_controller_client()->IsMagnifierVisible());
// Release touch to end the drag.
generator.ReleaseTouch();
// Magnifier should be hidden after the drag is released.
EXPECT_FALSE(selection_controller_client()->IsMagnifierVisible());
}
// Tests that insertion handles are properly positioned at 2x DSF.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraScaleFactorTest,
InsertionHandleCoordinates) {
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(true);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
ui::test::EventGenerator generator(rwhva->GetNativeView()->GetRootWindow());
// Tap inside a textfield and wait for the insertion cursor to appear.
TapAndWaitForCursor(generator,
ConvertPointFromView(rwhva, generator.delegate(),
GetPointInTextfield(2)));
// Cursor bounds should be a zero-width rect.
const gfx::RectF cursor_bounds =
rwhva->selection_controller()->GetVisibleRectBetweenBounds();
EXPECT_EQ(cursor_bounds.size(), gfx::SizeF(0, kCharacterHeight));
// Insertion handle should be shown just below the cursor.
const gfx::RectF handle_rect =
rwhva->selection_controller()->GetEndHandleRect();
EXPECT_LE(cursor_bounds.bottom(), handle_rect.y());
EXPECT_LE(handle_rect.y(), cursor_bounds.bottom() + 10);
// Drag to move the cursor handle one character right.
selection_controller_client()->InitWaitForSelectionEvent(
ui::INSERTION_HANDLE_MOVED);
generator.PressTouch(ConvertPointFromView(rwhva, generator.delegate(),
handle_rect.CenterPoint()));
generator.MoveTouchBy(kScaleFactor * kCharacterWidth, 0);
selection_controller_client()->Wait();
// Cursor handle should have moved one character right.
EXPECT_EQ(rwhva->selection_controller()->GetEndHandleRect(),
handle_rect + gfx::Vector2dF(kCharacterWidth, 0));
}
// Tests that the magnifier is correctly shown when dragging a insertion handle.
IN_PROC_BROWSER_TEST_F(TouchSelectionControllerClientAuraScaleFactorTest,
InsertionHandleDragShowsMagnifier) {
ASSERT_NO_FATAL_FAILURE(StartTestWithPage("/touch_selection.html"));
InitSelectionController(true);
RenderWidgetHostViewAura* rwhva = GetRenderWidgetHostViewAura();
ui::test::EventGenerator generator(rwhva->GetNativeView()->GetRootWindow());
// Tap inside a textfield and wait for the insertion cursor to appear.
TapAndWaitForCursor(generator,
ConvertPointFromView(rwhva, generator.delegate(),
GetPointInTextfield(2)));
// Magnifier should be hidden if no drag is in progress.
EXPECT_FALSE(selection_controller_client()->IsMagnifierVisible());
// Drag to move the cursor handle one character right.
const gfx::RectF handle_rect =
rwhva->selection_controller()->GetEndHandleRect();
selection_controller_client()->InitWaitForSelectionEvent(
ui::INSERTION_HANDLE_MOVED);
generator.PressTouch(ConvertPointFromView(rwhva, generator.delegate(),
handle_rect.CenterPoint()));
generator.MoveTouchBy(kScaleFactor * kCharacterWidth, 0);
selection_controller_client()->Wait();
// Magnifier should be shown while dragging the handle.
EXPECT_TRUE(selection_controller_client()->IsMagnifierVisible());
// Release touch to end the drag.
generator.ReleaseTouch();
// Magnifier should be hidden after the drag is released.
EXPECT_FALSE(selection_controller_client()->IsMagnifierVisible());
}
} // namespace content
|