Chromium Code Reviews

Side by Side Diff: content/browser/cache_storage/cache_storage_index_unittest.cc

Issue 2416713002: Write out CacheStorageCache size to index file. (Closed)
Patch Set: Only updating cache sizes for non-doomed caches Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
(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 {}
jkarlin 2016/12/14 14:19:56 DISALLOW_COPY_AND_ASSIGN(CacheStorageIndexTest)
cmumford 2016/12/15 22:29:15 Done.
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 EXPECT_EQ(2000, index.GetCacheSize("baz"));
81 EXPECT_EQ(CacheStorage::kSizeUnknown, index.GetCacheSize("<not-present>"));
82 }
83
84 TEST_F(CacheStorageIndexTest, TestDelete) {
85 CacheStorageIndex index;
86 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19));
87 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12));
88 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000));
89 EXPECT_EQ(3u, index.num_entries());
90 ASSERT_EQ(3u, index.ordered_cache_metadata().size());
91 EXPECT_EQ(1031, index.GetStorageSize());
92
93 auto it = index.ordered_cache_metadata().begin();
94 EXPECT_EQ("bar", it->name);
95 EXPECT_EQ(19u, it->size);
96 it++;
97 EXPECT_EQ("foo", it->name);
98 EXPECT_EQ(12u, it->size);
99 it++;
100 EXPECT_EQ("baz", it->name);
101 EXPECT_EQ(1000u, it->size);
102
103 index.Delete("bar");
104 EXPECT_EQ(2u, index.num_entries());
105 ASSERT_EQ(2u, index.ordered_cache_metadata().size());
106 EXPECT_EQ(1012, index.GetStorageSize());
107
108 it = index.ordered_cache_metadata().begin();
109 EXPECT_EQ("foo", it->name);
110 EXPECT_EQ(12u, it->size);
111 it++;
112 EXPECT_EQ("baz", it->name);
113 EXPECT_EQ(1000u, it->size);
114
115 index.Delete("baz");
116 EXPECT_EQ(1u, index.num_entries());
117 ASSERT_EQ(1u, index.ordered_cache_metadata().size());
118 EXPECT_EQ(12, index.GetStorageSize());
119
120 it = index.ordered_cache_metadata().begin();
121 EXPECT_EQ("foo", it->name);
122 EXPECT_EQ(12u, it->size);
123 }
124
125 TEST_F(CacheStorageIndexTest, TestInsert) {
126 CacheStorageIndex index;
127 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12));
128 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19));
129 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000));
130 EXPECT_EQ(3u, index.num_entries());
131 ASSERT_EQ(3u, index.ordered_cache_metadata().size());
132 EXPECT_EQ(1031, index.GetStorageSize());
133 }
134
135 TEST_F(CacheStorageIndexTest, TestMoveOperator) {
136 CacheStorageIndex index;
137 index.Insert(CacheStorageIndex::CacheMetadata("foo", 12));
138 index.Insert(CacheStorageIndex::CacheMetadata("bar", 19));
139 index.Insert(CacheStorageIndex::CacheMetadata("baz", 1000));
140
141 CacheStorageIndex index2;
142 index2 = std::move(index);
143
144 EXPECT_EQ(3u, index2.num_entries());
145 EXPECT_EQ(3u, index2.ordered_cache_metadata().size());
146 ASSERT_EQ(1031, index2.GetStorageSize());
147
148 EXPECT_EQ(0u, index.num_entries());
149 EXPECT_TRUE(index.ordered_cache_metadata().empty());
150 EXPECT_EQ(0u, index.GetStorageSize());
151
152 auto it = index2.ordered_cache_metadata().begin();
153 EXPECT_EQ("foo", it->name);
154 EXPECT_EQ(12u, it->size);
155 it++;
156 EXPECT_EQ("bar", it->name);
157 EXPECT_EQ(19u, it->size);
158 it++;
159 EXPECT_EQ("baz", it->name);
160 EXPECT_EQ(1000u, it->size);
161
162 EXPECT_EQ(3u, index2.num_entries());
163 ASSERT_EQ(3u, index2.ordered_cache_metadata().size());
164 EXPECT_EQ(1031, index2.GetStorageSize());
165 }
166
167 } // namespace content
OLDNEW

Powered by Google App Engine