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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <algorithm>
#include <set>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/hit_test.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/events/event_processor.h"
#include "ui/events/event_utils.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/views/bubble/bubble_delegate.h"
#include "ui/views/controls/textfield/textfield.h"
#include "ui/views/test/test_views_delegate.h"
#include "ui/views/test/test_widget_observer.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/views_delegate.h"
#include "ui/views/widget/native_widget_delegate.h"
#include "ui/views/widget/root_view.h"
#include "ui/views/widget/widget_deletion_observer.h"
#include "ui/views/window/dialog_delegate.h"
#include "ui/views/window/native_frame_view.h"
#if defined(OS_WIN)
#include "ui/views/win/hwnd_util.h"
#endif
namespace views {
namespace test {
namespace {
// 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;
}
} // namespace
// A view that keeps track of the events it receives, optionally consuming them.
class EventCountView : public View {
public:
// Whether to call SetHandled() on events as they are received. For some event
// types, this will allow EventCountView to receives future events in the
// event sequence, such as a drag.
enum HandleMode {
PROPAGATE_EVENTS,
CONSUME_EVENTS
};
EventCountView()
: last_flags_(0),
handle_mode_(PROPAGATE_EVENTS) {}
~EventCountView() override {}
int GetEventCount(ui::EventType type) {
return event_count_[type];
}
void ResetCounts() {
event_count_.clear();
}
int last_flags() const {
return last_flags_;
}
void set_handle_mode(HandleMode handle_mode) {
handle_mode_ = handle_mode;
}
protected:
// Overridden from View:
void OnMouseMoved(const ui::MouseEvent& event) override {
// MouseMove events are not re-dispatched from the RootView.
++event_count_[ui::ET_MOUSE_MOVED];
last_flags_ = 0;
}
// Overridden from ui::EventHandler:
void OnKeyEvent(ui::KeyEvent* event) override { RecordEvent(event); }
void OnMouseEvent(ui::MouseEvent* event) override { RecordEvent(event); }
void OnScrollEvent(ui::ScrollEvent* event) override { RecordEvent(event); }
void OnGestureEvent(ui::GestureEvent* event) override { RecordEvent(event); }
private:
void RecordEvent(ui::Event* event) {
++event_count_[event->type()];
last_flags_ = event->flags();
if (handle_mode_ == CONSUME_EVENTS)
event->SetHandled();
}
std::map<ui::EventType, int> event_count_;
int last_flags_;
HandleMode handle_mode_;
DISALLOW_COPY_AND_ASSIGN(EventCountView);
};
// A view that keeps track of the events it receives, and consumes all scroll
// gesture events and ui::ET_SCROLL events.
class ScrollableEventCountView : public EventCountView {
public:
ScrollableEventCountView() {}
~ScrollableEventCountView() override {}
private:
// Overridden from ui::EventHandler:
void OnGestureEvent(ui::GestureEvent* event) override {
EventCountView::OnGestureEvent(event);
switch (event->type()) {
case ui::ET_GESTURE_SCROLL_BEGIN:
case ui::ET_GESTURE_SCROLL_UPDATE:
case ui::ET_GESTURE_SCROLL_END:
case ui::ET_SCROLL_FLING_START:
event->SetHandled();
break;
default:
break;
}
}
void OnScrollEvent(ui::ScrollEvent* event) override {
EventCountView::OnScrollEvent(event);
if (event->type() == ui::ET_SCROLL)
event->SetHandled();
}
DISALLOW_COPY_AND_ASSIGN(ScrollableEventCountView);
};
// A view that implements GetMinimumSize.
class MinimumSizeFrameView : public NativeFrameView {
public:
explicit MinimumSizeFrameView(Widget* frame): NativeFrameView(frame) {}
~MinimumSizeFrameView() override {}
private:
// Overridden from View:
gfx::Size GetMinimumSize() const override { return gfx::Size(300, 400); }
DISALLOW_COPY_AND_ASSIGN(MinimumSizeFrameView);
};
// An event handler that simply keeps a count of the different types of events
// it receives.
class EventCountHandler : public ui::EventHandler {
public:
EventCountHandler() {}
~EventCountHandler() override {}
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_;
DISALLOW_COPY_AND_ASSIGN(EventCountHandler);
};
// Class that closes the widget (which ends up deleting it immediately) when the
// appropriate event is received.
class CloseWidgetView : public View {
public:
explicit CloseWidgetView(ui::EventType event_type)
: event_type_(event_type) {
}
// ui::EventHandler override:
void OnEvent(ui::Event* event) override {
if (event->type() == event_type_) {
// Go through NativeWidgetPrivate to simulate what happens if the OS
// deletes the NativeWindow out from under us.
GetWidget()->native_widget_private()->CloseNow();
} else {
View::OnEvent(event);
if (!event->IsTouchEvent())
event->SetHandled();
}
}
private:
const ui::EventType event_type_;
DISALLOW_COPY_AND_ASSIGN(CloseWidgetView);
};
ui::WindowShowState GetWidgetShowState(const Widget* widget) {
// Use IsMaximized/IsMinimized/IsFullScreen instead of GetWindowPlacement
// because the former is implemented on all platforms but the latter is not.
return widget->IsFullscreen() ? ui::SHOW_STATE_FULLSCREEN :
widget->IsMaximized() ? ui::SHOW_STATE_MAXIMIZED :
widget->IsMinimized() ? ui::SHOW_STATE_MINIMIZED :
widget->IsActive() ? ui::SHOW_STATE_NORMAL :
ui::SHOW_STATE_INACTIVE;
}
TEST_F(WidgetTest, WidgetInitParams) {
// Widgets are not transparent by default.
Widget::InitParams init1;
EXPECT_EQ(Widget::InitParams::INFER_OPACITY, init1.opacity);
}
TEST_F(WidgetTest, NativeWindowProperty) {
const char* key = "foo";
int value = 3;
Widget* widget = CreateTopLevelPlatformWidget();
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));
widget->CloseNow();
}
////////////////////////////////////////////////////////////////////////////////
// Widget::GetTopLevelWidget tests.
TEST_F(WidgetTest, GetTopLevelWidget_Native) {
// Create a hierarchy of native widgets.
Widget* toplevel = CreateTopLevelPlatformWidget();
gfx::NativeView parent = toplevel->GetNativeView();
Widget* child = CreateChildPlatformWidget(parent);
EXPECT_EQ(toplevel, toplevel->GetTopLevelWidget());
EXPECT_EQ(toplevel, child->GetTopLevelWidget());
toplevel->CloseNow();
// |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) {
Widget* top1 = CreateTopLevelPlatformWidget();
// CreateInputMethod before activated
top1->GetInputMethod();
top1->Show();
RunPendingMessages();
Widget* top2 = CreateTopLevelPlatformWidget();
top2->Show();
RunPendingMessages();
top1->Activate();
RunPendingMessages();
// Create InputMethod after deactivated.
top2->GetInputMethod();
top2->Activate();
RunPendingMessages();
top1->Activate();
RunPendingMessages();
top1->CloseNow();
top2->CloseNow();
}
// Tests visibility of child widgets.
TEST_F(WidgetTest, Visibility) {
Widget* toplevel = CreateTopLevelPlatformWidget();
gfx::NativeView parent = toplevel->GetNativeView();
Widget* child = CreateChildPlatformWidget(parent);
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());
toplevel->CloseNow();
// |child| should be automatically destroyed with |toplevel|.
}
// Test that child widgets are positioned relative to their parent.
TEST_F(WidgetTest, ChildBoundsRelativeToParent) {
Widget* toplevel = CreateTopLevelPlatformWidget();
Widget* child = CreateChildPlatformWidget(toplevel->GetNativeView());
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());
toplevel->CloseNow();
}
// Test z-order of child widgets relative to their parent.
TEST_F(WidgetTest, ChildStackedRelativeToParent) {
Widget* parent = CreateTopLevelPlatformWidget();
Widget* child = CreateChildPlatformWidget(parent->GetNativeView());
parent->SetBounds(gfx::Rect(160, 100, 320, 200));
child->SetBounds(gfx::Rect(50, 50, 30, 20));
// Child shown first. Initially not visible, but on top of parent when shown.
// Use ShowInactive whenever showing the child, otherwise the usual activation
// logic will just put it on top anyway. Here, we want to ensure it is on top
// of its parent regardless.
child->ShowInactive();
EXPECT_FALSE(child->IsVisible());
parent->Show();
EXPECT_TRUE(child->IsVisible());
EXPECT_TRUE(IsWindowStackedAbove(child, parent));
EXPECT_FALSE(IsWindowStackedAbove(parent, child)); // Sanity check.
Widget* popover = CreateTopLevelPlatformWidget();
popover->SetBounds(gfx::Rect(150, 90, 340, 240));
popover->Show();
EXPECT_TRUE(IsWindowStackedAbove(popover, child));
EXPECT_TRUE(IsWindowStackedAbove(child, parent));
// Showing the parent again should raise it and its child above the popover.
parent->Show();
EXPECT_TRUE(IsWindowStackedAbove(child, parent));
EXPECT_TRUE(IsWindowStackedAbove(parent, popover));
// Test grandchildren.
Widget* grandchild = CreateChildPlatformWidget(child->GetNativeView());
grandchild->SetBounds(gfx::Rect(5, 5, 15, 10));
grandchild->ShowInactive();
EXPECT_TRUE(IsWindowStackedAbove(grandchild, child));
EXPECT_TRUE(IsWindowStackedAbove(child, parent));
EXPECT_TRUE(IsWindowStackedAbove(parent, popover));
popover->Show();
EXPECT_TRUE(IsWindowStackedAbove(popover, grandchild));
EXPECT_TRUE(IsWindowStackedAbove(grandchild, child));
parent->Show();
EXPECT_TRUE(IsWindowStackedAbove(grandchild, child));
EXPECT_TRUE(IsWindowStackedAbove(child, popover));
// Test hiding and reshowing.
parent->Hide();
EXPECT_FALSE(grandchild->IsVisible());
parent->Show();
EXPECT_TRUE(IsWindowStackedAbove(grandchild, child));
EXPECT_TRUE(IsWindowStackedAbove(child, parent));
EXPECT_TRUE(IsWindowStackedAbove(parent, popover));
grandchild->Hide();
EXPECT_FALSE(grandchild->IsVisible());
grandchild->ShowInactive();
EXPECT_TRUE(IsWindowStackedAbove(grandchild, child));
EXPECT_TRUE(IsWindowStackedAbove(child, parent));
EXPECT_TRUE(IsWindowStackedAbove(parent, popover));
popover->CloseNow();
parent->CloseNow();
}
////////////////////////////////////////////////////////////////////////////////
// Widget ownership tests.
//
// Tests various permutations of Widget ownership specified in the
// InitParams::Ownership param.
// A WidgetTest that supplies a toplevel widget for NativeWidget to parent to.
class WidgetOwnershipTest : public WidgetTest {
public:
WidgetOwnershipTest() {}
~WidgetOwnershipTest() override {}
void SetUp() override {
WidgetTest::SetUp();
desktop_widget_ = CreateTopLevelPlatformWidget();
}
void TearDown() override {
desktop_widget_->CloseNow();
WidgetTest::TearDown();
}
private:
Widget* desktop_widget_;
DISALLOW_COPY_AND_ASSIGN(WidgetOwnershipTest);
};
// A bag of state to monitor destructions.
struct OwnershipTestState {
OwnershipTestState() : widget_deleted(false), native_widget_deleted(false) {}
bool widget_deleted;
bool native_widget_deleted;
};
// A platform NativeWidget subclass that updates a bag of state when it is
// destroyed.
class OwnershipTestNativeWidget : public PlatformNativeWidget {
public:
OwnershipTestNativeWidget(internal::NativeWidgetDelegate* delegate,
OwnershipTestState* state)
: PlatformNativeWidget(delegate),
state_(state) {
}
~OwnershipTestNativeWidget() override {
state_->native_widget_deleted = true;
}
private:
OwnershipTestState* state_;
DISALLOW_COPY_AND_ASSIGN(OwnershipTestNativeWidget);
};
// A views NativeWidget subclass that updates a bag of state when it is
// destroyed.
class OwnershipTestNativeWidgetAura : public NativeWidgetCapture {
public:
OwnershipTestNativeWidgetAura(internal::NativeWidgetDelegate* delegate,
OwnershipTestState* state)
: NativeWidgetCapture(delegate),
state_(state) {
}
~OwnershipTestNativeWidgetAura() override {
state_->native_widget_deleted = true;
}
private:
OwnershipTestState* state_;
DISALLOW_COPY_AND_ASSIGN(OwnershipTestNativeWidgetAura);
};
// 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() override { state_->widget_deleted = true; }
private:
OwnershipTestState* state_;
DISALLOW_COPY_AND_ASSIGN(OwnershipTestWidget);
};
// Widget owns its NativeWidget, part 1: NativeWidget is a platform-native
// widget.
TEST_F(WidgetOwnershipTest, Ownership_WidgetOwnsPlatformNativeWidget) {
OwnershipTestState state;
scoped_ptr<Widget> widget(new OwnershipTestWidget(&state));
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.native_widget =
new OwnershipTestNativeWidgetAura(widget.get(), &state);
params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
widget->Init(params);
// Now delete the Widget, which should delete the NativeWidget.
widget.reset();
EXPECT_TRUE(state.widget_deleted);
EXPECT_TRUE(state.native_widget_deleted);
// TODO(beng): write test for this ownership scenario and the NativeWidget
// being deleted out from under the Widget.
}
// Widget owns its NativeWidget, part 2: NativeWidget is a NativeWidget.
TEST_F(WidgetOwnershipTest, Ownership_WidgetOwnsViewsNativeWidget) {
OwnershipTestState state;
scoped_ptr<Widget> widget(new OwnershipTestWidget(&state));
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.native_widget =
new OwnershipTestNativeWidgetAura(widget.get(), &state);
params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
widget->Init(params);
// Now delete the Widget, which should delete the NativeWidget.
widget.reset();
EXPECT_TRUE(state.widget_deleted);
EXPECT_TRUE(state.native_widget_deleted);
// TODO(beng): write test for this ownership scenario and the NativeWidget
// being deleted out from under the Widget.
}
// Widget owns its NativeWidget, part 3: NativeWidget is a NativeWidget,
// destroy the parent view.
TEST_F(WidgetOwnershipTest,
Ownership_WidgetOwnsViewsNativeWidget_DestroyParentView) {
OwnershipTestState state;
Widget* toplevel = CreateTopLevelPlatformWidget();
scoped_ptr<Widget> widget(new OwnershipTestWidget(&state));
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.native_widget =
new OwnershipTestNativeWidgetAura(widget.get(), &state);
params.parent = toplevel->GetNativeView();
params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
widget->Init(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);
// Now delete it explicitly.
widget.reset();
EXPECT_TRUE(state.widget_deleted);
EXPECT_TRUE(state.native_widget_deleted);
}
// NativeWidget owns its Widget, part 1: NativeWidget is a platform-native
// widget.
TEST_F(WidgetOwnershipTest, Ownership_PlatformNativeWidgetOwnsWidget) {
OwnershipTestState state;
Widget* widget = new OwnershipTestWidget(&state);
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.native_widget =
new OwnershipTestNativeWidgetAura(widget, &state);
widget->Init(params);
// Now destroy the native widget.
widget->CloseNow();
EXPECT_TRUE(state.widget_deleted);
EXPECT_TRUE(state.native_widget_deleted);
}
// NativeWidget owns its Widget, part 2: NativeWidget is a NativeWidget.
TEST_F(WidgetOwnershipTest, Ownership_ViewsNativeWidgetOwnsWidget) {
OwnershipTestState state;
Widget* toplevel = CreateTopLevelPlatformWidget();
Widget* widget = new OwnershipTestWidget(&state);
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.native_widget =
new OwnershipTestNativeWidgetAura(widget, &state);
params.parent = toplevel->GetNativeView();
widget->Init(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.
RunPendingMessages();
EXPECT_TRUE(state.widget_deleted);
EXPECT_TRUE(state.native_widget_deleted);
}
// NativeWidget owns its Widget, part 3: NativeWidget is a platform-native
// widget, destroyed out from under it by the OS.
TEST_F(WidgetOwnershipTest,
Ownership_PlatformNativeWidgetOwnsWidget_NativeDestroy) {
OwnershipTestState state;
Widget* widget = new OwnershipTestWidget(&state);
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.native_widget =
new OwnershipTestNativeWidgetAura(widget, &state);
widget->Init(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);
}
// NativeWidget owns its Widget, part 4: NativeWidget is a NativeWidget,
// destroyed by the view hierarchy that contains it.
TEST_F(WidgetOwnershipTest,
Ownership_ViewsNativeWidgetOwnsWidget_NativeDestroy) {
OwnershipTestState state;
Widget* toplevel = CreateTopLevelPlatformWidget();
Widget* widget = new OwnershipTestWidget(&state);
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.native_widget =
new OwnershipTestNativeWidgetAura(widget, &state);
params.parent = toplevel->GetNativeView();
widget->Init(params);
// Destroy the widget (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.
RunPendingMessages();
EXPECT_TRUE(state.widget_deleted);
EXPECT_TRUE(state.native_widget_deleted);
}
// NativeWidget owns its Widget, part 5: NativeWidget is a NativeWidget,
// we close it directly.
TEST_F(WidgetOwnershipTest,
Ownership_ViewsNativeWidgetOwnsWidget_Close) {
OwnershipTestState state;
Widget* toplevel = CreateTopLevelPlatformWidget();
Widget* widget = new OwnershipTestWidget(&state);
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.native_widget =
new OwnershipTestNativeWidgetAura(widget, &state);
params.parent = toplevel->GetNativeView();
widget->Init(params);
// Destroy the widget.
widget->Close();
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.
RunPendingMessages();
EXPECT_TRUE(state.widget_deleted);
EXPECT_TRUE(state.native_widget_deleted);
}
// Widget owns its NativeWidget and has a WidgetDelegateView as its contents.
TEST_F(WidgetOwnershipTest,
Ownership_WidgetOwnsNativeWidgetWithWithWidgetDelegateView) {
OwnershipTestState state;
WidgetDelegateView* delegate_view = new WidgetDelegateView;
scoped_ptr<Widget> widget(new OwnershipTestWidget(&state));
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.native_widget =
new OwnershipTestNativeWidgetAura(widget.get(), &state);
params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.delegate = delegate_view;
widget->Init(params);
widget->SetContentsView(delegate_view);
// Now delete the Widget. There should be no crash or use-after-free.
widget.reset();
EXPECT_TRUE(state.widget_deleted);
EXPECT_TRUE(state.native_widget_deleted);
}
////////////////////////////////////////////////////////////////////////////////
// Test to verify using various Widget methods doesn't crash when the underlying
// NativeView is destroyed.
//
class WidgetWithDestroyedNativeViewTest : public ViewsTestBase {
public:
WidgetWithDestroyedNativeViewTest() {}
~WidgetWithDestroyedNativeViewTest() override {}
void InvokeWidgetMethods(Widget* widget) {
widget->GetNativeView();
widget->GetNativeWindow();
ui::Accelerator accelerator;
widget->GetAccelerator(0, &accelerator);
widget->GetTopLevelWidget();
widget->GetWindowBoundsInScreen();
widget->GetClientAreaBoundsInScreen();
widget->SetBounds(gfx::Rect(0, 0, 100, 80));
widget->SetSize(gfx::Size(10, 11));
widget->SetBoundsConstrained(gfx::Rect(0, 0, 120, 140));
widget->SetVisibilityChangedAnimationsEnabled(false);
widget->StackAtTop();
widget->IsClosed();
widget->Close();
widget->Hide();
widget->Activate();
widget->Deactivate();
widget->IsActive();
widget->DisableInactiveRendering();
widget->SetAlwaysOnTop(true);
widget->IsAlwaysOnTop();
widget->Maximize();
widget->Minimize();
widget->Restore();
widget->IsMaximized();
widget->IsFullscreen();
widget->SetOpacity(0);
widget->SetUseDragFrame(true);
widget->FlashFrame(true);
widget->IsVisible();
widget->GetThemeProvider();
widget->GetNativeTheme();
widget->GetFocusManager();
widget->GetInputMethod();
widget->SchedulePaintInRect(gfx::Rect(0, 0, 1, 2));
widget->IsMouseEventsEnabled();
widget->SetNativeWindowProperty("xx", widget);
widget->GetNativeWindowProperty("xx");
widget->GetFocusTraversable();
widget->GetLayer();
widget->ReorderNativeViews();
widget->SetCapture(widget->GetRootView());
widget->ReleaseCapture();
widget->HasCapture();
widget->GetWorkAreaBoundsInScreen();
widget->IsTranslucentWindowOpacitySupported();
}
private:
DISALLOW_COPY_AND_ASSIGN(WidgetWithDestroyedNativeViewTest);
};
TEST_F(WidgetWithDestroyedNativeViewTest, Test) {
{
Widget widget;
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
widget.Init(params);
widget.Show();
widget.native_widget_private()->CloseNow();
InvokeWidgetMethods(&widget);
}
#if !defined(OS_CHROMEOS)
{
Widget widget;
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.native_widget = new PlatformDesktopNativeWidget(&widget);
params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
widget.Init(params);
widget.Show();
widget.native_widget_private()->CloseNow();
InvokeWidgetMethods(&widget);
}
#endif
}
////////////////////////////////////////////////////////////////////////////////
// Widget observer tests.
//
class WidgetObserverTest : public WidgetTest, public WidgetObserver {
public:
WidgetObserverTest()
: active_(nullptr),
widget_closed_(nullptr),
widget_activated_(nullptr),
widget_shown_(nullptr),
widget_hidden_(nullptr),
widget_bounds_changed_(nullptr),
widget_to_close_on_hide_(nullptr) {
}
~WidgetObserverTest() override {}
// 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;
widget_closed_ = widget;
}
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;
}
}
void OnWidgetVisibilityChanged(Widget* widget, bool visible) override {
if (visible) {
widget_shown_ = widget;
return;
}
widget_hidden_ = widget;
if (widget_to_close_on_hide_) {
widget_to_close_on_hide_->Close();
widget_to_close_on_hide_ = nullptr;
}
}
void OnWidgetBoundsChanged(Widget* widget,
const gfx::Rect& new_bounds) override {
widget_bounds_changed_ = widget;
}
void reset() {
active_ = nullptr;
widget_closed_ = nullptr;
widget_activated_ = nullptr;
widget_deactivated_ = nullptr;
widget_shown_ = nullptr;
widget_hidden_ = nullptr;
widget_bounds_changed_ = nullptr;
}
Widget* NewWidget() {
Widget* widget = CreateTopLevelNativeWidget();
widget->AddObserver(this);
return widget;
}
const Widget* active() const { return active_; }
const Widget* widget_closed() const { return widget_closed_; }
const Widget* widget_activated() const { return widget_activated_; }
const Widget* widget_deactivated() const { return widget_deactivated_; }
const Widget* widget_shown() const { return widget_shown_; }
const Widget* widget_hidden() const { return widget_hidden_; }
const Widget* widget_bounds_changed() const { return widget_bounds_changed_; }
private:
Widget* active_;
Widget* widget_closed_;
Widget* widget_activated_;
Widget* widget_deactivated_;
Widget* widget_shown_;
Widget* widget_hidden_;
Widget* widget_bounds_changed_;
Widget* widget_to_close_on_hide_;
};
TEST_F(WidgetObserverTest, DISABLED_ActivationChange) {
Widget* toplevel = CreateTopLevelPlatformWidget();
Widget* toplevel1 = NewWidget();
Widget* toplevel2 = NewWidget();
toplevel1->Show();
toplevel2->Show();
reset();
toplevel1->Activate();
RunPendingMessages();
EXPECT_EQ(toplevel1, widget_activated());
toplevel2->Activate();
RunPendingMessages();
EXPECT_EQ(toplevel1, widget_deactivated());
EXPECT_EQ(toplevel2, widget_activated());
EXPECT_EQ(toplevel2, active());
toplevel->CloseNow();
}
TEST_F(WidgetObserverTest, DISABLED_VisibilityChange) {
Widget* toplevel = CreateTopLevelPlatformWidget();
Widget* child1 = NewWidget();
Widget* child2 = NewWidget();
toplevel->Show();
child1->Show();
child2->Show();
reset();
child1->Hide();
EXPECT_EQ(child1, widget_hidden());
child2->Hide();
EXPECT_EQ(child2, widget_hidden());
child1->Show();
EXPECT_EQ(child1, widget_shown());
child2->Show();
EXPECT_EQ(child2, widget_shown());
toplevel->CloseNow();
}
TEST_F(WidgetObserverTest, DestroyBubble) {
Widget* anchor = CreateTopLevelPlatformWidget();
anchor->Show();
BubbleDelegateView* bubble_delegate =
new BubbleDelegateView(anchor->client_view(), BubbleBorder::NONE);
Widget* bubble_widget(BubbleDelegateView::CreateBubble(bubble_delegate));
bubble_widget->Show();
bubble_widget->CloseNow();
anchor->Hide();
anchor->CloseNow();
}
TEST_F(WidgetObserverTest, WidgetBoundsChanged) {
Widget* child1 = NewWidget();
Widget* child2 = NewWidget();
child1->OnNativeWidgetMove();
EXPECT_EQ(child1, widget_bounds_changed());
child2->OnNativeWidgetMove();
EXPECT_EQ(child2, widget_bounds_changed());
child1->OnNativeWidgetSizeChanged(gfx::Size());
EXPECT_EQ(child1, widget_bounds_changed());
child2->OnNativeWidgetSizeChanged(gfx::Size());
EXPECT_EQ(child2, 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.
Widget* widget = new Widget(); // Note: owned by NativeWidget.
widget->AddObserver(this);
EXPECT_FALSE(widget_bounds_changed());
// Init causes a bounds change, even while not showing.
widget->Init(CreateParams(Widget::InitParams::TYPE_WINDOW));
EXPECT_TRUE(widget_bounds_changed());
reset();
// Resizing while hidden, triggers a change.
widget->SetSize(gfx::Size(160, 100));
EXPECT_FALSE(widget->IsVisible());
EXPECT_TRUE(widget_bounds_changed());
reset();
// Setting the same size does nothing.
widget->SetSize(gfx::Size(160, 100));
EXPECT_FALSE(widget_bounds_changed());
reset();
// Showing does nothing to the bounds.
widget->Show();
EXPECT_TRUE(widget->IsVisible());
EXPECT_FALSE(widget_bounds_changed());
reset();
// Resizing while shown.
widget->SetSize(gfx::Size(170, 100));
EXPECT_TRUE(widget_bounds_changed());
reset();
// Resize to the same thing while shown does nothing.
widget->SetSize(gfx::Size(170, 100));
EXPECT_FALSE(widget_bounds_changed());
reset();
// No bounds change when closing.
widget->CloseNow();
EXPECT_FALSE(widget_bounds_changed());
}
// Test correct behavior when widgets close themselves in response to visibility
// changes.
TEST_F(WidgetObserverTest, ClosingOnHiddenParent) {
Widget* parent = NewWidget();
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());
parent->CloseNow();
}
// 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.
Widget* 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());
widget->CloseNow();
// Same tests with a frameless window.
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());
widget->CloseNow();
}
// Before being enabled on Mac, this was #ifdef(false).
// TODO(tapted): Fix this for DesktopNativeWidgets on other platforms.
#if defined(OS_MACOSX)
// Aura needs shell to maximize/fullscreen window.
// NativeWidgetGtk doesn't implement GetRestoredBounds.
TEST_F(WidgetTest, GetRestoredBounds) {
Widget* toplevel = CreateTopLevelPlatformWidget();
EXPECT_EQ(toplevel->GetWindowBoundsInScreen().ToString(),
toplevel->GetRestoredBounds().ToString());
toplevel->Show();
toplevel->Maximize();
RunPendingMessages();
#if defined(OS_MACOSX)
// Current expectation on Mac is to do nothing on Maximize.
EXPECT_EQ(toplevel->GetWindowBoundsInScreen().ToString(),
toplevel->GetRestoredBounds().ToString());
#else
EXPECT_NE(toplevel->GetWindowBoundsInScreen().ToString(),
toplevel->GetRestoredBounds().ToString());
#endif
EXPECT_GT(toplevel->GetRestoredBounds().width(), 0);
EXPECT_GT(toplevel->GetRestoredBounds().height(), 0);
toplevel->Restore();
RunPendingMessages();
EXPECT_EQ(toplevel->GetWindowBoundsInScreen().ToString(),
toplevel->GetRestoredBounds().ToString());
toplevel->SetFullscreen(true);
RunPendingMessages();
EXPECT_NE(toplevel->GetWindowBoundsInScreen().ToString(),
toplevel->GetRestoredBounds().ToString());
EXPECT_GT(toplevel->GetRestoredBounds().width(), 0);
EXPECT_GT(toplevel->GetRestoredBounds().height(), 0);
}
#endif
// ExitFullscreenRestoreState doesn't use DesktopAura widgets. On Mac, there are
// currently only Desktop widgets and fullscreen changes need to occur in an
// interactive test to avoid flakes. Maximize on mac is also (intentionally) a
// no-op.
#if defined(OS_MACOSX) && !defined(USE_AURA)
#define MAYBE_ExitFullscreenRestoreState DISABLED_ExitFullscreenRestoreState
#else
#define MAYBE_ExitFullscreenRestoreState ExitFullscreenRestoreState
#endif
// Test that window state is not changed after getting out of full screen.
TEST_F(WidgetTest, MAYBE_ExitFullscreenRestoreState) {
Widget* toplevel = CreateTopLevelPlatformWidget();
toplevel->Show();
RunPendingMessages();
// This should be a normal state window.
EXPECT_EQ(ui::SHOW_STATE_NORMAL, GetWidgetShowState(toplevel));
toplevel->SetFullscreen(true);
EXPECT_EQ(ui::SHOW_STATE_FULLSCREEN, GetWidgetShowState(toplevel));
toplevel->SetFullscreen(false);
EXPECT_NE(ui::SHOW_STATE_FULLSCREEN, GetWidgetShowState(toplevel));
// And it should still be in normal state after getting out of full screen.
EXPECT_EQ(ui::SHOW_STATE_NORMAL, GetWidgetShowState(toplevel));
// Now, make it maximized.
toplevel->Maximize();
EXPECT_EQ(ui::SHOW_STATE_MAXIMIZED, GetWidgetShowState(toplevel));
toplevel->SetFullscreen(true);
EXPECT_EQ(ui::SHOW_STATE_FULLSCREEN, GetWidgetShowState(toplevel));
toplevel->SetFullscreen(false);
EXPECT_NE(ui::SHOW_STATE_FULLSCREEN, GetWidgetShowState(toplevel));
// And it stays maximized after getting out of full screen.
EXPECT_EQ(ui::SHOW_STATE_MAXIMIZED, GetWidgetShowState(toplevel));
// Clean up.
toplevel->Close();
RunPendingMessages();
}
// 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) {
Widget* toplevel = CreateTopLevelPlatformWidget();
View* container = toplevel->client_view();
Textfield* textfield = new Textfield();
textfield->SetText(base::ASCIIToUTF16("some text"));
container->AddChildView(textfield);
toplevel->Show();
textfield->RequestFocus();
// The press gets handled. The release doesn't have an effect.
ui::KeyEvent backspace_p(ui::ET_KEY_PRESSED, ui::VKEY_DELETE, ui::EF_NONE);
toplevel->OnKeyEvent(&backspace_p);
EXPECT_TRUE(backspace_p.stopped_propagation());
ui::KeyEvent backspace_r(ui::ET_KEY_RELEASED, ui::VKEY_DELETE, ui::EF_NONE);
toplevel->OnKeyEvent(&backspace_r);
EXPECT_FALSE(backspace_r.handled());
toplevel->Close();
}
// Verifies bubbles result in a focus lost when shown.
// TODO(msw): this tests relies on focus, it needs to be in
// interactive_ui_tests.
TEST_F(WidgetTest, DISABLED_FocusChangesOnBubble) {
// Create a widget, show and activate it and focus the contents view.
View* contents_view = new View;
contents_view->SetFocusable(true);
Widget widget;
Widget::InitParams init_params =
CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS);
init_params.bounds = gfx::Rect(0, 0, 200, 200);
init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
#if !defined(OS_CHROMEOS)
init_params.native_widget = new PlatformDesktopNativeWidget(&widget);
#endif
widget.Init(init_params);
widget.SetContentsView(contents_view);
widget.Show();
widget.Activate();
contents_view->RequestFocus();
EXPECT_TRUE(contents_view->HasFocus());
// Show a bubble.
BubbleDelegateView* bubble_delegate_view =
new BubbleDelegateView(contents_view, BubbleBorder::TOP_LEFT);
bubble_delegate_view->SetFocusable(true);
BubbleDelegateView::CreateBubble(bubble_delegate_view)->Show();
bubble_delegate_view->RequestFocus();
// |contents_view_| should no longer have focus.
EXPECT_FALSE(contents_view->HasFocus());
EXPECT_TRUE(bubble_delegate_view->HasFocus());
bubble_delegate_view->GetWidget()->CloseNow();
// Closing the bubble should result in focus going back to the contents view.
EXPECT_TRUE(contents_view->HasFocus());
}
class TestBubbleDelegateView : public BubbleDelegateView {
public:
TestBubbleDelegateView(View* anchor)
: BubbleDelegateView(anchor, BubbleBorder::NONE),
reset_controls_called_(false) {}
~TestBubbleDelegateView() override {}
bool ShouldShowCloseButton() const override {
reset_controls_called_ = true;
return true;
}
mutable bool reset_controls_called_;
};
TEST_F(WidgetTest, BubbleControlsResetOnInit) {
Widget* anchor = CreateTopLevelPlatformWidget();
anchor->Show();
TestBubbleDelegateView* bubble_delegate =
new TestBubbleDelegateView(anchor->client_view());
Widget* bubble_widget(BubbleDelegateView::CreateBubble(bubble_delegate));
EXPECT_TRUE(bubble_delegate->reset_controls_called_);
bubble_widget->Show();
bubble_widget->CloseNow();
anchor->Hide();
anchor->CloseNow();
}
#if defined(OS_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(WidgetTest, TestViewWidthAfterMinimizingWidget) {
// Create a widget.
Widget widget;
Widget::InitParams init_params =
CreateParams(Widget::InitParams::TYPE_WINDOW);
init_params.show_state = ui::SHOW_STATE_NORMAL;
gfx::Rect initial_bounds(0, 0, 300, 400);
init_params.bounds = initial_bounds;
init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
init_params.native_widget = new PlatformDesktopNativeWidget(&widget);
widget.Init(init_params);
NonClientView* non_client_view = widget.non_client_view();
NonClientFrameView* frame_view = new MinimumSizeFrameView(&widget);
non_client_view->SetFrameView(frame_view);
// Setting the frame view doesn't do a layout, so force one.
non_client_view->Layout();
widget.Show();
EXPECT_NE(0, frame_view->width());
widget.Minimize();
EXPECT_EQ(0, frame_view->width());
}
#endif
// Desktop native widget Aura tests are for non Chrome OS platforms.
#if !defined(OS_CHROMEOS)
// This class validates whether paints are received for a visible Widget.
// To achieve this it overrides the Show and Close methods on the Widget class
// and sets state whether subsequent paints are expected.
class DesktopAuraTestValidPaintWidget : public views::Widget {
public:
DesktopAuraTestValidPaintWidget()
: received_paint_(false),
expect_paint_(true),
received_paint_while_hidden_(false) {}
~DesktopAuraTestValidPaintWidget() override {}
void InitForTest(Widget::InitParams create_params);
void Show() override {
expect_paint_ = true;
views::Widget::Show();
}
void Close() override {
expect_paint_ = false;
views::Widget::Close();
}
void Hide() {
expect_paint_ = false;
views::Widget::Hide();
}
void OnNativeWidgetPaint(gfx::Canvas* canvas) override {
received_paint_ = true;
EXPECT_TRUE(expect_paint_);
if (!expect_paint_)
received_paint_while_hidden_ = true;
views::Widget::OnNativeWidgetPaint(canvas);
}
bool ReadReceivedPaintAndReset() {
bool result = received_paint_;
received_paint_ = false;
return result;
}
bool received_paint_while_hidden() const {
return received_paint_while_hidden_;
}
private:
bool received_paint_;
bool expect_paint_;
bool received_paint_while_hidden_;
DISALLOW_COPY_AND_ASSIGN(DesktopAuraTestValidPaintWidget);
};
void DesktopAuraTestValidPaintWidget::InitForTest(InitParams init_params) {
init_params.bounds = gfx::Rect(0, 0, 200, 200);
init_params.ownership = InitParams::WIDGET_OWNS_NATIVE_WIDGET;
init_params.native_widget = new PlatformDesktopNativeWidget(this);
Init(init_params);
View* contents_view = new View;
contents_view->SetFocusable(true);
SetContentsView(contents_view);
Show();
Activate();
}
TEST_F(WidgetTest, DesktopNativeWidgetNoPaintAfterCloseTest) {
DesktopAuraTestValidPaintWidget widget;
widget.InitForTest(CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS));
RunPendingMessages();
EXPECT_TRUE(widget.ReadReceivedPaintAndReset());
widget.SchedulePaintInRect(widget.GetRestoredBounds());
widget.Close();
RunPendingMessages();
EXPECT_FALSE(widget.ReadReceivedPaintAndReset());
EXPECT_FALSE(widget.received_paint_while_hidden());
}
TEST_F(WidgetTest, DesktopNativeWidgetNoPaintAfterHideTest) {
DesktopAuraTestValidPaintWidget widget;
widget.InitForTest(CreateParams(Widget::InitParams::TYPE_WINDOW_FRAMELESS));
RunPendingMessages();
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 visiblity state is set to visible if
// the underlying widget is hidden and then shown.
TEST_F(WidgetTest, TestWindowVisibilityAfterHide) {
// Create a widget.
Widget widget;
Widget::InitParams init_params =
CreateParams(Widget::InitParams::TYPE_WINDOW);
init_params.show_state = ui::SHOW_STATE_NORMAL;
gfx::Rect initial_bounds(0, 0, 300, 400);
init_params.bounds = initial_bounds;
init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
init_params.native_widget = new PlatformDesktopNativeWidget(&widget);
widget.Init(init_params);
NonClientView* non_client_view = widget.non_client_view();
NonClientFrameView* frame_view = new MinimumSizeFrameView(&widget);
non_client_view->SetFrameView(frame_view);
widget.Show();
EXPECT_TRUE(IsNativeWindowVisible(widget.GetNativeWindow()));
widget.Hide();
EXPECT_FALSE(IsNativeWindowVisible(widget.GetNativeWindow()));
widget.Show();
EXPECT_TRUE(IsNativeWindowVisible(widget.GetNativeWindow()));
}
// The following code verifies we can correctly destroy a Widget from a mouse
// enter/exit. We could test move/drag/enter/exit but in general we don't run
// nested message loops from such events, nor has the code ever really dealt
// with this situation.
// Generates two moves (first generates enter, second real move), a press, drag
// and release stopping at |last_event_type|.
void GenerateMouseEvents(Widget* widget, ui::EventType last_event_type) {
const gfx::Rect screen_bounds(widget->GetWindowBoundsInScreen());
ui::MouseEvent move_event(ui::ET_MOUSE_MOVED, screen_bounds.CenterPoint(),
screen_bounds.CenterPoint(), 0, 0);
ui::EventProcessor* dispatcher = WidgetTest::GetEventProcessor(widget);
ui::EventDispatchDetails details = dispatcher->OnEventFromSource(&move_event);
if (last_event_type == ui::ET_MOUSE_ENTERED || details.dispatcher_destroyed)
return;
details = dispatcher->OnEventFromSource(&move_event);
if (last_event_type == ui::ET_MOUSE_MOVED || details.dispatcher_destroyed)
return;
ui::MouseEvent press_event(ui::ET_MOUSE_PRESSED, screen_bounds.CenterPoint(),
screen_bounds.CenterPoint(), 0, 0);
details = dispatcher->OnEventFromSource(&press_event);
if (last_event_type == ui::ET_MOUSE_PRESSED || details.dispatcher_destroyed)
return;
gfx::Point end_point(screen_bounds.CenterPoint());
end_point.Offset(1, 1);
ui::MouseEvent drag_event(ui::ET_MOUSE_DRAGGED, end_point, end_point, 0, 0);
details = dispatcher->OnEventFromSource(&drag_event);
if (last_event_type == ui::ET_MOUSE_DRAGGED || details.dispatcher_destroyed)
return;
ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED, end_point, end_point, 0,
0);
details = dispatcher->OnEventFromSource(&release_event);
if (details.dispatcher_destroyed)
return;
}
// Creates a widget and invokes GenerateMouseEvents() with |last_event_type|.
void RunCloseWidgetDuringDispatchTest(WidgetTest* test,
ui::EventType last_event_type) {
// |widget| is deleted by CloseWidgetView.
Widget* widget = new Widget;
Widget::InitParams params =
test->CreateParams(Widget::InitParams::TYPE_POPUP);
params.native_widget = new PlatformDesktopNativeWidget(widget);
params.bounds = gfx::Rect(0, 0, 50, 100);
widget->Init(params);
widget->SetContentsView(new CloseWidgetView(last_event_type));
widget->Show();
GenerateMouseEvents(widget, last_event_type);
}
// Verifies deleting the widget from a mouse pressed event doesn't crash.
TEST_F(WidgetTest, CloseWidgetDuringMousePress) {
RunCloseWidgetDuringDispatchTest(this, ui::ET_MOUSE_PRESSED);
}
// Verifies deleting the widget from a mouse released event doesn't crash.
TEST_F(WidgetTest, CloseWidgetDuringMouseReleased) {
RunCloseWidgetDuringDispatchTest(this, ui::ET_MOUSE_RELEASED);
}
#endif // !defined(OS_CHROMEOS)
// Tests that wheel events generated from scroll events are targetted 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);
Widget* widget = CreateTopLevelPlatformWidget();
widget->GetRootView()->AddChildView(cursor_view);
// Generate a scroll event on the cursor view.
ui::ScrollEvent scroll(ui::ET_SCROLL,
gfx::Point(65, 5),
ui::EventTimeForNow(),
0,
0, 20,
0, 20,
2);
widget->OnScrollEvent(&scroll);
EXPECT_EQ(1, cursor_view->GetEventCount(ui::ET_SCROLL));
EXPECT_EQ(1, cursor_view->GetEventCount(ui::ET_MOUSEWHEEL));
cursor_view->ResetCounts();
ui::ScrollEvent scroll2(ui::ET_SCROLL,
gfx::Point(5, 5),
ui::EventTimeForNow(),
0,
0, 20,
0, 20,
2);
widget->OnScrollEvent(&scroll2);
EXPECT_EQ(0, cursor_view->GetEventCount(ui::ET_SCROLL));
EXPECT_EQ(0, cursor_view->GetEventCount(ui::ET_MOUSEWHEEL));
widget->CloseNow();
}
// 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);
Widget* widget = CreateTopLevelPlatformWidget();
widget->GetRootView()->AddChildView(noscroll_view);
widget->GetRootView()->AddChildView(scroll_view);
{
ui::GestureEvent begin(
5,
5,
0,
base::TimeDelta(),
ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_BEGIN));
widget->OnGestureEvent(&begin);
ui::GestureEvent update(
25,
15,
0,
base::TimeDelta(),
ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_UPDATE, 20, 10));
widget->OnGestureEvent(&update);
ui::GestureEvent end(25,
15,
0,
base::TimeDelta(),
ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_END));
widget->OnGestureEvent(&end);
EXPECT_EQ(1, noscroll_view->GetEventCount(ui::ET_GESTURE_SCROLL_BEGIN));
EXPECT_EQ(0, noscroll_view->GetEventCount(ui::ET_GESTURE_SCROLL_UPDATE));
EXPECT_EQ(0, noscroll_view->GetEventCount(ui::ET_GESTURE_SCROLL_END));
}
{
ui::GestureEvent begin(
65,
5,
0,
base::TimeDelta(),
ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_BEGIN));
widget->OnGestureEvent(&begin);
ui::GestureEvent update(
85,
15,
0,
base::TimeDelta(),
ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_UPDATE, 20, 10));
widget->OnGestureEvent(&update);
ui::GestureEvent end(85,
15,
0,
base::TimeDelta(),
ui::GestureEventDetails(ui::ET_GESTURE_SCROLL_END));
widget->OnGestureEvent(&end);
EXPECT_EQ(1, scroll_view->GetEventCount(ui::ET_GESTURE_SCROLL_BEGIN));
EXPECT_EQ(1, scroll_view->GetEventCount(ui::ET_GESTURE_SCROLL_UPDATE));
EXPECT_EQ(1, scroll_view->GetEventCount(ui::ET_GESTURE_SCROLL_END));
}
widget->CloseNow();
}
// 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) {
Widget* widget = CreateTopLevelNativeWidget();
View* root_view = widget->GetRootView();
scoped_ptr<EventCountView> view(new EventCountView());
view->set_owned_by_client();
view->SetBounds(0, 0, 20, 20);
root_view->AddChildView(view.get());
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::ET_SCROLL event. The event remains unhandled and should
// bubble up the views hierarchy to be re-dispatched on the root view.
ui::ScrollEvent scroll(ui::ET_SCROLL,
gfx::Point(5, 5),
ui::EventTimeForNow(),
0,
0, 20,
0, 20,
2);
widget->OnScrollEvent(&scroll);
EXPECT_EQ(2, h1.GetEventCount(ui::ET_SCROLL));
EXPECT_EQ(1, view->GetEventCount(ui::ET_SCROLL));
EXPECT_EQ(2, h2.GetEventCount(ui::ET_SCROLL));
// Unhandled scroll events are turned into wheel events and re-dispatched.
EXPECT_EQ(1, h1.GetEventCount(ui::ET_MOUSEWHEEL));
EXPECT_EQ(1, view->GetEventCount(ui::ET_MOUSEWHEEL));
EXPECT_EQ(1, h2.GetEventCount(ui::ET_MOUSEWHEEL));
h1.ResetCounts();
view->ResetCounts();
h2.ResetCounts();
// Dispatch a ui::ET_SCROLL_FLING_START event. The event remains unhandled and
// should bubble up the views hierarchy to be re-dispatched on the root view.
ui::ScrollEvent fling(ui::ET_SCROLL_FLING_START,
gfx::Point(5, 5),
ui::EventTimeForNow(),
0,
0, 20,
0, 20,
2);
widget->OnScrollEvent(&fling);
EXPECT_EQ(2, h1.GetEventCount(ui::ET_SCROLL_FLING_START));
EXPECT_EQ(1, view->GetEventCount(ui::ET_SCROLL_FLING_START));
EXPECT_EQ(2, h2.GetEventCount(ui::ET_SCROLL_FLING_START));
// Unhandled scroll events which are not of type ui::ET_SCROLL should not
// be turned into wheel events and re-dispatched.
EXPECT_EQ(0, h1.GetEventCount(ui::ET_MOUSEWHEEL));
EXPECT_EQ(0, view->GetEventCount(ui::ET_MOUSEWHEEL));
EXPECT_EQ(0, h2.GetEventCount(ui::ET_MOUSEWHEEL));
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::CONSUME_EVENTS);
// Dispatch a ui::ET_GESTURE_TAP_DOWN and a ui::ET_GESTURE_TAP_CANCEL event.
// The events are handled at the target phase and should not reach the
// post-target handler.
ui::GestureEvent tap_down(5,
5,
0,
ui::EventTimeForNow(),
ui::GestureEventDetails(ui::ET_GESTURE_TAP_DOWN));
widget->OnGestureEvent(&tap_down);
EXPECT_EQ(1, h1.GetEventCount(ui::ET_GESTURE_TAP_DOWN));
EXPECT_EQ(1, view->GetEventCount(ui::ET_GESTURE_TAP_DOWN));
EXPECT_EQ(0, h2.GetEventCount(ui::ET_GESTURE_TAP_DOWN));
ui::GestureEvent tap_cancel(
5,
5,
0,
ui::EventTimeForNow(),
ui::GestureEventDetails(ui::ET_GESTURE_TAP_CANCEL));
widget->OnGestureEvent(&tap_cancel);
EXPECT_EQ(1, h1.GetEventCount(ui::ET_GESTURE_TAP_CANCEL));
EXPECT_EQ(1, view->GetEventCount(ui::ET_GESTURE_TAP_CANCEL));
EXPECT_EQ(0, h2.GetEventCount(ui::ET_GESTURE_TAP_CANCEL));
h1.ResetCounts();
view->ResetCounts();
h2.ResetCounts();
// Dispatch a ui::ET_SCROLL event. The event is handled at the target phase
// and should not reach the post-target handler.
ui::ScrollEvent consumed_scroll(ui::ET_SCROLL,
gfx::Point(5, 5),
ui::EventTimeForNow(),
0,
0, 20,
0, 20,
2);
widget->OnScrollEvent(&consumed_scroll);
EXPECT_EQ(1, h1.GetEventCount(ui::ET_SCROLL));
EXPECT_EQ(1, view->GetEventCount(ui::ET_SCROLL));
EXPECT_EQ(0, h2.GetEventCount(ui::ET_SCROLL));
// Handled scroll events are not turned into wheel events and re-dispatched.
EXPECT_EQ(0, h1.GetEventCount(ui::ET_MOUSEWHEEL));
EXPECT_EQ(0, view->GetEventCount(ui::ET_MOUSEWHEEL));
EXPECT_EQ(0, h2.GetEventCount(ui::ET_MOUSEWHEEL));
widget->CloseNow();
}
TEST_F(WidgetTest, SynthesizeMouseMoveEvent) {
Widget* widget = CreateTopLevelNativeWidget();
View* root_view = widget->GetRootView();
EventCountView* v1 = new EventCountView();
v1->SetBounds(0, 0, 10, 10);
root_view->AddChildView(v1);
EventCountView* v2 = new EventCountView();
v2->SetBounds(0, 10, 10, 10);
root_view->AddChildView(v2);
gfx::Point cursor_location(5, 5);
ui::MouseEvent move(ui::ET_MOUSE_MOVED, cursor_location, cursor_location,
ui::EF_NONE, ui::EF_NONE);
widget->OnMouseEvent(&move);
EXPECT_EQ(1, v1->GetEventCount(ui::ET_MOUSE_ENTERED));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_MOUSE_ENTERED));
delete v1;
v2->SetBounds(0, 0, 10, 10);
EXPECT_EQ(0, v2->GetEventCount(ui::ET_MOUSE_ENTERED));
widget->SynthesizeMouseMoveEvent();
EXPECT_EQ(1, v2->GetEventCount(ui::ET_MOUSE_ENTERED));
}
namespace {
// ui::EventHandler which handles all mouse press events.
class MousePressEventConsumer : public ui::EventHandler {
public:
explicit MousePressEventConsumer() {
}
~MousePressEventConsumer() override {}
private:
// ui::EventHandler:
void OnMouseEvent(ui::MouseEvent* event) override {
if (event->type() == ui::ET_MOUSE_PRESSED)
event->SetHandled();
}
DISALLOW_COPY_AND_ASSIGN(MousePressEventConsumer);
};
} // namespace
// 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 = new EventCountView();
event_count_view->SetBounds(0, 0, 300, 300);
widget->GetRootView()->AddChildView(event_count_view);
MousePressEventConsumer consumer;
event_count_view->AddPostTargetHandler(&consumer);
ui::test::EventGenerator generator(GetContext(), widget->GetNativeWindow());
generator.PressTouch();
generator.ClickLeftButton();
EXPECT_EQ(1, event_count_view->GetEventCount(ui::ET_MOUSE_PRESSED));
EXPECT_EQ(1, event_count_view->GetEventCount(ui::ET_MOUSE_RELEASED));
widget->CloseNow();
}
// Used by SingleWindowClosing to count number of times WindowClosing() has
// been invoked.
class ClosingDelegate : public WidgetDelegate {
public:
ClosingDelegate() : count_(0), widget_(NULL) {}
int count() const { return count_; }
void set_widget(views::Widget* widget) { widget_ = widget; }
// WidgetDelegate overrides:
Widget* GetWidget() override { return widget_; }
const Widget* GetWidget() const override { return widget_; }
void WindowClosing() override { count_++; }
private:
int count_;
views::Widget* widget_;
DISALLOW_COPY_AND_ASSIGN(ClosingDelegate);
};
// Verifies WindowClosing() is invoked correctly on the delegate when a Widget
// is closed.
TEST_F(WidgetTest, SingleWindowClosing) {
scoped_ptr<ClosingDelegate> delegate(new ClosingDelegate());
Widget* widget = new Widget(); // Destroyed by CloseNow() below.
Widget::InitParams init_params =
CreateParams(Widget::InitParams::TYPE_WINDOW);
init_params.bounds = gfx::Rect(0, 0, 200, 200);
init_params.delegate = delegate.get();
#if !defined(OS_CHROMEOS)
init_params.native_widget = new PlatformDesktopNativeWidget(widget);
#endif
widget->Init(init_params);
EXPECT_EQ(0, delegate->count());
widget->CloseNow();
EXPECT_EQ(1, delegate->count());
}
class WidgetWindowTitleTest : public WidgetTest {
protected:
void RunTest(bool desktop_native_widget) {
Widget* widget = new Widget(); // Destroyed by CloseNow() below.
Widget::InitParams init_params =
CreateParams(Widget::InitParams::TYPE_WINDOW);
widget->Init(init_params);
#if !defined(OS_CHROMEOS)
if (desktop_native_widget)
init_params.native_widget = new PlatformDesktopNativeWidget(widget);
#else
DCHECK(!desktop_native_widget)
<< "DesktopNativeWidget does not exist on non-Aura or on ChromeOS.";
#endif
internal::NativeWidgetPrivate* native_widget =
widget->native_widget_private();
base::string16 empty;
base::string16 s1(base::UTF8ToUTF16("Title1"));
base::string16 s2(base::UTF8ToUTF16("Title2"));
base::string16 s3(base::UTF8ToUTF16("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));
widget->CloseNow();
}
};
TEST_F(WidgetWindowTitleTest, SetWindowTitleChanged_NativeWidget) {
// Use the default NativeWidget.
bool desktop_native_widget = false;
RunTest(desktop_native_widget);
}
// DesktopNativeWidget does not exist on non-Aura or on ChromeOS.
#if !defined(OS_CHROMEOS)
TEST_F(WidgetWindowTitleTest, SetWindowTitleChanged_DesktopNativeWidget) {
// Override to use a DesktopNativeWidget.
bool desktop_native_widget = true;
RunTest(desktop_native_widget);
}
#endif // !OS_CHROMEOS
TEST_F(WidgetTest, WidgetDeleted_InOnMousePressed) {
Widget* widget = new Widget;
Widget::InitParams params =
CreateParams(views::Widget::InitParams::TYPE_POPUP);
widget->Init(params);
widget->SetContentsView(new CloseWidgetView(ui::ET_MOUSE_PRESSED));
widget->SetSize(gfx::Size(100, 100));
widget->Show();
ui::test::EventGenerator generator(GetContext(), widget->GetNativeWindow());
WidgetDeletionObserver deletion_observer(widget);
generator.ClickLeftButton();
EXPECT_FALSE(deletion_observer.IsWidgetAlive());
// Yay we did not crash!
}
TEST_F(WidgetTest, WidgetDeleted_InDispatchGestureEvent) {
Widget* widget = new Widget;
Widget::InitParams params =
CreateParams(views::Widget::InitParams::TYPE_POPUP);
widget->Init(params);
widget->SetContentsView(new CloseWidgetView(ui::ET_GESTURE_TAP_DOWN));
widget->SetSize(gfx::Size(100, 100));
widget->Show();
ui::test::EventGenerator generator(GetContext(), widget->GetNativeWindow());
WidgetDeletionObserver deletion_observer(widget);
generator.GestureTapAt(widget->GetWindowBoundsInScreen().CenterPoint());
EXPECT_FALSE(deletion_observer.IsWidgetAlive());
// Yay we did not crash!
}
// See description of RunGetNativeThemeFromDestructor() for details.
class GetNativeThemeFromDestructorView : public WidgetDelegateView {
public:
GetNativeThemeFromDestructorView() {}
~GetNativeThemeFromDestructorView() override { VerifyNativeTheme(); }
View* GetContentsView() override { return this; }
private:
void VerifyNativeTheme() {
ASSERT_TRUE(GetNativeTheme() != NULL);
}
DISALLOW_COPY_AND_ASSIGN(GetNativeThemeFromDestructorView);
};
// 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(const Widget::InitParams& in_params,
bool is_first_run) {
bool needs_second_run = false;
// Destroyed by CloseNow() below.
Widget* widget = new Widget;
Widget::InitParams params(in_params);
// Deletes itself when the Widget is destroyed.
params.delegate = new GetNativeThemeFromDestructorView;
#if !defined(OS_CHROMEOS)
if (is_first_run) {
params.native_widget = new PlatformDesktopNativeWidget(widget);
needs_second_run = true;
}
#endif
widget->Init(params);
widget->CloseNow();
return needs_second_run;
}
// See description of RunGetNativeThemeFromDestructor() for details.
TEST_F(WidgetTest, GetNativeThemeFromDestructor) {
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
if (RunGetNativeThemeFromDestructor(params, true))
RunGetNativeThemeFromDestructor(params, false);
}
// Used by HideCloseDestroy. Allows setting a boolean when the widget is
// destroyed.
class CloseDestroysWidget : public Widget {
public:
explicit CloseDestroysWidget(bool* destroyed)
: destroyed_(destroyed) {
}
~CloseDestroysWidget() override {
if (destroyed_) {
*destroyed_ = true;
base::MessageLoop::current()->QuitNow();
}
}
void Detach() { destroyed_ = NULL; }
private:
// If non-null set to true from destructor.
bool* destroyed_;
DISALLOW_COPY_AND_ASSIGN(CloseDestroysWidget);
};
// An observer that registers that an animation has ended.
class AnimationEndObserver : public ui::ImplicitAnimationObserver {
public:
AnimationEndObserver() : animation_completed_(false) {}
~AnimationEndObserver() override {}
bool animation_completed() const { return animation_completed_; }
// ui::ImplicitAnimationObserver:
void OnImplicitAnimationsCompleted() override { animation_completed_ = true; }
private:
bool animation_completed_;
DISALLOW_COPY_AND_ASSIGN(AnimationEndObserver);
};
// An observer that registers the bounds of a widget on destruction.
class WidgetBoundsObserver : public WidgetObserver {
public:
WidgetBoundsObserver() {}
~WidgetBoundsObserver() override {}
gfx::Rect bounds() { return bounds_; }
// WidgetObserver:
void OnWidgetDestroying(Widget* widget) override {
bounds_ = widget->GetWindowBoundsInScreen();
}
private:
gfx::Rect bounds_;
DISALLOW_COPY_AND_ASSIGN(WidgetBoundsObserver);
};
// Verifies Close() results in destroying.
TEST_F(WidgetTest, CloseDestroys) {
bool destroyed = false;
CloseDestroysWidget* widget = new CloseDestroysWidget(&destroyed);
Widget::InitParams params =
CreateParams(views::Widget::InitParams::TYPE_MENU);
params.opacity = Widget::InitParams::OPAQUE_WINDOW;
#if !defined(OS_CHROMEOS)
params.native_widget = new PlatformDesktopNativeWidget(widget);
#endif
widget->Init(params);
widget->Show();
widget->Hide();
widget->Close();
EXPECT_FALSE(destroyed);
// Run the message loop as Close() asynchronously deletes.
base::RunLoop().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) {
scoped_ptr<Widget> widget(new Widget);
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(50, 50, 250, 250);
widget->Init(params);
AnimationEndObserver animation_observer;
WidgetBoundsObserver widget_observer;
gfx::Rect bounds(0, 0, 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->AddObserver(&widget_observer);
widget->Show();
// Animate the bounds change.
widget->SetBounds(bounds);
widget.reset();
EXPECT_FALSE(animation_observer.animation_completed());
}
EXPECT_TRUE(animation_observer.animation_completed());
EXPECT_EQ(widget_observer.bounds(), bounds);
}
// 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) {
scoped_ptr<Widget> widget(new Widget);
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
widget->Init(params);
}
// 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) {
scoped_ptr<Widget> widget(new Widget);
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_WINDOW);
params.bounds = gfx::Rect(0, 0, 200, 200);
params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
widget->Init(params);
widget->Show();
ui::test::EventGenerator generator(GetContext(), widget->GetNativeWindow());
generator.MoveMouseTo(10, 10);
generator.PressTouch();
widget.reset();
}
// A view that consumes mouse-pressed event and gesture-tap-down events.
class RootViewTestView : public View {
public:
RootViewTestView(): View() {}
private:
bool OnMousePressed(const ui::MouseEvent& event) override { return true; }
void OnGestureEvent(ui::GestureEvent* event) override {
if (event->type() == ui::ET_GESTURE_TAP_DOWN)
event->SetHandled();
}
};
// Checks if RootView::*_handler_ fields are unset when widget is hidden.
// Fails on chromium.webkit Windows bot, see crbug.com/264872.
#if defined(OS_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->AddChildView(view);
// Check RootView::mouse_pressed_handler_.
widget->Show();
EXPECT_EQ(NULL, GetMousePressedHandler(root_view));
gfx::Point click_location(45, 15);
ui::MouseEvent press(ui::ET_MOUSE_PRESSED, click_location, click_location,
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
widget->OnMouseEvent(&press);
EXPECT_EQ(view, GetMousePressedHandler(root_view));
widget->Hide();
EXPECT_EQ(NULL, GetMousePressedHandler(root_view));
// Check RootView::mouse_move_handler_.
widget->Show();
EXPECT_EQ(NULL, GetMouseMoveHandler(root_view));
gfx::Point move_location(45, 15);
ui::MouseEvent move(ui::ET_MOUSE_MOVED, move_location, move_location, 0, 0);
widget->OnMouseEvent(&move);
EXPECT_EQ(view, GetMouseMoveHandler(root_view));
widget->Hide();
EXPECT_EQ(NULL, GetMouseMoveHandler(root_view));
// Check RootView::gesture_handler_.
widget->Show();
EXPECT_EQ(NULL, GetGestureHandler(root_view));
ui::GestureEvent tap_down(15,
15,
0,
base::TimeDelta(),
ui::GestureEventDetails(ui::ET_GESTURE_TAP_DOWN));
widget->OnGestureEvent(&tap_down);
EXPECT_EQ(view, GetGestureHandler(root_view));
widget->Hide();
EXPECT_EQ(NULL, GetGestureHandler(root_view));
widget->Close();
}
// Convenience to make constructing a GestureEvent simpler.
class GestureEventForTest : public ui::GestureEvent {
public:
GestureEventForTest(ui::EventType type, int x, int y)
: GestureEvent(x,
y,
0,
base::TimeDelta(),
ui::GestureEventDetails(type)) {}
GestureEventForTest(ui::GestureEventDetails details, int x, int y)
: GestureEvent(x, y, 0, base::TimeDelta(), details) {}
};
// Tests that the |gesture_handler_| member in RootView is always NULL
// after the dispatch of a ui::ET_GESTURE_END event corresponding to
// the release of the final touch point on the screen, but that
// ui::ET_GESTURE_END 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->AddChildView(view);
widget->Show();
// If no gesture handler is set, a ui::ET_GESTURE_END event should not set
// the gesture handler.
EXPECT_EQ(NULL, GetGestureHandler(root_view));
GestureEventForTest end(ui::ET_GESTURE_END, 15, 15);
widget->OnGestureEvent(&end);
EXPECT_EQ(NULL, 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::CONSUME_EVENTS);
GestureEventForTest tap(ui::ET_GESTURE_TAP, 15, 15);
widget->OnGestureEvent(&tap);
EXPECT_TRUE(tap.handled());
EXPECT_EQ(view, GetGestureHandler(root_view));
// The gesture handler should remain unchanged on a ui::ET_GESTURE_END
// corresponding to a second touch point, but should be reset to NULL by a
// ui::ET_GESTURE_END corresponding to the final touch point.
ui::GestureEventDetails details(ui::ET_GESTURE_END);
details.set_touch_points(2);
GestureEventForTest end_second_touch_point(details, 15, 15);
widget->OnGestureEvent(&end_second_touch_point);
EXPECT_EQ(view, GetGestureHandler(root_view));
end = GestureEventForTest(ui::ET_GESTURE_END, 15, 15);
widget->OnGestureEvent(&end);
EXPECT_TRUE(end.handled());
EXPECT_EQ(NULL, 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 = GestureEventForTest(ui::ET_GESTURE_TAP, 15, 15);
widget->OnGestureEvent(&tap);
EXPECT_TRUE(tap.handled());
EXPECT_EQ(view, GetGestureHandler(root_view));
view->set_handle_mode(EventCountView::PROPAGATE_EVENTS);
// The gesture handler should remain unchanged on a ui::ET_GESTURE_END
// corresponding to a second touch point, but should be reset to NULL by a
// ui::ET_GESTURE_END corresponding to the final touch point.
end_second_touch_point = GestureEventForTest(details, 15, 15);
widget->OnGestureEvent(&end_second_touch_point);
EXPECT_EQ(view, GetGestureHandler(root_view));
end = GestureEventForTest(ui::ET_GESTURE_END, 15, 15);
widget->OnGestureEvent(&end);
EXPECT_FALSE(end.handled());
EXPECT_EQ(NULL, 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->AddChildView(v1);
v1->AddChildView(v2);
v2->AddChildView(v3);
v3->AddChildView(v4);
widget->Show();
// ui::ET_GESTURE_BEGIN events should never be seen by any view, but
// they should be marked as handled by OnEventProcessingStarted().
GestureEventForTest begin(ui::ET_GESTURE_BEGIN, 5, 5);
widget->OnGestureEvent(&begin);
EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_BEGIN));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_BEGIN));
EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_BEGIN));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_BEGIN));
EXPECT_EQ(NULL, GetGestureHandler(root_view));
EXPECT_TRUE(begin.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// ui::ET_GESTURE_END 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().
GestureEventForTest end(ui::ET_GESTURE_END, 5, 5);
widget->OnGestureEvent(&end);
EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(NULL, GetGestureHandler(root_view));
EXPECT_TRUE(end.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// ui::ET_GESTURE_END 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::ET_GESTURE_END);
details.set_touch_points(2);
GestureEventForTest end_second_touch_point(details, 5, 5);
widget->OnGestureEvent(&end_second_touch_point);
EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(NULL, GetGestureHandler(root_view));
EXPECT_TRUE(end_second_touch_point.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// ui::ET_GESTURE_SCROLL_UPDATE 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().
GestureEventForTest scroll_update(ui::ET_GESTURE_SCROLL_UPDATE, 5, 5);
widget->OnGestureEvent(&scroll_update);
EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_SCROLL_UPDATE));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_SCROLL_UPDATE));
EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_SCROLL_UPDATE));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_SCROLL_UPDATE));
EXPECT_EQ(NULL, GetGestureHandler(root_view));
EXPECT_TRUE(scroll_update.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// ui::ET_GESTURE_SCROLL_END 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().
GestureEventForTest scroll_end(ui::ET_GESTURE_SCROLL_END, 5, 5);
widget->OnGestureEvent(&scroll_end);
EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_SCROLL_END));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_SCROLL_END));
EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_SCROLL_END));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_SCROLL_END));
EXPECT_EQ(NULL, GetGestureHandler(root_view));
EXPECT_TRUE(scroll_end.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// ui::ET_SCROLL_FLING_START 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().
GestureEventForTest scroll_fling_start(ui::ET_SCROLL_FLING_START, 5, 5);
widget->OnGestureEvent(&scroll_fling_start);
EXPECT_EQ(0, v1->GetEventCount(ui::ET_SCROLL_FLING_START));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_SCROLL_FLING_START));
EXPECT_EQ(0, v3->GetEventCount(ui::ET_SCROLL_FLING_START));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_SCROLL_FLING_START));
EXPECT_EQ(NULL, 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->AddChildView(v1);
v1->AddChildView(v2);
v2->AddChildView(v3);
v3->AddChildView(v4);
widget->Show();
// No gesture handler is set in the root view and none of the views in the
// view hierarchy handle a ui::ET_GESTURE_TAP 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.
GestureEventForTest tap(ui::ET_GESTURE_TAP, 5, 5);
EXPECT_EQ(NULL, GetGestureHandler(root_view));
widget->OnGestureEvent(&tap);
EXPECT_EQ(1, v1->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(1, v2->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(1, v3->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(1, v4->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(NULL, 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::ET_GESTURE_TAP 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::CONSUME_EVENTS);
v2->set_handle_mode(EventCountView::CONSUME_EVENTS);
v3->set_handle_mode(EventCountView::CONSUME_EVENTS);
tap = GestureEventForTest(ui::ET_GESTURE_TAP, 5, 5);
widget->OnGestureEvent(&tap);
EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(1, v3->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(1, v4->GetEventCount(ui::ET_GESTURE_TAP));
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::CONSUME_EVENTS);
tap = GestureEventForTest(ui::ET_GESTURE_TAP, 5, 5);
widget->OnGestureEvent(&tap);
EXPECT_TRUE(tap.handled());
GestureEventForTest show_press(ui::ET_GESTURE_SHOW_PRESS, 5, 5);
widget->OnGestureEvent(&show_press);
tap = GestureEventForTest(ui::ET_GESTURE_TAP, 5, 5);
widget->OnGestureEvent(&tap);
EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(2, v3->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_SHOW_PRESS));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_SHOW_PRESS));
EXPECT_EQ(1, v3->GetEventCount(ui::ET_GESTURE_SHOW_PRESS));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_SHOW_PRESS));
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::ET_GESTURE_TAP 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::PROPAGATE_EVENTS);
tap = GestureEventForTest(ui::ET_GESTURE_TAP, 5, 5);
widget->OnGestureEvent(&tap);
EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(1, v3->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_TAP));
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->AddChildView(v1);
v1->AddChildView(v2);
v2->AddChildView(v3);
v3->AddChildView(v4);
widget->Show();
// Change the handle mode of |v3| to indicate that it would like to handle
// gesture events.
v3->set_handle_mode(EventCountView::CONSUME_EVENTS);
// When no gesture handler is set, dispatching a ui::ET_GESTURE_TAP_DOWN
// 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(NULL, GetGestureHandler(root_view));
GestureEventForTest tap_down(ui::ET_GESTURE_TAP_DOWN, 5, 5);
widget->OnGestureEvent(&tap_down);
EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_TAP_DOWN));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_TAP_DOWN));
EXPECT_EQ(1, v3->GetEventCount(ui::ET_GESTURE_TAP_DOWN));
EXPECT_EQ(1, v4->GetEventCount(ui::ET_GESTURE_TAP_DOWN));
EXPECT_EQ(v3, GetGestureHandler(root_view));
EXPECT_TRUE(tap_down.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// A ui::ET_GESTURE_TAP_CANCEL event should be dispatched to |v3| directly.
GestureEventForTest tap_cancel(ui::ET_GESTURE_TAP_CANCEL, 5, 5);
widget->OnGestureEvent(&tap_cancel);
EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_TAP_CANCEL));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_TAP_CANCEL));
EXPECT_EQ(1, v3->GetEventCount(ui::ET_GESTURE_TAP_CANCEL));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_TAP_CANCEL));
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::PROPAGATE_EVENTS);
v1->set_handle_mode(EventCountView::CONSUME_EVENTS);
// Dispatch a ui::ET_GESTURE_SCROLL_BEGIN 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|.
GestureEventForTest scroll_begin(ui::ET_GESTURE_SCROLL_BEGIN, 5, 5);
widget->OnGestureEvent(&scroll_begin);
EXPECT_EQ(1, v1->GetEventCount(ui::ET_GESTURE_SCROLL_BEGIN));
EXPECT_EQ(1, v2->GetEventCount(ui::ET_GESTURE_SCROLL_BEGIN));
EXPECT_EQ(1, v3->GetEventCount(ui::ET_GESTURE_SCROLL_BEGIN));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_SCROLL_BEGIN));
EXPECT_EQ(v1, GetGestureHandler(root_view));
EXPECT_TRUE(scroll_begin.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// A ui::ET_GESTURE_SCROLL_UPDATE event should be dispatched to |v1|
// directly.
GestureEventForTest scroll_update(ui::ET_GESTURE_SCROLL_UPDATE, 5, 5);
widget->OnGestureEvent(&scroll_update);
EXPECT_EQ(1, v1->GetEventCount(ui::ET_GESTURE_SCROLL_UPDATE));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_SCROLL_UPDATE));
EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_SCROLL_UPDATE));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_SCROLL_UPDATE));
EXPECT_EQ(v1, GetGestureHandler(root_view));
EXPECT_TRUE(scroll_update.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// A ui::ET_GESTURE_SCROLL_END event should be dispatched to |v1|
// directly and should not reset the gesture handler.
GestureEventForTest scroll_end(ui::ET_GESTURE_SCROLL_END, 5, 5);
widget->OnGestureEvent(&scroll_end);
EXPECT_EQ(1, v1->GetEventCount(ui::ET_GESTURE_SCROLL_END));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_SCROLL_END));
EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_SCROLL_END));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_SCROLL_END));
EXPECT_EQ(v1, GetGestureHandler(root_view));
EXPECT_TRUE(scroll_end.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// A ui::ET_GESTURE_PINCH_BEGIN event (which is a non-scroll event) should
// still be dispatched to |v1| directly.
GestureEventForTest pinch_begin(ui::ET_GESTURE_PINCH_BEGIN, 5, 5);
widget->OnGestureEvent(&pinch_begin);
EXPECT_EQ(1, v1->GetEventCount(ui::ET_GESTURE_PINCH_BEGIN));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_PINCH_BEGIN));
EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_PINCH_BEGIN));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_PINCH_BEGIN));
EXPECT_EQ(v1, GetGestureHandler(root_view));
EXPECT_TRUE(pinch_begin.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// A ui::ET_GESTURE_END event should be dispatched to |v1| and should
// set the gesture handler to NULL.
GestureEventForTest end(ui::ET_GESTURE_END, 5, 5);
widget->OnGestureEvent(&end);
EXPECT_EQ(1, v1->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(NULL, GetGestureHandler(root_view));
EXPECT_TRUE(end.handled());
widget->Close();
}
// 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 {
public:
GestureLocationView() {}
~GestureLocationView() override {}
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_;
DISALLOW_COPY_AND_ASSIGN(GestureLocationView);
};
// 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->AddChildView(v1);
v1->AddChildView(v2);
v2->AddChildView(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);
GestureEventForTest tap(
ui::ET_GESTURE_TAP, 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::ET_GESTURE_TAP));
EXPECT_EQ(1, view2->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(1, view3->GetEventCount(ui::ET_GESTURE_TAP));
widget->Close();
}
// Verifies that disabled views are permitted to be set as the default gesture
// handler in RootView. Also verifies that gesture events targeted to a disabled
// view are not actually dispatched to the view, but are still marked as
// handled.
TEST_F(WidgetTest, DisabledGestureEventTarget) {
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->AddChildView(v1);
v1->AddChildView(v2);
v2->AddChildView(v3);
v3->AddChildView(v4);
widget->Show();
// |v1|, |v2|, and |v3| all handle gesture events but |v3| is marked as
// disabled.
v1->set_handle_mode(EventCountView::CONSUME_EVENTS);
v2->set_handle_mode(EventCountView::CONSUME_EVENTS);
v3->set_handle_mode(EventCountView::CONSUME_EVENTS);
v3->SetEnabled(false);
// No gesture handler is set in the root view. In this case the tap event
// should be dispatched only to |v4|, the gesture handler should be set to
// |v3|, and the event should be marked as handled.
GestureEventForTest tap(ui::ET_GESTURE_TAP, 5, 5);
widget->OnGestureEvent(&tap);
EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(1, v4->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(v3, GetGestureHandler(root_view));
EXPECT_TRUE(tap.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// A subsequent gesture event should be marked as handled but not dispatched.
tap = GestureEventForTest(ui::ET_GESTURE_TAP, 5, 5);
widget->OnGestureEvent(&tap);
EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(v3, GetGestureHandler(root_view));
EXPECT_TRUE(tap.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// A GESTURE_END should reset the default gesture handler to NULL. It should
// also not be dispatched to |v3| but still marked as handled.
GestureEventForTest end(ui::ET_GESTURE_END, 5, 5);
widget->OnGestureEvent(&end);
EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(NULL, GetGestureHandler(root_view));
EXPECT_TRUE(end.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 which are dispatched to it.
v3->set_handle_mode(EventCountView::PROPAGATE_EVENTS);
// No gesture handler is set in the root view. In this case the tap event
// should be dispatched only to |v4| and the event should be marked as
// handled. Furthermore, the gesture handler should be set to
// |v3|; even though |v3| does not explicitly handle events, it is a
// valid target for the tap event because it is disabled.
tap = GestureEventForTest(ui::ET_GESTURE_TAP, 5, 5);
widget->OnGestureEvent(&tap);
EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(1, v4->GetEventCount(ui::ET_GESTURE_TAP));
EXPECT_EQ(v3, GetGestureHandler(root_view));
EXPECT_TRUE(tap.handled());
v1->ResetCounts();
v2->ResetCounts();
v3->ResetCounts();
v4->ResetCounts();
// A GESTURE_END should reset the default gesture handler to NULL. It should
// also not be dispatched to |v3| but still marked as handled.
end = GestureEventForTest(ui::ET_GESTURE_END, 5, 5);
widget->OnGestureEvent(&end);
EXPECT_EQ(0, v1->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(0, v2->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(0, v3->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(0, v4->GetEventCount(ui::ET_GESTURE_END));
EXPECT_EQ(NULL, GetGestureHandler(root_view));
EXPECT_TRUE(end.handled());
widget->Close();
}
// Test the result of Widget::GetAllChildWidgets().
TEST_F(WidgetTest, GetAllChildWidgets) {
// Create the following widget hierarchy:
//
// toplevel
// +-- w1
// +-- w11
// +-- w2
// +-- w21
// +-- w22
Widget* 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);
expected.insert(w1);
expected.insert(w11);
expected.insert(w2);
expected.insert(w21);
expected.insert(w22);
std::set<Widget*> widgets;
Widget::GetAllChildWidgets(toplevel->GetNativeView(), &widgets);
EXPECT_EQ(expected.size(), widgets.size());
EXPECT_TRUE(std::equal(expected.begin(), expected.end(), widgets.begin()));
}
// Used by DestroyChildWidgetsInOrder. On destruction adds the supplied name to
// a vector.
class DestroyedTrackingView : public View {
public:
DestroyedTrackingView(const std::string& name,
std::vector<std::string>* add_to)
: name_(name),
add_to_(add_to) {
}
~DestroyedTrackingView() override { add_to_->push_back(name_); }
private:
const std::string name_;
std::vector<std::string>* add_to_;
DISALLOW_COPY_AND_ASSIGN(DestroyedTrackingView);
};
class WidgetChildDestructionTest : public WidgetTest {
public:
WidgetChildDestructionTest() {}
// 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(views::Widget::InitParams::TYPE_WINDOW);
#if !defined(OS_CHROMEOS)
if (top_level_has_desktop_native_widget_aura)
params.native_widget = new PlatformDesktopNativeWidget(top_level);
#endif
top_level->Init(params);
top_level->GetRootView()->AddChildView(
new DestroyedTrackingView("parent", &destroyed));
top_level->Show();
Widget* child = new Widget;
Widget::InitParams child_params =
CreateParams(views::Widget::InitParams::TYPE_POPUP);
child_params.parent = top_level->GetNativeView();
#if !defined(OS_CHROMEOS)
if (child_has_desktop_native_widget_aura)
child_params.native_widget = new PlatformDesktopNativeWidget(child);
#endif
child->Init(child_params);
child->GetRootView()->AddChildView(
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]);
}
private:
DISALLOW_COPY_AND_ASSIGN(WidgetChildDestructionTest);
};
#if !defined(OS_CHROMEOS)
// 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);
}
#endif // !defined(OS_CHROMEOS)
// See description of RunDestroyChildWidgetsTest().
TEST_F(WidgetChildDestructionTest, DestroyChildWidgetsInOrder) {
RunDestroyChildWidgetsTest(false, false);
}
#if !defined(OS_CHROMEOS)
// Provides functionality to create a window modal dialog.
class ModalDialogDelegate : public DialogDelegateView {
public:
ModalDialogDelegate() {}
~ModalDialogDelegate() override {}
// WidgetDelegate overrides.
ui::ModalType GetModalType() const override { return ui::MODAL_TYPE_WINDOW; }
private:
DISALLOW_COPY_AND_ASSIGN(ModalDialogDelegate);
};
// This test verifies that whether mouse events when a modal dialog is
// displayed are eaten or recieved by the dialog.
TEST_F(WidgetTest, WindowMouseModalityTest) {
// Create a top level widget.
Widget top_level_widget;
Widget::InitParams init_params =
CreateParams(Widget::InitParams::TYPE_WINDOW);
init_params.show_state = ui::SHOW_STATE_NORMAL;
gfx::Rect initial_bounds(0, 0, 500, 500);
init_params.bounds = initial_bounds;
init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
init_params.native_widget =
new PlatformDesktopNativeWidget(&top_level_widget);
top_level_widget.Init(init_params);
top_level_widget.Show();
EXPECT_TRUE(top_level_widget.IsVisible());
// Create a view and validate that a mouse moves makes it to the view.
EventCountView* widget_view = new EventCountView();
widget_view->SetBounds(0, 0, 10, 10);
top_level_widget.GetRootView()->AddChildView(widget_view);
gfx::Point cursor_location_main(5, 5);
ui::MouseEvent move_main(ui::ET_MOUSE_MOVED,
cursor_location_main,
cursor_location_main,
ui::EF_NONE,
ui::EF_NONE);
ui::EventDispatchDetails details =
GetEventProcessor(&top_level_widget)->OnEventFromSource(&move_main);
ASSERT_FALSE(details.dispatcher_destroyed);
EXPECT_EQ(1, widget_view->GetEventCount(ui::ET_MOUSE_ENTERED));
widget_view->ResetCounts();
// Create a modal dialog and validate that a mouse down message makes it to
// the main view within the dialog.
// This instance will be destroyed when the dialog is destroyed.
ModalDialogDelegate* dialog_delegate = new ModalDialogDelegate;
Widget* modal_dialog_widget = views::DialogDelegate::CreateDialogWidget(
dialog_delegate, NULL, top_level_widget.GetNativeView());
modal_dialog_widget->SetBounds(gfx::Rect(100, 100, 200, 200));
EventCountView* dialog_widget_view = new EventCountView();
dialog_widget_view->SetBounds(0, 0, 50, 50);
modal_dialog_widget->GetRootView()->AddChildView(dialog_widget_view);
modal_dialog_widget->Show();
EXPECT_TRUE(modal_dialog_widget->IsVisible());
gfx::Point cursor_location_dialog(100, 100);
ui::MouseEvent mouse_down_dialog(ui::ET_MOUSE_PRESSED,
cursor_location_dialog,
cursor_location_dialog,
ui::EF_NONE,
ui::EF_NONE);
details = GetEventProcessor(&top_level_widget)->OnEventFromSource(
&mouse_down_dialog);
ASSERT_FALSE(details.dispatcher_destroyed);
EXPECT_EQ(1, dialog_widget_view->GetEventCount(ui::ET_MOUSE_PRESSED));
// Send a mouse move message to the main window. It should not be received by
// the main window as the modal dialog is still active.
gfx::Point cursor_location_main2(6, 6);
ui::MouseEvent mouse_down_main(ui::ET_MOUSE_MOVED,
cursor_location_main2,
cursor_location_main2,
ui::EF_NONE,
ui::EF_NONE);
details = GetEventProcessor(&top_level_widget)->OnEventFromSource(
&mouse_down_main);
ASSERT_FALSE(details.dispatcher_destroyed);
EXPECT_EQ(0, widget_view->GetEventCount(ui::ET_MOUSE_MOVED));
modal_dialog_widget->CloseNow();
top_level_widget.CloseNow();
}
// Verifies nativeview visbility matches that of Widget visibility when
// SetFullscreen is invoked.
TEST_F(WidgetTest, FullscreenStatePropagated) {
Widget::InitParams init_params =
CreateParams(Widget::InitParams::TYPE_WINDOW);
init_params.show_state = ui::SHOW_STATE_NORMAL;
init_params.bounds = gfx::Rect(0, 0, 500, 500);
init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
{
Widget top_level_widget;
top_level_widget.Init(init_params);
top_level_widget.SetFullscreen(true);
EXPECT_EQ(top_level_widget.IsVisible(),
IsNativeWindowVisible(top_level_widget.GetNativeWindow()));
top_level_widget.CloseNow();
}
#if !defined(OS_CHROMEOS)
{
Widget top_level_widget;
init_params.native_widget =
new PlatformDesktopNativeWidget(&top_level_widget);
top_level_widget.Init(init_params);
top_level_widget.SetFullscreen(true);
EXPECT_EQ(top_level_widget.IsVisible(),
IsNativeWindowVisible(top_level_widget.GetNativeWindow()));
top_level_widget.CloseNow();
}
#endif
}
#if defined(OS_WIN)
// Provides functionality to test widget activation via an activation flag
// which can be set by an accessor.
class ModalWindowTestWidgetDelegate : public WidgetDelegate {
public:
ModalWindowTestWidgetDelegate()
: widget_(NULL),
can_activate_(true) {}
virtual ~ModalWindowTestWidgetDelegate() {}
// Overridden from WidgetDelegate:
virtual void DeleteDelegate() override {
delete this;
}
virtual Widget* GetWidget() override {
return widget_;
}
virtual const Widget* GetWidget() const override {
return widget_;
}
virtual bool CanActivate() const override {
return can_activate_;
}
virtual bool ShouldAdvanceFocusToTopLevelWidget() const override {
return true;
}
void set_can_activate(bool can_activate) {
can_activate_ = can_activate;
}
void set_widget(Widget* widget) {
widget_ = widget;
}
private:
Widget* widget_;
bool can_activate_;
DISALLOW_COPY_AND_ASSIGN(ModalWindowTestWidgetDelegate);
};
// Tests whether we can activate the top level widget when a modal dialog is
// active.
TEST_F(WidgetTest, WindowModalityActivationTest) {
// Destroyed when the top level widget created below is destroyed.
ModalWindowTestWidgetDelegate* widget_delegate =
new ModalWindowTestWidgetDelegate;
// Create a top level widget.
Widget top_level_widget;
Widget::InitParams init_params =
CreateParams(Widget::InitParams::TYPE_WINDOW);
init_params.show_state = ui::SHOW_STATE_NORMAL;
gfx::Rect initial_bounds(0, 0, 500, 500);
init_params.bounds = initial_bounds;
init_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
init_params.native_widget = new DesktopNativeWidgetAura(&top_level_widget);
init_params.delegate = widget_delegate;
top_level_widget.Init(init_params);
widget_delegate->set_widget(&top_level_widget);
top_level_widget.Show();
EXPECT_TRUE(top_level_widget.IsVisible());
HWND win32_window = views::HWNDForWidget(&top_level_widget);
EXPECT_TRUE(::IsWindow(win32_window));
// This instance will be destroyed when the dialog is destroyed.
ModalDialogDelegate* dialog_delegate = new ModalDialogDelegate;
// We should be able to activate the window even if the WidgetDelegate
// says no, when a modal dialog is active.
widget_delegate->set_can_activate(false);
Widget* modal_dialog_widget = views::DialogDelegate::CreateDialogWidget(
dialog_delegate, NULL, top_level_widget.GetNativeWindow());
modal_dialog_widget->SetBounds(gfx::Rect(100, 100, 200, 200));
modal_dialog_widget->Show();
EXPECT_TRUE(modal_dialog_widget->IsVisible());
LRESULT activate_result = ::SendMessage(
win32_window,
WM_MOUSEACTIVATE,
reinterpret_cast<WPARAM>(win32_window),
MAKELPARAM(WM_LBUTTONDOWN, HTCLIENT));
EXPECT_EQ(activate_result, MA_ACTIVATE);
modal_dialog_widget->CloseNow();
top_level_widget.CloseNow();
}
#endif // defined(OS_WIN)
#endif // !defined(OS_CHROMEOS)
TEST_F(WidgetTest, ShowCreatesActiveWindow) {
Widget* widget = CreateTopLevelPlatformWidget();
widget->Show();
EXPECT_EQ(GetWidgetShowState(widget), ui::SHOW_STATE_NORMAL);
widget->CloseNow();
}
// OSX does not have a per-application "active" window such as provided by
// ::GetActiveWindow() on Windows. There is only a system-wide "keyWindow" which
// is updated asynchronously.
#if defined(OS_MACOSX)
#define MAYBE_ShowInactive DISABLED_ShowInactive
#else
#define MAYBE_ShowInactive ShowInactive
#endif
TEST_F(WidgetTest, MAYBE_ShowInactive) {
Widget* widget = CreateTopLevelPlatformWidget();
widget->ShowInactive();
EXPECT_EQ(GetWidgetShowState(widget), ui::SHOW_STATE_INACTIVE);
widget->CloseNow();
}
TEST_F(WidgetTest, InactiveBeforeShow) {
Widget* widget = CreateTopLevelPlatformWidget();
EXPECT_FALSE(widget->IsActive());
EXPECT_FALSE(widget->IsVisible());
widget->Show();
EXPECT_TRUE(widget->IsActive());
EXPECT_TRUE(widget->IsVisible());
widget->CloseNow();
}
TEST_F(WidgetTest, ShowInactiveAfterShow) {
// Create 2 widgets to ensure window layering does not change.
Widget* widget = CreateTopLevelPlatformWidget();
Widget* widget2 = CreateTopLevelPlatformWidget();
widget2->Show();
EXPECT_FALSE(widget->IsActive());
EXPECT_TRUE(widget2->IsVisible());
EXPECT_TRUE(widget2->IsActive());
widget->Show();
EXPECT_TRUE(widget->IsActive());
EXPECT_FALSE(widget2->IsActive());
widget->ShowInactive();
EXPECT_TRUE(widget->IsActive());
EXPECT_FALSE(widget2->IsActive());
EXPECT_EQ(GetWidgetShowState(widget), ui::SHOW_STATE_NORMAL);
widget2->CloseNow();
widget->CloseNow();
}
TEST_F(WidgetTest, ShowAfterShowInactive) {
Widget* widget = CreateTopLevelPlatformWidget();
widget->ShowInactive();
widget->Show();
EXPECT_EQ(GetWidgetShowState(widget), ui::SHOW_STATE_NORMAL);
widget->CloseNow();
}
#if !defined(OS_CHROMEOS)
TEST_F(WidgetTest, InactiveWidgetDoesNotGrabActivation) {
Widget* widget = CreateTopLevelPlatformWidget();
widget->Show();
EXPECT_EQ(GetWidgetShowState(widget), ui::SHOW_STATE_NORMAL);
Widget widget2;
Widget::InitParams params = CreateParams(Widget::InitParams::TYPE_POPUP);
params.native_widget = new PlatformDesktopNativeWidget(&widget2);
params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
widget2.Init(params);
widget2.Show();
EXPECT_EQ(GetWidgetShowState(&widget2), ui::SHOW_STATE_INACTIVE);
EXPECT_EQ(GetWidgetShowState(widget), ui::SHOW_STATE_NORMAL);
widget->CloseNow();
widget2.CloseNow();
}
#endif // !defined(OS_CHROMEOS)
namespace {
class FullscreenAwareFrame : public views::NonClientFrameView {
public:
explicit FullscreenAwareFrame(views::Widget* widget)
: widget_(widget), fullscreen_layout_called_(false) {}
~FullscreenAwareFrame() override {}
// 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, gfx::Path* window_mask) override {}
void ResetWindowControls() override {}
void UpdateWindowIcon() override {}
void UpdateWindowTitle() override {}
void SizeConstraintsChanged() override {}
// views::View overrides:
void Layout() override {
if (widget_->IsFullscreen())
fullscreen_layout_called_ = true;
}
bool fullscreen_layout_called() const { return fullscreen_layout_called_; }
private:
views::Widget* widget_;
bool fullscreen_layout_called_;
DISALLOW_COPY_AND_ASSIGN(FullscreenAwareFrame);
};
} // namespace
// Tests that frame Layout is called when a widget goes fullscreen without
// changing its size or title.
TEST_F(WidgetTest, FullscreenFrameLayout) {
Widget* widget = CreateTopLevelPlatformWidget();
FullscreenAwareFrame* frame = new FullscreenAwareFrame(widget);
widget->non_client_view()->SetFrameView(frame); // Owns |frame|.
widget->Maximize();
RunPendingMessages();
EXPECT_FALSE(frame->fullscreen_layout_called());
widget->SetFullscreen(true);
widget->Show();
RunPendingMessages();
EXPECT_TRUE(frame->fullscreen_layout_called());
widget->CloseNow();
}
#if !defined(OS_CHROMEOS)
namespace {
// Trivial WidgetObserverTest that invokes Widget::IsActive() from
// OnWindowDestroying.
class IsActiveFromDestroyObserver : public WidgetObserver {
public:
IsActiveFromDestroyObserver() {}
~IsActiveFromDestroyObserver() override {}
void OnWidgetDestroying(Widget* widget) override { widget->IsActive(); }
private:
DISALLOW_COPY_AND_ASSIGN(IsActiveFromDestroyObserver);
};
} // namespace
// Verifies Widget::IsActive() invoked from
// WidgetObserver::OnWidgetDestroying() in a child widget doesn't crash.
TEST_F(WidgetTest, IsActiveFromDestroy) {
// Create two widgets, one a child of the other.
IsActiveFromDestroyObserver observer;
Widget parent_widget;
Widget::InitParams parent_params =
CreateParams(Widget::InitParams::TYPE_POPUP);
parent_params.native_widget = new PlatformDesktopNativeWidget(&parent_widget);
parent_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
parent_widget.Init(parent_params);
parent_widget.Show();
Widget child_widget;
Widget::InitParams child_params =
CreateParams(Widget::InitParams::TYPE_POPUP);
child_params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
child_params.context = parent_widget.GetNativeWindow();
child_widget.Init(child_params);
child_widget.AddObserver(&observer);
child_widget.Show();
parent_widget.CloseNow();
}
#endif // !defined(OS_CHROMEOS)
// 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) {
EventCountView* view = new EventCountView;
view->set_handle_mode(EventCountView::CONSUME_EVENTS);
view->SetBounds(10, 10, 50, 40);
Widget* widget = CreateTopLevelFramelessPlatformWidget();
widget->GetRootView()->AddChildView(view);
widget->SetBounds(gfx::Rect(0, 0, 100, 80));
widget->Show();
ui::test::EventGenerator generator(GetContext(), widget->GetNativeWindow());
generator.set_current_location(gfx::Point(20, 20));
generator.ClickLeftButton();
EXPECT_EQ(1, view->GetEventCount(ui::ET_MOUSE_PRESSED));
EXPECT_EQ(1, view->GetEventCount(ui::ET_MOUSE_RELEASED));
EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON, view->last_flags());
generator.PressRightButton();
EXPECT_EQ(2, view->GetEventCount(ui::ET_MOUSE_PRESSED));
EXPECT_EQ(1, view->GetEventCount(ui::ET_MOUSE_RELEASED));
EXPECT_EQ(ui::EF_RIGHT_MOUSE_BUTTON, view->last_flags());
generator.ReleaseRightButton();
EXPECT_EQ(2, view->GetEventCount(ui::ET_MOUSE_PRESSED));
EXPECT_EQ(2, view->GetEventCount(ui::ET_MOUSE_RELEASED));
EXPECT_EQ(ui::EF_RIGHT_MOUSE_BUTTON, view->last_flags());
// Test mouse move events.
EXPECT_EQ(0, view->GetEventCount(ui::ET_MOUSE_MOVED));
EXPECT_EQ(0, view->GetEventCount(ui::ET_MOUSE_ENTERED));
// Move the mouse within the view (20, 20) -> (30, 30).
generator.MoveMouseTo(gfx::Point(30, 30));
EXPECT_EQ(1, view->GetEventCount(ui::ET_MOUSE_MOVED));
EXPECT_EQ(1, view->GetEventCount(ui::ET_MOUSE_ENTERED));
EXPECT_EQ(ui::EF_NONE, view->last_flags());
// Move it again - entered count shouldn't change.
generator.MoveMouseTo(gfx::Point(31, 31));
EXPECT_EQ(2, view->GetEventCount(ui::ET_MOUSE_MOVED));
EXPECT_EQ(1, view->GetEventCount(ui::ET_MOUSE_ENTERED));
EXPECT_EQ(0, view->GetEventCount(ui::ET_MOUSE_EXITED));
// Move it off the view.
generator.MoveMouseTo(gfx::Point(5, 5));
EXPECT_EQ(2, view->GetEventCount(ui::ET_MOUSE_MOVED));
EXPECT_EQ(1, view->GetEventCount(ui::ET_MOUSE_ENTERED));
EXPECT_EQ(1, view->GetEventCount(ui::ET_MOUSE_EXITED));
// Move it back on.
generator.MoveMouseTo(gfx::Point(20, 20));
EXPECT_EQ(3, view->GetEventCount(ui::ET_MOUSE_MOVED));
EXPECT_EQ(2, view->GetEventCount(ui::ET_MOUSE_ENTERED));
EXPECT_EQ(1, view->GetEventCount(ui::ET_MOUSE_EXITED));
// Drargging. Cover HasCapture() and NativeWidgetPrivate::IsMouseButtonDown().
generator.DragMouseTo(gfx::Point(40, 40));
EXPECT_EQ(3, view->GetEventCount(ui::ET_MOUSE_PRESSED));
EXPECT_EQ(3, view->GetEventCount(ui::ET_MOUSE_RELEASED));
EXPECT_EQ(1, view->GetEventCount(ui::ET_MOUSE_DRAGGED));
EXPECT_EQ(ui::EF_LEFT_MOUSE_BUTTON, view->last_flags());
widget->CloseNow();
}
// 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) {
Widget* 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());
widget->CloseNow();
}
} // namespace test
} // namespace views
|