favicon_backend_wrapper_unittest.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "weblayer/browser/favicon/favicon_backend_wrapper.h"
  5. #include <vector>
  6. #include "base/files/file_path.h"
  7. #include "base/files/scoped_temp_dir.h"
  8. #include "base/memory/ref_counted_memory.h"
  9. #include "base/test/task_environment.h"
  10. #include "base/threading/thread_task_runner_handle.h"
  11. #include "base/time/time.h"
  12. #include "components/favicon/core/favicon_backend.h"
  13. #include "components/favicon/core/favicon_database.h"
  14. #include "testing/gtest/include/gtest/gtest.h"
  15. #include "url/gurl.h"
  16. namespace weblayer {
  17. namespace {
  18. // Blobs for adding favicons.
  19. const unsigned char kBlob1[] =
  20. "12346102356120394751634516591348710478123649165419234519234512349134";
  21. } // namespace
  22. class FaviconBackendWrapperTest : public testing::Test {
  23. protected:
  24. favicon::FaviconBackend* backend() {
  25. return wrapper_->favicon_backend_.get();
  26. }
  27. // testing::Test:
  28. void SetUp() override {
  29. // Get a temporary directory for the test DB files.
  30. ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  31. db_path_ = temp_dir_.GetPath().AppendASCII("test_db");
  32. }
  33. void TearDown() override {
  34. wrapper_ = nullptr;
  35. testing::Test::TearDown();
  36. }
  37. base::test::SingleThreadTaskEnvironment task_environment_{
  38. base::test::TaskEnvironment::TimeSource::MOCK_TIME};
  39. base::ScopedTempDir temp_dir_;
  40. base::FilePath db_path_;
  41. scoped_refptr<FaviconBackendWrapper> wrapper_;
  42. };
  43. TEST_F(FaviconBackendWrapperTest, BasicExpire) {
  44. wrapper_ = base::MakeRefCounted<FaviconBackendWrapper>(
  45. base::ThreadTaskRunnerHandle::Get());
  46. wrapper_->Init(db_path_);
  47. ASSERT_TRUE(backend());
  48. auto* db = backend()->db();
  49. std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1));
  50. scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data));
  51. GURL url("http://google.com");
  52. const base::Time time1 = base::Time::Now();
  53. favicon_base::FaviconID favicon_id1 =
  54. db->AddFavicon(url, favicon_base::IconType::kTouchIcon, favicon,
  55. favicon::FaviconBitmapType::ON_VISIT, time1, gfx::Size());
  56. ASSERT_NE(0, favicon_id1);
  57. favicon::IconMappingID icon_mapping_id1 =
  58. db->AddIconMapping(url, favicon_id1);
  59. ASSERT_NE(0, icon_mapping_id1);
  60. // Fast forward past first expire running.
  61. task_environment_.FastForwardBy(kTimeDeltaForRunningExpireWithRemainingWork *
  62. 2);
  63. // The icon should still be there.
  64. EXPECT_TRUE(db->GetFaviconHeader(favicon_id1, nullptr, nullptr));
  65. EXPECT_TRUE(db->HasMappingFor(favicon_id1));
  66. // Fast forward such that the icon is removed.
  67. task_environment_.FastForwardBy(kTimeDeltaWhenEntriesAreRemoved);
  68. EXPECT_FALSE(db->GetFaviconHeader(favicon_id1, nullptr, nullptr));
  69. EXPECT_FALSE(db->HasMappingFor(favicon_id1));
  70. }
  71. TEST_F(FaviconBackendWrapperTest, ExpireWithOneRemaining) {
  72. wrapper_ = base::MakeRefCounted<FaviconBackendWrapper>(
  73. base::ThreadTaskRunnerHandle::Get());
  74. wrapper_->Init(db_path_);
  75. ASSERT_TRUE(backend());
  76. auto* db = backend()->db();
  77. // Add two entries. The second is more recent then the first.
  78. std::vector<unsigned char> data(kBlob1, kBlob1 + sizeof(kBlob1));
  79. scoped_refptr<base::RefCountedBytes> favicon(new base::RefCountedBytes(data));
  80. GURL url("http://google.com");
  81. const base::Time time1 = base::Time::Now();
  82. favicon_base::FaviconID favicon_id1 =
  83. db->AddFavicon(url, favicon_base::IconType::kTouchIcon, favicon,
  84. favicon::FaviconBitmapType::ON_VISIT, time1, gfx::Size());
  85. ASSERT_NE(0, favicon_id1);
  86. favicon::IconMappingID icon_mapping_id1 =
  87. db->AddIconMapping(url, favicon_id1);
  88. ASSERT_NE(0, icon_mapping_id1);
  89. const base::Time time2 = time1 + kTimeDeltaWhenEntriesAreRemoved / 2;
  90. favicon_base::FaviconID favicon_id2 =
  91. db->AddFavicon(url, favicon_base::IconType::kTouchIcon, favicon,
  92. favicon::FaviconBitmapType::ON_VISIT, time2, gfx::Size());
  93. ASSERT_NE(0, favicon_id2);
  94. favicon::IconMappingID icon_mapping_id2 =
  95. db->AddIconMapping(url, favicon_id2);
  96. ASSERT_NE(0, icon_mapping_id2);
  97. // Fast forward such the first entry is expired and should be removed, but
  98. // not the second.
  99. task_environment_.FastForwardBy(kTimeDeltaWhenEntriesAreRemoved +
  100. base::Days(1));
  101. EXPECT_FALSE(db->GetFaviconHeader(favicon_id1, nullptr, nullptr));
  102. EXPECT_FALSE(db->HasMappingFor(favicon_id1));
  103. EXPECT_TRUE(db->GetFaviconHeader(favicon_id2, nullptr, nullptr));
  104. EXPECT_TRUE(db->HasMappingFor(favicon_id2));
  105. // Fast forward enough such that second is removed.
  106. task_environment_.FastForwardBy(kTimeDeltaWhenEntriesAreRemoved +
  107. base::Days(1));
  108. EXPECT_FALSE(db->GetFaviconHeader(favicon_id2, nullptr, nullptr));
  109. EXPECT_FALSE(db->HasMappingFor(favicon_id2));
  110. }
  111. } // namespace weblayer