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 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <algorithm>
#include <memory>
#include <optional>
#include <set>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/test/gtest_util.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/accessibility/ax_node_data.h"
#include "ui/base/dragdrop/mojom/drag_drop_types.mojom.h"
#include "ui/base/hit_test.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/base/mojom/window_show_state.mojom.h"
#include "ui/color/color_id.h"
#include "ui/color/color_provider.h"
#include "ui/color/color_provider_key.h"
#include "ui/color/color_provider_manager.h"
#include "ui/color/color_recipe.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animation_observer.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/compositor/test/draw_waiter_for_test.h"
#include "ui/events/event_observer.h"
#include "ui/events/event_utils.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/native_theme/native_theme.h"
#include "ui/native_theme/test_native_theme.h"
#include "ui/views/bubble/bubble_dialog_delegate_view.h"
#include "ui/views/buildflags.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/event_monitor.h"
#include "ui/views/layout/fill_layout.h"
#include "ui/views/style/platform_style.h"
#include "ui/views/test/mock_drag_controller.h"
#include "ui/views/test/mock_native_widget.h"
#include "ui/views/test/native_widget_factory.h"
#include "ui/views/test/test_views.h"
#include "ui/views/test/test_widget_observer.h"
#include "ui/views/test/views_test_utils.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/view_test_api.h"
#include "ui/views/views_test_suite.h"
#include "ui/views/widget/native_widget_delegate.h"
#include "ui/views/widget/native_widget_private.h"
#include "ui/views/widget/root_view.h"
#include "ui/views/widget/unique_widget_ptr.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/views/widget/widget_deletion_observer.h"
#include "ui/views/widget/widget_interactive_uitest_utils.h"
#include "ui/views/widget/widget_removals_observer.h"
#include "ui/views/widget/widget_utils.h"
#include "ui/views/window/dialog_delegate.h"
#include "ui/views/window/native_frame_view.h"
#if defined(USE_AURA)
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/base/view_prop.h"
#include "ui/views/test/test_platform_native_widget.h"
#include "ui/views/widget/native_widget_aura.h"
#include "ui/wm/core/base_focus_rules.h"
#include "ui/wm/core/focus_controller.h"
#include "ui/wm/core/shadow_controller.h"
#include "ui/wm/core/shadow_controller_delegate.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "ui/base/win/window_event_target.h"
#include "ui/views/win/hwnd_util.h"
#endif
#if BUILDFLAG(IS_MAC)
#include "base/mac/mac_util.h"
#endif
#if BUILDFLAG(ENABLE_DESKTOP_AURA)
#include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
#endif
namespace views::test {
namespace {
using ::testing::_;
using ::testing::IsEmpty;
using ::testing::Not;
// TODO(tdanderson): This utility function is used in different unittest
// files. Move to a common location to avoid
// repeated code.
gfx::Point ConvertPointFromWidgetToView(View* view, const gfx::Point& p) {
gfx::Point tmp(p);
View::ConvertPointToTarget(view->GetWidget()->GetRootView(), view, &tmp);
return tmp;
}
std::unique_ptr<ui::test::EventGenerator> CreateEventGenerator(
gfx::NativeWindow root_window,
gfx::NativeWindow target_window) {
auto generator =
std::make_unique<ui::test::EventGenerator>(root_window, target_window);
return generator;
}
// Convenience to make constructing a GestureEvent simpler.
ui::GestureEvent CreateTestGestureEvent(ui::EventType type, int x, int y) {
return ui::GestureEvent(x, y, 0, base::TimeTicks(),
ui::GestureEventDetails(type));
}
ui::GestureEvent CreateTestGestureEvent(const ui::GestureEventDetails& details,
int x,
int y) {
return ui::GestureEvent(x, y, 0, base::TimeTicks(), details);
}
class TestWidgetRemovalsObserver : public WidgetRemovalsObserver {
public:
TestWidgetRemovalsObserver() = default;
TestWidgetRemovalsObserver(const TestWidgetRemovalsObserver&) = delete;
TestWidgetRemovalsObserver& operator=(const TestWidgetRemovalsObserver&) =
delete;
~TestWidgetRemovalsObserver() override = default;
void OnWillRemoveView(Widget* widget, View* view) override {
removed_views_.insert(view);
}
bool DidRemoveView(View* view) {
return removed_views_.find(view) != removed_views_.end();
}
private:
std::set<raw_ptr<View, SetExperimental>> removed_views_;
};
} // namespace
class WidgetTestBubbleDialogDelegateView : public BubbleDialogDelegateView {
METADATA_HEADER(WidgetTestBubbleDialogDelegateView, BubbleDialogDelegateView)
public:
explicit WidgetTestBubbleDialogDelegateView(View* anchor)
: BubbleDialogDelegateView(anchor, BubbleBorder::NONE) {}
~WidgetTestBubbleDialogDelegateView() override = default;
bool ShouldShowCloseButton() const override {
reset_controls_called_ = true;
return true;
}
mutable bool reset_controls_called_ = false;
};
BEGIN_METADATA(WidgetTestBubbleDialogDelegateView)
END_METADATA
// A view that keeps track of the events it receives, and consumes all scroll
// gesture events and ui::EventType::kScroll events.
class ScrollableEventCountView : public EventCountView {
METADATA_HEADER(ScrollableEventCountView, EventCountView)
public:
ScrollableEventCountView() = default;
ScrollableEventCountView(const ScrollableEventCountView&) = delete;
ScrollableEventCountView& operator=(const ScrollableEventCountView&) = delete;
~ScrollableEventCountView() override = default;
private:
// Overridden from ui::EventHandler:
void OnGestureEvent(ui::GestureEvent* event) override {
EventCountView::OnGestureEvent(event);
switch (event->type()) {
case ui::EventType::kGestureScrollBegin:
case ui::EventType::kGestureScrollUpdate:
case ui::EventType::kGestureScrollEnd:
case ui::EventType::kScrollFlingStart:
event->SetHandled();
break;
default:
break;
}
}
void OnScrollEvent(ui::ScrollEvent* event) override {
EventCountView::OnScrollEvent(event);
if (event->type() == ui::EventType::kScroll) {
event->SetHandled();
}
}
};
BEGIN_METADATA(ScrollableEventCountView)
END_METADATA
// A view that implements GetMinimumSize.
class MinimumSizeFrameView : public NativeFrameView {
METADATA_HEADER(MinimumSizeFrameView, NativeFrameView)
public:
explicit MinimumSizeFrameView(Widget* frame) : NativeFrameView(frame) {}
MinimumSizeFrameView(const MinimumSizeFrameView&) = delete;
MinimumSizeFrameView& operator=(const MinimumSizeFrameView&) = delete;
~MinimumSizeFrameView() override = default;
private:
// Overridden from View:
gfx::Size GetMinimumSize() const override { return gfx::Size(300, 400); }
};
BEGIN_METADATA(MinimumSizeFrameView)
END_METADATA
// An event handler that simply keeps a count of the different types of events
// it receives.
class EventCountHandler : public ui::EventHandler {
public:
EventCountHandler() = default;
EventCountHandler(const EventCountHandler&) = delete;
EventCountHandler& operator=(const EventCountHandler&) = delete;
~EventCountHandler() override = default;
int GetEventCount(ui::EventType type) { return event_count_[type]; }
void ResetCounts() { event_count_.clear(); }
protected:
// Overridden from ui::EventHandler:
void OnEvent(ui::Event* event) override {
RecordEvent(*event);
ui::EventHandler::OnEvent(event);
}
private:
void RecordEvent(const ui::Event& event) { ++event_count_[event.type()]; }
std::map<ui::EventType, int> event_count_;
};
TEST_F(WidgetTest, WidgetInitParams) {
// Widgets are not transparent by default.
Widget::InitParams init1(Widget::InitParams::CLIENT_OWNS_WIDGET);
EXPECT_EQ(Widget::InitParams::WindowOpacity::kInferred, init1.opacity);
}
// Tests that the internal name is propagated through widget initialization to
// the native widget and back.
class WidgetWithCustomParamsTest : public WidgetTest {
public:
using InitFunction = base::RepeatingCallback<void(Widget::InitParams*)>;
void SetInitFunction(const InitFunction& init) { init_ = std::move(init); }
Widget::InitParams CreateParams(Widget::InitParams::Ownership ownership,
Widget::InitParams::Type type) override {
Widget::InitParams params = WidgetTest::CreateParams(ownership, type);
DCHECK(init_) << "If you don't need an init function, use WidgetTest";
init_.Run(¶ms);
return params;
}
private:
InitFunction init_;
};
TEST_F(WidgetWithCustomParamsTest, NamePropagatedFromParams) {
SetInitFunction(base::BindLambdaForTesting(
[](Widget::InitParams* params) { params->name = "MyWidget"; }));
std::unique_ptr<Widget> widget =
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
EXPECT_EQ("MyWidget", widget->native_widget_private()->GetName());
EXPECT_EQ("MyWidget", widget->GetName());
}
TEST_F(WidgetWithCustomParamsTest, NamePropagatedFromDelegate) {
WidgetDelegate delegate;
delegate.set_internal_name("Foobar");
SetInitFunction(base::BindLambdaForTesting(
[&](Widget::InitParams* params) { params->delegate = &delegate; }));
std::unique_ptr<Widget> widget =
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
EXPECT_EQ(delegate.internal_name(),
widget->native_widget_private()->GetName());
EXPECT_EQ(delegate.internal_name(), widget->GetName());
}
// Test that Widget::InitParams::autosize allows widget to
// automatically resize when content view size changes.
TEST_F(WidgetWithCustomParamsTest, Autosize) {
SetInitFunction(base::BindLambdaForTesting(
[](Widget::InitParams* params) { params->autosize = true; }));
std::unique_ptr<Widget> widget =
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
auto* view = widget->SetContentsView(std::make_unique<views::View>());
widget->Show();
constexpr gfx::Size kInitialSize(100, 100);
view->SetPreferredSize(kInitialSize);
// RunScheduledLayout() is needed due to widget auto-resize.
views::test::RunScheduledLayout(widget.get());
const gfx::Size starting_size = widget->GetWindowBoundsInScreen().size();
constexpr gfx::Size kDelta(50, 50);
view->SetPreferredSize(kInitialSize + kDelta);
// RunScheduledLayout() is needed due to widget auto-resize.
views::test::RunScheduledLayout(widget.get());
const gfx::Size ending_size = widget->GetWindowBoundsInScreen().size();
EXPECT_EQ(ending_size, starting_size + kDelta);
}
namespace {
class ViewWithClassName : public View {
METADATA_HEADER(ViewWithClassName, View)
};
BEGIN_METADATA(ViewWithClassName)
END_METADATA
} // namespace
TEST_F(WidgetWithCustomParamsTest, NamePropagatedFromContentsViewClassName) {
WidgetDelegate delegate;
auto view = std::make_unique<ViewWithClassName>();
auto* contents = delegate.SetContentsView(std::move(view));
SetInitFunction(base::BindLambdaForTesting(
[&](Widget::InitParams* params) { params->delegate = &delegate; }));
std::unique_ptr<Widget> widget =
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
EXPECT_EQ(contents->GetClassName(),
widget->native_widget_private()->GetName());
EXPECT_EQ(contents->GetClassName(), widget->GetName());
}
namespace {
class TestView : public View {
METADATA_HEADER(TestView, View)
public:
~TestView() override = default;
void OnThemeChanged() override {
View::OnThemeChanged();
auto* native_theme = GetNativeTheme();
if (native_theme && native_theme->user_color()) {
user_color_ = *native_theme->user_color();
}
}
SkColor user_color() const { return user_color_; }
private:
SkColor user_color_ = SK_ColorWHITE;
};
BEGIN_METADATA(TestView)
END_METADATA
} // namespace
class WidgetColorModeTest : public WidgetTest {
public:
static constexpr SkColor kLightColor = SK_ColorWHITE;
static constexpr SkColor kDarkColor = SK_ColorBLACK;
WidgetColorModeTest() = default;
~WidgetColorModeTest() override = default;
void SetUp() override {
WidgetTest::SetUp();
// Setup color provider for the ui::kColorSysPrimary color.
ui::ColorProviderManager& manager =
ui::ColorProviderManager::GetForTesting();
manager.AppendColorProviderInitializer(base::BindRepeating(&AddColor));
}
void TearDown() override {
ui::ColorProviderManager::ResetForTesting();
WidgetTest::TearDown();
}
private:
static void AddColor(ui::ColorProvider* provider,
const ui::ColorProviderKey& key) {
ui::ColorMixer& mixer = provider->AddMixer();
mixer[ui::kColorSysPrimary] = {
key.color_mode == ui::ColorProviderKey::ColorMode::kDark ? kDarkColor
: kLightColor};
}
};
TEST_F(WidgetColorModeTest, ColorModeOverride_NoOverride) {
ui::TestNativeTheme test_theme;
std::unique_ptr<Widget> widget = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
test_theme.SetDarkMode(true);
widget->SetNativeThemeForTest(&test_theme);
widget->SetColorModeOverride({});
// Verify that we resolve the dark color when we don't override color mode.
EXPECT_EQ(kDarkColor,
widget->GetColorProvider()->GetColor(ui::kColorSysPrimary));
}
TEST_F(WidgetColorModeTest, ColorModeOverride_DarkOverride) {
ui::TestNativeTheme test_theme;
std::unique_ptr<Widget> widget = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
test_theme.SetDarkMode(false);
widget->SetNativeThemeForTest(&test_theme);
widget->SetColorModeOverride(ui::ColorProviderKey::ColorMode::kDark);
// Verify that we resolve the light color even though the theme is dark.
EXPECT_EQ(kDarkColor,
widget->GetColorProvider()->GetColor(ui::kColorSysPrimary));
}
TEST_F(WidgetColorModeTest, ColorModeOverride_LightOverride) {
ui::TestNativeTheme test_theme;
std::unique_ptr<Widget> widget = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
test_theme.SetDarkMode(true);
widget->SetNativeThemeForTest(&test_theme);
widget->SetColorModeOverride(ui::ColorProviderKey::ColorMode::kLight);
// Verify that we resolve the light color even though the theme is dark.
EXPECT_EQ(kLightColor,
widget->GetColorProvider()->GetColor(ui::kColorSysPrimary));
}
TEST_F(WidgetColorModeTest, ChildInheritsColorMode_NoOverrides) {
// Create the parent widget and set the native theme to dark.
ui::TestNativeTheme test_theme;
std::unique_ptr<Widget> widget = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
test_theme.SetDarkMode(true);
widget->SetNativeThemeForTest(&test_theme);
// Create the child widget.
std::unique_ptr<Widget> widget_child =
base::WrapUnique(CreateChildPlatformWidget(
widget->GetNativeView(), Widget::InitParams::CLIENT_OWNS_WIDGET));
// Ensure neither has an override set. The child should inherit the color mode
// of the parent.
widget->SetColorModeOverride({});
widget_child->SetColorModeOverride({});
EXPECT_EQ(kDarkColor,
widget->GetColorProvider()->GetColor(ui::kColorSysPrimary));
EXPECT_EQ(kDarkColor,
widget_child->GetColorProvider()->GetColor(ui::kColorSysPrimary));
// Set the parent's native theme to light. The child should inherit the color
// mode of the parent.
test_theme.SetDarkMode(false);
EXPECT_EQ(kLightColor,
widget->GetColorProvider()->GetColor(ui::kColorSysPrimary));
EXPECT_EQ(kLightColor,
widget_child->GetColorProvider()->GetColor(ui::kColorSysPrimary));
}
TEST_F(WidgetColorModeTest, ChildInheritsColorMode_Overrides) {
// Create the parent widget and set the native theme to dark.
ui::TestNativeTheme test_theme;
std::unique_ptr<Widget> widget = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
test_theme.SetDarkMode(true);
widget->SetNativeThemeForTest(&test_theme);
// Create the child widget.
std::unique_ptr<Widget> widget_child =
base::WrapUnique(CreateChildPlatformWidget(
widget->GetNativeView(), Widget::InitParams::CLIENT_OWNS_WIDGET));
// Ensure neither has an override set. The child should inherit the color mode
// of the parent.
widget->SetColorModeOverride({});
widget_child->SetColorModeOverride({});
EXPECT_EQ(kDarkColor,
widget->GetColorProvider()->GetColor(ui::kColorSysPrimary));
EXPECT_EQ(kDarkColor,
widget_child->GetColorProvider()->GetColor(ui::kColorSysPrimary));
// Set the parent's override to light, then back to dark. the child should
// follow the parent's overridden color mode.
widget->SetColorModeOverride(ui::ColorProviderKey::ColorMode::kLight);
EXPECT_EQ(kLightColor,
widget->GetColorProvider()->GetColor(ui::kColorSysPrimary));
EXPECT_EQ(kLightColor,
widget_child->GetColorProvider()->GetColor(ui::kColorSysPrimary));
widget->SetColorModeOverride(ui::ColorProviderKey::ColorMode::kDark);
EXPECT_EQ(kDarkColor,
widget->GetColorProvider()->GetColor(ui::kColorSysPrimary));
EXPECT_EQ(kDarkColor,
widget_child->GetColorProvider()->GetColor(ui::kColorSysPrimary));
// Override the child's color mode to light. The parent should continue to
// report a dark color mode.
widget_child->SetColorModeOverride(ui::ColorProviderKey::ColorMode::kLight);
EXPECT_EQ(kDarkColor,
widget->GetColorProvider()->GetColor(ui::kColorSysPrimary));
EXPECT_EQ(kLightColor,
widget_child->GetColorProvider()->GetColor(ui::kColorSysPrimary));
}
TEST_F(WidgetTest, NativeWindowProperty) {
const char* key = "foo";
int value = 3;
std::unique_ptr<Widget> widget = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
EXPECT_EQ(nullptr, widget->GetNativeWindowProperty(key));
widget->SetNativeWindowProperty(key, &value);
EXPECT_EQ(&value, widget->GetNativeWindowProperty(key));
widget->SetNativeWindowProperty(key, nullptr);
EXPECT_EQ(nullptr, widget->GetNativeWindowProperty(key));
}
TEST_F(WidgetTest, GetParent) {
// Create a hierarchy of native widgets.
std::unique_ptr<Widget> toplevel = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
std::unique_ptr<Widget> child = base::WrapUnique(CreateChildPlatformWidget(
toplevel->GetNativeView(), Widget::InitParams::CLIENT_OWNS_WIDGET));
std::unique_ptr<Widget> grandchild =
base::WrapUnique(CreateChildPlatformWidget(
child->GetNativeView(), Widget::InitParams::CLIENT_OWNS_WIDGET));
EXPECT_EQ(nullptr, toplevel->parent());
EXPECT_EQ(child.get(), grandchild->parent());
EXPECT_EQ(toplevel.get(), child->parent());
// children should be automatically destroyed with |toplevel|.
}
// Verify that there is no change in focus if |enable_arrow_key_traversal| is
// false (the default).
TEST_F(WidgetTest, ArrowKeyFocusTraversalOffByDefault) {
std::unique_ptr<Widget> toplevel = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
// Establish default value.
DCHECK(!toplevel->widget_delegate()->enable_arrow_key_traversal());
View* container = toplevel->client_view();
container->SetLayoutManager(std::make_unique<FillLayout>());
auto* const button1 =
container->AddChildView(std::make_unique<LabelButton>());
auto* const button2 =
container->AddChildView(std::make_unique<LabelButton>());
toplevel->Show();
button1->RequestFocus();
ui::KeyEvent right_arrow(ui::EventType::kKeyPressed, ui::VKEY_RIGHT,
ui::EF_NONE);
toplevel->OnKeyEvent(&right_arrow);
EXPECT_TRUE(button1->HasFocus());
EXPECT_FALSE(button2->HasFocus());
ui::KeyEvent left_arrow(ui::EventType::kKeyPressed, ui::VKEY_LEFT,
ui::EF_NONE);
toplevel->OnKeyEvent(&left_arrow);
EXPECT_TRUE(button1->HasFocus());
EXPECT_FALSE(button2->HasFocus());
ui::KeyEvent up_arrow(ui::EventType::kKeyPressed, ui::VKEY_UP, ui::EF_NONE);
toplevel->OnKeyEvent(&up_arrow);
EXPECT_TRUE(button1->HasFocus());
EXPECT_FALSE(button2->HasFocus());
ui::KeyEvent down_arrow(ui::EventType::kKeyPressed, ui::VKEY_DOWN,
ui::EF_NONE);
toplevel->OnKeyEvent(&down_arrow);
EXPECT_TRUE(button1->HasFocus());
EXPECT_FALSE(button2->HasFocus());
}
// Verify that arrow keys can change focus if |enable_arrow_key_traversal| is
// set to true.
TEST_F(WidgetTest, ArrowKeyTraversalMovesFocusBetweenViews) {
std::unique_ptr<Widget> toplevel = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
toplevel->widget_delegate()->SetEnableArrowKeyTraversal(true);
View* container = toplevel->client_view();
container->SetLayoutManager(std::make_unique<FillLayout>());
auto* const button1 =
container->AddChildView(std::make_unique<LabelButton>());
auto* const button2 =
container->AddChildView(std::make_unique<LabelButton>());
auto* const button3 =
container->AddChildView(std::make_unique<LabelButton>());
toplevel->Show();
button1->RequestFocus();
// Right should advance focus (similar to TAB).
ui::KeyEvent right_arrow(ui::EventType::kKeyPressed, ui::VKEY_RIGHT,
ui::EF_NONE);
toplevel->OnKeyEvent(&right_arrow);
EXPECT_FALSE(button1->HasFocus());
EXPECT_TRUE(button2->HasFocus());
EXPECT_FALSE(button3->HasFocus());
// Down should also advance focus.
ui::KeyEvent down_arrow(ui::EventType::kKeyPressed, ui::VKEY_DOWN,
ui::EF_NONE);
toplevel->OnKeyEvent(&down_arrow);
EXPECT_FALSE(button1->HasFocus());
EXPECT_FALSE(button2->HasFocus());
EXPECT_TRUE(button3->HasFocus());
// Left should reverse focus (similar to SHIFT+TAB).
ui::KeyEvent left_arrow(ui::EventType::kKeyPressed, ui::VKEY_LEFT,
ui::EF_NONE);
toplevel->OnKeyEvent(&left_arrow);
EXPECT_FALSE(button1->HasFocus());
EXPECT_TRUE(button2->HasFocus());
EXPECT_FALSE(button3->HasFocus());
// Up should also reverse focus.
ui::KeyEvent up_arrow(ui::EventType::kKeyPressed, ui::VKEY_UP, ui::EF_NONE);
toplevel->OnKeyEvent(&up_arrow);
EXPECT_TRUE(button1->HasFocus());
EXPECT_FALSE(button2->HasFocus());
EXPECT_FALSE(button3->HasFocus());
// Test backwards wrap-around.
ui::KeyEvent up_arrow2(ui::EventType::kKeyPressed, ui::VKEY_UP, ui::EF_NONE);
toplevel->OnKeyEvent(&up_arrow2);
EXPECT_FALSE(button1->HasFocus());
EXPECT_FALSE(button2->HasFocus());
EXPECT_TRUE(button3->HasFocus());
// Test forward wrap-around.
ui::KeyEvent down_arrow2(ui::EventType::kKeyPressed, ui::VKEY_DOWN,
ui::EF_NONE);
toplevel->OnKeyEvent(&down_arrow2);
EXPECT_TRUE(button1->HasFocus());
EXPECT_FALSE(button2->HasFocus());
EXPECT_FALSE(button3->HasFocus());
}
TEST_F(WidgetTest, ArrowKeyTraversalNotInheritedByChildWidgets) {
std::unique_ptr<Widget> parent = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
std::unique_ptr<Widget> child = base::WrapUnique(CreateChildPlatformWidget(
parent->GetNativeView(), Widget::InitParams::CLIENT_OWNS_WIDGET));
parent->widget_delegate()->SetEnableArrowKeyTraversal(true);
View* container = child->GetContentsView();
DCHECK(container);
container->SetLayoutManager(std::make_unique<FillLayout>());
auto* const button1 =
container->AddChildView(std::make_unique<LabelButton>());
auto* const button2 =
container->AddChildView(std::make_unique<LabelButton>());
parent->Show();
child->Show();
button1->RequestFocus();
// Arrow key should not cause focus change on child since only the parent
// Widget has |enable_arrow_key_traversal| set.
ui::KeyEvent right_arrow(ui::EventType::kKeyPressed, ui::VKEY_RIGHT,
ui::EF_NONE);
child->OnKeyEvent(&right_arrow);
EXPECT_TRUE(button1->HasFocus());
EXPECT_FALSE(button2->HasFocus());
}
TEST_F(WidgetTest, ArrowKeyTraversalMayBeExplicitlyEnabledByChildWidgets) {
std::unique_ptr<Widget> parent = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
std::unique_ptr<Widget> child = base::WrapUnique(CreateChildPlatformWidget(
parent->GetNativeView(), Widget::InitParams::CLIENT_OWNS_WIDGET));
child->widget_delegate()->SetEnableArrowKeyTraversal(true);
View* container = child->GetContentsView();
container->SetLayoutManager(std::make_unique<FillLayout>());
auto* const button1 =
container->AddChildView(std::make_unique<LabelButton>());
auto* const button2 =
container->AddChildView(std::make_unique<LabelButton>());
parent->Show();
child->Show();
button1->RequestFocus();
// Arrow key should cause focus key on child since child has flag set, even
// if the parent Widget does not.
ui::KeyEvent right_arrow(ui::EventType::kKeyPressed, ui::VKEY_RIGHT,
ui::EF_NONE);
child->OnKeyEvent(&right_arrow);
EXPECT_FALSE(button1->HasFocus());
EXPECT_TRUE(button2->HasFocus());
}
////////////////////////////////////////////////////////////////////////////////
// Widget::GetTopLevelWidget tests.
TEST_F(WidgetTest, GetTopLevelWidget_Native) {
// Create a hierarchy of native widgets.
std::unique_ptr<Widget> toplevel = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
gfx::NativeView parent = toplevel->GetNativeView();
std::unique_ptr<Widget> child = base::WrapUnique(CreateChildPlatformWidget(
parent, Widget::InitParams::CLIENT_OWNS_WIDGET));
EXPECT_EQ(toplevel.get(), toplevel->GetTopLevelWidget());
EXPECT_EQ(toplevel.get(), child->GetTopLevelWidget());
// |child| should be automatically destroyed with |toplevel|.
}
// Test if a focus manager and an inputmethod work without CHECK failure
// when window activation changes.
TEST_F(WidgetTest, ChangeActivation) {
std::unique_ptr<Widget> top1 = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
top1->Show();
RunPendingMessages();
std::unique_ptr<Widget> top2 = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
top2->Show();
RunPendingMessages();
top1->Activate();
RunPendingMessages();
top2->Activate();
RunPendingMessages();
top1->Activate();
RunPendingMessages();
}
// Tests visibility of child widgets.
TEST_F(WidgetTest, Visibility) {
std::unique_ptr<Widget> toplevel = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
gfx::NativeView parent = toplevel->GetNativeView();
std::unique_ptr<Widget> child = base::WrapUnique(CreateChildPlatformWidget(
parent, Widget::InitParams::CLIENT_OWNS_WIDGET));
EXPECT_FALSE(toplevel->IsVisible());
EXPECT_FALSE(child->IsVisible());
// Showing a child with a hidden parent keeps the child hidden.
child->Show();
EXPECT_FALSE(toplevel->IsVisible());
EXPECT_FALSE(child->IsVisible());
// Showing a hidden parent with a visible child shows both.
toplevel->Show();
EXPECT_TRUE(toplevel->IsVisible());
EXPECT_TRUE(child->IsVisible());
// Hiding a parent hides both parent and child.
toplevel->Hide();
EXPECT_FALSE(toplevel->IsVisible());
EXPECT_FALSE(child->IsVisible());
// Hiding a child while the parent is hidden keeps the child hidden when the
// parent is shown.
child->Hide();
toplevel->Show();
EXPECT_TRUE(toplevel->IsVisible());
EXPECT_FALSE(child->IsVisible());
// |child| should be automatically destroyed with |toplevel|.
}
// Test that child widgets are positioned relative to their parent.
TEST_F(WidgetTest, ChildBoundsRelativeToParent) {
std::unique_ptr<Widget> toplevel = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
std::unique_ptr<Widget> child = base::WrapUnique(CreateChildPlatformWidget(
toplevel->GetNativeView(), Widget::InitParams::CLIENT_OWNS_WIDGET));
toplevel->SetBounds(gfx::Rect(160, 100, 320, 200));
child->SetBounds(gfx::Rect(0, 0, 320, 200));
child->Show();
toplevel->Show();
gfx::Rect toplevel_bounds = toplevel->GetWindowBoundsInScreen();
// Check the parent origin. If it was (0, 0) the test wouldn't be interesting.
EXPECT_NE(gfx::Vector2d(0, 0), toplevel_bounds.OffsetFromOrigin());
// The child's origin is at (0, 0), but the same size, so bounds should match.
EXPECT_EQ(toplevel_bounds, child->GetWindowBoundsInScreen());
}
////////////////////////////////////////////////////////////////////////////////
// Widget ownership tests.
//
// Tests various permutations of Widget ownership specified in the
// InitParams::Ownership param. Make sure that they are properly destructed
// during shutdown.
// A bag of state to monitor destructions.
struct OwnershipTestState {
OwnershipTestState() = default;
bool widget_deleted = false;
bool native_widget_deleted = false;
};
class WidgetOwnershipTest : public WidgetTest {
public:
WidgetOwnershipTest() = default;
WidgetOwnershipTest(const WidgetOwnershipTest&) = delete;
WidgetOwnershipTest& operator=(const WidgetOwnershipTest&) = delete;
~WidgetOwnershipTest() override = default;
void TearDown() override {
EXPECT_TRUE(state()->widget_deleted);
EXPECT_TRUE(state()->native_widget_deleted);
WidgetTest::TearDown();
}
OwnershipTestState* state() { return &state_; }
private:
OwnershipTestState state_;
};
// A Widget subclass that updates a bag of state when it is destroyed.
class OwnershipTestWidget : public Widget {
public:
explicit OwnershipTestWidget(OwnershipTestState* state) : state_(state) {}
OwnershipTestWidget(const OwnershipTestWidget&) = delete;
OwnershipTestWidget& operator=(const OwnershipTestWidget&) = delete;
~OwnershipTestWidget() override { state_->widget_deleted = true; }
private:
raw_ptr<OwnershipTestState> state_;
};
class NativeWidgetDestroyedWaiter {
public:
explicit NativeWidgetDestroyedWaiter(OwnershipTestState* state)
: state_(state) {}
base::OnceClosure GetNativeWidgetDestroyedCallback() {
return base::BindOnce(
[](OwnershipTestState* state, base::RunLoop* run_loop) {
state->native_widget_deleted = true;
run_loop->Quit();
},
state_.get(), &run_loop_);
}
void Wait() {
if (!state_->native_widget_deleted) {
run_loop_.Run();
}
}
private:
base::RunLoop run_loop_;
raw_ptr<OwnershipTestState> state_;
};
using NativeWidgetOwnsWidgetTest = WidgetOwnershipTest;
// NativeWidget owns its Widget, part 1.1: NativeWidget is a non-desktop
// widget, CloseNow() destroys Widget and NativeWidget synchronously.
TEST_F(NativeWidgetOwnsWidgetTest, NonDesktopWidget_CloseNow) {
Widget* widget = new OwnershipTestWidget(state());
Widget::InitParams params =
CreateParams(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
Widget::InitParams::TYPE_POPUP);
params.native_widget = CreatePlatformNativeWidgetImpl(
widget, kStubCapture, &state()->native_widget_deleted);
widget->Init(std::move(params));
widget->CloseNow();
// Both widget and native widget should be deleted synchronously.
EXPECT_TRUE(state()->widget_deleted);
EXPECT_TRUE(state()->native_widget_deleted);
}
// NativeWidget owns its Widget, part 1.2: NativeWidget is a non-desktop
// widget, Close() destroys Widget and NativeWidget asynchronously.
TEST_F(NativeWidgetOwnsWidgetTest, NonDesktopWidget_Close) {
NativeWidgetDestroyedWaiter waiter(state());
Widget* widget = new OwnershipTestWidget(state());
Widget::InitParams params =
CreateParams(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
Widget::InitParams::TYPE_POPUP);
params.native_widget = CreatePlatformNativeWidgetImpl(
widget, kStubCapture, waiter.GetNativeWidgetDestroyedCallback());
widget->Init(std::move(params));
widget->Close();
waiter.Wait();
EXPECT_TRUE(state()->widget_deleted);
EXPECT_TRUE(state()->native_widget_deleted);
}
// NativeWidget owns its Widget, part 1.3: NativeWidget is a desktop
// widget, Close() destroys Widget and NativeWidget asynchronously.
#if BUILDFLAG(ENABLE_DESKTOP_AURA)
TEST_F(NativeWidgetOwnsWidgetTest, DesktopWidget_Close) {
NativeWidgetDestroyedWaiter waiter(state());
Widget* widget = new OwnershipTestWidget(state());
Widget::InitParams params =
CreateParams(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
Widget::InitParams::TYPE_POPUP);
params.native_widget = CreatePlatformDesktopNativeWidgetImpl(
widget, kStubCapture, waiter.GetNativeWidgetDestroyedCallback());
widget->Init(std::move(params));
widget->Close();
waiter.Wait();
EXPECT_TRUE(state()->widget_deleted);
EXPECT_TRUE(state()->native_widget_deleted);
}
#endif
// NativeWidget owns its Widget, part 1.4: NativeWidget is a desktop
// widget. Unlike desktop widget, CloseNow() might destroy Widget and
// NativeWidget asynchronously.
#if BUILDFLAG(ENABLE_DESKTOP_AURA)
TEST_F(NativeWidgetOwnsWidgetTest, DesktopWidget_CloseNow) {
NativeWidgetDestroyedWaiter waiter(state());
Widget* widget = new OwnershipTestWidget(state());
Widget::InitParams params =
CreateParams(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
Widget::InitParams::TYPE_POPUP);
params.native_widget = CreatePlatformDesktopNativeWidgetImpl(
widget, kStubCapture, waiter.GetNativeWidgetDestroyedCallback());
widget->Init(std::move(params));
widget->CloseNow();
waiter.Wait();
EXPECT_TRUE(state()->widget_deleted);
EXPECT_TRUE(state()->native_widget_deleted);
}
#endif
// NativeWidget owns its Widget, part 2.1: NativeWidget is a non-desktop
// widget. CloseNow() the parent should destroy the child.
TEST_F(NativeWidgetOwnsWidgetTest, NonDestkopWidget_CloseNowParent) {
NativeWidgetDestroyedWaiter waiter(state());
Widget* toplevel = CreateTopLevelPlatformWidget();
Widget* widget = new OwnershipTestWidget(state());
Widget::InitParams params =
CreateParams(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
Widget::InitParams::TYPE_POPUP);
params.parent = toplevel->GetNativeView();
params.native_widget = CreatePlatformNativeWidgetImpl(
widget, kStubCapture, waiter.GetNativeWidgetDestroyedCallback());
widget->Init(std::move(params));
// Now destroy the native widget. This is achieved by closing the toplevel.
toplevel->CloseNow();
// The NativeWidget won't be deleted until after a return to the message loop
// so we have to run pending messages before testing the destruction status.
waiter.Wait();
EXPECT_TRUE(state()->widget_deleted);
EXPECT_TRUE(state()->native_widget_deleted);
}
// NativeWidget owns its Widget, part 2.2: NativeWidget is a desktop
// widget. CloseNow() the parent should destroy the child.
#if BUILDFLAG(ENABLE_DESKTOP_AURA)
TEST_F(NativeWidgetOwnsWidgetTest, DestkopWidget_CloseNowParent) {
NativeWidgetDestroyedWaiter waiter(state());
Widget* toplevel = CreateTopLevelPlatformDesktopWidget();
Widget* widget = new OwnershipTestWidget(state());
Widget::InitParams params =
CreateParams(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
Widget::InitParams::TYPE_POPUP);
params.parent = toplevel->GetNativeView();
params.native_widget = CreatePlatformDesktopNativeWidgetImpl(
widget, kStubCapture, waiter.GetNativeWidgetDestroyedCallback());
widget->Init(std::move(params));
// Now destroy the native widget. This is achieved by closing the toplevel.
toplevel->CloseNow();
// The NativeWidget won't be deleted until after a return to the message loop
// so we have to run pending messages before testing the destruction status.
waiter.Wait();
EXPECT_TRUE(state()->widget_deleted);
EXPECT_TRUE(state()->native_widget_deleted);
}
#endif
// NativeWidget owns its Widget, part 3.1: NativeWidget is a non-desktop
// widget, destroyed out from under it by the OS.
TEST_F(NativeWidgetOwnsWidgetTest, NonDesktopWidget_NativeDestroy) {
Widget* widget = new OwnershipTestWidget(state());
Widget::InitParams params =
CreateParams(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
Widget::InitParams::TYPE_POPUP);
params.native_widget = CreatePlatformNativeWidgetImpl(
widget, kStubCapture, &state()->native_widget_deleted);
widget->Init(std::move(params));
// Now simulate a destroy of the platform native widget from the OS:
SimulateNativeDestroy(widget);
EXPECT_TRUE(state()->widget_deleted);
EXPECT_TRUE(state()->native_widget_deleted);
}
#if BUILDFLAG(ENABLE_DESKTOP_AURA)
// NativeWidget owns its Widget, part 3.2: NativeWidget is a desktop
// widget, destroyed out from under it by the OS.
TEST_F(NativeWidgetOwnsWidgetTest, DesktopWidget_NativeDestroy) {
NativeWidgetDestroyedWaiter waiter(state());
Widget* widget = new OwnershipTestWidget(state());
Widget::InitParams params =
CreateParams(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
Widget::InitParams::TYPE_POPUP);
params.native_widget = CreatePlatformDesktopNativeWidgetImpl(
widget, kStubCapture, waiter.GetNativeWidgetDestroyedCallback());
widget->Init(std::move(params));
// Now simulate a destroy of the platform native widget from the OS:
SimulateDesktopNativeDestroy(widget);
waiter.Wait();
EXPECT_TRUE(state()->widget_deleted);
EXPECT_TRUE(state()->native_widget_deleted);
}
#endif
using WidgetOwnsNativeWidgetTest = WidgetOwnershipTest;
// Widget owns its NativeWidget, part 1.
TEST_F(WidgetOwnsNativeWidgetTest, Ownership) {
auto widget = std::make_unique<OwnershipTestWidget>(state());
Widget::InitParams params =
CreateParamsForTestWidget(Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET,
Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.native_widget = CreatePlatformNativeWidgetImpl(
widget.get(), kStubCapture, &state()->native_widget_deleted);
widget->Init(std::move(params));
// Now delete the Widget, which should delete the NativeWidget.
widget.reset();
// TODO(beng): write test for this ownership scenario and the NativeWidget
// being deleted out from under the Widget.
}
// Widget owns its NativeWidget, part 2: destroy the parent view.
TEST_F(WidgetOwnsNativeWidgetTest, DestroyParentView) {
Widget* toplevel = CreateTopLevelPlatformWidget();
auto widget = std::make_unique<OwnershipTestWidget>(state());
Widget::InitParams params =
CreateParamsForTestWidget(Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET,
Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.parent = toplevel->GetNativeView();
params.native_widget = CreatePlatformNativeWidgetImpl(
widget.get(), kStubCapture, &state()->native_widget_deleted);
widget->Init(std::move(params));
// Now close the toplevel, which deletes the view hierarchy.
toplevel->CloseNow();
RunPendingMessages();
// This shouldn't delete the widget because it shouldn't be deleted
// from the native side.
EXPECT_FALSE(state()->widget_deleted);
EXPECT_FALSE(state()->native_widget_deleted);
}
// Widget owns its NativeWidget, part 3: has a WidgetDelegateView as contents.
TEST_F(WidgetOwnsNativeWidgetTest, WidgetDelegateView) {
auto widget = std::make_unique<OwnershipTestWidget>(state());
Widget::InitParams params =
CreateParamsForTestWidget(Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET,
Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.native_widget = CreatePlatformNativeWidgetImpl(
widget.get(), kStubCapture, &state()->native_widget_deleted);
params.delegate = new WidgetDelegateView(WidgetDelegateView::CreatePassKey());
widget->Init(std::move(params));
// Allow the Widget to go out of scope. There should be no crash or
// use-after-free.
}
// Widget owns its NativeWidget, part 4: Widget::CloseNow should be idempotent.
TEST_F(WidgetOwnsNativeWidgetTest, IdempotentCloseNow) {
auto widget = std::make_unique<OwnershipTestWidget>(state());
Widget::InitParams params =
CreateParamsForTestWidget(Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET,
Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.native_widget = CreatePlatformNativeWidgetImpl(
widget.get(), kStubCapture, &state()->native_widget_deleted);
widget->Init(std::move(params));
// Now close the Widget, which should delete the NativeWidget.
widget->CloseNow();
RunPendingMessages();
// Close the widget again should not crash.
widget->CloseNow();
RunPendingMessages();
}
// Widget owns its NativeWidget, part 5: Widget::Close should be idempotent.
TEST_F(WidgetOwnsNativeWidgetTest, IdempotentClose) {
auto widget = std::make_unique<OwnershipTestWidget>(state());
Widget::InitParams params =
CreateParamsForTestWidget(Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET,
Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.native_widget = CreatePlatformNativeWidgetImpl(
widget.get(), kStubCapture, &state()->native_widget_deleted);
widget->Init(std::move(params));
// Now close the Widget, which should delete the NativeWidget.
widget->Close();
RunPendingMessages();
// Close the widget again should not crash.
widget->Close();
RunPendingMessages();
}
// Tests for CLIENT_OWNS_WIDGET. The client holds a unique_ptr<Widget>.
// The NativeWidget will be destroyed when the platform window is closed.
using ClientOwnsWidgetTest = WidgetOwnershipTest;
TEST_F(ClientOwnsWidgetTest, Ownership) {
auto widget = std::make_unique<OwnershipTestWidget>(state());
Widget::InitParams params =
CreateParamsForTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET,
Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.native_widget = CreatePlatformNativeWidgetImpl(
widget.get(), kStubCapture, &state()->native_widget_deleted);
widget->Init(std::move(params));
widget->CloseNow();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(state()->native_widget_deleted);
}
TEST_F(ClientOwnsWidgetTest, DestructWithAsyncCloseFirst) {
auto widget = std::make_unique<OwnershipTestWidget>(state());
Widget::InitParams params =
CreateParamsForTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET,
Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.native_widget = CreatePlatformNativeWidgetImpl(
widget.get(), kStubCapture, &state()->native_widget_deleted);
widget->Init(std::move(params));
widget->Close();
widget.reset();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(state()->native_widget_deleted);
}
TEST_F(ClientOwnsWidgetTest, DestructWithoutExplicitClose) {
auto widget = std::make_unique<OwnershipTestWidget>(state());
Widget::InitParams params =
CreateParamsForTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET,
Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.native_widget = CreatePlatformNativeWidgetImpl(
widget.get(), kStubCapture, &state()->native_widget_deleted);
widget->Init(std::move(params));
widget.reset();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(state()->native_widget_deleted);
}
class WidgetDestroyCounter : public WidgetObserver {
public:
explicit WidgetDestroyCounter(Widget* widget)
: widget_(widget->GetWeakPtr()) {
widget_->AddObserver(this);
}
~WidgetDestroyCounter() override {
if (widget_) {
widget_->RemoveObserver(this);
}
}
int widget_destroying_count() const { return widget_destroying_count_; }
int widget_destroyed_count() const { return widget_destroyed_count_; }
private:
// WidgetObserver:
void OnWidgetDestroying(Widget* widget) override {
++widget_destroying_count_;
}
void OnWidgetDestroyed(Widget* widget) override { ++widget_destroyed_count_; }
base::WeakPtr<Widget> widget_;
int widget_destroying_count_ = 0;
int widget_destroyed_count_ = 0;
};
TEST_F(ClientOwnsWidgetTest, NotificationsTest) {
auto widget = std::make_unique<OwnershipTestWidget>(state());
Widget::InitParams params =
CreateParamsForTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET,
Widget::InitParams::TYPE_WINDOW_FRAMELESS);
params.native_widget = CreatePlatformNativeWidgetImpl(
widget.get(), kStubCapture, &state()->native_widget_deleted);
widget->Init(std::move(params));
auto observer = std::make_unique<WidgetDestroyCounter>(widget.get());
widget->Close();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(observer->widget_destroying_count(), 1);
EXPECT_EQ(observer->widget_destroyed_count(), 1);
widget.reset();
EXPECT_TRUE(state()->widget_deleted);
// The destroying & destroyed notifications should only happen once.
EXPECT_EQ(observer->widget_destroying_count(), 1);
EXPECT_EQ(observer->widget_destroyed_count(), 1);
}
////////////////////////////////////////////////////////////////////////////////
// Test to verify using various Widget methods doesn't crash when the underlying
// NativeView and NativeWidget is destroyed. Currently, for
// the WIDGET_OWNS_NATIVE_WIDGET ownership pattern, the NativeWidget will not be
// destroyed, but |native_widget_| will still be set to nullptr.
class WidgetWithDestroyedNativeViewOrNativeWidgetTest
: public ViewsTestBase,
public testing::WithParamInterface<
std::tuple<ViewsTestBase::NativeWidgetType,
Widget::InitParams::Ownership>> {
public:
WidgetWithDestroyedNativeViewOrNativeWidgetTest() = default;
WidgetWithDestroyedNativeViewOrNativeWidgetTest(
const WidgetWithDestroyedNativeViewOrNativeWidgetTest&) = delete;
WidgetWithDestroyedNativeViewOrNativeWidgetTest& operator=(
const WidgetWithDestroyedNativeViewOrNativeWidgetTest&) = delete;
~WidgetWithDestroyedNativeViewOrNativeWidgetTest() override = default;
// ViewsTestBase:
void SetUp() override {
set_native_widget_type(
std::get<ViewsTestBase::NativeWidgetType>(GetParam()));
ViewsTestBase::SetUp();
if (std::get<Widget::InitParams::Ownership>(GetParam()) ==
Widget::InitParams::CLIENT_OWNS_WIDGET) {
widget_ = std::make_unique<Widget>();
Widget::InitParams params =
CreateParamsForTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET,
Widget::InitParams::TYPE_WINDOW_FRAMELESS);
widget_->Init(std::move(params));
} else {
widget_ = CreateTestWidget(Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET);
}
widget()->Show();
widget()->native_widget_private()->CloseNow();
task_environment()->RunUntilIdle();
}
Widget* widget() { return widget_.get(); }
static std::string PrintTestName(
const ::testing::TestParamInfo<
WidgetWithDestroyedNativeViewOrNativeWidgetTest::ParamType>& info) {
std::string test_name;
switch (std::get<ViewsTestBase::NativeWidgetType>(info.param)) {
case ViewsTestBase::NativeWidgetType::kDefault:
test_name += "DefaultNativeWidget";
break;
case ViewsTestBase::NativeWidgetType::kDesktop:
test_name += "DesktopNativeWidget";
break;
}
test_name += "_";
switch (std::get<Widget::InitParams::Ownership>(info.param)) {
case Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET:
test_name += "WidgetOwnsNativeWidget";
break;
case Widget::InitParams::CLIENT_OWNS_WIDGET:
test_name += "ClientOwnsWidget";
break;
case Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET:
// Note: We don't test for this case in
// WidgetWithDestroyedNativeViewOrNativeWidgetTest.
test_name += "NativeWidgetOwnsWidget";
break;
}
return test_name;
}
private:
std::unique_ptr<Widget> widget_;
};
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, Activate) {
widget()->Activate();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, AddAndRemoveObserver) {
// Constructor calls |AddObserver()|
TestWidgetObserver observer(widget());
widget()->RemoveObserver(&observer);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
AddAndRemoveRemovalsObserver) {
TestWidgetRemovalsObserver removals_observer;
widget()->AddRemovalsObserver(&removals_observer);
widget()->RemoveRemovalsObserver(&removals_observer);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, AsWidget) {
widget()->AsWidget();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, CanActivate) {
widget()->CanActivate();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, CenterWindow) {
widget()->CenterWindow(gfx::Size());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, ClearNativeFocus) {
widget()->ClearNativeFocus();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, ClientView) {
widget()->client_view();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, Close) {
widget()->Close();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
CloseAllSecondaryWidgets) {
widget()->CloseAllSecondaryWidgets();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, CloseNow) {
widget()->CloseNow();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, ClosedReason) {
widget()->closed_reason();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, CloseWithReason) {
widget()->CloseWithReason(Widget::ClosedReason::kUnspecified);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
CreateNonClientFrameView) {
widget()->CreateNonClientFrameView();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, Deactivate) {
widget()->Deactivate();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, DraggedView) {
widget()->dragged_view();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, EndMoveLoop) {
widget()->EndMoveLoop();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, ExecuteCommand) {
widget()->ExecuteCommand(0);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, FlashFrame) {
widget()->FlashFrame(true);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, FrameType) {
widget()->frame_type();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, FrameTypeChanged) {
widget()->FrameTypeChanged();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetAccelerator) {
ui::Accelerator accelerator;
widget()->GetAccelerator(0, &accelerator);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetAllChildWidgets) {
Widget::Widgets widgets =
Widget::GetAllChildWidgets(widget()->GetNativeView());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetAllOwnedWidgets) {
Widget::Widgets widgets =
Widget::GetAllOwnedWidgets(widget()->GetNativeView());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetAndSetZOrderLevel) {
widget()->SetZOrderLevel(ui::ZOrderLevel::kNormal);
widget()->GetZOrderLevel();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
GetClientAreaBoundsInScreen) {
widget()->GetClientAreaBoundsInScreen();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetColorProvider) {
widget()->GetColorProvider();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetCompositor) {
widget()->GetCompositor();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetContentsView) {
widget()->GetContentsView();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetCustomTheme) {
widget()->GetCustomTheme();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetEventSink) {
widget()->GetEventSink();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetFocusSearch) {
widget()->GetFocusSearch();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetFocusManager) {
widget()->GetFocusManager();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetFocusTraversable) {
widget()->GetFocusTraversable();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetGestureConsumer) {
widget()->GetGestureConsumer();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetGestureRecognizer) {
widget()->GetGestureRecognizer();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetHitTestMask) {
SkPath mask;
widget()->GetHitTestMask(&mask);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetInputMethod) {
widget()->GetInputMethod();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetLayer) {
widget()->GetLayer();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetMinimumSize) {
widget()->GetMinimumSize();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetMaximumSize) {
widget()->GetMaximumSize();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetName) {
widget()->GetName();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetNativeTheme) {
widget()->GetNativeTheme();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetNativeView) {
widget()->GetNativeView();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetNativeWindow) {
widget()->GetNativeWindow();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
GetNativeWindowProperty) {
widget()->GetNativeWindowProperty("xx");
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetNonClientComponent) {
gfx::Point point;
widget()->GetNonClientComponent(point);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
GetPrimaryWindowWidget) {
widget()->GetPrimaryWindowWidget();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetRestoredBounds) {
widget()->GetRestoredBounds();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetRootView) {
widget()->GetRootView();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetSublevelManager) {
widget()->GetSublevelManager();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetThemeProvider) {
widget()->GetThemeProvider();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetTooltipManager) {
widget()->GetTooltipManager();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetTopLevelWidget) {
widget()->GetTopLevelWidget();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
GetTopLevelWidgetForNativeView) {
Widget::GetTopLevelWidgetForNativeView(widget()->GetNativeView());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetWeakPtr) {
widget()->GetWeakPtr();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
GetWidgetForNativeView) {
Widget::GetWidgetForNativeView(widget()->GetNativeView());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
GetWidgetForNativeWindow) {
Widget::GetWidgetForNativeWindow(widget()->GetNativeWindow());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
GetWindowBoundsInScreen) {
widget()->GetWindowBoundsInScreen();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
GetWorkAreaBoundsInScreen) {
widget()->GetWorkAreaBoundsInScreen();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetWorkspace) {
widget()->GetWorkspace();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, GetZOrderSublevel) {
widget()->GetZOrderSublevel();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, HasCapture) {
widget()->HasCapture();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, HasFocusManager) {
widget()->HasFocusManager();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, HasHitTestMask) {
widget()->HasHitTestMask();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, HasObserver) {
TestWidgetObserver observer(widget());
widget()->HasObserver(&observer);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, HasRemovalsObserver) {
TestWidgetRemovalsObserver observer;
widget()->HasRemovalsObserver(&observer);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, Hide) {
widget()->Hide();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, Init) {
Widget::InitParams params(
std::get<Widget::InitParams::Ownership>(GetParam()));
EXPECT_DCHECK_DEATH(widget()->Init(std::move(params)));
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, is_secondary_widget) {
widget()->is_secondary_widget();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, IsActive) {
widget()->IsActive();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, IsClosed) {
widget()->IsClosed();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, IsDialogBox) {
widget()->IsDialogBox();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, IsFullscreen) {
widget()->IsFullscreen();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, IsMaximized) {
widget()->IsMaximized();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, IsMinimized) {
widget()->IsMinimized();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, IsModal) {
widget()->IsModal();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, IsMouseEventsEnabled) {
widget()->IsMouseEventsEnabled();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, IsMoveLoopSupported) {
widget()->IsMoveLoopSupported();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
IsNativeWidgetInitialized) {
widget()->IsNativeWidgetInitialized();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, IsStackedAbove) {
std::unique_ptr<Widget> other_widget =
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
widget()->IsStackedAbove(other_widget->GetNativeView());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, IsVisible) {
widget()->IsVisible();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
IsVisibleOnAllWorkspaces) {
widget()->IsVisibleOnAllWorkspaces();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, OnGestureEvent) {
ui::GestureEvent event =
CreateTestGestureEvent(ui::EventType::kGestureScrollBegin, 5, 5);
widget()->OnGestureEvent(&event);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, OnKeyEvent) {
ui::KeyEvent event(ui::EventType::kKeyPressed, ui::VKEY_RIGHT, ui::EF_NONE);
widget()->OnKeyEvent(&event);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, OnMouseCaptureLost) {
widget()->OnMouseCaptureLost();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, OnMouseEvent) {
gfx::Point p(200, 200);
ui::MouseEvent event(ui::EventType::kMouseMoved, p, p, ui::EventTimeForNow(),
ui::EF_NONE, ui::EF_NONE);
widget()->OnMouseEvent(&event);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, OnNativeBlur) {
widget()->OnNativeBlur();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, OnNativeFocus) {
widget()->OnNativeFocus();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, OnNativeThemeUpdated) {
ui::TestNativeTheme theme;
widget()->OnNativeThemeUpdated(&theme);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
OnNativeWidgetActivationChanged) {
widget()->OnNativeWidgetActivationChanged(false);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
OnNativeWidgetAddedToCompositor) {
widget()->OnNativeWidgetAddedToCompositor();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
OnNativeWidgetBeginUserBoundsChange) {
widget()->OnNativeWidgetBeginUserBoundsChange();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, OnNativeWidgetCreated) {
EXPECT_DCHECK_DEATH(widget()->OnNativeWidgetCreated());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
OnNativeWidgetDestroyed) {
widget()->OnNativeWidgetDestroyed();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
OnNativeWidgetDestroying) {
EXPECT_DCHECK_DEATH(widget()->OnNativeWidgetDestroying());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
OnNativeWidgetEndUserBoundsChange) {
widget()->OnNativeWidgetEndUserBoundsChange();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, OnNativeWidgetMove) {
widget()->OnNativeWidgetMove();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, OnNativeWidgetPaint) {
auto display_list = base::MakeRefCounted<cc::DisplayItemList>();
widget()->OnNativeWidgetPaint(
ui::PaintContext(display_list.get(), 1, gfx::Rect(), false));
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
OnNativeWidgetParentChanged) {
widget()->OnNativeWidgetParentChanged(gfx::NativeView());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
OnNativeWidgetRemovingFromCompositor) {
widget()->OnNativeWidgetRemovingFromCompositor();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
OnNativeWidgetSizeChanged) {
widget()->OnNativeWidgetSizeChanged(gfx::Size());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
OnNativeWidgetVisibilityChanged) {
widget()->OnNativeWidgetVisibilityChanged(false);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
OnNativeWidgetWindowShowStateChanged) {
widget()->OnNativeWidgetWindowShowStateChanged();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
OnNativeWidgetWorkspaceChanged) {
widget()->OnNativeWidgetWorkspaceChanged();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, OnOwnerClosing) {
widget()->OnOwnerClosing();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
OnParentShouldPaintAsActiveChanged) {
widget()->OnParentShouldPaintAsActiveChanged();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, OnScrollEvent) {
ui::ScrollEvent scroll(ui::EventType::kScroll, gfx::Point(65, 5),
ui::EventTimeForNow(), 0, 0, 20, 0, 20, 2);
widget()->OnScrollEvent(&scroll);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
OnSizeConstraintsChanged) {
widget()->OnSizeConstraintsChanged();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, LayerTreeChanged) {
widget()->LayerTreeChanged();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
LayoutRootViewIfNecessary) {
widget()->LayoutRootViewIfNecessary();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, LockPaintAsActive) {
widget()->LockPaintAsActive();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, Maximize) {
widget()->Maximize();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, Minimize) {
widget()->Minimize();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, movement_disabled) {
widget()->movement_disabled();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, native_widget_private) {
widget()->native_widget_private();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, native_widget) {
widget()->native_widget();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, non_client_view) {
widget()->non_client_view();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
NotifyNativeViewHierarchyChanged) {
widget()->NotifyNativeViewHierarchyChanged();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
NotifyNativeViewHierarchyWillChange) {
widget()->NotifyNativeViewHierarchyWillChange();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, NotifyWillRemoveView) {
widget()->NotifyWillRemoveView(widget()->non_client_view());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, parent) {
widget()->parent();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
RegisterPaintAsActiveChangedCallback) {
auto subscription =
widget()->RegisterPaintAsActiveChangedCallback(base::DoNothing());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, ReleaseCapture) {
widget()->ReleaseCapture();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, ReorderNativeViews) {
widget()->ReorderNativeViews();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, ReparentNativeView) {
EXPECT_DCHECK_DEATH(
Widget::ReparentNativeView(widget()->GetNativeView(), gfx::NativeView()));
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, Restore) {
widget()->Restore();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, RunMoveLoop) {
widget()->RunMoveLoop(gfx::Vector2d(), Widget::MoveLoopSource::kMouse,
Widget::MoveLoopEscapeBehavior::kHide);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, RunShellDrag) {
std::unique_ptr<OSExchangeData> data(std::make_unique<OSExchangeData>());
widget()->RunShellDrag(nullptr, std::move(data), gfx::Point(), 0,
ui::mojom::DragEventSource::kMouse);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, ScheduleLayout) {
widget()->ScheduleLayout();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, SchedulePaintInRect) {
widget()->SchedulePaintInRect(gfx::Rect(0, 0, 1, 2));
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, SetAspectRatio) {
widget()->SetAspectRatio(gfx::SizeF(1.0, 1.0));
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, SetBounds) {
widget()->SetBounds(gfx::Rect(0, 0, 100, 80));
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, SetBoundsConstrained) {
widget()->SetBoundsConstrained(gfx::Rect(0, 0, 120, 140));
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
SetCanAppearInExistingFullscreenSpaces) {
widget()->SetCanAppearInExistingFullscreenSpaces(false);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, SetCapture) {
widget()->SetCapture(widget()->GetRootView());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, SetContentsView) {
View view;
EXPECT_DCHECK_DEATH(widget()->SetContentsView(std::make_unique<View>()));
EXPECT_DCHECK_DEATH(widget()->SetContentsView(&view));
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, SetCursor) {
widget()->SetCursor(ui::Cursor());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
SetFocusTraversableParent) {
std::unique_ptr<Widget> another_widget =
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
widget()->SetFocusTraversableParent(another_widget->GetFocusTraversable());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
SetFocusTraversableParentView) {
std::unique_ptr<Widget> another_widget =
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
widget()->SetFocusTraversableParentView(another_widget->GetContentsView());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, SetFullscreen) {
widget()->SetFullscreen(true);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, SetInitialFocus) {
widget()->SetInitialFocus(ui::mojom::WindowShowState::kInactive);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
SetNativeWindowProperty) {
widget()->SetNativeWindowProperty("xx", widget());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, SetOpacity) {
widget()->SetOpacity(0.f);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, SetShape) {
auto rects = std::make_unique<Widget::ShapeRects>();
rects->emplace_back(40, 0, 20, 100);
rects->emplace_back(0, 40, 100, 20);
widget()->SetShape(std::move(rects));
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, SetSize) {
widget()->SetSize(gfx::Size(10, 11));
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
SetVisibilityChangedAnimationsEnabled) {
widget()->SetVisibilityChangedAnimationsEnabled(false);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
SetVisibilityAnimationDuration) {
widget()->SetVisibilityAnimationDuration(base::Seconds(1));
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
SetVisibilityAnimationTransition) {
widget()->SetVisibilityAnimationTransition(Widget::ANIMATE_BOTH);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, SetVisible) {
widget()->SetVisibilityAnimationTransition(Widget::ANIMATE_BOTH);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
SetVisibleOnAllWorkspaces) {
widget()->SetVisibleOnAllWorkspaces(true);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
ShouldDescendIntoChildForEventHandling) {
widget()->ShouldDescendIntoChildForEventHandling(nullptr, gfx::NativeView(),
nullptr, gfx::Point(0, 0));
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
ShouldHandleNativeWidgetActivationChanged) {
widget()->ShouldHandleNativeWidgetActivationChanged(true);
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, ShouldPaintAsActive) {
widget()->ShouldPaintAsActive();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, ShouldUseNativeFrame) {
widget()->ShouldUseNativeFrame();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
ShouldWindowContentsBeTransparent) {
widget()->ShouldWindowContentsBeTransparent();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, Show) {
widget()->Show();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, ShowEmojiPanel) {
widget()->ShowEmojiPanel();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, ShowInactive) {
widget()->ShowInactive();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, StackAbove) {
std::unique_ptr<Widget> another_widget =
CreateTestWidget(std::get<Widget::InitParams::Ownership>(GetParam()));
widget()->StackAbove(another_widget->GetNativeView());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, StackAboveWidget) {
std::unique_ptr<Widget> another_widget =
CreateTestWidget(std::get<Widget::InitParams::Ownership>(GetParam()));
widget()->StackAboveWidget(another_widget.get());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, StackAtTop) {
widget()->StackAtTop();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest,
SynthesizeMouseMoveEvent) {
widget()->SynthesizeMouseMoveEvent();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, ThemeChanged) {
widget()->ThemeChanged();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, UnlockPaintAsActive) {
// UnlockPaintAsActive() is called in the destructor of PaintAsActiveLock.
// External invocation is not allowed.
EXPECT_DCHECK_DEATH(widget()->UnlockPaintAsActive());
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, UpdateWindowIcon) {
widget()->UpdateWindowIcon();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, UpdateWindowTitle) {
widget()->UpdateWindowTitle();
}
TEST_P(WidgetWithDestroyedNativeViewOrNativeWidgetTest, ViewHierarchyChanged) {
widget()->ViewHierarchyChanged(
ViewHierarchyChangedDetails(true, nullptr, nullptr, nullptr));
}
INSTANTIATE_TEST_SUITE_P(
PlatformWidgetWithDestroyedNativeViewOrNativeWidgetTest,
WidgetWithDestroyedNativeViewOrNativeWidgetTest,
::testing::Combine(
::testing::Values(ViewsTestBase::NativeWidgetType::kDefault,
ViewsTestBase::NativeWidgetType::kDesktop),
::testing::Values(Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET,
Widget::InitParams::CLIENT_OWNS_WIDGET)),
WidgetWithDestroyedNativeViewOrNativeWidgetTest::PrintTestName);
////////////////////////////////////////////////////////////////////////////////
// Widget observer tests.
//
class WidgetObserverTest : public WidgetTest, public WidgetObserver {
public:
WidgetObserverTest() = default;
~WidgetObserverTest() override = default;
// Set a widget to Close() the next time the Widget being observed is hidden.
void CloseOnNextHide(Widget* widget) { widget_to_close_on_hide_ = widget; }
// Overridden from WidgetObserver:
void OnWidgetDestroying(Widget* widget) override {
if (active_ == widget) {
active_ = nullptr;
}
if (widget_activated_ == widget) {
widget_activated_ = nullptr;
}
}
void OnWidgetActivationChanged(Widget* widget, bool active) override {
if (active) {
if (widget_activated_) {
widget_activated_->Deactivate();
}
widget_activated_ = widget;
active_ = widget;
} else {
if (widget_activated_ == widget) {
widget_activated_ = nullptr;
}
widget_deactivated_ = widget->GetName();
}
}
void OnWidgetVisibilityChanged(Widget* widget, bool visible) override {
if (visible) {
widget_shown_ = widget->GetName();
return;
}
widget_hidden_ = widget->GetName();
if (widget_to_close_on_hide_) {
std::exchange(widget_to_close_on_hide_, nullptr)->Close();
}
}
void OnWidgetBoundsChanged(Widget* widget,
const gfx::Rect& new_bounds) override {
widget_bounds_changed_ = widget->GetName();
}
void reset() {
active_ = nullptr;
widget_activated_ = nullptr;
widget_deactivated_.clear();
widget_shown_.clear();
widget_hidden_.clear();
widget_bounds_changed_.clear();
}
Widget* NewWidget(std::string name) {
Widget* widget = new Widget();
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
params.name = std::move(name);
widget->Init(std::move(params));
widget->AddObserver(this);
return widget;
}
const Widget* active() const { return active_; }
const Widget* widget_activated() const { return widget_activated_; }
const std::string& widget_deactivated() const { return widget_deactivated_; }
const std::string& widget_shown() const { return widget_shown_; }
const std::string& widget_hidden() const { return widget_hidden_; }
const std::string& widget_bounds_changed() const {
return widget_bounds_changed_;
}
private:
raw_ptr<Widget> active_ = nullptr;
raw_ptr<Widget> widget_activated_ = nullptr;
std::string widget_deactivated_;
std::string widget_shown_;
std::string widget_hidden_;
std::string widget_bounds_changed_;
raw_ptr<Widget> widget_to_close_on_hide_ = nullptr;
};
// This test appears to be flaky on Mac.
#if BUILDFLAG(IS_MAC)
#define MAYBE_ActivationChange DISABLED_ActivationChange
#else
#define MAYBE_ActivationChange ActivationChange
#endif
TEST_F(WidgetObserverTest, MAYBE_ActivationChange) {
WidgetAutoclosePtr toplevel1(NewWidget("top1"));
WidgetAutoclosePtr toplevel2(NewWidget("top2"));
toplevel1->Show();
toplevel2->Show();
reset();
toplevel1->Activate();
RunPendingMessages();
EXPECT_EQ(toplevel1.get(), widget_activated());
toplevel2->Activate();
RunPendingMessages();
EXPECT_EQ(toplevel1->GetName(), widget_deactivated());
EXPECT_EQ(toplevel2.get(), widget_activated());
EXPECT_EQ(toplevel2.get(), active());
}
namespace {
// This class simulates a focus manager that moves focus to a second widget when
// the first one is closed. It simulates a situation where a sequence of widget
// observers might try to call Widget::Close in response to a OnWidgetClosing().
class WidgetActivationForwarder : public TestWidgetObserver {
public:
WidgetActivationForwarder(Widget* current_active_widget,
Widget* widget_to_activate)
: TestWidgetObserver(current_active_widget),
widget_to_activate_(widget_to_activate) {}
WidgetActivationForwarder(const WidgetActivationForwarder&) = delete;
WidgetActivationForwarder& operator=(const WidgetActivationForwarder&) =
delete;
~WidgetActivationForwarder() override = default;
private:
// WidgetObserver overrides:
void OnWidgetClosing(Widget* widget) override {
widget->OnNativeWidgetActivationChanged(false);
widget_to_activate_->Activate();
}
void OnWidgetActivationChanged(Widget* widget, bool active) override {
if (!active) {
widget->Close();
}
}
raw_ptr<Widget> widget_to_activate_;
};
// This class observes a widget and counts the number of times OnWidgetClosing
// is called.
class WidgetCloseCounter : public TestWidgetObserver {
public:
explicit WidgetCloseCounter(Widget* widget) : TestWidgetObserver(widget) {}
WidgetCloseCounter(const WidgetCloseCounter&) = delete;
WidgetCloseCounter& operator=(const WidgetCloseCounter&) = delete;
~WidgetCloseCounter() override = default;
int close_count() const { return close_count_; }
private:
// WidgetObserver overrides:
void OnWidgetClosing(Widget* widget) override { close_count_++; }
int close_count_ = 0;
};
} // namespace
// Makes sure close notifications aren't sent more than once when a Widget is
// shutting down. Test for crbug.com/714334
TEST_F(WidgetObserverTest, CloseReentrancy) {
Widget* widget1 = CreateTopLevelPlatformWidget();
Widget* widget2 = CreateTopLevelPlatformWidget();
WidgetCloseCounter counter(widget1);
WidgetActivationForwarder focus_manager(widget1, widget2);
widget1->Close();
EXPECT_EQ(1, counter.close_count());
widget2->Close();
}
TEST_F(WidgetObserverTest, VisibilityChange) {
WidgetAutoclosePtr toplevel(CreateTopLevelPlatformWidget());
WidgetAutoclosePtr child1(NewWidget("child1"));
WidgetAutoclosePtr child2(NewWidget("child2"));
toplevel->Show();
child1->Show();
child2->Show();
reset();
child1->Hide();
EXPECT_EQ(child1->GetName(), widget_hidden());
child2->Hide();
EXPECT_EQ(child2->GetName(), widget_hidden());
child1->Show();
EXPECT_EQ(child1->GetName(), widget_shown());
child2->Show();
EXPECT_EQ(child2->GetName(), widget_shown());
}
TEST_F(WidgetObserverTest, DestroyBubble) {
// This test expect NativeWidgetAura, force its creation.
ViewsDelegate::GetInstance()->set_native_widget_factory(
ViewsDelegate::NativeWidgetFactory());
std::unique_ptr<Widget> anchor = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
anchor->Show();
auto bubble_delegate = std::make_unique<WidgetTestBubbleDialogDelegateView>(
anchor->client_view());
{
std::unique_ptr<Widget> bubble_widget =
base::WrapUnique(BubbleDialogDelegateView::CreateBubble(
bubble_delegate.release(), Widget::InitParams::CLIENT_OWNS_WIDGET));
bubble_widget->Show();
}
anchor->Hide();
}
TEST_F(WidgetObserverTest, WidgetBoundsChanged) {
WidgetAutoclosePtr child1(NewWidget("child1"));
WidgetAutoclosePtr child2(NewWidget("child2"));
child1->OnNativeWidgetMove();
EXPECT_EQ(child1->GetName(), widget_bounds_changed());
child2->OnNativeWidgetMove();
EXPECT_EQ(child2->GetName(), widget_bounds_changed());
child1->OnNativeWidgetSizeChanged(gfx::Size());
EXPECT_EQ(child1->GetName(), widget_bounds_changed());
child2->OnNativeWidgetSizeChanged(gfx::Size());
EXPECT_EQ(child2->GetName(), widget_bounds_changed());
}
// An extension to WidgetBoundsChanged to ensure notifications are forwarded
// by the NativeWidget implementation.
TEST_F(WidgetObserverTest, WidgetBoundsChangedNative) {
// Don't use NewWidget(), so that the Init() flow can be observed to ensure
// consistency across platforms.
auto widget = std::make_unique<Widget>();
widget->AddObserver(this);
EXPECT_THAT(widget_bounds_changed(), IsEmpty());
Widget::InitParams params = CreateParams(
Widget::InitParams::CLIENT_OWNS_WIDGET, Widget::InitParams::TYPE_WINDOW);
// Use an origin within the work area since platforms (e.g. Mac) may move a
// window into the work area when showing, triggering a bounds change.
params.bounds = gfx::Rect(50, 50, 100, 100);
params.name = "widget";
// Init causes a bounds change, even while not showing. Note some platforms
// cause a bounds change even when the bounds are empty. Mac does not.
widget->Init(std::move(params));
EXPECT_THAT(widget_bounds_changed(), Not(IsEmpty()));
reset();
// Resizing while hidden, triggers a change.
widget->SetSize(gfx::Size(160, 100));
EXPECT_FALSE(widget->IsVisible());
EXPECT_THAT(widget_bounds_changed(), Not(IsEmpty()));
reset();
// Setting the same size does nothing.
widget->SetSize(gfx::Size(160, 100));
EXPECT_THAT(widget_bounds_changed(), IsEmpty());
reset();
// Showing does nothing to the bounds.
widget->Show();
EXPECT_TRUE(widget->IsVisible());
EXPECT_THAT(widget_bounds_changed(), IsEmpty());
reset();
// Resizing while shown.
widget->SetSize(gfx::Size(170, 100));
EXPECT_THAT(widget_bounds_changed(), Not(IsEmpty()));
reset();
// Resize to the same thing while shown does nothing.
widget->SetSize(gfx::Size(170, 100));
EXPECT_THAT(widget_bounds_changed(), IsEmpty());
reset();
// Move, but don't change the size.
widget->SetBounds(gfx::Rect(110, 110, 170, 100));
EXPECT_THAT(widget_bounds_changed(), Not(IsEmpty()));
reset();
// Moving to the same place does nothing.
widget->SetBounds(gfx::Rect(110, 110, 170, 100));
EXPECT_THAT(widget_bounds_changed(), IsEmpty());
reset();
// No bounds change when closing.
std::exchange(widget, nullptr)->CloseNow();
EXPECT_THAT(widget_bounds_changed(), IsEmpty());
}
namespace {
class MoveTrackingTestDesktopWidgetDelegate : public TestDesktopWidgetDelegate {
public:
int move_count() const { return move_count_; }
// WidgetDelegate:
void OnWidgetMove() override { ++move_count_; }
private:
int move_count_ = 0;
};
} // namespace
class DesktopWidgetObserverTest : public WidgetObserverTest {
public:
DesktopWidgetObserverTest() = default;
DesktopWidgetObserverTest(const DesktopWidgetObserverTest&) = delete;
DesktopWidgetObserverTest& operator=(const DesktopWidgetObserverTest&) =
delete;
~DesktopWidgetObserverTest() override = default;
// WidgetObserverTest:
void SetUp() override {
set_native_widget_type(NativeWidgetType::kDesktop);
WidgetObserverTest::SetUp();
}
};
// An extension to the WidgetBoundsChangedNative test above to ensure move
// notifications propagate to the WidgetDelegate.
TEST_F(DesktopWidgetObserverTest, OnWidgetMovedWhenOriginChangesNative) {
MoveTrackingTestDesktopWidgetDelegate delegate;
Widget::InitParams params = CreateParams(
Widget::InitParams::CLIENT_OWNS_WIDGET, Widget::InitParams::TYPE_WINDOW);
delegate.InitWidget(std::move(params));
Widget* widget = delegate.GetWidget();
widget->Show();
widget->SetBounds(gfx::Rect(100, 100, 300, 200));
const int moves_during_init = delegate.move_count();
// Resize without changing origin. No move.
widget->SetBounds(gfx::Rect(100, 100, 310, 210));
EXPECT_EQ(moves_during_init, delegate.move_count());
// Move without changing size. Moves.
widget->SetBounds(gfx::Rect(110, 110, 310, 210));
EXPECT_EQ(moves_during_init + 1, delegate.move_count());
// Changing both moves.
widget->SetBounds(gfx::Rect(90, 90, 330, 230));
EXPECT_EQ(moves_during_init + 2, delegate.move_count());
// Just grow vertically. On Mac, this changes the AppKit origin since it is
// from the bottom left of the screen, but there is no move as far as views is
// concerned.
widget->SetBounds(gfx::Rect(90, 90, 330, 240));
// No change.
EXPECT_EQ(moves_during_init + 2, delegate.move_count());
// For a similar reason, move the widget down by the same amount that it grows
// vertically. The AppKit origin does not change, but it is a move.
widget->SetBounds(gfx::Rect(90, 100, 330, 250));
EXPECT_EQ(moves_during_init + 3, delegate.move_count());
}
// Test correct behavior when widgets close themselves in response to visibility
// changes.
TEST_F(WidgetObserverTest, ClosingOnHiddenParent) {
WidgetAutoclosePtr parent(NewWidget("parent"));
Widget* child = CreateChildPlatformWidget(parent->GetNativeView());
TestWidgetObserver child_observer(child);
EXPECT_FALSE(parent->IsVisible());
EXPECT_FALSE(child->IsVisible());
// Note |child| is TYPE_CONTROL, which start shown. So no need to show the
// child separately.
parent->Show();
EXPECT_TRUE(parent->IsVisible());
EXPECT_TRUE(child->IsVisible());
// Simulate a child widget that closes itself when the parent is hidden.
CloseOnNextHide(child);
EXPECT_FALSE(child_observer.widget_closed());
parent->Hide();
RunPendingMessages();
EXPECT_TRUE(child_observer.widget_closed());
}
// Test behavior of NativeWidget*::GetWindowPlacement on the native desktop.
#if BUILDFLAG(IS_LINUX)
// On desktop-Linux cheat and use non-desktop widgets. On X11, minimize is
// asynchronous. Also (harder) showing a window doesn't activate it without
// user interaction (or extra steps only done for interactive ui tests).
// Without that, show_state remains in ui::mojom::WindowShowState::kInactive
// throughout.
// TODO(tapted): Find a nice way to run this with desktop widgets on Linux.
TEST_F(WidgetTest, GetWindowPlacement) {
#else
TEST_F(DesktopWidgetTest, GetWindowPlacement) {
#endif
WidgetAutoclosePtr widget;
widget.reset(CreateTopLevelNativeWidget());
gfx::Rect expected_bounds(100, 110, 200, 220);
widget->SetBounds(expected_bounds);
widget->Show();
// Start with something invalid to ensure it changes.
ui::mojom::WindowShowState show_state = ui::mojom::WindowShowState::kEnd;
gfx::Rect restored_bounds;
internal::NativeWidgetPrivate* native_widget =
widget->native_widget_private();
native_widget->GetWindowPlacement(&restored_bounds, &show_state);
EXPECT_EQ(expected_bounds, restored_bounds);
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
// Non-desktop/Ash widgets start off in "default" until a Restore().
EXPECT_EQ(ui::mojom::WindowShowState::kDefault, show_state);
widget->Restore();
native_widget->GetWindowPlacement(&restored_bounds, &show_state);
#endif
EXPECT_EQ(ui::mojom::WindowShowState::kNormal, show_state);
views::test::PropertyWaiter minimize_waiter(
base::BindRepeating(&Widget::IsMinimized, base::Unretained(widget.get())),
true);
widget->Minimize();
EXPECT_TRUE(minimize_waiter.Wait());
native_widget->GetWindowPlacement(&restored_bounds, &show_state);
EXPECT_EQ(ui::mojom::WindowShowState::kMinimized, show_state);
EXPECT_EQ(expected_bounds, restored_bounds);
views::test::PropertyWaiter restore_waiter(
base::BindRepeating(&Widget::IsMinimized, base::Unretained(widget.get())),
false);
widget->Restore();
EXPECT_TRUE(restore_waiter.Wait());
native_widget->GetWindowPlacement(&restored_bounds, &show_state);
EXPECT_EQ(ui::mojom::WindowShowState::kNormal, show_state);
EXPECT_EQ(expected_bounds, restored_bounds);
expected_bounds = gfx::Rect(130, 140, 230, 250);
widget->SetBounds(expected_bounds);
native_widget->GetWindowPlacement(&restored_bounds, &show_state);
EXPECT_EQ(ui::mojom::WindowShowState::kNormal, show_state);
EXPECT_EQ(expected_bounds, restored_bounds);
widget->SetFullscreen(true);
native_widget->GetWindowPlacement(&restored_bounds, &show_state);
#if BUILDFLAG(IS_WIN)
// Desktop Aura widgets on Windows currently don't update show_state when
// going fullscreen, and report restored_bounds as the full screen size.
// See http://crbug.com/475813.
EXPECT_EQ(ui::mojom::WindowShowState::kNormal, show_state);
#else
EXPECT_EQ(ui::mojom::WindowShowState::kFullscreen, show_state);
EXPECT_EQ(expected_bounds, restored_bounds);
#endif
widget->SetFullscreen(false);
native_widget->GetWindowPlacement(&restored_bounds, &show_state);
EXPECT_EQ(ui::mojom::WindowShowState::kNormal, show_state);
EXPECT_EQ(expected_bounds, restored_bounds);
}
// Test that widget size constraints are properly applied immediately after
// Init(), and that SetBounds() calls are appropriately clamped.
TEST_F(DesktopWidgetTest, MinimumSizeConstraints) {
TestDesktopWidgetDelegate delegate;
gfx::Size minimum_size(100, 100);
const gfx::Size smaller_size(90, 90);
delegate.set_contents_view(new StaticSizedView(minimum_size));
delegate.InitWidget(CreateParams(Widget::InitParams::CLIENT_OWNS_WIDGET,
Widget::InitParams::TYPE_WINDOW));
Widget* widget = delegate.GetWidget();
// On desktop Linux, the Widget must be shown to ensure the window is mapped.
// On other platforms this line is optional.
widget->Show();
// Sanity checks.
EXPECT_GT(delegate.initial_bounds().width(), minimum_size.width());
EXPECT_GT(delegate.initial_bounds().height(), minimum_size.height());
EXPECT_EQ(delegate.initial_bounds().size(),
widget->GetWindowBoundsInScreen().size());
// Note: StaticSizedView doesn't currently provide a maximum size.
EXPECT_EQ(gfx::Size(), widget->GetMaximumSize());
if (!widget->ShouldUseNativeFrame()) {
// The test environment may have dwm disabled on Windows. In this case,
// CustomFrameView is used instead of the NativeFrameView, which will
// provide a minimum size that includes frame decorations.
minimum_size = widget->non_client_view()
->GetWindowBoundsForClientBounds(gfx::Rect(minimum_size))
.size();
}
EXPECT_EQ(minimum_size, widget->GetMinimumSize());
EXPECT_EQ(minimum_size, GetNativeWidgetMinimumContentSize(widget));
// Trying to resize smaller than the minimum size should restrict the content
// size to the minimum size.
widget->SetBounds(gfx::Rect(smaller_size));
EXPECT_EQ(minimum_size, widget->GetClientAreaBoundsInScreen().size());
widget->SetSize(smaller_size);
EXPECT_EQ(minimum_size, widget->GetClientAreaBoundsInScreen().size());
}
// When a non-desktop widget has a desktop child widget, due to the
// async nature of desktop widget shutdown, the parent can be destroyed before
// its child. Make sure that parent() returns nullptr at this time.
#if BUILDFLAG(ENABLE_DESKTOP_AURA)
TEST_F(DesktopWidgetTest, GetPossiblyDestroyedParent) {
WidgetAutoclosePtr root(CreateTopLevelNativeWidget());
const auto create_widget = [](Widget* parent, bool is_desktop) {
Widget* widget = new Widget;
Widget::InitParams init_params(
Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
Widget::InitParams::TYPE_WINDOW);
init_params.parent = parent->GetNativeView();
init_params.context = parent->GetNativeView();
if (is_desktop) {
init_params.native_widget =
new test::TestPlatformNativeWidget<DesktopNativeWidgetAura>(
widget, false, nullptr);
} else {
init_params.native_widget =
new test::TestPlatformNativeWidget<NativeWidgetAura>(widget, false,
nullptr);
}
widget->Init(std::move(init_params));
return widget;
};
WidgetAutoclosePtr child(create_widget(root.get(), /* non-desktop */ false));
WidgetAutoclosePtr grandchild(create_widget(child.get(), /* desktop */ true));
child.reset();
EXPECT_EQ(grandchild->parent(), nullptr);
}
#endif // BUILDFLAG(ENABLE_DESKTOP_AURA)
// Tests that SetBounds() and GetWindowBoundsInScreen() is symmetric when the
// widget is visible and not maximized or fullscreen.
TEST_F(WidgetTest, GetWindowBoundsInScreen) {
// Choose test coordinates away from edges and dimensions that are "small"
// (but not too small) to ensure the OS doesn't try to adjust them.
const gfx::Rect kTestBounds(150, 150, 400, 300);
const gfx::Size kTestSize(200, 180);
{
// First test a toplevel widget.
WidgetAutoclosePtr widget(CreateTopLevelPlatformWidget());
widget->Show();
EXPECT_NE(kTestSize.ToString(),
widget->GetWindowBoundsInScreen().size().ToString());
widget->SetSize(kTestSize);
EXPECT_EQ(kTestSize.ToString(),
widget->GetWindowBoundsInScreen().size().ToString());
EXPECT_NE(kTestBounds.ToString(),
widget->GetWindowBoundsInScreen().ToString());
widget->SetBounds(kTestBounds);
EXPECT_EQ(kTestBounds.ToString(),
widget->GetWindowBoundsInScreen().ToString());
// Changing just the size should not change the origin.
widget->SetSize(kTestSize);
EXPECT_EQ(kTestBounds.origin().ToString(),
widget->GetWindowBoundsInScreen().origin().ToString());
}
// Same tests with a frameless window.
WidgetAutoclosePtr widget(CreateTopLevelFramelessPlatformWidget());
widget->Show();
EXPECT_NE(kTestSize.ToString(),
widget->GetWindowBoundsInScreen().size().ToString());
widget->SetSize(kTestSize);
EXPECT_EQ(kTestSize.ToString(),
widget->GetWindowBoundsInScreen().size().ToString());
EXPECT_NE(kTestBounds.ToString(),
widget->GetWindowBoundsInScreen().ToString());
widget->SetBounds(kTestBounds);
EXPECT_EQ(kTestBounds.ToString(),
widget->GetWindowBoundsInScreen().ToString());
// For a frameless widget, the client bounds should also match.
EXPECT_EQ(kTestBounds.ToString(),
widget->GetClientAreaBoundsInScreen().ToString());
// Verify origin is stable for a frameless window as well.
widget->SetSize(kTestSize);
EXPECT_EQ(kTestBounds.origin().ToString(),
widget->GetWindowBoundsInScreen().origin().ToString());
}
// Chrome OS widgets need the shell to maximize/fullscreen window.
// Disable on desktop Linux because windows restore to the wrong bounds.
// See http://crbug.com/515369.
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
#define MAYBE_GetRestoredBounds DISABLED_GetRestoredBounds
#else
#define MAYBE_GetRestoredBounds GetRestoredBounds
#endif
// Test that GetRestoredBounds() returns the original bounds of the window.
TEST_F(DesktopWidgetTest, MAYBE_GetRestoredBounds) {
WidgetAutoclosePtr toplevel(CreateTopLevelNativeWidget());
toplevel->Show();
// Initial restored bounds have non-zero size.
EXPECT_FALSE(toplevel->GetRestoredBounds().IsEmpty());
const gfx::Rect bounds(100, 100, 200, 200);
toplevel->SetBounds(bounds);
EXPECT_EQ(bounds, toplevel->GetWindowBoundsInScreen());
EXPECT_EQ(bounds, toplevel->GetRestoredBounds());
toplevel->Maximize();
RunPendingMessages();
#if BUILDFLAG(IS_MAC)
// Current expectation on Mac is to do nothing on Maximize.
EXPECT_EQ(toplevel->GetWindowBoundsInScreen(), toplevel->GetRestoredBounds());
#else
EXPECT_NE(toplevel->GetWindowBoundsInScreen(), toplevel->GetRestoredBounds());
#endif
EXPECT_EQ(bounds, toplevel->GetRestoredBounds());
toplevel->Restore();
RunPendingMessages();
EXPECT_EQ(bounds, toplevel->GetWindowBoundsInScreen());
EXPECT_EQ(bounds, toplevel->GetRestoredBounds());
toplevel->SetFullscreen(true);
RunPendingMessages();
EXPECT_NE(toplevel->GetWindowBoundsInScreen(), toplevel->GetRestoredBounds());
EXPECT_EQ(bounds, toplevel->GetRestoredBounds());
toplevel->SetFullscreen(false);
RunPendingMessages();
EXPECT_EQ(bounds, toplevel->GetWindowBoundsInScreen());
EXPECT_EQ(bounds, toplevel->GetRestoredBounds());
}
// The key-event propagation from Widget happens differently on aura and
// non-aura systems because of the difference in IME. So this test works only on
// aura.
TEST_F(WidgetTest, KeyboardInputEvent) {
WidgetAutoclosePtr toplevel(CreateTopLevelPlatformWidget());
View* container = toplevel->client_view();
Textfield* textfield = new Textfield();
textfield->SetText(u"some text");
container->AddChildViewRaw(textfield);
toplevel->Show();
textfield->RequestFocus();
// The press gets handled. The release doesn't have an effect.
ui::KeyEvent backspace_p(ui::EventType::kKeyPressed, ui::VKEY_DELETE,
ui::EF_NONE);
toplevel->OnKeyEvent(&backspace_p);
EXPECT_TRUE(backspace_p.stopped_propagation());
ui::KeyEvent backspace_r(ui::EventType::kKeyReleased, ui::VKEY_DELETE,
ui::EF_NONE);
toplevel->OnKeyEvent(&backspace_r);
EXPECT_FALSE(backspace_r.handled());
}
TEST_F(WidgetTest, BubbleControlsResetOnInit) {
std::unique_ptr<Widget> anchor = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
anchor->Show();
{
auto bubble_delegate = std::make_unique<WidgetTestBubbleDialogDelegateView>(
anchor->client_view());
auto* bubble_delegate_ptr = bubble_delegate.get();
std::unique_ptr<Widget> bubble_widget =
base::WrapUnique(BubbleDialogDelegateView::CreateBubble(
bubble_delegate.release(), Widget::InitParams::CLIENT_OWNS_WIDGET));
EXPECT_TRUE(bubble_delegate_ptr->reset_controls_called_);
bubble_widget->Show();
}
anchor->Hide();
}
#if BUILDFLAG(IS_WIN)
// Test to ensure that after minimize, view width is set to zero. This is only
// the case for desktop widgets on Windows. Other platforms retain the window
// size while minimized.
TEST_F(DesktopWidgetTest, TestViewWidthAfterMinimizingWidget) {
// Create a widget.
std::unique_ptr<Widget> widget = CreateTestWidget(
Widget::InitParams::CLIENT_OWNS_WIDGET, Widget::InitParams::TYPE_WINDOW);
NonClientView* non_client_view = widget->non_client_view();
non_client_view->SetFrameView(
std::make_unique<MinimumSizeFrameView>(widget.get()));
// Setting the frame view doesn't do a layout, so force one.
non_client_view->InvalidateLayout();
views::test::RunScheduledLayout(non_client_view);
widget->Show();
EXPECT_NE(0, non_client_view->frame_view()->width());
widget->Minimize();
EXPECT_EQ(0, non_client_view->frame_view()->width());
}
#endif
// Desktop native widget Aura tests are for non Chrome OS platforms.
// This class validates whether paints are received for a visible Widget.
// It observes Widget visibility and Close() and tracks whether subsequent
// paints are expected.
class DesktopAuraTestValidPaintWidget : public Widget, public WidgetObserver {
public:
explicit DesktopAuraTestValidPaintWidget(Widget::InitParams init_params)
: Widget(std::move(init_params)) {
observation_.Observe(this);
}
DesktopAuraTestValidPaintWidget(const DesktopAuraTestValidPaintWidget&) =
delete;
DesktopAuraTestValidPaintWidget& operator=(
const DesktopAuraTestValidPaintWidget&) = delete;
~DesktopAuraTestValidPaintWidget() override = default;
bool ReadReceivedPaintAndReset() {
return std::exchange(received_paint_, false);
}
bool received_paint_while_hidden() const {
return received_paint_while_hidden_;
}
void WaitUntilPaint() {
if (received_paint_) {
return;
}
base::RunLoop runloop;
quit_closure_ = runloop.QuitClosure();
runloop.Run();
quit_closure_.Reset();
}
// WidgetObserver:
void OnWidgetDestroying(Widget* widget) override { expect_paint_ = false; }
void OnNativeWidgetPaint(const ui::PaintContext& context) override {
received_paint_ = true;
EXPECT_TRUE(expect_paint_);
if (!expect_paint_) {
received_paint_while_hidden_ = true;
}
Widget::OnNativeWidgetPaint(context);
if (!quit_closure_.is_null()) {
std::move(quit_closure_).Run();
}
}
void OnWidgetVisibilityChanged(Widget* widget, bool visible) override {
expect_paint_ = visible;
}
private:
bool received_paint_ = false;
bool expect_paint_ = true;
bool received_paint_while_hidden_ = false;
base::OnceClosure quit_closure_;
base::ScopedObservation<Widget, WidgetObserver> observation_{this};
};
namespace {
class ContentsView : public View {
METADATA_HEADER(ContentsView, View)
public:
ContentsView() {
GetViewAccessibility().SetRole(ax::mojom::Role::kDialog);
GetViewAccessibility().SetName(
std::string(), ax::mojom::NameFrom::kAttributeExplicitlyEmpty);
}
};
BEGIN_METADATA(ContentsView)
END_METADATA
} // namespace
class DesktopAuraPaintWidgetTest : public DesktopWidgetTest {
public:
std::unique_ptr<DesktopAuraTestValidPaintWidget>
CreateDesktopAuraTestValidPaintWidget(
Widget::InitParams::Type type =
Widget::InitParams::TYPE_WINDOW_FRAMELESS) {
auto widget = std::make_unique<DesktopAuraTestValidPaintWidget>(
CreateParamsForTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET,
type));
View* contents_view =
widget->SetContentsView(std::make_unique<ContentsView>());
contents_view->SetFocusBehavior(View::FocusBehavior::ALWAYS);
widget->Show();
widget->Activate();
return widget;
}
};
TEST_F(DesktopAuraPaintWidgetTest, DesktopNativeWidgetNoPaintAfterCloseTest) {
auto widget = CreateDesktopAuraTestValidPaintWidget();
widget->WaitUntilPaint();
EXPECT_TRUE(widget->ReadReceivedPaintAndReset());
widget->SchedulePaintInRect(widget->GetRestoredBounds());
widget->Close();
RunPendingMessages();
EXPECT_FALSE(widget->ReadReceivedPaintAndReset());
EXPECT_FALSE(widget->received_paint_while_hidden());
}
TEST_F(DesktopAuraPaintWidgetTest, DesktopNativeWidgetNoPaintAfterHideTest) {
auto widget = CreateDesktopAuraTestValidPaintWidget();
widget->WaitUntilPaint();
EXPECT_TRUE(widget->ReadReceivedPaintAndReset());
widget->SchedulePaintInRect(widget->GetRestoredBounds());
widget->Hide();
RunPendingMessages();
EXPECT_FALSE(widget->ReadReceivedPaintAndReset());
EXPECT_FALSE(widget->received_paint_while_hidden());
widget->Close();
}
// Test to ensure that the aura Window's visibility state is set to visible if
// the underlying widget is hidden and then shown.
TEST_F(DesktopWidgetTest, TestWindowVisibilityAfterHide) {
// Create a widget.
std::unique_ptr<Widget> widget = CreateTestWidget(
Widget::InitParams::CLIENT_OWNS_WIDGET, Widget::InitParams::TYPE_WINDOW);
NonClientView* non_client_view = widget->non_client_view();
non_client_view->SetFrameView(
std::make_unique<MinimumSizeFrameView>(widget.get()));
widget->Show();
EXPECT_TRUE(IsNativeWindowVisible(widget->GetNativeWindow()));
widget->Hide();
EXPECT_FALSE(IsNativeWindowVisible(widget->GetNativeWindow()));
widget->Show();
EXPECT_TRUE(IsNativeWindowVisible(widget->GetNativeWindow()));
}
// Tests that wheel events generated from scroll events are targeted to the
// views under the cursor when the focused view does not processed them.
TEST_F(WidgetTest, WheelEventsFromScrollEventTarget) {
EventCountView* cursor_view = new EventCountView;
cursor_view->SetBounds(60, 0, 50, 40);
WidgetAutoclosePtr widget(CreateTopLevelPlatformWidget());
widget->GetRootView()->AddChildViewRaw(cursor_view);
// Generate a scroll event on the cursor view.
ui::ScrollEvent scroll(ui::EventType::kScroll, gfx::Point(65, 5),
ui::EventTimeForNow(), 0, 0, 20, 0, 20, 2);
widget->OnScrollEvent(&scroll);
EXPECT_EQ(1, cursor_view->GetEventCount(ui::EventType::kScroll));
EXPECT_EQ(1, cursor_view->GetEventCount(ui::EventType::kMousewheel));
cursor_view->ResetCounts();
ui::ScrollEvent scroll2(ui::EventType::kScroll, gfx::Point(5, 5),
ui::EventTimeForNow(), 0, 0, 20, 0, 20, 2);
widget->OnScrollEvent(&scroll2);
EXPECT_EQ(0, cursor_view->GetEventCount(ui::EventType::kScroll));
EXPECT_EQ(0, cursor_view->GetEventCount(ui::EventType::kMousewheel));
}
// Tests that if a scroll-begin gesture is not handled, then subsequent scroll
// events are not dispatched to any view.
TEST_F(WidgetTest, GestureScrollEventDispatching) {
EventCountView* noscroll_view = new EventCountView;
EventCountView* scroll_view = new ScrollableEventCountView;
noscroll_view->SetBounds(0, 0, 50, 40);
scroll_view->SetBounds(60, 0, 40, 40);
WidgetAutoclosePtr widget(CreateTopLevelPlatformWidget());
widget->GetRootView()->AddChildViewRaw(noscroll_view);
widget->GetRootView()->AddChildViewRaw(scroll_view);
{
ui::GestureEvent begin =
CreateTestGestureEvent(ui::EventType::kGestureScrollBegin, 5, 5);
widget->OnGestureEvent(&begin);
ui::GestureEvent update = CreateTestGestureEvent(
ui::GestureEventDetails(ui::EventType::kGestureScrollUpdate, 20, 10),
25, 15);
widget->OnGestureEvent(&update);
ui::GestureEvent end =
CreateTestGestureEvent(ui::EventType::kGestureScrollEnd, 25, 15);
widget->OnGestureEvent(&end);
EXPECT_EQ(1,
noscroll_view->GetEventCount(ui::EventType::kGestureScrollBegin));
EXPECT_EQ(
0, noscroll_view->GetEventCount(ui::EventType::kGestureScrollUpdate));
EXPECT_EQ(0,
noscroll_view->GetEventCount(ui::EventType::kGestureScrollEnd));
}
{
ui::GestureEvent begin =
CreateTestGestureEvent(ui::EventType::kGestureScrollBegin, 65, 5);
widget->OnGestureEvent(&begin);
ui::GestureEvent update = CreateTestGestureEvent(
ui::GestureEventDetails(ui::EventType::kGestureScrollUpdate, 20, 10),
85, 15);
widget->OnGestureEvent(&update);
ui::GestureEvent end =
CreateTestGestureEvent(ui::EventType::kGestureScrollEnd, 85, 15);
widget->OnGestureEvent(&end);
EXPECT_EQ(1,
scroll_view->GetEventCount(ui::EventType::kGestureScrollBegin));
EXPECT_EQ(1,
scroll_view->GetEventCount(ui::EventType::kGestureScrollUpdate));
EXPECT_EQ(1, scroll_view->GetEventCount(ui::EventType::kGestureScrollEnd));
}
}
// Tests that event-handlers installed on the RootView get triggered correctly.
// TODO(tdanderson): Clean up this test as part of crbug.com/355680.
TEST_F(WidgetTest, EventHandlersOnRootView) {
WidgetAutoclosePtr widget(CreateTopLevelNativeWidget());
View* root_view = widget->GetRootView();
EventCountView* view =
root_view->AddChildView(std::make_unique<EventCountView>());
view->SetBounds(0, 0, 20, 20);
EventCountHandler h1;
root_view->AddPreTargetHandler(&h1);
EventCountHandler h2;
root_view->AddPostTargetHandler(&h2);
widget->SetBounds(gfx::Rect(0, 0, 100, 100));
widget->Show();
// Dispatch a ui::EventType::kScroll event. The event remains unhandled and
// should bubble up the views hierarchy to be re-dispatched on the root view.
ui::ScrollEvent scroll(ui::EventType::kScroll, gfx::Point(5, 5),
ui::EventTimeForNow(), 0, 0, 20, 0, 20, 2);
widget->OnScrollEvent(&scroll);
EXPECT_EQ(2, h1.GetEventCount(ui::EventType::kScroll));
EXPECT_EQ(1, view->GetEventCount(ui::EventType::kScroll));
EXPECT_EQ(2, h2.GetEventCount(ui::EventType::kScroll));
// Unhandled scroll events are turned into wheel events and re-dispatched.
EXPECT_EQ(1, h1.GetEventCount(ui::EventType::kMousewheel));
EXPECT_EQ(1, view->GetEventCount(ui::EventType::kMousewheel));
EXPECT_EQ(1, h2.GetEventCount(ui::EventType::kMousewheel));
h1.ResetCounts();
view->ResetCounts();
h2.ResetCounts();
// Dispatch a ui::EventType::kScrollFlingStart event. The event remains
// unhandled and should bubble up the views hierarchy to be re-dispatched on
// the root view.
ui::ScrollEvent fling(ui::EventType::kScrollFlingStart, gfx::Point(5, 5),
ui::EventTimeForNow(), 0, 0, 20, 0, 20, 2);
widget->OnScrollEvent(&fling);
EXPECT_EQ(2, h1.GetEventCount(ui::EventType::kScrollFlingStart));
EXPECT_EQ(1, view->GetEventCount(ui::EventType::kScrollFlingStart));
EXPECT_EQ(2, h2.GetEventCount(ui::EventType::kScrollFlingStart));
// Unhandled scroll events which are not of type ui::EventType::kScroll should
// not be turned into wheel events and re-dispatched.
EXPECT_EQ(0, h1.GetEventCount(ui::EventType::kMousewheel));
EXPECT_EQ(0, view->GetEventCount(ui::EventType::kMousewheel));
EXPECT_EQ(0, h2.GetEventCount(ui::EventType::kMousewheel));
h1.ResetCounts();
view->ResetCounts();
h2.ResetCounts();
// Change the handle mode of |view| so that events are marked as handled at
// the target phase.
view->set_handle_mode(EventCountView::HandleMode::kConsumeEvents);
// Dispatch a ui::EventType::kGestureTapDown and a
// ui::EventType::kGestureTapCancel event. The events are handled at the
// target phase and should not reach the post-target handler.
ui::GestureEvent tap_down =
CreateTestGestureEvent(ui::EventType::kGestureTapDown, 5, 5);
widget->OnGestureEvent(&tap_down);
EXPECT_EQ(1, h1.GetEventCount(ui::EventType::kGestureTapDown));
EXPECT_EQ(1, view->GetEventCount(ui::EventType::kGestureTapDown));
EXPECT_EQ(0, h2.GetEventCount(ui::EventType::kGestureTapDown));
ui::GestureEvent tap_cancel =
CreateTestGestureEvent(ui::EventType::kGestureTapCancel, 5, 5);
widget->OnGestureEvent(&tap_cancel);
EXPECT_EQ(1, h1.GetEventCount(ui::EventType::kGestureTapCancel));
EXPECT_EQ(1, view->GetEventCount(ui::EventType::kGestureTapCancel));
EXPECT_EQ(0, h2.GetEventCount(ui::EventType::kGestureTapCancel));
h1.ResetCounts();
view->ResetCounts();
h2.ResetCounts();
// Dispatch a ui::EventType::kScroll event. The event is handled at the target
// phase and should not reach the post-target handler.
ui::ScrollEvent consumed_scroll(ui::EventType::kScroll, gfx::Point(5, 5),
ui::EventTimeForNow(), 0, 0, 20, 0, 20, 2);
widget->OnScrollEvent(&consumed_scroll);
EXPECT_EQ(1, h1.GetEventCount(ui::EventType::kScroll));
EXPECT_EQ(1, view->GetEventCount(ui::EventType::kScroll));
EXPECT_EQ(0, h2.GetEventCount(ui::EventType::kScroll));
// Handled scroll events are not turned into wheel events and re-dispatched.
EXPECT_EQ(0, h1.GetEventCount(ui::EventType::kMousewheel));
EXPECT_EQ(0, view->GetEventCount(ui::EventType::kMousewheel));
EXPECT_EQ(0, h2.GetEventCount(ui::EventType::kMousewheel));
root_view->RemovePreTargetHandler(&h1);
}
TEST_F(WidgetTest, SynthesizeMouseMoveEvent) {
WidgetAutoclosePtr widget(CreateTopLevelNativeWidget());
View* root_view = widget->GetRootView();
widget->SetBounds(gfx::Rect(0, 0, 100, 100));
EventCountView* v1 =
root_view->AddChildView(std::make_unique<EventCountView>());
v1->SetBounds(5, 5, 10, 10);
EventCountView* v2 =
root_view->AddChildView(std::make_unique<EventCountView>());
v2->SetBounds(5, 15, 10, 10);
widget->Show();
// SynthesizeMouseMoveEvent does nothing until the mouse is entered.
widget->SynthesizeMouseMoveEvent();
EXPECT_EQ(0, v1->GetEventCount(ui::EventType::kMouseMoved));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kMouseMoved));
gfx::Point cursor_location(v1->GetBoundsInScreen().CenterPoint());
auto generator =
CreateEventGenerator(GetContext(), widget->GetNativeWindow());
generator->MoveMouseTo(cursor_location);
EXPECT_EQ(1, v1->GetEventCount(ui::EventType::kMouseMoved));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kMouseMoved));
// SynthesizeMouseMoveEvent dispatches an mousemove event.
widget->SynthesizeMouseMoveEvent();
EXPECT_EQ(2, v1->GetEventCount(ui::EventType::kMouseMoved));
root_view->RemoveChildViewT(v1);
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kMouseMoved));
v2->SetBounds(5, 5, 10, 10);
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kMouseMoved));
widget->SynthesizeMouseMoveEvent();
EXPECT_EQ(1, v2->GetEventCount(ui::EventType::kMouseMoved));
}
namespace {
// ui::EventHandler which handles all mouse press events.
class MousePressEventConsumer : public ui::EventHandler {
public:
MousePressEventConsumer() = default;
MousePressEventConsumer(const MousePressEventConsumer&) = delete;
MousePressEventConsumer& operator=(const MousePressEventConsumer&) = delete;
private:
// ui::EventHandler:
void OnMouseEvent(ui::MouseEvent* event) override {
if (event->type() == ui::EventType::kMousePressed) {
event->SetHandled();
}
}
};
} // namespace
// No touch on desktop Mac. Tracked in http://crbug.com/445520.
#if !BUILDFLAG(IS_MAC) || defined(USE_AURA)
// Test that mouse presses and mouse releases are dispatched normally when a
// touch is down.
TEST_F(WidgetTest, MouseEventDispatchWhileTouchIsDown) {
Widget* widget = CreateTopLevelNativeWidget();
widget->Show();
widget->SetSize(gfx::Size(300, 300));
EventCountView* event_count_view =
widget->GetRootView()->AddChildView(std::make_unique<EventCountView>());
event_count_view->SetBounds(0, 0, 300, 300);
MousePressEventConsumer consumer;
event_count_view->AddPostTargetHandler(&consumer);
auto generator =
CreateEventGenerator(GetContext(), widget->GetNativeWindow());
generator->PressTouch();
generator->ClickLeftButton();
EXPECT_EQ(1, event_count_view->GetEventCount(ui::EventType::kMousePressed));
EXPECT_EQ(1, event_count_view->GetEventCount(ui::EventType::kMouseReleased));
// For mus it's important we destroy the widget before the EventGenerator.
widget->CloseNow();
}
#endif // !BUILDFLAG(IS_MAC) || defined(USE_AURA)
// Tests that when there is no active capture, that a mouse press causes capture
// to be set.
TEST_F(WidgetTest, MousePressCausesCapture) {
Widget* widget = CreateTopLevelNativeWidget();
widget->Show();
widget->SetSize(gfx::Size(300, 300));
EventCountView* event_count_view =
widget->GetRootView()->AddChildView(std::make_unique<EventCountView>());
event_count_view->SetBounds(0, 0, 300, 300);
// No capture has been set.
EXPECT_EQ(gfx::NativeView(), internal::NativeWidgetPrivate::GetGlobalCapture(
widget->GetNativeView()));
MousePressEventConsumer consumer;
event_count_view->AddPostTargetHandler(&consumer);
auto generator =
CreateEventGenerator(GetContext(), widget->GetNativeWindow());
generator->MoveMouseTo(widget->GetClientAreaBoundsInScreen().CenterPoint());
generator->PressLeftButton();
EXPECT_EQ(1, event_count_view->GetEventCount(ui::EventType::kMousePressed));
EXPECT_EQ(
widget->GetNativeView(),
internal::NativeWidgetPrivate::GetGlobalCapture(widget->GetNativeView()));
// For mus it's important we destroy the widget before the EventGenerator.
widget->CloseNow();
}
namespace {
// An EventHandler which shows a Widget upon receiving a mouse event. The Widget
// proceeds to take capture.
class CaptureEventConsumer : public ui::EventHandler {
public:
explicit CaptureEventConsumer(Widget* widget) : widget_(widget) {}
CaptureEventConsumer(const CaptureEventConsumer&) = delete;
CaptureEventConsumer& operator=(const CaptureEventConsumer&) = delete;
~CaptureEventConsumer() override { widget_.ExtractAsDangling()->CloseNow(); }
private:
// ui::EventHandler:
void OnMouseEvent(ui::MouseEvent* event) override {
if (event->type() == ui::EventType::kMousePressed) {
event->SetHandled();
widget_->Show();
widget_->SetSize(gfx::Size(200, 200));
auto event_count_view = std::make_unique<EventCountView>();
event_count_view->SetBounds(0, 0, 200, 200);
widget_->SetCapture(
widget_->GetRootView()->AddChildView(std::move(event_count_view)));
}
}
raw_ptr<Widget> widget_;
};
} // namespace
// Tests that if explicit capture occurs during a mouse press, that implicit
// capture is not applied.
TEST_F(WidgetTest, CaptureDuringMousePressNotOverridden) {
Widget* widget = CreateTopLevelNativeWidget();
widget->Show();
widget->SetSize(gfx::Size(300, 300));
EventCountView* event_count_view =
widget->GetRootView()->AddChildView(std::make_unique<EventCountView>());
event_count_view->SetBounds(0, 0, 300, 300);
EXPECT_EQ(gfx::NativeView(), internal::NativeWidgetPrivate::GetGlobalCapture(
widget->GetNativeView()));
Widget* widget2 = CreateTopLevelNativeWidget();
// Gives explicit capture to |widget2|
CaptureEventConsumer consumer(widget2);
event_count_view->AddPostTargetHandler(&consumer);
auto generator =
CreateEventGenerator(GetRootWindow(widget), widget->GetNativeWindow());
generator->MoveMouseTo(widget->GetClientAreaBoundsInScreen().CenterPoint());
// This event should implicitly give capture to |widget|, except that
// |consumer| will explicitly set capture on |widget2|.
generator->PressLeftButton();
EXPECT_EQ(1, event_count_view->GetEventCount(ui::EventType::kMousePressed));
EXPECT_NE(
widget->GetNativeView(),
internal::NativeWidgetPrivate::GetGlobalCapture(widget->GetNativeView()));
EXPECT_EQ(
widget2->GetNativeView(),
internal::NativeWidgetPrivate::GetGlobalCapture(widget->GetNativeView()));
// For mus it's important we destroy the widget before the EventGenerator.
widget->CloseNow();
}
class ClosingEventObserver : public ui::EventObserver {
public:
explicit ClosingEventObserver(Widget* widget) : widget_(widget) {}
ClosingEventObserver(const ClosingEventObserver&) = delete;
ClosingEventObserver& operator=(const ClosingEventObserver&) = delete;
// ui::EventObserver:
void OnEvent(const ui::Event& event) override {
// Guard against attempting to close the widget twice.
if (widget_) {
widget_.ExtractAsDangling()->CloseNow();
}
}
private:
raw_ptr<Widget, DanglingUntriaged> widget_;
};
class ClosingView : public View {
METADATA_HEADER(ClosingView, View)
public:
explicit ClosingView(Widget* widget) : widget_(widget) {}
ClosingView(const ClosingView&) = delete;
ClosingView& operator=(const ClosingView&) = delete;
// View:
void OnEvent(ui::Event* event) override {
// Guard against closing twice and writing to freed memory.
if (widget_ && event->type() == ui::EventType::kMousePressed) {
Widget* widget = widget_;
widget_ = nullptr;
widget->CloseNow();
}
}
private:
raw_ptr<Widget> widget_;
};
BEGIN_METADATA(ClosingView)
END_METADATA
// Ensures that when multiple objects are intercepting OS-level events, that one
// can safely close a Widget that has capture.
TEST_F(WidgetTest, DestroyedWithCaptureViaEventMonitor) {
Widget* widget = CreateTopLevelNativeWidget();
TestWidgetObserver observer(widget);
widget->Show();
widget->SetSize(gfx::Size(300, 300));
// ClosingView and ClosingEventObserver both try to close the Widget. On Mac
// the order that EventMonitors receive OS events is not deterministic. If the
// one installed via SetCapture() sees it first, the event is swallowed (so
// both need to try). Note the regression test would only fail when the
// SetCapture() handler did _not_ swallow the event, but it still needs to try
// to close the Widget otherwise it will be left open, which fails elsewhere.
ClosingView* closing_view = widget->GetContentsView()->AddChildView(
std::make_unique<ClosingView>(widget));
widget->SetCapture(closing_view);
ClosingEventObserver closing_event_observer(widget);
auto monitor = EventMonitor::CreateApplicationMonitor(
&closing_event_observer, widget->GetNativeWindow(),
{ui::EventType::kMousePressed});
auto generator =
CreateEventGenerator(GetContext(), widget->GetNativeWindow());
generator->set_target(ui::test::EventGenerator::Target::APPLICATION);
EXPECT_FALSE(observer.widget_closed());
generator->PressLeftButton();
EXPECT_TRUE(observer.widget_closed());
}
TEST_F(WidgetTest, LockPaintAsActive) {
WidgetAutoclosePtr widget(CreateTopLevelPlatformWidget());
widget->ShowInactive();
EXPECT_FALSE(widget->ShouldPaintAsActive());
// First lock causes widget to paint as active.
auto lock = widget->LockPaintAsActive();
EXPECT_TRUE(widget->ShouldPaintAsActive());
// Second lock has no effect.
auto lock2 = widget->LockPaintAsActive();
EXPECT_TRUE(widget->ShouldPaintAsActive());
// Have to release twice to get back to inactive state.
lock2.reset();
EXPECT_TRUE(widget->ShouldPaintAsActive());
lock.reset();
EXPECT_FALSE(widget->ShouldPaintAsActive());
}
TEST_F(WidgetTest, LockPaintAsActive_AlreadyActive) {
WidgetAutoclosePtr widget(CreateTopLevelPlatformWidget());
widget->Show();
EXPECT_TRUE(widget->ShouldPaintAsActive());
// Lock has no effect.
auto lock = widget->LockPaintAsActive();
EXPECT_TRUE(widget->ShouldPaintAsActive());
// Remove lock has no effect.
lock.reset();
EXPECT_TRUE(widget->ShouldPaintAsActive());
}
TEST_F(WidgetTest, LockPaintAsActive_BecomesActive) {
WidgetAutoclosePtr widget(CreateTopLevelPlatformWidget());
widget->ShowInactive();
EXPECT_FALSE(widget->ShouldPaintAsActive());
// Lock toggles render mode.
auto lock = widget->LockPaintAsActive();
EXPECT_TRUE(widget->ShouldPaintAsActive());
widget->Activate();
// Remove lock has no effect.
lock.reset();
EXPECT_TRUE(widget->ShouldPaintAsActive());
}
class PaintAsActiveCallbackCounter {
public:
explicit PaintAsActiveCallbackCounter(Widget* widget) {
// Subscribe to |widget|'s paint-as-active change.
paint_as_active_subscription_ =
widget->RegisterPaintAsActiveChangedCallback(base::BindRepeating(
&PaintAsActiveCallbackCounter::Call, base::Unretained(this)));
}
void Call() { count_++; }
int CallCount() { return count_; }
private:
int count_ = 0;
base::CallbackListSubscription paint_as_active_subscription_;
};
TEST_F(WidgetTest, LockParentPaintAsActive) {
if constexpr (!PlatformStyle::kInactiveWidgetControlsAppearDisabled) {
return;
}
WidgetAutoclosePtr parent(CreateTopLevelPlatformWidget());
WidgetAutoclosePtr child(CreateChildPlatformWidget(parent->GetNativeView()));
WidgetAutoclosePtr grandchild(
CreateChildPlatformWidget(child->GetNativeView()));
WidgetAutoclosePtr other(CreateTopLevelPlatformWidget());
child->widget_delegate()->SetCanActivate(true);
grandchild->widget_delegate()->SetCanActivate(true);
PaintAsActiveCallbackCounter parent_control(parent.get());
PaintAsActiveCallbackCounter child_control(child.get());
PaintAsActiveCallbackCounter grandchild_control(grandchild.get());
PaintAsActiveCallbackCounter other_control(other.get());
parent->Show();
EXPECT_TRUE(parent->ShouldPaintAsActive());
EXPECT_TRUE(child->ShouldPaintAsActive());
EXPECT_TRUE(grandchild->ShouldPaintAsActive());
EXPECT_FALSE(other->ShouldPaintAsActive());
EXPECT_EQ(parent_control.CallCount(), 1);
EXPECT_EQ(child_control.CallCount(), 1);
EXPECT_EQ(grandchild_control.CallCount(), 1);
EXPECT_EQ(other_control.CallCount(), 0);
other->Show();
EXPECT_FALSE(parent->ShouldPaintAsActive());
EXPECT_FALSE(child->ShouldPaintAsActive());
EXPECT_FALSE(grandchild->ShouldPaintAsActive());
EXPECT_TRUE(other->ShouldPaintAsActive());
EXPECT_EQ(parent_control.CallCount(), 2);
EXPECT_EQ(child_control.CallCount(), 2);
EXPECT_EQ(grandchild_control.CallCount(), 2);
EXPECT_EQ(other_control.CallCount(), 1);
child->Show();
EXPECT_TRUE(parent->ShouldPaintAsActive());
EXPECT_TRUE(child->ShouldPaintAsActive());
EXPECT_TRUE(grandchild->ShouldPaintAsActive());
EXPECT_FALSE(other->ShouldPaintAsActive());
EXPECT_EQ(parent_control.CallCount(), 3);
EXPECT_EQ(child_control.CallCount(), 3);
EXPECT_EQ(grandchild_control.CallCount(), 3);
EXPECT_EQ(other_control.CallCount(), 2);
other->Show();
EXPECT_FALSE(parent->ShouldPaintAsActive());
EXPECT_FALSE(child->ShouldPaintAsActive());
EXPECT_FALSE(grandchild->ShouldPaintAsActive());
EXPECT_TRUE(other->ShouldPaintAsActive());
EXPECT_EQ(parent_control.CallCount(), 4);
EXPECT_EQ(child_control.CallCount(), 4);
EXPECT_EQ(grandchild_control.CallCount(), 4);
EXPECT_EQ(other_control.CallCount(), 3);
grandchild->Show();
EXPECT_TRUE(parent->ShouldPaintAsActive());
EXPECT_TRUE(child->ShouldPaintAsActive());
EXPECT_TRUE(grandchild->ShouldPaintAsActive());
EXPECT_FALSE(other->ShouldPaintAsActive());
EXPECT_EQ(parent_control.CallCount(), 5);
EXPECT_EQ(child_control.CallCount(), 5);
EXPECT_EQ(grandchild_control.CallCount(), 5);
EXPECT_EQ(other_control.CallCount(), 4);
}
// Tests to make sure that child widgets do not cause their parent widget to
// paint inactive immediately when they are closed. This avoids having the
// parent paint as inactive in the time between when the bubble is closed and
// when it's eventually destroyed by its native widget (see crbug.com/1303549).
TEST_F(DesktopWidgetTest,
ClosingActiveChildDoesNotPrematurelyPaintParentInactive) {
// top_level_widget that owns the bubble widget.
auto top_level_widget =
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
top_level_widget->Show();
// Create the child bubble widget.
auto bubble_widget = std::make_unique<Widget>();
Widget::InitParams init_params = CreateParamsForTestWidget(
Widget::InitParams::CLIENT_OWNS_WIDGET, Widget::InitParams::TYPE_BUBBLE);
init_params.parent = top_level_widget->GetNativeView();
bubble_widget->Init(std::move(init_params));
bubble_widget->Show();
EXPECT_TRUE(bubble_widget->ShouldPaintAsActive());
EXPECT_TRUE(top_level_widget->ShouldPaintAsActive());
// Closing the bubble wiget should not immediately cause the top level widget
// to paint inactive.
PaintAsActiveCallbackCounter top_level_counter(top_level_widget.get());
PaintAsActiveCallbackCounter bubble_counter(bubble_widget.get());
bubble_widget->Close();
EXPECT_FALSE(bubble_widget->ShouldPaintAsActive());
EXPECT_TRUE(top_level_widget->ShouldPaintAsActive());
EXPECT_EQ(top_level_counter.CallCount(), 0);
EXPECT_EQ(bubble_counter.CallCount(), 0);
}
// Tests that there is no crash when paint as active lock is removed for child
// widget while its parent widget is being closed.
TEST_F(DesktopWidgetTest, LockPaintAsActiveAndCloseParent) {
// Make sure that DesktopNativeWidgetAura is used for widgets.
test_views_delegate()->set_use_desktop_native_widgets(true);
std::unique_ptr<Widget> parent =
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
parent->Show();
auto delegate = std::make_unique<TestDesktopWidgetDelegate>();
Widget::InitParams params = CreateParams(
Widget::InitParams::CLIENT_OWNS_WIDGET, Widget::InitParams::TYPE_WINDOW);
params.parent = parent->GetNativeView();
delegate->InitWidget(std::move(params));
delegate->RegisterDeleteDelegateCallback(
WidgetDelegate::RegisterDeleteCallbackPassKey(),
base::DoNothingWithBoundArgs(delegate->GetWidget()->LockPaintAsActive()));
base::WeakPtr<Widget> child = delegate->GetWidget()->GetWeakPtr();
child->ShowInactive();
// Child widget and its delegate are destroyed when the parent widget is being
// closed. PaintAsActiveTestDesktopWidgetDelegate::paint_as_active_lock_ is
// also deleted which should not cause a crash.
parent->CloseNow();
// Ensure that child widget has been destroyed.
ASSERT_TRUE(child && child->IsClosed());
}
// Widget used to destroy itself when OnNativeWidgetDestroyed is called.
class TestNativeWidgetDestroyedWidget : public Widget {
public:
// Overridden from NativeWidgetDelegate:
void OnNativeWidgetDestroyed() override;
};
void TestNativeWidgetDestroyedWidget::OnNativeWidgetDestroyed() {
Widget::OnNativeWidgetDestroyed();
delete this;
}
// Verifies that widget destroyed itself in OnNativeWidgetDestroyed does not
// crash in ASan.
TEST_F(DesktopWidgetTest, WidgetDestroyedItselfDoesNotCrash) {
TestDesktopWidgetDelegate delegate(new TestNativeWidgetDestroyedWidget);
delegate.InitWidget(
CreateParamsForTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET,
Widget::InitParams::TYPE_WINDOW_FRAMELESS));
delegate.GetWidget()->Show();
delegate.GetWidget()->CloseNow();
}
// Verifies WindowClosing() is invoked correctly on the delegate when a Widget
// is closed.
TEST_F(DesktopWidgetTest, SingleWindowClosing) {
TestDesktopWidgetDelegate delegate;
delegate.InitWidget(CreateParams(Widget::InitParams::CLIENT_OWNS_WIDGET,
Widget::InitParams::TYPE_WINDOW));
EXPECT_EQ(0, delegate.window_closing_count());
delegate.GetWidget()->CloseNow();
EXPECT_EQ(1, delegate.window_closing_count());
}
TEST_F(DesktopWidgetTest, CloseRequested_AllowsClose) {
constexpr Widget::ClosedReason kReason = Widget::ClosedReason::kLostFocus;
TestDesktopWidgetDelegate delegate;
delegate.set_can_close(true);
delegate.InitWidget(CreateParams(Widget::InitParams::CLIENT_OWNS_WIDGET,
Widget::InitParams::TYPE_WINDOW));
WidgetDestroyedWaiter waiter(delegate.GetWidget());
delegate.GetWidget()->CloseWithReason(kReason);
EXPECT_TRUE(delegate.GetWidget()->IsClosed());
EXPECT_EQ(kReason, delegate.GetWidget()->closed_reason());
EXPECT_EQ(kReason, delegate.last_closed_reason());
waiter.Wait();
}
TEST_F(DesktopWidgetTest, CloseRequested_DisallowClose) {
constexpr Widget::ClosedReason kReason = Widget::ClosedReason::kLostFocus;
TestDesktopWidgetDelegate delegate;
delegate.set_can_close(false);
delegate.InitWidget(CreateParams(Widget::InitParams::CLIENT_OWNS_WIDGET,
Widget::InitParams::TYPE_WINDOW));
delegate.GetWidget()->CloseWithReason(kReason);
EXPECT_FALSE(delegate.GetWidget()->IsClosed());
EXPECT_EQ(Widget::ClosedReason::kUnspecified,
delegate.GetWidget()->closed_reason());
EXPECT_EQ(kReason, delegate.last_closed_reason());
delegate.GetWidget()->CloseNow();
}
TEST_F(DesktopWidgetTest, CloseRequested_SecondCloseIgnored) {
constexpr Widget::ClosedReason kReason1 = Widget::ClosedReason::kLostFocus;
constexpr Widget::ClosedReason kReason2 = Widget::ClosedReason::kUnspecified;
TestDesktopWidgetDelegate delegate;
delegate.set_can_close(true);
delegate.InitWidget(CreateParams(Widget::InitParams::CLIENT_OWNS_WIDGET,
Widget::InitParams::TYPE_WINDOW));
WidgetDestroyedWaiter waiter(delegate.GetWidget());
// Close for the first time.
delegate.GetWidget()->CloseWithReason(kReason1);
EXPECT_TRUE(delegate.GetWidget()->IsClosed());
EXPECT_EQ(kReason1, delegate.last_closed_reason());
// Calling close again should have no effect.
delegate.GetWidget()->CloseWithReason(kReason2);
EXPECT_TRUE(delegate.GetWidget()->IsClosed());
EXPECT_EQ(kReason1, delegate.last_closed_reason());
waiter.Wait();
}
class WidgetWindowTitleTest : public DesktopWidgetTest {
protected:
void RunTest(bool desktop_native_widget) {
auto widget = std::make_unique<Widget>();
Widget::InitParams init_params =
CreateParams(Widget::InitParams::CLIENT_OWNS_WIDGET,
Widget::InitParams::TYPE_WINDOW);
if (!desktop_native_widget) {
init_params.native_widget =
CreatePlatformNativeWidgetImpl(widget.get(), kStubCapture, nullptr);
}
widget->Init(std::move(init_params));
internal::NativeWidgetPrivate* native_widget =
widget->native_widget_private();
std::u16string empty;
std::u16string s1(u"Title1");
std::u16string s2(u"Title2");
std::u16string s3(u"TitleLong");
// The widget starts with no title, setting empty should not change
// anything.
EXPECT_FALSE(native_widget->SetWindowTitle(empty));
// Setting the title to something non-empty should cause a change.
EXPECT_TRUE(native_widget->SetWindowTitle(s1));
// Setting the title to something else with the same length should cause a
// change.
EXPECT_TRUE(native_widget->SetWindowTitle(s2));
// Setting the title to something else with a different length should cause
// a change.
EXPECT_TRUE(native_widget->SetWindowTitle(s3));
// Setting the title to the same thing twice should not cause a change.
EXPECT_FALSE(native_widget->SetWindowTitle(s3));
}
};
TEST_F(WidgetWindowTitleTest, SetWindowTitleChanged_NativeWidget) {
// Use the default NativeWidget.
bool desktop_native_widget = false;
RunTest(desktop_native_widget);
}
TEST_F(WidgetWindowTitleTest, SetWindowTitleChanged_DesktopNativeWidget) {
// Override to use a DesktopNativeWidget.
bool desktop_native_widget = true;
RunTest(desktop_native_widget);
}
TEST_F(WidgetTest, WidgetDeleted_InOnMousePressed) {
Widget* widget = new Widget;
Widget::InitParams params =
CreateParams(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
Widget::InitParams::TYPE_POPUP);
widget->Init(std::move(params));
widget->SetContentsView(
std::make_unique<CloseWidgetView>(ui::EventType::kMousePressed));
widget->SetSize(gfx::Size(100, 100));
widget->Show();
auto generator =
CreateEventGenerator(GetContext(), widget->GetNativeWindow());
WidgetDeletionObserver deletion_observer(widget);
generator->PressLeftButton();
if (deletion_observer.IsWidgetAlive()) {
generator->ReleaseLeftButton();
}
EXPECT_FALSE(deletion_observer.IsWidgetAlive());
// Yay we did not crash!
}
// No touch on desktop Mac. Tracked in http://crbug.com/445520.
#if !BUILDFLAG(IS_MAC) || defined(USE_AURA)
TEST_F(WidgetTest, WidgetDeleted_InDispatchGestureEvent) {
Widget* widget = new Widget;
Widget::InitParams params =
CreateParams(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
Widget::InitParams::TYPE_POPUP);
widget->Init(std::move(params));
widget->SetContentsView(
std::make_unique<CloseWidgetView>(ui::EventType::kGestureTapDown));
widget->SetSize(gfx::Size(100, 100));
widget->Show();
auto generator =
CreateEventGenerator(GetContext(), widget->GetNativeWindow());
WidgetDeletionObserver deletion_observer(widget);
generator->GestureTapAt(widget->GetWindowBoundsInScreen().CenterPoint());
EXPECT_FALSE(deletion_observer.IsWidgetAlive());
// Yay we did not crash!
}
#endif // !BUILDFLAG(IS_MAC) || defined(USE_AURA)
// See description of RunGetNativeThemeFromDestructor() for details.
class GetNativeThemeFromDestructorView : public WidgetDelegateView {
public:
GetNativeThemeFromDestructorView() = default;
GetNativeThemeFromDestructorView(const GetNativeThemeFromDestructorView&) =
delete;
GetNativeThemeFromDestructorView& operator=(
const GetNativeThemeFromDestructorView&) = delete;
~GetNativeThemeFromDestructorView() override { VerifyNativeTheme(); }
private:
void VerifyNativeTheme() { ASSERT_TRUE(GetNativeTheme() != nullptr); }
};
// Verifies GetNativeTheme() from the destructor of a WidgetDelegateView doesn't
// crash. |is_first_run| is true if this is the first call. A return value of
// true indicates this should be run again with a value of false.
// First run uses DesktopNativeWidgetAura (if possible). Second run doesn't.
bool RunGetNativeThemeFromDestructor(Widget::InitParams params,
bool is_first_run) {
bool needs_second_run = false;
// Destroyed by CloseNow() below.
WidgetTest::WidgetAutoclosePtr widget(new Widget);
// Deletes itself when the Widget is destroyed.
params.delegate = new GetNativeThemeFromDestructorView;
if (!is_first_run) {
params.native_widget =
CreatePlatformNativeWidgetImpl(widget.get(), kStubCapture, nullptr);
needs_second_run = true;
}
widget->Init(std::move(params));
return needs_second_run;
}
// See description of RunGetNativeThemeFromDestructor() for details.
TEST_F(DesktopWidgetTest, DISABLED_GetNativeThemeFromDestructor) {
if (RunGetNativeThemeFromDestructor(
CreateParams(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
Widget::InitParams::TYPE_POPUP),
true)) {
RunGetNativeThemeFromDestructor(
CreateParams(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
Widget::InitParams::TYPE_POPUP),
false);
}
}
// Used by HideCloseDestroy. Allows setting a boolean when the widget is
// destroyed.
class CloseDestroysWidget : public Widget {
public:
CloseDestroysWidget(bool* destroyed, base::OnceClosure quit_closure)
: destroyed_(destroyed), quit_closure_(std::move(quit_closure)) {
DCHECK(destroyed_);
DCHECK(quit_closure_);
}
CloseDestroysWidget(const CloseDestroysWidget&) = delete;
CloseDestroysWidget& operator=(const CloseDestroysWidget&) = delete;
~CloseDestroysWidget() override {
*destroyed_ = true;
std::move(quit_closure_).Run();
}
void Detach() { destroyed_ = nullptr; }
private:
raw_ptr<bool> destroyed_;
base::OnceClosure quit_closure_;
};
// An observer that registers that an animation has ended.
class AnimationEndObserver : public ui::ImplicitAnimationObserver {
public:
AnimationEndObserver() = default;
AnimationEndObserver(const AnimationEndObserver&) = delete;
AnimationEndObserver& operator=(const AnimationEndObserver&) = delete;
~AnimationEndObserver() override = default;
bool animation_completed() const { return animation_completed_; }
// ui::ImplicitAnimationObserver:
void OnImplicitAnimationsCompleted() override { animation_completed_ = true; }
private:
bool animation_completed_ = false;
};
// An observer that registers the bounds of a widget on destruction.
class WidgetBoundsObserver : public WidgetObserver {
public:
explicit WidgetBoundsObserver(Widget* widget) {
widget_observation_.Observe(widget);
}
WidgetBoundsObserver(const WidgetBoundsObserver&) = delete;
WidgetBoundsObserver& operator=(const WidgetBoundsObserver&) = delete;
~WidgetBoundsObserver() override = default;
gfx::Rect bounds() { return bounds_; }
// WidgetObserver:
void OnWidgetDestroying(Widget* widget) override {
EXPECT_TRUE(widget->GetNativeWindow());
EXPECT_TRUE(Widget::GetWidgetForNativeWindow(widget->GetNativeWindow()));
bounds_ = widget->GetWindowBoundsInScreen();
widget_observation_.Reset();
}
private:
gfx::Rect bounds_;
base::ScopedObservation<Widget, WidgetObserver> widget_observation_{this};
};
// Verifies Close() results in destroying.
TEST_F(DesktopWidgetTest, CloseDestroys) {
bool destroyed = false;
base::RunLoop run_loop;
CloseDestroysWidget* widget =
new CloseDestroysWidget(&destroyed, run_loop.QuitClosure());
Widget::InitParams params =
CreateParams(Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
Widget::InitParams::TYPE_MENU);
params.opacity = Widget::InitParams::WindowOpacity::kOpaque;
params.bounds = gfx::Rect(50, 50, 250, 250);
widget->Init(std::move(params));
widget->Show();
widget->Hide();
widget->Close();
EXPECT_FALSE(destroyed);
// Run the message loop as Close() asynchronously deletes.
run_loop.Run();
EXPECT_TRUE(destroyed);
// Close() should destroy the widget. If not we'll cleanup to avoid leaks.
if (!destroyed) {
widget->Detach();
widget->CloseNow();
}
}
// Tests that killing a widget while animating it does not crash.
TEST_F(WidgetTest, CloseWidgetWhileAnimating) {
std::unique_ptr<Widget> widget =
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
AnimationEndObserver animation_observer;
WidgetBoundsObserver widget_observer(widget.get());
gfx::Rect bounds(100, 100, 50, 50);
{
// Normal animations for tests have ZERO_DURATION, make sure we are actually
// animating the movement.
ui::ScopedAnimationDurationScaleMode animation_scale_mode(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
ui::ScopedLayerAnimationSettings animation_settings(
widget->GetLayer()->GetAnimator());
animation_settings.AddObserver(&animation_observer);
widget->Show();
// Animate the bounds change.
widget->SetBounds(bounds);
widget->CloseNow();
widget.reset();
EXPECT_FALSE(animation_observer.animation_completed());
}
EXPECT_TRUE(animation_observer.animation_completed());
EXPECT_EQ(widget_observer.bounds(), bounds);
}
// Test Widget::CloseAllSecondaryWidgets works as expected across platforms.
// ChromeOS doesn't implement or need CloseAllSecondaryWidgets() since
// everything is under a single root window.
#if BUILDFLAG(ENABLE_DESKTOP_AURA) || BUILDFLAG(IS_MAC)
TEST_F(DesktopWidgetTest, CloseAllSecondaryWidgets) {
Widget* widget1 = CreateTopLevelNativeWidget();
Widget* widget2 = CreateTopLevelNativeWidget();
TestWidgetObserver observer1(widget1);
TestWidgetObserver observer2(widget2);
widget1->Show(); // Just show the first one.
Widget::CloseAllSecondaryWidgets();
EXPECT_TRUE(observer1.widget_closed());
EXPECT_TRUE(observer2.widget_closed());
}
#endif
// Test that the NativeWidget is still valid during OnNativeWidgetDestroying(),
// and properties that depend on it are valid, when closed via CloseNow().
TEST_F(DesktopWidgetTest, ValidDuringOnNativeWidgetDestroyingFromCloseNow) {
Widget* widget = CreateTopLevelNativeWidget();
widget->Show();
gfx::Rect screen_rect(50, 50, 100, 100);
widget->SetBounds(screen_rect);
WidgetBoundsObserver observer(widget);
widget->CloseNow();
EXPECT_EQ(screen_rect, observer.bounds());
}
// Test that the NativeWidget is still valid during OnNativeWidgetDestroying(),
// and properties that depend on it are valid, when closed via Close().
TEST_F(DesktopWidgetTest, ValidDuringOnNativeWidgetDestroyingFromClose) {
Widget* widget = CreateTopLevelNativeWidget();
widget->Show();
gfx::Rect screen_rect(50, 50, 100, 100);
widget->SetBounds(screen_rect);
WidgetBoundsObserver observer(widget);
widget->Close();
EXPECT_EQ(gfx::Rect(), observer.bounds());
base::RunLoop().RunUntilIdle();
// Broken on Linux. See http://crbug.com/515379.
#if !BUILDFLAG(IS_LINUX)
EXPECT_EQ(screen_rect, observer.bounds());
#endif
}
// Tests that we do not crash when a Widget is destroyed by going out of
// scope (as opposed to being explicitly deleted by its NativeWidget).
TEST_F(WidgetTest, NoCrashOnWidgetDelete) {
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
}
TEST_F(WidgetTest, NoCrashOnResizeConstraintsWindowTitleOnPopup) {
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET,
Widget::InitParams::TYPE_POPUP)
->OnSizeConstraintsChanged();
}
// Tests that we do not crash when a Widget is destroyed before it finishes
// processing of pending input events in the message loop.
TEST_F(WidgetTest, NoCrashOnWidgetDeleteWithPendingEvents) {
std::unique_ptr<Widget> widget =
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
widget->Show();
auto generator =
CreateEventGenerator(GetContext(), widget->GetNativeWindow());
generator->MoveMouseTo(10, 10);
// No touch on desktop Mac. Tracked in http://crbug.com/445520.
#if BUILDFLAG(IS_MAC)
generator->ClickLeftButton();
#else
generator->PressTouch();
#endif
widget.reset();
}
// A view that consumes mouse-pressed event and gesture-tap-down events.
class RootViewTestView : public View {
METADATA_HEADER(RootViewTestView, View)
public:
RootViewTestView() = default;
private:
bool OnMousePressed(const ui::MouseEvent& event) override { return true; }
void OnGestureEvent(ui::GestureEvent* event) override {
if (event->type() == ui::EventType::kGestureTapDown) {
event->SetHandled();
}
}
};
BEGIN_METADATA(RootViewTestView)
END_METADATA
// Checks if RootView::*_handler_ fields are unset when widget is hidden.
// Fails on chromium.webkit Windows bot, see crbug.com/264872.
#if BUILDFLAG(IS_WIN)
#define MAYBE_DisableTestRootViewHandlersWhenHidden \
DISABLED_TestRootViewHandlersWhenHidden
#else
#define MAYBE_DisableTestRootViewHandlersWhenHidden \
TestRootViewHandlersWhenHidden
#endif
TEST_F(WidgetTest, MAYBE_DisableTestRootViewHandlersWhenHidden) {
Widget* widget = CreateTopLevelNativeWidget();
widget->SetBounds(gfx::Rect(0, 0, 300, 300));
View* view = new RootViewTestView();
view->SetBounds(0, 0, 300, 300);
internal::RootView* root_view =
static_cast<internal::RootView*>(widget->GetRootView());
root_view->AddChildViewRaw(view);
// Check RootView::mouse_pressed_handler_.
widget->Show();
EXPECT_EQ(nullptr, GetMousePressedHandler(root_view));
gfx::Point click_location(45, 15);
ui::MouseEvent press(ui::EventType::kMousePressed, click_location,
click_location, ui::EventTimeForNow(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
widget->OnMouseEvent(&press);
EXPECT_EQ(view, GetMousePressedHandler(root_view));
widget->Hide();
EXPECT_EQ(nullptr, GetMousePressedHandler(root_view));
// Check RootView::mouse_move_handler_.
widget->Show();
EXPECT_EQ(nullptr, GetMouseMoveHandler(root_view));
gfx::Point move_location(45, 15);
ui::MouseEvent move(ui::EventType::kMouseMoved, move_location, move_location,
ui::EventTimeForNow(), 0, 0);
widget->OnMouseEvent(&move);
EXPECT_EQ(view, GetMouseMoveHandler(root_view));
widget->Hide();
EXPECT_EQ(nullptr, GetMouseMoveHandler(root_view));
// Check RootView::gesture_handler_.
widget->Show();
EXPECT_EQ(nullptr, GetGestureHandler(root_view));
ui::GestureEvent tap_down =
CreateTestGestureEvent(ui::EventType::kGestureTapDown, 15, 15);
widget->OnGestureEvent(&tap_down);
EXPECT_EQ(view, GetGestureHandler(root_view));
widget->Hide();
EXPECT_EQ(nullptr, GetGestureHandler(root_view));
widget->Close();
}
// Tests that the |gesture_handler_| member in RootView is always NULL
// after the dispatch of a ui::EventType::kGestureEnd event corresponding to
// the release of the final touch point on the screen, but that
// ui::EventType::kGestureEnd events corresponding to the removal of any other
// touch point do not modify |gesture_handler_|.
TEST_F(WidgetTest, GestureEndEvents) {
Widget* widget = CreateTopLevelNativeWidget();
widget->SetBounds(gfx::Rect(0, 0, 300, 300));
EventCountView* view = new EventCountView();
view->SetBounds(0, 0, 300, 300);
internal::RootView* root_view =
static_cast<internal::RootView*>(widget->GetRootView());
root_view->AddChildViewRaw(view);
widget->Show();
// If no gesture handler is set, a ui::EventType::kGestureEnd event should not
// set the gesture handler.
EXPECT_EQ(nullptr, GetGestureHandler(root_view));
ui::GestureEvent end =
CreateTestGestureEvent(ui::EventType::kGestureEnd, 15, 15);
widget->OnGestureEvent(&end);
EXPECT_EQ(nullptr, GetGestureHandler(root_view));
// Change the handle mode of |view| to indicate that it would like
// to handle all events, then send a GESTURE_TAP to set the gesture handler.
view->set_handle_mode(EventCountView::HandleMode::kConsumeEvents);
ui::GestureEvent tap =
CreateTestGestureEvent(ui::EventType::kGestureTap, 15, 15);
widget->OnGestureEvent(&tap);
EXPECT_TRUE(tap.handled());
EXPECT_EQ(view, GetGestureHandler(root_view));
// The gesture handler should remain unchanged on a ui::EventType::kGestureEnd
// corresponding to a second touch point, but should be reset to NULL by a
// ui::EventType::kGestureEnd corresponding to the final touch point.
ui::GestureEventDetails details(ui::EventType::kGestureEnd);
details.set_touch_points(2);
ui::GestureEvent end_second_touch_point =
CreateTestGestureEvent(details, 15, 15);
widget->OnGestureEvent(&end_second_touch_point);
EXPECT_EQ(view, GetGestureHandler(root_view));
end = CreateTestGestureEvent(ui::EventType::kGestureEnd, 15, 15);
widget->OnGestureEvent(&end);
EXPECT_TRUE(end.handled());
EXPECT_EQ(nullptr, GetGestureHandler(root_view));
// Send a GESTURE_TAP to set the gesture handler, then change the handle
// mode of |view| to indicate that it does not want to handle any
// further events.
tap = CreateTestGestureEvent(ui::EventType::kGestureTap, 15, 15);
widget->OnGestureEvent(&tap);
EXPECT_TRUE(tap.handled());
EXPECT_EQ(view, GetGestureHandler(root_view));
view->set_handle_mode(EventCountView::HandleMode::kPropagateEvents);
// The gesture handler should remain unchanged on a ui::EventType::kGestureEnd
// corresponding to a second touch point, but should be reset to NULL by a
// ui::EventType::kGestureEnd corresponding to the final touch point.
end_second_touch_point = CreateTestGestureEvent(details, 15, 15);
widget->OnGestureEvent(&end_second_touch_point);
EXPECT_EQ(view, GetGestureHandler(root_view));
end = CreateTestGestureEvent(ui::EventType::kGestureEnd, 15, 15);
widget->OnGestureEvent(&end);
EXPECT_FALSE(end.handled());
EXPECT_EQ(nullptr, GetGestureHandler(root_view));
widget->Close();
}
// Tests that gesture events which should not be processed (because
// RootView::OnEventProcessingStarted() has marked them as handled) are not
// dispatched to any views.
TEST_F(WidgetTest, GestureEventsNotProcessed) {
Widget* widget = CreateTopLevelNativeWidget();
widget->SetBounds(gfx::Rect(0, 0, 300, 300));
// Define a hierarchy of four views (coordinates are in
// their parent coordinate space).
// v1 (0, 0, 300, 300)
// v2 (0, 0, 100, 100)
// v3 (0, 0, 50, 50)
// v4(0, 0, 10, 10)
EventCountView* v1 = new EventCountView();
v1->SetBounds(0, 0, 300, 300);
EventCountView* v2 = new EventCountView();
v2->SetBounds(0, 0, 100, 100);
EventCountView* v3 = new EventCountView();
v3->SetBounds(0, 0, 50, 50);
EventCountView* v4 = new EventCountView();
v4->SetBounds(0, 0, 10, 10);
internal::RootView* root_view =
static_cast<internal::RootView*>(widget->GetRootView());
root_view->AddChildViewRaw(v1);
v1->AddChildViewRaw(v2);
v2->AddChildViewRaw(v3);
v3->AddChildViewRaw(v4);
widget->Show();
// ui::EventType::kGestureBegin events should never be seen by any view, but
// they should be marked as handled by OnEventProcessingStarted().
ui::GestureEvent begin =
CreateTestGestureEvent(ui::EventType::kGestureBegin, 5, 5);
widget->OnGestureEvent(&begin);
EXPECT_EQ(0, v1->GetEventCount(ui::EventType::kGestureBegin));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kGestureBegin));
EXPECT_EQ(0, v3->GetEventCount(ui::EventType::kGestureBegin));
EXPECT_EQ(0, v4->GetEventCount(ui::EventType::kGestureBegin));
EXPECT_EQ(nullptr, GetGestureHandler(root_view));
EXPECT_TRUE(begin.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// ui::EventType::kGestureEnd events should not be seen by any view when there
// is no default gesture handler set, but they should be marked as handled by
// OnEventProcessingStarted().
ui::GestureEvent end =
CreateTestGestureEvent(ui::EventType::kGestureEnd, 5, 5);
widget->OnGestureEvent(&end);
EXPECT_EQ(0, v1->GetEventCount(ui::EventType::kGestureEnd));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kGestureEnd));
EXPECT_EQ(0, v3->GetEventCount(ui::EventType::kGestureEnd));
EXPECT_EQ(0, v4->GetEventCount(ui::EventType::kGestureEnd));
EXPECT_EQ(nullptr, GetGestureHandler(root_view));
EXPECT_TRUE(end.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// ui::EventType::kGestureEnd events not corresponding to the release of the
// final touch point should never be seen by any view, but they should
// be marked as handled by OnEventProcessingStarted().
ui::GestureEventDetails details(ui::EventType::kGestureEnd);
details.set_touch_points(2);
ui::GestureEvent end_second_touch_point =
CreateTestGestureEvent(details, 5, 5);
widget->OnGestureEvent(&end_second_touch_point);
EXPECT_EQ(0, v1->GetEventCount(ui::EventType::kGestureEnd));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kGestureEnd));
EXPECT_EQ(0, v3->GetEventCount(ui::EventType::kGestureEnd));
EXPECT_EQ(0, v4->GetEventCount(ui::EventType::kGestureEnd));
EXPECT_EQ(nullptr, GetGestureHandler(root_view));
EXPECT_TRUE(end_second_touch_point.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// ui::EventType::kGestureScrollUpdate events should never be seen by any view
// when there is no default gesture handler set, but they should be marked as
// handled by OnEventProcessingStarted().
ui::GestureEvent scroll_update =
CreateTestGestureEvent(ui::EventType::kGestureScrollUpdate, 5, 5);
widget->OnGestureEvent(&scroll_update);
EXPECT_EQ(0, v1->GetEventCount(ui::EventType::kGestureScrollUpdate));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kGestureScrollUpdate));
EXPECT_EQ(0, v3->GetEventCount(ui::EventType::kGestureScrollUpdate));
EXPECT_EQ(0, v4->GetEventCount(ui::EventType::kGestureScrollUpdate));
EXPECT_EQ(nullptr, GetGestureHandler(root_view));
EXPECT_TRUE(scroll_update.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// ui::EventType::kGestureScrollEnd events should never be seen by any view
// when there is no default gesture handler set, but they should be marked as
// handled by OnEventProcessingStarted().
ui::GestureEvent scroll_end =
CreateTestGestureEvent(ui::EventType::kGestureScrollEnd, 5, 5);
widget->OnGestureEvent(&scroll_end);
EXPECT_EQ(0, v1->GetEventCount(ui::EventType::kGestureScrollEnd));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kGestureScrollEnd));
EXPECT_EQ(0, v3->GetEventCount(ui::EventType::kGestureScrollEnd));
EXPECT_EQ(0, v4->GetEventCount(ui::EventType::kGestureScrollEnd));
EXPECT_EQ(nullptr, GetGestureHandler(root_view));
EXPECT_TRUE(scroll_end.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// ui::EventType::kScrollFlingStart events should never be seen by any view
// when there is no default gesture handler set, but they should be marked as
// handled by OnEventProcessingStarted().
ui::GestureEvent scroll_fling_start =
CreateTestGestureEvent(ui::EventType::kScrollFlingStart, 5, 5);
widget->OnGestureEvent(&scroll_fling_start);
EXPECT_EQ(0, v1->GetEventCount(ui::EventType::kScrollFlingStart));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kScrollFlingStart));
EXPECT_EQ(0, v3->GetEventCount(ui::EventType::kScrollFlingStart));
EXPECT_EQ(0, v4->GetEventCount(ui::EventType::kScrollFlingStart));
EXPECT_EQ(nullptr, GetGestureHandler(root_view));
EXPECT_TRUE(scroll_fling_start.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
widget->Close();
}
// Tests that a (non-scroll) gesture event is dispatched to the correct views
// in a view hierarchy and that the default gesture handler in RootView is set
// correctly.
TEST_F(WidgetTest, GestureEventDispatch) {
Widget* widget = CreateTopLevelNativeWidget();
widget->SetBounds(gfx::Rect(0, 0, 300, 300));
// Define a hierarchy of four views (coordinates are in
// their parent coordinate space).
// v1 (0, 0, 300, 300)
// v2 (0, 0, 100, 100)
// v3 (0, 0, 50, 50)
// v4(0, 0, 10, 10)
EventCountView* v1 = new EventCountView();
v1->SetBounds(0, 0, 300, 300);
EventCountView* v2 = new EventCountView();
v2->SetBounds(0, 0, 100, 100);
EventCountView* v3 = new EventCountView();
v3->SetBounds(0, 0, 50, 50);
EventCountView* v4 = new EventCountView();
v4->SetBounds(0, 0, 10, 10);
internal::RootView* root_view =
static_cast<internal::RootView*>(widget->GetRootView());
root_view->AddChildViewRaw(v1);
v1->AddChildViewRaw(v2);
v2->AddChildViewRaw(v3);
v3->AddChildViewRaw(v4);
widget->Show();
// No gesture handler is set in the root view and none of the views in the
// view hierarchy handle a ui::EventType::kGestureTap event. In this case the
// tap event should be dispatched to all views in the hierarchy, the gesture
// handler should remain unset, and the event should remain unhandled.
ui::GestureEvent tap =
CreateTestGestureEvent(ui::EventType::kGestureTap, 5, 5);
EXPECT_EQ(nullptr, GetGestureHandler(root_view));
widget->OnGestureEvent(&tap);
EXPECT_EQ(1, v1->GetEventCount(ui::EventType::kGestureTap));
EXPECT_EQ(1, v2->GetEventCount(ui::EventType::kGestureTap));
EXPECT_EQ(1, v3->GetEventCount(ui::EventType::kGestureTap));
EXPECT_EQ(1, v4->GetEventCount(ui::EventType::kGestureTap));
EXPECT_EQ(nullptr, GetGestureHandler(root_view));
EXPECT_FALSE(tap.handled());
// No gesture handler is set in the root view and |v1|, |v2|, and |v3| all
// handle a ui::EventType::kGestureTap event. In this case the tap event
// should be dispatched to |v4| and |v3|, the gesture handler should be set to
// |v3|, and the event should be marked as handled.
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
v1->set_handle_mode(EventCountView::HandleMode::kConsumeEvents);
v2->set_handle_mode(EventCountView::HandleMode::kConsumeEvents);
v3->set_handle_mode(EventCountView::HandleMode::kConsumeEvents);
tap = CreateTestGestureEvent(ui::EventType::kGestureTap, 5, 5);
widget->OnGestureEvent(&tap);
EXPECT_EQ(0, v1->GetEventCount(ui::EventType::kGestureTap));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kGestureTap));
EXPECT_EQ(1, v3->GetEventCount(ui::EventType::kGestureTap));
EXPECT_EQ(1, v4->GetEventCount(ui::EventType::kGestureTap));
EXPECT_EQ(v3, GetGestureHandler(root_view));
EXPECT_TRUE(tap.handled());
// The gesture handler is set to |v3| and all views handle all gesture event
// types. In this case subsequent gesture events should only be dispatched to
// |v3| and marked as handled. The gesture handler should remain as |v3|.
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
v4->set_handle_mode(EventCountView::HandleMode::kConsumeEvents);
tap = CreateTestGestureEvent(ui::EventType::kGestureTap, 5, 5);
widget->OnGestureEvent(&tap);
EXPECT_TRUE(tap.handled());
ui::GestureEvent show_press =
CreateTestGestureEvent(ui::EventType::kGestureShowPress, 5, 5);
widget->OnGestureEvent(&show_press);
tap = CreateTestGestureEvent(ui::EventType::kGestureTap, 5, 5);
widget->OnGestureEvent(&tap);
EXPECT_EQ(0, v1->GetEventCount(ui::EventType::kGestureTap));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kGestureTap));
EXPECT_EQ(2, v3->GetEventCount(ui::EventType::kGestureTap));
EXPECT_EQ(0, v4->GetEventCount(ui::EventType::kGestureTap));
EXPECT_EQ(0, v1->GetEventCount(ui::EventType::kGestureShowPress));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kGestureShowPress));
EXPECT_EQ(1, v3->GetEventCount(ui::EventType::kGestureShowPress));
EXPECT_EQ(0, v4->GetEventCount(ui::EventType::kGestureShowPress));
EXPECT_TRUE(tap.handled());
EXPECT_TRUE(show_press.handled());
EXPECT_EQ(v3, GetGestureHandler(root_view));
// The gesture handler is set to |v3|, but |v3| does not handle
// ui::EventType::kGestureTap events. In this case a tap gesture should be
// dispatched only to |v3|, but the event should remain unhandled. The gesture
// handler should remain as |v3|.
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
v3->set_handle_mode(EventCountView::HandleMode::kPropagateEvents);
tap = CreateTestGestureEvent(ui::EventType::kGestureTap, 5, 5);
widget->OnGestureEvent(&tap);
EXPECT_EQ(0, v1->GetEventCount(ui::EventType::kGestureTap));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kGestureTap));
EXPECT_EQ(1, v3->GetEventCount(ui::EventType::kGestureTap));
EXPECT_EQ(0, v4->GetEventCount(ui::EventType::kGestureTap));
EXPECT_FALSE(tap.handled());
EXPECT_EQ(v3, GetGestureHandler(root_view));
widget->Close();
}
// Tests that gesture scroll events will change the default gesture handler in
// RootView if the current handler to which they are dispatched does not handle
// gesture scroll events.
TEST_F(WidgetTest, ScrollGestureEventDispatch) {
Widget* widget = CreateTopLevelNativeWidget();
widget->SetBounds(gfx::Rect(0, 0, 300, 300));
// Define a hierarchy of four views (coordinates are in
// their parent coordinate space).
// v1 (0, 0, 300, 300)
// v2 (0, 0, 100, 100)
// v3 (0, 0, 50, 50)
// v4(0, 0, 10, 10)
EventCountView* v1 = new EventCountView();
v1->SetBounds(0, 0, 300, 300);
EventCountView* v2 = new EventCountView();
v2->SetBounds(0, 0, 100, 100);
EventCountView* v3 = new EventCountView();
v3->SetBounds(0, 0, 50, 50);
EventCountView* v4 = new EventCountView();
v4->SetBounds(0, 0, 10, 10);
internal::RootView* root_view =
static_cast<internal::RootView*>(widget->GetRootView());
root_view->AddChildViewRaw(v1);
v1->AddChildViewRaw(v2);
v2->AddChildViewRaw(v3);
v3->AddChildViewRaw(v4);
widget->Show();
// Change the handle mode of |v3| to indicate that it would like to handle
// gesture events.
v3->set_handle_mode(EventCountView::HandleMode::kConsumeEvents);
// When no gesture handler is set, dispatching a
// ui::EventType::kGestureTapDown should bubble up the views hierarchy until
// it reaches the first view that will handle it (|v3|) and then sets the
// handler to |v3|.
EXPECT_EQ(nullptr, GetGestureHandler(root_view));
ui::GestureEvent tap_down =
CreateTestGestureEvent(ui::EventType::kGestureTapDown, 5, 5);
widget->OnGestureEvent(&tap_down);
EXPECT_EQ(0, v1->GetEventCount(ui::EventType::kGestureTapDown));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kGestureTapDown));
EXPECT_EQ(1, v3->GetEventCount(ui::EventType::kGestureTapDown));
EXPECT_EQ(1, v4->GetEventCount(ui::EventType::kGestureTapDown));
EXPECT_EQ(v3, GetGestureHandler(root_view));
EXPECT_TRUE(tap_down.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// A ui::EventType::kGestureTapCancel event should be dispatched to |v3|
// directly.
ui::GestureEvent tap_cancel =
CreateTestGestureEvent(ui::EventType::kGestureTapCancel, 5, 5);
widget->OnGestureEvent(&tap_cancel);
EXPECT_EQ(0, v1->GetEventCount(ui::EventType::kGestureTapCancel));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kGestureTapCancel));
EXPECT_EQ(1, v3->GetEventCount(ui::EventType::kGestureTapCancel));
EXPECT_EQ(0, v4->GetEventCount(ui::EventType::kGestureTapCancel));
EXPECT_EQ(v3, GetGestureHandler(root_view));
EXPECT_TRUE(tap_cancel.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// Change the handle mode of |v3| to indicate that it would no longer like
// to handle events, and change the mode of |v1| to indicate that it would
// like to handle events.
v3->set_handle_mode(EventCountView::HandleMode::kPropagateEvents);
v1->set_handle_mode(EventCountView::HandleMode::kConsumeEvents);
// Dispatch a ui::EventType::kGestureScrollBegin event. Because the current
// gesture handler (|v3|) does not handle scroll events, the event should
// bubble up the views hierarchy until it reaches the first view that will
// handle it (|v1|) and then sets the handler to |v1|.
ui::GestureEvent scroll_begin =
CreateTestGestureEvent(ui::EventType::kGestureScrollBegin, 5, 5);
widget->OnGestureEvent(&scroll_begin);
EXPECT_EQ(1, v1->GetEventCount(ui::EventType::kGestureScrollBegin));
EXPECT_EQ(1, v2->GetEventCount(ui::EventType::kGestureScrollBegin));
EXPECT_EQ(1, v3->GetEventCount(ui::EventType::kGestureScrollBegin));
EXPECT_EQ(0, v4->GetEventCount(ui::EventType::kGestureScrollBegin));
EXPECT_EQ(v1, GetGestureHandler(root_view));
EXPECT_TRUE(scroll_begin.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// A ui::EventType::kGestureScrollUpdate event should be dispatched to |v1|
// directly.
ui::GestureEvent scroll_update =
CreateTestGestureEvent(ui::EventType::kGestureScrollUpdate, 5, 5);
widget->OnGestureEvent(&scroll_update);
EXPECT_EQ(1, v1->GetEventCount(ui::EventType::kGestureScrollUpdate));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kGestureScrollUpdate));
EXPECT_EQ(0, v3->GetEventCount(ui::EventType::kGestureScrollUpdate));
EXPECT_EQ(0, v4->GetEventCount(ui::EventType::kGestureScrollUpdate));
EXPECT_EQ(v1, GetGestureHandler(root_view));
EXPECT_TRUE(scroll_update.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// A ui::EventType::kGestureScrollEnd event should be dispatched to |v1|
// directly and should not reset the gesture handler.
ui::GestureEvent scroll_end =
CreateTestGestureEvent(ui::EventType::kGestureScrollEnd, 5, 5);
widget->OnGestureEvent(&scroll_end);
EXPECT_EQ(1, v1->GetEventCount(ui::EventType::kGestureScrollEnd));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kGestureScrollEnd));
EXPECT_EQ(0, v3->GetEventCount(ui::EventType::kGestureScrollEnd));
EXPECT_EQ(0, v4->GetEventCount(ui::EventType::kGestureScrollEnd));
EXPECT_EQ(v1, GetGestureHandler(root_view));
EXPECT_TRUE(scroll_end.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// A ui::EventType::kGesturePinchBegin event (which is a non-scroll event)
// should still be dispatched to |v1| directly.
ui::GestureEvent pinch_begin =
CreateTestGestureEvent(ui::EventType::kGesturePinchBegin, 5, 5);
widget->OnGestureEvent(&pinch_begin);
EXPECT_EQ(1, v1->GetEventCount(ui::EventType::kGesturePinchBegin));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kGesturePinchBegin));
EXPECT_EQ(0, v3->GetEventCount(ui::EventType::kGesturePinchBegin));
EXPECT_EQ(0, v4->GetEventCount(ui::EventType::kGesturePinchBegin));
EXPECT_EQ(v1, GetGestureHandler(root_view));
EXPECT_TRUE(pinch_begin.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// A ui::EventType::kGestureEnd event should be dispatched to |v1| and should
// set the gesture handler to NULL.
ui::GestureEvent end =
CreateTestGestureEvent(ui::EventType::kGestureEnd, 5, 5);
widget->OnGestureEvent(&end);
EXPECT_EQ(1, v1->GetEventCount(ui::EventType::kGestureEnd));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kGestureEnd));
EXPECT_EQ(0, v3->GetEventCount(ui::EventType::kGestureEnd));
EXPECT_EQ(0, v4->GetEventCount(ui::EventType::kGestureEnd));
EXPECT_EQ(nullptr, GetGestureHandler(root_view));
EXPECT_TRUE(end.handled());
widget->Close();
}
// TODO(b/271490637): on Mac a drag controller should still be notified when
// drag will start. Figure out how to write a unit test for Mac. Then remove
// this build flag check.
#if !BUILDFLAG(IS_MAC)
// Verifies that the drag controller is notified when the view drag will start.
TEST_F(WidgetTest, NotifyDragControllerWhenDragWillStart) {
// Create a widget whose contents view is draggable.
UniqueWidgetPtr widget(std::make_unique<Widget>());
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.bounds = gfx::Rect(/*width=*/650, /*height=*/650);
widget->Init(std::move(params));
widget->Show();
MockDragController mock_drag_controller;
views::View contents_view;
contents_view.set_drag_controller(&mock_drag_controller);
widget->SetContentsView(&contents_view);
// Expect the drag controller is notified of the drag start.
EXPECT_CALL(mock_drag_controller, OnWillStartDragForView(&contents_view));
// Drag-and-drop `contents_view` by mouse.
ui::test::EventGenerator generator(GetContext(), widget->GetNativeWindow());
generator.MoveMouseTo(contents_view.GetBoundsInScreen().CenterPoint());
generator.PressLeftButton();
generator.MoveMouseBy(/*x=*/200, /*y=*/0);
generator.ReleaseLeftButton();
}
#endif // !BUILDFLAG(IS_MAC)
// A class used in WidgetTest.GestureEventLocationWhileBubbling to verify
// that when a gesture event bubbles up a View hierarchy, the location
// of a gesture event seen by each View is in the local coordinate space
// of that View.
class GestureLocationView : public EventCountView {
METADATA_HEADER(GestureLocationView, EventCountView)
public:
GestureLocationView() = default;
GestureLocationView(const GestureLocationView&) = delete;
GestureLocationView& operator=(const GestureLocationView&) = delete;
~GestureLocationView() override = default;
void set_expected_location(gfx::Point expected_location) {
expected_location_ = expected_location;
}
// EventCountView:
void OnGestureEvent(ui::GestureEvent* event) override {
EventCountView::OnGestureEvent(event);
// Verify that the location of |event| is in the local coordinate
// space of |this|.
EXPECT_EQ(expected_location_, event->location());
}
private:
// The expected location of a gesture event dispatched to |this|.
gfx::Point expected_location_;
};
BEGIN_METADATA(GestureLocationView)
END_METADATA
// Verifies that the location of a gesture event is always in the local
// coordinate space of the View receiving the event while bubbling.
TEST_F(WidgetTest, GestureEventLocationWhileBubbling) {
Widget* widget = CreateTopLevelNativeWidget();
widget->SetBounds(gfx::Rect(0, 0, 300, 300));
// Define a hierarchy of three views (coordinates shown below are in the
// coordinate space of the root view, but the coordinates used for
// SetBounds() are in their parent coordinate space).
// v1 (50, 50, 150, 150)
// v2 (100, 70, 50, 80)
// v3 (120, 100, 10, 10)
GestureLocationView* v1 = new GestureLocationView();
v1->SetBounds(50, 50, 150, 150);
GestureLocationView* v2 = new GestureLocationView();
v2->SetBounds(50, 20, 50, 80);
GestureLocationView* v3 = new GestureLocationView();
v3->SetBounds(20, 30, 10, 10);
internal::RootView* root_view =
static_cast<internal::RootView*>(widget->GetRootView());
root_view->AddChildViewRaw(v1);
v1->AddChildViewRaw(v2);
v2->AddChildViewRaw(v3);
widget->Show();
// Define a GESTURE_TAP event located at (125, 105) in root view coordinates.
// This event is contained within all of |v1|, |v2|, and |v3|.
gfx::Point location_in_root(125, 105);
ui::GestureEvent tap = CreateTestGestureEvent(
ui::EventType::kGestureTap, location_in_root.x(), location_in_root.y());
// Calculate the location of the event in the local coordinate spaces
// of each of the views.
gfx::Point location_in_v1(ConvertPointFromWidgetToView(v1, location_in_root));
EXPECT_EQ(gfx::Point(75, 55), location_in_v1);
gfx::Point location_in_v2(ConvertPointFromWidgetToView(v2, location_in_root));
EXPECT_EQ(gfx::Point(25, 35), location_in_v2);
gfx::Point location_in_v3(ConvertPointFromWidgetToView(v3, location_in_root));
EXPECT_EQ(gfx::Point(5, 5), location_in_v3);
// Dispatch the event. When each view receives the event, its location should
// be in the local coordinate space of that view (see the check made by
// GestureLocationView). After dispatch is complete the event's location
// should be in the root coordinate space.
v1->set_expected_location(location_in_v1);
v2->set_expected_location(location_in_v2);
v3->set_expected_location(location_in_v3);
widget->OnGestureEvent(&tap);
EXPECT_EQ(location_in_root, tap.location());
// Verify that each view did in fact see the event.
EventCountView* view1 = v1;
EventCountView* view2 = v2;
EventCountView* view3 = v3;
EXPECT_EQ(1, view1->GetEventCount(ui::EventType::kGestureTap));
EXPECT_EQ(1, view2->GetEventCount(ui::EventType::kGestureTap));
EXPECT_EQ(1, view3->GetEventCount(ui::EventType::kGestureTap));
widget->Close();
}
// Test the result of Widget::GetAllChildWidgets().
TEST_F(WidgetTest, GetAllChildWidgets) {
// Create the following widget hierarchy:
//
// toplevel
// +-- w1
// +-- w11
// +-- w2
// +-- w21
// +-- w22
WidgetAutoclosePtr toplevel(CreateTopLevelPlatformWidget());
Widget* w1 = CreateChildPlatformWidget(toplevel->GetNativeView());
Widget* w11 = CreateChildPlatformWidget(w1->GetNativeView());
Widget* w2 = CreateChildPlatformWidget(toplevel->GetNativeView());
Widget* w21 = CreateChildPlatformWidget(w2->GetNativeView());
Widget* w22 = CreateChildPlatformWidget(w2->GetNativeView());
std::set<Widget*> expected;
expected.insert(toplevel.get());
expected.insert(w1);
expected.insert(w11);
expected.insert(w2);
expected.insert(w21);
expected.insert(w22);
Widget::Widgets child_widgets =
Widget::GetAllChildWidgets(toplevel->GetNativeView());
EXPECT_TRUE(std::ranges::equal(expected, child_widgets));
// Check GetAllOwnedWidgets(). On Aura, this includes "transient" children.
// Otherwise (on all platforms), it should be the same as GetAllChildWidgets()
// except the root Widget is not included.
EXPECT_EQ(1u, expected.erase(toplevel.get()));
Widget::Widgets owned_widgets =
Widget::GetAllOwnedWidgets(toplevel->GetNativeView());
EXPECT_TRUE(std::ranges::equal(expected, owned_widgets));
}
// Used by DestroyChildWidgetsInOrder. On destruction adds the supplied name to
// a vector.
class DestroyedTrackingView : public View {
METADATA_HEADER(DestroyedTrackingView, View)
public:
DestroyedTrackingView(const std::string& name,
std::vector<std::string>* add_to)
: name_(name), add_to_(add_to) {}
DestroyedTrackingView(const DestroyedTrackingView&) = delete;
DestroyedTrackingView& operator=(const DestroyedTrackingView&) = delete;
~DestroyedTrackingView() override { add_to_->push_back(name_); }
private:
const std::string name_;
raw_ptr<std::vector<std::string>> add_to_;
};
BEGIN_METADATA(DestroyedTrackingView)
END_METADATA
class WidgetChildDestructionTest : public DesktopWidgetTest {
public:
WidgetChildDestructionTest() = default;
WidgetChildDestructionTest(const WidgetChildDestructionTest&) = delete;
WidgetChildDestructionTest& operator=(const WidgetChildDestructionTest&) =
delete;
// Creates a top level and a child, destroys the child and verifies the views
// of the child are destroyed before the views of the parent.
void RunDestroyChildWidgetsTest(bool top_level_has_desktop_native_widget_aura,
bool child_has_desktop_native_widget_aura) {
// When a View is destroyed its name is added here.
std::vector<std::string> destroyed;
Widget* top_level = new Widget;
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
if (!top_level_has_desktop_native_widget_aura) {
params.native_widget =
CreatePlatformNativeWidgetImpl(top_level, kStubCapture, nullptr);
}
top_level->Init(std::move(params));
top_level->GetRootView()->AddChildViewRaw(
new DestroyedTrackingView("parent", &destroyed));
top_level->Show();
Widget* child = new Widget;
Widget::InitParams child_params =
CreateParams(Widget::InitParams::TYPE_POPUP);
child_params.parent = top_level->GetNativeView();
if (!child_has_desktop_native_widget_aura) {
child_params.native_widget =
CreatePlatformNativeWidgetImpl(child, kStubCapture, nullptr);
}
child->Init(std::move(child_params));
child->GetRootView()->AddChildViewRaw(
new DestroyedTrackingView("child", &destroyed));
child->Show();
// Should trigger destruction of the child too.
top_level->native_widget_private()->CloseNow();
// Child should be destroyed first.
ASSERT_EQ(2u, destroyed.size());
EXPECT_EQ("child", destroyed[0]);
EXPECT_EQ("parent", destroyed[1]);
}
};
// See description of RunDestroyChildWidgetsTest(). Parent uses
// DesktopNativeWidgetAura.
TEST_F(WidgetChildDestructionTest,
DestroyChildWidgetsInOrderWithDesktopNativeWidget) {
RunDestroyChildWidgetsTest(true, false);
}
// See description of RunDestroyChildWidgetsTest(). Both parent and child use
// DesktopNativeWidgetAura.
TEST_F(WidgetChildDestructionTest,
DestroyChildWidgetsInOrderWithDesktopNativeWidgetForBoth) {
RunDestroyChildWidgetsTest(true, true);
}
// See description of RunDestroyChildWidgetsTest().
TEST_F(WidgetChildDestructionTest, DestroyChildWidgetsInOrder) {
RunDestroyChildWidgetsTest(false, false);
}
// Verifies nativeview visbility matches that of Widget visibility when
// SetFullscreen is invoked.
TEST_F(WidgetTest, FullscreenStatePropagated) {
std::unique_ptr<Widget> top_level_widget =
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
top_level_widget->SetFullscreen(true);
EXPECT_EQ(top_level_widget->IsVisible(),
IsNativeWindowVisible(top_level_widget->GetNativeWindow()));
}
// Verifies nativeview visbility matches that of Widget visibility when
// SetFullscreen is invoked, for a widget provided with a desktop widget.
TEST_F(DesktopWidgetTest, FullscreenStatePropagated_DesktopWidget) {
std::unique_ptr<Widget> top_level_widget =
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
top_level_widget->SetFullscreen(true);
EXPECT_EQ(top_level_widget->IsVisible(),
IsNativeWindowVisible(top_level_widget->GetNativeWindow()));
}
// Used to delete the widget when the supplied bounds changes.
class DestroyingWidgetBoundsObserver : public WidgetObserver {
public:
explicit DestroyingWidgetBoundsObserver(Widget* widget) {
widget_observation_.Observe(widget);
}
// There are no assertions here as not all platforms call
// OnWidgetBoundsChanged() when going fullscreen.
~DestroyingWidgetBoundsObserver() override = default;
// WidgetObserver:
void OnWidgetBoundsChanged(Widget* widget,
const gfx::Rect& new_bounds) override {
widget_observation_.Reset();
}
private:
base::ScopedObservation<Widget, WidgetObserver> widget_observation_{this};
};
// Deletes a Widget when the bounds change as part of toggling fullscreen.
// This is a regression test for https://crbug.com/1197436.
// Disabled on Mac: This test has historically deleted the Widget not during
// SetFullscreen, but at the end of the test. When the Widget is deleted inside
// SetFullscreen, the test crashes.
// https://crbug.com/1307486
#if BUILDFLAG(IS_MAC)
#define MAYBE_DeleteInSetFullscreen DISABLED_DeleteInSetFullscreen
#else
#define MAYBE_DeleteInSetFullscreen DeleteInSetFullscreen
#endif
TEST_F(DesktopWidgetTest, MAYBE_DeleteInSetFullscreen) {
std::unique_ptr<Widget> widget = std::make_unique<Widget>();
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
params.ownership = Widget::InitParams::CLIENT_OWNS_WIDGET;
widget->Init(std::move(params));
Widget* w = widget.get();
DestroyingWidgetBoundsObserver destroyer(w);
w->SetFullscreen(true);
}
namespace {
class FullscreenAwareFrame : public views::NonClientFrameView {
METADATA_HEADER(FullscreenAwareFrame, views::NonClientFrameView)
public:
explicit FullscreenAwareFrame(views::Widget* widget) : widget_(widget) {}
FullscreenAwareFrame(const FullscreenAwareFrame&) = delete;
FullscreenAwareFrame& operator=(const FullscreenAwareFrame&) = delete;
~FullscreenAwareFrame() override = default;
// views::NonClientFrameView overrides:
gfx::Rect GetBoundsForClientView() const override { return gfx::Rect(); }
gfx::Rect GetWindowBoundsForClientBounds(
const gfx::Rect& client_bounds) const override {
return gfx::Rect();
}
int NonClientHitTest(const gfx::Point& point) override { return HTNOWHERE; }
void GetWindowMask(const gfx::Size& size, SkPath* window_mask) override {}
void ResetWindowControls() override {}
void UpdateWindowIcon() override {}
void UpdateWindowTitle() override {}
void SizeConstraintsChanged() override {}
// views::View overrides:
void Layout(PassKey) override {
if (widget_->IsFullscreen()) {
fullscreen_layout_called_ = true;
}
}
bool fullscreen_layout_called() const { return fullscreen_layout_called_; }
private:
raw_ptr<views::Widget> widget_;
bool fullscreen_layout_called_ = false;
};
BEGIN_METADATA(FullscreenAwareFrame)
END_METADATA
} // namespace
// Tests that frame Layout is called when a widget goes fullscreen without
// changing its size or title.
TEST_F(WidgetTest, FullscreenFrameLayout) {
WidgetAutoclosePtr widget(CreateTopLevelPlatformWidget());
auto frame_view = std::make_unique<FullscreenAwareFrame>(widget.get());
FullscreenAwareFrame* frame = frame_view.get();
widget->non_client_view()->SetFrameView(std::move(frame_view));
widget->Maximize();
RunPendingMessages();
EXPECT_FALSE(frame->fullscreen_layout_called());
widget->SetFullscreen(true);
widget->Show();
#if BUILDFLAG(IS_MAC)
// On macOS, a fullscreen layout is triggered from within SetFullscreen.
// https://crbug.com/1307496
EXPECT_TRUE(frame->fullscreen_layout_called());
#else
EXPECT_TRUE(ViewTestApi(frame).needs_layout());
#endif
widget->LayoutRootViewIfNecessary();
RunPendingMessages();
EXPECT_TRUE(frame->fullscreen_layout_called());
}
namespace {
// Trivial WidgetObserverTest that invokes Widget::IsActive() from
// OnWindowDestroying.
class IsActiveFromDestroyObserver : public WidgetObserver {
public:
explicit IsActiveFromDestroyObserver(Widget* widget) {
widget_observation_.Observe(widget);
}
IsActiveFromDestroyObserver(const IsActiveFromDestroyObserver&) = delete;
IsActiveFromDestroyObserver& operator=(const IsActiveFromDestroyObserver&) =
delete;
~IsActiveFromDestroyObserver() override = default;
void OnWidgetDestroying(Widget* widget) override {
widget->IsActive();
widget_observation_.Reset();
}
private:
base::ScopedObservation<Widget, WidgetObserver> widget_observation_{this};
};
} // namespace
class ChildDesktopWidgetTest : public DesktopWidgetTest {
public:
Widget::InitParams CreateParams(Widget::InitParams::Ownership ownership,
Widget::InitParams::Type type) override {
Widget::InitParams params =
DesktopWidgetTest::CreateParams(ownership, type);
if (context_) {
params.context = context_;
}
return params;
}
std::unique_ptr<Widget> CreateChildWidget(gfx::NativeWindow context) {
context_ = context;
return CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
}
private:
gfx::NativeWindow context_ = gfx::NativeWindow();
};
// Verifies Widget::IsActive() invoked from
// WidgetObserver::OnWidgetDestroying() in a child widget doesn't crash.
TEST_F(ChildDesktopWidgetTest, IsActiveFromDestroy) {
// Create two widgets, one a child of the other.
std::unique_ptr<Widget> parent_widget =
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
parent_widget->Show();
std::unique_ptr<Widget> child_widget =
CreateChildWidget(parent_widget->GetNativeWindow());
IsActiveFromDestroyObserver observer(child_widget.get());
child_widget->Show();
parent_widget->CloseNow();
}
// Tests that events propagate through from the dispatcher with the correct
// event type, and that the different platforms behave the same.
TEST_F(WidgetTest, MouseEventTypesViaGenerator) {
WidgetAutoclosePtr widget(CreateTopLevelFramelessPlatformWidget());
EventCountView* view =
widget->GetRootView()->AddChildView(std::make_unique<EventCountView>());
view->set_handle_mode(EventCountView::HandleMode::kConsumeEvents);
view->SetBounds(10, 10, 50, 40);
widget->SetBounds(gfx::Rect(0, 0, 100, 80));
widget->Show();
auto generator =
CreateEventGenerator(GetContext(), widget->GetNativeWindow());
const gfx::Point view_center_point = view->GetBoundsInScreen().CenterPoint();
generator->set_current_screen_location(view_center_point);
generator->ClickLeftButton();
EXPECT_EQ(1, view->GetEventCount(ui::EventType::kMousePressed));
EXPECT_EQ(1, view->GetEventCount(ui::EventType::kMouseReleased));
EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON, view->last_flags());
generator->PressRightButton();
EXPECT_EQ(2, view->GetEventCount(ui::EventType::kMousePressed));
EXPECT_EQ(1, view->GetEventCount(ui::EventType::kMouseReleased));
EXPECT_EQ(ui::EF_RIGHT_MOUSE_BUTTON, view->last_flags());
generator->ReleaseRightButton();
EXPECT_EQ(2, view->GetEventCount(ui::EventType::kMousePressed));
EXPECT_EQ(2, view->GetEventCount(ui::EventType::kMouseReleased));
EXPECT_EQ(ui::EF_RIGHT_MOUSE_BUTTON, view->last_flags());
// Test mouse move events.
EXPECT_EQ(0, view->GetEventCount(ui::EventType::kMouseMoved));
EXPECT_EQ(0, view->GetEventCount(ui::EventType::kMouseEntered));
// Move the mouse a displacement of (10, 10).
generator->MoveMouseTo(view_center_point + gfx::Vector2d(10, 10));
EXPECT_EQ(1, view->GetEventCount(ui::EventType::kMouseMoved));
EXPECT_EQ(1, view->GetEventCount(ui::EventType::kMouseEntered));
EXPECT_EQ(ui::EF_NONE, view->last_flags());
// Move it again - entered count shouldn't change.
generator->MoveMouseTo(view_center_point + gfx::Vector2d(11, 11));
EXPECT_EQ(2, view->GetEventCount(ui::EventType::kMouseMoved));
EXPECT_EQ(1, view->GetEventCount(ui::EventType::kMouseEntered));
EXPECT_EQ(0, view->GetEventCount(ui::EventType::kMouseExited));
// Move it off the view.
const gfx::Point out_of_bounds_point =
view->GetBoundsInScreen().bottom_right() + gfx::Vector2d(10, 10);
generator->MoveMouseTo(out_of_bounds_point);
EXPECT_EQ(2, view->GetEventCount(ui::EventType::kMouseMoved));
EXPECT_EQ(1, view->GetEventCount(ui::EventType::kMouseEntered));
EXPECT_EQ(1, view->GetEventCount(ui::EventType::kMouseExited));
// Move it back on.
generator->MoveMouseTo(view_center_point);
EXPECT_EQ(3, view->GetEventCount(ui::EventType::kMouseMoved));
EXPECT_EQ(2, view->GetEventCount(ui::EventType::kMouseEntered));
EXPECT_EQ(1, view->GetEventCount(ui::EventType::kMouseExited));
// Drargging. Cover HasCapture() and NativeWidgetPrivate::IsMouseButtonDown().
generator->DragMouseTo(out_of_bounds_point);
EXPECT_EQ(3, view->GetEventCount(ui::EventType::kMousePressed));
EXPECT_EQ(3, view->GetEventCount(ui::EventType::kMouseReleased));
EXPECT_EQ(1, view->GetEventCount(ui::EventType::kMouseDragged));
EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON, view->last_flags());
}
// Tests that the root view is correctly set up for Widget types that do not
// require a non-client view, before any other views are added to the widget.
// That is, before Widget::ReorderNativeViews() is called which, if called with
// a root view not set, could cause the root view to get resized to the widget.
TEST_F(WidgetTest, NonClientWindowValidAfterInit) {
WidgetAutoclosePtr widget(CreateTopLevelFramelessPlatformWidget());
View* root_view = widget->GetRootView();
// Size the root view to exceed the widget bounds.
const gfx::Rect test_rect(0, 0, 500, 500);
root_view->SetBoundsRect(test_rect);
EXPECT_NE(test_rect.size(), widget->GetWindowBoundsInScreen().size());
EXPECT_EQ(test_rect, root_view->bounds());
widget->ReorderNativeViews();
EXPECT_EQ(test_rect, root_view->bounds());
}
#if BUILDFLAG(IS_WIN)
// Provides functionality to subclass a window and keep track of messages
// received.
class SubclassWindowHelper {
public:
explicit SubclassWindowHelper(HWND window)
: window_(window), message_to_destroy_on_(0) {
EXPECT_EQ(instance_, nullptr);
instance_ = this;
EXPECT_TRUE(Subclass());
}
SubclassWindowHelper(const SubclassWindowHelper&) = delete;
SubclassWindowHelper& operator=(const SubclassWindowHelper&) = delete;
~SubclassWindowHelper() {
Unsubclass();
instance_ = nullptr;
}
// Returns true if the |message| passed in was received.
bool received_message(unsigned int message) {
return (messages_.find(message) != messages_.end());
}
void Clear() { messages_.clear(); }
void set_message_to_destroy_on(unsigned int message) {
message_to_destroy_on_ = message;
}
private:
bool Subclass() {
old_proc_ = reinterpret_cast<WNDPROC>(::SetWindowLongPtr(
window_, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(WndProc)));
return old_proc_ != nullptr;
}
void Unsubclass() {
::SetWindowLongPtr(window_, GWLP_WNDPROC,
reinterpret_cast<LONG_PTR>(old_proc_));
}
static LRESULT CALLBACK WndProc(HWND window,
unsigned int message,
WPARAM w_param,
LPARAM l_param) {
EXPECT_NE(instance_, nullptr);
EXPECT_EQ(window, instance_->window_);
// Keep track of messags received for this window.
instance_->messages_.insert(message);
LRESULT ret = ::CallWindowProc(instance_->old_proc_, window, message,
w_param, l_param);
if (message == instance_->message_to_destroy_on_) {
instance_->Unsubclass();
::DestroyWindow(window);
}
return ret;
}
WNDPROC old_proc_;
HWND window_;
static SubclassWindowHelper* instance_;
std::set<unsigned int> messages_;
unsigned int message_to_destroy_on_;
};
SubclassWindowHelper* SubclassWindowHelper::instance_ = nullptr;
// This test validates whether the WM_SYSCOMMAND message for SC_MOVE is
// received when we post a WM_NCLBUTTONDOWN message for the caption in the
// following scenarios:-
// 1. Posting a WM_NCMOUSEMOVE message for a different location.
// 2. Posting a WM_NCMOUSEMOVE message with a different hittest code.
// 3. Posting a WM_MOUSEMOVE message.
// Disabled because of flaky timeouts: http://crbug.com/592742
TEST_F(DesktopWidgetTest,
DISABLED_SysCommandMoveOnNCLButtonDownOnCaptionAndMoveTest) {
std::unique_ptr<Widget> widget = CreateTestWidget(
Widget::InitParams::CLIENT_OWNS_WIDGET, Widget::InitParams::TYPE_WINDOW);
widget->Show();
::SetCursorPos(500, 500);
HWND window = widget->GetNativeWindow()->GetHost()->GetAcceleratedWidget();
SubclassWindowHelper subclass_helper(window);
// Posting just a WM_NCLBUTTONDOWN message should not result in a
// WM_SYSCOMMAND
::PostMessage(window, WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(100, 100));
RunPendingMessages();
EXPECT_TRUE(subclass_helper.received_message(WM_NCLBUTTONDOWN));
EXPECT_FALSE(subclass_helper.received_message(WM_SYSCOMMAND));
subclass_helper.Clear();
// Posting a WM_NCLBUTTONDOWN message followed by a WM_NCMOUSEMOVE at the
// same location should not result in a WM_SYSCOMMAND message.
::PostMessage(window, WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(100, 100));
::PostMessage(window, WM_NCMOUSEMOVE, HTCAPTION, MAKELPARAM(100, 100));
RunPendingMessages();
EXPECT_TRUE(subclass_helper.received_message(WM_NCLBUTTONDOWN));
EXPECT_TRUE(subclass_helper.received_message(WM_NCMOUSEMOVE));
EXPECT_FALSE(subclass_helper.received_message(WM_SYSCOMMAND));
subclass_helper.Clear();
// Posting a WM_NCLBUTTONDOWN message followed by a WM_NCMOUSEMOVE at a
// different location should result in a WM_SYSCOMMAND message.
::PostMessage(window, WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(100, 100));
::PostMessage(window, WM_NCMOUSEMOVE, HTCAPTION, MAKELPARAM(110, 110));
RunPendingMessages();
EXPECT_TRUE(subclass_helper.received_message(WM_NCLBUTTONDOWN));
EXPECT_TRUE(subclass_helper.received_message(WM_NCMOUSEMOVE));
EXPECT_TRUE(subclass_helper.received_message(WM_SYSCOMMAND));
subclass_helper.Clear();
// Posting a WM_NCLBUTTONDOWN message followed by a WM_NCMOUSEMOVE at a
// different location with a different hittest code should result in a
// WM_SYSCOMMAND message.
::PostMessage(window, WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(100, 100));
::PostMessage(window, WM_NCMOUSEMOVE, HTTOP, MAKELPARAM(110, 102));
RunPendingMessages();
EXPECT_TRUE(subclass_helper.received_message(WM_NCLBUTTONDOWN));
EXPECT_TRUE(subclass_helper.received_message(WM_NCMOUSEMOVE));
EXPECT_TRUE(subclass_helper.received_message(WM_SYSCOMMAND));
subclass_helper.Clear();
// Posting a WM_NCLBUTTONDOWN message followed by a WM_MOUSEMOVE should
// result in a WM_SYSCOMMAND message.
::PostMessage(window, WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(100, 100));
::PostMessage(window, WM_MOUSEMOVE, HTCLIENT, MAKELPARAM(110, 110));
RunPendingMessages();
EXPECT_TRUE(subclass_helper.received_message(WM_NCLBUTTONDOWN));
EXPECT_TRUE(subclass_helper.received_message(WM_MOUSEMOVE));
EXPECT_TRUE(subclass_helper.received_message(WM_SYSCOMMAND));
}
// This test validates that destroying the window in the context of the
// WM_SYSCOMMAND message with SC_MOVE does not crash.
// Disabled because of flaky timeouts: http://crbug.com/592742
TEST_F(DesktopWidgetTest, DISABLED_DestroyInSysCommandNCLButtonDownOnCaption) {
std::unique_ptr<Widget> widget = CreateTestWidget(
Widget::InitParams::CLIENT_OWNS_WIDGET, Widget::InitParams::TYPE_WINDOW);
widget->Show();
::SetCursorPos(500, 500);
HWND window = widget->GetNativeWindow()->GetHost()->GetAcceleratedWidget();
SubclassWindowHelper subclass_helper(window);
// Destroying the window in the context of the WM_SYSCOMMAND message
// should not crash.
subclass_helper.set_message_to_destroy_on(WM_SYSCOMMAND);
::PostMessage(window, WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(100, 100));
::PostMessage(window, WM_NCMOUSEMOVE, HTCAPTION, MAKELPARAM(110, 110));
RunPendingMessages();
EXPECT_TRUE(subclass_helper.received_message(WM_NCLBUTTONDOWN));
EXPECT_TRUE(subclass_helper.received_message(WM_SYSCOMMAND));
}
#endif
// Test that the z-order levels round-trip.
TEST_F(WidgetTest, ZOrderLevel) {
WidgetAutoclosePtr widget(CreateTopLevelNativeWidget());
EXPECT_EQ(ui::ZOrderLevel::kNormal, widget->GetZOrderLevel());
widget->SetZOrderLevel(ui::ZOrderLevel::kFloatingWindow);
EXPECT_EQ(ui::ZOrderLevel::kFloatingWindow, widget->GetZOrderLevel());
widget->SetZOrderLevel(ui::ZOrderLevel::kNormal);
EXPECT_EQ(ui::ZOrderLevel::kNormal, widget->GetZOrderLevel());
}
namespace {
class ScaleFactorView : public View {
METADATA_HEADER(ScaleFactorView, View)
public:
ScaleFactorView() = default;
ScaleFactorView(const ScaleFactorView&) = delete;
ScaleFactorView& operator=(const ScaleFactorView&) = delete;
// Overridden from ui::LayerDelegate:
void OnDeviceScaleFactorChanged(float old_device_scale_factor,
float new_device_scale_factor) override {
last_scale_factor_ = new_device_scale_factor;
View::OnDeviceScaleFactorChanged(old_device_scale_factor,
new_device_scale_factor);
}
float last_scale_factor() const { return last_scale_factor_; }
private:
float last_scale_factor_ = 0.f;
};
BEGIN_METADATA(ScaleFactorView)
END_METADATA
} // namespace
// Ensure scale factor changes are propagated from the native Widget.
TEST_F(WidgetTest, OnDeviceScaleFactorChanged) {
// Automatically close the widget, but not delete it.
WidgetAutoclosePtr widget(CreateTopLevelPlatformWidget());
ScaleFactorView* view = new ScaleFactorView;
widget->GetRootView()->AddChildViewRaw(view);
float scale_factor = widget->GetLayer()->device_scale_factor();
EXPECT_NE(scale_factor, 0.f);
// For views that are not layer-backed, adding the view won't notify the view
// about the initial scale factor. Fake it.
view->OnDeviceScaleFactorChanged(0.f, scale_factor);
EXPECT_EQ(scale_factor, view->last_scale_factor());
// Changes should be propagated.
scale_factor *= 2.0f;
widget->GetLayer()->OnDeviceScaleFactorChanged(scale_factor);
EXPECT_EQ(scale_factor, view->last_scale_factor());
}
// Test that WidgetRemovalsObserver::OnWillRemoveView is called when deleting
// a view.
TEST_F(WidgetTest, WidgetRemovalsObserverCalled) {
WidgetAutoclosePtr widget(CreateTopLevelPlatformWidget());
TestWidgetRemovalsObserver removals_observer;
widget->AddRemovalsObserver(&removals_observer);
View* parent = new View();
widget->client_view()->AddChildViewRaw(parent);
View* child = new View();
parent->AddChildViewRaw(child);
widget->client_view()->RemoveChildView(parent);
EXPECT_TRUE(removals_observer.DidRemoveView(parent));
EXPECT_FALSE(removals_observer.DidRemoveView(child));
// Calling RemoveChildView() doesn't delete the view, but deleting
// |parent| will automatically delete |child|.
delete parent;
widget->RemoveRemovalsObserver(&removals_observer);
}
// Test that WidgetRemovalsObserver::OnWillRemoveView is called when deleting
// the root view.
TEST_F(WidgetTest, WidgetRemovalsObserverCalledWhenRemovingRootView) {
WidgetAutoclosePtr widget(CreateTopLevelPlatformWidget());
TestWidgetRemovalsObserver removals_observer;
widget->AddRemovalsObserver(&removals_observer);
views::View* root_view = widget->GetRootView();
widget.reset();
EXPECT_TRUE(removals_observer.DidRemoveView(root_view));
}
// Test that WidgetRemovalsObserver::OnWillRemoveView is called when moving
// a view from one widget to another, but not when moving a view within
// the same widget.
TEST_F(WidgetTest, WidgetRemovalsObserverCalledWhenMovingBetweenWidgets) {
WidgetAutoclosePtr widget(CreateTopLevelPlatformWidget());
TestWidgetRemovalsObserver removals_observer;
widget->AddRemovalsObserver(&removals_observer);
View* parent = new View();
widget->client_view()->AddChildViewRaw(parent);
View* child = new View();
widget->client_view()->AddChildViewRaw(child);
// Reparenting the child shouldn't call the removals observer.
parent->AddChildViewRaw(child);
EXPECT_FALSE(removals_observer.DidRemoveView(child));
// Moving the child to a different widget should call the removals observer.
WidgetAutoclosePtr widget2(CreateTopLevelPlatformWidget());
widget2->client_view()->AddChildViewRaw(child);
EXPECT_TRUE(removals_observer.DidRemoveView(child));
widget->RemoveRemovalsObserver(&removals_observer);
}
// Test dispatch of ui::EventType::kMousewheel.
TEST_F(WidgetTest, MouseWheelEvent) {
WidgetAutoclosePtr widget(CreateTopLevelPlatformWidget());
widget->SetBounds(gfx::Rect(0, 0, 600, 600));
EventCountView* event_count_view =
widget->client_view()->AddChildView(std::make_unique<EventCountView>());
event_count_view->SetBounds(0, 0, 600, 600);
widget->Show();
auto event_generator =
CreateEventGenerator(GetContext(), widget->GetNativeWindow());
event_generator->MoveMouseWheel(1, 1);
EXPECT_EQ(1, event_count_view->GetEventCount(ui::EventType::kMousewheel));
}
// Test that ui::EventType::kMouseEntered is dispatched even when not followed
// by ui::EventType::kMouseMoved.
TEST_F(WidgetTest, MouseEnteredWithoutMoved) {
WidgetAutoclosePtr widget(CreateTopLevelFramelessPlatformWidget());
widget->SetBounds(gfx::Rect(0, 0, 100, 100));
auto* root_view = static_cast<internal::RootView*>(widget->GetRootView());
auto* v1 = root_view->AddChildView(std::make_unique<EventCountView>());
v1->SetBounds(10, 10, 10, 10);
auto* v2 = root_view->AddChildView(std::make_unique<EventCountView>());
v2->SetBounds(20, 10, 10, 10);
widget->Show();
auto generator =
CreateEventGenerator(GetContext(), widget->GetNativeWindow());
// Enter |v1| and check that it received ui::EventType::kMouseEntered.
auto enter_location = v1->GetBoundsInScreen().CenterPoint();
generator->set_current_screen_location(enter_location);
generator->SendMouseEnter();
EXPECT_EQ(v1, GetMouseMoveHandler(root_view));
EXPECT_EQ(1, v1->GetEventCount(ui::EventType::kMouseEntered));
EXPECT_EQ(0, v1->GetEventCount(ui::EventType::kMouseMoved));
EXPECT_EQ(0, v1->GetEventCount(ui::EventType::kMouseExited));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kMouseEntered));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kMouseMoved));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kMouseExited));
// Enter |v2| and check that |v1| received ui::EventType::kMouseExited and
// |v2| received ui::EventType::kMouseEntered.
enter_location = v2->GetBoundsInScreen().CenterPoint();
generator->set_current_screen_location(enter_location);
generator->SendMouseEnter();
EXPECT_EQ(v2, GetMouseMoveHandler(root_view));
EXPECT_EQ(1, v1->GetEventCount(ui::EventType::kMouseEntered));
EXPECT_EQ(0, v1->GetEventCount(ui::EventType::kMouseMoved));
EXPECT_EQ(1, v1->GetEventCount(ui::EventType::kMouseExited));
EXPECT_EQ(1, v2->GetEventCount(ui::EventType::kMouseEntered));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kMouseMoved));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kMouseExited));
// Enter |root_view| and check that |v2| received ui::EventType::kMouseExited.
enter_location = gfx::Point(0, 0);
generator->set_current_screen_location(enter_location);
generator->SendMouseEnter();
EXPECT_EQ(nullptr, GetMouseMoveHandler(root_view));
EXPECT_EQ(1, v1->GetEventCount(ui::EventType::kMouseEntered));
EXPECT_EQ(0, v1->GetEventCount(ui::EventType::kMouseMoved));
EXPECT_EQ(1, v1->GetEventCount(ui::EventType::kMouseExited));
EXPECT_EQ(1, v2->GetEventCount(ui::EventType::kMouseEntered));
EXPECT_EQ(0, v2->GetEventCount(ui::EventType::kMouseMoved));
EXPECT_EQ(1, v2->GetEventCount(ui::EventType::kMouseExited));
}
// Test that ui::EventType::kMouseMoved after ui::EventType::kMouseEntered
// doesn't cause an extra ui::EventType::kMouseEntered.
TEST_F(WidgetTest, MouseMovedAfterEnteredDoesntCauseEntered) {
WidgetAutoclosePtr widget(CreateTopLevelFramelessPlatformWidget());
widget->SetBounds(gfx::Rect(0, 0, 100, 100));
auto* root_view = widget->GetRootView();
auto* v = root_view->AddChildView(std::make_unique<EventCountView>());
v->SetBounds(10, 10, 10, 10);
widget->Show();
auto generator =
CreateEventGenerator(GetContext(), widget->GetNativeWindow());
// Enter |v|.
auto enter_location = v->GetBoundsInScreen().CenterPoint();
generator->set_current_screen_location(enter_location);
generator->SendMouseEnter();
// Send ui::EventType::kMouseMoved at the same location and check that it
// didn't generate ui::EventType::kMouseEntered again.
generator->MoveMouseBy(0, 0);
EXPECT_EQ(1, v->GetEventCount(ui::EventType::kMouseEntered));
EXPECT_EQ(1, v->GetEventCount(ui::EventType::kMouseMoved));
// Reset state by entering |root_view|.
generator->MoveMouseTo(0, 0);
// Enter |v| again.
generator->set_current_screen_location(enter_location);
generator->SendMouseEnter();
// Send ui::EventType::kMouseMoved at a slightly offset location and check
// that it didn't generate ui::EventType::kMouseEntered again.
generator->MoveMouseBy(1, 1);
EXPECT_EQ(2, v->GetEventCount(ui::EventType::kMouseEntered));
EXPECT_EQ(2, v->GetEventCount(ui::EventType::kMouseMoved));
}
class CloseFromClosingObserver : public WidgetObserver {
public:
~CloseFromClosingObserver() override {
EXPECT_TRUE(was_on_widget_closing_called_);
}
// WidgetObserver:
void OnWidgetClosing(Widget* widget) override {
// OnWidgetClosing() should only be called once, even if Close() is called
// after CloseNow().
ASSERT_FALSE(was_on_widget_closing_called_);
was_on_widget_closing_called_ = true;
widget->Close();
}
private:
bool was_on_widget_closing_called_ = false;
};
TEST_F(WidgetTest, CloseNowFollowedByCloseDoesntCallOnWidgetClosingTwice) {
CloseFromClosingObserver observer;
std::unique_ptr<Widget> widget = std::make_unique<Widget>();
Widget::InitParams params = CreateParams(
Widget::InitParams::CLIENT_OWNS_WIDGET, Widget::InitParams::TYPE_WINDOW);
widget->Init(std::move(params));
widget->AddObserver(&observer);
widget->CloseNow();
widget->RemoveObserver(&observer);
widget.reset();
// Assertions are in CloseFromClosingObserver.
}
namespace {
class TestSaveWindowPlacementWidgetDelegate : public TestDesktopWidgetDelegate {
public:
TestSaveWindowPlacementWidgetDelegate() = default;
TestSaveWindowPlacementWidgetDelegate(
const TestSaveWindowPlacementWidgetDelegate&) = delete;
TestSaveWindowPlacementWidgetDelegate operator=(
const TestSaveWindowPlacementWidgetDelegate&) = delete;
~TestSaveWindowPlacementWidgetDelegate() override = default;
void set_should_save_window_placement(bool should_save) {
should_save_window_placement_ = should_save;
}
int save_window_placement_count() const {
return save_window_placement_count_;
}
// ViewsDelegate:
std::string GetWindowName() const final { return GetWidget()->GetName(); }
bool ShouldSaveWindowPlacement() const final {
return should_save_window_placement_;
}
void SaveWindowPlacement(const gfx::Rect& bounds,
ui::mojom::WindowShowState show_state) override {
save_window_placement_count_++;
}
private:
bool should_save_window_placement_ = true;
int save_window_placement_count_ = 0;
};
} // namespace
TEST_F(WidgetTest, ShouldSaveWindowPlacement) {
for (bool save : {false, true}) {
SCOPED_TRACE(save ? "ShouldSave" : "ShouldNotSave");
TestSaveWindowPlacementWidgetDelegate widget_delegate;
widget_delegate.set_should_save_window_placement(save);
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
params.ownership = Widget::InitParams::CLIENT_OWNS_WIDGET;
params.name = "TestWidget";
widget_delegate.InitWidget(std::move(params));
auto* widget = widget_delegate.GetWidget();
widget->Close();
EXPECT_EQ(save ? 1 : 0, widget_delegate.save_window_placement_count());
}
}
TEST_F(WidgetTest, RootViewAccessibilityCacheInitialized) {
std::unique_ptr<Widget> widget =
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
widget->Show();
EXPECT_TRUE(widget->GetRootView()->GetViewAccessibility().is_initialized());
}
TEST_F(WidgetTest, ClientViewAccessibilityProperties) {
std::unique_ptr<Widget> widget = CreateTestWidget(
Widget::InitParams::CLIENT_OWNS_WIDGET, Widget::InitParams::TYPE_WINDOW);
widget->Show();
ui::AXNodeData node_data;
widget->client_view()->GetViewAccessibility().GetAccessibleNodeData(
&node_data);
EXPECT_EQ(node_data.role, ax::mojom::Role::kClient);
}
TEST_F(WidgetTest, NonClientViewAccessibilityProperties) {
std::unique_ptr<Widget> widget = CreateTestWidget(
Widget::InitParams::CLIENT_OWNS_WIDGET, Widget::InitParams::TYPE_WINDOW);
NonClientView* non_client_view = widget->non_client_view();
non_client_view->SetFrameView(
std::make_unique<MinimumSizeFrameView>(widget.get()));
widget->Show();
ui::AXNodeData node_data;
non_client_view->GetViewAccessibility().GetAccessibleNodeData(&node_data);
EXPECT_EQ(node_data.role, ax::mojom::Role::kClient);
node_data = ui::AXNodeData();
non_client_view->frame_view()->GetViewAccessibility().GetAccessibleNodeData(
&node_data);
EXPECT_EQ(node_data.role, ax::mojom::Role::kClient);
}
TEST_F(WidgetTest, UpdateAccessibleURLForRootView) {
std::unique_ptr<Widget> widget = CreateTestWidget(
Widget::InitParams::CLIENT_OWNS_WIDGET, Widget::InitParams::TYPE_WINDOW);
widget->Show();
const GURL test_url("https://example.com");
widget->UpdateAccessibleURLForRootView(test_url);
ui::AXNodeData node_data;
widget->GetRootView()->GetViewAccessibility().GetAccessibleNodeData(
&node_data);
EXPECT_EQ(node_data.GetStringAttribute(ax::mojom::StringAttribute::kUrl),
test_url);
}
class WidgetChildObserver : public WidgetObserver {
public:
explicit WidgetChildObserver(Widget* widget) { observation_.Observe(widget); }
const Widget* child_widget() const { return child_widget_; }
private:
// WidgetObserver:
void OnWidgetChildAdded(Widget* widget, Widget* child_widget) override {
child_widget_ = child_widget;
WidgetObserver::OnWidgetChildAdded(widget, child_widget);
}
void OnWidgetChildRemoved(Widget* widget, Widget* child_widget) override {
EXPECT_EQ(widget, observation_.GetSource());
EXPECT_EQ(child_widget, child_widget_);
child_widget_ = nullptr;
WidgetObserver::OnWidgetChildRemoved(widget, child_widget);
}
base::ScopedObservation<Widget, WidgetObserver> observation_{this};
raw_ptr<Widget> child_widget_ = nullptr;
};
TEST_F(WidgetTest, ChildWidgetNotifiesObserverWhenInitializedAndDestroyed) {
// Adding a child widget should call back the observer.
std::unique_ptr<Widget> widget = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
WidgetChildObserver observer(widget.get());
std::unique_ptr<Widget> child_widget =
base::WrapUnique(CreateChildPlatformWidget(
widget->GetNativeView(), Widget::InitParams::CLIENT_OWNS_WIDGET));
EXPECT_EQ(observer.child_widget(), child_widget.get());
// Destroy the child and verify that the observer was notified.
child_widget.reset();
EXPECT_EQ(observer.child_widget(), nullptr);
}
TEST_F(WidgetTest, ChildWidgetNotifiesObserverWhenReparented) {
// Verify that reparenting a child widget notifies both the outgoing and
// incoming parent widgets.
std::unique_ptr<Widget> widget_1 = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
WidgetChildObserver observer_1(widget_1.get());
std::unique_ptr<Widget> widget_2 = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
WidgetChildObserver observer_2(widget_2.get());
// Create a child widget of `widget_1`.
std::unique_ptr<Widget> child_widget =
base::WrapUnique(CreateChildPlatformWidget(
widget_1->GetNativeView(), Widget::InitParams::CLIENT_OWNS_WIDGET));
EXPECT_EQ(observer_1.child_widget(), child_widget.get());
Widget::ReparentNativeView(child_widget->GetNativeView(),
widget_2->GetNativeView());
EXPECT_EQ(observer_1.child_widget(), nullptr);
EXPECT_EQ(observer_2.child_widget(), child_widget.get());
child_widget.reset();
EXPECT_EQ(observer_2.child_widget(), nullptr);
}
TEST_F(WidgetTest, NativeWidgetNotifiedOfWidgetDestructionForClientOwnsWidget) {
auto widget = std::make_unique<Widget>();
Widget::InitParams params = CreateParams(
Widget::InitParams::CLIENT_OWNS_WIDGET, Widget::InitParams::TYPE_WINDOW);
auto native_widget =
std::make_unique<testing::NiceMock<MockNativeWidget>>(widget.get());
ON_CALL(*native_widget, CreateNonClientFrameView).WillByDefault([]() {
return std::make_unique<NonClientFrameView>();
});
params.native_widget = native_widget.get();
widget->Init(std::move(params));
EXPECT_CALL(*native_widget, ClientDestroyedWidget());
widget.reset();
}
// Parameterized test that verifies the behavior of SetAspectRatio with respect
// to the excluded margin.
class WidgetSetAspectRatioTest
: public ViewsTestBase,
public testing::WithParamInterface<gfx::Size /* margin */> {
public:
WidgetSetAspectRatioTest() : margin_(GetParam()) {}
WidgetSetAspectRatioTest(const WidgetSetAspectRatioTest&) = delete;
WidgetSetAspectRatioTest& operator=(const WidgetSetAspectRatioTest&) = delete;
~WidgetSetAspectRatioTest() override = default;
// ViewsTestBase:
void SetUp() override {
ViewsTestBase::SetUp();
widget_ = std::make_unique<Widget>();
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
native_widget_ = std::make_unique<MockNativeWidget>(widget());
ON_CALL(*native_widget(), CreateNonClientFrameView).WillByDefault([this]() {
return std::make_unique<NonClientFrameViewWithFixedMargin>(margin());
});
params.native_widget = native_widget();
widget()->Init(std::move(params));
task_environment()->RunUntilIdle();
}
void TearDown() override {
// `ViewAccessibility` objects have some references to the `widget` which
// must be updated when the widget is freed. The function that is in charge
// of clearing these lists however (`OnNativeWidgetDestroying`), is never
// called in this test suite because we use a `MockNativeWindow` rather than
// a `NativeWindow`. So we make sure this clean up happens manually.
widget()->OnNativeWidgetDestroying();
native_widget_.reset();
widget()->Close();
widget_.reset();
ViewsTestBase::TearDown();
}
const gfx::Size& margin() const { return margin_; }
Widget* widget() { return widget_.get(); }
MockNativeWidget* native_widget() { return native_widget_.get(); }
private:
// Margin around the client view that should be excluded.
const gfx::Size margin_;
std::unique_ptr<Widget> widget_;
std::unique_ptr<MockNativeWidget> native_widget_;
// `NonClientFrameView` that pads the client view with a fixed-size margin,
// to leave room for drawing that's not included in the aspect ratio.
class NonClientFrameViewWithFixedMargin : public NonClientFrameView {
public:
// `margin` is the margin that we'll provide to our client view.
explicit NonClientFrameViewWithFixedMargin(const gfx::Size& margin)
: margin_(margin) {}
// NonClientFrameView
gfx::Rect GetBoundsForClientView() const override {
gfx::Rect r = bounds();
return gfx::Rect(r.x(), r.y(), r.width() - margin_.width(),
r.height() - margin_.height());
}
const gfx::Size margin_;
};
};
TEST_P(WidgetSetAspectRatioTest, SetAspectRatioIncludesMargin) {
// Provide a nonzero size. It doesn't particularly matter what, as long as
// it's larger than our margin.
const gfx::Rect root_view_bounds(0, 0, 100, 200);
ASSERT_GT(root_view_bounds.width(), margin().width());
ASSERT_GT(root_view_bounds.height(), margin().height());
widget()->non_client_view()->SetBoundsRect(root_view_bounds);
// Verify that the excluded margin matches the margin that our custom
// non-client frame provides.
const gfx::SizeF aspect_ratio(1.5f, 1.0f);
EXPECT_CALL(*native_widget(), SetAspectRatio(aspect_ratio, margin()));
widget()->SetAspectRatio(aspect_ratio);
}
INSTANTIATE_TEST_SUITE_P(WidgetSetAspectRatioTestInstantiation,
WidgetSetAspectRatioTest,
::testing::Values(gfx::Size(15, 20), gfx::Size(0, 0)));
class WidgetShadowTest : public WidgetTest {
public:
WidgetShadowTest() = default;
WidgetShadowTest(const WidgetShadowTest&) = delete;
WidgetShadowTest& operator=(const WidgetShadowTest&) = delete;
~WidgetShadowTest() override = default;
// WidgetTest:
void SetUp() override {
set_native_widget_type(NativeWidgetType::kDesktop);
WidgetTest::SetUp();
InitControllers();
}
void TearDown() override {
#if defined(USE_AURA) && !BUILDFLAG(ENABLE_DESKTOP_AURA)
shadow_controller_.reset();
focus_controller_.reset();
#endif
WidgetTest::TearDown();
}
Widget::InitParams CreateParams(Widget::InitParams::Ownership ownership,
Widget::InitParams::Type type) override {
Widget::InitParams params =
WidgetTest::CreateParams(ownership, override_type_.value_or(type));
params.shadow_type = Widget::InitParams::ShadowType::kDrop;
params.shadow_elevation = 10;
params.name = name_;
params.child = force_child_;
return params;
}
protected:
std::optional<Widget::InitParams::Type> override_type_;
std::string name_;
bool force_child_ = false;
private:
#if BUILDFLAG(ENABLE_DESKTOP_AURA) || BUILDFLAG(IS_MAC)
void InitControllers() {}
#else
class TestFocusRules : public wm::BaseFocusRules {
public:
TestFocusRules() = default;
TestFocusRules(const TestFocusRules&) = delete;
TestFocusRules& operator=(const TestFocusRules&) = delete;
bool SupportsChildActivation(const aura::Window* window) const override {
return true;
}
};
void InitControllers() {
focus_controller_ =
std::make_unique<wm::FocusController>(new TestFocusRules);
shadow_controller_ = std::make_unique<wm::ShadowController>(
focus_controller_.get(), nullptr);
}
std::unique_ptr<wm::FocusController> focus_controller_;
std::unique_ptr<wm::ShadowController> shadow_controller_;
#endif // !BUILDFLAG(ENABLE_DESKTOP_AURA) && !BUILDFLAG(IS_MAC)
};
// Disabled on Mac: All drop shadows are managed out of process for now.
#if BUILDFLAG(IS_MAC)
#define MAYBE_ShadowsInRootWindow DISABLED_ShadowsInRootWindow
#else
#define MAYBE_ShadowsInRootWindow ShadowsInRootWindow
#endif
// Test that shadows are not added to root windows when created or upon
// activation. Test that shadows are added to non-root windows even if not
// activated.
TEST_F(WidgetShadowTest, MAYBE_ShadowsInRootWindow) {
#if defined(USE_AURA) && !BUILDFLAG(ENABLE_DESKTOP_AURA)
// On ChromeOS, top-levels have shadows.
bool top_level_window_should_have_shadow = true;
#else
// On non-chromeos platforms, the hosting OS is responsible for the shadow.
bool top_level_window_should_have_shadow = false;
#endif
// To start, just create a Widget. This constructs the first ShadowController
// which will start observing the environment for additional aura::Window
// initialization. The very first ShadowController in DesktopNativeWidgetAura
// is created after the call to aura::Window::Init(), so the ShadowController
// Impl class won't ever see this first Window being initialized.
name_ = "other_top_level";
Widget* other_top_level = CreateTopLevelNativeWidget();
name_ = "top_level";
Widget* top_level = CreateTopLevelNativeWidget();
top_level->SetBounds(gfx::Rect(100, 100, 320, 200));
EXPECT_FALSE(WidgetHasInProcessShadow(top_level));
EXPECT_FALSE(top_level->IsVisible());
top_level->ShowInactive();
EXPECT_EQ(top_level_window_should_have_shadow,
WidgetHasInProcessShadow(top_level));
top_level->Show();
EXPECT_EQ(top_level_window_should_have_shadow,
WidgetHasInProcessShadow(top_level));
name_ = "control";
Widget* control = CreateChildNativeWidgetWithParent(top_level);
control->SetBounds(gfx::Rect(20, 20, 160, 100));
// Widgets of TYPE_CONTROL become visible during Init, so start with a shadow.
EXPECT_TRUE(WidgetHasInProcessShadow(control));
control->ShowInactive();
EXPECT_TRUE(WidgetHasInProcessShadow(control));
control->Show();
EXPECT_TRUE(WidgetHasInProcessShadow(control));
name_ = "child";
override_type_ = Widget::InitParams::TYPE_POPUP;
force_child_ = true;
Widget* child = CreateChildNativeWidgetWithParent(top_level);
child->SetBounds(gfx::Rect(20, 20, 160, 100));
// Now false: the Widget hasn't been shown yet.
EXPECT_FALSE(WidgetHasInProcessShadow(child));
child->ShowInactive();
EXPECT_TRUE(WidgetHasInProcessShadow(child));
child->Show();
EXPECT_TRUE(WidgetHasInProcessShadow(child));
other_top_level->Show();
// Re-activate the top level window. This handles a hypothetical case where
// a shadow is added via the ActivationChangeObserver rather than by the
// aura::WindowObserver. Activation changes only modify an existing shadow
// (if there is one), but should never install a Shadow, even if the Window
// properties otherwise say it should have one.
top_level->Show();
EXPECT_EQ(top_level_window_should_have_shadow,
WidgetHasInProcessShadow(top_level));
top_level->Close();
other_top_level->Close();
}
#if BUILDFLAG(IS_WIN)
// Tests the case where an intervening owner popup window is destroyed out from
// under the currently active modal top-level window. In this instance, the
// remaining top-level windows should be re-enabled.
TEST_F(DesktopWidgetTest, WindowModalOwnerDestroyedEnabledTest) {
// top_level_widget owns owner_dialog_widget which owns owned_dialog_widget.
std::unique_ptr<Widget> top_level_widget =
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET);
top_level_widget->Show();
// Create the owner modal dialog.
const auto create_params = [this](Widget* widget, gfx::NativeView parent) {
Widget::InitParams init_params =
CreateParamsForTestWidget(Widget::InitParams::TYPE_WINDOW);
init_params.delegate = new WidgetDelegate();
init_params.delegate->SetModalType(ui::mojom::ModalType::kWindow);
init_params.parent = parent;
init_params.native_widget =
new test::TestPlatformNativeWidget<DesktopNativeWidgetAura>(
widget, false, nullptr);
return init_params;
};
Widget owner_dialog_widget;
owner_dialog_widget.Init(
create_params(&owner_dialog_widget, top_level_widget->GetNativeView()));
owner_dialog_widget.Show();
HWND owner_hwnd = HWNDForWidget(&owner_dialog_widget);
// Create the owned modal dialog.
Widget owned_dialog_widget;
owned_dialog_widget.Init(
create_params(&owned_dialog_widget, owner_dialog_widget.GetNativeView()));
owned_dialog_widget.Show();
HWND owned_hwnd = HWNDForWidget(&owned_dialog_widget);
RunPendingMessages();
HWND top_hwnd = HWNDForWidget(top_level_widget.get());
EXPECT_FALSE(!!IsWindowEnabled(owner_hwnd));
EXPECT_FALSE(!!IsWindowEnabled(top_hwnd));
EXPECT_TRUE(!!IsWindowEnabled(owned_hwnd));
owner_dialog_widget.CloseNow();
RunPendingMessages();
EXPECT_FALSE(!!IsWindow(owner_hwnd));
EXPECT_FALSE(!!IsWindow(owned_hwnd));
EXPECT_TRUE(!!IsWindowEnabled(top_hwnd));
top_level_widget->CloseNow();
}
TEST_F(DesktopWidgetTest, StackAboveTest) {
WidgetAutoclosePtr root_one(CreateTopLevelNativeWidget());
WidgetAutoclosePtr root_two(CreateTopLevelNativeWidget());
Widget* child_one = CreateChildNativeWidgetWithParent(root_one->AsWidget());
Widget* child_one_b = CreateChildNativeWidgetWithParent(root_one->AsWidget());
Widget* child_two = CreateChildNativeWidgetWithParent(root_two->AsWidget());
Widget* grandchild_one =
CreateChildNativeWidgetWithParent(child_one->AsWidget());
Widget* grandchild_two =
CreateChildNativeWidgetWithParent(child_two->AsWidget());
root_one->ShowInactive();
child_one->ShowInactive();
child_one_b->ShowInactive();
grandchild_one->ShowInactive();
root_two->ShowInactive();
child_two->ShowInactive();
grandchild_two->ShowInactive();
// Creates the following where Z-Order is from Left to Right.
// root_one root_two
// / \ /
// child_one_b child_one child_two
// / /
// grandchild_one grandchild_two
//
// Note: child_one and grandchild_one were brought to front
// when grandchild_one was shown.
// Child elements are stacked above parent.
EXPECT_TRUE(child_one->IsStackedAbove(root_one->GetNativeView()));
EXPECT_TRUE(child_one_b->IsStackedAbove(root_one->GetNativeView()));
EXPECT_TRUE(grandchild_one->IsStackedAbove(child_one->GetNativeView()));
EXPECT_TRUE(grandchild_two->IsStackedAbove(root_two->GetNativeView()));
// Siblings with higher z-order are stacked correctly.
EXPECT_TRUE(child_one->IsStackedAbove(child_one_b->GetNativeView()));
EXPECT_TRUE(grandchild_one->IsStackedAbove(child_one_b->GetNativeView()));
// Root elements are stacked above child of a root with lower z-order.
EXPECT_TRUE(root_two->IsStackedAbove(root_one->GetNativeView()));
EXPECT_TRUE(root_two->IsStackedAbove(child_one_b->GetNativeView()));
// Child elements are stacked above child of root with lower z-order.
EXPECT_TRUE(child_two->IsStackedAbove(child_one_b->GetNativeView()));
EXPECT_TRUE(child_two->IsStackedAbove(grandchild_one->GetNativeView()));
EXPECT_TRUE(grandchild_two->IsStackedAbove(child_one->GetNativeView()));
EXPECT_TRUE(grandchild_two->IsStackedAbove(root_one->GetNativeView()));
// False cases to verify function is not just returning true for all cases.
EXPECT_FALSE(root_one->IsStackedAbove(grandchild_two->GetNativeView()));
EXPECT_FALSE(root_one->IsStackedAbove(grandchild_one->GetNativeView()));
EXPECT_FALSE(child_two->IsStackedAbove(grandchild_two->GetNativeView()));
EXPECT_FALSE(child_one->IsStackedAbove(grandchild_two->GetNativeView()));
EXPECT_FALSE(child_one_b->IsStackedAbove(child_two->GetNativeView()));
EXPECT_FALSE(grandchild_one->IsStackedAbove(grandchild_two->GetNativeView()));
EXPECT_FALSE(grandchild_one->IsStackedAbove(root_two->GetNativeView()));
EXPECT_FALSE(child_one_b->IsStackedAbove(grandchild_one->GetNativeView()));
}
#endif // BUILDFLAG(IS_WIN)
#if BUILDFLAG(ENABLE_DESKTOP_AURA) || BUILDFLAG(IS_MAC)
namespace {
class CompositingWidgetTest : public DesktopWidgetTest {
public:
CompositingWidgetTest()
: widget_types_{Widget::InitParams::TYPE_WINDOW,
Widget::InitParams::TYPE_WINDOW_FRAMELESS,
Widget::InitParams::TYPE_CONTROL,
Widget::InitParams::TYPE_POPUP,
Widget::InitParams::TYPE_MENU,
Widget::InitParams::TYPE_TOOLTIP,
Widget::InitParams::TYPE_BUBBLE,
Widget::InitParams::TYPE_DRAG} {}
CompositingWidgetTest(const CompositingWidgetTest&) = delete;
CompositingWidgetTest& operator=(const CompositingWidgetTest&) = delete;
~CompositingWidgetTest() override = default;
Widget::InitParams CreateParams(Widget::InitParams::Ownership ownership,
Widget::InitParams::Type type) override {
Widget::InitParams params =
DesktopWidgetTest::CreateParams(ownership, type);
params.opacity = opacity_;
return params;
}
void CheckAllWidgetsForOpacity(
const Widget::InitParams::WindowOpacity opacity) {
opacity_ = opacity;
for (const auto& widget_type : widget_types_) {
#if BUILDFLAG(IS_MAC)
// Tooltips are native on Mac. See NativeWidgetNSWindowBridge::Init.
if (widget_type == Widget::InitParams::TYPE_TOOLTIP) {
continue;
}
#elif BUILDFLAG(IS_WIN)
// Other widget types would require to create a parent window and the
// the purpose of this test is mainly X11 in the first place.
if (widget_type != Widget::InitParams::TYPE_WINDOW) {
continue;
}
#endif
std::unique_ptr<Widget> widget =
CreateTestWidget(Widget::InitParams::CLIENT_OWNS_WIDGET, widget_type);
// Use NativeWidgetAura directly.
if (widget_type == Widget::InitParams::TYPE_WINDOW_FRAMELESS ||
widget_type == Widget::InitParams::TYPE_CONTROL) {
continue;
}
#if BUILDFLAG(IS_MAC)
// Mac always always has a compositing window manager, but doesn't have
// transparent titlebars which is what ShouldWindowContentsBeTransparent()
// is currently used for. Asking for transparency should get it. Note that
// TestViewsDelegate::use_transparent_windows_ determines the result of
// kInferOpacity: assume it is false.
bool should_be_transparent =
opacity_ == Widget::InitParams::WindowOpacity::kTranslucent;
#else
bool should_be_transparent = widget->ShouldWindowContentsBeTransparent();
#endif
EXPECT_EQ(IsNativeWindowTransparent(widget->GetNativeWindow()),
should_be_transparent);
}
}
protected:
const std::vector<Widget::InitParams::Type> widget_types_;
Widget::InitParams::WindowOpacity opacity_ =
Widget::InitParams::WindowOpacity::kInferred;
};
} // namespace
// Only test manually set opacity via kOpaque or kTranslucent. kInferred is
// unpredictable and depends on the platform and window type.
TEST_F(CompositingWidgetTest, Transparency_DesktopWidgetOpaque) {
CheckAllWidgetsForOpacity(Widget::InitParams::WindowOpacity::kOpaque);
}
TEST_F(CompositingWidgetTest, Transparency_DesktopWidgetTranslucent) {
CheckAllWidgetsForOpacity(Widget::InitParams::WindowOpacity::kTranslucent);
}
namespace {
class ScreenshotWidgetTest : public ViewsTestBase {
public:
ScreenshotWidgetTest() = default;
// ViewsTestBase:
void SetUp() override {
ViewsTestBase::SetUp();
widget_ = std::make_unique<Widget>();
Widget::InitParams params =
CreateParams(Widget::InitParams::CLIENT_OWNS_WIDGET,
Widget::InitParams::TYPE_WINDOW);
native_widget_ =
std::make_unique<testing::NiceMock<MockNativeWidget>>(widget());
EXPECT_CALL(*native_widget(), SetAllowScreenshots(_))
.WillOnce([this](bool allowed) {
screenshots_allowed_ = allowed;
return true;
});
params.native_widget = native_widget();
widget()->Init(std::move(params));
}
void TearDown() override {
// `ViewAccessibility` objects have some references to the `widget` which
// must be updated when the widget is freed. The function that is in charge
// of clearing these lists however (`OnNativeWidgetDestroying`), is never
// called in this test suite because we use a `MockNativeWindow` rather than
// a `NativeWindow`. So we make sure this clean up happens manually.
widget()->OnNativeWidgetDestroying();
native_widget_.reset();
ViewsTestBase::TearDown();
}
Widget* widget() { return widget_.get(); }
MockNativeWidget* native_widget() { return native_widget_.get(); }
const std::optional<bool>& screenshots_allowed() {
return screenshots_allowed_;
}
private:
std::unique_ptr<Widget> widget_;
std::unique_ptr<MockNativeWidget> native_widget_;
std::optional<bool> screenshots_allowed_;
};
} // namespace
TEST_F(ScreenshotWidgetTest, CallsNativeWidget) {
widget()->SetAllowScreenshots(false);
ASSERT_TRUE(screenshots_allowed().has_value());
EXPECT_FALSE(screenshots_allowed().value());
}
#endif // BUILDFLAG(ENABLE_DESKTOP_AURA) || BUILDFLAG(IS_MAC)
namespace {
class WidgetModalVisibilityObserver : public WidgetObserver {
public:
MOCK_METHOD(void,
OnWidgetWindowModalVisibilityChanged,
(Widget*, bool),
(override));
};
} // namespace
TEST_F(WidgetTest, ChildWidgetNotifiesModalVisibilityChanged) {
std::unique_ptr<Widget> widget = base::WrapUnique(
CreateTopLevelPlatformWidget(Widget::InitParams::CLIENT_OWNS_WIDGET));
testing::NiceMock<WidgetModalVisibilityObserver> observer;
base::ScopedObservation<Widget, WidgetObserver> observation(&observer);
observation.Observe(widget.get());
widget->Show();
auto child_widget_delegate = std::make_unique<WidgetDelegate>();
auto child_widget = std::make_unique<Widget>();
child_widget_delegate->SetModalType(ui::mojom::ModalType::kWindow);
Widget::InitParams params = CreateParams(
Widget::InitParams::CLIENT_OWNS_WIDGET, Widget::InitParams::TYPE_WINDOW);
params.delegate = child_widget_delegate.get();
params.parent = widget->GetNativeView();
child_widget->Init(std::move(params));
// Sequence: show -> hide -> show -> destroy.
testing::InSequence seq;
EXPECT_CALL(observer,
OnWidgetWindowModalVisibilityChanged(widget.get(), true));
EXPECT_CALL(observer,
OnWidgetWindowModalVisibilityChanged(widget.get(), false));
EXPECT_CALL(observer,
OnWidgetWindowModalVisibilityChanged(widget.get(), true));
EXPECT_CALL(observer,
OnWidgetWindowModalVisibilityChanged(widget.get(), false));
WidgetVisibleWaiter waiter(child_widget.get());
child_widget->Show();
waiter.Wait();
child_widget->Hide();
waiter.WaitUntilInvisible();
child_widget->Show();
waiter.Wait();
// Destroy the child widget, the parent should be notified about child modal
// visibility change.
child_widget.reset();
// No need to wait for visibility change because the widget is already
// destroyed.
widget->RemoveObserver(&observer);
}
} // namespace views::test
|