Commit 9c84c56b authored by hidehiko@chromium.org's avatar hidehiko@chromium.org

Add unittest for google_apis::GetAboutOperation.

This CL adds test cases for google_apis::GetAboutOperation.
Also, it naturally introduces the testing base code for the operations
communicating with a drive api server, so that it will be reused for unit tests
of other operation classes in drive_api_operations.{h,cc} later.

BUG=162155
TEST=Ran unit_tests


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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@176066 0039d316-1c4b-4281-b951-d872f2087c98
parent 20c46c0e
// Copyright (c) 2013 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 "base/bind.h"
#include "base/file_path.h"
#include "base/message_loop_proxy.h"
#include "base/values.h"
#include "chrome/browser/google_apis/drive_api_operations.h"
#include "chrome/browser/google_apis/drive_api_url_generator.h"
#include "chrome/browser/google_apis/operation_registry.h"
#include "chrome/browser/google_apis/test_server/http_server.h"
#include "chrome/browser/google_apis/test_util.h"
#include "content/public/test/test_browser_thread.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace google_apis {
namespace {
const char kTestDriveApiAuthToken[] = "testtoken";
const char kTestUserAgent[] = "test-user-agent";
} // namespace
class DriveApiOperationsTest : public testing::Test {
public:
DriveApiOperationsTest()
: ui_thread_(content::BrowserThread::UI, &message_loop_),
file_thread_(content::BrowserThread::FILE),
io_thread_(content::BrowserThread::IO) {
}
virtual void SetUp() OVERRIDE {
file_thread_.Start();
io_thread_.StartIOThread();
request_context_getter_ = new net::TestURLRequestContextGetter(
content::BrowserThread::GetMessageLoopProxyForThread(
content::BrowserThread::IO));
ASSERT_TRUE(test_server_.InitializeAndWaitUntilReady());
test_server_.RegisterRequestHandler(
base::Bind(&DriveApiOperationsTest::HandleDataFileRequest,
base::Unretained(this)));
url_generator_.reset(new DriveApiUrlGenerator(
test_util::GetBaseUrlForTesting(test_server_.port())));
}
virtual void TearDown() OVERRIDE {
test_server_.ShutdownAndWaitUntilComplete();
request_context_getter_ = NULL;
expected_data_file_path_.clear();
}
MessageLoopForUI message_loop_;
content::TestBrowserThread ui_thread_;
content::TestBrowserThread file_thread_;
content::TestBrowserThread io_thread_;
test_server::HttpServer test_server_;
OperationRegistry operation_registry_;
scoped_ptr<DriveApiUrlGenerator> url_generator_;
scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
// This is a path to the file which contains expected response from
// the server. See also HandleDataFileRequest below.
FilePath expected_data_file_path_;
// The incoming HTTP request is saved so tests can verify the request
// parameters like HTTP method (ex. some operations should use DELETE
// instead of GET).
test_server::HttpRequest http_request_;
private:
// Reads the data file of |expected_data_file_path_| and returns its content
// for the request.
// To use this method, it is necessary to set |expected_data_file_path_|
// to the appropriate file path before sending the request to the server.
scoped_ptr<test_server::HttpResponse> HandleDataFileRequest(
const test_server::HttpRequest& request) {
http_request_ = request;
if (expected_data_file_path_.empty()) {
// The file is not specified. Delegate the processing to the next
// handler.
return scoped_ptr<test_server::HttpResponse>();
}
// Return the response from the data file.
return test_util::CreateHttpResponseFromFile(expected_data_file_path_);
}
};
TEST_F(DriveApiOperationsTest, GetAboutOperation_ValidFeed) {
// Set an expected data file containing valid result.
expected_data_file_path_ = test_util::GetTestFilePath("drive/about.json");
GDataErrorCode error = GDATA_OTHER_ERROR;
scoped_ptr<base::Value> feed_data;
GetAboutOperation* operation = new GetAboutOperation(
&operation_registry_,
request_context_getter_.get(),
*url_generator_,
base::Bind(&test_util::CopyResultsFromGetDataCallbackAndQuit,
&error, &feed_data));
operation->Start(kTestDriveApiAuthToken, kTestUserAgent,
base::Bind(&test_util::DoNothingForReAuthenticateCallback));
MessageLoop::current()->Run();
EXPECT_EQ(HTTP_SUCCESS, error);
EXPECT_EQ(test_server::METHOD_GET, http_request_.method);
EXPECT_EQ("/drive/v2/about", http_request_.relative_url);
EXPECT_TRUE(test_util::VerifyJsonData(
test_util::GetTestFilePath("drive/about.json"), feed_data.get()));
}
TEST_F(DriveApiOperationsTest, GetAboutOperation_InvalidFeed) {
// Set an expected data file containing invalid result.
expected_data_file_path_ = test_util::GetTestFilePath("gdata/testfile.txt");
GDataErrorCode error = GDATA_OTHER_ERROR;
scoped_ptr<base::Value> feed_data;
GetAboutOperation* operation = new GetAboutOperation(
&operation_registry_,
request_context_getter_.get(),
*url_generator_,
base::Bind(&test_util::CopyResultsFromGetDataCallbackAndQuit,
&error, &feed_data));
operation->Start(kTestDriveApiAuthToken, kTestUserAgent,
base::Bind(&test_util::DoNothingForReAuthenticateCallback));
MessageLoop::current()->Run();
// "parse error" should be returned, and the feed should be NULL.
EXPECT_EQ(GDATA_PARSE_ERROR, error);
EXPECT_EQ(test_server::METHOD_GET, http_request_.method);
EXPECT_EQ("/drive/v2/about", http_request_.relative_url);
EXPECT_FALSE(feed_data.get());
}
} // namespace google_apis
......@@ -6,6 +6,7 @@
#include "base/file_util.h"
#include "base/json/json_file_value_serializer.h"
#include "base/json/json_reader.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/string_util.h"
......@@ -90,6 +91,15 @@ void CopyResultsFromGetDataCallback(GDataErrorCode* error_out,
*error_out = error_in;
}
void CopyResultsFromGetDataCallbackAndQuit(GDataErrorCode* error_out,
scoped_ptr<base::Value>* value_out,
GDataErrorCode error_in,
scoped_ptr<base::Value> value_in) {
*error_out = error_in;
*value_out = value_in.Pass();
MessageLoop::current()->Quit();
}
void CopyResultsFromGetResourceEntryCallback(
GDataErrorCode* error_out,
scoped_ptr<ResourceEntry>* resource_entry_out,
......@@ -148,5 +158,35 @@ scoped_ptr<test_server::HttpResponse> CreateHttpResponseFromFile(
return http_response.Pass();
}
void DoNothingForReAuthenticateCallback(
AuthenticatedOperationInterface* /* operation */) {
NOTREACHED();
}
bool VerifyJsonData(const FilePath& expected_json_file_path,
const base::Value* json_data) {
if (!json_data) {
LOG(ERROR) << "json_data is NULL";
return false;
}
std::string expected_content;
if (!file_util::ReadFileToString(
expected_json_file_path, &expected_content)) {
LOG(ERROR) << "Failed to read file: " << expected_json_file_path.value();
return false;
}
scoped_ptr<base::Value> expected_json_data(
base::JSONReader::Read(expected_content));
if (!base::Value::Equals(expected_json_data.get(), json_data)) {
LOG(ERROR)
<< "The value of json_data is different from the file's content.";
return false;
}
return true;
}
} // namespace test_util
} // namespace google_apis
......@@ -20,6 +20,7 @@ class Value;
namespace google_apis {
class AccountMetadataFeed;
class AuthenticatedOperationInterface;
class ResourceEntry;
class ResourceList;
......@@ -60,6 +61,12 @@ void CopyResultsFromGetDataCallback(GDataErrorCode* error_out,
GDataErrorCode error_in,
scoped_ptr<base::Value> value_in);
// Copies the results from GetDataCallback and quit the message loop.
void CopyResultsFromGetDataCallbackAndQuit(GDataErrorCode* error_out,
scoped_ptr<base::Value>* value_out,
GDataErrorCode error_in,
scoped_ptr<base::Value> value_in);
// Copies the results from GetResourceEntryCallback.
void CopyResultsFromGetResourceEntryCallback(
GDataErrorCode* error_out,
......@@ -92,6 +99,18 @@ void CopyResultsFromDownloadActionCallback(
scoped_ptr<test_server::HttpResponse> CreateHttpResponseFromFile(
const FilePath& file_path);
// Does nothing for ReAuthenticateCallback(). This function should be used
// if it is not expected to reach this method as there won't be any
// authentication failures in the test.
void DoNothingForReAuthenticateCallback(
AuthenticatedOperationInterface* operation);
// Returns true if |json_data| is not NULL and equals to the content in
// |expected_json_file_path|. The failure reason will be logged into LOG(ERROR)
// if necessary.
bool VerifyJsonData(const FilePath& expected_json_file_path,
const base::Value* json_data);
} // namespace test_util
} // namespace google_apis
......
......@@ -761,6 +761,7 @@
'browser/google/google_url_tracker_unittest.cc',
'browser/google/google_util_unittest.cc',
'browser/google_apis/base_operations_unittest.cc',
'browser/google_apis/drive_api_operations_unittest.cc',
'browser/google_apis/drive_api_parser_unittest.cc',
'browser/google_apis/drive_api_url_generator_unittest.cc',
'browser/google_apis/drive_uploader_unittest.cc',
......@@ -2134,6 +2135,7 @@
'browser/sessions/session_backend_unittest.cc',
# Test files cannot be opened on Android.
'browser/google_apis/drive_api_operations_unittest.cc',
'browser/google_apis/fake_drive_service_unittest.cc',
'browser/google_apis/gdata_wapi_operations_unittest.cc',
'browser/google_apis/gdata_wapi_parser_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