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 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
|
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import <Cocoa/Cocoa.h>
#include <objc/runtime.h>
#include <cstddef>
#include <memory>
#include <string>
#import "base/apple/foundation_util.h"
#import "base/apple/scoped_objc_class_swizzler.h"
#include "base/functional/bind.h"
#import "base/mac/mac_util.h"
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "base/strings/stringprintf.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/task_environment.h"
#import "components/remote_cocoa/app_shim/bridged_content_view.h"
#import "components/remote_cocoa/app_shim/native_widget_mac_nswindow.h"
#import "components/remote_cocoa/app_shim/native_widget_ns_window_bridge.h"
#import "components/remote_cocoa/app_shim/views_nswindow_delegate.h"
#import "testing/gtest_mac.h"
#include "ui/base/cocoa/find_pasteboard.h"
#import "ui/base/cocoa/window_size_constants.h"
#include "ui/base/ime/input_method.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#import "ui/base/test/cocoa_helper.h"
#include "ui/display/screen.h"
#include "ui/events/test/cocoa_test_event_utils.h"
#import "ui/gfx/mac/coordinate_conversion.h"
#import "ui/views/cocoa/native_widget_mac_ns_window_host.h"
#import "ui/views/cocoa/text_input_host.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/controls/textfield/textfield_controller.h"
#include "ui/views/controls/textfield/textfield_model.h"
#include "ui/views/test/test_views_delegate.h"
#include "ui/views/view.h"
#include "ui/views/widget/native_widget_mac.h"
#include "ui/views/widget/root_view.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_observer.h"
using base::ASCIIToUTF16;
using base::SysNSStringToUTF16;
using base::SysNSStringToUTF8;
using base::SysUTF16ToNSString;
using base::SysUTF8ToNSString;
#define EXPECT_EQ_RANGE(a, b) \
EXPECT_EQ(a.location, b.location); \
EXPECT_EQ(a.length, b.length);
// Helpers to verify an expectation against both the actual toolkit-views
// behaviour and the Cocoa behaviour.
#define EXPECT_NSEQ_3(expected_literal, expected_cocoa, actual_views) \
EXPECT_NSEQ(expected_literal, actual_views); \
EXPECT_NSEQ(expected_cocoa, actual_views);
#define EXPECT_EQ_RANGE_3(expected_literal, expected_cocoa, actual_views) \
EXPECT_EQ_RANGE(expected_literal, actual_views); \
EXPECT_EQ_RANGE(expected_cocoa, actual_views);
#define EXPECT_EQ_3(expected_literal, expected_cocoa, actual_views) \
EXPECT_EQ(expected_literal, actual_views); \
EXPECT_EQ(expected_cocoa, actual_views);
namespace {
// Implemented NSResponder action messages for use in tests.
NSArray* const kMoveActions = @[
@"moveForward:",
@"moveRight:",
@"moveBackward:",
@"moveLeft:",
@"moveUp:",
@"moveDown:",
@"moveWordForward:",
@"moveWordBackward:",
@"moveToBeginningOfLine:",
@"moveToEndOfLine:",
@"moveToBeginningOfParagraph:",
@"moveToEndOfParagraph:",
@"moveToEndOfDocument:",
@"moveToBeginningOfDocument:",
@"pageDown:",
@"pageUp:",
@"moveWordRight:",
@"moveWordLeft:",
@"moveToLeftEndOfLine:",
@"moveToRightEndOfLine:"
];
NSArray* const kSelectActions = @[
@"moveBackwardAndModifySelection:",
@"moveForwardAndModifySelection:",
@"moveWordForwardAndModifySelection:",
@"moveWordBackwardAndModifySelection:",
@"moveUpAndModifySelection:",
@"moveDownAndModifySelection:",
@"moveToBeginningOfLineAndModifySelection:",
@"moveToEndOfLineAndModifySelection:",
@"moveToBeginningOfParagraphAndModifySelection:",
@"moveToEndOfParagraphAndModifySelection:",
@"moveToEndOfDocumentAndModifySelection:",
@"moveToBeginningOfDocumentAndModifySelection:",
@"pageDownAndModifySelection:",
@"pageUpAndModifySelection:",
@"moveParagraphForwardAndModifySelection:",
@"moveParagraphBackwardAndModifySelection:",
@"moveRightAndModifySelection:",
@"moveLeftAndModifySelection:",
@"moveWordRightAndModifySelection:",
@"moveWordLeftAndModifySelection:",
@"moveToLeftEndOfLineAndModifySelection:",
@"moveToRightEndOfLineAndModifySelection:"
];
NSArray* const kDeleteActions = @[
@"deleteForward:", @"deleteBackward:", @"deleteWordForward:",
@"deleteWordBackward:", @"deleteToBeginningOfLine:", @"deleteToEndOfLine:",
@"deleteToBeginningOfParagraph:", @"deleteToEndOfParagraph:"
];
// This omits @"insertText:":. See BridgedNativeWidgetTest.NilTextInputClient.
NSArray* const kMiscActions = @[ @"cancelOperation:", @"transpose:", @"yank:" ];
// Empty range shortcut for readability.
NSRange EmptyRange() {
return NSMakeRange(NSNotFound, 0);
}
// Sets |composition_text| as the composition text with caret placed at
// |caret_pos| and updates |caret_range|.
void SetCompositionText(ui::TextInputClient* client,
const std::u16string& composition_text,
const int caret_pos,
NSRange* caret_range) {
ui::CompositionText composition;
composition.selection = gfx::Range(caret_pos);
composition.text = composition_text;
client->SetCompositionText(composition);
if (caret_range) {
*caret_range = NSMakeRange(caret_pos, 0);
}
}
// Returns a zero width rectangle corresponding to current caret position.
gfx::Rect GetCaretBounds(const ui::TextInputClient* client) {
gfx::Rect caret_bounds = client->GetCaretBounds();
caret_bounds.set_width(0);
return caret_bounds;
}
// Returns a zero width rectangle corresponding to caret bounds when it's placed
// at |caret_pos| and updates |caret_range|.
gfx::Rect GetCaretBoundsForPosition(ui::TextInputClient* client,
const std::u16string& composition_text,
const int caret_pos,
NSRange* caret_range) {
SetCompositionText(client, composition_text, caret_pos, caret_range);
return GetCaretBounds(client);
}
// Returns the expected boundary rectangle for characters of |composition_text|
// within the |query_range|.
gfx::Rect GetExpectedBoundsForRange(ui::TextInputClient* client,
const std::u16string& composition_text,
NSRange query_range) {
gfx::Rect left_caret = GetCaretBoundsForPosition(
client, composition_text, query_range.location, nullptr);
gfx::Rect right_caret = GetCaretBoundsForPosition(
client, composition_text, query_range.location + query_range.length,
nullptr);
// The expected bounds correspond to the area between the left and right caret
// positions.
return gfx::Rect(left_caret.x(), left_caret.y(),
right_caret.x() - left_caret.x(), left_caret.height());
}
// Uses the NSTextInputClient protocol to extract a substring from |view|.
NSString* GetViewStringForRange(NSView<NSTextInputClient>* view,
NSRange range) {
return [[view attributedSubstringForProposedRange:range
actualRange:nullptr] string];
}
// The behavior of NSTextView for RTL strings is buggy for some move and select
// commands, but only when the command is received when there is a selection
// active. E.g. moveRight: moves a cursor right in an RTL string, but it moves
// to the left-end of a selection. See TestEditingCommands() for specifics.
// This is filed as rdar://27863290.
bool IsRTLMoveBuggy(SEL sel) {
return sel == @selector(moveWordRight:) || sel == @selector(moveWordLeft:) ||
sel == @selector(moveRight:) || sel == @selector(moveLeft:);
}
bool IsRTLSelectBuggy(SEL sel) {
return sel == @selector(moveWordRightAndModifySelection:) ||
sel == @selector(moveWordLeftAndModifySelection:) ||
sel == @selector(moveRightAndModifySelection:) ||
sel == @selector(moveLeftAndModifySelection:);
}
// Used by InterpretKeyEventsDonorForNSView to simulate IME behavior.
using InterpretKeyEventsCallback = base::RepeatingCallback<void(id)>;
InterpretKeyEventsCallback* g_fake_interpret_key_events = nullptr;
// Used by UpdateWindowsDonorForNSApp to hook -[NSApp updateWindows].
base::RepeatingClosure* g_update_windows_closure = nullptr;
// Used to provide a return value for +[NSTextInputContext currentInputContext].
NSTextInputContext* g_fake_current_input_context = nullptr;
} // namespace
// Subclass of BridgedContentView with an override of interpretKeyEvents:. Note
// the size of the class must match BridgedContentView since the method table
// is swapped out at runtime. This is basically a mock, but mocks are banned
// under ui/views. Method swizzling causes these tests to flake when
// parallelized in the same process.
@interface InterpretKeyEventMockedBridgedContentView : BridgedContentView
@end
@implementation InterpretKeyEventMockedBridgedContentView
- (void)interpretKeyEvents:(NSArray<NSEvent*>*)eventArray {
ASSERT_TRUE(g_fake_interpret_key_events);
g_fake_interpret_key_events->Run(self);
}
@end
@interface UpdateWindowsDonorForNSApp : NSApplication
@end
@implementation UpdateWindowsDonorForNSApp
- (void)updateWindows {
ASSERT_TRUE(g_update_windows_closure);
g_update_windows_closure->Run();
}
@end
@interface CurrentInputContextDonorForNSTextInputContext : NSTextInputContext
@end
@implementation CurrentInputContextDonorForNSTextInputContext
+ (NSTextInputContext*)currentInputContext {
return g_fake_current_input_context;
}
@end
// Let's not mess with the machine's actual find pasteboard!
@interface MockFindPasteboard : FindPasteboard
@end
@implementation MockFindPasteboard {
NSString* __strong _text;
}
+ (FindPasteboard*)sharedInstance {
static MockFindPasteboard* instance = nil;
if (!instance) {
instance = [[MockFindPasteboard alloc] init];
}
return instance;
}
- (instancetype)init {
self = [super init];
if (self) {
_text = @"";
}
return self;
}
- (void)loadTextFromPasteboard:(NSNotification*)notification {
// No-op
}
- (void)setFindText:(NSString*)newText {
_text = [newText copy];
}
- (NSString*)findText {
return _text;
}
@end
@interface NativeWidgetMacNSWindowForTesting : NativeWidgetMacNSWindow {
BOOL hasShadowForTesting;
}
@end
// An NSTextStorage subclass for our DummyTextView, to work around test
// failures with macOS 13. See crbug.com/1446817 .
@interface DummyTextStorage : NSTextStorage {
NSMutableAttributedString* __strong _backingStore;
}
@end
@implementation DummyTextStorage
- (id)init {
self = [super init];
if (self) {
_backingStore = [[NSMutableAttributedString alloc] init];
}
return self;
}
- (NSString*)string {
return [_backingStore string];
}
- (NSDictionary*)attributesAtIndex:(NSUInteger)location
effectiveRange:(NSRangePointer)range {
return [_backingStore attributesAtIndex:location effectiveRange:range];
}
- (void)replaceCharactersInRange:(NSRange)range withString:(NSString*)str {
[self beginEditing];
[_backingStore replaceCharactersInRange:range withString:str];
[self edited:NSTextStorageEditedCharacters
range:range
changeInLength:str.length - range.length];
[self endEditing];
}
- (void)setAttributes:(NSDictionary<NSAttributedStringKey, id>*)attrs
range:(NSRange)range {
[self beginEditing];
[_backingStore setAttributes:attrs range:range];
[self edited:NSTextStorageEditedAttributes range:range changeInLength:0];
[self endEditing];
}
@end
// An NSTextView subclass that uses its own NSTextStorage subclass, to work
// around test failures with macOS 13. See crbug.com/1446817 .
@interface DummyTextView : NSTextView {
}
@end
@implementation DummyTextView
- (instancetype)initWithFrame:(NSRect)frameRect
textContainer:(NSTextContainer*)container {
DummyTextStorage* textStorage = [[DummyTextStorage alloc] init];
NSTextContainer* textContainer = [[NSTextContainer alloc]
initWithSize:NSMakeSize(CGFLOAT_MAX, CGFLOAT_MAX)];
NSLayoutManager* layoutManager = [[NSLayoutManager alloc] init];
[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
self = [super initWithFrame:frameRect textContainer:textContainer];
return self;
}
@end
@implementation NativeWidgetMacNSWindowForTesting
// Preserves the value of the hasShadow flag. During testing, -hasShadow will
// always return NO because shadows are disabled on the bots.
- (void)setHasShadow:(BOOL)flag {
hasShadowForTesting = flag;
[super setHasShadow:flag];
}
// Returns the value of the hasShadow flag during tests.
- (BOOL)hasShadowForTesting {
return hasShadowForTesting;
}
@end
namespace views::test {
// Provides the |parent| argument to construct a NativeWidgetNSWindowBridge.
class MockNativeWidgetMac : public NativeWidgetMac {
public:
explicit MockNativeWidgetMac(internal::NativeWidgetDelegate* delegate)
: NativeWidgetMac(delegate) {}
MockNativeWidgetMac(const MockNativeWidgetMac&) = delete;
MockNativeWidgetMac& operator=(const MockNativeWidgetMac&) = delete;
using NativeWidgetMac::GetInProcessNSWindowBridge;
using NativeWidgetMac::GetNSWindowHost;
// internal::NativeWidgetPrivate:
void InitNativeWidget(Widget::InitParams params) override {
ownership_ = params.ownership;
NativeWidgetMacNSWindow* window = [[NativeWidgetMacNSWindowForTesting alloc]
initWithContentRect:ui::kWindowSizeDeterminedLater
styleMask:NSWindowStyleMaskBorderless
backing:NSBackingStoreBuffered
defer:NO];
GetNSWindowHost()->CreateInProcessNSWindowBridge(window);
if (auto* parent =
NativeWidgetMacNSWindowHost::GetFromNativeView(params.parent)) {
GetNSWindowHost()->SetParent(parent);
}
GetNSWindowHost()->InitWindow(params,
ConvertBoundsToScreenIfNeeded(params.bounds));
// Usually the bridge gets initialized here. It is skipped to run extra
// checks in tests, and so that a second window isn't created.
delegate_for_testing()->OnNativeWidgetCreated();
// To allow events to dispatch to a view, it needs a way to get focus.
SetFocusManager(GetWidget()->GetFocusManager());
}
void ReorderNativeViews() override {
// Called via Widget::Init to set the content view. No-op in these tests.
}
};
// Helper test base to construct a NativeWidgetNSWindowBridge with a valid
// parent.
class BridgedNativeWidgetTestBase : public ui::CocoaTest,
public WidgetObserver {
public:
struct SkipInitialization {};
BridgedNativeWidgetTestBase()
: widget_(new Widget),
native_widget_mac_(new MockNativeWidgetMac(widget_.get())) {
observation_.Observe(widget_.get());
}
explicit BridgedNativeWidgetTestBase(SkipInitialization tag)
: native_widget_mac_(nullptr) {}
BridgedNativeWidgetTestBase(const BridgedNativeWidgetTestBase&) = delete;
BridgedNativeWidgetTestBase& operator=(const BridgedNativeWidgetTestBase&) =
delete;
remote_cocoa::NativeWidgetNSWindowBridge* bridge() {
return native_widget_mac_ ? native_widget_mac_->GetInProcessNSWindowBridge()
: nullptr;
}
NativeWidgetMacNSWindowHost* GetNSWindowHost() {
return native_widget_mac_ ? native_widget_mac_->GetNSWindowHost() : nullptr;
}
// Generate an autoreleased KeyDown NSEvent* in |widget_| for pressing the
// corresponding |key_code|.
NSEvent* VkeyKeyDown(ui::KeyboardCode key_code) {
return cocoa_test_event_utils::SynthesizeKeyEvent(
widget_->GetNativeWindow().GetNativeNSWindow(), true /* keyDown */,
key_code, 0);
}
// Generate an autoreleased KeyDown NSEvent* using the given keycode, and
// representing the first unicode character of |chars|.
NSEvent* UnicodeKeyDown(int key_code, NSString* chars) {
return cocoa_test_event_utils::KeyEventWithKeyCode(
key_code, [chars characterAtIndex:0], NSEventTypeKeyDown, 0);
}
// Overridden from testing::Test:
void SetUp() override {
ui::CocoaTest::SetUp();
Widget::InitParams init_params(ownership_);
init_params.native_widget = native_widget_mac_.get();
init_params.type = type_;
init_params.opacity = opacity_;
init_params.bounds = bounds_;
init_params.shadow_type = shadow_type_;
if (native_widget_mac_) {
native_widget_mac_->GetWidget()->Init(std::move(init_params));
}
}
void TearDown() override {
// ui::CocoaTest::TearDown will wait until all NSWindows are destroyed, so
// be sure to destroy the widget (which will destroy its NSWindow)
// beforehand.
native_widget_mac_ = nullptr;
if (widget_) {
observation_.Reset();
widget_->CloseNow();
}
ui::CocoaTest::TearDown();
}
NSWindow* bridge_window() const {
if (auto* bridge = native_widget_mac_->GetInProcessNSWindowBridge()) {
return bridge->ns_window();
}
return nil;
}
bool BridgeWindowHasShadow() {
return [base::apple::ObjCCast<NativeWidgetMacNSWindowForTesting>(
bridge_window()) hasShadowForTesting];
}
// Overridden from WidgetObserver:
void OnWidgetDestroyed(Widget* widget) override {
native_widget_mac_ = nullptr;
if (observation_.IsObservingSource(widget)) {
observation_.Reset();
}
}
protected:
std::unique_ptr<Widget> widget_;
base::ScopedObservation<Widget, WidgetObserver> observation_{this};
raw_ptr<MockNativeWidgetMac> native_widget_mac_; // Owned by `widget_`.
// Use a frameless window, otherwise Widget will try to center the window
// before the tests covering the Init() flow are ready to do that.
Widget::InitParams::Type type_ = Widget::InitParams::TYPE_WINDOW_FRAMELESS;
// To control the lifetime without an actual window that must be closed,
// tests in this file use CLIENT_OWNS_WIDGET.
Widget::InitParams::Ownership ownership_ =
Widget::InitParams::CLIENT_OWNS_WIDGET;
// Opacity defaults to "infer" which is usually updated by ViewsDelegate.
Widget::InitParams::WindowOpacity opacity_ =
Widget::InitParams::WindowOpacity::kOpaque;
gfx::Rect bounds_ = gfx::Rect(100, 100, 100, 100);
Widget::InitParams::ShadowType shadow_type_ =
Widget::InitParams::ShadowType::kDefault;
private:
TestViewsDelegate test_views_delegate_;
display::ScopedNativeScreen screen_;
};
class BridgedNativeWidgetTest : public BridgedNativeWidgetTestBase,
public TextfieldController {
public:
using HandleKeyEventCallback =
base::RepeatingCallback<bool(Textfield*, const ui::KeyEvent& key_event)>;
BridgedNativeWidgetTest();
BridgedNativeWidgetTest(const BridgedNativeWidgetTest&) = delete;
BridgedNativeWidgetTest& operator=(const BridgedNativeWidgetTest&) = delete;
~BridgedNativeWidgetTest() override;
// Install a textfield with input type |text_input_type| in the view hierarchy
// and make it the text input client. Also initializes |dummy_text_view_|.
Textfield* InstallTextField(
const std::u16string& text,
ui::TextInputType text_input_type = ui::TEXT_INPUT_TYPE_TEXT);
Textfield* InstallTextField(const std::string& text);
// Returns the actual current text for |ns_view_|, or the selected substring.
NSString* GetActualText();
NSString* GetActualSelectedText();
// Returns the expected current text from |dummy_text_view_|, or the selected
// substring.
NSString* GetExpectedText();
NSString* GetExpectedSelectedText();
// Returns the actual selection range for |ns_view_|.
NSRange GetActualSelectionRange();
// Returns the expected selection range from |dummy_text_view_|.
NSRange GetExpectedSelectionRange();
// Set the selection range for the installed textfield and |dummy_text_view_|.
void SetSelectionRange(NSRange range);
// Perform command |sel| on |ns_view_| and |dummy_text_view_|.
void PerformCommand(SEL sel);
// Make selection from |start| to |end| on installed views::Textfield and
// |dummy_text_view_|. If |start| > |end|, extend selection to left from
// |start|.
void MakeSelection(int start, int end);
// Helper method to set the private |keyDownEvent_| field on |ns_view_|.
void SetKeyDownEvent(NSEvent* event);
// Sets a callback to run on the next HandleKeyEvent().
void SetHandleKeyEventCallback(HandleKeyEventCallback callback);
// testing::Test:
void SetUp() override;
void TearDown() override;
// TextfieldController:
bool HandleKeyEvent(Textfield* sender,
const ui::KeyEvent& key_event) override;
protected:
// Test delete to beginning of line or paragraph based on |sel|. |sel| can be
// either deleteToBeginningOfLine: or deleteToBeginningOfParagraph:.
void TestDeleteBeginning(SEL sel);
// Test delete to end of line or paragraph based on |sel|. |sel| can be
// either deleteToEndOfLine: or deleteToEndOfParagraph:.
void TestDeleteEnd(SEL sel);
// Test editing commands in |selectors| against the expectations set by
// |dummy_text_view_|. This is done by selecting every substring within a set
// of test strings (both RTL and non-RTL by default) and performing every
// selector on both the NSTextView and the BridgedContentView hosting a
// focused views::TextField to ensure the resulting text and selection ranges
// match. |selectors| is an NSArray of NSStrings. |cases| determines whether
// RTL strings are to be tested.
void TestEditingCommands(NSArray* selectors);
std::unique_ptr<views::View> view_;
// Owned by bridge().
BridgedContentView* __weak ns_view_;
// An NSTextView which helps set the expectations for our tests.
NSTextView* __strong dummy_text_view_;
HandleKeyEventCallback handle_key_event_callback_;
base::test::SingleThreadTaskEnvironment task_environment_{
base::test::SingleThreadTaskEnvironment::MainThreadType::UI};
};
// Class that counts occurrences of a VKEY_RETURN accelerator, marking them
// processed.
class EnterAcceleratorView : public View {
METADATA_HEADER(EnterAcceleratorView, View)
public:
EnterAcceleratorView() { AddAccelerator({ui::VKEY_RETURN, 0}); }
int count() const { return count_; }
// View:
bool AcceleratorPressed(const ui::Accelerator& accelerator) override {
++count_;
return true;
}
private:
int count_ = 0;
};
BEGIN_METADATA(EnterAcceleratorView)
END_METADATA
BridgedNativeWidgetTest::BridgedNativeWidgetTest() = default;
BridgedNativeWidgetTest::~BridgedNativeWidgetTest() = default;
Textfield* BridgedNativeWidgetTest::InstallTextField(
const std::u16string& text,
ui::TextInputType text_input_type) {
Textfield* textfield = new Textfield();
textfield->SetText(text);
textfield->SetTextInputType(text_input_type);
textfield->set_controller(this);
view_->RemoveAllChildViews();
view_->AddChildViewRaw(textfield);
textfield->SetBoundsRect(bounds_);
// Request focus so the InputMethod can dispatch events to the RootView, and
// have them delivered to the textfield. Note that focusing a textfield
// schedules a task to flash the cursor, so this requires |message_loop_|.
textfield->RequestFocus();
GetNSWindowHost()->text_input_host()->SetTextInputClient(textfield);
// Initialize the dummy text view. Initializing this with NSZeroRect causes
// weird NSTextView behavior on OSX 10.9.
dummy_text_view_ =
[[DummyTextView alloc] initWithFrame:NSMakeRect(0, 0, 100, 100)];
[dummy_text_view_ setString:SysUTF16ToNSString(text)];
return textfield;
}
Textfield* BridgedNativeWidgetTest::InstallTextField(const std::string& text) {
return InstallTextField(base::ASCIIToUTF16(text));
}
NSString* BridgedNativeWidgetTest::GetActualText() {
return GetViewStringForRange(ns_view_, EmptyRange());
}
NSString* BridgedNativeWidgetTest::GetActualSelectedText() {
return GetViewStringForRange(ns_view_, [ns_view_ selectedRange]);
}
NSString* BridgedNativeWidgetTest::GetExpectedText() {
return GetViewStringForRange(dummy_text_view_, EmptyRange());
}
NSString* BridgedNativeWidgetTest::GetExpectedSelectedText() {
return GetViewStringForRange(dummy_text_view_,
[dummy_text_view_ selectedRange]);
}
NSRange BridgedNativeWidgetTest::GetActualSelectionRange() {
return [ns_view_ selectedRange];
}
NSRange BridgedNativeWidgetTest::GetExpectedSelectionRange() {
return [dummy_text_view_ selectedRange];
}
void BridgedNativeWidgetTest::SetSelectionRange(NSRange range) {
ui::TextInputClient* client = [ns_view_ textInputClientForTesting];
client->SetEditableSelectionRange(gfx::Range(range));
[dummy_text_view_ setSelectedRange:range];
}
void BridgedNativeWidgetTest::PerformCommand(SEL sel) {
[ns_view_ doCommandBySelector:sel];
[dummy_text_view_ doCommandBySelector:sel];
}
void BridgedNativeWidgetTest::MakeSelection(int start, int end) {
ui::TextInputClient* client = [ns_view_ textInputClientForTesting];
const gfx::Range range(start, end);
// Although a gfx::Range is directed, the underlying model will not choose an
// affinity until the cursor is moved.
client->SetEditableSelectionRange(range);
// Set the range without an affinity. The first @selector sent to the text
// field determines the affinity. Note that Range::ToNSRange() may discard
// the direction since NSRange has no direction.
[dummy_text_view_ setSelectedRange:range.ToNSRange()];
}
void BridgedNativeWidgetTest::SetKeyDownEvent(NSEvent* event) {
ns_view_.keyDownEventForTesting = event;
}
void BridgedNativeWidgetTest::SetHandleKeyEventCallback(
HandleKeyEventCallback callback) {
handle_key_event_callback_ = std::move(callback);
}
void BridgedNativeWidgetTest::SetUp() {
BridgedNativeWidgetTestBase::SetUp();
view_ = std::make_unique<views::internal::RootView>(widget_.get());
NSWindow* window = bridge_window();
// The delegate should exist before setting the root view.
EXPECT_TRUE([window delegate]);
GetNSWindowHost()->SetRootView(view_.get());
bridge()->CreateContentView(GetNSWindowHost()->GetRootViewNSViewId(),
view_->bounds(), std::nullopt);
ns_view_ = bridge()->ns_view();
// Pretend it has been shown via NativeWidgetMac::Show().
[window orderFront:nil];
[window makeFirstResponder:bridge()->ns_view()];
}
void BridgedNativeWidgetTest::TearDown() {
// Clear kill buffer so that no state persists between tests.
TextfieldModel::ClearKillBuffer();
if (GetNSWindowHost()) {
GetNSWindowHost()->SetRootView(nullptr);
bridge()->DestroyContentView();
}
view_.reset();
BridgedNativeWidgetTestBase::TearDown();
}
bool BridgedNativeWidgetTest::HandleKeyEvent(Textfield* sender,
const ui::KeyEvent& key_event) {
if (handle_key_event_callback_) {
return handle_key_event_callback_.Run(sender, key_event);
}
return false;
}
void BridgedNativeWidgetTest::TestDeleteBeginning(SEL sel) {
InstallTextField("foo bar baz");
EXPECT_EQ_RANGE_3(NSMakeRange(11, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Move the caret to the beginning of the line.
SetSelectionRange(NSMakeRange(0, 0));
// Verify no deletion takes place.
PerformCommand(sel);
EXPECT_NSEQ_3(@"foo bar baz", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(0, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Move the caret as- "foo| bar baz".
SetSelectionRange(NSMakeRange(3, 0));
PerformCommand(sel);
// Verify state is "| bar baz".
EXPECT_NSEQ_3(@" bar baz", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(0, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Make a selection as- " bar |baz|".
SetSelectionRange(NSMakeRange(5, 3));
PerformCommand(sel);
// Verify only the selection is deleted so that the state is " bar |".
EXPECT_NSEQ_3(@" bar ", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(5, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Verify yanking inserts the deleted text.
PerformCommand(@selector(yank:));
EXPECT_NSEQ_3(@" bar baz", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(8, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
}
void BridgedNativeWidgetTest::TestDeleteEnd(SEL sel) {
InstallTextField("foo bar baz");
EXPECT_EQ_RANGE_3(NSMakeRange(11, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Caret is at the end of the line. Verify no deletion takes place.
PerformCommand(sel);
EXPECT_NSEQ_3(@"foo bar baz", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(11, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Move the caret as- "foo bar| baz".
SetSelectionRange(NSMakeRange(7, 0));
PerformCommand(sel);
// Verify state is "foo bar|".
EXPECT_NSEQ_3(@"foo bar", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(7, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Make a selection as- "|foo |bar".
SetSelectionRange(NSMakeRange(0, 4));
PerformCommand(sel);
// Verify only the selection is deleted so that the state is "|bar".
EXPECT_NSEQ_3(@"bar", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(0, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Verify yanking inserts the deleted text.
PerformCommand(@selector(yank:));
EXPECT_NSEQ_3(@"foo bar", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(4, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
}
void BridgedNativeWidgetTest::TestEditingCommands(NSArray* selectors) {
struct {
std::u16string test_string;
bool is_rtl;
} test_cases[] = {
{u"ab c", false},
{u"\x0634\x0632 \x064A", true},
};
for (const auto& test_case : test_cases) {
for (NSString* selector_string in selectors) {
SEL sel = NSSelectorFromString(selector_string);
const int len = test_case.test_string.length();
for (int i = 0; i <= len; i++) {
for (int j = 0; j <= len; j++) {
SCOPED_TRACE(base::StringPrintf(
"Testing range [%d-%d] for case %s and selector %s\n", i, j,
base::UTF16ToUTF8(test_case.test_string).c_str(),
base::SysNSStringToUTF8(selector_string).c_str()));
InstallTextField(test_case.test_string);
MakeSelection(i, j);
// Sanity checks for MakeSelection().
EXPECT_NSEQ(GetExpectedSelectedText(), GetActualSelectedText());
EXPECT_EQ_RANGE_3(NSMakeRange(std::min(i, j), std::abs(i - j)),
GetExpectedSelectionRange(),
GetActualSelectionRange());
// Bail out early for selection-modifying commands that are buggy in
// Cocoa, since the selected text will not match.
if (test_case.is_rtl && i != j && IsRTLSelectBuggy(sel)) {
continue;
}
PerformCommand(sel);
EXPECT_NSEQ(GetExpectedSelectedText(), GetActualSelectedText());
EXPECT_NSEQ(GetExpectedText(), GetActualText());
// Spot-check some Cocoa RTL bugs. These only manifest when there is a
// selection (i != j), not for regular cursor moves.
if (test_case.is_rtl && i != j && IsRTLMoveBuggy(sel)) {
if (sel == @selector(moveRight:)) {
// Surely moving right with an rtl string moves to the start of a
// range (i.e. min). But Cocoa moves to the end.
EXPECT_EQ_RANGE(NSMakeRange(std::max(i, j), 0),
GetExpectedSelectionRange());
EXPECT_EQ_RANGE(NSMakeRange(std::min(i, j), 0),
GetActualSelectionRange());
} else if (sel == @selector(moveLeft:)) {
EXPECT_EQ_RANGE(NSMakeRange(std::min(i, j), 0),
GetExpectedSelectionRange());
EXPECT_EQ_RANGE(NSMakeRange(std::max(i, j), 0),
GetActualSelectionRange());
}
continue;
}
EXPECT_EQ_RANGE(GetExpectedSelectionRange(),
GetActualSelectionRange());
}
}
}
}
}
// The TEST_VIEW macro expects the view it's testing to have a superview. In
// these tests, the NSView bridge is a contentView, at the root. These mimic
// what TEST_VIEW usually does.
TEST_F(BridgedNativeWidgetTest, BridgedNativeWidgetTest_TestViewAddRemove) {
BridgedContentView* view = bridge()->ns_view();
NSWindow* window = bridge_window();
EXPECT_NSEQ([window contentView], view);
EXPECT_NSEQ(window, [view window]);
// The superview of a contentView is an NSNextStepFrame.
EXPECT_TRUE([view superview]);
EXPECT_TRUE([view bridge]);
// Ensure the tracking area to propagate mouseMoved: events to the RootView is
// installed.
EXPECT_EQ(1u, [[view trackingAreas] count]);
// Closing the window should tear down the C++ bridge, remove references to
// any C++ objects in the ObjectiveC object, and remove it from the hierarchy.
[window close];
EXPECT_FALSE([view bridge]);
EXPECT_FALSE([view superview]);
EXPECT_FALSE([view window]);
EXPECT_EQ(0u, [[view trackingAreas] count]);
EXPECT_FALSE([window contentView]);
EXPECT_FALSE([window delegate]);
}
TEST_F(BridgedNativeWidgetTest, BridgedNativeWidgetTest_TestViewDisplay) {
[bridge()->ns_view() display];
}
// Test that resizing the window resizes the root view appropriately.
TEST_F(BridgedNativeWidgetTest, ViewSizeTracksWindow) {
const int kTestNewWidth = 400;
const int kTestNewHeight = 300;
// |bridge_window()| is borderless, so these should align.
NSSize window_size = [bridge_window() frame].size;
EXPECT_EQ(view_->width(), static_cast<int>(window_size.width));
EXPECT_EQ(view_->height(), static_cast<int>(window_size.height));
// Make sure a resize actually occurs.
EXPECT_NE(kTestNewWidth, view_->width());
EXPECT_NE(kTestNewHeight, view_->height());
[bridge_window() setFrame:NSMakeRect(0, 0, kTestNewWidth, kTestNewHeight)
display:NO];
EXPECT_EQ(kTestNewWidth, view_->width());
EXPECT_EQ(kTestNewHeight, view_->height());
}
TEST_F(BridgedNativeWidgetTest, GetInputMethodShouldNotReturnNull) {
EXPECT_TRUE(native_widget_mac_->GetInputMethod());
}
// A simpler test harness for testing initialization flows.
class BridgedNativeWidgetInitTest : public BridgedNativeWidgetTestBase {
public:
BridgedNativeWidgetInitTest()
: BridgedNativeWidgetTestBase(SkipInitialization()) {}
BridgedNativeWidgetInitTest(const BridgedNativeWidgetInitTest&) = delete;
BridgedNativeWidgetInitTest& operator=(const BridgedNativeWidgetInitTest&) =
delete;
// Prepares a new |window_| and |widget_| for a call to PerformInit().
void CreateNewWidgetToInit() {
native_widget_mac_ = nullptr;
if (widget_) {
observation_.Reset();
widget_->CloseNow();
}
widget_ = std::make_unique<Widget>();
observation_.Observe(widget_.get());
native_widget_mac_ = new MockNativeWidgetMac(widget_.get());
}
void PerformInit() {
Widget::InitParams init_params(ownership_);
init_params.native_widget = native_widget_mac_.get();
init_params.type = type_;
init_params.opacity = opacity_;
init_params.bounds = bounds_;
init_params.shadow_type = shadow_type_;
widget_->Init(std::move(init_params));
}
};
// Test that NativeWidgetNSWindowBridge remains sane if Init() is never called.
TEST_F(BridgedNativeWidgetInitTest, InitNotCalled) {
// Don't use a Widget* as the delegate. ~Widget() checks for Widget::
// |native_widget_destroyed_| being set to true. That can only happen with a
// non-null WidgetDelegate, which is only set in Widget::Init(). Then, since
// neither Widget nor NativeWidget take ownership, use a unique_ptr.
std::unique_ptr<MockNativeWidgetMac> native_widget(
new MockNativeWidgetMac(nullptr));
native_widget_mac_ = native_widget.get();
EXPECT_FALSE(bridge());
EXPECT_FALSE(GetNSWindowHost()->GetInProcessNSWindow());
native_widget_mac_ = nullptr;
}
// Tests the shadow type given in InitParams.
TEST_F(BridgedNativeWidgetInitTest, ShadowType) {
// Verify Widget::InitParam defaults and arguments added from SetUp().
EXPECT_EQ(Widget::InitParams::TYPE_WINDOW_FRAMELESS, type_);
EXPECT_EQ(Widget::InitParams::WindowOpacity::kOpaque, opacity_);
EXPECT_EQ(Widget::InitParams::ShadowType::kDefault, shadow_type_);
CreateNewWidgetToInit();
EXPECT_FALSE(
BridgeWindowHasShadow()); // Default for NSWindowStyleMaskBorderless.
PerformInit();
// Borderless is 0, so isn't really a mask. Check that nothing is set.
EXPECT_EQ(NSWindowStyleMaskBorderless, [bridge_window() styleMask]);
EXPECT_TRUE(BridgeWindowHasShadow()); // ShadowType::kDefault means a shadow.
CreateNewWidgetToInit();
shadow_type_ = Widget::InitParams::ShadowType::kNone;
PerformInit();
EXPECT_FALSE(BridgeWindowHasShadow()); // Preserves lack of shadow.
// Default for Widget::InitParams::TYPE_WINDOW.
CreateNewWidgetToInit();
PerformInit();
EXPECT_FALSE(BridgeWindowHasShadow()); // ShadowType::kNone removes shadow.
shadow_type_ = Widget::InitParams::ShadowType::kDefault;
CreateNewWidgetToInit();
PerformInit();
EXPECT_TRUE(BridgeWindowHasShadow()); // Preserves shadow.
}
// Ensure a nil NSTextInputContext is returned when the ui::TextInputClient is
// not editable, a password field, or unset.
TEST_F(BridgedNativeWidgetTest, InputContext) {
const std::u16string test_string = u"test_str";
InstallTextField(test_string, ui::TEXT_INPUT_TYPE_PASSWORD);
EXPECT_FALSE([ns_view_ inputContext]);
InstallTextField(test_string, ui::TEXT_INPUT_TYPE_TEXT);
EXPECT_TRUE([ns_view_ inputContext]);
GetNSWindowHost()->text_input_host()->SetTextInputClient(nullptr);
EXPECT_FALSE([ns_view_ inputContext]);
InstallTextField(test_string, ui::TEXT_INPUT_TYPE_NONE);
EXPECT_FALSE([ns_view_ inputContext]);
}
// Test getting complete string using text input protocol.
TEST_F(BridgedNativeWidgetTest, TextInput_GetCompleteString) {
const std::string test_string = "foo bar baz";
InstallTextField(test_string);
NSRange range = NSMakeRange(0, test_string.size());
NSRange actual_range;
NSAttributedString* actual_text =
[ns_view_ attributedSubstringForProposedRange:range
actualRange:&actual_range];
NSRange expected_range;
NSAttributedString* expected_text =
[dummy_text_view_ attributedSubstringForProposedRange:range
actualRange:&expected_range];
EXPECT_NSEQ_3(@"foo bar baz", [expected_text string], [actual_text string]);
EXPECT_EQ_RANGE_3(range, expected_range, actual_range);
}
// Test getting middle substring using text input protocol.
TEST_F(BridgedNativeWidgetTest, TextInput_GetMiddleSubstring) {
const std::string test_string = "foo bar baz";
InstallTextField(test_string);
NSRange range = NSMakeRange(4, 3);
NSRange actual_range;
NSAttributedString* actual_text =
[ns_view_ attributedSubstringForProposedRange:range
actualRange:&actual_range];
NSRange expected_range;
NSAttributedString* expected_text =
[dummy_text_view_ attributedSubstringForProposedRange:range
actualRange:&expected_range];
EXPECT_NSEQ_3(@"bar", [expected_text string], [actual_text string]);
EXPECT_EQ_RANGE_3(range, expected_range, actual_range);
}
// Test getting ending substring using text input protocol.
TEST_F(BridgedNativeWidgetTest, TextInput_GetEndingSubstring) {
const std::string test_string = "foo bar baz";
InstallTextField(test_string);
NSRange range = NSMakeRange(8, 100);
NSRange actual_range;
NSAttributedString* actual_text =
[ns_view_ attributedSubstringForProposedRange:range
actualRange:&actual_range];
NSRange expected_range;
NSAttributedString* expected_text =
[dummy_text_view_ attributedSubstringForProposedRange:range
actualRange:&expected_range];
EXPECT_NSEQ_3(@"baz", [expected_text string], [actual_text string]);
EXPECT_EQ_RANGE_3(NSMakeRange(8, 3), expected_range, actual_range);
}
// Test getting empty substring using text input protocol.
TEST_F(BridgedNativeWidgetTest, TextInput_GetEmptySubstring) {
const std::string test_string = "foo bar baz";
InstallTextField(test_string);
// Try with EmptyRange(). This behaves specially and should return the
// complete string and the corresponding text range.
NSRange range = EmptyRange();
NSRange actual_range = EmptyRange();
NSRange expected_range = EmptyRange();
NSAttributedString* actual_text =
[ns_view_ attributedSubstringForProposedRange:range
actualRange:&actual_range];
NSAttributedString* expected_text =
[dummy_text_view_ attributedSubstringForProposedRange:range
actualRange:&expected_range];
EXPECT_NSEQ_3(@"foo bar baz", [expected_text string], [actual_text string]);
EXPECT_EQ_RANGE_3(NSMakeRange(0, test_string.length()), expected_range,
actual_range);
// Try with a valid empty range.
range = NSMakeRange(2, 0);
actual_range = EmptyRange();
expected_range = EmptyRange();
actual_text = [ns_view_ attributedSubstringForProposedRange:range
actualRange:&actual_range];
expected_text =
[dummy_text_view_ attributedSubstringForProposedRange:range
actualRange:&expected_range];
EXPECT_NSEQ_3(nil, [expected_text string], [actual_text string]);
EXPECT_EQ_RANGE_3(range, expected_range, actual_range);
// Try with an out of bounds empty range.
range = NSMakeRange(20, 0);
actual_range = EmptyRange();
expected_range = EmptyRange();
actual_text = [ns_view_ attributedSubstringForProposedRange:range
actualRange:&actual_range];
expected_text =
[dummy_text_view_ attributedSubstringForProposedRange:range
actualRange:&expected_range];
EXPECT_NSEQ_3(nil, [expected_text string], [actual_text string]);
EXPECT_EQ_RANGE_3(NSMakeRange(0, 0), expected_range, actual_range);
}
// Test inserting text using text input protocol.
TEST_F(BridgedNativeWidgetTest, TextInput_InsertText) {
const std::string test_string = "foo";
InstallTextField(test_string);
[ns_view_ insertText:SysUTF8ToNSString(test_string)
replacementRange:EmptyRange()];
[dummy_text_view_ insertText:SysUTF8ToNSString(test_string)
replacementRange:EmptyRange()];
EXPECT_NSEQ_3(@"foofoo", GetExpectedText(), GetActualText());
}
// Test replacing text using text input protocol.
TEST_F(BridgedNativeWidgetTest, TextInput_ReplaceText) {
const std::string test_string = "foo bar";
InstallTextField(test_string);
[ns_view_ insertText:@"baz" replacementRange:NSMakeRange(4, 3)];
[dummy_text_view_ insertText:@"baz" replacementRange:NSMakeRange(4, 3)];
EXPECT_NSEQ_3(@"foo baz", GetExpectedText(), GetActualText());
}
// Test IME composition using text input protocol.
TEST_F(BridgedNativeWidgetTest, TextInput_Compose) {
const std::string test_string = "foo ";
InstallTextField(test_string);
EXPECT_EQ_3(NO, [dummy_text_view_ hasMarkedText], [ns_view_ hasMarkedText]);
// As per NSTextInputClient documentation, markedRange should return
// {NSNotFound, 0} iff hasMarked returns false. However, NSTextView returns
// {text_length, text_length} for this case. We maintain consistency with the
// documentation, hence the EXPECT_FALSE check.
EXPECT_FALSE(
NSEqualRanges([dummy_text_view_ markedRange], [ns_view_ markedRange]));
EXPECT_EQ_RANGE(EmptyRange(), [ns_view_ markedRange]);
// Start composition.
NSString* compositionText = @"bar";
NSUInteger compositionLength = [compositionText length];
[ns_view_ setMarkedText:compositionText
selectedRange:NSMakeRange(0, 2)
replacementRange:EmptyRange()];
[dummy_text_view_ setMarkedText:compositionText
selectedRange:NSMakeRange(0, 2)
replacementRange:EmptyRange()];
EXPECT_EQ_3(YES, [dummy_text_view_ hasMarkedText], [ns_view_ hasMarkedText]);
EXPECT_EQ_RANGE_3(NSMakeRange(test_string.size(), compositionLength),
[dummy_text_view_ markedRange], [ns_view_ markedRange]);
EXPECT_EQ_RANGE_3(NSMakeRange(test_string.size(), 2),
GetExpectedSelectionRange(), GetActualSelectionRange());
// Confirm composition.
[ns_view_ unmarkText];
[dummy_text_view_ unmarkText];
EXPECT_EQ_3(NO, [dummy_text_view_ hasMarkedText], [ns_view_ hasMarkedText]);
EXPECT_FALSE(
NSEqualRanges([dummy_text_view_ markedRange], [ns_view_ markedRange]));
EXPECT_EQ_RANGE(EmptyRange(), [ns_view_ markedRange]);
EXPECT_NSEQ_3(@"foo bar", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange([GetActualText() length], 0),
GetExpectedSelectionRange(), GetActualSelectionRange());
}
// Test IME composition for accented characters.
TEST_F(BridgedNativeWidgetTest, TextInput_AccentedCharacter) {
InstallTextField("abc");
// Simulate action messages generated when the key 'a' is pressed repeatedly
// and leads to the showing of an IME candidate window. To simulate an event,
// set the private keyDownEvent field on the BridgedContentView.
// First an insertText: message with key 'a' is generated.
SetKeyDownEvent(cocoa_test_event_utils::SynthesizeKeyEvent(
widget_->GetNativeWindow().GetNativeNSWindow(), true, ui::VKEY_A, 0));
[ns_view_ insertText:@"a" replacementRange:EmptyRange()];
[dummy_text_view_ insertText:@"a" replacementRange:EmptyRange()];
SetKeyDownEvent(nil);
EXPECT_EQ_3(NO, [dummy_text_view_ hasMarkedText], [ns_view_ hasMarkedText]);
EXPECT_NSEQ_3(@"abca", GetExpectedText(), GetActualText());
// Next the IME popup appears. On selecting the accented character using arrow
// keys, setMarkedText action message is generated which replaces the earlier
// inserted 'a'.
SetKeyDownEvent(cocoa_test_event_utils::SynthesizeKeyEvent(
widget_->GetNativeWindow().GetNativeNSWindow(), true, ui::VKEY_RIGHT, 0));
[ns_view_ setMarkedText:@"à"
selectedRange:NSMakeRange(0, 1)
replacementRange:NSMakeRange(3, 1)];
[dummy_text_view_ setMarkedText:@"à"
selectedRange:NSMakeRange(0, 1)
replacementRange:NSMakeRange(3, 1)];
SetKeyDownEvent(nil);
EXPECT_EQ_3(YES, [dummy_text_view_ hasMarkedText], [ns_view_ hasMarkedText]);
EXPECT_EQ_RANGE_3(NSMakeRange(3, 1), [dummy_text_view_ markedRange],
[ns_view_ markedRange]);
EXPECT_EQ_RANGE_3(NSMakeRange(3, 1), GetExpectedSelectionRange(),
GetActualSelectionRange());
EXPECT_NSEQ_3(@"abcà", GetExpectedText(), GetActualText());
// On pressing enter, the marked text is confirmed.
SetKeyDownEvent(cocoa_test_event_utils::SynthesizeKeyEvent(
widget_->GetNativeWindow().GetNativeNSWindow(), true, ui::VKEY_RETURN,
0));
[ns_view_ insertText:@"à" replacementRange:EmptyRange()];
[dummy_text_view_ insertText:@"à" replacementRange:EmptyRange()];
SetKeyDownEvent(nil);
EXPECT_EQ_3(NO, [dummy_text_view_ hasMarkedText], [ns_view_ hasMarkedText]);
EXPECT_EQ_RANGE_3(NSMakeRange(4, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
EXPECT_NSEQ_3(@"abcà", GetExpectedText(), GetActualText());
}
// Test moving the caret left and right using text input protocol.
TEST_F(BridgedNativeWidgetTest, TextInput_MoveLeftRight) {
InstallTextField("foo");
EXPECT_EQ_RANGE_3(NSMakeRange(3, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Move right not allowed, out of range.
PerformCommand(@selector(moveRight:));
EXPECT_EQ_RANGE_3(NSMakeRange(3, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Move left.
PerformCommand(@selector(moveLeft:));
EXPECT_EQ_RANGE_3(NSMakeRange(2, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Move right.
PerformCommand(@selector(moveRight:));
EXPECT_EQ_RANGE_3(NSMakeRange(3, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
}
// Test backward delete using text input protocol.
TEST_F(BridgedNativeWidgetTest, TextInput_DeleteBackward) {
InstallTextField("a");
EXPECT_EQ_RANGE_3(NSMakeRange(1, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Delete one character.
PerformCommand(@selector(deleteBackward:));
EXPECT_NSEQ_3(nil, GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(0, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Verify that deletion did not modify the kill buffer.
PerformCommand(@selector(yank:));
EXPECT_NSEQ_3(nil, GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(0, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Try to delete again on an empty string.
PerformCommand(@selector(deleteBackward:));
EXPECT_NSEQ_3(nil, GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(0, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
}
// Test forward delete using text input protocol.
TEST_F(BridgedNativeWidgetTest, TextInput_DeleteForward) {
InstallTextField("a");
EXPECT_EQ_RANGE_3(NSMakeRange(1, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// At the end of the string, can't delete forward.
PerformCommand(@selector(deleteForward:));
EXPECT_NSEQ_3(@"a", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(1, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Should succeed after moving left first.
PerformCommand(@selector(moveLeft:));
PerformCommand(@selector(deleteForward:));
EXPECT_NSEQ_3(nil, GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(0, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Verify that deletion did not modify the kill buffer.
PerformCommand(@selector(yank:));
EXPECT_NSEQ_3(nil, GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(0, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
}
// Test forward word deletion using text input protocol.
TEST_F(BridgedNativeWidgetTest, TextInput_DeleteWordForward) {
InstallTextField("foo bar baz");
EXPECT_EQ_RANGE_3(NSMakeRange(11, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Caret is at the end of the line. Verify no deletion takes place.
PerformCommand(@selector(deleteWordForward:));
EXPECT_NSEQ_3(@"foo bar baz", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(11, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Move the caret as- "foo b|ar baz".
SetSelectionRange(NSMakeRange(5, 0));
PerformCommand(@selector(deleteWordForward:));
// Verify state is "foo b| baz"
EXPECT_NSEQ_3(@"foo b baz", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(5, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Make a selection as- "|fo|o b baz".
SetSelectionRange(NSMakeRange(0, 2));
PerformCommand(@selector(deleteWordForward:));
// Verify only the selection is deleted and state is "|o b baz".
EXPECT_NSEQ_3(@"o b baz", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(0, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Verify that deletion did not modify the kill buffer.
PerformCommand(@selector(yank:));
EXPECT_NSEQ_3(@"o b baz", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(0, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
}
// Test backward word deletion using text input protocol.
TEST_F(BridgedNativeWidgetTest, TextInput_DeleteWordBackward) {
InstallTextField("foo bar baz");
EXPECT_EQ_RANGE_3(NSMakeRange(11, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Move the caret to the beginning of the line.
SetSelectionRange(NSMakeRange(0, 0));
// Verify no deletion takes place.
PerformCommand(@selector(deleteWordBackward:));
EXPECT_NSEQ_3(@"foo bar baz", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(0, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Move the caret as- "foo ba|r baz".
SetSelectionRange(NSMakeRange(6, 0));
PerformCommand(@selector(deleteWordBackward:));
// Verify state is "foo |r baz".
EXPECT_NSEQ_3(@"foo r baz", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(4, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Make a selection as- "f|oo r b|az".
SetSelectionRange(NSMakeRange(1, 6));
PerformCommand(@selector(deleteWordBackward:));
// Verify only the selection is deleted and state is "f|az"
EXPECT_NSEQ_3(@"faz", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(1, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
// Verify that deletion did not modify the kill buffer.
PerformCommand(@selector(yank:));
EXPECT_NSEQ_3(@"faz", GetExpectedText(), GetActualText());
EXPECT_EQ_RANGE_3(NSMakeRange(1, 0), GetExpectedSelectionRange(),
GetActualSelectionRange());
}
// Test deleting to beginning/end of line/paragraph using text input protocol.
TEST_F(BridgedNativeWidgetTest, TextInput_DeleteToBeginningOfLine) {
TestDeleteBeginning(@selector(deleteToBeginningOfLine:));
}
TEST_F(BridgedNativeWidgetTest, TextInput_DeleteToEndOfLine) {
TestDeleteEnd(@selector(deleteToEndOfLine:));
}
TEST_F(BridgedNativeWidgetTest, TextInput_DeleteToBeginningOfParagraph) {
TestDeleteBeginning(@selector(deleteToBeginningOfParagraph:));
}
TEST_F(BridgedNativeWidgetTest, TextInput_DeleteToEndOfParagraph) {
TestDeleteEnd(@selector(deleteToEndOfParagraph:));
}
// Test move commands against expectations set by |dummy_text_view_|.
TEST_F(BridgedNativeWidgetTest, TextInput_MoveEditingCommands) {
TestEditingCommands(kMoveActions);
}
// Test move and select commands against expectations set by |dummy_text_view_|.
TEST_F(BridgedNativeWidgetTest, TextInput_MoveAndSelectEditingCommands) {
TestEditingCommands(kSelectActions);
}
// Test delete commands against expectations set by |dummy_text_view_|.
TEST_F(BridgedNativeWidgetTest, TextInput_DeleteCommands) {
TestEditingCommands(kDeleteActions);
}
// Test that we don't crash during an action message even if the TextInputClient
// is nil. Regression test for crbug.com/615745.
TEST_F(BridgedNativeWidgetTest, NilTextInputClient) {
GetNSWindowHost()->text_input_host()->SetTextInputClient(nullptr);
NSMutableArray* selectors = [NSMutableArray array];
[selectors addObjectsFromArray:kMoveActions];
[selectors addObjectsFromArray:kSelectActions];
[selectors addObjectsFromArray:kDeleteActions];
// -insertText: is omitted from this list to avoid a DCHECK in
// doCommandBySelector:. AppKit never passes -insertText: to
// doCommandBySelector: (it calls -insertText: directly instead).
[selectors addObjectsFromArray:kMiscActions];
for (NSString* selector in selectors) {
[ns_view_ doCommandBySelector:NSSelectorFromString(selector)];
}
[ns_view_ insertText:@""];
}
// Test transpose command against expectations set by |dummy_text_view_|.
TEST_F(BridgedNativeWidgetTest, TextInput_Transpose) {
TestEditingCommands(@[ @"transpose:" ]);
}
// Test firstRectForCharacterRange:actualRange for cases where query range is
// empty or outside composition range.
TEST_F(BridgedNativeWidgetTest, TextInput_FirstRectForCharacterRange_Caret) {
InstallTextField("");
ui::TextInputClient* client = [ns_view_ textInputClientForTesting];
// No composition. Ensure bounds and range corresponding to the current caret
// position are returned.
// Initially selection range will be [0, 0].
NSRange caret_range = NSMakeRange(0, 0);
NSRange query_range = NSMakeRange(1, 1);
NSRange actual_range;
NSRect rect = [ns_view_ firstRectForCharacterRange:query_range
actualRange:&actual_range];
EXPECT_EQ(GetCaretBounds(client), gfx::ScreenRectFromNSRect(rect));
EXPECT_EQ_RANGE(caret_range, actual_range);
// Set composition with caret before second character ('e').
const std::u16string test_string = u"test_str";
const size_t kTextLength = 8;
SetCompositionText(client, test_string, 1, &caret_range);
// Test bounds returned for empty ranges within composition range. As per
// Apple's documentation for firstRectForCharacterRange:actualRange:, for an
// empty query range, the returned rectangle should coincide with the
// insertion point and have zero width. However in our implementation, if the
// empty query range lies within the composition range, we return a zero width
// rectangle corresponding to the query range location.
// Test bounds returned for empty range before second character ('e') are same
// as caret bounds when placed before second character.
query_range = NSMakeRange(1, 0);
rect = [ns_view_ firstRectForCharacterRange:query_range
actualRange:&actual_range];
EXPECT_EQ(GetCaretBoundsForPosition(client, test_string, 1, &caret_range),
gfx::ScreenRectFromNSRect(rect));
EXPECT_EQ_RANGE(query_range, actual_range);
// Test bounds returned for empty range after the composition text are same as
// caret bounds when placed after the composition text.
query_range = NSMakeRange(kTextLength, 0);
rect = [ns_view_ firstRectForCharacterRange:query_range
actualRange:&actual_range];
EXPECT_NE(GetCaretBoundsForPosition(client, test_string, 1, &caret_range),
gfx::ScreenRectFromNSRect(rect));
EXPECT_EQ(
GetCaretBoundsForPosition(client, test_string, kTextLength, &caret_range),
gfx::ScreenRectFromNSRect(rect));
EXPECT_EQ_RANGE(query_range, actual_range);
// Query outside composition range. Ensure bounds and range corresponding to
// the current caret position are returned.
query_range = NSMakeRange(kTextLength + 1, 0);
rect = [ns_view_ firstRectForCharacterRange:query_range
actualRange:&actual_range];
EXPECT_EQ(GetCaretBounds(client), gfx::ScreenRectFromNSRect(rect));
EXPECT_EQ_RANGE(caret_range, actual_range);
// Make sure not crashing by passing null pointer instead of actualRange.
rect = [ns_view_ firstRectForCharacterRange:query_range actualRange:nullptr];
}
// Test firstRectForCharacterRange:actualRange for non-empty query ranges within
// the composition range.
TEST_F(BridgedNativeWidgetTest, TextInput_FirstRectForCharacterRange) {
InstallTextField("");
ui::TextInputClient* client = [ns_view_ textInputClientForTesting];
const std::u16string test_string = u"test_str";
const size_t kTextLength = 8;
SetCompositionText(client, test_string, 1, nullptr);
// Query bounds for the whole composition string.
NSRange query_range = NSMakeRange(0, kTextLength);
NSRange actual_range;
NSRect rect = [ns_view_ firstRectForCharacterRange:query_range
actualRange:&actual_range];
EXPECT_EQ(GetExpectedBoundsForRange(client, test_string, query_range),
gfx::ScreenRectFromNSRect(rect));
EXPECT_EQ_RANGE(query_range, actual_range);
// Query bounds for the substring "est_".
query_range = NSMakeRange(1, 4);
rect = [ns_view_ firstRectForCharacterRange:query_range
actualRange:&actual_range];
EXPECT_EQ(GetExpectedBoundsForRange(client, test_string, query_range),
gfx::ScreenRectFromNSRect(rect));
EXPECT_EQ_RANGE(query_range, actual_range);
}
// Test simulated codepaths for IMEs that do not always "mark" text. E.g.
// phonetic languages such as Korean and Vietnamese.
TEST_F(BridgedNativeWidgetTest, TextInput_SimulatePhoneticIme) {
Textfield* textfield = InstallTextField("");
EXPECT_TRUE([ns_view_ textInputClientForTesting]);
object_setClass(ns_view_, [InterpretKeyEventMockedBridgedContentView class]);
// Sequence of calls (and corresponding keyDown events) obtained via tracing
// with 2-Set Korean IME and pressing q, o, then Enter on the keyboard.
NSEvent* q_in_ime = UnicodeKeyDown(12, @"ㅂ");
InterpretKeyEventsCallback handle_q_in_ime = base::BindRepeating([](id view) {
[view insertText:@"ㅂ" replacementRange:NSMakeRange(NSNotFound, 0)];
});
NSEvent* o_in_ime = UnicodeKeyDown(31, @"ㅐ");
InterpretKeyEventsCallback handle_o_in_ime = base::BindRepeating([](id view) {
[view insertText:@"배" replacementRange:NSMakeRange(0, 1)];
});
InterpretKeyEventsCallback handle_return_in_ime =
base::BindRepeating([](id view) {
// When confirming the composition, AppKit repeats itself.
[view insertText:@"배" replacementRange:NSMakeRange(0, 1)];
[view insertText:@"배" replacementRange:NSMakeRange(0, 1)];
[view doCommandBySelector:@selector(insertNewLine:)];
});
// Add a hook for the KeyEvent being received by the TextfieldController. E.g.
// this is where the Omnibox would start to search when Return is pressed.
bool saw_vkey_return = false;
SetHandleKeyEventCallback(base::BindRepeating(
[](bool* saw_return, Textfield* textfield, const ui::KeyEvent& event) {
if (event.key_code() == ui::VKEY_RETURN) {
EXPECT_FALSE(*saw_return);
*saw_return = true;
EXPECT_EQ(base::SysNSStringToUTF16(@"배"), textfield->GetText());
}
return false;
},
&saw_vkey_return));
EXPECT_EQ(u"", textfield->GetText());
g_fake_interpret_key_events = &handle_q_in_ime;
[ns_view_ keyDown:q_in_ime];
EXPECT_EQ(base::SysNSStringToUTF16(@"ㅂ"), textfield->GetText());
EXPECT_FALSE(saw_vkey_return);
g_fake_interpret_key_events = &handle_o_in_ime;
[ns_view_ keyDown:o_in_ime];
EXPECT_EQ(base::SysNSStringToUTF16(@"배"), textfield->GetText());
EXPECT_FALSE(saw_vkey_return);
// Note the "Enter" should not replace the replacement range, even though a
// replacement range was set.
g_fake_interpret_key_events = &handle_return_in_ime;
[ns_view_ keyDown:VkeyKeyDown(ui::VKEY_RETURN)];
EXPECT_EQ(base::SysNSStringToUTF16(@"배"), textfield->GetText());
// VKEY_RETURN should be seen by via the unhandled key event handler (but not
// via -insertText:.
EXPECT_TRUE(saw_vkey_return);
g_fake_interpret_key_events = nullptr;
}
// Test simulated codepaths for typing 'm', 'o', 'o', Enter in the Telex IME.
// This IME does not mark text, but, unlike 2-set Korean, it re-inserts the
// entire word on each keypress, even though only the last character in the word
// can be modified. This prevents the keypress being treated as a "character"
// event (which is unavoidably unfortunate for the Undo buffer), but also led to
// a codepath that suppressed a VKEY_RETURN when it should not, since there is
// no candidate IME window to dismiss for this IME.
TEST_F(BridgedNativeWidgetTest, TextInput_SimulateTelexMoo) {
Textfield* textfield = InstallTextField("");
EXPECT_TRUE([ns_view_ textInputClientForTesting]);
EnterAcceleratorView* enter_view = new EnterAcceleratorView();
textfield->parent()->AddChildView(enter_view);
// Sequence of calls (and corresponding keyDown events) obtained via tracing
// with Telex IME and pressing 'm', 'o', 'o', then Enter on the keyboard.
// Note that without the leading 'm', only one character changes, which could
// allow the keypress to be treated as a character event, which would not
// produce the bug.
NSEvent* m_in_ime = UnicodeKeyDown(46, @"m");
InterpretKeyEventsCallback handle_m_in_ime = base::BindRepeating([](id view) {
[view insertText:@"m" replacementRange:NSMakeRange(NSNotFound, 0)];
});
// Note that (unlike Korean IME), Telex generates a latin "o" for both events:
// it doesn't associate a unicode character on the second NSEvent.
NSEvent* o_in_ime = UnicodeKeyDown(31, @"o");
InterpretKeyEventsCallback handle_first_o_in_ime =
base::BindRepeating([](id view) {
// Note the whole word is replaced, not just the last character.
[view insertText:@"mo" replacementRange:NSMakeRange(0, 1)];
});
InterpretKeyEventsCallback handle_second_o_in_ime =
base::BindRepeating([](id view) {
[view insertText:@"mô" replacementRange:NSMakeRange(0, 2)];
});
InterpretKeyEventsCallback handle_return_in_ime =
base::BindRepeating([](id view) {
// Note the previous -insertText: repeats, even though it is unchanged.
// But the IME also follows with an -insertNewLine:.
[view insertText:@"mô" replacementRange:NSMakeRange(0, 2)];
[view doCommandBySelector:@selector(insertNewLine:)];
});
EXPECT_EQ(u"", textfield->GetText());
EXPECT_EQ(0, enter_view->count());
object_setClass(ns_view_, [InterpretKeyEventMockedBridgedContentView class]);
g_fake_interpret_key_events = &handle_m_in_ime;
[ns_view_ keyDown:m_in_ime];
EXPECT_EQ(base::SysNSStringToUTF16(@"m"), textfield->GetText());
EXPECT_EQ(0, enter_view->count());
g_fake_interpret_key_events = &handle_first_o_in_ime;
[ns_view_ keyDown:o_in_ime];
EXPECT_EQ(base::SysNSStringToUTF16(@"mo"), textfield->GetText());
EXPECT_EQ(0, enter_view->count());
g_fake_interpret_key_events = &handle_second_o_in_ime;
[ns_view_ keyDown:o_in_ime];
EXPECT_EQ(base::SysNSStringToUTF16(@"mô"), textfield->GetText());
EXPECT_EQ(0, enter_view->count());
g_fake_interpret_key_events = &handle_return_in_ime;
[ns_view_ keyDown:VkeyKeyDown(ui::VKEY_RETURN)];
EXPECT_EQ(base::SysNSStringToUTF16(@"mô"),
textfield->GetText()); // No change.
EXPECT_EQ(1, enter_view->count()); // Now we see the accelerator.
}
// Simulate 'a' and candidate selection keys. This should just insert "啊",
// suppressing accelerators.
TEST_F(BridgedNativeWidgetTest, TextInput_NoAcceleratorPinyinSelectWord) {
Textfield* textfield = InstallTextField("");
EXPECT_TRUE([ns_view_ textInputClientForTesting]);
EnterAcceleratorView* enter_view = new EnterAcceleratorView();
textfield->parent()->AddChildView(enter_view);
// Sequence of calls (and corresponding keyDown events) obtained via tracing
// with Pinyin IME and pressing 'a', Tab, PageDown, PageUp, Right, Down, Left,
// and finally Up on the keyboard.
// Note 0 is the actual keyCode for 'a', not a placeholder.
NSEvent* a_in_ime = UnicodeKeyDown(0, @"a");
InterpretKeyEventsCallback handle_a_in_ime = base::BindRepeating([](id view) {
// Pinyin does not change composition text while selecting candidate words.
[view setMarkedText:@"a"
selectedRange:NSMakeRange(1, 0)
replacementRange:NSMakeRange(NSNotFound, 0)];
});
InterpretKeyEventsCallback handle_tab_in_ime =
base::BindRepeating([](id view) {
[view setMarkedText:@"ā"
selectedRange:NSMakeRange(0, 1)
replacementRange:NSMakeRange(NSNotFound, 0)];
});
// Composition text will not change in candidate selection.
InterpretKeyEventsCallback handle_candidate_select_in_ime =
base::BindRepeating([](id view) {});
InterpretKeyEventsCallback handle_space_in_ime =
base::BindRepeating([](id view) {
// Space will confirm the composition.
[view insertText:@"啊" replacementRange:NSMakeRange(NSNotFound, 0)];
});
InterpretKeyEventsCallback handle_enter_in_ime =
base::BindRepeating([](id view) {
// Space after Space will generate -insertNewLine:.
[view doCommandBySelector:@selector(insertNewLine:)];
});
EXPECT_EQ(u"", textfield->GetText());
EXPECT_EQ(0, enter_view->count());
object_setClass(ns_view_, [InterpretKeyEventMockedBridgedContentView class]);
g_fake_interpret_key_events = &handle_a_in_ime;
[ns_view_ keyDown:a_in_ime];
EXPECT_EQ(base::SysNSStringToUTF16(@"a"), textfield->GetText());
EXPECT_EQ(0, enter_view->count());
g_fake_interpret_key_events = &handle_tab_in_ime;
[ns_view_ keyDown:VkeyKeyDown(ui::VKEY_RETURN)];
EXPECT_EQ(base::SysNSStringToUTF16(@"ā"), textfield->GetText());
EXPECT_EQ(0, enter_view->count()); // Not seen as an accelerator.
// Pinyin changes candidate word on this sequence of keys without changing the
// composition text. At the end of this sequence, the word "啊" should be
// selected.
const ui::KeyboardCode key_sequence[] = {ui::VKEY_NEXT, ui::VKEY_PRIOR,
ui::VKEY_RIGHT, ui::VKEY_DOWN,
ui::VKEY_LEFT, ui::VKEY_UP};
g_fake_interpret_key_events = &handle_candidate_select_in_ime;
for (auto key : key_sequence) {
[ns_view_ keyDown:VkeyKeyDown(key)];
EXPECT_EQ(base::SysNSStringToUTF16(@"ā"),
textfield->GetText()); // No change.
EXPECT_EQ(0, enter_view->count());
}
// Space to confirm composition
g_fake_interpret_key_events = &handle_space_in_ime;
[ns_view_ keyDown:VkeyKeyDown(ui::VKEY_SPACE)];
EXPECT_EQ(base::SysNSStringToUTF16(@"啊"), textfield->GetText());
EXPECT_EQ(0, enter_view->count());
// The next Enter should be processed as accelerator.
g_fake_interpret_key_events = &handle_enter_in_ime;
[ns_view_ keyDown:VkeyKeyDown(ui::VKEY_RETURN)];
EXPECT_EQ(base::SysNSStringToUTF16(@"啊"), textfield->GetText());
EXPECT_EQ(1, enter_view->count());
}
// Simulate 'a', Enter in Hiragana. This should just insert "あ", suppressing
// accelerators.
TEST_F(BridgedNativeWidgetTest, TextInput_NoAcceleratorEnterComposition) {
Textfield* textfield = InstallTextField("");
EXPECT_TRUE([ns_view_ textInputClientForTesting]);
EnterAcceleratorView* enter_view = new EnterAcceleratorView();
textfield->parent()->AddChildView(enter_view);
// Sequence of calls (and corresponding keyDown events) obtained via tracing
// with Hiragana IME and pressing 'a', then Enter on the keyboard.
// Note 0 is the actual keyCode for 'a', not a placeholder.
NSEvent* a_in_ime = UnicodeKeyDown(0, @"a");
InterpretKeyEventsCallback handle_a_in_ime = base::BindRepeating([](id view) {
// TODO(crbug.com/41254370): |text| should be an NSAttributedString.
[view setMarkedText:@"あ"
selectedRange:NSMakeRange(1, 0)
replacementRange:NSMakeRange(NSNotFound, 0)];
});
InterpretKeyEventsCallback handle_first_return_in_ime =
base::BindRepeating([](id view) {
[view insertText:@"あ" replacementRange:NSMakeRange(NSNotFound, 0)];
// Note there is no call to -insertNewLine: here.
});
InterpretKeyEventsCallback handle_second_return_in_ime = base::BindRepeating(
[](id view) { [view doCommandBySelector:@selector(insertNewLine:)]; });
EXPECT_EQ(u"", textfield->GetText());
EXPECT_EQ(0, enter_view->count());
object_setClass(ns_view_, [InterpretKeyEventMockedBridgedContentView class]);
g_fake_interpret_key_events = &handle_a_in_ime;
[ns_view_ keyDown:a_in_ime];
EXPECT_EQ(base::SysNSStringToUTF16(@"あ"), textfield->GetText());
EXPECT_EQ(0, enter_view->count());
g_fake_interpret_key_events = &handle_first_return_in_ime;
[ns_view_ keyDown:VkeyKeyDown(ui::VKEY_RETURN)];
EXPECT_EQ(base::SysNSStringToUTF16(@"あ"), textfield->GetText());
EXPECT_EQ(0, enter_view->count()); // Not seen as an accelerator.
g_fake_interpret_key_events = &handle_second_return_in_ime;
[ns_view_
keyDown:VkeyKeyDown(ui::VKEY_RETURN)]; // Sanity check: send Enter again.
EXPECT_EQ(base::SysNSStringToUTF16(@"あ"),
textfield->GetText()); // No change.
EXPECT_EQ(1, enter_view->count()); // Now we see the accelerator.
}
// Simulate 'a', Tab, Enter, Enter in Hiragana. This should just insert "a",
// suppressing accelerators.
TEST_F(BridgedNativeWidgetTest, TextInput_NoAcceleratorTabEnterComposition) {
Textfield* textfield = InstallTextField("");
EXPECT_TRUE([ns_view_ textInputClientForTesting]);
EnterAcceleratorView* enter_view = new EnterAcceleratorView();
textfield->parent()->AddChildView(enter_view);
// Sequence of calls (and corresponding keyDown events) obtained via tracing
// with Hiragana IME and pressing 'a', Tab, then Enter on the keyboard.
NSEvent* a_in_ime = UnicodeKeyDown(0, @"a");
InterpretKeyEventsCallback handle_a_in_ime = base::BindRepeating([](id view) {
// TODO(crbug.com/41254370): |text| should have an underline.
[view setMarkedText:@"あ"
selectedRange:NSMakeRange(1, 0)
replacementRange:NSMakeRange(NSNotFound, 0)];
});
InterpretKeyEventsCallback handle_tab_in_ime =
base::BindRepeating([](id view) {
// TODO(crbug.com/41254370): |text| should be an NSAttributedString (now
// with a different underline color).
[view setMarkedText:@"a"
selectedRange:NSMakeRange(0, 1)
replacementRange:NSMakeRange(NSNotFound, 0)];
// Note there is no -insertTab: generated.
});
InterpretKeyEventsCallback handle_first_return_in_ime =
base::BindRepeating([](id view) {
// Do *nothing*. Enter does not confirm nor change the composition, it
// just dismisses the IME window, leaving the text marked.
});
InterpretKeyEventsCallback handle_second_return_in_ime =
base::BindRepeating([](id view) {
// The second return will confirm the composition.
[view insertText:@"a" replacementRange:NSMakeRange(NSNotFound, 0)];
});
InterpretKeyEventsCallback handle_third_return_in_ime =
base::BindRepeating([](id view) {
// Only the third return will generate -insertNewLine:.
[view doCommandBySelector:@selector(insertNewLine:)];
});
EXPECT_EQ(u"", textfield->GetText());
EXPECT_EQ(0, enter_view->count());
object_setClass(ns_view_, [InterpretKeyEventMockedBridgedContentView class]);
g_fake_interpret_key_events = &handle_a_in_ime;
[ns_view_ keyDown:a_in_ime];
EXPECT_EQ(base::SysNSStringToUTF16(@"あ"), textfield->GetText());
EXPECT_EQ(0, enter_view->count());
g_fake_interpret_key_events = &handle_tab_in_ime;
[ns_view_ keyDown:VkeyKeyDown(ui::VKEY_TAB)];
// Tab will switch to a Romanji (Latin) character.
EXPECT_EQ(base::SysNSStringToUTF16(@"a"), textfield->GetText());
EXPECT_EQ(0, enter_view->count());
g_fake_interpret_key_events = &handle_first_return_in_ime;
[ns_view_ keyDown:VkeyKeyDown(ui::VKEY_RETURN)];
// Enter just dismisses the IME window. The composition is still active.
EXPECT_EQ(base::SysNSStringToUTF16(@"a"), textfield->GetText());
EXPECT_EQ(0, enter_view->count()); // Not seen as an accelerator.
g_fake_interpret_key_events = &handle_second_return_in_ime;
[ns_view_ keyDown:VkeyKeyDown(ui::VKEY_RETURN)];
// Enter now confirms the composition (unmarks text). Note there is still no
// IME window visible but, since there is marked text, IME is still active.
EXPECT_EQ(base::SysNSStringToUTF16(@"a"), textfield->GetText());
EXPECT_EQ(0, enter_view->count()); // Not seen as an accelerator.
g_fake_interpret_key_events = &handle_third_return_in_ime;
[ns_view_ keyDown:VkeyKeyDown(ui::VKEY_RETURN)]; // Send Enter a third time.
EXPECT_EQ(base::SysNSStringToUTF16(@"a"),
textfield->GetText()); // No change.
EXPECT_EQ(1, enter_view->count()); // Now we see the accelerator.
}
// Test a codepath that could hypothetically cause [NSApp updateWindows] to be
// called recursively due to IME dismissal during teardown triggering a focus
// change. Twice.
TEST_F(BridgedNativeWidgetTest, TextInput_RecursiveUpdateWindows) {
Textfield* textfield = InstallTextField("");
EXPECT_TRUE([ns_view_ textInputClientForTesting]);
object_setClass(ns_view_, [InterpretKeyEventMockedBridgedContentView class]);
base::apple::ScopedObjCClassSwizzler update_windows_swizzler(
[NSApplication class], [UpdateWindowsDonorForNSApp class],
@selector(updateWindows));
base::apple::ScopedObjCClassSwizzler current_input_context_swizzler(
[NSTextInputContext class],
[CurrentInputContextDonorForNSTextInputContext class],
@selector(currentInputContext));
int vkey_return_count = 0;
// Everything happens with this one event.
NSEvent* return_with_fake_ime = cocoa_test_event_utils::SynthesizeKeyEvent(
widget_->GetNativeWindow().GetNativeNSWindow(), true, ui::VKEY_RETURN, 0);
InterpretKeyEventsCallback generate_return_and_fake_ime = base::BindRepeating(
[](int* saw_return_count, id view) {
EXPECT_EQ(0, *saw_return_count);
// First generate the return to simulate an input context change.
[view insertText:@"\r" replacementRange:NSMakeRange(NSNotFound, 0)];
EXPECT_EQ(1, *saw_return_count);
},
&vkey_return_count);
bool saw_update_windows = false;
base::RepeatingClosure update_windows_closure = base::BindRepeating(
[](bool* saw_update_windows, BridgedContentView* view,
NativeWidgetMacNSWindowHost* host, Textfield* textfield) {
// Ensure updateWindows is not invoked recursively.
EXPECT_FALSE(*saw_update_windows);
*saw_update_windows = true;
// Inside updateWindows, assume the IME got dismissed and wants to
// insert its last bit of text for the old input context.
[view insertText:@"배" replacementRange:NSMakeRange(0, 1)];
// This is triggered by the setTextInputClient:nullptr in
// SetHandleKeyEventCallback(), so -inputContext should also be nil.
EXPECT_FALSE([view inputContext]);
// Ensure we can't recursively call updateWindows. A TextInputClient
// reacting to InsertChar could theoretically do this, but toolkit-views
// DCHECKs if there is recursive event dispatch, so call
// setTextInputClient directly.
host->text_input_host()->SetTextInputClient(textfield);
// Finally simulate what -[NSApp updateWindows] should _actually_ do,
// which is to update the input context (from the first responder).
g_fake_current_input_context = [view inputContext];
// Now, the |textfield| set above should have been set again.
EXPECT_TRUE(g_fake_current_input_context);
},
&saw_update_windows, ns_view_, GetNSWindowHost(), textfield);
SetHandleKeyEventCallback(base::BindRepeating(
[](int* saw_return_count, BridgedContentView* view,
NativeWidgetMacNSWindowHost* host, Textfield* textfield,
const ui::KeyEvent& event) {
if (event.key_code() == ui::VKEY_RETURN) {
*saw_return_count += 1;
// Simulate Textfield::OnBlur() by clearing the input method.
// Textfield needs to be in a Widget to do this normally.
host->text_input_host()->SetTextInputClient(nullptr);
}
return false;
},
&vkey_return_count, ns_view_, GetNSWindowHost()));
// Starting text (just insert it).
[ns_view_ insertText:@"ㅂ" replacementRange:NSMakeRange(NSNotFound, 0)];
EXPECT_EQ(base::SysNSStringToUTF16(@"ㅂ"), textfield->GetText());
g_fake_interpret_key_events = &generate_return_and_fake_ime;
g_update_windows_closure = &update_windows_closure;
g_fake_current_input_context = [ns_view_ inputContext];
EXPECT_TRUE(g_fake_current_input_context);
[ns_view_ keyDown:return_with_fake_ime];
// We should see one VKEY_RETURNs and one updateWindows. In particular, note
// that there is not a second VKEY_RETURN seen generated by keyDown: thinking
// the event has been unhandled. This is because it was handled when the fake
// IME sent \r.
EXPECT_TRUE(saw_update_windows);
EXPECT_EQ(1, vkey_return_count);
// The text inserted during updateWindows should have been inserted, even
// though we were trying to change the input context.
EXPECT_EQ(base::SysNSStringToUTF16(@"배"), textfield->GetText());
EXPECT_TRUE(g_fake_current_input_context);
g_fake_current_input_context = nullptr;
g_fake_interpret_key_events = nullptr;
g_update_windows_closure = nullptr;
}
// Write selection text to the pasteboard.
TEST_F(BridgedNativeWidgetTest, TextInput_WriteToPasteboard) {
const std::string test_string = "foo bar baz";
InstallTextField(test_string);
NSArray* types = @[ NSPasteboardTypeString ];
// Try to write with no selection. This will succeed, but the string will be
// empty.
{
NSPasteboard* pboard = [NSPasteboard pasteboardWithUniqueName];
BOOL wrote_to_pboard = [ns_view_ writeSelectionToPasteboard:pboard
types:types];
EXPECT_TRUE(wrote_to_pboard);
NSArray* objects = [pboard readObjectsForClasses:@[ [NSString class] ]
options:nullptr];
EXPECT_EQ(1u, [objects count]);
EXPECT_NSEQ(@"", [objects lastObject]);
}
// Write a selection successfully.
{
SetSelectionRange(NSMakeRange(4, 7));
NSPasteboard* pboard = [NSPasteboard pasteboardWithUniqueName];
BOOL wrote_to_pboard = [ns_view_ writeSelectionToPasteboard:pboard
types:types];
EXPECT_TRUE(wrote_to_pboard);
NSArray* objects = [pboard readObjectsForClasses:@[ [NSString class] ]
options:nullptr];
EXPECT_EQ(1u, [objects count]);
EXPECT_NSEQ(@"bar baz", [objects lastObject]);
}
}
TEST_F(BridgedNativeWidgetTest, WriteToFindPasteboard) {
base::apple::ScopedObjCClassSwizzler swizzler([FindPasteboard class],
[MockFindPasteboard class],
@selector(sharedInstance));
EXPECT_NSEQ(@"", [[FindPasteboard sharedInstance] findText]);
const std::string test_string = "foo bar baz";
InstallTextField(test_string);
SetSelectionRange(NSMakeRange(4, 7));
[ns_view_ copyToFindPboard:nil];
EXPECT_NSEQ(@"bar baz", [[FindPasteboard sharedInstance] findText]);
// Don't overwrite with empty selection
SetSelectionRange(NSMakeRange(0, 0));
[ns_view_ copyToFindPboard:nil];
EXPECT_NSEQ(@"bar baz", [[FindPasteboard sharedInstance] findText]);
}
} // namespace views::test
|