OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <algorithm> |
5 #include <set> | 6 #include <set> |
6 #include <vector> | 7 #include <vector> |
7 | 8 |
8 #include "base/command_line.h" | 9 #include "base/command_line.h" |
9 #include "base/file_path.h" | 10 #include "base/file_path.h" |
10 #include "base/file_util.h" | 11 #include "base/file_util.h" |
11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/path_service.h" | 14 #include "base/path_service.h" |
14 #include "base/string16.h" | 15 #include "base/string16.h" |
(...skipping 23 matching lines...) Expand all Loading... |
38 // backend directly. Most of the history backend functions are tested by the | 39 // backend directly. Most of the history backend functions are tested by the |
39 // history unit test. Because of the elaborate callbacks involved, this is no | 40 // history unit test. Because of the elaborate callbacks involved, this is no |
40 // harder than calling it directly for many things. | 41 // harder than calling it directly for many things. |
41 | 42 |
42 namespace { | 43 namespace { |
43 | 44 |
44 // data we'll put into the thumbnail database | 45 // data we'll put into the thumbnail database |
45 static const unsigned char blob1[] = | 46 static const unsigned char blob1[] = |
46 "12346102356120394751634516591348710478123649165419234519234512349134"; | 47 "12346102356120394751634516591348710478123649165419234519234512349134"; |
47 | 48 |
| 49 static const gfx::Size kTinySize = gfx::Size(10, 10); |
48 static const gfx::Size kSmallSize = gfx::Size(16, 16); | 50 static const gfx::Size kSmallSize = gfx::Size(16, 16); |
49 static const gfx::Size kLargeSize = gfx::Size(48, 48); | 51 static const gfx::Size kLargeSize = gfx::Size(32, 32); |
| 52 |
| 53 |
| 54 // Comparison functions as to make it easier to check results of |
| 55 // GetFaviconBitmaps() and GetIconMappings(). |
| 56 bool IconMappingLessThan(const history::IconMapping& a, |
| 57 const history::IconMapping& b) { |
| 58 return a.icon_url < b.icon_url; |
| 59 } |
| 60 |
| 61 bool FaviconBitmapLessThan(const history::FaviconBitmap& a, |
| 62 const history::FaviconBitmap& b) { |
| 63 return a.pixel_size.GetArea() < b.pixel_size.GetArea(); |
| 64 } |
50 | 65 |
51 } // namepace | 66 } // namepace |
52 | 67 |
53 namespace history { | 68 namespace history { |
54 | 69 |
55 class HistoryBackendTest; | 70 class HistoryBackendTest; |
56 | 71 |
57 // This must be a separate object since HistoryBackend manages its lifetime. | 72 // This must be a separate object since HistoryBackend manages its lifetime. |
58 // This just forwards the messages we're interested in to the test object. | 73 // This just forwards the messages we're interested in to the test object. |
59 class HistoryBackendTestDelegate : public HistoryBackend::Delegate { | 74 class HistoryBackendTestDelegate : public HistoryBackend::Delegate { |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 URLID id = backend_->db()->GetRowForURL(url, &row); | 216 URLID id = backend_->db()->GetRowForURL(url, &row); |
202 VisitVector visits; | 217 VisitVector visits; |
203 EXPECT_TRUE(backend_->db()->GetVisitsForURL(id, &visits)); | 218 EXPECT_TRUE(backend_->db()->GetVisitsForURL(id, &visits)); |
204 return visits[0].transition; | 219 return visits[0].transition; |
205 } | 220 } |
206 | 221 |
207 FilePath getTestDir() { | 222 FilePath getTestDir() { |
208 return test_dir_; | 223 return test_dir_; |
209 } | 224 } |
210 | 225 |
| 226 // Returns a gfx::Size vector with small size. |
| 227 const std::vector<gfx::Size>& GetSizesSmall() { |
| 228 CR_DEFINE_STATIC_LOCAL(std::vector<gfx::Size>, kSizesSmall, ()); |
| 229 if (kSizesSmall.empty()) |
| 230 kSizesSmall.push_back(kSmallSize); |
| 231 return kSizesSmall; |
| 232 } |
| 233 |
| 234 // Returns a gfx::Size vector with large size. |
| 235 const std::vector<gfx::Size>& GetSizesLarge() { |
| 236 CR_DEFINE_STATIC_LOCAL(std::vector<gfx::Size>, kSizesLarge, ()); |
| 237 if (kSizesLarge.empty()) |
| 238 kSizesLarge.push_back(kSmallSize); |
| 239 return kSizesLarge; |
| 240 } |
| 241 |
211 // Returns a gfx::Size vector with small and large sizes. | 242 // Returns a gfx::Size vector with small and large sizes. |
212 const std::vector<gfx::Size>& GetSizesSmallAndLarge() { | 243 const std::vector<gfx::Size>& GetSizesSmallAndLarge() { |
213 CR_DEFINE_STATIC_LOCAL(std::vector<gfx::Size>, kSizesSmallAndLarge, ()); | 244 CR_DEFINE_STATIC_LOCAL(std::vector<gfx::Size>, kSizesSmallAndLarge, ()); |
214 if (kSizesSmallAndLarge.empty()) { | 245 if (kSizesSmallAndLarge.empty()) { |
215 kSizesSmallAndLarge.push_back(kSmallSize); | 246 kSizesSmallAndLarge.push_back(kSmallSize); |
216 kSizesSmallAndLarge.push_back(kLargeSize); | 247 kSizesSmallAndLarge.push_back(kLargeSize); |
217 } | 248 } |
218 return kSizesSmallAndLarge; | 249 return kSizesSmallAndLarge; |
219 } | 250 } |
220 | 251 |
221 FaviconID GetFavicon(const GURL& url, IconType icon_type) { | 252 // Returns a gfx::Size vector with tiny, small and large sizes. |
| 253 const std::vector<gfx::Size>& GetSizesTinySmallAndLarge() { |
| 254 CR_DEFINE_STATIC_LOCAL(std::vector<gfx::Size>, |
| 255 kSizesTinySmallAndLarge, ()); |
| 256 if (kSizesTinySmallAndLarge.empty()) { |
| 257 kSizesTinySmallAndLarge.push_back(kTinySize); |
| 258 kSizesTinySmallAndLarge.push_back(kSmallSize); |
| 259 kSizesTinySmallAndLarge.push_back(kLargeSize); |
| 260 } |
| 261 return kSizesTinySmallAndLarge; |
| 262 } |
| 263 |
| 264 // Returns 1x and 2x scale factors. |
| 265 const std::vector<ui::ScaleFactor>& GetScaleFactors1x2x() { |
| 266 CR_DEFINE_STATIC_LOCAL(std::vector<ui::ScaleFactor>, kScaleFactors1x2x, ()); |
| 267 if (kScaleFactors1x2x.empty()) { |
| 268 kScaleFactors1x2x.push_back(ui::SCALE_FACTOR_100P); |
| 269 kScaleFactors1x2x.push_back(ui::SCALE_FACTOR_200P); |
| 270 } |
| 271 return kScaleFactors1x2x; |
| 272 } |
| 273 |
| 274 // Returns the number of icon mappings of |icon_type| to |page_url|. |
| 275 size_t NumIconMappingsForPageURL(const GURL& page_url, IconType icon_type) { |
222 std::vector<IconMapping> icon_mappings; | 276 std::vector<IconMapping> icon_mappings; |
223 if (backend_->thumbnail_db_->GetIconMappingsForPageURL(url, icon_type, | 277 backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url, icon_type, |
224 &icon_mappings)) | 278 &icon_mappings); |
225 return icon_mappings[0].icon_id; | 279 return icon_mappings.size(); |
226 else | 280 } |
227 return 0; | 281 |
| 282 // Returns the icon mappings for |page_url| sorted alphabetically by icon |
| 283 // URL in ascending order. |
| 284 bool GetSortedIconMappingsForPageURL( |
| 285 const GURL& page_url, |
| 286 std::vector<IconMapping>* icon_mappings) { |
| 287 if (!backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url, |
| 288 icon_mappings)) { |
| 289 return false; |
| 290 } |
| 291 std::sort(icon_mappings->begin(), icon_mappings->end(), |
| 292 IconMappingLessThan); |
| 293 return true; |
| 294 } |
| 295 |
| 296 // Returns the favicon bitmaps for |icon_id| sorted by pixel size in |
| 297 // ascending order. |
| 298 bool GetSortedFaviconBitmaps(FaviconID icon_id, |
| 299 std::vector<FaviconBitmap>* favicon_bitmaps) { |
| 300 if (!backend_->thumbnail_db_->GetFaviconBitmaps(icon_id, favicon_bitmaps)) |
| 301 return false; |
| 302 std::sort(favicon_bitmaps->begin(), favicon_bitmaps->end(), |
| 303 FaviconBitmapLessThan); |
| 304 return true; |
| 305 } |
| 306 |
| 307 // Returns true if there is exactly one favicon bitmap associated to |
| 308 // |icon_url|. If true, returns favicon bitmap in output parameter. |
| 309 bool GetOnlyFaviconBitmapForIconURL(const GURL& icon_url, |
| 310 IconType icon_type, |
| 311 FaviconBitmap* favicon_bitmap) { |
| 312 FaviconID id = backend_->thumbnail_db_->GetFaviconIDForFaviconURL( |
| 313 icon_url, icon_type, NULL); |
| 314 if (!id) |
| 315 return false; |
| 316 std::vector<FaviconBitmap> favicon_bitmaps; |
| 317 if (!backend_->thumbnail_db_->GetFaviconBitmaps(id, &favicon_bitmaps)) |
| 318 return false; |
| 319 if (favicon_bitmaps.size() != 1) |
| 320 return false; |
| 321 *favicon_bitmap = favicon_bitmaps[0]; |
| 322 return true; |
| 323 } |
| 324 |
| 325 // Generates |favicon_bitmap_data| with entries for the icon_urls and sizes |
| 326 // specified. The bitmap_data for entries are lowercase letters of the |
| 327 // alphabet starting at 'a' for the entry at index 0. |
| 328 void GenerateFaviconBitmapData( |
| 329 const GURL& icon_url1, |
| 330 const std::vector<gfx::Size>& icon_url1_sizes, |
| 331 std::vector<FaviconBitmapData>* favicon_bitmap_data) { |
| 332 GenerateFaviconBitmapData(icon_url1, icon_url1_sizes, GURL(), |
| 333 std::vector<gfx::Size>(), favicon_bitmap_data); |
| 334 } |
| 335 |
| 336 void GenerateFaviconBitmapData( |
| 337 const GURL& icon_url1, |
| 338 const std::vector<gfx::Size>& icon_url1_sizes, |
| 339 const GURL& icon_url2, |
| 340 const std::vector<gfx::Size>& icon_url2_sizes, |
| 341 std::vector<FaviconBitmapData>* favicon_bitmap_data) { |
| 342 favicon_bitmap_data->clear(); |
| 343 |
| 344 char bitmap_char = 'a'; |
| 345 for (size_t i = 0; i < icon_url1_sizes.size(); ++i) { |
| 346 std::vector<unsigned char> data; |
| 347 data.push_back(bitmap_char); |
| 348 FaviconBitmapData bitmap_data_element; |
| 349 bitmap_data_element.bitmap_data = |
| 350 base::RefCountedBytes::TakeVector(&data); |
| 351 bitmap_data_element.pixel_size = icon_url1_sizes[i]; |
| 352 bitmap_data_element.icon_url = icon_url1; |
| 353 favicon_bitmap_data->push_back(bitmap_data_element); |
| 354 |
| 355 ++bitmap_char; |
| 356 } |
| 357 |
| 358 for (size_t i = 0; i < icon_url2_sizes.size(); ++i) { |
| 359 std::vector<unsigned char> data; |
| 360 data.push_back(bitmap_char); |
| 361 FaviconBitmapData bitmap_data_element; |
| 362 bitmap_data_element.bitmap_data = |
| 363 base::RefCountedBytes::TakeVector(&data); |
| 364 bitmap_data_element.pixel_size = icon_url2_sizes[i]; |
| 365 bitmap_data_element.icon_url = icon_url2; |
| 366 favicon_bitmap_data->push_back(bitmap_data_element); |
| 367 |
| 368 ++bitmap_char; |
| 369 } |
| 370 } |
| 371 |
| 372 // Returns true if |bitmap_data| is equal to |expected_data|. |
| 373 bool BitmapDataEqual(char expected_data, |
| 374 scoped_refptr<base::RefCountedMemory> bitmap_data) { |
| 375 return bitmap_data.get() && |
| 376 bitmap_data->size() == 1u && |
| 377 *bitmap_data->front() == expected_data; |
228 } | 378 } |
229 | 379 |
230 BookmarkModel bookmark_model_; | 380 BookmarkModel bookmark_model_; |
231 | 381 |
232 protected: | 382 protected: |
233 bool loaded_; | 383 bool loaded_; |
234 | 384 |
235 private: | 385 private: |
236 friend class HistoryBackendTestDelegate; | 386 friend class HistoryBackendTestDelegate; |
237 | 387 |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 EXPECT_TRUE(out_favicon1); | 585 EXPECT_TRUE(out_favicon1); |
436 | 586 |
437 std::vector<FaviconBitmap> favicon_bitmaps; | 587 std::vector<FaviconBitmap> favicon_bitmaps; |
438 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps( | 588 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps( |
439 out_favicon1, &favicon_bitmaps)); | 589 out_favicon1, &favicon_bitmaps)); |
440 ASSERT_EQ(2u, favicon_bitmaps.size()); | 590 ASSERT_EQ(2u, favicon_bitmaps.size()); |
441 | 591 |
442 FaviconBitmap favicon_bitmap1 = favicon_bitmaps[0]; | 592 FaviconBitmap favicon_bitmap1 = favicon_bitmaps[0]; |
443 FaviconBitmap favicon_bitmap2 = favicon_bitmaps[1]; | 593 FaviconBitmap favicon_bitmap2 = favicon_bitmaps[1]; |
444 | 594 |
445 // Bitmaps do not need to be in particular order. | 595 // Favicon bitmaps do not need to be in particular order. |
446 if (favicon_bitmap1.pixel_size == kLargeSize) { | 596 if (favicon_bitmap1.pixel_size == kLargeSize) { |
447 FaviconBitmap tmp_favicon_bitmap = favicon_bitmap1; | 597 FaviconBitmap tmp_favicon_bitmap = favicon_bitmap1; |
448 favicon_bitmap1 = favicon_bitmap2; | 598 favicon_bitmap1 = favicon_bitmap2; |
449 favicon_bitmap2 = tmp_favicon_bitmap; | 599 favicon_bitmap2 = tmp_favicon_bitmap; |
450 } | 600 } |
451 | 601 |
452 EXPECT_EQ('a', *favicon_bitmap1.bitmap_data->front()); | 602 EXPECT_TRUE(BitmapDataEqual('a', favicon_bitmap1.bitmap_data)); |
453 EXPECT_EQ(kSmallSize, favicon_bitmap1.pixel_size); | 603 EXPECT_EQ(kSmallSize, favicon_bitmap1.pixel_size); |
454 | 604 |
455 EXPECT_EQ('b', *favicon_bitmap2.bitmap_data->front()); | 605 EXPECT_TRUE(BitmapDataEqual('b', favicon_bitmap2.bitmap_data)); |
456 EXPECT_EQ(kLargeSize, favicon_bitmap2.pixel_size); | 606 EXPECT_EQ(kLargeSize, favicon_bitmap2.pixel_size); |
457 | 607 |
458 FaviconID out_favicon2 = backend_->thumbnail_db_-> | 608 FaviconID out_favicon2 = backend_->thumbnail_db_-> |
459 GetFaviconIDForFaviconURL(favicon_url2, FAVICON, NULL); | 609 GetFaviconIDForFaviconURL(favicon_url2, FAVICON, NULL); |
460 EXPECT_FALSE(out_favicon2) << "Favicon not deleted"; | 610 EXPECT_FALSE(out_favicon2) << "Favicon not deleted"; |
461 | 611 |
462 // The remaining URL should still reference the same favicon, even if its | 612 // The remaining URL should still reference the same favicon, even if its |
463 // ID has changed. | 613 // ID has changed. |
464 EXPECT_EQ(out_favicon1, GetFavicon(outrow1.url(), FAVICON)); | 614 std::vector<IconMapping> mappings; |
| 615 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( |
| 616 outrow1.url(), FAVICON, &mappings)); |
| 617 EXPECT_EQ(1u, mappings.size()); |
| 618 EXPECT_EQ(out_favicon1, mappings[0].icon_id); |
465 | 619 |
466 // The first URL should still be bookmarked. | 620 // The first URL should still be bookmarked. |
467 EXPECT_TRUE(bookmark_model_.IsBookmarked(row1.url())); | 621 EXPECT_TRUE(bookmark_model_.IsBookmarked(row1.url())); |
468 | 622 |
469 // The full text database should have no data. | 623 // The full text database should have no data. |
470 std::vector<TextDatabase::Match> text_matches; | 624 std::vector<TextDatabase::Match> text_matches; |
471 Time first_time_searched; | 625 Time first_time_searched; |
472 backend_->text_database_->GetTextMatches(UTF8ToUTF16("Body"), | 626 backend_->text_database_->GetTextMatches(UTF8ToUTF16("Body"), |
473 QueryOptions(), | 627 QueryOptions(), |
474 &text_matches, | 628 &text_matches, |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
734 URLRow row2(GURL("http://news.google.com/")); | 888 URLRow row2(GURL("http://news.google.com/")); |
735 row2.set_visit_count(1); | 889 row2.set_visit_count(1); |
736 row2.set_last_visit(Time::Now()); | 890 row2.set_last_visit(Time::Now()); |
737 URLRows rows; | 891 URLRows rows; |
738 rows.push_back(row1); | 892 rows.push_back(row1); |
739 rows.push_back(row2); | 893 rows.push_back(row2); |
740 backend_->AddPagesWithDetails(rows, history::SOURCE_BROWSED); | 894 backend_->AddPagesWithDetails(rows, history::SOURCE_BROWSED); |
741 URLRow url_row1, url_row2; | 895 URLRow url_row1, url_row2; |
742 EXPECT_FALSE(backend_->db_->GetRowForURL(row1.url(), &url_row1) == 0); | 896 EXPECT_FALSE(backend_->db_->GetRowForURL(row1.url(), &url_row1) == 0); |
743 EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &url_row2) == 0); | 897 EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &url_row2) == 0); |
744 EXPECT_FALSE(GetFavicon(row1.url(), FAVICON) == 0); | 898 EXPECT_EQ(1u, NumIconMappingsForPageURL(row1.url(), FAVICON)); |
745 EXPECT_TRUE(GetFavicon(row2.url(), FAVICON) == 0); | 899 EXPECT_EQ(0u, NumIconMappingsForPageURL(row2.url(), FAVICON)); |
746 | 900 |
747 // Now provide one imported favicon for both URLs already in the registry. | 901 // Now provide one imported favicon for both URLs already in the registry. |
748 // The new favicon should only be used with the URL that doesn't already have | 902 // The new favicon should only be used with the URL that doesn't already have |
749 // a favicon. | 903 // a favicon. |
750 std::vector<history::ImportedFaviconUsage> favicons; | 904 std::vector<history::ImportedFaviconUsage> favicons; |
751 history::ImportedFaviconUsage favicon; | 905 history::ImportedFaviconUsage favicon; |
752 favicon.favicon_url = GURL("http://news.google.com/favicon.ico"); | 906 favicon.favicon_url = GURL("http://news.google.com/favicon.ico"); |
753 favicon.png_data.push_back('2'); | 907 favicon.png_data.push_back('2'); |
754 favicon.urls.insert(row1.url()); | 908 favicon.urls.insert(row1.url()); |
755 favicon.urls.insert(row2.url()); | 909 favicon.urls.insert(row2.url()); |
756 favicons.push_back(favicon); | 910 favicons.push_back(favicon); |
757 backend_->SetImportedFavicons(favicons); | 911 backend_->SetImportedFavicons(favicons); |
758 EXPECT_FALSE(backend_->db_->GetRowForURL(row1.url(), &url_row1) == 0); | 912 EXPECT_FALSE(backend_->db_->GetRowForURL(row1.url(), &url_row1) == 0); |
759 EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &url_row2) == 0); | 913 EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &url_row2) == 0); |
760 EXPECT_FALSE(GetFavicon(row1.url(), FAVICON) == 0); | 914 |
761 EXPECT_FALSE(GetFavicon(row2.url(), FAVICON) == 0); | 915 std::vector<IconMapping> mappings; |
762 EXPECT_FALSE(GetFavicon(row1.url(), FAVICON) == | 916 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( |
763 GetFavicon(row2.url(), FAVICON)); | 917 row1.url(), FAVICON, &mappings)); |
| 918 EXPECT_EQ(1u, mappings.size()); |
| 919 EXPECT_EQ(favicon1, mappings[0].icon_id); |
| 920 EXPECT_EQ(favicon_url1, mappings[0].icon_url); |
| 921 |
| 922 mappings.clear(); |
| 923 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( |
| 924 row2.url(), FAVICON, &mappings)); |
| 925 EXPECT_EQ(1u, mappings.size()); |
| 926 EXPECT_EQ(favicon.favicon_url, mappings[0].icon_url); |
764 | 927 |
765 // A URL should not be added to history (to store favicon), if | 928 // A URL should not be added to history (to store favicon), if |
766 // the URL is not bookmarked. | 929 // the URL is not bookmarked. |
767 GURL url3("http://mail.google.com"); | 930 GURL url3("http://mail.google.com"); |
768 favicons.clear(); | 931 favicons.clear(); |
769 favicon.favicon_url = GURL("http://mail.google.com/favicon.ico"); | 932 favicon.favicon_url = GURL("http://mail.google.com/favicon.ico"); |
770 favicon.png_data.push_back('3'); | 933 favicon.png_data.push_back('3'); |
771 favicon.urls.insert(url3); | 934 favicon.urls.insert(url3); |
772 favicons.push_back(favicon); | 935 favicons.push_back(favicon); |
773 backend_->SetImportedFavicons(favicons); | 936 backend_->SetImportedFavicons(favicons); |
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1110 EXPECT_EQ(cur_version, file_version); | 1273 EXPECT_EQ(cur_version, file_version); |
1111 | 1274 |
1112 // Check visit_source table is created and empty. | 1275 // Check visit_source table is created and empty. |
1113 s.Assign(db.GetUniqueStatement( | 1276 s.Assign(db.GetUniqueStatement( |
1114 "SELECT name FROM sqlite_master WHERE name=\"visit_source\"")); | 1277 "SELECT name FROM sqlite_master WHERE name=\"visit_source\"")); |
1115 ASSERT_TRUE(s.Step()); | 1278 ASSERT_TRUE(s.Step()); |
1116 s.Assign(db.GetUniqueStatement("SELECT * FROM visit_source LIMIT 10")); | 1279 s.Assign(db.GetUniqueStatement("SELECT * FROM visit_source LIMIT 10")); |
1117 EXPECT_FALSE(s.Step()); | 1280 EXPECT_FALSE(s.Step()); |
1118 } | 1281 } |
1119 | 1282 |
1120 TEST_F(HistoryBackendTest, SetFaviconMapping) { | 1283 // Test that SetFaviconMappingsForPageAndRedirects correctly updates icon |
| 1284 // mappings based on redirects, icon URLs and icon types. |
| 1285 TEST_F(HistoryBackendTest, SetFaviconMappingsForPageAndRedirects) { |
1121 // Init recent_redirects_ | 1286 // Init recent_redirects_ |
1122 const GURL url1("http://www.google.com"); | 1287 const GURL url1("http://www.google.com"); |
1123 const GURL url2("http://www.google.com/m"); | 1288 const GURL url2("http://www.google.com/m"); |
1124 URLRow url_info1(url1); | 1289 URLRow url_info1(url1); |
1125 url_info1.set_visit_count(0); | 1290 url_info1.set_visit_count(0); |
1126 url_info1.set_typed_count(0); | 1291 url_info1.set_typed_count(0); |
1127 url_info1.set_last_visit(base::Time()); | 1292 url_info1.set_last_visit(base::Time()); |
1128 url_info1.set_hidden(false); | 1293 url_info1.set_hidden(false); |
1129 backend_->db_->AddURL(url_info1); | 1294 backend_->db_->AddURL(url_info1); |
1130 | 1295 |
1131 URLRow url_info2(url2); | 1296 URLRow url_info2(url2); |
1132 url_info2.set_visit_count(0); | 1297 url_info2.set_visit_count(0); |
1133 url_info2.set_typed_count(0); | 1298 url_info2.set_typed_count(0); |
1134 url_info2.set_last_visit(base::Time()); | 1299 url_info2.set_last_visit(base::Time()); |
1135 url_info2.set_hidden(false); | 1300 url_info2.set_hidden(false); |
1136 backend_->db_->AddURL(url_info2); | 1301 backend_->db_->AddURL(url_info2); |
1137 | 1302 |
1138 history::RedirectList redirects; | 1303 history::RedirectList redirects; |
1139 redirects.push_back(url2); | 1304 redirects.push_back(url2); |
1140 redirects.push_back(url1); | 1305 redirects.push_back(url1); |
1141 backend_->recent_redirects_.Put(url1, redirects); | 1306 backend_->recent_redirects_.Put(url1, redirects); |
1142 | 1307 |
1143 const GURL icon_url("http://www.google.com/icon"); | 1308 const GURL icon_url1("http://www.google.com/icon"); |
1144 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); | 1309 const GURL icon_url2("http://www.google.com/icon2"); |
1145 // Add a favicon | 1310 |
1146 backend_->SetFavicon( | 1311 // Create mapping for a page with two favicons. |
1147 url1, icon_url, base::RefCountedBytes::TakeVector(&data), FAVICON); | 1312 IconURLSizesMap two_icon_url_sizes; |
1148 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 1313 two_icon_url_sizes[icon_url1] = GetSizesSmallAndLarge(); |
1149 url1, FAVICON, NULL)); | 1314 two_icon_url_sizes[icon_url2] = GetSizesSmallAndLarge(); |
1150 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 1315 |
1151 url2, FAVICON, NULL)); | 1316 // Create a mapping for a page with a single favicon. |
1152 | 1317 IconURLSizesMap one_icon_url_sizes; |
1153 // Add a touch_icon | 1318 one_icon_url_sizes[icon_url1] = GetSizesSmallAndLarge(); |
1154 backend_->SetFavicon( | 1319 |
1155 url1, icon_url, base::RefCountedBytes::TakeVector(&data), TOUCH_ICON); | 1320 std::vector<FaviconBitmapData> favicon_bitmap_data; |
1156 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 1321 |
1157 url1, TOUCH_ICON, NULL)); | 1322 // Add two favicons |
1158 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 1323 backend_->SetFavicons(url1, FAVICON, favicon_bitmap_data, |
1159 url2, TOUCH_ICON, NULL)); | 1324 two_icon_url_sizes); |
1160 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 1325 EXPECT_EQ(2u, NumIconMappingsForPageURL(url1, FAVICON)); |
1161 url1, FAVICON, NULL)); | 1326 EXPECT_EQ(2u, NumIconMappingsForPageURL(url2, FAVICON)); |
1162 | 1327 |
1163 // Add a TOUCH_PRECOMPOSED_ICON | 1328 // Add one touch_icon |
1164 backend_->SetFavicon(url1, | 1329 backend_->SetFavicons(url1, TOUCH_ICON, favicon_bitmap_data, |
1165 icon_url, | 1330 one_icon_url_sizes); |
1166 base::RefCountedBytes::TakeVector(&data), | 1331 EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, TOUCH_ICON)); |
1167 TOUCH_PRECOMPOSED_ICON); | 1332 EXPECT_EQ(1u, NumIconMappingsForPageURL(url2, TOUCH_ICON)); |
| 1333 EXPECT_EQ(2u, NumIconMappingsForPageURL(url1, FAVICON)); |
| 1334 |
| 1335 // Add one TOUCH_PRECOMPOSED_ICON |
| 1336 backend_->SetFavicons(url1, TOUCH_PRECOMPOSED_ICON, favicon_bitmap_data, |
| 1337 one_icon_url_sizes); |
1168 // The touch_icon was replaced. | 1338 // The touch_icon was replaced. |
1169 EXPECT_FALSE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 1339 EXPECT_EQ(0u, NumIconMappingsForPageURL(url1, TOUCH_ICON)); |
1170 url1, TOUCH_ICON, NULL)); | 1340 EXPECT_EQ(2u, NumIconMappingsForPageURL(url1, FAVICON)); |
1171 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 1341 EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, TOUCH_PRECOMPOSED_ICON)); |
1172 url1, FAVICON, NULL)); | 1342 EXPECT_EQ(1u, NumIconMappingsForPageURL(url2, TOUCH_PRECOMPOSED_ICON)); |
1173 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 1343 |
1174 url1, TOUCH_PRECOMPOSED_ICON, NULL)); | 1344 // Add a touch_icon. |
1175 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 1345 backend_->SetFavicons(url1, TOUCH_ICON, favicon_bitmap_data, |
1176 url2, TOUCH_PRECOMPOSED_ICON, NULL)); | 1346 one_icon_url_sizes); |
1177 | 1347 EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, TOUCH_ICON)); |
1178 // Add a touch_icon | 1348 EXPECT_EQ(2u, NumIconMappingsForPageURL(url1, FAVICON)); |
1179 backend_->SetFavicon( | |
1180 url1, icon_url, base::RefCountedBytes::TakeVector(&data), TOUCH_ICON); | |
1181 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | |
1182 url1, TOUCH_ICON, NULL)); | |
1183 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | |
1184 url1, FAVICON, NULL)); | |
1185 // The TOUCH_PRECOMPOSED_ICON was replaced. | 1349 // The TOUCH_PRECOMPOSED_ICON was replaced. |
1186 EXPECT_FALSE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 1350 EXPECT_EQ(0u, NumIconMappingsForPageURL(url1, TOUCH_PRECOMPOSED_ICON)); |
1187 url1, TOUCH_PRECOMPOSED_ICON, NULL)); | 1351 |
1188 | 1352 // Add a single favicon. |
1189 // Add a favicon | 1353 backend_->SetFavicons(url1, FAVICON, favicon_bitmap_data, |
1190 const GURL icon_url2("http://www.google.com/icon2"); | 1354 one_icon_url_sizes); |
1191 backend_->SetFavicon( | 1355 EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, TOUCH_ICON)); |
1192 url1, icon_url2, base::RefCountedBytes::TakeVector(&data), FAVICON); | 1356 EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, FAVICON)); |
1193 FaviconID icon_id = backend_->thumbnail_db_->GetFaviconIDForFaviconURL( | 1357 EXPECT_EQ(1u, NumIconMappingsForPageURL(url2, FAVICON)); |
1194 icon_url2, FAVICON, NULL); | 1358 |
1195 EXPECT_NE(0, icon_id); | 1359 // Add two favicons. |
1196 std::vector<IconMapping> icon_mapping; | 1360 backend_->SetFavicons(url1, FAVICON, favicon_bitmap_data, |
1197 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 1361 two_icon_url_sizes); |
1198 url1, &icon_mapping)); | 1362 EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, TOUCH_ICON)); |
1199 // The old icon was replaced. | 1363 EXPECT_EQ(2u, NumIconMappingsForPageURL(url1, FAVICON)); |
1200 EXPECT_TRUE(icon_mapping.size() > 1); | 1364 } |
1201 EXPECT_EQ(icon_id, icon_mapping[1].icon_id); | 1365 |
1202 } | 1366 // Test that there is no churn in icon mappings from calling |
1203 | 1367 // SetFavicons() twice with the same |icon_url_sizes| parameter. |
1204 TEST_F(HistoryBackendTest, AddOrUpdateIconMapping) { | 1368 TEST_F(HistoryBackendTest, SetFaviconMappingsForPageDuplicates) { |
1205 // Test the same icon and page mapping will not be added twice. other case | |
1206 // should be covered in TEST_F(HistoryBackendTest, SetFaviconMapping) | |
1207 const GURL url("http://www.google.com/"); | 1369 const GURL url("http://www.google.com/"); |
1208 const GURL icon_url("http://www.google.com/icon"); | 1370 const GURL icon_url("http://www.google.com/icon"); |
1209 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); | 1371 std::vector<FaviconBitmapData> favicon_bitmap_data; |
1210 | 1372 |
1211 backend_->SetFavicon( | 1373 IconURLSizesMap icon_url_sizes; |
1212 url, icon_url, base::RefCountedBytes::TakeVector(&data), FAVICON); | 1374 icon_url_sizes[icon_url] = GetSizesSmallAndLarge(); |
1213 FaviconID icon_id = backend_->thumbnail_db_->GetFaviconIDForFaviconURL( | 1375 |
| 1376 backend_->SetFavicons(url, FAVICON, favicon_bitmap_data, icon_url_sizes); |
| 1377 |
| 1378 std::vector<IconMapping> icon_mappings; |
| 1379 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( |
| 1380 url, FAVICON, &icon_mappings)); |
| 1381 EXPECT_EQ(1u, icon_mappings.size()); |
| 1382 IconMappingID mapping_id = icon_mappings[0].mapping_id; |
| 1383 |
| 1384 backend_->SetFavicons(url, FAVICON, favicon_bitmap_data, icon_url_sizes); |
| 1385 |
| 1386 icon_mappings.clear(); |
| 1387 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( |
| 1388 url, FAVICON, &icon_mappings)); |
| 1389 EXPECT_EQ(1u, icon_mappings.size()); |
| 1390 |
| 1391 // The same row in the icon_mapping table should be used for the mapping as |
| 1392 // before. |
| 1393 EXPECT_EQ(mapping_id, icon_mappings[0].mapping_id); |
| 1394 } |
| 1395 |
| 1396 // Test that setting favicons for a page which already has data does the |
| 1397 // right thing. |
| 1398 TEST_F(HistoryBackendTest, SetFavicons) { |
| 1399 const GURL page_url("http://www.google.com/"); |
| 1400 std::vector<FaviconBitmapData> favicon_bitmap_data; |
| 1401 IconURLSizesMap icon_url_sizes; |
| 1402 |
| 1403 // Set |page_url| as having two favicons each available from the web at two |
| 1404 // sizes. |
| 1405 const GURL icon_url1("http://www.google.com/icon1"); |
| 1406 const GURL icon_url2("http://www.google.com/icon2"); |
| 1407 |
| 1408 icon_url_sizes[icon_url1] = GetSizesSmallAndLarge(); |
| 1409 icon_url_sizes[icon_url2] = GetSizesSmallAndLarge(); |
| 1410 |
| 1411 // Set only sizes info for the favicons. |
| 1412 backend_->SetFavicons(page_url, FAVICON, favicon_bitmap_data, |
| 1413 icon_url_sizes); |
| 1414 |
| 1415 std::vector<IconMapping> icon_mappings; |
| 1416 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( |
| 1417 page_url, &icon_mappings)); |
| 1418 EXPECT_EQ(2u, icon_mappings.size()); |
| 1419 for (size_t i = 0; i < icon_mappings.size(); ++i) { |
| 1420 EXPECT_FALSE(backend_->thumbnail_db_->GetFaviconBitmaps( |
| 1421 icon_mappings[i].icon_id, NULL)); |
| 1422 } |
| 1423 |
| 1424 // Add bitmap data to the favicons. |
| 1425 GenerateFaviconBitmapData(icon_url1, |
| 1426 GetSizesSmall(), |
| 1427 icon_url2, |
| 1428 GetSizesSmallAndLarge(), |
| 1429 &favicon_bitmap_data); |
| 1430 |
| 1431 backend_->SetFavicons(page_url, FAVICON, favicon_bitmap_data, |
| 1432 icon_url_sizes); |
| 1433 |
| 1434 icon_mappings.clear(); |
| 1435 EXPECT_TRUE(GetSortedIconMappingsForPageURL(page_url, &icon_mappings)); |
| 1436 EXPECT_EQ(2u, icon_mappings.size()); |
| 1437 |
| 1438 GURL icon_url; |
| 1439 IconType icon_type; |
| 1440 FaviconSizes favicon_sizes; |
| 1441 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconHeader( |
| 1442 icon_mappings[0].icon_id, &icon_url, &icon_type, &favicon_sizes)); |
| 1443 EXPECT_EQ(icon_url1, icon_url); |
| 1444 EXPECT_EQ(FAVICON, icon_type); |
| 1445 EXPECT_EQ(GetSizesSmallAndLarge(), favicon_sizes); |
| 1446 |
| 1447 std::vector<FaviconBitmap> favicon_bitmaps; |
| 1448 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps( |
| 1449 icon_mappings[0].icon_id, &favicon_bitmaps)); |
| 1450 EXPECT_EQ(1u, favicon_bitmaps.size()); |
| 1451 EXPECT_TRUE(BitmapDataEqual('a', favicon_bitmaps[0].bitmap_data)); |
| 1452 EXPECT_EQ(kSmallSize, favicon_bitmaps[0].pixel_size); |
| 1453 |
| 1454 favicon_sizes.clear(); |
| 1455 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconHeader( |
| 1456 icon_mappings[1].icon_id, &icon_url, &icon_type, &favicon_sizes)); |
| 1457 EXPECT_EQ(icon_url2, icon_url); |
| 1458 EXPECT_EQ(FAVICON, icon_type); |
| 1459 EXPECT_EQ(GetSizesSmallAndLarge(), favicon_sizes); |
| 1460 |
| 1461 favicon_bitmaps.clear(); |
| 1462 EXPECT_TRUE(GetSortedFaviconBitmaps(icon_mappings[1].icon_id, |
| 1463 &favicon_bitmaps)); |
| 1464 |
| 1465 EXPECT_EQ(2u, favicon_bitmaps.size()); |
| 1466 EXPECT_EQ(kSmallSize, favicon_bitmaps[0].pixel_size); |
| 1467 EXPECT_TRUE(BitmapDataEqual('b', favicon_bitmaps[0].bitmap_data)); |
| 1468 EXPECT_EQ(kLargeSize, favicon_bitmaps[1].pixel_size); |
| 1469 EXPECT_TRUE(BitmapDataEqual('c', favicon_bitmaps[1].bitmap_data)); |
| 1470 |
| 1471 // Change the sizes for which the favicon at icon_url1 is available at from |
| 1472 // the web. Verify that all the data remains valid. |
| 1473 icon_url_sizes[icon_url1] = GetSizesTinySmallAndLarge(); |
| 1474 favicon_bitmap_data.clear(); |
| 1475 backend_->SetFavicons(page_url, FAVICON, favicon_bitmap_data, |
| 1476 icon_url_sizes); |
| 1477 |
| 1478 icon_mappings.clear(); |
| 1479 EXPECT_TRUE(GetSortedIconMappingsForPageURL(page_url, &icon_mappings)); |
| 1480 EXPECT_EQ(2u, icon_mappings.size()); |
| 1481 |
| 1482 favicon_sizes.clear(); |
| 1483 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconHeader( |
| 1484 icon_mappings[0].icon_id, &icon_url, &icon_type, &favicon_sizes)); |
| 1485 EXPECT_EQ(icon_url1, icon_url); |
| 1486 EXPECT_EQ(FAVICON, icon_type); |
| 1487 EXPECT_EQ(GetSizesTinySmallAndLarge(), favicon_sizes); |
| 1488 |
| 1489 favicon_bitmaps.clear(); |
| 1490 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps( |
| 1491 icon_mappings[0].icon_id, &favicon_bitmaps)); |
| 1492 EXPECT_EQ(1u, favicon_bitmaps.size()); |
| 1493 |
| 1494 favicon_sizes.clear(); |
| 1495 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconHeader( |
| 1496 icon_mappings[1].icon_id, &icon_url, &icon_type, &favicon_sizes)); |
| 1497 EXPECT_EQ(icon_url2, icon_url); |
| 1498 EXPECT_EQ(FAVICON, icon_type); |
| 1499 EXPECT_EQ(GetSizesSmallAndLarge(), favicon_sizes); |
| 1500 |
| 1501 favicon_bitmaps.clear(); |
| 1502 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps( |
| 1503 icon_mappings[1].icon_id, &favicon_bitmaps)); |
| 1504 EXPECT_EQ(2u, favicon_bitmaps.size()); |
| 1505 } |
| 1506 |
| 1507 // Test that changing the sizes that a favicon is available at from the web |
| 1508 // deletes stale favicons and favicon bitmaps. |
| 1509 TEST_F(HistoryBackendTest, SetFaviconsDeleteBitmaps) { |
| 1510 const GURL page_url("http://www.google.com/"); |
| 1511 const GURL icon_url("http://www.google.com/icon"); |
| 1512 |
| 1513 // Set |page_url| as having one favicon with two different sizes. |
| 1514 IconURLSizesMap icon_url_sizes; |
| 1515 icon_url_sizes[icon_url] = GetSizesSmallAndLarge(); |
| 1516 |
| 1517 std::vector<FaviconBitmapData> favicon_bitmap_data; |
| 1518 GenerateFaviconBitmapData(icon_url, GetSizesSmallAndLarge(), |
| 1519 &favicon_bitmap_data); |
| 1520 |
| 1521 // Add bitmap data and sizes information to the database. |
| 1522 backend_->SetFavicons(page_url, FAVICON, favicon_bitmap_data, |
| 1523 icon_url_sizes); |
| 1524 |
| 1525 FaviconID favicon_id = backend_->thumbnail_db_->GetFaviconIDForFaviconURL( |
1214 icon_url, FAVICON, NULL); | 1526 icon_url, FAVICON, NULL); |
1215 | 1527 EXPECT_NE(0, favicon_id); |
1216 // Add the same mapping | 1528 |
1217 FaviconID replaced; | 1529 std::vector<FaviconBitmap> favicon_bitmaps; |
1218 EXPECT_FALSE(backend_->AddOrUpdateIconMapping( | 1530 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(favicon_id, |
1219 url, icon_id, FAVICON, &replaced)); | 1531 &favicon_bitmaps)); |
1220 EXPECT_EQ(0, replaced); | 1532 EXPECT_EQ(2u, favicon_bitmaps.size()); |
1221 | 1533 |
1222 std::vector<IconMapping> icon_mapping; | 1534 // Change the bitmap sizes available from the web only to the small size only. |
1223 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 1535 icon_url_sizes[icon_url] = GetSizesSmall(); |
1224 url, &icon_mapping)); | 1536 favicon_bitmap_data.clear(); |
1225 EXPECT_EQ(1u, icon_mapping.size()); | 1537 backend_->SetFavicons(page_url, FAVICON, favicon_bitmap_data, |
1226 } | 1538 icon_url_sizes); |
1227 | 1539 |
1228 // Test that SetFavicon for a page which shares a FaviconID with another does | 1540 favicon_id = backend_->thumbnail_db_->GetFaviconIDForFaviconURL( |
1229 // the right thing. | 1541 icon_url, FAVICON, NULL); |
1230 TEST_F(HistoryBackendTest, SetSameFaviconURLForTwoPages) { | 1542 EXPECT_NE(0, favicon_id); |
1231 GURL favicon_url("http://www.google.com/favicon.ico"); | 1543 |
| 1544 favicon_bitmaps.clear(); |
| 1545 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(favicon_id, |
| 1546 &favicon_bitmaps)); |
| 1547 EXPECT_EQ(1u, favicon_bitmaps.size()); |
| 1548 EXPECT_EQ(kSmallSize, favicon_bitmaps[0].pixel_size); |
| 1549 |
| 1550 // Clear |icon_url_sizes|. The remaining favicon and its bitmap should both |
| 1551 // be deleted. |
| 1552 icon_url_sizes.clear(); |
| 1553 backend_->SetFavicons(page_url, FAVICON, favicon_bitmap_data, |
| 1554 icon_url_sizes); |
| 1555 EXPECT_EQ(0, backend_->thumbnail_db_->GetFaviconIDForFaviconURL( |
| 1556 icon_url, FAVICON, NULL)); |
| 1557 EXPECT_FALSE(backend_->thumbnail_db_->GetFaviconBitmaps(favicon_id, NULL)); |
| 1558 } |
| 1559 |
| 1560 // Test updating a single favicon bitmap's data via SetFavicons. |
| 1561 TEST_F(HistoryBackendTest, SetFaviconsReplaceBitmapData) { |
| 1562 |
| 1563 const GURL page_url("http://www.google.com/"); |
| 1564 const GURL icon_url("http://www.google.com/icon"); |
| 1565 IconURLSizesMap icon_url_sizes; |
| 1566 icon_url_sizes[icon_url] = GetSizesSmall(); |
| 1567 |
| 1568 std::vector<unsigned char> data_initial; |
| 1569 data_initial.push_back('a'); |
| 1570 |
| 1571 FaviconBitmapData bitmap_data_element; |
| 1572 bitmap_data_element.bitmap_data = |
| 1573 base::RefCountedBytes::TakeVector(&data_initial); |
| 1574 bitmap_data_element.pixel_size = kSmallSize; |
| 1575 bitmap_data_element.icon_url = icon_url; |
| 1576 std::vector<FaviconBitmapData> favicon_bitmap_data; |
| 1577 favicon_bitmap_data.push_back(bitmap_data_element); |
| 1578 |
| 1579 // Add bitmap to the database. |
| 1580 backend_->SetFavicons(page_url, FAVICON, favicon_bitmap_data, |
| 1581 icon_url_sizes); |
| 1582 |
| 1583 FaviconBitmap original_favicon_bitmap; |
| 1584 EXPECT_TRUE(GetOnlyFaviconBitmapForIconURL(icon_url, FAVICON, |
| 1585 &original_favicon_bitmap)); |
| 1586 EXPECT_TRUE(BitmapDataEqual('a', original_favicon_bitmap.bitmap_data)); |
| 1587 |
| 1588 // SetFavicons with identical data but a different bitmap. |
| 1589 std::vector<unsigned char> updated_data; |
| 1590 updated_data.push_back('b'); |
| 1591 favicon_bitmap_data[0].bitmap_data = |
| 1592 base::RefCountedBytes::TakeVector(&updated_data); |
| 1593 backend_->SetFavicons(page_url, FAVICON, favicon_bitmap_data, |
| 1594 icon_url_sizes); |
| 1595 |
| 1596 FaviconBitmap updated_favicon_bitmap; |
| 1597 EXPECT_TRUE(GetOnlyFaviconBitmapForIconURL(icon_url, FAVICON, |
| 1598 &updated_favicon_bitmap)); |
| 1599 EXPECT_TRUE(BitmapDataEqual('b', updated_favicon_bitmap.bitmap_data)); |
| 1600 |
| 1601 // There should be no churn in FaviconIDs or FaviconBitmapIds. |
| 1602 EXPECT_EQ(original_favicon_bitmap.icon_id, updated_favicon_bitmap.icon_id); |
| 1603 EXPECT_EQ(original_favicon_bitmap.bitmap_id, |
| 1604 updated_favicon_bitmap.bitmap_id); |
| 1605 } |
| 1606 |
| 1607 // Test that if two pages share the same FaviconID, changing the favicon for |
| 1608 // one page does not affect the other. |
| 1609 TEST_F(HistoryBackendTest, SetFaviconsSameFaviconURLForTwoPages) { |
| 1610 GURL icon_url("http://www.google.com/favicon.ico"); |
| 1611 GURL icon_url_new("http://www.google.com/favicon2.ico"); |
1232 GURL page_url1("http://www.google.com"); | 1612 GURL page_url1("http://www.google.com"); |
1233 GURL page_url2("http://www.google.ca"); | 1613 GURL page_url2("http://www.google.ca"); |
1234 | 1614 |
1235 scoped_refptr<base::RefCountedMemory> bitmap_data( | 1615 IconURLSizesMap icon_url_sizes; |
1236 new base::RefCountedBytes()); | 1616 icon_url_sizes[icon_url] = GetSizesSmall(); |
1237 backend_->SetFavicon(page_url1, favicon_url, bitmap_data, FAVICON); | 1617 |
| 1618 std::vector<FaviconBitmapData> favicon_bitmap_data; |
| 1619 GenerateFaviconBitmapData(icon_url, GetSizesSmallAndLarge(), |
| 1620 &favicon_bitmap_data); |
| 1621 |
| 1622 backend_->SetFavicons(page_url1, FAVICON, favicon_bitmap_data, |
| 1623 icon_url_sizes); |
| 1624 |
| 1625 std::vector<GURL> icon_urls; |
| 1626 icon_urls.push_back(icon_url); |
1238 | 1627 |
1239 scoped_refptr<GetFaviconRequest> request(new GetFaviconRequest( | 1628 scoped_refptr<GetFaviconRequest> request(new GetFaviconRequest( |
1240 base::Bind(&HistoryBackendTest::OnFaviconResults, | 1629 base::Bind(&HistoryBackendTest::OnFaviconResults, |
1241 base::Unretained(this)))); | 1630 base::Unretained(this)))); |
1242 HistoryBackendCancelableRequest cancellable_request; | 1631 HistoryBackendCancelableRequest cancellable_request; |
1243 cancellable_request.MockScheduleOfRequest<GetFaviconRequest>(request); | 1632 cancellable_request.MockScheduleOfRequest<GetFaviconRequest>(request); |
1244 backend_->UpdateFaviconMappingAndFetch(request, page_url2, favicon_url, | 1633 backend_->UpdateFaviconMappingsAndFetch(request, page_url2, icon_urls, |
1245 FAVICON); | 1634 FAVICON, kSmallSize.width(), GetScaleFactors1x2x()); |
1246 | 1635 |
1247 // Check that the same FaviconID is mapped to both page URLs. | 1636 // Check that the same FaviconID is mapped to both page URLs. |
1248 std::vector<IconMapping> icon_mappings1; | 1637 std::vector<IconMapping> icon_mappings; |
1249 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 1638 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( |
1250 page_url1, &icon_mappings1)); | 1639 page_url1, &icon_mappings)); |
1251 EXPECT_EQ(1u, icon_mappings1.size()); | 1640 EXPECT_EQ(1u, icon_mappings.size()); |
1252 FaviconID favicon_id = icon_mappings1[0].icon_id; | 1641 FaviconID favicon_id = icon_mappings[0].icon_id; |
1253 EXPECT_NE(0, favicon_id); | 1642 EXPECT_NE(0, favicon_id); |
1254 | 1643 |
1255 std::vector<IconMapping> icon_mappings2; | 1644 icon_mappings.clear(); |
1256 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 1645 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( |
1257 page_url2, &icon_mappings2)); | 1646 page_url2, &icon_mappings)); |
1258 EXPECT_EQ(1u, icon_mappings2.size()); | 1647 EXPECT_EQ(1u, icon_mappings.size()); |
1259 EXPECT_EQ(favicon_id, icon_mappings2[0].icon_id); | 1648 EXPECT_EQ(favicon_id, icon_mappings[0].icon_id); |
1260 | 1649 |
1261 // Update the bitmap data. | 1650 // Change the icon URL that |page_url1| is mapped to. |
1262 backend_->SetFavicon(page_url1, favicon_url, bitmap_data, FAVICON); | 1651 icon_url_sizes.clear(); |
1263 | 1652 icon_url_sizes[icon_url_new] = GetSizesSmall(); |
1264 // |page_url1| and |page_url2| should still map to the same FaviconID | 1653 GenerateFaviconBitmapData(icon_url_new, GetSizesSmall(), |
1265 // and have valid bitmap data. | 1654 &favicon_bitmap_data); |
1266 icon_mappings1.clear(); | 1655 backend_->SetFavicons(page_url1, FAVICON, favicon_bitmap_data, |
| 1656 icon_url_sizes); |
| 1657 |
| 1658 // |page_url1| should map to a new FaviconID and have valid bitmap data. |
| 1659 icon_mappings.clear(); |
1267 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 1660 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( |
1268 page_url1, &icon_mappings1)); | 1661 page_url1, &icon_mappings)); |
1269 EXPECT_EQ(1u, icon_mappings1.size()); | 1662 EXPECT_EQ(1u, icon_mappings.size()); |
1270 EXPECT_EQ(favicon_id, icon_mappings1[0].icon_id); | 1663 EXPECT_EQ(icon_url_new, icon_mappings[0].icon_url); |
1271 | 1664 EXPECT_NE(favicon_id, icon_mappings[0].icon_id); |
1272 icon_mappings2.clear(); | 1665 |
| 1666 std::vector<FaviconBitmap> favicon_bitmaps; |
| 1667 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps( |
| 1668 icon_mappings[0].icon_id, &favicon_bitmaps)); |
| 1669 EXPECT_EQ(1u, favicon_bitmaps.size()); |
| 1670 |
| 1671 // |page_url2| should still map to the same FaviconID and have valid bitmap |
| 1672 // data. |
| 1673 icon_mappings.clear(); |
1273 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 1674 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( |
1274 page_url2, &icon_mappings2)); | 1675 page_url2, &icon_mappings)); |
1275 EXPECT_EQ(1u, icon_mappings2.size()); | 1676 EXPECT_EQ(1u, icon_mappings.size()); |
1276 EXPECT_EQ(favicon_id, icon_mappings2[0].icon_id); | 1677 EXPECT_EQ(favicon_id, icon_mappings[0].icon_id); |
1277 | 1678 |
1278 std::vector<FaviconBitmap> favicon_bitmaps; | 1679 favicon_bitmaps.clear(); |
1279 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(favicon_id, | 1680 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(favicon_id, |
1280 &favicon_bitmaps)); | 1681 &favicon_bitmaps)); |
1281 EXPECT_EQ(1u, favicon_bitmaps.size()); | 1682 EXPECT_EQ(2u, favicon_bitmaps.size()); |
1282 } | 1683 } |
1283 | 1684 |
1284 TEST_F(HistoryBackendTest, GetFaviconForURL) { | 1685 // Tests when a merge is valid. |
1285 // This test will add a fav icon and touch icon for the same URL | 1686 TEST_F(HistoryBackendTest, IsMergeValid) { |
1286 // and check the behaviour of backend's GetFaviconForURL implementation. | 1687 GURL page_url("http://www.google.com"); |
| 1688 std::vector<FaviconBitmapData> favicon_bitmap_data; |
| 1689 |
| 1690 GURL icon_url("http://www.google.com/favicon.ico"); |
| 1691 const gfx::Size& pixel_size = kLargeSize; |
| 1692 GURL different_icon_url("http://www.google.com/favicon2.ico"); |
| 1693 |
| 1694 // A merge is invalid if icon URLs are already mapped to |page_url| but |
| 1695 // |icon_url| is not one of them. |
| 1696 IconURLSizesMap icon_url_sizes; |
| 1697 icon_url_sizes[different_icon_url] = GetSizesSmallAndLarge(); |
| 1698 backend_->SetFavicons(page_url, FAVICON, favicon_bitmap_data, icon_url_sizes); |
| 1699 |
| 1700 gfx::Size merge_pixel_size; |
| 1701 EXPECT_FALSE(backend_->IsMergeValid(page_url, icon_url, FAVICON, pixel_size, |
| 1702 &merge_pixel_size)); |
| 1703 |
| 1704 // A merge is invalid if the database knows |icon_url| but |pixel_size| is |
| 1705 // not part of the known favicon sizes. |
| 1706 icon_url_sizes.clear(); |
| 1707 icon_url_sizes[icon_url] = GetSizesSmall(); |
| 1708 backend_->SetFavicons(page_url, FAVICON, favicon_bitmap_data, icon_url_sizes); |
| 1709 |
| 1710 EXPECT_FALSE(backend_->IsMergeValid(page_url, icon_url, FAVICON, pixel_size, |
| 1711 &merge_pixel_size)); |
| 1712 |
| 1713 // A merge is valid when |page_url| is not mapped to any favicons and |
| 1714 // |icon_url| is not known by the database. |
| 1715 icon_url_sizes.clear(); |
| 1716 backend_->SetFavicons(page_url, FAVICON, favicon_bitmap_data, icon_url_sizes); |
| 1717 |
| 1718 EXPECT_TRUE(backend_->IsMergeValid(page_url, icon_url, FAVICON, pixel_size, |
| 1719 &merge_pixel_size)); |
| 1720 |
| 1721 // A merge is valid when |icon_url| is known by the database and |pixel_size| |
| 1722 // is part of the known favicon sizes. |
| 1723 icon_url_sizes[icon_url] = GetSizesSmallAndLarge(); |
| 1724 backend_->SetFavicons(page_url, FAVICON, favicon_bitmap_data, icon_url_sizes); |
| 1725 |
| 1726 EXPECT_TRUE(backend_->IsMergeValid(page_url, icon_url, FAVICON, pixel_size, |
| 1727 &merge_pixel_size)); |
| 1728 EXPECT_EQ(kLargeSize, merge_pixel_size); |
| 1729 |
| 1730 // A merge is valid when |icon_url| is known by the database and the favicon |
| 1731 // sizes for the favicon are not known. |
| 1732 icon_url_sizes[icon_url] = GetDefaultFaviconSizes(); |
| 1733 backend_->SetFavicons(page_url, FAVICON, favicon_bitmap_data, icon_url_sizes); |
| 1734 |
| 1735 EXPECT_TRUE(backend_->IsMergeValid(page_url, icon_url, FAVICON, pixel_size, |
| 1736 &merge_pixel_size)); |
| 1737 EXPECT_EQ(gfx::Size(), merge_pixel_size); |
| 1738 } |
| 1739 |
| 1740 // Tests calling MergeFavicon() repeatedly when there are initially no mappings |
| 1741 // for |page_url| in the database. |
| 1742 TEST_F(HistoryBackendTest, MergeFaviconPageURLNotInDB) { |
| 1743 GURL page_url("http://www.google.com"); |
| 1744 GURL icon_url("http://www.google.com/favicon.ico"); |
| 1745 |
| 1746 std::vector<unsigned char> bitmap_data1; |
| 1747 bitmap_data1.push_back('a'); |
| 1748 backend_->MergeFavicon(page_url, icon_url, FAVICON, |
| 1749 base::RefCountedBytes::TakeVector(&bitmap_data1), kSmallSize); |
| 1750 |
| 1751 std::vector<IconMapping> icon_mappings; |
| 1752 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( |
| 1753 page_url, &icon_mappings)); |
| 1754 EXPECT_EQ(1u, icon_mappings.size()); |
| 1755 |
| 1756 GURL icon_url_out; |
| 1757 IconType icon_type_out; |
| 1758 FaviconSizes favicon_sizes_out; |
| 1759 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconHeader( |
| 1760 icon_mappings[0].icon_id, &icon_url_out, &icon_type_out, |
| 1761 &favicon_sizes_out)); |
| 1762 EXPECT_EQ(icon_url, icon_url_out); |
| 1763 EXPECT_EQ(FAVICON, icon_type_out); |
| 1764 // The favicon sizes should be default as the real favicon sizes are unknown. |
| 1765 EXPECT_EQ(GetDefaultFaviconSizes(), favicon_sizes_out); |
| 1766 |
| 1767 FaviconBitmap favicon_bitmap; |
| 1768 EXPECT_TRUE(GetOnlyFaviconBitmapForIconURL(icon_url_out, FAVICON, |
| 1769 &favicon_bitmap)); |
| 1770 EXPECT_TRUE(BitmapDataEqual('a', favicon_bitmap.bitmap_data)); |
| 1771 EXPECT_EQ(gfx::Size(), favicon_bitmap.pixel_size); |
| 1772 |
| 1773 // Changing the bitmap data via merge favicons should work. |
| 1774 std::vector<unsigned char> bitmap_data2; |
| 1775 bitmap_data2.push_back('b'); |
| 1776 backend_->MergeFavicon(page_url, icon_url, FAVICON, |
| 1777 base::RefCountedBytes::TakeVector(&bitmap_data2), kSmallSize); |
| 1778 |
| 1779 // The favicon should still be valid and the bitmap data should have changed. |
| 1780 favicon_sizes_out.clear(); |
| 1781 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconHeader( |
| 1782 icon_mappings[0].icon_id, &icon_url_out, &icon_type_out, |
| 1783 &favicon_sizes_out)); |
| 1784 EXPECT_EQ(icon_url, icon_url_out); |
| 1785 EXPECT_EQ(FAVICON, icon_type_out); |
| 1786 EXPECT_EQ(GetDefaultFaviconSizes(), favicon_sizes_out); |
| 1787 |
| 1788 EXPECT_TRUE(GetOnlyFaviconBitmapForIconURL(icon_url_out, FAVICON, |
| 1789 &favicon_bitmap)); |
| 1790 EXPECT_TRUE(BitmapDataEqual('b', favicon_bitmap.bitmap_data)); |
| 1791 EXPECT_EQ(gfx::Size(), favicon_bitmap.pixel_size); |
| 1792 } |
| 1793 |
| 1794 // Test that MergeFavicon() adds the passed in favicon bitmaps to the database |
| 1795 // for a |page_url| with preexisting mappings only when it merges cleanly. |
| 1796 TEST_F(HistoryBackendTest, MergeFaviconPageURLInDB) { |
| 1797 GURL page_url("http://www.google.com"); |
| 1798 GURL icon_url1("http://www.google.com/favicon1.ico"); |
| 1799 GURL icon_url2("http://www.google.com/favicon2.ico"); |
| 1800 GURL icon_url3("http://www.google.com/favicon3.ico"); |
| 1801 |
| 1802 std::vector<unsigned char> data; |
| 1803 data.push_back('c'); |
| 1804 scoped_refptr<base::RefCountedBytes> bitmap_data( |
| 1805 base::RefCountedBytes::TakeVector(&data)); |
| 1806 |
| 1807 // Set and test initial state. |
| 1808 IconURLSizesMap icon_url_sizes; |
| 1809 icon_url_sizes[icon_url1] = GetSizesSmallAndLarge(); |
| 1810 icon_url_sizes[icon_url2] = GetSizesSmall(); |
| 1811 |
| 1812 std::vector<FaviconBitmapData> favicon_bitmap_data; |
| 1813 GenerateFaviconBitmapData(icon_url1, GetSizesSmall(), |
| 1814 &favicon_bitmap_data); |
| 1815 backend_->SetFavicons(page_url, FAVICON, favicon_bitmap_data, |
| 1816 icon_url_sizes); |
| 1817 |
| 1818 std::vector<IconMapping> icon_mappings; |
| 1819 EXPECT_TRUE(GetSortedIconMappingsForPageURL(page_url, &icon_mappings)); |
| 1820 EXPECT_EQ(2u, icon_mappings.size()); |
| 1821 EXPECT_EQ(icon_url1, icon_mappings[0].icon_url); |
| 1822 EXPECT_EQ(icon_url2, icon_mappings[1].icon_url); |
| 1823 |
| 1824 FaviconBitmap favicon_bitmap; |
| 1825 EXPECT_TRUE(GetOnlyFaviconBitmapForIconURL(icon_mappings[0].icon_url, |
| 1826 FAVICON, &favicon_bitmap)); |
| 1827 EXPECT_TRUE(BitmapDataEqual('a', favicon_bitmap.bitmap_data)); |
| 1828 EXPECT_EQ(kSmallSize, favicon_bitmap.pixel_size); |
| 1829 |
| 1830 EXPECT_FALSE(backend_->thumbnail_db_->GetFaviconBitmaps( |
| 1831 icon_mappings[1].icon_id, NULL)); |
| 1832 |
| 1833 // Test that invalid merge does not change the favicon bitmaps mapped to |
| 1834 // |page_url|. |
| 1835 backend_->MergeFavicon(page_url, icon_url3, FAVICON, bitmap_data, |
| 1836 kLargeSize); |
| 1837 |
| 1838 icon_mappings.clear(); |
| 1839 EXPECT_TRUE(GetSortedIconMappingsForPageURL(page_url, &icon_mappings)); |
| 1840 EXPECT_EQ(2u, icon_mappings.size()); |
| 1841 EXPECT_EQ(icon_url1, icon_mappings[0].icon_url); |
| 1842 EXPECT_EQ(icon_url2, icon_mappings[1].icon_url); |
| 1843 |
| 1844 EXPECT_TRUE(GetOnlyFaviconBitmapForIconURL(icon_mappings[0].icon_url, |
| 1845 FAVICON, &favicon_bitmap)); |
| 1846 EXPECT_TRUE(BitmapDataEqual('a', favicon_bitmap.bitmap_data)); |
| 1847 EXPECT_EQ(kSmallSize, favicon_bitmap.pixel_size); |
| 1848 |
| 1849 EXPECT_FALSE(backend_->thumbnail_db_->GetFaviconBitmaps( |
| 1850 icon_mappings[1].icon_id, NULL)); |
| 1851 |
| 1852 // Test that a valid merge updates the favicon bitmaps. |
| 1853 backend_->MergeFavicon(page_url, icon_url1, FAVICON, bitmap_data, |
| 1854 kLargeSize); |
| 1855 |
| 1856 icon_mappings.clear(); |
| 1857 EXPECT_TRUE(GetSortedIconMappingsForPageURL(page_url, &icon_mappings)); |
| 1858 EXPECT_EQ(2u, icon_mappings.size()); |
| 1859 EXPECT_EQ(icon_url1, icon_mappings[0].icon_url); |
| 1860 EXPECT_EQ(icon_url2, icon_mappings[1].icon_url); |
| 1861 |
| 1862 std::vector<FaviconBitmap> favicon_bitmaps; |
| 1863 EXPECT_TRUE(GetSortedFaviconBitmaps(icon_mappings[0].icon_id, |
| 1864 &favicon_bitmaps)); |
| 1865 EXPECT_TRUE(BitmapDataEqual('a', favicon_bitmaps[0].bitmap_data)); |
| 1866 EXPECT_EQ(kSmallSize, favicon_bitmaps[0].pixel_size); |
| 1867 EXPECT_TRUE(BitmapDataEqual('c', favicon_bitmaps[1].bitmap_data)); |
| 1868 EXPECT_EQ(kLargeSize, favicon_bitmaps[1].pixel_size); |
| 1869 |
| 1870 EXPECT_FALSE(backend_->thumbnail_db_->GetFaviconBitmaps( |
| 1871 icon_mappings[1].icon_id, NULL)); |
| 1872 } |
| 1873 |
| 1874 // Test UpdateFaviconMapingsAndFetch() when multiple icon types are passed in. |
| 1875 TEST_F(HistoryBackendTest, UpdateFaviconMappingsAndFetchMultipleIconTypes) { |
| 1876 GURL page_url1("http://www.google.com"); |
| 1877 GURL page_url2("http://news.google.com"); |
| 1878 GURL page_url3("http://mail.google.com"); |
| 1879 GURL icon_urla("http://www.google.com/favicon1.ico"); |
| 1880 GURL icon_urlb("http://www.google.com/favicon2.ico"); |
| 1881 GURL icon_urlc("http://www.google.com/favicon3.ico"); |
| 1882 |
| 1883 // |page_url1| is mapped to |icon_urla| which if of type TOUCH_ICON. |
| 1884 IconURLSizesMap icon_url_sizes; |
| 1885 icon_url_sizes[icon_urla] = GetSizesSmall(); |
| 1886 |
| 1887 std::vector<FaviconBitmapData> favicon_bitmap_data; |
| 1888 GenerateFaviconBitmapData(icon_urla, GetSizesSmall(), &favicon_bitmap_data); |
| 1889 backend_->SetFavicons(page_url1, TOUCH_ICON, favicon_bitmap_data, |
| 1890 icon_url_sizes); |
| 1891 |
| 1892 // |page_url2| is mapped to |icon_urlb| and |icon_urlc| which is of type |
| 1893 // TOUCH_PRECOMPOSED_ICON. |
| 1894 icon_url_sizes.clear(); |
| 1895 icon_url_sizes[icon_urlb] = GetSizesSmall(); |
| 1896 icon_url_sizes[icon_urlc] = GetSizesSmall(); |
| 1897 GenerateFaviconBitmapData(icon_urlb, GetSizesSmall(), icon_urlc, |
| 1898 GetSizesSmall(), &favicon_bitmap_data); |
| 1899 backend_->SetFavicons(page_url2, TOUCH_PRECOMPOSED_ICON, favicon_bitmap_data, |
| 1900 icon_url_sizes); |
| 1901 |
| 1902 std::vector<GURL> icon_urls; |
| 1903 icon_urls.push_back(icon_urla); |
| 1904 icon_urls.push_back(icon_urlb); |
| 1905 icon_urls.push_back(icon_urlc); |
| 1906 scoped_refptr<GetFaviconRequest> request(new GetFaviconRequest( |
| 1907 base::Bind(&HistoryBackendTest::OnFaviconResults, |
| 1908 base::Unretained(this)))); |
| 1909 HistoryBackendCancelableRequest cancellable_request; |
| 1910 cancellable_request.MockScheduleOfRequest<GetFaviconRequest>(request); |
| 1911 backend_->UpdateFaviconMappingsAndFetch(request, page_url3, icon_urls, |
| 1912 TOUCH_ICON | TOUCH_PRECOMPOSED_ICON, kSmallSize.width(), |
| 1913 GetScaleFactors1x2x()); |
| 1914 |
| 1915 // |page_url1| and |page_url2| should still be mapped to the same icon URLs. |
| 1916 std::vector<IconMapping> icon_mappings; |
| 1917 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url1, |
| 1918 &icon_mappings)); |
| 1919 EXPECT_EQ(1u, icon_mappings.size()); |
| 1920 EXPECT_EQ(icon_urla, icon_mappings[0].icon_url); |
| 1921 EXPECT_EQ(TOUCH_ICON, icon_mappings[0].icon_type); |
| 1922 |
| 1923 icon_mappings.clear(); |
| 1924 EXPECT_TRUE(GetSortedIconMappingsForPageURL(page_url2, &icon_mappings)); |
| 1925 EXPECT_EQ(2u, icon_mappings.size()); |
| 1926 EXPECT_EQ(icon_urlb, icon_mappings[0].icon_url); |
| 1927 EXPECT_EQ(TOUCH_PRECOMPOSED_ICON, icon_mappings[0].icon_type); |
| 1928 EXPECT_EQ(icon_urlc, icon_mappings[1].icon_url); |
| 1929 EXPECT_EQ(TOUCH_PRECOMPOSED_ICON, icon_mappings[1].icon_type); |
| 1930 |
| 1931 // |page_url3| should be mapped only to |icon_urlb| and |icon_urlc| as |
| 1932 // TOUCH_PRECOMPOSED_ICON is the largest IconType. |
| 1933 icon_mappings.clear(); |
| 1934 EXPECT_TRUE(GetSortedIconMappingsForPageURL(page_url3, &icon_mappings)); |
| 1935 EXPECT_EQ(2u, icon_mappings.size()); |
| 1936 EXPECT_EQ(icon_urlb, icon_mappings[0].icon_url); |
| 1937 EXPECT_EQ(TOUCH_PRECOMPOSED_ICON, icon_mappings[0].icon_type); |
| 1938 EXPECT_EQ(icon_urlc, icon_mappings[1].icon_url); |
| 1939 EXPECT_EQ(TOUCH_PRECOMPOSED_ICON, icon_mappings[1].icon_type); |
| 1940 } |
| 1941 |
| 1942 // Test the results of GetFaviconsFromDB() when there are no found |
| 1943 // favicons. |
| 1944 TEST_F(HistoryBackendTest, GetFaviconsFromDBEmpty) { |
| 1945 const GURL page_url("http://www.google.com/"); |
| 1946 |
| 1947 std::vector<FaviconBitmapResult> bitmap_results; |
| 1948 IconURLSizesMap icon_url_sizes; |
| 1949 EXPECT_FALSE(backend_->GetFaviconsFromDB(page_url, FAVICON, |
| 1950 kSmallSize.width(), GetScaleFactors1x2x(), &bitmap_results, |
| 1951 &icon_url_sizes)); |
| 1952 EXPECT_TRUE(bitmap_results.empty()); |
| 1953 EXPECT_TRUE(icon_url_sizes.empty()); |
| 1954 } |
| 1955 |
| 1956 // Test that GetFaviconsFromDB() returns results for the bitmaps which most |
| 1957 // closely match the passed in desired size and scale factors. |
| 1958 TEST_F(HistoryBackendTest, GetFaviconsFromDBSelectClosestMatch) { |
| 1959 const GURL page_url("http://www.google.com/"); |
| 1960 |
| 1961 const GURL icon_url("http://www.google.com/icon1"); |
| 1962 IconURLSizesMap icon_url_sizes; |
| 1963 icon_url_sizes[icon_url] = GetSizesTinySmallAndLarge(); |
| 1964 |
| 1965 std::vector<FaviconBitmapData> favicon_bitmap_data; |
| 1966 GenerateFaviconBitmapData(icon_url, GetSizesTinySmallAndLarge(), |
| 1967 &favicon_bitmap_data); |
| 1968 |
| 1969 backend_->SetFavicons(page_url, FAVICON, favicon_bitmap_data, |
| 1970 icon_url_sizes); |
| 1971 |
| 1972 std::vector<FaviconBitmapResult> bitmap_results_out; |
| 1973 IconURLSizesMap icon_url_sizes_out; |
| 1974 EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url, FAVICON, kSmallSize.width(), |
| 1975 GetScaleFactors1x2x(), &bitmap_results_out, &icon_url_sizes_out)); |
| 1976 |
| 1977 // The bitmap data for the 1x and 2x bitmaps should be returned as their sizes |
| 1978 // match exactly. |
| 1979 |
| 1980 EXPECT_EQ(2u, bitmap_results_out.size()); |
| 1981 // No required order for results. |
| 1982 if (bitmap_results_out[0].pixel_size == kLargeSize) { |
| 1983 FaviconBitmapResult tmp_result = bitmap_results_out[0]; |
| 1984 bitmap_results_out[0] = bitmap_results_out[1]; |
| 1985 bitmap_results_out[1] = tmp_result; |
| 1986 } |
| 1987 |
| 1988 EXPECT_FALSE(bitmap_results_out[0].expired); |
| 1989 EXPECT_TRUE(BitmapDataEqual('b', bitmap_results_out[0].bitmap_data)); |
| 1990 EXPECT_EQ(kSmallSize, bitmap_results_out[0].pixel_size); |
| 1991 EXPECT_EQ(icon_url, bitmap_results_out[0].icon_url); |
| 1992 EXPECT_EQ(FAVICON, bitmap_results_out[0].icon_type); |
| 1993 |
| 1994 EXPECT_FALSE(bitmap_results_out[1].expired); |
| 1995 EXPECT_TRUE(BitmapDataEqual('c', bitmap_results_out[1].bitmap_data)); |
| 1996 EXPECT_EQ(kLargeSize, bitmap_results_out[1].pixel_size); |
| 1997 EXPECT_EQ(icon_url, bitmap_results_out[1].icon_url); |
| 1998 EXPECT_EQ(FAVICON, bitmap_results_out[1].icon_type); |
| 1999 |
| 2000 EXPECT_EQ(icon_url_sizes, icon_url_sizes_out); |
| 2001 } |
| 2002 |
| 2003 // Test that GetFaviconsFromDB() returns results from the icon URL whose |
| 2004 // bitmaps most closely match the passed in desired size and scale factors. |
| 2005 TEST_F(HistoryBackendTest, GetFaviconsFromDBSingleIconURL) { |
| 2006 const GURL page_url("http://www.google.com/"); |
| 2007 |
| 2008 const GURL icon_url1("http://www.google.com/icon1"); |
| 2009 const GURL icon_url2("http://www.google.com/icon2"); |
| 2010 IconURLSizesMap icon_url_sizes; |
| 2011 icon_url_sizes[icon_url1] = GetSizesSmall(); |
| 2012 icon_url_sizes[icon_url2] = GetSizesSmallAndLarge(); |
| 2013 |
| 2014 std::vector<FaviconBitmapData> favicon_bitmap_data; |
| 2015 GenerateFaviconBitmapData(icon_url1, GetSizesSmall(), icon_url2, |
| 2016 GetSizesLarge(), &favicon_bitmap_data); |
| 2017 |
| 2018 backend_->SetFavicons(page_url, FAVICON, favicon_bitmap_data, |
| 2019 icon_url_sizes); |
| 2020 |
| 2021 std::vector<FaviconBitmapResult> bitmap_results_out; |
| 2022 IconURLSizesMap icon_url_sizes_out; |
| 2023 EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url, FAVICON, kSmallSize.width(), |
| 2024 GetScaleFactors1x2x(), &bitmap_results_out, &icon_url_sizes_out)); |
| 2025 |
| 2026 // The results should have the large bitmap as downscaling is preferred to |
| 2027 // upscaling. |
| 2028 EXPECT_EQ(1u, bitmap_results_out.size()); |
| 2029 EXPECT_EQ(kSmallSize, bitmap_results_out[0].pixel_size); |
| 2030 EXPECT_EQ(icon_url1, bitmap_results_out[0].icon_url); |
| 2031 |
| 2032 EXPECT_EQ(icon_url_sizes, icon_url_sizes_out); |
| 2033 } |
| 2034 |
| 2035 // Test the results of GetFaviconsFromDB() when called with different |
| 2036 // |icon_types|. |
| 2037 TEST_F(HistoryBackendTest, GetFaviconsFromDBIconType) { |
| 2038 const GURL page_url("http://www.google.com/"); |
| 2039 const GURL icon_url1("http://www.google.com/icon1.png"); |
| 2040 const GURL icon_url2("http://www.google.com/icon2.png"); |
| 2041 |
| 2042 IconURLSizesMap icon_url_sizes; |
| 2043 icon_url_sizes[icon_url1] = GetSizesSmall(); |
| 2044 icon_url_sizes[icon_url2] = GetSizesSmall(); |
| 2045 |
| 2046 std::vector<FaviconBitmapData> favicon_bitmap_data; |
| 2047 GenerateFaviconBitmapData(icon_url1, GetSizesSmall(), &favicon_bitmap_data); |
| 2048 backend_->SetFavicons(page_url, FAVICON, favicon_bitmap_data, |
| 2049 icon_url_sizes); |
| 2050 |
| 2051 GenerateFaviconBitmapData(icon_url2, GetSizesSmall(), &favicon_bitmap_data); |
| 2052 backend_->SetFavicons(page_url, TOUCH_ICON, favicon_bitmap_data, |
| 2053 icon_url_sizes); |
| 2054 |
| 2055 std::vector<FaviconBitmapResult> bitmap_results_out; |
| 2056 IconURLSizesMap icon_url_sizes_out; |
| 2057 EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url, FAVICON, |
| 2058 kSmallSize.width(), GetScaleFactors1x2x(), &bitmap_results_out, |
| 2059 &icon_url_sizes_out)); |
| 2060 |
| 2061 EXPECT_EQ(1u, bitmap_results_out.size()); |
| 2062 EXPECT_EQ(FAVICON, bitmap_results_out[0].icon_type); |
| 2063 EXPECT_EQ(icon_url_sizes, icon_url_sizes_out); |
| 2064 |
| 2065 bitmap_results_out.clear(); |
| 2066 icon_url_sizes_out.clear(); |
| 2067 EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url, TOUCH_ICON, |
| 2068 kSmallSize.width(), GetScaleFactors1x2x(), &bitmap_results_out, |
| 2069 &icon_url_sizes_out)); |
| 2070 |
| 2071 EXPECT_EQ(1u, bitmap_results_out.size()); |
| 2072 EXPECT_EQ(TOUCH_ICON, bitmap_results_out[0].icon_type); |
| 2073 EXPECT_EQ(icon_url_sizes, icon_url_sizes_out); |
| 2074 } |
| 2075 |
| 2076 // Test that GetFaviconsFromDB() correctly sets the expired flag for bitmap |
| 2077 // reults. |
| 2078 TEST_F(HistoryBackendTest, GetFaviconsFromDBExpired) { |
| 2079 const GURL page_url("http://www.google.com/"); |
| 2080 const GURL icon_url("http://www.google.com/icon.png"); |
| 2081 |
| 2082 std::vector<unsigned char> data; |
| 2083 data.push_back('a'); |
| 2084 scoped_refptr<base::RefCountedBytes> bitmap_data( |
| 2085 base::RefCountedBytes::TakeVector(&data)); |
| 2086 base::Time last_updated = base::Time::FromTimeT(0); |
| 2087 FaviconID icon_id = backend_->thumbnail_db_->AddFavicon(icon_url, FAVICON, |
| 2088 GetSizesSmallAndLarge(), bitmap_data, last_updated, kSmallSize); |
| 2089 EXPECT_NE(0, icon_id); |
| 2090 EXPECT_NE(0, backend_->thumbnail_db_->AddIconMapping(page_url, icon_id)); |
| 2091 |
| 2092 std::vector<FaviconBitmapResult> bitmap_results_out; |
| 2093 IconURLSizesMap icon_url_sizes_out; |
| 2094 EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url, FAVICON, |
| 2095 kSmallSize.width(), GetScaleFactors1x2x(), &bitmap_results_out, |
| 2096 &icon_url_sizes_out)); |
| 2097 |
| 2098 EXPECT_EQ(1u, bitmap_results_out.size()); |
| 2099 EXPECT_TRUE(bitmap_results_out[0].expired); |
| 2100 } |
| 2101 |
| 2102 TEST_F(HistoryBackendTest, CloneFaviconIsRestrictedToSameDomain) { |
1287 const GURL url("http://www.google.com/"); | 2103 const GURL url("http://www.google.com/"); |
1288 const GURL icon_url("http://www.google.com/icon"); | 2104 const GURL same_domain_url("http://www.google.com/subdir/index.html"); |
1289 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); | 2105 const GURL foreign_domain_url("http://www.not-google.com/"); |
1290 scoped_refptr<base::RefCountedBytes> bytes(new base::RefCountedBytes(data)); | 2106 const GURL icon_url("http://www.google.com/icon.png"); |
1291 // Used for testing the icon data after getting from DB | |
1292 std::string blob_data(bytes->front(), | |
1293 bytes->front() + bytes->size()); | |
1294 | 2107 |
1295 // Add a favicon | 2108 // Add a favicon |
1296 backend_->SetFavicon( | 2109 IconURLSizesMap icon_url_sizes; |
1297 url, icon_url, bytes.get(), FAVICON); | 2110 icon_url_sizes[icon_url] = GetSizesSmall(); |
| 2111 |
| 2112 std::vector<FaviconBitmapData> favicon_bitmap_data; |
| 2113 GenerateFaviconBitmapData(icon_url, GetSizesSmall(), &favicon_bitmap_data); |
| 2114 backend_->SetFavicons(url, FAVICON, favicon_bitmap_data, icon_url_sizes); |
1298 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | 2115 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( |
1299 url, FAVICON, NULL)); | 2116 url, FAVICON, NULL)); |
1300 | 2117 |
1301 // Add a touch_icon | |
1302 backend_->SetFavicon( | |
1303 url, icon_url, bytes.get(), TOUCH_ICON); | |
1304 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | |
1305 url, TOUCH_ICON, NULL)); | |
1306 | |
1307 // Test the Fav icon for this URL. | |
1308 std::vector<FaviconBitmapResult> favicon_bitmap_results; | |
1309 ASSERT_TRUE(backend_->GetFaviconFromDB(url, FAVICON, | |
1310 &favicon_bitmap_results)); | |
1311 EXPECT_EQ(1u, favicon_bitmap_results.size()); | |
1312 FaviconBitmapResult bitmap_result = favicon_bitmap_results[0]; | |
1313 std::string favicon_data( | |
1314 bitmap_result.bitmap_data->front(), | |
1315 bitmap_result.bitmap_data->front() + bitmap_result.bitmap_data->size()); | |
1316 | |
1317 EXPECT_EQ(FAVICON, bitmap_result.icon_type); | |
1318 EXPECT_EQ(icon_url, bitmap_result.icon_url); | |
1319 EXPECT_EQ(blob_data, favicon_data); | |
1320 | |
1321 // Test the touch icon for this URL. | |
1322 ASSERT_TRUE(backend_->GetFaviconFromDB(url, TOUCH_ICON, | |
1323 &favicon_bitmap_results)); | |
1324 EXPECT_EQ(1u, favicon_bitmap_results.size()); | |
1325 bitmap_result = favicon_bitmap_results[0]; | |
1326 std::string touchicon_data( | |
1327 bitmap_result.bitmap_data->front(), | |
1328 bitmap_result.bitmap_data->front() + bitmap_result.bitmap_data->size()); | |
1329 | |
1330 EXPECT_EQ(TOUCH_ICON, bitmap_result.icon_type); | |
1331 EXPECT_EQ(icon_url, bitmap_result.icon_url); | |
1332 EXPECT_EQ(blob_data, touchicon_data); | |
1333 } | |
1334 | |
1335 TEST_F(HistoryBackendTest, CloneFaviconIsRestrictedToSameDomain) { | |
1336 const GURL url("http://www.google.com/"); | |
1337 const GURL icon_url("http://www.google.com/icon"); | |
1338 const GURL same_domain_url("http://www.google.com/subdir/index.html"); | |
1339 const GURL foreign_domain_url("http://www.not-google.com/"); | |
1340 | |
1341 // Add a favicon | |
1342 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); | |
1343 scoped_refptr<base::RefCountedBytes> bytes(new base::RefCountedBytes(data)); | |
1344 backend_->SetFavicon( | |
1345 url, icon_url, bytes.get(), FAVICON); | |
1346 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( | |
1347 url, FAVICON, NULL)); | |
1348 | |
1349 // Validate starting state. | 2118 // Validate starting state. |
1350 std::vector<FaviconBitmapResult> favicon_bitmap_results; | 2119 std::vector<FaviconBitmapResult> bitmap_results_out; |
1351 EXPECT_TRUE(backend_->GetFaviconFromDB(url, FAVICON, | 2120 IconURLSizesMap icon_url_sizes_out; |
1352 &favicon_bitmap_results)); | 2121 EXPECT_TRUE(backend_->GetFaviconsFromDB(url, FAVICON, |
1353 EXPECT_FALSE(backend_->GetFaviconFromDB(same_domain_url, FAVICON, | 2122 kSmallSize.width(), GetScaleFactors1x2x(), &bitmap_results_out, |
1354 &favicon_bitmap_results)); | 2123 &icon_url_sizes_out)); |
1355 EXPECT_FALSE(backend_->GetFaviconFromDB(foreign_domain_url, FAVICON, | 2124 EXPECT_FALSE(backend_->GetFaviconsFromDB(same_domain_url, FAVICON, |
1356 &favicon_bitmap_results)); | 2125 kSmallSize.width(), GetScaleFactors1x2x(), &bitmap_results_out, |
| 2126 &icon_url_sizes_out)); |
| 2127 EXPECT_FALSE(backend_->GetFaviconsFromDB(foreign_domain_url, FAVICON, |
| 2128 kSmallSize.width(), GetScaleFactors1x2x(), &bitmap_results_out, |
| 2129 &icon_url_sizes_out)); |
1357 | 2130 |
1358 // Same-domain cloning should work. | 2131 // Same-domain cloning should work. |
1359 backend_->CloneFavicon(url, same_domain_url); | 2132 backend_->CloneFavicons(url, same_domain_url); |
1360 EXPECT_TRUE(backend_->GetFaviconFromDB(same_domain_url, FAVICON, | 2133 EXPECT_TRUE(backend_->GetFaviconsFromDB(same_domain_url, FAVICON, |
1361 &favicon_bitmap_results)); | 2134 kSmallSize.width(), GetScaleFactors1x2x(), &bitmap_results_out, |
| 2135 &icon_url_sizes_out)); |
1362 | 2136 |
1363 // Foreign-domain cloning is forbidden. | 2137 // Foreign-domain cloning is forbidden. |
1364 backend_->CloneFavicon(url, foreign_domain_url); | 2138 backend_->CloneFavicons(url, foreign_domain_url); |
1365 EXPECT_FALSE(backend_->GetFaviconFromDB(foreign_domain_url, FAVICON, | 2139 EXPECT_FALSE(backend_->GetFaviconsFromDB(foreign_domain_url, FAVICON, |
1366 &favicon_bitmap_results)); | 2140 kSmallSize.width(), GetScaleFactors1x2x(), &bitmap_results_out, |
| 2141 &icon_url_sizes_out)); |
1367 } | 2142 } |
1368 | 2143 |
1369 TEST_F(HistoryBackendTest, QueryFilteredURLs) { | 2144 TEST_F(HistoryBackendTest, QueryFilteredURLs) { |
1370 const char* google = "http://www.google.com/"; | 2145 const char* google = "http://www.google.com/"; |
1371 const char* yahoo = "http://www.yahoo.com/"; | 2146 const char* yahoo = "http://www.yahoo.com/"; |
1372 const char* yahoo_sports = "http://sports.yahoo.com/"; | 2147 const char* yahoo_sports = "http://sports.yahoo.com/"; |
1373 const char* yahoo_sports_with_article1 = | 2148 const char* yahoo_sports_with_article1 = |
1374 "http://sports.yahoo.com/article1.htm"; | 2149 "http://sports.yahoo.com/article1.htm"; |
1375 const char* yahoo_sports_with_article2 = | 2150 const char* yahoo_sports_with_article2 = |
1376 "http://sports.yahoo.com/article2.htm"; | 2151 "http://sports.yahoo.com/article2.htm"; |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1690 | 2465 |
1691 backend_->DeleteURL(url); | 2466 backend_->DeleteURL(url); |
1692 backend_->AddPageNoVisitForBookmark(url, string16()); | 2467 backend_->AddPageNoVisitForBookmark(url, string16()); |
1693 backend_->GetURL(url, &row); | 2468 backend_->GetURL(url, &row); |
1694 EXPECT_EQ(url, row.url()); | 2469 EXPECT_EQ(url, row.url()); |
1695 EXPECT_EQ(UTF8ToUTF16(url.spec()), row.title()); | 2470 EXPECT_EQ(UTF8ToUTF16(url.spec()), row.title()); |
1696 EXPECT_EQ(0, row.visit_count()); | 2471 EXPECT_EQ(0, row.visit_count()); |
1697 } | 2472 } |
1698 | 2473 |
1699 } // namespace history | 2474 } // namespace history |
OLD | NEW |