#include "chrome/browser/chromeos/drive/sync_client.h"
-#include "base/file_util.h"
#include "base/files/file_path.h"
+#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/testing_pref_service.h"
#include "chrome/browser/chromeos/drive/drive.pb.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_change.h"
#include "chrome/browser/chromeos/drive/file_system/move_operation.h"
-#include "chrome/browser/chromeos/drive/file_system/operation_observer.h"
+#include "chrome/browser/chromeos/drive/file_system/operation_delegate.h"
#include "chrome/browser/chromeos/drive/file_system/remove_operation.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/browser/chromeos/drive/job_scheduler.h"
#include "chrome/browser/drive/event_logger.h"
#include "chrome/browser/drive/fake_drive_service.h"
#include "content/public/test/test_browser_thread_bundle.h"
+#include "google_apis/drive/drive_api_parser.h"
#include "google_apis/drive/test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
base::Closure paused_action_;
};
-class DummyOperationObserver : public file_system::OperationObserver {
- // OperationObserver override:
- virtual void OnDirectoryChangedByOperation(
- const base::FilePath& path) OVERRIDE {}
-};
-
} // namespace
class SyncClientTest : public testing::Test {
logger_.reset(new EventLogger);
drive_service_.reset(new SyncClientTestDriveService);
- drive_service_->LoadResourceListForWapi("gdata/empty_feed.json");
- drive_service_->LoadAccountMetadataForWapi(
- "gdata/account_metadata.json");
scheduler_.reset(new JobScheduler(pref_service_.get(),
logger_.get(),
temp_dir_.path(), base::MessageLoopProxy::current().get()));
ASSERT_TRUE(metadata_storage_->Initialize());
- metadata_.reset(new internal::ResourceMetadata(
- metadata_storage_.get(), base::MessageLoopProxy::current()));
- ASSERT_EQ(FILE_ERROR_OK, metadata_->Initialize());
-
cache_.reset(new FileCache(metadata_storage_.get(),
temp_dir_.path(),
base::MessageLoopProxy::current().get(),
NULL /* free_disk_space_getter */));
ASSERT_TRUE(cache_->Initialize());
+ metadata_.reset(new internal::ResourceMetadata(
+ metadata_storage_.get(), cache_.get(),
+ base::MessageLoopProxy::current()));
+ ASSERT_EQ(FILE_ERROR_OK, metadata_->Initialize());
+
about_resource_loader_.reset(new AboutResourceLoader(scheduler_.get()));
loader_controller_.reset(new LoaderController);
change_list_loader_.reset(new ChangeListLoader(
base::MessageLoopProxy::current().get(),
metadata_.get(),
scheduler_.get(),
- drive_service_.get(),
about_resource_loader_.get(),
loader_controller_.get()));
ASSERT_NO_FATAL_FAILURE(SetUpTestData());
sync_client_.reset(new SyncClient(base::MessageLoopProxy::current().get(),
- &observer_,
+ &delegate_,
scheduler_.get(),
metadata_.get(),
cache_.get(),
// Adds a file to the service root and |resource_ids_|.
void AddFileEntry(const std::string& title) {
google_apis::GDataErrorCode error = google_apis::GDATA_FILE_ERROR;
- scoped_ptr<google_apis::ResourceEntry> entry;
+ scoped_ptr<google_apis::FileResource> entry;
drive_service_->AddNewFile(
"text/plain",
kRemoteContent,
base::RunLoop().RunUntilIdle();
ASSERT_EQ(google_apis::HTTP_CREATED, error);
ASSERT_TRUE(entry);
- resource_ids_[title] = entry->resource_id();
+ resource_ids_[title] = entry->file_id();
}
// Sets up data for tests.
// Load data from the service to the metadata.
FileError error = FILE_ERROR_FAILED;
- change_list_loader_->LoadForTesting(
+ change_list_loader_->LoadIfNeeded(
google_apis::test_util::CreateCopyResultCallback(&error));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(FILE_ERROR_OK, error);
// Prepare a removed file.
file_system::RemoveOperation remove_operation(
- base::MessageLoopProxy::current().get(), &observer_, metadata_.get(),
+ base::MessageLoopProxy::current().get(), &delegate_, metadata_.get(),
cache_.get());
remove_operation.Remove(
- metadata_->GetFilePath(GetLocalId("removed")),
+ util::GetDriveMyDriveRootPath().AppendASCII("removed"),
false, // is_recursive
google_apis::test_util::CreateCopyResultCallback(&error));
base::RunLoop().RunUntilIdle();
// Prepare a moved file.
file_system::MoveOperation move_operation(
- base::MessageLoopProxy::current().get(), &observer_, metadata_.get());
+ base::MessageLoopProxy::current().get(), &delegate_, metadata_.get());
move_operation.Move(
- metadata_->GetFilePath(GetLocalId("moved")),
+ util::GetDriveMyDriveRootPath().AppendASCII("moved"),
util::GetDriveMyDriveRootPath().AppendASCII("moved_new_title"),
- false, // preserve_last_modified
google_apis::test_util::CreateCopyResultCallback(&error));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(FILE_ERROR_OK, error);
fake_network_change_notifier_;
scoped_ptr<EventLogger> logger_;
scoped_ptr<SyncClientTestDriveService> drive_service_;
- DummyOperationObserver observer_;
+ file_system::OperationDelegate delegate_;
scoped_ptr<JobScheduler> scheduler_;
scoped_ptr<ResourceMetadataStorage,
test_util::DestroyHelperForTests> metadata_storage_;
- scoped_ptr<ResourceMetadata, test_util::DestroyHelperForTests> metadata_;
scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_;
+ scoped_ptr<ResourceMetadata, test_util::DestroyHelperForTests> metadata_;
scoped_ptr<AboutResourceLoader> about_resource_loader_;
scoped_ptr<LoaderController> loader_controller_;
scoped_ptr<ChangeListLoader> change_list_loader_;
sync_client_->StartProcessingBacklog();
base::RunLoop().RunUntilIdle();
- FileCacheEntry cache_entry;
+ ResourceEntry entry;
// Pinned files get downloaded.
- EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
- EXPECT_TRUE(cache_entry.is_present());
+ EXPECT_EQ(FILE_ERROR_OK,
+ metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
+ EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
- EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("bar"), &cache_entry));
- EXPECT_TRUE(cache_entry.is_present());
+ EXPECT_EQ(FILE_ERROR_OK,
+ metadata_->GetResourceEntryById(GetLocalId("bar"), &entry));
+ EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
- EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("baz"), &cache_entry));
- EXPECT_TRUE(cache_entry.is_present());
+ EXPECT_EQ(FILE_ERROR_OK,
+ metadata_->GetResourceEntryById(GetLocalId("baz"), &entry));
+ EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
// Dirty file gets uploaded.
- EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("dirty"), &cache_entry));
- EXPECT_FALSE(cache_entry.is_dirty());
+ EXPECT_EQ(FILE_ERROR_OK,
+ metadata_->GetResourceEntryById(GetLocalId("dirty"), &entry));
+ EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty());
// Removed entry is not found.
google_apis::GDataErrorCode status = google_apis::GDATA_OTHER_ERROR;
- scoped_ptr<google_apis::ResourceEntry> resource_entry;
- drive_service_->GetResourceEntry(
+ scoped_ptr<google_apis::FileResource> server_entry;
+ drive_service_->GetFileResource(
resource_ids_["removed"],
- google_apis::test_util::CreateCopyResultCallback(&status,
- &resource_entry));
+ google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
- ASSERT_TRUE(resource_entry);
- EXPECT_TRUE(resource_entry->deleted());
+ ASSERT_TRUE(server_entry);
+ EXPECT_TRUE(server_entry->labels().is_trashed());
// Moved entry was moved.
status = google_apis::GDATA_OTHER_ERROR;
- drive_service_->GetResourceEntry(
+ drive_service_->GetFileResource(
resource_ids_["moved"],
- google_apis::test_util::CreateCopyResultCallback(&status,
- &resource_entry));
+ google_apis::test_util::CreateCopyResultCallback(&status, &server_entry));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(google_apis::HTTP_SUCCESS, status);
- ASSERT_TRUE(resource_entry);
- EXPECT_EQ("moved_new_title", resource_entry->title());
+ ASSERT_TRUE(server_entry);
+ EXPECT_EQ("moved_new_title", server_entry->title());
}
TEST_F(SyncClientTest, AddFetchTask) {
sync_client_->AddFetchTask(GetLocalId("foo"));
base::RunLoop().RunUntilIdle();
- FileCacheEntry cache_entry;
- EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
- EXPECT_TRUE(cache_entry.is_present());
+ ResourceEntry entry;
+ EXPECT_EQ(FILE_ERROR_OK,
+ metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
+ EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
}
TEST_F(SyncClientTest, AddFetchTaskAndCancelled) {
base::RunLoop().RunUntilIdle();
// The file should be unpinned if the user wants the download to be cancelled.
- FileCacheEntry cache_entry;
- EXPECT_FALSE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
+ ResourceEntry entry;
+ EXPECT_EQ(FILE_ERROR_OK,
+ metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
+ EXPECT_FALSE(entry.file_specific_info().cache_state().is_pinned());
}
TEST_F(SyncClientTest, RemoveFetchTask) {
base::RunLoop().RunUntilIdle();
// Only "bar" should be fetched.
- FileCacheEntry cache_entry;
- EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
- EXPECT_FALSE(cache_entry.is_present());
+ ResourceEntry entry;
+ EXPECT_EQ(FILE_ERROR_OK,
+ metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
+ EXPECT_FALSE(entry.file_specific_info().cache_state().is_present());
- EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("bar"), &cache_entry));
- EXPECT_TRUE(cache_entry.is_present());
+ EXPECT_EQ(FILE_ERROR_OK,
+ metadata_->GetResourceEntryById(GetLocalId("bar"), &entry));
+ EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
- EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("baz"), &cache_entry));
- EXPECT_FALSE(cache_entry.is_present());
+ EXPECT_EQ(FILE_ERROR_OK,
+ metadata_->GetResourceEntryById(GetLocalId("baz"), &entry));
+ EXPECT_FALSE(entry.file_specific_info().cache_state().is_present());
}
base::RunLoop().RunUntilIdle();
// Not yet fetched nor uploaded.
- FileCacheEntry cache_entry;
- EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
- EXPECT_FALSE(cache_entry.is_present());
- EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("dirty"), &cache_entry));
- EXPECT_TRUE(cache_entry.is_dirty());
+ ResourceEntry entry;
+ EXPECT_EQ(FILE_ERROR_OK,
+ metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
+ EXPECT_FALSE(entry.file_specific_info().cache_state().is_present());
+ EXPECT_EQ(FILE_ERROR_OK,
+ metadata_->GetResourceEntryById(GetLocalId("dirty"), &entry));
+ EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty());
// Switch to online.
fake_network_change_notifier_->SetConnectionType(
base::RunLoop().RunUntilIdle();
// Fetched and uploaded.
- EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("foo"), &cache_entry));
- EXPECT_TRUE(cache_entry.is_present());
- EXPECT_TRUE(cache_->GetCacheEntry(GetLocalId("dirty"), &cache_entry));
- EXPECT_FALSE(cache_entry.is_dirty());
+ EXPECT_EQ(FILE_ERROR_OK,
+ metadata_->GetResourceEntryById(GetLocalId("foo"), &entry));
+ EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
+ EXPECT_EQ(FILE_ERROR_OK,
+ metadata_->GetResourceEntryById(GetLocalId("dirty"), &entry));
+ EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty());
}
TEST_F(SyncClientTest, ScheduleRerun) {
// Start syncing the child first.
sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), local_id2);
- base::RunLoop().RunUntilIdle();
// Start syncing the parent later.
sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), local_id1);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(ResourceEntry::CLEAN, entry2.metadata_edit_state());
}
+TEST_F(SyncClientTest, WaitForUpdateTaskToComplete) {
+ // Create a directory locally.
+ const base::FilePath kPath(FILE_PATH_LITERAL("drive/root/dir1"));
+
+ ResourceEntry parent;
+ EXPECT_EQ(FILE_ERROR_OK,
+ metadata_->GetResourceEntryByPath(kPath.DirName(), &parent));
+
+ ResourceEntry entry;
+ entry.set_parent_local_id(parent.local_id());
+ entry.set_title(kPath.BaseName().AsUTF8Unsafe());
+ entry.mutable_file_info()->set_is_directory(true);
+ entry.set_metadata_edit_state(ResourceEntry::DIRTY);
+ std::string local_id;
+ EXPECT_EQ(FILE_ERROR_OK, metadata_->AddEntry(entry, &local_id));
+
+ // Sync task is not yet avialable.
+ FileError error = FILE_ERROR_FAILED;
+ EXPECT_FALSE(sync_client_->WaitForUpdateTaskToComplete(
+ local_id, google_apis::test_util::CreateCopyResultCallback(&error)));
+
+ // Start syncing the directory and wait for it to complete.
+ sync_client_->AddUpdateTask(ClientContext(USER_INITIATED), local_id);
+
+ EXPECT_TRUE(sync_client_->WaitForUpdateTaskToComplete(
+ local_id, google_apis::test_util::CreateCopyResultCallback(&error)));
+
+ base::RunLoop().RunUntilIdle();
+
+ // The callback is called.
+ EXPECT_EQ(FILE_ERROR_OK, error);
+}
+
} // namespace internal
} // namespace drive