drive: Add DownloadOperationTest

Move DownloadOperation related tests from FileSystemTest to newly added DownloadedOperationTest.
Add a new method FakeFreeDiskSpaceGetter::Reset().

BUG=244344
TEST=unit_tests

Review URL: https://chromiumcodereview.appspot.com/16189010

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@203134 0039d316-1c4b-4281-b951-d872f2087c98
parent e48cf4fc
......@@ -12,6 +12,10 @@ FakeFreeDiskSpaceGetter::FakeFreeDiskSpaceGetter() {
FakeFreeDiskSpaceGetter::~FakeFreeDiskSpaceGetter() {
}
void FakeFreeDiskSpaceGetter::Reset() {
fake_values_.clear();
}
int64 FakeFreeDiskSpaceGetter::AmountOfFreeDiskSpace() {
if (fake_values_.empty())
return 0;
......
......@@ -19,6 +19,9 @@ class FakeFreeDiskSpaceGetter : public internal::FreeDiskSpaceGetterInterface {
FakeFreeDiskSpaceGetter();
virtual ~FakeFreeDiskSpaceGetter();
// Resets this instance.
void Reset();
// If this function is not called, AmountOfFreeDiskSpace() will return 0
// repeatedly.
//
......
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/drive/file_system/download_operation.h"
#include "base/file_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/google_apis/fake_drive_service.h"
#include "chrome/browser/google_apis/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace drive {
namespace file_system {
class DownloadOperationTest : public OperationTestBase {
protected:
virtual void SetUp() {
OperationTestBase::SetUp();
operation_.reset(new DownloadOperation(
blocking_task_runner(), observer(), scheduler(), metadata(), cache()));
}
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();
// Pretend we have enough space.
fake_free_disk_space_getter()->Reset();
fake_free_disk_space_getter()->set_fake_free_disk_space(
file_size + internal::kMinFreeSpace);
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));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
EXPECT_FALSE(entry->file_specific_info().is_hosted_document());
// The transfered file is cached and the change of "offline available"
// attribute is notified.
EXPECT_EQ(1U, observer()->get_changed_paths().size());
EXPECT_EQ(1U, observer()->get_changed_paths().count(file_in_root.DirName()));
// Verify that readable permission is set.
int permission = 0;
EXPECT_TRUE(file_util::GetPosixFilePermissions(file_path, &permission));
EXPECT_EQ(file_util::FILE_PERMISSION_READ_BY_USER |
file_util::FILE_PERMISSION_WRITE_BY_USER |
file_util::FILE_PERMISSION_READ_BY_GROUP |
file_util::FILE_PERMISSION_READ_BY_OTHERS, permission);
}
TEST_F(DownloadOperationTest,
EnsureFileDownloadedByPath_FromServer_NoSpaceAtAll) {
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
// Pretend we have no space at all.
fake_free_disk_space_getter()->Reset();
fake_free_disk_space_getter()->set_fake_free_disk_space(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));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_NO_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();
// Pretend we have no space first (checked before downloading a file),
// but then start reporting we have space. This is to emulate that
// the disk space was freed up by removing temporary files.
fake_free_disk_space_getter()->Reset();
fake_free_disk_space_getter()->set_fake_free_disk_space(
file_size + internal::kMinFreeSpace);
fake_free_disk_space_getter()->set_fake_free_disk_space(0);
fake_free_disk_space_getter()->set_fake_free_disk_space(
file_size + internal::kMinFreeSpace);
fake_free_disk_space_getter()->set_fake_free_disk_space(
file_size + internal::kMinFreeSpace);
// Store something of the file size in the temporary cache directory.
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;
cache()->StoreOnUIThread(
"<resource_id>", "<md5>", tmp_file,
internal::FileCache::FILE_OPERATION_COPY,
google_apis::test_util::CreateCopyResultCallback(&error));
google_apis::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));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
EXPECT_FALSE(entry->file_specific_info().is_hosted_document());
// The transfered file is cached and the change of "offline available"
// attribute is notified.
EXPECT_EQ(1U, observer()->get_changed_paths().size());
EXPECT_EQ(1U, observer()->get_changed_paths().count(file_in_root.DirName()));
// The cache entry should be removed in order to free up space.
FileCacheEntry cache_entry;
bool result = true;
cache()->GetCacheEntryOnUIThread(
"<resource_id>", "<md5>",
google_apis::test_util::CreateCopyResultCallback(&result,
&cache_entry));
google_apis::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();
// Pretend we have enough space first (checked before downloading a file),
// but then start reporting we have not enough space. This is to emulate that
// the disk space becomes full after the file is downloaded for some reason
// (ex. the actual file was larger than the expected size).
fake_free_disk_space_getter()->Reset();
fake_free_disk_space_getter()->set_fake_free_disk_space(
file_size + internal::kMinFreeSpace);
fake_free_disk_space_getter()->set_fake_free_disk_space(
internal::kMinFreeSpace - 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));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_NO_SPACE, error);
}
TEST_F(DownloadOperationTest, EnsureFileDownloadedByPath_FromCache) {
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));
// Store something as cached version of this file.
FileError error = FILE_ERROR_OK;
cache()->StoreOnUIThread(
src_entry.resource_id(),
src_entry.file_specific_info().file_md5(),
google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"),
internal::FileCache::FILE_OPERATION_COPY,
google_apis::test_util::CreateCopyResultCallback(&error));
google_apis::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));
google_apis::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));
google_apis::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));
}
TEST_F(DownloadOperationTest, EnsureFileDownloadedByResourceId) {
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));
std::string resource_id = src_entry.resource_id();
FileError error = FILE_ERROR_OK;
base::FilePath file_path;
scoped_ptr<ResourceEntry> entry;
operation_->EnsureFileDownloadedByResourceId(
resource_id,
ClientContext(USER_INITIATED),
GetFileContentInitializedCallback(),
google_apis::GetContentCallback(),
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
EXPECT_FALSE(entry->file_specific_info().is_hosted_document());
// The transfered file is cached and the change of "offline available"
// attribute is notified.
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;
base::Closure cancel_download;
google_apis::test_util::TestGetContentCallback get_content_callback;
FileError completion_error = FILE_ERROR_FAILED;
operation_->EnsureFileDownloadedByPath(
file_in_root,
ClientContext(USER_INITIATED),
google_apis::test_util::CreateCopyResultCallback(
&initialized_error, &entry, &local_path, &cancel_download),
get_content_callback.callback(),
google_apis::test_util::CreateCopyResultCallback(
&completion_error, &local_path_dontcare, &entry_dontcare));
google_apis::test_util::RunBlockingPoolTask();
// For the first time, file is downloaded from the remote server.
// In this case, |local_path| is empty while |cancel_download| is not.
EXPECT_EQ(FILE_ERROR_OK, initialized_error);
ASSERT_TRUE(entry);
ASSERT_TRUE(local_path.empty());
EXPECT_TRUE(!cancel_download.is_null());
// Content is available through the second callback argument.
EXPECT_EQ(static_cast<size_t>(entry->file_info().size()),
get_content_callback.GetConcatenatedData().size());
EXPECT_EQ(FILE_ERROR_OK, completion_error);
// The transfered file is cached and the change of "offline available"
// attribute is notified.
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;
base::Closure cancel_download;
google_apis::test_util::TestGetContentCallback get_content_callback;
FileError completion_error = FILE_ERROR_FAILED;
operation_->EnsureFileDownloadedByPath(
file_in_root,
ClientContext(USER_INITIATED),
google_apis::test_util::CreateCopyResultCallback(
&initialized_error, &entry, &local_path, &cancel_download),
get_content_callback.callback(),
google_apis::test_util::CreateCopyResultCallback(
&completion_error, &local_path_dontcare, &entry_dontcare));
google_apis::test_util::RunBlockingPoolTask();
// Try second download. In this case, the file should be cached, so
// |local_path| should not be empty while |cancel_download| is empty.
EXPECT_EQ(FILE_ERROR_OK, initialized_error);
ASSERT_TRUE(entry);
ASSERT_TRUE(!local_path.empty());
EXPECT_TRUE(cancel_download.is_null());
// The content is available from the cache file.
EXPECT_TRUE(get_content_callback.data().empty());
int64 local_file_size = 0;
file_util::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, EnsureFileDownloadedByResourceId_FromCache) {
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));
// Store something as cached version of this file.
FileError error = FILE_ERROR_FAILED;
cache()->StoreOnUIThread(
src_entry.resource_id(),
src_entry.file_specific_info().file_md5(),
google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"),
internal::FileCache::FILE_OPERATION_COPY,
google_apis::test_util::CreateCopyResultCallback(&error));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
// The file is obtained from the cache.
// Hence the downloading should work even if the drive service is offline.
fake_service()->set_offline(true);
std::string resource_id = src_entry.resource_id();
base::FilePath file_path;
scoped_ptr<ResourceEntry> entry;
operation_->EnsureFileDownloadedByResourceId(
resource_id,
ClientContext(USER_INITIATED),
GetFileContentInitializedCallback(),
google_apis::GetContentCallback(),
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
EXPECT_FALSE(entry->file_specific_info().is_hosted_document());
}
} // namespace file_system
} // namespace drive
......@@ -86,6 +86,9 @@ class OperationTestBase : public testing::Test {
return blocking_task_runner_;
}
internal::ResourceMetadata* metadata() { return metadata_.get(); }
FakeFreeDiskSpaceGetter* fake_free_disk_space_getter() {
return fake_free_disk_space_getter_.get();
}
internal::FileCache* cache() { return cache_.get(); }
private:
......
......@@ -11,7 +11,6 @@
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/files/scoped_temp_dir.h"
#include "base/json/json_file_value_serializer.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/stringprintf.h"
......@@ -217,13 +216,6 @@ class FileSystemTest : public testing::Test {
return result;
}
// Returns true if the cache entry exists for the given resource ID and MD5.
bool CacheEntryExists(const std::string& resource_id,
const std::string& md5) {
FileCacheEntry cache_entry;
return GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry);
}
// Flag for specifying the timestamp of the test filesystem cache.
enum SetUpTestFileSystemParam {
USE_OLD_TIMESTAMP,
......@@ -348,25 +340,6 @@ class FileSystemTest : public testing::Test {
return true;
}
// Verifies that |file_path| is a valid JSON file for the hosted document
// associated with |entry| (i.e. |url| and |resource_id| match).
void VerifyHostedDocumentJSONFile(const ResourceEntry& entry,
const base::FilePath& file_path) {
std::string error;
JSONFileValueSerializer serializer(file_path);
scoped_ptr<Value> value(serializer.Deserialize(NULL, &error));
ASSERT_TRUE(value) << "Parse error " << file_path.value() << ": " << error;
DictionaryValue* dict_value = NULL;
ASSERT_TRUE(value->GetAsDictionary(&dict_value));
std::string alternate_url, resource_id;
EXPECT_TRUE(dict_value->GetString("url", &alternate_url));
EXPECT_TRUE(dict_value->GetString("resource_id", &resource_id));
EXPECT_EQ(entry.file_specific_info().alternate_url(), alternate_url);
EXPECT_EQ(entry.resource_id(), resource_id);
}
base::MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_;
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
......@@ -1072,7 +1045,10 @@ TEST_F(FileSystemTest, TransferFileFromRemoteToLocal_HostedDocument) {
scoped_ptr<ResourceEntry> entry = GetResourceEntryByPathSync(
remote_src_file_path);
ASSERT_TRUE(entry);
VerifyHostedDocumentJSONFile(*entry, local_dest_file_path);
EXPECT_EQ(GURL(entry->file_specific_info().alternate_url()),
util::ReadUrlFromGDocFile(local_dest_file_path));
EXPECT_EQ(entry->resource_id(),
util::ReadResourceIdFromGDocFile(local_dest_file_path));
}
TEST_F(FileSystemTest, CopyNotExistingFile) {
......@@ -1209,345 +1185,6 @@ TEST_F(FileSystemTest, PinAndUnpin) {
EXPECT_EQ(FILE_ERROR_OK, error);
}
TEST_F(FileSystemTest, GetFileByPath_FromGData_EnoughSpace) {
ASSERT_TRUE(LoadRootFeedDocument());
// The transfered file is cached and the change of "offline available"
// attribute is notified.
EXPECT_CALL(*mock_directory_observer_, OnDirectoryChanged(
Eq(base::FilePath(FILE_PATH_LITERAL("drive/root"))))).Times(1);
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(file_in_root));
const int64 file_size = entry->file_info().size();
// Pretend we have enough space.
fake_free_disk_space_getter_->set_fake_free_disk_space(
file_size + internal::kMinFreeSpace);
FileError error = FILE_ERROR_FAILED;
base::FilePath file_path;
entry.reset();
file_system_->GetFileByPath(file_in_root,
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
EXPECT_FALSE(entry->file_specific_info().is_hosted_document());
// Verify that readable permission is set.
int permission = 0;
EXPECT_TRUE(file_util::GetPosixFilePermissions(file_path, &permission));
EXPECT_EQ(file_util::FILE_PERMISSION_READ_BY_USER |
file_util::FILE_PERMISSION_WRITE_BY_USER |
file_util::FILE_PERMISSION_READ_BY_GROUP |
file_util::FILE_PERMISSION_READ_BY_OTHERS, permission);
}
TEST_F(FileSystemTest, GetFileByPath_FromGData_NoSpaceAtAll) {
ASSERT_TRUE(LoadRootFeedDocument());
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
// Pretend we have no space at all.
fake_free_disk_space_getter_->set_fake_free_disk_space(0);
FileError error = FILE_ERROR_OK;
base::FilePath file_path;
scoped_ptr<ResourceEntry> entry;
file_system_->GetFileByPath(file_in_root,
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_NO_SPACE, error);
}
TEST_F(FileSystemTest, GetFileByPath_FromGData_NoEnoughSpaceButCanFreeUp) {
ASSERT_TRUE(LoadRootFeedDocument());
// The transfered file is cached and the change of "offline available"
// attribute is notified.
EXPECT_CALL(*mock_directory_observer_, OnDirectoryChanged(
Eq(base::FilePath(FILE_PATH_LITERAL("drive/root"))))).Times(1);
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(file_in_root));
const int64 file_size = entry->file_info().size();
// Pretend we have no space first (checked before downloading a file),
// but then start reporting we have space. This is to emulate that
// the disk space was freed up by removing temporary files.
fake_free_disk_space_getter_->set_fake_free_disk_space(
file_size + internal::kMinFreeSpace);
fake_free_disk_space_getter_->set_fake_free_disk_space(0);
fake_free_disk_space_getter_->set_fake_free_disk_space(
file_size + internal::kMinFreeSpace);
fake_free_disk_space_getter_->set_fake_free_disk_space(
file_size + internal::kMinFreeSpace);
// Store something of the file size in the temporary cache directory.
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;
cache_->StoreOnUIThread(
"<resource_id>", "<md5>", tmp_file,
internal::FileCache::FILE_OPERATION_COPY,
google_apis::test_util::CreateCopyResultCallback(&error));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(CacheEntryExists("<resource_id>", "<md5>"));
base::FilePath file_path;
entry.reset();
file_system_->GetFileByPath(file_in_root,
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
EXPECT_FALSE(entry->file_specific_info().is_hosted_document());
// The cache entry should be removed in order to free up space.
ASSERT_FALSE(CacheEntryExists("<resource_id>", "<md5>"));
}
TEST_F(FileSystemTest, GetFileByPath_FromGData_EnoughSpaceButBecomeFull) {
ASSERT_TRUE(LoadRootFeedDocument());
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(file_in_root));
const int64 file_size = entry->file_info().size();
// Pretend we have enough space first (checked before downloading a file),
// but then start reporting we have not enough space. This is to emulate that
// the disk space becomes full after the file is downloaded for some reason
// (ex. the actual file was larger than the expected size).
fake_free_disk_space_getter_->set_fake_free_disk_space(
file_size + internal::kMinFreeSpace);
fake_free_disk_space_getter_->set_fake_free_disk_space(
internal::kMinFreeSpace - 1);
fake_free_disk_space_getter_->set_fake_free_disk_space(
internal::kMinFreeSpace - 1);
FileError error = FILE_ERROR_OK;
base::FilePath file_path;
entry.reset();
file_system_->GetFileByPath(file_in_root,
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_NO_SPACE, error);
}
TEST_F(FileSystemTest, GetFileByPath_FromCache) {
fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace);
ASSERT_TRUE(LoadRootFeedDocument());
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(file_in_root));
// Store something as cached version of this file.
FileError error = FILE_ERROR_OK;
cache_->StoreOnUIThread(
entry->resource_id(),
entry->file_specific_info().file_md5(),
google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"),
internal::FileCache::FILE_OPERATION_COPY,
google_apis::test_util::CreateCopyResultCallback(&error));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
base::FilePath file_path;
entry.reset();
file_system_->GetFileByPath(file_in_root,
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
EXPECT_FALSE(entry->file_specific_info().is_hosted_document());
}
TEST_F(FileSystemTest, GetFileByPath_HostedDocument) {
ASSERT_TRUE(LoadRootFeedDocument());
base::FilePath file_in_root(FILE_PATH_LITERAL(
"drive/root/Document 1 excludeDir-test.gdoc"));
scoped_ptr<ResourceEntry> src_entry =
GetResourceEntryByPathSync(file_in_root);
ASSERT_TRUE(src_entry);
FileError error = FILE_ERROR_FAILED;
base::FilePath file_path;
scoped_ptr<ResourceEntry> entry;
file_system_->GetFileByPath(file_in_root,
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry));
google_apis::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());
ASSERT_TRUE(src_entry);
VerifyHostedDocumentJSONFile(*src_entry, file_path);
}
TEST_F(FileSystemTest, GetFileByResourceId) {
fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace);
// The transfered file is cached and the change of "offline available"
// attribute is notified.
EXPECT_CALL(*mock_directory_observer_, OnDirectoryChanged(
Eq(base::FilePath(FILE_PATH_LITERAL("drive/root"))))).Times(1);
ASSERT_TRUE(LoadRootFeedDocument());
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(file_in_root));
std::string resource_id = entry->resource_id();
FileError error = FILE_ERROR_OK;
base::FilePath file_path;
entry.reset();
file_system_->GetFileByResourceId(
resource_id,
ClientContext(USER_INITIATED),
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry),
google_apis::GetContentCallback());
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
EXPECT_FALSE(entry->file_specific_info().is_hosted_document());
}
TEST_F(FileSystemTest, GetFileContentByPath) {
fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace);
// The transfered file is cached and the change of "offline available"
// attribute is notified.
EXPECT_CALL(*mock_directory_observer_, OnDirectoryChanged(
Eq(base::FilePath(FILE_PATH_LITERAL("drive/root"))))).Times(1);
ASSERT_TRUE(LoadRootFeedDocument());
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
{
FileError initialized_error = FILE_ERROR_FAILED;
scoped_ptr<ResourceEntry> entry;
base::FilePath local_path;
base::Closure cancel_download;
google_apis::test_util::TestGetContentCallback get_content_callback;
FileError completion_error = FILE_ERROR_FAILED;
file_system_->GetFileContentByPath(
file_in_root,
google_apis::test_util::CreateCopyResultCallback(
&initialized_error, &entry, &local_path, &cancel_download),
get_content_callback.callback(),
google_apis::test_util::CreateCopyResultCallback(&completion_error));
google_apis::test_util::RunBlockingPoolTask();
// For the first time, file is downloaded from the remote server.
// In this case, |local_path| is empty while |cancel_download| is not.
EXPECT_EQ(FILE_ERROR_OK, initialized_error);
ASSERT_TRUE(entry);
ASSERT_TRUE(local_path.empty());
EXPECT_TRUE(!cancel_download.is_null());
// Content is available through the second callback argument.
EXPECT_EQ(static_cast<size_t>(entry->file_info().size()),
get_content_callback.GetConcatenatedData().size());
EXPECT_EQ(FILE_ERROR_OK, completion_error);
}
{
FileError initialized_error = FILE_ERROR_FAILED;
scoped_ptr<ResourceEntry> entry;
base::FilePath local_path;
base::Closure cancel_download;
google_apis::test_util::TestGetContentCallback get_content_callback;
FileError completion_error = FILE_ERROR_FAILED;
file_system_->GetFileContentByPath(
file_in_root,
google_apis::test_util::CreateCopyResultCallback(
&initialized_error, &entry, &local_path, &cancel_download),
get_content_callback.callback(),
google_apis::test_util::CreateCopyResultCallback(&completion_error));
google_apis::test_util::RunBlockingPoolTask();
// Try second download. In this case, the file should be cached, so
// |local_path| should not be empty while |cancel_download| is empty.
EXPECT_EQ(FILE_ERROR_OK, initialized_error);
ASSERT_TRUE(entry);
ASSERT_TRUE(!local_path.empty());
EXPECT_TRUE(cancel_download.is_null());
// The content is available from the cache file.
EXPECT_TRUE(get_content_callback.data().empty());
int64 local_file_size = 0;
file_util::GetFileSize(local_path, &local_file_size);
EXPECT_EQ(entry->file_info().size(), local_file_size);
EXPECT_EQ(FILE_ERROR_OK, completion_error);
}
}
TEST_F(FileSystemTest, GetFileByResourceId_FromCache) {
fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace);
ASSERT_TRUE(LoadRootFeedDocument());
base::FilePath file_in_root(FILE_PATH_LITERAL("drive/root/File 1.txt"));
scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(file_in_root));
// Store something as cached version of this file.
FileError error = FILE_ERROR_FAILED;
cache_->StoreOnUIThread(
entry->resource_id(),
entry->file_specific_info().file_md5(),
google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"),
internal::FileCache::FILE_OPERATION_COPY,
google_apis::test_util::CreateCopyResultCallback(&error));
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
// The file is obtained from the cache.
// Hence the downloading should work even if the drive service is offline.
fake_drive_service_->set_offline(true);
std::string resource_id = entry->resource_id();
base::FilePath file_path;
entry.reset();
file_system_->GetFileByResourceId(
resource_id,
ClientContext(USER_INITIATED),
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &entry),
google_apis::GetContentCallback());
google_apis::test_util::RunBlockingPoolTask();
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
EXPECT_FALSE(entry->file_specific_info().is_hosted_document());
}
TEST_F(FileSystemTest, GetAvailableSpace) {
FileError error = FILE_ERROR_OK;
int64 bytes_total;
......
......@@ -584,6 +584,7 @@
'browser/chromeos/drive/file_system_unittest.cc',
'browser/chromeos/drive/file_system/create_directory_operation_unittest.cc',
'browser/chromeos/drive/file_system/create_file_operation_unittest.cc',
'browser/chromeos/drive/file_system/download_operation_unittest.cc',
'browser/chromeos/drive/file_system/move_operation_unittest.cc',
'browser/chromeos/drive/file_system/operation_test_base.cc',
'browser/chromeos/drive/file_system/operation_test_base.h',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment