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
|
// 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 "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/values.h"
#include "chrome/browser/google_apis/base_operations.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 "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,
const GetDataCallback& callback)
: GetDataOperation(registry, callback) {
}
virtual ~JsonParseTestGetDataOperation() {
}
void NotifyStart() {
NotifyStartToOperationRegistry();
}
protected:
// GetDataOperation overrides:
virtual GURL GetURL() const OVERRIDE {
// This method is never called because this test does not fetch json from
// network.
NOTREACHED();
return GURL();
}
};
// Copies the results from GetDataCallback.
void CopyResultsFromGetDataCallback(GDataErrorCode* error_out,
scoped_ptr<base::Value>* value_out,
GDataErrorCode error_in,
scoped_ptr<base::Value> value_in) {
value_out->swap(value_in);
*error_out = error_in;
}
} // namespace
class BaseOperationsTest : public testing::Test {
protected:
BaseOperationsTest()
: ui_thread_(content::BrowserThread::UI, &message_loop_) {
}
virtual void SetUp() OVERRIDE {
profile_.reset(new TestingProfile);
runner_.reset(new OperationRunner(profile_.get(),
std::vector<std::string>() /* scopes */,
"" /* custom_user_agent*/));
runner_->Initialize();
}
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(),
base::Bind(&CopyResultsFromGetDataCallback, &error, &value));
get_data->NotifyStart();
const std::string valid_json_str = "{ \"test\": 123 }";
get_data->ParseResponse(HTTP_SUCCESS, valid_json_str);
// 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());
DictionaryValue* root_dict = NULL;
ASSERT_TRUE(value->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(
runner_->operation_registry(),
base::Bind(&CopyResultsFromGetDataCallback, &error, &value));
get_data->NotifyStart();
const std::string invalid_json_str = "$$$";
get_data->ParseResponse(HTTP_SUCCESS, invalid_json_str);
test_util::RunBlockingPoolTask();
// The parsing should fail.
EXPECT_EQ(GDATA_PARSE_ERROR, error);
ASSERT_FALSE(value.get());
}
} // namespace google_apis
|