| 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 <algorithm> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 const double kBoringness = 0.25; | 42 const double kBoringness = 0.25; |
| 43 const double kWorseBoringness = 0.50; | 43 const double kWorseBoringness = 0.50; |
| 44 const double kBetterBoringness = 0.10; | 44 const double kBetterBoringness = 0.10; |
| 45 const double kTotallyBoring = 1.0; | 45 const double kTotallyBoring = 1.0; |
| 46 | 46 |
| 47 const int64 kPage1 = 1234; | 47 const int64 kPage1 = 1234; |
| 48 | 48 |
| 49 const gfx::Size kSmallSize = gfx::Size(16, 16); | 49 const gfx::Size kSmallSize = gfx::Size(16, 16); |
| 50 const gfx::Size kLargeSize = gfx::Size(32, 32); | 50 const gfx::Size kLargeSize = gfx::Size(32, 32); |
| 51 | 51 |
| 52 const std::string kSizesSmallAndLarge = "16 16 32 32"; | |
| 53 | |
| 54 } // namespace | 52 } // namespace |
| 55 | 53 |
| 56 class ThumbnailDatabaseTest : public testing::Test { | 54 class ThumbnailDatabaseTest : public testing::Test { |
| 57 public: | 55 public: |
| 58 ThumbnailDatabaseTest() { | 56 ThumbnailDatabaseTest() { |
| 59 } | 57 } |
| 60 ~ThumbnailDatabaseTest() { | 58 ~ThumbnailDatabaseTest() { |
| 61 } | 59 } |
| 62 | 60 |
| 63 protected: | 61 protected: |
| 64 virtual void SetUp() { | 62 virtual void SetUp() { |
| 65 // Get a temporary directory for the test DB files. | 63 // Get a temporary directory for the test DB files. |
| 66 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 64 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 67 | 65 |
| 68 file_name_ = temp_dir_.path().AppendASCII("TestThumbnails.db"); | 66 file_name_ = temp_dir_.path().AppendASCII("TestThumbnails.db"); |
| 69 new_file_name_ = temp_dir_.path().AppendASCII("TestFavicons.db"); | 67 new_file_name_ = temp_dir_.path().AppendASCII("TestFavicons.db"); |
| 70 history_db_name_ = temp_dir_.path().AppendASCII("TestHistory.db"); | 68 history_db_name_ = temp_dir_.path().AppendASCII("TestHistory.db"); |
| 71 google_bitmap_.reset( | 69 google_bitmap_.reset( |
| 72 gfx::JPEGCodec::Decode(kGoogleThumbnail, sizeof(kGoogleThumbnail))); | 70 gfx::JPEGCodec::Decode(kGoogleThumbnail, sizeof(kGoogleThumbnail))); |
| 73 } | 71 } |
| 74 | 72 |
| 73 const FaviconSizes& GetSizesSmallAndLarge() { |
| 74 CR_DEFINE_STATIC_LOCAL(FaviconSizes, kSizesSmallAndLarge, ()); |
| 75 if (kSizesSmallAndLarge.empty()) { |
| 76 kSizesSmallAndLarge.push_back(kSmallSize); |
| 77 kSizesSmallAndLarge.push_back(kLargeSize); |
| 78 } |
| 79 return kSizesSmallAndLarge; |
| 80 } |
| 81 |
| 75 scoped_ptr<SkBitmap> google_bitmap_; | 82 scoped_ptr<SkBitmap> google_bitmap_; |
| 76 | 83 |
| 77 ScopedTempDir temp_dir_; | 84 ScopedTempDir temp_dir_; |
| 78 FilePath file_name_; | 85 FilePath file_name_; |
| 79 FilePath new_file_name_; | 86 FilePath new_file_name_; |
| 80 FilePath history_db_name_; | 87 FilePath history_db_name_; |
| 88 FaviconSizes sizes_small_and_large_; |
| 81 }; | 89 }; |
| 82 | 90 |
| 83 class IconMappingMigrationTest : public HistoryUnitTestBase { | 91 class IconMappingMigrationTest : public HistoryUnitTestBase { |
| 84 public: | 92 public: |
| 85 IconMappingMigrationTest() { | 93 IconMappingMigrationTest() { |
| 86 } | 94 } |
| 87 ~IconMappingMigrationTest() { | 95 ~IconMappingMigrationTest() { |
| 88 } | 96 } |
| 89 | 97 |
| 90 protected: | 98 protected: |
| (...skipping 27 matching lines...) Expand all Loading... |
| 118 TEST_F(ThumbnailDatabaseTest, GetFaviconAfterMigrationToTopSites) { | 126 TEST_F(ThumbnailDatabaseTest, GetFaviconAfterMigrationToTopSites) { |
| 119 ThumbnailDatabase db; | 127 ThumbnailDatabase db; |
| 120 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); | 128 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); |
| 121 db.BeginTransaction(); | 129 db.BeginTransaction(); |
| 122 | 130 |
| 123 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); | 131 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); |
| 124 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); | 132 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); |
| 125 | 133 |
| 126 GURL url("http://google.com"); | 134 GURL url("http://google.com"); |
| 127 FaviconID icon_id = db.AddFavicon(url, FAVICON); | 135 FaviconID icon_id = db.AddFavicon(url, FAVICON); |
| 128 db.SetFaviconSizes(icon_id, kSizesSmallAndLarge); | 136 db.SetFaviconSizes(icon_id, GetSizesSmallAndLarge()); |
| 129 base::Time time = base::Time::Now(); | 137 base::Time time = base::Time::Now(); |
| 130 FaviconBitmapID bitmap1_id = db.AddFaviconBitmap(icon_id, favicon, time, | 138 FaviconBitmapID bitmap1_id = db.AddFaviconBitmap(icon_id, favicon, time, |
| 131 kSmallSize); | 139 kSmallSize); |
| 132 FaviconBitmapID bitmap2_id = db.AddFaviconBitmap(icon_id, favicon, time, | 140 FaviconBitmapID bitmap2_id = db.AddFaviconBitmap(icon_id, favicon, time, |
| 133 kLargeSize); | 141 kLargeSize); |
| 134 EXPECT_TRUE(db.RenameAndDropThumbnails(file_name_, new_file_name_)); | 142 EXPECT_TRUE(db.RenameAndDropThumbnails(file_name_, new_file_name_)); |
| 135 EXPECT_TRUE(db.IsLatestVersion()); | 143 EXPECT_TRUE(db.IsLatestVersion()); |
| 136 | 144 |
| 137 GURL url_out; | 145 GURL url_out; |
| 138 IconType icon_type_out; | 146 IconType icon_type_out; |
| 139 std::string sizes_out; | 147 FaviconSizes sizes_out; |
| 140 EXPECT_TRUE(db.GetFaviconHeader(icon_id, &url_out, &icon_type_out, | 148 EXPECT_TRUE(db.GetFaviconHeader(icon_id, &url_out, &icon_type_out, |
| 141 &sizes_out)); | 149 &sizes_out)); |
| 142 | 150 |
| 143 EXPECT_EQ(url, url_out); | 151 EXPECT_EQ(url, url_out); |
| 144 EXPECT_EQ(FAVICON, icon_type_out); | 152 EXPECT_EQ(FAVICON, icon_type_out); |
| 145 EXPECT_STREQ(kSizesSmallAndLarge.c_str(), sizes_out.c_str()); | 153 EXPECT_EQ(GetSizesSmallAndLarge(), sizes_out); |
| 146 | 154 |
| 147 std::vector<FaviconBitmap> favicon_bitmaps_out; | 155 std::vector<FaviconBitmap> favicon_bitmaps_out; |
| 148 EXPECT_TRUE(db.GetFaviconBitmaps(icon_id, &favicon_bitmaps_out)); | 156 EXPECT_TRUE(db.GetFaviconBitmaps(icon_id, &favicon_bitmaps_out)); |
| 149 EXPECT_EQ(2u, favicon_bitmaps_out.size()); | 157 EXPECT_EQ(2u, favicon_bitmaps_out.size()); |
| 150 | 158 |
| 151 FaviconBitmap favicon_bitmap1 = favicon_bitmaps_out[0]; | 159 FaviconBitmap favicon_bitmap1 = favicon_bitmaps_out[0]; |
| 152 FaviconBitmap favicon_bitmap2 = favicon_bitmaps_out[1]; | 160 FaviconBitmap favicon_bitmap2 = favicon_bitmaps_out[1]; |
| 153 | 161 |
| 154 // Bitmaps do not need to be in particular order. | 162 // Favicon bitmaps do not need to be in particular order. |
| 155 if (favicon_bitmap1.bitmap_id == bitmap2_id) { | 163 if (favicon_bitmap1.bitmap_id == bitmap2_id) { |
| 156 FaviconBitmap tmp_favicon_bitmap = favicon_bitmap1; | 164 FaviconBitmap tmp_favicon_bitmap = favicon_bitmap1; |
| 157 favicon_bitmap1 = favicon_bitmap2; | 165 favicon_bitmap1 = favicon_bitmap2; |
| 158 favicon_bitmap2 = tmp_favicon_bitmap; | 166 favicon_bitmap2 = tmp_favicon_bitmap; |
| 159 } | 167 } |
| 160 | 168 |
| 161 EXPECT_EQ(bitmap1_id, favicon_bitmap1.bitmap_id); | 169 EXPECT_EQ(bitmap1_id, favicon_bitmap1.bitmap_id); |
| 162 EXPECT_EQ(icon_id, favicon_bitmap1.icon_id); | 170 EXPECT_EQ(icon_id, favicon_bitmap1.icon_id); |
| 163 EXPECT_EQ(time.ToTimeT(), favicon_bitmap1.last_updated.ToTimeT()); | 171 EXPECT_EQ(time.ToTimeT(), favicon_bitmap1.last_updated.ToTimeT()); |
| 164 EXPECT_EQ(data.size(), favicon_bitmap1.bitmap_data->size()); | 172 EXPECT_EQ(data.size(), favicon_bitmap1.bitmap_data->size()); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 185 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); | 193 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); |
| 186 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); | 194 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); |
| 187 | 195 |
| 188 GURL url("http://google.com"); | 196 GURL url("http://google.com"); |
| 189 FaviconID id = db.AddFavicon(url, TOUCH_ICON); | 197 FaviconID id = db.AddFavicon(url, TOUCH_ICON); |
| 190 EXPECT_NE(0, id); | 198 EXPECT_NE(0, id); |
| 191 base::Time time = base::Time::Now(); | 199 base::Time time = base::Time::Now(); |
| 192 db.AddFaviconBitmap(id, favicon, time, gfx::Size()); | 200 db.AddFaviconBitmap(id, favicon, time, gfx::Size()); |
| 193 | 201 |
| 194 EXPECT_NE(0, db.AddIconMapping(url, id)); | 202 EXPECT_NE(0, db.AddIconMapping(url, id)); |
| 195 std::vector<IconMapping> icon_mapping; | 203 std::vector<IconMapping> icon_mappings; |
| 196 EXPECT_TRUE(db.GetIconMappingsForPageURL(url, &icon_mapping)); | 204 EXPECT_TRUE(db.GetIconMappingsForPageURL(url, &icon_mappings)); |
| 197 EXPECT_EQ(1u, icon_mapping.size()); | 205 EXPECT_EQ(1u, icon_mappings.size()); |
| 198 EXPECT_EQ(url, icon_mapping.front().page_url); | 206 EXPECT_EQ(url, icon_mappings.front().page_url); |
| 199 EXPECT_EQ(id, icon_mapping.front().icon_id); | 207 EXPECT_EQ(id, icon_mappings.front().icon_id); |
| 200 } | 208 } |
| 201 | 209 |
| 202 TEST_F(ThumbnailDatabaseTest, UpdateIconMapping) { | 210 TEST_F(ThumbnailDatabaseTest, UpdateIconMapping) { |
| 203 ThumbnailDatabase db; | 211 ThumbnailDatabase db; |
| 204 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); | 212 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); |
| 205 db.BeginTransaction(); | 213 db.BeginTransaction(); |
| 206 | 214 |
| 207 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); | 215 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); |
| 208 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); | 216 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); |
| 209 | 217 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 | 255 |
| 248 FaviconID id2 = db.AddFavicon(url, FAVICON); | 256 FaviconID id2 = db.AddFavicon(url, FAVICON); |
| 249 db.AddFaviconBitmap(id2, favicon, time, gfx::Size()); | 257 db.AddFaviconBitmap(id2, favicon, time, gfx::Size()); |
| 250 EXPECT_TRUE(0 < db.AddIconMapping(url, id2)); | 258 EXPECT_TRUE(0 < db.AddIconMapping(url, id2)); |
| 251 ASSERT_NE(id, id2); | 259 ASSERT_NE(id, id2); |
| 252 | 260 |
| 253 std::vector<IconMapping> icon_mapping; | 261 std::vector<IconMapping> icon_mapping; |
| 254 EXPECT_TRUE(db.GetIconMappingsForPageURL(url, &icon_mapping)); | 262 EXPECT_TRUE(db.GetIconMappingsForPageURL(url, &icon_mapping)); |
| 255 ASSERT_EQ(2u, icon_mapping.size()); | 263 ASSERT_EQ(2u, icon_mapping.size()); |
| 256 EXPECT_EQ(icon_mapping.front().icon_type, TOUCH_ICON); | 264 EXPECT_EQ(icon_mapping.front().icon_type, TOUCH_ICON); |
| 257 EXPECT_TRUE(db.GetIconMappingForPageURL(url, FAVICON, NULL)); | 265 EXPECT_TRUE(db.GetIconMappingsForPageURL(url, FAVICON, NULL)); |
| 258 | 266 |
| 259 db.DeleteIconMappings(url); | 267 db.DeleteIconMappings(url); |
| 260 | 268 |
| 261 EXPECT_FALSE(db.GetIconMappingsForPageURL(url, NULL)); | 269 EXPECT_FALSE(db.GetIconMappingsForPageURL(url, NULL)); |
| 262 EXPECT_FALSE(db.GetIconMappingForPageURL(url, FAVICON, NULL)); | 270 EXPECT_FALSE(db.GetIconMappingsForPageURL(url, FAVICON, NULL)); |
| 263 } | 271 } |
| 264 | 272 |
| 265 TEST_F(ThumbnailDatabaseTest, GetIconMappingsForPageURL) { | 273 TEST_F(ThumbnailDatabaseTest, GetIconMappingsForPageURL) { |
| 266 ThumbnailDatabase db; | 274 ThumbnailDatabase db; |
| 267 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); | 275 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); |
| 268 db.BeginTransaction(); | 276 db.BeginTransaction(); |
| 269 | 277 |
| 270 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); | 278 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); |
| 271 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); | 279 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); |
| 272 | 280 |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 453 db.AddIconMapping(page_url, kept_id); | 461 db.AddIconMapping(page_url, kept_id); |
| 454 | 462 |
| 455 FaviconID new_favicon_id = | 463 FaviconID new_favicon_id = |
| 456 db.CopyFaviconAndFaviconBitmapsToTemporaryTables(kept_id); | 464 db.CopyFaviconAndFaviconBitmapsToTemporaryTables(kept_id); |
| 457 EXPECT_NE(0, new_favicon_id); | 465 EXPECT_NE(0, new_favicon_id); |
| 458 EXPECT_TRUE(db.AddToTemporaryIconMappingTable(page_url, new_favicon_id)); | 466 EXPECT_TRUE(db.AddToTemporaryIconMappingTable(page_url, new_favicon_id)); |
| 459 | 467 |
| 460 EXPECT_TRUE(db.CommitTemporaryTables()); | 468 EXPECT_TRUE(db.CommitTemporaryTables()); |
| 461 | 469 |
| 462 // Only copied data should be left. | 470 // Only copied data should be left. |
| 463 IconMapping icon_mapping; | 471 std::vector<IconMapping> icon_mappings; |
| 464 EXPECT_TRUE(db.GetIconMappingForPageURL(page_url, FAVICON, &icon_mapping)); | 472 EXPECT_TRUE(db.GetIconMappingsForPageURL(page_url, FAVICON, &icon_mappings)); |
| 465 EXPECT_EQ(new_favicon_id, icon_mapping.icon_id); | 473 EXPECT_EQ(1u, icon_mappings.size()); |
| 466 EXPECT_EQ(page_url, icon_mapping.page_url); | 474 EXPECT_EQ(new_favicon_id, icon_mappings[0].icon_id); |
| 475 EXPECT_EQ(page_url, icon_mappings[0].page_url); |
| 467 | 476 |
| 468 std::vector<FaviconBitmap> favicon_bitmaps; | 477 std::vector<FaviconBitmap> favicon_bitmaps; |
| 469 EXPECT_TRUE(db.GetFaviconBitmaps(icon_mapping.icon_id, &favicon_bitmaps)); | 478 EXPECT_TRUE(db.GetFaviconBitmaps(icon_mappings[0].icon_id, &favicon_bitmaps)); |
| 470 EXPECT_EQ(1u, favicon_bitmaps.size()); | 479 EXPECT_EQ(1u, favicon_bitmaps.size()); |
| 471 EXPECT_EQ(kLargeSize, favicon_bitmaps[0].pixel_size); | 480 EXPECT_EQ(kLargeSize, favicon_bitmaps[0].pixel_size); |
| 472 | 481 |
| 473 EXPECT_FALSE(db.GetFaviconIDForFaviconURL(unkept_url, false, NULL)); | 482 EXPECT_FALSE(db.GetFaviconIDForFaviconURL(unkept_url, false)); |
| 474 } | 483 } |
| 475 | 484 |
| 476 // Tests that deleting a favicon deletes the favicon row and favicon bitmap | 485 // Tests that deleting a favicon deletes the favicon row and favicon bitmap |
| 477 // rows from the database. | 486 // rows from the database. |
| 478 TEST_F(ThumbnailDatabaseTest, DeleteFavicon) { | 487 TEST_F(ThumbnailDatabaseTest, DeleteFavicon) { |
| 479 ThumbnailDatabase db; | 488 ThumbnailDatabase db; |
| 480 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); | 489 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); |
| 481 db.BeginTransaction(); | 490 db.BeginTransaction(); |
| 482 | 491 |
| 483 std::vector<unsigned char> data1(blob1, blob1 + sizeof(blob1)); | 492 std::vector<unsigned char> data1(blob1, blob1 + sizeof(blob1)); |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 EXPECT_NE(0, db.AddIconMapping(url, id3)); | 555 EXPECT_NE(0, db.AddIconMapping(url, id3)); |
| 547 | 556 |
| 548 icon_mapping.clear(); | 557 icon_mapping.clear(); |
| 549 EXPECT_TRUE(db.GetIconMappingsForPageURL(url, &icon_mapping)); | 558 EXPECT_TRUE(db.GetIconMappingsForPageURL(url, &icon_mapping)); |
| 550 | 559 |
| 551 EXPECT_EQ(url, icon_mapping.front().page_url); | 560 EXPECT_EQ(url, icon_mapping.front().page_url); |
| 552 EXPECT_EQ(id3, icon_mapping.front().icon_id); | 561 EXPECT_EQ(id3, icon_mapping.front().icon_id); |
| 553 EXPECT_EQ(TOUCH_PRECOMPOSED_ICON, icon_mapping.front().icon_type); | 562 EXPECT_EQ(TOUCH_PRECOMPOSED_ICON, icon_mapping.front().icon_type); |
| 554 } | 563 } |
| 555 | 564 |
| 565 // Test result of GetIconMappingsForPageURL when an icon type is passed in. |
| 566 TEST_F(ThumbnailDatabaseTest, GetIconMappingsForPageURLWithIconType) { |
| 567 ThumbnailDatabase db; |
| 568 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); |
| 569 db.BeginTransaction(); |
| 570 |
| 571 GURL url("http://google.com"); |
| 572 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); |
| 573 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); |
| 574 base::Time time = base::Time::Now(); |
| 575 |
| 576 FaviconID id1 = db.AddFavicon(url, FAVICON, kDefaultFaviconSizes, favicon, |
| 577 time, gfx::Size()); |
| 578 EXPECT_NE(0, db.AddIconMapping(url, id1)); |
| 579 |
| 580 FaviconID id2 = db.AddFavicon(url, TOUCH_ICON, kDefaultFaviconSizes, favicon, |
| 581 time, gfx::Size()); |
| 582 EXPECT_NE(0, db.AddIconMapping(url, id2)); |
| 583 |
| 584 std::vector<IconMapping> icon_mappings; |
| 585 EXPECT_TRUE(db.GetIconMappingsForPageURL(url, FAVICON | TOUCH_ICON, |
| 586 &icon_mappings)); |
| 587 EXPECT_EQ(2u, icon_mappings.size()); |
| 588 EXPECT_EQ(id2, icon_mappings[0].icon_id); |
| 589 EXPECT_EQ(id1, icon_mappings[1].icon_id); |
| 590 |
| 591 icon_mappings.clear(); |
| 592 EXPECT_TRUE(db.GetIconMappingsForPageURL(url, TOUCH_ICON, &icon_mappings)); |
| 593 EXPECT_EQ(1u, icon_mappings.size()); |
| 594 EXPECT_EQ(id2, icon_mappings[0].icon_id); |
| 595 } |
| 596 |
| 556 TEST_F(ThumbnailDatabaseTest, HasMappingFor) { | 597 TEST_F(ThumbnailDatabaseTest, HasMappingFor) { |
| 557 ThumbnailDatabase db; | 598 ThumbnailDatabase db; |
| 558 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); | 599 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); |
| 559 db.BeginTransaction(); | 600 db.BeginTransaction(); |
| 560 | 601 |
| 561 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); | 602 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); |
| 562 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); | 603 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); |
| 563 | 604 |
| 564 // Add a favicon which will have icon_mappings | 605 // Add a favicon which will have icon_mappings |
| 565 FaviconID id1 = db.AddFavicon(GURL("http://google.com"), FAVICON); | 606 FaviconID id1 = db.AddFavicon(GURL("http://google.com"), FAVICON); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 588 EXPECT_TRUE(db.HasMappingFor(id2)); | 629 EXPECT_TRUE(db.HasMappingFor(id2)); |
| 589 EXPECT_FALSE(db.HasMappingFor(id3)); | 630 EXPECT_FALSE(db.HasMappingFor(id3)); |
| 590 | 631 |
| 591 // Remove all mappings | 632 // Remove all mappings |
| 592 db.DeleteIconMappings(page_url); | 633 db.DeleteIconMappings(page_url); |
| 593 EXPECT_FALSE(db.HasMappingFor(id1)); | 634 EXPECT_FALSE(db.HasMappingFor(id1)); |
| 594 EXPECT_FALSE(db.HasMappingFor(id2)); | 635 EXPECT_FALSE(db.HasMappingFor(id2)); |
| 595 EXPECT_FALSE(db.HasMappingFor(id3)); | 636 EXPECT_FALSE(db.HasMappingFor(id3)); |
| 596 } | 637 } |
| 597 | 638 |
| 598 TEST_F(ThumbnailDatabaseTest, CloneIconMapping) { | 639 TEST_F(ThumbnailDatabaseTest, CloneIconMappings) { |
| 599 ThumbnailDatabase db; | 640 ThumbnailDatabase db; |
| 600 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); | 641 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); |
| 601 db.BeginTransaction(); | 642 db.BeginTransaction(); |
| 602 | 643 |
| 603 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); | 644 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); |
| 604 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); | 645 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); |
| 605 | 646 |
| 606 // Add a favicon which will have icon_mappings | 647 // Add a favicon which will have icon_mappings |
| 607 FaviconID id1 = db.AddFavicon(GURL("http://google.com"), FAVICON); | 648 FaviconID id1 = db.AddFavicon(GURL("http://google.com"), FAVICON); |
| 608 EXPECT_NE(0, id1); | 649 EXPECT_NE(0, id1); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 626 EXPECT_TRUE(db.AddIconMapping(page1_url, id2)); | 667 EXPECT_TRUE(db.AddIconMapping(page1_url, id2)); |
| 627 | 668 |
| 628 GURL page2_url("http://page2.com"); | 669 GURL page2_url("http://page2.com"); |
| 629 EXPECT_TRUE(db.AddIconMapping(page2_url, id3)); | 670 EXPECT_TRUE(db.AddIconMapping(page2_url, id3)); |
| 630 | 671 |
| 631 // Test we do nothing with existing mappings. | 672 // Test we do nothing with existing mappings. |
| 632 std::vector<IconMapping> icon_mapping; | 673 std::vector<IconMapping> icon_mapping; |
| 633 EXPECT_TRUE(db.GetIconMappingsForPageURL(page2_url, &icon_mapping)); | 674 EXPECT_TRUE(db.GetIconMappingsForPageURL(page2_url, &icon_mapping)); |
| 634 ASSERT_EQ(1U, icon_mapping.size()); | 675 ASSERT_EQ(1U, icon_mapping.size()); |
| 635 | 676 |
| 636 EXPECT_TRUE(db.CloneIconMapping(page1_url, page2_url)); | 677 EXPECT_TRUE(db.CloneIconMappings(page1_url, page2_url)); |
| 637 | 678 |
| 638 icon_mapping.clear(); | 679 icon_mapping.clear(); |
| 639 EXPECT_TRUE(db.GetIconMappingsForPageURL(page2_url, &icon_mapping)); | 680 EXPECT_TRUE(db.GetIconMappingsForPageURL(page2_url, &icon_mapping)); |
| 640 ASSERT_EQ(1U, icon_mapping.size()); | 681 ASSERT_EQ(1U, icon_mapping.size()); |
| 641 EXPECT_EQ(page2_url, icon_mapping[0].page_url); | 682 EXPECT_EQ(page2_url, icon_mapping[0].page_url); |
| 642 EXPECT_EQ(id3, icon_mapping[0].icon_id); | 683 EXPECT_EQ(id3, icon_mapping[0].icon_id); |
| 643 | 684 |
| 644 // Test we clone if the new page has no mappings. | 685 // Test we clone if the new page has no mappings. |
| 645 GURL page3_url("http://page3.com"); | 686 GURL page3_url("http://page3.com"); |
| 646 EXPECT_TRUE(db.CloneIconMapping(page1_url, page3_url)); | 687 EXPECT_TRUE(db.CloneIconMappings(page1_url, page3_url)); |
| 647 | 688 |
| 648 icon_mapping.clear(); | 689 icon_mapping.clear(); |
| 649 EXPECT_TRUE(db.GetIconMappingsForPageURL(page3_url, &icon_mapping)); | 690 EXPECT_TRUE(db.GetIconMappingsForPageURL(page3_url, &icon_mapping)); |
| 650 | 691 |
| 651 ASSERT_EQ(2U, icon_mapping.size()); | 692 ASSERT_EQ(2U, icon_mapping.size()); |
| 652 if (icon_mapping[0].icon_id == id2) | 693 if (icon_mapping[0].icon_id == id2) |
| 653 std::swap(icon_mapping[0], icon_mapping[1]); | 694 std::swap(icon_mapping[0], icon_mapping[1]); |
| 654 EXPECT_EQ(page3_url, icon_mapping[0].page_url); | 695 EXPECT_EQ(page3_url, icon_mapping[0].page_url); |
| 655 EXPECT_EQ(id1, icon_mapping[0].icon_id); | 696 EXPECT_EQ(id1, icon_mapping[0].icon_id); |
| 656 EXPECT_EQ(page3_url, icon_mapping[1].page_url); | 697 EXPECT_EQ(page3_url, icon_mapping[1].page_url); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 671 | 712 |
| 672 // Migration should be done. | 713 // Migration should be done. |
| 673 // Test one icon_mapping. | 714 // Test one icon_mapping. |
| 674 GURL page_url1 = GURL("http://google.com/"); | 715 GURL page_url1 = GURL("http://google.com/"); |
| 675 std::vector<IconMapping> icon_mappings; | 716 std::vector<IconMapping> icon_mappings; |
| 676 EXPECT_TRUE(db.GetIconMappingsForPageURL(page_url1, &icon_mappings)); | 717 EXPECT_TRUE(db.GetIconMappingsForPageURL(page_url1, &icon_mappings)); |
| 677 ASSERT_EQ(1u, icon_mappings.size()); | 718 ASSERT_EQ(1u, icon_mappings.size()); |
| 678 EXPECT_EQ(FAVICON, icon_mappings[0].icon_type); | 719 EXPECT_EQ(FAVICON, icon_mappings[0].icon_type); |
| 679 EXPECT_EQ(page_url1, icon_mappings[0].page_url); | 720 EXPECT_EQ(page_url1, icon_mappings[0].page_url); |
| 680 EXPECT_EQ(1, icon_mappings[0].icon_id); | 721 EXPECT_EQ(1, icon_mappings[0].icon_id); |
| 681 base::Time time; | 722 EXPECT_EQ(icon1, icon_mappings[0].icon_url); |
| 682 scoped_refptr<base::RefCountedMemory> out_data; | |
| 683 GURL out_icon_url; | |
| 684 ASSERT_TRUE(db.GetFavicon( | |
| 685 icon_mappings[0].icon_id, &time, &out_data, &out_icon_url, NULL)); | |
| 686 EXPECT_EQ(icon1, out_icon_url); | |
| 687 | 723 |
| 688 // Test a page which has the same icon. | 724 // Test a page which has the same icon. |
| 689 GURL page_url3 = GURL("http://www.google.com/"); | 725 GURL page_url3 = GURL("http://www.google.com/"); |
| 690 icon_mappings.clear(); | 726 icon_mappings.clear(); |
| 691 EXPECT_TRUE(db.GetIconMappingsForPageURL(page_url3, &icon_mappings)); | 727 EXPECT_TRUE(db.GetIconMappingsForPageURL(page_url3, &icon_mappings)); |
| 692 ASSERT_EQ(1u, icon_mappings.size()); | 728 ASSERT_EQ(1u, icon_mappings.size()); |
| 693 EXPECT_EQ(FAVICON, icon_mappings[0].icon_type); | 729 EXPECT_EQ(FAVICON, icon_mappings[0].icon_type); |
| 694 EXPECT_EQ(page_url3, icon_mappings[0].page_url); | 730 EXPECT_EQ(page_url3, icon_mappings[0].page_url); |
| 695 EXPECT_EQ(1, icon_mappings[0].icon_id); | 731 EXPECT_EQ(1, icon_mappings[0].icon_id); |
| 732 EXPECT_EQ(icon1, icon_mappings[0].icon_url); |
| 696 | 733 |
| 697 // Test a icon_mapping with different IconID. | 734 // Test a icon_mapping with different IconID. |
| 698 GURL page_url2 = GURL("http://yahoo.com/"); | 735 GURL page_url2 = GURL("http://yahoo.com/"); |
| 699 icon_mappings.clear(); | 736 icon_mappings.clear(); |
| 700 EXPECT_TRUE(db.GetIconMappingsForPageURL(page_url2, &icon_mappings)); | 737 EXPECT_TRUE(db.GetIconMappingsForPageURL(page_url2, &icon_mappings)); |
| 701 ASSERT_EQ(1u, icon_mappings.size()); | 738 ASSERT_EQ(1u, icon_mappings.size()); |
| 702 EXPECT_EQ(FAVICON, icon_mappings[0].icon_type); | 739 EXPECT_EQ(FAVICON, icon_mappings[0].icon_type); |
| 703 EXPECT_EQ(page_url2, icon_mappings[0].page_url); | 740 EXPECT_EQ(page_url2, icon_mappings[0].page_url); |
| 704 EXPECT_EQ(2, icon_mappings[0].icon_id); | 741 EXPECT_EQ(2, icon_mappings[0].icon_id); |
| 705 ASSERT_TRUE(db.GetFavicon( | 742 EXPECT_EQ(icon2, icon_mappings[0].icon_url); |
| 706 icon_mappings[0].icon_id, &time, &out_data, &out_icon_url, NULL)); | |
| 707 EXPECT_EQ(icon2, out_icon_url); | |
| 708 | 743 |
| 709 // Test a page without icon | 744 // Test a page without icon |
| 710 GURL page_url4 = GURL("http://www.google.com/blank.html"); | 745 GURL page_url4 = GURL("http://www.google.com/blank.html"); |
| 711 EXPECT_FALSE(db.GetIconMappingsForPageURL(page_url4, NULL)); | 746 EXPECT_FALSE(db.GetIconMappingsForPageURL(page_url4, NULL)); |
| 712 } | 747 } |
| 713 | 748 |
| 714 TEST_F(ThumbnailDatabaseTest, IconMappingEnumerator) { | 749 TEST_F(ThumbnailDatabaseTest, IconMappingEnumerator) { |
| 715 ThumbnailDatabase db; | 750 ThumbnailDatabase db; |
| 716 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); | 751 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); |
| 717 db.BeginTransaction(); | 752 db.BeginTransaction(); |
| 718 | 753 |
| 719 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); | 754 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); |
| 720 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); | 755 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); |
| 721 | 756 |
| 722 GURL url("http://google.com"); | 757 GURL url("http://google.com"); |
| 723 FaviconID touch_icon_id1 = db.AddFavicon(url, TOUCH_ICON); | 758 GURL icon_url1("http://google.com/favicon.ico"); |
| 759 FaviconID touch_icon_id1 = db.AddFavicon(icon_url1, TOUCH_ICON, |
| 760 GetSizesSmallAndLarge(), favicon, base::Time::Now(), kSmallSize); |
| 724 ASSERT_NE(0, touch_icon_id1); | 761 ASSERT_NE(0, touch_icon_id1); |
| 725 ASSERT_TRUE(db.AddFaviconBitmap(touch_icon_id1, favicon, base::Time::Now(), | |
| 726 gfx::Size())); | |
| 727 IconMappingID touch_mapping_id1 = db.AddIconMapping(url, touch_icon_id1); | 762 IconMappingID touch_mapping_id1 = db.AddIconMapping(url, touch_icon_id1); |
| 728 ASSERT_NE(0, touch_mapping_id1); | 763 ASSERT_NE(0, touch_mapping_id1); |
| 729 | 764 |
| 730 FaviconID favicon_id1 = db.AddFavicon(url, FAVICON); | 765 FaviconID favicon_id1 = db.AddFavicon(icon_url1, FAVICON, |
| 766 GetSizesSmallAndLarge(), |
| 767 favicon, base::Time::Now(), kSmallSize); |
| 731 ASSERT_NE(0, favicon_id1); | 768 ASSERT_NE(0, favicon_id1); |
| 732 ASSERT_TRUE(db.AddFaviconBitmap(favicon_id1, favicon, base::Time::Now(), | |
| 733 gfx::Size())); | |
| 734 IconMappingID favicon_mapping_id1 = db.AddIconMapping(url, favicon_id1); | 769 IconMappingID favicon_mapping_id1 = db.AddIconMapping(url, favicon_id1); |
| 735 ASSERT_NE(0, favicon_mapping_id1); | 770 ASSERT_NE(0, favicon_mapping_id1); |
| 736 | 771 |
| 737 GURL url2("http://chromium.org"); | 772 GURL url2("http://chromium.org"); |
| 738 FaviconID favicon_id2 = db.AddFavicon(url2, FAVICON); | 773 GURL icon_url2("http://chromium.org/favicon.ico"); |
| 774 FaviconID favicon_id2 = db.AddFavicon(icon_url2, FAVICON, |
| 775 GetSizesSmallAndLarge(), |
| 776 favicon, base::Time::Now(), kSmallSize); |
| 739 ASSERT_NE(0, favicon_id2); | 777 ASSERT_NE(0, favicon_id2); |
| 740 ASSERT_TRUE(db.AddFaviconBitmap(favicon_id2, favicon, base::Time::Now(), | |
| 741 gfx::Size())); | |
| 742 IconMappingID favicon_mapping_id2 = db.AddIconMapping(url2, favicon_id2); | 778 IconMappingID favicon_mapping_id2 = db.AddIconMapping(url2, favicon_id2); |
| 743 ASSERT_NE(0, favicon_mapping_id2); | 779 ASSERT_NE(0, favicon_mapping_id2); |
| 744 | 780 |
| 745 IconMapping icon_mapping; | 781 IconMapping icon_mapping; |
| 746 ThumbnailDatabase::IconMappingEnumerator enumerator1; | 782 ThumbnailDatabase::IconMappingEnumerator enumerator1; |
| 747 ASSERT_TRUE(db.InitIconMappingEnumerator(FAVICON, &enumerator1)); | 783 ASSERT_TRUE(db.InitIconMappingEnumerator(FAVICON, &enumerator1)); |
| 748 // There are 2 favicon mappings. | 784 // There are 2 favicon mappings. |
| 749 bool has_favicon_mapping1 = false; | 785 bool has_favicon_mapping1 = false; |
| 750 bool has_favicon_mapping2 = false; | 786 bool has_favicon_mapping2 = false; |
| 751 int mapping_count = 0; | 787 int mapping_count = 0; |
| 752 while (enumerator1.GetNextIconMapping(&icon_mapping)) { | 788 while (enumerator1.GetNextIconMapping(&icon_mapping)) { |
| 753 mapping_count++; | 789 mapping_count++; |
| 754 if (favicon_mapping_id1 == icon_mapping.mapping_id) { | 790 if (favicon_mapping_id1 == icon_mapping.mapping_id) { |
| 755 has_favicon_mapping1 = true; | 791 has_favicon_mapping1 = true; |
| 756 EXPECT_EQ(url, icon_mapping.page_url); | 792 EXPECT_EQ(url, icon_mapping.page_url); |
| 757 EXPECT_EQ(favicon_id1, icon_mapping.icon_id); | 793 EXPECT_EQ(favicon_id1, icon_mapping.icon_id); |
| 794 EXPECT_EQ(icon_url1, icon_mapping.icon_url); |
| 758 EXPECT_EQ(FAVICON, icon_mapping.icon_type); | 795 EXPECT_EQ(FAVICON, icon_mapping.icon_type); |
| 759 } else if (favicon_mapping_id2 == icon_mapping.mapping_id) { | 796 } else if (favicon_mapping_id2 == icon_mapping.mapping_id) { |
| 760 has_favicon_mapping2 = true; | 797 has_favicon_mapping2 = true; |
| 761 EXPECT_EQ(url2, icon_mapping.page_url); | 798 EXPECT_EQ(url2, icon_mapping.page_url); |
| 762 EXPECT_EQ(favicon_id2, icon_mapping.icon_id); | 799 EXPECT_EQ(favicon_id2, icon_mapping.icon_id); |
| 800 EXPECT_EQ(icon_url2, icon_mapping.icon_url); |
| 763 EXPECT_EQ(FAVICON, icon_mapping.icon_type); | 801 EXPECT_EQ(FAVICON, icon_mapping.icon_type); |
| 764 } | 802 } |
| 765 } | 803 } |
| 766 EXPECT_EQ(2, mapping_count); | 804 EXPECT_EQ(2, mapping_count); |
| 767 EXPECT_TRUE(has_favicon_mapping1); | 805 EXPECT_TRUE(has_favicon_mapping1); |
| 768 EXPECT_TRUE(has_favicon_mapping2); | 806 EXPECT_TRUE(has_favicon_mapping2); |
| 769 | 807 |
| 770 ThumbnailDatabase::IconMappingEnumerator enumerator2; | 808 ThumbnailDatabase::IconMappingEnumerator enumerator2; |
| 771 ASSERT_TRUE(db.InitIconMappingEnumerator(TOUCH_ICON, &enumerator2)); | 809 ASSERT_TRUE(db.InitIconMappingEnumerator(TOUCH_ICON, &enumerator2)); |
| 772 ASSERT_TRUE(enumerator2.GetNextIconMapping(&icon_mapping)); | 810 ASSERT_TRUE(enumerator2.GetNextIconMapping(&icon_mapping)); |
| 773 EXPECT_EQ(touch_mapping_id1, icon_mapping.mapping_id); | 811 EXPECT_EQ(touch_mapping_id1, icon_mapping.mapping_id); |
| 774 EXPECT_EQ(url, icon_mapping.page_url); | 812 EXPECT_EQ(url, icon_mapping.page_url); |
| 775 EXPECT_EQ(touch_icon_id1, icon_mapping.icon_id); | 813 EXPECT_EQ(touch_icon_id1, icon_mapping.icon_id); |
| 814 EXPECT_EQ(icon_url1, icon_mapping.icon_url); |
| 776 EXPECT_EQ(TOUCH_ICON, icon_mapping.icon_type); | 815 EXPECT_EQ(TOUCH_ICON, icon_mapping.icon_type); |
| 777 | 816 |
| 778 EXPECT_FALSE(enumerator2.GetNextIconMapping(&icon_mapping)); | 817 EXPECT_FALSE(enumerator2.GetNextIconMapping(&icon_mapping)); |
| 779 } | 818 } |
| 780 | 819 |
| 820 TEST_F(ThumbnailDatabaseTest, FaviconSizesToAndFromString) { |
| 821 // Invalid input. |
| 822 FaviconSizes sizes_missing_height; |
| 823 EXPECT_FALSE(ThumbnailDatabase::DatabaseStringToFaviconSizes("0 0 10", |
| 824 &sizes_missing_height)); |
| 825 EXPECT_EQ(0u, sizes_missing_height.size()); |
| 826 |
| 827 FaviconSizes sizes_non_int; |
| 828 EXPECT_FALSE(ThumbnailDatabase::DatabaseStringToFaviconSizes("0 0 a 10", |
| 829 &sizes_non_int)); |
| 830 EXPECT_EQ(0u, sizes_missing_height.size()); |
| 831 |
| 832 // Valid input. |
| 833 FaviconSizes sizes_empty; |
| 834 EXPECT_TRUE(ThumbnailDatabase::DatabaseStringToFaviconSizes("", |
| 835 &sizes_empty)); |
| 836 EXPECT_EQ(0u, sizes_empty.size()); |
| 837 |
| 838 FaviconSizes sizes_valid; |
| 839 EXPECT_TRUE(ThumbnailDatabase::DatabaseStringToFaviconSizes("10 15 20 25", |
| 840 &sizes_valid)); |
| 841 EXPECT_EQ(2u, sizes_valid.size()); |
| 842 if (sizes_valid[0] == gfx::Size(10, 15)) { |
| 843 EXPECT_EQ(sizes_valid[1], gfx::Size(20, 25)); |
| 844 } else { |
| 845 EXPECT_EQ(sizes_valid[0], gfx::Size(20, 25)); |
| 846 EXPECT_EQ(sizes_valid[1], gfx::Size(10, 15)); |
| 847 } |
| 848 |
| 849 std::string sizes_as_string; |
| 850 ThumbnailDatabase::FaviconSizesToDatabaseString(sizes_valid, |
| 851 &sizes_as_string); |
| 852 EXPECT_TRUE(sizes_as_string == "10 15 20 25" || |
| 853 sizes_as_string == "20 25 10 15"); |
| 854 } |
| 855 |
| 781 } // namespace history | 856 } // namespace history |
| OLD | NEW |