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..135a397fde22203705eb739130d05f2705b6163d 100644 |
--- a/chrome/browser/google_apis/base_operations_unittest.cc |
+++ b/chrome/browser/google_apis/base_operations_unittest.cc |
@@ -4,45 +4,29 @@ |
#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 { |
- public: |
- JsonParseTestGetDataOperation( |
- OperationRegistry* registry, |
- net::URLRequestContextGetter* url_request_context_getter, |
- const GetDataCallback& callback) |
- : GetDataOperation(registry, url_request_context_getter, callback) { |
- } |
- |
- virtual ~JsonParseTestGetDataOperation() { |
- } |
+const char kValidJsonString[] = "{ \"test\": 123 }"; |
+const char kInvalidJsonString[] = "$$$"; |
- void NotifyStart() { |
- NotifyStartToOperationRegistry(); |
+class FakeGetDataOperation : public GetDataOperation { |
+ public: |
+ explicit FakeGetDataOperation(const GetDataCallback& callback) |
+ : GetDataOperation(NULL, NULL, callback) { |
} |
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,48 +34,43 @@ class JsonParseTestGetDataOperation : public GetDataOperation { |
} // namespace |
class BaseOperationsTest : public testing::Test { |
- protected: |
- BaseOperationsTest() |
- : ui_thread_(content::BrowserThread::UI, &message_loop_) { |
+ public: |
+ BaseOperationsTest() : parse_json_callback_called(false), |
+ get_data_callback_called(false) { |
} |
- 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*/)); |
- runner_->Initialize(); |
+ void ParseJsonCallback(scoped_ptr<base::Value> value) { |
+ parse_json_result = value.Pass();; |
+ parse_json_callback_called = true; |
} |
- protected: |
- MessageLoopForUI message_loop_; |
- content::TestBrowserThread ui_thread_; |
- scoped_ptr<TestingProfile> profile_; |
- scoped_ptr<OperationRunner> runner_; |
+ 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; |
+ } |
+ |
+ // 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; |
satorux1
2012/12/14 05:02:42
class data members should end with _;
mtomasz
2012/12/14 05:43:48
Done.
|
}; |
-TEST_F(BaseOperationsTest, GetDataOperation_ParseValidJson) { |
+TEST_F(BaseOperationsTest, 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 }"; |
- |
- get_data->ParseResponse(HTTP_SUCCESS, valid_json_str); |
+ |
+ 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)); |
@@ -101,26 +80,50 @@ TEST_F(BaseOperationsTest, GetDataOperation_ParseValidJson) { |
EXPECT_EQ(123, int_value); |
} |
-TEST_F(BaseOperationsTest, GetDataOperation_ParseInvalidJson) { |
+TEST_F(BaseOperationsTest, ParseInvalidJson) { |
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 invalid_json_str = "$$$"; |
- |
- get_data->ParseResponse(HTTP_SUCCESS, invalid_json_str); |
+ |
+ 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( |
+ base::Bind(&BaseOperationsTest::GetDataCallback, |
+ base::Unretained(this))); |
+ |
+ 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(); |
+ |
+ 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( |
+ base::Bind(&BaseOperationsTest::GetDataCallback, |
+ base::Unretained(this))); |
+ |
+ 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_TRUE(get_data_result_value.get()); |
} |
} // namespace google_apis |