Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(801)

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_operations_unittest.cc

Issue 10808027: gdrive: Get JSON feeds parsing off the UI thread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Call NotifyFinish/NotifySuccess after parse. Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "base/memory/scoped_ptr.h"
6 #include "base/message_loop.h"
7 #include "base/values.h"
8 #include "chrome/browser/chromeos/gdata/gdata_operations.h"
9 #include "chrome/browser/chromeos/gdata/gdata_operation_runner.h"
10 #include "chrome/browser/chromeos/gdata/gdata_test_util.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/test/test_browser_thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace gdata {
17
18 namespace {
19
20 class JsonParseTestGetDataOperation : public GetDataOperation {
21 public:
22 JsonParseTestGetDataOperation(GDataOperationRegistry* registry,
23 Profile* profile,
24 const GetDataCallback& callback)
25 : GetDataOperation(registry, profile, callback) {
26 }
27
28 virtual ~JsonParseTestGetDataOperation() {
29 }
30
31 void NotifyStart() {
32 NotifyStartToOperationRegistry();
33 }
34
35 void NotifySuccess() {
36 NotifySuccessToOperationRegistry();
37 }
38
39 void NotifyFailure() {
40 NotifyFinish(GDataOperationRegistry::OPERATION_FAILED);
41 }
42
43 protected:
44 // GetDataOperation overrides:
45 virtual GURL GetURL() const OVERRIDE {
46 // This method is never called because this test does not fetch json from
47 // network.
48 NOTREACHED();
49 return GURL();
50 }
51 };
52
53 void GetDataOperationParseJsonCallback(GDataErrorCode* error_out,
54 scoped_ptr<base::Value>* value_out,
55 GDataErrorCode error_in,
56 scoped_ptr<base::Value> value_in) {
57 value_out->swap(value_in);
58 *error_out = error_in;
59 }
60
61 void ParseResponseResultCallback(bool* result_out,
62 bool result_in) {
63 *result_out = result_in;
64 }
65
66 } // namespace
67
68 class GDataOperationsTest : public testing::Test {
69 protected:
70 GDataOperationsTest()
71 : ui_thread_(content::BrowserThread::UI, &message_loop_) {
72 }
73
74 virtual void SetUp() OVERRIDE {
75 profile_.reset(new TestingProfile);
76 runner_.reset(new GDataOperationRunner(profile_.get()));
77 runner_->Initialize();
78 }
79
80 protected:
81 MessageLoopForUI message_loop_;
82 content::TestBrowserThread ui_thread_;
83 scoped_ptr<TestingProfile> profile_;
84 scoped_ptr<GDataOperationRunner> runner_;
85 };
86
87 TEST_F(GDataOperationsTest, GetDataOperationParseJson) {
88 scoped_ptr<base::Value> value;
89 GDataErrorCode error;
90 gdata::GetDataCallback cb = base::Bind(&GetDataOperationParseJsonCallback,
91 &error,
92 &value);
93 JsonParseTestGetDataOperation* getData =
94 new JsonParseTestGetDataOperation(runner_->operation_registry(),
95 profile_.get(),
96 cb);
97 getData->NotifyStart();
98
99 // Parses a valid json string.
100 {
101 std::string valid_json_str =
102 "{"
103 " \"test\": {"
104 " \"foo\": true,"
105 " \"bar\": 3.14,"
106 " \"baz\": \"bat\","
107 " \"moo\": \"cow\""
108 " },"
109 " \"list\": ["
110 " \"a\","
111 " \"b\""
112 " ]"
113 "}";
114
115 bool parse_result = false;
116 getData->ParseResponse(HTTP_SUCCESS,
117 valid_json_str,
118 base::Bind(&ParseResponseResultCallback,
119 &parse_result));
120 test_util::RunBlockingPoolTask();
121
122 EXPECT_EQ(HTTP_SUCCESS, error);
123 ASSERT_TRUE(value.get());
124 EXPECT_TRUE(parse_result);
125
126 DictionaryValue* root_dict = NULL;
127 ASSERT_TRUE(value->GetAsDictionary(&root_dict));
128
129 DictionaryValue* dict = NULL;
130 ListValue* list = NULL;
131 ASSERT_TRUE(root_dict->GetDictionary("test", &dict));
132 ASSERT_TRUE(root_dict->GetList("list", &list));
133
134 Value* dict_literals[2] = {0};
135 Value* dict_strings[2] = {0};
136 Value* list_values[2] = {0};
137 EXPECT_TRUE(dict->Get("foo", &dict_literals[0]));
138 EXPECT_TRUE(dict->Get("bar", &dict_literals[1]));
139 EXPECT_TRUE(dict->Get("baz", &dict_strings[0]));
140 EXPECT_TRUE(dict->Get("moo", &dict_strings[1]));
141 ASSERT_EQ(2u, list->GetSize());
142 EXPECT_TRUE(list->Get(0, &list_values[0]));
143 EXPECT_TRUE(list->Get(1, &list_values[1]));
144
145 bool b = false;
146 double d = 0;
147 std::string s;
148 EXPECT_TRUE(dict_literals[0]->GetAsBoolean(&b));
149 EXPECT_TRUE(b);
150 EXPECT_TRUE(dict_literals[1]->GetAsDouble(&d));
151 EXPECT_EQ(3.14, d);
152 EXPECT_TRUE(dict_strings[0]->GetAsString(&s));
153 EXPECT_EQ("bat", s);
154 EXPECT_TRUE(dict_strings[1]->GetAsString(&s));
155 EXPECT_EQ("cow", s);
156 EXPECT_TRUE(list_values[0]->GetAsString(&s));
157 EXPECT_EQ("a", s);
158 EXPECT_TRUE(list_values[1]->GetAsString(&s));
159 EXPECT_EQ("b", s);
160 }
161
162 getData->NotifySuccess();
163 }
164
165 TEST_F(GDataOperationsTest, GetDataOperationParseInvalidJson) {
166 scoped_ptr<base::Value> value;
167 GDataErrorCode error;
168 gdata::GetDataCallback cb = base::Bind(&GetDataOperationParseJsonCallback,
169 &error,
170 &value);
171 JsonParseTestGetDataOperation* getData =
172 new JsonParseTestGetDataOperation(runner_->operation_registry(),
173 profile_.get(),
174 cb);
175 getData->NotifyStart();
176
177 // Parses an invalid json string.
178 {
179 std::string invalid_json_str =
180 "/* hogehoge *"
181 " \"test\": {"
182 " \"moo\": \"cow"
183 " "
184 " \"list\": ["
185 " \"foo\","
186 " \"bar\""
187 " ]";
188
189 bool parse_result = true;
190 getData->ParseResponse(HTTP_SUCCESS,
191 invalid_json_str,
192 base::Bind(&ParseResponseResultCallback,
193 &parse_result));
194 test_util::RunBlockingPoolTask();
195
196 ASSERT_TRUE(value.get() == NULL);
197 EXPECT_EQ(GDATA_PARSE_ERROR, error);
198
199 EXPECT_FALSE(parse_result);
200 }
201
202 getData->NotifyFailure();
203 }
204
205 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698