summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/autofill/autofill_dialog_controller_unittest.cc
blob: e087266fadf217b82483380335abec1f8e64c036 (plain)
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
// Copyright (c) 2012 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 <map>
#include <utility>

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "base/guid.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/prefs/pref_service.h"
#include "base/run_loop.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/signin/account_tracker_service_factory.h"
#include "chrome/browser/ui/autofill/autofill_dialog_controller_impl.h"
#include "chrome/browser/ui/autofill/autofill_dialog_i18n_input.h"
#include "chrome/browser/ui/autofill/autofill_dialog_view.h"
#include "chrome/browser/ui/autofill/generated_credit_card_bubble_controller.h"
#include "chrome/browser/ui/autofill/mock_address_validator.h"
#include "chrome/browser/ui/autofill/mock_new_credit_card_bubble_controller.h"
#include "chrome/browser/ui/autofill/test_generated_credit_card_bubble_controller.h"
#include "chrome/browser/web_data_service_factory.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/render_messages.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "chrome/test/base/scoped_testing_local_state.h"
#include "chrome/test/base/testing_browser_process.h"
#include "chrome/test/base/testing_profile.h"
#include "components/autofill/content/browser/risk/proto/fingerprint.pb.h"
#include "components/autofill/content/browser/wallet/full_wallet.h"
#include "components/autofill/content/browser/wallet/gaia_account.h"
#include "components/autofill/content/browser/wallet/instrument.h"
#include "components/autofill/content/browser/wallet/mock_wallet_client.h"
#include "components/autofill/content/browser/wallet/wallet_address.h"
#include "components/autofill/content/browser/wallet/wallet_service_url.h"
#include "components/autofill/content/browser/wallet/wallet_test_util.h"
#include "components/autofill/core/browser/autofill_metrics.h"
#include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/test_personal_data_manager.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/autofill/core/common/autofill_switches.h"
#include "components/autofill/core/common/form_data.h"
#include "components/signin/core/browser/account_tracker_service.h"
#include "components/user_prefs/user_prefs.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/mock_render_process_host.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "grit/components_scaled_resources.h"
#include "grit/components_strings.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/libaddressinput/chromium/chrome_address_validator.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_field.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_problem.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"

#if defined(OS_WIN)
#include "ui/base/win/scoped_ole_initializer.h"
#endif

using base::ASCIIToUTF16;
using base::UTF8ToUTF16;

namespace autofill {

namespace {

using ::i18n::addressinput::FieldProblemMap;
using testing::AtLeast;
using testing::DoAll;
using testing::Return;
using testing::SetArgPointee;
using testing::_;

const char kSourceUrl[] = "http://localbike.shop";
const char kFakeEmail[] = "user@chromium.org";
const char kEditedBillingAddress[] = "123 edited billing address";
const char* kFieldsFromPage[] =
    { "email",
      "cc-name",
      "cc-number",
      "cc-exp-month",
      "cc-exp-year",
      "cc-csc",
      "billing name",
      "billing address-line1",
      "billing address-level2",
      "billing address-level1",
      "billing postal-code",
      "billing country",
      "billing tel",
      "shipping name",
      "shipping address-line1",
      "shipping address-level2",
      "shipping address-level1",
      "shipping postal-code",
      "shipping country",
      "shipping tel",
    };
const char kSettingsOrigin[] = "Chrome settings";
const char kTestCCNumberAmex[] = "376200000000002";
const char kTestCCNumberVisa[] = "4111111111111111";
const char kTestCCNumberMaster[] = "5555555555554444";
const char kTestCCNumberDiscover[] = "6011111111111117";
const char kTestCCNumberIncomplete[] = "4111111111";
// Credit card number fails Luhn check.
const char kTestCCNumberInvalid[] = "4111111111111112";

// Copies the initial values from |inputs| into |outputs|.
void CopyInitialValues(const DetailInputs& inputs, FieldValueMap* outputs) {
  for (size_t i = 0; i < inputs.size(); ++i) {
    const DetailInput& input = inputs[i];
    (*outputs)[input.type] = input.initial_value;
  }
}

scoped_ptr<wallet::WalletItems> CompleteAndValidWalletItems() {
  scoped_ptr<wallet::WalletItems> items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  items->AddAccount(wallet::GetTestGaiaAccount());
  items->AddInstrument(wallet::GetTestMaskedInstrument());
  items->AddAddress(wallet::GetTestShippingAddress());
  return items.Pass();
}

bool HasAnyError(const ValidityMessages& messages, ServerFieldType field) {
  return !messages.GetMessageOrDefault(field).text.empty();
}

bool HasUnsureError(const ValidityMessages& messages, ServerFieldType field) {
  const ValidityMessage& message = messages.GetMessageOrDefault(field);
  return !message.text.empty() && !message.sure;
}

class TestAutofillDialogView : public AutofillDialogView {
 public:
  TestAutofillDialogView()
      : updates_started_(0), save_details_locally_checked_(true) {}
  ~TestAutofillDialogView() override {}

  void Show() override {}
  void Hide() override {}

  void UpdatesStarted() override {
    updates_started_++;
  }

  void UpdatesFinished() override {
    updates_started_--;
    EXPECT_GE(updates_started_, 0);
  }

  void UpdateNotificationArea() override {
    EXPECT_GE(updates_started_, 1);
  }

  void UpdateAccountChooser() override {
    EXPECT_GE(updates_started_, 1);
  }

  void UpdateButtonStrip() override {
    EXPECT_GE(updates_started_, 1);
  }

  void UpdateOverlay() override {
    EXPECT_GE(updates_started_, 1);
  }

  void UpdateDetailArea() override {
    EXPECT_GE(updates_started_, 1);
  }

  void UpdateSection(DialogSection section) override {
    section_updates_[section]++;
    EXPECT_GE(updates_started_, 1);
  }

  void UpdateErrorBubble() override {
    EXPECT_GE(updates_started_, 1);
  }

  void FillSection(DialogSection section,
                   ServerFieldType originating_type) override {}
  void GetUserInput(DialogSection section, FieldValueMap* output) override {
    *output = outputs_[section];
  }

  base::string16 GetCvc() override { return base::string16(); }

  bool SaveDetailsLocally() override { return save_details_locally_checked_; }

  const content::NavigationController* ShowSignIn(const GURL& url) override {
    return NULL;
  }
  void HideSignIn() override {}

  MOCK_METHOD0(ModelChanged, void());
  MOCK_METHOD0(UpdateForErrors, void());

  void OnSignInResize(const gfx::Size& pref_size) override {}
  void ValidateSection(DialogSection) override {}

  void SetUserInput(DialogSection section, const FieldValueMap& map) {
    outputs_[section] = map;
  }

  void CheckSaveDetailsLocallyCheckbox(bool checked) {
    save_details_locally_checked_ = checked;
  }

  void ClearSectionUpdates() {
    section_updates_.clear();
  }

  std::map<DialogSection, size_t> section_updates() const {
    return section_updates_;
  }

 private:
  std::map<DialogSection, FieldValueMap> outputs_;
  std::map<DialogSection, size_t> section_updates_;

  int updates_started_;
  bool save_details_locally_checked_;

  DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogView);
};

class TestAutofillDialogController
    : public AutofillDialogControllerImpl,
      public base::SupportsWeakPtr<TestAutofillDialogController> {
 public:
  TestAutofillDialogController(
      content::WebContents* contents,
      const FormData& form_structure,
      const GURL& source_url,
      const AutofillClient::ResultCallback& callback,
      MockNewCreditCardBubbleController* mock_new_card_bubble_controller)
      : AutofillDialogControllerImpl(contents,
                                     form_structure,
                                     source_url,
                                     callback),
        mock_wallet_client_(
            Profile::FromBrowserContext(contents->GetBrowserContext())
                ->GetRequestContext(),
            this,
            source_url),
        mock_new_card_bubble_controller_(mock_new_card_bubble_controller),
        submit_button_delay_count_(0) {}

  ~TestAutofillDialogController() override {}

  AutofillDialogView* CreateView() override {
    return new testing::NiceMock<TestAutofillDialogView>();
  }

  void Init(content::BrowserContext* browser_context) {
    Profile* profile = Profile::FromBrowserContext(browser_context);
    test_manager_.Init(WebDataServiceFactory::GetAutofillWebDataForProfile(
                           profile,
                           ServiceAccessType::EXPLICIT_ACCESS),
                       user_prefs::UserPrefs::Get(browser_context),
                       AccountTrackerServiceFactory::GetForProfile(profile),
                       browser_context->IsOffTheRecord());
  }

  TestAutofillDialogView* GetView() {
    return static_cast<TestAutofillDialogView*>(view());
  }

  TestPersonalDataManager* GetTestingManager() {
    return &test_manager_;
  }

  MockAddressValidator* GetMockValidator() {
    return &mock_validator_;
  }

  wallet::MockWalletClient* GetTestingWalletClient() {
    return &mock_wallet_client_;
  }

  const GURL& open_tab_url() { return open_tab_url_; }

  void SimulateSigninError() {
    OnWalletSigninError();
  }

  // Skips past the 2 second wait between FinishSubmit and DoFinishSubmit.
  void ForceFinishSubmit() {
    DoFinishSubmit();
  }

  void SimulateSubmitButtonDelayBegin() {
    AutofillDialogControllerImpl::SubmitButtonDelayBegin();
  }

  void SimulateSubmitButtonDelayEnd() {
    AutofillDialogControllerImpl::SubmitButtonDelayEndForTesting();
  }

  using AutofillDialogControllerImpl::
      ClearLastWalletItemsFetchTimestampForTesting;

  // Returns the number of times that the submit button was delayed.
  int get_submit_button_delay_count() const {
    return submit_button_delay_count_;
  }

  MOCK_METHOD0(LoadRiskFingerprintData, void());
  using AutofillDialogControllerImpl::AccountChooserModelForTesting;
  using AutofillDialogControllerImpl::OnDidLoadRiskFingerprintData;
  using AutofillDialogControllerImpl::IsEditingExistingData;
  using AutofillDialogControllerImpl::IsManuallyEditingSection;
  using AutofillDialogControllerImpl::IsPayingWithWallet;
  using AutofillDialogControllerImpl::IsSubmitPausedOn;
  using AutofillDialogControllerImpl::NOT_CHECKED;
  using AutofillDialogControllerImpl::popup_input_type;
  using AutofillDialogControllerImpl::SignedInState;

 protected:
  PersonalDataManager* GetManager() const override {
    return const_cast<TestAutofillDialogController*>(this)->
        GetTestingManager();
  }

  AddressValidator* GetValidator() override {
    return &mock_validator_;
  }

  wallet::WalletClient* GetWalletClient() override {
    return &mock_wallet_client_;
  }

  void OpenTabWithUrl(const GURL& url) override {
    open_tab_url_ = url;
  }

  void ShowNewCreditCardBubble(
      scoped_ptr<CreditCard> new_card,
      scoped_ptr<AutofillProfile> billing_profile) override {
    mock_new_card_bubble_controller_->Show(new_card.Pass(),
                                           billing_profile.Pass());
  }

  // AutofillDialogControllerImpl calls this method before showing the dialog
  // window.
  void SubmitButtonDelayBegin() override {
    // Do not delay enabling the submit button in testing.
    submit_button_delay_count_++;
  }

 private:
  TestPersonalDataManager test_manager_;
  testing::NiceMock<wallet::MockWalletClient> mock_wallet_client_;

  // A mock validator object to prevent network requests and track when
  // validation rules are loaded or validation attempts occur.
  testing::NiceMock<MockAddressValidator> mock_validator_;

  GURL open_tab_url_;
  MockNewCreditCardBubbleController* mock_new_card_bubble_controller_;

  // The number of times that the submit button was delayed.
  int submit_button_delay_count_;

  DISALLOW_COPY_AND_ASSIGN(TestAutofillDialogController);
};

class AutofillDialogControllerTest : public ChromeRenderViewHostTestHarness {
 protected:
  AutofillDialogControllerTest(): form_structure_(NULL) {}

  // testing::Test implementation:
  void SetUp() override {
    ChromeRenderViewHostTestHarness::SetUp();
    Reset();
  }

  void TearDown() override {
    if (controller_)
      controller_->ViewClosed();
    ChromeRenderViewHostTestHarness::TearDown();
  }

  void Reset() {
    if (controller_)
      controller_->ViewClosed();

    test_generated_bubble_controller_ =
        new testing::NiceMock<TestGeneratedCreditCardBubbleController>(
            web_contents());
    ASSERT_TRUE(test_generated_bubble_controller_->IsInstalled());

    mock_new_card_bubble_controller_.reset(
        new MockNewCreditCardBubbleController);

    profile()->GetPrefs()->ClearPref(::prefs::kAutofillDialogSaveData);

    // We have to clear the old local state before creating a new one.
    scoped_local_state_.reset();
    scoped_local_state_.reset(new ScopedTestingLocalState(
        TestingBrowserProcess::GetGlobal()));

    SetUpControllerWithFormData(DefaultFormData());
  }

  FormData DefaultFormData() {
    FormData form_data;
    for (size_t i = 0; i < arraysize(kFieldsFromPage); ++i) {
      FormFieldData field;
      field.autocomplete_attribute = kFieldsFromPage[i];
      form_data.fields.push_back(field);
    }
    return form_data;
  }

  // Creates a new controller for |form_data|.
  void ResetControllerWithFormData(const FormData& form_data) {
    if (controller_)
      controller_->ViewClosed();

    AutofillClient::ResultCallback callback =
        base::Bind(&AutofillDialogControllerTest::FinishedCallback,
                   base::Unretained(this));
    controller_ = (new testing::NiceMock<TestAutofillDialogController>(
        web_contents(),
        form_data,
        GURL(kSourceUrl),
        callback,
        mock_new_card_bubble_controller_.get()))->AsWeakPtr();
    controller_->Init(profile());
  }

  // Creates a new controller for |form_data| and sets up some initial wallet
  // data for it.
  void SetUpControllerWithFormData(const FormData& form_data) {
    ResetControllerWithFormData(form_data);
    controller()->Show();
    if (controller() &&
        !profile()->GetPrefs()->GetBoolean(
            ::prefs::kAutofillDialogPayWithoutWallet)) {
      EXPECT_CALL(*controller()->GetTestingWalletClient(),
                  GetWalletItems(_, _));
      controller()->OnDidFetchWalletCookieValue(std::string());
      controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
    }
  }

  // Fills the inputs in SECTION_CC with data.
  void FillCreditCardInputs() {
    FieldValueMap cc_outputs;
    const DetailInputs& cc_inputs =
        controller()->RequestedFieldsForSection(SECTION_CC);
    for (size_t i = 0; i < cc_inputs.size(); ++i) {
      cc_outputs[cc_inputs[i].type] = cc_inputs[i].type == CREDIT_CARD_NUMBER ?
          ASCIIToUTF16(kTestCCNumberVisa) : ASCIIToUTF16("11");
    }
    controller()->GetView()->SetUserInput(SECTION_CC, cc_outputs);
  }

  // Fills the inputs in SECTION_CC_BILLING with valid data.
  void FillCCBillingInputs() {
    FieldValueMap outputs;
    const DetailInputs& inputs =
        controller()->RequestedFieldsForSection(SECTION_CC_BILLING);
    AutofillProfile full_profile(test::GetVerifiedProfile());
    CreditCard full_card(test::GetCreditCard());
    for (size_t i = 0; i < inputs.size(); ++i) {
      const ServerFieldType type = inputs[i].type;
      outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");

      if (outputs[type].empty())
        outputs[type] = full_card.GetInfo(AutofillType(type), "en-US");
    }
    controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
  }

  // Activates the 'Add new foo' option from the |section|'s suggestions
  // dropdown and fills the |section|'s inputs with the data from the
  // |data_model|.  If |section| is SECTION_CC, also fills in '123' for the CVC.
  void FillInputs(DialogSection section, const AutofillDataModel& data_model) {
    // Select the 'Add new foo' option.
    ui::MenuModel* model = GetMenuModelForSection(section);
    if (model)
      model->ActivatedAt(model->GetItemCount() - 2);

    // Fill the inputs.
    FieldValueMap outputs;
    const DetailInputs& inputs =
        controller()->RequestedFieldsForSection(section);
    for (size_t i = 0; i < inputs.size(); ++i) {
      ServerFieldType type = inputs[i].type;
      base::string16 output;
      if (type == CREDIT_CARD_VERIFICATION_CODE)
        output = ASCIIToUTF16("123");
      else
        output = data_model.GetInfo(AutofillType(type), "en-US");
      outputs[inputs[i].type] = output;
    }
    controller()->GetView()->SetUserInput(section, outputs);
  }

  std::vector<DialogNotification> NotificationsOfType(
      DialogNotification::Type type) {
    std::vector<DialogNotification> right_type;
    const std::vector<DialogNotification>& notifications =
        controller()->CurrentNotifications();
    for (size_t i = 0; i < notifications.size(); ++i) {
      if (notifications[i].type() == type)
        right_type.push_back(notifications[i]);
    }
    return right_type;
  }

  void SwitchToAutofill() {
    ui::MenuModel* model = controller_->MenuModelForAccountChooser();
    model->ActivatedAt(model->GetItemCount() - 1);
  }

  void SwitchToWallet() {
    controller_->MenuModelForAccountChooser()->ActivatedAt(0);
  }

  void SimulateSigninError() {
    controller_->SimulateSigninError();
  }

  void UseBillingForShipping() {
    controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(0);
  }

  base::string16 ValidateCCNumber(DialogSection section,
                                  const std::string& cc_number,
                                  bool should_pass) {
    FieldValueMap outputs;
    outputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("United States");
    outputs[CREDIT_CARD_NUMBER] = UTF8ToUTF16(cc_number);
    ValidityMessages messages =
        controller()->InputsAreValid(section, outputs);
    EXPECT_EQ(should_pass, !messages.HasSureError(CREDIT_CARD_NUMBER));
    return messages.GetMessageOrDefault(CREDIT_CARD_NUMBER).text;
  }

  void SubmitWithWalletItems(scoped_ptr<wallet::WalletItems> wallet_items) {
    controller()->OnDidGetWalletItems(wallet_items.Pass());
    AcceptAndLoadFakeFingerprint();
  }

  void AcceptAndLoadFakeFingerprint() {
    controller()->OnAccept();
    controller()->OnDidLoadRiskFingerprintData("a");
  }

  // Returns true if the given |section| contains a field of the given |type|.
  bool SectionContainsField(DialogSection section, ServerFieldType type) {
    const DetailInputs& inputs =
        controller()->RequestedFieldsForSection(section);
    for (DetailInputs::const_iterator it = inputs.begin(); it != inputs.end();
         ++it) {
      if (it->type == type)
        return true;
    }
    return false;
  }

  SuggestionsMenuModel* GetMenuModelForSection(DialogSection section) {
    ui::MenuModel* model = controller()->MenuModelForSection(section);
    return static_cast<SuggestionsMenuModel*>(model);
  }

  void SubmitAndVerifyShippingAndBillingResults() {
    // Test after setting use billing for shipping.
    UseBillingForShipping();

    controller()->OnAccept();

    ASSERT_EQ(20U, form_structure()->field_count());
    EXPECT_EQ(ADDRESS_HOME_COUNTRY,
              form_structure()->field(11)->Type().GetStorableType());
    EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(11)->Type().group());
    EXPECT_EQ(ADDRESS_HOME_COUNTRY,
              form_structure()->field(18)->Type().GetStorableType());
    EXPECT_EQ(ADDRESS_HOME, form_structure()->field(18)->Type().group());
    base::string16 billing_country = form_structure()->field(11)->value;
    EXPECT_EQ(2U, billing_country.size());
    base::string16 shipping_country = form_structure()->field(18)->value;
    EXPECT_EQ(2U, shipping_country.size());
    EXPECT_FALSE(billing_country.empty());
    EXPECT_FALSE(shipping_country.empty());
    EXPECT_EQ(billing_country, shipping_country);

    EXPECT_EQ(CREDIT_CARD_NAME,
              form_structure()->field(1)->Type().GetStorableType());
    base::string16 cc_name = form_structure()->field(1)->value;
    EXPECT_EQ(NAME_FULL, form_structure()->field(6)->Type().GetStorableType());
    EXPECT_EQ(NAME_BILLING, form_structure()->field(6)->Type().group());
    base::string16 billing_name = form_structure()->field(6)->value;
    EXPECT_EQ(NAME_FULL, form_structure()->field(13)->Type().GetStorableType());
    EXPECT_EQ(NAME, form_structure()->field(13)->Type().group());
    base::string16 shipping_name = form_structure()->field(13)->value;

    EXPECT_FALSE(cc_name.empty());
    EXPECT_FALSE(billing_name.empty());
    EXPECT_FALSE(shipping_name.empty());
    EXPECT_EQ(cc_name, billing_name);
    EXPECT_EQ(cc_name, shipping_name);
  }

  TestAutofillDialogController* controller() { return controller_.get(); }

  const FormStructure* form_structure() { return form_structure_; }

  TestGeneratedCreditCardBubbleController* test_generated_bubble_controller() {
    return test_generated_bubble_controller_;
  }

  const MockNewCreditCardBubbleController* mock_new_card_bubble_controller() {
    return mock_new_card_bubble_controller_.get();
  }

 private:
  void FinishedCallback(AutofillClient::RequestAutocompleteResult result,
                        const base::string16& debug_message,
                        const FormStructure* form_structure) {
    form_structure_ = form_structure;
  }

#if defined(OS_WIN)
  // http://crbug.com/227221
  ui::ScopedOleInitializer ole_initializer_;
#endif

  // The controller owns itself.
  base::WeakPtr<TestAutofillDialogController> controller_;

  // Returned when the dialog closes successfully.
  const FormStructure* form_structure_;

  // Used to monitor if the Autofill credit card bubble is shown. Owned by
  // |web_contents()|.
  TestGeneratedCreditCardBubbleController* test_generated_bubble_controller_;

  // Used to record when new card bubbles would show. Created in |Reset()|.
  scoped_ptr<MockNewCreditCardBubbleController>
      mock_new_card_bubble_controller_;

  scoped_ptr<ScopedTestingLocalState> scoped_local_state_;
};

}  // namespace

TEST_F(AutofillDialogControllerTest, RefuseToShowWithNoAutocompleteAttributes) {
  FormFieldData email_field;
  email_field.name = ASCIIToUTF16("email");
  FormFieldData cc_field;
  cc_field.name = ASCIIToUTF16("cc");
  FormFieldData billing_field;
  billing_field.name = ASCIIToUTF16("billing name");

  FormData form_data;
  form_data.fields.push_back(email_field);
  form_data.fields.push_back(cc_field);
  form_data.fields.push_back(billing_field);

  SetUpControllerWithFormData(form_data);
  EXPECT_FALSE(controller());
}

TEST_F(AutofillDialogControllerTest, RefuseToShowWithNoCcField) {
  FormFieldData shipping_tel;
  shipping_tel.autocomplete_attribute = "shipping tel";

  FormData form_data;
  form_data.fields.push_back(shipping_tel);

  SetUpControllerWithFormData(form_data);
  EXPECT_FALSE(controller());

  // Any cc- field will do.
  FormFieldData cc_field;
  cc_field.autocomplete_attribute = "cc-csc";
  form_data.fields.push_back(cc_field);

  SetUpControllerWithFormData(form_data);
  EXPECT_TRUE(controller());
}

// Ensure the default ValidityMessage has the expected values.
TEST_F(AutofillDialogControllerTest, DefaultValidityMessage) {
  ValidityMessages messages;
  ValidityMessage message = messages.GetMessageOrDefault(UNKNOWN_TYPE);
  EXPECT_FALSE(message.sure);
  EXPECT_TRUE(message.text.empty());
}

// This test makes sure nothing falls over when fields are being validity-
// checked.
TEST_F(AutofillDialogControllerTest, ValidityCheck) {
  for (size_t i = SECTION_MIN; i <= SECTION_MAX; ++i) {
    DialogSection section = static_cast<DialogSection>(i);
    const DetailInputs& shipping_inputs =
        controller()->RequestedFieldsForSection(section);
    for (DetailInputs::const_iterator iter = shipping_inputs.begin();
         iter != shipping_inputs.end(); ++iter) {
      controller()->InputValidityMessage(section, iter->type, base::string16());
    }
  }
}

// Test for phone number validation.
TEST_F(AutofillDialogControllerTest, PhoneNumberValidation) {
  // Construct FieldValueMap from existing data.
  SwitchToAutofill();

  for (size_t i = 0; i < 2; ++i) {
    ServerFieldType phone = i == 0 ? PHONE_HOME_WHOLE_NUMBER :
                                     PHONE_BILLING_WHOLE_NUMBER;
    ServerFieldType address = i == 0 ? ADDRESS_HOME_COUNTRY :
                                       ADDRESS_BILLING_COUNTRY;
    DialogSection section = i == 0 ? SECTION_SHIPPING : SECTION_BILLING;

    FieldValueMap outputs;
    const DetailInputs& inputs =
        controller()->RequestedFieldsForSection(section);
    AutofillProfile full_profile(test::GetVerifiedProfile());
    for (size_t j = 0; j < inputs.size(); ++j) {
      const ServerFieldType type = inputs[j].type;
      outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");
    }

    // Make sure country is United States.
    outputs[address] = ASCIIToUTF16("United States");

    // Existing data should have no errors.
    ValidityMessages messages = controller()->InputsAreValid(section, outputs);
    EXPECT_FALSE(HasAnyError(messages, phone));

    // Input an empty phone number.
    outputs[phone] = base::string16();
    messages = controller()->InputsAreValid(section, outputs);
    EXPECT_TRUE(HasUnsureError(messages, phone));

    // Input an invalid phone number.
    outputs[phone] = ASCIIToUTF16("ABC");
    messages = controller()->InputsAreValid(section, outputs);
    EXPECT_TRUE(messages.HasSureError(phone));

    // Input a local phone number.
    outputs[phone] = ASCIIToUTF16("2155546699");
    messages = controller()->InputsAreValid(section, outputs);
    EXPECT_FALSE(HasAnyError(messages, phone));

    // Input an invalid local phone number.
    outputs[phone] = ASCIIToUTF16("215554669");
    messages = controller()->InputsAreValid(section, outputs);
    EXPECT_TRUE(messages.HasSureError(phone));

    // Input an international phone number.
    outputs[phone] = ASCIIToUTF16("+33 892 70 12 39");
    messages = controller()->InputsAreValid(section, outputs);
    EXPECT_FALSE(HasAnyError(messages, phone));

    // Input an invalid international phone number.
    outputs[phone] = ASCIIToUTF16("+112333 892 70 12 39");
    messages = controller()->InputsAreValid(section, outputs);
    EXPECT_TRUE(messages.HasSureError(phone));

    // Input a valid Canadian number.
    outputs[phone] = ASCIIToUTF16("+1 506 887 1234");
    messages = controller()->InputsAreValid(section, outputs);
    EXPECT_FALSE(HasAnyError(messages, phone));

    // Input a valid Canadian number without the country code.
    outputs[phone] = ASCIIToUTF16("506 887 1234");
    messages = controller()->InputsAreValid(section, outputs);
    EXPECT_TRUE(HasAnyError(messages, phone));

    // Input a valid Canadian toll-free number.
    outputs[phone] = ASCIIToUTF16("310 1234");
    messages = controller()->InputsAreValid(section, outputs);
    EXPECT_TRUE(HasAnyError(messages, phone));
  }
}

TEST_F(AutofillDialogControllerTest, ExpirationDateValidity) {
  ui::ComboboxModel* exp_year_model =
      controller()->ComboboxModelForAutofillType(CREDIT_CARD_EXP_4_DIGIT_YEAR);
  ui::ComboboxModel* exp_month_model =
      controller()->ComboboxModelForAutofillType(CREDIT_CARD_EXP_MONTH);

  base::string16 default_year_value =
      exp_year_model->GetItemAt(exp_year_model->GetDefaultIndex());
  base::string16 default_month_value =
      exp_month_model->GetItemAt(exp_month_model->GetDefaultIndex());

  base::string16 other_year_value =
      exp_year_model->GetItemAt(exp_year_model->GetItemCount() - 1);
  base::string16 other_month_value =
      exp_month_model->GetItemAt(exp_month_model->GetItemCount() - 1);

  FieldValueMap outputs;
  outputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("United States");
  outputs[CREDIT_CARD_EXP_MONTH] = default_month_value;
  outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = default_year_value;

  // Expiration default values generate unsure validation errors (but not sure).
  ValidityMessages messages = controller()->InputsAreValid(SECTION_CC_BILLING,
                                                           outputs);
  EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
  EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH));

  // Expiration date with default month fails.
  outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = other_year_value;
  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
  EXPECT_FALSE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
  EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH));

  // Expiration date with default year fails.
  outputs[CREDIT_CARD_EXP_MONTH] = other_month_value;
  outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = default_year_value;
  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
  EXPECT_TRUE(HasUnsureError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
  EXPECT_FALSE(HasUnsureError(messages, CREDIT_CARD_EXP_MONTH));
}

TEST_F(AutofillDialogControllerTest, BillingNameValidation) {
  // Construct FieldValueMap from AutofillProfile data.
  SwitchToAutofill();

  FieldValueMap outputs;
  outputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("United States");

  // Input an empty billing name.
  outputs[NAME_BILLING_FULL] = base::string16();
  ValidityMessages messages = controller()->InputsAreValid(SECTION_BILLING,
                                                           outputs);
  EXPECT_TRUE(HasUnsureError(messages, NAME_BILLING_FULL));

  // Input a non-empty billing name.
  outputs[NAME_BILLING_FULL] = ASCIIToUTF16("Bob");
  messages = controller()->InputsAreValid(SECTION_BILLING, outputs);
  EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL));

  // Switch to Wallet which only considers names with with at least two names to
  // be valid.
  SwitchToWallet();

  // Setup some wallet state.
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  controller()->OnDidGetWalletItems(wallet_items.Pass());

  // Input an empty billing name. Data source should not change this behavior.
  outputs[NAME_BILLING_FULL] = base::string16();
  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
  EXPECT_TRUE(HasUnsureError(messages, NAME_BILLING_FULL));

  // Input a one name billing name. Wallet does not currently support this.
  outputs[NAME_BILLING_FULL] = ASCIIToUTF16("Bob");
  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
  EXPECT_TRUE(messages.HasSureError(NAME_BILLING_FULL));

  // Input a two name billing name.
  outputs[NAME_BILLING_FULL] = ASCIIToUTF16("Bob Barker");
  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
  EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL));

  // Input a more than two name billing name.
  outputs[NAME_BILLING_FULL] = ASCIIToUTF16("John Jacob Jingleheimer Schmidt"),
  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
  EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL));

  // Input a billing name with lots of crazy whitespace.
  outputs[NAME_BILLING_FULL] =
      ASCIIToUTF16("     \\n\\r John \\n  Jacob Jingleheimer \\t Schmidt  "),
  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
  EXPECT_FALSE(HasAnyError(messages, NAME_BILLING_FULL));
}

TEST_F(AutofillDialogControllerTest, CreditCardNumberValidation) {
  // Construct FieldValueMap from AutofillProfile data.
  SwitchToAutofill();

  // Should accept AMEX, Visa, Master and Discover.
  ValidateCCNumber(SECTION_CC, kTestCCNumberVisa, true);
  ValidateCCNumber(SECTION_CC, kTestCCNumberMaster, true);
  ValidateCCNumber(SECTION_CC, kTestCCNumberDiscover, true);
  ValidateCCNumber(SECTION_CC, kTestCCNumberAmex, true);
  ValidateCCNumber(SECTION_CC, kTestCCNumberIncomplete, false);
  ValidateCCNumber(SECTION_CC, kTestCCNumberInvalid, false);

  // Switch to Wallet which will not accept AMEX.
  SwitchToWallet();

  // Setup some wallet state on a merchant for which Wallet doesn't
  // support AMEX.
  controller()->OnDidGetWalletItems(
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));

  // Should accept Visa, Master and Discover, but not AMEX.
  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberVisa, true);
  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberMaster, true);
  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberDiscover, true);
  EXPECT_EQ(l10n_util::GetStringUTF16(
                IDS_AUTOFILL_CREDIT_CARD_NOT_SUPPORTED_BY_WALLET_FOR_MERCHANT),
            ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberAmex, false));
  EXPECT_EQ(
      l10n_util::GetStringUTF16(
          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_NUMBER),
      ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberIncomplete, false));
  EXPECT_EQ(
      l10n_util::GetStringUTF16(
          IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_CREDIT_CARD_NUMBER),
      ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberInvalid, false));

  // Setup some wallet state on a merchant for which Wallet supports AMEX.
  controller()->OnDidGetWalletItems(
      wallet::GetTestWalletItems(wallet::AMEX_ALLOWED));

  // Should accept Visa, Master, Discover, and AMEX.
  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberVisa, true);
  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberMaster, true);
  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberDiscover, true);
  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberAmex, true);
  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberIncomplete, false);
  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberInvalid, false);
}

TEST_F(AutofillDialogControllerTest, AutofillProfiles) {
  SwitchToAutofill();
  ui::MenuModel* shipping_model =
      controller()->MenuModelForSection(SECTION_SHIPPING);
  // Since the PersonalDataManager is empty, this should only have the
  // "use billing", "add new" and "manage" menu items.
  ASSERT_TRUE(shipping_model);
  EXPECT_EQ(3, shipping_model->GetItemCount());
  // On the other hand, the other models should be NULL when there's no
  // suggestion.
  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_BILLING));

  EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(3);

  // Empty profiles are ignored.
  AutofillProfile empty_profile(base::GenerateGUID(), kSettingsOrigin);
  empty_profile.SetRawInfo(NAME_FULL, ASCIIToUTF16("John Doe"));
  controller()->GetTestingManager()->AddTestingProfile(&empty_profile);
  shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING);
  ASSERT_TRUE(shipping_model);
  EXPECT_EQ(3, shipping_model->GetItemCount());

  // An otherwise full but unverified profile should be ignored.
  AutofillProfile full_profile(test::GetFullProfile());
  full_profile.set_origin("https://www.example.com");
  full_profile.SetRawInfo(ADDRESS_HOME_LINE2, base::string16());
  controller()->GetTestingManager()->AddTestingProfile(&full_profile);
  shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING);
  ASSERT_TRUE(shipping_model);
  EXPECT_EQ(3, shipping_model->GetItemCount());

  // A full, verified profile should be picked up.
  AutofillProfile verified_profile(test::GetVerifiedProfile());
  verified_profile.SetRawInfo(ADDRESS_HOME_LINE2, base::string16());
  controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
  shipping_model = controller()->MenuModelForSection(SECTION_SHIPPING);
  ASSERT_TRUE(shipping_model);
  EXPECT_EQ(4, shipping_model->GetItemCount());
}

// Checks that a valid profile is selected by default, but if invalid is
// popped into edit mode.
TEST_F(AutofillDialogControllerTest, AutofillProfilesPopInvalidIntoEdit) {
  SwitchToAutofill();
  SuggestionsMenuModel* shipping_model =
      GetMenuModelForSection(SECTION_SHIPPING);
  EXPECT_EQ(3, shipping_model->GetItemCount());
  // "Same as billing" is selected.
  EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
  EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));

  AutofillProfile verified_profile(test::GetVerifiedProfile());
  controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
  EXPECT_EQ(4, shipping_model->GetItemCount());
  EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
  EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING));

  // Now make up a problem and make sure the profile isn't in the list.
  Reset();
  SwitchToAutofill();
  FieldProblemMap problems;
  problems.insert(std::make_pair(::i18n::addressinput::POSTAL_CODE,
                                 ::i18n::addressinput::MISMATCHING_VALUE));
  EXPECT_CALL(*controller()->GetMockValidator(),
              ValidateAddress(CountryCodeMatcher("US"), _, _)).
      WillRepeatedly(DoAll(SetArgPointee<2>(problems),
                           Return(AddressValidator::SUCCESS)));

  controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
  shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
  EXPECT_EQ(4, shipping_model->GetItemCount());
  EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
  EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
}

// Makes sure suggestion profiles are re-validated when validation rules load.
TEST_F(AutofillDialogControllerTest, AutofillProfilesRevalidateAfterRulesLoad) {
  SwitchToAutofill();
  SuggestionsMenuModel* shipping_model =
      GetMenuModelForSection(SECTION_SHIPPING);
  EXPECT_EQ(3, shipping_model->GetItemCount());
  // "Same as billing" is selected.
  EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
  EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
  AutofillProfile verified_profile(test::GetVerifiedProfile());
  controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
  EXPECT_EQ(4, shipping_model->GetItemCount());
  EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
  EXPECT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING));

  FieldProblemMap problems;
  problems.insert(std::make_pair(::i18n::addressinput::POSTAL_CODE,
                                 ::i18n::addressinput::MISMATCHING_VALUE));
  EXPECT_CALL(*controller()->GetMockValidator(),
              ValidateAddress(CountryCodeMatcher("US"), _, _)).
      WillRepeatedly(DoAll(SetArgPointee<2>(problems),
                           Return(AddressValidator::SUCCESS)));

  controller()->OnAddressValidationRulesLoaded("US", true);
  EXPECT_EQ(4, shipping_model->GetItemCount());
  EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_SHIPPING));
  EXPECT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));
}

// Makes sure that the choice of which Autofill profile to use for each section
// is sticky.
TEST_F(AutofillDialogControllerTest, AutofillProfileDefaults) {
  SwitchToAutofill();
  AutofillProfile profile(test::GetVerifiedProfile());
  AutofillProfile profile2(test::GetVerifiedProfile2());
  controller()->GetTestingManager()->AddTestingProfile(&profile);
  controller()->GetTestingManager()->AddTestingProfile(&profile2);

  // Until a selection has been made, the default shipping suggestion is the
  // first one (after "use billing").
  SuggestionsMenuModel* shipping_model =
      GetMenuModelForSection(SECTION_SHIPPING);
  EXPECT_EQ(1, shipping_model->checked_item());

  for (int i = 2; i >= 0; --i) {
    shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
    shipping_model->ExecuteCommand(i, 0);
    FillCreditCardInputs();
    controller()->OnAccept();

    Reset();
    controller()->GetTestingManager()->AddTestingProfile(&profile);
    controller()->GetTestingManager()->AddTestingProfile(&profile2);
    shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
    EXPECT_EQ(i, shipping_model->checked_item());
  }

  // Try again, but don't add the default profile to the PDM. The dialog
  // should fall back to the first profile.
  shipping_model->ExecuteCommand(2, 0);
  FillCreditCardInputs();
  controller()->OnAccept();
  Reset();
  controller()->GetTestingManager()->AddTestingProfile(&profile);
  shipping_model = GetMenuModelForSection(SECTION_SHIPPING);
  EXPECT_EQ(1, shipping_model->checked_item());
}

// Makes sure that a newly added Autofill profile becomes set as the default
// choice for the next run.
TEST_F(AutofillDialogControllerTest, NewAutofillProfileIsDefault) {
  SwitchToAutofill();

  AutofillProfile profile(test::GetVerifiedProfile());
  CreditCard credit_card(test::GetVerifiedCreditCard());
  controller()->GetTestingManager()->AddTestingProfile(&profile);
  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);

  // Until a selection has been made, the default suggestion is the first one.
  // For the shipping section, this follows the "use billing" suggestion.
  EXPECT_EQ(0, GetMenuModelForSection(SECTION_CC)->checked_item());
  EXPECT_EQ(1, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());

  // Fill in the shipping and credit card sections with new data.
  AutofillProfile new_profile(test::GetVerifiedProfile2());
  CreditCard new_credit_card(test::GetVerifiedCreditCard2());
  FillInputs(SECTION_SHIPPING, new_profile);
  FillInputs(SECTION_CC, new_credit_card);
  controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
  controller()->OnAccept();

  // Update the |new_profile| and |new_credit_card|'s guids to the saved ones.
  new_profile.set_guid(
      controller()->GetTestingManager()->imported_profile().guid());
  new_credit_card.set_guid(
      controller()->GetTestingManager()->imported_credit_card().guid());

  // Reload the dialog. The newly added address and credit card should now be
  // set as the defaults.
  Reset();
  controller()->GetTestingManager()->AddTestingProfile(&profile);
  controller()->GetTestingManager()->AddTestingProfile(&new_profile);
  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
  controller()->GetTestingManager()->AddTestingCreditCard(&new_credit_card);

  // Until a selection has been made, the default suggestion is the first one.
  // For the shipping section, this follows the "use billing" suggestion.
  EXPECT_EQ(1, GetMenuModelForSection(SECTION_CC)->checked_item());
  EXPECT_EQ(2, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
}

TEST_F(AutofillDialogControllerTest, SuggestValidEmail) {
  SwitchToAutofill();
  AutofillProfile profile(test::GetVerifiedProfile());
  const base::string16 kValidEmail = ASCIIToUTF16(kFakeEmail);
  profile.SetRawInfo(EMAIL_ADDRESS, kValidEmail);
  controller()->GetTestingManager()->AddTestingProfile(&profile);

  // "add", "manage", and 1 suggestion.
  EXPECT_EQ(
      3, controller()->MenuModelForSection(SECTION_BILLING)->GetItemCount());
  // "add", "manage", 1 suggestion, and "same as billing".
  EXPECT_EQ(
      4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
}

TEST_F(AutofillDialogControllerTest, DoNotSuggestInvalidEmail) {
  SwitchToAutofill();
  AutofillProfile profile(test::GetVerifiedProfile());
  profile.SetRawInfo(EMAIL_ADDRESS, ASCIIToUTF16(".!#$%&'*+/=?^_`-@-.."));
  controller()->GetTestingManager()->AddTestingProfile(&profile);

  EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING));
  // "add", "manage", 1 suggestion, and "same as billing".
  EXPECT_EQ(
      4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
}

TEST_F(AutofillDialogControllerTest, SuggestValidAddress) {
  SwitchToAutofill();
  AutofillProfile full_profile(test::GetVerifiedProfile());
  full_profile.set_origin(kSettingsOrigin);
  controller()->GetTestingManager()->AddTestingProfile(&full_profile);
  // "add", "manage", and 1 suggestion.
  EXPECT_EQ(
      3, controller()->MenuModelForSection(SECTION_BILLING)->GetItemCount());
}

TEST_F(AutofillDialogControllerTest, DoNotSuggestInvalidAddress) {
  SwitchToAutofill();
  AutofillProfile full_profile(test::GetVerifiedProfile());
  full_profile.set_origin(kSettingsOrigin);
  full_profile.SetRawInfo(ADDRESS_HOME_STATE, ASCIIToUTF16("C"));
  controller()->GetTestingManager()->AddTestingProfile(&full_profile);
}

TEST_F(AutofillDialogControllerTest, DoNotSuggestIncompleteAddress) {
  SwitchToAutofill();
  AutofillProfile profile(test::GetVerifiedProfile());
  profile.SetRawInfo(ADDRESS_HOME_STATE, base::string16());
  controller()->GetTestingManager()->AddTestingProfile(&profile);

  // Same as shipping, manage, add new.
  EXPECT_EQ(3,
      controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
  EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING));
}

TEST_F(AutofillDialogControllerTest, DoSuggestShippingAddressWithoutEmail) {
  SwitchToAutofill();
  AutofillProfile profile(test::GetVerifiedProfile());
  profile.SetRawInfo(EMAIL_ADDRESS, base::string16());
  controller()->GetTestingManager()->AddTestingProfile(&profile);

  // Same as shipping, manage, add new, profile with missing email.
  EXPECT_EQ(4,
      controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
  // Billing addresses require email.
  EXPECT_FALSE(!!controller()->MenuModelForSection(SECTION_BILLING));
}

TEST_F(AutofillDialogControllerTest, AutofillCreditCards) {
  SwitchToAutofill();
  // Since the PersonalDataManager is empty, this should only have the
  // default menu items.
  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));

  EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(3);

  // Empty cards are ignored.
  CreditCard empty_card(base::GenerateGUID(), kSettingsOrigin);
  empty_card.SetRawInfo(CREDIT_CARD_NAME, ASCIIToUTF16("John Doe"));
  controller()->GetTestingManager()->AddTestingCreditCard(&empty_card);
  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));

  // An otherwise full but unverified card should be ignored.
  CreditCard full_card(test::GetCreditCard());
  full_card.set_origin("https://www.example.com");
  controller()->GetTestingManager()->AddTestingCreditCard(&full_card);
  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));

  // A full, verified card should be picked up.
  CreditCard verified_card(test::GetCreditCard());
  verified_card.set_origin(kSettingsOrigin);
  controller()->GetTestingManager()->AddTestingCreditCard(&verified_card);
  ui::MenuModel* credit_card_model =
      controller()->MenuModelForSection(SECTION_CC);
  ASSERT_TRUE(credit_card_model);
  EXPECT_EQ(3, credit_card_model->GetItemCount());
}

// Test selecting a shipping address different from billing as address.
TEST_F(AutofillDialogControllerTest, DontUseBillingAsShipping) {
  SwitchToAutofill();
  AutofillProfile full_profile(test::GetVerifiedProfile());
  AutofillProfile full_profile2(test::GetVerifiedProfile2());
  CreditCard credit_card(test::GetVerifiedCreditCard());
  controller()->GetTestingManager()->AddTestingProfile(&full_profile);
  controller()->GetTestingManager()->AddTestingProfile(&full_profile2);
  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
  ui::MenuModel* shipping_model =
      controller()->MenuModelForSection(SECTION_SHIPPING);
  shipping_model->ActivatedAt(2);

  controller()->OnAccept();
  ASSERT_EQ(20U, form_structure()->field_count());
  EXPECT_EQ(ADDRESS_HOME_STATE,
            form_structure()->field(9)->Type().GetStorableType());
  EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(9)->Type().group());
  EXPECT_EQ(ADDRESS_HOME_STATE,
            form_structure()->field(16)->Type().GetStorableType());
  EXPECT_EQ(ADDRESS_HOME, form_structure()->field(16)->Type().group());
  base::string16 billing_state = form_structure()->field(9)->value;
  base::string16 shipping_state = form_structure()->field(16)->value;
  EXPECT_FALSE(billing_state.empty());
  EXPECT_FALSE(shipping_state.empty());
  EXPECT_NE(billing_state, shipping_state);

  EXPECT_EQ(CREDIT_CARD_NAME,
            form_structure()->field(1)->Type().GetStorableType());
  base::string16 cc_name = form_structure()->field(1)->value;
  EXPECT_EQ(NAME_FULL, form_structure()->field(6)->Type().GetStorableType());
  EXPECT_EQ(NAME_BILLING, form_structure()->field(6)->Type().group());
  base::string16 billing_name = form_structure()->field(6)->value;
  EXPECT_EQ(NAME_FULL, form_structure()->field(13)->Type().GetStorableType());
  EXPECT_EQ(NAME, form_structure()->field(13)->Type().group());
  base::string16 shipping_name = form_structure()->field(13)->value;

  EXPECT_FALSE(cc_name.empty());
  EXPECT_FALSE(billing_name.empty());
  EXPECT_FALSE(shipping_name.empty());
  // Billing name should always be the same as cardholder name.
  EXPECT_EQ(cc_name, billing_name);
  EXPECT_NE(cc_name, shipping_name);
}

// Test selecting UseBillingForShipping.
TEST_F(AutofillDialogControllerTest, UseBillingAsShipping) {
  SwitchToAutofill();

  AutofillProfile full_profile(test::GetVerifiedProfile());
  controller()->GetTestingManager()->AddTestingProfile(&full_profile);

  AutofillProfile full_profile2(test::GetVerifiedProfile2());
  controller()->GetTestingManager()->AddTestingProfile(&full_profile2);

  CreditCard credit_card(test::GetVerifiedCreditCard());
  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);

  ASSERT_FALSE(controller()->IsManuallyEditingSection(SECTION_CC));
  ASSERT_FALSE(controller()->IsManuallyEditingSection(SECTION_BILLING));

  SubmitAndVerifyShippingAndBillingResults();
}

TEST_F(AutofillDialogControllerTest, UseBillingAsShippingManualInput) {
  SwitchToAutofill();

  ASSERT_TRUE(controller()->IsManuallyEditingSection(SECTION_CC));
  ASSERT_TRUE(controller()->IsManuallyEditingSection(SECTION_BILLING));

  CreditCard credit_card(test::GetVerifiedCreditCard());
  FillInputs(SECTION_CC, credit_card);

  AutofillProfile full_profile(test::GetVerifiedProfile());
  FillInputs(SECTION_BILLING, full_profile);

  SubmitAndVerifyShippingAndBillingResults();
}

// Tests that shipping and billing telephone fields are supported, and filled
// in by their respective profiles. http://crbug.com/244515
TEST_F(AutofillDialogControllerTest, BillingVsShippingPhoneNumber) {
  FormFieldData shipping_tel;
  shipping_tel.autocomplete_attribute = "shipping tel";
  FormFieldData billing_tel;
  billing_tel.autocomplete_attribute = "billing tel";
  FormFieldData cc_field;
  cc_field.autocomplete_attribute = "cc-csc";

  FormData form_data;
  form_data.fields.push_back(shipping_tel);
  form_data.fields.push_back(billing_tel);
  form_data.fields.push_back(cc_field);
  SetUpControllerWithFormData(form_data);

  SwitchToAutofill();

  // The profile that will be chosen for the shipping section.
  AutofillProfile shipping_profile(test::GetVerifiedProfile());
  // The profile that will be chosen for the billing section.
  AutofillProfile billing_profile(test::GetVerifiedProfile2());
  CreditCard credit_card(test::GetVerifiedCreditCard());
  controller()->GetTestingManager()->AddTestingProfile(&shipping_profile);
  controller()->GetTestingManager()->AddTestingProfile(&billing_profile);
  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
  ui::MenuModel* billing_model =
      controller()->MenuModelForSection(SECTION_BILLING);
  billing_model->ActivatedAt(1);

  controller()->OnAccept();
  ASSERT_EQ(3U, form_structure()->field_count());
  EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
            form_structure()->field(0)->Type().GetStorableType());
  EXPECT_EQ(PHONE_HOME, form_structure()->field(0)->Type().group());
  EXPECT_EQ(PHONE_HOME_WHOLE_NUMBER,
            form_structure()->field(1)->Type().GetStorableType());
  EXPECT_EQ(PHONE_BILLING, form_structure()->field(1)->Type().group());
  EXPECT_EQ(shipping_profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER),
            form_structure()->field(0)->value);
  EXPECT_EQ(billing_profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER),
            form_structure()->field(1)->value);
  EXPECT_NE(form_structure()->field(1)->value,
            form_structure()->field(0)->value);
}

// Similar to the above, but tests that street-address (i.e. all lines of the
// street address) is successfully filled for both shipping and billing
// sections.
TEST_F(AutofillDialogControllerTest, BillingVsShippingStreetAddress) {
  FormFieldData shipping_address;
  shipping_address.autocomplete_attribute = "shipping street-address";
  FormFieldData billing_address;
  billing_address.autocomplete_attribute = "billing street-address";
  FormFieldData shipping_address_textarea;
  shipping_address_textarea.autocomplete_attribute = "shipping street-address";
  shipping_address_textarea.form_control_type = "textarea";
  FormFieldData billing_address_textarea;
  billing_address_textarea.autocomplete_attribute = "billing street-address";
  billing_address_textarea.form_control_type = "textarea";
  FormFieldData cc_field;
  cc_field.autocomplete_attribute = "cc-csc";

  FormData form_data;
  form_data.fields.push_back(shipping_address);
  form_data.fields.push_back(billing_address);
  form_data.fields.push_back(shipping_address_textarea);
  form_data.fields.push_back(billing_address_textarea);
  form_data.fields.push_back(cc_field);
  SetUpControllerWithFormData(form_data);

  SwitchToAutofill();

  // The profile that will be chosen for the shipping section.
  AutofillProfile shipping_profile(test::GetVerifiedProfile());
  // The profile that will be chosen for the billing section.
  AutofillProfile billing_profile(test::GetVerifiedProfile2());
  CreditCard credit_card(test::GetVerifiedCreditCard());
  controller()->GetTestingManager()->AddTestingProfile(&shipping_profile);
  controller()->GetTestingManager()->AddTestingProfile(&billing_profile);
  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);
  ui::MenuModel* billing_model =
      controller()->MenuModelForSection(SECTION_BILLING);
  billing_model->ActivatedAt(1);

  controller()->OnAccept();
  ASSERT_EQ(5U, form_structure()->field_count());
  EXPECT_EQ(ADDRESS_HOME_STREET_ADDRESS,
            form_structure()->field(0)->Type().GetStorableType());
  EXPECT_EQ(ADDRESS_HOME, form_structure()->field(0)->Type().group());
  EXPECT_EQ(ADDRESS_HOME_STREET_ADDRESS,
            form_structure()->field(1)->Type().GetStorableType());
  EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(1)->Type().group());
  // Inexact matching; single-line inputs get the address data concatenated but
  // separated by commas.
  EXPECT_TRUE(base::StartsWith(form_structure()->field(0)->value,
                               shipping_profile.GetRawInfo(ADDRESS_HOME_LINE1),
                               base::CompareCase::SENSITIVE));
  EXPECT_TRUE(base::EndsWith(form_structure()->field(0)->value,
                             shipping_profile.GetRawInfo(ADDRESS_HOME_LINE2),
                             base::CompareCase::SENSITIVE));
  EXPECT_TRUE(base::StartsWith(form_structure()->field(1)->value,
                               billing_profile.GetRawInfo(ADDRESS_HOME_LINE1),
                               base::CompareCase::SENSITIVE));
  EXPECT_TRUE(base::EndsWith(form_structure()->field(1)->value,
                             billing_profile.GetRawInfo(ADDRESS_HOME_LINE2),
                             base::CompareCase::SENSITIVE));
  // The textareas should be an exact match.
  EXPECT_EQ(shipping_profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS),
            form_structure()->field(2)->value);
  EXPECT_EQ(billing_profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS),
            form_structure()->field(3)->value);

  EXPECT_NE(form_structure()->field(1)->value,
            form_structure()->field(0)->value);
  EXPECT_NE(form_structure()->field(3)->value,
            form_structure()->field(2)->value);
}

// Test asking for different pieces of the name.
TEST_F(AutofillDialogControllerTest, NamePieces) {
  const char* const attributes[] = {
      "shipping name",
      "billing name",
      "billing given-name",
      "billing family-name",
      "billing additional-name",
      "cc-csc"
  };

  FormData form_data;
  for (size_t i = 0; i < arraysize(attributes); ++i) {
    FormFieldData field;
    field.autocomplete_attribute.assign(attributes[i]);
    form_data.fields.push_back(field);
  }

  SetUpControllerWithFormData(form_data);
  SwitchToAutofill();

  // Billing.
  AutofillProfile test_profile(test::GetVerifiedProfile());
  test_profile.SetInfo(AutofillType(NAME_FULL),
                       ASCIIToUTF16("Fabian Jackson von Nacho"),
                       "en-US");
  controller()->GetTestingManager()->AddTestingProfile(&test_profile);

  // Credit card.
  CreditCard credit_card(test::GetVerifiedCreditCard());
  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);

  // Make shipping name different from billing.
  AutofillProfile test_profile2(test::GetVerifiedProfile2());
  test_profile2.SetInfo(AutofillType(NAME_FULL),
                        ASCIIToUTF16("Don Ford"),
                        "en-US");
  controller()->GetTestingManager()->AddTestingProfile(&test_profile2);
  ui::MenuModel* shipping_model =
      controller()->MenuModelForSection(SECTION_SHIPPING);
  shipping_model->ActivatedAt(2);

  controller()->OnAccept();

  EXPECT_EQ(NAME_FULL, form_structure()->field(0)->Type().GetStorableType());
  EXPECT_EQ(ASCIIToUTF16("Don Ford"),
            form_structure()->field(0)->value);

  EXPECT_EQ(NAME_FULL, form_structure()->field(1)->Type().GetStorableType());
  EXPECT_EQ(ASCIIToUTF16("Fabian Jackson von Nacho"),
            form_structure()->field(1)->value);

  EXPECT_EQ(NAME_FIRST, form_structure()->field(2)->Type().GetStorableType());
  EXPECT_EQ(ASCIIToUTF16("Fabian"),
            form_structure()->field(2)->value);

  EXPECT_EQ(NAME_LAST, form_structure()->field(3)->Type().GetStorableType());
  EXPECT_EQ(ASCIIToUTF16("von Nacho"),
            form_structure()->field(3)->value);

  EXPECT_EQ(NAME_MIDDLE, form_structure()->field(4)->Type().GetStorableType());
  EXPECT_EQ(ASCIIToUTF16("Jackson"),
            form_structure()->field(4)->value);
}

TEST_F(AutofillDialogControllerTest, AcceptLegalDocuments) {
  for (size_t i = 0; i < 2; ++i) {
    SCOPED_TRACE(testing::Message() << "Case " << i);

    EXPECT_CALL(*controller()->GetTestingWalletClient(),
                AcceptLegalDocuments(_, _));
    EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
    EXPECT_CALL(*controller(), LoadRiskFingerprintData());

    EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
    controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
    EXPECT_TRUE(controller()->LegalDocumentLinks().empty());

    scoped_ptr<wallet::WalletItems> wallet_items =
        CompleteAndValidWalletItems();
    wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
    wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
    controller()->OnDidGetWalletItems(wallet_items.Pass());
    EXPECT_FALSE(controller()->LegalDocumentLinks().empty());

    controller()->OnAccept();
    controller()->OnDidAcceptLegalDocuments();
    controller()->OnDidLoadRiskFingerprintData("a");

    // Now try it all over again with the location disclosure already accepted.
    // Nothing should change.
    Reset();
    base::ListValue preexisting_list;
    preexisting_list.AppendString(kFakeEmail);
    g_browser_process->local_state()->Set(
        ::prefs::kAutofillDialogWalletLocationAcceptance,
        preexisting_list);
  }
}

TEST_F(AutofillDialogControllerTest, RejectLegalDocuments) {
  for (size_t i = 0; i < 2; ++i) {
    SCOPED_TRACE(testing::Message() << "Case " << i);

    EXPECT_CALL(*controller()->GetTestingWalletClient(),
                AcceptLegalDocuments(_, _)).Times(0);

    scoped_ptr<wallet::WalletItems> wallet_items =
        CompleteAndValidWalletItems();
    wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
    wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
    controller()->OnDidGetWalletItems(wallet_items.Pass());
    EXPECT_FALSE(controller()->LegalDocumentLinks().empty());

    controller()->OnCancel();

    // Now try it all over again with the location disclosure already accepted.
    // Nothing should change.
    Reset();
    base::ListValue preexisting_list;
    preexisting_list.AppendString(kFakeEmail);
    g_browser_process->local_state()->Set(
        ::prefs::kAutofillDialogWalletLocationAcceptance,
        preexisting_list);
  }
}

TEST_F(AutofillDialogControllerTest, AcceptLocationDisclosure) {
  // Check that accepting the dialog registers the user's name in the list
  // of users who have accepted the geolocation terms.
  EXPECT_TRUE(g_browser_process->local_state()->GetList(
      ::prefs::kAutofillDialogWalletLocationAcceptance)->empty());

  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
  EXPECT_FALSE(controller()->LegalDocumentsText().empty());
  EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
  controller()->OnAccept();

  const base::ListValue* list = g_browser_process->local_state()->GetList(
      ::prefs::kAutofillDialogWalletLocationAcceptance);
  ASSERT_EQ(1U, list->GetSize());
  std::string accepted_username;
  EXPECT_TRUE(list->GetString(0, &accepted_username));
  EXPECT_EQ(kFakeEmail, accepted_username);

  // Now check it still works if that list starts off with some other username
  // in it.
  Reset();
  list = g_browser_process->local_state()->GetList(
      ::prefs::kAutofillDialogWalletLocationAcceptance);
  ASSERT_TRUE(list->empty());

  std::string kOtherUsername("spouse@example.com");
  base::ListValue preexisting_list;
  preexisting_list.AppendString(kOtherUsername);
  g_browser_process->local_state()->Set(
      ::prefs::kAutofillDialogWalletLocationAcceptance,
      preexisting_list);

  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
  EXPECT_FALSE(controller()->LegalDocumentsText().empty());
  EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
  controller()->OnAccept();

  list = g_browser_process->local_state()->GetList(
      ::prefs::kAutofillDialogWalletLocationAcceptance);
  ASSERT_EQ(2U, list->GetSize());
  EXPECT_NE(list->end(), list->Find(base::StringValue(kFakeEmail)));
  EXPECT_NE(list->end(), list->Find(base::StringValue(kOtherUsername)));

  // Now check the list doesn't change if the user cancels out of the dialog.
  Reset();
  list = g_browser_process->local_state()->GetList(
      ::prefs::kAutofillDialogWalletLocationAcceptance);
  ASSERT_TRUE(list->empty());

  g_browser_process->local_state()->Set(
      ::prefs::kAutofillDialogWalletLocationAcceptance,
      preexisting_list);

  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
  EXPECT_FALSE(controller()->LegalDocumentsText().empty());
  EXPECT_TRUE(controller()->LegalDocumentLinks().empty());
  controller()->OnCancel();

  list = g_browser_process->local_state()->GetList(
      ::prefs::kAutofillDialogWalletLocationAcceptance);
  ASSERT_EQ(1U, list->GetSize());
  EXPECT_NE(list->end(), list->Find(base::StringValue(kOtherUsername)));
  EXPECT_EQ(list->end(), list->Find(base::StringValue(kFakeEmail)));
}

TEST_F(AutofillDialogControllerTest, LegalDocumentOverflow) {
  for (size_t number_of_docs = 2; number_of_docs < 11; ++number_of_docs) {
    scoped_ptr<wallet::WalletItems> wallet_items =
        CompleteAndValidWalletItems();
    for (size_t i = 0; i < number_of_docs; ++i)
      wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());

    Reset();
    controller()->OnDidGetWalletItems(wallet_items.Pass());

    // The dialog is only equipped to handle 2-6 legal documents. More than
    // 6 errors out.
    if (number_of_docs <= 6U) {
      EXPECT_FALSE(controller()->LegalDocumentsText().empty());
    } else {
      EXPECT_TRUE(controller()->LegalDocumentsText().empty());
      EXPECT_EQ(1U, NotificationsOfType(
          DialogNotification::WALLET_ERROR).size());
    }
  }

  controller()->OnCancel();
}

// Makes sure the default object IDs are respected.
TEST_F(AutofillDialogControllerTest, WalletDefaultItems) {
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());

  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
  wallet_items->AddAddress(wallet::GetTestShippingAddress());
  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());

  controller()->OnDidGetWalletItems(wallet_items.Pass());
  // "add", "manage", and 4 suggestions.
  EXPECT_EQ(6,
      controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
      IsItemCheckedAt(2));
  ASSERT_FALSE(controller()->IsEditingExistingData(SECTION_CC_BILLING));
  // "use billing", "add", "manage", and 5 suggestions.
  EXPECT_EQ(8,
      controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_SHIPPING)->
      IsItemCheckedAt(4));
  ASSERT_FALSE(controller()->IsEditingExistingData(SECTION_SHIPPING));
}

// Tests that invalid and AMEX default instruments are ignored.
TEST_F(AutofillDialogControllerTest, SelectInstrument) {
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  // Tests if default instrument is invalid, then, the first valid instrument is
  // selected instead of the default instrument.
  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid());
  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());

  controller()->OnDidGetWalletItems(wallet_items.Pass());
  // 4 suggestions and "add", "manage".
  EXPECT_EQ(6,
      controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
      IsItemCheckedAt(0));

  // Tests if default instrument is AMEX but Wallet doesn't support
  // AMEX on this merchant, then the first valid instrument is
  // selected instead of the default instrument.
  wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
  wallet_items->AddInstrument(
      wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_DISALLOWED));
  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());

  controller()->OnDidGetWalletItems(wallet_items.Pass());
  // 4 suggestions and "add", "manage".
  EXPECT_EQ(6,
      controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
      IsItemCheckedAt(0));

  // Tests if default instrument is AMEX and it is allowed on this merchant,
  // then it is selected.
  wallet_items = wallet::GetTestWalletItems(wallet::AMEX_ALLOWED);
  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
  wallet_items->AddInstrument(
      wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_ALLOWED));
  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());

  controller()->OnDidGetWalletItems(wallet_items.Pass());
  // 4 suggestions and "add", "manage".
  EXPECT_EQ(6,
      controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
      IsItemCheckedAt(2));

  // Tests if only have AMEX and invalid instrument, then "add" is selected.
  wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid());
  wallet_items->AddInstrument(
      wallet::GetTestMaskedInstrumentAmex(wallet::AMEX_DISALLOWED));

  controller()->OnDidGetWalletItems(wallet_items.Pass());
  // 2 suggestions and "add", "manage".
  EXPECT_EQ(4,
      controller()->MenuModelForSection(SECTION_CC_BILLING)->GetItemCount());
  // "add"
  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING)->
      IsItemCheckedAt(2));
}

TEST_F(AutofillDialogControllerTest, SaveAddress) {
  EXPECT_CALL(*controller()->GetView(), ModelChanged());
  EXPECT_CALL(*controller()->GetTestingWalletClient(),
              SaveToWalletMock(testing::IsNull(),
                               testing::NotNull(),
                               testing::IsNull(),
                               testing::IsNull()));

  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
  controller()->OnDidGetWalletItems(wallet_items.Pass());
  // If there is no shipping address in wallet, it will default to
  // "same-as-billing" instead of "add-new-item". "same-as-billing" is covered
  // by the following tests. The penultimate item in the menu is "add-new-item".
  ui::MenuModel* shipping_model =
      controller()->MenuModelForSection(SECTION_SHIPPING);
  shipping_model->ActivatedAt(shipping_model->GetItemCount() - 2);

  AutofillProfile test_profile(test::GetVerifiedProfile());
  FillInputs(SECTION_SHIPPING, test_profile);

  AcceptAndLoadFakeFingerprint();
}

TEST_F(AutofillDialogControllerTest, SaveInstrument) {
  EXPECT_CALL(*controller()->GetView(), ModelChanged());
  EXPECT_CALL(*controller()->GetTestingWalletClient(),
              SaveToWalletMock(testing::NotNull(),
                               testing::IsNull(),
                               testing::IsNull(),
                               testing::IsNull()));

  FillCCBillingInputs();
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddAddress(wallet::GetTestShippingAddress());
  SubmitWithWalletItems(wallet_items.Pass());
}

TEST_F(AutofillDialogControllerTest, SaveInstrumentWithInvalidInstruments) {
  EXPECT_CALL(*controller()->GetView(), ModelChanged());
  EXPECT_CALL(*controller()->GetTestingWalletClient(),
              SaveToWalletMock(testing::NotNull(),
                               testing::IsNull(),
                               testing::IsNull(),
                               testing::IsNull()));

  FillCCBillingInputs();
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddAddress(wallet::GetTestShippingAddress());
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentInvalid());
  SubmitWithWalletItems(wallet_items.Pass());
}

TEST_F(AutofillDialogControllerTest, SaveInstrumentAndAddress) {
  EXPECT_CALL(*controller()->GetTestingWalletClient(),
              SaveToWalletMock(testing::NotNull(),
                               testing::NotNull(),
                               testing::IsNull(),
                               testing::IsNull()));

  FillCCBillingInputs();
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  SubmitWithWalletItems(wallet_items.Pass());
}

MATCHER(IsUpdatingExistingData, "updating existing Wallet data") {
  return !arg->object_id().empty();
}

MATCHER(UsesLocalBillingAddress, "uses the local billing address") {
  return arg->street_address()[0] == ASCIIToUTF16(kEditedBillingAddress);
}

// Tests that when using billing address for shipping, and there is no exact
// matched shipping address, then a shipping address should be added.
TEST_F(AutofillDialogControllerTest, BillingForShipping) {
  EXPECT_CALL(*controller()->GetTestingWalletClient(),
              SaveToWalletMock(testing::IsNull(),
                               testing::NotNull(),
                               testing::IsNull(),
                               testing::IsNull()));

  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
  // Select "Same as billing" in the address menu.
  UseBillingForShipping();

  AcceptAndLoadFakeFingerprint();
}

// Tests that when using billing address for shipping, and there is an exact
// matched shipping address, then a shipping address should not be added.
TEST_F(AutofillDialogControllerTest, BillingForShippingHasMatch) {
  EXPECT_CALL(*controller()->GetTestingWalletClient(),
              SaveToWalletMock(_, _, _, _)).Times(0);

  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  scoped_ptr<wallet::WalletItems::MaskedInstrument> instrument =
      wallet::GetTestMaskedInstrument();
  // Copy billing address as shipping address, and assign an id to it.
  scoped_ptr<wallet::Address> shipping_address(
      new wallet::Address(instrument->address()));
  shipping_address->set_object_id("shipping_address_id");
  wallet_items->AddAddress(shipping_address.Pass());
  wallet_items->AddInstrument(instrument.Pass());
  wallet_items->AddAddress(wallet::GetTestShippingAddress());

  controller()->OnDidGetWalletItems(wallet_items.Pass());
  // Select "Same as billing" in the address menu.
  UseBillingForShipping();

  AcceptAndLoadFakeFingerprint();
}

// Test that the local view contents is used when saving a new instrument and
// the user has selected "Same as billing".
TEST_F(AutofillDialogControllerTest, SaveInstrumentSameAsBilling) {
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
  controller()->OnDidGetWalletItems(wallet_items.Pass());

  ui::MenuModel* model = controller()->MenuModelForSection(SECTION_CC_BILLING);
  model->ActivatedAt(model->GetItemCount() - 2);

  FieldValueMap outputs;
  const DetailInputs& inputs =
      controller()->RequestedFieldsForSection(SECTION_CC_BILLING);
  AutofillProfile full_profile(test::GetVerifiedProfile());
  CreditCard full_card(test::GetCreditCard());
  for (size_t i = 0; i < inputs.size(); ++i) {
    const ServerFieldType type = inputs[i].type;
    if (type == ADDRESS_BILLING_STREET_ADDRESS)
      outputs[type] = ASCIIToUTF16(kEditedBillingAddress);
    else
      outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");

    if (outputs[type].empty())
      outputs[type] = full_card.GetInfo(AutofillType(type), "en-US");
  }
  controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);

  controller()->OnAccept();

  EXPECT_CALL(*controller()->GetTestingWalletClient(),
              SaveToWalletMock(testing::NotNull(),
                               UsesLocalBillingAddress(),
                               testing::IsNull(),
                               testing::IsNull()));
  AcceptAndLoadFakeFingerprint();
}

TEST_F(AutofillDialogControllerTest, CancelNoSave) {
  EXPECT_CALL(*controller()->GetTestingWalletClient(),
              SaveToWalletMock(_, _, _, _)).Times(0);

  EXPECT_CALL(*controller()->GetView(), ModelChanged());

  controller()->OnDidGetWalletItems(
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));
  controller()->OnCancel();
}

// Checks that clicking the Manage menu item opens a new tab with a different
// URL for Wallet and Autofill.
TEST_F(AutofillDialogControllerTest, ManageItem) {
  AutofillProfile full_profile(test::GetVerifiedProfile());
  full_profile.set_origin(kSettingsOrigin);
  full_profile.SetRawInfo(ADDRESS_HOME_LINE2, base::string16());
  controller()->GetTestingManager()->AddTestingProfile(&full_profile);
  SwitchToAutofill();

  SuggestionsMenuModel* shipping = GetMenuModelForSection(SECTION_SHIPPING);
  shipping->ExecuteCommand(shipping->GetItemCount() - 1, 0);
  GURL autofill_manage_url = controller()->open_tab_url();
  EXPECT_EQ("chrome", autofill_manage_url.scheme());

  SwitchToWallet();
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
  controller()->OnDidGetWalletItems(wallet_items.Pass());

  controller()->SuggestionItemSelected(shipping, shipping->GetItemCount() - 1);
  GURL wallet_manage_addresses_url = controller()->open_tab_url();
  EXPECT_EQ("https", wallet_manage_addresses_url.scheme());

  SuggestionsMenuModel* billing = GetMenuModelForSection(SECTION_CC_BILLING);
  controller()->SuggestionItemSelected(billing, billing->GetItemCount() - 1);
  GURL wallet_manage_instruments_url = controller()->open_tab_url();
  EXPECT_EQ("https", wallet_manage_instruments_url.scheme());

  EXPECT_NE(autofill_manage_url, wallet_manage_instruments_url);
  EXPECT_NE(wallet_manage_instruments_url, wallet_manage_addresses_url);
}

// Tests that adding an autofill profile and then submitting works.
TEST_F(AutofillDialogControllerTest, AddAutofillProfile) {
  SwitchToAutofill();
  EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(2);

  AutofillProfile full_profile(test::GetVerifiedProfile());
  CreditCard credit_card(test::GetVerifiedCreditCard());
  controller()->GetTestingManager()->AddTestingProfile(&full_profile);
  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);

  ui::MenuModel* model = controller()->MenuModelForSection(SECTION_BILLING);
  // Activate the "Add billing address" menu item.
  model->ActivatedAt(model->GetItemCount() - 2);

  // Fill in the inputs from the profile.
  FieldValueMap outputs;
  const DetailInputs& inputs =
      controller()->RequestedFieldsForSection(SECTION_BILLING);
  AutofillProfile full_profile2(test::GetVerifiedProfile2());
  for (size_t i = 0; i < inputs.size(); ++i) {
    const ServerFieldType type = inputs[i].type;
    outputs[type] = full_profile2.GetInfo(AutofillType(type), "en-US");
  }
  controller()->GetView()->SetUserInput(SECTION_BILLING, outputs);

  controller()->OnAccept();
  const AutofillProfile& added_profile =
      controller()->GetTestingManager()->imported_profile();

  const DetailInputs& shipping_inputs =
      controller()->RequestedFieldsForSection(SECTION_SHIPPING);
  for (size_t i = 0; i < shipping_inputs.size(); ++i) {
    const ServerFieldType type = shipping_inputs[i].type;
    EXPECT_EQ(full_profile2.GetInfo(AutofillType(type), "en-US"),
              added_profile.GetInfo(AutofillType(type), "en-US"));
  }
}

TEST_F(AutofillDialogControllerTest, VerifyCvv) {
  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
  EXPECT_CALL(*controller()->GetTestingWalletClient(),
              AuthenticateInstrument(_, _));

  SubmitWithWalletItems(CompleteAndValidWalletItems());

  EXPECT_TRUE(NotificationsOfType(DialogNotification::REQUIRED_ACTION).empty());
  EXPECT_TRUE(controller()->SectionIsActive(SECTION_SHIPPING));
  EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING));
  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));

  SuggestionState suggestion_state =
      controller()->SuggestionStateForSection(SECTION_CC_BILLING);
  EXPECT_TRUE(suggestion_state.extra_text.empty());

  controller()->OnDidGetFullWallet(
      wallet::GetTestFullWalletWithRequiredActions(
          std::vector<wallet::RequiredAction>(1, wallet::VERIFY_CVV)));
  ASSERT_TRUE(controller()->IsSubmitPausedOn(wallet::VERIFY_CVV));

  EXPECT_FALSE(
      NotificationsOfType(DialogNotification::REQUIRED_ACTION).empty());
  EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING));
  EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING));

  suggestion_state =
      controller()->SuggestionStateForSection(SECTION_CC_BILLING);
  EXPECT_FALSE(suggestion_state.extra_text.empty());
  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING));

  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));

  controller()->OnAccept();

  EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty());
}

TEST_F(AutofillDialogControllerTest, ErrorDuringSubmit) {
  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));

  SubmitWithWalletItems(CompleteAndValidWalletItems());

  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));

  controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR);

  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
}

TEST_F(AutofillDialogControllerTest, ErrorDuringVerifyCvv) {
  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));

  SubmitWithWalletItems(CompleteAndValidWalletItems());
  controller()->OnDidGetFullWallet(
      wallet::GetTestFullWalletWithRequiredActions(
          std::vector<wallet::RequiredAction>(1, wallet::VERIFY_CVV)));

  ASSERT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
  ASSERT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));

  controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR);

  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_CANCEL));
}

// Simulates receiving an INVALID_FORM_FIELD required action while processing a
// |WalletClientDelegate::OnDid{Save,Update}*()| call. This can happen if Online
// Wallet's server validation differs from Chrome's local validation.
TEST_F(AutofillDialogControllerTest, WalletServerSideValidation) {
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
  controller()->OnDidGetWalletItems(wallet_items.Pass());
  controller()->OnAccept();

  std::vector<wallet::RequiredAction> required_actions;
  required_actions.push_back(wallet::INVALID_FORM_FIELD);

  std::vector<wallet::FormFieldError> form_errors;
  form_errors.push_back(
      wallet::FormFieldError(wallet::FormFieldError::INVALID_POSTAL_CODE,
                             wallet::FormFieldError::SHIPPING_ADDRESS));

  EXPECT_CALL(*controller()->GetView(), UpdateForErrors());
  controller()->OnDidSaveToWallet(std::string(),
                                  std::string(),
                                  required_actions,
                                  form_errors);
}

// Simulates receiving unrecoverable Wallet server validation errors.
TEST_F(AutofillDialogControllerTest, WalletServerSideValidationUnrecoverable) {
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
  controller()->OnDidGetWalletItems(wallet_items.Pass());
  controller()->OnAccept();

  std::vector<wallet::RequiredAction> required_actions;
  required_actions.push_back(wallet::INVALID_FORM_FIELD);

  std::vector<wallet::FormFieldError> form_errors;
  form_errors.push_back(
      wallet::FormFieldError(wallet::FormFieldError::UNKNOWN_ERROR,
                             wallet::FormFieldError::UNKNOWN_LOCATION));

  controller()->OnDidSaveToWallet(std::string(),
                                  std::string(),
                                  required_actions,
                                  form_errors);

  EXPECT_EQ(1U, NotificationsOfType(
      DialogNotification::REQUIRED_ACTION).size());
}

// Test Wallet banners are show in the right situations. These banners promote
// saving details into Wallet (i.e. "[x] Save details to Wallet").
TEST_F(AutofillDialogControllerTest, WalletBanners) {
  // Simulate non-signed-in case.
  SetUpControllerWithFormData(DefaultFormData());
  GoogleServiceAuthError error(GoogleServiceAuthError::NONE);
  controller()->OnPassiveSigninFailure(error);
  EXPECT_EQ(0U, NotificationsOfType(
      DialogNotification::WALLET_USAGE_CONFIRMATION).size());

  // Sign in a user with a completed account.
  SetUpControllerWithFormData(DefaultFormData());
  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());

  // Full account; should show "Details from Wallet" message.
  EXPECT_EQ(1U, NotificationsOfType(
      DialogNotification::WALLET_USAGE_CONFIRMATION).size());
  SwitchToAutofill();
  EXPECT_EQ(1U, NotificationsOfType(
      DialogNotification::WALLET_USAGE_CONFIRMATION).size());

  // Start over and sign in a user with an incomplete account.
  SetUpControllerWithFormData(DefaultFormData());
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
  controller()->OnDidGetWalletItems(wallet_items.Pass());

  // Partial account.
  EXPECT_EQ(1U, NotificationsOfType(
      DialogNotification::WALLET_USAGE_CONFIRMATION).size());

  SwitchToAutofill();
  EXPECT_EQ(1U, NotificationsOfType(
      DialogNotification::WALLET_USAGE_CONFIRMATION).size());

  // A Wallet error should kill any Wallet promos.
  controller()->OnWalletError(wallet::WalletClient::UNKNOWN_ERROR);

  EXPECT_EQ(1U, NotificationsOfType(
      DialogNotification::WALLET_ERROR).size());
  EXPECT_EQ(0U, NotificationsOfType(
      DialogNotification::WALLET_USAGE_CONFIRMATION).size());
}

TEST_F(AutofillDialogControllerTest, ViewCancelDoesntSetPref) {
  ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
      ::prefs::kAutofillDialogPayWithoutWallet));

  SwitchToAutofill();

  controller()->OnCancel();
  controller()->ViewClosed();

  EXPECT_FALSE(profile()->GetPrefs()->HasPrefPath(
      ::prefs::kAutofillDialogPayWithoutWallet));
}

TEST_F(AutofillDialogControllerTest, SubmitWithSigninErrorDoesntSetPref) {
  ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
      ::prefs::kAutofillDialogPayWithoutWallet));

  SimulateSigninError();
  FillCreditCardInputs();
  controller()->OnAccept();

  EXPECT_FALSE(profile()->GetPrefs()->HasPrefPath(
      ::prefs::kAutofillDialogPayWithoutWallet));
}

// Tests that there's an overlay shown while waiting for full wallet items.
TEST_F(AutofillDialogControllerTest, WalletFirstRun) {
  EXPECT_TRUE(controller()->GetDialogOverlay().image.IsEmpty());

  SubmitWithWalletItems(CompleteAndValidWalletItems());
  EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty());

  controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
  EXPECT_FALSE(controller()->GetDialogOverlay().image.IsEmpty());
  EXPECT_FALSE(form_structure());

  // Don't make the test wait for 2 seconds.
  controller()->ForceFinishSubmit();
  EXPECT_TRUE(form_structure());
}

TEST_F(AutofillDialogControllerTest, ViewSubmitSetsPref) {
  ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
      ::prefs::kAutofillDialogPayWithoutWallet));

  SwitchToAutofill();
  FillCreditCardInputs();
  controller()->OnAccept();

  EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
      ::prefs::kAutofillDialogPayWithoutWallet));
  EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
      ::prefs::kAutofillDialogPayWithoutWallet));

  // Try again with a signin error (just leaves the pref alone).
  SetUpControllerWithFormData(DefaultFormData());

  // Setting up the controller again should not change the pref.
  EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
      ::prefs::kAutofillDialogPayWithoutWallet));
  EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
      ::prefs::kAutofillDialogPayWithoutWallet));

  SimulateSigninError();
  FillCreditCardInputs();
  controller()->OnAccept();
  EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
      ::prefs::kAutofillDialogPayWithoutWallet));
  EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
      ::prefs::kAutofillDialogPayWithoutWallet));

  // Successfully choosing wallet does set the pref.
  // Note that OnDidGetWalletItems sets the account chooser to wallet mode.
  SetUpControllerWithFormData(DefaultFormData());

  controller()->OnDidFetchWalletCookieValue(std::string());
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
  controller()->OnDidGetWalletItems(wallet_items.Pass());
  controller()->OnAccept();
  controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
  controller()->ForceFinishSubmit();

  EXPECT_TRUE(profile()->GetPrefs()->HasPrefPath(
      ::prefs::kAutofillDialogPayWithoutWallet));
  EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
      ::prefs::kAutofillDialogPayWithoutWallet));
}

TEST_F(AutofillDialogControllerTest, HideWalletEmail) {
  SwitchToAutofill();

  // Email field should be showing when using Autofill.
  EXPECT_TRUE(controller()->SectionIsActive(SECTION_BILLING));
  EXPECT_FALSE(controller()->SectionIsActive(SECTION_CC_BILLING));
  EXPECT_TRUE(SectionContainsField(SECTION_BILLING, EMAIL_ADDRESS));

  SwitchToWallet();

  // Reset the wallet state.
  controller()->OnDidGetWalletItems(scoped_ptr<wallet::WalletItems>());

  // Setup some wallet state, submit, and get a full wallet to end the flow.
  scoped_ptr<wallet::WalletItems> wallet_items = CompleteAndValidWalletItems();

  // Filling |form_structure()| depends on the current username and wallet items
  // being fetched. Until both of these have occurred, the user should not be
  // able to click Submit if using Wallet. The username fetch happened earlier.
  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
  controller()->OnDidGetWalletItems(wallet_items.Pass());
  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));

  // Email field should be absent when using Wallet.
  EXPECT_FALSE(controller()->SectionIsActive(SECTION_BILLING));
  EXPECT_TRUE(controller()->SectionIsActive(SECTION_CC_BILLING));
  EXPECT_FALSE(SectionContainsField(SECTION_CC_BILLING, EMAIL_ADDRESS));

  controller()->OnAccept();
  controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
  controller()->ForceFinishSubmit();

  ASSERT_TRUE(form_structure());
  size_t i = 0;
  for (; i < form_structure()->field_count(); ++i) {
    if (form_structure()->field(i)->Type().GetStorableType() == EMAIL_ADDRESS) {
      EXPECT_EQ(ASCIIToUTF16(kFakeEmail), form_structure()->field(i)->value);
      break;
    }
  }
  EXPECT_LT(i, form_structure()->field_count());
}

// Test if autofill types of returned form structure are correct for billing
// entries.
TEST_F(AutofillDialogControllerTest, AutofillTypes) {
  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
  controller()->OnAccept();
  controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
  controller()->ForceFinishSubmit();
  ASSERT_TRUE(form_structure());
  ASSERT_EQ(20U, form_structure()->field_count());
  EXPECT_EQ(EMAIL_ADDRESS,
            form_structure()->field(0)->Type().GetStorableType());
  EXPECT_EQ(CREDIT_CARD_NUMBER,
            form_structure()->field(2)->Type().GetStorableType());
  EXPECT_EQ(ADDRESS_HOME_STATE,
            form_structure()->field(9)->Type().GetStorableType());
  EXPECT_EQ(ADDRESS_BILLING, form_structure()->field(9)->Type().group());
  EXPECT_EQ(ADDRESS_HOME_STATE,
            form_structure()->field(16)->Type().GetStorableType());
  EXPECT_EQ(ADDRESS_HOME, form_structure()->field(16)->Type().group());
}

TEST_F(AutofillDialogControllerTest, SaveDetailsInChrome) {
  SwitchToAutofill();
  EXPECT_CALL(*controller()->GetView(), ModelChanged()).Times(4);

  AutofillProfile full_profile(test::GetVerifiedProfile());
  controller()->GetTestingManager()->AddTestingProfile(&full_profile);

  CreditCard card(test::GetVerifiedCreditCard());
  controller()->GetTestingManager()->AddTestingCreditCard(&card);
  EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());

  controller()->MenuModelForSection(SECTION_BILLING)->ActivatedAt(0);
  EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());

  controller()->MenuModelForSection(SECTION_BILLING)->ActivatedAt(1);
  EXPECT_TRUE(controller()->ShouldOfferToSaveInChrome());

  profile()->GetPrefs()->SetBoolean(prefs::kAutofillEnabled, false);
  EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());

  profile()->GetPrefs()->SetBoolean(prefs::kAutofillEnabled, true);
  controller()->MenuModelForSection(SECTION_BILLING)->ActivatedAt(1);
  EXPECT_TRUE(controller()->ShouldOfferToSaveInChrome());

  profile()->ForceIncognito(true);
  EXPECT_FALSE(controller()->ShouldOfferToSaveInChrome());
}

TEST_F(AutofillDialogControllerTest, DisabledAutofill) {
  SwitchToAutofill();
  ASSERT_TRUE(profile()->GetPrefs()->GetBoolean(prefs::kAutofillEnabled));

  AutofillProfile verified_profile(test::GetVerifiedProfile());
  controller()->GetTestingManager()->AddTestingProfile(&verified_profile);

  CreditCard credit_card(test::GetVerifiedCreditCard());
  controller()->GetTestingManager()->AddTestingCreditCard(&credit_card);

  // Verify suggestions menus should be showing when Autofill is enabled.
  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC));
  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_BILLING));
  EXPECT_EQ(
      4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());

  EXPECT_CALL(*controller()->GetView(), ModelChanged());
  profile()->GetPrefs()->SetBoolean(prefs::kAutofillEnabled, false);

  // Verify billing and credit card suggestions menus are hidden when Autofill
  // is disabled.
  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC));
  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_BILLING));
  // And that the shipping suggestions menu has less selections.
  EXPECT_EQ(
      2, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());

  // Additionally, editing fields should not show Autofill popups.
  ASSERT_NO_FATAL_FAILURE(controller()->UserEditedOrActivatedInput(
      SECTION_BILLING,
      NAME_BILLING_FULL,
      gfx::NativeView(),
      gfx::Rect(),
      verified_profile.GetInfo(AutofillType(NAME_FULL), "en-US").substr(0, 1),
      true));
  EXPECT_EQ(UNKNOWN_TYPE, controller()->popup_input_type());
}

// Tests that user is prompted when using instrument with minimal address.
TEST_F(AutofillDialogControllerTest, UpgradeMinimalAddress) {
  // A minimal address being selected should trigger error validation in the
  // view. Called once for each incomplete suggestion.
  EXPECT_CALL(*controller()->GetView(), UpdateForErrors());

  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentWithIdAndAddress(
      "id", wallet::GetTestMinimalAddress()));
  scoped_ptr<wallet::Address> address(wallet::GetTestShippingAddress());
  address->set_is_complete_address(false);
  wallet_items->AddAddress(address.Pass());
  controller()->OnDidGetWalletItems(wallet_items.Pass());

  // Assert that dialog's SECTION_CC_BILLING section is in edit mode.
  ASSERT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING));
  // Shipping section should be in edit mode because of
  // is_minimal_shipping_address.
  ASSERT_TRUE(controller()->IsEditingExistingData(SECTION_SHIPPING));
}

TEST_F(AutofillDialogControllerTest, RiskNeverLoadsWithPendingLegalDocuments) {
  EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(0);

  scoped_ptr<wallet::WalletItems> wallet_items = CompleteAndValidWalletItems();
  wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
  wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
  controller()->OnDidGetWalletItems(wallet_items.Pass());
  controller()->OnAccept();
}

TEST_F(AutofillDialogControllerTest, RiskLoadsAfterAcceptingLegalDocuments) {
  EXPECT_CALL(*controller(), LoadRiskFingerprintData()).Times(0);

  scoped_ptr<wallet::WalletItems> wallet_items = CompleteAndValidWalletItems();
  wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
  wallet_items->AddLegalDocument(wallet::GetTestLegalDocument());
  controller()->OnDidGetWalletItems(wallet_items.Pass());

  testing::Mock::VerifyAndClear(controller());
  EXPECT_CALL(*controller(), LoadRiskFingerprintData());

  controller()->OnAccept();

  // Simulate a risk load and verify |GetRiskData()| matches the encoded value.
  controller()->OnDidAcceptLegalDocuments();
  controller()->OnDidLoadRiskFingerprintData("a");
  EXPECT_EQ("a", controller()->GetRiskData());
}

TEST_F(AutofillDialogControllerTest, NoManageMenuItemForNewWalletUsers) {
  // Make sure the menu model item is created for a returning Wallet user.
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
  wallet_items->AddAddress(wallet::GetTestShippingAddress());
  controller()->OnDidGetWalletItems(wallet_items.Pass());

  EXPECT_TRUE(controller()->MenuModelForSection(SECTION_CC_BILLING));
  // "Same as billing", "123 address", "Add address...", and "Manage addresses".
  EXPECT_EQ(
      4, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());

  // Make sure the menu model item is not created for new Wallet users.
  base::DictionaryValue dict;
  scoped_ptr<base::ListValue> required_actions(new base::ListValue);
  required_actions->AppendString("setup_wallet");
  dict.Set("required_action", required_actions.release());
  controller()->OnDidGetWalletItems(
      wallet::WalletItems::CreateWalletItems(dict).Pass());

  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING));
  // "Same as billing" and "Add address...".
  EXPECT_EQ(
      2, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
}

TEST_F(AutofillDialogControllerTest, ShippingSectionCanBeHidden) {
  FormFieldData email_field;
  email_field.autocomplete_attribute = "email";
  FormFieldData cc_field;
  cc_field.autocomplete_attribute = "cc-number";
  FormFieldData billing_field;
  billing_field.autocomplete_attribute = "billing address-level1";

  FormData form_data;
  form_data.fields.push_back(email_field);
  form_data.fields.push_back(cc_field);
  form_data.fields.push_back(billing_field);

  AutofillProfile full_profile(test::GetVerifiedProfile());
  controller()->GetTestingManager()->AddTestingProfile(&full_profile);
  SetUpControllerWithFormData(form_data);

  SwitchToAutofill();

  EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING));

  FillCreditCardInputs();
  controller()->OnAccept();
  EXPECT_TRUE(form_structure());
}

TEST_F(AutofillDialogControllerTest, ShippingSectionCanBeHiddenForWallet) {
  FormFieldData email_field;
  email_field.autocomplete_attribute = "email";
  FormFieldData cc_field;
  cc_field.autocomplete_attribute = "cc-number";
  FormFieldData billing_field;
  billing_field.autocomplete_attribute = "billing address-level1";

  FormData form_data;
  form_data.fields.push_back(email_field);
  form_data.fields.push_back(cc_field);
  form_data.fields.push_back(billing_field);

  SetUpControllerWithFormData(form_data);
  EXPECT_FALSE(controller()->SectionIsActive(SECTION_SHIPPING));
  EXPECT_FALSE(controller()->IsShippingAddressRequired());

  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetFullWallet(_));
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
  SubmitWithWalletItems(wallet_items.Pass());
  controller()->OnDidGetFullWallet(wallet::GetTestFullWalletInstrumentOnly());
  controller()->ForceFinishSubmit();
  EXPECT_TRUE(form_structure());
}

TEST_F(AutofillDialogControllerTest, NotProdNotification) {
  // To make IsPayingWithWallet() true.
  controller()->OnDidGetWalletItems(
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));

  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  ASSERT_EQ(
      "",
      command_line->GetSwitchValueASCII(switches::kWalletServiceUseSandbox));

  command_line->AppendSwitchASCII(switches::kWalletServiceUseSandbox, "1");
  EXPECT_EQ(1U,
            NotificationsOfType(DialogNotification::DEVELOPER_WARNING).size());
}

TEST_F(AutofillDialogControllerTest, NoNotProdNotification) {
  // To make IsPayingWithWallet() true.
  controller()->OnDidGetWalletItems(
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));

  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  ASSERT_EQ(
      "",
      command_line->GetSwitchValueASCII(switches::kWalletServiceUseSandbox));

  command_line->AppendSwitchASCII(switches::kWalletServiceUseSandbox, "0");
  EXPECT_EQ(0U,
            NotificationsOfType(DialogNotification::DEVELOPER_WARNING).size());
}

// Ensure Wallet instruments marked expired by the server are shown as invalid.
TEST_F(AutofillDialogControllerTest, WalletExpiredCard) {
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentExpired());
  controller()->OnDidGetWalletItems(wallet_items.Pass());

  EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING));

  const DetailInputs& inputs =
      controller()->RequestedFieldsForSection(SECTION_CC_BILLING);
  FieldValueMap outputs;
  CopyInitialValues(inputs, &outputs);

  // The local inputs are invalid because the server said so. They'll
  // stay invalid until they differ from the remotely fetched model.
  ValidityMessages messages = controller()->InputsAreValid(SECTION_CC_BILLING,
                                                           outputs);
  EXPECT_TRUE(messages.HasSureError(CREDIT_CARD_EXP_MONTH));
  EXPECT_TRUE(messages.HasSureError(CREDIT_CARD_EXP_4_DIGIT_YEAR));

  // Make the local input year differ from the instrument.
  CopyInitialValues(inputs, &outputs);
  outputs[CREDIT_CARD_EXP_4_DIGIT_YEAR] = ASCIIToUTF16("3002");
  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
  EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_MONTH));
  EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));

  // Make the local input month differ from the instrument.
  CopyInitialValues(inputs, &outputs);
  outputs[CREDIT_CARD_EXP_MONTH] = ASCIIToUTF16("06");
  messages = controller()->InputsAreValid(SECTION_CC_BILLING, outputs);
  EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_MONTH));
  EXPECT_FALSE(HasAnyError(messages, CREDIT_CARD_EXP_4_DIGIT_YEAR));
}

TEST_F(AutofillDialogControllerTest, ChooseAnotherInstrumentOrAddress) {
  SubmitWithWalletItems(CompleteAndValidWalletItems());

  EXPECT_EQ(0U, NotificationsOfType(
      DialogNotification::REQUIRED_ACTION).size());
  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
  controller()->OnDidGetFullWallet(
      wallet::GetTestFullWalletWithRequiredActions(
          std::vector<wallet::RequiredAction>(
              1, wallet::CHOOSE_ANOTHER_INSTRUMENT_OR_ADDRESS)));
  EXPECT_EQ(1U, NotificationsOfType(
      DialogNotification::REQUIRED_ACTION).size());
  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());

  controller()->OnAccept();
  EXPECT_EQ(0U, NotificationsOfType(
      DialogNotification::REQUIRED_ACTION).size());
}

TEST_F(AutofillDialogControllerTest, NewCardBubbleShown) {
  SwitchToAutofill();
  FillCreditCardInputs();
  controller()->OnAccept();
  controller()->ViewClosed();

  EXPECT_EQ(1, mock_new_card_bubble_controller()->bubbles_shown());
  EXPECT_EQ(0, test_generated_bubble_controller()->bubbles_shown());
}

TEST_F(AutofillDialogControllerTest, GeneratedCardBubbleShown) {
  SubmitWithWalletItems(CompleteAndValidWalletItems());
  controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
  controller()->ForceFinishSubmit();
  controller()->ViewClosed();

  EXPECT_EQ(0, mock_new_card_bubble_controller()->bubbles_shown());
  EXPECT_EQ(1, test_generated_bubble_controller()->bubbles_shown());
}

// Verify that new Wallet data is fetched when the user switches away from the
// tab hosting the Autofill dialog and back. Also verify that the user's
// selection is preserved across this re-fetch.
TEST_F(AutofillDialogControllerTest, ReloadWalletItemsOnActivation) {
  // Initialize some Wallet data.
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
  wallet_items->AddAddress(wallet::GetTestShippingAddress());
  controller()->OnDidGetWalletItems(wallet_items.Pass());

  // Initially, the default entries should be selected.
  ui::MenuModel* cc_billing_model =
      controller()->MenuModelForSection(SECTION_CC_BILLING);
  ui::MenuModel* shipping_model =
      controller()->MenuModelForSection(SECTION_SHIPPING);
  // "add", "manage", and 2 suggestions.
  ASSERT_EQ(4, cc_billing_model->GetItemCount());
  EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(0));
  // "use billing", "add", "manage", and 2 suggestions.
  ASSERT_EQ(5, shipping_model->GetItemCount());
  EXPECT_TRUE(shipping_model->IsItemCheckedAt(2));

  // Select entries other than the defaults.
  cc_billing_model->ActivatedAt(1);
  shipping_model->ActivatedAt(1);
  // 2 suggestions, "add", and "manage".
  ASSERT_EQ(4, cc_billing_model->GetItemCount());
  EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(1));
  // "use billing", 2 suggestions, "add", "manage".
  ASSERT_EQ(5, shipping_model->GetItemCount());
  EXPECT_TRUE(shipping_model-> IsItemCheckedAt(1));

  // Simulate switching away from the tab and back.  This should issue a request
  // for wallet items.
  controller()->ClearLastWalletItemsFetchTimestampForTesting();
  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
  controller()->TabActivated();

  // Simulate a response that includes different items.
  wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrumentExpired());
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
  controller()->OnDidGetWalletItems(wallet_items.Pass());

  // The previously selected entries should still be selected.
  // 3 suggestions, "add", and "manage".
  ASSERT_EQ(5, cc_billing_model->GetItemCount());
  EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(2));
  // "use billing", 1 suggestion, "add", and "manage".
  ASSERT_EQ(4, shipping_model->GetItemCount());
  EXPECT_TRUE(shipping_model->IsItemCheckedAt(1));
}

// Verify that if the default values change when re-fetching Wallet data, these
// new default values are selected in the dialog.
TEST_F(AutofillDialogControllerTest,
       ReloadWalletItemsOnActivationWithNewDefaults) {
  // Initialize some Wallet data.
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddInstrument(wallet::GetTestMaskedInstrument());
  wallet_items->AddInstrument(wallet::GetTestNonDefaultMaskedInstrument());
  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
  wallet_items->AddAddress(wallet::GetTestShippingAddress());
  controller()->OnDidGetWalletItems(wallet_items.Pass());

  // Initially, the default entries should be selected.
  ui::MenuModel* cc_billing_model =
      controller()->MenuModelForSection(SECTION_CC_BILLING);
  ui::MenuModel* shipping_model =
      controller()->MenuModelForSection(SECTION_SHIPPING);
  // 2 suggestions, "add", and "manage".
  ASSERT_EQ(4, cc_billing_model->GetItemCount());
  EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(0));
  // "use billing", 2 suggestions, "add", and "manage".
  ASSERT_EQ(5, shipping_model->GetItemCount());
  EXPECT_TRUE(shipping_model->IsItemCheckedAt(2));

  // Simulate switching away from the tab and back.  This should issue a request
  // for wallet items.
  controller()->ClearLastWalletItemsFetchTimestampForTesting();
  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
  controller()->TabActivated();

  // Simulate a response that includes different default values.
  wallet_items =
      wallet::GetTestWalletItemsWithDefaultIds("new_default_instrument_id",
                                               "new_default_address_id",
                                               wallet::AMEX_DISALLOWED);
  scoped_ptr<wallet::Address> other_address = wallet::GetTestShippingAddress();
  other_address->set_object_id("other_address_id");
  scoped_ptr<wallet::Address> new_default_address =
      wallet::GetTestNonDefaultShippingAddress();
  new_default_address->set_object_id("new_default_address_id");

  wallet_items->AddInstrument(
      wallet::GetTestMaskedInstrumentWithId("other_instrument_id"));
  wallet_items->AddInstrument(
      wallet::GetTestMaskedInstrumentWithId("new_default_instrument_id"));
  wallet_items->AddAddress(new_default_address.Pass());
  wallet_items->AddAddress(other_address.Pass());
  controller()->OnDidGetWalletItems(wallet_items.Pass());

  // The new default entries should be selected.
  // 2 suggestions, "add", and "manage".
  ASSERT_EQ(4, cc_billing_model->GetItemCount());
  EXPECT_TRUE(cc_billing_model->IsItemCheckedAt(1));
  // "use billing", 2 suggestions, "add", and "manage".
  ASSERT_EQ(5, shipping_model->GetItemCount());
  EXPECT_TRUE(shipping_model->IsItemCheckedAt(1));
}

TEST_F(AutofillDialogControllerTest, ReloadWithEmptyWalletItems) {
  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
  controller()->MenuModelForSection(SECTION_CC_BILLING)->ActivatedAt(1);
  controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(1);

  controller()->ClearLastWalletItemsFetchTimestampForTesting();
  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
  controller()->TabActivated();

  controller()->OnDidGetWalletItems(
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED));

  EXPECT_FALSE(controller()->MenuModelForSection(SECTION_CC_BILLING));
  EXPECT_EQ(
      3, controller()->MenuModelForSection(SECTION_SHIPPING)->GetItemCount());
}

TEST_F(AutofillDialogControllerTest, SaveInChromeByDefault) {
  EXPECT_TRUE(controller()->ShouldSaveInChrome());
  SwitchToAutofill();
  FillCreditCardInputs();
  controller()->OnAccept();
  EXPECT_TRUE(controller()->ShouldSaveInChrome());
}

TEST_F(AutofillDialogControllerTest,
       SaveInChromePreferenceNotRememberedOnCancel) {
  EXPECT_TRUE(controller()->ShouldSaveInChrome());
  SwitchToAutofill();
  controller()->GetView()->CheckSaveDetailsLocallyCheckbox(false);
  controller()->OnCancel();
  EXPECT_TRUE(controller()->ShouldSaveInChrome());
}

TEST_F(AutofillDialogControllerTest,
       SaveInChromePreferenceRememberedOnSuccess) {
  EXPECT_TRUE(controller()->ShouldSaveInChrome());
  SwitchToAutofill();
  FillCreditCardInputs();
  controller()->GetView()->CheckSaveDetailsLocallyCheckbox(false);
  controller()->OnAccept();
  EXPECT_FALSE(controller()->ShouldSaveInChrome());
}

TEST_F(AutofillDialogControllerTest,
       SubmitButtonIsDisabled_SpinnerFinishesBeforeDelay) {
  // Reset Wallet state.
  controller()->OnDidGetWalletItems(scoped_ptr<wallet::WalletItems>());

  EXPECT_EQ(1, controller()->get_submit_button_delay_count());

  // Begin the submit button delay.
  controller()->SimulateSubmitButtonDelayBegin();

  EXPECT_TRUE(controller()->ShouldShowSpinner());
  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));

  // Stop the spinner.
  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());

  EXPECT_FALSE(controller()->ShouldShowSpinner());
  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));

  // End the submit button delay.
  controller()->SimulateSubmitButtonDelayEnd();

  EXPECT_FALSE(controller()->ShouldShowSpinner());
  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
}

TEST_F(AutofillDialogControllerTest,
       SubmitButtonIsDisabled_SpinnerFinishesAfterDelay) {
  // Reset Wallet state.
  controller()->OnDidGetWalletItems(scoped_ptr<wallet::WalletItems>());

  EXPECT_EQ(1, controller()->get_submit_button_delay_count());

  // Begin the submit button delay.
  controller()->SimulateSubmitButtonDelayBegin();

  EXPECT_TRUE(controller()->ShouldShowSpinner());
  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));

  // End the submit button delay.
  controller()->SimulateSubmitButtonDelayEnd();

  EXPECT_TRUE(controller()->ShouldShowSpinner());
  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));

  // Stop the spinner.
  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());

  EXPECT_FALSE(controller()->ShouldShowSpinner());
  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
}

TEST_F(AutofillDialogControllerTest, SubmitButtonIsDisabled_NoSpinner) {
  SwitchToAutofill();

  EXPECT_EQ(1, controller()->get_submit_button_delay_count());

  // Begin the submit button delay.
  controller()->SimulateSubmitButtonDelayBegin();

  EXPECT_FALSE(controller()->ShouldShowSpinner());
  EXPECT_FALSE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));

  // End the submit button delay.
  controller()->SimulateSubmitButtonDelayEnd();

  EXPECT_FALSE(controller()->ShouldShowSpinner());
  EXPECT_TRUE(controller()->IsDialogButtonEnabled(ui::DIALOG_BUTTON_OK));
}

TEST_F(AutofillDialogControllerTest, IconsForFields_NoCreditCard) {
  FieldValueMap values;
  values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
  FieldIconMap icons = controller()->IconsForFields(values);
  EXPECT_TRUE(icons.empty());
}

TEST_F(AutofillDialogControllerTest, IconsForFields_CreditCardNumberOnly) {
  FieldValueMap values;
  values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
  values[CREDIT_CARD_NUMBER] = ASCIIToUTF16(kTestCCNumberVisa);
  FieldIconMap icons = controller()->IconsForFields(values);
  EXPECT_EQ(1UL, icons.size());
  EXPECT_EQ(1UL, icons.count(CREDIT_CARD_NUMBER));
}

TEST_F(AutofillDialogControllerTest, IconsForFields_CvcOnly) {
  FieldValueMap values;
  values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
  values[CREDIT_CARD_VERIFICATION_CODE] = ASCIIToUTF16("123");
  FieldIconMap icons = controller()->IconsForFields(values);
  EXPECT_EQ(1UL, icons.size());
  EXPECT_EQ(1UL, icons.count(CREDIT_CARD_VERIFICATION_CODE));
}

TEST_F(AutofillDialogControllerTest, IconsForFields_BothCreditCardAndCvc) {
  FieldValueMap values;
  values[EMAIL_ADDRESS] = ASCIIToUTF16(kFakeEmail);
  values[CREDIT_CARD_NUMBER] = ASCIIToUTF16(kTestCCNumberVisa);
  values[CREDIT_CARD_VERIFICATION_CODE] = ASCIIToUTF16("123");
  FieldIconMap icons = controller()->IconsForFields(values);
  EXPECT_EQ(2UL, icons.size());
  EXPECT_EQ(1UL, icons.count(CREDIT_CARD_VERIFICATION_CODE));
  EXPECT_EQ(1UL, icons.count(CREDIT_CARD_NUMBER));
}

TEST_F(AutofillDialogControllerTest, FieldControlsIcons) {
  EXPECT_TRUE(controller()->FieldControlsIcons(CREDIT_CARD_NUMBER));
  EXPECT_FALSE(controller()->FieldControlsIcons(CREDIT_CARD_VERIFICATION_CODE));
  EXPECT_FALSE(controller()->FieldControlsIcons(EMAIL_ADDRESS));
}

TEST_F(AutofillDialogControllerTest, SaveCreditCardIncludesName_NoBilling) {
  SwitchToAutofill();

  CreditCard test_credit_card(test::GetVerifiedCreditCard());
  FillInputs(SECTION_CC, test_credit_card);

  AutofillProfile test_profile(test::GetVerifiedProfile());
  FillInputs(SECTION_BILLING, test_profile);

  UseBillingForShipping();

  controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
  controller()->OnAccept();

  TestPersonalDataManager* test_pdm = controller()->GetTestingManager();
  const CreditCard& imported_card = test_pdm->imported_credit_card();
  EXPECT_EQ(test_profile.GetInfo(AutofillType(NAME_FULL), "en-US"),
            imported_card.GetRawInfo(CREDIT_CARD_NAME));
}

TEST_F(AutofillDialogControllerTest, SaveCreditCardIncludesName_WithBilling) {
  SwitchToAutofill();

  TestPersonalDataManager* test_pdm = controller()->GetTestingManager();
  AutofillProfile test_profile(test::GetVerifiedProfile());

  EXPECT_CALL(*controller()->GetView(), ModelChanged());
  test_pdm->AddTestingProfile(&test_profile);
  ASSERT_TRUE(controller()->MenuModelForSection(SECTION_BILLING));

  CreditCard test_credit_card(test::GetVerifiedCreditCard());
  FillInputs(SECTION_CC, test_credit_card);

  controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
  controller()->OnAccept();

  const CreditCard& imported_card = test_pdm->imported_credit_card();
  EXPECT_EQ(test_profile.GetInfo(AutofillType(NAME_FULL), "en-US"),
            imported_card.GetRawInfo(CREDIT_CARD_NAME));

  controller()->ViewClosed();
}

TEST_F(AutofillDialogControllerTest, InputEditability) {
  // Empty wallet items: all fields are editable.
  scoped_ptr<wallet::WalletItems> items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  controller()->OnDidGetWalletItems(items.Pass());

  DialogSection sections[] = { SECTION_CC_BILLING, SECTION_SHIPPING };
  for (size_t i = 0; i < arraysize(sections); ++i) {
    const DetailInputs& inputs =
        controller()->RequestedFieldsForSection(sections[i]);
    for (size_t j = 0; j < inputs.size(); ++j) {
      EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i]));
    }
  }

  // Expired instrument: CC number + CVV are not editable.
  items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  scoped_ptr<wallet::WalletItems::MaskedInstrument> expired_instrument =
      wallet::GetTestMaskedInstrumentExpired();
  items->AddInstrument(expired_instrument.Pass());
  controller()->OnDidGetWalletItems(items.Pass());
  EXPECT_TRUE(controller()->IsEditingExistingData(SECTION_CC_BILLING));

  FieldValueMap outputs;
  CopyInitialValues(
      controller()->RequestedFieldsForSection(SECTION_CC_BILLING),
      &outputs);
  controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);

  for (size_t i = 0; i < arraysize(sections); ++i) {
    const DetailInputs& inputs =
        controller()->RequestedFieldsForSection(sections[i]);
    for (size_t j = 0; j < inputs.size(); ++j) {
      if (inputs[j].type == CREDIT_CARD_NUMBER ||
          inputs[j].type == CREDIT_CARD_VERIFICATION_CODE) {
        EXPECT_FALSE(controller()->InputIsEditable(inputs[j], sections[i]));
      } else {
        EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i]));
      }
    }
  }

  // User changes the billing address; same story.
  outputs[ADDRESS_BILLING_ZIP] = ASCIIToUTF16("77025");
  controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
  for (size_t i = 0; i < arraysize(sections); ++i) {
    const DetailInputs& inputs =
        controller()->RequestedFieldsForSection(sections[i]);
    for (size_t j = 0; j < inputs.size(); ++j) {
      if (inputs[j].type == CREDIT_CARD_NUMBER ||
          inputs[j].type == CREDIT_CARD_VERIFICATION_CODE) {
        EXPECT_FALSE(controller()->InputIsEditable(inputs[j], sections[i]));
      } else {
        EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i]));
      }
    }
  }

  // User changes a detail of the CC itself (expiration date), CVV is now
  // editable (and mandatory).
  outputs[CREDIT_CARD_EXP_MONTH] = ASCIIToUTF16("06");
  controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);
  for (size_t i = 0; i < arraysize(sections); ++i) {
    const DetailInputs& inputs =
        controller()->RequestedFieldsForSection(sections[i]);
    for (size_t j = 0; j < inputs.size(); ++j) {
      if (inputs[j].type == CREDIT_CARD_NUMBER)
        EXPECT_FALSE(controller()->InputIsEditable(inputs[j], sections[i]));
      else
        EXPECT_TRUE(controller()->InputIsEditable(inputs[j], sections[i]));
    }
  }
}

// When the default country is something besides US, wallet is not selected
// and the account chooser shouldn't be visible.
TEST_F(AutofillDialogControllerTest, HideWalletInOtherCountries) {
  // Addresses from different countries.
  AutofillProfile us_profile(base::GenerateGUID(), kSettingsOrigin),
      es_profile(base::GenerateGUID(), kSettingsOrigin),
      es_profile2(base::GenerateGUID(), kSettingsOrigin);
  us_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
  es_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("ES"));
  es_profile2.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("ES"));

  // If US is indicated (via timezone), show Wallet.
  ResetControllerWithFormData(DefaultFormData());
  controller()->GetTestingManager()->set_timezone_country_code("US");
  controller()->Show();
  EXPECT_TRUE(
      controller()->AccountChooserModelForTesting()->WalletIsSelected());
  controller()->OnDidFetchWalletCookieValue(std::string());
  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
  EXPECT_TRUE(controller()->ShouldShowAccountChooser());
  EXPECT_TRUE(
      controller()->AccountChooserModelForTesting()->WalletIsSelected());

  // If US is not indicated, don't show Wallet.
  ResetControllerWithFormData(DefaultFormData());
  controller()->GetTestingManager()->set_timezone_country_code("ES");
  controller()->Show();
  controller()->OnDidFetchWalletCookieValue(std::string());
  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
  EXPECT_FALSE(controller()->ShouldShowAccountChooser());

  // If US is indicated (via a profile), show Wallet.
  ResetControllerWithFormData(DefaultFormData());
  controller()->GetTestingManager()->set_timezone_country_code("ES");
  controller()->GetTestingManager()->AddTestingProfile(&us_profile);
  controller()->Show();
  controller()->OnDidFetchWalletCookieValue(std::string());
  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
  EXPECT_TRUE(controller()->ShouldShowAccountChooser());
  EXPECT_TRUE(
      controller()->AccountChooserModelForTesting()->WalletIsSelected());

  // Make sure the profile doesn't just override the timezone.
  ResetControllerWithFormData(DefaultFormData());
  controller()->GetTestingManager()->set_timezone_country_code("US");
  controller()->GetTestingManager()->AddTestingProfile(&es_profile);
  controller()->Show();
  controller()->OnDidFetchWalletCookieValue(std::string());
  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
  EXPECT_TRUE(controller()->ShouldShowAccountChooser());
  EXPECT_TRUE(
      controller()->AccountChooserModelForTesting()->WalletIsSelected());

  // Only takes one US address to enable Wallet.
  ResetControllerWithFormData(DefaultFormData());
  controller()->GetTestingManager()->set_timezone_country_code("FR");
  controller()->GetTestingManager()->AddTestingProfile(&es_profile);
  controller()->GetTestingManager()->AddTestingProfile(&es_profile2);
  controller()->GetTestingManager()->AddTestingProfile(&us_profile);
  controller()->Show();
  controller()->OnDidFetchWalletCookieValue(std::string());
  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
  EXPECT_TRUE(controller()->ShouldShowAccountChooser());
  EXPECT_TRUE(
      controller()->AccountChooserModelForTesting()->WalletIsSelected());
}

TEST_F(AutofillDialogControllerTest, DontGetWalletTillNecessary) {
  // When starting on local data mode, the dialog will provide a "Use Google
  // Wallet" link.
  profile()->GetPrefs()->SetBoolean(
      ::prefs::kAutofillDialogPayWithoutWallet, true);
  ResetControllerWithFormData(DefaultFormData());
  controller()->Show();
  base::string16 use_wallet_text = controller()->SignInLinkText();
  EXPECT_EQ(TestAutofillDialogController::NOT_CHECKED,
            controller()->SignedInState());

  // When clicked, this link will ask for wallet items. If there's a signin
  // failure, the link will switch to "Sign in to use Google Wallet".
  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
  controller()->SignInLinkClicked();
  EXPECT_NE(TestAutofillDialogController::NOT_CHECKED,
            controller()->SignedInState());
  controller()->OnDidFetchWalletCookieValue(std::string());
  controller()->OnDidGetWalletItems(CompleteAndValidWalletItems());
  controller()->OnPassiveSigninFailure(GoogleServiceAuthError(
      GoogleServiceAuthError::CONNECTION_FAILED));
  EXPECT_NE(use_wallet_text, controller()->SignInLinkText());
}

TEST_F(AutofillDialogControllerTest, MultiAccountSwitch) {
  std::vector<std::string> users;
  users.push_back("user_1@example.com");
  users.push_back("user_2@example.com");
  controller()->OnDidGetWalletItems(
      wallet::GetTestWalletItemsWithUsers(users, 0));

  // Items should be: Account 1, account 2, add account, disable wallet.
  EXPECT_EQ(4, controller()->MenuModelForAccountChooser()->GetItemCount());
  EXPECT_EQ(0U, controller()->GetTestingWalletClient()->user_index());

  // GetWalletItems should be called when the user switches accounts.
  EXPECT_CALL(*controller()->GetTestingWalletClient(), GetWalletItems(_, _));
  controller()->MenuModelForAccountChooser()->ActivatedAt(1);
  // The wallet client should be updated to the new user index.
  EXPECT_EQ(1U, controller()->GetTestingWalletClient()->user_index());
}

TEST_F(AutofillDialogControllerTest, PassiveAuthFailure) {
  controller()->OnDidGetWalletItems(
      wallet::GetTestWalletItemsWithRequiredAction(
           wallet::PASSIVE_GAIA_AUTH));
  EXPECT_TRUE(controller()->ShouldShowSpinner());
  controller()->OnPassiveSigninFailure(GoogleServiceAuthError(
      GoogleServiceAuthError::NONE));
  EXPECT_FALSE(controller()->ShouldShowSpinner());
}

TEST_F(AutofillDialogControllerTest, WalletShippingSameAsBilling) {
  // Assert initial state.
  ASSERT_FALSE(profile()->GetPrefs()->HasPrefPath(
      ::prefs::kAutofillDialogWalletShippingSameAsBilling));

  // Verify that false pref defaults to wallet defaults.
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
  wallet_items->AddAddress(wallet::GetTestShippingAddress());
  controller()->OnDidGetWalletItems(wallet_items.Pass());
  ASSERT_FALSE(profile()->GetPrefs()->GetBoolean(
      ::prefs::kAutofillDialogWalletShippingSameAsBilling));
  EXPECT_EQ(2, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());

  // Set "Same as Billing" for the shipping address and verify it sets the pref
  // and selects the appropriate menu item.
  UseBillingForShipping();
  ASSERT_EQ(0, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());
  controller()->ForceFinishSubmit();
  ASSERT_TRUE(profile()->GetPrefs()->GetBoolean(
      ::prefs::kAutofillDialogWalletShippingSameAsBilling));

  // Getting new wallet info shouldn't disrupt the preference and menu should be
  // set accordingly.
  Reset();
  wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddAddress(wallet::GetTestNonDefaultShippingAddress());
  wallet_items->AddAddress(wallet::GetTestShippingAddress());
  controller()->OnDidGetWalletItems(wallet_items.Pass());
  EXPECT_TRUE(profile()->GetPrefs()->GetBoolean(
      ::prefs::kAutofillDialogWalletShippingSameAsBilling));
  EXPECT_EQ(0, GetMenuModelForSection(SECTION_SHIPPING)->checked_item());

  // Choose a different address and ensure pref gets set to false.
  controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(1);
  controller()->ForceFinishSubmit();
  EXPECT_FALSE(profile()->GetPrefs()->GetBoolean(
      ::prefs::kAutofillDialogWalletShippingSameAsBilling));
}

// Verifies that a call to the IconsForFields() method before the card type is
// known returns a placeholder image that is at least as large as the icons for
// all of the supported major credit card issuers.
TEST_F(AutofillDialogControllerTest, IconReservedForCreditCardField) {
  FieldValueMap inputs;
  inputs[CREDIT_CARD_NUMBER] = base::string16();

  FieldIconMap icons = controller()->IconsForFields(inputs);
  EXPECT_EQ(1U, icons.size());

  ASSERT_EQ(1U, icons.count(CREDIT_CARD_NUMBER));
  gfx::Image placeholder_icon = icons[CREDIT_CARD_NUMBER];

  // Verify that the placeholder icon is at least as large as the icons for the
  // supported credit card issuers.
  const int kSupportedCardIdrs[] = {
    IDR_AUTOFILL_CC_AMEX,
    IDR_AUTOFILL_CC_DISCOVER,
    IDR_AUTOFILL_CC_GENERIC,
    IDR_AUTOFILL_CC_MASTERCARD,
    IDR_AUTOFILL_CC_VISA,
  };
  ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
  for (size_t i = 0; i < arraysize(kSupportedCardIdrs); ++i) {
    SCOPED_TRACE(base::IntToString(i));
    gfx::Image supported_card_icon = rb.GetImageNamed(kSupportedCardIdrs[i]);
    EXPECT_GE(placeholder_icon.Width(), supported_card_icon.Width());
    EXPECT_GE(placeholder_icon.Height(), supported_card_icon.Height());
  }
}

TEST_F(AutofillDialogControllerTest, CountryChangeUpdatesSection) {
  TestAutofillDialogView* view = controller()->GetView();
  view->ClearSectionUpdates();

  controller()->UserEditedOrActivatedInput(SECTION_SHIPPING,
                                           ADDRESS_HOME_COUNTRY,
                                           gfx::NativeView(),
                                           gfx::Rect(),
                                           ASCIIToUTF16("Belarus"),
                                           true);
  std::map<DialogSection, size_t> updates = view->section_updates();
  EXPECT_EQ(1U, updates[SECTION_SHIPPING]);
  EXPECT_EQ(1U, updates.size());

  view->ClearSectionUpdates();

  controller()->UserEditedOrActivatedInput(SECTION_CC_BILLING,
                                           ADDRESS_BILLING_COUNTRY,
                                           gfx::NativeView(),
                                           gfx::Rect(),
                                           ASCIIToUTF16("France"),
                                           true);
  updates = view->section_updates();
  EXPECT_EQ(1U, updates[SECTION_CC_BILLING]);
  EXPECT_EQ(1U, updates.size());

  SwitchToAutofill();
  view->ClearSectionUpdates();

  controller()->UserEditedOrActivatedInput(SECTION_BILLING,
                                           ADDRESS_BILLING_COUNTRY,
                                           gfx::NativeView(),
                                           gfx::Rect(),
                                           ASCIIToUTF16("Italy"),
                                           true);
  updates = view->section_updates();
  EXPECT_EQ(1U, updates[SECTION_BILLING]);
  EXPECT_EQ(1U, updates.size());
}

TEST_F(AutofillDialogControllerTest, CorrectCountryFromInputs) {
  EXPECT_CALL(*controller()->GetMockValidator(),
              ValidateAddress(CountryCodeMatcher("DE"), _, _));

  FieldValueMap billing_inputs;
  billing_inputs[ADDRESS_BILLING_COUNTRY] = ASCIIToUTF16("Germany");
  controller()->InputsAreValid(SECTION_BILLING, billing_inputs);

  EXPECT_CALL(*controller()->GetMockValidator(),
              ValidateAddress(CountryCodeMatcher("FR"), _, _));

  FieldValueMap shipping_inputs;
  shipping_inputs[ADDRESS_HOME_COUNTRY] = ASCIIToUTF16("France");
  controller()->InputsAreValid(SECTION_SHIPPING, shipping_inputs);
}

TEST_F(AutofillDialogControllerTest, ValidationRulesLoadedOnCountryChange) {
  ResetControllerWithFormData(DefaultFormData());
  EXPECT_CALL(*controller()->GetMockValidator(),
              LoadRules("US")).Times(AtLeast(1));
  controller()->Show();

  EXPECT_CALL(*controller()->GetMockValidator(), LoadRules("FR"));
  controller()->UserEditedOrActivatedInput(SECTION_BILLING,
                                           ADDRESS_BILLING_COUNTRY,
                                           gfx::NativeView(),
                                           gfx::Rect(),
                                           ASCIIToUTF16("France"),
                                           true);
}

TEST_F(AutofillDialogControllerTest, UsValidationRulesLoadedForJpOnlyProfile) {
  ResetControllerWithFormData(DefaultFormData());
  AutofillProfile jp_profile(base::GenerateGUID(), kSettingsOrigin);
  jp_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("JP"));
  controller()->GetTestingManager()->AddTestingProfile(&jp_profile);
  EXPECT_CALL(*controller()->GetMockValidator(), LoadRules("US"));
  EXPECT_CALL(*controller()->GetMockValidator(),
              LoadRules("JP")).Times(AtLeast(1));
  controller()->Show();
}

TEST_F(AutofillDialogControllerTest, InvalidWhenRulesNotReady) {
  // Select "Add new shipping address...".
  controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(1);

  // If the rules haven't loaded yet, validation errors should show on submit.
  EXPECT_CALL(*controller()->GetMockValidator(),
              ValidateAddress(CountryCodeMatcher("US"), _, _)).
              WillRepeatedly(Return(AddressValidator::RULES_NOT_READY));

  FieldValueMap inputs;
  inputs[ADDRESS_HOME_ZIP] = ASCIIToUTF16("1234");
  inputs[ADDRESS_HOME_COUNTRY] = ASCIIToUTF16("United States");

  ValidityMessages messages =
      controller()->InputsAreValid(SECTION_SHIPPING, inputs);
  EXPECT_FALSE(messages.GetMessageOrDefault(ADDRESS_HOME_ZIP).text.empty());
  EXPECT_FALSE(messages.HasSureError(ADDRESS_HOME_ZIP));
  // Country should never show an error message as it's always valid.
  EXPECT_TRUE(messages.GetMessageOrDefault(ADDRESS_HOME_COUNTRY).text.empty());
}

TEST_F(AutofillDialogControllerTest, ValidButUnverifiedWhenRulesFail) {
  SwitchToAutofill();

  // Add suggestions so the credit card and billing sections aren't showing
  // their manual inputs (to isolate to just shipping).
  AutofillProfile verified_profile(test::GetVerifiedProfile());
  controller()->GetTestingManager()->AddTestingProfile(&verified_profile);
  CreditCard verified_card(test::GetVerifiedCreditCard());
  controller()->GetTestingManager()->AddTestingCreditCard(&verified_card);

  // Select "Add new shipping address...".
  controller()->MenuModelForSection(SECTION_SHIPPING)->ActivatedAt(2);

  // If the rules are unavailable, validation errors should not show.
  EXPECT_CALL(*controller()->GetMockValidator(),
              ValidateAddress(CountryCodeMatcher("US"), _, _)).
              WillRepeatedly(Return(AddressValidator::RULES_UNAVAILABLE));

  FieldValueMap outputs;
  AutofillProfile full_profile(test::GetFullProfile());
  const DetailInputs& inputs =
      controller()->RequestedFieldsForSection(SECTION_SHIPPING);
  for (size_t i = 0; i < inputs.size(); ++i) {
    const ServerFieldType type = inputs[i].type;
    outputs[type] = full_profile.GetInfo(AutofillType(type), "en-US");
  }
  controller()->GetView()->SetUserInput(SECTION_SHIPPING, outputs);
  controller()->GetView()->CheckSaveDetailsLocallyCheckbox(true);
  controller()->OnAccept();

  // Profiles saved while rules are unavailable shouldn't be verified.
  const AutofillProfile& imported_profile =
      controller()->GetTestingManager()->imported_profile();
  ASSERT_EQ(imported_profile.GetInfo(AutofillType(NAME_FULL), "en-US"),
            full_profile.GetInfo(AutofillType(NAME_FULL), "en-US"));
  EXPECT_EQ(imported_profile.origin(), GURL(kSourceUrl).GetOrigin().spec());
  EXPECT_FALSE(imported_profile.IsVerified());
}

TEST_F(AutofillDialogControllerTest, LimitedCountryChoices) {
  ui::ComboboxModel* shipping_country_model =
      controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
  const int default_number_of_countries =
      shipping_country_model->GetItemCount();
  // We show a lot of countries by default, but the exact number doesn't matter.
  EXPECT_GT(default_number_of_countries, 50);

  // Create a form data that simulates:
  //   <select autocomplete="billing country">
  //     <option value="AU">Down Under</option>
  //     <option value="">fR</option>  <!-- Case doesn't matter -->
  //     <option value="GRMNY">Germany</option>
  //   </select>
  // Only country codes are respected, whether they're in value or the option's
  // text content. Thus the first two options should be recognized.
  FormData form_data;
  FormFieldData field;
  field.autocomplete_attribute = "billing country";
  field.option_contents.push_back(ASCIIToUTF16("Down Under"));
  field.option_values.push_back(ASCIIToUTF16("AU"));
  field.option_contents.push_back(ASCIIToUTF16("Fr"));
  field.option_values.push_back(ASCIIToUTF16(""));
  field.option_contents.push_back(ASCIIToUTF16("Germany"));
  field.option_values.push_back(ASCIIToUTF16("GRMNY"));

  FormFieldData cc_field;
  cc_field.autocomplete_attribute = "cc-csc";

  form_data.fields.push_back(field);
  form_data.fields.push_back(cc_field);
  ResetControllerWithFormData(form_data);
  controller()->Show();

  // Shipping model shouldn't have changed.
  shipping_country_model =
      controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
  EXPECT_EQ(default_number_of_countries,
            shipping_country_model->GetItemCount());
  // Billing model now only has two items.
  ui::ComboboxModel* billing_country_model =
      controller()->ComboboxModelForAutofillType(ADDRESS_BILLING_COUNTRY);
  ASSERT_EQ(2, billing_country_model->GetItemCount());
  EXPECT_EQ(billing_country_model->GetItemAt(0), ASCIIToUTF16("Australia"));
  EXPECT_EQ(billing_country_model->GetItemAt(1), ASCIIToUTF16("France"));

  // Make sure it also applies to profile suggestions.
  AutofillProfile us_profile(test::GetVerifiedProfile());
  us_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("US"));
  controller()->GetTestingManager()->AddTestingProfile(&us_profile);
  // Don't show a suggestion if the only one that exists is disabled.
  EXPECT_FALSE(
      controller()->SuggestionStateForSection(SECTION_BILLING).visible);

  // Add a profile with an acceptable country; suggestion should be shown.
  ResetControllerWithFormData(form_data);
  controller()->Show();
  AutofillProfile au_profile(test::GetVerifiedProfile2());
  au_profile.SetRawInfo(ADDRESS_HOME_COUNTRY, ASCIIToUTF16("AU"));
  controller()->GetTestingManager()->AddTestingProfile(&us_profile);
  controller()->GetTestingManager()->AddTestingProfile(&au_profile);
  ui::MenuModel* model = controller()->MenuModelForSection(SECTION_BILLING);
  ASSERT_TRUE(model);
  EXPECT_EQ(4, model->GetItemCount());
  EXPECT_FALSE(model->IsEnabledAt(0));
  EXPECT_TRUE(model->IsEnabledAt(1));

  // Add <input type="text" autocomplete="billing country"></input>
  // This should open up selection of all countries again.
  FormFieldData field2;
  field2.autocomplete_attribute = "billing country";
  form_data.fields.push_back(field2);
  ResetControllerWithFormData(form_data);
  controller()->Show();

  billing_country_model =
      controller()->ComboboxModelForAutofillType(ADDRESS_BILLING_COUNTRY);
  EXPECT_EQ(default_number_of_countries,
            billing_country_model->GetItemCount());
}

TEST_F(AutofillDialogControllerTest, WalletUnsupportedCountries) {
  // Create a form data that simulates:
  //   <select autocomplete="billing country">
  //     <option value="IQ">Iraq</option>
  //     <option value="MX">Mexico</option>
  //   </select>
  // i.e. contains a mix of supported and unsupported countries.
  FormData form_data;
  FormFieldData field;
  field.autocomplete_attribute = "shipping country";
  field.option_contents.push_back(ASCIIToUTF16("Iraq"));
  field.option_values.push_back(ASCIIToUTF16("IQ"));
  field.option_contents.push_back(ASCIIToUTF16("Mexico"));
  field.option_values.push_back(ASCIIToUTF16("MX"));

  FormFieldData cc_field;
  cc_field.autocomplete_attribute = "cc-csc";

  form_data.fields.push_back(field);
  form_data.fields.push_back(cc_field);
  ResetControllerWithFormData(form_data);
  controller()->Show();

  ui::ComboboxModel* shipping_country_model =
      controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
  ASSERT_EQ(2, shipping_country_model->GetItemCount());
  EXPECT_EQ(shipping_country_model->GetItemAt(0), ASCIIToUTF16("Iraq"));
  EXPECT_EQ(shipping_country_model->GetItemAt(1), ASCIIToUTF16("Mexico"));

  // Switch to Wallet, add limitations.
  SetUpControllerWithFormData(form_data);
  SwitchToWallet();
  scoped_ptr<wallet::WalletItems> wallet_items =
      wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddAllowedShippingCountry("MX");
  wallet_items->AddAllowedShippingCountry("GB");
  controller()->OnDidGetWalletItems(wallet_items.Pass());

  // Only the intersection is available.
  EXPECT_TRUE(controller()->IsPayingWithWallet());
  shipping_country_model =
      controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
  ASSERT_EQ(1, shipping_country_model->GetItemCount());
  EXPECT_EQ(shipping_country_model->GetItemAt(0), ASCIIToUTF16("Mexico"));

  // Empty intersection; Wallet's automatically disabled.
  wallet_items = wallet::GetTestWalletItems(wallet::AMEX_DISALLOWED);
  wallet_items->AddAllowedShippingCountry("CA");
  wallet_items->AddAllowedShippingCountry("GB");
  controller()->OnDidGetWalletItems(wallet_items.Pass());
  EXPECT_FALSE(controller()->IsPayingWithWallet());

  // Since it's disabled, we revert to accepting all the countries.
  shipping_country_model =
      controller()->ComboboxModelForAutofillType(ADDRESS_HOME_COUNTRY);
  ASSERT_EQ(2, shipping_country_model->GetItemCount());
  EXPECT_EQ(shipping_country_model->GetItemAt(0), ASCIIToUTF16("Iraq"));
  EXPECT_EQ(shipping_country_model->GetItemAt(1), ASCIIToUTF16("Mexico"));
}

// http://crbug.com/388018
TEST_F(AutofillDialogControllerTest, NoCountryChoices) {
  // Create a form data that simulates:
  //   <select autocomplete="billing country">
  //     <option value="ATL">Atlantis</option>
  //     <option value="ELD">Eldorado</option>
  //   </select>
  // i.e. contains a list of no valid countries.
  FormData form_data;
  FormFieldData field;
  field.autocomplete_attribute = "billing country";
  field.option_contents.push_back(ASCIIToUTF16("Atlantis"));
  field.option_values.push_back(ASCIIToUTF16("ATL"));
  field.option_contents.push_back(ASCIIToUTF16("Eldorado"));
  field.option_values.push_back(ASCIIToUTF16("ELD"));

  FormFieldData cc_field;
  cc_field.autocomplete_attribute = "cc-csc";

  form_data.fields.push_back(field);
  form_data.fields.push_back(cc_field);
  ResetControllerWithFormData(form_data);
  controller()->Show();

  // Controller aborts and self destructs.
  EXPECT_EQ(0, controller());
}

TEST_F(AutofillDialogControllerTest, LimitedCcChoices) {
  SwitchToAutofill();
  // Typically, MC and Visa are both valid.
  ValidateCCNumber(SECTION_CC, kTestCCNumberMaster, true);
  ValidateCCNumber(SECTION_CC, kTestCCNumberVisa, true);

  FormData form_data;
  FormFieldData field;
  field.autocomplete_attribute = "billing cc-type";
  field.option_contents.push_back(ASCIIToUTF16("Visa"));
  field.option_values.push_back(ASCIIToUTF16("V"));
  field.option_contents.push_back(ASCIIToUTF16("Amex"));
  field.option_values.push_back(ASCIIToUTF16("AX"));
  form_data.fields.push_back(field);
  ResetControllerWithFormData(form_data);
  controller()->Show();

  // MC is not valid because it's missing from FormData.
  EXPECT_EQ(l10n_util::GetStringUTF16(
                IDS_AUTOFILL_DIALOG_VALIDATION_UNACCEPTED_MASTERCARD),
            ValidateCCNumber(SECTION_CC, kTestCCNumberMaster, false));
  ValidateCCNumber(SECTION_CC, kTestCCNumberVisa, true);

  CreditCard visa_card(test::GetVerifiedCreditCard());
  CreditCard amex_card(test::GetVerifiedCreditCard2());

  CreditCard master_card(base::GenerateGUID(), "chrome settings");
  test::SetCreditCardInfo(
      &master_card, "Mr Foo", "5105105105105100", "07", "2099");

  controller()->GetTestingManager()->AddTestingCreditCard(&visa_card);
  controller()->GetTestingManager()->AddTestingCreditCard(&amex_card);
  controller()->GetTestingManager()->AddTestingCreditCard(&master_card);

  // The stored MC is disabled in the dropdown.
  ui::MenuModel* model = controller()->MenuModelForSection(SECTION_CC);
  ASSERT_TRUE(model);
  ASSERT_EQ(5, model->GetItemCount());
  EXPECT_TRUE(model->IsEnabledAt(0));
  EXPECT_TRUE(model->IsEnabledAt(1));
  EXPECT_FALSE(model->IsEnabledAt(2));
  EXPECT_TRUE(model->IsEnabledAt(3));
  EXPECT_TRUE(model->IsEnabledAt(4));

  // No MC; Wallet is disabled.
  SetUpControllerWithFormData(form_data);
  EXPECT_FALSE(controller()->IsPayingWithWallet());

  // In Autofill mode, Discover is disallowed because it's not in FormData.
  ValidateCCNumber(SECTION_CC, kTestCCNumberDiscover, false);

  field.option_contents.push_back(ASCIIToUTF16("Mastercard"));
  field.option_values.push_back(ASCIIToUTF16("Mastercard"));
  form_data.fields[0] = field;

  // Add MC to FormData; Wallet is enabled.
  SetUpControllerWithFormData(form_data);
  EXPECT_TRUE(controller()->IsPayingWithWallet());
  // Even though Discover isn't in FormData, it's allowed because Wallet always
  // generates a MC Virtual card.
  ValidateCCNumber(SECTION_CC_BILLING, kTestCCNumberDiscover, true);
}

TEST_F(AutofillDialogControllerTest, SuggestCountrylessProfiles) {
  SwitchToAutofill();

  FieldValueMap outputs;
  outputs[ADDRESS_HOME_COUNTRY] = ASCIIToUTF16("US");
  controller()->GetView()->SetUserInput(SECTION_SHIPPING, outputs);

  AutofillProfile profile(test::GetVerifiedProfile());
  profile.SetRawInfo(NAME_FULL, ASCIIToUTF16("The Man Without a Country"));
  profile.SetRawInfo(ADDRESS_HOME_COUNTRY, base::string16());
  controller()->GetTestingManager()->AddTestingProfile(&profile);

  controller()->UserEditedOrActivatedInput(
      SECTION_SHIPPING,
      NAME_FULL,
      gfx::NativeView(),
      gfx::Rect(),
      profile.GetRawInfo(NAME_FULL).substr(0, 1),
      true);
  EXPECT_EQ(NAME_FULL, controller()->popup_input_type());
}

TEST_F(AutofillDialogControllerTest, SwitchFromWalletWithFirstName) {
  controller()->MenuModelForSection(SECTION_CC_BILLING)->ActivatedAt(2);

  FieldValueMap outputs;
  outputs[NAME_FULL] = ASCIIToUTF16("madonna");
  controller()->GetView()->SetUserInput(SECTION_CC_BILLING, outputs);

  ASSERT_NO_FATAL_FAILURE(SwitchToAutofill());
}

// Regression test for http://crbug.com/382777
TEST_F(AutofillDialogControllerTest, WalletBillingCountry) {
  FormFieldData cc_field;
  cc_field.autocomplete_attribute = "cc-number";
  FormFieldData billing_country, billing_country_name, shipping_country,
      shipping_country_name;
  billing_country.autocomplete_attribute = "billing country";
  billing_country_name.autocomplete_attribute = "billing country-name";
  shipping_country.autocomplete_attribute = "shipping country";
  shipping_country_name.autocomplete_attribute = "shipping country-name";

  FormData form_data;
  form_data.fields.push_back(cc_field);
  form_data.fields.push_back(billing_country);
  form_data.fields.push_back(billing_country_name);
  form_data.fields.push_back(shipping_country);
  form_data.fields.push_back(shipping_country_name);

  SetUpControllerWithFormData(form_data);
  AcceptAndLoadFakeFingerprint();
  controller()->OnDidGetFullWallet(wallet::GetTestFullWallet());
  controller()->ForceFinishSubmit();

  ASSERT_EQ(5U, form_structure()->field_count());
  EXPECT_EQ(ADDRESS_HOME_COUNTRY,
            form_structure()->field(1)->Type().GetStorableType());
  EXPECT_EQ(ASCIIToUTF16("US"), form_structure()->field(1)->value);
  EXPECT_EQ(ADDRESS_HOME_COUNTRY,
            form_structure()->field(2)->Type().GetStorableType());
  EXPECT_EQ(ASCIIToUTF16("United States"), form_structure()->field(2)->value);
  EXPECT_EQ(ADDRESS_HOME_COUNTRY,
            form_structure()->field(3)->Type().GetStorableType());
  EXPECT_EQ(ASCIIToUTF16("US"), form_structure()->field(3)->value);
  EXPECT_EQ(ADDRESS_HOME_COUNTRY,
            form_structure()->field(4)->Type().GetStorableType());
  EXPECT_EQ(ASCIIToUTF16("United States"), form_structure()->field(4)->value);
}

}  // namespace autofill