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
|
// 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 "components/favicon/core/favicon_handler.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/favicon/chrome_favicon_client_factory.h"
#include "chrome/browser/favicon/favicon_service_factory.h"
#include "chrome/browser/favicon/favicon_tab_helper.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
#include "components/favicon/core/favicon_service.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/favicon_size.h"
#include "ui/gfx/image/image.h"
class TestFaviconHandler;
using favicon::FaviconURL;
namespace {
// Fill the given bmp with valid png data.
void FillDataToBitmap(int w, int h, SkBitmap* bmp) {
bmp->allocN32Pixels(w, h);
unsigned char* src_data =
reinterpret_cast<unsigned char*>(bmp->getAddr32(0, 0));
for (int i = 0; i < w * h; i++) {
src_data[i * 4 + 0] = static_cast<unsigned char>(i % 255);
src_data[i * 4 + 1] = static_cast<unsigned char>(i % 255);
src_data[i * 4 + 2] = static_cast<unsigned char>(i % 255);
src_data[i * 4 + 3] = static_cast<unsigned char>(i % 255);
}
}
// Fill the given data buffer with valid png data.
void FillBitmap(int w, int h, std::vector<unsigned char>* output) {
SkBitmap bitmap;
FillDataToBitmap(w, h, &bitmap);
gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, output);
}
void SetFaviconRawBitmapResult(
const GURL& icon_url,
favicon_base::IconType icon_type,
bool expired,
std::vector<favicon_base::FaviconRawBitmapResult>* favicon_bitmap_results) {
scoped_refptr<base::RefCountedBytes> data(new base::RefCountedBytes());
FillBitmap(gfx::kFaviconSize, gfx::kFaviconSize, &data->data());
favicon_base::FaviconRawBitmapResult bitmap_result;
bitmap_result.expired = expired;
bitmap_result.bitmap_data = data;
// Use a pixel size other than (0,0) as (0,0) has a special meaning.
bitmap_result.pixel_size = gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize);
bitmap_result.icon_type = icon_type;
bitmap_result.icon_url = icon_url;
favicon_bitmap_results->push_back(bitmap_result);
}
void SetFaviconRawBitmapResult(
const GURL& icon_url,
std::vector<favicon_base::FaviconRawBitmapResult>* favicon_bitmap_results) {
SetFaviconRawBitmapResult(icon_url,
favicon_base::FAVICON,
false /* expired */,
favicon_bitmap_results);
}
// This class is used to save the download request for verifying with test case.
// It also will be used to invoke the onDidDownload callback.
class DownloadHandler {
public:
explicit DownloadHandler(TestFaviconHandler* favicon_helper)
: favicon_helper_(favicon_helper),
failed_(false) {
}
~DownloadHandler() {}
void Reset() {
download_.reset();
failed_ = false;
}
void AddDownload(
int download_id,
const GURL& image_url,
const std::vector<int>& image_sizes,
int max_image_size) {
download_.reset(new Download(
download_id, image_url, image_sizes, max_image_size, false));
}
void InvokeCallback();
void set_failed(bool failed) { failed_ = failed; }
bool HasDownload() const { return download_.get(); }
const GURL& GetImageUrl() const { return download_->image_url; }
void SetImageSizes(const std::vector<int>& sizes) {
download_->image_sizes = sizes; }
private:
struct Download {
Download(int id,
GURL url,
const std::vector<int>& sizes,
int max_size,
bool failed)
: download_id(id),
image_url(url),
image_sizes(sizes),
max_image_size(max_size) {}
~Download() {}
int download_id;
GURL image_url;
std::vector<int> image_sizes;
int max_image_size;
};
TestFaviconHandler* favicon_helper_;
scoped_ptr<Download> download_;
bool failed_;
DISALLOW_COPY_AND_ASSIGN(DownloadHandler);
};
// This class is used to save the history request for verifying with test case.
// It also will be used to simulate the history response.
class HistoryRequestHandler {
public:
HistoryRequestHandler(const GURL& page_url,
const GURL& icon_url,
int icon_type,
const favicon_base::FaviconResultsCallback& callback)
: page_url_(page_url),
icon_url_(icon_url),
icon_type_(icon_type),
callback_(callback) {
}
HistoryRequestHandler(const GURL& page_url,
const GURL& icon_url,
int icon_type,
const std::vector<unsigned char>& bitmap_data,
const gfx::Size& size)
: page_url_(page_url),
icon_url_(icon_url),
icon_type_(icon_type),
bitmap_data_(bitmap_data),
size_(size) {
}
~HistoryRequestHandler() {}
void InvokeCallback();
const GURL page_url_;
const GURL icon_url_;
const int icon_type_;
const std::vector<unsigned char> bitmap_data_;
const gfx::Size size_;
std::vector<favicon_base::FaviconRawBitmapResult> history_results_;
favicon_base::FaviconResultsCallback callback_;
private:
DISALLOW_COPY_AND_ASSIGN(HistoryRequestHandler);
};
} // namespace
class TestFaviconDriver : public favicon::FaviconDriver {
public:
TestFaviconDriver()
: favicon_validity_(false),
num_active_favicon_(0),
num_favicon_available_(0),
update_active_favicon_(false) {}
~TestFaviconDriver() override {}
// favicon::FaviconDriver implementation.
void FetchFavicon(const GURL& url) override {
ADD_FAILURE() << "TestFaviconDriver::FetchFavicon() "
<< "should never be called in tests.";
}
void SaveFavicon() override {
ADD_FAILURE() << "TestFaviconDriver::SaveFavicon() "
<< "should never be called in tests.";
}
gfx::Image GetFavicon() const override {
ADD_FAILURE() << "TestFaviconDriver::GetFavicon() "
<< "should never be called in tests.";
return gfx::Image();
}
bool FaviconIsValid() const override {
ADD_FAILURE() << "TestFaviconDriver::FaviconIsValid() "
<< "should never be called in tests.";
return false;
}
bool HasPendingTasksForTest() override {
ADD_FAILURE() << "TestFaviconDriver::HasPendingTasksForTest() "
<< "should never be called in tests.";
return false;
}
int StartDownload(const GURL& url, int max_bitmap_size) override {
ADD_FAILURE() << "TestFaviconDriver::StartDownload() "
<< "should never be called in tests.";
return -1;
}
bool IsOffTheRecord() override { return false; }
bool IsBookmarked(const GURL& url) override { return false; }
GURL GetActiveURL() override { return url_; }
base::string16 GetActiveTitle() override { return base::string16(); }
bool GetActiveFaviconValidity() override { return favicon_validity_; }
void SetActiveFaviconValidity(bool favicon_validity) override {
favicon_validity_ = favicon_validity;
}
GURL GetActiveFaviconURL() override { return favicon_url_; }
void SetActiveFaviconURL(const GURL& favicon_url) override {
favicon_url_ = favicon_url;
}
gfx::Image GetActiveFaviconImage() override { return image_; }
void SetActiveFaviconImage(const gfx::Image& image) override {
image_ = image;
}
void OnFaviconAvailable(const gfx::Image& image,
const GURL& icon_url,
bool update_active_favicon) override {
++num_favicon_available_;
available_image_ = image;
available_icon_url_ = icon_url;
update_active_favicon_ = update_active_favicon;
if (!update_active_favicon)
return;
++num_active_favicon_;
SetActiveFaviconURL(icon_url);
SetActiveFaviconValidity(true);
SetActiveFaviconImage(image);
}
size_t num_active_favicon() const { return num_active_favicon_; }
size_t num_favicon_available() const { return num_favicon_available_; }
void ResetNumActiveFavicon() { num_active_favicon_ = 0; }
void ResetNumFaviconAvailable() { num_favicon_available_ = 0; }
void SetActiveURL(GURL url) { url_ = url; }
const gfx::Image available_favicon() { return available_image_; }
const GURL available_icon_url() { return available_icon_url_; }
bool update_active_favicon() { return update_active_favicon_; }
private:
GURL favicon_url_;
GURL url_;
gfx::Image image_;
bool favicon_validity_;
// The number of times that NotifyFaviconAvailable() has been called with
// |is_active_favicon| is true.
size_t num_active_favicon_;
// The number of times that NotifyFaviconAvailable() has been called.
size_t num_favicon_available_;
gfx::Image available_image_;
GURL available_icon_url_;
bool update_active_favicon_;
DISALLOW_COPY_AND_ASSIGN(TestFaviconDriver);
};
// This class is used to catch the FaviconHandler's download and history
// request, and also provide the methods to access the FaviconHandler
// internals.
class TestFaviconHandler : public favicon::FaviconHandler {
public:
static int GetMaximalIconSize(favicon_base::IconType icon_type) {
return FaviconHandler::GetMaximalIconSize(icon_type);
}
TestFaviconHandler(const GURL& page_url,
TestFaviconDriver* driver,
Type type,
bool download_largest_icon)
: favicon::FaviconHandler(nullptr,
driver,
type,
download_largest_icon),
download_id_(0) {
driver->SetActiveURL(page_url);
download_handler_.reset(new DownloadHandler(this));
}
~TestFaviconHandler() override {}
HistoryRequestHandler* history_handler() {
return history_handler_.get();
}
// This method will take the ownership of the given handler.
void set_history_handler(HistoryRequestHandler* handler) {
history_handler_.reset(handler);
}
DownloadHandler* download_handler() {
return download_handler_.get();
}
// Methods to access favicon internals.
const std::vector<FaviconURL>& urls() {
return image_urls_;
}
FaviconURL* current_candidate() {
return favicon::FaviconHandler::current_candidate();
}
const FaviconCandidate& best_favicon_candidate() {
return best_favicon_candidate_;
}
protected:
void UpdateFaviconMappingAndFetch(
const GURL& page_url,
const GURL& icon_url,
favicon_base::IconType icon_type,
const favicon_base::FaviconResultsCallback& callback,
base::CancelableTaskTracker* tracker) override {
history_handler_.reset(new HistoryRequestHandler(page_url, icon_url,
icon_type, callback));
}
void GetFaviconFromFaviconService(
const GURL& icon_url,
favicon_base::IconType icon_type,
const favicon_base::FaviconResultsCallback& callback,
base::CancelableTaskTracker* tracker) override {
history_handler_.reset(new HistoryRequestHandler(GURL(), icon_url,
icon_type, callback));
}
void GetFaviconForURLFromFaviconService(
const GURL& page_url,
int icon_types,
const favicon_base::FaviconResultsCallback& callback,
base::CancelableTaskTracker* tracker) override {
history_handler_.reset(new HistoryRequestHandler(page_url, GURL(),
icon_types, callback));
}
int DownloadFavicon(const GURL& image_url, int max_bitmap_size) override {
download_id_++;
std::vector<int> sizes;
sizes.push_back(0);
download_handler_->AddDownload(
download_id_, image_url, sizes, max_bitmap_size);
return download_id_;
}
void SetHistoryFavicons(const GURL& page_url,
const GURL& icon_url,
favicon_base::IconType icon_type,
const gfx::Image& image) override {
scoped_refptr<base::RefCountedMemory> bytes = image.As1xPNGBytes();
std::vector<unsigned char> bitmap_data(bytes->front(),
bytes->front() + bytes->size());
history_handler_.reset(new HistoryRequestHandler(
page_url, icon_url, icon_type, bitmap_data, image.Size()));
}
bool ShouldSaveFavicon(const GURL& url) override { return true; }
GURL page_url_;
private:
// The unique id of a download request. It will be returned to a
// FaviconHandler.
int download_id_;
scoped_ptr<DownloadHandler> download_handler_;
scoped_ptr<HistoryRequestHandler> history_handler_;
DISALLOW_COPY_AND_ASSIGN(TestFaviconHandler);
};
namespace {
void HistoryRequestHandler::InvokeCallback() {
if (!callback_.is_null()) {
callback_.Run(history_results_);
}
}
void DownloadHandler::InvokeCallback() {
std::vector<gfx::Size> original_bitmap_sizes;
std::vector<SkBitmap> bitmaps;
if (!failed_) {
for (std::vector<int>::const_iterator i = download_->image_sizes.begin();
i != download_->image_sizes.end(); ++i) {
int original_size = (*i > 0) ? *i : gfx::kFaviconSize;
int downloaded_size = original_size;
if (download_->max_image_size != 0 &&
downloaded_size > download_->max_image_size) {
downloaded_size = download_->max_image_size;
}
SkBitmap bitmap;
FillDataToBitmap(downloaded_size, downloaded_size, &bitmap);
bitmaps.push_back(bitmap);
original_bitmap_sizes.push_back(gfx::Size(original_size, original_size));
}
}
favicon_helper_->OnDidDownloadFavicon(download_->download_id,
download_->image_url,
bitmaps,
original_bitmap_sizes);
}
class FaviconHandlerTest : public ChromeRenderViewHostTestHarness {
public:
FaviconHandlerTest() {
}
~FaviconHandlerTest() override {}
// Simulates requesting a favicon for |page_url| given:
// - We have not previously cached anything in history for |page_url| or for
// any of |candidates|.
// - The page provides favicons at |candidate_icons|.
// - The favicons at |candidate_icons| have edge pixel sizes of
// |candidate_icon_sizes|.
void DownloadTillDoneIgnoringHistory(
TestFaviconDriver* favicon_driver,
TestFaviconHandler* favicon_handler,
const GURL& page_url,
const std::vector<FaviconURL>& candidate_icons,
const int* candidate_icon_sizes) {
UpdateFaviconURL(
favicon_driver, favicon_handler, page_url, candidate_icons);
EXPECT_EQ(candidate_icons.size(), favicon_handler->image_urls().size());
DownloadHandler* download_handler = favicon_handler->download_handler();
for (size_t i = 0; i < candidate_icons.size(); ++i) {
favicon_handler->history_handler()->history_results_.clear();
favicon_handler->history_handler()->InvokeCallback();
ASSERT_TRUE(download_handler->HasDownload());
EXPECT_EQ(download_handler->GetImageUrl(),
candidate_icons[i].icon_url);
std::vector<int> sizes;
sizes.push_back(candidate_icon_sizes[i]);
download_handler->SetImageSizes(sizes);
download_handler->InvokeCallback();
if (favicon_driver->num_active_favicon())
return;
}
}
void UpdateFaviconURL(TestFaviconDriver* favicon_driver,
TestFaviconHandler* favicon_handler,
const GURL& page_url,
const std::vector<FaviconURL>& candidate_icons) {
favicon_driver->ResetNumActiveFavicon();
favicon_handler->FetchFavicon(page_url);
favicon_handler->history_handler()->InvokeCallback();
favicon_handler->OnUpdateFaviconURL(candidate_icons);
}
void SetUp() override {
// The score computed by SelectFaviconFrames() is dependent on the supported
// scale factors of the platform. It is used for determining the goodness of
// a downloaded bitmap in favicon::FaviconHandler::OnDidDownloadFavicon().
// Force the values of the scale factors so that the tests produce the same
// results on all platforms.
std::vector<ui::ScaleFactor> scale_factors;
scale_factors.push_back(ui::SCALE_FACTOR_100P);
scoped_set_supported_scale_factors_.reset(
new ui::test::ScopedSetSupportedScaleFactors(scale_factors));
ChromeRenderViewHostTestHarness::SetUp();
}
void TearDown() override {
Profile* profile = Profile::FromBrowserContext(
web_contents()->GetBrowserContext());
FaviconServiceFactory::GetInstance()->SetTestingFactory(profile, nullptr);
ChromeRenderViewHostTestHarness::TearDown();
}
private:
typedef scoped_ptr<ui::test::ScopedSetSupportedScaleFactors>
ScopedSetSupportedScaleFactors;
ScopedSetSupportedScaleFactors scoped_set_supported_scale_factors_;
DISALLOW_COPY_AND_ASSIGN(FaviconHandlerTest);
};
TEST_F(FaviconHandlerTest, GetFaviconFromHistory) {
const GURL page_url("http://www.google.com");
const GURL icon_url("http://www.google.com/favicon");
TestFaviconDriver driver;
TestFaviconHandler helper(page_url, &driver, favicon::FaviconHandler::FAVICON,
false);
helper.FetchFavicon(page_url);
HistoryRequestHandler* history_handler = helper.history_handler();
// Ensure the data given to history is correct.
ASSERT_TRUE(history_handler);
EXPECT_EQ(page_url, history_handler->page_url_);
EXPECT_EQ(GURL(), history_handler->icon_url_);
EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
SetFaviconRawBitmapResult(icon_url, &history_handler->history_results_);
// Send history response.
history_handler->InvokeCallback();
// Verify FaviconHandler status
EXPECT_TRUE(driver.GetActiveFaviconValidity());
EXPECT_EQ(icon_url, driver.GetActiveFaviconURL());
// Simulates update favicon url.
std::vector<FaviconURL> urls;
urls.push_back(
FaviconURL(icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
helper.OnUpdateFaviconURL(urls);
// Verify FaviconHandler status
EXPECT_EQ(1U, helper.urls().size());
ASSERT_TRUE(helper.current_candidate());
ASSERT_EQ(icon_url, helper.current_candidate()->icon_url);
ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type);
// Favicon shouldn't request to download icon.
EXPECT_FALSE(helper.download_handler()->HasDownload());
}
TEST_F(FaviconHandlerTest, DownloadFavicon) {
const GURL page_url("http://www.google.com");
const GURL icon_url("http://www.google.com/favicon");
TestFaviconDriver driver;
TestFaviconHandler helper(page_url, &driver, favicon::FaviconHandler::FAVICON,
false);
helper.FetchFavicon(page_url);
HistoryRequestHandler* history_handler = helper.history_handler();
// Ensure the data given to history is correct.
ASSERT_TRUE(history_handler);
EXPECT_EQ(page_url, history_handler->page_url_);
EXPECT_EQ(GURL(), history_handler->icon_url_);
EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
// Set icon data expired
SetFaviconRawBitmapResult(icon_url,
favicon_base::FAVICON,
true /* expired */,
&history_handler->history_results_);
// Send history response.
history_handler->InvokeCallback();
// Verify FaviconHandler status
EXPECT_TRUE(driver.GetActiveFaviconValidity());
EXPECT_EQ(icon_url, driver.GetActiveFaviconURL());
// Simulates update favicon url.
std::vector<FaviconURL> urls;
urls.push_back(
FaviconURL(icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
helper.OnUpdateFaviconURL(urls);
// Verify FaviconHandler status
EXPECT_EQ(1U, helper.urls().size());
ASSERT_TRUE(helper.current_candidate());
ASSERT_EQ(icon_url, helper.current_candidate()->icon_url);
ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type);
// Favicon should request to download icon now.
DownloadHandler* download_handler = helper.download_handler();
EXPECT_TRUE(helper.download_handler()->HasDownload());
// Verify the download request.
EXPECT_EQ(icon_url, download_handler->GetImageUrl());
// Reset the history_handler to verify whether favicon is set.
helper.set_history_handler(nullptr);
// Smulates download done.
download_handler->InvokeCallback();
// New icon should be saved to history backend and navigation entry.
history_handler = helper.history_handler();
ASSERT_TRUE(history_handler);
EXPECT_EQ(icon_url, history_handler->icon_url_);
EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
EXPECT_LT(0U, history_handler->bitmap_data_.size());
EXPECT_EQ(page_url, history_handler->page_url_);
// Verify NavigationEntry.
EXPECT_EQ(icon_url, driver.GetActiveFaviconURL());
EXPECT_TRUE(driver.GetActiveFaviconValidity());
EXPECT_FALSE(driver.GetActiveFaviconImage().IsEmpty());
EXPECT_EQ(gfx::kFaviconSize, driver.GetActiveFaviconImage().Width());
}
TEST_F(FaviconHandlerTest, UpdateAndDownloadFavicon) {
const GURL page_url("http://www.google.com");
const GURL icon_url("http://www.google.com/favicon");
const GURL new_icon_url("http://www.google.com/new_favicon");
TestFaviconDriver driver;
TestFaviconHandler helper(page_url, &driver, favicon::FaviconHandler::FAVICON,
false);
helper.FetchFavicon(page_url);
HistoryRequestHandler* history_handler = helper.history_handler();
// Ensure the data given to history is correct.
ASSERT_TRUE(history_handler);
EXPECT_EQ(page_url, history_handler->page_url_);
EXPECT_EQ(GURL(), history_handler->icon_url_);
EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
// Set valid icon data.
SetFaviconRawBitmapResult(icon_url, &history_handler->history_results_);
// Send history response.
history_handler->InvokeCallback();
// Verify FaviconHandler status.
EXPECT_TRUE(driver.GetActiveFaviconValidity());
EXPECT_EQ(icon_url, driver.GetActiveFaviconURL());
// Reset the history_handler to verify whether new icon is requested from
// history.
helper.set_history_handler(nullptr);
// Simulates update with the different favicon url.
std::vector<FaviconURL> urls;
urls.push_back(FaviconURL(
new_icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
helper.OnUpdateFaviconURL(urls);
// Verify FaviconHandler status.
EXPECT_EQ(1U, helper.urls().size());
ASSERT_TRUE(helper.current_candidate());
ASSERT_EQ(new_icon_url, helper.current_candidate()->icon_url);
ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type);
// Favicon should be requested from history.
history_handler = helper.history_handler();
ASSERT_TRUE(history_handler);
EXPECT_EQ(new_icon_url, history_handler->icon_url_);
EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
EXPECT_EQ(page_url, history_handler->page_url_);
// Simulate not find icon.
history_handler->history_results_.clear();
history_handler->InvokeCallback();
// Favicon should request to download icon now.
DownloadHandler* download_handler = helper.download_handler();
EXPECT_TRUE(helper.download_handler()->HasDownload());
// Verify the download request.
EXPECT_EQ(new_icon_url, download_handler->GetImageUrl());
// Reset the history_handler to verify whether favicon is set.
helper.set_history_handler(nullptr);
// Smulates download done.
download_handler->InvokeCallback();
// New icon should be saved to history backend and navigation entry.
history_handler = helper.history_handler();
ASSERT_TRUE(history_handler);
EXPECT_EQ(new_icon_url, history_handler->icon_url_);
EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
EXPECT_LT(0U, history_handler->bitmap_data_.size());
EXPECT_EQ(page_url, history_handler->page_url_);
// Verify NavigationEntry.
EXPECT_EQ(new_icon_url, driver.GetActiveFaviconURL());
EXPECT_TRUE(driver.GetActiveFaviconValidity());
EXPECT_FALSE(driver.GetActiveFaviconImage().IsEmpty());
EXPECT_EQ(gfx::kFaviconSize, driver.GetActiveFaviconImage().Width());
}
TEST_F(FaviconHandlerTest, FaviconInHistoryInvalid) {
const GURL page_url("http://www.google.com");
const GURL icon_url("http://www.google.com/favicon");
TestFaviconDriver driver;
TestFaviconHandler helper(page_url, &driver, favicon::FaviconHandler::FAVICON,
false);
helper.FetchFavicon(page_url);
HistoryRequestHandler* history_handler = helper.history_handler();
// Ensure the data given to history is correct.
ASSERT_TRUE(history_handler);
EXPECT_EQ(page_url, history_handler->page_url_);
EXPECT_EQ(GURL(), history_handler->icon_url_);
EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
// Set non empty but invalid data.
favicon_base::FaviconRawBitmapResult bitmap_result;
bitmap_result.expired = false;
// Empty bitmap data is invalid.
bitmap_result.bitmap_data = new base::RefCountedBytes();
bitmap_result.pixel_size = gfx::Size(gfx::kFaviconSize, gfx::kFaviconSize);
bitmap_result.icon_type = favicon_base::FAVICON;
bitmap_result.icon_url = icon_url;
history_handler->history_results_.clear();
history_handler->history_results_.push_back(bitmap_result);
// Send history response.
history_handler->InvokeCallback();
// The NavigationEntry should not be set yet as the history data is invalid.
EXPECT_FALSE(driver.GetActiveFaviconValidity());
EXPECT_EQ(GURL(), driver.GetActiveFaviconURL());
// Reset the history_handler to verify whether new icon is requested from
// history.
helper.set_history_handler(nullptr);
// Simulates update with matching favicon URL.
std::vector<FaviconURL> urls;
urls.push_back(
FaviconURL(icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
helper.OnUpdateFaviconURL(urls);
// A download for the favicon should be requested, and we should not do
// another history request.
DownloadHandler* download_handler = helper.download_handler();
EXPECT_TRUE(helper.download_handler()->HasDownload());
EXPECT_EQ(nullptr, helper.history_handler());
// Verify the download request.
EXPECT_EQ(icon_url, download_handler->GetImageUrl());
// Simulates download done.
download_handler->InvokeCallback();
// New icon should be saved to history backend and navigation entry.
history_handler = helper.history_handler();
ASSERT_TRUE(history_handler);
EXPECT_EQ(icon_url, history_handler->icon_url_);
EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
EXPECT_LT(0U, history_handler->bitmap_data_.size());
EXPECT_EQ(page_url, history_handler->page_url_);
// Verify NavigationEntry.
EXPECT_EQ(icon_url, driver.GetActiveFaviconURL());
EXPECT_TRUE(driver.GetActiveFaviconValidity());
EXPECT_FALSE(driver.GetActiveFaviconImage().IsEmpty());
EXPECT_EQ(gfx::kFaviconSize, driver.GetActiveFaviconImage().Width());
}
TEST_F(FaviconHandlerTest, UpdateFavicon) {
const GURL page_url("http://www.google.com");
const GURL icon_url("http://www.google.com/favicon");
const GURL new_icon_url("http://www.google.com/new_favicon");
TestFaviconDriver driver;
TestFaviconHandler helper(page_url, &driver, favicon::FaviconHandler::FAVICON,
false);
helper.FetchFavicon(page_url);
HistoryRequestHandler* history_handler = helper.history_handler();
// Ensure the data given to history is correct.
ASSERT_TRUE(history_handler);
EXPECT_EQ(page_url, history_handler->page_url_);
EXPECT_EQ(GURL(), history_handler->icon_url_);
EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
SetFaviconRawBitmapResult(icon_url, &history_handler->history_results_);
// Send history response.
history_handler->InvokeCallback();
// Verify FaviconHandler status.
EXPECT_TRUE(driver.GetActiveFaviconValidity());
EXPECT_EQ(icon_url, driver.GetActiveFaviconURL());
// Reset the history_handler to verify whether new icon is requested from
// history.
helper.set_history_handler(nullptr);
// Simulates update with the different favicon url.
std::vector<FaviconURL> urls;
urls.push_back(FaviconURL(
new_icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
helper.OnUpdateFaviconURL(urls);
// Verify FaviconHandler status.
EXPECT_EQ(1U, helper.urls().size());
ASSERT_TRUE(helper.current_candidate());
ASSERT_EQ(new_icon_url, helper.current_candidate()->icon_url);
ASSERT_EQ(favicon_base::FAVICON, helper.current_candidate()->icon_type);
// Favicon should be requested from history.
history_handler = helper.history_handler();
ASSERT_TRUE(history_handler);
EXPECT_EQ(new_icon_url, history_handler->icon_url_);
EXPECT_EQ(favicon_base::FAVICON, history_handler->icon_type_);
EXPECT_EQ(page_url, history_handler->page_url_);
// Simulate find icon.
SetFaviconRawBitmapResult(new_icon_url, &history_handler->history_results_);
history_handler->InvokeCallback();
// Shouldn't request download favicon
EXPECT_FALSE(helper.download_handler()->HasDownload());
// Verify the favicon status.
EXPECT_EQ(new_icon_url, driver.GetActiveFaviconURL());
EXPECT_TRUE(driver.GetActiveFaviconValidity());
EXPECT_FALSE(driver.GetActiveFaviconImage().IsEmpty());
}
TEST_F(FaviconHandlerTest, Download2ndFaviconURLCandidate) {
const GURL page_url("http://www.google.com");
const GURL icon_url("http://www.google.com/favicon");
const GURL new_icon_url("http://www.google.com/new_favicon");
TestFaviconDriver driver;
TestFaviconHandler helper(page_url, &driver, favicon::FaviconHandler::TOUCH,
false);
helper.FetchFavicon(page_url);
HistoryRequestHandler* history_handler = helper.history_handler();
// Ensure the data given to history is correct.
ASSERT_TRUE(history_handler);
EXPECT_EQ(page_url, history_handler->page_url_);
EXPECT_EQ(GURL(), history_handler->icon_url_);
EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON | favicon_base::TOUCH_ICON,
history_handler->icon_type_);
// Icon not found.
history_handler->history_results_.clear();
// Send history response.
history_handler->InvokeCallback();
// Verify FaviconHandler status.
EXPECT_FALSE(driver.GetActiveFaviconValidity());
EXPECT_EQ(GURL(), driver.GetActiveFaviconURL());
// Reset the history_handler to verify whether new icon is requested from
// history.
helper.set_history_handler(nullptr);
// Simulates update with the different favicon url.
std::vector<FaviconURL> urls;
urls.push_back(FaviconURL(icon_url,
favicon_base::TOUCH_PRECOMPOSED_ICON,
std::vector<gfx::Size>()));
urls.push_back(FaviconURL(
new_icon_url, favicon_base::TOUCH_ICON, std::vector<gfx::Size>()));
urls.push_back(FaviconURL(
new_icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
helper.OnUpdateFaviconURL(urls);
// Verify FaviconHandler status.
EXPECT_EQ(2U, helper.urls().size());
ASSERT_TRUE(helper.current_candidate());
ASSERT_EQ(icon_url, helper.current_candidate()->icon_url);
ASSERT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON,
helper.current_candidate()->icon_type);
// Favicon should be requested from history.
history_handler = helper.history_handler();
ASSERT_TRUE(history_handler);
EXPECT_EQ(icon_url, history_handler->icon_url_);
EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON, history_handler->icon_type_);
EXPECT_EQ(page_url, history_handler->page_url_);
// Simulate not find icon.
history_handler->history_results_.clear();
history_handler->InvokeCallback();
// Should request download favicon.
DownloadHandler* download_handler = helper.download_handler();
EXPECT_TRUE(helper.download_handler()->HasDownload());
// Verify the download request.
EXPECT_EQ(icon_url, download_handler->GetImageUrl());
// Reset the history_handler to verify whether favicon is request from
// history.
helper.set_history_handler(nullptr);
// Smulates download failed.
download_handler->set_failed(true);
download_handler->InvokeCallback();
// Left 1 url.
EXPECT_EQ(1U, helper.urls().size());
ASSERT_TRUE(helper.current_candidate());
EXPECT_EQ(new_icon_url, helper.current_candidate()->icon_url);
EXPECT_EQ(favicon_base::TOUCH_ICON, helper.current_candidate()->icon_type);
// Favicon should be requested from history.
history_handler = helper.history_handler();
ASSERT_TRUE(history_handler);
EXPECT_EQ(new_icon_url, history_handler->icon_url_);
EXPECT_EQ(favicon_base::TOUCH_ICON, history_handler->icon_type_);
EXPECT_EQ(page_url, history_handler->page_url_);
// Reset download handler
download_handler->Reset();
// Simulates getting a expired icon from history.
SetFaviconRawBitmapResult(new_icon_url,
favicon_base::TOUCH_ICON,
true /* expired */,
&history_handler->history_results_);
history_handler->InvokeCallback();
// Verify the download request.
EXPECT_TRUE(helper.download_handler()->HasDownload());
EXPECT_EQ(new_icon_url, download_handler->GetImageUrl());
helper.set_history_handler(nullptr);
// Simulates icon being downloaded.
download_handler->InvokeCallback();
// New icon should be saved to history backend.
history_handler = helper.history_handler();
ASSERT_TRUE(history_handler);
EXPECT_EQ(new_icon_url, history_handler->icon_url_);
EXPECT_EQ(favicon_base::TOUCH_ICON, history_handler->icon_type_);
EXPECT_LT(0U, history_handler->bitmap_data_.size());
EXPECT_EQ(page_url, history_handler->page_url_);
}
TEST_F(FaviconHandlerTest, UpdateDuringDownloading) {
const GURL page_url("http://www.google.com");
const GURL icon_url("http://www.google.com/favicon");
const GURL new_icon_url("http://www.google.com/new_favicon");
TestFaviconDriver driver;
TestFaviconHandler helper(page_url, &driver, favicon::FaviconHandler::TOUCH,
false);
helper.FetchFavicon(page_url);
HistoryRequestHandler* history_handler = helper.history_handler();
// Ensure the data given to history is correct.
ASSERT_TRUE(history_handler);
EXPECT_EQ(page_url, history_handler->page_url_);
EXPECT_EQ(GURL(), history_handler->icon_url_);
EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON | favicon_base::TOUCH_ICON,
history_handler->icon_type_);
// Icon not found.
history_handler->history_results_.clear();
// Send history response.
history_handler->InvokeCallback();
// Verify FaviconHandler status.
EXPECT_FALSE(driver.GetActiveFaviconValidity());
EXPECT_EQ(GURL(), driver.GetActiveFaviconURL());
// Reset the history_handler to verify whether new icon is requested from
// history.
helper.set_history_handler(nullptr);
// Simulates update with the different favicon url.
std::vector<FaviconURL> urls;
urls.push_back(FaviconURL(icon_url,
favicon_base::TOUCH_PRECOMPOSED_ICON,
std::vector<gfx::Size>()));
urls.push_back(FaviconURL(
new_icon_url, favicon_base::TOUCH_ICON, std::vector<gfx::Size>()));
urls.push_back(FaviconURL(
new_icon_url, favicon_base::FAVICON, std::vector<gfx::Size>()));
helper.OnUpdateFaviconURL(urls);
// Verify FaviconHandler status.
EXPECT_EQ(2U, helper.urls().size());
ASSERT_TRUE(helper.current_candidate());
ASSERT_EQ(icon_url, helper.current_candidate()->icon_url);
ASSERT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON,
helper.current_candidate()->icon_type);
// Favicon should be requested from history.
history_handler = helper.history_handler();
ASSERT_TRUE(history_handler);
EXPECT_EQ(icon_url, history_handler->icon_url_);
EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON, history_handler->icon_type_);
EXPECT_EQ(page_url, history_handler->page_url_);
// Simulate not find icon.
history_handler->history_results_.clear();
history_handler->InvokeCallback();
// Should request download favicon.
DownloadHandler* download_handler = helper.download_handler();
EXPECT_TRUE(helper.download_handler()->HasDownload());
// Verify the download request.
EXPECT_EQ(icon_url, download_handler->GetImageUrl());
// Reset the history_handler to verify whether favicon is request from
// history.
helper.set_history_handler(nullptr);
const GURL latest_icon_url("http://www.google.com/latest_favicon");
std::vector<FaviconURL> latest_urls;
latest_urls.push_back(FaviconURL(
latest_icon_url, favicon_base::TOUCH_ICON, std::vector<gfx::Size>()));
helper.OnUpdateFaviconURL(latest_urls);
EXPECT_EQ(1U, helper.urls().size());
EXPECT_EQ(latest_icon_url, helper.current_candidate()->icon_url);
EXPECT_EQ(favicon_base::TOUCH_ICON, helper.current_candidate()->icon_type);
// Whether new icon is requested from history
history_handler = helper.history_handler();
ASSERT_TRUE(history_handler);
EXPECT_EQ(latest_icon_url, history_handler->icon_url_);
EXPECT_EQ(favicon_base::TOUCH_ICON, history_handler->icon_type_);
EXPECT_EQ(page_url, history_handler->page_url_);
// Reset the history_handler to verify whether favicon is request from
// history.
// Save the callback for late use.
favicon_base::FaviconResultsCallback callback = history_handler->callback_;
helper.set_history_handler(nullptr);
// Simulates download succeed.
download_handler->InvokeCallback();
// The downloaded icon should be thrown away as there is favicon update.
EXPECT_FALSE(helper.history_handler());
download_handler->Reset();
// Simulates getting the icon from history.
scoped_ptr<HistoryRequestHandler> handler;
handler.reset(new HistoryRequestHandler(
page_url, latest_icon_url, favicon_base::TOUCH_ICON, callback));
SetFaviconRawBitmapResult(latest_icon_url,
favicon_base::TOUCH_ICON,
false /* expired */,
&handler->history_results_);
handler->InvokeCallback();
// No download request.
EXPECT_FALSE(download_handler->HasDownload());
}
#if !defined(OS_ANDROID)
// Test the favicon which is selected when the web page provides several
// favicons and none of the favicons are cached in history.
// The goal of this test is to be more of an integration test than
// SelectFaviconFramesTest.*.
TEST_F(FaviconHandlerTest, MultipleFavicons) {
const GURL kPageURL("http://www.google.com");
const FaviconURL kSourceIconURLs[] = {
FaviconURL(GURL("http://www.google.com/a"),
favicon_base::FAVICON,
std::vector<gfx::Size>()),
FaviconURL(GURL("http://www.google.com/b"),
favicon_base::FAVICON,
std::vector<gfx::Size>()),
FaviconURL(GURL("http://www.google.com/c"),
favicon_base::FAVICON,
std::vector<gfx::Size>()),
FaviconURL(GURL("http://www.google.com/d"),
favicon_base::FAVICON,
std::vector<gfx::Size>()),
FaviconURL(GURL("http://www.google.com/e"),
favicon_base::FAVICON,
std::vector<gfx::Size>())};
// Set the supported scale factors to 1x and 2x. This affects the behavior of
// SelectFaviconFrames().
std::vector<ui::ScaleFactor> scale_factors;
scale_factors.push_back(ui::SCALE_FACTOR_100P);
scale_factors.push_back(ui::SCALE_FACTOR_200P);
ui::test::ScopedSetSupportedScaleFactors scoped_supported(scale_factors);
// 1) Test that if there are several single resolution favicons to choose from
// that the largest exact match is chosen.
TestFaviconDriver driver1;
TestFaviconHandler handler1(kPageURL, &driver1,
favicon::FaviconHandler::FAVICON, false);
const int kSizes1[] = { 16, 24, 32, 48, 256 };
std::vector<FaviconURL> urls1(kSourceIconURLs,
kSourceIconURLs + arraysize(kSizes1));
DownloadTillDoneIgnoringHistory(
&driver1, &handler1, kPageURL, urls1, kSizes1);
EXPECT_EQ(0u, handler1.image_urls().size());
EXPECT_TRUE(driver1.GetActiveFaviconValidity());
EXPECT_FALSE(driver1.GetActiveFaviconImage().IsEmpty());
EXPECT_EQ(gfx::kFaviconSize, driver1.GetActiveFaviconImage().Width());
size_t expected_index = 2u;
EXPECT_EQ(32, kSizes1[expected_index]);
EXPECT_EQ(kSourceIconURLs[expected_index].icon_url,
driver1.GetActiveFaviconURL());
// 2) Test that if there are several single resolution favicons to choose
// from, the exact match is preferred even if it results in upsampling.
TestFaviconDriver driver2;
TestFaviconHandler handler2(kPageURL, &driver2,
favicon::FaviconHandler::FAVICON, false);
const int kSizes2[] = { 16, 24, 48, 256 };
std::vector<FaviconURL> urls2(kSourceIconURLs,
kSourceIconURLs + arraysize(kSizes2));
DownloadTillDoneIgnoringHistory(
&driver2, &handler2, kPageURL, urls2, kSizes2);
EXPECT_TRUE(driver2.GetActiveFaviconValidity());
expected_index = 0u;
EXPECT_EQ(16, kSizes2[expected_index]);
EXPECT_EQ(kSourceIconURLs[expected_index].icon_url,
driver2.GetActiveFaviconURL());
// 3) Test that favicons which need to be upsampled a little or downsampled
// a little are preferred over huge favicons.
TestFaviconDriver driver3;
TestFaviconHandler handler3(kPageURL, &driver3,
favicon::FaviconHandler::FAVICON, false);
const int kSizes3[] = { 256, 48 };
std::vector<FaviconURL> urls3(kSourceIconURLs,
kSourceIconURLs + arraysize(kSizes3));
DownloadTillDoneIgnoringHistory(
&driver3, &handler3, kPageURL, urls3, kSizes3);
EXPECT_TRUE(driver3.GetActiveFaviconValidity());
expected_index = 1u;
EXPECT_EQ(48, kSizes3[expected_index]);
EXPECT_EQ(kSourceIconURLs[expected_index].icon_url,
driver3.GetActiveFaviconURL());
TestFaviconDriver driver4;
TestFaviconHandler handler4(kPageURL, &driver4,
favicon::FaviconHandler::FAVICON, false);
const int kSizes4[] = { 17, 256 };
std::vector<FaviconURL> urls4(kSourceIconURLs,
kSourceIconURLs + arraysize(kSizes4));
DownloadTillDoneIgnoringHistory(
&driver4, &handler4, kPageURL, urls4, kSizes4);
EXPECT_TRUE(driver4.GetActiveFaviconValidity());
expected_index = 0u;
EXPECT_EQ(17, kSizes4[expected_index]);
EXPECT_EQ(kSourceIconURLs[expected_index].icon_url,
driver4.GetActiveFaviconURL());
}
#endif
TEST_F(FaviconHandlerTest, TestSortFavicon) {
const GURL kPageURL("http://www.google.com");
std::vector<gfx::Size> icon1;
icon1.push_back(gfx::Size(1024, 1024));
icon1.push_back(gfx::Size(512, 512));
std::vector<gfx::Size> icon2;
icon2.push_back(gfx::Size(15, 15));
icon2.push_back(gfx::Size(16, 16));
std::vector<gfx::Size> icon3;
icon3.push_back(gfx::Size(16, 16));
icon3.push_back(gfx::Size(14, 14));
const FaviconURL kSourceIconURLs[] = {
FaviconURL(GURL("http://www.google.com/a"), favicon_base::FAVICON, icon1),
FaviconURL(GURL("http://www.google.com/b"), favicon_base::FAVICON, icon2),
FaviconURL(GURL("http://www.google.com/c"), favicon_base::FAVICON, icon3),
FaviconURL(GURL("http://www.google.com/d"),
favicon_base::FAVICON,
std::vector<gfx::Size>()),
FaviconURL(GURL("http://www.google.com/e"),
favicon_base::FAVICON,
std::vector<gfx::Size>())};
TestFaviconDriver driver1;
TestFaviconHandler handler1(kPageURL, &driver1,
favicon::FaviconHandler::FAVICON, true);
std::vector<FaviconURL> urls1(kSourceIconURLs,
kSourceIconURLs + arraysize(kSourceIconURLs));
UpdateFaviconURL(&driver1, &handler1, kPageURL, urls1);
struct ExpectedResult {
// The favicon's index in kSourceIconURLs.
size_t favicon_index;
// Width of largest bitmap.
int width;
} results[] = {
// First is icon1, though its size larger than maximal.
{0, 1024},
// Second is icon2
// The 16x16 is largest.
{1, 16},
// Third is icon3 though it has same size as icon2.
// The 16x16 is largest.
{2, 16},
// The rest of bitmaps come in order, there is no sizes attribute.
{3, -1},
{4, -1},
};
const std::vector<FaviconURL>& icons = handler1.image_urls();
ASSERT_EQ(5u, icons.size());
for (size_t i = 0; i < icons.size(); ++i) {
EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url,
icons[i].icon_url);
if (results[i].width != -1)
EXPECT_EQ(results[i].width, icons[i].icon_sizes[0].width());
}
}
TEST_F(FaviconHandlerTest, TestDownloadLargestFavicon) {
const GURL kPageURL("http://www.google.com");
std::vector<gfx::Size> icon1;
icon1.push_back(gfx::Size(1024, 1024));
icon1.push_back(gfx::Size(512, 512));
std::vector<gfx::Size> icon2;
icon2.push_back(gfx::Size(15, 15));
icon2.push_back(gfx::Size(14, 14));
std::vector<gfx::Size> icon3;
icon3.push_back(gfx::Size(16, 16));
icon3.push_back(gfx::Size(512, 512));
const FaviconURL kSourceIconURLs[] = {
FaviconURL(
GURL("http://www.google.com/a"), favicon_base::FAVICON, icon1),
FaviconURL(
GURL("http://www.google.com/b"), favicon_base::FAVICON, icon2),
FaviconURL(
GURL("http://www.google.com/c"), favicon_base::FAVICON, icon3),
FaviconURL(GURL("http://www.google.com/d"),
favicon_base::FAVICON,
std::vector<gfx::Size>()),
FaviconURL(GURL("http://www.google.com/e"),
favicon_base::FAVICON,
std::vector<gfx::Size>())};
TestFaviconDriver driver1;
TestFaviconHandler handler1(kPageURL, &driver1,
favicon::FaviconHandler::FAVICON, true);
std::vector<FaviconURL> urls1(kSourceIconURLs,
kSourceIconURLs + arraysize(kSourceIconURLs));
UpdateFaviconURL(&driver1, &handler1, kPageURL, urls1);
// Simulate the download failed, to check whether the icons were requested
// to download according their size.
struct ExpectedResult {
// The size of image_urls_.
size_t image_urls_size;
// The favicon's index in kSourceIconURLs.
size_t favicon_index;
// Width of largest bitmap.
int width;
} results[] = {
{5, 0, 1024},
{4, 2, 512},
{3, 1, 15},
// The rest of bitmaps come in order.
{2, 3, -1},
{1, 4, -1},
};
for (int i = 0; i < 5; ++i) {
ASSERT_EQ(results[i].image_urls_size, handler1.image_urls().size());
EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url,
handler1.current_candidate()->icon_url);
if (results[i].width != -1) {
EXPECT_EQ(results[i].width, handler1.current_candidate()->
icon_sizes[0].width());
}
// Simulate no favicon from history.
handler1.history_handler()->history_results_.clear();
handler1.history_handler()->InvokeCallback();
// Verify download request
ASSERT_TRUE(handler1.download_handler()->HasDownload());
EXPECT_EQ(kSourceIconURLs[results[i].favicon_index].icon_url,
handler1.download_handler()->GetImageUrl());
// Simulate the download failed.
handler1.download_handler()->set_failed(true);
handler1.download_handler()->InvokeCallback();
}
}
TEST_F(FaviconHandlerTest, TestSelectLargestFavicon) {
const GURL kPageURL("http://www.google.com");
std::vector<gfx::Size> one_icon;
one_icon.push_back(gfx::Size(15, 15));
std::vector<gfx::Size> two_icons;
two_icons.push_back(gfx::Size(14, 14));
two_icons.push_back(gfx::Size(16, 16));
const FaviconURL kSourceIconURLs[] = {
FaviconURL(
GURL("http://www.google.com/b"), favicon_base::FAVICON, one_icon),
FaviconURL(
GURL("http://www.google.com/c"), favicon_base::FAVICON, two_icons)};
TestFaviconDriver driver1;
TestFaviconHandler handler1(kPageURL, &driver1,
favicon::FaviconHandler::FAVICON, true);
std::vector<FaviconURL> urls1(kSourceIconURLs,
kSourceIconURLs + arraysize(kSourceIconURLs));
UpdateFaviconURL(&driver1, &handler1, kPageURL, urls1);
ASSERT_EQ(2u, handler1.urls().size());
// Index of largest favicon in kSourceIconURLs.
size_t i = 1;
// The largest bitmap's index in Favicon .
int b = 1;
// Verify the icon_bitmaps_ was initialized correctly.
EXPECT_EQ(kSourceIconURLs[i].icon_url,
handler1.current_candidate()->icon_url);
EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b],
handler1.current_candidate()->icon_sizes[0]);
// Simulate no favicon from history.
handler1.history_handler()->history_results_.clear();
handler1.history_handler()->InvokeCallback();
// Verify download request
ASSERT_TRUE(handler1.download_handler()->HasDownload());
EXPECT_EQ(kSourceIconURLs[i].icon_url,
handler1.download_handler()->GetImageUrl());
// Give the correct download result.
std::vector<int> sizes;
for (std::vector<gfx::Size>::const_iterator j =
kSourceIconURLs[i].icon_sizes.begin();
j != kSourceIconURLs[i].icon_sizes.end(); ++j)
sizes.push_back(j->width());
handler1.download_handler()->SetImageSizes(sizes);
handler1.download_handler()->InvokeCallback();
// Verify the largest bitmap has been saved into history.
EXPECT_EQ(kSourceIconURLs[i].icon_url, handler1.history_handler()->icon_url_);
EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b],
handler1.history_handler()->size_);
// Verify NotifyFaviconAvailable().
EXPECT_FALSE(driver1.update_active_favicon());
EXPECT_EQ(kSourceIconURLs[i].icon_url, driver1.available_icon_url());
EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b],
driver1.available_favicon().Size());
}
TEST_F(FaviconHandlerTest, TestFaviconWasScaledAfterDownload) {
const GURL kPageURL("http://www.google.com");
const int kMaximalSize =
TestFaviconHandler::GetMaximalIconSize(favicon_base::FAVICON);
std::vector<gfx::Size> icon1;
icon1.push_back(gfx::Size(kMaximalSize + 1, kMaximalSize + 1));
std::vector<gfx::Size> icon2;
icon2.push_back(gfx::Size(kMaximalSize + 2, kMaximalSize + 2));
const FaviconURL kSourceIconURLs[] = {
FaviconURL(
GURL("http://www.google.com/b"), favicon_base::FAVICON, icon1),
FaviconURL(
GURL("http://www.google.com/c"), favicon_base::FAVICON, icon2)};
TestFaviconDriver driver1;
TestFaviconHandler handler1(kPageURL, &driver1,
favicon::FaviconHandler::FAVICON, true);
std::vector<FaviconURL> urls1(kSourceIconURLs,
kSourceIconURLs + arraysize(kSourceIconURLs));
UpdateFaviconURL(&driver1, &handler1, kPageURL, urls1);
ASSERT_EQ(2u, handler1.urls().size());
// Index of largest favicon in kSourceIconURLs.
size_t i = 1;
// The largest bitmap's index in Favicon .
int b = 0;
// Verify the icon_bitmaps_ was initialized correctly.
EXPECT_EQ(kSourceIconURLs[i].icon_url,
handler1.current_candidate()->icon_url);
EXPECT_EQ(kSourceIconURLs[i].icon_sizes[b],
handler1.current_candidate()->icon_sizes[0]);
// Simulate no favicon from history.
handler1.history_handler()->history_results_.clear();
handler1.history_handler()->InvokeCallback();
// Verify download request
ASSERT_TRUE(handler1.download_handler()->HasDownload());
EXPECT_EQ(kSourceIconURLs[i].icon_url,
handler1.download_handler()->GetImageUrl());
// Give the scaled download bitmap.
std::vector<int> sizes;
sizes.push_back(kMaximalSize);
handler1.download_handler()->SetImageSizes(sizes);
handler1.download_handler()->InvokeCallback();
// Verify the largest bitmap has been saved into history though it was
// scaled down to maximal size and smaller than icon1 now.
EXPECT_EQ(kSourceIconURLs[i].icon_url, handler1.history_handler()->icon_url_);
EXPECT_EQ(gfx::Size(kMaximalSize, kMaximalSize),
handler1.history_handler()->size_);
}
TEST_F(FaviconHandlerTest, TestKeepDownloadedLargestFavicon) {
const GURL kPageURL("http://www.google.com");
std::vector<gfx::Size> icon1;
icon1.push_back(gfx::Size(16, 16));
const int actual_size1 = 10;
std::vector<gfx::Size> icon2;
icon2.push_back(gfx::Size(15, 15));
const int actual_size2 = 12;
const FaviconURL kSourceIconURLs[] = {
FaviconURL(GURL("http://www.google.com/b"), favicon_base::FAVICON, icon1),
FaviconURL(GURL("http://www.google.com/c"), favicon_base::FAVICON, icon2),
FaviconURL(GURL("http://www.google.com/d"),
favicon_base::FAVICON,
std::vector<gfx::Size>())};
TestFaviconDriver driver1;
TestFaviconHandler handler1(kPageURL, &driver1,
favicon::FaviconHandler::FAVICON, true);
std::vector<FaviconURL> urls1(kSourceIconURLs,
kSourceIconURLs + arraysize(kSourceIconURLs));
UpdateFaviconURL(&driver1, &handler1, kPageURL, urls1);
ASSERT_EQ(3u, handler1.urls().size());
// Simulate no favicon from history.
handler1.history_handler()->history_results_.clear();
handler1.history_handler()->InvokeCallback();
// Verify the first icon was request to download
ASSERT_TRUE(handler1.download_handler()->HasDownload());
EXPECT_EQ(kSourceIconURLs[0].icon_url,
handler1.download_handler()->GetImageUrl());
// Give the incorrect size.
std::vector<int> sizes;
sizes.push_back(actual_size1);
handler1.download_handler()->SetImageSizes(sizes);
handler1.download_handler()->InvokeCallback();
// Simulate no favicon from history.
handler1.history_handler()->history_results_.clear();
handler1.history_handler()->InvokeCallback();
// Verify the 2nd icon was request to download
ASSERT_TRUE(handler1.download_handler()->HasDownload());
EXPECT_EQ(kSourceIconURLs[1].icon_url,
handler1.download_handler()->GetImageUrl());
// Very the best candidate is icon1
EXPECT_EQ(kSourceIconURLs[0].icon_url,
handler1.best_favicon_candidate().image_url);
EXPECT_EQ(gfx::Size(actual_size1, actual_size1),
handler1.best_favicon_candidate().image.Size());
// Give the incorrect size.
sizes.clear();
sizes.push_back(actual_size2);
handler1.download_handler()->SetImageSizes(sizes);
handler1.download_handler()->InvokeCallback();
// Verify icon2 has been saved into history.
EXPECT_EQ(kSourceIconURLs[1].icon_url, handler1.history_handler()->icon_url_);
EXPECT_EQ(gfx::Size(actual_size2, actual_size2),
handler1.history_handler()->size_);
}
static KeyedService* BuildFaviconService(content::BrowserContext* context) {
Profile* profile = Profile::FromBrowserContext(context);
return new favicon::FaviconService(
ChromeFaviconClientFactory::GetForProfile(profile),
HistoryServiceFactory::GetForProfile(profile,
ServiceAccessType::EXPLICIT_ACCESS));
}
static KeyedService* BuildHistoryService(content::BrowserContext* context) {
return nullptr;
}
// Test that Favicon is not requested repeatedly during the same session if
// server returns HTTP 404 status.
TEST_F(FaviconHandlerTest, UnableToDownloadFavicon) {
const GURL missing_icon_url("http://www.google.com/favicon.ico");
const GURL another_icon_url("http://www.youtube.com/favicon.ico");
Profile* profile = Profile::FromBrowserContext(
web_contents()->GetBrowserContext());
FaviconServiceFactory::GetInstance()->SetTestingFactory(
profile, BuildFaviconService);
HistoryServiceFactory::GetInstance()->SetTestingFactory(
profile, BuildHistoryService);
favicon::FaviconService* favicon_service =
FaviconServiceFactory::GetForProfile(profile,
ServiceAccessType::IMPLICIT_ACCESS);
FaviconTabHelper::CreateForWebContents(web_contents());
FaviconTabHelper* favicon_tab_helper =
FaviconTabHelper::FromWebContents(web_contents());
std::vector<SkBitmap> empty_icons;
std::vector<gfx::Size> empty_icon_sizes;
int download_id = 0;
// Try to download missing icon.
download_id = favicon_tab_helper->StartDownload(missing_icon_url, 0);
EXPECT_NE(0, download_id);
EXPECT_FALSE(favicon_service->WasUnableToDownloadFavicon(missing_icon_url));
// Report download failure with HTTP 503 status.
favicon_tab_helper->DidDownloadFavicon(download_id, 503, missing_icon_url,
empty_icons, empty_icon_sizes);
// Icon is not marked as UnableToDownload as HTTP status is not 404.
EXPECT_FALSE(favicon_service->WasUnableToDownloadFavicon(missing_icon_url));
// Try to download again.
download_id = favicon_tab_helper->StartDownload(missing_icon_url, 0);
EXPECT_NE(0, download_id);
EXPECT_FALSE(favicon_service->WasUnableToDownloadFavicon(missing_icon_url));
// Report download failure with HTTP 404 status.
favicon_tab_helper->DidDownloadFavicon(download_id, 404, missing_icon_url,
empty_icons, empty_icon_sizes);
// Icon is marked as UnableToDownload.
EXPECT_TRUE(favicon_service->WasUnableToDownloadFavicon(missing_icon_url));
// Try to download again.
download_id = favicon_tab_helper->StartDownload(missing_icon_url, 0);
// Download is not started and Icon is still marked as UnableToDownload.
EXPECT_EQ(0, download_id);
EXPECT_TRUE(favicon_service->WasUnableToDownloadFavicon(missing_icon_url));
// Try to download another icon.
download_id = favicon_tab_helper->StartDownload(another_icon_url, 0);
// Download is started as another icon URL is not same as missing_icon_url.
EXPECT_NE(0, download_id);
EXPECT_FALSE(favicon_service->WasUnableToDownloadFavicon(another_icon_url));
// Clear the list of missing icons.
favicon_service->ClearUnableToDownloadFavicons();
EXPECT_FALSE(favicon_service->WasUnableToDownloadFavicon(missing_icon_url));
EXPECT_FALSE(favicon_service->WasUnableToDownloadFavicon(another_icon_url));
// Try to download again.
download_id = favicon_tab_helper->StartDownload(missing_icon_url, 0);
EXPECT_NE(0, download_id);
// Report download success with HTTP 200 status.
favicon_tab_helper->DidDownloadFavicon(download_id, 200, missing_icon_url,
empty_icons, empty_icon_sizes);
// Icon is not marked as UnableToDownload as HTTP status is not 404.
EXPECT_FALSE(favicon_service->WasUnableToDownloadFavicon(missing_icon_url));
}
class FaviconHandlerActiveFaviconValidityParamTest :
public FaviconHandlerTest,
public ::testing::WithParamInterface<bool> {
public:
FaviconHandlerActiveFaviconValidityParamTest() {}
~FaviconHandlerActiveFaviconValidityParamTest() override {}
bool GetActiveFaviconValiditySetting() {
return GetParam();
}
private:
DISALLOW_COPY_AND_ASSIGN(FaviconHandlerActiveFaviconValidityParamTest);
};
TEST_P(FaviconHandlerActiveFaviconValidityParamTest,
TestDownloadLargestIconDoesNotImpactActiveFaviconValidity) {
const GURL page_url("http://www.google.com");
std::vector<gfx::Size> one_icon;
one_icon.push_back(gfx::Size(15, 15));
const GURL old_favicon_url("http://www.google.com/old");
const GURL new_favicon_url("http://www.google.com/b");
const FaviconURL source_icon_urls[] = {
FaviconURL(new_favicon_url, favicon_base::FAVICON, one_icon)};
TestFaviconDriver driver1;
TestFaviconHandler handler1(page_url, &driver1,
favicon::FaviconHandler::FAVICON, true);
std::vector<FaviconURL> urls1(source_icon_urls,
source_icon_urls + arraysize(source_icon_urls));
UpdateFaviconURL(&driver1, &handler1, page_url, urls1);
HistoryRequestHandler* history_handler = handler1.history_handler();
// Simulate the active favicon is updated, this shouldn't happen in real
// use case, but we want to verify the behavior below is not impacted by
// accident.
driver1.SetActiveFaviconValidity(GetActiveFaviconValiditySetting());
// Simulate the get favicon from history, but favicon URL didn't match.
SetFaviconRawBitmapResult(old_favicon_url,
&history_handler->history_results_);
history_handler->InvokeCallback();
// Since we downloaded largest icon, and don't want to set active favicon
// NotifyFaviconAvaliable() should be called with is_active_favicon as false.
EXPECT_EQ(old_favicon_url, driver1.available_icon_url());
EXPECT_FALSE(driver1.update_active_favicon());
EXPECT_EQ(1u, driver1.num_favicon_available());
// We are trying to get favicon from history again.
history_handler = handler1.history_handler();
EXPECT_EQ(new_favicon_url, history_handler->icon_url_);
// Simulate the get expired favicon from history.
history_handler->history_results_.clear();
SetFaviconRawBitmapResult(new_favicon_url, favicon_base::FAVICON, true,
&history_handler->history_results_);
history_handler->InvokeCallback();
// Since we downloaded largest icon, and don't want to set active favicon
// NotifyFaviconAvaliable() should be called with is_active_favicon as false.
EXPECT_EQ(new_favicon_url, driver1.available_icon_url());
EXPECT_FALSE(driver1.update_active_favicon());
EXPECT_EQ(2u, driver1.num_favicon_available());
// We are trying to download favicon.
DownloadHandler* download_handler = handler1.download_handler();
EXPECT_TRUE(download_handler->HasDownload());
EXPECT_EQ(new_favicon_url, download_handler->GetImageUrl());
// Simulate the download succeed.
download_handler->InvokeCallback();
// Since we downloaded largest icon, and don't want to set active favicon
// NotifyFaviconAvaliable() should be called with is_active_favicon as false.
EXPECT_EQ(new_favicon_url, driver1.available_icon_url());
EXPECT_FALSE(driver1.update_active_favicon());
EXPECT_EQ(3u, driver1.num_favicon_available());
}
INSTANTIATE_TEST_CASE_P(FaviconHandlerTestActiveFaviconValidityTrueOrFalse,
FaviconHandlerActiveFaviconValidityParamTest,
::testing::Bool());
} // namespace.
|