This source file includes following definitions.
- weak_factory_
- SetUp
- TearDown
- VerifyAndClearChange
- file_system_context
- change_tracker
- TEST_F
- TEST_F
- TEST_F
#include "base/stl_util.h"
#include "chrome/browser/sync_file_system/local/canned_syncable_file_system.h"
#include "chrome/browser/sync_file_system/local/local_file_change_tracker.h"
#include "chrome/browser/sync_file_system/local/local_file_sync_context.h"
#include "chrome/browser/sync_file_system/local/sync_file_system_backend.h"
#include "chrome/browser/sync_file_system/syncable_file_system_util.h"
#include "content/public/test/async_file_test_helper.h"
#include "content/public/test/sandbox_file_system_test_helper.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
#include "third_party/leveldatabase/src/include/leveldb/env.h"
#include "webkit/browser/fileapi/file_system_context.h"
#include "webkit/browser/fileapi/file_system_operation_context.h"
#include "webkit/browser/fileapi/isolated_context.h"
#include "webkit/browser/quota/quota_manager.h"
#include "webkit/common/fileapi/file_system_types.h"
#include "webkit/common/quota/quota_types.h"
using base::PlatformFileError;
using content::SandboxFileSystemTestHelper;
using fileapi::FileSystemContext;
using fileapi::FileSystemOperationContext;
using fileapi::FileSystemURL;
using fileapi::FileSystemURLSet;
using quota::QuotaManager;
using quota::QuotaStatusCode;
namespace sync_file_system {
class SyncableFileSystemTest : public testing::Test {
public:
SyncableFileSystemTest()
: in_memory_env_(leveldb::NewMemEnv(leveldb::Env::Default())),
file_system_(GURL("http://example.com/"),
in_memory_env_.get(),
base::MessageLoopProxy::current().get(),
base::MessageLoopProxy::current().get()),
weak_factory_(this) {}
virtual void SetUp() {
ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
file_system_.SetUp(CannedSyncableFileSystem::QUOTA_ENABLED);
sync_context_ =
new LocalFileSyncContext(data_dir_.path(),
in_memory_env_.get(),
base::MessageLoopProxy::current().get(),
base::MessageLoopProxy::current().get());
ASSERT_EQ(
sync_file_system::SYNC_STATUS_OK,
file_system_.MaybeInitializeFileSystemContext(sync_context_.get()));
}
virtual void TearDown() {
if (sync_context_.get())
sync_context_->ShutdownOnUIThread();
sync_context_ = NULL;
file_system_.TearDown();
RevokeSyncableFileSystem();
}
protected:
void VerifyAndClearChange(const FileSystemURL& url,
const FileChange& expected_change) {
SCOPED_TRACE(testing::Message() << url.DebugString() <<
" expecting:" << expected_change.DebugString());
FileChangeList changes;
change_tracker()->GetChangesForURL(url, &changes);
ASSERT_EQ(1U, changes.size());
SCOPED_TRACE(testing::Message() << url.DebugString() <<
" actual:" << changes.DebugString());
EXPECT_EQ(expected_change, changes.front());
change_tracker()->ClearChangesForURL(url);
}
FileSystemURL URL(const std::string& path) {
return file_system_.URL(path);
}
FileSystemContext* file_system_context() {
return file_system_.file_system_context();
}
LocalFileChangeTracker* change_tracker() {
return file_system_.backend()->change_tracker();
}
ScopedEnableSyncFSDirectoryOperation enable_directory_operation_;
base::ScopedTempDir data_dir_;
content::TestBrowserThreadBundle thread_bundle_;
scoped_ptr<leveldb::Env> in_memory_env_;
CannedSyncableFileSystem file_system_;
private:
scoped_refptr<LocalFileSyncContext> sync_context_;
base::WeakPtrFactory<SyncableFileSystemTest> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(SyncableFileSystemTest);
};
TEST_F(SyncableFileSystemTest, SyncableLocalSandboxCombined) {
EXPECT_EQ(base::File::FILE_OK,
file_system_.OpenFileSystem());
EXPECT_EQ(base::File::FILE_OK,
file_system_.CreateDirectory(URL("dir")));
EXPECT_EQ(base::File::FILE_OK,
file_system_.CreateFile(URL("dir/foo")));
const int64 kOriginalQuota = QuotaManager::kSyncableStorageDefaultHostQuota;
const int64 kQuota = 12345 * 1024;
QuotaManager::kSyncableStorageDefaultHostQuota = kQuota;
int64 usage, quota;
EXPECT_EQ(quota::kQuotaStatusOk,
file_system_.GetUsageAndQuota(&usage, "a));
EXPECT_EQ(kQuota, quota);
EXPECT_GT(usage, 0);
const int64 kFileSizeToExtend = 333;
EXPECT_EQ(base::File::FILE_OK,
file_system_.CreateFile(URL("dir/foo")));
EXPECT_EQ(base::File::FILE_OK,
file_system_.TruncateFile(URL("dir/foo"), kFileSizeToExtend));
int64 new_usage;
EXPECT_EQ(quota::kQuotaStatusOk,
file_system_.GetUsageAndQuota(&new_usage, "a));
EXPECT_EQ(kFileSizeToExtend, new_usage - usage);
QuotaManager::kSyncableStorageDefaultHostQuota = new_usage;
EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE,
file_system_.TruncateFile(URL("dir/foo"), kFileSizeToExtend + 1));
usage = new_usage;
EXPECT_EQ(quota::kQuotaStatusOk,
file_system_.GetUsageAndQuota(&new_usage, "a));
EXPECT_EQ(usage, new_usage);
EXPECT_EQ(base::File::FILE_OK,
file_system_.DeleteFileSystem());
EXPECT_EQ(quota::kQuotaStatusOk,
file_system_.GetUsageAndQuota(&usage, "a));
EXPECT_EQ(0, usage);
QuotaManager::kSyncableStorageDefaultHostQuota = kOriginalQuota;
}
TEST_F(SyncableFileSystemTest, ChangeTrackerSimple) {
EXPECT_EQ(base::File::FILE_OK,
file_system_.OpenFileSystem());
const char kPath0[] = "dir a";
const char kPath1[] = "dir a/dir";
const char kPath2[] = "dir a/file";
const char kPath3[] = "dir b";
EXPECT_EQ(base::File::FILE_OK,
file_system_.CreateDirectory(URL(kPath0)));
EXPECT_EQ(base::File::FILE_OK,
file_system_.CreateDirectory(URL(kPath1)));
EXPECT_EQ(base::File::FILE_OK,
file_system_.CreateFile(URL(kPath2)));
EXPECT_EQ(base::File::FILE_OK,
file_system_.TruncateFile(URL(kPath2), 1));
EXPECT_EQ(base::File::FILE_OK,
file_system_.TruncateFile(URL(kPath2), 2));
FileSystemURLSet urls;
file_system_.GetChangedURLsInTracker(&urls);
EXPECT_EQ(3U, urls.size());
EXPECT_TRUE(ContainsKey(urls, URL(kPath0)));
EXPECT_TRUE(ContainsKey(urls, URL(kPath1)));
EXPECT_TRUE(ContainsKey(urls, URL(kPath2)));
VerifyAndClearChange(URL(kPath0),
FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
sync_file_system::SYNC_FILE_TYPE_DIRECTORY));
VerifyAndClearChange(URL(kPath1),
FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
sync_file_system::SYNC_FILE_TYPE_DIRECTORY));
VerifyAndClearChange(URL(kPath2),
FileChange(FileChange::FILE_CHANGE_ADD_OR_UPDATE,
sync_file_system::SYNC_FILE_TYPE_FILE));
EXPECT_EQ(base::File::FILE_OK,
file_system_.CreateDirectory(URL(kPath3)));
EXPECT_EQ(base::File::FILE_OK,
file_system_.Remove(URL(kPath3), false ));
urls.clear();
file_system_.GetChangedURLsInTracker(&urls);
EXPECT_TRUE(urls.empty());
EXPECT_EQ(base::File::FILE_OK,
file_system_.Remove(URL(kPath0), true ));
urls.clear();
file_system_.GetChangedURLsInTracker(&urls);
EXPECT_EQ(3U, urls.size());
EXPECT_TRUE(ContainsKey(urls, URL(kPath0)));
EXPECT_TRUE(ContainsKey(urls, URL(kPath1)));
EXPECT_TRUE(ContainsKey(urls, URL(kPath2)));
VerifyAndClearChange(URL(kPath0),
FileChange(FileChange::FILE_CHANGE_DELETE,
sync_file_system::SYNC_FILE_TYPE_DIRECTORY));
VerifyAndClearChange(URL(kPath1),
FileChange(FileChange::FILE_CHANGE_DELETE,
sync_file_system::SYNC_FILE_TYPE_DIRECTORY));
VerifyAndClearChange(URL(kPath2),
FileChange(FileChange::FILE_CHANGE_DELETE,
sync_file_system::SYNC_FILE_TYPE_FILE));
}
TEST_F(SyncableFileSystemTest, DisableDirectoryOperations) {
bool was_enabled = IsSyncFSDirectoryOperationEnabled();
SetEnableSyncFSDirectoryOperation(false);
EXPECT_EQ(base::File::FILE_OK,
file_system_.OpenFileSystem());
EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION,
file_system_.CreateDirectory(URL("dir")));
SandboxFileSystemTestHelper other_file_system_(
GURL("http://foo.com/"), fileapi::kFileSystemTypeTemporary);
other_file_system_.SetUp(file_system_.file_system_context());
const FileSystemURL kSrcDir = other_file_system_.CreateURLFromUTF8("/a");
const FileSystemURL kSrcChild = other_file_system_.CreateURLFromUTF8("/a/b");
EXPECT_EQ(base::File::FILE_OK,
content::AsyncFileTestHelper::CreateDirectory(
other_file_system_.file_system_context(), kSrcDir));
EXPECT_EQ(base::File::FILE_OK,
content::AsyncFileTestHelper::CreateFile(
other_file_system_.file_system_context(), kSrcChild));
EXPECT_NE(base::File::FILE_OK,
file_system_.Copy(kSrcDir, URL("dest")));
other_file_system_.TearDown();
SetEnableSyncFSDirectoryOperation(was_enabled);
}
}