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
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/controls/scroll_view.h"
#include <algorithm>
#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/gtest_util.h"
#include "base/test/icu_test_util.h"
#include "base/test/mock_callback.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_timeouts.h"
#include "base/timer/timer.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/metadata/metadata_header_macros.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/ui_base_features.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_type.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/events/test/event_generator.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/geometry/point_f.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/border.h"
#include "ui/views/controls/scrollbar/base_scroll_bar_thumb.h"
#include "ui/views/controls/scrollbar/overlay_scroll_bar.h"
#include "ui/views/controls/scrollbar/scroll_bar_views.h"
#include "ui/views/test/test_views.h"
#include "ui/views/test/views_test_base.h"
#include "ui/views/test/views_test_utils.h"
#include "ui/views/test/widget_test.h"
#include "ui/views/view_observer.h"
#include "ui/views/view_test_api.h"
#include "ui/views/view_tracker.h"
#if BUILDFLAG(IS_MAC)
#include "ui/base/test/scoped_preferred_scroller_style_mac.h"
#endif
enum ScrollBarOrientation { HORIZONTAL, VERTICAL };
namespace views {
namespace test {
class ScrollViewTestApi {
public:
explicit ScrollViewTestApi(ScrollView* scroll_view)
: scroll_view_(scroll_view) {}
ScrollViewTestApi(const ScrollViewTestApi&) = delete;
ScrollViewTestApi& operator=(const ScrollViewTestApi&) = delete;
~ScrollViewTestApi() = default;
ScrollBar* GetScrollBar(ScrollBarOrientation orientation) {
ScrollBar* scroll_bar = orientation == VERTICAL
? scroll_view_->vertical_scroll_bar()
: scroll_view_->horizontal_scroll_bar();
return scroll_bar;
}
const base::OneShotTimer& GetScrollBarTimer(
ScrollBarOrientation orientation) {
return GetScrollBar(orientation)->repeater_.timer_for_testing();
}
BaseScrollBarThumb* GetScrollBarThumb(ScrollBarOrientation orientation) {
return GetScrollBar(orientation)->thumb_;
}
gfx::Vector2d IntegralViewOffset() {
return gfx::Point() - gfx::ToFlooredPoint(CurrentOffset());
}
gfx::PointF CurrentOffset() const { return scroll_view_->CurrentOffset(); }
base::RetainingOneShotTimer* GetScrollBarHideTimer(
ScrollBarOrientation orientation) {
return ScrollBar::GetHideTimerForTesting(GetScrollBar(orientation));
}
View* corner_view() { return scroll_view_->corner_view_.get(); }
View* contents_viewport() {
return scroll_view_->GetContentsViewportForTest();
}
View* more_content_left() { return scroll_view_->more_content_left_.get(); }
View* more_content_top() { return scroll_view_->more_content_top_.get(); }
View* more_content_right() { return scroll_view_->more_content_right_.get(); }
View* more_content_bottom() {
return scroll_view_->more_content_bottom_.get();
}
private:
raw_ptr<ScrollView> scroll_view_;
};
} // namespace test
namespace {
const int kWidth = 100;
const int kMinHeight = 50;
const int kMaxHeight = 100;
class FixedView : public View {
METADATA_HEADER(FixedView, View)
public:
FixedView() = default;
FixedView(const FixedView&) = delete;
FixedView& operator=(const FixedView&) = delete;
~FixedView() override = default;
std::optional<bool> did_show_tooltip_in_viewport() const {
return did_show_tooltip_in_viewport_;
}
void Layout(PassKey) override {
gfx::Size pref = GetPreferredSize({});
SetBounds(x(), y(), pref.width(), pref.height());
}
void UpdateTooltipForFocus() override {
View::UpdateTooltipForFocus();
// To verify `UpdateTooltipForFocus()` is called after the view scrolled, we
// can set `did_show_tooltip_in_viewport_` as true only when this view is in
// the visible rect.
did_show_tooltip_in_viewport_ = !GetVisibleBounds().IsEmpty();
}
void SetFocus() { Focus(); }
private:
// True if this view is in the viewport of its parent scroll view to show the
// tooltip.
std::optional<bool> did_show_tooltip_in_viewport_;
};
BEGIN_METADATA(FixedView)
END_METADATA
class CustomView : public View {
METADATA_HEADER(CustomView, View)
public:
CustomView() = default;
CustomView(const CustomView&) = delete;
CustomView& operator=(const CustomView&) = delete;
~CustomView() override = default;
const gfx::Point last_location() const { return last_location_; }
void Layout(PassKey) override {
gfx::Size pref = GetPreferredSize({});
int width = pref.width();
int height = pref.height();
if (parent()) {
width = std::max(parent()->width(), width);
height = std::max(parent()->height(), height);
}
SetBounds(x(), y(), width, height);
}
bool OnMousePressed(const ui::MouseEvent& event) override {
last_location_ = event.location();
return true;
}
private:
gfx::Point last_location_;
};
BEGIN_METADATA(CustomView)
END_METADATA
void CheckScrollbarVisibility(const ScrollView* scroll_view,
ScrollBarOrientation orientation,
bool should_be_visible) {
const ScrollBar* scrollbar = orientation == HORIZONTAL
? scroll_view->horizontal_scroll_bar()
: scroll_view->vertical_scroll_bar();
if (should_be_visible) {
ASSERT_TRUE(scrollbar);
EXPECT_TRUE(scrollbar->GetVisible());
} else {
EXPECT_TRUE(!scrollbar || !scrollbar->GetVisible());
}
}
ui::MouseEvent TestLeftMouseAt(const gfx::Point& location, ui::EventType type) {
return ui::MouseEvent(type, location, location, base::TimeTicks(),
ui::EF_LEFT_MOUSE_BUTTON, ui::EF_LEFT_MOUSE_BUTTON);
}
// This view has a large width, but the height always matches the parent's
// height. This is similar to a TableView that has many columns showing, but
// very few rows.
class VerticalResizingView : public View {
METADATA_HEADER(VerticalResizingView, View)
public:
VerticalResizingView() = default;
VerticalResizingView(const VerticalResizingView&) = delete;
VerticalResizingView& operator=(const VerticalResizingView&) = delete;
~VerticalResizingView() override = default;
void Layout(PassKey) override {
int width = 10000;
int height = parent()->height();
SetBounds(x(), y(), width, height);
}
};
BEGIN_METADATA(VerticalResizingView)
END_METADATA
// Same as VerticalResizingView, but horizontal instead.
class HorizontalResizingView : public View {
METADATA_HEADER(HorizontalResizingView, View)
public:
HorizontalResizingView() = default;
HorizontalResizingView(const HorizontalResizingView&) = delete;
HorizontalResizingView& operator=(const HorizontalResizingView&) = delete;
~HorizontalResizingView() override = default;
void Layout(PassKey) override {
int height = 10000;
int width = parent()->width();
SetBounds(x(), y(), width, height);
}
};
BEGIN_METADATA(HorizontalResizingView)
END_METADATA
class TestScrollBarThumb : public BaseScrollBarThumb {
METADATA_HEADER(TestScrollBarThumb, BaseScrollBarThumb)
public:
using BaseScrollBarThumb::BaseScrollBarThumb;
// BaseScrollBarThumb:
gfx::Size CalculatePreferredSize(
const SizeBounds& /*available_size*/) const override {
return gfx::Size(1, 1);
}
void OnPaint(gfx::Canvas* canvas) override {}
};
BEGIN_METADATA(TestScrollBarThumb)
END_METADATA
class TestScrollBar : public ScrollBar {
METADATA_HEADER(TestScrollBar, ScrollBar)
public:
TestScrollBar(Orientation orientation, bool overlaps_content, int thickness)
: ScrollBar(orientation),
overlaps_content_(overlaps_content),
thickness_(thickness) {
SetThumb(new TestScrollBarThumb(this));
}
// ScrollBar:
int GetThickness() const override { return thickness_; }
bool OverlapsContent() const override { return overlaps_content_; }
gfx::Rect GetTrackBounds() const override {
gfx::Rect bounds = GetLocalBounds();
bounds.set_width(GetThickness());
return bounds;
}
private:
const bool overlaps_content_ = false;
const int thickness_ = 0;
};
BEGIN_METADATA(TestScrollBar)
END_METADATA
} // namespace
using test::ScrollViewTestApi;
// Simple test harness for testing a ScrollView directly.
class ScrollViewTest : public ViewsTestBase {
public:
ScrollViewTest() = default;
ScrollViewTest(const ScrollViewTest&) = delete;
ScrollViewTest& operator=(const ScrollViewTest&) = delete;
void SetUp() override {
ViewsTestBase::SetUp();
scroll_view_ = std::make_unique<ScrollView>();
}
View* InstallContents() {
const gfx::Rect default_outer_bounds(0, 0, 100, 100);
auto* view = scroll_view_->SetContents(std::make_unique<View>());
scroll_view_->SetBoundsRect(default_outer_bounds);
// Setting the contents will invalidate the layout. Call RunScheduledLayout
// to immediately run the scheduled layout.
views::test::RunScheduledLayout(scroll_view_.get());
return view;
}
// For the purposes of these tests, we directly call SetBounds on the
// contents of the ScrollView to see how the ScrollView reacts to it. Calling
// SetBounds will not invalidate parent layouts. Thus, we need to manually
// invalidate the layout of the scroll view. Production code should not need
// to manually invalidate the layout of the ScrollView as the ScrollView's
// layout should be invalidated through the call paths that change the size of
// the ScrollView's contents. (For example, via AddChildView).
void InvalidateAndRunScheduledLayoutOnScrollView() {
scroll_view_->InvalidateLayout();
views::test::RunScheduledLayout(scroll_view_.get());
}
protected:
#if BUILDFLAG(IS_MAC)
void SetOverlayScrollersEnabled(bool enabled) {
// Ensure the old scroller override is destroyed before creating a new one.
// Otherwise, the swizzlers are interleaved and restore incorrect methods.
scroller_style_.reset();
scroller_style_ =
std::make_unique<ui::test::ScopedPreferredScrollerStyle>(enabled);
}
private:
// Disable overlay scrollers by default. This needs to be set before
// |scroll_view_| is initialized, otherwise scrollers will try to animate to
// change modes, which requires a MessageLoop to exist. Tests should only
// modify this via SetOverlayScrollersEnabled().
std::unique_ptr<ui::test::ScopedPreferredScrollerStyle> scroller_style_ =
std::make_unique<ui::test::ScopedPreferredScrollerStyle>(false);
protected:
#endif
int VerticalScrollBarWidth() {
return scroll_view_->vertical_scroll_bar()->GetThickness();
}
int HorizontalScrollBarHeight() {
return scroll_view_->horizontal_scroll_bar()->GetThickness();
}
std::unique_ptr<ScrollView> scroll_view_;
};
// Test harness that includes a Widget to help test ui::Event handling.
class WidgetScrollViewTest : public test::WidgetTest,
public ui::CompositorObserver {
public:
static constexpr int kDefaultHeight = 100;
static constexpr int kDefaultWidth = 100;
WidgetScrollViewTest() = default;
WidgetScrollViewTest(const WidgetScrollViewTest&) = delete;
WidgetScrollViewTest& operator=(const WidgetScrollViewTest&) = delete;
// Call this before adding the ScrollView to test with overlay scrollbars.
void SetUseOverlayScrollers() { use_overlay_scrollers_ = true; }
// Adds a ScrollView with the given |contents_view| and does layout.
ScrollView* AddScrollViewWithContents(std::unique_ptr<View> contents,
bool commit_layers = true) {
#if BUILDFLAG(IS_MAC)
scroller_style_ = std::make_unique<ui::test::ScopedPreferredScrollerStyle>(
use_overlay_scrollers_);
#endif
const gfx::Rect default_bounds(50, 50, kDefaultWidth, kDefaultHeight);
widget_ = CreateTopLevelFramelessPlatformWidget();
widget_->SetBounds(default_bounds);
widget_->Show();
ScrollView* scroll_view =
widget_->SetContentsView(std::make_unique<ScrollView>());
scroll_view->SetContents(std::move(contents));
views::test::RunScheduledLayout(scroll_view);
widget_->GetCompositor()->AddObserver(this);
// Ensure the Compositor has committed layer changes before attempting to
// use them for impl-side scrolling. Note that simply RunUntilIdle() works
// when tests are run in isolation, but compositor scheduling can interact
// between test runs in the general case.
if (commit_layers) {
WaitForCommit();
}
return scroll_view;
}
// Adds a ScrollView with a contents view of the given |size| and does layout.
ScrollView* AddScrollViewWithContentSize(const gfx::Size& contents_size,
bool commit_layers = true) {
auto contents = std::make_unique<View>();
contents->SetSize(contents_size);
return AddScrollViewWithContents(std::move(contents), commit_layers);
}
// Wait for a commit to be observed on the compositor.
void WaitForCommit() {
base::RunLoop run_loop;
quit_closure_ = run_loop.QuitClosure();
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(
FROM_HERE, quit_closure_, TestTimeouts::action_timeout());
run_loop.Run();
EXPECT_TRUE(quit_closure_.is_null()) << "Timed out waiting for a commit.";
}
void TestClickAt(const gfx::Point& location) {
ui::MouseEvent press(
TestLeftMouseAt(location, ui::EventType::kMousePressed));
ui::MouseEvent release(
TestLeftMouseAt(location, ui::EventType::kMouseReleased));
widget_->OnMouseEvent(&press);
widget_->OnMouseEvent(&release);
}
// testing::Test:
void TearDown() override {
widget_->GetCompositor()->RemoveObserver(this);
if (widget_) {
widget_.ExtractAsDangling()->CloseNow();
}
WidgetTest::TearDown();
}
protected:
Widget* widget() const { return widget_; }
private:
// ui::CompositorObserver:
void OnCompositingDidCommit(ui::Compositor* compositor) override {
quit_closure_.Run();
quit_closure_.Reset();
}
raw_ptr<Widget> widget_ = nullptr;
// Disable scrollbar hiding (i.e. disable overlay scrollbars) by default.
bool use_overlay_scrollers_ = false;
base::RepeatingClosure quit_closure_;
#if BUILDFLAG(IS_MAC)
std::unique_ptr<ui::test::ScopedPreferredScrollerStyle> scroller_style_;
#endif
};
constexpr int WidgetScrollViewTest::kDefaultHeight;
constexpr int WidgetScrollViewTest::kDefaultWidth;
// A gtest parameter to permute over whether ScrollView uses a left-to-right or
// right-to-left locale, or whether it uses ui::Layers or View bounds offsets to
// position contents (i.e. ::features::kUiCompositorScrollWithLayers).
enum class UiConfig { kLtr, kLtrWithLayers, kRtl, kRtlWithLayers };
class WidgetScrollViewTestRTLAndLayers
: public WidgetScrollViewTest,
public ::testing::WithParamInterface<UiConfig> {
public:
WidgetScrollViewTestRTLAndLayers() : locale_(IsTestingRtl() ? "he" : "en") {
if (IsTestingLayers()) {
layer_config_.InitAndEnableFeature(
::features::kUiCompositorScrollWithLayers);
} else {
layer_config_.InitAndDisableFeature(
::features::kUiCompositorScrollWithLayers);
}
}
WidgetScrollViewTestRTLAndLayers(const WidgetScrollViewTestRTLAndLayers&) =
delete;
WidgetScrollViewTestRTLAndLayers& operator=(
const WidgetScrollViewTestRTLAndLayers&) = delete;
bool IsTestingRtl() const {
return GetParam() == UiConfig::kRtl ||
GetParam() == UiConfig::kRtlWithLayers;
}
bool IsTestingLayers() const {
return GetParam() == UiConfig::kLtrWithLayers ||
GetParam() == UiConfig::kRtlWithLayers;
}
// Returns a point in the coordinate space of |target| by translating a point
// inset one pixel from the top of the Widget and one pixel on the leading
// side of the Widget. There should be no scroll bar on this side. If
// |flip_result| is true, automatically account for flipped coordinates in
// RTL.
gfx::Point HitTestInCorner(View* target, bool flip_result = true) const {
const gfx::Point test_mouse_point_in_root =
IsTestingRtl() ? gfx::Point(kDefaultWidth - 1, 1) : gfx::Point(1, 1);
gfx::Point point = test_mouse_point_in_root;
View::ConvertPointToTarget(widget()->GetRootView(), target, &point);
if (flip_result) {
return gfx::Point(target->GetMirroredXInView(point.x()), point.y());
}
return point;
}
private:
base::test::ScopedRestoreICUDefaultLocale locale_;
base::test::ScopedFeatureList layer_config_;
};
std::string UiConfigToString(const testing::TestParamInfo<UiConfig>& info) {
switch (info.param) {
case UiConfig::kLtr:
return "LTR";
case UiConfig::kLtrWithLayers:
return "LTR_LAYERS";
case UiConfig::kRtl:
return "RTL";
case UiConfig::kRtlWithLayers:
return "RTL_LAYERS";
}
NOTREACHED();
}
// Verifies the viewport is sized to fit the available space.
TEST_F(ScrollViewTest, ViewportSizedToFit) {
View* contents = InstallContents();
EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString());
}
// Verifies the viewport and content is sized to fit the available space for
// bounded scroll view.
TEST_F(ScrollViewTest, BoundedViewportSizedToFit) {
View* contents = InstallContents();
scroll_view_->ClipHeightTo(100, 200);
scroll_view_->SetBorder(CreateSolidBorder(2, 0));
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_EQ("2,2 96x96", contents->parent()->bounds().ToString());
// Make sure the width of |contents| is set properly not to overflow the
// viewport.
EXPECT_EQ(96, contents->width());
}
// Verifies that the scroll view calls post-layout callback on every layout.
TEST_F(ScrollViewTest, LayoutCallbackCalledOnEveryLayout) {
auto mock_post_layout_cb_ =
base::MockCallback<base::RepeatingCallback<void(ScrollView*)>>();
scroll_view_->RegisterPostLayoutCallback(mock_post_layout_cb_.Get());
EXPECT_CALL(mock_post_layout_cb_, Run(scroll_view_.get())).Times(1);
InstallContents();
ASSERT_FALSE(scroll_view_->GetContentsBounds().IsEmpty());
views::test::RunScheduledLayout(scroll_view_.get());
}
// Verifies that the vertical scrollbar does not unnecessarily appear for a
// contents whose height always matches the height of the viewport.
TEST_F(ScrollViewTest, VerticalScrollbarDoesNotAppearUnnecessarily) {
const gfx::Rect default_outer_bounds(0, 0, 100, 100);
scroll_view_->SetContents(std::make_unique<VerticalResizingView>());
scroll_view_->SetBoundsRect(default_outer_bounds);
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_FALSE(scroll_view_->vertical_scroll_bar()->GetVisible());
EXPECT_TRUE(scroll_view_->horizontal_scroll_bar()->GetVisible());
}
// Same as above, but setting horizontal scroll bar to hidden.
TEST_F(ScrollViewTest, HorizontalScrollbarDoesNotAppearIfHidden) {
const gfx::Rect default_outer_bounds(0, 0, 100, 100);
scroll_view_->SetHorizontalScrollBarMode(
ScrollView::ScrollBarMode::kHiddenButEnabled);
scroll_view_->SetContents(std::make_unique<VerticalResizingView>());
scroll_view_->SetBoundsRect(default_outer_bounds);
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_FALSE(scroll_view_->vertical_scroll_bar()->GetVisible());
EXPECT_FALSE(scroll_view_->horizontal_scroll_bar()->GetVisible());
}
// Same as above, but setting vertical scrollbar instead.
TEST_F(ScrollViewTest, VerticalScrollbarDoesNotAppearIfHidden) {
const gfx::Rect default_outer_bounds(0, 0, 100, 100);
scroll_view_->SetVerticalScrollBarMode(
ScrollView::ScrollBarMode::kHiddenButEnabled);
scroll_view_->SetContents(std::make_unique<HorizontalResizingView>());
scroll_view_->SetBoundsRect(default_outer_bounds);
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_FALSE(scroll_view_->vertical_scroll_bar()->GetVisible());
EXPECT_FALSE(scroll_view_->horizontal_scroll_bar()->GetVisible());
}
// Same as above, but setting horizontal scroll bar to disabled.
TEST_F(ScrollViewTest, HorizontalScrollbarDoesNotAppearIfDisabled) {
const gfx::Rect default_outer_bounds(0, 0, 100, 100);
scroll_view_->SetHorizontalScrollBarMode(
ScrollView::ScrollBarMode::kDisabled);
scroll_view_->SetContents(std::make_unique<VerticalResizingView>());
scroll_view_->SetBoundsRect(default_outer_bounds);
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_FALSE(scroll_view_->vertical_scroll_bar()->GetVisible());
EXPECT_FALSE(scroll_view_->horizontal_scroll_bar()->GetVisible());
}
// Same as above, but setting vertical scrollbar instead.
TEST_F(ScrollViewTest, VerticallScrollbarDoesNotAppearIfDisabled) {
const gfx::Rect default_outer_bounds(0, 0, 100, 100);
scroll_view_->SetVerticalScrollBarMode(ScrollView::ScrollBarMode::kDisabled);
scroll_view_->SetContents(std::make_unique<HorizontalResizingView>());
scroll_view_->SetBoundsRect(default_outer_bounds);
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_FALSE(scroll_view_->vertical_scroll_bar()->GetVisible());
EXPECT_FALSE(scroll_view_->horizontal_scroll_bar()->GetVisible());
}
TEST_F(ScrollViewTest, AccessibleProperties) {
ScrollViewTestApi test_api(scroll_view_.get());
InstallContents();
ui::AXNodeData data;
scroll_view_->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(data.role, ax::mojom::Role::kScrollView);
}
// Verifies the scrollbars are added as necessary.
// If on Mac, test the non-overlay scrollbars.
TEST_F(ScrollViewTest, ScrollBars) {
View* contents = InstallContents();
// Size the contents such that vertical scrollbar is needed.
contents->SetBounds(0, 0, 50, 400);
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutWidth(),
contents->parent()->width());
EXPECT_EQ(100, contents->parent()->height());
CheckScrollbarVisibility(scroll_view_.get(), VERTICAL, true);
CheckScrollbarVisibility(scroll_view_.get(), HORIZONTAL, false);
EXPECT_TRUE(!scroll_view_->horizontal_scroll_bar() ||
!scroll_view_->horizontal_scroll_bar()->GetVisible());
ASSERT_TRUE(scroll_view_->vertical_scroll_bar() != nullptr);
EXPECT_TRUE(scroll_view_->vertical_scroll_bar()->GetVisible());
// Size the contents such that horizontal scrollbar is needed.
contents->SetBounds(0, 0, 400, 50);
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_EQ(100, contents->parent()->width());
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutHeight(),
contents->parent()->height());
CheckScrollbarVisibility(scroll_view_.get(), VERTICAL, false);
CheckScrollbarVisibility(scroll_view_.get(), HORIZONTAL, true);
// Both horizontal and vertical.
contents->SetBounds(0, 0, 300, 400);
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutWidth(),
contents->parent()->width());
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutHeight(),
contents->parent()->height());
CheckScrollbarVisibility(scroll_view_.get(), VERTICAL, true);
CheckScrollbarVisibility(scroll_view_.get(), HORIZONTAL, true);
// Add a border, test vertical scrollbar.
const int kTopPadding = 1;
const int kLeftPadding = 2;
const int kBottomPadding = 3;
const int kRightPadding = 4;
scroll_view_->SetBorder(CreateEmptyBorder(gfx::Insets::TLBR(
kTopPadding, kLeftPadding, kBottomPadding, kRightPadding)));
contents->SetBounds(0, 0, 50, 400);
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutWidth() - kLeftPadding -
kRightPadding,
contents->parent()->width());
EXPECT_EQ(100 - kTopPadding - kBottomPadding, contents->parent()->height());
EXPECT_TRUE(!scroll_view_->horizontal_scroll_bar() ||
!scroll_view_->horizontal_scroll_bar()->GetVisible());
ASSERT_TRUE(scroll_view_->vertical_scroll_bar() != nullptr);
EXPECT_TRUE(scroll_view_->vertical_scroll_bar()->GetVisible());
gfx::Rect bounds = scroll_view_->vertical_scroll_bar()->bounds();
EXPECT_EQ(100 - VerticalScrollBarWidth() - kRightPadding, bounds.x());
EXPECT_EQ(100 - kRightPadding, bounds.right());
EXPECT_EQ(kTopPadding, bounds.y());
EXPECT_EQ(100 - kBottomPadding, bounds.bottom());
// Horizontal with border.
contents->SetBounds(0, 0, 400, 50);
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_EQ(100 - kLeftPadding - kRightPadding, contents->parent()->width());
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutHeight() - kTopPadding -
kBottomPadding,
contents->parent()->height());
ASSERT_TRUE(scroll_view_->horizontal_scroll_bar() != nullptr);
EXPECT_TRUE(scroll_view_->horizontal_scroll_bar()->GetVisible());
EXPECT_TRUE(!scroll_view_->vertical_scroll_bar() ||
!scroll_view_->vertical_scroll_bar()->GetVisible());
bounds = scroll_view_->horizontal_scroll_bar()->bounds();
EXPECT_EQ(kLeftPadding, bounds.x());
EXPECT_EQ(100 - kRightPadding, bounds.right());
EXPECT_EQ(100 - kBottomPadding - HorizontalScrollBarHeight(), bounds.y());
EXPECT_EQ(100 - kBottomPadding, bounds.bottom());
// Both horizontal and vertical with border.
contents->SetBounds(0, 0, 300, 400);
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutWidth() - kLeftPadding -
kRightPadding,
contents->parent()->width());
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutHeight() - kTopPadding -
kBottomPadding,
contents->parent()->height());
bounds = scroll_view_->horizontal_scroll_bar()->bounds();
// Check horiz.
ASSERT_TRUE(scroll_view_->horizontal_scroll_bar() != nullptr);
EXPECT_TRUE(scroll_view_->horizontal_scroll_bar()->GetVisible());
bounds = scroll_view_->horizontal_scroll_bar()->bounds();
EXPECT_EQ(kLeftPadding, bounds.x());
EXPECT_EQ(100 - kRightPadding - VerticalScrollBarWidth(), bounds.right());
EXPECT_EQ(100 - kBottomPadding - HorizontalScrollBarHeight(), bounds.y());
EXPECT_EQ(100 - kBottomPadding, bounds.bottom());
// Check vert.
ASSERT_TRUE(scroll_view_->vertical_scroll_bar() != nullptr);
EXPECT_TRUE(scroll_view_->vertical_scroll_bar()->GetVisible());
bounds = scroll_view_->vertical_scroll_bar()->bounds();
EXPECT_EQ(100 - VerticalScrollBarWidth() - kRightPadding, bounds.x());
EXPECT_EQ(100 - kRightPadding, bounds.right());
EXPECT_EQ(kTopPadding, bounds.y());
EXPECT_EQ(100 - kBottomPadding - HorizontalScrollBarHeight(),
bounds.bottom());
}
// Tests that after scrolling the child (which was gained the focus) into the
// viewport of the scroll view, the tooltip should be shown up.
TEST_F(WidgetScrollViewTest, ScrollChildToVisibleOnFocusWithTooltip) {
// Create a scroll view and its contents view.
auto contents_ptr = std::make_unique<CustomView>();
contents_ptr->SetPreferredSize(gfx::Size(100, 200));
auto* contents = contents_ptr.get();
AddScrollViewWithContents(std::move(contents_ptr));
// Create a child view into the contents view, and set it position outside of
// the visible rect.
auto child = std::make_unique<FixedView>();
child->SetPreferredSize(gfx::Size(10, 10));
child->SetPosition(gfx::Point(50, 110));
auto* child_ptr = contents->AddChildView(std::move(child));
// Bring a focus to this child view which should scroll it into the visible
// rect of the scroll view.
child_ptr->SetFocus();
ASSERT_TRUE(child_ptr->did_show_tooltip_in_viewport().has_value());
EXPECT_TRUE(child_ptr->did_show_tooltip_in_viewport().value());
}
// Assertions around adding a header.
TEST_F(WidgetScrollViewTest, Header) {
auto contents_ptr = std::make_unique<View>();
auto* contents = contents_ptr.get();
ScrollView* scroll_view = AddScrollViewWithContents(std::move(contents_ptr));
auto* header = scroll_view->SetHeader(std::make_unique<CustomView>());
View* header_parent = header->parent();
views::test::RunScheduledLayout(widget());
// |header|s preferred size is empty, which should result in all space going
// to contents.
EXPECT_EQ("0,0 100x0", header->parent()->bounds().ToString());
EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString());
// With layered scrolling, ScrollView::Layout() will impose a size on the
// contents that fills the viewport. Since the test view doesn't have its own
// Layout, reset it in this case so that adding a header doesn't shift the
// contents down and require scrollbars.
if (contents->layer()) {
EXPECT_EQ("0,0 100x100", contents->bounds().ToString());
contents->SetBoundsRect(gfx::Rect());
}
EXPECT_EQ("0,0 0x0", contents->bounds().ToString());
// Get the header a height of 20.
header->SetPreferredSize(gfx::Size(10, 20));
EXPECT_TRUE(ViewTestApi(scroll_view).needs_layout());
views::test::RunScheduledLayout(widget());
EXPECT_EQ("0,0 100x20", header->parent()->bounds().ToString());
EXPECT_EQ("0,20 100x80", contents->parent()->bounds().ToString());
if (contents->layer()) {
EXPECT_EQ("0,0 100x80", contents->bounds().ToString());
contents->SetBoundsRect(gfx::Rect());
}
EXPECT_EQ("0,0 0x0", contents->bounds().ToString());
// Remove the header.
scroll_view->SetHeader(nullptr);
// SetHeader(nullptr) deletes header.
header = nullptr;
views::test::RunScheduledLayout(widget());
EXPECT_EQ("0,0 100x0", header_parent->bounds().ToString());
EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString());
}
// Verifies the scrollbars are added as necessary when a header is present.
TEST_F(ScrollViewTest, ScrollBarsWithHeader) {
auto* header = scroll_view_->SetHeader(std::make_unique<CustomView>());
View* contents = InstallContents();
header->SetPreferredSize(gfx::Size(10, 20));
// Size the contents such that vertical scrollbar is needed.
contents->SetBounds(0, 0, 50, 400);
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_EQ(0, contents->parent()->x());
EXPECT_EQ(20, contents->parent()->y());
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutWidth(),
contents->parent()->width());
EXPECT_EQ(80, contents->parent()->height());
EXPECT_EQ(0, header->parent()->x());
EXPECT_EQ(0, header->parent()->y());
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutWidth(),
header->parent()->width());
EXPECT_EQ(20, header->parent()->height());
EXPECT_TRUE(!scroll_view_->horizontal_scroll_bar() ||
!scroll_view_->horizontal_scroll_bar()->GetVisible());
ASSERT_TRUE(scroll_view_->vertical_scroll_bar() != nullptr);
EXPECT_TRUE(scroll_view_->vertical_scroll_bar()->GetVisible());
// Make sure the vertical scrollbar overlaps the header for traditional
// scrollbars and doesn't overlap the header for overlay scrollbars.
const int expected_scrollbar_y =
scroll_view_->vertical_scroll_bar()->OverlapsContent()
? header->bounds().bottom()
: header->y();
EXPECT_EQ(expected_scrollbar_y, scroll_view_->vertical_scroll_bar()->y());
EXPECT_EQ(header->y(), contents->y());
// Size the contents such that horizontal scrollbar is needed.
contents->SetBounds(0, 0, 400, 50);
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_EQ(0, contents->parent()->x());
EXPECT_EQ(20, contents->parent()->y());
EXPECT_EQ(100, contents->parent()->width());
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutHeight() - 20,
contents->parent()->height());
EXPECT_EQ(0, header->parent()->x());
EXPECT_EQ(0, header->parent()->y());
EXPECT_EQ(100, header->parent()->width());
EXPECT_EQ(20, header->parent()->height());
ASSERT_TRUE(scroll_view_->horizontal_scroll_bar() != nullptr);
EXPECT_TRUE(scroll_view_->horizontal_scroll_bar()->GetVisible());
EXPECT_TRUE(!scroll_view_->vertical_scroll_bar() ||
!scroll_view_->vertical_scroll_bar()->GetVisible());
// Both horizontal and vertical.
contents->SetBounds(0, 0, 300, 400);
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_EQ(0, contents->parent()->x());
EXPECT_EQ(20, contents->parent()->y());
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutWidth(),
contents->parent()->width());
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutHeight() - 20,
contents->parent()->height());
EXPECT_EQ(0, header->parent()->x());
EXPECT_EQ(0, header->parent()->y());
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutWidth(),
header->parent()->width());
EXPECT_EQ(20, header->parent()->height());
ASSERT_TRUE(scroll_view_->horizontal_scroll_bar() != nullptr);
EXPECT_TRUE(scroll_view_->horizontal_scroll_bar()->GetVisible());
ASSERT_TRUE(scroll_view_->vertical_scroll_bar() != nullptr);
EXPECT_TRUE(scroll_view_->vertical_scroll_bar()->GetVisible());
}
// Verifies the header scrolls horizontally with the content.
TEST_F(ScrollViewTest, HeaderScrollsWithContent) {
ScrollViewTestApi test_api(scroll_view_.get());
auto contents = std::make_unique<CustomView>();
contents->SetPreferredSize(gfx::Size(500, 500));
scroll_view_->SetContents(std::move(contents));
auto* header = scroll_view_->SetHeader(std::make_unique<CustomView>());
header->SetPreferredSize(gfx::Size(500, 20));
scroll_view_->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
EXPECT_EQ(gfx::Vector2d(0, 0), test_api.IntegralViewOffset());
EXPECT_EQ(gfx::Point(0, 0), header->origin());
// Scroll the horizontal scrollbar.
ASSERT_TRUE(scroll_view_->horizontal_scroll_bar());
scroll_view_->ScrollToPosition(test_api.GetScrollBar(HORIZONTAL), 1);
EXPECT_EQ(gfx::Vector2d(-1, 0), test_api.IntegralViewOffset());
EXPECT_EQ(gfx::Point(-1, 0), header->origin());
// Scrolling the vertical scrollbar shouldn't effect the header.
ASSERT_TRUE(scroll_view_->vertical_scroll_bar());
scroll_view_->ScrollToPosition(test_api.GetScrollBar(VERTICAL), 1);
EXPECT_EQ(gfx::Vector2d(-1, -1), test_api.IntegralViewOffset());
EXPECT_EQ(gfx::Point(-1, 0), header->origin());
}
// Test that calling ScrollToPosition() also updates the position of the
// corresponding ScrollBar.
TEST_F(ScrollViewTest, ScrollToPositionUpdatesScrollBar) {
ScrollViewTestApi test_api(scroll_view_.get());
View* contents = InstallContents();
// Scroll the horizontal scrollbar, after which, the scroll bar thumb position
// should be updated (i.e. it should be non-zero).
contents->SetBounds(0, 0, 400, 50);
InvalidateAndRunScheduledLayoutOnScrollView();
auto* scroll_bar = test_api.GetScrollBar(HORIZONTAL);
ASSERT_TRUE(scroll_bar);
EXPECT_TRUE(scroll_bar->GetVisible());
EXPECT_EQ(0, scroll_bar->GetPosition());
scroll_view_->ScrollToPosition(scroll_bar, 20);
EXPECT_GT(scroll_bar->GetPosition(), 0);
// Scroll the vertical scrollbar.
contents->SetBounds(0, 0, 50, 400);
InvalidateAndRunScheduledLayoutOnScrollView();
scroll_bar = test_api.GetScrollBar(VERTICAL);
ASSERT_TRUE(scroll_bar);
EXPECT_TRUE(scroll_bar->GetVisible());
EXPECT_EQ(0, scroll_bar->GetPosition());
scroll_view_->ScrollToPosition(scroll_bar, 20);
EXPECT_GT(scroll_bar->GetPosition(), 0);
}
TEST_F(ScrollViewTest, HorizontalScrollBarAccessibleScrollXProperties) {
ScrollViewTestApi test_api(scroll_view_.get());
View* contents = InstallContents();
contents->SetBounds(0, 0, 500, 20);
InvalidateAndRunScheduledLayoutOnScrollView();
auto* scroll_bar = test_api.GetScrollBar(HORIZONTAL);
ASSERT_TRUE(scroll_bar);
// Verify kScrollXMin and kScrollXMax with initial content size.
ui::AXNodeData scroll_view_node_data;
scroll_view_node_data = ui::AXNodeData();
scroll_view_->GetViewAccessibility().GetAccessibleNodeData(
&scroll_view_node_data);
EXPECT_TRUE(scroll_view_node_data.GetBoolAttribute(
ax::mojom::BoolAttribute::kScrollable));
EXPECT_EQ(scroll_bar->GetMinPosition(),
scroll_view_node_data.GetIntAttribute(
ax::mojom::IntAttribute::kScrollXMin));
EXPECT_EQ(scroll_bar->GetMaxPosition(),
scroll_view_node_data.GetIntAttribute(
ax::mojom::IntAttribute::kScrollXMax));
// Set the content width to 400 after which kScrollXMax should be updated
// and kScrollXMin remains 0 as GetMinPosition() always returns 0.
contents->SetBounds(0, 0, 400, 50);
InvalidateAndRunScheduledLayoutOnScrollView();
scroll_view_node_data = ui::AXNodeData();
scroll_view_->GetViewAccessibility().GetAccessibleNodeData(
&scroll_view_node_data);
EXPECT_EQ(0, scroll_view_node_data.GetIntAttribute(
ax::mojom::IntAttribute::kScrollXMin));
EXPECT_EQ(400 - test_api.contents_viewport()->width(),
scroll_view_node_data.GetIntAttribute(
ax::mojom::IntAttribute::kScrollXMax));
// Scroll the horizontal scrollbar after which kScrollX should be updated.
EXPECT_EQ(0, scroll_view_node_data.GetIntAttribute(
ax::mojom::IntAttribute::kScrollX));
scroll_view_->ScrollToPosition(scroll_bar, 20);
scroll_view_node_data = ui::AXNodeData();
scroll_view_->GetViewAccessibility().GetAccessibleNodeData(
&scroll_view_node_data);
EXPECT_EQ(
test_api.CurrentOffset().x(),
scroll_view_node_data.GetIntAttribute(ax::mojom::IntAttribute::kScrollX));
}
TEST_F(ScrollViewTest, VerticalScrollBarAccessibleScrollYProperties) {
ScrollViewTestApi test_api(scroll_view_.get());
View* contents = InstallContents();
contents->SetBounds(0, 0, 50, 500);
InvalidateAndRunScheduledLayoutOnScrollView();
auto* scroll_bar = test_api.GetScrollBar(VERTICAL);
ASSERT_TRUE(scroll_bar);
// Verify kScrollYMin and kScrollYMax with initial content size.
ui::AXNodeData scroll_view_node_data;
scroll_view_node_data = ui::AXNodeData();
scroll_view_->GetViewAccessibility().GetAccessibleNodeData(
&scroll_view_node_data);
EXPECT_TRUE(scroll_view_node_data.GetBoolAttribute(
ax::mojom::BoolAttribute::kScrollable));
EXPECT_EQ(scroll_bar->GetMinPosition(),
scroll_view_node_data.GetIntAttribute(
ax::mojom::IntAttribute::kScrollYMin));
EXPECT_EQ(scroll_bar->GetMaxPosition(),
scroll_view_node_data.GetIntAttribute(
ax::mojom::IntAttribute::kScrollYMax));
// Set the content width to 400 after which kScrollYMax should be updated
// and kScrollYMin remains 0 as GetMinPosition() always returns 0.
contents->SetBounds(0, 0, 50, 400);
InvalidateAndRunScheduledLayoutOnScrollView();
scroll_view_node_data = ui::AXNodeData();
scroll_view_->GetViewAccessibility().GetAccessibleNodeData(
&scroll_view_node_data);
EXPECT_EQ(0, scroll_view_node_data.GetIntAttribute(
ax::mojom::IntAttribute::kScrollYMin));
EXPECT_EQ(400 - test_api.contents_viewport()->height(),
scroll_view_node_data.GetIntAttribute(
ax::mojom::IntAttribute::kScrollYMax));
// Scroll the vertical scrollbar after which kScrollY should be updated.
EXPECT_EQ(0, scroll_view_node_data.GetIntAttribute(
ax::mojom::IntAttribute::kScrollY));
scroll_view_->ScrollToPosition(scroll_bar, 20);
scroll_view_node_data = ui::AXNodeData();
scroll_view_->GetViewAccessibility().GetAccessibleNodeData(
&scroll_view_node_data);
EXPECT_EQ(
test_api.CurrentOffset().y(),
scroll_view_node_data.GetIntAttribute(ax::mojom::IntAttribute::kScrollY));
}
// Test that calling ScrollToPosition() also updates the position of the
// child view even when the horizontal scrollbar is hidden.
TEST_F(ScrollViewTest, ScrollToPositionUpdatesWithHiddenHorizontalScrollBar) {
scroll_view_->SetHorizontalScrollBarMode(
ScrollView::ScrollBarMode::kHiddenButEnabled);
ScrollViewTestApi test_api(scroll_view_.get());
View* contents = InstallContents();
contents->SetBounds(0, 0, 400, 50);
InvalidateAndRunScheduledLayoutOnScrollView();
auto* scroll_bar = test_api.GetScrollBar(HORIZONTAL);
ASSERT_TRUE(scroll_bar);
EXPECT_FALSE(scroll_bar->GetVisible());
// We can't rely on the scrollbar, which may not be updated as it's not
// visible, but we can check the scroll offset itself.
EXPECT_EQ(0, test_api.CurrentOffset().x());
scroll_view_->ScrollToPosition(scroll_bar, 20);
EXPECT_GT(test_api.CurrentOffset().x(), 0);
}
// Test that calling ScrollToPosition() also updates the position of the
// child view even when the horizontal scrollbar is hidden.
TEST_F(ScrollViewTest, ScrollToPositionUpdatesWithHiddenVerticalScrollBar) {
scroll_view_->SetVerticalScrollBarMode(
ScrollView::ScrollBarMode::kHiddenButEnabled);
ScrollViewTestApi test_api(scroll_view_.get());
View* contents = InstallContents();
contents->SetBounds(0, 0, 50, 400);
InvalidateAndRunScheduledLayoutOnScrollView();
auto* scroll_bar = test_api.GetScrollBar(VERTICAL);
ASSERT_TRUE(scroll_bar);
EXPECT_FALSE(scroll_bar->GetVisible());
// We can't rely on the scrollbar, which may not be updated as it's not
// visible, but we can check the scroll offset itself.
EXPECT_EQ(0, test_api.CurrentOffset().y());
scroll_view_->ScrollToPosition(scroll_bar, 20);
EXPECT_GT(test_api.CurrentOffset().y(), 0);
}
// Verifies ScrollRectToVisible() on the child works.
TEST_F(ScrollViewTest, ScrollRectToVisible) {
ScrollViewTestApi test_api(scroll_view_.get());
auto contents = std::make_unique<CustomView>();
contents->SetPreferredSize(gfx::Size(500, 1000));
auto* contents_ptr = scroll_view_->SetContents(std::move(contents));
scroll_view_->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_EQ(gfx::Vector2d(0, 0), test_api.IntegralViewOffset());
// Scroll to y=405 height=10, this should make the y position of the content
// at (405 + 10) - viewport_height (scroll region bottom aligned).
contents_ptr->ScrollRectToVisible(gfx::Rect(0, 405, 10, 10));
const int viewport_height = test_api.contents_viewport()->height();
// Expect there to be a horizontal scrollbar, making the viewport shorter.
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutHeight(), viewport_height);
gfx::PointF offset = test_api.CurrentOffset();
EXPECT_EQ(415 - viewport_height, offset.y());
// Scroll to the current y-location and 10x10; should do nothing.
contents_ptr->ScrollRectToVisible(gfx::Rect(0, offset.y(), 10, 10));
EXPECT_EQ(415 - viewport_height, test_api.CurrentOffset().y());
}
// Verifies ScrollByOffset() method works as expected
TEST_F(ScrollViewTest, ScrollByOffset) {
// setup
ScrollViewTestApi test_api(scroll_view_.get());
auto contents = std::make_unique<CustomView>();
contents->SetPreferredSize(gfx::Size(500, 1000));
scroll_view_->SetContents(std::move(contents));
scroll_view_->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_EQ(gfx::Vector2d(0, 0), test_api.IntegralViewOffset());
// scroll by an offset of x=5 and y=5
scroll_view_->ScrollByOffset(gfx::PointF(5, 5));
EXPECT_EQ(test_api.CurrentOffset().x(), 5);
EXPECT_EQ(test_api.CurrentOffset().y(), 5);
// scroll back to the initial position
scroll_view_->ScrollByOffset(gfx::PointF(-5, -5));
EXPECT_EQ(test_api.CurrentOffset().x(), 0);
EXPECT_EQ(test_api.CurrentOffset().y(), 0);
}
// Verifies ScrollRectToVisible() scrolls the view horizontally even if the
// horizontal scrollbar is hidden (but not disabled).
TEST_F(ScrollViewTest, ScrollRectToVisibleWithHiddenHorizontalScrollbar) {
scroll_view_->SetHorizontalScrollBarMode(
ScrollView::ScrollBarMode::kHiddenButEnabled);
ScrollViewTestApi test_api(scroll_view_.get());
auto contents = std::make_unique<CustomView>();
contents->SetPreferredSize(gfx::Size(500, 1000));
auto* contents_ptr = scroll_view_->SetContents(std::move(contents));
scroll_view_->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_EQ(gfx::Vector2d(0, 0), test_api.IntegralViewOffset());
// Scroll to x=305 width=10, this should make the x position of the content
// at (305 + 10) - viewport_width (scroll region right aligned).
contents_ptr->ScrollRectToVisible(gfx::Rect(305, 0, 10, 10));
const int viewport_width = test_api.contents_viewport()->width();
// Expect there to be a vertical scrollbar, making the viewport shorter.
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutWidth(), viewport_width);
gfx::PointF offset = test_api.CurrentOffset();
EXPECT_EQ(315 - viewport_width, offset.x());
// Scroll to the current x-location and 10x10; should do nothing.
contents_ptr->ScrollRectToVisible(gfx::Rect(offset.x(), 0, 10, 10));
EXPECT_EQ(315 - viewport_width, test_api.CurrentOffset().x());
}
// Verifies ScrollRectToVisible() scrolls the view vertically even if the
// vertical scrollbar is hidden (but not disabled).
TEST_F(ScrollViewTest, ScrollRectToVisibleWithHiddenVerticalScrollbar) {
scroll_view_->SetVerticalScrollBarMode(
ScrollView::ScrollBarMode::kHiddenButEnabled);
ScrollViewTestApi test_api(scroll_view_.get());
auto contents = std::make_unique<CustomView>();
contents->SetPreferredSize(gfx::Size(1000, 500));
auto* contents_ptr = scroll_view_->SetContents(std::move(contents));
scroll_view_->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_EQ(gfx::Vector2d(0, 0), test_api.IntegralViewOffset());
// Scroll to y=305 height=10, this should make the y position of the content
// at (305 + 10) - viewport_height (scroll region bottom aligned).
contents_ptr->ScrollRectToVisible(gfx::Rect(0, 305, 10, 10));
const int viewport_height = test_api.contents_viewport()->height();
// Expect there to be a vertical scrollbar, making the viewport shorter.
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutHeight(), viewport_height);
gfx::PointF offset = test_api.CurrentOffset();
EXPECT_EQ(315 - viewport_height, offset.y());
// Scroll to the current y-location and 10x10; should do nothing.
contents_ptr->ScrollRectToVisible(gfx::Rect(0, offset.y(), 10, 10));
EXPECT_EQ(315 - viewport_height, test_api.CurrentOffset().y());
}
// Verifies ScrollRectToVisible() does not scroll the view horizontally or
// vertically if the scrollbars are disabled.
TEST_F(ScrollViewTest, ScrollRectToVisibleWithDisabledScrollbars) {
scroll_view_->SetHorizontalScrollBarMode(
ScrollView::ScrollBarMode::kDisabled);
scroll_view_->SetVerticalScrollBarMode(ScrollView::ScrollBarMode::kDisabled);
ScrollViewTestApi test_api(scroll_view_.get());
auto contents = std::make_unique<CustomView>();
contents->SetPreferredSize(gfx::Size(500, 1000));
auto* contents_ptr = scroll_view_->SetContents(std::move(contents));
scroll_view_->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_EQ(gfx::Vector2d(0, 0), test_api.IntegralViewOffset());
contents_ptr->ScrollRectToVisible(gfx::Rect(305, 0, 10, 10));
EXPECT_EQ(0, test_api.CurrentOffset().x());
contents_ptr->ScrollRectToVisible(gfx::Rect(0, 305, 10, 10));
EXPECT_EQ(0, test_api.CurrentOffset().y());
}
// Verifies that child scrolls into view when it's focused.
TEST_F(ScrollViewTest, ScrollChildToVisibleOnFocus) {
ScrollViewTestApi test_api(scroll_view_.get());
auto contents = std::make_unique<CustomView>();
contents->SetPreferredSize(gfx::Size(500, 1000));
auto* contents_ptr = scroll_view_->SetContents(std::move(contents));
auto child = std::make_unique<FixedView>();
child->SetPreferredSize(gfx::Size(10, 10));
child->SetPosition(gfx::Point(0, 405));
auto* child_ptr = contents_ptr->AddChildView(std::move(child));
scroll_view_->SetBoundsRect(gfx::Rect(0, 0, 100, 100));
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_EQ(gfx::Vector2d(), test_api.IntegralViewOffset());
// Set focus to the child control. This should cause the control to scroll to
// y=405 height=10. Like the above test, this should make the y position of
// the content at (405 + 10) - viewport_height (scroll region bottom aligned).
child_ptr->SetFocus();
const int viewport_height = test_api.contents_viewport()->height();
// Expect there to be a horizontal scrollbar, making the viewport shorter.
EXPECT_EQ(100 - scroll_view_->GetScrollBarLayoutHeight(), viewport_height);
gfx::PointF offset = test_api.CurrentOffset();
EXPECT_EQ(415 - viewport_height, offset.y());
}
// Verifies that ScrollView scrolls into view when its contents root is focused.
TEST_F(ScrollViewTest, ScrollViewToVisibleOnContentsRootFocus) {
ScrollViewTestApi outer_test_api(scroll_view_.get());
auto outer_contents = std::make_unique<CustomView>();
outer_contents->SetPreferredSize(gfx::Size(500, 1000));
auto* outer_contents_ptr =
scroll_view_->SetContents(std::move(outer_contents));
auto inner_scroll_view = std::make_unique<ScrollView>();
auto* inner_scroll_view_ptr =
outer_contents_ptr->AddChildView(std::move(inner_scroll_view));
ScrollViewTestApi inner_test_api(inner_scroll_view_ptr);
auto inner_contents = std::make_unique<FixedView>();
inner_contents->SetPreferredSize(gfx::Size(500, 1000));
auto* inner_contents_ptr =
inner_scroll_view_ptr->SetContents(std::move(inner_contents));
inner_scroll_view_ptr->SetBoundsRect(gfx::Rect(0, 510, 100, 100));
views::test::RunScheduledLayout(inner_scroll_view_ptr);
EXPECT_EQ(gfx::Vector2d(), inner_test_api.IntegralViewOffset());
scroll_view_->SetBoundsRect(gfx::Rect(0, 0, 200, 200));
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_EQ(gfx::Vector2d(), outer_test_api.IntegralViewOffset());
// Scroll the inner scroll view to y=405 height=10. This should make the y
// position of the inner content at (405 + 10) - inner_viewport_height
// (scroll region bottom aligned). The outer scroll view should not scroll.
inner_contents_ptr->ScrollRectToVisible(gfx::Rect(0, 405, 10, 10));
const int inner_viewport_height =
inner_test_api.contents_viewport()->height();
gfx::PointF inner_offset = inner_test_api.CurrentOffset();
EXPECT_EQ(415 - inner_viewport_height, inner_offset.y());
gfx::PointF outer_offset = outer_test_api.CurrentOffset();
EXPECT_EQ(0, outer_offset.y());
// Set focus to the inner scroll view's contents root. This should cause the
// outer scroll view to scroll to y=510 height=100 so that the y position of
// the outer content is at (510 + 100) - outer_viewport_height (scroll region
// bottom aligned). The inner scroll view should not scroll.
inner_contents_ptr->SetFocus();
const int outer_viewport_height =
outer_test_api.contents_viewport()->height();
inner_offset = inner_test_api.CurrentOffset();
EXPECT_EQ(415 - inner_viewport_height, inner_offset.y());
outer_offset = outer_test_api.CurrentOffset();
EXPECT_EQ(610 - outer_viewport_height, outer_offset.y());
}
// Verifies ClipHeightTo() uses the height of the content when it is between the
// minimum and maximum height values.
TEST_F(ScrollViewTest, ClipHeightToNormalContentHeight) {
scroll_view_->ClipHeightTo(kMinHeight, kMaxHeight);
const int kNormalContentHeight = 75;
scroll_view_->SetContents(std::make_unique<views::StaticSizedView>(
gfx::Size(kWidth, kNormalContentHeight)));
EXPECT_EQ(gfx::Size(kWidth, kNormalContentHeight),
scroll_view_->GetPreferredSize({}));
scroll_view_->SizeToPreferredSize();
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_EQ(gfx::Size(kWidth, kNormalContentHeight),
scroll_view_->contents()->size());
EXPECT_EQ(gfx::Size(kWidth, kNormalContentHeight), scroll_view_->size());
}
// Verifies ClipHeightTo() uses the minimum height when the content is shorter
// than the minimum height value.
TEST_F(ScrollViewTest, ClipHeightToShortContentHeight) {
scroll_view_->ClipHeightTo(kMinHeight, kMaxHeight);
const int kShortContentHeight = 10;
View* contents =
scroll_view_->SetContents(std::make_unique<views::StaticSizedView>(
gfx::Size(kWidth, kShortContentHeight)));
EXPECT_EQ(gfx::Size(kWidth, kMinHeight), scroll_view_->GetPreferredSize({}));
scroll_view_->SizeToPreferredSize();
views::test::RunScheduledLayout(scroll_view_.get());
// Layered scrolling requires the contents to fill the viewport.
if (contents->layer()) {
EXPECT_EQ(gfx::Size(kWidth, kMinHeight), scroll_view_->contents()->size());
} else {
EXPECT_EQ(gfx::Size(kWidth, kShortContentHeight),
scroll_view_->contents()->size());
}
EXPECT_EQ(gfx::Size(kWidth, kMinHeight), scroll_view_->size());
}
// Verifies ClipHeightTo() uses the maximum height when the content is longer
// thamn the maximum height value.
TEST_F(ScrollViewTest, ClipHeightToTallContentHeight) {
scroll_view_->ClipHeightTo(kMinHeight, kMaxHeight);
const int kTallContentHeight = 1000;
scroll_view_->SetContents(std::make_unique<views::StaticSizedView>(
gfx::Size(kWidth, kTallContentHeight)));
EXPECT_EQ(gfx::Size(kWidth, kMaxHeight), scroll_view_->GetPreferredSize({}));
scroll_view_->SizeToPreferredSize();
views::test::RunScheduledLayout(scroll_view_.get());
// The width may be less than kWidth if the scroll bar takes up some width.
EXPECT_GE(kWidth, scroll_view_->contents()->width());
EXPECT_EQ(kTallContentHeight, scroll_view_->contents()->height());
EXPECT_EQ(gfx::Size(kWidth, kMaxHeight), scroll_view_->size());
}
// Verifies that when ClipHeightTo() produces a scrollbar, it reduces the width
// of the inner content of the ScrollView.
TEST_F(ScrollViewTest, ClipHeightToScrollbarUsesWidth) {
scroll_view_->ClipHeightTo(kMinHeight, kMaxHeight);
// Create a view that will be much taller than it is wide.
scroll_view_->SetContents(
std::make_unique<views::ProportionallySizedView>(1000));
// Without any width, it will default to 0,0 but be overridden by min height.
scroll_view_->SizeToPreferredSize();
EXPECT_EQ(gfx::Size(0, kMinHeight), scroll_view_->GetPreferredSize({}));
gfx::Size new_size(kWidth, scroll_view_->GetHeightForWidth(kWidth));
scroll_view_->SetSize(new_size);
views::test::RunScheduledLayout(scroll_view_.get());
int expected_width = kWidth - scroll_view_->GetScrollBarLayoutWidth();
EXPECT_EQ(scroll_view_->contents()->size().width(), expected_width);
EXPECT_EQ(scroll_view_->contents()->size().height(), 1000 * expected_width);
EXPECT_EQ(gfx::Size(kWidth, kMaxHeight), scroll_view_->size());
}
// Verifies ClipHeightTo() updates the ScrollView's preferred size.
TEST_F(ScrollViewTest, ClipHeightToUpdatesPreferredSize) {
auto contents_view = std::make_unique<View>();
contents_view->SetPreferredSize(gfx::Size(100, 100));
scroll_view_->SetContents(std::move(contents_view));
EXPECT_FALSE(scroll_view_->is_bounded());
constexpr int kMinHeight1 = 20;
constexpr int kMaxHeight1 = 80;
scroll_view_->ClipHeightTo(kMinHeight1, kMaxHeight1);
EXPECT_TRUE(scroll_view_->is_bounded());
EXPECT_EQ(scroll_view_->GetPreferredSize({}).height(), kMaxHeight1);
constexpr int kMinHeight2 = 200;
constexpr int kMaxHeight2 = 300;
scroll_view_->ClipHeightTo(kMinHeight2, kMaxHeight2);
EXPECT_EQ(scroll_view_->GetPreferredSize({}).height(), kMinHeight2);
}
TEST_F(ScrollViewTest, CornerViewVisibility) {
View* contents = InstallContents();
View* corner_view = ScrollViewTestApi(scroll_view_.get()).corner_view();
contents->SetBounds(0, 0, 200, 200);
InvalidateAndRunScheduledLayoutOnScrollView();
// Corner view should not exist if using overlay scrollbars.
if (scroll_view_->vertical_scroll_bar()->OverlapsContent()) {
EXPECT_FALSE(corner_view->parent());
return;
}
// Corner view should be visible when both scrollbars are visible.
EXPECT_EQ(scroll_view_.get(), corner_view->parent());
EXPECT_TRUE(corner_view->GetVisible());
// Corner view should be aligned to the scrollbars.
EXPECT_EQ(scroll_view_->vertical_scroll_bar()->x(), corner_view->x());
EXPECT_EQ(scroll_view_->horizontal_scroll_bar()->y(), corner_view->y());
EXPECT_EQ(scroll_view_->GetScrollBarLayoutWidth(), corner_view->width());
EXPECT_EQ(scroll_view_->GetScrollBarLayoutHeight(), corner_view->height());
// Corner view should be removed when only the vertical scrollbar is visible.
contents->SetBounds(0, 0, 50, 200);
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_FALSE(corner_view->parent());
// ... or when only the horizontal scrollbar is visible.
contents->SetBounds(0, 0, 200, 50);
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_FALSE(corner_view->parent());
// ... or when no scrollbar is visible.
contents->SetBounds(0, 0, 50, 50);
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_FALSE(corner_view->parent());
// Corner view should reappear when both scrollbars reappear.
contents->SetBounds(0, 0, 200, 200);
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_EQ(scroll_view_.get(), corner_view->parent());
EXPECT_TRUE(corner_view->GetVisible());
}
// This test needs a widget so that color changes will be reflected.
TEST_F(WidgetScrollViewTest, ChildWithLayerTest) {
auto contents_ptr = std::make_unique<View>();
auto* contents = contents_ptr.get();
ScrollView* scroll_view = AddScrollViewWithContents(std::move(contents_ptr));
ScrollViewTestApi test_api(scroll_view);
if (test_api.contents_viewport()->layer()) {
return;
}
View* child = contents->AddChildView(std::make_unique<View>());
child->SetPaintToLayer(ui::LAYER_TEXTURED);
ASSERT_TRUE(test_api.contents_viewport()->layer());
// The default ScrollView color is opaque, so that fills bounds opaquely
// should be true.
EXPECT_TRUE(test_api.contents_viewport()->layer()->fills_bounds_opaquely());
// Setting a std::nullopt color should make fills opaquely false.
scroll_view->SetBackgroundColor(std::nullopt);
EXPECT_FALSE(test_api.contents_viewport()->layer()->fills_bounds_opaquely());
child->DestroyLayer();
EXPECT_FALSE(test_api.contents_viewport()->layer());
child->AddChildView(std::make_unique<View>());
EXPECT_FALSE(test_api.contents_viewport()->layer());
child->SetPaintToLayer(ui::LAYER_TEXTURED);
EXPECT_TRUE(test_api.contents_viewport()->layer());
}
// Validates that if a child of a ScrollView adds a layer, then a layer
// is not added to the ScrollView's viewport.
TEST_F(ScrollViewTest, DontCreateLayerOnViewportIfLayerOnScrollViewCreated) {
View* contents = InstallContents();
ScrollViewTestApi test_api(scroll_view_.get());
if (test_api.contents_viewport()->layer()) {
return;
}
scroll_view_->SetPaintToLayer();
View* child = contents->AddChildView(std::make_unique<View>());
child->SetPaintToLayer(ui::LAYER_TEXTURED);
EXPECT_FALSE(test_api.contents_viewport()->layer());
}
// Validates if the contents_viewport uses correct layer type when adding views
// with different types of layers.
TEST_F(ScrollViewTest, ContentsViewportLayerUsed_ScrollWithLayersDisabled) {
// Disabling scroll_with_layers feature explicitly.
ScrollView scroll_view(ScrollView::ScrollWithLayers::kDisabled);
ScrollViewTestApi test_api(&scroll_view);
View* contents = scroll_view.SetContents(std::make_unique<View>());
ASSERT_FALSE(test_api.contents_viewport()->layer());
View* child = contents->AddChildView(std::make_unique<View>());
child->SetPaintToLayer();
// When contents does not have a layer, contents_viewport is TEXTURED layer.
EXPECT_EQ(test_api.contents_viewport()->layer()->type(), ui::LAYER_TEXTURED);
contents->SetPaintToLayer();
// When contents is a TEXTURED layer.
EXPECT_EQ(test_api.contents_viewport()->layer()->type(), ui::LAYER_NOT_DRAWN);
contents->SetPaintToLayer(ui::LAYER_NOT_DRAWN);
// When contents is a NOT_DRAWN layer.
EXPECT_EQ(test_api.contents_viewport()->layer()->type(), ui::LAYER_TEXTURED);
}
// Validates the layer of contents_viewport_, when contents_ does not have a
// layer.
TEST_F(
ScrollViewTest,
ContentsViewportLayerWhenContentsDoesNotHaveLayer_ScrollWithLayersDisabled) {
// Disabling scroll_with_layers feature explicitly.
ScrollView scroll_view(ScrollView::ScrollWithLayers::kDisabled);
ScrollViewTestApi test_api(&scroll_view);
auto contents = std::make_unique<View>();
View* child = contents->AddChildView(std::make_unique<View>());
contents->AddChildView(std::make_unique<View>());
scroll_view.SetContents(std::move(contents));
// No layer needed for contents_viewport since no descendant view has a layer.
EXPECT_FALSE(test_api.contents_viewport()->layer());
child->SetPaintToLayer();
// TEXTURED layer needed for contents_viewport since a descendant view has a
// layer.
EXPECT_EQ(test_api.contents_viewport()->layer()->type(), ui::LAYER_TEXTURED);
}
// Validates if scroll_with_layers is enabled, we disallow to change the layer
// of contents_ once the contents of ScrollView are set.
TEST_F(
ScrollViewTest,
ContentsLayerCannotBeChangedAfterContentsAreSet_ScrollWithLayersEnabled) {
ScrollView scroll_view(ScrollView::ScrollWithLayers::kEnabled);
ScrollViewTestApi test_api(&scroll_view);
View* contents = scroll_view.SetContents(std::make_unique<View>());
EXPECT_DCHECK_DEATH(contents->SetPaintToLayer(ui::LAYER_NOT_DRAWN));
}
// Validates if scroll_with_layers is disabled, we can change the layer of
// contents_ once the contents of ScrollView are set.
TEST_F(ScrollViewTest,
ContentsLayerCanBeChangedAfterContentsAreSet_ScrollWithLayersDisabled) {
ScrollView scroll_view(ScrollView::ScrollWithLayers::kDisabled);
ScrollViewTestApi test_api(&scroll_view);
View* contents = scroll_view.SetContents(std::make_unique<View>());
ASSERT_NO_FATAL_FAILURE(contents->SetPaintToLayer());
}
// Validates if the content of contents_viewport is changed, a correct layer is
// used for contents_viewport.
TEST_F(
ScrollViewTest,
ContentsViewportLayerUsedWhenScrollViewContentsAreChanged_ScrollWithLayersDisabled) {
// Disabling scroll_with_layers feature explicitly.
ScrollView scroll_view(ScrollView::ScrollWithLayers::kDisabled);
ScrollViewTestApi test_api(&scroll_view);
auto contents = std::make_unique<View>();
contents->AddChildView(std::make_unique<View>());
scroll_view.SetContents(std::move(contents));
// Replacing the old contents of scroll view.
auto a_view = std::make_unique<View>();
a_view->AddChildView(std::make_unique<View>());
View* child = a_view->AddChildView(std::make_unique<View>());
child->SetPaintToLayer();
scroll_view.SetContents(std::move(a_view));
EXPECT_EQ(test_api.contents_viewport()->layer()->type(), ui::LAYER_TEXTURED);
}
// Validates correct behavior of layers used for contents_viewport used when
// scroll with layers is enabled.
TEST_F(ScrollViewTest, ContentsViewportLayerUsed_ScrollWithLayersEnabled) {
ScrollView scroll_view(ScrollView::ScrollWithLayers::kEnabled);
ScrollViewTestApi test_api(&scroll_view);
// scroll_with_layer feature ensures that contents_viewport always have a
// layer.
ASSERT_TRUE(test_api.contents_viewport()->layer());
EXPECT_EQ(test_api.contents_viewport()->layer()->type(), ui::LAYER_NOT_DRAWN);
// scroll_with_layer feature enables a layer on content before adding to
// contents_viewport_.
View* contents = scroll_view.SetContents(std::make_unique<View>());
EXPECT_EQ(test_api.contents_viewport()->layer()->type(), ui::LAYER_NOT_DRAWN);
View* child = contents->AddChildView(std::make_unique<View>());
child->SetPaintToLayer();
EXPECT_EQ(test_api.contents_viewport()->layer()->type(), ui::LAYER_NOT_DRAWN);
}
// Validates if correct layers are used for contents_viewport used when
// ScrollView enables a NOT_DRAWN layer on contents when scroll with layers in
// enabled.
TEST_F(
ScrollViewTest,
ContentsViewportLayerUsedWhenNotDrawnUsedForContents_ScrollWithLayersEnabled) {
ScrollView scroll_view(ScrollView::ScrollWithLayers::kEnabled);
ScrollViewTestApi test_api(&scroll_view);
// scroll_with_layer feature ensures that contents_viewport always have a
// layer.
ASSERT_TRUE(test_api.contents_viewport()->layer());
EXPECT_EQ(test_api.contents_viewport()->layer()->type(), ui::LAYER_NOT_DRAWN);
// changing the layer type that the scrollview enables on contents.
scroll_view.SetContentsLayerType(ui::LAYER_NOT_DRAWN);
View* contents = scroll_view.SetContents(std::make_unique<View>());
EXPECT_EQ(test_api.contents_viewport()->layer()->type(), ui::LAYER_TEXTURED);
View* child = contents->AddChildView(std::make_unique<View>());
child->SetPaintToLayer();
EXPECT_EQ(test_api.contents_viewport()->layer()->type(), ui::LAYER_TEXTURED);
}
TEST_F(ScrollViewTest,
ContentsViewportLayerHasRoundedCorners_ScrollWithLayersEnabled) {
ScrollView scroll_view(ScrollView::ScrollWithLayers::kEnabled);
ScrollViewTestApi test_api(&scroll_view);
ASSERT_TRUE(test_api.contents_viewport()->layer());
const gfx::RoundedCornersF corner_radii = gfx::RoundedCornersF{16};
scroll_view.SetViewportRoundedCornerRadius(corner_radii);
EXPECT_EQ(test_api.contents_viewport()->layer()->rounded_corner_radii(),
corner_radii);
}
#if BUILDFLAG(IS_MAC)
// Tests the overlay scrollbars on Mac. Ensure that they show up properly and
// do not overlap each other.
TEST_F(ScrollViewTest, CocoaOverlayScrollBars) {
SetOverlayScrollersEnabled(true);
View* contents = InstallContents();
// Size the contents such that vertical scrollbar is needed.
// Since it is overlaid, the ViewPort size should match the ScrollView.
contents->SetBounds(0, 0, 50, 400);
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_EQ(100, contents->parent()->width());
EXPECT_EQ(100, contents->parent()->height());
EXPECT_EQ(0, scroll_view_->GetScrollBarLayoutWidth());
CheckScrollbarVisibility(scroll_view_.get(), VERTICAL, true);
CheckScrollbarVisibility(scroll_view_.get(), HORIZONTAL, false);
// Size the contents such that horizontal scrollbar is needed.
contents->SetBounds(0, 0, 400, 50);
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_EQ(100, contents->parent()->width());
EXPECT_EQ(100, contents->parent()->height());
EXPECT_EQ(0, scroll_view_->GetScrollBarLayoutHeight());
CheckScrollbarVisibility(scroll_view_.get(), VERTICAL, false);
CheckScrollbarVisibility(scroll_view_.get(), HORIZONTAL, true);
// Both horizontal and vertical scrollbars.
contents->SetBounds(0, 0, 300, 400);
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_EQ(100, contents->parent()->width());
EXPECT_EQ(100, contents->parent()->height());
EXPECT_EQ(0, scroll_view_->GetScrollBarLayoutWidth());
EXPECT_EQ(0, scroll_view_->GetScrollBarLayoutHeight());
CheckScrollbarVisibility(scroll_view_.get(), VERTICAL, true);
CheckScrollbarVisibility(scroll_view_.get(), HORIZONTAL, true);
// Make sure the horizontal and vertical scrollbars don't overlap each other.
gfx::Rect vert_bounds = scroll_view_->vertical_scroll_bar()->bounds();
gfx::Rect horiz_bounds = scroll_view_->horizontal_scroll_bar()->bounds();
EXPECT_EQ(vert_bounds.x(), horiz_bounds.right());
EXPECT_EQ(horiz_bounds.y(), vert_bounds.bottom());
// Switch to the non-overlay style and check that the ViewPort is now sized
// to be smaller, and ScrollbarWidth and ScrollbarHeight are non-zero.
SetOverlayScrollersEnabled(false);
EXPECT_TRUE(ViewTestApi(scroll_view_.get()).needs_layout());
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_EQ(100 - VerticalScrollBarWidth(), contents->parent()->width());
EXPECT_EQ(100 - HorizontalScrollBarHeight(), contents->parent()->height());
EXPECT_NE(0, VerticalScrollBarWidth());
EXPECT_NE(0, HorizontalScrollBarHeight());
}
// Test that overlay scroll bars will only process events when visible.
TEST_F(WidgetScrollViewTest,
OverlayScrollBarsCannotProcessEventsWhenTransparent) {
// Allow expectations to distinguish between fade outs and immediate changes.
ui::ScopedAnimationDurationScaleMode really_animate(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
SetUseOverlayScrollers();
ScrollView* scroll_view = AddScrollViewWithContentSize(
gfx::Size(kDefaultWidth * 5, kDefaultHeight * 5));
ScrollViewTestApi test_api(scroll_view);
ScrollBar* scroll_bar = test_api.GetScrollBar(HORIZONTAL);
// Verify scroll bar is unable to process events.
EXPECT_FALSE(scroll_bar->GetCanProcessEventsWithinSubtree());
ui::test::EventGenerator generator(
GetContext(), scroll_view->GetWidget()->GetNativeWindow());
generator.GenerateTrackpadRest();
// Since the scroll bar will become visible, it should now be able to process
// events.
EXPECT_TRUE(scroll_bar->GetCanProcessEventsWithinSubtree());
}
// Test overlay scrollbar behavior when just resting fingers on the trackpad.
TEST_F(WidgetScrollViewTest, ScrollersOnRest) {
// Allow expectations to distinguish between fade outs and immediate changes.
ui::ScopedAnimationDurationScaleMode really_animate(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
const float kMaxOpacity = 0.8f; // Constant from cocoa_scroll_bar.mm.
SetUseOverlayScrollers();
// Set up with both scrollers.
ScrollView* scroll_view = AddScrollViewWithContentSize(
gfx::Size(kDefaultWidth * 5, kDefaultHeight * 5));
ScrollViewTestApi test_api(scroll_view);
const auto bar = std::to_array<ScrollBar*>(
{test_api.GetScrollBar(HORIZONTAL), test_api.GetScrollBar(VERTICAL)});
const auto hide_timer = std::to_array<base::RetainingOneShotTimer*>(
{test_api.GetScrollBarHideTimer(HORIZONTAL),
test_api.GetScrollBarHideTimer(VERTICAL)});
EXPECT_EQ(0, bar[HORIZONTAL]->layer()->opacity());
EXPECT_EQ(0, bar[VERTICAL]->layer()->opacity());
ui::test::EventGenerator generator(
GetContext(), scroll_view->GetWidget()->GetNativeWindow());
generator.GenerateTrackpadRest();
// Scrollers should be max opacity without an animation.
EXPECT_EQ(kMaxOpacity, bar[HORIZONTAL]->layer()->opacity());
EXPECT_EQ(kMaxOpacity, bar[VERTICAL]->layer()->opacity());
EXPECT_FALSE(hide_timer[HORIZONTAL]->IsRunning());
EXPECT_FALSE(hide_timer[VERTICAL]->IsRunning());
generator.CancelTrackpadRest();
// Scrollers should start fading out, but only after a delay.
for (ScrollBarOrientation orientation : {HORIZONTAL, VERTICAL}) {
EXPECT_EQ(kMaxOpacity, bar[orientation]->layer()->GetTargetOpacity());
EXPECT_TRUE(hide_timer[orientation]->IsRunning());
// Trigger the timer. Should then be fading out.
hide_timer[orientation]->user_task().Run();
hide_timer[orientation]->Stop();
EXPECT_EQ(0, bar[orientation]->layer()->GetTargetOpacity());
}
// Rest again.
generator.GenerateTrackpadRest();
EXPECT_EQ(kMaxOpacity, bar[HORIZONTAL]->layer()->GetTargetOpacity());
EXPECT_EQ(kMaxOpacity, bar[VERTICAL]->layer()->GetTargetOpacity());
// Scroll vertically.
const float y_offset = 3;
const int kSteps = 1;
const int kNnumFingers = 2;
generator.ScrollSequence(generator.current_screen_location(),
base::TimeDelta(), 0, y_offset, kSteps,
kNnumFingers);
// Horizontal scroller should start fading out immediately.
EXPECT_EQ(kMaxOpacity, bar[HORIZONTAL]->layer()->opacity());
EXPECT_EQ(0, bar[HORIZONTAL]->layer()->GetTargetOpacity());
EXPECT_FALSE(hide_timer[HORIZONTAL]->IsRunning());
// Vertical should remain visible, but ready to fade out after a delay.
EXPECT_EQ(kMaxOpacity, bar[VERTICAL]->layer()->opacity());
EXPECT_EQ(kMaxOpacity, bar[VERTICAL]->layer()->GetTargetOpacity());
EXPECT_TRUE(hide_timer[VERTICAL]->IsRunning());
// Scrolling should have occurred.
EXPECT_EQ(gfx::PointF(0, y_offset), test_api.CurrentOffset());
// Then, scrolling horizontally should show the horizontal scroller. The
// vertical scroller should still be visible, running its hide timer.
const float x_offset = 5;
generator.ScrollSequence(generator.current_screen_location(),
base::TimeDelta(), x_offset, 0, kSteps,
kNnumFingers);
for (ScrollBarOrientation orientation : {HORIZONTAL, VERTICAL}) {
EXPECT_EQ(kMaxOpacity, bar[orientation]->layer()->opacity());
EXPECT_EQ(kMaxOpacity, bar[orientation]->layer()->GetTargetOpacity());
EXPECT_TRUE(hide_timer[orientation]->IsRunning());
}
// Now scrolling has occurred in both directions.
EXPECT_EQ(gfx::PointF(x_offset, y_offset), test_api.CurrentOffset());
}
#endif // BUILDFLAG(IS_MAC)
// Test that increasing the size of the viewport "below" scrolled content causes
// the content to scroll up so that it still fills the viewport.
TEST_F(ScrollViewTest, ConstrainScrollToBounds) {
ScrollViewTestApi test_api(scroll_view_.get());
View* contents = InstallContents();
contents->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_EQ(gfx::PointF(), test_api.CurrentOffset());
// Scroll as far as it goes and query location to discount scroll bars.
contents->ScrollRectToVisible(gfx::Rect(300, 300, 1, 1));
const gfx::PointF fully_scrolled = test_api.CurrentOffset();
EXPECT_NE(gfx::PointF(), fully_scrolled);
// Making the viewport 55 pixels taller should scroll up the same amount.
scroll_view_->SetBoundsRect(gfx::Rect(0, 0, 100, 155));
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_EQ(fully_scrolled.y() - 55, test_api.CurrentOffset().y());
EXPECT_EQ(fully_scrolled.x(), test_api.CurrentOffset().x());
// And 77 pixels wider should scroll left. Also make it short again: the y-
// offset from the last change should remain.
scroll_view_->SetBoundsRect(gfx::Rect(0, 0, 177, 100));
InvalidateAndRunScheduledLayoutOnScrollView();
EXPECT_EQ(fully_scrolled.y() - 55, test_api.CurrentOffset().y());
EXPECT_EQ(fully_scrolled.x() - 77, test_api.CurrentOffset().x());
}
// Calling Layout on ScrollView should not reset the scroll location.
TEST_F(ScrollViewTest, ContentScrollNotResetOnLayout) {
ScrollViewTestApi test_api(scroll_view_.get());
auto* contents = scroll_view_->SetContents(std::make_unique<CustomView>());
contents->SetPreferredSize(gfx::Size(300, 300));
scroll_view_->ClipHeightTo(0, 150);
scroll_view_->SizeToPreferredSize();
// ScrollView preferred width matches that of |contents|, with the height
// capped at the value we clipped to.
EXPECT_EQ(gfx::Size(300, 150), scroll_view_->size());
// Scroll down.
scroll_view_->ScrollToPosition(test_api.GetScrollBar(VERTICAL), 25);
EXPECT_EQ(25, test_api.CurrentOffset().y());
// Call Layout; no change to scroll position.
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_EQ(25, test_api.CurrentOffset().y());
// Change contents of |contents|, call Layout; still no change to scroll
// position.
contents->SetPreferredSize(gfx::Size(300, 500));
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_EQ(25, test_api.CurrentOffset().y());
// Change |contents| to be shorter than the ScrollView's clipped height.
// This /will/ change the scroll location due to ConstrainScrollToBounds.
contents->SetPreferredSize(gfx::Size(300, 50));
views::test::RunScheduledLayout(scroll_view_.get());
EXPECT_EQ(0, test_api.CurrentOffset().y());
}
TEST_F(ScrollViewTest, ArrowKeyScrolling) {
// Set up with vertical scrollbar.
auto contents = std::make_unique<FixedView>();
contents->SetPreferredSize(gfx::Size(kWidth, kMaxHeight * 5));
scroll_view_->SetContents(std::move(contents));
scroll_view_->ClipHeightTo(0, kMaxHeight);
scroll_view_->SetSize(gfx::Size(kWidth, kMaxHeight));
CheckScrollbarVisibility(scroll_view_.get(), VERTICAL, true);
// The vertical position starts at 0.
ScrollViewTestApi test_api(scroll_view_.get());
EXPECT_EQ(0, test_api.IntegralViewOffset().y());
// Pressing the down arrow key scrolls down. The amount isn't important.
ui::KeyEvent down_arrow(ui::EventType::kKeyPressed, ui::VKEY_DOWN,
ui::EF_NONE);
EXPECT_TRUE(scroll_view_->OnKeyPressed(down_arrow));
EXPECT_GT(0, test_api.IntegralViewOffset().y());
// Pressing the up arrow key scrolls back to the origin.
ui::KeyEvent up_arrow(ui::EventType::kKeyPressed, ui::VKEY_UP, ui::EF_NONE);
EXPECT_TRUE(scroll_view_->OnKeyPressed(up_arrow));
EXPECT_EQ(0, test_api.IntegralViewOffset().y());
}
TEST_F(ScrollViewTest, ArrowKeyScrollingDisabled) {
// Set up with vertical scrollbar.
auto contents = std::make_unique<FixedView>();
contents->SetPreferredSize(gfx::Size(kWidth, kMaxHeight * 5));
scroll_view_->SetContents(std::move(contents));
scroll_view_->ClipHeightTo(0, kMaxHeight);
scroll_view_->SetSize(gfx::Size(kWidth, kMaxHeight));
CheckScrollbarVisibility(scroll_view_.get(), VERTICAL, true);
// Disable keyboard scrolling.
scroll_view_->SetAllowKeyboardScrolling(false);
// The vertical position starts at 0.
ScrollViewTestApi test_api(scroll_view_.get());
EXPECT_EQ(0, test_api.IntegralViewOffset().y());
// Pressing the down arrow key does not consume the event, nor scroll.
ui::KeyEvent down(ui::EventType::kKeyPressed, ui::VKEY_DOWN, ui::EF_NONE);
EXPECT_FALSE(scroll_view_->OnKeyPressed(down));
EXPECT_EQ(0, test_api.IntegralViewOffset().y());
}
// Test that overflow indicators turn on appropriately.
TEST_F(ScrollViewTest, VerticalOverflowIndicators) {
ScrollViewTestApi test_api(scroll_view_.get());
// Set up with vertical scrollbar.
auto contents = std::make_unique<FixedView>();
contents->SetPreferredSize(gfx::Size(kWidth, kMaxHeight * 5));
scroll_view_->SetContents(std::move(contents));
scroll_view_->ClipHeightTo(0, kMaxHeight);
// Make sure the size is set such that no horizontal scrollbar gets shown.
scroll_view_->SetSize(gfx::Size(
kWidth + test_api.GetScrollBar(VERTICAL)->GetThickness(), kMaxHeight));
// Make sure the initial origin is 0,0
EXPECT_EQ(gfx::PointF(0, 0), test_api.CurrentOffset());
// The vertical scroll bar should be visible and the horizontal scroll bar
// should not.
CheckScrollbarVisibility(scroll_view_.get(), VERTICAL, true);
CheckScrollbarVisibility(scroll_view_.get(), HORIZONTAL, false);
// The overflow indicator on the bottom should be visible.
EXPECT_TRUE(test_api.more_content_bottom()->GetVisible());
// The overflow indicator on the top should not be visible.
EXPECT_FALSE(test_api.more_content_top()->GetVisible());
// No other overflow indicators should be visible.
EXPECT_FALSE(test_api.more_content_left()->GetVisible());
EXPECT_FALSE(test_api.more_content_right()->GetVisible());
// Now scroll the view to someplace in the middle of the scrollable region.
int offset = kMaxHeight * 2;
scroll_view_->ScrollToPosition(test_api.GetScrollBar(VERTICAL), offset);
EXPECT_EQ(gfx::PointF(0, offset), test_api.CurrentOffset());
// At this point, both overflow indicators on the top and bottom should be
// visible.
EXPECT_TRUE(test_api.more_content_top()->GetVisible());
EXPECT_TRUE(test_api.more_content_bottom()->GetVisible());
// The left and right overflow indicators should still not be visible.
EXPECT_FALSE(test_api.more_content_left()->GetVisible());
EXPECT_FALSE(test_api.more_content_right()->GetVisible());
// Finally scroll the view to end of the scrollable region.
offset = kMaxHeight * 4;
scroll_view_->ScrollToPosition(test_api.GetScrollBar(VERTICAL), offset);
EXPECT_EQ(gfx::PointF(0, offset), test_api.CurrentOffset());
// The overflow indicator on the bottom should not be visible.
EXPECT_FALSE(test_api.more_content_bottom()->GetVisible());
// The overflow indicator on the top should be visible.
EXPECT_TRUE(test_api.more_content_top()->GetVisible());
// As above, no other overflow indicators should be visible.
EXPECT_FALSE(test_api.more_content_left()->GetVisible());
EXPECT_FALSE(test_api.more_content_right()->GetVisible());
}
TEST_F(ScrollViewTest, HorizontalOverflowIndicators) {
const int kHeight = 100;
ScrollViewTestApi test_api(scroll_view_.get());
// Set up with horizontal scrollbar.
auto* contents = scroll_view_->SetContents(std::make_unique<FixedView>());
contents->SetPreferredSize(gfx::Size(kWidth * 5, kHeight));
// Make sure the size is set such that no vertical scrollbar gets shown.
scroll_view_->SetSize(gfx::Size(
kWidth, kHeight + test_api.GetScrollBar(HORIZONTAL)->GetThickness()));
contents->SetBounds(0, 0, kWidth * 5, kHeight);
// Make sure the initial origin is 0,0
EXPECT_EQ(gfx::PointF(0, 0), test_api.CurrentOffset());
// The horizontal scroll bar should be visible and the vertical scroll bar
// should not.
CheckScrollbarVisibility(scroll_view_.get(), HORIZONTAL, true);
CheckScrollbarVisibility(scroll_view_.get(), VERTICAL, false);
// The overflow indicator on the right should be visible.
EXPECT_TRUE(test_api.more_content_right()->GetVisible());
// The overflow indicator on the left should not be visible.
EXPECT_FALSE(test_api.more_content_left()->GetVisible());
// No other overflow indicators should be visible.
EXPECT_FALSE(test_api.more_content_top()->GetVisible());
EXPECT_FALSE(test_api.more_content_bottom()->GetVisible());
// Now scroll the view to someplace in the middle of the scrollable region.
int offset = kWidth * 2;
scroll_view_->ScrollToPosition(test_api.GetScrollBar(HORIZONTAL), offset);
EXPECT_EQ(gfx::PointF(offset, 0), test_api.CurrentOffset());
// At this point, both overflow indicators on the left and right should be
// visible.
EXPECT_TRUE(test_api.more_content_left()->GetVisible());
EXPECT_TRUE(test_api.more_content_right()->GetVisible());
// The top and bottom overflow indicators should still not be visible.
EXPECT_FALSE(test_api.more_content_top()->GetVisible());
EXPECT_FALSE(test_api.more_content_bottom()->GetVisible());
// Finally scroll the view to end of the scrollable region.
offset = kWidth * 4;
scroll_view_->ScrollToPosition(test_api.GetScrollBar(HORIZONTAL), offset);
EXPECT_EQ(gfx::PointF(offset, 0), test_api.CurrentOffset());
// The overflow indicator on the right should not be visible.
EXPECT_FALSE(test_api.more_content_right()->GetVisible());
// The overflow indicator on the left should be visible.
EXPECT_TRUE(test_api.more_content_left()->GetVisible());
// As above, no other overflow indicators should be visible.
EXPECT_FALSE(test_api.more_content_top()->GetVisible());
EXPECT_FALSE(test_api.more_content_bottom()->GetVisible());
}
TEST_F(ScrollViewTest, HorizontalVerticalOverflowIndicators) {
const int kHeight = 100;
ScrollViewTestApi test_api(scroll_view_.get());
// Set up with both horizontal and vertical scrollbars.
auto contents = std::make_unique<FixedView>();
contents->SetPreferredSize(gfx::Size(kWidth * 5, kHeight * 5));
scroll_view_->SetContents(std::move(contents));
// Make sure the size is set such that both scrollbars are shown.
scroll_view_->SetSize(gfx::Size(kWidth, kHeight));
// Make sure the initial origin is 0,0
EXPECT_EQ(gfx::PointF(0, 0), test_api.CurrentOffset());
// The horizontal and vertical scroll bars should be visible.
CheckScrollbarVisibility(scroll_view_.get(), HORIZONTAL, true);
CheckScrollbarVisibility(scroll_view_.get(), VERTICAL, true);
// The overflow indicators on the right and bottom should not be visible since
// they are against the scrollbars.
EXPECT_FALSE(test_api.more_content_right()->GetVisible());
EXPECT_FALSE(test_api.more_content_bottom()->GetVisible());
// The overflow indicators on the left and top should not be visible.
EXPECT_FALSE(test_api.more_content_left()->GetVisible());
EXPECT_FALSE(test_api.more_content_top()->GetVisible());
// Now scroll the view to someplace in the middle of the horizontal scrollable
// region.
int offset_x = kWidth * 2;
scroll_view_->ScrollToPosition(test_api.GetScrollBar(HORIZONTAL), offset_x);
EXPECT_EQ(gfx::PointF(offset_x, 0), test_api.CurrentOffset());
// Since there is a vertical scrollbar only the overflow indicator on the left
// should be visible and the one on the right should still not be visible.
EXPECT_TRUE(test_api.more_content_left()->GetVisible());
EXPECT_FALSE(test_api.more_content_right()->GetVisible());
// The top and bottom overflow indicators should still not be visible.
EXPECT_FALSE(test_api.more_content_top()->GetVisible());
EXPECT_FALSE(test_api.more_content_bottom()->GetVisible());
// Next, scroll the view to end of the scrollable region.
offset_x = kWidth * 4;
scroll_view_->ScrollToPosition(test_api.GetScrollBar(HORIZONTAL), offset_x);
EXPECT_EQ(gfx::PointF(offset_x, 0), test_api.CurrentOffset());
// The overflow indicator on the right should still not be visible.
EXPECT_FALSE(test_api.more_content_right()->GetVisible());
// The overflow indicator on the left should be visible.
EXPECT_TRUE(test_api.more_content_left()->GetVisible());
// As above, the other overflow indicators should not be visible because the
// view hasn't scrolled vertically and the bottom indicator is against the
// horizontal scrollbar.
EXPECT_FALSE(test_api.more_content_top()->GetVisible());
EXPECT_FALSE(test_api.more_content_bottom()->GetVisible());
// Return the view back to the horizontal origin.
scroll_view_->ScrollToPosition(test_api.GetScrollBar(HORIZONTAL), 0);
EXPECT_EQ(gfx::PointF(0, 0), test_api.CurrentOffset());
// The overflow indicators on the right and bottom should not be visible since
// they are against the scrollbars.
EXPECT_FALSE(test_api.more_content_right()->GetVisible());
EXPECT_FALSE(test_api.more_content_bottom()->GetVisible());
// The overflow indicators on the left and top should not be visible since the
// is at the origin.
EXPECT_FALSE(test_api.more_content_left()->GetVisible());
EXPECT_FALSE(test_api.more_content_top()->GetVisible());
// Now scroll the view to somplace in the middle of the vertical scrollable
// region.
int offset_y = kHeight * 2;
scroll_view_->ScrollToPosition(test_api.GetScrollBar(VERTICAL), offset_y);
EXPECT_EQ(gfx::PointF(0, offset_y), test_api.CurrentOffset());
// Similar to the above, since there is a horizontal scrollbar only the
// overflow indicator on the top should be visible and the one on the bottom
// should still not be visible.
EXPECT_TRUE(test_api.more_content_top()->GetVisible());
EXPECT_FALSE(test_api.more_content_bottom()->GetVisible());
// The left and right overflow indicators should still not be visible.
EXPECT_FALSE(test_api.more_content_left()->GetVisible());
EXPECT_FALSE(test_api.more_content_right()->GetVisible());
// Finally, for the vertical test scroll the region all the way to the end.
offset_y = kHeight * 4;
scroll_view_->ScrollToPosition(test_api.GetScrollBar(VERTICAL), offset_y);
EXPECT_EQ(gfx::PointF(0, offset_y), test_api.CurrentOffset());
// The overflow indicator on the bottom should still not be visible.
EXPECT_FALSE(test_api.more_content_bottom()->GetVisible());
// The overflow indicator on the top should still be visible.
EXPECT_TRUE(test_api.more_content_top()->GetVisible());
// As above, the other overflow indicators should not be visible because the
// view hasn't scrolled horizontally and the right indicator is against the
// vertical scrollbar.
EXPECT_FALSE(test_api.more_content_left()->GetVisible());
EXPECT_FALSE(test_api.more_content_right()->GetVisible());
// Back to the horizontal. Scroll all the way to the end in the horizontal
// direction.
offset_x = kWidth * 4;
scroll_view_->ScrollToPosition(test_api.GetScrollBar(HORIZONTAL), offset_x);
EXPECT_EQ(gfx::PointF(offset_x, offset_y), test_api.CurrentOffset());
// The overflow indicator on the bottom and right should still not be visible.
EXPECT_FALSE(test_api.more_content_bottom()->GetVisible());
EXPECT_FALSE(test_api.more_content_right()->GetVisible());
// The overflow indicators on the top and left should now be visible.
EXPECT_TRUE(test_api.more_content_top()->GetVisible());
EXPECT_TRUE(test_api.more_content_left()->GetVisible());
}
TEST_F(ScrollViewTest, VerticalWithHeaderOverflowIndicators) {
ScrollViewTestApi test_api(scroll_view_.get());
// Set up with vertical scrollbar and a header.
auto contents = std::make_unique<FixedView>();
auto header = std::make_unique<CustomView>();
contents->SetPreferredSize(gfx::Size(kWidth, kMaxHeight * 5));
header->SetPreferredSize(gfx::Size(10, 20));
scroll_view_->SetContents(std::move(contents));
auto* header_ptr = scroll_view_->SetHeader(std::move(header));
scroll_view_->ClipHeightTo(0, kMaxHeight + header_ptr->height());
// Make sure the size is set such that no horizontal scrollbar gets shown.
scroll_view_->SetSize(
gfx::Size(kWidth + test_api.GetScrollBar(VERTICAL)->GetThickness(),
kMaxHeight + header_ptr->height()));
// Make sure the initial origin is 0,0
EXPECT_EQ(gfx::PointF(0, 0), test_api.CurrentOffset());
// The vertical scroll bar should be visible and the horizontal scroll bar
// should not.
CheckScrollbarVisibility(scroll_view_.get(), VERTICAL, true);
CheckScrollbarVisibility(scroll_view_.get(), HORIZONTAL, false);
// The overflow indicator on the bottom should be visible.
EXPECT_TRUE(test_api.more_content_bottom()->GetVisible());
// The overflow indicator on the top should not be visible.
EXPECT_FALSE(test_api.more_content_top()->GetVisible());
// No other overflow indicators should be visible.
EXPECT_FALSE(test_api.more_content_left()->GetVisible());
EXPECT_FALSE(test_api.more_content_right()->GetVisible());
// Now scroll the view to someplace in the middle of the scrollable region.
int offset = kMaxHeight * 2;
scroll_view_->ScrollToPosition(test_api.GetScrollBar(VERTICAL), offset);
EXPECT_EQ(gfx::PointF(0, offset), test_api.CurrentOffset());
// At this point, only the overflow indicator on the bottom should be visible
// because the top indicator never comes on because of the presence of the
// header.
EXPECT_FALSE(test_api.more_content_top()->GetVisible());
EXPECT_TRUE(test_api.more_content_bottom()->GetVisible());
// The left and right overflow indicators should still not be visible.
EXPECT_FALSE(test_api.more_content_left()->GetVisible());
EXPECT_FALSE(test_api.more_content_right()->GetVisible());
// Finally scroll the view to end of the scrollable region.
offset = test_api.GetScrollBar(VERTICAL)->GetMaxPosition();
scroll_view_->ScrollToPosition(test_api.GetScrollBar(VERTICAL), offset);
EXPECT_EQ(gfx::PointF(0, offset), test_api.CurrentOffset());
// The overflow indicator on the bottom should not be visible now.
EXPECT_FALSE(test_api.more_content_bottom()->GetVisible());
// The overflow indicator on the top should still not be visible.
EXPECT_FALSE(test_api.more_content_top()->GetVisible());
// As above, no other overflow indicators should be visible.
EXPECT_FALSE(test_api.more_content_left()->GetVisible());
EXPECT_FALSE(test_api.more_content_right()->GetVisible());
}
TEST_F(ScrollViewTest, CustomOverflowIndicator) {
const int kHeight = 100;
ScrollViewTestApi test_api(scroll_view_.get());
// Set up with both horizontal and vertical scrolling.
auto contents = std::make_unique<FixedView>();
contents->SetPreferredSize(gfx::Size(kWidth * 5, kHeight * 5));
scroll_view_->SetContents(std::move(contents));
// Hide both scrollbars so they don't interfere with indicator visibility.
scroll_view_->SetHorizontalScrollBarMode(
views::ScrollView::ScrollBarMode::kHiddenButEnabled);
scroll_view_->SetVerticalScrollBarMode(
views::ScrollView::ScrollBarMode::kHiddenButEnabled);
// Make sure the size is set so the ScrollView is smaller than its contents
// in both directions.
scroll_view_->SetSize(gfx::Size(kWidth, kHeight));
// The horizontal and vertical scroll bars should not be visible.
CheckScrollbarVisibility(scroll_view_.get(), HORIZONTAL, false);
CheckScrollbarVisibility(scroll_view_.get(), VERTICAL, false);
// Make sure the initial origin is 0,0
EXPECT_EQ(gfx::PointF(0, 0), test_api.CurrentOffset());
// Now scroll the view to someplace in the middle of the scrollable region.
int offset_x = kWidth * 2;
scroll_view_->ScrollToPosition(test_api.GetScrollBar(HORIZONTAL), offset_x);
int offset_y = kHeight * 2;
scroll_view_->ScrollToPosition(test_api.GetScrollBar(VERTICAL), offset_y);
EXPECT_EQ(gfx::PointF(offset_x, offset_y), test_api.CurrentOffset());
// All overflow indicators should be visible.
ASSERT_TRUE(test_api.more_content_right()->GetVisible());
ASSERT_TRUE(test_api.more_content_bottom()->GetVisible());
ASSERT_TRUE(test_api.more_content_left()->GetVisible());
ASSERT_TRUE(test_api.more_content_top()->GetVisible());
// This should be similar to the default separator.
View* left_indicator = scroll_view_->SetCustomOverflowIndicator(
OverflowIndicatorAlignment::kLeft, std::make_unique<View>(), 1, true);
EXPECT_EQ(gfx::Rect(0, 0, 1, 100), left_indicator->bounds());
if (left_indicator->layer()) {
EXPECT_TRUE(left_indicator->layer()->fills_bounds_opaquely());
}
// A larger, but still reasonable, indicator that is not opaque.
View* top_indicator = scroll_view_->SetCustomOverflowIndicator(
OverflowIndicatorAlignment::kTop, std::make_unique<View>(), 20, false);
EXPECT_EQ(gfx::Rect(0, 0, 100, 20), top_indicator->bounds());
if (top_indicator->layer()) {
EXPECT_FALSE(top_indicator->layer()->fills_bounds_opaquely());
}
// Negative thickness doesn't make sense. It should be treated like zero.
View* right_indicator = scroll_view_->SetCustomOverflowIndicator(
OverflowIndicatorAlignment::kRight, std::make_unique<View>(), -1, true);
EXPECT_EQ(gfx::Rect(100, 0, 0, 100), right_indicator->bounds());
// Thicker than the scrollview is strange, but works as you'd expect.
View* bottom_indicator = scroll_view_->SetCustomOverflowIndicator(
OverflowIndicatorAlignment::kBottom, std::make_unique<View>(), 1000,
true);
EXPECT_EQ(gfx::Rect(0, -900, 100, 1000), bottom_indicator->bounds());
}
// Ensure ScrollView::Layout succeeds if a disabled scrollbar's overlap style
// does not match the other scrollbar.
TEST_F(ScrollViewTest, IgnoreOverlapWithDisabledHorizontalScroll) {
ScrollViewTestApi test_api(scroll_view_.get());
constexpr int kThickness = 1;
// Assume horizontal scroll bar is the default and is overlapping.
scroll_view_->SetHorizontalScrollBar(
std::make_unique<TestScrollBar>(ScrollBar::Orientation::kHorizontal,
/*overlaps_content=*/true, kThickness));
// Assume vertical scroll bar is custom and it we want it to not overlap.
scroll_view_->SetVerticalScrollBar(
std::make_unique<TestScrollBar>(ScrollBar::Orientation::kVertical,
/*overlaps_content=*/false, kThickness));
// Also, let's turn off horizontal scroll bar.
scroll_view_->SetHorizontalScrollBarMode(
ScrollView::ScrollBarMode::kDisabled);
View* contents = InstallContents();
contents->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
InvalidateAndRunScheduledLayoutOnScrollView();
gfx::Size expected_size = scroll_view_->size();
expected_size.Enlarge(-kThickness, 0);
EXPECT_EQ(expected_size, test_api.contents_viewport()->size());
}
// Ensure ScrollView::Layout succeeds if a hidden but enabled scrollbar's
// overlap style does not match the other scrollbar.
TEST_F(ScrollViewTest, IgnoreOverlapWithHiddenHorizontalScroll) {
ScrollViewTestApi test_api(scroll_view_.get());
constexpr int kThickness = 1;
// Assume horizontal scroll bar is the default and is overlapping.
scroll_view_->SetHorizontalScrollBar(
std::make_unique<TestScrollBar>(ScrollBar::Orientation::kHorizontal,
/*overlaps_content=*/true, kThickness));
// Assume vertical scroll bar is custom and it we want it to not overlap.
scroll_view_->SetVerticalScrollBar(
std::make_unique<TestScrollBar>(ScrollBar::Orientation::kVertical,
/*overlaps_content=*/false, kThickness));
// Also, let's turn off horizontal scroll bar.
scroll_view_->SetHorizontalScrollBarMode(
ScrollView::ScrollBarMode::kHiddenButEnabled);
View* contents = InstallContents();
contents->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
InvalidateAndRunScheduledLayoutOnScrollView();
gfx::Size expected_size = scroll_view_->size();
expected_size.Enlarge(-kThickness, 0);
EXPECT_EQ(expected_size, test_api.contents_viewport()->size());
}
// Ensure ScrollView::Layout succeeds if a disabled scrollbar's overlap style
// does not match the other scrollbar.
TEST_F(ScrollViewTest, IgnoreOverlapWithDisabledVerticalScroll) {
ScrollViewTestApi test_api(scroll_view_.get());
constexpr int kThickness = 1;
// Assume horizontal scroll bar is custom and it we want it to not overlap.
scroll_view_->SetHorizontalScrollBar(
std::make_unique<TestScrollBar>(ScrollBar::Orientation::kHorizontal,
/*overlaps_content=*/false, kThickness));
// Assume vertical scroll bar is the default and is overlapping.
scroll_view_->SetVerticalScrollBar(
std::make_unique<TestScrollBar>(ScrollBar::Orientation::kVertical,
/*overlaps_content=*/true, kThickness));
// Also, let's turn off horizontal scroll bar.
scroll_view_->SetVerticalScrollBarMode(ScrollView::ScrollBarMode::kDisabled);
View* contents = InstallContents();
contents->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
InvalidateAndRunScheduledLayoutOnScrollView();
gfx::Size expected_size = scroll_view_->size();
expected_size.Enlarge(0, -kThickness);
EXPECT_EQ(expected_size, test_api.contents_viewport()->size());
}
// Ensure ScrollView::Layout succeeds if a hidden but enabled scrollbar's
// overlap style does not match the other scrollbar.
TEST_F(ScrollViewTest, IgnoreOverlapWithHiddenVerticalScroll) {
ScrollViewTestApi test_api(scroll_view_.get());
constexpr int kThickness = 1;
// Assume horizontal scroll bar is custom and it we want it to not overlap.
scroll_view_->SetHorizontalScrollBar(
std::make_unique<TestScrollBar>(ScrollBar::Orientation::kHorizontal,
/*overlaps_content=*/false, kThickness));
// Assume vertical scroll bar is the default and is overlapping.
scroll_view_->SetVerticalScrollBar(
std::make_unique<TestScrollBar>(ScrollBar::Orientation::kVertical,
/*overlaps_content=*/true, kThickness));
// Also, let's turn off horizontal scroll bar.
scroll_view_->SetVerticalScrollBarMode(
ScrollView::ScrollBarMode::kHiddenButEnabled);
View* contents = InstallContents();
contents->SetBoundsRect(gfx::Rect(0, 0, 300, 300));
InvalidateAndRunScheduledLayoutOnScrollView();
gfx::Size expected_size = scroll_view_->size();
expected_size.Enlarge(0, -kThickness);
EXPECT_EQ(expected_size, test_api.contents_viewport()->size());
}
TEST_F(ScrollViewTest, TestSettingContentsToNull) {
View* contents = InstallContents();
ViewTracker tracker(contents);
ASSERT_TRUE(tracker.view());
// Make sure the content is installed and working.
EXPECT_EQ("0,0 100x100", contents->parent()->bounds().ToString());
// This should be legal and not DCHECK.
scroll_view_->SetContents(nullptr);
// The content should now be gone.
EXPECT_FALSE(scroll_view_->contents());
// The contents view should have also been deleted (and therefore the tracker
// is no longer tracking a view).
EXPECT_FALSE(tracker.view());
}
// Test scrolling behavior when clicking on the scroll track.
TEST_F(WidgetScrollViewTest, ScrollTrackScrolling) {
// Set up with a vertical scroller.
ScrollView* scroll_view =
AddScrollViewWithContentSize(gfx::Size(10, kDefaultHeight * 5));
ScrollViewTestApi test_api(scroll_view);
ScrollBar* scroll_bar = test_api.GetScrollBar(VERTICAL);
View* thumb = test_api.GetScrollBarThumb(VERTICAL);
// Click in the middle of the track, ensuring it's below the thumb.
const gfx::Point location = scroll_bar->bounds().CenterPoint();
EXPECT_GT(location.y(), thumb->bounds().bottom());
ui::MouseEvent press(TestLeftMouseAt(location, ui::EventType::kMousePressed));
ui::MouseEvent release(
TestLeftMouseAt(location, ui::EventType::kMouseReleased));
const base::OneShotTimer& timer = test_api.GetScrollBarTimer(VERTICAL);
EXPECT_FALSE(timer.IsRunning());
EXPECT_EQ(0, scroll_view->GetVisibleRect().y());
scroll_bar->OnMouseEvent(&press);
// Clicking the scroll track should scroll one "page".
EXPECT_EQ(kDefaultHeight, scroll_view->GetVisibleRect().y());
// While the mouse is pressed, timer should trigger more scroll events.
EXPECT_TRUE(timer.IsRunning());
// Upon release timer should stop (and scroll position should remain).
scroll_bar->OnMouseEvent(&release);
EXPECT_FALSE(timer.IsRunning());
EXPECT_EQ(kDefaultHeight, scroll_view->GetVisibleRect().y());
}
// Test that LocatedEvents are transformed correctly when scrolling.
TEST_F(WidgetScrollViewTest, EventLocation) {
// Set up with both scrollers.
auto contents = std::make_unique<CustomView>();
auto* contents_ptr = contents.get();
contents->SetPreferredSize(gfx::Size(kDefaultHeight * 5, kDefaultHeight * 5));
AddScrollViewWithContents(std::move(contents));
const gfx::Point location_in_widget(10, 10);
// Click without scrolling.
TestClickAt(location_in_widget);
EXPECT_EQ(location_in_widget, contents_ptr->last_location());
// Scroll down a page.
contents_ptr->ScrollRectToVisible(
gfx::Rect(0, kDefaultHeight, 1, kDefaultHeight));
TestClickAt(location_in_widget);
EXPECT_EQ(gfx::Point(10, 10 + kDefaultHeight), contents_ptr->last_location());
// Scroll right a page (and back up).
contents_ptr->ScrollRectToVisible(
gfx::Rect(kDefaultWidth, 0, kDefaultWidth, 1));
TestClickAt(location_in_widget);
EXPECT_EQ(gfx::Point(10 + kDefaultWidth, 10), contents_ptr->last_location());
// Scroll both directions.
contents_ptr->ScrollRectToVisible(
gfx::Rect(kDefaultWidth, kDefaultHeight, kDefaultWidth, kDefaultHeight));
TestClickAt(location_in_widget);
EXPECT_EQ(gfx::Point(10 + kDefaultWidth, 10 + kDefaultHeight),
contents_ptr->last_location());
}
// Ensure behavior of ScrollRectToVisible() is consistent when scrolling with
// and without layers, and under LTR and RTL.
TEST_P(WidgetScrollViewTestRTLAndLayers, ScrollOffsetWithoutLayers) {
// Set up with both scrollers. And a nested view hierarchy like:
// +-------------+
// |XX |
// | +----------|
// | | |
// | | +-------|
// | | | |
// | | | etc. |
// | | | |
// +-------------+
// Note that "XX" indicates the size of the viewport.
constexpr int kNesting = 5;
constexpr int kCellWidth = kDefaultWidth;
constexpr int kCellHeight = kDefaultHeight;
constexpr gfx::Size kContentSize(kCellWidth * kNesting,
kCellHeight * kNesting);
ScrollView* scroll_view = AddScrollViewWithContentSize(kContentSize, false);
ScrollViewTestApi test_api(scroll_view);
EXPECT_EQ(gfx::PointF(0, 0), test_api.CurrentOffset());
// Sanity check that the contents has a layer iff testing layers.
EXPECT_EQ(IsTestingLayers(), !!scroll_view->contents()->layer());
if (IsTestingRtl()) {
// Sanity check the hit-testing logic on the root view. That is, verify that
// coordinates really do flip in RTL. The difference inside the viewport is
// that the flipping should occur consistently in the entire contents (not
// just the visible contents), and take into account the scroll offset.
EXPECT_EQ(gfx::Point(kDefaultWidth - 1, 1),
HitTestInCorner(scroll_view->GetWidget()->GetRootView(), false));
EXPECT_EQ(gfx::Point(kContentSize.width() - 1, 1),
HitTestInCorner(scroll_view->contents(), false));
} else {
EXPECT_EQ(gfx::Point(1, 1),
HitTestInCorner(scroll_view->GetWidget()->GetRootView(), false));
EXPECT_EQ(gfx::Point(1, 1),
HitTestInCorner(scroll_view->contents(), false));
}
// Test vertical scrolling using coordinates on the contents canvas.
gfx::Rect offset(0, kCellHeight * 2, kCellWidth, kCellHeight);
scroll_view->contents()->ScrollRectToVisible(offset);
EXPECT_EQ(gfx::PointF(0, offset.y()), test_api.CurrentOffset());
// Rely on auto-flipping for this and future HitTestInCorner() calls.
EXPECT_EQ(gfx::Point(1, kCellHeight * 2 + 1),
HitTestInCorner(scroll_view->contents()));
// Test horizontal scrolling.
offset.set_x(kCellWidth * 2);
scroll_view->contents()->ScrollRectToVisible(offset);
EXPECT_EQ(gfx::PointF(offset.x(), offset.y()), test_api.CurrentOffset());
EXPECT_EQ(gfx::Point(kCellWidth * 2 + 1, kCellHeight * 2 + 1),
HitTestInCorner(scroll_view->contents()));
// Reset the scrolling.
scroll_view->contents()->ScrollRectToVisible(gfx::Rect(0, 0, 1, 1));
// Test transformations through a nested view hierarchy.
View* deepest_view = scroll_view->contents();
constexpr gfx::Rect kCellRect(kCellWidth, kCellHeight, kContentSize.width(),
kContentSize.height());
for (int i = 1; i < kNesting; ++i) {
SCOPED_TRACE(testing::Message("Nesting = ") << i);
View* child = new View;
child->SetBoundsRect(kCellRect);
deepest_view->AddChildViewRaw(child);
deepest_view = child;
// Add a view in one quadrant. Scrolling just this view should only scroll
// far enough for it to become visible. That is, it should be positioned at
// the bottom right of the viewport, not the top-left. But since there are
// scroll bars, the scroll offset needs to go "a bit more".
View* partial_view = new View;
partial_view->SetSize(gfx::Size(kCellWidth / 3, kCellHeight / 3));
deepest_view->AddChildViewRaw(partial_view);
partial_view->ScrollViewToVisible();
int x_offset_in_cell = kCellWidth - partial_view->width();
if (!scroll_view->horizontal_scroll_bar()->OverlapsContent()) {
x_offset_in_cell -= scroll_view->horizontal_scroll_bar()->GetThickness();
}
int y_offset_in_cell = kCellHeight - partial_view->height();
if (!scroll_view->vertical_scroll_bar()->OverlapsContent()) {
y_offset_in_cell -= scroll_view->vertical_scroll_bar()->GetThickness();
}
EXPECT_EQ(gfx::PointF(kCellWidth * i - x_offset_in_cell,
kCellHeight * i - y_offset_in_cell),
test_api.CurrentOffset());
// Now scroll the rest.
deepest_view->ScrollViewToVisible();
EXPECT_EQ(gfx::PointF(kCellWidth * i, kCellHeight * i),
test_api.CurrentOffset());
// The partial view should now be at the top-left of the viewport (top-right
// in RTL).
EXPECT_EQ(gfx::Point(1, 1), HitTestInCorner(partial_view));
gfx::Point origin;
View::ConvertPointToWidget(partial_view, &origin);
constexpr gfx::Point kTestPointRTL(kDefaultWidth - kCellWidth / 3, 0);
EXPECT_EQ(IsTestingRtl() ? kTestPointRTL : gfx::Point(), origin);
}
// Scrolling to the deepest view should have moved the viewport so that the
// (kNesting - 1) parent views are all off-screen.
EXPECT_EQ(
gfx::PointF(kCellWidth * (kNesting - 1), kCellHeight * (kNesting - 1)),
test_api.CurrentOffset());
}
// Test that views scroll offsets are in sync with the layer scroll offsets.
TEST_P(WidgetScrollViewTestRTLAndLayers, ScrollOffsetUsingLayers) {
// Set up with both scrollers, but don't commit the layer changes yet.
ScrollView* scroll_view = AddScrollViewWithContentSize(
gfx::Size(kDefaultWidth * 5, kDefaultHeight * 5), false);
ScrollViewTestApi test_api(scroll_view);
EXPECT_EQ(gfx::PointF(0, 0), test_api.CurrentOffset());
// UI code may request a scroll before layer changes are committed.
gfx::Rect offset(0, kDefaultHeight * 2, kDefaultWidth, kDefaultHeight);
scroll_view->contents()->ScrollRectToVisible(offset);
EXPECT_EQ(gfx::PointF(0, offset.y()), test_api.CurrentOffset());
// The following only makes sense when layered scrolling is enabled.
View* container = scroll_view->contents();
EXPECT_EQ(IsTestingLayers(), !!container->layer());
if (!container->layer()) {
return;
}
// Container and viewport should have layers.
EXPECT_TRUE(container->layer());
EXPECT_TRUE(test_api.contents_viewport()->layer());
// In a Widget, so there should be a compositor.
ui::Compositor* compositor = container->layer()->GetCompositor();
EXPECT_TRUE(compositor);
// But setting on the impl side should fail since the layer isn't committed.
cc::ElementId element_id =
container->layer()->cc_layer_for_testing()->element_id();
EXPECT_FALSE(compositor->ScrollLayerTo(element_id, gfx::PointF(0, 0)));
EXPECT_EQ(gfx::PointF(0, offset.y()), test_api.CurrentOffset());
WaitForCommit();
EXPECT_EQ(gfx::PointF(0, offset.y()), test_api.CurrentOffset());
// Upon commit, the impl side should report the same value too.
gfx::PointF impl_offset;
EXPECT_TRUE(compositor->GetScrollOffsetForLayer(element_id, &impl_offset));
EXPECT_EQ(gfx::PointF(0, offset.y()), impl_offset);
// Now impl-side scrolling should work, and also update the ScrollView.
offset.set_y(kDefaultHeight * 3);
EXPECT_TRUE(
compositor->ScrollLayerTo(element_id, gfx::PointF(0, offset.y())));
EXPECT_EQ(gfx::PointF(0, offset.y()), test_api.CurrentOffset());
// Scroll via ScrollView API. Should be reflected on the impl side.
offset.set_y(kDefaultHeight * 4);
scroll_view->contents()->ScrollRectToVisible(offset);
EXPECT_EQ(gfx::PointF(0, offset.y()), test_api.CurrentOffset());
EXPECT_TRUE(compositor->GetScrollOffsetForLayer(element_id, &impl_offset));
EXPECT_EQ(gfx::PointF(0, offset.y()), impl_offset);
// Test horizontal scrolling.
offset.set_x(kDefaultWidth * 2);
scroll_view->contents()->ScrollRectToVisible(offset);
EXPECT_EQ(gfx::PointF(offset.x(), offset.y()), test_api.CurrentOffset());
EXPECT_TRUE(compositor->GetScrollOffsetForLayer(element_id, &impl_offset));
EXPECT_EQ(gfx::PointF(offset.x(), offset.y()), impl_offset);
}
namespace {
// Applies |scroll_event| to |scroll_view| and verifies that the event is
// applied correctly whether or not compositor scrolling is enabled.
static void ApplyScrollEvent(const ScrollViewTestApi& test_api,
ScrollView* scroll_view,
ui::ScrollEvent& scroll_event) {
EXPECT_FALSE(scroll_event.handled());
EXPECT_FALSE(scroll_event.stopped_propagation());
scroll_view->OnScrollEvent(&scroll_event);
// Check to see if the scroll event is handled by the scroll view.
if (base::FeatureList::IsEnabled(::features::kUiCompositorScrollWithLayers)) {
// If UiCompositorScrollWithLayers is enabled, the event is set handled
// and its propagation is stopped.
EXPECT_TRUE(scroll_event.handled());
EXPECT_TRUE(scroll_event.stopped_propagation());
} else {
// If UiCompositorScrollWithLayers is disabled, the event isn't handled.
// This informs Widget::OnScrollEvent() to convert to a MouseWheel event
// and dispatch again. Simulate that.
EXPECT_FALSE(scroll_event.handled());
EXPECT_FALSE(scroll_event.stopped_propagation());
EXPECT_EQ(gfx::PointF(), test_api.CurrentOffset());
ui::MouseWheelEvent wheel(scroll_event);
scroll_view->OnMouseEvent(&wheel);
}
}
} // namespace
// Tests to see the scroll events are handled correctly in composited and
// non-composited scrolling.
TEST_F(WidgetScrollViewTest, CompositedScrollEvents) {
// Set up with a vertical scroll bar.
ScrollView* scroll_view =
AddScrollViewWithContentSize(gfx::Size(10, kDefaultHeight * 5));
ScrollViewTestApi test_api(scroll_view);
// Create a fake scroll event and send it to the scroll view.
ui::ScrollEvent scroll(ui::EventType::kScroll, gfx::Point(),
base::TimeTicks::Now(), 0, 0, -10, 0, -10, 3);
ApplyScrollEvent(test_api, scroll_view, scroll);
// Check if the scroll view has been offset.
EXPECT_EQ(gfx::PointF(0, 10), test_api.CurrentOffset());
}
// Tests to see that transposed (treat-as-horizontal) scroll events are handled
// correctly in composited and non-composited scrolling.
TEST_F(WidgetScrollViewTest, CompositedTransposedScrollEvents) {
// Set up with a vertical scroll bar.
ScrollView* scroll_view =
AddScrollViewWithContentSize(gfx::Size(kDefaultHeight * 5, 10));
scroll_view->SetTreatAllScrollEventsAsHorizontal(true);
ScrollViewTestApi test_api(scroll_view);
// Create a fake scroll event and send it to the scroll view.
// Note that this is still a VERTICAL scroll event, but we'll be looking for
// HORIZONTAL motion later because we're transposed.
ui::ScrollEvent scroll(ui::EventType::kScroll, gfx::Point(),
base::TimeTicks::Now(), 0, 0, -10, 0, -10, 3);
ApplyScrollEvent(test_api, scroll_view, scroll);
// Check if the scroll view has been offset.
EXPECT_EQ(gfx::PointF(10, 0), test_api.CurrentOffset());
}
// Tests to see that transposed (treat-as-horizontal) scroll events are handled
// correctly in composited and non-composited scrolling when the scroll offset
// is somewhat ambiguous. This is the case where the horizontal component is
// larger than the vertical.
TEST_F(WidgetScrollViewTest,
DISABLED_CompositedTransposedScrollEventsHorizontalComponentIsLarger) {
// Set up with a vertical scroll bar.
ScrollView* scroll_view =
AddScrollViewWithContentSize(gfx::Size(kDefaultHeight * 5, 10));
scroll_view->SetTreatAllScrollEventsAsHorizontal(true);
ScrollViewTestApi test_api(scroll_view);
// Create a fake scroll event and send it to the scroll view.
// This will be a horizontal scroll event but there will be a conflicting
// vertical element. We should still scroll horizontally, since the horizontal
// component is greater.
ui::ScrollEvent scroll(ui::EventType::kScroll, gfx::Point(),
base::TimeTicks::Now(), 0, -10, 7, -10, 7, 3);
ApplyScrollEvent(test_api, scroll_view, scroll);
// Check if the scroll view has been offset.
EXPECT_EQ(gfx::PointF(10, 0), test_api.CurrentOffset());
}
// Tests to see that transposed (treat-as-horizontal) scroll events are handled
// correctly in composited and non-composited scrolling when the scroll offset
// is somewhat ambiguous. This is the case where the vertical component is
// larger than the horizontal.
TEST_F(WidgetScrollViewTest,
CompositedTransposedScrollEventsVerticalComponentIsLarger) {
// Set up with a vertical scroll bar.
ScrollView* scroll_view =
AddScrollViewWithContentSize(gfx::Size(kDefaultHeight * 5, 10));
scroll_view->SetTreatAllScrollEventsAsHorizontal(true);
ScrollViewTestApi test_api(scroll_view);
// Create a fake scroll event and send it to the scroll view.
// This will be a vertical scroll event but there will be a conflicting
// horizontal element. We should still scroll horizontally, since the vertical
// component is greater.
ui::ScrollEvent scroll(ui::EventType::kScroll, gfx::Point(),
base::TimeTicks::Now(), 0, 7, -10, 7, -10, 3);
ApplyScrollEvent(test_api, scroll_view, scroll);
// Check if the scroll view has been offset.
EXPECT_EQ(gfx::PointF(10, 0), test_api.CurrentOffset());
}
TEST_F(WidgetScrollViewTest, UnboundedScrollViewUsesContentPreferredSize) {
auto contents = std::make_unique<View>();
constexpr gfx::Size kContentsPreferredSize(500, 500);
contents->SetPreferredSize(kContentsPreferredSize);
ScrollView* scroll_view =
AddScrollViewWithContents(std::move(contents), true);
EXPECT_EQ(kContentsPreferredSize, scroll_view->GetPreferredSize({}));
constexpr gfx::Insets kInsets(20);
scroll_view->SetBorder(CreateEmptyBorder(kInsets));
gfx::Size preferred_size_with_insets(kContentsPreferredSize);
preferred_size_with_insets.Enlarge(kInsets.width(), kInsets.height());
EXPECT_EQ(preferred_size_with_insets, scroll_view->GetPreferredSize({}));
}
INSTANTIATE_TEST_SUITE_P(All,
WidgetScrollViewTestRTLAndLayers,
::testing::Values(UiConfig::kLtr,
UiConfig::kRtl,
UiConfig::kLtrWithLayers,
UiConfig::kRtlWithLayers),
&UiConfigToString);
} // namespace views
|