Commit 4fce48c6 authored by kinaba@chromium.org's avatar kinaba@chromium.org

drive: Move some of FileSystemTest tests to lower layers.

* Base name duplication is handled and should be tested in ResourceMetadata.
* OpenFile's behavior can be tested in OpenFileOperation.

Along the way did some clean up.

BUG=none

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@221659 0039d316-1c4b-4281-b951-d872f2087c98
parent fde97433
......@@ -116,6 +116,9 @@ TEST_F(OpenFileOperationTest, CreateNonExistingFile) {
&error, &file_path, &close_callback));
test_util::RunBlockingPoolTask();
EXPECT_EQ(1U, observer()->get_changed_paths().size());
EXPECT_TRUE(observer()->get_changed_paths().count(file_in_root.DirName()));
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(base::PathExists(file_path));
int64 local_file_size;
......@@ -147,6 +150,10 @@ TEST_F(OpenFileOperationTest, OpenOrCreateExistingFile) {
&error, &file_path, &close_callback));
test_util::RunBlockingPoolTask();
// Notified because 'available offline' status of the existing file changes.
EXPECT_EQ(1U, observer()->get_changed_paths().size());
EXPECT_TRUE(observer()->get_changed_paths().count(file_in_root.DirName()));
EXPECT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(base::PathExists(file_path));
int64 local_file_size;
......@@ -158,6 +165,16 @@ TEST_F(OpenFileOperationTest, OpenOrCreateExistingFile) {
EXPECT_EQ(
1U,
observer()->upload_needed_local_ids().count(src_entry.resource_id()));
bool success = false;
FileCacheEntry cache_entry;
cache()->GetCacheEntryOnUIThread(
src_entry.resource_id(),
google_apis::test_util::CreateCopyResultCallback(&success, &cache_entry));
test_util::RunBlockingPoolTask();
EXPECT_TRUE(success);
EXPECT_TRUE(cache_entry.is_present());
EXPECT_TRUE(cache_entry.is_dirty());
}
TEST_F(OpenFileOperationTest, OpenOrCreateNonExistingFile) {
......
......@@ -83,9 +83,6 @@ class FileSystemTest : public testing::Test {
pref_service_.reset(new TestingPrefServiceSimple);
test_util::RegisterDrivePrefs(pref_service_->registry());
fake_network_change_notifier_.reset(
new test_util::FakeNetworkChangeNotifier);
fake_drive_service_.reset(new FakeDriveService);
fake_drive_service_->LoadResourceListForWapi(
"gdata/root_feed.json");
......@@ -172,8 +169,7 @@ class FileSystemTest : public testing::Test {
scoped_ptr<ResourceEntryVector> entries;
file_system_->ReadDirectoryByPath(
file_path,
google_apis::test_util::CreateCopyResultCallback(
&error, &entries));
google_apis::test_util::CreateCopyResultCallback(&error, &entries));
test_util::RunBlockingPoolTask();
return entries.Pass();
......@@ -184,16 +180,6 @@ class FileSystemTest : public testing::Test {
return GetResourceEntryByPathSync(file_path);
}
// Gets the resource ID of |file_path|. Returns an empty string if not found.
std::string GetResourceIdByPath(const base::FilePath& file_path) {
scoped_ptr<ResourceEntry> entry =
GetResourceEntryByPathSync(file_path);
if (entry)
return entry->resource_id();
else
return "";
}
// Flag for specifying the timestamp of the test filesystem cache.
enum SetUpTestFileSystemParam {
USE_OLD_TIMESTAMP,
......@@ -289,8 +275,6 @@ class FileSystemTest : public testing::Test {
// We don't use TestingProfile::GetPrefs() in favor of having less
// dependencies to Profile in general.
scoped_ptr<TestingPrefServiceSimple> pref_service_;
scoped_ptr<test_util::FakeNetworkChangeNotifier>
fake_network_change_notifier_;
scoped_ptr<FakeDriveService> fake_drive_service_;
scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_;
......@@ -404,48 +388,6 @@ TEST_F(FileSystemTest, GetNonExistingFile) {
EXPECT_FALSE(entry);
}
TEST_F(FileSystemTest, GetEncodedFileNames) {
const base::FilePath kFilePath1(
FILE_PATH_LITERAL("drive/root/Slash / in file 1.txt"));
scoped_ptr<ResourceEntry> entry = GetResourceEntryByPathSync(kFilePath1);
ASSERT_FALSE(entry);
const base::FilePath kFilePath2 = base::FilePath::FromUTF8Unsafe(
"drive/root/Slash \xE2\x88\x95 in file 1.txt");
entry = GetResourceEntryByPathSync(kFilePath2);
ASSERT_TRUE(entry);
EXPECT_EQ("file:slash_file_resource_id", entry->resource_id());
const base::FilePath kFilePath3 = base::FilePath::FromUTF8Unsafe(
"drive/root/Slash \xE2\x88\x95 in directory/Slash SubDir File.txt");
entry = GetResourceEntryByPathSync(kFilePath3);
ASSERT_TRUE(entry);
EXPECT_EQ("file:slash_subdir_file", entry->resource_id());
}
TEST_F(FileSystemTest, GetDuplicateNames) {
const base::FilePath kFilePath1(
FILE_PATH_LITERAL("drive/root/Duplicate Name.txt"));
scoped_ptr<ResourceEntry> entry = GetResourceEntryByPathSync(kFilePath1);
ASSERT_TRUE(entry);
const std::string resource_id1 = entry->resource_id();
const base::FilePath kFilePath2(
FILE_PATH_LITERAL("drive/root/Duplicate Name (1).txt"));
entry = GetResourceEntryByPathSync(kFilePath2);
ASSERT_TRUE(entry);
const std::string resource_id2 = entry->resource_id();
// The entries are de-duped non-deterministically, so we shouldn't rely on the
// names matching specific resource ids.
const std::string file3_resource_id = "file:3_file_resource_id";
const std::string file4_resource_id = "file:4_file_resource_id";
EXPECT_TRUE(file3_resource_id == resource_id1 ||
file3_resource_id == resource_id2);
EXPECT_TRUE(file4_resource_id == resource_id1 ||
file4_resource_id == resource_id2);
}
TEST_F(FileSystemTest, GetExistingDirectory) {
const base::FilePath kFilePath(FILE_PATH_LITERAL("drive/root/Directory 1"));
scoped_ptr<ResourceEntry> entry = GetResourceEntryByPathSync(kFilePath);
......@@ -541,8 +483,6 @@ TEST_F(FileSystemTest, LoadFileSystemFromCacheWhileOffline) {
// Make GetResourceList fail for simulating offline situation. This will
// leave the file system "loaded from cache, but not synced with server"
// state.
fake_network_change_notifier_->SetConnectionType(
net::NetworkChangeNotifier::CONNECTION_NONE);
fake_drive_service_->set_offline(true);
// Load the root.
......@@ -559,19 +499,17 @@ TEST_F(FileSystemTest, LoadFileSystemFromCacheWhileOffline) {
FILE_PATH_LITERAL("drive/root/File1"))));
EXPECT_TRUE(EntryExists(base::FilePath(
FILE_PATH_LITERAL("drive/root/Dir1"))));
EXPECT_TRUE(
EntryExists(base::FilePath(FILE_PATH_LITERAL("drive/root/Dir1/File2"))));
EXPECT_TRUE(EntryExists(base::FilePath(
FILE_PATH_LITERAL("drive/root/Dir1/File2"))));
EXPECT_TRUE(EntryExists(base::FilePath(
FILE_PATH_LITERAL("drive/root/Dir1/SubDir2"))));
EXPECT_TRUE(EntryExists(
base::FilePath(FILE_PATH_LITERAL("drive/root/Dir1/SubDir2/File3"))));
EXPECT_TRUE(EntryExists(base::FilePath(
FILE_PATH_LITERAL("drive/root/Dir1/SubDir2/File3"))));
// Since the file system has at least succeeded to load cached snapshot,
// the file system should be able to start periodic refresh.
// To test it, call CheckForUpdates and verify it does try to check
// updates, which will cause directory changes.
fake_network_change_notifier_->SetConnectionType(
net::NetworkChangeNotifier::CONNECTION_WIFI);
fake_drive_service_->set_offline(false);
file_system_->CheckForUpdates();
......@@ -716,83 +654,6 @@ TEST_F(FileSystemTest, GetAvailableSpace) {
EXPECT_EQ(GG_LONGLONG(9876543210), bytes_total);
}
TEST_F(FileSystemTest, OpenAndCloseFile) {
ASSERT_TRUE(LoadFullResourceList());
const base::FilePath kFileInRoot(FILE_PATH_LITERAL("drive/root/File 1.txt"));
scoped_ptr<ResourceEntry> entry(GetResourceEntryByPathSync(kFileInRoot));
const std::string& file_resource_id = entry->resource_id();
// Open kFileInRoot ("drive/root/File 1.txt").
FileError error = FILE_ERROR_FAILED;
base::FilePath file_path;
base::Closure close_callback;
file_system_->OpenFile(
kFileInRoot,
OPEN_FILE,
std::string(), // mime_type
google_apis::test_util::CreateCopyResultCallback(
&error, &file_path, &close_callback));
test_util::RunBlockingPoolTask();
const base::FilePath opened_file_path = file_path;
// Verify that the file was properly opened.
EXPECT_EQ(FILE_ERROR_OK, error);
// The opened file is downloaded, which means the file is available
// offline. The directory change should be notified so Files.app can change
// the offline availability status of the file.
ASSERT_EQ(1u, mock_directory_observer_->changed_directories().size());
EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("drive/root")),
mock_directory_observer_->changed_directories()[0]);
// Verify that the file contents match the expected contents.
const std::string kExpectedContent = "This is some test content.";
std::string cache_file_data;
EXPECT_TRUE(base::ReadFileToString(opened_file_path, &cache_file_data));
EXPECT_EQ(kExpectedContent, cache_file_data);
FileCacheEntry cache_entry;
EXPECT_TRUE(cache_->GetCacheEntry(file_resource_id, &cache_entry));
EXPECT_TRUE(cache_entry.is_present());
EXPECT_TRUE(cache_entry.is_dirty());
base::FilePath cache_file_path;
EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(file_resource_id, &cache_file_path));
EXPECT_EQ(cache_file_path, opened_file_path);
// Write a new content.
const std::string kNewContent = kExpectedContent + kExpectedContent;
EXPECT_TRUE(google_apis::test_util::WriteStringToFile(cache_file_path,
kNewContent));
// Close kFileInRoot ("drive/root/File 1.txt").
ASSERT_FALSE(close_callback.is_null());
close_callback.Run();
test_util::RunBlockingPoolTask();
// Verify that the file was properly closed.
EXPECT_EQ(FILE_ERROR_OK, error);
// Verify that the file was synced as expected.
google_apis::GDataErrorCode gdata_error = google_apis::GDATA_FILE_ERROR;
scoped_ptr<google_apis::ResourceEntry> gdata_entry;
fake_drive_service_->GetResourceEntry(
file_resource_id,
google_apis::test_util::CreateCopyResultCallback(
&gdata_error, &gdata_entry));
test_util::RunBlockingPoolTask();
EXPECT_EQ(gdata_error, google_apis::HTTP_SUCCESS);
ASSERT_TRUE(gdata_entry);
EXPECT_EQ(static_cast<int>(kNewContent.size()), gdata_entry->file_size());
// The modified file is uploaded. The directory change should be notified
// so Files.app can show new metadata of the modified file.
ASSERT_EQ(2u, mock_directory_observer_->changed_directories().size());
EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("drive/root")),
mock_directory_observer_->changed_directories()[1]);
}
TEST_F(FileSystemTest, MarkCacheFileAsMountedAndUnmounted) {
ASSERT_TRUE(LoadFullResourceList());
......
......@@ -41,24 +41,35 @@ std::vector<std::string> GetSortedBaseNames(
return base_names;
}
// Creates a ResourceEntry for a directory.
ResourceEntry CreateDirectoryEntry(const std::string& title,
const std::string& parent_local_id) {
// Creates a ResourceEntry for a directory with explicitly set resource_id.
ResourceEntry CreateDirectoryEntryWithResourceId(
const std::string& title,
const std::string& resource_id,
const std::string& parent_local_id) {
ResourceEntry entry;
entry.set_title(title);
entry.set_resource_id("id:" + title);
entry.set_resource_id(resource_id);
entry.set_parent_local_id(parent_local_id);
entry.mutable_file_info()->set_is_directory(true);
entry.mutable_directory_specific_info()->set_changestamp(kTestChangestamp);
return entry;
}
// Creates a ResourceEntry for a file.
ResourceEntry CreateFileEntry(const std::string& title,
const std::string& parent_local_id) {
// Creates a ResourceEntry for a directory.
ResourceEntry CreateDirectoryEntry(const std::string& title,
const std::string& parent_local_id) {
return CreateDirectoryEntryWithResourceId(
title, "id:" + title, parent_local_id);
}
// Creates a ResourceEntry for a file with explicitly set resource_id.
ResourceEntry CreateFileEntryWithResourceId(
const std::string& title,
const std::string& resource_id,
const std::string& parent_local_id) {
ResourceEntry entry;
entry.set_title(title);
entry.set_resource_id("id:" + title);
entry.set_resource_id(resource_id);
entry.set_parent_local_id(parent_local_id);
entry.mutable_file_info()->set_is_directory(false);
entry.mutable_file_info()->set_size(1024);
......@@ -66,6 +77,12 @@ ResourceEntry CreateFileEntry(const std::string& title,
return entry;
}
// Creates a ResourceEntry for a file.
ResourceEntry CreateFileEntry(const std::string& title,
const std::string& parent_local_id) {
return CreateFileEntryWithResourceId(title, "id:" + title, parent_local_id);
}
// Creates the following files/directories
// drive/root/dir1/
// drive/root/dir2/
......@@ -651,5 +668,99 @@ TEST_F(ResourceMetadataTest, Iterate) {
EXPECT_EQ(6, directory_count);
}
TEST_F(ResourceMetadataTest, DuplicatedNames) {
ResourceEntry entry;
// When multiple entries with the same title are added in a single directory,
// their base_names are de-duped.
// - drive/root/foo
// - drive/root/foo (1)
std::string dir_id_0;
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry(
CreateDirectoryEntryWithResourceId(
"foo", "foo0", kTestRootResourceId), &dir_id_0));
std::string dir_id_1;
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry(
CreateDirectoryEntryWithResourceId(
"foo", "foo1", kTestRootResourceId), &dir_id_1));
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryById(
dir_id_0, &entry));
EXPECT_EQ("foo", entry.base_name());
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryById(
dir_id_1, &entry));
EXPECT_EQ("foo (1)", entry.base_name());
// - drive/root/foo/bar.txt
// - drive/root/foo/bar (1).txt
// - drive/root/foo/bar (2).txt
std::string file_id_0;
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry(
CreateFileEntryWithResourceId(
"bar.txt", "bar0", dir_id_0), &file_id_0));
std::string file_id_1;
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry(
CreateFileEntryWithResourceId(
"bar.txt", "bar1", dir_id_0), &file_id_1));
std::string file_id_2;
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry(
CreateFileEntryWithResourceId(
"bar.txt", "bar2", dir_id_0), &file_id_2));
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryById(
file_id_0, &entry));
EXPECT_EQ("bar.txt", entry.base_name());
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryById(
file_id_1, &entry));
EXPECT_EQ("bar (1).txt", entry.base_name());
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryById(
file_id_2, &entry));
EXPECT_EQ("bar (2).txt", entry.base_name());
// Same name but different parent. No renaming.
// - drive/root/foo (1)/bar.txt
std::string file_id_3;
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry(
CreateFileEntryWithResourceId(
"bar.txt", "bar3", dir_id_1), &file_id_3));
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryById(
file_id_3, &entry));
EXPECT_EQ("bar.txt", entry.base_name());
// Checks that the entries can be looked up by the de-duped paths.
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryByPath(
base::FilePath::FromUTF8Unsafe("drive/root/foo/bar (2).txt"), &entry));
EXPECT_EQ("bar2", entry.resource_id());
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryByPath(
base::FilePath::FromUTF8Unsafe("drive/root/foo (1)/bar.txt"), &entry));
EXPECT_EQ("bar3", entry.resource_id());
}
TEST_F(ResourceMetadataTest, EncodedNames) {
ResourceEntry entry;
std::string dir_id;
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry(
CreateDirectoryEntry("\\(^o^)/", kTestRootResourceId), &dir_id));
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryById(
dir_id, &entry));
EXPECT_EQ("\\(^o^)\xE2\x88\x95", entry.base_name());
std::string file_id;
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->AddEntry(
CreateFileEntryWithResourceId("Slash /.txt", "myfile", dir_id),
&file_id));
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryById(
file_id, &entry));
EXPECT_EQ("Slash \xE2\x88\x95.txt", entry.base_name());
ASSERT_EQ(FILE_ERROR_OK, resource_metadata_->GetResourceEntryByPath(
base::FilePath::FromUTF8Unsafe(
"drive/root/\\(^o^)\xE2\x88\x95/Slash \xE2\x88\x95.txt"),
&entry));
EXPECT_EQ("myfile", entry.resource_id());
}
} // namespace internal
} // namespace drive
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