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

Side by Side Diff: components/history/core/browser/thumbnail_database_unittest.cc

Issue 2823093002: Make FaviconService::GetRawFaviconForPageURL() select the best candidate among all the icon types (Closed)
Patch Set: Merge branch 'master' into icon_type Created 3 years, 7 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 unified diff | Download patch
« no previous file with comments | « components/history/core/browser/thumbnail_database.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/files/file_enumerator.h" 10 #include "base/files/file_enumerator.h"
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 EXPECT_EQ(3u, sql::test::CountTableColumns(db, "favicons")); 69 EXPECT_EQ(3u, sql::test::CountTableColumns(db, "favicons"));
70 70
71 // [id], [icon_id], [last_updated], [image_data], [width], [height] and 71 // [id], [icon_id], [last_updated], [image_data], [width], [height] and
72 // [last_requested]. 72 // [last_requested].
73 EXPECT_EQ(7u, sql::test::CountTableColumns(db, "favicon_bitmaps")); 73 EXPECT_EQ(7u, sql::test::CountTableColumns(db, "favicon_bitmaps"));
74 74
75 // [id], [page_url], and [icon_id]. 75 // [id], [page_url], and [icon_id].
76 EXPECT_EQ(3u, sql::test::CountTableColumns(db, "icon_mapping")); 76 EXPECT_EQ(3u, sql::test::CountTableColumns(db, "icon_mapping"));
77 } 77 }
78 78
79 // Adds a favicon at |icon_url| with |icon_type| with default bitmap data and
80 // maps |page_url| to |icon_url|.
81 void AddAndMapFaviconSimple(ThumbnailDatabase* db,
82 const GURL& page_url,
83 const GURL& icon_url,
84 favicon_base::IconType icon_type) {
85 scoped_refptr<base::RefCountedStaticMemory> data(
86 new base::RefCountedStaticMemory(kBlob1, sizeof(kBlob1)));
87 favicon_base::FaviconID favicon_id =
88 db->AddFavicon(icon_url, icon_type, data, base::Time::Now(), gfx::Size());
89 db->AddIconMapping(page_url, favicon_id);
90 }
91
79 void VerifyDatabaseEmpty(sql::Connection* db) { 92 void VerifyDatabaseEmpty(sql::Connection* db) {
80 size_t rows = 0; 93 size_t rows = 0;
81 EXPECT_TRUE(sql::test::CountTableRows(db, "favicons", &rows)); 94 EXPECT_TRUE(sql::test::CountTableRows(db, "favicons", &rows));
82 EXPECT_EQ(0u, rows); 95 EXPECT_EQ(0u, rows);
83 EXPECT_TRUE(sql::test::CountTableRows(db, "favicon_bitmaps", &rows)); 96 EXPECT_TRUE(sql::test::CountTableRows(db, "favicon_bitmaps", &rows));
84 EXPECT_EQ(0u, rows); 97 EXPECT_EQ(0u, rows);
85 EXPECT_TRUE(sql::test::CountTableRows(db, "icon_mapping", &rows)); 98 EXPECT_TRUE(sql::test::CountTableRows(db, "icon_mapping", &rows));
86 EXPECT_EQ(0u, rows); 99 EXPECT_EQ(0u, rows);
87 } 100 }
88 101
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 } 153 }
141 154
142 if (memcmp(favicon_bitmaps[0].bitmap_data->front(), 155 if (memcmp(favicon_bitmaps[0].bitmap_data->front(),
143 expected_icon_contents, expected_icon_contents_size)) { 156 expected_icon_contents, expected_icon_contents_size)) {
144 ADD_FAILURE() << "failed to match |expected_icon_contents|"; 157 ADD_FAILURE() << "failed to match |expected_icon_contents|";
145 return false; 158 return false;
146 } 159 }
147 return true; 160 return true;
148 } 161 }
149 162
163 bool CompareIconMappingIconUrl(const IconMapping& a, const IconMapping& b) {
164 return a.icon_url < b.icon_url;
165 }
166
167 void SortMappingsByIconUrl(std::vector<IconMapping>* mappings) {
168 std::sort(mappings->begin(), mappings->end(), &CompareIconMappingIconUrl);
169 }
170
150 } // namespace 171 } // namespace
151 172
152 class ThumbnailDatabaseTest : public testing::Test { 173 class ThumbnailDatabaseTest : public testing::Test {
153 public: 174 public:
154 ThumbnailDatabaseTest() {} 175 ThumbnailDatabaseTest() {}
155 ~ThumbnailDatabaseTest() override {} 176 ~ThumbnailDatabaseTest() override {}
156 177
157 // Initialize a thumbnail database instance from the SQL file at 178 // Initialize a thumbnail database instance from the SQL file at
158 // |golden_path| in the "History/" subdirectory of test data. 179 // |golden_path| in the "History/" subdirectory of test data.
159 std::unique_ptr<ThumbnailDatabase> LoadFromGolden(const char* golden_path) { 180 std::unique_ptr<ThumbnailDatabase> LoadFromGolden(const char* golden_path) {
(...skipping 323 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 icon_mappings.clear(); 504 icon_mappings.clear();
484 EXPECT_TRUE(db.GetIconMappingsForPageURL(page_url, &icon_mappings)); 505 EXPECT_TRUE(db.GetIconMappingsForPageURL(page_url, &icon_mappings));
485 506
486 EXPECT_EQ(page_url, icon_mappings.front().page_url); 507 EXPECT_EQ(page_url, icon_mappings.front().page_url);
487 EXPECT_EQ(id3, icon_mappings.front().icon_id); 508 EXPECT_EQ(id3, icon_mappings.front().icon_id);
488 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON, 509 EXPECT_EQ(favicon_base::TOUCH_PRECOMPOSED_ICON,
489 icon_mappings.front().icon_type); 510 icon_mappings.front().icon_type);
490 EXPECT_EQ(icon_url, icon_mappings.front().icon_url); 511 EXPECT_EQ(icon_url, icon_mappings.front().icon_url);
491 } 512 }
492 513
493 // Test result of GetIconMappingsForPageURL when an icon type is passed in. 514 // Test that when multiple icon types are passed to GetIconMappingsForPageURL()
494 TEST_F(ThumbnailDatabaseTest, GetIconMappingsForPageURLWithIconType) { 515 // that the results are filtered according to the passed in types.
516 TEST_F(ThumbnailDatabaseTest, GetIconMappingsForPageURLWithIconTypes) {
495 ThumbnailDatabase db(NULL); 517 ThumbnailDatabase db(NULL);
496 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); 518 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_));
497 db.BeginTransaction(); 519 db.BeginTransaction();
498 520
499 GURL url("http://google.com"); 521 const GURL kPageUrl("http://www.google.com");
500 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1)); 522 AddAndMapFaviconSimple(&db, kPageUrl, kIconUrl1, favicon_base::FAVICON);
501 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); 523 AddAndMapFaviconSimple(&db, kPageUrl, kIconUrl2, favicon_base::TOUCH_ICON);
502 base::Time time = base::Time::Now(); 524 AddAndMapFaviconSimple(&db, kPageUrl, kIconUrl3, favicon_base::TOUCH_ICON);
525 AddAndMapFaviconSimple(&db, kPageUrl, kIconUrl5,
526 favicon_base::TOUCH_PRECOMPOSED_ICON);
503 527
504 favicon_base::FaviconID id1 = 528 // Only the mappings for FAVICON and TOUCH_ICON should be returned.
505 db.AddFavicon(url, favicon_base::FAVICON, favicon, time, gfx::Size());
506 EXPECT_NE(0, db.AddIconMapping(url, id1));
507
508 favicon_base::FaviconID id2 =
509 db.AddFavicon(url, favicon_base::TOUCH_ICON, favicon, time, gfx::Size());
510 EXPECT_NE(0, db.AddIconMapping(url, id2));
511
512 favicon_base::FaviconID id3 =
513 db.AddFavicon(url, favicon_base::TOUCH_ICON, favicon, time, gfx::Size());
514 EXPECT_NE(0, db.AddIconMapping(url, id3));
515
516 // Only the mappings for favicons of type TOUCH_ICON should be returned as
517 // TOUCH_ICON is a larger icon type than FAVICON.
518 std::vector<IconMapping> icon_mappings; 529 std::vector<IconMapping> icon_mappings;
519 EXPECT_TRUE(db.GetIconMappingsForPageURL( 530 EXPECT_TRUE(db.GetIconMappingsForPageURL(
520 url, 531 kPageUrl, favicon_base::FAVICON | favicon_base::TOUCH_ICON,
521 favicon_base::FAVICON | favicon_base::TOUCH_ICON |
522 favicon_base::TOUCH_PRECOMPOSED_ICON,
523 &icon_mappings)); 532 &icon_mappings));
533 SortMappingsByIconUrl(&icon_mappings);
524 534
525 EXPECT_EQ(2u, icon_mappings.size()); 535 ASSERT_EQ(3u, icon_mappings.size());
526 if (id2 == icon_mappings[0].icon_id) { 536 EXPECT_EQ(kIconUrl1, icon_mappings[0].icon_url);
527 EXPECT_EQ(id3, icon_mappings[1].icon_id); 537 EXPECT_EQ(kIconUrl3, icon_mappings[1].icon_url);
528 } else { 538 EXPECT_EQ(kIconUrl2, icon_mappings[2].icon_url);
529 EXPECT_EQ(id3, icon_mappings[0].icon_id);
530 EXPECT_EQ(id2, icon_mappings[1].icon_id);
531 }
532
533 icon_mappings.clear();
534 EXPECT_TRUE(db.GetIconMappingsForPageURL(
535 url, favicon_base::TOUCH_ICON, &icon_mappings));
536 if (id2 == icon_mappings[0].icon_id) {
537 EXPECT_EQ(id3, icon_mappings[1].icon_id);
538 } else {
539 EXPECT_EQ(id3, icon_mappings[0].icon_id);
540 EXPECT_EQ(id2, icon_mappings[1].icon_id);
541 }
542
543 icon_mappings.clear();
544 EXPECT_TRUE(
545 db.GetIconMappingsForPageURL(url, favicon_base::FAVICON, &icon_mappings));
546 EXPECT_EQ(1u, icon_mappings.size());
547 EXPECT_EQ(id1, icon_mappings[0].icon_id);
548 } 539 }
549 540
550 TEST_F(ThumbnailDatabaseTest, HasMappingFor) { 541 TEST_F(ThumbnailDatabaseTest, HasMappingFor) {
551 ThumbnailDatabase db(NULL); 542 ThumbnailDatabase db(NULL);
552 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); 543 ASSERT_EQ(sql::INIT_OK, db.Init(file_name_));
553 db.BeginTransaction(); 544 db.BeginTransaction();
554 545
555 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1)); 546 std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1));
556 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); 547 scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data));
557 548
(...skipping 522 matching lines...) Expand 10 before | Expand all | Expand 10 after
1080 ThumbnailDatabase db(NULL); 1071 ThumbnailDatabase db(NULL);
1081 ASSERT_EQ(sql::INIT_OK, db.Init(db_path)); 1072 ASSERT_EQ(sql::INIT_OK, db.Init(db_path));
1082 1073
1083 // Verify that the resulting schema is correct, whether it 1074 // Verify that the resulting schema is correct, whether it
1084 // involved razing the file or fixing things in place. 1075 // involved razing the file or fixing things in place.
1085 VerifyTablesAndColumns(&db.db_); 1076 VerifyTablesAndColumns(&db.db_);
1086 } 1077 }
1087 } 1078 }
1088 1079
1089 } // namespace history 1080 } // namespace history
OLDNEW
« no previous file with comments | « components/history/core/browser/thumbnail_database.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698