Fix build break
[chromium-blink-merge.git] / chrome / browser / google_apis / base_operations_unittest.cc
blob047e5c284b23ead6f1ba17a7849cb0d9b1140bea
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/google_apis/base_operations.h"
7 #include "base/bind.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop.h"
10 #include "base/values.h"
11 #include "chrome/browser/google_apis/operation_runner.h"
12 #include "chrome/browser/google_apis/test_util.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "content/public/test/test_browser_thread.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 namespace google_apis {
19 namespace {
21 const char kValidJsonString[] = "{ \"test\": 123 }";
22 const char kInvalidJsonString[] = "$$$";
24 class FakeGetDataOperation : public GetDataOperation {
25 public:
26 explicit FakeGetDataOperation(OperationRegistry* registry,
27 const GetDataCallback& callback)
28 : GetDataOperation(registry, NULL, callback) {
31 virtual ~FakeGetDataOperation() {
34 void NotifyStart() {
35 NotifyStartToOperationRegistry();
38 protected:
39 virtual GURL GetURL() const OVERRIDE {
40 NOTREACHED(); // This method is not called in tests.
41 return GURL();
45 } // namespace
47 class BaseOperationsTest : public testing::Test {
48 public:
49 BaseOperationsTest()
50 : ui_thread_(content::BrowserThread::UI, &message_loop_),
51 parse_json_callback_called_(false),
52 get_data_callback_called_(false) {
55 void ParseJsonCallback(scoped_ptr<base::Value> value) {
56 parse_json_result_ = value.Pass();;
57 parse_json_callback_called_ = true;
60 void GetDataCallback(GDataErrorCode error, scoped_ptr<base::Value> value) {
61 get_data_result_error_ = error;
62 get_data_result_value_ = value.Pass();
63 get_data_callback_called_ = true;
66 virtual void SetUp() OVERRIDE {
67 profile_.reset(new TestingProfile);
68 runner_.reset(new OperationRunner(profile_.get(),
69 NULL /* url_request_context_getter */,
70 std::vector<std::string>() /* scopes */,
71 std::string() /* custom user agent */));
72 runner_->Initialize();
73 LOG(ERROR) << "Initialized.";
76 MessageLoopForUI message_loop_;
77 content::TestBrowserThread ui_thread_;
78 scoped_ptr<TestingProfile> profile_;
79 scoped_ptr<OperationRunner> runner_;
81 // Following members stores data returned with callbacks to be verified
82 // by tests.
83 scoped_ptr<base::Value> parse_json_result_;
84 bool parse_json_callback_called_;
85 GDataErrorCode get_data_result_error_;
86 scoped_ptr<base::Value> get_data_result_value_;
87 bool get_data_callback_called_;
90 TEST_F(BaseOperationsTest, ParseValidJson) {
91 ParseJson(kValidJsonString,
92 base::Bind(&BaseOperationsTest::ParseJsonCallback,
93 base::Unretained(this)));
94 // Should wait for a blocking pool task, as the JSON parsing is done in the
95 // blocking pool.
96 test_util::RunBlockingPoolTask();
98 ASSERT_TRUE(parse_json_callback_called_);
99 ASSERT_TRUE(parse_json_result_.get());
101 DictionaryValue* root_dict = NULL;
102 ASSERT_TRUE(parse_json_result_->GetAsDictionary(&root_dict));
104 int int_value = 0;
105 ASSERT_TRUE(root_dict->GetInteger("test", &int_value));
106 EXPECT_EQ(123, int_value);
109 TEST_F(BaseOperationsTest, ParseInvalidJson) {
110 ParseJson(kInvalidJsonString,
111 base::Bind(&BaseOperationsTest::ParseJsonCallback,
112 base::Unretained(this)));
113 // Should wait for a blocking pool task, as the JSON parsing is done in the
114 // blocking pool.
115 test_util::RunBlockingPoolTask();
117 ASSERT_TRUE(parse_json_callback_called_);
118 ASSERT_FALSE(parse_json_result_.get());
121 TEST_F(BaseOperationsTest, GetDataOperationParseValidResponse) {
122 FakeGetDataOperation* get_data_operation =
123 new FakeGetDataOperation(
124 runner_->operation_registry(),
125 base::Bind(&BaseOperationsTest::GetDataCallback,
126 base::Unretained(this)));
127 get_data_operation->NotifyStart();
129 get_data_operation->ParseResponse(HTTP_SUCCESS, kValidJsonString);
130 // Should wait for a blocking pool task, as the JSON parsing is done in the
131 // blocking pool.
132 test_util::RunBlockingPoolTask();
134 ASSERT_TRUE(get_data_callback_called_);
135 ASSERT_EQ(HTTP_SUCCESS, get_data_result_error_);
136 ASSERT_TRUE(get_data_result_value_.get());
139 TEST_F(BaseOperationsTest, GetDataOperationParseInvalidResponse) {
140 FakeGetDataOperation* get_data_operation =
141 new FakeGetDataOperation(
142 runner_->operation_registry(),
143 base::Bind(&BaseOperationsTest::GetDataCallback,
144 base::Unretained(this)));
145 get_data_operation->NotifyStart();
147 get_data_operation->ParseResponse(HTTP_SUCCESS, kInvalidJsonString);
148 // Should wait for a blocking pool task, as the JSON parsing is done in the
149 // blocking pool.
150 test_util::RunBlockingPoolTask();
152 ASSERT_TRUE(get_data_callback_called_);
153 ASSERT_EQ(GDATA_PARSE_ERROR, get_data_result_error_);
154 ASSERT_FALSE(get_data_result_value_.get());
157 } // namespace google_apis