Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / appcache / appcache_disk_cache_unittest.cc
1 // Copyright 2014 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 "base/bind.h"
6 #include "base/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/message_loop/message_loop_proxy.h"
9 #include "base/run_loop.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "webkit/browser/appcache/appcache_disk_cache.h"
14
15 using appcache::AppCacheDiskCache;
16
17 namespace content {
18
19 class AppCacheDiskCacheTest : public testing::Test {
20  public:
21   AppCacheDiskCacheTest() {}
22
23   virtual void SetUp() OVERRIDE {
24     // Use the current thread for the DiskCache's cache_thread.
25     message_loop_.reset(new base::MessageLoopForIO());
26     cache_thread_ = base::MessageLoopProxy::current();
27     ASSERT_TRUE(directory_.CreateUniqueTempDir());
28     completion_callback_ = base::Bind(
29         &AppCacheDiskCacheTest::OnComplete,
30         base::Unretained(this));
31   }
32
33   virtual void TearDown() OVERRIDE {
34     base::RunLoop().RunUntilIdle();
35     message_loop_.reset(NULL);
36   }
37
38   void FlushCacheTasks() {
39     base::RunLoop().RunUntilIdle();
40   }
41
42   void OnComplete(int err) {
43     completion_results_.push_back(err);
44   }
45
46   base::ScopedTempDir directory_;
47   scoped_ptr<base::MessageLoop> message_loop_;
48   scoped_refptr<base::MessageLoopProxy> cache_thread_;
49   net::CompletionCallback completion_callback_;
50   std::vector<int> completion_results_;
51
52   static const int k10MBytes = 10 * 1024 * 1024;
53 };
54
55 TEST_F(AppCacheDiskCacheTest, DisablePriorToInitCompletion) {
56   AppCacheDiskCache::Entry* entry = NULL;
57
58   // Create an instance and start it initializing, queue up
59   // one of each kind of "entry" function.
60   scoped_ptr<AppCacheDiskCache> disk_cache(new AppCacheDiskCache);
61   EXPECT_FALSE(disk_cache->is_disabled());
62   disk_cache->InitWithDiskBackend(
63       directory_.path(), k10MBytes, false, cache_thread_,
64       completion_callback_);
65   disk_cache->CreateEntry(1, &entry, completion_callback_);
66   disk_cache->OpenEntry(2, &entry, completion_callback_);
67   disk_cache->DoomEntry(3, completion_callback_);
68
69   // Pull the plug on all that.
70   EXPECT_FALSE(disk_cache->is_disabled());
71   disk_cache->Disable();
72   EXPECT_TRUE(disk_cache->is_disabled());
73
74   FlushCacheTasks();
75
76   EXPECT_EQ(NULL, entry);
77   EXPECT_EQ(4u, completion_results_.size());
78   for (std::vector<int>::const_iterator iter = completion_results_.begin();
79        iter < completion_results_.end(); ++iter) {
80     EXPECT_EQ(net::ERR_ABORTED, *iter);
81   }
82
83   // Ensure the directory can be deleted at this point.
84   EXPECT_TRUE(base::DirectoryExists(directory_.path()));
85   EXPECT_FALSE(base::IsDirectoryEmpty(directory_.path()));
86   EXPECT_TRUE(base::DeleteFile(directory_.path(), true));
87   EXPECT_FALSE(base::DirectoryExists(directory_.path()));
88 }
89
90 TEST_F(AppCacheDiskCacheTest, DisableAfterInitted) {
91   // Create an instance and let it fully init.
92   scoped_ptr<AppCacheDiskCache> disk_cache(new AppCacheDiskCache);
93   EXPECT_FALSE(disk_cache->is_disabled());
94   disk_cache->InitWithDiskBackend(
95       directory_.path(), k10MBytes, false, cache_thread_,
96       completion_callback_);
97   FlushCacheTasks();
98   EXPECT_EQ(1u, completion_results_.size());
99   EXPECT_EQ(net::OK, completion_results_[0]);
100
101   // Pull the plug
102   disk_cache->Disable();
103   FlushCacheTasks();
104
105   // Ensure the directory can be deleted at this point.
106   EXPECT_TRUE(base::DirectoryExists(directory_.path()));
107   EXPECT_FALSE(base::IsDirectoryEmpty(directory_.path()));
108   EXPECT_TRUE(base::DeleteFile(directory_.path(), true));
109   EXPECT_FALSE(base::DirectoryExists(directory_.path()));
110
111   // Methods should return immediately when disabled and not invoke
112   // the callback at all.
113   AppCacheDiskCache::Entry* entry = NULL;
114   completion_results_.clear();
115   EXPECT_EQ(net::ERR_ABORTED,
116             disk_cache->CreateEntry(1, &entry, completion_callback_));
117   EXPECT_EQ(net::ERR_ABORTED,
118             disk_cache->OpenEntry(2, &entry, completion_callback_));
119   EXPECT_EQ(net::ERR_ABORTED,
120             disk_cache->DoomEntry(3, completion_callback_));
121   FlushCacheTasks();
122   EXPECT_TRUE(completion_results_.empty());
123 }
124
125 // Flaky on Android: http://crbug.com/339534
126 TEST_F(AppCacheDiskCacheTest, DISABLED_DisableWithEntriesOpen) {
127   // Create an instance and let it fully init.
128   scoped_ptr<AppCacheDiskCache> disk_cache(new AppCacheDiskCache);
129   EXPECT_FALSE(disk_cache->is_disabled());
130   disk_cache->InitWithDiskBackend(
131       directory_.path(), k10MBytes, false, cache_thread_,
132       completion_callback_);
133   FlushCacheTasks();
134   EXPECT_EQ(1u, completion_results_.size());
135   EXPECT_EQ(net::OK, completion_results_[0]);
136
137   // Note: We don't have detailed expectations of the DiskCache
138   // operations because on android it's really SimpleCache which
139   // does behave differently.
140   //
141   // What matters for the corruption handling and service reinitiazation
142   // is that the directory can be deleted after the calling Disable() method,
143   // and we do have expectations about that.
144
145   // Create/open some entries.
146   AppCacheDiskCache::Entry* entry1 = NULL;
147   AppCacheDiskCache::Entry* entry2 = NULL;
148   disk_cache->CreateEntry(1, &entry1, completion_callback_);
149   disk_cache->CreateEntry(2, &entry2, completion_callback_);
150   FlushCacheTasks();
151   EXPECT_TRUE(entry1);
152   EXPECT_TRUE(entry2);
153
154   // Write something to one of the entries and flush it.
155   const char* kData = "Hello";
156   const int kDataLen = strlen(kData) + 1;
157   scoped_refptr<net::IOBuffer> write_buf(new net::WrappedIOBuffer(kData));
158   entry1->Write(0, 0, write_buf, kDataLen, completion_callback_);
159   FlushCacheTasks();
160
161   // Queue up a read and a write.
162   scoped_refptr<net::IOBuffer> read_buf = new net::IOBuffer(kDataLen);
163   entry1->Read(0, 0, read_buf.get(), kDataLen, completion_callback_);
164   entry2->Write(0, 0, write_buf.get(), kDataLen, completion_callback_);
165
166   // Pull the plug
167   disk_cache->Disable();
168   FlushCacheTasks();
169
170   // Ensure the directory can be deleted at this point.
171   EXPECT_TRUE(base::DirectoryExists(directory_.path()));
172   EXPECT_FALSE(base::IsDirectoryEmpty(directory_.path()));
173   EXPECT_TRUE(base::DeleteFile(directory_.path(), true));
174   EXPECT_FALSE(base::DirectoryExists(directory_.path()));
175
176   disk_cache.reset(NULL);
177
178   // Also, new IO operations should fail immediately.
179   EXPECT_EQ(
180       net::ERR_ABORTED,
181       entry1->Read(0, 0, read_buf.get(), kDataLen, completion_callback_));
182   entry1->Close();
183   entry2->Close();
184
185   FlushCacheTasks();
186 }
187
188 }  // namespace content