OLD | NEW |
| (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 } // namespace | |
62 | |
63 class GDataOperationsTest : public testing::Test { | |
64 protected: | |
65 GDataOperationsTest() | |
66 : ui_thread_(content::BrowserThread::UI, &message_loop_) { | |
67 } | |
68 | |
69 virtual void SetUp() OVERRIDE { | |
70 profile_.reset(new TestingProfile); | |
71 runner_.reset(new GDataOperationRunner(profile_.get())); | |
72 runner_->Initialize(); | |
73 } | |
74 | |
75 protected: | |
76 MessageLoopForUI message_loop_; | |
77 content::TestBrowserThread ui_thread_; | |
78 scoped_ptr<TestingProfile> profile_; | |
79 scoped_ptr<GDataOperationRunner> runner_; | |
80 }; | |
81 | |
82 TEST_F(GDataOperationsTest, GetDataOperationParseJson) { | |
83 scoped_ptr<base::Value> value; | |
84 GDataErrorCode error; | |
85 gdata::GetDataCallback cb = base::Bind(&GetDataOperationParseJsonCallback, | |
86 &error, | |
87 &value); | |
88 JsonParseTestGetDataOperation* getData = | |
89 new JsonParseTestGetDataOperation(runner_->operation_registry(), | |
90 profile_.get(), | |
91 cb); | |
92 getData->NotifyStart(); | |
93 | |
94 // Parses a valid json string. | |
95 { | |
96 std::string valid_json_str = | |
97 "{" | |
98 " \"test\": {" | |
99 " \"foo\": true," | |
100 " \"bar\": 3.14," | |
101 " \"baz\": \"bat\"," | |
102 " \"moo\": \"cow\"" | |
103 " }," | |
104 " \"list\": [" | |
105 " \"a\"," | |
106 " \"b\"" | |
107 " ]" | |
108 "}"; | |
109 | |
110 getData->ParseResponse(HTTP_SUCCESS, valid_json_str); | |
111 test_util::RunBlockingPoolTask(); | |
112 | |
113 EXPECT_EQ(HTTP_SUCCESS, error); | |
114 ASSERT_TRUE(value.get()); | |
115 | |
116 DictionaryValue* root_dict = NULL; | |
117 ASSERT_TRUE(value->GetAsDictionary(&root_dict)); | |
118 | |
119 DictionaryValue* dict = NULL; | |
120 ListValue* list = NULL; | |
121 ASSERT_TRUE(root_dict->GetDictionary("test", &dict)); | |
122 ASSERT_TRUE(root_dict->GetList("list", &list)); | |
123 | |
124 Value* dict_literals[2] = {0}; | |
125 Value* dict_strings[2] = {0}; | |
126 Value* list_values[2] = {0}; | |
127 EXPECT_TRUE(dict->Get("foo", &dict_literals[0])); | |
128 EXPECT_TRUE(dict->Get("bar", &dict_literals[1])); | |
129 EXPECT_TRUE(dict->Get("baz", &dict_strings[0])); | |
130 EXPECT_TRUE(dict->Get("moo", &dict_strings[1])); | |
131 ASSERT_EQ(2u, list->GetSize()); | |
132 EXPECT_TRUE(list->Get(0, &list_values[0])); | |
133 EXPECT_TRUE(list->Get(1, &list_values[1])); | |
134 | |
135 bool b = false; | |
136 double d = 0; | |
137 std::string s; | |
138 EXPECT_TRUE(dict_literals[0]->GetAsBoolean(&b)); | |
139 EXPECT_TRUE(b); | |
140 EXPECT_TRUE(dict_literals[1]->GetAsDouble(&d)); | |
141 EXPECT_EQ(3.14, d); | |
142 EXPECT_TRUE(dict_strings[0]->GetAsString(&s)); | |
143 EXPECT_EQ("bat", s); | |
144 EXPECT_TRUE(dict_strings[1]->GetAsString(&s)); | |
145 EXPECT_EQ("cow", s); | |
146 EXPECT_TRUE(list_values[0]->GetAsString(&s)); | |
147 EXPECT_EQ("a", s); | |
148 EXPECT_TRUE(list_values[1]->GetAsString(&s)); | |
149 EXPECT_EQ("b", s); | |
150 } | |
151 } | |
152 | |
153 TEST_F(GDataOperationsTest, GetDataOperationParseInvalidJson) { | |
154 scoped_ptr<base::Value> value; | |
155 GDataErrorCode error; | |
156 gdata::GetDataCallback cb = base::Bind(&GetDataOperationParseJsonCallback, | |
157 &error, | |
158 &value); | |
159 JsonParseTestGetDataOperation* getData = | |
160 new JsonParseTestGetDataOperation(runner_->operation_registry(), | |
161 profile_.get(), | |
162 cb); | |
163 getData->NotifyStart(); | |
164 | |
165 // Parses an invalid json string. | |
166 { | |
167 std::string invalid_json_str = | |
168 "/* hogehoge *" | |
169 " \"test\": {" | |
170 " \"moo\": \"cow" | |
171 " " | |
172 " \"list\": [" | |
173 " \"foo\"," | |
174 " \"bar\"" | |
175 " ]"; | |
176 | |
177 getData->ParseResponse(HTTP_SUCCESS, invalid_json_str); | |
178 test_util::RunBlockingPoolTask(); | |
179 | |
180 EXPECT_EQ(GDATA_PARSE_ERROR, error); | |
181 ASSERT_TRUE(value.get() == NULL); | |
182 } | |
183 } | |
184 | |
185 } // namespace gdata | |
OLD | NEW |