Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2057)

Side by Side Diff: webkit/fileapi/file_system_quota_unittest.cc

Issue 9372044: Refactor FileSystemOperation to take callback for each method. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Reflected kinuko's comments + Fixture for failing-Write -> Cancel pattern. Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 // This test checks the entire behavior of FileSystem usage and quota, such as: 5 // This test checks the entire behavior of FileSystem usage and quota, such as:
6 // 1) the actual size of files on disk, 6 // 1) the actual size of files on disk,
7 // 2) the described size in .usage, and 7 // 2) the described size in .usage, and
8 // 3) the result of QuotaManager::GetUsageAndQuota. 8 // 3) the result of QuotaManager::GetUsageAndQuota.
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/file_util.h" 11 #include "base/file_util.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
14 #include "base/message_loop.h" 15 #include "base/message_loop.h"
15 #include "base/platform_file.h" 16 #include "base/platform_file.h"
16 #include "base/scoped_temp_dir.h" 17 #include "base/scoped_temp_dir.h"
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
18 #include "webkit/fileapi/file_system_callback_dispatcher.h"
19 #include "webkit/fileapi/file_system_operation.h" 19 #include "webkit/fileapi/file_system_operation.h"
20 #include "webkit/fileapi/file_system_test_helper.h" 20 #include "webkit/fileapi/file_system_test_helper.h"
21 #include "webkit/fileapi/file_system_usage_cache.h" 21 #include "webkit/fileapi/file_system_usage_cache.h"
22 #include "webkit/fileapi/file_system_util.h" 22 #include "webkit/fileapi/file_system_util.h"
23 #include "webkit/fileapi/local_file_util.h" 23 #include "webkit/fileapi/local_file_util.h"
24 #include "webkit/fileapi/quota_file_util.h" 24 #include "webkit/fileapi/quota_file_util.h"
25 #include "webkit/quota/quota_manager.h" 25 #include "webkit/quota/quota_manager.h"
26 26
27 namespace fileapi { 27 namespace fileapi {
28 28
29 const int kFileOperationStatusNotSet = 1; 29 const int kFileOperationStatusNotSet = 1;
30 30
31 class FileSystemQuotaTest : public testing::Test { 31 class FileSystemQuotaTest
32 : public testing::Test,
33 public base::SupportsWeakPtr<FileSystemQuotaTest> {
32 public: 34 public:
33 FileSystemQuotaTest() 35 FileSystemQuotaTest()
34 : local_file_util_(new LocalFileUtil(QuotaFileUtil::CreateDefault())), 36 : local_file_util_(new LocalFileUtil(QuotaFileUtil::CreateDefault())),
35 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), 37 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)),
36 status_(kFileOperationStatusNotSet), 38 status_(kFileOperationStatusNotSet),
37 quota_status_(quota::kQuotaStatusUnknown), 39 quota_status_(quota::kQuotaStatusUnknown),
38 usage_(-1), 40 usage_(-1),
39 quota_(-1) {} 41 quota_(-1) {}
40 42
41 FileSystemOperation* operation(); 43 FileSystemOperation* operation();
42 44
43 void set_status(int status) { status_ = status; }
44 int status() const { return status_; } 45 int status() const { return status_; }
45 quota::QuotaStatusCode quota_status() const { return quota_status_; } 46 quota::QuotaStatusCode quota_status() const { return quota_status_; }
46 int64 usage() { return usage_; } 47 int64 usage() { return usage_; }
47 int64 quota() { return quota_; } 48 int64 quota() { return quota_; }
48 49
49 virtual void SetUp(); 50 virtual void SetUp();
50 virtual void TearDown(); 51 virtual void TearDown();
51 52
52 void OnGetUsageAndQuota( 53 void OnGetUsageAndQuota(
53 quota::QuotaStatusCode status, int64 usage, int64 quota); 54 quota::QuotaStatusCode status, int64 usage, int64 quota);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 return CreateVirtualTemporaryDirInDir(FilePath()); 110 return CreateVirtualTemporaryDirInDir(FilePath());
110 } 111 }
111 112
112 FilePath child_dir_path_; 113 FilePath child_dir_path_;
113 FilePath child_file1_path_; 114 FilePath child_file1_path_;
114 FilePath child_file2_path_; 115 FilePath child_file2_path_;
115 FilePath grandchild_file1_path_; 116 FilePath grandchild_file1_path_;
116 FilePath grandchild_file2_path_; 117 FilePath grandchild_file2_path_;
117 118
118 protected: 119 protected:
120 // Callback for recording test results.
121 FileSystemOperationInterface::StatusCallback RecordStatusCallback() {
122 return base::Bind(&FileSystemQuotaTest::DidFinish, AsWeakPtr());
123 }
124
125 void DidFinish(base::PlatformFileError status) {
126 status_ = status;
127 }
128
119 FileSystemTestOriginHelper test_helper_; 129 FileSystemTestOriginHelper test_helper_;
120 130
121 ScopedTempDir work_dir_; 131 ScopedTempDir work_dir_;
122 scoped_refptr<quota::QuotaManager> quota_manager_; 132 scoped_refptr<quota::QuotaManager> quota_manager_;
123 scoped_ptr<LocalFileUtil> local_file_util_; 133 scoped_ptr<LocalFileUtil> local_file_util_;
124 134
125 base::WeakPtrFactory<FileSystemQuotaTest> weak_factory_; 135 base::WeakPtrFactory<FileSystemQuotaTest> weak_factory_;
126 136
127 // For post-operation status. 137 // For post-operation status.
128 int status_; 138 int status_;
(...skipping 10 matching lines...) Expand all
139 149
140 int64 RevokeUsageCache() { 150 int64 RevokeUsageCache() {
141 quota_manager_->ResetUsageTracker(test_helper_.storage_type()); 151 quota_manager_->ResetUsageTracker(test_helper_.storage_type());
142 return test_helper_.RevokeUsageCache(); 152 return test_helper_.RevokeUsageCache();
143 } 153 }
144 154
145 private: 155 private:
146 scoped_refptr<FileSystemContext> file_system_context_; 156 scoped_refptr<FileSystemContext> file_system_context_;
147 }; 157 };
148 158
149 namespace {
150
151 class MockDispatcher : public FileSystemCallbackDispatcher {
152 public:
153 explicit MockDispatcher(FileSystemQuotaTest* test) : test_(test) { }
154
155 virtual void DidFail(base::PlatformFileError status) {
156 test_->set_status(status);
157 }
158
159 virtual void DidSucceed() {
160 test_->set_status(base::PLATFORM_FILE_OK);
161 }
162
163 virtual void DidGetLocalPath(const FilePath& local_path) {
164 ADD_FAILURE();
165 }
166
167 virtual void DidReadMetadata(
168 const base::PlatformFileInfo& info,
169 const FilePath& platform_path) {
170 ADD_FAILURE();
171 }
172
173 virtual void DidReadDirectory(
174 const std::vector<base::FileUtilProxy::Entry>& entries,
175 bool /* has_more */) {
176 ADD_FAILURE();
177 }
178
179 virtual void DidOpenFileSystem(const std::string&, const GURL&) {
180 ADD_FAILURE();
181 }
182
183 virtual void DidWrite(int64 bytes, bool complete) {
184 ADD_FAILURE();
185 }
186
187 private:
188 FileSystemQuotaTest* test_;
189 };
190
191 } // namespace (anonymous)
192
193 void FileSystemQuotaTest::SetUp() { 159 void FileSystemQuotaTest::SetUp() {
194 ASSERT_TRUE(work_dir_.CreateUniqueTempDir()); 160 ASSERT_TRUE(work_dir_.CreateUniqueTempDir());
195 FilePath filesystem_dir_path = work_dir_.path().AppendASCII("filesystem"); 161 FilePath filesystem_dir_path = work_dir_.path().AppendASCII("filesystem");
196 file_util::CreateDirectory(filesystem_dir_path); 162 file_util::CreateDirectory(filesystem_dir_path);
197 163
198 quota_manager_ = new quota::QuotaManager( 164 quota_manager_ = new quota::QuotaManager(
199 false /* is_incognito */, 165 false /* is_incognito */,
200 filesystem_dir_path, 166 filesystem_dir_path,
201 base::MessageLoopProxy::current(), 167 base::MessageLoopProxy::current(),
202 base::MessageLoopProxy::current(), 168 base::MessageLoopProxy::current(),
203 NULL); 169 NULL);
204 170
205 test_helper_.SetUp(filesystem_dir_path, 171 test_helper_.SetUp(filesystem_dir_path,
206 false /* unlimited quota */, 172 false /* unlimited quota */,
207 quota_manager_->proxy(), 173 quota_manager_->proxy(),
208 local_file_util_.get()); 174 local_file_util_.get());
209 } 175 }
210 176
211 void FileSystemQuotaTest::TearDown() { 177 void FileSystemQuotaTest::TearDown() {
212 quota_manager_ = NULL; 178 quota_manager_ = NULL;
213 test_helper_.TearDown(); 179 test_helper_.TearDown();
214 } 180 }
215 181
216 FileSystemOperation* FileSystemQuotaTest::operation() { 182 FileSystemOperation* FileSystemQuotaTest::operation() {
217 return test_helper_.NewOperation(new MockDispatcher(this)); 183 return test_helper_.NewOperation();
218 } 184 }
219 185
220 void FileSystemQuotaTest::OnGetUsageAndQuota( 186 void FileSystemQuotaTest::OnGetUsageAndQuota(
221 quota::QuotaStatusCode status, int64 usage, int64 quota) { 187 quota::QuotaStatusCode status, int64 usage, int64 quota) {
222 quota_status_ = status; 188 quota_status_ = status;
223 usage_ = usage; 189 usage_ = usage;
224 quota_ = quota; 190 quota_ = quota;
225 } 191 }
226 192
227 void FileSystemQuotaTest::PrepareFileSet(const FilePath& virtual_path) { 193 void FileSystemQuotaTest::PrepareFileSet(const FilePath& virtual_path) {
228 child_dir_path_ = CreateVirtualTemporaryDirInDir(virtual_path); 194 child_dir_path_ = CreateVirtualTemporaryDirInDir(virtual_path);
229 child_file1_path_ = CreateVirtualTemporaryFileInDir(virtual_path); 195 child_file1_path_ = CreateVirtualTemporaryFileInDir(virtual_path);
230 child_file2_path_ = CreateVirtualTemporaryFileInDir(virtual_path); 196 child_file2_path_ = CreateVirtualTemporaryFileInDir(virtual_path);
231 grandchild_file1_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_); 197 grandchild_file1_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_);
232 grandchild_file2_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_); 198 grandchild_file2_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_);
233 } 199 }
234 200
235 TEST_F(FileSystemQuotaTest, TestMoveSuccessSrcDirRecursive) { 201 TEST_F(FileSystemQuotaTest, TestMoveSuccessSrcDirRecursive) {
236 FilePath src_dir_path(CreateVirtualTemporaryDir()); 202 FilePath src_dir_path(CreateVirtualTemporaryDir());
237 PrepareFileSet(src_dir_path); 203 PrepareFileSet(src_dir_path);
238 FilePath dest_dir_path(CreateVirtualTemporaryDir()); 204 FilePath dest_dir_path(CreateVirtualTemporaryDir());
239 205
240 EXPECT_EQ(0, ActualSize()); 206 EXPECT_EQ(0, ActualSize());
241 207
242 operation()->Truncate(URLForPath(child_file1_path_), 5000); 208 operation()->Truncate(URLForPath(child_file1_path_), 5000,
243 operation()->Truncate(URLForPath(child_file2_path_), 400); 209 RecordStatusCallback());
244 operation()->Truncate(URLForPath(grandchild_file1_path_), 30); 210 operation()->Truncate(URLForPath(child_file2_path_), 400,
245 operation()->Truncate(URLForPath(grandchild_file2_path_), 2); 211 RecordStatusCallback());
212 operation()->Truncate(URLForPath(grandchild_file1_path_), 30,
213 RecordStatusCallback());
214 operation()->Truncate(URLForPath(grandchild_file2_path_), 2,
215 RecordStatusCallback());
246 MessageLoop::current()->RunAllPending(); 216 MessageLoop::current()->RunAllPending();
247 217
248 const int64 all_file_size = 5000 + 400 + 30 + 2; 218 const int64 all_file_size = 5000 + 400 + 30 + 2;
249 219
250 EXPECT_EQ(all_file_size, ActualSize()); 220 EXPECT_EQ(all_file_size, ActualSize());
251 EXPECT_EQ(all_file_size, SizeInUsageFile()); 221 EXPECT_EQ(all_file_size, SizeInUsageFile());
252 GetUsageAndQuotaFromQuotaManager(); 222 GetUsageAndQuotaFromQuotaManager();
253 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); 223 EXPECT_EQ(quota::kQuotaStatusOk, quota_status());
254 EXPECT_EQ(all_file_size, usage()); 224 EXPECT_EQ(all_file_size, usage());
255 ASSERT_LT(all_file_size, quota()); 225 ASSERT_LT(all_file_size, quota());
256 226
257 operation()->Move(URLForPath(src_dir_path), URLForPath(dest_dir_path)); 227 operation()->Move(URLForPath(src_dir_path), URLForPath(dest_dir_path),
228 RecordStatusCallback());
258 MessageLoop::current()->RunAllPending(); 229 MessageLoop::current()->RunAllPending();
259 230
260 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); 231 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
261 EXPECT_TRUE(VirtualDirectoryExists(dest_dir_path.Append( 232 EXPECT_TRUE(VirtualDirectoryExists(dest_dir_path.Append(
262 child_dir_path_.BaseName()))); 233 child_dir_path_.BaseName())));
263 EXPECT_TRUE(VirtualFileExists(dest_dir_path.Append( 234 EXPECT_TRUE(VirtualFileExists(dest_dir_path.Append(
264 child_dir_path_.BaseName()).Append( 235 child_dir_path_.BaseName()).Append(
265 grandchild_file1_path_.BaseName()))); 236 grandchild_file1_path_.BaseName())));
266 237
267 EXPECT_EQ(all_file_size, ActualSize()); 238 EXPECT_EQ(all_file_size, ActualSize());
268 EXPECT_EQ(all_file_size, SizeInUsageFile()); 239 EXPECT_EQ(all_file_size, SizeInUsageFile());
269 GetUsageAndQuotaFromQuotaManager(); 240 GetUsageAndQuotaFromQuotaManager();
270 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); 241 EXPECT_EQ(quota::kQuotaStatusOk, quota_status());
271 EXPECT_EQ(all_file_size, usage()); 242 EXPECT_EQ(all_file_size, usage());
272 ASSERT_LT(all_file_size, quota()); 243 ASSERT_LT(all_file_size, quota());
273 } 244 }
274 245
275 TEST_F(FileSystemQuotaTest, TestCopySuccessSrcDirRecursive) { 246 TEST_F(FileSystemQuotaTest, TestCopySuccessSrcDirRecursive) {
276 FilePath src_dir_path(CreateVirtualTemporaryDir()); 247 FilePath src_dir_path(CreateVirtualTemporaryDir());
277 PrepareFileSet(src_dir_path); 248 PrepareFileSet(src_dir_path);
278 FilePath dest_dir1_path(CreateVirtualTemporaryDir()); 249 FilePath dest_dir1_path(CreateVirtualTemporaryDir());
279 FilePath dest_dir2_path(CreateVirtualTemporaryDir()); 250 FilePath dest_dir2_path(CreateVirtualTemporaryDir());
280 251
281 EXPECT_EQ(0, ActualSize()); 252 EXPECT_EQ(0, ActualSize());
282 253
283 operation()->Truncate(URLForPath(child_file1_path_), 8000); 254 operation()->Truncate(URLForPath(child_file1_path_), 8000,
284 operation()->Truncate(URLForPath(child_file2_path_), 700); 255 RecordStatusCallback());
285 operation()->Truncate(URLForPath(grandchild_file1_path_), 60); 256 operation()->Truncate(URLForPath(child_file2_path_), 700,
286 operation()->Truncate(URLForPath(grandchild_file2_path_), 5); 257 RecordStatusCallback());
258 operation()->Truncate(URLForPath(grandchild_file1_path_), 60,
259 RecordStatusCallback());
260 operation()->Truncate(URLForPath(grandchild_file2_path_), 5,
261 RecordStatusCallback());
287 MessageLoop::current()->RunAllPending(); 262 MessageLoop::current()->RunAllPending();
288 263
289 const int64 child_file_size = 8000 + 700; 264 const int64 child_file_size = 8000 + 700;
290 const int64 grandchild_file_size = 60 + 5; 265 const int64 grandchild_file_size = 60 + 5;
291 const int64 all_file_size = child_file_size + grandchild_file_size; 266 const int64 all_file_size = child_file_size + grandchild_file_size;
292 267
293 EXPECT_EQ(all_file_size, ActualSize()); 268 EXPECT_EQ(all_file_size, ActualSize());
294 EXPECT_EQ(all_file_size, SizeInUsageFile()); 269 EXPECT_EQ(all_file_size, SizeInUsageFile());
295 GetUsageAndQuotaFromQuotaManager(); 270 GetUsageAndQuotaFromQuotaManager();
296 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); 271 EXPECT_EQ(quota::kQuotaStatusOk, quota_status());
297 EXPECT_EQ(all_file_size, usage()); 272 EXPECT_EQ(all_file_size, usage());
298 ASSERT_LT(all_file_size, quota()); 273 ASSERT_LT(all_file_size, quota());
299 274
300 operation()->Copy(URLForPath(src_dir_path), URLForPath(dest_dir1_path)); 275 operation()->Copy(URLForPath(src_dir_path), URLForPath(dest_dir1_path),
276 RecordStatusCallback());
301 MessageLoop::current()->RunAllPending(); 277 MessageLoop::current()->RunAllPending();
302 278
303 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); 279 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
304 EXPECT_TRUE(VirtualDirectoryExists(src_dir_path.Append( 280 EXPECT_TRUE(VirtualDirectoryExists(src_dir_path.Append(
305 child_dir_path_.BaseName()))); 281 child_dir_path_.BaseName())));
306 EXPECT_TRUE(VirtualFileExists(src_dir_path.Append( 282 EXPECT_TRUE(VirtualFileExists(src_dir_path.Append(
307 child_dir_path_.BaseName()).Append( 283 child_dir_path_.BaseName()).Append(
308 grandchild_file1_path_.BaseName()))); 284 grandchild_file1_path_.BaseName())));
309 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); 285 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
310 EXPECT_TRUE(VirtualDirectoryExists(dest_dir1_path.Append( 286 EXPECT_TRUE(VirtualDirectoryExists(dest_dir1_path.Append(
311 child_dir_path_.BaseName()))); 287 child_dir_path_.BaseName())));
312 EXPECT_TRUE(VirtualFileExists(dest_dir1_path.Append( 288 EXPECT_TRUE(VirtualFileExists(dest_dir1_path.Append(
313 child_dir_path_.BaseName()).Append( 289 child_dir_path_.BaseName()).Append(
314 grandchild_file1_path_.BaseName()))); 290 grandchild_file1_path_.BaseName())));
315 291
316 EXPECT_EQ(2 * all_file_size, ActualSize()); 292 EXPECT_EQ(2 * all_file_size, ActualSize());
317 EXPECT_EQ(2 * all_file_size, SizeInUsageFile()); 293 EXPECT_EQ(2 * all_file_size, SizeInUsageFile());
318 GetUsageAndQuotaFromQuotaManager(); 294 GetUsageAndQuotaFromQuotaManager();
319 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); 295 EXPECT_EQ(quota::kQuotaStatusOk, quota_status());
320 EXPECT_EQ(2 * all_file_size, usage()); 296 EXPECT_EQ(2 * all_file_size, usage());
321 ASSERT_LT(2 * all_file_size, quota()); 297 ASSERT_LT(2 * all_file_size, quota());
322 298
323 operation()->Copy(URLForPath(child_dir_path_), URLForPath(dest_dir2_path)); 299 operation()->Copy(URLForPath(child_dir_path_), URLForPath(dest_dir2_path),
300 RecordStatusCallback());
324 MessageLoop::current()->RunAllPending(); 301 MessageLoop::current()->RunAllPending();
325 302
326 EXPECT_EQ(base::PLATFORM_FILE_OK, status()); 303 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
327 304
328 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, ActualSize()); 305 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, ActualSize());
329 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, SizeInUsageFile()); 306 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, SizeInUsageFile());
330 GetUsageAndQuotaFromQuotaManager(); 307 GetUsageAndQuotaFromQuotaManager();
331 EXPECT_EQ(quota::kQuotaStatusOk, quota_status()); 308 EXPECT_EQ(quota::kQuotaStatusOk, quota_status());
332 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, usage()); 309 EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, usage());
333 ASSERT_LT(2 * child_file_size + 3 * grandchild_file_size, quota()); 310 ASSERT_LT(2 * child_file_size + 3 * grandchild_file_size, quota());
334 } 311 }
335 312
336 } // namespace fileapi 313 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/fileapi/file_system_operation_write_unittest.cc ('k') | webkit/fileapi/file_system_test_helper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698