1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <array>
#include <memory>
#include <string>
#include "base/files/scoped_temp_dir.h"
#include "base/format_macros.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/strings/pattern.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/to_string.h"
#include "base/test/gmock_expected_support.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/test/test_future.h"
#include "base/threading/thread_restrictions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/apps/platform_apps/app_browsertest_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/devtools/devtools_window_testing.h"
#include "chrome/browser/extensions/api/tabs/tabs_api.h"
#include "chrome/browser/extensions/api/tabs/tabs_constants.h"
#include "chrome/browser/extensions/api/tabs/tabs_windows_api.h"
#include "chrome/browser/extensions/browser_extension_window_controller.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_tab_util.h"
#include "chrome/browser/extensions/profile_util.h"
#include "chrome/browser/extensions/window_controller.h"
#include "chrome/browser/prefs/incognito_mode_prefs.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/resource_coordinator/tab_lifecycle_unit_external.h"
#include "chrome/browser/resource_coordinator/tab_lifecycle_unit_source.h"
#include "chrome/browser/resource_coordinator/tab_manager.h"
#include "chrome/browser/resource_coordinator/time.h"
#include "chrome/browser/resource_coordinator/utils.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/browser_window.h"
#include "chrome/browser/ui/tabs/saved_tab_groups/saved_tab_group_utils.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/web_applications/test/isolated_web_app_test_utils.h"
#include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h"
#include "chrome/browser/web_applications/isolated_web_apps/commands/install_isolated_web_app_command.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_install_source.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_trust_checker.h"
#include "chrome/browser/web_applications/isolated_web_apps/isolated_web_app_url_info.h"
#include "chrome/browser/web_applications/isolated_web_apps/test/isolated_web_app_builder.h"
#include "chrome/browser/web_applications/isolated_web_apps/test/test_signed_web_bundle_builder.h"
#include "chrome/browser/web_applications/test/web_app_install_test_utils.h"
#include "chrome/browser/web_applications/web_app_command_scheduler.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_provider_factory.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/saved_tab_groups/public/tab_group_sync_service.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/common/content_features.h"
#include "content/public/common/url_constants.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/test_navigation_observer.h"
#include "extensions/browser/api_test_utils.h"
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_function_dispatcher.h"
#include "extensions/browser/test_event_router_observer.h"
#include "extensions/common/constants.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/extension_builder.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/mojom/context_type.mojom.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/result_catcher.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "pdf/buildflags.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/page/page_zoom.h"
#include "ui/base/ozone_buildflags.h"
#include "ui/base/window_open_disposition.h"
#include "ui/display/display.h"
#include "ui/display/screen.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/views/widget/widget_interactive_uitest_utils.h"
#if BUILDFLAG(IS_MAC)
#include "ui/base/test/scoped_fake_nswindow_fullscreen.h"
#endif
#if BUILDFLAG(ENABLE_PDF)
#include "base/test/with_feature_override.h"
#include "chrome/browser/pdf/pdf_extension_test_base.h"
#include "chrome/browser/pdf/pdf_extension_test_util.h"
#include "chrome/browser/pdf/test_pdf_viewer_stream_manager.h"
#include "pdf/pdf_features.h"
#endif
namespace extensions {
namespace keys = tabs_constants;
namespace utils = api_test_utils;
namespace {
// Returns true if |val| contains any privacy information, e.g. url,
// pendingUrl, title or faviconUrl.
bool HasAnyPrivacySensitiveFields(const base::Value::Dict& dict) {
constexpr std::array privacySensitiveKeys{
tabs_constants::kUrlKey, tabs_constants::kTitleKey,
tabs_constants::kFaviconUrlKey, tabs_constants::kPendingUrlKey};
for (auto* key : privacySensitiveKeys) {
if (dict.contains(key)) {
return true;
}
}
return false;
}
class TestFunctionDispatcherDelegate
: public extensions::ExtensionFunctionDispatcher::Delegate {
public:
explicit TestFunctionDispatcherDelegate(Browser* browser)
: browser_(browser) {}
~TestFunctionDispatcherDelegate() override = default;
private:
extensions::WindowController* GetExtensionWindowController() const override {
return browser_->extension_window_controller();
}
content::WebContents* GetAssociatedWebContents() const override {
return nullptr;
}
raw_ptr<Browser> browser_;
};
class ExtensionTabsTest : public PlatformAppBrowserTest {
public:
ExtensionTabsTest() = default;
ExtensionTabsTest(const ExtensionTabsTest&) = delete;
ExtensionTabsTest& operator=(const ExtensionTabsTest&) = delete;
std::string GetWindowType(Browser* test_browser,
scoped_refptr<const Extension> extension) {
auto function = base::MakeRefCounted<WindowsGetFunction>();
function->set_extension(extension.get());
std::string args = base::StringPrintf(
R"([%u, {"windowTypes": ["normal", "popup", "devtools", "app"]}])",
ExtensionTabUtil::GetWindowId(test_browser));
base::Value::Dict result =
utils::ToDict(utils::RunFunctionAndReturnSingleResult(
function.get(), args, browser()->profile()));
return api_test_utils::GetString(result, "type");
}
std::optional<base::Value> RunFunctionWithDispatcherDelegateAndReturnValue(
scoped_refptr<ExtensionFunction> function,
const std::string& args,
Browser* browser) {
auto dispatcher = std::make_unique<extensions::ExtensionFunctionDispatcher>(
browser->profile());
TestFunctionDispatcherDelegate dispatcher_delegate(browser);
dispatcher->set_delegate(&dispatcher_delegate);
return utils::RunFunctionWithDelegateAndReturnSingleResult(
std::move(function), args, std::move(dispatcher),
utils::FunctionMode::kNone);
}
};
class ExtensionWindowCreateTest : public InProcessBrowserTest {
public:
// Runs chrome.windows.create(), expecting an error.
std::string RunCreateWindowExpectError(const std::string& args) {
auto function = base::MakeRefCounted<WindowsCreateFunction>();
function->set_extension(ExtensionBuilder("Test").Build().get());
return api_test_utils::RunFunctionAndReturnError(function.get(), args,
browser()->profile());
}
};
const int kUndefinedId = INT_MIN;
const ExtensionTabUtil::ScrubTabBehavior kDontScrubBehavior = {
ExtensionTabUtil::kDontScrubTab, ExtensionTabUtil::kDontScrubTab};
int GetTabId(const base::Value::Dict& tab) {
return tab.FindInt(extension_misc::kId).value_or(kUndefinedId);
}
int GetTabWindowId(const base::Value::Dict& tab) {
return tab.FindInt(keys::kWindowIdKey).value_or(kUndefinedId);
}
int GetWindowId(const base::Value::Dict& window) {
return window.FindInt(extension_misc::kId).value_or(kUndefinedId);
;
}
} // namespace
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetWindow) {
int window_id = ExtensionTabUtil::GetWindowId(browser());
// Invalid window ID error.
auto function = base::MakeRefCounted<WindowsGetFunction>();
scoped_refptr<const Extension> extension(ExtensionBuilder("Test").Build());
function->set_extension(extension.get());
EXPECT_TRUE(base::MatchPattern(
utils::RunFunctionAndReturnError(
function.get(), base::StringPrintf("[%u]", window_id + 1),
browser()->profile()),
ExtensionTabUtil::kWindowNotFoundError));
// Basic window details.
gfx::Rect bounds;
if (browser()->window()->IsMinimized())
bounds = browser()->window()->GetRestoredBounds();
else
bounds = browser()->window()->GetBounds();
function = base::MakeRefCounted<WindowsGetFunction>();
function->set_extension(extension.get());
base::Value::Dict result =
utils::ToDict(utils::RunFunctionAndReturnSingleResult(
function.get(), base::StringPrintf("[%u]", window_id),
browser()->profile()));
EXPECT_EQ(window_id, GetWindowId(result));
EXPECT_FALSE(api_test_utils::GetBoolean(result, "incognito"));
EXPECT_EQ("normal", api_test_utils::GetString(result, "type"));
EXPECT_EQ(bounds.x(), api_test_utils::GetInteger(result, "left"));
EXPECT_EQ(bounds.y(), api_test_utils::GetInteger(result, "top"));
EXPECT_EQ(bounds.width(), api_test_utils::GetInteger(result, "width"));
EXPECT_EQ(bounds.height(), api_test_utils::GetInteger(result, "height"));
// With "populate" enabled.
function = base::MakeRefCounted<WindowsGetFunction>();
function->set_extension(extension.get());
result = utils::ToDict(utils::RunFunctionAndReturnSingleResult(
function.get(),
base::StringPrintf("[%u, {\"populate\": true}]", window_id),
browser()->profile()));
EXPECT_EQ(window_id, GetWindowId(result));
// "populate" was enabled so tabs should be populated.
base::Value::List tabs =
api_test_utils::GetList(result, ExtensionTabUtil::kTabsKey);
ASSERT_FALSE(tabs.empty());
std::optional<int> tab0_id = tabs[0].GetDict().FindInt(extension_misc::kId);
ASSERT_TRUE(tab0_id.has_value());
EXPECT_GE(*tab0_id, 0);
// TODO(aa): Can't assume window is focused. On mac, calling Activate() from a
// browser test doesn't seem to do anything, so can't test the opposite
// either.
EXPECT_EQ(browser()->window()->IsActive(),
api_test_utils::GetBoolean(result, "focused"));
// TODO(aa): Minimized and maximized dimensions. Is there a way to set
// minimize/maximize programmatically?
// Check window type.
EXPECT_EQ("normal", GetWindowType(browser(), extension));
Browser* test_browser = Browser::Create(
Browser::CreateParams(Browser::TYPE_POPUP, browser()->profile(), true));
EXPECT_EQ("popup", GetWindowType(test_browser, extension));
DevToolsWindow* devtools = DevToolsWindowTesting::OpenDevToolsWindowSync(
browser()->tab_strip_model()->GetWebContentsAt(0), false /* is_docked */);
EXPECT_EQ("devtools",
GetWindowType(DevToolsWindowTesting::Get(devtools)->browser(),
extension));
DevToolsWindowTesting::CloseDevToolsWindowSync(devtools);
test_browser = Browser::Create(Browser::CreateParams::CreateForApp(
"test-app", true, gfx::Rect(), browser()->profile(), true));
EXPECT_EQ("app", GetWindowType(test_browser, extension));
test_browser = Browser::Create(Browser::CreateParams::CreateForAppPopup(
"test-app-popup", true, gfx::Rect(), browser()->profile(), true));
EXPECT_EQ("popup", GetWindowType(test_browser, extension));
// Incognito.
Browser* incognito_browser = CreateIncognitoBrowser();
int incognito_window_id = ExtensionTabUtil::GetWindowId(incognito_browser);
// Without "include_incognito".
function = base::MakeRefCounted<WindowsGetFunction>();
function->set_extension(extension.get());
EXPECT_TRUE(base::MatchPattern(
utils::RunFunctionAndReturnError(
function.get(), base::StringPrintf("[%u]", incognito_window_id),
browser()->profile()),
ExtensionTabUtil::kWindowNotFoundError));
// With "include_incognito".
function = base::MakeRefCounted<WindowsGetFunction>();
function->set_extension(extension.get());
result = utils::ToDict(utils::RunFunctionAndReturnSingleResult(
function.get(), base::StringPrintf("[%u]", incognito_window_id),
browser()->profile(), api_test_utils::FunctionMode::kIncognito));
EXPECT_TRUE(api_test_utils::GetBoolean(result, "incognito"));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetCurrentWindow) {
int window_id = ExtensionTabUtil::GetWindowId(browser());
Browser* new_browser = CreateBrowser(browser()->profile());
int new_id = ExtensionTabUtil::GetWindowId(new_browser);
// Get the current window using new_browser.
auto function = base::MakeRefCounted<WindowsGetCurrentFunction>();
scoped_refptr<const Extension> extension(ExtensionBuilder("Test").Build());
function->set_extension(extension.get());
base::Value::Dict result =
utils::ToDict(RunFunctionWithDispatcherDelegateAndReturnValue(
function.get(), "[]", new_browser));
// The id should match the window id of the browser instance that was passed
// to RunFunctionWithDispatcherDelegateAndReturnValue.
EXPECT_EQ(new_id, GetWindowId(result));
EXPECT_FALSE(result.contains(ExtensionTabUtil::kTabsKey));
// Get the current window using the old window and make the tabs populated.
function = base::MakeRefCounted<WindowsGetCurrentFunction>();
function->set_extension(extension.get());
result = utils::ToDict(RunFunctionWithDispatcherDelegateAndReturnValue(
function.get(), "[{\"populate\": true}]", browser()));
// The id should match the window id of the browser instance that was passed
// to RunFunctionWithDispatcherDelegateAndReturnValue.
EXPECT_EQ(window_id, GetWindowId(result));
// "populate" was enabled so tabs should be populated.
base::Value::List tabs =
api_test_utils::GetList(result, ExtensionTabUtil::kTabsKey);
ASSERT_FALSE(tabs.empty());
std::optional<int> tab0_id = tabs[0].GetDict().FindInt(extension_misc::kId);
ASSERT_TRUE(tab0_id.has_value());
// The tab id should not be -1 as this is a browser window.
EXPECT_GE(*tab0_id, 0);
}
// TODO(crbug.com/40745605): Test is flaky on Linux debug builds.
#if BUILDFLAG(IS_LINUX) && !defined(NDEBUG)
#define MAYBE_GetAllWindows DISABLED_GetAllWindows
#else
#define MAYBE_GetAllWindows GetAllWindows
#endif
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, MAYBE_GetAllWindows) {
const size_t NUM_WINDOWS = 5;
std::set<int> window_ids;
std::set<int> result_ids;
window_ids.insert(ExtensionTabUtil::GetWindowId(browser()));
for (size_t i = 0; i < NUM_WINDOWS - 1; ++i) {
Browser* new_browser = CreateBrowser(browser()->profile());
window_ids.insert(ExtensionTabUtil::GetWindowId(new_browser));
}
// Application windows should not be accessible to extensions (app windows are
// only accessible to the owning item).
AppWindow* app_window = CreateTestAppWindow("{}");
// Undocked DevTools window should not be accessible, unless included in the
// type filter mask.
DevToolsWindow* devtools = DevToolsWindowTesting::OpenDevToolsWindowSync(
browser()->tab_strip_model()->GetWebContentsAt(0), false /* is_docked */);
auto function = base::MakeRefCounted<WindowsGetAllFunction>();
scoped_refptr<const Extension> extension(ExtensionBuilder("Test").Build());
function->set_extension(extension.get());
base::Value::List windows =
utils::ToList(utils::RunFunctionAndReturnSingleResult(
function.get(), "[]", browser()->profile()));
EXPECT_EQ(window_ids.size(), windows.size());
for (const base::Value& result_window : windows) {
base::Value::Dict result_window_dict = utils::ToDict(result_window);
result_ids.insert(GetWindowId(result_window_dict));
// "populate" was not passed in so tabs are not populated.
const base::Value::List* tabs =
result_window_dict.FindList(ExtensionTabUtil::kTabsKey);
EXPECT_FALSE(tabs);
}
// The returned ids should contain all the current browser instance ids.
EXPECT_EQ(window_ids, result_ids);
result_ids.clear();
function = base::MakeRefCounted<WindowsGetAllFunction>();
function->set_extension(extension.get());
windows = utils::ToList(utils::RunFunctionAndReturnSingleResult(
function.get(), "[{\"populate\": true}]", browser()->profile()));
EXPECT_EQ(window_ids.size(), windows.size());
for (const base::Value& result_window : windows) {
base::Value::Dict result_window_dict = utils::ToDict(result_window);
result_ids.insert(GetWindowId(result_window_dict));
// "populate" was enabled so tabs should be populated.
const base::Value::List* tabs =
result_window_dict.FindList(ExtensionTabUtil::kTabsKey);
EXPECT_TRUE(tabs);
}
// The returned ids should contain all the current app, browser and
// devtools instance ids.
EXPECT_EQ(window_ids, result_ids);
DevToolsWindowTesting::CloseDevToolsWindowSync(devtools);
CloseAppWindow(app_window);
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, GetAllWindowsAllTypes) {
const size_t NUM_WINDOWS = 5;
std::set<int> window_ids;
std::set<int> result_ids;
window_ids.insert(ExtensionTabUtil::GetWindowId(browser()));
for (size_t i = 0; i < NUM_WINDOWS - 1; ++i) {
Browser* new_browser = CreateBrowser(browser()->profile());
window_ids.insert(ExtensionTabUtil::GetWindowId(new_browser));
}
// Application windows should not be accessible to extensions (app windows are
// only accessible to the owning item).
AppWindow* app_window = CreateTestAppWindow("{}");
// Undocked DevTools window should be accessible too, since they have been
// explicitly requested as part of the type filter mask.
DevToolsWindow* devtools = DevToolsWindowTesting::OpenDevToolsWindowSync(
browser()->tab_strip_model()->GetWebContentsAt(0), false /* is_docked */);
window_ids.insert(ExtensionTabUtil::GetWindowId(
DevToolsWindowTesting::Get(devtools)->browser()));
auto function = base::MakeRefCounted<WindowsGetAllFunction>();
scoped_refptr<const Extension> extension(ExtensionBuilder("Test").Build());
function->set_extension(extension.get());
base::Value::List windows(
utils::ToList(utils::RunFunctionAndReturnSingleResult(
function.get(),
"[{\"windowTypes\": [\"app\", \"devtools\", \"normal\", \"panel\", "
"\"popup\"]}]",
browser()->profile())));
EXPECT_EQ(window_ids.size(), windows.size());
for (const base::Value& result_window : windows) {
base::Value::Dict result_window_dict = utils::ToDict(result_window);
result_ids.insert(GetWindowId(result_window_dict));
// "populate" was not passed in so tabs are not populated.
const base::Value::List* tabs =
result_window_dict.FindList(ExtensionTabUtil::kTabsKey);
EXPECT_FALSE(tabs);
}
// The returned ids should contain all the browser and devtools instance ids.
EXPECT_EQ(window_ids, result_ids);
result_ids.clear();
function = base::MakeRefCounted<WindowsGetAllFunction>();
function->set_extension(extension.get());
windows = utils::ToList(utils::RunFunctionAndReturnSingleResult(
function.get(),
"[{\"populate\": true, \"windowTypes\": [\"app\", \"devtools\", "
"\"normal\", \"panel\", \"popup\"]}]",
browser()->profile()));
EXPECT_EQ(window_ids.size(), windows.size());
for (const base::Value& result_window : windows) {
base::Value::Dict result_window_dict = utils::ToDict(result_window);
result_ids.insert(GetWindowId(result_window_dict));
// "populate" was enabled so tabs should be populated.
const base::Value::List* tabs =
result_window_dict.FindList(ExtensionTabUtil::kTabsKey);
EXPECT_TRUE(tabs);
}
// The returned ids should contain all the browser and devtools instance ids.
EXPECT_EQ(window_ids, result_ids);
DevToolsWindowTesting::CloseDevToolsWindowSync(devtools);
CloseAppWindow(app_window);
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, UpdateNoPermissions) {
// The test empty extension has no permissions, therefore it should not get
// tab data in the function result.
auto update_tab_function = base::MakeRefCounted<TabsUpdateFunction>();
scoped_refptr<const Extension> empty_extension(
ExtensionBuilder("Test").Build());
update_tab_function->set_extension(empty_extension.get());
// Without a callback the function will not generate a result.
update_tab_function->set_has_callback(true);
const base::Value::Dict result =
utils::ToDict(utils::RunFunctionAndReturnSingleResult(
update_tab_function.get(),
"[null, {\"url\": \"about:blank\", \"pinned\": true}]",
browser()->profile()));
// The url is stripped since the extension does not have tab permissions.
EXPECT_FALSE(result.contains("url"));
EXPECT_TRUE(api_test_utils::GetBoolean(result, "pinned"));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest,
DisallowNonIncognitoUrlInIncognitoWindow) {
Browser* incognito = CreateIncognitoBrowser();
auto update_tab_function = base::MakeRefCounted<TabsUpdateFunction>();
scoped_refptr<const Extension> extension(ExtensionBuilder("Test").Build());
update_tab_function->set_extension(extension.get());
update_tab_function->set_include_incognito_information(true);
std::string error = api_test_utils::RunFunctionAndReturnError(
update_tab_function.get(),
std::string("[null, {\"url\": \"") + chrome::kChromeUIExtensionsURL +
chrome::kExtensionConfigureCommandsSubPage + "\"}]",
incognito->profile(), // incognito doesn't have any tabs.
api_test_utils::FunctionMode::kNone);
EXPECT_EQ(ErrorUtils::FormatErrorMessage(
tabs_constants::kURLsNotAllowedInIncognitoError,
std::string(chrome::kChromeUIExtensionsURL) +
chrome::kExtensionConfigureCommandsSubPage),
error);
// Ensure the tab was not updated. It should stay as the new tab page.
EXPECT_EQ(1, incognito->tab_strip_model()->count());
EXPECT_EQ(GURL(url::kAboutBlankURL), incognito->tab_strip_model()
->GetActiveWebContents()
->GetLastCommittedURL());
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DefaultToIncognitoWhenItIsForced) {
static const char kArgsWithoutExplicitIncognitoParam[] =
"[{\"url\": \"about:blank\"}]";
// Force Incognito mode.
IncognitoModePrefs::SetAvailability(
browser()->profile()->GetPrefs(),
policy::IncognitoModeAvailability::kForced);
// Run without an explicit "incognito" param.
auto function = base::MakeRefCounted<WindowsCreateFunction>();
function->SetRenderFrameHost(browser()
->tab_strip_model()
->GetActiveWebContents()
->GetPrimaryMainFrame());
scoped_refptr<const Extension> extension(ExtensionBuilder("Test").Build());
function->set_extension(extension.get());
base::Value::Dict result =
utils::ToDict(utils::RunFunctionAndReturnSingleResult(
function.get(), kArgsWithoutExplicitIncognitoParam,
browser()->profile(), api_test_utils::FunctionMode::kIncognito));
// Make sure it is a new(different) window.
EXPECT_NE(ExtensionTabUtil::GetWindowId(browser()), GetWindowId(result));
// ... and it is incognito.
EXPECT_TRUE(api_test_utils::GetBoolean(result, "incognito"));
// Now try creating a window from incognito window.
Browser* incognito_browser = CreateIncognitoBrowser();
// Run without an explicit "incognito" param.
function = base::MakeRefCounted<WindowsCreateFunction>();
function->SetRenderFrameHost(browser()
->tab_strip_model()
->GetActiveWebContents()
->GetPrimaryMainFrame());
function->set_extension(extension.get());
result = utils::ToDict(utils::RunFunctionAndReturnSingleResult(
function.get(), kArgsWithoutExplicitIncognitoParam,
incognito_browser->profile(), api_test_utils::FunctionMode::kIncognito));
// Make sure it is a new(different) window.
EXPECT_NE(ExtensionTabUtil::GetWindowId(incognito_browser),
GetWindowId(result));
// ... and it is incognito.
EXPECT_TRUE(api_test_utils::GetBoolean(result, "incognito"));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest,
DefaultToIncognitoWhenItIsForcedAndNoArgs) {
static const char kEmptyArgs[] = "[]";
// Force Incognito mode.
IncognitoModePrefs::SetAvailability(
browser()->profile()->GetPrefs(),
policy::IncognitoModeAvailability::kForced);
// Run without an explicit "incognito" param.
auto function = base::MakeRefCounted<WindowsCreateFunction>();
scoped_refptr<const Extension> extension(ExtensionBuilder("Test").Build());
function->set_extension(extension.get());
base::Value::Dict result =
utils::ToDict(utils::RunFunctionAndReturnSingleResult(
function.get(), kEmptyArgs, browser()->profile(),
api_test_utils::FunctionMode::kIncognito));
// Make sure it is a new(different) window.
EXPECT_NE(ExtensionTabUtil::GetWindowId(browser()), GetWindowId(result));
// ... and it is incognito.
EXPECT_TRUE(api_test_utils::GetBoolean(result, "incognito"));
// Now try creating a window from incognito window.
Browser* incognito_browser = CreateIncognitoBrowser();
// Run without an explicit "incognito" param.
function = base::MakeRefCounted<WindowsCreateFunction>();
function->set_extension(extension.get());
result = utils::ToDict(utils::RunFunctionAndReturnSingleResult(
function.get(), kEmptyArgs, incognito_browser->profile(),
api_test_utils::FunctionMode::kIncognito));
// Make sure it is a new(different) window.
EXPECT_NE(ExtensionTabUtil::GetWindowId(incognito_browser),
GetWindowId(result));
// ... and it is incognito.
EXPECT_TRUE(api_test_utils::GetBoolean(result, "incognito"));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest,
DontCreateNormalWindowWhenIncognitoForced) {
static const char kArgsWithExplicitIncognitoParam[] =
"[{\"url\": \"about:blank\", \"incognito\": false }]";
// Force Incognito mode.
IncognitoModePrefs::SetAvailability(
browser()->profile()->GetPrefs(),
policy::IncognitoModeAvailability::kForced);
// Run with an explicit "incognito" param.
auto function = base::MakeRefCounted<WindowsCreateFunction>();
scoped_refptr<const Extension> extension(ExtensionBuilder("Test").Build());
function->set_extension(extension.get());
EXPECT_TRUE(base::MatchPattern(
utils::RunFunctionAndReturnError(function.get(),
kArgsWithExplicitIncognitoParam,
browser()->profile()),
keys::kIncognitoModeIsForced));
// Now try opening a normal window from incognito window.
Browser* incognito_browser = CreateIncognitoBrowser();
// Run with an explicit "incognito" param.
function = base::MakeRefCounted<WindowsCreateFunction>();
function->set_extension(extension.get());
EXPECT_TRUE(base::MatchPattern(
utils::RunFunctionAndReturnError(function.get(),
kArgsWithExplicitIncognitoParam,
incognito_browser->profile()),
keys::kIncognitoModeIsForced));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest,
DontCreateIncognitoWindowWhenIncognitoDisabled) {
static const char kArgs[] =
"[{\"url\": \"about:blank\", \"incognito\": true }]";
Browser* incognito_browser = CreateIncognitoBrowser();
// Disable Incognito mode.
IncognitoModePrefs::SetAvailability(
browser()->profile()->GetPrefs(),
policy::IncognitoModeAvailability::kDisabled);
// Run in normal window.
auto function = base::MakeRefCounted<WindowsCreateFunction>();
scoped_refptr<const Extension> extension(ExtensionBuilder("Test").Build());
function->set_extension(extension.get());
EXPECT_TRUE(
base::MatchPattern(utils::RunFunctionAndReturnError(function.get(), kArgs,
browser()->profile()),
keys::kIncognitoModeIsDisabled));
// Run in incognito window.
function = base::MakeRefCounted<WindowsCreateFunction>();
function->set_extension(extension.get());
EXPECT_TRUE(base::MatchPattern(
utils::RunFunctionAndReturnError(function.get(), kArgs,
incognito_browser->profile()),
keys::kIncognitoModeIsDisabled));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, QueryCurrentWindowTabs) {
const size_t kExtraWindows = 3;
for (size_t i = 0; i < kExtraWindows; ++i)
CreateBrowser(browser()->profile());
GURL url(url::kAboutBlankURL);
ASSERT_TRUE(AddTabAtIndex(0, url, ui::PAGE_TRANSITION_LINK));
int window_id = ExtensionTabUtil::GetWindowId(browser());
// Get tabs in the 'current' window called from non-focused browser.
auto function = base::MakeRefCounted<TabsQueryFunction>();
function->set_extension(ExtensionBuilder("Test").Build().get());
base::Value::List result_tabs =
utils::ToList(RunFunctionWithDispatcherDelegateAndReturnValue(
function.get(), "[{\"currentWindow\":true}]", browser()));
// We should have one initial tab and one added tab.
EXPECT_EQ(2u, result_tabs.size());
for (const base::Value& result_tab : result_tabs) {
EXPECT_EQ(window_id, GetTabWindowId(utils::ToDict(result_tab)));
}
// Get tabs NOT in the 'current' window called from non-focused browser.
function = base::MakeRefCounted<TabsQueryFunction>();
function->set_extension(ExtensionBuilder("Test").Build().get());
result_tabs = utils::ToList(RunFunctionWithDispatcherDelegateAndReturnValue(
function.get(), "[{\"currentWindow\":false}]", browser()));
// We should have one tab for each extra window.
EXPECT_EQ(kExtraWindows, result_tabs.size());
for (const base::Value& result_tab : result_tabs) {
EXPECT_NE(window_id, GetTabWindowId(utils::ToDict(result_tab)));
}
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, QueryAllTabsWithDevTools) {
const size_t kNumWindows = 3;
std::set<int> window_ids;
window_ids.insert(ExtensionTabUtil::GetWindowId(browser()));
for (size_t i = 0; i < kNumWindows - 1; ++i) {
Browser* new_browser = CreateBrowser(browser()->profile());
window_ids.insert(ExtensionTabUtil::GetWindowId(new_browser));
}
// Undocked DevTools window should not be accessible.
DevToolsWindow* devtools = DevToolsWindowTesting::OpenDevToolsWindowSync(
browser()->tab_strip_model()->GetWebContentsAt(0), false /* is_docked */);
// Get tabs in the 'current' window called from non-focused browser.
auto function = base::MakeRefCounted<TabsQueryFunction>();
function->set_extension(ExtensionBuilder("Test").Build().get());
base::Value::List result_tabs(
utils::ToList(utils::RunFunctionAndReturnSingleResult(
function.get(), "[{}]", browser()->profile())));
std::set<int> result_ids;
// We should have one tab per browser except for DevTools.
EXPECT_EQ(kNumWindows, result_tabs.size());
for (const base::Value& result_tab : result_tabs) {
result_ids.insert(GetTabWindowId(utils::ToDict(result_tab)));
}
EXPECT_EQ(window_ids, result_ids);
DevToolsWindowTesting::CloseDevToolsWindowSync(devtools);
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, QueryTabGroups) {
ASSERT_TRUE(browser()->tab_strip_model()->SupportsTabGroups());
GURL url(url::kAboutBlankURL);
ASSERT_TRUE(AddTabAtIndex(0, url, ui::PAGE_TRANSITION_LINK));
ASSERT_TRUE(AddTabAtIndex(0, url, ui::PAGE_TRANSITION_LINK));
ASSERT_TRUE(AddTabAtIndex(0, url, ui::PAGE_TRANSITION_LINK));
tab_groups::TabGroupId group_id =
browser()->tab_strip_model()->AddToNewGroup({0, 1});
auto function = base::MakeRefCounted<TabsQueryFunction>();
function->set_extension(ExtensionBuilder("Test").Build().get());
constexpr char kFormatQueryArgs[] = R"([{"groupId":%d}])";
const std::string args = base::StringPrintf(
kFormatQueryArgs, ExtensionTabUtil::GetGroupId(group_id));
base::Value::List result(
utils::ToList(utils::RunFunctionAndReturnSingleResult(
function.get(), args, browser()->profile())));
EXPECT_EQ(2u, result.size());
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DontCreateTabInClosingPopupWindow) {
// Test creates new popup window, closes it right away and then tries to open
// a new tab in it. Tab should not be opened in the popup window, but in a
// tabbed browser window.
Browser* popup_browser = Browser::Create(
Browser::CreateParams(Browser::TYPE_POPUP, browser()->profile(), true));
int window_id = ExtensionTabUtil::GetWindowId(popup_browser);
chrome::CloseWindow(popup_browser);
auto create_tab_function = base::MakeRefCounted<TabsCreateFunction>();
create_tab_function->set_extension(ExtensionBuilder("Test").Build().get());
// Without a callback the function will not generate a result.
create_tab_function->set_has_callback(true);
static const char kNewBlankTabArgs[] =
"[{\"url\": \"about:blank\", \"windowId\": %u}]";
const base::Value::Dict result =
utils::ToDict(utils::RunFunctionAndReturnSingleResult(
create_tab_function.get(),
base::StringPrintf(kNewBlankTabArgs, window_id),
browser()->profile()));
EXPECT_NE(window_id, GetTabWindowId(result));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, InvalidUpdateWindowState) {
int window_id = ExtensionTabUtil::GetWindowId(browser());
static const char kArgsMinimizedWithFocus[] =
"[%u, {\"state\": \"minimized\", \"focused\": true}]";
auto function = base::MakeRefCounted<WindowsUpdateFunction>();
scoped_refptr<const Extension> extension(ExtensionBuilder("Test").Build());
function->set_extension(extension.get());
EXPECT_TRUE(base::MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf(kArgsMinimizedWithFocus, window_id),
browser()->profile()),
keys::kInvalidWindowStateError));
static const char kArgsMaximizedWithoutFocus[] =
"[%u, {\"state\": \"maximized\", \"focused\": false}]";
function = base::MakeRefCounted<WindowsUpdateFunction>();
function->set_extension(extension.get());
EXPECT_TRUE(base::MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf(kArgsMaximizedWithoutFocus, window_id),
browser()->profile()),
keys::kInvalidWindowStateError));
static const char kArgsMinimizedWithBounds[] =
"[%u, {\"state\": \"minimized\", \"width\": 500}]";
function = base::MakeRefCounted<WindowsUpdateFunction>();
function->set_extension(extension.get());
EXPECT_TRUE(base::MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf(kArgsMinimizedWithBounds, window_id),
browser()->profile()),
keys::kInvalidWindowStateError));
static const char kArgsMaximizedWithBounds[] =
"[%u, {\"state\": \"maximized\", \"width\": 500}]";
function = base::MakeRefCounted<WindowsUpdateFunction>();
function->set_extension(extension.get());
EXPECT_TRUE(base::MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf(kArgsMaximizedWithBounds, window_id),
browser()->profile()),
keys::kInvalidWindowStateError));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, InvalidUpdateWindowBounds) {
scoped_refptr<const Extension> extension(ExtensionBuilder("Test").Build());
// Get the display bounds so we can test whether the window intersects.
gfx::Rect displays;
for (const auto& display : display::Screen::GetScreen()->GetAllDisplays())
displays.Union(display.bounds());
int window_id = ExtensionTabUtil::GetWindowId(browser());
gfx::Rect window_bounds = browser()->window()->GetBounds();
static const char kArgsUpdateFunction[] = "[%u, {\"left\": %d, \"top\": %d}]";
// We use a small value to move the window outside or inside the bounds.
int window_offset = window_bounds.size().width() * 0.1;
{
// Window bounds that do not intersect with the display are not valid.
int window_left = displays.right() + window_offset;
int window_top = displays.bottom() + window_offset;
auto function = base::MakeRefCounted<WindowsUpdateFunction>();
function->set_extension(extension.get());
EXPECT_TRUE(base::MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf(kArgsUpdateFunction, window_id, window_left,
window_top),
browser()->profile()),
keys::kInvalidWindowBoundsError));
}
{
// Window bounds that intersect less than 50% with the display are not
// valid.
int window_left = displays.right() - window_offset;
int window_top = displays.bottom() - window_offset;
auto function = base::MakeRefCounted<WindowsUpdateFunction>();
function->set_extension(extension.get());
EXPECT_TRUE(base::MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf(kArgsUpdateFunction, window_id, window_left,
window_top),
browser()->profile()),
keys::kInvalidWindowBoundsError));
}
{
// Window bounds that intersect 50% or more with the display are valid.
int window_left = displays.right() - window_bounds.width() + window_offset;
int window_top = displays.bottom() - window_bounds.height() + window_offset;
auto function = base::MakeRefCounted<WindowsUpdateFunction>();
function->set_extension(extension.get());
EXPECT_TRUE(utils::RunFunction(
function.get(),
base::StringPrintf(kArgsUpdateFunction, window_id, window_left,
window_top),
browser()->profile(), api_test_utils::FunctionMode::kNone));
}
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, UpdateDevToolsWindow) {
DevToolsWindow* devtools = DevToolsWindowTesting::OpenDevToolsWindowSync(
browser()->tab_strip_model()->GetWebContentsAt(0), false /* is_docked */);
auto get_function = base::MakeRefCounted<WindowsGetFunction>();
scoped_refptr<const Extension> extension(
ExtensionBuilder("Test").Build().get());
get_function->set_extension(extension.get());
base::Value::Dict result =
utils::ToDict(utils::RunFunctionAndReturnSingleResult(
get_function.get(),
base::StringPrintf(
"[%u, {\"windowTypes\": [\"devtools\"]}]",
ExtensionTabUtil::GetWindowId(
DevToolsWindowTesting::Get(devtools)->browser())),
browser()->profile()));
// Verify the updating width/height works.
int32_t new_width = api_test_utils::GetInteger(result, "width") - 50;
int32_t new_height = api_test_utils::GetInteger(result, "height") - 50;
auto update_function = base::MakeRefCounted<WindowsUpdateFunction>();
result = utils::ToDict(utils::RunFunctionAndReturnSingleResult(
update_function.get(),
base::StringPrintf("[%u, {\"width\": %d, \"height\": %d}]",
ExtensionTabUtil::GetWindowId(
DevToolsWindowTesting::Get(devtools)->browser()),
new_width, new_height),
browser()->profile()));
EXPECT_EQ(new_width, api_test_utils::GetInteger(result, "width"));
EXPECT_EQ(new_height, api_test_utils::GetInteger(result, "height"));
DevToolsWindowTesting::CloseDevToolsWindowSync(devtools);
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, ExtensionAPICannotNavigateDevtools) {
scoped_refptr<const Extension> extension =
ExtensionBuilder("Test").AddAPIPermission("tabs").Build();
DevToolsWindow* devtools = DevToolsWindowTesting::OpenDevToolsWindowSync(
browser()->tab_strip_model()->GetWebContentsAt(0), false /* is_docked */);
auto function = base::MakeRefCounted<TabsUpdateFunction>();
function->set_extension(extension.get());
EXPECT_TRUE(base::MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf(
"[%d, {\"url\":\"http://example.com\"}]",
ExtensionTabUtil::GetTabId(
DevToolsWindowTesting::Get(devtools)->main_web_contents())),
DevToolsWindowTesting::Get(devtools)->browser()->profile()),
tabs_constants::kNotAllowedForDevToolsError));
DevToolsWindowTesting::CloseDevToolsWindowSync(devtools);
}
#if BUILDFLAG(IS_MAC)
// https://crbug.com/836327
#define MAYBE_AcceptState DISABLED_AcceptState
#else
#define MAYBE_AcceptState AcceptState
#endif
IN_PROC_BROWSER_TEST_F(ExtensionWindowCreateTest, MAYBE_AcceptState) {
#if BUILDFLAG(IS_MAC)
ui::test::ScopedFakeNSWindowFullscreen fake_fullscreen;
#endif
auto function = base::MakeRefCounted<WindowsCreateFunction>();
scoped_refptr<const Extension> extension(ExtensionBuilder("Test").Build());
function->set_extension(extension.get());
function->SetBrowserContextForTesting(browser()->profile());
base::Value::Dict result =
utils::ToDict(utils::RunFunctionAndReturnSingleResult(
function.get(), "[{\"state\": \"minimized\"}]", browser()->profile(),
api_test_utils::FunctionMode::kIncognito));
int window_id = GetWindowId(result);
std::string error;
WindowController* new_controller =
ExtensionTabUtil::GetControllerFromWindowID(
ChromeExtensionFunctionDetails(function.get()), window_id, &error);
ASSERT_TRUE(new_controller);
EXPECT_TRUE(error.empty());
Browser* new_browser = new_controller->GetBrowser();
ASSERT_TRUE(new_browser);
// TODO(crbug.com/40254339): Remove this workaround if this wait is no longer
// needed.
// These builds flags are limiting the check for IsMinimized() for Linux.
// For Linux, we only check X11 window manager and not Wayland since our
// current fix only applies to X11.
#if BUILDFLAG(IS_LINUX)
// Must be checked inside IS_LINUX to compile on windows/mac.
#if BUILDFLAG(IS_OZONE_X11)
// DesktopWindowTreeHostX11::IsMinimized() relies on an asynchronous update
// from the window server
views::test::PropertyWaiter minimize_waiter(
base::BindRepeating(&BrowserWindow::IsMinimized,
base::Unretained(new_browser->window())),
true);
EXPECT_TRUE(minimize_waiter.Wait());
#elif BUILDFLAG(IS_OZONE_WAYLAND)
// TODO(crbug.com/40252593): Find a fix/workaround for wayland and add
// verification of IsMinimized() for as well.
#endif
#else
EXPECT_TRUE(new_controller->window()->IsMinimized());
#endif // BUILDFLAG(IS_LINUX)
function = base::MakeRefCounted<WindowsCreateFunction>();
function->set_extension(extension.get());
function->SetBrowserContextForTesting(browser()->profile());
result = utils::ToDict(utils::RunFunctionAndReturnSingleResult(
function.get(), "[{\"state\": \"fullscreen\"}]", browser()->profile(),
api_test_utils::FunctionMode::kIncognito));
window_id = GetWindowId(result);
new_controller = ExtensionTabUtil::GetControllerFromWindowID(
ChromeExtensionFunctionDetails(function.get()), window_id, &error);
ASSERT_TRUE(new_controller);
EXPECT_TRUE(error.empty());
EXPECT_TRUE(new_controller->GetBrowser()->window()->IsFullscreen());
// Let the message loop run so that |fake_fullscreen| finishes transition.
content::RunAllPendingInMessageLoop();
}
IN_PROC_BROWSER_TEST_F(ExtensionWindowCreateTest, ValidateCreateWindowState) {
EXPECT_TRUE(
base::MatchPattern(RunCreateWindowExpectError(
"[{\"state\": \"minimized\", \"focused\": true}]"),
keys::kInvalidWindowStateError));
EXPECT_TRUE(base::MatchPattern(
RunCreateWindowExpectError(
"[{\"state\": \"maximized\", \"focused\": false}]"),
keys::kInvalidWindowStateError));
EXPECT_TRUE(base::MatchPattern(
RunCreateWindowExpectError(
"[{\"state\": \"fullscreen\", \"focused\": false}]"),
keys::kInvalidWindowStateError));
EXPECT_TRUE(
base::MatchPattern(RunCreateWindowExpectError(
"[{\"state\": \"minimized\", \"width\": 500}]"),
keys::kInvalidWindowStateError));
EXPECT_TRUE(
base::MatchPattern(RunCreateWindowExpectError(
"[{\"state\": \"maximized\", \"width\": 500}]"),
keys::kInvalidWindowStateError));
EXPECT_TRUE(
base::MatchPattern(RunCreateWindowExpectError(
"[{\"state\": \"fullscreen\", \"width\": 500}]"),
keys::kInvalidWindowStateError));
}
IN_PROC_BROWSER_TEST_F(ExtensionWindowCreateTest, ValidateCreateWindowBounds) {
// Get the display bounds so we can test whether the window intersects.
gfx::Rect displays;
for (const auto& display : display::Screen::GetScreen()->GetAllDisplays())
displays.Union(display.bounds());
static const char kArgsCreateFunction[] =
"[{\"left\": %d, \"top\": %d, \"width\": %d, \"height\": %d }]";
int window_width = 100;
int window_height = 100;
// We use a small value to move the window outside or inside the bounds.
int window_offset = 10;
{
// Window bounds that do not intersect with the display are not valid.
int window_left = displays.right() + window_offset;
int window_top = displays.bottom() + window_offset;
EXPECT_TRUE(
base::MatchPattern(RunCreateWindowExpectError(base::StringPrintf(
kArgsCreateFunction, window_left, window_top,
window_width, window_height)),
keys::kInvalidWindowBoundsError));
}
{
// Window bounds that intersect less than 50% with the display are not
// valid.
int window_left = displays.right() - window_offset;
int window_top = displays.bottom() - window_offset;
EXPECT_TRUE(
base::MatchPattern(RunCreateWindowExpectError(base::StringPrintf(
kArgsCreateFunction, window_left, window_top,
window_width, window_height)),
keys::kInvalidWindowBoundsError));
}
{
// Window bounds that intersect 50% or more with the display are valid.
int window_left = displays.right() - window_width + window_offset;
int window_top = displays.bottom() - window_height + window_offset;
auto function = base::MakeRefCounted<WindowsCreateFunction>();
function->set_extension(ExtensionBuilder("Test").Build().get());
EXPECT_TRUE(utils::RunFunction(
function.get(),
base::StringPrintf(kArgsCreateFunction, window_left, window_top,
window_width, window_height),
browser()->profile(), api_test_utils::FunctionMode::kNone));
}
{
// Window bounds that specify size and not position should be adjusted
// to the screen in case the window is not visible.
// For this, update the current window bounds so the new window position
// needs to be adjusted to fit.
gfx::Rect current_window_bounds = browser()->window()->GetBounds();
current_window_bounds.set_x(current_window_bounds.x() +
current_window_bounds.width() - window_offset);
current_window_bounds.set_y(current_window_bounds.y() +
current_window_bounds.height() - window_offset);
browser()->window()->SetBounds(current_window_bounds);
static const char kArgsCreateFunctionOnlySize[] =
"[{\"width\": %d, \"height\": %d }]";
auto function = base::MakeRefCounted<WindowsCreateFunction>();
function->set_extension(ExtensionBuilder("Test").Build().get());
EXPECT_TRUE(utils::RunFunction(
function.get(),
base::StringPrintf(kArgsCreateFunctionOnlySize, window_width,
window_height),
browser()->profile(), api_test_utils::FunctionMode::kNone));
}
}
IN_PROC_BROWSER_TEST_F(ExtensionWindowCreateTest, CreatePopupWindowFromWebUI) {
auto function = base::MakeRefCounted<WindowsCreateFunction>();
function->SetBrowserContextForTesting(browser()->profile());
function->set_source_context_type(mojom::ContextType::kUntrustedWebUi);
const base::Value::Dict result =
utils::ToDict(utils::RunFunctionAndReturnSingleResult(
function.get(), R"([{"type": "popup"}])", browser()->profile()));
int window_id = GetWindowId(result);
std::string error;
EXPECT_TRUE(ExtensionTabUtil::GetControllerFromWindowID(
ChromeExtensionFunctionDetails(function.get()), window_id, &error));
EXPECT_TRUE(error.empty());
}
struct ExtensionWindowCreateIwaParam {
std::string test_name;
bool want_success;
std::string args;
};
// Test that `windows.create` functions correctly for Isolated Web Apps.
class ExtensionWindowCreateIwaTest
: public InProcessBrowserTest,
public testing::WithParamInterface<ExtensionWindowCreateIwaParam> {
public:
ExtensionWindowCreateIwaTest() {
scoped_feature_list_.InitAndEnableFeature(features::kIsolatedWebApps);
set_open_about_blank_on_browser_launch(false);
}
void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
web_app::test::WaitUntilReady(
web_app::WebAppProvider::GetForTest(profile()));
ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
}
void SetUpDefaultCommandLine(base::CommandLine* command_line) override {
InProcessBrowserTest::SetUpDefaultCommandLine(command_line);
// Suppress "Welcome to Google Chrome" window
command_line->AppendSwitch(switches::kNoFirstRun);
command_line->AppendSwitch(switches::kNoStartupWindow);
command_line->AppendSwitch(switches::kKeepAliveForTest);
}
void TearDownOnMainThread() override {
if (BrowserList::GetInstance()->empty()) {
// Tests crash during teardown if no browser has opened combined with the
// command line switches above. Open a browser to avoid the crash.
CreateBrowser(profile());
}
InProcessBrowserTest::TearDownOnMainThread();
}
Profile* profile() {
// We cannot use `browser()->profile()` here, because `browser()` is
// `nullptr` due to the command line switches above.
return profile_util::GetLastUsedProfile();
}
protected:
web_app::IsolatedWebAppUrlInfo InstallAndTrustBundle() {
auto bundle = web_app::IsolatedWebAppBuilder(web_app::ManifestBuilder())
.AddHtml("/", "Hello extensions!")
.BuildBundle(web_app::test::GetDefaultEd25519KeyPair());
return bundle->InstallChecked(profile());
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
web_app::OsIntegrationManager::ScopedSuppressForTesting os_hooks_suppress_;
base::ScopedTempDir scoped_temp_dir_;
};
IN_PROC_BROWSER_TEST_P(ExtensionWindowCreateIwaTest, CreateWindowForIwa) {
auto url_info = InstallAndTrustBundle();
EXPECT_EQ(BrowserList::GetInstance()->size(), 0ul);
scoped_refptr<const Extension> extension =
ExtensionBuilder("ExtensionWindowCreateIwaTest").Build();
auto function = base::MakeRefCounted<WindowsCreateFunction>();
function->set_extension(extension);
bool result =
api_test_utils::RunFunction(function.get(), GetParam().args, profile(),
api_test_utils::FunctionMode::kNone);
if (GetParam().want_success) {
EXPECT_TRUE(result) << function->GetError();
// A single browser for the IWA should now be open.
ASSERT_EQ(BrowserList::GetInstance()->size(), 1ul);
Browser* iwa_browser = *BrowserList::GetInstance()->begin();
ASSERT_EQ(iwa_browser->tab_strip_model()->count(), 1);
EXPECT_EQ(iwa_browser->tab_strip_model()->GetWebContentsAt(0)->GetURL(),
url_info.origin().GetURL().Resolve("/index.html"));
} else {
EXPECT_FALSE(result);
// No browser should have opened.
EXPECT_EQ(BrowserList::GetInstance()->size(), 0ul);
}
}
INSTANTIATE_TEST_SUITE_P(
/* no prefix */,
ExtensionWindowCreateIwaTest,
testing::Values(
ExtensionWindowCreateIwaParam{.test_name = "iwa_and_https",
.want_success = false,
.args = R"([{
"url": [
"isolated-app://4tkrnsmftl4ggvvdkfth3piainqragus2qbhf7rlz2a3wo3rh4wqaaic/index.html",
"https://example.com"
]
}])"},
ExtensionWindowCreateIwaParam{.test_name = "https_and_iwa_and_https",
.want_success = false,
.args = R"([{
"url": [
"https://example.com",
"isolated-app://4tkrnsmftl4ggvvdkfth3piainqragus2qbhf7rlz2a3wo3rh4wqaaic/index.html",
"https://example.com"
]
}])"},
// If we ever support tabbed IWAs, then this test must be updated to
// `.want_success true`.
ExtensionWindowCreateIwaParam{.test_name = "iwa_and_iwa",
.want_success = false,
.args = R"([{
"url": [
"isolated-app://4tkrnsmftl4ggvvdkfth3piainqragus2qbhf7rlz2a3wo3rh4wqaaic/index.html",
"isolated-app://4tkrnsmftl4ggvvdkfth3piainqragus2qbhf7rlz2a3wo3rh4wqaaic/index.html"
]
}])"},
ExtensionWindowCreateIwaParam{.test_name = "iwa_and_different_iwa",
.want_success = false,
.args = R"([{
"url": [
"isolated-app://4tkrnsmftl4ggvvdkfth3piainqragus2qbhf7rlz2a3wo3rh4wqaaic/index.html",
"isolated-app://5dp4lo5h6tpc4vuokowxmlqs5gpbainu2nqvuddccx5mqsnje7fqaaic/index.html"
]
}])"},
ExtensionWindowCreateIwaParam{.test_name = "invalid_iwa_url",
.want_success = false,
.args = R"([{
"url": [
"isolated-app://invalid-iwa-url"
]
}])"},
// If we ever support tabbed IWAs, this test must be updated: If `tabId`
// refers to the tab of the same IWA origin that is specified in `url`,
// it should be allowed.
ExtensionWindowCreateIwaParam{.test_name = "iwa_and_tab_id",
.want_success = false,
.args = R"([{
"url":
"isolated-app://4tkrnsmftl4ggvvdkfth3piainqragus2qbhf7rlz2a3wo3rh4wqaaic/index.html",
"tabId": 1
}])"},
ExtensionWindowCreateIwaParam{.test_name = "iwa",
.want_success = true,
.args = R"([{
"url": "isolated-app://4tkrnsmftl4ggvvdkfth3piainqragus2qbhf7rlz2a3wo3rh4wqaaic/index.html",
}])"}),
[](const testing::TestParamInfo<ExtensionWindowCreateIwaTest::ParamType>&
info) { return info.param.test_name; });
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DuplicateTab) {
content::OpenURLParams params(GURL(url::kAboutBlankURL), content::Referrer(),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui::PAGE_TRANSITION_LINK, false);
content::WebContents* web_contents =
browser()->OpenURL(params, /*navigation_handle_callback=*/{});
int tab_id = ExtensionTabUtil::GetTabId(web_contents);
int window_id = ExtensionTabUtil::GetWindowIdOfTab(web_contents);
int tab_index = -1;
TabStripModel* tab_strip;
ExtensionTabUtil::GetTabStripModel(web_contents, &tab_strip, &tab_index);
auto duplicate_tab_function = base::MakeRefCounted<TabsDuplicateFunction>();
scoped_refptr<const Extension> empty_tab_extension =
ExtensionBuilder("Test").AddAPIPermission("tabs").Build();
duplicate_tab_function->set_extension(empty_tab_extension.get());
duplicate_tab_function->set_has_callback(true);
const base::Value::Dict duplicate_result =
utils::ToDict(utils::RunFunctionAndReturnSingleResult(
duplicate_tab_function.get(), base::StringPrintf("[%u]", tab_id),
browser()->profile()));
int duplicate_tab_id = GetTabId(duplicate_result);
int duplicate_tab_window_id = GetTabWindowId(duplicate_result);
int duplicate_tab_index =
api_test_utils::GetInteger(duplicate_result, "index");
// Duplicate tab id should be different from the original tab id.
EXPECT_NE(tab_id, duplicate_tab_id);
EXPECT_EQ(window_id, duplicate_tab_window_id);
EXPECT_EQ(tab_index + 1, duplicate_tab_index);
// The test empty tab extension has tabs permissions, therefore
// |duplicate_result| should contain url, pendingUrl, title or faviconUrl
// in the function result.
EXPECT_TRUE(HasAnyPrivacySensitiveFields(duplicate_result));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DuplicateTabNoPermission) {
content::OpenURLParams params(GURL(url::kAboutBlankURL), content::Referrer(),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui::PAGE_TRANSITION_LINK, false);
content::WebContents* web_contents =
browser()->OpenURL(params, /*navigation_handle_callback=*/{});
int tab_id = ExtensionTabUtil::GetTabId(web_contents);
int window_id = ExtensionTabUtil::GetWindowIdOfTab(web_contents);
int tab_index = -1;
TabStripModel* tab_strip;
ExtensionTabUtil::GetTabStripModel(web_contents, &tab_strip, &tab_index);
auto duplicate_tab_function = base::MakeRefCounted<TabsDuplicateFunction>();
scoped_refptr<const Extension> empty_extension(
ExtensionBuilder("Test").Build());
duplicate_tab_function->set_extension(empty_extension.get());
duplicate_tab_function->set_has_callback(true);
const base::Value::Dict duplicate_result =
utils::ToDict(utils::RunFunctionAndReturnSingleResult(
duplicate_tab_function.get(), base::StringPrintf("[%u]", tab_id),
browser()->profile()));
int duplicate_tab_id = GetTabId(duplicate_result);
int duplicate_tab_window_id = GetTabWindowId(duplicate_result);
int duplicate_tab_index =
api_test_utils::GetInteger(duplicate_result, "index");
// Duplicate tab id should be different from the original tab id.
EXPECT_NE(tab_id, duplicate_tab_id);
EXPECT_EQ(window_id, duplicate_tab_window_id);
EXPECT_EQ(tab_index + 1, duplicate_tab_index);
// The test empty extension has no permissions, therefore |duplicate_result|
// should not contain url, pendingUrl, title and faviconUrl in the function
// result.
EXPECT_FALSE(HasAnyPrivacySensitiveFields(duplicate_result));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, NoTabsEventOnDevTools) {
extensions::ResultCatcher catcher;
ExtensionTestMessageListener listener("ready", ReplyBehavior::kWillReply);
ASSERT_TRUE(
LoadExtension(test_data_dir_.AppendASCII("api_test/tabs/no_events")));
ASSERT_TRUE(listener.WaitUntilSatisfied());
DevToolsWindow* devtools = DevToolsWindowTesting::OpenDevToolsWindowSync(
browser()->tab_strip_model()->GetWebContentsAt(0), false /* is_docked */);
listener.Reply("stop");
ASSERT_TRUE(catcher.GetNextResult());
DevToolsWindowTesting::CloseDevToolsWindowSync(devtools);
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, NoTabsAppWindow) {
extensions::ResultCatcher catcher;
ExtensionTestMessageListener listener("ready", ReplyBehavior::kWillReply);
ASSERT_TRUE(
LoadExtension(test_data_dir_.AppendASCII("api_test/tabs/no_events")));
ASSERT_TRUE(listener.WaitUntilSatisfied());
AppWindow* app_window = CreateTestAppWindow(
"{\"outerBounds\": "
"{\"width\": 300, \"height\": 300,"
" \"minWidth\": 200, \"minHeight\": 200,"
" \"maxWidth\": 400, \"maxHeight\": 400}}");
listener.Reply("stop");
ASSERT_TRUE(catcher.GetNextResult());
CloseAppWindow(app_window);
}
// Crashes on Mac/Win only. http://crbug.com/708996
#if BUILDFLAG(IS_MAC)
#define MAYBE_FilteredEvents DISABLED_FilteredEvents
#else
#define MAYBE_FilteredEvents FilteredEvents
#endif
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, MAYBE_FilteredEvents) {
extensions::ResultCatcher catcher;
ExtensionTestMessageListener listener("ready", ReplyBehavior::kWillReply);
ASSERT_TRUE(
LoadExtension(test_data_dir_.AppendASCII("api_test/windows/events")));
ASSERT_TRUE(listener.WaitUntilSatisfied());
AppWindow* app_window = CreateTestAppWindow(
"{\"outerBounds\": "
"{\"width\": 300, \"height\": 300,"
" \"minWidth\": 200, \"minHeight\": 200,"
" \"maxWidth\": 400, \"maxHeight\": 400}}");
Browser* browser_window =
Browser::Create(Browser::CreateParams(browser()->profile(), true));
AddBlankTabAndShow(browser_window);
DevToolsWindow* devtools_window =
DevToolsWindowTesting::OpenDevToolsWindowSync(
browser()->tab_strip_model()->GetWebContentsAt(0),
false /* is_docked */);
chrome::CloseWindow(browser_window);
DevToolsWindowTesting::CloseDevToolsWindowSync(devtools_window);
CloseAppWindow(app_window);
// TODO(llandwerlin): It seems creating an app window on MacOSX
// won't create an activation event whereas it does on all other
// platform. Disable focus event tests for now.
#if BUILDFLAG(IS_MAC)
listener.Reply("");
#else
listener.Reply("focus");
#endif
ASSERT_TRUE(catcher.GetNextResult());
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, OnBoundsChanged) {
extensions::ResultCatcher catcher;
ExtensionTestMessageListener listener("ready", ReplyBehavior::kWillReply);
ASSERT_TRUE(
LoadExtension(test_data_dir_.AppendASCII("api_test/windows/bounds")));
ASSERT_TRUE(listener.WaitUntilSatisfied());
gfx::Rect rect = browser()->window()->GetBounds();
rect.Inset(10);
browser()->window()->SetBounds(rect);
listener.Reply(base::StringPrintf(
R"({"top": %u, "left": %u, "width": %u, "height": %u})", rect.y(),
rect.x(), rect.width(), rect.height()));
ASSERT_TRUE(catcher.GetNextResult());
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, WindowsCreate) {
ASSERT_TRUE(RunExtensionTest("api_test/windows/create")) << message_;
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, ExecuteScriptOnDevTools) {
scoped_refptr<const Extension> extension =
ExtensionBuilder("Test").AddAPIPermission("tabs").Build();
DevToolsWindow* devtools = DevToolsWindowTesting::OpenDevToolsWindowSync(
browser()->tab_strip_model()->GetWebContentsAt(0), false /* is_docked */);
auto function = base::MakeRefCounted<TabsExecuteScriptFunction>();
function->set_extension(extension.get());
EXPECT_TRUE(base::MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf("[%u, {\"code\": \"true\"}]",
api::windows::WINDOW_ID_CURRENT),
DevToolsWindowTesting::Get(devtools)->browser()->profile()),
manifest_errors::kCannotAccessPageWithUrl));
DevToolsWindowTesting::CloseDevToolsWindowSync(devtools);
}
// TODO(georgesak): change this browsertest to an unittest.
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DiscardedProperty) {
ASSERT_TRUE(g_browser_process && g_browser_process->GetTabManager());
resource_coordinator::TabManager* tab_manager =
g_browser_process->GetTabManager();
// To avoid flakes when focus changes, set the active tab strip model
// explicitly.
resource_coordinator::GetTabLifecycleUnitSource()
->SetFocusedTabStripModelForTesting(browser()->tab_strip_model());
// Create two aditional tabs.
content::OpenURLParams params(GURL(url::kAboutBlankURL), content::Referrer(),
WindowOpenDisposition::NEW_BACKGROUND_TAB,
ui::PAGE_TRANSITION_LINK, false);
content::WebContents* web_contents_a =
browser()->OpenURL(params, /*navigation_handle_callback=*/{});
content::WebContents* web_contents_b =
browser()->OpenURL(params, /*navigation_handle_callback=*/{});
// Set up query function with an extension.
scoped_refptr<const Extension> extension = ExtensionBuilder("Test").Build();
auto RunQueryFunction = [this, &extension](const char* query_info) {
auto function = base::MakeRefCounted<TabsQueryFunction>();
function->set_extension(extension.get());
return utils::ToList(utils::RunFunctionAndReturnSingleResult(
function.get(), query_info, browser()->profile()));
};
// Get non-discarded tabs.
{
base::Value::List result(RunQueryFunction("[{\"discarded\": false}]"));
// The two created plus the default tab.
EXPECT_EQ(3u, result.size());
}
// Get discarded tabs.
{
base::Value::List result(RunQueryFunction("[{\"discarded\": true}]"));
EXPECT_EQ(0u, result.size());
}
TabStripModel* tab_strip_model = browser()->tab_strip_model();
// Creates Tab object to ensure the property is correct for the extension.
api::tabs::Tab tab_object_a = ExtensionTabUtil::CreateTabObject(
web_contents_a, kDontScrubBehavior, nullptr, tab_strip_model, 0);
EXPECT_FALSE(tab_object_a.discarded);
// Discards one tab.
EXPECT_TRUE(tab_manager->DiscardTabByExtension(web_contents_a));
web_contents_a = tab_strip_model->GetWebContentsAt(1);
// Make sure the property is changed accordingly after discarding the tab.
tab_object_a = ExtensionTabUtil::CreateTabObject(
web_contents_a, kDontScrubBehavior, nullptr, tab_strip_model, 0);
EXPECT_TRUE(tab_object_a.discarded);
// Get non-discarded tabs after discarding one tab.
{
base::Value::List result(RunQueryFunction("[{\"discarded\": false}]"));
EXPECT_EQ(2u, result.size());
}
// Get discarded tabs after discarding one tab.
{
base::Value::List result(RunQueryFunction("[{\"discarded\": true}]"));
EXPECT_EQ(1u, result.size());
// Make sure the returned tab is the correct one.
int tab_id_a = ExtensionTabUtil::GetTabId(web_contents_a);
ASSERT_TRUE(result[0].is_dict());
std::optional<int> id = result[0].GetDict().FindInt(extension_misc::kId);
ASSERT_TRUE(id);
EXPECT_EQ(tab_id_a, *id);
}
// Discards another created tab.
EXPECT_TRUE(tab_manager->DiscardTabByExtension(web_contents_b));
// Get non-discarded tabs after discarding two created tabs.
{
base::Value::List result(RunQueryFunction("[{\"discarded\": false}]"));
ASSERT_EQ(1u, result.size());
// Make sure the returned tab is the correct one.
int tab_id_c =
ExtensionTabUtil::GetTabId(tab_strip_model->GetWebContentsAt(0));
ASSERT_TRUE(result[0].is_dict());
std::optional<int> id = result[0].GetDict().FindInt(extension_misc::kId);
ASSERT_TRUE(id);
EXPECT_EQ(tab_id_c, *id);
}
// Get discarded tabs after discarding two created tabs.
{
base::Value::List result(RunQueryFunction("[{\"discarded\": true}]"));
EXPECT_EQ(2u, result.size());
}
// Activates the first created tab.
tab_strip_model->ActivateTabAt(1);
// Get non-discarded tabs after activating a discarded tab.
{
base::Value::List result(RunQueryFunction("[{\"discarded\": false}]"));
EXPECT_EQ(2u, result.size());
}
// Get discarded tabs after activating a discarded tab.
{
base::Value::List result(RunQueryFunction("[{\"discarded\": true}]"));
EXPECT_EQ(1u, result.size());
}
}
// Tests chrome.tabs.discard(tabId).
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DiscardWithId) {
// Create an additional tab.
ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(url::kAboutBlankURL),
WindowOpenDisposition::NEW_BACKGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
content::WebContents* web_contents =
browser()->tab_strip_model()->GetWebContentsAt(1);
// Set up the function with an extension.
scoped_refptr<const Extension> extension = ExtensionBuilder("Test").Build();
auto discard = base::MakeRefCounted<TabsDiscardFunction>();
discard->set_extension(extension.get());
// Run function passing the tab id as argument.
int tab_id = ExtensionTabUtil::GetTabId(web_contents);
const base::Value::Dict result =
utils::ToDict(utils::RunFunctionAndReturnSingleResult(
discard.get(), base::StringPrintf("[%u]", tab_id),
browser()->profile()));
// Confirms that TabManager sees the tab as discarded.
web_contents = browser()->tab_strip_model()->GetWebContentsAt(1);
EXPECT_EQ(resource_coordinator::TabLifecycleUnitExternal::FromWebContents(
web_contents)
->GetTabState(),
::mojom::LifecycleUnitState::DISCARDED);
// Make sure the returned tab is the one discarded and its discarded state is
// correct.
tab_id = ExtensionTabUtil::GetTabId(web_contents);
EXPECT_EQ(tab_id, api_test_utils::GetInteger(result, "id"));
EXPECT_TRUE(api_test_utils::GetBoolean(result, "discarded"));
// The result should be scrubbed.
EXPECT_FALSE(result.contains("url"));
// Tests chrome.tabs.discard(tabId) with an already discarded tab. It has to
// return the error stating that the tab couldn't be discarded.
auto discarded = base::MakeRefCounted<TabsDiscardFunction>();
discarded->set_extension(extension.get());
std::string error = utils::RunFunctionAndReturnError(
discarded.get(), base::StringPrintf("[%u]", tab_id),
browser()->profile());
EXPECT_TRUE(base::MatchPattern(error, keys::kCannotDiscardTab));
}
// Tests chrome.tabs.discard(invalidId).
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DiscardWithInvalidId) {
// Create an additional tab.
ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(url::kAboutBlankURL),
WindowOpenDisposition::NEW_BACKGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
// Set up the function with an extension.
scoped_refptr<const Extension> extension = ExtensionBuilder("Test").Build();
auto discard = base::MakeRefCounted<TabsDiscardFunction>();
discard->set_extension(extension.get());
// Run function passing an invalid id as argument.
int tab_invalid_id = ExtensionTabUtil::GetTabId(
browser()->tab_strip_model()->GetWebContentsAt(0));
tab_invalid_id = std::max(
tab_invalid_id, ExtensionTabUtil::GetTabId(
browser()->tab_strip_model()->GetWebContentsAt(1)));
tab_invalid_id++;
std::string error = utils::RunFunctionAndReturnError(
discard.get(), base::StringPrintf("[%u]", tab_invalid_id),
browser()->profile());
// State should still be `ACTIVE` as no tab was discarded.
EXPECT_EQ(resource_coordinator::TabLifecycleUnitExternal::FromWebContents(
browser()->tab_strip_model()->GetWebContentsAt(1))
->GetTabState(),
::mojom::LifecycleUnitState::ACTIVE);
// Check error message.
EXPECT_TRUE(base::MatchPattern(error, ExtensionTabUtil::kTabNotFoundError));
}
// Tests chrome.tabs.discard().
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, DiscardWithoutId) {
// Create an additional tab.
ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(url::kAboutBlankURL),
WindowOpenDisposition::NEW_BACKGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
content::WebContents* web_contents =
browser()->tab_strip_model()->GetWebContentsAt(1);
// Set up the function with an extension.
scoped_refptr<const Extension> extension = ExtensionBuilder("Test").Build();
auto discard = base::MakeRefCounted<TabsDiscardFunction>();
discard->set_extension(extension.get());
// Run without passing an id.
const base::Value::Dict result =
utils::ToDict(utils::RunFunctionAndReturnSingleResult(
discard.get(), "[]", browser()->profile()));
// Confirms that TabManager sees the tab as discarded.
web_contents = browser()->tab_strip_model()->GetWebContentsAt(1);
EXPECT_EQ(resource_coordinator::TabLifecycleUnitExternal::FromWebContents(
web_contents)
->GetTabState(),
::mojom::LifecycleUnitState::DISCARDED);
// Make sure the returned tab is the one discarded and its discarded state is
// correct.
EXPECT_EQ(ExtensionTabUtil::GetTabId(web_contents),
api_test_utils::GetInteger(result, "id"));
EXPECT_TRUE(api_test_utils::GetBoolean(result, "discarded"));
// The result should be scrubbed.
EXPECT_FALSE(result.contains("url"));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, TestGroupDetachedAndReInserted) {
// This implicitly creates the `TabsEventRouter`, which is required to get a
// tab update event.
TabsWindowsAPI::Get(profile())->tabs_event_router();
chrome::AddTabAt(browser(), GURL(), -1, true);
chrome::AddTabAt(browser(), GURL(), -1, true);
chrome::AddTabAt(browser(), GURL(), -1, true);
tab_groups::TabGroupId group =
browser()->tab_strip_model()->AddToNewGroup({0, 1});
TestEventRouterObserver event_observer(
EventRouter::Get(browser()->profile()));
std::unique_ptr<DetachedTabCollection> detached_group =
browser()->tab_strip_model()->DetachTabGroupForInsertion(group);
event_observer.WaitForEventWithName(api::tabs::OnUpdated::kEventName);
EXPECT_TRUE(base::Contains(event_observer.events(),
api::tabs::OnUpdated::kEventName));
event_observer.ClearEvents();
browser()->tab_strip_model()->InsertDetachedTabGroupAt(
std::move(detached_group), 1);
// Group added as well as the tab's group changed event should be sent.
event_observer.WaitForEventWithName(api::tabs::OnUpdated::kEventName);
EXPECT_TRUE(base::Contains(event_observer.events(),
api::tabs::OnUpdated::kEventName));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, Freezing) {
// Create a background tab.
ui_test_utils::NavigateToURLWithDisposition(
browser(), GURL(url::kAboutBlankURL),
WindowOpenDisposition::NEW_BACKGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
content::WebContents* web_contents =
browser()->tab_strip_model()->GetWebContentsAt(1);
// Create an extension.
scoped_refptr<const Extension> extension = ExtensionBuilder("Test").Build();
// Query all tabs. Their "frozen" property should be false.
{
auto query_function = base::MakeRefCounted<TabsQueryFunction>();
query_function->set_extension(extension.get());
base::Value::List result(
utils::ToList(utils::RunFunctionAndReturnSingleResult(
query_function.get(), "[{}]", browser()->profile())));
ASSERT_EQ(2u, result.size());
for (auto& element : result) {
base::Value::Dict dict = utils::ToDict(element);
std::optional<bool> frozen = dict.FindBool("frozen");
ASSERT_TRUE(frozen.has_value());
EXPECT_FALSE(frozen.value());
}
}
// This implicitly creates the `TabsEventRouter`, which is required to get a
// tab update event.
TabsWindowsAPI::Get(profile())->tabs_event_router();
// Freeze the background tab and wait for a tab update event.
TestEventRouterObserver event_router_observer(EventRouter::Get(profile()));
web_contents->SetPageFrozen(true);
event_router_observer.WaitForEventWithName(api::tabs::OnUpdated::kEventName);
// Check arguments for the tab update event.
//
// Note: Must simulate dispatching to an actual extension to get arguments,
// because the tab details exposed vary by extension. The arguments for the
// event received by `WillDispatchEvent()` are empty.
{
auto event_it =
event_router_observer.events().find(api::tabs::OnUpdated::kEventName);
ASSERT_NE(event_it, event_router_observer.events().end());
std::optional<base::Value::List> args_out;
mojom::EventFilteringInfoPtr event_filtering_info_out;
EXPECT_TRUE(event_it->second->will_dispatch_callback.Run(
profile(), mojom::ContextType::kUnprivilegedExtension, extension.get(),
/* listener_filter=*/nullptr, args_out, event_filtering_info_out));
ASSERT_TRUE(args_out.has_value());
ASSERT_EQ(args_out->size(), 3U);
EXPECT_EQ(ExtensionTabUtil::GetTabId(web_contents),
args_out.value()[0].GetInt());
auto changed_properties = utils::ToDict(args_out.value()[1]);
auto frozen_changed_property = changed_properties.FindBool("frozen");
ASSERT_TRUE(frozen_changed_property.has_value());
EXPECT_TRUE(frozen_changed_property.value());
}
// Query frozen tabs. There should be 1 and its "frozen" property should be
// true.
{
auto query_function = base::MakeRefCounted<TabsQueryFunction>();
query_function->set_extension(extension.get());
base::Value::List result(
utils::ToList(utils::RunFunctionAndReturnSingleResult(
query_function.get(), "[{\"frozen\": true}]",
browser()->profile())));
ASSERT_EQ(1u, result.size());
base::Value::Dict tab = utils::ToDict(result.front());
std::optional<bool> frozen = tab.FindBool("frozen");
ASSERT_TRUE(frozen.has_value());
EXPECT_TRUE(frozen.value());
EXPECT_EQ(ExtensionTabUtil::GetTabId(web_contents),
api_test_utils::GetInteger(tab, "id"));
}
// Query non-frozen tabs. There should be 1 and its "frozen" property should
// be false.
{
auto query_function = base::MakeRefCounted<TabsQueryFunction>();
query_function->set_extension(extension.get());
base::Value::List result(
utils::ToList(utils::RunFunctionAndReturnSingleResult(
query_function.get(), "[{\"frozen\": false}]",
browser()->profile())));
ASSERT_EQ(1u, result.size());
std::optional<bool> frozen =
utils::ToDict(result.front()).FindBool("frozen");
ASSERT_TRUE(frozen.has_value());
EXPECT_FALSE(frozen.value());
}
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsTest, AutoDiscardableProperty) {
// Create two aditional tabs.
content::OpenURLParams params(GURL(url::kAboutBlankURL), content::Referrer(),
WindowOpenDisposition::NEW_BACKGROUND_TAB,
ui::PAGE_TRANSITION_LINK, false);
content::WebContents* web_contents_a =
browser()->OpenURL(params, /*navigation_handle_callback=*/{});
content::WebContents* web_contents_b =
browser()->OpenURL(params, /*navigation_handle_callback=*/{});
// Creates Tab object to ensure the property is correct for the extension.
TabStripModel* tab_strip_model = browser()->tab_strip_model();
api::tabs::Tab tab_object_a = ExtensionTabUtil::CreateTabObject(
web_contents_a, kDontScrubBehavior, nullptr, tab_strip_model, 0);
EXPECT_TRUE(tab_object_a.auto_discardable);
// Set up query and update functions with the extension.
scoped_refptr<const Extension> extension = ExtensionBuilder("Test").Build();
auto RunQueryFunction = [this, &extension](const char* query_info) {
auto function = base::MakeRefCounted<TabsQueryFunction>();
function->set_extension(extension.get());
return utils::ToList(utils::RunFunctionAndReturnSingleResult(
function.get(), query_info, browser()->profile()));
};
auto RunUpdateFunction = [this, &extension](std::string update_info) {
auto function = base::MakeRefCounted<TabsUpdateFunction>();
function->set_extension(extension.get());
return utils::ToDict(utils::RunFunctionAndReturnSingleResult(
function.get(), update_info, browser()->profile()));
};
// Queries and results used.
const char* kAutoDiscardableQueryInfo = "[{\"autoDiscardable\": true}]";
const char* kNonAutoDiscardableQueryInfo = "[{\"autoDiscardable\": false}]";
// Get auto-discardable tabs. Returns all since tabs are auto-discardable
// by default.
base::Value::List query_result = RunQueryFunction(kAutoDiscardableQueryInfo);
EXPECT_EQ(3u, query_result.size());
// Get non auto-discardable tabs.
query_result = RunQueryFunction(kNonAutoDiscardableQueryInfo);
EXPECT_EQ(0u, query_result.size());
// Update the auto-discardable state of web contents A.
int tab_id_a = ExtensionTabUtil::GetTabId(web_contents_a);
base::Value::Dict update_result = RunUpdateFunction(
base::StringPrintf("[%u, {\"autoDiscardable\": false}]", tab_id_a));
EXPECT_EQ(tab_id_a, api_test_utils::GetInteger(update_result, "id"));
EXPECT_FALSE(api_test_utils::GetBoolean(update_result, "autoDiscardable"));
// Make sure the property is changed accordingly after updating the tab.
tab_object_a = ExtensionTabUtil::CreateTabObject(
web_contents_a, kDontScrubBehavior, nullptr, tab_strip_model, 0);
EXPECT_FALSE(tab_object_a.auto_discardable);
// Get auto-discardable tabs after changing the status of web contents A.
query_result = RunQueryFunction(kAutoDiscardableQueryInfo);
EXPECT_EQ(2u, query_result.size());
// Get non auto-discardable tabs after changing the status of web contents A.
query_result = RunQueryFunction(kNonAutoDiscardableQueryInfo);
ASSERT_EQ(1u, query_result.size());
// Make sure the returned tab is the correct one.
ASSERT_TRUE(query_result[0].is_dict());
std::optional<int> tab_id =
query_result[0].GetDict().FindInt(extension_misc::kId);
ASSERT_TRUE(tab_id);
EXPECT_EQ(tab_id_a, *tab_id);
// Update the auto-discardable state of web contents B.
int tab_id_b = ExtensionTabUtil::GetTabId(web_contents_b);
update_result = RunUpdateFunction(
base::StringPrintf("[%u, {\"autoDiscardable\": false}]", tab_id_b));
EXPECT_EQ(tab_id_b, api_test_utils::GetInteger(update_result, "id"));
EXPECT_FALSE(api_test_utils::GetBoolean(update_result, "autoDiscardable"));
// Get auto-discardable tabs after changing the status of both created tabs.
query_result = RunQueryFunction(kAutoDiscardableQueryInfo);
EXPECT_EQ(1u, query_result.size());
// Make sure the returned tab is the correct one.
ASSERT_TRUE(query_result[0].is_dict());
std::optional<int> id_value =
query_result[0].GetDict().FindInt(extension_misc::kId);
ASSERT_TRUE(id_value);
EXPECT_EQ(ExtensionTabUtil::GetTabId(tab_strip_model->GetWebContentsAt(0)),
*id_value);
// Get auto-discardable tabs after changing the status of both created tabs.
query_result = RunQueryFunction(kNonAutoDiscardableQueryInfo);
EXPECT_EQ(2u, query_result.size());
// Resets the first tab back to auto-discardable.
update_result = RunUpdateFunction(
base::StringPrintf("[%u, {\"autoDiscardable\": true}]", tab_id_a));
EXPECT_EQ(tab_id_a, api_test_utils::GetInteger(update_result, "id"));
EXPECT_TRUE(api_test_utils::GetBoolean(update_result, "autoDiscardable"));
// Get auto-discardable tabs after resetting the status of web contents A.
query_result = RunQueryFunction(kAutoDiscardableQueryInfo);
EXPECT_EQ(2u, query_result.size());
// Get non auto-discardable tabs after resetting the status of web contents A.
query_result = RunQueryFunction(kNonAutoDiscardableQueryInfo);
EXPECT_EQ(1u, query_result.size());
}
// Tester class for the tabs.zoom* api functions.
class ExtensionTabsZoomTest : public ExtensionTabsTest {
public:
void SetUpOnMainThread() override;
// Runs chrome.tabs.setZoom().
bool RunSetZoom(int tab_id, double zoom_factor);
// Runs chrome.tabs.getZoom().
testing::AssertionResult RunGetZoom(int tab_id, double* zoom_factor);
// Runs chrome.tabs.setZoomSettings().
bool RunSetZoomSettings(int tab_id, const char* mode, const char* scope);
// Runs chrome.tabs.getZoomSettings().
testing::AssertionResult RunGetZoomSettings(int tab_id,
std::string* mode,
std::string* scope);
// Runs chrome.tabs.getZoomSettings() and returns default zoom.
testing::AssertionResult RunGetDefaultZoom(int tab_id,
double* default_zoom_factor);
// Runs chrome.tabs.setZoom(), expecting an error.
std::string RunSetZoomExpectError(int tab_id,
double zoom_factor);
// Runs chrome.tabs.setZoomSettings(), expecting an error.
std::string RunSetZoomSettingsExpectError(int tab_id,
const char* mode,
const char* scope);
content::WebContents* OpenUrlAndWaitForLoad(const GURL& url);
private:
scoped_refptr<const Extension> extension_;
};
void ExtensionTabsZoomTest::SetUpOnMainThread() {
ExtensionTabsTest::SetUpOnMainThread();
extension_ = ExtensionBuilder("Test").Build();
}
bool ExtensionTabsZoomTest::RunSetZoom(int tab_id, double zoom_factor) {
auto set_zoom_function = base::MakeRefCounted<TabsSetZoomFunction>();
set_zoom_function->set_extension(extension_.get());
set_zoom_function->set_has_callback(true);
return utils::RunFunction(
set_zoom_function.get(),
base::StringPrintf("[%u, %lf]", tab_id, zoom_factor),
browser()->profile(), api_test_utils::FunctionMode::kNone);
}
testing::AssertionResult ExtensionTabsZoomTest::RunGetZoom(
int tab_id,
double* zoom_factor) {
auto get_zoom_function = base::MakeRefCounted<TabsGetZoomFunction>();
get_zoom_function->set_extension(extension_.get());
get_zoom_function->set_has_callback(true);
std::optional<base::Value> get_zoom_result =
utils::RunFunctionAndReturnSingleResult(
get_zoom_function.get(), base::StringPrintf("[%u]", tab_id),
browser()->profile());
if (!get_zoom_result)
return testing::AssertionFailure() << "no result";
std::optional<double> maybe_value = get_zoom_result->GetIfDouble();
if (!maybe_value.has_value())
return testing::AssertionFailure() << "result was not a double";
*zoom_factor = maybe_value.value();
return testing::AssertionSuccess();
}
bool ExtensionTabsZoomTest::RunSetZoomSettings(int tab_id,
const char* mode,
const char* scope) {
auto set_zoom_settings_function =
base::MakeRefCounted<TabsSetZoomSettingsFunction>();
set_zoom_settings_function->set_extension(extension_.get());
std::string args;
if (scope) {
args = base::StringPrintf("[%u, {\"mode\": \"%s\", \"scope\": \"%s\"}]",
tab_id, mode, scope);
} else {
args = base::StringPrintf("[%u, {\"mode\": \"%s\"}]", tab_id, mode);
}
return utils::RunFunction(set_zoom_settings_function.get(), args,
browser()->profile(),
api_test_utils::FunctionMode::kNone);
}
testing::AssertionResult ExtensionTabsZoomTest::RunGetZoomSettings(
int tab_id,
std::string* mode,
std::string* scope) {
DCHECK(mode);
DCHECK(scope);
auto get_zoom_settings_function =
base::MakeRefCounted<TabsGetZoomSettingsFunction>();
get_zoom_settings_function->set_extension(extension_.get());
get_zoom_settings_function->set_has_callback(true);
std::optional<base::Value> get_zoom_settings_result =
utils::RunFunctionAndReturnSingleResult(
get_zoom_settings_function.get(), base::StringPrintf("[%u]", tab_id),
browser()->profile());
if (!get_zoom_settings_result)
return testing::AssertionFailure() << "no result";
base::Value::Dict get_zoom_settings_dict =
utils::ToDict(std::move(get_zoom_settings_result));
*mode = api_test_utils::GetString(get_zoom_settings_dict, "mode");
*scope = api_test_utils::GetString(get_zoom_settings_dict, "scope");
return testing::AssertionSuccess();
}
testing::AssertionResult ExtensionTabsZoomTest::RunGetDefaultZoom(
int tab_id,
double* default_zoom_factor) {
DCHECK(default_zoom_factor);
auto get_zoom_settings_function =
base::MakeRefCounted<TabsGetZoomSettingsFunction>();
get_zoom_settings_function->set_extension(extension_.get());
get_zoom_settings_function->set_has_callback(true);
std::optional<base::Value> get_zoom_settings_result =
utils::RunFunctionAndReturnSingleResult(
get_zoom_settings_function.get(), base::StringPrintf("[%u]", tab_id),
browser()->profile());
if (!get_zoom_settings_result && get_zoom_settings_result->is_dict()) {
return testing::AssertionFailure()
<< "no result or result is not a dictionary";
}
std::optional<double> default_zoom_factor_setting =
get_zoom_settings_result->GetDict().FindDouble("defaultZoomFactor");
if (!default_zoom_factor_setting) {
return testing::AssertionFailure()
<< "default zoom factor not found in result";
}
*default_zoom_factor = *default_zoom_factor_setting;
return testing::AssertionSuccess();
}
std::string ExtensionTabsZoomTest::RunSetZoomExpectError(int tab_id,
double zoom_factor) {
auto set_zoom_function = base::MakeRefCounted<TabsSetZoomFunction>();
set_zoom_function->set_extension(extension_.get());
set_zoom_function->set_has_callback(true);
return utils::RunFunctionAndReturnError(
set_zoom_function.get(),
base::StringPrintf("[%u, %lf]", tab_id, zoom_factor),
browser()->profile());
}
std::string ExtensionTabsZoomTest::RunSetZoomSettingsExpectError(
int tab_id,
const char* mode,
const char* scope) {
auto set_zoom_settings_function =
base::MakeRefCounted<TabsSetZoomSettingsFunction>();
set_zoom_settings_function->set_extension(extension_.get());
return utils::RunFunctionAndReturnError(
set_zoom_settings_function.get(),
base::StringPrintf("[%u, {\"mode\": \"%s\", "
"\"scope\": \"%s\"}]",
tab_id, mode, scope),
browser()->profile());
}
content::WebContents* ExtensionTabsZoomTest::OpenUrlAndWaitForLoad(
const GURL& url) {
ui_test_utils::NavigateToURLWithDisposition(
browser(), url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
return browser()->tab_strip_model()->GetActiveWebContents();
}
namespace {
double GetZoomLevel(const content::WebContents* web_contents) {
return zoom::ZoomController::FromWebContents(web_contents)->GetZoomLevel();
}
content::OpenURLParams GetOpenParams(const char* url) {
return content::OpenURLParams(GURL(url), content::Referrer(),
WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui::PAGE_TRANSITION_LINK, false);
}
} // namespace
IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, SetAndGetZoom) {
content::OpenURLParams params(GetOpenParams(url::kAboutBlankURL));
content::WebContents* web_contents = OpenUrlAndWaitForLoad(params.url);
int tab_id = ExtensionTabUtil::GetTabId(web_contents);
// Test default values before we set anything.
double zoom_factor = -1;
EXPECT_TRUE(RunGetZoom(tab_id, &zoom_factor));
EXPECT_EQ(1.0, zoom_factor);
// Test chrome.tabs.setZoom().
const double kZoomLevel = 0.8;
EXPECT_TRUE(RunSetZoom(tab_id, kZoomLevel));
EXPECT_EQ(kZoomLevel,
blink::ZoomLevelToZoomFactor(GetZoomLevel(web_contents)));
// Test chrome.tabs.getZoom().
zoom_factor = -1;
EXPECT_TRUE(RunGetZoom(tab_id, &zoom_factor));
EXPECT_EQ(kZoomLevel, zoom_factor);
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, GetDefaultZoom) {
content::OpenURLParams params(GetOpenParams(url::kAboutBlankURL));
content::WebContents* web_contents = OpenUrlAndWaitForLoad(params.url);
int tab_id = ExtensionTabUtil::GetTabId(web_contents);
zoom::ZoomController* zoom_controller =
zoom::ZoomController::FromWebContents(web_contents);
double default_zoom_factor = -1.0;
EXPECT_TRUE(RunGetDefaultZoom(tab_id, &default_zoom_factor));
EXPECT_TRUE(blink::ZoomValuesEqual(
zoom_controller->GetDefaultZoomLevel(),
blink::ZoomFactorToZoomLevel(default_zoom_factor)));
// Change the default zoom level and verify GetDefaultZoom returns the
// correct value.
content::StoragePartition* partition =
web_contents->GetBrowserContext()->GetStoragePartition(
web_contents->GetSiteInstance());
ChromeZoomLevelPrefs* zoom_prefs =
static_cast<ChromeZoomLevelPrefs*>(partition->GetZoomLevelDelegate());
double default_zoom_level = zoom_controller->GetDefaultZoomLevel();
zoom_prefs->SetDefaultZoomLevelPref(default_zoom_level + 0.5);
default_zoom_factor = -1.0;
EXPECT_TRUE(RunGetDefaultZoom(tab_id, &default_zoom_factor));
EXPECT_TRUE(blink::ZoomValuesEqual(
default_zoom_level + 0.5,
blink::ZoomFactorToZoomLevel(default_zoom_factor)));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, SetToDefaultZoom) {
content::OpenURLParams params(GetOpenParams(url::kAboutBlankURL));
content::WebContents* web_contents = OpenUrlAndWaitForLoad(params.url);
int tab_id = ExtensionTabUtil::GetTabId(web_contents);
zoom::ZoomController* zoom_controller =
zoom::ZoomController::FromWebContents(web_contents);
double default_zoom_level = zoom_controller->GetDefaultZoomLevel();
double new_default_zoom_level = default_zoom_level + 0.42;
content::StoragePartition* partition =
web_contents->GetBrowserContext()->GetStoragePartition(
web_contents->GetSiteInstance());
ChromeZoomLevelPrefs* zoom_prefs =
static_cast<ChromeZoomLevelPrefs*>(partition->GetZoomLevelDelegate());
zoom_prefs->SetDefaultZoomLevelPref(new_default_zoom_level);
double observed_zoom_factor = -1.0;
EXPECT_TRUE(RunSetZoom(tab_id, 0.0));
EXPECT_TRUE(RunGetZoom(tab_id, &observed_zoom_factor));
EXPECT_TRUE(blink::ZoomValuesEqual(
new_default_zoom_level,
blink::ZoomFactorToZoomLevel(observed_zoom_factor)));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, ZoomSettings) {
// In this test we need two URLs that (1) represent real pages (i.e. they
// load without causing an error page load), (2) have different domains, and
// (3) are zoomable by the extension API (this last condition rules out
// chrome:// urls). We achieve this by noting that about:blank meets these
// requirements, allowing us to spin up an embedded http server on localhost
// to get the other domain.
net::EmbeddedTestServer http_server;
http_server.ServeFilesFromSourceDirectory("chrome/test/data");
ASSERT_TRUE(http_server.Start());
GURL url_A = http_server.GetURL("/simple.html");
GURL url_B("about:blank");
// Tabs A1 and A2 are navigated to the same origin, while B is navigated
// to a different one.
content::WebContents* web_contents_A1 = OpenUrlAndWaitForLoad(url_A);
content::WebContents* web_contents_A2 = OpenUrlAndWaitForLoad(url_A);
content::WebContents* web_contents_B = OpenUrlAndWaitForLoad(url_B);
int tab_id_A1 = ExtensionTabUtil::GetTabId(web_contents_A1);
int tab_id_A2 = ExtensionTabUtil::GetTabId(web_contents_A2);
int tab_id_B = ExtensionTabUtil::GetTabId(web_contents_B);
ASSERT_FLOAT_EQ(1.f,
blink::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1)));
ASSERT_FLOAT_EQ(1.f,
blink::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2)));
ASSERT_FLOAT_EQ(1.f,
blink::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_B)));
// Test per-origin automatic zoom settings.
EXPECT_TRUE(RunSetZoom(tab_id_B, 1.f));
EXPECT_TRUE(RunSetZoom(tab_id_A2, 1.1f));
EXPECT_FLOAT_EQ(1.1f,
blink::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1)));
EXPECT_FLOAT_EQ(1.1f,
blink::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2)));
EXPECT_FLOAT_EQ(1.f,
blink::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_B)));
// Test per-tab automatic zoom settings.
EXPECT_TRUE(RunSetZoomSettings(tab_id_A1, "automatic", "per-tab"));
EXPECT_TRUE(RunSetZoom(tab_id_A1, 1.2f));
EXPECT_FLOAT_EQ(1.2f,
blink::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1)));
EXPECT_FLOAT_EQ(1.1f,
blink::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2)));
// Test 'manual' mode.
EXPECT_TRUE(RunSetZoomSettings(tab_id_A1, "manual", nullptr));
EXPECT_TRUE(RunSetZoom(tab_id_A1, 1.3f));
EXPECT_FLOAT_EQ(1.3f,
blink::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1)));
EXPECT_FLOAT_EQ(1.1f,
blink::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2)));
// Test 'disabled' mode, which will reset A1's zoom to 1.f.
EXPECT_TRUE(RunSetZoomSettings(tab_id_A1, "disabled", nullptr));
std::string error = RunSetZoomExpectError(tab_id_A1, 1.4f);
EXPECT_TRUE(base::MatchPattern(error, keys::kCannotZoomDisabledTabError));
EXPECT_FLOAT_EQ(1.f,
blink::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A1)));
// We should still be able to zoom A2 though.
EXPECT_TRUE(RunSetZoom(tab_id_A2, 1.4f));
EXPECT_FLOAT_EQ(1.4f,
blink::ZoomLevelToZoomFactor(GetZoomLevel(web_contents_A2)));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, PerTabResetsOnNavigation) {
net::EmbeddedTestServer http_server;
http_server.ServeFilesFromSourceDirectory("chrome/test/data");
ASSERT_TRUE(http_server.Start());
GURL url_A = http_server.GetURL("/simple.html");
GURL url_B("about:blank");
content::WebContents* web_contents = OpenUrlAndWaitForLoad(url_A);
int tab_id = ExtensionTabUtil::GetTabId(web_contents);
EXPECT_TRUE(RunSetZoomSettings(tab_id, "automatic", "per-tab"));
std::string mode;
std::string scope;
EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope));
EXPECT_EQ("automatic", mode);
EXPECT_EQ("per-tab", scope);
// Navigation of tab should reset mode to per-origin.
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(browser(), url_B,
1);
EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope));
EXPECT_EQ("automatic", mode);
EXPECT_EQ("per-origin", scope);
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, GetZoomSettings) {
content::OpenURLParams params(GetOpenParams(url::kAboutBlankURL));
content::WebContents* web_contents = OpenUrlAndWaitForLoad(params.url);
int tab_id = ExtensionTabUtil::GetTabId(web_contents);
std::string mode;
std::string scope;
EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope));
EXPECT_EQ("automatic", mode);
EXPECT_EQ("per-origin", scope);
EXPECT_TRUE(RunSetZoomSettings(tab_id, "automatic", "per-tab"));
EXPECT_TRUE(RunGetZoomSettings(tab_id, &mode, &scope));
EXPECT_EQ("automatic", mode);
EXPECT_EQ("per-tab", scope);
std::string error =
RunSetZoomSettingsExpectError(tab_id, "manual", "per-origin");
EXPECT_TRUE(base::MatchPattern(error, keys::kPerOriginOnlyInAutomaticError));
error =
RunSetZoomSettingsExpectError(tab_id, "disabled", "per-origin");
EXPECT_TRUE(base::MatchPattern(error, keys::kPerOriginOnlyInAutomaticError));
}
IN_PROC_BROWSER_TEST_F(ExtensionTabsZoomTest, CannotZoomInvalidTab) {
content::OpenURLParams params(GetOpenParams(url::kAboutBlankURL));
content::WebContents* web_contents = OpenUrlAndWaitForLoad(params.url);
int tab_id = ExtensionTabUtil::GetTabId(web_contents);
int bogus_id = tab_id + 100;
std::string error = RunSetZoomExpectError(bogus_id, 3.14159);
EXPECT_TRUE(base::MatchPattern(error, ExtensionTabUtil::kTabNotFoundError));
error = RunSetZoomSettingsExpectError(bogus_id, "manual", "per-tab");
EXPECT_TRUE(base::MatchPattern(error, ExtensionTabUtil::kTabNotFoundError));
const char kNewTestTabArgs[] = "chrome://version";
params = GetOpenParams(kNewTestTabArgs);
web_contents = browser()->OpenURL(params, /*navigation_handle_callback=*/{});
tab_id = ExtensionTabUtil::GetTabId(web_contents);
// Test chrome.tabs.setZoom().
error = RunSetZoomExpectError(tab_id, 3.14159);
EXPECT_TRUE(
base::MatchPattern(error, manifest_errors::kCannotAccessChromeUrl));
// chrome.tabs.setZoomSettings().
error = RunSetZoomSettingsExpectError(tab_id, "manual", "per-tab");
EXPECT_TRUE(
base::MatchPattern(error, manifest_errors::kCannotAccessChromeUrl));
}
#if BUILDFLAG(ENABLE_PDF)
class ExtensionApiPdfTest : public base::test::WithFeatureOverride,
public PDFExtensionTestBase {
public:
ExtensionApiPdfTest()
: base::test::WithFeatureOverride(chrome_pdf::features::kPdfOopif) {}
bool UseOopif() const override { return GetParam(); }
};
// Regression test for crbug.com/660498.
IN_PROC_BROWSER_TEST_P(ExtensionApiPdfTest, TemporaryAddressSpoof) {
content::WebContents* first_web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_TRUE(first_web_contents);
chrome::NewTab(browser());
content::WebContents* second_web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
ASSERT_NE(first_web_contents, second_web_contents);
GURL url = embedded_test_server()->GetURL(
"/extensions/api_test/tabs/pdf_extension_test.html");
content::TestNavigationManager navigation_manager(
second_web_contents, GURL("http://www.facebook.com:83"));
ui_test_utils::NavigateToURLWithDisposition(
browser(), url, WindowOpenDisposition::CURRENT_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
// Ensure the PDF has loaded, and get the WebContents to click.
auto* web_contents_for_click = second_web_contents;
if (UseOopif()) {
ASSERT_TRUE(GetTestPdfViewerStreamManager(second_web_contents)
->WaitUntilPdfLoadedInFirstChild());
} else {
ASSERT_TRUE(
pdf_extension_test_util::EnsurePDFHasLoaded(second_web_contents));
auto inner_web_contents = web_contents_for_click->GetInnerWebContents();
ASSERT_EQ(1U, inner_web_contents.size());
// With MimeHandlerViewInCrossProcessFrame input should directly route to
// the guest WebContents as there is no longer a BrowserPlugin involved.
web_contents_for_click = inner_web_contents[0];
}
// (400, 300) in `web_contents_for_click` translates to a different coordinate
// in the PDF Viewer. The exact coordinate depends on the PDF Viewer's UI
// layout. In the test PDF embedded in pdf_extension_test.html, the entire PDF
// content area is a giant link to http://www.facebook.com:83. As long as this
// click hits that link target, it triggers the navigation required for test.
content::SimulateMouseClickAt(web_contents_for_click, 0,
blink::WebMouseEvent::Button::kLeft,
gfx::Point(400, 300));
ASSERT_TRUE(navigation_manager.WaitForRequestStart());
browser()->tab_strip_model()->ActivateTabAt(
0, TabStripUserGestureDetails(
TabStripUserGestureDetails::GestureType::kOther));
EXPECT_EQ(first_web_contents,
browser()->tab_strip_model()->GetActiveWebContents());
browser()->tab_strip_model()->ActivateTabAt(
1, TabStripUserGestureDetails(
TabStripUserGestureDetails::GestureType::kOther));
EXPECT_EQ(second_web_contents,
browser()->tab_strip_model()->GetActiveWebContents());
EXPECT_EQ(url, second_web_contents->GetVisibleURL());
// Wait for the TestNavigationManager-monitored navigation to complete to
// avoid a race during browser teardown (see crbug.com/882213).
ASSERT_TRUE(navigation_manager.WaitForNavigationFinished());
}
// TODO(crbug.com/40268279): Stop testing both modes after OOPIF PDF viewer
// launches.
INSTANTIATE_FEATURE_OVERRIDE_TEST_SUITE(ExtensionApiPdfTest);
#endif // BUILDFLAG(ENABLE_PDF)
// Tests how chrome.windows.create behaves when setSelfAsOpener parameter is
// used. setSelfAsOpener was introduced as a fix for https://crbug.com/713888
// and https://crbug.com/718489. This is a (slightly morphed) regression test
// for https://crbug.com/597750.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowsCreate_WithOpener) {
const extensions::Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("../simple_with_file"));
ASSERT_TRUE(extension);
// Navigate a tab to an extension page.
GURL extension_url = extension->GetResourceURL("file.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), extension_url));
content::WebContents* old_contents =
browser()->tab_strip_model()->GetActiveWebContents();
// Execute chrome.windows.create and store the new tab in |new_contents|.
content::WebContents* new_contents = nullptr;
{
content::WebContentsAddedObserver observer;
std::string script = base::StringPrintf(
R"( window.name = 'old-contents';
chrome.windows.create({url: '%s', setSelfAsOpener: true}); )",
extension_url.spec().c_str());
ASSERT_TRUE(content::ExecJs(old_contents, script));
new_contents = observer.GetWebContents();
ASSERT_TRUE(content::WaitForLoadStop(new_contents));
}
// Navigate the old and the new tab to a web URL.
ASSERT_TRUE(StartEmbeddedTestServer());
GURL web_url1 = embedded_test_server()->GetURL("/title1.html");
GURL web_url2 = embedded_test_server()->GetURL("/title2.html");
{
content::TestNavigationObserver nav_observer(new_contents, 1);
ASSERT_TRUE(content::ExecJs(
new_contents, "window.location = '" + web_url1.spec() + "';"));
nav_observer.Wait();
}
{
content::TestNavigationObserver nav_observer(old_contents, 1);
ASSERT_TRUE(content::ExecJs(
old_contents, "window.location = '" + web_url2.spec() + "';"));
nav_observer.Wait();
}
EXPECT_EQ(web_url1,
new_contents->GetPrimaryMainFrame()->GetLastCommittedURL());
EXPECT_EQ(web_url2,
old_contents->GetPrimaryMainFrame()->GetLastCommittedURL());
// Verify that the old and new tab are in the same process.
EXPECT_EQ(old_contents->GetPrimaryMainFrame()->GetProcess(),
new_contents->GetPrimaryMainFrame()->GetProcess());
// Verify the old and new contents are in the same BrowsingInstance.
EXPECT_TRUE(old_contents->GetPrimaryMainFrame()
->GetSiteInstance()
->IsRelatedSiteInstance(
new_contents->GetPrimaryMainFrame()->GetSiteInstance()));
// Verify that the |new_contents| has |window.opener| set.
EXPECT_EQ(old_contents->GetPrimaryMainFrame()->GetLastCommittedURL().spec(),
EvalJs(new_contents, "window.opener.location.href"));
// Verify that |new_contents| can find |old_contents| using window.open/name.
std::string location_of_other_window =
EvalJs(new_contents,
"var w = window.open('', 'old-contents');\n"
"w.location.href;")
.ExtractString();
EXPECT_EQ(old_contents->GetPrimaryMainFrame()->GetLastCommittedURL().spec(),
location_of_other_window);
}
// Tests how chrome.windows.create behaves when setSelfAsOpener parameter is not
// used. setSelfAsOpener was introduced as a fix for https://crbug.com/713888
// and https://crbug.com/718489.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowsCreate_NoOpener) {
const Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("../simple_with_file"));
ASSERT_TRUE(extension);
// Navigate a tab to an extension page.
GURL extension_url = extension->GetResourceURL("file.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), extension_url));
content::WebContents* old_contents =
browser()->tab_strip_model()->GetActiveWebContents();
// Execute chrome.windows.create and store the new tab in |new_contents|.
content::WebContents* new_contents = nullptr;
{
content::WebContentsAddedObserver observer;
std::string script = base::StringPrintf(
R"( window.name = 'old-contents';
chrome.windows.create({url: '%s'}); )",
extension_url.spec().c_str());
ASSERT_TRUE(content::ExecJs(old_contents, script));
new_contents = observer.GetWebContents();
ASSERT_TRUE(content::WaitForLoadStop(new_contents));
}
// Verify the old and new contents are NOT in the same BrowsingInstance.
EXPECT_FALSE(old_contents->GetPrimaryMainFrame()
->GetSiteInstance()
->IsRelatedSiteInstance(
new_contents->GetPrimaryMainFrame()->GetSiteInstance()));
// Verify that the |new_contents| doesn't have |window.opener| set.
EXPECT_EQ(false, EvalJs(new_contents, "!!window.opener"));
// TODO(lukasza): http://crbug.com/786411: Verify that |new_contents| can NOT
// find |old_contents| using window.open/name. This is currently broken,
// because browsing instance boundaries are pierced for all extension frames
// (we hope this can be limited to background pages / contents).
}
// Tests the origin of tabs created through chrome.windows.create().
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, WindowsCreate_OpenerAndOrigin) {
const extensions::Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("../simple_with_file"));
ASSERT_TRUE(extension);
// Navigate a tab to an extension page.
GURL extension_url = extension->GetResourceURL("file.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), extension_url));
content::WebContents* web_contents =
browser()->tab_strip_model()->GetActiveWebContents();
const std::string extension_origin_str =
url::Origin::Create(extension->url()).Serialize();
constexpr char kDataURL[] = "data:text/html,<html>test</html>";
std::string extension_url_str = extension_url.spec();
struct TestCase {
// The url to use in chrome.windows.create().
std::string url;
// If set, its value will be used to specify |setSelfAsOpener|.
std::optional<bool> set_self_as_opener;
// The origin we expect the new tab to be in, opaque origins will be "null".
std::string expected_origin_str;
};
auto test_cases = std::to_array<TestCase>({
// about:blank URLs.
// With opener relationship, about:blank urls will get the extension's
// origin, without opener relationship, they will get opaque/"null"
// origin.
{url::kAboutBlankURL, true, extension_origin_str},
{url::kAboutBlankURL, false, "null"},
{url::kAboutBlankURL, std::nullopt, "null"},
// data:... URLs.
// With opener relationship or not, "data:..." URLs always gets unique
// origin, so origin will always be "null" in these cases.
{kDataURL, true, "null"},
{kDataURL, false, "null"},
{kDataURL, std::nullopt, "null"},
// chrome-extension:// URLs.
// These always get extension origin.
{extension_url_str, true, extension_origin_str},
{extension_url_str, false, extension_origin_str},
{extension_url_str, std::nullopt, extension_origin_str},
});
auto run_test_case = [&web_contents](const TestCase& test_case) {
std::string maybe_specify_set_self_as_opener;
if (test_case.set_self_as_opener) {
maybe_specify_set_self_as_opener =
base::StringPrintf(", setSelfAsOpener: %s",
base::ToString(*test_case.set_self_as_opener));
}
std::string script = base::StringPrintf(
R"( chrome.windows.create({url: '%s'%s}); )", test_case.url.c_str(),
maybe_specify_set_self_as_opener.c_str());
content::WebContents* new_contents = nullptr;
{
content::WebContentsAddedObserver observer;
ASSERT_TRUE(content::ExecJs(web_contents, script));
new_contents = observer.GetWebContents();
}
ASSERT_TRUE(new_contents);
ASSERT_TRUE(content::WaitForLoadStop(new_contents));
EXPECT_EQ(test_case.expected_origin_str, EvalJs(new_contents, "origin;"));
const bool is_opaque_origin =
new_contents->GetPrimaryMainFrame()->GetLastCommittedOrigin().opaque();
EXPECT_EQ(test_case.expected_origin_str == "null", is_opaque_origin);
};
for (size_t i = 0; i < std::size(test_cases); ++i) {
const auto& test_case = test_cases[i];
SCOPED_TRACE(
base::StringPrintf("#%" PRIuS " %s", i, test_case.url.c_str()));
run_test_case(test_case);
}
}
// Tests updating a URL of a web tab to an about:blank. Verify that the new
// frame is placed in the correct process, has the correct origin and that no
// DCHECKs are hit anywhere.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, TabsUpdate_WebToAboutBlank) {
ASSERT_TRUE(embedded_test_server()->Start());
const extensions::Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("../simple_with_file"));
ASSERT_TRUE(extension);
GURL extension_url = extension->GetResourceURL("file.html");
url::Origin extension_origin = url::Origin::Create(extension_url);
GURL web_url = embedded_test_server()->GetURL("/title1.html");
url::Origin web_origin = url::Origin::Create(web_url);
GURL about_blank_url = GURL(url::kAboutBlankURL);
// Navigate a tab to an extension page.
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), extension_url));
content::WebContents* extension_contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(
extension_origin,
extension_contents->GetPrimaryMainFrame()->GetLastCommittedOrigin());
// Create another tab and navigate it to a web page.
content::WebContents* test_contents = nullptr;
{
content::WebContentsAddedObserver test_contents_observer;
ui_test_utils::NavigateToURLWithDisposition(
browser(), web_url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
test_contents = test_contents_observer.GetWebContents();
}
EXPECT_EQ(web_origin,
test_contents->GetPrimaryMainFrame()->GetLastCommittedOrigin());
EXPECT_NE(extension_contents->GetPrimaryMainFrame()->GetProcess(),
test_contents->GetPrimaryMainFrame()->GetProcess());
// Use |chrome.tabs.update| API to navigate |test_contents| to an about:blank
// URL.
{
content::TestNavigationObserver nav_observer(test_contents, 1);
int test_tab_id = ExtensionTabUtil::GetTabId(test_contents);
content::ExecuteScriptAsync(
extension_contents,
content::JsReplace("chrome.tabs.update($1, { url: $2 })", test_tab_id,
about_blank_url));
nav_observer.WaitForNavigationFinished();
EXPECT_TRUE(nav_observer.last_navigation_succeeded());
EXPECT_EQ(about_blank_url, nav_observer.last_navigation_url());
}
// Verify the origin and process of the about:blank tab.
content::RenderFrameHost* test_frame = test_contents->GetPrimaryMainFrame();
EXPECT_EQ(about_blank_url, test_frame->GetLastCommittedURL());
EXPECT_EQ(extension_contents->GetPrimaryMainFrame()->GetProcess(),
test_contents->GetPrimaryMainFrame()->GetProcess());
// Note that committing with the extension origin wouldn't be possible when
// targeting an incognito window (see also IncognitoApiTest.Incognito test).
EXPECT_EQ(extension_origin, test_frame->GetLastCommittedOrigin());
}
// Tests updating a URL of a web tab to an about:newtab. Verify that the new
// frame is placed in the correct process, has the correct origin and that no
// DCHECKs are hit anywhere.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, TabsUpdate_WebToAboutNewTab) {
ASSERT_TRUE(embedded_test_server()->Start());
const extensions::Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("../simple_with_file"));
ASSERT_TRUE(extension);
GURL extension_url = extension->GetResourceURL("file.html");
url::Origin extension_origin = url::Origin::Create(extension_url);
GURL web_url = embedded_test_server()->GetURL("/title1.html");
url::Origin web_origin = url::Origin::Create(web_url);
// https://crbug.com/1145381: about:version is rewritten to chrome://version
// when entered in the omnibox or used in a bookmark. Such rewriting is
// definitely undesirable for http-initiated navigations (see r818969), but
// it is less clear what should happen in extension-initiated navigations.
GURL about_newtab_url = GURL("about:newtab");
GURL chrome_newtab_url = GURL("chrome://new-tab-page/");
// Navigate a tab to an extension page.
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), extension_url));
content::WebContents* extension_contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(
extension_origin,
extension_contents->GetPrimaryMainFrame()->GetLastCommittedOrigin());
// Create another tab and navigate it to a web page.
content::WebContents* test_contents = nullptr;
{
content::WebContentsAddedObserver test_contents_observer;
ui_test_utils::NavigateToURLWithDisposition(
browser(), web_url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
test_contents = test_contents_observer.GetWebContents();
}
// Use |chrome.tabs.update| API to navigate |test_contents| to an about:newtab
// URL.
{
content::TestNavigationObserver nav_observer(test_contents, 1);
int test_tab_id = ExtensionTabUtil::GetTabId(test_contents);
content::ExecuteScriptAsync(
extension_contents,
content::JsReplace("chrome.tabs.update($1, { url: $2 })", test_tab_id,
about_newtab_url));
nav_observer.WaitForNavigationFinished();
EXPECT_TRUE(nav_observer.last_navigation_succeeded());
EXPECT_EQ(chrome_newtab_url, nav_observer.last_navigation_url());
}
// Verify the origin and process of the about:newtab tab.
content::RenderFrameHost* test_frame = test_contents->GetPrimaryMainFrame();
EXPECT_EQ(chrome_newtab_url, test_frame->GetLastCommittedURL());
EXPECT_EQ(url::Origin::Create(chrome_newtab_url),
test_frame->GetLastCommittedOrigin());
EXPECT_NE(extension_contents->GetPrimaryMainFrame()->GetProcess(),
test_contents->GetPrimaryMainFrame()->GetProcess());
}
// Tests updating a URL of a web tab to a non-web-accessible-resource of an
// extension - such navigation should be allowed.
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, TabsUpdate_WebToNonWAR) {
ASSERT_TRUE(embedded_test_server()->Start());
const extensions::Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("../simple_with_file"));
ASSERT_TRUE(extension);
GURL extension_url = extension->GetResourceURL("file.html");
url::Origin extension_origin = url::Origin::Create(extension_url);
GURL web_url = embedded_test_server()->GetURL("/title1.html");
url::Origin web_origin = url::Origin::Create(web_url);
GURL non_war_url = extension_url;
// Navigate a tab to an extension page.
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), extension_url));
content::WebContents* extension_contents =
browser()->tab_strip_model()->GetActiveWebContents();
EXPECT_EQ(
extension_origin,
extension_contents->GetPrimaryMainFrame()->GetLastCommittedOrigin());
// Create another tab and navigate it to a web page.
content::WebContents* test_contents = nullptr;
{
content::WebContentsAddedObserver test_contents_observer;
ui_test_utils::NavigateToURLWithDisposition(
browser(), web_url, WindowOpenDisposition::NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_LOAD_STOP);
test_contents = test_contents_observer.GetWebContents();
}
EXPECT_EQ(web_origin,
test_contents->GetPrimaryMainFrame()->GetLastCommittedOrigin());
EXPECT_NE(extension_contents->GetPrimaryMainFrame()->GetProcess(),
test_contents->GetPrimaryMainFrame()->GetProcess());
// Use |chrome.tabs.update| API to navigate |test_contents| to a
// non-web-accessible-resource of an extension.
{
content::TestNavigationObserver nav_observer(test_contents, 1);
int test_tab_id = ExtensionTabUtil::GetTabId(test_contents);
content::ExecuteScriptAsync(
extension_contents,
content::JsReplace("chrome.tabs.update($1, { url: $2 })", test_tab_id,
non_war_url));
nav_observer.WaitForNavigationFinished();
EXPECT_TRUE(nav_observer.last_navigation_succeeded());
EXPECT_EQ(non_war_url, nav_observer.last_navigation_url());
}
// Verify the origin and process of the navigated tab.
content::RenderFrameHost* test_frame = test_contents->GetPrimaryMainFrame();
EXPECT_EQ(non_war_url, test_frame->GetLastCommittedURL());
EXPECT_EQ(extension_origin, test_frame->GetLastCommittedOrigin());
EXPECT_EQ(extension_contents->GetPrimaryMainFrame()->GetProcess(),
test_contents->GetPrimaryMainFrame()->GetProcess());
}
IN_PROC_BROWSER_TEST_F(ExtensionApiTest,
ExtensionAPICannotCreateWindowForDevtools) {
DevToolsWindow* devtools = DevToolsWindowTesting::OpenDevToolsWindowSync(
browser()->tab_strip_model()->GetWebContentsAt(0), false /* is_docked */);
auto function = base::MakeRefCounted<WindowsCreateFunction>();
scoped_refptr<const Extension> extension(ExtensionBuilder("Test").Build());
function->set_extension(extension.get());
EXPECT_TRUE(base::MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf(
R"([{"tabId": %d}])",
ExtensionTabUtil::GetTabId(
DevToolsWindowTesting::Get(devtools)->main_web_contents())),
DevToolsWindowTesting::Get(devtools)->browser()->profile()),
tabs_constants::kNotAllowedForDevToolsError));
DevToolsWindowTesting::CloseDevToolsWindowSync(devtools);
}
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ExtensionAPICannotMoveDevtoolsTab) {
DevToolsWindow* devtools = DevToolsWindowTesting::OpenDevToolsWindowSync(
browser()->tab_strip_model()->GetWebContentsAt(0), false /* is_docked */);
auto function = base::MakeRefCounted<TabsMoveFunction>();
EXPECT_TRUE(base::MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf(
R"([%d, {"index": -1}])",
ExtensionTabUtil::GetTabId(
DevToolsWindowTesting::Get(devtools)->main_web_contents())),
DevToolsWindowTesting::Get(devtools)->browser()->profile()),
tabs_constants::kNotAllowedForDevToolsError));
DevToolsWindowTesting::CloseDevToolsWindowSync(devtools);
}
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ExtensionAPICannotGroupDevtoolsTab) {
ASSERT_TRUE(browser()->tab_strip_model()->SupportsTabGroups());
DevToolsWindow* devtools = DevToolsWindowTesting::OpenDevToolsWindowSync(
browser()->tab_strip_model()->GetWebContentsAt(0), false /* is_docked */);
auto function = base::MakeRefCounted<TabsGroupFunction>();
EXPECT_TRUE(base::MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf(
R"([{"tabIds": %d}])",
ExtensionTabUtil::GetTabId(
DevToolsWindowTesting::Get(devtools)->main_web_contents())),
DevToolsWindowTesting::Get(devtools)->browser()->profile()),
tabs_constants::kNotAllowedForDevToolsError));
DevToolsWindowTesting::CloseDevToolsWindowSync(devtools);
}
IN_PROC_BROWSER_TEST_F(ExtensionApiTest, ExtensionAPICannotDiscardDevtoolsTab) {
DevToolsWindow* devtools = DevToolsWindowTesting::OpenDevToolsWindowSync(
browser()->tab_strip_model()->GetWebContentsAt(0), false /* is_docked */);
auto function = base::MakeRefCounted<TabsDiscardFunction>();
EXPECT_TRUE(base::MatchPattern(
utils::RunFunctionAndReturnError(
function.get(),
base::StringPrintf(
"[%d]",
ExtensionTabUtil::GetTabId(
DevToolsWindowTesting::Get(devtools)->main_web_contents())),
DevToolsWindowTesting::Get(devtools)->browser()->profile()),
tabs_constants::kNotAllowedForDevToolsError));
DevToolsWindowTesting::CloseDevToolsWindowSync(devtools);
}
} // namespace extensions
|