1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/autofill/core/browser/personal_data_manager.h"
#include <stddef.h>
#include <algorithm>
#include <list>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/command_line.h"
#include "base/files/scoped_temp_dir.h"
#include "base/guid.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/field_trial.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/histogram_tester.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "components/autofill/core/browser/autofill_experiments.h"
#include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/form_structure.h"
#include "components/autofill/core/browser/personal_data_manager_observer.h"
#include "components/autofill/core/browser/webdata/autofill_table.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/autofill/core/common/autofill_constants.h"
#include "components/autofill/core/common/autofill_pref_names.h"
#include "components/autofill/core/common/autofill_switches.h"
#include "components/autofill/core/common/form_data.h"
#include "components/os_crypt/os_crypt_mocker.h"
#include "components/prefs/pref_service.h"
#include "components/signin/core/browser/account_tracker_service.h"
#include "components/signin/core/browser/fake_signin_manager.h"
#include "components/signin/core/browser/test_signin_client.h"
#include "components/signin/core/common/signin_pref_names.h"
#include "components/variations/entropy_provider.h"
#include "components/variations/variations_associated_data.h"
#include "components/webdata/common/web_data_service_base.h"
#include "components/webdata/common/web_database_service.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::ASCIIToUTF16;
using base::UTF8ToUTF16;
namespace autofill {
namespace {
enum UserMode { USER_MODE_NORMAL, USER_MODE_INCOGNITO };
const std::string kUTF8MidlineEllipsis =
" "
"\xE2\x80\xA2\xE2\x80\x86"
"\xE2\x80\xA2\xE2\x80\x86"
"\xE2\x80\xA2\xE2\x80\x86"
"\xE2\x80\xA2\xE2\x80\x86";
ACTION(QuitMainMessageLoop) {
base::MessageLoop::current()->QuitWhenIdle();
}
class PersonalDataLoadedObserverMock : public PersonalDataManagerObserver {
public:
PersonalDataLoadedObserverMock() {}
virtual ~PersonalDataLoadedObserverMock() {}
MOCK_METHOD0(OnPersonalDataChanged, void());
};
template <typename T>
bool CompareElements(T* a, T* b) {
return a->Compare(*b) < 0;
}
template <typename T>
bool ElementsEqual(T* a, T* b) {
return a->Compare(*b) == 0;
}
// Verifies that two vectors have the same elements (according to T::Compare)
// while ignoring order. This is useful because multiple profiles or credit
// cards that are added to the SQLite DB within the same second will be returned
// in GUID (aka random) order.
template <typename T>
void ExpectSameElements(const std::vector<T*>& expectations,
const std::vector<T*>& results) {
ASSERT_EQ(expectations.size(), results.size());
std::vector<T*> expectations_copy = expectations;
std::sort(
expectations_copy.begin(), expectations_copy.end(), CompareElements<T>);
std::vector<T*> results_copy = results;
std::sort(results_copy.begin(), results_copy.end(), CompareElements<T>);
EXPECT_EQ(std::mismatch(results_copy.begin(),
results_copy.end(),
expectations_copy.begin(),
ElementsEqual<T>).first,
results_copy.end());
}
} // anonymous namespace
class PersonalDataManagerTest : public testing::Test {
protected:
PersonalDataManagerTest() : autofill_table_(nullptr) {}
void SetUp() override {
OSCryptMocker::SetUpWithSingleton();
prefs_ = test::PrefServiceForTesting();
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
base::FilePath path = temp_dir_.GetPath().AppendASCII("TestWebDB");
web_database_ =
new WebDatabaseService(path, base::ThreadTaskRunnerHandle::Get(),
base::ThreadTaskRunnerHandle::Get());
// Setup account tracker.
signin_client_.reset(new TestSigninClient(prefs_.get()));
account_tracker_.reset(new AccountTrackerService());
account_tracker_->Initialize(signin_client_.get());
signin_manager_.reset(new FakeSigninManagerBase(signin_client_.get(),
account_tracker_.get()));
signin_manager_->Initialize(prefs_.get());
// Hacky: hold onto a pointer but pass ownership.
autofill_table_ = new AutofillTable;
web_database_->AddTable(std::unique_ptr<WebDatabaseTable>(autofill_table_));
web_database_->LoadDatabase();
autofill_database_service_ = new AutofillWebDataService(
web_database_, base::ThreadTaskRunnerHandle::Get(),
base::ThreadTaskRunnerHandle::Get(),
WebDataServiceBase::ProfileErrorCallback());
autofill_database_service_->Init();
test::DisableSystemServices(prefs_.get());
ResetPersonalDataManager(USER_MODE_NORMAL);
// There are no field trials enabled by default.
field_trial_list_.reset();
// Reset the deduping pref to its default value.
personal_data_->pref_service_->SetInteger(
prefs::kAutofillLastVersionDeduped, 0);
personal_data_->pref_service_->SetBoolean(
prefs::kAutofillProfileUseDatesFixed, false);
}
void TearDown() override {
// Order of destruction is important as AutofillManager relies on
// PersonalDataManager to be around when it gets destroyed.
signin_manager_->Shutdown();
signin_manager_.reset();
account_tracker_->Shutdown();
account_tracker_.reset();
signin_client_.reset();
test::DisableSystemServices(prefs_.get());
OSCryptMocker::TearDown();
}
void ResetPersonalDataManager(UserMode user_mode) {
bool is_incognito = (user_mode == USER_MODE_INCOGNITO);
personal_data_.reset(new PersonalDataManager("en"));
personal_data_->Init(
scoped_refptr<AutofillWebDataService>(autofill_database_service_),
prefs_.get(),
account_tracker_.get(),
signin_manager_.get(),
is_incognito);
personal_data_->AddObserver(&personal_data_observer_);
personal_data_->OnSyncServiceInitialized(nullptr);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
}
void ResetProfiles() {
std::vector<AutofillProfile> empty_profiles;
personal_data_->SetProfiles(&empty_profiles);
}
void EnableWalletCardImport() {
signin_manager_->SetAuthenticatedAccountInfo("12345",
"syncuser@example.com");
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kEnableOfferStoreUnmaskedWalletCards);
}
void EnableAutofillProfileCleanup() {
personal_data_->is_autofill_profile_cleanup_pending_ = true;
}
void SetupReferenceProfile() {
ASSERT_EQ(0U, personal_data_->GetProfiles().size());
AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile, "Marion", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox", "123 Zoo St", "unit 5",
"Hollywood", "CA", "91601", "US", "12345678910");
personal_data_->AddProfile(profile);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
ASSERT_EQ(1U, personal_data_->GetProfiles().size());
}
// Helper method that will add credit card fields in |form|, according to the
// specified values. If a value is nullptr, the corresponding field won't get
// added (empty string will add a field with an empty string as the value).
void AddFullCreditCardForm(FormData* form,
const char* name,
const char* number,
const char* month,
const char* year) {
FormFieldData field;
if (name) {
test::CreateTestFormField("Name on card:", "name_on_card", name, "text",
&field);
form->fields.push_back(field);
}
if (number) {
test::CreateTestFormField("Card Number:", "card_number", number, "text",
&field);
form->fields.push_back(field);
}
if (month) {
test::CreateTestFormField("Exp Month:", "exp_month", month, "text",
&field);
form->fields.push_back(field);
}
if (year) {
test::CreateTestFormField("Exp Year:", "exp_year", year, "text", &field);
form->fields.push_back(field);
}
}
// Adds three local cards to the |personal_data_|. The three cards are
// different: two are from different companies and the third doesn't have a
// number. All three have different owners and credit card number. This allows
// to test the suggestions based on name as well as on credit card number.
void SetupReferenceLocalCreditCards() {
ASSERT_EQ(0U, personal_data_->GetCreditCards().size());
CreditCard credit_card0("287151C8-6AB1-487C-9095-28E80BE5DA15",
"https://www.example.com");
test::SetCreditCardInfo(&credit_card0, "Clyde Barrow",
"347666888555" /* American Express */, "04",
"2999");
credit_card0.set_use_count(3);
credit_card0.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(1));
personal_data_->AddCreditCard(credit_card0);
CreditCard credit_card1("1141084B-72D7-4B73-90CF-3D6AC154673B",
"https://www.example.com");
credit_card1.set_use_count(300);
credit_card1.set_use_date(base::Time::Now() -
base::TimeDelta::FromDays(10));
test::SetCreditCardInfo(&credit_card1, "John Dillinger",
"423456789012" /* Visa */, "01", "2999");
personal_data_->AddCreditCard(credit_card1);
CreditCard credit_card2("002149C1-EE28-4213-A3B9-DA243FFF021B",
"https://www.example.com");
credit_card2.set_use_count(1);
credit_card2.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(1));
test::SetCreditCardInfo(&credit_card2, "Bonnie Parker",
"518765432109" /* Mastercard */, "12", "2999");
personal_data_->AddCreditCard(credit_card2);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
}
// Helper methods that simply forward the call to the private member (to avoid
// having to friend every test that needs to access the private
// PersonalDataManager::ImportAddressProfile or ImportCreditCard).
bool ImportAddressProfiles(const FormStructure& form) {
return personal_data_->ImportAddressProfiles(form);
}
bool ImportCreditCard(const FormStructure& form,
bool should_return_local_card,
std::unique_ptr<CreditCard>* imported_credit_card) {
return personal_data_->ImportCreditCard(form, should_return_local_card,
imported_credit_card);
}
// Sets up the profile order field trial group and parameter. Sets up the
// suggestions limit parameter to |limit_param|.
void EnableAutofillProfileLimitFieldTrial(const std::string& limit_param) {
DCHECK(!limit_param.empty());
// Clear the existing |field_trial_list_| and variation parameters.
field_trial_list_.reset(NULL);
field_trial_list_.reset(
new base::FieldTrialList(
base::MakeUnique<metrics::SHA1EntropyProvider>("foo")));
variations::testing::ClearAllVariationParams();
std::map<std::string, std::string> params;
params[kFrecencyFieldTrialLimitParam] = limit_param;
variations::AssociateVariationParams(kFrecencyFieldTrialName, "LimitToN",
params);
field_trial_ = base::FieldTrialList::CreateFieldTrial(
kFrecencyFieldTrialName, "LimitToN");
field_trial_->group();
}
void SubmitFormAndExpectImportedCardWithData(const FormData& form,
const char* exp_name,
const char* exp_cc_num,
const char* exp_cc_month,
const char* exp_cc_year) {
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card;
EXPECT_TRUE(ImportCreditCard(form_structure, false, &imported_credit_card));
ASSERT_TRUE(imported_credit_card);
personal_data_->SaveImportedCreditCard(*imported_credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
CreditCard expected(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&expected, exp_name, exp_cc_num, exp_cc_month,
exp_cc_year);
const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, expected.Compare(*results[0]));
}
// The temporary directory should be deleted at the end to ensure that
// files are not used anymore and deletion succeeds.
base::ScopedTempDir temp_dir_;
base::MessageLoopForUI message_loop_;
std::unique_ptr<PrefService> prefs_;
std::unique_ptr<AccountTrackerService> account_tracker_;
std::unique_ptr<FakeSigninManagerBase> signin_manager_;
std::unique_ptr<TestSigninClient> signin_client_;
scoped_refptr<AutofillWebDataService> autofill_database_service_;
scoped_refptr<WebDatabaseService> web_database_;
AutofillTable* autofill_table_; // weak ref
PersonalDataLoadedObserverMock personal_data_observer_;
std::unique_ptr<PersonalDataManager> personal_data_;
std::unique_ptr<base::FieldTrialList> field_trial_list_;
scoped_refptr<base::FieldTrial> field_trial_;
};
TEST_F(PersonalDataManagerTest, AddProfile) {
// Add profile0 to the database.
AutofillProfile profile0(test::GetFullProfile());
profile0.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("j@s.com"));
personal_data_->AddProfile(profile0);
// Reload the database.
ResetPersonalDataManager(USER_MODE_NORMAL);
// Verify the addition.
const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
ASSERT_EQ(1U, results1.size());
EXPECT_EQ(0, profile0.Compare(*results1[0]));
// Add profile with identical values. Duplicates should not get saved.
AutofillProfile profile0a = profile0;
profile0a.set_guid(base::GenerateGUID());
personal_data_->AddProfile(profile0a);
// Reload the database.
ResetPersonalDataManager(USER_MODE_NORMAL);
// Verify the non-addition.
const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
ASSERT_EQ(1U, results2.size());
EXPECT_EQ(0, profile0.Compare(*results2[0]));
// New profile with different email.
AutofillProfile profile1 = profile0;
profile1.set_guid(base::GenerateGUID());
profile1.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("john@smith.com"));
// Add the different profile. This should save as a separate profile.
// Note that if this same profile was "merged" it would collapse to one
// profile with a multi-valued entry for email.
personal_data_->AddProfile(profile1);
// Reload the database.
ResetPersonalDataManager(USER_MODE_NORMAL);
// Verify the addition.
std::vector<AutofillProfile*> profiles;
profiles.push_back(&profile0);
profiles.push_back(&profile1);
ExpectSameElements(profiles, personal_data_->GetProfiles());
}
// Test that a new profile has its basic information set.
TEST_F(PersonalDataManagerTest, AddProfile_BasicInformation) {
// Add a profile to the database.
AutofillProfile profile(test::GetFullProfile());
profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16("j@s.com"));
personal_data_->AddProfile(profile);
// Reload the database.
ResetPersonalDataManager(USER_MODE_NORMAL);
// Verify the addition.
const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, profile.Compare(*results[0]));
// Make sure the use count and use date were set.
EXPECT_EQ(1U, results[0]->use_count());
EXPECT_NE(base::Time(), results[0]->use_date());
EXPECT_NE(base::Time(), results[0]->modification_date());
}
TEST_F(PersonalDataManagerTest, DontDuplicateServerProfile) {
EnableWalletCardImport();
std::vector<AutofillProfile> server_profiles;
server_profiles.push_back(
AutofillProfile(AutofillProfile::SERVER_PROFILE, "a123"));
test::SetProfileInfo(&server_profiles.back(), "John", "", "Doe", "",
"ACME Corp", "500 Oak View", "Apt 8", "Houston", "TX",
"77401", "US", "");
// Wallet only provides a full name, so the above first and last names
// will be ignored when the profile is written to the DB.
server_profiles.back().SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe"));
autofill_table_->SetServerProfiles(server_profiles);
personal_data_->Refresh();
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
EXPECT_EQ(1U, personal_data_->GetProfiles().size());
// Add profile with identical values. Duplicates should not get saved.
AutofillProfile scraped_profile(base::GenerateGUID(),
"https://www.example.com");
test::SetProfileInfo(&scraped_profile, "John", "", "Doe", "", "ACME Corp",
"500 Oak View", "Apt 8", "Houston", "TX", "77401", "US",
"");
EXPECT_TRUE(scraped_profile.IsSubsetOf(server_profiles.back(), "en-US"));
std::string saved_guid = personal_data_->SaveImportedProfile(scraped_profile);
EXPECT_NE(scraped_profile.guid(), saved_guid);
personal_data_->Refresh();
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Verify the non-addition.
EXPECT_EQ(0U, personal_data_->web_profiles().size());
ASSERT_EQ(1U, personal_data_->GetProfiles().size());
// Verify that the server profile's use date was updated.
const AutofillProfile* server_profile = personal_data_->GetProfiles()[0];
EXPECT_GT(base::TimeDelta::FromMilliseconds(500),
base::Time::Now() - server_profile->use_date());
}
// Tests that SaveImportedProfile sets the modification date on new profiles.
TEST_F(PersonalDataManagerTest, SaveImportedProfileSetModificationDate) {
AutofillProfile profile(test::GetFullProfile());
EXPECT_NE(base::Time(), profile.modification_date());
personal_data_->SaveImportedProfile(profile);
const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
ASSERT_EQ(1U, profiles.size());
EXPECT_GT(base::TimeDelta::FromMilliseconds(500),
base::Time::Now() - profiles[0]->modification_date());
}
TEST_F(PersonalDataManagerTest, AddUpdateRemoveProfiles) {
AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile0,
"Marion", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
"91601", "US", "12345678910");
AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile1,
"Josephine", "Alicia", "Saenz",
"joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
"US", "19482937549");
AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile2,
"Josephine", "Alicia", "Saenz",
"joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
"32801", "US", "19482937549");
// Add two test profiles to the database.
personal_data_->AddProfile(profile0);
personal_data_->AddProfile(profile1);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
std::vector<AutofillProfile*> profiles;
profiles.push_back(&profile0);
profiles.push_back(&profile1);
ExpectSameElements(profiles, personal_data_->GetProfiles());
// Update, remove, and add.
profile0.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
personal_data_->UpdateProfile(profile0);
personal_data_->RemoveByGUID(profile1.guid());
personal_data_->AddProfile(profile2);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
profiles.clear();
profiles.push_back(&profile0);
profiles.push_back(&profile2);
ExpectSameElements(profiles, personal_data_->GetProfiles());
// Reset the PersonalDataManager. This tests that the personal data was saved
// to the web database, and that we can load the profiles from the web
// database.
ResetPersonalDataManager(USER_MODE_NORMAL);
// Verify that we've loaded the profiles from the web database.
ExpectSameElements(profiles, personal_data_->GetProfiles());
}
TEST_F(PersonalDataManagerTest, AddUpdateRemoveCreditCards) {
CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&credit_card0,
"John Dillinger", "423456789012" /* Visa */, "01", "2999");
CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&credit_card1, "Bonnie Parker",
"518765432109" /* Mastercard */, "12", "2999");
CreditCard credit_card2(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&credit_card2, "Clyde Barrow",
"347666888555" /* American Express */, "04", "2999");
// Add two test credit cards to the database.
personal_data_->AddCreditCard(credit_card0);
personal_data_->AddCreditCard(credit_card1);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
std::vector<CreditCard*> cards;
cards.push_back(&credit_card0);
cards.push_back(&credit_card1);
ExpectSameElements(cards, personal_data_->GetCreditCards());
// Update, remove, and add.
credit_card0.SetRawInfo(CREDIT_CARD_NAME_FULL, ASCIIToUTF16("Joe"));
personal_data_->UpdateCreditCard(credit_card0);
personal_data_->RemoveByGUID(credit_card1.guid());
personal_data_->AddCreditCard(credit_card2);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
cards.clear();
cards.push_back(&credit_card0);
cards.push_back(&credit_card2);
ExpectSameElements(cards, personal_data_->GetCreditCards());
// Reset the PersonalDataManager. This tests that the personal data was saved
// to the web database, and that we can load the credit cards from the web
// database.
ResetPersonalDataManager(USER_MODE_NORMAL);
// Verify that we've loaded the credit cards from the web database.
cards.clear();
cards.push_back(&credit_card0);
cards.push_back(&credit_card2);
ExpectSameElements(cards, personal_data_->GetCreditCards());
}
// Test that a new credit card has its basic information set.
TEST_F(PersonalDataManagerTest, AddCreditCard_BasicInformation) {
// Add a credit to the database.
CreditCard credit_card(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&credit_card, "John Dillinger",
"423456789012" /* Visa */, "01", "2999");
personal_data_->AddCreditCard(credit_card);
// Reload the database.
ResetPersonalDataManager(USER_MODE_NORMAL);
// Verify the addition.
const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, credit_card.Compare(*results[0]));
// Make sure the use count and use date were set.
EXPECT_EQ(1U, results[0]->use_count());
EXPECT_NE(base::Time(), results[0]->use_date());
EXPECT_NE(base::Time(), results[0]->modification_date());
}
TEST_F(PersonalDataManagerTest, UpdateUnverifiedProfilesAndCreditCards) {
// Start with unverified data.
AutofillProfile profile(base::GenerateGUID(), "https://www.example.com/");
test::SetProfileInfo(&profile,
"Marion", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
"91601", "US", "12345678910");
EXPECT_FALSE(profile.IsVerified());
CreditCard credit_card(base::GenerateGUID(), "https://www.example.com/");
test::SetCreditCardInfo(&credit_card,
"John Dillinger", "423456789012" /* Visa */, "01", "2999");
EXPECT_FALSE(credit_card.IsVerified());
// Add the data to the database.
personal_data_->AddProfile(profile);
personal_data_->AddCreditCard(credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
const std::vector<AutofillProfile*>& profiles1 =
personal_data_->GetProfiles();
const std::vector<CreditCard*>& cards1 = personal_data_->GetCreditCards();
ASSERT_EQ(1U, profiles1.size());
ASSERT_EQ(1U, cards1.size());
EXPECT_EQ(0, profile.Compare(*profiles1[0]));
EXPECT_EQ(0, credit_card.Compare(*cards1[0]));
// Try to update with just the origin changed.
AutofillProfile original_profile(profile);
CreditCard original_credit_card(credit_card);
profile.set_origin(kSettingsOrigin);
credit_card.set_origin(kSettingsOrigin);
EXPECT_TRUE(profile.IsVerified());
EXPECT_TRUE(credit_card.IsVerified());
personal_data_->UpdateProfile(profile);
personal_data_->UpdateCreditCard(credit_card);
// Note: No refresh, as no update is expected.
const std::vector<AutofillProfile*>& profiles2 =
personal_data_->GetProfiles();
const std::vector<CreditCard*>& cards2 = personal_data_->GetCreditCards();
ASSERT_EQ(1U, profiles2.size());
ASSERT_EQ(1U, cards2.size());
EXPECT_NE(profile.origin(), profiles2[0]->origin());
EXPECT_NE(credit_card.origin(), cards2[0]->origin());
EXPECT_EQ(original_profile.origin(), profiles2[0]->origin());
EXPECT_EQ(original_credit_card.origin(), cards2[0]->origin());
// Try to update with data changed as well.
profile.SetRawInfo(NAME_FIRST, ASCIIToUTF16("John"));
credit_card.SetRawInfo(CREDIT_CARD_NAME_FULL, ASCIIToUTF16("Joe"));
personal_data_->UpdateProfile(profile);
personal_data_->UpdateCreditCard(credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
const std::vector<AutofillProfile*>& profiles3 =
personal_data_->GetProfiles();
const std::vector<CreditCard*>& cards3 = personal_data_->GetCreditCards();
ASSERT_EQ(1U, profiles3.size());
ASSERT_EQ(1U, cards3.size());
EXPECT_EQ(0, profile.Compare(*profiles3[0]));
EXPECT_EQ(0, credit_card.Compare(*cards3[0]));
EXPECT_EQ(profile.origin(), profiles3[0]->origin());
EXPECT_EQ(credit_card.origin(), cards3[0]->origin());
}
// Makes sure that full cards are re-masked when full PAN storage is off.
TEST_F(PersonalDataManagerTest, RefuseToStoreFullCard) {
// On Linux this should be disabled automatically. Elsewhere, only if the
// flag is passed.
#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
EXPECT_FALSE(base::CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableOfferStoreUnmaskedWalletCards));
#else
base::CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kDisableOfferStoreUnmaskedWalletCards);
#endif
std::vector<CreditCard> server_cards;
server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
"347666888555" /* American Express */, "04", "2999");
test::SetServerCreditCards(autofill_table_, server_cards);
personal_data_->Refresh();
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
ASSERT_EQ(1U, personal_data_->GetCreditCards().size());
EXPECT_EQ(CreditCard::MASKED_SERVER_CARD,
personal_data_->GetCreditCards()[0]->record_type());
}
TEST_F(PersonalDataManagerTest, OfferStoreUnmaskedCards) {
#if defined(OS_CHROMEOS) || defined(OS_WIN) || defined(OS_MACOSX) || \
defined(OS_IOS) || defined(OS_ANDROID)
bool should_offer = true;
#elif defined(OS_LINUX)
bool should_offer = false;
#endif
EXPECT_EQ(should_offer, OfferStoreUnmaskedCards());
}
// Tests that UpdateServerCreditCard can be used to mask or unmask server cards.
TEST_F(PersonalDataManagerTest, UpdateServerCreditCards) {
EnableWalletCardImport();
std::vector<CreditCard> server_cards;
server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
"9012" /* Visa */, "01", "2999");
server_cards.back().SetTypeForMaskedCard(kVisaCard);
server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456"));
test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker",
"2109" /* Mastercard */, "12", "2999");
server_cards.back().SetTypeForMaskedCard(kMasterCard);
server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
"347666888555" /* American Express */, "04", "2999");
test::SetServerCreditCards(autofill_table_, server_cards);
personal_data_->Refresh();
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
if (!OfferStoreUnmaskedCards()) {
for (CreditCard* card : personal_data_->GetCreditCards()) {
EXPECT_EQ(CreditCard::MASKED_SERVER_CARD, card->record_type());
}
// The rest of this test doesn't work if we're force-masking all unmasked
// cards.
return;
}
// The GUIDs will be different, so just compare the data.
for (size_t i = 0; i < 3; ++i)
EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
CreditCard* unmasked_card = &server_cards.front();
unmasked_card->set_record_type(CreditCard::FULL_SERVER_CARD);
unmasked_card->SetNumber(ASCIIToUTF16("423456789012"));
EXPECT_NE(0, server_cards.front().Compare(
*personal_data_->GetCreditCards().front()));
personal_data_->UpdateServerCreditCard(*unmasked_card);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
for (size_t i = 0; i < 3; ++i)
EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
CreditCard* remasked_card = &server_cards.back();
remasked_card->set_record_type(CreditCard::MASKED_SERVER_CARD);
remasked_card->SetNumber(ASCIIToUTF16("8555"));
EXPECT_NE(
0, server_cards.back().Compare(*personal_data_->GetCreditCards().back()));
personal_data_->UpdateServerCreditCard(*remasked_card);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
for (size_t i = 0; i < 3; ++i)
EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
}
TEST_F(PersonalDataManagerTest, AddProfilesAndCreditCards) {
AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile0,
"Marion", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
"91601", "US", "12345678910");
AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile1,
"Josephine", "Alicia", "Saenz",
"joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
"US", "19482937549");
CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&credit_card0,
"John Dillinger", "423456789012" /* Visa */, "01", "2999");
CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&credit_card1, "Bonnie Parker",
"518765432109" /* Mastercard */, "12", "2999");
// Add two test profiles to the database.
personal_data_->AddProfile(profile0);
personal_data_->AddProfile(profile1);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
std::vector<AutofillProfile*> profiles;
profiles.push_back(&profile0);
profiles.push_back(&profile1);
ExpectSameElements(profiles, personal_data_->GetProfiles());
// Add two test credit cards to the database.
personal_data_->AddCreditCard(credit_card0);
personal_data_->AddCreditCard(credit_card1);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
std::vector<CreditCard*> cards;
cards.push_back(&credit_card0);
cards.push_back(&credit_card1);
ExpectSameElements(cards, personal_data_->GetCreditCards());
// Determine uniqueness by inserting all of the GUIDs into a set and verifying
// the size of the set matches the number of GUIDs.
std::set<std::string> guids;
guids.insert(profile0.guid());
guids.insert(profile1.guid());
guids.insert(credit_card0.guid());
guids.insert(credit_card1.guid());
EXPECT_EQ(4U, guids.size());
}
// Test for http://crbug.com/50047. Makes sure that guids are populated
// correctly on load.
TEST_F(PersonalDataManagerTest, PopulateUniqueIDsOnLoad) {
AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile0,
"y", "", "", "", "", "", "", "", "", "", "", "");
// Add the profile0 to the db.
personal_data_->AddProfile(profile0);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Verify that we've loaded the profiles from the web database.
const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
ASSERT_EQ(1U, results2.size());
EXPECT_EQ(0, profile0.Compare(*results2[0]));
// Add a new profile.
AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile1,
"z", "", "", "", "", "", "", "", "", "", "", "");
personal_data_->AddProfile(profile1);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Make sure the two profiles have different GUIDs, both valid.
const std::vector<AutofillProfile*>& results3 = personal_data_->GetProfiles();
ASSERT_EQ(2U, results3.size());
EXPECT_NE(results3[0]->guid(), results3[1]->guid());
EXPECT_TRUE(base::IsValidGUID(results3[0]->guid()));
EXPECT_TRUE(base::IsValidGUID(results3[1]->guid()));
}
TEST_F(PersonalDataManagerTest, SetUniqueCreditCardLabels) {
CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
credit_card0.SetRawInfo(CREDIT_CARD_NAME_FULL, ASCIIToUTF16("John"));
CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com");
credit_card1.SetRawInfo(CREDIT_CARD_NAME_FULL, ASCIIToUTF16("Paul"));
CreditCard credit_card2(base::GenerateGUID(), "https://www.example.com");
credit_card2.SetRawInfo(CREDIT_CARD_NAME_FULL, ASCIIToUTF16("Ringo"));
CreditCard credit_card3(base::GenerateGUID(), "https://www.example.com");
credit_card3.SetRawInfo(CREDIT_CARD_NAME_FULL, ASCIIToUTF16("Other"));
CreditCard credit_card4(base::GenerateGUID(), "https://www.example.com");
credit_card4.SetRawInfo(CREDIT_CARD_NAME_FULL, ASCIIToUTF16("Ozzy"));
CreditCard credit_card5(base::GenerateGUID(), "https://www.example.com");
credit_card5.SetRawInfo(CREDIT_CARD_NAME_FULL, ASCIIToUTF16("Dio"));
// Add the test credit cards to the database.
personal_data_->AddCreditCard(credit_card0);
personal_data_->AddCreditCard(credit_card1);
personal_data_->AddCreditCard(credit_card2);
personal_data_->AddCreditCard(credit_card3);
personal_data_->AddCreditCard(credit_card4);
personal_data_->AddCreditCard(credit_card5);
// Reset the PersonalDataManager. This tests that the personal data was saved
// to the web database, and that we can load the credit cards from the web
// database.
ResetPersonalDataManager(USER_MODE_NORMAL);
std::vector<CreditCard*> cards;
cards.push_back(&credit_card0);
cards.push_back(&credit_card1);
cards.push_back(&credit_card2);
cards.push_back(&credit_card3);
cards.push_back(&credit_card4);
cards.push_back(&credit_card5);
ExpectSameElements(cards, personal_data_->GetCreditCards());
}
TEST_F(PersonalDataManagerTest, SetEmptyProfile) {
AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile0,
"", "", "", "", "", "", "", "", "", "", "", "");
// Add the empty profile to the database.
personal_data_->AddProfile(profile0);
// Note: no refresh here.
// Reset the PersonalDataManager. This tests that the personal data was saved
// to the web database, and that we can load the profiles from the web
// database.
ResetPersonalDataManager(USER_MODE_NORMAL);
// Verify that we've loaded the profiles from the web database.
const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
ASSERT_EQ(0U, results2.size());
}
TEST_F(PersonalDataManagerTest, SetEmptyCreditCard) {
CreditCard credit_card0(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&credit_card0, "", "", "", "");
// Add the empty credit card to the database.
personal_data_->AddCreditCard(credit_card0);
// Note: no refresh here.
// Reset the PersonalDataManager. This tests that the personal data was saved
// to the web database, and that we can load the credit cards from the web
// database.
ResetPersonalDataManager(USER_MODE_NORMAL);
// Verify that we've loaded the credit cards from the web database.
const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
ASSERT_EQ(0U, results2.size());
}
TEST_F(PersonalDataManagerTest, Refresh) {
AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile0,
"Marion", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
"91601", "US", "12345678910");
AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile1,
"Josephine", "Alicia", "Saenz",
"joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
"US", "19482937549");
// Add the test profiles to the database.
personal_data_->AddProfile(profile0);
personal_data_->AddProfile(profile1);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
std::vector<AutofillProfile*> profiles;
profiles.push_back(&profile0);
profiles.push_back(&profile1);
ExpectSameElements(profiles, personal_data_->GetProfiles());
AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile2,
"Josephine", "Alicia", "Saenz",
"joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
"32801", "US", "19482937549");
autofill_database_service_->AddAutofillProfile(profile2);
personal_data_->Refresh();
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
profiles.clear();
profiles.push_back(&profile0);
profiles.push_back(&profile1);
profiles.push_back(&profile2);
ExpectSameElements(profiles, personal_data_->GetProfiles());
autofill_database_service_->RemoveAutofillProfile(profile1.guid());
autofill_database_service_->RemoveAutofillProfile(profile2.guid());
// Before telling the PDM to refresh, simulate an edit to one of the deleted
// profiles via a SetProfile update (this would happen if the Autofill window
// was open with a previous snapshot of the profiles, and something
// [e.g. sync] removed a profile from the browser. In this edge case, we will
// end up in a consistent state by dropping the write).
profile0.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Mar"));
profile2.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Jo"));
personal_data_->UpdateProfile(profile0);
personal_data_->AddProfile(profile1);
personal_data_->AddProfile(profile2);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(profile0, *results[0]);
}
// ImportAddressProfiles tests.
TEST_F(PersonalDataManagerTest, ImportAddressProfiles) {
FormData form;
FormFieldData field;
test::CreateTestFormField(
"First name:", "first_name", "George", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Last name:", "last_name", "Washington", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Email:", "email", "theprez@gmail.com", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Address:", "address1", "21 Laussat St", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form.fields.push_back(field);
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure));
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&expected, "George", NULL,
"Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
"San Francisco", "California", "94102", NULL, NULL);
const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, expected.Compare(*results[0]));
}
TEST_F(PersonalDataManagerTest, ImportAddressProfiles_BadEmail) {
FormData form;
FormFieldData field;
test::CreateTestFormField(
"First name:", "first_name", "George", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Last name:", "last_name", "Washington", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Email:", "email", "bogus", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Address:", "address1", "21 Laussat St", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form.fields.push_back(field);
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
EXPECT_FALSE(ImportAddressProfiles(form_structure));
const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
ASSERT_EQ(0U, results.size());
}
// Tests that a 'confirm email' field does not block profile import.
TEST_F(PersonalDataManagerTest, ImportAddressProfiles_TwoEmails) {
FormData form;
FormFieldData field;
test::CreateTestFormField(
"Name:", "name", "George Washington", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Address:", "address1", "21 Laussat St", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Email:", "email", "example@example.com", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Confirm email:", "confirm_email", "example@example.com", "text", &field);
form.fields.push_back(field);
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure));
const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
ASSERT_EQ(1U, results.size());
}
// Tests two email fields containing different values blocks profile import.
TEST_F(PersonalDataManagerTest, ImportAddressProfiles_TwoDifferentEmails) {
FormData form;
FormFieldData field;
test::CreateTestFormField(
"Name:", "name", "George Washington", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Address:", "address1", "21 Laussat St", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Email:", "email", "example@example.com", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Email:", "email2", "example2@example.com", "text", &field);
form.fields.push_back(field);
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
EXPECT_FALSE(ImportAddressProfiles(form_structure));
const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
ASSERT_EQ(0U, results.size());
}
// Tests that not enough filled fields will result in not importing an address.
TEST_F(PersonalDataManagerTest, ImportAddressProfiles_NotEnoughFilledFields) {
FormData form;
FormFieldData field;
test::CreateTestFormField(
"First name:", "first_name", "George", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Last name:", "last_name", "Washington", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Card number:", "card_number", "4111 1111 1111 1111", "text", &field);
form.fields.push_back(field);
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
EXPECT_FALSE(ImportAddressProfiles(form_structure));
const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
ASSERT_EQ(0U, profiles.size());
const std::vector<CreditCard*>& cards = personal_data_->GetCreditCards();
ASSERT_EQ(0U, cards.size());
}
TEST_F(PersonalDataManagerTest, ImportAddressProfiles_MinimumAddressUSA) {
// United States addresses must specifiy one address line, a city, state and
// zip code.
FormData form;
FormFieldData field;
test::CreateTestFormField("Name:", "name", "Barack Obama", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "Washington", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "DC", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "20500", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Country:", "country", "USA", "text", &field);
form.fields.push_back(field);
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure));
const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
ASSERT_EQ(1U, profiles.size());
}
TEST_F(PersonalDataManagerTest, ImportAddressProfiles_MinimumAddressGB) {
// British addresses do not require a state/province as the county is usually
// not requested on forms.
FormData form;
FormFieldData field;
test::CreateTestFormField("Name:", "name", "David Cameron", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Address:", "address", "10 Downing Street", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "London", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Postcode:", "postcode", "SW1A 2AA", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Country:", "country", "United Kingdom", "text", &field);
form.fields.push_back(field);
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure));
const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
ASSERT_EQ(1U, profiles.size());
}
TEST_F(PersonalDataManagerTest, ImportAddressProfiles_MinimumAddressGI) {
// Gibraltar has the most minimal set of requirements for a valid address.
// There are no cities or provinces and no postal/zip code system.
FormData form;
FormFieldData field;
test::CreateTestFormField(
"Name:", "name", "Sir Adrian Johns", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Address:", "address", "The Convent, Main Street", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Country:", "country", "Gibraltar", "text", &field);
form.fields.push_back(field);
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure));
const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
ASSERT_EQ(1U, profiles.size());
}
TEST_F(PersonalDataManagerTest,
ImportAddressProfiles_PhoneNumberSplitAcrossMultipleFields) {
FormData form;
FormFieldData field;
test::CreateTestFormField(
"First name:", "first_name", "George", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Last name:", "last_name", "Washington", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Phone #:", "home_phone_area_code", "650", "text", &field);
field.max_length = 3;
form.fields.push_back(field);
test::CreateTestFormField(
"Phone #:", "home_phone_prefix", "555", "text", &field);
field.max_length = 3;
form.fields.push_back(field);
test::CreateTestFormField(
"Phone #:", "home_phone_suffix", "0000", "text", &field);
field.max_length = 4;
form.fields.push_back(field);
test::CreateTestFormField(
"Address:", "address1", "21 Laussat St", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form.fields.push_back(field);
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure));
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&expected, "George", NULL,
"Washington", NULL, NULL, "21 Laussat St", NULL,
"San Francisco", "California", "94102", NULL, "(650) 555-0000");
const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, expected.Compare(*results[0]));
}
TEST_F(PersonalDataManagerTest, ImportAddressProfiles_MultilineAddress) {
FormData form;
FormFieldData field;
test::CreateTestFormField(
"First name:", "first_name", "George", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Last name:", "last_name", "Washington", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Email:", "email", "theprez@gmail.com", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField(
"Address:",
"street_address",
"21 Laussat St\n"
"Apt. #42",
"textarea",
&field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form.fields.push_back(field);
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure));
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&expected, "George", NULL,
"Washington", "theprez@gmail.com", NULL, "21 Laussat St", "Apt. #42",
"San Francisco", "California", "94102", NULL, NULL);
const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, expected.Compare(*results[0]));
}
TEST_F(PersonalDataManagerTest,
ImportAddressProfiles_TwoValidProfilesDifferentForms) {
FormData form1;
FormFieldData field;
test::CreateTestFormField(
"First name:", "first_name", "George", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField(
"Last name:", "last_name", "Washington", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField(
"Email:", "email", "theprez@gmail.com", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField(
"Address:", "address1", "21 Laussat St", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form1.fields.push_back(field);
FormStructure form_structure1(form1);
form_structure1.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure1));
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&expected, "George", NULL,
"Washington", "theprez@gmail.com", NULL, "21 Laussat St", NULL,
"San Francisco", "California", "94102", NULL, NULL);
const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
ASSERT_EQ(1U, results1.size());
EXPECT_EQ(0, expected.Compare(*results1[0]));
// Now create a completely different profile.
FormData form2;
test::CreateTestFormField(
"First name:", "first_name", "John", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField(
"Last name:", "last_name", "Adams", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField(
"Email:", "email", "second@gmail.com", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField(
"Address:", "address1", "22 Laussat St", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form2.fields.push_back(field);
FormStructure form_structure2(form2);
form_structure2.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure2));
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
AutofillProfile expected2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&expected2, "John", NULL,
"Adams", "second@gmail.com", NULL, "22 Laussat St", NULL,
"San Francisco", "California", "94102", NULL, NULL);
std::vector<AutofillProfile*> profiles;
profiles.push_back(&expected);
profiles.push_back(&expected2);
ExpectSameElements(profiles, personal_data_->GetProfiles());
}
TEST_F(PersonalDataManagerTest,
ImportAddressProfiles_TwoValidProfilesSameForm) {
FormData form;
FormFieldData field;
test::CreateTestFormField("First name:", "first_name", "George", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Last name:", "last_name", "Washington", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Email:", "email", "theprez@gmail.com", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Address:", "address1", "21 Laussat St", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form.fields.push_back(field);
// Different address.
test::CreateTestFormField("First name:", "first_name", "John", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Last name:", "last_name", "Adams", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Email:", "email", "second@gmail.com", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Address:", "address1", "22 Laussat St", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form.fields.push_back(field);
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure));
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&expected, "George", NULL, "Washington",
"theprez@gmail.com", NULL, "21 Laussat St", NULL,
"San Francisco", "California", "94102", NULL, NULL);
AutofillProfile expected2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&expected2, "John", NULL, "Adams", "second@gmail.com",
NULL, "22 Laussat St", NULL, "San Francisco",
"California", "94102", NULL, NULL);
const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
ASSERT_EQ(2U, results.size());
std::vector<AutofillProfile*> profiles;
profiles.push_back(&expected);
profiles.push_back(&expected2);
ExpectSameElements(profiles, personal_data_->GetProfiles());
}
TEST_F(PersonalDataManagerTest,
ImportAddressProfiles_OneValidProfileSameForm_PartsHidden) {
FormData form;
FormFieldData field;
test::CreateTestFormField("First name:", "first_name", "George", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Last name:", "last_name", "Washington", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Email:", "email", "theprez@gmail.com", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Address:", "address1", "21 Laussat St", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form.fields.push_back(field);
// There is an empty but hidden form section (this has been observed on sites
// where users can choose which form section they choose by unhiding it).
test::CreateTestFormField("First name:", "first_name", "", "text",
&field);
field.is_focusable = false;
form.fields.push_back(field);
test::CreateTestFormField("Last name:", "last_name", "", "text", &field);
field.is_focusable = false;
form.fields.push_back(field);
test::CreateTestFormField("Email:", "email", "", "text",
&field);
field.is_focusable = false;
form.fields.push_back(field);
test::CreateTestFormField("Address:", "address1", "", "text",
&field);
field.is_focusable = false;
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "", "text", &field);
field.is_focusable = false;
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "", "text", &field);
field.is_focusable = false;
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "", "text", &field);
field.is_focusable = false;
form.fields.push_back(field);
// Still able to do the import.
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure));
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&expected, "George", NULL, "Washington",
"theprez@gmail.com", NULL, "21 Laussat St", NULL,
"San Francisco", "California", "94102", NULL, NULL);
const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
ASSERT_EQ(1U, results.size());
std::vector<AutofillProfile*> profiles;
profiles.push_back(&expected);
ExpectSameElements(profiles, personal_data_->GetProfiles());
}
// A maximum of two address profiles are imported per form.
TEST_F(PersonalDataManagerTest,
ImportAddressProfiles_ThreeValidProfilesSameForm) {
FormData form;
FormFieldData field;
test::CreateTestFormField("First name:", "first_name", "George", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Last name:", "last_name", "Washington", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Email:", "email", "theprez@gmail.com", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Address:", "address1", "21 Laussat St", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form.fields.push_back(field);
// Different address within the same form.
test::CreateTestFormField("First name:", "first_name", "John", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Last name:", "last_name", "Adams", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Email:", "email", "second@gmail.com", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Address:", "address1", "22 Laussat St", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form.fields.push_back(field);
// Yet another different address.
test::CreateTestFormField("First name:", "first_name", "David", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Last name:", "last_name", "Cameron", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Address:", "address", "10 Downing Street", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "London", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Postcode:", "postcode", "SW1A 2AA", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Country:", "country", "United Kingdom", "text",
&field);
form.fields.push_back(field);
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure));
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Only two are saved.
AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&expected, "George", NULL, "Washington",
"theprez@gmail.com", NULL, "21 Laussat St", NULL,
"San Francisco", "California", "94102", NULL, NULL);
AutofillProfile expected2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&expected2, "John", NULL, "Adams", "second@gmail.com",
NULL, "22 Laussat St", NULL, "San Francisco",
"California", "94102", NULL, NULL);
const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
ASSERT_EQ(2U, results.size());
std::vector<AutofillProfile*> profiles;
profiles.push_back(&expected);
profiles.push_back(&expected2);
ExpectSameElements(profiles, personal_data_->GetProfiles());
}
TEST_F(PersonalDataManagerTest, ImportAddressProfiles_SameProfileWithConflict) {
FormData form1;
FormFieldData field;
test::CreateTestFormField(
"First name:", "first_name", "George", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField(
"Last name:", "last_name", "Washington", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField(
"Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField(
"Address Line 2:", "address2", "Suite A", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField(
"Email:", "email", "theprez@gmail.com", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField("Phone:", "phone", "6505556666", "text", &field);
form1.fields.push_back(field);
FormStructure form_structure1(form1);
form_structure1.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure1));
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&expected, "George", nullptr, "Washington",
"theprez@gmail.com", nullptr, "1600 Pennsylvania Avenue",
"Suite A", "San Francisco", "California", "94102",
nullptr, "(650) 555-6666");
const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
ASSERT_EQ(1U, results1.size());
EXPECT_EQ(0, expected.Compare(*results1[0]));
// Now create an updated profile.
FormData form2;
test::CreateTestFormField(
"First name:", "first_name", "George", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField(
"Last name:", "last_name", "Washington", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField(
"Address:", "address", "1600 Pennsylvania Avenue", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField(
"Address Line 2:", "address2", "Suite A", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField(
"Email:", "email", "theprez@gmail.com", "text", &field);
form2.fields.push_back(field);
// Country gets added.
test::CreateTestFormField("Country:", "country", "USA", "text", &field);
form2.fields.push_back(field);
// Same phone number with different formatting doesn't create a new profile.
test::CreateTestFormField("Phone:", "phone", "650-555-6666", "text", &field);
form2.fields.push_back(field);
FormStructure form_structure2(form2);
form_structure2.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure2));
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
// Full name, phone formatting and country are updated.
expected.SetRawInfo(NAME_FULL, ASCIIToUTF16("George Washington"));
expected.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("+1 650-555-6666"));
expected.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
ASSERT_EQ(1U, results2.size());
EXPECT_EQ(0, expected.Compare(*results2[0]));
}
TEST_F(PersonalDataManagerTest, ImportAddressProfiles_MissingInfoInOld) {
FormData form1;
FormFieldData field;
test::CreateTestFormField(
"First name:", "first_name", "George", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField(
"Last name:", "last_name", "Washington", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField(
"Address Line 1:", "address", "190 High Street", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
form1.fields.push_back(field);
FormStructure form_structure1(form1);
form_structure1.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure1));
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&expected, "George", NULL,
"Washington", NULL, NULL, "190 High Street", NULL,
"Philadelphia", "Pennsylvania", "19106", NULL, NULL);
const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
ASSERT_EQ(1U, results1.size());
EXPECT_EQ(0, expected.Compare(*results1[0]));
// Submit a form with new data for the first profile.
FormData form2;
test::CreateTestFormField(
"First name:", "first_name", "George", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField(
"Last name:", "last_name", "Washington", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField(
"Email:", "email", "theprez@gmail.com", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField(
"Address Line 1:", "address", "190 High Street", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
form2.fields.push_back(field);
FormStructure form_structure2(form2);
form_structure2.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure2));
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
AutofillProfile expected2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&expected2, "George", NULL,
"Washington", "theprez@gmail.com", NULL, "190 High Street", NULL,
"Philadelphia", "Pennsylvania", "19106", NULL, NULL);
expected2.SetRawInfo(NAME_FULL, ASCIIToUTF16("George Washington"));
ASSERT_EQ(1U, results2.size());
EXPECT_EQ(0, expected2.Compare(*results2[0]));
}
TEST_F(PersonalDataManagerTest, ImportAddressProfiles_MissingInfoInNew) {
FormData form1;
FormFieldData field;
test::CreateTestFormField(
"First name:", "first_name", "George", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField(
"Last name:", "last_name", "Washington", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField(
"Company:", "company", "Government", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField(
"Email:", "email", "theprez@gmail.com", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField(
"Address Line 1:", "address", "190 High Street", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
form1.fields.push_back(field);
FormStructure form_structure1(form1);
form_structure1.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure1));
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&expected, "George", NULL,
"Washington", "theprez@gmail.com", "Government", "190 High Street", NULL,
"Philadelphia", "Pennsylvania", "19106", NULL, NULL);
const std::vector<AutofillProfile*>& results1 = personal_data_->GetProfiles();
ASSERT_EQ(1U, results1.size());
EXPECT_EQ(0, expected.Compare(*results1[0]));
// Submit a form with new data for the first profile.
FormData form2;
test::CreateTestFormField(
"First name:", "first_name", "George", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField(
"Last name:", "last_name", "Washington", "text", &field);
form2.fields.push_back(field);
// Note missing Company field.
test::CreateTestFormField(
"Email:", "email", "theprez@gmail.com", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField(
"Address Line 1:", "address", "190 High Street", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField("State:", "state", "Pennsylvania", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField("Zip:", "zipcode", "19106", "text", &field);
form2.fields.push_back(field);
FormStructure form_structure2(form2);
form_structure2.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure2));
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
// The merge operation will populate the full name if it's empty.
expected.SetRawInfo(NAME_FULL, ASCIIToUTF16("George Washington"));
ASSERT_EQ(1U, results2.size());
EXPECT_EQ(0, expected.Compare(*results2[0]));
}
TEST_F(PersonalDataManagerTest, ImportAddressProfiles_InsufficientAddress) {
FormData form1;
FormFieldData field;
test::CreateTestFormField(
"First name:", "first_name", "George", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField(
"Last name:", "last_name", "Washington", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField(
"Company:", "company", "Government", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField(
"Email:", "email", "theprez@gmail.com", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField(
"Address Line 1:", "address", "190 High Street", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField("City:", "city", "Philadelphia", "text", &field);
form1.fields.push_back(field);
FormStructure form_structure1(form1);
form_structure1.DetermineHeuristicTypes();
EXPECT_FALSE(ImportAddressProfiles(form_structure1));
// Since no refresh is expected, reload the data from the database to make
// sure no changes were written out.
ResetPersonalDataManager(USER_MODE_NORMAL);
const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
ASSERT_EQ(0U, profiles.size());
const std::vector<CreditCard*>& cards = personal_data_->GetCreditCards();
ASSERT_EQ(0U, cards.size());
}
// Ensure that if a verified profile already exists, aggregated profiles cannot
// modify it in any way. This also checks the profile merging/matching algorithm
// works: if either the full name OR all the non-empty name pieces match, the
// profile is a match.
TEST_F(PersonalDataManagerTest,
ImportAddressProfiles_ExistingVerifiedProfileWithConflict) {
// Start with a verified profile.
AutofillProfile profile(base::GenerateGUID(), kSettingsOrigin);
test::SetProfileInfo(&profile, "Marion", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5",
"Hollywood", "CA", "91601", "US", "12345678910");
EXPECT_TRUE(profile.IsVerified());
// Add the profile to the database.
personal_data_->AddProfile(profile);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Simulate a form submission with conflicting info.
FormData form;
FormFieldData field;
test::CreateTestFormField("First name:", "first_name", "Marion Mitchell",
"text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Last name:", "last_name", "Morrison", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Email:", "email", "johnwayne@me.xyz", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Address:", "address1", "123 Zoo St.", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "Hollywood", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "CA", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "91601", "text", &field);
form.fields.push_back(field);
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure));
// Wait for the refresh, which in this case is a no-op.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Expect that no new profile is saved.
const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, profile.Compare(*results[0]));
// Try the same thing, but without "Mitchell". The profiles should still match
// because the non empty name pieces (first and last) match that stored in the
// profile.
test::CreateTestFormField("First name:", "first_name", "Marion", "text",
&field);
form.fields[0] = field;
FormStructure form_structure2(form);
form_structure2.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure2));
// Wait for the refresh, which in this case is a no-op.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Expect that no new profile is saved.
const std::vector<AutofillProfile*>& results2 = personal_data_->GetProfiles();
ASSERT_EQ(1U, results2.size());
EXPECT_EQ(0, profile.Compare(*results2[0]));
}
// Tests that no profile is inferred if the country is not recognized.
TEST_F(PersonalDataManagerTest, ImportAddressProfiles_UnrecognizedCountry) {
FormData form;
FormFieldData field;
test::CreateTestFormField("First name:", "first_name", "George", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Last name:", "last_name", "Washington", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Email:", "email", "theprez@gmail.com", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Address:", "address1", "21 Laussat St", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Country:", "country", "Notacountry", "text",
&field);
form.fields.push_back(field);
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
EXPECT_FALSE(ImportAddressProfiles(form_structure));
// Since no refresh is expected, reload the data from the database to make
// sure no changes were written out.
ResetPersonalDataManager(USER_MODE_NORMAL);
const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
ASSERT_EQ(0U, profiles.size());
const std::vector<CreditCard*>& cards = personal_data_->GetCreditCards();
ASSERT_EQ(0U, cards.size());
}
// Tests that a profile is created for countries with composed names.
TEST_F(PersonalDataManagerTest,
ImportAddressProfiles_CompleteComposedCountryName) {
FormData form;
FormFieldData field;
test::CreateTestFormField("First name:", "first_name", "George", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Last name:", "last_name", "Washington", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Email:", "email", "theprez@gmail.com", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Address:", "address1", "21 Laussat St", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Country:", "country", "Myanmar [Burma]", "text",
&field);
form.fields.push_back(field);
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
EXPECT_TRUE(ImportAddressProfiles(form_structure));
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
AutofillProfile expected(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&expected, "George", nullptr, "Washington",
"theprez@gmail.com", nullptr, "21 Laussat St", nullptr,
"San Francisco", "California", "94102", "MM", nullptr);
const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, expected.Compare(*results[0]));
}
// TODO(crbug.com/634131): Create profiles if part of a standalone part of a
// composed country name is present.
// Tests that a profile is created if a standalone part of a composed country
// name is present.
TEST_F(PersonalDataManagerTest,
ImportAddressProfiles_IncompleteComposedCountryName) {
FormData form;
FormFieldData field;
test::CreateTestFormField("First name:", "first_name", "George", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Last name:", "last_name", "Washington", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Email:", "email", "theprez@gmail.com", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Address:", "address1", "21 Laussat St", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Country:", "country",
"Myanmar", // Missing the [Burma] part
"text", &field);
form.fields.push_back(field);
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
EXPECT_FALSE(ImportAddressProfiles(form_structure));
// Since no refresh is expected, reload the data from the database to make
// sure no changes were written out.
ResetPersonalDataManager(USER_MODE_NORMAL);
const std::vector<AutofillProfile*>& profiles = personal_data_->GetProfiles();
ASSERT_EQ(0U, profiles.size());
const std::vector<CreditCard*>& cards = personal_data_->GetCreditCards();
ASSERT_EQ(0U, cards.size());
}
// ImportCreditCard tests.
// Tests that a valid credit card is extracted.
TEST_F(PersonalDataManagerTest, ImportCreditCard_Valid) {
// Add a single valid credit card form.
FormData form;
AddFullCreditCardForm(&form, "Biggie Smalls", "4111-1111-1111-1111", "01",
"2999");
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card;
EXPECT_TRUE(ImportCreditCard(form_structure, false, &imported_credit_card));
ASSERT_TRUE(imported_credit_card);
personal_data_->SaveImportedCreditCard(*imported_credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
CreditCard expected(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&expected, "Biggie Smalls", "4111111111111111", "01",
"2999");
const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, expected.Compare(*results[0]));
}
// Tests that an invalid credit card is not extracted.
TEST_F(PersonalDataManagerTest, ImportCreditCard_Invalid) {
FormData form;
AddFullCreditCardForm(&form, "Jim Johansen", "1000000000000000", "02",
"2999");
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card;
EXPECT_FALSE(ImportCreditCard(form_structure, false, &imported_credit_card));
ASSERT_FALSE(imported_credit_card);
// Since no refresh is expected, reload the data from the database to make
// sure no changes were written out.
ResetPersonalDataManager(USER_MODE_NORMAL);
const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
ASSERT_EQ(0U, results.size());
}
// Tests that a valid credit card is extracted when the option text for month
// select can't be parsed but its value can.
TEST_F(PersonalDataManagerTest, ImportCreditCard_MonthSelectInvalidText) {
// Add a single valid credit card form with an invalid option value.
FormData form;
AddFullCreditCardForm(&form, "Biggie Smalls", "4111-1111-1111-1111",
"Feb (2)", "2999");
// Add option values and contents to the expiration month field.
ASSERT_EQ(ASCIIToUTF16("exp_month"), form.fields[2].name);
std::vector<base::string16> values;
values.push_back(ASCIIToUTF16("1"));
values.push_back(ASCIIToUTF16("2"));
values.push_back(ASCIIToUTF16("3"));
std::vector<base::string16> contents;
contents.push_back(ASCIIToUTF16("Jan (1)"));
contents.push_back(ASCIIToUTF16("Feb (2)"));
contents.push_back(ASCIIToUTF16("Mar (3)"));
form.fields[2].option_values = values;
form.fields[2].option_contents = contents;
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card;
EXPECT_TRUE(ImportCreditCard(form_structure, false, &imported_credit_card));
ASSERT_TRUE(imported_credit_card);
personal_data_->SaveImportedCreditCard(*imported_credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// See that the invalid option text was converted to the right value.
CreditCard expected(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&expected, "Biggie Smalls", "4111111111111111", "02",
"2999");
const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, expected.Compare(*results[0]));
}
TEST_F(PersonalDataManagerTest, ImportCreditCard_TwoValidCards) {
// Start with a single valid credit card form.
FormData form1;
AddFullCreditCardForm(&form1, "Biggie Smalls", "4111-1111-1111-1111", "01",
"2999");
FormStructure form_structure1(form1);
form_structure1.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card;
EXPECT_TRUE(ImportCreditCard(form_structure1, false, &imported_credit_card));
ASSERT_TRUE(imported_credit_card);
personal_data_->SaveImportedCreditCard(*imported_credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
CreditCard expected(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&expected, "Biggie Smalls", "4111111111111111", "01",
"2999");
const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, expected.Compare(*results[0]));
// Add a second different valid credit card.
FormData form2;
AddFullCreditCardForm(&form2, "", "5500 0000 0000 0004", "02", "2999");
FormStructure form_structure2(form2);
form_structure2.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card2;
EXPECT_TRUE(ImportCreditCard(form_structure2, false, &imported_credit_card2));
ASSERT_TRUE(imported_credit_card2);
personal_data_->SaveImportedCreditCard(*imported_credit_card2);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&expected2, "", "5500000000000004", "02", "2999");
std::vector<CreditCard*> cards;
cards.push_back(&expected);
cards.push_back(&expected2);
ExpectSameElements(cards, personal_data_->GetCreditCards());
}
// This form has the expiration year as one field with MM/YY.
TEST_F(PersonalDataManagerTest, ImportCreditCard_Month2DigitYearCombination) {
FormData form;
FormFieldData field;
test::CreateTestFormField("Name on card:", "name_on_card", "John MMYY",
"text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Card Number:", "card_number", "4111111111111111",
"text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Exp Date:", "exp_date", "05/45", "text", &field);
field.autocomplete_attribute = "cc-exp";
field.max_length = 5;
form.fields.push_back(field);
SubmitFormAndExpectImportedCardWithData(form, "John MMYY", "4111111111111111",
"05", "2045");
}
// This form has the expiration year as one field with MM/YYYY.
TEST_F(PersonalDataManagerTest, ImportCreditCard_Month4DigitYearCombination) {
FormData form;
FormFieldData field;
test::CreateTestFormField("Name on card:", "name_on_card", "John MMYYYY",
"text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Card Number:", "card_number", "4111111111111111",
"text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Exp Date:", "exp_date", "05/2045", "text", &field);
field.autocomplete_attribute = "cc-exp";
field.max_length = 7;
form.fields.push_back(field);
SubmitFormAndExpectImportedCardWithData(form, "John MMYYYY",
"4111111111111111", "05", "2045");
}
// This form has the expiration year as one field with M/YYYY.
TEST_F(PersonalDataManagerTest, ImportCreditCard_1DigitMonth4DigitYear) {
FormData form;
FormFieldData field;
test::CreateTestFormField("Name on card:", "name_on_card", "John MYYYY",
"text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Card Number:", "card_number", "4111111111111111",
"text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Exp Date:", "exp_date", "5/2045", "text", &field);
field.autocomplete_attribute = "cc-exp";
form.fields.push_back(field);
SubmitFormAndExpectImportedCardWithData(form, "John MYYYY",
"4111111111111111", "05", "2045");
}
// This form has the expiration year as a 2-digit field.
TEST_F(PersonalDataManagerTest, ImportCreditCard_2DigitYear) {
FormData form;
FormFieldData field;
test::CreateTestFormField("Name on card:", "name_on_card", "John Smith",
"text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Card Number:", "card_number", "4111111111111111",
"text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Exp Month:", "exp_month", "05", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Exp Year:", "exp_year", "45", "text", &field);
field.max_length = 2;
form.fields.push_back(field);
SubmitFormAndExpectImportedCardWithData(form, "John Smith",
"4111111111111111", "05", "2045");
}
// Tests that a credit card is extracted because it only matches a masked server
// card.
TEST_F(PersonalDataManagerTest,
ImportCreditCard_DuplicateServerCards_MaskedCard) {
// Add a masked server card.
std::vector<CreditCard> server_cards;
server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
"1111" /* Visa */, "01", "2999");
server_cards.back().SetTypeForMaskedCard(kVisaCard);
test::SetServerCreditCards(autofill_table_, server_cards);
// Type the same data as the masked card into a form.
FormData form;
AddFullCreditCardForm(&form, "John Dillinger", "4111111111111111", "01",
"2999");
// The card should be offered to be saved locally because it only matches the
// masked server card.
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card;
EXPECT_TRUE(ImportCreditCard(form_structure, false, &imported_credit_card));
ASSERT_TRUE(imported_credit_card);
personal_data_->SaveImportedCreditCard(*imported_credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
}
// Tests that a credit card is not extracted because it matches a full server
// card.
TEST_F(PersonalDataManagerTest,
ImportCreditCard_DuplicateServerCards_FullCard) {
// Add a full server card.
std::vector<CreditCard> server_cards;
server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
"347666888555" /* American Express */, "04", "2999");
test::SetServerCreditCards(autofill_table_, server_cards);
// Type the same data as the unmasked card into a form.
FormData form;
AddFullCreditCardForm(&form, "Clyde Barrow", "347666888555", "04", "2999");
// The card should not be offered to be saved locally because it only matches
// the full server card.
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card;
EXPECT_FALSE(ImportCreditCard(form_structure, false, &imported_credit_card));
ASSERT_FALSE(imported_credit_card);
}
TEST_F(PersonalDataManagerTest, ImportCreditCard_SameCreditCardWithConflict) {
// Start with a single valid credit card form.
FormData form1;
AddFullCreditCardForm(&form1, "Biggie Smalls", "4111-1111-1111-1111", "01",
"2998");
FormStructure form_structure1(form1);
form_structure1.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card;
EXPECT_TRUE(ImportCreditCard(form_structure1, false, &imported_credit_card));
ASSERT_TRUE(imported_credit_card);
personal_data_->SaveImportedCreditCard(*imported_credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
CreditCard expected(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&expected, "Biggie Smalls", "4111111111111111", "01",
"2998");
const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, expected.Compare(*results[0]));
// Add a second different valid credit card where the year is different but
// the credit card number matches.
FormData form2;
AddFullCreditCardForm(&form2, "Biggie Smalls", "4111 1111 1111 1111", "01",
/* different year */ "2999");
FormStructure form_structure2(form2);
form_structure2.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card2;
EXPECT_TRUE(ImportCreditCard(form_structure2, false, &imported_credit_card2));
EXPECT_FALSE(imported_credit_card2);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Expect that the newer information is saved. In this case the year is
// updated to "2999".
CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&expected2, "Biggie Smalls", "4111111111111111", "01",
"2999");
const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results2.size());
EXPECT_EQ(0, expected2.Compare(*results2[0]));
}
TEST_F(PersonalDataManagerTest, ImportCreditCard_ShouldReturnLocalCard) {
// Start with a single valid credit card form.
FormData form1;
AddFullCreditCardForm(&form1, "Biggie Smalls", "4111-1111-1111-1111", "01",
"2998");
FormStructure form_structure1(form1);
form_structure1.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card;
EXPECT_TRUE(ImportCreditCard(form_structure1, false, &imported_credit_card));
ASSERT_TRUE(imported_credit_card);
personal_data_->SaveImportedCreditCard(*imported_credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
CreditCard expected(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&expected, "Biggie Smalls", "4111111111111111", "01",
"2998");
const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, expected.Compare(*results[0]));
// Add a second different valid credit card where the year is different but
// the credit card number matches.
FormData form2;
AddFullCreditCardForm(&form2, "Biggie Smalls", "4111 1111 1111 1111", "01",
/* different year */ "2999");
FormStructure form_structure2(form2);
form_structure2.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card2;
EXPECT_TRUE(ImportCreditCard(form_structure2,
/* should_return_local_card= */ true,
&imported_credit_card2));
// The local card is returned after an update.
EXPECT_TRUE(imported_credit_card2);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Expect that the newer information is saved. In this case the year is
// updated to "2999".
CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&expected2, "Biggie Smalls", "4111111111111111", "01",
"2999");
const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results2.size());
EXPECT_EQ(0, expected2.Compare(*results2[0]));
}
TEST_F(PersonalDataManagerTest, ImportCreditCard_EmptyCardWithConflict) {
// Start with a single valid credit card form.
FormData form1;
AddFullCreditCardForm(&form1, "Biggie Smalls", "4111-1111-1111-1111", "01",
"2998");
FormStructure form_structure1(form1);
form_structure1.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card;
EXPECT_TRUE(ImportCreditCard(form_structure1, false, &imported_credit_card));
ASSERT_TRUE(imported_credit_card);
personal_data_->SaveImportedCreditCard(*imported_credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
CreditCard expected(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&expected, "Biggie Smalls", "4111111111111111", "01",
"2998");
const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, expected.Compare(*results[0]));
// Add a second credit card with no number.
FormData form2;
AddFullCreditCardForm(&form2, "Biggie Smalls", /* no number */ nullptr, "01",
"2999");
FormStructure form_structure2(form2);
form_structure2.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card2;
EXPECT_FALSE(
ImportCreditCard(form_structure2, false, &imported_credit_card2));
EXPECT_FALSE(imported_credit_card2);
// Since no refresh is expected, reload the data from the database to make
// sure no changes were written out.
ResetPersonalDataManager(USER_MODE_NORMAL);
// No change is expected.
CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&expected2, "Biggie Smalls", "4111111111111111", "01",
"2998");
const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results2.size());
EXPECT_EQ(0, expected2.Compare(*results2[0]));
}
TEST_F(PersonalDataManagerTest, ImportCreditCard_MissingInfoInNew) {
// Start with a single valid credit card form.
FormData form1;
AddFullCreditCardForm(&form1, "Biggie Smalls", "4111-1111-1111-1111", "01",
"2999");
FormStructure form_structure1(form1);
form_structure1.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card;
EXPECT_TRUE(ImportCreditCard(form_structure1, false, &imported_credit_card));
ASSERT_TRUE(imported_credit_card);
personal_data_->SaveImportedCreditCard(*imported_credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
CreditCard expected(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&expected,
"Biggie Smalls", "4111111111111111", "01", "2999");
const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, expected.Compare(*results[0]));
// Add a second different valid credit card where the name is missing but
// the credit card number matches.
FormData form2;
AddFullCreditCardForm(&form2, /* missing name */ nullptr,
"4111-1111-1111-1111", "01", "2999");
FormStructure form_structure2(form2);
form_structure2.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card2;
EXPECT_TRUE(ImportCreditCard(form_structure2, false, &imported_credit_card2));
EXPECT_FALSE(imported_credit_card2);
// Since no refresh is expected, reload the data from the database to make
// sure no changes were written out.
ResetPersonalDataManager(USER_MODE_NORMAL);
// No change is expected.
CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&expected2,
"Biggie Smalls", "4111111111111111", "01", "2999");
const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results2.size());
EXPECT_EQ(0, expected2.Compare(*results2[0]));
// Add a third credit card where the expiration date is missing.
FormData form3;
AddFullCreditCardForm(&form3, "Johnny McEnroe", "5555555555554444",
/* no month */ nullptr,
/* no year */ nullptr);
FormStructure form_structure3(form3);
form_structure3.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card3;
EXPECT_FALSE(
ImportCreditCard(form_structure3, false, &imported_credit_card3));
ASSERT_FALSE(imported_credit_card3);
// Since no refresh is expected, reload the data from the database to make
// sure no changes were written out.
ResetPersonalDataManager(USER_MODE_NORMAL);
// No change is expected.
CreditCard expected3(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&expected3,
"Biggie Smalls", "4111111111111111", "01", "2999");
const std::vector<CreditCard*>& results3 = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results3.size());
EXPECT_EQ(0, expected3.Compare(*results3[0]));
}
TEST_F(PersonalDataManagerTest, ImportCreditCard_MissingInfoInOld) {
// Start with a single valid credit card stored via the preferences.
// Note the empty name.
CreditCard saved_credit_card(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&saved_credit_card, "", "4111111111111111" /* Visa */,
"01", "2998");
personal_data_->AddCreditCard(saved_credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
const std::vector<CreditCard*>& results1 = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results1.size());
EXPECT_EQ(saved_credit_card, *results1[0]);
// Add a second different valid credit card where the year is different but
// the credit card number matches.
FormData form;
AddFullCreditCardForm(&form, "Biggie Smalls", "4111-1111-1111-1111", "01",
/* different year */ "2999");
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card;
EXPECT_TRUE(ImportCreditCard(form_structure, false, &imported_credit_card));
EXPECT_FALSE(imported_credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Expect that the newer information is saved. In this case the year is
// added to the existing credit card.
CreditCard expected2(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&expected2, "Biggie Smalls", "4111111111111111", "01",
"2999");
const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results2.size());
EXPECT_EQ(0, expected2.Compare(*results2[0]));
}
// We allow the user to store a credit card number with separators via the UI.
// We should not try to re-aggregate the same card with the separators stripped.
TEST_F(PersonalDataManagerTest, ImportCreditCard_SameCardWithSeparators) {
// Start with a single valid credit card stored via the preferences.
// Note the separators in the credit card number.
CreditCard saved_credit_card(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&saved_credit_card,
"Biggie Smalls", "4111 1111 1111 1111" /* Visa */, "01", "2999");
personal_data_->AddCreditCard(saved_credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
const std::vector<CreditCard*>& results1 = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results1.size());
EXPECT_EQ(0, saved_credit_card.Compare(*results1[0]));
// Import the same card info, but with different separators in the number.
FormData form;
AddFullCreditCardForm(&form, "Biggie Smalls", "4111-1111-1111-1111", "01",
"2999");
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card;
EXPECT_TRUE(ImportCreditCard(form_structure, false, &imported_credit_card));
EXPECT_FALSE(imported_credit_card);
// Since no refresh is expected, reload the data from the database to make
// sure no changes were written out.
ResetPersonalDataManager(USER_MODE_NORMAL);
// Expect that no new card is saved.
const std::vector<CreditCard*>& results2 = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results2.size());
EXPECT_EQ(0, saved_credit_card.Compare(*results2[0]));
}
// Ensure that if a verified credit card already exists, aggregated credit cards
// cannot modify it in any way.
TEST_F(PersonalDataManagerTest,
ImportCreditCard_ExistingVerifiedCardWithConflict) {
// Start with a verified credit card.
CreditCard credit_card(base::GenerateGUID(), kSettingsOrigin);
test::SetCreditCardInfo(&credit_card, "Biggie Smalls",
"4111 1111 1111 1111" /* Visa */, "01", "2998");
EXPECT_TRUE(credit_card.IsVerified());
// Add the credit card to the database.
personal_data_->AddCreditCard(credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Simulate a form submission with conflicting expiration year.
FormData form;
AddFullCreditCardForm(&form, "Biggie Smalls", "4111 1111 1111 1111", "01",
/* different year */ "2999");
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card;
EXPECT_TRUE(ImportCreditCard(form_structure, false, &imported_credit_card));
ASSERT_FALSE(imported_credit_card);
// Since no refresh is expected, reload the data from the database to make
// sure no changes were written out.
ResetPersonalDataManager(USER_MODE_NORMAL);
// Expect that the saved credit card is not modified.
const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, credit_card.Compare(*results[0]));
}
// ImportFormData tests (both addresses and credit cards).
// Test that a form with both address and credit card sections imports the
// address and the credit card.
TEST_F(PersonalDataManagerTest, ImportFormData_OneAddressOneCreditCard) {
FormData form;
FormFieldData field;
// Address section.
test::CreateTestFormField("First name:", "first_name", "George", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Last name:", "last_name", "Washington", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Email:", "email", "theprez@gmail.com", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Address:", "address1", "21 Laussat St", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form.fields.push_back(field);
// Credit card section.
AddFullCreditCardForm(&form, "Biggie Smalls", "4111-1111-1111-1111", "01",
"2999");
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card;
EXPECT_TRUE(personal_data_->ImportFormData(form_structure, false,
&imported_credit_card));
ASSERT_TRUE(imported_credit_card);
personal_data_->SaveImportedCreditCard(*imported_credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Test that the address has been saved.
AutofillProfile expected_address(base::GenerateGUID(),
"https://www.example.com");
test::SetProfileInfo(&expected_address, "George", NULL, "Washington",
"theprez@gmail.com", NULL, "21 Laussat St", NULL,
"San Francisco", "California", "94102", NULL, NULL);
const std::vector<AutofillProfile*>& results_addr =
personal_data_->GetProfiles();
ASSERT_EQ(1U, results_addr.size());
EXPECT_EQ(0, expected_address.Compare(*results_addr[0]));
// Test that the credit card has also been saved.
CreditCard expected_card(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&expected_card, "Biggie Smalls", "4111111111111111",
"01", "2999");
const std::vector<CreditCard*>& results_cards =
personal_data_->GetCreditCards();
ASSERT_EQ(1U, results_cards.size());
EXPECT_EQ(0, expected_card.Compare(*results_cards[0]));
}
// Test that a form with two address sections and a credit card section does not
// import the address but does import the credit card.
TEST_F(PersonalDataManagerTest, ImportFormData_TwoAddressesOneCreditCard) {
FormData form;
FormFieldData field;
// Address section 1.
test::CreateTestFormField("First name:", "first_name", "George", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Last name:", "last_name", "Washington", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Email:", "email", "theprez@gmail.com", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("Address:", "address1", "21 Laussat St", "text",
&field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "San Francisco", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "California", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "94102", "text", &field);
form.fields.push_back(field);
// Address section 2.
test::CreateTestFormField("Name:", "name", "Barack Obama", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Address:", "address", "1600 Pennsylvania Avenue",
"text", &field);
form.fields.push_back(field);
test::CreateTestFormField("City:", "city", "Washington", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("State:", "state", "DC", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Zip:", "zip", "20500", "text", &field);
form.fields.push_back(field);
test::CreateTestFormField("Country:", "country", "USA", "text", &field);
form.fields.push_back(field);
// Credit card section.
AddFullCreditCardForm(&form, "Biggie Smalls", "4111-1111-1111-1111", "01",
"2999");
FormStructure form_structure(form);
form_structure.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card;
// Still returns true because the credit card import was successful.
EXPECT_TRUE(personal_data_->ImportFormData(form_structure, false,
&imported_credit_card));
ASSERT_TRUE(imported_credit_card);
personal_data_->SaveImportedCreditCard(*imported_credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Test that both addresses have been saved.
EXPECT_EQ(2U, personal_data_->GetProfiles().size());
// Test that the credit card has been saved.
CreditCard expected_card(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&expected_card, "Biggie Smalls", "4111111111111111",
"01", "2999");
const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, expected_card.Compare(*results[0]));
}
// Ensure that verified profiles can be saved via SaveImportedProfile,
// overwriting existing unverified profiles.
TEST_F(PersonalDataManagerTest, SaveImportedProfileWithVerifiedData) {
// Start with an unverified profile.
AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile,
"Marion", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
"91601", "US", "12345678910");
EXPECT_FALSE(profile.IsVerified());
// Add the profile to the database.
personal_data_->AddProfile(profile);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
AutofillProfile new_verified_profile = profile;
new_verified_profile.set_guid(base::GenerateGUID());
new_verified_profile.set_origin(kSettingsOrigin);
new_verified_profile.SetRawInfo(PHONE_HOME_WHOLE_NUMBER,
ASCIIToUTF16("1 234 567-8910"));
EXPECT_TRUE(new_verified_profile.IsVerified());
personal_data_->SaveImportedProfile(new_verified_profile);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// The new profile should be merged into the existing one.
const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
ASSERT_EQ(1U, results.size());
AutofillProfile expected(new_verified_profile);
expected.SetRawInfo(NAME_FULL, ASCIIToUTF16("Marion Mitchell Morrison"));
expected.SetRawInfo(PHONE_HOME_WHOLE_NUMBER, ASCIIToUTF16("+1 234-567-8910"));
EXPECT_EQ(0, expected.Compare(*results[0]))
<< "result = {" << *results[0] << "} | expected = {" << expected << "}";
}
// Ensure that verified credit cards can be saved via SaveImportedCreditCard.
TEST_F(PersonalDataManagerTest, SaveImportedCreditCardWithVerifiedData) {
// Start with a verified credit card.
CreditCard credit_card(base::GenerateGUID(), kSettingsOrigin);
test::SetCreditCardInfo(&credit_card,
"Biggie Smalls", "4111 1111 1111 1111" /* Visa */, "01", "2999");
EXPECT_TRUE(credit_card.IsVerified());
// Add the credit card to the database.
personal_data_->AddCreditCard(credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
CreditCard new_verified_card = credit_card;
new_verified_card.set_guid(base::GenerateGUID());
new_verified_card.SetRawInfo(CREDIT_CARD_NAME_FULL, ASCIIToUTF16("B. Small"));
EXPECT_TRUE(new_verified_card.IsVerified());
personal_data_->SaveImportedCreditCard(new_verified_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Expect that the saved credit card is updated.
const std::vector<CreditCard*>& results = personal_data_->GetCreditCards();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(ASCIIToUTF16("B. Small"),
results[0]->GetRawInfo(CREDIT_CARD_NAME_FULL));
}
TEST_F(PersonalDataManagerTest, GetNonEmptyTypes) {
// Check that there are no available types with no profiles stored.
ServerFieldTypeSet non_empty_types;
personal_data_->GetNonEmptyTypes(&non_empty_types);
EXPECT_EQ(0U, non_empty_types.size());
// Test with one profile stored.
AutofillProfile profile0(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile0,
"Marion", NULL, "Morrison",
"johnwayne@me.xyz", NULL, "123 Zoo St.", NULL, "Hollywood", "CA",
"91601", "US", "14155678910");
personal_data_->AddProfile(profile0);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
personal_data_->GetNonEmptyTypes(&non_empty_types);
EXPECT_EQ(15U, non_empty_types.size());
EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
EXPECT_TRUE(non_empty_types.count(NAME_LAST));
EXPECT_TRUE(non_empty_types.count(NAME_FULL));
EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STREET_ADDRESS));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
// Test with multiple profiles stored.
AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile1,
"Josephine", "Alicia", "Saenz",
"joewayne@me.xyz", "Fox", "903 Apple Ct.", NULL, "Orlando", "FL", "32801",
"US", "16502937549");
AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile2,
"Josephine", "Alicia", "Saenz",
"joewayne@me.xyz", "Fox", "1212 Center.", "Bld. 5", "Orlando", "FL",
"32801", "US", "16502937549");
personal_data_->AddProfile(profile1);
personal_data_->AddProfile(profile2);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
personal_data_->GetNonEmptyTypes(&non_empty_types);
EXPECT_EQ(19U, non_empty_types.size());
EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE));
EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE_INITIAL));
EXPECT_TRUE(non_empty_types.count(NAME_LAST));
EXPECT_TRUE(non_empty_types.count(NAME_FULL));
EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
EXPECT_TRUE(non_empty_types.count(COMPANY_NAME));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE2));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STREET_ADDRESS));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
// Test with credit card information also stored.
CreditCard credit_card(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&credit_card,
"John Dillinger", "423456789012" /* Visa */,
"01", "2999");
personal_data_->AddCreditCard(credit_card);
// Verify that the web database has been updated and the notification sent.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
personal_data_->GetNonEmptyTypes(&non_empty_types);
EXPECT_EQ(29U, non_empty_types.size());
EXPECT_TRUE(non_empty_types.count(NAME_FIRST));
EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE));
EXPECT_TRUE(non_empty_types.count(NAME_MIDDLE_INITIAL));
EXPECT_TRUE(non_empty_types.count(NAME_LAST));
EXPECT_TRUE(non_empty_types.count(NAME_FULL));
EXPECT_TRUE(non_empty_types.count(EMAIL_ADDRESS));
EXPECT_TRUE(non_empty_types.count(COMPANY_NAME));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE1));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_LINE2));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STREET_ADDRESS));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_CITY));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_STATE));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_ZIP));
EXPECT_TRUE(non_empty_types.count(ADDRESS_HOME_COUNTRY));
EXPECT_TRUE(non_empty_types.count(PHONE_HOME_NUMBER));
EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_CODE));
EXPECT_TRUE(non_empty_types.count(PHONE_HOME_COUNTRY_CODE));
EXPECT_TRUE(non_empty_types.count(PHONE_HOME_CITY_AND_NUMBER));
EXPECT_TRUE(non_empty_types.count(PHONE_HOME_WHOLE_NUMBER));
EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_NAME_FULL));
EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_NAME_FIRST));
EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_NAME_LAST));
EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_NUMBER));
EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_TYPE));
EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_MONTH));
EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_2_DIGIT_YEAR));
EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_4_DIGIT_YEAR));
EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_DATE_2_DIGIT_YEAR));
EXPECT_TRUE(non_empty_types.count(CREDIT_CARD_EXP_DATE_4_DIGIT_YEAR));
}
TEST_F(PersonalDataManagerTest, IncognitoReadOnly) {
ASSERT_TRUE(personal_data_->GetProfiles().empty());
ASSERT_TRUE(personal_data_->GetCreditCards().empty());
AutofillProfile steve_jobs(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&steve_jobs, "Steven", "Paul", "Jobs", "sjobs@apple.com",
"Apple Computer, Inc.", "1 Infinite Loop", "", "Cupertino", "CA", "95014",
"US", "(800) 275-2273");
personal_data_->AddProfile(steve_jobs);
CreditCard bill_gates(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(
&bill_gates, "William H. Gates", "5555555555554444", "1", "2020");
personal_data_->AddCreditCard(bill_gates);
// The personal data manager should be able to read existing profiles in an
// off-the-record context.
ResetPersonalDataManager(USER_MODE_INCOGNITO);
ASSERT_EQ(1U, personal_data_->GetProfiles().size());
ASSERT_EQ(1U, personal_data_->GetCreditCards().size());
// No adds, saves, or updates should take effect.
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()).Times(0);
// Add profiles or credit card shouldn't work.
personal_data_->AddProfile(test::GetFullProfile());
CreditCard larry_page(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(
&larry_page, "Lawrence Page", "4111111111111111", "10", "2025");
personal_data_->AddCreditCard(larry_page);
ResetPersonalDataManager(USER_MODE_INCOGNITO);
EXPECT_EQ(1U, personal_data_->GetProfiles().size());
EXPECT_EQ(1U, personal_data_->GetCreditCards().size());
// Saving or creating profiles from imported profiles shouldn't work.
steve_jobs.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Steve"));
personal_data_->SaveImportedProfile(steve_jobs);
bill_gates.SetRawInfo(CREDIT_CARD_NAME_FULL, ASCIIToUTF16("Bill Gates"));
personal_data_->SaveImportedCreditCard(bill_gates);
ResetPersonalDataManager(USER_MODE_INCOGNITO);
EXPECT_EQ(ASCIIToUTF16("Steven"),
personal_data_->GetProfiles()[0]->GetRawInfo(NAME_FIRST));
EXPECT_EQ(
ASCIIToUTF16("William H. Gates"),
personal_data_->GetCreditCards()[0]->GetRawInfo(CREDIT_CARD_NAME_FULL));
// Updating existing profiles shouldn't work.
steve_jobs.SetRawInfo(NAME_FIRST, ASCIIToUTF16("Steve"));
personal_data_->UpdateProfile(steve_jobs);
bill_gates.SetRawInfo(CREDIT_CARD_NAME_FULL, ASCIIToUTF16("Bill Gates"));
personal_data_->UpdateCreditCard(bill_gates);
ResetPersonalDataManager(USER_MODE_INCOGNITO);
EXPECT_EQ(ASCIIToUTF16("Steven"),
personal_data_->GetProfiles()[0]->GetRawInfo(NAME_FIRST));
EXPECT_EQ(
ASCIIToUTF16("William H. Gates"),
personal_data_->GetCreditCards()[0]->GetRawInfo(CREDIT_CARD_NAME_FULL));
// Removing shouldn't work.
personal_data_->RemoveByGUID(steve_jobs.guid());
personal_data_->RemoveByGUID(bill_gates.guid());
ResetPersonalDataManager(USER_MODE_INCOGNITO);
EXPECT_EQ(1U, personal_data_->GetProfiles().size());
EXPECT_EQ(1U, personal_data_->GetCreditCards().size());
}
TEST_F(PersonalDataManagerTest, DefaultCountryCodeIsCached) {
// The return value should always be some country code, no matter what.
std::string default_country =
personal_data_->GetDefaultCountryCodeForNewAddress();
EXPECT_EQ(2U, default_country.size());
AutofillProfile moose(base::GenerateGUID(), kSettingsOrigin);
test::SetProfileInfo(&moose, "Moose", "P", "McMahon", "mpm@example.com",
"", "1 Taiga TKTR", "", "Calgary", "AB", "T2B 2K2",
"CA", "(800) 555-9000");
personal_data_->AddProfile(moose);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// The value is cached and doesn't change even after adding an address.
EXPECT_EQ(default_country,
personal_data_->GetDefaultCountryCodeForNewAddress());
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged()).Times(2);
// Disabling Autofill blows away this cache and shouldn't account for Autofill
// profiles.
prefs_->SetBoolean(prefs::kAutofillEnabled, false);
EXPECT_EQ(default_country,
personal_data_->GetDefaultCountryCodeForNewAddress());
// Enabling Autofill blows away the cached value and should reflect the new
// value (accounting for profiles).
prefs_->SetBoolean(prefs::kAutofillEnabled, true);
EXPECT_EQ(base::UTF16ToUTF8(moose.GetRawInfo(ADDRESS_HOME_COUNTRY)),
personal_data_->GetDefaultCountryCodeForNewAddress());
}
TEST_F(PersonalDataManagerTest, DefaultCountryCodeComesFromProfiles) {
AutofillProfile moose(base::GenerateGUID(), kSettingsOrigin);
test::SetProfileInfo(&moose, "Moose", "P", "McMahon", "mpm@example.com",
"", "1 Taiga TKTR", "", "Calgary", "AB", "T2B 2K2",
"CA", "(800) 555-9000");
personal_data_->AddProfile(moose);
ResetPersonalDataManager(USER_MODE_NORMAL);
EXPECT_EQ("CA", personal_data_->GetDefaultCountryCodeForNewAddress());
// Multiple profiles cast votes.
AutofillProfile armadillo(base::GenerateGUID(), kSettingsOrigin);
test::SetProfileInfo(&armadillo, "Armin", "Dill", "Oh", "ado@example.com",
"", "1 Speed Bump", "", "Lubbock", "TX", "77500",
"MX", "(800) 555-9000");
AutofillProfile armadillo2(base::GenerateGUID(), kSettingsOrigin);
test::SetProfileInfo(&armadillo2, "Armin", "Dill", "Oh", "ado@example.com",
"", "2 Speed Bump", "", "Lubbock", "TX", "77500",
"MX", "(800) 555-9000");
personal_data_->AddProfile(armadillo);
personal_data_->AddProfile(armadillo2);
ResetPersonalDataManager(USER_MODE_NORMAL);
EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress());
personal_data_->RemoveByGUID(armadillo.guid());
personal_data_->RemoveByGUID(armadillo2.guid());
ResetPersonalDataManager(USER_MODE_NORMAL);
// Verified profiles count more.
armadillo.set_origin("http://randomwebsite.com");
armadillo2.set_origin("http://randomwebsite.com");
personal_data_->AddProfile(armadillo);
personal_data_->AddProfile(armadillo2);
ResetPersonalDataManager(USER_MODE_NORMAL);
EXPECT_EQ("CA", personal_data_->GetDefaultCountryCodeForNewAddress());
personal_data_->RemoveByGUID(armadillo.guid());
ResetPersonalDataManager(USER_MODE_NORMAL);
// But unverified profiles can be a tie breaker.
armadillo.set_origin(kSettingsOrigin);
personal_data_->AddProfile(armadillo);
ResetPersonalDataManager(USER_MODE_NORMAL);
EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress());
// Invalid country codes are ignored.
personal_data_->RemoveByGUID(armadillo.guid());
personal_data_->RemoveByGUID(moose.guid());
AutofillProfile space_invader(base::GenerateGUID(), kSettingsOrigin);
test::SetProfileInfo(&space_invader, "Marty", "", "Martian",
"mm@example.com", "", "1 Flying Object", "", "Valles Marineris", "",
"", "XX", "");
personal_data_->AddProfile(moose);
ResetPersonalDataManager(USER_MODE_NORMAL);
EXPECT_EQ("MX", personal_data_->GetDefaultCountryCodeForNewAddress());
}
TEST_F(PersonalDataManagerTest, UpdateLanguageCodeInProfile) {
AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile,
"Marion", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox", "123 Zoo St.", "unit 5", "Hollywood", "CA",
"91601", "US", "12345678910");
personal_data_->AddProfile(profile);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
profile.set_language_code("en");
personal_data_->UpdateProfile(profile);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
const std::vector<AutofillProfile*>& results = personal_data_->GetProfiles();
ASSERT_EQ(1U, results.size());
EXPECT_EQ(0, profile.Compare(*results[0]));
EXPECT_EQ("en", results[0]->language_code());
}
TEST_F(PersonalDataManagerTest, GetProfileSuggestions) {
AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile,
"Marion", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox",
"123 Zoo St.\nSecond Line\nThird line", "unit 5", "Hollywood", "CA",
"91601", "US", "12345678910");
personal_data_->AddProfile(profile);
ResetPersonalDataManager(USER_MODE_NORMAL);
std::vector<Suggestion> suggestions = personal_data_->GetProfileSuggestions(
AutofillType(ADDRESS_HOME_STREET_ADDRESS), base::ASCIIToUTF16("123"),
false, std::vector<ServerFieldType>());
ASSERT_FALSE(suggestions.empty());
EXPECT_EQ(base::ASCIIToUTF16("123 Zoo St., Second Line, Third line, unit 5"),
suggestions[0].value);
}
TEST_F(PersonalDataManagerTest, GetProfileSuggestions_PhoneSubstring) {
AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile, "Marion", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox",
"123 Zoo St.\nSecond Line\nThird line", "unit 5",
"Hollywood", "CA", "91601", "US", "12345678910");
personal_data_->AddProfile(profile);
ResetPersonalDataManager(USER_MODE_NORMAL);
std::vector<Suggestion> suggestions = personal_data_->GetProfileSuggestions(
AutofillType(PHONE_HOME_WHOLE_NUMBER), base::ASCIIToUTF16("234"), false,
std::vector<ServerFieldType>());
ASSERT_FALSE(suggestions.empty());
EXPECT_EQ(base::ASCIIToUTF16("12345678910"), suggestions[0].value);
}
TEST_F(PersonalDataManagerTest, GetProfileSuggestions_HideSubsets) {
AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile, "Marion", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox",
"123 Zoo St.\nSecond Line\nThird line", "unit 5",
"Hollywood", "CA", "91601", "US", "12345678910");
// Dupe profile, except different in email address (irrelevant for this form).
AutofillProfile profile1 = profile;
profile1.set_guid(base::GenerateGUID());
profile1.SetRawInfo(EMAIL_ADDRESS, base::ASCIIToUTF16("spam_me@example.com"));
// Dupe profile, except different in address state.
AutofillProfile profile2 = profile;
profile2.set_guid(base::GenerateGUID());
profile2.SetRawInfo(ADDRESS_HOME_STATE, base::ASCIIToUTF16("TX"));
// Subset profile.
AutofillProfile profile3 = profile;
profile3.set_guid(base::GenerateGUID());
profile3.SetRawInfo(ADDRESS_HOME_STATE, base::string16());
// For easier results verification, make sure |profile| is suggested first.
profile.set_use_count(5);
personal_data_->AddProfile(profile);
personal_data_->AddProfile(profile1);
personal_data_->AddProfile(profile2);
personal_data_->AddProfile(profile3);
ResetPersonalDataManager(USER_MODE_NORMAL);
// Simulate a form with street address, city and state.
std::vector<ServerFieldType> types;
types.push_back(ADDRESS_HOME_CITY);
types.push_back(ADDRESS_HOME_STATE);
std::vector<Suggestion> suggestions = personal_data_->GetProfileSuggestions(
AutofillType(ADDRESS_HOME_STREET_ADDRESS), base::ASCIIToUTF16("123"),
false, types);
ASSERT_EQ(2U, suggestions.size());
EXPECT_EQ(base::ASCIIToUTF16("Hollywood, CA"), suggestions[0].label);
EXPECT_EQ(base::ASCIIToUTF16("Hollywood, TX"), suggestions[1].label);
}
// Tests that GetProfileSuggestions orders its suggestions based on the frecency
// formula.
TEST_F(PersonalDataManagerTest, GetProfileSuggestions_Ranking) {
// Set up the profiles. They are named with number suffixes X so the X is the
// order in which they should be ordered by frecency.
AutofillProfile profile3(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile3, "Marion3", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox",
"123 Zoo St.\nSecond Line\nThird line", "unit 5",
"Hollywood", "CA", "91601", "US", "12345678910");
profile3.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(1));
profile3.set_use_count(5);
personal_data_->AddProfile(profile3);
AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile1, "Marion1", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox",
"123 Zoo St.\nSecond Line\nThird line", "unit 5",
"Hollywood", "CA", "91601", "US", "12345678910");
profile1.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(1));
profile1.set_use_count(10);
personal_data_->AddProfile(profile1);
AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile2, "Marion2", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox",
"123 Zoo St.\nSecond Line\nThird line", "unit 5",
"Hollywood", "CA", "91601", "US", "12345678910");
profile2.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(15));
profile2.set_use_count(300);
personal_data_->AddProfile(profile2);
ResetPersonalDataManager(USER_MODE_NORMAL);
std::vector<Suggestion> suggestions = personal_data_->GetProfileSuggestions(
AutofillType(NAME_FIRST), base::ASCIIToUTF16("Ma"), false,
std::vector<ServerFieldType>());
ASSERT_EQ(3U, suggestions.size());
EXPECT_EQ(suggestions[0].value, base::ASCIIToUTF16("Marion1"));
EXPECT_EQ(suggestions[1].value, base::ASCIIToUTF16("Marion2"));
EXPECT_EQ(suggestions[2].value, base::ASCIIToUTF16("Marion3"));
}
// Tests that GetProfileSuggestions returns all profiles suggestions by default
// and only two if the appropriate field trial is set.
TEST_F(PersonalDataManagerTest, GetProfileSuggestions_NumberOfSuggestions) {
// Set up 3 different profiles.
AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile1, "Marion1", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox",
"123 Zoo St.\nSecond Line\nThird line", "unit 5",
"Hollywood", "CA", "91601", "US", "12345678910");
personal_data_->AddProfile(profile1);
AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile2, "Marion2", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox",
"123 Zoo St.\nSecond Line\nThird line", "unit 5",
"Hollywood", "CA", "91601", "US", "12345678910");
personal_data_->AddProfile(profile2);
AutofillProfile profile3(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile3, "Marion3", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox",
"123 Zoo St.\nSecond Line\nThird line", "unit 5",
"Hollywood", "CA", "91601", "US", "12345678910");
personal_data_->AddProfile(profile3);
ResetPersonalDataManager(USER_MODE_NORMAL);
// Verify that all the profiles are suggested.
std::vector<Suggestion> suggestions = personal_data_->GetProfileSuggestions(
AutofillType(NAME_FIRST), base::string16(), false,
std::vector<ServerFieldType>());
EXPECT_EQ(3U, suggestions.size());
// Verify that only two profiles are suggested.
EnableAutofillProfileLimitFieldTrial("2");
suggestions = personal_data_->GetProfileSuggestions(
AutofillType(NAME_FIRST), base::string16(), false,
std::vector<ServerFieldType>());
EXPECT_EQ(2U, suggestions.size());
}
// Tests that GetProfileSuggestions returns the right number of profile
// suggestions when the limit to three field trial is set and there are less
// than three profiles.
TEST_F(PersonalDataManagerTest,
GetProfileSuggestions_LimitIsMoreThanProfileSuggestions) {
EnableAutofillProfileLimitFieldTrial("3");
// Set up 2 different profiles.
AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile1, "Marion1", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox",
"123 Zoo St.\nSecond Line\nThird line", "unit 5",
"Hollywood", "CA", "91601", "US", "12345678910");
personal_data_->AddProfile(profile1);
AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile2, "Marion2", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox",
"123 Zoo St.\nSecond Line\nThird line", "unit 5",
"Hollywood", "CA", "91601", "US", "12345678910");
personal_data_->AddProfile(profile2);
ResetPersonalDataManager(USER_MODE_NORMAL);
std::vector<Suggestion> suggestions = personal_data_->GetProfileSuggestions(
AutofillType(NAME_FIRST), base::string16(), false,
std::vector<ServerFieldType>());
EXPECT_EQ(2U, suggestions.size());
}
// Test that a masked server card is not suggested if more that six numbers have
// been typed in the field.
TEST_F(PersonalDataManagerTest,
GetCreditCardSuggestions_MaskedCardWithMoreThan6Numbers) {
EnableWalletCardImport();
// Add a masked server card.
std::vector<CreditCard> server_cards;
server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b459"));
test::SetCreditCardInfo(&server_cards.back(), "Emmet Dalton", "2110", "12",
"2999");
server_cards.back().SetTypeForMaskedCard(kVisaCard);
test::SetServerCreditCards(autofill_table_, server_cards);
personal_data_->Refresh();
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
std::vector<Suggestion> suggestions =
personal_data_->GetCreditCardSuggestions(AutofillType(CREDIT_CARD_NUMBER),
ASCIIToUTF16("12345678"));
// There should be no suggestions.
ASSERT_EQ(0U, suggestions.size());
}
// Test that local credit cards are ordered as expected.
TEST_F(PersonalDataManagerTest, GetCreditCardSuggestions_LocalCardsRanking) {
SetupReferenceLocalCreditCards();
// Sublabel is card number when filling name (exact format depends on
// the platform, but the last 4 digits should appear).
std::vector<Suggestion> suggestions =
personal_data_->GetCreditCardSuggestions(
AutofillType(CREDIT_CARD_NAME_FULL),
/* field_contents= */ base::string16());
ASSERT_EQ(3U, suggestions.size());
// Ordered as expected.
EXPECT_EQ(ASCIIToUTF16("John Dillinger"), suggestions[0].value);
EXPECT_TRUE(suggestions[0].label.find(ASCIIToUTF16("9012")) !=
base::string16::npos);
EXPECT_EQ(ASCIIToUTF16("Clyde Barrow"), suggestions[1].value);
EXPECT_TRUE(suggestions[1].label.find(ASCIIToUTF16("8555")) !=
base::string16::npos);
EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[2].value);
EXPECT_TRUE(suggestions[2].label.find(ASCIIToUTF16("2109")) !=
base::string16::npos);
}
// Test that local and server cards are ordered as expected.
TEST_F(PersonalDataManagerTest,
GetCreditCardSuggestions_LocalAndServerCardsRanking) {
EnableWalletCardImport();
SetupReferenceLocalCreditCards();
// Add some server cards.
std::vector<CreditCard> server_cards;
server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b459"));
test::SetCreditCardInfo(&server_cards.back(), "Emmet Dalton", "2110", "12",
"2999");
server_cards.back().set_use_count(2);
server_cards.back().set_use_date(base::Time::Now() -
base::TimeDelta::FromDays(1));
server_cards.back().SetTypeForMaskedCard(kVisaCard);
server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "b460"));
test::SetCreditCardInfo(&server_cards.back(), "Jesse James", "2109", "12",
"2999");
server_cards.back().set_use_count(6);
server_cards.back().set_use_date(base::Time::Now() -
base::TimeDelta::FromDays(1));
test::SetServerCreditCards(autofill_table_, server_cards);
personal_data_->Refresh();
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
std::vector<Suggestion> suggestions =
personal_data_->GetCreditCardSuggestions(
AutofillType(CREDIT_CARD_NAME_FULL),
/* field_contents= */ base::string16());
ASSERT_EQ(5U, suggestions.size());
// All cards should be ordered as expected.
EXPECT_EQ(ASCIIToUTF16("Jesse James"), suggestions[0].value);
EXPECT_EQ(ASCIIToUTF16("John Dillinger"), suggestions[1].value);
EXPECT_EQ(ASCIIToUTF16("Clyde Barrow"), suggestions[2].value);
EXPECT_EQ(ASCIIToUTF16("Emmet Dalton"), suggestions[3].value);
EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[4].value);
}
// Test that expired cards are ordered by frecency and are always suggested
// after non expired cards even if they have a higher frecency score.
TEST_F(PersonalDataManagerTest, GetCreditCardSuggestions_ExpiredCards) {
ASSERT_EQ(0U, personal_data_->GetCreditCards().size());
// Add a never used non expired credit card.
CreditCard credit_card0("002149C1-EE28-4213-A3B9-DA243FFF021B",
"https://www.example.com");
test::SetCreditCardInfo(&credit_card0, "Bonnie Parker",
"518765432109" /* Mastercard */, "04", "2999");
personal_data_->AddCreditCard(credit_card0);
// Add an expired card with a higher frecency score.
CreditCard credit_card1("287151C8-6AB1-487C-9095-28E80BE5DA15",
"https://www.example.com");
test::SetCreditCardInfo(&credit_card1, "Clyde Barrow",
"347666888555" /* American Express */, "04", "1999");
credit_card1.set_use_count(300);
credit_card1.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(10));
personal_data_->AddCreditCard(credit_card1);
// Add an expired card with a lower frecency score.
CreditCard credit_card2("1141084B-72D7-4B73-90CF-3D6AC154673B",
"https://www.example.com");
credit_card2.set_use_count(3);
credit_card2.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(1));
test::SetCreditCardInfo(&credit_card2, "John Dillinger",
"423456789012" /* Visa */, "01", "1998");
personal_data_->AddCreditCard(credit_card2);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
std::vector<Suggestion> suggestions =
personal_data_->GetCreditCardSuggestions(
AutofillType(CREDIT_CARD_NAME_FULL),
/* field_contents= */ base::string16());
ASSERT_EQ(3U, suggestions.size());
// The never used non expired card should be suggested first.
EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[0].value);
// The expired cards should be sorted by frecency
EXPECT_EQ(ASCIIToUTF16("Clyde Barrow"), suggestions[1].value);
EXPECT_EQ(ASCIIToUTF16("John Dillinger"), suggestions[2].value);
}
// Test that a card that doesn't have a number is not shown in the suggestions
// when querying credit cards by their number.
TEST_F(PersonalDataManagerTest, GetCreditCardSuggestions_NumberMissing) {
// Create one normal credit card and one credit card with the number missing.
ASSERT_EQ(0U, personal_data_->GetCreditCards().size());
CreditCard credit_card0("287151C8-6AB1-487C-9095-28E80BE5DA15",
"https://www.example.com");
test::SetCreditCardInfo(&credit_card0, "Clyde Barrow",
"347666888555" /* American Express */, "04", "2999");
credit_card0.set_use_count(3);
credit_card0.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(1));
personal_data_->AddCreditCard(credit_card0);
CreditCard credit_card1("1141084B-72D7-4B73-90CF-3D6AC154673B",
"https://www.example.com");
credit_card1.set_use_count(300);
credit_card1.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(10));
test::SetCreditCardInfo(&credit_card1, "John Dillinger", "", "01", "2999");
personal_data_->AddCreditCard(credit_card1);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
ASSERT_EQ(2U, personal_data_->GetCreditCards().size());
// Sublabel is expiration date when filling card number. The second card
// doesn't have a number so it should not be included in the suggestions.
std::vector<Suggestion> suggestions =
personal_data_->GetCreditCardSuggestions(
AutofillType(CREDIT_CARD_NUMBER),
/* field_contents= */ base::string16());
ASSERT_EQ(1U, suggestions.size());
EXPECT_EQ(UTF8ToUTF16("Amex" + kUTF8MidlineEllipsis + "8555"),
suggestions[0].value);
EXPECT_EQ(ASCIIToUTF16("04/99"), suggestions[0].label);
}
// Tests the suggestions of duplicate local and server credit cards.
TEST_F(PersonalDataManagerTest, GetCreditCardSuggestions_ServerDuplicates) {
EnableWalletCardImport();
SetupReferenceLocalCreditCards();
// Add some server cards. If there are local dupes, the locals should be
// hidden.
std::vector<CreditCard> server_cards;
// This server card matches a local card, except the local card is missing the
// number. This should count as a dupe and thus not be shown in the
// suggestions since the locally saved card takes precedence.
server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
"9012" /* Visa */, "01", "2999");
server_cards.back().set_use_count(2);
server_cards.back().set_use_date(base::Time::Now() -
base::TimeDelta::FromDays(15));
server_cards.back().SetTypeForMaskedCard(kVisaCard);
// This server card is identical to a local card, but has a different
// card type. Not a dupe and therefore both should appear in the suggestions.
server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456"));
test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker", "2109", "12",
"2999");
server_cards.back().set_use_count(3);
server_cards.back().set_use_date(base::Time::Now() -
base::TimeDelta::FromDays(15));
server_cards.back().SetTypeForMaskedCard(kVisaCard);
// This unmasked server card is an exact dupe of a local card. Therefore only
// this card should appear in the suggestions as full server cards have
// precedence over local cards.
server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
"347666888555" /* American Express */, "04", "2999");
server_cards.back().set_use_count(1);
server_cards.back().set_use_date(base::Time::Now() -
base::TimeDelta::FromDays(15));
test::SetServerCreditCards(autofill_table_, server_cards);
personal_data_->Refresh();
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
std::vector<Suggestion> suggestions =
personal_data_->GetCreditCardSuggestions(
AutofillType(CREDIT_CARD_NAME_FULL),
/* field_contents= */ base::string16());
ASSERT_EQ(4U, suggestions.size());
EXPECT_EQ(ASCIIToUTF16("John Dillinger"), suggestions[0].value);
EXPECT_EQ(ASCIIToUTF16("Clyde Barrow"), suggestions[1].value);
EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[2].value);
EXPECT_EQ(ASCIIToUTF16("Bonnie Parker"), suggestions[3].value);
suggestions = personal_data_->GetCreditCardSuggestions(
AutofillType(CREDIT_CARD_NUMBER), /* field_contents= */ base::string16());
ASSERT_EQ(4U, suggestions.size());
EXPECT_EQ(UTF8ToUTF16("Visa" + kUTF8MidlineEllipsis + "9012"),
suggestions[0].value);
EXPECT_EQ(UTF8ToUTF16("Amex" + kUTF8MidlineEllipsis + "8555"),
suggestions[1].value);
EXPECT_EQ(UTF8ToUTF16("MasterCard" + kUTF8MidlineEllipsis + "2109"),
suggestions[2].value);
EXPECT_EQ(UTF8ToUTF16("Visa" + kUTF8MidlineEllipsis + "2109"),
suggestions[3].value);
}
// Tests that a full server card can be a dupe of more than one local card.
TEST_F(PersonalDataManagerTest,
GetCreditCardSuggestions_ServerCardDuplicateOfMultipleLocalCards) {
EnableWalletCardImport();
SetupReferenceLocalCreditCards();
// Add a duplicate server card.
std::vector<CreditCard> server_cards;
// This unmasked server card is an exact dupe of a local card. Therefore only
// the local card should appear in the suggestions.
server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
"347666888555" /* American Express */, "04", "2999");
test::SetServerCreditCards(autofill_table_, server_cards);
personal_data_->Refresh();
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
std::vector<Suggestion> suggestions =
personal_data_->GetCreditCardSuggestions(
AutofillType(CREDIT_CARD_NAME_FULL),
/* field_contents= */ base::string16());
ASSERT_EQ(3U, suggestions.size());
// Add a second dupe local card to make sure a full server card can be a dupe
// of more than one local card.
CreditCard credit_card3("4141084B-72D7-4B73-90CF-3D6AC154673B",
"https://www.example.com");
test::SetCreditCardInfo(&credit_card3, "Clyde Barrow", "", "04", "");
personal_data_->AddCreditCard(credit_card3);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
suggestions = personal_data_->GetCreditCardSuggestions(
AutofillType(CREDIT_CARD_NAME_FULL),
/* field_contents= */ base::string16());
ASSERT_EQ(3U, suggestions.size());
}
// Tests that only the full server card is kept when deduping with a local
// duplicate of it.
TEST_F(PersonalDataManagerTest,
DedupeCreditCardToSuggest_FullServerShadowsLocal) {
std::list<CreditCard*> credit_cards;
// Create 3 different local credit cards.
CreditCard local_card("287151C8-6AB1-487C-9095-28E80BE5DA15",
"https://www.example.com");
test::SetCreditCardInfo(&local_card, "Homer Simpson",
"423456789012" /* Visa */, "01", "2999");
local_card.set_use_count(3);
local_card.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(1));
credit_cards.push_back(&local_card);
// Create a full server card that is a duplicate of one of the local cards.
CreditCard full_server_card(CreditCard::FULL_SERVER_CARD, "c789");
test::SetCreditCardInfo(&full_server_card, "Homer Simpson",
"423456789012" /* Visa */, "01", "2999");
full_server_card.set_use_count(1);
full_server_card.set_use_date(base::Time::Now() -
base::TimeDelta::FromDays(15));
credit_cards.push_back(&full_server_card);
PersonalDataManager::DedupeCreditCardToSuggest(&credit_cards);
ASSERT_EQ(1U, credit_cards.size());
const CreditCard* deduped_card(credit_cards.front());
EXPECT_TRUE(*deduped_card == full_server_card);
}
// Tests that only the local card is kept when deduping with a masked server
// duplicate of it.
TEST_F(PersonalDataManagerTest, DedupeCreditCardToSuggest_LocalShadowsMasked) {
std::list<CreditCard*> credit_cards;
CreditCard local_card("1141084B-72D7-4B73-90CF-3D6AC154673B",
"https://www.example.com");
local_card.set_use_count(300);
local_card.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(10));
test::SetCreditCardInfo(&local_card, "Homer Simpson",
"423456789012" /* Visa */, "01", "2999");
credit_cards.push_back(&local_card);
// Create a masked server card that is a duplicate of a local card.
CreditCard masked_card(CreditCard::MASKED_SERVER_CARD, "a123");
test::SetCreditCardInfo(&masked_card, "Homer Simpson", "9012" /* Visa */,
"01", "2999");
masked_card.set_use_count(2);
masked_card.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(15));
masked_card.SetTypeForMaskedCard(kVisaCard);
credit_cards.push_back(&masked_card);
PersonalDataManager::DedupeCreditCardToSuggest(&credit_cards);
ASSERT_EQ(1U, credit_cards.size());
const CreditCard* deduped_card(credit_cards.front());
EXPECT_TRUE(*deduped_card == local_card);
}
// Tests that identical full server and masked credit cards are not deduped.
TEST_F(PersonalDataManagerTest, DedupeCreditCardToSuggest_FullServerAndMasked) {
std::list<CreditCard*> credit_cards;
// Create a full server card that is a duplicate of one of the local cards.
CreditCard full_server_card(CreditCard::FULL_SERVER_CARD, "c789");
test::SetCreditCardInfo(&full_server_card, "Homer Simpson",
"423456789012" /* Visa */, "01", "2999");
full_server_card.set_use_count(1);
full_server_card.set_use_date(base::Time::Now() -
base::TimeDelta::FromDays(15));
credit_cards.push_back(&full_server_card);
// Create a masked server card that is a duplicate of a local card.
CreditCard masked_card(CreditCard::MASKED_SERVER_CARD, "a123");
test::SetCreditCardInfo(&masked_card, "Homer Simpson", "9012" /* Visa */,
"01", "2999");
masked_card.set_use_count(2);
masked_card.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(15));
masked_card.SetTypeForMaskedCard(kVisaCard);
credit_cards.push_back(&masked_card);
PersonalDataManager::DedupeCreditCardToSuggest(&credit_cards);
EXPECT_EQ(2U, credit_cards.size());
}
// Tests that slightly different local, full server, and masked credit cards are
// not deduped.
TEST_F(PersonalDataManagerTest, DedupeCreditCardToSuggest_DifferentCards) {
std::list<CreditCard*> credit_cards;
CreditCard credit_card2("002149C1-EE28-4213-A3B9-DA243FFF021B",
"https://www.example.com");
credit_card2.set_use_count(1);
credit_card2.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(1));
test::SetCreditCardInfo(&credit_card2, "Homer Simpson",
"518765432109" /* Mastercard */, "", "");
credit_cards.push_back(&credit_card2);
// Create a masked server card that is slightly different of the local card.
CreditCard credit_card4(CreditCard::MASKED_SERVER_CARD, "b456");
test::SetCreditCardInfo(&credit_card4, "Homer Simpson", "2109", "12", "2999");
credit_card4.set_use_count(3);
credit_card4.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(15));
credit_card4.SetTypeForMaskedCard(kVisaCard);
credit_cards.push_back(&credit_card4);
// Create a full server card that is slightly different of the two other
// cards.
CreditCard credit_card5(CreditCard::FULL_SERVER_CARD, "c789");
test::SetCreditCardInfo(&credit_card5, "Homer Simpson",
"347666888555" /* American Express */, "04", "2999");
credit_card5.set_use_count(1);
credit_card5.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(15));
credit_cards.push_back(&credit_card5);
PersonalDataManager::DedupeCreditCardToSuggest(&credit_cards);
EXPECT_EQ(3U, credit_cards.size());
}
TEST_F(PersonalDataManagerTest, RecordUseOf) {
base::Time creation_time = base::Time::FromTimeT(12345);
AutofillProfile profile(test::GetFullProfile());
profile.set_use_date(creation_time);
profile.set_modification_date(creation_time);
EXPECT_EQ(1U, profile.use_count());
EXPECT_EQ(creation_time, profile.use_date());
EXPECT_EQ(creation_time, profile.modification_date());
personal_data_->AddProfile(profile);
CreditCard credit_card(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&credit_card, "John Dillinger",
"423456789012" /* Visa */, "01", "2999");
credit_card.set_use_date(creation_time);
credit_card.set_modification_date(creation_time);
EXPECT_EQ(1U, credit_card.use_count());
EXPECT_EQ(creation_time, credit_card.use_date());
EXPECT_EQ(creation_time, credit_card.modification_date());
personal_data_->AddCreditCard(credit_card);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Notify the PDM that the profile and credit card were used.
AutofillProfile* added_profile =
personal_data_->GetProfileByGUID(profile.guid());
ASSERT_TRUE(added_profile);
EXPECT_EQ(*added_profile, profile);
EXPECT_EQ(1U, added_profile->use_count());
EXPECT_EQ(creation_time, added_profile->use_date());
EXPECT_NE(creation_time, added_profile->modification_date());
personal_data_->RecordUseOf(profile);
CreditCard* added_card =
personal_data_->GetCreditCardByGUID(credit_card.guid());
ASSERT_TRUE(added_card);
EXPECT_EQ(*added_card, credit_card);
EXPECT_EQ(1U, added_card->use_count());
EXPECT_EQ(creation_time, added_card->use_date());
EXPECT_NE(creation_time, added_card->modification_date());
personal_data_->RecordUseOf(credit_card);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Verify usage stats are updated.
added_profile = personal_data_->GetProfileByGUID(profile.guid());
ASSERT_TRUE(added_profile);
EXPECT_EQ(2U, added_profile->use_count());
EXPECT_NE(creation_time, added_profile->use_date());
EXPECT_NE(creation_time, added_profile->modification_date());
added_card = personal_data_->GetCreditCardByGUID(credit_card.guid());
ASSERT_TRUE(added_card);
EXPECT_EQ(2U, added_card->use_count());
EXPECT_NE(creation_time, added_card->use_date());
EXPECT_NE(creation_time, added_card->modification_date());
}
TEST_F(PersonalDataManagerTest, UpdateServerCreditCardUsageStats) {
EnableWalletCardImport();
std::vector<CreditCard> server_cards;
server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
"9012" /* Visa */, "01", "2999");
server_cards.back().SetTypeForMaskedCard(kVisaCard);
server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "b456"));
test::SetCreditCardInfo(&server_cards.back(), "Bonnie Parker",
"4444" /* Mastercard */, "12", "2999");
server_cards.back().SetTypeForMaskedCard(kMasterCard);
server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
"347666888555" /* American Express */, "04", "2999");
test::SetServerCreditCards(autofill_table_, server_cards);
personal_data_->Refresh();
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
if (!OfferStoreUnmaskedCards()) {
for (CreditCard* card : personal_data_->GetCreditCards()) {
EXPECT_EQ(CreditCard::MASKED_SERVER_CARD, card->record_type());
}
// The rest of this test doesn't work if we're force-masking all unmasked
// cards.
return;
}
// The GUIDs will be different, so just compare the data.
for (size_t i = 0; i < 3; ++i)
EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
CreditCard* unmasked_card = &server_cards.front();
unmasked_card->set_record_type(CreditCard::FULL_SERVER_CARD);
unmasked_card->SetNumber(ASCIIToUTF16("423456789012"));
EXPECT_NE(0, unmasked_card->Compare(
*personal_data_->GetCreditCards().front()));
personal_data_->UpdateServerCreditCard(*unmasked_card);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
for (size_t i = 0; i < 3; ++i)
EXPECT_EQ(0, server_cards[i].Compare(*personal_data_->GetCreditCards()[i]));
// For an unmasked card, usage data starts out as 2 because of the unmasking
// which is considered a use.
EXPECT_EQ(2U, personal_data_->GetCreditCards()[0]->use_count());
EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[0]->use_date());
EXPECT_EQ(1U, personal_data_->GetCreditCards()[1]->use_count());
EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[1]->use_date());
// Having unmasked this card, usage stats should be 2 and Now().
EXPECT_EQ(2U, personal_data_->GetCreditCards()[2]->use_count());
EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[2]->use_date());
base::Time initial_use_date = personal_data_->GetCreditCards()[2]->use_date();
server_cards.back().set_guid(personal_data_->GetCreditCards()[2]->guid());
personal_data_->RecordUseOf(server_cards.back());
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
EXPECT_EQ(2U, personal_data_->GetCreditCards()[0]->use_count());
EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[0]->use_date());
EXPECT_EQ(1U, personal_data_->GetCreditCards()[1]->use_count());
EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[1]->use_date());
EXPECT_EQ(3U, personal_data_->GetCreditCards()[2]->use_count());
EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[2]->use_date());
// Time may or may not have elapsed between unmasking and RecordUseOf.
EXPECT_LE(initial_use_date, personal_data_->GetCreditCards()[2]->use_date());
// Can record usage stats on masked cards.
server_cards[1].set_guid(personal_data_->GetCreditCards()[1]->guid());
personal_data_->RecordUseOf(server_cards[1]);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
EXPECT_EQ(2U, personal_data_->GetCreditCards()[1]->use_count());
EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[1]->use_date());
// Upgrading to unmasked retains the usage stats (and increments them).
CreditCard* unmasked_card2 = &server_cards[1];
unmasked_card2->set_record_type(CreditCard::FULL_SERVER_CARD);
unmasked_card2->SetNumber(ASCIIToUTF16("5555555555554444"));
personal_data_->UpdateServerCreditCard(*unmasked_card2);
server_cards[1].set_guid(personal_data_->GetCreditCards()[1]->guid());
personal_data_->RecordUseOf(server_cards[1]);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
ASSERT_EQ(3U, personal_data_->GetCreditCards().size());
EXPECT_EQ(3U, personal_data_->GetCreditCards()[1]->use_count());
EXPECT_NE(base::Time(), personal_data_->GetCreditCards()[1]->use_date());
}
TEST_F(PersonalDataManagerTest, ClearAllServerData) {
// Add a server card.
std::vector<CreditCard> server_cards;
server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
"9012" /* Visa */, "01", "2999");
server_cards.back().SetTypeForMaskedCard(kVisaCard);
test::SetServerCreditCards(autofill_table_, server_cards);
personal_data_->Refresh();
// Need to set the google services username
EnableWalletCardImport();
// Add a server profile.
std::vector<AutofillProfile> server_profiles;
server_profiles.push_back(
AutofillProfile(AutofillProfile::SERVER_PROFILE, "a123"));
test::SetProfileInfo(&server_profiles.back(), "John", "", "Doe", "",
"ACME Corp", "500 Oak View", "Apt 8", "Houston", "TX",
"77401", "US", "");
autofill_table_->SetServerProfiles(server_profiles);
// The card and profile should be there.
ResetPersonalDataManager(USER_MODE_NORMAL);
EXPECT_FALSE(personal_data_->GetCreditCards().empty());
EXPECT_FALSE(personal_data_->GetProfiles().empty());
personal_data_->ClearAllServerData();
// Reload the database, everything should be gone.
ResetPersonalDataManager(USER_MODE_NORMAL);
EXPECT_TRUE(personal_data_->GetCreditCards().empty());
EXPECT_TRUE(personal_data_->GetProfiles().empty());
}
TEST_F(PersonalDataManagerTest, DontDuplicateServerCard) {
EnableWalletCardImport();
std::vector<CreditCard> server_cards;
server_cards.push_back(CreditCard(CreditCard::MASKED_SERVER_CARD, "a123"));
test::SetCreditCardInfo(&server_cards.back(), "John Dillinger",
"1881" /* Visa */, "01", "2999");
server_cards.back().SetTypeForMaskedCard(kVisaCard);
server_cards.push_back(CreditCard(CreditCard::FULL_SERVER_CARD, "c789"));
test::SetCreditCardInfo(&server_cards.back(), "Clyde Barrow",
"347666888555" /* American Express */, "04", "2999");
test::SetServerCreditCards(autofill_table_, server_cards);
personal_data_->Refresh();
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// A valid credit card form. A user re-enters one of their masked cards.
// We shouldn't offer to save. It's possible this is actually a different card
// but it's very unlikely. And these circumstances will also arise if the user
// has the same card available locally and synced from payments.
FormData form1;
FormFieldData field;
test::CreateTestFormField("Name on card:", "name_on_card", "John Dillinger",
"text", &field);
form1.fields.push_back(field);
test::CreateTestFormField("Card Number:", "card_number", "4012888888881881",
"text", &field);
form1.fields.push_back(field);
test::CreateTestFormField("Exp Month:", "exp_month", "01", "text", &field);
form1.fields.push_back(field);
test::CreateTestFormField("Exp Year:", "exp_year", "2999", "text", &field);
form1.fields.push_back(field);
FormStructure form_structure1(form1);
form_structure1.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card;
EXPECT_FALSE(personal_data_->ImportFormData(form_structure1, false,
&imported_credit_card));
EXPECT_FALSE(imported_credit_card);
// A user re-types (or fills with) an unmasked card. Don't offer to save
// here, either. Since it's unmasked, we know for certain that it's the same
// card.
FormData form2;
test::CreateTestFormField("Name on card:", "name_on_card", "Clyde Barrow",
"text", &field);
form2.fields.push_back(field);
test::CreateTestFormField("Card Number:", "card_number", "347666888555",
"text", &field);
form2.fields.push_back(field);
test::CreateTestFormField("Exp Month:", "exp_month", "04", "text", &field);
form2.fields.push_back(field);
test::CreateTestFormField("Exp Year:", "exp_year", "2999", "text", &field);
form2.fields.push_back(field);
FormStructure form_structure2(form2);
form_structure2.DetermineHeuristicTypes();
std::unique_ptr<CreditCard> imported_credit_card2;
EXPECT_FALSE(personal_data_->ImportFormData(form_structure2, false,
&imported_credit_card2));
EXPECT_FALSE(imported_credit_card2);
}
// Tests the SaveImportedProfile method with different profiles to make sure the
// merge logic works correctly.
TEST_F(PersonalDataManagerTest, SaveImportedProfile) {
typedef struct {
autofill::ServerFieldType field_type;
std::string field_value;
} ProfileField;
typedef std::vector<ProfileField> ProfileFields;
typedef struct {
// Each test starts with a default pre-existing profile and applies these
// changes to it.
ProfileFields changes_to_original;
// Each test saves a second profile. Applies these changes to the default
// values before saving.
ProfileFields changes_to_new;
// For tests with profile merging, makes sure that these fields' values are
// the ones we expect (depending on the test).
ProfileFields changed_field_values;
} TestCase;
TestCase test_cases[] = {
// Test that saving an identical profile except for the name results in
// two profiles being saved.
{ProfileFields(), {{NAME_FIRST, "Marionette"}}},
// Test that saving an identical profile except with the middle name
// initial instead of the full middle name results in the profiles
// getting merged and the full middle name being kept.
{ProfileFields(),
{{NAME_MIDDLE, "M"}},
{{NAME_MIDDLE, "Mitchell"}, {NAME_FULL, "Marion Mitchell Morrison"}}},
// Test that saving an identical profile except with the full middle name
// instead of the middle name initial results in the profiles getting
// merged and the full middle name replacing the initial.
{{{NAME_MIDDLE, "M"}},
{{NAME_MIDDLE, "Mitchell"}},
{{NAME_MIDDLE, "Mitchell"}}},
// Test that saving an identical profile except with no middle name
// results in the profiles getting merged and the full middle name being
// kept.
{ProfileFields(), {{NAME_MIDDLE, ""}}, {{NAME_MIDDLE, "Mitchell"}}},
// Test that saving an identical profile except with a middle name initial
// results in the profiles getting merged and the middle name initial
// being saved.
{{{NAME_MIDDLE, ""}}, {{NAME_MIDDLE, "M"}}, {{NAME_MIDDLE, "M"}}},
// Test that saving an identical profile except with a middle name
// results in the profiles getting merged and the full middle name being
// saved.
{{{NAME_MIDDLE, ""}},
{{NAME_MIDDLE, "Mitchell"}},
{{NAME_MIDDLE, "Mitchell"}}},
// Test that saving a identical profile except with the full name set
// instead of the name parts results in the two profiles being merged and
// all the name parts kept and the full name being added.
{
{
{NAME_FIRST, "Marion"},
{NAME_MIDDLE, "Mitchell"},
{NAME_LAST, "Morrison"},
{NAME_FULL, ""},
},
{
{NAME_FIRST, ""},
{NAME_MIDDLE, ""},
{NAME_LAST, ""},
{NAME_FULL, "Marion Mitchell Morrison"},
},
{
{NAME_FIRST, "Marion"},
{NAME_MIDDLE, "Mitchell"},
{NAME_LAST, "Morrison"},
{NAME_FULL, "Marion Mitchell Morrison"},
},
},
// Test that saving a identical profile except with the name parts set
// instead of the full name results in the two profiles being merged and
// the full name being kept and all the name parts being added.
{
{
{NAME_FIRST, ""},
{NAME_MIDDLE, ""},
{NAME_LAST, ""},
{NAME_FULL, "Marion Mitchell Morrison"},
},
{
{NAME_FIRST, "Marion"},
{NAME_MIDDLE, "Mitchell"},
{NAME_LAST, "Morrison"},
{NAME_FULL, ""},
},
{
{NAME_FIRST, "Marion"},
{NAME_MIDDLE, "Mitchell"},
{NAME_LAST, "Morrison"},
{NAME_FULL, "Marion Mitchell Morrison"},
},
},
// Test that saving a profile that has only a full name set does not get
// merged with a profile with only the name parts set if the names are
// different.
{
{
{NAME_FIRST, "Marion"},
{NAME_MIDDLE, "Mitchell"},
{NAME_LAST, "Morrison"},
{NAME_FULL, ""},
},
{
{NAME_FIRST, ""},
{NAME_MIDDLE, ""},
{NAME_LAST, ""},
{NAME_FULL, "John Thompson Smith"},
},
},
// Test that saving a profile that has only the name parts set does not
// get merged with a profile with only the full name set if the names are
// different.
{
{
{NAME_FIRST, ""},
{NAME_MIDDLE, ""},
{NAME_LAST, ""},
{NAME_FULL, "John Thompson Smith"},
},
{
{NAME_FIRST, "Marion"},
{NAME_MIDDLE, "Mitchell"},
{NAME_LAST, "Morrison"},
{NAME_FULL, ""},
},
},
// Test that saving an identical profile except for the first address line
// results in two profiles being saved.
{ProfileFields(), {{ADDRESS_HOME_LINE1, "123 Aquarium St."}}},
// Test that saving an identical profile except for the second address
// line results in two profiles being saved.
{ProfileFields(), {{ADDRESS_HOME_LINE2, "unit 7"}}},
// Tests that saving an identical profile that has a new piece of
// information (company name) results in a merge and that the original
// empty value gets overwritten by the new information.
{{{COMPANY_NAME, ""}}, ProfileFields(), {{COMPANY_NAME, "Fox"}}},
// Tests that saving an identical profile except a loss of information
// results in a merge but the original value is not overwritten (no
// information loss).
{ProfileFields(), {{COMPANY_NAME, ""}}, {{COMPANY_NAME, "Fox"}}},
// Tests that saving an identical profile except a slightly different
// postal code results in a merge with the new value kept.
{{{ADDRESS_HOME_ZIP, "R2C 0A1"}},
{{ADDRESS_HOME_ZIP, "R2C0A1"}},
{{ADDRESS_HOME_ZIP, "R2C0A1"}}},
{{{ADDRESS_HOME_ZIP, "R2C0A1"}},
{{ADDRESS_HOME_ZIP, "R2C 0A1"}},
{{ADDRESS_HOME_ZIP, "R2C 0A1"}}},
{{{ADDRESS_HOME_ZIP, "r2c 0a1"}},
{{ADDRESS_HOME_ZIP, "R2C0A1"}},
{{ADDRESS_HOME_ZIP, "R2C0A1"}}},
// Tests that saving an identical profile plus a new piece of information
// on the address line 2 results in a merge and that the original empty
// value gets overwritten by the new information.
{{{ADDRESS_HOME_LINE2, ""}},
ProfileFields(),
{{ADDRESS_HOME_LINE2, "unit 5"}}},
// Tests that saving an identical profile except a loss of information on
// the address line 2 results in a merge but that the original value gets
// not overwritten (no information loss).
{ProfileFields(),
{{ADDRESS_HOME_LINE2, ""}},
{{ADDRESS_HOME_LINE2, "unit 5"}}},
// Tests that saving an identical except with more punctuation in the fist
// address line, while the second is empty, results in a merge and that
// the original address gets overwritten.
{{{ADDRESS_HOME_LINE2, ""}},
{{ADDRESS_HOME_LINE2, ""}, {ADDRESS_HOME_LINE1, "123, Zoo St."}},
{{ADDRESS_HOME_LINE1, "123, Zoo St."}}},
// Tests that saving an identical profile except with less punctuation in
// the fist address line, while the second is empty, results in a merge
// and that the longer address is retained.
{{{ADDRESS_HOME_LINE2, ""}, {ADDRESS_HOME_LINE1, "123, Zoo St."}},
{{ADDRESS_HOME_LINE2, ""}},
{{ADDRESS_HOME_LINE1, "123 Zoo St"}}},
// Tests that saving an identical profile except additional punctuation in
// the two address lines results in a merge and that the newer address
// is retained.
{ProfileFields(),
{{ADDRESS_HOME_LINE1, "123, Zoo St."}, {ADDRESS_HOME_LINE2, "unit. 5"}},
{{ADDRESS_HOME_LINE1, "123, Zoo St."}, {ADDRESS_HOME_LINE2, "unit. 5"}}},
// Tests that saving an identical profile except less punctuation in the
// two address lines results in a merge and that the newer address is
// retained.
{{{ADDRESS_HOME_LINE1, "123, Zoo St."}, {ADDRESS_HOME_LINE2, "unit. 5"}},
ProfileFields(),
{{ADDRESS_HOME_LINE1, "123 Zoo St"}, {ADDRESS_HOME_LINE2, "unit 5"}}},
// Tests that saving an identical profile with accented characters in
// the two address lines results in a merge and that the newer address
// is retained.
{ProfileFields(),
{{ADDRESS_HOME_LINE1, "123 Zôö St"}, {ADDRESS_HOME_LINE2, "üñìt 5"}},
{{ADDRESS_HOME_LINE1, "123 Zôö St"}, {ADDRESS_HOME_LINE2, "üñìt 5"}}},
// Tests that saving an identical profile without accented characters in
// the two address lines results in a merge and that the newer address
// is retained.
{{{ADDRESS_HOME_LINE1, "123 Zôö St"}, {ADDRESS_HOME_LINE2, "üñìt 5"}},
ProfileFields(),
{{ADDRESS_HOME_LINE1, "123 Zoo St"}, {ADDRESS_HOME_LINE2, "unit 5"}}},
// Tests that saving an identical profile except that the address line 1
// is in the address line 2 results in a merge and that the multi-lne
// address is retained.
{ProfileFields(),
{{ADDRESS_HOME_LINE1, "123 Zoo St, unit 5"}, {ADDRESS_HOME_LINE2, ""}},
{{ADDRESS_HOME_LINE1, "123 Zoo St"}, {ADDRESS_HOME_LINE2, "unit 5"}}},
// Tests that saving an identical profile except that the address line 2
// contains part of the old address line 1 results in a merge and that the
// original address lines of the reference profile get overwritten.
{{{ADDRESS_HOME_LINE1, "123 Zoo St, unit 5"}, {ADDRESS_HOME_LINE2, ""}},
ProfileFields(),
{{ADDRESS_HOME_LINE1, "123 Zoo St"}, {ADDRESS_HOME_LINE2, "unit 5"}}},
// Tests that saving an identical profile except that the state is the
// abbreviation instead of the full form results in a merge and that the
// original state gets overwritten.
{{{ADDRESS_HOME_STATE, "California"}},
ProfileFields(),
{{ADDRESS_HOME_STATE, "CA"}}},
// Tests that saving an identical profile except that the state is the
// full form instead of the abbreviation results in a merge and that the
// abbreviated state is retained.
{ProfileFields(),
{{ADDRESS_HOME_STATE, "California"}},
{{ADDRESS_HOME_STATE, "CA"}}},
// Tests that saving and identical profile except that the company name
// has different punctuation and case results in a merge and that the
// syntax of the new profile replaces the old one.
{{{COMPANY_NAME, "Stark inc"}},
{{COMPANY_NAME, "Stark Inc."}},
{{COMPANY_NAME, "Stark Inc."}}},
};
for (TestCase test_case : test_cases) {
SetupReferenceProfile();
const std::vector<AutofillProfile*>& initial_profiles =
personal_data_->GetProfiles();
// Apply changes to the original profile (if applicable).
for (ProfileField change : test_case.changes_to_original) {
initial_profiles.front()->SetRawInfo(
change.field_type, base::UTF8ToUTF16(change.field_value));
}
AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile2, "Marion", "Mitchell", "Morrison",
"johnwayne@me.xyz", "Fox", "123 Zoo St", "unit 5",
"Hollywood", "CA", "91601", "US", "12345678910");
// Apply changes to the second profile (if applicable).
for (ProfileField change : test_case.changes_to_new) {
profile2.SetRawInfo(change.field_type,
base::UTF8ToUTF16(change.field_value));
}
personal_data_->SaveImportedProfile(profile2);
const std::vector<AutofillProfile*>& saved_profiles =
personal_data_->GetProfiles();
// If there are no merge changes to verify, make sure that two profiles were
// saved.
if (test_case.changed_field_values.empty()) {
EXPECT_EQ(2U, saved_profiles.size());
} else {
EXPECT_EQ(1U, saved_profiles.size());
// Make sure the new information was merged correctly.
for (ProfileField changed_field : test_case.changed_field_values) {
EXPECT_EQ(base::UTF8ToUTF16(changed_field.field_value),
saved_profiles.front()->GetRawInfo(changed_field.field_type));
}
// Verify that the merged profile's use count, use date and modification
// date were properly updated.
EXPECT_EQ(1U, saved_profiles.front()->use_count());
EXPECT_GT(base::TimeDelta::FromMilliseconds(500),
base::Time::Now() - saved_profiles.front()->use_date());
EXPECT_GT(
base::TimeDelta::FromMilliseconds(500),
base::Time::Now() - saved_profiles.front()->modification_date());
}
// Erase the profiles for the next test.
ResetProfiles();
}
}
// Tests that MergeProfile tries to merge the imported profile into the
// existing profile in decreasing order of frecency.
TEST_F(PersonalDataManagerTest, MergeProfile_Frecency) {
// Create two very similar profiles except with different company names.
std::unique_ptr<AutofillProfile> profile1 = base::MakeUnique<AutofillProfile>(
base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(profile1.get(), "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "SNP", "742 Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "12345678910");
AutofillProfile* profile2 =
new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(profile2, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "Fox", "742 Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "12345678910");
// Give the "Fox" profile a bigger frecency score.
profile2->set_use_count(15);
// Create the |existing_profiles| vector.
std::vector<std::unique_ptr<AutofillProfile>> existing_profiles;
existing_profiles.push_back(std::move(profile1));
existing_profiles.push_back(base::WrapUnique(profile2));
// Create a new imported profile with no company name.
AutofillProfile imported_profile(base::GenerateGUID(),
"https://www.example.com");
test::SetProfileInfo(&imported_profile, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "", "742 Evergreen Terrace", "",
"Springfield", "IL", "91601", "US", "12345678910");
// Merge the imported profile into the existing profiles.
std::vector<AutofillProfile> profiles;
std::string guid = personal_data_->MergeProfile(
imported_profile, &existing_profiles, "US-EN", &profiles);
// The new profile should be merged into the "fox" profile.
EXPECT_EQ(profile2->guid(), guid);
}
// Tests that MergeProfile produces a merged profile with the expected usage
// statistics.
TEST_F(PersonalDataManagerTest, MergeProfile_UsageStats) {
// Create an initial profile with a use count of 10, an old use date and an
// old modification date of 4 days ago.
AutofillProfile* profile =
new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(profile, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "SNP", "742 Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "12345678910");
profile->set_use_count(4U);
profile->set_use_date(base::Time::Now() - base::TimeDelta::FromDays(4));
profile->set_modification_date(base::Time::Now() -
base::TimeDelta::FromDays(4));
// Create the |existing_profiles| vector.
std::vector<std::unique_ptr<AutofillProfile>> existing_profiles;
existing_profiles.push_back(base::WrapUnique(profile));
// Create a new imported profile that will get merged with the existing one.
AutofillProfile imported_profile(base::GenerateGUID(),
"https://www.example.com");
test::SetProfileInfo(&imported_profile, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "", "742 Evergreen Terrace", "",
"Springfield", "IL", "91601", "US", "12345678910");
// Merge the imported profile into the existing profiles.
std::vector<AutofillProfile> profiles;
std::string guid = personal_data_->MergeProfile(
imported_profile, &existing_profiles, "US-EN", &profiles);
// The new profile should be merged into the existing profile.
EXPECT_EQ(profile->guid(), guid);
// The use count should have be max(4, 1) => 4.
EXPECT_EQ(4U, profile->use_count());
// The use date and modification dates should have been set to less than 500
// milliseconds ago.
EXPECT_GT(base::TimeDelta::FromMilliseconds(500),
base::Time::Now() - profile->use_date());
EXPECT_GT(base::TimeDelta::FromMilliseconds(500),
base::Time::Now() - profile->modification_date());
}
// Tests that DedupeProfiles sets the correct profile guids to
// delete after merging similar profiles.
TEST_F(PersonalDataManagerTest, DedupeProfiles_ProfilesToDelete) {
// Create the profile for which to find duplicates. It has the highest
// frecency.
AutofillProfile* profile1 =
new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(profile1, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "", "742. Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "12345678910");
profile1->set_use_count(9);
// Create a different profile that should not be deduped (different address).
AutofillProfile* profile2 =
new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(profile2, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "Fox", "1234 Other Street", "",
"Springfield", "IL", "91601", "US", "12345678910");
profile2->set_use_count(7);
// Create a profile similar to profile1 which should be deduped.
AutofillProfile* profile3 =
new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(profile3, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "", "742 Evergreen Terrace", "",
"Springfield", "IL", "91601", "US", "12345678910");
profile3->set_use_count(5);
// Create another different profile that should not be deduped (different
// name).
AutofillProfile* profile4 =
new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(profile4, "Marjorie", "Jacqueline", "Simpson",
"homer.simpson@abc.com", "Fox", "742 Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "12345678910");
profile4->set_use_count(3);
// Create another profile similar to profile1. Since that one has the lowest
// frecency, the result of the merge should be in this profile at the end of
// the test.
AutofillProfile* profile5 =
new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(profile5, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
"", "Springfield", "IL", "91601", "US", "12345678910");
profile5->set_use_count(1);
// Add the profiles.
std::vector<std::unique_ptr<AutofillProfile>> existing_profiles;
existing_profiles.push_back(base::WrapUnique(profile1));
existing_profiles.push_back(base::WrapUnique(profile2));
existing_profiles.push_back(base::WrapUnique(profile3));
existing_profiles.push_back(base::WrapUnique(profile4));
existing_profiles.push_back(base::WrapUnique(profile5));
// Enable the profile cleanup.
EnableAutofillProfileCleanup();
base::HistogramTester histogram_tester;
std::unordered_map<std::string, std::string> guids_merge_map;
std::unordered_set<AutofillProfile*> profiles_to_delete;
personal_data_->DedupeProfiles(&existing_profiles, &profiles_to_delete,
&guids_merge_map);
// 5 profiles were considered for dedupe.
histogram_tester.ExpectUniqueSample(
"Autofill.NumberOfProfilesConsideredForDedupe", 5, 1);
// 2 profiles were removed (profiles 1 and 3).
histogram_tester.ExpectUniqueSample(
"Autofill.NumberOfProfilesRemovedDuringDedupe", 2, 1);
// Profile1 should be deleted because it was sent as the profile to merge and
// thus was merged into profile3 and then into profile5.
EXPECT_TRUE(profiles_to_delete.count(profile1));
// Profile3 should be deleted because profile1 was merged into it and the
// resulting profile was then merged into profile5.
EXPECT_TRUE(profiles_to_delete.count(profile3));
// Only these two profiles should be deleted.
EXPECT_EQ(2U, profiles_to_delete.size());
// All profiles should still be present in |existing_profiles|.
EXPECT_EQ(5U, existing_profiles.size());
}
// Tests that DedupeProfiles sets the correct merge mapping for billing address
// id references.
TEST_F(PersonalDataManagerTest, DedupeProfiles_GuidsMergeMap) {
// Create the profile for which to find duplicates. It has the highest
// frecency.
AutofillProfile* profile1 =
new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(profile1, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "", "742. Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "12345678910");
profile1->set_use_count(9);
// Create a different profile that should not be deduped (different address).
AutofillProfile* profile2 =
new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(profile2, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "Fox", "1234 Other Street", "",
"Springfield", "IL", "91601", "US", "12345678910");
profile2->set_use_count(7);
// Create a profile similar to profile1 which should be deduped.
AutofillProfile* profile3 =
new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(profile3, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "", "742 Evergreen Terrace", "",
"Springfield", "IL", "91601", "US", "12345678910");
profile3->set_use_count(5);
// Create another different profile that should not be deduped (different
// name).
AutofillProfile* profile4 =
new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(profile4, "Marjorie", "Jacqueline", "Simpson",
"homer.simpson@abc.com", "Fox", "742 Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "12345678910");
profile4->set_use_count(3);
// Create another profile similar to profile1. Since that one has the lowest
// frecency, the result of the merge should be in this profile at the end of
// the test.
AutofillProfile* profile5 =
new AutofillProfile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(profile5, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
"", "Springfield", "IL", "91601", "US", "12345678910");
profile5->set_use_count(1);
// Add the profiles.
std::vector<std::unique_ptr<AutofillProfile>> existing_profiles;
existing_profiles.push_back(base::WrapUnique(profile1));
existing_profiles.push_back(base::WrapUnique(profile2));
existing_profiles.push_back(base::WrapUnique(profile3));
existing_profiles.push_back(base::WrapUnique(profile4));
existing_profiles.push_back(base::WrapUnique(profile5));
// Enable the profile cleanup.
EnableAutofillProfileCleanup();
std::unordered_map<std::string, std::string> guids_merge_map;
std::unordered_set<AutofillProfile*> profiles_to_delete;
personal_data_->DedupeProfiles(&existing_profiles, &profiles_to_delete,
&guids_merge_map);
// The two profile merges should be recorded in the map.
EXPECT_EQ(2U, guids_merge_map.size());
// Profile 1 was merged into profile 3.
ASSERT_TRUE(guids_merge_map.count(profile1->guid()));
EXPECT_TRUE(guids_merge_map.at(profile1->guid()) == profile3->guid());
// Profile 3 was merged into profile 5.
ASSERT_TRUE(guids_merge_map.count(profile3->guid()));
EXPECT_TRUE(guids_merge_map.at(profile3->guid()) == profile5->guid());
}
// Tests that UpdateCardsBillingAddressReference sets the correct billing
// address id as specified in the map.
TEST_F(PersonalDataManagerTest, UpdateCardsBillingAddressReference) {
/* The merges will be as follow:
A -> B F (not merged)
\
-> E
/
C -> D
*/
std::unordered_map<std::string, std::string> guids_merge_map;
guids_merge_map.insert(std::pair<std::string, std::string>("A", "B"));
guids_merge_map.insert(std::pair<std::string, std::string>("C", "D"));
guids_merge_map.insert(std::pair<std::string, std::string>("B", "E"));
guids_merge_map.insert(std::pair<std::string, std::string>("D", "E"));
// Create cards that use A, D, E and F as their billing address id.
CreditCard* credit_card1 =
new CreditCard(base::GenerateGUID(), "https://www.example.com");
credit_card1->set_billing_address_id("A");
CreditCard* credit_card2 =
new CreditCard(base::GenerateGUID(), "https://www.example.com");
credit_card2->set_billing_address_id("D");
CreditCard* credit_card3 =
new CreditCard(base::GenerateGUID(), "https://www.example.com");
credit_card3->set_billing_address_id("E");
CreditCard* credit_card4 =
new CreditCard(base::GenerateGUID(), "https://www.example.com");
credit_card4->set_billing_address_id("F");
// Add the credit cards to the database.
personal_data_->local_credit_cards_.push_back(base::WrapUnique(credit_card1));
personal_data_->local_credit_cards_.push_back(base::WrapUnique(credit_card2));
personal_data_->local_credit_cards_.push_back(base::WrapUnique(credit_card3));
personal_data_->local_credit_cards_.push_back(base::WrapUnique(credit_card4));
personal_data_->UpdateCardsBillingAddressReference(guids_merge_map);
// The first card's billing address should now be E.
EXPECT_EQ("E", credit_card1->billing_address_id());
// The second card's billing address should now be E.
EXPECT_EQ("E", credit_card2->billing_address_id());
// The third card's billing address should still be E.
EXPECT_EQ("E", credit_card3->billing_address_id());
// The fourth card's billing address should still be F.
EXPECT_EQ("F", credit_card4->billing_address_id());
}
// Tests that ApplyDedupingRoutine updates the credit cards' billing address id
// based on the deduped profiles.
TEST_F(PersonalDataManagerTest,
ApplyDedupingRoutine_CardsBillingAddressIdUpdated) {
// A set of 6 profiles will be created. They should merge in this way:
// 1 -> 2 -> 3
// 4 -> 5
// 6
// Set their frencency score so that profile 3 has a higher score than 5, and
// 5 has a higher score than 6. This will ensure a deterministic order when
// verifying results.
// Create a set of 3 profiles to be merged together.
// Create a profile with a higher frecency score.
AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile1, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "", "742. Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "");
profile1.set_use_count(12);
profile1.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(1));
// Create a profile with a medium frecency score.
AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile2, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "", "742 Evergreen Terrace", "",
"Springfield", "IL", "91601", "", "12345678910");
profile2.set_use_count(5);
profile2.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(3));
// Create a profile with a lower frecency score.
AutofillProfile profile3(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile3, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
"", "Springfield", "IL", "91601", "", "");
profile3.set_use_count(3);
profile3.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(5));
// Create a set of two profiles to be merged together.
// Create a profile with a higher frecency score.
AutofillProfile profile4(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile4, "Marge", "B", "Simpson",
"marge.simpson@abc.com", "", "742. Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "");
profile4.set_use_count(11);
profile4.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(1));
// Create a profile with a lower frecency score.
AutofillProfile profile5(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile5, "Marge", "B", "Simpson",
"marge.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
"", "Springfield", "IL", "91601", "", "");
profile5.set_use_count(5);
profile5.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(3));
// Create a unique profile.
AutofillProfile profile6(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile6, "Bart", "J", "Simpson",
"bart.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
"", "Springfield", "IL", "91601", "", "");
profile6.set_use_count(10);
profile6.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(1));
// Add three credit cards. Give them a frecency score so that they are
// suggested in order (1, 2, 3). This will ensure a deterministic order for
// verifying results.
CreditCard credit_card1(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&credit_card1, "Clyde Barrow",
"347666888555" /* American Express */, "04", "2999");
credit_card1.set_use_count(10);
CreditCard credit_card2(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&credit_card2, "John Dillinger",
"423456789012" /* Visa */, "01", "2999");
credit_card2.set_use_count(5);
CreditCard credit_card3(base::GenerateGUID(), "https://www.example.com");
test::SetCreditCardInfo(&credit_card3, "Bonnie Parker",
"518765432109" /* Mastercard */, "12", "2999");
credit_card3.set_use_count(1);
// Associate the first card with profile1.
credit_card1.set_billing_address_id(profile1.guid());
// Associate the second card with profile4.
credit_card2.set_billing_address_id(profile4.guid());
// Associate the third card with profile6.
credit_card3.set_billing_address_id(profile6.guid());
personal_data_->AddProfile(profile1);
personal_data_->AddProfile(profile2);
personal_data_->AddProfile(profile3);
personal_data_->AddProfile(profile4);
personal_data_->AddProfile(profile5);
personal_data_->AddProfile(profile6);
personal_data_->AddCreditCard(credit_card1);
personal_data_->AddCreditCard(credit_card2);
personal_data_->AddCreditCard(credit_card3);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Make sure the 6 profiles and 3 credit cards were saved.
EXPECT_EQ(6U, personal_data_->GetProfiles().size());
EXPECT_EQ(3U, personal_data_->GetCreditCards().size());
// Enable the profile cleanup now. Otherwise it would be triggered by the
// calls to AddProfile.
EnableAutofillProfileCleanup();
EXPECT_TRUE(personal_data_->ApplyDedupingRoutine());
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Get the profiles and cards sorted by frecency to have a deterministic
// order.
std::vector<AutofillProfile*> profiles =
personal_data_->GetProfilesToSuggest();
std::vector<CreditCard*> credit_cards =
personal_data_->GetCreditCardsToSuggest();
// |profile1| should have been merged into |profile2| which should then have
// been merged into |profile3|. |profile4| should have been merged into
// |profile5| and |profile6| should not have merged. Therefore there should be
// 3 profile left.
ASSERT_EQ(3U, profiles.size());
// Make sure the remaining profiles are the expected ones.
EXPECT_EQ(profile3.guid(), profiles[0]->guid());
EXPECT_EQ(profile5.guid(), profiles[1]->guid());
EXPECT_EQ(profile6.guid(), profiles[2]->guid());
// |credit_card1|'s billing address should now be profile 3.
EXPECT_EQ(profile3.guid(), credit_cards[0]->billing_address_id());
// |credit_card2|'s billing address should now be profile 5.
EXPECT_EQ(profile5.guid(), credit_cards[1]->billing_address_id());
// |credit_card3|'s billing address should still be profile 6.
EXPECT_EQ(profile6.guid(), credit_cards[2]->billing_address_id());
}
// Tests that ApplyDedupingRoutine merges the profile values correctly, i.e.
// never lose information and keep the syntax of the profile with the higher
// frecency score.
TEST_F(PersonalDataManagerTest, ApplyDedupingRoutine_MergedProfileValues) {
// Create a profile with a higher frecency score.
AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile1, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "", "742. Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "");
profile1.set_use_count(10);
profile1.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(1));
// Create a profile with a medium frecency score.
AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile2, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "", "742 Evergreen Terrace", "",
"Springfield", "IL", "91601", "", "12345678910");
profile2.set_use_count(5);
profile2.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(3));
// Create a profile with a lower frecency score.
AutofillProfile profile3(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile3, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
"", "Springfield", "IL", "91601", "", "");
profile3.set_use_count(3);
profile3.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(5));
personal_data_->AddProfile(profile1);
personal_data_->AddProfile(profile2);
personal_data_->AddProfile(profile3);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Make sure the 3 profiles were saved;
EXPECT_EQ(3U, personal_data_->GetProfiles().size());
// Enable the profile cleanup now. Otherwise it would be triggered by the
// calls to AddProfile.
EnableAutofillProfileCleanup();
base::HistogramTester histogram_tester;
EXPECT_TRUE(personal_data_->ApplyDedupingRoutine());
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
std::vector<AutofillProfile*> profiles = personal_data_->GetProfiles();
// |profile1| should have been merged into |profile2| which should then have
// been merged into |profile3|. Therefore there should only be 1 saved
// profile.
ASSERT_EQ(1U, profiles.size());
// 3 profiles were considered for dedupe.
histogram_tester.ExpectUniqueSample(
"Autofill.NumberOfProfilesConsideredForDedupe", 3, 1);
// 2 profiles were removed (profiles 1 and 2).
histogram_tester.ExpectUniqueSample(
"Autofill.NumberOfProfilesRemovedDuringDedupe", 2, 1);
// Since profiles with higher frecency scores are merged into profiles with
// lower frecency scores, the result of the merge should be contained in
// profile3 since it had a lower frecency score compared to profile1.
EXPECT_EQ(profile3.guid(), profiles[0]->guid());
// The address syntax that results from the merge should be the one from the
// imported profile (highest frecency).
EXPECT_EQ(UTF8ToUTF16("742. Evergreen Terrace"),
profiles[0]->GetRawInfo(ADDRESS_HOME_LINE1));
// The middle name should be full, even if the profile with the higher
// frecency only had an initial (no loss of information).
EXPECT_EQ(UTF8ToUTF16("Jay"), profiles[0]->GetRawInfo(NAME_MIDDLE));
// The specified phone number from profile1 should be kept (no loss of
// information).
EXPECT_EQ(UTF8ToUTF16("12345678910"),
profiles[0]->GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
// The specified company name from profile2 should be kept (no loss of
// information).
EXPECT_EQ(UTF8ToUTF16("Fox"), profiles[0]->GetRawInfo(COMPANY_NAME));
// The specified country from the imported profile shoudl be kept (no loss of
// information).
EXPECT_EQ(UTF8ToUTF16("US"), profiles[0]->GetRawInfo(ADDRESS_HOME_COUNTRY));
// The use count that results from the merge should be the max of all the
// profiles use counts.
EXPECT_EQ(10U, profiles[0]->use_count());
// The use date that results from the merge should be the one from the
// profile1 since it was the most recently used profile.
EXPECT_LT(profile1.use_date() - base::TimeDelta::FromSeconds(10),
profiles[0]->use_date());
}
// Tests that ApplyDedupingRoutine only keeps the verified profile with its
// original data when deduping with similar profiles, even if it has a higher
// frecency score.
TEST_F(PersonalDataManagerTest, ApplyDedupingRoutine_VerifiedProfileFirst) {
// Create a verified profile with a higher frecency score.
AutofillProfile profile1(base::GenerateGUID(), kSettingsOrigin);
test::SetProfileInfo(&profile1, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "", "742 Evergreen Terrace", "",
"Springfield", "IL", "91601", "", "12345678910");
profile1.set_use_count(7);
profile1.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(1));
// Create a similar non verified profile with a medium frecency score.
AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile2, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "", "742. Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "");
profile2.set_use_count(5);
profile2.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(3));
// Create a similar non verified profile with a lower frecency score.
AutofillProfile profile3(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile3, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
"", "Springfield", "IL", "91601", "", "");
profile3.set_use_count(3);
profile3.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(5));
personal_data_->AddProfile(profile1);
personal_data_->AddProfile(profile2);
personal_data_->AddProfile(profile3);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Make sure the 3 profiles were saved.
EXPECT_EQ(3U, personal_data_->GetProfiles().size());
// Enable the profile cleanup now. Otherwise it would be triggered by the
// calls to AddProfile.
EnableAutofillProfileCleanup();
base::HistogramTester histogram_tester;
EXPECT_TRUE(personal_data_->ApplyDedupingRoutine());
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
std::vector<AutofillProfile*> profiles = personal_data_->GetProfiles();
// |profile2| should have merged with |profile3|. |profile3|
// should then have been discarded because it is similar to the verified
// |profile1|.
ASSERT_EQ(1U, profiles.size());
// 3 profiles were considered for dedupe.
histogram_tester.ExpectUniqueSample(
"Autofill.NumberOfProfilesConsideredForDedupe", 3, 1);
// 2 profile were removed (profiles 2 and 3).
histogram_tester.ExpectUniqueSample(
"Autofill.NumberOfProfilesRemovedDuringDedupe", 2, 1);
// Only the verified |profile1| with its original data should have been kept.
EXPECT_EQ(profile1.guid(), profiles[0]->guid());
EXPECT_TRUE(profile1 == *profiles[0]);
EXPECT_EQ(profile1.use_count(), profiles[0]->use_count());
EXPECT_LT(profile1.use_date() - TimeDelta::FromSeconds(2),
profiles[0]->use_date());
EXPECT_GT(profile1.use_date() + TimeDelta::FromSeconds(2),
profiles[0]->use_date());
}
// Tests that ApplyDedupingRoutine only keeps the verified profile with its
// original data when deduping with similar profiles, even if it has a lower
// frecency score.
TEST_F(PersonalDataManagerTest, ApplyDedupingRoutine_VerifiedProfileLast) {
// Create a profile to dedupe with a higher frecency score.
AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile1, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "", "742. Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "");
profile1.set_use_count(5);
profile1.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(3));
// Create a similar non verified profile with a medium frecency score.
AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile2, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
"", "Springfield", "IL", "91601", "", "");
profile2.set_use_count(5);
profile2.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(3));
// Create a similar verified profile with a lower frecency score.
AutofillProfile profile3(base::GenerateGUID(), kSettingsOrigin);
test::SetProfileInfo(&profile3, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "", "742 Evergreen Terrace", "",
"Springfield", "IL", "91601", "", "12345678910");
profile3.set_use_count(3);
profile3.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(5));
personal_data_->AddProfile(profile1);
personal_data_->AddProfile(profile2);
personal_data_->AddProfile(profile3);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Make sure the 3 profiles were saved.
EXPECT_EQ(3U, personal_data_->GetProfiles().size());
// Enable the profile cleanup now. Otherwise it would be triggered by the
// calls to AddProfile.
EnableAutofillProfileCleanup();
base::HistogramTester histogram_tester;
EXPECT_TRUE(personal_data_->ApplyDedupingRoutine());
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
std::vector<AutofillProfile*> profiles = personal_data_->GetProfiles();
// |profile1| should have merged with |profile2|. |profile2|
// should then have been discarded because it is similar to the verified
// |profile3|.
ASSERT_EQ(1U, profiles.size());
// 3 profiles were considered for dedupe.
histogram_tester.ExpectUniqueSample(
"Autofill.NumberOfProfilesConsideredForDedupe", 3, 1);
// 2 profile were removed (profiles 1 and 2).
histogram_tester.ExpectUniqueSample(
"Autofill.NumberOfProfilesRemovedDuringDedupe", 2, 1);
// Only the verified |profile2| with it's original data should have been kept.
EXPECT_EQ(profile3.guid(), profiles[0]->guid());
EXPECT_TRUE(profile3 == *profiles[0]);
EXPECT_EQ(profile3.use_count(), profiles[0]->use_count());
EXPECT_LT(profile3.use_date() - TimeDelta::FromSeconds(2),
profiles[0]->use_date());
EXPECT_GT(profile3.use_date() + TimeDelta::FromSeconds(2),
profiles[0]->use_date());
}
// Tests that ApplyDedupingRoutine does not merge unverified data into
// a verified profile. Also tests that two verified profiles don't get merged.
TEST_F(PersonalDataManagerTest, ApplyDedupingRoutine_MultipleVerifiedProfiles) {
// Create a profile to dedupe with a higher frecency score.
AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile1, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "", "742. Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "");
profile1.set_use_count(5);
profile1.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(3));
// Create a similar verified profile with a medium frecency score.
AutofillProfile profile2(base::GenerateGUID(), kSettingsOrigin);
test::SetProfileInfo(&profile2, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
"", "Springfield", "IL", "91601", "", "");
profile2.set_use_count(5);
profile2.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(3));
// Create a similar verified profile with a lower frecency score.
AutofillProfile profile3(base::GenerateGUID(), kSettingsOrigin);
test::SetProfileInfo(&profile3, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "", "742 Evergreen Terrace", "",
"Springfield", "IL", "91601", "", "12345678910");
profile3.set_use_count(3);
profile3.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(5));
personal_data_->AddProfile(profile1);
personal_data_->AddProfile(profile2);
personal_data_->AddProfile(profile3);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Make sure the 3 profiles were saved.
EXPECT_EQ(3U, personal_data_->GetProfiles().size());
// Enable the profile cleanup now. Otherwise it would be triggered by the
// calls to AddProfile.
EnableAutofillProfileCleanup();
base::HistogramTester histogram_tester;
EXPECT_TRUE(personal_data_->ApplyDedupingRoutine());
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Get the profiles, sorted by frecency to have a deterministic order.
std::vector<AutofillProfile*> profiles =
personal_data_->GetProfilesToSuggest();
// |profile1| should have been discarded because the saved profile with the
// highest frecency score is verified (|profile2|). Therefore, |profile1|'s
// data should not have been merged with |profile2|'s data. Then |profile2|
// should have been compared to |profile3| but they should not have merged
// because both profiles are verified.
ASSERT_EQ(2U, profiles.size());
// 3 profiles were considered for dedupe.
histogram_tester.ExpectUniqueSample(
"Autofill.NumberOfProfilesConsideredForDedupe", 3, 1);
// 1 profile was removed (|profile1|).
histogram_tester.ExpectUniqueSample(
"Autofill.NumberOfProfilesRemovedDuringDedupe", 1, 1);
EXPECT_EQ(profile2.guid(), profiles[0]->guid());
EXPECT_EQ(profile3.guid(), profiles[1]->guid());
// The profiles should have kept their original data.
EXPECT_TRUE(profile2 == *profiles[0]);
EXPECT_TRUE(profile3 == *profiles[1]);
EXPECT_EQ(profile2.use_count(), profiles[0]->use_count());
EXPECT_EQ(profile3.use_count(), profiles[1]->use_count());
EXPECT_LT(profile2.use_date() - TimeDelta::FromSeconds(2),
profiles[0]->use_date());
EXPECT_GT(profile2.use_date() + TimeDelta::FromSeconds(2),
profiles[0]->use_date());
EXPECT_LT(profile3.use_date() - TimeDelta::FromSeconds(2),
profiles[1]->use_date());
EXPECT_GT(profile3.use_date() + TimeDelta::FromSeconds(2),
profiles[1]->use_date());
}
// Tests that ApplyProfileUseDatesFix sets the use date of profiles from an
// incorrect value of 0 to [two weeks from now]. Also tests that SetProfiles
// does not modify any other profiles.
TEST_F(PersonalDataManagerTest, ApplyProfileUseDatesFix) {
// Set the kAutofillProfileUseDatesFixed pref to true so that the fix is not
// applied just yet.
personal_data_->pref_service_->SetBoolean(
prefs::kAutofillProfileUseDatesFixed, true);
// Create a profile. The use date will be set to now automatically.
AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile1, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "SNP", "742 Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "12345678910");
// Create another profile and set its use date to the default value.
AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile2, "Marge", "", "Simpson",
"homer.simpson@abc.com", "SNP", "742 Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "12345678910");
profile2.set_use_date(base::Time());
personal_data_->AddProfile(profile1);
personal_data_->AddProfile(profile2);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Get a sorted list of profiles. |profile1| will be first and |profile2| will
// be second.
std::vector<AutofillProfile*> saved_profiles =
personal_data_->GetProfilesToSuggest();
ASSERT_EQ(2U, saved_profiles.size());
// The use dates should not have been modified.
EXPECT_LE(base::Time::Now() - base::TimeDelta::FromDays(1),
saved_profiles[0]->use_date());
EXPECT_EQ(base::Time(), saved_profiles[1]->use_date());
// Set the pref to false to indicate the fix has never been run.
personal_data_->pref_service_->SetBoolean(
prefs::kAutofillProfileUseDatesFixed, false);
personal_data_->ApplyProfileUseDatesFix();
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Get a sorted list of profiles.
saved_profiles = personal_data_->GetProfilesToSuggest();
ASSERT_EQ(2U, saved_profiles.size());
// |profile1|'s use date should not have been modified.
EXPECT_LE(base::Time::Now() - base::TimeDelta::FromDays(1),
saved_profiles[0]->use_date());
// |profile2|'s use date should have been set to two weeks before now.
EXPECT_LE(base::Time::Now() - base::TimeDelta::FromDays(15),
saved_profiles[1]->use_date());
EXPECT_GE(base::Time::Now() - base::TimeDelta::FromDays(13),
saved_profiles[1]->use_date());
}
// Tests that ApplyProfileUseDatesFix does apply the fix if it's already been
// applied.
TEST_F(PersonalDataManagerTest, ApplyProfileUseDatesFix_NotAppliedTwice) {
// Set the kAutofillProfileUseDatesFixed pref which means the fix has already
// been applied.
personal_data_->pref_service_->SetBoolean(
prefs::kAutofillProfileUseDatesFixed, true);
// Create two profiles.
AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile1, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "SNP", "742 Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "12345678910");
AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile2, "Marge", "", "Simpson",
"homer.simpson@abc.com", "SNP", "742 Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "12345678910");
profile2.set_use_date(base::Time());
personal_data_->AddProfile(profile1);
personal_data_->AddProfile(profile2);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Get a sorted list of profiles. |profile1| will be first and |profile2| will
// be second.
std::vector<AutofillProfile*> saved_profiles =
personal_data_->GetProfilesToSuggest();
ASSERT_EQ(2U, saved_profiles.size());
// The use dates should not have been modified.
EXPECT_LE(base::Time::Now() - base::TimeDelta::FromDays(1),
saved_profiles[0]->use_date());
EXPECT_EQ(base::Time(), saved_profiles[1]->use_date());
}
// Tests that ApplyDedupingRoutine works as expected in a realistic scenario.
// Tests that it merges the diffent set of similar profiles independently and
// that the resulting profiles have the right values, has no effect on the other
// profiles and that the data of verified profiles is not modified.
TEST_F(PersonalDataManagerTest, ApplyDedupingRoutine_MultipleDedupes) {
// Create a Homer home profile with a higher frecency score than other Homer
// profiles.
AutofillProfile Homer1(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&Homer1, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "", "742. Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "");
Homer1.set_use_count(10);
Homer1.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(1));
// Create a Homer home profile with a medium frecency score compared to other
// Homer profiles.
AutofillProfile Homer2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&Homer2, "Homer", "Jay", "Simpson",
"homer.simpson@abc.com", "", "742 Evergreen Terrace", "",
"Springfield", "IL", "91601", "", "12345678910");
Homer2.set_use_count(5);
Homer2.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(3));
// Create a Homer home profile with a lower frecency score than other Homer
// profiles.
AutofillProfile Homer3(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&Homer3, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
"", "Springfield", "IL", "91601", "", "");
Homer3.set_use_count(3);
Homer3.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(5));
// Create a Homer work profile (different address).
AutofillProfile Homer4(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&Homer4, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "Fox", "12 Nuclear Plant.", "",
"Springfield", "IL", "91601", "US", "9876543");
Homer4.set_use_count(3);
Homer4.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(5));
// Create a Marge profile with a lower frecency score that other Marge
// profiles.
AutofillProfile Marge1(base::GenerateGUID(), kSettingsOrigin);
test::SetProfileInfo(&Marge1, "Marjorie", "J", "Simpson",
"marge.simpson@abc.com", "", "742 Evergreen Terrace", "",
"Springfield", "IL", "91601", "", "12345678910");
Marge1.set_use_count(4);
Marge1.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(3));
// Create a verified Marge home profile with a lower frecency score that the
// other Marge profile.
AutofillProfile Marge2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&Marge2, "Marjorie", "Jacqueline", "Simpson",
"marge.simpson@abc.com", "", "742 Evergreen Terrace", "",
"Springfield", "IL", "91601", "", "12345678910");
Marge2.set_use_count(2);
Marge2.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(3));
// Create a Barney profile (guest user).
AutofillProfile Barney(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&Barney, "Barney", "", "Gumble", "barney.gumble@abc.com",
"ABC", "123 Other Street", "", "Springfield", "IL",
"91601", "", "");
Barney.set_use_count(1);
Barney.set_use_date(base::Time::Now() - base::TimeDelta::FromDays(180));
personal_data_->AddProfile(Homer1);
personal_data_->AddProfile(Homer2);
personal_data_->AddProfile(Homer3);
personal_data_->AddProfile(Homer4);
personal_data_->AddProfile(Marge1);
personal_data_->AddProfile(Marge2);
personal_data_->AddProfile(Barney);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Make sure the 7 profiles were saved;
EXPECT_EQ(7U, personal_data_->GetProfiles().size());
// Enable the profile cleanup now. Otherwise it would be triggered by the
// calls to AddProfile.
EnableAutofillProfileCleanup();
base::HistogramTester histogram_tester;
// |Homer1| should get merged into |Homer2| which should then be merged into
// |Homer3|. |Marge2| should be discarded in favor of |Marge1| which is
// verified. |Homer4| and |Barney| should not be deduped at all.
EXPECT_TRUE(personal_data_->ApplyDedupingRoutine());
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Get the profiles, sorted by frecency to have a deterministic order.
std::vector<AutofillProfile*> profiles =
personal_data_->GetProfilesToSuggest();
// The 2 duplicates Homer home profiles with the higher frecency and the
// unverified Marge profile should have been deduped.
ASSERT_EQ(4U, profiles.size());
// 7 profiles were considered for dedupe.
histogram_tester.ExpectUniqueSample(
"Autofill.NumberOfProfilesConsideredForDedupe", 7, 1);
// 3 profile were removed (|Homer1|, |Homer2| and |Marge2|).
histogram_tester.ExpectUniqueSample(
"Autofill.NumberOfProfilesRemovedDuringDedupe", 3, 1);
// The remaining profiles should be |Homer3|, |Marge1|, |Homer4| and |Barney|
// in this order of frecency.
EXPECT_EQ(Homer3.guid(), profiles[0]->guid());
EXPECT_EQ(Marge1.guid(), profiles[1]->guid());
EXPECT_EQ(Homer4.guid(), profiles[2]->guid());
EXPECT_EQ(Barney.guid(), profiles[3]->guid());
// |Homer3|'s data:
// The address should be saved with the syntax of |Homer1| since it has the
// highest frecency score.
EXPECT_EQ(UTF8ToUTF16("742. Evergreen Terrace"),
profiles[0]->GetRawInfo(ADDRESS_HOME_LINE1));
// The middle name should be the full version found in |Homer2|,
EXPECT_EQ(UTF8ToUTF16("Jay"), profiles[0]->GetRawInfo(NAME_MIDDLE));
// The phone number from |Homer2| should be kept (no loss of information).
EXPECT_EQ(UTF8ToUTF16("12345678910"),
profiles[0]->GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
// The company name from |Homer3| should be kept (no loss of information).
EXPECT_EQ(UTF8ToUTF16("Fox"), profiles[0]->GetRawInfo(COMPANY_NAME));
// The country from |Homer1| profile should be kept (no loss of information).
EXPECT_EQ(UTF8ToUTF16("US"), profiles[0]->GetRawInfo(ADDRESS_HOME_COUNTRY));
// The use count that results from the merge should be the max of Homer 1, 2
// and 3's respective use counts.
EXPECT_EQ(10U, profiles[0]->use_count());
// The use date that results from the merge should be the one from the
// |Homer1| since it was the most recently used profile.
EXPECT_LT(Homer1.use_date() - base::TimeDelta::FromSeconds(5),
profiles[0]->use_date());
EXPECT_GT(Homer1.use_date() + base::TimeDelta::FromSeconds(5),
profiles[0]->use_date());
// The other profiles should not have been modified.
EXPECT_TRUE(Marge1 == *profiles[1]);
EXPECT_TRUE(Homer4 == *profiles[2]);
EXPECT_TRUE(Barney == *profiles[3]);
}
// Tests that ApplyDedupingRoutine is not run if the feature is disabled.
TEST_F(PersonalDataManagerTest, ApplyDedupingRoutine_FeatureDisabled) {
// Create a profile to dedupe.
AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile1, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "", "742. Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "");
// Create a similar profile.
AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile2, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
"", "Springfield", "IL", "91601", "", "");
personal_data_->AddProfile(profile1);
personal_data_->AddProfile(profile2);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Make sure both profiles were saved.
EXPECT_EQ(2U, personal_data_->GetProfiles().size());
// The deduping routine should not be run.
EXPECT_FALSE(personal_data_->ApplyDedupingRoutine());
// Both profiles should still be present.
EXPECT_EQ(2U, personal_data_->GetProfiles().size());
}
TEST_F(PersonalDataManagerTest, ApplyDedupingRoutine_NopIfZeroProfiles) {
EXPECT_TRUE(personal_data_->GetProfiles().empty());
EnableAutofillProfileCleanup();
EXPECT_FALSE(personal_data_->ApplyDedupingRoutine());
}
TEST_F(PersonalDataManagerTest, ApplyDedupingRoutine_NopIfOneProfile) {
// Create a profile to dedupe.
AutofillProfile profile(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "", "742. Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "");
personal_data_->AddProfile(profile);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
EXPECT_EQ(1U, personal_data_->GetProfiles().size());
// Enable the profile cleanup now. Otherwise it would be triggered by the
// calls to AddProfile.
EnableAutofillProfileCleanup();
EXPECT_FALSE(personal_data_->ApplyDedupingRoutine());
}
// Tests that ApplyDedupingRoutine is not run a second time on the same major
// version.
TEST_F(PersonalDataManagerTest, ApplyDedupingRoutine_OncePerVersion) {
// Create a profile to dedupe.
AutofillProfile profile1(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile1, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "", "742. Evergreen Terrace",
"", "Springfield", "IL", "91601", "US", "");
// Create a similar profile.
AutofillProfile profile2(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile2, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
"", "Springfield", "IL", "91601", "", "");
personal_data_->AddProfile(profile1);
personal_data_->AddProfile(profile2);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
EXPECT_EQ(2U, personal_data_->GetProfiles().size());
// Enable the profile cleanup now. Otherwise it would be triggered by the
// calls to AddProfile.
EnableAutofillProfileCleanup();
// The deduping routine should be run a first time.
EXPECT_TRUE(personal_data_->ApplyDedupingRoutine());
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
std::vector<AutofillProfile*> profiles = personal_data_->GetProfiles();
// The profiles should have been deduped
EXPECT_EQ(1U, profiles.size());
// Add another duplicate profile.
AutofillProfile profile3(base::GenerateGUID(), "https://www.example.com");
test::SetProfileInfo(&profile3, "Homer", "J", "Simpson",
"homer.simpson@abc.com", "Fox", "742 Evergreen Terrace.",
"", "Springfield", "IL", "91601", "", "");
personal_data_->AddProfile(profile3);
EXPECT_CALL(personal_data_observer_, OnPersonalDataChanged())
.WillOnce(QuitMainMessageLoop());
base::RunLoop().Run();
// Make sure |profile3| was saved.
EXPECT_EQ(2U, personal_data_->GetProfiles().size());
// Re-enable the profile cleanup now that the profile was added.
EnableAutofillProfileCleanup();
// The deduping routine should not be run.
EXPECT_FALSE(personal_data_->ApplyDedupingRoutine());
// The two duplicate profiles should still be present.
EXPECT_EQ(2U, personal_data_->GetProfiles().size());
}
} // namespace autofill
|