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

Side by Side 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: Changes as requested by Sky and stevenjb 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 unified diff | Download patch | Annotate | Revision Log
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 <set> 5 #include <set>
6 #include <vector> 6 #include <vector>
7 7
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/file_path.h" 9 #include "base/file_path.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 30 matching lines...) Expand all
41 41
42 namespace { 42 namespace {
43 43
44 // data we'll put into the thumbnail database 44 // data we'll put into the thumbnail database
45 static const unsigned char blob1[] = 45 static const unsigned char blob1[] =
46 "12346102356120394751634516591348710478123649165419234519234512349134"; 46 "12346102356120394751634516591348710478123649165419234519234512349134";
47 47
48 static const gfx::Size kSmallSize = gfx::Size(16, 16); 48 static const gfx::Size kSmallSize = gfx::Size(16, 16);
49 static const gfx::Size kLargeSize = gfx::Size(48, 48); 49 static const gfx::Size kLargeSize = gfx::Size(48, 48);
50 50
51 const std::string kSizesSmall = "16 16";
52 const std::string kSizesSmallAndLarge = "16 16 48 48";
53
51 } // namepace 54 } // namepace
52 55
53 namespace history { 56 namespace history {
54 57
55 class HistoryBackendTest; 58 class HistoryBackendTest;
56 59
57 // This must be a separate object since HistoryBackend manages its lifetime. 60 // 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. 61 // This just forwards the messages we're interested in to the test object.
59 class HistoryBackendTestDelegate : public HistoryBackend::Delegate { 62 class HistoryBackendTestDelegate : public HistoryBackend::Delegate {
60 public: 63 public:
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 URLID id = backend_->db()->GetRowForURL(url, &row); 197 URLID id = backend_->db()->GetRowForURL(url, &row);
195 VisitVector visits; 198 VisitVector visits;
196 EXPECT_TRUE(backend_->db()->GetVisitsForURL(id, &visits)); 199 EXPECT_TRUE(backend_->db()->GetVisitsForURL(id, &visits));
197 return visits[0].transition; 200 return visits[0].transition;
198 } 201 }
199 202
200 FilePath getTestDir() { 203 FilePath getTestDir() {
201 return test_dir_; 204 return test_dir_;
202 } 205 }
203 206
204 FaviconID GetFavicon(const GURL& url, IconType icon_type) { 207 size_t NumIconMappingsForPageURL(const GURL& page_url, IconType icon_type) {
205 IconMapping icon_mapping; 208 std::vector<IconMapping> icon_mappings;
206 if (backend_->thumbnail_db_->GetIconMappingForPageURL(url, icon_type, 209 if (backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url, icon_type,
207 &icon_mapping)) 210 &icon_mappings)) {
208 return icon_mapping.icon_id; 211 return icon_mappings.size();
209 else 212 }
210 return 0; 213 return 0u;
214 }
215
216 // Returns true if there is exactly one favicon bitmap associated to
217 // |icon_url|. If true, returns favicon bitmap in output parameter.
218 bool GetOnlyFaviconBitmapForIconURL(const GURL& icon_url,
219 IconType icon_type,
220 FaviconBitmap* favicon_bitmap) {
221 FaviconID id = backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
222 icon_url, icon_type);
223 if (!id)
224 return false;
225 std::vector<FaviconBitmap> favicon_bitmaps;
226 if (!backend_->thumbnail_db_->GetFaviconBitmaps(id, &favicon_bitmaps))
227 return false;
228 if (favicon_bitmaps.size() != 1)
229 return false;
230 *favicon_bitmap = favicon_bitmaps[0];
231 return true;
232 }
233
234 // Deletes favicons and favicon bitmaps for |page_url| and |icon_type|.
235 bool DeleteFaviconsAndFaviconBitmaps(const GURL& page_url,
236 IconType icon_type) {
237 backend_->SetFavicons(page_url,
238 icon_type,
239 std::vector<FaviconDataElement>(),
240 IconURLSizesMap());
241 return !backend_->thumbnail_db_->GetIconMappingsForPageURL(page_url,
242 icon_type, NULL);
243 }
244
245 // Sets the favicons for |page_url| and |icon_type|. This is very similar to
246 // HistoryBackend::SetFavicons except that this call will delete favicon
247 // bitmaps which are not in |elements|. This allows setting a favicon state
248 // where a favicon bitmap can be guaranteed to be only specified via sizes and
249 // not have any associated bitmap data in the favicon_bitmaps table.
250 void SetFaviconsState(const GURL& page_url,
251 IconType icon_type,
252 const std::vector<FaviconDataElement>& elements,
253 const IconURLSizesMap& icon_url_sizes) {
254 EXPECT_TRUE(DeleteFaviconsAndFaviconBitmaps(page_url, icon_type));
255 backend_->SetFavicons(page_url, icon_type, elements, icon_url_sizes);
256 }
257
258 // Sets |elements| to have entries for the icon_urls and sizes specified.
259 // The bitmap_data for entries are lowercase letters of the alphabet starting
260 // at 'a' for the entry at index 0.
261 void SetFaviconDataElements(const GURL& icon_url1,
262 const FaviconSizes& icon_url1_sizes,
263 std::vector<FaviconDataElement>* elements) {
264 SetFaviconDataElements(icon_url1, icon_url1_sizes, GURL(), FaviconSizes(),
265 elements);
266 }
267
268 void SetFaviconDataElements(const GURL& icon_url1,
269 const FaviconSizes& icon_url1_sizes,
270 const GURL& icon_url2,
271 const FaviconSizes& icon_url2_sizes,
272 std::vector<FaviconDataElement>* elements) {
273 elements->clear();
274
275 char bitmap_char = 'a';
276 std::vector<gfx::Size> icon_url1_sizes_vector = icon_url1_sizes.ToVector();
277 for (size_t i = 0; i < icon_url1_sizes_vector.size(); ++i) {
278 std::vector<unsigned char> data;
279 data.push_back(bitmap_char);
280 FaviconDataElement element;
281 element.bitmap_data = base::RefCountedBytes::TakeVector(&data);
282 element.pixel_size = icon_url1_sizes_vector[i];
283 element.icon_url = icon_url1;
284 elements->push_back(element);
285
286 ++bitmap_char;
287 }
288
289 std::vector<gfx::Size> icon_url2_sizes_vector = icon_url2_sizes.ToVector();
290 for (size_t i = 0; i < icon_url2_sizes_vector.size(); ++i) {
291 std::vector<unsigned char> data;
292 data.push_back(bitmap_char);
293 FaviconDataElement element;
294 element.bitmap_data = base::RefCountedBytes::TakeVector(&data);
295 element.pixel_size = icon_url2_sizes_vector[i];
296 element.icon_url = icon_url2;
297 elements->push_back(element);
298
299 ++bitmap_char;
300 }
301 }
302
303 bool BitmapDataEqual(char expected_data,
304 scoped_refptr<base::RefCountedMemory> bitmap_data) {
305 std::string bitmap_data_string(bitmap_data->front(),
306 bitmap_data->front() + bitmap_data->size());
307 return bitmap_data_string.size() == 1u &&
308 bitmap_data_string[0] == expected_data;
211 } 309 }
212 310
213 BookmarkModel bookmark_model_; 311 BookmarkModel bookmark_model_;
214 312
215 protected: 313 protected:
216 bool loaded_; 314 bool loaded_;
217 315
218 private: 316 private:
219 friend class HistoryBackendTestDelegate; 317 friend class HistoryBackendTestDelegate;
220 318
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 504
407 // All thumbnails should be deleted. 505 // All thumbnails should be deleted.
408 std::vector<unsigned char> out_data; 506 std::vector<unsigned char> out_data;
409 EXPECT_FALSE(backend_->thumbnail_db_->GetPageThumbnail(outrow1.id(), 507 EXPECT_FALSE(backend_->thumbnail_db_->GetPageThumbnail(outrow1.id(),
410 &out_data)); 508 &out_data));
411 EXPECT_FALSE(backend_->thumbnail_db_->GetPageThumbnail(row2_id, &out_data)); 509 EXPECT_FALSE(backend_->thumbnail_db_->GetPageThumbnail(row2_id, &out_data));
412 510
413 // We should have a favicon and favicon bitmaps for the first URL only. We 511 // We should have a favicon and favicon bitmaps for the first URL only. We
414 // look them up by favicon URL since the IDs may have changed. 512 // look them up by favicon URL since the IDs may have changed.
415 FaviconID out_favicon1 = backend_->thumbnail_db_-> 513 FaviconID out_favicon1 = backend_->thumbnail_db_->
416 GetFaviconIDForFaviconURL(favicon_url1, FAVICON, NULL); 514 GetFaviconIDForFaviconURL(favicon_url1, FAVICON);
417 EXPECT_TRUE(out_favicon1); 515 EXPECT_TRUE(out_favicon1);
418 516
419 std::vector<FaviconBitmap> favicon_bitmaps; 517 std::vector<FaviconBitmap> favicon_bitmaps;
420 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps( 518 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(
421 out_favicon1, &favicon_bitmaps)); 519 out_favicon1, &favicon_bitmaps));
422 ASSERT_EQ(2u, favicon_bitmaps.size()); 520 ASSERT_EQ(2u, favicon_bitmaps.size());
423 521
424 FaviconBitmap favicon_bitmap1 = favicon_bitmaps[0]; 522 FaviconBitmap favicon_bitmap1 = favicon_bitmaps[0];
425 FaviconBitmap favicon_bitmap2 = favicon_bitmaps[1]; 523 FaviconBitmap favicon_bitmap2 = favicon_bitmaps[1];
426 524
427 // Bitmaps do not need to be in particular order. 525 // Favicon bitmaps do not need to be in particular order.
428 if (favicon_bitmap1.pixel_size == kLargeSize) { 526 if (favicon_bitmap1.pixel_size == kLargeSize) {
429 FaviconBitmap tmp_favicon_bitmap = favicon_bitmap1; 527 FaviconBitmap tmp_favicon_bitmap = favicon_bitmap1;
430 favicon_bitmap1 = favicon_bitmap2; 528 favicon_bitmap1 = favicon_bitmap2;
431 favicon_bitmap2 = tmp_favicon_bitmap; 529 favicon_bitmap2 = tmp_favicon_bitmap;
432 } 530 }
433 531
434 EXPECT_EQ('a', *favicon_bitmap1.bitmap_data->front()); 532 EXPECT_TRUE(BitmapDataEqual('a', favicon_bitmap1.bitmap_data));
435 EXPECT_EQ(kSmallSize, favicon_bitmap1.pixel_size); 533 EXPECT_EQ(kSmallSize, favicon_bitmap1.pixel_size);
436 534
437 EXPECT_EQ('b', *favicon_bitmap2.bitmap_data->front()); 535 EXPECT_TRUE(BitmapDataEqual('b', favicon_bitmap2.bitmap_data));
438 EXPECT_EQ(kLargeSize, favicon_bitmap2.pixel_size); 536 EXPECT_EQ(kLargeSize, favicon_bitmap2.pixel_size);
439 537
440 FaviconID out_favicon2 = backend_->thumbnail_db_-> 538 FaviconID out_favicon2 = backend_->thumbnail_db_->
441 GetFaviconIDForFaviconURL(favicon_url2, FAVICON, NULL); 539 GetFaviconIDForFaviconURL(favicon_url2, FAVICON);
442 EXPECT_FALSE(out_favicon2) << "Favicon not deleted"; 540 EXPECT_FALSE(out_favicon2) << "Favicon not deleted";
443 541
444 // The remaining URL should still reference the same favicon, even if its 542 // The remaining URL should still reference the same favicon, even if its
445 // ID has changed. 543 // ID has changed.
446 EXPECT_EQ(out_favicon1, GetFavicon(outrow1.url(), FAVICON)); 544 std::vector<IconMapping> mappings;
545 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
546 outrow1.url(), FAVICON, &mappings));
547 EXPECT_EQ(1u, mappings.size());
548 EXPECT_EQ(out_favicon1, mappings[0].icon_id);
447 549
448 // The first URL should still be bookmarked. 550 // The first URL should still be bookmarked.
449 EXPECT_TRUE(bookmark_model_.IsBookmarked(row1.url())); 551 EXPECT_TRUE(bookmark_model_.IsBookmarked(row1.url()));
450 552
451 // The full text database should have no data. 553 // The full text database should have no data.
452 std::vector<TextDatabase::Match> text_matches; 554 std::vector<TextDatabase::Match> text_matches;
453 Time first_time_searched; 555 Time first_time_searched;
454 backend_->text_database_->GetTextMatches(UTF8ToUTF16("Body"), 556 backend_->text_database_->GetTextMatches(UTF8ToUTF16("Body"),
455 QueryOptions(), 557 QueryOptions(),
456 &text_matches, 558 &text_matches,
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 &first_time_searched); 614 &first_time_searched);
513 EXPECT_EQ(0U, text_matches.size()); 615 EXPECT_EQ(0U, text_matches.size());
514 } 616 }
515 617
516 TEST_F(HistoryBackendTest, URLsNoLongerBookmarked) { 618 TEST_F(HistoryBackendTest, URLsNoLongerBookmarked) {
517 GURL favicon_url1("http://www.google.com/favicon.ico"); 619 GURL favicon_url1("http://www.google.com/favicon.ico");
518 GURL favicon_url2("http://news.google.com/favicon.ico"); 620 GURL favicon_url2("http://news.google.com/favicon.ico");
519 621
520 std::vector<unsigned char> data; 622 std::vector<unsigned char> data;
521 data.push_back('1'); 623 data.push_back('1');
624
625 FaviconID favicon2 = backend_->thumbnail_db_->AddFavicon(
626 favicon_url2,
627 FAVICON,
628 "0 0",
629 new base::RefCountedBytes(data),
630 Time::Now(),
631 gfx::Size());
632
522 FaviconID favicon1 = backend_->thumbnail_db_->AddFavicon( 633 FaviconID favicon1 = backend_->thumbnail_db_->AddFavicon(
523 favicon_url1, 634 favicon_url1,
524 FAVICON, 635 FAVICON,
525 "0 0", 636 "0 0",
526 new base::RefCountedBytes(data), 637 new base::RefCountedBytes(data),
527 Time::Now(), 638 Time::Now(),
528 gfx::Size()); 639 gfx::Size());
529 640
530 data[0] = '2'; 641 data[0] = '2';
531 FaviconID favicon2 = backend_->thumbnail_db_->AddFavicon( 642 EXPECT_TRUE(backend_->thumbnail_db_->AddFaviconBitmap(
532 favicon_url2, 643 favicon2, new base::RefCountedBytes(data), Time::Now(), gfx::Size()));
533 FAVICON,
534 "0 0",
535 new base::RefCountedBytes(data),
536 Time::Now(),
537 gfx::Size());
538 644
539 // First visit two URLs. 645 // First visit two URLs.
540 URLRow row1(GURL("http://www.google.com/")); 646 URLRow row1(GURL("http://www.google.com/"));
541 row1.set_visit_count(2); 647 row1.set_visit_count(2);
542 row1.set_typed_count(1); 648 row1.set_typed_count(1);
543 row1.set_last_visit(Time::Now()); 649 row1.set_last_visit(Time::Now());
544 EXPECT_TRUE(backend_->thumbnail_db_->AddIconMapping(row1.url(), favicon1)); 650 EXPECT_TRUE(backend_->thumbnail_db_->AddIconMapping(row1.url(), favicon1));
545 651
546 URLRow row2(GURL("http://news.google.com/")); 652 URLRow row2(GURL("http://news.google.com/"));
547 row2.set_visit_count(1); 653 row2.set_visit_count(1);
(...skipping 18 matching lines...) Expand all
566 672
567 // Make sure url 2 is still valid, but has no visits. 673 // Make sure url 2 is still valid, but has no visits.
568 URLRow tmp_url_row; 674 URLRow tmp_url_row;
569 EXPECT_EQ(row2_id, backend_->db_->GetRowForURL(row2.url(), NULL)); 675 EXPECT_EQ(row2_id, backend_->db_->GetRowForURL(row2.url(), NULL));
570 VisitVector visits; 676 VisitVector visits;
571 backend_->db_->GetVisitsForURL(row2_id, &visits); 677 backend_->db_->GetVisitsForURL(row2_id, &visits);
572 EXPECT_EQ(0U, visits.size()); 678 EXPECT_EQ(0U, visits.size());
573 // The favicon should still be valid. 679 // The favicon should still be valid.
574 EXPECT_EQ(favicon2, 680 EXPECT_EQ(favicon2,
575 backend_->thumbnail_db_->GetFaviconIDForFaviconURL(favicon_url2, 681 backend_->thumbnail_db_->GetFaviconIDForFaviconURL(favicon_url2,
576 FAVICON, 682 FAVICON));
577 NULL));
578 683
579 // Unstar row2. 684 // Unstar row2.
580 bookmark_utils::RemoveAllBookmarks(&bookmark_model_, row2.url()); 685 bookmark_utils::RemoveAllBookmarks(&bookmark_model_, row2.url());
581 686
582 // Tell the backend it was unstarred. We have to explicitly do this as 687 // Tell the backend it was unstarred. We have to explicitly do this as
583 // BookmarkModel isn't wired up to the backend during testing. 688 // BookmarkModel isn't wired up to the backend during testing.
584 std::set<GURL> unstarred_urls; 689 std::set<GURL> unstarred_urls;
585 unstarred_urls.insert(row2.url()); 690 unstarred_urls.insert(row2.url());
586 backend_->URLsNoLongerBookmarked(unstarred_urls); 691 backend_->URLsNoLongerBookmarked(unstarred_urls);
587 692
588 // The URL should no longer exist. 693 // The URL should no longer exist.
589 EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &tmp_url_row)); 694 EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &tmp_url_row));
590 // And the favicon should be deleted. 695 // And the favicon should be deleted.
591 EXPECT_EQ(0, 696 EXPECT_EQ(0,
592 backend_->thumbnail_db_->GetFaviconIDForFaviconURL(favicon_url2, 697 backend_->thumbnail_db_->GetFaviconIDForFaviconURL(favicon_url2,
593 FAVICON, 698 FAVICON));
594 NULL));
595 699
596 // Unstar row 1. 700 // Unstar row 1.
597 bookmark_utils::RemoveAllBookmarks(&bookmark_model_, row1.url()); 701 bookmark_utils::RemoveAllBookmarks(&bookmark_model_, row1.url());
598 // Tell the backend it was unstarred. We have to explicitly do this as 702 // Tell the backend it was unstarred. We have to explicitly do this as
599 // BookmarkModel isn't wired up to the backend during testing. 703 // BookmarkModel isn't wired up to the backend during testing.
600 unstarred_urls.clear(); 704 unstarred_urls.clear();
601 unstarred_urls.insert(row1.url()); 705 unstarred_urls.insert(row1.url());
602 backend_->URLsNoLongerBookmarked(unstarred_urls); 706 backend_->URLsNoLongerBookmarked(unstarred_urls);
603 707
604 // The URL should still exist (because there were visits). 708 // The URL should still exist (because there were visits).
605 EXPECT_EQ(row1_id, backend_->db_->GetRowForURL(row1.url(), NULL)); 709 EXPECT_EQ(row1_id, backend_->db_->GetRowForURL(row1.url(), NULL));
606 710
607 // There should still be visits. 711 // There should still be visits.
608 visits.clear(); 712 visits.clear();
609 backend_->db_->GetVisitsForURL(row1_id, &visits); 713 backend_->db_->GetVisitsForURL(row1_id, &visits);
610 EXPECT_EQ(1U, visits.size()); 714 EXPECT_EQ(1U, visits.size());
611 715
612 // The favicon should still be valid. 716 // The favicon should still be valid.
613 EXPECT_EQ(favicon1, 717 EXPECT_EQ(favicon1,
614 backend_->thumbnail_db_->GetFaviconIDForFaviconURL(favicon_url1, 718 backend_->thumbnail_db_->GetFaviconIDForFaviconURL(favicon_url1,
615 FAVICON, 719 FAVICON));
616 NULL));
617 } 720 }
618 721
619 // Tests a handful of assertions for a navigation with a type of 722 // Tests a handful of assertions for a navigation with a type of
620 // KEYWORD_GENERATED. 723 // KEYWORD_GENERATED.
621 TEST_F(HistoryBackendTest, KeywordGenerated) { 724 TEST_F(HistoryBackendTest, KeywordGenerated) {
622 ASSERT_TRUE(backend_.get()); 725 ASSERT_TRUE(backend_.get());
623 726
624 GURL url("http://google.com"); 727 GURL url("http://google.com");
625 728
626 Time visit_time = Time::Now() - base::TimeDelta::FromDays(1); 729 Time visit_time = Time::Now() - base::TimeDelta::FromDays(1);
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
697 800
698 TEST_F(HistoryBackendTest, ImportedFaviconsTest) { 801 TEST_F(HistoryBackendTest, ImportedFaviconsTest) {
699 // Setup test data - two Urls in the history, one with favicon assigned and 802 // Setup test data - two Urls in the history, one with favicon assigned and
700 // one without. 803 // one without.
701 GURL favicon_url1("http://www.google.com/favicon.ico"); 804 GURL favicon_url1("http://www.google.com/favicon.ico");
702 std::vector<unsigned char> data; 805 std::vector<unsigned char> data;
703 data.push_back('1'); 806 data.push_back('1');
704 FaviconID favicon1 = backend_->thumbnail_db_->AddFavicon( 807 FaviconID favicon1 = backend_->thumbnail_db_->AddFavicon(
705 favicon_url1, 808 favicon_url1,
706 FAVICON, 809 FAVICON,
707 "0 0", 810 kSizesSmallAndLarge,
708 base::RefCountedBytes::TakeVector(&data), 811 base::RefCountedBytes::TakeVector(&data),
709 Time::Now(), 812 Time::Now(),
710 gfx::Size()); 813 kSmallSize);
814
711 URLRow row1(GURL("http://www.google.com/")); 815 URLRow row1(GURL("http://www.google.com/"));
712 row1.set_visit_count(1); 816 row1.set_visit_count(1);
713 row1.set_last_visit(Time::Now()); 817 row1.set_last_visit(Time::Now());
714 EXPECT_TRUE(backend_->thumbnail_db_->AddIconMapping(row1.url(), favicon1)); 818 EXPECT_TRUE(backend_->thumbnail_db_->AddIconMapping(row1.url(), favicon1));
715 819
716 URLRow row2(GURL("http://news.google.com/")); 820 URLRow row2(GURL("http://news.google.com/"));
717 row2.set_visit_count(1); 821 row2.set_visit_count(1);
718 row2.set_last_visit(Time::Now()); 822 row2.set_last_visit(Time::Now());
719 URLRows rows; 823 URLRows rows;
720 rows.push_back(row1); 824 rows.push_back(row1);
721 rows.push_back(row2); 825 rows.push_back(row2);
722 backend_->AddPagesWithDetails(rows, history::SOURCE_BROWSED); 826 backend_->AddPagesWithDetails(rows, history::SOURCE_BROWSED);
723 URLRow url_row1, url_row2; 827 URLRow url_row1, url_row2;
724 EXPECT_FALSE(backend_->db_->GetRowForURL(row1.url(), &url_row1) == 0); 828 EXPECT_FALSE(backend_->db_->GetRowForURL(row1.url(), &url_row1) == 0);
725 EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &url_row2) == 0); 829 EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &url_row2) == 0);
726 EXPECT_FALSE(GetFavicon(row1.url(), FAVICON) == 0); 830 EXPECT_EQ(1u, NumIconMappingsForPageURL(row1.url(), FAVICON));
727 EXPECT_TRUE(GetFavicon(row2.url(), FAVICON) == 0); 831 EXPECT_EQ(0u, NumIconMappingsForPageURL(row2.url(), FAVICON));
728 832
729 // Now provide one imported favicon for both URLs already in the registry. 833 // Now provide one imported favicon for both URLs already in the registry.
730 // The new favicon should only be used with the URL that doesn't already have 834 // The new favicon should only be used with the URL that doesn't already have
731 // a favicon. 835 // a favicon.
732 std::vector<history::ImportedFaviconUsage> favicons; 836 std::vector<history::ImportedFaviconUsage> favicons;
733 history::ImportedFaviconUsage favicon; 837 history::ImportedFaviconUsage favicon;
734 favicon.favicon_url = GURL("http://news.google.com/favicon.ico"); 838 favicon.favicon_url = GURL("http://news.google.com/favicon.ico");
735 favicon.png_data.push_back('2'); 839 favicon.png_data.push_back('2');
736 favicon.urls.insert(row1.url()); 840 favicon.urls.insert(row1.url());
737 favicon.urls.insert(row2.url()); 841 favicon.urls.insert(row2.url());
738 favicons.push_back(favicon); 842 favicons.push_back(favicon);
739 backend_->SetImportedFavicons(favicons); 843 backend_->SetImportedFavicons(favicons);
740 EXPECT_FALSE(backend_->db_->GetRowForURL(row1.url(), &url_row1) == 0); 844 EXPECT_FALSE(backend_->db_->GetRowForURL(row1.url(), &url_row1) == 0);
741 EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &url_row2) == 0); 845 EXPECT_FALSE(backend_->db_->GetRowForURL(row2.url(), &url_row2) == 0);
742 EXPECT_FALSE(GetFavicon(row1.url(), FAVICON) == 0); 846
743 EXPECT_FALSE(GetFavicon(row2.url(), FAVICON) == 0); 847 std::vector<IconMapping> mappings;
744 EXPECT_FALSE(GetFavicon(row1.url(), FAVICON) == 848 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
745 GetFavicon(row2.url(), FAVICON)); 849 row1.url(), FAVICON, &mappings));
850 EXPECT_EQ(1u, mappings.size());
851 EXPECT_EQ(favicon1, mappings[0].icon_id);
852 EXPECT_EQ(favicon_url1, mappings[0].icon_url);
853
854 mappings.clear();
855 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
856 row2.url(), FAVICON, &mappings));
857 EXPECT_EQ(1u, mappings.size());
858 EXPECT_EQ(favicon.favicon_url, mappings[0].icon_url);
746 859
747 // A URL should not be added to history (to store favicon), if 860 // A URL should not be added to history (to store favicon), if
748 // the URL is not bookmarked. 861 // the URL is not bookmarked.
749 GURL url3("http://mail.google.com"); 862 GURL url3("http://mail.google.com");
750 favicons.clear(); 863 favicons.clear();
751 favicon.favicon_url = GURL("http://mail.google.com/favicon.ico"); 864 favicon.favicon_url = GURL("http://mail.google.com/favicon.ico");
752 favicon.png_data.push_back('3'); 865 favicon.png_data.push_back('3');
753 favicon.urls.insert(url3); 866 favicon.urls.insert(url3);
754 favicons.push_back(favicon); 867 favicons.push_back(favicon);
755 backend_->SetImportedFavicons(favicons); 868 backend_->SetImportedFavicons(favicons);
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
1092 EXPECT_EQ(cur_version, file_version); 1205 EXPECT_EQ(cur_version, file_version);
1093 1206
1094 // Check visit_source table is created and empty. 1207 // Check visit_source table is created and empty.
1095 s.Assign(db.GetUniqueStatement( 1208 s.Assign(db.GetUniqueStatement(
1096 "SELECT name FROM sqlite_master WHERE name=\"visit_source\"")); 1209 "SELECT name FROM sqlite_master WHERE name=\"visit_source\""));
1097 ASSERT_TRUE(s.Step()); 1210 ASSERT_TRUE(s.Step());
1098 s.Assign(db.GetUniqueStatement("SELECT * FROM visit_source LIMIT 10")); 1211 s.Assign(db.GetUniqueStatement("SELECT * FROM visit_source LIMIT 10"));
1099 EXPECT_FALSE(s.Step()); 1212 EXPECT_FALSE(s.Step());
1100 } 1213 }
1101 1214
1102 TEST_F(HistoryBackendTest, SetFaviconMapping) { 1215 TEST_F(HistoryBackendTest, SetFaviconMappingsForPageAndRedirects) {
1103 // Init recent_redirects_ 1216 // Init recent_redirects_
1104 const GURL url1("http://www.google.com"); 1217 const GURL url1("http://www.google.com");
1105 const GURL url2("http://www.google.com/m"); 1218 const GURL url2("http://www.google.com/m");
1106 URLRow url_info1(url1); 1219 URLRow url_info1(url1);
1107 url_info1.set_visit_count(0); 1220 url_info1.set_visit_count(0);
1108 url_info1.set_typed_count(0); 1221 url_info1.set_typed_count(0);
1109 url_info1.set_last_visit(base::Time()); 1222 url_info1.set_last_visit(base::Time());
1110 url_info1.set_hidden(false); 1223 url_info1.set_hidden(false);
1111 backend_->db_->AddURL(url_info1); 1224 backend_->db_->AddURL(url_info1);
1112 1225
1113 URLRow url_info2(url2); 1226 URLRow url_info2(url2);
1114 url_info2.set_visit_count(0); 1227 url_info2.set_visit_count(0);
1115 url_info2.set_typed_count(0); 1228 url_info2.set_typed_count(0);
1116 url_info2.set_last_visit(base::Time()); 1229 url_info2.set_last_visit(base::Time());
1117 url_info2.set_hidden(false); 1230 url_info2.set_hidden(false);
1118 backend_->db_->AddURL(url_info2); 1231 backend_->db_->AddURL(url_info2);
1119 1232
1120 history::RedirectList redirects; 1233 history::RedirectList redirects;
1121 redirects.push_back(url2); 1234 redirects.push_back(url2);
1122 redirects.push_back(url1); 1235 redirects.push_back(url1);
1123 backend_->recent_redirects_.Put(url1, redirects); 1236 backend_->recent_redirects_.Put(url1, redirects);
1124 1237
1125 const GURL icon_url("http://www.google.com/icon"); 1238 const GURL icon_url1("http://www.google.com/icon");
1126 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); 1239 const GURL icon_url2("http://www.google.com/icon2");
1127 // Add a favicon 1240
1128 backend_->SetFavicon( 1241 // Create mapping for a page with two favicons.
1129 url1, icon_url, base::RefCountedBytes::TakeVector(&data), FAVICON); 1242 IconURLSizesMap two_icon_url_sizes;
1130 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL( 1243 two_icon_url_sizes[icon_url1] = FaviconSizes(kSizesSmallAndLarge);
1131 url1, FAVICON, NULL)); 1244 two_icon_url_sizes[icon_url2] = FaviconSizes(kSizesSmallAndLarge);
1132 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL( 1245
1133 url2, FAVICON, NULL)); 1246 // Create a mapping for a page with a single favicon.
1134 1247 IconURLSizesMap one_icon_url_sizes;
1135 // Add a touch_icon 1248 one_icon_url_sizes[icon_url1] = FaviconSizes(kSizesSmallAndLarge);
1136 backend_->SetFavicon( 1249
1137 url1, icon_url, base::RefCountedBytes::TakeVector(&data), TOUCH_ICON); 1250 std::vector<FaviconDataElement> elements;
1138 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL( 1251
1139 url1, TOUCH_ICON, NULL)); 1252 // Add two favicons
1140 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL( 1253 backend_->SetFavicons(url1, FAVICON, elements, two_icon_url_sizes);
1141 url2, TOUCH_ICON, NULL)); 1254 EXPECT_EQ(2u, NumIconMappingsForPageURL(url1, FAVICON));
1142 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL( 1255 EXPECT_EQ(2u, NumIconMappingsForPageURL(url2, FAVICON));
1143 url1, FAVICON, NULL)); 1256
1144 1257 // Add one touch_icon
1145 // Add a TOUCH_PRECOMPOSED_ICON 1258 backend_->SetFavicons(url1, TOUCH_ICON, elements, one_icon_url_sizes);
1146 backend_->SetFavicon(url1, 1259 EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, TOUCH_ICON));
1147 icon_url, 1260 EXPECT_EQ(1u, NumIconMappingsForPageURL(url2, TOUCH_ICON));
1148 base::RefCountedBytes::TakeVector(&data), 1261 EXPECT_EQ(2u, NumIconMappingsForPageURL(url1, FAVICON));
1149 TOUCH_PRECOMPOSED_ICON); 1262
1263 // Add one TOUCH_PRECOMPOSED_ICON
1264 backend_->SetFavicons(url1, TOUCH_PRECOMPOSED_ICON, elements,
1265 one_icon_url_sizes);
1150 // The touch_icon was replaced. 1266 // The touch_icon was replaced.
1151 EXPECT_FALSE(backend_->thumbnail_db_->GetIconMappingForPageURL( 1267 EXPECT_EQ(0u, NumIconMappingsForPageURL(url1, TOUCH_ICON));
1152 url1, TOUCH_ICON, NULL)); 1268 EXPECT_EQ(2u, NumIconMappingsForPageURL(url1, FAVICON));
1153 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL( 1269 EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, TOUCH_PRECOMPOSED_ICON));
1154 url1, FAVICON, NULL)); 1270 EXPECT_EQ(1u, NumIconMappingsForPageURL(url2, TOUCH_PRECOMPOSED_ICON));
1155 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL( 1271
1156 url1, TOUCH_PRECOMPOSED_ICON, NULL)); 1272 // Add a touch_icon.
1157 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL( 1273 backend_->SetFavicons(url1, TOUCH_ICON, elements, one_icon_url_sizes);
1158 url2, TOUCH_PRECOMPOSED_ICON, NULL)); 1274 EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, TOUCH_ICON));
1159 1275 EXPECT_EQ(2u, NumIconMappingsForPageURL(url1, FAVICON));
1160 // Add a touch_icon
1161 backend_->SetFavicon(
1162 url1, icon_url, base::RefCountedBytes::TakeVector(&data), TOUCH_ICON);
1163 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL(
1164 url1, TOUCH_ICON, NULL));
1165 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL(
1166 url1, FAVICON, NULL));
1167 // The TOUCH_PRECOMPOSED_ICON was replaced. 1276 // The TOUCH_PRECOMPOSED_ICON was replaced.
1168 EXPECT_FALSE(backend_->thumbnail_db_->GetIconMappingForPageURL( 1277 EXPECT_EQ(0u, NumIconMappingsForPageURL(url1, TOUCH_PRECOMPOSED_ICON));
1169 url1, TOUCH_PRECOMPOSED_ICON, NULL)); 1278
1170 1279 // Add a single favicon.
1171 // Add a favicon 1280 backend_->SetFavicons(url1, FAVICON, elements, one_icon_url_sizes);
1172 const GURL icon_url2("http://www.google.com/icon2"); 1281 // The favicon of type FAVICON for |icon_url2| should have no more mappings
1173 backend_->SetFavicon( 1282 // and should have been deleted.
1174 url1, icon_url2, base::RefCountedBytes::TakeVector(&data), FAVICON); 1283 EXPECT_EQ(0, backend_->thumbnail_db_->GetFaviconIDForFaviconURL(icon_url2,
1175 FaviconID icon_id = backend_->thumbnail_db_->GetFaviconIDForFaviconURL( 1284 FAVICON));
1176 icon_url2, FAVICON, NULL); 1285 // The remaining mapping should be |icon_url1|.
1177 EXPECT_NE(0, icon_id); 1286 FaviconID icon_id1 = backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
1178 std::vector<IconMapping> icon_mapping; 1287 icon_url1, FAVICON);
1288 EXPECT_NE(0, icon_id1);
1289 std::vector<IconMapping> icon_mappings;
1179 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( 1290 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
1180 url1, &icon_mapping)); 1291 url1, FAVICON, &icon_mappings));
1181 // The old icon was replaced. 1292 EXPECT_EQ(1u, icon_mappings.size());
1182 EXPECT_TRUE(icon_mapping.size() > 1); 1293 EXPECT_EQ(icon_id1, icon_mappings[0].icon_id);
1183 EXPECT_EQ(icon_id, icon_mapping[1].icon_id); 1294
1184 } 1295 // Add two favicons.
1185 1296 backend_->SetFavicons(url1, FAVICON, elements, two_icon_url_sizes);
1186 TEST_F(HistoryBackendTest, AddOrUpdateIconMapping) { 1297 EXPECT_EQ(1u, NumIconMappingsForPageURL(url1, TOUCH_ICON));
1187 // Test the same icon and page mapping will not be added twice. other case 1298 EXPECT_EQ(2u, NumIconMappingsForPageURL(url1, FAVICON));
1188 // should be covered in TEST_F(HistoryBackendTest, SetFaviconMapping) 1299 }
1300
1301 // Test that there is no churn in icon mappings from calling
1302 // SetFaviconMappingsForPage() with icon mappings which are already in the
1303 // database.
1304 TEST_F(HistoryBackendTest, SetFaviconMappingsForPageDuplicates) {
1189 const GURL url("http://www.google.com/"); 1305 const GURL url("http://www.google.com/");
1190 const GURL icon_url("http://www.google.com/icon"); 1306 const GURL icon_url("http://www.google.com/icon");
1191 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); 1307 std::vector<FaviconDataElement> elements;
1192 1308
1193 backend_->SetFavicon( 1309 IconURLSizesMap icon_url_sizes;
1194 url, icon_url, base::RefCountedBytes::TakeVector(&data), FAVICON); 1310 icon_url_sizes[icon_url] = FaviconSizes(kSizesSmallAndLarge);
1195 FaviconID icon_id = backend_->thumbnail_db_->GetFaviconIDForFaviconURL( 1311
1196 icon_url, FAVICON, NULL); 1312 backend_->SetFavicons(url, FAVICON, elements, icon_url_sizes);
1197 1313
1198 // Add the same mapping 1314 std::vector<IconMapping> icon_mappings;
1199 FaviconID replaced;
1200 EXPECT_FALSE(backend_->AddOrUpdateIconMapping(
1201 url, icon_id, FAVICON, &replaced));
1202 EXPECT_EQ(0, replaced);
1203
1204 std::vector<IconMapping> icon_mapping;
1205 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL( 1315 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
1206 url, &icon_mapping)); 1316 url, FAVICON, &icon_mappings));
1207 EXPECT_EQ(1u, icon_mapping.size()); 1317 EXPECT_EQ(1u, icon_mappings.size());
1208 } 1318 IconMappingID mapping_id = icon_mappings[0].mapping_id;
1209 1319 GURL favicon_url = icon_mappings[0].icon_url;
1210 TEST_F(HistoryBackendTest, GetFaviconForURL) { 1320 FaviconID favicon_id = icon_mappings[0].icon_id;
1211 // This test will add a fav icon and touch icon for the same URL 1321
1212 // and check the behaviour of backend's GetFaviconForURL implementation. 1322 HistoryBackend::IconURLFaviconIDMap icon_url_ids;
1213 const GURL url("http://www.google.com/"); 1323 icon_url_ids[favicon_url] = favicon_id;
1324
1325 // False should be returned as no mappings should have changed.
1326 EXPECT_FALSE(backend_->SetFaviconMappingsForPage(url, FAVICON, icon_url_ids));
1327
1328 icon_mappings.clear();
1329 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
1330 url, FAVICON, &icon_mappings));
1331 EXPECT_EQ(1u, icon_mappings.size());
1332
1333 // The same row in the icon_mapping table should be used for the mapping as
1334 // before.
1335 EXPECT_EQ(mapping_id, icon_mappings[0].mapping_id);
1336 }
1337
1338 // Test that setting favicons for a page which already has data does the
1339 // right thing.
1340 TEST_F(HistoryBackendTest, SetFavicons) {
1341 const GURL page_url("http://www.google.com/");
1342 std::vector<FaviconDataElement> elements;
1343 IconURLSizesMap icon_url_sizes;
1344
1345 EXPECT_TRUE(DeleteFaviconsAndFaviconBitmaps(page_url, FAVICON));
1346
1347 // Set |page_url| as having two favicons each available from the web at two
1348 // sizes.
1349 const GURL icon_url1("http://www.google.com/icon1");
1350 const GURL icon_url2("http://www.google.com/icon2");
1351
1352 icon_url_sizes[icon_url1] = FaviconSizes(kSizesSmallAndLarge);
1353 icon_url_sizes[icon_url2] = FaviconSizes(kSizesSmallAndLarge);
1354
1355 // Set only sizes info for the favicons.
1356 backend_->SetFavicons(page_url, FAVICON, elements, icon_url_sizes);
1357
1358 std::vector<IconMapping> icon_mappings;
1359 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
1360 page_url, &icon_mappings));
1361 EXPECT_EQ(2u, icon_mappings.size());
1362 for (size_t i = 0; i < icon_mappings.size(); ++i) {
1363 EXPECT_FALSE(backend_->thumbnail_db_->GetFaviconBitmaps(
1364 icon_mappings[i].icon_id, NULL));
1365 }
1366
1367 // Add bitmap data to the favicons.
1368 SetFaviconDataElements(icon_url1,
1369 FaviconSizes(kSizesSmall),
1370 icon_url2,
1371 FaviconSizes(kSizesSmallAndLarge),
1372 &elements);
1373
1374 backend_->SetFavicons(page_url, FAVICON, elements, icon_url_sizes);
1375
1376 icon_mappings.clear();
1377 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
1378 page_url, &icon_mappings));
1379 EXPECT_EQ(2u, icon_mappings.size());
1380 IconMapping mapping1 = icon_mappings[0];
1381 IconMapping mapping2 = icon_mappings[1];
1382
1383 // Icon mappings do not need to be returned in particular order from database
1384 // with respect to icon url.
1385 if (mapping1.icon_url == icon_url2) {
1386 IconMapping tmp_mapping = mapping1;
1387 mapping1 = mapping2;
1388 mapping2 = tmp_mapping;
1389 }
1390
1391 GURL icon_url;
1392 IconType icon_type;
1393 std::string sizes;
1394 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconHeader(mapping1.icon_id,
1395 &icon_url, &icon_type, &sizes));
1396 EXPECT_EQ(icon_url1, icon_url);
1397 EXPECT_EQ(FAVICON, icon_type);
1398 EXPECT_EQ(kSizesSmallAndLarge, sizes);
1399
1400 std::vector<FaviconBitmap> favicon_bitmaps;
1401 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(mapping1.icon_id,
1402 &favicon_bitmaps));
1403 EXPECT_EQ(1u, favicon_bitmaps.size());
1404 EXPECT_TRUE(BitmapDataEqual('a', favicon_bitmaps[0].bitmap_data));
1405 EXPECT_EQ(kSmallSize, favicon_bitmaps[0].pixel_size);
1406
1407 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconHeader(mapping2.icon_id,
1408 &icon_url, &icon_type, &sizes));
1409 EXPECT_EQ(icon_url2, icon_url);
1410 EXPECT_EQ(FAVICON, icon_type);
1411 EXPECT_EQ(kSizesSmallAndLarge, sizes);
1412
1413 favicon_bitmaps.clear();
1414 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(mapping2.icon_id,
1415 &favicon_bitmaps));
1416
1417 EXPECT_EQ(2u, favicon_bitmaps.size());
1418
1419 // Favicon bitmaps do not need to be in particular order.
1420 if (favicon_bitmaps[0].pixel_size == kSmallSize) {
1421 EXPECT_TRUE(BitmapDataEqual('b', favicon_bitmaps[0].bitmap_data));
1422 EXPECT_EQ(kLargeSize, favicon_bitmaps[1].pixel_size);
1423 EXPECT_TRUE(BitmapDataEqual('c', favicon_bitmaps[1].bitmap_data));
1424 } else {
1425 EXPECT_EQ(kLargeSize, favicon_bitmaps[0].pixel_size);
1426 EXPECT_TRUE(BitmapDataEqual('c', favicon_bitmaps[0].bitmap_data));
1427 EXPECT_EQ(kSmallSize, favicon_bitmaps[1].pixel_size);
1428 EXPECT_TRUE(BitmapDataEqual('b', favicon_bitmaps[1].bitmap_data));
1429 }
1430
1431 // Change the sizes for which the favicon at icon_url1 is available at from
1432 // the web. Verify that all the data remains valid.
1433 const std::string kSizesTinySmallAndLarge = "10 10 " + kSizesSmallAndLarge;
1434 icon_url_sizes[icon_url1] = FaviconSizes(kSizesTinySmallAndLarge);
1435 elements.clear();
1436 backend_->SetFavicons(page_url, FAVICON, elements, icon_url_sizes);
1437
1438 icon_mappings.clear();
1439 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingsForPageURL(
1440 page_url, &icon_mappings));
1441 EXPECT_EQ(2u, icon_mappings.size());
1442 mapping1 = icon_mappings[0];
1443 mapping2 = icon_mappings[1];
1444
1445 // Icon mappings do not need to be returned in particular order from database
1446 // with respect to icon urls.
1447 if (mapping1.icon_url == icon_url2) {
1448 IconMapping tmp_mapping = mapping1;
1449 mapping1 = mapping2;
1450 mapping2 = tmp_mapping;
1451 }
1452
1453 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconHeader(mapping1.icon_id,
1454 &icon_url, &icon_type, &sizes));
1455 EXPECT_EQ(icon_url1, icon_url);
1456 EXPECT_EQ(FAVICON, icon_type);
1457 EXPECT_EQ(kSizesTinySmallAndLarge, sizes);
1458
1459 favicon_bitmaps.clear();
1460 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(mapping1.icon_id,
1461 &favicon_bitmaps));
1462 EXPECT_EQ(1u, favicon_bitmaps.size());
1463
1464 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconHeader(mapping2.icon_id,
1465 &icon_url, &icon_type, &sizes));
1466 EXPECT_EQ(icon_url2, icon_url);
1467 EXPECT_EQ(FAVICON, icon_type);
1468 EXPECT_EQ(kSizesSmallAndLarge, sizes);
1469
1470 favicon_bitmaps.clear();
1471 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(mapping2.icon_id,
1472 &favicon_bitmaps));
1473 EXPECT_EQ(2u, favicon_bitmaps.size());
1474 }
1475
1476 // Test that changing the sizes that a favicon is available at from the web
1477 // deletes stale favicon bitmaps.
1478 TEST_F(HistoryBackendTest, SetFaviconsDeleteBitmaps) {
1479 const GURL page_url("http://www.google.com/");
1214 const GURL icon_url("http://www.google.com/icon"); 1480 const GURL icon_url("http://www.google.com/icon");
1215 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); 1481
1216 scoped_refptr<base::RefCountedBytes> bytes(new base::RefCountedBytes(data)); 1482 // Set |page_url| as having one favicon with two different sizes.
1217 // Used for testing the icon data after getting from DB 1483 IconURLSizesMap icon_url_sizes;
1218 std::string blob_data(bytes->front(), 1484 icon_url_sizes[icon_url] = FaviconSizes(kSizesSmallAndLarge);
1219 bytes->front() + bytes->size()); 1485
1220 1486 std::vector<FaviconDataElement> elements;
1221 // Add a favicon 1487 SetFaviconDataElements(icon_url, FaviconSizes(kSizesSmallAndLarge),
1222 backend_->SetFavicon( 1488 &elements);
1223 url, icon_url, bytes.get(), FAVICON); 1489
1224 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL( 1490 // Add bitmap data and sizes information to the database.
1225 url, FAVICON, NULL)); 1491 backend_->SetFavicons(page_url, FAVICON, elements, icon_url_sizes);
1226 1492
1227 // Add a touch_icon 1493 FaviconID favicon_id = backend_->thumbnail_db_->GetFaviconIDForFaviconURL(
1228 backend_->SetFavicon( 1494 icon_url, FAVICON);
1229 url, icon_url, bytes.get(), TOUCH_ICON); 1495 EXPECT_NE(0, favicon_id);
1230 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL( 1496
1231 url, TOUCH_ICON, NULL)); 1497 std::vector<FaviconBitmap> favicon_bitmaps;
1232 1498 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(favicon_id,
1233 // Test the Fav icon for this URL. 1499 &favicon_bitmaps));
1234 FaviconData favicon; 1500 EXPECT_EQ(2u, favicon_bitmaps.size());
1235 ASSERT_TRUE(backend_->GetFaviconFromDB(url, FAVICON, &favicon)); 1501
1236 std::string favicon_data( 1502 // Change the bitmap sizes avaible from the web only to the small size only.
1237 favicon.image_data->front(), 1503 icon_url_sizes[icon_url] = FaviconSizes(kSizesSmall);
1238 favicon.image_data->front() + favicon.image_data->size()); 1504 elements.clear();
1239 1505 backend_->SetFavicons(page_url, FAVICON, elements, icon_url_sizes);
1240 EXPECT_EQ(FAVICON, favicon.icon_type); 1506
1241 EXPECT_EQ(icon_url, favicon.icon_url); 1507 favicon_id = backend_->thumbnail_db_->GetFaviconIDForFaviconURL(icon_url,
1242 EXPECT_EQ(blob_data, favicon_data); 1508 FAVICON);
1243 1509 EXPECT_NE(0, favicon_id);
1244 // Test the touch icon for this URL. 1510
1245 ASSERT_TRUE(backend_->GetFaviconFromDB(url, TOUCH_ICON, &favicon)); 1511 favicon_bitmaps.clear();
1246 std::string touchicon_data( 1512 EXPECT_TRUE(backend_->thumbnail_db_->GetFaviconBitmaps(favicon_id,
1247 favicon.image_data->front(), 1513 &favicon_bitmaps));
1248 favicon.image_data->front() + favicon.image_data->size()); 1514 EXPECT_EQ(1u, favicon_bitmaps.size());
1249 1515 EXPECT_EQ(kSmallSize, favicon_bitmaps[0].pixel_size);
1250 EXPECT_EQ(TOUCH_ICON, favicon.icon_type); 1516 }
1251 EXPECT_EQ(icon_url, favicon.icon_url); 1517
1252 EXPECT_EQ(blob_data, touchicon_data); 1518 // Test updating a single favicon bitmap's data via SetFavicons.
1519 TEST_F(HistoryBackendTest, SetFaviconsReplaceBitmapData) {
1520
1521 const GURL page_url("http://www.google.com/");
1522 const GURL icon_url("http://www.google.com/icon");
1523 IconURLSizesMap icon_url_sizes;
1524 icon_url_sizes[icon_url] = FaviconSizes(kSizesSmall);
1525
1526 std::vector<unsigned char> data_initial;
1527 data_initial.push_back('a');
1528
1529 FaviconDataElement element;
1530 element.bitmap_data = base::RefCountedBytes::TakeVector(&data_initial);
1531 element.pixel_size = kSmallSize;
1532 element.icon_url = icon_url;
1533 std::vector<FaviconDataElement> elements;
1534 elements.push_back(element);
1535
1536 // Add bitmap to the database.
1537 backend_->SetFavicons(page_url, FAVICON, elements, icon_url_sizes);
1538
1539 FaviconBitmap favicon_bitmap;
1540 EXPECT_TRUE(GetOnlyFaviconBitmapForIconURL(icon_url, FAVICON,
1541 &favicon_bitmap));
1542 EXPECT_TRUE(BitmapDataEqual('a', favicon_bitmap.bitmap_data));
1543
1544 // SetFavicons with identical data but a different bitmap.
1545 std::vector<unsigned char> updated_data;
1546 updated_data.push_back('b');
1547 elements[0].bitmap_data = base::RefCountedBytes::TakeVector(&updated_data);
1548 backend_->SetFavicons(page_url, FAVICON, elements, icon_url_sizes);
1549
1550 EXPECT_TRUE(GetOnlyFaviconBitmapForIconURL(icon_url, FAVICON,
1551 &favicon_bitmap));
1552 EXPECT_TRUE(BitmapDataEqual('b', favicon_bitmap.bitmap_data));
1553 }
1554
1555 // Test GetFaviconsFromDB returns expected data in FaviconData.
1556 TEST_F(HistoryBackendTest, GetFaviconsFromDB) {
1557 const GURL page_url("http://www.google.com/");
1558
1559 // |page_url| has favicons at two different icon URLs on the web. Each icon
1560 // is a .ico file with two bitmaps.
1561 const GURL icon_url1("http://www.google.com/icon1.ico");
1562 const GURL icon_url2("http://www.google.com/icon2.ico");
1563
1564 IconURLSizesMap icon_url_sizes;
1565 icon_url_sizes[icon_url1] = FaviconSizes(kSizesSmallAndLarge);
1566 icon_url_sizes[icon_url2] = FaviconSizes(kSizesSmallAndLarge);
1567
1568 // The thumbnail database has stored bitmap data only for the favicon at
1569 // |icon_url1|.
1570 std::vector<FaviconDataElement> elements;
1571 SetFaviconDataElements(icon_url1, FaviconSizes(kSizesSmallAndLarge),
1572 &elements);
1573
1574 SetFaviconsState(page_url, FAVICON, elements, icon_url_sizes);
1575
1576 FaviconData favicon_data;
1577 EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url, FAVICON, NULL,
1578 &favicon_data));
1579
1580 EXPECT_TRUE(favicon_data.known_icon);
1581 EXPECT_FALSE(favicon_data.expired);
1582 EXPECT_EQ(FAVICON, favicon_data.icon_type);
1583
1584 EXPECT_EQ(2u, favicon_data.elements.size());
1585 FaviconDataElement element1 = favicon_data.elements[0];
1586 FaviconDataElement element2 = favicon_data.elements[1];
1587
1588 // No required order for FaviconDataElements.
1589 if (element1.pixel_size == kLargeSize) {
1590 FaviconDataElement tmp_element = element1;
1591 element1 = element2;
1592 element2 = tmp_element;
1593 }
1594
1595 EXPECT_TRUE(BitmapDataEqual('a', element1.bitmap_data));
1596 EXPECT_EQ(kSmallSize, element1.pixel_size);
1597 EXPECT_EQ(icon_url1, element1.icon_url);
1598
1599 EXPECT_TRUE(BitmapDataEqual('b', element2.bitmap_data));
1600 EXPECT_EQ(kLargeSize, element2.pixel_size);
1601 EXPECT_EQ(icon_url1, element2.icon_url);
1602
1603 EXPECT_EQ(2u, favicon_data.icon_url_sizes.size());
1604 EXPECT_TRUE(favicon_data.icon_url_sizes.count(icon_url1));
1605 EXPECT_EQ(kSizesSmallAndLarge,
1606 favicon_data.icon_url_sizes[icon_url1].ToString());
1607 EXPECT_TRUE(favicon_data.icon_url_sizes.count(icon_url2));
1608 EXPECT_EQ(kSizesSmallAndLarge,
1609 favicon_data.icon_url_sizes[icon_url2].ToString());
1610 }
1611
1612 // Tests that |expired| == true in FaviconData returned by GetFaviconsFromDB
1613 // when one of the favicons are out of date.
1614 TEST_F(HistoryBackendTest, GetFaviconsFromDBExpired) {
1615 const GURL page_url("http://www.google.com/");
1616
1617 // |page_url| has favicons at two different icon URLs on the web. Each icon
1618 // is a .png with a single bitmap.
1619 const GURL icon_url1("http://www.google.com/icon1.png");
1620 const GURL icon_url2("http://www.google.com/icon2.png");
1621
1622 IconURLSizesMap icon_url_sizes;
1623 icon_url_sizes[icon_url1] = FaviconSizes(kSizesSmall);
1624 icon_url_sizes[icon_url2] = FaviconSizes(kSizesSmall);
1625
1626 std::vector<FaviconDataElement> elements;
1627 SetFaviconDataElements(icon_url1, FaviconSizes(kSizesSmall), icon_url2,
1628 FaviconSizes(kSizesSmall), &elements);
1629
1630 SetFaviconsState(page_url, FAVICON, elements, icon_url_sizes);
1631
1632 // Set the favicon for |icon_url1| as out of date.
1633 FaviconID favicon_id1 =
1634 backend_->thumbnail_db_->GetFaviconIDForFaviconURL(icon_url1, FAVICON);
1635 EXPECT_NE(0, favicon_id1);
1636 EXPECT_TRUE(backend_->thumbnail_db_->SetFaviconOutOfDate(favicon_id1));
1637
1638 FaviconData favicon_data;
1639 EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url, FAVICON, NULL,
1640 &favicon_data));
1641 // Favicon should be expired.
1642 EXPECT_TRUE(favicon_data.expired);
1643 }
1644
1645 // Test FaviconData returned by GetFaviconsFromDB when a non NULL
1646 // desired_pixel_size is passed in.
1647 TEST_F(HistoryBackendTest, GetFaviconsFromDBDesiredSize) {
1648 const GURL page_url("http://www.google.com/");
1649
1650 // |page_url| has favicons at two different icon URLs on the web. Each icon
1651 // is a .ico file with two bitmaps.
1652 const GURL icon_url1("http://www.google.com/icon1.ico");
1653 const GURL icon_url2("http://www.google.com/icon2.ico");
1654
1655 IconURLSizesMap icon_url_sizes;
1656 icon_url_sizes[icon_url1] = FaviconSizes(kSizesSmallAndLarge);
1657 icon_url_sizes[icon_url2] = FaviconSizes(kSizesSmallAndLarge);
1658
1659 // The thumbnail database has stored bitmap data only for the favicon at
1660 // |icon_url1|.
1661 std::vector<FaviconDataElement> elements;
1662 SetFaviconDataElements(icon_url1, FaviconSizes(kSizesSmallAndLarge),
1663 &elements);
1664
1665 SetFaviconsState(page_url, FAVICON, elements, icon_url_sizes);
1666
1667 gfx::Size desired_size(40, 40);
1668 FaviconData favicon_data1;
1669 EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url, FAVICON, &desired_size,
1670 &favicon_data1));
1671
1672 // FaviconData should have single entry with data for 48x48 bitmap because it
1673 // is a better match for the desired size than the 16x16 bitmap.
1674 EXPECT_TRUE(favicon_data1.known_icon);
1675 EXPECT_FALSE(favicon_data1.expired);
1676 EXPECT_EQ(FAVICON, favicon_data1.icon_type);
1677 EXPECT_EQ(1u, favicon_data1.elements.size());
1678 EXPECT_TRUE(BitmapDataEqual('b', favicon_data1.elements[0].bitmap_data));
1679 EXPECT_EQ(kLargeSize, favicon_data1.elements[0].pixel_size);
1680 EXPECT_EQ(icon_url1, favicon_data1.elements[0].icon_url);
1681
1682 desired_size = gfx::Size(20, 20);
1683 FaviconData favicon_data2;
1684 EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url, FAVICON, &desired_size,
1685 &favicon_data2));
1686
1687 // FaviconData should have single entry with data for 16x16 bitmap because it
1688 // is a better match for the desired size than the 48x48 bitmap.
1689 EXPECT_TRUE(favicon_data2.known_icon);
1690 EXPECT_FALSE(favicon_data2.expired);
1691 EXPECT_EQ(FAVICON, favicon_data2.icon_type);
1692 EXPECT_EQ(1u, favicon_data2.elements.size());
1693 EXPECT_TRUE(BitmapDataEqual('a', favicon_data2.elements[0].bitmap_data));
1694 EXPECT_EQ(kSmallSize, favicon_data2.elements[0].pixel_size);
1695 EXPECT_EQ(icon_url1, favicon_data2.elements[0].icon_url);
1696 }
1697
1698 // Test FaviconData returned by GetFaviconsFromDB for different IconTypes.
1699 TEST_F(HistoryBackendTest, GetFaviconsFromDBIconType) {
1700 const GURL page_url("http://www.google.com/");
1701 const GURL icon_url1("http://www.google.com/icon1.png");
1702 const GURL icon_url2("http://www.google.com/icon2.png");
1703
1704 IconURLSizesMap icon_url_sizes;
1705 icon_url_sizes[icon_url1] = FaviconSizes(kSizesSmall);
1706 icon_url_sizes[icon_url2] = FaviconSizes(kSizesSmall);
1707
1708 std::vector<FaviconDataElement> elements;
1709 SetFaviconDataElements(icon_url1, FaviconSizes(kSizesSmall), &elements);
1710 SetFaviconsState(page_url, FAVICON, elements, icon_url_sizes);
1711
1712 SetFaviconDataElements(icon_url2, FaviconSizes(kSizesSmall), &elements);
1713 std::vector<unsigned char> touch_bitmap_data;
1714 touch_bitmap_data.push_back('b');
1715 elements[0].bitmap_data = base::RefCountedBytes::TakeVector(
1716 &touch_bitmap_data);
1717 backend_->SetFavicons(page_url, TOUCH_ICON, elements, icon_url_sizes);
1718
1719 // Test favicon of type FAVICON for this URL.
1720 FaviconData favicon_data;
1721 EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url, FAVICON, NULL,
1722 &favicon_data));
1723
1724 EXPECT_TRUE(favicon_data.known_icon);
1725 EXPECT_FALSE(favicon_data.expired);
1726 EXPECT_EQ(FAVICON, favicon_data.icon_type);
1727 EXPECT_EQ(1u, favicon_data.elements.size());
1728 EXPECT_TRUE(BitmapDataEqual('a', favicon_data.elements[0].bitmap_data));
1729 EXPECT_EQ(kSmallSize, favicon_data.elements[0].pixel_size);
1730 EXPECT_EQ(icon_url1, favicon_data.elements[0].icon_url);
1731
1732 // Test favicon of type TOUCH_ICON for this URL.
1733 FaviconData touchicon_data;
1734 EXPECT_TRUE(backend_->GetFaviconsFromDB(page_url, TOUCH_ICON, NULL,
1735 &touchicon_data));
1736
1737 EXPECT_TRUE(touchicon_data.known_icon);
1738 EXPECT_FALSE(touchicon_data.expired);
1739 EXPECT_EQ(TOUCH_ICON, touchicon_data.icon_type);
1740 EXPECT_EQ(1u, touchicon_data.elements.size());
1741 EXPECT_TRUE(BitmapDataEqual('b', touchicon_data.elements[0].bitmap_data));
1742 EXPECT_EQ(kSmallSize, touchicon_data.elements[0].pixel_size);
1743 EXPECT_EQ(icon_url2, touchicon_data.elements[0].icon_url);
1744 }
1745
1746 // Test FaviconData returned by GetFaviconsFromDB when there are no found
1747 // favicons.
1748 TEST_F(HistoryBackendTest, GetFaviconsFromDBEmpty) {
1749 const GURL page_url("http://www.google.com/");
1750
1751 EXPECT_TRUE(DeleteFaviconsAndFaviconBitmaps(page_url, FAVICON));
1752
1753 FaviconData favicon_data;
1754 EXPECT_FALSE(backend_->GetFaviconsFromDB(page_url, FAVICON, NULL,
1755 &favicon_data));
1756 EXPECT_FALSE(favicon_data.known_icon);
1253 } 1757 }
1254 1758
1255 TEST_F(HistoryBackendTest, CloneFaviconIsRestrictedToSameDomain) { 1759 TEST_F(HistoryBackendTest, CloneFaviconIsRestrictedToSameDomain) {
1256 const GURL url("http://www.google.com/"); 1760 const GURL url("http://www.google.com/");
1257 const GURL icon_url("http://www.google.com/icon");
1258 const GURL same_domain_url("http://www.google.com/subdir/index.html"); 1761 const GURL same_domain_url("http://www.google.com/subdir/index.html");
1259 const GURL foreign_domain_url("http://www.not-google.com/"); 1762 const GURL foreign_domain_url("http://www.not-google.com/");
1260 1763 const GURL icon_url("http://www.google.com/icon.png");
1261 // Add a favicon 1764
1262 std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); 1765 // Add a favicon.
1263 scoped_refptr<base::RefCountedBytes> bytes(new base::RefCountedBytes(data)); 1766 IconURLSizesMap icon_url_sizes;
1264 backend_->SetFavicon( 1767 icon_url_sizes[icon_url] = FaviconSizes(kSizesSmall);
1265 url, icon_url, bytes.get(), FAVICON); 1768 std::vector<FaviconDataElement> elements;
1266 EXPECT_TRUE(backend_->thumbnail_db_->GetIconMappingForPageURL( 1769 SetFaviconDataElements(icon_url, FaviconSizes(kSizesSmall), &elements);
1267 url, FAVICON, NULL)); 1770 SetFaviconsState(url, FAVICON, elements, icon_url_sizes);
1268 1771
1269 // Validate starting state. 1772 // Validate starting state.
1270 FaviconData favicon; 1773 FaviconData favicon_data;
1271 EXPECT_TRUE(backend_->GetFaviconFromDB(url, FAVICON, &favicon)); 1774 EXPECT_TRUE(backend_->GetFaviconsFromDB(url, FAVICON, NULL,
1272 EXPECT_FALSE(backend_->GetFaviconFromDB(same_domain_url, FAVICON, &favicon)); 1775 &favicon_data));
1273 EXPECT_FALSE(backend_->GetFaviconFromDB(foreign_domain_url, 1776 FaviconData favicon_data_same_domain;
1274 FAVICON, &favicon)); 1777 EXPECT_FALSE(backend_->GetFaviconsFromDB(same_domain_url, FAVICON, NULL,
1778 &favicon_data_same_domain));
1779 FaviconData favicon_data_foreign_domain;
1780 EXPECT_FALSE(backend_->GetFaviconsFromDB(foreign_domain_url, FAVICON, NULL,
1781 &favicon_data_foreign_domain));
1275 1782
1276 // Same-domain cloning should work. 1783 // Same-domain cloning should work.
1277 backend_->CloneFavicon(url, same_domain_url); 1784 backend_->CloneFavicon(url, same_domain_url);
1278 EXPECT_TRUE(backend_->GetFaviconFromDB(same_domain_url, FAVICON, &favicon)); 1785 FaviconData favicon_data_cloned_same_domain;
1786 EXPECT_TRUE(backend_->GetFaviconsFromDB(same_domain_url, FAVICON, NULL,
1787 &favicon_data_cloned_same_domain));
1279 1788
1280 // Foreign-domain cloning is forbidden. 1789 // Foreign-domain cloning is forbidden.
1281 backend_->CloneFavicon(url, foreign_domain_url); 1790 backend_->CloneFavicon(url, foreign_domain_url);
1282 EXPECT_FALSE(backend_->GetFaviconFromDB(foreign_domain_url, 1791 FaviconData favicon_data_cloned_foreign_domain;
1283 FAVICON, &favicon)); 1792 EXPECT_FALSE(backend_->GetFaviconsFromDB(foreign_domain_url, FAVICON, NULL,
1793 &favicon_data_cloned_foreign_domain));
1284 } 1794 }
1285 1795
1286 TEST_F(HistoryBackendTest, QueryFilteredURLs) { 1796 TEST_F(HistoryBackendTest, QueryFilteredURLs) {
1287 const char* google = "http://www.google.com/"; 1797 const char* google = "http://www.google.com/";
1288 const char* yahoo = "http://www.yahoo.com/"; 1798 const char* yahoo = "http://www.yahoo.com/";
1289 const char* yahoo_sports = "http://sports.yahoo.com/"; 1799 const char* yahoo_sports = "http://sports.yahoo.com/";
1290 const char* yahoo_sports_with_article1 = 1800 const char* yahoo_sports_with_article1 =
1291 "http://sports.yahoo.com/article1.htm"; 1801 "http://sports.yahoo.com/article1.htm";
1292 const char* yahoo_sports_with_article2 = 1802 const char* yahoo_sports_with_article2 =
1293 "http://sports.yahoo.com/article2.htm"; 1803 "http://sports.yahoo.com/article2.htm";
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after
1607 2117
1608 backend_->DeleteURL(url); 2118 backend_->DeleteURL(url);
1609 backend_->AddPageNoVisitForBookmark(url, string16()); 2119 backend_->AddPageNoVisitForBookmark(url, string16());
1610 backend_->GetURL(url, &row); 2120 backend_->GetURL(url, &row);
1611 EXPECT_EQ(url, row.url()); 2121 EXPECT_EQ(url, row.url());
1612 EXPECT_EQ(UTF8ToUTF16(url.spec()), row.title()); 2122 EXPECT_EQ(UTF8ToUTF16(url.spec()), row.title());
1613 EXPECT_EQ(0, row.visit_count()); 2123 EXPECT_EQ(0, row.visit_count());
1614 } 2124 }
1615 2125
1616 } // namespace history 2126 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698