Commit 82c277c0 authored by kinaba@chromium.org's avatar kinaba@chromium.org

Rewrite DownloadHandlerTest using DummyFileSystem.

BUG=246098

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@203941 0039d316-1c4b-4281-b951-d872f2087c98
parent a3ad43da
......@@ -6,9 +6,9 @@
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop.h"
#include "chrome/browser/chromeos/drive/dummy_file_system.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/browser/chromeos/drive/file_write_helper.h"
#include "chrome/browser/chromeos/drive/mock_file_system.h"
#include "chrome/browser/google_apis/test_util.h"
#include "content/public/test/mock_download_item.h"
#include "content/public/test/mock_download_manager.h"
......@@ -16,25 +16,54 @@
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::Return;
using ::testing::SaveArg;
using ::testing::_;
namespace drive {
namespace {
// Copies |file_path| to |out_file_path|, is used as a
// SubstituteDriveDownloadPathCallback.
void CopySubstituteDriveDownloadPathResult(base::FilePath* out_file_path,
const base::FilePath& file_path) {
*out_file_path = file_path;
}
// Flags to control the state of the test file system.
enum DownloadPathState {
// Simulates the state that requested path just simply exists.
PATH_EXISTS,
// Simulates the state that requested path fails to be accessed.
PATH_INVALID,
// Simulates the state that the requested path does not exist.
PATH_NOT_EXIST,
// Simulates the state that the path does not exist nor be able to be created.
PATH_NOT_EXIST_AND_CREATE_FAIL,
};
// Copies |value| to |out|, is used as a content::CheckForFileExistenceCallback.
void CopyCheckForFileExistenceResult(bool* out, bool value) {
*out = value;
}
// Test file system for verifying the behavior of DownloadHandler, by simulating
// various responses from FileSystem.
class DownloadHandlerTestFileSystem : public DummyFileSystem {
public:
DownloadHandlerTestFileSystem() : state_(PATH_INVALID) {}
void set_download_path_state(DownloadPathState state) { state_ = state; }
// FileSystemInterface overrides.
virtual void GetResourceEntryByPath(
const base::FilePath& file_path,
const GetResourceEntryCallback& callback) OVERRIDE {
if (state_ == PATH_EXISTS) {
callback.Run(FILE_ERROR_OK, make_scoped_ptr(new ResourceEntry));
return;
}
callback.Run(
state_ == PATH_INVALID ? FILE_ERROR_FAILED : FILE_ERROR_NOT_FOUND,
scoped_ptr<ResourceEntry>());
}
virtual void CreateDirectory(
const base::FilePath& directory_path,
bool is_exclusive,
bool is_recursive,
const FileOperationCallback& callback) OVERRIDE {
callback.Run(state_ == PATH_NOT_EXIST ? FILE_ERROR_OK : FILE_ERROR_FAILED);
}
private:
DownloadPathState state_;
};
} // namespace
......@@ -49,36 +78,23 @@ class DownloadHandlerTest : public testing::Test {
// Set expectations for download item.
EXPECT_CALL(download_item_, GetState())
.WillRepeatedly(Return(content::DownloadItem::IN_PROGRESS));
// Set expectations for file system to save argument callbacks.
EXPECT_CALL(file_system_, GetResourceEntryByPath(_, _))
.WillRepeatedly(SaveArg<1>(&get_entry_info_callback_));
EXPECT_CALL(file_system_, CreateDirectory(_, _, _, _))
.WillRepeatedly(SaveArg<3>(&create_directory_callback_));
.WillRepeatedly(testing::Return(content::DownloadItem::IN_PROGRESS));
file_write_helper_.reset(new FileWriteHelper(&file_system_));
file_write_helper_.reset(new FileWriteHelper(&test_file_system_));
download_handler_.reset(
new DownloadHandler(file_write_helper_.get(), &file_system_));
new DownloadHandler(file_write_helper_.get(), &test_file_system_));
download_handler_->Initialize(download_manager_.get(), temp_dir_.path());
}
virtual void TearDown() OVERRIDE {
}
protected:
base::ScopedTempDir temp_dir_;
base::MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_;
scoped_ptr<content::MockDownloadManager> download_manager_;
MockFileSystem file_system_;
DownloadHandlerTestFileSystem test_file_system_;
scoped_ptr<FileWriteHelper> file_write_helper_;
scoped_ptr<DownloadHandler> download_handler_;
content::MockDownloadItem download_item_;
// Argument callbacks passed to the file system.
GetResourceEntryCallback get_entry_info_callback_;
FileOperationCallback create_directory_callback_;
};
TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathNonDrivePath) {
......@@ -90,7 +106,7 @@ TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathNonDrivePath) {
download_handler_->SubstituteDriveDownloadPath(
non_drive_path,
&download_item_,
base::Bind(&CopySubstituteDriveDownloadPathResult, &substituted_path));
google_apis::test_util::CreateCopyResultCallback(&substituted_path));
google_apis::test_util::RunBlockingPoolTask();
// Check the result.
......@@ -102,18 +118,15 @@ TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPath) {
const base::FilePath drive_path =
util::GetDriveMountPointPath().AppendASCII("test.dat");
// Test the case that the download target directory already exists.
test_file_system_.set_download_path_state(PATH_EXISTS);
// Call SubstituteDriveDownloadPath()
base::FilePath substituted_path;
download_handler_->SubstituteDriveDownloadPath(
drive_path,
&download_item_,
base::Bind(&CopySubstituteDriveDownloadPathResult, &substituted_path));
google_apis::test_util::RunBlockingPoolTask();
// Return result of GetResourceEntryByPath(), destination directory found.
scoped_ptr<ResourceEntry> entry(new ResourceEntry);
ASSERT_FALSE(get_entry_info_callback_.is_null());
get_entry_info_callback_.Run(FILE_ERROR_OK, entry.Pass());
google_apis::test_util::CreateCopyResultCallback(&substituted_path));
google_apis::test_util::RunBlockingPoolTask();
// Check the result.
......@@ -126,18 +139,16 @@ TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathGetEntryFailure) {
const base::FilePath drive_path =
util::GetDriveMountPointPath().AppendASCII("test.dat");
// Test the case that access to the download target directory failed for some
// reason.
test_file_system_.set_download_path_state(PATH_INVALID);
// Call SubstituteDriveDownloadPath()
base::FilePath substituted_path;
download_handler_->SubstituteDriveDownloadPath(
drive_path,
&download_item_,
base::Bind(&CopySubstituteDriveDownloadPathResult, &substituted_path));
google_apis::test_util::RunBlockingPoolTask();
// Return result of GetResourceEntryByPath(), failing for some reason.
ASSERT_FALSE(get_entry_info_callback_.is_null());
get_entry_info_callback_.Run(FILE_ERROR_FAILED,
scoped_ptr<ResourceEntry>());
google_apis::test_util::CreateCopyResultCallback(&substituted_path));
google_apis::test_util::RunBlockingPoolTask();
// Check the result.
......@@ -148,23 +159,16 @@ TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathCreateDirectory) {
const base::FilePath drive_path =
util::GetDriveMountPointPath().AppendASCII("test.dat");
// Test the case that access to the download target directory does not exist,
// and thus will be created in DownloadHandler.
test_file_system_.set_download_path_state(PATH_NOT_EXIST);
// Call SubstituteDriveDownloadPath()
base::FilePath substituted_path;
download_handler_->SubstituteDriveDownloadPath(
drive_path,
&download_item_,
base::Bind(&CopySubstituteDriveDownloadPathResult, &substituted_path));
google_apis::test_util::RunBlockingPoolTask();
// Return result of GetResourceEntryByPath(), destination directory not found.
ASSERT_FALSE(get_entry_info_callback_.is_null());
get_entry_info_callback_.Run(FILE_ERROR_NOT_FOUND,
scoped_ptr<ResourceEntry>());
google_apis::test_util::RunBlockingPoolTask();
// Return result of CreateDirecotry().
ASSERT_FALSE(create_directory_callback_.is_null());
create_directory_callback_.Run(FILE_ERROR_OK);
google_apis::test_util::CreateCopyResultCallback(&substituted_path));
google_apis::test_util::RunBlockingPoolTask();
// Check the result.
......@@ -178,23 +182,16 @@ TEST_F(DownloadHandlerTest,
const base::FilePath drive_path =
util::GetDriveMountPointPath().AppendASCII("test.dat");
// Test the case that access to the download target directory does not exist,
// and creation fails for some reason.
test_file_system_.set_download_path_state(PATH_NOT_EXIST_AND_CREATE_FAIL);
// Call SubstituteDriveDownloadPath()
base::FilePath substituted_path;
download_handler_->SubstituteDriveDownloadPath(
drive_path,
&download_item_,
base::Bind(&CopySubstituteDriveDownloadPathResult, &substituted_path));
google_apis::test_util::RunBlockingPoolTask();
// Return result of GetResourceEntryByPath(), destination directory not found.
ASSERT_FALSE(get_entry_info_callback_.is_null());
get_entry_info_callback_.Run(FILE_ERROR_NOT_FOUND,
scoped_ptr<ResourceEntry>());
google_apis::test_util::RunBlockingPoolTask();
// Return result of CreateDirecotry().
ASSERT_FALSE(create_directory_callback_.is_null());
create_directory_callback_.Run(FILE_ERROR_FAILED);
google_apis::test_util::CreateCopyResultCallback(&substituted_path));
google_apis::test_util::RunBlockingPoolTask();
// Check the result.
......@@ -206,19 +203,14 @@ TEST_F(DownloadHandlerTest,
TEST_F(DownloadHandlerTest, SubstituteDriveDownloadPathForSavePackage) {
const base::FilePath drive_path =
util::GetDriveMountPointPath().AppendASCII("test.dat");
test_file_system_.set_download_path_state(PATH_EXISTS);
// Call SubstituteDriveDownloadPath()
base::FilePath substituted_path;
download_handler_->SubstituteDriveDownloadPath(
drive_path,
NULL, // DownloadItem is not available at this moment.
base::Bind(&CopySubstituteDriveDownloadPathResult, &substituted_path));
google_apis::test_util::RunBlockingPoolTask();
// Return result of GetResourceEntryByPath(), destination directory found.
scoped_ptr<ResourceEntry> entry(new ResourceEntry);
ASSERT_FALSE(get_entry_info_callback_.is_null());
get_entry_info_callback_.Run(FILE_ERROR_OK, entry.Pass());
google_apis::test_util::CreateCopyResultCallback(&substituted_path));
google_apis::test_util::RunBlockingPoolTask();
// Check the result of SubstituteDriveDownloadPath().
......@@ -244,37 +236,26 @@ TEST_F(DownloadHandlerTest, CheckForFileExistence) {
ASSERT_TRUE(download_handler_->IsDriveDownload(&download_item_));
EXPECT_EQ(drive_path, download_handler_->GetTargetPath(&download_item_));
// Test for the case when the path exists.
test_file_system_.set_download_path_state(PATH_EXISTS);
// Call CheckForFileExistence.
bool file_exists = false;
download_handler_->CheckForFileExistence(
&download_item_,
base::Bind(&CopyCheckForFileExistenceResult, &file_exists));
google_apis::test_util::RunBlockingPoolTask();
// Return result of GetResourceEntryByPath(), file exists.
{
scoped_ptr<ResourceEntry> entry(new ResourceEntry);
ASSERT_FALSE(get_entry_info_callback_.is_null());
get_entry_info_callback_.Run(FILE_ERROR_OK, entry.Pass());
}
google_apis::test_util::CreateCopyResultCallback(&file_exists));
google_apis::test_util::RunBlockingPoolTask();
// Check the result.
EXPECT_TRUE(file_exists);
// Reset callback to call CheckForFileExistence again.
get_entry_info_callback_.Reset();
// Test for the case when the path does not exist.
test_file_system_.set_download_path_state(PATH_NOT_EXIST);
// Call CheckForFileExistence again.
download_handler_->CheckForFileExistence(
&download_item_,
base::Bind(&CopyCheckForFileExistenceResult, &file_exists));
google_apis::test_util::RunBlockingPoolTask();
// Return result of GetResourceEntryByPath(), file does not exist.
ASSERT_FALSE(get_entry_info_callback_.is_null());
get_entry_info_callback_.Run(FILE_ERROR_NOT_FOUND,
scoped_ptr<ResourceEntry>());
google_apis::test_util::CreateCopyResultCallback(&file_exists));
google_apis::test_util::RunBlockingPoolTask();
// Check the result.
......
// Copyright (c) 2012 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/mock_file_system.h"
namespace drive {
MockFileSystem::MockFileSystem() {
}
MockFileSystem::~MockFileSystem() {
}
} // namespace drive
// Copyright (c) 2012 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.
#ifndef CHROME_BROWSER_CHROMEOS_DRIVE_MOCK_FILE_SYSTEM_H_
#define CHROME_BROWSER_CHROMEOS_DRIVE_MOCK_FILE_SYSTEM_H_
#include <string>
#include "chrome/browser/chromeos/drive/file_system_interface.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace drive {
class FileSystemObserver;
// Mock for FileSystemInterface.
class MockFileSystem : public FileSystemInterface {
public:
MockFileSystem();
virtual ~MockFileSystem();
// FileSystemInterface overrides.
MOCK_METHOD0(Initialize, void());
MOCK_METHOD1(AddObserver, void(FileSystemObserver* observer));
MOCK_METHOD1(RemoveObserver,
void(FileSystemObserver* observer));
MOCK_METHOD0(CheckForUpdates, void());
MOCK_METHOD2(GetResourceEntryById,
void(const std::string& resource_id,
const GetResourceEntryCallback& callback));
MOCK_METHOD3(Search, void(const std::string& search_query,
const GURL& next_url,
const SearchCallback& callback));
MOCK_METHOD4(SearchMetadata, void(const std::string& query,
int options,
int at_most_num_matches,
const SearchMetadataCallback& callback));
MOCK_METHOD3(TransferFileFromRemoteToLocal,
void(const base::FilePath& local_src_file_path,
const base::FilePath& remote_dest_file_path,
const FileOperationCallback& callback));
MOCK_METHOD3(TransferFileFromLocalToRemote,
void(const base::FilePath& local_src_file_path,
const base::FilePath& remote_dest_file_path,
const FileOperationCallback& callback));
MOCK_METHOD2(OpenFile, void(const base::FilePath& file_path,
const OpenFileCallback& callback));
MOCK_METHOD2(CloseFile, void(const base::FilePath& file_path,
const FileOperationCallback& callback));
MOCK_METHOD3(Copy, void(const base::FilePath& src_file_path,
const base::FilePath& dest_file_path,
const FileOperationCallback& callback));
MOCK_METHOD3(Move, void(const base::FilePath& src_file_path,
const base::FilePath& dest_file_path,
const FileOperationCallback& callback));
MOCK_METHOD3(Remove, void(const base::FilePath& file_path,
bool is_recursive,
const FileOperationCallback& callback));
MOCK_METHOD4(CreateDirectory,
void(const base::FilePath& directory_path,
bool is_exclusive,
bool is_recursive,
const FileOperationCallback& callback));
MOCK_METHOD3(CreateFile,
void(const base::FilePath& file_path,
bool is_exclusive,
const FileOperationCallback& callback));
MOCK_METHOD4(TouchFile,
void(const base::FilePath& file_path,
const base::Time& last_access_time,
const base::Time& last_modified_time,
const FileOperationCallback& callback));
MOCK_METHOD2(Pin, void(const base::FilePath& file_path,
const FileOperationCallback& callback));
MOCK_METHOD2(Unpin, void(const base::FilePath& file_path,
const FileOperationCallback& callback));
MOCK_METHOD2(GetFileByPath,
void(const base::FilePath& file_path,
const GetFileCallback& callback));
MOCK_METHOD4(
GetFileByResourceId,
void(const std::string& resource_id,
const ClientContext& context,
const GetFileCallback& get_file_callback,
const google_apis::GetContentCallback& get_content_callback));
MOCK_METHOD4(
GetFileContentByPath,
void(const base::FilePath& file_path,
const GetFileContentInitializedCallback& initialized_callback,
const google_apis::GetContentCallback& get_content_callback,
const FileOperationCallback& completion_callback));
MOCK_METHOD3(UpdateFileByResourceId,
void(const std::string& resource_id,
const ClientContext& context,
const FileOperationCallback& callback));
MOCK_METHOD2(GetResourceEntryByPath,
void(const base::FilePath& file_path,
const GetResourceEntryCallback& callback));
MOCK_METHOD2(ReadDirectoryByPath,
void(const base::FilePath& file_path,
const ReadDirectoryWithSettingCallback& callback));
MOCK_METHOD2(RefreshDirectory,
void(const base::FilePath& file_path,
const FileOperationCallback& callback));
MOCK_METHOD1(GetAvailableSpace,
void(const GetAvailableSpaceCallback& callback));
MOCK_METHOD1(GetMetadata,
void(const GetFilesystemMetadataCallback& callback));
MOCK_METHOD2(MarkCacheFileAsMounted,
void(const base::FilePath& drive_file_path,
const OpenFileCallback& callback));
MOCK_METHOD2(MarkCacheFileAsUnmounted,
void(const base::FilePath& cache_file_path,
const FileOperationCallback& callback));
MOCK_METHOD3(GetCacheEntryByResourceId,
void(const std::string& resource_id,
const std::string& md5,
const GetCacheEntryCallback& callback));
MOCK_METHOD0(Reload, void());
};
} // namespace drive
#endif // CHROME_BROWSER_CHROMEOS_DRIVE_MOCK_FILE_SYSTEM_H_
......@@ -602,8 +602,6 @@
'browser/chromeos/drive/mock_directory_change_observer.h',
'browser/chromeos/drive/mock_file_cache_observer.cc',
'browser/chromeos/drive/mock_file_cache_observer.h',
'browser/chromeos/drive/mock_file_system.cc',
'browser/chromeos/drive/mock_file_system.h',
'browser/chromeos/drive/remove_stale_cache_files_unittest.cc',
'browser/chromeos/drive/resource_entry_conversion_unittest.cc',
'browser/chromeos/drive/resource_metadata_storage_unittest.cc',
......
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