1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <optional>
#include <string>
#include <tuple>
#include <vector>
#include "ash/accessibility/accessibility_controller.h"
#include "ash/accessibility/magnifier/docked_magnifier_controller.h"
#include "ash/accessibility/magnifier/magnifier_glass.h"
#include "ash/annotator/annotation_tray.h"
#include "ash/annotator/annotations_overlay_controller.h"
#include "ash/annotator/annotator_controller.h"
#include "ash/app_list/app_list_controller_impl.h"
#include "ash/capture_mode/capture_mode_bar_view.h"
#include "ash/capture_mode/capture_mode_behavior.h"
#include "ash/capture_mode/capture_mode_constants.h"
#include "ash/capture_mode/capture_mode_controller.h"
#include "ash/capture_mode/capture_mode_menu_group.h"
#include "ash/capture_mode/capture_mode_menu_toggle_button.h"
#include "ash/capture_mode/capture_mode_metrics.h"
#include "ash/capture_mode/capture_mode_session.h"
#include "ash/capture_mode/capture_mode_session_focus_cycler.h"
#include "ash/capture_mode/capture_mode_session_test_api.h"
#include "ash/capture_mode/capture_mode_settings_test_api.h"
#include "ash/capture_mode/capture_mode_settings_view.h"
#include "ash/capture_mode/capture_mode_source_view.h"
#include "ash/capture_mode/capture_mode_test_util.h"
#include "ash/capture_mode/capture_mode_type_view.h"
#include "ash/capture_mode/capture_mode_types.h"
#include "ash/capture_mode/capture_mode_util.h"
#include "ash/capture_mode/fake_folder_selection_dialog_factory.h"
#include "ash/capture_mode/stop_recording_button_tray.h"
#include "ash/capture_mode/test_capture_mode_delegate.h"
#include "ash/capture_mode/user_nudge_controller.h"
#include "ash/capture_mode/video_recording_watcher.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/display/cursor_window_controller.h"
#include "ash/display/output_protection_delegate.h"
#include "ash/display/screen_orientation_controller_test_api.h"
#include "ash/display/window_tree_host_manager.h"
#include "ash/projector/projector_controller_impl.h"
#include "ash/projector/projector_metrics.h"
#include "ash/public/cpp/capture_mode/capture_mode_test_api.h"
#include "ash/public/cpp/holding_space/holding_space_test_api.h"
#include "ash/public/cpp/projector/projector_new_screencast_precondition.h"
#include "ash/public/cpp/projector/projector_session.h"
#include "ash/public/cpp/shelf_types.h"
#include "ash/public/cpp/shell_window_ids.h"
#include "ash/public/cpp/test/mock_projector_client.h"
#include "ash/public/cpp/test/shell_test_api.h"
#include "ash/root_window_controller.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/style/icon_button.h"
#include "ash/style/tab_slider_button.h"
#include "ash/system/status_area_widget.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/ash_test_util.h"
#include "ash/test/test_widget_builder.h"
#include "ash/wm/desks/desk.h"
#include "ash/wm/desks/desks_controller.h"
#include "ash/wm/desks/desks_test_util.h"
#include "ash/wm/overview/overview_controller.h"
#include "ash/wm/overview/overview_item.h"
#include "ash/wm/tablet_mode/tablet_mode_controller.h"
#include "ash/wm/tablet_mode/tablet_mode_controller_test_api.h"
#include "ash/wm/window_state.h"
#include "ash/wm/window_util.h"
#include "base/containers/contains.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/safe_base_name.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "chromeos/ash/services/recording/recording_service_test_api.h"
#include "chromeos/dbus/power/fake_power_manager_client.h"
#include "chromeos/dbus/power_manager/suspend.pb.h"
#include "chromeos/ui/frame/caption_buttons/frame_caption_button_container_view.h"
#include "chromeos/ui/frame/frame_header.h"
#include "components/account_id/account_id.h"
#include "components/user_manager/user_type.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "services/viz/privileged/mojom/compositing/frame_sink_video_capture.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/client/aura_constants.h"
#include "ui/aura/client/capture_client.h"
#include "ui/aura/client/capture_client_observer.h"
#include "ui/aura/client/cursor_shape_client.h"
#include "ui/aura/client/window_parenting_client.h"
#include "ui/aura/window_tracker.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/clipboard/clipboard_buffer.h"
#include "ui/base/cursor/cursor.h"
#include "ui/base/cursor/cursor_factory.h"
#include "ui/base/cursor/mojom/cursor_type.mojom-shared.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/base/mojom/window_show_state.mojom.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/layer.h"
#include "ui/compositor/scoped_animation_duration_scale_mode.h"
#include "ui/display/screen.h"
#include "ui/display/types/display_constants.h"
#include "ui/display/util/display_util.h"
#include "ui/events/event_constants.h"
#include "ui/events/event_handler.h"
#include "ui/events/test/event_generator.h"
#include "ui/gfx/geometry/dip_util.h"
#include "ui/gfx/geometry/insets.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/rect_conversions.h"
#include "ui/gfx/geometry/size_conversions.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/message_center/public/cpp/notification.h"
#include "ui/views/accessibility/view_accessibility.h"
#include "ui/views/view.h"
#include "ui/views/widget/widget.h"
#include "ui/wm/core/coordinate_conversion.h"
#include "ui/wm/core/window_modality_controller.h"
#include "ui/wm/core/window_util.h"
namespace ash {
namespace {
using ::ui::mojom::CursorType;
constexpr char kEndRecordingReasonInClamshellHistogramName[] =
"Ash.CaptureModeController.EndRecordingReason.ClamshellMode";
// Returns true if the software-composited cursor is enabled.
bool IsCursorCompositingEnabled() {
return Shell::Get()
->window_tree_host_manager()
->cursor_window_controller()
->is_cursor_compositing_enabled();
}
// Sets up a callback that will be triggered when a capture file (image or
// video) is deleted as a result of a user action. The callback will verify the
// successful deletion of the file, and will quit the given `loop`.
void SetUpFileDeletionVerifier(base::RunLoop* loop) {
DCHECK(loop);
CaptureModeTestApi().SetOnCaptureFileDeletedCallback(
base::BindLambdaForTesting(
[loop](const base::FilePath& path, bool delete_successful) {
EXPECT_TRUE(delete_successful);
base::ScopedAllowBlockingForTesting allow_blocking;
EXPECT_FALSE(base::PathExists(path));
loop->Quit();
}));
}
// Defines a capture client observer, that sets the input capture to the window
// given to the constructor, and destroys it once capture is lost.
class TestCaptureClientObserver : public aura::client::CaptureClientObserver {
public:
explicit TestCaptureClientObserver(std::unique_ptr<aura::Window> window)
: window_(std::move(window)) {
DCHECK(window_);
auto* capture_client =
aura::client::GetCaptureClient(window_->GetRootWindow());
capture_client->SetCapture(window_.get());
capture_client->AddObserver(this);
}
~TestCaptureClientObserver() override { StopObserving(); }
// aura::client::CaptureClientObserver:
void OnCaptureChanged(aura::Window* lost_capture,
aura::Window* gained_capture) override {
if (lost_capture != window_.get())
return;
StopObserving();
window_.reset();
}
private:
void StopObserving() {
if (!window_)
return;
auto* capture_client =
aura::client::GetCaptureClient(window_->GetRootWindow());
capture_client->RemoveObserver(this);
}
std::unique_ptr<aura::Window> window_;
};
} // namespace
class CaptureModeTestBase : public AshTestBase {
public:
CaptureModeTestBase() = default;
explicit CaptureModeTestBase(base::test::TaskEnvironment::TimeSource time)
: AshTestBase(time) {}
CaptureModeTestBase(const CaptureModeTestBase&) = delete;
CaptureModeTestBase& operator=(const CaptureModeTestBase&) = delete;
~CaptureModeTestBase() override = default;
// AshTestBase:
void SetUp() override {
AshTestBase::SetUp();
}
views::Widget* GetCaptureModeLabelWidget() const {
auto* session = CaptureModeController::Get()->capture_mode_session();
DCHECK(session);
return CaptureModeSessionTestApi(session).GetCaptureLabelWidget();
}
CaptureModeSettingsView* GetCaptureModeSettingsView() const {
auto* session = CaptureModeController::Get()->capture_mode_session();
DCHECK(session);
return CaptureModeSessionTestApi(session).GetCaptureModeSettingsView();
}
views::Widget* GetCaptureModeSettingsWidget() const {
auto* session = CaptureModeController::Get()->capture_mode_session();
DCHECK(session);
return CaptureModeSessionTestApi(session).GetCaptureModeSettingsWidget();
}
bool IsFolderSelectionDialogShown() const {
auto* session = CaptureModeController::Get()->capture_mode_session();
DCHECK(session);
return CaptureModeSessionTestApi(session).IsFolderSelectionDialogShown();
}
bool AreAllCaptureSessionUisVisible() const {
auto* session = CaptureModeController::Get()->capture_mode_session();
DCHECK(session);
return CaptureModeSessionTestApi(session).AreAllUisVisible();
}
aura::Window* GetDimensionsLabelWindow() const {
auto* controller = CaptureModeController::Get();
DCHECK(controller->IsActive());
auto* widget = CaptureModeSessionTestApi(controller->capture_mode_session())
.GetDimensionsLabelWidget();
return widget ? widget->GetNativeWindow() : nullptr;
}
std::optional<gfx::Point> GetMagnifierGlassCenterPoint() const {
auto* controller = CaptureModeController::Get();
DCHECK(controller->IsActive());
auto& magnifier =
CaptureModeSessionTestApi(controller->capture_mode_session())
.GetMagnifierGlass();
if (magnifier.host_widget_for_testing()) {
return magnifier.host_widget_for_testing()
->GetWindowBoundsInScreen()
.CenterPoint();
}
return std::nullopt;
}
// Start Capture Mode with source region and type image.
CaptureModeController* StartImageRegionCapture() {
return StartCaptureSession(CaptureModeSource::kRegion,
CaptureModeType::kImage);
}
CaptureModeController* StartSessionAndRecordWindow(aura::Window* window) {
auto* controller = StartCaptureSession(CaptureModeSource::kWindow,
CaptureModeType::kVideo);
GetEventGenerator()->MoveMouseToCenterOf(window);
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
return controller;
}
// Select a region by pressing and dragging the mouse.
void SelectRegion(const gfx::Rect& region_in_screen,
bool release_mouse = true) {
SelectCaptureModeRegion(GetEventGenerator(), region_in_screen,
release_mouse);
}
void WaitForSessionToEnd() {
auto* controller = CaptureModeController::Get();
if (!controller->IsActive())
return;
auto* test_delegate = static_cast<TestCaptureModeDelegate*>(
controller->delegate_for_testing());
ASSERT_TRUE(test_delegate);
base::RunLoop run_loop;
test_delegate->set_on_session_state_changed_callback(
run_loop.QuitClosure());
run_loop.Run();
ASSERT_FALSE(controller->IsActive());
}
void RemoveSecondaryDisplay() {
const int64_t primary_id = WindowTreeHostManager::GetPrimaryDisplayId();
display::ManagedDisplayInfo primary_info =
display_manager()->GetDisplayInfo(primary_id);
std::vector<display::ManagedDisplayInfo> display_info_list;
display_info_list.push_back(primary_info);
display_manager()->OnNativeDisplaysChanged(display_info_list);
// Spin the run loop so that we get a signal that the associated root window
// of the removed display is destroyed.
base::RunLoop().RunUntilIdle();
}
void SwitchToUser2() {
constexpr char kUserEmail[] = "user2@capture_mode";
SimulateUserLogin({kUserEmail});
}
void OpenSettingsView() {
auto* session = static_cast<CaptureModeSession*>(
CaptureModeController::Get()->capture_mode_session());
DCHECK(session);
ASSERT_EQ(session->session_type(), SessionType::kReal);
ClickOnView(CaptureModeSessionTestApi(session)
.GetCaptureModeBarView()
->settings_button(),
GetEventGenerator());
}
std::unique_ptr<aura::Window> CreateTransientModalChildWindow(
gfx::Rect child_window_bounds,
aura::Window* transient_parent) {
auto child = CreateTestWindow(child_window_bounds);
wm::AddTransientChild(transient_parent, child.get());
child->Show();
child->SetProperty(aura::client::kModalKey, ui::mojom::ModalType::kWindow);
wm::SetModalParent(child.get(), transient_parent);
return child;
}
void VerifyOverlayWindow(aura::Window* overlay_window,
CaptureModeSource source,
const gfx::Rect user_region) {
VerifyOverlayWindowForCaptureMode(overlay_window, GetWindowBeingRecorded(),
source, user_region);
}
aura::Window* GetWindowBeingRecorded() const {
auto* controller = CaptureModeController::Get();
DCHECK(controller->is_recording_in_progress());
return controller->video_recording_watcher_for_testing()
->window_being_recorded();
}
protected:
// Used in other fixtures to more easily initialise Sunfish and Scanner
// features.
void InitFeatures(bool sunfish_enabled, bool scanner_enabled) {
std::vector<base::test::FeatureRef> enabled_features;
std::vector<base::test::FeatureRef> disabled_features;
(sunfish_enabled ? enabled_features : disabled_features)
.push_back(features::kSunfishFeature);
(scanner_enabled ? enabled_features : disabled_features)
.push_back(features::kScannerUpdate);
(scanner_enabled ? enabled_features : disabled_features)
.push_back(features::kScannerDogfood);
feature_list_.InitWithFeatures(enabled_features, disabled_features);
}
base::test::ScopedFeatureList feature_list_;
};
std::string SunfishScannerTestName(bool sunfish_enabled, bool scanner_enabled) {
return base::StrCat({"Sunfish", sunfish_enabled ? "Enabled" : "Disabled",
"Scanner", scanner_enabled ? "Enabled" : "Disabled"});
}
std::string SourceSunfishScannerTestName(CaptureModeSource source,
bool sunfish_enabled,
bool scanner_enabled) {
std::string test_name;
switch (source) {
case CaptureModeSource::kFullscreen:
test_name = "Fullscreen";
break;
case CaptureModeSource::kRegion:
test_name = "Region";
break;
case CaptureModeSource::kWindow:
test_name = "Window";
break;
}
test_name += SunfishScannerTestName(sunfish_enabled, scanner_enabled);
return test_name;
}
class CaptureModeTest
: public CaptureModeTestBase,
public testing::WithParamInterface<std::tuple<bool, bool>> {
public:
CaptureModeTest() = default;
explicit CaptureModeTest(base::test::TaskEnvironment::TimeSource time)
: CaptureModeTestBase(time) {}
// CaptureModeTestBase:
void SetUp() override {
auto [sunfish_enabled, scanner_enabled] = GetParam();
InitFeatures(sunfish_enabled, scanner_enabled);
CaptureModeTestBase::SetUp();
}
};
INSTANTIATE_TEST_SUITE_P(
,
CaptureModeTest,
testing::Combine(testing::Bool(), testing::Bool()),
[](const testing::TestParamInfo<CaptureModeTest::ParamType>& info) {
bool sunfish_enabled = std::get<0>(info.param);
bool scanner_enabled = std::get<1>(info.param);
return SunfishScannerTestName(sunfish_enabled, scanner_enabled);
});
class CaptureSessionWidgetClosed {
public:
explicit CaptureSessionWidgetClosed(views::Widget* widget) {
DCHECK(widget);
widget_ = widget->GetWeakPtr();
}
CaptureSessionWidgetClosed(const CaptureSessionWidgetClosed&) = delete;
CaptureSessionWidgetClosed& operator=(const CaptureSessionWidgetClosed&) =
delete;
~CaptureSessionWidgetClosed() = default;
bool GetWidgetClosed() const { return !widget_ || widget_->IsClosed(); }
private:
base::WeakPtr<views::Widget> widget_;
};
TEST_P(CaptureModeTest, StartStop) {
auto* controller = CaptureModeController::Get();
controller->Start(CaptureModeEntryType::kQuickSettings);
EXPECT_TRUE(controller->IsActive());
// Calling start again is a no-op.
controller->Start(CaptureModeEntryType::kQuickSettings);
EXPECT_TRUE(controller->IsActive());
// Closing the session should close the native window of capture mode bar
// immediately.
auto* bar_window = GetCaptureModeBarWidget()->GetNativeWindow();
aura::WindowTracker tracker({bar_window});
controller->Stop();
EXPECT_TRUE(tracker.windows().empty());
EXPECT_FALSE(controller->IsActive());
}
TEST_P(CaptureModeTest, CheckCursorVisibility) {
// Hide cursor before entering capture mode.
auto* cursor_manager = Shell::Get()->cursor_manager();
cursor_manager->SetCursor(CursorType::kPointer);
cursor_manager->HideCursor();
cursor_manager->DisableMouseEvents();
EXPECT_FALSE(cursor_manager->IsCursorVisible());
auto* controller = CaptureModeController::Get();
controller->Start(CaptureModeEntryType::kQuickSettings);
// After capture mode initialization, cursor should be visible.
EXPECT_TRUE(cursor_manager->IsCursorVisible());
EXPECT_TRUE(cursor_manager->IsMouseEventsEnabled());
// Enter tablet mode.
SwitchToTabletMode();
// After entering tablet mode, cursor should be invisible and locked.
EXPECT_FALSE(cursor_manager->IsCursorVisible());
EXPECT_TRUE(cursor_manager->IsCursorLocked());
// Leave tablet mode, cursor should be visible again.
LeaveTabletMode();
EXPECT_TRUE(cursor_manager->IsCursorVisible());
}
TEST_P(CaptureModeTest, CheckCursorVisibilityOnTabletMode) {
auto* cursor_manager = Shell::Get()->cursor_manager();
// Enter tablet mode.
SwitchToTabletMode();
// After entering tablet mode, cursor should be invisible.
EXPECT_FALSE(cursor_manager->IsCursorVisible());
// Open capture mode.
auto* controller = CaptureModeController::Get();
controller->Start(CaptureModeEntryType::kQuickSettings);
// Cursor should be invisible since it's still in tablet mode.
EXPECT_FALSE(cursor_manager->IsCursorVisible());
// Leave tablet mode, cursor should be visible now.
LeaveTabletMode();
EXPECT_TRUE(cursor_manager->IsCursorVisible());
}
// Regression test for https://crbug.com/1172425.
TEST_P(CaptureModeTest, NoCrashOnClearingCapture) {
TestCaptureClientObserver observer(CreateTestWindow(gfx::Rect(200, 200)));
auto* controller = StartImageRegionCapture();
EXPECT_TRUE(controller->IsActive());
}
TEST_P(CaptureModeTest, CheckWidgetClosed) {
auto* controller = CaptureModeController::Get();
controller->Start(CaptureModeEntryType::kQuickSettings);
EXPECT_TRUE(controller->IsActive());
EXPECT_TRUE(GetCaptureModeBarWidget());
CaptureSessionWidgetClosed observer(GetCaptureModeBarWidget());
EXPECT_FALSE(observer.GetWidgetClosed());
controller->Stop();
EXPECT_FALSE(controller->IsActive());
EXPECT_FALSE(controller->capture_mode_session());
// The Widget should have been destroyed by now.
EXPECT_TRUE(observer.GetWidgetClosed());
}
TEST_P(CaptureModeTest, StartWithMostRecentTypeAndSource) {
auto* controller = CaptureModeController::Get();
controller->SetSource(CaptureModeSource::kFullscreen);
controller->SetType(CaptureModeType::kVideo);
controller->Start(CaptureModeEntryType::kQuickSettings);
EXPECT_TRUE(controller->IsActive());
EXPECT_FALSE(GetImageToggleButton()->selected());
EXPECT_TRUE(GetVideoToggleButton()->selected());
EXPECT_TRUE(GetFullscreenToggleButton()->selected());
EXPECT_FALSE(GetRegionToggleButton()->selected());
EXPECT_FALSE(GetWindowToggleButton()->selected());
ClickOnView(GetCloseButton(), GetEventGenerator());
EXPECT_FALSE(controller->IsActive());
}
TEST_P(CaptureModeTest, AccessibleCheckedState) {
auto* controller = CaptureModeController::Get();
controller->Start(CaptureModeEntryType::kQuickSettings);
ui::AXNodeData data;
GetImageToggleButton()->SetSelected(true);
GetImageToggleButton()->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(data.GetCheckedState(), ax::mojom::CheckedState::kTrue);
data = ui::AXNodeData();
GetImageToggleButton()->SetSelected(false);
GetImageToggleButton()->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(data.GetCheckedState(), ax::mojom::CheckedState::kFalse);
}
TEST_P(CaptureModeTest, ChangeTypeAndSourceFromUI) {
auto* controller = CaptureModeController::Get();
controller->Start(CaptureModeEntryType::kQuickSettings);
EXPECT_TRUE(controller->IsActive());
EXPECT_TRUE(GetImageToggleButton()->selected());
EXPECT_FALSE(GetVideoToggleButton()->selected());
auto* event_generator = GetEventGenerator();
ClickOnView(GetVideoToggleButton(), event_generator);
EXPECT_FALSE(GetImageToggleButton()->selected());
EXPECT_TRUE(GetVideoToggleButton()->selected());
EXPECT_EQ(controller->type(), CaptureModeType::kVideo);
ClickOnView(GetWindowToggleButton(), event_generator);
EXPECT_FALSE(GetFullscreenToggleButton()->selected());
EXPECT_FALSE(GetRegionToggleButton()->selected());
EXPECT_TRUE(GetWindowToggleButton()->selected());
EXPECT_EQ(controller->source(), CaptureModeSource::kWindow);
ClickOnView(GetFullscreenToggleButton(), event_generator);
EXPECT_TRUE(GetFullscreenToggleButton()->selected());
EXPECT_FALSE(GetRegionToggleButton()->selected());
EXPECT_FALSE(GetWindowToggleButton()->selected());
EXPECT_EQ(controller->source(), CaptureModeSource::kFullscreen);
}
TEST_P(CaptureModeTest, VideoRecordingUiBehavior) {
// Start Capture Mode in a fullscreen video recording mode.
CaptureModeController* controller = StartCaptureSession(
CaptureModeSource::kFullscreen, CaptureModeType::kVideo);
EXPECT_TRUE(controller->IsActive());
EXPECT_FALSE(controller->is_recording_in_progress());
EXPECT_FALSE(IsCursorCompositingEnabled());
// Hit Enter to begin recording.
auto* event_generator = GetEventGenerator();
SendKey(ui::VKEY_RETURN, event_generator);
EXPECT_EQ(CursorType::kPointer,
Shell::Get()->cursor_manager()->GetCursor().type());
WaitForRecordingToStart();
EXPECT_FALSE(controller->IsActive());
EXPECT_TRUE(controller->is_recording_in_progress());
// The composited cursor should remain disabled now that we're using the
// cursor overlay on the capturer. The stop-recording button should show up in
// the status area widget.
EXPECT_FALSE(IsCursorCompositingEnabled());
auto* stop_recording_button = Shell::GetPrimaryRootWindowController()
->GetStatusAreaWidget()
->stop_recording_button_tray();
EXPECT_TRUE(stop_recording_button->visible_preferred());
// End recording via the stop-recording button. Expect that it's now hidden.
base::HistogramTester histogram_tester;
ClickOnView(stop_recording_button, event_generator);
EXPECT_FALSE(stop_recording_button->visible_preferred());
EXPECT_FALSE(controller->is_recording_in_progress());
EXPECT_FALSE(IsCursorCompositingEnabled());
histogram_tester.ExpectBucketCount(
kEndRecordingReasonInClamshellHistogramName,
EndRecordingReason::kStopRecordingButton, 1);
}
TEST_P(CaptureModeTest, NoCrashOnMultipleClicksOnStopRecordingButton) {
ash::CaptureModeTestApi test_api;
test_api.StartForFullscreen(/*for_video=*/true);
test_api.PerformCapture();
test_api.FlushRecordingServiceForTesting();
auto* stop_recording_button = Shell::GetPrimaryRootWindowController()
->GetStatusAreaWidget()
->stop_recording_button_tray();
EXPECT_TRUE(stop_recording_button->visible_preferred());
// Use slow animations so that the stop recording button takes much longer to
// hide, so it's easier to repro the crash at http://b/270625738.
ui::ScopedAnimationDurationScaleMode animation_scale(
ui::ScopedAnimationDurationScaleMode::SLOW_DURATION);
LeftClickOn(stop_recording_button);
test_api.FlushRecordingServiceForTesting();
// There should be no crash on the second click.
LeftClickOn(stop_recording_button);
}
// Tests the behavior of repositioning a region with capture mode.
TEST_P(CaptureModeTest, CaptureRegionRepositionBehavior) {
// Use a set display size as we will be choosing points in this test.
UpdateDisplay("800x700");
auto* controller = StartImageRegionCapture();
// The first time selecting a region, the region is a default rect.
EXPECT_EQ(gfx::Rect(), controller->user_capture_region());
// Press down and drag to select a region.
SelectRegion(gfx::Rect(100, 100, 600, 600));
// Click somewhere in the center on the region and drag. The whole region
// should move. Note that the point cannot be in the capture button bounds,
// which is located in the center of the region.
auto* event_generator = GetEventGenerator();
event_generator->set_current_screen_location(gfx::Point(200, 200));
event_generator->DragMouseBy(-50, -50);
EXPECT_EQ(gfx::Rect(50, 50, 600, 600), controller->user_capture_region());
// Try to drag the region offscreen. The region should be bound by the display
// size.
event_generator->set_current_screen_location(gfx::Point(100, 100));
event_generator->DragMouseBy(-150, -150);
EXPECT_EQ(gfx::Rect(600, 600), controller->user_capture_region());
}
// Tests the behavior of resizing a region with capture mode using the corner
// drag affordances.
TEST_P(CaptureModeTest, CaptureRegionCornerResizeBehavior) {
// Use a set display size as we will be choosing points in this test.
UpdateDisplay("800x700");
auto* controller = StartImageRegionCapture();
// Create the initial region.
const gfx::Rect target_region(gfx::Rect(200, 200, 400, 400));
SelectRegion(target_region);
// For each corner point try dragging to several points and verify that the
// capture region is as expected.
struct {
std::string trace;
gfx::Point drag_point;
// The point that stays the same while dragging. It is the opposite vertex
// to |drag_point| on |target_region|.
gfx::Point anchor_point;
} kDragCornerCases[] = {
{"origin", target_region.origin(), target_region.bottom_right()},
{"top_right", target_region.top_right(), target_region.bottom_left()},
{"bottom_right", target_region.bottom_right(), target_region.origin()},
{"bottom_left", target_region.bottom_left(), target_region.top_right()},
};
// The test corner points are one in each corner outside |target_region| and
// one point inside |target_region|.
auto drag_test_points = {gfx::Point(100, 100), gfx::Point(700, 100),
gfx::Point(700, 700), gfx::Point(100, 700),
gfx::Point(400, 400)};
auto* event_generator = GetEventGenerator();
for (auto test_case : kDragCornerCases) {
SCOPED_TRACE(test_case.trace);
event_generator->set_current_screen_location(test_case.drag_point);
event_generator->PressLeftButton();
// At each drag test point, the region rect should be the rect created by
// the given |corner_point| and the drag test point. That is, the width
// should match the x distance between the two points, the height should
// match the y distance between the two points and that both points are
// contained in the region.
for (auto drag_test_point : drag_test_points) {
event_generator->MoveMouseTo(drag_test_point);
gfx::Rect region = controller->user_capture_region();
const gfx::Vector2d distance = test_case.anchor_point - drag_test_point;
EXPECT_EQ(std::abs(distance.x()), region.width());
EXPECT_EQ(std::abs(distance.y()), region.height());
// gfx::Rect::Contains returns the point (x+width, y+height) as false, so
// make the region one unit bigger to account for this.
region.Inset(gfx::Insets(-1));
EXPECT_TRUE(region.Contains(drag_test_point));
EXPECT_TRUE(region.Contains(test_case.anchor_point));
}
// Make sure the region is reset for the next iteration.
event_generator->MoveMouseTo(test_case.drag_point);
event_generator->ReleaseLeftButton();
ASSERT_EQ(target_region, controller->user_capture_region());
}
}
// Tests the behavior of resizing a region with capture mode using the edge drag
// affordances.
TEST_P(CaptureModeTest, CaptureRegionEdgeResizeBehavior) {
// Use a set display size as we will be choosing points in this test.
UpdateDisplay("800x700");
auto* controller = StartImageRegionCapture();
// Create the initial region.
const gfx::Rect target_region(gfx::Rect(200, 200, 200, 200));
SelectRegion(target_region);
// For each edge point try dragging to several points and verify that the
// capture region is as expected.
struct DragEdgeCase {
std::string trace;
gfx::Point drag_point;
// True if horizontal direction (left, right). Height stays the same while
// dragging if true, width stays the same while dragging if false.
bool horizontal;
// The edge that stays the same while dragging. It is the opposite edge to
// |drag_point|. For example, if |drag_point| is the left center of
// |target_region|, then |anchor_edge| is the right edge.
int anchor_edge;
};
// Cases where the drag starts in the center of the edge, i.e., at the
// indicator circles.
std::vector<DragEdgeCase> drag_edge_cases = {
{"left", target_region.left_center(), true, target_region.right()},
{"top", target_region.top_center(), false, target_region.bottom()},
{"right", target_region.right_center(), true, target_region.x()},
{"bottom", target_region.bottom_center(), false, target_region.y()},
};
// Append cases where the drag starts along the edge but not at the circles.
std::vector<DragEdgeCase> offset_cases = {};
for (auto center_case : drag_edge_cases) {
DragEdgeCase new_case(center_case);
center_case.horizontal ? new_case.drag_point.Offset(0, 25)
: new_case.drag_point.Offset(25, 0);
offset_cases.push_back(new_case);
}
drag_edge_cases.insert(drag_edge_cases.end(), offset_cases.begin(),
offset_cases.end());
// Drag to a couple of points that change both x and y. In all these cases,
// only the width or height should change.
auto drag_test_points = {gfx::Point(150, 150), gfx::Point(350, 350),
gfx::Point(450, 450)};
auto* event_generator = GetEventGenerator();
for (auto test_case : drag_edge_cases) {
SCOPED_TRACE(test_case.trace);
event_generator->set_current_screen_location(test_case.drag_point);
event_generator->PressLeftButton();
for (auto drag_test_point : drag_test_points) {
event_generator->MoveMouseTo(drag_test_point);
const gfx::Rect region = controller->user_capture_region();
// One of width/height will always be the same as |target_region|'s
// initial width/height, depending on the edge affordance. The other
// dimension will be the distance from |drag_test_point| to the anchor
// edge.
const int variable_length = std::abs(
(test_case.horizontal ? drag_test_point.x() : drag_test_point.y()) -
test_case.anchor_edge);
const int expected_width =
test_case.horizontal ? variable_length : target_region.width();
const int expected_height =
test_case.horizontal ? target_region.height() : variable_length;
EXPECT_EQ(expected_width, region.width());
EXPECT_EQ(expected_height, region.height());
}
// Make sure the region is reset for the next iteration.
event_generator->MoveMouseTo(test_case.drag_point);
event_generator->ReleaseLeftButton();
ASSERT_EQ(target_region, controller->user_capture_region());
}
}
// Tests that the capture region persists after exiting and reentering capture
// mode.
TEST_P(CaptureModeTest, CaptureRegionPersistsAfterExit) {
auto* controller = StartImageRegionCapture();
const gfx::Rect region(100, 100, 200, 200);
SelectRegion(region);
controller->Stop();
controller->Start(CaptureModeEntryType::kQuickSettings);
EXPECT_EQ(region, controller->user_capture_region());
}
// Tests that the capture region resets when clicking outside the current
// capture regions bounds.
TEST_P(CaptureModeTest, CaptureRegionResetsOnClickOutside) {
auto* controller = StartImageRegionCapture();
SelectRegion(gfx::Rect(100, 100, 200, 200));
// Click on an area outside of the current capture region. The capture region
// should reset to default rect.
auto* event_generator = GetEventGenerator();
event_generator->set_current_screen_location(gfx::Point(400, 400));
event_generator->ClickLeftButton();
EXPECT_EQ(gfx::Rect(), controller->user_capture_region());
}
// Tests that buttons on the capture mode bar still work when a region is
// "covering" them.
TEST_P(CaptureModeTest, CaptureRegionCoversCaptureModeBar) {
auto [sunfish_enabled, scanner_enabled] = GetParam();
if (sunfish_enabled || scanner_enabled) {
// This test fails when either Sunfish or Scanner is enabled.
// TODO: b/381965299 - Fix these test failures.
GTEST_SKIP();
}
UpdateDisplay("800x700");
auto* controller = StartImageRegionCapture();
// Select a region such that the capture mode bar is covered.
SelectRegion(gfx::Rect(5, 5, 795, 695));
EXPECT_TRUE(controller->user_capture_region().Contains(
GetCaptureModeBarView()->GetBoundsInScreen()));
// Click on the fullscreen toggle button to verify that we enter fullscreen
// capture mode. Then click on the region toggle button to verify that we
// reenter region capture mode and that the region is still covering the
// capture mode bar.
auto* event_generator = GetEventGenerator();
ClickOnView(GetFullscreenToggleButton(), event_generator);
EXPECT_EQ(CaptureModeSource::kFullscreen, controller->source());
ClickOnView(GetRegionToggleButton(), GetEventGenerator());
ASSERT_EQ(CaptureModeSource::kRegion, controller->source());
ASSERT_TRUE(controller->user_capture_region().Contains(
GetCaptureModeBarView()->GetBoundsInScreen()));
ClickOnView(GetCloseButton(), event_generator);
EXPECT_FALSE(controller->IsActive());
}
// Tests that the magnifying glass appears while fine tuning the capture region,
// and that the cursor is hidden if the magnifying glass is present.
TEST_P(CaptureModeTest, CaptureRegionMagnifierWhenFineTuning) {
const gfx::Vector2d kDragDelta(50, 50);
UpdateDisplay("800x700");
// Start Capture Mode in a region in image mode.
StartImageRegionCapture();
// Press down and drag to select a region. The magnifier should not be
// visible yet.
gfx::Rect capture_region{200, 200, 400, 400};
SelectRegion(capture_region);
EXPECT_EQ(std::nullopt, GetMagnifierGlassCenterPoint());
auto check_magnifier_shows_properly = [this](const gfx::Point& origin,
const gfx::Point& destination,
bool should_show_magnifier) {
// If |should_show_magnifier|, check that the magnifying glass is centered
// on the mouse after press and during drag, and that the cursor is hidden.
// If not |should_show_magnifier|, check that the magnifying glass never
// shows. Should always be not visible when mouse button is released.
auto* event_generator = GetEventGenerator();
std::optional<gfx::Point> expected_origin =
should_show_magnifier ? std::make_optional(origin) : std::nullopt;
std::optional<gfx::Point> expected_destination =
should_show_magnifier ? std::make_optional(destination) : std::nullopt;
auto* cursor_manager = Shell::Get()->cursor_manager();
EXPECT_TRUE(cursor_manager->IsCursorVisible());
// Move cursor to |origin| and click.
event_generator->set_current_screen_location(origin);
event_generator->PressLeftButton();
EXPECT_EQ(expected_origin, GetMagnifierGlassCenterPoint());
EXPECT_NE(should_show_magnifier, cursor_manager->IsCursorVisible());
// Drag to |destination| while holding left button.
event_generator->MoveMouseTo(destination);
EXPECT_EQ(expected_destination, GetMagnifierGlassCenterPoint());
EXPECT_NE(should_show_magnifier, cursor_manager->IsCursorVisible());
// Drag back to |origin| while still holding left button.
event_generator->MoveMouseTo(origin);
EXPECT_EQ(expected_origin, GetMagnifierGlassCenterPoint());
EXPECT_NE(should_show_magnifier, cursor_manager->IsCursorVisible());
// Release left button.
event_generator->ReleaseLeftButton();
EXPECT_EQ(std::nullopt, GetMagnifierGlassCenterPoint());
EXPECT_TRUE(cursor_manager->IsCursorVisible());
};
// Drag the capture region from within the existing selected region. The
// magnifier should not be visible at any point.
check_magnifier_shows_properly(gfx::Point(400, 250), gfx::Point(500, 350),
/*should_show_magnifier=*/false);
// Check that each corner fine tune position shows the magnifier when
// dragging.
struct {
std::string trace;
FineTunePosition position;
} kFineTunePositions[] = {
{"top_left_vertex", FineTunePosition::kTopLeftVertex},
{"top_right_vertex", FineTunePosition::kTopRightVertex},
{"bottom_right_vertex", FineTunePosition::kBottomRightVertex},
{"bottom_left_vertex", FineTunePosition::kBottomLeftVertex}};
for (const auto& fine_tune_position : kFineTunePositions) {
SCOPED_TRACE(fine_tune_position.trace);
const gfx::Point drag_affordance_location =
capture_mode_util::GetLocationForFineTunePosition(
capture_region, fine_tune_position.position);
check_magnifier_shows_properly(drag_affordance_location,
drag_affordance_location + kDragDelta,
/*should_show_magnifier=*/true);
}
}
// Tests that the dimensions label properly renders for capture regions.
TEST_P(CaptureModeTest, CaptureRegionDimensionsLabelLocation) {
UpdateDisplay("900x800");
// Start Capture Mode in a region in image mode.
StartImageRegionCapture();
// Press down and don't move the mouse. Label shouldn't display for empty
// capture regions.
auto* generator = GetEventGenerator();
generator->set_current_screen_location(gfx::Point(0, 0));
generator->PressLeftButton();
auto* controller = CaptureModeController::Get();
EXPECT_TRUE(controller->IsActive());
EXPECT_TRUE(controller->user_capture_region().IsEmpty());
EXPECT_EQ(nullptr, GetDimensionsLabelWindow());
generator->ReleaseLeftButton();
// Press down and drag to select a large region. Verify that the dimensions
// label is centered and that the label is below the capture region.
gfx::Rect capture_region{100, 100, 600, 200};
SelectRegion(capture_region, /*release_mouse=*/false);
EXPECT_EQ(capture_region.CenterPoint().x(),
GetDimensionsLabelWindow()->bounds().CenterPoint().x());
EXPECT_EQ(capture_region.bottom() +
CaptureModeSession::kSizeLabelYDistanceFromRegionDp,
GetDimensionsLabelWindow()->bounds().y());
generator->ReleaseLeftButton();
EXPECT_EQ(nullptr, GetDimensionsLabelWindow());
// Create a new capture region close to the left side of the screen such that
// if the label was centered it would extend out of the screen.
// The x value of the label should be the left edge of the screen (0).
capture_region.SetRect(2, 100, 2, 100);
SelectRegion(capture_region, /*release_mouse=*/false);
EXPECT_EQ(0, GetDimensionsLabelWindow()->bounds().x());
generator->ReleaseLeftButton();
EXPECT_EQ(nullptr, GetDimensionsLabelWindow());
// Create a new capture region close to the right side of the screen such that
// if the label was centered it would extend out of the screen.
// The right (x + width) of the label should be the right edge of the screen
// (900).
capture_region.SetRect(896, 100, 2, 100);
SelectRegion(capture_region, /*release_mouse=*/false);
EXPECT_EQ(900, GetDimensionsLabelWindow()->bounds().right());
generator->ReleaseLeftButton();
EXPECT_EQ(nullptr, GetDimensionsLabelWindow());
// Create a new capture region close to the bottom side of the screen.
// The label should now appear inside the capture region, just above the
// bottom edge. It should be above the bottom of the screen as well.
capture_region.SetRect(100, 700, 600, 100);
SelectRegion(capture_region, /*release_mouse=*/false);
EXPECT_EQ(800 - CaptureModeSession::kSizeLabelYDistanceFromRegionDp,
GetDimensionsLabelWindow()->bounds().bottom());
generator->ReleaseLeftButton();
EXPECT_EQ(nullptr, GetDimensionsLabelWindow());
}
TEST_P(CaptureModeTest, CaptureRegionCaptureButtonLocation) {
UpdateDisplay("900x800");
auto* controller = StartImageRegionCapture();
// Select a large region. Verify that the capture button widget is centered.
constexpr gfx::Rect kLargeRegion(200, 200, 500, 500);
SelectRegion(kLargeRegion);
views::Widget* capture_button_widget = GetCaptureModeLabelWidget();
ASSERT_TRUE(capture_button_widget);
aura::Window* capture_button_window =
capture_button_widget->GetNativeWindow();
EXPECT_EQ(kLargeRegion.CenterPoint(),
capture_button_window->bounds().CenterPoint());
// Drag the bottom corner so that the region is too small to fit the capture
// button. Verify that the button is aligned horizontally and placed above the
// region.
auto* event_generator = GetEventGenerator();
constexpr gfx::Rect kSmallRegion(200, 200, 20, 20);
event_generator->DragMouseTo(kSmallRegion.bottom_right());
EXPECT_EQ(kSmallRegion, controller->user_capture_region());
EXPECT_EQ(kSmallRegion.CenterPoint().x(),
capture_button_window->bounds().CenterPoint().x());
const int distance_from_region =
CaptureModeSession::kCaptureButtonDistanceFromRegionDp;
EXPECT_EQ(kSmallRegion.y() - distance_from_region,
capture_button_window->bounds().bottom());
// Click inside the region to drag the entire region to the top of the
// screen. Verify that the button is aligned horizontally and placed below the
// region.
event_generator->set_current_screen_location(kSmallRegion.CenterPoint());
constexpr gfx::Rect kRegionAtTop(200, 0, 20, 20);
event_generator->DragMouseTo(kRegionAtTop.CenterPoint());
EXPECT_EQ(kRegionAtTop, controller->user_capture_region());
EXPECT_EQ(kRegionAtTop.CenterPoint().x(),
capture_button_window->bounds().CenterPoint().x());
EXPECT_EQ(kRegionAtTop.bottom() + distance_from_region,
capture_button_window->bounds().y());
}
// Tests some edge cases to ensure the capture button does not intersect the
// capture bar and end up unclickable since it is stacked below the capture bar.
// Regression test for https://crbug.com/1186462.
TEST_P(CaptureModeTest, CaptureRegionCaptureButtonDoesNotIntersectCaptureBar) {
UpdateDisplay("800x700");
StartImageRegionCapture();
// Create a region that would cover the capture mode bar. Add some insets to
// ensure that the capture button could fit inside. Verify that the two
// widgets do not overlap.
const gfx::Rect capture_bar_bounds =
GetCaptureModeBarWidget()->GetWindowBoundsInScreen();
gfx::Rect region_bounds = capture_bar_bounds;
region_bounds.Inset(-20);
SelectRegion(region_bounds);
EXPECT_FALSE(capture_bar_bounds.Intersects(
GetCaptureModeLabelWidget()->GetWindowBoundsInScreen()));
// Create a thin region above the capture mode bar. The algorithm would
// normally place the capture label under the region, but should adjust to
// avoid intersecting.
auto* event_generator = GetEventGenerator();
event_generator->set_current_screen_location(gfx::Point());
event_generator->ClickLeftButton();
const int capture_bar_midpoint_x = capture_bar_bounds.CenterPoint().x();
SelectRegion(
gfx::Rect(capture_bar_midpoint_x, capture_bar_bounds.y() - 10, 20, 10));
EXPECT_FALSE(capture_bar_bounds.Intersects(
GetCaptureModeLabelWidget()->GetWindowBoundsInScreen()));
// Create a thin region below the capture mode bar which reaches the bottom of
// the display. The algorithm would normally place the capture label above
// the region, but should adjust to avoid intersecting.
event_generator->set_current_screen_location(gfx::Point());
event_generator->ClickLeftButton();
SelectRegion(gfx::Rect(capture_bar_midpoint_x, capture_bar_bounds.bottom(),
20, 700 - capture_bar_bounds.bottom()));
EXPECT_FALSE(capture_bar_bounds.Intersects(
GetCaptureModeLabelWidget()->GetWindowBoundsInScreen()));
// Create a thin region that is vertical as tall as the display, and at the
// left edge of the display. The capture label button should be right of the
// region.
event_generator->set_current_screen_location(gfx::Point());
event_generator->ClickLeftButton();
SelectRegion(gfx::Rect(20, 700));
EXPECT_GT(GetCaptureModeLabelWidget()->GetWindowBoundsInScreen().x(), 20);
}
// Tests that pressing on the capture bar and releasing the press outside of the
// capture bar, the capture region could still be draggable and set. Regression
// test for https://crbug.com/1325028.
TEST_P(CaptureModeTest, SetCaptureRegionAfterPressOnCaptureBar) {
UpdateDisplay("800x600");
auto* controller =
StartCaptureSession(CaptureModeSource::kRegion, CaptureModeType::kVideo);
auto* settings_button = GetSettingsButton();
// Press on the settings button without release.
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseTo(
settings_button->GetBoundsInScreen().CenterPoint());
event_generator->PressLeftButton();
// Move mouse to the outside of the capture bar and then release the press.
event_generator->MoveMouseTo({300, 300});
event_generator->ReleaseLeftButton();
// Set the capture region, and verify it's set successfully.
const gfx::Rect region_bounds(100, 100, 200, 200);
SelectRegion(region_bounds);
EXPECT_EQ(controller->user_capture_region(), region_bounds);
}
TEST_P(CaptureModeTest, WindowCapture) {
// Create 2 windows that overlap with each other.
const gfx::Rect bounds1(0, 0, 200, 200);
std::unique_ptr<aura::Window> window1(CreateTestWindow(bounds1));
const gfx::Rect bounds2(150, 150, 200, 200);
std::unique_ptr<aura::Window> window2(CreateTestWindow(bounds2));
auto* controller = CaptureModeController::Get();
controller->SetSource(CaptureModeSource::kWindow);
controller->SetType(CaptureModeType::kImage);
controller->Start(CaptureModeEntryType::kAccelTakeWindowScreenshot);
EXPECT_TRUE(controller->IsActive());
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseToCenterOf(window1.get());
auto* capture_mode_session = controller->capture_mode_session();
EXPECT_EQ(capture_mode_session->GetSelectedWindow(), window1.get());
event_generator->MoveMouseToCenterOf(window2.get());
EXPECT_EQ(capture_mode_session->GetSelectedWindow(), window2.get());
// Now move the mouse to the overlapped area.
event_generator->MoveMouseTo(gfx::Point(175, 175));
EXPECT_EQ(capture_mode_session->GetSelectedWindow(), window2.get());
// Close the current selected window should automatically focus to next one.
window2.reset();
EXPECT_EQ(capture_mode_session->GetSelectedWindow(), window1.get());
// Open another one on top also change the selected window.
std::unique_ptr<aura::Window> window3(CreateTestWindow(bounds2));
EXPECT_EQ(capture_mode_session->GetSelectedWindow(), window3.get());
// Minimize the window should also automatically change the selected window.
WindowState::Get(window3.get())->Minimize();
EXPECT_EQ(capture_mode_session->GetSelectedWindow(), window1.get());
// Stop the capture session to avoid CaptureModeSession from receiving more
// events during test tearing down.
controller->Stop();
}
TEST_P(CaptureModeTest, WindowCaptureConfineBoundsDoNotOverlapWindowCaption) {
std::unique_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(200, 200)));
auto* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kVideo);
GetEventGenerator()->MoveMouseToCenterOf(window.get());
auto* capture_mode_session = controller->capture_mode_session();
EXPECT_EQ(capture_mode_session->GetSelectedWindow(), window.get());
auto* frame_header = capture_mode_util::GetWindowFrameHeader(window.get());
auto* caption_button_container = frame_header->caption_button_container();
// While the session is still active, the calculated confine bounds should not
// overlap with the frame caption.
EXPECT_FALSE(controller->GetCaptureSurfaceConfineBounds().Intersects(
caption_button_container->bounds()));
// Start recording and expect that the confine bounds calculated during
// recording still do not overlap with the frame caption.
StartVideoRecordingImmediately();
WaitForRecordingToStart();
EXPECT_FALSE(controller->GetCaptureSurfaceConfineBounds().Intersects(
caption_button_container->bounds()));
}
// Tests that the capture bar is located on the root with the cursor when
// starting capture mode.
TEST_P(CaptureModeTest, MultiDisplayCaptureBarInitialLocation) {
UpdateDisplay("800x700,801+0-800x700");
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseTo(gfx::Point(1000, 500));
auto* controller = StartImageRegionCapture();
EXPECT_TRUE(gfx::Rect(801, 0, 800, 800)
.Contains(GetCaptureModeBarView()->GetBoundsInScreen()));
controller->Stop();
event_generator->MoveMouseTo(gfx::Point(100, 500));
StartImageRegionCapture();
EXPECT_TRUE(gfx::Rect(800, 800).Contains(
GetCaptureModeBarView()->GetBoundsInScreen()));
}
// Tests behavior of a capture mode session if the active display is removed.
TEST_P(CaptureModeTest, DisplayRemoval) {
UpdateDisplay("1200x700,1201+0-800x700");
// Start capture mode on the secondary display.
GetEventGenerator()->MoveMouseTo(gfx::Point(1300, 500));
auto* controller = StartImageRegionCapture();
auto* session = controller->capture_mode_session();
EXPECT_TRUE(gfx::Rect(1201, 0, 800, 700)
.Contains(GetCaptureModeBarView()->GetBoundsInScreen()));
ASSERT_EQ(Shell::GetAllRootWindows()[1], session->current_root());
RemoveSecondaryDisplay();
// Tests that the capture mode bar is now on the primary display.
const gfx::Rect bar_bounds_in_screen =
GetCaptureModeBarView()->GetBoundsInScreen();
EXPECT_TRUE(gfx::Rect(1200, 700).Contains(bar_bounds_in_screen));
ASSERT_EQ(Shell::GetAllRootWindows()[0], session->current_root());
// Tests that the capture mode bar is centered on the primary display.
// Regression test for http://b/303094552.
EXPECT_EQ(600, bar_bounds_in_screen.CenterPoint().x());
}
// Tests behavior of a capture mode session if the active display is removed
// and countdown running.
TEST_P(CaptureModeTest, DisplayRemovalWithCountdownVisible) {
UpdateDisplay("800x700,801+0-800x700");
// Start capture mode on the secondary display.
auto recorded_window = CreateTestWindow(gfx::Rect(1000, 200, 400, 400));
auto* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kVideo);
GetEventGenerator()->MoveMouseToCenterOf(recorded_window.get());
auto* session = controller->capture_mode_session();
RemoveSecondaryDisplay();
ASSERT_EQ(Shell::GetAllRootWindows()[0], session->current_root());
// Test passes if no crash.
}
// Tests behavior of a capture mode session if the active display is removed,
// countdown running, fullscreen window, and in overview mode.
TEST_P(CaptureModeTest,
DisplayRemovalWithCountdownVisibleFullscreenWindowAndInOverview) {
UpdateDisplay("800x700,801+0-800x700");
// Start capture mode on the secondary display.
auto recorded_window = CreateTestWindow(gfx::Rect(1000, 200, 400, 400));
auto* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kVideo);
GetEventGenerator()->MoveMouseToCenterOf(recorded_window.get());
// Make the window fullscreen. This is important as the corner case is
// moving a fullscreen window triggers the shelf to occur, which changes
// display metrics.
recorded_window->SetProperty(aura::client::kShowStateKey,
ui::mojom::WindowShowState::kFullscreen);
auto* session = controller->capture_mode_session();
EnterOverview();
RemoveSecondaryDisplay();
ASSERT_EQ(Shell::GetAllRootWindows()[0], session->current_root());
// Test passes if no crash.
}
// Tests that using fullscreen or window source, moving the mouse across
// displays will change the root window of the capture session.
TEST_P(CaptureModeTest, MultiDisplayFullscreenOrWindowSourceRootWindow) {
UpdateDisplay("800x700,801+0-800x700");
ASSERT_EQ(2u, Shell::GetAllRootWindows().size());
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseTo(gfx::Point(100, 500));
for (auto source :
{CaptureModeSource::kFullscreen, CaptureModeSource::kWindow}) {
SCOPED_TRACE(source == CaptureModeSource::kFullscreen ? "Fullscreen source"
: "Window source");
auto* controller = StartCaptureSession(source, CaptureModeType::kImage);
auto* session = controller->capture_mode_session();
EXPECT_EQ(Shell::GetAllRootWindows()[0], session->current_root());
event_generator->MoveMouseTo(gfx::Point(1000, 500));
EXPECT_EQ(Shell::GetAllRootWindows()[1], session->current_root());
event_generator->MoveMouseTo(gfx::Point(100, 500));
EXPECT_EQ(Shell::GetAllRootWindows()[0], session->current_root());
controller->Stop();
}
}
// Tests that in region mode, moving the mouse across displays will not change
// the root window of the capture session, but clicking on a new display will.
TEST_P(CaptureModeTest, MultiDisplayRegionSourceRootWindow) {
UpdateDisplay("800x700,801+0-800x700");
ASSERT_EQ(2u, Shell::GetAllRootWindows().size());
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseTo(gfx::Point(100, 500));
auto* controller = StartImageRegionCapture();
auto* session = controller->capture_mode_session();
EXPECT_EQ(Shell::GetAllRootWindows()[0], session->current_root());
// Tests that moving the mouse to the secondary display does not change the
// root.
event_generator->MoveMouseTo(gfx::Point(1000, 500));
EXPECT_EQ(Shell::GetAllRootWindows()[0], session->current_root());
// Tests that pressing the mouse changes the root. The capture bar stays on
// the primary display until the mouse is released.
event_generator->PressLeftButton();
EXPECT_EQ(Shell::GetAllRootWindows()[1], session->current_root());
EXPECT_TRUE(gfx::Rect(800, 800).Contains(
GetCaptureModeBarView()->GetBoundsInScreen()));
event_generator->ReleaseLeftButton();
EXPECT_EQ(Shell::GetAllRootWindows()[1], session->current_root());
EXPECT_TRUE(gfx::Rect(801, 0, 800, 800)
.Contains(GetCaptureModeBarView()->GetBoundsInScreen()));
}
// Tests that using touch on multi display setups works as intended. Regression
// test for https://crbug.com/1159512.
TEST_P(CaptureModeTest, MultiDisplayTouch) {
UpdateDisplay("800x700,801+0-800x700");
ASSERT_EQ(2u, Shell::GetAllRootWindows().size());
auto* controller = StartImageRegionCapture();
auto* session = controller->capture_mode_session();
ASSERT_EQ(Shell::GetAllRootWindows()[0], session->current_root());
// Touch and move your finger on the secondary display. We should switch roots
// and the region size should be as expected.
auto* event_generator = GetEventGenerator();
event_generator->PressTouch(gfx::Point(1000, 200));
event_generator->MoveTouch(gfx::Point(1200, 400));
event_generator->ReleaseTouch();
EXPECT_EQ(Shell::GetAllRootWindows()[1], session->current_root());
EXPECT_EQ(gfx::Size(200, 200), controller->user_capture_region().size());
}
TEST_P(CaptureModeTest, RegionCursorStates) {
auto [sunfish_enabled, scanner_enabled] = GetParam();
if (sunfish_enabled || scanner_enabled) {
// This test fails when either Sunfish or Scanner is enabled.
// TODO: b/381965299 - Fix these test failures.
GTEST_SKIP();
}
UpdateDisplay("800x700,801+0-800x700");
auto* cursor_manager = Shell::Get()->cursor_manager();
auto* event_generator = GetEventGenerator();
struct {
std::string scoped_trace;
gfx::Rect display_rect;
gfx::Point point;
gfx::Rect capture_region;
} kRegionTestCases[] = {
{"primary_display", gfx::Rect(0, 0, 800, 700), gfx::Point(250, 250),
gfx::Rect(200, 200, 200, 200)},
{"external_display", gfx::Rect(801, 0, 800, 700), gfx::Point(1050, 250),
gfx::Rect(1000, 200, 200, 200)},
};
for (auto test_case : kRegionTestCases) {
SCOPED_TRACE(test_case.scoped_trace);
event_generator->MoveMouseTo(test_case.point);
const CursorType original_cursor_type = cursor_manager->GetCursor().type();
EXPECT_FALSE(cursor_manager->IsCursorLocked());
auto* controller = StartImageRegionCapture();
EXPECT_TRUE(test_case.display_rect.Contains(
GetCaptureModeBarView()->GetBoundsInScreen()));
auto outside_point = test_case.capture_region.origin();
outside_point.Offset(-10, -10);
// Clear the previous region if any.
event_generator->MoveMouseTo(outside_point);
event_generator->ClickLeftButton();
EXPECT_TRUE(cursor_manager->IsCursorVisible());
EXPECT_EQ(CursorType::kCell, cursor_manager->GetCursor().type());
EXPECT_TRUE(cursor_manager->IsCursorLocked());
// Makes sure that the cursor is updated when the user releases the region
// select and is still hovering in the same location.
SelectRegion(test_case.capture_region);
EXPECT_EQ(CursorType::kSouthEastResize, cursor_manager->GetCursor().type());
// Verify that all of the `FineTunePosition` locations have the correct
// cursor when hovered over both in primary display and external display.
event_generator->MoveMouseTo(test_case.capture_region.origin());
EXPECT_EQ(CursorType::kNorthWestResize, cursor_manager->GetCursor().type());
event_generator->MoveMouseTo(test_case.capture_region.top_center());
EXPECT_EQ(CursorType::kNorthSouthResize,
cursor_manager->GetCursor().type());
event_generator->MoveMouseTo(test_case.capture_region.top_right());
EXPECT_EQ(CursorType::kNorthEastResize, cursor_manager->GetCursor().type());
event_generator->MoveMouseTo(test_case.capture_region.right_center());
EXPECT_EQ(CursorType::kEastWestResize, cursor_manager->GetCursor().type());
event_generator->MoveMouseTo(test_case.capture_region.bottom_right());
EXPECT_EQ(CursorType::kSouthEastResize, cursor_manager->GetCursor().type());
event_generator->MoveMouseTo(test_case.capture_region.bottom_center());
EXPECT_EQ(CursorType::kNorthSouthResize,
cursor_manager->GetCursor().type());
event_generator->MoveMouseTo(test_case.capture_region.bottom_left());
EXPECT_EQ(CursorType::kSouthWestResize, cursor_manager->GetCursor().type());
event_generator->MoveMouseTo(test_case.capture_region.left_center());
EXPECT_EQ(CursorType::kEastWestResize, cursor_manager->GetCursor().type());
// Tests that within the bounds of the selected region, the cursor is a hand
// when hovering over the capture button, otherwise it is a
// multi-directional move cursor.
event_generator->MoveMouseTo(test_case.point);
EXPECT_EQ(CursorType::kMove, cursor_manager->GetCursor().type());
event_generator->MoveMouseTo(test_case.capture_region.CenterPoint());
EXPECT_EQ(CursorType::kHand, cursor_manager->GetCursor().type());
// Tests that the cursor changes to a cell type when hovering over the
// unselected region.
event_generator->MoveMouseTo(outside_point);
EXPECT_EQ(CursorType::kCell, cursor_manager->GetCursor().type());
// Check that cursor is unlocked when changing sources, and that the cursor
// changes to a pointer when hovering over the capture mode bar.
event_generator->MoveMouseTo(
GetRegionToggleButton()->GetBoundsInScreen().CenterPoint());
EXPECT_EQ(CursorType::kPointer, cursor_manager->GetCursor().type());
event_generator->MoveMouseTo(
GetWindowToggleButton()->GetBoundsInScreen().CenterPoint());
EXPECT_EQ(CursorType::kPointer, cursor_manager->GetCursor().type());
event_generator->ClickLeftButton();
ASSERT_EQ(CaptureModeSource::kWindow, controller->source());
// The event on the capture bar to change capture source will still keep the
// cursor locked.
EXPECT_TRUE(cursor_manager->IsCursorLocked());
EXPECT_EQ(CursorType::kPointer, cursor_manager->GetCursor().type());
// Tests that on changing back to region capture mode, the cursor becomes
// locked, and is still a pointer type over the bar, whilst a cell cursor
// otherwise (not over the selected region).
event_generator->MoveMouseTo(
GetRegionToggleButton()->GetBoundsInScreen().CenterPoint());
event_generator->ClickLeftButton();
EXPECT_TRUE(cursor_manager->IsCursorLocked());
EXPECT_EQ(CursorType::kPointer, cursor_manager->GetCursor().type());
// Tests that clicking on the button again doesn't change the cursor.
event_generator->ClickLeftButton();
EXPECT_EQ(CursorType::kPointer, cursor_manager->GetCursor().type());
event_generator->MoveMouseTo(outside_point);
EXPECT_EQ(CursorType::kCell, cursor_manager->GetCursor().type());
// Tests that when exiting capture mode that the cursor is restored to its
// original state.
controller->Stop();
EXPECT_FALSE(controller->IsActive());
EXPECT_FALSE(cursor_manager->IsCursorLocked());
EXPECT_EQ(original_cursor_type, cursor_manager->GetCursor().type());
}
// Tests the cursor state in tablet mode.
auto* controller = StartImageRegionCapture();
// Enter tablet mode, the cursor should be hidden.
SwitchToTabletMode();
EXPECT_FALSE(cursor_manager->IsCursorVisible());
EXPECT_TRUE(cursor_manager->IsCursorLocked());
// Move mouse but it should still be invisible.
event_generator->MoveMouseTo(gfx::Point(100, 100));
EXPECT_FALSE(cursor_manager->IsCursorVisible());
EXPECT_TRUE(cursor_manager->IsCursorLocked());
// Return to clamshell mode, mouse should appear again.
LeaveTabletMode();
EXPECT_TRUE(cursor_manager->IsCursorVisible());
EXPECT_EQ(CursorType::kCell, cursor_manager->GetCursor().type());
controller->Stop();
}
// Regression testing for https://crbug.com/1334824.
TEST_P(CaptureModeTest, CursorShouldNotChangeWhileAdjustingRegion) {
UpdateDisplay("800x600");
auto* cursor_manager = Shell::Get()->cursor_manager();
auto* event_generator = GetEventGenerator();
EXPECT_FALSE(cursor_manager->IsCursorLocked());
StartImageRegionCapture();
event_generator->MoveMouseTo(gfx::Point(200, 200));
EXPECT_EQ(CursorType::kCell, cursor_manager->GetCursor().type());
event_generator->PressLeftButton();
event_generator->MoveMouseTo(gfx::Point(300, 300));
EXPECT_EQ(CursorType::kSouthEastResize, cursor_manager->GetCursor().type());
// Drag the region by moving the cursor to the center point of the capture bar
// and expect that it doesn't change back to a pointer.
const auto capture_bar_center =
GetCaptureModeBarView()->GetBoundsInScreen().CenterPoint();
event_generator->MoveMouseTo(capture_bar_center);
EXPECT_EQ(CursorType::kSouthEastResize, cursor_manager->GetCursor().type());
}
TEST_P(CaptureModeTest, FullscreenCursorStates) {
auto* cursor_manager = Shell::Get()->cursor_manager();
CursorType original_cursor_type = cursor_manager->GetCursor().type();
EXPECT_FALSE(cursor_manager->IsCursorLocked());
EXPECT_EQ(CursorType::kPointer, original_cursor_type);
auto* event_generator = GetEventGenerator();
CaptureModeController* controller = StartCaptureSession(
CaptureModeSource::kFullscreen, CaptureModeType::kImage);
EXPECT_EQ(controller->type(), CaptureModeType::kImage);
EXPECT_TRUE(cursor_manager->IsCursorLocked());
event_generator->MoveMouseTo(gfx::Point(175, 175));
EXPECT_TRUE(cursor_manager->IsCursorVisible());
// Use image capture icon as the mouse cursor icon in image capture mode.
EXPECT_EQ(CursorType::kCustom, cursor_manager->GetCursor().type());
CaptureModeSessionTestApi test_api(controller->capture_mode_session());
EXPECT_TRUE(test_api.IsUsingCustomCursor(CaptureModeType::kImage));
// Move the mouse over to capture label widget won't change the cursor since
// it's a label not a label button.
event_generator->MoveMouseTo(test_api.GetCaptureLabelWidget()
->GetWindowBoundsInScreen()
.CenterPoint());
EXPECT_EQ(CursorType::kCustom, cursor_manager->GetCursor().type());
EXPECT_TRUE(test_api.IsUsingCustomCursor(CaptureModeType::kImage));
// Use pointer mouse if the event is on the capture bar.
ClickOnView(GetVideoToggleButton(), event_generator);
EXPECT_EQ(controller->type(), CaptureModeType::kVideo);
EXPECT_EQ(CursorType::kPointer, cursor_manager->GetCursor().type());
EXPECT_TRUE(cursor_manager->IsCursorLocked());
EXPECT_TRUE(cursor_manager->IsCursorVisible());
// Use video record icon as the mouse cursor icon in video recording mode.
event_generator->MoveMouseTo(gfx::Point(175, 175));
EXPECT_EQ(CursorType::kCustom, cursor_manager->GetCursor().type());
EXPECT_TRUE(test_api.IsUsingCustomCursor(CaptureModeType::kVideo));
EXPECT_TRUE(cursor_manager->IsCursorLocked());
EXPECT_TRUE(cursor_manager->IsCursorVisible());
// Enter tablet mode, the cursor should be hidden.
// To avoid flaky failures due to mouse devices blocking entering tablet mode,
// we detach all mouse devices. This shouldn't affect testing the cursor
// status.
SwitchToTabletMode();
EXPECT_FALSE(cursor_manager->IsCursorVisible());
EXPECT_TRUE(cursor_manager->IsCursorLocked());
// Exit tablet mode, the cursor should appear again.
LeaveTabletMode();
EXPECT_TRUE(cursor_manager->IsCursorVisible());
EXPECT_EQ(CursorType::kCustom, cursor_manager->GetCursor().type());
EXPECT_TRUE(test_api.IsUsingCustomCursor(CaptureModeType::kVideo));
EXPECT_TRUE(cursor_manager->IsCursorLocked());
// Stop capture mode, the cursor should be restored to its original state.
controller->Stop();
EXPECT_FALSE(controller->IsActive());
EXPECT_FALSE(cursor_manager->IsCursorLocked());
EXPECT_EQ(original_cursor_type, cursor_manager->GetCursor().type());
// Test that if we're in tablet mode for dev purpose, the cursor should still
// be visible.
Shell::Get()->tablet_mode_controller()->SetEnabledForDev(true);
StartCaptureSession(CaptureModeSource::kFullscreen, CaptureModeType::kImage);
EXPECT_EQ(controller->type(), CaptureModeType::kImage);
EXPECT_TRUE(cursor_manager->IsCursorLocked());
event_generator->MoveMouseTo(gfx::Point(175, 175));
EXPECT_TRUE(cursor_manager->IsCursorVisible());
}
TEST_P(CaptureModeTest, WindowCursorStates) {
std::unique_ptr<aura::Window> window(CreateTestWindow(gfx::Rect(200, 200)));
auto* cursor_manager = Shell::Get()->cursor_manager();
CursorType original_cursor_type = cursor_manager->GetCursor().type();
EXPECT_FALSE(cursor_manager->IsCursorLocked());
EXPECT_EQ(CursorType::kPointer, original_cursor_type);
auto* event_generator = GetEventGenerator();
CaptureModeController* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kImage);
EXPECT_EQ(controller->type(), CaptureModeType::kImage);
// If the mouse is above the window, use the image capture icon.
event_generator->MoveMouseTo(gfx::Point(150, 150));
EXPECT_TRUE(cursor_manager->IsCursorLocked());
EXPECT_TRUE(cursor_manager->IsCursorVisible());
EXPECT_EQ(CursorType::kCustom, cursor_manager->GetCursor().type());
CaptureModeSessionTestApi test_api(controller->capture_mode_session());
EXPECT_TRUE(test_api.IsUsingCustomCursor(CaptureModeType::kImage));
// If the mouse is not above the window, use a pointer.
event_generator->MoveMouseTo(gfx::Point(300, 300));
EXPECT_TRUE(cursor_manager->IsCursorLocked());
EXPECT_TRUE(cursor_manager->IsCursorVisible());
EXPECT_EQ(CursorType::kPointer, cursor_manager->GetCursor().type());
// Use pointer mouse if the event is on the capture bar.
ClickOnView(GetVideoToggleButton(), event_generator);
EXPECT_EQ(controller->type(), CaptureModeType::kVideo);
EXPECT_EQ(CursorType::kPointer, cursor_manager->GetCursor().type());
EXPECT_TRUE(cursor_manager->IsCursorLocked());
EXPECT_TRUE(cursor_manager->IsCursorVisible());
// Use video record icon as the mouse cursor icon in video recording mode.
event_generator->MoveMouseTo(gfx::Point(150, 150));
EXPECT_TRUE(cursor_manager->IsCursorLocked());
EXPECT_TRUE(cursor_manager->IsCursorVisible());
EXPECT_EQ(CursorType::kCustom, cursor_manager->GetCursor().type());
EXPECT_TRUE(test_api.IsUsingCustomCursor(CaptureModeType::kVideo));
// If the mouse is not above the window, use the original mouse cursor.
event_generator->MoveMouseTo(gfx::Point(300, 300));
EXPECT_TRUE(cursor_manager->IsCursorLocked());
EXPECT_TRUE(cursor_manager->IsCursorVisible());
EXPECT_EQ(CursorType::kPointer, cursor_manager->GetCursor().type());
// Move above the window again, the cursor should change back to the video
// record icon.
event_generator->MoveMouseTo(gfx::Point(150, 150));
EXPECT_TRUE(cursor_manager->IsCursorLocked());
EXPECT_TRUE(cursor_manager->IsCursorVisible());
EXPECT_EQ(CursorType::kCustom, cursor_manager->GetCursor().type());
EXPECT_TRUE(test_api.IsUsingCustomCursor(CaptureModeType::kVideo));
// Enter tablet mode, the cursor should be hidden.
// To avoid flaky failures due to mouse devices blocking entering tablet mode,
// we detach all mouse devices. This shouldn't affect testing the cursor
// status.
SwitchToTabletMode();
EXPECT_FALSE(cursor_manager->IsCursorVisible());
EXPECT_TRUE(cursor_manager->IsCursorLocked());
// Exit tablet mode, the cursor should appear again.
LeaveTabletMode();
EXPECT_TRUE(cursor_manager->IsCursorVisible());
EXPECT_EQ(CursorType::kCustom, cursor_manager->GetCursor().type());
EXPECT_TRUE(test_api.IsUsingCustomCursor(CaptureModeType::kVideo));
EXPECT_TRUE(cursor_manager->IsCursorLocked());
// Stop capture mode, the cursor should be restored to its original state.
controller->Stop();
EXPECT_FALSE(controller->IsActive());
EXPECT_FALSE(cursor_manager->IsCursorLocked());
EXPECT_EQ(original_cursor_type, cursor_manager->GetCursor().type());
}
// Tests that nothing crashes when windows are destroyed while being observed.
TEST_P(CaptureModeTest, WindowDestruction) {
// Create 2 windows that overlap with each other.
const gfx::Rect bounds1(0, 0, 200, 200);
const gfx::Rect bounds2(150, 150, 200, 200);
const gfx::Rect bounds3(50, 50, 200, 200);
std::unique_ptr<aura::Window> window1(CreateTestWindow(bounds1));
std::unique_ptr<aura::Window> window2(CreateTestWindow(bounds2));
auto* cursor_manager = Shell::Get()->cursor_manager();
CursorType original_cursor_type = cursor_manager->GetCursor().type();
EXPECT_FALSE(cursor_manager->IsCursorLocked());
EXPECT_EQ(CursorType::kPointer, original_cursor_type);
// Start capture session with Image type, so we have a custom cursor.
auto* event_generator = GetEventGenerator();
CaptureModeController* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kImage);
EXPECT_EQ(controller->type(), CaptureModeType::kImage);
// If the mouse is above the window, use the image capture icon.
event_generator->MoveMouseToCenterOf(window2.get());
EXPECT_TRUE(cursor_manager->IsCursorLocked());
EXPECT_TRUE(cursor_manager->IsCursorVisible());
EXPECT_EQ(CursorType::kCustom, cursor_manager->GetCursor().type());
auto* capture_mode_session = controller->capture_mode_session();
CaptureModeSessionTestApi test_api(capture_mode_session);
EXPECT_TRUE(test_api.IsUsingCustomCursor(CaptureModeType::kImage));
// Destroy the window while hovering. There is no window underneath, so it
// should revert back to a pointer.
window2.reset();
EXPECT_TRUE(cursor_manager->IsCursorLocked());
EXPECT_TRUE(cursor_manager->IsCursorVisible());
EXPECT_EQ(CursorType::kPointer, cursor_manager->GetCursor().type());
// Destroy the window while mouse is in a pressed state. Cursor should revert
// back to the original cursor.
std::unique_ptr<aura::Window> window3(CreateTestWindow(bounds2));
EXPECT_EQ(CursorType::kCustom, cursor_manager->GetCursor().type());
EXPECT_TRUE(test_api.IsUsingCustomCursor(CaptureModeType::kImage));
event_generator->PressLeftButton();
EXPECT_EQ(CursorType::kCustom, cursor_manager->GetCursor().type());
EXPECT_TRUE(test_api.IsUsingCustomCursor(CaptureModeType::kImage));
window3.reset();
event_generator->ReleaseLeftButton();
EXPECT_EQ(original_cursor_type, cursor_manager->GetCursor().type());
// When hovering over a window, if it is destroyed and there is another window
// under the cursor location in screen, then the selected window is
// automatically updated.
std::unique_ptr<aura::Window> window4(CreateTestWindow(bounds3));
event_generator->MoveMouseToCenterOf(window4.get());
EXPECT_EQ(CursorType::kCustom, cursor_manager->GetCursor().type());
EXPECT_TRUE(test_api.IsUsingCustomCursor(CaptureModeType::kImage));
EXPECT_EQ(capture_mode_session->GetSelectedWindow(), window4.get());
window4.reset();
EXPECT_EQ(CursorType::kCustom, cursor_manager->GetCursor().type());
EXPECT_TRUE(test_api.IsUsingCustomCursor(CaptureModeType::kImage));
// Check to see it's observing window1.
EXPECT_EQ(capture_mode_session->GetSelectedWindow(), window1.get());
// Cursor is over a window in the mouse pressed state. If the window is
// destroyed and there is another window under the cursor, the selected window
// is updated and the new selected window is captured.
std::unique_ptr<aura::Window> window5(CreateTestWindow(bounds3));
EXPECT_EQ(capture_mode_session->GetSelectedWindow(), window5.get());
event_generator->PressLeftButton();
window5.reset();
EXPECT_EQ(CursorType::kCustom, cursor_manager->GetCursor().type());
EXPECT_TRUE(test_api.IsUsingCustomCursor(CaptureModeType::kImage));
EXPECT_EQ(capture_mode_session->GetSelectedWindow(), window1.get());
event_generator->ReleaseLeftButton();
EXPECT_FALSE(controller->IsActive());
}
TEST_P(CaptureModeTest, CursorUpdatedOnDisplayRotation) {
UpdateDisplay("600x400");
const int64_t display_id =
display::Screen::GetScreen()->GetPrimaryDisplay().id();
display::SetInternalDisplayIds({display_id});
ScreenOrientationControllerTestApi orientation_test_api(
Shell::Get()->screen_orientation_controller());
auto* event_generator = GetEventGenerator();
auto* cursor_manager = Shell::Get()->cursor_manager();
CaptureModeController* controller = StartCaptureSession(
CaptureModeSource::kFullscreen, CaptureModeType::kImage);
event_generator->MoveMouseTo(gfx::Point(175, 175));
EXPECT_TRUE(cursor_manager->IsCursorVisible());
// Use image capture icon as the mouse cursor icon in image capture mode.
const ui::Cursor landscape_cursor = cursor_manager->GetCursor();
EXPECT_EQ(CursorType::kCustom, landscape_cursor.type());
CaptureModeSessionTestApi session_test_api(
controller->capture_mode_session());
EXPECT_TRUE(session_test_api.IsUsingCustomCursor(CaptureModeType::kImage));
// Rotate the screen.
orientation_test_api.SetDisplayRotation(
display::Display::ROTATE_270, display::Display::RotationSource::ACTIVE);
const ui::Cursor portrait_cursor = cursor_manager->GetCursor();
EXPECT_TRUE(session_test_api.IsUsingCustomCursor(CaptureModeType::kImage));
EXPECT_NE(landscape_cursor, portrait_cursor);
}
// Tests that in Region mode, cursor compositing is used instead of the system
// cursor when the cursor is being dragged.
TEST_P(CaptureModeTest, RegionDragCursorCompositing) {
auto* event_generator = GetEventGenerator();
auto* session = StartImageRegionCapture()->capture_mode_session();
auto* cursor_manager = Shell::Get()->cursor_manager();
// Initially cursor should be visible and cursor compositing is not enabled.
EXPECT_FALSE(session->is_drag_in_progress());
EXPECT_FALSE(IsCursorCompositingEnabled());
EXPECT_TRUE(cursor_manager->IsCursorVisible());
const gfx::Rect target_region(gfx::Rect(200, 200, 200, 200));
// For each start and end point try dragging and verify that cursor
// compositing is functioning as expected.
struct {
std::string trace;
gfx::Point start_point;
gfx::Point end_point;
} kDragCases[] = {
{"initial_region", target_region.origin(), target_region.bottom_right()},
{"edge_resize", target_region.right_center(),
gfx::Point(target_region.right_center() + gfx::Vector2d(50, 0))},
{"corner_resize", target_region.origin(), gfx::Point(175, 175)},
{"move", gfx::Point(250, 250), gfx::Point(300, 300)},
};
for (auto test_case : kDragCases) {
SCOPED_TRACE(test_case.trace);
event_generator->MoveMouseTo(test_case.start_point);
event_generator->PressLeftButton();
EXPECT_TRUE(session->is_drag_in_progress());
EXPECT_TRUE(IsCursorCompositingEnabled());
event_generator->MoveMouseTo(test_case.end_point);
EXPECT_TRUE(session->is_drag_in_progress());
EXPECT_TRUE(IsCursorCompositingEnabled());
event_generator->ReleaseLeftButton();
EXPECT_FALSE(session->is_drag_in_progress());
EXPECT_FALSE(IsCursorCompositingEnabled());
}
}
// Test that during countdown, capture mode session should not handle any
// incoming input events.
TEST_P(CaptureModeTest, DoNotHandleEventDuringCountDown) {
// We need a non-zero duration to avoid infinite loop on countdown.
ui::ScopedAnimationDurationScaleMode animation_scale(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
// Create 2 windows that overlap with each other.
std::unique_ptr<aura::Window> window1(CreateTestWindow(gfx::Rect(200, 200)));
std::unique_ptr<aura::Window> window2(
CreateTestWindow(gfx::Rect(150, 150, 200, 200)));
auto* controller = CaptureModeController::Get();
controller->SetSource(CaptureModeSource::kWindow);
controller->SetType(CaptureModeType::kVideo);
controller->Start(CaptureModeEntryType::kQuickSettings);
EXPECT_TRUE(controller->IsActive());
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseToCenterOf(window1.get());
auto* capture_mode_session = controller->capture_mode_session();
EXPECT_EQ(capture_mode_session->GetSelectedWindow(), window1.get());
// Start video recording. Countdown should start at this moment.
event_generator->ClickLeftButton();
// Now move the mouse onto the other window, we should not change the captured
// window during countdown.
event_generator->MoveMouseToCenterOf(window2.get());
EXPECT_EQ(capture_mode_session->GetSelectedWindow(), window1.get());
EXPECT_NE(capture_mode_session->GetSelectedWindow(), window2.get());
WaitForRecordingToStart();
}
// Test that during countdown, window changes or crashes are handled.
TEST_P(CaptureModeTest, WindowChangesDuringCountdown) {
// We need a non-zero duration to avoid infinite loop on countdown.
ui::ScopedAnimationDurationScaleMode animation_scale(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
std::unique_ptr<aura::Window> window;
auto* controller = CaptureModeController::Get();
controller->SetSource(CaptureModeSource::kWindow);
controller->SetType(CaptureModeType::kVideo);
auto start_countdown = [this, &window, controller]() {
window = CreateTestWindow(gfx::Rect(200, 200));
controller->Start(CaptureModeEntryType::kQuickSettings);
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseToCenterOf(window.get());
event_generator->ClickLeftButton();
EXPECT_TRUE(controller->IsActive());
EXPECT_FALSE(controller->is_recording_in_progress());
};
// Destroying or minimizing the observed window terminates the countdown and
// exits capture mode.
start_countdown();
window.reset();
EXPECT_FALSE(controller->IsActive());
start_countdown();
WindowState::Get(window.get())->Minimize();
EXPECT_FALSE(controller->IsActive());
// Activation changes (such as opening overview) should not terminate the
// countdown.
start_countdown();
EnterOverview();
EXPECT_TRUE(controller->IsActive());
EXPECT_FALSE(controller->is_recording_in_progress());
// Wait for countdown to finish and check that recording starts.
WaitForRecordingToStart();
EXPECT_FALSE(controller->IsActive());
EXPECT_TRUE(controller->is_recording_in_progress());
}
// Verifies that the video notification will show the same thumbnail image as
// sent by recording service.
TEST_P(CaptureModeTest, VideoNotificationThumbnail) {
auto* controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kVideo);
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
CaptureModeTestApi().FlushRecordingServiceForTesting();
auto* test_delegate =
static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());
// Request and wait for a video frame so that the recording service can use it
// to create a video thumbnail.
test_delegate->RequestAndWaitForVideoFrame();
SkBitmap service_thumbnail =
gfx::Image(test_delegate->GetVideoThumbnail()).AsBitmap();
EXPECT_FALSE(service_thumbnail.drawsNothing());
CaptureNotificationWaiter waiter;
controller->EndVideoRecording(EndRecordingReason::kStopRecordingButton);
EXPECT_FALSE(controller->is_recording_in_progress());
waiter.Wait();
// Verify that the service's thumbnail is the same image shown in the
// notification shown when recording ends.
const message_center::Notification* notification = GetPreviewNotification();
EXPECT_TRUE(notification);
EXPECT_FALSE(notification->image().IsEmpty());
const SkBitmap notification_thumbnail = notification->image().AsBitmap();
EXPECT_TRUE(
gfx::test::AreBitmapsEqual(notification_thumbnail, service_thumbnail));
}
TEST_P(CaptureModeTest, LowDriveFsSpace) {
auto* controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kVideo);
const base::FilePath drive_fs_folder = CreateFolderOnDriveFS("test");
controller->SetCustomCaptureFolder(drive_fs_folder);
auto* test_delegate =
static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());
// Simulate low DriveFS free space by setting it to e.g. 200 MB.
test_delegate->set_fake_drive_fs_free_bytes(200 * 1024 * 1024);
base::HistogramTester histogram_tester;
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
CaptureModeTestApi().FlushRecordingServiceForTesting();
test_delegate->RequestAndWaitForVideoFrame();
// Recording should end immediately due to a low Drive FS free space.
WaitForCaptureFileToBeSaved();
histogram_tester.ExpectBucketCount(
kEndRecordingReasonInClamshellHistogramName,
EndRecordingReason::kLowDriveFsQuota, 1);
}
TEST_P(CaptureModeTest, WindowRecordingCaptureId) {
auto window = CreateTestWindow(gfx::Rect(200, 200));
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kVideo);
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseToCenterOf(window.get());
auto* controller = CaptureModeController::Get();
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
// The window should have a valid capture ID.
EXPECT_TRUE(window->subtree_capture_id().is_valid());
// Once recording ends, the window should no longer be marked as capturable.
controller->EndVideoRecording(EndRecordingReason::kStopRecordingButton);
EXPECT_FALSE(controller->is_recording_in_progress());
EXPECT_FALSE(window->subtree_capture_id().is_valid());
}
TEST_P(CaptureModeTest, ClosingDimmedWidgetAboveRecordedWindow) {
views::Widget* widget = TestWidgetBuilder().BuildOwnedByNativeWidget();
auto* window = widget->GetNativeWindow();
auto recorded_window = CreateTestWindow(gfx::Rect(200, 200));
auto* controller = StartSessionAndRecordWindow(recorded_window.get());
EXPECT_TRUE(controller->is_recording_in_progress());
auto* recording_watcher = controller->video_recording_watcher_for_testing();
// Activate the window so that it becomes on top of the recorded window, and
// expect it gets dimmed.
wm::ActivateWindow(window);
EXPECT_TRUE(recording_watcher->IsWindowDimmedForTesting(window));
// Close the widget, this should not lead to any use-after-free. See
// https://crbug.com/1273197.
widget->Close();
}
TEST_P(CaptureModeTest, DimmingOfUnRecordedWindows) {
auto win1 = CreateTestWindow(gfx::Rect(200, 200));
auto win2 = CreateTestWindow(gfx::Rect(200, 200));
auto recorded_window = CreateTestWindow(gfx::Rect(200, 200));
auto* controller = StartSessionAndRecordWindow(recorded_window.get());
auto* recording_watcher = controller->video_recording_watcher_for_testing();
auto* shield_layer = recording_watcher->layer();
// Since the recorded window is the top most, no windows should be
// individually dimmed.
EXPECT_TRUE(recording_watcher->should_paint_layer());
EXPECT_TRUE(IsLayerStackedRightBelow(shield_layer, recorded_window->layer()));
EXPECT_FALSE(
recording_watcher->IsWindowDimmedForTesting(recorded_window.get()));
EXPECT_FALSE(recording_watcher->IsWindowDimmedForTesting(win1.get()));
EXPECT_FALSE(recording_watcher->IsWindowDimmedForTesting(win2.get()));
// Activating |win1| brings it to the front of the shield, so it should be
// dimmed separately.
wm::ActivateWindow(win1.get());
EXPECT_TRUE(recording_watcher->should_paint_layer());
EXPECT_TRUE(IsLayerStackedRightBelow(shield_layer, recorded_window->layer()));
EXPECT_FALSE(
recording_watcher->IsWindowDimmedForTesting(recorded_window.get()));
EXPECT_TRUE(recording_watcher->IsWindowDimmedForTesting(win1.get()));
EXPECT_FALSE(recording_watcher->IsWindowDimmedForTesting(win2.get()));
// Similarly for |win2|.
wm::ActivateWindow(win2.get());
EXPECT_TRUE(recording_watcher->should_paint_layer());
EXPECT_TRUE(IsLayerStackedRightBelow(shield_layer, recorded_window->layer()));
EXPECT_FALSE(
recording_watcher->IsWindowDimmedForTesting(recorded_window.get()));
EXPECT_TRUE(recording_watcher->IsWindowDimmedForTesting(win1.get()));
EXPECT_TRUE(recording_watcher->IsWindowDimmedForTesting(win2.get()));
// Minimizing the recorded window should stop painting the shield, and the
// dimmers should be removed.
WindowState::Get(recorded_window.get())->Minimize();
EXPECT_FALSE(recording_watcher->should_paint_layer());
EXPECT_FALSE(recording_watcher->IsWindowDimmedForTesting(win1.get()));
EXPECT_FALSE(recording_watcher->IsWindowDimmedForTesting(win2.get()));
// Activating the recorded window again unminimizes the window, which will
// reenable painting the shield.
wm::ActivateWindow(recorded_window.get());
EXPECT_FALSE(recording_watcher->IsWindowDimmedForTesting(win1.get()));
EXPECT_FALSE(recording_watcher->IsWindowDimmedForTesting(win2.get()));
EXPECT_FALSE(WindowState::Get(recorded_window.get())->IsMinimized());
EXPECT_TRUE(recording_watcher->should_paint_layer());
// Destroying a dimmed window is correctly tracked.
wm::ActivateWindow(win2.get());
EXPECT_TRUE(recording_watcher->IsWindowDimmedForTesting(win2.get()));
win2.reset();
EXPECT_FALSE(recording_watcher->IsWindowDimmedForTesting(win2.get()));
}
TEST_P(CaptureModeTest, DimmingWithDesks) {
auto recorded_window = CreateAppWindow(gfx::Rect(250, 100));
auto* controller = StartSessionAndRecordWindow(recorded_window.get());
auto* recording_watcher = controller->video_recording_watcher_for_testing();
EXPECT_TRUE(recording_watcher->should_paint_layer());
auto* desks_controller = DesksController::Get();
desks_controller->NewDesk(DesksCreationRemovalSource::kKeyboard);
Desk* desk_2 = desks_controller->desks()[1].get();
ActivateDesk(desk_2);
// A window on a different desk than that of the recorded window should not be
// dimmed.
auto win1 = CreateAppWindow(gfx::Rect(200, 200));
EXPECT_FALSE(recording_watcher->IsWindowDimmedForTesting(win1.get()));
// However, moving it to the desk of the recorded window should give it a
// dimmer, since it's a more recently-used window (i.e. above the recorded
// window).
Desk* desk_1 = desks_controller->desks()[0].get();
desks_controller->MoveWindowFromActiveDeskTo(
win1.get(), desk_1, win1->GetRootWindow(),
DesksMoveWindowFromActiveDeskSource::kShortcut);
EXPECT_TRUE(recording_watcher->IsWindowDimmedForTesting(win1.get()));
// Moving the recorded window out of the active desk should destroy the
// dimmer.
ActivateDesk(desk_1);
desks_controller->MoveWindowFromActiveDeskTo(
recorded_window.get(), desk_2, recorded_window->GetRootWindow(),
DesksMoveWindowFromActiveDeskSource::kShortcut);
EXPECT_FALSE(recording_watcher->IsWindowDimmedForTesting(win1.get()));
}
TEST_P(CaptureModeTest, DimmingWithDisplays) {
UpdateDisplay("500x400,401+0-800x700");
auto recorded_window = CreateAppWindow(gfx::Rect(250, 100));
auto* controller = StartSessionAndRecordWindow(recorded_window.get());
auto* recording_watcher = controller->video_recording_watcher_for_testing();
EXPECT_TRUE(recording_watcher->should_paint_layer());
// Create a new window on the second display. It should not be dimmed.
auto window = CreateTestWindow(gfx::Rect(420, 10, 200, 200));
auto roots = Shell::GetAllRootWindows();
EXPECT_EQ(roots[1], window->GetRootWindow());
EXPECT_FALSE(recording_watcher->IsWindowDimmedForTesting(window.get()));
// However when moved to the first display, it gets dimmed.
window_util::MoveWindowToDisplay(window.get(),
roots[0]->GetHost()->GetDisplayId());
EXPECT_TRUE(recording_watcher->IsWindowDimmedForTesting(window.get()));
// Moving the recorded window to the second display will remove the dimming of
// |window|.
window_util::MoveWindowToDisplay(recorded_window.get(),
roots[1]->GetHost()->GetDisplayId());
EXPECT_FALSE(recording_watcher->IsWindowDimmedForTesting(window.get()));
}
TEST_P(CaptureModeTest, MultiDisplayWindowRecording) {
UpdateDisplay("500x400,401+0-800x700");
auto roots = Shell::GetAllRootWindows();
ASSERT_EQ(2u, roots.size());
auto window = CreateTestWindow(gfx::Rect(200, 200));
auto* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kVideo);
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseToCenterOf(window.get());
auto* session_layer = controller->capture_mode_session()->layer();
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
// The session layer is reused to paint the recording shield.
auto* shield_layer =
controller->video_recording_watcher_for_testing()->layer();
EXPECT_EQ(session_layer, shield_layer);
EXPECT_EQ(shield_layer->parent(), window->layer()->parent());
EXPECT_TRUE(IsLayerStackedRightBelow(shield_layer, window->layer()));
EXPECT_EQ(shield_layer->bounds(), roots[0]->bounds());
// The capturer should capture from the frame sink of the first display.
// The video size should match the window's size.
CaptureModeTestApi test_api;
test_api.FlushRecordingServiceForTesting();
auto* test_delegate =
static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());
EXPECT_EQ(roots[0]->GetFrameSinkId(), test_delegate->GetCurrentFrameSinkId());
EXPECT_EQ(roots[0]->bounds().size(),
test_delegate->GetCurrentFrameSinkSizeInPixels());
EXPECT_EQ(window->bounds().size(), test_delegate->GetCurrentVideoSize());
// Moving a window to a different display should be propagated to the service,
// with the new root's frame sink ID, and the new root's size.
window_util::MoveWindowToDisplay(window.get(),
roots[1]->GetHost()->GetDisplayId());
test_api.FlushRecordingServiceForTesting();
ASSERT_EQ(window->GetRootWindow(), roots[1]);
EXPECT_EQ(roots[1]->GetFrameSinkId(), test_delegate->GetCurrentFrameSinkId());
EXPECT_EQ(roots[1]->bounds().size(),
test_delegate->GetCurrentFrameSinkSizeInPixels());
EXPECT_EQ(window->bounds().size(), test_delegate->GetCurrentVideoSize());
// The shield layer should move with the window, and maintain the stacking
// below the window's layer.
EXPECT_EQ(shield_layer->parent(), window->layer()->parent());
EXPECT_TRUE(IsLayerStackedRightBelow(shield_layer, window->layer()));
EXPECT_EQ(shield_layer->bounds(), roots[1]->bounds());
}
// Flaky especially on MSan: https://crbug.com/1293188
TEST_P(CaptureModeTest, DISABLED_WindowResizing) {
UpdateDisplay("700x600");
auto window = CreateTestWindow(gfx::Rect(200, 200));
auto* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kVideo);
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseToCenterOf(window.get());
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
auto* test_delegate =
static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());
CaptureModeTestApi test_api;
test_api.FlushRecordingServiceForTesting();
EXPECT_EQ(gfx::Size(200, 200), test_delegate->GetCurrentVideoSize());
EXPECT_EQ(gfx::Size(700, 600),
test_delegate->GetCurrentFrameSinkSizeInPixels());
// Multiple resize events should be throttled.
window->SetBounds(gfx::Rect(250, 250));
test_api.FlushRecordingServiceForTesting();
EXPECT_EQ(gfx::Size(200, 200), test_delegate->GetCurrentVideoSize());
window->SetBounds(gfx::Rect(250, 300));
test_api.FlushRecordingServiceForTesting();
EXPECT_EQ(gfx::Size(200, 200), test_delegate->GetCurrentVideoSize());
window->SetBounds(gfx::Rect(300, 300));
test_api.FlushRecordingServiceForTesting();
EXPECT_EQ(gfx::Size(200, 200), test_delegate->GetCurrentVideoSize());
// Once throttling ends, the current size is pushed.
auto* recording_watcher = controller->video_recording_watcher_for_testing();
recording_watcher->SendThrottledWindowSizeChangedNowForTesting();
test_api.FlushRecordingServiceForTesting();
EXPECT_EQ(gfx::Size(300, 300), test_delegate->GetCurrentVideoSize());
EXPECT_EQ(gfx::Size(700, 600),
test_delegate->GetCurrentFrameSinkSizeInPixels());
// Maximizing a window changes its size, and is pushed to the service with
// throttling.
WindowState::Get(window.get())->Maximize();
test_api.FlushRecordingServiceForTesting();
EXPECT_EQ(gfx::Size(300, 300), test_delegate->GetCurrentVideoSize());
recording_watcher->SendThrottledWindowSizeChangedNowForTesting();
test_api.FlushRecordingServiceForTesting();
EXPECT_NE(gfx::Size(300, 300), test_delegate->GetCurrentVideoSize());
EXPECT_EQ(window->bounds().size(), test_delegate->GetCurrentVideoSize());
}
TEST_P(CaptureModeTest, RotateDisplayWhileRecording) {
UpdateDisplay("600x800");
auto* controller =
StartCaptureSession(CaptureModeSource::kRegion, CaptureModeType::kVideo);
SelectRegion(gfx::Rect(20, 40, 100, 200));
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
// Initially the frame sink size matches the un-rotated display size in DIPs,
// but the video size matches the size of the crop region.
CaptureModeTestApi test_api;
test_api.FlushRecordingServiceForTesting();
auto* test_delegate =
static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());
EXPECT_EQ(gfx::Size(600, 800),
test_delegate->GetCurrentFrameSinkSizeInPixels());
EXPECT_EQ(gfx::Size(100, 200), test_delegate->GetCurrentVideoSize());
// Rotate by 90 degree, the frame sink size should be updated to match that.
// The video size should remain unaffected.
Shell::Get()->display_manager()->SetDisplayRotation(
WindowTreeHostManager::GetPrimaryDisplayId(), display::Display::ROTATE_90,
display::Display::RotationSource::USER);
test_api.FlushRecordingServiceForTesting();
EXPECT_EQ(gfx::Size(800, 600),
test_delegate->GetCurrentFrameSinkSizeInPixels());
EXPECT_EQ(gfx::Size(100, 200), test_delegate->GetCurrentVideoSize());
}
// Regression test for https://crbug.com/1331095.
// This is disabled due to flakiness: b/318349807
TEST_P(CaptureModeTest, DISABLED_CornerRegionWithScreenRotation) {
UpdateDisplay("800x600");
// Pick a region at the bottom right corner of the landscape screen, so that
// when the screen is rotated to portrait, the unadjusted region becomes
// outside the new portrait bounds.
auto* controller =
StartCaptureSession(CaptureModeSource::kRegion, CaptureModeType::kVideo);
SelectRegion(gfx::Rect(700, 400, 100, 200));
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
CaptureModeTestApi test_api;
test_api.FlushRecordingServiceForTesting();
auto* test_delegate =
static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());
EXPECT_EQ(gfx::Size(100, 200), test_delegate->GetCurrentVideoSize());
auto* root_window = Shell::GetPrimaryRootWindow();
auto* recording_watcher = controller->video_recording_watcher_for_testing();
gfx::Rect effective_region_bounds =
recording_watcher->GetEffectivePartialRegionBounds();
EXPECT_FALSE(effective_region_bounds.IsEmpty());
EXPECT_TRUE(root_window->bounds().Contains(effective_region_bounds));
// Verifies that the bounds of the visible rect of the frame is within the
// bounds of the root window.
auto verify_video_frame = [&](const media::VideoFrame& frame,
const gfx::Rect& content_rect) {
EXPECT_TRUE(root_window->bounds().Contains(frame.visible_rect()));
};
test_delegate->recording_service()->RequestAndWaitForVideoFrame(
base::BindLambdaForTesting(verify_video_frame));
// Rotate by 90 degree, the adjusted region bounds should not be empty and
// should remain within the bounds of the new portrait root window bounds.
Shell::Get()->display_manager()->SetDisplayRotation(
WindowTreeHostManager::GetPrimaryDisplayId(), display::Display::ROTATE_90,
display::Display::RotationSource::USER);
test_api.FlushRecordingServiceForTesting();
EXPECT_EQ(gfx::Size(100, 200), test_delegate->GetCurrentVideoSize());
effective_region_bounds =
recording_watcher->GetEffectivePartialRegionBounds();
EXPECT_FALSE(effective_region_bounds.IsEmpty());
EXPECT_TRUE(root_window->bounds().Contains(effective_region_bounds));
test_delegate->recording_service()->RequestAndWaitForVideoFrame(
base::BindLambdaForTesting(verify_video_frame));
}
// Tests that the video frames delivered to the service for recorded windows are
// valid (i.e. they have the correct size, and suffer from no letterboxing, even
// when the window gets resized).
// This is a regression test for https://crbug.com/1214023.
//
// TODO(crbug.com/1439950): This test is flaky.
TEST_P(CaptureModeTest, DISABLED_VerifyWindowRecordingVideoFrames) {
auto window = CreateTestWindow(gfx::Rect(100, 50, 200, 200));
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kVideo);
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseToCenterOf(window.get());
auto* controller = CaptureModeController::Get();
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
CaptureModeTestApi test_api;
test_api.FlushRecordingServiceForTesting();
bool is_video_frame_valid = false;
std::string failures;
auto verify_video_frame = [&](const media::VideoFrame& frame,
const gfx::Rect& content_rect) {
is_video_frame_valid = true;
failures.clear();
// Having the content positioned at (0,0) with a size that matches the
// current window's size means that there is no letterboxing.
if (gfx::Point() != content_rect.origin()) {
is_video_frame_valid = false;
failures =
base::StringPrintf("content_rect is not at (0,0), instead at: %s\n",
content_rect.origin().ToString().c_str());
}
const gfx::Size window_size = window->bounds().size();
if (window_size != content_rect.size()) {
is_video_frame_valid = false;
failures += base::StringPrintf(
"content_rect doesn't match the window size:\n"
" content_rect.size(): %s\n"
" window_size: %s\n",
content_rect.size().ToString().c_str(),
window_size.ToString().c_str());
}
// The video frame contents should match the bounds of the video frame.
if (frame.visible_rect() != content_rect) {
is_video_frame_valid = false;
failures += base::StringPrintf(
"content_rect doesn't match the frame's visible_rect:\n"
" content_rect: %s\n"
" visible_rect: %s\n",
content_rect.ToString().c_str(),
frame.visible_rect().ToString().c_str());
}
if (frame.coded_size() != window_size) {
is_video_frame_valid = false;
failures += base::StringPrintf(
"the frame's coded size doesn't match the window size:\n"
" frame.coded_size(): %s\n"
" window_size: %s\n",
frame.coded_size().ToString().c_str(),
window_size.ToString().c_str());
}
};
auto* test_delegate =
static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());
ASSERT_TRUE(test_delegate->recording_service());
{
SCOPED_TRACE("Initial window size");
test_delegate->recording_service()->RequestAndWaitForVideoFrame(
base::BindLambdaForTesting(verify_video_frame));
EXPECT_TRUE(is_video_frame_valid) << failures;
}
// Even when the window is resized and the throttled size reaches the service,
// new video frames should still be valid.
window->SetBounds(gfx::Rect(120, 60, 600, 500));
auto* recording_watcher = controller->video_recording_watcher_for_testing();
recording_watcher->SendThrottledWindowSizeChangedNowForTesting();
test_api.FlushRecordingServiceForTesting();
{
SCOPED_TRACE("After window resizing");
// A video frame is produced on the Viz side when a CopyOutputRequest is
// fulfilled. Those CopyOutputRequests could have been placed before the
// window's layer resize results in a new resized render pass in Viz. But
// eventually this must happen, and a valid frame must be delivered.
int remaining_attempts = 2;
do {
--remaining_attempts;
test_delegate->recording_service()->RequestAndWaitForVideoFrame(
base::BindLambdaForTesting(verify_video_frame));
} while (!is_video_frame_valid && remaining_attempts);
EXPECT_TRUE(is_video_frame_valid) << failures;
}
controller->EndVideoRecording(EndRecordingReason::kStopRecordingButton);
EXPECT_FALSE(controller->is_recording_in_progress());
}
// Tests that the focus should be on the `Settings` button after closing the
// settings menu.
TEST_P(CaptureModeTest, ReturnFocusToSettingsButtonAfterSettingsMenuIsClosed) {
auto* controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kImage);
auto* capture_mode_session =
static_cast<CaptureModeSession*>(controller->capture_mode_session());
ASSERT_EQ(capture_mode_session->session_type(), SessionType::kReal);
CaptureModeSessionTestApi test_api(capture_mode_session);
using FocusGroup = CaptureModeSessionFocusCycler::FocusGroup;
auto* event_generator = GetEventGenerator();
// Check the initial focus of the focus ring.
EXPECT_EQ(FocusGroup::kNone, test_api.GetCurrentFocusGroup());
// Tab six times, `Settings` button should be focused.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/6);
EXPECT_EQ(FocusGroup::kSettingsClose, test_api.GetCurrentFocusGroup());
EXPECT_TRUE(CaptureModeSessionFocusCycler::HighlightHelper::Get(
test_api.GetCaptureModeBarView()->settings_button())
->has_focus());
// Press the space key and the settings menu will be opened.
SendKey(ui::VKEY_SPACE, event_generator, ui::EF_NONE);
EXPECT_TRUE(test_api.GetCaptureModeSettingsView());
EXPECT_EQ(FocusGroup::kPendingSettings, test_api.GetCurrentFocusGroup());
// Close the settings menu, the focus ring should be on the `Settings` button.
SendKey(ui::VKEY_ESCAPE, event_generator, ui::EF_NONE);
EXPECT_FALSE(test_api.GetCaptureModeSettingsView());
EXPECT_EQ(FocusGroup::kSettingsClose, test_api.GetCurrentFocusGroup());
EXPECT_TRUE(CaptureModeSessionFocusCycler::HighlightHelper::Get(
test_api.GetCaptureModeBarView()->settings_button())
->has_focus());
// Tab the space key to open the settings menu again and tab to focus on the
// settings menu item.
SendKey(ui::VKEY_SPACE, event_generator, ui::EF_NONE);
EXPECT_TRUE(test_api.GetCaptureModeSettingsView());
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/3);
EXPECT_EQ(FocusGroup::kSettingsMenu, test_api.GetCurrentFocusGroup());
// Close the settings menu, the focus ring should be on the `Settings` button.
SendKey(ui::VKEY_ESCAPE, event_generator, ui::EF_NONE);
EXPECT_FALSE(test_api.GetCaptureModeSettingsView());
EXPECT_EQ(FocusGroup::kSettingsClose, test_api.GetCurrentFocusGroup());
EXPECT_TRUE(CaptureModeSessionFocusCycler::HighlightHelper::Get(
test_api.GetCaptureModeBarView()->settings_button())
->has_focus());
}
// Tests that minimized window(s) will be ignored whereas four corners occluded
// but overall partially occluded window will be focusable while tabbing through
// in `kWindow` mode.
TEST_P(CaptureModeTest, IgnoreMinimizeWindowsInKWindow) {
// Layout of three windows: four corners of `window3` are occluded by
// `window1` and `window2`.
//
// +------+
// | | +-----------+
// | 1 |-------| |
// | | 3 | 2 |
// | | | |
// | | | |
// | |-------| |
// | | +-----------+
// +------+
std::unique_ptr<aura::Window> window3 =
CreateTestWindow(gfx::Rect(100, 45, 150, 200));
std::unique_ptr<aura::Window> window2 =
CreateTestWindow(gfx::Rect(150, 50, 150, 250));
std::unique_ptr<aura::Window> window1 =
CreateTestWindow(gfx::Rect(20, 30, 100, 300));
std::unique_ptr<aura::Window> window4(
CreateTestWindow(gfx::Rect(0, 0, 50, 90)));
WindowState::Get(window4.get())->Minimize();
auto* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kImage);
auto* capture_mode_session =
static_cast<CaptureModeSession*>(controller->capture_mode_session());
ASSERT_EQ(capture_mode_session->session_type(), SessionType::kReal);
CaptureModeSessionTestApi test_api(capture_mode_session);
using FocusGroup = CaptureModeSessionFocusCycler::FocusGroup;
auto* event_generator = GetEventGenerator();
EXPECT_EQ(FocusGroup::kNone, test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
// Tab six times, `window1` should be focused. Tab another time, `window2`
// should be focused. Tab again, `window3` will be focused.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/6);
EXPECT_EQ(FocusGroup::kCaptureWindow, test_api.GetCurrentFocusGroup());
EXPECT_EQ(window1.get(), capture_mode_session->GetSelectedWindow());
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(window2.get(), capture_mode_session->GetSelectedWindow());
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(window3.get(), capture_mode_session->GetSelectedWindow());
// Tab once, the `settings` button should be focused. The minimized `window4`
// will be ignored during the tabbing process.
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(FocusGroup::kSettingsClose, test_api.GetCurrentFocusGroup());
controller->Stop();
}
// Tests that partially occluded window(s) will be focusable even when four
// edges are occluded by other windows while tabbing through in `kWindow` mode.
TEST_P(CaptureModeTest, PartiallyOccludedWindowIsFocusableInKWindow) {
// Layout of five windows: four edges of `window3` is occluded by `window1`,
// `window2`, `window4` and `window5` respectively, but the middle part is not
// occluded.
// +-----------+
// | |
// +----| 4 |
// | | |---------+
// | | | |
// | +-|-------|-+ |
// | 1 | 3 | 2 |
// | | | |
// | +-|-------|--+ |
// | | |--------+
// +----| 5 |
// | |
// +------------+
std::unique_ptr<aura::Window> window3 =
CreateTestWindow(gfx::Rect(100, 45, 150, 200));
std::unique_ptr<aura::Window> window2 =
CreateTestWindow(gfx::Rect(150, 50, 150, 250));
std::unique_ptr<aura::Window> window1 =
CreateTestWindow(gfx::Rect(20, 30, 100, 300));
std::unique_ptr<aura::Window> window4 =
CreateTestWindow(gfx::Rect(50, 5, 150, 55));
std::unique_ptr<aura::Window> window5 =
CreateTestWindow(gfx::Rect(60, 225, 210, 45));
auto* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kImage);
auto* capture_mode_session =
static_cast<CaptureModeSession*>(controller->capture_mode_session());
ASSERT_EQ(capture_mode_session->session_type(), SessionType::kReal);
CaptureModeSessionTestApi test_api(capture_mode_session);
using FocusGroup = CaptureModeSessionFocusCycler::FocusGroup;
auto* event_generator = GetEventGenerator();
EXPECT_EQ(FocusGroup::kNone, test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
// Tab six times, `window5` should be focused. Then `window4`, `window1`,
// `window2` and `window3` will be focused after each tab.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/6);
EXPECT_EQ(FocusGroup::kCaptureWindow, test_api.GetCurrentFocusGroup());
EXPECT_EQ(window5.get(), capture_mode_session->GetSelectedWindow());
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(window4.get(), capture_mode_session->GetSelectedWindow());
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(window1.get(), capture_mode_session->GetSelectedWindow());
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(window2.get(), capture_mode_session->GetSelectedWindow());
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(window3.get(), capture_mode_session->GetSelectedWindow());
// Tab once, the `settings` button should be focused.
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(FocusGroup::kSettingsClose, test_api.GetCurrentFocusGroup());
controller->Stop();
}
// Tests that fully occluded window(s) will be ignored while tabbing in
// `kWindow`.
TEST_P(CaptureModeTest, IgnoreFullyOccludedWindowWhileTabbingInKWindow) {
// Layout of six windows: `window3` is fully occluded by `window1`, `window2`,
// `window4`, `window5` and `window6`.
// +-----------+
// | |
// +----| 4 |
// | 1 | |---------+
// | +-----------------+ |
// | | | |
// | | 6 | 2 |
// | | | |
// | +-----------------+ |
// | | |--------+
// +----| 5 |
// | |
// +------------+
std::unique_ptr<aura::Window> window3 =
CreateTestWindow(gfx::Rect(100, 45, 150, 200));
std::unique_ptr<aura::Window> window2 =
CreateTestWindow(gfx::Rect(150, 50, 150, 250));
std::unique_ptr<aura::Window> window1 =
CreateTestWindow(gfx::Rect(20, 30, 100, 300));
std::unique_ptr<aura::Window> window4 =
CreateTestWindow(gfx::Rect(50, 5, 150, 55));
std::unique_ptr<aura::Window> window5 =
CreateTestWindow(gfx::Rect(60, 225, 210, 45));
std::unique_ptr<aura::Window> window6 =
CreateTestWindow(gfx::Rect(30, 55, 175, 185));
auto* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kImage);
auto* capture_mode_session =
static_cast<CaptureModeSession*>(controller->capture_mode_session());
ASSERT_EQ(capture_mode_session->session_type(), SessionType::kReal);
CaptureModeSessionTestApi test_api(capture_mode_session);
using FocusGroup = CaptureModeSessionFocusCycler::FocusGroup;
auto* event_generator = GetEventGenerator();
EXPECT_EQ(FocusGroup::kNone, test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
// Tab six times, `window6` should be focused. Then `window5`, `window4`,
// `window1` and `window2` will be focused after each tab.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/6);
EXPECT_EQ(FocusGroup::kCaptureWindow, test_api.GetCurrentFocusGroup());
EXPECT_EQ(window6.get(), capture_mode_session->GetSelectedWindow());
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(window5.get(), capture_mode_session->GetSelectedWindow());
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(window4.get(), capture_mode_session->GetSelectedWindow());
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(window1.get(), capture_mode_session->GetSelectedWindow());
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(window2.get(), capture_mode_session->GetSelectedWindow());
// Tab once, the `settings` button should be focused. The fully occluded
// `window3` will be ignored during the tabbing process.
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(FocusGroup::kSettingsClose, test_api.GetCurrentFocusGroup());
}
// Tests that only Tab and Shift + Tab events advance/reverse focus and stop
// event propagation. Other events like Alt + Tab should still behave as
// intended.
TEST_P(CaptureModeTest, OnlyAdvanceFocusWhenTabShiftPressed) {
auto window1 = CreateTestWindow();
auto window2 = CreateTestWindow();
auto* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kVideo);
auto* capture_mode_session =
static_cast<CaptureModeSession*>(controller->capture_mode_session());
ASSERT_EQ(capture_mode_session->session_type(), SessionType::kReal);
CaptureModeSessionTestApi test_api(capture_mode_session);
using FocusGroup = CaptureModeSessionFocusCycler::FocusGroup;
auto* event_generator = GetEventGenerator();
EXPECT_EQ(FocusGroup::kNone, test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
// Tab should advance focus forward.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/6);
EXPECT_EQ(window2.get(), capture_mode_session->GetSelectedWindow());
// Shift + Tab should advance focus backwards (reverse focus).
SendKey(ui::VKEY_TAB, event_generator, ui::EF_SHIFT_DOWN, /*count=*/5);
EXPECT_EQ(FocusGroup::kTypeSource, test_api.GetCurrentFocusGroup());
// Non-shortcut modifiers like Caps Lock should not count towards the flags we
// are concerned with, so Tab and Shift + Tab should still work normally.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_CAPS_LOCK_ON, /*count=*/5);
EXPECT_EQ(window2.get(), capture_mode_session->GetSelectedWindow());
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NUM_LOCK_ON | ui::EF_SHIFT_DOWN,
/*count=*/5);
EXPECT_EQ(FocusGroup::kTypeSource, test_api.GetCurrentFocusGroup());
// Alt + Tab should cycle the active window, and the focus should not change.
ASSERT_EQ(window_util::GetActiveWindow(), window2.get());
// We need to wait synchronously until the event has been fully processed to
// check if the activation has been changed.
ui::test::EmulateFullKeyPressReleaseSequence(event_generator, ui::VKEY_TAB,
false, false, true, false);
EXPECT_EQ(window_util::GetActiveWindow(), window1.get());
EXPECT_EQ(FocusGroup::kTypeSource, test_api.GetCurrentFocusGroup());
ui::test::EmulateFullKeyPressReleaseSequence(event_generator, ui::VKEY_TAB,
false, false, true, false);
EXPECT_EQ(window_util::GetActiveWindow(), window2.get());
EXPECT_EQ(FocusGroup::kTypeSource, test_api.GetCurrentFocusGroup());
// Alt + Shift + Tab should also cycle the active window in the reverse
// direction.
ui::test::EmulateFullKeyPressReleaseSequence(event_generator, ui::VKEY_TAB,
false, true, true, false);
EXPECT_EQ(window_util::GetActiveWindow(), window1.get());
EXPECT_EQ(FocusGroup::kTypeSource, test_api.GetCurrentFocusGroup());
// Ctrl + Tab and Ctrl + Shift + Tab should not do anything.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_CONTROL_DOWN);
EXPECT_EQ(window_util::GetActiveWindow(), window1.get());
EXPECT_EQ(FocusGroup::kTypeSource, test_api.GetCurrentFocusGroup());
SendKey(ui::VKEY_TAB, event_generator,
ui::EF_CONTROL_DOWN | ui::EF_SHIFT_DOWN);
EXPECT_EQ(window_util::GetActiveWindow(), window1.get());
EXPECT_EQ(FocusGroup::kTypeSource, test_api.GetCurrentFocusGroup());
}
// Tests that the capture region will be refreshed if in overview to reflect the
// bounds of the overview item for this window in `kWindow` mode.
TEST_P(CaptureModeTest, RefreshCaptureRegionInOverviewForKWindow) {
auto window = CreateAppWindow(gfx::Rect(100, 50, 200, 200));
auto* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kImage);
auto* session = controller->capture_mode_session();
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseToCenterOf(window.get());
EXPECT_EQ(window.get(), session->GetSelectedWindow());
// Start overview and verify that the capture region is refreshed correctly.
auto* overview_controller = OverviewController::Get();
overview_controller->StartOverview(OverviewStartAction::kTests);
ASSERT_TRUE(overview_controller->InOverviewSession());
auto* overview_item =
overview_controller->overview_session()->GetOverviewItemForWindow(
window.get());
const auto target_bounds = overview_item->target_bounds();
event_generator->MoveMouseTo(
gfx::ToRoundedPoint(target_bounds.CenterPoint()));
auto capture_region_in_overview =
CaptureModeSessionTestApi(session).GetSelectedWindowTargetBounds();
wm::ConvertRectToScreen(window->GetRootWindow(), &capture_region_in_overview);
EXPECT_EQ(capture_region_in_overview, gfx::ToRoundedRect(target_bounds));
}
class CaptureModeSaveFileTest : public CaptureModeTestBase,
public testing::WithParamInterface<
std::tuple<CaptureModeType, bool, bool>> {
public:
CaptureModeSaveFileTest() = default;
CaptureModeSaveFileTest(
const CaptureModeSaveFileTest& capture_mode_save_file_test) = delete;
CaptureModeSaveFileTest& operator=(const CaptureModeSaveFileTest&) = delete;
~CaptureModeSaveFileTest() override = default;
// CaptureModeTestBase:
void SetUp() override {
auto [unused_type, sunfish_enabled, scanner_enabled] = GetParam();
InitFeatures(sunfish_enabled, scanner_enabled);
CaptureModeTestBase::SetUp();
}
void StartCaptureSessionWithParam() {
StartCaptureSession(CaptureModeSource::kFullscreen,
std::get<CaptureModeType>(GetParam()));
}
// Based on the `CaptureModeType`, it performs the capture and then returns
// the path of the saved image or video files.
base::FilePath PerformCapture() {
auto* controller = CaptureModeController::Get();
switch (std::get<CaptureModeType>(GetParam())) {
case CaptureModeType::kImage:
controller->PerformCapture();
return WaitForCaptureFileToBeSaved();
case CaptureModeType::kVideo:
StartVideoRecordingImmediately();
controller->EndVideoRecording(EndRecordingReason::kStopRecordingButton);
return WaitForCaptureFileToBeSaved();
}
}
};
// Tests that if the custom folder becomes unavailable, the captured file should
// be saved into the default folder. Otherwise, it's saved into custom folder.
TEST_P(CaptureModeSaveFileTest, SaveCapturedFileWithCustomFolder) {
auto* controller = CaptureModeController::Get();
const base::FilePath default_folder =
controller->delegate_for_testing()->GetUserDefaultDownloadsFolder();
const base::FilePath custom_folder((FILE_PATH_LITERAL("/home/tests")));
controller->SetCustomCaptureFolder(custom_folder);
// Make sure the current folder is the custom folder here and then perform
// capture.
auto capture_folder = controller->GetCurrentCaptureFolder();
EXPECT_FALSE(capture_folder.is_default_downloads_folder);
StartCaptureSessionWithParam();
base::FilePath file_saved_path = PerformCapture();
// Since `custom_folder` is not available, the captured files will be saved
// into default folder;
EXPECT_EQ(file_saved_path.DirName(), default_folder);
// Now create an available custom folder and set it for custom capture folder.
const base::FilePath available_custom_folder =
CreateCustomFolderInUserDownloadsPath("test");
controller->SetCustomCaptureFolder(available_custom_folder);
capture_folder = controller->GetCurrentCaptureFolder();
EXPECT_FALSE(capture_folder.is_default_downloads_folder);
StartCaptureSessionWithParam();
file_saved_path = PerformCapture();
// Since `available_custom_folder` is now available, the captured files will
// be saved into the custom folder;
EXPECT_EQ(file_saved_path.DirName(), available_custom_folder);
}
TEST_P(CaptureModeSaveFileTest, CaptureModeSaveToLocationMetric) {
constexpr char kHistogramBase[] = "SaveLocation";
const std::string histogram_name = BuildHistogramName(
kHistogramBase, /*behavior=*/nullptr, /*append_ui_mode_suffix=*/true);
base::HistogramTester histogram_tester;
auto* controller = CaptureModeController::Get();
auto* test_delegate = controller->delegate_for_testing();
// Initialize four different save-to locations for screen capture that
// includes default downloads folder, local customized folder, root drive and
// a specific folder on drive.
const auto downloads_folder = test_delegate->GetUserDefaultDownloadsFolder();
const base::FilePath custom_folder =
CreateCustomFolderInUserDownloadsPath("test");
base::FilePath mount_point_path;
test_delegate->GetDriveFsMountPointPath(&mount_point_path);
const auto root_drive_folder = mount_point_path.Append("root");
const base::FilePath non_root_drive_folder = CreateFolderOnDriveFS("test");
const base::FilePath onedrive_root =
test_delegate->GetOneDriveMountPointPath();
const base::FilePath onedrive_folder = onedrive_root.Append("test");
{
base::ScopedAllowBlockingForTesting allow_blocking;
ASSERT_TRUE(base::CreateDirectory(onedrive_folder));
}
struct {
base::FilePath set_save_file_folder;
CaptureModeSaveToLocation save_location;
} kTestCases[] = {
{downloads_folder, CaptureModeSaveToLocation::kDefault},
{custom_folder, CaptureModeSaveToLocation::kCustomizedFolder},
{root_drive_folder, CaptureModeSaveToLocation::kDrive},
{non_root_drive_folder, CaptureModeSaveToLocation::kDriveFolder},
{onedrive_root, CaptureModeSaveToLocation::kOneDrive},
{onedrive_folder, CaptureModeSaveToLocation::kOneDriveFolder},
};
for (auto test_case : kTestCases) {
histogram_tester.ExpectBucketCount(histogram_name, test_case.save_location,
0);
}
// Set four different save-to locations in clamshell mode and check the
// histogram results.
EXPECT_FALSE(Shell::Get()->IsInTabletMode());
for (auto test_case : kTestCases) {
StartCaptureSessionWithParam();
controller->SetCustomCaptureFolder(test_case.set_save_file_folder);
auto file_saved_path = PerformCapture();
histogram_tester.ExpectBucketCount(histogram_name, test_case.save_location,
1);
}
// Set four different save-to locations in tablet mode and check the histogram
// results.
SwitchToTabletMode();
EXPECT_TRUE(Shell::Get()->IsInTabletMode());
for (auto test_case : kTestCases) {
StartCaptureSessionWithParam();
controller->SetCustomCaptureFolder(test_case.set_save_file_folder);
auto file_saved_path = PerformCapture();
histogram_tester.ExpectBucketCount(histogram_name, test_case.save_location,
1);
}
}
INSTANTIATE_TEST_SUITE_P(
All,
CaptureModeSaveFileTest,
testing::Combine(testing::Values(CaptureModeType::kImage,
CaptureModeType::kVideo),
testing::Bool(),
testing::Bool()),
[](const testing::TestParamInfo<CaptureModeSaveFileTest::ParamType>& info) {
auto type = std::get<CaptureModeType>(info.param);
bool sunfish_enabled = std::get<1>(info.param);
bool scanner_enabled = std::get<2>(info.param);
std::string test_name;
switch (type) {
case CaptureModeType::kImage:
test_name = "Image";
break;
case CaptureModeType::kVideo:
test_name = "Video";
break;
}
test_name += SunfishScannerTestName(sunfish_enabled, scanner_enabled);
return test_name;
});
// Test fixture for verifying that the videos are recorded at the pixel size of
// the targets being captured in all recording modes. This avoids having the
// scaling in CopyOutputRequests when performing the capture at a different size
// than that of the render pass (which is in pixels). This scaling causes a loss
// of quality, and a blurry video frames. https://crbug.com/1215185.
class CaptureModeRecordingSizeTest : public CaptureModeTest {
public:
CaptureModeRecordingSizeTest() = default;
~CaptureModeRecordingSizeTest() override = default;
// CaptureModeTest:
void SetUp() override {
CaptureModeTest::SetUp();
window_ = CreateTestWindow(gfx::Rect(100, 50, 200, 200));
CaptureModeController::Get()->SetUserCaptureRegion(user_region_,
/*by_user=*/true);
UpdateDisplay("800x600");
}
void TearDown() override {
window_.reset();
CaptureModeTest::TearDown();
}
// Converts the given |size| from DIPs to pixels based on the given value of
// |dsf|.
gfx::Size ToPixels(const gfx::Size& size, float dsf) const {
return gfx::ToFlooredSize(gfx::ConvertSizeToPixels(size, dsf));
}
protected:
// Verifies the size of the received video frame.
static void VerifyVideoFrame(const gfx::Size& expected_video_size,
const media::VideoFrame& frame,
const gfx::Rect& content_rect) {
// The I420 pixel format does not like odd dimensions, so the size of the
// visible rect in the video frame will be adjusted to be an even value.
const gfx::Size adjusted_size(expected_video_size.width() & ~1,
expected_video_size.height() & ~1);
EXPECT_EQ(adjusted_size, frame.visible_rect().size());
}
CaptureModeController* StartVideoRecording(CaptureModeSource source) {
auto* controller = StartCaptureSession(source, CaptureModeType::kVideo);
if (source == CaptureModeSource::kWindow)
GetEventGenerator()->MoveMouseToCenterOf(window_.get());
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
CaptureModeTestApi().FlushRecordingServiceForTesting();
return controller;
}
protected:
const gfx::Rect user_region_{20, 50};
std::unique_ptr<aura::Window> window_;
};
INSTANTIATE_TEST_SUITE_P(
,
CaptureModeRecordingSizeTest,
testing::Combine(testing::Bool(), testing::Bool()),
[](const testing::TestParamInfo<CaptureModeRecordingSizeTest::ParamType>&
info) {
bool sunfish_enabled = std::get<0>(info.param);
bool scanner_enabled = std::get<1>(info.param);
return SunfishScannerTestName(sunfish_enabled, scanner_enabled);
});
// TODO(crbug.com/1291073): Flaky on ChromeOS.
#if BUILDFLAG(IS_CHROMEOS)
#define MAYBE_CaptureAtPixelsFullscreen DISABLED_CaptureAtPixelsFullscreen
#else
#define MAYBE_CaptureAtPixelsFullscreen CaptureAtPixelsFullscreen
#endif
TEST_P(CaptureModeRecordingSizeTest, MAYBE_CaptureAtPixelsFullscreen) {
float dsf = 1.6f;
SetDeviceScaleFactor(dsf);
EXPECT_EQ(dsf, window_->GetHost()->device_scale_factor());
auto* controller = StartVideoRecording(CaptureModeSource::kFullscreen);
auto* root = window_->GetRootWindow();
gfx::Size initial_root_window_size_pixels =
ToPixels(root->bounds().size(), dsf);
auto* test_delegate =
static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());
ASSERT_TRUE(test_delegate->recording_service());
{
SCOPED_TRACE("Testing @ 1.6 device scale factor");
EXPECT_EQ(initial_root_window_size_pixels,
test_delegate->GetCurrentVideoSize());
EXPECT_EQ(
dsf, test_delegate->recording_service()->GetCurrentDeviceScaleFactor());
EXPECT_EQ(initial_root_window_size_pixels,
test_delegate->GetCurrentFrameSinkSizeInPixels());
test_delegate->recording_service()->RequestAndWaitForVideoFrame(
base::BindOnce(&CaptureModeRecordingSizeTest::VerifyVideoFrame,
initial_root_window_size_pixels));
}
// Change the DSF and expect the video size will remain at the initial pixel
// size of the fullscreen.
dsf = 2.f;
SetDeviceScaleFactor(dsf);
EXPECT_EQ(dsf, window_->GetHost()->device_scale_factor());
{
SCOPED_TRACE("Testing @ 2.0 device scale factor");
EXPECT_EQ(initial_root_window_size_pixels,
test_delegate->GetCurrentVideoSize());
// The recording service still tracks the up-to-date DSF and frame sink
// pixel size even though it doesn't change the video size from its initial
// value.
EXPECT_EQ(
dsf, test_delegate->recording_service()->GetCurrentDeviceScaleFactor());
EXPECT_EQ(ToPixels(root->bounds().size(), dsf),
test_delegate->GetCurrentFrameSinkSizeInPixels());
test_delegate->recording_service()->RequestAndWaitForVideoFrame(
base::BindOnce(&CaptureModeRecordingSizeTest::VerifyVideoFrame,
initial_root_window_size_pixels));
}
// When recording the fullscreen, the video size never changes, and remains at
// the initial pixel size of the recording. Hence, there should be no
// reconfigures.
EXPECT_EQ(0, test_delegate->recording_service()
->GetNumberOfVideoEncoderReconfigures());
}
// The test is flaky. https://crbug.com/1287724.
TEST_P(CaptureModeRecordingSizeTest, DISABLED_CaptureAtPixelsRegion) {
float dsf = 1.6f;
SetDeviceScaleFactor(dsf);
EXPECT_EQ(dsf, window_->GetHost()->device_scale_factor());
auto* controller = StartVideoRecording(CaptureModeSource::kRegion);
auto* test_delegate =
static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());
ASSERT_TRUE(test_delegate->recording_service());
{
SCOPED_TRACE("Testing @ 1.6 device scale factor");
const gfx::Size expected_video_size = ToPixels(user_region_.size(), dsf);
EXPECT_EQ(expected_video_size, test_delegate->GetCurrentVideoSize());
EXPECT_EQ(
dsf, test_delegate->recording_service()->GetCurrentDeviceScaleFactor());
test_delegate->recording_service()->RequestAndWaitForVideoFrame(
base::BindOnce(&CaptureModeRecordingSizeTest::VerifyVideoFrame,
expected_video_size));
}
// Change the DSF and expect the video size to change to match the new pixel
// size of the recorded target.
dsf = 2.f;
SetDeviceScaleFactor(dsf);
EXPECT_EQ(dsf, window_->GetHost()->device_scale_factor());
{
SCOPED_TRACE("Testing @ 2.0 device scale factor");
const gfx::Size expected_video_size = ToPixels(user_region_.size(), dsf);
EXPECT_EQ(expected_video_size, test_delegate->GetCurrentVideoSize());
EXPECT_EQ(
dsf, test_delegate->recording_service()->GetCurrentDeviceScaleFactor());
test_delegate->recording_service()->RequestAndWaitForVideoFrame(
base::BindOnce(&CaptureModeRecordingSizeTest::VerifyVideoFrame,
expected_video_size));
}
// Since the user chooses the capture region in DIPs, its corresponding pixel
// size will change when changing the device scale factor. Therefore, the
// encoder is expected to reconfigure once.
EXPECT_EQ(1, test_delegate->recording_service()
->GetNumberOfVideoEncoderReconfigures());
}
// The test is flaky. https://crbug.com/1287724.
TEST_P(CaptureModeRecordingSizeTest, DISABLED_CaptureAtPixelsWindow) {
float dsf = 1.6f;
SetDeviceScaleFactor(dsf);
EXPECT_EQ(dsf, window_->GetHost()->device_scale_factor());
auto* controller = StartVideoRecording(CaptureModeSource::kWindow);
auto* test_delegate =
static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());
ASSERT_TRUE(test_delegate->recording_service());
{
SCOPED_TRACE("Testing @ 1.6 device scale factor");
const gfx::Size expected_video_size =
ToPixels(window_->GetBoundsInRootWindow().size(), dsf);
EXPECT_EQ(expected_video_size, test_delegate->GetCurrentVideoSize());
EXPECT_EQ(
dsf, test_delegate->recording_service()->GetCurrentDeviceScaleFactor());
test_delegate->recording_service()->RequestAndWaitForVideoFrame(
base::BindOnce(&CaptureModeRecordingSizeTest::VerifyVideoFrame,
expected_video_size));
}
// Change the DSF and expect the video size to change to match the new pixel
// size of the recorded target.
dsf = 2.f;
SetDeviceScaleFactor(dsf);
EXPECT_EQ(dsf, window_->GetHost()->device_scale_factor());
{
SCOPED_TRACE("Testing @ 2.0 device scale factor");
const gfx::Size expected_video_size =
ToPixels(window_->GetBoundsInRootWindow().size(), dsf);
EXPECT_EQ(expected_video_size, test_delegate->GetCurrentVideoSize());
EXPECT_EQ(
dsf, test_delegate->recording_service()->GetCurrentDeviceScaleFactor());
test_delegate->recording_service()->RequestAndWaitForVideoFrame(
base::BindOnce(&CaptureModeRecordingSizeTest::VerifyVideoFrame,
expected_video_size));
}
// When changing the device scale factor, the DIPs size of the window doesn't
// change, but (like |kRegion|) its pixel size will. Hence, the
// reconfiguration.
EXPECT_EQ(1, test_delegate->recording_service()
->GetNumberOfVideoEncoderReconfigures());
}
// Tests the behavior of screen recording with the presence of HDCP secure
// content on the screen in all capture mode sources (fullscreen, region, and
// window) depending on the test param.
class CaptureModeHdcpTest : public CaptureModeTestBase,
public ::testing::WithParamInterface<
std::tuple<CaptureModeSource, bool, bool>> {
public:
CaptureModeHdcpTest() = default;
~CaptureModeHdcpTest() override = default;
// CaptureModeTestBase:
void SetUp() override {
auto [unused_source, sunfish_enabled, scanner_enabled] = GetParam();
InitFeatures(sunfish_enabled, scanner_enabled);
CaptureModeTestBase::SetUp();
window_ = CreateTestWindow(gfx::Rect(200, 200));
// Create a child window with protected content. This simulates the real
// behavior of a browser window hosting a page with protected content, where
// the window that has a protection mask is the RenderWidgetHostViewAura,
// which is a descendant of the BrowserFrame window which can get recorded.
protected_content_window_ = CreateTestWindow(gfx::Rect(150, 150));
window_->AddChild(protected_content_window_.get());
protection_delegate_ = std::make_unique<OutputProtectionDelegate>(
protected_content_window_.get());
CaptureModeController::Get()->SetUserCaptureRegion(gfx::Rect(20, 50),
/*by_user=*/true);
}
void TearDown() override {
protection_delegate_.reset();
protected_content_window_.reset();
window_.reset();
CaptureModeTestBase::TearDown();
}
// Enters the capture mode session.
void StartSessionForVideo() {
StartCaptureSession(std::get<CaptureModeSource>(GetParam()),
CaptureModeType::kVideo);
}
// Attempts video recording from the capture mode source set by the test
// param.
void AttemptRecording() {
auto* controller = CaptureModeController::Get();
ASSERT_TRUE(controller->IsActive());
switch (std::get<CaptureModeSource>(GetParam())) {
case CaptureModeSource::kFullscreen:
case CaptureModeSource::kRegion:
controller->StartVideoRecordingImmediatelyForTesting();
break;
case CaptureModeSource::kWindow:
// Window capture mode selects the window under the cursor as the
// capture source.
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseToCenterOf(window_.get());
controller->StartVideoRecordingImmediatelyForTesting();
break;
}
}
protected:
std::unique_ptr<aura::Window> window_;
std::unique_ptr<aura::Window> protected_content_window_;
std::unique_ptr<OutputProtectionDelegate> protection_delegate_;
};
TEST_P(CaptureModeHdcpTest, WindowBecomesProtectedWhileRecording) {
StartSessionForVideo();
AttemptRecording();
WaitForRecordingToStart();
auto* controller = CaptureModeController::Get();
EXPECT_TRUE(controller->is_recording_in_progress());
// The window becomes HDCP protected, which should end video recording.
base::HistogramTester histogram_tester;
protection_delegate_->SetProtection(display::CONTENT_PROTECTION_METHOD_HDCP,
base::DoNothing());
EXPECT_FALSE(controller->is_recording_in_progress());
histogram_tester.ExpectBucketCount(
kEndRecordingReasonInClamshellHistogramName,
EndRecordingReason::kHdcpInterruption, 1);
}
TEST_P(CaptureModeHdcpTest, ProtectedTabBecomesActiveAfterRecordingStarts) {
// Simulate protected content being on an inactive tab.
protection_delegate_->SetProtection(display::CONTENT_PROTECTION_METHOD_HDCP,
base::DoNothing());
Shell::GetPrimaryRootWindow()
->GetChildById(kShellWindowId_UnparentedContainer)
->AddChild(protected_content_window_.get());
// Recording should start normally, since the protected window is not a
// descendant of the window being recorded.
auto* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kVideo);
GetEventGenerator()->MoveMouseToCenterOf(window_.get());
controller->StartVideoRecordingImmediatelyForTesting();
WaitForRecordingToStart();
EXPECT_TRUE(controller->is_recording_in_progress());
// Simulate activating the tab that has protected content by parenting it back
// to the window being recorded. Recording should stop immediately.
window_->AddChild(protected_content_window_.get());
EXPECT_FALSE(controller->is_recording_in_progress());
}
TEST_P(CaptureModeHdcpTest, ProtectedWindowDestruction) {
auto window_2 = CreateTestWindow(gfx::Rect(100, 50));
OutputProtectionDelegate protection_delegate_2(window_2.get());
protection_delegate_2.SetProtection(display::CONTENT_PROTECTION_METHOD_HDCP,
base::DoNothing());
StartSessionForVideo();
AttemptRecording();
// Recording cannot start because of another protected window on the screen,
// except when we're capturing a different |window_|.
auto* controller = CaptureModeController::Get();
EXPECT_FALSE(controller->IsActive());
if (std::get<CaptureModeSource>(GetParam()) == CaptureModeSource::kWindow) {
WaitForRecordingToStart();
EXPECT_TRUE(controller->is_recording_in_progress());
controller->EndVideoRecording(EndRecordingReason::kStopRecordingButton);
EXPECT_FALSE(controller->is_recording_in_progress());
// Wait for the video file to be saved so that we can start a new recording.
WaitForCaptureFileToBeSaved();
} else {
EXPECT_FALSE(controller->is_recording_in_progress());
}
// When the protected window is destroyed, it's possbile now to record from
// all capture sources.
window_2.reset();
StartSessionForVideo();
AttemptRecording();
WaitForRecordingToStart();
EXPECT_FALSE(controller->IsActive());
EXPECT_TRUE(controller->is_recording_in_progress());
}
TEST_P(CaptureModeHdcpTest, WindowBecomesProtectedBeforeRecording) {
protection_delegate_->SetProtection(display::CONTENT_PROTECTION_METHOD_HDCP,
base::DoNothing());
StartSessionForVideo();
AttemptRecording();
// Recording cannot even start.
auto* controller = CaptureModeController::Get();
EXPECT_FALSE(controller->is_recording_in_progress());
EXPECT_FALSE(controller->IsActive());
}
TEST_P(CaptureModeHdcpTest, ProtectedWindowInMultiDisplay) {
UpdateDisplay("500x400,401+0-500x400");
auto roots = Shell::GetAllRootWindows();
ASSERT_EQ(2u, roots.size());
protection_delegate_->SetProtection(display::CONTENT_PROTECTION_METHOD_HDCP,
base::DoNothing());
// Move the cursor to the secondary display before starting the session to
// make sure the session starts on that display.
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseTo(roots[1]->GetBoundsInScreen().CenterPoint());
StartSessionForVideo();
// Also, make sure the selected region is in the secondary display.
auto* controller = CaptureModeController::Get();
EXPECT_EQ(controller->capture_mode_session()->current_root(), roots[1]);
AttemptRecording();
// Recording should be able to start (since the protected window is on the
// first display) unless the protected window itself is the one being
// recorded.
if (std::get<CaptureModeSource>(GetParam()) == CaptureModeSource::kWindow) {
EXPECT_FALSE(controller->is_recording_in_progress());
} else {
WaitForRecordingToStart();
EXPECT_TRUE(controller->is_recording_in_progress());
// Moving the protected window to the display being recorded should
// terminate the recording.
base::HistogramTester histogram_tester;
window_util::MoveWindowToDisplay(window_.get(),
roots[1]->GetHost()->GetDisplayId());
ASSERT_EQ(window_->GetRootWindow(), roots[1]);
ASSERT_EQ(protected_content_window_->GetRootWindow(), roots[1]);
EXPECT_FALSE(controller->is_recording_in_progress());
histogram_tester.ExpectBucketCount(
kEndRecordingReasonInClamshellHistogramName,
EndRecordingReason::kHdcpInterruption, 1);
}
}
INSTANTIATE_TEST_SUITE_P(
All,
CaptureModeHdcpTest,
testing::Combine(testing::Values(CaptureModeSource::kFullscreen,
CaptureModeSource::kRegion,
CaptureModeSource::kWindow),
testing::Bool(),
testing::Bool()),
[](const testing::TestParamInfo<CaptureModeHdcpTest::ParamType>& info) {
auto source = std::get<CaptureModeSource>(info.param);
bool sunfish_enabled = std::get<1>(info.param);
bool scanner_enabled = std::get<2>(info.param);
return SourceSunfishScannerTestName(source, sunfish_enabled,
scanner_enabled);
});
TEST_P(CaptureModeTest, ClosingWindowBeingRecorded) {
auto window = CreateTestWindow(gfx::Rect(200, 200));
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kVideo);
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseToCenterOf(window.get());
auto* controller = CaptureModeController::Get();
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
// The window should have a valid capture ID.
EXPECT_TRUE(window->subtree_capture_id().is_valid());
// Generate a couple of mouse moves, so that the second one gets throttled
// using the `VideoRecordingWatcher::cursor_events_throttle_timer_`. This is
// needed for a regression testing of https://crbug.com/1273609.
event_generator->MoveMouseBy(20, 30);
event_generator->MoveMouseBy(-10, -20);
// Closing the window being recorded should end video recording.
base::HistogramTester histogram_tester;
window.reset();
auto* stop_recording_button = Shell::GetPrimaryRootWindowController()
->GetStatusAreaWidget()
->stop_recording_button_tray();
EXPECT_FALSE(stop_recording_button->visible_preferred());
EXPECT_FALSE(controller->is_recording_in_progress());
WaitForCaptureFileToBeSaved();
EXPECT_FALSE(controller->video_recording_watcher_for_testing());
histogram_tester.ExpectBucketCount(
kEndRecordingReasonInClamshellHistogramName,
EndRecordingReason::kDisplayOrWindowClosing, 1);
}
TEST_P(CaptureModeTest, DetachDisplayWhileWindowRecording) {
UpdateDisplay("500x400,401+0-500x400");
// Create a window on the second display.
auto window = CreateTestWindow(gfx::Rect(450, 20, 200, 200));
auto roots = Shell::GetAllRootWindows();
ASSERT_EQ(2u, roots.size());
EXPECT_EQ(window->GetRootWindow(), roots[1]);
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kVideo);
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseTo(window->GetBoundsInScreen().CenterPoint());
auto* controller = CaptureModeController::Get();
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
auto* stop_recording_button = RootWindowController::ForWindow(roots[1])
->GetStatusAreaWidget()
->stop_recording_button_tray();
EXPECT_TRUE(stop_recording_button->visible_preferred());
// Disconnecting the display, on which the window being recorded is located,
// should not end the recording. The window should be reparented to another
// display, and the stop-recording button should move with to that display.
RemoveSecondaryDisplay();
roots = Shell::GetAllRootWindows();
ASSERT_EQ(1u, roots.size());
EXPECT_TRUE(controller->is_recording_in_progress());
stop_recording_button = RootWindowController::ForWindow(roots[0])
->GetStatusAreaWidget()
->stop_recording_button_tray();
EXPECT_TRUE(stop_recording_button->visible_preferred());
}
TEST_P(CaptureModeTest, SuspendWhileSessionIsActive) {
auto* controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kVideo);
EXPECT_TRUE(controller->IsActive());
power_manager_client()->SendSuspendImminent(
power_manager::SuspendImminent::IDLE);
EXPECT_FALSE(controller->IsActive());
}
TEST_P(CaptureModeTest, SuspendAfterCountdownStarts) {
// User NORMAL_DURATION for the countdown animation so we can have predictable
// timings.
ui::ScopedAnimationDurationScaleMode animation_scale(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
auto* controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kVideo);
// Hit Enter to begin recording, wait for 1 second, then suspend the device.
auto* event_generator = GetEventGenerator();
SendKey(ui::VKEY_RETURN, event_generator);
WaitForSeconds(1);
power_manager_client()->SendSuspendImminent(
power_manager::SuspendImminent::IDLE);
EXPECT_FALSE(controller->IsActive());
EXPECT_FALSE(controller->is_recording_in_progress());
}
TEST_P(CaptureModeTest, SuspendAfterRecordingStarts) {
auto* controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kVideo);
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
base::HistogramTester histogram_tester;
power_manager_client()->SendSuspendImminent(
power_manager::SuspendImminent::IDLE);
EXPECT_FALSE(controller->is_recording_in_progress());
histogram_tester.ExpectBucketCount(
kEndRecordingReasonInClamshellHistogramName,
EndRecordingReason::kImminentSuspend, 1);
}
TEST_P(CaptureModeTest, SwitchUsersWhileRecording) {
auto* controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kVideo);
StartVideoRecordingImmediately();
base::HistogramTester histogram_tester;
EXPECT_TRUE(controller->is_recording_in_progress());
SwitchToUser2();
EXPECT_FALSE(controller->is_recording_in_progress());
histogram_tester.ExpectBucketCount(
kEndRecordingReasonInClamshellHistogramName,
EndRecordingReason::kActiveUserChange, 1);
}
TEST_P(CaptureModeTest, SwitchUsersAfterCountdownStarts) {
// User NORMAL_DURATION for the countdown animation so we can have predictable
// timings.
ui::ScopedAnimationDurationScaleMode animation_scale(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
auto* controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kVideo);
// Hit Enter to begin recording, wait for 1 second, then switch users.
auto* event_generator = GetEventGenerator();
SendKey(ui::VKEY_RETURN, event_generator);
WaitForSeconds(1);
SwitchToUser2();
EXPECT_FALSE(controller->IsActive());
EXPECT_FALSE(controller->is_recording_in_progress());
}
TEST_P(CaptureModeTest, ClosingDisplayBeingFullscreenRecorded) {
UpdateDisplay("500x400,401+0-500x400");
auto roots = Shell::GetAllRootWindows();
ASSERT_EQ(2u, roots.size());
StartCaptureSession(CaptureModeSource::kFullscreen, CaptureModeType::kVideo);
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseTo(roots[1]->GetBoundsInScreen().CenterPoint());
auto* controller = CaptureModeController::Get();
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
auto* stop_recording_button = RootWindowController::ForWindow(roots[1])
->GetStatusAreaWidget()
->stop_recording_button_tray();
EXPECT_TRUE(stop_recording_button->visible_preferred());
// Disconnecting the display being fullscreen recorded should end the
// recording and remove the stop recording button.
base::HistogramTester histogram_tester;
RemoveSecondaryDisplay();
roots = Shell::GetAllRootWindows();
ASSERT_EQ(1u, roots.size());
EXPECT_FALSE(controller->is_recording_in_progress());
stop_recording_button = RootWindowController::ForWindow(roots[0])
->GetStatusAreaWidget()
->stop_recording_button_tray();
EXPECT_FALSE(stop_recording_button->visible_preferred());
histogram_tester.ExpectBucketCount(
kEndRecordingReasonInClamshellHistogramName,
EndRecordingReason::kDisplayOrWindowClosing, 1);
}
TEST_P(CaptureModeTest, ShuttingDownWhileRecording) {
StartCaptureSession(CaptureModeSource::kFullscreen, CaptureModeType::kVideo);
auto* controller = CaptureModeController::Get();
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
// Exiting the test now will shut down ash while recording is in progress,
// there should be no crashes when
// VideoRecordingWatcher::OnChromeTerminating() terminates the recording.
}
// Tests that metrics are recorded properly for capture mode bar buttons.
TEST_P(CaptureModeTest, CaptureModeBarButtonTypeHistograms) {
constexpr char kClamshellHistogram[] =
"Ash.CaptureModeController.BarButtons.ClamshellMode";
constexpr char kTabletHistogram[] =
"Ash.CaptureModeController.BarButtons.TabletMode";
base::HistogramTester histogram_tester;
CaptureModeController::Get()->Start(CaptureModeEntryType::kQuickSettings);
auto* event_generator = GetEventGenerator();
// Tests each bar button in clamshell mode.
ClickOnView(GetImageToggleButton(), event_generator);
histogram_tester.ExpectBucketCount(
kClamshellHistogram, CaptureModeBarButtonType::kScreenCapture, 1);
ClickOnView(GetVideoToggleButton(), event_generator);
histogram_tester.ExpectBucketCount(
kClamshellHistogram, CaptureModeBarButtonType::kScreenRecord, 1);
ClickOnView(GetFullscreenToggleButton(), event_generator);
histogram_tester.ExpectBucketCount(kClamshellHistogram,
CaptureModeBarButtonType::kFull, 1);
ClickOnView(GetRegionToggleButton(), event_generator);
histogram_tester.ExpectBucketCount(kClamshellHistogram,
CaptureModeBarButtonType::kRegion, 1);
ClickOnView(GetWindowToggleButton(), event_generator);
histogram_tester.ExpectBucketCount(kClamshellHistogram,
CaptureModeBarButtonType::kWindow, 1);
// Enter tablet mode and test the bar buttons.
ash::TabletModeControllerTestApi().EnterTabletMode();
ASSERT_TRUE(display::Screen::GetScreen()->InTabletMode());
ClickOnView(GetImageToggleButton(), event_generator);
histogram_tester.ExpectBucketCount(
kTabletHistogram, CaptureModeBarButtonType::kScreenCapture, 1);
ClickOnView(GetVideoToggleButton(), event_generator);
histogram_tester.ExpectBucketCount(
kTabletHistogram, CaptureModeBarButtonType::kScreenRecord, 1);
ClickOnView(GetFullscreenToggleButton(), event_generator);
histogram_tester.ExpectBucketCount(kTabletHistogram,
CaptureModeBarButtonType::kFull, 1);
ClickOnView(GetRegionToggleButton(), event_generator);
histogram_tester.ExpectBucketCount(kTabletHistogram,
CaptureModeBarButtonType::kRegion, 1);
ClickOnView(GetWindowToggleButton(), event_generator);
histogram_tester.ExpectBucketCount(kTabletHistogram,
CaptureModeBarButtonType::kWindow, 1);
}
TEST_P(CaptureModeTest, CaptureSessionSwitchedModeMetric) {
auto [sunfish_enabled, scanner_enabled] = GetParam();
if (sunfish_enabled || scanner_enabled) {
// This test fails when either Sunfish or Scanner is enabled.
// TODO: b/381965299 - Fix these test failures.
GTEST_SKIP();
}
constexpr char kHistogramName[] =
"Ash.CaptureModeController.SwitchesFromInitialCaptureMode";
base::HistogramTester histogram_tester;
histogram_tester.ExpectBucketCount(kHistogramName, false, 0);
histogram_tester.ExpectBucketCount(kHistogramName, true, 0);
// Perform a capture without switching modes. A false should be recorded.
auto* controller = StartImageRegionCapture();
SelectRegion(gfx::Rect(100, 100));
auto* event_generator = GetEventGenerator();
SendKey(ui::VKEY_RETURN, event_generator);
histogram_tester.ExpectBucketCount(kHistogramName, false, 1);
histogram_tester.ExpectBucketCount(kHistogramName, true, 0);
// Perform a capture after switching to fullscreen mode. A true should be
// recorded.
controller->Start(CaptureModeEntryType::kQuickSettings);
ClickOnView(GetFullscreenToggleButton(), event_generator);
SendKey(ui::VKEY_RETURN, event_generator);
histogram_tester.ExpectBucketCount(kHistogramName, false, 1);
histogram_tester.ExpectBucketCount(kHistogramName, true, 1);
// Perform a capture after switching to another mode and back to the original
// mode. A true should still be recorded as there was some switching done.
controller->Start(CaptureModeEntryType::kQuickSettings);
ClickOnView(GetRegionToggleButton(), event_generator);
ClickOnView(GetFullscreenToggleButton(), event_generator);
SendKey(ui::VKEY_RETURN, event_generator);
histogram_tester.ExpectBucketCount(kHistogramName, false, 1);
histogram_tester.ExpectBucketCount(kHistogramName, true, 2);
}
// Test that cancel recording during countdown won't cause crash.
TEST_P(CaptureModeTest, CancelCaptureDuringCountDown) {
ui::ScopedAnimationDurationScaleMode animation_scale(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
StartCaptureSession(CaptureModeSource::kFullscreen, CaptureModeType::kVideo);
// Hit Enter to begin recording, Wait for 1 second, then press ESC while count
// down is in progress.
auto* event_generator = GetEventGenerator();
SendKey(ui::VKEY_RETURN, event_generator);
WaitForSeconds(1);
CaptureModeTestApi test_api;
EXPECT_TRUE(test_api.IsInCountDownAnimation());
SendKey(ui::VKEY_ESCAPE, event_generator);
EXPECT_FALSE(test_api.IsInCountDownAnimation());
EXPECT_FALSE(test_api.IsSessionActive());
EXPECT_FALSE(test_api.IsVideoRecordingInProgress());
}
TEST_P(CaptureModeTest, EscDuringCountDownWhileSettingsOpen) {
ui::ScopedAnimationDurationScaleMode animation_scale(
ui::ScopedAnimationDurationScaleMode::NORMAL_DURATION);
StartCaptureSession(CaptureModeSource::kFullscreen, CaptureModeType::kVideo);
// Hitting Esc while the settings menu is open and the count down is in
// progress should end the session directly.
auto* event_generator = GetEventGenerator();
ClickOnView(GetSettingsButton(), event_generator);
EXPECT_TRUE(GetCaptureModeSettingsWidget());
SendKey(ui::VKEY_RETURN, event_generator);
WaitForSeconds(1);
CaptureModeTestApi test_api;
EXPECT_TRUE(test_api.IsInCountDownAnimation());
SendKey(ui::VKEY_ESCAPE, event_generator);
EXPECT_FALSE(test_api.IsInCountDownAnimation());
EXPECT_FALSE(test_api.IsSessionActive());
EXPECT_FALSE(test_api.IsVideoRecordingInProgress());
}
// Tests that metrics are recorded properly for capture region adjustments.
TEST_P(CaptureModeTest, NumberOfCaptureRegionAdjustmentsHistogram) {
constexpr char kClamshellHistogram[] =
"Ash.CaptureModeController.CaptureRegionAdjusted.ClamshellMode";
constexpr char kTabletHistogram[] =
"Ash.CaptureModeController.CaptureRegionAdjusted.TabletMode";
base::HistogramTester histogram_tester;
UpdateDisplay("800x700");
auto* controller = StartImageRegionCapture();
// Create the initial region.
const gfx::Rect target_region(gfx::Rect(200, 200, 400, 400));
SelectRegion(target_region);
auto resize_and_reset_region = [](ui::test::EventGenerator* event_generator,
const gfx::Point& top_right) {
// Enlarges the region and then resize it back to its original size.
event_generator->set_current_screen_location(top_right);
event_generator->DragMouseTo(top_right + gfx::Vector2d(50, 50));
event_generator->DragMouseTo(top_right);
};
auto move_and_reset_region = [](ui::test::EventGenerator* event_generator,
const gfx::Point& drag_point) {
// Moves the region and then moves it back to its original position.
event_generator->set_current_screen_location(drag_point);
event_generator->DragMouseTo(drag_point + gfx::Vector2d(-50, -50));
event_generator->DragMouseTo(drag_point);
};
// Resize the region twice by dragging the top right of the region out and
// then back again.
auto* event_generator = GetEventGenerator();
auto top_right = target_region.top_right();
resize_and_reset_region(event_generator, top_right);
// Move the region twice by dragging within the region.
const gfx::Point drag_point(300, 300);
move_and_reset_region(event_generator, drag_point);
// Perform a capture to record the count.
controller->PerformCapture();
histogram_tester.ExpectBucketCount(kClamshellHistogram, 4, 1);
// Create a new image region capture. Move the region twice then change
// sources to fullscreen and back to region. This toggle should reset the
// count. Perform a capture to record the count.
StartImageRegionCapture();
move_and_reset_region(event_generator, drag_point);
controller->SetSource(CaptureModeSource::kFullscreen);
controller->SetSource(CaptureModeSource::kRegion);
controller->PerformCapture();
histogram_tester.ExpectBucketCount(kClamshellHistogram, 0, 1);
// Enter tablet mode and restart the capture session. The capture region
// should be remembered.
ash::TabletModeControllerTestApi().EnterTabletMode();
ASSERT_TRUE(display::Screen::GetScreen()->InTabletMode());
StartImageRegionCapture();
ASSERT_EQ(target_region, controller->user_capture_region());
// Resize the region twice by dragging the top right of the region out and
// then back again.
resize_and_reset_region(event_generator, top_right);
// Move the region twice by dragging within the region.
move_and_reset_region(event_generator, drag_point);
// Perform a capture to record the count.
controller->PerformCapture();
histogram_tester.ExpectBucketCount(kTabletHistogram, 4, 1);
// Restart the region capture and resize it. Then create a new region by
// dragging outside of the existing capture region. This should reset the
// counter. Change source to record a sample.
StartImageRegionCapture();
resize_and_reset_region(event_generator, top_right);
SelectRegion(gfx::Rect(0, 0, 100, 100));
controller->PerformCapture();
histogram_tester.ExpectBucketCount(kTabletHistogram, 0, 1);
}
TEST_P(CaptureModeTest, ResizeRegionBoundedByDisplay) {
UpdateDisplay("800x700");
auto* controller = StartImageRegionCapture();
ASSERT_TRUE(controller->IsActive());
ASSERT_EQ(CaptureModeSource::kRegion, controller->source());
// Attempt to create a new region that goes outside of the display bounds.
gfx::Rect target_region(gfx::Rect(200, 200, 800, 800));
auto* event_generator = GetEventGenerator();
event_generator->set_current_screen_location(target_region.origin());
event_generator->PressLeftButton();
event_generator->MoveMouseTo(target_region.bottom_right());
event_generator->ReleaseLeftButton();
// The region should stay within the display bounds.
ASSERT_TRUE(
controller->capture_mode_session()->current_root()->bounds().Contains(
controller->user_capture_region()));
EXPECT_EQ(gfx::Rect(200, 200, 600, 500), controller->user_capture_region());
// Attempt to adjust the existing region outside of the display bounds.
event_generator->set_current_screen_location(target_region.origin());
event_generator->PressLeftButton();
event_generator->MoveMouseTo(gfx::Point(-100, -100));
event_generator->ReleaseLeftButton();
// The region should stay within the display bounds.
ASSERT_TRUE(
controller->capture_mode_session()->current_root()->bounds().Contains(
controller->user_capture_region()));
EXPECT_EQ(gfx::Rect(0, 0, 800, 700), controller->user_capture_region());
}
TEST_P(CaptureModeTest, FullscreenCapture) {
ui::ScopedAnimationDurationScaleMode animation_scale(
ui::ScopedAnimationDurationScaleMode::NON_ZERO_DURATION);
CaptureModeController* controller = StartCaptureSession(
CaptureModeSource::kFullscreen, CaptureModeType::kImage);
EXPECT_TRUE(controller->IsActive());
// Press anywhere to capture image.
auto* event_generator = GetEventGenerator();
event_generator->ClickLeftButton();
EXPECT_FALSE(controller->IsActive());
controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kVideo);
EXPECT_TRUE(controller->IsActive());
// Press anywhere to capture video.
event_generator->ClickLeftButton();
WaitForRecordingToStart();
EXPECT_FALSE(controller->IsActive());
}
// Tests that there is no crash when touching the capture label widget in tablet
// mode when capturing a window. Regression test for https://crbug.com/1152938.
TEST_P(CaptureModeTest, TabletTouchCaptureLabelWidgetWindowMode) {
SwitchToTabletMode();
// Enter capture window mode.
CaptureModeController* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kImage);
ASSERT_TRUE(controller->IsActive());
// Press and release on where the capture label widget would be.
auto* event_generator = GetEventGenerator();
DCHECK(GetCaptureModeLabelWidget());
event_generator->set_current_screen_location(
GetCaptureModeLabelWidget()->GetWindowBoundsInScreen().CenterPoint());
event_generator->PressTouch();
event_generator->ReleaseTouch();
// There are no windows and home screen window is excluded from window capture
// mode, so capture mode will still remain active.
EXPECT_TRUE(Shell::Get()->app_list_controller()->IsHomeScreenVisible());
EXPECT_TRUE(controller->IsActive());
}
// Tests that after rotating a display, the capture session widgets are updated
// and the capture region is reset.
TEST_P(CaptureModeTest, DisplayRotation) {
auto [sunfish_enabled, scanner_enabled] = GetParam();
if (sunfish_enabled || scanner_enabled) {
// This test crashes when either Sunfish or Scanner is enabled.
// TODO: b/381965299 - Fix these test failures.
GTEST_SKIP();
}
UpdateDisplay("1200x600");
auto* controller = StartImageRegionCapture();
SelectRegion(gfx::Rect(1200, 400));
OpenSettingsView();
// Rotate the primary display by 90 degrees. Test that the region, capture
// bar and capture settings fit within the rotated bounds, and the capture
// label widget is still centered in the region.
Shell::Get()->display_manager()->SetDisplayRotation(
WindowTreeHostManager::GetPrimaryDisplayId(), display::Display::ROTATE_90,
display::Display::RotationSource::USER);
const gfx::Rect rotated_root_bounds(600, 1200);
EXPECT_TRUE(rotated_root_bounds.Contains(controller->user_capture_region()));
const gfx::Rect capture_bar_bounds =
GetCaptureModeBarView()->GetBoundsInScreen();
const gfx::Rect settings_bounds =
CaptureModeSettingsTestApi().GetSettingsView()->GetBoundsInScreen();
EXPECT_TRUE(rotated_root_bounds.Contains(capture_bar_bounds));
EXPECT_TRUE(rotated_root_bounds.Contains(settings_bounds));
// Verify that the space between the bottom of the settings and the top
// of the capture bar is `kSpaceBetweenCaptureBarAndSettingsMenu`.
EXPECT_EQ(capture_bar_bounds.y() - settings_bounds.bottom(),
capture_mode::kSpaceBetweenCaptureBarAndSettingsMenu);
views::Widget* capture_label_widget = GetCaptureModeLabelWidget();
ASSERT_TRUE(capture_label_widget);
EXPECT_EQ(controller->user_capture_region().CenterPoint(),
capture_label_widget->GetWindowBoundsInScreen().CenterPoint());
}
TEST_P(CaptureModeTest, DisplayBoundsChange) {
UpdateDisplay("1200x600");
auto* controller = StartImageRegionCapture();
SelectRegion(gfx::Rect(1200, 400));
// Shrink the display. The capture region should shrink, and the capture bar
// should be adjusted to be centered.
UpdateDisplay("700x600");
EXPECT_EQ(gfx::Rect(700, 400), controller->user_capture_region());
EXPECT_EQ(350,
GetCaptureModeBarView()->GetBoundsInScreen().CenterPoint().x());
}
TEST_P(CaptureModeTest, ReenterOnSmallerDisplay) {
UpdateDisplay("1200x600,1201+0-700x600");
// Start off with the primary display as the targeted display. Create a region
// that fits the primary display but would be too big for the secondary
// display.
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseTo(gfx::Point(700, 300));
auto* controller = StartImageRegionCapture();
SelectRegion(gfx::Rect(1200, 400));
EXPECT_EQ(gfx::Rect(1200, 400), controller->user_capture_region());
controller->Stop();
// Make the secondary display the targeted display. Test that the region has
// shrunk to fit the display.
event_generator->MoveMouseTo(gfx::Point(1500, 300));
StartImageRegionCapture();
EXPECT_EQ(gfx::Rect(700, 400), controller->user_capture_region());
}
// Tests tabbing when in capture window mode.
TEST_P(CaptureModeTest, KeyboardNavigationBasic) {
auto* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kImage);
using FocusGroup = CaptureModeSessionFocusCycler::FocusGroup;
CaptureModeSessionTestApi test_api(controller->capture_mode_session());
// Initially nothing is focused.
EXPECT_EQ(FocusGroup::kNone, test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
// Tab once, we are now focusing the type and source buttons group on the
// capture bar.
auto* event_generator = GetEventGenerator();
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(FocusGroup::kTypeSource, test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
// Tab four times to focus the last source button (window mode button).
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/4);
EXPECT_EQ(FocusGroup::kTypeSource, test_api.GetCurrentFocusGroup());
EXPECT_EQ(4u, test_api.GetCurrentFocusIndex());
// Tab once to focus the settings and close buttons group on the capture bar.
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(FocusGroup::kSettingsClose, test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
// Shift tab to focus the last source button again.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_SHIFT_DOWN);
EXPECT_EQ(FocusGroup::kTypeSource, test_api.GetCurrentFocusGroup());
EXPECT_EQ(4u, test_api.GetCurrentFocusIndex());
// Press esc to clear focus, but remain in capture mode.
SendKey(ui::VKEY_ESCAPE, event_generator);
EXPECT_EQ(FocusGroup::kNone, test_api.GetCurrentFocusGroup());
EXPECT_TRUE(controller->IsActive());
// Tests that pressing esc when there is no focus will exit capture mode.
SendKey(ui::VKEY_ESCAPE, event_generator);
EXPECT_FALSE(controller->IsActive());
}
// Tests tabbing through windows on multiple displays when in capture window
// mode.
TEST_P(CaptureModeTest, KeyboardNavigationTabThroughWindowsOnMultipleDisplays) {
UpdateDisplay("800x700,801+0-800x700");
std::vector<raw_ptr<aura::Window, VectorExperimental>> root_windows =
Shell::GetAllRootWindows();
ASSERT_EQ(2u, root_windows.size());
// Create three windows, one of them is a modal transient child.
std::unique_ptr<aura::Window> window1(
CreateTestWindow(gfx::Rect(0, 0, 200, 200)));
auto window1_transient = CreateTransientModalChildWindow(
gfx::Rect(20, 30, 200, 150), window1.get());
std::unique_ptr<aura::Window> window2(
CreateTestWindow(gfx::Rect(900, 0, 200, 200)));
auto* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kImage);
auto* capture_mode_session =
static_cast<CaptureModeSession*>(controller->capture_mode_session());
ASSERT_EQ(capture_mode_session->session_type(), SessionType::kReal);
using FocusGroup = CaptureModeSessionFocusCycler::FocusGroup;
CaptureModeSessionTestApi test_api(capture_mode_session);
// Initially nothing is focused.
EXPECT_EQ(FocusGroup::kNone, test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
// Tab five times, we are now focusing the window mode button on the
// capture bar.
auto* event_generator = GetEventGenerator();
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/5);
EXPECT_EQ(FocusGroup::kTypeSource, test_api.GetCurrentFocusGroup());
EXPECT_EQ(4u, test_api.GetCurrentFocusIndex());
// Enter space to select window mode.
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_EQ(FocusGroup::kTypeSource, test_api.GetCurrentFocusGroup());
EXPECT_EQ(CaptureModeSource::kWindow, controller->source());
// Tab once, we are now focusing |window2| and capture mode bar is on
// display2.
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(FocusGroup::kCaptureWindow, test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
EXPECT_EQ(window2.get(), capture_mode_session->GetSelectedWindow());
EXPECT_EQ(root_windows[1], capture_mode_session->current_root());
// Tab once, we are now focusing |window1_transient|. Since
// |window1_transient| is on display1, capture mode bar will be moved to
// display1 as well.
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(FocusGroup::kCaptureWindow, test_api.GetCurrentFocusGroup());
EXPECT_EQ(1u, test_api.GetCurrentFocusIndex());
EXPECT_EQ(window1_transient.get(), capture_mode_session->GetSelectedWindow());
EXPECT_EQ(root_windows[0], capture_mode_session->current_root());
// Tab once, we are now focusing |window1|. Capture mode bar still stays on
// display1.
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(FocusGroup::kCaptureWindow, test_api.GetCurrentFocusGroup());
EXPECT_EQ(2u, test_api.GetCurrentFocusIndex());
EXPECT_EQ(window1.get(), capture_mode_session->GetSelectedWindow());
EXPECT_EQ(root_windows[0], capture_mode_session->current_root());
// Press space, make sure nothing is changed and no crash.
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_TRUE(controller->IsActive());
EXPECT_EQ(window1.get(), capture_mode_session->GetSelectedWindow());
EXPECT_EQ(root_windows[0], capture_mode_session->current_root());
// Tab once to focus the settings and close buttons group on the capture bar.
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(FocusGroup::kSettingsClose, test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
// Shift tab to focus |window1| again.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_SHIFT_DOWN);
EXPECT_EQ(FocusGroup::kCaptureWindow, test_api.GetCurrentFocusGroup());
EXPECT_EQ(2u, test_api.GetCurrentFocusIndex());
EXPECT_EQ(window1.get(), capture_mode_session->GetSelectedWindow());
// Shift tab to focus |window1_transient|.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_SHIFT_DOWN);
EXPECT_EQ(FocusGroup::kCaptureWindow, test_api.GetCurrentFocusGroup());
EXPECT_EQ(1u, test_api.GetCurrentFocusIndex());
EXPECT_EQ(window1_transient.get(), capture_mode_session->GetSelectedWindow());
EXPECT_EQ(root_windows[0], capture_mode_session->current_root());
// Shift tab to focus |window2|. Capture mode bar will be moved to display2 as
// well.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_SHIFT_DOWN);
EXPECT_EQ(FocusGroup::kCaptureWindow, test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
EXPECT_EQ(window2.get(), capture_mode_session->GetSelectedWindow());
EXPECT_EQ(root_windows[1], capture_mode_session->current_root());
// Press esc to clear focus, but remain in capture mode with |window2|
// selected.
SendKey(ui::VKEY_ESCAPE, event_generator);
EXPECT_EQ(FocusGroup::kNone, test_api.GetCurrentFocusGroup());
EXPECT_TRUE(controller->IsActive());
EXPECT_EQ(window2.get(), capture_mode_session->GetSelectedWindow());
EXPECT_EQ(root_windows[1], capture_mode_session->current_root());
// Press return. Since there's a selected window, capture mode will
// be ended after capturing the selected window.
SendKey(ui::VKEY_RETURN, event_generator);
EXPECT_FALSE(controller->IsActive());
}
// Tests tabbing through partial capture affordance circles, which are drawn
// directly on a layer and therefore have a custom implementation using
// `views::AxVirtualView`.
TEST_P(CaptureModeTest, KeyboardNavigationAffordanceCircles) {
// We need two displays to test display removal.
UpdateDisplay("800x700,801+0-800x700");
// The `views::AxVirtualView`'s are not created unless they are needed
// (ChromeVox is enabled).
Shell::Get()->accessibility_controller()->spoken_feedback().SetEnabled(true);
auto* controller =
StartCaptureSession(CaptureModeSource::kRegion, CaptureModeType::kImage);
CaptureModeSessionTestApi test_api(controller->capture_mode_session());
// Select a region and ensure it is on the secondary display, we will test
// removing the display while focused on an affordance circle on the secondary
// display later.
const gfx::Rect target_region(gfx::Rect(850, 50, 200, 200));
SelectRegion(target_region);
ASSERT_EQ(Shell::GetAllRootWindows().size(), 2u);
ASSERT_EQ(test_api.GetCaptureModeBarView()
->GetWidget()
->GetNativeWindow()
->GetRootWindow(),
Shell::GetAllRootWindows()[1]);
// Both the `views::AxVirtualView`s and their widget are created lazily; they
// won't be created until tabbed into.
EXPECT_FALSE(test_api.HasAxVirtualWidget());
EXPECT_EQ(test_api.GetAxVirtualViewsCount(), 0u);
// Tab through the bar until we reach the first element that needs a
// `views::AxVirtualView`.
while (test_api.GetCurrentFocusGroup() !=
CaptureModeSessionFocusCycler::FocusGroup::kSelection) {
SendKey(ui::VKEY_TAB, GetEventGenerator());
}
EXPECT_TRUE(test_api.HasAxVirtualWidget());
EXPECT_EQ(test_api.GetAxVirtualViewsCount(), 1u);
// Tab through several circles and test that more `views::AxVirtualView`s have
// been lazily created.
SendKey(ui::VKEY_TAB, GetEventGenerator(), ui::EF_NONE, /*count=*/2);
EXPECT_EQ(test_api.GetAxVirtualViewsCount(), 3u);
// Test that when we remove the secondary display, the `views::AxVirtualView`s
// and their widget are destroyed.
RemoveSecondaryDisplay();
EXPECT_FALSE(test_api.HasAxVirtualWidget());
EXPECT_EQ(test_api.GetAxVirtualViewsCount(), 0u);
}
// Tests that a click will remove focus.
TEST_P(CaptureModeTest, KeyboardNavigationClicksRemoveFocus) {
auto* controller = StartImageRegionCapture();
auto* event_generator = GetEventGenerator();
CaptureModeSessionTestApi test_api(controller->capture_mode_session());
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_TRUE(test_api.HasFocus());
event_generator->ClickLeftButton();
EXPECT_FALSE(test_api.HasFocus());
}
// Tests that pressing space on a focused button will activate it.
TEST_P(CaptureModeTest, KeyboardNavigationSpaceToActivateButton) {
auto [sunfish_enabled, scanner_enabled] = GetParam();
if (sunfish_enabled || scanner_enabled) {
// This test fails when either Sunfish or Scanner is enabled.
// TODO: b/381965299 - Fix these test failures.
GTEST_SKIP();
}
auto* controller = StartImageRegionCapture();
SelectRegion(gfx::Rect(200, 200));
auto* event_generator = GetEventGenerator();
// Tab to the button which changes the capture type to video and hit space.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/2);
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_EQ(CaptureModeType::kVideo, controller->type());
// Shift tab and space to change the capture type back to image.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_SHIFT_DOWN);
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_EQ(CaptureModeType::kImage, controller->type());
// Tab to the fullscreen button and hit space.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/2);
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_EQ(CaptureModeSource::kFullscreen, controller->source());
// Tab to the region button and hit space to return to region capture mode.
SendKey(ui::VKEY_TAB, event_generator);
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_EQ(CaptureModeSource::kRegion, controller->source());
// Tab to the capture button and hit space to perform a capture, which exits
// capture mode.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/11);
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_FALSE(controller->IsActive());
}
// Tests that functionality to create and adjust a region with keyboard
// shortcuts works as intended.
TEST_P(CaptureModeTest, KeyboardNavigationSelectRegion) {
auto* controller = StartImageRegionCapture();
auto* event_generator = GetEventGenerator();
ASSERT_TRUE(controller->user_capture_region().IsEmpty());
// Test that hitting space will create a default region.
SendKey(ui::VKEY_SPACE, event_generator);
gfx::Rect capture_region = controller->user_capture_region();
EXPECT_FALSE(capture_region.IsEmpty());
// Test that hitting an arrow key will do nothing as the selection region is
// not focused initially.
SendKey(ui::VKEY_RIGHT, event_generator);
EXPECT_EQ(capture_region, controller->user_capture_region());
const int arrow_shift = capture_mode::kArrowKeyboardRegionChangeDp;
// Hit tab until the whole region is focused.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/6);
CaptureModeSessionTestApi test_api(controller->capture_mode_session());
EXPECT_EQ(CaptureModeSessionFocusCycler::FocusGroup::kSelection,
test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
// Arrow keys should shift the whole region.
SendKey(ui::VKEY_RIGHT, event_generator);
EXPECT_EQ(capture_region.origin() + gfx::Vector2d(arrow_shift, 0),
controller->user_capture_region().origin());
EXPECT_EQ(capture_region.size(), controller->user_capture_region().size());
SendKey(ui::VKEY_RIGHT, event_generator, ui::EF_SHIFT_DOWN);
EXPECT_EQ(
capture_region.origin() +
gfx::Vector2d(
arrow_shift + capture_mode::kShiftArrowKeyboardRegionChangeDp, 0),
controller->user_capture_region().origin());
EXPECT_EQ(capture_region.size(), controller->user_capture_region().size());
// Hit tab so that the top left affordance circle is focused. Left and up keys
// should enlarge the region, right and bottom keys should shrink the region.
capture_region = controller->user_capture_region();
SendKey(ui::VKEY_TAB, event_generator);
SendKey(ui::VKEY_LEFT, event_generator);
SendKey(ui::VKEY_UP, event_generator);
EXPECT_EQ(capture_region.size() + gfx::Size(arrow_shift, arrow_shift),
controller->user_capture_region().size());
SendKey(ui::VKEY_RIGHT, event_generator);
SendKey(ui::VKEY_DOWN, event_generator);
EXPECT_EQ(capture_region.size(), controller->user_capture_region().size());
// Tab until we focus the bottom right affordance circle. Left and up keys
// should shrink the region, right and bottom keys should enlarge the region.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/4);
SendKey(ui::VKEY_LEFT, event_generator);
SendKey(ui::VKEY_UP, event_generator);
EXPECT_EQ(capture_region.size() - gfx::Size(arrow_shift, arrow_shift),
controller->user_capture_region().size());
SendKey(ui::VKEY_RIGHT, event_generator);
SendKey(ui::VKEY_DOWN, event_generator);
EXPECT_EQ(capture_region.size(), controller->user_capture_region().size());
}
// Tests behavior regarding the default region when using keyboard navigation.
TEST_P(CaptureModeTest, KeyboardNavigationDefaultRegion) {
auto [sunfish_enabled, scanner_enabled] = GetParam();
if (sunfish_enabled || scanner_enabled) {
// This test fails when either Sunfish or Scanner is enabled.
// TODO: b/381965299 - Fix these test failures.
GTEST_SKIP();
}
auto* controller = StartImageRegionCapture();
auto* event_generator = GetEventGenerator();
ASSERT_TRUE(controller->user_capture_region().IsEmpty());
// Hit space when nothing is focused to get the expected default capture
// region.
SendKey(ui::VKEY_SPACE, event_generator);
const gfx::Rect expected_default_region = controller->user_capture_region();
SelectRegion(gfx::Rect(20, 20, 200, 200));
// Hit space when there is an existing region. Tests that the region remains
// unchanged.
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_EQ(gfx::Rect(20, 20, 200, 200), controller->user_capture_region());
// Tab to the image toggle button. Tests that hitting space does not change
// the region size.
SelectRegion(gfx::Rect());
SendKey(ui::VKEY_TAB, event_generator);
CaptureModeSessionTestApi test_api(controller->capture_mode_session());
ASSERT_EQ(CaptureModeSessionFocusCycler::FocusGroup::kTypeSource,
test_api.GetCurrentFocusGroup());
ASSERT_EQ(0u, test_api.GetCurrentFocusIndex());
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_EQ(gfx::Rect(), controller->user_capture_region());
// Tests that hitting space while focusing the region toggle button when in
// region capture mode will make the capture region the default size.
// SelectRegion removes focus since it uses mouse clicks.
SelectRegion(gfx::Rect());
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE,
/*count=*/4);
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_EQ(expected_default_region, controller->user_capture_region());
// Tests that hitting space while focusing the region toggle button when not
// in region capture mode does nothing to the capture region.
SelectRegion(gfx::Rect());
ClickOnView(GetWindowToggleButton(), event_generator);
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE,
/*count=*/4);
ASSERT_EQ(CaptureModeSessionFocusCycler::FocusGroup::kTypeSource,
test_api.GetCurrentFocusGroup());
ASSERT_EQ(3u, test_api.GetCurrentFocusIndex());
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_EQ(gfx::Rect(), controller->user_capture_region());
}
TEST_P(CaptureModeTest, A11yEnterWithNoFocus) {
auto* controller = StartImageRegionCapture();
SelectRegion(gfx::Rect(20, 20, 200, 200));
CaptureModeSessionTestApi test_api(controller->capture_mode_session());
ASSERT_EQ(CaptureModeSessionFocusCycler::FocusGroup::kNone,
test_api.GetCurrentFocusGroup());
// When nothing is focused, the `Enter` key should perform the capture.
auto* event_generator = GetEventGenerator();
SendKey(ui::VKEY_RETURN, event_generator);
EXPECT_FALSE(controller->IsActive());
}
TEST_P(CaptureModeTest, A11yEnterWithFocusOnRegionKnob) {
auto* controller = StartImageRegionCapture();
SelectRegion(gfx::Rect(20, 20, 200, 200));
CaptureModeSessionTestApi test_api(controller->capture_mode_session());
// Tab until you reach the region adjustment focus group.
auto* event_generator = GetEventGenerator();
while (test_api.GetCurrentFocusGroup() !=
CaptureModeSessionFocusCycler::FocusGroup::kSelection) {
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE);
}
// Tab twice more to be on one of the knobs.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/2);
ASSERT_EQ(CaptureModeSessionFocusCycler::FocusGroup::kSelection,
test_api.GetCurrentFocusGroup());
// Enter should perform capture.
SendKey(ui::VKEY_RETURN, event_generator);
EXPECT_FALSE(controller->IsActive());
}
TEST_P(CaptureModeTest, A11yEnterWithFocusOnWindow) {
auto window = CreateTestWindow(gfx::Rect(200, 200));
auto* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kImage);
CaptureModeSessionTestApi test_api(controller->capture_mode_session());
// Tab until you reach the window to be captured.
auto* event_generator = GetEventGenerator();
while (test_api.GetCurrentFocusGroup() !=
CaptureModeSessionFocusCycler::FocusGroup::kCaptureWindow) {
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE);
}
// Enter should perform capture.
SendKey(ui::VKEY_RETURN, event_generator);
EXPECT_FALSE(controller->IsActive());
}
TEST_P(CaptureModeTest, A11yEnterWithFocusOnFullscreenButton) {
auto* controller = StartImageRegionCapture();
EXPECT_EQ(controller->source(), CaptureModeSource::kRegion);
CaptureModeSessionTestApi test_api(controller->capture_mode_session());
// Tab once to enter focus into the bar.
auto* event_generator = GetEventGenerator();
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE);
ASSERT_EQ(CaptureModeSessionFocusCycler::FocusGroup::kTypeSource,
test_api.GetCurrentFocusGroup());
// Tab until you reach the fullscreen toggle button.
auto* fullscreen_toggle_button = test_api.GetCaptureModeBarView()
->GetCaptureSourceView()
->fullscreen_toggle_button();
while (test_api.GetCurrentFocusedView()->GetView() !=
fullscreen_toggle_button) {
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE);
}
// The first Enter will switch the source to `kFullscreen`.
SendKey(ui::VKEY_RETURN, event_generator);
EXPECT_TRUE(controller->IsActive());
EXPECT_EQ(controller->source(), CaptureModeSource::kFullscreen);
// The focus should not change.
EXPECT_EQ(test_api.GetCurrentFocusedView()->GetView(),
fullscreen_toggle_button);
// The second Enter should perform capture.
SendKey(ui::VKEY_RETURN, event_generator);
EXPECT_FALSE(controller->IsActive());
}
// Tests that the UAF issue caused by `NotifyAccessibilityEvent` after the
// button been destroyed has been handled without leading to a crash.
TEST_P(CaptureModeTest, KeyboardNavigationButtonDestroyedAfterBeenActivated) {
auto [sunfish_enabled, scanner_enabled] = GetParam();
if (sunfish_enabled) {
// This test crashes if Sunfish is enabled.
// TODO: b/381965299 - Fix these test failures.
GTEST_SKIP();
}
if (scanner_enabled) {
// This test fails if Sunfish is disabled and Scanner is enabled.
// TODO: b/381965299 - Fix these test failures.
GTEST_SKIP();
}
auto* controller = StartImageRegionCapture();
SelectRegion(gfx::Rect(200, 300));
using FocusGroup = CaptureModeSessionFocusCycler::FocusGroup;
auto* event_generator = GetEventGenerator();
// Tab 15 times to reach the capture button and press space key to activate
// the button.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/15);
EXPECT_EQ(FocusGroup::kCaptureButton,
CaptureModeSessionTestApi(controller->capture_mode_session())
.GetCurrentFocusGroup());
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_FALSE(controller->IsActive());
controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kImage);
// Tab 7 times to reach the close button and press space key to activate the
// button.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/7);
EXPECT_EQ(FocusGroup::kSettingsClose,
CaptureModeSessionTestApi(controller->capture_mode_session())
.GetCurrentFocusGroup());
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_FALSE(controller->IsActive());
}
// Tests that accessibility overrides are set as expected on capture mode
// widgets.
TEST_P(CaptureModeTest, AccessibilityFocusAnnotator) {
auto [sunfish_enabled, scanner_enabled] = GetParam();
if (sunfish_enabled || scanner_enabled) {
// This test fails when either Sunfish or Scanner is enabled.
// TODO: b/381965299 - Fix these test failures.
GTEST_SKIP();
}
StartImageRegionCapture();
// Helper that takes in a current widget and checks if the accessibility next
// and previous focus widgets match the given.
auto check_a11y_overrides = [](const std::string& id, views::Widget* widget,
views::Widget* expected_previous,
views::Widget* expected_next) -> void {
SCOPED_TRACE(id);
views::View* contents_view = widget->GetContentsView();
views::ViewAccessibility& view_accessibility =
contents_view->GetViewAccessibility();
EXPECT_EQ(expected_previous, view_accessibility.GetPreviousWindowFocus());
EXPECT_EQ(expected_next, view_accessibility.GetNextWindowFocus());
};
// With no region, there is no capture label button and no settings menu
// opened, so the bar is the only focusable capture session widget.
views::Widget* bar_widget = GetCaptureModeBarWidget();
check_a11y_overrides("bar", bar_widget, nullptr, nullptr);
// With a region, the focus should go from the bar widget to the label widget
// and back.
SendKey(ui::VKEY_SPACE, GetEventGenerator());
views::Widget* label_widget = GetCaptureModeLabelWidget();
check_a11y_overrides("bar", bar_widget, label_widget, label_widget);
check_a11y_overrides("label", label_widget, bar_widget, bar_widget);
// With a settings menu open, the focus should go from the bar widget to the
// label widget to the settings widget and back to the bar widget.
ClickOnView(GetSettingsButton(), GetEventGenerator());
views::Widget* settings_widget = GetCaptureModeSettingsWidget();
ASSERT_TRUE(settings_widget);
check_a11y_overrides("bar", bar_widget, settings_widget, label_widget);
check_a11y_overrides("label", label_widget, bar_widget, settings_widget);
check_a11y_overrides("settings", settings_widget, label_widget, bar_widget);
}
// Tests that a captured image is written to the clipboard.
TEST_P(CaptureModeTest, ClipboardWrite) {
auto* clipboard = ui::Clipboard::GetForCurrentThread();
ASSERT_NE(clipboard, nullptr);
const ui::ClipboardSequenceNumberToken before_sequence_number =
clipboard->GetSequenceNumber(ui::ClipboardBuffer::kCopyPaste);
CaptureNotificationWaiter waiter;
CaptureModeController::Get()->CaptureScreenshotsOfAllDisplays();
waiter.Wait();
const ui::ClipboardSequenceNumberToken after_sequence_number =
clipboard->GetSequenceNumber(ui::ClipboardBuffer::kCopyPaste);
EXPECT_NE(before_sequence_number, after_sequence_number);
}
// Tests the reverse tabbing behavior of the keyboard navigation.
TEST_P(CaptureModeTest, ReverseTabbingTest) {
using FocusGroup = CaptureModeSessionFocusCycler::FocusGroup;
auto* event_generator = GetEventGenerator();
for (CaptureModeSource source :
{CaptureModeSource::kFullscreen, CaptureModeSource::kRegion,
CaptureModeSource::kWindow}) {
auto* controller = StartCaptureSession(source, CaptureModeType::kImage);
CaptureModeSessionTestApi test_api(controller->capture_mode_session());
// Nothing is focused initially.
EXPECT_EQ(FocusGroup::kNone, test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
// Reverse tabbing once and the focus should be on the close button.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_SHIFT_DOWN);
EXPECT_EQ(FocusGroup::kSettingsClose, test_api.GetCurrentFocusGroup());
EXPECT_TRUE(
CaptureModeSessionFocusCycler::HighlightHelper::Get(GetCloseButton())
->has_focus());
controller->Stop();
}
}
// A regression test for a UAF issue reported at https://crbug.com/1350743, in
// which if a the native widget of the settings menu gets deleted without
// calling `Close()` or `CloseNow()` on the widget, we get a UAF. This can
// happen when all the windows in the window tree hierarchy gets deleted e.g.
// when shutting down.
TEST_P(CaptureModeTest, SettingsMenuWidgetDestruction) {
CaptureModeTestApi().StartForFullscreen(true);
ClickOnView(GetSettingsButton(), GetEventGenerator());
auto* widget = GetCaptureModeSettingsWidget();
ASSERT_TRUE(widget);
delete widget->GetNativeWindow();
}
// A test class that uses a mock time task environment.
class CaptureModeMockTimeTest : public CaptureModeTest {
public:
CaptureModeMockTimeTest()
: CaptureModeTest(base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
CaptureModeMockTimeTest(const CaptureModeMockTimeTest&) = delete;
CaptureModeMockTimeTest& operator=(const CaptureModeMockTimeTest&) = delete;
~CaptureModeMockTimeTest() override = default;
};
INSTANTIATE_TEST_SUITE_P(
,
CaptureModeMockTimeTest,
testing::Combine(testing::Bool(), testing::Bool()),
[](const testing::TestParamInfo<CaptureModeMockTimeTest::ParamType>& info) {
bool sunfish_enabled = std::get<0>(info.param);
bool scanner_enabled = std::get<1>(info.param);
return SunfishScannerTestName(sunfish_enabled, scanner_enabled);
});
// Tests that the consecutive screenshots histogram is recorded properly.
TEST_P(CaptureModeMockTimeTest, ConsecutiveScreenshotsHistograms) {
constexpr char kConsecutiveScreenshotsHistogram[] =
"Ash.CaptureModeController.ConsecutiveScreenshots";
base::HistogramTester histogram_tester;
auto take_n_screenshots = [this](int n) {
for (int i = 0; i < n; ++i) {
auto* controller = StartImageRegionCapture();
controller->PerformCapture();
}
};
// Take three consecutive screenshots. Should only record after 5 seconds.
StartImageRegionCapture();
const gfx::Rect capture_region(200, 200, 400, 400);
SelectRegion(capture_region);
take_n_screenshots(3);
histogram_tester.ExpectBucketCount(kConsecutiveScreenshotsHistogram, 3, 0);
task_environment()->FastForwardBy(base::Seconds(5));
histogram_tester.ExpectBucketCount(kConsecutiveScreenshotsHistogram, 3, 1);
// Take only one screenshot. This should not be recorded.
take_n_screenshots(1);
histogram_tester.ExpectBucketCount(kConsecutiveScreenshotsHistogram, 1, 0);
task_environment()->FastForwardBy(base::Seconds(5));
histogram_tester.ExpectBucketCount(kConsecutiveScreenshotsHistogram, 1, 0);
// Take a screenshot, change source and take another screenshot. This should
// count as 2 consecutive screenshots.
take_n_screenshots(1);
auto* controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kImage);
controller->PerformCapture();
histogram_tester.ExpectBucketCount(kConsecutiveScreenshotsHistogram, 2, 0);
task_environment()->FastForwardBy(base::Seconds(5));
histogram_tester.ExpectBucketCount(kConsecutiveScreenshotsHistogram, 2, 1);
}
// Tests that the user capture region will be cleared up after a period of time.
TEST_P(CaptureModeMockTimeTest, ClearUserCaptureRegionBetweenSessions) {
UpdateDisplay("900x800");
auto* controller = StartImageRegionCapture();
EXPECT_EQ(gfx::Rect(), controller->user_capture_region());
const gfx::Rect capture_region(100, 100, 600, 700);
SelectRegion(capture_region);
EXPECT_EQ(capture_region, controller->user_capture_region());
controller->PerformCapture();
EXPECT_EQ(capture_region, controller->user_capture_region());
// Start region image capture again shortly after the previous capture
// session, we should still be able to reuse the previous capture region.
task_environment()->FastForwardBy(base::Minutes(1));
StartImageRegionCapture();
EXPECT_EQ(capture_region, controller->user_capture_region());
auto* event_generator = GetEventGenerator();
// Even if the capture is cancelled, we still remember the capture region.
SendKey(ui::VKEY_ESCAPE, event_generator);
EXPECT_EQ(capture_region, controller->user_capture_region());
// Wait for 8 second and then start region image capture again. We should have
// forgot the previous capture region.
task_environment()->FastForwardBy(base::Minutes(8));
StartImageRegionCapture();
EXPECT_EQ(gfx::Rect(), controller->user_capture_region());
}
// Tests that in Region mode, the capture bar hides and shows itself correctly.
TEST_P(CaptureModeTest, CaptureBarOpacity) {
auto [sunfish_enabled, scanner_enabled] = GetParam();
if (sunfish_enabled || scanner_enabled) {
// This test fails when either Sunfish or Scanner is enabled.
// TODO: b/381965299 - Fix these test failures.
GTEST_SKIP();
}
UpdateDisplay("800x700");
auto* event_generator = GetEventGenerator();
auto* controller = StartImageRegionCapture();
EXPECT_TRUE(controller->IsActive());
ui::Layer* capture_bar_layer = GetCaptureModeBarWidget()->GetLayer();
// Check to see it starts off opaque.
EXPECT_EQ(1.f, capture_bar_layer->GetTargetOpacity());
// Make sure that the bar is transparent when selecting a region.
const gfx::Rect target_region(gfx::BoundingRect(
gfx::Point(0, 0),
GetCaptureModeBarView()->GetBoundsInScreen().top_right() +
gfx::Vector2d(0, -50)));
event_generator->MoveMouseTo(target_region.origin());
event_generator->PressLeftButton();
EXPECT_EQ(0.f, capture_bar_layer->GetTargetOpacity());
event_generator->MoveMouseTo(target_region.bottom_right());
EXPECT_EQ(0.f, capture_bar_layer->GetTargetOpacity());
event_generator->ReleaseLeftButton();
// When there is no overlap of the selected region and the bar, the bar should
// be opaque.
EXPECT_EQ(1.f, capture_bar_layer->GetTargetOpacity());
// Bar becomes transparent when the region is being moved.
event_generator->MoveMouseTo(target_region.origin() + gfx::Vector2d(50, 50));
event_generator->PressLeftButton();
EXPECT_EQ(0.f, capture_bar_layer->GetTargetOpacity());
event_generator->MoveMouseTo(target_region.bottom_center());
EXPECT_EQ(0.f, capture_bar_layer->GetTargetOpacity());
event_generator->ReleaseLeftButton();
// The region overlaps the capture bar, so we set the opacity of the bar to
// the overlapped opacity.
EXPECT_EQ(capture_mode::kCaptureUiOverlapOpacity,
capture_bar_layer->GetTargetOpacity());
// When there is overlap, the toolbar turns opaque on mouseover.
event_generator->MoveMouseTo(
GetCaptureModeBarView()->GetBoundsInScreen().CenterPoint());
EXPECT_EQ(1.f, capture_bar_layer->GetTargetOpacity());
// Capture bar drops back to the overlapped opacity when the mouse is no
// longer hovering.
event_generator->MoveMouseTo(
GetCaptureModeBarView()->GetBoundsInScreen().top_center() +
gfx::Vector2d(0, -50));
EXPECT_EQ(capture_mode::kCaptureUiOverlapOpacity,
capture_bar_layer->GetTargetOpacity());
// Check that the opacity is reset when we select another region.
SelectRegion(target_region);
EXPECT_EQ(1.f, capture_bar_layer->GetTargetOpacity());
}
TEST_P(CaptureModeTest, CaptureBarOpacityOnHoveringOnCaptureLabel) {
UpdateDisplay("800x700");
auto* event_generator = GetEventGenerator();
auto* controller = StartImageRegionCapture();
EXPECT_TRUE(controller->IsActive());
ui::Layer* capture_bar_layer = GetCaptureModeBarWidget()->GetLayer();
// Set the capture region to make it overlap with the capture bar. And then
// move the mouse to the outside of the capture bar, verify it has the
// overlapped opacity.
const gfx::Rect capture_region(200, 500, 130, 130);
SelectRegion(capture_region);
event_generator->MoveMouseTo({10, 10});
EXPECT_EQ(capture_mode::kCaptureUiOverlapOpacity,
capture_bar_layer->GetTargetOpacity());
// Move mouse on top of the capture label, verify the bar becomes fully
// opaque.
event_generator->MoveMouseTo(
GetCaptureModeLabelWidget()->GetWindowBoundsInScreen().CenterPoint());
EXPECT_EQ(1.f, capture_bar_layer->GetTargetOpacity());
}
// Tests that the quick action histogram is recorded properly.
TEST_P(CaptureModeTest, QuickActionHistograms) {
constexpr char kQuickActionHistogramName[] =
"Ash.CaptureModeController.QuickAction";
base::HistogramTester histogram_tester;
auto* controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kImage);
EXPECT_TRUE(controller->IsActive());
{
CaptureNotificationWaiter waiter;
controller->PerformCapture();
waiter.Wait();
}
// Verify clicking delete on screenshot notification.
base::RunLoop loop;
SetUpFileDeletionVerifier(&loop);
const int delete_button = 1;
ClickOnNotification(delete_button);
loop.Run();
EXPECT_FALSE(GetPreviewNotification());
histogram_tester.ExpectBucketCount(kQuickActionHistogramName,
CaptureQuickAction::kDelete, 1);
controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kImage);
{
CaptureNotificationWaiter waiter;
controller->PerformCapture();
waiter.Wait();
}
// Click on the notification body. This should open the default handler.
ClickOnNotification(std::nullopt);
EXPECT_FALSE(GetPreviewNotification());
histogram_tester.ExpectBucketCount(kQuickActionHistogramName,
CaptureQuickAction::kOpenDefault, 1);
controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kImage);
{
CaptureNotificationWaiter waiter;
controller->PerformCapture();
waiter.Wait();
}
const int edit_button = 0;
// Verify clicking edit on screenshot notification.
ClickOnNotification(edit_button);
EXPECT_FALSE(GetPreviewNotification());
histogram_tester.ExpectBucketCount(kQuickActionHistogramName,
CaptureQuickAction::kBacklight, 1);
}
TEST_P(CaptureModeTest, NotificationButtonOfVideoRecording) {
auto* controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kVideo);
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
CaptureModeTestApi test_api;
test_api.FlushRecordingServiceForTesting();
test_api.StopVideoRecording();
CaptureNotificationWaiter().Wait();
EXPECT_TRUE(GetPreviewNotification());
// Verify clicking delete on video recording notification.
base::RunLoop loop;
SetUpFileDeletionVerifier(&loop);
const int delete_button = 0;
ClickOnNotification(delete_button);
loop.Run();
EXPECT_FALSE(GetPreviewNotification());
}
TEST_P(CaptureModeTest, CannotDoMultipleRecordings) {
StartCaptureSession(CaptureModeSource::kFullscreen, CaptureModeType::kVideo);
auto* controller = CaptureModeController::Get();
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
EXPECT_EQ(CaptureModeType::kVideo, controller->type());
// Start a new session with the current type which set to kVideo, the type
// should be switched automatically to kImage, and video toggle button should
// be disabled.
controller->Start(CaptureModeEntryType::kQuickSettings);
EXPECT_TRUE(controller->IsActive());
EXPECT_EQ(CaptureModeType::kImage, controller->type());
EXPECT_TRUE(GetImageToggleButton()->selected());
EXPECT_FALSE(GetVideoToggleButton()->selected());
EXPECT_FALSE(GetVideoToggleButton()->GetEnabled());
// Clicking on the video button should do nothing.
ClickOnView(GetVideoToggleButton(), GetEventGenerator());
EXPECT_TRUE(GetImageToggleButton()->selected());
EXPECT_FALSE(GetVideoToggleButton()->selected());
EXPECT_EQ(CaptureModeType::kImage, controller->type());
// Things should go back to normal when there's no recording going on and the
// video file has been fully saved.
controller->Stop();
controller->EndVideoRecording(EndRecordingReason::kStopRecordingButton);
EXPECT_FALSE(controller->can_start_new_recording());
WaitForCaptureFileToBeSaved();
EXPECT_TRUE(controller->can_start_new_recording());
StartCaptureSession(CaptureModeSource::kFullscreen, CaptureModeType::kVideo);
EXPECT_EQ(CaptureModeType::kVideo, controller->type());
EXPECT_FALSE(GetImageToggleButton()->selected());
EXPECT_TRUE(GetVideoToggleButton()->selected());
EXPECT_TRUE(GetVideoToggleButton()->GetEnabled());
}
// Tests the basic settings menu functionality.
TEST_P(CaptureModeTest, SettingsMenuVisibilityBasic) {
auto* event_generator = GetEventGenerator();
auto* controller = StartImageRegionCapture();
EXPECT_TRUE(controller->IsActive());
// Session starts with settings menu not initialized.
EXPECT_FALSE(GetCaptureModeSettingsWidget());
// Test clicking the settings button toggles the button as well as
// opens/closes the settings menu.
ClickOnView(GetSettingsButton(), event_generator);
EXPECT_TRUE(GetCaptureModeSettingsWidget());
EXPECT_TRUE(GetSettingsButton()->toggled());
ClickOnView(GetSettingsButton(), event_generator);
EXPECT_FALSE(GetCaptureModeSettingsWidget());
EXPECT_FALSE(GetSettingsButton()->toggled());
}
// Tests how interacting with the rest of the screen (i.e. clicking outside of
// the bar/menu, on other buttons) affects whether the settings menu should
// close or not.
TEST_P(CaptureModeTest, SettingsMenuVisibilityClicking) {
UpdateDisplay("800x700");
auto* event_generator = GetEventGenerator();
auto* controller = StartImageRegionCapture();
EXPECT_TRUE(controller->IsActive());
// Test clicking on the option of settings menu doesn't close the
// settings menu.
ClickOnView(GetSettingsButton(), event_generator);
ClickOnView(GetCaptureModeSettingsView(), event_generator);
EXPECT_TRUE(GetCaptureModeSettingsWidget());
EXPECT_TRUE(GetSettingsButton()->toggled());
CaptureModeSettingsTestApi test_api;
ClickOnView(test_api.GetAudioOffOption(), event_generator);
EXPECT_TRUE(GetCaptureModeSettingsWidget());
EXPECT_TRUE(GetSettingsButton()->toggled());
// Test clicking on the capture bar closes the settings menu.
event_generator->MoveMouseTo(
GetCaptureModeBarView()->GetBoundsInScreen().top_center() +
gfx::Vector2d(0, 2));
event_generator->ClickLeftButton();
EXPECT_FALSE(GetCaptureModeSettingsWidget());
EXPECT_FALSE(GetSettingsButton()->toggled());
// Test clicking on a different source closes the settings menu.
ClickOnView(GetSettingsButton(), event_generator);
ClickOnView(GetFullscreenToggleButton(), event_generator);
EXPECT_FALSE(GetCaptureModeSettingsWidget());
// Test clicking on a different type closes the settings menu.
ClickOnView(GetSettingsButton(), event_generator);
ClickOnView(GetVideoToggleButton(), event_generator);
EXPECT_FALSE(GetCaptureModeSettingsWidget());
// Exit the capture session with the settings menu open, and test to make sure
// the new session starts with the settings menu hidden.
ClickOnView(GetSettingsButton(), event_generator);
SendKey(ui::VKEY_ESCAPE, event_generator);
StartCaptureSession(CaptureModeSource::kFullscreen, CaptureModeType::kImage);
EXPECT_FALSE(GetCaptureModeSettingsWidget());
// Take a screenshot with the settings menu open, and test to make sure the
// new session starts with the settings menu hidden.
ClickOnView(GetSettingsButton(), event_generator);
// Take screenshot.
SendKey(ui::VKEY_RETURN, event_generator);
StartImageRegionCapture();
EXPECT_FALSE(GetCaptureModeSettingsWidget());
}
// Tests capture bar and settings menu visibility / opacity when capture region
// is being or after drawn.
TEST_P(CaptureModeTest, CaptureBarAndSettingsMenuVisibilityDrawingRegion) {
auto [sunfish_enabled, scanner_enabled] = GetParam();
if (sunfish_enabled || scanner_enabled) {
// This test crashes when either Sunfish or Scanner is enabled.
// TODO: b/381965299 - Fix these test failures.
GTEST_SKIP();
}
UpdateDisplay("800x700");
auto* event_generator = GetEventGenerator();
auto* controller = StartImageRegionCapture();
auto* capture_bar_widget = GetCaptureModeBarWidget();
ui::Layer* capture_bar_layer = capture_bar_widget->GetLayer();
EXPECT_TRUE(controller->IsActive());
auto* session =
static_cast<CaptureModeSession*>(controller->capture_mode_session());
ASSERT_EQ(session->session_type(), SessionType::kReal);
// Test the settings menu and capture bar are hidden when the user clicks to
// start selecting a region.
ClickOnView(GetSettingsButton(), event_generator);
EXPECT_TRUE(GetCaptureModeSettingsWidget());
const gfx::Rect target_region(gfx::BoundingRect(
gfx::Point(0, 0),
capture_bar_widget->GetWindowBoundsInScreen().top_right() +
gfx::Vector2d(0, -50)));
// Moving the cursor outside the bounds of the settings menu should update the
// cursor to `kPointer`, since the only possible operation here when clicking
// is to dismiss the settings menu rather than take a screenshot or update the
// region.
event_generator->MoveMouseTo(target_region.origin());
auto* cursor_manager = Shell::Get()->cursor_manager();
EXPECT_EQ(CursorType::kPointer, cursor_manager->GetCursor().type());
// Pressing outside the bounds of the settings should dismiss it immediately,
// update the cursor to `kCell` (to signal that it's now possible to select a
// region), but region selection doesn't start until the next click event.
event_generator->PressLeftButton();
EXPECT_FALSE(GetCaptureModeSettingsWidget());
EXPECT_EQ(CursorType::kCell, cursor_manager->GetCursor().type());
EXPECT_FALSE(session->is_selecting_region());
event_generator->ReleaseLeftButton();
EXPECT_FALSE(session->is_selecting_region());
event_generator->PressLeftButton();
EXPECT_TRUE(session->is_selecting_region());
event_generator->MoveMouseTo(target_region.bottom_right());
EXPECT_EQ(0.f, capture_bar_layer->GetTargetOpacity());
event_generator->ReleaseLeftButton();
EXPECT_FALSE(GetCaptureModeSettingsWidget());
// Test that the settings menu will dismiss immediately when clicking
// somewhere in the middle of the capture region.
ClickOnView(GetSettingsButton(), event_generator);
event_generator->MoveMouseTo(target_region.origin() + gfx::Vector2d(50, 50));
event_generator->PressLeftButton();
EXPECT_FALSE(GetCaptureModeSettingsWidget());
event_generator->ReleaseLeftButton();
event_generator->PressLeftButton();
EXPECT_EQ(CursorType::kMove, cursor_manager->GetCursor().type());
EXPECT_FALSE(session->is_selecting_region());
EXPECT_TRUE(session->is_drag_in_progress());
// This creates a region that overlaps with the capture bar. The capture bar
// should be fully transparent while dragging the region is in progress.
event_generator->MoveMouseTo(target_region.bottom_center());
EXPECT_EQ(0.f, capture_bar_layer->GetTargetOpacity());
event_generator->ReleaseLeftButton();
// With an overlapping region (as dragged to above), the capture bar opacity
// is changed based on hover. If the settings menu is open/visible, the
// capture bar will always be visible no matter if the mouse is hovered on it
// or not.
event_generator->MoveMouseTo(target_region.origin());
EXPECT_EQ(capture_mode::kCaptureUiOverlapOpacity,
capture_bar_layer->GetTargetOpacity());
// Move mouse on top of the capture bar, verify that capture bar becomes
// visible.
event_generator->MoveMouseTo(
capture_bar_widget->GetWindowBoundsInScreen().CenterPoint());
EXPECT_EQ(1.f, capture_bar_layer->GetTargetOpacity());
ClickOnView(GetSettingsButton(), event_generator);
EXPECT_TRUE(GetCaptureModeSettingsWidget());
EXPECT_EQ(1.f, capture_bar_layer->GetTargetOpacity());
// Move mouse onto the settings menu, confirm the capture bar is still
// visible.
event_generator->MoveMouseTo(
GetCaptureModeSettingsView()->GetBoundsInScreen().CenterPoint());
EXPECT_EQ(1.f, capture_bar_layer->GetTargetOpacity());
// Move mouse to the outside of the capture bar and settings, verify that
// settings menu are still open and both capture bar and settings have full
// opaque.
event_generator->MoveMouseTo(target_region.origin());
auto* settings_menu = GetCaptureModeSettingsView();
EXPECT_TRUE(settings_menu);
EXPECT_EQ(1.f, capture_bar_layer->GetTargetOpacity());
EXPECT_EQ(1.f, settings_menu->layer()->GetTargetOpacity());
// Close settings menu, and move mouse to the outside of the capture bar,
// verify capture bar has the overlapped opacity.
ClickOnView(GetSettingsButton(), event_generator);
event_generator->MoveMouseTo(target_region.origin());
EXPECT_EQ(capture_mode::kCaptureUiOverlapOpacity,
capture_bar_layer->GetTargetOpacity());
}
TEST_P(CaptureModeTest, CaptureFolderSetting) {
auto* controller = CaptureModeController::Get();
auto* test_delegate = controller->delegate_for_testing();
const auto default_downloads_folder =
test_delegate->GetUserDefaultDownloadsFolder();
auto capture_folder = controller->GetCurrentCaptureFolder();
EXPECT_EQ(capture_folder.path, default_downloads_folder);
EXPECT_TRUE(capture_folder.is_default_downloads_folder);
const base::FilePath custom_folder(FILE_PATH_LITERAL("/home/tests"));
controller->SetCustomCaptureFolder(custom_folder);
capture_folder = controller->GetCurrentCaptureFolder();
EXPECT_EQ(capture_folder.path, custom_folder);
EXPECT_FALSE(capture_folder.is_default_downloads_folder);
}
TEST_P(CaptureModeTest, CaptureFolderSetToDefaultDownloads) {
auto* controller = CaptureModeController::Get();
auto* test_delegate = controller->delegate_for_testing();
const base::FilePath custom_folder(FILE_PATH_LITERAL("/home/tests"));
controller->SetCustomCaptureFolder(custom_folder);
auto capture_folder = controller->GetCurrentCaptureFolder();
EXPECT_FALSE(capture_folder.is_default_downloads_folder);
// If the user selects the default downloads folder manually, we should be
// able to detect that.
const auto default_downloads_folder =
test_delegate->GetUserDefaultDownloadsFolder();
controller->SetCustomCaptureFolder(default_downloads_folder);
capture_folder = controller->GetCurrentCaptureFolder();
EXPECT_EQ(capture_folder.path, default_downloads_folder);
EXPECT_TRUE(capture_folder.is_default_downloads_folder);
}
TEST_P(CaptureModeTest, UsesDefaultFolderWithCustomFolderSet) {
auto* controller = CaptureModeController::Get();
auto* test_delegate = controller->delegate_for_testing();
const base::FilePath custom_folder(FILE_PATH_LITERAL("/home/tests"));
controller->SetCustomCaptureFolder(custom_folder);
auto capture_folder = controller->GetCurrentCaptureFolder();
EXPECT_FALSE(capture_folder.is_default_downloads_folder);
// If the user selects to force use the default downloads folder even while
// a custom folder is set, we should respect that, but we shouldn't clear the
// custom folder.
controller->SetUsesDefaultCaptureFolder(true);
const auto default_downloads_folder =
test_delegate->GetUserDefaultDownloadsFolder();
capture_folder = controller->GetCurrentCaptureFolder();
EXPECT_EQ(capture_folder.path, default_downloads_folder);
EXPECT_TRUE(capture_folder.is_default_downloads_folder);
// Setting another custom folder value, would reset the
// "UsesDefaultCaptureFolder" value, and the new custom folder will be used.
const base::FilePath custom_folder2(FILE_PATH_LITERAL("/home/tests2"));
controller->SetCustomCaptureFolder(custom_folder2);
capture_folder = controller->GetCurrentCaptureFolder();
EXPECT_EQ(capture_folder.path, custom_folder2);
EXPECT_FALSE(capture_folder.is_default_downloads_folder);
}
TEST_P(CaptureModeTest, CaptureFolderSetToEmptyPath) {
auto* controller = CaptureModeController::Get();
auto* test_delegate = controller->delegate_for_testing();
const base::FilePath custom_folder(FILE_PATH_LITERAL("/home/tests"));
controller->SetCustomCaptureFolder(custom_folder);
auto capture_folder = controller->GetCurrentCaptureFolder();
EXPECT_FALSE(capture_folder.is_default_downloads_folder);
// If we set the custom path to an empty folder to clear, we should revert
// back to the default downloads folder.
controller->SetCustomCaptureFolder(base::FilePath());
const auto default_downloads_folder =
test_delegate->GetUserDefaultDownloadsFolder();
capture_folder = controller->GetCurrentCaptureFolder();
EXPECT_EQ(capture_folder.path, default_downloads_folder);
EXPECT_TRUE(capture_folder.is_default_downloads_folder);
}
TEST_P(CaptureModeTest, SimulateUserCancelingDlpWarningDialog) {
auto* controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kVideo);
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
// Simulate the user canceling the DLP warning dialog at the end of the
// recording which is shown to the user to alert about restricted content
// showing up on the screen during the recording. In this case, the user
// requests the deletion of the file.
auto* test_delegate =
static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());
test_delegate->set_should_save_after_dlp_check(false);
base::RunLoop loop;
SetUpFileDeletionVerifier(&loop);
controller->EndVideoRecording(EndRecordingReason::kStopRecordingButton);
loop.Run();
// No notification should show in this case, nor any thing on Tote.
EXPECT_FALSE(GetPreviewNotification());
ash::HoldingSpaceTestApi holding_space_api;
EXPECT_TRUE(holding_space_api.GetScreenCaptureViews().empty());
EXPECT_TRUE(controller->can_start_new_recording());
}
// Tests that `CaptureScreenshotOfGivenWindow` can take window screenshot
// successfully and that the image size matches the window size.
TEST_P(CaptureModeTest, InstantScreenshotForkWindow) {
const gfx::Rect window_bounds(10, 20, 700, 500);
std::unique_ptr<aura::Window> window(CreateTestWindow(window_bounds));
CaptureModeController::Get()->CaptureScreenshotOfGivenWindow(window.get());
const auto file_path = WaitForCaptureFileToBeSaved();
gfx::Image image = ReadAndDecodeImageFile(file_path);
EXPECT_EQ(image.Size(), window_bounds.size());
}
// Tests the capture mode behavior in the default capture mode session and
// during video recording.
TEST_P(CaptureModeTest, CaptureModeDefaultBehavior) {
CaptureModeController* controller = StartCaptureSession(
CaptureModeSource::kFullscreen, CaptureModeType::kVideo);
ASSERT_TRUE(controller->IsActive());
auto* session =
static_cast<CaptureModeSession*>(controller->capture_mode_session());
ASSERT_EQ(session->session_type(), SessionType::kReal);
const CaptureModeBehavior* active_behavior = session->active_behavior();
ASSERT_TRUE(active_behavior);
auto expected_behavior = [&]() {
EXPECT_TRUE(active_behavior->ShouldImageCaptureTypeBeAllowed());
EXPECT_TRUE(active_behavior->ShouldVideoCaptureTypeBeAllowed());
EXPECT_TRUE(active_behavior->ShouldFulscreenCaptureSourceBeAllowed());
EXPECT_TRUE(active_behavior->ShouldRegionCaptureSourceBeAllowed());
EXPECT_TRUE(active_behavior->ShouldWindowCaptureSourceBeAllowed());
EXPECT_TRUE(
active_behavior->SupportsAudioRecordingMode(AudioRecordingMode::kOff));
EXPECT_TRUE(active_behavior->SupportsAudioRecordingMode(
AudioRecordingMode::kMicrophone));
EXPECT_TRUE(active_behavior->ShouldCameraSelectionSettingsBeIncluded());
EXPECT_TRUE(active_behavior->ShouldDemoToolsSettingsBeIncluded());
EXPECT_TRUE(active_behavior->ShouldSaveToSettingsBeIncluded());
EXPECT_TRUE(active_behavior->ShouldGifBeSupported());
EXPECT_TRUE(active_behavior->ShouldShowPreviewNotification());
EXPECT_FALSE(active_behavior->ShouldSkipVideoRecordingCountDown());
EXPECT_FALSE(active_behavior->ShouldCreateAnnotationsOverlayController());
EXPECT_TRUE(active_behavior->ShouldShowUserNudge());
EXPECT_FALSE(active_behavior->ShouldAutoSelectFirstCamera());
};
expected_behavior();
views::Widget* bar_widget = GetCaptureModeBarWidget();
ASSERT_TRUE(bar_widget);
EXPECT_TRUE(GetImageToggleButton());
EXPECT_TRUE(GetVideoToggleButton());
EXPECT_TRUE(GetFullscreenToggleButton());
EXPECT_TRUE(GetRegionToggleButton());
EXPECT_TRUE(GetWindowToggleButton());
EXPECT_FALSE(GetStartRecordingButton());
EXPECT_TRUE(GetSettingsButton());
EXPECT_TRUE(GetCloseButton());
StartVideoRecordingImmediately();
expected_behavior();
}
// Tests that the capture mode session can be started with the keyboard shortcut
// 'Ctrl + Shift + Overview' with `kImage` as the default type and `kRegion` as
// the default source. And the screen recording can be ended with the keyboard
// shortcut 'Search + Shift + X'.
TEST_P(CaptureModeTest, KeyboardShortcutTest) {
base::HistogramTester histogram_tester;
histogram_tester.ExpectBucketCount(
kEndRecordingReasonInClamshellHistogramName,
EndRecordingReason::kKeyboardShortcut, 0);
auto* event_generator = GetEventGenerator();
event_generator->PressAndReleaseKey(ui::VKEY_MEDIA_LAUNCH_APP1,
ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN);
auto* controller = CaptureModeController::Get();
EXPECT_TRUE(controller->IsActive());
EXPECT_EQ(controller->type(), CaptureModeType::kImage);
EXPECT_EQ(controller->source(), CaptureModeSource::kRegion);
controller->SetType(CaptureModeType::kVideo);
controller->SetSource(CaptureModeSource::kFullscreen);
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
event_generator->PressAndReleaseKey(ui::VKEY_X,
ui::EF_SHIFT_DOWN | ui::EF_COMMAND_DOWN);
EXPECT_FALSE(controller->is_recording_in_progress());
histogram_tester.ExpectBucketCount(
kEndRecordingReasonInClamshellHistogramName,
EndRecordingReason::kKeyboardShortcut, 1);
}
TEST_P(CaptureModeTest, StopRecordingButtonTrayAccessibleName) {
ash::CaptureModeTestApi test_api;
test_api.StartForFullscreen(/*for_video=*/true);
test_api.PerformCapture();
test_api.FlushRecordingServiceForTesting();
auto* stop_recording_button = Shell::GetPrimaryRootWindowController()
->GetStatusAreaWidget()
->stop_recording_button_tray();
EXPECT_TRUE(stop_recording_button->visible_preferred());
ui::AXNodeData tray_data;
stop_recording_button->GetViewAccessibility().GetAccessibleNodeData(
&tray_data);
EXPECT_EQ(tray_data.GetString16Attribute(ax::mojom::StringAttribute::kName),
l10n_util::GetStringUTF16(
IDS_ASH_STATUS_AREA_STOP_RECORDING_BUTTON_ACCESSIBLE_NAME));
}
namespace {
// -----------------------------------------------------------------------------
// TestVideoCaptureOverlay:
// Defines a fake video capture overlay to be used in testing the behavior of
// the cursor overlay. The VideoRecordingWatcher will control this overlay via
// mojo.
using Overlay = viz::mojom::FrameSinkVideoCaptureOverlay;
class TestVideoCaptureOverlay : public Overlay {
public:
explicit TestVideoCaptureOverlay(mojo::PendingReceiver<Overlay> receiver)
: receiver_(this, std::move(receiver)) {}
~TestVideoCaptureOverlay() override = default;
const gfx::RectF& last_bounds() const { return last_bounds_; }
bool IsHidden() const { return last_bounds_ == gfx::RectF(); }
// viz::mojom::FrameSinkVideoCaptureOverlay:
void SetImageAndBounds(const SkBitmap& image,
const gfx::RectF& bounds) override {
last_bounds_ = bounds;
}
void SetBounds(const gfx::RectF& bounds) override { last_bounds_ = bounds; }
void OnCapturedMouseEvent(const gfx::Point& coordinates) override {}
private:
mojo::Receiver<viz::mojom::FrameSinkVideoCaptureOverlay> receiver_;
gfx::RectF last_bounds_;
};
// -----------------------------------------------------------------------------
// CaptureModeCursorOverlayTest:
// Defines a test fixure to test the behavior of the cursor overlay.
class CaptureModeCursorOverlayTest : public CaptureModeTest {
public:
CaptureModeCursorOverlayTest() = default;
~CaptureModeCursorOverlayTest() override = default;
aura::Window* window() const { return window_.get(); }
TestVideoCaptureOverlay* fake_overlay() const { return fake_overlay_.get(); }
// CaptureModeTest:
void SetUp() override {
CaptureModeTest::SetUp();
window_ = CreateTestWindow(gfx::Rect(200, 200));
}
void TearDown() override {
window_.reset();
CaptureModeTest::TearDown();
}
CaptureModeController* StartRecordingAndSetupFakeOverlay(
CaptureModeSource source) {
auto* controller = StartCaptureSession(source, CaptureModeType::kVideo);
auto* event_generator = GetEventGenerator();
if (source == CaptureModeSource::kWindow)
event_generator->MoveMouseToCenterOf(window_.get());
StartVideoRecordingImmediately();
EXPECT_TRUE(controller->is_recording_in_progress());
auto* recording_watcher = controller->video_recording_watcher_for_testing();
mojo::PendingRemote<Overlay> overlay_pending_remote;
fake_overlay_ = std::make_unique<TestVideoCaptureOverlay>(
overlay_pending_remote.InitWithNewPipeAndPassReceiver());
recording_watcher->BindCursorOverlayForTesting(
std::move(overlay_pending_remote));
// The overlay should be initially hidden until a mourse event is received.
FlushOverlay();
EXPECT_TRUE(fake_overlay()->IsHidden());
// Generating some mouse events may or may not show the overlay, depending
// on the conditions of the test. Each test will verify its expectation
// after this returns.
event_generator->MoveMouseBy(10, 10);
FlushOverlay();
return controller;
}
void FlushOverlay() {
auto* controller = CaptureModeController::Get();
DCHECK(controller->is_recording_in_progress());
controller->video_recording_watcher_for_testing()
->FlushCursorOverlayForTesting();
}
// The docked magnifier is one of the features that force the software-
// composited cursor to be used when enabled. We use it to test the behavior
// of the cursor overlay in that case.
void SetDockedMagnifierEnabled(bool enabled) {
Shell::Get()->docked_magnifier_controller()->SetEnabled(enabled);
}
// Checks that capturing a screenshot hides the cursor. After the capture is
// complete, checks that the cursor returns to the previous state, i.e.
// hidden for tablet mode but visible for clamshell mode.
void CaptureScreenshotAndCheckCursorVisibility(
CaptureModeController* controller) {
EXPECT_EQ(controller->type(), CaptureModeType::kImage);
auto* cursor_manager = Shell::Get()->cursor_manager();
bool in_tablet_mode = display::Screen::GetScreen()->InTabletMode();
// The capture mode session locks the cursor for the whole active session
// except in the tablet mode unless the cursor is visible.
EXPECT_EQ(!in_tablet_mode, cursor_manager->IsCursorLocked());
EXPECT_EQ(!in_tablet_mode, cursor_manager->IsCursorVisible());
EXPECT_TRUE(controller->IsActive());
// Make sure the cursor is hidden while capturing the screenshot.
CaptureNotificationWaiter waiter;
controller->PerformCapture();
EXPECT_FALSE(cursor_manager->IsCursorVisible());
EXPECT_FALSE(controller->IsActive());
// The cursor visibility should be restored after the capture is done.
waiter.Wait();
EXPECT_EQ(!in_tablet_mode, cursor_manager->IsCursorVisible());
EXPECT_FALSE(cursor_manager->IsCursorLocked());
}
private:
std::unique_ptr<aura::Window> window_;
std::unique_ptr<TestVideoCaptureOverlay> fake_overlay_;
};
INSTANTIATE_TEST_SUITE_P(
,
CaptureModeCursorOverlayTest,
testing::Combine(testing::Bool(), testing::Bool()),
[](const testing::TestParamInfo<CaptureModeCursorOverlayTest::ParamType>&
info) {
bool sunfish_enabled = std::get<0>(info.param);
bool scanner_enabled = std::get<1>(info.param);
return SunfishScannerTestName(sunfish_enabled, scanner_enabled);
});
} // namespace
TEST_P(CaptureModeCursorOverlayTest, TabletModeHidesCursorOverlay) {
StartRecordingAndSetupFakeOverlay(CaptureModeSource::kFullscreen);
EXPECT_FALSE(fake_overlay()->IsHidden());
// Entering tablet mode should hide the cursor overlay.
SwitchToTabletMode();
FlushOverlay();
EXPECT_TRUE(fake_overlay()->IsHidden());
// Exiting tablet mode should reshow the overlay.
LeaveTabletMode();
FlushOverlay();
EXPECT_FALSE(fake_overlay()->IsHidden());
}
// Tests that the cursor is hidden while taking a screenshot in tablet mode and
// remains hidden afterward.
TEST_P(CaptureModeCursorOverlayTest, TabletModeHidesCursor) {
// Enter tablet mode.
SwitchToTabletMode();
auto* cursor_manager = Shell::Get()->cursor_manager();
CaptureModeController* controller = StartCaptureSession(
CaptureModeSource::kFullscreen, CaptureModeType::kImage);
// Test the hardware cursor.
CaptureScreenshotAndCheckCursorVisibility(controller);
// Test the software cursor enabled by docked magnifier.
SetDockedMagnifierEnabled(true);
EXPECT_TRUE(IsCursorCompositingEnabled());
controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kImage);
CaptureScreenshotAndCheckCursorVisibility(controller);
// Exiting tablet mode.
LeaveTabletMode();
EXPECT_TRUE(cursor_manager->IsCursorVisible());
}
// Tests that a cursor is hidden while taking a fullscreen screenshot
// (crbug.com/1186652).
TEST_P(CaptureModeCursorOverlayTest, CursorInFullscreenScreenshot) {
auto* cursor_manager = Shell::Get()->cursor_manager();
EXPECT_FALSE(cursor_manager->IsCursorLocked());
CaptureModeController* controller = StartCaptureSession(
CaptureModeSource::kFullscreen, CaptureModeType::kImage);
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseTo(gfx::Point(175, 175));
// Test the hardware cursor.
CaptureScreenshotAndCheckCursorVisibility(controller);
// Test the software cursor enabled by docked magnifier.
SetDockedMagnifierEnabled(true);
EXPECT_TRUE(IsCursorCompositingEnabled());
controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kImage);
CaptureScreenshotAndCheckCursorVisibility(controller);
}
// Tests that a cursor is hidden while taking a region screenshot
// (crbug.com/1186652).
TEST_P(CaptureModeCursorOverlayTest, CursorInPartialRegionScreenshot) {
// Use a set display size as we will be choosing points in this test.
UpdateDisplay("800x700");
auto* cursor_manager = Shell::Get()->cursor_manager();
EXPECT_FALSE(cursor_manager->IsCursorLocked());
auto* event_generator = GetEventGenerator();
auto* controller = StartImageRegionCapture();
// Create the initial capture region.
const gfx::Rect target_region(gfx::Rect(50, 50, 200, 200));
SelectRegion(target_region);
event_generator->MoveMouseTo(gfx::Point(175, 175));
// Test the hardware cursor.
CaptureScreenshotAndCheckCursorVisibility(controller);
// Test the software cursor enabled by docked magnifier.
SetDockedMagnifierEnabled(true);
EXPECT_TRUE(IsCursorCompositingEnabled());
controller = StartImageRegionCapture();
CaptureScreenshotAndCheckCursorVisibility(controller);
}
TEST_P(CaptureModeCursorOverlayTest, SoftwareCursorInitiallyEnabled) {
// The software cursor is enabled before recording starts.
SetDockedMagnifierEnabled(true);
EXPECT_TRUE(IsCursorCompositingEnabled());
// Hence the overlay will be hidden initially.
StartRecordingAndSetupFakeOverlay(CaptureModeSource::kFullscreen);
EXPECT_TRUE(fake_overlay()->IsHidden());
}
TEST_P(CaptureModeCursorOverlayTest, SoftwareCursorInFullscreenRecording) {
StartRecordingAndSetupFakeOverlay(CaptureModeSource::kFullscreen);
EXPECT_FALSE(fake_overlay()->IsHidden());
// When the software-composited cursor is enabled, the overlay is hidden to
// avoid having two overlapping cursors in the video.
SetDockedMagnifierEnabled(true);
EXPECT_TRUE(IsCursorCompositingEnabled());
FlushOverlay();
EXPECT_TRUE(fake_overlay()->IsHidden());
SetDockedMagnifierEnabled(false);
EXPECT_FALSE(IsCursorCompositingEnabled());
FlushOverlay();
EXPECT_FALSE(fake_overlay()->IsHidden());
}
TEST_P(CaptureModeCursorOverlayTest, SoftwareCursorInPartialRegionRecording) {
CaptureModeController::Get()->SetUserCaptureRegion(gfx::Rect(20, 20),
/*by_user=*/true);
StartRecordingAndSetupFakeOverlay(CaptureModeSource::kRegion);
EXPECT_FALSE(fake_overlay()->IsHidden());
// The behavior in this case is exactly the same as in fullscreen recording.
SetDockedMagnifierEnabled(true);
EXPECT_TRUE(IsCursorCompositingEnabled());
FlushOverlay();
EXPECT_TRUE(fake_overlay()->IsHidden());
}
TEST_P(CaptureModeCursorOverlayTest, SoftwareCursorInWindowRecording) {
StartRecordingAndSetupFakeOverlay(CaptureModeSource::kWindow);
EXPECT_FALSE(fake_overlay()->IsHidden());
// When recording a window, the software cursor has no effect of the cursor
// overlay, since the cursor widget is not in the recorded window subtree, so
// it cannot be captured by the frame sink capturer. We have to provide cursor
// capturing through the overlay.
SetDockedMagnifierEnabled(true);
EXPECT_TRUE(IsCursorCompositingEnabled());
FlushOverlay();
EXPECT_FALSE(fake_overlay()->IsHidden());
}
TEST_P(CaptureModeCursorOverlayTest, OverlayHidesWhenOutOfBounds) {
StartRecordingAndSetupFakeOverlay(CaptureModeSource::kWindow);
EXPECT_FALSE(fake_overlay()->IsHidden());
const gfx::Point bottom_right =
window()->GetBoundsInRootWindow().bottom_right();
auto* generator = GetEventGenerator();
// Generate a click event to overcome throttling.
generator->MoveMouseTo(bottom_right);
generator->ClickLeftButton();
FlushOverlay();
EXPECT_TRUE(fake_overlay()->IsHidden());
}
namespace {
// A CursorShapeClient that always fails to return cursor data.
class FakeCursorShapeClient : public aura::client::CursorShapeClient {
public:
FakeCursorShapeClient() = default;
FakeCursorShapeClient(const FakeCursorShapeClient&) = delete;
FakeCursorShapeClient& operator=(const FakeCursorShapeClient&) = delete;
~FakeCursorShapeClient() override = default;
// aura::client::CursorShapeClient:
std::optional<ui::CursorData> GetCursorData(
const ui::Cursor& cursor) const override {
return std::nullopt;
}
};
} // namespace
TEST_P(CaptureModeCursorOverlayTest, OverlayWhenCursorIsHiddenOrFails) {
StartRecordingAndSetupFakeOverlay(CaptureModeSource::kWindow);
EXPECT_FALSE(fake_overlay()->IsHidden());
// Move cursor, the overlay should update.
gfx::RectF last_bounds = fake_overlay()->last_bounds();
auto* generator = GetEventGenerator();
// Generate a click event to overcome throttling.
generator->MoveMouseBy(10, 10);
generator->ClickLeftButton();
FlushOverlay();
EXPECT_FALSE(fake_overlay()->IsHidden());
EXPECT_NE(fake_overlay()->last_bounds(), last_bounds);
// Hide cursor, the overlay should be empty and hidden.
auto* cursor_manager = Shell::Get()->cursor_manager();
cursor_manager->SetCursor(CursorType::kNone);
// Lock the cursor to prevent mouse events from changing it back.
cursor_manager->LockCursor();
generator->MoveMouseBy(10, 10);
generator->ClickLeftButton();
FlushOverlay();
EXPECT_TRUE(fake_overlay()->IsHidden());
EXPECT_EQ(fake_overlay()->last_bounds(), gfx::RectF());
// While the cursor is hidden, the overlay shouldn't change.
generator->MoveMouseBy(10, 10);
generator->ClickLeftButton();
FlushOverlay();
EXPECT_TRUE(fake_overlay()->IsHidden());
EXPECT_EQ(fake_overlay()->last_bounds(), gfx::RectF());
// Unhide cursor, the overlay should update.
cursor_manager->UnlockCursor();
generator->ClickLeftButton();
FlushOverlay();
EXPECT_FALSE(fake_overlay()->IsHidden());
EXPECT_NE(fake_overlay()->last_bounds(), gfx::RectF());
// Set a fake cursor shape client so that retrieving the cursor data fails.
// The overlay shouldn't change.
FakeCursorShapeClient cursor_shape_client;
aura::client::SetCursorShapeClient(&cursor_shape_client);
last_bounds = fake_overlay()->last_bounds();
generator->MoveMouseBy(10, 10);
generator->ClickLeftButton();
FlushOverlay();
EXPECT_FALSE(fake_overlay()->IsHidden());
EXPECT_EQ(fake_overlay()->last_bounds(), last_bounds);
}
// Verifies that the cursor overlay bounds calculation takes into account the
// cursor image scale factor. https://crbug.com/1222494.
TEST_P(CaptureModeCursorOverlayTest, OverlayBoundsAccountForCursorScaleFactor) {
UpdateDisplay("500x400");
StartRecordingAndSetupFakeOverlay(CaptureModeSource::kFullscreen);
EXPECT_FALSE(fake_overlay()->IsHidden());
auto* cursor_manager = Shell::Get()->cursor_manager();
auto set_cursor = [cursor_manager](const gfx::Size& cursor_image_size,
float cursor_image_scale_factor) {
SkBitmap cursor_image;
cursor_image.allocN32Pixels(cursor_image_size.width(),
cursor_image_size.height());
ui::Cursor cursor = ui::Cursor::NewCustom(
std::move(cursor_image), gfx::Point(), cursor_image_scale_factor);
cursor.SetPlatformCursor(
ui::CursorFactory::GetInstance()->CreateImageCursor(
cursor.type(), cursor.custom_bitmap(), cursor.custom_hotspot(),
cursor.image_scale_factor()));
cursor_manager->SetCursor(std::move(cursor));
};
struct {
gfx::Size cursor_size;
float cursor_image_scale_factor;
} kTestCases[] = {
{
gfx::Size(50, 50),
/*cursor_image_scale_factor=*/2.f,
},
{
gfx::Size(25, 25),
/*cursor_image_scale_factor=*/1.f,
},
};
// Both of the above test cases should yield the same cursor overlay relative
// bounds when the cursor is at the center of the screen.
// Origin is 0.5f (center)
// Size is 25 (cursor image dip size) / {500,400} = {0.05f, 0.0625f}
const gfx::RectF expected_overlay_bounds{0.5f, 0.5f, 0.05f, 0.0625f};
const gfx::Point screen_center =
window()->GetRootWindow()->bounds().CenterPoint();
auto* generator = GetEventGenerator();
for (const auto& test_case : kTestCases) {
set_cursor(test_case.cursor_size, test_case.cursor_image_scale_factor);
// Lock the cursor to prevent mouse events from changing it back to a
// default kPointer cursor type.
cursor_manager->LockCursor();
// Generate a click event to overcome throttling.
generator->MoveMouseTo(screen_center);
generator->ClickLeftButton();
FlushOverlay();
EXPECT_FALSE(fake_overlay()->IsHidden());
EXPECT_EQ(expected_overlay_bounds, fake_overlay()->last_bounds());
// Unlock the cursor back.
cursor_manager->UnlockCursor();
}
}
// -----------------------------------------------------------------------------
// TODO(afakhry): Add more cursor overlay tests.
// Test fixture to verify capture mode + projector integration.
namespace {
constexpr char kProjectorCreationFlowHistogramName[] =
"Ash.Projector.CreationFlow.ClamshellMode";
} // namespace
class ProjectorCaptureModeIntegrationTestsBase : public CaptureModeTestBase {
public:
ProjectorCaptureModeIntegrationTestsBase() = default;
~ProjectorCaptureModeIntegrationTestsBase() override = default;
static constexpr gfx::Rect kUserRegion{20, 50, 60, 70};
MockProjectorClient* projector_client() {
return projector_helper_.projector_client();
}
aura::Window* window() const { return window_.get(); }
// CaptureModeTestBase:
void SetUp() override {
CaptureModeTestBase::SetUp();
projector_helper_.SetUp();
window_ = CreateTestWindow(gfx::Rect(20, 30, 200, 200));
CaptureModeController::Get()->SetUserCaptureRegion(kUserRegion,
/*by_user=*/true);
}
void TearDown() override {
window_.reset();
CaptureModeTestBase::TearDown();
}
void StartProjectorModeSession() {
projector_helper_.StartProjectorModeSession();
}
void StartRecordingForProjectorFromSource(CaptureModeSource source) {
StartProjectorModeSession();
auto* controller = CaptureModeController::Get();
controller->SetSource(source);
switch (source) {
case CaptureModeSource::kFullscreen:
case CaptureModeSource::kRegion:
break;
case CaptureModeSource::kWindow:
auto* generator = GetEventGenerator();
generator->MoveMouseTo(window_->GetBoundsInScreen().CenterPoint());
break;
}
CaptureModeTestApi().PerformCapture();
WaitForRecordingToStart();
EXPECT_TRUE(controller->is_recording_in_progress());
}
protected:
ProjectorCaptureModeIntegrationHelper projector_helper_;
std::unique_ptr<aura::Window> window_;
base::HistogramTester histogram_tester_;
};
class ProjectorCaptureModeIntegrationTests
: public ProjectorCaptureModeIntegrationTestsBase,
public ::testing::WithParamInterface<std::tuple<bool, bool>> {
public:
// ProjectorCaptureModeIntegrationTestsBase:
void SetUp() override {
auto [sunfish_enabled, scanner_enabled] = GetParam();
InitFeatures(sunfish_enabled, scanner_enabled);
ProjectorCaptureModeIntegrationTestsBase::SetUp();
}
};
INSTANTIATE_TEST_SUITE_P(
,
ProjectorCaptureModeIntegrationTests,
testing::Combine(testing::Bool(), testing::Bool()),
[](const testing::TestParamInfo<
ProjectorCaptureModeIntegrationTests::ParamType>& info) {
bool sunfish_enabled = std::get<0>(info.param);
bool scanner_enabled = std::get<1>(info.param);
return SunfishScannerTestName(sunfish_enabled, scanner_enabled);
});
class ProjectorCaptureModeIntegrationTestsWithSource
: public ProjectorCaptureModeIntegrationTestsBase,
public ::testing::WithParamInterface<
std::tuple<CaptureModeSource, bool, bool>> {
public:
// ProjectorCaptureModeIntegrationTestsBase:
void SetUp() override {
auto [unused_source, sunfish_enabled, scanner_enabled] = GetParam();
InitFeatures(sunfish_enabled, scanner_enabled);
ProjectorCaptureModeIntegrationTestsBase::SetUp();
}
};
// static
constexpr gfx::Rect ProjectorCaptureModeIntegrationTestsBase::kUserRegion;
TEST_P(ProjectorCaptureModeIntegrationTests, EntryPoint) {
// With the most recent source type set to kImage, starting capture mode for
// the projector workflow will still force it to kVideo.
auto* controller = CaptureModeController::Get();
controller->SetType(CaptureModeType::kImage);
// Also, audio recording is initially disabled. However, the projector flow
// forces it enabled.
EXPECT_EQ(AudioRecordingMode::kOff,
controller->GetEffectiveAudioRecordingMode());
StartProjectorModeSession();
EXPECT_TRUE(controller->IsActive());
auto* session = controller->capture_mode_session();
ASSERT_TRUE(session);
const CaptureModeBehavior* behavior = session->active_behavior();
ASSERT_TRUE(behavior);
EXPECT_TRUE(
behavior->SupportsAudioRecordingMode(AudioRecordingMode::kMicrophone));
EXPECT_EQ(AudioRecordingMode::kMicrophone,
controller->GetEffectiveAudioRecordingMode());
constexpr char kEntryPointHistogram[] =
"Ash.CaptureModeController.EntryPoint.ClamshellMode";
histogram_tester_.ExpectBucketCount(kEntryPointHistogram,
CaptureModeEntryType::kProjector, 1);
}
// Tests that a fullscreen screenshot can be taken via the keyboard shortcut
// while a Projector-initiated session is active without ending the session.
TEST_P(ProjectorCaptureModeIntegrationTests, FullscreenScreenshotKeyCombo) {
StartProjectorModeSession();
PressAndReleaseKey(ui::VKEY_MEDIA_LAUNCH_APP1, ui::EF_CONTROL_DOWN);
WaitForCaptureFileToBeSaved();
auto* controller = CaptureModeController::Get();
ASSERT_TRUE(controller->IsActive());
CaptureModeBehavior* active_behavior =
controller->capture_mode_session()->active_behavior();
ASSERT_TRUE(active_behavior);
EXPECT_EQ(active_behavior->behavior_type(), BehaviorType::kProjector);
}
// Tests that the settings view is simplified in projector mode.
TEST_P(ProjectorCaptureModeIntegrationTests, CaptureModeSettings) {
auto* controller = CaptureModeController::Get();
StartProjectorModeSession();
auto* event_generator = GetEventGenerator();
ClickOnView(GetSettingsButton(), event_generator);
CaptureModeSettingsTestApi test_api;
// The "Save-to" menu group should never be added.
CaptureModeMenuGroup* save_to_menu_group = test_api.GetSaveToMenuGroup();
EXPECT_FALSE(save_to_menu_group);
// The audio-off option should never be added.
EXPECT_FALSE(test_api.GetAudioOffOption());
CaptureModeMenuGroup* audio_input_menu_group =
test_api.GetAudioInputMenuGroup();
EXPECT_TRUE(audio_input_menu_group->IsOptionChecked(kAudioMicrophone));
EXPECT_EQ(AudioRecordingMode::kMicrophone,
controller->GetEffectiveAudioRecordingMode());
}
TEST_P(ProjectorCaptureModeIntegrationTests, AudioCaptureDisabledByPolicy) {
auto* controller = CaptureModeController::Get();
auto* delegate =
static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());
delegate->set_is_audio_capture_disabled_by_policy(true);
EXPECT_EQ(AudioRecordingMode::kOff,
controller->GetEffectiveAudioRecordingMode());
// A projector session is not allowed to start when audio recording is
// disabled by policy.
EXPECT_FALSE(projector_helper_.CanStartProjectorSession());
}
TEST_P(ProjectorCaptureModeIntegrationTests,
AudioCaptureDisabledByPolicyAfterSessionStarts) {
auto* controller = CaptureModeController::Get();
auto* delegate =
static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());
EXPECT_EQ(AudioRecordingMode::kOff,
controller->GetEffectiveAudioRecordingMode());
// At this point, a Projector session is allowed to begin.
EXPECT_TRUE(projector_helper_.CanStartProjectorSession());
StartProjectorModeSession();
// Flip the audio policy now before recording begins. Attempt to start
// recording, but expect that the capture mode session will end *without*
// starting a new recording.
delegate->set_is_audio_capture_disabled_by_policy(true);
PressAndReleaseKey(ui::VKEY_RETURN);
WaitForSessionToEnd();
EXPECT_FALSE(controller->is_recording_in_progress());
// The Projector session preconditions should now be up-to-date.
EXPECT_FALSE(projector_helper_.CanStartProjectorSession());
}
// Tests the keyboard navigation for projector mode. The `image_toggle_button_`
// in `CaptureModeTypeView` and the `Off` audio input option in
// `CaptureModeSettingsView` are not available in projector mode.
TEST_P(ProjectorCaptureModeIntegrationTests, KeyboardNavigationBasic) {
auto* controller = CaptureModeController::Get();
// Use `kFullscreen` here to minimize the number of tabs to reach the setting
// button.
controller->SetSource(CaptureModeSource::kFullscreen);
StartProjectorModeSession();
using FocusGroup = CaptureModeSessionFocusCycler::FocusGroup;
CaptureModeSessionTestApi test_api(controller->capture_mode_session());
EXPECT_FALSE(GetImageToggleButton());
// Tab once, check the current focused view is the video toggle button.
auto* event_generator = GetEventGenerator();
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(test_api.GetCurrentFocusedView()->GetView(),
GetVideoToggleButton());
// Now tab four times to focus the settings button and enter space to open the
// settings menu.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/4);
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_EQ(FocusGroup::kPendingSettings, test_api.GetCurrentFocusGroup());
CaptureModeSettingsView* settings_menu =
test_api.GetCaptureModeSettingsView();
ASSERT_TRUE(settings_menu);
CaptureModeSettingsTestApi settings_test_api;
// The `Off` option should not be visible.
EXPECT_FALSE(settings_test_api.GetAudioOffOption());
// Tab twice, the current focused view is the `Microphone` option.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/2);
EXPECT_EQ(test_api.GetCurrentFocusedView()->GetView(),
settings_test_api.GetMicrophoneOption());
}
TEST_P(ProjectorCaptureModeIntegrationTests, BarButtonsState) {
auto* controller = CaptureModeController::Get();
StartProjectorModeSession();
EXPECT_TRUE(controller->IsActive());
// The image toggle button shouldn't be available, whereas the video toggle
// button should be enabled and active.
EXPECT_FALSE(GetImageToggleButton());
EXPECT_TRUE(GetVideoToggleButton()->GetEnabled());
EXPECT_TRUE(GetVideoToggleButton()->selected());
}
TEST_P(ProjectorCaptureModeIntegrationTests, StartEndRecording) {
auto* controller = CaptureModeController::Get();
controller->SetSource(CaptureModeSource::kFullscreen);
StartProjectorModeSession();
EXPECT_TRUE(controller->IsActive());
histogram_tester_.ExpectUniqueSample(kProjectorCreationFlowHistogramName,
ProjectorCreationFlow::kSessionStarted,
/*expected_bucket_count=*/1);
// Hit Enter to begin recording. The recording session should be marked for
// projector.
PressAndReleaseKey(ui::VKEY_RETURN);
EXPECT_CALL(*projector_client(), StartSpeechRecognition());
WaitForRecordingToStart();
histogram_tester_.ExpectBucketCount(kProjectorCreationFlowHistogramName,
ProjectorCreationFlow::kRecordingStarted,
/*expected_count=*/1);
EXPECT_FALSE(controller->IsActive());
EXPECT_TRUE(controller->is_recording_in_progress());
const CaptureModeBehavior* active_behavior =
controller->video_recording_watcher_for_testing()->active_behavior();
ASSERT_TRUE(active_behavior);
EXPECT_TRUE(active_behavior->ShouldCreateAnnotationsOverlayController());
EXPECT_CALL(*projector_client(), StopSpeechRecognition());
controller->EndVideoRecording(EndRecordingReason::kStopRecordingButton);
WaitForCaptureFileToBeSaved();
histogram_tester_.ExpectBucketCount(kProjectorCreationFlowHistogramName,
ProjectorCreationFlow::kRecordingEnded,
/*expected_count=*/1);
histogram_tester_.ExpectBucketCount(kProjectorCreationFlowHistogramName,
ProjectorCreationFlow::kSessionStopped,
/*expected_count=*/1);
histogram_tester_.ExpectTotalCount(kProjectorCreationFlowHistogramName,
/*expected_count=*/4);
}
TEST_P(ProjectorCaptureModeIntegrationTests,
ProjectorSessionNeverStartsWhenCaptureModeIsBlocked) {
auto* controller = CaptureModeController::Get();
controller->SetSource(CaptureModeSource::kFullscreen);
auto* test_delegate =
static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());
test_delegate->set_is_allowed_by_policy(false);
ProjectorController::Get()->StartProjectorSession(
base::SafeBaseName::Create("projector_data").value());
// Both sessions will never start.
EXPECT_FALSE(controller->IsActive());
EXPECT_FALSE(ProjectorSession::Get()->is_active());
EXPECT_FALSE(controller->is_recording_in_progress());
}
TEST_P(ProjectorCaptureModeIntegrationTests,
ProjectorSessionNeverStartsWhenVideoRecordingIsOnGoing) {
auto* controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kVideo);
EXPECT_CALL(
*projector_client(),
OnNewScreencastPreconditionChanged(NewScreencastPrecondition(
NewScreencastPreconditionState::kDisabled,
{NewScreencastPreconditionReason::kScreenRecordingInProgress})));
controller->StartVideoRecordingImmediatelyForTesting();
EXPECT_TRUE(controller->is_recording_in_progress());
EXPECT_FALSE(ProjectorSession::Get()->is_active());
EXPECT_NE(ProjectorController::Get()->GetNewScreencastPrecondition().state,
NewScreencastPreconditionState::kEnabled);
// There is another OnNewScreencastPreconditionChanged() call during tear
// down.
EXPECT_CALL(*projector_client(),
OnNewScreencastPreconditionChanged(NewScreencastPrecondition(
NewScreencastPreconditionState::kEnabled,
{NewScreencastPreconditionReason::kEnabledBySoda})));
}
// Tests that the capture mode configurations in normal capture mode session
// that include the capture mode type, capture mode source and capture mode
// audio settings will not be overridden by the projector-initiated capture mode
// session.
TEST_P(ProjectorCaptureModeIntegrationTests,
RestoreCaptureSessionConfigurationsInNormalCaptureSession) {
// Start an image capture mode session in window mode.
auto* controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kImage);
// Stop the normal capture mode session and start a new projector-initated
// capture mode session. By default the session will be of type video and in
// fullscreen mode with audio on.
controller->Stop();
StartProjectorModeSession();
EXPECT_TRUE(controller->IsActive());
EXPECT_EQ(controller->type(), CaptureModeType::kVideo);
EXPECT_EQ(controller->source(), CaptureModeSource::kFullscreen);
EXPECT_EQ(AudioRecordingMode::kMicrophone,
controller->GetEffectiveAudioRecordingMode());
// Stop the projector-initiated capture mode session and the original capture
// mode configurations will be restored.
controller->Stop();
EXPECT_EQ(controller->type(), CaptureModeType::kImage);
EXPECT_EQ(controller->source(), CaptureModeSource::kWindow);
EXPECT_EQ(AudioRecordingMode::kOff,
controller->GetEffectiveAudioRecordingMode());
// Start a new projector-initiated capture mode session and start the region
// recording.
StartProjectorModeSession();
controller->SetSource(CaptureModeSource::kRegion);
CaptureModeTestApi test_api;
test_api.SetUserSelectedRegion(gfx::Rect(100, 100, 200, 200));
test_api.PerformCapture();
WaitForSeconds(1);
// Start another capture mode session and the source should be restored as
// what has been set before the projector-initiated capture mode session.
controller->Start(CaptureModeEntryType::kQuickSettings);
EXPECT_EQ(controller->source(), CaptureModeSource::kWindow);
controller->Stop();
test_api.StopVideoRecording();
// After completing the video recording in projector-initiated capture mode
// session, the capture mode configurations will be restored as what has been
// set before the projector-initiated capture mode session.
EXPECT_EQ(controller->type(), CaptureModeType::kImage);
EXPECT_EQ(AudioRecordingMode::kOff,
controller->GetEffectiveAudioRecordingMode());
}
namespace {
enum AbortReason {
kBlockedByDlp,
kBlockedByPolicy,
kUserPressedEsc,
};
struct {
const std::string scope_trace;
const AbortReason reason;
} kTestCases[] = {
{"Blocked by DLP", kBlockedByDlp},
{"Blocked by policy", kBlockedByPolicy},
{"User Pressed Esc", kUserPressedEsc},
};
} // namespace
TEST_P(ProjectorCaptureModeIntegrationTests,
ProjectorSessionAbortedBeforeCountDownStarts) {
auto* controller = CaptureModeController::Get();
controller->SetSource(CaptureModeSource::kFullscreen);
for (const auto& test_case : kTestCases) {
SCOPED_TRACE(test_case.scope_trace);
StartProjectorModeSession();
auto* test_delegate = static_cast<TestCaptureModeDelegate*>(
controller->delegate_for_testing());
switch (test_case.reason) {
case kBlockedByDlp:
test_delegate->set_is_allowed_by_dlp(false);
PressAndReleaseKey(ui::VKEY_RETURN);
break;
case kBlockedByPolicy:
test_delegate->set_is_allowed_by_policy(false);
PressAndReleaseKey(ui::VKEY_RETURN);
break;
case kUserPressedEsc:
PressAndReleaseKey(ui::VKEY_ESCAPE);
break;
}
// The session will end immediately without a count down.
EXPECT_FALSE(controller->IsActive());
EXPECT_FALSE(ProjectorSession::Get()->is_active());
EXPECT_FALSE(controller->is_recording_in_progress());
// Prepare for next iteration by resetting things back to default.
test_delegate->ResetAllowancesToDefault();
}
histogram_tester_.ExpectBucketCount(kProjectorCreationFlowHistogramName,
ProjectorCreationFlow::kSessionStarted,
/*expected_count=*/3);
histogram_tester_.ExpectBucketCount(kProjectorCreationFlowHistogramName,
ProjectorCreationFlow::kRecordingAborted,
/*expected_count=*/3);
histogram_tester_.ExpectBucketCount(kProjectorCreationFlowHistogramName,
ProjectorCreationFlow::kSessionStopped,
/*expected_count=*/3);
histogram_tester_.ExpectTotalCount(kProjectorCreationFlowHistogramName,
/*expected_count=*/9);
}
TEST_P(ProjectorCaptureModeIntegrationTests,
ProjectorSessionAbortedAfterCountDownStarts) {
ui::ScopedAnimationDurationScaleMode animation_scale(
ui::ScopedAnimationDurationScaleMode::FAST_DURATION);
auto* controller = CaptureModeController::Get();
controller->SetSource(CaptureModeSource::kFullscreen);
for (const auto& test_case : kTestCases) {
SCOPED_TRACE(test_case.scope_trace);
StartProjectorModeSession();
PressAndReleaseKey(ui::VKEY_RETURN);
auto* test_delegate = static_cast<TestCaptureModeDelegate*>(
controller->delegate_for_testing());
switch (test_case.reason) {
case kBlockedByDlp:
test_delegate->set_is_allowed_by_dlp(false);
break;
case kBlockedByPolicy:
test_delegate->set_is_allowed_by_policy(false);
break;
case kUserPressedEsc:
PressAndReleaseKey(ui::VKEY_ESCAPE);
break;
}
WaitForSessionToEnd();
EXPECT_FALSE(ProjectorSession::Get()->is_active());
EXPECT_FALSE(controller->is_recording_in_progress());
// Prepare for next iteration by resetting things back to default.
test_delegate->ResetAllowancesToDefault();
}
histogram_tester_.ExpectBucketCount(kProjectorCreationFlowHistogramName,
ProjectorCreationFlow::kSessionStarted,
/*expected_count=*/3);
histogram_tester_.ExpectBucketCount(kProjectorCreationFlowHistogramName,
ProjectorCreationFlow::kRecordingAborted,
/*expected_count=*/3);
histogram_tester_.ExpectBucketCount(kProjectorCreationFlowHistogramName,
ProjectorCreationFlow::kSessionStopped,
/*expected_count=*/3);
histogram_tester_.ExpectTotalCount(kProjectorCreationFlowHistogramName,
/*expected_count=*/9);
}
TEST_P(ProjectorCaptureModeIntegrationTests, AnnotationsOverlayWidget) {
auto* controller = CaptureModeController::Get();
controller->SetSource(CaptureModeSource::kFullscreen);
StartProjectorModeSession();
EXPECT_TRUE(controller->IsActive());
PressAndReleaseKey(ui::VKEY_RETURN);
WaitForRecordingToStart();
CaptureModeTestApi test_api;
AnnotationsOverlayController* overlay_controller =
test_api.GetAnnotationsOverlayController();
EXPECT_FALSE(overlay_controller->is_enabled());
auto* overlay_window = overlay_controller->GetOverlayNativeWindow();
VerifyOverlayEnabledState(overlay_window, /*overlay_enabled_state=*/false);
auto* annotator_controller = Shell::Get()->annotator_controller();
annotator_controller->EnableAnnotatorTool();
EXPECT_TRUE(overlay_controller->is_enabled());
VerifyOverlayEnabledState(overlay_window, /*overlay_enabled_state=*/true);
annotator_controller->ResetTools();
EXPECT_FALSE(overlay_controller->is_enabled());
VerifyOverlayEnabledState(overlay_window, /*overlay_enabled_state=*/false);
}
TEST_P(ProjectorCaptureModeIntegrationTests,
AnnotationsOverlayDockedMagnifier) {
auto* controller = CaptureModeController::Get();
controller->SetSource(CaptureModeSource::kFullscreen);
StartProjectorModeSession();
EXPECT_TRUE(controller->IsActive());
PressAndReleaseKey(ui::VKEY_RETURN);
WaitForRecordingToStart();
CaptureModeTestApi test_api;
AnnotationsOverlayController* overlay_controller =
test_api.GetAnnotationsOverlayController();
auto* annotator_controller = Shell::Get()->annotator_controller();
annotator_controller->EnableAnnotatorTool();
EXPECT_TRUE(overlay_controller->is_enabled());
auto* overlay_window = overlay_controller->GetOverlayNativeWindow();
// Before the docked magnifier gets enabled, the overlay's bounds should match
// the root window's bounds.
auto* root_window = overlay_window->GetRootWindow();
const gfx::Rect root_window_bounds = root_window->bounds();
EXPECT_EQ(root_window_bounds, overlay_window->GetBoundsInRootWindow());
// Once the magnifier is enabled, the overlay should be pushed down so that
// it doesn't cover the magnifier viewport.
auto* docked_magnifier = Shell::Get()->docked_magnifier_controller();
docked_magnifier->SetEnabled(true);
const gfx::Rect expected_bounds = gfx::SubtractRects(
root_window_bounds,
docked_magnifier->GetTotalMagnifierBoundsForRoot(root_window));
EXPECT_EQ(expected_bounds, overlay_window->GetBoundsInRootWindow());
// It should go back to original bounds once the magnifier is disabled.
docked_magnifier->SetEnabled(false);
EXPECT_EQ(root_window_bounds, overlay_window->GetBoundsInRootWindow());
}
TEST_P(ProjectorCaptureModeIntegrationTestsWithSource,
AnnotationsOverlayWidgetBounds) {
const auto capture_source = std::get<CaptureModeSource>(GetParam());
StartRecordingForProjectorFromSource(capture_source);
CaptureModeTestApi test_api;
AnnotationsOverlayController* overlay_controller =
test_api.GetAnnotationsOverlayController();
EXPECT_FALSE(overlay_controller->is_enabled());
auto* overlay_window = overlay_controller->GetOverlayNativeWindow();
VerifyOverlayWindow(overlay_window, capture_source, kUserRegion);
}
// Regression test for https://crbug.com/1322655.
TEST_P(ProjectorCaptureModeIntegrationTestsWithSource,
AnnotationsOverlayWidgetBoundsSecondDisplay) {
UpdateDisplay("800x700,801+0-800x700");
const gfx::Point point_in_second_display = gfx::Point(1000, 500);
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseTo(point_in_second_display);
window()->SetBoundsInScreen(
gfx::Rect(900, 0, 600, 500),
display::Screen::GetScreen()->GetDisplayNearestWindow(
Shell::GetAllRootWindows()[1]));
const auto capture_source = std::get<CaptureModeSource>(GetParam());
StartRecordingForProjectorFromSource(capture_source);
const auto roots = Shell::GetAllRootWindows();
EXPECT_EQ(roots[1], GetWindowBeingRecorded()->GetRootWindow());
CaptureModeTestApi test_api;
AnnotationsOverlayController* overlay_controller =
test_api.GetAnnotationsOverlayController();
EXPECT_FALSE(overlay_controller->is_enabled());
auto* overlay_window = overlay_controller->GetOverlayNativeWindow();
VerifyOverlayWindow(overlay_window, capture_source, kUserRegion);
}
// Tests the projector behavior in the projector-initiated capture mode session
// and during video recording.
TEST_P(ProjectorCaptureModeIntegrationTestsWithSource, ProjectorBehavior) {
CaptureModeController* controller = CaptureModeController::Get();
EXPECT_EQ(AudioRecordingMode::kOff,
controller->GetEffectiveAudioRecordingMode());
EXPECT_TRUE(projector_helper_.CanStartProjectorSession());
StartProjectorModeSession();
ASSERT_TRUE(controller->IsActive());
auto* session =
static_cast<CaptureModeSession*>(controller->capture_mode_session());
ASSERT_TRUE(session);
ASSERT_EQ(session->session_type(), SessionType::kReal);
const CaptureModeBehavior* projector_active_behavior =
session->active_behavior();
ASSERT_TRUE(projector_active_behavior);
auto expected_behavior = [&]() {
EXPECT_FALSE(projector_active_behavior->ShouldImageCaptureTypeBeAllowed());
EXPECT_TRUE(projector_active_behavior->ShouldVideoCaptureTypeBeAllowed());
EXPECT_TRUE(
projector_active_behavior->ShouldFulscreenCaptureSourceBeAllowed());
EXPECT_TRUE(
projector_active_behavior->ShouldRegionCaptureSourceBeAllowed());
EXPECT_TRUE(
projector_active_behavior->ShouldWindowCaptureSourceBeAllowed());
EXPECT_FALSE(projector_active_behavior->SupportsAudioRecordingMode(
AudioRecordingMode::kOff));
EXPECT_TRUE(projector_active_behavior->SupportsAudioRecordingMode(
AudioRecordingMode::kMicrophone));
EXPECT_TRUE(
projector_active_behavior->ShouldCameraSelectionSettingsBeIncluded());
EXPECT_TRUE(projector_active_behavior->ShouldDemoToolsSettingsBeIncluded());
EXPECT_FALSE(projector_active_behavior->ShouldSaveToSettingsBeIncluded());
EXPECT_FALSE(projector_active_behavior->ShouldGifBeSupported());
EXPECT_FALSE(projector_active_behavior->ShouldShowPreviewNotification());
EXPECT_FALSE(
projector_active_behavior->ShouldSkipVideoRecordingCountDown());
EXPECT_TRUE(
projector_active_behavior->ShouldCreateAnnotationsOverlayController());
EXPECT_FALSE(projector_active_behavior->ShouldShowUserNudge());
EXPECT_TRUE(projector_active_behavior->ShouldAutoSelectFirstCamera());
};
expected_behavior();
views::Widget* bar_widget = GetCaptureModeBarWidget();
ASSERT_TRUE(bar_widget);
EXPECT_FALSE(GetImageToggleButton());
EXPECT_TRUE(GetVideoToggleButton());
EXPECT_TRUE(GetFullscreenToggleButton());
EXPECT_TRUE(GetRegionToggleButton());
EXPECT_TRUE(GetWindowToggleButton());
EXPECT_FALSE(GetStartRecordingButton());
EXPECT_TRUE(GetSettingsButton());
EXPECT_TRUE(GetCloseButton());
auto* annotator_controller = Shell::Get()->annotator_controller();
annotator_controller->EnableAnnotatorTool();
PressAndReleaseKey(ui::VKEY_RETURN);
WaitForRecordingToStart();
expected_behavior();
CaptureModeTestApi().StopVideoRecording();
}
// Tests that neither preview notification nor recording in tote is shown if in
// projector mode.
TEST_P(ProjectorCaptureModeIntegrationTestsWithSource,
NotShowRecordingInToteOrNotificationForProjectorMode) {
const auto capture_source = std::get<CaptureModeSource>(GetParam());
StartRecordingForProjectorFromSource(capture_source);
CaptureModeTestApi().StopVideoRecording();
WaitForCaptureFileToBeSaved();
EXPECT_FALSE(GetPreviewNotification());
ash::HoldingSpaceTestApi holding_space_api;
EXPECT_TRUE(holding_space_api.GetScreenCaptureViews().empty());
}
// Tests that metrics are recorded correctly for capture configuration entering
// from projector in both clamshell and tablet mode.
TEST_P(ProjectorCaptureModeIntegrationTestsWithSource,
ProjectorCaptureConfigurationMetrics) {
const auto capture_source = std::get<CaptureModeSource>(GetParam());
constexpr char kProjectorCaptureConfigurationHistogramBase[] =
"CaptureConfiguration";
ash::CaptureModeTestApi test_api;
const bool kTabletEnabledStates[]{false, true};
for (const bool tablet_enabled : kTabletEnabledStates) {
if (tablet_enabled) {
SwitchToTabletMode();
EXPECT_TRUE(Shell::Get()->IsInTabletMode());
} else {
EXPECT_FALSE(Shell::Get()->IsInTabletMode());
}
const std::string histogram_name =
BuildHistogramName(kProjectorCaptureConfigurationHistogramBase,
test_api.GetBehavior(BehaviorType::kProjector),
/*append_ui_mode_suffix=*/true);
histogram_tester_.ExpectBucketCount(
histogram_name,
GetConfiguration(CaptureModeType::kVideo, capture_source,
RecordingType::kWebM),
0);
StartRecordingForProjectorFromSource(capture_source);
WaitForSeconds(1);
test_api.StopVideoRecording();
EXPECT_FALSE(CaptureModeController::Get()->is_recording_in_progress());
histogram_tester_.ExpectUniqueSample(
histogram_name,
GetConfiguration(CaptureModeType::kVideo, capture_source,
RecordingType::kWebM),
1);
WaitForCaptureFileToBeSaved();
}
}
// Tests that metrics are recorded correctly for screen recording length
// entering from projector in both clamshell and tablet mode.
TEST_P(ProjectorCaptureModeIntegrationTestsWithSource,
ProjectorScreenRecordingLengthMetrics) {
const auto capture_source = std::get<CaptureModeSource>(GetParam());
constexpr char kProjectorRecordTimeHistogramBase[] = "ScreenRecordingLength";
ash::CaptureModeTestApi test_api;
const bool kTabletEnabledStates[]{false, true};
for (const bool tablet_enabled : kTabletEnabledStates) {
if (tablet_enabled) {
SwitchToTabletMode();
EXPECT_TRUE(Shell::Get()->IsInTabletMode());
} else {
EXPECT_FALSE(Shell::Get()->IsInTabletMode());
}
StartRecordingForProjectorFromSource(capture_source);
WaitForSeconds(1);
test_api.StopVideoRecording();
EXPECT_FALSE(CaptureModeController::Get()->is_recording_in_progress());
WaitForCaptureFileToBeSaved();
histogram_tester_.ExpectUniqueSample(
BuildHistogramName(kProjectorRecordTimeHistogramBase,
test_api.GetBehavior(BehaviorType::kProjector),
/*append_ui_mode_suffix=*/true),
/*sample=*/1, /*expected_bucket_count=*/1);
}
}
// Tests that metrics are recorded correctly for capture region adjustment
// entering from projector in both clamshell and tablet mode.
TEST_P(ProjectorCaptureModeIntegrationTests,
ProjectorCaptureRegionAdjustmentTest) {
constexpr char kProjectorCaptureRegionAdjustmentHistogramBase[] =
"CaptureRegionAdjusted";
auto resize_and_reset_region = [](ui::test::EventGenerator* event_generator,
const gfx::Point& top_right) {
// Enlarges the region and then resize it back to its original size.
event_generator->set_current_screen_location(top_right);
event_generator->DragMouseTo(top_right + gfx::Vector2d(50, 50));
event_generator->DragMouseTo(top_right);
};
auto move_and_reset_region = [](ui::test::EventGenerator* event_generator,
const gfx::Point& drag_point) {
// Moves the region and then moves it back to its original position.
event_generator->set_current_screen_location(drag_point);
event_generator->DragMouseTo(drag_point + gfx::Vector2d(-50, -50));
event_generator->DragMouseTo(drag_point);
};
ash::CaptureModeTestApi test_api;
const std::string histogram_name =
BuildHistogramName(kProjectorCaptureRegionAdjustmentHistogramBase,
test_api.GetBehavior(BehaviorType::kProjector),
/*append_ui_mode_suffix=*/true);
histogram_tester_.ExpectBucketCount(histogram_name, 0, 0);
auto* event_generator = GetEventGenerator();
const gfx::Rect target_region(gfx::Rect(100, 100, 200, 200));
auto top_right = target_region.top_right();
const bool kTabletEnabledStates[] = {false, true};
for (const bool tablet_enabled : kTabletEnabledStates) {
if (tablet_enabled) {
SwitchToTabletMode();
EXPECT_TRUE(Shell::Get()->IsInTabletMode());
} else {
EXPECT_FALSE(Shell::Get()->IsInTabletMode());
}
StartProjectorModeSession();
auto* controller = CaptureModeController::Get();
controller->SetSource(CaptureModeSource::kRegion);
test_api.SetUserSelectedRegion(target_region);
// Resize the region twice by dragging the top right of the region out and
// then back again.
resize_and_reset_region(event_generator, top_right);
// Move the region twice by dragging within the region.
const gfx::Point drag_point(300, 300);
move_and_reset_region(event_generator, drag_point);
test_api.PerformCapture();
WaitForSeconds(1);
test_api.StopVideoRecording();
EXPECT_FALSE(controller->is_recording_in_progress());
histogram_tester_.ExpectBucketCount(histogram_name, 4, 1);
WaitForCaptureFileToBeSaved();
}
}
// Tests that if the user is in projector mode, then presses the shortcut to
// start default capture mode, it is ignored.
TEST_P(ProjectorCaptureModeIntegrationTests, SwitchToDefaultCaptureMode) {
StartProjectorModeSession();
VerifyActiveBehavior(BehaviorType::kProjector);
PressAndReleaseKey(ui::VKEY_MEDIA_LAUNCH_APP1,
ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN);
VerifyActiveBehavior(BehaviorType::kProjector);
}
INSTANTIATE_TEST_SUITE_P(
All,
ProjectorCaptureModeIntegrationTestsWithSource,
testing::Combine(testing::Values(CaptureModeSource::kFullscreen,
CaptureModeSource::kRegion,
CaptureModeSource::kWindow),
testing::Bool(),
testing::Bool()),
[](const testing::TestParamInfo<
ProjectorCaptureModeIntegrationTestsWithSource::ParamType>& info) {
auto source = std::get<CaptureModeSource>(info.param);
bool sunfish_enabled = std::get<1>(info.param);
bool scanner_enabled = std::get<2>(info.param);
return SourceSunfishScannerTestName(source, sunfish_enabled,
scanner_enabled);
});
class AnnotatorCaptureModeIntegrationTestsBase : public CaptureModeTestBase {
public:
AnnotatorCaptureModeIntegrationTestsBase() = default;
~AnnotatorCaptureModeIntegrationTestsBase() override = default;
static constexpr gfx::Rect kUserRegion{20, 50, 60, 70};
aura::Window* window() const { return window_.get(); }
// CaptureModeTestBase:
void SetUp() override {
CaptureModeTestBase::SetUp();
annotator_helper_.SetUp();
window_ = CreateTestWindow(gfx::Rect(20, 30, 200, 200));
CaptureModeController::Get()->SetUserCaptureRegion(kUserRegion,
/*by_user=*/true);
}
void TearDown() override {
window_.reset();
CaptureModeTestBase::TearDown();
}
void StartRecordingFromSource(CaptureModeSource source) {
ash::CaptureModeTestApi test_api;
switch (source) {
case CaptureModeSource::kFullscreen:
test_api.StartForFullscreen(/*for_video=*/true);
break;
case CaptureModeSource::kRegion:
test_api.StartForRegion(/*for_video=*/true);
break;
case CaptureModeSource::kWindow:
test_api.StartForWindow(/*for_video=*/true);
auto* generator = GetEventGenerator();
generator->MoveMouseTo(window_->GetBoundsInScreen().CenterPoint());
break;
}
CaptureModeTestApi().PerformCapture();
WaitForRecordingToStart();
EXPECT_TRUE(CaptureModeController::Get()->is_recording_in_progress());
}
protected:
AnnotatorIntegrationHelper annotator_helper_;
std::unique_ptr<aura::Window> window_;
base::HistogramTester histogram_tester_;
};
class AnnotatorCaptureModeIntegrationTests
: public AnnotatorCaptureModeIntegrationTestsBase,
public ::testing::WithParamInterface<std::tuple<bool, bool>> {
public:
// AnnotatorCaptureModeIntegrationTestsBase:
void SetUp() override {
auto [sunfish_enabled, scanner_enabled] = GetParam();
InitFeatures(sunfish_enabled, scanner_enabled);
AnnotatorCaptureModeIntegrationTestsBase::SetUp();
}
};
INSTANTIATE_TEST_SUITE_P(
,
AnnotatorCaptureModeIntegrationTests,
testing::Combine(testing::Bool(), testing::Bool()),
[](const testing::TestParamInfo<
AnnotatorCaptureModeIntegrationTests::ParamType>& info) {
bool sunfish_enabled = std::get<0>(info.param);
bool scanner_enabled = std::get<1>(info.param);
return SunfishScannerTestName(sunfish_enabled, scanner_enabled);
});
class AnnotatorCaptureModeIntegrationTestsWithSource
: public AnnotatorCaptureModeIntegrationTestsBase,
public ::testing::WithParamInterface<
std::tuple<CaptureModeSource, bool, bool>> {
public:
// AnnotatorCaptureModeIntegrationTestsBase:
void SetUp() override {
auto [unused_source, sunfish_enabled, scanner_enabled] = GetParam();
InitFeatures(sunfish_enabled, scanner_enabled);
AnnotatorCaptureModeIntegrationTestsBase::SetUp();
}
};
TEST_P(AnnotatorCaptureModeIntegrationTests, AnnotationsOverlayWidget) {
StartRecordingFromSource(CaptureModeSource::kFullscreen);
PressAndReleaseKey(ui::VKEY_RETURN);
WaitForRecordingToStart();
CaptureModeTestApi test_api;
AnnotationsOverlayController* overlay_controller =
test_api.GetAnnotationsOverlayController();
EXPECT_FALSE(overlay_controller->is_enabled());
auto* overlay_window = overlay_controller->GetOverlayNativeWindow();
VerifyOverlayEnabledState(overlay_window, /*overlay_enabled_state=*/false);
auto* annotator_controller = Shell::Get()->annotator_controller();
annotator_controller->EnableAnnotatorTool();
EXPECT_TRUE(overlay_controller->is_enabled());
VerifyOverlayEnabledState(overlay_window, /*overlay_enabled_state=*/true);
annotator_controller->ResetTools();
EXPECT_FALSE(overlay_controller->is_enabled());
VerifyOverlayEnabledState(overlay_window, /*overlay_enabled_state=*/false);
}
TEST_P(AnnotatorCaptureModeIntegrationTests,
AnnotationsOverlayDockedMagnifier) {
StartRecordingFromSource(CaptureModeSource::kFullscreen);
PressAndReleaseKey(ui::VKEY_RETURN);
WaitForRecordingToStart();
CaptureModeTestApi test_api;
AnnotationsOverlayController* overlay_controller =
test_api.GetAnnotationsOverlayController();
auto* annotator_controller = Shell::Get()->annotator_controller();
annotator_controller->EnableAnnotatorTool();
EXPECT_TRUE(overlay_controller->is_enabled());
auto* overlay_window = overlay_controller->GetOverlayNativeWindow();
// Before the docked magnifier gets enabled, the overlay's bounds should match
// the root window's bounds.
auto* root_window = overlay_window->GetRootWindow();
const gfx::Rect root_window_bounds = root_window->bounds();
EXPECT_EQ(root_window_bounds, overlay_window->GetBoundsInRootWindow());
// Once the magnifier is enabled, the overlay should be pushed down so that
// it doesn't cover the magnifier viewport.
auto* docked_magnifier = Shell::Get()->docked_magnifier_controller();
docked_magnifier->SetEnabled(true);
const gfx::Rect expected_bounds = gfx::SubtractRects(
root_window_bounds,
docked_magnifier->GetTotalMagnifierBoundsForRoot(root_window));
EXPECT_EQ(expected_bounds, overlay_window->GetBoundsInRootWindow());
// It should go back to original bounds once the magnifier is disabled.
docked_magnifier->SetEnabled(false);
EXPECT_EQ(root_window_bounds, overlay_window->GetBoundsInRootWindow());
}
namespace {
// Defines a class that intercepts the events at the post-target handling phase
// and caches the last event target to which the event was routed.
class EventTargetCatcher : public ui::EventHandler {
public:
EventTargetCatcher() {
Shell::GetPrimaryRootWindow()->AddPostTargetHandler(this);
}
EventTargetCatcher(const EventTargetCatcher&) = delete;
EventTargetCatcher& operator=(const EventTargetCatcher&) = delete;
~EventTargetCatcher() override {
Shell::GetPrimaryRootWindow()->RemovePostTargetHandler(this);
}
ui::EventTarget* last_event_target() { return last_event_target_; }
// ui::EventHandler:
void OnEvent(ui::Event* event) override {
ui::EventHandler::OnEvent(event);
last_event_target_ = event->target();
}
private:
raw_ptr<ui::EventTarget> last_event_target_ = nullptr;
};
} // namespace
TEST_P(AnnotatorCaptureModeIntegrationTests,
AnnotationsOverlayWidgetTargeting) {
StartRecordingFromSource(CaptureModeSource::kFullscreen);
PressAndReleaseKey(ui::VKEY_RETURN);
WaitForRecordingToStart();
CaptureModeTestApi test_api;
AnnotationsOverlayController* overlay_controller =
test_api.GetAnnotationsOverlayController();
auto* annotator_controller = Shell::Get()->annotator_controller();
annotator_controller->EnableAnnotatorTool();
EXPECT_TRUE(overlay_controller->is_enabled());
auto* overlay_window = overlay_controller->GetOverlayNativeWindow();
VerifyOverlayEnabledState(overlay_window, /*overlay_enabled_state=*/true);
// Open the annotation tray bubble.
auto* root_window = Shell::GetPrimaryRootWindow();
auto* status_area_widget =
RootWindowController::ForWindow(root_window)->GetStatusAreaWidget();
AnnotationTray* annotations_tray = status_area_widget->annotation_tray();
annotations_tray->ShowBubble();
EXPECT_TRUE(annotations_tray->GetBubbleView());
// Clicking anywhere outside the projector shelf pod should be targeted to the
// overlay widget window and close the annotation tray bubble.
EventTargetCatcher event_target_catcher;
auto* event_generator = GetEventGenerator();
event_generator->set_current_screen_location(gfx::Point(10, 10));
event_generator->ClickLeftButton();
EXPECT_EQ(overlay_window, event_target_catcher.last_event_target());
EXPECT_FALSE(annotations_tray->GetBubbleView());
// Now move the mouse over the projector shelf pod, the overlay should not
// consume the event, and it should instead go through to that pod.
EXPECT_TRUE(annotations_tray->visible_preferred());
event_generator->MoveMouseTo(
annotations_tray->GetBoundsInScreen().CenterPoint());
EXPECT_EQ(annotations_tray->GetWidget()->GetNativeWindow(),
event_target_catcher.last_event_target());
// The overlay status hasn't changed.
VerifyOverlayEnabledState(overlay_window, /*overlay_enabled_state=*/true);
// Now move the mouse and then click on the stop recording button, the overlay
// should not consume the event. The video recording should be ended.
StopRecordingButtonTray* stop_recording_button =
status_area_widget->stop_recording_button_tray();
const gfx::Point stop_button_center_point =
stop_recording_button->GetBoundsInScreen().CenterPoint();
event_generator->MoveMouseTo(stop_button_center_point);
event_generator->ClickLeftButton();
EXPECT_FALSE(CaptureModeController::Get()->is_recording_in_progress());
}
// Tests that auto hidden shelf can be brought back if user moves mouse to the
// shelf activation area even while annotation is active.
TEST_P(AnnotatorCaptureModeIntegrationTests,
BringBackAutoHiddenShelfWhileAnnotationIsOn) {
auto* root_window = Shell::GetPrimaryRootWindow();
// Set `shelf` to always auto-hidden.
Shelf* shelf = RootWindowController::ForWindow(root_window)->shelf();
shelf->SetAutoHideBehavior(ShelfAutoHideBehavior::kAlways);
StartRecordingFromSource(CaptureModeSource::kFullscreen);
PressAndReleaseKey(ui::VKEY_RETURN);
WaitForRecordingToStart();
auto* event_generator = GetEventGenerator();
auto* annotator_controller = Shell::Get()->annotator_controller();
const gfx::Rect root_window_bounds_in_screen =
root_window->GetBoundsInScreen();
const int display_width = root_window_bounds_in_screen.width();
const int display_height = root_window_bounds_in_screen.height();
const gfx::Point display_center = root_window_bounds_in_screen.CenterPoint();
struct {
const std::string scope_trace;
const ShelfAlignment shelf_alignment;
} kAlignmentTestCases[] = {
{"Shelf has botton alignment", ShelfAlignment::kBottom},
{"Shelf has left alignment", ShelfAlignment::kLeft},
{"Shelf has right alignment", ShelfAlignment::kRight},
};
for (const auto& test_case : kAlignmentTestCases) {
SCOPED_TRACE(test_case.scope_trace);
// Enable annotation.
annotator_controller->EnableAnnotatorTool();
// Verify shelf is invisible right now.
EXPECT_FALSE(shelf->IsVisible());
shelf->SetAlignment(test_case.shelf_alignment);
switch (test_case.shelf_alignment) {
case ShelfAlignment::kBottom:
case ShelfAlignment::kBottomLocked:
event_generator->MoveMouseTo(0, display_height);
break;
case ShelfAlignment::kLeft:
event_generator->MoveMouseTo(0, display_height);
break;
case ShelfAlignment::kRight:
event_generator->MoveMouseTo(display_width, display_height);
break;
}
// Verify after mouse is moved on top of the shelf activation area, shelf is
// brought back and visible once the animation to show shelf is finished.
ShellTestApi().WaitForWindowFinishAnimating(shelf->GetWindow());
EXPECT_TRUE(shelf->IsVisible());
// Disable annotation.
annotator_controller->ResetTools();
// Move mouse to the outside of the shelf activation area, and wait for the
// animation to hide shelf to finish.
event_generator->MoveMouseTo(display_center);
ShellTestApi().WaitForWindowFinishAnimating(shelf->GetWindow());
}
}
TEST_P(AnnotatorCaptureModeIntegrationTestsWithSource,
AnnotationsOverlayWidgetBounds) {
const auto capture_source = std::get<CaptureModeSource>(GetParam());
StartRecordingFromSource(capture_source);
CaptureModeTestApi test_api;
AnnotationsOverlayController* overlay_controller =
test_api.GetAnnotationsOverlayController();
EXPECT_FALSE(overlay_controller->is_enabled());
auto* overlay_window = overlay_controller->GetOverlayNativeWindow();
VerifyOverlayWindow(overlay_window, capture_source, kUserRegion);
}
TEST_P(AnnotatorCaptureModeIntegrationTestsWithSource,
AnnotationsOverlayWidgetBoundsSecondDisplay) {
UpdateDisplay("800x700,801+0-800x700");
const gfx::Point point_in_second_display = gfx::Point(1000, 500);
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseTo(point_in_second_display);
window()->SetBoundsInScreen(
gfx::Rect(900, 0, 600, 500),
display::Screen::GetScreen()->GetDisplayNearestWindow(
Shell::GetAllRootWindows()[1]));
const auto capture_source = std::get<CaptureModeSource>(GetParam());
StartRecordingFromSource(capture_source);
const auto roots = Shell::GetAllRootWindows();
EXPECT_EQ(roots[1], GetWindowBeingRecorded()->GetRootWindow());
CaptureModeTestApi test_api;
AnnotationsOverlayController* overlay_controller =
test_api.GetAnnotationsOverlayController();
EXPECT_FALSE(overlay_controller->is_enabled());
auto* overlay_window = overlay_controller->GetOverlayNativeWindow();
VerifyOverlayWindow(overlay_window, capture_source, kUserRegion);
}
INSTANTIATE_TEST_SUITE_P(
All,
AnnotatorCaptureModeIntegrationTestsWithSource,
testing::Combine(testing::Values(CaptureModeSource::kFullscreen,
CaptureModeSource::kRegion,
CaptureModeSource::kWindow),
testing::Bool(),
testing::Bool()),
[](const testing::TestParamInfo<
AnnotatorCaptureModeIntegrationTestsWithSource::ParamType>& info) {
auto source = std::get<CaptureModeSource>(info.param);
bool sunfish_enabled = std::get<1>(info.param);
bool scanner_enabled = std::get<2>(info.param);
return SourceSunfishScannerTestName(source, sunfish_enabled,
scanner_enabled);
});
// -----------------------------------------------------------------------------
// CaptureModeSettingsTest:
// Test fixture for CaptureMode settings view.
class CaptureModeSettingsTestBase : public CaptureModeTestBase {
public:
CaptureModeSettingsTestBase() = default;
~CaptureModeSettingsTestBase() override = default;
// CaptureModeTestBase:
void SetUp() override {
CaptureModeTestBase::SetUp();
FakeFolderSelectionDialogFactory::Start();
}
void TearDown() override {
FakeFolderSelectionDialogFactory::Stop();
CaptureModeTestBase::TearDown();
}
CaptureModeSettingsView* GetCaptureModeSettingsView() const {
auto* session = CaptureModeController::Get()->capture_mode_session();
DCHECK(session);
return CaptureModeSessionTestApi(session).GetCaptureModeSettingsView();
}
void WaitForSettingsMenuToBeRefreshed() {
base::RunLoop run_loop;
CaptureModeSettingsTestApi().SetOnSettingsMenuRefreshedCallback(
run_loop.QuitClosure());
run_loop.Run();
}
};
class CaptureModeSettingsTest
: public CaptureModeSettingsTestBase,
public testing::WithParamInterface<std::tuple<bool, bool>> {
public:
CaptureModeSettingsTest() = default;
~CaptureModeSettingsTest() override = default;
// CaptureModeSettingsTestBase:
void SetUp() override {
auto [sunfish_enabled, scanner_enabled] = GetParam();
InitFeatures(sunfish_enabled, scanner_enabled);
CaptureModeSettingsTestBase::SetUp();
}
};
INSTANTIATE_TEST_SUITE_P(
,
CaptureModeSettingsTest,
testing::Combine(testing::Bool(), testing::Bool()),
[](const testing::TestParamInfo<CaptureModeSettingsTest::ParamType>& info) {
bool sunfish_enabled = std::get<0>(info.param);
bool scanner_enabled = std::get<1>(info.param);
return SunfishScannerTestName(sunfish_enabled, scanner_enabled);
});
TEST_P(CaptureModeSettingsTest, NudgeChangesRootWithBar) {
UpdateDisplay("800x700,801+0-800x700");
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseTo(gfx::Point(100, 500));
auto* controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kImage);
auto* session =
static_cast<CaptureModeSession*>(controller->capture_mode_session());
ASSERT_EQ(session->session_type(), SessionType::kReal);
auto* capture_toast_controller = session->capture_toast_controller();
EXPECT_EQ(Shell::GetAllRootWindows()[0], session->current_root());
EXPECT_EQ(capture_toast_controller->capture_toast_widget()
->GetNativeWindow()
->GetRootWindow(),
session->current_root());
event_generator->MoveMouseTo(gfx::Point(1000, 500));
EXPECT_EQ(Shell::GetAllRootWindows()[1], session->current_root());
EXPECT_EQ(capture_toast_controller->capture_toast_widget()
->GetNativeWindow()
->GetRootWindow(),
session->current_root());
}
TEST_P(CaptureModeSettingsTest, NudgeBehaviorWhenSelectingRegion) {
UpdateDisplay("800x700,801+0-800x700");
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseTo(gfx::Point(100, 500));
auto* controller = StartImageRegionCapture();
auto* session =
static_cast<CaptureModeSession*>(controller->capture_mode_session());
ASSERT_EQ(session->session_type(), SessionType::kReal);
EXPECT_EQ(Shell::GetAllRootWindows()[0], session->current_root());
// Nudge hides while selecting a region, but doesn't change roots until the
// region change is committed.
auto* nudge_controller = GetUserNudgeController();
event_generator->MoveMouseTo(gfx::Point(1000, 500));
event_generator->PressLeftButton();
EXPECT_FALSE(nudge_controller->is_visible());
// If we release without selecting a valid region (i.e., an empty region), the
// nudge should be visible again.
event_generator->ReleaseLeftButton();
EXPECT_EQ(Shell::GetAllRootWindows()[1], session->current_root());
// The nudge shows again, and is on the second display.
EXPECT_TRUE(nudge_controller->is_visible());
EXPECT_EQ(session->capture_toast_controller()
->capture_toast_widget()
->GetNativeWindow()
->GetRootWindow(),
session->current_root());
}
TEST_P(CaptureModeSettingsTest, NudgeDoesNotShowForAllUserTypes) {
struct {
std::string trace;
user_manager::UserType user_type;
bool can_see_nudge;
} kUserTypeTestCases[] = {
{"regular user", user_manager::UserType::kRegular, true},
{"child", user_manager::UserType::kChild, true},
{"guest", user_manager::UserType::kGuest, false},
{"public account", user_manager::UserType::kPublicAccount, false},
{"kiosk app", user_manager::UserType::kKioskChromeApp, false},
{"web kiosk app", user_manager::UserType::kKioskWebApp, false},
};
for (const auto& test_case : kUserTypeTestCases) {
SCOPED_TRACE(test_case.trace);
ClearLogin();
SimulateUserLogin({"example@gmail.com", test_case.user_type});
auto* controller = StartImageRegionCapture();
EXPECT_EQ(test_case.can_see_nudge, controller->CanShowSunfishRegionNudge());
auto* nudge_controller = GetUserNudgeController();
EXPECT_EQ(test_case.can_see_nudge, !!nudge_controller);
controller->Stop();
}
}
// Tests that the capture mode settings menu is centered with respect to the
// capture bar.
TEST_P(CaptureModeSettingsTest, SettingsMenuCenteredWithCaptureBar) {
StartCaptureSession(CaptureModeSource::kFullscreen, CaptureModeType::kImage);
auto* bar_widget = GetCaptureModeBarWidget();
ASSERT_TRUE(bar_widget);
ClickOnView(GetSettingsButton(), GetEventGenerator());
auto* settings_widget = GetCaptureModeSettingsWidget();
ASSERT_TRUE(settings_widget);
EXPECT_NEAR(settings_widget->GetWindowBoundsInScreen().CenterPoint().x(),
bar_widget->GetWindowBoundsInScreen().CenterPoint().x(),
/*abs_error=*/1);
}
// Tests that it's possbile to take a screenshot using the keyboard shortcut at
// the login screen without any crashes. https://crbug.com/1266728.
TEST_P(CaptureModeSettingsTest, TakeScreenshotAtLoginScreen) {
ClearLogin();
PressAndReleaseKey(ui::VKEY_MEDIA_LAUNCH_APP1, ui::EF_CONTROL_DOWN);
WaitForCaptureFileToBeSaved();
}
// Tests that clicking on audio input buttons updates the state in the
// controller, and persists between sessions.
TEST_P(CaptureModeSettingsTest, AudioInputSettingsMenu) {
auto* controller = StartImageRegionCapture();
auto* event_generator = GetEventGenerator();
// Test that the audio recording preference is defaulted to off.
ClickOnView(GetSettingsButton(), event_generator);
EXPECT_EQ(AudioRecordingMode::kOff,
controller->GetEffectiveAudioRecordingMode());
CaptureModeSettingsTestApi test_api;
CaptureModeMenuGroup* audio_input_menu_group =
test_api.GetAudioInputMenuGroup();
EXPECT_TRUE(audio_input_menu_group->IsOptionChecked(kAudioOff));
EXPECT_FALSE(audio_input_menu_group->IsOptionChecked(kAudioMicrophone));
// Click on the |microphone| option. It should be checked after click along
// with |off| is unchecked. Recording preference is set to microphone.
views::View* microphone_option = test_api.GetMicrophoneOption();
ClickOnView(microphone_option, event_generator);
EXPECT_TRUE(audio_input_menu_group->IsOptionChecked(kAudioMicrophone));
EXPECT_FALSE(audio_input_menu_group->IsOptionChecked(kAudioOff));
EXPECT_EQ(AudioRecordingMode::kMicrophone,
controller->GetEffectiveAudioRecordingMode());
// Test that the user selected audio preference for audio recording is
// remembered between sessions.
SendKey(ui::VKEY_ESCAPE, event_generator);
StartImageRegionCapture();
EXPECT_EQ(AudioRecordingMode::kMicrophone,
controller->GetEffectiveAudioRecordingMode());
}
TEST_P(CaptureModeSettingsTest, AccessibleName) {
StartImageRegionCapture();
ClickOnView(GetSettingsButton(), GetEventGenerator());
CaptureModeSettingsTestApi test_api;
CaptureModeMenuGroup* audio_input_menu_group =
test_api.GetAudioInputMenuGroup();
views::View* menu_header_view = audio_input_menu_group->menu_header();
ui::AXNodeData data;
menu_header_view->GetViewAccessibility().GetAccessibleNodeData(&data);
EXPECT_EQ(l10n_util::GetStringUTF16(IDS_ASH_SCREEN_CAPTURE_AUDIO_INPUT),
data.GetString16Attribute(ax::mojom::StringAttribute::kName));
}
TEST_P(CaptureModeSettingsTest, AudioCaptureDisabledByPolicy) {
auto* controller = CaptureModeController::Get();
// Even if audio recording is set to enabled, the policy setting will
// overwrite it.
controller->SetAudioRecordingMode(AudioRecordingMode::kMicrophone);
auto* delegate =
static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());
delegate->set_is_audio_capture_disabled_by_policy(true);
EXPECT_EQ(AudioRecordingMode::kOff,
controller->GetEffectiveAudioRecordingMode());
StartImageRegionCapture();
// Open the settings menu, and check that "Audio Off" setting is dimmed out,
// and the "Microphone" setting was not added. This menu group should be
// marked as "managed by policy".
ClickOnView(GetSettingsButton(), GetEventGenerator());
CaptureModeSettingsTestApi test_api;
CaptureModeMenuGroup* audio_input_menu_group =
test_api.GetAudioInputMenuGroup();
EXPECT_TRUE(audio_input_menu_group->IsManagedByPolicy());
EXPECT_TRUE(audio_input_menu_group->IsOptionChecked(kAudioOff));
EXPECT_FALSE(audio_input_menu_group->IsOptionEnabled(kAudioOff));
EXPECT_FALSE(test_api.GetMicrophoneOption());
}
TEST_P(CaptureModeSettingsTest, SelectFolderFromDialog) {
auto* controller = StartImageRegionCapture();
auto* event_generator = GetEventGenerator();
ClickOnView(GetSettingsButton(), event_generator);
// Initially there should only be an option for the default downloads folder.
CaptureModeSettingsTestApi test_api;
EXPECT_TRUE(test_api.GetDefaultDownloadsOption());
EXPECT_FALSE(test_api.GetCustomFolderOptionIfAny());
CaptureModeMenuGroup* save_to_menu_group = test_api.GetSaveToMenuGroup();
EXPECT_TRUE(save_to_menu_group->IsOptionChecked(kDownloadsFolder));
ClickOnView(test_api.GetSelectFolderMenuItem(), event_generator);
EXPECT_TRUE(IsFolderSelectionDialogShown());
EXPECT_FALSE(AreAllCaptureSessionUisVisible());
auto* dialog_factory = FakeFolderSelectionDialogFactory::Get();
auto* dialog_window = dialog_factory->GetDialogWindow();
auto* window_state = WindowState::Get(dialog_window);
ASSERT_TRUE(window_state);
EXPECT_FALSE(window_state->CanMaximize());
EXPECT_FALSE(window_state->CanMinimize());
EXPECT_FALSE(window_state->CanResize());
// Accepting the dialog with a folder selection should dismiss it and add a
// new option for the custom selected folder in the settings menu.
const base::FilePath custom_folder(
CreateCustomFolderInUserDownloadsPath("test"));
dialog_factory->AcceptPath(custom_folder);
WaitForSettingsMenuToBeRefreshed();
EXPECT_FALSE(IsFolderSelectionDialogShown());
EXPECT_TRUE(AreAllCaptureSessionUisVisible());
EXPECT_TRUE(save_to_menu_group->IsOptionChecked(kCustomFolder));
EXPECT_FALSE(save_to_menu_group->IsOptionChecked(kDownloadsFolder));
EXPECT_EQ(u"test",
save_to_menu_group->GetOptionLabelForTesting(kCustomFolder));
// This should update the folder that will be used by the controller.
auto capture_folder = controller->GetCurrentCaptureFolder();
EXPECT_EQ(capture_folder.path, custom_folder);
EXPECT_FALSE(capture_folder.is_default_downloads_folder);
}
// Tests that folder selection dialog can be opened without crash while in
// window capture mode.
TEST_P(CaptureModeSettingsTest, SelectFolderInWindowCaptureMode) {
std::unique_ptr<aura::Window> window1(
CreateTestWindow(gfx::Rect(0, 0, 200, 300)));
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kImage);
auto* event_generator = GetEventGenerator();
ClickOnView(GetSettingsButton(), event_generator);
CaptureModeSettingsTestApi test_api;
ClickOnView(test_api.GetSelectFolderMenuItem(), event_generator);
EXPECT_TRUE(IsFolderSelectionDialogShown());
}
TEST_P(CaptureModeSettingsTest, DismissDialogWithoutSelection) {
auto* controller = StartImageRegionCapture();
const auto old_capture_folder = controller->GetCurrentCaptureFolder();
// Open the settings menu, and click the "Select folder" menu item.
auto* event_generator = GetEventGenerator();
ClickOnView(GetSettingsButton(), event_generator);
CaptureModeSettingsTestApi test_api;
ClickOnView(test_api.GetSelectFolderMenuItem(), event_generator);
EXPECT_TRUE(IsFolderSelectionDialogShown());
// Cancel and dismiss the dialog. There should be no change in the folder
// selection.
auto* dialog_factory = FakeFolderSelectionDialogFactory::Get();
dialog_factory->CancelDialog();
EXPECT_FALSE(IsFolderSelectionDialogShown());
EXPECT_FALSE(test_api.GetCustomFolderOptionIfAny());
CaptureModeMenuGroup* save_to_menu_group = test_api.GetSaveToMenuGroup();
EXPECT_TRUE(save_to_menu_group->IsOptionChecked(kDownloadsFolder));
const auto new_capture_folder = controller->GetCurrentCaptureFolder();
EXPECT_EQ(old_capture_folder.path, new_capture_folder.path);
EXPECT_EQ(old_capture_folder.is_default_downloads_folder,
new_capture_folder.is_default_downloads_folder);
}
TEST_P(CaptureModeSettingsTest, AcceptUpdatedCustomFolderFromDialog) {
// Start a new session with a pre-configured custom folder.
auto* controller = CaptureModeController::Get();
const base::FilePath custom_folder(
CreateCustomFolderInUserDownloadsPath("test"));
controller->SetCustomCaptureFolder(custom_folder);
StartImageRegionCapture();
// Open the settings menu and check there already exists an item for that
// pre-configured custom folder.
auto* event_generator = GetEventGenerator();
ClickOnView(GetSettingsButton(), event_generator);
WaitForSettingsMenuToBeRefreshed();
CaptureModeSettingsTestApi test_api;
EXPECT_TRUE(test_api.GetDefaultDownloadsOption());
auto* custom_folder_view = test_api.GetCustomFolderOptionIfAny();
EXPECT_TRUE(custom_folder_view);
CaptureModeMenuGroup* save_to_menu_group = test_api.GetSaveToMenuGroup();
EXPECT_FALSE(save_to_menu_group->IsOptionChecked(kDownloadsFolder));
EXPECT_TRUE(save_to_menu_group->IsOptionChecked(kCustomFolder));
// Now open the folder selection dialog and select a different folder. The
// existing *same* item in the menu should be updated.
ClickOnView(test_api.GetSelectFolderMenuItem(), event_generator);
EXPECT_TRUE(IsFolderSelectionDialogShown());
auto* dialog_factory = FakeFolderSelectionDialogFactory::Get();
const base::FilePath new_folder(
CreateCustomFolderInUserDownloadsPath("test1"));
dialog_factory->AcceptPath(new_folder);
WaitForSettingsMenuToBeRefreshed();
EXPECT_FALSE(IsFolderSelectionDialogShown());
EXPECT_EQ(custom_folder_view, test_api.GetCustomFolderOptionIfAny());
EXPECT_TRUE(save_to_menu_group->IsOptionChecked(kCustomFolder));
EXPECT_FALSE(save_to_menu_group->IsOptionChecked(kDownloadsFolder));
EXPECT_EQ(u"test1",
save_to_menu_group->GetOptionLabelForTesting(kCustomFolder));
// This should update the folder that will be used by the controller.
const auto capture_folder = controller->GetCurrentCaptureFolder();
EXPECT_EQ(capture_folder.path, new_folder);
EXPECT_FALSE(capture_folder.is_default_downloads_folder);
}
TEST_P(CaptureModeSettingsTest,
InitializeSettingsViewWithUnavailableCustomFolder) {
// Start a new session with a pre-configured unavailable custom folder.
auto* controller = CaptureModeController::Get();
const base::FilePath default_folder =
controller->delegate_for_testing()->GetUserDefaultDownloadsFolder();
const base::FilePath custom_folder(FILE_PATH_LITERAL("/home/random"));
controller->SetCustomCaptureFolder(custom_folder);
StartImageRegionCapture();
// Open the settings menu and check there already exists an item for that
// pre-configured custom folder. Since the custom folder is unavailable, the
// item should be disabled and dimmed. The item of the default folder should
// be checked.
auto* event_generator = GetEventGenerator();
ClickOnView(GetSettingsButton(), event_generator);
WaitForSettingsMenuToBeRefreshed();
CaptureModeSettingsTestApi test_api;
EXPECT_TRUE(test_api.GetDefaultDownloadsOption());
auto* custom_folder_view = test_api.GetCustomFolderOptionIfAny();
EXPECT_TRUE(custom_folder_view);
CaptureModeMenuGroup* save_to_menu_group = test_api.GetSaveToMenuGroup();
EXPECT_TRUE(save_to_menu_group->IsOptionChecked(kDownloadsFolder));
EXPECT_FALSE(save_to_menu_group->IsOptionChecked(kCustomFolder));
EXPECT_FALSE(custom_folder_view->GetEnabled());
EXPECT_EQ(u"random",
save_to_menu_group->GetOptionLabelForTesting(kCustomFolder));
// Now open the folder selection dialog and select an available folder. The
// item of the custom folder should be checked and enabled.
ClickOnView(test_api.GetSelectFolderMenuItem(), event_generator);
EXPECT_TRUE(IsFolderSelectionDialogShown());
auto* dialog_factory = FakeFolderSelectionDialogFactory::Get();
const base::FilePath new_folder(
CreateCustomFolderInUserDownloadsPath("test"));
dialog_factory->AcceptPath(new_folder);
WaitForSettingsMenuToBeRefreshed();
EXPECT_EQ(custom_folder_view, test_api.GetCustomFolderOptionIfAny());
EXPECT_TRUE(save_to_menu_group->IsOptionChecked(kCustomFolder));
EXPECT_FALSE(save_to_menu_group->IsOptionChecked(kDownloadsFolder));
EXPECT_TRUE(custom_folder_view->GetEnabled());
EXPECT_EQ(u"test",
save_to_menu_group->GetOptionLabelForTesting(kCustomFolder));
}
TEST_P(CaptureModeSettingsTest, DeleteCustomFolderFromDialog) {
// Start a new session with a pre-configured custom folder.
auto* controller = CaptureModeController::Get();
const base::FilePath custom_folder(
CreateCustomFolderInUserDownloadsPath("test"));
controller->SetCustomCaptureFolder(custom_folder);
StartImageRegionCapture();
// Open the settings menu and check there exists an item for that custom
// folder. And the item is checked to indicate the current folder in use to
// save the captured files is the custom folder.
auto* event_generator = GetEventGenerator();
ClickOnView(GetSettingsButton(), event_generator);
WaitForSettingsMenuToBeRefreshed();
CaptureModeSettingsTestApi test_api;
EXPECT_TRUE(test_api.GetDefaultDownloadsOption());
auto* custom_folder_view = test_api.GetCustomFolderOptionIfAny();
EXPECT_TRUE(custom_folder_view);
CaptureModeMenuGroup* save_to_menu_group = test_api.GetSaveToMenuGroup();
EXPECT_TRUE(save_to_menu_group->IsOptionChecked(kCustomFolder));
// Now open the folder selection dialog and delete the custom folder. Check
// the item on the settings menu for custom folder is still there but disabled
// and dimmed. The item of the default folder is checked now.
ClickOnView(test_api.GetSelectFolderMenuItem(), event_generator);
EXPECT_TRUE(IsFolderSelectionDialogShown());
auto* dialog_factory = FakeFolderSelectionDialogFactory::Get();
{
base::ScopedAllowBlockingForTesting allow_blocking;
const bool result = base::DeleteFile(custom_folder);
DCHECK(result);
}
dialog_factory->CancelDialog();
WaitForSettingsMenuToBeRefreshed();
EXPECT_TRUE(custom_folder_view);
EXPECT_FALSE(save_to_menu_group->IsOptionChecked(kCustomFolder));
EXPECT_FALSE(custom_folder_view->GetEnabled());
EXPECT_TRUE(save_to_menu_group->IsOptionChecked(kDownloadsFolder));
}
TEST_P(CaptureModeSettingsTest, AccessibleCheckedStateChange) {
// Start a new session with a pre-configured custom folder.
ui::AXNodeData data;
auto* controller = CaptureModeController::Get();
const base::FilePath custom_folder(
CreateCustomFolderInUserDownloadsPath("test"));
controller->SetCustomCaptureFolder(custom_folder);
StartImageRegionCapture();
auto* event_generator = GetEventGenerator();
ClickOnView(GetSettingsButton(), event_generator);
WaitForSettingsMenuToBeRefreshed();
CaptureModeSettingsTestApi test_api;
CaptureModeMenuGroup* save_to_menu_group = test_api.GetSaveToMenuGroup();
auto* checked_custom_folder_view =
save_to_menu_group->SetOptionCheckedForTesting(kCustomFolder, true);
checked_custom_folder_view->GetViewAccessibility().GetAccessibleNodeData(
&data);
EXPECT_EQ(data.GetCheckedState(), ax::mojom::CheckedState::kTrue);
data = ui::AXNodeData();
auto* unchecked_custom_folder_view =
save_to_menu_group->SetOptionCheckedForTesting(kCustomFolder, false);
unchecked_custom_folder_view->GetViewAccessibility().GetAccessibleNodeData(
&data);
EXPECT_EQ(data.GetCheckedState(), ax::mojom::CheckedState::kFalse);
}
TEST_P(CaptureModeSettingsTest, AcceptDefaultDownloadsFolderFromDialog) {
// Start a new session with a pre-configured custom folder.
auto* controller = CaptureModeController::Get();
controller->SetCustomCaptureFolder(
base::FilePath(FILE_PATH_LITERAL("/home/tests/foo")));
StartImageRegionCapture();
auto* event_generator = GetEventGenerator();
ClickOnView(GetSettingsButton(), event_generator);
WaitForSettingsMenuToBeRefreshed();
CaptureModeSettingsTestApi test_api;
ClickOnView(test_api.GetSelectFolderMenuItem(), event_generator);
// Selecting the same folder as the default downloads folder should result in
// removing the custom folder option from the menu.
auto* test_delegate = controller->delegate_for_testing();
const auto default_downloads_folder =
test_delegate->GetUserDefaultDownloadsFolder();
auto* dialog_factory = FakeFolderSelectionDialogFactory::Get();
dialog_factory->AcceptPath(default_downloads_folder);
EXPECT_FALSE(IsFolderSelectionDialogShown());
EXPECT_TRUE(test_api.GetDefaultDownloadsOption());
EXPECT_FALSE(test_api.GetCustomFolderOptionIfAny());
CaptureModeMenuGroup* save_to_menu_group = test_api.GetSaveToMenuGroup();
EXPECT_TRUE(save_to_menu_group->IsOptionChecked(kDownloadsFolder));
}
TEST_P(CaptureModeSettingsTest, SwitchWhichFolderToUserFromOptions) {
// Start a new session with a pre-configured custom folder.
auto* controller = CaptureModeController::Get();
const base::FilePath custom_path(
(CreateCustomFolderInUserDownloadsPath("test")));
controller->SetCustomCaptureFolder(custom_path);
StartImageRegionCapture();
auto* event_generator = GetEventGenerator();
ClickOnView(GetSettingsButton(), event_generator);
WaitForSettingsMenuToBeRefreshed();
// Clicking the "Downloads" option will set it as the folder of choice, but
// won't clear the custom folder.
CaptureModeSettingsTestApi test_api;
ClickOnView(test_api.GetDefaultDownloadsOption(), event_generator);
CaptureModeMenuGroup* save_to_menu_group = test_api.GetSaveToMenuGroup();
EXPECT_TRUE(save_to_menu_group->IsOptionChecked(kDownloadsFolder));
EXPECT_FALSE(save_to_menu_group->IsOptionChecked(kCustomFolder));
const auto default_downloads_folder =
controller->delegate_for_testing()->GetUserDefaultDownloadsFolder();
auto capture_folder = controller->GetCurrentCaptureFolder();
EXPECT_EQ(capture_folder.path, default_downloads_folder);
EXPECT_TRUE(capture_folder.is_default_downloads_folder);
EXPECT_EQ(custom_path, controller->GetCustomCaptureFolder());
// Clicking on the custom folder option will switch back to using it.
ClickOnView(test_api.GetCustomFolderOptionIfAny(), event_generator);
EXPECT_TRUE(save_to_menu_group->IsOptionChecked(kCustomFolder));
EXPECT_FALSE(save_to_menu_group->IsOptionChecked(kDownloadsFolder));
capture_folder = controller->GetCurrentCaptureFolder();
EXPECT_EQ(capture_folder.path, custom_path);
EXPECT_FALSE(capture_folder.is_default_downloads_folder);
}
// Tests that when there's no overlap betwwen capture label widget and settings
// widget, capture label widget is shown/hidden correctly after open/close the
// folder selection window.
TEST_P(CaptureModeSettingsTest, CaptureLabelViewNotOverlapsWithSettingsView) {
auto [sunfish_enabled, scanner_enabled] = GetParam();
if (sunfish_enabled || scanner_enabled) {
// This test crashes when either Sunfish or Scanner is enabled.
// TODO: b/381965299 - Fix these test failures.
GTEST_SKIP();
}
// Update the display size to make sure capture label widget will not
// overlap with settings widget
UpdateDisplay("800x600");
auto* controller = CaptureModeController::Get();
// Set the region at an area far away from where the settings menu shows.
controller->SetUserCaptureRegion(gfx::Rect(200, 200), /*by_user=*/true);
StartImageRegionCapture();
auto* event_generator = GetEventGenerator();
// Tests that the capture label widget doesn't overlap with settings widget.
// Both capture label widget and settings widget are visible.
views::Widget* capture_label_widget = GetCaptureModeLabelWidget();
ClickOnView(GetSettingsButton(), event_generator);
views::Widget* settings_widget = GetCaptureModeSettingsWidget();
EXPECT_FALSE(capture_label_widget->GetWindowBoundsInScreen().Intersects(
settings_widget->GetWindowBoundsInScreen()));
EXPECT_TRUE(capture_label_widget->IsVisible());
EXPECT_TRUE(settings_widget->IsVisible());
// Open folder selection window, check that both capture label widget and
// settings widget are invisible.
CaptureModeSettingsTestApi test_api;
auto* dialog_factory = FakeFolderSelectionDialogFactory::Get();
ClickOnView(test_api.GetSelectFolderMenuItem(), event_generator);
EXPECT_TRUE(IsFolderSelectionDialogShown());
EXPECT_FALSE(capture_label_widget->IsVisible());
EXPECT_FALSE(settings_widget->IsVisible());
// Now close folder selection window, check that capture label widget and
// settings widget become visible.
dialog_factory->CancelDialog();
EXPECT_FALSE(IsFolderSelectionDialogShown());
EXPECT_TRUE(capture_label_widget->IsVisible());
EXPECT_EQ(capture_label_widget->GetLayer()->GetTargetOpacity(), 1.f);
EXPECT_TRUE(settings_widget->IsVisible());
// Close settings widget. Capture label widget is visible.
ClickOnView(GetSettingsButton(), event_generator);
EXPECT_TRUE(capture_label_widget->IsVisible());
controller->Stop();
}
// Tests that when capture label widget overlaps with settings widget, capture
// label widget is shown/hidden correctly after open/close the folder selection
// window, open/close settings menu. Regression test for
// https://crbug.com/1279606.
TEST_P(CaptureModeSettingsTest, CaptureLabelViewOverlapsWithSettingsView) {
// Update display size to make capture label widget overlap with settings
// widget.
UpdateDisplay("1100x700");
auto* controller = StartImageRegionCapture();
auto* event_generator = GetEventGenerator();
// Tests that capture label widget overlaps with settings widget and is
// hidden after setting widget is shown.
auto* capture_label_widget = GetCaptureModeLabelWidget();
ClickOnView(GetSettingsButton(), event_generator);
auto* settings_widget = GetCaptureModeSettingsWidget();
EXPECT_TRUE(capture_label_widget->GetWindowBoundsInScreen().Intersects(
settings_widget->GetWindowBoundsInScreen()));
EXPECT_FALSE(GetCaptureModeLabelWidget()->IsVisible());
EXPECT_TRUE(settings_widget->IsVisible());
// Open folder selection window, capture label widget is invisible.
CaptureModeSettingsTestApi test_api;
auto* dialog_factory = FakeFolderSelectionDialogFactory::Get();
ClickOnView(test_api.GetSelectFolderMenuItem(), event_generator);
EXPECT_TRUE(IsFolderSelectionDialogShown());
EXPECT_FALSE(capture_label_widget->IsVisible());
// Close folder selection window, capture label widget is invisible.
dialog_factory->CancelDialog();
EXPECT_FALSE(IsFolderSelectionDialogShown());
EXPECT_FALSE(capture_label_widget->IsVisible());
// Tests that capture label widget is visible after settings widget is
// closed.
ClickOnView(GetSettingsButton(), event_generator);
EXPECT_TRUE(capture_label_widget->IsVisible());
EXPECT_EQ(capture_label_widget->GetLayer()->GetTargetOpacity(), 1.f);
controller->Stop();
}
TEST_P(CaptureModeSettingsTest, PressingEnterSelectsFocusedItem) {
auto* controller =
StartCaptureSession(CaptureModeSource::kRegion, CaptureModeType::kImage);
using FocusGroup = CaptureModeSessionFocusCycler::FocusGroup;
CaptureModeSessionTestApi session_test_api(
controller->capture_mode_session());
// Tab six times to focus on the settings button.
auto* event_generator = GetEventGenerator();
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/6);
EXPECT_EQ(FocusGroup::kSettingsClose,
session_test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, session_test_api.GetCurrentFocusIndex());
// Press the enter key to open the settings menu. The current focus group
// should be `kPendingSettings`.
SendKey(ui::VKEY_RETURN, event_generator);
ASSERT_TRUE(GetCaptureModeSettingsView());
EXPECT_EQ(FocusGroup::kPendingSettings,
session_test_api.GetCurrentFocusGroup());
// Tab once to enter focus into the settings menu.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE);
ASSERT_EQ(FocusGroup::kSettingsMenu, session_test_api.GetCurrentFocusGroup());
// Tab until focus reaches the `kAudioMicrophone` option.
CaptureModeSettingsTestApi settings_test_api;
auto* mic_option = settings_test_api.GetMicrophoneOption();
while (session_test_api.GetCurrentFocusedView()->GetView() != mic_option) {
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE);
}
CaptureModeMenuGroup* audio_input_menu_group =
settings_test_api.GetAudioInputMenuGroup();
EXPECT_TRUE(audio_input_menu_group->IsOptionChecked(kAudioOff));
EXPECT_FALSE(audio_input_menu_group->IsOptionChecked(kAudioMicrophone));
// Press the enter key, and now microphone should be on.
SendKey(ui::VKEY_RETURN, event_generator);
EXPECT_FALSE(audio_input_menu_group->IsOptionChecked(kAudioOff));
EXPECT_TRUE(audio_input_menu_group->IsOptionChecked(kAudioMicrophone));
}
// Tests the basic keyboard navigation functions for the settings menu.
TEST_P(CaptureModeSettingsTest, KeyboardNavigationForSettingsMenu) {
auto* controller =
StartCaptureSession(CaptureModeSource::kRegion, CaptureModeType::kImage);
using FocusGroup = CaptureModeSessionFocusCycler::FocusGroup;
CaptureModeSessionTestApi session_test_api(
controller->capture_mode_session());
// Tab six times to focus on the settings button.
auto* event_generator = GetEventGenerator();
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/6);
EXPECT_EQ(FocusGroup::kSettingsClose,
session_test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, session_test_api.GetCurrentFocusIndex());
// Enter space to open the settings menu. The current focus group should be
// `kPendingSettings`.
SendKey(ui::VKEY_SPACE, event_generator);
ASSERT_TRUE(GetCaptureModeSettingsView());
EXPECT_EQ(FocusGroup::kPendingSettings,
session_test_api.GetCurrentFocusGroup());
CaptureModeSettingsTestApi settings_test_api;
CaptureModeMenuGroup* audio_input_menu_group =
settings_test_api.GetAudioInputMenuGroup();
// Tab once to focus on the first item in the settings menu (`Audio input`
// header).
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(FocusGroup::kSettingsMenu, session_test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, session_test_api.GetCurrentFocusIndex());
// Tab once to enter focus into the settings menu.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE);
ASSERT_EQ(FocusGroup::kSettingsMenu, session_test_api.GetCurrentFocusGroup());
// Check that the `Off` option is the checked option not the `Microphone`.
EXPECT_TRUE(audio_input_menu_group->IsOptionChecked(kAudioOff));
EXPECT_FALSE(audio_input_menu_group->IsOptionChecked(kAudioMicrophone));
// Tab until focus reaches the `kAudioMicrophone` option.
auto* mic_option = settings_test_api.GetMicrophoneOption();
while (session_test_api.GetCurrentFocusedView()->GetView() != mic_option) {
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE);
}
// Enter space, and check that now the `Microphone` option is checked.
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_FALSE(audio_input_menu_group->IsOptionChecked(kAudioOff));
EXPECT_TRUE(audio_input_menu_group->IsOptionChecked(kAudioMicrophone));
Switch* toggle_button =
settings_test_api.GetDemoToolsMenuToggleButton()->toggle_button();
// The demo tools toggle button will be disabled by default.
EXPECT_FALSE(toggle_button->GetIsOn());
// Tab until focus reaches the demo tools toggle button and enter space to
// enable it.
while (session_test_api.GetCurrentFocusedView()->GetView() !=
toggle_button) {
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE);
}
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_TRUE(toggle_button->GetIsOn());
// Tab until focus reaches the `Select folder...` menu item.
auto* select_folder_option = settings_test_api.GetSelectFolderMenuItem();
while (session_test_api.GetCurrentFocusedView()->GetView() !=
select_folder_option) {
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE);
}
// Enter space to open the folder selection window.
SendKey(ui::VKEY_SPACE, event_generator);
auto* dialog_factory = FakeFolderSelectionDialogFactory::Get();
EXPECT_TRUE(IsFolderSelectionDialogShown());
// Close selection window.
dialog_factory->CancelDialog();
EXPECT_FALSE(IsFolderSelectionDialogShown());
// Now tab once to focus on the settings button and enter space on it to close
// the settings menu.
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(FocusGroup::kSettingsClose,
session_test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, session_test_api.GetCurrentFocusIndex());
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_FALSE(GetCaptureModeSettingsView());
}
// Tests that the disabled option in the settings menu will be skipped while
// tabbing through.
TEST_P(CaptureModeSettingsTest,
KeyboardNavigationForSettingsMenuWithDisabledOption) {
// Start a new session with a pre-configured unavailable custom folder.
auto* controller = CaptureModeController::Get();
const base::FilePath custom_folder(FILE_PATH_LITERAL("/home/random"));
controller->SetCustomCaptureFolder(custom_folder);
StartImageRegionCapture();
using FocusGroup = CaptureModeSessionFocusCycler::FocusGroup;
CaptureModeSessionTestApi session_test_api(
controller->capture_mode_session());
// Tab six times to focus the settings button and enter space to open the
// settings menu.
auto* event_generator = GetEventGenerator();
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, 6);
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_EQ(FocusGroup::kPendingSettings,
session_test_api.GetCurrentFocusGroup());
CaptureModeSettingsView* settings_menu = GetCaptureModeSettingsView();
ASSERT_TRUE(settings_menu);
// Since the custom folder is unavailable, the `kCustomFolder` should be
// disabled and won't be returned via
// `CaptureModeSettingsViews::GetHighlightableItems`.
CaptureModeSettingsTestApi settings_test_api;
auto* custom_folder_view = settings_test_api.GetCustomFolderOptionIfAny();
ASSERT_TRUE(custom_folder_view);
EXPECT_FALSE(custom_folder_view->GetEnabled());
std::vector<CaptureModeSessionFocusCycler::HighlightableView*>
highlightable_items = settings_menu->GetHighlightableItems();
EXPECT_FALSE(base::Contains(
highlightable_items, custom_folder_view,
&CaptureModeSessionFocusCycler::HighlightableView::GetView));
// Tab once to enter focus into the settings menu.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE);
ASSERT_EQ(FocusGroup::kSettingsMenu, session_test_api.GetCurrentFocusGroup());
// Tab until the focus is on the default `Downloads` option.
auto* downloads_option = settings_test_api.GetDefaultDownloadsOption();
ASSERT_TRUE(downloads_option);
while (session_test_api.GetCurrentFocusedView()->GetView() !=
downloads_option) {
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE);
}
// Tab once to check the disabled `kCustomFolder` option is skipped and now
// the `Select folder...` menu item gets focused.
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(session_test_api.GetCurrentFocusedView()->GetView(),
settings_test_api.GetSelectFolderMenuItem());
}
// Tests that selecting the default `Downloads` folder as the custom folder via
// keyboard navigation doesn't lead to a crash. Regression test for
// https://crbug.com/1269373.
TEST_P(CaptureModeSettingsTest,
KeyboardNavigationForRemovingCustomFolderOption) {
// Start a new session with a pre-configured custom folder.
auto* controller = CaptureModeController::Get();
const base::FilePath custom_folder(
CreateCustomFolderInUserDownloadsPath("test"));
controller->SetCustomCaptureFolder(custom_folder);
StartImageRegionCapture();
using FocusGroup = CaptureModeSessionFocusCycler::FocusGroup;
CaptureModeSessionTestApi test_api(controller->capture_mode_session());
// Tab six times to focus the settings button, then enter space to open the
// settings menu. Wait for the settings menu to be refreshed.
auto* event_generator = GetEventGenerator();
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, /*count=*/6);
SendKey(ui::VKEY_SPACE, event_generator);
WaitForSettingsMenuToBeRefreshed();
EXPECT_EQ(FocusGroup::kPendingSettings, test_api.GetCurrentFocusGroup());
CaptureModeSettingsView* settings_menu = GetCaptureModeSettingsView();
ASSERT_TRUE(settings_menu);
// Tab once to enter focus into the settings menu.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE);
ASSERT_EQ(FocusGroup::kSettingsMenu, test_api.GetCurrentFocusGroup());
// Tab until focus reaches the `Select folder...` menu item.
CaptureModeSettingsTestApi settings_test_api;
auto* select_folder_option = settings_test_api.GetSelectFolderMenuItem();
while (test_api.GetCurrentFocusedView()->GetView() != select_folder_option) {
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE);
}
// Enter space to open the folder selection window.
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_TRUE(IsFolderSelectionDialogShown());
// Select the default `Downloads` folder as the custom folder which will
// have custom folder option get removed.
auto* test_delegate = controller->delegate_for_testing();
const auto default_downloads_folder =
test_delegate->GetUserDefaultDownloadsFolder();
auto* dialog_factory = FakeFolderSelectionDialogFactory::Get();
dialog_factory->AcceptPath(default_downloads_folder);
// Press space to ensure the folder selection window can be opened after the
// custom folder is removed from the settings menu.
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_TRUE(IsFolderSelectionDialogShown());
dialog_factory->CancelDialog();
// Tab once to make sure there's no crash and the focus gets moved to
// settings button.
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(FocusGroup::kSettingsClose, test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, test_api.GetCurrentFocusIndex());
}
// Tests that first time selecting a custom folder via keyboard navigation.
// After the custom folder is selected, tabbing one more time will move focus
// from the settings menu to the settings button.
TEST_P(CaptureModeSettingsTest, KeyboardNavigationForAddingCustomFolderOption) {
auto* controller = CaptureModeController::Get();
StartImageRegionCapture();
using FocusGroup = CaptureModeSessionFocusCycler::FocusGroup;
CaptureModeSessionTestApi session_test_api(
controller->capture_mode_session());
// Tab six times to focus on the settings button, then enter space to open
// the settings menu.
auto* event_generator = GetEventGenerator();
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE, 6);
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_EQ(FocusGroup::kPendingSettings,
session_test_api.GetCurrentFocusGroup());
CaptureModeSettingsView* settings_menu = GetCaptureModeSettingsView();
ASSERT_TRUE(settings_menu);
// Tab once to enter focus into the settings menu.
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE);
ASSERT_EQ(FocusGroup::kSettingsMenu, session_test_api.GetCurrentFocusGroup());
// Tab until focus reaches the `Select folder...` menu item.
CaptureModeSettingsTestApi settings_test_api;
auto* select_folder_option = settings_test_api.GetSelectFolderMenuItem();
while (session_test_api.GetCurrentFocusedView()->GetView() !=
select_folder_option) {
SendKey(ui::VKEY_TAB, event_generator, ui::EF_NONE);
}
// Enter space to open the folder selection window.
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_TRUE(IsFolderSelectionDialogShown());
// Select the custom folder and wait for the settings menu to be refreshed.
// The custom folder option should be added to the settings menu and checked.
const base::FilePath custom_folder(
CreateCustomFolderInUserDownloadsPath("test"));
controller->SetCustomCaptureFolder(custom_folder);
auto* dialog_factory = FakeFolderSelectionDialogFactory::Get();
dialog_factory->AcceptPath(custom_folder);
WaitForSettingsMenuToBeRefreshed();
EXPECT_TRUE(settings_test_api.GetCustomFolderOptionIfAny());
// Press space to ensure the folder selection window can be opened after the
// custom folder is added to the settings menu.
SendKey(ui::VKEY_SPACE, event_generator);
EXPECT_TRUE(IsFolderSelectionDialogShown());
dialog_factory->CancelDialog();
// Tab once to make sure the focus gets moved to settings button.
SendKey(ui::VKEY_TAB, event_generator);
EXPECT_EQ(FocusGroup::kSettingsClose,
session_test_api.GetCurrentFocusGroup());
EXPECT_EQ(0u, session_test_api.GetCurrentFocusIndex());
}
// Tests the folder selection settings when it's recommended by policy.
TEST_P(CaptureModeSettingsTest, FolderRecommendedByPolicy) {
auto* controller = StartImageRegionCapture();
// Set the pref to recommended values.
const base::FilePath custom_folder(
CreateCustomFolderInUserDownloadsPath("test"));
auto* test_delegate =
static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());
test_delegate->set_policy_capture_path(
{custom_folder,
CaptureModeDelegate::CapturePathEnforcement::kRecommended});
// Open settings.
auto* event_generator = GetEventGenerator();
ClickOnView(GetSettingsButton(), event_generator);
std::unique_ptr<CaptureModeSettingsTestApi> test_api =
std::make_unique<CaptureModeSettingsTestApi>();
WaitForSettingsMenuToBeRefreshed();
// Custom folder is set, but Downloads option and select folder is enabled.
EXPECT_FALSE(controller->IsCustomFolderManagedByPolicy());
CaptureModeMenuGroup* save_to_menu_group = test_api->GetSaveToMenuGroup();
EXPECT_FALSE(save_to_menu_group->IsManagedByPolicy());
EXPECT_TRUE(test_api->GetCustomFolderOptionIfAny()->GetEnabled());
EXPECT_TRUE(save_to_menu_group->IsOptionChecked(kCustomFolder));
EXPECT_TRUE(test_api->GetDefaultDownloadsOption()->GetEnabled());
EXPECT_FALSE(save_to_menu_group->IsOptionChecked(kDownloadsFolder));
EXPECT_TRUE(test_api->GetSelectFolderMenuItem()->GetEnabled());
}
// Tests the folder selection settings when it's enforced by policy.
TEST_P(CaptureModeSettingsTest, FolderSetByPolicy) {
auto* controller = StartImageRegionCapture();
// Set the pref to managed values.
const base::FilePath custom_folder(
CreateCustomFolderInUserDownloadsPath("test"));
auto* test_delegate =
static_cast<TestCaptureModeDelegate*>(controller->delegate_for_testing());
test_delegate->set_policy_capture_path(
{custom_folder, CaptureModeDelegate::CapturePathEnforcement::kManaged});
// Open settings.
auto* event_generator = GetEventGenerator();
ClickOnView(GetSettingsButton(), event_generator);
std::unique_ptr<CaptureModeSettingsTestApi> test_api =
std::make_unique<CaptureModeSettingsTestApi>();
WaitForSettingsMenuToBeRefreshed();
// Custom folder is set, but Downloads option and select folder are not
// enabled.
EXPECT_TRUE(controller->IsCustomFolderManagedByPolicy());
CaptureModeMenuGroup* save_to_menu_group = test_api->GetSaveToMenuGroup();
EXPECT_TRUE(save_to_menu_group->IsManagedByPolicy());
EXPECT_TRUE(test_api->GetCustomFolderOptionIfAny()->GetEnabled());
EXPECT_TRUE(save_to_menu_group->IsOptionChecked(kCustomFolder));
EXPECT_FALSE(test_api->GetDefaultDownloadsOption()->GetEnabled());
EXPECT_FALSE(save_to_menu_group->IsOptionChecked(kDownloadsFolder));
EXPECT_FALSE(test_api->GetSelectFolderMenuItem()->GetEnabled());
}
// -----------------------------------------------------------------------------
// CaptureModeHistogramTest:
// Test fixture to verify screen capture histograms depending on the test
// param (true for tablet mode, false for clamshell mode).
class CaptureModeHistogramTest
: public CaptureModeSettingsTestBase,
public ::testing::WithParamInterface<std::tuple<bool, bool, bool>> {
public:
CaptureModeHistogramTest() = default;
~CaptureModeHistogramTest() override = default;
// CaptureModeSettingsTestBase:
void SetUp() override {
auto [unused_is_tablet, sunfish_enabled, scanner_enabled] = GetParam();
InitFeatures(sunfish_enabled, scanner_enabled);
CaptureModeSettingsTestBase::SetUp();
if (std::get<0>(GetParam())) {
SwitchToTabletMode();
}
}
void StartSessionForVideo() {
StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kVideo);
}
void StartRecording() { CaptureModeTestApi().PerformCapture(); }
void StopRecording() { CaptureModeTestApi().StopVideoRecording(); }
void OpenView(const views::View* view,
ui::test::EventGenerator* event_generator) {
if (std::get<0>(GetParam())) {
TouchOnView(view, event_generator);
} else {
ClickOnView(view, event_generator);
}
}
};
// Tests that metrics are recorded properly for various capture mode entry
// points.
TEST_P(CaptureModeHistogramTest, CaptureModeEntryPointHistograms) {
constexpr char kHistogramNameBase[] = "EntryPoint";
const std::string histogram_name = BuildHistogramName(
kHistogramNameBase, /*behavior=*/nullptr, /*append_ui_mode_suffix=*/true);
base::HistogramTester histogram_tester;
auto* controller = CaptureModeController::Get();
controller->Start(CaptureModeEntryType::kAccelTakeWindowScreenshot);
histogram_tester.ExpectBucketCount(
histogram_name, CaptureModeEntryType::kAccelTakeWindowScreenshot, 1);
controller->Stop();
controller->Start(CaptureModeEntryType::kAccelTakePartialScreenshot);
histogram_tester.ExpectBucketCount(
histogram_name, CaptureModeEntryType::kAccelTakePartialScreenshot, 1);
controller->Stop();
controller->Start(CaptureModeEntryType::kQuickSettings);
histogram_tester.ExpectBucketCount(histogram_name,
CaptureModeEntryType::kQuickSettings, 1);
controller->Stop();
controller->Start(CaptureModeEntryType::kStylusPalette);
histogram_tester.ExpectBucketCount(histogram_name,
CaptureModeEntryType::kStylusPalette, 1);
controller->Stop();
std::unique_ptr<aura::Window> window(
CreateTestWindow(gfx::Rect(10, 20, 700, 500)));
controller->CaptureScreenshotOfGivenWindow(window.get());
WaitForCaptureFileToBeSaved();
histogram_tester.ExpectBucketCount(
histogram_name, CaptureModeEntryType::kCaptureGivenWindow, 1);
// Check total counts for each histogram to ensure calls aren't counted in
// multiple buckets.
histogram_tester.ExpectTotalCount(histogram_name, 5);
histogram_tester.ExpectTotalCount(histogram_name, 5);
}
// Tests that metrics are recorded properly for capture mode configurations when
// taking a screenshot.
TEST_P(CaptureModeHistogramTest, ScreenshotConfigurationHistogram) {
constexpr char kHistogramNameBase[] = "CaptureConfiguration";
const std::string histogram_name = BuildHistogramName(
kHistogramNameBase, /*behavior=*/nullptr, /*append_ui_mode_suffix=*/true);
base::HistogramTester histogram_tester;
// Use a set display size as we will be choosing points in this test.
UpdateDisplay("800x700");
// Create a window for window captures later.
std::unique_ptr<aura::Window> window(
CreateTestWindow(gfx::Rect(600, 600, 100, 100)));
// Perform a fullscreen screenshot.
auto* controller = StartCaptureSession(CaptureModeSource::kFullscreen,
CaptureModeType::kImage);
controller->PerformCapture();
histogram_tester.ExpectBucketCount(
histogram_name, CaptureModeConfiguration::kFullscreenScreenshot, 1);
// Perform a region screenshot.
controller =
StartCaptureSession(CaptureModeSource::kRegion, CaptureModeType::kImage);
const gfx::Rect capture_region(200, 200, 400, 400);
SelectRegion(capture_region);
controller->PerformCapture();
histogram_tester.ExpectBucketCount(
histogram_name, CaptureModeConfiguration::kRegionScreenshot, 1);
// Perform a window screenshot.
controller =
StartCaptureSession(CaptureModeSource::kWindow, CaptureModeType::kImage);
auto* event_generator = GetEventGenerator();
event_generator->MoveMouseToCenterOf(window.get());
EXPECT_EQ(window.get(),
controller->capture_mode_session()->GetSelectedWindow());
controller->PerformCapture();
histogram_tester.ExpectBucketCount(
histogram_name, CaptureModeConfiguration::kWindowScreenshot, 1);
}
TEST_P(CaptureModeHistogramTest, VideoRecordingAudioVideoMetrics) {
constexpr char kHistogramNameBase[] = "AudioRecordingMode";
const std::string histogram_name = BuildHistogramName(
kHistogramNameBase, /*behavior=*/nullptr, /*append_ui_mode_suffix=*/true);
base::HistogramTester histogram_tester;
histogram_tester.ExpectBucketCount(histogram_name, AudioRecordingMode::kOff,
0);
histogram_tester.ExpectBucketCount(histogram_name,
AudioRecordingMode::kMicrophone, 0);
histogram_tester.ExpectBucketCount(histogram_name,
AudioRecordingMode::kSystem, 0);
histogram_tester.ExpectBucketCount(
histogram_name, AudioRecordingMode::kSystemAndMicrophone, 0);
// Perform a video recording with audio off. `kOff` should be recorded.
StartSessionForVideo();
CaptureModeTestApi().SetAudioRecordingMode(AudioRecordingMode::kOff);
StartRecording();
histogram_tester.ExpectBucketCount(histogram_name, AudioRecordingMode::kOff,
1);
histogram_tester.ExpectBucketCount(histogram_name,
AudioRecordingMode::kMicrophone, 0);
histogram_tester.ExpectBucketCount(histogram_name,
AudioRecordingMode::kSystem, 0);
histogram_tester.ExpectBucketCount(
histogram_name, AudioRecordingMode::kSystemAndMicrophone, 0);
WaitForSeconds(1);
StopRecording();
WaitForCaptureFileToBeSaved();
histogram_tester.ExpectTotalCount(
BuildHistogramName("ScreenRecordingFileSize", /*behavior=*/nullptr,
/*append_ui_mode_suffix=*/true),
/*expected_count=*/1);
// Perform a video recording with microphone audio recording on. `kMicrophone`
// should be recorded.
StartSessionForVideo();
CaptureModeTestApi().SetAudioRecordingMode(AudioRecordingMode::kMicrophone);
StartRecording();
histogram_tester.ExpectBucketCount(histogram_name, AudioRecordingMode::kOff,
1);
histogram_tester.ExpectBucketCount(histogram_name,
AudioRecordingMode::kMicrophone, 1);
histogram_tester.ExpectBucketCount(histogram_name,
AudioRecordingMode::kSystem, 0);
histogram_tester.ExpectBucketCount(
histogram_name, AudioRecordingMode::kSystemAndMicrophone, 0);
StopRecording();
}
TEST_P(CaptureModeHistogramTest, CaptureModeSwitchToDefaultReasonMetric) {
constexpr char kHistogramNameBase[] = "SwitchToDefaultReason";
const std::string histogram_name = BuildHistogramName(
kHistogramNameBase, /*behavior=*/nullptr, /*append_ui_mode_suffix=*/true);
base::HistogramTester histogram_tester;
auto* controller = CaptureModeController::Get();
const auto downloads_folder =
controller->delegate_for_testing()->GetUserDefaultDownloadsFolder();
const base::FilePath non_available_custom_folder(
FILE_PATH_LITERAL("/home/test"));
const base::FilePath available_custom_folder =
CreateCustomFolderInUserDownloadsPath("test");
histogram_tester.ExpectBucketCount(
histogram_name, CaptureModeSwitchToDefaultReason::kFolderUnavailable, 0);
histogram_tester.ExpectBucketCount(
histogram_name,
CaptureModeSwitchToDefaultReason::kUserSelectedFromFolderSelectionDialog,
0);
histogram_tester.ExpectBucketCount(
histogram_name,
CaptureModeSwitchToDefaultReason::kUserSelectedFromSettingsMenu, 0);
StartImageRegionCapture();
// Set the custom folder to an unavailable folder the switch to default
// reason should be recorded as `kFolderUnavailable`.
controller->SetCustomCaptureFolder(non_available_custom_folder);
EXPECT_EQ(controller->GetCurrentCaptureFolder().path,
non_available_custom_folder);
auto* event_generator = GetEventGenerator();
OpenView(GetSettingsButton(), event_generator);
WaitForSettingsMenuToBeRefreshed();
CaptureModeSettingsTestApi test_api;
CaptureModeMenuGroup* save_to_menu_group = test_api.GetSaveToMenuGroup();
EXPECT_TRUE(save_to_menu_group->IsOptionChecked(kDownloadsFolder));
histogram_tester.ExpectBucketCount(
histogram_name, CaptureModeSwitchToDefaultReason::kFolderUnavailable, 1);
// Select the save-to location to default downloads folder from folder
// selection dialog and the switch to default reason should be recorded as
// kUserSelectedFromSettingsMenu.
controller->SetCustomCaptureFolder(available_custom_folder);
EXPECT_EQ(controller->GetCurrentCaptureFolder().path,
available_custom_folder);
OpenView(test_api.GetSelectFolderMenuItem(), event_generator);
EXPECT_TRUE(IsFolderSelectionDialogShown());
auto* dialog_factory = FakeFolderSelectionDialogFactory::Get();
dialog_factory->AcceptPath(downloads_folder);
EXPECT_TRUE(save_to_menu_group->IsOptionChecked(kDownloadsFolder));
histogram_tester.ExpectBucketCount(
histogram_name,
CaptureModeSwitchToDefaultReason::kUserSelectedFromFolderSelectionDialog,
1);
// Select the save-to location to default downloads folder from settings
// menu and the switch to default reason should be recorded as
// `kUserSelectedFromFolderSelectionDialog`.
controller->SetCustomCaptureFolder(available_custom_folder);
EXPECT_EQ(controller->GetCurrentCaptureFolder().path,
available_custom_folder);
OpenView(test_api.GetDefaultDownloadsOption(), event_generator);
EXPECT_TRUE(save_to_menu_group->IsOptionChecked(kDownloadsFolder));
histogram_tester.ExpectBucketCount(
histogram_name,
CaptureModeSwitchToDefaultReason::kUserSelectedFromSettingsMenu, 1);
}
INSTANTIATE_TEST_SUITE_P(
All,
CaptureModeHistogramTest,
testing::Combine(testing::Bool(), testing::Bool(), testing::Bool()),
[](const testing::TestParamInfo<CaptureModeHistogramTest::ParamType>&
info) {
bool is_tablet_mode = std::get<0>(info.param);
bool sunfish_enabled = std::get<1>(info.param);
bool scanner_enabled = std::get<2>(info.param);
std::string test_name = is_tablet_mode ? "Tablet" : "Clamshell";
test_name += SunfishScannerTestName(sunfish_enabled, scanner_enabled);
return test_name;
});
} // namespace ash
|