1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <optional>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/field_trial.h"
#include "base/rand_util.h"
#include "base/run_loop.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.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/strings/utf_string_conversions.h"
#include "base/test/bind.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/mock_entropy_provider.h"
#include "base/test/run_until.h"
#include "base/test/scoped_feature_list.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/autofill/autofill_flow_test_util.h"
#include "chrome/browser/autofill/autofill_uitest.h"
#include "chrome/browser/autofill/autofill_uitest_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_io_data.h"
#include "chrome/browser/translate/chrome_translate_client.h"
#include "chrome/browser/translate/translate_service.h"
#include "chrome/browser/translate/translate_test_utils.h"
#include "chrome/browser/ui/autofill/autofill_suggestion_controller.h"
#include "chrome/browser/ui/autofill/chrome_autofill_client.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/translate/translate_bubble_model.h"
#include "chrome/browser/ui/translate/translate_bubble_test_utils.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/interactive_test_utils.h"
#include "chrome/test/base/test_switches.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/autofill/content/browser/content_autofill_driver.h"
#include "components/autofill/content/browser/test_autofill_manager_injector.h"
#include "components/autofill/core/browser/crowdsourcing/votes_uploader_test_api.h"
#include "components/autofill/core/browser/data_model/addresses/autofill_profile.h"
#include "components/autofill/core/browser/data_quality/validation.h"
#include "components/autofill/core/browser/foundations/browser_autofill_manager.h"
#include "components/autofill/core/browser/foundations/browser_autofill_manager_test_api.h"
#include "components/autofill/core/browser/foundations/browser_autofill_manager_test_delegate.h"
#include "components/autofill/core/browser/foundations/mock_autofill_manager_observer.h"
#include "components/autofill/core/browser/foundations/test_autofill_manager_waiter.h"
#include "components/autofill/core/browser/test_utils/autofill_test_utils.h"
#include "components/autofill/core/common/autofill_constants.h"
#include "components/autofill/core/common/autofill_features.h"
#include "components/autofill/core/common/autofill_util.h"
#include "components/metrics/content/subprocess_metrics_provider.h"
#include "components/network_session_configurator/common/network_switches.h"
#include "components/translate/core/browser/translate_manager.h"
#include "components/translate/core/common/translate_switches.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_mock_cert_verifier.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "content/public/test/scoped_accessibility_mode_override.h"
#include "content/public/test/test_renderer_host.h"
#include "content/public/test/test_utils.h"
#include "content/public/test/url_loader_interceptor.h"
#include "net/base/net_errors.h"
#include "net/cert/mock_cert_verifier.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/controllable_http_response.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "services/network/public/cpp/features.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/switches.h"
#include "third_party/re2/src/re2/re2.h"
#include "ui/events/base_event_utils.h"
#include "ui/events/keycodes/dom/keycode_converter.h"
#include "ui/events/keycodes/dom_us_layout_data.h"
#include "ui/events/keycodes/keyboard_code_conversion.h"
#include "ui/events/keycodes/keyboard_codes.h"
#if BUILDFLAG(IS_CHROMEOS) && BUILDFLAG(ENABLE_EXTENSIONS)
// Includes for ChromeVox accessibility tests.
#include "chrome/browser/ash/accessibility/accessibility_manager.h"
#include "chrome/browser/ash/accessibility/chromevox_test_utils.h"
#include "chrome/browser/ash/accessibility/speech_monitor.h"
#include "chrome/browser/ui/aura/accessibility/automation_manager_aura.h"
#include "extensions/browser/browsertest_util.h"
#include "ui/base/test/ui_controls.h"
#endif // BUILDFLAG(IS_CHROMEOS) && BUILDFLAG(ENABLE_EXTENSIONS)
using ::base::ASCIIToUTF16;
using ::base::test::RunClosure;
using ::content::URLLoaderInterceptor;
using ::testing::_;
using ::testing::AllOf;
using ::testing::AssertionFailure;
using ::testing::AssertionResult;
using ::testing::AssertionSuccess;
using ::testing::ElementsAre;
using ::testing::Eq;
using ::testing::Field;
using ::testing::Property;
using ::testing::StartsWith;
using ::testing::UnorderedElementsAreArray;
namespace autofill {
namespace {
constexpr char kTestShippingFormString[] = R"(
<html>
<head>
<!-- Disable extra network request for /favicon.ico -->
<link rel="icon" href="data:,">
</head>
<body>
An example of a shipping address form.
<form action="https://www.example.com/" method="POST" id="shipping">
<label for="firstname">First name:</label>
<input type="text" id="firstname"><br>
<label for="lastname">Last name:</label>
<input type="text" id="lastname"><br>
<label for="address1">Address line 1:</label>
<input type="text" id="address1"><br>
<label for="address2">Address line 2:</label>
<input type="text" id="address2"><br>
<label for="city">City:</label>
<input type="text" id="city"><br>
<label for="state">State:</label>
<select id="state">
<option value="" selected="yes">--</option>
<option value="CA">California</option>
<option value="TX">Texas</option>
</select><br>
<label for="zip">ZIP code:</label>
<input type="text" id="zip"><br>
<label for="country">Country:</label>
<select style="appearance:base-select" id="country">
<option value="" selected="yes">--</option>
<option value="CA">Canada</option>
<option value="US">United States</option>
</select><br>
<label for="phone">Phone number:</label>
<input type="text" id="phone"><br>
</form>
)";
// Searches all frames of the primary page in `web_contents` and returns one
// called `name`. If there are none, returns null, if there are more, returns
// an arbitrary one.
content::RenderFrameHost* RenderFrameHostForName(
content::WebContents* web_contents,
const std::string& name) {
return content::FrameMatchingPredicate(
web_contents->GetPrimaryPage(),
base::BindRepeating(&content::FrameMatchesName, name));
}
autofill::ElementExpr GetElementById(const std::string& id) {
return autofill::ElementExpr(
base::StringPrintf("document.getElementById(`%s`)", id.c_str()));
}
// Represents a field's expected or actual (as extracted from the DOM) id (not
// its name) and value.
struct FieldValue {
std::string id;
std::string value;
FormControlType form_control_type = FormControlType::kInputText;
};
std::ostream& operator<<(std::ostream& os, const FieldValue& field) {
return os << "{" << field.id << "=" << field.value << "}";
}
// Returns the field IDs and values of a collection of fields.
//
// Note that `control_elements` is *not* the ID of a form, but a JavaScript
// expression that evaluates to a collection of form-control elements, such has
// `document.getElementById('myForm').elements`.
std::vector<FieldValue> GetFieldValues(
const ElementExpr& control_elements,
content::ToRenderFrameHost execution_target) {
std::string script = base::StringPrintf(
R"( const fields = [];
for (const field of %s) {
fields.push({
id: field.id,
value: field.value
});
}
fields;
)",
control_elements->c_str());
content::EvalJsResult r = content::EvalJs(execution_target, script);
DCHECK(r.value.is_list()) << r.error;
std::vector<FieldValue> fields;
for (const base::Value& field : r.value.GetList()) {
const auto& field_dict = field.GetDict();
fields.push_back({.id = *field_dict.FindString("id"),
.value = *field_dict.FindString("value")});
}
return fields;
}
// Types the characters of `value` after focusing field `e`.
[[nodiscard]] AssertionResult EnterTextIntoField(
const autofill::ElementExpr& e,
std::string_view value,
AutofillUiTest* test,
content::ToRenderFrameHost execution_target) {
AssertionResult a = FocusField(e, execution_target);
if (!a) {
return a;
}
for (const char c : value) {
ui::DomKey key = ui::DomKey::FromCharacter(c);
if (!test->SendKeyToPageAndWait(key, {})) {
return AssertionFailure()
<< __func__ << "(): Could not type '" << value << "' into " << *e;
}
}
return AssertionSuccess();
}
const std::vector<FieldValue> kEmptyAddress{
{"firstname", ""}, {"lastname", ""}, {"address1", ""},
{"address2", ""}, {"city", ""}, {"state", ""},
{"zip", ""}, {"country", ""}, {"phone", ""}};
const struct {
const char* first_name = "Milton";
const char* middle_name = "C.";
const char* last_name = "Waddams";
const char* full_name = "Milton C. Waddams";
const char* address1 = "4120 Freidrich Lane";
const char* address2 = "Basement";
const char* city = "Austin";
const char* state_short = "TX";
const char* state = "Texas";
const char* zip = "78744";
const char* country = "US";
const char* phone = "5125551234";
const char* company = "Initech";
const char* email = "red.swingline@initech.com";
} kDefaultAddressValues;
const std::vector<FieldValue> kDefaultAddress{
{"firstname", kDefaultAddressValues.first_name},
{"lastname", kDefaultAddressValues.last_name},
{"address1", kDefaultAddressValues.address1},
{"address2", kDefaultAddressValues.address2},
{"city", kDefaultAddressValues.city},
{"state", kDefaultAddressValues.state_short},
{"zip", kDefaultAddressValues.zip},
{"country", kDefaultAddressValues.country},
{"phone", kDefaultAddressValues.phone}};
// Returns a copy of `fields` except that the value of `update.id` is set to
// `update.value`.
[[nodiscard]] std::vector<FieldValue> MergeValue(std::vector<FieldValue> fields,
const FieldValue& update) {
for (auto& field : fields) {
if (field.id == update.id) {
field.value = update.value;
return fields;
}
}
NOTREACHED();
}
// A generic "map" function, intended to lift values `args...` to a matcher
// `fun(args)...`. For example, `ElementsAreArray(Map({x, y, z}, fun))` is
// `ElementsAreArray({fun(x), fun(y), fun(z)})`.
template <typename Arg, typename Fun>
[[nodiscard]] auto Map(const std::vector<Arg>& args, Fun fun) {
std::vector<decltype(std::invoke(fun, args[0]))> matchers;
for (const Arg& arg : args) {
matchers.push_back(std::invoke(fun, arg));
}
return matchers;
}
// Matches a container of FieldValues if the `i`th actual FieldValue::value
// matches the `i`th `expected` FieldValue::value.
// As a sanity check, also requires that the `i`th actual FieldValue::id
// starts with the `i`th `expected` FieldValue::id.
[[nodiscard]] auto ValuesAre(const std::vector<FieldValue>& expected) {
return UnorderedElementsAreArray(
Map(expected, [](const FieldValue& expected) {
return AllOf(Field(&FieldValue::id, StartsWith(expected.id)),
Field(&FieldValue::value, Eq(expected.value)));
}));
}
[[nodiscard]] auto FieldsAre(auto matcher) {
return Property(&FormData::fields, ElementsAreArray(matcher));
}
// An object that waits for an observed form-control element to change its value
// to a non-empty string.
//
// See ListenForValueChange() for details.
class ValueWaiter {
public:
static constexpr base::TimeDelta kDefaultTimeout = base::Seconds(5);
ValueWaiter(int waiterId, content::ToRenderFrameHost execution_target)
: waiterId_(waiterId), execution_target_(execution_target) {}
// Returns the non-empty value of the observed form-control element, or
// std::nullopt if no value change is observed before `timeout`.
[[nodiscard]] std::optional<std::string> Wait(
base::TimeDelta timeout = kDefaultTimeout) && {
const std::string kFunction = R"(
// Polls the value of `window[observedValueSlots]` and replies with the
// value once its non-`undefined` or `timeoutMillis` have elapsed.
//
// The value is expected to be populated by listenForValueChange().
function pollValue(waiterId, timeoutMillis) {
console.log(`pollValue('${waiterId}', ${timeoutMillis})`);
let interval = undefined;
let timeout = undefined;
return new Promise(resolve => {
function reply(r) {
console.log(`pollValue('${waiterId}', ${timeoutMillis}): `+
`replying '${r}'`);
resolve(r);
clearTimeout(timeout);
clearInterval(interval);
}
function replyIfSet(r) {
if (r !== undefined)
reply(r);
}
timeout = setTimeout(function() {
console.log(`pollValue('${waiterId}', ${timeoutMillis}): timeout`);
reply(null);
}, timeoutMillis);
const kPollingIntervalMillis = 100;
interval = setInterval(function() {
replyIfSet(window.observedValueSlots[waiterId]);
}, kPollingIntervalMillis);
replyIfSet(window.observedValueSlots[waiterId]);
});
}
)";
std::string call = base::StringPrintf("pollValue(`%d`, %" PRId64 ")",
waiterId_, timeout.InMilliseconds());
content::EvalJsResult r =
content::EvalJs(execution_target_, kFunction + call);
return !r.value.is_none() ? std::make_optional(r.ExtractString())
: std::nullopt;
}
private:
int waiterId_;
content::ToRenderFrameHost execution_target_;
};
// Registers observers for a value change of a field `id`. This listener fires
// on the first time *any* object whose ID is `id` changes its value to a
// non-empty string after the global `unblock_variable` has become true.
//
// It is particularly useful for detecting refills.
//
// For example, consider the following chain JavaScript statements:
//
// 1. window.unblock = undefined // or any other value that converts to false;
// 2. document.body.innerHTML += '<input id="id">';
// 3. document.getElementById('id').value = "foo";
// 4. document.getElementById('id').remove();
// 5. document.body.innerHTML += '<input id="id">';
// 6. document.getElementById('id').value = "foo";
// 7. window.unblock = true;
// 8. document.getElementById('id').value = "";
// 9. document.getElementById('id').value = "bar";
//
// Then `ListenForValueChange("id", "unblock", rfh).Wait(base::Seconds(5)) ==
// "bar"`. The ListenForValueChange() call happens any point before Event 9, and
// Event 9 happens no later than 5 seconds after that.
[[nodiscard]] ValueWaiter ListenForValueChange(
const std::string& id,
const std::optional<std::string>& unblock_variable,
content::ToRenderFrameHost execution_target) {
const std::string kFunction = R"(
// This function observes the DOM for an attached form-control element `id`.
//
// On the first `change` event of such an element, it stores that element's
// value in an array `window[observedValueSlots]`.
//
// Returns the index of that value.
function listenForValueChange(id, unblockVariable) {
console.log(`listenForValueChange('${id}')`);
if (window.observedValueSlots === undefined)
window.observedValueSlots = [];
const waiterId = window.observedValueSlots.length;
window.observedValueSlots.push(undefined);
let observer = undefined;
function changeHandler() {
console.log(`listenForValueChange('${id}'): changeHandler()`);
// Since other handlers may manipulate the fields value or remove it
// from the DOM or replace it, we delay its execution.
setTimeout(function() {
console.log(`listenForValueChange('${id}'): changeHandler() timer`);
if (unblockVariable && window[unblockVariable] !== true) {
console.log(`listenForValueChange('${id}'): `+
`observed change, blocked by '${unblockVariable}'`);
return;
}
const e = document.getElementById(id);
if (e === null) {
console.log(`listenForValueChange('${id}'): element not found`);
return;
}
if (e.value === '') {
console.log(`listenForValueChange('${id}'): empty value`);
return;
}
console.log(`listenForValueChange('${id}'): storing in slot`);
window.observedValueSlots[waiterId] = e.value;
e.removeEventListener('change', changeHandler);
observer.disconnect();
}, 0);
}
// Observes the DOM to see if a new element `id` is added or some element
// changes its ID to `id`.
observer = new MutationObserver(function(mutations) {
const e = document.getElementById(id);
if (e !== null) {
console.log(`listenForValueChange('${id}'): some element has been `+
`attached or change`);
e.addEventListener('change', changeHandler);
}
});
observer.observe(document, {
attributes: true,
childList: true,
characterData: false,
subtree: true
});
const e = document.getElementById(id);
if (e !== null) {
console.log(`listenForValueChange('${id}'): element exists already`);
e.addEventListener('change', changeHandler);
}
return waiterId;
}
)";
std::string call =
base::StringPrintf("listenForValueChange(`%s`, `%s`)", id.c_str(),
unblock_variable.value_or("").c_str());
content::EvalJsResult r = content::EvalJs(execution_target, kFunction + call);
int waiterId = r.ExtractInt();
return ValueWaiter(waiterId, execution_target);
}
// Test fixtures derive from this class. This class hierarchy allows test
// fixtures to have distinct list of test parameters.
//
// TODO(crbug.com/41383133): Parametrize this class to ensure that all tests in
// this run with all possible valid combinations of
// features and field trials.
class AutofillInteractiveTestBase : public AutofillUiTest {
public:
AutofillInteractiveTestBase()
: https_server_(net::EmbeddedTestServer::TYPE_HTTPS) {
// Disable AutofillPageLanguageDetection because due to the little text in
// the HTML files, the detected language is flaky (e.g., it often detects
// "fr" instead of "en").
feature_list_.InitWithFeatures(
/*enabled_features=*/
{},
/*disabled_features=*/{features::kAutofillPageLanguageDetection});
}
~AutofillInteractiveTestBase() override = default;
AutofillInteractiveTestBase(const AutofillInteractiveTestBase&) = delete;
AutofillInteractiveTestBase& operator=(const AutofillInteractiveTestBase&) =
delete;
bool IsPopupShown() {
return !!ChromeAutofillClient::FromWebContentsForTesting(GetWebContents())
->suggestion_controller_for_testing();
}
std::vector<FieldValue> GetFormValues(
const ElementExpr& form = GetElementById("shipping")) {
return GetFieldValues(ElementExpr(*form + ".elements"), GetWebContents());
}
base::RepeatingClosure ExpectValues(
const std::vector<FieldValue>& expected_values,
const ElementExpr& form = GetElementById("shipping")) {
return base::BindRepeating(
[](AutofillInteractiveTestBase* self,
const std::vector<FieldValue>& expected_values,
const ElementExpr& form) {
EXPECT_THAT(self->GetFormValues(form), ValuesAre(expected_values));
},
this, expected_values, form);
}
content::EvalJsResult GetFieldValueById(const std::string& field_id) {
return GetFieldValue(GetElementById(field_id));
}
content::EvalJsResult GetFieldCheckedById(const std::string& field_id) {
return GetFieldChecked(GetElementById(field_id), GetWebContents());
}
content::EvalJsResult GetFieldValue(ElementExpr e) {
return GetFieldValue(e, GetWebContents());
}
content::EvalJsResult GetFieldValue(
const ElementExpr& e,
content::ToRenderFrameHost execution_target) {
std::string script = base::StringPrintf("%s.value", e->c_str());
return content::EvalJs(execution_target, script);
}
content::EvalJsResult GetFieldChecked(
const ElementExpr& e,
content::ToRenderFrameHost execution_target) {
std::string script = base::StringPrintf("%s.checked", e->c_str());
return content::EvalJs(execution_target, script);
}
void SetUp() override {
ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
AutofillUiTest::SetUp();
}
void SetUpOnMainThread() override {
AutofillUiTest::SetUpOnMainThread();
https_server_.SetSSLConfig(net::EmbeddedTestServer::CERT_OK);
https_server_.ServeFilesFromSourceDirectory("chrome/test/data");
https_server_.RegisterRequestHandler(base::BindRepeating(
&AutofillInteractiveTestBase::HandleTestURL, base::Unretained(this)));
ASSERT_TRUE(https_server_.InitializeAndListen());
https_server_.StartAcceptingConnections();
controllable_http_response_ =
std::make_unique<net::test_server::ControllableHttpResponse>(
embedded_test_server(), "/mock_translate_script.js",
true /*relative_url_is_prefix*/);
// Ensure that `embedded_test_server()` serves both domains used below.
host_resolver()->AddRule("*", "127.0.0.1");
embedded_test_server()->RegisterRequestHandler(base::BindRepeating(
&AutofillInteractiveTestBase::HandleTestURL, base::Unretained(this)));
embedded_test_server()->StartAcceptingConnections();
// By default, all SSL cert checks are valid. Can be overriden in tests if
// needed.
cert_verifier_.mock_cert_verifier()->set_default_result(net::OK);
}
void SetUpCommandLine(base::CommandLine* command_line) override {
AutofillUiTest::SetUpCommandLine(command_line);
cert_verifier_.SetUpCommandLine(command_line);
// Needed to allow input before commit on various builders.
command_line->AppendSwitch(blink::switches::kAllowPreCommitInput);
// TODO(crbug.com/40200965): Migrate to a better mechanism for testing
// around language detection.
command_line->AppendSwitch(switches::kOverrideLanguageDetection);
}
void SetUpInProcessBrowserTestFixture() override {
AutofillUiTest::SetUpInProcessBrowserTestFixture();
cert_verifier_.SetUpInProcessBrowserTestFixture();
}
void TearDownInProcessBrowserTestFixture() override {
cert_verifier_.TearDownInProcessBrowserTestFixture();
AutofillUiTest::TearDownInProcessBrowserTestFixture();
}
std::unique_ptr<net::test_server::HttpResponse> HandleTestURL(
const net::test_server::HttpRequest& request) {
if (!base::Contains(path_keyed_response_bodies_, request.relative_url)) {
return nullptr;
}
auto response = std::make_unique<net::test_server::BasicHttpResponse>();
response->set_code(net::HTTP_OK);
response->set_content_type("text/html;charset=utf-8");
response->set_content(path_keyed_response_bodies_[request.relative_url]);
return std::move(response);
}
translate::LanguageState& GetLanguageState() {
ChromeTranslateClient* client = ChromeTranslateClient::FromWebContents(
browser()->tab_strip_model()->GetActiveWebContents());
return *client->GetTranslateManager()->GetLanguageState();
}
// This is largely a copy of CheckForTranslateUI() from Translate's
// translate_language_browsertest.cc.
void NavigateToContentAndWaitForLanguageDetection(const char* content) {
ASSERT_TRUE(browser());
auto waiter = CreateTranslateWaiter(
browser()->tab_strip_model()->GetActiveWebContents(),
translate::TranslateWaiter::WaitEvent::kLanguageDetermined);
SetTestUrlResponse(content);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
waiter->Wait();
// Language detection sometimes fires early with an "und" (= undetermined)
// detected code.
size_t wait_counter = 0;
constexpr size_t kMaxWaits = 2;
while (GetLanguageState().source_language() == "und" ||
GetLanguageState().source_language().empty()) {
++wait_counter;
ASSERT_LE(wait_counter, kMaxWaits)
<< "Translate reported no/undetermined language " << wait_counter
<< " times";
CreateTranslateWaiter(
browser()->tab_strip_model()->GetActiveWebContents(),
translate::TranslateWaiter::WaitEvent::kLanguageDetermined)
->Wait();
}
const TranslateBubbleModel* model =
translate::test_utils::GetCurrentModel(browser());
ASSERT_NE(nullptr, model);
}
// This is largely a copy of Translate() from Translate's
// translate_language_browsertest.cc.
void Translate(const bool first_translate) {
auto waiter = CreateTranslateWaiter(
browser()->tab_strip_model()->GetActiveWebContents(),
translate::TranslateWaiter::WaitEvent::kPageTranslated);
EXPECT_EQ(
TranslateBubbleModel::VIEW_STATE_BEFORE_TRANSLATE,
translate::test_utils::GetCurrentModel(browser())->GetViewState());
translate::test_utils::PressTranslate(browser());
if (first_translate)
SimulateURLFetch();
waiter->Wait();
EXPECT_EQ(
TranslateBubbleModel::VIEW_STATE_AFTER_TRANSLATE,
translate::test_utils::GetCurrentModel(browser())->GetViewState());
}
void CreateTestProfile() {
AutofillProfile profile(AddressCountryCode(kDefaultAddressValues.country));
test::SetProfileInfo(
&profile, kDefaultAddressValues.first_name,
kDefaultAddressValues.middle_name, kDefaultAddressValues.last_name,
kDefaultAddressValues.email, kDefaultAddressValues.company,
kDefaultAddressValues.address1, kDefaultAddressValues.address2,
kDefaultAddressValues.city, kDefaultAddressValues.state,
kDefaultAddressValues.zip, kDefaultAddressValues.country,
kDefaultAddressValues.phone);
profile.usage_history().set_use_count(
9999999); // We want this to be the first profile.
AddTestProfile(browser()->profile(), profile);
}
void CreateSecondTestProfile() {
AutofillProfile profile(AddressCountryCode("US"));
test::SetProfileInfo(&profile, "Alice", "M.", "Wonderland",
"alice@wonderland.com", "Magic", "333 Cat Queen St.",
"Rooftop", "Liliput", "CA", "10003", "US",
"15166900292");
AddTestProfile(browser()->profile(), profile);
}
void CreateTestCreditCart() {
CreditCard card;
test::SetCreditCardInfo(&card, "Milton Waddams", "4111111111111111", "09",
"2999", "");
AddTestCreditCard(browser()->profile(), card);
}
void SimulateURLFetch() {
std::string script = R"(
var google = {};
google.translate = (function() {
return {
TranslateService: function() {
return {
isAvailable : function() {
return true;
},
restore : function() {
return;
},
getDetectedLanguage : function() {
return "ja";
},
translatePage : function(sourceLang, targetLang,
onTranslateProgress) {
document.getElementsByTagName("body")[0].innerHTML = `)" +
std::string(kTestShippingFormString) + R"(`;
onTranslateProgress(100, true, false);
}
};
}
};
})();
cr.googleTranslate.onTranslateElementLoad(); )";
controllable_http_response_->WaitForRequest();
controllable_http_response_->Send(
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/javascript\r\n"
"\r\n");
controllable_http_response_->Send(script);
controllable_http_response_->Done();
}
// Make a pointless round trip to the renderer, giving the popup a chance to
// show if it's going to. If it does show, an assert in
// BrowserAutofillManagerTestDelegateImpl will trigger.
void MakeSurePopupDoesntAppear() {
EXPECT_EQ(42, content::EvalJs(GetWebContents(), "42"));
}
void FillElementWithValue(const std::string& element_id,
const std::string& value) {
// Sends "`element_id`:`value`" to `msg_queue` if the `element_id`'s
// value has changed to `value`.
std::string script = base::StringPrintf(
R"( (function() {
const element_id = '%s';
const value = '%s';
const field = document.getElementById(element_id);
const listener = function() {
if (field.value === value) {
field.removeEventListener('input', listener);
domAutomationController.send(element_id +':'+ field.value);
}
};
field.addEventListener('input', listener, false);
return 'done';
})(); )",
element_id.c_str(), value.c_str());
ASSERT_TRUE(content::ExecJs(GetWebContents(), script));
content::DOMMessageQueue msg_queue(GetWebContents());
for (char16_t character : value) {
ui::DomKey dom_key = ui::DomKey::FromCharacter(character);
const ui::PrintableCodeEntry* code_entry = std::ranges::find_if(
ui::kPrintableCodeMap,
[character](const ui::PrintableCodeEntry& entry) {
return entry.character[0] == character ||
entry.character[1] == character;
});
ASSERT_TRUE(code_entry != std::end(ui::kPrintableCodeMap));
bool shift = code_entry->character[1] == character;
ui::DomCode dom_code = code_entry->dom_code;
content::SimulateKeyPress(GetWebContents(), dom_key, dom_code,
ui::DomCodeToUsLayoutKeyboardCode(dom_code),
false, shift, false, false);
}
std::string reply;
ASSERT_TRUE(msg_queue.WaitForMessage(&reply));
ASSERT_EQ("\"" + element_id + ":" + value + "\"", reply);
}
void DeleteElementValue(const ElementExpr& field) {
std::string script = base::StringPrintf("%s.value = '';", field->c_str());
ASSERT_TRUE(content::ExecJs(GetWebContents(), script));
ASSERT_EQ("", GetFieldValue(field));
}
void ExecuteScript(const std::string& script) {
ASSERT_TRUE(content::ExecJs(GetWebContents(), script));
}
GURL GetTestUrl() const { return https_server_.GetURL(kTestUrlPath); }
void SetTestUrlResponse(std::string content) {
SetResponseForUrlPath(kTestUrlPath, std::move(content));
}
void SetResponseForUrlPath(std::string path, std::string content) {
path_keyed_response_bodies_[std::move(path)] = std::move(content);
}
net::EmbeddedTestServer* https_server() { return &https_server_; }
static const char kTestUrlPath[];
base::HistogramTester& histogram_tester() { return histogram_tester_; }
private:
net::EmbeddedTestServer https_server_;
// Similar to net::MockCertVerifier, but also updates the CertVerifier
// used by the NetworkService.
content::ContentMockCertVerifier cert_verifier_;
// KeyPressEventCallback that serves as a sink to ensure that every key press
// event the tests create and have the WebContents forward is handled by some
// key press event callback. It is necessary to have this sink because if no
// key press event callback handles the event (at least on Mac), a DCHECK
// ends up going off that the `event` doesn't have an `os_event` associated
// with it.
content::RenderWidgetHost::KeyPressEventCallback key_press_event_sink_;
std::unique_ptr<net::test_server::ControllableHttpResponse>
controllable_http_response_;
// A map of relative paths to content that shall be served with an HTTP_OK
// response. If the map contains no entry, the request falls through to the
// serving from disk.
std::map<std::string, std::string> path_keyed_response_bodies_;
base::test::ScopedFeatureList feature_list_;
base::HistogramTester histogram_tester_;
};
const char AutofillInteractiveTestBase::kTestUrlPath[] =
"/internal/test_url_path";
class AutofillInteractiveTest : public AutofillInteractiveTestBase {
protected:
AutofillInteractiveTest() = default;
~AutofillInteractiveTest() override = default;
void SetUpCommandLine(base::CommandLine* command_line) override {
AutofillInteractiveTestBase::SetUpCommandLine(command_line);
command_line->AppendSwitchASCII(
translate::switches::kTranslateScriptURL,
embedded_test_server()->GetURL("/mock_translate_script.js").spec());
}
};
class AutofillInteractiveTestWithHistogramTester
: public AutofillInteractiveTest {
public:
AutofillInteractiveTestWithHistogramTester() {
feature_list_.InitWithFeatureState(
features::test::kAutofillServerCommunication, true);
}
void SetUp() override {
url_loader_interceptor_ = std::make_unique<URLLoaderInterceptor>(
base::BindRepeating([](URLLoaderInterceptor::RequestParams* params) {
// Only allow requests to be loaded that are necessary for the test.
// This allows a histogram to test properties of some specific
// requests.
std::vector<std::string> allowlist = {
"/internal/test_url_path", "https://clients1.google.com/tbproxy",
"https://content-autofill.googleapis.com/"};
// Intercept if not allow-listed.
return std::ranges::all_of(allowlist, [¶ms](const auto& s) {
return params->url_request.url.spec().find(s) == std::string::npos;
});
}));
AutofillInteractiveTest::SetUp();
}
void TearDownOnMainThread() override {
url_loader_interceptor_.reset();
AutofillInteractiveTest::TearDownOnMainThread();
}
void SetUpCommandLine(base::CommandLine* command_line) override {
AutofillInteractiveTest::SetUpCommandLine(command_line);
// Prevents proxy.pac requests.
command_line->AppendSwitch(switches::kNoProxyServer);
}
private:
std::unique_ptr<URLLoaderInterceptor> url_loader_interceptor_;
base::test::ScopedFeatureList feature_list_;
};
// Test the basic form-fill flow.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, BasicFormFill) {
CreateTestProfile();
SetTestUrlResponse(kTestShippingFormString);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.show_method = ShowMethod::ByChar('M'),
.after_select = ExpectValues(MergeValue(
kEmptyAddress, {"firstname", "M"}))}));
EXPECT_THAT(GetFormValues(), ValuesAre(kDefaultAddress));
}
// Test that hidden selects get filled. Hidden selects are often used by widgets
// which look like <select>s but are actually constructed out of divs.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, FillHiddenSelect) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/form_hidden_select.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
// Make sure the form was filled correctly.
EXPECT_EQ(kDefaultAddressValues.first_name, GetFieldValueById("firstname"));
EXPECT_EQ(kDefaultAddressValues.address1, GetFieldValueById("address1"));
EXPECT_EQ(kDefaultAddressValues.city, GetFieldValueById("city"));
EXPECT_EQ(kDefaultAddressValues.state_short, GetFieldValueById("state"));
}
class AutofillInteractiveTest_PrefillFormAndFill
: public AutofillInteractiveTest {
public:
AutofillInteractiveTest_PrefillFormAndFill() {
scoped_feature_list_.InitAndDisableFeature(
features::kAutofillSkipPreFilledFields);
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
};
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, BasicUndoAutofill) {
CreateTestProfile();
SetTestUrlResponse(kTestShippingFormString);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.show_method = ShowMethod::ByChar('M'),
.after_select = ExpectValues(MergeValue(
kEmptyAddress, {"firstname", "M"}))}));
EXPECT_THAT(GetFormValues(), ValuesAre(kDefaultAddress));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.expect_previews = false,
.show_method = ShowMethod::ByClick(),
.target_index = 1}));
std::vector<FieldValue> expected_values = kEmptyAddress;
expected_values[0].value = "M";
EXPECT_THAT(GetFormValues(), ValuesAre(expected_values));
}
// TODO(crbug.com/40924835) Flaky on Mac.
#if BUILDFLAG(IS_MAC)
#define MAYBE_ModifyTextFieldAndFill DISABLED_ModifyTextFieldAndFill
#else
#define MAYBE_ModifyTextFieldAndFill ModifyTextFieldAndFill
#endif
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
MAYBE_ModifyTextFieldAndFill) {
CreateTestProfile();
SetTestUrlResponse(kTestShippingFormString);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
// Modify a field.
ASSERT_TRUE(FocusField(GetElementById("city"), GetWebContents()));
FillElementWithValue("city", "Montreal");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
EXPECT_THAT(GetFormValues(),
ValuesAre(MergeValue(kDefaultAddress, {"city", "Montreal"})));
}
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, ModifyTextNotifiesObserver) {
CreateTestProfile();
SetTestUrlResponse(kTestShippingFormString);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
autofill::MockAutofillManagerObserver observer;
BrowserAutofillManager* autofill_manager = GetBrowserAutofillManager();
autofill_manager->AddObserver(&observer);
// OnAfterTextFieldValueChanged will eventually be called with the final text
// "Montreal".
EventWaiter<bool> waiter({true});
EXPECT_CALL(observer, OnAfterTextFieldValueChanged(_, _, _, _))
.WillRepeatedly([&](AutofillManager&, FormGlobalId, FieldGlobalId,
std::u16string text_value) {
if (text_value == u"Montreal") {
waiter.OnEvent(true);
}
});
ASSERT_TRUE(FocusField(GetElementById("city"), GetWebContents()));
FillElementWithValue("city", "Montreal");
ASSERT_TRUE(waiter.Wait());
autofill_manager->RemoveObserver(&observer);
}
// Same as ModifyTextNotifiesObserver, but for textarea rather than input
// elements.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
ModifyTextAreaNotifiesObserver) {
constexpr char kForm[] = R"(
<html>
<head>
<!-- Disable extra network request for /favicon.ico -->
<link rel="icon" href="data:,">
</head>
<body>
<form action="https://www.example.com/" method="POST" id="shipping">
<label for="address1">Address line 1:</label>
<textarea id="address1"></textarea>
</form>
)";
CreateTestProfile();
SetTestUrlResponse(kForm);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
autofill::MockAutofillManagerObserver observer;
BrowserAutofillManager* autofill_manager = GetBrowserAutofillManager();
autofill_manager->AddObserver(&observer);
EventWaiter<bool> waiter({true});
EXPECT_CALL(observer, OnAfterTextFieldValueChanged(_, _, _, _))
.WillRepeatedly([&](AutofillManager&, FormGlobalId, FieldGlobalId,
std::u16string text_value) {
if (text_value == u"My Address") {
waiter.OnEvent(true);
}
});
ASSERT_TRUE(FocusField(GetElementById("address1"), GetWebContents()));
FillElementWithValue("address1", "My Address");
ASSERT_TRUE(waiter.Wait());
autofill_manager->RemoveObserver(&observer);
}
void DoModifySelectFieldAndFill(AutofillInteractiveTest* test) {
test->CreateTestProfile();
test->SetTestUrlResponse(kTestShippingFormString);
ASSERT_TRUE(
ui_test_utils::NavigateToURL(test->browser(), test->GetTestUrl()));
// Modify a field.
ASSERT_TRUE(FocusField(GetElementById("state"), test->GetWebContents()));
ASSERT_NE(kDefaultAddressValues.state_short, "CA");
test->FillElementWithValue("state", "CA");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), test));
EXPECT_THAT(test->GetFormValues(),
ValuesAre(MergeValue(kDefaultAddress, {"state", "CA"})));
}
// Test that autofill doesn't refill a <select> field initially modified by the
// user.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, ModifySelectFieldAndFill) {
DoModifySelectFieldAndFill(this);
}
// Test that autofill works when the website prefills the form.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest_PrefillFormAndFill,
PrefillFormAndFill) {
const char kPrefillScript[] =
R"( <script>
document.getElementById('firstname').value = 'Seb';
document.getElementById('lastname').value = 'Bell';
document.getElementById('address1').value = '3243 Notre-Dame Ouest';
document.getElementById('address2').value = 'apt 843';
document.getElementById('city').value = 'Montreal';
document.getElementById('zip').value = 'H5D 4D3';
document.getElementById('phone').value = '15142223344';
</script>)";
CreateTestProfile();
SetTestUrlResponse(base::StrCat({kTestShippingFormString, kPrefillScript}));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
// We need to delete the prefilled value and then trigger the autofill.
auto Delete = [this] { DeleteElementValue(GetElementById("firstname")); };
ASSERT_TRUE(
AutofillFlow(GetElementById("firstname"), this,
{.after_focus = base::BindLambdaForTesting(Delete)}));
EXPECT_THAT(GetFormValues(), ValuesAre(kDefaultAddress));
}
// Test that form filling can be initiated by pressing the down arrow.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillViaDownArrow) {
CreateTestProfile();
SetTestUrlResponse(kTestShippingFormString);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
// Focus a fillable field.
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
// The form should be filled.
EXPECT_THAT(GetFormValues(), ValuesAre(kDefaultAddress));
}
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillSelectViaTab) {
CreateTestProfile();
SetTestUrlResponse(kTestShippingFormString);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
// Focus a fillable field.
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
// The form should be filled.
EXPECT_THAT(GetFormValues(), ValuesAre(kDefaultAddress));
}
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillViaClick) {
CreateTestProfile();
SetTestUrlResponse(kTestShippingFormString);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.show_method = ShowMethod::ByClick(),
.execution_target = GetWebContents()}));
EXPECT_THAT(GetFormValues(), ValuesAre(kDefaultAddress));
}
// Makes sure that the first click does or does not activate the autofill popup
// on the initial click within a fillable field.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, Click) {
CreateTestProfile();
SetTestUrlResponse(kTestShippingFormString);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.do_focus = false,
.show_method = ShowMethod::ByClick(),
.execution_target = GetWebContents()}));
EXPECT_THAT(GetFormValues(), ValuesAre(kDefaultAddress));
}
// Makes sure that clicking outside the focused field doesn't activate
// the popup.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DontAutofillForOutsideClick) {
static const char kDisabledButton[] =
R"(<button disabled id='disabled-button'>Cant click this</button>)";
CreateTestProfile();
SetTestUrlResponse(base::StrCat({kTestShippingFormString, kDisabledButton}));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
// Clicking a disabled button will generate a mouse event but focus doesn't
// change. This tests that autofill can handle a mouse event outside a focused
// input *without* showing the popup.
ASSERT_FALSE(AutofillFlow(GetElementById("disabled-button"), this,
{.do_focus = false,
.do_select = false,
.do_accept = false,
.show_method = ShowMethod::ByClick(),
.execution_target = GetWebContents()}));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.do_focus = false,
.show_method = ShowMethod::ByClick(),
.execution_target = GetWebContents()}));
}
// Makes sure that clicking a field while there is no enough height in the
// content area for at least one suggestion, won't show the autofill popup. This
// is a regression test for crbug.com/1108181
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
DontAutofillShowPopupWhenNoEnoughHeightInContentArea) {
// This firstname field starts at y=-100px and has a height of 5120px. There
// is no enough space to show at least one row of the autofill popup and hence
// the autofill shouldn't be shown.
static const char kTestFormWithLargeInputField[] =
R"(<form action="https://www.example.com/" method="POST">
<label for="firstname">First name:</label>
<input type="text" id="firstname" style="position:fixed;
top:-100px;height:5120px"><br>
<label for="lastname">Last name:</label>
<input type="text" id="lastname"><br>
<label for="city">City:</label>
<input type="text" id="city"><br>
</form>)";
CreateTestProfile();
SetTestUrlResponse(kTestFormWithLargeInputField);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
ASSERT_FALSE(AutofillFlow(GetElementById("firstname"), this,
{.do_select = false,
.do_accept = false,
.show_method = ShowMethod::ByClick(),
// Since failure is expected, no need to retry
// showing the Autofill popup too often.
.max_show_tries = 2,
.execution_target = GetWebContents()}));
}
// Test that a field is still autofillable after the previously autofilled
// value is deleted.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnDeleteValueAfterAutofill) {
CreateTestProfile();
SetTestUrlResponse(kTestShippingFormString);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.show_method = ShowMethod::ByChar('M')}));
EXPECT_THAT(GetFormValues(), ValuesAre(kDefaultAddress));
// Delete the value of a filled field.
DeleteElementValue(GetElementById("firstname"));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.show_method = ShowMethod::ByChar('M')}));
EXPECT_EQ("Milton", GetFieldValue(GetElementById("firstname")));
}
// Test that an input field is not rendered with the blue autofilled
// background color when choosing an option from the datalist suggestion list.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnSelectOptionFromDatalist) {
static const char kTestForm[] =
R"( <p>The text is some page content to paint</p>
<form action="https://www.example.com/" method="POST">
<input list="dl" type="search" id="firstname"><br>
<datalist id="dl">
<option value="Adam"></option>
<option value="Bob"></option>
<option value="Carl"></option>
</datalist>
</form> )";
SetTestUrlResponse(kTestForm);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
auto GetBackgroundColor = [this](const ElementExpr& id) {
std::string script = base::StringPrintf(
"document.defaultView.getComputedStyle(%s).backgroundColor",
id->c_str());
return content::EvalJs(GetWebContents(), script).ExtractString();
};
std::string original_color = GetBackgroundColor(GetElementById("firstname"));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.num_profile_suggestions = 0, .target_index = 1}));
EXPECT_EQ("Bob", GetFieldValueById("firstname"));
EXPECT_EQ(GetBackgroundColor(GetElementById("firstname")), original_color);
}
// Test that an <input> field with a <datalist> has a working drop down even if
// it was dynamically changed to <input type="password"> temporarily. This is a
// regression test for crbug.com/918351.
IN_PROC_BROWSER_TEST_F(
AutofillInteractiveTest,
OnSelectOptionFromDatalistTurningToPasswordFieldAndBack) {
static const char kTestForm[] =
R"( <p>The text is some page content to paint</p>
<form action="https://www.example.com/" method="POST">
<input list="dl" type="search" id="firstname"><br>
<datalist id="dl">
<option value="Adam"></option>
<option value="Bob"></option>
<option value="Carl"></option>
</datalist>
</form> )";
SetTestUrlResponse(kTestForm);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
ASSERT_TRUE(content::ExecJs(
GetWebContents(),
"document.getElementById('firstname').type = 'password';"));
// At this point, the IsPasswordFieldForAutofill() function returns true and
// will continue to return true for the field, even when the type is changed
// back to 'search'.
ASSERT_TRUE(
content::ExecJs(GetWebContents(),
"document.getElementById('firstname').type = 'search';"));
// Regression test for crbug.com/918351 whether the datalist becomes available
// again.
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.num_profile_suggestions = 0, .target_index = 1}));
// Pressing the down arrow preselects the first item. Pressing it again
// selects the second item.
EXPECT_EQ("Bob", GetFieldValueById("firstname"));
}
// Test that a JavaScript oninput event is fired after auto-filling a form.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnInputAfterAutofill) {
static const char kOnInputScript[] =
R"( <script>
focused_fired = false;
unfocused_fired = false;
changed_select_fired = false;
unchanged_select_fired = false;
document.getElementById('firstname').oninput = function() {
focused_fired = true;
};
document.getElementById('lastname').oninput = function() {
unfocused_fired = true;
};
document.getElementById('state').oninput = function() {
changed_select_fired = true;
};
document.getElementById('country').oninput = function() {
unchanged_select_fired = true;
};
document.getElementById('country').value = 'US';
</script> )";
CreateTestProfile();
SetTestUrlResponse(base::StrCat({kTestShippingFormString, kOnInputScript}));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.show_method = ShowMethod::ByChar('M')}));
EXPECT_THAT(GetFormValues(), ValuesAre(kDefaultAddress));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "focused_fired;"));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "unfocused_fired;"));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "changed_select_fired;"));
EXPECT_EQ(false,
content::EvalJs(GetWebContents(), "unchanged_select_fired;"));
}
// Test that a JavaScript onchange event is fired after auto-filling a form.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, OnChangeAfterAutofill) {
static const char kOnChangeScript[] =
R"( <script>
focused_fired = false;
unfocused_fired = false;
changed_select_fired = false;
unchanged_select_fired = false;
document.getElementById('firstname').onchange = function() {
focused_fired = true;
};
document.getElementById('lastname').onchange = function() {
unfocused_fired = true;
};
document.getElementById('state').onchange = function() {
changed_select_fired = true;
};
document.getElementById('country').onchange = function() {
unchanged_select_fired = true;
};
document.getElementById('country').value = 'US';
</script> )";
CreateTestProfile();
SetTestUrlResponse(base::StrCat({kTestShippingFormString, kOnChangeScript}));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.show_method = ShowMethod::ByChar('M')}));
// The form should be filled.
EXPECT_THAT(GetFormValues(), ValuesAre(kDefaultAddress));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "focused_fired;"));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "unfocused_fired;"));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "changed_select_fired;"));
EXPECT_EQ(false,
content::EvalJs(GetWebContents(), "unchanged_select_fired;"));
}
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, InputFiresBeforeChange) {
static const char kInputFiresBeforeChangeScript[] =
R"(<script>
inputElementEvents = [];
function recordInputElementEvent(e) {
if (e.target.tagName != 'INPUT') throw 'only <input> tags allowed';
inputElementEvents.push(e.type);
}
selectElementEvents = [];
function recordSelectElementEvent(e) {
if (e.target.tagName != 'SELECT') throw 'only <select> tags allowed';
selectElementEvents.push(e.type);
}
document.getElementById('lastname').oninput = recordInputElementEvent;
document.getElementById('lastname').onchange = recordInputElementEvent;
document.getElementById('country').oninput = recordSelectElementEvent;
document.getElementById('country').onchange = recordSelectElementEvent;
</script>)";
CreateTestProfile();
SetTestUrlResponse(
base::StrCat({kTestShippingFormString, kInputFiresBeforeChangeScript}));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.show_method = ShowMethod::ByChar('M'),
.after_select = ExpectValues(MergeValue(
kEmptyAddress, {"firstname", "M"}))}));
EXPECT_THAT(GetFormValues(), ValuesAre(kDefaultAddress));
EXPECT_EQ(2, content::EvalJs(GetWebContents(), "inputElementEvents.length;"));
std::vector<std::string> input_element_events = {
content::EvalJs(GetWebContents(), "inputElementEvents[0];")
.ExtractString(),
content::EvalJs(GetWebContents(), "inputElementEvents[1];")
.ExtractString(),
};
EXPECT_THAT(input_element_events, ElementsAre("input", "change"));
EXPECT_EQ(2,
content::EvalJs(GetWebContents(), "selectElementEvents.length;"));
std::vector<std::string> select_element_events = {
content::EvalJs(GetWebContents(), "selectElementEvents[0];")
.ExtractString(),
content::EvalJs(GetWebContents(), "selectElementEvents[1];")
.ExtractString(),
};
EXPECT_THAT(select_element_events, ElementsAre("input", "change"));
}
// Test that we can autofill forms distinguished only by their `id` attribute.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
AutofillFormsDistinguishedById) {
static const char kScript[] =
R"( <script>
var mainForm = document.forms[0];
mainForm.id = 'mainForm';
var newForm = document.createElement('form');
newForm.action = mainForm.action;
newForm.method = mainForm.method;
newForm.id = 'newForm';
mainForm.parentNode.insertBefore(newForm, mainForm);
</script> )";
CreateTestProfile();
SetTestUrlResponse(base::StrCat({kTestShippingFormString, kScript}));
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.show_method = ShowMethod::ByChar('M'),
.after_select = ExpectValues(
MergeValue(kEmptyAddress, {"firstname", "M"}),
GetElementById("mainForm"))}));
EXPECT_THAT(GetFormValues(GetElementById("mainForm")),
ValuesAre(kDefaultAddress));
}
// Test that we properly autofill forms with repeated fields.
// In the wild, the repeated fields are typically either email fields
// (duplicated for "confirmation"); or variants that are hot-swapped via
// JavaScript, with only one actually visible at any given time.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillFormWithRepeatedField) {
static const char kForm[] =
R"( <form action="https://www.example.com/" method="POST">
<label for="firstname">First name:</label>
<input type="text" id="firstname"><br>
<label for="lastname">Last name:</label>
<input type="text" id="lastname"><br>
<label for="address1">Address line 1:</label>
<input type="text" id="address1"><br>
<label for="address2">Address line 2:</label>
<input type="text" id="address2"><br>
<label for="city">City:</label>
<input type="text" id="city"><br>
<label for="state">State:</label>
<select id="state">
<option value="" selected="yes">--</option>
<option value="CA">California</option>
<option value="TX">Texas</option>
</select><br>
<label for="state_freeform" style="display:none">State:</label>
<input type="text" id="state_freeform" style="display:none"><br>
<label for="zip">ZIP code:</label>
<input type="text" id="zip"><br>
<label for="country">Country:</label>
<select style="appearance:base-select" id="country">
<option value="" selected="yes">--</option>
<option value="CA">Canada</option>
<option value="US">United States</option>
</select><br>
<label for="phone">Phone number:</label>
<input type="text" id="phone"><br>
</form> )";
CreateTestProfile();
SetTestUrlResponse(kForm);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
std::vector<FieldValue> empty = kEmptyAddress;
empty.insert(empty.begin() + 6, {"state_freeform", ""});
std::vector<FieldValue> filled = kDefaultAddress;
filled.insert(filled.begin() + 6, {"state_freeform", ""});
ASSERT_TRUE(AutofillFlow(
GetElementById("firstname"), this,
{.show_method = ShowMethod::ByChar('M'),
.after_select = ExpectValues(MergeValue(empty, {"firstname", "M"}),
ElementExpr("document.forms[0]"))}));
EXPECT_THAT(GetFormValues(ElementExpr("document.forms[0]")),
ValuesAre(filled));
}
// Test that we properly autofill forms with non-autofillable fields.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
AutofillFormWithAutocompleteOffField) {
static const char kForm[] =
R"( <form action="https://www.example.com/" method="POST">
<label for="firstname">First name:</label>
<input type="text" id="firstname"><br>
<label for="middlename">Middle name:</label>
<input type="text" id="middlename" autocomplete="off" /><br>
<label for="lastname">Last name:</label>
<input type="text" id="lastname"><br>
<label for="address1">Address line 1:</label>
<input type="text" id="address1"><br>
<label for="address2">Address line 2:</label>
<input type="text" id="address2"><br>
<label for="city">City:</label>
<input type="text" id="city"><br>
<label for="state">State:</label>
<select id="state">
<option value="" selected="yes">--</option>
<option value="CA">California</option>
<option value="TX">Texas</option>
</select><br>
<label for="zip">ZIP code:</label>
<input type="text" id="zip"><br>
<label for="country">Country:</label>
<select style="appearance:base-select" id="country">
<option value="" selected="yes">--</option>
<option value="CA">Canada</option>
<option value="US">United States</option>
</select><br>
<label for="phone">Phone number:</label>
<input type="text" id="phone"><br>
</form> )";
CreateTestProfile();
SetTestUrlResponse(kForm);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
std::vector<FieldValue> empty = kEmptyAddress;
empty.insert(empty.begin() + 1, {"middlename", ""});
std::vector<FieldValue> filled = kDefaultAddress;
filled.insert(filled.begin() + 1, {"middlename", "C."});
ASSERT_TRUE(AutofillFlow(
GetElementById("firstname"), this,
{.show_method = ShowMethod::ByChar('M'),
.after_select = ExpectValues(MergeValue(empty, {"firstname", "M"}),
ElementExpr("document.forms[0]"))}));
EXPECT_THAT(GetFormValues(ElementExpr("document.forms[0]")),
ValuesAre(filled));
}
// Test that we can Autofill dynamically generated forms.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, DynamicFormFill) {
static const char kDynamicForm[] =
R"( <p>Some text to paint</p>
<form id="form" action="https://www.example.com/"
method="POST"></form>
<script>
function AddElement(name, label) {
var form = document.getElementById('form');
var label_text = document.createTextNode(label);
var label_element = document.createElement('label');
label_element.setAttribute('for', name);
label_element.appendChild(label_text);
form.appendChild(label_element);
if (name === 'state' || name === 'country') {
var select_element = document.createElement('select');
select_element.setAttribute('id', name);
select_element.setAttribute('name', name);
/* Add an empty selected option. */
var default_option = new Option('--', '', true);
select_element.appendChild(default_option);
/* Add the other options. */
if (name == 'state') {
var option1 = new Option('California', 'CA');
select_element.appendChild(option1);
var option2 = new Option('Texas', 'TX');
select_element.appendChild(option2);
} else {
var option1 = new Option('Canada', 'CA');
select_element.appendChild(option1);
var option2 = new Option('United States', 'US');
select_element.appendChild(option2);
}
form.appendChild(select_element);
} else {
var input_element = document.createElement('input');
input_element.setAttribute('id', name);
input_element.setAttribute('name', name);
form.appendChild(input_element);
}
form.appendChild(document.createElement('br'));
};
function BuildForm() {
var elements = [
['firstname', 'First name:'],
['lastname', 'Last name:'],
['address1', 'Address line 1:'],
['address2', 'Address line 2:'],
['city', 'City:'],
['state', 'State:'],
['zip', 'ZIP code:'],
['country', 'Country:'],
['phone', 'Phone number:'],
];
for (var i = 0; i < elements.length; i++) {
var name = elements[i][0];
var label = elements[i][1];
AddElement(name, label);
}
};
</script> )";
CreateTestProfile();
SetTestUrlResponse(kDynamicForm);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
// Dynamically construct the form.
ASSERT_TRUE(content::ExecJs(GetWebContents(), "BuildForm();"));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.show_method = ShowMethod::ByChar('M'),
.after_select = ExpectValues(
MergeValue(kEmptyAddress, {"firstname", "M"}),
GetElementById("form"))}));
EXPECT_THAT(GetFormValues(GetElementById("form")),
ValuesAre(kDefaultAddress));
}
// Test that form filling works after reloading the current page.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillAfterReload) {
CreateTestProfile();
SetTestUrlResponse(kTestShippingFormString);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
// Reload the page.
content::WebContents* web_contents = GetWebContents();
web_contents->GetController().Reload(content::ReloadType::NORMAL, false);
EXPECT_TRUE(content::WaitForLoadStop(web_contents));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.show_method = ShowMethod::ByChar('M'),
.after_select = ExpectValues(MergeValue(
kEmptyAddress, {"firstname", "M"}))}));
EXPECT_THAT(GetFormValues(), ValuesAre(kDefaultAddress));
}
// Test that filling a form sends all the expected events to the different
// fields being filled.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillEvents) {
// TODO(crbug.com/40468256): Remove the autocomplete attribute from the
// textarea field when the bug is fixed.
static const char kTestEventFormString[] =
R"( <script type="text/javascript">
var inputfocus = false;
var inputkeydown = false;
var inputinput = false;
var inputchange = false;
var inputkeyup = false;
var inputblur = false;
var textfocus = false;
var textkeydown = false;
var textinput= false;
var textchange = false;
var textkeyup = false;
var textblur = false;
var selectfocus = false;
var selectinput = false;
var selectchange = false;
var selectblur = false;
</script>
A form for testing events.
<form action="https://www.example.com/" method="POST" id="shipping">
<label for="firstname">First name:</label>
<input type="text" id="firstname"><br>
<label for="lastname">Last name:</label>
<input type="text" id="lastname"
onfocus="inputfocus = true" onkeydown="inputkeydown = true"
oninput="inputinput = true" onchange="inputchange = true"
onkeyup="inputkeyup = true" onblur="inputblur = true" ><br>
<label for="address1">Address line 1:</label>
<input type="text" id="address1"><br>
<label for="address2">Address line 2:</label>
<input type="text" id="address2"><br>
<label for="city">City:</label>
<textarea rows="4" cols="50" id="city" name="city"
autocomplete="address-level2" onfocus="textfocus = true"
onkeydown="textkeydown = true" oninput="textinput = true"
onchange="textchange = true" onkeyup="textkeyup = true"
onblur="textblur = true"></textarea><br>
<label for="state">State:</label>
<select id="state"
onfocus="selectfocus = true" oninput="selectinput = true"
onchange="selectchange = true" onblur="selectblur = true" >
<option value="" selected="yes">--</option>
<option value="CA">California</option>
<option value="NY">New York</option>
<option value="TX">Texas</option>
</select><br>
<label for="zip">ZIP code:</label>
<input type="text" id="zip"><br>
<label for="country">Country:</label>
<select style="appearance:base-select" id="country">
<option value="" selected="yes">--</option>
<option value="CA">Canada</option>
<option value="US">United States</option>
</select><br>
<label for="phone">Phone number:</label>
<input type="text" id="phone"><br>
</form> )";
CreateTestProfile();
SetTestUrlResponse(kTestEventFormString);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.show_method = ShowMethod::ByChar('M'),
.after_select = ExpectValues(MergeValue(
kEmptyAddress, {"firstname", "M"}))}));
EXPECT_THAT(GetFormValues(), ValuesAre(kDefaultAddress));
// Checks that all the events were fired for the input field.
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "inputfocus;"));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "inputkeydown;"));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "inputinput;"));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "inputchange;"));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "inputkeyup;"));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "inputblur;"));
// Checks that all the events were fired for the textarea field.
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "textfocus;"));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "textkeydown;"));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "textinput;"));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "textchange;"));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "textkeyup;"));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "textblur;"));
// Checks that all the events were fired for the select field.
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "selectfocus;"));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "selectinput;"));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "selectchange;"));
EXPECT_EQ(true, content::EvalJs(GetWebContents(), "selectblur;"));
}
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, AutofillAfterTranslate) {
ASSERT_TRUE(TranslateService::IsTranslateBubbleEnabled());
translate::TranslateManager::SetIgnoreMissingKeyForTesting(true);
CreateTestProfile();
static const char kForm[] =
R"( <form action="https://www.example.com/" method="POST">
<label for="fn">Nom</label>
<input type="text" id="fn"><br>
<label for="ln">Nom de famille</label>
<input type="text" id="ln"><br>
<label for="a1">Address line 1:</label>
<input type="text" id="a1"><br>
<label for="a2">Address line 2:</label>
<input type="text" id="a2"><br>
<label for="ci">City:</label>
<input type="text" id="ci"><br>
<label for="st">State:</label>
<select id="st">
<option value="" selected="yes">--</option>
<option value="CA">California</option>
<option value="TX">Texas</option>
</select><br>
<label for="z">ZIP code:</label>
<input type="text" id="z"><br>
<label for="co">Country:</label>
<select style="appearance:base-select" id="co">
<option value="" selected="yes">--</option>
<option value="CA">Canada</option>
<option value="US">United States</option>
</select><br>
<label for="ph">Phone number:</label>
<input type="text" id="ph"><br>
</form>
Nous serons importants et intéressants, mais les épreuves et les
peines peuvent lui en procurer de grandes en raison de situations
occasionnelles.
Puis quelques avantages
)";
// The above additional French words ensure the translate bar will appear.
//
// TODO(crbug.com/40200965): The current translate testing overrides the
// result to be Adopted Language: 'fr' (the language the Chrome's
// translate feature believes the page language to be in). The behavior
// required here is to only force a translation which should not rely on
// language detection. The override simply just seeds the translate code
// so that a translate event occurs in a more testable way.
NavigateToContentAndWaitForLanguageDetection(kForm);
ASSERT_EQ("fr", GetLanguageState().current_language());
ASSERT_NO_FATAL_FAILURE(Translate(true));
ASSERT_EQ("fr", GetLanguageState().source_language());
ASSERT_EQ("en", GetLanguageState().current_language());
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.show_method = ShowMethod::ByChar('M'),
.after_select = ExpectValues(MergeValue(
kEmptyAddress, {"firstname", "M"}))}));
EXPECT_THAT(GetFormValues(), ValuesAre(kDefaultAddress));
}
// Test phone fields parse correctly from a given profile.
// The high level key presses execute the following: Select the first text
// field, invoke the autofill popup list, select the first profile within the
// list, and commit to the profile to populate the form.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, ComparePhoneNumbers) {
AutofillProfile profile(AddressCountryCode("US"));
profile.SetRawInfo(NAME_FIRST, u"Bob");
profile.SetRawInfo(NAME_LAST, u"Smith");
profile.SetRawInfo(ADDRESS_HOME_LINE1, u"1234 H St.");
profile.SetRawInfo(ADDRESS_HOME_CITY, u"San Jose");
profile.SetRawInfo(ADDRESS_HOME_STATE, u"CA");
profile.SetRawInfo(ADDRESS_HOME_ZIP, u"95110");
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, u"1-408-555-4567");
AddTestProfile(browser()->profile(), profile);
GURL url = embedded_test_server()->GetURL("/autofill/form_phones.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(GetElementById("NAME_FIRST1"), this));
EXPECT_EQ("Bob", GetFieldValueById("NAME_FIRST1"));
EXPECT_EQ("Smith", GetFieldValueById("NAME_LAST1"));
EXPECT_EQ("1234 H St.", GetFieldValueById("ADDRESS_HOME_LINE1"));
EXPECT_EQ("San Jose", GetFieldValueById("ADDRESS_HOME_CITY"));
EXPECT_EQ("CA", GetFieldValueById("ADDRESS_HOME_STATE"));
EXPECT_EQ("95110", GetFieldValueById("ADDRESS_HOME_ZIP"));
EXPECT_EQ("14085554567", GetFieldValueById("PHONE_HOME_WHOLE_NUMBER"));
ASSERT_TRUE(AutofillFlow(GetElementById("NAME_FIRST2"), this));
EXPECT_EQ("Bob", GetFieldValueById("NAME_FIRST2"));
EXPECT_EQ("Smith", GetFieldValueById("NAME_LAST2"));
EXPECT_EQ("408", GetFieldValueById("PHONE_HOME_CITY_CODE-1"));
EXPECT_EQ("5554567", GetFieldValueById("PHONE_HOME_NUMBER"));
ASSERT_TRUE(AutofillFlow(GetElementById("NAME_FIRST3"), this));
EXPECT_EQ("Bob", GetFieldValueById("NAME_FIRST3"));
EXPECT_EQ("Smith", GetFieldValueById("NAME_LAST3"));
EXPECT_EQ("408", GetFieldValueById("PHONE_HOME_CITY_CODE-2"));
EXPECT_EQ("555", GetFieldValueById("PHONE_HOME_NUMBER_3-1"));
EXPECT_EQ("4567", GetFieldValueById("PHONE_HOME_NUMBER_4-1"));
EXPECT_EQ("", GetFieldValueById("PHONE_HOME_EXT-1"));
ASSERT_TRUE(AutofillFlow(GetElementById("NAME_FIRST4"), this));
EXPECT_EQ("Bob", GetFieldValueById("NAME_FIRST4"));
EXPECT_EQ("Smith", GetFieldValueById("NAME_LAST4"));
EXPECT_EQ("1", GetFieldValueById("PHONE_HOME_COUNTRY_CODE-1"));
EXPECT_EQ("408", GetFieldValueById("PHONE_HOME_CITY_CODE-3"));
EXPECT_EQ("555", GetFieldValueById("PHONE_HOME_NUMBER_3-2"));
EXPECT_EQ("4567", GetFieldValueById("PHONE_HOME_NUMBER_4-2"));
EXPECT_EQ("", GetFieldValueById("PHONE_HOME_EXT-2"));
}
// Test that Autofill does not fill in Company Name if disabled
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, NoAutofillForCompanyName) {
std::string addr_line1("1234 H St.");
std::string company_name("Company X");
AutofillProfile profile(i18n_model_definition::kLegacyHierarchyCountryCode);
profile.SetRawInfo(NAME_FIRST, u"Bob");
profile.SetRawInfo(NAME_LAST, u"Smith");
profile.SetRawInfo(EMAIL_ADDRESS, u"bsmith@gmail.com");
profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16(addr_line1));
profile.SetRawInfo(ADDRESS_HOME_CITY, u"San Jose");
profile.SetRawInfo(ADDRESS_HOME_STATE, u"CA");
profile.SetRawInfo(ADDRESS_HOME_ZIP, u"95110");
profile.SetRawInfo(COMPANY_NAME, ASCIIToUTF16(company_name));
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, u"408-871-4567");
AddTestProfile(browser()->profile(), profile);
GURL url =
embedded_test_server()->GetURL("/autofill/read_only_field_test.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
EXPECT_EQ(addr_line1, GetFieldValueById("address"));
EXPECT_EQ(company_name, GetFieldValueById("company"));
}
// TODO(crbug.com/40810623): Check back if flakiness is fixed now.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
NoAutofillSuggestionForCompanyName) {
static const char kTestShippingFormWithCompanyString[] = R"(
An example of a shipping address form.
<form action="https://www.example.com/" method="POST">
<label for="firstname">First name:</label>
<input type="text" id="firstname"><br>
<label for="lastname">Last name:</label>
<input type="text" id="lastname"><br>
<label for="address1">Address line 1:</label>
<input type="text" id="address1"><br>
<label for="address2">Address line 2:</label>
<input type="text" id="address2"><br>
<label for="city">City:</label>
<input type="text" id="city"><br>
<label for="state">State:</label>
<select id="state">
<option value="" selected="yes">--</option>
<option value="CA">California</option>
<option value="TX">Texas</option>
</select><br>
<label for="zip">ZIP code:</label>
<input type="text" id="zip"><br>
<label for="country">Country:</label>
<select style="appearance:base-select" id="country">
<option value="" selected="yes">--</option>
<option value="CA">Canada</option>
<option value="US">United States</option>
</select><br>
<label for="phone">Phone number:</label>
<input type="text" id="phone"><br>
<label for="company">First company:</label>
<input type="text" id="company"><br>
</form>
)";
CreateTestProfile();
SetTestUrlResponse(kTestShippingFormWithCompanyString);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this,
{.show_method = ShowMethod::ByClick(),
.execution_target = GetWebContents()}));
EXPECT_EQ("Initech", GetFieldValueById("company"));
}
// Test that Autofill does not fill in read-only fields.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, NoAutofillForReadOnlyFields) {
std::string addr_line1("1234 H St.");
AutofillProfile profile(i18n_model_definition::kLegacyHierarchyCountryCode);
profile.SetRawInfo(NAME_FIRST, u"Bob");
profile.SetRawInfo(NAME_LAST, u"Smith");
profile.SetRawInfo(EMAIL_ADDRESS, u"bsmith@gmail.com");
profile.SetRawInfo(ADDRESS_HOME_LINE1, ASCIIToUTF16(addr_line1));
profile.SetRawInfo(ADDRESS_HOME_CITY, u"San Jose");
profile.SetRawInfo(ADDRESS_HOME_STATE, u"CA");
profile.SetRawInfo(ADDRESS_HOME_ZIP, u"95110");
profile.SetRawInfo(COMPANY_NAME, u"Company X");
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, u"408-871-4567");
AddTestProfile(browser()->profile(), profile);
GURL url =
embedded_test_server()->GetURL("/autofill/read_only_field_test.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
EXPECT_EQ("", GetFieldValueById("email"));
EXPECT_EQ(addr_line1, GetFieldValueById("address"));
}
// Test form is fillable from a profile after form was reset.
// Steps:
// 1. Fill form using a saved profile.
// 2. Reset the form.
// 3. Fill form using a saved profile.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, FormFillableOnReset) {
CreateTestProfile();
GURL url =
embedded_test_server()->GetURL("/autofill/autofill_test_form.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(GetElementById("NAME_FIRST"), this));
ASSERT_TRUE(content::ExecJs(GetWebContents(),
"document.getElementById('testform').reset()"));
ASSERT_TRUE(AutofillFlow(GetElementById("NAME_FIRST"), this));
EXPECT_EQ("Milton", GetFieldValueById("NAME_FIRST"));
EXPECT_EQ("Waddams", GetFieldValueById("NAME_LAST"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("EMAIL_ADDRESS"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("ADDRESS_HOME_LINE1"));
EXPECT_EQ("Austin", GetFieldValueById("ADDRESS_HOME_CITY"));
EXPECT_EQ("Texas", GetFieldValueById("ADDRESS_HOME_STATE"));
EXPECT_EQ("78744", GetFieldValueById("ADDRESS_HOME_ZIP"));
EXPECT_EQ("United States", GetFieldValueById("ADDRESS_HOME_COUNTRY"));
EXPECT_EQ("5125551234", GetFieldValueById("PHONE_HOME_WHOLE_NUMBER"));
}
// Test Autofill distinguishes a middle initial in a name.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
DistinguishMiddleInitialWithinName) {
CreateTestProfile();
GURL url =
embedded_test_server()->GetURL("/autofill/autofill_middleinit_form.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(GetElementById("NAME_FIRST"), this));
EXPECT_EQ("C.", GetFieldValueById("NAME_MIDDLE"));
}
// Test forms with multiple email addresses are filled properly.
// Entire form should be filled with one user gesture.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
MultipleEmailFilledByOneUserGesture) {
std::string email("bsmith@gmail.com");
AutofillProfile profile(i18n_model_definition::kLegacyHierarchyCountryCode);
profile.SetRawInfo(NAME_FIRST, u"Bob");
profile.SetRawInfo(NAME_LAST, u"Smith");
profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16(email));
profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, u"4088714567");
AddTestProfile(browser()->profile(), profile);
GURL url = embedded_test_server()->GetURL(
"/autofill/autofill_confirmemail_form.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(GetElementById("NAME_FIRST"), this));
EXPECT_EQ(email, GetFieldValueById("EMAIL_CONFIRM"));
// TODO(isherman): verify entire form.
}
// Test latency time on form submit with lots of stored Autofill profiles.
// This test verifies when a profile is selected from the Autofill dictionary
// that consists of thousands of profiles, the form does not hang after being
// submitted.
// Flakily times out creating 1500 profiles: http://crbug.com/281527
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
DISABLED_FormFillLatencyAfterSubmit) {
std::vector<std::string> cities;
cities.push_back("San Jose");
cities.push_back("San Francisco");
cities.push_back("Sacramento");
cities.push_back("Los Angeles");
std::vector<std::string> streets;
streets.push_back("St");
streets.push_back("Ave");
streets.push_back("Ln");
streets.push_back("Ct");
constexpr int kNumProfiles = 1500;
for (int i = 0; i < kNumProfiles; i++) {
AutofillProfile profile(AddressCountryCode("US"));
std::u16string name(base::NumberToString16(i));
std::u16string email(name + u"@example.com");
std::u16string street =
ASCIIToUTF16(base::NumberToString(base::RandInt(0, 10000)) + " " +
streets[base::RandInt(0, streets.size() - 1)]);
std::u16string city =
ASCIIToUTF16(cities[base::RandInt(0, cities.size() - 1)]);
std::u16string zip(base::NumberToString16(base::RandInt(0, 10000)));
profile.SetRawInfo(NAME_FIRST, name);
profile.SetRawInfo(EMAIL_ADDRESS, email);
profile.SetRawInfo(ADDRESS_HOME_LINE1, street);
profile.SetRawInfo(ADDRESS_HOME_CITY, city);
profile.SetRawInfo(ADDRESS_HOME_STATE, u"CA");
profile.SetRawInfo(ADDRESS_HOME_ZIP, zip);
AddTestProfile(browser()->profile(), profile);
}
GURL url = embedded_test_server()->GetURL(
"/autofill/latency_after_submit_test.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(GetElementById("NAME_FIRST"), this));
content::LoadStopObserver load_stop_observer(GetWebContents());
ASSERT_TRUE(content::ExecJs(GetWebContents(),
"document.getElementById('testform').submit();"));
// This will ensure the test didn't hang.
load_stop_observer.Wait();
}
// Test that Chrome doesn't crash when autocomplete is disabled while the user
// is interacting with the form. This is a regression test for
// http://crbug.com/160476
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
DisableAutocompleteWhileFilling) {
CreateTestProfile();
SetTestUrlResponse(kTestShippingFormString);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
// When suggestions are shown, disable autocomplete for the active field.
auto SetAutocompleteOff = [this] {
ASSERT_TRUE(content::ExecJs(
GetWebContents(),
"document.querySelector('input').autocomplete = 'off';"));
};
ASSERT_TRUE(AutofillFlow(
GetElementById("firstname"), this,
{.show_method = ShowMethod::ByChar('M'),
.after_select = base::BindLambdaForTesting(SetAutocompleteOff)}));
}
// Test that a page with 2 forms with no name and id containing fields with no
// name or if get filled properly.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
FillFormAndFieldWithNoNameOrId) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"/autofill/forms_without_identifiers.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
auto name = ElementExpr("document.forms[1].elements[0]");
auto email = ElementExpr("document.forms[1].elements[1]");
ASSERT_TRUE(
AutofillFlow(name, this, {.show_method = ShowMethod::ByChar('M')}));
EXPECT_EQ("Milton C. Waddams", GetFieldValue(name));
EXPECT_EQ("red.swingline@initech.com", GetFieldValue(email));
}
// The following four tests verify that we can autofill forms with multiple
// nameless forms, and repetitive field names and make sure that the dynamic
// refill would not trigger a wrong refill, regardless of the form.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
Dynamic_MultipleNoNameForms_BadNames_FourthForm) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/multiple_noname_forms_badnames.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname_4"), this));
DoNothingAndWait(base::Seconds(2)); // Wait to for possible refills.
EXPECT_EQ("", GetFieldValueById("firstname_1"));
EXPECT_EQ("", GetFieldValueById("lastname_1"));
EXPECT_EQ("", GetFieldValueById("email_1"));
EXPECT_EQ("", GetFieldValueById("firstname_2"));
EXPECT_EQ("", GetFieldValueById("lastname_2"));
EXPECT_EQ("", GetFieldValueById("email_2"));
EXPECT_EQ("", GetFieldValueById("firstname_3"));
EXPECT_EQ("", GetFieldValueById("lastname_3"));
EXPECT_EQ("", GetFieldValueById("email_3"));
EXPECT_EQ("Milton", GetFieldValueById("firstname_4"));
EXPECT_EQ("Waddams", GetFieldValueById("lastname_4"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email_4"));
}
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
Dynamic_MultipleNoNameForms_BadNames_ThirdForm) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/multiple_noname_forms_badnames.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname_3"), this));
DoNothingAndWait(base::Seconds(2)); // Wait to for possible refills.
EXPECT_EQ("", GetFieldValueById("firstname_1"));
EXPECT_EQ("", GetFieldValueById("lastname_1"));
EXPECT_EQ("", GetFieldValueById("email_1"));
EXPECT_EQ("", GetFieldValueById("firstname_2"));
EXPECT_EQ("", GetFieldValueById("lastname_2"));
EXPECT_EQ("", GetFieldValueById("email_2"));
EXPECT_EQ("Milton", GetFieldValueById("firstname_3"));
EXPECT_EQ("Waddams", GetFieldValueById("lastname_3"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email_3"));
EXPECT_EQ("", GetFieldValueById("firstname_4"));
EXPECT_EQ("", GetFieldValueById("lastname_4"));
EXPECT_EQ("", GetFieldValueById("email_4"));
}
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
Dynamic_MultipleNoNameForms_BadNames_SecondForm) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/multiple_noname_forms_badnames.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname_2"), this));
DoNothingAndWait(base::Seconds(2)); // Wait to for possible refills.
EXPECT_EQ("", GetFieldValueById("firstname_1"));
EXPECT_EQ("", GetFieldValueById("lastname_1"));
EXPECT_EQ("", GetFieldValueById("email_1"));
EXPECT_EQ("Milton", GetFieldValueById("firstname_2"));
EXPECT_EQ("Waddams", GetFieldValueById("lastname_2"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email_2"));
EXPECT_EQ("", GetFieldValueById("firstname_3"));
EXPECT_EQ("", GetFieldValueById("lastname_3"));
EXPECT_EQ("", GetFieldValueById("email_3"));
}
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest,
Dynamic_MultipleNoNameForms_BadNames_FirstForm) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/multiple_noname_forms_badnames.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname_1"), this));
DoNothingAndWait(base::Seconds(2)); // Wait to for possible refills.
EXPECT_EQ("Milton", GetFieldValueById("firstname_1"));
EXPECT_EQ("Waddams", GetFieldValueById("lastname_1"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email_1"));
EXPECT_EQ("", GetFieldValueById("firstname_2"));
EXPECT_EQ("", GetFieldValueById("lastname_2"));
EXPECT_EQ("", GetFieldValueById("email_2"));
EXPECT_EQ("", GetFieldValueById("firstname_3"));
EXPECT_EQ("", GetFieldValueById("lastname_3"));
EXPECT_EQ("", GetFieldValueById("email_3"));
}
// Test that we can Autofill forms where some fields name change during the
// fill.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, FieldsChangeName) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"/autofill/field_changing_name_during_fill.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
EXPECT_EQ("Milton", GetFieldValueById("firstname"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address"));
EXPECT_EQ("TX", GetFieldValueById("state"));
EXPECT_EQ("Austin", GetFieldValueById("city"));
EXPECT_EQ("Initech", GetFieldValueById("company"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email"));
EXPECT_EQ("5125551234", GetFieldValueById("phone"));
}
// Test that credit card autofill works.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestBase, FillLocalCreditCard) {
CreateTestCreditCart();
GURL url = https_server()->GetURL("a.com",
"/autofill/autofill_creditcard_form.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(GetElementById("CREDIT_CARD_NAME_FULL"), this));
EXPECT_EQ("Milton Waddams", GetFieldValueById("CREDIT_CARD_NAME_FULL"));
EXPECT_EQ("4111111111111111", GetFieldValueById("CREDIT_CARD_NUMBER"));
EXPECT_EQ("09", GetFieldValueById("CREDIT_CARD_EXP_MONTH"));
EXPECT_EQ("2999", GetFieldValueById("CREDIT_CARD_EXP_4_DIGIT_YEAR"));
}
// Test that we do not fill formless non-checkout forms when we enable the
// formless form restrictions.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestBase, NoAutocomplete) {
CreateTestProfile();
GURL url =
embedded_test_server()->GetURL("/autofill/formless_no_autocomplete.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
EXPECT_EQ("Milton", GetFieldValueById("firstname"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address"));
EXPECT_EQ("TX", GetFieldValueById("state"));
EXPECT_EQ("Austin", GetFieldValueById("city"));
EXPECT_EQ("Initech", GetFieldValueById("company"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email"));
EXPECT_EQ("5125551234", GetFieldValueById("phone"));
}
// Test that we do not fill formless non-checkout forms when we enable the
// formless form restrictions. This test differs from the NoAutocomplete
// version of the the test in that at least one of the fields has an
// autocomplete attribute, so autofill will always be aware of the existence
// of the form.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestBase, SomeAutocomplete) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"/autofill/formless_some_autocomplete.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
EXPECT_EQ("Milton", GetFieldValueById("firstname"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address"));
EXPECT_EQ("TX", GetFieldValueById("state"));
EXPECT_EQ("Austin", GetFieldValueById("city"));
EXPECT_EQ("Initech", GetFieldValueById("company"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email"));
EXPECT_EQ("5125551234", GetFieldValueById("phone"));
}
// Test that we do not fill formless non-checkout forms when we enable the
// formless form restrictions.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestBase, AllAutocomplete) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"/autofill/formless_all_autocomplete.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
EXPECT_EQ("Milton", GetFieldValueById("firstname"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address"));
EXPECT_EQ("TX", GetFieldValueById("state"));
EXPECT_EQ("Austin", GetFieldValueById("city"));
EXPECT_EQ("Initech", GetFieldValueById("company"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email"));
EXPECT_EQ("15125551234", GetFieldValueById("phone"));
}
// An extension of the test fixture for tests with site isolation.
class AutofillInteractiveIsolationTest : public AutofillInteractiveTestBase {
protected:
AutofillInteractiveIsolationTest() = default;
~AutofillInteractiveIsolationTest() override = default;
private:
void SetUpCommandLine(base::CommandLine* command_line) override {
AutofillInteractiveTestBase::SetUpCommandLine(command_line);
// Append --site-per-process flag.
content::IsolateAllSitesForTesting(command_line);
}
};
enum class FrameType { kIFrame, kFencedFrame };
class AutofillInteractiveFencedFrameTest
: public AutofillInteractiveIsolationTest,
public ::testing::WithParamInterface<FrameType> {
protected:
AutofillInteractiveFencedFrameTest() {
std::vector<base::test::FeatureRefAndParams> enabled;
std::vector<base::test::FeatureRef> disabled;
if (GetParam() != FrameType::kIFrame) {
enabled.push_back({network::features::kBrowsingTopics, {}});
enabled.push_back({blink::features::kFencedFramesAPIChanges, {}});
scoped_feature_list_.InitWithFeaturesAndParameters(enabled, disabled);
fenced_frame_test_helper_ =
std::make_unique<content::test::FencedFrameTestHelper>();
}
}
~AutofillInteractiveFencedFrameTest() override = default;
content::RenderFrameHost* primary_main_frame_host() {
return GetWebContents()->GetPrimaryMainFrame();
}
content::RenderFrameHost* LoadSubFrame(std::string relative_url) {
GURL frame_url = https_server()->GetURL(
"b.com", (GetParam() == FrameType::kIFrame ? "" : "/fenced_frames") +
relative_url);
switch (GetParam()) {
case FrameType::kIFrame: {
EXPECT_TRUE(content::NavigateIframeToURL(GetWebContents(), "crossFrame",
frame_url));
// TODO(crbug.com/40838553) Use AutofillManager::OnFormParsed instead of
// DoNothingAndWait.
// Wait to make sure the cross-frame form is parsed.
DoNothingAndWait(base::Seconds(2));
content::RenderFrameHost* cross_frame =
RenderFrameHostForName(GetWebContents(), "crossFrame");
return cross_frame;
}
case FrameType::kFencedFrame: {
// Creates a <fencedframe> element in the renderer.
content::RenderFrameHost* cross_frame =
fenced_frame_test_helper_->CreateFencedFrame(
primary_main_frame_host(), frame_url);
// TODO(crbug.com/40838553) Use AutofillManager::OnFormParsed instead of
// DoNothingAndWait.
// Wait to make sure the cross-frame form is parsed.
DoNothingAndWait(base::Seconds(2));
return cross_frame;
}
}
NOTREACHED();
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
std::unique_ptr<content::test::FencedFrameTestHelper>
fenced_frame_test_helper_;
};
INSTANTIATE_TEST_SUITE_P(AutofillInteractiveTest,
AutofillInteractiveFencedFrameTest,
::testing::Values(FrameType::kFencedFrame,
FrameType::kIFrame));
IN_PROC_BROWSER_TEST_P(AutofillInteractiveFencedFrameTest,
SimpleCrossSiteFill) {
test_delegate()->SetIgnoreBackToBackMessages(
ObservedUiEvents::kPreviewFormData, true);
CreateTestProfile();
// Main frame is on a.com, iframe/fenced frame is on b.com.
GURL url =
https_server()->GetURL("a.com", "/autofill/cross_origin_iframe.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
content::RenderFrameHost* cross_frame_host =
LoadSubFrame("/autofill/autofill_test_form.html");
ASSERT_TRUE(cross_frame_host);
ContentAutofillDriver* cross_driver =
ContentAutofillDriver::GetForRenderFrameHost(cross_frame_host);
ASSERT_TRUE(cross_driver);
// Let `test_delegate()` also observe autofill events in the iframe.
test_delegate()->Observe(cross_driver->GetAutofillManager());
ASSERT_TRUE(AutofillFlow(GetElementById("NAME_FIRST"), this,
{.execution_target = cross_frame_host}));
EXPECT_EQ("Milton",
GetFieldValue(GetElementById("NAME_FIRST"), cross_frame_host));
}
// This test verifies that credit card (payment card list) popup works when the
// form is inside an OOPIF/Fenced Frame.
IN_PROC_BROWSER_TEST_P(AutofillInteractiveFencedFrameTest,
CrossSitePaymentForms) {
CreateTestCreditCart();
// Main frame is on a.com, iframe/fenced frame is on b.com.
GURL url =
https_server()->GetURL("a.com", "/autofill/cross_origin_iframe.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
content::RenderFrameHost* cross_frame_host =
LoadSubFrame("/autofill/autofill_creditcard_form.html");
ASSERT_TRUE(cross_frame_host);
ContentAutofillDriver* cross_driver =
ContentAutofillDriver::GetForRenderFrameHost(cross_frame_host);
ASSERT_TRUE(cross_driver);
// Let `test_delegate()` also observe autofill events in the iframe.
test_delegate()->Observe(cross_driver->GetAutofillManager());
auto Wait = [this] { DoNothingAndWait(base::Seconds(2)); };
ASSERT_TRUE(AutofillFlow(GetElementById("CREDIT_CARD_NUMBER"), this,
{.after_focus = base::BindLambdaForTesting(Wait),
.execution_target = cross_frame_host}));
}
// Tests that deleting the subframe that has opened the Autofill popup closes
// the popup.
IN_PROC_BROWSER_TEST_P(AutofillInteractiveFencedFrameTest,
DeletingFrameClosesPopup) {
CreateTestProfile();
// Main frame is on a.com, fenced frame is on b.com.
ASSERT_TRUE(ui_test_utils::NavigateToURL(
browser(),
https_server()->GetURL("a.com", "/autofill/cross_origin_iframe.html")));
content::RenderFrameHost* cross_frame_host =
LoadSubFrame("/autofill/autofill_test_form.html");
ASSERT_TRUE(cross_frame_host);
// We need the fencedframe element to have id set to a known value
if (GetParam() != FrameType::kIFrame) {
ASSERT_TRUE(content::ExecJs(
GetWebContents(),
"document.getElementsByTagName('fencedframe')[0].id = 'crossFF';"));
}
ContentAutofillDriver* cross_driver =
ContentAutofillDriver::GetForRenderFrameHost(cross_frame_host);
ASSERT_TRUE(cross_driver);
// Let `test_delegate()` also observe autofill events in the iframe.
test_delegate()->Observe(cross_driver->GetAutofillManager());
// Open the Autofill popup but do not accept the suggestion yet. Deleting the
// subframe should close the popup.
ASSERT_TRUE(
AutofillFlow(GetElementById("NAME_FIRST"), this,
{.do_accept = false, .execution_target = cross_frame_host}));
// Do not accept the suggestion yet, to keep the pop-up shown.
EXPECT_TRUE(IsPopupShown());
// Delete the iframe/fenced frame.
std::string script_delete = base::StringPrintf(
"document.body.removeChild(document.getElementById('%s'))",
GetParam() == FrameType::kIFrame ? "crossFrame" : "crossFF");
ASSERT_TRUE(content::ExecJs(GetWebContents(), script_delete));
EXPECT_FALSE(IsPopupShown());
}
// Tests that when changing the tab while the popup is open, closes the popup.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTest, ChangingTabClosesPopup) {
CreateTestProfile();
SetTestUrlResponse(kTestShippingFormString);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
// Open the Autofill popup but do not accept the suggestion yet. Selecting
// another tab should close the popup.
content::WebContents* original_tab = GetWebContents();
ASSERT_TRUE(
AutofillFlow(GetElementById("firstname"), this, {.do_accept = false}));
EXPECT_TRUE(IsPopupShown());
AddBlankTabAndShow(browser());
ASSERT_NE(original_tab, GetWebContents());
chrome::CloseWebContents(browser(), GetWebContents(),
/*add_to_history=*/false);
ASSERT_EQ(original_tab, GetWebContents());
EXPECT_FALSE(IsPopupShown());
}
// Test fixture for refill behavior.
//
// BrowserAutofillManager only executes a refill if it happens within the time
// delta `kLimitBeforeRefill` of the original refill. On slow bots, this timeout
// may cause flakiness. Therefore, this fixture increases the limit before
// refill to an unreasonably large time.
class AutofillInteractiveTestDynamicForm : public AutofillInteractiveTest {
public:
void SetUpOnMainThread() override {
AutofillInteractiveTest::SetUpOnMainThread();
test_api(test_api(*GetBrowserAutofillManager()).form_filler())
.set_limit_before_refill(base::Hours(1));
}
ValueWaiter ListenForRefill(
const std::string& id,
std::optional<std::string> unblock_variable = "refill") {
return ListenForValueChange(id, unblock_variable, GetWebContents());
}
};
// Test that we can Autofill dynamically generated forms.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicChangingFormFill) {
CreateTestProfile();
GURL url =
embedded_test_server()->GetURL("a.com", "/autofill/dynamic_form.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill = ListenForRefill("firstname_form1");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
ASSERT_TRUE(std::move(refill).Wait());
// Make sure the new form was filled correctly.
EXPECT_EQ("Milton", GetFieldValueById("firstname_form1"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address_form1"));
EXPECT_EQ("TX", GetFieldValueById("state_form1"));
EXPECT_EQ("Austin", GetFieldValueById("city_form1"));
EXPECT_EQ("Initech", GetFieldValueById("company_form1"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email_form1"));
EXPECT_EQ("15125551234", GetFieldValueById("phone_form1"));
}
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
TwoDynamicChangingFormsFill) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL("a.com",
"/autofill/two_dynamic_forms.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill = ListenForRefill("firstname_form1");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname_form1"), this));
ASSERT_TRUE(std::move(refill).Wait());
// Make sure the new form was filled correctly.
EXPECT_EQ("Milton", GetFieldValueById("firstname_form1"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address_form1"));
EXPECT_EQ("TX", GetFieldValueById("state_form1"));
EXPECT_EQ("Austin", GetFieldValueById("city_form1"));
EXPECT_EQ("Initech", GetFieldValueById("company_form1"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email_form1"));
EXPECT_EQ("15125551234", GetFieldValueById("phone_form1"));
refill = ListenForRefill("firstname_form2");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname_form2"), this));
ASSERT_TRUE(std::move(refill).Wait());
// Make sure the new form was filled correctly.
EXPECT_EQ("Milton", GetFieldValueById("firstname_form2"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address_form2"));
EXPECT_EQ("TX", GetFieldValueById("state_form2"));
EXPECT_EQ("Austin", GetFieldValueById("city_form2"));
EXPECT_EQ("Initech", GetFieldValueById("company_form2"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email_form2"));
EXPECT_EQ("15125551234", GetFieldValueById("phone_form2"));
}
// Test that forms that dynamically change a second time do not get filled.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicChangingFormFill_SecondChange) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/double_dynamic_form.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill = ListenForRefill("firstname_form");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
ASSERT_FALSE(std::move(refill).Wait());
// Make sure the new form was not filled.
EXPECT_EQ("", GetFieldValueById("firstname_form"));
EXPECT_EQ("", GetFieldValueById("address_form"));
EXPECT_EQ("CA", GetFieldValueById("state_form")); // Default value.
EXPECT_EQ("", GetFieldValueById("city_form"));
EXPECT_EQ("", GetFieldValueById("company_form"));
EXPECT_EQ("", GetFieldValueById("email_form"));
EXPECT_EQ("", GetFieldValueById("phone_form"));
}
// Test that forms that dynamically change after the refill limit do not get
// filled.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicChangingFormFill_AfterDelay) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/dynamic_form_after_delay.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
// Lower the refill limit, so the test doesn't have to wait forever.
constexpr base::TimeDelta kLimitBeforeRefillForTest = base::Milliseconds(100);
test_api(test_api(*GetBrowserAutofillManager()).form_filler())
.set_limit_before_refill(kLimitBeforeRefillForTest);
ValueWaiter refill = ListenForRefill("firstname_form1");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
DoNothingAndWaitAndIgnoreEvents(kLimitBeforeRefillForTest +
base::Milliseconds(1));
ASSERT_FALSE(std::move(refill).Wait());
// Make sure that the new form was not filled.
EXPECT_EQ("", GetFieldValueById("firstname_form1"));
EXPECT_EQ("", GetFieldValueById("address_form1"));
EXPECT_EQ("CA", GetFieldValueById("state_form1")); // Default value.
EXPECT_EQ("", GetFieldValueById("city_form1"));
EXPECT_EQ("", GetFieldValueById("company_form1"));
EXPECT_EQ("", GetFieldValueById("email_form1"));
EXPECT_EQ("", GetFieldValueById("phone_form1"));
}
// Test that only field of a type group that was filled initially get refilled.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicChangingFormFill_AddsNewFieldTypeGroups) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/dynamic_form_new_field_types.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill = ListenForRefill("firstname_form1");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
ASSERT_TRUE(std::move(refill).Wait());
// The fields present in the initial fill should be filled.
EXPECT_EQ("Milton", GetFieldValueById("firstname_form1"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address_form1"));
EXPECT_EQ("TX", GetFieldValueById("state_form1"));
EXPECT_EQ("Austin", GetFieldValueById("city_form1"));
// Fields from group that were not present in the initial fill should not be
// filled
EXPECT_EQ("", GetFieldValueById("company_form1"));
// Fields that were present but hidden in the initial fill should not be
// filled.
EXPECT_EQ("", GetFieldValueById("email_form1"));
// The phone should be filled even if it's a different format than the initial
// fill.
EXPECT_EQ("5125551234", GetFieldValueById("phone_form1"));
}
// Test that we can autofill forms that dynamically change select fields to text
// fields by changing the visibilities.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicFormFill_SelectToText) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/dynamic_form_select_to_text.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill = ListenForRefill("firstname");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
ASSERT_TRUE(std::move(refill).Wait());
// Make sure the new form was filled correctly.
EXPECT_EQ("Milton", GetFieldValueById("firstname"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address1"));
EXPECT_EQ("Texas", GetFieldValueById("state_us"));
EXPECT_EQ("Austin", GetFieldValueById("city"));
EXPECT_EQ("Initech", GetFieldValueById("company"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email"));
EXPECT_EQ("5125551234", GetFieldValueById("phone"));
}
// Test that we can autofill forms that dynamically change the visibility of a
// field after it's autofilled.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicFormFill_VisibilitySwitch) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/dynamic_form_visibility_switch.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill = ListenForRefill("firstname");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
ASSERT_TRUE(std::move(refill).Wait());
// Make sure the new form was filled correctly.
EXPECT_EQ("Milton", GetFieldValueById("firstname"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address1"));
// Both fields must be filled after a refill.
EXPECT_EQ("Texas", GetFieldValueById("state_first"));
EXPECT_EQ("Texas", GetFieldValueById("state_second"));
EXPECT_EQ("Austin", GetFieldValueById("city"));
EXPECT_EQ("Initech", GetFieldValueById("company"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email"));
EXPECT_EQ("5125551234", GetFieldValueById("phone"));
}
// Test that we can autofill forms that dynamically change the element that
// has been clicked on.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicFormFill_FirstElementDisappears) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/dynamic_form_element_invalid.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill = ListenForRefill("address1");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
ASSERT_TRUE(std::move(refill).Wait());
// Make sure the new form was filled correctly.
EXPECT_EQ("Milton", GetFieldValueById("firstname2"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address1"));
EXPECT_EQ("Austin", GetFieldValueById("city"));
EXPECT_EQ("Initech", GetFieldValueById("company"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email"));
EXPECT_EQ("5125551234", GetFieldValueById("phone"));
}
// Test that we can autofill forms that dynamically change the element that
// has been clicked on, even though the form has no name.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicFormFill_FirstElementDisappearsNoNameForm) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/dynamic_form_element_invalid_noname_form.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill = ListenForRefill("address1");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
ASSERT_TRUE(std::move(refill).Wait());
// Make sure the new form was filled correctly.
EXPECT_EQ("Milton", GetFieldValueById("firstname2"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address1"));
EXPECT_EQ("Austin", GetFieldValueById("city"));
EXPECT_EQ("Initech", GetFieldValueById("company"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email"));
EXPECT_EQ("5125551234", GetFieldValueById("phone"));
}
// Test that we can autofill forms that dynamically change the element that
// has been clicked on, even though there are multiple forms with identical
// names.
IN_PROC_BROWSER_TEST_F(
AutofillInteractiveTestDynamicForm,
DynamicFormFill_FirstElementDisappearsMultipleBadNameForms) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com",
"/autofill/dynamic_form_element_invalid_multiple_badname_forms.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill = ListenForRefill("address1_7");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname_5"), this));
ASSERT_TRUE(std::move(refill).Wait());
// Make sure the second form was filled correctly, and the first form was left
// unfilled.
EXPECT_EQ("", GetFieldValueById("firstname_1"));
EXPECT_EQ("", GetFieldValueById("firstname_2"));
EXPECT_EQ("", GetFieldValueById("address1_3"));
EXPECT_EQ("CA", GetFieldValueById("country_4")); // default
EXPECT_EQ("Milton", GetFieldValueById("firstname_6"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address1_7"));
EXPECT_EQ("US", GetFieldValueById("country_8"));
}
// Test that we can autofill forms that dynamically change the element that
// has been clicked on, even though there are multiple forms with identical
// names.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicFormFill_FirstElementDisappearsBadnameUnowned) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/dynamic_form_element_invalid_unowned_badnames.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill = ListenForRefill("address1_7");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname_5"), this));
ASSERT_TRUE(std::move(refill).Wait());
// Make sure the second form was filled correctly, and the first form was left
// unfilled.
EXPECT_EQ("", GetFieldValueById("firstname_1"));
EXPECT_EQ("", GetFieldValueById("firstname_2"));
EXPECT_EQ("", GetFieldValueById("address1_3"));
EXPECT_EQ("CA", GetFieldValueById("country_4")); // default
EXPECT_EQ("Milton", GetFieldValueById("firstname_6"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address1_7"));
EXPECT_EQ("US", GetFieldValueById("country_8"));
}
// Test that we can autofill forms that dynamically change the element that
// has been clicked on, even though there are multiple forms with no name.
IN_PROC_BROWSER_TEST_F(
AutofillInteractiveTestDynamicForm,
DynamicFormFill_FirstElementDisappearsMultipleNoNameForms) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com",
"/autofill/dynamic_form_element_invalid_multiple_noname_forms.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill = ListenForRefill("address1_7");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname_5"), this));
ASSERT_TRUE(std::move(refill).Wait());
// Make sure the second form was filled correctly, and the first form was left
// unfilled.
EXPECT_EQ("", GetFieldValueById("firstname_1"));
EXPECT_EQ("", GetFieldValueById("firstname_2"));
EXPECT_EQ("", GetFieldValueById("address1_3"));
EXPECT_EQ("CA", GetFieldValueById("country_4")); // default
EXPECT_EQ("Milton", GetFieldValueById("firstname_6"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address1_7"));
EXPECT_EQ("US", GetFieldValueById("country_8"));
}
// Test that we can autofill forms that dynamically change the element that
// has been clicked on, even though the elements are unowned.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicFormFill_FirstElementDisappearsUnowned) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/dynamic_form_element_invalid_unowned.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill = ListenForRefill("address1");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
ASSERT_TRUE(std::move(refill).Wait());
// Make sure the new form was filled correctly.
EXPECT_EQ("Milton", GetFieldValueById("firstname2"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address1"));
EXPECT_EQ("Austin", GetFieldValueById("city"));
EXPECT_EQ("Initech", GetFieldValueById("company"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email"));
EXPECT_EQ("5125551234", GetFieldValueById("phone"));
}
// Test that credit card fields are re-filled.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicChangingFormFill_AlsoForCreditCard) {
CreateTestCreditCart();
GURL url = https_server()->GetURL("a.com",
"/autofill/dynamic_form_credit_card.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill = ListenForRefill("cc-name");
ASSERT_TRUE(AutofillFlow(GetElementById("cc-name"), this,
{.show_method = ShowMethod::ByChar('M')}));
ASSERT_TRUE(std::move(refill).Wait(base::Seconds(10)));
EXPECT_EQ("Milton Waddams", GetFieldValueById("cc-name"));
EXPECT_EQ("4111111111111111", GetFieldValueById("cc-num"));
EXPECT_EQ("09", GetFieldValueById("cc-exp-month"));
EXPECT_EQ("2999", GetFieldValueById("cc-exp-year"));
EXPECT_EQ("", GetFieldValueById("cc-csc"));
}
void DoDynamicChangingFormFill_SelectUpdated(
AutofillInteractiveTestDynamicForm* test,
net::EmbeddedTestServer* test_server,
bool should_test_async_update) {
test->CreateTestProfile();
GURL url = test_server->GetURL(
"a.com",
base::StringPrintf(("/autofill/dynamic_form_select_options_change.html"
"?is_async=%s"),
base::ToString(should_test_async_update)));
ASSERT_TRUE(ui_test_utils::NavigateToURL(test->browser(), url));
// Check that the test page correctly parsed the GET parameters by checking
// type of the inserted field.
auto has_n_controls_of_type = [](FormControlType control_type,
size_t expected_number,
const FormStructure& form) {
size_t num_found = 0u;
for (const std::unique_ptr<AutofillField>& field : form.fields()) {
if (field->form_control_type() == control_type) {
++num_found;
}
}
return num_found == expected_number;
};
ASSERT_TRUE(
WaitForMatchingForm(test->GetBrowserAutofillManager(),
base::BindRepeating(has_n_controls_of_type,
FormControlType::kSelectOne, 2)));
ValueWaiter refill = test->ListenForRefill("state");
// Trigger first fill.
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), test));
// Wait till the first onchange event fired on the 'state' field after the
// <option>s in the 'state' field have been updated.
ASSERT_TRUE(std::move(refill).Wait());
// Check that the page correctly parsed the 'is_async' GET parameter.
ASSERT_EQ(should_test_async_update, test->GetFieldCheckedById("is_async"));
// Make sure the new form was filled correctly.
EXPECT_EQ(kDefaultAddressValues.first_name,
test->GetFieldValueById("firstname"));
EXPECT_EQ(kDefaultAddressValues.address1,
test->GetFieldValueById("address1"));
EXPECT_EQ(kDefaultAddressValues.state_short,
test->GetFieldValueById("state"));
EXPECT_EQ(kDefaultAddressValues.city, test->GetFieldValueById("city"));
}
// Test that we can Autofill dynamically changing selects that have options
// added and removed.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicChangingFormFill_SelectUpdated) {
DoDynamicChangingFormFill_SelectUpdated(this, embedded_test_server(),
/*should_test_async_update=*/false);
}
// Test that we can Autofill dynamically changing selects that have options
// added and removed, when the updating occurs asynchronously.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicChangingFormFill_SelectUpdatedAsync) {
DoDynamicChangingFormFill_SelectUpdated(this, embedded_test_server(),
/*should_test_async_update=*/true);
}
// Test that we can Autofill dynamically changing selects that have options
// added and removed only once.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicChangingFormFill_DoubleSelectUpdated) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/dynamic_form_double_select_options_change.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill1 = ListenForRefill("address1", "refill1");
ValueWaiter refill2 = ListenForRefill("firstname", "refill2");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
ASSERT_TRUE(std::move(refill1).Wait());
ASSERT_FALSE(std::move(refill2).Wait());
// Upon the first fill, JS resets the address1 field, which triggers a refill.
// Upon the refill, JS resets the T
EXPECT_EQ("", GetFieldValueById(
"firstname")); // That field value was reset dynamically.
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address1"));
EXPECT_EQ("CA", GetFieldValueById("state")); // The <select>'s default value.
EXPECT_EQ("Austin", GetFieldValueById("city"));
EXPECT_EQ("Initech", GetFieldValueById("company"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email"));
EXPECT_EQ("5125551234", GetFieldValueById("phone"));
}
// Test that we can Autofill dynamically generated forms with no name if the
// NameForAutofill of the first field matches.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicChangingFormFill_FormWithoutName) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/dynamic_form_no_name.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill = ListenForRefill("firstname_form1");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
ASSERT_TRUE(std::move(refill).Wait());
// Make sure the new form was filled correctly.
EXPECT_EQ("Milton", GetFieldValueById("firstname_form1"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address_form1"));
EXPECT_EQ("TX", GetFieldValueById("state_form1"));
EXPECT_EQ("Austin", GetFieldValueById("city_form1"));
EXPECT_EQ("Initech", GetFieldValueById("company_form1"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email_form1"));
EXPECT_EQ("15125551234", GetFieldValueById("phone_form1"));
}
// Test that we can Autofill dynamically changing selects that have options
// added and removed for forms with no names if the NameForAutofill of the first
// field matches.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicChangingFormFill_SelectUpdated_FormWithoutName) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com",
"/autofill/dynamic_form_with_no_name_select_options_change.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill = ListenForRefill("firstname");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
ASSERT_TRUE(std::move(refill).Wait());
// Make sure the new form was filled correctly.
EXPECT_EQ("Milton", GetFieldValueById("firstname"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address1"));
EXPECT_EQ("TX", GetFieldValueById("state"));
EXPECT_EQ("Austin", GetFieldValueById("city"));
EXPECT_EQ("Initech", GetFieldValueById("company"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email"));
EXPECT_EQ("5125551234", GetFieldValueById("phone"));
}
// Test that we can Autofill dynamically generated synthetic forms if the
// NameForAutofill of the first field matches.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicChangingFormFill_SyntheticForm) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/dynamic_synthetic_form.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill = ListenForRefill("firstname_syntheticform1");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
ASSERT_TRUE(std::move(refill).Wait());
// Make sure the new form was filled correctly.
EXPECT_EQ("Milton", GetFieldValueById("firstname_syntheticform1"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address_syntheticform1"));
EXPECT_EQ("TX", GetFieldValueById("state_syntheticform1"));
EXPECT_EQ("Austin", GetFieldValueById("city_syntheticform1"));
EXPECT_EQ("Initech", GetFieldValueById("company_syntheticform1"));
EXPECT_EQ("red.swingline@initech.com",
GetFieldValueById("email_syntheticform1"));
EXPECT_EQ("15125551234", GetFieldValueById("phone_syntheticform1"));
}
// Test that we can Autofill dynamically synthetic forms when the select options
// change if the NameForAutofill of the first field matches
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
DynamicChangingFormFill_SelectUpdated_SyntheticForm) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/dynamic_synthetic_form_select_options_change.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter refill = ListenForRefill("firstname");
ASSERT_TRUE(AutofillFlow(GetElementById("firstname"), this));
ASSERT_TRUE(std::move(refill).Wait());
// Make sure the new form was filled correctly.
EXPECT_EQ("Milton", GetFieldValueById("firstname"));
EXPECT_EQ("4120 Freidrich Lane", GetFieldValueById("address1"));
EXPECT_EQ("TX", GetFieldValueById("state"));
EXPECT_EQ("Austin", GetFieldValueById("city"));
EXPECT_EQ("Initech", GetFieldValueById("company"));
EXPECT_EQ("red.swingline@initech.com", GetFieldValueById("email"));
EXPECT_EQ("5125551234", GetFieldValueById("phone"));
}
// Some websites have JavaScript handlers that mess with the input of the user
// and autofill. A common problem is that the date "09/2999" gets reformatted
// into "09 / 20" instead of "09 / 99".
// In these tests, the following steps will happen:
// 1) Autofill recognizes an expiration date field with maxlength=7, will infer
// that it is supposed to fill 09/2999 and will fill that value.
// 2) The website sees the content 09/2999 and reformats it to 09 / 29 because
// this is what websites do sometimes.
// 3) The AutofillAgent recognizes that it failed to fill 09/2999 and fills
// 09 / 99 instead.
// 4) The promise waits to see 09 / 99 and resolved.
// Flaky on Linux MSAN https://crbug.com/362299091.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestDynamicForm,
FillCardOnReformattingForm) {
CreateTestCreditCart();
GURL url = https_server()->GetURL(
"a.com", "/autofill/autofill_creditcard_form_with_date_formatter.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ValueWaiter reformat_waiter =
ListenForValueChange("CREDIT_CARD_EXP_DATE", "unblock", GetWebContents());
ASSERT_TRUE(AutofillFlow(GetElementById("CREDIT_CARD_NAME_FULL"), this));
ASSERT_TRUE(std::move(reformat_waiter).Wait());
EXPECT_EQ("09 / 99", GetFieldValue(GetElementById("CREDIT_CARD_EXP_DATE")));
// Since votes are emitted and quality metrics are recorded asynchronously, we
// need to explicitly wait for the pending votes. Since voting is scheduled on
// submission, we first need to wait for the submission (otherwise, there are
// no pending to vote for).
//
// Additionally, we wait for a navigation because that's when the key metrics
// are emitted.
content::LoadStopObserver load_stop_observer(GetWebContents());
BrowserAutofillManager& autofill_manager = *GetBrowserAutofillManager();
TestAutofillManagerSingleEventWaiter submission_waiter(
autofill_manager, &AutofillManager::Observer::OnFormSubmitted);
ASSERT_TRUE(content::ExecJs(GetWebContents(),
"document.getElementById('testform').submit();"));
ASSERT_TRUE(std::move(submission_waiter).Wait());
ASSERT_TRUE(test_api(ContentAutofillClient::FromWebContents(GetWebContents())
->GetVotesUploader())
.FlushPendingVotes());
load_stop_observer.Wait();
// Short hand for ExpectBucketCount:
auto expect_count = [&](std::string_view name,
base::HistogramBase::Sample32 sample,
base::HistogramBase::Count32 expected_count) {
histogram_tester().ExpectBucketCount(name, sample, expected_count);
};
expect_count("Autofill.KeyMetrics.FillingReadiness.CreditCard", 1, 1);
expect_count("Autofill.KeyMetrics.FillingAcceptance.CreditCard", 1, 1);
expect_count("Autofill.KeyMetrics.FillingCorrectness.CreditCard", 1, 1);
expect_count("Autofill.KeyMetrics.FillingAssistance.CreditCard", 1, 1);
// Ensure that refills don't count as edits.
expect_count("Autofill.PerfectFilling.CreditCards", 1, 1);
// Bucket 0 = edited, 1 = accepted; 3 samples for 3 fields.
expect_count("Autofill.EditedAutofilledFieldAtSubmission2.Aggregate", 0, 0);
expect_count("Autofill.EditedAutofilledFieldAtSubmission2.Aggregate", 1, 3);
}
// Shadow DOM tests consist of two cases:
// - Case 0: the <form> is in the main DOM;
// - Case 1: the <form> is in a shadow DOM.
class AutofillInteractiveTestShadowDom
: public AutofillInteractiveTest,
public ::testing::WithParamInterface<size_t> {
public:
size_t case_num() const { return GetParam(); }
// Replaces "$1" in `str` with the `case_num()`.
std::string WithCaseNum(std::string_view str) const {
return base::ReplaceStringPlaceholders(
str, {base::NumberToString(case_num())}, nullptr);
}
ElementExpr JsElement(std::string_view js_expr) {
return ElementExpr(WithCaseNum(js_expr));
}
content::EvalJsResult Js(std::string_view js_code) {
return content::EvalJs(GetWebContents(), WithCaseNum(js_code));
}
};
INSTANTIATE_TEST_SUITE_P(AutofillInteractiveTest,
AutofillInteractiveTestShadowDom,
::testing::Values(0, 1));
// Tests that in a shadow-DOM-transcending form, Autofill detects labels
// *outside* of the field's shadow DOM.
IN_PROC_BROWSER_TEST_P(AutofillInteractiveTestShadowDom,
LabelInHostingDomOfField) {
CreateTestProfile();
GURL url =
embedded_test_server()->GetURL("a.com", "/autofill/shadowdom.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(JsElement("getNameElement($1)"), this));
EXPECT_EQ("Milton C. Waddams", Js("getName($1)"));
EXPECT_EQ("4120 Freidrich Lane", Js("getAddress($1)"));
EXPECT_EQ("Austin", Js("getCity($1)"));
EXPECT_EQ("TX", Js("getState($1)"));
EXPECT_EQ("78744", Js("getZip($1)"));
}
// Tests that in a shadow-DOM-transcending form, Autofill detects labels
// *inside* of the field's shadow DOM.
IN_PROC_BROWSER_TEST_P(AutofillInteractiveTestShadowDom,
LabelInSameShadowDomAsField) {
CreateTestProfile();
GURL url = embedded_test_server()->GetURL(
"a.com", "/autofill/shadowdom-no-inference.html");
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), url));
ASSERT_TRUE(AutofillFlow(JsElement("getNameElement($1)"), this));
EXPECT_EQ("Milton C. Waddams", Js("getName($1)"));
EXPECT_EQ("4120 Freidrich Lane", Js("getAddress($1)"));
EXPECT_EQ("TX", Js("getState($1)"));
}
// ChromeVox is only available on ChromeOS.
#if BUILDFLAG(IS_CHROMEOS) && BUILDFLAG(ENABLE_EXTENSIONS)
class AutofillInteractiveTestChromeVox : public AutofillInteractiveTestBase {
public:
AutofillInteractiveTestChromeVox() = default;
~AutofillInteractiveTestChromeVox() override = default;
void SetUpOnMainThread() override {
AutofillInteractiveTestBase::SetUpOnMainThread();
chromevox_test_utils_ = std::make_unique<ash::ChromeVoxTestUtils>();
}
void TearDownOnMainThread() override {
chromevox_test_utils_.reset();
// Unload the ChromeVox extension so the browser doesn't try to respond to
// in-flight requests during test shutdown. https://crbug.com/923090
ash::AccessibilityManager::Get()->EnableSpokenFeedback(false);
AutomationManagerAura::GetInstance()->Disable();
AutofillInteractiveTestBase::TearDownOnMainThread();
}
ash::ChromeVoxTestUtils* chromevox_test_utils() {
return chromevox_test_utils_.get();
}
ash::test::SpeechMonitor* sm() { return chromevox_test_utils()->sm(); }
std::unique_ptr<ash::ChromeVoxTestUtils> chromevox_test_utils_;
};
// Ensure that autofill suggestions are properly read out via ChromeVox.
// This is a regressions test for crbug.com/1208913.
// TODO(crbug.com/40820453): Flaky on ChromeOS
#if BUILDFLAG(IS_CHROMEOS)
#define MAYBE_TestNotificationOfAutofillDropdown \
DISABLED_TestNotificationOfAutofillDropdown
#else
#define MAYBE_TestNotificationOfAutofillDropdown \
TestNotificationOfAutofillDropdown
#endif
IN_PROC_BROWSER_TEST_F(AutofillInteractiveTestChromeVox,
MAYBE_TestNotificationOfAutofillDropdown) {
CreateTestProfile();
SetTestUrlResponse(kTestShippingFormString);
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
chromevox_test_utils()->EnableChromeVox();
content::ScopedAccessibilityModeOverride scoped_accessibility_mode(
web_contents(), ui::kAXModeComplete);
// The following contains a sequence of calls to
// sm()->ExpectSpeechPattern() and test_delegate()->Wait(). It is essential
// to first flush the expected speech patterns, otherwise the two functions
// start incompatible RunLoops.
sm()->ExpectSpeechPattern("Web Content");
sm()->Call([this] {
content::WaitForAccessibilityTreeToContainNodeWithName(web_contents(),
"First name:");
web_contents()->Focus();
test_delegate()->SetExpectations({ObservedUiEvents::kSuggestionsShown});
ASSERT_TRUE(FocusField(GetElementById("firstname"), GetWebContents()));
});
sm()->ExpectSpeechPattern("First name:");
sm()->ExpectSpeechPattern("Edit text");
sm()->ExpectSpeechPattern("Region");
// Wait for suggestions popup to show up. This needs to happen before we
// simulate the cursor down key press.
sm()->Call([this] { ASSERT_TRUE(test_delegate()->Wait()); });
sm()->Call([this] {
test_delegate()->SetExpectations({ObservedUiEvents::kPreviewFormData});
ASSERT_TRUE(
ui_controls::SendKeyPress(browser()->window()->GetNativeWindow(),
ui::VKEY_DOWN, false, false, false, false));
});
sm()->ExpectSpeechPattern("Autofill menu opened");
sm()->ExpectSpeechPattern("Milton 4120 Freidrich Lane");
sm()->ExpectSpeechPattern("List item");
sm()->ExpectSpeechPattern("1 of 2");
sm()->Call([this] { ASSERT_TRUE(test_delegate()->Wait()); });
sm()->Replay();
}
#endif // BUILDFLAG(IS_CHROMEOS) && BUILDFLAG(ENABLE_EXTENSIONS)
class AutofillInteractiveFormSubmissionTest
: public AutofillInteractiveTestBase {
public:
class MockAutofillManager : public BrowserAutofillManager {
public:
explicit MockAutofillManager(ContentAutofillDriver* driver)
: BrowserAutofillManager(driver) {}
MOCK_METHOD(void,
OnFormSubmittedImpl,
(const FormData&, mojom::SubmissionSource),
(override));
TestAutofillManagerWaiter& text_field_change_waiter() {
return text_field_change_waiter_;
}
TestAutofillManagerWaiter& select_field_change_waiter() {
return select_field_change_waiter_;
}
private:
TestAutofillManagerWaiter text_field_change_waiter_{
*this,
{AutofillManagerEvent::kTextFieldValueChanged}};
TestAutofillManagerWaiter select_field_change_waiter_{
*this,
{AutofillManagerEvent::kSelectControlSelectionChanged}};
};
MockAutofillManager* autofill_manager() {
return autofill_manager(GetWebContents()->GetPrimaryMainFrame());
}
MockAutofillManager* autofill_manager(content::RenderFrameHost* rfh) {
return autofill_manager_injector_[rfh];
}
void SetUpOnMainThread() override {
AutofillInteractiveTestBase::SetUpOnMainThread();
SetUpServer();
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
ASSERT_TRUE(WaitForMatchingForm(
autofill_manager(), base::BindRepeating([](const FormStructure& form) {
return form.fields().size() == 5;
})));
}
void SetUpServer() {
SetTestUrlResponse(R"(
<html><body>
<form id='shipping' method='POST' action='/success.html'>
Name: <input type='text' id='name'><br>
Address: <input type='text' id='address'><br>
City: <input type='text' id='city'><br>
ZIP: <input type='text' id='zip'><br>
State: <select id='state'>
<option value='CA'>CA</option>
<option value='TX'>TX</option>
<option value='WA'>WA</option>
</select><br>
</form>
)");
SetResponseForUrlPath("/success.html", "<html><body>Happy times!");
SetResponseForUrlPath("/xhr", "<foo>Happy times!</foo>");
}
void EnterValues() {
EnterValues({{"name", "Sarah"},
{"address", "123 Main Road"},
{"state", "WA", FormControlType::kSelectOne}});
}
void EnterValues(const std::vector<FieldValue>& values) {
for (const FieldValue& value : values) {
ASSERT_TRUE(EnterTextIntoField(GetElementById(value.id), value.value,
this, GetWebContents()));
using enum FormControlType;
switch (value.form_control_type) {
case kInputText: {
auto& waiter = autofill_manager()->text_field_change_waiter();
ASSERT_TRUE(waiter.Wait(1));
break;
}
case kSelectOne: {
auto& waiter = autofill_manager()->select_field_change_waiter();
ASSERT_TRUE(waiter.Wait(1));
break;
}
default:
NOTREACHED();
}
}
}
struct NameValue {
std::u16string name;
std::u16string value;
};
[[nodiscard]] static auto HasNameValue(const NameValue& nv) {
return AllOf(Property("name", &FormFieldData::name, nv.name),
Property("value", &FormFieldData::value, nv.value));
}
[[nodiscard]] static auto HasExpectedValues() {
std::vector<NameValue> expected = {{u"name", u"Sarah"},
{u"address", u"123 Main Road"},
{u"city", u""},
{u"zip", u""},
{u"state", u"WA"}};
return FieldsAre(Map(expected, HasNameValue));
}
struct NameValueUserInput {
std::u16string name;
std::u16string value;
std::u16string user_input;
};
[[nodiscard]] static auto HasNameValueUserInput(
const NameValueUserInput& nvu) {
return AllOf(
Property("name", &FormFieldData::name, nvu.name),
Property("value", &FormFieldData::value, nvu.value),
Property("user_input", &FormFieldData::user_input, nvu.user_input));
}
void ExecuteScript(const std::string& script) {
ASSERT_TRUE(content::ExecJs(GetWebContents(), script));
}
private:
TestAutofillManagerInjector<MockAutofillManager> autofill_manager_injector_;
};
// Tests that user-triggered submission triggers a submission event in
// BrowserAutofillManager.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveFormSubmissionTest, Submission) {
EnterValues();
base::RunLoop run_loop;
// Ensure that only expected form submissions are recorded.
EXPECT_CALL(*autofill_manager(), OnFormSubmittedImpl).Times(0);
EXPECT_CALL(*autofill_manager(),
OnFormSubmittedImpl(HasExpectedValues(),
mojom::SubmissionSource::FORM_SUBMISSION))
.Times(1)
.WillRepeatedly(RunClosure(run_loop.QuitClosure()));
ExecuteScript("document.getElementById('shipping').submit();");
run_loop.Run();
}
// Tests that non-link-click, renderer-initiated navigation triggers a
// submission event in BrowserAutofillManager.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveFormSubmissionTest,
ProbableSubmission) {
EnterValues();
base::RunLoop run_loop;
// Ensure that only expected form submissions are recorded.
EXPECT_CALL(*autofill_manager(), OnFormSubmittedImpl).Times(0);
EXPECT_CALL(
*autofill_manager(),
OnFormSubmittedImpl(HasExpectedValues(),
mojom::SubmissionSource::PROBABLY_FORM_SUBMITTED))
.Times(1)
.WillRepeatedly(RunClosure(run_loop.QuitClosure()));
// Add a delay before navigating away to avoid race conditions. This is
// appropriate since we're faking user interaction here.
ExecuteScript(
"setTimeout(() => { window.location.assign('/success.html'); }, 50);");
run_loop.Run();
}
// Tests that a same document navigation can trigger a form submission.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveFormSubmissionTest,
SameDocumentNavigation) {
EnterValues();
base::RunLoop run_loop;
// Ensure that only expected form submissions are recorded.
EXPECT_CALL(*autofill_manager(), OnFormSubmittedImpl).Times(0);
EXPECT_CALL(
*autofill_manager(),
OnFormSubmittedImpl(HasExpectedValues(),
mojom::SubmissionSource::SAME_DOCUMENT_NAVIGATION))
.Times(1)
.WillRepeatedly(RunClosure(run_loop.QuitClosure()));
// Simulate form submission.
ExecuteScript(
R"(
// Same document navigation:
document.getElementById('shipping').style.display = 'none';
const url = new URL(window.location);
url.searchParams.set('foo', 'bar');
window.history.pushState({}, '', url);
// Hide form, which is the trigger for the submission event.
document.getElementById('shipping').style.display = 'none';
)");
// This forces layout update.
RunUntilInputProcessed(GetRenderViewHost()->GetWidget());
run_loop.Run();
}
// Tests that an XHR request can indicate a form submission.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveFormSubmissionTest,
XhrSucceededAndHideForm) {
EnterValues();
base::RunLoop run_loop;
// Ensure that only expected form submissions are recorded.
EXPECT_CALL(*autofill_manager(), OnFormSubmittedImpl).Times(0);
EXPECT_CALL(*autofill_manager(),
OnFormSubmittedImpl(HasExpectedValues(),
mojom::SubmissionSource::XHR_SUCCEEDED))
.Times(1)
.WillRepeatedly(RunClosure(run_loop.QuitClosure()));
// Simulate form submission.
ExecuteScript(
R"(
// SubmissionSource::XHR_SUCCEEDED is triggered if an XHR is observed
// after the form has been made invisible.
document.getElementById('shipping').style.display = 'none';
const xhr = new XMLHttpRequest();
xhr.open('GET', '/xhr', true);
xhr.send(null);
)");
// This forces layout update.
RunUntilInputProcessed(GetRenderViewHost()->GetWidget());
run_loop.Run();
}
// Tests that an XHR request can indicate a form submission - even if the form
// is deleted from the DOM.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveFormSubmissionTest,
XhrSucceededAndDeleteForm) {
EnterValues();
base::RunLoop run_loop;
// Ensure that only expected form submissions are recorded.
EXPECT_CALL(*autofill_manager(), OnFormSubmittedImpl).Times(0);
EXPECT_CALL(*autofill_manager(),
OnFormSubmittedImpl(HasExpectedValues(),
mojom::SubmissionSource::XHR_SUCCEEDED))
.Times(1)
.WillRepeatedly(RunClosure(run_loop.QuitClosure()));
// Simulate form submission.
ExecuteScript(
R"(
// SubmissionSource::XHR_SUCCEEDED is triggered if an XHR is observed
// after the form has been deleted.
const form = document.getElementById('shipping');
form.remove();
const xhr = new XMLHttpRequest();
xhr.open('GET', '/xhr', true);
xhr.send(null);
)");
// This forces layout update.
RunUntilInputProcessed(GetRenderViewHost()->GetWidget());
run_loop.Run();
}
// Tests that a DOM mutation after an XHR can indicate a form submission.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveFormSubmissionTest,
DomMutationAfterXhr) {
EnterValues();
base::RunLoop run_loop;
// Ensure that only expected form submissions are recorded.
EXPECT_CALL(*autofill_manager(), OnFormSubmittedImpl).Times(0);
EXPECT_CALL(*autofill_manager(),
OnFormSubmittedImpl(HasExpectedValues(),
mojom::SubmissionSource::XHR_SUCCEEDED))
.Times(1)
.WillRepeatedly(RunClosure(run_loop.QuitClosure()));
// Simulate form submission.
ExecuteScript(
R"(
const xhr = new XMLHttpRequest();
xhr.open('GET', '/xhr', true);
xhr.onload = () => {
// SubmissionSource::XHR_SUCCEEDED is triggered if a form
// is hidden an XHR was observed.
document.getElementById('shipping').style.display = 'none';
}
xhr.send(null);
)");
// This forces layout update.
RunUntilInputProcessed(GetRenderViewHost()->GetWidget());
run_loop.Run();
}
// Tests that FormFieldData::user_input has the text that the user typed into
// the field. This is needed in order to show the save-card dialog when the
// page replaces the <input> value with '***'.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveFormSubmissionTest,
RememberUserInput) {
const std::vector<NameValueUserInput> kExpectedSubmittedValues{
{u"name", u"JS Modified Name", u"Sarah"},
{u"address", u"JS Modified Address", u"123 Main Road"},
{u"city", u"", u""},
{u"zip", u"", u""},
{u"state", u"WA", u""}}; // user_input is not set for <select>.
EnterValues();
ExecuteScript("document.getElementById('name').value = 'JS Modified Name';");
ExecuteScript(
"document.getElementById('address').value = 'JS Modified Address';");
base::RunLoop run_loop;
// Ensure that only expected form submissions are recorded.
EXPECT_CALL(*autofill_manager(), OnFormSubmittedImpl).Times(0);
EXPECT_CALL(*autofill_manager(),
OnFormSubmittedImpl(FieldsAre(Map(kExpectedSubmittedValues,
HasNameValueUserInput)),
mojom::SubmissionSource::FORM_SUBMISSION))
.Times(1)
.WillRepeatedly(RunClosure(run_loop.QuitClosure()));
ExecuteScript("document.getElementById('shipping').submit();");
run_loop.Run();
}
// Tests scenario where in sequence:
// 1) The user types into a form
// 2) The form is cleared via JavaScript
// 3) The user autofills the form
// 4) The user submits the form
// That FormFieldData::user_input is empty and does not contain stale data that
// the user typed into the form.
IN_PROC_BROWSER_TEST_F(AutofillInteractiveFormSubmissionTest,
TreatAutofillAsUserInput) {
CreateTestProfile();
EnterValues({{"address", "User Entered Address"}});
ExecuteScript("document.getElementById('address').value = '';");
ASSERT_TRUE(AutofillFlow(GetElementById("name"), this,
{.show_method = ShowMethod::ByChar('M')}));
const std::vector<FieldValue> kExpectedAddress{
{"name", kDefaultAddressValues.full_name},
{"address", kDefaultAddressValues.address1},
{"city", kDefaultAddressValues.city},
{"zip", kDefaultAddressValues.zip},
{"state", kDefaultAddressValues.state_short}};
EXPECT_THAT(GetFormValues(), ValuesAre(kExpectedAddress));
base::RunLoop run_loop;
// Ensure that only expected form submissions are recorded.
EXPECT_CALL(*autofill_manager(), OnFormSubmittedImpl).Times(0);
EXPECT_CALL(
*autofill_manager(),
OnFormSubmittedImpl(
FieldsAre(Map(kExpectedAddress,
[](const FieldValue& fv) {
return HasNameValueUserInput(
{base::UTF8ToUTF16(fv.id),
base::UTF8ToUTF16(fv.value), u""});
})),
mojom::SubmissionSource::FORM_SUBMISSION))
.Times(1)
.WillRepeatedly(RunClosure(run_loop.QuitClosure()));
ExecuteScript("document.getElementById('shipping').submit();");
run_loop.Run();
}
class AutofillInteractiveFormlessFormSubmissionTest
: public AutofillInteractiveFormSubmissionTest {
public:
void SetUpOnMainThread() override {
AutofillInteractiveTestBase::SetUpOnMainThread();
SetUpServer();
ASSERT_TRUE(ui_test_utils::NavigateToURL(browser(), GetTestUrl()));
ASSERT_TRUE(WaitForMatchingForm(
autofill_manager(), base::BindRepeating([](const FormStructure& form) {
return form.fields().size() == 3;
})));
}
void SetUpServer() {
SetTestUrlResponse(R"(
<html><body>
Name: <input type='text' id='name'><br>
Address: <input type='text' id='address'><br>
City: <input type='text' id='city'><br>
)");
SetResponseForUrlPath("/success.html", "<html><body>Happy times!");
SetResponseForUrlPath("/xhr", "<foo>Happy times!</foo>");
}
base::test::ScopedFeatureList scoped_feature_list_;
};
// Tests that a submission is detected when the following steps are done in
// sequence:
// 1) User fills <input>s on page without <form>.
// 2) The page does an XHR
// 3) The page navigates
IN_PROC_BROWSER_TEST_F(AutofillInteractiveFormlessFormSubmissionTest,
NavigationAfterXhr) {
const std::vector<FieldValue> kEnteredValues = {
{"name", "Sarah"}, {"address", "123 Main Road"}, {"city", "Moonbeam"}};
EnterValues(kEnteredValues);
base::RunLoop run_loop;
// Ensure that only expected form submissions are recorded.
EXPECT_CALL(*autofill_manager(), OnFormSubmittedImpl).Times(0);
EXPECT_CALL(
*autofill_manager(),
OnFormSubmittedImpl(FieldsAre(Map(kEnteredValues,
[](const FieldValue& fv) {
return HasNameValue(
{base::UTF8ToUTF16(fv.id),
base::UTF8ToUTF16(fv.value)});
})),
mojom::SubmissionSource::PROBABLY_FORM_SUBMITTED))
.Times(1)
.WillRepeatedly(RunClosure(run_loop.QuitClosure()));
// Simulate XHR, then page navigation.
ExecuteScript(
R"(
const xhr = new XMLHttpRequest();
xhr.open('GET', '/xhr', true);
xhr.onload = () => {
setTimeout(() => {
window.location.href = 'success.html';
}, 50);
}
xhr.send(null);
)");
run_loop.Run();
}
} // namespace
} // namespace autofill
|