Skip to content

Commit

Permalink
drive: Simplify some tests in single thread manner
Browse files Browse the repository at this point in the history
Replace asynchronous method calls with synchronous ones.
Replace use of RunBlockingPoolTask with RunUntilIdle.

BUG=None
TEST=unit_tests
R=kinaba@chromium.org

Review URL: https://codereview.chromium.org/17406008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@207198 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
hashimoto@chromium.org committed Jun 19, 2013
1 parent 71c20bd commit 10bab33
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 281 deletions.
95 changes: 26 additions & 69 deletions chrome/browser/chromeos/drive/change_list_processor_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#include "chrome/browser/chromeos/drive/change_list_processor.h"

#include "base/files/scoped_temp_dir.h"
#include "base/location.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/task_runner_util.h"
#include "base/values.h"
#include "chrome/browser/chromeos/drive/drive.pb.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
Expand Down Expand Up @@ -42,20 +40,10 @@ struct EntryExpectation {
class ChangeListProcessorTest : public testing::Test {
protected:
virtual void SetUp() OVERRIDE {
blocking_task_runner_ = base::MessageLoopProxy::current();
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
metadata_.reset(new internal::ResourceMetadata(temp_dir_.path(),
blocking_task_runner_));

FileError error = FILE_ERROR_FAILED;
base::PostTaskAndReplyWithResult(
blocking_task_runner_,
FROM_HERE,
base::Bind(&ResourceMetadata::Initialize,
base::Unretained(metadata_.get())),
google_apis::test_util::CreateCopyResultCallback(&error));
google_apis::test_util::RunBlockingPoolTask();
ASSERT_EQ(FILE_ERROR_OK, error);
metadata_.reset(new internal::ResourceMetadata(
temp_dir_.path(), base::MessageLoopProxy::current()));
ASSERT_EQ(FILE_ERROR_OK, metadata_->Initialize());
}

virtual void TearDown() OVERRIDE {
Expand All @@ -82,69 +70,33 @@ class ChangeListProcessorTest : public testing::Test {
about_resource->set_root_folder_id("fake_root");

ChangeListProcessor processor(metadata_.get());
blocking_task_runner_->PostTask(
FROM_HERE,
base::Bind(&ChangeListProcessor::Apply,
base::Unretained(&processor),
base::Passed(&about_resource),
base::Passed(&changes),
false)); // is_delta_update
google_apis::test_util::RunBlockingPoolTask();
processor.Apply(about_resource.Pass(),
changes.Pass(),
false /* is_delta_update */);
}

// Applies the |changes| to |metadata_| as a delta update. Delta changelists
// should contain their changestamp in themselves.
std::set<base::FilePath> ApplyChangeList(ScopedVector<ChangeList> changes) {
scoped_ptr<google_apis::AboutResource> null_about_resource;

ChangeListProcessor processor(metadata_.get());
blocking_task_runner_->PostTask(
FROM_HERE,
base::Bind(&ChangeListProcessor::Apply,
base::Unretained(&processor),
base::Passed(&null_about_resource),
base::Passed(&changes),
true)); // is_delta_update
google_apis::test_util::RunBlockingPoolTask();
processor.Apply(scoped_ptr<google_apis::AboutResource>(),
changes.Pass(),
true /* is_delta_update */);
return processor.changed_dirs();
}

// Gets the resource entry for the path from |metadata_| synchronously.
// Returns null if the entry does not exist.
scoped_ptr<ResourceEntry> GetResourceEntry(const std::string& path) {
FileError error = FILE_ERROR_FAILED;
scoped_ptr<ResourceEntry> entry(new ResourceEntry);
base::PostTaskAndReplyWithResult(
blocking_task_runner_.get(),
FROM_HERE,
base::Bind(&internal::ResourceMetadata::GetResourceEntryByPath,
base::Unretained(metadata_.get()),
base::FilePath::FromUTF8Unsafe(path),
entry.get()),
base::Bind(google_apis::test_util::CreateCopyResultCallback(&error)));
google_apis::test_util::RunBlockingPoolTask();
FileError error = metadata_->GetResourceEntryByPath(
base::FilePath::FromUTF8Unsafe(path), entry.get());
if (error != FILE_ERROR_OK)
entry.reset();
return entry.Pass();
}

// Gets the largest changestamp stored in |metadata_|.
int64 GetChangestamp() {
int64 changestamp = -1;
base::PostTaskAndReplyWithResult(
blocking_task_runner_.get(),
FROM_HERE,
base::Bind(&internal::ResourceMetadata::GetLargestChangestamp,
base::Unretained(metadata_.get())),
base::Bind(
google_apis::test_util::CreateCopyResultCallback(&changestamp)));
google_apis::test_util::RunBlockingPoolTask();
return changestamp;
}

private:
content::TestBrowserThreadBundle thread_bundle_;
scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_;
base::ScopedTempDir temp_dir_;
scoped_ptr<internal::ResourceMetadata, test_util::DestroyHelperForTests>
metadata_;
Expand Down Expand Up @@ -202,7 +154,7 @@ TEST_F(ChangeListProcessorTest, ApplyFullResourceList) {
entry->file_info().is_directory() ? DIRECTORY : FILE);
}

EXPECT_EQ(kBaseResourceListChangestamp, GetChangestamp());
EXPECT_EQ(kBaseResourceListChangestamp, metadata_->GetLargestChangestamp());
}

TEST_F(ChangeListProcessorTest, DeltaFileAddedInNewDirectory) {
Expand Down Expand Up @@ -233,7 +185,8 @@ TEST_F(ChangeListProcessorTest, DeltaFileAddedInNewDirectory) {
std::set<base::FilePath> changed_dirs =
ApplyChangeList(ParseChangeList(kTestJson));

EXPECT_EQ(16730, GetChangestamp()); // the value is written in kTestJson.
// The value is written in kTestJson.
EXPECT_EQ(16730, metadata_->GetLargestChangestamp());
EXPECT_TRUE(GetResourceEntry("drive/root/New Directory"));
EXPECT_TRUE(GetResourceEntry(
"drive/root/New Directory/File in new dir.gdoc"));
Expand Down Expand Up @@ -267,7 +220,8 @@ TEST_F(ChangeListProcessorTest, DeltaDirMovedFromRootToDirectory) {
std::set<base::FilePath> changed_dirs =
ApplyChangeList(ParseChangeList(kTestJson));

EXPECT_EQ(16809, GetChangestamp()); // the value is written in kTestJson.
// The value is written in kTestJson.
EXPECT_EQ(16809, metadata_->GetLargestChangestamp());
EXPECT_FALSE(GetResourceEntry("drive/root/Directory 1"));
EXPECT_TRUE(GetResourceEntry(
"drive/root/Directory 2 excludeDir-test/Directory 1"));
Expand Down Expand Up @@ -308,7 +262,8 @@ TEST_F(ChangeListProcessorTest, DeltaFileMovedFromDirectoryToRoot) {
std::set<base::FilePath> changed_dirs =
ApplyChangeList(ParseChangeList(kTestJson));

EXPECT_EQ(16815, GetChangestamp()); // the value is written in kTestJson.
// The value is written in kTestJson.
EXPECT_EQ(16815, metadata_->GetLargestChangestamp());
EXPECT_FALSE(GetResourceEntry(
"drive/root/Directory 1/SubDirectory File 1.txt"));
EXPECT_TRUE(GetResourceEntry("drive/root/SubDirectory File 1.txt"));
Expand Down Expand Up @@ -344,7 +299,8 @@ TEST_F(ChangeListProcessorTest, DeltaFileRenamedInDirectory) {
std::set<base::FilePath> changed_dirs =
ApplyChangeList(ParseChangeList(kTestJson));

EXPECT_EQ(16767, GetChangestamp()); // the value is written in kTestJson.
// The value is written in kTestJson.
EXPECT_EQ(16767, metadata_->GetLargestChangestamp());
EXPECT_FALSE(GetResourceEntry(
"drive/root/Directory 1/SubDirectory File 1.txt"));
EXPECT_TRUE(GetResourceEntry(
Expand Down Expand Up @@ -381,7 +337,7 @@ TEST_F(ChangeListProcessorTest, DeltaAddAndDeleteFileInRoot) {
ApplyFullResourceList(ParseChangeList(kBaseResourceListFile));
std::set<base::FilePath> changed_dirs =
ApplyChangeList(ParseChangeList(kTestJsonAdd));
EXPECT_EQ(16683, GetChangestamp());
EXPECT_EQ(16683, metadata_->GetLargestChangestamp());
EXPECT_TRUE(GetResourceEntry("drive/root/Added file.gdoc"));
EXPECT_EQ(1U, changed_dirs.size());
EXPECT_TRUE(changed_dirs.count(
Expand All @@ -399,7 +355,7 @@ TEST_F(ChangeListProcessorTest, DeltaAddAndDeleteFileInRoot) {

// Apply.
changed_dirs = ApplyChangeList(ParseChangeList(kTestJsonDelete));
EXPECT_EQ(16687, GetChangestamp());
EXPECT_EQ(16687, metadata_->GetLargestChangestamp());
EXPECT_FALSE(GetResourceEntry("drive/root/Added file.gdoc"));
EXPECT_EQ(1U, changed_dirs.size());
EXPECT_TRUE(changed_dirs.count(
Expand Down Expand Up @@ -432,7 +388,7 @@ TEST_F(ChangeListProcessorTest, DeltaAddAndDeleteFileFromExistingDirectory) {
ApplyFullResourceList(ParseChangeList(kBaseResourceListFile));
std::set<base::FilePath> changed_dirs =
ApplyChangeList(ParseChangeList(kTestJsonAdd));
EXPECT_EQ(16730, GetChangestamp());
EXPECT_EQ(16730, metadata_->GetLargestChangestamp());
EXPECT_TRUE(GetResourceEntry("drive/root/Directory 1/Added file.gdoc"));

EXPECT_EQ(2U, changed_dirs.size());
Expand All @@ -453,7 +409,7 @@ TEST_F(ChangeListProcessorTest, DeltaAddAndDeleteFileFromExistingDirectory) {

// Apply.
changed_dirs = ApplyChangeList(ParseChangeList(kTestJsonDelete));
EXPECT_EQ(16770, GetChangestamp());
EXPECT_EQ(16770, metadata_->GetLargestChangestamp());
EXPECT_FALSE(GetResourceEntry("drive/root/Directory 1/Added file.gdoc"));

EXPECT_EQ(1U, changed_dirs.size());
Expand Down Expand Up @@ -489,7 +445,8 @@ TEST_F(ChangeListProcessorTest, DeltaAddFileToNewButDeletedDirectory) {
std::set<base::FilePath> changed_dirs =
ApplyChangeList(ParseChangeList(kTestJson));

EXPECT_EQ(16730, GetChangestamp()); // the value is written in kTestJson.
// The value is written in kTestJson.
EXPECT_EQ(16730, metadata_->GetLargestChangestamp());
EXPECT_FALSE(GetResourceEntry("drive/root/New Directory/new_pdf_file.pdf"));

EXPECT_TRUE(changed_dirs.empty());
Expand Down
18 changes: 9 additions & 9 deletions chrome/browser/chromeos/drive/fake_file_system_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "chrome/browser/chromeos/drive/fake_file_system.h"

#include "base/file_util.h"
#include "base/run_loop.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/browser/drive/fake_drive_service.h"
#include "chrome/browser/google_apis/test_util.h"
Expand All @@ -25,8 +26,7 @@ class FakeFileSystemTest : public ::testing::Test {
"chromeos/gdata/account_metadata.json");

// Create a testee instance.
fake_file_system_.reset(
new FakeFileSystem(fake_drive_service_.get()));
fake_file_system_.reset(new FakeFileSystem(fake_drive_service_.get()));
ASSERT_TRUE(fake_file_system_->InitializeForTesting());
}

Expand All @@ -43,7 +43,7 @@ TEST_F(FakeFileSystemTest, GetResourceEntryById) {
fake_file_system_->GetResourceEntryById(
resource_id,
google_apis::test_util::CreateCopyResultCallback(&error, &entry));
google_apis::test_util::RunBlockingPoolTask();
base::RunLoop().RunUntilIdle();

ASSERT_EQ(FILE_ERROR_OK, error);
EXPECT_EQ(resource_id, entry->resource_id());
Expand All @@ -67,7 +67,7 @@ TEST_F(FakeFileSystemTest, GetFileContentByPath) {
&initialize_error, &entry, &cache_file_path, &cancel_download),
get_content_callback.callback(),
google_apis::test_util::CreateCopyResultCallback(&completion_error));
google_apis::test_util::RunBlockingPoolTask();
base::RunLoop().RunUntilIdle();

EXPECT_EQ(FILE_ERROR_OK, initialize_error);
EXPECT_TRUE(entry);
Expand All @@ -93,7 +93,7 @@ TEST_F(FakeFileSystemTest, GetFileContentByPath) {
&initialize_error, &entry, &cache_file_path, &cancel_download),
get_content_callback.callback(),
google_apis::test_util::CreateCopyResultCallback(&completion_error));
google_apis::test_util::RunBlockingPoolTask();
base::RunLoop().RunUntilIdle();

EXPECT_EQ(FILE_ERROR_OK, initialize_error);
EXPECT_TRUE(entry);
Expand Down Expand Up @@ -126,7 +126,7 @@ TEST_F(FakeFileSystemTest, GetFileContentByPath_Directory) {
&initialize_error, &entry, &cache_file_path, &cancel_download),
get_content_callback.callback(),
google_apis::test_util::CreateCopyResultCallback(&completion_error));
google_apis::test_util::RunBlockingPoolTask();
base::RunLoop().RunUntilIdle();

EXPECT_EQ(FILE_ERROR_NOT_A_FILE, completion_error);
}
Expand All @@ -138,7 +138,7 @@ TEST_F(FakeFileSystemTest, GetResourceEntryByPath) {
util::GetDriveMyDriveRootPath().AppendASCII(
"Directory 1/Sub Directory Folder"),
google_apis::test_util::CreateCopyResultCallback(&error, &entry));
google_apis::test_util::RunBlockingPoolTask();
base::RunLoop().RunUntilIdle();

ASSERT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
Expand All @@ -151,7 +151,7 @@ TEST_F(FakeFileSystemTest, GetResourceEntryByPath_Root) {
fake_file_system_->GetResourceEntryByPath(
util::GetDriveMyDriveRootPath(),
google_apis::test_util::CreateCopyResultCallback(&error, &entry));
google_apis::test_util::RunBlockingPoolTask();
base::RunLoop().RunUntilIdle();

ASSERT_EQ(FILE_ERROR_OK, error);
ASSERT_TRUE(entry);
Expand All @@ -166,7 +166,7 @@ TEST_F(FakeFileSystemTest, GetResourceEntryByPath_Invalid) {
fake_file_system_->GetResourceEntryByPath(
util::GetDriveMyDriveRootPath().AppendASCII("Invalid File Name"),
google_apis::test_util::CreateCopyResultCallback(&error, &entry));
google_apis::test_util::RunBlockingPoolTask();
base::RunLoop().RunUntilIdle();

ASSERT_EQ(FILE_ERROR_NOT_FOUND, error);
ASSERT_FALSE(entry);
Expand Down
Loading

0 comments on commit 10bab33

Please sign in to comment.