summaryrefslogtreecommitdiffstats
path: root/chrome/browser/google_apis/base_operations_unittest.cc
blob: 2f7e01a74e73028ce3d2ac7baf9bde356c7d5f90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) 2012 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 "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/test/test_browser_thread.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace google_apis {

namespace {

const char kValidJsonString[] = "{ \"test\": 123 }";
const char kInvalidJsonString[] = "$$$";

class FakeGetDataOperation : public GetDataOperation {
 public:
  explicit FakeGetDataOperation(OperationRegistry* registry,
                                const GetDataCallback& callback)
      : GetDataOperation(registry, NULL, callback) {
  }

  virtual ~FakeGetDataOperation() {
  }

  void NotifyStart() {
    NotifyStartToOperationRegistry();
  }

 protected:
  virtual GURL GetURL() const OVERRIDE {
    NOTREACHED();  // This method is not called in tests.
    return GURL();
  }
};

}  // namespace

class BaseOperationsTest : public testing::Test {
 public:
  BaseOperationsTest()
      : 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 */));
    runner_->Initialize();
    LOG(ERROR) << "Initialized.";
  }

  MessageLoopForUI message_loop_;
  content::TestBrowserThread ui_thread_;
  scoped_ptr<TestingProfile> profile_;
  scoped_ptr<OperationRunner> runner_;

  // 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_;
};

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();

  ASSERT_TRUE(parse_json_callback_called_);
  ASSERT_TRUE(parse_json_result_.get());

  DictionaryValue* root_dict = NULL;
  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, 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(),
          base::Bind(&BaseOperationsTest::GetDataCallback,
                     base::Unretained(this)));
  get_data_operation->NotifyStart();

  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 =
      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();

  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