Chromium Code Reviews| Index: webkit/fileapi/file_system_quota_unittest.cc |
| diff --git a/webkit/fileapi/file_system_quota_unittest.cc b/webkit/fileapi/file_system_quota_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ab047801051e303fbc6f5de04efa117114682e03 |
| --- /dev/null |
| +++ b/webkit/fileapi/file_system_quota_unittest.cc |
| @@ -0,0 +1,374 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// This test checks the entire behavior of FileSystem usage and quota, such as: |
| +// 1) the actual size of files on disk, |
| +// 2) the described size in .usage, and |
| +// 3) the result of QuotaManager::GetUsageAndQuota. |
| + |
| +#include "webkit/fileapi/file_system_operation.h" |
| + |
| +#include "base/file_util.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_callback_factory.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/message_loop.h" |
| +#include "base/platform_file.h" |
| +#include "base/scoped_temp_dir.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "webkit/fileapi/file_system_callback_dispatcher.h" |
| +#include "webkit/fileapi/file_system_operation.h" |
| +#include "webkit/fileapi/file_system_test_helper.h" |
| +#include "webkit/fileapi/file_system_usage_cache.h" |
| +#include "webkit/fileapi/file_system_util.h" |
| +#include "webkit/fileapi/local_file_system_file_util.h" |
| +#include "webkit/quota/quota_manager.h" |
| + |
| +namespace fileapi { |
| + |
| +const int kFileOperationStatusNotSet = 1; |
| + |
| +class FileSystemQuotaTest : public testing::Test { |
| + public: |
| + FileSystemQuotaTest() |
| + : callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| + status_(kFileOperationStatusNotSet), |
| + quota_status_(quota::kQuotaStatusUnknown), |
| + usage_(-1), |
| + quota_(-1) {} |
| + |
| + FileSystemOperation* operation(); |
| + |
| + void set_status(int status) { status_ = status; } |
| + int status() const { return status_; } |
| + int64 quota() { return quota_; } |
| + |
| + virtual void SetUp(); |
| + virtual void TearDown(); |
| + |
| + void OnGetUsageAndQuota( |
| + quota::QuotaStatusCode status, int64 usage, int64 quota); |
| + |
| + protected: |
| + base::PlatformFile OpenFile(const FilePath& virtual_path); |
| + void PrepareFileSet(const FilePath& virtual_path); |
| + |
| + GURL URLForPath(const FilePath& path) const { |
| + return test_helper_.GetURLForPath(path); |
| + } |
| + |
| + FilePath PlatformPath(FilePath virtual_path) { |
|
kinuko
2011/06/13 12:01:19
const FilePath& ? (here and elsewhere)
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
Done.
|
| + return test_helper_.GetLocalPath(virtual_path); |
| + } |
| + |
| + int64 ActualSize() { |
| + return test_helper_.ComputeCurrentOriginUsage(); |
| + } |
| + |
| + int64 SizeInUsageFile() { |
| + return test_helper_.GetCachedOriginUsage(); |
| + } |
| + |
| + int64 SizeFromQuotaManager() { |
|
kinuko
2011/06/13 12:01:19
I might prefer declaring this as GetUsageAndQuotaF
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
Done.
|
| + quota_manager_->GetUsageAndQuota( |
| + test_helper_.origin(), test_helper_.storage_type(), |
| + callback_factory_.NewCallback( |
| + &FileSystemQuotaTest::OnGetUsageAndQuota)); |
| + MessageLoop::current()->RunAllPending(); |
| + return usage_; |
| + } |
| + |
| + bool VirtualFileExists(FilePath virtual_path) { |
| + return file_util::PathExists(PlatformPath(virtual_path)) && |
| + !file_util::DirectoryExists(PlatformPath(virtual_path)); |
| + } |
| + |
| + bool VirtualDirectoryExists(FilePath virtual_path) { |
| + return file_util::DirectoryExists(PlatformPath(virtual_path)); |
| + } |
| + |
| + FilePath CreateVirtualDirectory(const char* virtual_path_string) { |
|
kinuko
2011/06/13 12:01:19
this isn't used?
|
| + FilePath virtual_path(virtual_path_string); |
|
kinuko
2011/06/13 12:01:19
This wouldn't work on Windows would it?
You can u
|
| + file_util::CreateDirectory(PlatformPath(virtual_path)); |
| + return virtual_path; |
| + } |
| + |
| + FilePath CreateVirtualDirectoryInDir(const char* virtual_path_string, |
| + const FilePath& virtual_dir_path) { |
|
kinuko
2011/06/13 12:01:19
this isn't used?
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
Done.
|
| + FilePath virtual_path(virtual_dir_path.AppendASCII(virtual_path_string)); |
| + file_util::CreateDirectory(PlatformPath(virtual_path)); |
| + return virtual_path; |
| + } |
| + |
| + FilePath CreateVirtualTemporaryFileInDir(const FilePath& virtual_dir_path) { |
| + FilePath absolute_dir_path(PlatformPath(virtual_dir_path)); |
| + FilePath absolute_file_path; |
| + EXPECT_TRUE(file_util::CreateTemporaryFileInDir(absolute_dir_path, |
| + &absolute_file_path)); |
| + return virtual_dir_path.Append(absolute_file_path.BaseName()); |
| + } |
| + |
| + FilePath CreateVirtualTemporaryDirInDir(const FilePath& virtual_dir_path, |
| + const FilePath::StringType& prefix) { |
| + FilePath absolute_parent_dir_path(PlatformPath(virtual_dir_path)); |
| + FilePath absolute_child_dir_path; |
| + EXPECT_TRUE(file_util::CreateTemporaryDirInDir(absolute_parent_dir_path, |
| + prefix, |
| + &absolute_child_dir_path)); |
| + return virtual_dir_path.Append(absolute_child_dir_path.BaseName()); |
| + } |
| + |
| + FilePath CreateVirtualTemporaryDir(const FilePath::StringType& prefix) { |
| + return CreateVirtualTemporaryDirInDir(FilePath(), prefix); |
| + } |
| + |
| + FilePath child_dir_path_; |
| + FilePath child_file1_path_; |
| + FilePath child_file2_path_; |
| + FilePath grandchild_file1_path_; |
| + FilePath grandchild_file2_path_; |
| + |
| + private: |
| + FileSystemTestOriginHelper test_helper_; |
| + |
| + ScopedTempDir work_dir_; |
| + scoped_refptr<quota::QuotaManager> quota_manager_; |
| + |
| + base::PlatformFile child_file1_; |
| + base::PlatformFile child_file2_; |
| + base::PlatformFile grandchild_file1_; |
| + base::PlatformFile grandchild_file2_; |
|
kinuko
2011/06/13 12:01:19
If they're defined as member vars just to get dele
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
Ah, I found that they're not required at all. The
|
| + |
| + base::ScopedCallbackFactory<FileSystemQuotaTest> callback_factory_; |
| + |
| + // For post-operation status. |
| + int status_; |
| + quota::QuotaStatusCode quota_status_; |
| + int64 usage_; |
| + int64 quota_; |
| + base::PlatformFileInfo info_; |
| + FilePath path_; |
| + FilePath local_path_; |
| + std::vector<base::FileUtilProxy::Entry> entries_; |
|
kinuko
2011/06/13 12:01:19
many of these variables appear unused now.
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
Done.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(FileSystemQuotaTest); |
| +}; |
| + |
| +namespace { |
| + |
| +class MockDispatcher : public FileSystemCallbackDispatcher { |
| + public: |
| + MockDispatcher(FileSystemQuotaTest* test) : test_(test) { } |
| + |
| + virtual void DidFail(base::PlatformFileError status) { |
| + test_->set_status(status); |
| + } |
| + |
| + virtual void DidSucceed() { |
| + test_->set_status(base::PLATFORM_FILE_OK); |
| + } |
| + |
| + virtual void DidGetLocalPath(const FilePath& local_path) { |
| + ADD_FAILURE(); |
| + } |
| + |
| + virtual void DidReadMetadata( |
| + const base::PlatformFileInfo& info, |
| + const FilePath& platform_path) { |
| + ADD_FAILURE(); |
| + } |
| + |
| + virtual void DidReadDirectory( |
| + const std::vector<base::FileUtilProxy::Entry>& entries, |
| + bool /* has_more */) { |
| + ADD_FAILURE(); |
| + } |
| + |
| + virtual void DidOpenFileSystem(const std::string&, const GURL&) { |
| + ADD_FAILURE(); |
| + } |
| + |
| + virtual void DidWrite(int64 bytes, bool complete) { |
| + ADD_FAILURE(); |
| + } |
| + |
| + private: |
| + FileSystemQuotaTest* test_; |
| +}; |
| + |
| +} // namespace (anonymous) |
| + |
| +void FileSystemQuotaTest::SetUp() { |
| + ASSERT_TRUE(work_dir_.CreateUniqueTempDir()); |
| + FilePath filesystem_dir_path = work_dir_.path().AppendASCII("filesystem"); |
| + file_util::CreateDirectory(filesystem_dir_path); |
| + |
| + quota_manager_ = new quota::QuotaManager( |
| + false /* is_incognito */, |
| + filesystem_dir_path, |
| + base::MessageLoopProxy::CreateForCurrentThread(), |
| + base::MessageLoopProxy::CreateForCurrentThread(), |
| + NULL); |
| + |
| + test_helper_.SetUp(filesystem_dir_path, |
| + false /* incognito */, |
| + false /* unlimited quota */, |
| + quota_manager_->proxy(), |
| + LocalFileSystemFileUtil::GetInstance()); |
| + |
| + child_file1_ = base::kInvalidPlatformFileValue; |
| + child_file2_ = base::kInvalidPlatformFileValue; |
| + grandchild_file1_ = base::kInvalidPlatformFileValue; |
| + grandchild_file2_ = base::kInvalidPlatformFileValue; |
|
kinuko
2011/06/13 12:01:19
I think they should be initialized at the test cla
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
They're removed as described above.
|
| +} |
| + |
| +void FileSystemQuotaTest::TearDown() { |
| + quota_manager_ = NULL; |
| + |
| + EXPECT_NE(base::kInvalidPlatformFileValue, child_file1_); |
| + EXPECT_NE(base::kInvalidPlatformFileValue, child_file2_); |
| + EXPECT_NE(base::kInvalidPlatformFileValue, grandchild_file1_); |
| + EXPECT_NE(base::kInvalidPlatformFileValue, grandchild_file2_); |
| + base::ClosePlatformFile(child_file1_); |
| + base::ClosePlatformFile(child_file2_); |
| + base::ClosePlatformFile(grandchild_file1_); |
| + base::ClosePlatformFile(grandchild_file2_); |
| + |
| + // Runs cleanup tasks posted by QuataManager and FileSystemContext. |
| + MessageLoop::current()->RunAllPending(); |
|
kinuko
2011/06/13 12:01:19
We won't need this line as helper's TearDown() cal
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
Done.
|
| + test_helper_.TearDown(); |
| +} |
| + |
| +FileSystemOperation* FileSystemQuotaTest::operation() { |
| + return test_helper_.NewOperation(new MockDispatcher(this)); |
| +} |
| + |
| +base::PlatformFile FileSystemQuotaTest::OpenFile(const FilePath& virtual_path) { |
| + base::PlatformFile file; |
|
kinuko
2011/06/13 12:01:19
I think this line should be merged with line 251 (
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
OpenFile() is now removed as described above.
|
| + bool created; |
| + base::PlatformFileError error_code; |
|
kinuko
2011/06/13 12:01:19
maybe they should be initialized in prior? (create
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
ditto.
|
| + file = base::CreatePlatformFile( |
| + PlatformPath(virtual_path), |
| + base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE | |
| + base::PLATFORM_FILE_ASYNC, &created, &error_code); |
| + EXPECT_EQ(base::PLATFORM_FILE_OK, error_code); |
| + return file; |
| +} |
| + |
| +void FileSystemQuotaTest::OnGetUsageAndQuota( |
| + quota::QuotaStatusCode status, int64 usage, int64 quota) { |
| + quota_status_ = status; |
| + usage_ = usage; |
| + quota_ = quota; |
| +} |
| + |
| +void FileSystemQuotaTest::PrepareFileSet(const FilePath& virtual_path) { |
| + child_dir_path_ = CreateVirtualTemporaryDirInDir(virtual_path, "prefix"); |
| + child_file1_path_ = CreateVirtualTemporaryFileInDir(virtual_path); |
| + child_file1_ = OpenFile(child_file1_path_); |
| + child_file2_path_ = CreateVirtualTemporaryFileInDir(virtual_path); |
| + child_file2_ = OpenFile(child_file2_path_); |
| + grandchild_file1_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_); |
| + grandchild_file1_ = OpenFile(grandchild_file1_path_); |
| + grandchild_file2_path_ = CreateVirtualTemporaryFileInDir(child_dir_path_); |
| + grandchild_file2_ = OpenFile(grandchild_file2_path_); |
| +} |
| + |
| +TEST_F(FileSystemQuotaTest, TestMoveSuccessSrcDirRecursive) { |
| + FilePath src_dir_path(CreateVirtualTemporaryDir("")); |
|
kinuko
2011/06/13 12:01:19
I think this will need to be FILE_PATH_LITERAL("")
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
Removed all prefixes.
|
| + PrepareFileSet(src_dir_path); |
| + FilePath dest_dir_path(CreateVirtualTemporaryDir("")); |
| + |
| + EXPECT_EQ(0, ActualSize()); |
| + |
| + operation()->Truncate(URLForPath(child_file1_path_), 5000); |
| + operation()->Truncate(URLForPath(child_file2_path_), 400); |
| + operation()->Truncate(URLForPath(grandchild_file1_path_), 30); |
| + operation()->Truncate(URLForPath(grandchild_file2_path_), 2); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + const int64 all_file_size = 5000+400+30+2; |
|
kinuko
2011/06/13 12:01:19
nit: space between digits and '+'
Dai Mikurube (NOT FULLTIME)
2011/06/14 06:29:41
Done.
|
| + const int64 usage_file_size = FileSystemUsageCache::kUsageFileSize; |
| + |
| + EXPECT_EQ(all_file_size, ActualSize()); |
| + EXPECT_EQ(all_file_size, SizeInUsageFile()); |
| + EXPECT_EQ(all_file_size + usage_file_size, SizeFromQuotaManager()); |
| + ASSERT_LT(all_file_size, quota()); |
| + |
| + operation()->Move(URLForPath(src_dir_path), URLForPath(dest_dir_path)); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| + EXPECT_TRUE(VirtualDirectoryExists(dest_dir_path.Append( |
| + child_dir_path_.BaseName()))); |
| + EXPECT_TRUE(VirtualFileExists(dest_dir_path.Append( |
| + child_dir_path_.BaseName()).Append( |
| + grandchild_file1_path_.BaseName()))); |
| + |
| + EXPECT_EQ(all_file_size, ActualSize()); |
| + EXPECT_EQ(all_file_size, SizeInUsageFile()); |
| + EXPECT_EQ(all_file_size + usage_file_size, SizeFromQuotaManager()); |
| + ASSERT_LT(all_file_size, quota()); |
| +} |
| + |
| +TEST_F(FileSystemQuotaTest, TestCopySuccessSrcDirRecursive) { |
| + FilePath src_dir_path(CreateVirtualTemporaryDir("")); |
| + PrepareFileSet(src_dir_path); |
| + FilePath dest_dir1_path(CreateVirtualTemporaryDir("")); |
| + FilePath dest_dir2_path(CreateVirtualTemporaryDir("")); |
| + |
| + EXPECT_EQ(0, ActualSize()); |
| + |
| + operation()->Truncate(URLForPath(child_file1_path_), 8000); |
| + operation()->Truncate(URLForPath(child_file2_path_), 700); |
| + operation()->Truncate(URLForPath(grandchild_file1_path_), 60); |
| + operation()->Truncate(URLForPath(grandchild_file2_path_), 5); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + const int64 child_file_size = 8000+700; |
| + const int64 grandchild_file_size = 60+5; |
| + const int64 all_file_size = child_file_size + grandchild_file_size; |
| + const int64 usage_file_size = FileSystemUsageCache::kUsageFileSize; |
| + |
| + EXPECT_EQ(all_file_size, ActualSize()); |
| + EXPECT_EQ(all_file_size, SizeInUsageFile()); |
| + EXPECT_EQ(all_file_size + usage_file_size, SizeFromQuotaManager()); |
| + ASSERT_LT(all_file_size, quota()); |
| + |
| + operation()->Copy(URLForPath(src_dir_path), URLForPath(dest_dir1_path)); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| + EXPECT_TRUE(VirtualDirectoryExists(src_dir_path.Append( |
| + child_dir_path_.BaseName()))); |
| + EXPECT_TRUE(VirtualFileExists(src_dir_path.Append( |
| + child_dir_path_.BaseName()).Append( |
| + grandchild_file1_path_.BaseName()))); |
| + EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| + EXPECT_TRUE(VirtualDirectoryExists(dest_dir1_path.Append( |
| + child_dir_path_.BaseName()))); |
| + EXPECT_TRUE(VirtualFileExists(dest_dir1_path.Append( |
| + child_dir_path_.BaseName()).Append( |
| + grandchild_file1_path_.BaseName()))); |
| + |
| + EXPECT_EQ(2 * all_file_size, ActualSize()); |
| + EXPECT_EQ(2 * all_file_size, SizeInUsageFile()); |
| + EXPECT_EQ(2 * all_file_size + usage_file_size, SizeFromQuotaManager()); |
| + ASSERT_LT(2 * all_file_size, quota()); |
| + |
| + operation()->Copy(URLForPath(child_dir_path_), URLForPath(dest_dir2_path)); |
| + MessageLoop::current()->RunAllPending(); |
| + |
| + EXPECT_EQ(base::PLATFORM_FILE_OK, status()); |
| + |
| + EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, |
| + ActualSize()); |
| + EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size, |
| + SizeInUsageFile()); |
| + EXPECT_EQ(2 * child_file_size + 3 * grandchild_file_size + usage_file_size, |
| + SizeFromQuotaManager()); |
| + ASSERT_LT(2 * child_file_size + 3 * grandchild_file_size, quota()); |
| +} |
| + |
| +} // namespace fileapi |