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

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

Issue 6905095: Integrated test with the actual QuotaManager and PathManager. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 8 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
(Empty)
1 // Copyright (c) 2011 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 "webkit/fileapi/file_system_operation.h"
6
7 #include "base/file_util.h"
8 #include "base/logging.h"
9 #include "base/memory/scoped_callback_factory.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_temp_dir.h"
12 #include "base/message_loop.h"
13 #include "base/platform_file.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "webkit/fileapi/file_system_callback_dispatcher.h"
16 #include "webkit/fileapi/file_system_context.h"
17 #include "webkit/fileapi/file_system_file_util.h"
18 #include "webkit/fileapi/file_system_mount_point_provider.h"
19 #include "webkit/fileapi/file_system_operation.h"
20 #include "webkit/fileapi/file_system_path_manager.h"
21 #include "webkit/fileapi/file_system_usage_cache.h"
22 #include "webkit/fileapi/file_system_util.h"
23 #include "webkit/fileapi/local_file_system_file_util.h"
24 #include "webkit/fileapi/quota_file_util.h"
25 #include "webkit/fileapi/sandbox_mount_point_provider.h"
26 #include "webkit/quota/quota_manager.h"
27
28 namespace fileapi {
29
30 const int kFileOperationStatusNotSet = 1;
31
32 class FileSystemQuotaTest : public testing::Test {
33 public:
34 FileSystemQuotaTest()
35 : status_(kFileOperationStatusNotSet),
36 quota_status_(quota::kQuotaStatusUnknown),
37 usage_(-1),
38 quota_(-1),
39 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {}
40
41 FileSystemOperation* operation();
42
43 void set_local_path(const FilePath& path) { local_path_ = path; }
44 const FilePath& local_path() const { return local_path_; }
45 void set_status(int status) { status_ = status; }
46 int status() const { return status_; }
47 void set_info(const base::PlatformFileInfo& info) { info_ = info; }
48 const base::PlatformFileInfo& info() const { return info_; }
49 void set_path(const FilePath& path) { path_ = path; }
50 const FilePath& path() const { return path_; }
51 void set_entries(const std::vector<base::FileUtilProxy::Entry>& entries) {
52 entries_ = entries;
53 }
54 const std::vector<base::FileUtilProxy::Entry>& entries() const {
55 return entries_;
56 }
57
58 virtual void SetUp();
59 virtual void TearDown();
60
61 void OnGetUsageAndQuota(
62 quota::QuotaStatusCode status, int64 usage, int64 quota);
63
64 protected:
65 base::PlatformFile OpenFile(const FilePath& virtual_path);
66 void PrepareFileSet(const FilePath& virtual_path);
67
68 GURL URLForPath(const FilePath& path) const {
69 // Only the path will actually get used.
70 return GURL(GetFileSystemRootURI(GURL("http://www.example.com/"),
71 kFileSystemTypeTemporary).spec() + path.MaybeAsASCII());
72 }
73
74 FilePath PlatformPath(FilePath virtual_path) {
75 return root_dir_.Append(virtual_path);
76 }
77
78 bool VirtualFileExists(FilePath virtual_path) {
79 return file_util::PathExists(PlatformPath(virtual_path)) &&
80 !file_util::DirectoryExists(PlatformPath(virtual_path));
81 }
82
83 bool VirtualDirectoryExists(FilePath virtual_path) {
84 return file_util::DirectoryExists(PlatformPath(virtual_path));
85 }
86
87 FilePath CreateVirtualDirectory(const char* virtual_path_string) {
88 FilePath virtual_path(virtual_path_string);
89 file_util::CreateDirectory(PlatformPath(virtual_path));
90 return virtual_path;
91 }
92
93 FilePath CreateVirtualDirectoryInDir(const char* virtual_path_string,
94 const FilePath& virtual_dir_path) {
95 FilePath virtual_path(virtual_dir_path.AppendASCII(virtual_path_string));
96 file_util::CreateDirectory(PlatformPath(virtual_path));
97 return virtual_path;
98 }
99
100 FilePath CreateVirtualTemporaryFileInDir(const FilePath& virtual_dir_path) {
101 FilePath absolute_dir_path(PlatformPath(virtual_dir_path));
102 FilePath absolute_file_path;
103 if (file_util::CreateTemporaryFileInDir(absolute_dir_path,
104 &absolute_file_path))
105 return virtual_dir_path.Append(absolute_file_path.BaseName());
106 else
107 return FilePath();
108 }
109
110 FilePath CreateVirtualTemporaryDirInDir(const FilePath& virtual_dir_path,
111 const FilePath::StringType& prefix) {
112 FilePath absolute_parent_dir_path(PlatformPath(virtual_dir_path));
113 FilePath absolute_child_dir_path;
114 if (file_util::CreateTemporaryDirInDir(absolute_parent_dir_path, prefix,
115 &absolute_child_dir_path))
116 return virtual_dir_path.Append(absolute_child_dir_path.BaseName());
117 else
118 return FilePath();
119 }
120
121 FilePath CreateVirtualTemporaryDir(const FilePath::StringType& prefix) {
122 return CreateVirtualTemporaryDirInDir(FilePath(), prefix);
123 }
124
125 ScopedTempDir base_dir_;
126 scoped_refptr<FileSystemContext> file_system_context_;
127 scoped_refptr<quota::QuotaManager> quota_manager_;
128 scoped_ptr<FileSystemPathManager> path_manager_;
129
130 FilePath child_dir_path_;
131 FilePath child_file1_path_;
132 FilePath child_file2_path_;
133 FilePath grandchild_file1_path_;
134 FilePath grandchild_file2_path_;
135 base::PlatformFile child_file1_;
136 base::PlatformFile child_file2_;
137 base::PlatformFile grandchild_file1_;
138 base::PlatformFile grandchild_file2_;
139
140 // For post-operation status.
141 int status_;
142 quota::QuotaStatusCode quota_status_;
143 int64 usage_;
144 int64 quota_;
145 base::PlatformFileInfo info_;
146 FilePath path_;
147 FilePath local_path_;
148 FilePath filesystem_dir_;
149 FilePath root_dir_;
150 std::vector<base::FileUtilProxy::Entry> entries_;
151
152 base::ScopedCallbackFactory<FileSystemQuotaTest> callback_factory_;
153
154 DISALLOW_COPY_AND_ASSIGN(FileSystemQuotaTest);
155 };
156
157 namespace {
158
159 class MockDispatcher : public FileSystemCallbackDispatcher {
160 public:
161 MockDispatcher(FileSystemQuotaTest* test) : test_(test) { }
162
163 virtual void DidFail(base::PlatformFileError status) {
164 test_->set_status(status);
165 }
166
167 virtual void DidSucceed() {
168 test_->set_status(base::PLATFORM_FILE_OK);
169 }
170
171 virtual void DidGetLocalPath(const FilePath& local_path) {
172 test_->set_local_path(local_path);
173 test_->set_status(base::PLATFORM_FILE_OK);
174 }
175
176 virtual void DidReadMetadata(
177 const base::PlatformFileInfo& info,
178 const FilePath& platform_path) {
179 test_->set_info(info);
180 test_->set_path(platform_path);
181 test_->set_status(base::PLATFORM_FILE_OK);
182 }
183
184 virtual void DidReadDirectory(
185 const std::vector<base::FileUtilProxy::Entry>& entries,
186 bool /* has_more */) {
187 test_->set_entries(entries);
188 }
189
190 virtual void DidOpenFileSystem(const std::string&, const GURL&) {
191 NOTREACHED();
Paweł Hajdan Jr. 2011/04/29 09:21:51 nit: Did you mean FAIL/ADD_FAILURE here and below?
Dai Mikurube (NOT FULLTIME) 2011/05/10 08:58:22 Modified to ADD_FAILURE().
192 }
193
194 virtual void DidWrite(int64 bytes, bool complete) {
195 NOTREACHED();
196 }
197
198 private:
199 FileSystemQuotaTest* test_;
200 };
201
202 } // namespace (anonymous)
203
204 void FileSystemQuotaTest::SetUp() {
205 ASSERT_TRUE(base_dir_.CreateUniqueTempDir());
206 filesystem_dir_ = base_dir_.path().AppendASCII("filesystem");
207 file_util::CreateDirectory(filesystem_dir_);
208
209 path_manager_.reset(new FileSystemPathManager(
210 base::MessageLoopProxy::CreateForCurrentThread(),
211 filesystem_dir_, NULL, false, true));
212
213 quota_manager_ = new quota::QuotaManager(
214 false /* is_incognito */,
215 filesystem_dir_,
216 base::MessageLoopProxy::CreateForCurrentThread(),
217 base::MessageLoopProxy::CreateForCurrentThread());
218
219 file_system_context_ = new FileSystemContext(
220 base::MessageLoopProxy::CreateForCurrentThread(),
221 base::MessageLoopProxy::CreateForCurrentThread(),
222 NULL, quota_manager_, filesystem_dir_,
223 false /* is_incognito */, true, false,
224 path_manager_.get());
225
226 // Creates directory including "chrome-*".
227 root_dir_ = path_manager_->sandbox_provider()->
228 ValidateFileSystemRootAndGetPathOnFileThread(
229 GURL("http://www.example.com"), kFileSystemTypeTemporary,
230 FilePath(), true);
231 }
232
233 void FileSystemQuotaTest::TearDown() {
234 file_system_context_ = NULL;
235 quota_manager_ = NULL;
236 path_manager_.reset(NULL);
237 }
238
239 FileSystemOperation* FileSystemQuotaTest::operation() {
240 FileSystemOperation* operation = new FileSystemOperation(
241 new MockDispatcher(this),
242 base::MessageLoopProxy::CreateForCurrentThread(),
243 file_system_context_,
244 LocalFileSystemFileUtil::GetInstance());
245 operation->file_system_operation_context()->set_src_type(
246 kFileSystemTypeTemporary);
247 operation->file_system_operation_context()->set_dest_type(
248 kFileSystemTypeTemporary);
249 GURL origin_url("http://www.example.com/");
250 operation->file_system_operation_context()->set_src_origin_url(origin_url);
251 operation->file_system_operation_context()->set_dest_origin_url(origin_url);
252
253 return operation;
254 }
255
256 base::PlatformFile FileSystemQuotaTest::OpenFile(const FilePath& virtual_path) {
257 base::PlatformFile file;
258 bool created;
259 base::PlatformFileError error_code;
260 file = base::CreatePlatformFile(
261 PlatformPath(virtual_path),
262 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE |
263 base::PLATFORM_FILE_ASYNC, &created, &error_code);
264 //ASSERT_EQ(base::PLATFORM_FILE_OK, error_code);
265 return file;
266 }
267
268 void FileSystemQuotaTest::OnGetUsageAndQuota(
269 quota::QuotaStatusCode status, int64 usage, int64 quota) {
270 quota_status_ = status;
271 usage_ = usage;
272 quota_ = quota;
273 }
274
275 void FileSystemQuotaTest::PrepareFileSet(const FilePath& virtual_path) {
276 child_dir_path_ = CreateVirtualTemporaryDirInDir(virtual_path, "prefix");
277 child_file1_path_ = CreateVirtualTemporaryFileInDir(virtual_path);
278 child_file1_ = OpenFile(child_file1_path_);
279 child_file2_path_ = CreateVirtualTemporaryFileInDir(virtual_path);
280 child_file2_ = OpenFile(child_file2_path_);
281 grandchild_file1_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_);
282 grandchild_file1_ = OpenFile(grandchild_file1_path_);
283 grandchild_file2_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_);
284 grandchild_file2_ = OpenFile(grandchild_file2_path_);
285 }
286
287 TEST_F(FileSystemQuotaTest, TestMoveSuccessSrcDirRecursive) {
288 FilePath src_dir_path(CreateVirtualTemporaryDir(""));
289 PrepareFileSet(src_dir_path);
290 FilePath dest_dir_path(CreateVirtualTemporaryDir(""));
291
292 ASSERT_TRUE(base::TruncatePlatformFile(child_file1_, 1013));
293 ASSERT_TRUE(base::TruncatePlatformFile(child_file2_, 129));
294 ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file1_, 94));
295 ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file2_, 517));
296
297 EXPECT_EQ(1013+129+94+517,
298 file_util::ComputeDirectorySize(PlatformPath(src_dir_path)));
299
300 quota_manager_->GetUsageAndQuota(
301 GURL("http://www.example.com"), quota::kStorageTypeTemporary,
302 callback_factory_.NewCallback(&FileSystemQuotaTest::OnGetUsageAndQuota));
303 MessageLoop::current()->RunAllPending();
304 EXPECT_EQ(1013+129+94+517 + FileSystemUsageCache::kUsageFileSize, usage_);
305
306 operation()->Move(URLForPath(src_dir_path), URLForPath(dest_dir_path));
307 MessageLoop::current()->RunAllPending();
308
309 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
310 EXPECT_TRUE(VirtualDirectoryExists(dest_dir_path.Append(
311 child_dir_path_.BaseName())));
312 EXPECT_TRUE(VirtualFileExists(dest_dir_path.Append(
313 child_dir_path_.BaseName()).Append(
314 grandchild_file1_path_.BaseName())));
315
316 quota_manager_->GetUsageAndQuota(
317 GURL("http://www.example.com"), quota::kStorageTypeTemporary,
318 callback_factory_.NewCallback(&FileSystemQuotaTest::OnGetUsageAndQuota));
319 MessageLoop::current()->RunAllPending();
320 EXPECT_EQ(1013+129+94+517 + FileSystemUsageCache::kUsageFileSize, usage_);
321 }
322
323 TEST_F(FileSystemQuotaTest, TestCopySuccessSrcDirRecursive) {
324 FilePath src_dir_path(CreateVirtualTemporaryDir(""));
325 PrepareFileSet(src_dir_path);
326 FilePath dest_dir_path(CreateVirtualTemporaryDir(""));
327
328 ASSERT_TRUE(base::TruncatePlatformFile(child_file1_, 12));
329 ASSERT_TRUE(base::TruncatePlatformFile(child_file2_, 23));
330 ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file1_, 34));
331 ASSERT_TRUE(base::TruncatePlatformFile(grandchild_file2_, 45));
332
333 EXPECT_EQ(12+23+34+45,
334 file_util::ComputeDirectorySize(PlatformPath(src_dir_path)));
335
336 quota_manager_->GetUsageAndQuota(
337 GURL("http://www.example.com"), quota::kStorageTypeTemporary,
338 callback_factory_.NewCallback(&FileSystemQuotaTest::OnGetUsageAndQuota));
339 MessageLoop::current()->RunAllPending();
340 EXPECT_EQ(12+23+34+45 + FileSystemUsageCache::kUsageFileSize, usage_);
341
342 operation()->Copy(URLForPath(src_dir_path), URLForPath(dest_dir_path));
343 MessageLoop::current()->RunAllPending();
344
345 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
346 EXPECT_TRUE(VirtualDirectoryExists(src_dir_path.Append(
347 child_dir_path_.BaseName())));
348 EXPECT_TRUE(VirtualFileExists(src_dir_path.Append(
349 child_dir_path_.BaseName()).Append(
350 grandchild_file1_path_.BaseName())));
351 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
352 EXPECT_TRUE(VirtualDirectoryExists(dest_dir_path.Append(
353 child_dir_path_.BaseName())));
354 EXPECT_TRUE(VirtualFileExists(dest_dir_path.Append(
355 child_dir_path_.BaseName()).Append(
356 grandchild_file1_path_.BaseName())));
357
358 // TODO(dmikurube): These checks still fail... Notifying is required.
359 // we have to check :
360 // 1. The actual size of files on disk
361 // 2. The described size in .usage
362 // 3. The result of QuotaManager::GetUsageAndQuota
363 // are the same number.
364 EXPECT_EQ(2*(12+23+34+45) + FileSystemUsageCache::kUsageFileSize,
365 file_util::ComputeDirectorySize(PlatformPath(FilePath())));
366
367 quota_manager_->GetUsageAndQuota(
368 GURL("http://www.example.com"), quota::kStorageTypeTemporary,
369 callback_factory_.NewCallback(&FileSystemQuotaTest::OnGetUsageAndQuota));
370 MessageLoop::current()->RunAllPending();
371 EXPECT_EQ(2*(12+23+34+45) + FileSystemUsageCache::kUsageFileSize, usage_);
372
373 operation()->Copy(URLForPath(child_dir_path_), URLForPath(dest_dir_path));
374 MessageLoop::current()->RunAllPending();
375
376 quota_manager_->GetUsageAndQuota(
377 GURL("http://www.example.com"), quota::kStorageTypeTemporary,
378 callback_factory_.NewCallback(&FileSystemQuotaTest::OnGetUsageAndQuota));
379 MessageLoop::current()->RunAllPending();
380 EXPECT_EQ(2*(12+23)+3*(34+45) + FileSystemUsageCache::kUsageFileSize, usage_);
381 }
382
383 #if 0
Paweł Hajdan Jr. 2011/04/29 09:21:51 A general tip (I assume you will update it before
Dai Mikurube (NOT FULLTIME) 2011/05/10 08:58:22 Yes, I uploaded this patch just to share the curre
384 TEST_F(FileSystemQuotaTest, TestRemoveSuccess) {
385 FilePath empty_dir_path(CreateVirtualTemporaryDir(""));
386 EXPECT_TRUE(VirtualDirectoryExists(empty_dir_path));
387
388 operation()->Remove(URLForPath(empty_dir_path), false /* recursive */);
389 MessageLoop::current()->RunAllPending();
390 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
391 EXPECT_FALSE(VirtualDirectoryExists(empty_dir_path));
392
393 // Removing a non-empty directory with recursive flag == true should be ok.
394 // parent_dir
395 // | |
396 // child_dir child_file
397 // Verify deleting parent_dir.
398 FilePath parent_dir_path(CreateVirtualTemporaryDir(""));
399 FilePath child_file_path(CreateVirtualTemporaryFileInDir(parent_dir_path));
400 FilePath child_dir_path(CreateVirtualTemporaryDirInDir(parent_dir_path,
401 "child_dir"));
402 ASSERT_FALSE(child_dir_path.empty());
403
404 operation()->Remove(URLForPath(parent_dir_path), true /* recursive */);
405 MessageLoop::current()->RunAllPending();
406 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
407 EXPECT_FALSE(VirtualDirectoryExists(parent_dir_path));
408 }
409
410 TEST_F(FileSystemQuotaTest, TestTruncate) {
411 FilePath dir_path(CreateVirtualTemporaryDir(""));
412 FilePath file_path(CreateVirtualTemporaryFileInDir(dir_path));
413
414 char test_data[] = "test data";
415 int data_size = static_cast<int>(sizeof(test_data));
416 EXPECT_EQ(data_size,
417 file_util::WriteFile(PlatformPath(file_path),
418 test_data, data_size));
419
420 // Check that its length is the size of the data written.
421 operation()->GetMetadata(URLForPath(file_path));
422 MessageLoop::current()->RunAllPending();
423 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
424 EXPECT_FALSE(info().is_directory);
425 EXPECT_EQ(data_size, info().size);
426
427 // Extend the file by truncating it.
428 int length = 17;
429 operation()->Truncate(URLForPath(file_path), length);
430 MessageLoop::current()->RunAllPending();
431 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
432
433 // Check that its length is now 17 and that it's all zeroes after the test
434 // data.
435 base::PlatformFileInfo info;
436
437 EXPECT_TRUE(file_util::GetFileInfo(PlatformPath(file_path), &info));
438 EXPECT_EQ(length, info.size);
439 char data[100];
440 EXPECT_EQ(length, file_util::ReadFile(PlatformPath(file_path), data, length));
441 for (int i = 0; i < length; ++i) {
442 if (i < static_cast<int>(sizeof(test_data)))
443 EXPECT_EQ(test_data[i], data[i]);
444 else
445 EXPECT_EQ(0, data[i]);
446 }
447
448 // Shorten the file by truncating it.
449 length = 3;
450 operation()->Truncate(URLForPath(file_path), length);
451 MessageLoop::current()->RunAllPending();
452 EXPECT_EQ(base::PLATFORM_FILE_OK, status());
453
454 // Check that its length is now 3 and that it contains only bits of test data.
455 EXPECT_TRUE(file_util::GetFileInfo(PlatformPath(file_path), &info));
456 EXPECT_EQ(length, info.size);
457 EXPECT_EQ(length, file_util::ReadFile(PlatformPath(file_path), data, length));
458 for (int i = 0; i < length; ++i)
459 EXPECT_EQ(test_data[i], data[i]);
460 }
461 #endif
462
463 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/fileapi/file_system_operation_unittest.cc ('k') | webkit/tools/test_shell/test_shell.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698