This source file includes following definitions.
- SetUp
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "chrome/browser/chromeos/drive/file_system/download_operation.h"
#include "base/file_util.h"
#include "base/task_runner_util.h"
#include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h"
#include "chrome/browser/chromeos/drive/file_cache.h"
#include "chrome/browser/chromeos/drive/file_system/operation_test_base.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/browser/chromeos/drive/job_scheduler.h"
#include "chrome/browser/drive/fake_drive_service.h"
#include "google_apis/drive/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/constants/cryptohome.h"
namespace drive {
namespace file_system {
class DownloadOperationTest : public OperationTestBase {
protected:
virtual void SetUp() OVERRIDE {
OperationTestBase::SetUp();
operation_.reset(new DownloadOperation(
blocking_task_runner(), observer(), scheduler(), metadata(), cache(),
temp_dir()));
}
scoped_ptr<DownloadOperation> operation_;
};
TEST_F(DownloadOperationTest,
EnsureFileDownloadedByPath_FromServer_EnoughSpace) {
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
ResourceEntry src_entry;
ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry));
const int64 file_size = src_entry.file_info().size();
fake_free_disk_space_getter()->set_default_value(
file_size + cryptohome::kMinFreeSpaceInBytes);
FileError error = FILE_ERROR_FAILED;
base::FilePath file_path;
scoped_ptr<ResourceEntry> entry;
operation_->EnsureFileDownloadedByPath(
file_in_root,
ClientContext(USER_INITIATED),
GetFileContentInitializedCallback(),
google_apis::GetContentCallback(),
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
EXPECT_FALSE(entry->file_specific_info().is_hosted_document());
EXPECT_EQ(1U, observer()->get_changed_paths().size());
EXPECT_EQ(1U, observer()->get_changed_paths().count(file_in_root.DirName()));
}
TEST_F(DownloadOperationTest,
EnsureFileDownloadedByPath_FromServer_NoSpaceAtAll) {
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
fake_free_disk_space_getter()->set_default_value(0);
FileError error = FILE_ERROR_OK;
base::FilePath file_path;
scoped_ptr<ResourceEntry> entry;
operation_->EnsureFileDownloadedByPath(
file_in_root,
ClientContext(USER_INITIATED),
GetFileContentInitializedCallback(),
google_apis::GetContentCallback(),
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_NO_LOCAL_SPACE, error);
}
TEST_F(DownloadOperationTest,
EnsureFileDownloadedByPath_FromServer_NoEnoughSpaceButCanFreeUp) {
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
ResourceEntry src_entry;
ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry));
const int64 file_size = src_entry.file_info().size();
fake_free_disk_space_getter()->PushFakeValue(
file_size + cryptohome::kMinFreeSpaceInBytes);
fake_free_disk_space_getter()->PushFakeValue(0);
fake_free_disk_space_getter()->set_default_value(
file_size + cryptohome::kMinFreeSpaceInBytes);
const std::string content(file_size, 'x');
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const base::FilePath tmp_file =
temp_dir.path().AppendASCII("something.txt");
ASSERT_TRUE(google_apis::test_util::WriteStringToFile(tmp_file, content));
FileError error = FILE_ERROR_FAILED;
base::PostTaskAndReplyWithResult(
blocking_task_runner(),
FROM_HERE,
base::Bind(&internal::FileCache::Store,
base::Unretained(cache()),
"<id>", "<md5>", tmp_file,
internal::FileCache::FILE_OPERATION_COPY),
google_apis::test_util::CreateCopyResultCallback(&error));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
base::FilePath file_path;
scoped_ptr<ResourceEntry> entry;
operation_->EnsureFileDownloadedByPath(
file_in_root,
ClientContext(USER_INITIATED),
GetFileContentInitializedCallback(),
google_apis::GetContentCallback(),
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
EXPECT_FALSE(entry->file_specific_info().is_hosted_document());
EXPECT_EQ(1U, observer()->get_changed_paths().size());
EXPECT_EQ(1U, observer()->get_changed_paths().count(file_in_root.DirName()));
FileCacheEntry cache_entry;
bool result = true;
base::PostTaskAndReplyWithResult(
blocking_task_runner(),
FROM_HERE,
base::Bind(&internal::FileCache::GetCacheEntry,
base::Unretained(cache()),
"<id>",
&cache_entry),
google_apis::test_util::CreateCopyResultCallback(&result));
test_util::RunBlockingPoolTask();
ASSERT_FALSE(result);
}
TEST_F(DownloadOperationTest,
EnsureFileDownloadedByPath_FromServer_EnoughSpaceButBecomeFull) {
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
ResourceEntry src_entry;
ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry));
const int64 file_size = src_entry.file_info().size();
fake_free_disk_space_getter()->PushFakeValue(
file_size + cryptohome::kMinFreeSpaceInBytes);
fake_free_disk_space_getter()->set_default_value(
cryptohome::kMinFreeSpaceInBytes - 1);
FileError error = FILE_ERROR_OK;
base::FilePath file_path;
scoped_ptr<ResourceEntry> entry;
operation_->EnsureFileDownloadedByPath(
file_in_root,
ClientContext(USER_INITIATED),
GetFileContentInitializedCallback(),
google_apis::GetContentCallback(),
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_NO_LOCAL_SPACE, error);
}
TEST_F(DownloadOperationTest, EnsureFileDownloadedByPath_FromCache) {
base::FilePath temp_file;
ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir(), &temp_file));
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
ResourceEntry src_entry;
ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry));
FileError error = FILE_ERROR_OK;
base::PostTaskAndReplyWithResult(
blocking_task_runner(),
FROM_HERE,
base::Bind(&internal::FileCache::Store,
base::Unretained(cache()),
GetLocalId(file_in_root),
src_entry.file_specific_info().md5(),
temp_file,
internal::FileCache::FILE_OPERATION_COPY),
google_apis::test_util::CreateCopyResultCallback(&error));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
base::FilePath file_path;
scoped_ptr<ResourceEntry> entry;
operation_->EnsureFileDownloadedByPath(
file_in_root,
ClientContext(USER_INITIATED),
GetFileContentInitializedCallback(),
google_apis::GetContentCallback(),
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
EXPECT_FALSE(entry->file_specific_info().is_hosted_document());
}
TEST_F(DownloadOperationTest, EnsureFileDownloadedByPath_HostedDocument) {
base::FilePath file_in_root(FILE_PATH_LITERAL(
"drive/root/Document 1 excludeDir-test.gdoc"));
FileError error = FILE_ERROR_FAILED;
base::FilePath file_path;
scoped_ptr<ResourceEntry> entry;
operation_->EnsureFileDownloadedByPath(
file_in_root,
ClientContext(USER_INITIATED),
GetFileContentInitializedCallback(),
google_apis::GetContentCallback(),
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
EXPECT_TRUE(entry->file_specific_info().is_hosted_document());
EXPECT_FALSE(file_path.empty());
EXPECT_EQ(GURL(entry->file_specific_info().alternate_url()),
util::ReadUrlFromGDocFile(file_path));
EXPECT_EQ(entry->resource_id(), util::ReadResourceIdFromGDocFile(file_path));
EXPECT_EQ(FILE_PATH_LITERAL(".gdoc"), file_path.Extension());
}
TEST_F(DownloadOperationTest, EnsureFileDownloadedByLocalId) {
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
ResourceEntry src_entry;
ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry));
FileError error = FILE_ERROR_OK;
base::FilePath file_path;
scoped_ptr<ResourceEntry> entry;
operation_->EnsureFileDownloadedByLocalId(
GetLocalId(file_in_root),
ClientContext(USER_INITIATED),
GetFileContentInitializedCallback(),
google_apis::GetContentCallback(),
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
EXPECT_FALSE(entry->file_specific_info().is_hosted_document());
EXPECT_EQ(1U, observer()->get_changed_paths().size());
EXPECT_EQ(1U, observer()->get_changed_paths().count(file_in_root.DirName()));
}
TEST_F(DownloadOperationTest,
EnsureFileDownloadedByPath_WithGetContentCallback) {
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
{
FileError initialized_error = FILE_ERROR_FAILED;
scoped_ptr<ResourceEntry> entry, entry_dontcare;
base::FilePath local_path, local_path_dontcare;
google_apis::test_util::TestGetContentCallback get_content_callback;
FileError completion_error = FILE_ERROR_FAILED;
base::Closure cancel_download = operation_->EnsureFileDownloadedByPath(
file_in_root,
ClientContext(USER_INITIATED),
google_apis::test_util::CreateCopyResultCallback(
&initialized_error, &local_path, &entry),
get_content_callback.callback(),
google_apis::test_util::CreateCopyResultCallback(
&completion_error, &local_path_dontcare, &entry_dontcare));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, initialized_error);
ASSERT_TRUE(entry);
ASSERT_TRUE(local_path.empty());
EXPECT_FALSE(cancel_download.is_null());
EXPECT_EQ(static_cast<size_t>(entry->file_info().size()),
get_content_callback.GetConcatenatedData().size());
EXPECT_EQ(FILE_ERROR_OK, completion_error);
EXPECT_EQ(1U, observer()->get_changed_paths().size());
EXPECT_EQ(1U,
observer()->get_changed_paths().count(file_in_root.DirName()));
}
{
FileError initialized_error = FILE_ERROR_FAILED;
scoped_ptr<ResourceEntry> entry, entry_dontcare;
base::FilePath local_path, local_path_dontcare;
google_apis::test_util::TestGetContentCallback get_content_callback;
FileError completion_error = FILE_ERROR_FAILED;
base::Closure cancel_download = operation_->EnsureFileDownloadedByPath(
file_in_root,
ClientContext(USER_INITIATED),
google_apis::test_util::CreateCopyResultCallback(
&initialized_error, &local_path, &entry),
get_content_callback.callback(),
google_apis::test_util::CreateCopyResultCallback(
&completion_error, &local_path_dontcare, &entry_dontcare));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, initialized_error);
ASSERT_TRUE(entry);
ASSERT_TRUE(!local_path.empty());
EXPECT_FALSE(cancel_download.is_null());
EXPECT_TRUE(get_content_callback.data().empty());
int64 local_file_size = 0;
base::GetFileSize(local_path, &local_file_size);
EXPECT_EQ(entry->file_info().size(), local_file_size);
EXPECT_EQ(FILE_ERROR_OK, completion_error);
}
}
TEST_F(DownloadOperationTest, EnsureFileDownloadedByLocalId_FromCache) {
base::FilePath temp_file;
ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir(), &temp_file));
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
ResourceEntry src_entry;
ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry));
FileError error = FILE_ERROR_FAILED;
base::PostTaskAndReplyWithResult(
blocking_task_runner(),
FROM_HERE,
base::Bind(&internal::FileCache::Store,
base::Unretained(cache()),
GetLocalId(file_in_root),
src_entry.file_specific_info().md5(),
temp_file,
internal::FileCache::FILE_OPERATION_COPY),
google_apis::test_util::CreateCopyResultCallback(&error));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
fake_service()->set_offline(true);
base::FilePath file_path;
scoped_ptr<ResourceEntry> entry;
operation_->EnsureFileDownloadedByLocalId(
GetLocalId(file_in_root),
ClientContext(USER_INITIATED),
GetFileContentInitializedCallback(),
google_apis::GetContentCallback(),
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
EXPECT_FALSE(entry->file_specific_info().is_hosted_document());
}
TEST_F(DownloadOperationTest, EnsureFileDownloadedByPath_DirtyCache) {
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
ResourceEntry src_entry;
ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry));
base::FilePath dirty_file = temp_dir().AppendASCII("dirty.txt");
size_t dirty_size = src_entry.file_info().size() + 10;
google_apis::test_util::WriteStringToFile(dirty_file,
std::string(dirty_size, 'x'));
FileError error = FILE_ERROR_FAILED;
base::PostTaskAndReplyWithResult(
blocking_task_runner(),
FROM_HERE,
base::Bind(&internal::FileCache::Store,
base::Unretained(cache()),
GetLocalId(file_in_root),
std::string(),
dirty_file,
internal::FileCache::FILE_OPERATION_COPY),
google_apis::test_util::CreateCopyResultCallback(&error));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
FileError init_error;
base::FilePath init_path;
scoped_ptr<ResourceEntry> init_entry;
base::FilePath file_path;
scoped_ptr<ResourceEntry> entry;
base::Closure cancel_callback = operation_->EnsureFileDownloadedByPath(
file_in_root,
ClientContext(USER_INITIATED),
google_apis::test_util::CreateCopyResultCallback(
&init_error, &init_path, &init_entry),
google_apis::GetContentCallback(),
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
EXPECT_EQ(static_cast<int64>(dirty_size), init_entry->file_info().size());
EXPECT_EQ(static_cast<int64>(dirty_size), entry->file_info().size());
}
TEST_F(DownloadOperationTest, EnsureFileDownloadedByPath_LocallyCreatedFile) {
base::FilePath file_path(FILE_PATH_LITERAL("drive/root/New File.txt"));
ResourceEntry parent;
ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_path.DirName(), &parent));
ResourceEntry new_file;
new_file.set_title("New File.txt");
new_file.set_parent_local_id(parent.local_id());
FileError error = FILE_ERROR_FAILED;
std::string local_id;
base::PostTaskAndReplyWithResult(
blocking_task_runner(),
FROM_HERE,
base::Bind(&internal::ResourceMetadata::AddEntry,
base::Unretained(metadata()),
new_file,
&local_id),
google_apis::test_util::CreateCopyResultCallback(&error));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
base::FilePath cache_file_path;
scoped_ptr<ResourceEntry> entry;
operation_->EnsureFileDownloadedByPath(
file_path,
ClientContext(USER_INITIATED),
GetFileContentInitializedCallback(),
google_apis::GetContentCallback(),
google_apis::test_util::CreateCopyResultCallback(
&error, &cache_file_path, &entry));
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
int64 cache_file_size = 0;
EXPECT_TRUE(base::GetFileSize(cache_file_path, &cache_file_size));
EXPECT_EQ(static_cast<int64>(0), cache_file_size);
ASSERT_TRUE(entry);
EXPECT_EQ(cache_file_size, entry->file_info().size());
}
TEST_F(DownloadOperationTest, CancelBeforeDownloadStarts) {
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
ResourceEntry src_entry;
ASSERT_EQ(FILE_ERROR_OK, GetLocalResourceEntry(file_in_root, &src_entry));
FileError error = FILE_ERROR_OK;
base::FilePath file_path;
scoped_ptr<ResourceEntry> entry;
base::Closure cancel_closure = operation_->EnsureFileDownloadedByLocalId(
GetLocalId(file_in_root),
ClientContext(USER_INITIATED),
GetFileContentInitializedCallback(),
google_apis::GetContentCallback(),
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
ASSERT_FALSE(cancel_closure.is_null());
cancel_closure.Run();
test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_ABORT, error);
}
}
}