OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 5 #include "content/browser/cache_storage/cache_storage_index.h" |
| 6 |
| 7 #include <list> |
| 8 #include <utility> |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 |
| 11 namespace content { |
| 12 |
| 13 namespace { |
| 14 |
| 15 void ClearIndex(CacheStorageIndex* index) { |
| 16 std::list<std::string> names; |
| 17 for (const auto& metadata : index->ordered_cache_metadata()) |
| 18 names.push_back(metadata.name); |
| 19 for (const std::string& name : names) |
| 20 index->Delete(name); |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
| 25 class CacheStorageIndexTest : public testing::Test { |
| 26 public: |
| 27 void SetUp() override {} |
| 28 void TearDown() override {} |
| 29 }; |
| 30 |
| 31 TEST_F(CacheStorageIndexTest, TestDefaultConstructor) { |
| 32 CacheStorageIndex index; |
| 33 EXPECT_EQ(0u, index.num_entries()); |
| 34 EXPECT_TRUE(index.ordered_cache_metadata().empty()); |
| 35 EXPECT_EQ(0u, index.GetStorageSize()); |
| 36 } |
| 37 |
| 38 TEST_F(CacheStorageIndexTest, TestCopyConstructor) { |
| 39 CacheStorageIndex index; |
| 40 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12)); |
| 41 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19)); |
| 42 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000)); |
| 43 EXPECT_EQ(3u, index.num_entries()); |
| 44 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); |
| 45 EXPECT_EQ(1031, index.GetStorageSize()); |
| 46 |
| 47 CacheStorageIndex index2(index); |
| 48 ClearIndex(&index); |
| 49 |
| 50 auto it = index2.ordered_cache_metadata().begin(); |
| 51 EXPECT_EQ("foo", it->name); |
| 52 EXPECT_EQ(12u, it->size); |
| 53 it++; |
| 54 EXPECT_EQ("bar", it->name); |
| 55 EXPECT_EQ(19u, it->size); |
| 56 it++; |
| 57 EXPECT_EQ("baz", it->name); |
| 58 EXPECT_EQ(1000u, it->size); |
| 59 |
| 60 EXPECT_EQ(3u, index2.num_entries()); |
| 61 ASSERT_EQ(3u, index2.ordered_cache_metadata().size()); |
| 62 EXPECT_EQ(1031, index2.GetStorageSize()); |
| 63 } |
| 64 |
| 65 TEST_F(CacheStorageIndexTest, TestSetCacheSize) { |
| 66 CacheStorageIndex index; |
| 67 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12)); |
| 68 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19)); |
| 69 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000)); |
| 70 EXPECT_EQ(3u, index.num_entries()); |
| 71 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); |
| 72 EXPECT_EQ(1031, index.GetStorageSize()); |
| 73 |
| 74 EXPECT_TRUE(index.SetCacheSize("baz", 2000)); |
| 75 EXPECT_EQ(2031, index.GetStorageSize()); |
| 76 |
| 77 EXPECT_FALSE(index.SetCacheSize("baz", 2000)); |
| 78 EXPECT_EQ(2031, index.GetStorageSize()); |
| 79 |
| 80 index.SetCacheSizeModified("baz", 500); |
| 81 EXPECT_EQ(2531, index.GetStorageSize()); |
| 82 EXPECT_EQ(2500, index.GetCacheSize("baz")); |
| 83 EXPECT_EQ(CacheStorage::kSizeUnknown, index.GetCacheSize("<not-present>")); |
| 84 } |
| 85 |
| 86 TEST_F(CacheStorageIndexTest, TestDelete) { |
| 87 CacheStorageIndex index; |
| 88 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19)); |
| 89 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12)); |
| 90 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000)); |
| 91 EXPECT_EQ(3u, index.num_entries()); |
| 92 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); |
| 93 EXPECT_EQ(1031, index.GetStorageSize()); |
| 94 |
| 95 auto it = index.ordered_cache_metadata().begin(); |
| 96 EXPECT_EQ("bar", it->name); |
| 97 EXPECT_EQ(19u, it->size); |
| 98 it++; |
| 99 EXPECT_EQ("foo", it->name); |
| 100 EXPECT_EQ(12u, it->size); |
| 101 it++; |
| 102 EXPECT_EQ("baz", it->name); |
| 103 EXPECT_EQ(1000u, it->size); |
| 104 |
| 105 index.Delete("bar"); |
| 106 EXPECT_EQ(2u, index.num_entries()); |
| 107 ASSERT_EQ(2u, index.ordered_cache_metadata().size()); |
| 108 EXPECT_EQ(1012, index.GetStorageSize()); |
| 109 |
| 110 it = index.ordered_cache_metadata().begin(); |
| 111 EXPECT_EQ("foo", it->name); |
| 112 EXPECT_EQ(12u, it->size); |
| 113 it++; |
| 114 EXPECT_EQ("baz", it->name); |
| 115 EXPECT_EQ(1000u, it->size); |
| 116 |
| 117 index.Delete("baz"); |
| 118 EXPECT_EQ(1u, index.num_entries()); |
| 119 ASSERT_EQ(1u, index.ordered_cache_metadata().size()); |
| 120 EXPECT_EQ(12, index.GetStorageSize()); |
| 121 |
| 122 it = index.ordered_cache_metadata().begin(); |
| 123 EXPECT_EQ("foo", it->name); |
| 124 EXPECT_EQ(12u, it->size); |
| 125 } |
| 126 |
| 127 TEST_F(CacheStorageIndexTest, TestInsert) { |
| 128 CacheStorageIndex index; |
| 129 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12)); |
| 130 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19)); |
| 131 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000)); |
| 132 EXPECT_EQ(3u, index.num_entries()); |
| 133 ASSERT_EQ(3u, index.ordered_cache_metadata().size()); |
| 134 EXPECT_EQ(1031, index.GetStorageSize()); |
| 135 } |
| 136 |
| 137 TEST_F(CacheStorageIndexTest, TestMoveOperator) { |
| 138 CacheStorageIndex index; |
| 139 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12)); |
| 140 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19)); |
| 141 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000)); |
| 142 |
| 143 CacheStorageIndex index2; |
| 144 index2 = std::move(index); |
| 145 |
| 146 EXPECT_EQ(3u, index2.num_entries()); |
| 147 EXPECT_EQ(3u, index2.ordered_cache_metadata().size()); |
| 148 ASSERT_EQ(1031, index2.GetStorageSize()); |
| 149 |
| 150 EXPECT_EQ(0u, index.num_entries()); |
| 151 EXPECT_TRUE(index.ordered_cache_metadata().empty()); |
| 152 EXPECT_EQ(0u, index.GetStorageSize()); |
| 153 |
| 154 auto it = index2.ordered_cache_metadata().begin(); |
| 155 EXPECT_EQ("foo", it->name); |
| 156 EXPECT_EQ(12u, it->size); |
| 157 it++; |
| 158 EXPECT_EQ("bar", it->name); |
| 159 EXPECT_EQ(19u, it->size); |
| 160 it++; |
| 161 EXPECT_EQ("baz", it->name); |
| 162 EXPECT_EQ(1000u, it->size); |
| 163 |
| 164 EXPECT_EQ(3u, index2.num_entries()); |
| 165 ASSERT_EQ(3u, index2.ordered_cache_metadata().size()); |
| 166 EXPECT_EQ(1031, index2.GetStorageSize()); |
| 167 } |
| 168 |
| 169 } // namespace content |
OLD | NEW |