| Index: chrome/browser/google_apis/base_operations_unittest.cc
|
| diff --git a/chrome/browser/google_apis/base_operations_unittest.cc b/chrome/browser/google_apis/base_operations_unittest.cc
|
| index 8a5b52bad562909dd84f8358eee92a5b7bbe7286..2f7e01a74e73028ce3d2ac7baf9bde356c7d5f90 100644
|
| --- a/chrome/browser/google_apis/base_operations_unittest.cc
|
| +++ b/chrome/browser/google_apis/base_operations_unittest.cc
|
| @@ -4,33 +4,31 @@
|
|
|
| #include "chrome/browser/google_apis/base_operations.h"
|
|
|
| +#include "base/bind.h"
|
| #include "base/memory/scoped_ptr.h"
|
| #include "base/message_loop.h"
|
| #include "base/values.h"
|
| #include "chrome/browser/google_apis/operation_runner.h"
|
| #include "chrome/browser/google_apis/test_util.h"
|
| #include "chrome/test/base/testing_profile.h"
|
| -#include "content/public/browser/browser_thread.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 {
|
|
|
| -// The class is used to test that the JSON parsing is done in the blocking
|
| -// pool, instead of UI thread.
|
| -class JsonParseTestGetDataOperation : public GetDataOperation {
|
| +const char kValidJsonString[] = "{ \"test\": 123 }";
|
| +const char kInvalidJsonString[] = "$$$";
|
| +
|
| +class FakeGetDataOperation : public GetDataOperation {
|
| public:
|
| - JsonParseTestGetDataOperation(
|
| - OperationRegistry* registry,
|
| - net::URLRequestContextGetter* url_request_context_getter,
|
| - const GetDataCallback& callback)
|
| - : GetDataOperation(registry, url_request_context_getter, callback) {
|
| + explicit FakeGetDataOperation(OperationRegistry* registry,
|
| + const GetDataCallback& callback)
|
| + : GetDataOperation(registry, NULL, callback) {
|
| }
|
|
|
| - virtual ~JsonParseTestGetDataOperation() {
|
| + virtual ~FakeGetDataOperation() {
|
| }
|
|
|
| void NotifyStart() {
|
| @@ -38,11 +36,8 @@ class JsonParseTestGetDataOperation : public GetDataOperation {
|
| }
|
|
|
| protected:
|
| - // GetDataOperation overrides:
|
| virtual GURL GetURL() const OVERRIDE {
|
| - // This method is never called because this test does not fetch json from
|
| - // network.
|
| - NOTREACHED();
|
| + NOTREACHED(); // This method is not called in tests.
|
| return GURL();
|
| }
|
| };
|
| @@ -50,77 +45,113 @@ class JsonParseTestGetDataOperation : public GetDataOperation {
|
| } // namespace
|
|
|
| class BaseOperationsTest : public testing::Test {
|
| - protected:
|
| + public:
|
| BaseOperationsTest()
|
| - : ui_thread_(content::BrowserThread::UI, &message_loop_) {
|
| + : ui_thread_(content::BrowserThread::UI, &message_loop_),
|
| + parse_json_callback_called_(false),
|
| + get_data_callback_called_(false) {
|
| + }
|
| +
|
| + void ParseJsonCallback(scoped_ptr<base::Value> value) {
|
| + parse_json_result_ = value.Pass();;
|
| + parse_json_callback_called_ = true;
|
| + }
|
| +
|
| + void GetDataCallback(GDataErrorCode error, scoped_ptr<base::Value> value) {
|
| + get_data_result_error_ = error;
|
| + get_data_result_value_ = value.Pass();
|
| + get_data_callback_called_ = true;
|
| }
|
|
|
| virtual void SetUp() OVERRIDE {
|
| profile_.reset(new TestingProfile);
|
| runner_.reset(new OperationRunner(profile_.get(),
|
| NULL /* url_request_context_getter */,
|
| - std::vector<std::string>() /* scopes */,
|
| - "" /* custom_user_agent*/));
|
| + std::vector<std::string>() /* scopes */,
|
| + "" /* custom user agent */));
|
| runner_->Initialize();
|
| + LOG(ERROR) << "Initialized.";
|
| }
|
|
|
| - protected:
|
| MessageLoopForUI message_loop_;
|
| content::TestBrowserThread ui_thread_;
|
| scoped_ptr<TestingProfile> profile_;
|
| scoped_ptr<OperationRunner> runner_;
|
| -};
|
| -
|
| -TEST_F(BaseOperationsTest, GetDataOperation_ParseValidJson) {
|
| - scoped_ptr<base::Value> value;
|
| - GDataErrorCode error = GDATA_OTHER_ERROR;
|
| - JsonParseTestGetDataOperation* get_data =
|
| - new JsonParseTestGetDataOperation(
|
| - runner_->operation_registry(),
|
| - NULL, // request_context_getter.
|
| - base::Bind(&test_util::CopyResultsFromGetDataCallback,
|
| - &error,
|
| - &value));
|
| - get_data->NotifyStart();
|
|
|
| - const std::string valid_json_str = "{ \"test\": 123 }";
|
| + // Following members stores data returned with callbacks to be verified
|
| + // by tests.
|
| + scoped_ptr<base::Value> parse_json_result_;
|
| + bool parse_json_callback_called_;
|
| + GDataErrorCode get_data_result_error_;
|
| + scoped_ptr<base::Value> get_data_result_value_;
|
| + bool get_data_callback_called_;
|
| +};
|
|
|
| - get_data->ParseResponse(HTTP_SUCCESS, valid_json_str);
|
| +TEST_F(BaseOperationsTest, ParseValidJson) {
|
| + ParseJson(kValidJsonString,
|
| + base::Bind(&BaseOperationsTest::ParseJsonCallback,
|
| + base::Unretained(this)));
|
| // Should wait for a blocking pool task, as the JSON parsing is done in the
|
| // blocking pool.
|
| test_util::RunBlockingPoolTask();
|
|
|
| - EXPECT_EQ(HTTP_SUCCESS, error);
|
| - ASSERT_TRUE(value.get());
|
| + ASSERT_TRUE(parse_json_callback_called_);
|
| + ASSERT_TRUE(parse_json_result_.get());
|
|
|
| DictionaryValue* root_dict = NULL;
|
| - ASSERT_TRUE(value->GetAsDictionary(&root_dict));
|
| + ASSERT_TRUE(parse_json_result_->GetAsDictionary(&root_dict));
|
|
|
| int int_value = 0;
|
| ASSERT_TRUE(root_dict->GetInteger("test", &int_value));
|
| EXPECT_EQ(123, int_value);
|
| }
|
|
|
| -TEST_F(BaseOperationsTest, GetDataOperation_ParseInvalidJson) {
|
| - scoped_ptr<base::Value> value;
|
| - GDataErrorCode error = GDATA_OTHER_ERROR;
|
| - JsonParseTestGetDataOperation* get_data =
|
| - new JsonParseTestGetDataOperation(
|
| +TEST_F(BaseOperationsTest, ParseInvalidJson) {
|
| + ParseJson(kInvalidJsonString,
|
| + base::Bind(&BaseOperationsTest::ParseJsonCallback,
|
| + base::Unretained(this)));
|
| + // Should wait for a blocking pool task, as the JSON parsing is done in the
|
| + // blocking pool.
|
| + test_util::RunBlockingPoolTask();
|
| +
|
| + ASSERT_TRUE(parse_json_callback_called_);
|
| + ASSERT_FALSE(parse_json_result_.get());
|
| +}
|
| +
|
| +TEST_F(BaseOperationsTest, GetDataOperationParseValidResponse) {
|
| + FakeGetDataOperation* get_data_operation =
|
| + new FakeGetDataOperation(
|
| runner_->operation_registry(),
|
| - NULL, // request_context_getter.
|
| - base::Bind(&test_util::CopyResultsFromGetDataCallback,
|
| - &error,
|
| - &value));
|
| - get_data->NotifyStart();
|
| + base::Bind(&BaseOperationsTest::GetDataCallback,
|
| + base::Unretained(this)));
|
| + get_data_operation->NotifyStart();
|
|
|
| - const std::string invalid_json_str = "$$$";
|
| + get_data_operation->ParseResponse(HTTP_SUCCESS, kValidJsonString);
|
| + // Should wait for a blocking pool task, as the JSON parsing is done in the
|
| + // blocking pool.
|
| + test_util::RunBlockingPoolTask();
|
|
|
| - get_data->ParseResponse(HTTP_SUCCESS, invalid_json_str);
|
| + ASSERT_TRUE(get_data_callback_called_);
|
| + ASSERT_EQ(HTTP_SUCCESS, get_data_result_error_);
|
| + ASSERT_TRUE(get_data_result_value_.get());
|
| +}
|
| +
|
| +TEST_F(BaseOperationsTest, GetDataOperationParseInvalidResponse) {
|
| + FakeGetDataOperation* get_data_operation =
|
| + new FakeGetDataOperation(
|
| + runner_->operation_registry(),
|
| + base::Bind(&BaseOperationsTest::GetDataCallback,
|
| + base::Unretained(this)));
|
| + get_data_operation->NotifyStart();
|
| +
|
| + get_data_operation->ParseResponse(HTTP_SUCCESS, kInvalidJsonString);
|
| + // Should wait for a blocking pool task, as the JSON parsing is done in the
|
| + // blocking pool.
|
| test_util::RunBlockingPoolTask();
|
|
|
| - // The parsing should fail.
|
| - EXPECT_EQ(GDATA_PARSE_ERROR, error);
|
| - ASSERT_FALSE(value.get());
|
| + ASSERT_TRUE(get_data_callback_called_);
|
| + ASSERT_EQ(GDATA_PARSE_ERROR, get_data_result_error_);
|
| + ASSERT_FALSE(get_data_result_value_.get());
|
| }
|
|
|
| } // namespace google_apis
|
|
|