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
|
// 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 "chrome/browser/history/history_backend.h"
#include <algorithm>
#include <set>
#include <vector>
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/path_service.h"
#include "base/prefs/pref_service.h"
#include "base/run_loop.h"
#include "base/strings/string16.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/history/content_visit_delegate.h"
#include "chrome/browser/history/history_service.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/history/in_memory_history_backend.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "components/favicon_base/favicon_usage_data.h"
#include "components/history/core/browser/history_constants.h"
#include "components/history/core/browser/history_database_params.h"
#include "components/history/core/browser/history_service_observer.h"
#include "components/history/core/browser/in_memory_database.h"
#include "components/history/core/browser/keyword_search_term.h"
#include "components/history/core/browser/visit_filter.h"
#include "components/history/core/test/history_client_fake_bookmarks.h"
#include "components/history/core/test/test_history_database.h"
#include "content/public/test/test_browser_thread.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "url/gurl.h"
using base::Time;
// This file only tests functionality where it is most convenient to call the
// backend directly. Most of the history backend functions are tested by the
// history unit test. Because of the elaborate callbacks involved, this is no
// harder than calling it directly for many things.
namespace {
const int kTinyEdgeSize = 10;
const int kSmallEdgeSize = 16;
const int kLargeEdgeSize = 32;
const gfx::Size kTinySize = gfx::Size(kTinyEdgeSize, kTinyEdgeSize);
const gfx::Size kSmallSize = gfx::Size(kSmallEdgeSize, kSmallEdgeSize);
const gfx::Size kLargeSize = gfx::Size(kLargeEdgeSize, kLargeEdgeSize);
typedef base::Callback<void(const history::URLRow*,
const history::URLRow*,
const history::URLRow*)>
SimulateNotificationCallback;
class HistoryClientMock : public history::HistoryClientFakeBookmarks {
public:
MOCK_METHOD0(BlockUntilBookmarksLoaded, void());
};
void SimulateNotificationURLVisited(history::HistoryServiceObserver* observer,
const history::URLRow* row1,
const history::URLRow* row2,
const history::URLRow* row3) {
history::URLRows rows;
rows.push_back(*row1);
if (row2)
rows.push_back(*row2);
if (row3)
rows.push_back(*row3);
base::Time visit_time;
history::RedirectList redirects;
for (const auto& row : rows) {
observer->OnURLVisited(
nullptr, ui::PAGE_TRANSITION_LINK, row, redirects, visit_time);
}
}
void SimulateNotificationURLsModified(history::HistoryServiceObserver* observer,
const history::URLRow* row1,
const history::URLRow* row2,
const history::URLRow* row3) {
history::URLRows rows;
rows.push_back(*row1);
if (row2)
rows.push_back(*row2);
if (row3)
rows.push_back(*row3);
observer->OnURLsModified(nullptr, rows);
}
} // namespace
namespace history {
class HistoryBackendTestBase;
// This must be a separate object since HistoryBackend manages its lifetime.
// This just forwards the messages we're interested in to the test object.
class HistoryBackendTestDelegate : public HistoryBackend::Delegate {
public:
explicit HistoryBackendTestDelegate(HistoryBackendTestBase* test)
: test_(test) {}
void NotifyProfileError(sql::InitStatus init_status) override {}
void SetInMemoryBackend(scoped_ptr<InMemoryHistoryBackend> backend) override;
void NotifyAddVisit(const BriefVisitInfo& info) override {}
void NotifyFaviconChanged(const std::set<GURL>& urls) override;
void NotifyURLVisited(ui::PageTransition transition,
const URLRow& row,
const RedirectList& redirects,
base::Time visit_time) override;
void NotifyURLsModified(const URLRows& changed_urls) override;
void NotifyURLsDeleted(bool all_history,
bool expired,
const URLRows& deleted_rows,
const std::set<GURL>& favicon_urls) override;
void NotifyKeywordSearchTermUpdated(const URLRow& row,
KeywordID keyword_id,
const base::string16& term) override;
void NotifyKeywordSearchTermDeleted(URLID url_id) override;
void DBLoaded() override;
private:
// Not owned by us.
HistoryBackendTestBase* test_;
DISALLOW_COPY_AND_ASSIGN(HistoryBackendTestDelegate);
};
class HistoryBackendTestBase : public testing::Test {
public:
typedef std::vector<std::pair<ui::PageTransition, URLRow>> URLVisitedList;
typedef std::vector<URLRows> URLsModifiedList;
typedef std::vector<std::pair<bool, bool>> URLsDeletedList;
HistoryBackendTestBase()
: loaded_(false),
favicon_changed_notifications_(0),
ui_thread_(content::BrowserThread::UI, &message_loop_) {}
~HistoryBackendTestBase() override {
}
protected:
int favicon_changed_notifications() const {
return favicon_changed_notifications_;
}
void ClearFaviconChangedNotificationCounter() {
favicon_changed_notifications_ = 0;
}
int num_url_visited_notifications() const {
return url_visited_notifications_.size();
}
const URLVisitedList& url_visited_notifications() const {
return url_visited_notifications_;
}
int num_urls_modified_notifications() const {
return urls_modified_notifications_.size();
}
const URLsModifiedList& urls_modified_notifications() const {
return urls_modified_notifications_;
}
const URLsDeletedList& urls_deleted_notifications() const {
return urls_deleted_notifications_;
}
void ClearBroadcastedNotifications() {
url_visited_notifications_.clear();
urls_modified_notifications_.clear();
urls_deleted_notifications_.clear();
}
base::FilePath test_dir() {
return test_dir_;
}
void NotifyFaviconChanged(const std::set<GURL>& changed_favicons) {
++favicon_changed_notifications_;
}
void NotifyURLVisited(ui::PageTransition transition,
const URLRow& row,
const RedirectList& redirects,
base::Time visit_time) {
// Send the notifications directly to the in-memory database.
mem_backend_->OnURLVisited(nullptr, transition, row, redirects, visit_time);
url_visited_notifications_.push_back(std::make_pair(transition, row));
}
void NotifyURLsModified(const URLRows& changed_urls) {
// Send the notifications directly to the in-memory database.
mem_backend_->OnURLsModified(nullptr, changed_urls);
urls_modified_notifications_.push_back(changed_urls);
}
void NotifyURLsDeleted(bool all_history,
bool expired,
const URLRows& deleted_rows,
const std::set<GURL>& favicon_urls) {
mem_backend_->OnURLsDeleted(nullptr, all_history, expired, deleted_rows,
favicon_urls);
urls_deleted_notifications_.push_back(std::make_pair(all_history, expired));
}
void NotifyKeywordSearchTermUpdated(const URLRow& row,
KeywordID keyword_id,
const base::string16& term) {
mem_backend_->OnKeywordSearchTermUpdated(nullptr, row, keyword_id, term);
}
void NotifyKeywordSearchTermDeleted(URLID url_id) {
mem_backend_->OnKeywordSearchTermDeleted(nullptr, url_id);
}
history::HistoryClientFakeBookmarks history_client_;
scoped_refptr<HistoryBackend> backend_; // Will be NULL on init failure.
scoped_ptr<InMemoryHistoryBackend> mem_backend_;
bool loaded_;
private:
friend class HistoryBackendTestDelegate;
// testing::Test
void SetUp() override {
ClearFaviconChangedNotificationCounter();
if (!base::CreateNewTempDirectory(FILE_PATH_LITERAL("BackendTest"),
&test_dir_))
return;
backend_ = new HistoryBackend(new HistoryBackendTestDelegate(this),
&history_client_);
backend_->Init(std::string(), false,
TestHistoryDatabaseParamsForPath(test_dir_));
}
void TearDown() override {
if (backend_.get())
backend_->Closing();
backend_ = NULL;
mem_backend_.reset();
base::DeleteFile(test_dir_, true);
base::RunLoop().RunUntilIdle();
history_client_.ClearAllBookmarks();
}
void SetInMemoryBackend(scoped_ptr<InMemoryHistoryBackend> backend) {
mem_backend_.swap(backend);
}
// The types and details of notifications which were broadcasted.
int favicon_changed_notifications_;
URLVisitedList url_visited_notifications_;
URLsModifiedList urls_modified_notifications_;
URLsDeletedList urls_deleted_notifications_;
base::MessageLoop message_loop_;
base::FilePath test_dir_;
content::TestBrowserThread ui_thread_;
DISALLOW_COPY_AND_ASSIGN(HistoryBackendTestBase);
};
void HistoryBackendTestDelegate::SetInMemoryBackend(
scoped_ptr<InMemoryHistoryBackend> backend) {
test_->SetInMemoryBackend(backend.Pass());
}
void HistoryBackendTestDelegate::NotifyFaviconChanged(
const std::set<GURL>& changed_favicons) {
test_->NotifyFaviconChanged(changed_favicons);
}
void HistoryBackendTestDelegate::NotifyURLVisited(ui::PageTransition transition,
const URLRow& row,
const RedirectList& redirects,
base::Time visit_time) {
test_->NotifyURLVisited(transition, row, redirects, visit_time);
}
void HistoryBackendTestDelegate::NotifyURLsModified(
const URLRows& changed_urls) {
test_->NotifyURLsModified(changed_urls);
}
void HistoryBackendTestDelegate::NotifyURLsDeleted(
bool all_history,
bool expired,
const URLRows& deleted_rows,
const std::set<GURL>& favicon_urls) {
test_->NotifyURLsDeleted(all_history, expired, deleted_rows, favicon_urls);
}
void HistoryBackendTestDelegate::NotifyKeywordSearchTermUpdated(
const URLRow& row,
KeywordID keyword_id,
const base::string16& term) {
test_->NotifyKeywordSearchTermUpdated(row, keyword_id, term);
}
void HistoryBackendTestDelegate::NotifyKeywordSearchTermDeleted(URLID url_id) {
test_->NotifyKeywordSearchTermDeleted(url_id);
}
void HistoryBackendTestDelegate::DBLoaded() {
test_->loaded_ = true;
}
class HistoryBackendTest : public HistoryBackendTestBase {
public:
HistoryBackendTest() {}
~HistoryBackendTest() override {}
protected:
void AddRedirectChain(const char* sequence[], int nav_entry_id) {
AddRedirectChainWithTransitionAndTime(sequence, nav_entry_id,
ui::PAGE_TRANSITION_LINK,
Time::Now());
}
void AddRedirectChainWithTransitionAndTime(
const char* sequence[],
int nav_entry_id,
ui::PageTransition transition,
base::Time time) {
history::RedirectList redirects;
for (int i = 0; sequence[i] != NULL; ++i)
redirects.push_back(GURL(sequence[i]));
ContextID context_id = reinterpret_cast<ContextID>(1);
history::HistoryAddPageArgs request(
redirects.back(), time, context_id, nav_entry_id, GURL(),
redirects, transition, history::SOURCE_BROWSED,
true);
backend_->AddPage(request);
}
// Adds CLIENT_REDIRECT page transition.
// |url1| is the source URL and |url2| is the destination.
// |did_replace| is true if the transition is non-user initiated and the
// navigation entry for |url2| has replaced that for |url1|. The possibly
// updated transition code of the visit records for |url1| and |url2| is
// returned by filling in |*transition1| and |*transition2|, respectively.
// |time| is a time of the redirect.
void AddClientRedirect(const GURL& url1, const GURL& url2, bool did_replace,
base::Time time,
int* transition1, int* transition2) {
ContextID dummy_context_id = reinterpret_cast<ContextID>(0x87654321);
history::RedirectList redirects;
if (url1.is_valid())
redirects.push_back(url1);
if (url2.is_valid())
redirects.push_back(url2);
HistoryAddPageArgs request(
url2, time, dummy_context_id, 0, url1,
redirects, ui::PAGE_TRANSITION_CLIENT_REDIRECT,
history::SOURCE_BROWSED, did_replace);
backend_->AddPage(request);
*transition1 = GetTransition(url1);
*transition2 = GetTransition(url2);
}
int GetTransition(const GURL& url) {
if (!url.is_valid())
return 0;
URLRow row;
URLID id = backend_->db()->GetRowForURL(url, &row);
VisitVector visits;
EXPECT_TRUE(backend_->db()->GetVisitsForURL(id, &visits));
return visits[0].transition;
}
// Returns a vector with the small and large edge sizes.
const std::vector<int> GetEdgeSizesSmallAndLarge() {
std::vector<int> sizes_small_and_large;
sizes_small_and_large.push_back(kSmallEdgeSize);
sizes_small_and_large.push_back(kLargeEdgeSize);
return sizes_small_and_large;
}
// Returns the number of icon mappings of |icon_type| to |page_url|.
size_t NumIconMappingsForPageURL(const GURL& page_url,
favicon_base::IconType icon_type) {
std::vector<IconMapping> icon_mappings;
backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url, icon_type,
&icon_mappings);
return icon_mappings.size();
}
// Returns the icon mappings for |page_url| sorted alphabetically by icon
// URL in ascending order. Returns true if there is at least one icon
// mapping.
bool GetSortedIconMappingsForPageURL(
const GURL& page_url,
std::vector<IconMapping>* icon_mappings) {
if (!backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url,
icon_mappings)) {
return false;
}
std::sort(icon_mappings->begin(), icon_mappings->end(),
[](const history::IconMapping& a, const history::IconMapping& b) {
return a.icon_url < b.icon_url;
});
return true;
}
// Returns the favicon bitmaps for |icon_id| sorted by pixel size in
// ascending order. Returns true if there is at least one favicon bitmap.
bool GetSortedFaviconBitmaps(favicon_base::FaviconID icon_id,
std::vector<FaviconBitmap>* favicon_bitmaps) {
if (!backend_->thumbnail_db_->GetFaviconBitmaps(icon_id, favicon_bitmaps))
return false;
std::sort(
favicon_bitmaps->begin(), favicon_bitmaps->end(),
[](const history::FaviconBitmap& a, const history::FaviconBitmap& b) {
return a.pixel_size.GetArea() < b.pixel_size.GetArea();
});
return true;
}
// Returns true if there is exactly one favicon bitmap associated to
// |favicon_id|. If true, returns favicon bitmap in output parameter.
bool GetOnlyFaviconBitmap(const favicon_base::FaviconID icon_id,
FaviconBitmap* favicon_bitmap) {
std::vector<FaviconBitmap> favicon_bitmaps;
if (!backend_->thumbnail_db_->GetFaviconBitmaps(icon_id, &favicon_bitmaps))
return false;
if (favicon_bitmaps.size() != 1)
return false;
*favicon_bitmap = favicon_bitmaps[0];
return true;
}
// Creates an |edge_size|x|edge_size| bitmap of |color|.
SkBitmap CreateBitmap(SkColor color, int edge_size) {
SkBitmap bitmap;
bitmap.allocN32Pixels(edge_size, edge_size);
bitmap.eraseColor(color);
return bitmap;
}
// Returns true if |bitmap_data| is equal to |expected_data|.
bool BitmapDataEqual(char expected_data,
scoped_refptr<base::RefCountedMemory> bitmap_data) {
return bitmap_data.get() &&
bitmap_data->size() == 1u &&
*bitmap_data->front() == expected_data;
}
// Returns true if |bitmap_data| is of |color|.
bool BitmapColorEqual(SkColor expected_color,
scoped_refptr<base::RefCountedMemory> bitmap_data) {
SkBitmap bitmap;
if (!gfx::PNGCodec::Decode(
bitmap_data->front(), bitmap_data->size(), &bitmap))
return false;
SkAutoLockPixels bitmap_lock(bitmap);
return expected_color == bitmap.getColor(0, 0);
}
private:
DISALLOW_COPY_AND_ASSIGN(HistoryBackendTest);
};
class InMemoryHistoryBackendTest : public HistoryBackendTestBase {
public:
InMemoryHistoryBackendTest() {}
~InMemoryHistoryBackendTest() override {}
protected:
void SimulateNotificationURLsDeleted(const URLRow* row1,
const URLRow* row2 = NULL,
const URLRow* row3 = NULL) {
URLRows rows;
rows.push_back(*row1);
if (row2) rows.push_back(*row2);
if (row3) rows.push_back(*row3);
NotifyURLsDeleted(false, false, rows, std::set<GURL>());
}
size_t GetNumberOfMatchingSearchTerms(const int keyword_id,
const base::string16& prefix) {
std::vector<KeywordSearchTermVisit> matching_terms;
mem_backend_->db()->GetMostRecentKeywordSearchTerms(
keyword_id, prefix, 1, &matching_terms);
return matching_terms.size();
}
static URLRow CreateTestTypedURL() {
URLRow url_row(GURL("https://www.google.com/"));
url_row.set_id(10);
url_row.set_title(base::UTF8ToUTF16("Google Search"));
url_row.set_typed_count(1);
url_row.set_visit_count(1);
url_row.set_last_visit(Time::Now() - base::TimeDelta::FromHours(1));
return url_row;
}
static URLRow CreateAnotherTestTypedURL() {
URLRow url_row(GURL("https://maps.google.com/"));
url_row.set_id(20);
url_row.set_title(base::UTF8ToUTF16("Google Maps"));
url_row.set_typed_count(2);
url_row.set_visit_count(3);
url_row.set_last_visit(Time::Now() - base::TimeDelta::FromHours(2));
return url_row;
}
static URLRow CreateTestNonTypedURL() {
URLRow url_row(GURL("https://news.google.com/"));
url_row.set_id(30);
url_row.set_title(base::UTF8ToUTF16("Google News"));
url_row.set_visit_count(5);
url_row.set_last_visit(Time::Now() - base::TimeDelta::FromHours(3));
return url_row;
}
void PopulateTestURLsAndSearchTerms(URLRow* row1,
URLRow* row2,
const base::string16& term1,
const base::string16& term2);
void TestAddingAndChangingURLRows(
const SimulateNotificationCallback& callback);
static const KeywordID kTestKeywordId;
static const char kTestSearchTerm1[];
static const char kTestSearchTerm2[];
private:
DISALLOW_COPY_AND_ASSIGN(InMemoryHistoryBackendTest);
};
const KeywordID InMemoryHistoryBackendTest::kTestKeywordId = 42;
const char InMemoryHistoryBackendTest::kTestSearchTerm1[] = "banana";
const char InMemoryHistoryBackendTest::kTestSearchTerm2[] = "orange";
// http://crbug.com/114287
#if defined(OS_WIN)
#define MAYBE_Loaded DISABLED_Loaded
#else
#define MAYBE_Loaded Loaded
#endif // defined(OS_WIN)
TEST_F(HistoryBackendTest, MAYBE_Loaded) {
ASSERT_TRUE(backend_.get());
ASSERT_TRUE(loaded_);
}
TEST_F(HistoryBackendTest, DeleteAll) {
ASSERT_TRUE(backend_.get());
// Add two favicons, each with two bitmaps. Note that we add favicon2 before
// adding favicon1. This is so that favicon1 one gets ID 2 autoassigned to
// the database, which will change when the other one is deleted. This way
// we can test that updating works properly.
GURL favicon_url1("http://www.google.com/favicon.ico");
GURL favicon_url2("http://news.google.com/favicon.ico");
favicon_base::FaviconID favicon2 =
backend_->thumbnail_db_->AddFavicon(favicon_url2, favicon_base::FAVICON);
favicon_base::FaviconID favicon1 =
backend_->thumbnail_db_->AddFavicon(favicon_url1, favicon_base::FAVICON);
std::vector<unsigned char> data;
data.push_back('a');
EXPECT_TRUE(backend_->thumbnail_db_->AddFaviconBitmap(favicon1,
new base::RefCountedBytes(data), Time::Now(), kSmallSize));
data[0] = 'b';
EXPECT_TRUE(backend_->thumbnail_db_->AddFaviconBitmap(favicon1,
new base::RefCountedBytes(data), Time::Now(), kLargeSize));
data[0] = 'c';
EXPECT_TRUE(backend_->thumbnail_db_->AddFaviconBitmap(favicon2,
new base::RefCountedBytes(data), Time::Now(), kSmallSize));
data[0] = 'd';
EXPECT_TRUE(backend_->thumbnail_db_->AddFaviconBitmap(favicon2,
new base::RefCountedBytes(data), Time::Now(), kLargeSize));
// First visit two URLs.
URLRow row1(GURL("http://www.google.com/"));
row1.set_visit_count(2);
row1.set_typed_count(1);
row1.set_last_visit(Time::Now());
backend_->thumbnail_db_->AddIconMapping(row1.url(), favicon1);
URLRow row2(GURL("http://news.google.com/"));
row2.set_visit_count(1);
row2.set_last_visit(Time::Now());
backend_->thumbnail_db_->AddIconMapping(row2.url(), favicon2);
URLRows rows;
rows.push_back(row2); // Reversed order for the same reason as favicons.
rows.push_back(row1);
backend_->AddPagesWithDetails(rows, history::SOURCE_BROWSED);
URLID row1_id = backend_->db_->GetRowForURL(row1.url(), NULL);
URLID row2_id = backend_->db_->GetRowForURL(row2.url(), NULL);
// Get the two visits for the URLs we just added.
VisitVector visits;
backend_->db_->GetVisitsForURL(row1_id, &visits);
ASSERT_EQ(1U, visits.size());
visits.clear();
backend_->db_->GetVisitsForURL(row2_id, &visits);
ASSERT_EQ(1U, visits.size());
// The in-memory backend should have been set and it should have gotten the
// typed URL.
ASSERT_TRUE(mem_backend_.get());
URLRow outrow1;
EXPECT_TRUE(mem_backend_->db_->GetRowForURL(row1.url(), NULL));
// Star row1.
history_client_.AddBookmark(row1.url());
// Now finally clear all history.
ClearBroadcastedNotifications();
backend_->DeleteAllHistory();
// The first URL should be preserved but the time should be cleared.
EXPECT_TRUE(backend_->db_->GetRowForURL(row1.url(), &outrow1));
EXPECT_EQ(row1.url(), outrow1.url());
EXPECT_EQ(0, outrow1.visit_count());
EXPECT_EQ(0, outrow1.typed_count());
EXPECT_TRUE(Time() == outrow1.last_visit());
// The second row should be deleted.
URLRow outrow2;
EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &outrow2));
// All visits should be deleted for both URLs.
VisitVector all_visits;
backend_->db_->GetAllVisitsInRange(Time(), Time(), 0, &all_visits);
ASSERT_EQ(0U, all_visits.size());
// We should have a favicon and favicon bitmaps for the first URL only. We
// look them up by favicon URL since the IDs may have changed.
favicon_base::FaviconID out_favicon1 =
backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
favicon_url1, favicon_base::FAVICON, NULL);
EXPECT_TRUE(out_favicon1);
std::vector<FaviconBitmap> favicon_bitmaps;
EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(
out_favicon1, &favicon_bitmaps));
ASSERT_EQ(2u, favicon_bitmaps.size());
FaviconBitmap favicon_bitmap1 = favicon_bitmaps[0];
FaviconBitmap favicon_bitmap2 = favicon_bitmaps[1];
// Favicon bitmaps do not need to be in particular order.
if (favicon_bitmap1.pixel_size == kLargeSize) {
FaviconBitmap tmp_favicon_bitmap = favicon_bitmap1;
favicon_bitmap1 = favicon_bitmap2;
favicon_bitmap2 = tmp_favicon_bitmap;
}
EXPECT_TRUE(BitmapDataEqual('a', favicon_bitmap1.bitmap_data));
EXPECT_EQ(kSmallSize, favicon_bitmap1.pixel_size);
EXPECT_TRUE(BitmapDataEqual('b', favicon_bitmap2.bitmap_data));
EXPECT_EQ(kLargeSize, favicon_bitmap2.pixel_size);
favicon_base::FaviconID out_favicon2 =
backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
favicon_url2, favicon_base::FAVICON, NULL);
EXPECT_FALSE(out_favicon2) << "Favicon not deleted";
// The remaining URL should still reference the same favicon, even if its
// ID has changed.
std::vector<IconMapping> mappings;
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
outrow1.url(), favicon_base::FAVICON, &mappings));
EXPECT_EQ(1u, mappings.size());
EXPECT_EQ(out_favicon1, mappings[0].icon_id);
// The first URL should still be bookmarked.
EXPECT_TRUE(history_client_.IsBookmarked(row1.url()));
// Check that we fire the notification about all history having been deleted.
ASSERT_EQ(1u, urls_deleted_notifications().size());
EXPECT_TRUE(urls_deleted_notifications()[0].first);
EXPECT_FALSE(urls_deleted_notifications()[0].second);
}
// Checks that adding a visit, then calling DeleteAll, and then trying to add
// data for the visited page works. This can happen when clearing the history
// immediately after visiting a page.
TEST_F(HistoryBackendTest, DeleteAllThenAddData) {
ASSERT_TRUE(backend_.get());
Time visit_time = Time::Now();
GURL url("http://www.google.com/");
HistoryAddPageArgs request(url, visit_time, NULL, 0, GURL(),
history::RedirectList(),
ui::PAGE_TRANSITION_KEYWORD_GENERATED,
history::SOURCE_BROWSED, false);
backend_->AddPage(request);
// Check that a row was added.
URLRow outrow;
EXPECT_TRUE(backend_->db_->GetRowForURL(url, &outrow));
// Check that the visit was added.
VisitVector all_visits;
backend_->db_->GetAllVisitsInRange(Time(), Time(), 0, &all_visits);
ASSERT_EQ(1U, all_visits.size());
// Clear all history.
backend_->DeleteAllHistory();
// The row should be deleted.
EXPECT_FALSE(backend_->db_->GetRowForURL(url, &outrow));
// The visit should be deleted.
backend_->db_->GetAllVisitsInRange(Time(), Time(), 0, &all_visits);
ASSERT_EQ(0U, all_visits.size());
// Try and set the title.
backend_->SetPageTitle(url, base::UTF8ToUTF16("Title"));
// The row should still be deleted.
EXPECT_FALSE(backend_->db_->GetRowForURL(url, &outrow));
// The visit should still be deleted.
backend_->db_->GetAllVisitsInRange(Time(), Time(), 0, &all_visits);
ASSERT_EQ(0U, all_visits.size());
}
TEST_F(HistoryBackendTest, URLsNoLongerBookmarked) {
GURL favicon_url1("http://www.google.com/favicon.ico");
GURL favicon_url2("http://news.google.com/favicon.ico");
std::vector<unsigned char> data;
data.push_back('1');
favicon_base::FaviconID favicon1 =
backend_->thumbnail_db_->AddFavicon(favicon_url1,
favicon_base::FAVICON,
new base::RefCountedBytes(data),
Time::Now(),
gfx::Size());
data[0] = '2';
favicon_base::FaviconID favicon2 =
backend_->thumbnail_db_->AddFavicon(favicon_url2,
favicon_base::FAVICON,
new base::RefCountedBytes(data),
Time::Now(),
gfx::Size());
// First visit two URLs.
URLRow row1(GURL("http://www.google.com/"));
row1.set_visit_count(2);
row1.set_typed_count(1);
row1.set_last_visit(Time::Now());
EXPECT_TRUE(backend_->thumbnail_db_->AddIconMapping(row1.url(), favicon1));
URLRow row2(GURL("http://news.google.com/"));
row2.set_visit_count(1);
row2.set_last_visit(Time::Now());
EXPECT_TRUE(backend_->thumbnail_db_->AddIconMapping(row2.url(), favicon2));
URLRows rows;
rows.push_back(row2); // Reversed order for the same reason as favicons.
rows.push_back(row1);
backend_->AddPagesWithDetails(rows, history::SOURCE_BROWSED);
URLID row1_id = backend_->db_->GetRowForURL(row1.url(), NULL);
URLID row2_id = backend_->db_->GetRowForURL(row2.url(), NULL);
// Star the two URLs.
history_client_.AddBookmark(row1.url());
history_client_.AddBookmark(row2.url());
// Delete url 2. Because url 2 is starred this won't delete the URL, only
// the visits.
backend_->expirer_.DeleteURL(row2.url());
// Make sure url 2 is still valid, but has no visits.
URLRow tmp_url_row;
EXPECT_EQ(row2_id, backend_->db_->GetRowForURL(row2.url(), NULL));
VisitVector visits;
backend_->db_->GetVisitsForURL(row2_id, &visits);
EXPECT_EQ(0U, visits.size());
// The favicon should still be valid.
EXPECT_EQ(favicon2,
backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
favicon_url2, favicon_base::FAVICON, NULL));
// Unstar row2.
history_client_.DelBookmark(row2.url());
// Tell the backend it was unstarred. We have to explicitly do this as
// BookmarkModel isn't wired up to the backend during testing.
std::set<GURL> unstarred_urls;
unstarred_urls.insert(row2.url());
backend_->URLsNoLongerBookmarked(unstarred_urls);
// The URL should no longer exist.
EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &tmp_url_row));
// And the favicon should be deleted.
EXPECT_EQ(0,
backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
favicon_url2, favicon_base::FAVICON, NULL));
// Unstar row 1.
history_client_.DelBookmark(row1.url());
// Tell the backend it was unstarred. We have to explicitly do this as
// BookmarkModel isn't wired up to the backend during testing.
unstarred_urls.clear();
unstarred_urls.insert(row1.url());
backend_->URLsNoLongerBookmarked(unstarred_urls);
// The URL should still exist (because there were visits).
EXPECT_EQ(row1_id, backend_->db_->GetRowForURL(row1.url(), NULL));
// There should still be visits.
visits.clear();
backend_->db_->GetVisitsForURL(row1_id, &visits);
EXPECT_EQ(1U, visits.size());
// The favicon should still be valid.
EXPECT_EQ(favicon1,
backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
favicon_url1, favicon_base::FAVICON, NULL));
}
// Tests a handful of assertions for a navigation with a type of
// KEYWORD_GENERATED.
TEST_F(HistoryBackendTest, KeywordGenerated) {
ASSERT_TRUE(backend_.get());
GURL url("http://google.com");
Time visit_time = Time::Now() - base::TimeDelta::FromDays(1);
HistoryAddPageArgs request(url, visit_time, NULL, 0, GURL(),
history::RedirectList(),
ui::PAGE_TRANSITION_KEYWORD_GENERATED,
history::SOURCE_BROWSED, false);
backend_->AddPage(request);
// A row should have been added for the url.
URLRow row;
URLID url_id = backend_->db()->GetRowForURL(url, &row);
ASSERT_NE(0, url_id);
// The typed count should be 1.
ASSERT_EQ(1, row.typed_count());
// KEYWORD_GENERATED urls should not be added to the segment db.
std::string segment_name = VisitSegmentDatabase::ComputeSegmentName(url);
EXPECT_EQ(0, backend_->db()->GetSegmentNamed(segment_name));
// One visit should be added.
VisitVector visits;
EXPECT_TRUE(backend_->db()->GetVisitsForURL(url_id, &visits));
EXPECT_EQ(1U, visits.size());
// But no visible visits.
visits.clear();
QueryOptions query_options;
query_options.max_count = 1;
backend_->db()->GetVisibleVisitsInRange(query_options, &visits);
EXPECT_TRUE(visits.empty());
// Expire the visits.
std::set<GURL> restrict_urls;
backend_->expire_backend()->ExpireHistoryBetween(restrict_urls,
visit_time, Time::Now());
// The visit should have been nuked.
visits.clear();
EXPECT_TRUE(backend_->db()->GetVisitsForURL(url_id, &visits));
EXPECT_TRUE(visits.empty());
// As well as the url.
ASSERT_EQ(0, backend_->db()->GetRowForURL(url, &row));
}
TEST_F(HistoryBackendTest, ClientRedirect) {
ASSERT_TRUE(backend_.get());
int transition1;
int transition2;
// Initial transition to page A.
GURL url_a("http://google.com/a");
AddClientRedirect(GURL(), url_a, false, base::Time(),
&transition1, &transition2);
EXPECT_TRUE(transition2 & ui::PAGE_TRANSITION_CHAIN_END);
// User initiated redirect to page B.
GURL url_b("http://google.com/b");
AddClientRedirect(url_a, url_b, false, base::Time(),
&transition1, &transition2);
EXPECT_TRUE(transition1 & ui::PAGE_TRANSITION_CHAIN_END);
EXPECT_TRUE(transition2 & ui::PAGE_TRANSITION_CHAIN_END);
// Non-user initiated redirect to page C.
GURL url_c("http://google.com/c");
AddClientRedirect(url_b, url_c, true, base::Time(),
&transition1, &transition2);
EXPECT_FALSE(transition1 & ui::PAGE_TRANSITION_CHAIN_END);
EXPECT_TRUE(transition2 & ui::PAGE_TRANSITION_CHAIN_END);
}
TEST_F(HistoryBackendTest, AddPagesWithDetails) {
ASSERT_TRUE(backend_.get());
// Import one non-typed URL, and two recent and one expired typed URLs.
URLRow row1(GURL("https://news.google.com/"));
row1.set_visit_count(1);
row1.set_last_visit(Time::Now());
URLRow row2(GURL("https://www.google.com/"));
row2.set_typed_count(1);
row2.set_last_visit(Time::Now());
URLRow row3(GURL("https://mail.google.com/"));
row3.set_visit_count(1);
row3.set_typed_count(1);
row3.set_last_visit(Time::Now() - base::TimeDelta::FromDays(7 - 1));
URLRow row4(GURL("https://maps.google.com/"));
row4.set_visit_count(1);
row4.set_typed_count(1);
row4.set_last_visit(Time::Now() - base::TimeDelta::FromDays(365 + 2));
URLRows rows;
rows.push_back(row1);
rows.push_back(row2);
rows.push_back(row3);
rows.push_back(row4);
backend_->AddPagesWithDetails(rows, history::SOURCE_BROWSED);
// Verify that recent URLs have ended up in the main |db_|, while the already
// expired URL has been ignored.
URLRow stored_row1, stored_row2, stored_row3, stored_row4;
EXPECT_NE(0, backend_->db_->GetRowForURL(row1.url(), &stored_row1));
EXPECT_NE(0, backend_->db_->GetRowForURL(row2.url(), &stored_row2));
EXPECT_NE(0, backend_->db_->GetRowForURL(row3.url(), &stored_row3));
EXPECT_EQ(0, backend_->db_->GetRowForURL(row4.url(), &stored_row4));
// Ensure that a notification was fired for both typed and non-typed URLs.
// Further verify that the IDs in the notification are set to those that are
// in effect in the main database. The InMemoryHistoryBackend relies on this
// for caching.
ASSERT_EQ(1, num_urls_modified_notifications());
const URLRows& changed_urls = urls_modified_notifications()[0];
EXPECT_EQ(3u, changed_urls.size());
URLRows::const_iterator it_row1 =
std::find_if(changed_urls.begin(),
changed_urls.end(),
history::URLRow::URLRowHasURL(row1.url()));
ASSERT_NE(changed_urls.end(), it_row1);
EXPECT_EQ(stored_row1.id(), it_row1->id());
URLRows::const_iterator it_row2 =
std::find_if(changed_urls.begin(),
changed_urls.end(),
history::URLRow::URLRowHasURL(row2.url()));
ASSERT_NE(changed_urls.end(), it_row2);
EXPECT_EQ(stored_row2.id(), it_row2->id());
URLRows::const_iterator it_row3 =
std::find_if(changed_urls.begin(),
changed_urls.end(),
history::URLRow::URLRowHasURL(row3.url()));
ASSERT_NE(changed_urls.end(), it_row3);
EXPECT_EQ(stored_row3.id(), it_row3->id());
}
TEST_F(HistoryBackendTest, UpdateURLs) {
ASSERT_TRUE(backend_.get());
// Add three pages directly to the database.
URLRow row1(GURL("https://news.google.com/"));
row1.set_visit_count(1);
row1.set_last_visit(Time::Now());
URLRow row2(GURL("https://maps.google.com/"));
row2.set_visit_count(2);
row2.set_last_visit(Time::Now());
URLRow row3(GURL("https://www.google.com/"));
row3.set_visit_count(3);
row3.set_last_visit(Time::Now());
backend_->db_->AddURL(row1);
backend_->db_->AddURL(row2);
backend_->db_->AddURL(row3);
// Now create changed versions of all URLRows by incrementing their visit
// counts, and in the meantime, also delete the second row from the database.
URLRow altered_row1, altered_row2, altered_row3;
backend_->db_->GetRowForURL(row1.url(), &altered_row1);
altered_row1.set_visit_count(42);
backend_->db_->GetRowForURL(row2.url(), &altered_row2);
altered_row2.set_visit_count(43);
backend_->db_->GetRowForURL(row3.url(), &altered_row3);
altered_row3.set_visit_count(44);
backend_->db_->DeleteURLRow(altered_row2.id());
// Now try to update all three rows at once. The change to the second URLRow
// should be ignored, as it is no longer present in the DB.
URLRows rows;
rows.push_back(altered_row1);
rows.push_back(altered_row2);
rows.push_back(altered_row3);
EXPECT_EQ(2u, backend_->UpdateURLs(rows));
URLRow stored_row1, stored_row3;
EXPECT_NE(0, backend_->db_->GetRowForURL(row1.url(), &stored_row1));
EXPECT_NE(0, backend_->db_->GetRowForURL(row3.url(), &stored_row3));
EXPECT_EQ(altered_row1.visit_count(), stored_row1.visit_count());
EXPECT_EQ(altered_row3.visit_count(), stored_row3.visit_count());
// Ensure that a notification was fired, and further verify that the IDs in
// the notification are set to those that are in effect in the main database.
// The InMemoryHistoryBackend relies on this for caching.
ASSERT_EQ(1, num_urls_modified_notifications());
const URLRows& changed_urls = urls_modified_notifications()[0];
EXPECT_EQ(2u, changed_urls.size());
URLRows::const_iterator it_row1 =
std::find_if(changed_urls.begin(),
changed_urls.end(),
history::URLRow::URLRowHasURL(row1.url()));
ASSERT_NE(changed_urls.end(), it_row1);
EXPECT_EQ(altered_row1.id(), it_row1->id());
EXPECT_EQ(altered_row1.visit_count(), it_row1->visit_count());
URLRows::const_iterator it_row3 =
std::find_if(changed_urls.begin(),
changed_urls.end(),
history::URLRow::URLRowHasURL(row3.url()));
ASSERT_NE(changed_urls.end(), it_row3);
EXPECT_EQ(altered_row3.id(), it_row3->id());
EXPECT_EQ(altered_row3.visit_count(), it_row3->visit_count());
}
// This verifies that a notification is fired. In-depth testing of logic should
// be done in HistoryTest.SetTitle.
TEST_F(HistoryBackendTest, SetPageTitleFiresNotificationWithCorrectDetails) {
const char kTestUrlTitle[] = "Google Search";
ASSERT_TRUE(backend_.get());
// Add two pages, then change the title of the second one.
URLRow row1(GURL("https://news.google.com/"));
row1.set_typed_count(1);
row1.set_last_visit(Time::Now());
URLRow row2(GURL("https://www.google.com/"));
row2.set_visit_count(2);
row2.set_last_visit(Time::Now());
URLRows rows;
rows.push_back(row1);
rows.push_back(row2);
backend_->AddPagesWithDetails(rows, history::SOURCE_BROWSED);
ClearBroadcastedNotifications();
backend_->SetPageTitle(row2.url(), base::UTF8ToUTF16(kTestUrlTitle));
// Ensure that a notification was fired, and further verify that the IDs in
// the notification are set to those that are in effect in the main database.
// The InMemoryHistoryBackend relies on this for caching.
URLRow stored_row2;
EXPECT_TRUE(backend_->GetURL(row2.url(), &stored_row2));
ASSERT_EQ(1, num_urls_modified_notifications());
const URLRows& changed_urls = urls_modified_notifications()[0];
ASSERT_EQ(1u, changed_urls.size());
EXPECT_EQ(base::UTF8ToUTF16(kTestUrlTitle), changed_urls[0].title());
EXPECT_EQ(stored_row2.id(), changed_urls[0].id());
}
// There's no importer on Android.
#if !defined(OS_ANDROID)
TEST_F(HistoryBackendTest, ImportedFaviconsTest) {
// Setup test data - two Urls in the history, one with favicon assigned and
// one without.
GURL favicon_url1("http://www.google.com/favicon.ico");
std::vector<unsigned char> data;
data.push_back('1');
favicon_base::FaviconID favicon1 = backend_->thumbnail_db_->AddFavicon(
favicon_url1,
favicon_base::FAVICON,
base::RefCountedBytes::TakeVector(&data),
Time::Now(),
gfx::Size());
URLRow row1(GURL("http://www.google.com/"));
row1.set_visit_count(1);
row1.set_last_visit(Time::Now());
EXPECT_TRUE(backend_->thumbnail_db_->AddIconMapping(row1.url(), favicon1));
URLRow row2(GURL("http://news.google.com/"));
row2.set_visit_count(1);
row2.set_last_visit(Time::Now());
URLRows rows;
rows.push_back(row1);
rows.push_back(row2);
backend_->AddPagesWithDetails(rows, history::SOURCE_BROWSED);
URLRow url_row1, url_row2;
EXPECT_FALSE(backend_->db_->GetRowForURL(row1.url(), &url_row1) == 0);
EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &url_row2) == 0);
EXPECT_EQ(1u, NumIconMappingsForPageURL(row1.url(), favicon_base::FAVICON));
EXPECT_EQ(0u, NumIconMappingsForPageURL(row2.url(), favicon_base::FAVICON));
// Now provide one imported favicon for both URLs already in the registry.
// The new favicon should only be used with the URL that doesn't already have
// a favicon.
favicon_base::FaviconUsageDataList favicons;
favicon_base::FaviconUsageData favicon;
favicon.favicon_url = GURL("http://news.google.com/favicon.ico");
favicon.png_data.push_back('2');
favicon.urls.insert(row1.url());
favicon.urls.insert(row2.url());
favicons.push_back(favicon);
backend_->SetImportedFavicons(favicons);
EXPECT_FALSE(backend_->db_->GetRowForURL(row1.url(), &url_row1) == 0);
EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &url_row2) == 0);
std::vector<IconMapping> mappings;
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
row1.url(), favicon_base::FAVICON, &mappings));
EXPECT_EQ(1u, mappings.size());
EXPECT_EQ(favicon1, mappings[0].icon_id);
EXPECT_EQ(favicon_url1, mappings[0].icon_url);
mappings.clear();
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
row2.url(), favicon_base::FAVICON, &mappings));
EXPECT_EQ(1u, mappings.size());
EXPECT_EQ(favicon.favicon_url, mappings[0].icon_url);
// A URL should not be added to history (to store favicon), if
// the URL is not bookmarked.
GURL url3("http://mail.google.com");
favicons.clear();
favicon.favicon_url = GURL("http://mail.google.com/favicon.ico");
favicon.png_data.push_back('3');
favicon.urls.insert(url3);
favicons.push_back(favicon);
backend_->SetImportedFavicons(favicons);
URLRow url_row3;
EXPECT_TRUE(backend_->db_->GetRowForURL(url3, &url_row3) == 0);
// If the URL is bookmarked, it should get added to history with 0 visits.
history_client_.AddBookmark(url3);
backend_->SetImportedFavicons(favicons);
EXPECT_FALSE(backend_->db_->GetRowForURL(url3, &url_row3) == 0);
EXPECT_TRUE(url_row3.visit_count() == 0);
}
#endif // !defined(OS_ANDROID)
TEST_F(HistoryBackendTest, StripUsernamePasswordTest) {
ASSERT_TRUE(backend_.get());
GURL url("http://anyuser:anypass@www.google.com");
GURL stripped_url("http://www.google.com");
// Clear all history.
backend_->DeleteAllHistory();
// Visit the url with username, password.
backend_->AddPageVisit(url, base::Time::Now(), 0,
ui::PageTransitionFromInt(
ui::PageTransitionGetQualifier(ui::PAGE_TRANSITION_TYPED)),
history::SOURCE_BROWSED);
// Fetch the row information about stripped url from history db.
VisitVector visits;
URLID row_id = backend_->db_->GetRowForURL(stripped_url, NULL);
backend_->db_->GetVisitsForURL(row_id, &visits);
// Check if stripped url is stored in database.
ASSERT_EQ(1U, visits.size());
}
TEST_F(HistoryBackendTest, AddPageVisitSource) {
ASSERT_TRUE(backend_.get());
GURL url("http://www.google.com");
// Clear all history.
backend_->DeleteAllHistory();
// Assume visiting the url from an externsion.
backend_->AddPageVisit(
url, base::Time::Now(), 0, ui::PAGE_TRANSITION_TYPED,
history::SOURCE_EXTENSION);
// Assume the url is imported from Firefox.
backend_->AddPageVisit(url, base::Time::Now(), 0,
ui::PAGE_TRANSITION_TYPED,
history::SOURCE_FIREFOX_IMPORTED);
// Assume this url is also synced.
backend_->AddPageVisit(url, base::Time::Now(), 0,
ui::PAGE_TRANSITION_TYPED,
history::SOURCE_SYNCED);
// Fetch the row information about the url from history db.
VisitVector visits;
URLID row_id = backend_->db_->GetRowForURL(url, NULL);
backend_->db_->GetVisitsForURL(row_id, &visits);
// Check if all the visits to the url are stored in database.
ASSERT_EQ(3U, visits.size());
VisitSourceMap visit_sources;
ASSERT_TRUE(backend_->GetVisitsSource(visits, &visit_sources));
ASSERT_EQ(3U, visit_sources.size());
int sources = 0;
for (int i = 0; i < 3; i++) {
switch (visit_sources[visits[i].visit_id]) {
case history::SOURCE_EXTENSION:
sources |= 0x1;
break;
case history::SOURCE_FIREFOX_IMPORTED:
sources |= 0x2;
break;
case history::SOURCE_SYNCED:
sources |= 0x4;
default:
break;
}
}
EXPECT_EQ(0x7, sources);
}
TEST_F(HistoryBackendTest, AddPageVisitNotLastVisit) {
ASSERT_TRUE(backend_.get());
GURL url("http://www.google.com");
// Clear all history.
backend_->DeleteAllHistory();
// Create visit times
base::Time recent_time = base::Time::Now();
base::TimeDelta visit_age = base::TimeDelta::FromDays(3);
base::Time older_time = recent_time - visit_age;
// Visit the url with recent time.
backend_->AddPageVisit(url, recent_time, 0,
ui::PageTransitionFromInt(
ui::PageTransitionGetQualifier(ui::PAGE_TRANSITION_TYPED)),
history::SOURCE_BROWSED);
// Add to the url a visit with older time (could be syncing from another
// client, etc.).
backend_->AddPageVisit(url, older_time, 0,
ui::PageTransitionFromInt(
ui::PageTransitionGetQualifier(ui::PAGE_TRANSITION_TYPED)),
history::SOURCE_SYNCED);
// Fetch the row information about url from history db.
VisitVector visits;
URLRow row;
URLID row_id = backend_->db_->GetRowForURL(url, &row);
backend_->db_->GetVisitsForURL(row_id, &visits);
// Last visit time should be the most recent time, not the most recently added
// visit.
ASSERT_EQ(2U, visits.size());
ASSERT_EQ(recent_time, row.last_visit());
}
TEST_F(HistoryBackendTest, AddPageVisitFiresNotificationWithCorrectDetails) {
ASSERT_TRUE(backend_.get());
GURL url1("http://www.google.com");
GURL url2("http://maps.google.com");
// Clear all history.
backend_->DeleteAllHistory();
ClearBroadcastedNotifications();
// Visit two distinct URLs, the second one twice.
backend_->AddPageVisit(url1, base::Time::Now(), 0,
ui::PAGE_TRANSITION_LINK,
history::SOURCE_BROWSED);
for (int i = 0; i < 2; ++i) {
backend_->AddPageVisit(url2, base::Time::Now(), 0,
ui::PAGE_TRANSITION_TYPED,
history::SOURCE_BROWSED);
}
URLRow stored_row1, stored_row2;
EXPECT_NE(0, backend_->db_->GetRowForURL(url1, &stored_row1));
EXPECT_NE(0, backend_->db_->GetRowForURL(url2, &stored_row2));
// Expect that HistoryServiceObserver::OnURLVisited has been called 3 times,
// and that each time the URLRows have the correct URLs and IDs set.
ASSERT_EQ(3, num_url_visited_notifications());
EXPECT_TRUE(ui::PageTransitionCoreTypeIs(url_visited_notifications()[0].first,
ui::PAGE_TRANSITION_LINK));
EXPECT_EQ(stored_row1.id(), url_visited_notifications()[0].second.id());
EXPECT_EQ(stored_row1.url(), url_visited_notifications()[0].second.url());
EXPECT_TRUE(ui::PageTransitionCoreTypeIs(url_visited_notifications()[1].first,
ui::PAGE_TRANSITION_TYPED));
EXPECT_EQ(stored_row2.id(), url_visited_notifications()[1].second.id());
EXPECT_EQ(stored_row2.url(), url_visited_notifications()[1].second.url());
EXPECT_TRUE(ui::PageTransitionCoreTypeIs(url_visited_notifications()[2].first,
ui::PAGE_TRANSITION_TYPED));
EXPECT_EQ(stored_row2.id(), url_visited_notifications()[2].second.id());
EXPECT_EQ(stored_row2.url(), url_visited_notifications()[2].second.url());
}
TEST_F(HistoryBackendTest, AddPageArgsSource) {
ASSERT_TRUE(backend_.get());
GURL url("http://testpageargs.com");
// Assume this page is browsed by user.
HistoryAddPageArgs request1(url, base::Time::Now(), NULL, 0, GURL(),
history::RedirectList(),
ui::PAGE_TRANSITION_KEYWORD_GENERATED,
history::SOURCE_BROWSED, false);
backend_->AddPage(request1);
// Assume this page is synced.
HistoryAddPageArgs request2(url, base::Time::Now(), NULL, 0, GURL(),
history::RedirectList(),
ui::PAGE_TRANSITION_LINK,
history::SOURCE_SYNCED, false);
backend_->AddPage(request2);
// Assume this page is browsed again.
HistoryAddPageArgs request3(url, base::Time::Now(), NULL, 0, GURL(),
history::RedirectList(),
ui::PAGE_TRANSITION_TYPED,
history::SOURCE_BROWSED, false);
backend_->AddPage(request3);
// Three visits should be added with proper sources.
VisitVector visits;
URLRow row;
URLID id = backend_->db()->GetRowForURL(url, &row);
ASSERT_TRUE(backend_->db()->GetVisitsForURL(id, &visits));
ASSERT_EQ(3U, visits.size());
VisitSourceMap visit_sources;
ASSERT_TRUE(backend_->GetVisitsSource(visits, &visit_sources));
ASSERT_EQ(1U, visit_sources.size());
EXPECT_EQ(history::SOURCE_SYNCED, visit_sources.begin()->second);
}
TEST_F(HistoryBackendTest, AddVisitsSource) {
ASSERT_TRUE(backend_.get());
GURL url1("http://www.cnn.com");
std::vector<VisitInfo> visits1, visits2;
visits1.push_back(VisitInfo(
Time::Now() - base::TimeDelta::FromDays(5),
ui::PAGE_TRANSITION_LINK));
visits1.push_back(VisitInfo(
Time::Now() - base::TimeDelta::FromDays(1),
ui::PAGE_TRANSITION_LINK));
visits1.push_back(VisitInfo(
Time::Now(), ui::PAGE_TRANSITION_LINK));
GURL url2("http://www.example.com");
visits2.push_back(VisitInfo(
Time::Now() - base::TimeDelta::FromDays(10),
ui::PAGE_TRANSITION_LINK));
visits2.push_back(VisitInfo(Time::Now(), ui::PAGE_TRANSITION_LINK));
// Clear all history.
backend_->DeleteAllHistory();
// Add the visits.
backend_->AddVisits(url1, visits1, history::SOURCE_IE_IMPORTED);
backend_->AddVisits(url2, visits2, history::SOURCE_SYNCED);
// Verify the visits were added with their sources.
VisitVector visits;
URLRow row;
URLID id = backend_->db()->GetRowForURL(url1, &row);
ASSERT_TRUE(backend_->db()->GetVisitsForURL(id, &visits));
ASSERT_EQ(3U, visits.size());
VisitSourceMap visit_sources;
ASSERT_TRUE(backend_->GetVisitsSource(visits, &visit_sources));
ASSERT_EQ(3U, visit_sources.size());
for (int i = 0; i < 3; i++)
EXPECT_EQ(history::SOURCE_IE_IMPORTED, visit_sources[visits[i].visit_id]);
id = backend_->db()->GetRowForURL(url2, &row);
ASSERT_TRUE(backend_->db()->GetVisitsForURL(id, &visits));
ASSERT_EQ(2U, visits.size());
ASSERT_TRUE(backend_->GetVisitsSource(visits, &visit_sources));
ASSERT_EQ(2U, visit_sources.size());
for (int i = 0; i < 2; i++)
EXPECT_EQ(history::SOURCE_SYNCED, visit_sources[visits[i].visit_id]);
}
TEST_F(HistoryBackendTest, GetMostRecentVisits) {
ASSERT_TRUE(backend_.get());
GURL url1("http://www.cnn.com");
std::vector<VisitInfo> visits1;
visits1.push_back(VisitInfo(
Time::Now() - base::TimeDelta::FromDays(5),
ui::PAGE_TRANSITION_LINK));
visits1.push_back(VisitInfo(
Time::Now() - base::TimeDelta::FromDays(1),
ui::PAGE_TRANSITION_LINK));
visits1.push_back(VisitInfo(
Time::Now(), ui::PAGE_TRANSITION_LINK));
// Clear all history.
backend_->DeleteAllHistory();
// Add the visits.
backend_->AddVisits(url1, visits1, history::SOURCE_IE_IMPORTED);
// Verify the visits were added with their sources.
VisitVector visits;
URLRow row;
URLID id = backend_->db()->GetRowForURL(url1, &row);
ASSERT_TRUE(backend_->db()->GetMostRecentVisitsForURL(id, 1, &visits));
ASSERT_EQ(1U, visits.size());
EXPECT_EQ(visits1[2].first, visits[0].visit_time);
}
TEST_F(HistoryBackendTest, RemoveVisitsTransitions) {
ASSERT_TRUE(backend_.get());
// Clear all history.
backend_->DeleteAllHistory();
GURL url1("http://www.cnn.com");
VisitInfo typed_visit(
Time::Now() - base::TimeDelta::FromDays(6),
ui::PAGE_TRANSITION_TYPED);
VisitInfo reload_visit(
Time::Now() - base::TimeDelta::FromDays(5),
ui::PAGE_TRANSITION_RELOAD);
VisitInfo link_visit(
Time::Now() - base::TimeDelta::FromDays(4),
ui::PAGE_TRANSITION_LINK);
std::vector<VisitInfo> visits_to_add;
visits_to_add.push_back(typed_visit);
visits_to_add.push_back(reload_visit);
visits_to_add.push_back(link_visit);
// Add the visits.
backend_->AddVisits(url1, visits_to_add, history::SOURCE_SYNCED);
// Verify that the various counts are what we expect.
VisitVector visits;
URLRow row;
URLID id = backend_->db()->GetRowForURL(url1, &row);
ASSERT_TRUE(backend_->db()->GetVisitsForURL(id, &visits));
ASSERT_EQ(3U, visits.size());
ASSERT_EQ(1, row.typed_count());
ASSERT_EQ(2, row.visit_count());
// Now, delete the typed visit and verify that typed_count is updated.
ASSERT_TRUE(backend_->RemoveVisits(VisitVector(1, visits[0])));
id = backend_->db()->GetRowForURL(url1, &row);
ASSERT_TRUE(backend_->db()->GetVisitsForURL(id, &visits));
ASSERT_EQ(2U, visits.size());
ASSERT_EQ(0, row.typed_count());
ASSERT_EQ(1, row.visit_count());
// Delete the reload visit now and verify that none of the counts have
// changed.
ASSERT_TRUE(backend_->RemoveVisits(VisitVector(1, visits[0])));
id = backend_->db()->GetRowForURL(url1, &row);
ASSERT_TRUE(backend_->db()->GetVisitsForURL(id, &visits));
ASSERT_EQ(1U, visits.size());
ASSERT_EQ(0, row.typed_count());
ASSERT_EQ(1, row.visit_count());
// Delete the last visit and verify that we delete the URL.
ASSERT_TRUE(backend_->RemoveVisits(VisitVector(1, visits[0])));
ASSERT_EQ(0, backend_->db()->GetRowForURL(url1, &row));
}
TEST_F(HistoryBackendTest, RemoveVisitsSource) {
ASSERT_TRUE(backend_.get());
GURL url1("http://www.cnn.com");
std::vector<VisitInfo> visits1, visits2;
visits1.push_back(VisitInfo(
Time::Now() - base::TimeDelta::FromDays(5),
ui::PAGE_TRANSITION_LINK));
visits1.push_back(VisitInfo(Time::Now(),
ui::PAGE_TRANSITION_LINK));
GURL url2("http://www.example.com");
visits2.push_back(VisitInfo(
Time::Now() - base::TimeDelta::FromDays(10),
ui::PAGE_TRANSITION_LINK));
visits2.push_back(VisitInfo(Time::Now(), ui::PAGE_TRANSITION_LINK));
// Clear all history.
backend_->DeleteAllHistory();
// Add the visits.
backend_->AddVisits(url1, visits1, history::SOURCE_IE_IMPORTED);
backend_->AddVisits(url2, visits2, history::SOURCE_SYNCED);
// Verify the visits of url1 were added.
VisitVector visits;
URLRow row;
URLID id = backend_->db()->GetRowForURL(url1, &row);
ASSERT_TRUE(backend_->db()->GetVisitsForURL(id, &visits));
ASSERT_EQ(2U, visits.size());
// Remove these visits.
ASSERT_TRUE(backend_->RemoveVisits(visits));
// Now check only url2's source in visit_source table.
VisitSourceMap visit_sources;
ASSERT_TRUE(backend_->GetVisitsSource(visits, &visit_sources));
ASSERT_EQ(0U, visit_sources.size());
id = backend_->db()->GetRowForURL(url2, &row);
ASSERT_TRUE(backend_->db()->GetVisitsForURL(id, &visits));
ASSERT_EQ(2U, visits.size());
ASSERT_TRUE(backend_->GetVisitsSource(visits, &visit_sources));
ASSERT_EQ(2U, visit_sources.size());
for (int i = 0; i < 2; i++)
EXPECT_EQ(history::SOURCE_SYNCED, visit_sources[visits[i].visit_id]);
}
// Test for migration of adding visit_source table.
TEST_F(HistoryBackendTest, MigrationVisitSource) {
ASSERT_TRUE(backend_.get());
backend_->Closing();
backend_ = NULL;
base::FilePath old_history_path;
ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &old_history_path));
old_history_path = old_history_path.AppendASCII("History");
old_history_path = old_history_path.AppendASCII("HistoryNoSource");
// Copy history database file to current directory so that it will be deleted
// in Teardown.
base::FilePath new_history_path(test_dir());
base::DeleteFile(new_history_path, true);
base::CreateDirectory(new_history_path);
base::FilePath new_history_file = new_history_path.Append(kHistoryFilename);
ASSERT_TRUE(base::CopyFile(old_history_path, new_history_file));
backend_ = new HistoryBackend(new HistoryBackendTestDelegate(this),
&history_client_);
backend_->Init(std::string(), false,
TestHistoryDatabaseParamsForPath(new_history_path));
backend_->Closing();
backend_ = NULL;
// Now the database should already be migrated.
// Check version first.
int cur_version = HistoryDatabase::GetCurrentVersion();
sql::Connection db;
ASSERT_TRUE(db.Open(new_history_file));
sql::Statement s(db.GetUniqueStatement(
"SELECT value FROM meta WHERE key = 'version'"));
ASSERT_TRUE(s.Step());
int file_version = s.ColumnInt(0);
EXPECT_EQ(cur_version, file_version);
// Check visit_source table is created and empty.
s.Assign(db.GetUniqueStatement(
"SELECT name FROM sqlite_master WHERE name=\"visit_source\""));
ASSERT_TRUE(s.Step());
s.Assign(db.GetUniqueStatement("SELECT * FROM visit_source LIMIT 10"));
EXPECT_FALSE(s.Step());
}
// Test that SetFaviconMappingsForPageAndRedirects correctly updates icon
// mappings based on redirects, icon URLs and icon types.
TEST_F(HistoryBackendTest, SetFaviconMappingsForPageAndRedirects) {
// Init recent_redirects_
const GURL url1("http://www.google.com");
const GURL url2("http://www.google.com/m");
URLRow url_info1(url1);
url_info1.set_visit_count(0);
url_info1.set_typed_count(0);
url_info1.set_last_visit(base::Time());
url_info1.set_hidden(false);
backend_->db_->AddURL(url_info1);
URLRow url_info2(url2);
url_info2.set_visit_count(0);
url_info2.set_typed_count(0);
url_info2.set_last_visit(base::Time());
url_info2.set_hidden(false);
backend_->db_->AddURL(url_info2);
history::RedirectList redirects;
redirects.push_back(url2);
redirects.push_back(url1);
backend_->recent_redirects_.Put(url1, redirects);
const GURL icon_url1("http://www.google.com/icon");
const GURL icon_url2("http://www.google.com/icon2");
std::vector<SkBitmap> bitmaps;
bitmaps.push_back(CreateBitmap(SK_ColorBLUE, kSmallEdgeSize));
bitmaps.push_back(CreateBitmap(SK_ColorRED, kLargeEdgeSize));
// Add a favicon.
backend_->SetFavicons(url1, favicon_base::FAVICON, icon_url1, bitmaps);
EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, favicon_base::FAVICON));
EXPECT_EQ(1u, NumIconMappingsForPageURL(url2, favicon_base::FAVICON));
// Add one touch_icon
backend_->SetFavicons(url1, favicon_base::TOUCH_ICON, icon_url1, bitmaps);
EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, favicon_base::TOUCH_ICON));
EXPECT_EQ(1u, NumIconMappingsForPageURL(url2, favicon_base::TOUCH_ICON));
EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, favicon_base::FAVICON));
// Add one TOUCH_PRECOMPOSED_ICON
backend_->SetFavicons(
url1, favicon_base::TOUCH_PRECOMPOSED_ICON, icon_url1, bitmaps);
// The touch_icon was replaced.
EXPECT_EQ(0u, NumIconMappingsForPageURL(url1, favicon_base::TOUCH_ICON));
EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, favicon_base::FAVICON));
EXPECT_EQ(
1u,
NumIconMappingsForPageURL(url1, favicon_base::TOUCH_PRECOMPOSED_ICON));
EXPECT_EQ(
1u,
NumIconMappingsForPageURL(url2, favicon_base::TOUCH_PRECOMPOSED_ICON));
// Add a touch_icon.
backend_->SetFavicons(url1, favicon_base::TOUCH_ICON, icon_url1, bitmaps);
EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, favicon_base::TOUCH_ICON));
EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, favicon_base::FAVICON));
// The TOUCH_PRECOMPOSED_ICON was replaced.
EXPECT_EQ(
0u,
NumIconMappingsForPageURL(url1, favicon_base::TOUCH_PRECOMPOSED_ICON));
// Add a different favicon.
backend_->SetFavicons(url1, favicon_base::FAVICON, icon_url2, bitmaps);
EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, favicon_base::TOUCH_ICON));
EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, favicon_base::FAVICON));
EXPECT_EQ(1u, NumIconMappingsForPageURL(url2, favicon_base::FAVICON));
}
// Test that there is no churn in icon mappings from calling
// SetFavicons() twice with the same |bitmaps| parameter.
TEST_F(HistoryBackendTest, SetFaviconMappingsForPageDuplicates) {
const GURL url("http://www.google.com/");
const GURL icon_url("http://www.google.com/icon");
std::vector<SkBitmap> bitmaps;
bitmaps.push_back(CreateBitmap(SK_ColorBLUE, kSmallEdgeSize));
bitmaps.push_back(CreateBitmap(SK_ColorRED, kLargeEdgeSize));
backend_->SetFavicons(url, favicon_base::FAVICON, icon_url, bitmaps);
std::vector<IconMapping> icon_mappings;
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
url, favicon_base::FAVICON, &icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
IconMappingID mapping_id = icon_mappings[0].mapping_id;
backend_->SetFavicons(url, favicon_base::FAVICON, icon_url, bitmaps);
icon_mappings.clear();
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
url, favicon_base::FAVICON, &icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
// The same row in the icon_mapping table should be used for the mapping as
// before.
EXPECT_EQ(mapping_id, icon_mappings[0].mapping_id);
}
// Test that calling SetFavicons() with FaviconBitmapData of different pixel
// sizes than the initially passed in FaviconBitmapData deletes the no longer
// used favicon bitmaps.
TEST_F(HistoryBackendTest, SetFaviconsDeleteBitmaps) {
const GURL page_url("http://www.google.com/");
const GURL icon_url("http://www.google.com/icon");
std::vector<SkBitmap> bitmaps;
bitmaps.push_back(CreateBitmap(SK_ColorBLUE, kSmallEdgeSize));
bitmaps.push_back(CreateBitmap(SK_ColorRED, kLargeEdgeSize));
backend_->SetFavicons(page_url, favicon_base::FAVICON, icon_url, bitmaps);
// Test initial state.
std::vector<IconMapping> icon_mappings;
EXPECT_TRUE(GetSortedIconMappingsForPageURL(page_url, &icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(icon_url, icon_mappings[0].icon_url);
EXPECT_EQ(favicon_base::FAVICON, icon_mappings[0].icon_type);
favicon_base::FaviconID favicon_id = icon_mappings[0].icon_id;
std::vector<FaviconBitmap> favicon_bitmaps;
EXPECT_TRUE(GetSortedFaviconBitmaps(favicon_id, &favicon_bitmaps));
EXPECT_EQ(2u, favicon_bitmaps.size());
FaviconBitmapID small_bitmap_id = favicon_bitmaps[0].bitmap_id;
EXPECT_NE(0, small_bitmap_id);
EXPECT_TRUE(BitmapColorEqual(SK_ColorBLUE, favicon_bitmaps[0].bitmap_data));
EXPECT_EQ(kSmallSize, favicon_bitmaps[0].pixel_size);
FaviconBitmapID large_bitmap_id = favicon_bitmaps[1].bitmap_id;
EXPECT_NE(0, large_bitmap_id);
EXPECT_TRUE(BitmapColorEqual(SK_ColorRED, favicon_bitmaps[1].bitmap_data));
EXPECT_EQ(kLargeSize, favicon_bitmaps[1].pixel_size);
// Call SetFavicons() with bitmap data for only the large bitmap. Check that
// the small bitmap is in fact deleted.
bitmaps.clear();
bitmaps.push_back(CreateBitmap(SK_ColorWHITE, kLargeEdgeSize));
backend_->SetFavicons(page_url, favicon_base::FAVICON, icon_url, bitmaps);
scoped_refptr<base::RefCountedMemory> bitmap_data_out;
gfx::Size pixel_size_out;
EXPECT_FALSE(backend_->thumbnail_db_->GetFaviconBitmap(small_bitmap_id,
NULL, &bitmap_data_out, &pixel_size_out));
EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmap(large_bitmap_id,
NULL, &bitmap_data_out, &pixel_size_out));
EXPECT_TRUE(BitmapColorEqual(SK_ColorWHITE, bitmap_data_out));
EXPECT_EQ(kLargeSize, pixel_size_out);
icon_mappings.clear();
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url,
&icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(favicon_id, icon_mappings[0].icon_id);
// Notifications should have been broadcast for each call to SetFavicons().
EXPECT_EQ(2, favicon_changed_notifications());
}
// Test updating a single favicon bitmap's data via SetFavicons.
TEST_F(HistoryBackendTest, SetFaviconsReplaceBitmapData) {
const GURL page_url("http://www.google.com/");
const GURL icon_url("http://www.google.com/icon");
std::vector<SkBitmap> bitmaps;
bitmaps.push_back(CreateBitmap(SK_ColorBLUE, kSmallEdgeSize));
// Add bitmap to the database.
backend_->SetFavicons(page_url, favicon_base::FAVICON, icon_url, bitmaps);
favicon_base::FaviconID original_favicon_id =
backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
icon_url, favicon_base::FAVICON, NULL);
EXPECT_NE(0, original_favicon_id);
FaviconBitmap original_favicon_bitmap;
EXPECT_TRUE(
GetOnlyFaviconBitmap(original_favicon_id, &original_favicon_bitmap));
EXPECT_TRUE(
BitmapColorEqual(SK_ColorBLUE, original_favicon_bitmap.bitmap_data));
EXPECT_EQ(1, favicon_changed_notifications());
// Call SetFavicons() with completely identical data.
bitmaps[0] = CreateBitmap(SK_ColorBLUE, kSmallEdgeSize);
backend_->SetFavicons(page_url, favicon_base::FAVICON, icon_url, bitmaps);
favicon_base::FaviconID updated_favicon_id =
backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
icon_url, favicon_base::FAVICON, NULL);
EXPECT_NE(0, updated_favicon_id);
FaviconBitmap updated_favicon_bitmap;
EXPECT_TRUE(
GetOnlyFaviconBitmap(updated_favicon_id, &updated_favicon_bitmap));
EXPECT_TRUE(
BitmapColorEqual(SK_ColorBLUE, updated_favicon_bitmap.bitmap_data));
// Because the bitmap data is byte equivalent, no notifications should have
// been broadcasted.
EXPECT_EQ(1, favicon_changed_notifications());
// Call SetFavicons() with a different bitmap of the same size.
bitmaps[0] = CreateBitmap(SK_ColorWHITE, kSmallEdgeSize);
backend_->SetFavicons(page_url, favicon_base::FAVICON, icon_url, bitmaps);
updated_favicon_id = backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
icon_url, favicon_base::FAVICON, NULL);
EXPECT_NE(0, updated_favicon_id);
EXPECT_TRUE(
GetOnlyFaviconBitmap(updated_favicon_id, &updated_favicon_bitmap));
EXPECT_TRUE(
BitmapColorEqual(SK_ColorWHITE, updated_favicon_bitmap.bitmap_data));
// There should be no churn in FaviconIDs or FaviconBitmapIds even though
// the bitmap data changed.
EXPECT_EQ(original_favicon_bitmap.icon_id, updated_favicon_bitmap.icon_id);
EXPECT_EQ(original_favicon_bitmap.bitmap_id,
updated_favicon_bitmap.bitmap_id);
// A notification should have been broadcasted as the favicon bitmap data has
// changed.
EXPECT_EQ(2, favicon_changed_notifications());
}
// Test that if two pages share the same FaviconID, changing the favicon for
// one page does not affect the other.
TEST_F(HistoryBackendTest, SetFaviconsSameFaviconURLForTwoPages) {
GURL icon_url("http://www.google.com/favicon.ico");
GURL icon_url_new("http://www.google.com/favicon2.ico");
GURL page_url1("http://www.google.com");
GURL page_url2("http://www.google.ca");
std::vector<SkBitmap> bitmaps;
bitmaps.push_back(CreateBitmap(SK_ColorBLUE, kSmallEdgeSize));
bitmaps.push_back(CreateBitmap(SK_ColorRED, kLargeEdgeSize));
backend_->SetFavicons(page_url1, favicon_base::FAVICON, icon_url, bitmaps);
std::vector<GURL> icon_urls;
icon_urls.push_back(icon_url);
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results;
backend_->UpdateFaviconMappingsAndFetch(page_url2,
icon_urls,
favicon_base::FAVICON,
GetEdgeSizesSmallAndLarge(),
&bitmap_results);
// Check that the same FaviconID is mapped to both page URLs.
std::vector<IconMapping> icon_mappings;
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
page_url1, &icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
favicon_base::FaviconID favicon_id = icon_mappings[0].icon_id;
EXPECT_NE(0, favicon_id);
icon_mappings.clear();
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
page_url2, &icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(favicon_id, icon_mappings[0].icon_id);
// Change the icon URL that |page_url1| is mapped to.
bitmaps.clear();
bitmaps.push_back(CreateBitmap(SK_ColorWHITE, kSmallEdgeSize));
backend_->SetFavicons(
page_url1, favicon_base::FAVICON, icon_url_new, bitmaps);
// |page_url1| should map to a new FaviconID and have valid bitmap data.
icon_mappings.clear();
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
page_url1, &icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(icon_url_new, icon_mappings[0].icon_url);
EXPECT_NE(favicon_id, icon_mappings[0].icon_id);
std::vector<FaviconBitmap> favicon_bitmaps;
EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(
icon_mappings[0].icon_id, &favicon_bitmaps));
EXPECT_EQ(1u, favicon_bitmaps.size());
// |page_url2| should still map to the same FaviconID and have valid bitmap
// data.
icon_mappings.clear();
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
page_url2, &icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(favicon_id, icon_mappings[0].icon_id);
favicon_bitmaps.clear();
EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(favicon_id,
&favicon_bitmaps));
EXPECT_EQ(2u, favicon_bitmaps.size());
// A notification should have been broadcast for each call to SetFavicons()
// and each call to UpdateFaviconMappingsAndFetch().
EXPECT_EQ(3, favicon_changed_notifications());
}
// Test that no notifications are broadcast as a result of calling
// UpdateFaviconMappingsAndFetch() for an icon URL which is already
// mapped to the passed in page URL.
TEST_F(HistoryBackendTest, UpdateFaviconMappingsAndFetchNoChange) {
GURL page_url("http://www.google.com");
GURL icon_url("http://www.google.com/favicon.ico");
std::vector<SkBitmap> bitmaps;
bitmaps.push_back(CreateBitmap(SK_ColorBLUE, kSmallEdgeSize));
backend_->SetFavicons(page_url, favicon_base::FAVICON, icon_url, bitmaps);
favicon_base::FaviconID icon_id =
backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
icon_url, favicon_base::FAVICON, NULL);
EXPECT_NE(0, icon_id);
EXPECT_EQ(1, favicon_changed_notifications());
std::vector<GURL> icon_urls;
icon_urls.push_back(icon_url);
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results;
backend_->UpdateFaviconMappingsAndFetch(page_url,
icon_urls,
favicon_base::FAVICON,
GetEdgeSizesSmallAndLarge(),
&bitmap_results);
EXPECT_EQ(icon_id,
backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
icon_url, favicon_base::FAVICON, NULL));
// No notification should have been broadcast as no icon mapping, favicon,
// or favicon bitmap was updated, added or removed.
EXPECT_EQ(1, favicon_changed_notifications());
}
// Test repeatedly calling MergeFavicon(). |page_url| is initially not known
// to the database.
TEST_F(HistoryBackendTest, MergeFaviconPageURLNotInDB) {
GURL page_url("http://www.google.com");
GURL icon_url("http:/www.google.com/favicon.ico");
std::vector<unsigned char> data;
data.push_back('a');
scoped_refptr<base::RefCountedBytes> bitmap_data(
new base::RefCountedBytes(data));
backend_->MergeFavicon(
page_url, icon_url, favicon_base::FAVICON, bitmap_data, kSmallSize);
// |page_url| should now be mapped to |icon_url| and the favicon bitmap should
// not be expired.
std::vector<IconMapping> icon_mappings;
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url,
&icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(icon_url, icon_mappings[0].icon_url);
FaviconBitmap favicon_bitmap;
EXPECT_TRUE(GetOnlyFaviconBitmap(icon_mappings[0].icon_id, &favicon_bitmap));
EXPECT_NE(base::Time(), favicon_bitmap.last_updated);
EXPECT_TRUE(BitmapDataEqual('a', favicon_bitmap.bitmap_data));
EXPECT_EQ(kSmallSize, favicon_bitmap.pixel_size);
data[0] = 'b';
bitmap_data = new base::RefCountedBytes(data);
backend_->MergeFavicon(
page_url, icon_url, favicon_base::FAVICON, bitmap_data, kSmallSize);
// |page_url| should still have a single favicon bitmap. The bitmap data
// should be updated.
icon_mappings.clear();
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url,
&icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(icon_url, icon_mappings[0].icon_url);
EXPECT_TRUE(GetOnlyFaviconBitmap(icon_mappings[0].icon_id, &favicon_bitmap));
EXPECT_NE(base::Time(), favicon_bitmap.last_updated);
EXPECT_TRUE(BitmapDataEqual('b', favicon_bitmap.bitmap_data));
EXPECT_EQ(kSmallSize, favicon_bitmap.pixel_size);
}
// Test calling MergeFavicon() when |page_url| is known to the database.
TEST_F(HistoryBackendTest, MergeFaviconPageURLInDB) {
GURL page_url("http://www.google.com");
GURL icon_url1("http:/www.google.com/favicon.ico");
GURL icon_url2("http://www.google.com/favicon2.ico");
std::vector<SkBitmap> bitmaps;
bitmaps.push_back(CreateBitmap(SK_ColorBLUE, kSmallEdgeSize));
backend_->SetFavicons(page_url, favicon_base::FAVICON, icon_url1, bitmaps);
// Test initial state.
std::vector<IconMapping> icon_mappings;
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url,
&icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(icon_url1, icon_mappings[0].icon_url);
FaviconBitmap favicon_bitmap;
EXPECT_TRUE(GetOnlyFaviconBitmap(icon_mappings[0].icon_id, &favicon_bitmap));
EXPECT_NE(base::Time(), favicon_bitmap.last_updated);
EXPECT_TRUE(BitmapColorEqual(SK_ColorBLUE, favicon_bitmap.bitmap_data));
EXPECT_EQ(kSmallSize, favicon_bitmap.pixel_size);
EXPECT_EQ(1, favicon_changed_notifications());
// 1) Merge identical favicon bitmap.
std::vector<unsigned char> data;
gfx::PNGCodec::EncodeBGRASkBitmap(bitmaps[0], false, &data);
scoped_refptr<base::RefCountedBytes> bitmap_data(
new base::RefCountedBytes(data));
backend_->MergeFavicon(
page_url, icon_url1, favicon_base::FAVICON, bitmap_data, kSmallSize);
// All the data should stay the same and no notifications should have been
// sent.
icon_mappings.clear();
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url,
&icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(icon_url1, icon_mappings[0].icon_url);
EXPECT_TRUE(GetOnlyFaviconBitmap(icon_mappings[0].icon_id, &favicon_bitmap));
EXPECT_NE(base::Time(), favicon_bitmap.last_updated);
EXPECT_TRUE(BitmapColorEqual(SK_ColorBLUE, favicon_bitmap.bitmap_data));
EXPECT_EQ(kSmallSize, favicon_bitmap.pixel_size);
EXPECT_EQ(1, favicon_changed_notifications());
// 2) Merge favicon bitmap of the same size.
data.clear();
data.push_back('b');
bitmap_data = new base::RefCountedBytes(data);
backend_->MergeFavicon(
page_url, icon_url1, favicon_base::FAVICON, bitmap_data, kSmallSize);
// The small favicon bitmap at |icon_url1| should be overwritten.
icon_mappings.clear();
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url,
&icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(icon_url1, icon_mappings[0].icon_url);
EXPECT_TRUE(GetOnlyFaviconBitmap(icon_mappings[0].icon_id, &favicon_bitmap));
EXPECT_NE(base::Time(), favicon_bitmap.last_updated);
EXPECT_TRUE(BitmapDataEqual('b', favicon_bitmap.bitmap_data));
EXPECT_EQ(kSmallSize, favicon_bitmap.pixel_size);
// 3) Merge favicon for the same icon URL, but a pixel size for which there is
// no favicon bitmap.
data[0] = 'c';
bitmap_data = new base::RefCountedBytes(data);
backend_->MergeFavicon(
page_url, icon_url1, favicon_base::FAVICON, bitmap_data, kTinySize);
// A new favicon bitmap should be created and the preexisting favicon bitmap
// ('b') should be expired.
icon_mappings.clear();
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url,
&icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(icon_url1, icon_mappings[0].icon_url);
std::vector<FaviconBitmap> favicon_bitmaps;
EXPECT_TRUE(GetSortedFaviconBitmaps(icon_mappings[0].icon_id,
&favicon_bitmaps));
EXPECT_NE(base::Time(), favicon_bitmaps[0].last_updated);
EXPECT_TRUE(BitmapDataEqual('c', favicon_bitmaps[0].bitmap_data));
EXPECT_EQ(kTinySize, favicon_bitmaps[0].pixel_size);
EXPECT_EQ(base::Time(), favicon_bitmaps[1].last_updated);
EXPECT_TRUE(BitmapDataEqual('b', favicon_bitmaps[1].bitmap_data));
EXPECT_EQ(kSmallSize, favicon_bitmaps[1].pixel_size);
// 4) Merge favicon for an icon URL different from the icon URLs already
// mapped to page URL.
data[0] = 'd';
bitmap_data = new base::RefCountedBytes(data);
backend_->MergeFavicon(
page_url, icon_url2, favicon_base::FAVICON, bitmap_data, kSmallSize);
// The existing favicon bitmaps should be copied over to the newly created
// favicon at |icon_url2|. |page_url| should solely be mapped to |icon_url2|.
icon_mappings.clear();
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url,
&icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(icon_url2, icon_mappings[0].icon_url);
favicon_bitmaps.clear();
EXPECT_TRUE(GetSortedFaviconBitmaps(icon_mappings[0].icon_id,
&favicon_bitmaps));
EXPECT_EQ(base::Time(), favicon_bitmaps[0].last_updated);
EXPECT_TRUE(BitmapDataEqual('c', favicon_bitmaps[0].bitmap_data));
EXPECT_EQ(kTinySize, favicon_bitmaps[0].pixel_size);
// The favicon being merged should take precedence over the preexisting
// favicon bitmaps.
EXPECT_NE(base::Time(), favicon_bitmaps[1].last_updated);
EXPECT_TRUE(BitmapDataEqual('d', favicon_bitmaps[1].bitmap_data));
EXPECT_EQ(kSmallSize, favicon_bitmaps[1].pixel_size);
// A notification should have been broadcast for each call to SetFavicons()
// and MergeFavicon().
EXPECT_EQ(4, favicon_changed_notifications());
}
// Test calling MergeFavicon() when |icon_url| is known to the database but not
// mapped to |page_url|.
TEST_F(HistoryBackendTest, MergeFaviconIconURLMappedToDifferentPageURL) {
GURL page_url1("http://www.google.com");
GURL page_url2("http://news.google.com");
GURL page_url3("http://maps.google.com");
GURL icon_url("http:/www.google.com/favicon.ico");
std::vector<SkBitmap> bitmaps;
bitmaps.push_back(CreateBitmap(SK_ColorBLUE, kSmallEdgeSize));
backend_->SetFavicons(page_url1, favicon_base::FAVICON, icon_url, bitmaps);
// Test initial state.
std::vector<IconMapping> icon_mappings;
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url1,
&icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(icon_url, icon_mappings[0].icon_url);
FaviconBitmap favicon_bitmap;
EXPECT_TRUE(GetOnlyFaviconBitmap(icon_mappings[0].icon_id, &favicon_bitmap));
EXPECT_NE(base::Time(), favicon_bitmap.last_updated);
EXPECT_TRUE(BitmapColorEqual(SK_ColorBLUE, favicon_bitmap.bitmap_data));
EXPECT_EQ(kSmallSize, favicon_bitmap.pixel_size);
// 1) Merge in an identical favicon bitmap data but for a different page URL.
std::vector<unsigned char> data;
gfx::PNGCodec::EncodeBGRASkBitmap(bitmaps[0], false, &data);
scoped_refptr<base::RefCountedBytes> bitmap_data(
new base::RefCountedBytes(data));
backend_->MergeFavicon(
page_url2, icon_url, favicon_base::FAVICON, bitmap_data, kSmallSize);
favicon_base::FaviconID favicon_id =
backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
icon_url, favicon_base::FAVICON, NULL);
EXPECT_NE(0, favicon_id);
EXPECT_TRUE(GetOnlyFaviconBitmap(favicon_id, &favicon_bitmap));
EXPECT_NE(base::Time(), favicon_bitmap.last_updated);
EXPECT_TRUE(BitmapColorEqual(SK_ColorBLUE, favicon_bitmap.bitmap_data));
EXPECT_EQ(kSmallSize, favicon_bitmap.pixel_size);
// 2) Merging a favicon bitmap with different bitmap data for the same icon
// URL should overwrite the small favicon bitmap at |icon_url|.
data.clear();
data.push_back('b');
bitmap_data = new base::RefCountedBytes(data);
backend_->MergeFavicon(
page_url3, icon_url, favicon_base::FAVICON, bitmap_data, kSmallSize);
favicon_id = backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
icon_url, favicon_base::FAVICON, NULL);
EXPECT_NE(0, favicon_id);
EXPECT_TRUE(GetOnlyFaviconBitmap(favicon_id, &favicon_bitmap));
EXPECT_NE(base::Time(), favicon_bitmap.last_updated);
EXPECT_TRUE(BitmapDataEqual('b', favicon_bitmap.bitmap_data));
EXPECT_EQ(kSmallSize, favicon_bitmap.pixel_size);
// |icon_url| should be mapped to all three page URLs.
icon_mappings.clear();
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url1,
&icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(favicon_id, icon_mappings[0].icon_id);
icon_mappings.clear();
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url2,
&icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(favicon_id, icon_mappings[0].icon_id);
icon_mappings.clear();
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url3,
&icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(favicon_id, icon_mappings[0].icon_id);
// A notification should have been broadcast for each call to SetFavicons()
// and MergeFavicon().
EXPECT_EQ(3, favicon_changed_notifications());
}
// Test that MergeFavicon() does not add more than
// |kMaxFaviconBitmapsPerIconURL| to a favicon.
TEST_F(HistoryBackendTest, MergeFaviconMaxFaviconBitmapsPerIconURL) {
GURL page_url("http://www.google.com");
std::string icon_url_string("http://www.google.com/favicon.ico");
size_t replace_index = icon_url_string.size() - 1;
std::vector<unsigned char> data;
data.push_back('a');
scoped_refptr<base::RefCountedMemory> bitmap_data =
base::RefCountedBytes::TakeVector(&data);
int pixel_size = 1;
for (size_t i = 0; i < kMaxFaviconBitmapsPerIconURL + 1; ++i) {
icon_url_string[replace_index] = '0' + i;
GURL icon_url(icon_url_string);
backend_->MergeFavicon(page_url,
icon_url,
favicon_base::FAVICON,
bitmap_data,
gfx::Size(pixel_size, pixel_size));
++pixel_size;
}
// There should be a single favicon mapped to |page_url| with exactly
// kMaxFaviconBitmapsPerIconURL favicon bitmaps.
std::vector<IconMapping> icon_mappings;
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url,
&icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
std::vector<FaviconBitmap> favicon_bitmaps;
EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(
icon_mappings[0].icon_id, &favicon_bitmaps));
EXPECT_EQ(kMaxFaviconBitmapsPerIconURL, favicon_bitmaps.size());
}
// Tests that the favicon set by MergeFavicon() shows up in the result of
// GetFaviconsForURL().
TEST_F(HistoryBackendTest, MergeFaviconShowsUpInGetFaviconsForURLResult) {
GURL page_url("http://www.google.com");
GURL icon_url("http://www.google.com/favicon.ico");
GURL merged_icon_url("http://wwww.google.com/favicon2.ico");
std::vector<SkBitmap> bitmaps;
bitmaps.push_back(CreateBitmap(SK_ColorBLUE, kSmallEdgeSize));
bitmaps.push_back(CreateBitmap(SK_ColorRED, kLargeEdgeSize));
// Set some preexisting favicons for |page_url|.
backend_->SetFavicons(page_url, favicon_base::FAVICON, icon_url, bitmaps);
// Merge small favicon.
std::vector<unsigned char> data;
data.push_back('c');
scoped_refptr<base::RefCountedBytes> bitmap_data(
new base::RefCountedBytes(data));
backend_->MergeFavicon(page_url,
merged_icon_url,
favicon_base::FAVICON,
bitmap_data,
kSmallSize);
// Request favicon bitmaps for both 1x and 2x to simulate request done by
// BookmarkModel::GetFavicon().
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results;
backend_->GetFaviconsForURL(page_url,
favicon_base::FAVICON,
GetEdgeSizesSmallAndLarge(),
&bitmap_results);
EXPECT_EQ(2u, bitmap_results.size());
const favicon_base::FaviconRawBitmapResult& first_result = bitmap_results[0];
const favicon_base::FaviconRawBitmapResult& result =
(first_result.pixel_size == kSmallSize) ? first_result
: bitmap_results[1];
EXPECT_TRUE(BitmapDataEqual('c', result.bitmap_data));
}
// Tests GetFaviconsForURL with icon_types priority,
TEST_F(HistoryBackendTest, TestGetFaviconsForURLWithIconTypesPriority) {
GURL page_url("http://www.google.com");
GURL icon_url("http://www.google.com/favicon.ico");
GURL touch_icon_url("http://wwww.google.com/touch_icon.ico");
std::vector<SkBitmap> favicon_bitmaps;
favicon_bitmaps.push_back(CreateBitmap(SK_ColorBLUE, 16));
favicon_bitmaps.push_back(CreateBitmap(SK_ColorRED, 32));
std::vector<SkBitmap> touch_bitmaps;
touch_bitmaps.push_back(CreateBitmap(SK_ColorWHITE, 64));
// Set some preexisting favicons for |page_url|.
backend_->SetFavicons(
page_url, favicon_base::FAVICON, icon_url, favicon_bitmaps);
backend_->SetFavicons(
page_url, favicon_base::TOUCH_ICON, touch_icon_url, touch_bitmaps);
favicon_base::FaviconRawBitmapResult result;
std::vector<int> icon_types;
icon_types.push_back(favicon_base::FAVICON);
icon_types.push_back(favicon_base::TOUCH_ICON);
backend_->GetLargestFaviconForURL(page_url, icon_types, 16, &result);
// Verify the result icon is 32x32 favicon.
EXPECT_EQ(gfx::Size(32, 32), result.pixel_size);
EXPECT_EQ(favicon_base::FAVICON, result.icon_type);
// Change Minimal size to 32x32 and verify the 64x64 touch icon returned.
backend_->GetLargestFaviconForURL(page_url, icon_types, 32, &result);
EXPECT_EQ(gfx::Size(64, 64), result.pixel_size);
EXPECT_EQ(favicon_base::TOUCH_ICON, result.icon_type);
}
// Test the the first types of icon is returned if its size equal to the
// second types icon.
TEST_F(HistoryBackendTest, TestGetFaviconsForURLReturnFavicon) {
GURL page_url("http://www.google.com");
GURL icon_url("http://www.google.com/favicon.ico");
GURL touch_icon_url("http://wwww.google.com/touch_icon.ico");
std::vector<SkBitmap> favicon_bitmaps;
favicon_bitmaps.push_back(CreateBitmap(SK_ColorBLUE, 16));
favicon_bitmaps.push_back(CreateBitmap(SK_ColorRED, 32));
std::vector<SkBitmap> touch_bitmaps;
touch_bitmaps.push_back(CreateBitmap(SK_ColorWHITE, 32));
// Set some preexisting favicons for |page_url|.
backend_->SetFavicons(
page_url, favicon_base::FAVICON, icon_url, favicon_bitmaps);
backend_->SetFavicons(
page_url, favicon_base::TOUCH_ICON, touch_icon_url, touch_bitmaps);
favicon_base::FaviconRawBitmapResult result;
std::vector<int> icon_types;
icon_types.push_back(favicon_base::FAVICON);
icon_types.push_back(favicon_base::TOUCH_ICON);
backend_->GetLargestFaviconForURL(page_url, icon_types, 16, &result);
// Verify the result icon is 32x32 favicon.
EXPECT_EQ(gfx::Size(32, 32), result.pixel_size);
EXPECT_EQ(favicon_base::FAVICON, result.icon_type);
// Change minimal size to 32x32 and verify the 32x32 favicon returned.
favicon_base::FaviconRawBitmapResult result1;
backend_->GetLargestFaviconForURL(page_url, icon_types, 32, &result1);
EXPECT_EQ(gfx::Size(32, 32), result1.pixel_size);
EXPECT_EQ(favicon_base::FAVICON, result1.icon_type);
}
// Test the favicon is returned if its size is smaller than minimal size,
// because it is only one available.
TEST_F(HistoryBackendTest, TestGetFaviconsForURLReturnFaviconEvenItSmaller) {
GURL page_url("http://www.google.com");
GURL icon_url("http://www.google.com/favicon.ico");
std::vector<SkBitmap> bitmaps;
bitmaps.push_back(CreateBitmap(SK_ColorBLUE, 16));
// Set preexisting favicons for |page_url|.
backend_->SetFavicons(page_url, favicon_base::FAVICON, icon_url, bitmaps);
favicon_base::FaviconRawBitmapResult result;
std::vector<int> icon_types;
icon_types.push_back(favicon_base::FAVICON);
icon_types.push_back(favicon_base::TOUCH_ICON);
backend_->GetLargestFaviconForURL(page_url, icon_types, 32, &result);
// Verify 16x16 icon is returned, even it small than minimal_size.
EXPECT_EQ(gfx::Size(16, 16), result.pixel_size);
EXPECT_EQ(favicon_base::FAVICON, result.icon_type);
}
// Test UpdateFaviconMapingsAndFetch() when multiple icon types are passed in.
TEST_F(HistoryBackendTest, UpdateFaviconMappingsAndFetchMultipleIconTypes) {
GURL page_url1("http://www.google.com");
GURL page_url2("http://news.google.com");
GURL page_url3("http://mail.google.com");
GURL icon_urla("http://www.google.com/favicon1.ico");
GURL icon_urlb("http://www.google.com/favicon2.ico");
std::vector<SkBitmap> bitmaps;
bitmaps.push_back(CreateBitmap(SK_ColorBLUE, kSmallEdgeSize));
// |page_url1| is mapped to |icon_urla| which if of type TOUCH_ICON.
backend_->SetFavicons(
page_url1, favicon_base::TOUCH_ICON, icon_urla, bitmaps);
// |page_url2| is mapped to |icon_urlb| which is of type
// TOUCH_PRECOMPOSED_ICON.
backend_->SetFavicons(
page_url2, favicon_base::TOUCH_PRECOMPOSED_ICON, icon_urlb, bitmaps);
std::vector<GURL> icon_urls;
icon_urls.push_back(icon_urla);
icon_urls.push_back(icon_urlb);
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results;
backend_->UpdateFaviconMappingsAndFetch(
page_url3,
icon_urls,
(favicon_base::TOUCH_ICON | favicon_base::TOUCH_PRECOMPOSED_ICON),
GetEdgeSizesSmallAndLarge(),
&bitmap_results);
// |page_url1| and |page_url2| should still be mapped to the same icon URLs.
std::vector<IconMapping> icon_mappings;
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url1,
&icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(icon_urla, icon_mappings[0].icon_url);
EXPECT_EQ(favicon_base::TOUCH_ICON, icon_mappings[0].icon_type);
icon_mappings.clear();
EXPECT_TRUE(GetSortedIconMappingsForPageURL(page_url2, &icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(icon_urlb, icon_mappings[0].icon_url);
EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON, icon_mappings[0].icon_type);
// |page_url3| should be mapped only to |icon_urlb| as TOUCH_PRECOMPOSED_ICON
// is the largest IconType.
icon_mappings.clear();
EXPECT_TRUE(GetSortedIconMappingsForPageURL(page_url3, &icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(icon_urlb, icon_mappings[0].icon_url);
EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON, icon_mappings[0].icon_type);
}
// Test the results of GetFaviconsFromDB() when there are no found favicons.
TEST_F(HistoryBackendTest, GetFaviconsFromDBEmpty) {
const GURL page_url("http://www.google.com/");
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results;
EXPECT_FALSE(backend_->GetFaviconsFromDB(page_url,
favicon_base::FAVICON,
GetEdgeSizesSmallAndLarge(),
&bitmap_results));
EXPECT_TRUE(bitmap_results.empty());
}
// Test the results of GetFaviconsFromDB() when there are matching favicons
// but there are no associated favicon bitmaps.
TEST_F(HistoryBackendTest, GetFaviconsFromDBNoFaviconBitmaps) {
const GURL page_url("http://www.google.com/");
const GURL icon_url("http://www.google.com/icon1");
favicon_base::FaviconID icon_id =
backend_->thumbnail_db_->AddFavicon(icon_url, favicon_base::FAVICON);
EXPECT_NE(0, icon_id);
EXPECT_NE(0, backend_->thumbnail_db_->AddIconMapping(page_url, icon_id));
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results_out;
EXPECT_FALSE(backend_->GetFaviconsFromDB(page_url,
favicon_base::FAVICON,
GetEdgeSizesSmallAndLarge(),
&bitmap_results_out));
EXPECT_TRUE(bitmap_results_out.empty());
}
// Test that GetFaviconsFromDB() returns results for the bitmaps which most
// closely match the passed in the desired pixel sizes.
TEST_F(HistoryBackendTest, GetFaviconsFromDBSelectClosestMatch) {
const GURL page_url("http://www.google.com/");
const GURL icon_url("http://www.google.com/icon1");
std::vector<SkBitmap> bitmaps;
bitmaps.push_back(CreateBitmap(SK_ColorWHITE, kTinyEdgeSize));
bitmaps.push_back(CreateBitmap(SK_ColorBLUE, kSmallEdgeSize));
bitmaps.push_back(CreateBitmap(SK_ColorRED, kLargeEdgeSize));
backend_->SetFavicons(page_url, favicon_base::FAVICON, icon_url, bitmaps);
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results_out;
EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url,
favicon_base::FAVICON,
GetEdgeSizesSmallAndLarge(),
&bitmap_results_out));
// The bitmap data for the small and large bitmaps should be returned as their
// sizes match exactly.
EXPECT_EQ(2u, bitmap_results_out.size());
// No required order for results.
if (bitmap_results_out[0].pixel_size == kLargeSize) {
favicon_base::FaviconRawBitmapResult tmp_result = bitmap_results_out[0];
bitmap_results_out[0] = bitmap_results_out[1];
bitmap_results_out[1] = tmp_result;
}
EXPECT_FALSE(bitmap_results_out[0].expired);
EXPECT_TRUE(
BitmapColorEqual(SK_ColorBLUE, bitmap_results_out[0].bitmap_data));
EXPECT_EQ(kSmallSize, bitmap_results_out[0].pixel_size);
EXPECT_EQ(icon_url, bitmap_results_out[0].icon_url);
EXPECT_EQ(favicon_base::FAVICON, bitmap_results_out[0].icon_type);
EXPECT_FALSE(bitmap_results_out[1].expired);
EXPECT_TRUE(BitmapColorEqual(SK_ColorRED, bitmap_results_out[1].bitmap_data));
EXPECT_EQ(kLargeSize, bitmap_results_out[1].pixel_size);
EXPECT_EQ(icon_url, bitmap_results_out[1].icon_url);
EXPECT_EQ(favicon_base::FAVICON, bitmap_results_out[1].icon_type);
}
// Test the results of GetFaviconsFromDB() when called with different
// |icon_types|.
TEST_F(HistoryBackendTest, GetFaviconsFromDBIconType) {
const GURL page_url("http://www.google.com/");
const GURL icon_url1("http://www.google.com/icon1.png");
const GURL icon_url2("http://www.google.com/icon2.png");
std::vector<SkBitmap> bitmaps;
bitmaps.push_back(CreateBitmap(SK_ColorBLUE, kSmallEdgeSize));
std::vector<favicon_base::FaviconRawBitmapData> favicon_bitmap_data;
backend_->SetFavicons(page_url, favicon_base::FAVICON, icon_url1, bitmaps);
backend_->SetFavicons(page_url, favicon_base::TOUCH_ICON, icon_url2, bitmaps);
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results_out;
EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url,
favicon_base::FAVICON,
GetEdgeSizesSmallAndLarge(),
&bitmap_results_out));
EXPECT_EQ(1u, bitmap_results_out.size());
EXPECT_EQ(favicon_base::FAVICON, bitmap_results_out[0].icon_type);
EXPECT_EQ(icon_url1, bitmap_results_out[0].icon_url);
bitmap_results_out.clear();
EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url,
favicon_base::TOUCH_ICON,
GetEdgeSizesSmallAndLarge(),
&bitmap_results_out));
EXPECT_EQ(1u, bitmap_results_out.size());
EXPECT_EQ(favicon_base::TOUCH_ICON, bitmap_results_out[0].icon_type);
EXPECT_EQ(icon_url2, bitmap_results_out[0].icon_url);
}
// Test that GetFaviconsFromDB() correctly sets the expired flag for bitmap
// reults.
TEST_F(HistoryBackendTest, GetFaviconsFromDBExpired) {
const GURL page_url("http://www.google.com/");
const GURL icon_url("http://www.google.com/icon.png");
std::vector<unsigned char> data;
data.push_back('a');
scoped_refptr<base::RefCountedBytes> bitmap_data(
base::RefCountedBytes::TakeVector(&data));
base::Time last_updated = base::Time::FromTimeT(0);
favicon_base::FaviconID icon_id = backend_->thumbnail_db_->AddFavicon(
icon_url, favicon_base::FAVICON, bitmap_data, last_updated, kSmallSize);
EXPECT_NE(0, icon_id);
EXPECT_NE(0, backend_->thumbnail_db_->AddIconMapping(page_url, icon_id));
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results_out;
EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url,
favicon_base::FAVICON,
GetEdgeSizesSmallAndLarge(),
&bitmap_results_out));
EXPECT_EQ(1u, bitmap_results_out.size());
EXPECT_TRUE(bitmap_results_out[0].expired);
}
// Check that UpdateFaviconMappingsAndFetch() call back to the UI when there is
// no valid thumbnail database.
TEST_F(HistoryBackendTest, UpdateFaviconMappingsAndFetchNoDB) {
// Make the thumbnail database invalid.
backend_->thumbnail_db_.reset();
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results;
backend_->UpdateFaviconMappingsAndFetch(GURL(),
std::vector<GURL>(),
favicon_base::FAVICON,
GetEdgeSizesSmallAndLarge(),
&bitmap_results);
EXPECT_TRUE(bitmap_results.empty());
}
TEST_F(HistoryBackendTest, CloneFaviconIsRestrictedToSameDomain) {
const GURL url("http://www.google.com/");
const GURL same_domain_url("http://www.google.com/subdir/index.html");
const GURL foreign_domain_url("http://www.not-google.com/");
const GURL icon_url("http://www.google.com/icon.png");
std::vector<SkBitmap> bitmaps;
bitmaps.push_back(CreateBitmap(SK_ColorBLUE, kSmallEdgeSize));
// Add a favicon
backend_->SetFavicons(url, favicon_base::FAVICON, icon_url, bitmaps);
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
url, favicon_base::FAVICON, NULL));
// Validate starting state.
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results_out;
EXPECT_TRUE(backend_->GetFaviconsFromDB(url,
favicon_base::FAVICON,
GetEdgeSizesSmallAndLarge(),
&bitmap_results_out));
EXPECT_FALSE(backend_->GetFaviconsFromDB(same_domain_url,
favicon_base::FAVICON,
GetEdgeSizesSmallAndLarge(),
&bitmap_results_out));
EXPECT_FALSE(backend_->GetFaviconsFromDB(foreign_domain_url,
favicon_base::FAVICON,
GetEdgeSizesSmallAndLarge(),
&bitmap_results_out));
// Same-domain cloning should work.
backend_->CloneFavicons(url, same_domain_url);
EXPECT_TRUE(backend_->GetFaviconsFromDB(same_domain_url,
favicon_base::FAVICON,
GetEdgeSizesSmallAndLarge(),
&bitmap_results_out));
// Foreign-domain cloning is forbidden.
backend_->CloneFavicons(url, foreign_domain_url);
EXPECT_FALSE(backend_->GetFaviconsFromDB(foreign_domain_url,
favicon_base::FAVICON,
GetEdgeSizesSmallAndLarge(),
&bitmap_results_out));
}
TEST_F(HistoryBackendTest, QueryFilteredURLs) {
const char* google = "http://www.google.com/";
const char* yahoo = "http://www.yahoo.com/";
const char* yahoo_sports = "http://sports.yahoo.com/";
const char* yahoo_sports_with_article1 =
"http://sports.yahoo.com/article1.htm";
const char* yahoo_sports_with_article2 =
"http://sports.yahoo.com/article2.htm";
const char* yahoo_sports_soccer = "http://sports.yahoo.com/soccer";
const char* apple = "http://www.apple.com/";
// Clear all history.
backend_->DeleteAllHistory();
Time tested_time = Time::Now().LocalMidnight() +
base::TimeDelta::FromHours(4);
base::TimeDelta half_an_hour = base::TimeDelta::FromMinutes(30);
base::TimeDelta one_hour = base::TimeDelta::FromHours(1);
base::TimeDelta one_day = base::TimeDelta::FromDays(1);
const ui::PageTransition kTypedTransition =
ui::PAGE_TRANSITION_TYPED;
const ui::PageTransition kKeywordGeneratedTransition =
ui::PAGE_TRANSITION_KEYWORD_GENERATED;
const char* redirect_sequence[2];
redirect_sequence[1] = NULL;
redirect_sequence[0] = google;
AddRedirectChainWithTransitionAndTime(
redirect_sequence, 0, kTypedTransition,
tested_time - one_day - half_an_hour * 2);
AddRedirectChainWithTransitionAndTime(
redirect_sequence, 0,
kTypedTransition, tested_time - one_day);
AddRedirectChainWithTransitionAndTime(
redirect_sequence, 0,
kTypedTransition, tested_time - half_an_hour / 2);
AddRedirectChainWithTransitionAndTime(
redirect_sequence, 0,
kTypedTransition, tested_time);
// Add a visit with a transition that will make sure that no segment gets
// created for this page (so the subsequent entries will have different URLIDs
// and SegmentIDs).
redirect_sequence[0] = apple;
AddRedirectChainWithTransitionAndTime(
redirect_sequence, 0, kKeywordGeneratedTransition,
tested_time - one_day + one_hour * 6);
redirect_sequence[0] = yahoo;
AddRedirectChainWithTransitionAndTime(
redirect_sequence, 0, kTypedTransition,
tested_time - one_day + half_an_hour);
AddRedirectChainWithTransitionAndTime(
redirect_sequence, 0, kTypedTransition,
tested_time - one_day + half_an_hour * 2);
redirect_sequence[0] = yahoo_sports;
AddRedirectChainWithTransitionAndTime(
redirect_sequence, 0, kTypedTransition,
tested_time - one_day - half_an_hour * 2);
AddRedirectChainWithTransitionAndTime(
redirect_sequence, 0, kTypedTransition,
tested_time - one_day);
int transition1, transition2;
AddClientRedirect(GURL(yahoo_sports), GURL(yahoo_sports_with_article1), false,
tested_time - one_day + half_an_hour,
&transition1, &transition2);
AddClientRedirect(GURL(yahoo_sports_with_article1),
GURL(yahoo_sports_with_article2),
false,
tested_time - one_day + half_an_hour * 2,
&transition1, &transition2);
redirect_sequence[0] = yahoo_sports_soccer;
AddRedirectChainWithTransitionAndTime(redirect_sequence, 0,
kTypedTransition,
tested_time - half_an_hour);
backend_->Commit();
VisitFilter filter;
FilteredURLList filtered_list;
// Time limit is |tested_time| +/- 45 min.
base::TimeDelta three_quarters_of_an_hour = base::TimeDelta::FromMinutes(45);
filter.SetFilterTime(tested_time);
filter.SetFilterWidth(three_quarters_of_an_hour);
backend_->QueryFilteredURLs(100, filter, false, &filtered_list);
ASSERT_EQ(4U, filtered_list.size());
EXPECT_EQ(std::string(google), filtered_list[0].url.spec());
EXPECT_EQ(std::string(yahoo_sports_soccer), filtered_list[1].url.spec());
EXPECT_EQ(std::string(yahoo), filtered_list[2].url.spec());
EXPECT_EQ(std::string(yahoo_sports), filtered_list[3].url.spec());
// Time limit is between |tested_time| and |tested_time| + 2 hours.
filter.SetFilterTime(tested_time + one_hour);
filter.SetFilterWidth(one_hour);
backend_->QueryFilteredURLs(100, filter, false, &filtered_list);
ASSERT_EQ(3U, filtered_list.size());
EXPECT_EQ(std::string(google), filtered_list[0].url.spec());
EXPECT_EQ(std::string(yahoo), filtered_list[1].url.spec());
EXPECT_EQ(std::string(yahoo_sports), filtered_list[2].url.spec());
// Time limit is between |tested_time| - 2 hours and |tested_time|.
filter.SetFilterTime(tested_time - one_hour);
filter.SetFilterWidth(one_hour);
backend_->QueryFilteredURLs(100, filter, false, &filtered_list);
ASSERT_EQ(3U, filtered_list.size());
EXPECT_EQ(std::string(google), filtered_list[0].url.spec());
EXPECT_EQ(std::string(yahoo_sports_soccer), filtered_list[1].url.spec());
EXPECT_EQ(std::string(yahoo_sports), filtered_list[2].url.spec());
filter.ClearFilters();
base::Time::Exploded exploded_time;
tested_time.LocalExplode(&exploded_time);
// Today.
filter.SetFilterTime(tested_time);
filter.SetDayOfTheWeekFilter(static_cast<int>(exploded_time.day_of_week));
backend_->QueryFilteredURLs(100, filter, false, &filtered_list);
ASSERT_EQ(2U, filtered_list.size());
EXPECT_EQ(std::string(google), filtered_list[0].url.spec());
EXPECT_EQ(std::string(yahoo_sports_soccer), filtered_list[1].url.spec());
// Today + time limit - only yahoo_sports_soccer should fit.
filter.SetFilterTime(tested_time - base::TimeDelta::FromMinutes(40));
filter.SetFilterWidth(base::TimeDelta::FromMinutes(20));
backend_->QueryFilteredURLs(100, filter, false, &filtered_list);
ASSERT_EQ(1U, filtered_list.size());
EXPECT_EQ(std::string(yahoo_sports_soccer), filtered_list[0].url.spec());
// Make sure we get debug data if we request it.
filter.SetFilterTime(tested_time);
filter.SetFilterWidth(one_hour * 2);
backend_->QueryFilteredURLs(100, filter, true, &filtered_list);
// If the SegmentID is used by QueryFilteredURLs when generating the debug
// data instead of the URLID, the |total_visits| for the |yahoo_sports_soccer|
// entry will be zero instead of 1.
ASSERT_GE(filtered_list.size(), 2U);
EXPECT_EQ(std::string(google), filtered_list[0].url.spec());
EXPECT_EQ(std::string(yahoo_sports_soccer), filtered_list[1].url.spec());
EXPECT_EQ(4U, filtered_list[0].extended_info.total_visits);
EXPECT_EQ(1U, filtered_list[1].extended_info.total_visits);
}
TEST_F(HistoryBackendTest, UpdateVisitDuration) {
// This unit test will test adding and deleting visit details information.
ASSERT_TRUE(backend_.get());
GURL url1("http://www.cnn.com");
std::vector<VisitInfo> visit_info1, visit_info2;
Time start_ts = Time::Now() - base::TimeDelta::FromDays(5);
Time end_ts = start_ts + base::TimeDelta::FromDays(2);
visit_info1.push_back(VisitInfo(start_ts, ui::PAGE_TRANSITION_LINK));
GURL url2("http://www.example.com");
visit_info2.push_back(VisitInfo(Time::Now() - base::TimeDelta::FromDays(10),
ui::PAGE_TRANSITION_LINK));
// Clear all history.
backend_->DeleteAllHistory();
// Add the visits.
backend_->AddVisits(url1, visit_info1, history::SOURCE_BROWSED);
backend_->AddVisits(url2, visit_info2, history::SOURCE_BROWSED);
// Verify the entries for both visits were added in visit_details.
VisitVector visits1, visits2;
URLRow row;
URLID url_id1 = backend_->db()->GetRowForURL(url1, &row);
ASSERT_TRUE(backend_->db()->GetVisitsForURL(url_id1, &visits1));
ASSERT_EQ(1U, visits1.size());
EXPECT_EQ(0, visits1[0].visit_duration.ToInternalValue());
URLID url_id2 = backend_->db()->GetRowForURL(url2, &row);
ASSERT_TRUE(backend_->db()->GetVisitsForURL(url_id2, &visits2));
ASSERT_EQ(1U, visits2.size());
EXPECT_EQ(0, visits2[0].visit_duration.ToInternalValue());
// Update the visit to cnn.com.
backend_->UpdateVisitDuration(visits1[0].visit_id, end_ts);
// Check the duration for visiting cnn.com was correctly updated.
ASSERT_TRUE(backend_->db()->GetVisitsForURL(url_id1, &visits1));
ASSERT_EQ(1U, visits1.size());
base::TimeDelta expected_duration = end_ts - start_ts;
EXPECT_EQ(expected_duration.ToInternalValue(),
visits1[0].visit_duration.ToInternalValue());
// Remove the visit to cnn.com.
ASSERT_TRUE(backend_->RemoveVisits(visits1));
}
// Test for migration of adding visit_duration column.
TEST_F(HistoryBackendTest, MigrationVisitDuration) {
ASSERT_TRUE(backend_.get());
backend_->Closing();
backend_ = NULL;
base::FilePath old_history_path, old_history;
ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &old_history_path));
old_history_path = old_history_path.AppendASCII("History");
old_history = old_history_path.AppendASCII("HistoryNoDuration");
// Copy history database file to current directory so that it will be deleted
// in Teardown.
base::FilePath new_history_path(test_dir());
base::DeleteFile(new_history_path, true);
base::CreateDirectory(new_history_path);
base::FilePath new_history_file = new_history_path.Append(kHistoryFilename);
ASSERT_TRUE(base::CopyFile(old_history, new_history_file));
backend_ = new HistoryBackend(new HistoryBackendTestDelegate(this),
&history_client_);
backend_->Init(std::string(), false,
TestHistoryDatabaseParamsForPath(new_history_path));
backend_->Closing();
backend_ = NULL;
// Now the history database should already be migrated.
// Check version in history database first.
int cur_version = HistoryDatabase::GetCurrentVersion();
sql::Connection db;
ASSERT_TRUE(db.Open(new_history_file));
sql::Statement s(db.GetUniqueStatement(
"SELECT value FROM meta WHERE key = 'version'"));
ASSERT_TRUE(s.Step());
int file_version = s.ColumnInt(0);
EXPECT_EQ(cur_version, file_version);
// Check visit_duration column in visits table is created and set to 0.
s.Assign(db.GetUniqueStatement(
"SELECT visit_duration FROM visits LIMIT 1"));
ASSERT_TRUE(s.Step());
EXPECT_EQ(0, s.ColumnInt(0));
}
TEST_F(HistoryBackendTest, AddPageNoVisitForBookmark) {
ASSERT_TRUE(backend_.get());
GURL url("http://www.google.com");
base::string16 title(base::UTF8ToUTF16("Bookmark title"));
backend_->AddPageNoVisitForBookmark(url, title);
URLRow row;
backend_->GetURL(url, &row);
EXPECT_EQ(url, row.url());
EXPECT_EQ(title, row.title());
EXPECT_EQ(0, row.visit_count());
backend_->DeleteURL(url);
backend_->AddPageNoVisitForBookmark(url, base::string16());
backend_->GetURL(url, &row);
EXPECT_EQ(url, row.url());
EXPECT_EQ(base::UTF8ToUTF16(url.spec()), row.title());
EXPECT_EQ(0, row.visit_count());
}
TEST_F(HistoryBackendTest, ExpireHistoryForTimes) {
ASSERT_TRUE(backend_.get());
HistoryAddPageArgs args[10];
for (size_t i = 0; i < arraysize(args); ++i) {
args[i].url = GURL("http://example" +
std::string((i % 2 == 0 ? ".com" : ".net")));
args[i].time = base::Time::FromInternalValue(i);
backend_->AddPage(args[i]);
}
EXPECT_EQ(base::Time(), backend_->GetFirstRecordedTimeForTest());
URLRow row;
for (size_t i = 0; i < arraysize(args); ++i) {
EXPECT_TRUE(backend_->GetURL(args[i].url, &row));
}
std::set<base::Time> times;
times.insert(args[5].time);
backend_->ExpireHistoryForTimes(times,
base::Time::FromInternalValue(2),
base::Time::FromInternalValue(8));
EXPECT_EQ(base::Time::FromInternalValue(0),
backend_->GetFirstRecordedTimeForTest());
// Visits to http://example.com are untouched.
VisitVector visit_vector;
EXPECT_TRUE(backend_->GetVisitsForURL(
backend_->db_->GetRowForURL(GURL("http://example.com"), NULL),
&visit_vector));
ASSERT_EQ(5u, visit_vector.size());
EXPECT_EQ(base::Time::FromInternalValue(0), visit_vector[0].visit_time);
EXPECT_EQ(base::Time::FromInternalValue(2), visit_vector[1].visit_time);
EXPECT_EQ(base::Time::FromInternalValue(4), visit_vector[2].visit_time);
EXPECT_EQ(base::Time::FromInternalValue(6), visit_vector[3].visit_time);
EXPECT_EQ(base::Time::FromInternalValue(8), visit_vector[4].visit_time);
// Visits to http://example.net between [2,8] are removed.
visit_vector.clear();
EXPECT_TRUE(backend_->GetVisitsForURL(
backend_->db_->GetRowForURL(GURL("http://example.net"), NULL),
&visit_vector));
ASSERT_EQ(2u, visit_vector.size());
EXPECT_EQ(base::Time::FromInternalValue(1), visit_vector[0].visit_time);
EXPECT_EQ(base::Time::FromInternalValue(9), visit_vector[1].visit_time);
EXPECT_EQ(base::Time::FromInternalValue(0),
backend_->GetFirstRecordedTimeForTest());
}
TEST_F(HistoryBackendTest, ExpireHistory) {
ASSERT_TRUE(backend_.get());
// Since history operations are dependent on the local timezone, make all
// entries relative to a fixed, local reference time.
base::Time reference_time = base::Time::UnixEpoch().LocalMidnight() +
base::TimeDelta::FromHours(12);
// Insert 4 entries into the database.
HistoryAddPageArgs args[4];
for (size_t i = 0; i < arraysize(args); ++i) {
args[i].url = GURL("http://example" + base::IntToString(i) + ".com");
args[i].time = reference_time + base::TimeDelta::FromDays(i);
backend_->AddPage(args[i]);
}
URLRow url_rows[4];
for (unsigned int i = 0; i < arraysize(args); ++i)
ASSERT_TRUE(backend_->GetURL(args[i].url, &url_rows[i]));
std::vector<ExpireHistoryArgs> expire_list;
VisitVector visits;
// Passing an empty map should be a no-op.
backend_->ExpireHistory(expire_list);
backend_->db()->GetAllVisitsInRange(base::Time(), base::Time(), 0, &visits);
EXPECT_EQ(4U, visits.size());
// Trying to delete an unknown URL with the time of the first visit should
// also be a no-op.
expire_list.resize(expire_list.size() + 1);
expire_list[0].SetTimeRangeForOneDay(args[0].time);
expire_list[0].urls.insert(GURL("http://google.does-not-exist"));
backend_->ExpireHistory(expire_list);
backend_->db()->GetAllVisitsInRange(base::Time(), base::Time(), 0, &visits);
EXPECT_EQ(4U, visits.size());
// Now add the first URL with the same time -- it should get deleted.
expire_list.back().urls.insert(url_rows[0].url());
backend_->ExpireHistory(expire_list);
backend_->db()->GetAllVisitsInRange(base::Time(), base::Time(), 0, &visits);
ASSERT_EQ(3U, visits.size());
EXPECT_EQ(visits[0].url_id, url_rows[1].id());
EXPECT_EQ(visits[1].url_id, url_rows[2].id());
EXPECT_EQ(visits[2].url_id, url_rows[3].id());
// The first recorded time should also get updated.
EXPECT_EQ(backend_->GetFirstRecordedTimeForTest(), args[1].time);
// Now delete the rest of the visits in one call.
for (unsigned int i = 1; i < arraysize(args); ++i) {
expire_list.resize(expire_list.size() + 1);
expire_list[i].SetTimeRangeForOneDay(args[i].time);
expire_list[i].urls.insert(args[i].url);
}
backend_->ExpireHistory(expire_list);
backend_->db()->GetAllVisitsInRange(base::Time(), base::Time(), 0, &visits);
ASSERT_EQ(0U, visits.size());
}
TEST_F(HistoryBackendTest, DeleteMatchingUrlsForKeyword) {
// Set up urls and keyword_search_terms
GURL url1("https://www.bing.com/?q=bar");
URLRow url_info1(url1);
url_info1.set_visit_count(0);
url_info1.set_typed_count(0);
url_info1.set_last_visit(Time());
url_info1.set_hidden(false);
const URLID url1_id = backend_->db()->AddURL(url_info1);
EXPECT_NE(0, url1_id);
KeywordID keyword_id = 1;
base::string16 keyword = base::UTF8ToUTF16("bar");
ASSERT_TRUE(backend_->db()->SetKeywordSearchTermsForURL(
url1_id, keyword_id, keyword));
GURL url2("https://www.google.com/?q=bar");
URLRow url_info2(url2);
url_info2.set_visit_count(0);
url_info2.set_typed_count(0);
url_info2.set_last_visit(Time());
url_info2.set_hidden(false);
const URLID url2_id = backend_->db()->AddURL(url_info2);
EXPECT_NE(0, url2_id);
KeywordID keyword_id2 = 2;
ASSERT_TRUE(backend_->db()->SetKeywordSearchTermsForURL(
url2_id, keyword_id2, keyword));
// Add another visit to the same URL
URLRow url_info3(url2);
url_info3.set_visit_count(0);
url_info3.set_typed_count(0);
url_info3.set_last_visit(Time());
url_info3.set_hidden(false);
const URLID url3_id = backend_->db()->AddURL(url_info3);
EXPECT_NE(0, url3_id);
ASSERT_TRUE(backend_->db()->SetKeywordSearchTermsForURL(
url3_id, keyword_id2, keyword));
// Test that deletion works correctly
backend_->DeleteMatchingURLsForKeyword(keyword_id2, keyword);
// Test that rows 2 and 3 are deleted, while 1 is intact
URLRow row;
EXPECT_TRUE(backend_->db()->GetURLRow(url1_id, &row));
EXPECT_EQ(url1.spec(), row.url().spec());
EXPECT_FALSE(backend_->db()->GetURLRow(url2_id, &row));
EXPECT_FALSE(backend_->db()->GetURLRow(url3_id, &row));
// Test that corresponding keyword search terms are deleted for rows 2 & 3,
// but not for row 1
EXPECT_TRUE(backend_->db()->GetKeywordSearchTermRow(url1_id, NULL));
EXPECT_FALSE(backend_->db()->GetKeywordSearchTermRow(url2_id, NULL));
EXPECT_FALSE(backend_->db()->GetKeywordSearchTermRow(url3_id, NULL));
}
// Simple test that removes a bookmark. This test exercises the code paths in
// History that block till bookmark bar model is loaded.
TEST_F(HistoryBackendTest, RemoveNotification) {
scoped_ptr<TestingProfile> profile(new TestingProfile());
// Add a URL.
GURL url("http://www.google.com");
HistoryClientMock history_client;
history_client.AddBookmark(url);
scoped_ptr<HistoryService> service(new HistoryService(
&history_client, scoped_ptr<history::VisitDelegate>()));
EXPECT_TRUE(
service->Init(profile->GetPrefs()->GetString(prefs::kAcceptLanguages),
TestHistoryDatabaseParamsForPath(profile->GetPath())));
service->AddPage(
url, base::Time::Now(), NULL, 1, GURL(), RedirectList(),
ui::PAGE_TRANSITION_TYPED, SOURCE_BROWSED, false);
// This won't actually delete the URL, rather it'll empty out the visits.
// This triggers blocking on the BookmarkModel.
EXPECT_CALL(history_client, BlockUntilBookmarksLoaded());
service->DeleteURL(url);
}
// Test DeleteFTSIndexDatabases deletes expected files.
TEST_F(HistoryBackendTest, DeleteFTSIndexDatabases) {
ASSERT_TRUE(backend_.get());
base::FilePath history_path(test_dir());
base::FilePath db1(history_path.AppendASCII("History Index 2013-05"));
base::FilePath db1_journal(db1.InsertBeforeExtensionASCII("-journal"));
base::FilePath db1_wal(db1.InsertBeforeExtensionASCII("-wal"));
base::FilePath db2_symlink(history_path.AppendASCII("History Index 2013-06"));
base::FilePath db2_actual(history_path.AppendASCII("Underlying DB"));
// Setup dummy index database files.
const char* data = "Dummy";
const size_t data_len = 5;
ASSERT_TRUE(base::WriteFile(db1, data, data_len));
ASSERT_TRUE(base::WriteFile(db1_journal, data, data_len));
ASSERT_TRUE(base::WriteFile(db1_wal, data, data_len));
ASSERT_TRUE(base::WriteFile(db2_actual, data, data_len));
#if defined(OS_POSIX)
EXPECT_TRUE(base::CreateSymbolicLink(db2_actual, db2_symlink));
#endif
// Delete all DTS index databases.
backend_->DeleteFTSIndexDatabases();
EXPECT_FALSE(base::PathExists(db1));
EXPECT_FALSE(base::PathExists(db1_wal));
EXPECT_FALSE(base::PathExists(db1_journal));
EXPECT_FALSE(base::PathExists(db2_symlink));
EXPECT_TRUE(base::PathExists(db2_actual)); // Symlinks shouldn't be followed.
}
// Common implementation for the two tests below, given that the only difference
// between them is the type of the notification sent out.
void InMemoryHistoryBackendTest::TestAddingAndChangingURLRows(
const SimulateNotificationCallback& callback) {
const char kTestTypedURLAlternativeTitle[] = "Google Search Again";
const char kTestNonTypedURLAlternativeTitle[] = "Google News Again";
// Notify the in-memory database that a typed and non-typed URLRow (which were
// never before seen by the cache) have been modified.
URLRow row1(CreateTestTypedURL());
URLRow row2(CreateTestNonTypedURL());
callback.Run(&row1, &row2, nullptr);
// The in-memory database should only pick up the typed URL, and should ignore
// the non-typed one. The typed URL should retain the ID that was present in
// the notification.
URLRow cached_row1, cached_row2;
EXPECT_NE(0, mem_backend_->db()->GetRowForURL(row1.url(), &cached_row1));
EXPECT_EQ(0, mem_backend_->db()->GetRowForURL(row2.url(), &cached_row2));
EXPECT_EQ(row1.id(), cached_row1.id());
// Try changing attributes (other than typed_count) for existing URLRows.
row1.set_title(base::UTF8ToUTF16(kTestTypedURLAlternativeTitle));
row2.set_title(base::UTF8ToUTF16(kTestNonTypedURLAlternativeTitle));
callback.Run(&row1, &row2, nullptr);
// URLRows that are cached by the in-memory database should be updated.
EXPECT_NE(0, mem_backend_->db()->GetRowForURL(row1.url(), &cached_row1));
EXPECT_EQ(0, mem_backend_->db()->GetRowForURL(row2.url(), &cached_row2));
EXPECT_EQ(base::UTF8ToUTF16(kTestTypedURLAlternativeTitle),
cached_row1.title());
// Now decrease the typed count for the typed URLRow, and increase it for the
// previously non-typed URLRow.
row1.set_typed_count(0);
row2.set_typed_count(2);
callback.Run(&row1, &row2, nullptr);
// The in-memory database should stop caching the first URLRow, and start
// caching the second URLRow.
EXPECT_EQ(0, mem_backend_->db()->GetRowForURL(row1.url(), &cached_row1));
EXPECT_NE(0, mem_backend_->db()->GetRowForURL(row2.url(), &cached_row2));
EXPECT_EQ(row2.id(), cached_row2.id());
EXPECT_EQ(base::UTF8ToUTF16(kTestNonTypedURLAlternativeTitle),
cached_row2.title());
}
TEST_F(InMemoryHistoryBackendTest, OnURLsModified) {
TestAddingAndChangingURLRows(base::Bind(
&SimulateNotificationURLsModified, base::Unretained(mem_backend_.get())));
}
TEST_F(InMemoryHistoryBackendTest, OnURLsVisisted) {
TestAddingAndChangingURLRows(base::Bind(
&SimulateNotificationURLVisited, base::Unretained(mem_backend_.get())));
}
TEST_F(InMemoryHistoryBackendTest, OnURLsDeletedPiecewise) {
// Add two typed and one non-typed URLRow to the in-memory database.
URLRow row1(CreateTestTypedURL());
URLRow row2(CreateAnotherTestTypedURL());
URLRow row3(CreateTestNonTypedURL());
SimulateNotificationURLsModified(mem_backend_.get(), &row1, &row2, &row3);
// Notify the in-memory database that the second typed URL and the non-typed
// URL has been deleted.
SimulateNotificationURLsDeleted(&row2, &row3);
// Expect that the first typed URL remains intact, the second typed URL is
// correctly removed, and the non-typed URL does not magically appear.
URLRow cached_row1;
EXPECT_NE(0, mem_backend_->db()->GetRowForURL(row1.url(), &cached_row1));
EXPECT_EQ(0, mem_backend_->db()->GetRowForURL(row2.url(), NULL));
EXPECT_EQ(0, mem_backend_->db()->GetRowForURL(row3.url(), NULL));
EXPECT_EQ(row1.id(), cached_row1.id());
}
TEST_F(InMemoryHistoryBackendTest, OnURLsDeletedEnMasse) {
// Add two typed and one non-typed URLRow to the in-memory database.
URLRow row1(CreateTestTypedURL());
URLRow row2(CreateAnotherTestTypedURL());
URLRow row3(CreateTestNonTypedURL());
SimulateNotificationURLsModified(mem_backend_.get(), &row1, &row2, &row3);
// Now notify the in-memory database that all history has been deleted.
mem_backend_->OnURLsDeleted(nullptr, true, false, URLRows(),
std::set<GURL>());
// Expect that everything goes away.
EXPECT_EQ(0, mem_backend_->db()->GetRowForURL(row1.url(), NULL));
EXPECT_EQ(0, mem_backend_->db()->GetRowForURL(row2.url(), NULL));
EXPECT_EQ(0, mem_backend_->db()->GetRowForURL(row3.url(), NULL));
}
void InMemoryHistoryBackendTest::PopulateTestURLsAndSearchTerms(
URLRow* row1,
URLRow* row2,
const base::string16& term1,
const base::string16& term2) {
// Add a typed and a non-typed URLRow to the in-memory database. This time,
// though, do it through the history backend...
URLRows rows;
rows.push_back(*row1);
rows.push_back(*row2);
backend_->AddPagesWithDetails(rows, history::SOURCE_BROWSED);
backend_->db()->GetRowForURL(row1->url(), row1); // Get effective IDs from
backend_->db()->GetRowForURL(row2->url(), row2); // the database.
// ... so that we can also use that for adding the search terms. This way, we
// not only test that the notifications involved are handled correctly, but
// also that they are fired correctly (in the history backend).
backend_->SetKeywordSearchTermsForURL(row1->url(), kTestKeywordId, term1);
backend_->SetKeywordSearchTermsForURL(row2->url(), kTestKeywordId, term2);
}
TEST_F(InMemoryHistoryBackendTest, SetKeywordSearchTerms) {
URLRow row1(CreateTestTypedURL());
URLRow row2(CreateTestNonTypedURL());
base::string16 term1(base::UTF8ToUTF16(kTestSearchTerm1));
base::string16 term2(base::UTF8ToUTF16(kTestSearchTerm2));
PopulateTestURLsAndSearchTerms(&row1, &row2, term1, term2);
// Both URLs now have associated search terms, so the in-memory database
// should cache both of them, regardless whether they have been typed or not.
URLRow cached_row1, cached_row2;
EXPECT_NE(0, mem_backend_->db()->GetRowForURL(row1.url(), &cached_row1));
EXPECT_NE(0, mem_backend_->db()->GetRowForURL(row2.url(), &cached_row2));
EXPECT_EQ(row1.id(), cached_row1.id());
EXPECT_EQ(row2.id(), cached_row2.id());
// Verify that lookups will actually return both search terms; and also check
// at the low level that the rows are there.
EXPECT_EQ(1u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term1));
EXPECT_EQ(1u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term2));
EXPECT_TRUE(mem_backend_->db()->GetKeywordSearchTermRow(row1.id(), NULL));
EXPECT_TRUE(mem_backend_->db()->GetKeywordSearchTermRow(row2.id(), NULL));
}
TEST_F(InMemoryHistoryBackendTest, DeleteKeywordSearchTerms) {
URLRow row1(CreateTestTypedURL());
URLRow row2(CreateTestNonTypedURL());
base::string16 term1(base::UTF8ToUTF16(kTestSearchTerm1));
base::string16 term2(base::UTF8ToUTF16(kTestSearchTerm2));
PopulateTestURLsAndSearchTerms(&row1, &row2, term1, term2);
// Delete both search terms. This should be reflected in the in-memory DB.
backend_->DeleteKeywordSearchTermForURL(row1.url());
backend_->DeleteKeywordSearchTermForURL(row2.url());
// The typed URL should remain intact.
// Note: we do not need to guarantee anything about the non-typed URL.
URLRow cached_row1;
EXPECT_NE(0, mem_backend_->db()->GetRowForURL(row1.url(), &cached_row1));
EXPECT_EQ(row1.id(), cached_row1.id());
// Verify that the search terms are no longer returned as results, and also
// check at the low level that they are gone for good.
EXPECT_EQ(0u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term1));
EXPECT_EQ(0u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term2));
EXPECT_FALSE(mem_backend_->db()->GetKeywordSearchTermRow(row1.id(), NULL));
EXPECT_FALSE(mem_backend_->db()->GetKeywordSearchTermRow(row2.id(), NULL));
}
TEST_F(InMemoryHistoryBackendTest, DeleteAllSearchTermsForKeyword) {
URLRow row1(CreateTestTypedURL());
URLRow row2(CreateTestNonTypedURL());
base::string16 term1(base::UTF8ToUTF16(kTestSearchTerm1));
base::string16 term2(base::UTF8ToUTF16(kTestSearchTerm2));
PopulateTestURLsAndSearchTerms(&row1, &row2, term1, term2);
// Delete all corresponding search terms from the in-memory database.
KeywordID id = kTestKeywordId;
mem_backend_->DeleteAllSearchTermsForKeyword(id);
// The typed URL should remain intact.
// Note: we do not need to guarantee anything about the non-typed URL.
URLRow cached_row1;
EXPECT_NE(0, mem_backend_->db()->GetRowForURL(row1.url(), &cached_row1));
EXPECT_EQ(row1.id(), cached_row1.id());
// Verify that the search terms are no longer returned as results, and also
// check at the low level that they are gone for good.
EXPECT_EQ(0u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term1));
EXPECT_EQ(0u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term2));
EXPECT_FALSE(mem_backend_->db()->GetKeywordSearchTermRow(row1.id(), NULL));
EXPECT_FALSE(mem_backend_->db()->GetKeywordSearchTermRow(row2.id(), NULL));
}
TEST_F(InMemoryHistoryBackendTest, OnURLsDeletedWithSearchTerms) {
URLRow row1(CreateTestTypedURL());
URLRow row2(CreateTestNonTypedURL());
base::string16 term1(base::UTF8ToUTF16(kTestSearchTerm1));
base::string16 term2(base::UTF8ToUTF16(kTestSearchTerm2));
PopulateTestURLsAndSearchTerms(&row1, &row2, term1, term2);
// Notify the in-memory database that the second typed URL has been deleted.
SimulateNotificationURLsDeleted(&row2);
// Verify that the second term is no longer returned as result, and also check
// at the low level that it is gone for good. The term corresponding to the
// first URLRow should not be affected.
EXPECT_EQ(1u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term1));
EXPECT_EQ(0u, GetNumberOfMatchingSearchTerms(kTestKeywordId, term2));
EXPECT_TRUE(mem_backend_->db()->GetKeywordSearchTermRow(row1.id(), NULL));
EXPECT_FALSE(mem_backend_->db()->GetKeywordSearchTermRow(row2.id(), NULL));
}
} // namespace history
|