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

Unified Diff: components/history/core/browser/thumbnail_database_unittest.cc

Issue 2903573002: [Thumbnails DB] Add functionality to clear unused on-demand favicons. (Closed)
Patch Set: Another build fix Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: 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 4dbd0c6f64e00039c9c134db30ee9858dd1d6d3f..22074b3e71622ec4b0c6420b3db476d03cc56e81 100644
--- a/components/history/core/browser/thumbnail_database_unittest.cc
+++ b/components/history/core/browser/thumbnail_database_unittest.cc
@@ -12,16 +12,27 @@
#include "base/files/scoped_temp_dir.h"
#include "base/memory/ref_counted_memory.h"
#include "base/path_service.h"
+#include "base/strings/stringprintf.h"
+#include "build/build_config.h"
+#include "components/history/core/browser/history_backend_client.h"
pkotwicz 2017/07/10 05:23:28 Do you need this include?
jkrcal 2017/07/10 16:14:30 Ah, correct. Thanks!
#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::AllOf;
+using testing::ElementsAre;
+using testing::Field;
+using testing::StrictMock;
pkotwicz 2017/07/10 05:23:28 Do you need this line?
jkrcal 2017/07/10 16:14:30 Done.
+using testing::Pair;
+using testing::Return;
+
namespace history {
namespace {
@@ -169,6 +180,26 @@ void SortMappingsByIconUrl(std::vector<IconMapping>* mappings) {
std::sort(mappings->begin(), mappings->end(), &CompareIconMappingIconUrl);
}
+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 {
@@ -367,6 +398,131 @@ TEST_F(ThumbnailDatabaseTest, TouchDoesNotUpdateStandardFavicons) {
EXPECT_EQ(base::Time(), last_requested); // No update.
}
+// Test that ThumbnailDatabase::GetOldOnDemandFavicons() returns on-demand icons
+// which were requested prior to the passed in timestamp.
+TEST_F(ThumbnailDatabaseTest, GetOldOnDemandFaviconsReturnsOld) {
+ ThumbnailDatabase db(nullptr);
+ 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: old unused case.
pkotwicz 2017/07/10 05:23:28 You don't need the comment here because you have a
jkrcal 2017/07/10 16:14:30 Done.
+ GURL url("http://google.com/favicon.ico");
+ favicon_base::FaviconID icon =
+ db.AddFavicon(url, favicon_base::FAVICON, favicon,
+ FaviconBitmapType::ON_DEMAND, start, gfx::Size());
+ ASSERT_NE(0, icon);
+ // Associate two different URLs with the icon.
+ GURL page_url1("http://google.com/1");
+ ASSERT_NE(0, db.AddIconMapping(page_url1, icon));
+ GURL page_url2("http://google.com/2");
+ ASSERT_NE(0, db.AddIconMapping(page_url2, icon));
+
+ base::Time get_older_than = start + base::TimeDelta::FromSeconds(1);
+ auto map = db.GetOldOnDemandFavicons(get_older_than);
+
+ // The icon is returned.
+ EXPECT_THAT(map, ElementsAre(Pair(
+ icon, AllOf(Field(&FaviconURLs::icon_url, url),
+ Field(&FaviconURLs::page_urls,
+ ElementsAre(page_url1, page_url2))))));
+}
+
+// Test that ThumbnailDatabase::GetOldOnDemandFavicons() returns on-visit icons
+// if the on-visit icons have expired. We need this behavior in order to delete
+// icons stored via HistoryService::SetOnDemandFavicons() prior to on-demand
+// icons setting the "last_requested" time.
+TEST_F(ThumbnailDatabaseTest, GetOldOnDemandFaviconsReturnsExpired) {
+ ThumbnailDatabase db(nullptr);
+ 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: standard favicon (not on-demand) but expired.
+ GURL url("http://google.com/favicon.ico");
+ favicon_base::FaviconID icon =
+ db.AddFavicon(url, favicon_base::FAVICON, favicon,
+ FaviconBitmapType::ON_VISIT, start, gfx::Size());
+ ASSERT_NE(0, icon);
+ GURL page_url("http://google.com/");
+ ASSERT_NE(0, db.AddIconMapping(page_url, icon));
+ ASSERT_TRUE(db.SetFaviconOutOfDate(icon));
+
+ // The threshold is ignored for expired icons.
+ auto map = db.GetOldOnDemandFavicons(/*threshold=*/base::Time::Now());
+
+ // The icon is returned.
+ EXPECT_THAT(map,
+ ElementsAre(Pair(icon, AllOf(Field(&FaviconURLs::icon_url, url),
+ Field(&FaviconURLs::page_urls,
+ ElementsAre(page_url))))));
+}
+
+// Test that ThumbnailDatabase::GetOldOnDemandFavicons() does not return
+// on-demand icons which were requested after to the passed in timestamp.
pkotwicz 2017/07/10 05:23:28 Nit: "after to the" -> "after the"
jkrcal 2017/07/10 16:14:30 Done. (sorry for not proof-reading your previous p
+TEST_F(ThumbnailDatabaseTest, GetOldOnDemandFaviconsDoesNotReturnFresh) {
+ ThumbnailDatabase db(nullptr);
+ 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: freshly used case.
+ GURL url("http://google.com/favicon.ico");
+ favicon_base::FaviconID icon =
+ db.AddFavicon(url, favicon_base::FAVICON, favicon,
+ FaviconBitmapType::ON_DEMAND, start, gfx::Size());
+ ASSERT_NE(0, icon);
+ ASSERT_NE(0, db.AddIconMapping(GURL("http://google.com/"), icon));
+
+ // Touch the icon 3 weeks later.
+ base::Time now = start + base::TimeDelta::FromDays(21);
+ EXPECT_TRUE(db.TouchOnDemandFavicon(url, now));
+
+ base::Time get_older_than = start + base::TimeDelta::FromSeconds(1);
+ auto list = db.GetOldOnDemandFavicons(get_older_than);
+
+ // No icon is returned.
+ EXPECT_TRUE(list.empty());
+}
+
+// Test that ThumbnailDatabase::GetOldOnDemandFavicons() does not return
+// non-expired on-visit icons.
+TEST_F(ThumbnailDatabaseTest, GetOldOnDemandFaviconsDoesNotDeleteStandard) {
+ ThumbnailDatabase db(nullptr);
+ 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: standard favicon (not on-demand).
+ favicon_base::FaviconID icon = db.AddFavicon(
+ GURL("http://google.com/favicon.ico"), favicon_base::FAVICON, favicon,
+ FaviconBitmapType::ON_VISIT, start, gfx::Size());
+ ASSERT_NE(0, icon);
+ ASSERT_NE(0, db.AddIconMapping(GURL("http://google.com/"), icon));
+
+ base::Time get_older_than = start + base::TimeDelta::FromSeconds(1);
+ auto list = db.GetOldOnDemandFavicons(get_older_than);
+
+ // No icon is returned.
+ EXPECT_TRUE(list.empty());
+}
+
TEST_F(ThumbnailDatabaseTest, DeleteIconMappings) {
ThumbnailDatabase db(NULL);
ASSERT_EQ(sql::INIT_OK, db.Init(file_name_));

Powered by Google App Engine
This is Rietveld 408576698