Enable rotate style visibility animation for new style web contents modal dialog
[chromium-blink-merge.git] / net / disk_cache / disk_cache_test_base.cc
blob36de25d0dc7f73d920c6bfd4e01c1f914f11ac4c
1 // Copyright (c) 2012 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.
5 #include "net/disk_cache/disk_cache_test_base.h"
7 #include "base/file_util.h"
8 #include "base/path_service.h"
9 #include "base/run_loop.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12 #include "net/base/test_completion_callback.h"
13 #include "net/disk_cache/backend_impl.h"
14 #include "net/disk_cache/cache_util.h"
15 #include "net/disk_cache/disk_cache.h"
16 #include "net/disk_cache/disk_cache_test_util.h"
17 #include "net/disk_cache/mem_backend_impl.h"
18 #include "net/disk_cache/simple/simple_backend_impl.h"
20 DiskCacheTest::DiskCacheTest() {
21 CHECK(temp_dir_.CreateUniqueTempDir());
22 cache_path_ = temp_dir_.path();
23 if (!MessageLoop::current())
24 message_loop_.reset(new MessageLoopForIO());
27 DiskCacheTest::~DiskCacheTest() {
30 bool DiskCacheTest::CopyTestCache(const std::string& name) {
31 base::FilePath path;
32 PathService::Get(base::DIR_SOURCE_ROOT, &path);
33 path = path.AppendASCII("net");
34 path = path.AppendASCII("data");
35 path = path.AppendASCII("cache_tests");
36 path = path.AppendASCII(name);
38 if (!CleanupCacheDir())
39 return false;
40 return file_util::CopyDirectory(path, cache_path_, false);
43 bool DiskCacheTest::CleanupCacheDir() {
44 return DeleteCache(cache_path_);
47 void DiskCacheTest::TearDown() {
48 base::RunLoop().RunUntilIdle();
51 DiskCacheTestWithCache::DiskCacheTestWithCache()
52 : cache_(NULL),
53 cache_impl_(NULL),
54 mem_cache_(NULL),
55 mask_(0),
56 size_(0),
57 type_(net::DISK_CACHE),
58 memory_only_(false),
59 simple_cache_mode_(false),
60 force_creation_(false),
61 new_eviction_(false),
62 first_cleanup_(true),
63 integrity_(true),
64 use_current_thread_(false),
65 cache_thread_("CacheThread") {
68 DiskCacheTestWithCache::~DiskCacheTestWithCache() {}
70 void DiskCacheTestWithCache::InitCache() {
71 if (memory_only_)
72 InitMemoryCache();
73 else
74 InitDiskCache();
76 ASSERT_TRUE(NULL != cache_);
77 // TODO(pasko): enable Entry Count for the Simple Cache.
78 if (first_cleanup_ && !simple_cache_mode_)
79 ASSERT_EQ(0, cache_->GetEntryCount());
82 // We are expected to leak memory when simulating crashes.
83 void DiskCacheTestWithCache::SimulateCrash() {
84 ASSERT_TRUE(!memory_only_);
85 net::TestCompletionCallback cb;
86 int rv = cache_impl_->FlushQueueForTest(cb.callback());
87 ASSERT_EQ(net::OK, cb.GetResult(rv));
88 cache_impl_->ClearRefCountForTest();
90 delete cache_impl_;
91 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
93 CreateBackend(disk_cache::kNoRandom, &cache_thread_);
96 void DiskCacheTestWithCache::SetTestMode() {
97 ASSERT_TRUE(!memory_only_);
98 cache_impl_->SetUnitTestMode();
101 void DiskCacheTestWithCache::SetMaxSize(int size) {
102 size_ = size;
103 if (cache_impl_)
104 EXPECT_TRUE(cache_impl_->SetMaxSize(size));
106 if (mem_cache_)
107 EXPECT_TRUE(mem_cache_->SetMaxSize(size));
110 int DiskCacheTestWithCache::OpenEntry(const std::string& key,
111 disk_cache::Entry** entry) {
112 net::TestCompletionCallback cb;
113 int rv = cache_->OpenEntry(key, entry, cb.callback());
114 return cb.GetResult(rv);
117 int DiskCacheTestWithCache::CreateEntry(const std::string& key,
118 disk_cache::Entry** entry) {
119 net::TestCompletionCallback cb;
120 int rv = cache_->CreateEntry(key, entry, cb.callback());
121 return cb.GetResult(rv);
124 int DiskCacheTestWithCache::DoomEntry(const std::string& key) {
125 net::TestCompletionCallback cb;
126 int rv = cache_->DoomEntry(key, cb.callback());
127 return cb.GetResult(rv);
130 int DiskCacheTestWithCache::DoomAllEntries() {
131 net::TestCompletionCallback cb;
132 int rv = cache_->DoomAllEntries(cb.callback());
133 return cb.GetResult(rv);
136 int DiskCacheTestWithCache::DoomEntriesBetween(const base::Time initial_time,
137 const base::Time end_time) {
138 net::TestCompletionCallback cb;
139 int rv = cache_->DoomEntriesBetween(initial_time, end_time, cb.callback());
140 return cb.GetResult(rv);
143 int DiskCacheTestWithCache::DoomEntriesSince(const base::Time initial_time) {
144 net::TestCompletionCallback cb;
145 int rv = cache_->DoomEntriesSince(initial_time, cb.callback());
146 return cb.GetResult(rv);
149 int DiskCacheTestWithCache::OpenNextEntry(void** iter,
150 disk_cache::Entry** next_entry) {
151 net::TestCompletionCallback cb;
152 int rv = cache_->OpenNextEntry(iter, next_entry, cb.callback());
153 return cb.GetResult(rv);
156 void DiskCacheTestWithCache::FlushQueueForTest() {
157 if (memory_only_ || !cache_impl_)
158 return;
160 net::TestCompletionCallback cb;
161 int rv = cache_impl_->FlushQueueForTest(cb.callback());
162 EXPECT_EQ(net::OK, cb.GetResult(rv));
165 void DiskCacheTestWithCache::RunTaskForTest(const base::Closure& closure) {
166 if (memory_only_ || !cache_impl_) {
167 closure.Run();
168 return;
171 net::TestCompletionCallback cb;
172 int rv = cache_impl_->RunTaskForTest(closure, cb.callback());
173 EXPECT_EQ(net::OK, cb.GetResult(rv));
176 int DiskCacheTestWithCache::ReadData(disk_cache::Entry* entry, int index,
177 int offset, net::IOBuffer* buf, int len) {
178 net::TestCompletionCallback cb;
179 int rv = entry->ReadData(index, offset, buf, len, cb.callback());
180 return cb.GetResult(rv);
183 int DiskCacheTestWithCache::WriteData(disk_cache::Entry* entry, int index,
184 int offset, net::IOBuffer* buf, int len,
185 bool truncate) {
186 net::TestCompletionCallback cb;
187 int rv = entry->WriteData(index, offset, buf, len, cb.callback(), truncate);
188 return cb.GetResult(rv);
191 int DiskCacheTestWithCache::ReadSparseData(disk_cache::Entry* entry,
192 int64 offset, net::IOBuffer* buf,
193 int len) {
194 net::TestCompletionCallback cb;
195 int rv = entry->ReadSparseData(offset, buf, len, cb.callback());
196 return cb.GetResult(rv);
199 int DiskCacheTestWithCache::WriteSparseData(disk_cache::Entry* entry,
200 int64 offset,
201 net::IOBuffer* buf, int len) {
202 net::TestCompletionCallback cb;
203 int rv = entry->WriteSparseData(offset, buf, len, cb.callback());
204 return cb.GetResult(rv);
207 void DiskCacheTestWithCache::TrimForTest(bool empty) {
208 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimForTest,
209 base::Unretained(cache_impl_),
210 empty));
213 void DiskCacheTestWithCache::TrimDeletedListForTest(bool empty) {
214 RunTaskForTest(base::Bind(&disk_cache::BackendImpl::TrimDeletedListForTest,
215 base::Unretained(cache_impl_),
216 empty));
219 void DiskCacheTestWithCache::AddDelay() {
220 base::Time initial = base::Time::Now();
221 while (base::Time::Now() <= initial) {
222 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1));
226 void DiskCacheTestWithCache::TearDown() {
227 base::RunLoop().RunUntilIdle();
228 delete cache_;
229 if (cache_thread_.IsRunning())
230 cache_thread_.Stop();
232 if (!memory_only_ && !simple_cache_mode_ && integrity_) {
233 EXPECT_TRUE(CheckCacheIntegrity(cache_path_, new_eviction_, mask_));
236 PlatformTest::TearDown();
239 void DiskCacheTestWithCache::InitMemoryCache() {
240 mem_cache_ = new disk_cache::MemBackendImpl(NULL);
241 cache_ = mem_cache_;
242 ASSERT_TRUE(NULL != cache_);
244 if (size_)
245 EXPECT_TRUE(mem_cache_->SetMaxSize(size_));
247 ASSERT_TRUE(mem_cache_->Init());
250 void DiskCacheTestWithCache::InitDiskCache() {
251 if (first_cleanup_)
252 ASSERT_TRUE(CleanupCacheDir());
254 if (!cache_thread_.IsRunning()) {
255 ASSERT_TRUE(cache_thread_.StartWithOptions(
256 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
258 ASSERT_TRUE(cache_thread_.message_loop() != NULL);
260 CreateBackend(disk_cache::kNoRandom, &cache_thread_);
263 void DiskCacheTestWithCache::CreateBackend(uint32 flags, base::Thread* thread) {
264 base::MessageLoopProxy* runner;
265 if (use_current_thread_)
266 runner = base::MessageLoopProxy::current();
267 else
268 runner = thread->message_loop_proxy();
270 if (simple_cache_mode_) {
271 net::TestCompletionCallback cb;
272 disk_cache::SimpleBackendImpl* simple_backend =
273 new disk_cache::SimpleBackendImpl(cache_path_, size_, type_,
274 make_scoped_refptr(runner), NULL);
275 int rv = simple_backend->Init(cb.callback());
276 ASSERT_EQ(net::OK, cb.GetResult(rv));
277 cache_ = simple_backend;
278 return;
281 if (mask_)
282 cache_impl_ = new disk_cache::BackendImpl(cache_path_, mask_, runner, NULL);
283 else
284 cache_impl_ = new disk_cache::BackendImpl(cache_path_, runner, NULL);
285 cache_ = cache_impl_;
286 ASSERT_TRUE(NULL != cache_);
287 if (size_)
288 EXPECT_TRUE(cache_impl_->SetMaxSize(size_));
289 if (new_eviction_)
290 cache_impl_->SetNewEviction();
291 cache_impl_->SetType(type_);
292 cache_impl_->SetFlags(flags);
293 net::TestCompletionCallback cb;
294 int rv = cache_impl_->Init(cb.callback());
295 ASSERT_EQ(net::OK, cb.GetResult(rv));
296 cache_ = cache_impl_;