This source file includes following definitions.
- GetStatus
- GetCancelStatus
- SetUp
- TearDown
- operation_runner
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/basictypes.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/platform_file.h"
#include "base/run_loop.h"
#include "content/public/test/test_file_system_context.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "webkit/browser/fileapi/file_system_context.h"
#include "webkit/browser/fileapi/file_system_operation_runner.h"
using fileapi::FileSystemContext;
using fileapi::FileSystemOperationRunner;
using fileapi::FileSystemType;
using fileapi::FileSystemURL;
namespace content {
void GetStatus(bool* done,
base::File::Error *status_out,
base::File::Error status) {
ASSERT_FALSE(*done);
*done = true;
*status_out = status;
}
void GetCancelStatus(bool* operation_done,
bool* cancel_done,
base::File::Error *status_out,
base::File::Error status) {
ASSERT_TRUE(*operation_done);
ASSERT_FALSE(*cancel_done);
*cancel_done = true;
*status_out = status;
}
class FileSystemOperationRunnerTest : public testing::Test {
protected:
FileSystemOperationRunnerTest() {}
virtual ~FileSystemOperationRunnerTest() {}
virtual void SetUp() OVERRIDE {
ASSERT_TRUE(base_.CreateUniqueTempDir());
base::FilePath base_dir = base_.path();
file_system_context_ =
CreateFileSystemContextForTesting(NULL, base_dir);
}
virtual void TearDown() OVERRIDE {
file_system_context_ = NULL;
base::RunLoop().RunUntilIdle();
}
FileSystemURL URL(const std::string& path) {
return file_system_context_->CreateCrackedFileSystemURL(
GURL("http://example.com"), fileapi::kFileSystemTypeTemporary,
base::FilePath::FromUTF8Unsafe(path));
}
FileSystemOperationRunner* operation_runner() {
return file_system_context_->operation_runner();
}
private:
base::ScopedTempDir base_;
base::MessageLoop message_loop_;
scoped_refptr<FileSystemContext> file_system_context_;
DISALLOW_COPY_AND_ASSIGN(FileSystemOperationRunnerTest);
};
TEST_F(FileSystemOperationRunnerTest, NotFoundError) {
bool done = false;
base::File::Error status = base::File::FILE_ERROR_FAILED;
operation_runner()->Truncate(URL("foo"), 0,
base::Bind(&GetStatus, &done, &status));
ASSERT_FALSE(done);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(done);
ASSERT_EQ(base::File::FILE_ERROR_NOT_FOUND, status);
}
TEST_F(FileSystemOperationRunnerTest, InvalidURLError) {
bool done = false;
base::File::Error status = base::File::FILE_ERROR_FAILED;
operation_runner()->Truncate(FileSystemURL(), 0,
base::Bind(&GetStatus, &done, &status));
ASSERT_FALSE(done);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(done);
ASSERT_EQ(base::File::FILE_ERROR_INVALID_URL, status);
}
TEST_F(FileSystemOperationRunnerTest, NotFoundErrorAndCancel) {
bool done = false;
bool cancel_done = false;
base::File::Error status = base::File::FILE_ERROR_FAILED;
base::File::Error cancel_status = base::File::FILE_ERROR_FAILED;
FileSystemOperationRunner::OperationID id =
operation_runner()->Truncate(URL("foo"), 0,
base::Bind(&GetStatus, &done, &status));
operation_runner()->Cancel(id, base::Bind(&GetCancelStatus,
&done, &cancel_done,
&cancel_status));
ASSERT_FALSE(done);
ASSERT_FALSE(cancel_done);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(done);
ASSERT_TRUE(cancel_done);
ASSERT_EQ(base::File::FILE_ERROR_NOT_FOUND, status);
ASSERT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, cancel_status);
}
TEST_F(FileSystemOperationRunnerTest, InvalidURLErrorAndCancel) {
bool done = false;
bool cancel_done = false;
base::File::Error status = base::File::FILE_ERROR_FAILED;
base::File::Error cancel_status = base::File::FILE_ERROR_FAILED;
FileSystemOperationRunner::OperationID id =
operation_runner()->Truncate(FileSystemURL(), 0,
base::Bind(&GetStatus, &done, &status));
operation_runner()->Cancel(id, base::Bind(&GetCancelStatus,
&done, &cancel_done,
&cancel_status));
ASSERT_FALSE(done);
ASSERT_FALSE(cancel_done);
base::RunLoop().RunUntilIdle();
ASSERT_TRUE(done);
ASSERT_TRUE(cancel_done);
ASSERT_EQ(base::File::FILE_ERROR_INVALID_URL, status);
ASSERT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, cancel_status);
}
TEST_F(FileSystemOperationRunnerTest, CancelWithInvalidId) {
const FileSystemOperationRunner::OperationID kInvalidId = -1;
bool done = true;
bool cancel_done = false;
base::File::Error cancel_status = base::File::FILE_ERROR_FAILED;
operation_runner()->Cancel(kInvalidId, base::Bind(&GetCancelStatus,
&done, &cancel_done,
&cancel_status));
ASSERT_TRUE(cancel_done);
ASSERT_EQ(base::File::FILE_ERROR_INVALID_OPERATION, cancel_status);
}
}