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
|
// Copyright 2012 The Chromium Authors
// 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_backend.h"
#include <memory>
#include <vector>
#include "base/containers/lru_cache.h"
#include "base/files/scoped_temp_dir.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "components/favicon/core/favicon_backend_delegate.h"
#include "components/favicon/core/favicon_database.h"
#include "components/favicon/core/favicon_types.h"
#include "components/favicon_base/favicon_types.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/codec/png_codec.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image_unittest_util.h"
#include "url/gurl.h"
namespace favicon {
namespace {
using favicon::FaviconBitmap;
using favicon::FaviconBitmapType;
using favicon::IconMapping;
using favicon_base::IconType;
using favicon_base::IconTypeSet;
using ::testing::ElementsAre;
using ::testing::UnorderedElementsAre;
using RedirectCache = base::LRUCache<GURL, std::vector<GURL>>;
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);
} // namespace
class FaviconBackendTest : public testing::Test, public FaviconBackendDelegate {
public:
FaviconBackendTest() = default;
~FaviconBackendTest() override = default;
// testing::Test
void SetUp() override {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
backend_ = FaviconBackend::Create(
temp_dir_.GetPath().AppendASCII("favicons"), this);
ASSERT_TRUE(backend_);
}
void TearDown() override { backend_.reset(); }
// FaviconBackendDelegate:
std::vector<GURL> GetCachedRecentRedirectsForPage(
const GURL& page_url) override {
auto iter = recent_redirects_.Get(page_url);
if (iter != recent_redirects_.end()) {
return iter->second;
}
return {page_url};
}
protected:
void SetFavicons(const base::flat_set<GURL>& page_urls,
favicon_base::IconType icon_type,
const GURL& icon_url,
const std::vector<SkBitmap>& bitmaps) {
backend_->SetFavicons(page_urls, icon_type, icon_url, bitmaps,
FaviconBitmapType::ON_VISIT);
}
// 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, IconType icon_type) {
std::vector<IconMapping> icon_mappings;
backend_->db()->GetIconMappingsForPageURL(page_url, {icon_type},
&icon_mappings);
return icon_mappings.size();
}
// Returns whether a favicon mapping with the combination of `page_url_type`,
// `icon_type` and `page_url_type` exists.
bool SingleIconMappingForPageURLExists(const GURL& page_url,
IconType icon_type,
PageUrlType page_url_type) {
std::vector<IconMapping> icon_mappings;
backend_->db()->GetIconMappingsForPageURL(page_url, {icon_type},
&icon_mappings);
return icon_mappings.size() == 1u &&
icon_mappings.front().page_url_type == page_url_type;
}
// Returns the icon mappings for `page_url`.
std::vector<IconMapping> GetIconMappingsForPageURL(const GURL& page_url) {
std::vector<IconMapping> icon_mappings;
backend_->db()->GetIconMappingsForPageURL(page_url, &icon_mappings);
return icon_mappings;
}
// 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_->db()->GetFaviconBitmaps(icon_id, favicon_bitmaps)) {
return false;
}
std::sort(favicon_bitmaps->begin(), favicon_bitmaps->end(),
[](const FaviconBitmap& a, const 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_->db()->GetFaviconBitmaps(icon_id, &favicon_bitmaps)) {
return false;
}
if (favicon_bitmaps.size() != 1) {
return false;
}
*favicon_bitmap = favicon_bitmaps[0];
return true;
}
// 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 = gfx::PNGCodec::Decode(*bitmap_data);
if (bitmap.isNull()) {
return false;
}
return expected_color == bitmap.getColor(0, 0);
}
base::test::TaskEnvironment task_environment_;
std::unique_ptr<FaviconBackend> backend_;
// Used in GetCachedRecentRedirectsForPage().
RedirectCache recent_redirects_{8};
private:
base::ScopedTempDir temp_dir_;
};
TEST_F(FaviconBackendTest, CloneFaviconMappingsForPages) {
// Init recent_redirects_
const GURL url1("http://www.google.com");
const GURL url2("http://www.google.ca");
const GURL url3("http://www.google.ca/m");
recent_redirects_.Put(url1, std::vector<GURL>{url1});
recent_redirects_.Put(url3, std::vector<GURL>{url2, url3});
const GURL icon_url1("http://www.google.com/icon");
std::vector<SkBitmap> bitmaps = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE)};
// Add a favicon.
SetFavicons({url1}, IconType::kFavicon, icon_url1, bitmaps);
EXPECT_TRUE(SingleIconMappingForPageURLExists(url1, IconType::kFavicon,
PageUrlType::kRegular));
backend_->CloneFaviconMappingsForPages(url1, {IconType::kFavicon},
base::flat_set<GURL>({url3}));
EXPECT_TRUE(SingleIconMappingForPageURLExists(url2, IconType::kFavicon,
PageUrlType::kRedirect));
EXPECT_TRUE(SingleIconMappingForPageURLExists(url3, IconType::kFavicon,
PageUrlType::kRegular));
}
// Test that SetFaviconMappingsForPageAndRedirects correctly updates icon
// mappings based on redirects, icon URLs and icon types.
TEST_F(FaviconBackendTest, SetFaviconMappingsForPageAndRedirects) {
// Init recent_redirects_
const GURL url1("http://www.google.com");
const GURL url2("http://www.google.com/m");
recent_redirects_.Put(url1, std::vector<GURL>{url2, url1});
const GURL icon_url1("http://www.google.com/icon");
const GURL icon_url2("http://www.google.com/icon2");
std::vector<SkBitmap> bitmaps = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE),
gfx::test::CreateBitmap(kLargeEdgeSize, SK_ColorRED)};
// Add a favicon.
SetFavicons({url1}, IconType::kFavicon, icon_url1, bitmaps);
EXPECT_TRUE(SingleIconMappingForPageURLExists(url1, IconType::kFavicon,
PageUrlType::kRegular));
EXPECT_TRUE(SingleIconMappingForPageURLExists(url2, IconType::kFavicon,
PageUrlType::kRedirect));
// Add one touch_icon
SetFavicons({url1}, IconType::kTouchIcon, icon_url1, bitmaps);
EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, IconType::kTouchIcon));
EXPECT_EQ(1u, NumIconMappingsForPageURL(url2, IconType::kTouchIcon));
EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, IconType::kFavicon));
// Add one kTouchPrecomposedIcon
SetFavicons({url1}, IconType::kTouchPrecomposedIcon, icon_url1, bitmaps);
// The touch_icon was replaced.
EXPECT_EQ(0u, NumIconMappingsForPageURL(url1, IconType::kTouchIcon));
EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, IconType::kFavicon));
EXPECT_EQ(1u,
NumIconMappingsForPageURL(url1, IconType::kTouchPrecomposedIcon));
EXPECT_EQ(1u,
NumIconMappingsForPageURL(url2, IconType::kTouchPrecomposedIcon));
// Add a touch_icon.
SetFavicons({url1}, IconType::kTouchIcon, icon_url1, bitmaps);
EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, IconType::kTouchIcon));
EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, IconType::kFavicon));
// The kTouchPrecomposedIcon was replaced.
EXPECT_EQ(0u,
NumIconMappingsForPageURL(url1, IconType::kTouchPrecomposedIcon));
// Add a web manifest_icon.
SetFavicons({url1}, IconType::kWebManifestIcon, icon_url2, bitmaps);
EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, IconType::kWebManifestIcon));
EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, IconType::kFavicon));
// The kTouchIcon was replaced.
EXPECT_EQ(0u, NumIconMappingsForPageURL(url1, IconType::kTouchIcon));
// The kTouchPrecomposedIcon was replaced.
EXPECT_EQ(0u,
NumIconMappingsForPageURL(url1, IconType::kTouchPrecomposedIcon));
// Add a different favicon.
SetFavicons({url1}, IconType::kFavicon, icon_url2, bitmaps);
EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, IconType::kWebManifestIcon));
EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, IconType::kFavicon));
EXPECT_EQ(1u, NumIconMappingsForPageURL(url2, IconType::kFavicon));
}
// Test that an icon mapping changes from PageUrlType::kRegular to
// PageUrlType::kRedirect when a web developer makes a page which previously did
// not redirect into a redirect.
TEST_F(FaviconBackendTest, SetFaviconMappingsForPageRedirectBecomesRegular) {
const GURL redirect_url("http://www.google.com/");
const GURL regular_url("http://google.com/");
const GURL icon_url("http://google.com/icon");
std::vector<SkBitmap> bitmaps = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE),
gfx::test::CreateBitmap(kLargeEdgeSize, SK_ColorRED)};
// Save the redirect_url as a PageUrlType::kRegular page.
SetFavicons({redirect_url}, IconType::kFavicon, icon_url, bitmaps);
std::vector<IconMapping> icon_mappings;
EXPECT_TRUE(backend_->db()->GetIconMappingsForPageURL(
redirect_url, {IconType::kFavicon}, &icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(PageUrlType::kRegular, icon_mappings.front().page_url_type);
favicon::IconMappingID original_id = icon_mappings[0].mapping_id;
// Save the redirect_url as a PageUrlType::kRedirect page.
recent_redirects_.Put(regular_url,
std::vector<GURL>{redirect_url, regular_url});
SetFavicons({regular_url}, IconType::kFavicon, icon_url, bitmaps);
icon_mappings.clear();
EXPECT_TRUE(backend_->db()->GetIconMappingsForPageURL(
redirect_url, {IconType::kFavicon}, &icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(PageUrlType::kRedirect, icon_mappings.front().page_url_type);
// Entry should have been changed as the page_url_type differed.
EXPECT_NE(original_id, icon_mappings[0].mapping_id);
}
TEST_F(FaviconBackendTest,
SetFaviconMappingsForPageAndRedirectsWithFragmentWithoutStripping) {
const GURL url("http://www.google.com#abc");
const GURL url_without_ref("http://www.google.com");
const GURL icon_url("http://www.google.com/icon");
SetFavicons({url}, IconType::kFavicon, icon_url,
std::vector<SkBitmap>{
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE)});
EXPECT_EQ(1u, NumIconMappingsForPageURL(url, IconType::kFavicon));
EXPECT_EQ(0u, NumIconMappingsForPageURL(url_without_ref, IconType::kFavicon));
}
// Test that there is no churn in icon mappings from calling
// SetFavicons() twice with the same `bitmaps` parameter.
TEST_F(FaviconBackendTest, SetFaviconMappingsForPageDuplicates) {
const GURL url("http://www.google.com/");
const GURL icon_url("http://www.google.com/icon");
std::vector<SkBitmap> bitmaps = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE),
gfx::test::CreateBitmap(kLargeEdgeSize, SK_ColorRED)};
SetFavicons({url}, IconType::kFavicon, icon_url, bitmaps);
std::vector<IconMapping> icon_mappings;
EXPECT_TRUE(backend_->db()->GetIconMappingsForPageURL(
url, {IconType::kFavicon}, &icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
favicon::IconMappingID mapping_id = icon_mappings[0].mapping_id;
SetFavicons({url}, IconType::kFavicon, icon_url, bitmaps);
icon_mappings.clear();
EXPECT_TRUE(backend_->db()->GetIconMappingsForPageURL(
url, {IconType::kFavicon}, &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(FaviconBackendTest, SetFaviconsDeleteBitmaps) {
const GURL page_url("http://www.google.com/");
const GURL icon_url("http://www.google.com/icon");
std::vector<SkBitmap> bitmaps = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE),
gfx::test::CreateBitmap(kLargeEdgeSize, SK_ColorRED)};
SetFavicons({page_url}, IconType::kFavicon, icon_url, bitmaps);
// Test initial state.
std::vector<IconMapping> icon_mappings = GetIconMappingsForPageURL(page_url);
ASSERT_EQ(1u, icon_mappings.size());
EXPECT_EQ(icon_url, icon_mappings[0].icon_url);
EXPECT_EQ(IconType::kFavicon, 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());
favicon::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);
favicon::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 = {gfx::test::CreateBitmap(kLargeEdgeSize, SK_ColorWHITE)};
SetFavicons({page_url}, IconType::kFavicon, icon_url, bitmaps);
scoped_refptr<base::RefCountedMemory> bitmap_data_out;
gfx::Size pixel_size_out;
EXPECT_FALSE(backend_->db()->GetFaviconBitmap(
small_bitmap_id, nullptr, nullptr, &bitmap_data_out, &pixel_size_out));
EXPECT_TRUE(backend_->db()->GetFaviconBitmap(
large_bitmap_id, nullptr, nullptr, &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_->db()->GetIconMappingsForPageURL(page_url, &icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(favicon_id, icon_mappings[0].icon_id);
}
// Test updating a single favicon bitmap's data via SetFavicons.
TEST_F(FaviconBackendTest, SetFaviconsReplaceBitmapData) {
const GURL page_url("http://www.google.com/");
const GURL icon_url("http://www.google.com/icon");
std::vector<SkBitmap> bitmaps = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE)};
// Add bitmap to the database.
SetFavicons({page_url}, IconType::kFavicon, icon_url, bitmaps);
favicon_base::FaviconID original_favicon_id =
backend_->db()->GetFaviconIDForFaviconURL(icon_url, IconType::kFavicon);
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_NE(base::Time(), original_favicon_bitmap.last_updated);
// Call SetFavicons() with completely identical data.
bitmaps[0] = gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE);
SetFavicons({page_url}, IconType::kFavicon, icon_url, bitmaps);
favicon_base::FaviconID updated_favicon_id =
backend_->db()->GetFaviconIDForFaviconURL(icon_url, IconType::kFavicon);
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));
EXPECT_NE(base::Time(), updated_favicon_bitmap.last_updated);
// Call SetFavicons() with a different bitmap of the same size.
bitmaps[0] = gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorWHITE);
SetFavicons({page_url}, IconType::kFavicon, icon_url, bitmaps);
updated_favicon_id =
backend_->db()->GetFaviconIDForFaviconURL(icon_url, IconType::kFavicon);
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);
}
// Test that if two pages share the same FaviconID, changing the favicon for
// one page does not affect the other.
TEST_F(FaviconBackendTest, 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.com/page");
std::vector<SkBitmap> bitmaps = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE),
gfx::test::CreateBitmap(kLargeEdgeSize, SK_ColorRED)};
SetFavicons({page_url1}, IconType::kFavicon, icon_url, bitmaps);
backend_->UpdateFaviconMappingsAndFetch(
{page_url2}, icon_url, IconType::kFavicon, GetEdgeSizesSmallAndLarge());
// Check that the same FaviconID is mapped to both page URLs.
std::vector<IconMapping> icon_mappings;
EXPECT_TRUE(
backend_->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_->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 = {gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorWHITE)};
SetFavicons({page_url1}, IconType::kFavicon, icon_url_new, bitmaps);
// `page_url1` should map to a new FaviconID and have valid bitmap data.
icon_mappings.clear();
EXPECT_TRUE(
backend_->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_->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_->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_->db()->GetFaviconBitmaps(favicon_id, &favicon_bitmaps));
EXPECT_EQ(2u, favicon_bitmaps.size());
}
// Test that if two pages share the same favicon, reported via a single call to
// SetFavicons(), it gets associated to both page URLs.
TEST_F(FaviconBackendTest, SetFaviconsWithTwoPageURLs) {
GURL icon_url("http://www.google.com/favicon.ico");
GURL page_url1("http://www.google.com");
GURL page_url2("http://www.google.ca");
std::vector<SkBitmap> bitmaps = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE),
gfx::test::CreateBitmap(kLargeEdgeSize, SK_ColorRED)};
SetFavicons({page_url1, page_url2}, IconType::kFavicon, icon_url, bitmaps);
std::vector<IconMapping> icon_mappings;
EXPECT_TRUE(
backend_->db()->GetIconMappingsForPageURL(page_url1, &icon_mappings));
ASSERT_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_->db()->GetIconMappingsForPageURL(page_url2, &icon_mappings));
ASSERT_EQ(1u, icon_mappings.size());
EXPECT_EQ(favicon_id, icon_mappings[0].icon_id);
}
// Test that favicon mappings can be deleted using DeleteFaviconMappings().
TEST_F(FaviconBackendTest, DeleteFaviconMappings) {
GURL icon_url1("http://www.google.com/favicon.ico");
GURL icon_url2("http://www.google.com/favicon2.ico");
GURL page_url("http://www.google.com");
std::vector<SkBitmap> bitmaps = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE),
gfx::test::CreateBitmap(kLargeEdgeSize, SK_ColorRED)};
// Setup
SetFavicons({page_url}, IconType::kFavicon, icon_url1, bitmaps);
SetFavicons({page_url}, IconType::kTouchIcon, icon_url2, bitmaps);
// Delete one of the two mappings.
backend_->DeleteFaviconMappings({page_url}, IconType::kTouchIcon);
EXPECT_EQ(1u, NumIconMappingsForPageURL(page_url, IconType::kFavicon));
EXPECT_EQ(0u, NumIconMappingsForPageURL(page_url, IconType::kTouchIcon));
// Delete the second mapping.
backend_->DeleteFaviconMappings({page_url}, IconType::kFavicon);
EXPECT_EQ(0u, NumIconMappingsForPageURL(page_url, IconType::kFavicon));
}
// Tests calling SetOnDemandFavicons(). Neither `page_url` nor `icon_url` are
// known to the database.
TEST_F(FaviconBackendTest, SetOnDemandFaviconsForEmptyDB) {
GURL page_url("http://www.google.com");
GURL icon_url("http:/www.google.com/favicon.ico");
std::vector<SkBitmap> bitmaps = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorRED)};
EXPECT_TRUE(
backend_
->SetOnDemandFavicons(page_url, IconType::kFavicon, icon_url, bitmaps)
.did_update_bitmap);
favicon_base::FaviconID favicon_id =
backend_->db()->GetFaviconIDForFaviconURL(icon_url, IconType::kFavicon);
EXPECT_NE(0, favicon_id);
FaviconBitmap favicon_bitmap;
ASSERT_TRUE(GetOnlyFaviconBitmap(favicon_id, &favicon_bitmap));
// The newly set bitmap should have been retrieved.
EXPECT_TRUE(BitmapColorEqual(SK_ColorRED, favicon_bitmap.bitmap_data));
// The favicon should be marked as expired.
EXPECT_EQ(base::Time(), favicon_bitmap.last_updated);
// The raw bitmap result is marked as fetched on-demand.
favicon_base::FaviconRawBitmapResult result =
backend_->GetLargestFaviconForUrl(
page_url, std::vector<IconTypeSet>({{IconType::kFavicon}}),
kSmallEdgeSize);
EXPECT_FALSE(result.fetched_because_of_page_visit);
}
// Tests calling SetOnDemandFavicons(). `page_url` is known to the database
// but `icon_url` is not (the second should be irrelevant though).
TEST_F(FaviconBackendTest, SetOnDemandFaviconsForPageInDB) {
GURL page_url("http://www.google.com");
GURL icon_url1("http:/www.google.com/favicon1.ico");
GURL icon_url2("http:/www.google.com/favicon2.ico");
std::vector<SkBitmap> bitmaps = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE)};
// Add bitmap to the database.
SetFavicons({page_url}, IconType::kFavicon, icon_url1, bitmaps);
favicon_base::FaviconID original_favicon_id =
backend_->db()->GetFaviconIDForFaviconURL(icon_url1, IconType::kFavicon);
ASSERT_NE(0, original_favicon_id);
// Call SetOnDemandFavicons() with a different icon URL and bitmap data.
bitmaps[0] = gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorWHITE);
EXPECT_FALSE(backend_
->SetOnDemandFavicons(page_url, IconType::kFavicon,
icon_url2, bitmaps)
.did_update_bitmap);
EXPECT_EQ(0, backend_->db()->GetFaviconIDForFaviconURL(icon_url2,
IconType::kFavicon));
FaviconBitmap favicon_bitmap;
ASSERT_TRUE(GetOnlyFaviconBitmap(original_favicon_id, &favicon_bitmap));
// The original bitmap should have been retrieved.
EXPECT_TRUE(BitmapColorEqual(SK_ColorBLUE, favicon_bitmap.bitmap_data));
// The favicon should not be marked as expired.
EXPECT_NE(base::Time(), favicon_bitmap.last_updated);
// The raw bitmap result is not marked as fetched on-demand.
favicon_base::FaviconRawBitmapResult result =
backend_->GetLargestFaviconForUrl(
page_url, std::vector<IconTypeSet>({{IconType::kFavicon}}),
kSmallEdgeSize);
EXPECT_TRUE(result.fetched_because_of_page_visit);
}
// Tests calling SetOnDemandFavicons(). `page_url` is not known to the
// database but `icon_url` is.
TEST_F(FaviconBackendTest, SetOnDemandFaviconsForIconInDB) {
const GURL old_page_url("http://www.google.com/old");
const GURL page_url("http://www.google.com/");
const GURL icon_url("http://www.google.com/icon");
std::vector<SkBitmap> bitmaps = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE)};
// Add bitmap to the database.
SetFavicons({old_page_url}, IconType::kFavicon, icon_url, bitmaps);
favicon_base::FaviconID original_favicon_id =
backend_->db()->GetFaviconIDForFaviconURL(icon_url, IconType::kFavicon);
ASSERT_NE(0, original_favicon_id);
// Call SetOnDemandFavicons() with a different bitmap.
bitmaps[0] = gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorWHITE);
EXPECT_FALSE(
backend_
->SetOnDemandFavicons(page_url, IconType::kFavicon, icon_url, bitmaps)
.did_update_bitmap);
EXPECT_EQ(original_favicon_id, backend_->db()->GetFaviconIDForFaviconURL(
icon_url, IconType::kFavicon));
FaviconBitmap favicon_bitmap;
ASSERT_TRUE(GetOnlyFaviconBitmap(original_favicon_id, &favicon_bitmap));
// The original bitmap should have been retrieved.
EXPECT_TRUE(BitmapColorEqual(SK_ColorBLUE, favicon_bitmap.bitmap_data));
// The favicon should not be marked as expired.
EXPECT_NE(base::Time(), favicon_bitmap.last_updated);
// The raw bitmap result is not marked as fetched on-demand.
favicon_base::FaviconRawBitmapResult result =
backend_->GetLargestFaviconForUrl(
page_url, std::vector<IconTypeSet>({{IconType::kFavicon}}),
kSmallEdgeSize);
EXPECT_TRUE(result.fetched_because_of_page_visit);
}
// Test repeatedly calling MergeFavicon(). `page_url` is initially not known
// to the database.
TEST_F(FaviconBackendTest, 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, IconType::kFavicon, bitmap_data,
kSmallSize);
// `page_url` should now be mapped to `icon_url` and the favicon bitmap should
// be expired.
std::vector<IconMapping> icon_mappings;
EXPECT_TRUE(
backend_->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_EQ(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, IconType::kFavicon, bitmap_data,
kSmallSize);
// `page_url` should still have a single favicon bitmap. The bitmap data
// should be updated.
icon_mappings.clear();
EXPECT_TRUE(
backend_->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_EQ(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(FaviconBackendTest, 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 = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE)};
SetFavicons({page_url}, IconType::kFavicon, icon_url1, bitmaps);
// Test initial state.
std::vector<IconMapping> icon_mappings;
EXPECT_TRUE(
backend_->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);
// 1) Merge identical favicon bitmap.
std::optional<std::vector<uint8_t>> data = gfx::PNGCodec::EncodeBGRASkBitmap(
bitmaps[0], /*discard_transparency=*/false);
scoped_refptr<base::RefCountedBytes> bitmap_data(
new base::RefCountedBytes(data.value()));
backend_->MergeFavicon(page_url, icon_url1, IconType::kFavicon, bitmap_data,
kSmallSize);
// All the data should stay the same and no notifications should have been
// sent.
icon_mappings.clear();
EXPECT_TRUE(
backend_->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);
// 2) Merge favicon bitmap of the same size.
data->clear();
data->push_back('b');
bitmap_data = new base::RefCountedBytes(data.value());
backend_->MergeFavicon(page_url, icon_url1, IconType::kFavicon, bitmap_data,
kSmallSize);
// The small favicon bitmap at `icon_url1` should be overwritten.
icon_mappings.clear();
EXPECT_TRUE(
backend_->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_EQ(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.value()[0] = 'c';
bitmap_data = new base::RefCountedBytes(data.value());
backend_->MergeFavicon(page_url, icon_url1, IconType::kFavicon, 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_->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_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);
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.value()[0] = 'd';
bitmap_data = new base::RefCountedBytes(data.value());
backend_->MergeFavicon(page_url, icon_url2, IconType::kFavicon, 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_->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_EQ(base::Time(), favicon_bitmaps[1].last_updated);
EXPECT_TRUE(BitmapDataEqual('d', favicon_bitmaps[1].bitmap_data));
EXPECT_EQ(kSmallSize, favicon_bitmaps[1].pixel_size);
}
// Test calling MergeFavicon() when `icon_url` is known to the database but not
// mapped to `page_url`.
TEST_F(FaviconBackendTest, 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 = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE)};
SetFavicons({page_url1}, IconType::kFavicon, icon_url, bitmaps);
// Test initial state.
std::vector<IconMapping> icon_mappings;
EXPECT_TRUE(
backend_->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::optional<std::vector<uint8_t>> data = gfx::PNGCodec::EncodeBGRASkBitmap(
bitmaps[0], /*discard_transparency=*/false);
scoped_refptr<base::RefCountedBytes> bitmap_data(
new base::RefCountedBytes(data.value()));
backend_->MergeFavicon(page_url2, icon_url, IconType::kFavicon, bitmap_data,
kSmallSize);
favicon_base::FaviconID favicon_id =
backend_->db()->GetFaviconIDForFaviconURL(icon_url, IconType::kFavicon);
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.value());
backend_->MergeFavicon(page_url3, icon_url, IconType::kFavicon, bitmap_data,
kSmallSize);
favicon_id =
backend_->db()->GetFaviconIDForFaviconURL(icon_url, IconType::kFavicon);
EXPECT_NE(0, favicon_id);
EXPECT_TRUE(GetOnlyFaviconBitmap(favicon_id, &favicon_bitmap));
EXPECT_EQ(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_->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_->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_->db()->GetIconMappingsForPageURL(page_url3, &icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
EXPECT_EQ(favicon_id, icon_mappings[0].icon_id);
}
// Test that MergeFavicon() does not add more than
// `kMaxFaviconBitmapsPerIconURL` to a favicon.
TEST_F(FaviconBackendTest, 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');
auto bitmap_data =
base::MakeRefCounted<base::RefCountedBytes>(std::move(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, IconType::kFavicon, 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_->db()->GetIconMappingsForPageURL(page_url, &icon_mappings));
EXPECT_EQ(1u, icon_mappings.size());
std::vector<FaviconBitmap> favicon_bitmaps;
EXPECT_TRUE(backend_->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(FaviconBackendTest, 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 = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE),
gfx::test::CreateBitmap(kLargeEdgeSize, SK_ColorRED)};
// Set some preexisting favicons for `page_url`.
SetFavicons({page_url}, IconType::kFavicon, 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, IconType::kFavicon,
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, {IconType::kFavicon},
GetEdgeSizesSmallAndLarge(), false);
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 that calling MergeFavicon() with identical favicon data does not affect
// the favicon bitmap's "last updated" time. This is important because sync
// calls MergeFavicon() for all of the favicons that it manages at startup.
TEST_F(FaviconBackendTest, MergeIdenticalFaviconDoesNotChangeLastUpdatedTime) {
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, IconType::kFavicon, bitmap_data,
kSmallSize);
// Find the ID of the add favicon bitmap.
std::vector<IconMapping> icon_mappings;
ASSERT_TRUE(
backend_->db()->GetIconMappingsForPageURL(page_url, &icon_mappings));
ASSERT_EQ(1u, icon_mappings.size());
std::vector<FaviconBitmap> favicon_bitmaps;
ASSERT_TRUE(backend_->db()->GetFaviconBitmaps(icon_mappings[0].icon_id,
&favicon_bitmaps));
// Change the last updated time of the just added favicon bitmap.
const base::Time kLastUpdateTime = base::Time::Now() - base::Days(314);
backend_->db()->SetFaviconBitmapLastUpdateTime(favicon_bitmaps[0].bitmap_id,
kLastUpdateTime);
// Call MergeFavicon() with identical data.
backend_->MergeFavicon(page_url, icon_url, IconType::kFavicon, bitmap_data,
kSmallSize);
// Check that the "last updated" time did not change.
icon_mappings.clear();
ASSERT_TRUE(
backend_->db()->GetIconMappingsForPageURL(page_url, &icon_mappings));
ASSERT_EQ(1u, icon_mappings.size());
favicon_bitmaps.clear();
ASSERT_TRUE(backend_->db()->GetFaviconBitmaps(icon_mappings[0].icon_id,
&favicon_bitmaps));
EXPECT_EQ(kLastUpdateTime, favicon_bitmaps[0].last_updated);
}
// Tests GetFaviconsForURL with icon_types priority,
TEST_F(FaviconBackendTest, 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 = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE),
gfx::test::CreateBitmap(kLargeEdgeSize, SK_ColorRED)};
std::vector<SkBitmap> touch_bitmaps = {
gfx::test::CreateBitmap(/*size=*/64, SK_ColorWHITE)};
// Set some preexisting favicons for `page_url`.
SetFavicons({page_url}, IconType::kFavicon, icon_url, favicon_bitmaps);
SetFavicons({page_url}, IconType::kTouchIcon, touch_icon_url, touch_bitmaps);
std::vector<IconTypeSet> icon_types;
icon_types.push_back({IconType::kFavicon});
icon_types.push_back({IconType::kTouchIcon});
favicon_base::FaviconRawBitmapResult result =
backend_->GetLargestFaviconForUrl(page_url, icon_types, 16);
// Verify the result icon is 32x32 favicon.
EXPECT_EQ(gfx::Size(32, 32), result.pixel_size);
EXPECT_EQ(IconType::kFavicon, result.icon_type);
// Change Minimal size to 32x32 and verify the 64x64 touch icon returned.
result = backend_->GetLargestFaviconForUrl(page_url, icon_types, 32);
EXPECT_EQ(gfx::Size(64, 64), result.pixel_size);
EXPECT_EQ(IconType::kTouchIcon, result.icon_type);
}
// Test the the first types of icon is returned if its size equal to the
// second types icon.
TEST_F(FaviconBackendTest, 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 = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE),
gfx::test::CreateBitmap(kLargeEdgeSize, SK_ColorRED)};
std::vector<SkBitmap> touch_bitmaps = {
gfx::test::CreateBitmap(kLargeEdgeSize, SK_ColorWHITE)};
// Set some preexisting favicons for `page_url`.
SetFavicons({page_url}, IconType::kFavicon, icon_url, favicon_bitmaps);
SetFavicons({page_url}, IconType::kTouchIcon, touch_icon_url, touch_bitmaps);
std::vector<IconTypeSet> icon_types;
icon_types.push_back({IconType::kFavicon});
icon_types.push_back({IconType::kTouchIcon});
favicon_base::FaviconRawBitmapResult result =
backend_->GetLargestFaviconForUrl(page_url, icon_types, 16);
// Verify the result icon is 32x32 favicon.
EXPECT_EQ(gfx::Size(32, 32), result.pixel_size);
EXPECT_EQ(IconType::kFavicon, 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);
EXPECT_EQ(gfx::Size(32, 32), result1.pixel_size);
EXPECT_EQ(IconType::kFavicon, result1.icon_type);
}
// Test the favicon is returned if its size is smaller than minimal size,
// because it is only one available.
TEST_F(FaviconBackendTest, TestGetFaviconsForURLReturnFaviconEvenItSmaller) {
GURL page_url("http://www.google.com");
GURL icon_url("http://www.google.com/favicon.ico");
std::vector<SkBitmap> bitmaps = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE)};
// Set preexisting favicons for `page_url`.
SetFavicons({page_url}, IconType::kFavicon, icon_url, bitmaps);
std::vector<IconTypeSet> icon_types;
icon_types.push_back({IconType::kFavicon});
icon_types.push_back({IconType::kTouchIcon});
favicon_base::FaviconRawBitmapResult result =
backend_->GetLargestFaviconForUrl(page_url, icon_types, 32);
// Verify 16x16 icon is returned, even it small than minimal_size.
EXPECT_EQ(gfx::Size(16, 16), result.pixel_size);
EXPECT_EQ(IconType::kFavicon, result.icon_type);
}
// Test the results of GetFaviconsForUrl() when there are no found favicons.
TEST_F(FaviconBackendTest, GetFaviconsForUrlEmpty) {
const GURL page_url("http://www.google.com/");
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results =
backend_->GetFaviconsForUrl(page_url, {IconType::kFavicon},
GetEdgeSizesSmallAndLarge(), false);
EXPECT_TRUE(bitmap_results.empty());
}
// Test the results of GetFaviconsForUrl() when there are matching favicons
// but there are no associated favicon bitmaps.
TEST_F(FaviconBackendTest, GetFaviconsForUrlNoFaviconBitmaps) {
const GURL page_url("http://www.google.com/");
const GURL icon_url("http://www.google.com/icon1");
favicon_base::FaviconID icon_id =
backend_->db()->AddFavicon(icon_url, IconType::kFavicon);
EXPECT_NE(0, icon_id);
EXPECT_NE(0, backend_->db()->AddIconMapping(page_url, icon_id,
PageUrlType::kRegular));
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results_out =
backend_->GetFaviconsForUrl(page_url, {IconType::kFavicon},
GetEdgeSizesSmallAndLarge(), false);
EXPECT_TRUE(bitmap_results_out.empty());
}
// Test that GetFaviconsForUrl() returns results for the bitmaps which most
// closely match the passed in the desired pixel sizes.
TEST_F(FaviconBackendTest, GetFaviconsForUrlSelectClosestMatch) {
const GURL page_url("http://www.google.com/");
const GURL icon_url("http://www.google.com/icon1");
std::vector<SkBitmap> bitmaps = {
gfx::test::CreateBitmap(kTinyEdgeSize, SK_ColorWHITE),
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE),
gfx::test::CreateBitmap(kLargeEdgeSize, SK_ColorRED)};
SetFavicons({page_url}, IconType::kFavicon, icon_url, bitmaps);
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results_out =
backend_->GetFaviconsForUrl(page_url, {IconType::kFavicon},
GetEdgeSizesSmallAndLarge(), false);
// 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(IconType::kFavicon, 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(IconType::kFavicon, bitmap_results_out[1].icon_type);
}
// Test the results of GetFaviconsForUrl() when called with different
// `icon_types`.
TEST_F(FaviconBackendTest, GetFaviconsForUrlIconType) {
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 = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE)};
std::vector<favicon_base::FaviconRawBitmapData> favicon_bitmap_data;
SetFavicons({page_url}, IconType::kFavicon, icon_url1, bitmaps);
SetFavicons({page_url}, IconType::kTouchIcon, icon_url2, bitmaps);
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results_out =
backend_->GetFaviconsForUrl(page_url, {IconType::kFavicon},
GetEdgeSizesSmallAndLarge(), false);
EXPECT_EQ(1u, bitmap_results_out.size());
EXPECT_EQ(IconType::kFavicon, bitmap_results_out[0].icon_type);
EXPECT_EQ(icon_url1, bitmap_results_out[0].icon_url);
bitmap_results_out = backend_->GetFaviconsForUrl(
page_url, {IconType::kTouchIcon}, GetEdgeSizesSmallAndLarge(), false);
EXPECT_EQ(1u, bitmap_results_out.size());
EXPECT_EQ(IconType::kTouchIcon, bitmap_results_out[0].icon_type);
EXPECT_EQ(icon_url2, bitmap_results_out[0].icon_url);
}
// Test that GetFaviconsForUrl() behaves correctly for different values of
// `fallback_to_host`.
TEST_F(FaviconBackendTest, GetFaviconsForUrlFallbackToHost) {
const GURL page_url_http("http://www.google.com/");
const GURL page_url_https("https://www.google.com/");
const GURL page_url_http_same_prefix("http://www.google.com.au/");
const GURL page_url_http_same_suffix("http://m.www.google.com/");
const GURL page_url_different_scheme("file://www.google.com/");
const GURL icon_url1("http://www.google.com.au/icon.png");
const GURL icon_url2("http://maps.google.com.au/icon.png");
const GURL icon_url3("https://www.google.com/icon.png");
std::vector<favicon_base::FaviconRawBitmapData> favicon_bitmap_data;
SetFavicons({page_url_http_same_prefix}, IconType::kFavicon, icon_url1,
{gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE)});
SetFavicons({page_url_http_same_suffix}, IconType::kFavicon, icon_url2,
{gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE)});
{
// Querying for the http URL with `fallback_to_host`=false returns nothing.
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results_out =
backend_->GetFaviconsForUrl(page_url_http,
{IconType::kFavicon, IconType::kTouchIcon},
{kSmallEdgeSize}, false);
EXPECT_TRUE(bitmap_results_out.empty());
// Querying for the http URL with `fallback_to_host`=true should not return
// the favicon associated with a different host, even when that host has the
// same prefix or suffix.
bitmap_results_out = backend_->GetFaviconsForUrl(
page_url_http, {IconType::kFavicon, IconType::kTouchIcon},
{kSmallEdgeSize}, true);
EXPECT_TRUE(bitmap_results_out.empty());
}
SetFavicons({page_url_https}, IconType::kFavicon, icon_url3,
{gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE)});
{
base::HistogramTester histogram_tester;
// Querying for the http URL with `fallback_to_host`=false returns nothing.
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results_out =
backend_->GetFaviconsForUrl(page_url_http,
{IconType::kFavicon, IconType::kTouchIcon},
{kSmallEdgeSize}, false);
EXPECT_TRUE(bitmap_results_out.empty());
// Querying for the http URL with `fallback_to_host`=true returns the
// favicon associated with the https URL.
bitmap_results_out = backend_->GetFaviconsForUrl(
page_url_http, {IconType::kFavicon, IconType::kTouchIcon},
{kSmallEdgeSize}, true);
ASSERT_EQ(1u, bitmap_results_out.size());
EXPECT_EQ(icon_url3, bitmap_results_out[0].icon_url);
histogram_tester.ExpectBucketCount("Favicons.FallbackToHostSuccess", true,
1);
}
{
base::HistogramTester histogram_tester;
// Querying for a URL with non HTTP/HTTPS scheme returns nothing even if
// `fallback_to_host` is true.
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results_out =
backend_->GetFaviconsForUrl(page_url_different_scheme,
{IconType::kFavicon, IconType::kTouchIcon},
{kSmallEdgeSize}, false);
EXPECT_TRUE(bitmap_results_out.empty());
bitmap_results_out = backend_->GetFaviconsForUrl(
page_url_different_scheme, {IconType::kFavicon, IconType::kTouchIcon},
{kSmallEdgeSize}, true);
EXPECT_TRUE(bitmap_results_out.empty());
histogram_tester.ExpectBucketCount("Favicons.FallbackToHostSuccess", true,
0);
}
}
// Test that when GetFaviconsForUrl() is called with multiple icon types that
// the best favicon bitmap is selected from among all of the icon types.
TEST_F(FaviconBackendTest, GetFaviconsForUrlMultipleIconTypes) {
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<favicon_base::FaviconRawBitmapData> favicon_bitmap_data;
SetFavicons({page_url}, IconType::kFavicon, icon_url1,
{gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE)});
SetFavicons({page_url}, IconType::kTouchIcon, icon_url2,
{gfx::test::CreateBitmap(kLargeEdgeSize, SK_ColorBLUE)});
struct TestCase {
int desired_edge_size;
GURL expected_icon_url;
} kTestCases[]{{kSmallEdgeSize, icon_url1}, {kLargeEdgeSize, icon_url2}};
for (const TestCase& test_case : kTestCases) {
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results =
backend_->GetFaviconsForUrl(page_url,
{IconType::kFavicon, IconType::kTouchIcon},
{test_case.desired_edge_size}, false);
ASSERT_EQ(1u, bitmap_results.size());
EXPECT_EQ(test_case.expected_icon_url, bitmap_results[0].icon_url);
}
}
// Test that GetFaviconsForUrl() correctly sets the expired flag for bitmap
// reults.
TEST_F(FaviconBackendTest, GetFaviconsForUrlExpired) {
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');
auto bitmap_data =
base::MakeRefCounted<base::RefCountedBytes>(std::move(data));
base::Time last_updated = base::Time::FromTimeT(0);
favicon_base::FaviconID icon_id = backend_->db()->AddFavicon(
icon_url, IconType::kFavicon, bitmap_data, FaviconBitmapType::ON_VISIT,
last_updated, kSmallSize);
EXPECT_NE(0, icon_id);
EXPECT_NE(0, backend_->db()->AddIconMapping(page_url, icon_id,
PageUrlType::kRegular));
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results_out =
backend_->GetFaviconsForUrl(page_url, {IconType::kFavicon},
GetEdgeSizesSmallAndLarge(), false);
EXPECT_EQ(1u, bitmap_results_out.size());
EXPECT_TRUE(bitmap_results_out[0].expired);
}
// Tests that GetFaviconsForUrl() updates the IconSuccess histogram.
TEST_F(FaviconBackendTest, GetFaviconsForUrlUpdateSizeHistogram) {
base::HistogramTester histogram_tester;
const GURL page_url("http://www.google.com/");
const GURL icon_url1("http://www.google.com/icon1.png");
std::vector<SkBitmap> bitmaps = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE)};
std::vector<favicon_base::FaviconRawBitmapData> favicon_bitmap_data;
SetFavicons({page_url}, IconType::kFavicon, icon_url1, bitmaps);
std::vector<favicon_base::FaviconRawBitmapResult> bitmap_results_out =
backend_->GetFaviconsForUrl(page_url, {IconType::kFavicon},
GetEdgeSizesSmallAndLarge(), false);
EXPECT_EQ(1u, bitmap_results_out.size());
EXPECT_EQ(IconType::kFavicon, bitmap_results_out[0].icon_type);
EXPECT_EQ(icon_url1, bitmap_results_out[0].icon_url);
histogram_tester.ExpectBucketCount("Favicons.IconSuccess.16px", true, 1);
}
// Test that a favicon isn't loaded cross-origin.
TEST_F(FaviconBackendTest, FaviconCacheWillNotLoadCrossOrigin) {
GURL icon_url("http://www.google.com/favicon.ico");
GURL page_url1("http://www.google.com");
GURL page_url2("http://www.google.ca");
std::vector<SkBitmap> bitmaps = {
gfx::test::CreateBitmap(kSmallEdgeSize, SK_ColorBLUE),
gfx::test::CreateBitmap(kLargeEdgeSize, SK_ColorRED)};
// Store `icon_url` for `page_url1`, but just attempt load for `page_url2`.
SetFavicons({page_url1}, IconType::kFavicon, icon_url, bitmaps);
backend_->UpdateFaviconMappingsAndFetch(
{page_url2}, icon_url, IconType::kFavicon, GetEdgeSizesSmallAndLarge());
// Check that the same FaviconID is mapped just to `page_url1`.
std::vector<IconMapping> icon_mappings;
EXPECT_TRUE(
backend_->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_FALSE(
backend_->db()->GetIconMappingsForPageURL(page_url2, &icon_mappings));
}
} // namespace favicon
|