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 protected: | |
40 // GetDataOperation overrides: | |
41 virtual GURL GetURL() const OVERRIDE { | |
42 // This method is never called because this test does not fetch json from | |
43 // network. | |
44 NOTREACHED(); | |
45 return GURL(); | |
46 } | |
47 }; | |
48 | |
49 } // namespace | |
50 | |
51 class GDataOperationsTest : public testing::Test { | |
52 protected: | |
53 GDataOperationsTest() | |
54 : ui_thread_(content::BrowserThread::UI, &message_loop_) { | |
55 } | |
56 | |
57 virtual void SetUp() OVERRIDE { | |
58 profile_.reset(new TestingProfile); | |
59 runner_.reset(new GDataOperationRunner(profile_.get())); | |
60 runner_->Initialize(); | |
61 } | |
62 | |
63 protected: | |
64 MessageLoopForUI message_loop_; | |
65 content::TestBrowserThread ui_thread_; | |
66 scoped_ptr<TestingProfile> profile_; | |
67 scoped_ptr<GDataOperationRunner> runner_; | |
68 }; | |
69 | |
70 void GetDataOperationParseJsonCallback(GDataErrorCode* error_out, | |
hashimoto
2012/07/19 03:57:57
nit: Move this function into the anonymous namespa
yoshiki
2012/07/19 04:58:45
Done.
| |
71 scoped_ptr<base::Value>* value_out, | |
72 GDataErrorCode error_in, | |
73 scoped_ptr<base::Value> value_in) { | |
74 value_out->swap(value_in); | |
75 *error_out = error_in; | |
76 } | |
77 | |
78 | |
79 TEST_F(GDataOperationsTest, GetDataOperationParseJson) { | |
80 scoped_ptr<base::Value> value; | |
81 GDataErrorCode error; | |
82 gdata::GetDataCallback cb = base::Bind(&GetDataOperationParseJsonCallback, | |
83 &error, | |
84 &value); | |
85 JsonParseTestGetDataOperation* getData = | |
86 new JsonParseTestGetDataOperation(runner_->operation_registry(), | |
87 profile_.get(), | |
88 cb); | |
89 getData->NotifyStart(); | |
90 | |
91 // Parses a valid json string. | |
92 { | |
93 std::string valid_json_str = | |
94 "{" | |
95 " \"test\": {" | |
96 " \"foo\": true," | |
97 " \"bar\": 3.14," | |
98 " \"baz\": \"bat\"," | |
99 " \"moo\": \"cow\"" | |
100 " }," | |
101 " \"list\": [" | |
102 " \"a\"," | |
103 " \"b\"" | |
104 " ]" | |
105 "}"; | |
106 | |
107 getData->ParseResponse(HTTP_SUCCESS, valid_json_str); | |
108 test_util::RunBlockingPoolTask(); | |
109 message_loop_.RunAllPending(); | |
hashimoto
2012/07/19 03:57:57
Is this line needed?
yoshiki
2012/07/19 04:58:45
Done. RunAllPending() is called already in test_ut
| |
110 | |
111 EXPECT_EQ(HTTP_SUCCESS, error); | |
112 ASSERT_TRUE(value.get()); | |
113 | |
114 DictionaryValue* root_dict = NULL; | |
115 ASSERT_TRUE(value->GetAsDictionary(&root_dict)); | |
116 | |
117 DictionaryValue* dict = NULL; | |
118 ListValue* list = NULL; | |
119 ASSERT_TRUE(root_dict->GetDictionary("test", &dict)); | |
120 ASSERT_TRUE(root_dict->GetList("list", &list)); | |
121 | |
122 Value* dict_literals[2] = {0}; | |
123 Value* dict_strings[2] = {0}; | |
124 Value* list_values[2] = {0}; | |
125 EXPECT_TRUE(dict->Remove("foo", &dict_literals[0])); | |
hashimoto
2012/07/19 03:57:57
Could you use Get() instead of Remove()?
This way,
yoshiki
2012/07/19 04:58:45
Done.
| |
126 EXPECT_TRUE(dict->Remove("bar", &dict_literals[1])); | |
127 EXPECT_TRUE(dict->Remove("baz", &dict_strings[0])); | |
128 EXPECT_TRUE(dict->Remove("moo", &dict_strings[1])); | |
129 ASSERT_EQ(2u, list->GetSize()); | |
130 EXPECT_TRUE(list->Remove(0, &list_values[0])); | |
131 EXPECT_TRUE(list->Remove(0, &list_values[1])); | |
132 | |
133 delete dict_literals[0]; | |
134 delete dict_literals[1]; | |
135 delete dict_strings[0]; | |
136 delete dict_strings[1]; | |
137 delete list_values[0]; | |
138 delete list_values[1]; | |
139 } | |
140 | |
141 getData->NotifySuccess(); | |
142 } | |
143 | |
144 TEST_F(GDataOperationsTest, GetDataOperationParseInvalidJson) { | |
145 scoped_ptr<base::Value> value; | |
146 GDataErrorCode error; | |
147 gdata::GetDataCallback cb = base::Bind(&GetDataOperationParseJsonCallback, | |
148 &error, | |
149 &value); | |
150 JsonParseTestGetDataOperation* getData = | |
151 new JsonParseTestGetDataOperation(runner_->operation_registry(), | |
152 profile_.get(), | |
153 cb); | |
154 getData->NotifyStart(); | |
155 | |
156 // Parses an invalid json string. | |
157 { | |
158 std::string invalid_json_str = | |
159 "/* hogehoge *" | |
160 " \"test\": {" | |
161 " \"moo\": \"cow" | |
162 " " | |
163 " \"list\": [" | |
164 " \"foo\"," | |
165 " \"bar\"" | |
166 " ]"; | |
167 | |
168 getData->ParseResponse(HTTP_SUCCESS, invalid_json_str); | |
169 test_util::RunBlockingPoolTask(); | |
170 message_loop_.RunAllPending(); | |
hashimoto
2012/07/19 03:57:57
Is this line needed?
yoshiki
2012/07/19 04:58:45
Done.
| |
171 | |
172 ASSERT_TRUE(value.get() == NULL); | |
173 EXPECT_EQ(GDATA_PARSE_ERROR, error); | |
174 } | |
175 | |
176 getData->NotifySuccess(); | |
hashimoto
2012/07/19 03:57:57
Why notifying success after parse failure?
yoshiki
2012/07/19 04:58:45
Done.
| |
177 } | |
178 | |
179 } // namespace gdata | |
OLD | NEW |