This source file includes following definitions.
- Increment
- GetFileSystem
- closed_
- OpenFile
- Close
- closed
- VerifyWrite
- VerifyRead
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/chromeos/drive/fileapi/fileapi_worker.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/chromeos/drive/dummy_file_system.h"
#include "chrome/browser/chromeos/drive/test_util.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "google_apis/drive/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace drive {
namespace fileapi_internal {
namespace {
void Increment(int* num_called) {
++*num_called;
}
FileSystemInterface* GetFileSystem(FileSystemInterface* instance) {
return instance;
}
class TestFileSystemForOpenFile : public DummyFileSystem {
public:
TestFileSystemForOpenFile(const base::FilePath& local_file_path,
OpenMode expected_open_mode)
: local_file_path_(local_file_path),
expected_open_mode_(expected_open_mode),
closed_(false) {
}
virtual void OpenFile(const base::FilePath& file_path,
OpenMode open_mode,
const std::string& mime_type,
const drive::OpenFileCallback& callback) OVERRIDE {
EXPECT_EQ(expected_open_mode_, open_mode);
callback.Run(
FILE_ERROR_OK,
local_file_path_,
base::Bind(&TestFileSystemForOpenFile::Close, base::Unretained(this)));
}
void Close() {
closed_ = true;
}
bool closed() const { return closed_; }
private:
const base::FilePath local_file_path_;
const OpenMode expected_open_mode_;
bool closed_;
};
void VerifyWrite(
int64 expected_size,
const base::FilePath& expected_written_path,
const std::string& write_data,
base::File::Error result,
base::PlatformFile platform_file,
const base::Closure& close_callback) {
EXPECT_EQ(base::File::FILE_OK, result);
EXPECT_NE(base::kInvalidPlatformFileValue, platform_file);
EXPECT_FALSE(close_callback.is_null());
base::PlatformFileInfo info;
EXPECT_TRUE(base::GetPlatformFileInfo(platform_file, &info));
EXPECT_EQ(expected_size, info.size);
const int data_size = static_cast<int>(write_data.size());
EXPECT_EQ(data_size,
base::WritePlatformFile(platform_file, 0, write_data.c_str(),
data_size));
EXPECT_TRUE(base::TruncatePlatformFile(platform_file, data_size));
base::ClosePlatformFile(platform_file);
close_callback.Run();
std::string written;
EXPECT_TRUE(base::ReadFileToString(expected_written_path, &written));
EXPECT_EQ(write_data, written);
}
void VerifyRead(const std::string& expected_data,
base::File::Error result,
base::PlatformFile platform_file,
const base::Closure& close_callback) {
EXPECT_EQ(base::File::FILE_OK, result);
EXPECT_NE(base::kInvalidPlatformFileValue, platform_file);
EXPECT_FALSE(close_callback.is_null());
const int data_size = static_cast<int>(expected_data.size());
base::PlatformFileInfo info;
EXPECT_TRUE(base::GetPlatformFileInfo(platform_file, &info));
EXPECT_EQ(data_size, info.size);
std::vector<char> buffer(data_size);
EXPECT_EQ(data_size,
base::ReadPlatformFile(platform_file, 0, buffer.data(), data_size));
EXPECT_EQ(expected_data, std::string(buffer.begin(), buffer.end()));
base::ClosePlatformFile(platform_file);
close_callback.Run();
}
}
class FileApiWorkerTest : public testing::Test {
private:
content::TestBrowserThreadBundle thread_bundle_;
};
TEST_F(FileApiWorkerTest, RunFileSystemCallbackSuccess) {
DummyFileSystem dummy_file_system;
FileSystemInterface* file_system = NULL;
RunFileSystemCallback(
base::Bind(&GetFileSystem, &dummy_file_system),
google_apis::test_util::CreateCopyResultCallback(&file_system),
base::Closure());
EXPECT_EQ(&dummy_file_system, file_system);
}
TEST_F(FileApiWorkerTest, RunFileSystemCallbackFail) {
FileSystemInterface* file_system = NULL;
int num_called = 0;
RunFileSystemCallback(
base::Bind(&GetFileSystem, static_cast<FileSystemInterface*>(NULL)),
google_apis::test_util::CreateCopyResultCallback(&file_system),
base::Bind(&Increment, &num_called));
EXPECT_EQ(1, num_called);
RunFileSystemCallback(
base::Bind(&GetFileSystem, static_cast<FileSystemInterface*>(NULL)),
google_apis::test_util::CreateCopyResultCallback(&file_system),
base::Closure());
}
TEST_F(FileApiWorkerTest, OpenFileForCreateWrite) {
const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever");
const std::string kWriteData = "byebye";
base::FilePath temp_path;
base::CreateTemporaryFile(&temp_path);
TestFileSystemForOpenFile file_system(temp_path, CREATE_FILE);
const int64 kExpectedSize = 0;
OpenFile(kDummyPath,
base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE,
base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData),
&file_system);
test_util::RunBlockingPoolTask();
EXPECT_TRUE(file_system.closed());
}
TEST_F(FileApiWorkerTest, OpenFileForOpenAlwaysWrite) {
const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever");
const std::string kWriteData = "byebye";
const std::string kInitialData = "hello";
base::FilePath temp_path;
base::CreateTemporaryFile(&temp_path);
google_apis::test_util::WriteStringToFile(temp_path, kInitialData);
TestFileSystemForOpenFile file_system(temp_path, OPEN_OR_CREATE_FILE);
const int64 kExpectedSize = static_cast<int64>(kInitialData.size());
OpenFile(kDummyPath,
base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE,
base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData),
&file_system);
test_util::RunBlockingPoolTask();
EXPECT_TRUE(file_system.closed());
}
TEST_F(FileApiWorkerTest, OpenFileForOpenTruncatedWrite) {
const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever");
const std::string kInitialData = "hello";
const std::string kWriteData = "byebye";
base::FilePath temp_path;
base::CreateTemporaryFile(&temp_path);
google_apis::test_util::WriteStringToFile(temp_path, kInitialData);
TestFileSystemForOpenFile file_system(temp_path, OPEN_FILE);
const int64 kExpectedSize = 0;
OpenFile(kDummyPath,
base::PLATFORM_FILE_OPEN_TRUNCATED | base::PLATFORM_FILE_WRITE,
base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData),
&file_system);
test_util::RunBlockingPoolTask();
EXPECT_TRUE(file_system.closed());
}
TEST_F(FileApiWorkerTest, OpenFileForOpenCreateAlwaysWrite) {
const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever");
const std::string kInitialData = "hello";
const std::string kWriteData = "byebye";
base::FilePath temp_path;
base::CreateTemporaryFile(&temp_path);
google_apis::test_util::WriteStringToFile(temp_path, kInitialData);
TestFileSystemForOpenFile file_system(temp_path, OPEN_OR_CREATE_FILE);
const int64 kExpectedSize = 0;
OpenFile(kDummyPath,
base::PLATFORM_FILE_CREATE_ALWAYS | base::PLATFORM_FILE_WRITE,
base::Bind(&VerifyWrite, kExpectedSize, temp_path, kWriteData),
&file_system);
test_util::RunBlockingPoolTask();
EXPECT_TRUE(file_system.closed());
}
TEST_F(FileApiWorkerTest, OpenFileForOpenRead) {
const base::FilePath kDummyPath = base::FilePath::FromUTF8Unsafe("whatever");
const std::string kInitialData = "hello";
base::FilePath temp_path;
base::CreateTemporaryFile(&temp_path);
google_apis::test_util::WriteStringToFile(temp_path, kInitialData);
TestFileSystemForOpenFile file_system(temp_path, OPEN_FILE);
OpenFile(kDummyPath,
base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ,
base::Bind(&VerifyRead, kInitialData),
&file_system);
test_util::RunBlockingPoolTask();
EXPECT_TRUE(file_system.closed());
}
}
}