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