Commit 3b78761f authored by satorux@chromium.org's avatar satorux@chromium.org

google_apis: Implement DownloadFile in FakeDriveService

BUG=162350
TEST=none; no changes in production code.

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@176031 0039d316-1c4b-4281-b951-d872f2087c98
parent fe72d4f4
......@@ -4,6 +4,7 @@
#include "chrome/browser/google_apis/fake_drive_service.h"
#include "base/file_util.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/stringprintf.h"
......@@ -224,6 +225,29 @@ void FakeDriveService::DownloadFile(
const GetContentCallback& get_content_callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!download_action_callback.is_null());
base::DictionaryValue* entry = FindEntryByContentUrl(content_url);
if (!entry) {
base::MessageLoopProxy::current()->PostTask(
FROM_HERE,
base::Bind(download_action_callback, HTTP_NOT_FOUND, FilePath()));
return;
}
// Write the content URL as the content of the file.
if (static_cast<int>(content_url.spec().size()) !=
file_util::WriteFile(local_cache_path,
content_url.spec().data(),
content_url.spec().size())) {
base::MessageLoopProxy::current()->PostTask(
FROM_HERE,
base::Bind(download_action_callback, GDATA_FILE_ERROR, FilePath()));
return;
}
base::MessageLoopProxy::current()->PostTask(
FROM_HERE,
base::Bind(download_action_callback, HTTP_SUCCESS, local_cache_path));
}
void FakeDriveService::CopyHostedDocument(
......
......@@ -4,6 +4,8 @@
#include "chrome/browser/google_apis/fake_drive_service.h"
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/google_apis/gdata_wapi_parser.h"
......@@ -169,6 +171,56 @@ TEST_F(FakeDriveServiceTest, DeleteResource_NonexistingFile) {
EXPECT_EQ(HTTP_NOT_FOUND, error);
}
TEST_F(FakeDriveServiceTest, DownloadFile_ExistingFile) {
ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const GURL kContentUrl("https://file_content_url/");
const FilePath kOutputFilePath = temp_dir.path().AppendASCII("whatever.txt");
GDataErrorCode error = GDATA_OTHER_ERROR;
FilePath output_file_path;
fake_service_.DownloadFile(
FilePath::FromUTF8Unsafe("/drive/whatever.txt"), // virtual path
kOutputFilePath,
kContentUrl,
base::Bind(&test_util::CopyResultsFromDownloadActionCallback,
&error,
&output_file_path),
GetContentCallback());
message_loop_.RunUntilIdle();
EXPECT_EQ(HTTP_SUCCESS, error);
EXPECT_EQ(output_file_path, kOutputFilePath);
std::string content;
ASSERT_TRUE(file_util::ReadFileToString(output_file_path, &content));
EXPECT_EQ(kContentUrl.spec(), content);
}
TEST_F(FakeDriveServiceTest, DownloadFile_NonexistingFile) {
ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
base::ScopedTempDir temp_dir;
ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
const GURL kContentUrl("https://non_existing_content_url/");
const FilePath kOutputFilePath = temp_dir.path().AppendASCII("whatever.txt");
GDataErrorCode error = GDATA_OTHER_ERROR;
FilePath output_file_path;
fake_service_.DownloadFile(
FilePath::FromUTF8Unsafe("/drive/whatever.txt"), // virtual path
kOutputFilePath,
kContentUrl,
base::Bind(&test_util::CopyResultsFromDownloadActionCallback,
&error,
&output_file_path),
GetContentCallback());
message_loop_.RunUntilIdle();
EXPECT_EQ(HTTP_NOT_FOUND, error);
}
TEST_F(FakeDriveServiceTest, CopyHostedDocument_ExistingHostedDocument) {
ASSERT_TRUE(fake_service_.LoadResourceListForWapi("gdata/root_feed.json"));
......
......@@ -117,6 +117,15 @@ void CopyResultsFromGetAccountMetadataCallback(
*error_out = error_in;
}
void CopyResultsFromDownloadActionCallback(
GDataErrorCode* error_out,
FilePath* temp_file_out,
GDataErrorCode error_in,
const FilePath& temp_file_in) {
*error_out = error_in;
*temp_file_out = temp_file_in;
}
// Returns a HttpResponse created from the given file path.
scoped_ptr<test_server::HttpResponse> CreateHttpResponseFromFile(
const FilePath& file_path) {
......
......@@ -81,6 +81,13 @@ void CopyResultsFromGetAccountMetadataCallback(
GDataErrorCode error_in,
scoped_ptr<AccountMetadataFeed> account_metadata_in);
// Copies the results from DownloadActionCallback.
void CopyResultsFromDownloadActionCallback(
GDataErrorCode* error_out,
FilePath* temp_file_out,
GDataErrorCode error_in,
const FilePath& temp_file_in);
// Returns a HttpResponse created from the given file path.
scoped_ptr<test_server::HttpResponse> CreateHttpResponseFromFile(
const FilePath& file_path);
......
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