summaryrefslogtreecommitdiffstats
path: root/webkit/fileapi/syncable
diff options
context:
space:
mode:
authorkinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-10-09 09:12:37 +0000
committerkinuko@chromium.org <kinuko@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-10-09 09:12:37 +0000
commitbfbfb0d659d904967767bfa033dd1db8090f8940 (patch)
tree5c3fee561ee60e9778cf5b1b8d86ad819b15db3b /webkit/fileapi/syncable
parent7d0843650a0f12af402c60b26297600d9896ab98 (diff)
downloadchromium_src-bfbfb0d659d904967767bfa033dd1db8090f8940.zip
chromium_src-bfbfb0d659d904967767bfa033dd1db8090f8940.tar.gz
chromium_src-bfbfb0d659d904967767bfa033dd1db8090f8940.tar.bz2
Add common helper code for SyncableFileSystem testing
BUG=154234 TEST=existing test (SyncableFileSystemTest.*) Review URL: https://codereview.chromium.org/11033058 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@160809 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'webkit/fileapi/syncable')
-rw-r--r--webkit/fileapi/syncable/canned_syncable_file_system.cc149
-rw-r--r--webkit/fileapi/syncable/canned_syncable_file_system.h99
-rw-r--r--webkit/fileapi/syncable/syncable_file_system_unittest.cc135
3 files changed, 282 insertions, 101 deletions
diff --git a/webkit/fileapi/syncable/canned_syncable_file_system.cc b/webkit/fileapi/syncable/canned_syncable_file_system.cc
new file mode 100644
index 0000000..50e4c51
--- /dev/null
+++ b/webkit/fileapi/syncable/canned_syncable_file_system.cc
@@ -0,0 +1,149 @@
+// Copyright (c) 2012 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.
+
+#include "webkit/fileapi/syncable/canned_syncable_file_system.h"
+
+#include "base/bind.h"
+#include "base/file_util.h"
+#include "base/message_loop_proxy.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "webkit/fileapi/file_system_context.h"
+#include "webkit/fileapi/file_system_operation_context.h"
+#include "webkit/fileapi/file_system_task_runners.h"
+#include "webkit/fileapi/file_system_util.h"
+#include "webkit/fileapi/isolated_context.h"
+#include "webkit/fileapi/local_file_system_operation.h"
+#include "webkit/fileapi/mock_file_system_options.h"
+#include "webkit/quota/mock_special_storage_policy.h"
+#include "webkit/quota/quota_manager.h"
+
+using base::PlatformFileError;
+using quota::QuotaManager;
+
+namespace fileapi {
+
+CannedSyncableFileSystem::CannedSyncableFileSystem(
+ const GURL& origin, const std::string& service)
+ : service_name_(service),
+ test_helper_(origin, kFileSystemTypeSyncable),
+ result_(base::PLATFORM_FILE_OK),
+ sync_status_(SYNC_STATUS_OK),
+ weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
+}
+
+CannedSyncableFileSystem::~CannedSyncableFileSystem() {}
+
+void CannedSyncableFileSystem::SetUp() {
+ ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
+
+ scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
+ new quota::MockSpecialStoragePolicy();
+
+ quota_manager_ = new QuotaManager(
+ false /* is_incognito */,
+ data_dir_.path(),
+ base::MessageLoopProxy::current(),
+ base::MessageLoopProxy::current(),
+ storage_policy);
+
+ file_system_context_ = new FileSystemContext(
+ FileSystemTaskRunners::CreateMockTaskRunners(),
+ storage_policy,
+ quota_manager_->proxy(),
+ data_dir_.path(),
+ CreateAllowFileAccessOptions());
+
+ test_helper_.SetUp(file_system_context_.get(), NULL);
+}
+
+void CannedSyncableFileSystem::TearDown() {
+ quota_manager_ = NULL;
+ test_helper_.TearDown();
+}
+
+FileSystemURL CannedSyncableFileSystem::URL(const std::string& path) const {
+ return FileSystemURL(GURL(root_url_.spec() + path));
+}
+
+PlatformFileError CannedSyncableFileSystem::OpenFileSystem() {
+ file_system_context_->OpenSyncableFileSystem(
+ service_name_,
+ test_helper_.origin(), test_helper_.type(),
+ true /* create */,
+ base::Bind(&CannedSyncableFileSystem::DidOpenFileSystem,
+ weak_factory_.GetWeakPtr()));
+ MessageLoop::current()->RunAllPending();
+ return result_;
+}
+
+PlatformFileError CannedSyncableFileSystem::CreateDirectory(
+ const FileSystemURL& url) {
+ result_ = base::PLATFORM_FILE_ERROR_FAILED;
+ test_helper_.NewOperation()->CreateDirectory(
+ url, false /* exclusive */, false /* recursive */,
+ base::Bind(&CannedSyncableFileSystem::StatusCallback,
+ weak_factory_.GetWeakPtr()));
+ MessageLoop::current()->RunAllPending();
+ return result_;
+}
+
+PlatformFileError CannedSyncableFileSystem::CreateFile(
+ const FileSystemURL& url) {
+ result_ = base::PLATFORM_FILE_ERROR_FAILED;
+ test_helper_.NewOperation()->CreateFile(
+ url, false /* exclusive */,
+ base::Bind(&CannedSyncableFileSystem::StatusCallback,
+ weak_factory_.GetWeakPtr()));
+ MessageLoop::current()->RunAllPending();
+ return result_;
+}
+
+PlatformFileError CannedSyncableFileSystem::TruncateFile(
+ const FileSystemURL& url, int64 size) {
+ result_ = base::PLATFORM_FILE_ERROR_FAILED;
+ test_helper_.NewOperation()->Truncate(
+ url, size,
+ base::Bind(&CannedSyncableFileSystem::StatusCallback,
+ weak_factory_.GetWeakPtr()));
+ MessageLoop::current()->RunAllPending();
+ return result_;
+}
+
+PlatformFileError CannedSyncableFileSystem::Remove(
+ const FileSystemURL& url, bool recursive) {
+ result_ = base::PLATFORM_FILE_ERROR_FAILED;
+ test_helper_.NewOperation()->Remove(
+ url, recursive,
+ base::Bind(&CannedSyncableFileSystem::StatusCallback,
+ weak_factory_.GetWeakPtr()));
+ MessageLoop::current()->RunAllPending();
+ return result_;
+}
+
+PlatformFileError CannedSyncableFileSystem::DeleteFileSystem() {
+ file_system_context_->DeleteFileSystem(
+ test_helper_.origin(), test_helper_.type(),
+ base::Bind(&CannedSyncableFileSystem::StatusCallback,
+ weak_factory_.GetWeakPtr()));
+ MessageLoop::current()->RunAllPending();
+ return result_;
+}
+
+void CannedSyncableFileSystem::DidOpenFileSystem(
+ PlatformFileError result, const std::string& name, const GURL& root) {
+ result_ = result;
+ root_url_ = root;
+}
+
+void CannedSyncableFileSystem::StatusCallback(PlatformFileError result) {
+ result_ = result;
+}
+
+FileSystemOperationContext* CannedSyncableFileSystem::NewOperationContext() {
+ FileSystemOperationContext* context = test_helper_.NewOperationContext();
+ context->set_allowed_bytes_growth(kint64max);
+ return context;
+}
+
+} // namespace fileapi
diff --git a/webkit/fileapi/syncable/canned_syncable_file_system.h b/webkit/fileapi/syncable/canned_syncable_file_system.h
new file mode 100644
index 0000000..10dd446
--- /dev/null
+++ b/webkit/fileapi/syncable/canned_syncable_file_system.h
@@ -0,0 +1,99 @@
+// Copyright (c) 2012 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.
+
+#ifndef WEBKIT_FILEAPI_SYNCABLE_CANNED_SYNCABLE_FILE_SYSTEM_H_
+#define WEBKIT_FILEAPI_SYNCABLE_CANNED_SYNCABLE_FILE_SYSTEM_H_
+
+#include <string>
+
+#include "base/basictypes.h"
+#include "base/message_loop.h"
+#include "base/platform_file.h"
+#include "base/scoped_temp_dir.h"
+#include "webkit/fileapi/file_system_types.h"
+#include "webkit/fileapi/fileapi_export.h"
+#include "webkit/fileapi/local_file_system_test_helper.h"
+#include "webkit/fileapi/syncable/sync_status_code.h"
+
+namespace base {
+class Thread;
+class MessageLoopProxy;
+}
+
+namespace quota {
+class QuotaManager;
+}
+
+namespace fileapi {
+
+class FileSystemContext;
+
+// A canned syncable filesystem for testing.
+// This internally creates its own QuotaManager and FileSystemContext
+// (as we do so for each isolated application).
+class FILEAPI_EXPORT CannedSyncableFileSystem {
+ public:
+ CannedSyncableFileSystem(const GURL& origin, const std::string& service);
+ ~CannedSyncableFileSystem();
+
+ // SetUp must be called before using this instance.
+ void SetUp();
+
+ // TearDown must be called before destructing this instance.
+ void TearDown();
+
+ // Creates a FileSystemURL for the given (utf8) path string.
+ FileSystemURL URL(const std::string& path) const;
+
+ // Opens a new syncable file system.
+ base::PlatformFileError OpenFileSystem();
+
+ // Accessors.
+ FileSystemContext* file_system_context() {
+ return file_system_context_.get();
+ }
+ quota::QuotaManager* quota_manager() { return quota_manager_.get(); }
+ GURL origin() const { return test_helper_.origin(); }
+ FileSystemType type() const { return test_helper_.type(); }
+ quota::StorageType storage_type() const {
+ return test_helper_.storage_type();
+ }
+
+ // Helper routines to perform file system operations.
+ // (They run on the current thread and returns synchronously).
+ base::PlatformFileError CreateDirectory(const FileSystemURL& url);
+ base::PlatformFileError CreateFile(const FileSystemURL& url);
+ base::PlatformFileError TruncateFile(const FileSystemURL& url, int64 size);
+ base::PlatformFileError Remove(const FileSystemURL& url, bool recursive);
+
+ // Pruges the file system local storage.
+ base::PlatformFileError DeleteFileSystem();
+
+ private:
+ // Callbacks.
+ void DidOpenFileSystem(base::PlatformFileError result,
+ const std::string& name,
+ const GURL& root);
+ void StatusCallback(base::PlatformFileError result);
+
+ FileSystemOperationContext* NewOperationContext();
+
+ ScopedTempDir data_dir_;
+ const std::string service_name_;
+
+ scoped_refptr<quota::QuotaManager> quota_manager_;
+ scoped_refptr<FileSystemContext> file_system_context_;
+ LocalFileSystemTestOriginHelper test_helper_;
+ GURL root_url_;
+ base::PlatformFileError result_;
+ SyncStatusCode sync_status_;
+
+ base::WeakPtrFactory<CannedSyncableFileSystem> weak_factory_;
+
+ DISALLOW_COPY_AND_ASSIGN(CannedSyncableFileSystem);
+};
+
+} // namespace fileapi
+
+#endif // WEBKIT_FILEAPI_SYNCABLE_CANNED_SYNCABLE_FILE_SYSTEM_H_
diff --git a/webkit/fileapi/syncable/syncable_file_system_unittest.cc b/webkit/fileapi/syncable/syncable_file_system_unittest.cc
index e0a1848..4e84820 100644
--- a/webkit/fileapi/syncable/syncable_file_system_unittest.cc
+++ b/webkit/fileapi/syncable/syncable_file_system_unittest.cc
@@ -2,23 +2,13 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "base/basictypes.h"
-#include "base/bind.h"
-#include "base/file_util.h"
-#include "base/message_loop.h"
-#include "base/message_loop_proxy.h"
-#include "base/platform_file.h"
-#include "base/scoped_temp_dir.h"
#include "testing/gtest/include/gtest/gtest.h"
-#include "webkit/fileapi/file_system_context.h"
-#include "webkit/fileapi/file_system_operation_context.h"
#include "webkit/fileapi/file_system_task_runners.h"
#include "webkit/fileapi/file_system_types.h"
-#include "webkit/fileapi/file_system_util.h"
+#include "webkit/fileapi/isolated_context.h"
#include "webkit/fileapi/local_file_system_operation.h"
-#include "webkit/fileapi/local_file_system_test_helper.h"
-#include "webkit/fileapi/mock_file_system_options.h"
-#include "webkit/quota/mock_special_storage_policy.h"
+#include "webkit/fileapi/syncable/canned_syncable_file_system.h"
+#include "webkit/fileapi/syncable/syncable_file_system_util.h"
#include "webkit/quota/quota_manager.h"
#include "webkit/quota/quota_types.h"
@@ -30,38 +20,23 @@ namespace fileapi {
class SyncableFileSystemTest : public testing::Test {
public:
SyncableFileSystemTest()
- : test_helper_(GURL("http://example.com/"), kFileSystemTypeSyncable),
+ : file_system_(GURL("http://example.com/"), "test"),
quota_status_(quota::kQuotaStatusUnknown),
usage_(-1),
quota_(-1),
weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {}
void SetUp() {
- ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
-
- scoped_refptr<quota::SpecialStoragePolicy> storage_policy =
- new quota::MockSpecialStoragePolicy();
-
- quota_manager_ = new QuotaManager(
- false /* is_incognito */,
- data_dir_.path(),
- base::MessageLoopProxy::current(),
- base::MessageLoopProxy::current(),
- storage_policy);
-
- file_system_context_ = new FileSystemContext(
- FileSystemTaskRunners::CreateMockTaskRunners(),
- storage_policy,
- quota_manager_->proxy(),
- data_dir_.path(),
- CreateAllowFileAccessOptions());
-
- test_helper_.SetUp(file_system_context_.get(), NULL);
+ file_system_.SetUp();
}
void TearDown() {
- quota_manager_ = NULL;
- test_helper_.TearDown();
+ file_system_.TearDown();
+
+ // Make sure we don't leave the external filesystem.
+ // (CannedSyncableFileSystem::TearDown does not do this as there may be
+ // multiple syncable file systems registered for the name)
+ RevokeSyncableFileSystem("test");
}
void DidGetUsageAndQuota(QuotaStatusCode status, int64 usage, int64 quota) {
@@ -70,28 +45,12 @@ class SyncableFileSystemTest : public testing::Test {
quota_ = quota;
}
- void DidOpenFileSystem(base::PlatformFileError result,
- const std::string& name,
- const GURL& root) {
- result_ = result;
- root_url_ = root;
- }
-
- void StatusCallback(base::PlatformFileError result) {
- result_ = result;
- }
-
protected:
- FileSystemOperationContext* NewOperationContext() {
- FileSystemOperationContext* context = test_helper_.NewOperationContext();
- context->set_allowed_bytes_growth(kint64max);
- return context;
- }
-
void GetUsageAndQuota(int64* usage, int64* quota) {
- quota_manager_->GetUsageAndQuota(
- test_helper_.origin(),
- test_helper_.storage_type(),
+ quota_status_ = quota::kQuotaStatusUnknown;
+ file_system_.quota_manager()->GetUsageAndQuota(
+ file_system_.origin(),
+ file_system_.storage_type(),
base::Bind(&SyncableFileSystemTest::DidGetUsageAndQuota,
weak_factory_.GetWeakPtr()));
MessageLoop::current()->RunAllPending();
@@ -103,16 +62,14 @@ class SyncableFileSystemTest : public testing::Test {
}
FileSystemURL URL(const std::string& path) {
- return FileSystemURL(GURL(root_url_.spec() + path));
+ return file_system_.URL(path);
}
ScopedTempDir data_dir_;
MessageLoop message_loop_;
- scoped_refptr<QuotaManager> quota_manager_;
- scoped_refptr<FileSystemContext> file_system_context_;
- LocalFileSystemTestOriginHelper test_helper_;
- GURL root_url_;
- base::PlatformFileError result_;
+
+ CannedSyncableFileSystem file_system_;
+
QuotaStatusCode quota_status_;
int64 usage_;
int64 quota_;
@@ -124,29 +81,14 @@ class SyncableFileSystemTest : public testing::Test {
// Brief combined testing. Just see if all the sandbox feature works.
TEST_F(SyncableFileSystemTest, SyncableLocalSandboxCombined) {
// Opens a syncable file system.
- file_system_context_->OpenSyncableFileSystem(
- "syncable-test",
- test_helper_.origin(), test_helper_.type(),
- true /* create */,
- base::Bind(&SyncableFileSystemTest::DidOpenFileSystem,
- weak_factory_.GetWeakPtr()));
- MessageLoop::current()->RunAllPending();
- EXPECT_EQ(base::PLATFORM_FILE_OK, result_);
+ EXPECT_EQ(base::PLATFORM_FILE_OK,
+ file_system_.OpenFileSystem());
// Do some operations.
- test_helper_.NewOperation()->CreateDirectory(
- URL("dir"), false /* exclusive */, false /* recursive */,
- base::Bind(&SyncableFileSystemTest::StatusCallback,
- weak_factory_.GetWeakPtr()));
- MessageLoop::current()->RunAllPending();
- EXPECT_EQ(base::PLATFORM_FILE_OK, result_);
-
- test_helper_.NewOperation()->CreateFile(
- URL("dir/foo"), false /* exclusive */,
- base::Bind(&SyncableFileSystemTest::StatusCallback,
- weak_factory_.GetWeakPtr()));
- MessageLoop::current()->RunAllPending();
- EXPECT_EQ(base::PLATFORM_FILE_OK, result_);
+ EXPECT_EQ(base::PLATFORM_FILE_OK,
+ file_system_.CreateDirectory(URL("dir")));
+ EXPECT_EQ(base::PLATFORM_FILE_OK,
+ file_system_.CreateFile(URL("dir/foo")));
const int64 kQuota = 12345 * 1024;
QuotaManager::kSyncableStorageDefaultHostQuota = kQuota;
@@ -160,12 +102,11 @@ TEST_F(SyncableFileSystemTest, SyncableLocalSandboxCombined) {
// Truncate to extend an existing file and see if the usage reflects it.
const int64 kFileSizeToExtend = 333;
- test_helper_.NewOperation()->Truncate(
- URL("dir/foo"), kFileSizeToExtend,
- base::Bind(&SyncableFileSystemTest::StatusCallback,
- weak_factory_.GetWeakPtr()));
- MessageLoop::current()->RunAllPending();
- EXPECT_EQ(base::PLATFORM_FILE_OK, result_);
+ EXPECT_EQ(base::PLATFORM_FILE_OK,
+ file_system_.CreateFile(URL("dir/foo")));
+
+ EXPECT_EQ(base::PLATFORM_FILE_OK,
+ file_system_.TruncateFile(URL("dir/foo"), kFileSizeToExtend));
int64 new_usage;
GetUsageAndQuota(&new_usage, NULL);
@@ -174,24 +115,16 @@ TEST_F(SyncableFileSystemTest, SyncableLocalSandboxCombined) {
// Shrink the quota to the current usage, try to extend the file further
// and see if it fails.
QuotaManager::kSyncableStorageDefaultHostQuota = new_usage;
- test_helper_.NewOperation()->Truncate(
- URL("dir/foo"), kFileSizeToExtend + 1,
- base::Bind(&SyncableFileSystemTest::StatusCallback,
- weak_factory_.GetWeakPtr()));
- MessageLoop::current()->RunAllPending();
- EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, result_);
+ EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
+ file_system_.TruncateFile(URL("dir/foo"), kFileSizeToExtend + 1));
usage = new_usage;
GetUsageAndQuota(&new_usage, NULL);
EXPECT_EQ(usage, new_usage);
// Deletes the file system.
- file_system_context_->DeleteFileSystem(
- test_helper_.origin(), test_helper_.type(),
- base::Bind(&SyncableFileSystemTest::StatusCallback,
- weak_factory_.GetWeakPtr()));
- MessageLoop::current()->RunAllPending();
- EXPECT_EQ(base::PLATFORM_FILE_OK, result_);
+ EXPECT_EQ(base::PLATFORM_FILE_OK,
+ file_system_.DeleteFileSystem());
// Now the usage must be zero.
GetUsageAndQuota(&usage, NULL);