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
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h"
#include <algorithm>
#include <string>
#include <vector>
#include "ash/ash_switches.h"
#include "ash/shelf/shelf_item_delegate_manager.h"
#include "ash/shelf/shelf_model.h"
#include "ash/shelf/shelf_model_observer.h"
#include "ash/shell.h"
#include "ash/test/shelf_item_delegate_manager_test_api.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/ui/ash/chrome_launcher_prefs.h"
#include "chrome/browser/ui/ash/launcher/app_window_launcher_item_controller.h"
#include "chrome/browser/ui/ash/launcher/launcher_application_menu_item_model.h"
#include "chrome/browser/ui/ash/launcher/launcher_item_controller.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/host_desktop.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/browser_with_test_window_test.h"
#include "chrome/test/base/testing_pref_service_syncable.h"
#include "chrome/test/base/testing_profile.h"
#include "content/public/browser/web_contents.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_constants.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/aura/client/window_tree_client.h"
#include "ui/base/models/menu_model.h"
#if defined(OS_CHROMEOS)
#include "ash/test/test_session_state_delegate.h"
#include "ash/test/test_shell_delegate.h"
#include "chrome/browser/apps/scoped_keep_alive.h"
#include "chrome/browser/chromeos/login/users/fake_user_manager.h"
#include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
#include "chrome/browser/ui/apps/chrome_app_delegate.h"
#include "chrome/browser/ui/ash/launcher/app_window_launcher_controller.h"
#include "chrome/browser/ui/ash/launcher/browser_status_monitor.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_window_manager_chromeos.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile_manager.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/test/test_utils.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 "ui/aura/window.h"
#endif
using base::ASCIIToUTF16;
using extensions::Extension;
using extensions::Manifest;
using extensions::UnloadedExtensionInfo;
namespace {
const char* offline_gmail_url = "https://mail.google.com/mail/mu/u";
const char* gmail_url = "https://mail.google.com/mail/u";
const char* kGmailLaunchURL = "https://mail.google.com/mail/ca";
#if defined(OS_CHROMEOS)
// An extension prefix.
const char kCrxAppPrefix[] = "_crx_";
#endif
// ShelfModelObserver implementation that tracks what messages are invoked.
class TestShelfModelObserver : public ash::ShelfModelObserver {
public:
TestShelfModelObserver()
: added_(0),
removed_(0),
changed_(0) {
}
~TestShelfModelObserver() override {}
// Overridden from ash::ShelfModelObserver:
void ShelfItemAdded(int index) override {
++added_;
last_index_ = index;
}
void ShelfItemRemoved(int index, ash::ShelfID id) override {
++removed_;
last_index_ = index;
}
void ShelfItemChanged(int index, const ash::ShelfItem& old_item) override {
++changed_;
last_index_ = index;
}
void ShelfItemMoved(int start_index, int target_index) override {
last_index_ = target_index;
}
void ShelfStatusChanged() override {}
void clear_counts() {
added_ = 0;
removed_ = 0;
changed_ = 0;
last_index_ = 0;
}
int added() const { return added_; }
int removed() const { return removed_; }
int changed() const { return changed_; }
int last_index() const { return last_index_; }
private:
int added_;
int removed_;
int changed_;
int last_index_;
DISALLOW_COPY_AND_ASSIGN(TestShelfModelObserver);
};
// Test implementation of AppIconLoader.
class TestAppIconLoaderImpl : public extensions::AppIconLoader {
public:
TestAppIconLoaderImpl() : fetch_count_(0) {
}
~TestAppIconLoaderImpl() override {}
// AppIconLoader implementation:
void FetchImage(const std::string& id) override { ++fetch_count_; }
void ClearImage(const std::string& id) override {}
void UpdateImage(const std::string& id) override {}
int fetch_count() const { return fetch_count_; }
private:
int fetch_count_;
DISALLOW_COPY_AND_ASSIGN(TestAppIconLoaderImpl);
};
// Test implementation of AppTabHelper.
class TestAppTabHelperImpl : public ChromeLauncherController::AppTabHelper {
public:
TestAppTabHelperImpl() {}
~TestAppTabHelperImpl() override {}
// Sets the id for the specified tab. The id is removed if Remove() is
// invoked.
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();
}
// AppTabHelper implementation:
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) override {
for (TabToStringMap::const_iterator i = tab_id_map_.begin();
i != tab_id_map_.end(); ++i) {
if (i->second == id)
return true;
}
return false;
}
void SetCurrentUser(Profile* profile) override {
// We can ignore this for now.
}
private:
typedef std::map<content::WebContents*, std::string> TabToStringMap;
TabToStringMap tab_id_map_;
DISALLOW_COPY_AND_ASSIGN(TestAppTabHelperImpl);
};
// Test implementation of a V2 app launcher item controller.
class TestV2AppLauncherItemController : public LauncherItemController {
public:
TestV2AppLauncherItemController(const std::string& app_id,
ChromeLauncherController* controller)
: LauncherItemController(LauncherItemController::TYPE_APP,
app_id,
controller) {
}
~TestV2AppLauncherItemController() override {}
// Override for LauncherItemController:
bool IsOpen() const override { return true; }
bool IsVisible() const override { return true; }
void Launch(ash::LaunchSource source, int event_flags) override {}
bool Activate(ash::LaunchSource source) override { return false; }
void Close() override {}
bool ItemSelected(const ui::Event& event) override { return false; }
base::string16 GetTitle() override { return base::string16(); }
ChromeLauncherAppMenuItems GetApplicationList(int event_flags) override {
ChromeLauncherAppMenuItems items;
items.push_back(
new ChromeLauncherAppMenuItem(base::string16(), NULL, false));
items.push_back(
new ChromeLauncherAppMenuItem(base::string16(), NULL, false));
return items.Pass();
}
ui::MenuModel* CreateContextMenu(aura::Window* root_window) override {
return NULL;
}
ash::ShelfMenuModel* CreateApplicationMenu(int event_flags) override {
return NULL;
}
bool IsDraggable() override { return false; }
bool ShouldShowTooltip() override { return false; }
private:
DISALLOW_COPY_AND_ASSIGN(TestV2AppLauncherItemController);
};
} // namespace
class ChromeLauncherControllerTest : public BrowserWithTestWindowTest {
protected:
ChromeLauncherControllerTest()
: BrowserWithTestWindowTest(
Browser::TYPE_TABBED,
chrome::HOST_DESKTOP_TYPE_ASH,
false),
test_controller_(NULL),
extension_service_(NULL) {
}
~ChromeLauncherControllerTest() override {}
void SetUp() override {
BrowserWithTestWindowTest::SetUp();
model_.reset(new ash::ShelfModel);
model_observer_.reset(new TestShelfModelObserver);
model_->AddObserver(model_observer_.get());
if (ash::Shell::HasInstance()) {
item_delegate_manager_ =
ash::Shell::GetInstance()->shelf_item_delegate_manager();
} else {
item_delegate_manager_ =
new ash::ShelfItemDelegateManager(model_.get());
}
base::DictionaryValue manifest;
manifest.SetString(extensions::manifest_keys::kName,
"launcher controller test extension");
manifest.SetString(extensions::manifest_keys::kVersion, "1");
manifest.SetString(extensions::manifest_keys::kDescription,
"for testing pinned apps");
extensions::TestExtensionSystem* extension_system(
static_cast<extensions::TestExtensionSystem*>(
extensions::ExtensionSystem::Get(profile())));
extension_service_ = extension_system->CreateExtensionService(
base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
std::string error;
extension1_ = Extension::Create(base::FilePath(), Manifest::UNPACKED,
manifest,
Extension::NO_FLAGS,
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
&error);
extension2_ = Extension::Create(base::FilePath(), Manifest::UNPACKED,
manifest,
Extension::NO_FLAGS,
"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
&error);
// Fake gmail extension.
base::DictionaryValue manifest_gmail;
manifest_gmail.SetString(extensions::manifest_keys::kName,
"Gmail launcher controller test extension");
manifest_gmail.SetString(extensions::manifest_keys::kVersion, "1");
manifest_gmail.SetString(extensions::manifest_keys::kDescription,
"for testing pinned Gmail");
manifest_gmail.SetString(extensions::manifest_keys::kLaunchWebURL,
kGmailLaunchURL);
base::ListValue* list = new base::ListValue();
list->Append(new base::StringValue("*://mail.google.com/mail/ca"));
manifest_gmail.Set(extensions::manifest_keys::kWebURLs, list);
extension3_ = Extension::Create(base::FilePath(), Manifest::UNPACKED,
manifest_gmail,
Extension::NO_FLAGS,
extension_misc::kGmailAppId,
&error);
// Fake search extension.
extension4_ = Extension::Create(base::FilePath(), Manifest::UNPACKED,
manifest,
Extension::NO_FLAGS,
extension_misc::kGoogleSearchAppId,
&error);
extension5_ = Extension::Create(base::FilePath(), Manifest::UNPACKED,
manifest,
Extension::NO_FLAGS,
"cccccccccccccccccccccccccccccccc",
&error);
extension6_ = Extension::Create(base::FilePath(), Manifest::UNPACKED,
manifest,
Extension::NO_FLAGS,
"dddddddddddddddddddddddddddddddd",
&error);
extension7_ = Extension::Create(base::FilePath(), Manifest::UNPACKED,
manifest,
Extension::NO_FLAGS,
"eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
&error);
extension8_ = Extension::Create(base::FilePath(), Manifest::UNPACKED,
manifest,
Extension::NO_FLAGS,
"ffffffffffffffffffffffffffffffff",
&error);
}
// Creates a running V2 app (not pinned) of type |app_id|.
virtual void CreateRunningV2App(const std::string& app_id) {
DCHECK(!test_controller_);
ash::ShelfID id =
launcher_controller_->CreateAppShortcutLauncherItemWithType(
app_id,
model_->item_count(),
ash::TYPE_PLATFORM_APP);
DCHECK(id);
// Change the created launcher controller into a V2 app controller.
test_controller_ = new TestV2AppLauncherItemController(app_id,
launcher_controller_.get());
launcher_controller_->SetItemController(id, test_controller_);
}
// Sets the stage for a multi user test.
virtual void SetUpMultiUserScenario(base::ListValue* user_a,
base::ListValue* user_b) {
InitLauncherController();
EXPECT_EQ("AppList, Chrome", GetPinnedAppStatus());
// Set an empty pinned pref to begin with.
base::ListValue no_user;
SetShelfChromeIconIndex(0);
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
no_user.DeepCopy());
EXPECT_EQ("AppList, Chrome", GetPinnedAppStatus());
// Assume all applications have been added already.
extension_service_->AddExtension(extension1_.get());
extension_service_->AddExtension(extension2_.get());
extension_service_->AddExtension(extension3_.get());
extension_service_->AddExtension(extension4_.get());
extension_service_->AddExtension(extension5_.get());
extension_service_->AddExtension(extension6_.get());
extension_service_->AddExtension(extension7_.get());
extension_service_->AddExtension(extension8_.get());
// There should be nothing in the list by now.
EXPECT_EQ("AppList, Chrome", GetPinnedAppStatus());
// Set user a preferences.
InsertPrefValue(user_a, 0, extension1_->id());
InsertPrefValue(user_a, 1, extension2_->id());
InsertPrefValue(user_a, 2, extension3_->id());
InsertPrefValue(user_a, 3, extension4_->id());
InsertPrefValue(user_a, 4, extension5_->id());
InsertPrefValue(user_a, 5, extension6_->id());
// Set user b preferences.
InsertPrefValue(user_b, 0, extension7_->id());
InsertPrefValue(user_b, 1, extension8_->id());
}
void TearDown() override {
launcher_controller_->SetShelfItemDelegateManagerForTest(nullptr);
if (!ash::Shell::HasInstance())
delete item_delegate_manager_;
model_->RemoveObserver(model_observer_.get());
model_observer_.reset();
launcher_controller_.reset();
model_.reset();
BrowserWithTestWindowTest::TearDown();
}
void AddAppListLauncherItem() {
ash::ShelfItem app_list;
app_list.type = ash::TYPE_APP_LIST;
model_->Add(app_list);
}
void InitLauncherController() {
AddAppListLauncherItem();
launcher_controller_.reset(
new ChromeLauncherController(profile(), model_.get()));
if (!ash::Shell::HasInstance())
SetShelfItemDelegateManager(item_delegate_manager_);
launcher_controller_->Init();
}
void InitLauncherControllerWithBrowser() {
InitLauncherController();
chrome::NewTab(browser());
BrowserList::SetLastActive(browser());
}
void SetAppIconLoader(extensions::AppIconLoader* loader) {
launcher_controller_->SetAppIconLoaderForTest(loader);
}
void SetAppTabHelper(ChromeLauncherController::AppTabHelper* helper) {
launcher_controller_->SetAppTabHelperForTest(helper);
}
void SetShelfItemDelegateManager(ash::ShelfItemDelegateManager* manager) {
launcher_controller_->SetShelfItemDelegateManagerForTest(manager);
}
void InsertPrefValue(base::ListValue* pref_value,
int index,
const std::string& extension_id) {
base::DictionaryValue* entry = new base::DictionaryValue();
entry->SetString(ash::kPinnedAppsPrefAppIDPath, extension_id);
pref_value->Insert(index, entry);
}
// Gets the currently configured app launchers from the controller.
void GetAppLaunchers(ChromeLauncherController* controller,
std::vector<std::string>* launchers) {
launchers->clear();
for (ash::ShelfItems::const_iterator iter(model_->items().begin());
iter != model_->items().end(); ++iter) {
ChromeLauncherController::IDToItemControllerMap::const_iterator
entry(controller->id_to_item_controller_map_.find(iter->id));
if (iter->type == ash::TYPE_APP_SHORTCUT &&
entry != controller->id_to_item_controller_map_.end()) {
launchers->push_back(entry->second->app_id());
}
}
}
// Get the setup of the currently shown launcher 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_PLATFORM_APP:
result+= "*";
// FALLTHROUGH
case ash::TYPE_WINDOWED_APP: {
const std::string& app =
launcher_controller_->GetAppIDForShelfID(model_->items()[i].id);
if (app == extension1_->id()) {
result += "app1";
EXPECT_FALSE(
launcher_controller_->IsAppPinned(extension1_->id()));
} else if (app == extension2_->id()) {
result += "app2";
EXPECT_FALSE(
launcher_controller_->IsAppPinned(extension2_->id()));
} else if (app == extension3_->id()) {
result += "app3";
EXPECT_FALSE(
launcher_controller_->IsAppPinned(extension3_->id()));
} else if (app == extension4_->id()) {
result += "app4";
EXPECT_FALSE(
launcher_controller_->IsAppPinned(extension4_->id()));
} else if (app == extension5_->id()) {
result += "app5";
EXPECT_FALSE(
launcher_controller_->IsAppPinned(extension5_->id()));
} else if (app == extension6_->id()) {
result += "app6";
EXPECT_FALSE(
launcher_controller_->IsAppPinned(extension6_->id()));
} else if (app == extension7_->id()) {
result += "app7";
EXPECT_FALSE(
launcher_controller_->IsAppPinned(extension7_->id()));
} else if (app == extension8_->id()) {
result += "app8";
EXPECT_FALSE(
launcher_controller_->IsAppPinned(extension8_->id()));
} else {
result += "unknown";
}
break;
}
case ash::TYPE_APP_SHORTCUT: {
const std::string& app =
launcher_controller_->GetAppIDForShelfID(model_->items()[i].id);
if (app == extension1_->id()) {
result += "App1";
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension1_->id()));
} else if (app == extension2_->id()) {
result += "App2";
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension2_->id()));
} else if (app == extension3_->id()) {
result += "App3";
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension3_->id()));
} else if (app == extension4_->id()) {
result += "App4";
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension4_->id()));
} else if (app == extension5_->id()) {
result += "App5";
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension5_->id()));
} else if (app == extension6_->id()) {
result += "App6";
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension6_->id()));
} else if (app == extension7_->id()) {
result += "App7";
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension7_->id()));
} else if (app == extension8_->id()) {
result += "App8";
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension8_->id()));
} else {
result += "unknown";
}
break;
}
case ash::TYPE_BROWSER_SHORTCUT:
result += "Chrome";
break;
case ash::TYPE_APP_LIST:
result += "AppList";
break;
default:
result += "Unknown";
break;
}
}
return result;
}
// Set the index at which the chrome icon should be.
void SetShelfChromeIconIndex(int index) {
profile()->GetTestingPrefService()->SetInteger(prefs::kShelfChromeIconIndex,
index);
}
// Remember the order of unpinned but running applications for the current
// user.
void RememberUnpinnedRunningApplicationOrder() {
launcher_controller_->RememberUnpinnedRunningApplicationOrder();
}
// Restore the order of running but unpinned applications for a given user.
void RestoreUnpinnedRunningApplicationOrder(const std::string& user_id) {
launcher_controller_->RestoreUnpinnedRunningApplicationOrder(user_id);
}
// Needed for extension service & friends to work.
scoped_refptr<Extension> extension1_;
scoped_refptr<Extension> extension2_;
scoped_refptr<Extension> extension3_;
scoped_refptr<Extension> extension4_;
scoped_refptr<Extension> extension5_;
scoped_refptr<Extension> extension6_;
scoped_refptr<Extension> extension7_;
scoped_refptr<Extension> extension8_;
scoped_ptr<ChromeLauncherController> launcher_controller_;
scoped_ptr<TestShelfModelObserver> model_observer_;
scoped_ptr<ash::ShelfModel> model_;
// |item_delegate_manager_| owns |test_controller_|.
LauncherItemController* test_controller_;
ExtensionService* extension_service_;
ash::ShelfItemDelegateManager* item_delegate_manager_;
private:
DISALLOW_COPY_AND_ASSIGN(ChromeLauncherControllerTest);
};
#if defined(OS_CHROMEOS)
// A browser window proxy which is able to associate an aura native window with
// it.
class TestBrowserWindowAura : public TestBrowserWindow {
public:
// |native_window| will still be owned by the caller after the constructor
// was called.
explicit TestBrowserWindowAura(aura::Window* native_window)
: native_window_(native_window) {
}
virtual ~TestBrowserWindowAura() {}
virtual gfx::NativeWindow GetNativeWindow() const override {
return native_window_.get();
}
Browser* browser() { return browser_.get(); }
void CreateBrowser(const Browser::CreateParams& params) {
Browser::CreateParams create_params = params;
create_params.window = this;
browser_.reset(new Browser(create_params));
}
private:
scoped_ptr<Browser> browser_;
scoped_ptr<aura::Window> native_window_;
DISALLOW_COPY_AND_ASSIGN(TestBrowserWindowAura);
};
// Creates a test browser window which has a native window.
scoped_ptr<TestBrowserWindowAura> CreateTestBrowserWindow(
const Browser::CreateParams& params) {
// Create a window.
aura::Window* window = new aura::Window(NULL);
window->set_id(0);
window->SetType(ui::wm::WINDOW_TYPE_NORMAL);
window->Init(aura::WINDOW_LAYER_TEXTURED);
window->Show();
scoped_ptr<TestBrowserWindowAura> browser_window(
new TestBrowserWindowAura(window));
browser_window->CreateBrowser(params);
return browser_window.Pass();
}
// Watches WebContents and blocks until it is destroyed. This is needed for
// the destruction of a V2 application.
class WebContentsDestroyedWatcher : public content::WebContentsObserver {
public:
explicit WebContentsDestroyedWatcher(content::WebContents* web_contents)
: content::WebContentsObserver(web_contents),
message_loop_runner_(new content::MessageLoopRunner) {
EXPECT_TRUE(web_contents != NULL);
}
virtual ~WebContentsDestroyedWatcher() {}
// Waits until the WebContents is destroyed.
void Wait() {
message_loop_runner_->Run();
}
private:
// Overridden WebContentsObserver methods.
virtual void WebContentsDestroyed() override {
message_loop_runner_->Quit();
}
scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
DISALLOW_COPY_AND_ASSIGN(WebContentsDestroyedWatcher);
};
// A V1 windowed application.
class V1App : public TestBrowserWindow {
public:
V1App(Profile* profile, const std::string& app_name) {
// Create a window.
native_window_.reset(new aura::Window(NULL));
native_window_->set_id(0);
native_window_->SetType(ui::wm::WINDOW_TYPE_POPUP);
native_window_->Init(aura::WINDOW_LAYER_TEXTURED);
native_window_->Show();
aura::client::ParentWindowWithContext(native_window_.get(),
ash::Shell::GetPrimaryRootWindow(),
gfx::Rect(10, 10, 20, 30));
Browser::CreateParams params =
Browser::CreateParams::CreateForApp(kCrxAppPrefix + app_name,
true /* trusted_source */,
gfx::Rect(),
profile,
chrome::HOST_DESKTOP_TYPE_ASH);
params.window = this;
browser_.reset(new Browser(params));
chrome::AddTabAt(browser_.get(), GURL(), 0, true);
}
virtual ~V1App() {
// close all tabs. Note that we do not need to destroy the browser itself.
browser_->tab_strip_model()->CloseAllTabs();
}
Browser* browser() { return browser_.get(); }
// TestBrowserWindow override:
virtual gfx::NativeWindow GetNativeWindow() const override {
return native_window_.get();
}
private:
// The associated browser with this app.
scoped_ptr<Browser> browser_;
// The native window we use.
scoped_ptr<aura::Window> native_window_;
DISALLOW_COPY_AND_ASSIGN(V1App);
};
// A V2 application which gets 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) {
window_ = new extensions::AppWindow(
profile,
new ChromeAppDelegate(make_scoped_ptr(new ScopedKeepAlive)),
extension);
extensions::AppWindow::CreateParams params =
extensions::AppWindow::CreateParams();
window_->Init(GURL(std::string()),
new extensions::AppWindowContentsImpl(window_), params);
}
virtual ~V2App() {
WebContentsDestroyedWatcher destroyed_watcher(window_->web_contents());
window_->GetBaseWindow()->Close();
destroyed_watcher.Wait();
}
extensions::AppWindow* window() { return window_; }
private:
// The app window which represents the application. Note that the window
// deletes itself asynchronously after window_->GetBaseWindow()->Close() gets
// called.
extensions::AppWindow* window_;
DISALLOW_COPY_AND_ASSIGN(V2App);
};
// The testing framework to test multi profile scenarios.
class MultiProfileMultiBrowserShelfLayoutChromeLauncherControllerTest
: public ChromeLauncherControllerTest {
protected:
MultiProfileMultiBrowserShelfLayoutChromeLauncherControllerTest() {
}
virtual ~MultiProfileMultiBrowserShelfLayoutChromeLauncherControllerTest() {
}
// Overwrite the Setup function to enable multi profile and needed objects.
virtual void SetUp() override {
profile_manager_.reset(
new TestingProfileManager(TestingBrowserProcess::GetGlobal()));
ASSERT_TRUE(profile_manager_->SetUp());
// AvatarMenu and multiple profiles works after user logged in.
profile_manager_->SetLoggedIn(true);
// Initialize the UserManager singleton to a fresh FakeUserManager instance.
user_manager_enabler_.reset(
new chromeos::ScopedUserManagerEnabler(new chromeos::FakeUserManager));
// Initialize the rest.
ChromeLauncherControllerTest::SetUp();
// Get some base objects.
session_delegate()->set_logged_in_users(2);
shell_delegate_ = static_cast<ash::test::TestShellDelegate*>(
ash::Shell::GetInstance()->delegate());
shell_delegate_->set_multi_profiles_enabled(true);
}
virtual void TearDown() {
ChromeLauncherControllerTest::TearDown();
user_manager_enabler_.reset();
for (ProfileToNameMap::iterator it = created_profiles_.begin();
it != created_profiles_.end(); ++it)
profile_manager_->DeleteTestingProfile(it->second);
// A Task is leaked if we don't destroy everything, then run the message
// loop.
base::MessageLoop::current()->PostTask(FROM_HERE,
base::MessageLoop::QuitClosure());
base::MessageLoop::current()->Run();
}
// Creates a profile for a given |user_name|. Note that this class will keep
// the ownership of the created object.
TestingProfile* CreateMultiUserProfile(const std::string& user_name) {
std::string email_string = user_name + "@example.com";
static_cast<ash::test::TestSessionStateDelegate*>(
ash::Shell::GetInstance()->session_state_delegate())
->AddUser(email_string);
// Add a user to the fake user manager.
session_delegate()->AddUser(email_string);
GetFakeUserManager()->AddUser(email_string);
GetFakeUserManager()->LoginUser(email_string);
TestingProfile* profile =
profile_manager()->CreateTestingProfile(email_string);
EXPECT_TRUE(profile);
// Remember the profile name so that we can destroy it upon destruction.
created_profiles_[profile] = email_string;
if (chrome::MultiUserWindowManager::GetInstance())
chrome::MultiUserWindowManager::GetInstance()->AddUser(profile);
if (launcher_controller_)
launcher_controller_->AdditionalUserAddedToSession(profile);
return profile;
}
// Switch to another user.
void SwitchActiveUser(const std::string& name) {
session_delegate()->SwitchActiveUser(name);
GetFakeUserManager()->SwitchActiveUser(name);
chrome::MultiUserWindowManagerChromeOS* manager =
static_cast<chrome::MultiUserWindowManagerChromeOS*>(
chrome::MultiUserWindowManager::GetInstance());
manager->SetAnimationSpeedForTest(
chrome::MultiUserWindowManagerChromeOS::ANIMATION_SPEED_DISABLED);
manager->ActiveUserChanged(name);
launcher_controller_->browser_status_monitor_for_test()->
ActiveUserChanged(name);
launcher_controller_->app_window_controller_for_test()->
ActiveUserChanged(name);
}
// Creates a browser with a |profile| and load a tab with a |title| and |url|.
Browser* CreateBrowserAndTabWithProfile(Profile* profile,
const std::string& title,
const std::string& url) {
Browser::CreateParams params(profile, chrome::HOST_DESKTOP_TYPE_ASH);
Browser* browser = chrome::CreateBrowserWithTestWindowForParams(¶ms);
chrome::NewTab(browser);
BrowserList::SetLastActive(browser);
NavigateAndCommitActiveTabWithTitle(
browser, GURL(url), ASCIIToUTF16(title));
return browser;
}
// Creates a running V1 application.
// Note that with the use of the app_tab_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);
// Create a new app tab helper and assign it to the launcher so that this
// app gets properly detected.
// TODO(skuhne): Create a more intelligent app tab helper which is able to
// detect all running apps properly.
TestAppTabHelperImpl* app_tab_helper = new TestAppTabHelperImpl;
app_tab_helper->SetAppID(
v1_app->browser()->tab_strip_model()->GetWebContentsAt(0),
app_name);
SetAppTabHelper(app_tab_helper);
NavigateAndCommitActiveTabWithTitle(
v1_app->browser(), GURL(url), ASCIIToUTF16(""));
return v1_app;
}
ash::test::TestSessionStateDelegate* session_delegate() {
return static_cast<ash::test::TestSessionStateDelegate*>(
ash::Shell::GetInstance()->session_state_delegate());
}
ash::test::TestShellDelegate* shell_delegate() { return shell_delegate_; }
// Override BrowserWithTestWindowTest:
virtual TestingProfile* CreateProfile() override {
return CreateMultiUserProfile("user1");
}
virtual void DestroyProfile(TestingProfile* profile) override {
// Delete the profile through our profile manager.
ProfileToNameMap::iterator it = created_profiles_.find(profile);
DCHECK(it != created_profiles_.end());
profile_manager_->DeleteTestingProfile(it->second);
created_profiles_.erase(it);
}
private:
typedef std::map<Profile*, std::string> ProfileToNameMap;
TestingProfileManager* profile_manager() { return profile_manager_.get(); }
chromeos::FakeUserManager* GetFakeUserManager() {
return static_cast<chromeos::FakeUserManager*>(
user_manager::UserManager::Get());
}
scoped_ptr<TestingProfileManager> profile_manager_;
scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_;
ash::test::TestShellDelegate* shell_delegate_;
ProfileToNameMap created_profiles_;
DISALLOW_COPY_AND_ASSIGN(
MultiProfileMultiBrowserShelfLayoutChromeLauncherControllerTest);
};
#endif // defined(OS_CHROMEOS)
TEST_F(ChromeLauncherControllerTest, DefaultApps) {
InitLauncherController();
// Model should only contain the browser shortcut and app list items.
EXPECT_EQ(2, model_->item_count());
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension3_->id()));
// Installing |extension3_| should add it to the launcher - behind the
// chrome icon.
extension_service_->AddExtension(extension3_.get());
EXPECT_EQ("AppList, Chrome, App3", GetPinnedAppStatus());
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension2_->id()));
}
// Check that the restauration of launcher items is happening in the same order
// as the user has pinned them (on another system) when they are synced reverse
// order.
TEST_F(ChromeLauncherControllerTest, RestoreDefaultAppsReverseOrder) {
InitLauncherController();
base::ListValue policy_value;
InsertPrefValue(&policy_value, 0, extension1_->id());
InsertPrefValue(&policy_value, 1, extension2_->id());
InsertPrefValue(&policy_value, 2, extension3_->id());
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
policy_value.DeepCopy());
SetShelfChromeIconIndex(0);
// Model should only contain the browser shortcut and app list items.
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension3_->id()));
EXPECT_EQ("AppList, Chrome", GetPinnedAppStatus());
// Installing |extension3_| should add it to the shelf - behind the
// chrome icon.
ash::ShelfItem item;
extension_service_->AddExtension(extension3_.get());
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension2_->id()));
EXPECT_EQ("AppList, Chrome, App3", GetPinnedAppStatus());
// Installing |extension2_| should add it to the launcher - behind the
// chrome icon, but in first location.
extension_service_->AddExtension(extension2_.get());
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_EQ("AppList, Chrome, App2, App3", GetPinnedAppStatus());
// Installing |extension1_| should add it to the launcher - behind the
// chrome icon, but in first location.
extension_service_->AddExtension(extension1_.get());
EXPECT_EQ("AppList, Chrome, App1, App2, App3", GetPinnedAppStatus());
}
// Check that the restauration of launcher items is happening in the same order
// as the user has pinned them (on another system) when they are synced random
// order.
TEST_F(ChromeLauncherControllerTest, RestoreDefaultAppsRandomOrder) {
InitLauncherController();
base::ListValue policy_value;
InsertPrefValue(&policy_value, 0, extension1_->id());
InsertPrefValue(&policy_value, 1, extension2_->id());
InsertPrefValue(&policy_value, 2, extension3_->id());
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
policy_value.DeepCopy());
SetShelfChromeIconIndex(0);
// Model should only contain the browser shortcut and app list items.
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension3_->id()));
EXPECT_EQ("AppList, Chrome", GetPinnedAppStatus());
// Installing |extension2_| should add it to the launcher - behind the
// chrome icon.
extension_service_->AddExtension(extension2_.get());
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension3_->id()));
EXPECT_EQ("AppList, Chrome, App2", GetPinnedAppStatus());
// Installing |extension1_| should add it to the launcher - behind the
// chrome icon, but in first location.
extension_service_->AddExtension(extension1_.get());
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension3_->id()));
EXPECT_EQ("AppList, Chrome, App1, App2", GetPinnedAppStatus());
// Installing |extension3_| should add it to the launcher - behind the
// chrome icon, but in first location.
extension_service_->AddExtension(extension3_.get());
EXPECT_EQ("AppList, Chrome, App1, App2, App3", GetPinnedAppStatus());
}
// Check that the restauration of launcher 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(ChromeLauncherControllerTest, RestoreDefaultAppsRandomOrderChromeMoved) {
InitLauncherController();
base::ListValue policy_value;
InsertPrefValue(&policy_value, 0, extension1_->id());
InsertPrefValue(&policy_value, 1, extension2_->id());
InsertPrefValue(&policy_value, 2, extension3_->id());
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
policy_value.DeepCopy());
SetShelfChromeIconIndex(1);
// Model should only contain the browser shortcut and app list items.
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension3_->id()));
EXPECT_EQ("AppList, Chrome", GetPinnedAppStatus());
// Installing |extension2_| should add it to the shelf - behind the
// chrome icon.
ash::ShelfItem item;
extension_service_->AddExtension(extension2_.get());
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension3_->id()));
EXPECT_EQ("AppList, Chrome, App2", GetPinnedAppStatus());
// Installing |extension1_| should add it to the launcher - behind the
// chrome icon, but in first location.
extension_service_->AddExtension(extension1_.get());
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension3_->id()));
EXPECT_EQ("AppList, App1, Chrome, App2", GetPinnedAppStatus());
// Installing |extension3_| should add it to the launcher - behind the
// chrome icon, but in first location.
extension_service_->AddExtension(extension3_.get());
EXPECT_EQ("AppList, App1, Chrome, App2, App3", GetPinnedAppStatus());
}
// Check that syncing to a different state does the correct thing.
TEST_F(ChromeLauncherControllerTest, RestoreDefaultAppsResyncOrder) {
InitLauncherController();
base::ListValue policy_value;
InsertPrefValue(&policy_value, 0, extension1_->id());
InsertPrefValue(&policy_value, 1, extension2_->id());
InsertPrefValue(&policy_value, 2, extension3_->id());
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
policy_value.DeepCopy());
// The shelf layout has always one static item at the beginning (App List).
SetShelfChromeIconIndex(0);
extension_service_->AddExtension(extension2_.get());
EXPECT_EQ("AppList, Chrome, App2", GetPinnedAppStatus());
extension_service_->AddExtension(extension1_.get());
EXPECT_EQ("AppList, Chrome, App1, App2", GetPinnedAppStatus());
extension_service_->AddExtension(extension3_.get());
EXPECT_EQ("AppList, Chrome, App1, App2, App3", GetPinnedAppStatus());
// Change the order with increasing chrome position and decreasing position.
base::ListValue policy_value1;
InsertPrefValue(&policy_value1, 0, extension3_->id());
InsertPrefValue(&policy_value1, 1, extension1_->id());
InsertPrefValue(&policy_value1, 2, extension2_->id());
SetShelfChromeIconIndex(3);
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
policy_value1.DeepCopy());
EXPECT_EQ("AppList, App3, App1, App2, Chrome", GetPinnedAppStatus());
base::ListValue policy_value2;
InsertPrefValue(&policy_value2, 0, extension2_->id());
InsertPrefValue(&policy_value2, 1, extension3_->id());
InsertPrefValue(&policy_value2, 2, extension1_->id());
SetShelfChromeIconIndex(2);
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
policy_value2.DeepCopy());
EXPECT_EQ("AppList, App2, App3, Chrome, App1", GetPinnedAppStatus());
// Check that the chrome icon can also be at the first possible location.
SetShelfChromeIconIndex(0);
base::ListValue policy_value3;
InsertPrefValue(&policy_value3, 0, extension3_->id());
InsertPrefValue(&policy_value3, 1, extension2_->id());
InsertPrefValue(&policy_value3, 2, extension1_->id());
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
policy_value3.DeepCopy());
EXPECT_EQ("AppList, Chrome, App3, App2, App1", GetPinnedAppStatus());
// Check that unloading of extensions works as expected.
extension_service_->UnloadExtension(extension1_->id(),
UnloadedExtensionInfo::REASON_UNINSTALL);
EXPECT_EQ("AppList, Chrome, App3, App2", GetPinnedAppStatus());
extension_service_->UnloadExtension(extension2_->id(),
UnloadedExtensionInfo::REASON_UNINSTALL);
EXPECT_EQ("AppList, Chrome, App3", GetPinnedAppStatus());
// Check that an update of an extension does not crash the system.
extension_service_->UnloadExtension(extension3_->id(),
UnloadedExtensionInfo::REASON_UPDATE);
EXPECT_EQ("AppList, Chrome, App3", GetPinnedAppStatus());
}
// Check that simple locking of an application will 'create' a launcher item.
TEST_F(ChromeLauncherControllerTest, CheckLockApps) {
InitLauncherController();
// Model should only contain the browser shortcut and app list items.
EXPECT_EQ(2, model_->item_count());
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension2_->id()));
launcher_controller_->LockV1AppWithID(extension1_->id());
EXPECT_EQ(3, model_->item_count());
EXPECT_EQ(ash::TYPE_WINDOWED_APP, model_->items()[2].type);
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_TRUE(launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension2_->id()));
launcher_controller_->UnlockV1AppWithID(extension1_->id());
EXPECT_EQ(2, model_->item_count());
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension2_->id()));
}
// Check that multiple locks of an application will be properly handled.
TEST_F(ChromeLauncherControllerTest, CheckMultiLockApps) {
InitLauncherController();
// Model should only contain the browser shortcut and app list items.
EXPECT_EQ(2, model_->item_count());
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
for (int i = 0; i < 2; i++) {
launcher_controller_->LockV1AppWithID(extension1_->id());
EXPECT_EQ(3, model_->item_count());
EXPECT_EQ(ash::TYPE_WINDOWED_APP, model_->items()[2].type);
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_TRUE(launcher_controller_->IsWindowedAppInLauncher(
extension1_->id()));
}
launcher_controller_->UnlockV1AppWithID(extension1_->id());
EXPECT_EQ(3, model_->item_count());
EXPECT_EQ(ash::TYPE_WINDOWED_APP, model_->items()[2].type);
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_TRUE(launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
launcher_controller_->UnlockV1AppWithID(extension1_->id());
EXPECT_EQ(2, model_->item_count());
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
}
// Check that already pinned items are not effected by locks.
TEST_F(ChromeLauncherControllerTest, CheckAlreadyPinnedLockApps) {
InitLauncherController();
// Model should only contain the browser shortcut and app list items.
EXPECT_EQ(2, model_->item_count());
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
launcher_controller_->PinAppWithID(extension1_->id());
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_EQ(3, model_->item_count());
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[2].type);
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
launcher_controller_->LockV1AppWithID(extension1_->id());
EXPECT_EQ(3, model_->item_count());
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[2].type);
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
launcher_controller_->UnlockV1AppWithID(extension1_->id());
EXPECT_EQ(3, model_->item_count());
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[2].type);
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
launcher_controller_->UnpinAppWithID(extension1_->id());
EXPECT_EQ(2, model_->item_count());
}
// Check that already pinned items which get locked stay after unpinning.
TEST_F(ChromeLauncherControllerTest, CheckPinnedAppsStayAfterUnlock) {
InitLauncherController();
// Model should only contain the browser shortcut and app list items.
EXPECT_EQ(2, model_->item_count());
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
launcher_controller_->PinAppWithID(extension1_->id());
EXPECT_EQ(3, model_->item_count());
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[2].type);
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
launcher_controller_->LockV1AppWithID(extension1_->id());
EXPECT_EQ(3, model_->item_count());
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[2].type);
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
launcher_controller_->UnpinAppWithID(extension1_->id());
EXPECT_EQ(3, model_->item_count());
EXPECT_EQ(ash::TYPE_WINDOWED_APP, model_->items()[2].type);
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_TRUE(launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
launcher_controller_->UnlockV1AppWithID(extension1_->id());
EXPECT_EQ(2, model_->item_count());
}
#if defined(OS_CHROMEOS)
// Check that running applications wich are not pinned get properly restored
// upon user change.
TEST_F(ChromeLauncherControllerTest, CheckRunningAppOrder) {
InitLauncherController();
// Model should only contain the browser shortcut and app list items.
EXPECT_EQ(2, model_->item_count());
// Add a few running applications.
launcher_controller_->LockV1AppWithID(extension1_->id());
launcher_controller_->LockV1AppWithID(extension2_->id());
launcher_controller_->LockV1AppWithID(extension3_->id());
EXPECT_EQ(5, model_->item_count());
// Note that this not only checks the order of applications but also the
// running type.
EXPECT_EQ("AppList, Chrome, app1, app2, app3", GetPinnedAppStatus());
// Remember the current order of applications for the current user.
const std::string& current_user_id =
multi_user_util::GetUserIDFromProfile(profile());
RememberUnpinnedRunningApplicationOrder();
// Switch some items and check that restoring a user which was not yet
// remembered changes nothing.
model_->Move(2, 3);
EXPECT_EQ("AppList, Chrome, app2, app1, app3", GetPinnedAppStatus());
RestoreUnpinnedRunningApplicationOrder("second-fake-user@fake.com");
EXPECT_EQ("AppList, Chrome, app2, app1, app3", GetPinnedAppStatus());
// Restoring the stored user should however do the right thing.
RestoreUnpinnedRunningApplicationOrder(current_user_id);
EXPECT_EQ("AppList, Chrome, app1, app2, app3", GetPinnedAppStatus());
// Switch again some items and even delete one - making sure that the missing
// item gets properly handled.
model_->Move(3, 4);
launcher_controller_->UnlockV1AppWithID(extension1_->id());
EXPECT_EQ("AppList, Chrome, app3, app2", GetPinnedAppStatus());
RestoreUnpinnedRunningApplicationOrder(current_user_id);
EXPECT_EQ("AppList, Chrome, app2, app3", GetPinnedAppStatus());
// Check that removing more items does not crash and changes nothing.
launcher_controller_->UnlockV1AppWithID(extension2_->id());
RestoreUnpinnedRunningApplicationOrder(current_user_id);
EXPECT_EQ("AppList, Chrome, app3", GetPinnedAppStatus());
launcher_controller_->UnlockV1AppWithID(extension3_->id());
RestoreUnpinnedRunningApplicationOrder(current_user_id);
EXPECT_EQ("AppList, Chrome", GetPinnedAppStatus());
}
// Check that with multi profile V1 apps are properly added / removed from the
// shelf.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeLauncherControllerTest,
V1AppUpdateOnUserSwitch) {
// Create a browser item in the LauncherController.
InitLauncherController();
EXPECT_EQ(2, model_->item_count());
{
// Create a "windowed gmail app".
scoped_ptr<V1App> v1_app(CreateRunningV1App(
profile(), extension_misc::kGmailAppId, gmail_url));
EXPECT_EQ(3, model_->item_count());
// After switching to a second user the item should be gone.
std::string user2 = "user2";
TestingProfile* profile2 = CreateMultiUserProfile(user2);
SwitchActiveUser(profile2->GetProfileName());
EXPECT_EQ(2, model_->item_count());
// After switching back the item should be back.
SwitchActiveUser(profile()->GetProfileName());
EXPECT_EQ(3, model_->item_count());
// Note we destroy now the gmail app with the closure end.
}
EXPECT_EQ(2, model_->item_count());
}
// Check edge cases with multi profile V1 apps in the shelf.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeLauncherControllerTest,
V1AppUpdateOnUserSwitchEdgecases) {
// Create a browser item in the LauncherController.
InitLauncherController();
// First test: Create an app when the user is not active.
std::string user2 = "user2";
TestingProfile* profile2 = CreateMultiUserProfile(user2);
{
// Create a "windowed gmail app".
scoped_ptr<V1App> v1_app(CreateRunningV1App(
profile2, extension_misc::kGmailAppId, gmail_url));
EXPECT_EQ(2, model_->item_count());
// However - switching to the user should show it.
SwitchActiveUser(profile2->GetProfileName());
EXPECT_EQ(3, model_->item_count());
// Second test: Remove the app when the user is not active and see that it
// works.
SwitchActiveUser(profile()->GetProfileName());
EXPECT_EQ(2, model_->item_count());
// Note: the closure ends and the browser will go away.
}
EXPECT_EQ(2, model_->item_count());
SwitchActiveUser(profile2->GetProfileName());
EXPECT_EQ(2, model_->item_count());
SwitchActiveUser(profile()->GetProfileName());
EXPECT_EQ(2, model_->item_count());
}
// Check edge case where a visiting V1 app gets closed (crbug.com/321374).
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeLauncherControllerTest,
V1CloseOnVisitingDesktop) {
// Create a browser item in the LauncherController.
InitLauncherController();
chrome::MultiUserWindowManager* manager =
chrome::MultiUserWindowManager::GetInstance();
// First create an app when the user is active.
std::string user2 = "user2";
TestingProfile* profile2 = CreateMultiUserProfile(user2);
{
// Create a "windowed gmail app".
scoped_ptr<V1App> v1_app(CreateRunningV1App(
profile(),
extension_misc::kGmailAppId,
kGmailLaunchURL));
EXPECT_EQ(3, model_->item_count());
// Transfer the app to the other screen and switch users.
manager->ShowWindowForUser(v1_app->browser()->window()->GetNativeWindow(),
user2);
EXPECT_EQ(3, model_->item_count());
SwitchActiveUser(profile2->GetProfileName());
EXPECT_EQ(2, model_->item_count());
}
// After the app was destroyed, switch back. (which caused already a crash).
SwitchActiveUser(profile()->GetProfileName());
// Create the same app again - which was also causing the crash.
EXPECT_EQ(2, model_->item_count());
{
// Create a "windowed gmail app".
scoped_ptr<V1App> v1_app(CreateRunningV1App(
profile(),
extension_misc::kGmailAppId,
kGmailLaunchURL));
EXPECT_EQ(3, model_->item_count());
}
SwitchActiveUser(profile2->GetProfileName());
EXPECT_EQ(2, model_->item_count());
}
// Check edge cases with multi profile V1 apps in the shelf.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeLauncherControllerTest,
V1AppUpdateOnUserSwitchEdgecases2) {
// Create a browser item in the LauncherController.
InitLauncherController();
TestAppTabHelperImpl* app_tab_helper = new TestAppTabHelperImpl;
SetAppTabHelper(app_tab_helper);
// First test: Create an app when the user is not active.
std::string user2 = "user2";
TestingProfile* profile2 = CreateMultiUserProfile(user2);
SwitchActiveUser(profile2->GetProfileName());
{
// Create a "windowed gmail app".
scoped_ptr<V1App> v1_app(CreateRunningV1App(
profile(), extension_misc::kGmailAppId, gmail_url));
EXPECT_EQ(2, model_->item_count());
// However - switching to the user should show it.
SwitchActiveUser(profile()->GetProfileName());
EXPECT_EQ(3, model_->item_count());
// Second test: Remove the app when the user is not active and see that it
// works.
SwitchActiveUser(profile2->GetProfileName());
EXPECT_EQ(2, model_->item_count());
v1_app.reset();
}
EXPECT_EQ(2, model_->item_count());
SwitchActiveUser(profile()->GetProfileName());
EXPECT_EQ(2, model_->item_count());
SwitchActiveUser(profile2->GetProfileName());
EXPECT_EQ(2, model_->item_count());
}
// Check that activating an item which is on another user's desktop, will bring
// it back.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeLauncherControllerTest,
TestLauncherActivationPullsBackWindow) {
// Create a browser item in the LauncherController.
InitLauncherController();
chrome::MultiUserWindowManager* manager =
chrome::MultiUserWindowManager::GetInstance();
// Add two users to the window manager.
std::string user2 = "user2";
TestingProfile* profile2 = CreateMultiUserProfile(user2);
manager->AddUser(profile());
manager->AddUser(profile2);
const std::string& current_user =
multi_user_util::GetUserIDFromProfile(profile());
// Create a browser window with a native window for the current user.
scoped_ptr<BrowserWindow> browser_window(CreateTestBrowserWindow(
Browser::CreateParams(profile(), chrome::HOST_DESKTOP_TYPE_ASH)));
aura::Window* window = browser_window->GetNativeWindow();
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.
launcher_controller_->ActivateWindowOrMinimizeIfActive(browser_window.get(),
false);
EXPECT_TRUE(manager->IsWindowOnDesktopOfUser(window, current_user));
// Transfer the window to another user's desktop and check that activating it
// does pull it back to that user.
manager->ShowWindowForUser(window,
multi_user_util::GetUserIDFromProfile(profile2));
EXPECT_FALSE(manager->IsWindowOnDesktopOfUser(window, current_user));
launcher_controller_->ActivateWindowOrMinimizeIfActive(browser_window.get(),
false);
EXPECT_TRUE(manager->IsWindowOnDesktopOfUser(window, current_user));
}
#endif
// Check that lock -> pin -> unlock -> unpin does properly transition.
TEST_F(ChromeLauncherControllerTest, CheckLockPinUnlockUnpin) {
InitLauncherController();
// Model should only contain the browser shortcut and app list items.
EXPECT_EQ(2, model_->item_count());
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
launcher_controller_->LockV1AppWithID(extension1_->id());
EXPECT_EQ(3, model_->item_count());
EXPECT_EQ(ash::TYPE_WINDOWED_APP, model_->items()[2].type);
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_TRUE(launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
launcher_controller_->PinAppWithID(extension1_->id());
EXPECT_EQ(3, model_->item_count());
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[2].type);
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
launcher_controller_->UnlockV1AppWithID(extension1_->id());
EXPECT_EQ(3, model_->item_count());
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[2].type);
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(
launcher_controller_->IsWindowedAppInLauncher(extension1_->id()));
launcher_controller_->UnpinAppWithID(extension1_->id());
EXPECT_EQ(2, model_->item_count());
}
// Check that a locked (windowed V1 application) will be properly converted
// between locked and pinned when the order gets changed through a profile /
// policy change.
TEST_F(ChromeLauncherControllerTest, RestoreDefaultAndLockedAppsResyncOrder) {
InitLauncherController();
base::ListValue policy_value0;
InsertPrefValue(&policy_value0, 0, extension1_->id());
InsertPrefValue(&policy_value0, 1, extension3_->id());
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
policy_value0.DeepCopy());
// The shelf layout has always one static item at the beginning (App List).
SetShelfChromeIconIndex(0);
extension_service_->AddExtension(extension1_.get());
EXPECT_EQ("AppList, Chrome, App1", GetPinnedAppStatus());
extension_service_->AddExtension(extension2_.get());
// No new app icon will be generated.
EXPECT_EQ("AppList, Chrome, App1", GetPinnedAppStatus());
// Add the app as locked app which will add it (un-pinned).
launcher_controller_->LockV1AppWithID(extension2_->id());
EXPECT_EQ("AppList, Chrome, App1, app2", GetPinnedAppStatus());
extension_service_->AddExtension(extension3_.get());
EXPECT_EQ("AppList, Chrome, App1, App3, app2", GetPinnedAppStatus());
// Now request to pin all items which should convert the locked item into a
// pinned item.
base::ListValue policy_value1;
InsertPrefValue(&policy_value1, 0, extension3_->id());
InsertPrefValue(&policy_value1, 1, extension2_->id());
InsertPrefValue(&policy_value1, 2, extension1_->id());
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
policy_value1.DeepCopy());
EXPECT_EQ("AppList, Chrome, App3, App2, App1", GetPinnedAppStatus());
// Going back to a status where there is no requirement for app 2 to be pinned
// should convert it back to locked but not pinned and state. The position
// is determined by the |ShelfModel|'s weight system and since running
// applications are not allowed to be mixed with shortcuts, it should show up
// at the end of the list.
base::ListValue policy_value2;
InsertPrefValue(&policy_value2, 0, extension3_->id());
InsertPrefValue(&policy_value2, 1, extension1_->id());
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
policy_value2.DeepCopy());
EXPECT_EQ("AppList, Chrome, App3, App1, app2", GetPinnedAppStatus());
// Removing an item should simply close it and everything should shift.
base::ListValue policy_value3;
InsertPrefValue(&policy_value3, 0, extension3_->id());
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
policy_value3.DeepCopy());
EXPECT_EQ("AppList, Chrome, App3, app2", GetPinnedAppStatus());
}
// Check that a running and not pinned V2 application will be properly converted
// between locked and pinned when the order gets changed through a profile /
// policy change.
TEST_F(ChromeLauncherControllerTest,
RestoreDefaultAndRunningV2AppsResyncOrder) {
InitLauncherController();
base::ListValue policy_value0;
InsertPrefValue(&policy_value0, 0, extension1_->id());
InsertPrefValue(&policy_value0, 1, extension3_->id());
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
policy_value0.DeepCopy());
// The shelf layout has always one static item at the beginning (app List).
SetShelfChromeIconIndex(0);
extension_service_->AddExtension(extension1_.get());
EXPECT_EQ("AppList, Chrome, App1", GetPinnedAppStatus());
extension_service_->AddExtension(extension2_.get());
// No new app icon will be generated.
EXPECT_EQ("AppList, Chrome, App1", GetPinnedAppStatus());
// Add the app as an unpinned but running V2 app.
CreateRunningV2App(extension2_->id());
EXPECT_EQ("AppList, Chrome, App1, *app2", GetPinnedAppStatus());
extension_service_->AddExtension(extension3_.get());
EXPECT_EQ("AppList, Chrome, App1, App3, *app2", GetPinnedAppStatus());
// Now request to pin all items which should convert the locked item into a
// pinned item.
base::ListValue policy_value1;
InsertPrefValue(&policy_value1, 0, extension3_->id());
InsertPrefValue(&policy_value1, 1, extension2_->id());
InsertPrefValue(&policy_value1, 2, extension1_->id());
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
policy_value1.DeepCopy());
EXPECT_EQ("AppList, Chrome, App3, App2, App1", GetPinnedAppStatus());
// Going back to a status where there is no requirement for app 2 to be pinned
// should convert it back to running V2 app. Since the position is determined
// by the |ShelfModel|'s weight system, it will be after last pinned item.
base::ListValue policy_value2;
InsertPrefValue(&policy_value2, 0, extension3_->id());
InsertPrefValue(&policy_value2, 1, extension1_->id());
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
policy_value2.DeepCopy());
EXPECT_EQ("AppList, Chrome, App3, App1, *app2", GetPinnedAppStatus());
// Removing an item should simply close it and everything should shift.
base::ListValue policy_value3;
InsertPrefValue(&policy_value3, 0, extension3_->id());
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
policy_value3.DeepCopy());
EXPECT_EQ("AppList, Chrome, App3, *app2", 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(ChromeLauncherControllerTest, UserSwitchIconRestore) {
base::ListValue user_a;
base::ListValue user_b;
SetUpMultiUserScenario(&user_a, &user_b);
// Show user 1.
SetShelfChromeIconIndex(6);
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
user_a.DeepCopy());
EXPECT_EQ("AppList, App1, App2, App3, App4, App5, App6, Chrome",
GetPinnedAppStatus());
// Show user 2.
SetShelfChromeIconIndex(4);
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
user_b.DeepCopy());
EXPECT_EQ("AppList, App7, App8, Chrome", GetPinnedAppStatus());
// Switch back to 1.
SetShelfChromeIconIndex(8);
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
user_a.DeepCopy());
EXPECT_EQ("AppList, App1, App2, App3, App4, App5, App6, Chrome",
GetPinnedAppStatus());
// Switch back to 2.
SetShelfChromeIconIndex(4);
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
user_b.DeepCopy());
EXPECT_EQ("AppList, 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(ChromeLauncherControllerTest, UserSwitchIconRestoreWithRunningV2App) {
base::ListValue user_a;
base::ListValue user_b;
SetUpMultiUserScenario(&user_a, &user_b);
// Run App1 and assume that it is a V2 app.
CreateRunningV2App(extension1_->id());
// Show user 1.
SetShelfChromeIconIndex(6);
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
user_a.DeepCopy());
EXPECT_EQ("AppList, App1, App2, App3, App4, App5, App6, Chrome",
GetPinnedAppStatus());
// Show user 2.
SetShelfChromeIconIndex(4);
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
user_b.DeepCopy());
EXPECT_EQ("AppList, App7, App8, Chrome, *app1", GetPinnedAppStatus());
// Switch back to 1.
SetShelfChromeIconIndex(8);
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
user_a.DeepCopy());
EXPECT_EQ("AppList, App1, App2, App3, App4, App5, App6, Chrome",
GetPinnedAppStatus());
// Switch back to 2.
SetShelfChromeIconIndex(4);
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
user_b.DeepCopy());
EXPECT_EQ("AppList, App7, App8, Chrome, *app1", 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(ChromeLauncherControllerTest,
UserSwitchIconRestoreWithRunningV2AppChromeInMiddle) {
base::ListValue user_a;
base::ListValue user_b;
SetUpMultiUserScenario(&user_a, &user_b);
// Run App1 and assume that it is a V2 app.
CreateRunningV2App(extension1_->id());
// Show user 1.
SetShelfChromeIconIndex(5);
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
user_a.DeepCopy());
EXPECT_EQ("AppList, App1, App2, App3, App4, App5, Chrome, App6",
GetPinnedAppStatus());
// Show user 2.
SetShelfChromeIconIndex(4);
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
user_b.DeepCopy());
EXPECT_EQ("AppList, App7, App8, Chrome, *app1", GetPinnedAppStatus());
// Switch back to 1.
SetShelfChromeIconIndex(5);
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
user_a.DeepCopy());
EXPECT_EQ("AppList, App1, App2, App3, App4, App5, Chrome, App6",
GetPinnedAppStatus());
}
TEST_F(ChromeLauncherControllerTest, Policy) {
extension_service_->AddExtension(extension1_.get());
extension_service_->AddExtension(extension3_.get());
base::ListValue policy_value;
InsertPrefValue(&policy_value, 0, extension1_->id());
InsertPrefValue(&policy_value, 1, extension2_->id());
profile()->GetTestingPrefService()->SetManagedPref(prefs::kPinnedLauncherApps,
policy_value.DeepCopy());
// Only |extension1_| should get pinned. |extension2_| is specified but not
// installed, and |extension3_| is part of the default set, but that shouldn't
// take effect when the policy override is in place.
InitLauncherController();
EXPECT_EQ(3, model_->item_count());
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[2].type);
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension3_->id()));
// Installing |extension2_| should add it to the launcher.
extension_service_->AddExtension(extension2_.get());
EXPECT_EQ(4, model_->item_count());
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[2].type);
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[3].type);
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension3_->id()));
// Removing |extension1_| from the policy should be reflected in the launcher.
policy_value.Remove(0, NULL);
profile()->GetTestingPrefService()->SetManagedPref(prefs::kPinnedLauncherApps,
policy_value.DeepCopy());
EXPECT_EQ(3, model_->item_count());
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[2].type);
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension1_->id()));
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension2_->id()));
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension3_->id()));
}
TEST_F(ChromeLauncherControllerTest, UnpinWithUninstall) {
extension_service_->AddExtension(extension3_.get());
extension_service_->AddExtension(extension4_.get());
InitLauncherController();
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension3_->id()));
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension4_->id()));
extension_service_->UnloadExtension(extension3_->id(),
UnloadedExtensionInfo::REASON_UNINSTALL);
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension3_->id()));
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension4_->id()));
}
TEST_F(ChromeLauncherControllerTest, PrefUpdates) {
extension_service_->AddExtension(extension2_.get());
extension_service_->AddExtension(extension3_.get());
extension_service_->AddExtension(extension4_.get());
InitLauncherController();
std::vector<std::string> expected_launchers;
std::vector<std::string> actual_launchers;
base::ListValue pref_value;
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
pref_value.DeepCopy());
GetAppLaunchers(launcher_controller_.get(), &actual_launchers);
EXPECT_EQ(expected_launchers, actual_launchers);
// Unavailable extensions don't create launcher items.
InsertPrefValue(&pref_value, 0, extension1_->id());
InsertPrefValue(&pref_value, 1, extension2_->id());
InsertPrefValue(&pref_value, 2, extension4_->id());
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
pref_value.DeepCopy());
expected_launchers.push_back(extension2_->id());
expected_launchers.push_back(extension4_->id());
GetAppLaunchers(launcher_controller_.get(), &actual_launchers);
EXPECT_EQ(expected_launchers, actual_launchers);
// Redundant pref entries show up only once.
InsertPrefValue(&pref_value, 2, extension3_->id());
InsertPrefValue(&pref_value, 2, extension3_->id());
InsertPrefValue(&pref_value, 5, extension3_->id());
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
pref_value.DeepCopy());
expected_launchers.insert(expected_launchers.begin() + 1, extension3_->id());
GetAppLaunchers(launcher_controller_.get(), &actual_launchers);
EXPECT_EQ(expected_launchers, actual_launchers);
// Order changes are reflected correctly.
pref_value.Clear();
InsertPrefValue(&pref_value, 0, extension4_->id());
InsertPrefValue(&pref_value, 1, extension3_->id());
InsertPrefValue(&pref_value, 2, extension2_->id());
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
pref_value.DeepCopy());
std::reverse(expected_launchers.begin(), expected_launchers.end());
GetAppLaunchers(launcher_controller_.get(), &actual_launchers);
EXPECT_EQ(expected_launchers, actual_launchers);
// Clearing works.
pref_value.Clear();
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
pref_value.DeepCopy());
expected_launchers.clear();
GetAppLaunchers(launcher_controller_.get(), &actual_launchers);
EXPECT_EQ(expected_launchers, actual_launchers);
}
TEST_F(ChromeLauncherControllerTest, PendingInsertionOrder) {
extension_service_->AddExtension(extension1_.get());
extension_service_->AddExtension(extension3_.get());
InitLauncherController();
base::ListValue pref_value;
InsertPrefValue(&pref_value, 0, extension1_->id());
InsertPrefValue(&pref_value, 1, extension2_->id());
InsertPrefValue(&pref_value, 2, extension3_->id());
profile()->GetTestingPrefService()->SetUserPref(prefs::kPinnedLauncherApps,
pref_value.DeepCopy());
std::vector<std::string> expected_launchers;
expected_launchers.push_back(extension1_->id());
expected_launchers.push_back(extension3_->id());
std::vector<std::string> actual_launchers;
GetAppLaunchers(launcher_controller_.get(), &actual_launchers);
EXPECT_EQ(expected_launchers, actual_launchers);
// Install |extension2| and verify it shows up between the other two.
extension_service_->AddExtension(extension2_.get());
expected_launchers.insert(expected_launchers.begin() + 1, extension2_->id());
GetAppLaunchers(launcher_controller_.get(), &actual_launchers);
EXPECT_EQ(expected_launchers, actual_launchers);
}
// Checks the created menus and menu lists for correctness. It uses the given
// |controller| to create the objects for the given |item| and checks the
// found item count against the |expected_items|. The |title| list contains the
// menu titles in the order of their appearance in the menu (not including the
// application name).
bool CheckMenuCreation(ChromeLauncherController* controller,
const ash::ShelfItem& item,
size_t expected_items,
base::string16 title[],
bool is_browser) {
ChromeLauncherAppMenuItems items = controller->GetApplicationList(item, 0);
// A new behavior has been added: Only show menus if there is at least one
// item available.
if (expected_items < 1 && is_browser) {
EXPECT_EQ(0u, items.size());
return items.size() == 0;
}
// There should be one item in there: The title.
EXPECT_EQ(expected_items + 1, items.size());
EXPECT_FALSE(items[0]->IsEnabled());
for (size_t i = 0; i < expected_items; i++) {
EXPECT_EQ(title[i], items[1 + i]->title());
// Check that the first real item has a leading separator.
if (i == 1)
EXPECT_TRUE(items[i]->HasLeadingSeparator());
else
EXPECT_FALSE(items[i]->HasLeadingSeparator());
}
scoped_ptr<ash::ShelfMenuModel> menu(new LauncherApplicationMenuItemModel(
controller->GetApplicationList(item, 0)));
// The first element in the menu is a spacing separator. On some systems
// (e.g. Windows) such things do not exist. As such we check the existence
// and adjust dynamically.
int first_item = menu->GetTypeAt(0) == ui::MenuModel::TYPE_SEPARATOR ? 1 : 0;
int expected_menu_items = first_item +
(expected_items ? (expected_items + 3) : 2);
EXPECT_EQ(expected_menu_items, menu->GetItemCount());
EXPECT_FALSE(menu->IsEnabledAt(first_item));
if (expected_items) {
EXPECT_EQ(ui::MenuModel::TYPE_SEPARATOR,
menu->GetTypeAt(first_item + 1));
}
return items.size() == expected_items + 1;
}
// Check that browsers get reflected correctly in the launcher menu.
TEST_F(ChromeLauncherControllerTest, BrowserMenuGeneration) {
EXPECT_EQ(1U, chrome::GetTotalBrowserCount());
chrome::NewTab(browser());
InitLauncherController();
// Check that the browser list is empty at this time.
ash::ShelfItem item_browser;
item_browser.type = ash::TYPE_BROWSER_SHORTCUT;
item_browser.id =
launcher_controller_->GetShelfIDForAppID(extension_misc::kChromeAppId);
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_browser, 0, NULL, true));
// Now make the created browser() visible by adding it to the active browser
// list.
BrowserList::SetLastActive(browser());
base::string16 title1 = ASCIIToUTF16("Test1");
NavigateAndCommitActiveTabWithTitle(browser(), GURL("http://test1"), title1);
base::string16 one_menu_item[] = { title1 };
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_browser, 1, one_menu_item, true));
// Create one more browser/window and check that one more was added.
Browser::CreateParams ash_params(profile(), chrome::HOST_DESKTOP_TYPE_ASH);
scoped_ptr<Browser> browser2(
chrome::CreateBrowserWithTestWindowForParams(&ash_params));
chrome::NewTab(browser2.get());
BrowserList::SetLastActive(browser2.get());
base::string16 title2 = ASCIIToUTF16("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.
base::string16 two_menu_items[] = {title1, title2};
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_browser, 2, two_menu_items, true));
// Apparently we have to close all tabs we have.
chrome::CloseTab(browser2.get());
}
#if defined(OS_CHROMEOS)
// Check the multi profile case where only user related browsers should show
// up.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeLauncherControllerTest,
BrowserMenuGenerationTwoUsers) {
// Create a browser item in the LauncherController.
InitLauncherController();
ash::ShelfItem item_browser;
item_browser.type = ash::TYPE_BROWSER_SHORTCUT;
item_browser.id =
launcher_controller_->GetShelfIDForAppID(extension_misc::kChromeAppId);
// Check that the menu is empty.
chrome::NewTab(browser());
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_browser, 0, NULL, true));
// Show the created |browser()| by adding it to the active browser list.
BrowserList::SetLastActive(browser());
base::string16 title1 = ASCIIToUTF16("Test1");
NavigateAndCommitActiveTabWithTitle(browser(), GURL("http://test1"), title1);
base::string16 one_menu_item1[] = { title1 };
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_browser, 1, one_menu_item1, true));
// Create a browser for another user and check that it is not included in the
// users running browser list.
std::string user2 = "user2";
TestingProfile* profile2 = CreateMultiUserProfile(user2);
scoped_ptr<Browser> browser2(
CreateBrowserAndTabWithProfile(profile2, user2, "http://test2"));
base::string16 one_menu_item2[] = { ASCIIToUTF16(user2) };
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_browser, 1, one_menu_item1, true));
// Switch to the other user and make sure that only that browser window gets
// shown.
SwitchActiveUser(profile2->GetProfileName());
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_browser, 1, one_menu_item2, true));
// Transferred browsers of other users should not show up in the list.
chrome::MultiUserWindowManager::GetInstance()->ShowWindowForUser(
browser()->window()->GetNativeWindow(),
user2);
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_browser, 1, one_menu_item2, true));
chrome::CloseTab(browser2.get());
}
#endif // defined(OS_CHROMEOS)
// Check that V1 apps are correctly reflected in the launcher 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(ChromeLauncherControllerTest, V1AppMenuGeneration) {
EXPECT_EQ(1U, chrome::GetTotalBrowserCount());
EXPECT_EQ(0, browser()->tab_strip_model()->count());
InitLauncherControllerWithBrowser();
// Model should only contain the browser shortcut and app list items.
EXPECT_EQ(2, model_->item_count());
EXPECT_FALSE(launcher_controller_->IsAppPinned(extension3_->id()));
// Installing |extension3_| adds it to the launcher.
ash::ShelfID gmail_id = model_->next_id();
extension_service_->AddExtension(extension3_.get());
EXPECT_EQ(3, model_->item_count());
int gmail_index = model_->ItemIndexByID(gmail_id);
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[gmail_index].type);
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension3_->id()));
launcher_controller_->SetRefocusURLPatternForTest(gmail_id, GURL(gmail_url));
// Check the menu content.
ash::ShelfItem item_browser;
item_browser.type = ash::TYPE_BROWSER_SHORTCUT;
item_browser.id =
launcher_controller_->GetShelfIDForAppID(extension_misc::kChromeAppId);
ash::ShelfItem item_gmail;
item_gmail.type = ash::TYPE_APP_SHORTCUT;
item_gmail.id = gmail_id;
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_gmail, 0, NULL, false));
// Set the gmail URL to a new tab.
base::string16 title1 = ASCIIToUTF16("Test1");
NavigateAndCommitActiveTabWithTitle(browser(), GURL(gmail_url), title1);
base::string16 one_menu_item[] = { title1 };
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_gmail, 1, one_menu_item, false));
// Create one empty tab.
chrome::NewTab(browser());
base::string16 title2 = ASCIIToUTF16("Test2");
NavigateAndCommitActiveTabWithTitle(
browser(),
GURL("https://bla"),
title2);
// and another one with another gmail instance.
chrome::NewTab(browser());
base::string16 title3 = ASCIIToUTF16("Test3");
NavigateAndCommitActiveTabWithTitle(browser(), GURL(gmail_url), title3);
base::string16 two_menu_items[] = {title1, title3};
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_gmail, 2, two_menu_items, false));
// Even though the item is in the V1 app list, it should also be in the
// browser list.
base::string16 browser_menu_item[] = {title3};
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_browser, 1, browser_menu_item, false));
// Test that closing of (all) the item(s) does work (and all menus get
// updated properly).
launcher_controller_->Close(item_gmail.id);
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_gmail, 0, NULL, false));
base::string16 browser_menu_item2[] = { title2 };
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_browser, 1, browser_menu_item2, false));
}
#if defined(OS_CHROMEOS)
// Check the multi profile case where only user related apps should show up.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeLauncherControllerTest,
V1AppMenuGenerationTwoUsers) {
// Create a browser item in the LauncherController.
InitLauncherController();
chrome::NewTab(browser());
// Installing |extension3_| adds it to the launcher.
ash::ShelfID gmail_id = model_->next_id();
extension_service_->AddExtension(extension3_.get());
EXPECT_EQ(3, model_->item_count());
int gmail_index = model_->ItemIndexByID(gmail_id);
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[gmail_index].type);
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension3_->id()));
launcher_controller_->SetRefocusURLPatternForTest(gmail_id, GURL(gmail_url));
// Check the menu content.
ash::ShelfItem item_browser;
item_browser.type = ash::TYPE_BROWSER_SHORTCUT;
item_browser.id =
launcher_controller_->GetShelfIDForAppID(extension_misc::kChromeAppId);
ash::ShelfItem item_gmail;
item_gmail.type = ash::TYPE_APP_SHORTCUT;
item_gmail.id = gmail_id;
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_gmail, 0, NULL, false));
// Set the gmail URL to a new tab.
base::string16 title1 = ASCIIToUTF16("Test1");
NavigateAndCommitActiveTabWithTitle(browser(), GURL(gmail_url), title1);
base::string16 one_menu_item[] = { title1 };
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_gmail, 1, one_menu_item, false));
// Create a second profile and switch to that user.
std::string user2 = "user2";
TestingProfile* profile2 = CreateMultiUserProfile(user2);
SwitchActiveUser(profile2->GetProfileName());
// No item should have content yet.
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_browser, 0, NULL, true));
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_gmail, 0, NULL, false));
// Transfer the browser of the first user - it should still not show up.
chrome::MultiUserWindowManager::GetInstance()->ShowWindowForUser(
browser()->window()->GetNativeWindow(),
user2);
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_browser, 0, NULL, true));
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_gmail, 0, NULL, false));
}
// Check that V2 applications are creating items properly in the launcher when
// instantiated by the current user.
TEST_F(MultiProfileMultiBrowserShelfLayoutChromeLauncherControllerTest,
V2AppHandlingTwoUsers) {
InitLauncherController();
// Create a profile for our second user (will be destroyed by the framework).
TestingProfile* profile2 = CreateMultiUserProfile("user2");
// Check that there is a browser and a app launcher.
EXPECT_EQ(2, model_->item_count());
// Add a v2 app.
V2App v2_app(profile(), extension1_.get());
EXPECT_EQ(3, model_->item_count());
// After switching users the item should go away.
SwitchActiveUser(profile2->GetProfileName());
EXPECT_EQ(2, model_->item_count());
// And it should come back when switching back.
SwitchActiveUser(profile()->GetProfileName());
EXPECT_EQ(3, 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(MultiProfileMultiBrowserShelfLayoutChromeLauncherControllerTest,
V2AppHandlingTwoUsersEdgeCases) {
InitLauncherController();
// Create a profile for our second user (will be destroyed by the framework).
TestingProfile* profile2 = CreateMultiUserProfile("user2");
// Check that there is a browser and a app launcher.
EXPECT_EQ(2, model_->item_count());
// Switch to an inactive user.
SwitchActiveUser(profile2->GetProfileName());
EXPECT_EQ(2, model_->item_count());
// Add the v2 app to the inactive user and check that no item was added to
// the launcher.
{
V2App v2_app(profile(), extension1_.get());
EXPECT_EQ(2, model_->item_count());
// Switch to the primary user and check that the item is shown.
SwitchActiveUser(profile()->GetProfileName());
EXPECT_EQ(3, model_->item_count());
// Switch to the second user and check that the item goes away - even if the
// item gets closed.
SwitchActiveUser(profile2->GetProfileName());
EXPECT_EQ(2, model_->item_count());
}
// After the application was killed there should be still 2 items.
EXPECT_EQ(2, model_->item_count());
// Switching then back to the default user should not show the additional item
// anymore.
SwitchActiveUser(profile()->GetProfileName());
EXPECT_EQ(2, model_->item_count());
}
// 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(MultiProfileMultiBrowserShelfLayoutChromeLauncherControllerTest,
V2AppFollowsTeleportedWindow) {
InitLauncherController();
chrome::MultiUserWindowManager* manager =
chrome::MultiUserWindowManager::GetInstance();
// Create and add three users / profiles, and go to #1's desktop.
TestingProfile* profile1 = CreateMultiUserProfile("user-1");
TestingProfile* profile2 = CreateMultiUserProfile("user-2");
TestingProfile* profile3 = CreateMultiUserProfile("user-3");
SwitchActiveUser(profile1->GetProfileName());
// A v2 app for user #1 should be shown first and get hidden when switching to
// desktop #2.
V2App v2_app_1(profile1, extension1_.get());
EXPECT_TRUE(v2_app_1.window()->GetNativeWindow()->IsVisible());
SwitchActiveUser(profile2->GetProfileName());
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.
manager->ShowWindowForUser(v2_app_1.window()->GetNativeWindow(),
profile2->GetProfileName());
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.
SwitchActiveUser(profile1->GetProfileName());
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 create an app for user #1 there should land on
// his own desktop (#1).
SwitchActiveUser(profile3->GetProfileName());
V2App v2_app_5(profile1, extension1_.get());
EXPECT_FALSE(v2_app_5.window()->GetNativeWindow()->IsVisible());
SwitchActiveUser(profile1->GetProfileName());
EXPECT_TRUE(v2_app_5.window()->GetNativeWindow()->IsVisible());
// Switching to desktop #2, hiding the app window and creating an app should
// teleport there automatically.
SwitchActiveUser(profile2->GetProfileName());
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(MultiProfileMultiBrowserShelfLayoutChromeLauncherControllerTest,
V2AppHiddenWindows) {
InitLauncherController();
TestingProfile* profile2 = CreateMultiUserProfile("user-2");
SwitchActiveUser(profile()->GetProfileName());
EXPECT_EQ(2, model_->item_count());
V2App v2_app_1(profile(), extension1_.get());
EXPECT_EQ(3, model_->item_count());
{
// Hide and show the app.
v2_app_1.window()->Hide();
EXPECT_EQ(2, model_->item_count());
v2_app_1.window()->Show(extensions::AppWindow::SHOW_ACTIVE);
EXPECT_EQ(3, model_->item_count());
}
{
// Switch user, hide and show the app and switch back.
SwitchActiveUser(profile2->GetProfileName());
EXPECT_EQ(2, model_->item_count());
v2_app_1.window()->Hide();
EXPECT_EQ(2, model_->item_count());
v2_app_1.window()->Show(extensions::AppWindow::SHOW_ACTIVE);
EXPECT_EQ(2, model_->item_count());
SwitchActiveUser(profile()->GetProfileName());
EXPECT_EQ(3, model_->item_count());
}
{
// Switch user, hide the app, switch back and then show it again.
SwitchActiveUser(profile2->GetProfileName());
EXPECT_EQ(2, model_->item_count());
v2_app_1.window()->Hide();
EXPECT_EQ(2, model_->item_count());
SwitchActiveUser(profile()->GetProfileName());
EXPECT_EQ(2, model_->item_count());
v2_app_1.window()->Show(extensions::AppWindow::SHOW_ACTIVE);
EXPECT_EQ(3, 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(3, model_->item_count());
v2_app_2.window()->Hide();
EXPECT_EQ(3, model_->item_count());
v2_app_2.window()->Show(extensions::AppWindow::SHOW_ACTIVE);
EXPECT_EQ(3, model_->item_count());
v2_app_1.window()->Hide();
v2_app_2.window()->Hide();
EXPECT_EQ(2, model_->item_count());
}
}
#endif // defined(OS_CHROMEOS)
// Checks that the generated menu list properly activates items.
TEST_F(ChromeLauncherControllerTest, V1AppMenuExecution) {
InitLauncherControllerWithBrowser();
// Add |extension3_| to the launcher and add two items.
GURL gmail = GURL("https://mail.google.com/mail/u");
ash::ShelfID gmail_id = model_->next_id();
extension_service_->AddExtension(extension3_.get());
launcher_controller_->SetRefocusURLPatternForTest(gmail_id, GURL(gmail_url));
base::string16 title1 = ASCIIToUTF16("Test1");
NavigateAndCommitActiveTabWithTitle(browser(), GURL(gmail_url), title1);
chrome::NewTab(browser());
base::string16 title2 = ASCIIToUTF16("Test2");
NavigateAndCommitActiveTabWithTitle(browser(), GURL(gmail_url), title2);
// Check that the menu is properly set.
ash::ShelfItem item_gmail;
item_gmail.type = ash::TYPE_APP_SHORTCUT;
item_gmail.id = gmail_id;
base::string16 two_menu_items[] = {title1, title2};
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_gmail, 2, two_menu_items, false));
EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
// Execute the second item in the list (which shouldn't do anything since that
// item is per definition already the active tab).
{
scoped_ptr<ash::ShelfMenuModel> menu(new LauncherApplicationMenuItemModel(
launcher_controller_->GetApplicationList(item_gmail, 0)));
// The first element in the menu is a spacing separator. On some systems
// (e.g. Windows) such things do not exist. As such we check the existence
// and adjust dynamically.
int first_item =
(menu->GetTypeAt(0) == ui::MenuModel::TYPE_SEPARATOR) ? 1 : 0;
menu->ActivatedAt(first_item + 3);
}
EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
// Execute the first item.
{
scoped_ptr<ash::ShelfMenuModel> menu(new LauncherApplicationMenuItemModel(
launcher_controller_->GetApplicationList(item_gmail, 0)));
int first_item =
(menu->GetTypeAt(0) == ui::MenuModel::TYPE_SEPARATOR) ? 1 : 0;
menu->ActivatedAt(first_item + 2);
}
// Now the active tab should be the second item.
EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
}
// Checks that the generated menu list properly deletes items.
TEST_F(ChromeLauncherControllerTest, V1AppMenuDeletionExecution) {
InitLauncherControllerWithBrowser();
// Add |extension3_| to the launcher and add two items.
GURL gmail = GURL("https://mail.google.com/mail/u");
ash::ShelfID gmail_id = model_->next_id();
extension_service_->AddExtension(extension3_.get());
launcher_controller_->SetRefocusURLPatternForTest(gmail_id, GURL(gmail_url));
base::string16 title1 = ASCIIToUTF16("Test1");
NavigateAndCommitActiveTabWithTitle(browser(), GURL(gmail_url), title1);
chrome::NewTab(browser());
base::string16 title2 = ASCIIToUTF16("Test2");
NavigateAndCommitActiveTabWithTitle(browser(), GURL(gmail_url), title2);
// Check that the menu is properly set.
ash::ShelfItem item_gmail;
item_gmail.type = ash::TYPE_APP_SHORTCUT;
item_gmail.id = gmail_id;
base::string16 two_menu_items[] = {title1, title2};
EXPECT_TRUE(CheckMenuCreation(
launcher_controller_.get(), item_gmail, 2, two_menu_items, false));
int tabs = browser()->tab_strip_model()->count();
// Activate the proper tab through the menu item.
{
ChromeLauncherAppMenuItems items =
launcher_controller_->GetApplicationList(item_gmail, 0);
items[1]->Execute(0);
EXPECT_EQ(tabs, browser()->tab_strip_model()->count());
}
// Delete one tab through the menu item.
{
ChromeLauncherAppMenuItems items =
launcher_controller_->GetApplicationList(item_gmail, 0);
items[1]->Execute(ui::EF_SHIFT_DOWN);
EXPECT_EQ(--tabs, browser()->tab_strip_model()->count());
}
}
// Tests that panels create launcher items correctly
TEST_F(ChromeLauncherControllerTest, AppPanels) {
InitLauncherControllerWithBrowser();
// App list and Browser shortcut ShelfItems are added.
EXPECT_EQ(2, model_observer_->added());
TestAppIconLoaderImpl* app_icon_loader = new TestAppIconLoaderImpl();
SetAppIconLoader(app_icon_loader);
// Test adding an app panel
std::string app_id = extension1_->id();
AppWindowLauncherItemController* app_panel_controller =
new AppWindowLauncherItemController(
LauncherItemController::TYPE_APP_PANEL,
"id",
app_id,
launcher_controller_.get());
ash::ShelfID shelf_id1 = launcher_controller_->CreateAppLauncherItem(
app_panel_controller, app_id, ash::STATUS_RUNNING);
int panel_index = model_observer_->last_index();
EXPECT_EQ(3, model_observer_->added());
EXPECT_EQ(0, model_observer_->changed());
EXPECT_EQ(1, app_icon_loader->fetch_count());
model_observer_->clear_counts();
// App panels should have a separate identifier than the app id
EXPECT_EQ(0, launcher_controller_->GetShelfIDForAppID(app_id));
// Setting the app image image should not change the panel if it set its icon
app_panel_controller->set_image_set_by_controller(true);
gfx::ImageSkia image;
launcher_controller_->SetAppImage(app_id, image);
EXPECT_EQ(0, model_observer_->changed());
model_observer_->clear_counts();
// Add a second app panel and verify that it get the same index as the first
// one had, being added to the left of the existing panel.
AppWindowLauncherItemController* app_panel_controller2 =
new AppWindowLauncherItemController(
LauncherItemController::TYPE_APP_PANEL,
"id",
app_id,
launcher_controller_.get());
ash::ShelfID shelf_id2 = launcher_controller_->CreateAppLauncherItem(
app_panel_controller2, app_id, ash::STATUS_RUNNING);
EXPECT_EQ(panel_index, model_observer_->last_index());
EXPECT_EQ(1, model_observer_->added());
model_observer_->clear_counts();
launcher_controller_->CloseLauncherItem(shelf_id2);
launcher_controller_->CloseLauncherItem(shelf_id1);
EXPECT_EQ(2, model_observer_->removed());
}
// Tests that the Gmail extension matches more then the app itself claims with
// the manifest file.
TEST_F(ChromeLauncherControllerTest, GmailMatching) {
InitLauncherControllerWithBrowser();
// Create a Gmail browser tab.
chrome::NewTab(browser());
base::string16 title = ASCIIToUTF16("Test");
NavigateAndCommitActiveTabWithTitle(browser(), GURL(gmail_url), title);
content::WebContents* content =
browser()->tab_strip_model()->GetActiveWebContents();
// Check that the launcher controller does not recognize the running app.
EXPECT_FALSE(launcher_controller_->ContentCanBeHandledByGmailApp(content));
// Installing |extension3_| adds it to the launcher.
ash::ShelfID gmail_id = model_->next_id();
extension_service_->AddExtension(extension3_.get());
EXPECT_EQ(3, model_->item_count());
int gmail_index = model_->ItemIndexByID(gmail_id);
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[gmail_index].type);
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension3_->id()));
// Check that it is now handled.
EXPECT_TRUE(launcher_controller_->ContentCanBeHandledByGmailApp(content));
// Check also that the app has detected that properly.
ash::ShelfItem item_gmail;
item_gmail.type = ash::TYPE_APP_SHORTCUT;
item_gmail.id = gmail_id;
EXPECT_EQ(2U, launcher_controller_->GetApplicationList(item_gmail, 0).size());
}
// Tests that the Gmail extension does not match the offline verison.
TEST_F(ChromeLauncherControllerTest, GmailOfflineMatching) {
InitLauncherControllerWithBrowser();
// Create a Gmail browser tab.
chrome::NewTab(browser());
base::string16 title = ASCIIToUTF16("Test");
NavigateAndCommitActiveTabWithTitle(browser(),
GURL(offline_gmail_url),
title);
content::WebContents* content =
browser()->tab_strip_model()->GetActiveWebContents();
// Installing |extension3_| adds it to the launcher.
ash::ShelfID gmail_id = model_->next_id();
extension_service_->AddExtension(extension3_.get());
EXPECT_EQ(3, model_->item_count());
int gmail_index = model_->ItemIndexByID(gmail_id);
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[gmail_index].type);
EXPECT_TRUE(launcher_controller_->IsAppPinned(extension3_->id()));
// The content should not be able to be handled by the app.
EXPECT_FALSE(launcher_controller_->ContentCanBeHandledByGmailApp(content));
}
// Verify that the launcher item positions are persisted and restored.
TEST_F(ChromeLauncherControllerTest, PersistLauncherItemPositions) {
InitLauncherController();
TestAppTabHelperImpl* app_tab_helper = new TestAppTabHelperImpl;
SetAppTabHelper(app_tab_helper);
EXPECT_EQ(ash::TYPE_APP_LIST, model_->items()[0].type);
EXPECT_EQ(ash::TYPE_BROWSER_SHORTCUT, model_->items()[1].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());
app_tab_helper->SetAppID(tab_strip_model->GetWebContentsAt(0), "1");
app_tab_helper->SetAppID(tab_strip_model->GetWebContentsAt(1), "2");
EXPECT_FALSE(launcher_controller_->IsAppPinned("1"));
launcher_controller_->PinAppWithID("1");
EXPECT_TRUE(launcher_controller_->IsAppPinned("1"));
launcher_controller_->PinAppWithID("2");
EXPECT_EQ(ash::TYPE_APP_LIST, model_->items()[0].type);
EXPECT_EQ(ash::TYPE_BROWSER_SHORTCUT, model_->items()[1].type);
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[2].type);
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[3].type);
// Move browser shortcut item from index 1 to index 3.
model_->Move(1, 3);
EXPECT_EQ(ash::TYPE_APP_LIST, model_->items()[0].type);
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[1].type);
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[2].type);
EXPECT_EQ(ash::TYPE_BROWSER_SHORTCUT, model_->items()[3].type);
SetShelfItemDelegateManager(nullptr);
launcher_controller_.reset();
if (!ash::Shell::HasInstance()) {
delete item_delegate_manager_;
} else {
// Clear already registered ShelfItemDelegate.
ash::test::ShelfItemDelegateManagerTestAPI test(item_delegate_manager_);
test.RemoveAllShelfItemDelegateForTest();
}
model_.reset(new ash::ShelfModel);
AddAppListLauncherItem();
launcher_controller_.reset(
ChromeLauncherController::CreateInstance(profile(), model_.get()));
app_tab_helper = new TestAppTabHelperImpl;
app_tab_helper->SetAppID(tab_strip_model->GetWebContentsAt(0), "1");
app_tab_helper->SetAppID(tab_strip_model->GetWebContentsAt(1), "2");
SetAppTabHelper(app_tab_helper);
if (!ash::Shell::HasInstance()) {
item_delegate_manager_ = new ash::ShelfItemDelegateManager(model_.get());
SetShelfItemDelegateManager(item_delegate_manager_);
}
launcher_controller_->Init();
// Check ShelfItems are restored after resetting ChromeLauncherController.
EXPECT_EQ(ash::TYPE_APP_LIST, model_->items()[0].type);
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[1].type);
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[2].type);
EXPECT_EQ(ash::TYPE_BROWSER_SHORTCUT, model_->items()[3].type);
}
// Verifies pinned apps are persisted and restored.
TEST_F(ChromeLauncherControllerTest, PersistPinned) {
InitLauncherControllerWithBrowser();
size_t initial_size = model_->items().size();
TabStripModel* tab_strip_model = browser()->tab_strip_model();
EXPECT_EQ(1, tab_strip_model->count());
TestAppTabHelperImpl* app_tab_helper = new TestAppTabHelperImpl;
app_tab_helper->SetAppID(tab_strip_model->GetWebContentsAt(0), "1");
SetAppTabHelper(app_tab_helper);
TestAppIconLoaderImpl* app_icon_loader = new TestAppIconLoaderImpl;
SetAppIconLoader(app_icon_loader);
EXPECT_EQ(0, app_icon_loader->fetch_count());
launcher_controller_->PinAppWithID("1");
ash::ShelfID id = launcher_controller_->GetShelfIDForAppID("1");
int app_index = model_->ItemIndexByID(id);
EXPECT_EQ(1, app_icon_loader->fetch_count());
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[app_index].type);
EXPECT_TRUE(launcher_controller_->IsAppPinned("1"));
EXPECT_FALSE(launcher_controller_->IsAppPinned("0"));
EXPECT_EQ(initial_size + 1, model_->items().size());
SetShelfItemDelegateManager(nullptr);
launcher_controller_.reset();
if (!ash::Shell::HasInstance()) {
delete item_delegate_manager_;
} else {
// Clear already registered ShelfItemDelegate.
ash::test::ShelfItemDelegateManagerTestAPI test(item_delegate_manager_);
test.RemoveAllShelfItemDelegateForTest();
}
model_.reset(new ash::ShelfModel);
AddAppListLauncherItem();
launcher_controller_.reset(
ChromeLauncherController::CreateInstance(profile(), model_.get()));
app_tab_helper = new TestAppTabHelperImpl;
app_tab_helper->SetAppID(tab_strip_model->GetWebContentsAt(0), "1");
SetAppTabHelper(app_tab_helper);
app_icon_loader = new TestAppIconLoaderImpl;
SetAppIconLoader(app_icon_loader);
if (!ash::Shell::HasInstance()) {
item_delegate_manager_ = new ash::ShelfItemDelegateManager(model_.get());
SetShelfItemDelegateManager(item_delegate_manager_);
}
launcher_controller_->Init();
EXPECT_EQ(1, app_icon_loader->fetch_count());
ASSERT_EQ(initial_size + 1, model_->items().size());
EXPECT_TRUE(launcher_controller_->IsAppPinned("1"));
EXPECT_FALSE(launcher_controller_->IsAppPinned("0"));
EXPECT_EQ(ash::TYPE_APP_SHORTCUT, model_->items()[app_index].type);
launcher_controller_->UnpinAppWithID("1");
ASSERT_EQ(initial_size, model_->items().size());
}
|