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, | |
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 | |
81 scoped_ptr<base::Value> value; | |
82 GDataErrorCode error; | |
83 gdata::GetDataCallback cb = base::Bind(&GetDataOperationParseJsonCallback, | |
84 &error, | |
85 &value); | |
86 JsonParseTestGetDataOperation* getData = | |
87 new JsonParseTestGetDataOperation(runner_->operation_registry(), | |
88 profile_.get(), | |
89 cb); | |
90 getData->NotifyStart(); | |
91 | |
92 // Parses a valid json string. | |
93 { | |
94 std::string valid_json_str = | |
95 "{" | |
96 " \"test\": {" | |
97 " \"foo\": true," | |
98 " \"bar\": 3.14," | |
99 " \"baz\": \"bat\"," | |
100 " \"moo\": \"cow\"" | |
101 " }," | |
102 " \"list\": [" | |
103 " \"a\"," | |
104 " \"b\"" | |
105 " ]" | |
106 "}"; | |
107 | |
108 getData->ParseResponse(HTTP_SUCCESS, valid_json_str); | |
109 test_util::RunBlockingPoolTask(); | |
110 message_loop_.RunAllPending(); | |
111 | |
112 EXPECT_EQ(HTTP_SUCCESS, error); | |
113 ASSERT_TRUE(value.get()); | |
114 | |
115 DictionaryValue* root_dict = NULL; | |
116 ASSERT_TRUE(value->GetAsDictionary(&root_dict)); | |
117 | |
118 DictionaryValue* dict = NULL; | |
119 ListValue* list = NULL; | |
120 ASSERT_TRUE(root_dict->GetDictionary("test", &dict)); | |
121 ASSERT_TRUE(root_dict->GetList("list", &list)); | |
122 | |
123 Value* dict_literals[2] = {0}; | |
124 Value* dict_strings[2] = {0}; | |
125 Value* list_values[2] = {0}; | |
126 EXPECT_TRUE(dict->Remove("foo", &dict_literals[0])); | |
127 EXPECT_TRUE(dict->Remove("bar", &dict_literals[1])); | |
128 EXPECT_TRUE(dict->Remove("baz", &dict_strings[0])); | |
129 EXPECT_TRUE(dict->Remove("moo", &dict_strings[1])); | |
130 ASSERT_EQ(2u, list->GetSize()); | |
131 EXPECT_TRUE(list->Remove(0, &list_values[0])); | |
132 EXPECT_TRUE(list->Remove(0, &list_values[1])); | |
133 } | |
134 | |
135 // Parses an invalid json string. | |
136 { | |
137 std::string invalid_json_str = | |
138 "/* hogehoge *" | |
139 " \"test\": {" | |
140 " \"moo\": \"cow" | |
141 " " | |
142 " \"list\": [" | |
143 " \"foo\"," | |
144 " \"bar\"" | |
145 " ]"; | |
146 | |
147 getData->ParseResponse(HTTP_SUCCESS, invalid_json_str); | |
148 test_util::RunBlockingPoolTask(); | |
149 message_loop_.RunAllPending(); | |
150 | |
151 ASSERT_TRUE(value.get() == NULL); | |
152 EXPECT_EQ(GDATA_PARSE_ERROR, error); | |
153 } | |
154 | |
155 getData->NotifySuccess(); | |
156 } | |
157 | |
158 } // namespace gdata | |
OLD | NEW |