1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983
|
// Copyright 2017 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/aura/window_occlusion_tracker.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/gtest_util.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/env.h"
#include "ui/aura/test/aura_test_base.h"
#include "ui/aura/test/test_window_delegate.h"
#include "ui/aura/test/test_windows.h"
#include "ui/aura/test/window_occlusion_tracker_test_api.h"
#include "ui/aura/window_observer.h"
#include "ui/aura/window_occlusion_change_builder.h"
#include "ui/base/ui_base_features.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/layer_animation_observer.h"
#include "ui/compositor/layer_animation_sequence.h"
#include "ui/compositor/layer_animator.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/compositor/scoped_layer_animation_settings.h"
#include "ui/compositor/test/layer_animator_test_controller.h"
#include "ui/gfx/interpolated_transform.h"
namespace aura {
namespace {
constexpr base::TimeDelta kTransitionDuration = base::Seconds(3);
class FakeWindowOcclusionChangeBuilder : public WindowOcclusionChangeBuilder {
public:
FakeWindowOcclusionChangeBuilder() = default;
FakeWindowOcclusionChangeBuilder(const FakeWindowOcclusionChangeBuilder&) =
delete;
FakeWindowOcclusionChangeBuilder& operator=(
const FakeWindowOcclusionChangeBuilder&) = delete;
~FakeWindowOcclusionChangeBuilder() override = default;
// WindowOcclusionChangeBuilder:
void Add(Window* window,
Window::OcclusionState occlusion_state,
SkRegion occluded_region) override {}
};
class MockWindowDelegate : public test::ColorTestWindowDelegate {
public:
MockWindowDelegate() : test::ColorTestWindowDelegate(SK_ColorWHITE) {}
MockWindowDelegate(const MockWindowDelegate&) = delete;
MockWindowDelegate& operator=(const MockWindowDelegate&) = delete;
~MockWindowDelegate() override { EXPECT_FALSE(is_expecting_call()); }
void set_window(Window* window) { window_ = window; }
Window* window() { return window_; }
void SetName(const std::string& name) { window_->SetName(name); }
void set_expectation(Window::OcclusionState occlusion_state,
SkRegion occluded_region = SkRegion()) {
expected_occlusion_state_ = occlusion_state;
expected_occluded_region_ = occluded_region;
}
bool is_expecting_call() const {
return expected_occlusion_state_ != Window::OcclusionState::UNKNOWN;
}
void OnWindowOcclusionChanged(
Window::OcclusionState old_occlusion_state,
Window::OcclusionState new_occlusion_state) override {
SCOPED_TRACE(window_->GetName());
ASSERT_TRUE(window_);
EXPECT_NE(new_occlusion_state, Window::OcclusionState::UNKNOWN);
EXPECT_EQ(new_occlusion_state, expected_occlusion_state_);
EXPECT_EQ(window_->occluded_region_in_root(), expected_occluded_region_);
expected_occlusion_state_ = Window::OcclusionState::UNKNOWN;
expected_occluded_region_ = SkRegion();
}
private:
Window::OcclusionState expected_occlusion_state_ =
Window::OcclusionState::UNKNOWN;
SkRegion expected_occluded_region_ = SkRegion();
raw_ptr<Window> window_ = nullptr;
};
class WindowOcclusionTrackerTest : public test::AuraTestBase {
public:
WindowOcclusionTrackerTest() = default;
WindowOcclusionTrackerTest(const WindowOcclusionTrackerTest&) = delete;
WindowOcclusionTrackerTest& operator=(const WindowOcclusionTrackerTest&) =
delete;
#if BUILDFLAG(IS_WIN)
void SetUp() override {
// Native Window Occlusion calculation runs in the background and can
// interfere with the expectations of these tests, so, disable it.
scoped_feature_list_.InitWithFeatures(
/*enabled_features=*/{},
/*disabled_features=*/{features::kCalculateNativeWinOcclusion});
AuraTestBase::SetUp();
}
#endif
Window* CreateTrackedWindow(
MockWindowDelegate* delegate,
const gfx::Rect& bounds,
Window* parent = nullptr,
bool transparent = false,
ui::LayerType layer_type = ui::LAYER_TEXTURED,
WindowOcclusionTracker* secondary_occlusion_tracker = nullptr) {
Window* window = new Window(delegate);
delegate->set_window(window);
window->SetType(client::WINDOW_TYPE_NORMAL);
window->Init(layer_type);
if (layer_type == ui::LAYER_SOLID_COLOR)
window->layer()->SetColor(SK_ColorBLACK);
window->SetTransparent(transparent);
window->SetBounds(bounds);
window->Show();
parent = parent ? parent : root_window();
parent->AddChild(window);
if (secondary_occlusion_tracker) {
secondary_occlusion_tracker->Track(window);
} else {
window->TrackOcclusionState();
}
return window;
}
Window* CreateUntrackedWindow(const gfx::Rect& bounds,
Window* parent = nullptr,
ui::LayerType layer_type = ui::LAYER_TEXTURED) {
if (layer_type == ui::LAYER_TEXTURED) {
return test::CreateTestWindow(SK_ColorWHITE, 1, bounds,
parent ? parent : root_window());
}
DCHECK_EQ(ui::LAYER_SOLID_COLOR, layer_type);
Window* window = new Window(nullptr);
window->SetType(client::WINDOW_TYPE_NORMAL);
window->Init(ui::LAYER_SOLID_COLOR);
window->layer()->SetColor(SK_ColorBLACK);
window->SetBounds(bounds);
root_window()->AddChild(window);
window->Show();
return window;
}
WindowOcclusionTracker& GetOcclusionTracker() {
return *Env::GetInstance()->GetWindowOcclusionTracker();
}
std::unique_ptr<WindowOcclusionTracker> CreateSecondaryOcclusionTracker() {
auto occlusion_tracker = std::make_unique<WindowOcclusionTracker>();
// Any secondary trackers should not be mutating the `aura::Window`'s
// occlusion state. That is the sole responsibility of the primary tracker
// in `aura::Env`.
occlusion_tracker->set_occlusion_change_builder_factory(base::BindRepeating(
[]() -> std::unique_ptr<WindowOcclusionChangeBuilder> {
return std::make_unique<FakeWindowOcclusionChangeBuilder>();
}));
return occlusion_tracker;
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
SkRegion SkRegionFromSkIRects(std::initializer_list<SkIRect> rects) {
SkRegion r;
r.setRects(rects.begin(), rects.size());
return r;
}
} // namespace
// Verify that non-overlapping windows have a VISIBLE occlusion state.
// _____ _____
// | | | |
// |____| |____|
TEST_F(WindowOcclusionTrackerTest, NonOverlappingWindows) {
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(15, 0, 10, 10)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_b, gfx::Rect(15, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
}
// Verify that partially overlapping windows have a VISIBLE occlusion state.
// ______
// |__| |
// |_____|
TEST_F(WindowOcclusionTrackerTest, PartiallyOverlappingWindow) {
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeWH(5, 5)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
}
// Verify that a window whose bounds are covered by a hidden window is not
// occluded. Also, verify that calling Show() on the hidden window causes
// occlusion states to be recomputed.
// __.... ... = hidden window
// |__| .
// .......
TEST_F(WindowOcclusionTrackerTest, HiddenWindowCoversWindow) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b. Expect it to be non-occluded and expect window a to be
// occluded.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 15, 15));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
// Hide window b. Expect window a to be non-occluded and window b to be
// occluded.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
delegate_b->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
window_b->Hide();
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
// Show window b. Expect window a to be occluded and window b to be non-
// occluded.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
window_b->Show();
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
}
class WindowOcclusionTrackerOpacityTest
: public WindowOcclusionTrackerTest,
public testing::WithParamInterface<bool> {
public:
WindowOcclusionTrackerOpacityTest() = default;
~WindowOcclusionTrackerOpacityTest() override = default;
WindowOcclusionTrackerOpacityTest(const WindowOcclusionTrackerOpacityTest&) =
delete;
WindowOcclusionTrackerOpacityTest& operator=(
const WindowOcclusionTrackerOpacityTest&) = delete;
// WindowOcclusionTrackerTest:
void SetUp() override {
use_solid_color_layer_ = GetParam();
WindowOcclusionTrackerTest::SetUp();
}
void SetOpacity(aura::Window* window, float opacity) {
if (use_solid_color_layer_)
window->layer()->SetColor(SkColorSetARGB(255 * opacity, 255, 255, 255));
else
window->layer()->SetOpacity(opacity);
}
ui::LayerType layer_type() const {
return use_solid_color_layer_ ? ui::LAYER_SOLID_COLOR : ui::LAYER_TEXTURED;
}
private:
bool use_solid_color_layer_;
};
// Verify that a window whose bounds are covered by a semi-transparent window is
// not occluded. Also, verify that that when the opacity of a window changes,
// occlusion states are updated.
// __.... ... = semi-transparent window
// |__| .
// .......
TEST_P(WindowOcclusionTrackerOpacityTest, SemiTransparentWindowCoversWindow) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b. Expect it to be non-occluded and expect window a to be
// occluded.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 15, 15),
nullptr, false, layer_type());
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
// Change the opacity of window b to 0.5f. Expect both windows to be non-
// occluded.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
SetOpacity(window_b, 0.5f);
EXPECT_FALSE(delegate_a->is_expecting_call());
// Change the opacity of window b back to 1.0f. Expect window a to be
// occluded.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
SetOpacity(window_b, 1.f);
EXPECT_FALSE(delegate_a->is_expecting_call());
}
// Same as previous test, but the occlusion state of the semi-transparent is not
// tracked.
TEST_P(WindowOcclusionTrackerOpacityTest,
SemiTransparentUntrackedWindowCoversWindow) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create untracked window b. Expect window a to be occluded.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
Window* window_b =
CreateUntrackedWindow(gfx::Rect(0, 0, 15, 15), nullptr, layer_type());
EXPECT_FALSE(delegate_a->is_expecting_call());
// Change the opacity of window b to 0.5f. Expect both windows to be non-
// occluded.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
SetOpacity(window_b, 0.5f);
EXPECT_FALSE(delegate_a->is_expecting_call());
// Change the opacity of window b back to 1.0f. Expect window a to be
// occluded.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
SetOpacity(window_b, 1.0f);
EXPECT_FALSE(delegate_a->is_expecting_call());
}
// Verify that one window whose bounds are covered by a set of two opaque
// windows is occluded.
// ______
// | | | <-- these two windows cover another window
// |__|__|
TEST_F(WindowOcclusionTrackerTest, TwoWindowsOccludeOneWindow) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b with bounds that partially cover window a. Expect both
// windows to be non-occluded.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeWH(5, 10)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 5, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
// Create window c with bounds that cover the portion of window a that isn't
// already covered by window b. Expect window a to be occluded and window a/b
// to be non-occluded.
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(5, 0, 5, 10)));
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_c, gfx::Rect(5, 0, 5, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
EXPECT_FALSE(delegate_c->is_expecting_call());
}
// Verify that a window and its child that are covered by a sibling are
// occluded.
TEST_F(WindowOcclusionTrackerTest, SiblingOccludesWindowAndChild) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 20, 20));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b, with bounds that occlude half of its parent window a.
// Expect it to be non-occluded.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 20), window_a);
EXPECT_FALSE(delegate_b->is_expecting_call());
// Create window c, with bounds that occlude window a and window b. Expect it
// to be non-occluded, and window a and b to be occluded.
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_b->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 20, 20));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
EXPECT_FALSE(delegate_c->is_expecting_call());
}
// Verify that a window with one half covered by a child and the other half
// covered by a sibling is non-occluded.
TEST_F(WindowOcclusionTrackerTest, ChildAndSiblingOccludeOneWindow) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 20, 20));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b, with bounds that occlude half of its parent window a.
// Expect it to be non-occluded.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 20), window_a);
EXPECT_FALSE(delegate_b->is_expecting_call());
// Create window c, with bounds that occlude the other half of window a.
// Expect it to be non-occluded and expect window a to remain non-occluded.
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(10, 0, 10, 20)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(10, 0, 10, 20)));
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_c, gfx::Rect(10, 0, 10, 20));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
EXPECT_FALSE(delegate_c->is_expecting_call());
}
// Verify that a window covered by 2 non-occluded children is non-occluded.
TEST_F(WindowOcclusionTrackerTest, ChildrenOccludeOneWindow) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 20, 20));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b, with bounds that cover half of its parent window a. Expect
// it to be non-occluded.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 20), window_a);
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window c, with bounds that cover the other half of its parent window
// a. Expect it to be non-occluded. Expect window a to remain non-occluded.
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(10, 0, 10, 20)));
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_c, gfx::Rect(10, 0, 10, 20), window_a);
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
EXPECT_FALSE(delegate_c->is_expecting_call());
}
// Verify that when the bounds of a child window covers the bounds of a parent
// window but is itself visible, the parent window is visible.
TEST_F(WindowOcclusionTrackerTest, ChildDoesNotOccludeParent) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b with window a as parent. The bounds of window b fully cover
// window a. Expect both windows to be non-occluded.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b =
CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 10), window_a);
EXPECT_FALSE(delegate_b->is_expecting_call());
// Create window c whose bounds don't overlap existing windows.
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(15, 0, 10, 10)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(15, 0, 10, 10)));
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_c = CreateTrackedWindow(delegate_c, gfx::Rect(15, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
EXPECT_FALSE(delegate_c->is_expecting_call());
// Change the parent of window b from window a to window c. Expect all windows
// to remain non-occluded.
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
window_c->AddChild(window_b);
EXPECT_FALSE(delegate_b->is_expecting_call());
}
// Verify that when the stacking order of windows change, occlusion states are
// updated.
TEST_F(WindowOcclusionTrackerTest, StackingChanged) {
// Create three windows that have the same bounds. Expect window on top of the
// stack to be non-occluded and other windows to be occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_b->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_b->is_expecting_call());
EXPECT_FALSE(delegate_c->is_expecting_call());
// Move window a on top of the stack. Expect it to be non-occluded and expect
// window c to be occluded.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
delegate_c->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
root_window()->StackChildAtTop(window_a);
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_c->is_expecting_call());
}
// Verify that when the stacking order of two transparent window changes, the
// occlusion states of their children is updated. The goal of this test is to
// ensure that the fact that the windows whose stacking order change are
// transparent doesn't prevent occlusion states from being recomputed.
TEST_F(WindowOcclusionTrackerTest, TransparentParentStackingChanged) {
// Create window a which is transparent. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10),
root_window(), true);
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b which has the same bounds as its parent window a. Expect it
// to be non-occluded.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 10), window_a);
EXPECT_FALSE(delegate_b->is_expecting_call());
// Create window c which is transparent and has the same bounds as window a
// and window b. Expect it to be non-occluded.
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_c = CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 10, 10),
root_window(), true);
EXPECT_FALSE(delegate_c->is_expecting_call());
// Create window d which has the same bounds as its parent window c. Expect
// window d to be non-occluded and window a and b to be occluded.
MockWindowDelegate* delegate_d = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_b->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_d->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_d, gfx::Rect(0, 0, 10, 10), window_c);
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
EXPECT_FALSE(delegate_d->is_expecting_call());
// Move window a on top of the stack. Expect window a and b to be non-occluded
// and window c and d to be occluded.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
delegate_c->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_d->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
root_window()->StackChildAtTop(window_a);
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
EXPECT_FALSE(delegate_c->is_expecting_call());
EXPECT_FALSE(delegate_d->is_expecting_call());
}
// Verify that when StackChildAtTop() is called on a window whose occlusion
// state is not tracked, the occlusion state of tracked siblings is updated.
TEST_F(WindowOcclusionTrackerTest, UntrackedWindowStackingChanged) {
Window* window_a = CreateUntrackedWindow(gfx::Rect(0, 0, 5, 5));
// Create window b. Expect it to be non-occluded.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 5, 5));
EXPECT_FALSE(delegate_b->is_expecting_call());
// Stack window a on top of window b. Expect window b to be occluded.
delegate_b->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
root_window()->StackChildAtTop(window_a);
EXPECT_FALSE(delegate_b->is_expecting_call());
}
// Verify that occlusion states are updated when the bounds of a window change.
TEST_F(WindowOcclusionTrackerTest, BoundsChanged) {
// Create two non-overlapping windows. Expect them to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 10, 10, 10)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
// Move window b on top of window a. Expect window a to be occluded.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
window_b->SetBounds(window_a->bounds());
EXPECT_FALSE(delegate_a->is_expecting_call());
}
// Verify that when the bounds of a window are animated, occlusion states are
// updated at the beginning and at the end of the animation, but not during the
// animation. At the beginning of the animation, the window animated window
// should be considered non-occluded and should not occlude other windows. The
// animated window starts occluded.
TEST_F(WindowOcclusionTrackerTest, OccludedWindowBoundsAnimated) {
ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
ui::LayerAnimatorTestController test_controller(
ui::LayerAnimator::CreateImplicitAnimator());
ui::ScopedLayerAnimationSettings layer_animation_settings(
test_controller.animator());
layer_animation_settings.SetTransitionDuration(kTransitionDuration);
// Create 3 windows. Window a is unoccluded. Window c occludes window b.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 10, 10, 10)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
window_b->layer()->SetAnimator(test_controller.animator());
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_b->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_c, gfx::Rect(0, 10, 10, 10));
EXPECT_FALSE(delegate_b->is_expecting_call());
EXPECT_FALSE(delegate_c->is_expecting_call());
// Start animating the bounds of window b so that it moves on top of window a.
// Window b should be non-occluded when the animation starts.
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
window_b->SetBounds(window_a->bounds());
test_controller.Step(kTransitionDuration / 3);
EXPECT_FALSE(delegate_b->is_expecting_call());
// Window a should remain non-occluded during the animation.
test_controller.Step(kTransitionDuration / 3);
// Window b should occlude window a at the end of the animation.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
// Window b should have window c in its potential occlusion region.
delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 10, 10, 10)));
test_controller.Step(kTransitionDuration / 3);
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
window_b->layer()->SetAnimator(nullptr);
}
// Same as the previous test, but the animated window starts non-occluded.
TEST_F(WindowOcclusionTrackerTest, NonOccludedWindowBoundsAnimated) {
ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
ui::LayerAnimatorTestController test_controller(
ui::LayerAnimator::CreateImplicitAnimator());
ui::ScopedLayerAnimationSettings layer_animation_settings(
test_controller.animator());
layer_animation_settings.SetTransitionDuration(kTransitionDuration);
// Create 3 windows. Window a is unoccluded. Window c occludes window b.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 10, 10, 10)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_b->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_c = CreateTrackedWindow(delegate_c, gfx::Rect(0, 10, 10, 10));
EXPECT_FALSE(delegate_b->is_expecting_call());
EXPECT_FALSE(delegate_c->is_expecting_call());
window_c->layer()->SetAnimator(test_controller.animator());
// Start animating the bounds of window c so that it moves on top of window a.
// Window b should be non-occluded when the animation starts.
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
window_c->SetBounds(window_a->bounds());
test_controller.Step(kTransitionDuration / 3);
EXPECT_FALSE(delegate_b->is_expecting_call());
// Window a should remain non-occluded during the animation.
test_controller.Step(kTransitionDuration / 3);
// Window c should occlude window a at the end of the animation.
// Window b should have a potentially occluded region including window c.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 0, 10, 10)));
test_controller.Step(kTransitionDuration / 3);
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
window_c->layer()->SetAnimator(nullptr);
}
// Verify that occlusion states are updated when the bounds of a transparent
// window with opaque children change.
TEST_F(WindowOcclusionTrackerTest, TransparentParentBoundsChanged) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b which doesn't overlap window a and is transparent. Expect
// it to be non-occluded.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 10, 10),
root_window(), true);
EXPECT_FALSE(delegate_b->is_expecting_call());
// Create window c which has window b as parent and doesn't occlude any
// window.
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 5, 5), window_b);
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_c->is_expecting_call());
// Move window b so that window c occludes window a. Expect window a to be
// occluded and other windows to be non-occluded.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
window_b->SetBounds(gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
}
// Verify that occlusion states are updated when the bounds of a window whose
// occlusion state is not tracked change.
TEST_F(WindowOcclusionTrackerTest, UntrackedWindowBoundsChanged) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b. It should not occlude window a.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
Window* window_b = CreateUntrackedWindow(gfx::Rect(0, 10, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Move window b on top of window a. Expect window a to be occluded.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
window_b->SetBounds(window_a->bounds());
EXPECT_FALSE(delegate_a->is_expecting_call());
}
// Verify that occlusion states are updated when the transform of a window
// changes.
TEST_F(WindowOcclusionTrackerTest, TransformChanged) {
// Create two non-overlapping windows. Expect them to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
// Scale and translate window b so that it covers window a. Expect window a to
// be occluded.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
gfx::Transform transform;
transform.Translate(0.0f, -10.0f);
transform.Scale(2.0f, 2.0f);
window_b->SetTransform(transform);
EXPECT_FALSE(delegate_a->is_expecting_call());
}
// Verify that when the transform of a window is animated, occlusion states are
// updated at the beginning and at the end of the animation, but not during the
// animation. At the beginning of the animation, the window animated window
// should be considered non-occluded and should not occlude other windows. The
// animated window starts occluded.
TEST_F(WindowOcclusionTrackerTest, OccludedWindowTransformAnimated) {
ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
ui::LayerAnimatorTestController test_controller(
ui::LayerAnimator::CreateImplicitAnimator());
ui::ScopedLayerAnimationSettings layer_animation_settings(
test_controller.animator());
layer_animation_settings.SetTransitionDuration(kTransitionDuration);
// Create 3 windows. Window a is unoccluded. Window c occludes window b.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
window_b->layer()->SetAnimator(test_controller.animator());
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_b->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_c, gfx::Rect(0, 10, 5, 5));
EXPECT_FALSE(delegate_b->is_expecting_call());
EXPECT_FALSE(delegate_c->is_expecting_call());
// Start animating the transform of window b so that it moves on top of window
// a. Window b should be non-occluded when the animation starts.
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
auto transform = std::make_unique<ui::InterpolatedScale>(
gfx::Point3F(1, 1, 1), gfx::Point3F(2.0f, 2.0f, 1));
transform->SetChild(std::make_unique<ui::InterpolatedTranslation>(
gfx::PointF(), gfx::PointF(-0.0f, -10.0f)));
test_controller.animator()->StartAnimation(new ui::LayerAnimationSequence(
ui::LayerAnimationElement::CreateInterpolatedTransformElement(
std::move(transform), kTransitionDuration)));
test_controller.Step(kTransitionDuration / 3);
EXPECT_FALSE(delegate_b->is_expecting_call());
// Window a should remain non-occluded during the animation.
test_controller.Step(kTransitionDuration / 3);
// Window b should occlude window a at the end of the animation.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
// Window b should see window c as part of the potential occlusion region.
delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
test_controller.Step(kTransitionDuration / 3);
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
window_b->layer()->SetAnimator(nullptr);
}
// Same as the previous test, but the animated window starts non-occluded.
TEST_F(WindowOcclusionTrackerTest, NonOccludedWindowTransformAnimated) {
ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
ui::LayerAnimatorTestController test_controller(
ui::LayerAnimator::CreateImplicitAnimator());
ui::ScopedLayerAnimationSettings layer_animation_settings(
test_controller.animator());
layer_animation_settings.SetTransitionDuration(kTransitionDuration);
// Create 3 windows. Window a is unoccluded. Window c occludes window b.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 20, 20));
EXPECT_FALSE(delegate_a->is_expecting_call());
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 20, 10, 10)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_b, gfx::Rect(0, 20, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_b->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_c = CreateTrackedWindow(delegate_c, gfx::Rect(0, 20, 10, 10));
EXPECT_FALSE(delegate_b->is_expecting_call());
EXPECT_FALSE(delegate_c->is_expecting_call());
window_c->layer()->SetAnimator(test_controller.animator());
// Start animating the bounds of window c so that it moves on top of window a.
// Window b should be non-occluded when the animation starts.
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
auto transform = std::make_unique<ui::InterpolatedScale>(
gfx::Point3F(1, 1, 1), gfx::Point3F(2.0f, 2.0f, 1));
transform->SetChild(std::make_unique<ui::InterpolatedTranslation>(
gfx::PointF(), gfx::PointF(-0.0f, -20.0f)));
test_controller.animator()->StartAnimation(new ui::LayerAnimationSequence(
ui::LayerAnimationElement::CreateInterpolatedTransformElement(
std::move(transform), kTransitionDuration)));
test_controller.Step(kTransitionDuration / 3);
EXPECT_FALSE(delegate_b->is_expecting_call());
// Window a should remain non-occluded during the animation.
test_controller.Step(kTransitionDuration / 3);
// Window c should occlude window a at the end of the animation.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
// Window b should now see window c in the potential occlusion region.
delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeWH(20, 20)));
test_controller.Step(kTransitionDuration / 3);
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
window_c->layer()->SetAnimator(nullptr);
}
// Verify that occlusion states are updated when the transform of a transparent
// window with opaque children change.
TEST_F(WindowOcclusionTrackerTest, TransparentParentTransformChanged) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b which doesn't overlap window a and is transparent. Expect
// it to be non-occluded.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 10, 10),
root_window(), true);
EXPECT_FALSE(delegate_b->is_expecting_call());
// Create window c which has window b as parent and doesn't occlude any
// window.
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 5, 5), window_b);
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_c->is_expecting_call());
// Scale and translate window b so that window c occludes window a. Expect
// window a to be occluded and other windows to be non-occluded.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
gfx::Transform transform;
transform.Translate(0.0f, -10.0f);
transform.Scale(2.0f, 2.0f);
window_b->SetTransform(transform);
EXPECT_FALSE(delegate_a->is_expecting_call());
}
// Verify that occlusion states are updated when the transform of a window whose
// occlusion state is not tracked changes.
TEST_F(WindowOcclusionTrackerTest, UntrackedWindowTransformChanged) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b. It should not occlude window a.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
Window* window_b = CreateUntrackedWindow(gfx::Rect(0, 10, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Scale and translate window b so that it occludes window a. Expect window a
// to be occluded.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
gfx::Transform transform;
transform.Translate(0.0f, -10.0f);
transform.Scale(2.0f, 2.0f);
window_b->SetTransform(transform);
EXPECT_FALSE(delegate_a->is_expecting_call());
}
// Verify that deleting an untracked window which covers a tracked window causes
// the tracked window to be non-occluded.
TEST_F(WindowOcclusionTrackerTest, DeleteUntrackedWindow) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b which occludes window a.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
Window* window_b = CreateUntrackedWindow(gfx::Rect(0, 0, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Delete window b. Expect a to be non-occluded.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
delete window_b;
EXPECT_FALSE(delegate_a->is_expecting_call());
}
// Verify that removing an untracked window which covers a tracked window causes
// the tracked window to be non-occluded.
TEST_F(WindowOcclusionTrackerTest, RemoveUntrackedWindow) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b which occludes window a.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
Window* window_b = CreateUntrackedWindow(gfx::Rect(0, 0, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Delete window b. Expect a to be non-occluded.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
root_window()->RemoveChild(window_b);
EXPECT_FALSE(delegate_a->is_expecting_call());
delete window_b;
}
// Verify that when a tracked window is removed and re-added to a root,
// occlusion states are still tracked.
TEST_F(WindowOcclusionTrackerTest, RemoveAndAddTrackedToRoot) {
Window* window_a = CreateUntrackedWindow(gfx::Rect(0, 0, 1, 1));
CreateUntrackedWindow(gfx::Rect(0, 0, 10, 10), window_a);
// Create window b. Expect it to be non-occluded.
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_c = CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 5, 5));
EXPECT_FALSE(delegate_c->is_expecting_call());
// Remove window c from its root.
root_window()->RemoveChild(window_c);
// Add window c back under its root.
root_window()->AddChild(window_c);
// Create untracked window d which covers window a. Expect window a to be
// occluded.
delegate_c->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
Window* window_d = CreateUntrackedWindow(gfx::Rect(0, 0, 5, 5));
EXPECT_FALSE(delegate_c->is_expecting_call());
// Move window d so that it doesn't cover window c.
delegate_c->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
window_d->SetBounds(gfx::Rect(0, 10, 5, 5));
EXPECT_FALSE(delegate_c->is_expecting_call());
// Stack window a on top of window c. Expect window c to be non-occluded. This
// won't work if WindowOcclusionTracked didn't register as an observer of
// window a when window c was made a child of root_window().
delegate_c->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
root_window()->StackChildAtTop(window_a);
EXPECT_FALSE(delegate_c->is_expecting_call());
}
namespace {
class ResizeWindowObserver : public WindowObserver {
public:
ResizeWindowObserver(Window* window_to_resize)
: window_to_resize_(window_to_resize) {}
ResizeWindowObserver(const ResizeWindowObserver&) = delete;
ResizeWindowObserver& operator=(const ResizeWindowObserver&) = delete;
void OnWindowBoundsChanged(Window* window,
const gfx::Rect& old_bounds,
const gfx::Rect& new_bounds,
ui::PropertyChangeReason reason) override {
window_to_resize_->SetBounds(old_bounds);
}
private:
const raw_ptr<Window> window_to_resize_;
};
} // namespace
// Verify that when the bounds of a child window are updated in response to the
// bounds of a parent window being updated, occlusion states are updated once.
TEST_F(WindowOcclusionTrackerTest, ResizeChildFromObserver) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b. Expect it to be non-occluded and to occlude window a.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
// Create window c, which is a child of window b. Expect it to be non-
// occluded.
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_c =
CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 5, 5), window_b);
EXPECT_FALSE(delegate_b->is_expecting_call());
// Create an observer that will resize window c when window b is resized.
ResizeWindowObserver resize_window_observer(window_c);
window_b->AddObserver(&resize_window_observer);
// Resize window b. Expect window c to be resized so that window a stays
// occluded. Window a should not temporarily be non-occluded.
window_b->SetBounds(gfx::Rect(0, 0, 5, 5));
window_b->RemoveObserver(&resize_window_observer);
}
// Verify that the bounds of windows are changed multiple times within the scope
// of a ScopedPause, occlusion states are updated once at the end of the scope.
TEST_F(WindowOcclusionTrackerTest, ScopedPause) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b which doesn't overlap window a. Expect it to be non
// occluded.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
// Change bounds multiple times. At the end of the scope, expect window a to
// be occluded.
{
WindowOcclusionTracker::ScopedPause pause_occlusion_tracking;
window_b->SetBounds(window_a->bounds());
window_a->SetBounds(gfx::Rect(0, 10, 5, 5));
window_b->SetBounds(window_a->bounds());
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
}
EXPECT_FALSE(delegate_a->is_expecting_call());
}
// Same as the previous test, but with nested ScopedPause.
TEST_F(WindowOcclusionTrackerTest, NestedScopedPause) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b which doesn't overlap window a. Expect it to be non-
// occluded.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 10, 5, 5)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
// Change bounds multiple times. At the end of the scope, expect window a to
// be occluded.
{
WindowOcclusionTracker::ScopedPause pause_occlusion_tracking_a;
{
WindowOcclusionTracker::ScopedPause pause_occlusion_tracking_b;
window_b->SetBounds(window_a->bounds());
}
{
WindowOcclusionTracker::ScopedPause pause_occlusion_tracking_c;
window_a->SetBounds(gfx::Rect(0, 10, 5, 5));
}
{
WindowOcclusionTracker::ScopedPause pause_occlusion_tracking_d;
window_b->SetBounds(window_a->bounds());
}
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
}
EXPECT_FALSE(delegate_a->is_expecting_call());
}
// Verify that bounds are computed correctly when a hierarchy of windows have
// transforms.
TEST_F(WindowOcclusionTrackerTest, HierarchyOfTransforms) {
gfx::Transform scale_2x_transform;
scale_2x_transform.Scale(2.0f, 2.0f);
// Effective bounds: x = 2, y = 2, height = 10, width = 10
Window* window_a = CreateUntrackedWindow(gfx::Rect(2, 2, 5, 5));
window_a->SetTransform(scale_2x_transform);
// Effective bounds: x = 4, y = 4, height = 4, width = 4
Window* window_b = CreateUntrackedWindow(gfx::Rect(1, 1, 2, 2), window_a);
// Effective bounds: x = 34, y = 36, height = 8, width = 10
CreateUntrackedWindow(gfx::Rect(15, 16, 4, 5), window_b);
MockWindowDelegate* delegate_d = new MockWindowDelegate();
delegate_d->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_d = CreateTrackedWindow(delegate_d, gfx::Rect(34, 36, 8, 10));
EXPECT_FALSE(delegate_d->is_expecting_call());
delegate_d->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
root_window()->StackChildAtBottom(window_d);
EXPECT_FALSE(delegate_d->is_expecting_call());
SkRegion occluded_area = SkRegionFromSkIRects(
{SkIRect::MakeXYWH(2, 2, 10, 10), SkIRect::MakeXYWH(4, 4, 4, 4),
SkIRect::MakeXYWH(34, 36, 8, 10)});
delegate_d->set_expectation(Window::OcclusionState::VISIBLE, occluded_area);
window_d->SetBounds(gfx::Rect(35, 36, 8, 10));
EXPECT_FALSE(delegate_d->is_expecting_call());
// |window_d| should remain non-occluded with the following bounds changes.
window_d->SetBounds(gfx::Rect(33, 36, 8, 10));
window_d->SetBounds(gfx::Rect(34, 37, 8, 10));
window_d->SetBounds(gfx::Rect(34, 35, 8, 10));
}
// Verify that clipping is taken into account when computing occlusion.
TEST_F(WindowOcclusionTrackerTest, Clipping) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b. Expect it to be non-occluded.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeWH(5, 5)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
window_b->layer()->SetMasksToBounds(true);
// Create window c which has window b as parent. Don't expect it to occlude
// window a since its bounds are clipped by window b.
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 10, 10), window_b);
EXPECT_FALSE(delegate_c->is_expecting_call());
}
// Verify that the DCHECK(!WindowIsAnimated(window)) in
// WindowOcclusionTracker::OnWindowDestroyed() doesn't fire if a window is
// destroyed with an incomplete animation (~Window should complete the animation
// and the window should be removed from |animated_windows_| before
// OnWindowDestroyed() is called).
TEST_F(WindowOcclusionTrackerTest, DestroyWindowWithPendingAnimation) {
ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
ui::LayerAnimatorTestController test_controller(
ui::LayerAnimator::CreateImplicitAnimator());
ui::ScopedLayerAnimationSettings layer_animation_settings(
test_controller.animator());
layer_animation_settings.SetTransitionDuration(kTransitionDuration);
MockWindowDelegate* delegate = new MockWindowDelegate();
delegate->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window = CreateTrackedWindow(delegate, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate->is_expecting_call());
window->layer()->SetAnimator(test_controller.animator());
// Start animating the bounds of window.
window->SetBounds(gfx::Rect(10, 10, 5, 5));
test_controller.Step(kTransitionDuration / 3);
EXPECT_TRUE(test_controller.animator()->IsAnimatingProperty(
ui::LayerAnimationElement::BOUNDS));
// Destroy the window. Expect no DCHECK failure.
delete window;
}
// Verify that `WindowOcclusionTracker` can be destroyed safely with a pending
// animation. This mostly applies to secondary `WindowOcclusionTracker`s,
// not the long-lived one in `aura::Env`.
TEST_F(WindowOcclusionTrackerTest,
DestroyOcclusionTrackerWithPendingAnimation) {
auto occlusion_tracker = CreateSecondaryOcclusionTracker();
ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
ui::LayerAnimatorTestController test_controller(
ui::LayerAnimator::CreateImplicitAnimator());
ui::ScopedLayerAnimationSettings layer_animation_settings(
test_controller.animator());
layer_animation_settings.SetTransitionDuration(kTransitionDuration);
Window* window = CreateTrackedWindow(
new MockWindowDelegate, gfx::Rect(0, 0, 10, 10), /*parent=*/nullptr,
/*transparent=*/false, ui::LAYER_TEXTURED, occlusion_tracker.get());
window->layer()->SetAnimator(test_controller.animator());
// Start animating the bounds of window.
window->SetBounds(gfx::Rect(10, 10, 5, 5));
test_controller.Step(kTransitionDuration / 3);
ASSERT_TRUE(test_controller.animator()->IsAnimatingProperty(
ui::LayerAnimationElement::BOUNDS));
// There's no explicit test expectation here other not crashing on shutdown.
occlusion_tracker.reset();
// Start animating the bounds of window again. Ensures more animations can
// be started without crashes.
test_controller.animator()->AbortAllAnimations();
window->SetBounds(gfx::Rect(20, 20, 10, 10));
test_controller.Step(kTransitionDuration / 2);
}
// Verify that an animated window stops being considered as animated when its
// layer is recreated.
TEST_F(WindowOcclusionTrackerTest, RecreateLayerOfAnimatedWindow) {
ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
ui::LayerAnimatorTestController test_controller(
ui::LayerAnimator::CreateImplicitAnimator());
ui::ScopedLayerAnimationSettings layer_animation_settings(
test_controller.animator());
layer_animation_settings.SetTransitionDuration(kTransitionDuration);
// Create 2 windows. Window b occludes window a.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(2, 2, 1, 1));
EXPECT_FALSE(delegate_a->is_expecting_call());
window_a->layer()->SetAnimator(test_controller.animator());
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
// Start animating the bounds of window a. Window a should be non-occluded
// when the animation starts.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
window_a->SetBounds(gfx::Rect(6, 6, 1, 1));
test_controller.Step(kTransitionDuration / 2);
EXPECT_FALSE(delegate_a->is_expecting_call());
// Recreate the layer of window b. Expect this to behave the same as if the
// animation was abandoned. Occlusion region should be half way between the
// animation bounds.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
std::unique_ptr<ui::Layer> old_layer = window_a->RecreateLayer();
EXPECT_FALSE(delegate_a->is_expecting_call());
window_a->layer()->SetAnimator(nullptr);
}
namespace {
class ObserverChangingWindowBounds : public WindowObserver {
public:
ObserverChangingWindowBounds() = default;
ObserverChangingWindowBounds(const ObserverChangingWindowBounds&) = delete;
ObserverChangingWindowBounds& operator=(const ObserverChangingWindowBounds&) =
delete;
// WindowObserver:
void OnWindowParentChanged(Window* window, Window* parent) override {
window->SetBounds(gfx::Rect(1, 2, 3, 4));
}
};
} // namespace
// Verify that no crash occurs if a tracked window is modified by an observer
// after it has been added to a new root but before WindowOcclusionTracker has
// been notified.
TEST_F(WindowOcclusionTrackerTest, ChangeTrackedWindowBeforeObserveAddToRoot) {
// Create a window. Expect it to be non-occluded.
MockWindowDelegate* delegate = new MockWindowDelegate();
delegate->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window = CreateTrackedWindow(delegate, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate->is_expecting_call());
// Remove the window from its root.
root_window()->RemoveChild(window);
// Add an observer that changes the bounds of |window| when it gets a new
// parent.
ObserverChangingWindowBounds observer;
window->AddObserver(&observer);
// Re-add the window to its root. Expect no crash when |observer| changes the
// bounds.
root_window()->AddChild(window);
window->RemoveObserver(&observer);
}
namespace {
class ObserverDestroyingWindowOnAnimationEnded
: public ui::LayerAnimationObserver {
public:
ObserverDestroyingWindowOnAnimationEnded(Window* window) : window_(window) {}
ObserverDestroyingWindowOnAnimationEnded(
const ObserverDestroyingWindowOnAnimationEnded&) = delete;
ObserverDestroyingWindowOnAnimationEnded& operator=(
const ObserverDestroyingWindowOnAnimationEnded&) = delete;
~ObserverDestroyingWindowOnAnimationEnded() override {
EXPECT_FALSE(window_);
}
void OnLayerAnimationEnded(ui::LayerAnimationSequence* sequence) override {
EXPECT_TRUE(window_);
delete window_;
window_ = nullptr;
}
void OnLayerAnimationAborted(ui::LayerAnimationSequence* sequence) override {}
void OnLayerAnimationScheduled(
ui::LayerAnimationSequence* sequence) override {}
private:
raw_ptr<Window, DanglingUntriaged> window_;
};
} // namespace
// Verify that no crash occurs if a LayerAnimationObserver destroys a tracked
// window before WindowOcclusionTracker is notified that the animation ended.
TEST_P(WindowOcclusionTrackerOpacityTest,
DestroyTrackedWindowFromLayerAnimationObserver) {
ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
ui::LayerAnimatorTestController test_controller(
ui::LayerAnimator::CreateImplicitAnimator());
ui::ScopedLayerAnimationSettings layer_animation_settings(
test_controller.animator());
layer_animation_settings.SetTransitionDuration(kTransitionDuration);
// Create a window. Expect it to be non-occluded.
MockWindowDelegate* delegate = new MockWindowDelegate();
delegate->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window = CreateTrackedWindow(delegate, gfx::Rect(0, 0, 10, 10),
nullptr, false, layer_type());
EXPECT_FALSE(delegate->is_expecting_call());
window->layer()->SetAnimator(test_controller.animator());
// Add a LayerAnimationObserver that destroys the window when an animation
// ends.
ObserverDestroyingWindowOnAnimationEnded observer(window);
window->layer()->GetAnimator()->AddObserver(&observer);
// Start animating the opacity of the window.
SetOpacity(window, 0.5f);
// Complete the animation. Expect no crash.
window->layer()->GetAnimator()->StopAnimating();
}
// Verify that no crash occurs if an animation completes on a non-tracked
// window's layer after the window has been removed from a root with a tracked
// window and deleted.
TEST_P(WindowOcclusionTrackerOpacityTest,
DeleteNonTrackedAnimatedWindowRemovedFromTrackedRoot) {
ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
ui::LayerAnimatorTestController test_controller(
ui::LayerAnimator::CreateImplicitAnimator());
ui::ScopedLayerAnimationSettings layer_animation_settings(
test_controller.animator());
layer_animation_settings.SetTransitionDuration(kTransitionDuration);
// Create a tracked window. Expect it to be non-occluded.
MockWindowDelegate* delegate = new MockWindowDelegate();
delegate->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate->is_expecting_call());
// Create a non-tracked window and add an observer that deletes it when its
// stops being animated.
delegate->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(10, 0, 10, 10)));
Window* window =
CreateUntrackedWindow(gfx::Rect(10, 0, 10, 10), nullptr, layer_type());
EXPECT_FALSE(delegate->is_expecting_call());
window->layer()->SetAnimator(test_controller.animator());
ObserverDestroyingWindowOnAnimationEnded observer(window);
window->layer()->GetAnimator()->AddObserver(&observer);
// Animate the window. WindowOcclusionTracker should add itself as an observer
// of its LayerAnimator (after |observer|). Upon beginning animation, the
// window should no longer affect the occluded region.
delegate->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
SetOpacity(window, 0.1);
// Drive the animation so that the color's alpha value changes.
test_controller.Step(kTransitionDuration / 2);
EXPECT_FALSE(delegate->is_expecting_call());
// Remove the non-tracked window from its root. WindowOcclusionTracker should
// remove the window from its list of animated windows and stop observing it
// and its LayerAnimator.
root_window()->RemoveChild(window);
// Complete animations on the window. |observer| will delete the window when
// it is notified that animations are complete. Expect that
// WindowOcclusionTracker will not try to access |window| after that (if it
// does, the test will crash).
window->layer()->GetAnimator()->StopAnimating();
}
TEST_P(WindowOcclusionTrackerOpacityTest,
OpacityAnimationShouldNotOccludeWindow) {
ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
ui::LayerAnimatorTestController test_controller(
ui::LayerAnimator::CreateImplicitAnimator());
ui::ScopedLayerAnimationSettings layer_animation_settings(
test_controller.animator());
layer_animation_settings.SetTransitionDuration(kTransitionDuration);
// Create a tracked window. Expect it to be non-occluded.
MockWindowDelegate* delegate = new MockWindowDelegate();
delegate->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
auto* foo = CreateTrackedWindow(delegate, gfx::Rect(0, 0, 10, 10));
foo->SetName("A");
EXPECT_FALSE(delegate->is_expecting_call());
// Create a non-tracked window which occludes the tracked window.
delegate->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
Window* window =
CreateUntrackedWindow(gfx::Rect(0, 0, 10, 10), nullptr, layer_type());
window->SetName("B");
EXPECT_FALSE(delegate->is_expecting_call());
// Set the opacity to make the tracked window VISIBLE.
delegate->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
SetOpacity(window, 0.1);
EXPECT_FALSE(delegate->is_expecting_call());
// Animate the window. WindowOcclusionTracker should add itself as an observer
// of its LayerAnimator (after |observer|). Upon beginning animation, the
// window should no longer affect the occluded region.
window->layer()->SetAnimator(test_controller.animator());
delegate->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
SetOpacity(window, 1.0);
EXPECT_TRUE(delegate->is_expecting_call());
// Drive the animation so that the color's alpha value changes.
test_controller.Step(kTransitionDuration / 2);
EXPECT_TRUE(delegate->is_expecting_call());
window->layer()->GetAnimator()->StopAnimating();
EXPECT_FALSE(delegate->is_expecting_call());
window->layer()->SetAnimator(nullptr);
}
namespace {
class WindowDelegateHidingWindowIfOccluded : public MockWindowDelegate {
public:
explicit WindowDelegateHidingWindowIfOccluded(Window* other_window)
: other_window_(other_window) {}
WindowDelegateHidingWindowIfOccluded(
const WindowDelegateHidingWindowIfOccluded&) = delete;
WindowDelegateHidingWindowIfOccluded& operator=(
const WindowDelegateHidingWindowIfOccluded&) = delete;
// MockWindowDelegate:
void OnWindowOcclusionChanged(
Window::OcclusionState old_occlusion_state,
Window::OcclusionState new_occlusion_state) override {
MockWindowDelegate::OnWindowOcclusionChanged(old_occlusion_state,
new_occlusion_state);
if (new_occlusion_state == Window::OcclusionState::HIDDEN) {
other_window_->Hide();
}
}
private:
raw_ptr<Window, DanglingUntriaged> other_window_;
};
class WindowDelegateWithQueuedExpectation : public MockWindowDelegate {
public:
WindowDelegateWithQueuedExpectation() = default;
WindowDelegateWithQueuedExpectation(
const WindowDelegateWithQueuedExpectation&) = delete;
WindowDelegateWithQueuedExpectation& operator=(
const WindowDelegateWithQueuedExpectation&) = delete;
void set_queued_expectation(Window::OcclusionState occlusion_state,
const SkRegion& occluded_region) {
queued_expected_occlusion_state_ = occlusion_state;
queued_expected_occluded_region_ = occluded_region;
}
// MockWindowDelegate:
void OnWindowOcclusionChanged(
Window::OcclusionState old_occlusion_state,
Window::OcclusionState new_occlusion_state) override {
MockWindowDelegate::OnWindowOcclusionChanged(old_occlusion_state,
new_occlusion_state);
if (queued_expected_occlusion_state_ != Window::OcclusionState::UNKNOWN) {
set_expectation(queued_expected_occlusion_state_,
queued_expected_occluded_region_);
queued_expected_occlusion_state_ = Window::OcclusionState::UNKNOWN;
queued_expected_occluded_region_ = SkRegion();
}
}
private:
Window::OcclusionState queued_expected_occlusion_state_ =
Window::OcclusionState::UNKNOWN;
SkRegion queued_expected_occluded_region_ = SkRegion();
};
} // namespace
// Verify that a window delegate can change the visibility of another window
// when it is notified that its occlusion changed.
TEST_F(WindowOcclusionTrackerTest, HideFromOnWindowOcclusionChanged) {
// Create a tracked window. Expect it to be visible.
WindowDelegateWithQueuedExpectation* delegate_a =
new WindowDelegateWithQueuedExpectation();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create a tracked window. Expect it to be visible.
MockWindowDelegate* delegate_b =
new WindowDelegateHidingWindowIfOccluded(window_a);
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(10, 0, 10, 10)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(10, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
// Hide the tracked window. It should be able to hide |window_a|. Before
// |window_a| is hidden, it will notice that the occlusion region has changed
// now that |window_b| is hidden. Then, it will be hidden by |window_b|.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
delegate_a->set_queued_expectation(Window::OcclusionState::HIDDEN,
SkRegion());
delegate_b->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
window_b->Hide();
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
EXPECT_FALSE(window_a->IsVisible());
EXPECT_FALSE(window_b->IsVisible());
}
namespace {
class WindowDelegateDeletingWindow : public MockWindowDelegate {
public:
WindowDelegateDeletingWindow() = default;
WindowDelegateDeletingWindow(const WindowDelegateDeletingWindow&) = delete;
WindowDelegateDeletingWindow& operator=(const WindowDelegateDeletingWindow&) =
delete;
void set_other_window(Window* other_window) { other_window_ = other_window; }
// MockWindowDelegate:
void OnWindowOcclusionChanged(
Window::OcclusionState old_occlusion_state,
Window::OcclusionState new_occlusion_state) override {
MockWindowDelegate::OnWindowOcclusionChanged(old_occlusion_state,
new_occlusion_state);
if (new_occlusion_state == Window::OcclusionState::OCCLUDED) {
delete other_window_;
other_window_ = nullptr;
}
}
private:
raw_ptr<Window, DanglingUntriaged> other_window_ = nullptr;
};
} // namespace
// Verify that a window can delete a window that is on top of it when it is
// notified that its occlusion changed (a crash would occur if
// WindowOcclusionTracker accessed that window after it was deleted).
TEST_F(WindowOcclusionTrackerTest, DeleteFromOnWindowOcclusionChanged) {
// Create a tracked window. Expect it to be visible.
WindowDelegateDeletingWindow* delegate_a = new WindowDelegateDeletingWindow();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create a tracked window. Expect it to be visible.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(10, 0, 10, 10)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(10, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
// Create a tracked window. Expect it to be visible.
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_a->set_expectation(
Window::OcclusionState::VISIBLE,
SkRegionFromSkIRects({SkIRect::MakeXYWH(10, 0, 10, 10),
SkIRect::MakeXYWH(20, 0, 10, 10)}));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(20, 0, 10, 10)));
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_c = CreateTrackedWindow(delegate_c, gfx::Rect(20, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
EXPECT_FALSE(delegate_c->is_expecting_call());
// |window_c| will be deleted when |window_a| is occluded.
delegate_a->set_other_window(window_c);
// Move |window_b| on top of |window_a|.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
window_b->SetBounds(window_a->bounds());
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
}
namespace {
class WindowDelegateChangingWindowVisibility : public MockWindowDelegate {
public:
WindowDelegateChangingWindowVisibility() = default;
WindowDelegateChangingWindowVisibility(
const WindowDelegateChangingWindowVisibility&) = delete;
WindowDelegateChangingWindowVisibility& operator=(
const WindowDelegateChangingWindowVisibility&) = delete;
void set_window_to_update(Window* window) { window_to_update_ = window; }
// MockWindowDelegate:
void OnWindowOcclusionChanged(
Window::OcclusionState old_occlusion_state,
Window::OcclusionState new_occlusion_state) override {
MockWindowDelegate::OnWindowOcclusionChanged(old_occlusion_state,
new_occlusion_state);
if (!window_to_update_)
return;
++num_occlusion_change_;
if (window_to_update_->IsVisible()) {
window_to_update_->Hide();
if (num_occlusion_change_ <= 3)
set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
} else {
window_to_update_->Show();
set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
}
}
private:
raw_ptr<Window> window_to_update_ = nullptr;
int num_occlusion_change_ = 0;
};
} // namespace
// Verify that if a window changes its visibility every time it is notified that
// its occlusion state changed, a DCHECK occurs.
TEST_F(WindowOcclusionTrackerTest, OcclusionStatesDontBecomeStable) {
test::WindowOcclusionTrackerTestApi test_api(
Env::GetInstance()->GetWindowOcclusionTracker());
// Create 2 superposed tracked windows.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
// Create a hidden tracked window.
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(10, 0, 10, 10)));
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_c = CreateTrackedWindow(delegate_c, gfx::Rect(10, 0, 10, 10));
EXPECT_FALSE(delegate_c->is_expecting_call());
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
delegate_c->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
window_c->Hide();
EXPECT_FALSE(delegate_c->is_expecting_call());
// Create a tracked window. Expect it to be non-occluded.
auto* delegate_d = new WindowDelegateChangingWindowVisibility();
delegate_b->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(20, 0, 10, 10)));
delegate_d->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_d = CreateTrackedWindow(delegate_d, gfx::Rect(20, 0, 10, 10));
EXPECT_FALSE(delegate_b->is_expecting_call());
EXPECT_FALSE(delegate_d->is_expecting_call());
// Store a pointer to |window_d| in |delegate_d|. This will cause a call to
// Show()/Hide() every time |delegate_d| is notified of an occlusion change.
delegate_d->set_window_to_update(window_d);
// Hide |window_d|. This will cause occlusion to be recomputed multiple times.
// Once the maximum number of times that occlusion can be recomputed is
// reached, the occlusion state of all IsVisible() windows should be set to
// VISIBLE.
EXPECT_DCHECK_DEATH({
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(20, 0, 10, 10)));
delegate_d->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
window_d->Hide();
});
}
// Verify that the occlusion states are correctly updated when a branch of the
// tree is hidden.
TEST_F(WindowOcclusionTrackerTest, HideTreeBranch) {
// Create a branch of 3 tracked windows. Expect them to be visible.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b =
CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 10, 10), window_a);
EXPECT_FALSE(delegate_b->is_expecting_call());
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_c, gfx::Rect(0, 20, 10, 10), window_b);
EXPECT_FALSE(delegate_c->is_expecting_call());
// Hide |window_b| (and hence |window_c|). Expect |window_b| and |window_c| to
// be hidden.
delegate_b->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
delegate_c->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
window_b->Hide();
EXPECT_FALSE(delegate_b->is_expecting_call());
EXPECT_FALSE(delegate_c->is_expecting_call());
}
// Verify that a window covered by a shaped window isn't considered occluded.
TEST_F(WindowOcclusionTrackerTest, WindowWithAlphaShape) {
// Create 2 superposed tracked windows.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
// Set a shape for the top window. The window underneath should no longer be
// occluded.
auto shape = std::make_unique<ui::Layer::ShapeRects>();
shape->emplace_back(0, 0, 5, 5);
// Shaped windows are not considered opaque, so the occluded region is empty.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
window_b->layer()->SetAlphaShape(std::move(shape));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Clear the shape for the top window. The window underneath should be
// occluded.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
window_b->layer()->SetAlphaShape(nullptr);
EXPECT_FALSE(delegate_a->is_expecting_call());
}
// Verify that a window covered by a window whose parent has an alpha shape
// isn't considered occluded.
TEST_F(WindowOcclusionTrackerTest, WindowWithParentAlphaShape) {
// Create a child and parent that cover another window.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 20, 20));
EXPECT_FALSE(delegate_a->is_expecting_call());
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeWH(10, 10)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
MockWindowDelegate* delegate_c = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_c->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_c, gfx::Rect(0, 0, 20, 20), window_b);
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_c->is_expecting_call());
// Set a shape for |window_b|. |window_a| and |window_b| should no longer be
// occluded.
auto shape = std::make_unique<ui::Layer::ShapeRects>();
shape->emplace_back(0, 0, 5, 5);
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
window_b->layer()->SetAlphaShape(std::move(shape));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Clear the shape for |window_b|. |window_a| and |window_b| should be
// occluded.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
window_b->layer()->SetAlphaShape(nullptr);
EXPECT_FALSE(delegate_a->is_expecting_call());
}
namespace {
class WindowDelegateHidingWindow : public MockWindowDelegate {
public:
WindowDelegateHidingWindow() = default;
WindowDelegateHidingWindow(const WindowDelegateHidingWindow&) = delete;
WindowDelegateHidingWindow& operator=(const WindowDelegateHidingWindow&) =
delete;
void set_window_to_update(Window* window) { window_to_update_ = window; }
// MockWindowDelegate:
void OnWindowOcclusionChanged(
Window::OcclusionState old_occlusion_state,
Window::OcclusionState new_occlusion_state) override {
MockWindowDelegate::OnWindowOcclusionChanged(old_occlusion_state,
new_occlusion_state);
if (!window_to_update_)
return;
window_to_update_->Hide();
}
private:
raw_ptr<Window, DanglingUntriaged> window_to_update_ = nullptr;
};
class WindowDelegateAddingAndHidingChild : public MockWindowDelegate {
public:
explicit WindowDelegateAddingAndHidingChild(WindowOcclusionTrackerTest* test)
: test_(test) {}
WindowDelegateAddingAndHidingChild(
const WindowDelegateAddingAndHidingChild&) = delete;
WindowDelegateAddingAndHidingChild& operator=(
const WindowDelegateAddingAndHidingChild&) = delete;
void set_queued_expectation(Window::OcclusionState occlusion_state,
const SkRegion& occluded_region) {
queued_expected_occlusion_state_ = occlusion_state;
queued_expected_occluded_region_ = occluded_region;
}
void set_window_to_update(Window* window) { window_to_update_ = window; }
// MockWindowDelegate:
void OnWindowOcclusionChanged(
Window::OcclusionState old_occlusion_state,
Window::OcclusionState new_occlusion_state) override {
MockWindowDelegate::OnWindowOcclusionChanged(old_occlusion_state,
new_occlusion_state);
if (queued_expected_occlusion_state_ != Window::OcclusionState::UNKNOWN) {
set_expectation(queued_expected_occlusion_state_,
queued_expected_occluded_region_);
queued_expected_occlusion_state_ = Window::OcclusionState::UNKNOWN;
queued_expected_occluded_region_ = SkRegion();
}
if (!window_to_update_)
return;
// Create a child window and hide it. Since this code runs when occlusion
// has already been recomputed twice, if one of the operations below causes
// occlusion to be recomputed, the test will fail with a DCHECK.
Window* window =
test_->CreateUntrackedWindow(gfx::Rect(0, 0, 5, 5), window_to_update_);
window->Hide();
}
private:
raw_ptr<WindowOcclusionTrackerTest> test_;
raw_ptr<Window> window_to_update_ = nullptr;
Window::OcclusionState queued_expected_occlusion_state_ =
Window::OcclusionState::UNKNOWN;
SkRegion queued_expected_occluded_region_ = SkRegion();
};
} // namespace
// Verify that hiding a window that has a hidden parent doesn't cause occlusion
// to be recomputed.
TEST_F(WindowOcclusionTrackerTest,
HideWindowWithHiddenParentOnOcclusionChange) {
test::WindowOcclusionTrackerTestApi test_api(
Env::GetInstance()->GetWindowOcclusionTracker());
auto* delegate_a = new WindowDelegateAddingAndHidingChild(this);
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
auto* delegate_b = new WindowDelegateHidingWindow();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 10, 10, 10)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(0, 10, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
// When |window_b| is hidden, it will hide |window_a|. |window_a| will in turn
// add a child to itself and hide it.
delegate_a->set_window_to_update(window_a);
delegate_b->set_window_to_update(window_a);
// Initially A is marked as visible with no potential occlusion.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
delegate_a->set_queued_expectation(Window::OcclusionState::HIDDEN,
SkRegion());
delegate_b->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
// Hiding a child to |window_a| and hiding it shouldn't cause occlusion to be
// recomputed too many times (i.e. the call below shouldn't DCHECK).
window_b->Hide();
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
}
// Verify that hiding a window changes the occlusion region to show that the
// window is fully occluded.
TEST_F(WindowOcclusionTrackerTest,
HideWindowChangesOcclusionRegionToBeFullyOccluded) {
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
window_a->Hide();
EXPECT_FALSE(delegate_a->is_expecting_call());
}
// Test partial occlusion, test partial occlusion changing hidden, alpha shape
// occlusion from multiple windows
// Verify that a window can occlude another one partially.
TEST_F(WindowOcclusionTrackerTest, WindowOccludesWindowPartially) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 20, 20));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b, occluding window a partially.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(6, 7, 8, 9)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(6, 7, 8, 9));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
// Hiding window b should stop occluding window a partially.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
delegate_b->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
window_b->Hide();
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
}
// Verify that windows with alpha shape do not affect occlusion regions.
TEST_F(WindowOcclusionTrackerTest,
WindowWithAlphaShapeDoesNotPartiallyOccludeOtherWindows) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 20, 20));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b, occluding window a partially.
MockWindowDelegate* delegate_b = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(6, 7, 8, 9)));
delegate_b->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_b = CreateTrackedWindow(delegate_b, gfx::Rect(6, 7, 8, 9));
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_b->is_expecting_call());
// Set a shape for window b. The window underneath should no longer be
// partially occluded.
auto shape = std::make_unique<ui::Layer::ShapeRects>();
shape->emplace_back(0, 0, 5, 5);
// Shaped windows are not considered opaque, so the occluded region is empty.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
window_b->layer()->SetAlphaShape(std::move(shape));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Clear the shape for the top window. The window underneath should be
// occluded.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(6, 7, 8, 9)));
window_b->layer()->SetAlphaShape(nullptr);
EXPECT_FALSE(delegate_a->is_expecting_call());
}
// Verify that a window can be occluded by multiple other windows.
TEST_F(WindowOcclusionTrackerTest, WindowCanBeOccludedByMultipleWindows) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(5, 5, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
SkRegion window_a_occlusion = SkRegion(SkIRect::MakeXYWH(14, 14, 5, 5));
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
window_a_occlusion);
CreateUntrackedWindow(gfx::Rect(14, 14, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
window_a_occlusion.op(SkIRect::MakeXYWH(1, 1, 5, 5), SkRegion::Op::kUnion_Op);
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
window_a_occlusion);
CreateUntrackedWindow(gfx::Rect(1, 1, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
window_a_occlusion.op(SkIRect::MakeXYWH(14, 1, 5, 5),
SkRegion::Op::kUnion_Op);
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
window_a_occlusion);
CreateUntrackedWindow(gfx::Rect(14, 1, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
window_a_occlusion.op(SkIRect::MakeXYWH(10, 10, 2, 3),
SkRegion::Op::kUnion_Op);
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
window_a_occlusion);
CreateUntrackedWindow(gfx::Rect(10, 10, 2, 3));
EXPECT_FALSE(delegate_a->is_expecting_call());
}
// Verify that the excluded window is indeed ignored by occlusion tracking.
TEST_P(WindowOcclusionTrackerOpacityTest, ExcludeWindow) {
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->SetName("WindowA");
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
Window* window_b = CreateUntrackedWindow(gfx::Rect(0, 0, 100, 100), nullptr);
EXPECT_FALSE(delegate_a->is_expecting_call());
MockWindowDelegate* delegate_bb = new MockWindowDelegate();
delegate_bb->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_bb, gfx::Rect(0, 0, 10, 10), window_b);
EXPECT_FALSE(delegate_bb->is_expecting_call());
delegate_bb->SetName("WindowBB");
delegate_bb->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
Window* window_c = CreateUntrackedWindow(gfx::Rect(0, 0, 100, 100), nullptr);
EXPECT_FALSE(delegate_bb->is_expecting_call());
{
// |window_b| is excluded, so its child's occlusion state becomes VISIBlE.
delegate_bb->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
EXPECT_TRUE(delegate_bb->is_expecting_call());
WindowOcclusionTracker::ScopedExclude scoped(window_b);
EXPECT_FALSE(delegate_bb->is_expecting_call());
// Moving |window_c| out from |window_a| will make |window_a| visible
// because |window_b| is ignored.
SkRegion window_a_occlusion(SkIRect::MakeXYWH(100, 100, 100, 100));
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
window_a_occlusion);
SkRegion window_bb_occlusion;
window_bb_occlusion.op(SkIRect::MakeXYWH(100, 100, 100, 100),
SkRegion::kUnion_Op);
delegate_bb->set_expectation(Window::OcclusionState::VISIBLE,
window_bb_occlusion);
window_c->SetBounds(gfx::Rect(100, 100, 100, 100));
// Un-excluding wil make |window_bb| OCCLUDED.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
delegate_bb->set_expectation(Window::OcclusionState::VISIBLE,
window_bb_occlusion);
}
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_bb->is_expecting_call());
{
delegate_bb->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
SkRegion window_a_occlusion(SkIRect::MakeXYWH(100, 100, 100, 100));
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
window_a_occlusion);
EXPECT_TRUE(delegate_bb->is_expecting_call());
WindowOcclusionTracker::ScopedExclude scoped(window_b);
EXPECT_FALSE(delegate_bb->is_expecting_call());
EXPECT_FALSE(delegate_a->is_expecting_call());
// Moving |window_b| will not affect the occlusion status.
window_b->SetBounds(gfx::Rect(5, 5, 100, 100));
// Un-excluding will update the occlustion status.
// A's occlustion status includes all windows above a.
window_a_occlusion.setEmpty();
window_a_occlusion.op(SkIRect::MakeXYWH(5, 5, 100, 100),
SkRegion::kUnion_Op);
window_a_occlusion.op(SkIRect::MakeXYWH(100, 100, 100, 100),
SkRegion::kUnion_Op);
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
window_a_occlusion);
SkRegion window_bb_occlusion(SkIRect::MakeXYWH(100, 100, 100, 100));
delegate_bb->set_expectation(Window::OcclusionState::VISIBLE,
window_bb_occlusion);
}
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_bb->is_expecting_call());
{
delegate_bb->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
SkRegion window_a_occlusion(SkIRect::MakeXYWH(100, 100, 100, 100));
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
window_a_occlusion);
EXPECT_TRUE(delegate_bb->is_expecting_call());
WindowOcclusionTracker::ScopedExclude scoped(window_b);
EXPECT_FALSE(delegate_bb->is_expecting_call());
EXPECT_FALSE(delegate_a->is_expecting_call());
// Deleting the excluded window will un-exclude itself and recomputes the
// occlustion state, but should not affect the state on existing windows
// because it's already excluded.
delete window_b;
EXPECT_FALSE(scoped.window());
}
MockWindowDelegate* delegate_d = new MockWindowDelegate();
delegate_d->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
auto* window_d = CreateTrackedWindow(delegate_d, gfx::Rect(0, 0, 10, 10),
nullptr, false, layer_type());
window_d->SetName("WindowD");
EXPECT_FALSE(delegate_a->is_expecting_call());
EXPECT_FALSE(delegate_d->is_expecting_call());
{
// Make sure excluding the tracked window also works.
SkRegion window_a_occlusion(SkIRect::MakeXYWH(100, 100, 100, 100));
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
window_a_occlusion);
WindowOcclusionTracker::ScopedExclude scoped(window_d);
EXPECT_FALSE(delegate_a->is_expecting_call());
// Changing opacity/bounds shouldn't change the occlusion state.
SetOpacity(window_d, 0.5f);
window_d->SetBounds(gfx::Rect(0, 0, 20, 20));
// A is now visible even if |window_d| is un-excluded because
// window_d is not fully opaque.
}
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
SetOpacity(window_d, 1.f);
EXPECT_FALSE(delegate_a->is_expecting_call());
}
// Test that calling OnOcclusionStateChanged on a root window causes children
// of the root window to have their delegate notified that it is occluded or
// visible, depending on whether the root window is occluded or not.
TEST_F(WindowOcclusionTrackerTest, NativeWindowOcclusion) {
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
// Make the host call OnOcclusionStateChanged on the root window.
host()->SetNativeWindowOcclusionState(Window::OcclusionState::OCCLUDED, {});
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
host()->SetNativeWindowOcclusionState(Window::OcclusionState::VISIBLE, {});
EXPECT_FALSE(delegate_a->is_expecting_call());
}
TEST_F(WindowOcclusionTrackerTest, ScopedForceVisible) {
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
window->Hide();
EXPECT_FALSE(delegate_a->is_expecting_call());
// Using ScopedForceVisible when the window is hidden should force it visible.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
{
WindowOcclusionTracker::ScopedForceVisible force_visible(window);
EXPECT_FALSE(delegate_a->is_expecting_call());
// Destroying the ScopedForceVisible should return the window to hidden.
delegate_a->set_expectation(Window::OcclusionState::HIDDEN, SkRegion());
}
EXPECT_FALSE(delegate_a->is_expecting_call());
}
TEST_F(WindowOcclusionTrackerTest, ScopedForceVisibleSiblingsIgnored) {
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
CreateUntrackedWindow(gfx::Rect(0, 0, 100, 100), nullptr);
EXPECT_FALSE(delegate_a->is_expecting_call());
// Using ScopedForceVisible when the window is occluded should force it
// visible.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
{
WindowOcclusionTracker::ScopedForceVisible force_visible(window);
EXPECT_FALSE(delegate_a->is_expecting_call());
// Destroying the ScopedForceVisible should return the window to hidden.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
}
EXPECT_FALSE(delegate_a->is_expecting_call());
}
TEST_F(WindowOcclusionTrackerTest, ScopedForceVisibleWithOccludedSibling) {
// Creates three windows, a parent with two children. Both children have
// the same bounds.
std::unique_ptr<WindowOcclusionTracker::ScopedPause>
pause_occlusion_tracking =
std::make_unique<WindowOcclusionTracker::ScopedPause>();
MockWindowDelegate* parent_delegate = new MockWindowDelegate();
Window* parent_window =
CreateTrackedWindow(parent_delegate, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(parent_delegate->is_expecting_call());
MockWindowDelegate* occluded_child_delegate = new MockWindowDelegate();
CreateTrackedWindow(occluded_child_delegate, gfx::Rect(0, 0, 10, 10),
parent_window);
EXPECT_FALSE(occluded_child_delegate->is_expecting_call());
MockWindowDelegate* visible_child_delegate = new MockWindowDelegate();
CreateTrackedWindow(visible_child_delegate, gfx::Rect(0, 0, 10, 10),
parent_window);
EXPECT_FALSE(visible_child_delegate->is_expecting_call());
// Initial state after creation.
parent_delegate->set_expectation(Window::OcclusionState::VISIBLE);
occluded_child_delegate->set_expectation(Window::OcclusionState::OCCLUDED);
visible_child_delegate->set_expectation(Window::OcclusionState::VISIBLE);
pause_occlusion_tracking.reset();
EXPECT_FALSE(parent_delegate->is_expecting_call());
EXPECT_FALSE(occluded_child_delegate->is_expecting_call());
EXPECT_FALSE(visible_child_delegate->is_expecting_call());
// Hiding the parent should result in all windows being hidden.
parent_delegate->set_expectation(Window::OcclusionState::HIDDEN);
occluded_child_delegate->set_expectation(Window::OcclusionState::HIDDEN);
visible_child_delegate->set_expectation(Window::OcclusionState::HIDDEN);
parent_window->Hide();
EXPECT_FALSE(parent_delegate->is_expecting_call());
EXPECT_FALSE(occluded_child_delegate->is_expecting_call());
EXPECT_FALSE(visible_child_delegate->is_expecting_call());
// Creating a ScopedForceVisible for the parent should return to the initial
// state.
parent_delegate->set_expectation(Window::OcclusionState::VISIBLE);
occluded_child_delegate->set_expectation(Window::OcclusionState::OCCLUDED);
visible_child_delegate->set_expectation(Window::OcclusionState::VISIBLE);
WindowOcclusionTracker::ScopedForceVisible force_visible(parent_window);
EXPECT_FALSE(parent_delegate->is_expecting_call());
EXPECT_FALSE(occluded_child_delegate->is_expecting_call());
EXPECT_FALSE(visible_child_delegate->is_expecting_call());
// Do another show, so that once the |force_visible| is destroyed the
// assertions in MockWindowDelegate aren't tripped.
parent_window->Show();
}
// Simulates a scenario in which a browser window is forced visible (e.g. while
// projecting) and its parent container (e.g. a virtual desks container) was
// hidden. Verifies that the browser window and its descendants remain visible
// from an occlusion stand point.
TEST_F(WindowOcclusionTrackerTest, ScopedForceVisibleHiddenContainer) {
std::unique_ptr<WindowOcclusionTracker::ScopedPause>
pause_occlusion_tracking =
std::make_unique<WindowOcclusionTracker::ScopedPause>();
MockWindowDelegate* root_delegate = new MockWindowDelegate();
Window* root = CreateTrackedWindow(root_delegate, gfx::Rect(0, 0, 100, 100));
MockWindowDelegate* container_delegate = new MockWindowDelegate();
Window* container =
CreateTrackedWindow(container_delegate, gfx::Rect(0, 0, 100, 100), root);
MockWindowDelegate* browser_delegate = new MockWindowDelegate();
Window* browser =
CreateTrackedWindow(browser_delegate, gfx::Rect(0, 0, 10, 10), container);
MockWindowDelegate* webcontents_delegate = new MockWindowDelegate();
Window* webcontents = CreateTrackedWindow(webcontents_delegate,
gfx::Rect(0, 0, 10, 10), browser);
root_delegate->set_expectation(Window::OcclusionState::VISIBLE);
container_delegate->set_expectation(Window::OcclusionState::VISIBLE);
browser_delegate->set_expectation(Window::OcclusionState::VISIBLE);
webcontents_delegate->set_expectation(Window::OcclusionState::VISIBLE);
pause_occlusion_tracking.reset();
EXPECT_FALSE(root_delegate->is_expecting_call());
EXPECT_FALSE(container_delegate->is_expecting_call());
EXPECT_FALSE(browser_delegate->is_expecting_call());
EXPECT_FALSE(webcontents_delegate->is_expecting_call());
WindowOcclusionTracker::ScopedForceVisible force_visible(browser);
container_delegate->set_expectation(Window::OcclusionState::HIDDEN);
container->Hide();
EXPECT_FALSE(root_delegate->is_expecting_call());
EXPECT_FALSE(container_delegate->is_expecting_call());
EXPECT_FALSE(browser_delegate->is_expecting_call());
EXPECT_FALSE(webcontents_delegate->is_expecting_call());
EXPECT_EQ(Window::OcclusionState::VISIBLE, webcontents->GetOcclusionState());
EXPECT_TRUE(webcontents->TargetVisibility());
container_delegate->set_expectation(Window::OcclusionState::VISIBLE);
container->Show();
}
TEST_F(WindowOcclusionTrackerTest, ComputeTargetOcclusionForWindow) {
auto* window_a = CreateUntrackedWindow(gfx::Rect(5, 5, 10, 10));
CreateUntrackedWindow(gfx::Rect(14, 14, 5, 5));
CreateUntrackedWindow(gfx::Rect(1, 1, 5, 5));
CreateUntrackedWindow(gfx::Rect(14, 1, 5, 5));
CreateUntrackedWindow(gfx::Rect(10, 10, 2, 3));
SkRegion window_a_occlusion = SkRegionFromSkIRects(
{SkIRect::MakeXYWH(14, 14, 5, 5), SkIRect::MakeXYWH(1, 1, 5, 5),
SkIRect::MakeXYWH(14, 1, 5, 5), SkIRect::MakeXYWH(10, 10, 2, 3)});
auto& occlusion_tracker = GetOcclusionTracker();
window_a->TrackOcclusionState();
auto occlusion_data =
occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
EXPECT_EQ(Window::OcclusionState::VISIBLE, occlusion_data.occlusion_state);
EXPECT_EQ(window_a_occlusion, occlusion_data.occluded_region);
}
TEST_F(WindowOcclusionTrackerTest,
ComputeTargetOcclusionForWindowUsesTargetBounds) {
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(10, 10, 10, 10)));
Window* window_b = CreateUntrackedWindow(gfx::Rect(10, 10, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Should be visible for target occlusion.
auto& occlusion_tracker = GetOcclusionTracker();
auto occlusion_data =
occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
EXPECT_EQ(Window::OcclusionState::VISIBLE, occlusion_data.occlusion_state);
EXPECT_EQ(SkRegion(SkIRect::MakeXYWH(10, 10, 10, 10)),
occlusion_data.occluded_region);
// Start animating |window_b| to fully occlude |window_a|.
ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
ui::LayerAnimatorTestController test_controller(
ui::LayerAnimator::CreateImplicitAnimator());
ui::ScopedLayerAnimationSettings layer_animation_settings(
test_controller.animator());
layer_animation_settings.SetTransitionDuration(kTransitionDuration);
window_b->layer()->SetAnimator(test_controller.animator());
window_b->SetBounds(gfx::Rect(0, 0, 10, 10));
// Animated windows are ignored by the occlusion tracker.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
test_controller.Step(kTransitionDuration / 3);
EXPECT_FALSE(delegate_a->is_expecting_call());
// Target occlusion should include them, however:
occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
// Don't expect the target occlusion to be affected by progress through
// the animation.
test_controller.Step(kTransitionDuration / 3);
occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
// Finish the animation, and expect the occlusion state to update.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
test_controller.Step(kTransitionDuration / 3);
window_b->layer()->SetAnimator(nullptr);
EXPECT_FALSE(delegate_a->is_expecting_call());
}
TEST_P(WindowOcclusionTrackerOpacityTest,
ComputeTargetOcclusionForWindowUsesTargetOpacity) {
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
Window* window_b =
CreateUntrackedWindow(gfx::Rect(0, 0, 10, 10), nullptr, layer_type());
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
SetOpacity(window_b, 0.0f);
EXPECT_FALSE(delegate_a->is_expecting_call());
// Should be visible for target occlusion.
auto& occlusion_tracker = GetOcclusionTracker();
auto occlusion_data =
occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
EXPECT_EQ(Window::OcclusionState::VISIBLE, occlusion_data.occlusion_state);
EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
// Start animating |window_b| to fully occlude |window_a|.
ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
ui::LayerAnimatorTestController test_controller(
ui::LayerAnimator::CreateImplicitAnimator());
ui::ScopedLayerAnimationSettings layer_animation_settings(
test_controller.animator());
layer_animation_settings.SetTransitionDuration(kTransitionDuration);
window_b->layer()->SetAnimator(test_controller.animator());
SetOpacity(window_b, 1.0f);
// Opacity animation uses threaded animation.
test_controller.StartThreadedAnimationsIfNeeded();
// Animated windows are ignored by the occlusion tracker.
test_controller.Step(kTransitionDuration / 3);
// Target occlusion should include them, however:
occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
// Don't expect the target occlusion to be affected by progress through
// the animation.
test_controller.Step(kTransitionDuration / 3);
occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
// Finish the animation, and expect the occlusion state to update.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
test_controller.Step(kTransitionDuration / 3);
// Explicitly stop animation because threaded animation may have started
// a bit later. |kTransitionDuration| may not be quite enough to reach the
// end.
window_b->layer()->GetAnimator()->StopAnimating();
window_b->layer()->SetAnimator(nullptr);
EXPECT_FALSE(delegate_a->is_expecting_call());
}
TEST_F(WindowOcclusionTrackerTest,
ComputeTargetOcclusionForWindowUsesTargetVisibility) {
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
Window* window_b = CreateUntrackedWindow(gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Hide the window.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
window_b->Hide();
EXPECT_FALSE(delegate_a->is_expecting_call());
// Should be visible for target occlusion.
auto& occlusion_tracker = GetOcclusionTracker();
auto occlusion_data =
occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
EXPECT_EQ(Window::OcclusionState::VISIBLE, occlusion_data.occlusion_state);
EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
// Start animating |window_b| to fully occlude |window_a|.
ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
ui::LayerAnimatorTestController test_controller(
ui::LayerAnimator::CreateImplicitAnimator());
ui::ScopedLayerAnimationSettings layer_animation_settings(
test_controller.animator());
layer_animation_settings.SetTransitionDuration(kTransitionDuration);
window_b->layer()->SetAnimator(test_controller.animator());
window_b->Show();
// Animated windows are ignored by the occlusion tracker.
test_controller.Step(kTransitionDuration / 3);
// Target occlusion should include them, however:
occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
// Don't expect the target occlusion to be affected by progress through
// the animation.
test_controller.Step(kTransitionDuration / 3);
occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
// Finish the animation, and expect the occlusion state to update.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
test_controller.Step(kTransitionDuration / 3);
window_b->layer()->SetAnimator(nullptr);
EXPECT_FALSE(delegate_a->is_expecting_call());
}
TEST_F(WindowOcclusionTrackerTest,
ComputeTargetOcclusionForWindowTransformHierarchy) {
gfx::Transform scale_2x_transform;
scale_2x_transform.Scale(2.0f, 2.0f);
// Effective bounds: x = 2, y = 2, height = 10, width = 10
Window* window_a = CreateUntrackedWindow(gfx::Rect(2, 2, 5, 5));
window_a->SetTransform(scale_2x_transform);
// Effective bounds: x = 4, y = 4, height = 4, width = 4
Window* window_b = CreateUntrackedWindow(gfx::Rect(1, 1, 2, 2), window_a);
// Effective bounds: x = 34, y = 36, height = 8, width = 10
CreateUntrackedWindow(gfx::Rect(15, 16, 4, 5), window_b);
Window* window_d = CreateUntrackedWindow(gfx::Rect(34, 36, 8, 10));
auto& occlusion_tracker = GetOcclusionTracker();
window_d->TrackOcclusionState();
auto occlusion_data =
occlusion_tracker.ComputeTargetOcclusionForWindow(window_d);
EXPECT_EQ(Window::OcclusionState::VISIBLE, occlusion_data.occlusion_state);
EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
root_window()->StackChildAtBottom(window_d);
occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_d);
EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
window_d->SetBounds(gfx::Rect(63, 72, 8, 10));
SkRegion occluded_area = SkRegionFromSkIRects(
{SkIRect::MakeXYWH(2, 2, 10, 10), SkIRect::MakeXYWH(4, 4, 4, 4),
SkIRect::MakeXYWH(34, 36, 8, 10)});
occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_d);
EXPECT_EQ(Window::OcclusionState::VISIBLE, occlusion_data.occlusion_state);
EXPECT_EQ(occluded_area, occlusion_data.occluded_region);
// Set a target transform on |window_b| which should increase the size of
// its child window, occluding |window_d|.
ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
ui::LayerAnimatorTestController test_controller(
ui::LayerAnimator::CreateImplicitAnimator());
ui::ScopedLayerAnimationSettings layer_animation_settings(
test_controller.animator());
layer_animation_settings.SetTransitionDuration(kTransitionDuration);
// Scale |window_b|.
// |window_b| effective bounds: 4,4 8x8
// |window_b|'s child's effective bounds: 64,68 16x20
window_b->layer()->SetAnimator(test_controller.animator());
window_b->layer()->SetTransform(scale_2x_transform);
// Transform animation uses threaded animation.
test_controller.StartThreadedAnimationsIfNeeded();
// Animated windows are ignored by the occlusion tracker.
test_controller.Step(kTransitionDuration / 3);
// Target occlusion should include them, however:
SkRegion occluded_area_transformed = SkRegionFromSkIRects(
{SkIRect::MakeXYWH(2, 2, 10, 10), SkIRect::MakeXYWH(4, 4, 8, 8),
SkIRect::MakeXYWH(64, 68, 16, 20)});
occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_d);
EXPECT_EQ(Window::OcclusionState::VISIBLE, occlusion_data.occlusion_state);
EXPECT_EQ(occluded_area_transformed, occlusion_data.occluded_region);
// Don't expect the target occlusion to be affected by progress through
// the animation.
test_controller.Step(kTransitionDuration / 3);
occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_d);
EXPECT_EQ(Window::OcclusionState::VISIBLE, occlusion_data.occlusion_state);
EXPECT_EQ(occluded_area_transformed, occlusion_data.occluded_region);
window_b->layer()->GetAnimator()->StopAnimating();
window_b->layer()->SetAnimator(nullptr);
}
TEST_F(WindowOcclusionTrackerTest, ComputeTargetOcclusionForAnimatedWindow) {
// Computing the target occlusion for an animated window should use the
// target values, and therefore compute the target occlusion for the animated
// window at its final location in this test.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(10, 10, 10, 10)));
Window* window_b = CreateUntrackedWindow(gfx::Rect(10, 10, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Start animating |window_a| to be fully occluded by |window_b|.
ui::ScopedAnimationDurationScaleMode scoped_animation_duration_scale_mode(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
ui::LayerAnimatorTestController test_controller(
ui::LayerAnimator::CreateImplicitAnimator());
ui::ScopedLayerAnimationSettings layer_animation_settings(
test_controller.animator());
layer_animation_settings.SetTransitionDuration(kTransitionDuration);
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
window_a->layer()->SetAnimator(test_controller.animator());
window_a->SetBounds(gfx::Rect(10, 10, 10, 10));
test_controller.Step(kTransitionDuration / 3);
EXPECT_FALSE(delegate_a->is_expecting_call());
// Target occlusion should take into account the final bounds.
auto& occlusion_tracker = GetOcclusionTracker();
auto occlusion_data =
occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
// Don't expect the target occlusion to be affected by progress through
// the animation.
test_controller.Step(kTransitionDuration / 3);
occlusion_data = occlusion_tracker.ComputeTargetOcclusionForWindow(window_a);
EXPECT_EQ(Window::OcclusionState::OCCLUDED, occlusion_data.occlusion_state);
EXPECT_EQ(SkRegion(), occlusion_data.occluded_region);
// Finish the animation, and expect the occlusion state to update.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
test_controller.Step(kTransitionDuration / 3);
window_b->layer()->SetAnimator(nullptr);
EXPECT_FALSE(delegate_a->is_expecting_call());
}
TEST_F(WindowOcclusionTrackerTest,
SetOpaqueRegionsForOcclusionAffectsOcclusionOfOtherWindows) {
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(10, 10, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
Window* window_b = CreateUntrackedWindow(gfx::Rect(10, 10, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Make |window_b| transparent, which should make it no longer affect
// |window_a|'s occlusion.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
window_b->SetTransparent(true);
EXPECT_FALSE(delegate_a->is_expecting_call());
// Set the opaque regions for occlusion to fully cover |window_a|. Opaque
// regions for occlusion are relative to the window.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
window_b->SetOpaqueRegionsForOcclusion({gfx::Rect(0, 0, 10, 10)});
EXPECT_FALSE(delegate_a->is_expecting_call());
// Setting the opaque regions for occlusion to an empty list should restore to
// normal behavior:
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
window_b->SetOpaqueRegionsForOcclusion({});
EXPECT_FALSE(delegate_a->is_expecting_call());
}
TEST_F(
WindowOcclusionTrackerTest,
SetOpaqueRegionsForOcclusionOfAWindowDoesNotAffectOcclusionOfThatWindowItself) {
// The opaque regions for occlusion of a window affect how that window
// occludes other windows, but should not affect occlusion for that window
// itself. This is because occluding only the opaque regions of occlusion for
// a window may still leave translucent parts of that window visible.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(5, 5, 5, 5)));
CreateUntrackedWindow(gfx::Rect(5, 5, 5, 5));
EXPECT_FALSE(delegate_a->is_expecting_call());
window_a->SetTransparent(true);
// Changing the opaque regions for occlusion should not affect how much
// |window_a| is occluded by |window_b|.
window_a->SetOpaqueRegionsForOcclusion({gfx::Rect(0, 0, 0, 0)});
window_a->SetOpaqueRegionsForOcclusion({gfx::Rect(0, 0, 1, 1)});
window_a->SetOpaqueRegionsForOcclusion({gfx::Rect(0, 0, 5, 5)});
window_a->SetOpaqueRegionsForOcclusion({});
}
TEST_F(WindowOcclusionTrackerTest,
SemiOpaqueSolidColorLayerDoesNotAffectChildOpacity) {
// Create window a. Expect it to be non-occluded.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 10, 10));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Create window b. Expect it to be non-occluded and expect window a to be
// occluded.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
Window* window_b = CreateUntrackedWindow(gfx::Rect(0, 0, 15, 15), nullptr,
ui::LAYER_SOLID_COLOR);
EXPECT_FALSE(delegate_a->is_expecting_call());
// Semi-opaque color on the window_b should make window a visible.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
window_b->layer()->SetColor(SkColorSetARGB(127, 255, 255, 255));
EXPECT_FALSE(delegate_a->is_expecting_call());
// Creating opaque layer on top of a half-opaque solid_color layer
// can occlude the window.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
Window* window_c = CreateUntrackedWindow(gfx::Rect(0, 0, 15, 15), window_b);
EXPECT_FALSE(delegate_a->is_expecting_call());
DCHECK(window_c);
// Removing the opaque layer should make the window_a visible.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
delete window_c;
EXPECT_FALSE(delegate_a->is_expecting_call());
}
TEST_F(WindowOcclusionTrackerTest, OccludedFractionalWindow) {
// Test that a window which gets a fractional scale after a transform is
// treated as its floored size when being occluded i.e. a 6.875x6.875 window
// gets occluded by a 6x6 window. Read comment on
// |WindowOcclusionTracker::RecomputeOcclusionImpl()| to understand why we do
// this.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 11, 11));
EXPECT_FALSE(delegate_a->is_expecting_call());
// 11 * 0.625 = 0.6875
window_a->SetTransform(gfx::Transform::MakeScale(0.625f));
// Since `window_a` is treated as a 6x6 window, it gets marked as occluded.
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
CreateUntrackedWindow(gfx::Rect(0, 0, 6, 6));
EXPECT_FALSE(delegate_a->is_expecting_call());
// 12 * 0.625 = 7.5
// Now `window_a` is treated as 7x7 window and thus cannot be occluded by 6x6
// window.
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 0, 6, 6)));
window_a->SetBounds(gfx::Rect(0, 0, 12, 12));
EXPECT_FALSE(delegate_a->is_expecting_call());
}
TEST_F(WindowOcclusionTrackerTest, OccludingFractionalWindow) {
// Test that a window which gets a fractional scale after a transform is
// treated as its ceiled value when occluding other windows i.e.
// a 10.625x10.625 window occludes an 11x11 window. Read comment on
// |WindowOcclusionTracker::RecomputeOcclusionImpl()| to understand why we do
// this.
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
Window* window_a = CreateTrackedWindow(delegate_a, gfx::Rect(0, 0, 11, 11));
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
Window* window_b = CreateUntrackedWindow(gfx::Rect(0, 0, 17, 17));
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
window_b->SetTransparent(true);
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
// 17 * 0.625 = 10.625
// `window_b` occludes `window_a` of size 11x11.
window_b->SetTransform(gfx::Transform::MakeScale(0.625f));
window_b->SetOpaqueRegionsForOcclusion({gfx::Rect(0, 0, 17, 17)});
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::VISIBLE,
SkRegion(SkIRect::MakeXYWH(0, 0, 11, 11)));
window_a->SetBounds(gfx::Rect(0, 0, 12, 12));
EXPECT_FALSE(delegate_a->is_expecting_call());
}
TEST_F(WindowOcclusionTrackerTest, ClipToRootWindow) {
// Test that a window larger than the root window is occluded by a window the
// same size as the root window.
auto outside_root_window_bounds = root_window()->bounds();
outside_root_window_bounds.Outset(100);
MockWindowDelegate* delegate_a = new MockWindowDelegate();
delegate_a->set_expectation(Window::OcclusionState::VISIBLE, SkRegion());
CreateTrackedWindow(delegate_a, outside_root_window_bounds);
EXPECT_FALSE(delegate_a->is_expecting_call());
delegate_a->set_expectation(Window::OcclusionState::OCCLUDED, SkRegion());
CreateUntrackedWindow(root_window()->bounds());
EXPECT_FALSE(delegate_a->is_expecting_call());
}
// Run tests with LAYER_TEXTURE_LAYER type or LAYER_SOLID_COLOR type.
INSTANTIATE_TEST_SUITE_P(All,
WindowOcclusionTrackerOpacityTest,
testing::Bool());
} // namespace aura
|