This source file includes following definitions.
- set_quit_closure
- observerd_local_id
- OnDirectoryChangedByOperation
- OnEntryUpdatedByOperation
- SetUp
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/chromeos/drive/file_system/get_file_for_saving_operation.h"
#include "base/callback.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/run_loop.h"
#include "base/task_runner_util.h"
#include "chrome/browser/chromeos/drive/drive.pb.h"
#include "chrome/browser/chromeos/drive/file_cache.h"
#include "chrome/browser/chromeos/drive/file_errors.h"
#include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
#include "chrome/browser/chromeos/drive/file_write_watcher.h"
#include "google_apis/drive/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace drive {
namespace file_system {
namespace {
class TestObserver : public OperationObserver {
public:
void set_quit_closure(const base::Closure& quit_closure) {
quit_closure_ = quit_closure;
}
const std::string& observerd_local_id() const {
return observed_local_id_;
}
virtual void OnDirectoryChangedByOperation(
const base::FilePath& path) OVERRIDE {}
virtual void OnEntryUpdatedByOperation(const std::string& local_id) OVERRIDE {
observed_local_id_ = local_id;
if (!quit_closure_.is_null())
quit_closure_.Run();
}
private:
std::string observed_local_id_;
base::Closure quit_closure_;
};
}
class GetFileForSavingOperationTest : public OperationTestBase {
protected:
GetFileForSavingOperationTest()
: OperationTestBase(content::TestBrowserThreadBundle::IO_MAINLOOP) {
}
virtual void SetUp() OVERRIDE {
OperationTestBase::SetUp();
operation_.reset(new GetFileForSavingOperation(
logger(), blocking_task_runner(), &observer_, scheduler(), metadata(),
cache(), temp_dir()));
operation_->file_write_watcher_for_testing()->DisableDelayForTesting();
}
TestObserver observer_;
scoped_ptr<GetFileForSavingOperation> operation_;
};
TEST_F(GetFileForSavingOperationTest, GetFileForSaving_Exist) {
base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/File 1.txt"));
ResourceEntry src_entry;
ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
FileError error = FILE_ERROR_FAILED;
scoped_ptr<ResourceEntry> entry;
base::FilePath local_path;
operation_->GetFileForSaving(
drive_path,
google_apis::test_util::CreateCopyResultCallback(
&error, &local_path, &entry));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
EXPECT_EQ(src_entry.resource_id(), entry->resource_id());
bool success = false;
FileCacheEntry cache_entry;
base::PostTaskAndReplyWithResult(
blocking_task_runner(),
FROM_HERE,
base::Bind(&internal::FileCache::GetCacheEntry,
base::Unretained(cache()),
GetLocalId(drive_path),
&cache_entry),
google_apis::test_util::CreateCopyResultCallback(&success));
test_util::RunBlockingPoolTask();
EXPECT_TRUE(success);
EXPECT_TRUE(cache_entry.is_present());
EXPECT_TRUE(cache_entry.is_dirty());
{
base::RunLoop run_loop;
observer_.set_quit_closure(run_loop.QuitClosure());
google_apis::test_util::WriteStringToFile(local_path, "hello");
run_loop.Run();
EXPECT_EQ(GetLocalId(drive_path), observer_.observerd_local_id());
}
}
TEST_F(GetFileForSavingOperationTest, GetFileForSaving_NotExist) {
base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/NotExist.txt"));
ResourceEntry src_entry;
ASSERT_EQ(FILE_ERROR_NOT_FOUND,
GetLocalResourceEntry(drive_path, &src_entry));
FileError error = FILE_ERROR_FAILED;
scoped_ptr<ResourceEntry> entry;
base::FilePath local_path;
operation_->GetFileForSaving(
drive_path,
google_apis::test_util::CreateCopyResultCallback(
&error, &local_path, &entry));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
EXPECT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
int64 size = -1;
EXPECT_TRUE(base::GetFileSize(local_path, &size));
EXPECT_EQ(0, size);
}
TEST_F(GetFileForSavingOperationTest, GetFileForSaving_Directory) {
base::FilePath drive_path(FILE_PATH_LITERAL("drive/root/Directory 1"));
ResourceEntry src_entry;
ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(drive_path, &src_entry));
ASSERT_TRUE(src_entry.file_info().is_directory());
FileError error = FILE_ERROR_FAILED;
scoped_ptr<ResourceEntry> entry;
base::FilePath local_path;
operation_->GetFileForSaving(
drive_path,
google_apis::test_util::CreateCopyResultCallback(
&error, &local_path, &entry));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_EXISTS, error);
}
}
}