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
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller.h"
#include <stddef.h>
#include <algorithm>
#include <initializer_list>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/web_app_id_constants.h"
#include "ash/display/display_configuration_controller.h"
#include "ash/multi_user/multi_user_window_manager_impl.h"
#include "ash/public/cpp/app_list/internal_app_id_constants.h"
#include "ash/public/cpp/multi_user_window_manager.h"
#include "ash/public/cpp/shelf_item.h"
#include "ash/public/cpp/shelf_item_delegate.h"
#include "ash/public/cpp/shelf_model.h"
#include "ash/public/cpp/shelf_prefs.h"
#include "ash/public/cpp/shelf_types.h"
#include "ash/public/cpp/window_properties.h"
#include "ash/shelf/shelf_application_menu_model.h"
#include "ash/webui/system_apps/public/system_web_app_type.h"
#include "base/check.h"
#include "base/check_deref.h"
#include "base/check_op.h"
#include "base/command_line.h"
#include "base/containers/contains.h"
#include "base/containers/flat_map.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/functional/callback_helpers.h"
#include "base/location.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram.h"
#include "base/metrics/statistics_recorder.h"
#include "base/notreached.h"
#include "base/run_loop.h"
#include "base/scoped_observation.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/test_future.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/apps/app_service/app_icon/app_icon_factory.h"
#include "chrome/browser/apps/app_service/app_icon/app_icon_util.h"
#include "chrome/browser/apps/app_service/app_registry_cache_waiter.h"
#include "chrome/browser/apps/app_service/app_service_proxy.h"
#include "chrome/browser/apps/app_service/app_service_proxy_factory.h"
#include "chrome/browser/apps/app_service/app_service_test.h"
#include "chrome/browser/apps/app_service/policy_util.h"
#include "chrome/browser/apps/app_service/promise_apps/promise_app.h"
#include "chrome/browser/apps/app_service/promise_apps/promise_app_metrics.h"
#include "chrome/browser/apps/app_service/promise_apps/promise_app_registry_cache.h"
#include "chrome/browser/apps/app_service/promise_apps/promise_app_service.h"
#include "chrome/browser/ash/app_list/app_list_controller_delegate.h"
#include "chrome/browser/ash/app_list/app_list_syncable_service.h"
#include "chrome/browser/ash/app_list/app_list_syncable_service_factory.h"
#include "chrome/browser/ash/app_list/app_list_test_util.h"
#include "chrome/browser/ash/app_list/app_service/app_service_app_icon_loader.h"
#include "chrome/browser/ash/app_list/app_service/app_service_promise_app_icon_loader.h"
#include "chrome/browser/ash/app_list/arc/arc_app_icon.h"
#include "chrome/browser/ash/app_list/arc/arc_app_list_prefs.h"
#include "chrome/browser/ash/app_list/arc/arc_app_test.h"
#include "chrome/browser/ash/app_list/arc/arc_app_utils.h"
#include "chrome/browser/ash/app_list/arc/arc_default_app_list.h"
#include "chrome/browser/ash/apps/apk_web_app_service.h"
#include "chrome/browser/ash/arc/arc_util.h"
#include "chrome/browser/ash/arc/session/arc_session_manager.h"
#include "chrome/browser/ash/crostini/crostini_test_helper.h"
#include "chrome/browser/ash/crostini/crostini_util.h"
#include "chrome/browser/ash/eche_app/app_id.h"
#include "chrome/browser/ash/login/demo_mode/demo_mode_test_helper.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/ash/system_web_apps/apps/camera_app/camera_system_web_app_info.h"
#include "chrome/browser/ash/system_web_apps/apps/os_flags_system_web_app_info.h"
#include "chrome/browser/ash/system_web_apps/system_web_app_manager.h"
#include "chrome/browser/ash/system_web_apps/test_support/test_system_web_app_manager.h"
#include "chrome/browser/ash/wallpaper_handlers/wallpaper_fetcher_delegate.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/media/router/media_router_feature.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/ui/app_icon_loader.h"
#include "chrome/browser/ui/apps/chrome_app_delegate.h"
#include "chrome/browser/ui/ash/multi_user/multi_profile_support.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_helper.h"
#include "chrome/browser/ui/ash/session/session_controller_client_impl.h"
#include "chrome/browser/ui/ash/shelf/app_service/app_service_app_window_shelf_item_controller.h"
#include "chrome/browser/ui/ash/shelf/app_window_shelf_controller.h"
#include "chrome/browser/ui/ash/shelf/app_window_shelf_item_controller.h"
#include "chrome/browser/ui/ash/shelf/arc_app_window.h"
#include "chrome/browser/ui/ash/shelf/browser_status_monitor.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller_test_util.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_controller_util.h"
#include "chrome/browser/ui/ash/shelf/chrome_shelf_prefs.h"
#include "chrome/browser/ui/ash/shelf/shelf_controller_helper.h"
#include "chrome/browser/ui/ash/shelf/shelf_spinner_controller.h"
#include "chrome/browser/ui/ash/shelf/shelf_spinner_item_controller.h"
#include "chrome/browser/ui/ash/wallpaper/wallpaper_controller_client_impl.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/web_applications/test/isolated_web_app_test_utils.h"
#include "chrome/browser/ui/web_applications/test/web_app_browsertest_util.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_url_info.h"
#include "chrome/browser/web_applications/isolated_web_apps/test/isolated_web_app_builder.h"
#include "chrome/browser/web_applications/policy/app_service_web_app_policy.h"
#include "chrome/browser/web_applications/policy/web_app_policy_manager.h"
#include "chrome/browser/web_applications/preinstalled_web_app_manager.h"
#include "chrome/browser/web_applications/test/fake_web_app_provider.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_helpers.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "chrome/browser/web_applications/web_app_utils.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/test_browser_window_aura.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "chromeos/ash/components/browser_context_helper/annotated_account_id.h"
#include "chromeos/ash/components/dbus/concierge/concierge_client.h"
#include "chromeos/ash/components/file_manager/app_id.h"
#include "chromeos/ash/experiences/arc/app/arc_app_constants.h"
#include "chromeos/ash/experiences/arc/arc_prefs.h"
#include "chromeos/ash/experiences/arc/metrics/arc_metrics_constants.h"
#include "chromeos/ash/experiences/arc/mojom/app.mojom.h"
#include "chromeos/ash/experiences/arc/mojom/compatibility_mode.mojom.h"
#include "chromeos/ash/experiences/arc/test/arc_util_test_support.h"
#include "chromeos/ash/experiences/arc/test/fake_app_instance.h"
#include "chromeos/ash/experiences/system_web_apps/types/system_web_app_delegate.h"
#include "chromeos/ash/experiences/system_web_apps/types/system_web_app_delegate_map.h"
#include "components/account_id/account_id.h"
#include "components/app_constants/constants.h"
#include "components/exo/shell_surface_util.h"
#include "components/keep_alive_registry/scoped_keep_alive.h"
#include "components/prefs/pref_service.h"
#include "components/services/app_service/public/cpp/app_registry_cache.h"
#include "components/services/app_service/public/cpp/app_types.h"
#include "components/services/app_service/public/cpp/app_update.h"
#include "components/services/app_service/public/cpp/instance.h"
#include "components/services/app_service/public/cpp/instance_registry.h"
#include "components/services/app_service/public/cpp/package_id.h"
#include "components/services/app_service/public/cpp/stub_icon_loader.h"
#include "components/services/app_service/public/cpp/types_util.h"
#include "components/sync/base/data_type.h"
#include "components/sync/model/sync_change.h"
#include "components/sync/protocol/app_list_specifics.pb.h"
#include "components/sync/protocol/entity_specifics.pb.h"
#include "components/sync/service/sync_service.h"
#include "components/sync/service/sync_user_settings.h"
#include "components/sync/test/fake_sync_change_processor.h"
#include "components/sync/test/test_sync_service.h"
#include "components/sync_preferences/pref_model_associator.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "components/user_manager/test_helper.h"
#include "components/viz/test/test_gpu_service_holder.h"
#include "components/webapps/browser/installable/installable_metrics.h"
#include "components/webapps/common/web_app_id.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/web_contents_tester.h"
#include "extensions/browser/app_window/app_window_contents.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "extensions/browser/app_window/native_app_window.h"
#include "extensions/browser/extension_registrar.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/grit/extensions_browser_resources.h"
#include "services/network/test/test_network_connection_tracker.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/client/window_parenting_client.h"
#include "ui/aura/window.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/menu_model.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_features.h"
#include "ui/display/display.h"
#include "ui/display/display_switches.h"
#include "ui/display/screen.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/event_constants.h"
#include "ui/events/types/event_type.h"
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/image/image_skia_rep.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "ui/gfx/skia_util.h"
#include "ui/views/widget/widget.h"
#include "url/gurl.h"
namespace content {
class BrowserContext;
} // namespace content
using base::ASCIIToUTF16;
using extensions::Extension;
using extensions::UnloadedExtensionReason;
using extensions::mojom::ManifestLocation;
namespace {
constexpr char kGmailUrl[] = "https://mail.google.com/mail/u";
constexpr char kGmailLaunchURL[] = "https://mail.google.com/mail/ca";
constexpr char kLaunchURL[] = "https://foo.example/";
// An extension prefix.
constexpr char kCrxAppPrefix[] = "_crx_";
// Dummy app id is used to put at least one pin record to prevent initializing
// pin model with preinstalled apps that can affect some tests.
constexpr char kDummyAppId[] = "dummyappid_dummyappid_dummyappid";
constexpr int kSizeInDip = extension_misc::EXTENSION_ICON_MEDIUM;
std::unique_ptr<KeyedService> BuildTestSyncService(
content::BrowserContext* context) {
return std::make_unique<syncer::TestSyncService>();
}
std::vector<arc::mojom::AppInfoPtr> GetArcSettingsAppInfo() {
std::vector<arc::mojom::AppInfoPtr> apps;
arc::mojom::AppInfoPtr app(arc::mojom::AppInfo::New());
app->name = "settings";
app->package_name = "com.android.settings";
app->activity = "com.android.settings.Settings";
app->sticky = true;
apps.push_back(std::move(app));
return apps;
}
int GetPrimaryDisplayId() {
return display::Screen::GetScreen()->GetPrimaryDisplay().id();
}
bool ValidateImageIsFullyLoaded(const gfx::ImageSkia& image) {
if (kSizeInDip != image.width() || kSizeInDip != image.height()) {
return false;
}
const std::vector<ui::ResourceScaleFactor>& scale_factors =
ui::GetSupportedResourceScaleFactors();
for (const auto scale_factor : scale_factors) {
const float scale = ui::GetScaleForResourceScaleFactor(scale_factor);
if (!image.HasRepresentation(scale)) {
return false;
}
const gfx::ImageSkiaRep& representation = image.GetRepresentation(scale);
if (representation.is_null() ||
representation.pixel_width() != base::ClampCeil(kSizeInDip * scale) ||
representation.pixel_height() != base::ClampCeil(kSizeInDip * scale)) {
return false;
}
}
return true;
}
// Test implementation of AppIconLoader.
class TestAppIconLoaderImpl : public AppIconLoader {
public:
TestAppIconLoaderImpl() = default;
TestAppIconLoaderImpl(const TestAppIconLoaderImpl&) = delete;
TestAppIconLoaderImpl& operator=(const TestAppIconLoaderImpl&) = delete;
~TestAppIconLoaderImpl() override = default;
void AddSupportedApp(const std::string& id) { supported_apps_.insert(id); }
// AppIconLoader implementation:
bool CanLoadImageForApp(const std::string& id) override {
return supported_apps_.find(id) != supported_apps_.end();
}
void FetchImage(const std::string& id) override { ++fetch_count_; }
void ClearImage(const std::string& id) override { ++clear_count_; }
void UpdateImage(const std::string& id) override {}
int fetch_count() const { return fetch_count_; }
int clear_count() const { return clear_count_; }
private:
int fetch_count_ = 0;
int clear_count_ = 0;
std::set<std::string> supported_apps_;
};
// Fake AppServiceAppIconLoader to wait for icons loaded.
class FakeAppServiceAppIconLoader : public AppServiceAppIconLoader {
public:
FakeAppServiceAppIconLoader(Profile* profile,
int resource_size_in_dip,
AppIconLoaderDelegate* delegate)
: AppServiceAppIconLoader(profile, resource_size_in_dip, delegate) {}
void WaitForIconLoadded(
const std::vector<std::string>& expected_icon_loaded_app_ids) {
bool icon_loaded = true;
for (const auto& app_id : expected_icon_loaded_app_ids) {
if (!base::Contains(icon_loaded_app_ids_, app_id)) {
icon_loaded = false;
break;
}
}
if (icon_loaded) {
return;
}
expected_icon_loaded_app_ids_ = expected_icon_loaded_app_ids;
base::RunLoop run_loop;
icon_loaded_callback_ = run_loop.QuitClosure();
run_loop.Run();
}
private:
// Callback invoked when the icon is loaded.
void OnLoadIcon(const std::string& app_id,
apps::IconValuePtr icon_value) override {
AppServiceAppIconLoader::OnLoadIcon(app_id, std::move(icon_value));
icon_loaded_app_ids_.insert(app_id);
bool icon_loaded = true;
for (const auto& id : expected_icon_loaded_app_ids_) {
if (!base::Contains(icon_loaded_app_ids_, id)) {
icon_loaded = false;
break;
}
}
if (icon_loaded && !icon_loaded_callback_.is_null()) {
std::move(icon_loaded_callback_).Run();
}
}
base::OnceClosure icon_loaded_callback_;
std::set<std::string> icon_loaded_app_ids_;
std::vector<std::string> expected_icon_loaded_app_ids_;
};
// Test implementation of ShelfControllerHelper.
class TestShelfControllerHelper : public ShelfControllerHelper {
public:
TestShelfControllerHelper() : ShelfControllerHelper(nullptr) {}
explicit TestShelfControllerHelper(Profile* profile)
: ShelfControllerHelper(profile) {}
TestShelfControllerHelper(const TestShelfControllerHelper&) = delete;
TestShelfControllerHelper& operator=(const TestShelfControllerHelper&) =
delete;
~TestShelfControllerHelper() override = default;
// Sets the id for the specified tab.
void SetAppID(content::WebContents* tab, const std::string& id) {
tab_id_map_[tab] = id;
}
// Returns true if there is an id registered for |tab|.
bool HasAppID(content::WebContents* tab) const {
return tab_id_map_.find(tab) != tab_id_map_.end();
}
// ShelfControllerHelper:
std::string GetAppID(content::WebContents* tab) override {
return tab_id_map_.find(tab) != tab_id_map_.end() ? tab_id_map_[tab]
: std::string();
}
bool IsValidIDForCurrentUser(const std::string& id) const override {
for (TabToStringMap::const_iterator i = tab_id_map_.begin();
i != tab_id_map_.end(); ++i) {
if (i->second == id) {
return true;
}
}
return false;
}
ArcAppListPrefs* GetArcAppListPrefs() const override { return nullptr; }
private:
typedef std::map<content::WebContents*, std::string> TabToStringMap;
TabToStringMap tab_id_map_;
};
// Test implementation of a V2 app shelf item controller.
class TestV2AppShelfItemController : public ash::ShelfItemDelegate {
public:
explicit TestV2AppShelfItemController(const std::string& app_id)
: ash::ShelfItemDelegate(ash::ShelfID(app_id)) {}
TestV2AppShelfItemController(const TestV2AppShelfItemController&) = delete;
TestV2AppShelfItemController& operator=(const TestV2AppShelfItemController&) =
delete;
~TestV2AppShelfItemController() override = default;
// Override for ash::ShelfItemDelegate:
void ItemSelected(std::unique_ptr<ui::Event> event,
int64_t display_id,
ash::ShelfLaunchSource source,
ItemSelectedCallback callback,
const ItemFilterPredicate& filter_predicate) override {
std::move(callback).Run(ash::SHELF_ACTION_WINDOW_ACTIVATED, {});
}
void ExecuteCommand(bool, int64_t, int32_t, int64_t) override {}
void Close() override {}
};
// Simulates selection of the shelf item.
void SelectItem(ash::ShelfItemDelegate* delegate) {
std::unique_ptr<ui::Event> event = std::make_unique<ui::MouseEvent>(
ui::EventType::kMousePressed, gfx::Point(), gfx::Point(),
ui::EventTimeForNow(), ui::EF_NONE, 0);
delegate->ItemSelected(std::move(event), display::kInvalidDisplayId,
ash::LAUNCH_FROM_UNKNOWN, base::DoNothing(),
base::NullCallback());
}
bool IsWindowOnDesktopOfUser(aura::Window* window,
const AccountId& account_id) {
return MultiUserWindowManagerHelper::GetInstance()->IsWindowOnDesktopOfUser(
window, account_id);
}
void UpdateAppRegistryCache(Profile* profile,
const std::string& app_id,
bool block,
bool pause,
std::optional<bool> show_in_shelf) {
std::vector<apps::AppPtr> apps;
apps::AppPtr app =
std::make_unique<apps::App>(apps::AppType::kChromeApp, app_id);
app->app_id = app_id;
if (block) {
app->readiness = apps::Readiness::kDisabledByPolicy;
} else {
app->readiness = apps::Readiness::kReady;
}
if (pause) {
app->paused = true;
} else {
app->paused = false;
}
if (show_in_shelf.has_value()) {
app->show_in_shelf = show_in_shelf;
}
apps.push_back(std::move(app));
apps::AppServiceProxyFactory::GetForProfile(profile)->OnApps(
std::move(apps), apps::AppType::kChromeApp,
false /* should_notify_initialized */);
}
} // namespace
class ChromeShelfControllerTestBase : public BrowserWithTestWindowTest,
public apps::AppRegistryCache::Observer {
protected:
ChromeShelfControllerTestBase()
: BrowserWithTestWindowTest(Browser::TYPE_NORMAL),
skip_preinstalled_web_app_startup_(
web_app::PreinstalledWebAppManager::SkipStartupForTesting()) {}
void SetUp() override {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
command_line->AppendSwitch(switches::kUseFirstDisplayAsInternal);
// Prevent preinstalled apps from installing so these tests can control when
// they are installed.
command_line->AppendSwitch(switches::kDisableDefaultApps);
ash::ConciergeClient::InitializeFake(/*fake_cicerone_client=*/nullptr);
app_list::AppListSyncableServiceFactory::SetUseInTesting(true);
BrowserWithTestWindowTest::SetUp();
// WallpaperControllerClientImpl should be created before Profile
// instantiation happening in BrowserWithTestWindowTest::SetUp().
// However, it should run after more global object set up, such as
// local_state's creation, which is also done together in
// BrowserWithTestWindowTest::SetUp(). Unfortunately, there's no
// clean way to handle such a case, and at this moment, this reordering
// fortunately, does not seem to impact to the real testing behavior
// we workaround it by instantiating WallpaperControllerClientImpl
// after profile creation.
wallpaper_controller_client_ = std::make_unique<
WallpaperControllerClientImpl>(
CHECK_DEREF(TestingBrowserProcess::GetGlobal()->local_state()),
std::make_unique<wallpaper_handlers::WallpaperFetcherDelegateImpl>());
wallpaper_controller_client_->Init();
model_ = std::make_unique<ash::ShelfModel>();
base::Value::Dict manifest;
manifest.SetByDottedPath(extensions::manifest_keys::kName,
"launcher controller test extension");
manifest.SetByDottedPath(extensions::manifest_keys::kVersion, "1");
manifest.SetByDottedPath(extensions::manifest_keys::kManifestVersion, 2);
manifest.SetByDottedPath(extensions::manifest_keys::kDescription,
"for testing pinned apps");
// AppService checks the app's type. So set the
// manifest_keys::kLaunchWebURL, so that the extension can get the type
// from manifest value, and then AppService can get the extension's type.
manifest.SetByDottedPath(extensions::manifest_keys::kLaunchWebURL,
kLaunchURL);
base::Value::Dict manifest_platform_app;
manifest_platform_app.SetByDottedPath(
extensions::manifest_keys::kName,
"launcher controller test platform app");
manifest_platform_app.SetByDottedPath(extensions::manifest_keys::kVersion,
"1");
manifest_platform_app.SetByDottedPath(
extensions::manifest_keys::kDescription,
"for testing pinned platform apps");
base::Value::List scripts;
scripts.Append("main.js");
manifest_platform_app.SetByDottedPath(
extensions::manifest_keys::kPlatformAppBackgroundScripts,
std::move(scripts));
SyncServiceFactory::GetInstance()->SetTestingFactory(
profile(), base::BindRepeating(&BuildTestSyncService));
extensions::TestExtensionSystem* extension_system(
static_cast<extensions::TestExtensionSystem*>(
extensions::ExtensionSystem::Get(profile())));
extension_service_ = extension_system->CreateExtensionService(
base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
extension_service_->Init();
DCHECK(profile());
extension_registrar_ = extensions::ExtensionRegistrar::Get(profile());
extension_registry_ = extensions::ExtensionRegistry::Get(profile());
app_service_test_.SetUp(profile());
app_registry_cache_observer_.Observe(
&(apps::AppServiceProxyFactory::GetForProfile(profile())
->AppRegistryCache()));
if (auto_start_arc_test_) {
arc_test_.SetUp(profile());
}
// Wait until |extension_system| is signaled as started.
base::RunLoop run_loop;
extension_system->ready().Post(FROM_HERE, run_loop.QuitClosure());
run_loop.Run();
app_list_syncable_service_ =
app_list::AppListSyncableServiceFactory::GetForProfile(profile());
StartAppSyncService(app_list_syncable_service_->GetAllSyncDataForTesting());
std::string error;
extension_chrome_ = Extension::Create(
base::FilePath(), ManifestLocation::kUnpacked, manifest,
Extension::NO_FLAGS, app_constants::kChromeAppId, &error);
extension1_ = Extension::Create(
base::FilePath(), ManifestLocation::kUnpacked, manifest,
Extension::NO_FLAGS, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", &error);
extension2_ = Extension::Create(
base::FilePath(), ManifestLocation::kUnpacked, manifest,
Extension::NO_FLAGS, "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", &error);
extension5_ = Extension::Create(
base::FilePath(), ManifestLocation::kUnpacked, manifest,
Extension::NO_FLAGS, "cccccccccccccccccccccccccccccccc", &error);
extension6_ = Extension::Create(
base::FilePath(), ManifestLocation::kUnpacked, manifest,
Extension::NO_FLAGS, "dddddddddddddddddddddddddddddddd", &error);
extension7_ = Extension::Create(
base::FilePath(), ManifestLocation::kUnpacked, manifest,
Extension::NO_FLAGS, "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee", &error);
extension8_ = Extension::Create(
base::FilePath(), ManifestLocation::kUnpacked, manifest,
Extension::NO_FLAGS, "ffffffffffffffffffffffffffffffff", &error);
extension_platform_app_ = Extension::Create(
base::FilePath(), ManifestLocation::kUnpacked, manifest_platform_app,
Extension::NO_FLAGS, "gggggggggggggggggggggggggggggggg", &error);
arc_support_host_ = Extension::Create(
base::FilePath(), ManifestLocation::kUnpacked, manifest,
Extension::NO_FLAGS, arc::kPlayStoreAppId, &error);
extension_registrar_->AddExtension(extension_chrome_.get());
if (StartWebAppProviderForMainProfile()) {
StartWebAppProvider(profile());
}
}
virtual bool StartWebAppProviderForMainProfile() const { return true; }
void StartWebAppProvider(Profile* profile) {
auto* provider = web_app::FakeWebAppProvider::Get(profile);
auto* system_web_app_manager = ash::TestSystemWebAppManager::Get(profile);
provider->SetStartSystemOnStart(true);
provider->Start();
system_web_app_manager->ScheduleStart();
base::RunLoop run_loop;
provider->on_external_managers_synchronized().Post(FROM_HERE,
run_loop.QuitClosure());
run_loop.Run();
}
// Note that this resets previously installed SWAs.
void InstallSystemWebApp(
std::unique_ptr<ash::SystemWebAppDelegate> delegate) {
auto* system_web_app_manager =
ash::SystemWebAppManager::GetForTest(profile());
ash::SystemWebAppDelegateMap swa_map;
swa_map.emplace(delegate->GetType(), std::move(delegate));
system_web_app_manager->SetSystemAppsForTesting(std::move(swa_map));
system_web_app_manager->InstallSystemAppsForTesting();
}
ui::BaseWindow* GetLastActiveWindowForItemController(
AppWindowShelfItemController* item_controller) {
return item_controller->last_active_window_;
}
// Creates a running platform V2 app (not pinned) of type |app_id|.
virtual void CreateRunningV2App(const std::string& app_id) {
DCHECK(!test_controller_);
// Change the created shelf item controller into a V2 app controller.
std::unique_ptr<TestV2AppShelfItemController> controller =
std::make_unique<TestV2AppShelfItemController>(app_id);
test_controller_ = controller.get();
ash::ShelfID id = shelf_controller_->CreateAppItem(
std::move(controller), ash::STATUS_RUNNING, /*pinned=*/false);
ASSERT_TRUE(IsPlatformApp(id));
}
// Sets the stage for a multi user test.
virtual void SetUpMultiUserScenario(syncer::SyncChangeList* user_a,
syncer::SyncChangeList* user_b) {
InitShelfController();
EXPECT_EQ("Chrome", GetPinnedAppStatus());
// Set an empty pinned pref to begin with.
syncer::SyncChangeList sync_list;
InsertAddPinChange(&sync_list, 0, app_constants::kChromeAppId);
SendPinChanges(sync_list, true);
EXPECT_EQ("Chrome", GetPinnedAppStatus());
// Assume all applications have been added already.
AddWebApp(ash::kGoogleDocsAppId);
AddWebApp(ash::kGmailAppId);
extension_registrar_->AddExtension(extension1_.get());
extension_registrar_->AddExtension(extension2_.get());
extension_registrar_->AddExtension(extension5_.get());
extension_registrar_->AddExtension(extension6_.get());
extension_registrar_->AddExtension(extension7_.get());
extension_registrar_->AddExtension(extension8_.get());
extension_registrar_->AddExtension(extension_platform_app_.get());
// There should be nothing in the list by now.
EXPECT_EQ("Chrome", GetPinnedAppStatus());
// Set user a preferences.
InsertAddPinChange(user_a, 0, extension1_->id());
InsertAddPinChange(user_a, 1, extension2_->id());
InsertAddPinChange(user_a, 2, ash::kGmailAppId);
InsertAddPinChange(user_a, 3, extension_platform_app_->id());
InsertAddPinChange(user_a, 4, ash::kGoogleDocsAppId);
InsertAddPinChange(user_a, 5, extension5_->id());
InsertAddPinChange(user_a, 6, app_constants::kChromeAppId);
// Set user b preferences.
InsertAddPinChange(user_b, 0, extension6_->id());
InsertAddPinChange(user_b, 1, extension7_->id());
InsertAddPinChange(user_b, 2, extension8_->id());
InsertAddPinChange(user_b, 3, app_constants::kChromeAppId);
}
void TearDown() override {
app_registry_cache_observer_.Reset();
arc_test_.TearDown();
shelf_controller_ = nullptr;
wallpaper_controller_client_.reset();
BrowserWithTestWindowTest::TearDown();
ash::ConciergeClient::Shutdown();
app_list::AppListSyncableServiceFactory::SetUseInTesting(false);
}
std::unique_ptr<BrowserWindow> CreateBrowserWindow() override {
return CreateTestBrowserWindowAura();
}
std::unique_ptr<Browser> CreateBrowserWithTestWindowForProfile(
Profile* profile) {
auto browser_window = CreateTestBrowserWindowAura();
auto browser = CreateBrowser(profile, Browser::TYPE_NORMAL, false,
browser_window.get());
// Self deleting.
new TestBrowserWindowOwner(std::move(browser_window));
return browser;
}
// Create an uninitialized controller instance.
ChromeShelfController* CreateShelfController() {
shelf_controller_ =
std::make_unique<ChromeShelfController>(profile(), model_.get());
shelf_controller_->SetProfileForTest(profile());
shelf_controller_->SetShelfControllerHelperForTest(
std::make_unique<ShelfControllerHelper>(profile()));
return shelf_controller_.get();
}
// Create and initialize the controller, owned by the test shell delegate.
void InitShelfController() { CreateShelfController()->Init(); }
// Create and initialize the controller; create a tab and show the browser.
void InitShelfControllerWithBrowser() {
InitShelfController();
chrome::NewTab(browser());
browser()->window()->Show();
}
// Destroy the controller instance and clear the local pointer.
void ResetShelfController() { shelf_controller_.reset(); }
// Destroy and recreate the controller; clear and reinitialize the ShelfModel.
// Returns a pointer to the uninitialized controller, owned by shell delegate.
// TODO(msw): This does not accurately represent ChromeShelfController
// lifetime or usage in production, and does not accurately simulate restarts.
ChromeShelfController* RecreateShelfController() {
// Destroy any existing controller first; only one may exist at a time.
ResetShelfController();
model_ = std::make_unique<ash::ShelfModel>();
return CreateShelfController();
}
void StartAppSyncService(const syncer::SyncDataList& init_sync_list) {
app_list_syncable_service_->MergeDataAndStartSyncing(
syncer::APP_LIST, init_sync_list,
std::make_unique<syncer::FakeSyncChangeProcessor>());
EXPECT_EQ(init_sync_list.size(),
app_list_syncable_service_->sync_items().size());
}
void StopAppSyncService() {
app_list_syncable_service_->StopSyncing(syncer::APP_LIST);
}
sync_preferences::PrefModelAssociator* GetPrefSyncService() {
sync_preferences::PrefServiceSyncable* pref_sync =
profile()->GetTestingPrefService();
sync_preferences::PrefModelAssociator* pref_sync_service =
static_cast<sync_preferences::PrefModelAssociator*>(
pref_sync->GetSyncableService(syncer::OS_PREFERENCES));
return pref_sync_service;
}
void StartPrefSyncService(const syncer::SyncDataList& init_sync_list) {
std::optional<syncer::ModelError> error =
GetPrefSyncService()->MergeDataAndStartSyncing(
syncer::OS_PREFERENCES, init_sync_list,
std::make_unique<syncer::FakeSyncChangeProcessor>());
EXPECT_FALSE(error.has_value());
}
syncer::SyncData CreateStringPrefsSyncData(const std::string& pref_name,
const std::string& pref_value) {
sync_pb::EntitySpecifics specifics;
specifics.mutable_os_preference()->mutable_preference()->set_name(
pref_name);
specifics.mutable_os_preference()->mutable_preference()->set_value(
base::StringPrintf("\"%s\"", pref_value.c_str()));
return syncer::SyncData::CreateRemoteData(
specifics, syncer::ClientTagHash::FromHashed("unused"));
}
void SetAppIconLoader(std::unique_ptr<AppIconLoader> loader) {
std::vector<std::unique_ptr<AppIconLoader>> loaders;
loaders.push_back(std::move(loader));
shelf_controller_->SetAppIconLoadersForTest(loaders);
}
void SetAppIconLoaders(std::unique_ptr<AppIconLoader> loader1,
std::unique_ptr<AppIconLoader> loader2) {
std::vector<std::unique_ptr<AppIconLoader>> loaders;
loaders.push_back(std::move(loader1));
loaders.push_back(std::move(loader2));
shelf_controller_->SetAppIconLoadersForTest(loaders);
}
void SetShelfControllerHelper(ShelfControllerHelper* helper) {
shelf_controller_->SetShelfControllerHelperForTest(
base::WrapUnique<ShelfControllerHelper>(helper));
}
void AppendPrefValue(base::Value::List& pref_values,
std::string_view policy_id) {
pref_values.Append(base::Value::Dict().Set(
ChromeShelfPrefs::kPinnedAppsPrefAppIDKey, policy_id));
}
void RemovePrefValue(base::Value::List& pref_values,
std::string_view policy_id) {
pref_values.EraseIf([&policy_id](const auto& entry) {
return *entry.GetDict().FindString(
ChromeShelfPrefs::kPinnedAppsPrefAppIDKey) == policy_id;
});
}
void InsertRemoveAllPinsChange(syncer::SyncChangeList* list) {
for (const auto& sync_peer : app_list_syncable_service_->sync_items()) {
sync_pb::EntitySpecifics specifics;
sync_pb::AppListSpecifics* app_list_specifics =
specifics.mutable_app_list();
app_list_specifics->set_item_id(sync_peer.first);
app_list_specifics->set_item_type(sync_pb::AppListSpecifics::TYPE_APP);
syncer::SyncData sync_data =
syncer::SyncData::CreateLocalData(sync_peer.first, "Test", specifics);
list->push_back(syncer::SyncChange(
FROM_HERE, syncer::SyncChange::ACTION_DELETE, sync_data));
}
}
syncer::StringOrdinal GeneratePinPosition(int position) {
syncer::StringOrdinal ordinal_position =
syncer::StringOrdinal::CreateInitialOrdinal();
for (int i = 0; i < position; ++i) {
ordinal_position = ordinal_position.CreateAfter();
}
return ordinal_position;
}
void InsertPinChange(syncer::SyncChangeList* list,
int position,
bool add_pin_change,
const std::string& app_id,
syncer::SyncChange::SyncChangeType type) {
sync_pb::EntitySpecifics specifics;
sync_pb::AppListSpecifics* app_list_specifics =
specifics.mutable_app_list();
app_list_specifics->set_item_id(app_id);
app_list_specifics->set_item_type(sync_pb::AppListSpecifics::TYPE_APP);
if (add_pin_change) {
if (position >= 0) {
app_list_specifics->set_item_pin_ordinal(
GeneratePinPosition(position).ToInternalValue());
} else {
app_list_specifics->set_item_pin_ordinal(std::string());
}
}
syncer::SyncData sync_data =
syncer::SyncData::CreateLocalData(app_id, "Test", specifics);
list->push_back(syncer::SyncChange(FROM_HERE, type, sync_data));
}
void InsertAddPinChange(syncer::SyncChangeList* list,
int position,
const std::string& app_id) {
InsertPinChange(list, position, true, app_id,
syncer::SyncChange::ACTION_ADD);
}
void InsertUpdatePinChange(syncer::SyncChangeList* list,
int position,
const std::string& app_id) {
InsertPinChange(list, position, true, app_id,
syncer::SyncChange::ACTION_UPDATE);
}
void InsertRemovePinChange(syncer::SyncChangeList* list,
const std::string& app_id) {
InsertPinChange(list, -1, true, app_id, syncer::SyncChange::ACTION_UPDATE);
}
void InsertLegacyPinChange(syncer::SyncChangeList* list,
const std::string& app_id) {
InsertPinChange(list, -1, false, app_id, syncer::SyncChange::ACTION_UPDATE);
}
void ResetPinModel() {
syncer::SyncChangeList sync_list;
InsertRemoveAllPinsChange(&sync_list);
InsertAddPinChange(&sync_list, 0, kDummyAppId);
app_list_syncable_service_->ProcessSyncChanges(FROM_HERE, sync_list);
}
void SendPinChanges(const syncer::SyncChangeList& sync_list,
bool reset_pin_model) {
if (!reset_pin_model) {
app_list_syncable_service_->ProcessSyncChanges(FROM_HERE, sync_list);
} else {
syncer::SyncChangeList combined_sync_list;
InsertRemoveAllPinsChange(&combined_sync_list);
combined_sync_list.insert(combined_sync_list.end(), sync_list.begin(),
sync_list.end());
app_list_syncable_service_->ProcessSyncChanges(FROM_HERE,
combined_sync_list);
}
content::RunAllTasksUntilIdle();
}
// Set the index at which the chrome icon should be.
void SetShelfChromeIconIndex(int index) {
DCHECK(
app_list_syncable_service_->GetPinPosition(app_constants::kChromeAppId)
.IsValid());
syncer::StringOrdinal chrome_position;
chrome_position = index == 0 ? GeneratePinPosition(0).CreateBefore()
: GeneratePinPosition(index - 1).CreateBetween(
GeneratePinPosition(index));
syncer::SyncChangeList sync_list;
sync_pb::EntitySpecifics specifics;
sync_pb::AppListSpecifics* app_list_specifics =
specifics.mutable_app_list();
app_list_specifics->set_item_id(app_constants::kChromeAppId);
app_list_specifics->set_item_type(sync_pb::AppListSpecifics::TYPE_APP);
app_list_specifics->set_item_pin_ordinal(chrome_position.ToInternalValue());
syncer::SyncData sync_data = syncer::SyncData::CreateLocalData(
app_constants::kChromeAppId, "Test", specifics);
sync_list.push_back(syncer::SyncChange(
FROM_HERE, syncer::SyncChange::ACTION_UPDATE, sync_data));
app_list_syncable_service_->ProcessSyncChanges(FROM_HERE, sync_list);
content::RunAllTasksUntilIdle();
}
// Gets the IDs of the currently pinned app items.
void GetPinnedAppIds(ChromeShelfController* controller,
std::vector<std::string>* app_ids) {
app_ids->clear();
for (const auto& item : model_->items()) {
if (item.type == ash::TYPE_PINNED_APP) {
app_ids->push_back(item.id.app_id);
}
}
}
// Get the setup of the currently shown shelf items in one string.
// Each pinned element will start with a big letter, each running but not
// pinned V1 app will start with a small letter and each running but not
// pinned V2 app will start with a '*' + small letter.
std::string GetPinnedAppStatus() {
std::string result;
for (int i = 0; i < model_->item_count(); i++) {
if (!result.empty()) {
result.append(", ");
}
switch (model_->items()[i].type) {
case ash::TYPE_APP: {
if (IsPlatformApp(model_->items()[i].id)) {
result += "*";
}
const std::string& app = model_->items()[i].id.app_id;
EXPECT_FALSE(shelf_controller_->IsAppPinned(app));
if (app == extension1_->id()) {
result += "app1";
} else if (app == extension2_->id()) {
result += "app2";
} else if (app == extension5_->id()) {
result += "app5";
} else if (app == extension6_->id()) {
result += "app6";
} else if (app == extension7_->id()) {
result += "app7";
} else if (app == extension8_->id()) {
result += "app8";
} else if (app == ash::kGmailAppId) {
result += "gmail";
} else if (app == extension_platform_app_->id()) {
result += "platform_app";
} else if (app == ash::kGoogleDocsAppId) {
result += "doc";
} else if (app == ash::kYoutubeAppId) {
result += "youtube";
} else {
result += app_service_test_.GetAppName(app);
}
break;
}
case ash::TYPE_PINNED_APP: {
if (IsPlatformApp(model_->items()[i].id)) {
result += "*";
}
const std::string& app = model_->items()[i].id.app_id;
EXPECT_TRUE(shelf_controller_->IsAppPinned(app));
if (app == extension1_->id()) {
result += "App1";
} else if (app == extension2_->id()) {
result += "App2";
} else if (app == extension5_->id()) {
result += "App5";
} else if (app == extension6_->id()) {
result += "App6";
} else if (app == extension7_->id()) {
result += "App7";
} else if (app == extension8_->id()) {
result += "App8";
} else if (app == ash::kGmailAppId) {
result += "Gmail";
} else if (app == ash::kGoogleCalendarAppId) {
result += "Calendar";
} else if (app == ash::kGoogleDocsAppId) {
result += "Doc";
} else if (app == ash::kMessagesAppId) {
result += "Messages";
} else if (app == ash::kYoutubeAppId) {
result += "Youtube";
} else if (app == extension_platform_app_->id()) {
result += "Platform_App";
} else if (app == arc_support_host_->id()) {
result += "Play Store";
} else if (app == arc::kSettingsAppId) {
result += "Android Settings";
} else {
bool arc_app_found = false;
for (const auto& arc_app : arc_test_.fake_apps()) {
if (app == ArcAppTest::GetAppId(*arc_app)) {
result += arc_app->name;
arc_app_found = true;
break;
}
}
if (!arc_app_found) {
result += app_service_test_.GetAppName(app);
}
}
break;
}
case ash::TYPE_BROWSER_SHORTCUT:
result += "Chrome";
break;
default:
result += "Unknown";
break;
}
}
return result;
}
bool IsAppPolicyPinned(const std::string& app_id) {
return GetPinnableForAppID(app_id, profile()) ==
AppListControllerDelegate::PIN_FIXED;
}
// Returns the list containing app IDs of items shown in shelf. The order of
// IDs matches the order of associated shelf items in the shelf model.
std::vector<std::string> GetAppsShownInShelf() const {
std::vector<std::string> app_ids;
for (auto& item : model_->items()) {
app_ids.push_back(item.id.app_id);
}
return app_ids;
}
// Remember the order of unpinned but running applications for the current
// user.
void RememberUnpinnedRunningApplicationOrder() {
shelf_controller_->RememberUnpinnedRunningApplicationOrder();
}
// Restore the order of running but unpinned applications for a given user.
void RestoreUnpinnedRunningApplicationOrder(const AccountId& account_id) {
shelf_controller_->RestoreUnpinnedRunningApplicationOrder(
account_id.GetUserEmail());
}
void SendListOfArcApps() {
arc_test_.app_instance()->SendRefreshAppList(arc_test_.fake_apps());
}
void SendListOfArcShortcuts() {
arc_test_.app_instance()->SendInstallShortcuts(arc_test_.fake_shortcuts());
}
void UninstallArcApps() {
arc_test_.app_instance()->SendRefreshAppList(
std::vector<arc::mojom::AppInfoPtr>());
}
// TODO(victorhsieh): Add test coverage for when ARC is started regardless
// Play Store opt-in status, and the followed opt-in and opt-out.
void EnablePlayStore(bool enabled) {
arc::SetArcPlayStoreEnabledForProfile(profile(), enabled);
base::RunLoop().RunUntilIdle();
}
void ValidateArcState(bool arc_enabled,
bool arc_managed,
arc::ArcSessionManager::State state,
const std::string& pin_status) {
EXPECT_EQ(arc_enabled, arc::IsArcPlayStoreEnabledForProfile(profile()));
EXPECT_EQ(arc_managed,
arc::IsArcPlayStoreEnabledPreferenceManagedForProfile(profile()));
EXPECT_EQ(state, arc_test_.arc_session_manager()->state());
EXPECT_EQ(pin_status, GetPinnedAppStatus());
}
// Creates app window and set optional ARC application id.
views::Widget* CreateArcWindow(const std::string& window_app_id) {
views::Widget::InitParams params(
views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
views::Widget::InitParams::TYPE_WINDOW);
params.bounds = gfx::Rect(5, 5, 20, 20);
params.context = GetContext();
views::Widget* widget = new views::Widget();
widget->Init(std::move(params));
// Set ARC id before showing the window to be recognized in
// AppServiceAppWindowArcTracker.
exo::SetShellApplicationId(widget->GetNativeWindow(), window_app_id);
widget->Show();
widget->Activate();
return widget;
}
arc::mojom::AppInfoPtr CreateAppInfo(const std::string& name,
const std::string& activity,
const std::string& package_name) {
auto appinfo = arc::mojom::AppInfo::New();
appinfo->name = name;
appinfo->package_name = package_name;
appinfo->activity = activity;
return appinfo;
}
std::string AddArcAppAndShortcut(const arc::mojom::AppInfo& app_info) {
ArcAppListPrefs* const prefs = arc_test_.arc_app_list_prefs();
std::optional<uint64_t> app_size_in_bytes;
std::optional<uint64_t> data_size_in_bytes;
if (!app_info.app_storage.is_null()) {
app_size_in_bytes = app_info.app_storage->app_size_in_bytes;
data_size_in_bytes = app_info.app_storage->data_size_in_bytes;
}
// Adding app to the prefs, and check that the app is accessible by id.
prefs->AddAppAndShortcut(
app_info.name, app_info.package_name, app_info.activity,
std::string() /* intent_uri */, std::string() /* icon_resource_id */,
app_info.version_name, false /* sticky */,
true /* notifications_enabled */, true /* app_ready */,
false /* suspended */, false /* shortcut */, true /* launchable */,
false /* need_fixup */, ArcAppListPrefs::WindowLayout(),
app_size_in_bytes, data_size_in_bytes, app_info.app_category);
const std::string app_id =
ArcAppListPrefs::GetAppId(app_info.package_name, app_info.activity);
EXPECT_TRUE(prefs->GetApp(app_id));
return app_id;
}
void NotifyOnTaskCreated(const arc::mojom::AppInfo& appinfo,
int32_t task_id) {
ArcAppListPrefs* const prefs = arc_test_.arc_app_list_prefs();
prefs->OnTaskCreated(task_id, appinfo.package_name, appinfo.activity,
appinfo.name, std::string(), /*session_id=*/0);
}
// Creates a window with TYPE_APP shelf item type and the given app_id.
views::Widget* CreateShelfAppWindow(const std::string& app_id) {
views::Widget::InitParams params(
views::Widget::InitParams::NATIVE_WIDGET_OWNS_WIDGET,
views::Widget::InitParams::TYPE_WINDOW);
params.context = GetContext();
params.bounds = gfx::Rect(5, 5, 20, 20);
views::Widget* widget = new views::Widget();
widget->Init(std::move(params));
aura::Window* window = widget->GetNativeWindow();
const ash::ShelfID shelf_id(app_id);
window->SetProperty(ash::kShelfIDKey, shelf_id.Serialize());
window->SetProperty<int>(ash::kShelfItemTypeKey, ash::TYPE_APP);
window->SetProperty(ash::kAppIDKey, app_id);
widget->Show();
widget->Activate();
return widget;
}
void NotifyOnTaskDestroyed(int32_t task_id) {
ArcAppListPrefs* const prefs = arc_test_.arc_app_list_prefs();
prefs->OnTaskDestroyed(task_id);
}
// Add extension.
void AddExtension(const Extension* extension) {
extension_registrar_->AddExtension(extension);
}
// Uninstall extension.
void UninstallExtension(const std::string& extension_id,
extensions::UninstallReason reason) {
extension_registrar_->UninstallExtension(extension_id, reason, nullptr);
}
// Unload extension.
void UnloadExtension(const std::string& extension_id,
UnloadedExtensionReason reason) {
extension_registrar_->RemoveExtension(extension_id, reason);
}
const GURL& GetWebAppUrl(const std::string& web_app_id) const {
static const base::flat_map<std::string, GURL> web_app_id_to_start_url{
{ash::kGmailAppId,
GURL("https://mail.google.com/mail/?usp=installed_webapp")},
{ash::kGoogleCalendarAppId,
GURL("https://calendar.google.com/calendar/r")},
{ash::kGoogleDocsAppId,
GURL("https://docs.google.com/document/?usp=installed_webapp")},
{ash::kMessagesAppId, GURL("https://messages.google.com/web/")},
{ash::kYoutubeAppId, GURL("https://www.youtube.com/?feature=ytca")}};
DCHECK(base::Contains(web_app_id_to_start_url, web_app_id));
return web_app_id_to_start_url.at(web_app_id);
}
void AddWebApp(const std::string& web_app_id) {
auto web_app_info =
web_app::WebAppInstallInfo::CreateWithStartUrlForTesting(
GetWebAppUrl(web_app_id));
webapps::AppId installed_app_id =
web_app::test::InstallWebApp(profile(), std::move(web_app_info));
ASSERT_EQ(installed_app_id, web_app_id);
apps::AppReadinessWaiter(profile(), web_app_id).Await();
}
web_app::IsolatedWebAppUrlInfo AddIsolatedWebApp() {
const std::unique_ptr<web_app::ScopedBundledIsolatedWebApp> app_bundle =
web_app::IsolatedWebAppBuilder(web_app::ManifestBuilder())
.BuildBundle();
const web_app::IsolatedWebAppUrlInfo url_info =
app_bundle
->InstallWithSource(
profile(),
&web_app::IsolatedWebAppInstallSource::FromExternalPolicy)
.value();
apps::AppReadinessWaiter(profile(), url_info.app_id()).Await();
return url_info;
}
webapps::AppId InstallExternalWebApp(
const GURL& start_url,
const std::optional<GURL>& install_url = {}) {
auto web_app_info =
web_app::WebAppInstallInfo::CreateWithStartUrlForTesting(
GURL(start_url));
web_app_info->install_url = GURL(install_url ? *install_url : start_url);
const webapps::AppId expected_web_app_id = web_app::GenerateAppId(
/*manifest_id_path=*/std::nullopt, web_app_info->start_url());
webapps::AppId web_app_id = web_app::test::InstallWebApp(
profile(), std::move(web_app_info),
/*overwrite_existing_manifest_fields =*/false,
webapps::WebappInstallSource::EXTERNAL_POLICY);
DCHECK_EQ(expected_web_app_id, web_app_id);
return web_app_id;
}
webapps::AppId InstallExternalWebApp(
const std::string& start_url,
const std::optional<std::string>& install_url = {}) {
return InstallExternalWebApp(GURL(start_url), install_url
? GURL(*install_url)
: std::optional<GURL>());
}
void RemoveWebApp(const char* web_app_id) {
web_app::test::UninstallWebApp(profile(), web_app_id);
apps::AppReadinessWaiter(profile(), web_app_id,
apps::Readiness::kUninstalledByUser)
.Await();
}
void OnAppRegistryCacheWillBeDestroyed(
apps::AppRegistryCache* cache) override {
app_registry_cache_observer_.Reset();
}
template <class... Args>
void SetPinnedLauncherAppsPolicy(Args&&... args) {
base::Value::List pinned_launcher_apps;
(AppendPrefValue(pinned_launcher_apps, std::forward<Args>(args)), ...);
profile()->GetTestingPrefService()->SetManagedPref(
prefs::kPolicyPinnedLauncherApps,
base::Value(std::move(pinned_launcher_apps)));
}
apps::AppServiceTest& app_service_test() { return app_service_test_; }
bool IsPlatformApp(const ash::ShelfID& id) const {
const extensions::Extension* extension =
GetExtensionForAppID(id.app_id, profile());
// An extension can be synced / updated at any time and therefore not be
// available.
return extension ? extension->is_platform_app() : false;
}
// Needed for extension service & friends to work.
scoped_refptr<Extension> extension_chrome_;
scoped_refptr<Extension> extension1_;
scoped_refptr<Extension> extension2_;
scoped_refptr<Extension> extension5_;
scoped_refptr<Extension> extension6_;
scoped_refptr<Extension> extension7_;
scoped_refptr<Extension> extension8_;
scoped_refptr<Extension> extension_platform_app_;
scoped_refptr<Extension> arc_support_host_;
ArcAppTest arc_test_{ArcAppTest::UserManagerMode::kDoNothing};
bool auto_start_arc_test_ = false;
std::unique_ptr<ChromeShelfController> shelf_controller_;
std::unique_ptr<ash::ShelfModel> model_;
// |item_delegate_manager_| owns |test_controller_|.
raw_ptr<ash::ShelfItemDelegate, DanglingUntriaged> test_controller_ = nullptr;
raw_ptr<extensions::ExtensionRegistrar, DanglingUntriaged>
extension_registrar_ = nullptr;
raw_ptr<extensions::ExtensionRegistry, DanglingUntriaged>
extension_registry_ = nullptr;
raw_ptr<extensions::ExtensionService, DanglingUntriaged> extension_service_ =
nullptr;
raw_ptr<app_list::AppListSyncableService, DanglingUntriaged>
app_list_syncable_service_ = nullptr;
base::AutoReset<bool> skip_preinstalled_web_app_startup_;
base::ScopedObservation<apps::AppRegistryCache,
apps::AppRegistryCache::Observer>
app_registry_cache_observer_{this};
private:
std::unique_ptr<TestBrowserWindow> CreateTestBrowserWindowAura() {
auto window = std::make_unique<aura::Window>(
nullptr, aura::client::WINDOW_TYPE_NORMAL);
window->SetId(0);
window->Init(ui::LAYER_TEXTURED);
aura::client::ParentWindowWithContext(window.get(), GetContext(),
gfx::Rect(200, 200),
display::kInvalidDisplayId);
return std::make_unique<TestBrowserWindowAura>(std::move(window));
}
std::unique_ptr<WallpaperControllerClientImpl> wallpaper_controller_client_;
apps::AppServiceTest app_service_test_;
};
class ChromeShelfControllerWithArcTest : public ChromeShelfControllerTestBase {
protected:
ChromeShelfControllerWithArcTest() { auto_start_arc_test_ = true; }
ChromeShelfControllerWithArcTest(const ChromeShelfControllerWithArcTest&) =
delete;
ChromeShelfControllerWithArcTest& operator=(
const ChromeShelfControllerWithArcTest&) = delete;
~ChromeShelfControllerWithArcTest() override = default;
void SetUp() override {
// To prevent crash on test exit and pending decode request.
ArcAppIcon::DisableSafeDecodingForTesting();
ChromeShelfControllerTestBase::SetUp();
}
private:
// isolated web app builder uses json parser from the decoder
data_decoder::test::InProcessDataDecoder in_process_data_decoder_;
};
class ChromeShelfControllerTest : public ChromeShelfControllerTestBase {
public:
ChromeShelfControllerTest() {
// `media_router::kMediaRouter` is disabled because it has unmet
// dependencies and is unrelated to this unit test.
feature_list_.InitAndDisableFeature(media_router::kMediaRouter);
}
~ChromeShelfControllerTest() override = default;
private:
// CrostiniTestHelper overrides feature list after GPU thread has started.
viz::TestGpuServiceHolder::ScopedAllowRacyFeatureListOverrides
gpu_thread_allow_racy_overrides_;
base::test::ScopedFeatureList feature_list_;
};
// A V1 windowed application.
class V1App : public TestBrowserWindow {
public:
V1App(Profile* profile, const std::string& app_name) {
Browser::CreateParams params = Browser::CreateParams::CreateForApp(
kCrxAppPrefix + app_name, true /* trusted_source */, gfx::Rect(),
profile, true);
params.window = this;
browser_ = Browser::DeprecatedCreateOwnedForTesting(params);
chrome::AddTabAt(browser_.get(), GURL(), 0, true);
}
V1App(const V1App&) = delete;
V1App& operator=(const V1App&) = delete;
~V1App() override {
// close all tabs. Note that we do not need to destroy the browser itself.
browser_->tab_strip_model()->CloseAllTabs();
}
Browser* browser() { return browser_.get(); }
private:
// The associated browser with this app.
std::unique_ptr<Browser> browser_;
};
// A V2 application window created with an |extension| and for a |profile|.
// Upon destruction it will properly close the application.
class V2App {
public:
V2App(Profile* profile,
const extensions::Extension* extension,
extensions::AppWindow::WindowType window_type =
extensions::AppWindow::WINDOW_TYPE_DEFAULT)
: creator_web_contents_(
content::WebContentsTester::CreateTestWebContents(profile,
nullptr)) {
window_ = new extensions::AppWindow(
profile, std::make_unique<ChromeAppDelegate>(profile, true), extension);
extensions::AppWindow::CreateParams params;
params.window_type = window_type;
// Note: normally, the creator RFH is the background page of the
// app/extension calling chrome.app.window.create. For unit testing
// purposes, just passing in a random RenderFrameHost is Good Enoughâ„¢.
window_->Init(GURL(std::string()),
std::make_unique<extensions::AppWindowContentsImpl>(window_),
creator_web_contents_->GetPrimaryMainFrame(), params);
}
V2App(const V2App&) = delete;
V2App& operator=(const V2App&) = delete;
virtual ~V2App() {
content::WebContentsDestroyedWatcher destroyed_watcher(
window_->web_contents());
window_->GetBaseWindow()->Close();
destroyed_watcher.Wait();
}
extensions::AppWindow* window() { return window_; }
private:
std::unique_ptr<content::WebContents> creator_web_contents_;
// The app window which represents the application. Note that the window
// deletes itself asynchronously after window_->GetBaseWindow()->Close() gets
// called.
raw_ptr<extensions::AppWindow, DanglingUntriaged> window_;
};
// The testing framework to test multi profile scenarios.
class MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest
: public ChromeShelfControllerTestBase {
protected:
MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest() {
// `kMediaRouter` is disabled because it has unmet dependencies and is
// unrelated to this unit test.
scoped_feature_list_.InitAndDisableFeature(media_router::kMediaRouter);
}
MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest(
const MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest&) =
delete;
MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest& operator=(
const MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest&) =
delete;
~MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest() override =
default;
// Overwrite the Setup function to enable multi profile and needed objects.
void SetUp() override {
// Initialize the rest.
ChromeShelfControllerTestBase::SetUp();
// Ensure there are multiple profiles. User 0 is created during setup.
CreateMultiUserProfile("user1@example.com", GaiaId("fakegaia1"));
ASSERT_TRUE(SessionControllerClientImpl::IsMultiProfileAvailable());
}
void TearDown() override {
ChromeShelfControllerTestBase::TearDown();
// A Task is leaked if we don't destroy everything, then run the message
// loop.
base::RunLoop().RunUntilIdle();
}
bool StartWebAppProviderForMainProfile() const override {
// The provider is started in CreateMultiUserProfile()
return false;
}
// Creates a user and profile for a given `email`. Note that this class will
// keep the ownership of the created object.
TestingProfile* CreateMultiUserProfile(std::string_view email,
const GaiaId& gaia_id) {
LogIn(email, gaia_id);
return CreateProfile(std::string(email));
}
// Switch to another user by AccountId.
// TODO(b/40286020): Migrate into SwitchActiveUser().
void SwitchActiveUserByAccountId(const AccountId& account_id) {
user_manager()->SwitchActiveUser(account_id);
ash::MultiUserWindowManagerImpl::Get()->SetAnimationSpeedForTest(
ash::MultiUserWindowManagerImpl::ANIMATION_SPEED_DISABLED);
ash::MultiUserWindowManagerImpl::Get()->OnActiveUserSessionChanged(
account_id);
}
// Creates a browser with a |profile| and load a tab with a |title| and |url|.
std::unique_ptr<Browser> CreateBrowserAndTabWithProfile(
Profile* profile,
const std::string& title,
const std::string& url) {
std::unique_ptr<Browser> browser(
CreateBrowserWithTestWindowForProfile(profile));
chrome::NewTab(browser.get());
browser->window()->Show();
NavigateAndCommitActiveTabWithTitle(browser.get(), GURL(url),
ASCIIToUTF16(title));
return browser;
}
// Creates a running V1 application.
// Note that with the use of the shelf_controller_helper as done below,
// this is only usable with a single v1 application.
V1App* CreateRunningV1App(Profile* profile,
const std::string& app_name,
const std::string& url) {
V1App* v1_app = new V1App(profile, app_name);
NavigateAndCommitActiveTabWithTitle(v1_app->browser(), GURL(url),
std::u16string());
return v1_app;
}
// Override BrowserWithTestWindowTest:
std::optional<std::string> GetDefaultProfileName() override {
return "user0@example.com";
}
void LogIn(std::string_view email, const GaiaId& gaia_id) override {
// TODO(crbug.com/40286020): Merge into BrowserWithTestWindowTest.
const AccountId account_id = AccountId::FromUserEmailGaiaId(email, gaia_id);
// Add a user to the fake user manager.
user_manager()->AddGaiaUser(account_id, user_manager::UserType::kRegular);
user_manager()->UserLoggedIn(
account_id, user_manager::TestHelper::GetFakeUsernameHash(account_id));
}
TestingProfile* CreateProfile(const std::string& profile_name) override {
TestingProfile* profile =
BrowserWithTestWindowTest::CreateProfile(profile_name);
StartWebAppProvider(profile);
if (MultiUserWindowManagerHelper::GetInstance()) {
MultiUserWindowManagerHelper::GetInstance()->AddUser(profile);
}
if (shelf_controller_) {
shelf_controller_->AdditionalUserAddedToSession(profile);
}
return profile;
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
class ChromeShelfControllerMultiProfileWithArcTest
: public MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest {
protected:
ChromeShelfControllerMultiProfileWithArcTest() {
auto_start_arc_test_ = true;
}
ChromeShelfControllerMultiProfileWithArcTest(
const ChromeShelfControllerMultiProfileWithArcTest&) = delete;
ChromeShelfControllerMultiProfileWithArcTest& operator=(
const ChromeShelfControllerMultiProfileWithArcTest&) = delete;
~ChromeShelfControllerMultiProfileWithArcTest() override = default;
};
TEST_F(ChromeShelfControllerTest, DefaultShelfPrefValues) {
StartPrefSyncService(syncer::SyncDataList());
InitShelfController();
// Verify shelf prefs are initialized to default values if they're not set
// either locally nor in sync data.
PrefService* const prefs = browser()->profile()->GetPrefs();
EXPECT_EQ(ash::ShelfAlignment::kBottom,
ash::GetShelfAlignmentPref(prefs, GetPrimaryDisplayId()));
EXPECT_EQ(ash::ShelfAutoHideBehavior::kNever,
ash::GetShelfAutoHideBehaviorPref(prefs, GetPrimaryDisplayId()));
// Verify that shelf pref values don't change locally if they change in sync
// after local value has been initialized.
syncer::SyncChangeList os_change_list;
os_change_list.emplace_back(
FROM_HERE, syncer::SyncChange::ACTION_UPDATE,
CreateStringPrefsSyncData(ash::prefs::kShelfAlignment,
ash::kShelfAlignmentLeft));
os_change_list.emplace_back(
FROM_HERE, syncer::SyncChange::ACTION_UPDATE,
CreateStringPrefsSyncData(ash::prefs::kShelfAutoHideBehavior,
ash::kShelfAutoHideBehaviorAlways));
GetPrefSyncService()->ProcessSyncChanges(FROM_HERE, os_change_list);
EXPECT_EQ(ash::ShelfAlignment::kBottom,
ash::GetShelfAlignmentPref(prefs, GetPrimaryDisplayId()));
EXPECT_EQ(ash::ShelfAutoHideBehavior::kNever,
ash::GetShelfAutoHideBehaviorPref(prefs, GetPrimaryDisplayId()));
}
TEST_F(ChromeShelfControllerTest, ShelfPrefsInitializedFromSyncData) {
// Add shelf prefs to synced pref data, and start syncing.
syncer::SyncDataList sync_list;
sync_list.push_back(CreateStringPrefsSyncData(ash::prefs::kShelfAlignment,
ash::kShelfAlignmentLeft));
sync_list.push_back(CreateStringPrefsSyncData(
ash::prefs::kShelfAutoHideBehavior, ash::kShelfAutoHideBehaviorAlways));
StartPrefSyncService(std::move(sync_list));
// Verify local shelf state is initialized to reflect values in sync data if
// shelf controller gets initialized after initial sync pref values have been
// received.
InitShelfController();
PrefService* const prefs = browser()->profile()->GetPrefs();
EXPECT_EQ(ash::ShelfAlignment::kLeft,
ash::GetShelfAlignmentPref(prefs, GetPrimaryDisplayId()));
EXPECT_EQ(ash::ShelfAutoHideBehavior::kAlways,
ash::GetShelfAutoHideBehaviorPref(prefs, GetPrimaryDisplayId()));
// Verify further synced shelf prefs changes do not affect local shelf state.
syncer::SyncChangeList os_change_list;
os_change_list.emplace_back(
FROM_HERE, syncer::SyncChange::ACTION_UPDATE,
CreateStringPrefsSyncData(ash::prefs::kShelfAlignment,
ash::kShelfAlignmentBottom));
os_change_list.emplace_back(
FROM_HERE, syncer::SyncChange::ACTION_UPDATE,
CreateStringPrefsSyncData(ash::prefs::kShelfAutoHideBehavior,
ash::kShelfAutoHideBehaviorNever));
GetPrefSyncService()->ProcessSyncChanges(FROM_HERE, os_change_list);
EXPECT_EQ(ash::ShelfAlignment::kLeft,
ash::GetShelfAlignmentPref(prefs, GetPrimaryDisplayId()));
EXPECT_EQ(ash::ShelfAutoHideBehavior::kAlways,
ash::GetShelfAutoHideBehaviorPref(prefs, GetPrimaryDisplayId()));
}
TEST_F(ChromeShelfControllerTest,
ShelfControllerUpdatesShelfPrefAfterInitialPrefSync) {
// Initialize shelf controller, and verify shelf state reflects default values
// before initial synced prefs have been received.
InitShelfController();
PrefService* const prefs = browser()->profile()->GetPrefs();
EXPECT_EQ(ash::ShelfAlignment::kBottom,
ash::GetShelfAlignmentPref(prefs, GetPrimaryDisplayId()));
EXPECT_EQ(ash::ShelfAutoHideBehavior::kNever,
ash::GetShelfAutoHideBehaviorPref(prefs, GetPrimaryDisplayId()));
// If initial sync data contains shelf prefs, local shelf state should be
// updated to reflect shelf state in the initial sync data.
syncer::SyncDataList sync_list;
sync_list.push_back(CreateStringPrefsSyncData(ash::prefs::kShelfAlignment,
ash::kShelfAlignmentLeft));
sync_list.push_back(CreateStringPrefsSyncData(
ash::prefs::kShelfAutoHideBehavior, ash::kShelfAutoHideBehaviorAlways));
StartPrefSyncService(std::move(sync_list));
EXPECT_EQ(ash::ShelfAlignment::kLeft,
ash::GetShelfAlignmentPref(prefs, GetPrimaryDisplayId()));
EXPECT_EQ(ash::ShelfAutoHideBehavior::kAlways,
ash::GetShelfAutoHideBehaviorPref(prefs, GetPrimaryDisplayId()));
// Verify further synced shelf prefs changes do not affect local shelf state.
syncer::SyncChangeList os_change_list;
os_change_list.emplace_back(
FROM_HERE, syncer::SyncChange::ACTION_UPDATE,
CreateStringPrefsSyncData(ash::prefs::kShelfAlignment,
ash::kShelfAlignmentBottom));
os_change_list.emplace_back(
FROM_HERE, syncer::SyncChange::ACTION_UPDATE,
CreateStringPrefsSyncData(ash::prefs::kShelfAutoHideBehavior,
ash::kShelfAutoHideBehaviorNever));
GetPrefSyncService()->ProcessSyncChanges(FROM_HERE, os_change_list);
EXPECT_EQ(ash::ShelfAlignment::kLeft,
ash::GetShelfAlignmentPref(prefs, GetPrimaryDisplayId()));
EXPECT_EQ(ash::ShelfAutoHideBehavior::kAlways,
ash::GetShelfAutoHideBehaviorPref(prefs, GetPrimaryDisplayId()));
}
TEST_F(ChromeShelfControllerTest, SyncedShelfPrefsDontOverrideLocalPref) {
// Initialize shelf prefs before shelf controller gets initialized.
PrefService* const prefs = browser()->profile()->GetPrefs();
ash::SetShelfAlignmentPref(prefs, GetPrimaryDisplayId(),
ash::ShelfAlignment::kLeft);
ash::SetShelfAutoHideBehaviorPref(prefs, GetPrimaryDisplayId(),
ash::ShelfAutoHideBehavior::kAlways);
// Set initial sync data to conflict with the local shelf prefs.
syncer::SyncDataList sync_list;
sync_list.push_back(CreateStringPrefsSyncData(ash::prefs::kShelfAlignment,
ash::kShelfAlignmentBottom));
sync_list.push_back(CreateStringPrefsSyncData(
ash::prefs::kShelfAutoHideBehavior, ash::kShelfAutoHideBehaviorNever));
StartPrefSyncService(std::move(sync_list));
// Initialize shelf controller, and verify shelf state reflects initially set
// local prefs.
InitShelfController();
EXPECT_EQ(ash::ShelfAlignment::kLeft,
ash::GetShelfAlignmentPref(prefs, GetPrimaryDisplayId()));
EXPECT_EQ(ash::ShelfAutoHideBehavior::kAlways,
ash::GetShelfAutoHideBehaviorPref(prefs, GetPrimaryDisplayId()));
}
TEST_F(ChromeShelfControllerTest, PreinstalledApps) {
InitShelfController();
// The model should only contain the browser shortcut item.
EXPECT_EQ("Chrome", GetPinnedAppStatus());
// Pinning the non-preinstalled app. It should appear at the end. No
// preinstalled app is currently installed.
extension_registrar_->AddExtension(extension1_.get());
PinAppWithIDToShelf(extension1_->id());
EXPECT_EQ("Chrome, App1", GetPinnedAppStatus());
// Install preinstalled apps in reverse order, compared how they are declared.
// However pin positions should be in the order as they declared. Note,
// preinstalled apps appear on shelf between manually pinned App1.
// Prefs are not yet synced. No default pin appears.
AddWebApp(ash::kYoutubeAppId);
EXPECT_EQ("Chrome, App1", GetPinnedAppStatus());
StartPrefSyncService(syncer::SyncDataList());
EXPECT_EQ("Chrome, Youtube, App1", GetPinnedAppStatus());
AddWebApp(ash::kMessagesAppId);
EXPECT_EQ("Chrome, Messages, Youtube, App1", GetPinnedAppStatus());
AddWebApp(ash::kGoogleCalendarAppId);
EXPECT_EQ("Chrome, Calendar, Messages, Youtube, App1", GetPinnedAppStatus());
AddWebApp(ash::kGmailAppId);
EXPECT_EQ("Chrome, Gmail, Calendar, Messages, Youtube, App1",
GetPinnedAppStatus());
}
TEST_F(ChromeShelfControllerWithArcTest, ArcAppsHiddenFromLaunchCanBePinned) {
InitShelfController();
// Register Android Settings.
arc::mojom::AppHost* app_host = arc_test_.arc_app_list_prefs();
app_host->OnAppListRefreshed(GetArcSettingsAppInfo());
// Pin Android settings.
PinAppWithIDToShelf(arc::kSettingsAppId);
EXPECT_EQ("Chrome, Android Settings", GetPinnedAppStatus());
// The pin should remain after syncing prefs.
StartPrefSyncService(syncer::SyncDataList());
EXPECT_EQ("Chrome, Android Settings", GetPinnedAppStatus());
}
TEST_F(ChromeShelfControllerWithArcTest, ArcAppPinCrossPlatformWorkflow) {
// Work on ARC disabled platform first.
const std::string arc_app_id1 =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[0]);
const std::string arc_app_id2 =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[1]);
const std::string arc_app_id3 =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[2]);
InitShelfController();
extension_registrar_->AddExtension(extension1_.get());
extension_registrar_->AddExtension(extension2_.get());
AddWebApp(ash::kGmailAppId);
// extension 1, 3 are pinned by user
syncer::SyncChangeList sync_list;
InsertAddPinChange(&sync_list, 0, extension1_->id());
InsertAddPinChange(&sync_list, 1, arc_app_id1);
InsertAddPinChange(&sync_list, 2, extension2_->id());
InsertAddPinChange(&sync_list, 3, arc_app_id2);
InsertAddPinChange(&sync_list, 4, ash::kGmailAppId);
SendPinChanges(sync_list, true);
SetShelfChromeIconIndex(1);
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(arc_app_id1));
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(arc_app_id2));
EXPECT_TRUE(shelf_controller_->IsAppPinned(ash::kGmailAppId));
EXPECT_FALSE(shelf_controller_->IsAppPinned(arc_app_id3));
EXPECT_EQ("App1, Chrome, App2, Gmail", GetPinnedAppStatus());
// Persist pin state, we don't have active pin for ARC apps yet, but pin
// model should have it.
syncer::SyncDataList copy_sync_list =
app_list_syncable_service_->GetAllSyncDataForTesting();
ResetShelfController();
SendPinChanges(syncer::SyncChangeList(), true);
StopAppSyncService();
EXPECT_EQ(0U, app_list_syncable_service_->sync_items().size());
// Move to ARC enabled platform, restart syncing with stored data.
StartAppSyncService(copy_sync_list);
RecreateShelfController()->Init();
// Set FakeAppServiceAppIconLoader to wait for icons loaded.
auto fake_app_service_icon_loader =
std::make_unique<FakeAppServiceAppIconLoader>(
profile(), extension_misc::EXTENSION_ICON_MEDIUM,
shelf_controller_.get());
FakeAppServiceAppIconLoader* icon_loader = fake_app_service_icon_loader.get();
SetAppIconLoader(std::move(fake_app_service_icon_loader));
// Pins must be automatically updated.
SendListOfArcApps();
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_TRUE(shelf_controller_->IsAppPinned(arc_app_id1));
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_TRUE(shelf_controller_->IsAppPinned(arc_app_id2));
EXPECT_TRUE(shelf_controller_->IsAppPinned(ash::kGmailAppId));
EXPECT_FALSE(shelf_controller_->IsAppPinned(arc_app_id3));
EXPECT_EQ("App1, Chrome, Fake App 1, App2, Fake App 2, Gmail",
GetPinnedAppStatus());
// Now move pins on ARC enabled platform.
model_->Move(0, 3);
model_->Move(2, 0);
model_->Move(2, 4);
model_->Move(3, 1);
EXPECT_EQ("App2, Fake App 2, Chrome, App1, Fake App 1, Gmail",
GetPinnedAppStatus());
// ARC apps, Fake App 2 and Fake App 1, are updated due to icon updates. So
// wait for ARC apps icon updated by AppService before reset sync service.
icon_loader->WaitForIconLoadded(
std::vector<std::string>{arc_app_id1, arc_app_id2});
copy_sync_list = app_list_syncable_service_->GetAllSyncDataForTesting();
ResetShelfController();
ResetPinModel();
SendPinChanges(syncer::SyncChangeList(), true);
StopAppSyncService();
EXPECT_EQ(0U, app_list_syncable_service_->sync_items().size());
// Move back to ARC disabled platform.
EnablePlayStore(false);
StartAppSyncService(copy_sync_list);
RecreateShelfController()->Init();
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(arc_app_id1));
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(arc_app_id2));
EXPECT_TRUE(shelf_controller_->IsAppPinned(ash::kGmailAppId));
EXPECT_FALSE(shelf_controller_->IsAppPinned(arc_app_id3));
EXPECT_EQ("App2, Chrome, App1, Gmail", GetPinnedAppStatus());
// Now move/remove pins on ARC disabled platform.
model_->Move(3, 1);
shelf_controller_->UnpinAppWithID(extension2_->id());
EXPECT_EQ("Gmail, Chrome, App1", GetPinnedAppStatus());
EnablePlayStore(true);
SendListOfArcApps();
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_TRUE(shelf_controller_->IsAppPinned(arc_app_id1));
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_TRUE(shelf_controller_->IsAppPinned(arc_app_id2));
EXPECT_TRUE(shelf_controller_->IsAppPinned(ash::kGmailAppId));
EXPECT_FALSE(shelf_controller_->IsAppPinned(arc_app_id3));
EXPECT_EQ("Gmail, Fake App 2, Chrome, App1, Fake App 1",
GetPinnedAppStatus());
}
// Ensure correct merging of policy pinned apps and user pinned apps.
TEST_F(ChromeShelfControllerTest, MergePolicyAndUserPrefPinnedApps) {
InitShelfController();
// Install two versions of google docs with different install_urls.
const GURL& google_docs_start_url = GetWebAppUrl(ash::kGoogleDocsAppId);
const GURL google_docs_install_url_v1{
base::StrCat({google_docs_start_url.spec(), "&v=1"})};
const GURL google_docs_install_url_v2{
base::StrCat({google_docs_start_url.spec(), "&v=2"})};
InstallExternalWebApp(/*start_url=*/google_docs_start_url,
/*install_url=*/google_docs_install_url_v1);
InstallExternalWebApp(/*start_url=*/google_docs_start_url,
/*install_url=*/google_docs_install_url_v2);
// Check that both values are propagated to PolicyIds().
std::vector<std::string> google_docs_install_urls = {
google_docs_install_url_v1.spec(),
google_docs_install_url_v2.spec(),
};
apps::AppServiceProxyFactory::GetForProfile(profile())
->AppRegistryCache()
.ForOneApp(ash::kGoogleDocsAppId, [&google_docs_install_urls](
const auto& update) {
ASSERT_THAT(update.PolicyIds(), testing::UnorderedElementsAreArray(
google_docs_install_urls));
});
InstallExternalWebApp(GetWebAppUrl(ash::kGmailAppId));
extension_registrar_->AddExtension(extension1_.get());
extension_registrar_->AddExtension(extension5_.get());
// extension 1, 3 are pinned by user
syncer::SyncChangeList sync_list;
InsertAddPinChange(&sync_list, 0, extension1_->id());
InsertAddPinChange(&sync_list, 1, app_constants::kChromeAppId);
InsertAddPinChange(&sync_list, 2, ash::kGmailAppId);
SendPinChanges(sync_list, /*reset_pin_model=*/true);
base::Value::List policy_value;
// extension 2 4 are pinned by policy
AppendPrefValue(policy_value, extension2_->id());
AppendPrefValue(policy_value, google_docs_install_url_v2.spec());
profile()->GetTestingPrefService()->SetManagedPref(
prefs::kPolicyPinnedLauncherApps, base::Value(policy_value.Clone()));
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
// 2 is not pinned as it's not installed
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_TRUE(shelf_controller_->IsAppPinned(ash::kGmailAppId));
EXPECT_TRUE(shelf_controller_->IsAppPinned(ash::kGoogleDocsAppId));
// install extension 2 and check
AddExtension(extension2_.get());
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension2_->id()));
// Check user can manually pin or unpin these apps
EXPECT_EQ(AppListControllerDelegate::PIN_EDITABLE,
GetPinnableForAppID(extension1_->id(), profile()));
EXPECT_EQ(AppListControllerDelegate::PIN_FIXED,
GetPinnableForAppID(extension2_->id(), profile()));
EXPECT_EQ(AppListControllerDelegate::PIN_EDITABLE,
GetPinnableForAppID(ash::kGmailAppId, profile()));
EXPECT_EQ(AppListControllerDelegate::PIN_FIXED,
GetPinnableForAppID(ash::kGoogleDocsAppId, profile()));
// Check the order of shelf pinned apps
EXPECT_EQ("App2, Doc, App1, Chrome, Gmail", GetPinnedAppStatus());
}
// Check that the restoration of shelf items is happening in the same order
// as the user has pinned them (on another system) when they are synced reverse
// order.
TEST_F(ChromeShelfControllerTest, RestorePreinstalledAppsReverseOrder) {
InitShelfController();
syncer::SyncChangeList sync_list;
InsertAddPinChange(&sync_list, 0, extension1_->id());
InsertAddPinChange(&sync_list, 1, extension2_->id());
InsertAddPinChange(&sync_list, 2, ash::kGmailAppId);
SendPinChanges(sync_list, true);
// The model should only contain the browser shortcut and app list items.
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(ash::kGmailAppId));
EXPECT_EQ("Chrome", GetPinnedAppStatus());
// Installing Gmail should add it to the shelf - behind the
// chrome icon.
ash::ShelfItem item;
AddWebApp(ash::kGmailAppId);
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_EQ("Chrome, Gmail", GetPinnedAppStatus());
// Installing |extension2_| should add it to the shelf - behind the
// chrome icon, but in first location.
AddExtension(extension2_.get());
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_EQ("Chrome, App2, Gmail", GetPinnedAppStatus());
// Installing |extension1_| should add it to the shelf - behind the
// chrome icon, but in first location.
AddExtension(extension1_.get());
EXPECT_EQ("Chrome, App1, App2, Gmail", GetPinnedAppStatus());
}
// Check that the restoration of shelf items is happening in the same order
// as the user has pinned them (on another system) when they are synced random
// order.
TEST_F(ChromeShelfControllerTest, RestorePreinstalledAppsRandomOrder) {
InitShelfController();
syncer::SyncChangeList sync_list;
InsertAddPinChange(&sync_list, 0, extension1_->id());
InsertAddPinChange(&sync_list, 1, extension2_->id());
InsertAddPinChange(&sync_list, 2, ash::kGmailAppId);
SendPinChanges(sync_list, true);
// The model should only contain the browser shortcut and app list items.
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(ash::kGmailAppId));
EXPECT_EQ("Chrome", GetPinnedAppStatus());
// Installing |extension2_| should add it to the shelf - behind the
// chrome icon.
AddExtension(extension2_.get());
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(ash::kGmailAppId));
EXPECT_EQ("Chrome, App2", GetPinnedAppStatus());
// Installing |extension1_| should add it to the shelf - behind the
// chrome icon, but in first location.
AddExtension(extension1_.get());
EXPECT_FALSE(shelf_controller_->IsAppPinned(ash::kGmailAppId));
EXPECT_EQ("Chrome, App1, App2", GetPinnedAppStatus());
// Installing Gmail should add it to the shelf - behind the chrome icon,
// but in first location.
AddWebApp(ash::kGmailAppId);
EXPECT_EQ("Chrome, App1, App2, Gmail", GetPinnedAppStatus());
}
// Check that the restoration of shelf items is happening in the same order
// as the user has pinned / moved them (on another system) when they are synced
// random order - including the chrome icon.
TEST_F(ChromeShelfControllerTest,
RestorePreinstalledAppsRandomOrderChromeMoved) {
InitShelfController();
syncer::SyncChangeList sync_list;
InsertAddPinChange(&sync_list, 0, extension1_->id());
InsertAddPinChange(&sync_list, 1, app_constants::kChromeAppId);
InsertAddPinChange(&sync_list, 2, extension2_->id());
InsertAddPinChange(&sync_list, 3, ash::kGmailAppId);
SendPinChanges(sync_list, true);
// The model should only contain the browser shortcut and app list items.
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(ash::kGmailAppId));
EXPECT_EQ("Chrome", GetPinnedAppStatus());
// Installing |extension2_| should add it to the shelf - behind the
// chrome icon.
ash::ShelfItem item;
AddExtension(extension2_.get());
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(ash::kGmailAppId));
EXPECT_EQ("Chrome, App2", GetPinnedAppStatus());
// Installing |extension1_| should add it to the shelf - behind the
// chrome icon, but in first location.
AddExtension(extension1_.get());
EXPECT_FALSE(shelf_controller_->IsAppPinned(ash::kGmailAppId));
EXPECT_EQ("App1, Chrome, App2", GetPinnedAppStatus());
// Installing Gmail should add it to the shelf - behind the chrome icon,
// but in first location.
AddWebApp(ash::kGmailAppId);
EXPECT_EQ("App1, Chrome, App2, Gmail", GetPinnedAppStatus());
}
// Check that syncing to a different state does the correct thing.
TEST_F(ChromeShelfControllerTest, RestorePreinstalledAppsResyncOrder) {
InitShelfController();
syncer::SyncChangeList sync_list0;
InsertAddPinChange(&sync_list0, 0, extension1_->id());
InsertAddPinChange(&sync_list0, 1, extension2_->id());
InsertAddPinChange(&sync_list0, 2, ash::kGmailAppId);
SendPinChanges(sync_list0, true);
// The shelf layout has always one static item at the beginning (App List).
AddExtension(extension2_.get());
EXPECT_EQ("Chrome, App2", GetPinnedAppStatus());
AddExtension(extension1_.get());
EXPECT_EQ("Chrome, App1, App2", GetPinnedAppStatus());
AddWebApp(ash::kGmailAppId);
EXPECT_EQ("Chrome, App1, App2, Gmail", GetPinnedAppStatus());
// Change the order with increasing chrome position and decreasing position.
syncer::SyncChangeList sync_list1;
InsertAddPinChange(&sync_list1, 0, ash::kGmailAppId);
InsertAddPinChange(&sync_list1, 1, extension1_->id());
InsertAddPinChange(&sync_list1, 2, extension2_->id());
InsertAddPinChange(&sync_list1, 3, app_constants::kChromeAppId);
SendPinChanges(sync_list1, true);
EXPECT_EQ("Gmail, App1, App2, Chrome", GetPinnedAppStatus());
syncer::SyncChangeList sync_list2;
InsertAddPinChange(&sync_list2, 0, extension2_->id());
InsertAddPinChange(&sync_list2, 1, ash::kGmailAppId);
InsertAddPinChange(&sync_list2, 2, app_constants::kChromeAppId);
InsertAddPinChange(&sync_list2, 3, extension1_->id());
SendPinChanges(sync_list2, true);
EXPECT_EQ("App2, Gmail, Chrome, App1", GetPinnedAppStatus());
// Check that the chrome icon can also be at the first possible location.
syncer::SyncChangeList sync_list3;
InsertAddPinChange(&sync_list3, 0, ash::kGmailAppId);
InsertAddPinChange(&sync_list3, 1, extension2_->id());
InsertAddPinChange(&sync_list3, 2, extension1_->id());
SendPinChanges(sync_list3, true);
EXPECT_EQ("Chrome, Gmail, App2, App1", GetPinnedAppStatus());
// Check that uninstalling of extensions works as expected.
UninstallExtension(extension1_->id(),
extensions::UninstallReason::UNINSTALL_REASON_FOR_TESTING);
EXPECT_EQ("Chrome, Gmail, App2", GetPinnedAppStatus());
// Check that an update of an extension does not crash the system.
UnloadExtension(extension2_->id(), UnloadedExtensionReason::UPDATE);
EXPECT_EQ("Chrome, Gmail, App2", GetPinnedAppStatus());
}
// Test the V1 app interaction flow: run it, activate it, close it.
TEST_F(ChromeShelfControllerTest, V1AppRunActivateClose) {
InitShelfController();
// The model should only contain the browser shortcut item.
EXPECT_EQ(1, model_->item_count());
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_EQ(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
// Reporting that the app is running should create a new shelf item.
shelf_controller_->SetAppStatus(extension1_->id(), ash::STATUS_RUNNING);
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ(ash::TYPE_APP, model_->items()[1].type);
EXPECT_EQ(ash::STATUS_RUNNING, model_->items()[1].status);
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_NE(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
// Reporting that the app is running again should have no effect.
shelf_controller_->SetAppStatus(extension1_->id(), ash::STATUS_RUNNING);
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ(ash::STATUS_RUNNING, model_->items()[1].status);
// Reporting that the app is closed should remove its shelf item.
shelf_controller_->SetAppStatus(extension1_->id(), ash::STATUS_CLOSED);
EXPECT_EQ(1, model_->item_count());
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_EQ(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
// Reporting that the app is closed again should have no effect.
shelf_controller_->SetAppStatus(extension1_->id(), ash::STATUS_CLOSED);
EXPECT_EQ(1, model_->item_count());
}
// Test the V1 app interaction flow: pin it, run it, close it, unpin it.
TEST_F(ChromeShelfControllerTest, V1AppPinRunCloseUnpin) {
InitShelfController();
// The model should only contain the browser shortcut.
EXPECT_EQ(1, model_->item_count());
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_EQ(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
// Pinning the app should create a new shelf item.
PinAppWithIDToShelf(extension1_->id());
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[1].type);
EXPECT_EQ(ash::STATUS_CLOSED, model_->items()[1].status);
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_NE(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
// Reporting that the app is running should just update the existing item.
shelf_controller_->SetAppStatus(extension1_->id(), ash::STATUS_RUNNING);
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[1].type);
EXPECT_EQ(ash::STATUS_RUNNING, model_->items()[1].status);
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_NE(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
// Reporting that the app is closed should just update the existing item.
shelf_controller_->SetAppStatus(extension1_->id(), ash::STATUS_CLOSED);
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[1].type);
EXPECT_EQ(ash::STATUS_CLOSED, model_->items()[1].status);
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_NE(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
// Unpinning the app should remove its shelf item.
shelf_controller_->UnpinAppWithID(extension1_->id());
EXPECT_EQ(1, model_->item_count());
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_EQ(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
}
// Test the V1 app interaction flow: run it, pin it, close it, unpin it.
TEST_F(ChromeShelfControllerTest, V1AppRunPinCloseUnpin) {
// Set the app type of extension1_ to a chrome app, as the default unknown app
// type is not allowed to be pinned.
std::vector<apps::AppPtr> apps;
apps::AppPtr app =
std::make_unique<apps::App>(apps::AppType::kChromeApp, extension1_->id());
apps.push_back(std::move(app));
apps::AppServiceProxyFactory::GetForProfile(profile())->OnApps(
std::move(apps), apps::AppType::kChromeApp,
/*should_notify_initialized=*/false);
InitShelfController();
// The model should only contain the browser shortcut.
EXPECT_EQ(1, model_->item_count());
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_EQ(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
// Reporting that the app is running should create a new shelf item.
shelf_controller_->SetAppStatus(extension1_->id(), ash::STATUS_RUNNING);
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ(ash::TYPE_APP, model_->items()[1].type);
EXPECT_EQ(ash::STATUS_RUNNING, model_->items()[1].status);
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_NE(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
// Pinning the app should just update the existing item.
PinAppWithIDToShelf(extension1_->id());
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[1].type);
EXPECT_EQ(ash::STATUS_RUNNING, model_->items()[1].status);
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_NE(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
// Reporting that the app is closed should just update the existing item.
shelf_controller_->SetAppStatus(extension1_->id(), ash::STATUS_CLOSED);
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[1].type);
EXPECT_EQ(ash::STATUS_CLOSED, model_->items()[1].status);
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_NE(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
// Unpinning the app should remove its shelf item.
shelf_controller_->UnpinAppWithID(extension1_->id());
EXPECT_EQ(1, model_->item_count());
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_EQ(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
}
// Test the V1 app interaction flow: pin it, run it, unpin it, close it.
TEST_F(ChromeShelfControllerTest, V1AppPinRunUnpinClose) {
InitShelfController();
// The model should only contain the browser shortcut item.
EXPECT_EQ(1, model_->item_count());
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_EQ(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
// Pinning the app should create a new shelf item.
PinAppWithIDToShelf(extension1_->id());
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[1].type);
EXPECT_EQ(ash::STATUS_CLOSED, model_->items()[1].status);
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_NE(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
// Reporting that the app is running should just update the existing item.
shelf_controller_->SetAppStatus(extension1_->id(), ash::STATUS_RUNNING);
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[1].type);
EXPECT_EQ(ash::STATUS_RUNNING, model_->items()[1].status);
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_NE(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
// Unpinning the app should just update the existing item.
shelf_controller_->UnpinAppWithID(extension1_->id());
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ(ash::TYPE_APP, model_->items()[1].type);
EXPECT_EQ(ash::STATUS_RUNNING, model_->items()[1].status);
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_NE(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
// Reporting that the app is closed should remove its shelf item.
shelf_controller_->SetAppStatus(extension1_->id(), ash::STATUS_CLOSED);
EXPECT_EQ(1, model_->item_count());
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_EQ(nullptr,
shelf_controller_->GetItem(ash::ShelfID(extension1_->id())));
}
// Ensure unpinned V1 app ordering is properly restored after user changes.
TEST_F(ChromeShelfControllerTest, CheckRunningV1AppOrder) {
InitShelfController();
// The model should only contain the browser shortcut item.
EXPECT_EQ(1, model_->item_count());
// Add a few running applications.
shelf_controller_->SetAppStatus(extension1_->id(), ash::STATUS_RUNNING);
shelf_controller_->SetAppStatus(extension2_->id(), ash::STATUS_RUNNING);
shelf_controller_->SetAppStatus(ash::kGmailAppId, ash::STATUS_RUNNING);
EXPECT_EQ(4, model_->item_count());
// Note that this not only checks the order of applications but also the
// running type.
EXPECT_EQ("Chrome, app1, app2, gmail", GetPinnedAppStatus());
// Remember the current order of applications for the current user.
const AccountId& current_account_id =
multi_user_util::GetAccountIdFromProfile(profile());
RememberUnpinnedRunningApplicationOrder();
// Switch some items and check that restoring a user which was not yet
// remembered changes nothing.
model_->Move(1, 2);
EXPECT_EQ("Chrome, app2, app1, gmail", GetPinnedAppStatus());
const AccountId second_fake_account_id(
AccountId::FromUserEmail("second-fake-user@fake.com"));
RestoreUnpinnedRunningApplicationOrder(second_fake_account_id);
EXPECT_EQ("Chrome, app2, app1, gmail", GetPinnedAppStatus());
// Restoring the stored user should however do the right thing.
RestoreUnpinnedRunningApplicationOrder(current_account_id);
EXPECT_EQ("Chrome, app1, app2, gmail", GetPinnedAppStatus());
// Switch again some items and even delete one - making sure that the missing
// item gets properly handled.
model_->Move(2, 3);
shelf_controller_->SetAppStatus(extension1_->id(), ash::STATUS_CLOSED);
EXPECT_EQ("Chrome, gmail, app2", GetPinnedAppStatus());
RestoreUnpinnedRunningApplicationOrder(current_account_id);
EXPECT_EQ("Chrome, app2, gmail", GetPinnedAppStatus());
// Check that removing more items does not crash and changes nothing.
shelf_controller_->SetAppStatus(extension2_->id(), ash::STATUS_CLOSED);
RestoreUnpinnedRunningApplicationOrder(current_account_id);
EXPECT_EQ("Chrome, gmail", GetPinnedAppStatus());
shelf_controller_->SetAppStatus(ash::kGmailAppId, ash::STATUS_CLOSED);
RestoreUnpinnedRunningApplicationOrder(current_account_id);
EXPECT_EQ("Chrome", GetPinnedAppStatus());
}
TEST_F(ChromeShelfControllerWithArcTest, ArcDeferredLaunch) {
InitShelfController();
const arc::mojom::ShortcutInfo& shortcut = arc_test_.fake_shortcuts()[0];
const std::string arc_app_id1 =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[0]);
const std::string arc_app_id2 =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[1]);
const std::string arc_app_id3 =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[2]);
const std::string arc_shortcut_id = ArcAppTest::GetAppId(shortcut);
SendListOfArcApps();
SendListOfArcShortcuts();
arc_test_.StopArcInstance();
const ash::ShelfID shelf_id_app_1(arc_app_id1);
const ash::ShelfID shelf_id_app_2(arc_app_id2);
const ash::ShelfID shelf_id_app_3(arc_app_id3);
const ash::ShelfID shelf_id_shortcut(arc_shortcut_id);
EXPECT_FALSE(shelf_controller_->GetItem(shelf_id_app_1));
EXPECT_FALSE(shelf_controller_->GetItem(shelf_id_app_2));
EXPECT_FALSE(shelf_controller_->GetItem(shelf_id_app_3));
EXPECT_FALSE(shelf_controller_->GetItem(shelf_id_shortcut));
arc::LaunchApp(profile(), arc_app_id1, ui::EF_LEFT_MOUSE_BUTTON,
arc::UserInteractionType::NOT_USER_INITIATED);
arc::LaunchApp(profile(), arc_app_id1, ui::EF_LEFT_MOUSE_BUTTON,
arc::UserInteractionType::NOT_USER_INITIATED);
arc::LaunchApp(profile(), arc_app_id2, ui::EF_LEFT_MOUSE_BUTTON,
arc::UserInteractionType::NOT_USER_INITIATED);
arc::LaunchApp(profile(), arc_app_id3, ui::EF_LEFT_MOUSE_BUTTON,
arc::UserInteractionType::NOT_USER_INITIATED);
arc::LaunchApp(profile(), arc_shortcut_id, ui::EF_LEFT_MOUSE_BUTTON,
arc::UserInteractionType::NOT_USER_INITIATED);
EXPECT_TRUE(shelf_controller_->GetItem(shelf_id_app_1));
EXPECT_TRUE(shelf_controller_->GetItem(shelf_id_app_2));
EXPECT_TRUE(shelf_controller_->GetItem(shelf_id_app_3));
EXPECT_TRUE(shelf_controller_->GetItem(shelf_id_shortcut));
// We activated arc_app_id1 twice but expect one close for item controller
// stops launching request.
ash::ShelfItemDelegate* item_delegate =
model_->GetShelfItemDelegate(shelf_id_app_1);
ASSERT_NE(nullptr, item_delegate);
item_delegate->Close();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(shelf_controller_->GetItem(shelf_id_app_1));
EXPECT_TRUE(shelf_controller_->GetItem(shelf_id_app_2));
EXPECT_TRUE(shelf_controller_->GetItem(shelf_id_app_3));
EXPECT_TRUE(shelf_controller_->GetItem(shelf_id_shortcut));
arc_test_.RestartArcInstance();
SendListOfArcApps();
base::RunLoop().RunUntilIdle();
// Now spinner controllers should go away together with shelf items and ARC
// app instance should receive request for launching apps and shortcuts.
EXPECT_FALSE(shelf_controller_->GetItem(shelf_id_app_1));
EXPECT_FALSE(shelf_controller_->GetItem(shelf_id_app_2));
EXPECT_FALSE(shelf_controller_->GetItem(shelf_id_app_3));
EXPECT_FALSE(shelf_controller_->GetItem(shelf_id_shortcut));
EXPECT_EQ(0U, arc_test_.app_instance()->launch_requests().size());
ASSERT_EQ(3U, arc_test_.app_instance()->launch_intents().size());
EXPECT_GE(arc_test_.app_instance()->launch_intents()[0].find(
"component=fake.app.1/.activity;"),
0u);
EXPECT_GE(arc_test_.app_instance()->launch_intents()[0].find(
"S.org.chromium.arc.request.deferred.start="),
0u);
EXPECT_GE(arc_test_.app_instance()->launch_intents()[1].find(
"component=fake.app.2/.activity;"),
0u);
EXPECT_GE(arc_test_.app_instance()->launch_intents()[1].find(
"S.org.chromium.arc.request.deferred.start="),
0u);
EXPECT_EQ(arc_test_.app_instance()->launch_intents()[2].c_str(),
shortcut.intent_uri);
}
// Launch is canceled in case app becomes suspended.
TEST_F(ChromeShelfControllerWithArcTest, ArcDeferredLaunchForSuspendedApp) {
InitShelfController();
// Register app first.
std::vector<arc::mojom::AppInfoPtr> apps;
apps.emplace_back(arc_test_.fake_apps()[0]->Clone());
const std::string app_id = ArcAppTest::GetAppId(*apps[0]);
arc_test_.app_instance()->SendRefreshAppList(apps);
arc_test_.StopArcInstance();
// Restart ARC
arc_test_.RestartArcInstance();
// Deferred controller should be allocated on start.
const ash::ShelfID shelf_id(app_id);
arc::LaunchApp(profile(), app_id, ui::EF_LEFT_MOUSE_BUTTON,
arc::UserInteractionType::NOT_USER_INITIATED);
EXPECT_TRUE(shelf_controller_->GetItem(shelf_id));
// Send app with suspended state.
apps[0]->suspended = true;
arc_test_.app_instance()->SendRefreshAppList(apps);
// Controller automatically closed.
EXPECT_FALSE(shelf_controller_->GetItem(shelf_id));
// And no launch request issued.
EXPECT_TRUE(arc_test_.app_instance()->launch_requests().empty());
}
// Ensure the spinner controller does not override the active app controller
// (crbug.com/701152).
TEST_F(ChromeShelfControllerWithArcTest, ArcDeferredLaunchForActiveApp) {
InitShelfController();
SendListOfArcApps();
arc_test_.StopArcInstance();
const std::string app_id = ArcAppTest::GetAppId(*arc_test_.fake_apps()[0]);
PinAppWithIDToShelf(app_id);
EXPECT_TRUE(shelf_controller_->IsAppPinned(app_id));
const ash::ShelfID shelf_id(app_id);
const ash::ShelfItem* item = shelf_controller_->GetItem(shelf_id);
ASSERT_NE(nullptr, item);
EXPECT_EQ(ash::STATUS_CLOSED, item->status);
EXPECT_EQ(ash::TYPE_PINNED_APP, item->type);
// Play Store app is ARC app that might be represented by native Chrome
// platform app.
model_->ReplaceShelfItemDelegate(
shelf_id,
std::make_unique<AppServiceAppWindowShelfItemController>(
shelf_id, shelf_controller_->app_service_app_window_controller()));
shelf_controller_->SetItemStatus(shelf_id, ash::STATUS_RUNNING);
// This launch request should be ignored in case of active app.
arc::LaunchApp(profile(), app_id, ui::EF_LEFT_MOUSE_BUTTON,
arc::UserInteractionType::NOT_USER_INITIATED);
EXPECT_FALSE(shelf_controller_->GetShelfSpinnerController()->HasApp(app_id));
// Closing the app should leave a pinned but closed shelf item shortcut.
shelf_controller_->ReplaceWithAppShortcutOrRemove(shelf_id);
item = shelf_controller_->GetItem(shelf_id);
ASSERT_NE(nullptr, item);
EXPECT_EQ(ash::STATUS_CLOSED, item->status);
EXPECT_EQ(ash::TYPE_PINNED_APP, item->type);
// Now launch request should not be ignored.
arc::LaunchApp(profile(), app_id, ui::EF_LEFT_MOUSE_BUTTON,
arc::UserInteractionType::NOT_USER_INITIATED);
EXPECT_TRUE(shelf_controller_->GetShelfSpinnerController()->HasApp(app_id));
}
// TODO(crbug.com/915840): this test is flakey and/or often crashes.
TEST_F(ChromeShelfControllerMultiProfileWithArcTest, DISABLED_ArcMultiUser) {
SendListOfArcApps();
InitShelfController();
SetShelfControllerHelper(new TestShelfControllerHelper);
// App1 exists all the time.
// App2 is created when primary user is active and destroyed when secondary
// user is active.
// Gmail created when secondary user is active.
constexpr char kUser2[] = "user2@example.com";
const GaiaId kFakeGaia2("fakegaia2");
TestingProfile* profile2 = CreateMultiUserProfile(kUser2, kFakeGaia2);
const AccountId account_id(
multi_user_util::GetAccountIdFromProfile(profile()));
const AccountId account_id2(
multi_user_util::GetAccountIdFromProfile(profile2));
const std::string arc_app_id1 =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[0]);
const std::string arc_app_id2 =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[1]);
const std::string arc_app_id3 =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[2]);
std::string window_app_id1("org.chromium.arc.1");
views::Widget* arc_window1 = CreateArcWindow(window_app_id1);
arc_test_.app_instance()->SendTaskCreated(1, *arc_test_.fake_apps()[0],
std::string());
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id1)));
std::string window_app_id2("org.chromium.arc.2");
views::Widget* arc_window2 = CreateArcWindow(window_app_id2);
arc_test_.app_instance()->SendTaskCreated(2, *arc_test_.fake_apps()[1],
std::string());
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id2)));
shelf_controller_->SetProfileForTest(profile2);
SwitchActiveUserByAccountId(account_id2);
EXPECT_FALSE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id1)));
EXPECT_FALSE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id2)));
std::string window_app_id3("org.chromium.arc.3");
views::Widget* arc_window3 = CreateArcWindow(window_app_id3);
arc_test_.app_instance()->SendTaskCreated(3, *arc_test_.fake_apps()[2],
std::string());
EXPECT_FALSE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id3)));
arc_window2->CloseNow();
arc_test_.app_instance()->SendTaskDestroyed(2);
shelf_controller_->SetProfileForTest(profile());
SwitchActiveUserByAccountId(account_id);
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id1)));
EXPECT_FALSE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id2)));
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id3)));
// Close active window to let test passes.
arc_window1->CloseNow();
arc_window3->CloseNow();
}
TEST_F(ChromeShelfControllerWithArcTest, ArcRunningApp) {
InitShelfController();
const std::string arc_app_id =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[0]);
SendListOfArcApps();
EXPECT_FALSE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id)));
// Normal flow, create/destroy tasks.
std::string window_app_id1("org.chromium.arc.1");
std::string window_app_id2("org.chromium.arc.2");
std::string window_app_id3("org.chromium.arc.3");
CreateArcWindow(window_app_id1);
arc_test_.app_instance()->SendTaskCreated(1, *arc_test_.fake_apps()[0],
std::string());
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id)));
CreateArcWindow(window_app_id2);
arc_test_.app_instance()->SendTaskCreated(2, *arc_test_.fake_apps()[0],
std::string());
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id)));
arc_test_.app_instance()->SendTaskDestroyed(1);
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id)));
arc_test_.app_instance()->SendTaskDestroyed(2);
EXPECT_FALSE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id)));
// Stopping bridge removes apps.
CreateArcWindow(window_app_id3);
arc_test_.app_instance()->SendTaskCreated(3, *arc_test_.fake_apps()[0],
std::string());
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id)));
arc_test_.StopArcInstance();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id)));
}
// Test race creation/deletion of ARC app.
// TODO(khmel): Remove after moving everything to wayland protocol.
TEST_F(ChromeShelfControllerWithArcTest, ArcRaceCreateClose) {
InitShelfController();
const std::string arc_app_id1 =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[0]);
const std::string arc_app_id2 =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[1]);
SendListOfArcApps();
// ARC window created before and closed after mojom notification.
std::string window_app_id1("org.chromium.arc.1");
views::Widget* arc_window = CreateArcWindow(window_app_id1);
EXPECT_FALSE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id1)));
ASSERT_TRUE(arc_window);
arc_test_.app_instance()->SendTaskCreated(1, *arc_test_.fake_apps()[0],
std::string());
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id1)));
arc_test_.app_instance()->SendTaskDestroyed(1);
EXPECT_FALSE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id1)));
arc_window->Close();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id1)));
// ARC window created after and closed before mojom notification.
std::string window_app_id2("org.chromium.arc.2");
arc_test_.app_instance()->SendTaskCreated(2, *arc_test_.fake_apps()[1],
std::string());
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id2)));
arc_window = CreateArcWindow(window_app_id2);
ASSERT_TRUE(arc_window);
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id2)));
arc_window->Close();
base::RunLoop().RunUntilIdle();
// Closing window does not close shelf item. It is closed on task destroy.
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id2)));
arc_test_.app_instance()->SendTaskDestroyed(2);
EXPECT_FALSE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id2)));
}
TEST_F(ChromeShelfControllerWithArcTest, ArcWindowRecreation) {
InitShelfController();
const std::string arc_app_id =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[0]);
SendListOfArcApps();
std::string window_app_id("org.chromium.arc.1");
views::Widget* arc_window = CreateArcWindow(window_app_id);
ASSERT_TRUE(arc_window);
arc_test_.app_instance()->SendTaskCreated(1, *arc_test_.fake_apps()[0],
std::string());
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id)));
for (int i = 0; i < 3; ++i) {
arc_window->Close();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id)));
arc_window = CreateArcWindow(window_app_id);
ASSERT_TRUE(arc_window);
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID(arc_app_id)));
}
}
// Verifies edge cases when Extension app item controller may be overwritten by
// ARC app item controller and vice versa. This should not happen in normal
// cases but in case of ARC boot failure this may lead to such situation. This
// test verifies that dynamic change of app item controllers is safe. See more
// crbug.com/770005.
TEST_F(ChromeShelfControllerWithArcTest, OverrideAppItemController) {
extension_registrar_->AddExtension(arc_support_host_.get());
InitShelfController();
SendListOfArcApps();
arc::mojom::AppInfoPtr app_info = CreateAppInfo(
"Play Store", arc::kPlayStoreActivity, arc::kPlayStorePackage);
EXPECT_EQ(arc::kPlayStoreAppId, AddArcAppAndShortcut(*app_info));
std::string window_app_id("org.chromium.arc.1");
const ash::ShelfID play_store_shelf_id(arc::kPlayStoreAppId);
shelf_controller_->UnpinAppWithID(arc::kPlayStoreAppId);
EXPECT_FALSE(shelf_controller_->GetItem(play_store_shelf_id));
// Try 4 different scenarios with different creation and destroying orders.
// Scenario 1: Create OptIn, Play Store. Destroy OptIn, Play Store.
{
std::unique_ptr<V2App> play_store_optin =
std::make_unique<V2App>(profile(), arc_support_host_.get(),
extensions::AppWindow::WINDOW_TYPE_DEFAULT);
EXPECT_TRUE(shelf_controller_->GetItem(play_store_shelf_id));
views::Widget* arc_window = CreateArcWindow(window_app_id);
ASSERT_TRUE(arc_window);
arc_test_.app_instance()->SendTaskCreated(1, *app_info, std::string());
EXPECT_TRUE(shelf_controller_->GetItem(play_store_shelf_id));
play_store_optin.reset();
EXPECT_TRUE(shelf_controller_->GetItem(play_store_shelf_id));
arc_window->CloseNow();
arc_test_.app_instance()->SendTaskDestroyed(1);
EXPECT_FALSE(shelf_controller_->GetItem(play_store_shelf_id));
}
// Scenario 2: Create OptIn, Play Store. Destroy Play Store, OptIn.
{
std::unique_ptr<V2App> play_store_optin =
std::make_unique<V2App>(profile(), arc_support_host_.get(),
extensions::AppWindow::WINDOW_TYPE_DEFAULT);
EXPECT_TRUE(shelf_controller_->GetItem(play_store_shelf_id));
views::Widget* arc_window = CreateArcWindow(window_app_id);
ASSERT_TRUE(arc_window);
arc_test_.app_instance()->SendTaskCreated(1, *app_info, std::string());
EXPECT_TRUE(shelf_controller_->GetItem(play_store_shelf_id));
arc_window->CloseNow();
arc_test_.app_instance()->SendTaskDestroyed(1);
EXPECT_FALSE(shelf_controller_->GetItem(play_store_shelf_id));
play_store_optin.reset();
EXPECT_FALSE(shelf_controller_->GetItem(play_store_shelf_id));
}
// Scenario 3: Create Play Store, OptIn. Destroy OptIn, Play Store.
{
views::Widget* arc_window = CreateArcWindow(window_app_id);
ASSERT_TRUE(arc_window);
arc_test_.app_instance()->SendTaskCreated(1, *app_info, std::string());
EXPECT_TRUE(shelf_controller_->GetItem(play_store_shelf_id));
std::unique_ptr<V2App> play_store_optin =
std::make_unique<V2App>(profile(), arc_support_host_.get(),
extensions::AppWindow::WINDOW_TYPE_DEFAULT);
EXPECT_TRUE(shelf_controller_->GetItem(play_store_shelf_id));
play_store_optin.reset();
EXPECT_FALSE(shelf_controller_->GetItem(play_store_shelf_id));
arc_window->CloseNow();
arc_test_.app_instance()->SendTaskDestroyed(1);
EXPECT_FALSE(shelf_controller_->GetItem(play_store_shelf_id));
}
// Scenario 4: Create Play Store, OptIn. Destroy Play Store, OptIn.
{
views::Widget* arc_window = CreateArcWindow(window_app_id);
ASSERT_TRUE(arc_window);
arc_test_.app_instance()->SendTaskCreated(1, *app_info, std::string());
EXPECT_TRUE(shelf_controller_->GetItem(play_store_shelf_id));
std::unique_ptr<V2App> play_store_optin =
std::make_unique<V2App>(profile(), arc_support_host_.get(),
extensions::AppWindow::WINDOW_TYPE_DEFAULT);
EXPECT_TRUE(shelf_controller_->GetItem(play_store_shelf_id));
arc_window->CloseNow();
arc_test_.app_instance()->SendTaskDestroyed(1);
EXPECT_TRUE(shelf_controller_->GetItem(play_store_shelf_id));
play_store_optin.reset();
EXPECT_FALSE(shelf_controller_->GetItem(play_store_shelf_id));
}
}
// Validate that ARC app is pinned correctly and pin is removed automatically
// once app is uninstalled.
TEST_F(ChromeShelfControllerWithArcTest, ArcAppPin) {
InitShelfController();
const std::string arc_app_id =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[0]);
SendListOfArcApps();
extension_registrar_->AddExtension(extension1_.get());
extension_registrar_->AddExtension(extension2_.get());
// Allow async callbacks to run.
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(arc_app_id));
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension2_->id()));
PinAppWithIDToShelf(extension1_->id());
PinAppWithIDToShelf(arc_app_id);
PinAppWithIDToShelf(extension2_->id());
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_TRUE(shelf_controller_->IsAppPinned(arc_app_id));
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_EQ("Chrome, App1, Fake App 1, App2", GetPinnedAppStatus());
UninstallArcApps();
// Allow async callbacks to run.
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(shelf_controller_->IsAppPinned(arc_app_id));
EXPECT_EQ("Chrome, App1, App2", GetPinnedAppStatus());
SendListOfArcApps();
// Allow async callbacks to run.
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(shelf_controller_->IsAppPinned(arc_app_id));
EXPECT_EQ("Chrome, App1, App2", GetPinnedAppStatus());
// Opt-Out/Opt-In remove item from the shelf.
PinAppWithIDToShelf(arc_app_id);
EXPECT_EQ("Chrome, App1, App2, Fake App 1", GetPinnedAppStatus());
EnablePlayStore(false);
EXPECT_EQ("Chrome, App1, App2", GetPinnedAppStatus());
EnablePlayStore(true);
EXPECT_EQ("Chrome, App1, App2", GetPinnedAppStatus());
SendListOfArcApps();
// Allow async callbacks to run.
base::RunLoop().RunUntilIdle();
EXPECT_EQ("Chrome, App1, App2, Fake App 1", GetPinnedAppStatus());
}
// Validates that ARC app pins persist across OptOut/OptIn.
TEST_F(ChromeShelfControllerWithArcTest, ArcAppPinOptOutOptIn) {
InitShelfController();
const std::string arc_app_id1 =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[0]);
const std::string arc_app_id2 =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[1]);
SendListOfArcApps();
extension_registrar_->AddExtension(extension1_.get());
extension_registrar_->AddExtension(extension2_.get());
PinAppWithIDToShelf(extension1_->id());
PinAppWithIDToShelf(arc_app_id2);
PinAppWithIDToShelf(extension2_->id());
PinAppWithIDToShelf(arc_app_id1);
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_TRUE(shelf_controller_->IsAppPinned(arc_app_id1));
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_TRUE(shelf_controller_->IsAppPinned(arc_app_id2));
EXPECT_EQ("Chrome, App1, Fake App 2, App2, Fake App 1", GetPinnedAppStatus());
EnablePlayStore(false);
EXPECT_EQ("Chrome, App1, App2", GetPinnedAppStatus());
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(arc_app_id1));
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(arc_app_id2));
EnablePlayStore(true);
SendListOfArcApps();
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_TRUE(shelf_controller_->IsAppPinned(arc_app_id1));
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_TRUE(shelf_controller_->IsAppPinned(arc_app_id2));
EXPECT_EQ("Chrome, App1, Fake App 2, App2, Fake App 1", GetPinnedAppStatus());
}
TEST_F(ChromeShelfControllerWithArcTest, DISABLED_ArcCustomAppIcon) {
InitShelfController();
// Wait until other apps are updated to avoid race condition while accessing
// last updated item.
base::RunLoop().RunUntilIdle();
// Register fake ARC apps.
SendListOfArcApps();
// Use first fake ARC app for testing.
const std::string arc_app_id =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[0]);
const ash::ShelfID arc_shelf_id(arc_app_id);
// Generate icon for the testing app and use compressed png content as test
// input. Take shortcut to separate from default app icon.
auto icon = arc_test_.app_instance()->GenerateIconResponse(
extension_misc::EXTENSION_ICON_SMALL, false /* app_icon */);
ASSERT_TRUE(icon);
ASSERT_TRUE(icon->icon_png_data.has_value());
EXPECT_FALSE(icon->icon_png_data->empty());
std::string png_data(icon->icon_png_data->begin(),
icon->icon_png_data->end());
// Some input that represents invalid png content.
std::string invalid_png_data("aaaaaa");
EXPECT_FALSE(shelf_controller_->GetItem(arc_shelf_id));
std::string window_app_id1("org.chromium.arc.1");
std::string window_app_id2("org.chromium.arc.2");
views::Widget* window1 = CreateArcWindow(window_app_id1);
ASSERT_TRUE(window1 && window1->GetNativeWindow());
arc_test_.app_instance()->SendTaskCreated(1, *arc_test_.fake_apps()[0],
std::string());
views::Widget* window2 = CreateArcWindow(window_app_id2);
ASSERT_TRUE(window2 && window2->GetNativeWindow());
arc_test_.app_instance()->SendTaskCreated(2, *arc_test_.fake_apps()[0],
std::string());
EXPECT_TRUE(shelf_controller_->GetItem(arc_shelf_id));
ash::ShelfItemDelegate* item_delegate =
model_->GetShelfItemDelegate(arc_shelf_id);
ASSERT_TRUE(item_delegate);
base::RunLoop().RunUntilIdle();
auto get_icon = [=, this]() {
return *shelf_controller_->GetItem(arc_shelf_id)->image.bitmap();
};
const SkBitmap default_icon = get_icon();
// No custom icon set. Acitivating windows should not change icon.
window1->Activate();
EXPECT_TRUE(gfx::test::AreBitmapsEqual(default_icon, get_icon()));
window2->Activate();
EXPECT_TRUE(gfx::test::AreBitmapsEqual(default_icon, get_icon()));
// Set custom icon on active item. Icon should change to custom.
arc_test_.app_instance()->SendTaskDescription(2, std::string(), png_data);
const SkBitmap custom_icon = get_icon();
EXPECT_FALSE(gfx::test::AreBitmapsEqual(default_icon, custom_icon));
// Switch back to the item without custom icon. Icon should be changed to
// default.
window1->Activate();
EXPECT_TRUE(gfx::test::AreBitmapsEqual(default_icon, get_icon()));
// Test that setting an invalid icon should not change custom icon.
arc_test_.app_instance()->SendTaskDescription(1, std::string(), png_data);
EXPECT_TRUE(gfx::test::AreBitmapsEqual(custom_icon, get_icon()));
arc_test_.app_instance()->SendTaskDescription(1, std::string(),
invalid_png_data);
EXPECT_TRUE(gfx::test::AreBitmapsEqual(custom_icon, get_icon()));
// Check window removing with active custom icon. Reseting custom icon of
// inactive window doesn't reset shelf icon.
arc_test_.app_instance()->SendTaskDescription(2, std::string(),
std::string());
EXPECT_TRUE(gfx::test::AreBitmapsEqual(custom_icon, get_icon()));
// Set custom icon back to validate closing active window later.
arc_test_.app_instance()->SendTaskDescription(2, std::string(), png_data);
EXPECT_TRUE(gfx::test::AreBitmapsEqual(custom_icon, get_icon()));
// Reseting custom icon of active window resets shelf icon.
arc_test_.app_instance()->SendTaskDescription(1, std::string(),
std::string());
// Wait for default icon load.
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(gfx::test::AreBitmapsEqual(default_icon, get_icon()));
window1->CloseNow();
EXPECT_TRUE(gfx::test::AreBitmapsEqual(custom_icon, get_icon()));
}
TEST_F(ChromeShelfControllerWithArcTest, ArcWindowPackageName) {
InitShelfController();
SendListOfArcApps();
std::string window_app_id1("org.chromium.arc.1");
std::string window_app_id2("org.chromium.arc.2");
std::string window_app_id3("org.chromium.arc.3");
views::Widget* arc_window1 = CreateArcWindow(window_app_id1);
arc_test_.app_instance()->SendTaskCreated(1, *arc_test_.fake_apps()[0],
std::string());
const std::string* package_name1 =
arc_window1->GetNativeWindow()->GetProperty(ash::kArcPackageNameKey);
ASSERT_TRUE(package_name1);
EXPECT_EQ(*package_name1, arc_test_.fake_apps()[0]->package_name);
views::Widget* arc_window2 = CreateArcWindow(window_app_id2);
arc_test_.app_instance()->SendTaskCreated(2, *arc_test_.fake_apps()[1],
std::string());
const std::string* package_name2 =
arc_window2->GetNativeWindow()->GetProperty(ash::kArcPackageNameKey);
ASSERT_TRUE(package_name2);
EXPECT_EQ(*package_name2, arc_test_.fake_apps()[1]->package_name);
// Create another window with the same package name.
views::Widget* arc_window3 = CreateArcWindow(window_app_id3);
arc_test_.app_instance()->SendTaskCreated(3, *arc_test_.fake_apps()[1],
std::string());
const std::string* package_name3 =
arc_window3->GetNativeWindow()->GetProperty(ash::kArcPackageNameKey);
ASSERT_TRUE(package_name3);
EXPECT_EQ(*package_name3, arc_test_.fake_apps()[1]->package_name);
arc_window1->CloseNow();
arc_window2->CloseNow();
arc_window3->CloseNow();
}
// Check that with multi profile V1 apps are properly added / removed from the
// shelf.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest,
V1AppUpdateOnUserSwitch) {
// Create a browser item in the controller.
InitShelfController();
EXPECT_EQ(1, model_->item_count());
{
// Create a "windowed gmail app".
std::unique_ptr<V1App> v1_app(
CreateRunningV1App(profile(), extension_misc::kGmailAppId, kGmailUrl));
EXPECT_EQ(2, model_->item_count());
// After switching to a second user the item should be gone.
const char kUser2[] = "user2@example.com";
const GaiaId kFakeGaia2("fakegaia2");
TestingProfile* profile2 = CreateMultiUserProfile(kUser2, kFakeGaia2);
const AccountId account_id2(
multi_user_util::GetAccountIdFromProfile(profile2));
const AccountId account_id(
multi_user_util::GetAccountIdFromProfile(profile()));
SwitchActiveUserByAccountId(account_id2);
EXPECT_EQ(1, model_->item_count());
// After switching back the item should be back.
SwitchActiveUserByAccountId(account_id);
EXPECT_EQ(2, model_->item_count());
// Note we destroy now the gmail app with the closure end.
}
EXPECT_EQ(1, model_->item_count());
}
// Check edge cases with multi profile V1 apps in the shelf.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest,
V1AppUpdateOnUserSwitchEdgecases) {
// Create a browser item in the controller.
InitShelfController();
// First test: Create an app when the user is not active.
constexpr char kUser2[] = "user2@example.com";
const GaiaId kFakeGaia2("fakegaia2");
TestingProfile* profile2 = CreateMultiUserProfile(kUser2, kFakeGaia2);
const AccountId account_id2(
multi_user_util::GetAccountIdFromProfile(profile2));
const AccountId account_id(
multi_user_util::GetAccountIdFromProfile(profile()));
{
// Create a "windowed gmail app".
std::unique_ptr<V1App> v1_app(
CreateRunningV1App(profile2, extension_misc::kGmailAppId, kGmailUrl));
EXPECT_EQ(1, model_->item_count());
// However - switching to the user should show it.
SwitchActiveUserByAccountId(account_id2);
EXPECT_EQ(2, model_->item_count());
// Second test: Remove the app when the user is not active and see that it
// works.
SwitchActiveUserByAccountId(account_id);
EXPECT_EQ(1, model_->item_count());
// Note: the closure ends and the browser will go away.
}
EXPECT_EQ(1, model_->item_count());
SwitchActiveUserByAccountId(account_id2);
EXPECT_EQ(1, model_->item_count());
SwitchActiveUserByAccountId(account_id);
EXPECT_EQ(1, model_->item_count());
}
// Check edge case where a visiting V1 app gets closed (crbug.com/321374).
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest,
V1CloseOnVisitingDesktop) {
// Create a browser item in the controller.
InitShelfController();
// First create an app when the user is active.
constexpr char kUser2[] = "user2@example.com";
const GaiaId kFakeGaia2("fakegaia2");
TestingProfile* profile2 = CreateMultiUserProfile(kUser2, kFakeGaia2);
const AccountId account_id(
multi_user_util::GetAccountIdFromProfile(profile()));
const AccountId account_id2(
multi_user_util::GetAccountIdFromProfile(profile2));
{
// Create a "windowed gmail app".
std::unique_ptr<V1App> v1_app(CreateRunningV1App(
profile(), extension_misc::kGmailAppId, kGmailLaunchURL));
EXPECT_EQ(2, model_->item_count());
SwitchActiveUserByAccountId(account_id2);
EXPECT_EQ(1, model_->item_count());
}
// After the app was destroyed, switch back. (which caused already a crash).
SwitchActiveUserByAccountId(account_id);
// Create the same app again - which was also causing the crash.
EXPECT_EQ(1, model_->item_count());
{
// Create a "windowed gmail app".
std::unique_ptr<V1App> v1_app(CreateRunningV1App(
profile(), extension_misc::kGmailAppId, kGmailLaunchURL));
EXPECT_EQ(2, model_->item_count());
}
SwitchActiveUserByAccountId(account_id2);
EXPECT_EQ(1, model_->item_count());
}
// Check edge cases with multi profile V1 apps in the shelf.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest,
V1AppUpdateOnUserSwitchEdgecases2) {
// Create a browser item in the controller.
InitShelfController();
// First test: Create an app when the user is not active.
constexpr char kUser2[] = "user2@example.com";
const GaiaId kFakeGaia2("fakegaia2");
TestingProfile* profile2 = CreateMultiUserProfile(kUser2, kFakeGaia2);
const AccountId account_id(
multi_user_util::GetAccountIdFromProfile(profile()));
const AccountId account_id2(
multi_user_util::GetAccountIdFromProfile(profile2));
SwitchActiveUserByAccountId(account_id2);
{
// Create a "windowed gmail app".
std::unique_ptr<V1App> v1_app(
CreateRunningV1App(profile(), extension_misc::kGmailAppId, kGmailUrl));
EXPECT_EQ(1, model_->item_count());
// However - switching to the user should show it.
SwitchActiveUserByAccountId(account_id);
EXPECT_EQ(2, model_->item_count());
// Second test: Remove the app when the user is not active and see that it
// works.
SwitchActiveUserByAccountId(account_id2);
EXPECT_EQ(1, model_->item_count());
v1_app.reset();
}
EXPECT_EQ(1, model_->item_count());
SwitchActiveUserByAccountId(account_id);
EXPECT_EQ(1, model_->item_count());
SwitchActiveUserByAccountId(account_id2);
EXPECT_EQ(1, model_->item_count());
}
// Check that activating an item which is on another user's desktop, will bring
// it back.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest,
TestShelfActivationPullsBackWindow) {
// Create a browser item in the controller.
InitShelfController();
ash::MultiUserWindowManager* window_manager =
MultiUserWindowManagerHelper::GetWindowManager();
// Create a second test profile. The first is the one in profile() created in
// BrowserWithTestWindowTest::SetUp(). No need to add the profiles to the
// MultiUserWindowManagerHelper here. CreateMultiUserProfile() already does
// that.
TestingProfile* profile2 =
CreateMultiUserProfile("user2@example.com", GaiaId("fakegaia2"));
const AccountId current_user =
multi_user_util::GetAccountIdFromProfile(profile());
// Create a browser window with a native window for the current user.
std::unique_ptr<Browser> browser(
CreateBrowserWithTestWindowForProfile(profile()));
BrowserWindow* browser_window = browser->window();
aura::Window* window = browser_window->GetNativeWindow();
window_manager->SetWindowOwner(window, current_user);
// Check that an activation of the window on its owner's desktop does not
// change the visibility to another user.
shelf_controller_->ActivateWindowOrMinimizeIfActive(browser_window, false);
EXPECT_TRUE(IsWindowOnDesktopOfUser(window, current_user));
// Transfer the window to another user's desktop and check that activating it
// does pull it back to that user.
window_manager->ShowWindowForUser(
window, multi_user_util::GetAccountIdFromProfile(profile2));
EXPECT_FALSE(IsWindowOnDesktopOfUser(window, current_user));
shelf_controller_->ActivateWindowOrMinimizeIfActive(browser_window, false);
EXPECT_TRUE(IsWindowOnDesktopOfUser(window, current_user));
}
// Tests that web app icon is removed from shelf after user switch if the app is
// not installed by the new active user, even if the user has the URL associated
// with the app open in a tab.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest,
WebAppNotShownIfNotInstalledAfterUserSwitch) {
// Create a browser item in the controller.
InitShelfController();
constexpr char kUser2[] = "user2@example.com";
const GaiaId kFakeGaia2("fakegaia2");
TestingProfile* profile2 = CreateMultiUserProfile(kUser2, kFakeGaia2);
const AccountId account_id(
multi_user_util::GetAccountIdFromProfile(profile()));
const AccountId account_id2(
multi_user_util::GetAccountIdFromProfile(profile2));
constexpr char kWebAppUrl[] = "https://webappone.com/";
constexpr char kWebAppName[] = "WebApp1";
// Set up the test so
// * the primary user has a test app pinned to shelf, and
// * secondary user has a tab with the URL associated with the app open (but
// does not have the app installed).
auto web_app_info = web_app::WebAppInstallInfo::CreateWithStartUrlForTesting(
GURL(kWebAppUrl));
webapps::AppId installed_app_id =
web_app::test::InstallWebApp(profile(), std::move(web_app_info));
PinAppWithIDToShelf(installed_app_id);
std::unique_ptr<Browser> profile2_browser =
CreateBrowserAndTabWithProfile(profile2, kWebAppName, kWebAppUrl);
EXPECT_EQ(
std::vector<std::string>({app_constants::kChromeAppId, installed_app_id}),
GetAppsShownInShelf());
// Switch to the secondary user, and verify the app only installed in the
// primary profile is removed from the model.
SwitchActiveUserByAccountId(account_id2);
EXPECT_EQ(std::vector<std::string>({app_constants::kChromeAppId}),
GetAppsShownInShelf());
chrome::CloseTab(profile2_browser.get());
}
// Check that a running windowed V1 application will be properly pinned and
// unpinned when the order gets changed through a profile / policy change.
TEST_F(ChromeShelfControllerTest, RestoreDefaultAndRunningV1AppsResyncOrder) {
InitShelfController();
StartPrefSyncService(syncer::SyncDataList());
syncer::SyncChangeList sync_list;
InsertAddPinChange(&sync_list, 0, extension1_->id());
InsertAddPinChange(&sync_list, 1, ash::kGmailAppId);
SendPinChanges(sync_list, true);
// The shelf layout has always one static item at the beginning (App List).
AddExtension(extension1_.get());
EXPECT_EQ("Chrome, App1", GetPinnedAppStatus());
AddExtension(extension2_.get());
// No new app icon will be generated.
EXPECT_EQ("Chrome, App1", GetPinnedAppStatus());
// Set the app status as running, which will add an unpinned item.
shelf_controller_->SetAppStatus(extension2_->id(), ash::STATUS_RUNNING);
EXPECT_EQ("Chrome, App1, app2", GetPinnedAppStatus());
AddWebApp(ash::kGmailAppId);
EXPECT_EQ("Chrome, App1, Gmail, app2", GetPinnedAppStatus());
// Now request to pin all items, which will pin the running unpinned items.
syncer::SyncChangeList sync_list1;
InsertAddPinChange(&sync_list1, 0, ash::kGmailAppId);
InsertAddPinChange(&sync_list1, 1, extension2_->id());
InsertAddPinChange(&sync_list1, 2, extension1_->id());
SendPinChanges(sync_list1, true);
EXPECT_EQ("Chrome, Gmail, App2, App1", GetPinnedAppStatus());
// Removing the requirement for app 2 to be pinned should convert it back to
// running but not pinned. It should move towards the end of the shelf, after
// the pinned items, as determined by the |ShelfModel|'s weight system.
syncer::SyncChangeList sync_list2;
InsertAddPinChange(&sync_list2, 0, ash::kGmailAppId);
InsertAddPinChange(&sync_list2, 1, extension1_->id());
SendPinChanges(sync_list2, true);
EXPECT_EQ("Chrome, Gmail, App1, app2", GetPinnedAppStatus());
// Removing an item should simply close it and everything should shift.
syncer::SyncChangeList sync_list3;
InsertRemovePinChange(&sync_list3, extension1_->id());
SendPinChanges(sync_list3, false /* reset_pin_model */);
EXPECT_EQ("Chrome, Gmail, app2", GetPinnedAppStatus());
}
// Check that a running unpinned V2 application will be properly pinned and
// unpinned when the order gets changed through a profile / policy change.
TEST_F(ChromeShelfControllerTest, RestoreDefaultAndRunningV2AppsResyncOrder) {
InitShelfController();
syncer::SyncChangeList sync_list0;
InsertAddPinChange(&sync_list0, 0, extension1_->id());
InsertAddPinChange(&sync_list0, 1, ash::kGmailAppId);
SendPinChanges(sync_list0, true);
// The shelf layout has always one static item at the beginning (app List).
AddExtension(extension1_.get());
EXPECT_EQ("Chrome, App1", GetPinnedAppStatus());
AddExtension(extension_platform_app_.get());
// No new app icon will be generated.
EXPECT_EQ("Chrome, App1", GetPinnedAppStatus());
// Add an unpinned but running V2 app.
CreateRunningV2App(extension_platform_app_->id());
EXPECT_EQ("Chrome, App1, *platform_app", GetPinnedAppStatus());
AddWebApp(ash::kGmailAppId);
EXPECT_EQ("Chrome, App1, Gmail, *platform_app", GetPinnedAppStatus());
// Now request to pin all items, which should pin the running unpinned item.
syncer::SyncChangeList sync_list1;
InsertAddPinChange(&sync_list1, 0, ash::kGmailAppId);
InsertAddPinChange(&sync_list1, 1, extension_platform_app_->id());
InsertAddPinChange(&sync_list1, 2, extension1_->id());
SendPinChanges(sync_list1, true);
EXPECT_EQ("Chrome, Gmail, *Platform_App, App1", GetPinnedAppStatus());
// Removing the requirement for app 2 to be pinned should convert it back to
// running but not pinned. It should move towards the end of the shelf, after
// the pinned items, as determined by the |ShelfModel|'s weight system.
syncer::SyncChangeList sync_list2;
InsertAddPinChange(&sync_list2, 0, ash::kGmailAppId);
InsertAddPinChange(&sync_list2, 1, extension1_->id());
SendPinChanges(sync_list2, true);
EXPECT_EQ("Chrome, Gmail, App1, *platform_app", GetPinnedAppStatus());
// Removing an item should simply close it and everything should shift.
syncer::SyncChangeList sync_list3;
InsertAddPinChange(&sync_list3, 0, ash::kGmailAppId);
SendPinChanges(sync_list3, true);
EXPECT_EQ("Chrome, Gmail, *platform_app", GetPinnedAppStatus());
}
// Each user has a different set of applications pinned. Check that when
// switching between the two users, the state gets properly set.
TEST_F(ChromeShelfControllerTest, UserSwitchIconRestore) {
syncer::SyncChangeList user_a;
syncer::SyncChangeList user_b;
SetUpMultiUserScenario(&user_a, &user_b);
// Show user 1.
SendPinChanges(user_a, true);
EXPECT_EQ("App1, App2, Gmail, *Platform_App, Doc, App5, Chrome",
GetPinnedAppStatus());
// Show user 2.
SendPinChanges(user_b, true);
EXPECT_EQ("App6, App7, App8, Chrome", GetPinnedAppStatus());
// Switch back to 1.
SendPinChanges(user_a, true);
EXPECT_EQ("App1, App2, Gmail, *Platform_App, Doc, App5, Chrome",
GetPinnedAppStatus());
// Switch back to 2.
SendPinChanges(user_b, true);
EXPECT_EQ("App6, App7, App8, Chrome", GetPinnedAppStatus());
}
// Each user has a different set of applications pinned, and one user has an
// application running. Check that when switching between the two users, the
// state gets properly set.
TEST_F(ChromeShelfControllerTest, UserSwitchIconRestoreWithRunningV2App) {
syncer::SyncChangeList user_a;
syncer::SyncChangeList user_b;
SetUpMultiUserScenario(&user_a, &user_b);
// Run the platform (V2) app.
CreateRunningV2App(extension_platform_app_->id());
// Show user 1.
SendPinChanges(user_a, true);
EXPECT_EQ("App1, App2, Gmail, *Platform_App, Doc, App5, Chrome",
GetPinnedAppStatus());
// Show user 2.
SendPinChanges(user_b, true);
EXPECT_EQ("App6, App7, App8, Chrome, *platform_app", GetPinnedAppStatus());
// Switch back to 1.
SendPinChanges(user_a, true);
EXPECT_EQ("App1, App2, Gmail, *Platform_App, Doc, App5, Chrome",
GetPinnedAppStatus());
// Switch back to 2.
SendPinChanges(user_b, true);
EXPECT_EQ("App6, App7, App8, Chrome, *platform_app", GetPinnedAppStatus());
}
// Each user has a different set of applications pinned, and one user has an
// application running. The chrome icon is not the last item in the list.
// Check that when switching between the two users, the state gets properly set.
// There was once a bug associated with this.
TEST_F(ChromeShelfControllerTest,
UserSwitchIconRestoreWithRunningV2AppChromeInMiddle) {
syncer::SyncChangeList user_a;
syncer::SyncChangeList user_b;
SetUpMultiUserScenario(&user_a, &user_b);
// Run the platform (V2) app.
CreateRunningV2App(extension_platform_app_->id());
// Show user 1.
SendPinChanges(user_a, true);
SetShelfChromeIconIndex(5);
EXPECT_EQ("App1, App2, Gmail, *Platform_App, Doc, Chrome, App5",
GetPinnedAppStatus());
// Show user 2.
SendPinChanges(user_b, true);
SetShelfChromeIconIndex(3);
EXPECT_EQ("App6, App7, App8, Chrome, *platform_app", GetPinnedAppStatus());
// Switch back to 1.
SendPinChanges(user_a, true);
SetShelfChromeIconIndex(5);
EXPECT_EQ("App1, App2, Gmail, *Platform_App, Doc, Chrome, App5",
GetPinnedAppStatus());
}
TEST_F(ChromeShelfControllerTest, Policy) {
// Install some apps.
InstallSystemWebApp(std::make_unique<CameraSystemAppDelegate>(profile()));
extension_registrar_->AddExtension(extension2_.get());
AddWebApp(ash::kYoutubeAppId);
// Here and below we pretend that Gmail is both a preinstalled and a
// force-installed app and instruct that "gmail" policy_id has to be treated
// accordingly. This means that both "gmail" and
// "https://mail.google.com/mail/?usp=installed_webapp" are valid policy_ids
// for ash::kGmailAppId.
constexpr std::string_view kGmailPolicyId = "gmail";
base::flat_map<std::string_view, std::string_view>
preinstalled_web_apps_mapping;
preinstalled_web_apps_mapping.emplace(kGmailPolicyId, ash::kGmailAppId);
web_app::WebAppPolicyManager::SetPreinstalledWebAppsMappingForTesting(
preinstalled_web_apps_mapping);
// Force-install Gmail.
auto gmail_start_url = GetWebAppUrl(ash::kGmailAppId);
auto gmail_install_url = gmail_start_url;
InstallExternalWebApp(gmail_start_url, gmail_install_url);
// Pin policy should be initialized before controller start.
base::Value::List policy_value;
AppendPrefValue(policy_value, extension1_->id());
AppendPrefValue(policy_value, extension2_->id());
AppendPrefValue(policy_value,
std::string{*web_app::GetPolicyIdForSystemWebAppType(
ash::SystemWebAppType::CAMERA)});
// Pin Gmail by its install_url (see above).
AppendPrefValue(policy_value, gmail_install_url.spec());
profile()->GetTestingPrefService()->SetManagedPref(
prefs::kPolicyPinnedLauncherApps, base::Value(policy_value.Clone()));
InitShelfController();
// |extension2_|, Camera and Gmail should get pinned. |extension1_| is
// specified but not installed, and Youtube is part of the default set, but
// that shouldn't take effect when the policy override is in place.
EXPECT_EQ("Chrome, App2, Camera, Gmail", GetPinnedAppStatus());
EXPECT_TRUE(IsAppPolicyPinned(extension2_->id()));
EXPECT_TRUE(IsAppPolicyPinned(ash::kCameraAppId));
EXPECT_TRUE(IsAppPolicyPinned(ash::kGmailAppId));
// Installing |extension1_| should add it to the shelf. Note, App1 goes
// before App2 that is aligned with the pin order in policy.
AddExtension(extension1_.get());
EXPECT_EQ("Chrome, App1, App2, Camera, Gmail", GetPinnedAppStatus());
EXPECT_TRUE(IsAppPolicyPinned(extension1_->id()));
// Removing |extension1_| from the policy should not be reflected in the
// shelf and pin will exist.
RemovePrefValue(policy_value, extension1_->id());
profile()->GetTestingPrefService()->SetManagedPref(
prefs::kPolicyPinnedLauncherApps, base::Value(policy_value.Clone()));
EXPECT_EQ("Chrome, App1, App2, Camera, Gmail", GetPinnedAppStatus());
EXPECT_FALSE(IsAppPolicyPinned(extension1_->id()));
// Remove Gmail from the policy. It should stay pinned, but is no longer
// fixed.
RemovePrefValue(policy_value, gmail_install_url.spec());
profile()->GetTestingPrefService()->SetManagedPref(
prefs::kPolicyPinnedLauncherApps, base::Value(policy_value.Clone()));
EXPECT_EQ("Chrome, App1, App2, Camera, Gmail", GetPinnedAppStatus());
EXPECT_FALSE(IsAppPolicyPinned(ash::kGmailAppId));
// Check that Gmail can also be pinned by direct mapping.
AppendPrefValue(policy_value, std::string(kGmailPolicyId));
profile()->GetTestingPrefService()->SetManagedPref(
prefs::kPolicyPinnedLauncherApps, base::Value(policy_value.Clone()));
EXPECT_EQ("Chrome, App1, App2, Camera, Gmail", GetPinnedAppStatus());
EXPECT_TRUE(IsAppPolicyPinned(ash::kGmailAppId));
}
TEST_F(ChromeShelfControllerTest, UnpinWithUninstall) {
AddWebApp(ash::kGmailAppId);
AddWebApp(ash::kYoutubeAppId);
InitShelfController();
StartPrefSyncService(syncer::SyncDataList());
EXPECT_TRUE(shelf_controller_->IsAppPinned(ash::kGmailAppId));
EXPECT_TRUE(shelf_controller_->IsAppPinned(ash::kYoutubeAppId));
RemoveWebApp(ash::kGmailAppId);
EXPECT_FALSE(shelf_controller_->IsAppPinned(ash::kGmailAppId));
EXPECT_TRUE(shelf_controller_->IsAppPinned(ash::kYoutubeAppId));
}
TEST_F(ChromeShelfControllerTest, SyncUpdates) {
extension_registrar_->AddExtension(extension2_.get());
AddWebApp(ash::kGmailAppId);
AddWebApp(ash::kGoogleDocsAppId);
InitShelfController();
syncer::SyncChangeList sync_list;
InsertAddPinChange(&sync_list, 10, app_constants::kChromeAppId);
SendPinChanges(sync_list, true);
std::vector<std::string> expected_pinned_apps;
std::vector<std::string> actual_pinned_apps;
GetPinnedAppIds(shelf_controller_.get(), &actual_pinned_apps);
EXPECT_EQ(expected_pinned_apps, actual_pinned_apps);
// Unavailable extensions don't create shelf items.
sync_list.clear();
InsertAddPinChange(&sync_list, 0, extension1_->id());
InsertAddPinChange(&sync_list, 1, extension2_->id());
InsertAddPinChange(&sync_list, 3, ash::kGoogleDocsAppId);
SendPinChanges(sync_list, false);
expected_pinned_apps.push_back(extension2_->id());
expected_pinned_apps.push_back(ash::kGoogleDocsAppId);
GetPinnedAppIds(shelf_controller_.get(), &actual_pinned_apps);
EXPECT_EQ(expected_pinned_apps, actual_pinned_apps);
sync_list.clear();
InsertAddPinChange(&sync_list, 2, ash::kGmailAppId);
SendPinChanges(sync_list, false);
expected_pinned_apps.insert(expected_pinned_apps.begin() + 1,
ash::kGmailAppId);
GetPinnedAppIds(shelf_controller_.get(), &actual_pinned_apps);
EXPECT_EQ(expected_pinned_apps, actual_pinned_apps);
sync_list.clear();
InsertUpdatePinChange(&sync_list, 0, ash::kGoogleDocsAppId);
InsertUpdatePinChange(&sync_list, 1, ash::kGmailAppId);
InsertUpdatePinChange(&sync_list, 2, extension2_->id());
SendPinChanges(sync_list, false);
std::reverse(expected_pinned_apps.begin(), expected_pinned_apps.end());
GetPinnedAppIds(shelf_controller_.get(), &actual_pinned_apps);
EXPECT_EQ(expected_pinned_apps, actual_pinned_apps);
// Sending legacy sync change without pin info should not affect pin model.
sync_list.clear();
InsertLegacyPinChange(&sync_list, ash::kGoogleDocsAppId);
SendPinChanges(sync_list, false);
GetPinnedAppIds(shelf_controller_.get(), &actual_pinned_apps);
EXPECT_EQ(expected_pinned_apps, actual_pinned_apps);
sync_list.clear();
InsertRemovePinChange(&sync_list, ash::kGoogleDocsAppId);
SendPinChanges(sync_list, false);
expected_pinned_apps.erase(expected_pinned_apps.begin());
GetPinnedAppIds(shelf_controller_.get(), &actual_pinned_apps);
EXPECT_EQ(expected_pinned_apps, actual_pinned_apps);
sync_list.clear();
InsertRemovePinChange(&sync_list, ash::kGmailAppId);
InsertRemovePinChange(&sync_list, extension2_->id());
SendPinChanges(sync_list, false);
expected_pinned_apps.clear();
GetPinnedAppIds(shelf_controller_.get(), &actual_pinned_apps);
EXPECT_EQ(expected_pinned_apps, actual_pinned_apps);
}
TEST_F(ChromeShelfControllerTest, PendingInsertionOrder) {
extension_registrar_->AddExtension(extension1_.get());
AddWebApp(ash::kGmailAppId);
InitShelfController();
syncer::SyncChangeList sync_list;
InsertAddPinChange(&sync_list, 0, extension1_->id());
InsertAddPinChange(&sync_list, 1, extension2_->id());
InsertAddPinChange(&sync_list, 2, ash::kGmailAppId);
SendPinChanges(sync_list, true);
std::vector<std::string> expected_pinned_apps;
expected_pinned_apps.push_back(extension1_->id());
expected_pinned_apps.push_back(ash::kGmailAppId);
std::vector<std::string> actual_pinned_apps;
GetPinnedAppIds(shelf_controller_.get(), &actual_pinned_apps);
EXPECT_EQ(expected_pinned_apps, actual_pinned_apps);
// Install |extension2| and verify it shows up between the other two.
AddExtension(extension2_.get());
expected_pinned_apps.insert(expected_pinned_apps.begin() + 1,
extension2_->id());
GetPinnedAppIds(shelf_controller_.get(), &actual_pinned_apps);
EXPECT_EQ(expected_pinned_apps, actual_pinned_apps);
}
// Ensure |controller| creates the expected menu items for the given shelf item.
void CheckAppMenu(ChromeShelfController* controller,
const ash::ShelfItem& item,
size_t expected_item_count,
std::u16string expected_item_titles[]) {
auto items = controller->GetAppMenuItemsForTesting(item);
ASSERT_EQ(expected_item_count, items.size());
for (size_t i = 0; i < expected_item_count; i++) {
EXPECT_EQ(expected_item_titles[i], items[i].title);
}
}
// Check that browsers get reflected correctly in the shelf menu.
TEST_F(ChromeShelfControllerTest, BrowserMenuGeneration) {
EXPECT_EQ(1U, chrome::GetTotalBrowserCount());
chrome::NewTab(browser());
InitShelfController();
// Check that the browser list is empty at this time.
ash::ShelfItem item_browser;
item_browser.type = ash::TYPE_BROWSER_SHORTCUT;
item_browser.id = ash::ShelfID(app_constants::kChromeAppId);
CheckAppMenu(shelf_controller_.get(), item_browser, 0, nullptr);
// Now make the created browser() visible by showing its browser window.
browser()->window()->Show();
std::u16string title1 = u"Test1";
NavigateAndCommitActiveTabWithTitle(browser(), GURL("http://test1"), title1);
std::u16string one_menu_item[] = {title1};
CheckAppMenu(shelf_controller_.get(), item_browser, 1, one_menu_item);
// Create one more browser/window and check that one more was added.
std::unique_ptr<Browser> browser2(
CreateBrowserWithTestWindowForProfile(profile()));
chrome::NewTab(browser2.get());
browser2->window()->Show();
std::u16string title2 = u"Test2";
NavigateAndCommitActiveTabWithTitle(browser2.get(), GURL("http://test2"),
title2);
// Check that the list contains now two entries - make furthermore sure that
// the active item is the first entry.
std::u16string two_menu_items[] = {title1, title2};
CheckAppMenu(shelf_controller_.get(), item_browser, 2, two_menu_items);
// Apparently we have to close all tabs we have.
chrome::CloseTab(browser2.get());
}
// Check the multi profile case where only user related browsers should show up.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest,
BrowserMenuGenerationTwoUsers) {
// Create a browser item in the controller.
InitShelfController();
ash::ShelfItem item_browser;
item_browser.type = ash::TYPE_BROWSER_SHORTCUT;
item_browser.id = ash::ShelfID(app_constants::kChromeAppId);
// Check that the menu is empty.
chrome::NewTab(browser());
CheckAppMenu(shelf_controller_.get(), item_browser, 0, nullptr);
// Show the created |browser()| by showing its window.
browser()->window()->Show();
std::u16string title1 = u"Test1";
NavigateAndCommitActiveTabWithTitle(browser(), GURL("http://test1"), title1);
std::u16string one_menu_item1[] = {title1};
CheckAppMenu(shelf_controller_.get(), item_browser, 1, one_menu_item1);
// Create a browser for another user and check that it is not included in the
// users running browser list.
constexpr char kUser2[] = "user2@example.com";
const GaiaId kFakeGaia2("fakegaia2");
TestingProfile* profile2 = CreateMultiUserProfile(kUser2, kFakeGaia2);
const AccountId account_id2(
multi_user_util::GetAccountIdFromProfile(profile2));
std::unique_ptr<Browser> browser2(
CreateBrowserAndTabWithProfile(profile2, "user2", "http://test2"));
std::u16string one_menu_item2[] = {u"user2"};
CheckAppMenu(shelf_controller_.get(), item_browser, 1, one_menu_item1);
// Switch to the other user and make sure that only that browser window gets
// shown.
SwitchActiveUserByAccountId(account_id2);
CheckAppMenu(shelf_controller_.get(), item_browser, 1, one_menu_item2);
// Transferred browsers of other users should not show up in the list.
MultiUserWindowManagerHelper::GetWindowManager()->ShowWindowForUser(
browser()->window()->GetNativeWindow(), account_id2);
CheckAppMenu(shelf_controller_.get(), item_browser, 1, one_menu_item2);
chrome::CloseTab(browser2.get());
}
// Check that V1 apps are correctly reflected in the shelf menu using the
// refocus logic.
// Note that the extension matching logic is tested by the extension system
// and does not need a separate test here.
TEST_F(ChromeShelfControllerTest, V1AppMenuGeneration) {
EXPECT_EQ(1U, chrome::GetTotalBrowserCount());
EXPECT_EQ(0, browser()->tab_strip_model()->count());
InitShelfControllerWithBrowser();
StartPrefSyncService(syncer::SyncDataList());
// The model should only contain the browser shortcut item.
EXPECT_EQ(1, model_->item_count());
EXPECT_FALSE(shelf_controller_->IsAppPinned(ash::kGmailAppId));
// Installing Gmail pins it to the shelf.
const ash::ShelfID gmail_id(ash::kGmailAppId);
AddWebApp(ash::kGmailAppId);
EXPECT_TRUE(shelf_controller_->IsAppPinned(ash::kGmailAppId));
SetRefocusURL(gmail_id, GURL(kGmailUrl));
// Check the menu content.
ash::ShelfItem item_browser;
item_browser.type = ash::TYPE_BROWSER_SHORTCUT;
item_browser.id = ash::ShelfID(app_constants::kChromeAppId);
ash::ShelfItem item_gmail;
item_gmail.type = ash::TYPE_PINNED_APP;
item_gmail.id = gmail_id;
CheckAppMenu(shelf_controller_.get(), item_gmail, 0, nullptr);
// Set the gmail URL to a new tab.
std::u16string title1 = u"Test1";
NavigateAndCommitActiveTabWithTitle(browser(), GURL(kGmailUrl), title1);
std::u16string one_menu_item[] = {title1};
CheckAppMenu(shelf_controller_.get(), item_gmail, 1, one_menu_item);
// Create one empty tab.
chrome::NewTab(browser());
std::u16string title2 = u"Test2";
NavigateAndCommitActiveTabWithTitle(browser(), GURL("https://bla"), title2);
// and another one with another gmail instance.
chrome::NewTab(browser());
std::u16string title3 = u"Test3";
NavigateAndCommitActiveTabWithTitle(browser(), GURL(kGmailUrl), title3);
std::u16string two_menu_items[] = {title1, title3};
CheckAppMenu(shelf_controller_.get(), item_gmail, 2, two_menu_items);
// Even though the item is in the V1 app list, it should also be in the
// browser list.
std::u16string browser_menu_item[] = {title3};
CheckAppMenu(shelf_controller_.get(), item_browser, 1, browser_menu_item);
// Test that closing of (all) the item(s) does work (and all menus get
// updated properly).
shelf_controller_->Close(item_gmail.id);
CheckAppMenu(shelf_controller_.get(), item_gmail, 0, nullptr);
std::u16string browser_menu_item2[] = {title2};
CheckAppMenu(shelf_controller_.get(), item_browser, 1, browser_menu_item2);
}
// Check the multi profile case where only user related apps should show up.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest,
V1AppMenuGenerationTwoUsers) {
// Create a browser item in the controller.
InitShelfController();
StartPrefSyncService(syncer::SyncDataList());
chrome::NewTab(browser());
// Installing Gmail pins it to the shelf.
const ash::ShelfID gmail_id(ash::kGmailAppId);
AddWebApp(ash::kGmailAppId);
EXPECT_TRUE(shelf_controller_->IsAppPinned(ash::kGmailAppId));
SetRefocusURL(gmail_id, GURL(kGmailUrl));
// Check the menu content.
ash::ShelfItem item_browser;
item_browser.type = ash::TYPE_BROWSER_SHORTCUT;
item_browser.id = ash::ShelfID(app_constants::kChromeAppId);
ash::ShelfItem item_gmail;
item_gmail.type = ash::TYPE_PINNED_APP;
item_gmail.id = gmail_id;
CheckAppMenu(shelf_controller_.get(), item_gmail, 0, nullptr);
// Set the gmail URL to a new tab.
std::u16string title1 = u"Test1";
NavigateAndCommitActiveTabWithTitle(browser(), GURL(kGmailUrl), title1);
std::u16string one_menu_item[] = {title1};
CheckAppMenu(shelf_controller_.get(), item_gmail, 1, one_menu_item);
// Create a second profile and switch to that user.
constexpr char kUser2[] = "user2@example.com";
const GaiaId kFakeGaia2("fakegaia2");
TestingProfile* profile2 = CreateMultiUserProfile(kUser2, kFakeGaia2);
const AccountId account_id2(
multi_user_util::GetAccountIdFromProfile(profile2));
SwitchActiveUserByAccountId(account_id2);
// No item should have content yet.
CheckAppMenu(shelf_controller_.get(), item_browser, 0, nullptr);
CheckAppMenu(shelf_controller_.get(), item_gmail, 0, nullptr);
// Transfer the browser of the first user - it should still not show up.
MultiUserWindowManagerHelper::GetWindowManager()->ShowWindowForUser(
browser()->window()->GetNativeWindow(), account_id2);
CheckAppMenu(shelf_controller_.get(), item_browser, 0, nullptr);
CheckAppMenu(shelf_controller_.get(), item_gmail, 0, nullptr);
}
// Check that V2 applications are creating items properly in the shelf when
// instantiated by the current user.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest,
V2AppHandlingTwoUsers) {
InitShelfController();
const AccountId account_id(
multi_user_util::GetAccountIdFromProfile(profile()));
// Check that there is a browser.
EXPECT_EQ(1, model_->item_count());
// Add a v2 app.
AddExtension(extension1_.get());
V2App v2_app(profile(), extension1_.get());
EXPECT_EQ(2, model_->item_count());
// Create a profile for our second user (will be destroyed by the framework).
TestingProfile* profile2 =
CreateMultiUserProfile("user2@example.com", GaiaId("fakegaia2"));
const AccountId account_id2(
multi_user_util::GetAccountIdFromProfile(profile2));
// After switching users the item should go away.
SwitchActiveUserByAccountId(account_id2);
EXPECT_EQ(1, model_->item_count());
// And it should come back when switching back.
SwitchActiveUserByAccountId(account_id);
EXPECT_EQ(2, model_->item_count());
}
// Check that V2 applications are creating items properly in edge cases:
// a background user creates a V2 app, gets active and inactive again and then
// deletes the app.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest,
V2AppHandlingTwoUsersEdgeCases) {
InitShelfController();
// Create a profile for our second user (will be destroyed by the framework).
TestingProfile* profile2 =
CreateMultiUserProfile("user2@example.com", GaiaId("fakegaia2"));
const AccountId account_id(
multi_user_util::GetAccountIdFromProfile(profile()));
const AccountId account_id2(
multi_user_util::GetAccountIdFromProfile(profile2));
// Check that there is a browser, back button and an app.
EXPECT_EQ(1, model_->item_count());
// Switch to an inactive user.
SwitchActiveUserByAccountId(account_id2);
EXPECT_EQ(1, model_->item_count());
// Add the v2 app to the inactive user and check that no item was added to
// the shelf.
{
AddExtension(extension1_.get());
V2App v2_app(profile(), extension1_.get());
EXPECT_EQ(1, model_->item_count());
// Switch to the primary user and check that the item is shown.
SwitchActiveUserByAccountId(account_id);
EXPECT_EQ(2, model_->item_count());
// Switch to the second user and check that the item goes away - even if the
// item gets closed.
SwitchActiveUserByAccountId(account_id2);
EXPECT_EQ(1, model_->item_count());
}
// After the application was killed there should still be 1 item.
EXPECT_EQ(1, model_->item_count());
// Switching then back to the default user should not show the additional
// item anymore.
SwitchActiveUserByAccountId(account_id);
EXPECT_EQ(1, model_->item_count());
}
TEST_F(ChromeShelfControllerTest, Active) {
InitShelfController();
// Creates a new app window.
int initial_item_count = model_->item_count();
AddExtension(extension1_.get());
V2App app_1(profile(), extension1_.get());
EXPECT_TRUE(app_1.window()->GetNativeWindow()->IsVisible());
EXPECT_EQ(initial_item_count + 1, model_->item_count());
ash::ShelfItemDelegate* app_item_delegate_1 =
model_->GetShelfItemDelegate(model_->items()[initial_item_count].id);
ASSERT_TRUE(app_item_delegate_1);
AppWindowShelfItemController* app_item_controller_1 =
app_item_delegate_1->AsAppWindowShelfItemController();
ASSERT_TRUE(app_item_controller_1);
ui::BaseWindow* last_active =
GetLastActiveWindowForItemController(app_item_controller_1);
EXPECT_EQ(app_1.window()->GetNativeWindow(), last_active->GetNativeWindow());
// Change the status so that we can verify it gets reset when the active
// window changes.
shelf_controller_->SetItemStatus(app_item_delegate_1->shelf_id(),
ash::STATUS_ATTENTION);
// Creates another app window, which should become active and reset |app_1|'s
// status (to running).
AddExtension(extension2_.get());
V2App app_2(profile(), extension2_.get());
EXPECT_TRUE(app_2.window()->GetNativeWindow()->IsVisible());
EXPECT_EQ(initial_item_count + 2, model_->item_count());
ash::ShelfItemDelegate* app_item_delegate_2 =
model_->GetShelfItemDelegate(model_->items()[initial_item_count + 1].id);
ASSERT_TRUE(app_item_delegate_2);
AppWindowShelfItemController* app_item_controller_2 =
app_item_delegate_2->AsAppWindowShelfItemController();
ASSERT_TRUE(app_item_controller_2);
last_active = GetLastActiveWindowForItemController(app_item_controller_2);
EXPECT_EQ(app_2.window()->GetNativeWindow(), last_active->GetNativeWindow());
const ash::ShelfItem* shelf_item_1 =
shelf_controller_->GetItem(app_item_delegate_1->shelf_id());
ASSERT_TRUE(shelf_item_1);
EXPECT_EQ(ash::STATUS_RUNNING, shelf_item_1->status);
shelf_controller_->SetItemStatus(app_item_delegate_2->shelf_id(),
ash::STATUS_ATTENTION);
// Activate the first window, which should reset the status of the
// second apps window.
app_1.window()->GetBaseWindow()->Activate();
const ash::ShelfItem* shelf_item_2 =
shelf_controller_->GetItem(app_item_delegate_2->shelf_id());
ASSERT_TRUE(shelf_item_2);
EXPECT_EQ(ash::STATUS_RUNNING, shelf_item_2->status);
}
// Check that V2 applications will be made visible on the target desktop if
// another window of the same type got previously teleported there.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest,
V2AppFollowsTeleportedWindow) {
InitShelfController();
ash::MultiUserWindowManager* window_manager =
MultiUserWindowManagerHelper::GetWindowManager();
// Create and add three users / profiles, and go to #1's desktop.
TestingProfile* profile1 =
CreateMultiUserProfile("user-1@example.com", GaiaId("fakegaia1"));
TestingProfile* profile2 =
CreateMultiUserProfile("user-2@example.com", GaiaId("fakegaia2"));
TestingProfile* profile3 =
CreateMultiUserProfile("user-3@example.com", GaiaId("fakegaia3"));
const AccountId account_id1(
multi_user_util::GetAccountIdFromProfile(profile1));
const AccountId account_id2(
multi_user_util::GetAccountIdFromProfile(profile2));
const AccountId account_id3(
multi_user_util::GetAccountIdFromProfile(profile3));
extensions::TestExtensionSystem* extension_system1(
static_cast<extensions::TestExtensionSystem*>(
extensions::ExtensionSystem::Get(profile1)));
extensions::ExtensionService* extension_service1 =
extension_system1->CreateExtensionService(
base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
extension_service1->Init();
SwitchActiveUserByAccountId(account_id1);
// A v2 app for user #1 should be shown first and get hidden when switching
// to desktop #2.
extensions::ExtensionRegistrar::Get(profile1)->AddExtension(extension1_);
V2App v2_app_1(profile1, extension1_.get());
EXPECT_TRUE(v2_app_1.window()->GetNativeWindow()->IsVisible());
SwitchActiveUserByAccountId(account_id2);
EXPECT_FALSE(v2_app_1.window()->GetNativeWindow()->IsVisible());
// Add a v2 app for user #1 while on desktop #2 should not be shown.
V2App v2_app_2(profile1, extension1_.get());
EXPECT_FALSE(v2_app_1.window()->GetNativeWindow()->IsVisible());
EXPECT_FALSE(v2_app_2.window()->GetNativeWindow()->IsVisible());
// Teleport the app from user #1 to the desktop #2 should show it.
window_manager->ShowWindowForUser(v2_app_1.window()->GetNativeWindow(),
account_id2);
EXPECT_TRUE(v2_app_1.window()->GetNativeWindow()->IsVisible());
EXPECT_FALSE(v2_app_2.window()->GetNativeWindow()->IsVisible());
// Creating a new application for user #1 on desktop #2 should teleport it
// there automatically.
V2App v2_app_3(profile1, extension1_.get());
EXPECT_TRUE(v2_app_1.window()->GetNativeWindow()->IsVisible());
EXPECT_FALSE(v2_app_2.window()->GetNativeWindow()->IsVisible());
EXPECT_TRUE(v2_app_3.window()->GetNativeWindow()->IsVisible());
// Switching back to desktop#1 and creating an app for user #1 should move
// the app on desktop #1.
SwitchActiveUserByAccountId(account_id1);
V2App v2_app_4(profile1, extension1_.get());
EXPECT_FALSE(v2_app_1.window()->GetNativeWindow()->IsVisible());
EXPECT_TRUE(v2_app_2.window()->GetNativeWindow()->IsVisible());
EXPECT_FALSE(v2_app_3.window()->GetNativeWindow()->IsVisible());
EXPECT_TRUE(v2_app_4.window()->GetNativeWindow()->IsVisible());
// Switching to desktop #3 and creating an app for user #1 should place it
// on that user's desktop (#1).
SwitchActiveUserByAccountId(account_id3);
V2App v2_app_5(profile1, extension1_.get());
EXPECT_FALSE(v2_app_5.window()->GetNativeWindow()->IsVisible());
SwitchActiveUserByAccountId(account_id1);
EXPECT_TRUE(v2_app_5.window()->GetNativeWindow()->IsVisible());
// Switching to desktop #2, hiding the app window and creating an app should
// teleport there automatically.
SwitchActiveUserByAccountId(account_id2);
v2_app_1.window()->Hide();
V2App v2_app_6(profile1, extension1_.get());
EXPECT_FALSE(v2_app_1.window()->GetNativeWindow()->IsVisible());
EXPECT_FALSE(v2_app_2.window()->GetNativeWindow()->IsVisible());
EXPECT_TRUE(v2_app_6.window()->GetNativeWindow()->IsVisible());
}
// Check that V2 applications hide correctly on the shelf when the app window
// is hidden.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest,
V2AppHiddenWindows) {
InitShelfController();
TestingProfile* profile2 =
CreateMultiUserProfile("user-2@example.com", GaiaId("fakegaia2"));
const AccountId account_id2(
multi_user_util::GetAccountIdFromProfile(profile2));
// If switch to account_id2 is not run, the following switch to account_id
// is invalid, because the user account is not changed, so switch to
// account_id2 first.
SwitchActiveUserByAccountId(account_id2);
const AccountId account_id(
multi_user_util::GetAccountIdFromProfile(profile()));
SwitchActiveUserByAccountId(account_id);
EXPECT_EQ(1, model_->item_count());
AddExtension(extension1_.get());
V2App v2_app_1(profile(), extension1_.get());
EXPECT_EQ(2, model_->item_count());
{
// Hide and show the app.
v2_app_1.window()->Hide();
EXPECT_EQ(1, model_->item_count());
v2_app_1.window()->Show(extensions::AppWindow::SHOW_ACTIVE);
EXPECT_EQ(2, model_->item_count());
}
{
// Switch user, hide and show the app and switch back.
SwitchActiveUserByAccountId(account_id2);
EXPECT_EQ(1, model_->item_count());
v2_app_1.window()->Hide();
EXPECT_EQ(1, model_->item_count());
v2_app_1.window()->Show(extensions::AppWindow::SHOW_ACTIVE);
EXPECT_EQ(1, model_->item_count());
SwitchActiveUserByAccountId(account_id);
EXPECT_EQ(2, model_->item_count());
}
{
// Switch user, hide the app, switch back and then show it again.
SwitchActiveUserByAccountId(account_id2);
EXPECT_EQ(1, model_->item_count());
v2_app_1.window()->Hide();
EXPECT_EQ(1, model_->item_count());
SwitchActiveUserByAccountId(account_id);
// The following expectation does not work in current impl. It was working
// before because MultiProfileSupport is not attached to user associated
// with profile() hence not actually handling windows for the user. It is
// a real bug. See http://crbug.com/693634 EXPECT_EQ(2,
// model_->item_count());
v2_app_1.window()->Show(extensions::AppWindow::SHOW_ACTIVE);
EXPECT_EQ(2, model_->item_count());
}
{
// Create a second app, hide and show it and then hide both apps.
V2App v2_app_2(profile(), extension1_.get());
EXPECT_EQ(2, model_->item_count());
v2_app_2.window()->Hide();
EXPECT_EQ(2, model_->item_count());
v2_app_2.window()->Show(extensions::AppWindow::SHOW_ACTIVE);
EXPECT_EQ(2, model_->item_count());
v2_app_1.window()->Hide();
v2_app_2.window()->Hide();
EXPECT_EQ(1, model_->item_count());
}
}
// Checks that spinners are hidden and restored on profile switching
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest,
SpinnersUpdateOnUserSwitch) {
InitShelfController();
const AccountId account_id(
multi_user_util::GetAccountIdFromProfile(profile()));
constexpr char kUser2[] = "user2@example.com";
const GaiaId kFakeGaia2("fakegaia2");
const TestingProfile* profile2 = CreateMultiUserProfile(kUser2, kFakeGaia2);
const AccountId account_id2(
multi_user_util::GetAccountIdFromProfile(profile2));
const std::string app_id = extension1_->id();
extension_registrar_->AddExtension(extension1_.get());
EXPECT_EQ(1, model_->item_count());
EXPECT_FALSE(shelf_controller_->GetShelfSpinnerController()->HasApp(app_id));
// Add a spinner to the shelf
shelf_controller_->GetShelfSpinnerController()->AddSpinnerToShelf(
app_id, std::make_unique<ShelfSpinnerItemController>(app_id));
EXPECT_EQ(2, model_->item_count());
EXPECT_TRUE(shelf_controller_->GetShelfSpinnerController()->HasApp(app_id));
// Switch to a new profile
SwitchActiveUserByAccountId(account_id2);
EXPECT_EQ(1, model_->item_count());
EXPECT_FALSE(shelf_controller_->GetShelfSpinnerController()->HasApp(app_id));
// Switch back
SwitchActiveUserByAccountId(account_id);
EXPECT_EQ(2, model_->item_count());
EXPECT_TRUE(shelf_controller_->GetShelfSpinnerController()->HasApp(app_id));
// Close the spinner
shelf_controller_->GetShelfSpinnerController()->CloseSpinner(app_id);
EXPECT_EQ(1, model_->item_count());
EXPECT_FALSE(shelf_controller_->GetShelfSpinnerController()->HasApp(app_id));
}
// Checks that pinned spinners are hidden and restored on profile switching
// but are not removed when the spinner closes.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeShelfControllerTest,
PinnedSpinnersUpdateOnUserSwitch) {
InitShelfController();
const AccountId account_id(
multi_user_util::GetAccountIdFromProfile(profile()));
constexpr char kUser2[] = "user2@example.com";
const GaiaId kFakeGaia2("fakegaia2");
const TestingProfile* profile2 = CreateMultiUserProfile(kUser2, kFakeGaia2);
const AccountId account_id2(
multi_user_util::GetAccountIdFromProfile(profile2));
const std::string app_id = extension1_->id();
AddExtension(extension1_.get());
EXPECT_EQ(1, model_->item_count());
EXPECT_FALSE(shelf_controller_->GetShelfSpinnerController()->HasApp(app_id));
// Pin an app to the shelf
PinAppWithIDToShelf(app_id);
EXPECT_TRUE(shelf_controller_->IsAppPinned(app_id));
EXPECT_EQ(2, model_->item_count());
EXPECT_FALSE(shelf_controller_->GetShelfSpinnerController()->HasApp(app_id));
// Activate the spinner
shelf_controller_->GetShelfSpinnerController()->AddSpinnerToShelf(
app_id, std::make_unique<ShelfSpinnerItemController>(app_id));
EXPECT_TRUE(shelf_controller_->IsAppPinned(app_id));
EXPECT_EQ(2, model_->item_count());
EXPECT_TRUE(shelf_controller_->GetShelfSpinnerController()->HasApp(app_id));
// Switch to a new profile
SwitchActiveUserByAccountId(account_id2);
EXPECT_FALSE(shelf_controller_->IsAppPinned(app_id));
EXPECT_EQ(1, model_->item_count());
EXPECT_FALSE(shelf_controller_->GetShelfSpinnerController()->HasApp(app_id));
// Switch back
SwitchActiveUserByAccountId(account_id);
EXPECT_TRUE(shelf_controller_->IsAppPinned(app_id));
EXPECT_EQ(2, model_->item_count());
EXPECT_TRUE(shelf_controller_->GetShelfSpinnerController()->HasApp(app_id));
// Close the spinner
shelf_controller_->GetShelfSpinnerController()->CloseSpinner(app_id);
EXPECT_TRUE(shelf_controller_->IsAppPinned(app_id));
EXPECT_EQ(2, model_->item_count());
EXPECT_FALSE(shelf_controller_->GetShelfSpinnerController()->HasApp(app_id));
}
// Checks that the generated menu list properly activates items.
TEST_F(ChromeShelfControllerTest, V1AppMenuExecution) {
InitShelfControllerWithBrowser();
StartPrefSyncService(syncer::SyncDataList());
// Add Gmail to the shelf and add two items.
GURL gmail = GURL("https://mail.google.com/mail/u");
const ash::ShelfID gmail_id(ash::kGmailAppId);
AddWebApp(ash::kGmailAppId);
SetRefocusURL(gmail_id, GURL(kGmailUrl));
std::u16string title1 = u"Test1";
NavigateAndCommitActiveTabWithTitle(browser(), GURL(kGmailUrl), title1);
chrome::NewTab(browser());
std::u16string title2 = u"Test2";
NavigateAndCommitActiveTabWithTitle(browser(), GURL(kGmailUrl), title2);
// Check that the menu is properly set.
ash::ShelfItem item_gmail;
item_gmail.type = ash::TYPE_PINNED_APP;
item_gmail.id = gmail_id;
std::u16string two_menu_items[] = {title1, title2};
CheckAppMenu(shelf_controller_.get(), item_gmail, 2, two_menu_items);
ash::ShelfItemDelegate* item_delegate =
model_->GetShelfItemDelegate(gmail_id);
ASSERT_TRUE(item_delegate);
EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
// Execute the second item in the menu, after the title,
// this shouldn't do anything since that item is already the active tab.
{
ash::ShelfApplicationMenuModel menu(
std::u16string(),
shelf_controller_->GetAppMenuItemsForTesting(item_gmail),
item_delegate);
menu.ActivatedAt(2);
}
EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
// Execute the first item in the menu, after the title,
// this should activate the other tab.
{
ash::ShelfApplicationMenuModel menu(
std::u16string(),
shelf_controller_->GetAppMenuItemsForTesting(item_gmail),
item_delegate);
menu.ActivatedAt(1);
}
EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
}
// Checks that the generated menu list properly deletes items.
TEST_F(ChromeShelfControllerTest, V1AppMenuDeletionExecution) {
InitShelfControllerWithBrowser();
StartPrefSyncService(syncer::SyncDataList());
// Add Gmail to the shelf and add two items.
const ash::ShelfID gmail_id(ash::kGmailAppId);
AddWebApp(ash::kGmailAppId);
SetRefocusURL(gmail_id, GURL(kGmailUrl));
std::u16string title1 = u"Test1";
NavigateAndCommitActiveTabWithTitle(browser(), GURL(kGmailUrl), title1);
chrome::NewTab(browser());
std::u16string title2 = u"Test2";
NavigateAndCommitActiveTabWithTitle(browser(), GURL(kGmailUrl), title2);
// Check that the menu is properly set.
ash::ShelfItem item_gmail;
item_gmail.type = ash::TYPE_PINNED_APP;
item_gmail.id = gmail_id;
std::u16string two_menu_items[] = {title1, title2};
CheckAppMenu(shelf_controller_.get(), item_gmail, 2, two_menu_items);
ash::ShelfItemDelegate* item_delegate =
model_->GetShelfItemDelegate(gmail_id);
ASSERT_TRUE(item_delegate);
int tabs = browser()->tab_strip_model()->count();
// Activate the proper tab through the menu item.
{
auto items = shelf_controller_->GetAppMenuItemsForTesting(item_gmail);
item_delegate->ExecuteCommand(false, 1, ui::EF_NONE,
display::kInvalidDisplayId);
EXPECT_EQ(tabs, browser()->tab_strip_model()->count());
}
// Delete one tab through the menu item.
{
auto items = shelf_controller_->GetAppMenuItemsForTesting(item_gmail);
item_delegate->ExecuteCommand(false, 1, ui::EF_SHIFT_DOWN,
display::kInvalidDisplayId);
EXPECT_EQ(--tabs, browser()->tab_strip_model()->count());
}
}
// Verify that the shelf item positions are persisted and restored.
TEST_F(ChromeShelfControllerTest, PersistShelfItemPositions) {
InitShelfController();
TestShelfControllerHelper* helper = new TestShelfControllerHelper;
SetShelfControllerHelper(helper);
EXPECT_EQ(ash::TYPE_BROWSER_SHORTCUT, model_->items()[0].type);
TabStripModel* tab_strip_model = browser()->tab_strip_model();
EXPECT_EQ(0, tab_strip_model->count());
chrome::NewTab(browser());
chrome::NewTab(browser());
EXPECT_EQ(2, tab_strip_model->count());
helper->SetAppID(tab_strip_model->GetWebContentsAt(0), "1");
helper->SetAppID(tab_strip_model->GetWebContentsAt(1), "2");
EXPECT_FALSE(shelf_controller_->IsAppPinned("1"));
PinAppWithIDToShelf("1");
EXPECT_TRUE(shelf_controller_->IsAppPinned("1"));
PinAppWithIDToShelf("2");
EXPECT_EQ(ash::TYPE_BROWSER_SHORTCUT, model_->items()[0].type);
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[1].type);
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[2].type);
// Move browser shortcut item from index 0 to index 2.
model_->Move(0, 2);
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[0].type);
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[1].type);
EXPECT_EQ(ash::TYPE_BROWSER_SHORTCUT, model_->items()[2].type);
RecreateShelfController();
helper = new TestShelfControllerHelper(profile());
helper->SetAppID(tab_strip_model->GetWebContentsAt(0), "1");
helper->SetAppID(tab_strip_model->GetWebContentsAt(1), "2");
SetShelfControllerHelper(helper);
shelf_controller_->Init();
// Check ShelfItems are restored after resetting ChromeShelfController.
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[0].type);
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[1].type);
EXPECT_EQ(ash::TYPE_BROWSER_SHORTCUT, model_->items()[2].type);
}
// Verifies pinned apps are persisted and restored.
TEST_F(ChromeShelfControllerTest, PersistPinned) {
InitShelfControllerWithBrowser();
size_t initial_size = model_->items().size();
TabStripModel* tab_strip_model = browser()->tab_strip_model();
EXPECT_EQ(1, tab_strip_model->count());
TestShelfControllerHelper* helper = new TestShelfControllerHelper;
helper->SetAppID(tab_strip_model->GetWebContentsAt(0), "1");
SetShelfControllerHelper(helper);
// app_icon_loader is owned by ChromeShelfController.
TestAppIconLoaderImpl* app_icon_loader = new TestAppIconLoaderImpl;
app_icon_loader->AddSupportedApp("1");
SetAppIconLoader(std::unique_ptr<AppIconLoader>(app_icon_loader));
EXPECT_EQ(0, app_icon_loader->fetch_count());
PinAppWithIDToShelf("1");
int app_index = model_->ItemIndexByID(ash::ShelfID("1"));
EXPECT_EQ(1, app_icon_loader->fetch_count());
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[app_index].type);
EXPECT_TRUE(shelf_controller_->IsAppPinned("1"));
EXPECT_FALSE(shelf_controller_->IsAppPinned("0"));
EXPECT_EQ(initial_size + 1, model_->items().size());
RecreateShelfController();
helper = new TestShelfControllerHelper(profile());
helper->SetAppID(tab_strip_model->GetWebContentsAt(0), "1");
SetShelfControllerHelper(helper);
// app_icon_loader is owned by ChromeShelfController.
app_icon_loader = new TestAppIconLoaderImpl;
app_icon_loader->AddSupportedApp("1");
SetAppIconLoader(std::unique_ptr<AppIconLoader>(app_icon_loader));
shelf_controller_->Init();
app_index = model_->ItemIndexByID(ash::ShelfID("1"));
EXPECT_EQ(1, app_icon_loader->fetch_count());
ASSERT_EQ(initial_size + 1, model_->items().size());
EXPECT_TRUE(shelf_controller_->IsAppPinned("1"));
EXPECT_FALSE(shelf_controller_->IsAppPinned("0"));
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[app_index].type);
shelf_controller_->UnpinAppWithID("1");
EXPECT_EQ(initial_size + 1, model_->items().size());
tab_strip_model->CloseWebContentsAt(0, 0);
EXPECT_EQ(initial_size, model_->items().size());
}
// Verifies that ShelfID property is updated for browsers that are present when
// ChromeShelfController is created.
TEST_F(ChromeShelfControllerTest, ExistingBrowserWindowShelfIDSet) {
InitShelfControllerWithBrowser();
PinAppWithIDToShelf("1");
TabStripModel* tab_strip_model = browser()->tab_strip_model();
ASSERT_EQ(1, tab_strip_model->count());
TestShelfControllerHelper* helper = new TestShelfControllerHelper;
helper->SetAppID(tab_strip_model->GetWebContentsAt(0), "0");
SetShelfControllerHelper(helper);
RecreateShelfController();
helper = new TestShelfControllerHelper(profile());
helper->SetAppID(tab_strip_model->GetWebContentsAt(0), "1");
SetShelfControllerHelper(helper);
shelf_controller_->Init();
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID("1")));
EXPECT_EQ(ash::ShelfID("1"),
ash::ShelfID::Deserialize(
browser()->window()->GetNativeWindow()->GetProperty(
ash::kShelfIDKey)));
}
TEST_F(ChromeShelfControllerTest, MultipleAppIconLoaders) {
InitShelfControllerWithBrowser();
const ash::ShelfID shelf_id1(extension1_->id());
const ash::ShelfID shelf_id2(extension2_->id());
const ash::ShelfID shelf_id3(ash::kGmailAppId);
// app_icon_loader1 and app_icon_loader2 are owned by
// ChromeShelfController.
TestAppIconLoaderImpl* app_icon_loader1 = new TestAppIconLoaderImpl();
TestAppIconLoaderImpl* app_icon_loader2 = new TestAppIconLoaderImpl();
app_icon_loader1->AddSupportedApp(shelf_id1.app_id);
app_icon_loader2->AddSupportedApp(shelf_id2.app_id);
SetAppIconLoaders(std::unique_ptr<AppIconLoader>(app_icon_loader1),
std::unique_ptr<AppIconLoader>(app_icon_loader2));
shelf_controller_->CreateAppItem(
std::make_unique<AppServiceAppWindowShelfItemController>(
shelf_id3, shelf_controller_->app_service_app_window_controller()),
ash::STATUS_RUNNING, /*pinned=*/false);
EXPECT_EQ(0, app_icon_loader1->fetch_count());
EXPECT_EQ(0, app_icon_loader1->clear_count());
EXPECT_EQ(0, app_icon_loader2->fetch_count());
EXPECT_EQ(0, app_icon_loader2->clear_count());
shelf_controller_->CreateAppItem(
std::make_unique<AppServiceAppWindowShelfItemController>(
shelf_id2, shelf_controller_->app_service_app_window_controller()),
ash::STATUS_RUNNING, /*pinned=*/false);
EXPECT_EQ(0, app_icon_loader1->fetch_count());
EXPECT_EQ(0, app_icon_loader1->clear_count());
EXPECT_EQ(1, app_icon_loader2->fetch_count());
EXPECT_EQ(0, app_icon_loader2->clear_count());
shelf_controller_->CreateAppItem(
std::make_unique<AppServiceAppWindowShelfItemController>(
shelf_id1, shelf_controller_->app_service_app_window_controller()),
ash::STATUS_RUNNING, /*pinned=*/false);
EXPECT_EQ(1, app_icon_loader1->fetch_count());
EXPECT_EQ(0, app_icon_loader1->clear_count());
EXPECT_EQ(1, app_icon_loader2->fetch_count());
EXPECT_EQ(0, app_icon_loader2->clear_count());
shelf_controller_->ReplaceWithAppShortcutOrRemove(shelf_id1);
EXPECT_EQ(1, app_icon_loader1->fetch_count());
EXPECT_EQ(1, app_icon_loader1->clear_count());
EXPECT_EQ(1, app_icon_loader2->fetch_count());
EXPECT_EQ(0, app_icon_loader2->clear_count());
shelf_controller_->ReplaceWithAppShortcutOrRemove(shelf_id2);
EXPECT_EQ(1, app_icon_loader1->fetch_count());
EXPECT_EQ(1, app_icon_loader1->clear_count());
EXPECT_EQ(1, app_icon_loader2->fetch_count());
EXPECT_EQ(1, app_icon_loader2->clear_count());
shelf_controller_->ReplaceWithAppShortcutOrRemove(shelf_id3);
EXPECT_EQ(1, app_icon_loader1->fetch_count());
EXPECT_EQ(1, app_icon_loader1->clear_count());
EXPECT_EQ(1, app_icon_loader2->fetch_count());
EXPECT_EQ(1, app_icon_loader2->clear_count());
}
TEST_F(ChromeShelfControllerWithArcTest, ArcAppPinPolicy) {
InitShelfControllerWithBrowser();
constexpr char kExampleArcPackageName[] = "com.example.app";
arc::mojom::AppInfoPtr appinfo =
CreateAppInfo("Some App", "SomeActivity", kExampleArcPackageName);
const std::string example_app_id = AddArcAppAndShortcut(*appinfo);
// Sets up policy that pins this ARC app. Unlike native extensions, ARC apps
// are pinned by |package_name| rather than the actual |app_id|.
SetPinnedLauncherAppsPolicy(kExampleArcPackageName);
EXPECT_TRUE(shelf_controller_->IsAppPinned(example_app_id));
EXPECT_EQ(AppListControllerDelegate::PIN_FIXED,
GetPinnableForAppID(example_app_id, profile()));
}
TEST_F(ChromeShelfControllerWithArcTest, IwaPinPolicy) {
InitShelfControllerWithBrowser();
const web_app::IsolatedWebAppUrlInfo url_info = AddIsolatedWebApp();
SetPinnedLauncherAppsPolicy(url_info.web_bundle_id().id());
EXPECT_TRUE(shelf_controller_->IsAppPinned(url_info.app_id()));
EXPECT_EQ(AppListControllerDelegate::PIN_FIXED,
GetPinnableForAppID(url_info.app_id(), profile()));
}
TEST_F(ChromeShelfControllerWithArcTest, ApkWebAppPinPolicy) {
InitShelfControllerWithBrowser();
constexpr char kMapsWebPackageName[] = "com.google.maps";
auto* service = ash::ApkWebAppService::Get(browser()->profile());
ASSERT_TRUE(service);
base::test::TestFuture<const std::string&, const webapps::AppId&> future;
service->SetWebAppInstalledCallbackForTesting(future.GetCallback());
auto package = arc::mojom::ArcPackageInfo::New(kMapsWebPackageName, 1, 1, 1,
/*sync=*/true);
package->web_app_info =
arc::mojom::WebAppInfo::New("Google Maps", "https://www.google.com/maps/",
"https://www.google.com/", 1000000);
std::vector<arc::mojom::ArcPackageInfoPtr> packages;
packages.push_back(std::move(package));
arc_test_.app_instance()->SendRefreshPackageList(std::move(packages));
auto [maps_package_name, maps_app_id] = future.Take();
ASSERT_EQ(maps_package_name, kMapsWebPackageName);
// Sets up policy that pins this apk-based Web App. Unlike regular Web Apps,
// Web Apps originating from apks are pinned by |package_name| rather than
// their |install_url|.
SetPinnedLauncherAppsPolicy(kMapsWebPackageName);
EXPECT_TRUE(shelf_controller_->IsAppPinned(maps_app_id));
EXPECT_EQ(AppListControllerDelegate::PIN_FIXED,
GetPinnableForAppID(maps_app_id, profile()));
}
TEST_F(ChromeShelfControllerWithArcTest, ArcManaged) {
extension_registrar_->AddExtension(arc_support_host_.get());
// Test enables ARC, so turn it off for initial values.
EnablePlayStore(false);
InitShelfController();
// To prevent import legacy pins each time.
// Initially pins are imported from legacy pref based model.
StartPrefSyncService(syncer::SyncDataList());
// Initial run, ARC is not managed and disabled, Play Store pin should be
// available.
ValidateArcState(false, false, arc::ArcSessionManager::State::STOPPED,
"Chrome, Play Store");
// ARC is managed and enabled, Play Store pin should be available.
// Note: CHECKING_REQUIREMENTS here means that opt-in flow is skipped.
profile()->GetTestingPrefService()->SetManagedPref(
arc::prefs::kArcEnabled, std::make_unique<base::Value>(true));
base::RunLoop().RunUntilIdle();
ValidateArcState(true, true,
arc::ArcSessionManager::State::CHECKING_REQUIREMENTS,
"Chrome, Play Store");
// ARC is managed and disabled, Play Store pin should not be available.
profile()->GetTestingPrefService()->SetManagedPref(
arc::prefs::kArcEnabled, std::make_unique<base::Value>(false));
base::RunLoop().RunUntilIdle();
ValidateArcState(false, true, arc::ArcSessionManager::State::STOPPED,
"Chrome");
// ARC is not managed and disabled, Play Store pin should be available.
profile()->GetTestingPrefService()->RemoveManagedPref(
arc::prefs::kArcEnabled);
base::RunLoop().RunUntilIdle();
ValidateArcState(false, false, arc::ArcSessionManager::State::STOPPED,
"Chrome, Play Store");
// ARC is not managed and enabled, Play Store pin should be available.
// Note: CHECKING_REQUIREMENTS here means that opt-in flow starts.
EnablePlayStore(true);
ValidateArcState(true, false,
arc::ArcSessionManager::State::CHECKING_REQUIREMENTS,
"Chrome, Play Store");
// User disables ARC. ARC is not managed and disabled, Play Store pin should
// be automatically removed.
EnablePlayStore(false);
ValidateArcState(false, false, arc::ArcSessionManager::State::STOPPED,
"Chrome");
// Even if re-enable it again, Play Store pin does not appear automatically.
EnablePlayStore(true);
ValidateArcState(true, false,
arc::ArcSessionManager::State::CHECKING_REQUIREMENTS,
"Chrome");
}
// Test the application menu of a shelf item with multiple ARC windows.
TEST_F(ChromeShelfControllerWithArcTest, ShelfItemWithMultipleWindows) {
InitShelfControllerWithBrowser();
arc::mojom::AppInfoPtr appinfo =
CreateAppInfo("Test1", "test", "com.example.app");
AddArcAppAndShortcut(*appinfo);
// Widgets will be deleted by the system.
NotifyOnTaskCreated(*appinfo, 1 /* task_id */);
views::Widget* window1 = CreateArcWindow("org.chromium.arc.1");
ASSERT_TRUE(window1);
EXPECT_TRUE(window1->IsActive());
NotifyOnTaskCreated(*appinfo, 2 /* task_id */);
views::Widget* window2 = CreateArcWindow("org.chromium.arc.2");
ASSERT_TRUE(window2);
EXPECT_FALSE(window1->IsActive());
EXPECT_TRUE(window2->IsActive());
const std::string app_id = ArcAppTest::GetAppId(*appinfo);
ash::ShelfItemDelegate* item_delegate =
model_->GetShelfItemDelegate(ash::ShelfID(app_id));
ASSERT_TRUE(item_delegate);
// Selecting the item will show its application menu. It does not change the
// active window.
SelectItem(item_delegate);
EXPECT_FALSE(window1->IsActive());
EXPECT_TRUE(window2->IsActive());
// Command ids are just app window indices. Note, apps are registered in
// opposite order. Last created goes in front.
auto items = item_delegate->GetAppMenuItems(0, base::NullCallback());
ASSERT_EQ(items.size(), 2U);
// Execute command 1 to activate the first window.
item_delegate->ExecuteCommand(false, 1, ui::EF_NONE,
display::kInvalidDisplayId);
EXPECT_TRUE(window1->IsActive());
EXPECT_FALSE(window2->IsActive());
// Selecting the item will show its application menu. It does not change the
// active window.
SelectItem(item_delegate);
EXPECT_TRUE(window1->IsActive());
EXPECT_FALSE(window2->IsActive());
// Execute command 0 to activate the second window.
item_delegate->ExecuteCommand(false, 0, ui::EF_NONE,
display::kInvalidDisplayId);
EXPECT_FALSE(window1->IsActive());
EXPECT_TRUE(window2->IsActive());
}
namespace {
class ChromeShelfControllerArcDefaultAppsTest
: public ChromeShelfControllerTestBase {
public:
ChromeShelfControllerArcDefaultAppsTest() = default;
ChromeShelfControllerArcDefaultAppsTest(
const ChromeShelfControllerArcDefaultAppsTest&) = delete;
ChromeShelfControllerArcDefaultAppsTest& operator=(
const ChromeShelfControllerArcDefaultAppsTest&) = delete;
~ChromeShelfControllerArcDefaultAppsTest() override = default;
protected:
void SetUp() override {
ArcAppIcon::DisableSafeDecodingForTesting();
ArcDefaultAppList::UseTestAppsDirectory();
ChromeShelfControllerTestBase::SetUp();
}
};
class ChromeShelfControllerPlayStoreAvailabilityTest
: public ChromeShelfControllerTestBase,
public ::testing::WithParamInterface<bool> {
public:
ChromeShelfControllerPlayStoreAvailabilityTest() = default;
ChromeShelfControllerPlayStoreAvailabilityTest(
const ChromeShelfControllerPlayStoreAvailabilityTest&) = delete;
ChromeShelfControllerPlayStoreAvailabilityTest& operator=(
const ChromeShelfControllerPlayStoreAvailabilityTest&) = delete;
~ChromeShelfControllerPlayStoreAvailabilityTest() override = default;
protected:
void SetUp() override {
if (GetParam()) {
arc::SetArcAlwaysStartWithoutPlayStoreForTesting();
}
// To prevent crash on test exit and pending decode request.
ArcAppIcon::DisableSafeDecodingForTesting();
ArcDefaultAppList::UseTestAppsDirectory();
ChromeShelfControllerTestBase::SetUp();
}
};
} // namespace
// TODO(crbug.com/40890072) Test is flaky on ChromeOS.
#if BUILDFLAG(IS_CHROMEOS)
#define MAYBE_DefaultApps DISABLED_DefaultApps
#else
#define MAYBE_DefaultApps DefaultApps
#endif
TEST_F(ChromeShelfControllerArcDefaultAppsTest, MAYBE_DefaultApps) {
arc_test_.SetUp(profile());
InitShelfController();
ArcAppListPrefs* const prefs = arc_test_.arc_app_list_prefs();
EnablePlayStore(false);
EXPECT_FALSE(arc::IsArcPlayStoreEnabledForProfile(profile()));
ASSERT_TRUE(prefs->GetAppIds().size());
const std::string app_id =
ArcAppTest::GetAppId(*arc_test_.fake_default_apps()[0]);
const ash::ShelfID shelf_id(app_id);
EXPECT_FALSE(shelf_controller_->GetItem(shelf_id));
EXPECT_TRUE(arc::LaunchApp(profile(), app_id, ui::EF_LEFT_MOUSE_BUTTON,
arc::UserInteractionType::NOT_USER_INITIATED));
EXPECT_TRUE(arc::IsArcPlayStoreEnabledForProfile(profile()));
EXPECT_TRUE(shelf_controller_->GetItem(shelf_id));
// Stop ARC again. Shelf item should go away.
EnablePlayStore(false);
EXPECT_FALSE(shelf_controller_->GetItem(shelf_id));
EXPECT_TRUE(arc::LaunchApp(profile(), app_id, ui::EF_LEFT_MOUSE_BUTTON,
arc::UserInteractionType::NOT_USER_INITIATED));
EXPECT_TRUE(arc::IsArcPlayStoreEnabledForProfile(profile()));
EXPECT_TRUE(shelf_controller_->GetItem(shelf_id));
auto* item_delegate = model_->GetShelfItemDelegate(shelf_id);
ASSERT_TRUE(item_delegate);
EXPECT_TRUE(shelf_controller_->GetShelfSpinnerController()->HasApp(app_id));
// Initially, a default icon is set for the shelf item.
EXPECT_FALSE(item_delegate->image_set_by_controller());
std::string window_app_id("org.chromium.arc.1");
CreateArcWindow(window_app_id);
arc_test_.app_instance()->SendTaskCreated(
1, *arc_test_.fake_default_apps()[0], std::string());
EXPECT_TRUE(shelf_controller_->GetItem(shelf_id));
// Refresh delegate, it was changed.
item_delegate = model_->GetShelfItemDelegate(shelf_id);
ASSERT_TRUE(item_delegate);
EXPECT_FALSE(shelf_controller_->GetShelfSpinnerController()->HasApp(app_id));
EXPECT_FALSE(item_delegate->image_set_by_controller());
// Wait for the real app icon image to be decoded and set for the shelf item.
base::RunLoop().RunUntilIdle();
const std::vector<ui::ResourceScaleFactor>& scale_factors =
ui::GetSupportedResourceScaleFactors();
for (const auto scale_factor : scale_factors) {
// Force the icon to be loaded.
shelf_controller_->GetItem(shelf_id)->image.GetRepresentation(
ui::GetScaleForResourceScaleFactor(scale_factor));
}
EXPECT_TRUE(
ValidateImageIsFullyLoaded(shelf_controller_->GetItem(shelf_id)->image));
}
TEST_F(ChromeShelfControllerArcDefaultAppsTest, PlayStoreDeferredLaunch) {
// Add ARC host app to enable Play Store default app.
extension_registrar_->AddExtension(arc_support_host_.get());
arc_test_.SetUp(profile());
ArcAppListPrefs* const prefs = arc_test_.arc_app_list_prefs();
EXPECT_TRUE(prefs->IsRegistered(arc::kPlayStoreAppId));
InitShelfController();
EnablePlayStore(true);
// Pin Play Store. It should be pinned but not scheduled for deferred launch.
PinAppWithIDToShelf(arc::kPlayStoreAppId);
EXPECT_TRUE(shelf_controller_->IsAppPinned(arc::kPlayStoreAppId));
EXPECT_FALSE(shelf_controller_->GetShelfSpinnerController()->HasApp(
arc::kPlayStoreAppId));
// Simulate click. This should schedule Play Store for deferred launch.
ash::ShelfItemDelegate* item_delegate =
model_->GetShelfItemDelegate(ash::ShelfID(arc::kPlayStoreAppId));
EXPECT_TRUE(item_delegate);
SelectItem(item_delegate);
EXPECT_TRUE(shelf_controller_->IsAppPinned(arc::kPlayStoreAppId));
EXPECT_TRUE(shelf_controller_->GetShelfSpinnerController()->HasApp(
arc::kPlayStoreAppId));
}
TEST_F(ChromeShelfControllerArcDefaultAppsTest, PlayStoreLaunchMetric) {
extension_registrar_->AddExtension(arc_support_host_.get());
arc_test_.SetUp(profile());
ArcAppListPrefs* const prefs = arc_test_.arc_app_list_prefs();
InitShelfController();
EnablePlayStore(true);
// Play Store available now as a default app but is not ready yet.
std::unique_ptr<ArcAppListPrefs::AppInfo> app_info =
prefs->GetApp(arc::kPlayStoreAppId);
ASSERT_TRUE(app_info);
EXPECT_FALSE(app_info->ready);
constexpr char kHistogramName[] = "Arc.PlayStoreLaunch.TimeDelta";
// Launch Play Store in deferred mode.
arc::LaunchApp(profile(), arc::kPlayStoreAppId, ui::EF_LEFT_MOUSE_BUTTON,
arc::UserInteractionType::NOT_USER_INITIATED);
// This is deferred launch, no actual intents are delivered to ARC.
EXPECT_EQ(0U, arc_test_.app_instance()->launch_intents().size());
std::vector<arc::mojom::AppInfoPtr> apps;
apps.emplace_back(arc::mojom::AppInfo::New("", arc::kPlayStorePackage,
arc::kPlayStoreActivity));
arc_test_.app_instance()->SendRefreshAppList(apps);
ASSERT_EQ(1U, arc_test_.app_instance()->launch_intents().size());
std::string play_store_window_id("org.chromium.arc.1");
views::Widget* play_store_window = CreateArcWindow(play_store_window_id);
arc_test_.app_instance()->SendTaskCreated(
1, *apps[0], arc_test_.app_instance()->launch_intents()[0]);
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID(arc::kPlayStoreAppId)));
// UMA is reported since app becomes ready.
base::HistogramBase* const histogram =
base::StatisticsRecorder::FindHistogram(kHistogramName);
ASSERT_TRUE(histogram);
std::unique_ptr<base::HistogramSamples> samples = histogram->SnapshotDelta();
ASSERT_EQ(1, samples->TotalCount());
play_store_window->Close();
// Launch Play Store in app-ready mode.
arc::LaunchApp(profile(), arc::kPlayStoreAppId, ui::EF_LEFT_MOUSE_BUTTON,
arc::UserInteractionType::NOT_USER_INITIATED);
ASSERT_EQ(2U, arc_test_.app_instance()->launch_intents().size());
play_store_window_id = "org.chromium.arc.2";
play_store_window = CreateArcWindow(play_store_window_id);
arc_test_.app_instance()->SendTaskCreated(
2, *apps[0], arc_test_.app_instance()->launch_intents()[1]);
EXPECT_TRUE(shelf_controller_->GetItem(ash::ShelfID(arc::kPlayStoreAppId)));
// UMA is reported for app-ready launch. Note, previous call of SnapshotDelta
// resets samples, so we expect here only one recorded.
EXPECT_EQ(1, histogram->SnapshotDelta()->TotalCount());
play_store_window->Close();
}
TEST_F(ChromeShelfControllerArcDefaultAppsTest, DeferredLaunchMetric) {
extension_registrar_->AddExtension(arc_support_host_.get());
arc_test_.SetUp(profile());
InitShelfController();
EnablePlayStore(true);
constexpr char kHistogramName[] =
"Arc.FirstAppLaunchDelay.TimeDeltaUntilAppLaunch";
// Launch Play Store in deferred mode.
arc::LaunchApp(profile(), arc::kPlayStoreAppId, ui::EF_LEFT_MOUSE_BUTTON,
arc::UserInteractionType::NOT_USER_INITIATED);
EXPECT_FALSE(base::StatisticsRecorder::FindHistogram(kHistogramName));
std::vector<arc::mojom::AppInfoPtr> apps;
apps.emplace_back(arc::mojom::AppInfo::New("", arc::kPlayStorePackage,
arc::kPlayStoreActivity));
arc_test_.app_instance()->SendRefreshAppList(apps);
// No window attached at this time.
EXPECT_FALSE(base::StatisticsRecorder::FindHistogram(kHistogramName));
std::string play_store_window_id("org.chromium.arc.1");
views::Widget* const play_store_window =
CreateArcWindow(play_store_window_id);
ASSERT_EQ(1U, arc_test_.app_instance()->launch_intents().size());
arc_test_.app_instance()->SendTaskCreated(
1, *apps[0], arc_test_.app_instance()->launch_intents()[0]);
// UMA is reported since app becomes ready.
base::HistogramBase* const histogram =
base::StatisticsRecorder::FindHistogram(kHistogramName);
ASSERT_TRUE(histogram);
std::unique_ptr<base::HistogramSamples> samples = histogram->SnapshotDelta();
ASSERT_EQ(1, samples->TotalCount());
play_store_window->Close();
}
// Tests that the Play Store is not visible in AOSP image and visible in default
// images.
TEST_P(ChromeShelfControllerPlayStoreAvailabilityTest, Visible) {
extension_registrar_->AddExtension(arc_support_host_.get());
arc_test_.SetUp(profile());
InitShelfController();
StartPrefSyncService(syncer::SyncDataList());
ArcAppListPrefs* const prefs = arc_test_.arc_app_list_prefs();
EXPECT_EQ(arc::IsPlayStoreAvailable(),
prefs->IsRegistered(arc::kPlayStoreAppId));
// If the Play Store available, it is pinned by default.
EXPECT_EQ(arc::IsPlayStoreAvailable(),
shelf_controller_->IsAppPinned(arc::kPlayStoreAppId));
arc_test_.TearDown();
}
// Checks the case when several app items have the same ordinal position (which
// is valid case).
TEST_F(ChromeShelfControllerTest, CheckPositionConflict) {
InitShelfController();
extension_registrar_->AddExtension(extension1_.get());
extension_registrar_->AddExtension(extension2_.get());
AddWebApp(ash::kGmailAppId);
syncer::SyncChangeList sync_list;
InsertAddPinChange(&sync_list, 0, app_constants::kChromeAppId);
InsertAddPinChange(&sync_list, 1, extension1_->id());
InsertAddPinChange(&sync_list, 1, extension2_->id());
InsertAddPinChange(&sync_list, 1, ash::kGmailAppId);
SendPinChanges(sync_list, true);
EXPECT_EQ("Chrome, App1, App2, Gmail", GetPinnedAppStatus());
const syncer::StringOrdinal position_chrome =
app_list_syncable_service_->GetPinPosition(app_constants::kChromeAppId);
const syncer::StringOrdinal position_1 =
app_list_syncable_service_->GetPinPosition(extension1_->id());
const syncer::StringOrdinal position_2 =
app_list_syncable_service_->GetPinPosition(extension2_->id());
const syncer::StringOrdinal position_3 =
app_list_syncable_service_->GetPinPosition(ash::kGmailAppId);
EXPECT_TRUE(position_chrome.LessThan(position_1));
EXPECT_TRUE(position_1.Equals(position_2));
EXPECT_TRUE(position_2.Equals(position_3));
// Move Chrome between App1 and App2.
// Note, move target_index is in context when moved element is removed from
// array first.
model_->Move(0, 1);
EXPECT_EQ("App1, Chrome, App2, Gmail", GetPinnedAppStatus());
// Expect sync positions for only Chrome is updated and its resolution is
// after all duplicated ordinals.
EXPECT_TRUE(position_3.LessThan(
app_list_syncable_service_->GetPinPosition(app_constants::kChromeAppId)));
EXPECT_TRUE(position_1.Equals(
app_list_syncable_service_->GetPinPosition(extension1_->id())));
EXPECT_TRUE(position_1.Equals(
app_list_syncable_service_->GetPinPosition(extension1_->id())));
EXPECT_TRUE(position_2.Equals(
app_list_syncable_service_->GetPinPosition(extension2_->id())));
EXPECT_TRUE(position_3.Equals(
app_list_syncable_service_->GetPinPosition(ash::kGmailAppId)));
}
// Test the case when sync app is turned off and we need to use local copy to
// support user's pins.
TEST_F(ChromeShelfControllerTest, SyncOffLocalUpdate) {
InitShelfController();
extension_registrar_->AddExtension(extension1_.get());
extension_registrar_->AddExtension(extension2_.get());
syncer::SyncChangeList sync_list;
InsertAddPinChange(&sync_list, 0, app_constants::kChromeAppId);
InsertAddPinChange(&sync_list, 1, extension1_->id());
InsertAddPinChange(&sync_list, 1, extension2_->id());
SendPinChanges(sync_list, true);
EXPECT_EQ("Chrome, App1, App2", GetPinnedAppStatus());
syncer::SyncDataList copy_sync_list =
app_list_syncable_service_->GetAllSyncDataForTesting();
app_list_syncable_service_->StopSyncing(syncer::APP_LIST);
RecreateShelfController()->Init();
// Pinned state should not change.
EXPECT_EQ("Chrome, App1, App2", GetPinnedAppStatus());
shelf_controller_->UnpinAppWithID(extension2_->id());
EXPECT_EQ("Chrome, App1", GetPinnedAppStatus());
// Resume syncing and sync information overrides local copy.
StartAppSyncService(copy_sync_list);
base::RunLoop().RunUntilIdle();
EXPECT_EQ("Chrome, App1, App2", GetPinnedAppStatus());
}
// Test the Settings can be pinned and unpinned.
TEST_F(ChromeShelfControllerTest, InternalAppPinUnpin) {
InitShelfController();
// The model should only contain the browser shortcut item.
EXPECT_EQ(1, model_->item_count());
const std::string app_id = ash::kInternalAppIdSettings;
EXPECT_FALSE(shelf_controller_->IsAppPinned(app_id));
// Pin Settings.
PinAppWithIDToShelf(app_id);
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[1].type);
EXPECT_EQ(ash::STATUS_CLOSED, model_->items()[1].status);
EXPECT_TRUE(shelf_controller_->IsAppPinned(app_id));
// Unpin Settings.
shelf_controller_->UnpinAppWithID(app_id);
EXPECT_EQ(1, model_->item_count());
EXPECT_FALSE(shelf_controller_->IsAppPinned(app_id));
}
// TODO(b/194627475): Move these tests to chrome_shelf_controller_browsertest.cc
class ChromeShelfControllerDemoModeTest : public ChromeShelfControllerTestBase {
protected:
ChromeShelfControllerDemoModeTest() { auto_start_arc_test_ = true; }
ChromeShelfControllerDemoModeTest(const ChromeShelfControllerDemoModeTest&) =
delete;
ChromeShelfControllerDemoModeTest& operator=(
const ChromeShelfControllerDemoModeTest&) = delete;
~ChromeShelfControllerDemoModeTest() override = default;
void SetUp() override {
// To prevent crash on test exit and pending decode request.
ArcAppIcon::DisableSafeDecodingForTesting();
ChromeShelfControllerTestBase::SetUp();
// Fake Demo Mode.
demo_mode_test_helper_ = std::make_unique<ash::DemoModeTestHelper>();
GetInstallAttributes()->SetDemoMode();
demo_mode_test_helper_->InitializeSession();
}
void TearDown() override {
demo_mode_test_helper_.reset();
ChromeShelfControllerTestBase::TearDown();
}
private:
std::unique_ptr<ash::DemoModeTestHelper> demo_mode_test_helper_;
};
TEST_F(ChromeShelfControllerDemoModeTest, PinnedAppsOnline) {
network::TestNetworkConnectionTracker::GetInstance()->SetConnectionType(
network::mojom::ConnectionType::CONNECTION_ETHERNET);
InitShelfControllerWithBrowser();
extension_registrar_->AddExtension(extension1_.get());
extension_registrar_->AddExtension(extension2_.get());
arc::mojom::AppInfoPtr appinfo =
CreateAppInfo("Some App", "SomeActivity", "com.example.app");
const std::string app_id = AddArcAppAndShortcut(*appinfo);
arc::mojom::AppInfoPtr online_only_appinfo =
CreateAppInfo("Some App", "SomeActivity", "com.example.onlineonly");
const std::string online_only_app_id =
AddArcAppAndShortcut(*online_only_appinfo);
constexpr char kWebAppUrl[] = "https://test-pwa.com/";
webapps::AppId web_app_id = InstallExternalWebApp(kWebAppUrl);
// If the device is offline, extension2, onlineonly, and TestPWA should be
// unpinned. Since the device is online here, these apps should still be
// pinned, even though we're ignoring them here.
ash::DemoSession::Get()->OverrideIgnorePinPolicyAppsForTesting(
{extension2_->id(), online_only_appinfo->package_name});
SetPinnedLauncherAppsPolicy(extension1_->id(), extension2_->id(),
appinfo->package_name,
online_only_appinfo->package_name, kWebAppUrl);
// Since the device is online, all policy pinned apps are pinned.
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_EQ(AppListControllerDelegate::PIN_FIXED,
GetPinnableForAppID(extension1_->id(), profile()));
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_EQ(AppListControllerDelegate::PIN_FIXED,
GetPinnableForAppID(extension2_->id(), profile()));
EXPECT_TRUE(shelf_controller_->IsAppPinned(app_id));
EXPECT_EQ(AppListControllerDelegate::PIN_FIXED,
GetPinnableForAppID(app_id, profile()));
EXPECT_TRUE(shelf_controller_->IsAppPinned(online_only_app_id));
EXPECT_EQ(AppListControllerDelegate::PIN_FIXED,
GetPinnableForAppID(online_only_app_id, profile()));
EXPECT_TRUE(shelf_controller_->IsAppPinned(web_app_id));
EXPECT_EQ(AppListControllerDelegate::PIN_FIXED,
GetPinnableForAppID(web_app_id, profile()));
}
TEST_F(ChromeShelfControllerDemoModeTest, PinnedAppsOffline) {
network::TestNetworkConnectionTracker::GetInstance()->SetConnectionType(
network::mojom::ConnectionType::CONNECTION_NONE);
InitShelfControllerWithBrowser();
extension_registrar_->AddExtension(extension1_.get());
extension_registrar_->AddExtension(extension2_.get());
arc::mojom::AppInfoPtr appinfo =
CreateAppInfo("Some App", "SomeActivity", "com.example.app");
const std::string app_id = AddArcAppAndShortcut(*appinfo);
arc::mojom::AppInfoPtr online_only_appinfo =
CreateAppInfo("Some App", "SomeActivity", "com.example.onlineonly");
const std::string online_only_app_id =
AddArcAppAndShortcut(*online_only_appinfo);
constexpr char kWebAppUrl[] = "https://test-pwa.com/";
webapps::AppId web_app_id = InstallExternalWebApp(kWebAppUrl);
// If the device is offline, extension2 and onlineonly, and TestPWA should be
// unpinned.
ash::DemoSession::Get()->OverrideIgnorePinPolicyAppsForTesting(
{extension2_->id(), online_only_appinfo->package_name, kWebAppUrl});
SetPinnedLauncherAppsPolicy(extension1_->id(), extension2_->id(),
appinfo->package_name,
online_only_appinfo->package_name, kWebAppUrl);
// Since the device is offline, the policy pinned apps that shouldn't be
// pinned in Demo Mode are unpinned.
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_EQ(AppListControllerDelegate::PIN_FIXED,
GetPinnableForAppID(extension1_->id(), profile()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_EQ(AppListControllerDelegate::PIN_EDITABLE,
GetPinnableForAppID(extension2_->id(), profile()));
EXPECT_TRUE(shelf_controller_->IsAppPinned(app_id));
EXPECT_EQ(AppListControllerDelegate::PIN_FIXED,
GetPinnableForAppID(app_id, profile()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(online_only_app_id));
EXPECT_EQ(AppListControllerDelegate::PIN_EDITABLE,
GetPinnableForAppID(online_only_app_id, profile()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(web_app_id));
EXPECT_EQ(AppListControllerDelegate::PIN_EDITABLE,
GetPinnableForAppID(web_app_id, profile()));
// Pin a Chrome app that would have been pinned by policy but was suppressed
// for Demo Mode.
PinAppWithIDToShelf(extension2_->id());
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_EQ(AppListControllerDelegate::PIN_EDITABLE,
GetPinnableForAppID(extension2_->id(), profile()));
// Pin an ARC app that would have been pinned by policy but was suppressed for
// Demo Mode.
PinAppWithIDToShelf(online_only_app_id);
EXPECT_TRUE(shelf_controller_->IsAppPinned(online_only_app_id));
EXPECT_EQ(AppListControllerDelegate::PIN_EDITABLE,
GetPinnableForAppID(online_only_app_id, profile()));
// Pin a web app that would have been pinned by policy but was suppressed for
// Demo Mode.
PinAppWithIDToShelf(web_app_id);
EXPECT_TRUE(shelf_controller_->IsAppPinned(web_app_id));
EXPECT_EQ(AppListControllerDelegate::PIN_EDITABLE,
GetPinnableForAppID(web_app_id, profile()));
}
// Tests behavior for ensuring some component apps can be marked unpinnable.
TEST_F(ChromeShelfControllerTest, UnpinnableComponentApps) {
InitShelfController();
const char* kPinnableApp = file_manager::kFileManagerAppId;
const char* kNoPinApps[] = {ash::eche_app::kEcheAppId};
EXPECT_EQ(AppListControllerDelegate::PIN_EDITABLE,
GetPinnableForAppID(kPinnableApp, profile()));
for (const char* id : kNoPinApps) {
EXPECT_EQ(AppListControllerDelegate::NO_PIN,
GetPinnableForAppID(id, profile()));
}
}
TEST_F(ChromeShelfControllerTest, DoNotShowInShelf) {
syncer::SyncChangeList sync_list;
InsertAddPinChange(&sync_list, 0, extension1_->id());
InsertAddPinChange(&sync_list, 0, extension2_->id());
SendPinChanges(sync_list, true);
AddExtension(extension1_.get());
AddExtension(extension2_.get());
// Set App1.show_in_shelf to false.
std::vector<apps::AppPtr> apps;
apps::AppPtr app =
std::make_unique<apps::App>(apps::AppType::kChromeApp, extension1_->id());
app->show_in_shelf = false;
apps.push_back(std::move(app));
apps::AppServiceProxyFactory::GetForProfile(profile())->OnApps(
std::move(apps), apps::AppType::kChromeApp,
false /* should_notify_initialized */);
InitShelfController();
EXPECT_EQ("Chrome, App2", GetPinnedAppStatus());
EXPECT_FALSE(IsAppPinEditable(apps::AppType::kChromeApp, extension1_->id(),
profile()));
}
TEST_F(ChromeShelfControllerTest, OsFlagsNotShowInShelfNotPinnable) {
auto delegate = std::make_unique<OsFlagsSystemWebAppDelegate>(profile());
ash::SystemWebAppType app_type = delegate->GetType();
InstallSystemWebApp(std::move(delegate));
InitShelfController();
std::optional<webapps::AppId> app_id =
ash::SystemWebAppManager::GetForTest(profile())->GetAppIdForSystemApp(
app_type);
ASSERT_TRUE(app_id);
EXPECT_EQ("Chrome", GetPinnedAppStatus());
EXPECT_FALSE(IsAppPinEditable(apps::AppType::kSystemWeb, *app_id, profile()));
}
TEST_F(ChromeShelfControllerWithArcTest, ReplacePinnedItem) {
InitShelfController();
SendListOfArcApps();
const std::string arc_app_id1 =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[0]);
const std::string arc_app_id2 =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[1]);
extension_registrar_->AddExtension(extension1_.get());
extension_registrar_->AddExtension(extension2_.get());
PinAppWithIDToShelf(extension1_->id());
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(arc_app_id1));
// Replace pin extension to ARC app
shelf_controller_->ReplacePinnedItem(extension1_->id(), arc_app_id1);
EXPECT_TRUE(shelf_controller_->IsAppPinned(arc_app_id1));
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
// Replace pin ARC app to ARC app
shelf_controller_->ReplacePinnedItem(arc_app_id1, arc_app_id2);
EXPECT_TRUE(shelf_controller_->IsAppPinned(arc_app_id2));
EXPECT_FALSE(shelf_controller_->IsAppPinned(arc_app_id1));
// Replace pin ARC app to extension app
shelf_controller_->ReplacePinnedItem(arc_app_id2, extension1_->id());
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(arc_app_id2));
// Replace pin extension app to extension app
shelf_controller_->ReplacePinnedItem(extension1_->id(), extension2_->id());
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
// Try to replace item that is not pinned.
shelf_controller_->ReplacePinnedItem(arc_app_id2, extension1_->id());
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(shelf_controller_->IsAppPinned(arc_app_id2));
// Try to replace item with item that is already pinned.
PinAppWithIDToShelf(extension1_->id());
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
shelf_controller_->ReplacePinnedItem(extension2_->id(), extension1_->id());
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension2_->id()));
}
TEST_F(ChromeShelfControllerWithArcTest, PinAtIndex) {
InitShelfController();
SendListOfArcApps();
const std::string arc_app_id1 =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[0]);
const std::string arc_app_id2 =
ArcAppTest::GetAppId(*arc_test_.fake_apps()[1]);
extension_registrar_->AddExtension(extension1_.get());
extension_registrar_->AddExtension(extension2_.get());
int index = 0;
shelf_controller_->PinAppAtIndex(extension1_->id(), index);
EXPECT_EQ(index,
shelf_controller_->PinnedItemIndexByAppID(extension1_->id()));
shelf_controller_->PinAppAtIndex(extension2_->id(), index);
EXPECT_EQ(index,
shelf_controller_->PinnedItemIndexByAppID(extension2_->id()));
EXPECT_NE(index,
shelf_controller_->PinnedItemIndexByAppID(extension1_->id()));
index = 3;
shelf_controller_->PinAppAtIndex(arc_app_id1, index);
EXPECT_EQ(index, shelf_controller_->PinnedItemIndexByAppID(arc_app_id1));
// Test pinning at invalid index.
index = -100;
shelf_controller_->PinAppAtIndex(arc_app_id2, index);
EXPECT_NE(index, shelf_controller_->PinnedItemIndexByAppID(arc_app_id2));
EXPECT_EQ(-1, shelf_controller_->PinnedItemIndexByAppID(arc_app_id2));
// Test pinning already pinned app.
index = 0;
shelf_controller_->PinAppAtIndex(arc_app_id1, index);
EXPECT_NE(index, shelf_controller_->PinnedItemIndexByAppID(arc_app_id1));
EXPECT_EQ(3, shelf_controller_->PinnedItemIndexByAppID(arc_app_id1));
}
class ChromeShelfControllerWebAppTest : public ChromeShelfControllerTestBase {
protected:
ChromeShelfControllerWebAppTest() = default;
~ChromeShelfControllerWebAppTest() override = default;
};
// Test the web app interaction flow: pin it, run it, unpin it, close it.
TEST_F(ChromeShelfControllerWebAppTest, WebAppPinRunUnpinClose) {
constexpr char kWebAppUrl[] = "https://webappone.com/";
constexpr char kWebAppName[] = "WebApp1";
InitShelfController();
const webapps::AppId app_id = web_app::test::InstallDummyWebApp(
profile(), kWebAppName, GURL(kWebAppUrl));
base::RunLoop().RunUntilIdle();
// The model should only contain the browser shortcut item.
EXPECT_EQ(1, model_->item_count());
EXPECT_FALSE(shelf_controller_->IsAppPinned(app_id));
EXPECT_EQ(nullptr, shelf_controller_->GetItem(ash::ShelfID(app_id)));
// Pinning the app should create a new shelf item.
PinAppWithIDToShelf(app_id);
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[1].type);
EXPECT_EQ(ash::STATUS_CLOSED, model_->items()[1].status);
EXPECT_TRUE(shelf_controller_->IsAppPinned(app_id));
EXPECT_NE(nullptr, shelf_controller_->GetItem(ash::ShelfID(app_id)));
// Reporting that the app is running should just update the existing item.
shelf_controller_->SetAppStatus(app_id, ash::STATUS_RUNNING);
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ(ash::TYPE_PINNED_APP, model_->items()[1].type);
EXPECT_EQ(ash::STATUS_RUNNING, model_->items()[1].status);
EXPECT_TRUE(shelf_controller_->IsAppPinned(app_id));
EXPECT_NE(nullptr, shelf_controller_->GetItem(ash::ShelfID(app_id)));
// Unpinning the app should just update the existing item.
shelf_controller_->UnpinAppWithID(app_id);
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ(ash::TYPE_APP, model_->items()[1].type);
EXPECT_EQ(ash::STATUS_RUNNING, model_->items()[1].status);
EXPECT_FALSE(shelf_controller_->IsAppPinned(app_id));
EXPECT_NE(nullptr, shelf_controller_->GetItem(ash::ShelfID(app_id)));
// Reporting that the app is closed should remove its shelf item.
shelf_controller_->SetAppStatus(app_id, ash::STATUS_CLOSED);
EXPECT_EQ(1, model_->item_count());
EXPECT_FALSE(shelf_controller_->IsAppPinned(app_id));
EXPECT_EQ(nullptr, shelf_controller_->GetItem(ash::ShelfID(app_id)));
}
// Test the app status when the paused app is blocked, un-blocked, and un-paused
TEST_F(ChromeShelfControllerTest, VerifyAppStatusForPausedApp) {
AddExtension(extension1_.get());
// Set the app as paused
UpdateAppRegistryCache(profile(), extension1_->id(), false /* block */,
true /* pause */, std::nullopt /* show_in_shelf */);
InitShelfController();
PinAppWithIDToShelf(extension1_->id());
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ(ash::AppStatus::kPaused, model_->items()[1].app_status);
// Set the app as blocked
UpdateAppRegistryCache(profile(), extension1_->id(), true /* block */,
true /* pause */, std::nullopt /* show_in_shelf */);
EXPECT_EQ(ash::AppStatus::kBlocked, model_->items()[1].app_status);
// Set the app as ready, but still paused;
UpdateAppRegistryCache(profile(), extension1_->id(), false /* block */,
true /* pause */, std::nullopt /* show_in_shelf */);
EXPECT_EQ(ash::AppStatus::kPaused, model_->items()[1].app_status);
// Set the app as ready, and not paused;
UpdateAppRegistryCache(profile(), extension1_->id(), false /* block */,
false /* pause */, std::nullopt /* show_in_shelf */);
EXPECT_EQ(ash::AppStatus::kReady, model_->items()[1].app_status);
}
// Test the app status when the blocked app is paused, un-paused, hidden,
// visible and un-blocked
TEST_F(ChromeShelfControllerTest, VerifyAppStatusForBlockedApp) {
AddExtension(extension1_.get());
// Set the app as blocked
UpdateAppRegistryCache(profile(), extension1_->id(), true /* block */,
false /* pause */, std::nullopt /* show_in_shelf */);
InitShelfController();
PinAppWithIDToShelf(extension1_->id());
EXPECT_EQ(2, model_->item_count());
EXPECT_EQ(ash::AppStatus::kBlocked, model_->items()[1].app_status);
// Set the app as paused
UpdateAppRegistryCache(profile(), extension1_->id(), true /* block */,
true /* pause */, std::nullopt /* show_in_shelf */);
EXPECT_EQ(ash::AppStatus::kBlocked, model_->items()[1].app_status);
// Set the app as blocked, but un-paused
UpdateAppRegistryCache(profile(), extension1_->id(), true /* block */,
false /* pause */, std::nullopt /* show_in_shelf */);
EXPECT_EQ(ash::AppStatus::kBlocked, model_->items()[1].app_status);
// Set the app as ready, and not paused
UpdateAppRegistryCache(profile(), extension1_->id(), false /* block */,
false /* pause */, std::nullopt /* show_in_shelf */);
EXPECT_EQ(ash::AppStatus::kReady, model_->items()[1].app_status);
// Set the app as blocked and hidden
UpdateAppRegistryCache(profile(), extension1_->id(), true /* block */,
false /* pause */, false /* show_in_shelf */);
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
// Set the app as blocked and visible
UpdateAppRegistryCache(profile(), extension1_->id(), true /* block */,
false /* pause */, true /* show_in_shelf */);
EXPECT_EQ(ash::AppStatus::kBlocked, model_->items()[1].app_status);
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
// Set the app as ready
UpdateAppRegistryCache(profile(), extension1_->id(), false /* block */,
false /* pause */, std::nullopt /* show_in_shelf */);
EXPECT_EQ(ash::AppStatus::kReady, model_->items()[1].app_status);
}
TEST_F(ChromeShelfControllerTest, PinnedAppsRespectShownInShelfState) {
InitShelfController();
// Pin a test app.
AddExtension(extension1_.get());
PinAppWithIDToShelf(extension1_->id());
EXPECT_EQ(2, model_->item_count());
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_EQ(1, model_->ItemIndexByAppID(extension1_->id()));
// Update the app so it's considered not shown in shelf, and verify it's no
// longer pinned.
UpdateAppRegistryCache(profile(), extension1_->id(), /*block=*/false,
/*pause=*/false,
/*show_in_shelf=*/false);
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
// Update the app so it's allowed in shelf again, verify it gets pinned.
UpdateAppRegistryCache(profile(), extension1_->id(), /*block=*/false,
/*pause=*/false,
/*show_in_shelf=*/true);
EXPECT_TRUE(model_->IsAppPinned(extension1_->id()));
EXPECT_TRUE(model_->AllowedToSetAppPinState(extension1_->id(), false));
EXPECT_EQ(1, model_->ItemIndexByAppID(extension1_->id()));
}
TEST_F(ChromeShelfControllerTest, AppIndexAfterUnhidingFirstPinnedApp) {
InitShelfController();
// Pin a test app.
AddExtension(extension1_.get());
PinAppWithIDToShelf(extension1_->id());
EXPECT_EQ(2, model_->item_count());
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension1_->id()));
EXPECT_EQ(1, model_->ItemIndexByAppID(extension1_->id()));
model_->Move(1, 0);
EXPECT_EQ(0, model_->ItemIndexByAppID(extension1_->id()));
// Update the app so it's considered not shown in shelf, and verify it's no
// longer pinned.
UpdateAppRegistryCache(profile(), extension1_->id(), /*block=*/false,
/*pause=*/false,
/*show_in_shelf=*/false);
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension1_->id()));
// Update the app so it's allowed in shelf again, verify it gets pinned.
UpdateAppRegistryCache(profile(), extension1_->id(), /*block=*/false,
/*pause=*/false,
/*show_in_shelf=*/true);
EXPECT_TRUE(model_->IsAppPinned(extension1_->id()));
EXPECT_TRUE(model_->AllowedToSetAppPinState(extension1_->id(), false));
EXPECT_EQ(0, model_->ItemIndexByAppID(extension1_->id()));
}
TEST_F(ChromeShelfControllerTest,
AppIndexAfterUnhidingtPinnedAppWithOtherHiddenApps) {
InitShelfController();
// Pin test apps.
AddExtension(extension1_.get());
AddExtension(extension2_.get());
AddExtension(extension5_.get());
AddExtension(extension6_.get());
PinAppWithIDToShelf(extension1_->id());
PinAppWithIDToShelf(extension2_->id());
PinAppWithIDToShelf(extension5_->id());
PinAppWithIDToShelf(extension6_->id());
EXPECT_EQ(5, model_->item_count());
EXPECT_TRUE(shelf_controller_->IsAppPinned(extension2_->id()));
EXPECT_EQ(2, model_->ItemIndexByAppID(extension2_->id()));
// Update all test apps so they're considered not shown in shelf, and verify
// it's no longer pinned.
UpdateAppRegistryCache(profile(), extension1_->id(), /*block=*/false,
/*pause=*/false,
/*show_in_shelf=*/false);
UpdateAppRegistryCache(profile(), extension2_->id(), /*block=*/false,
/*pause=*/false,
/*show_in_shelf=*/false);
UpdateAppRegistryCache(profile(), extension5_->id(), /*block=*/false,
/*pause=*/false,
/*show_in_shelf=*/false);
EXPECT_FALSE(shelf_controller_->IsAppPinned(extension2_->id()));
// Update app 2 so it's allowed in shelf again, verify it gets pinned.
UpdateAppRegistryCache(profile(), extension2_->id(), /*block=*/false,
/*pause=*/false,
/*show_in_shelf=*/true);
EXPECT_TRUE(model_->IsAppPinned(extension2_->id()));
EXPECT_TRUE(model_->AllowedToSetAppPinState(extension2_->id(), false));
EXPECT_EQ(1, model_->ItemIndexByAppID(extension2_->id()));
EXPECT_EQ(2, model_->ItemIndexByAppID(extension6_->id()));
}
TEST_F(ChromeShelfControllerTest, AppsHiddenFromShelfDontGetPinnedByPolicy) {
AddExtension(extension1_.get());
// Pin a test app by policy.
SetPinnedLauncherAppsPolicy(extension1_->id());
InitShelfController();
EXPECT_EQ(2, model_->item_count());
EXPECT_TRUE(model_->IsAppPinned(extension1_->id()));
EXPECT_EQ(1, model_->ItemIndexByAppID(extension1_->id()));
// Update the app so it's considered not shown in shelf, and verify it's no
// longer pinned.
UpdateAppRegistryCache(profile(), extension1_->id(), /*block=*/false,
/*pause=*/false,
/*show_in_shelf=*/false);
EXPECT_FALSE(model_->IsAppPinned(extension1_->id()));
// Update the app so it's allowed in shelf again, verify it gets pinned.
UpdateAppRegistryCache(profile(), extension1_->id(), /*block=*/false,
/*pause=*/false,
/*show_in_shelf=*/true);
EXPECT_TRUE(model_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(model_->AllowedToSetAppPinState(extension1_->id(), false));
EXPECT_EQ(1, model_->ItemIndexByAppID(extension1_->id()));
}
TEST_F(ChromeShelfControllerTest, AppHiddenFromShelfNotPinnedOnInstall) {
AddExtension(extension1_.get());
InitShelfController();
const std::string app_id = extension1_->id();
const apps::AppType app_type =
apps::AppServiceProxyFactory::GetForProfile(profile())
->AppRegistryCache()
.GetAppType(app_id);
PinAppWithIDToShelf(app_id);
EXPECT_EQ(2, model_->item_count());
EXPECT_TRUE(model_->IsAppPinned(app_id));
EXPECT_EQ(1, model_->ItemIndexByAppID(app_id));
EXPECT_TRUE(IsAppPinEditable(app_type, app_id, profile()));
// Block the extension so it gets removed from shelf.
UpdateAppRegistryCache(profile(), app_id, /*block=*/true,
/*pause=*/false,
/*show_in_shelf=*/false);
EXPECT_FALSE(model_->IsAppPinned(app_id));
// Unblock the extension, but mark it as not shown in shelf - verify it
// doesn't get pinned/added to shelf.
UpdateAppRegistryCache(profile(), app_id, /*block=*/false,
/*pause=*/false,
/*show_in_shelf=*/false);
EXPECT_FALSE(model_->IsAppPinned(app_id));
EXPECT_FALSE(IsAppPinEditable(app_type, app_id, profile()));
// Allow the app to be shown in shelf, and verify it gets pinned again.
UpdateAppRegistryCache(profile(), extension1_->id(), /*block=*/false,
/*pause=*/false,
/*show_in_shelf=*/true);
EXPECT_TRUE(model_->IsAppPinned(extension1_->id()));
EXPECT_TRUE(model_->AllowedToSetAppPinState(extension1_->id(), false));
EXPECT_EQ(1, model_->ItemIndexByAppID(extension1_->id()));
EXPECT_TRUE(IsAppPinEditable(app_type, app_id, profile()));
}
class ChromeShelfControllerPromiseAppsTest : public ChromeShelfControllerTest,
public ash::ShelfModelObserver {
public:
ChromeShelfControllerPromiseAppsTest() {
auto_start_arc_test_ = true;
feature_list_.InitAndEnableFeature(ash::features::kPromiseIcons);
}
~ChromeShelfControllerPromiseAppsTest() override = default;
void SetUp() override {
// To prevent crash on test exit and pending decode request.
ArcAppIcon::DisableSafeDecodingForTesting();
ChromeShelfControllerTestBase::SetUp();
}
SkBitmap ApplyEffectsToBitmap(SkBitmap bitmap, apps::IconEffects effects) {
auto iv = std::make_unique<apps::IconValue>();
iv->uncompressed = gfx::ImageSkia::CreateFromBitmap(bitmap, 1.0f);
iv->icon_type = apps::IconType::kUncompressed;
base::test::TestFuture<apps::IconValuePtr> image_with_effects;
apps::ApplyIconEffects(/*profile=*/nullptr, /*app_id=*/std::nullopt,
effects, bitmap.width(), std::move(iv),
image_with_effects.GetCallback());
return *image_with_effects.Get()->uncompressed.bitmap();
}
apps::PromiseAppRegistryCache* cache() {
return apps::AppServiceProxyFactory::GetForProfile(profile())
->PromiseAppRegistryCache();
}
apps::PromiseAppService* service() {
return apps::AppServiceProxyFactory::GetForProfile(profile())
->PromiseAppService();
}
void WaitForItemUpdate() {
if (!obs_.IsObserving()) {
obs_.Observe(model_.get());
}
wait_run_loop_ = std::make_unique<base::RunLoop>();
wait_run_loop_->Run();
}
// ShelfModelObserver overrides:
void ShelfItemChanged(int, const ash::ShelfItem&) override {
if (wait_run_loop_ && wait_run_loop_->running()) {
wait_run_loop_->Quit();
}
}
void ShelfItemAdded(int) override {
if (wait_run_loop_ && wait_run_loop_->running()) {
wait_run_loop_->Quit();
}
}
private:
base::test::ScopedFeatureList feature_list_;
base::ScopedObservation<ash::ShelfModel, ash::ShelfModelObserver> obs_{this};
std::unique_ptr<base::RunLoop> wait_run_loop_;
};
TEST_F(ChromeShelfControllerPromiseAppsTest, PromiseAppUpdatesShelfItem) {
// Register a promise app.
const apps::PackageId package_id =
apps::PackageId(apps::PackageType::kArc, "com.example.test");
apps::PromiseAppPtr promise_app =
std::make_unique<apps::PromiseApp>(package_id);
promise_app->status = apps::PromiseStatus::kPending;
promise_app->name = "App Name";
promise_app->should_show = true;
cache()->OnPromiseApp(std::move(promise_app));
// Create a shelf item for the promise app.
InitShelfController();
PinAppWithIDToShelf(package_id.ToString());
// Verify the details of the shelf item.
EXPECT_TRUE(model_->IsAppPinned(package_id.ToString()));
ash::ShelfID id(package_id.ToString());
const ash::ShelfItem* item = shelf_controller_->GetItem(id);
EXPECT_EQ(item->title, ShelfControllerHelper::GetLabelForPromiseStatus(
apps::PromiseStatus::kPending));
EXPECT_EQ(item->accessible_name,
ShelfControllerHelper::GetAccessibleLabelForPromiseStatus(
"App Name", apps::PromiseStatus::kPending));
EXPECT_EQ(item->progress, 0);
EXPECT_EQ(item->app_status, ash::AppStatus::kPending);
// Push an progress and status update to the promise app.
apps::PromiseAppPtr update = std::make_unique<apps::PromiseApp>(package_id);
update->progress = 0.3;
update->status = apps::PromiseStatus::kInstalling;
cache()->OnPromiseApp(std::move(update));
// Verify that the shelf item has updated details.
EXPECT_EQ(item->title, ShelfControllerHelper::GetLabelForPromiseStatus(
apps::PromiseStatus::kInstalling));
EXPECT_EQ(item->accessible_name,
ShelfControllerHelper::GetAccessibleLabelForPromiseStatus(
"App Name", apps::PromiseStatus::kInstalling));
EXPECT_EQ(item->progress, 0.3f);
EXPECT_EQ(item->app_status, ash::AppStatus::kInstalling);
}
TEST_F(ChromeShelfControllerPromiseAppsTest,
PromiseAppUpdatesCorrectShelfItem) {
// Register the main promise app that we will check the updates for.
const apps::PackageId package_id =
apps::PackageId(apps::PackageType::kArc, "main.package.for.test");
apps::PromiseAppPtr promise_app =
std::make_unique<apps::PromiseApp>(package_id);
promise_app->status = apps::PromiseStatus::kPending;
promise_app->should_show = true;
cache()->OnPromiseApp(std::move(promise_app));
// Register another promise app that will have a shelf item but which we do
// not expect updates for.
const apps::PackageId other_package_id =
apps::PackageId(apps::PackageType::kArc, "other.package");
apps::PromiseAppPtr other_promise_app =
std::make_unique<apps::PromiseApp>(other_package_id);
other_promise_app->status = apps::PromiseStatus::kPending;
other_promise_app->should_show = true;
cache()->OnPromiseApp(std::move(other_promise_app));
// Create shelf items for the promise apps.
InitShelfController();
PinAppWithIDToShelf(package_id.ToString());
PinAppWithIDToShelf(other_package_id.ToString());
// Verify the status of the main shelf item.
EXPECT_TRUE(model_->IsAppPinned(package_id.ToString()));
ash::ShelfID id(package_id.ToString());
const ash::ShelfItem* item = shelf_controller_->GetItem(id);
EXPECT_EQ(item->app_status, ash::AppStatus::kPending);
// Verify the status of the other shelf item.
EXPECT_TRUE(model_->IsAppPinned(other_package_id.ToString()));
ash::ShelfID other_id(other_package_id.ToString());
const ash::ShelfItem* other_item = shelf_controller_->GetItem(other_id);
EXPECT_EQ(other_item->app_status, ash::AppStatus::kPending);
// Push an update to the main promise app.
apps::PromiseAppPtr update = std::make_unique<apps::PromiseApp>(package_id);
update->status = apps::PromiseStatus::kInstalling;
cache()->OnPromiseApp(std::move(update));
// Verify that the main shelf item has an updated status.
EXPECT_EQ(item->app_status, ash::AppStatus::kInstalling);
// Verify that the other shelf item remains the same.
EXPECT_EQ(other_item->app_status, ash::AppStatus::kPending);
}
TEST_F(ChromeShelfControllerPromiseAppsTest,
ShelfItemFetchesAndAppliesEffectsToIcon) {
// Register the main promise app that we will check the updates for.
const apps::PackageId package_id =
apps::PackageId(apps::PackageType::kArc, "com.example.test");
apps::PromiseAppPtr promise_app =
std::make_unique<apps::PromiseApp>(package_id);
promise_app->status = apps::PromiseStatus::kPending;
promise_app->should_show = true;
cache()->OnPromiseApp(std::move(promise_app));
// Add a test icon for the promise app.
SkBitmap base_bitmap =
gfx::test::CreateBitmap(extension_misc::EXTENSION_ICON_MEDIUM,
extension_misc::EXTENSION_ICON_MEDIUM);
apps::PromiseAppIconPtr icon = std::make_unique<apps::PromiseAppIcon>();
icon->icon = base_bitmap;
icon->width_in_pixels = extension_misc::EXTENSION_ICON_MEDIUM;
service()->PromiseAppIconCache()->SaveIcon(package_id, std::move(icon));
// Create shelf items for the promise apps. This should trigger the
// AppServicePromiseAppIconLoader to fetch the image.
InitShelfController();
PinAppWithIDToShelf(package_id.ToString());
WaitForItemUpdate();
// Verify that the icon has the correct effects applied to it.
EXPECT_TRUE(model_->IsAppPinned(package_id.ToString()));
ash::ShelfID id(package_id.ToString());
const ash::ShelfItem* item = shelf_controller_->GetItem(id);
SkBitmap result_bitmap = *item->image.bitmap();
SkBitmap expected_bitmap =
ApplyEffectsToBitmap(base_bitmap, apps::IconEffects::kCrOsStandardMask);
EXPECT_TRUE(gfx::BitmapsAreEqual(result_bitmap, expected_bitmap));
}
TEST_F(ChromeShelfControllerPromiseAppsTest, RemoveShelfItem) {
// Register a promise app.
apps::AppType app_type = apps::AppType::kArc;
std::string identifier = "test.com.example";
apps::PackageId package_id(apps::PackageType::kArc, identifier);
apps::PromiseAppPtr promise_app =
std::make_unique<apps::PromiseApp>(package_id);
promise_app->progress = 0.9;
promise_app->should_show = true;
cache()->OnPromiseApp(std::move(promise_app));
// Create a shelf item for the promise app.
InitShelfController();
PinAppWithIDToShelf(package_id.ToString());
// Verify the details of the shelf item.
EXPECT_TRUE(model_->IsAppPinned(package_id.ToString()));
ash::ShelfID id(package_id.ToString());
const ash::ShelfItem* item = shelf_controller_->GetItem(id);
EXPECT_TRUE(item);
// Register (i.e. "install") an app in AppRegistryCache with a matching
// package ID. This should trigger removal of the promise app and hence the
// unpinning of the shelf item.
std::string app_id = "qwertyuiopasdfghjkl";
apps::AppPtr app = std::make_unique<apps::App>(app_type, app_id);
app->publisher_id = identifier;
app->readiness = apps::Readiness::kReady;
std::vector<apps::AppPtr> apps;
apps.push_back(std::move(app));
apps::AppServiceProxyFactory::GetForProfile(profile())->OnApps(
std::move(apps), app_type,
/*should_notify_initialized=*/false);
// Item should no longer be in the shelf.
EXPECT_FALSE(model_->IsAppPinned(package_id.ToString()));
item = shelf_controller_->GetItem(id);
EXPECT_FALSE(item);
}
TEST_F(ChromeShelfControllerPromiseAppsTest, PinnedPromiseAppShelfItemType) {
// Register a promise app.
const apps::PackageId package_id =
apps::PackageId(apps::PackageType::kArc, "com.example.test");
apps::PromiseAppPtr promise_app =
std::make_unique<apps::PromiseApp>(package_id);
promise_app->should_show = true;
cache()->OnPromiseApp(std::move(promise_app));
// Create a shelf item for the promise app.
InitShelfController();
PinAppWithIDToShelf(package_id.ToString());
// Verify the app is identifiable as a promise app.
EXPECT_TRUE(model_->IsAppPinned(package_id.ToString()));
ash::ShelfID id(package_id.ToString());
const ash::ShelfItem* item = shelf_controller_->GetItem(id);
EXPECT_TRUE(item->is_promise_app);
}
TEST_F(ChromeShelfControllerPromiseAppsTest,
PromiseAppGetsPinnedByMatchingSyncData) {
const apps::PackageId package_id =
apps::PackageId(apps::PackageType::kArc, "com.example.test");
// Add entry in sync data that has a matching PackageId with the promise app.
SendPinChanges(syncer::SyncChangeList(), true);
StopAppSyncService();
syncer::SyncDataList sync_list;
sync_list.push_back((app_list::CreateAppRemoteData(
"asdfghjkl", "App Name", /*parent_id=*/std::string(), "ordinal",
/*item_pin_ordinal=*/
syncer::StringOrdinal::CreateInitialOrdinal().ToDebugString(),
/*item_type=*/sync_pb::AppListSpecifics_AppListItemType_TYPE_APP,
/*promise_package_id=*/package_id.ToString())));
StartAppSyncService(sync_list);
base::RunLoop().RunUntilIdle();
InitShelfController();
// Register a new promise app.
apps::PromiseAppPtr promise_app =
std::make_unique<apps::PromiseApp>(package_id);
promise_app->should_show = true;
cache()->OnPromiseApp(std::move(promise_app));
// Verify that the promise app is in the shelf without having to explicitly
// pin it.
EXPECT_TRUE(model_->IsAppPinned(package_id.ToString()));
ash::ShelfID id(package_id.ToString());
const ash::ShelfItem* item = shelf_controller_->GetItem(id);
ASSERT_TRUE(item);
EXPECT_TRUE(item->is_promise_app);
}
TEST_F(ChromeShelfControllerPromiseAppsTest,
PromiseAppNotPinnedByMatchingSyncDataIfNotReadyToBeShown) {
const apps::PackageId package_id =
apps::PackageId(apps::PackageType::kArc, "com.example.test");
// Add entry in sync data that has a matching PackageId with our promise app.
SendPinChanges(syncer::SyncChangeList(), true);
StopAppSyncService();
syncer::SyncDataList sync_list;
sync_list.push_back((app_list::CreateAppRemoteData(
"asdfghjkl", "App Name", /*parent_id=*/std::string(), "ordinal",
/*item_pin_ordinal=*/
syncer::StringOrdinal::CreateInitialOrdinal().ToDebugString(),
/*item_type=*/sync_pb::AppListSpecifics_AppListItemType_TYPE_APP,
/*promise_package_id=*/package_id.ToString())));
StartAppSyncService(sync_list);
base::RunLoop().RunUntilIdle();
InitShelfController();
// Verify no promise app shelf item is created if promise app isn't present in
// the PromiseAppRegistryCache.
EXPECT_FALSE(model_->IsAppPinned(package_id.ToString()));
// Register a new promise app with the default should_show=false.
apps::PromiseAppPtr promise_app =
std::make_unique<apps::PromiseApp>(package_id);
cache()->OnPromiseApp(std::move(promise_app));
// Trigger the shelf to update the pinned apps.
SendPinChanges(syncer::SyncChangeList(), /*reset_pin_model=*/false);
// Verify that the promise app is still not on the shelf.
EXPECT_FALSE(model_->IsAppPinned(package_id.ToString()));
}
// This test verifies that the sync data will trigger the creation of a
// ShelfItem with specific promise app fields for a promise app registration, or
// a regular ShelfItem for an installed app. We should not create a regular
// ShelfItem for a promise app or a ShelfItem with promise app fields for an
// installed app.
TEST_F(ChromeShelfControllerPromiseAppsTest, SyncDataCreatesCorrectShelfItem) {
std::string package_name = "com.example.test";
const apps::PackageId package_id =
apps::PackageId(apps::PackageType::kArc, package_name);
std::string app_id = "bijolhehmgkcaahdbconomepmenmeomc";
// Add entry in sync data that has a matching PackageId with our promise app.
SendPinChanges(syncer::SyncChangeList(), true);
StopAppSyncService();
syncer::SyncDataList sync_list;
sync_list.push_back((app_list::CreateAppRemoteData(
app_id, "App Name", /*parent_id=*/std::string(), "ordinal",
/*item_pin_ordinal=*/GeneratePinPosition(1).ToDebugString(),
/*item_type=*/sync_pb::AppListSpecifics_AppListItemType_TYPE_APP,
/*promise_package_id=*/package_id.ToString())));
StartAppSyncService(sync_list);
base::RunLoop().RunUntilIdle();
InitShelfController();
// Start the promise app installation but don't allow it to be visible yet.
apps::PromiseAppPtr promise_app =
std::make_unique<apps::PromiseApp>(package_id);
promise_app->should_show = false;
cache()->OnPromiseApp(std::move(promise_app));
// Force an update to the shelf items pinned by sync.
SendPinChanges(syncer::SyncChangeList(), /*reset_pin_model=*/false);
// No shelf items should be created for the sync data.
EXPECT_FALSE(model_->IsAppPinned(app_id));
EXPECT_FALSE(model_->IsAppPinned(package_id.ToString()));
// Update the visibility of the promise app.
apps::PromiseAppPtr promise_app_update =
std::make_unique<apps::PromiseApp>(package_id);
promise_app_update->should_show = true;
promise_app_update->progress = 0.3;
cache()->OnPromiseApp(std::move(promise_app_update));
// Only the promise app ShelfItem should exist.
EXPECT_FALSE(model_->IsAppPinned(app_id));
EXPECT_TRUE(model_->IsAppPinned(package_id.ToString()));
// Confirm the promise app ShelfItem fields.
ash::ShelfID promise_shelf_id(package_id.ToString());
const ash::ShelfItem* promise_item =
shelf_controller_->GetItem(promise_shelf_id);
ASSERT_TRUE(promise_item);
EXPECT_TRUE(promise_item->is_promise_app);
EXPECT_EQ(promise_item->progress, 0.3f);
EXPECT_EQ(promise_item->package_id, package_id.ToString());
// Add an app in ArcAppListPrefs, which will register an app in App Service.
// We need to add an app into ArcAppListPrefs (and not just the
// AppRegistryCache) because ChromeShelfPrefs checks ArcAppListPrefs to
// confirm whether an app ID is valid when retrieving all the pinned apps from
// sync.
arc::mojom::AppInfoPtr app_info =
CreateAppInfo("Test Name", "Test Activity", "com.example.test");
AddArcAppAndShortcut(*app_info);
// Only the installed app shelf item should exist.
EXPECT_TRUE(model_->IsAppPinned(app_id));
EXPECT_FALSE(model_->IsAppPinned(package_id.ToString()));
// Confirm that the ShelfItem is not for the promise app.
ash::ShelfID shelf_id(app_id);
const ash::ShelfItem* app_item = shelf_controller_->GetItem(shelf_id);
ASSERT_TRUE(app_item);
EXPECT_FALSE(app_item->is_promise_app);
EXPECT_EQ(app_item->progress, -1);
EXPECT_EQ(app_item->package_id, package_id.ToString());
}
TEST_F(ChromeShelfControllerPromiseAppsTest, ShelfItemCreationUpdatesMetrics) {
base::HistogramTester histogram_tester;
histogram_tester.ExpectBucketCount(
apps::kPromiseAppLifecycleEventHistogram,
apps::PromiseAppLifecycleEvent::kCreatedInShelf, 0);
const apps::PackageId package_id =
apps::PackageId(apps::PackageType::kArc, "com.example.test");
apps::PromiseAppPtr promise_app =
std::make_unique<apps::PromiseApp>(package_id);
promise_app->should_show = true;
cache()->OnPromiseApp(std::move(promise_app));
InitShelfController();
PinAppWithIDToShelf(package_id.ToString());
EXPECT_TRUE(model_->IsAppPinned(package_id.ToString()));
histogram_tester.ExpectBucketCount(
apps::kPromiseAppLifecycleEventHistogram,
apps::PromiseAppLifecycleEvent::kCreatedInShelf, 1);
}
INSTANTIATE_TEST_SUITE_P(All,
ChromeShelfControllerPlayStoreAvailabilityTest,
::testing::Values(false, true));
|