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 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559
|
// 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.
#include "ui/views/controls/menu/menu_controller.h"
#include <algorithm>
#include <functional>
#include <type_traits>
#include <utility>
#include <vector>
#include "base/containers/span.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/i18n/rtl.h"
#include "base/memory/raw_ptr.h"
#include "base/strings/stringprintf.h"
#include "base/strings/to_string.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/current_thread.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "ui/accessibility/ax_action_data.h"
#include "ui/accessibility/ax_mode.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/accessibility/platform/ax_platform_for_test.h"
#include "ui/accessibility/platform/ax_platform_node.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/menu_source_type.mojom-shared.h"
#include "ui/base/owned_window_anchor.h"
#include "ui/base/ozone_buildflags.h"
#include "ui/base/ui_base_types.h"
#include "ui/events/event.h"
#include "ui/events/event_constants.h"
#include "ui/events/event_handler.h"
#include "ui/events/event_utils.h"
#include "ui/events/test/event_generator.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/menu/menu_controller_delegate.h"
#include "ui/views/controls/menu/menu_delegate.h"
#include "ui/views/controls/menu/menu_host.h"
#include "ui/views/controls/menu/menu_host_root_view.h"
#include "ui/views/controls/menu/menu_item_view.h"
#include "ui/views/controls/menu/menu_scroll_view_container.h"
#include "ui/views/controls/menu/menu_types.h"
#include "ui/views/controls/menu/submenu_view.h"
#include "ui/views/test/ax_event_counter.h"
#include "ui/views/test/menu_test_utils.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/widget/root_view.h"
#include "ui/views/widget/widget_utils.h"
#if defined(USE_AURA)
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/client/drag_drop_client.h"
#include "ui/aura/client/drag_drop_client_observer.h"
#include "ui/aura/null_window_targeter.h"
#include "ui/aura/scoped_window_targeter.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/test/test_windows.h"
#include "ui/aura/window.h"
#include "ui/base/dragdrop/drag_drop_types.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom-shared.h"
#include "ui/views/controls/menu/menu_pre_target_handler.h"
#endif
#if BUILDFLAG(IS_OZONE)
#include "ui/ozone/public/ozone_platform.h"
#endif
#if BUILDFLAG(IS_OZONE_X11)
#include "ui/events/test/events_test_utils_x11.h"
#endif
namespace views {
namespace {
using ::ui::mojom::DragOperation;
constexpr MenuAnchorPosition kBubblePositions[] = {
MenuAnchorPosition::kBubbleTopLeft,
MenuAnchorPosition::kBubbleTopRight,
MenuAnchorPosition::kBubbleLeft,
MenuAnchorPosition::kBubbleRight,
MenuAnchorPosition::kBubbleBottomLeft,
MenuAnchorPosition::kBubbleBottomRight};
bool ShouldIgnoreScreenBoundsForMenus() {
#if BUILDFLAG(IS_OZONE)
// Some platforms, such as Wayland, disallow client applications to manipulate
// global screen coordinates, requiring menus to be positioned relative to
// their parent windows. See comment in ozone_platform_wayland.cc.
return !ui::OzonePlatform::GetInstance()
->GetPlatformProperties()
.supports_global_screen_coordinates;
#else
return false;
#endif
}
gfx::Size GetPreferredSizeForSubmenu(SubmenuView& submenu) {
auto size = submenu.GetPreferredSize({});
const auto insets = submenu.GetScrollViewContainer()->GetInsets();
size.Enlarge(insets.width(), insets.height());
return size;
}
// Unfortunately a macro rather than a function, to work around ASSERT_ not
// working in non-void functions.
#define GET_CHILD_BUTTON(name, parent, index) \
ASSERT_GT((parent)->children().size(), size_t{(index)}); \
auto* const name = Button::AsButton((parent)->children()[index]); \
ASSERT_NE(nullptr, name)
// Test implementation of MenuControllerDelegate that only reports the values
// called of OnMenuClosed.
class TestMenuControllerDelegate : public internal::MenuControllerDelegate {
public:
TestMenuControllerDelegate() = default;
int on_menu_closed_called() const { return on_menu_closed_called_; }
NotifyType on_menu_closed_notify_type() const {
return on_menu_closed_notify_type_;
}
const MenuItemView* on_menu_closed_menu() const {
return on_menu_closed_menu_;
}
int on_menu_closed_mouse_event_flags() const {
return on_menu_closed_mouse_event_flags_;
}
// On a subsequent call to OnMenuClosed |controller| will be deleted.
void set_on_menu_closed_callback(base::RepeatingClosure callback) {
on_menu_closed_callback_ = std::move(callback);
}
// internal::MenuControllerDelegate:
void OnMenuClosed(NotifyType type,
MenuItemView* menu,
int mouse_event_flags) override;
void SiblingMenuCreated(MenuItemView* menu) override;
private:
// Number of times OnMenuClosed has been called.
int on_menu_closed_called_ = 0;
// The values passed on the last call of OnMenuClosed.
NotifyType on_menu_closed_notify_type_ = NOTIFY_DELEGATE;
raw_ptr<MenuItemView> on_menu_closed_menu_ = nullptr;
int on_menu_closed_mouse_event_flags_ = 0;
// Optional callback triggered during OnMenuClosed
base::RepeatingClosure on_menu_closed_callback_;
};
void TestMenuControllerDelegate::OnMenuClosed(NotifyType type,
MenuItemView* menu,
int mouse_event_flags) {
++on_menu_closed_called_;
on_menu_closed_notify_type_ = type;
on_menu_closed_menu_ = menu;
on_menu_closed_mouse_event_flags_ = mouse_event_flags;
if (on_menu_closed_callback_) {
on_menu_closed_callback_.Run();
}
}
void TestMenuControllerDelegate::SiblingMenuCreated(MenuItemView* menu) {}
class TestEventHandler : public ui::EventHandler {
public:
TestEventHandler() = default;
void OnTouchEvent(ui::TouchEvent* event) override {
if (event->type() == ui::EventType::kTouchPressed) {
++outstanding_touches_;
} else if (event->type() == ui::EventType::kTouchReleased ||
event->type() == ui::EventType::kTouchCancelled) {
--outstanding_touches_;
}
}
int outstanding_touches() const { return outstanding_touches_; }
private:
int outstanding_touches_ = 0;
};
// A test widget that counts gesture events.
class GestureTestWidget : public Widget {
public:
GestureTestWidget() = default;
void OnGestureEvent(ui::GestureEvent* event) override;
int gesture_count() const { return gesture_count_; }
private:
int gesture_count_ = 0;
};
void GestureTestWidget::OnGestureEvent(ui::GestureEvent* event) {
++gesture_count_;
}
#if defined(USE_AURA)
// A DragDropClient which does not trigger a nested run loop. Instead a
// callback is triggered during StartDragAndDrop in order to allow testing.
class TestDragDropClient : public aura::client::DragDropClient {
public:
explicit TestDragDropClient(base::RepeatingClosure callback)
: start_drag_and_drop_callback_(std::move(callback)) {}
// aura::client::DragDropClient:
DragOperation StartDragAndDrop(std::unique_ptr<ui::OSExchangeData> data,
aura::Window* root_window,
aura::Window* source_window,
const gfx::Point& screen_location,
int allowed_operations,
ui::mojom::DragEventSource source) override;
#if BUILDFLAG(IS_LINUX)
void UpdateDragImage(const gfx::ImageSkia& image,
const gfx::Vector2d& offset) override {}
#endif
void DragCancel() override;
bool IsDragDropInProgress() override;
void AddObserver(aura::client::DragDropClientObserver* observer) override {}
void RemoveObserver(aura::client::DragDropClientObserver* observer) override {
}
private:
base::RepeatingClosure start_drag_and_drop_callback_;
bool drag_in_progress_ = false;
};
DragOperation TestDragDropClient::StartDragAndDrop(
std::unique_ptr<ui::OSExchangeData> data,
aura::Window* root_window,
aura::Window* source_window,
const gfx::Point& screen_location,
int allowed_operations,
ui::mojom::DragEventSource source) {
drag_in_progress_ = true;
start_drag_and_drop_callback_.Run();
return DragOperation::kNone;
}
void TestDragDropClient::DragCancel() {
drag_in_progress_ = false;
}
bool TestDragDropClient::IsDragDropInProgress() {
return drag_in_progress_;
}
#endif // defined(USE_AURA)
// View which cancels the menu it belongs to on mouse press.
class CancelMenuOnMousePressView : public View {
METADATA_HEADER(CancelMenuOnMousePressView, View)
public:
explicit CancelMenuOnMousePressView(base::WeakPtr<MenuController> controller)
: controller_(controller) {}
// View:
bool OnMousePressed(const ui::MouseEvent& event) override;
gfx::Size CalculatePreferredSize(
const SizeBounds& /*available_size*/) const override;
private:
const base::WeakPtr<MenuController> controller_;
};
bool CancelMenuOnMousePressView::OnMousePressed(const ui::MouseEvent& event) {
controller_->Cancel(MenuController::ExitType::kAll);
return true;
}
gfx::Size CancelMenuOnMousePressView::CalculatePreferredSize(
const SizeBounds& /*available_size*/) const {
// This is needed to prevent the view from being "squashed" to zero height
// when the menu which owns it is shown. In such state the logic which
// determines if the menu contains the mouse press location doesn't work.
return size();
}
BEGIN_METADATA(CancelMenuOnMousePressView)
END_METADATA
} // namespace
struct MenuBoundsOptions {
gfx::Rect anchor_bounds = gfx::Rect(500, 500, 10, 10);
gfx::Rect monitor_bounds = gfx::Rect(0, 0, 1000, 1000);
gfx::Size menu_size = gfx::Size(100, 150);
MenuAnchorPosition menu_anchor = MenuAnchorPosition::kTopLeft;
MenuItemView::MenuPosition menu_position =
MenuItemView::MenuPosition::kBestFit;
};
class MenuControllerTest : public ViewsTestBase,
public testing::WithParamInterface<bool> {
public:
MenuControllerTest() = default;
// ViewsTestBase:
void SetUp() override;
void TearDown() override;
void ReleaseTouchId(int id);
void PressKey(ui::KeyboardCode key_code);
void DispatchKey(ui::KeyboardCode key_code);
gfx::Rect CalculateMenuBounds(const MenuBoundsOptions& options);
gfx::Rect CalculateBubbleMenuBoundsWithoutInsets(
const MenuBoundsOptions& options,
MenuItemView* menu_item = nullptr);
MenuItemView::MenuPosition menu_item_actual_position() const {
return menu_item_->actual_menu_position();
}
gfx::Rect CalculateExpectedMenuAnchorRect(MenuItemView* menu_item);
MenuController::MenuOpenDirection GetChildMenuOpenDirectionAtDepth(
size_t depth) const;
void SetChildMenuOpenDirectionAtDepth(
size_t depth,
MenuController::MenuOpenDirection direction);
void MenuChildrenChanged(MenuItemView* item);
static MenuAnchorPosition AdjustAnchorPositionForRtl(
MenuAnchorPosition position);
#if defined(USE_AURA)
// Verifies that a non-nested menu fully closes when receiving an escape key.
void TestAsyncEscapeKey();
// Verifies that an open menu receives a cancel event, and closes.
void TestCancelEvent();
#endif // defined(USE_AURA)
// Verifies the state of the |menu_controller_| before destroying it.
void VerifyDragCompleteThenDestroy();
// Sets up |menu_controller_delegate_| to be destroyed when OnMenuClosed is
// called.
void TestDragCompleteThenDestroyOnMenuClosed();
// Tests destroying the active |menu_controller_| and replacing it with a new
// active instance.
void TestMenuControllerReplacementDuringDrag();
// Tests that the menu does not destroy itself when canceled during a drag.
void TestCancelAllDuringDrag();
// Tests that destroying the menu during ViewsDelegate::ReleaseRef does not
// cause a crash.
void TestDestroyedDuringViewsRelease();
void TestMenuFitsOnScreen(MenuAnchorPosition menu_anchor_position,
const gfx::Rect& monitor_bounds);
void TestMenuFitsOnScreenSmallAnchor(MenuAnchorPosition menu_anchor_position,
const gfx::Rect& monitor_bounds);
void TestMenuFitsOnSmallScreen(MenuAnchorPosition menu_anchor_position,
const gfx::Rect& monitor_bounds);
// Given an onscreen menu `item` with screen bounds `parent_bounds`, verifies
// that the submenu opened from `item` fits inside `monitor_bounds`.
void TestSubmenuFitsOnScreen(MenuItemView* item,
const gfx::Rect& monitor_bounds,
const gfx::Rect& parent_bounds,
MenuAnchorPosition menu_anchor);
protected:
void SetPendingStateItem(MenuItemView* item);
void SetState(MenuItemView* item);
void IncrementSelection();
void DecrementSelection();
void DestroyMenuControllerOnMenuClosed(TestMenuControllerDelegate* delegate);
MenuItemView* FindInitialSelectableMenuItemDown(MenuItemView* parent);
MenuItemView* FindInitialSelectableMenuItemUp(MenuItemView* parent);
internal::MenuControllerDelegate* current_controller_delegate() {
return menu_controller_->delegate_;
}
bool showing() const { return menu_controller_->showing_; }
MenuHost* menu_host_for_submenu(SubmenuView* submenu) {
return submenu->host_;
}
MenuHostRootView* CreateMenuHostRootView(MenuHost* host);
void MenuHostOnDragWillStart(MenuHost* host);
void MenuHostOnDragComplete(MenuHost* host);
void SelectByChar(char16_t character);
void SetDropMenuItem(MenuItemView* target,
MenuDelegate::DropPosition position);
void SetComboboxType(MenuController::ComboboxType combobox_type);
template <typename T>
static T ConvertEvent(View* source, const T& event) {
return T(event, source, source->GetWidget()->GetRootView());
}
// These functions expect `event` to be in coordinates of `source`.
void SetSelectionOnPointerDown(SubmenuView* source,
const ui::MouseEvent& event);
bool ProcessMousePressed(SubmenuView* source, const ui::MouseEvent& event);
bool ProcessMouseDragged(SubmenuView* source, const ui::MouseEvent& event);
void ProcessMouseReleased(SubmenuView* source, const ui::MouseEvent& event);
void ProcessMouseMoved(SubmenuView* source, const ui::MouseEvent& event);
void ProcessGestureEvent(SubmenuView* source, const ui::GestureEvent& event);
void ProcessTouchEvent(SubmenuView* source, const ui::TouchEvent& event);
void Accept(MenuItemView* item, int event_flags);
// Causes the |menu_controller_| to begin dragging. Use TestDragDropClient to
// avoid nesting message loops.
void StartDrag();
void SetUpMenuControllerForCalculateBounds(const MenuBoundsOptions& options,
MenuItemView* menu_item);
GestureTestWidget* owner() { return owner_.get(); }
ui::test::EventGenerator* event_generator() { return event_generator_.get(); }
MenuItemView* menu_item() { return menu_item_.get(); }
test::TestMenuDelegate* menu_delegate() { return menu_delegate_.get(); }
TestMenuControllerDelegate* menu_controller_delegate() {
return menu_controller_delegate_.get();
}
MenuController* menu_controller() { return menu_controller_; }
const MenuItemView* pending_state_item() const {
return menu_controller_->pending_state_.item;
}
MenuController::ExitType menu_exit_type() const {
return menu_controller_->exit_type_;
}
// Displays a submenu with appropriate params. If the first arg is null (the
// default), displays `menu_item()->GetSubmenu()`. Supply a second arg if you
// want a callback to modify the init params before calling
// `SubmenuView::ShowAt()`.
template <typename T = void (*)(MenuHost::InitParams&)>
requires(std::is_invocable_v<T, MenuHost::InitParams&>)
void ShowSubmenu(
SubmenuView* submenu = nullptr,
T&& adjust_params = [](auto&) {}) {
if (!submenu) {
submenu = menu_item()->GetSubmenu();
}
MenuHost::InitParams params;
params.parent = owner();
params.bounds = gfx::Rect(GetPreferredSizeForSubmenu(*submenu));
std::invoke(std::forward<T>(adjust_params), params);
submenu->ShowAt(params);
}
// Adds a menu item having buttons as children and returns it. If
// `single_child` is true, the hosting menu item has only one child button.
MenuItemView* AddButtonMenuItems(bool single_child);
void DestroyMenuItem();
Button* hot_button() { return menu_controller_->hot_button_; }
void SetHotTrackedButton(Button* hot_button);
void ExitMenuRun();
void DestroyMenuController();
void DestroyMenuControllerDelegate();
int owner_gesture_count() const { return owner_->gesture_count(); }
static bool SelectionWraps();
void OpenMenu(MenuItemView* parent,
const MenuBoundsOptions& options = MenuBoundsOptions());
gfx::Insets GetBorderAndShadowInsets(bool is_submenu);
private:
virtual bool for_drop() const { return false; }
std::unique_ptr<GestureTestWidget> owner_;
std::unique_ptr<ui::test::EventGenerator> event_generator_;
std::unique_ptr<MenuItemView> menu_item_;
std::unique_ptr<TestMenuControllerDelegate> menu_controller_delegate_;
std::unique_ptr<test::TestMenuDelegate> menu_delegate_;
raw_ptr<MenuController> menu_controller_ = nullptr;
};
void MenuControllerTest::SetUp() {
if (testing::UnitTest::GetInstance()->current_test_info()->value_param()) {
base::i18n::SetRTLForTesting(GetParam());
}
set_views_delegate(std::make_unique<test::ReleaseRefTestViewsDelegate>());
ViewsTestBase::SetUp();
ASSERT_TRUE(base::CurrentUIThread::IsSet());
owner_ = std::make_unique<GestureTestWidget>();
Widget::InitParams params = CreateParams(
Widget::InitParams::CLIENT_OWNS_WIDGET, Widget::InitParams::TYPE_POPUP);
owner_->Init(std::move(params));
event_generator_ =
std::make_unique<ui::test::EventGenerator>(GetRootWindow(owner()));
owner_->Show();
menu_delegate_ = std::make_unique<test::TestMenuDelegate>();
menu_item_ = std::make_unique<MenuItemView>(menu_delegate_.get());
menu_item_->AppendMenuItem(1, u"One");
menu_item_->AppendMenuItem(2, u"Two");
menu_item_->AppendMenuItem(3, u"Three");
menu_item_->AppendMenuItem(4, u"Four");
menu_controller_delegate_ = std::make_unique<TestMenuControllerDelegate>();
menu_controller_ =
new MenuController(for_drop(), menu_controller_delegate_.get());
menu_controller_->owner_ = owner();
menu_controller_->showing_ = true;
menu_controller_->SetSelection(menu_item(),
MenuController::SELECTION_UPDATE_IMMEDIATELY);
menu_item_->set_controller(menu_controller_);
}
void MenuControllerTest::TearDown() {
owner_->CloseNow();
DestroyMenuController();
menu_controller_delegate_.reset();
// `menu_item_` must be torn down before `ViewsTestBase::TearDown()`, since
// it may transitively own a `Compositor` that is registered with a context
// factory that `TearDown()` will destroy.
menu_item_.reset();
ViewsTestBase::TearDown();
base::i18n::SetRTLForTesting(false);
}
void MenuControllerTest::ReleaseTouchId(int id) {
event_generator_->ReleaseTouchId(id);
}
void MenuControllerTest::PressKey(ui::KeyboardCode key_code) {
event_generator_->PressKey(key_code, 0);
}
void MenuControllerTest::DispatchKey(ui::KeyboardCode key_code) {
ui::KeyEvent event(ui::EventType::kKeyPressed, key_code, 0);
menu_controller_->OnWillDispatchKeyEvent(&event);
}
gfx::Rect MenuControllerTest::CalculateMenuBounds(
const MenuBoundsOptions& options) {
SetUpMenuControllerForCalculateBounds(options, menu_item_.get());
MenuController::MenuOpenDirection resulting_direction;
ui::OwnedWindowAnchor anchor;
return menu_controller_->CalculateMenuBounds(
menu_item_.get(), MenuController::MenuOpenDirection::kLeading,
&resulting_direction, &anchor);
}
gfx::Rect MenuControllerTest::CalculateBubbleMenuBoundsWithoutInsets(
const MenuBoundsOptions& options,
MenuItemView* menu_item) {
if (!menu_item) {
menu_item = menu_item_.get();
}
SetUpMenuControllerForCalculateBounds(options, menu_item);
MenuController::MenuOpenDirection resulting_direction;
ui::OwnedWindowAnchor anchor;
gfx::Rect bounds = menu_controller_->CalculateBubbleMenuBounds(
menu_item, MenuController::MenuOpenDirection::kLeading,
&resulting_direction, &anchor);
bounds.Inset(menu_item->GetSubmenu()
->GetScrollViewContainer()
->outside_border_insets());
return bounds;
}
gfx::Rect MenuControllerTest::CalculateExpectedMenuAnchorRect(
MenuItemView* menu_item) {
if (!menu_item->GetParentMenuItem()) {
return menu_controller_->state_.initial_bounds;
}
gfx::Rect bounds = menu_item->GetBoundsInScreen();
bounds.set_height(1);
return bounds;
}
MenuController::MenuOpenDirection
MenuControllerTest::GetChildMenuOpenDirectionAtDepth(size_t depth) const {
return menu_controller_->GetChildMenuOpenDirectionAtDepth(depth);
}
void MenuControllerTest::SetChildMenuOpenDirectionAtDepth(
size_t depth,
MenuController::MenuOpenDirection direction) {
menu_controller_->SetChildMenuOpenDirectionAtDepth(depth, direction);
}
void MenuControllerTest::MenuChildrenChanged(MenuItemView* item) {
menu_controller_->MenuChildrenChanged(item);
}
// static
MenuAnchorPosition MenuControllerTest::AdjustAnchorPositionForRtl(
MenuAnchorPosition position) {
return MenuController::AdjustAnchorPositionForRtl(position);
}
#if defined(USE_AURA)
void MenuControllerTest::TestAsyncEscapeKey() {
ui::KeyEvent event(ui::EventType::kKeyPressed, ui::VKEY_ESCAPE, 0);
menu_controller_->OnWillDispatchKeyEvent(&event);
}
void MenuControllerTest::TestCancelEvent() {
EXPECT_EQ(MenuController::ExitType::kNone, menu_controller_->exit_type());
ui::CancelModeEvent cancel_event;
event_generator_->Dispatch(&cancel_event);
EXPECT_EQ(MenuController::ExitType::kAll, menu_controller_->exit_type());
}
#endif // defined(USE_AURA)
void MenuControllerTest::VerifyDragCompleteThenDestroy() {
EXPECT_FALSE(menu_controller()->drag_in_progress());
EXPECT_EQ(MenuController::ExitType::kAll, menu_controller()->exit_type());
DestroyMenuController();
}
void MenuControllerTest::TestDragCompleteThenDestroyOnMenuClosed() {
menu_controller_delegate_->set_on_menu_closed_callback(
base::BindRepeating(&MenuControllerTest::VerifyDragCompleteThenDestroy,
base::Unretained(this)));
}
void MenuControllerTest::TestMenuControllerReplacementDuringDrag() {
DestroyMenuController();
menu_item()->GetSubmenu()->Close();
menu_controller_ =
new MenuController(/*for_drop=*/false, menu_controller_delegate_.get());
menu_controller_->owner_ = owner_.get();
menu_controller_->showing_ = true;
}
void MenuControllerTest::TestCancelAllDuringDrag() {
menu_controller_->Cancel(MenuController::ExitType::kAll);
EXPECT_EQ(0, menu_controller_delegate_->on_menu_closed_called());
}
void MenuControllerTest::TestDestroyedDuringViewsRelease() {
// |test_views_delegate_| is owned by views::ViewsTestBase and not deleted
// until TearDown. MenuControllerTest outlives it.
static_cast<test::ReleaseRefTestViewsDelegate*>(test_views_delegate())
->set_release_ref_callback(base::BindRepeating(
&MenuControllerTest::DestroyMenuController, base::Unretained(this)));
menu_controller_->ExitMenu();
}
void MenuControllerTest::TestMenuFitsOnScreen(
MenuAnchorPosition menu_anchor_position,
const gfx::Rect& monitor_bounds) {
constexpr int kButtonSize = 50;
const auto test_within_bounds = [&](gfx::Point origin) {
const MenuBoundsOptions options = {
.anchor_bounds = gfx::Rect(origin, gfx::Size(kButtonSize, kButtonSize)),
.monitor_bounds = monitor_bounds,
.menu_anchor = menu_anchor_position};
EXPECT_TRUE(options.monitor_bounds.Contains(
CalculateBubbleMenuBoundsWithoutInsets(options)))
<< "Anchor position: " << base::ToString(menu_anchor_position)
<< ", monitor bounds: " << monitor_bounds.ToString()
<< ", origin: " << origin.ToString();
};
// Simulate a bottom shelf with a tall menu.
const gfx::Point monitor_center = monitor_bounds.CenterPoint();
test_within_bounds(
gfx::Point(monitor_center.x(), monitor_bounds.bottom() - kButtonSize));
// Simulate a left shelf with a tall menu.
test_within_bounds(gfx::Point(monitor_bounds.x(), monitor_center.y()));
// Simulate right shelf with a tall menu.
test_within_bounds(
gfx::Point(monitor_bounds.right() - kButtonSize, monitor_center.y()));
}
void MenuControllerTest::TestMenuFitsOnScreenSmallAnchor(
MenuAnchorPosition menu_anchor_position,
const gfx::Rect& monitor_bounds) {
const auto test_within_bounds = [&](gfx::Point origin) {
const MenuBoundsOptions options = {.anchor_bounds = gfx::Rect(origin, {}),
.monitor_bounds = monitor_bounds,
.menu_anchor = menu_anchor_position};
EXPECT_TRUE(options.monitor_bounds.Contains(
CalculateBubbleMenuBoundsWithoutInsets(options)))
<< "Anchor position: " << base::ToString(menu_anchor_position)
<< ", monitor bounds: " << monitor_bounds.ToString()
<< ", origin: " << origin.ToString();
};
test_within_bounds(monitor_bounds.origin());
test_within_bounds(monitor_bounds.bottom_left());
test_within_bounds(monitor_bounds.top_right());
test_within_bounds(monitor_bounds.bottom_right());
}
void MenuControllerTest::TestMenuFitsOnSmallScreen(
MenuAnchorPosition menu_anchor_position,
const gfx::Rect& monitor_bounds) {
const auto test_within_bounds = [&](gfx::Point origin) {
const MenuBoundsOptions options = {
.anchor_bounds = gfx::Rect(origin, {}),
.monitor_bounds = monitor_bounds,
.menu_size = monitor_bounds.size() + gfx::Size(100, 100),
.menu_anchor = menu_anchor_position};
EXPECT_TRUE(options.monitor_bounds.Contains(
CalculateBubbleMenuBoundsWithoutInsets(options)))
<< "Anchor position: " << base::ToString(menu_anchor_position)
<< ", monitor bounds: " << monitor_bounds.ToString()
<< ", origin: " << origin.ToString();
};
test_within_bounds(monitor_bounds.origin());
test_within_bounds(monitor_bounds.bottom_left());
test_within_bounds(monitor_bounds.top_right());
test_within_bounds(monitor_bounds.bottom_right());
test_within_bounds(monitor_bounds.CenterPoint());
}
void MenuControllerTest::TestSubmenuFitsOnScreen(
MenuItemView* item,
const gfx::Rect& monitor_bounds,
const gfx::Rect& parent_bounds,
MenuAnchorPosition menu_anchor) {
const MenuBoundsOptions options = {
.monitor_bounds = monitor_bounds,
.menu_size = GetPreferredSizeForSubmenu(*item->GetSubmenu()),
.menu_anchor = menu_anchor};
// Put the parent menu onscreen so that `item` is in a widget hierarchy and
// `GetBoundsInScreen()` will work.
SubmenuView* const submenu = item->GetParentMenuItem()->GetSubmenu();
ShowSubmenu(submenu, [&](auto& params) {
// Set the bounds such that `item` (which is assumed to be the sole
// content of `submenu`) has screen bounds `parent_bounds`.
params.bounds = parent_bounds;
params.bounds.Inset(-submenu->GetScrollViewContainer()->GetInsets());
});
const gfx::Rect final_bounds =
CalculateBubbleMenuBoundsWithoutInsets(options, item);
EXPECT_TRUE(monitor_bounds.Contains(final_bounds))
<< monitor_bounds.ToString() << " does not contain "
<< final_bounds.ToString();
submenu->Close();
}
void MenuControllerTest::SetPendingStateItem(MenuItemView* item) {
menu_controller_->pending_state_.item = item;
menu_controller_->pending_state_.submenu_open = true;
}
void MenuControllerTest::SetState(MenuItemView* item) {
SetPendingStateItem(item);
menu_controller_->state_.item = item;
menu_controller_->state_.submenu_open = true;
}
void MenuControllerTest::IncrementSelection() {
menu_controller_->IncrementSelection(
MenuController::INCREMENT_SELECTION_DOWN);
}
void MenuControllerTest::DecrementSelection() {
menu_controller_->IncrementSelection(MenuController::INCREMENT_SELECTION_UP);
}
void MenuControllerTest::DestroyMenuControllerOnMenuClosed(
TestMenuControllerDelegate* delegate) {
// Unretained() is safe here as the test should outlive the delegate. If not
// we want to know.
delegate->set_on_menu_closed_callback(base::BindRepeating(
&MenuControllerTest::DestroyMenuController, base::Unretained(this)));
}
void MenuControllerTest::DestroyMenuControllerDelegate() {
menu_controller_delegate_.reset();
}
MenuItemView* MenuControllerTest::FindInitialSelectableMenuItemDown(
MenuItemView* parent) {
return menu_controller_->FindInitialSelectableMenuItem(
parent, MenuController::INCREMENT_SELECTION_DOWN);
}
MenuItemView* MenuControllerTest::FindInitialSelectableMenuItemUp(
MenuItemView* parent) {
return menu_controller_->FindInitialSelectableMenuItem(
parent, MenuController::INCREMENT_SELECTION_UP);
}
MenuHostRootView* MenuControllerTest::CreateMenuHostRootView(MenuHost* host) {
return static_cast<MenuHostRootView*>(host->CreateRootView());
}
void MenuControllerTest::MenuHostOnDragWillStart(MenuHost* host) {
host->OnDragWillStart();
}
void MenuControllerTest::MenuHostOnDragComplete(MenuHost* host) {
host->OnDragComplete();
}
void MenuControllerTest::SelectByChar(char16_t character) {
menu_controller_->SelectByChar(character);
}
void MenuControllerTest::SetDropMenuItem(MenuItemView* target,
MenuDelegate::DropPosition position) {
menu_controller_->SetDropMenuItem(target, position);
}
void MenuControllerTest::SetComboboxType(
MenuController::ComboboxType combobox_type) {
menu_controller_->set_combobox_type(combobox_type);
}
void MenuControllerTest::SetSelectionOnPointerDown(
SubmenuView* source,
const ui::MouseEvent& event) {
const ui::MouseEvent converted_event = ConvertEvent(source, event);
menu_controller_->SetSelectionOnPointerDown(source, &converted_event);
}
bool MenuControllerTest::ProcessMousePressed(SubmenuView* source,
const ui::MouseEvent& event) {
return menu_controller_->OnMousePressed(source, ConvertEvent(source, event));
}
bool MenuControllerTest::ProcessMouseDragged(SubmenuView* source,
const ui::MouseEvent& event) {
return menu_controller_->OnMouseDragged(source, ConvertEvent(source, event));
}
void MenuControllerTest::ProcessMouseReleased(SubmenuView* source,
const ui::MouseEvent& event) {
menu_controller_->OnMouseReleased(source, ConvertEvent(source, event));
}
void MenuControllerTest::ProcessMouseMoved(SubmenuView* source,
const ui::MouseEvent& event) {
menu_controller_->OnMouseMoved(source, ConvertEvent(source, event));
}
void MenuControllerTest::ProcessGestureEvent(SubmenuView* source,
const ui::GestureEvent& event) {
ui::GestureEvent converted_event = ConvertEvent(source, event);
menu_controller_->OnGestureEvent(source, &converted_event);
}
void MenuControllerTest::ProcessTouchEvent(SubmenuView* source,
const ui::TouchEvent& event) {
ui::TouchEvent converted_event = ConvertEvent(source, event);
menu_controller_->OnTouchEvent(source, &converted_event);
}
void MenuControllerTest::Accept(MenuItemView* item, int event_flags) {
menu_controller_->Accept(item, event_flags);
views::test::WaitForMenuClosureAnimation();
}
void MenuControllerTest::StartDrag() {
MenuItemView* const dragged_item =
menu_item()->GetSubmenu()->GetMenuItemAt(0);
menu_controller_->state_.item = dragged_item;
menu_controller_->StartDrag(menu_item()->GetSubmenu(),
dragged_item->bounds().CenterPoint());
}
void MenuControllerTest::SetUpMenuControllerForCalculateBounds(
const MenuBoundsOptions& options,
MenuItemView* menu_item) {
// Must set both `state_` and `pending_state_` in case, while processing, the
// controller commits the pending state.
menu_controller_->pending_state_.anchor = menu_controller_->state_.anchor =
options.menu_anchor;
menu_controller_->pending_state_.initial_bounds =
menu_controller_->state_.initial_bounds = options.anchor_bounds;
menu_controller_->pending_state_.monitor_bounds =
menu_controller_->state_.monitor_bounds = options.monitor_bounds;
menu_item->set_actual_menu_position(options.menu_position);
menu_item->GetSubmenu()->GetScrollViewContainer()->SetPreferredSize(
options.menu_size);
}
MenuItemView* MenuControllerTest::AddButtonMenuItems(bool single_child) {
MenuItemView* const item_view = menu_item()->AppendMenuItem(5, u"Five");
const size_t children_count = single_child ? 1 : 3;
for (size_t i = 0; i < children_count; ++i) {
item_view
->AddChildView(
std::make_unique<LabelButton>(Button::PressedCallback(), u"Label"))
// This is an in-menu button. Hence it must be always focusable.
->SetFocusBehavior(View::FocusBehavior::ALWAYS);
}
ShowSubmenu();
return item_view;
}
void MenuControllerTest::DestroyMenuItem() {
menu_item_.reset();
}
void MenuControllerTest::SetHotTrackedButton(Button* hot_button) {
menu_controller_->SetHotTrackedButton(hot_button);
}
void MenuControllerTest::ExitMenuRun() {
menu_controller_->SetExitType(MenuController::ExitType::kOutermost);
menu_controller_->ExitTopMostMenu();
}
void MenuControllerTest::DestroyMenuController() {
if (!menu_controller_) {
return;
}
if (!owner_->IsClosed()) {
owner_->RemoveObserver(menu_controller_);
}
menu_controller_->showing_ = false;
menu_controller_->owner_ = nullptr;
delete menu_controller_.ExtractAsDangling();
}
// static
bool MenuControllerTest::SelectionWraps() {
return MenuConfig::instance().arrow_key_selection_wraps;
}
void MenuControllerTest::OpenMenu(MenuItemView* parent,
const MenuBoundsOptions& options) {
SetUpMenuControllerForCalculateBounds(options, parent);
menu_controller_->OpenMenuImpl(parent, true);
}
gfx::Insets MenuControllerTest::GetBorderAndShadowInsets(bool is_submenu) {
const MenuConfig& menu_config = MenuConfig::instance();
int elevation = menu_config.bubble_menu_shadow_elevation;
BubbleBorder::Shadow shadow_type = BubbleBorder::STANDARD_SHADOW;
#if BUILDFLAG(IS_CHROMEOS)
// Increase the submenu shadow elevation and change the shadow style to
// ChromeOS system UI shadow style when using Ash System UI layout.
if (menu_controller_->use_ash_system_ui_layout()) {
if (is_submenu) {
elevation = menu_config.bubble_submenu_shadow_elevation;
}
shadow_type = BubbleBorder::CHROMEOS_SYSTEM_UI_SHADOW;
}
#endif
return BubbleBorder::GetBorderAndShadowInsets(elevation, shadow_type);
}
// Creates the menu controller with `for_drop` set to true. I.e., the menu is
// being shown because something was dragged over it.
class MenuControllerForDropTest : public MenuControllerTest {
public:
MenuControllerForDropTest() = default;
~MenuControllerForDropTest() override = default;
private:
bool for_drop() const override { return true; }
};
INSTANTIATE_TEST_SUITE_P(All,
MenuControllerTest,
testing::Bool(),
[](const auto& info) {
return info.param ? "RTL" : "LTR";
});
#if defined(USE_AURA)
// Tests that an event targeter which blocks events will be honored by the menu
// event dispatcher.
TEST_F(MenuControllerTest, EventTargeter) {
{
// With the aura::NullWindowTargeter instantiated and assigned we expect
// the menu to not handle the key event.
aura::ScopedWindowTargeter scoped_targeter(
GetRootWindow(owner()), std::make_unique<aura::NullWindowTargeter>());
PressKey(ui::VKEY_ESCAPE);
EXPECT_EQ(MenuController::ExitType::kNone, menu_exit_type());
}
// Now that the targeter has been destroyed, expect to exit the menu
// normally when hitting escape.
TestAsyncEscapeKey();
EXPECT_EQ(MenuController::ExitType::kAll, menu_exit_type());
}
#endif // defined(USE_AURA)
#if BUILDFLAG(IS_OZONE_X11)
// Tests that touch event ids are released correctly. See crbug.com/439051 for
// details. When the ids aren't managed correctly, we get stuck down touches.
TEST_F(MenuControllerTest, TouchIdsReleasedCorrectly) {
// Run this test only for X11.
if (ui::OzonePlatform::GetPlatformNameForTest() != "x11") {
GTEST_SKIP();
}
TestEventHandler test_event_handler;
GetRootWindow(owner())->AddPreTargetHandler(&test_event_handler);
ui::SetUpTouchDevicesForTest({1});
event_generator()->PressTouchId(0);
event_generator()->PressTouchId(1);
event_generator()->ReleaseTouchId(0);
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
MenuControllerTest::ReleaseTouchId(1);
TestAsyncEscapeKey();
EXPECT_EQ(MenuController::ExitType::kAll, menu_exit_type());
EXPECT_EQ(0, test_event_handler.outstanding_touches());
GetRootWindow(owner())->RemovePreTargetHandler(&test_event_handler);
}
#endif // BUILDFLAG(IS_OZONE_X11)
// Tests that initial selected menu items are correct when items are enabled or
// disabled.
TEST_F(MenuControllerTest, InitialSelectedItem) {
// Leave items "Two", "Three", and "Four" enabled.
SubmenuView* const submenu = menu_item()->GetSubmenu();
submenu->GetMenuItemAt(0)->SetEnabled(false);
const auto check_has_command = [](const MenuItemView* item, int command) {
ASSERT_NE(nullptr, item);
EXPECT_EQ(command, item->GetCommand());
};
// The first selectable item should be item "Two".
check_has_command(FindInitialSelectableMenuItemDown(menu_item()), 2);
// The last selectable item should be item "Four".
check_has_command(FindInitialSelectableMenuItemUp(menu_item()), 4);
// Leave items "One" and "Two" enabled.
submenu->GetMenuItemAt(0)->SetEnabled(true);
submenu->GetMenuItemAt(2)->SetEnabled(false);
submenu->GetMenuItemAt(3)->SetEnabled(false);
// The first selectable item should be item "One".
check_has_command(FindInitialSelectableMenuItemDown(menu_item()), 1);
// The last selectable item should be item "Two".
check_has_command(FindInitialSelectableMenuItemUp(menu_item()), 2);
// Leave only a single item "One" enabled.
submenu->GetMenuItemAt(1)->SetEnabled(false);
// The first selectable item should be item "One".
check_has_command(FindInitialSelectableMenuItemDown(menu_item()), 1);
// The last selectable item should be item "One".
check_has_command(FindInitialSelectableMenuItemUp(menu_item()), 1);
// Leave only a single item "Three" enabled.
submenu->GetMenuItemAt(0)->SetEnabled(false);
submenu->GetMenuItemAt(2)->SetEnabled(true);
// The first selectable item should be item "Three".
check_has_command(FindInitialSelectableMenuItemDown(menu_item()), 3);
// The last selectable item should be item "Three".
check_has_command(FindInitialSelectableMenuItemUp(menu_item()), 3);
// Leave only a single item ("Two") selected.
submenu->GetMenuItemAt(1)->SetEnabled(true);
submenu->GetMenuItemAt(2)->SetEnabled(false);
// The first selectable item should be item "Two".
check_has_command(FindInitialSelectableMenuItemDown(menu_item()), 2);
// The last selectable item should be item "Two".
check_has_command(FindInitialSelectableMenuItemUp(menu_item()), 2);
}
// Verifies that the scroll arrow is shown when the menu content
// does not fit within the available bounds.
// (https://crbug.com/338585369)
TEST_F(MenuControllerTest, VerifyScrollArrowShown) {
SubmenuView* const submenu = menu_item()->GetSubmenu();
auto* const scroll_container = submenu->GetScrollViewContainer();
MenuHost::InitParams params;
params.parent = owner();
params.bounds = gfx::Rect(GetPreferredSizeForSubmenu(*submenu));
// Show the menu at its preferred size without restriction
submenu->ShowAt(params);
EXPECT_FALSE(scroll_container->scroll_down_button()->GetVisible());
// decrease the available space by 1 so the contents no longer fit
params.bounds.set_height(params.bounds.height() - 1);
submenu->ShowAt(params);
EXPECT_TRUE(scroll_container->scroll_down_button()->GetVisible());
}
// Verifies that the context menu bubble should prioritize its cached menu
// position (above or below the anchor) after its size updates
// (https://crbug.com/1126244).
TEST_F(MenuControllerTest, VerifyMenuBubblePositionAfterSizeChanges) {
constexpr gfx::Rect kMonitorBounds(0, 0, 500, 500);
constexpr gfx::Size kMenuSize(100, 200);
const gfx::Insets border_and_shadow_insets =
GetBorderAndShadowInsets(/*is_submenu=*/false);
// Calculate the suitable anchor point to ensure that if the menu shows below
// the anchor point, the bottom of the menu should be one pixel off the
// bottom of the display. It means that there is insufficient space for the
// menu below the anchor.
const gfx::Point anchor_point(kMonitorBounds.width() / 2,
kMonitorBounds.bottom() + 1 -
kMenuSize.height() +
border_and_shadow_insets.height());
MenuBoundsOptions options = {
.anchor_bounds = gfx::Rect(anchor_point, gfx::Size()),
.monitor_bounds = kMonitorBounds,
.menu_anchor = MenuAnchorPosition::kBubbleRight};
// Case 1: There is insufficient space for the menu below `anchor_point` and
// there is no cached menu position. The menu should show above the anchor.
options.menu_size = kMenuSize;
EXPECT_GT(options.anchor_bounds.y() - border_and_shadow_insets.height() +
kMenuSize.height(),
kMonitorBounds.bottom());
CalculateBubbleMenuBoundsWithoutInsets(options);
EXPECT_EQ(MenuItemView::MenuPosition::kAboveBounds,
menu_item_actual_position());
// Case 2: There is insufficient space for the menu below `anchor_point`. The
// cached position is below the anchor. The menu should show above the anchor.
options.menu_position = MenuItemView::MenuPosition::kBelowBounds;
CalculateBubbleMenuBoundsWithoutInsets(options);
EXPECT_EQ(MenuItemView::MenuPosition::kAboveBounds,
menu_item_actual_position());
// Case 3: There is enough space for the menu below `anchor_point`. The cached
// menu position is above the anchor. The menu should show above the anchor.
constexpr gfx::Size kUpdatedSize(kMenuSize.width(), kMenuSize.height() / 2);
EXPECT_LE(options.anchor_bounds.y() - border_and_shadow_insets.height() +
kUpdatedSize.height(),
kMonitorBounds.bottom());
options.menu_size = kUpdatedSize;
options.menu_position = MenuItemView::MenuPosition::kAboveBounds;
CalculateBubbleMenuBoundsWithoutInsets(options);
EXPECT_EQ(MenuItemView::MenuPosition::kAboveBounds,
menu_item_actual_position());
}
// Verifies that the context menu bubble position,
// MenuAnchorPosition::kBubbleBottomRight, does not shift as items are removed.
// The menu position will shift if items are added and the menu no longer fits
// in its previous position.
TEST_F(MenuControllerTest, VerifyContextMenuBubblePositionAfterSizeChanges) {
constexpr gfx::Rect kMonitorBounds(0, 0, 500, 500);
constexpr gfx::Size kMenuSize(100, 200);
const gfx::Insets border_and_shadow_insets =
GetBorderAndShadowInsets(/*is_submenu=*/false);
// Calculate the suitable anchor point to ensure that if the menu shows below
// the anchor point, the bottom of the menu should be one pixel off the
// bottom of the display. It means that there is insufficient space for the
// menu below the anchor.
const gfx::Point anchor_point(kMonitorBounds.width() / 2,
kMonitorBounds.bottom() + 1 -
kMenuSize.height() +
border_and_shadow_insets.height());
MenuBoundsOptions options = {
.anchor_bounds = gfx::Rect(anchor_point, gfx::Size()),
.monitor_bounds = kMonitorBounds,
.menu_anchor = MenuAnchorPosition::kBubbleBottomRight};
// Case 1: There is insufficient space for the menu below `anchor_point` and
// there is no cached menu position. The menu should show above the anchor.
options.menu_size = kMenuSize;
EXPECT_GT(options.anchor_bounds.y() - border_and_shadow_insets.height() +
kMenuSize.height(),
kMonitorBounds.bottom());
CalculateBubbleMenuBoundsWithoutInsets(options);
EXPECT_EQ(MenuItemView::MenuPosition::kAboveBounds,
menu_item_actual_position());
// Case 2: There is insufficient space for the menu below `anchor_point`. The
// cached position is below the anchor. The menu should show above the anchor
// point.
options.menu_position = MenuItemView::MenuPosition::kBelowBounds;
CalculateBubbleMenuBoundsWithoutInsets(options);
EXPECT_EQ(MenuItemView::MenuPosition::kAboveBounds,
menu_item_actual_position());
// Case 3: There is enough space for the menu below `anchor_point`. The cached
// menu position is above the anchor. The menu should show above the anchor.
constexpr gfx::Size kUpdatedSize(kMenuSize.width(), kMenuSize.height() / 2);
EXPECT_LE(options.anchor_bounds.y() - border_and_shadow_insets.height() +
kUpdatedSize.height(),
kMonitorBounds.bottom());
options.menu_size = kUpdatedSize;
options.menu_position = MenuItemView::MenuPosition::kAboveBounds;
CalculateBubbleMenuBoundsWithoutInsets(options);
EXPECT_EQ(MenuItemView::MenuPosition::kAboveBounds,
menu_item_actual_position());
// Case 4: There is enough space for the menu below `anchor_point`. The cached
// menu position is below the anchor. The menu should show below the anchor.
options.menu_position = MenuItemView::MenuPosition::kBelowBounds;
CalculateBubbleMenuBoundsWithoutInsets(options);
EXPECT_EQ(MenuItemView::MenuPosition::kBelowBounds,
menu_item_actual_position());
}
// Tests that opening the menu and pressing 'Home' selects the first menu item.
TEST_F(MenuControllerTest, FirstSelectedItem) {
ShowSubmenu();
SubmenuView* const submenu = menu_item()->GetSubmenu();
SetPendingStateItem(submenu->GetMenuItemAt(0));
EXPECT_EQ(1, pending_state_item()->GetCommand());
// Select the first menu item.
DispatchKey(ui::VKEY_HOME);
EXPECT_EQ(1, pending_state_item()->GetCommand());
// Fake initial root item selection and submenu showing.
SetPendingStateItem(menu_item());
EXPECT_EQ(0, pending_state_item()->GetCommand());
// Select the first menu item.
DispatchKey(ui::VKEY_HOME);
EXPECT_EQ(1, pending_state_item()->GetCommand());
// Select the last item.
SetPendingStateItem(submenu->GetMenuItemAt(3));
EXPECT_EQ(4, pending_state_item()->GetCommand());
// Select the first menu item.
DispatchKey(ui::VKEY_HOME);
EXPECT_EQ(1, pending_state_item()->GetCommand());
}
// Tests that opening the menu and pressing 'End' selects the last enabled menu
// item.
TEST_F(MenuControllerTest, LastSelectedItem) {
// Fake initial root item selection and submenu showing.
ShowSubmenu();
SetPendingStateItem(menu_item());
EXPECT_EQ(0, pending_state_item()->GetCommand());
// Select the last menu item.
DispatchKey(ui::VKEY_END);
EXPECT_EQ(4, pending_state_item()->GetCommand());
// Select the last item.
SubmenuView* const submenu = menu_item()->GetSubmenu();
SetPendingStateItem(submenu->GetMenuItemAt(3));
EXPECT_EQ(4, pending_state_item()->GetCommand());
// Select the last menu item.
DispatchKey(ui::VKEY_END);
EXPECT_EQ(4, pending_state_item()->GetCommand());
// Select the first item.
SetPendingStateItem(submenu->GetMenuItemAt(0));
EXPECT_EQ(1, pending_state_item()->GetCommand());
// Select the last menu item.
DispatchKey(ui::VKEY_END);
EXPECT_EQ(4, pending_state_item()->GetCommand());
}
// MenuController tests which set expectations about how menu item selection
// behaves should verify test cases work as intended for all supported selection
// mechanisms.
class MenuControllerSelectionTest : public MenuControllerTest {
public:
MenuControllerSelectionTest() = default;
protected:
// Models a mechanism by which menu item selection can be incremented and/or
// decremented.
struct SelectionMechanism {
base::RepeatingClosure IncrementSelection;
base::RepeatingClosure DecrementSelection;
};
// Returns all mechanisms by which menu item selection can be incremented
// and/or decremented.
const std::vector<SelectionMechanism>& selection_mechanisms() {
return selection_mechanisms_;
}
private:
const std::vector<SelectionMechanism> selection_mechanisms_ = {
// Updates selection via IncrementSelection()/DecrementSelection().
{base::BindRepeating(&MenuControllerSelectionTest::IncrementSelection,
base::Unretained(this)),
base::BindRepeating(&MenuControllerSelectionTest::DecrementSelection,
base::Unretained(this))},
// Updates selection via down/up arrow keys.
{base::BindRepeating(&MenuControllerTest::DispatchKey,
base::Unretained(this),
ui::VKEY_DOWN),
base::BindRepeating(&MenuControllerTest::DispatchKey,
base::Unretained(this),
ui::VKEY_UP)},
// Updates selection via next/prior keys.
{base::BindRepeating(&MenuControllerTest::DispatchKey,
base::Unretained(this),
ui::VKEY_NEXT),
base::BindRepeating(&MenuControllerTest::DispatchKey,
base::Unretained(this),
ui::VKEY_PRIOR)}};
};
// Tests that opening menu and exercising various mechanisms to update
// selection iterates over enabled items.
TEST_F(MenuControllerSelectionTest, NextSelectedItem) {
ShowSubmenu();
SubmenuView* const submenu = menu_item()->GetSubmenu();
for (const auto& selection_mechanism : selection_mechanisms()) {
// Disabling the item "Three" gets it skipped when using keyboard to
// navigate.
submenu->GetMenuItemAt(2)->SetEnabled(false);
// Fake initial hot selection.
SetPendingStateItem(submenu->GetMenuItemAt(0));
EXPECT_EQ(1, pending_state_item()->GetCommand());
// Move down in the menu.
// Select next item.
selection_mechanism.IncrementSelection.Run();
EXPECT_EQ(2, pending_state_item()->GetCommand());
// Skip disabled item.
selection_mechanism.IncrementSelection.Run();
EXPECT_EQ(4, pending_state_item()->GetCommand());
selection_mechanism.IncrementSelection.Run();
if (SelectionWraps()) {
// Wrap around.
EXPECT_EQ(1, pending_state_item()->GetCommand());
// Move up in the menu.
// Wrap around.
selection_mechanism.DecrementSelection.Run();
}
EXPECT_EQ(4, pending_state_item()->GetCommand());
// Skip disabled item.
selection_mechanism.DecrementSelection.Run();
EXPECT_EQ(2, pending_state_item()->GetCommand());
// Select previous item.
selection_mechanism.DecrementSelection.Run();
EXPECT_EQ(1, pending_state_item()->GetCommand());
}
}
// Tests that opening menu and exercising various mechanisms to decrement
// selection selects the last enabled menu item.
TEST_F(MenuControllerSelectionTest, PreviousSelectedItem) {
ShowSubmenu();
SubmenuView* const submenu = menu_item()->GetSubmenu();
for (const auto& selection_mechanism : selection_mechanisms()) {
// Disabling the item "Four" gets it skipped when using keyboard to
// navigate.
submenu->GetMenuItemAt(3)->SetEnabled(false);
// Fake initial root item selection and submenu showing.
SetPendingStateItem(menu_item());
EXPECT_EQ(0, pending_state_item()->GetCommand());
// Move up and select a previous (in our case the last enabled) item.
selection_mechanism.DecrementSelection.Run();
EXPECT_EQ(3, pending_state_item()->GetCommand());
}
}
// Tests that the APIs related to the current selected item work correctly.
TEST_F(MenuControllerTest, CurrentSelectedItem) {
ShowSubmenu();
SetPendingStateItem(menu_item()->GetSubmenu()->GetMenuItemAt(0));
EXPECT_EQ(1, pending_state_item()->GetCommand());
// Select the first menu-item.
DispatchKey(ui::VKEY_HOME);
EXPECT_EQ(pending_state_item(), menu_controller()->GetSelectedMenuItem());
// The API should let the submenu stay open if already so, but clear any
// selections within it.
EXPECT_TRUE(showing());
EXPECT_EQ(1, pending_state_item()->GetCommand());
menu_controller()->SelectItemAndOpenSubmenu(menu_item());
EXPECT_TRUE(showing());
EXPECT_EQ(0, pending_state_item()->GetCommand());
}
// Tests that opening menu and calling SelectByChar works correctly.
TEST_F(MenuControllerTest, SelectByChar) {
SetComboboxType(MenuController::ComboboxType::kReadonly);
ShowSubmenu();
// Handle null character should do nothing.
SelectByChar(0);
EXPECT_EQ(0, pending_state_item()->GetCommand());
// Handle searching for 'f'; should find "Four".
SelectByChar('f');
EXPECT_EQ(4, pending_state_item()->GetCommand());
}
TEST_F(MenuControllerTest, SelectChildButtonView) {
AddButtonMenuItems(/*single_child=*/false);
SubmenuView* const submenu = menu_item()->GetSubmenu();
const View* const buttons_view = submenu->children()[4];
ASSERT_NE(nullptr, buttons_view);
GET_CHILD_BUTTON(button1, buttons_view, 0);
GET_CHILD_BUTTON(button2, buttons_view, 1);
GET_CHILD_BUTTON(button3, buttons_view, 2);
// Handle searching for 'f'; should find "Four".
SelectByChar('f');
EXPECT_EQ(4, pending_state_item()->GetCommand());
EXPECT_FALSE(button1->IsHotTracked());
EXPECT_FALSE(button2->IsHotTracked());
EXPECT_FALSE(button3->IsHotTracked());
// Move selection to |button1|.
IncrementSelection();
EXPECT_EQ(5, pending_state_item()->GetCommand());
EXPECT_TRUE(button1->IsHotTracked());
EXPECT_FALSE(button2->IsHotTracked());
EXPECT_FALSE(button3->IsHotTracked());
// Move selection to |button2|.
IncrementSelection();
EXPECT_EQ(5, pending_state_item()->GetCommand());
EXPECT_FALSE(button1->IsHotTracked());
EXPECT_TRUE(button2->IsHotTracked());
EXPECT_FALSE(button3->IsHotTracked());
// Move selection to |button3|.
IncrementSelection();
EXPECT_EQ(5, pending_state_item()->GetCommand());
EXPECT_FALSE(button1->IsHotTracked());
EXPECT_FALSE(button2->IsHotTracked());
EXPECT_TRUE(button3->IsHotTracked());
// Move a mouse to hot track the |button1|.
const gfx::Point location = View::ConvertPointToTarget(
button1, submenu, button1->GetLocalBounds().CenterPoint());
ProcessMouseMoved(
submenu, ui::MouseEvent(ui::EventType::kMouseMoved, location, location,
ui::EventTimeForNow(), 0, 0));
EXPECT_EQ(button1, hot_button());
EXPECT_TRUE(button1->IsHotTracked());
// Incrementing selection should move hot tracking to the second button
// (next after the first button).
IncrementSelection();
EXPECT_EQ(5, pending_state_item()->GetCommand());
EXPECT_FALSE(button1->IsHotTracked());
EXPECT_TRUE(button2->IsHotTracked());
EXPECT_FALSE(button3->IsHotTracked());
// Increment selection twice to wrap around.
IncrementSelection();
IncrementSelection();
EXPECT_EQ(SelectionWraps() ? 1 : 5, pending_state_item()->GetCommand());
}
TEST_F(MenuControllerTest, DeleteChildButtonView) {
AddButtonMenuItems(/*single_child=*/false);
// Handle searching for 'f'; should find "Four".
SelectByChar('f');
EXPECT_EQ(4, pending_state_item()->GetCommand());
const View* const buttons_view = menu_item()->GetSubmenu()->children()[4];
ASSERT_NE(nullptr, buttons_view);
GET_CHILD_BUTTON(button1, buttons_view, 0);
GET_CHILD_BUTTON(button2, buttons_view, 1);
GET_CHILD_BUTTON(button3, buttons_view, 2);
EXPECT_FALSE(button1->IsHotTracked());
EXPECT_FALSE(button2->IsHotTracked());
EXPECT_FALSE(button3->IsHotTracked());
// Increment twice to move selection to |button2|.
IncrementSelection();
IncrementSelection();
EXPECT_EQ(5, pending_state_item()->GetCommand());
EXPECT_FALSE(button1->IsHotTracked());
EXPECT_TRUE(button2->IsHotTracked());
EXPECT_FALSE(button3->IsHotTracked());
// Delete |button2| while it is hot-tracked.
// This should update MenuController via ViewHierarchyChanged and reset
// |hot_button_|.
delete button2;
// Incrementing selection should now set hot-tracked item to |button1|.
// It should not crash.
IncrementSelection();
EXPECT_EQ(5, pending_state_item()->GetCommand());
EXPECT_TRUE(button1->IsHotTracked());
EXPECT_FALSE(button3->IsHotTracked());
}
// Verifies that the child button is hot tracked after the host menu item is
// selected by `MenuController::SelectItemAndOpenSubmenu()`.
TEST_F(MenuControllerTest, ChildButtonHotTrackedAfterMenuItemSelection) {
// Add a menu item which owns a button as child.
MenuItemView* const hosting_menu_item =
AddButtonMenuItems(/*single_child=*/true);
ASSERT_FALSE(hosting_menu_item->IsSelected());
GET_CHILD_BUTTON(button, hosting_menu_item, 0);
EXPECT_FALSE(button->IsHotTracked());
menu_controller()->SelectItemAndOpenSubmenu(hosting_menu_item);
EXPECT_TRUE(hosting_menu_item->IsSelected());
EXPECT_TRUE(button->IsHotTracked());
}
// Verifies that the child button of the menu item which is under mouse
// hovering is hot tracked (https://crbug.com/1135000).
TEST_F(MenuControllerTest, ChildButtonHotTrackedAfterMouseMove) {
// Add a menu item which owns a button as child.
const MenuItemView* const hosting_menu_item =
AddButtonMenuItems(/*single_child=*/true);
GET_CHILD_BUTTON(button, hosting_menu_item, 0);
EXPECT_FALSE(button->IsHotTracked());
SubmenuView* const submenu = menu_item()->GetSubmenu();
const gfx::Point location = View::ConvertPointToTarget(
button, submenu, button->GetLocalBounds().CenterPoint());
ProcessMouseMoved(
submenu, ui::MouseEvent(ui::EventType::kMouseMoved, location, location,
ui::EventTimeForNow(), 0, 0));
// After the mouse moves to `button`, `button` should be hot tracked.
EXPECT_EQ(button, hot_button());
EXPECT_TRUE(button->IsHotTracked());
}
// Creates a menu with Button child views, simulates running a nested
// menu and tests that existing the nested run restores hot-tracked child
// view.
TEST_F(MenuControllerTest, ChildButtonHotTrackedWhenNested) {
AddButtonMenuItems(/*single_child=*/false);
// Handle searching for 'f'; should find "Four".
SelectByChar('f');
EXPECT_EQ(4, pending_state_item()->GetCommand());
const View* const buttons_view = menu_item()->GetSubmenu()->children()[4];
ASSERT_NE(nullptr, buttons_view);
GET_CHILD_BUTTON(button1, buttons_view, 0);
GET_CHILD_BUTTON(button2, buttons_view, 1);
GET_CHILD_BUTTON(button3, buttons_view, 2);
EXPECT_FALSE(button1->IsHotTracked());
EXPECT_FALSE(button2->IsHotTracked());
EXPECT_FALSE(button3->IsHotTracked());
// Increment twice to move selection to |button2|.
IncrementSelection();
IncrementSelection();
EXPECT_EQ(5, pending_state_item()->GetCommand());
EXPECT_FALSE(button1->IsHotTracked());
EXPECT_TRUE(button2->IsHotTracked());
EXPECT_FALSE(button3->IsHotTracked());
EXPECT_EQ(button2, hot_button());
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
// |button2| should stay in hot-tracked state but menu controller should not
// track it anymore (preventing resetting hot-tracked state when changing
// selection while a nested run is active).
EXPECT_TRUE(button2->IsHotTracked());
EXPECT_EQ(nullptr, hot_button());
// Setting hot-tracked button while nested should get reverted when nested
// menu run ends.
SetHotTrackedButton(button1);
EXPECT_TRUE(button1->IsHotTracked());
EXPECT_EQ(button1, hot_button());
// Setting the hot tracked state twice on the same button via the
// menu controller should still set the hot tracked state on the button
// again.
button1->SetHotTracked(false);
SetHotTrackedButton(button1);
EXPECT_TRUE(button1->IsHotTracked());
EXPECT_EQ(button1, hot_button());
ExitMenuRun();
EXPECT_FALSE(button1->IsHotTracked());
EXPECT_TRUE(button2->IsHotTracked());
EXPECT_EQ(button2, hot_button());
}
// Tests that a menu opened asynchronously, will notify its
// MenuControllerDelegate when Accept is called.
TEST_F(MenuControllerTest, AsynchronousAccept) {
views::test::DisableMenuClosureAnimations();
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
EXPECT_EQ(0, menu_controller_delegate()->on_menu_closed_called());
MenuItemView* const accepted = menu_item()->GetSubmenu()->GetMenuItemAt(0);
constexpr int kEventFlags = 42;
Accept(accepted, kEventFlags);
EXPECT_EQ(1, menu_controller_delegate()->on_menu_closed_called());
EXPECT_EQ(accepted, menu_controller_delegate()->on_menu_closed_menu());
EXPECT_EQ(kEventFlags,
menu_controller_delegate()->on_menu_closed_mouse_event_flags());
EXPECT_EQ(internal::MenuControllerDelegate::NOTIFY_DELEGATE,
menu_controller_delegate()->on_menu_closed_notify_type());
}
// Tests that a menu opened asynchronously, will notify its
// MenuControllerDelegate when CancelAll is called.
TEST_F(MenuControllerTest, AsynchronousCancelAll) {
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
EXPECT_EQ(0, menu_controller_delegate()->on_menu_closed_called());
menu_controller()->Cancel(MenuController::ExitType::kAll);
EXPECT_EQ(1, menu_controller_delegate()->on_menu_closed_called());
EXPECT_EQ(nullptr, menu_controller_delegate()->on_menu_closed_menu());
EXPECT_EQ(0, menu_controller_delegate()->on_menu_closed_mouse_event_flags());
EXPECT_EQ(internal::MenuControllerDelegate::NOTIFY_DELEGATE,
menu_controller_delegate()->on_menu_closed_notify_type());
EXPECT_EQ(MenuController::ExitType::kAll, menu_controller()->exit_type());
}
// Tests that canceling a nested menu restores the previous
// MenuControllerDelegate, and notifies each delegate.
TEST_F(MenuControllerTest, AsynchronousNestedDelegate) {
auto nested_delegate = std::make_unique<TestMenuControllerDelegate>();
menu_controller()->AddNestedDelegate(nested_delegate.get());
EXPECT_EQ(nested_delegate.get(), current_controller_delegate());
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
menu_controller()->Cancel(MenuController::ExitType::kAll);
EXPECT_EQ(menu_controller_delegate(), current_controller_delegate());
EXPECT_EQ(1, menu_controller_delegate()->on_menu_closed_called());
EXPECT_EQ(1, nested_delegate->on_menu_closed_called());
EXPECT_EQ(nullptr, nested_delegate->on_menu_closed_menu());
EXPECT_EQ(0, nested_delegate->on_menu_closed_mouse_event_flags());
EXPECT_EQ(internal::MenuControllerDelegate::NOTIFY_DELEGATE,
nested_delegate->on_menu_closed_notify_type());
EXPECT_EQ(MenuController::ExitType::kAll, menu_controller()->exit_type());
}
// Tests that dropping within an asynchronous menu does not hide the menu.
TEST_F(MenuControllerTest, AsynchronousPerformDrop) {
SubmenuView* const source = menu_item()->GetSubmenu();
MenuItemView* const target = source->GetMenuItemAt(0);
SetDropMenuItem(target, MenuDelegate::DropPosition::kAfter);
ui::OSExchangeData drop_data;
gfx::PointF location(target->origin());
const ui::DropTargetEvent target_event(drop_data, location, location,
ui::DragDropTypes::DRAG_MOVE);
DragOperation output_drag_op = DragOperation::kNone;
menu_controller()
->GetDropCallback(source, target_event)
.Run(target_event, output_drag_op,
/*drag_image_layer_owner=*/nullptr);
EXPECT_TRUE(static_cast<test::TestMenuDelegate*>(target->GetDelegate())
->is_drop_performed());
EXPECT_TRUE(showing());
EXPECT_EQ(0, menu_controller_delegate()->on_menu_closed_called());
}
// Tests that dragging within an asynchronous menu notifies the
// MenuControllerDelegate for shutdown.
TEST_F(MenuControllerTest, AsynchronousDragComplete) {
TestDragCompleteThenDestroyOnMenuClosed();
menu_controller()->OnDragWillStart();
menu_controller()->OnDragComplete(true);
EXPECT_EQ(1, menu_controller_delegate()->on_menu_closed_called());
EXPECT_EQ(nullptr, menu_controller_delegate()->on_menu_closed_menu());
EXPECT_EQ(internal::MenuControllerDelegate::NOTIFY_DELEGATE,
menu_controller_delegate()->on_menu_closed_notify_type());
}
// Tests that completing drag with `should_close` set to false will not close
// the menu.
TEST_F(MenuControllerTest, AsynchronousDragCompleteWithoutClose) {
TestDragCompleteThenDestroyOnMenuClosed();
menu_controller()->OnDragWillStart();
menu_controller()->OnDragComplete(false);
// TODO(crbug.com/375959961): For X11, the menu is closed on drag completion
// because the native widget's state is not properly updated.
EXPECT_EQ(BUILDFLAG(IS_OZONE_X11) ? 1 : 0,
menu_controller_delegate()->on_menu_closed_called());
}
// Tests that if Cancel is called during a drag, that OnMenuClosed is still
// notified when the drag completes.
TEST_F(MenuControllerTest, AsynchronousCancelDuringDrag) {
TestDragCompleteThenDestroyOnMenuClosed();
menu_controller()->OnDragWillStart();
menu_controller()->Cancel(MenuController::ExitType::kAll);
menu_controller()->OnDragComplete(true);
EXPECT_EQ(1, menu_controller_delegate()->on_menu_closed_called());
EXPECT_EQ(nullptr, menu_controller_delegate()->on_menu_closed_menu());
EXPECT_EQ(internal::MenuControllerDelegate::NOTIFY_DELEGATE,
menu_controller_delegate()->on_menu_closed_notify_type());
}
// Tests that if a menu is destroyed while drag operations are occurring, that
// the MenuHost does not crash as the drag completes.
TEST_F(MenuControllerTest, AsynchronousDragHostDeleted) {
ShowSubmenu();
SubmenuView* const submenu = menu_item()->GetSubmenu();
MenuHost* const host = menu_host_for_submenu(submenu);
MenuHostOnDragWillStart(host);
submenu->Close();
DestroyMenuItem();
MenuHostOnDragComplete(host);
}
// Tests that getting the drop callback does not hide the menu.
TEST_F(MenuControllerTest, AsyncDropCallback) {
SubmenuView* const source = menu_item()->GetSubmenu();
MenuItemView* const target = source->GetMenuItemAt(0);
SetDropMenuItem(target, MenuDelegate::DropPosition::kAfter);
ui::OSExchangeData drop_data;
gfx::PointF location(target->origin());
const ui::DropTargetEvent target_event(drop_data, location, location,
ui::DragDropTypes::DRAG_MOVE);
auto drop_cb = menu_controller()->GetDropCallback(source, target_event);
const auto* const menu_delegate =
static_cast<test::TestMenuDelegate*>(target->GetDelegate());
EXPECT_FALSE(menu_delegate->is_drop_performed());
EXPECT_TRUE(showing());
EXPECT_EQ(0, menu_controller_delegate()->on_menu_closed_called());
DragOperation output_drag_op;
std::move(drop_cb).Run(target_event, output_drag_op,
/*drag_image_layer_owner=*/nullptr);
EXPECT_TRUE(menu_delegate->is_drop_performed());
}
// Tests that getting the drop callback hides the menu when it's being shown for
// a drop.
TEST_F(MenuControllerForDropTest, AsyncDropCallback) {
SubmenuView* const source = menu_item()->GetSubmenu();
MenuItemView* const target = source->GetMenuItemAt(0);
SetDropMenuItem(target, MenuDelegate::DropPosition::kAfter);
ui::OSExchangeData drop_data;
gfx::PointF location(target->origin());
const ui::DropTargetEvent target_event(drop_data, location, location,
ui::DragDropTypes::DRAG_MOVE);
auto drop_cb = menu_controller()->GetDropCallback(source, target_event);
const auto* const menu_delegate =
static_cast<test::TestMenuDelegate*>(target->GetDelegate());
EXPECT_FALSE(menu_delegate->is_drop_performed());
EXPECT_FALSE(showing());
EXPECT_EQ(1, menu_controller_delegate()->on_menu_closed_called());
DragOperation output_drag_op;
std::move(drop_cb).Run(target_event, output_drag_op,
/*drag_image_layer_owner=*/nullptr);
EXPECT_TRUE(menu_delegate->is_drop_performed());
}
TEST_F(MenuControllerForDropTest, OnMouseReleasedIgnored) {
ShowSubmenu();
SubmenuView* const submenu = menu_item()->GetSubmenu();
MenuItemView* const target = submenu->GetMenuItemAt(0);
const gfx::Point press_location = target->bounds().CenterPoint();
ProcessMouseReleased(
submenu, ui::MouseEvent(ui::EventType::kMouseReleased, press_location,
press_location, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, 0));
// The command shouldn't be executed if this menu is open for a drop.
EXPECT_EQ(menu_delegate()->execute_command_id(),
test::TestMenuDelegate::kInvalidExecuteCommandId);
EXPECT_EQ(menu_controller_delegate()->on_menu_closed_called(), 0);
EXPECT_TRUE(showing());
}
// Widget destruction and cleanup occurs on the MessageLoop after the
// MenuController has been destroyed. A MenuHostRootView should not attempt to
// access a destroyed MenuController. This test should not cause a crash.
TEST_F(MenuControllerTest, HostReceivesInputBeforeDestruction) {
ShowSubmenu();
SubmenuView* const submenu = menu_item()->GetSubmenu();
const gfx::Point location =
submenu->bounds().bottom_right() + gfx::Vector2d(1, 1);
// Normally created as the full Widget is brought up. Explicitly created
// here for testing.
std::unique_ptr<MenuHostRootView> root_view(
CreateMenuHostRootView(menu_host_for_submenu(submenu)));
DestroyMenuController();
// This should not attempt to access the destroyed MenuController and should
// not crash.
root_view->OnMouseMoved(ui::MouseEvent(ui::EventType::kMouseMoved, location,
location, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, 0));
}
// Tests that an asynchronous menu nested within an asynchronous menu closes
// both menus, and notifies both delegates.
TEST_F(MenuControllerTest, DoubleAsynchronousNested) {
// Nested run
auto nested_delegate = std::make_unique<TestMenuControllerDelegate>();
menu_controller()->AddNestedDelegate(nested_delegate.get());
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
menu_controller()->Cancel(MenuController::ExitType::kAll);
EXPECT_EQ(1, menu_controller_delegate()->on_menu_closed_called());
EXPECT_EQ(1, nested_delegate->on_menu_closed_called());
}
// Tests that setting send_gesture_events_to_owner flag forwards gesture
// events to owner and the forwarding stops when the current gesture sequence
// ends.
TEST_F(MenuControllerTest, PreserveGestureForOwner) {
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kBottomCenter);
ShowSubmenu();
SubmenuView* const submenu = menu_item()->GetSubmenu();
const gfx::Point location =
submenu->GetLocalBounds().bottom_left() + gfx::Vector2d(0, 10);
const ui::GestureEvent event(
location.x(), location.y(), 0, ui::EventTimeForNow(),
ui::GestureEventDetails(ui::EventType::kGestureScrollBegin));
// Gesture events should not be forwarded if the flag is not set.
EXPECT_EQ(owner_gesture_count(), 0);
EXPECT_FALSE(menu_controller()->send_gesture_events_to_owner());
ProcessGestureEvent(submenu, event);
EXPECT_EQ(owner_gesture_count(), 0);
// The menu's owner should receive gestures triggered outside the menu.
menu_controller()->set_send_gesture_events_to_owner(true);
ProcessGestureEvent(submenu, event);
EXPECT_EQ(owner_gesture_count(), 1);
const ui::GestureEvent event2(
location.x(), location.y(), 0, ui::EventTimeForNow(),
ui::GestureEventDetails(ui::EventType::kGestureEnd));
ProcessGestureEvent(submenu, event2);
EXPECT_EQ(owner_gesture_count(), 2);
// EventType::kGestureEnd resets the |send_gesture_events_to_owner_| flag, so
// further gesture events should not be sent to the owner.
ProcessGestureEvent(submenu, event2);
EXPECT_EQ(owner_gesture_count(), 2);
}
#if defined(USE_AURA)
// Tests that setting `send_gesture_events_to_owner` flag forwards gesture
// events to the NativeView specified for gestures and not the owner's
// NativeView.
TEST_F(MenuControllerTest, ForwardsEventsToNativeViewForGestures) {
aura::test::EventCountDelegate child_delegate;
auto child_window = std::make_unique<aura::Window>(&child_delegate);
child_window->Init(ui::LAYER_TEXTURED);
owner()->GetNativeView()->AddChild(child_window.get());
// Ensure menu is closed before running with the menu with `child_window` as
// the NativeView for gestures.
menu_controller()->Cancel(MenuController::ExitType::kAll);
menu_controller()->Run(
owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kBottomCenter, ui::mojom::MenuSourceType::kNone,
MenuController::MenuType::kNormal, false, child_window.get());
ShowSubmenu(nullptr, [&](auto& params) {
params.native_view_for_gestures = child_window.get();
});
SubmenuView* const submenu = menu_item()->GetSubmenu();
const gfx::Point location =
submenu->GetLocalBounds().bottom_left() + gfx::Vector2d(0, 10);
const ui::GestureEvent event(
location.x(), location.y(), 0, ui::EventTimeForNow(),
ui::GestureEventDetails(ui::EventType::kGestureScrollBegin));
// Gesture events should not be forwarded to either the `child_window` or
// the hosts native window if the flag is not set.
EXPECT_EQ(0, owner_gesture_count());
EXPECT_EQ(0, child_delegate.GetGestureCountAndReset());
EXPECT_FALSE(menu_controller()->send_gesture_events_to_owner());
ProcessGestureEvent(submenu, event);
EXPECT_EQ(0, owner_gesture_count());
EXPECT_EQ(0, child_delegate.GetGestureCountAndReset());
// The `child_window` should receive gestures triggered outside the menu.
menu_controller()->set_send_gesture_events_to_owner(true);
ProcessGestureEvent(submenu, event);
EXPECT_EQ(0, owner_gesture_count());
EXPECT_EQ(1, child_delegate.GetGestureCountAndReset());
const ui::GestureEvent event2(
location.x(), location.y(), 0, ui::EventTimeForNow(),
ui::GestureEventDetails(ui::EventType::kGestureEnd));
ProcessGestureEvent(submenu, event2);
EXPECT_EQ(0, owner_gesture_count());
EXPECT_EQ(1, child_delegate.GetGestureCountAndReset());
// EventType::kGestureEnd resets the `send_gesture_events_to_owner_` flag, so
// further gesture events should not be sent to the `child_window`.
ProcessGestureEvent(submenu, event2);
EXPECT_EQ(0, owner_gesture_count());
EXPECT_EQ(0, child_delegate.GetGestureCountAndReset());
}
#endif
// Tests that touch outside menu does not closes the menu when forwarding
// gesture events to owner.
TEST_F(MenuControllerTest, NoTouchCloseWhenSendingGesturesToOwner) {
views::test::DisableMenuClosureAnimations();
// Owner wants the gesture events.
menu_controller()->set_send_gesture_events_to_owner(true);
// Show a sub menu and touch outside of it.
ShowSubmenu();
SubmenuView* const submenu = menu_item()->GetSubmenu();
const gfx::Point location =
submenu->GetLocalBounds().bottom_right() + gfx::Vector2d(1, 1);
const ui::TouchEvent touch_event(
ui::EventType::kTouchPressed, location, ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::kTouch, 0));
ProcessTouchEvent(submenu, touch_event);
// Menu should still be visible.
EXPECT_TRUE(showing());
// The current gesture sequence ends.
ProcessGestureEvent(
submenu,
ui::GestureEvent(location.x(), location.y(), 0, ui::EventTimeForNow(),
ui::GestureEventDetails(ui::EventType::kGestureEnd)));
// Touch outside again and menu should be closed.
ProcessTouchEvent(submenu, touch_event);
views::test::WaitForMenuClosureAnimation();
EXPECT_FALSE(showing());
EXPECT_EQ(MenuController::ExitType::kAll, menu_controller()->exit_type());
}
// Tests that a nested menu does not crash when trying to repost events that
// occur outside of the bounds of the menu. Instead a proper shutdown should
// occur.
TEST_F(MenuControllerTest, AsynchronousRepostEvent) {
views::test::DisableMenuClosureAnimations();
auto nested_delegate = std::make_unique<TestMenuControllerDelegate>();
menu_controller()->AddNestedDelegate(nested_delegate.get());
EXPECT_EQ(nested_delegate.get(), current_controller_delegate());
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
// Show a sub menu to target with a pointer selection. However have the
// event occur outside of the bounds of the entire menu.
ShowSubmenu();
SubmenuView* const submenu = menu_item()->GetSubmenu();
const gfx::Point location =
submenu->GetLocalBounds().bottom_right() + gfx::Vector2d(1, 1);
// When attempting to select outside of all menus this should lead to a
// shutdown. This should not crash while attempting to repost the event.
SetSelectionOnPointerDown(
submenu,
ui::MouseEvent(ui::EventType::kMousePressed, location, location,
ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0));
views::test::WaitForMenuClosureAnimation();
EXPECT_EQ(menu_controller_delegate(), current_controller_delegate());
EXPECT_EQ(1, menu_controller_delegate()->on_menu_closed_called());
EXPECT_EQ(1, nested_delegate->on_menu_closed_called());
EXPECT_EQ(nullptr, nested_delegate->on_menu_closed_menu());
EXPECT_EQ(0, nested_delegate->on_menu_closed_mouse_event_flags());
EXPECT_EQ(internal::MenuControllerDelegate::NOTIFY_DELEGATE,
nested_delegate->on_menu_closed_notify_type());
EXPECT_EQ(MenuController::ExitType::kAll, menu_controller()->exit_type());
}
// Tests that an asynchronous menu reposts touch events that occur outside of
// the bounds of the menu, and that the menu closes.
TEST_F(MenuControllerTest, AsynchronousTouchEventRepostEvent) {
views::test::DisableMenuClosureAnimations();
// Show a sub menu to target with a touch event. However have the event
// occur outside of the bounds of the entire menu.
ShowSubmenu();
SubmenuView* const submenu = menu_item()->GetSubmenu();
const gfx::Point location =
submenu->GetLocalBounds().bottom_right() + gfx::Vector2d(1, 1);
ProcessTouchEvent(
submenu,
ui::TouchEvent(ui::EventType::kTouchPressed, location,
ui::EventTimeForNow(),
ui::PointerDetails(ui::EventPointerType::kTouch, 0)));
views::test::WaitForMenuClosureAnimation();
EXPECT_FALSE(showing());
EXPECT_EQ(1, menu_controller_delegate()->on_menu_closed_called());
EXPECT_EQ(nullptr, menu_controller_delegate()->on_menu_closed_menu());
EXPECT_EQ(0, menu_controller_delegate()->on_menu_closed_mouse_event_flags());
EXPECT_EQ(internal::MenuControllerDelegate::NOTIFY_DELEGATE,
menu_controller_delegate()->on_menu_closed_notify_type());
EXPECT_EQ(MenuController::ExitType::kAll, menu_controller()->exit_type());
}
// Tests that having the MenuController deleted during RepostEvent does not
// cause a crash. ASAN bots should not detect use-after-free in
// MenuController.
TEST_F(MenuControllerTest, AsynchronousRepostEventDeletesController) {
views::test::DisableMenuClosureAnimations();
auto nested_delegate = std::make_unique<TestMenuControllerDelegate>();
menu_controller()->AddNestedDelegate(nested_delegate.get());
EXPECT_EQ(nested_delegate.get(), current_controller_delegate());
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
// Show a sub menu to target with a pointer selection. However have the
// event occur outside of the bounds of the entire menu.
ShowSubmenu();
SubmenuView* const submenu = menu_item()->GetSubmenu();
const gfx::Point location =
submenu->GetLocalBounds().bottom_right() + gfx::Vector2d(1, 1);
// This will lead to MenuController being deleted during the event repost.
// The remainder of this test, and TearDown should not crash.
DestroyMenuControllerOnMenuClosed(nested_delegate.get());
// When attempting to select outside of all menus this should lead to a
// shutdown. This should not crash while attempting to repost the event.
SetSelectionOnPointerDown(
submenu,
ui::MouseEvent(ui::EventType::kMousePressed, location, location,
ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0));
views::test::WaitForMenuClosureAnimation();
// Close to remove observers before test TearDown
submenu->Close();
EXPECT_EQ(1, nested_delegate->on_menu_closed_called());
}
// Tests that having the MenuController deleted during OnGestureEvent does not
// cause a crash. ASAN bots should not detect use-after-free in
// MenuController.
TEST_F(MenuControllerTest, AsynchronousGestureDeletesController) {
views::test::DisableMenuClosureAnimations();
auto nested_delegate = std::make_unique<TestMenuControllerDelegate>();
menu_controller()->AddNestedDelegate(nested_delegate.get());
EXPECT_EQ(nested_delegate.get(), current_controller_delegate());
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
// Show a sub menu to target with a tap event.
ShowSubmenu();
SubmenuView* const submenu = menu_item()->GetSubmenu();
const gfx::Point location = submenu->GetMenuItemAt(0)->bounds().CenterPoint();
// This will lead to MenuController being deleted during the processing of
// the gesture event. The remainder of this test, and TearDown should not
// crash.
DestroyMenuControllerOnMenuClosed(nested_delegate.get());
ProcessGestureEvent(
submenu,
ui::GestureEvent(location.x(), location.y(), 0, ui::EventTimeForNow(),
ui::GestureEventDetails(ui::EventType::kGestureTap)));
views::test::WaitForMenuClosureAnimation();
// Close to remove observers before test TearDown
submenu->Close();
EXPECT_EQ(1, nested_delegate->on_menu_closed_called());
}
// Test that the menu is properly placed where it best fits.
TEST_F(MenuControllerTest, CalculateMenuBoundsBestFitTest) {
const bool ignore_screen_bounds_for_menus =
ShouldIgnoreScreenBoundsForMenus();
// Fits in all locations -> placed below.
MenuBoundsOptions options;
options.anchor_bounds =
gfx::Rect(options.menu_size.width(), options.menu_size.height(), 0, 0);
options.monitor_bounds =
gfx::Rect(0, 0, options.anchor_bounds.right() + options.menu_size.width(),
options.anchor_bounds.bottom() + options.menu_size.height());
gfx::Rect expected(options.anchor_bounds.x(), options.anchor_bounds.bottom(),
options.menu_size.width(), options.menu_size.height());
EXPECT_EQ(expected, CalculateMenuBounds(options));
// Fits above and to both sides -> placed above.
options.monitor_bounds.set_height(options.anchor_bounds.bottom());
expected.set_y(
options.anchor_bounds.y() -
(ignore_screen_bounds_for_menus ? 0 : options.menu_size.height()));
EXPECT_EQ(expected, CalculateMenuBounds(options));
// Fits on both sides, prefer right -> placed right.
options.anchor_bounds.set_y(options.menu_size.height() / 2);
options.monitor_bounds.set_height(options.menu_size.height());
if (ignore_screen_bounds_for_menus) {
expected.set_y(options.anchor_bounds.y());
} else {
expected.set_origin(
{options.anchor_bounds.right(), options.monitor_bounds.y()});
}
EXPECT_EQ(expected, CalculateMenuBounds(options));
// Fits only on left -> placed left.
options.monitor_bounds.set_width(options.anchor_bounds.right());
if (!ignore_screen_bounds_for_menus) {
expected.set_x(options.anchor_bounds.x() - options.menu_size.width());
}
EXPECT_EQ(expected, CalculateMenuBounds(options));
// Fits on both sides, prefer left -> placed left.
options.menu_anchor = MenuAnchorPosition::kTopRight;
options.monitor_bounds.set_width(options.anchor_bounds.right() +
options.menu_size.width());
if (ignore_screen_bounds_for_menus) {
expected.set_x(options.anchor_bounds.right() - options.menu_size.width());
}
EXPECT_EQ(expected, CalculateMenuBounds(options));
// Fits only on right -> placed right.
options.anchor_bounds.set_x(0);
expected.set_x(
options.anchor_bounds.right() -
(ignore_screen_bounds_for_menus ? options.menu_size.width() : 0));
EXPECT_EQ(expected, CalculateMenuBounds(options));
}
// Tests that the menu is properly placed according to its anchor.
TEST_F(MenuControllerTest, CalculateMenuBoundsAnchorTest) {
MenuBoundsOptions options = {.menu_anchor = MenuAnchorPosition::kTopLeft};
gfx::Rect expected(options.anchor_bounds.x(), options.anchor_bounds.bottom(),
options.menu_size.width(), options.menu_size.height());
EXPECT_EQ(expected, CalculateMenuBounds(options));
options.menu_anchor = MenuAnchorPosition::kTopRight;
expected.set_x(options.anchor_bounds.right() - options.menu_size.width());
EXPECT_EQ(expected, CalculateMenuBounds(options));
// Menu will be placed above or below with an offset.
options.menu_anchor = MenuAnchorPosition::kBottomCenter;
constexpr int kTouchYPadding = 15;
// Menu fits above -> placed above.
expected.set_origin(
{options.anchor_bounds.x() +
(options.anchor_bounds.width() - options.menu_size.width()) / 2,
options.anchor_bounds.y() - options.menu_size.height() -
kTouchYPadding});
EXPECT_EQ(expected, CalculateMenuBounds(options));
// Menu does not fit above -> placed below.
options.anchor_bounds = gfx::Rect(options.menu_size.width(),
options.menu_size.height() / 2, 0, 0);
expected.set_origin(
{options.anchor_bounds.x() +
(options.anchor_bounds.width() - options.menu_size.width()) / 2,
(ShouldIgnoreScreenBoundsForMenus()
? (-options.anchor_bounds.bottom() - kTouchYPadding)
: options.anchor_bounds.y() + kTouchYPadding)});
EXPECT_EQ(expected, CalculateMenuBounds(options));
}
// Regression test for https://crbug.com/1217711
TEST_F(MenuControllerTest, MenuAnchorPositionFlippedInRtl) {
ASSERT_FALSE(base::i18n::IsRTL());
// Test the AdjustAnchorPositionForRtl() method directly, rather than
// running the menu, because it's awkward to access the menu's window. Also,
// the menu bounds are already tested separately.
constexpr struct {
MenuAnchorPosition original_position;
MenuAnchorPosition mirrored_position;
} kPositions[] = {
{MenuAnchorPosition::kTopLeft, MenuAnchorPosition::kTopRight},
{MenuAnchorPosition::kBubbleTopLeft, MenuAnchorPosition::kBubbleTopRight},
{MenuAnchorPosition::kBubbleLeft, MenuAnchorPosition::kBubbleRight},
{MenuAnchorPosition::kBubbleBottomLeft,
MenuAnchorPosition::kBubbleBottomRight}};
for (const auto& position : kPositions) {
EXPECT_EQ(position.original_position,
AdjustAnchorPositionForRtl(position.original_position));
EXPECT_EQ(position.mirrored_position,
AdjustAnchorPositionForRtl(position.mirrored_position));
}
base::i18n::SetRTLForTesting(true);
// Anchor positions are left/right flipped in RTL.
for (const auto& position : kPositions) {
EXPECT_EQ(position.mirrored_position,
AdjustAnchorPositionForRtl(position.original_position));
EXPECT_EQ(position.original_position,
AdjustAnchorPositionForRtl(position.mirrored_position));
}
base::i18n::SetRTLForTesting(false);
}
TEST_F(MenuControllerTest, CalculateMenuBoundsMonitorFitTest) {
constexpr gfx::Rect kMonitorBounds(0, 0, 100, 100);
MenuBoundsOptions options = {
.anchor_bounds = gfx::Rect(),
.monitor_bounds = kMonitorBounds,
.menu_size =
gfx::Size(kMonitorBounds.width() / 2, kMonitorBounds.height() * 2)};
gfx::Rect expected(options.anchor_bounds.x(), options.anchor_bounds.bottom(),
options.menu_size.width(), kMonitorBounds.height());
EXPECT_EQ(expected, CalculateMenuBounds(options));
options.menu_size =
gfx::Size(kMonitorBounds.width() * 2, kMonitorBounds.height() / 2);
expected.set_size({kMonitorBounds.width(), options.menu_size.height()});
EXPECT_EQ(expected, CalculateMenuBounds(options));
options.menu_size =
gfx::Size(kMonitorBounds.width() * 2, kMonitorBounds.height() * 2);
expected.set_size(kMonitorBounds.size());
EXPECT_EQ(expected, CalculateMenuBounds(options));
}
// Test that menus show up on screen with non-zero sized anchors.
TEST_P(MenuControllerTest, TestMenuFitsOnScreen) {
// Simulate multiple display layouts.
constexpr int kDisplaySize = 500;
constexpr int kCoords[] = {-kDisplaySize, 0, kDisplaySize};
for (int x : kCoords) {
for (int y : kCoords) {
const gfx::Rect monitor_bounds(x, y, kDisplaySize, kDisplaySize);
for (auto position : kBubblePositions) {
TestMenuFitsOnScreen(position, monitor_bounds);
}
}
}
}
// Test that menus show up on screen with zero sized anchors.
TEST_P(MenuControllerTest, TestMenuFitsOnScreenSmallAnchor) {
// Simulate multiple display layouts.
constexpr int kDisplaySize = 500;
constexpr int kCoords[] = {-kDisplaySize, 0, kDisplaySize};
for (int x : kCoords) {
for (int y : kCoords) {
const gfx::Rect monitor_bounds(x, y, kDisplaySize, kDisplaySize);
for (auto position : kBubblePositions) {
TestMenuFitsOnScreenSmallAnchor(position, monitor_bounds);
}
}
}
}
// Test that menus fit a small screen.
TEST_P(MenuControllerTest, TestMenuFitsOnSmallScreen) {
// Simulate multiple display layouts.
constexpr int kDisplaySize = 500;
constexpr int kCoords[] = {-kDisplaySize, 0, kDisplaySize};
for (int x : kCoords) {
for (int y : kCoords) {
const gfx::Rect monitor_bounds(x, y, kDisplaySize, kDisplaySize);
for (auto position : kBubblePositions) {
TestMenuFitsOnSmallScreen(position, monitor_bounds);
}
}
}
}
// Test that submenus are displayed within the screen bounds on smaller
// screens.
TEST_P(MenuControllerTest, TestSubmenuFitsOnScreen) {
menu_controller()->set_use_ash_system_ui_layout(true);
SubmenuView* const submenu = menu_item()->GetSubmenu();
const std::vector<MenuItemView*> menu_items = submenu->GetMenuItems();
std::ranges::for_each(base::span(menu_items).subspan<1>(),
[&](auto* item) { menu_item()->RemoveMenuItem(item); });
MenuItemView* const sub_item = submenu->GetMenuItemAt(0);
sub_item->AppendMenuItem(11, u"Subitem.One");
const int menu_width = MenuConfig::instance().touchable_menu_min_width;
const gfx::Size parent_size(menu_width, menu_width);
const gfx::Size parent_size_wide(menu_width * 2, menu_width);
const int display_width = parent_size.width() * 3;
const int display_height = parent_size.height() * 3;
for (auto menu_position : {MenuAnchorPosition::kBubbleTopLeft,
MenuAnchorPosition::kBubbleTopRight}) {
// Simulate multiple display layouts.
for (int x : {-display_width, 0, display_width}) {
for (int y : {-display_height, 0, display_height}) {
const gfx::Rect monitor_bounds(x, y, display_width, display_height);
const int x_min = monitor_bounds.x();
const int x_max = monitor_bounds.right() - parent_size.width();
const int y_min = monitor_bounds.y();
const int y_max = monitor_bounds.bottom() - parent_size.height();
for (const auto& origin :
{gfx::Point(x_min, y_min), gfx::Point(x_max, y_min),
gfx::Point((x_min + x_max) / 2, y_min),
gfx::Point(x_min, (y_min + y_max) / 2),
gfx::Point(x_min, y_max)}) {
TestSubmenuFitsOnScreen(sub_item, monitor_bounds,
gfx::Rect(origin, parent_size),
menu_position);
}
// Extra wide menu: test with insufficient room on both sides.
TestSubmenuFitsOnScreen(
sub_item, monitor_bounds,
gfx::Rect(gfx::Point(x_min + (x_max - x_min) / 4, y_min),
parent_size_wide),
menu_position);
}
}
}
}
// Test that a menu that was originally drawn below the anchor does not get
// squished or move above the anchor when it grows vertically and horizontally
// beyond the monitor bounds.
TEST_F(MenuControllerTest, GrowingMenuMovesLaterallyNotVertically) {
// We can't know the position of windows in Wayland. Thus, this case is not
// valid for Wayland.
if (ShouldIgnoreScreenBoundsForMenus()) {
return;
}
MenuBoundsOptions options = {
// The anchor should be near the bottom right side of the screen.
.anchor_bounds = gfx::Rect(80, 70, 15, 10),
.monitor_bounds = gfx::Rect(0, 0, 100, 100),
// The menu should fit the available space, below the anchor.
.menu_size = gfx::Size(20, 20),
};
// Ensure the menu is initially drawn below the bounds, and the MenuPosition
// is set to MenuPosition::kBelowBounds;
EXPECT_EQ(gfx::Rect(80, 80, 20, 20), CalculateMenuBounds(options));
EXPECT_EQ(MenuItemView::MenuPosition::kBelowBounds,
menu_item_actual_position());
// The menu bounds are larger than the remaining space on the monitor. This
// simulates the case where the menu has been grown vertically and
// horizontally to where it would no longer fit on the screen.
options.menu_size = gfx::Size(50, 50);
options.menu_position = MenuItemView::MenuPosition::kBelowBounds;
// The menu bounds should move left to show the wider menu, and grow to fill
// the remaining vertical space without moving upwards.
EXPECT_EQ(gfx::Rect(50, 80, 50, 20), CalculateMenuBounds(options));
}
#if defined(USE_AURA)
// This tests that mouse moved events from the initial position of the mouse
// when the menu was shown don't select the menu item at the mouse position.
TEST_F(MenuControllerTest, MouseAtMenuItemOnShow) {
// Most tests create an already shown menu but this test needs one that's
// not shown, so it can show it. The mouse position is remembered when
// the menu is shown.
auto menu_item = std::make_unique<MenuItemView>(menu_delegate());
const MenuItemView* const first_item = menu_item->AppendMenuItem(1, u"One");
menu_item->AppendMenuItem(2, u"Two");
menu_item->set_controller(menu_controller());
// Move the mouse to where the first menu item will be shown,
// and show the menu.
const gfx::Size item_size = first_item->CalculatePreferredSize({});
gfx::Point location(item_size.width() / 2, item_size.height() / 2);
GetRootWindow(owner())->MoveCursorTo(location);
menu_controller()->Run(owner(), nullptr, menu_item.get(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
EXPECT_EQ(0, pending_state_item()->GetCommand());
// Synthesize an event at the mouse position when the menu was opened.
// It should be ignored, and selected item shouldn't change.
SubmenuView* const submenu = menu_item->GetSubmenu();
View::ConvertPointFromScreen(submenu, &location);
ProcessMouseMoved(
submenu, ui::MouseEvent(ui::EventType::kMouseMoved, location, location,
ui::EventTimeForNow(), 0, 0));
EXPECT_EQ(0, pending_state_item()->GetCommand());
// Synthesize an event at a slightly different mouse position. It
// should cause the item under the cursor to be selected.
location.Offset(0, 1);
ProcessMouseMoved(
submenu, ui::MouseEvent(ui::EventType::kMouseMoved, location, location,
ui::EventTimeForNow(), 0, 0));
EXPECT_EQ(1, pending_state_item()->GetCommand());
}
// Tests that when an asynchronous menu receives a cancel event, that it
// closes.
TEST_F(MenuControllerTest, AsynchronousCancelEvent) {
ExitMenuRun();
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
EXPECT_EQ(MenuController::ExitType::kNone, menu_controller()->exit_type());
ui::CancelModeEvent cancel_event;
event_generator()->Dispatch(&cancel_event);
EXPECT_EQ(MenuController::ExitType::kAll, menu_controller()->exit_type());
}
TEST_F(MenuControllerTest, WidgetStateChangeCancelsMenu) {
ExitMenuRun();
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
EXPECT_TRUE(showing());
EXPECT_EQ(MenuController::ExitType::kNone, menu_controller()->exit_type());
owner()->SetFullscreen(true);
EXPECT_FALSE(showing());
EXPECT_EQ(MenuController::ExitType::kAll, menu_controller()->exit_type());
}
// TODO(pkasting): The test below fails most of the time on Wayland; not clear
// it's important to support this case.
#if BUILDFLAG(ENABLE_DESKTOP_AURA) && !BUILDFLAG(IS_OZONE_WAYLAND)
class DesktopMenuControllerTest : public MenuControllerTest {
public:
// MenuControllerTest:
void SetUp() override {
set_native_widget_type(NativeWidgetType::kDesktop);
MenuControllerTest::SetUp();
}
};
// Tests that menus without parent widgets do not crash in
// MenuPreTargetHandler. Having neither parent nor context pointers when
// creating a Widget is only valid in desktop Aura.
TEST_F(DesktopMenuControllerTest, RunWithoutWidgetDoesntCrash) {
ExitMenuRun();
menu_controller()->Run(nullptr, nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
}
#endif // BUILDFLAG(ENABLE_DESKTOP_AURA) && !BUILDFLAG(IS_OZONE_WAYLAND)
// Tests that if a MenuController is destroying during drag/drop, and another
// MenuController becomes active, that the exiting of drag does not cause a
// crash.
TEST_F(MenuControllerTest, MenuControllerReplacedDuringDrag) {
// Build the menu so that the appropriate root window is available to set
// the drag drop client on.
AddButtonMenuItems(/*single_child=*/false);
TestDragDropClient drag_drop_client(base::BindRepeating(
&MenuControllerTest::TestMenuControllerReplacementDuringDrag,
base::Unretained(this)));
aura::client::SetDragDropClient(
GetRootWindow(menu_item()->GetSubmenu()->GetWidget()), &drag_drop_client);
StartDrag();
}
// Tests that if a CancelAll is called during drag-and-drop that it does not
// destroy the MenuController. On Windows and Linux this destruction also
// destroys the Widget used for drag-and-drop, thereby ending the drag.
TEST_F(MenuControllerTest, CancelAllDuringDrag) {
// Build the menu so that the appropriate root window is available to set
// the drag drop client on.
AddButtonMenuItems(/*single_child=*/false);
TestDragDropClient drag_drop_client(base::BindRepeating(
&MenuControllerTest::TestCancelAllDuringDrag, base::Unretained(this)));
aura::client::SetDragDropClient(
GetRootWindow(menu_item()->GetSubmenu()->GetWidget()), &drag_drop_client);
StartDrag();
}
// Tests that capture is restored to the submenu after a drag and drop.
TEST_F(MenuControllerTest, RestoreCaptureAfterDrag) {
// Build the menu so that the appropriate root window is available to set
// the drag drop client on.
AddButtonMenuItems(/*single_child=*/false);
menu_delegate()->set_should_close_on_drag_complete(false);
SubmenuView* const base_submenu = menu_item()->GetSubmenu();
MenuHost* const base_host = menu_host_for_submenu(base_submenu);
base_host->SetCapture(base_submenu);
TestDragDropClient drag_drop_client(base::DoNothing());
aura::client::SetDragDropClient(
GetRootWindow(menu_item()->GetSubmenu()->GetWidget()), &drag_drop_client);
EXPECT_TRUE(base_host->HasCapture());
StartDrag();
// TODO(crbug.com/375959961): For X11, the menu is closed on drag completion
// because the native widget's state is not properly updated.
EXPECT_NE(base_host->HasCapture(), BUILDFLAG(IS_OZONE_X11));
}
// Tests that capture is not restored to the submenu after a drag and drop where
// the submenu is no longer showing.
TEST_F(MenuControllerTest, DontRestoreCaptureOnHiddenHostAfterDrag) {
// Build the menu so that the appropriate root window is available to set
// the drag drop client on.
AddButtonMenuItems(/*single_child=*/false);
menu_delegate()->set_should_close_on_drag_complete(true);
SubmenuView* const base_submenu = menu_item()->GetSubmenu();
MenuHost* const base_host = menu_host_for_submenu(base_submenu);
base_host->SetCapture(base_submenu);
TestDragDropClient drag_drop_client(base::DoNothing());
aura::client::SetDragDropClient(
GetRootWindow(menu_item()->GetSubmenu()->GetWidget()), &drag_drop_client);
EXPECT_TRUE(base_host->HasCapture());
StartDrag();
EXPECT_FALSE(base_host->HasCapture());
}
// Tests that capture is not given to the submenu after drag and drop if the
// submenu didn't have capture before.
TEST_F(MenuControllerTest, HostWithoutCaptureAfterDrag) {
// Build the menu so that the appropriate root window is available to set
// the drag drop client on.
AddButtonMenuItems(/*single_child=*/false);
menu_delegate()->set_should_close_on_drag_complete(false);
SubmenuView* const base_submenu = menu_item()->GetSubmenu();
MenuHost* const base_host = menu_host_for_submenu(base_submenu);
base_host->ReleaseCapture();
TestDragDropClient drag_drop_client(base::DoNothing());
aura::client::SetDragDropClient(
GetRootWindow(menu_item()->GetSubmenu()->GetWidget()), &drag_drop_client);
EXPECT_FALSE(base_host->HasCapture());
StartDrag();
EXPECT_FALSE(base_host->HasCapture());
}
// Tests that when releasing the ref on ViewsDelegate and MenuController is
// deleted, that shutdown occurs without crashing.
TEST_F(MenuControllerTest, DestroyedDuringViewsRelease) {
ExitMenuRun();
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
TestDestroyedDuringViewsRelease();
}
// Tests that when a context menu is opened above an empty menu item, and a
// right-click occurs over the empty item, that the bottom menu is not hidden,
// that a request to relaunch the context menu is received, and that
// subsequently pressing ESC does not crash the browser.
TEST_F(MenuControllerTest, RepostEventToEmptyMenuItem) {
// Setup a submenu. Additionally hook up appropriate Widget and View
// containers, with bounds, so that hit testing works.
ShowSubmenu();
SubmenuView* const base_submenu = menu_item()->GetSubmenu();
menu_host_for_submenu(base_submenu)
->SetContentsView(base_submenu->GetScrollViewContainer());
// Build the submenu to have an empty menu item. Additionally hook up
// appropriate Widget and View containers with bounds, so that hit testing
// works.
MenuItemView* submenu_item = menu_item()->AppendSubMenu(0, std::u16string());
submenu_item->UpdateEmptyMenusAndMetrics();
SubmenuView* const submenu_view = submenu_item->GetSubmenu();
const auto insets = submenu_view->GetScrollViewContainer()->GetInsets();
const gfx::Rect bounds(0, 50, 50 + insets.width(), 50 + insets.height());
// TODO(pkasting): The bounds manipulation in this whole test is suspicious;
// understand it more deeply, see why the lambda here is needed and if it
// can be removed or any of this test simplified/clarified.
ShowSubmenu(submenu_view, [&](auto& params) { params.bounds = bounds; });
menu_host_for_submenu(submenu_view)
->SetContentsView(submenu_view->GetScrollViewContainer());
// Set that the last selection target was the item which launches the
// submenu, as the empty item can never become a target.
SetPendingStateItem(submenu_item);
// Nest a context menu.
auto nested_menu_delegate_1 = std::make_unique<test::TestMenuDelegate>();
auto nested_menu_item_1 =
std::make_unique<MenuItemView>(nested_menu_delegate_1.get());
nested_menu_item_1->set_controller(menu_controller());
SubmenuView* const nested_menu_submenu = nested_menu_item_1->CreateSubmenu();
ShowSubmenu(nested_menu_submenu);
menu_host_for_submenu(nested_menu_submenu)
->SetContentsView(nested_menu_submenu->GetScrollViewContainer());
auto nested_controller_delegate_1 =
std::make_unique<TestMenuControllerDelegate>();
menu_controller()->AddNestedDelegate(nested_controller_delegate_1.get());
menu_controller()->Run(
owner(), nullptr, nested_menu_item_1.get(), gfx::Rect(150, 50, 100, 100),
MenuAnchorPosition::kTopLeft, ui::mojom::MenuSourceType::kNone,
MenuController::MenuType::kContextMenu);
// Press down outside of the context menu, and within the empty menu item.
// This should close the first context menu.
gfx::Point press_location = submenu_view->GetLocalBounds().CenterPoint();
const gfx::Point press_location_for_nested_menu =
View::ConvertPointFromScreen(
nested_menu_submenu,
View::ConvertPointToScreen(submenu_view, press_location));
ProcessMousePressed(
nested_menu_submenu,
ui::MouseEvent(ui::EventType::kMousePressed,
press_location_for_nested_menu,
press_location_for_nested_menu, ui::EventTimeForNow(),
ui::EF_RIGHT_MOUSE_BUTTON, 0));
EXPECT_EQ(nested_controller_delegate_1->on_menu_closed_called(), 1);
EXPECT_EQ(menu_controller_delegate(), current_controller_delegate());
// While the current state is the menu item which launched the sub menu,
// cause a drag in the empty menu item. This should not hide the menu.
SetState(submenu_item);
press_location.Offset(-5, 0);
ProcessMouseDragged(
submenu_view, ui::MouseEvent(ui::EventType::kMouseDragged, press_location,
press_location, ui::EventTimeForNow(),
ui::EF_RIGHT_MOUSE_BUTTON, 0));
ASSERT_EQ(menu_delegate()->will_hide_menu_count(), 0);
// Release the mouse in the empty menu item, triggering a context menu
// request.
ProcessMouseReleased(
submenu_view,
ui::MouseEvent(ui::EventType::kMouseReleased, press_location,
press_location, ui::EventTimeForNow(),
ui::EF_RIGHT_MOUSE_BUTTON, 0));
EXPECT_EQ(menu_delegate()->show_context_menu_count(), 1);
EXPECT_EQ(menu_delegate()->show_context_menu_source(), submenu_item);
// Nest a context menu.
auto nested_menu_delegate_2 = std::make_unique<test::TestMenuDelegate>();
auto nested_menu_item_2 =
std::make_unique<MenuItemView>(nested_menu_delegate_2.get());
nested_menu_item_2->set_controller(menu_controller());
auto nested_controller_delegate_2 =
std::make_unique<TestMenuControllerDelegate>();
menu_controller()->AddNestedDelegate(nested_controller_delegate_2.get());
menu_controller()->Run(
owner(), nullptr, nested_menu_item_2.get(), gfx::Rect(150, 50, 100, 100),
MenuAnchorPosition::kTopLeft, ui::mojom::MenuSourceType::kNone,
MenuController::MenuType::kContextMenu);
// The escape key should only close the nested menu. SelectByChar should not
// crash.
TestAsyncEscapeKey();
EXPECT_EQ(nested_controller_delegate_2->on_menu_closed_called(), 1);
EXPECT_EQ(menu_controller_delegate(), current_controller_delegate());
}
// Drag the mouse from an external view into a menu
// When the mouse leaves the menu while still in the process of dragging
// the menu item view highlight should turn off
TEST_F(MenuControllerTest, DragFromViewIntoMenuAndExit) {
auto drag_view = std::make_unique<View>();
drag_view->SetBounds(0, 500, 100, 100);
const gfx::Point press_location = drag_view->GetLocalBounds().CenterPoint();
ShowSubmenu();
SubmenuView* const submenu = menu_item()->GetSubmenu();
const MenuItemView* const first_item = submenu->GetMenuItemAt(0);
const gfx::Point drag_location = first_item->bounds().CenterPoint();
// Begin drag on an external view
drag_view->OnMousePressed(ui::MouseEvent(
ui::EventType::kMousePressed, press_location, press_location,
ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0));
// Drag into a menu item
ProcessMouseDragged(
submenu,
ui::MouseEvent(ui::EventType::kMouseDragged, drag_location, drag_location,
ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0));
EXPECT_TRUE(first_item->IsSelected());
// Drag out of the menu item
constexpr gfx::Point kReleaseLocation(200, 50);
ProcessMouseDragged(
submenu, ui::MouseEvent(ui::EventType::kMouseDragged, kReleaseLocation,
kReleaseLocation, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, 0));
EXPECT_FALSE(first_item->IsSelected());
// Complete drag with release
ProcessMouseReleased(
submenu, ui::MouseEvent(ui::EventType::kMouseReleased, kReleaseLocation,
kReleaseLocation, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, 0));
}
// Tests that |MenuHost::InitParams| are correctly forwarded to the created
// |aura::Window|.
TEST_F(MenuControllerTest, AuraWindowIsInitializedWithMenuHostInitParams) {
constexpr gfx::Rect kAnchorRect(1, 5, 2, 5);
ShowSubmenu(nullptr, [anchor_rect = kAnchorRect](auto& params) {
params.owned_window_anchor.anchor_rect = anchor_rect;
});
auto* property =
menu_item()->GetSubmenu()->GetWidget()->GetNativeWindow()->GetProperty(
aura::client::kOwnedWindowAnchor);
ASSERT_TRUE(property);
EXPECT_EQ(kAnchorRect, property->anchor_rect);
}
// Tests that |aura::Window| has the correct properties when a context menu is
// shown.
TEST_F(MenuControllerTest, ContextMenuInitializesAuraWindowWhenShown) {
// Checking that context menu properties are calculated correctly.
MenuBoundsOptions options = {.menu_anchor = MenuAnchorPosition::kTopLeft};
SetUpMenuControllerForCalculateBounds(options, menu_item());
menu_controller()->Run(owner(), nullptr, menu_item(), options.anchor_bounds,
options.menu_anchor, ui::mojom::MenuSourceType::kNone,
MenuController::MenuType::kContextMenu);
SubmenuView* const submenu = menu_item()->GetSubmenu();
const aura::Window* window = submenu->GetWidget()->GetNativeWindow();
const ui::OwnedWindowAnchor* anchor =
window->GetProperty(aura::client::kOwnedWindowAnchor);
EXPECT_TRUE(anchor);
EXPECT_EQ(ui::OwnedWindowAnchorPosition::kBottomLeft,
anchor->anchor_position);
EXPECT_EQ(ui::OwnedWindowAnchorGravity::kBottomRight, anchor->anchor_gravity);
EXPECT_EQ((ui::OwnedWindowConstraintAdjustment::kAdjustmentSlideX |
ui::OwnedWindowConstraintAdjustment::kAdjustmentFlipY |
ui::OwnedWindowConstraintAdjustment::kAdjustmentRezizeY),
anchor->constraint_adjustment);
EXPECT_EQ(CalculateExpectedMenuAnchorRect(menu_item()), anchor->anchor_rect);
// Checking that child menu properties are calculated correctly.
MenuItemView* const child_menu = submenu->GetMenuItemAt(0);
child_menu->CreateSubmenu();
ASSERT_NE(nullptr, child_menu->GetParentMenuItem());
options.menu_anchor = MenuAnchorPosition::kTopRight;
SetUpMenuControllerForCalculateBounds(options, child_menu);
menu_controller()->Run(owner(), nullptr, child_menu,
child_menu->GetBoundsInScreen(), options.menu_anchor);
ASSERT_NE(nullptr, child_menu->GetWidget());
window = child_menu->GetSubmenu()->GetWidget()->GetNativeWindow();
anchor = window->GetProperty(aura::client::kOwnedWindowAnchor);
EXPECT_TRUE(anchor);
EXPECT_EQ(ui::OwnedWindowAnchorPosition::kTopRight, anchor->anchor_position);
EXPECT_EQ(ui::OwnedWindowAnchorGravity::kBottomRight, anchor->anchor_gravity);
EXPECT_EQ((ui::OwnedWindowConstraintAdjustment::kAdjustmentSlideY |
ui::OwnedWindowConstraintAdjustment::kAdjustmentFlipX |
ui::OwnedWindowConstraintAdjustment::kAdjustmentResizeX |
ui::OwnedWindowConstraintAdjustment::kAdjustmentRezizeY),
anchor->constraint_adjustment);
EXPECT_EQ(CalculateExpectedMenuAnchorRect(child_menu), anchor->anchor_rect);
}
// Tests that |aura::Window| has the correct properties when a root or a child
// menu is shown.
TEST_F(MenuControllerTest, RootAndChildMenusInitializeAuraWindowWhenShown) {
// Checking that root menu properties are calculated correctly.
SubmenuView* const submenu = menu_item()->GetSubmenu();
MenuBoundsOptions options = {
.menu_size = GetPreferredSizeForSubmenu(*submenu),
.menu_anchor = MenuAnchorPosition::kTopLeft};
SetUpMenuControllerForCalculateBounds(options, menu_item());
menu_controller()->Run(owner(), nullptr, menu_item(), options.anchor_bounds,
options.menu_anchor);
const aura::Window* window = submenu->GetWidget()->GetNativeWindow();
const ui::OwnedWindowAnchor* anchor =
window->GetProperty(aura::client::kOwnedWindowAnchor);
EXPECT_TRUE(anchor);
EXPECT_EQ(ui::OwnedWindowAnchorPosition::kBottomLeft,
anchor->anchor_position);
EXPECT_EQ(ui::OwnedWindowAnchorGravity::kBottomRight, anchor->anchor_gravity);
EXPECT_EQ((ui::OwnedWindowConstraintAdjustment::kAdjustmentSlideX |
ui::OwnedWindowConstraintAdjustment::kAdjustmentFlipY |
ui::OwnedWindowConstraintAdjustment::kAdjustmentRezizeY),
anchor->constraint_adjustment);
EXPECT_EQ(CalculateExpectedMenuAnchorRect(menu_item()), anchor->anchor_rect);
// Checking that child menu properties are calculated correctly.
MenuItemView* const child_item = submenu->GetMenuItemAt(0);
child_item->AppendMenuItem(1, u"Child one");
SubmenuView* const child_submenu = child_item->GetSubmenu();
ASSERT_NE(nullptr, child_item->GetParentMenuItem());
options.menu_size = GetPreferredSizeForSubmenu(*child_submenu);
options.menu_anchor = MenuAnchorPosition::kTopRight;
SetUpMenuControllerForCalculateBounds(options, child_item);
menu_controller()->Run(owner(), nullptr, child_item,
child_item->GetBoundsInScreen(), options.menu_anchor);
ASSERT_NE(nullptr, child_item->GetWidget());
window = child_submenu->GetWidget()->GetNativeWindow();
anchor = window->GetProperty(aura::client::kOwnedWindowAnchor);
EXPECT_TRUE(anchor);
EXPECT_EQ(ui::OwnedWindowAnchorPosition::kTopRight, anchor->anchor_position);
EXPECT_EQ(ui::OwnedWindowAnchorGravity::kBottomRight, anchor->anchor_gravity);
EXPECT_EQ((ui::OwnedWindowConstraintAdjustment::kAdjustmentSlideY |
ui::OwnedWindowConstraintAdjustment::kAdjustmentFlipX |
ui::OwnedWindowConstraintAdjustment::kAdjustmentResizeX |
ui::OwnedWindowConstraintAdjustment::kAdjustmentRezizeY),
anchor->constraint_adjustment);
const auto anchor_rect = anchor->anchor_rect;
EXPECT_EQ(CalculateExpectedMenuAnchorRect(child_item), anchor->anchor_rect);
// Try to reposition the existing menu. Its anchor must change.
child_item->SetY(child_item->y() + 2);
menu_controller()->Run(owner(), nullptr, child_item,
child_item->GetBoundsInScreen(),
MenuAnchorPosition::kTopLeft);
MenuChildrenChanged(child_item);
EXPECT_EQ(CalculateExpectedMenuAnchorRect(child_item), anchor->anchor_rect);
// New anchor mustn't be the same as the old one.
EXPECT_NE(anchor->anchor_rect, anchor_rect);
}
// Test that if `SetTriggerActionWithNonIconChildViews` true that even with
// child views the click will be registered. Detect that the click was
// registered by checking if the TestMenuControllerDelegate received the signal
// that the menu should be closed.
TEST_F(MenuControllerTest, RegisterClickWithChildViews) {
DestroyMenuControllerOnMenuClosed(menu_controller_delegate());
ShowSubmenu();
SubmenuView* submenu = menu_item()->GetSubmenu();
MenuItemView* first_menu_item = submenu->GetMenuItemAt(0);
first_menu_item->AddChildView(std::make_unique<View>());
const gfx::Point press_location = first_menu_item->bounds().CenterPoint();
ProcessMousePressed(
submenu, ui::MouseEvent(ui::EventType::kMousePressed, press_location,
press_location, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, 0));
ProcessMouseReleased(
submenu, ui::MouseEvent(ui::EventType::kMouseReleased, press_location,
press_location, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, 0));
// No signal when there's a child view and
// SetTriggerActionWithNonIconChildViews is false.
EXPECT_EQ(menu_controller_delegate()->on_menu_closed_called(), 0);
first_menu_item->SetTriggerActionWithNonIconChildViews(true);
ProcessMousePressed(
submenu, ui::MouseEvent(ui::EventType::kMousePressed, press_location,
press_location, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, 0));
ProcessMouseReleased(
submenu, ui::MouseEvent(ui::EventType::kMouseReleased, press_location,
press_location, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, 0));
// We should receive a signal to close the menu when
// SetTriggerActionWithNonIconChildViews is true.
EXPECT_EQ(menu_controller_delegate()->on_menu_closed_called(), 1);
// Because the menu has been closed, destroy the menu controller delegate as
// well to avoid dangling pointers to the menu.
DestroyMenuControllerDelegate();
}
#endif // defined(USE_AURA)
// Tests that having the MenuController deleted during OnMousePressed does not
// cause a crash. ASAN bots should not detect use-after-free in
// MenuController.
TEST_F(MenuControllerTest, NoUseAfterFreeWhenMenuCanceledOnMousePress) {
DestroyMenuControllerOnMenuClosed(menu_controller_delegate());
// Creating own MenuItem for a minimal test environment.
auto item = std::make_unique<MenuItemView>(menu_delegate());
item->set_controller(menu_controller());
item->SetBounds(0, 0, 50, 50);
SubmenuView* const submenu = item->CreateSubmenu();
auto* const canceling_view =
submenu->AddChildView(std::make_unique<CancelMenuOnMousePressView>(
menu_controller()->AsWeakPtr()));
canceling_view->SetBoundsRect(item->GetLocalBounds());
menu_controller()->Run(owner(), nullptr, item.get(), item->bounds(),
MenuAnchorPosition::kTopLeft);
ShowSubmenu(submenu);
// Simulate a mouse press in the middle of the |closing_widget|.
const gfx::Point location = canceling_view->bounds().CenterPoint();
EXPECT_TRUE(ProcessMousePressed(
submenu,
ui::MouseEvent(ui::EventType::kMousePressed, location, location,
ui::EventTimeForNow(), ui::EF_LEFT_MOUSE_BUTTON, 0)));
// Close to remove observers before test TearDown.
submenu->Close();
}
TEST_F(MenuControllerTest, SetSelectionIndices_MenuItemsOnly) {
SubmenuView* const submenu = menu_item()->GetSubmenu();
MenuItemView* const item1 = submenu->GetMenuItemAt(0);
MenuItemView* const item2 = submenu->GetMenuItemAt(1);
MenuItemView* const item3 = submenu->GetMenuItemAt(2);
MenuItemView* const item4 = submenu->GetMenuItemAt(3);
OpenMenu(menu_item());
ui::AXNodeData data;
item1->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(1, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(4, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
item2->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(2, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(4, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
item3->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(3, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(4, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
item4->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(4, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(4, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
}
TEST_F(MenuControllerTest,
SetSelectionIndices_MenuItemsOnly_SkipHiddenAndDisabled) {
SubmenuView* const submenu = menu_item()->GetSubmenu();
MenuItemView* const item1 = submenu->GetMenuItemAt(0);
item1->SetEnabled(false);
const MenuItemView* const item2 = submenu->GetMenuItemAt(1);
MenuItemView* const item3 = submenu->GetMenuItemAt(2);
item3->SetVisible(false);
const MenuItemView* const item4 = submenu->GetMenuItemAt(3);
OpenMenu(menu_item());
ui::AXNodeData data;
item2->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(1, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(2, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
item4->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(2, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(2, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
}
TEST_F(MenuControllerTest, SetSelectionIndices_Buttons) {
AddButtonMenuItems(/*single_child=*/false);
SubmenuView* const submenu = menu_item()->GetSubmenu();
const MenuItemView* const item1 = submenu->GetMenuItemAt(0);
const MenuItemView* const item2 = submenu->GetMenuItemAt(1);
const MenuItemView* const item3 = submenu->GetMenuItemAt(2);
const MenuItemView* const item4 = submenu->GetMenuItemAt(3);
const MenuItemView* const item5 = submenu->GetMenuItemAt(4);
GET_CHILD_BUTTON(button1, item5, 0);
GET_CHILD_BUTTON(button2, item5, 1);
GET_CHILD_BUTTON(button3, item5, 2);
OpenMenu(menu_item());
ui::AXNodeData data;
item1->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(1, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(7, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
item2->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(2, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(7, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
item3->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(3, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(7, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
item4->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(4, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(7, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
button1->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(5, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(7, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
button2->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(6, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(7, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
button3->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(7, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(7, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
}
TEST_F(MenuControllerTest, SetSelectionIndices_Buttons_SkipHiddenAndDisabled) {
AddButtonMenuItems(/*single_child=*/false);
SubmenuView* const submenu = menu_item()->GetSubmenu();
const MenuItemView* const item1 = submenu->GetMenuItemAt(0);
const MenuItemView* const item2 = submenu->GetMenuItemAt(1);
const MenuItemView* const item3 = submenu->GetMenuItemAt(2);
const MenuItemView* const item4 = submenu->GetMenuItemAt(3);
const MenuItemView* const item5 = submenu->GetMenuItemAt(4);
GET_CHILD_BUTTON(button1, item5, 0);
GET_CHILD_BUTTON(button2, item5, 1);
GET_CHILD_BUTTON(button3, item5, 2);
button1->SetEnabled(false);
button2->SetVisible(false);
OpenMenu(menu_item());
ui::AXNodeData data;
item1->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(1, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(5, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
item2->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(2, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(5, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
item3->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(3, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(5, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
item4->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(4, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(5, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
button3->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(5, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(5, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
}
TEST_F(MenuControllerTest, SetSelectionIndices_NestedButtons) {
SubmenuView* const submenu = menu_item()->GetSubmenu();
const MenuItemView* const item1 = submenu->GetMenuItemAt(0);
const MenuItemView* const item2 = submenu->GetMenuItemAt(1);
const MenuItemView* const item3 = submenu->GetMenuItemAt(2);
MenuItemView* const item4 = submenu->GetMenuItemAt(3);
// This simulates how buttons are nested in views in the main app menu.
auto* const container_view = item4->AddChildView(std::make_unique<View>());
container_view->GetViewAccessibility().SetRole(ax::mojom::Role::kMenu);
// There's usually a label before the traversable elements.
container_view->AddChildView(std::make_unique<Label>());
// Add two focusable buttons (buttons in menus are always focusable).
auto* const button1 =
container_view->AddChildView(std::make_unique<LabelButton>());
button1->SetFocusBehavior(View::FocusBehavior::ALWAYS);
button1->GetViewAccessibility().SetRole(ax::mojom::Role::kMenuItem);
auto* const button2 =
container_view->AddChildView(std::make_unique<LabelButton>());
button2->GetViewAccessibility().SetRole(ax::mojom::Role::kMenuItem);
button2->SetFocusBehavior(View::FocusBehavior::ALWAYS);
OpenMenu(menu_item());
ui::AXNodeData data;
item1->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(1, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(5, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
item2->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(2, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(5, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
item3->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(3, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(5, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
data = ui::AXNodeData();
button1->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(4, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(5, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
data = ui::AXNodeData();
button2->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(5, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(5, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
}
TEST_F(MenuControllerTest, AccessibleProperties) {
SubmenuView* const submenu = menu_item()->GetSubmenu();
MenuScrollViewContainer* scroll_view_container =
submenu->GetScrollViewContainer();
ui::AXNodeData data;
scroll_view_container->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(data.role, ax::mojom::Role::kMenuBar);
}
TEST_F(MenuControllerTest, SetSelectionIndices_ChildrenChanged) {
AddButtonMenuItems(/*single_child=*/false);
SubmenuView* const submenu = menu_item()->GetSubmenu();
MenuItemView* const item1 = submenu->GetMenuItemAt(0);
MenuItemView* const item2 = submenu->GetMenuItemAt(1);
const MenuItemView* const item3 = submenu->GetMenuItemAt(2);
const MenuItemView* const item4 = submenu->GetMenuItemAt(3);
const MenuItemView* const item5 = submenu->GetMenuItemAt(4);
GET_CHILD_BUTTON(button1, item5, 0);
GET_CHILD_BUTTON(button2, item5, 1);
GET_CHILD_BUTTON(button3, item5, 2);
OpenMenu(menu_item());
const auto expect_coordinates = [](const View* v, std::optional<int> pos,
std::optional<int> size) {
ui::AXNodeData data;
v->GetViewAccessibility().GetAccessibleNodeData(&data);
const auto check_attribute = [&](const auto& expected, auto attribute) {
EXPECT_EQ(expected.has_value(), data.HasIntAttribute(attribute));
if (expected.has_value()) {
EXPECT_EQ(expected.value(), data.GetIntAttribute(attribute));
}
};
check_attribute(pos, ax::mojom::IntAttribute::kPosInSet);
check_attribute(size, ax::mojom::IntAttribute::kSetSize);
};
expect_coordinates(item1, 1, 7);
expect_coordinates(item2, 2, 7);
expect_coordinates(item3, 3, 7);
expect_coordinates(item4, 4, 7);
expect_coordinates(button1, 5, 7);
expect_coordinates(button2, 6, 7);
expect_coordinates(button3, 7, 7);
// Simulate a menu model update.
item1->SetEnabled(false);
button1->SetEnabled(false);
const MenuItemView* const item6 = menu_item()->AppendMenuItem(6, u"Six");
menu_item()->RemoveMenuItem(item2);
MenuChildrenChanged(menu_item());
// Verify that disabled menu items no longer have PosInSet or SetSize.
expect_coordinates(item1, std::nullopt, std::nullopt);
expect_coordinates(button1, std::nullopt, std::nullopt);
expect_coordinates(item3, 1, 5);
expect_coordinates(item4, 2, 5);
expect_coordinates(button2, 3, 5);
expect_coordinates(button3, 4, 5);
expect_coordinates(item6, 5, 5);
}
// Tests that a menu opened asynchronously, will notify its
// MenuControllerDelegate when accessibility performs a do default action.
TEST_F(MenuControllerTest, AccessibilityDoDefaultCallsAccept) {
views::test::DisableMenuClosureAnimations();
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
EXPECT_EQ(0, menu_controller_delegate()->on_menu_closed_called());
MenuItemView* const accepted = menu_item()->GetSubmenu()->GetMenuItemAt(0);
ui::AXActionData data;
data.action = ax::mojom::Action::kDoDefault;
accepted->HandleAccessibleAction(data);
views::test::WaitForMenuClosureAnimation();
EXPECT_EQ(1, menu_controller_delegate()->on_menu_closed_called());
EXPECT_EQ(accepted, menu_controller_delegate()->on_menu_closed_menu());
EXPECT_EQ(internal::MenuControllerDelegate::NOTIFY_DELEGATE,
menu_controller_delegate()->on_menu_closed_notify_type());
}
// Test that the kSelectedChildrenChanged event is emitted on
// the root menu item when the selected menu item changes.
TEST_F(MenuControllerTest, AccessibilityEmitsSelectChildrenChanged) {
const test::AXEventCounter ax_counter(views::AXUpdateNotifier::Get());
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
EXPECT_EQ(ax_counter.GetCount(ax::mojom::Event::kSelectedChildrenChanged), 0);
// Arrow down to select an item checking the event has been emitted.
DispatchKey(ui::VKEY_DOWN);
EXPECT_EQ(ax_counter.GetCount(ax::mojom::Event::kSelectedChildrenChanged), 1);
DispatchKey(ui::VKEY_DOWN);
EXPECT_EQ(ax_counter.GetCount(ax::mojom::Event::kSelectedChildrenChanged), 2);
}
TEST_F(MenuControllerTest, AccessibilityEmitsMenuOpenedClosedEvents) {
const test::AXEventCounter ax_counter(views::AXUpdateNotifier::Get());
EXPECT_EQ(0, ax_counter.GetCount(ax::mojom::Event::kMenuStart));
EXPECT_EQ(0, ax_counter.GetCount(ax::mojom::Event::kMenuEnd));
EXPECT_EQ(0, ax_counter.GetCount(ax::mojom::Event::kMenuPopupStart));
EXPECT_EQ(0, ax_counter.GetCount(ax::mojom::Event::kMenuPopupEnd));
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
EXPECT_EQ(1, ax_counter.GetCount(ax::mojom::Event::kMenuStart));
EXPECT_EQ(0, ax_counter.GetCount(ax::mojom::Event::kMenuEnd));
EXPECT_EQ(1, ax_counter.GetCount(ax::mojom::Event::kMenuPopupStart));
EXPECT_EQ(0, ax_counter.GetCount(ax::mojom::Event::kMenuPopupEnd));
menu_controller()->Cancel(MenuController::ExitType::kAll);
EXPECT_EQ(1, ax_counter.GetCount(ax::mojom::Event::kMenuStart));
EXPECT_EQ(1, ax_counter.GetCount(ax::mojom::Event::kMenuEnd));
EXPECT_EQ(1, ax_counter.GetCount(ax::mojom::Event::kMenuPopupStart));
EXPECT_EQ(1, ax_counter.GetCount(ax::mojom::Event::kMenuPopupEnd));
}
// Test that in accessibility mode disabled menu items are taken into account
// during items indices assignment.
TEST_F(MenuControllerTest, AccessibilityDisabledItemsIndices) {
const ::ui::ScopedAXModeSetter ax_mode_setter(ui::AXMode::kNativeAPIs);
SubmenuView* const submenu = menu_item()->GetSubmenu();
const MenuItemView* const item1 = submenu->GetMenuItemAt(0);
MenuItemView* const item2 = submenu->GetMenuItemAt(1);
const MenuItemView* const item3 = submenu->GetMenuItemAt(2);
const MenuItemView* const item4 = submenu->GetMenuItemAt(3);
item2->SetEnabled(false);
OpenMenu(menu_item());
ui::AXNodeData data;
item1->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(1, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(4, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
item2->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(2, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(4, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
item3->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(3, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(4, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
item4->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(4, data.GetIntAttribute(ax::mojom::IntAttribute::kPosInSet));
EXPECT_EQ(4, data.GetIntAttribute(ax::mojom::IntAttribute::kSetSize));
}
#if BUILDFLAG(IS_MAC)
// This test exercises a Mac-specific behavior, by which hotkeys using
// modifiers cause menus to close and the hotkeys to be handled by the browser
// window. This specific test case tries using cmd-ctrl-f, which normally
// means "Fullscreen".
TEST_F(MenuControllerTest, BrowserHotkeysCancelMenusAndAreRedispatched) {
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
int options = ui::EF_COMMAND_DOWN;
ui::KeyEvent press_cmd(ui::EventType::kKeyPressed, ui::VKEY_COMMAND, options);
menu_controller()->OnWillDispatchKeyEvent(&press_cmd);
EXPECT_TRUE(showing()); // ensure the command press itself doesn't cancel
options |= ui::EF_CONTROL_DOWN;
ui::KeyEvent press_ctrl(ui::EventType::kKeyPressed, ui::VKEY_CONTROL,
options);
menu_controller()->OnWillDispatchKeyEvent(&press_ctrl);
EXPECT_TRUE(showing());
ui::KeyEvent press_f(ui::EventType::kKeyPressed, ui::VKEY_F, options);
menu_controller()->OnWillDispatchKeyEvent(&press_f);
EXPECT_FALSE(showing());
EXPECT_FALSE(press_f.handled());
EXPECT_FALSE(press_f.stopped_propagation());
}
#endif
TEST_F(MenuControllerTest, SubmenuOpenByKey) {
// Create a submenu.
MenuItemView* const child_menu = menu_item()->GetSubmenu()->GetMenuItemAt(0);
const SubmenuView* const submenu = child_menu->CreateSubmenu();
child_menu->AppendMenuItem(5, u"Five");
child_menu->AppendMenuItem(6, u"Six");
// Open the menu and select the menu item that has a submenu.
OpenMenu(menu_item());
SetState(child_menu);
EXPECT_EQ(1, pending_state_item()->GetCommand());
EXPECT_EQ(nullptr, submenu->host());
// Dispatch a key to open the submenu.
DispatchKey(ui::VKEY_RIGHT);
EXPECT_EQ(5, pending_state_item()->GetCommand());
EXPECT_NE(nullptr, submenu->host());
}
class ExecuteCommandWithoutClosingMenuTest : public MenuControllerTest {
public:
void SetUp() override {
MenuControllerTest::SetUp();
views::test::DisableMenuClosureAnimations();
menu_controller()->Run(owner(), nullptr, menu_item(), gfx::Rect(),
MenuAnchorPosition::kTopLeft);
ShowSubmenu();
menu_delegate()->set_should_execute_command_without_closing_menu(true);
}
};
TEST_F(ExecuteCommandWithoutClosingMenuTest, OnClick) {
EXPECT_EQ(0, menu_controller_delegate()->on_menu_closed_called());
SubmenuView* const submenu = menu_item()->GetSubmenu();
const MenuItemView* const menu_item_view = submenu->GetMenuItemAt(0);
const gfx::Point press_location = menu_item_view->bounds().CenterPoint();
ProcessMousePressed(
submenu, ui::MouseEvent(ui::EventType::kMousePressed, press_location,
press_location, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, 0));
ProcessMouseReleased(
submenu, ui::MouseEvent(ui::EventType::kMouseReleased, press_location,
press_location, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, 0));
EXPECT_EQ(0, menu_controller_delegate()->on_menu_closed_called());
EXPECT_TRUE(showing());
EXPECT_EQ(menu_delegate()->execute_command_id(),
menu_item_view->GetCommand());
}
TEST_F(ExecuteCommandWithoutClosingMenuTest, OnTap) {
EXPECT_EQ(0, menu_controller_delegate()->on_menu_closed_called());
SubmenuView* const submenu = menu_item()->GetSubmenu();
const MenuItemView* const menu_item_view = submenu->GetMenuItemAt(0);
const gfx::Point tap_location = menu_item_view->bounds().CenterPoint();
ProcessGestureEvent(
submenu, ui::GestureEvent(
tap_location.x(), tap_location.y(), 0, ui::EventTimeForNow(),
ui::GestureEventDetails(ui::EventType::kGestureTap)));
EXPECT_EQ(0, menu_controller_delegate()->on_menu_closed_called());
EXPECT_TRUE(showing());
EXPECT_EQ(menu_delegate()->execute_command_id(),
menu_item_view->GetCommand());
}
TEST_F(ExecuteCommandWithoutClosingMenuTest, OnReturnKey) {
EXPECT_EQ(0, menu_controller_delegate()->on_menu_closed_called());
DispatchKey(ui::VKEY_DOWN);
DispatchKey(ui::VKEY_RETURN);
EXPECT_EQ(0, menu_controller_delegate()->on_menu_closed_called());
EXPECT_TRUE(showing());
EXPECT_EQ(menu_delegate()->execute_command_id(),
menu_item()->GetSubmenu()->GetMenuItemAt(0)->GetCommand());
}
// Simple test to ensure child menu open direction is correctly set and
// retrieved.
TEST_F(MenuControllerTest, ChildMenuOpenDirectionStateUpdatesCorrectly) {
// Before any open directions have been set, the leading direction should
// be used as the default for any depth value.
EXPECT_EQ(MenuController::MenuOpenDirection::kLeading,
GetChildMenuOpenDirectionAtDepth(0));
EXPECT_EQ(MenuController::MenuOpenDirection::kLeading,
GetChildMenuOpenDirectionAtDepth(1));
EXPECT_EQ(MenuController::MenuOpenDirection::kLeading,
GetChildMenuOpenDirectionAtDepth(10));
// Set alternating open directions, this should be correctly reflected in
// subsequent open direction queries.
SetChildMenuOpenDirectionAtDepth(1,
MenuController::MenuOpenDirection::kLeading);
SetChildMenuOpenDirectionAtDepth(
2, MenuController::MenuOpenDirection::kTrailing);
SetChildMenuOpenDirectionAtDepth(3,
MenuController::MenuOpenDirection::kLeading);
SetChildMenuOpenDirectionAtDepth(
4, MenuController::MenuOpenDirection::kTrailing);
EXPECT_EQ(MenuController::MenuOpenDirection::kLeading,
GetChildMenuOpenDirectionAtDepth(0));
EXPECT_EQ(MenuController::MenuOpenDirection::kLeading,
GetChildMenuOpenDirectionAtDepth(1));
EXPECT_EQ(MenuController::MenuOpenDirection::kTrailing,
GetChildMenuOpenDirectionAtDepth(2));
EXPECT_EQ(MenuController::MenuOpenDirection::kLeading,
GetChildMenuOpenDirectionAtDepth(3));
EXPECT_EQ(MenuController::MenuOpenDirection::kTrailing,
GetChildMenuOpenDirectionAtDepth(4));
EXPECT_EQ(MenuController::MenuOpenDirection::kLeading,
GetChildMenuOpenDirectionAtDepth(10));
}
TEST_F(MenuControllerTest, MenuHostHasCorrectZOrderLevel) {
ShowSubmenu();
SubmenuView* const submenu = menu_item()->GetSubmenu();
MenuHost* const host = menu_host_for_submenu(submenu);
// Ensure that the menu host has the correct z order level.
EXPECT_EQ(ui::ZOrderLevel::kFloatingWindow, host->GetZOrderLevel());
}
// Tests that updating an empty menu's children, while the "empty item"
// placeholder is selected correctly transfers selection to the parent.
TEST_F(MenuControllerTest, RemoveEmptyMenuMenuItemWhileSelected) {
views::MenuItemView* const root_menu = menu_item();
OpenMenu(root_menu);
// Remove all items.
root_menu->RemoveAllMenuItems();
root_menu->ChildrenChanged();
SubmenuView* const submenu = root_menu->GetSubmenu();
ASSERT_TRUE(submenu);
// The menu should only have an empty-menu menu item as a placeholder.
ASSERT_EQ(1u, submenu->children().size());
auto* const empty_child =
AsViewClass<EmptyMenuMenuItem>(submenu->children()[0]);
ASSERT_NE(empty_child, nullptr);
menu_controller()->SelectItemAndOpenSubmenu(empty_child);
ASSERT_TRUE(empty_child->IsSelected());
// Adding a new item will remove the empty-menu menu item.
// Selection should be transferred gracefully.
views::MenuItemView* const item = root_menu->AppendMenuItem(1, u"item 1");
item->SetVisible(true);
root_menu->ChildrenChanged();
// The parent should be selected and the remaining view should be the added
// menu item.
EXPECT_TRUE(root_menu->IsSelected());
ASSERT_EQ(1u, submenu->children().size());
EXPECT_EQ(item, submenu->children()[0]);
}
#if BUILDFLAG(IS_WIN)
// The following tests are only relevant on platforms that select the first
// menu item when a menu is opened via keyboard input.
TEST_F(MenuControllerTest, FirstMenuItemSelectedWhenOpenedFromKeyboard) {
// Use existing menu items from the test setup.
MenuItemView* root = menu_item();
MenuItemView* item1 = root->GetSubmenu()->GetMenuItemAt(0);
MenuItemView* item2 = root->GetSubmenu()->GetMenuItemAt(1);
MenuItemView* item3 = root->GetSubmenu()->GetMenuItemAt(2);
// Open the menu via keyboard input.
menu_controller()->Run(owner(), /*button_controller=*/nullptr, root,
gfx::Rect(), MenuAnchorPosition::kTopLeft,
ui::mojom::MenuSourceType::kKeyboard,
/*menu_type=*/MenuController::MenuType::kNormal,
/*is_nested_drag=*/false);
EXPECT_TRUE(item1->IsSelected());
EXPECT_FALSE(item2->IsSelected());
EXPECT_FALSE(item3->IsSelected());
}
TEST_F(MenuControllerTest, NoItemSelectedWhenOpenedFromMouse) {
// Use existing menu items from the test setup.
MenuItemView* root = menu_item();
MenuItemView* item1 = root->GetSubmenu()->GetMenuItemAt(0);
MenuItemView* item2 = root->GetSubmenu()->GetMenuItemAt(1);
MenuItemView* item3 = root->GetSubmenu()->GetMenuItemAt(2);
// Open the menu via mouse click.
menu_controller()->Run(owner(), /*button_controller=*/nullptr, root,
gfx::Rect(), MenuAnchorPosition::kTopLeft,
ui::mojom::MenuSourceType::kMouse,
/*menu_type=*/MenuController::MenuType::kNormal,
/*is_nested_drag=*/false);
EXPECT_FALSE(item1->IsSelected());
EXPECT_FALSE(item2->IsSelected());
EXPECT_FALSE(item3->IsSelected());
}
TEST_F(MenuControllerTest,
FirstMenuItemButtonHotTrackedWhenOpenedFromKeyboard) {
// Set up a menu with one button in the first menu item.
SubmenuView* const submenu = menu_item()->GetSubmenu();
MenuItemView* first_item = submenu->GetMenuItemAt(0);
first_item->SetTitle(std::u16string());
const View* const buttons_view = submenu->children()[0];
first_item
->AddChildView(
std::make_unique<LabelButton>(Button::PressedCallback(), u"Label"))
// This is an in-menu button. Hence it must be always focusable.
->SetFocusBehavior(View::FocusBehavior::ALWAYS);
// Open the menu via keyboard input.
menu_controller()->Run(owner(), /*button_controller=*/nullptr, menu_item(),
gfx::Rect(), MenuAnchorPosition::kTopLeft,
ui::mojom::MenuSourceType::kKeyboard,
/*menu_type=*/MenuController::MenuType::kNormal,
/*is_nested_drag=*/false);
EXPECT_TRUE(first_item->IsSelected());
// Access the button inside the first menu item.
GET_CHILD_BUTTON(button1, buttons_view, 0);
EXPECT_TRUE(button1->IsHotTracked());
}
TEST_F(MenuControllerTest,
FirstMenuItemButtonNotHotTrackedWhenOpenedFromMouse) {
// Set up a menu with one button in the first menu item.
SubmenuView* const submenu = menu_item()->GetSubmenu();
MenuItemView* first_item = submenu->GetMenuItemAt(0);
first_item->SetTitle(std::u16string());
const View* const buttons_view = submenu->children()[0];
first_item
->AddChildView(
std::make_unique<LabelButton>(Button::PressedCallback(), u"Label"))
// This is an in-menu button. Hence it must be always focusable.
->SetFocusBehavior(View::FocusBehavior::ALWAYS);
// Open the menu via mouse input.
menu_controller()->Run(owner(), /*button_controller=*/nullptr, menu_item(),
gfx::Rect(), MenuAnchorPosition::kTopLeft,
ui::mojom::MenuSourceType::kMouse,
/*menu_type=*/MenuController::MenuType::kNormal,
/*is_nested_drag=*/false);
EXPECT_FALSE(first_item->IsSelected());
// Access the button inside the first menu item.
GET_CHILD_BUTTON(button1, buttons_view, 0);
EXPECT_FALSE(button1->IsHotTracked());
}
#endif // BUILDFLAG(IS_WIN)
} // namespace views
|