| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #include "webkit/fileapi/quota_file_util.h" | 5 #include "webkit/fileapi/quota_file_util.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/memory/scoped_callback_factory.h" | 8 #include "base/memory/scoped_callback_factory.h" |
| 9 #include "base/memory/scoped_temp_dir.h" | 9 #include "base/memory/scoped_temp_dir.h" |
| 10 #include "base/message_loop_proxy.h" | 10 #include "base/message_loop_proxy.h" |
| 11 #include "base/platform_file.h" | 11 #include "base/platform_file.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "webkit/fileapi/file_system_context.h" | 13 #include "webkit/fileapi/file_system_context.h" |
| 14 #include "webkit/fileapi/file_system_file_util.h" | 14 #include "webkit/fileapi/file_system_file_util.h" |
| 15 #include "webkit/fileapi/file_system_operation_context.h" | 15 #include "webkit/fileapi/file_system_operation_context.h" |
| 16 #include "webkit/fileapi/file_system_path_manager.h" | 16 #include "webkit/fileapi/file_system_path_manager.h" |
| 17 #include "webkit/fileapi/file_system_types.h" | 17 #include "webkit/fileapi/file_system_types.h" |
| 18 #include "webkit/fileapi/file_system_usage_cache.h" | 18 #include "webkit/fileapi/file_system_usage_cache.h" |
| 19 #include "webkit/fileapi/sandbox_mount_point_provider.h" |
| 19 | 20 |
| 20 using namespace fileapi; | 21 using namespace fileapi; |
| 21 | 22 |
| 22 namespace { | |
| 23 | |
| 24 class MockFileSystemPathManager : public FileSystemPathManager { | |
| 25 public: | |
| 26 MockFileSystemPathManager(const FilePath& filesystem_path) | |
| 27 : FileSystemPathManager(base::MessageLoopProxy::CreateForCurrentThread(), | |
| 28 filesystem_path, NULL, false, true), | |
| 29 test_filesystem_path_(filesystem_path) {} | |
| 30 | |
| 31 virtual FilePath ValidateFileSystemRootAndGetPathOnFileThread( | |
| 32 const GURL& origin_url, | |
| 33 fileapi::FileSystemType type, | |
| 34 const FilePath& virtual_path, | |
| 35 bool create) { | |
| 36 return test_filesystem_path_; | |
| 37 } | |
| 38 | |
| 39 private: | |
| 40 FilePath test_filesystem_path_; | |
| 41 }; | |
| 42 | |
| 43 } // namespace (anonymous) | |
| 44 | |
| 45 class QuotaFileUtilTest : public testing::Test { | 23 class QuotaFileUtilTest : public testing::Test { |
| 46 public: | 24 public: |
| 47 QuotaFileUtilTest() | 25 QuotaFileUtilTest() |
| 48 : callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 26 : callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 49 } | 27 } |
| 50 | 28 |
| 51 void SetUp() { | 29 void SetUp() { |
| 52 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | 30 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
| 53 filesystem_dir_ = data_dir_.path().AppendASCII("filesystem"); | 31 filesystem_dir_ = data_dir_.path().AppendASCII("filesystem"); |
| 54 file_util::CreateDirectory(filesystem_dir_); | 32 file_util::CreateDirectory(filesystem_dir_); |
| 55 | 33 |
| 34 path_manager_ = new FileSystemPathManager( |
| 35 base::MessageLoopProxy::CreateForCurrentThread(), |
| 36 filesystem_dir_, NULL, false, true); |
| 37 |
| 38 file_system_context_ = new FileSystemContext( |
| 39 base::MessageLoopProxy::CreateForCurrentThread(), |
| 40 base::MessageLoopProxy::CreateForCurrentThread(), |
| 41 NULL, NULL, FilePath(), false, |
| 42 true, true, path_manager_); |
| 43 |
| 44 // Creates the filesystem directory. |
| 45 path_manager_->sandbox_provider()-> |
| 46 ValidateFileSystemRootAndGetPathOnFileThread( |
| 47 GURL("http://www.example.com"), kFileSystemTypeTemporary, |
| 48 FilePath(), true); |
| 49 |
| 56 usage_file_path_ = Path(FileSystemUsageCache::kUsageFileName); | 50 usage_file_path_ = Path(FileSystemUsageCache::kUsageFileName); |
| 57 FileSystemUsageCache::UpdateUsage(usage_file_path_, 0); | 51 FileSystemUsageCache::UpdateUsage(usage_file_path_, 0); |
| 58 } | 52 } |
| 59 | 53 |
| 60 protected: | 54 protected: |
| 55 scoped_refptr<FileSystemContext> file_system_context_; |
| 56 FileSystemPathManager* path_manager_; |
| 57 |
| 61 FileSystemOperationContext* NewContext() { | 58 FileSystemOperationContext* NewContext() { |
| 62 FileSystemOperationContext *context = new FileSystemOperationContext( | 59 FileSystemOperationContext *context = new FileSystemOperationContext( |
| 63 new FileSystemContext(base::MessageLoopProxy::CreateForCurrentThread(), | 60 file_system_context_, QuotaFileUtil::GetInstance()); |
| 64 base::MessageLoopProxy::CreateForCurrentThread(), | 61 context->set_src_origin_url(GURL("http://www.example.com")); |
| 65 NULL, NULL, FilePath(), false, | |
| 66 true, true, | |
| 67 new MockFileSystemPathManager(filesystem_dir_)), | |
| 68 QuotaFileUtil::GetInstance()); | |
| 69 context->set_src_type(fileapi::kFileSystemTypeTemporary); | 62 context->set_src_type(fileapi::kFileSystemTypeTemporary); |
| 70 return context; | 63 return context; |
| 71 } | 64 } |
| 72 | 65 |
| 73 QuotaFileUtil* FileUtil() { | 66 QuotaFileUtil* FileUtil() { |
| 74 return QuotaFileUtil::GetInstance(); | 67 return QuotaFileUtil::GetInstance(); |
| 75 } | 68 } |
| 76 | 69 |
| 77 FilePath Path(const char *file_name) { | 70 FilePath Path(const char *file_name) { |
| 78 return filesystem_dir_.AppendASCII(file_name); | 71 return path_manager_->sandbox_provider()->GetBaseDirectoryForOriginAndType( |
| 72 GURL("http://www.example.com"), kFileSystemTypeTemporary). |
| 73 AppendASCII(file_name); |
| 79 } | 74 } |
| 80 | 75 |
| 81 base::PlatformFileError CreateFile(const char* file_name, | 76 base::PlatformFileError CreateFile(const char* file_name, |
| 82 base::PlatformFile* file_handle, bool* created) { | 77 base::PlatformFile* file_handle, bool* created) { |
| 83 int file_flags = base::PLATFORM_FILE_CREATE | | 78 int file_flags = base::PLATFORM_FILE_CREATE | |
| 84 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_ASYNC; | 79 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_ASYNC; |
| 85 | 80 |
| 86 scoped_ptr<FileSystemOperationContext> context(NewContext()); | 81 scoped_ptr<FileSystemOperationContext> context(NewContext()); |
| 87 return FileUtil()->CreateOrOpen(context.get(), Path(file_name), | 82 return FileUtil()->CreateOrOpen(context.get(), Path(file_name), |
| 88 file_flags, file_handle, created); | 83 file_flags, file_handle, created); |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 ASSERT_EQ(1140, GetCachedUsage()); | 419 ASSERT_EQ(1140, GetCachedUsage()); |
| 425 | 420 |
| 426 context.reset(NewContext()); | 421 context.reset(NewContext()); |
| 427 context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit); | 422 context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit); |
| 428 ASSERT_EQ(base::PLATFORM_FILE_OK, | 423 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 429 QuotaFileUtil::GetInstance()->Delete(context.get(), | 424 QuotaFileUtil::GetInstance()->Delete(context.get(), |
| 430 Path(dir), | 425 Path(dir), |
| 431 true)); | 426 true)); |
| 432 ASSERT_EQ(0, GetCachedUsage()); | 427 ASSERT_EQ(0, GetCachedUsage()); |
| 433 } | 428 } |
| OLD | NEW |