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