Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Unified Diff: chrome/browser/history/history_backend_unittest.cc

Issue 10802066: Adds support for saving favicon size into history database. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/history/history_backend_unittest.cc
diff --git a/chrome/browser/history/history_backend_unittest.cc b/chrome/browser/history/history_backend_unittest.cc
index 35b4fd9965432a8ca93f1f210edb64b9ee01e71a..e803ae73af26c9e0eb09b05bba8fed976ebf0321 100644
--- a/chrome/browser/history/history_backend_unittest.cc
+++ b/chrome/browser/history/history_backend_unittest.cc
@@ -48,6 +48,9 @@ static const unsigned char blob1[] =
static const gfx::Size kSmallSize = gfx::Size(16, 16);
static const gfx::Size kLargeSize = gfx::Size(48, 48);
+const std::string kSizesSmall = "16 16";
+const std::string kSizesSmallAndLarge = "16 16 48 48";
+
} // namepace
namespace history {
@@ -201,13 +204,109 @@ class HistoryBackendTest : public testing::Test {
return test_dir_;
}
- FaviconID GetFavicon(const GURL& url, IconType icon_type) {
- IconMapping icon_mapping;
- if (backend_->thumbnail_db_->GetIconMappingForPageURL(url, icon_type,
- &icon_mapping))
- return icon_mapping.icon_id;
- else
- return 0;
+ // Returns the number of icon mappings of |icon_type| to |page_url|.
+ size_t NumIconMappingsForPageURL(const GURL& page_url, IconType icon_type) {
+ std::vector<IconMapping> icon_mappings;
+ if (backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url, icon_type,
+ &icon_mappings)) {
+ return icon_mappings.size();
+ }
+ return 0u;
+ }
+
+ // Returns true if there is exactly one favicon bitmap associated to
+ // |icon_url|. If true, returns favicon bitmap in output parameter.
+ bool GetOnlyFaviconBitmapForIconURL(const GURL& icon_url,
+ IconType icon_type,
+ FaviconBitmap* favicon_bitmap) {
+ FaviconID id = backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
+ icon_url, icon_type);
+ if (!id)
+ return false;
+ std::vector<FaviconBitmap> favicon_bitmaps;
+ if (!backend_->thumbnail_db_->GetFaviconBitmaps(id, &favicon_bitmaps))
+ return false;
+ if (favicon_bitmaps.size() != 1)
+ return false;
+ *favicon_bitmap = favicon_bitmaps[0];
+ return true;
+ }
+
+ // Deletes favicons and favicon bitmaps for |page_url| and |icon_type|.
+ bool DeleteFaviconsAndFaviconBitmaps(const GURL& page_url,
+ IconType icon_type) {
+ backend_->SetFavicons(page_url,
+ icon_type,
+ std::vector<FaviconDataElement>(),
+ IconURLSizesMap());
+ return !backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url,
+ icon_type, NULL);
+ }
+
+ // Sets the favicons for |page_url| and |icon_type|. This is very similar to
+ // HistoryBackend::SetFavicons except that this call will delete favicon
+ // bitmaps which are not in |elements|. This allows setting a favicon state
+ // where a favicon bitmap can be guaranteed to be only specified via sizes and
+ // not have any associated bitmap data in the favicon_bitmaps table.
+ void SetFaviconsState(const GURL& page_url,
+ IconType icon_type,
+ const std::vector<FaviconDataElement>& elements,
+ const IconURLSizesMap& icon_url_sizes) {
+ EXPECT_TRUE(DeleteFaviconsAndFaviconBitmaps(page_url, icon_type));
+ backend_->SetFavicons(page_url, icon_type, elements, icon_url_sizes);
+ }
+
+ // Sets |elements| to have entries for the icon_urls and sizes specified.
+ // The bitmap_data for entries are lowercase letters of the alphabet starting
+ // at 'a' for the entry at index 0.
+ void SetFaviconDataElements(const GURL& icon_url1,
+ const FaviconSizes& icon_url1_sizes,
+ std::vector<FaviconDataElement>* elements) {
+ SetFaviconDataElements(icon_url1, icon_url1_sizes, GURL(), FaviconSizes(),
+ elements);
+ }
+
+ void SetFaviconDataElements(const GURL& icon_url1,
+ const FaviconSizes& icon_url1_sizes,
+ const GURL& icon_url2,
+ const FaviconSizes& icon_url2_sizes,
+ std::vector<FaviconDataElement>* elements) {
+ elements->clear();
+
+ char bitmap_char = 'a';
+ std::vector<gfx::Size> icon_url1_sizes_vector = icon_url1_sizes.ToVector();
+ for (size_t i = 0; i < icon_url1_sizes_vector.size(); ++i) {
+ std::vector<unsigned char> data;
+ data.push_back(bitmap_char);
+ FaviconDataElement element;
+ element.bitmap_data = base::RefCountedBytes::TakeVector(&data);
+ element.pixel_size = icon_url1_sizes_vector[i];
+ element.icon_url = icon_url1;
+ elements->push_back(element);
+
+ ++bitmap_char;
+ }
+
+ std::vector<gfx::Size> icon_url2_sizes_vector = icon_url2_sizes.ToVector();
+ for (size_t i = 0; i < icon_url2_sizes_vector.size(); ++i) {
+ std::vector<unsigned char> data;
+ data.push_back(bitmap_char);
+ FaviconDataElement element;
+ element.bitmap_data = base::RefCountedBytes::TakeVector(&data);
+ element.pixel_size = icon_url2_sizes_vector[i];
+ element.icon_url = icon_url2;
+ elements->push_back(element);
+
+ ++bitmap_char;
+ }
+ }
+
+ // Returns true if |bitmap_data| is equal to |expected_data|.
+ bool BitmapDataEqual(char expected_data,
+ scoped_refptr<base::RefCountedMemory> bitmap_data) {
+ return bitmap_data.get() &&
+ bitmap_data->size() == 1u &&
+ *bitmap_data->front() == expected_data;
}
BookmarkModel bookmark_model_;
@@ -228,7 +327,9 @@ class HistoryBackendTest : public testing::Test {
new HistoryBackendTestDelegate(this),
&bookmark_model_);
backend_->Init(std::string(), false);
+ backend_->set_single_favicon_bitmap_per_icon_type(false);
}
+
virtual void TearDown() {
if (backend_.get())
backend_->Closing();
@@ -413,7 +514,7 @@ TEST_F(HistoryBackendTest, DeleteAll) {
// We should have a favicon and favicon bitmaps for the first URL only. We
// look them up by favicon URL since the IDs may have changed.
FaviconID out_favicon1 = backend_->thumbnail_db_->
- GetFaviconIDForFaviconURL(favicon_url1, FAVICON, NULL);
+ GetFaviconIDForFaviconURL(favicon_url1, FAVICON);
EXPECT_TRUE(out_favicon1);
std::vector<FaviconBitmap> favicon_bitmaps;
@@ -424,26 +525,30 @@ TEST_F(HistoryBackendTest, DeleteAll) {
FaviconBitmap favicon_bitmap1 = favicon_bitmaps[0];
FaviconBitmap favicon_bitmap2 = favicon_bitmaps[1];
- // Bitmaps do not need to be in particular order.
+ // Favicon bitmaps do not need to be in particular order.
if (favicon_bitmap1.pixel_size == kLargeSize) {
FaviconBitmap tmp_favicon_bitmap = favicon_bitmap1;
favicon_bitmap1 = favicon_bitmap2;
favicon_bitmap2 = tmp_favicon_bitmap;
}
- EXPECT_EQ('a', *favicon_bitmap1.bitmap_data->front());
+ EXPECT_TRUE(BitmapDataEqual('a', favicon_bitmap1.bitmap_data));
EXPECT_EQ(kSmallSize, favicon_bitmap1.pixel_size);
- EXPECT_EQ('b', *favicon_bitmap2.bitmap_data->front());
+ EXPECT_TRUE(BitmapDataEqual('b', favicon_bitmap2.bitmap_data));
EXPECT_EQ(kLargeSize, favicon_bitmap2.pixel_size);
FaviconID out_favicon2 = backend_->thumbnail_db_->
- GetFaviconIDForFaviconURL(favicon_url2, FAVICON, NULL);
+ GetFaviconIDForFaviconURL(favicon_url2, FAVICON);
EXPECT_FALSE(out_favicon2) << "Favicon not deleted";
// The remaining URL should still reference the same favicon, even if its
// ID has changed.
- EXPECT_EQ(out_favicon1, GetFavicon(outrow1.url(), FAVICON));
+ std::vector<IconMapping> mappings;
+ EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
+ outrow1.url(), FAVICON, &mappings));
+ EXPECT_EQ(1u, mappings.size());
+ EXPECT_EQ(out_favicon1, mappings[0].icon_id);
// The first URL should still be bookmarked.
EXPECT_TRUE(bookmark_model_.IsBookmarked(row1.url()));
@@ -573,8 +678,7 @@ TEST_F(HistoryBackendTest, URLsNoLongerBookmarked) {
// The favicon should still be valid.
EXPECT_EQ(favicon2,
backend_->thumbnail_db_->GetFaviconIDForFaviconURL(favicon_url2,
- FAVICON,
- NULL));
+ FAVICON));
// Unstar row2.
bookmark_utils::RemoveAllBookmarks(&bookmark_model_, row2.url());
@@ -590,8 +694,7 @@ TEST_F(HistoryBackendTest, URLsNoLongerBookmarked) {
// And the favicon should be deleted.
EXPECT_EQ(0,
backend_->thumbnail_db_->GetFaviconIDForFaviconURL(favicon_url2,
- FAVICON,
- NULL));
+ FAVICON));
// Unstar row 1.
bookmark_utils::RemoveAllBookmarks(&bookmark_model_, row1.url());
@@ -612,8 +715,7 @@ TEST_F(HistoryBackendTest, URLsNoLongerBookmarked) {
// The favicon should still be valid.
EXPECT_EQ(favicon1,
backend_->thumbnail_db_->GetFaviconIDForFaviconURL(favicon_url1,
- FAVICON,
- NULL));
+ FAVICON));
}
// Tests a handful of assertions for a navigation with a type of
@@ -723,8 +825,8 @@ TEST_F(HistoryBackendTest, ImportedFaviconsTest) {
URLRow url_row1, url_row2;
EXPECT_FALSE(backend_->db_->GetRowForURL(row1.url(), &url_row1) == 0);
EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &url_row2) == 0);
- EXPECT_FALSE(GetFavicon(row1.url(), FAVICON) == 0);
- EXPECT_TRUE(GetFavicon(row2.url(), FAVICON) == 0);
+ EXPECT_EQ(1u, NumIconMappingsForPageURL(row1.url(), FAVICON));
+ EXPECT_EQ(0u, NumIconMappingsForPageURL(row2.url(), FAVICON));
// Now provide one imported favicon for both URLs already in the registry.
// The new favicon should only be used with the URL that doesn't already have
@@ -739,10 +841,19 @@ TEST_F(HistoryBackendTest, ImportedFaviconsTest) {
backend_->SetImportedFavicons(favicons);
EXPECT_FALSE(backend_->db_->GetRowForURL(row1.url(), &url_row1) == 0);
EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &url_row2) == 0);
- EXPECT_FALSE(GetFavicon(row1.url(), FAVICON) == 0);
- EXPECT_FALSE(GetFavicon(row2.url(), FAVICON) == 0);
- EXPECT_FALSE(GetFavicon(row1.url(), FAVICON) ==
- GetFavicon(row2.url(), FAVICON));
+
+ std::vector<IconMapping> mappings;
+ EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
+ row1.url(), FAVICON, &mappings));
+ EXPECT_EQ(1u, mappings.size());
+ EXPECT_EQ(favicon1, mappings[0].icon_id);
+ EXPECT_EQ(favicon_url1, mappings[0].icon_url);
+
+ mappings.clear();
+ EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
+ row2.url(), FAVICON, &mappings));
+ EXPECT_EQ(1u, mappings.size());
+ EXPECT_EQ(favicon.favicon_url, mappings[0].icon_url);
// A URL should not be added to history (to store favicon), if
// the URL is not bookmarked.
@@ -1099,7 +1210,9 @@ TEST_F(HistoryBackendTest, MigrationVisitSource) {
EXPECT_FALSE(s.Step());
}
-TEST_F(HistoryBackendTest, SetFaviconMapping) {
+// Test that SetFaviconMappingsForPageAndRedirects correctly updates icon
+// mappings based on redirects, icon URLs and icon types.
+TEST_F(HistoryBackendTest, SetFaviconMappingsForPageAndRedirects) {
// Init recent_redirects_
const GURL url1("http://www.google.com");
const GURL url2("http://www.google.com/m");
@@ -1122,165 +1235,501 @@ TEST_F(HistoryBackendTest, SetFaviconMapping) {
redirects.push_back(url1);
backend_->recent_redirects_.Put(url1, redirects);
- const GURL icon_url("http://www.google.com/icon");
- std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1));
- // Add a favicon
- backend_->SetFavicon(
- url1, icon_url, base::RefCountedBytes::TakeVector(&data), FAVICON);
- EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL(
- url1, FAVICON, NULL));
- EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL(
- url2, FAVICON, NULL));
-
- // Add a touch_icon
- backend_->SetFavicon(
- url1, icon_url, base::RefCountedBytes::TakeVector(&data), TOUCH_ICON);
- EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL(
- url1, TOUCH_ICON, NULL));
- EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL(
- url2, TOUCH_ICON, NULL));
- EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL(
- url1, FAVICON, NULL));
-
- // Add a TOUCH_PRECOMPOSED_ICON
- backend_->SetFavicon(url1,
- icon_url,
- base::RefCountedBytes::TakeVector(&data),
- TOUCH_PRECOMPOSED_ICON);
+ const GURL icon_url1("http://www.google.com/icon");
+ const GURL icon_url2("http://www.google.com/icon2");
+
+ // Create mapping for a page with two favicons.
+ IconURLSizesMap two_icon_url_sizes;
+ two_icon_url_sizes[icon_url1] = FaviconSizes(kSizesSmallAndLarge);
+ two_icon_url_sizes[icon_url2] = FaviconSizes(kSizesSmallAndLarge);
+
+ // Create a mapping for a page with a single favicon.
+ IconURLSizesMap one_icon_url_sizes;
+ one_icon_url_sizes[icon_url1] = FaviconSizes(kSizesSmallAndLarge);
+
+ std::vector<FaviconDataElement> elements;
+
+ // Add two favicons
+ backend_->SetFavicons(url1, FAVICON, elements, two_icon_url_sizes);
+ EXPECT_EQ(2u, NumIconMappingsForPageURL(url1, FAVICON));
+ EXPECT_EQ(2u, NumIconMappingsForPageURL(url2, FAVICON));
+
+ // Add one touch_icon
+ backend_->SetFavicons(url1, TOUCH_ICON, elements, one_icon_url_sizes);
+ EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, TOUCH_ICON));
+ EXPECT_EQ(1u, NumIconMappingsForPageURL(url2, TOUCH_ICON));
+ EXPECT_EQ(2u, NumIconMappingsForPageURL(url1, FAVICON));
+
+ // Add one TOUCH_PRECOMPOSED_ICON
+ backend_->SetFavicons(url1, TOUCH_PRECOMPOSED_ICON, elements,
+ one_icon_url_sizes);
// The touch_icon was replaced.
- EXPECT_FALSE(backend_->thumbnail_db_->GetIconMappingForPageURL(
- url1, TOUCH_ICON, NULL));
- EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL(
- url1, FAVICON, NULL));
- EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL(
- url1, TOUCH_PRECOMPOSED_ICON, NULL));
- EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL(
- url2, TOUCH_PRECOMPOSED_ICON, NULL));
-
- // Add a touch_icon
- backend_->SetFavicon(
- url1, icon_url, base::RefCountedBytes::TakeVector(&data), TOUCH_ICON);
- EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL(
- url1, TOUCH_ICON, NULL));
- EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL(
- url1, FAVICON, NULL));
+ EXPECT_EQ(0u, NumIconMappingsForPageURL(url1, TOUCH_ICON));
+ EXPECT_EQ(2u, NumIconMappingsForPageURL(url1, FAVICON));
+ EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, TOUCH_PRECOMPOSED_ICON));
+ EXPECT_EQ(1u, NumIconMappingsForPageURL(url2, TOUCH_PRECOMPOSED_ICON));
+
+ // Add a touch_icon.
+ backend_->SetFavicons(url1, TOUCH_ICON, elements, one_icon_url_sizes);
+ EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, TOUCH_ICON));
+ EXPECT_EQ(2u, NumIconMappingsForPageURL(url1, FAVICON));
// The TOUCH_PRECOMPOSED_ICON was replaced.
- EXPECT_FALSE(backend_->thumbnail_db_->GetIconMappingForPageURL(
- url1, TOUCH_PRECOMPOSED_ICON, NULL));
-
- // Add a favicon
- const GURL icon_url2("http://www.google.com/icon2");
- backend_->SetFavicon(
- url1, icon_url2, base::RefCountedBytes::TakeVector(&data), FAVICON);
- FaviconID icon_id = backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
- icon_url2, FAVICON, NULL);
- EXPECT_NE(0, icon_id);
- std::vector<IconMapping> icon_mapping;
+ EXPECT_EQ(0u, NumIconMappingsForPageURL(url1, TOUCH_PRECOMPOSED_ICON));
+
+ // Add a single favicon.
+ backend_->SetFavicons(url1, FAVICON, elements, one_icon_url_sizes);
+ // The favicon of type FAVICON for |icon_url2| should have no more mappings
+ // and should have been deleted.
+ EXPECT_EQ(0, backend_->thumbnail_db_->GetFaviconIDForFaviconURL(icon_url2,
+ FAVICON));
+ // The remaining mapping should be |icon_url1|.
+ std::vector<IconMapping> icon_mappings;
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
- url1, &icon_mapping));
- // The old icon was replaced.
- EXPECT_TRUE(icon_mapping.size() > 1);
- EXPECT_EQ(icon_id, icon_mapping[1].icon_id);
+ url1, FAVICON, &icon_mappings));
+ EXPECT_EQ(1u, icon_mappings.size());
+ EXPECT_EQ(icon_url1, icon_mappings[0].icon_url);
+
+ // Add two favicons.
+ backend_->SetFavicons(url1, FAVICON, elements, two_icon_url_sizes);
+ EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, TOUCH_ICON));
+ EXPECT_EQ(2u, NumIconMappingsForPageURL(url1, FAVICON));
}
-TEST_F(HistoryBackendTest, AddOrUpdateIconMapping) {
- // Test the same icon and page mapping will not be added twice. other case
- // should be covered in TEST_F(HistoryBackendTest, SetFaviconMapping)
+// Test that there is no churn in icon mappings from calling
+// SetFaviconMappingsForPage() with icon mappings which are already in the
+// database.
+TEST_F(HistoryBackendTest, SetFaviconMappingsForPageDuplicates) {
const GURL url("http://www.google.com/");
const GURL icon_url("http://www.google.com/icon");
- std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1));
+ std::vector<FaviconDataElement> elements;
+
+ IconURLSizesMap icon_url_sizes;
+ icon_url_sizes[icon_url] = FaviconSizes(kSizesSmallAndLarge);
+
+ backend_->SetFavicons(url, FAVICON, elements, icon_url_sizes);
- backend_->SetFavicon(
- url, icon_url, base::RefCountedBytes::TakeVector(&data), FAVICON);
- FaviconID icon_id = backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
- icon_url, FAVICON, NULL);
+ std::vector<IconMapping> icon_mappings;
+ EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
+ url, FAVICON, &icon_mappings));
+ EXPECT_EQ(1u, icon_mappings.size());
+ IconMappingID mapping_id = icon_mappings[0].mapping_id;
+ GURL favicon_url = icon_mappings[0].icon_url;
+ FaviconID favicon_id = icon_mappings[0].icon_id;
+
+ HistoryBackend::IconURLFaviconIDMap icon_url_ids;
+ icon_url_ids[favicon_url] = favicon_id;
- // Add the same mapping
- FaviconID replaced;
- EXPECT_FALSE(backend_->AddOrUpdateIconMapping(
- url, icon_id, FAVICON, &replaced));
- EXPECT_EQ(0, replaced);
+ // False should be returned as no mappings should have changed.
+ EXPECT_FALSE(backend_->SetFaviconMappingsForPage(url, FAVICON, icon_url_ids));
- std::vector<IconMapping> icon_mapping;
+ icon_mappings.clear();
EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
- url, &icon_mapping));
- EXPECT_EQ(1u, icon_mapping.size());
+ url, FAVICON, &icon_mappings));
+ EXPECT_EQ(1u, icon_mappings.size());
+
+ // The same row in the icon_mapping table should be used for the mapping as
+ // before.
+ EXPECT_EQ(mapping_id, icon_mappings[0].mapping_id);
}
-TEST_F(HistoryBackendTest, GetFaviconForURL) {
- // This test will add a fav icon and touch icon for the same URL
- // and check the behaviour of backend's GetFaviconForURL implementation.
- const GURL url("http://www.google.com/");
+// Test that setting favicons for a page which already has data does the
+// right thing.
+TEST_F(HistoryBackendTest, SetFavicons) {
+ const GURL page_url("http://www.google.com/");
+ std::vector<FaviconDataElement> elements;
+ IconURLSizesMap icon_url_sizes;
+
+ EXPECT_TRUE(DeleteFaviconsAndFaviconBitmaps(page_url, FAVICON));
+
+ // Set |page_url| as having two favicons each available from the web at two
+ // sizes.
+ const GURL icon_url1("http://www.google.com/icon1");
+ const GURL icon_url2("http://www.google.com/icon2");
+
+ icon_url_sizes[icon_url1] = FaviconSizes(kSizesSmallAndLarge);
+ icon_url_sizes[icon_url2] = FaviconSizes(kSizesSmallAndLarge);
+
+ // Set only sizes info for the favicons.
+ backend_->SetFavicons(page_url, FAVICON, elements, icon_url_sizes);
+
+ std::vector<IconMapping> icon_mappings;
+ EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
+ page_url, &icon_mappings));
+ EXPECT_EQ(2u, icon_mappings.size());
+ for (size_t i = 0; i < icon_mappings.size(); ++i) {
+ EXPECT_FALSE(backend_->thumbnail_db_->GetFaviconBitmaps(
+ icon_mappings[i].icon_id, NULL));
+ }
+
+ // Add bitmap data to the favicons.
+ SetFaviconDataElements(icon_url1,
+ FaviconSizes(kSizesSmall),
+ icon_url2,
+ FaviconSizes(kSizesSmallAndLarge),
+ &elements);
+
+ backend_->SetFavicons(page_url, FAVICON, elements, icon_url_sizes);
+
+ icon_mappings.clear();
+ EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
+ page_url, &icon_mappings));
+ EXPECT_EQ(2u, icon_mappings.size());
+ IconMapping mapping1 = icon_mappings[0];
+ IconMapping mapping2 = icon_mappings[1];
+
+ // Icon mappings do not need to be returned in particular order from database
+ // with respect to icon url.
+ if (mapping1.icon_url == icon_url2) {
+ IconMapping tmp_mapping = mapping1;
+ mapping1 = mapping2;
+ mapping2 = tmp_mapping;
+ }
+
+ GURL icon_url;
+ IconType icon_type;
+ std::string sizes;
+ EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconHeader(mapping1.icon_id,
+ &icon_url, &icon_type, &sizes));
+ EXPECT_EQ(icon_url1, icon_url);
+ EXPECT_EQ(FAVICON, icon_type);
+ EXPECT_EQ(kSizesSmallAndLarge, sizes);
+
+ std::vector<FaviconBitmap> favicon_bitmaps;
+ EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(mapping1.icon_id,
+ &favicon_bitmaps));
+ EXPECT_EQ(1u, favicon_bitmaps.size());
+ EXPECT_TRUE(BitmapDataEqual('a', favicon_bitmaps[0].bitmap_data));
+ EXPECT_EQ(kSmallSize, favicon_bitmaps[0].pixel_size);
+
+ EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconHeader(mapping2.icon_id,
+ &icon_url, &icon_type, &sizes));
+ EXPECT_EQ(icon_url2, icon_url);
+ EXPECT_EQ(FAVICON, icon_type);
+ EXPECT_EQ(kSizesSmallAndLarge, sizes);
+
+ favicon_bitmaps.clear();
+ EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(mapping2.icon_id,
+ &favicon_bitmaps));
+
+ EXPECT_EQ(2u, favicon_bitmaps.size());
+
+ // Favicon bitmaps do not need to be in particular order.
+ if (favicon_bitmaps[0].pixel_size == kSmallSize) {
+ EXPECT_TRUE(BitmapDataEqual('b', favicon_bitmaps[0].bitmap_data));
+ EXPECT_EQ(kLargeSize, favicon_bitmaps[1].pixel_size);
+ EXPECT_TRUE(BitmapDataEqual('c', favicon_bitmaps[1].bitmap_data));
+ } else {
+ EXPECT_EQ(kLargeSize, favicon_bitmaps[0].pixel_size);
+ EXPECT_TRUE(BitmapDataEqual('c', favicon_bitmaps[0].bitmap_data));
+ EXPECT_EQ(kSmallSize, favicon_bitmaps[1].pixel_size);
+ EXPECT_TRUE(BitmapDataEqual('b', favicon_bitmaps[1].bitmap_data));
+ }
+
+ // Change the sizes for which the favicon at icon_url1 is available at from
+ // the web. Verify that all the data remains valid.
+ const std::string kSizesTinySmallAndLarge = "10 10 " + kSizesSmallAndLarge;
+ icon_url_sizes[icon_url1] = FaviconSizes(kSizesTinySmallAndLarge);
+ elements.clear();
+ backend_->SetFavicons(page_url, FAVICON, elements, icon_url_sizes);
+
+ icon_mappings.clear();
+ EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
+ page_url, &icon_mappings));
+ EXPECT_EQ(2u, icon_mappings.size());
+ mapping1 = icon_mappings[0];
+ mapping2 = icon_mappings[1];
+
+ // Icon mappings do not need to be returned in particular order from database
+ // with respect to icon urls.
+ if (mapping1.icon_url == icon_url2) {
+ IconMapping tmp_mapping = mapping1;
+ mapping1 = mapping2;
+ mapping2 = tmp_mapping;
+ }
+
+ EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconHeader(mapping1.icon_id,
+ &icon_url, &icon_type, &sizes));
+ EXPECT_EQ(icon_url1, icon_url);
+ EXPECT_EQ(FAVICON, icon_type);
+ EXPECT_EQ(kSizesTinySmallAndLarge, sizes);
+
+ favicon_bitmaps.clear();
+ EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(mapping1.icon_id,
+ &favicon_bitmaps));
+ EXPECT_EQ(1u, favicon_bitmaps.size());
+
+ EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconHeader(mapping2.icon_id,
+ &icon_url, &icon_type, &sizes));
+ EXPECT_EQ(icon_url2, icon_url);
+ EXPECT_EQ(FAVICON, icon_type);
+ EXPECT_EQ(kSizesSmallAndLarge, sizes);
+
+ favicon_bitmaps.clear();
+ EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(mapping2.icon_id,
+ &favicon_bitmaps));
+ EXPECT_EQ(2u, favicon_bitmaps.size());
+}
+
+// Test that changing the sizes that a favicon is available at from the web
+// deletes stale favicon bitmaps.
+TEST_F(HistoryBackendTest, SetFaviconsDeleteBitmaps) {
+ const GURL page_url("http://www.google.com/");
const GURL icon_url("http://www.google.com/icon");
- std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1));
- scoped_refptr<base::RefCountedBytes> bytes(new base::RefCountedBytes(data));
- // Used for testing the icon data after getting from DB
- std::string blob_data(bytes->front(),
- bytes->front() + bytes->size());
-
- // Add a favicon
- backend_->SetFavicon(
- url, icon_url, bytes.get(), FAVICON);
- EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL(
- url, FAVICON, NULL));
-
- // Add a touch_icon
- backend_->SetFavicon(
- url, icon_url, bytes.get(), TOUCH_ICON);
- EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL(
- url, TOUCH_ICON, NULL));
-
- // Test the Fav icon for this URL.
- FaviconData favicon;
- ASSERT_TRUE(backend_->GetFaviconFromDB(url, FAVICON, &favicon));
- std::string favicon_data(
- favicon.image_data->front(),
- favicon.image_data->front() + favicon.image_data->size());
-
- EXPECT_EQ(FAVICON, favicon.icon_type);
- EXPECT_EQ(icon_url, favicon.icon_url);
- EXPECT_EQ(blob_data, favicon_data);
-
- // Test the touch icon for this URL.
- ASSERT_TRUE(backend_->GetFaviconFromDB(url, TOUCH_ICON, &favicon));
- std::string touchicon_data(
- favicon.image_data->front(),
- favicon.image_data->front() + favicon.image_data->size());
-
- EXPECT_EQ(TOUCH_ICON, favicon.icon_type);
- EXPECT_EQ(icon_url, favicon.icon_url);
- EXPECT_EQ(blob_data, touchicon_data);
+
+ // Set |page_url| as having one favicon with two different sizes.
+ IconURLSizesMap icon_url_sizes;
+ icon_url_sizes[icon_url] = FaviconSizes(kSizesSmallAndLarge);
+
+ std::vector<FaviconDataElement> elements;
+ SetFaviconDataElements(icon_url, FaviconSizes(kSizesSmallAndLarge),
+ &elements);
+
+ // Add bitmap data and sizes information to the database.
+ backend_->SetFavicons(page_url, FAVICON, elements, icon_url_sizes);
+
+ FaviconID favicon_id = backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
+ icon_url, FAVICON);
+ EXPECT_NE(0, favicon_id);
+
+ std::vector<FaviconBitmap> favicon_bitmaps;
+ EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(favicon_id,
+ &favicon_bitmaps));
+ EXPECT_EQ(2u, favicon_bitmaps.size());
+
+ // Change the bitmap sizes avaible from the web only to the small size only.
+ icon_url_sizes[icon_url] = FaviconSizes(kSizesSmall);
+ elements.clear();
+ backend_->SetFavicons(page_url, FAVICON, elements, icon_url_sizes);
+
+ favicon_id = backend_->thumbnail_db_->GetFaviconIDForFaviconURL(icon_url,
+ FAVICON);
+ EXPECT_NE(0, favicon_id);
+
+ favicon_bitmaps.clear();
+ EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(favicon_id,
+ &favicon_bitmaps));
+ EXPECT_EQ(1u, favicon_bitmaps.size());
+ EXPECT_EQ(kSmallSize, favicon_bitmaps[0].pixel_size);
+}
+
+// Test updating a single favicon bitmap's data via SetFavicons.
+TEST_F(HistoryBackendTest, SetFaviconsReplaceBitmapData) {
+
+ const GURL page_url("http://www.google.com/");
+ const GURL icon_url("http://www.google.com/icon");
+ IconURLSizesMap icon_url_sizes;
+ icon_url_sizes[icon_url] = FaviconSizes(kSizesSmall);
+
+ std::vector<unsigned char> data_initial;
+ data_initial.push_back('a');
+
+ FaviconDataElement element;
+ element.bitmap_data = base::RefCountedBytes::TakeVector(&data_initial);
+ element.pixel_size = kSmallSize;
+ element.icon_url = icon_url;
+ std::vector<FaviconDataElement> elements;
+ elements.push_back(element);
+
+ // Add bitmap to the database.
+ backend_->SetFavicons(page_url, FAVICON, elements, icon_url_sizes);
+
+ FaviconBitmap favicon_bitmap;
+ EXPECT_TRUE(GetOnlyFaviconBitmapForIconURL(icon_url, FAVICON,
+ &favicon_bitmap));
+ EXPECT_TRUE(BitmapDataEqual('a', favicon_bitmap.bitmap_data));
+
+ // SetFavicons with identical data but a different bitmap.
+ std::vector<unsigned char> updated_data;
+ updated_data.push_back('b');
+ elements[0].bitmap_data = base::RefCountedBytes::TakeVector(&updated_data);
+ backend_->SetFavicons(page_url, FAVICON, elements, icon_url_sizes);
+
+ EXPECT_TRUE(GetOnlyFaviconBitmapForIconURL(icon_url, FAVICON,
+ &favicon_bitmap));
+ EXPECT_TRUE(BitmapDataEqual('b', favicon_bitmap.bitmap_data));
+}
+
+// Test GetFaviconsFromDB returns expected data in FaviconData.
+TEST_F(HistoryBackendTest, GetFaviconsFromDB) {
+ const GURL page_url("http://www.google.com/");
+
+ // |page_url| has favicons at two different icon URLs on the web. Each icon
+ // is a .ico file with two bitmaps.
+ const GURL icon_url1("http://www.google.com/icon1.ico");
+ const GURL icon_url2("http://www.google.com/icon2.ico");
+
+ IconURLSizesMap icon_url_sizes;
+ icon_url_sizes[icon_url1] = FaviconSizes(kSizesSmallAndLarge);
+ icon_url_sizes[icon_url2] = FaviconSizes(kSizesSmallAndLarge);
+
+ // The thumbnail database has stored bitmap data only for the favicon at
+ // |icon_url1|.
+ std::vector<FaviconDataElement> elements;
+ SetFaviconDataElements(icon_url1, FaviconSizes(kSizesSmallAndLarge),
+ &elements);
+
+ SetFaviconsState(page_url, FAVICON, elements, icon_url_sizes);
+
+ FaviconData favicon_data;
+ EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url, FAVICON, &favicon_data));
+
+ EXPECT_TRUE(favicon_data.known_icon);
+ EXPECT_FALSE(favicon_data.expired);
+ EXPECT_EQ(FAVICON, favicon_data.icon_type);
+
+ EXPECT_EQ(2u, favicon_data.elements.size());
+ FaviconDataElement element1 = favicon_data.elements[0];
+ FaviconDataElement element2 = favicon_data.elements[1];
+
+ // No required order for FaviconDataElements.
+ if (element1.pixel_size == kLargeSize) {
+ FaviconDataElement tmp_element = element1;
+ element1 = element2;
+ element2 = tmp_element;
+ }
+
+ EXPECT_TRUE(BitmapDataEqual('a', element1.bitmap_data));
+ EXPECT_EQ(kSmallSize, element1.pixel_size);
+ EXPECT_EQ(icon_url1, element1.icon_url);
+
+ EXPECT_TRUE(BitmapDataEqual('b', element2.bitmap_data));
+ EXPECT_EQ(kLargeSize, element2.pixel_size);
+ EXPECT_EQ(icon_url1, element2.icon_url);
+
+ EXPECT_EQ(2u, favicon_data.icon_url_sizes.size());
+ EXPECT_TRUE(favicon_data.icon_url_sizes.count(icon_url1));
+ EXPECT_EQ(kSizesSmallAndLarge,
+ favicon_data.icon_url_sizes[icon_url1].ToString());
+ EXPECT_TRUE(favicon_data.icon_url_sizes.count(icon_url2));
+ EXPECT_EQ(kSizesSmallAndLarge,
+ favicon_data.icon_url_sizes[icon_url2].ToString());
+}
+
+// Tests that |expired| == true in FaviconData returned by GetFaviconsFromDB
+// when one of the favicons are out of date.
+TEST_F(HistoryBackendTest, GetFaviconsFromDBExpired) {
+ const GURL page_url("http://www.google.com/");
+
+ // |page_url| has favicons at two different icon URLs on the web. Each icon
+ // is a .png with a single bitmap.
+ const GURL icon_url1("http://www.google.com/icon1.png");
+ const GURL icon_url2("http://www.google.com/icon2.png");
+
+ IconURLSizesMap icon_url_sizes;
+ icon_url_sizes[icon_url1] = FaviconSizes(kSizesSmall);
+ icon_url_sizes[icon_url2] = FaviconSizes(kSizesSmall);
+
+ std::vector<FaviconDataElement> elements;
+ SetFaviconDataElements(icon_url1, FaviconSizes(kSizesSmall), icon_url2,
+ FaviconSizes(kSizesSmall), &elements);
+
+ SetFaviconsState(page_url, FAVICON, elements, icon_url_sizes);
+
+ // Set the favicon for |icon_url1| as out of date.
+ FaviconID favicon_id1 =
+ backend_->thumbnail_db_->GetFaviconIDForFaviconURL(icon_url1, FAVICON);
+ EXPECT_NE(0, favicon_id1);
+ EXPECT_TRUE(backend_->thumbnail_db_->SetFaviconOutOfDate(favicon_id1));
+
+ FaviconData favicon_data;
+ EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url, FAVICON, &favicon_data));
+ // Favicon should be expired.
+ EXPECT_TRUE(favicon_data.expired);
+}
+
+// Test FaviconData returned by GetFaviconsFromDB for different IconTypes.
+TEST_F(HistoryBackendTest, GetFaviconsFromDBIconType) {
+ const GURL page_url("http://www.google.com/");
+ const GURL icon_url1("http://www.google.com/icon1.png");
+ const GURL icon_url2("http://www.google.com/icon2.png");
+
+ IconURLSizesMap icon_url_sizes;
+ icon_url_sizes[icon_url1] = FaviconSizes(kSizesSmall);
+ icon_url_sizes[icon_url2] = FaviconSizes(kSizesSmall);
+
+ std::vector<FaviconDataElement> elements;
+ SetFaviconDataElements(icon_url1, FaviconSizes(kSizesSmall), &elements);
+ SetFaviconsState(page_url, FAVICON, elements, icon_url_sizes);
+
+ SetFaviconDataElements(icon_url2, FaviconSizes(kSizesSmall), &elements);
+ std::vector<unsigned char> touch_bitmap_data;
+ touch_bitmap_data.push_back('b');
+ elements[0].bitmap_data = base::RefCountedBytes::TakeVector(
+ &touch_bitmap_data);
+ backend_->SetFavicons(page_url, TOUCH_ICON, elements, icon_url_sizes);
+
+ // Test favicon of type FAVICON for this URL.
+ FaviconData favicon_data;
+ EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url, FAVICON, &favicon_data));
+
+ EXPECT_TRUE(favicon_data.known_icon);
+ EXPECT_FALSE(favicon_data.expired);
+ EXPECT_EQ(FAVICON, favicon_data.icon_type);
+ EXPECT_EQ(1u, favicon_data.elements.size());
+ EXPECT_TRUE(BitmapDataEqual('a', favicon_data.elements[0].bitmap_data));
+ EXPECT_EQ(kSmallSize, favicon_data.elements[0].pixel_size);
+ EXPECT_EQ(icon_url1, favicon_data.elements[0].icon_url);
+
+ // Test favicon of type TOUCH_ICON for this URL.
+ FaviconData touchicon_data;
+ EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url, TOUCH_ICON,
+ &touchicon_data));
+
+ EXPECT_TRUE(touchicon_data.known_icon);
+ EXPECT_FALSE(touchicon_data.expired);
+ EXPECT_EQ(TOUCH_ICON, touchicon_data.icon_type);
+ EXPECT_EQ(1u, touchicon_data.elements.size());
+ EXPECT_TRUE(BitmapDataEqual('b', touchicon_data.elements[0].bitmap_data));
+ EXPECT_EQ(kSmallSize, touchicon_data.elements[0].pixel_size);
+ EXPECT_EQ(icon_url2, touchicon_data.elements[0].icon_url);
+}
+
+// Test FaviconData returned by GetFaviconsFromDB when there are no found
+// favicons.
+TEST_F(HistoryBackendTest, GetFaviconsFromDBEmpty) {
+ const GURL page_url("http://www.google.com/");
+
+ EXPECT_TRUE(DeleteFaviconsAndFaviconBitmaps(page_url, FAVICON));
+
+ FaviconData favicon_data;
+ EXPECT_FALSE(backend_->GetFaviconsFromDB(page_url, FAVICON, &favicon_data));
+ EXPECT_FALSE(favicon_data.known_icon);
}
TEST_F(HistoryBackendTest, CloneFaviconIsRestrictedToSameDomain) {
const GURL url("http://www.google.com/");
- const GURL icon_url("http://www.google.com/icon");
const GURL same_domain_url("http://www.google.com/subdir/index.html");
const GURL foreign_domain_url("http://www.not-google.com/");
+ const GURL icon_url("http://www.google.com/icon.png");
- // Add a favicon
- std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1));
- scoped_refptr<base::RefCountedBytes> bytes(new base::RefCountedBytes(data));
- backend_->SetFavicon(
- url, icon_url, bytes.get(), FAVICON);
- EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL(
- url, FAVICON, NULL));
+ // Add a favicon.
+ IconURLSizesMap icon_url_sizes;
+ icon_url_sizes[icon_url] = FaviconSizes(kSizesSmall);
+ std::vector<FaviconDataElement> elements;
+ SetFaviconDataElements(icon_url, FaviconSizes(kSizesSmall), &elements);
+ SetFaviconsState(url, FAVICON, elements, icon_url_sizes);
// Validate starting state.
- FaviconData favicon;
- EXPECT_TRUE(backend_->GetFaviconFromDB(url, FAVICON, &favicon));
- EXPECT_FALSE(backend_->GetFaviconFromDB(same_domain_url, FAVICON, &favicon));
- EXPECT_FALSE(backend_->GetFaviconFromDB(foreign_domain_url,
- FAVICON, &favicon));
+ FaviconData favicon_data;
+ EXPECT_TRUE(backend_->GetFaviconsFromDB(url, FAVICON, &favicon_data));
+ FaviconData favicon_data_same_domain;
+ EXPECT_FALSE(backend_->GetFaviconsFromDB(same_domain_url, FAVICON,
+ &favicon_data_same_domain));
+ FaviconData favicon_data_foreign_domain;
+ EXPECT_FALSE(backend_->GetFaviconsFromDB(foreign_domain_url, FAVICON,
+ &favicon_data_foreign_domain));
// Same-domain cloning should work.
- backend_->CloneFavicon(url, same_domain_url);
- EXPECT_TRUE(backend_->GetFaviconFromDB(same_domain_url, FAVICON, &favicon));
+ backend_->CloneFavicons(url, same_domain_url);
+ FaviconData favicon_data_cloned_same_domain;
+ EXPECT_TRUE(backend_->GetFaviconsFromDB(same_domain_url, FAVICON,
+ &favicon_data_cloned_same_domain));
// Foreign-domain cloning is forbidden.
- backend_->CloneFavicon(url, foreign_domain_url);
- EXPECT_FALSE(backend_->GetFaviconFromDB(foreign_domain_url,
- FAVICON, &favicon));
+ backend_->CloneFavicons(url, foreign_domain_url);
+ FaviconData favicon_data_cloned_foreign_domain;
+ EXPECT_FALSE(backend_->GetFaviconsFromDB(foreign_domain_url, FAVICON,
+ &favicon_data_cloned_foreign_domain));
}
TEST_F(HistoryBackendTest, QueryFilteredURLs) {

Powered by Google App Engine
This is Rietveld 408576698