Index: components/history/core/browser/thumbnail_database_unittest.cc |
diff --git a/components/history/core/browser/thumbnail_database_unittest.cc b/components/history/core/browser/thumbnail_database_unittest.cc |
index cd1c83fafcc3d9e84992705ca98c55521be199e4..7aadf601f8a92b58ff63c8ce312503f3c1075746 100644 |
--- a/components/history/core/browser/thumbnail_database_unittest.cc |
+++ b/components/history/core/browser/thumbnail_database_unittest.cc |
@@ -12,16 +12,21 @@ |
#include "base/files/scoped_temp_dir.h" |
#include "base/memory/ref_counted_memory.h" |
#include "base/path_service.h" |
+#include "components/history/core/browser/history_backend_client.h" |
#include "components/history/core/browser/thumbnail_database.h" |
#include "components/history/core/test/database_test_utils.h" |
#include "sql/connection.h" |
#include "sql/recovery.h" |
#include "sql/test/scoped_error_expecter.h" |
#include "sql/test/test_helpers.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
#include "testing/gtest/include/gtest/gtest.h" |
#include "third_party/sqlite/sqlite3.h" |
#include "url/gurl.h" |
+using testing::StrictMock; |
+using testing::Return; |
+ |
namespace history { |
namespace { |
@@ -147,6 +152,26 @@ WARN_UNUSED_RESULT bool CheckPageHasIcon( |
return true; |
} |
+class MockHistoryBackendClient : public HistoryBackendClient { |
+ public: |
+ // MOCK_METHOD0(~HistoryBackendClient, void()); |
+ MOCK_METHOD1(IsBookmarked, bool(const GURL& url)); |
+ MOCK_METHOD1(GetBookmarks, void(std::vector<URLAndTitle>* bookmarks)); |
+ MOCK_METHOD0(ShouldReportDatabaseError, bool()); |
+ MOCK_METHOD1(IsWebSafe, bool(const GURL& url)); |
+ |
+#if defined(OS_ANDROID) |
+ MOCK_METHOD4(OnHistoryBackendInitialized, |
+ void(HistoryBackend* history_backend, |
+ HistoryDatabase* history_database, |
+ ThumbnailDatabase* thumbnail_database, |
+ const base::FilePath& history_dir)); |
+ MOCK_METHOD2(OnHistoryBackendDestroyed, |
+ void(HistoryBackend* history_backend, |
+ const base::FilePath& history_dir)); |
+#endif // defined(OS_ANDROID) |
+}; |
+ |
} // namespace |
class ThumbnailDatabaseTest : public testing::Test { |
@@ -203,37 +228,109 @@ TEST_F(ThumbnailDatabaseTest, AddIconMapping) { |
EXPECT_EQ(id, icon_mappings.front().icon_id); |
} |
-TEST_F(ThumbnailDatabaseTest, LastRequestedTime) { |
+TEST_F(ThumbnailDatabaseTest, CleanUnusedFavicons) { |
+ StrictMock<MockHistoryBackendClient> mock_client; |
+ ThumbnailDatabase db(&mock_client); |
+ ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); |
+ db.BeginTransaction(); |
+ |
+ base::Time start; |
+ ASSERT_TRUE(base::Time::FromUTCExploded({2017, 5, 0, 1, 0, 0, 0, 0}, &start)); |
+ std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1)); |
+ scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); |
+ |
+ // Icon 1: recently used case. |
+ GURL url1("http://google.com"); |
+ favicon_base::FaviconID icon1 = db.AddFavicon(url1, favicon_base::FAVICON); |
+ EXPECT_NE(0, icon1); |
+ FaviconBitmapID bitmap1 = |
+ db.AddFaviconBitmap(icon1, favicon, start, gfx::Size()); |
+ EXPECT_NE(0, bitmap1); |
+ EXPECT_NE(0, db.AddIconMapping(url1, icon1)); |
+ |
+ // Icon 2: not recently used case (not bookmarked). |
+ GURL url2("http://youtube.com"); |
+ EXPECT_CALL(mock_client, IsBookmarked(url2)).WillOnce(Return(false)); |
+ favicon_base::FaviconID icon2 = db.AddFavicon(url2, favicon_base::FAVICON); |
+ EXPECT_NE(0, icon2); |
+ EXPECT_NE(0, db.AddFaviconBitmap(icon2, favicon, start, gfx::Size())); |
+ EXPECT_NE(0, db.AddIconMapping(url2, icon2)); |
+ |
+ // Icon 3: not recently used case (bookmarked). |
+ GURL url3("http://photos.google.com"); |
+ EXPECT_CALL(mock_client, IsBookmarked(url3)).WillOnce(Return(true)); |
+ favicon_base::FaviconID icon3 = db.AddFavicon(url3, favicon_base::FAVICON); |
+ EXPECT_NE(0, icon3); |
+ EXPECT_NE(0, db.AddFaviconBitmap(icon3, favicon, start, gfx::Size())); |
+ EXPECT_NE(0, db.AddIconMapping(url3, icon3)); |
+ |
+ // Update the last_requested info for the first icon 3 weeks later. |
+ scoped_refptr<base::RefCountedMemory> bitmap_data_out; |
+ gfx::Size pixel_size_out; |
+ EXPECT_TRUE(db.GetFaviconBitmap(bitmap1, |
+ start + base::TimeDelta::FromDays(21), |
+ nullptr, &bitmap_data_out, &pixel_size_out)); |
+ |
+ db.CleanUnusedFavicons(start + base::TimeDelta::FromDays(14)); |
+ |
+ // The first icon is not deleted. |
+ EXPECT_TRUE(db.GetFaviconHeader(icon1, nullptr, nullptr)); |
+ std::vector<FaviconBitmap> favicon_bitmaps1; |
+ EXPECT_TRUE(db.GetFaviconBitmaps(icon1, &favicon_bitmaps1)); |
+ ASSERT_EQ(1u, favicon_bitmaps1.size()); |
+ std::vector<IconMapping> icon_mapping1; |
+ EXPECT_TRUE(db.GetIconMappingsForPageURL(url1, &icon_mapping1)); |
+ ASSERT_EQ(1u, icon_mapping1.size()); |
+ EXPECT_EQ(icon1, icon_mapping1[0].icon_id); |
+ |
+ // The second icon gets deleted with all details. |
+ EXPECT_FALSE(db.GetIconMappingsForPageURL(url2, nullptr)); |
+ EXPECT_FALSE(db.GetFaviconHeader(icon2, nullptr, nullptr)); |
+ EXPECT_FALSE(db.GetFaviconBitmaps(icon2, nullptr)); |
+ |
+ // The third icon is not deleted because the page URL is bookmarked. |
+ EXPECT_TRUE(db.GetFaviconHeader(icon3, nullptr, nullptr)); |
+ std::vector<FaviconBitmap> favicon_bitmaps3; |
+ EXPECT_TRUE(db.GetFaviconBitmaps(icon3, &favicon_bitmaps3)); |
+ ASSERT_EQ(1u, favicon_bitmaps3.size()); |
+ std::vector<IconMapping> icon_mapping3; |
+ EXPECT_TRUE(db.GetIconMappingsForPageURL(url3, &icon_mapping3)); |
+ ASSERT_EQ(1u, icon_mapping3.size()); |
+ EXPECT_EQ(icon3, icon_mapping3[0].icon_id); |
+} |
+ |
+TEST_F(ThumbnailDatabaseTest, DoNotUpdateLastRequestedTimeIfZero) { |
ThumbnailDatabase db(NULL); |
ASSERT_EQ(sql::INIT_OK, db.Init(file_name_)); |
db.BeginTransaction(); |
+ base::Time start; |
+ ASSERT_TRUE(base::Time::FromUTCExploded({2017, 5, 0, 1, 0, 0, 0, 0}, &start)); |
std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1)); |
scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data)); |
GURL url("http://google.com"); |
- base::Time now = base::Time::Now(); |
- favicon_base::FaviconID id = |
- db.AddFavicon(url, favicon_base::TOUCH_ICON, favicon, now, gfx::Size()); |
- ASSERT_NE(0, id); |
- |
- // Fetching the last requested time of a non-existent bitmap should fail. |
- base::Time last_requested = base::Time::UnixEpoch(); |
- EXPECT_FALSE(db.GetFaviconBitmap(id + 1, NULL, &last_requested, NULL, NULL)); |
- EXPECT_EQ(last_requested, base::Time::UnixEpoch()); // Remains unchanged. |
- |
- // Fetching the last requested time of a bitmap that has no last request |
- // should return a null timestamp. |
- last_requested = base::Time::UnixEpoch(); |
- EXPECT_TRUE(db.GetFaviconBitmap(id, NULL, &last_requested, NULL, NULL)); |
- EXPECT_TRUE(last_requested.is_null()); |
- |
- // Setting the last requested time of an existing bitmap should succeed, and |
- // the set time should be returned by the corresponding "Get". |
- last_requested = base::Time::UnixEpoch(); |
- EXPECT_TRUE(db.SetFaviconBitmapLastRequestedTime(id, now)); |
- EXPECT_TRUE(db.GetFaviconBitmap(id, NULL, &last_requested, NULL, NULL)); |
- EXPECT_EQ(last_requested, now); |
+ favicon_base::FaviconID icon = db.AddFavicon(url, favicon_base::FAVICON); |
+ EXPECT_NE(0, icon); |
+ FaviconBitmapID bitmap = |
+ db.AddFaviconBitmap(icon, favicon, start, gfx::Size()); |
+ EXPECT_NE(0, bitmap); |
+ EXPECT_NE(0, db.AddIconMapping(url, icon)); |
+ |
+ // Access the icon 3 weeks later with zero time - this happens e.g. from an |
+ // incognito profile. |
+ scoped_refptr<base::RefCountedMemory> bitmap_data_out; |
+ gfx::Size pixel_size_out; |
+ EXPECT_TRUE(db.GetFaviconBitmap(bitmap, base::Time(), nullptr, |
+ &bitmap_data_out, &pixel_size_out)); |
+ |
+ db.CleanUnusedFavicons(start + base::TimeDelta::FromDays(14)); |
+ |
+ // The icon gets deleted with all details because the (incognito) visit with |
+ // zero time did not count. |
+ EXPECT_FALSE(db.GetIconMappingsForPageURL(url, nullptr)); |
+ EXPECT_FALSE(db.GetFaviconHeader(icon, nullptr, nullptr)); |
+ EXPECT_FALSE(db.GetFaviconBitmaps(icon, nullptr)); |
} |
TEST_F(ThumbnailDatabaseTest, DeleteIconMappings) { |