15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <algorithm> 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector> 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/command_line.h" 102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h" 112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/scoped_temp_dir.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted_memory.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/history_database.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/history_unittest_base.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/thumbnail_database.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_constants.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_paths.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/thumbnail_score.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/test/base/testing_profile.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/tools/profiles/thumbnail-inl.h" 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h" 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "third_party/skia/include/core/SkBitmap.h" 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/codec/jpeg_codec.h" 25eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h" 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using base::Time; 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using base::TimeDelta; 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace history { 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace { 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// data we'll put into the thumbnail database 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const unsigned char blob1[] = 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "12346102356120394751634516591348710478123649165419234519234512349134"; 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const unsigned char blob2[] = 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "goiwuegrqrcomizqyzkjalitbahxfjytrqvpqeroicxmnlkhlzunacxaneviawrtxcywhgef"; 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static const unsigned char blob3[] = 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "3716871354098370776510470746794707624107647054607467847164027"; 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const double kBoringness = 0.25; 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const double kWorseBoringness = 0.50; 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const double kBetterBoringness = 0.10; 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const double kTotallyBoring = 1.0; 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int64 kPage1 = 1234; 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const gfx::Size kSmallSize = gfx::Size(16, 16); 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const gfx::Size kLargeSize = gfx::Size(32, 32); 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ThumbnailDatabaseTest : public testing::Test { 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabaseTest() { 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual ~ThumbnailDatabaseTest() { 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void SetUp() { 625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Get a temporary directory for the test DB files. 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) file_name_ = temp_dir_.path().AppendASCII("TestThumbnails.db"); 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) new_file_name_ = temp_dir_.path().AppendASCII("TestFavicons.db"); 675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) history_db_name_ = temp_dir_.path().AppendASCII("TestHistory.db"); 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) google_bitmap_.reset( 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) gfx::JPEGCodec::Decode(kGoogleThumbnail, sizeof(kGoogleThumbnail))); 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<SkBitmap> google_bitmap_; 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::ScopedTempDir temp_dir_; 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath file_name_; 762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath new_file_name_; 772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath history_db_name_; 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class IconMappingMigrationTest : public HistoryUnitTestBase { 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) IconMappingMigrationTest() { 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual ~IconMappingMigrationTest() { 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void SetUp() { 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) profile_.reset(new TestingProfile); 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath data_path; 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &data_path)); 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) data_path = data_path.AppendASCII("History"); 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) history_db_name_ = profile_->GetPath().Append(chrome::kHistoryFilename); 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Set up history and thumbnails as they would be before migration. 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_NO_FATAL_FAILURE( 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ExecuteSQLScript(data_path.AppendASCII("history.20.sql"), 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) history_db_name_)); 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) thumbnail_db_name_ = 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) profile_->GetPath().Append(chrome::kThumbnailsFilename); 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_NO_FATAL_FAILURE( 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ExecuteSQLScript(data_path.AppendASCII("thumbnails.3.sql"), 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) thumbnail_db_name_)); 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath history_db_name_; 1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) base::FilePath thumbnail_db_name_; 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<TestingProfile> profile_; 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ThumbnailDatabaseTest, GetFaviconAfterMigrationToTopSites) { 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase db; 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.BeginTransaction(); 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL url("http://google.com"); 124eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch chrome::FaviconID icon_id = db.AddFavicon(url, chrome::FAVICON); 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Time time = base::Time::Now(); 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FaviconBitmapID bitmap1_id = db.AddFaviconBitmap(icon_id, favicon, time, 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) kSmallSize); 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FaviconBitmapID bitmap2_id = db.AddFaviconBitmap(icon_id, favicon, time, 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) kLargeSize); 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.RenameAndDropThumbnails(file_name_, new_file_name_)); 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.IsLatestVersion()); 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL url_out; 13490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::IconType icon_type_out; 135eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_TRUE(db.GetFaviconHeader(icon_id, &url_out, &icon_type_out)); 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(url, url_out); 13890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(chrome::FAVICON, icon_type_out); 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<FaviconBitmap> favicon_bitmaps_out; 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetFaviconBitmaps(icon_id, &favicon_bitmaps_out)); 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(2u, favicon_bitmaps_out.size()); 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FaviconBitmap favicon_bitmap1 = favicon_bitmaps_out[0]; 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FaviconBitmap favicon_bitmap2 = favicon_bitmaps_out[1]; 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Favicon bitmaps do not need to be in particular order. 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (favicon_bitmap1.bitmap_id == bitmap2_id) { 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) FaviconBitmap tmp_favicon_bitmap = favicon_bitmap1; 1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) favicon_bitmap1 = favicon_bitmap2; 1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) favicon_bitmap2 = tmp_favicon_bitmap; 1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(bitmap1_id, favicon_bitmap1.bitmap_id); 1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(icon_id, favicon_bitmap1.icon_id); 1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(time.ToInternalValue(), 1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) favicon_bitmap1.last_updated.ToInternalValue()); 1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(data.size(), favicon_bitmap1.bitmap_data->size()); 1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(std::equal(data.begin(), 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) data.end(), 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) favicon_bitmap1.bitmap_data->front())); 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(kSmallSize, favicon_bitmap1.pixel_size); 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(bitmap2_id, favicon_bitmap2.bitmap_id); 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(icon_id, favicon_bitmap2.icon_id); 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(time.ToInternalValue(), 1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) favicon_bitmap2.last_updated.ToInternalValue()); 1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(data.size(), favicon_bitmap2.bitmap_data->size()); 1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(std::equal(data.begin(), 1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) data.end(), 1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) favicon_bitmap2.bitmap_data->front())); 1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(kLargeSize, favicon_bitmap2.pixel_size); 1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ThumbnailDatabaseTest, AddIconMapping) { 1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase db; 1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); 1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.BeginTransaction(); 1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); 1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); 1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL url("http://google.com"); 1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Time time = base::Time::Now(); 18590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID id = db.AddFavicon(url, 18690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::TOUCH_ICON, 18790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) favicon, 18890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) time, 18990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) gfx::Size()); 1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_NE(0, id); 1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_NE(0, db.AddIconMapping(url, id)); 1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<IconMapping> icon_mappings; 1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetIconMappingsForPageURL(url, &icon_mappings)); 1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(1u, icon_mappings.size()); 1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(url, icon_mappings.front().page_url); 1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(id, icon_mappings.front().icon_id); 1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ThumbnailDatabaseTest, UpdateIconMapping) { 2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase db; 2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); 2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.BeginTransaction(); 2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL url("http://google.com"); 20690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID id = 207eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch db.AddFavicon(url, chrome::TOUCH_ICON); 2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 209a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) EXPECT_LT(0, db.AddIconMapping(url, id)); 2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<IconMapping> icon_mapping; 2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetIconMappingsForPageURL(url, &icon_mapping)); 2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(1u, icon_mapping.size()); 2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(url, icon_mapping.front().page_url); 2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(id, icon_mapping.front().icon_id); 2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL url1("http://www.google.com/"); 21790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID new_id = 218eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch db.AddFavicon(url1, chrome::TOUCH_ICON); 2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.UpdateIconMapping(icon_mapping.front().mapping_id, new_id)); 2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) icon_mapping.clear(); 2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetIconMappingsForPageURL(url, &icon_mapping)); 2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(1u, icon_mapping.size()); 2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(url, icon_mapping.front().page_url); 2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(new_id, icon_mapping.front().icon_id); 2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_NE(id, icon_mapping.front().icon_id); 2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ThumbnailDatabaseTest, DeleteIconMappings) { 2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase db; 2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); 2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.BeginTransaction(); 2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); 2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); 2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL url("http://google.com"); 23890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID id = 239eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch db.AddFavicon(url, chrome::TOUCH_ICON); 2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Time time = base::Time::Now(); 2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.AddFaviconBitmap(id, favicon, time, gfx::Size()); 242a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) EXPECT_LT(0, db.AddIconMapping(url, id)); 2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 24490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID id2 = 245eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch db.AddFavicon(url, chrome::FAVICON); 246a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) EXPECT_LT(0, db.AddIconMapping(url, id2)); 2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_NE(id, id2); 2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<IconMapping> icon_mapping; 2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetIconMappingsForPageURL(url, &icon_mapping)); 2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(2u, icon_mapping.size()); 25290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(icon_mapping.front().icon_type, chrome::TOUCH_ICON); 25390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_TRUE(db.GetIconMappingsForPageURL(url, chrome::FAVICON, NULL)); 2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.DeleteIconMappings(url); 2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_FALSE(db.GetIconMappingsForPageURL(url, NULL)); 25890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_FALSE(db.GetIconMappingsForPageURL(url, chrome::FAVICON, NULL)); 2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ThumbnailDatabaseTest, GetIconMappingsForPageURL) { 2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase db; 2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); 2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.BeginTransaction(); 2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); 2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); 2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL url("http://google.com"); 2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 271eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch chrome::FaviconID id1 = db.AddFavicon(url, chrome::TOUCH_ICON); 2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Time time = base::Time::Now(); 2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.AddFaviconBitmap(id1, favicon, time, kSmallSize); 2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.AddFaviconBitmap(id1, favicon, time, kLargeSize); 275a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) EXPECT_LT(0, db.AddIconMapping(url, id1)); 2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 277eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch chrome::FaviconID id2 = db.AddFavicon(url, chrome::FAVICON); 2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_NE(id1, id2); 2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.AddFaviconBitmap(id2, favicon, time, kSmallSize); 280a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) EXPECT_LT(0, db.AddIconMapping(url, id2)); 2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<IconMapping> icon_mappings; 2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetIconMappingsForPageURL(url, &icon_mappings)); 2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(2u, icon_mappings.size()); 2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(id1, icon_mappings[0].icon_id); 2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(id2, icon_mappings[1].icon_id); 2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test upgrading database to version 4. 2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ThumbnailDatabaseTest, UpgradeToVersion4) { 2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase db; 2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); 2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.BeginTransaction(); 2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* name = "favicons"; 2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string sql; 2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.append("DROP TABLE IF EXISTS "); 2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.append(name); 2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.db_.Execute(sql.c_str())); 3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.resize(0); 3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.append("CREATE TABLE "); 3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.append(name); 3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.append("(" 3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "id INTEGER PRIMARY KEY," 3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "url LONGVARCHAR NOT NULL," 3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "last_updated INTEGER DEFAULT 0," 3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "image_data BLOB)"); 3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.db_.Execute(sql.c_str())); 3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.UpgradeToVersion4()); 3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL url("http://google.com"); 3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql::Statement statement; 3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) statement.Assign(db.db_.GetCachedStatement(SQL_FROM_HERE, 3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "INSERT INTO favicons (url, icon_type) VALUES (?, ?)")); 3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) statement.BindString(0, URLDatabase::GURLToDatabaseURL(url)); 31990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) statement.BindInt(1, chrome::TOUCH_ICON); 3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(statement.Run()); 3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) statement.Assign(db.db_.GetCachedStatement(SQL_FROM_HERE, 3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "SELECT icon_type FROM favicons")); 3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(statement.Step()); 3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 32690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(chrome::TOUCH_ICON, 32790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) static_cast<chrome::IconType>(statement.ColumnInt(0))); 3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test upgrading database to version 5. 3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ThumbnailDatabaseTest, UpgradeToVersion5) { 3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase db; 3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); 3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.BeginTransaction(); 3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* name = "favicons"; 3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string sql; 3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.append("DROP TABLE IF EXISTS "); 3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.append(name); 3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.db_.Execute(sql.c_str())); 3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.resize(0); 3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.append("CREATE TABLE "); 3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.append(name); 3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.append("(" 3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "id INTEGER PRIMARY KEY," 3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "url LONGVARCHAR NOT NULL," 3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "last_updated INTEGER DEFAULT 0," 3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "image_data BLOB," 3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "icon_type INTEGER DEFAULT 1)"); 3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_TRUE(db.db_.Execute(sql.c_str())); 3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_TRUE(db.UpgradeToVersion5()); 3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql = "SELECT sizes FROM favicons"; 3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.db_.Execute(sql.c_str())); 3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test upgrading database to version 6. 3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ThumbnailDatabaseTest, UpgradeToVersion6) { 3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase db; 3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); 3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.BeginTransaction(); 3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const char* name = "favicons"; 3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string sql; 3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.append("DROP TABLE IF EXISTS "); 3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.append(name); 3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.db_.Execute(sql.c_str())); 3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.clear(); 3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.append("CREATE TABLE "); 3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.append(name); 3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql.append("(" 3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "id INTEGER PRIMARY KEY," 3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "url LONGVARCHAR NOT NULL," 3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "last_updated INTEGER DEFAULT 0," 3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "image_data BLOB," 3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "icon_type INTEGER DEFAULT 1," 3805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "sizes LONGVARCHAR)"); 3815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.db_.Execute(sql.c_str())); 3825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int favicon_id = 1; 3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL url("http://google.com"); 3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int64 last_updated = Time::Now().ToInternalValue(); 3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); 3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<base::RefCountedBytes> bitmap_data( 3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) new base::RefCountedBytes(data)); 3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) sql::Statement statement; 3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) statement.Assign(db.db_.GetCachedStatement(SQL_FROM_HERE, 3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "INSERT INTO favicons (id, url, last_updated, image_data, icon_type, " 3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "sizes) VALUES (?, ?, ?, ?, ?, ?)")); 3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) statement.BindInt(0, favicon_id); 3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) statement.BindString(1, URLDatabase::GURLToDatabaseURL(url)); 3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) statement.BindInt64(2, last_updated); 3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) statement.BindBlob(3, bitmap_data->front(), 3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static_cast<int>(bitmap_data->size())); 39990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) statement.BindInt(4, chrome::TOUCH_ICON); 4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) statement.BindCString(5, "Data which happened to be there"); 4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(statement.Run()); 4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.UpgradeToVersion6()); 4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) statement.Assign(db.db_.GetCachedStatement(SQL_FROM_HERE, 4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "SELECT id, url, icon_type, sizes FROM favicons")); 4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(statement.Step()); 4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(favicon_id, statement.ColumnInt(0)); 4095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(url, GURL(statement.ColumnString(1))); 41090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(chrome::TOUCH_ICON, statement.ColumnInt(2)); 4115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Any previous data in sizes should be cleared. 412c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) EXPECT_EQ(std::string(), statement.ColumnString(3)); 4135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) statement.Assign(db.db_.GetCachedStatement(SQL_FROM_HERE, 4155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "SELECT icon_id, last_updated, image_data, width, height " 4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) "FROM favicon_bitmaps")); 4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(statement.Step()); 4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(favicon_id, statement.ColumnInt(0)); 4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(last_updated, statement.ColumnInt64(1)); 4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(static_cast<int>(bitmap_data->size()), 4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) statement.ColumnByteLength(2)); 4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(0, statement.ColumnInt(3)); 4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(0, statement.ColumnInt(4)); 4245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 426eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// Test upgrading database to version 7. 427eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen MurdochTEST_F(ThumbnailDatabaseTest, UpgradeToVersion7) { 428eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ThumbnailDatabase db; 429eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); 430eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch db.BeginTransaction(); 431eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 432eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch const char* name = "favicons"; 433eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch std::string sql; 434eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch sql.append("DROP TABLE IF EXISTS "); 435eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch sql.append(name); 436eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_TRUE(db.db_.Execute(sql.c_str())); 437eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 438eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch sql.clear(); 439eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch sql.append("CREATE TABLE "); 440eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch sql.append(name); 441eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch sql.append("(" 442eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch "id INTEGER PRIMARY KEY," 443eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch "url LONGVARCHAR NOT NULL," 444eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch "icon_type INTEGER DEFAULT 1," 445eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch "sizes LONGVARCHAR)"); 446eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_TRUE(db.db_.Execute(sql.c_str())); 447eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 448eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch int favicon_id = 1; 449eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch GURL url("http://google.com"); 450eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 451eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch sql::Statement statement; 452eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch statement.Assign(db.db_.GetCachedStatement(SQL_FROM_HERE, 453eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch "INSERT INTO favicons (id, url, icon_type, sizes) " 454eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch "VALUES (?, ?, ?, ?)")); 455eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch statement.BindInt(0, favicon_id); 456eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch statement.BindString(1, URLDatabase::GURLToDatabaseURL(url)); 457eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch statement.BindInt(2, chrome::TOUCH_ICON); 458eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch statement.BindCString(3, "Data which happened to be there"); 459eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_TRUE(statement.Run()); 460eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 461eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_TRUE(db.UpgradeToVersion7()); 462eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 463eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_FALSE(db.db_.DoesColumnExist("favicons", "sizes")); 464eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 465eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch statement.Assign(db.db_.GetCachedStatement(SQL_FROM_HERE, 466eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch "SELECT id, url, icon_type FROM favicons")); 467eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_TRUE(statement.Step()); 468eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_EQ(favicon_id, statement.ColumnInt(0)); 469eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_EQ(url, GURL(statement.ColumnString(1))); 470eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch EXPECT_EQ(chrome::TOUCH_ICON, statement.ColumnInt(2)); 471eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch} 472eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch 4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test that only data moved to a temporary table is left in the main table 4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// once the temporary table is committed. 4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ThumbnailDatabaseTest, TemporaryTables) { 4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase db; 4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); 4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.BeginTransaction(); 4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.InitTemporaryTables()); 4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); 4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); 4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL unkept_url("http://google.com/favicon2.ico"); 488eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch chrome::FaviconID unkept_id = db.AddFavicon(unkept_url, chrome::FAVICON); 4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.AddFaviconBitmap(unkept_id, favicon, base::Time::Now(), kSmallSize); 4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL kept_url("http://google.com/favicon.ico"); 492eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch chrome::FaviconID kept_id = db.AddFavicon(kept_url, chrome::FAVICON); 4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.AddFaviconBitmap(kept_id, favicon, base::Time::Now(), kLargeSize); 4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL page_url("http://google.com"); 4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.AddIconMapping(page_url, unkept_id); 4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.AddIconMapping(page_url, kept_id); 4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 49990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID new_favicon_id = 5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.CopyFaviconAndFaviconBitmapsToTemporaryTables(kept_id); 5015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_NE(0, new_favicon_id); 5025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.AddToTemporaryIconMappingTable(page_url, new_favicon_id)); 5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.CommitTemporaryTables()); 5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Only copied data should be left. 5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<IconMapping> icon_mappings; 50890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_TRUE( 50990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) db.GetIconMappingsForPageURL(page_url, chrome::FAVICON, &icon_mappings)); 5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(1u, icon_mappings.size()); 5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(new_favicon_id, icon_mappings[0].icon_id); 5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(page_url, icon_mappings[0].page_url); 5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<FaviconBitmap> favicon_bitmaps; 5155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetFaviconBitmaps(icon_mappings[0].icon_id, &favicon_bitmaps)); 5165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(1u, favicon_bitmaps.size()); 5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(kLargeSize, favicon_bitmaps[0].pixel_size); 5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_FALSE(db.GetFaviconIDForFaviconURL(unkept_url, false, NULL)); 5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Tests that deleting a favicon deletes the favicon row and favicon bitmap 5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// rows from the database. 5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ThumbnailDatabaseTest, DeleteFavicon) { 5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase db; 5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); 5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.BeginTransaction(); 5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<unsigned char> data1(blob1, blob1 + sizeof(blob1)); 5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<base::RefCountedBytes> favicon1( 5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) new base::RefCountedBytes(data1)); 5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<unsigned char> data2(blob2, blob2 + sizeof(blob2)); 5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<base::RefCountedBytes> favicon2( 5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) new base::RefCountedBytes(data2)); 5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL url("http://google.com"); 537eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch chrome::FaviconID id = db.AddFavicon(url, chrome::FAVICON); 5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Time last_updated = base::Time::Now(); 5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.AddFaviconBitmap(id, favicon1, last_updated, kSmallSize); 5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.AddFaviconBitmap(id, favicon2, last_updated, kLargeSize); 5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetFaviconBitmaps(id, NULL)); 5435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.DeleteFavicon(id)); 5455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_FALSE(db.GetFaviconBitmaps(id, NULL)); 5465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 5475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ThumbnailDatabaseTest, GetIconMappingsForPageURLForReturnOrder) { 5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase db; 5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); 5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.BeginTransaction(); 5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Add a favicon 5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); 5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); 5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL page_url("http://google.com"); 5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL icon_url("http://google.com/favicon.ico"); 5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Time time = base::Time::Now(); 5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 56190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID id = db.AddFavicon(icon_url, 56290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FAVICON, 56390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) favicon, 56490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) time, 56590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) gfx::Size()); 5665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_NE(0, db.AddIconMapping(page_url, id)); 5675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<IconMapping> icon_mappings; 5685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetIconMappingsForPageURL(page_url, &icon_mappings)); 5695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(page_url, icon_mappings.front().page_url); 5715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(id, icon_mappings.front().icon_id); 57290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(chrome::FAVICON, icon_mappings.front().icon_type); 5735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(icon_url, icon_mappings.front().icon_url); 5745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Add a touch icon 5765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<unsigned char> data2(blob2, blob2 + sizeof(blob2)); 5775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<base::RefCountedBytes> favicon2 = 5785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) new base::RefCountedBytes(data); 5795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 58090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID id2 = db.AddFavicon(icon_url, 58190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::TOUCH_ICON, 58290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) favicon2, 58390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) time, 58490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) gfx::Size()); 5855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_NE(0, db.AddIconMapping(page_url, id2)); 5865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) icon_mappings.clear(); 5885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetIconMappingsForPageURL(page_url, &icon_mappings)); 5895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(page_url, icon_mappings.front().page_url); 5915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(id2, icon_mappings.front().icon_id); 59290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(chrome::TOUCH_ICON, icon_mappings.front().icon_type); 5935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(icon_url, icon_mappings.front().icon_url); 5945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Add a touch precomposed icon 5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<base::RefCountedBytes> favicon3 = 5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) new base::RefCountedBytes(data2); 5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 59990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID id3 = db.AddFavicon(icon_url, 60090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::TOUCH_PRECOMPOSED_ICON, 60190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) favicon3, 60290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) time, 60390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) gfx::Size()); 6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_NE(0, db.AddIconMapping(page_url, id3)); 6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 6065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) icon_mappings.clear(); 6075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetIconMappingsForPageURL(page_url, &icon_mappings)); 6085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 6095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(page_url, icon_mappings.front().page_url); 6105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(id3, icon_mappings.front().icon_id); 61190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(chrome::TOUCH_PRECOMPOSED_ICON, icon_mappings.front().icon_type); 6125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(icon_url, icon_mappings.front().icon_url); 6135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 6145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 6155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Test result of GetIconMappingsForPageURL when an icon type is passed in. 6165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ThumbnailDatabaseTest, GetIconMappingsForPageURLWithIconType) { 6175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase db; 6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); 6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.BeginTransaction(); 6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL url("http://google.com"); 6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); 6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); 6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Time time = base::Time::Now(); 6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 62690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID id1 = db.AddFavicon(url, 62790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FAVICON, 62890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) favicon, 62990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) time, 63090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) gfx::Size()); 6315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_NE(0, db.AddIconMapping(url, id1)); 6325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 63390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID id2 = db.AddFavicon(url, 63490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::TOUCH_ICON, 63590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) favicon, 63690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) time, 63790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) gfx::Size()); 6385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_NE(0, db.AddIconMapping(url, id2)); 6395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 64090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID id3 = db.AddFavicon(url, 64190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::TOUCH_ICON, 64290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) favicon, 64390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) time, 64490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) gfx::Size()); 6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_NE(0, db.AddIconMapping(url, id3)); 6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Only the mappings for favicons of type TOUCH_ICON should be returned as 6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // TOUCH_ICON is a larger icon type than FAVICON. 6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<IconMapping> icon_mappings; 6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetIconMappingsForPageURL( 6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) url, 65290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FAVICON | chrome::TOUCH_ICON | chrome::TOUCH_PRECOMPOSED_ICON, 6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) &icon_mappings)); 6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(2u, icon_mappings.size()); 6565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (id2 == icon_mappings[0].icon_id) { 6575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(id3, icon_mappings[1].icon_id); 6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 6595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(id3, icon_mappings[0].icon_id); 6605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(id2, icon_mappings[1].icon_id); 6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) icon_mappings.clear(); 66490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_TRUE( 66590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) db.GetIconMappingsForPageURL(url, chrome::TOUCH_ICON, &icon_mappings)); 6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (id2 == icon_mappings[0].icon_id) { 6675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(id3, icon_mappings[1].icon_id); 6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else { 6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(id3, icon_mappings[0].icon_id); 6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(id2, icon_mappings[1].icon_id); 6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) icon_mappings.clear(); 67490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_TRUE( 67590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) db.GetIconMappingsForPageURL(url, chrome::FAVICON, &icon_mappings)); 6765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(1u, icon_mappings.size()); 6775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(id1, icon_mappings[0].icon_id); 6785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 6795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 6805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ThumbnailDatabaseTest, HasMappingFor) { 6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase db; 6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); 6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.BeginTransaction(); 6845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 6855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); 6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); 6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 6885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Add a favicon which will have icon_mappings 6895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Time time = base::Time::Now(); 69090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID id1 = db.AddFavicon(GURL("http://google.com"), 69190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FAVICON, 69290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) favicon, 69390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) time, 69490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) gfx::Size()); 6955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_NE(id1, 0); 6965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 6975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Add another type of favicon 6985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) time = base::Time::Now(); 69990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID id2 = db.AddFavicon(GURL("http://www.google.com/icon"), 70090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::TOUCH_ICON, 70190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) favicon, 70290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) time, 70390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) gfx::Size()); 7045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_NE(id2, 0); 7055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Add 3rd favicon 7075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) time = base::Time::Now(); 70890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID id3 = db.AddFavicon(GURL("http://www.google.com/icon"), 70990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::TOUCH_ICON, 71090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) favicon, 71190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) time, 71290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) gfx::Size()); 7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_NE(id3, 0); 7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Add 2 icon mapping 7165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL page_url("http://www.google.com"); 7175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.AddIconMapping(page_url, id1)); 7185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.AddIconMapping(page_url, id2)); 7195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.HasMappingFor(id1)); 7215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.HasMappingFor(id2)); 7225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_FALSE(db.HasMappingFor(id3)); 7235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Remove all mappings 7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.DeleteIconMappings(page_url); 7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_FALSE(db.HasMappingFor(id1)); 7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_FALSE(db.HasMappingFor(id2)); 7285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_FALSE(db.HasMappingFor(id3)); 7295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 7305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ThumbnailDatabaseTest, CloneIconMappings) { 7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase db; 7335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); 7345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.BeginTransaction(); 7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); 7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); 7385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Add a favicon which will have icon_mappings 74090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID id1 = db.AddFavicon( 741eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch GURL("http://google.com"), chrome::FAVICON); 7425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_NE(0, id1); 7435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) base::Time time = base::Time::Now(); 7445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.AddFaviconBitmap(id1, favicon, time, gfx::Size()); 7455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Add another type of favicon 74790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID id2 = db.AddFavicon(GURL("http://www.google.com/icon"), 748eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch chrome::TOUCH_ICON); 7495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_NE(0, id2); 7505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) time = base::Time::Now(); 7515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.AddFaviconBitmap(id2, favicon, time, gfx::Size()); 7525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Add 3rd favicon 75490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID id3 = db.AddFavicon(GURL("http://www.google.com/icon"), 755eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch chrome::TOUCH_ICON); 7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_NE(0, id3); 7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) time = base::Time::Now(); 7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.AddFaviconBitmap(id3, favicon, time, gfx::Size()); 7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL page1_url("http://page1.com"); 7615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.AddIconMapping(page1_url, id1)); 7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.AddIconMapping(page1_url, id2)); 7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL page2_url("http://page2.com"); 7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.AddIconMapping(page2_url, id3)); 7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Test we do nothing with existing mappings. 7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<IconMapping> icon_mapping; 7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetIconMappingsForPageURL(page2_url, &icon_mapping)); 7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(1U, icon_mapping.size()); 7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.CloneIconMappings(page1_url, page2_url)); 7735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) icon_mapping.clear(); 7755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetIconMappingsForPageURL(page2_url, &icon_mapping)); 7765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(1U, icon_mapping.size()); 7775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(page2_url, icon_mapping[0].page_url); 7785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(id3, icon_mapping[0].icon_id); 7795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Test we clone if the new page has no mappings. 7815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL page3_url("http://page3.com"); 7825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.CloneIconMappings(page1_url, page3_url)); 7835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) icon_mapping.clear(); 7855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetIconMappingsForPageURL(page3_url, &icon_mapping)); 7865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(2U, icon_mapping.size()); 7885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (icon_mapping[0].icon_id == id2) 7895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::swap(icon_mapping[0], icon_mapping[1]); 7905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(page3_url, icon_mapping[0].page_url); 7915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(id1, icon_mapping[0].icon_id); 7925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(page3_url, icon_mapping[1].page_url); 7935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(id2, icon_mapping[1].icon_id); 7945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 7955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 7965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(IconMappingMigrationTest, TestIconMappingMigration) { 7975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) HistoryDatabase history_db; 7985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_TRUE(history_db.db_.Open(history_db_name_)); 7995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) history_db.BeginTransaction(); 8005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 8015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const GURL icon1 = GURL("http://www.google.com/favicon.ico"); 8025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const GURL icon2 = GURL("http://www.yahoo.com/favicon.ico"); 8035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 8045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase db; 8055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(sql::INIT_OK, db.Init(thumbnail_db_name_, NULL, &history_db)); 8065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.BeginTransaction(); 8075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 8085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Migration should be done. 8095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Test one icon_mapping. 8105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL page_url1 = GURL("http://google.com/"); 8115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<IconMapping> icon_mappings; 8125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetIconMappingsForPageURL(page_url1, &icon_mappings)); 8135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(1u, icon_mappings.size()); 81490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(chrome::FAVICON, icon_mappings[0].icon_type); 8155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(page_url1, icon_mappings[0].page_url); 8165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(1, icon_mappings[0].icon_id); 8175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(icon1, icon_mappings[0].icon_url); 8185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 8195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Test a page which has the same icon. 8205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL page_url3 = GURL("http://www.google.com/"); 8215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) icon_mappings.clear(); 8225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetIconMappingsForPageURL(page_url3, &icon_mappings)); 8235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(1u, icon_mappings.size()); 82490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(chrome::FAVICON, icon_mappings[0].icon_type); 8255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(page_url3, icon_mappings[0].page_url); 8265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(1, icon_mappings[0].icon_id); 8275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(icon1, icon_mappings[0].icon_url); 8285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 8295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Test a icon_mapping with different IconID. 8305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL page_url2 = GURL("http://yahoo.com/"); 8315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) icon_mappings.clear(); 8325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(db.GetIconMappingsForPageURL(page_url2, &icon_mappings)); 8335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(1u, icon_mappings.size()); 83490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(chrome::FAVICON, icon_mappings[0].icon_type); 8355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(page_url2, icon_mappings[0].page_url); 8365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(2, icon_mappings[0].icon_id); 8375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(icon2, icon_mappings[0].icon_url); 8385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 8395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Test a page without icon 8405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL page_url4 = GURL("http://www.google.com/blank.html"); 8415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_FALSE(db.GetIconMappingsForPageURL(page_url4, NULL)); 8425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 8435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 8445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(ThumbnailDatabaseTest, IconMappingEnumerator) { 8455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase db; 8465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_EQ(sql::INIT_OK, db.Init(file_name_, NULL, NULL)); 8475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) db.BeginTransaction(); 8485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 8495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::vector<unsigned char> data(blob1, blob1 + sizeof(blob1)); 8505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); 8515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 8525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL url("http://google.com"); 8535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL icon_url1("http://google.com/favicon.ico"); 85490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID touch_icon_id1 = db.AddFavicon(icon_url1, 85590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::TOUCH_ICON, 85690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) favicon, 85790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::Time::Now(), 85890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) gfx::Size()); 8595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_NE(0, touch_icon_id1); 8605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) IconMappingID touch_mapping_id1 = db.AddIconMapping(url, touch_icon_id1); 8615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_NE(0, touch_mapping_id1); 8625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 86390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID favicon_id1 = db.AddFavicon(icon_url1, 86490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FAVICON, 86590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) favicon, 86690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::Time::Now(), 86790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) gfx::Size()); 8685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_NE(0, favicon_id1); 8695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) IconMappingID favicon_mapping_id1 = db.AddIconMapping(url, favicon_id1); 8705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_NE(0, favicon_mapping_id1); 8715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 8725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL url2("http://chromium.org"); 8735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) GURL icon_url2("http://chromium.org/favicon.ico"); 87490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FaviconID favicon_id2 = db.AddFavicon(icon_url2, 87590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) chrome::FAVICON, 87690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) favicon, 87790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::Time::Now(), 87890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) gfx::Size()); 8795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_NE(0, favicon_id2); 8805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) IconMappingID favicon_mapping_id2 = db.AddIconMapping(url2, favicon_id2); 8815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_NE(0, favicon_mapping_id2); 8825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 8835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) IconMapping icon_mapping; 8845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase::IconMappingEnumerator enumerator1; 88590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ASSERT_TRUE(db.InitIconMappingEnumerator(chrome::FAVICON, &enumerator1)); 8865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // There are 2 favicon mappings. 8875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool has_favicon_mapping1 = false; 8885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool has_favicon_mapping2 = false; 8895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int mapping_count = 0; 8905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) while (enumerator1.GetNextIconMapping(&icon_mapping)) { 8915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) mapping_count++; 8925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (favicon_mapping_id1 == icon_mapping.mapping_id) { 8935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) has_favicon_mapping1 = true; 8945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(url, icon_mapping.page_url); 8955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(favicon_id1, icon_mapping.icon_id); 8965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(icon_url1, icon_mapping.icon_url); 89790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(chrome::FAVICON, icon_mapping.icon_type); 8985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } else if (favicon_mapping_id2 == icon_mapping.mapping_id) { 8995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) has_favicon_mapping2 = true; 9005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(url2, icon_mapping.page_url); 9015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(favicon_id2, icon_mapping.icon_id); 9025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(icon_url2, icon_mapping.icon_url); 90390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(chrome::FAVICON, icon_mapping.icon_type); 9045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 9055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 9065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(2, mapping_count); 9075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(has_favicon_mapping1); 9085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_TRUE(has_favicon_mapping2); 9095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 9105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ThumbnailDatabase::IconMappingEnumerator enumerator2; 91190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ASSERT_TRUE(db.InitIconMappingEnumerator(chrome::TOUCH_ICON, &enumerator2)); 9125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ASSERT_TRUE(enumerator2.GetNextIconMapping(&icon_mapping)); 9135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(touch_mapping_id1, icon_mapping.mapping_id); 9145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(url, icon_mapping.page_url); 9155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(touch_icon_id1, icon_mapping.icon_id); 9165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_EQ(icon_url1, icon_mapping.icon_url); 91790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) EXPECT_EQ(chrome::TOUCH_ICON, icon_mapping.icon_type); 9185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 9195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) EXPECT_FALSE(enumerator2.GetNextIconMapping(&icon_mapping)); 9205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 9215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 9225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace history 923