OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/files/scoped_temp_dir.h" | 5 #include "base/files/scoped_temp_dir.h" |
6 #include "base/hash.h" | 6 #include "base/hash.h" |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/pickle.h" | 9 #include "base/pickle.h" |
10 #include "base/sha1.h" | 10 #include "base/sha1.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 EntryMetadata NewEntryMetadataWithValues() { | 33 EntryMetadata NewEntryMetadataWithValues() { |
34 return EntryMetadata(kTestLastUsedTime, kTestEntrySize); | 34 return EntryMetadata(kTestLastUsedTime, kTestEntrySize); |
35 } | 35 } |
36 | 36 |
37 void CheckEntryMetadataValues(const EntryMetadata& entry_metadata) { | 37 void CheckEntryMetadataValues(const EntryMetadata& entry_metadata) { |
38 EXPECT_EQ(kTestLastUsedTime, entry_metadata.GetLastUsedTime()); | 38 EXPECT_EQ(kTestLastUsedTime, entry_metadata.GetLastUsedTime()); |
39 EXPECT_EQ(kTestEntrySize, entry_metadata.GetEntrySize()); | 39 EXPECT_EQ(kTestEntrySize, entry_metadata.GetEntrySize()); |
40 } | 40 } |
41 }; | 41 }; |
42 | 42 |
43 class TestSimpleIndexFile : public SimpleIndexFile, | 43 class MockSimpleIndexFile : public SimpleIndexFile, |
44 public base::SupportsWeakPtr<TestSimpleIndexFile> { | 44 public base::SupportsWeakPtr<MockSimpleIndexFile> { |
45 public: | 45 public: |
46 TestSimpleIndexFile() | 46 MockSimpleIndexFile() |
47 : SimpleIndexFile(NULL, NULL, base::FilePath()), | 47 : SimpleIndexFile(NULL, NULL, base::FilePath()), |
48 get_index_entries_calls_(0), | 48 get_index_entries_calls_(0), |
49 doom_entry_set_calls_(0), | 49 doom_entry_set_calls_(0), |
50 last_response_thread_(NULL), | 50 last_response_thread_(NULL), |
51 disk_writes_(0) {} | 51 disk_writes_(0) {} |
52 | 52 |
53 virtual void LoadIndexEntries( | 53 virtual void LoadIndexEntries( |
54 scoped_refptr<base::SingleThreadTaskRunner> response_thread, | 54 scoped_refptr<base::SingleThreadTaskRunner> response_thread, |
55 const SimpleIndexFile::IndexCompletionCallback& | 55 const SimpleIndexFile::IndexCompletionCallback& |
56 completion_callback) OVERRIDE { | 56 completion_callback) OVERRIDE { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 int doom_entry_set_calls_; | 96 int doom_entry_set_calls_; |
97 base::SingleThreadTaskRunner* last_response_thread_; | 97 base::SingleThreadTaskRunner* last_response_thread_; |
98 base::Callback<void(int)> last_doom_reply_callback_; | 98 base::Callback<void(int)> last_doom_reply_callback_; |
99 int disk_writes_; | 99 int disk_writes_; |
100 SimpleIndex::EntrySet disk_write_entry_set_; | 100 SimpleIndex::EntrySet disk_write_entry_set_; |
101 }; | 101 }; |
102 | 102 |
103 class SimpleIndexTest : public testing::Test { | 103 class SimpleIndexTest : public testing::Test { |
104 public: | 104 public: |
105 virtual void SetUp() OVERRIDE { | 105 virtual void SetUp() OVERRIDE { |
106 scoped_ptr<TestSimpleIndexFile> index_file(new TestSimpleIndexFile()); | 106 scoped_ptr<MockSimpleIndexFile> index_file(new MockSimpleIndexFile()); |
107 index_file_ = index_file->AsWeakPtr(); | 107 index_file_ = index_file->AsWeakPtr(); |
108 index_.reset(new SimpleIndex(NULL, base::FilePath(), | 108 index_.reset(new SimpleIndex(NULL, base::FilePath(), |
109 index_file.PassAs<SimpleIndexFile>())); | 109 index_file.PassAs<SimpleIndexFile>())); |
110 | 110 |
111 index_->Initialize(); | 111 index_->Initialize(); |
112 } | 112 } |
113 | 113 |
114 void WaitForTimeChange() { | 114 void WaitForTimeChange() { |
115 base::Time now(base::Time::Now()); | 115 base::Time now(base::Time::Now()); |
116 | 116 |
(...skipping 21 matching lines...) Expand all Loading... |
138 } | 138 } |
139 | 139 |
140 void ReturnIndexFile() { | 140 void ReturnIndexFile() { |
141 scoped_ptr<SimpleIndex::EntrySet> map(new SimpleIndex::EntrySet); | 141 scoped_ptr<SimpleIndex::EntrySet> map(new SimpleIndex::EntrySet); |
142 map->swap(index_file_return_map_); | 142 map->swap(index_file_return_map_); |
143 index_file_->completion_callback().Run(map.Pass(), false); | 143 index_file_->completion_callback().Run(map.Pass(), false); |
144 } | 144 } |
145 | 145 |
146 // Non-const for timer manipulation. | 146 // Non-const for timer manipulation. |
147 SimpleIndex* index() { return index_.get(); } | 147 SimpleIndex* index() { return index_.get(); } |
148 const TestSimpleIndexFile* index_file() const { return index_file_.get(); } | 148 const MockSimpleIndexFile* index_file() const { return index_file_.get(); } |
149 | 149 |
150 protected: | 150 protected: |
151 SimpleIndex::EntrySet index_file_return_map_; | 151 SimpleIndex::EntrySet index_file_return_map_; |
152 scoped_ptr<SimpleIndex> index_; | 152 scoped_ptr<SimpleIndex> index_; |
153 base::WeakPtr<TestSimpleIndexFile> index_file_; | 153 base::WeakPtr<MockSimpleIndexFile> index_file_; |
154 }; | 154 }; |
155 | 155 |
156 TEST_F(EntryMetadataTest, Basics) { | 156 TEST_F(EntryMetadataTest, Basics) { |
157 EntryMetadata entry_metadata; | 157 EntryMetadata entry_metadata; |
158 EXPECT_EQ(base::Time::FromInternalValue(0), entry_metadata.GetLastUsedTime()); | 158 EXPECT_EQ(base::Time::FromInternalValue(0), entry_metadata.GetLastUsedTime()); |
159 EXPECT_EQ(size_t(0), entry_metadata.GetEntrySize()); | 159 EXPECT_EQ(size_t(0), entry_metadata.GetEntrySize()); |
160 | 160 |
161 entry_metadata = NewEntryMetadataWithValues(); | 161 entry_metadata = NewEntryMetadataWithValues(); |
162 CheckEntryMetadataValues(entry_metadata); | 162 CheckEntryMetadataValues(entry_metadata); |
163 | 163 |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
567 WaitForTimeChange(); | 567 WaitForTimeChange(); |
568 EXPECT_EQ(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); | 568 EXPECT_EQ(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); |
569 index()->Insert("key2"); | 569 index()->Insert("key2"); |
570 index()->UpdateEntrySize("key2", 40); | 570 index()->UpdateEntrySize("key2", 40); |
571 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); | 571 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); |
572 EXPECT_LT(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); | 572 EXPECT_LT(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); |
573 index()->write_to_disk_timer_.Stop(); | 573 index()->write_to_disk_timer_.Stop(); |
574 } | 574 } |
575 | 575 |
576 } // namespace disk_cache | 576 } // namespace disk_cache |
OLD | NEW |