OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "dbus/values_util.h" | 5 #include "dbus/values_util.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/float_util.h" | 9 #include "base/float_util.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "dbus/message.h" | 13 #include "dbus/message.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
15 | 15 |
| 16 namespace dbus { |
| 17 |
16 TEST(ValuesUtilTest, PopBasicTypes) { | 18 TEST(ValuesUtilTest, PopBasicTypes) { |
17 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 19 scoped_ptr<Response> response(Response::CreateEmpty()); |
18 // Append basic type values. | 20 // Append basic type values. |
19 dbus::MessageWriter writer(response.get()); | 21 MessageWriter writer(response.get()); |
20 const uint8 kByteValue = 42; | 22 const uint8 kByteValue = 42; |
21 writer.AppendByte(kByteValue); | 23 writer.AppendByte(kByteValue); |
22 const bool kBoolValue = true; | 24 const bool kBoolValue = true; |
23 writer.AppendBool(kBoolValue); | 25 writer.AppendBool(kBoolValue); |
24 const int16 kInt16Value = -43; | 26 const int16 kInt16Value = -43; |
25 writer.AppendInt16(kInt16Value); | 27 writer.AppendInt16(kInt16Value); |
26 const uint16 kUint16Value = 44; | 28 const uint16 kUint16Value = 44; |
27 writer.AppendUint16(kUint16Value); | 29 writer.AppendUint16(kUint16Value); |
28 const int32 kInt32Value = -45; | 30 const int32 kInt32Value = -45; |
29 writer.AppendInt32(kInt32Value); | 31 writer.AppendInt32(kInt32Value); |
30 const uint32 kUint32Value = 46; | 32 const uint32 kUint32Value = 46; |
31 writer.AppendUint32(kUint32Value); | 33 writer.AppendUint32(kUint32Value); |
32 const int64 kInt64Value = -47; | 34 const int64 kInt64Value = -47; |
33 writer.AppendInt64(kInt64Value); | 35 writer.AppendInt64(kInt64Value); |
34 const uint64 kUint64Value = 48; | 36 const uint64 kUint64Value = 48; |
35 writer.AppendUint64(kUint64Value); | 37 writer.AppendUint64(kUint64Value); |
36 const double kDoubleValue = 4.9; | 38 const double kDoubleValue = 4.9; |
37 writer.AppendDouble(kDoubleValue); | 39 writer.AppendDouble(kDoubleValue); |
38 const std::string kStringValue = "fifty"; | 40 const std::string kStringValue = "fifty"; |
39 writer.AppendString(kStringValue); | 41 writer.AppendString(kStringValue); |
40 const std::string kEmptyStringValue; | 42 const std::string kEmptyStringValue; |
41 writer.AppendString(kEmptyStringValue); | 43 writer.AppendString(kEmptyStringValue); |
42 const dbus::ObjectPath kObjectPathValue("/ObjectPath"); | 44 const ObjectPath kObjectPathValue("/ObjectPath"); |
43 writer.AppendObjectPath(kObjectPathValue); | 45 writer.AppendObjectPath(kObjectPathValue); |
44 | 46 |
45 dbus::MessageReader reader(response.get()); | 47 MessageReader reader(response.get()); |
46 scoped_ptr<base::Value> value; | 48 scoped_ptr<base::Value> value; |
47 scoped_ptr<base::Value> expected_value; | 49 scoped_ptr<base::Value> expected_value; |
48 // Pop a byte. | 50 // Pop a byte. |
49 value.reset(dbus::PopDataAsValue(&reader)); | 51 value.reset(PopDataAsValue(&reader)); |
50 ASSERT_TRUE(value.get() != NULL); | 52 ASSERT_TRUE(value.get() != NULL); |
51 expected_value.reset(new base::FundamentalValue(kByteValue)); | 53 expected_value.reset(new base::FundamentalValue(kByteValue)); |
52 EXPECT_TRUE(value->Equals(expected_value.get())); | 54 EXPECT_TRUE(value->Equals(expected_value.get())); |
53 // Pop a bool. | 55 // Pop a bool. |
54 value.reset(dbus::PopDataAsValue(&reader)); | 56 value.reset(PopDataAsValue(&reader)); |
55 ASSERT_TRUE(value.get() != NULL); | 57 ASSERT_TRUE(value.get() != NULL); |
56 expected_value.reset(new base::FundamentalValue(kBoolValue)); | 58 expected_value.reset(new base::FundamentalValue(kBoolValue)); |
57 EXPECT_TRUE(value->Equals(expected_value.get())); | 59 EXPECT_TRUE(value->Equals(expected_value.get())); |
58 // Pop an int16. | 60 // Pop an int16. |
59 value.reset(dbus::PopDataAsValue(&reader)); | 61 value.reset(PopDataAsValue(&reader)); |
60 ASSERT_TRUE(value.get() != NULL); | 62 ASSERT_TRUE(value.get() != NULL); |
61 expected_value.reset(new base::FundamentalValue(kInt16Value)); | 63 expected_value.reset(new base::FundamentalValue(kInt16Value)); |
62 EXPECT_TRUE(value->Equals(expected_value.get())); | 64 EXPECT_TRUE(value->Equals(expected_value.get())); |
63 // Pop a uint16. | 65 // Pop a uint16. |
64 value.reset(dbus::PopDataAsValue(&reader)); | 66 value.reset(PopDataAsValue(&reader)); |
65 ASSERT_TRUE(value.get() != NULL); | 67 ASSERT_TRUE(value.get() != NULL); |
66 expected_value.reset(new base::FundamentalValue(kUint16Value)); | 68 expected_value.reset(new base::FundamentalValue(kUint16Value)); |
67 EXPECT_TRUE(value->Equals(expected_value.get())); | 69 EXPECT_TRUE(value->Equals(expected_value.get())); |
68 // Pop an int32. | 70 // Pop an int32. |
69 value.reset(dbus::PopDataAsValue(&reader)); | 71 value.reset(PopDataAsValue(&reader)); |
70 ASSERT_TRUE(value.get() != NULL); | 72 ASSERT_TRUE(value.get() != NULL); |
71 expected_value.reset(new base::FundamentalValue(kInt32Value)); | 73 expected_value.reset(new base::FundamentalValue(kInt32Value)); |
72 EXPECT_TRUE(value->Equals(expected_value.get())); | 74 EXPECT_TRUE(value->Equals(expected_value.get())); |
73 // Pop a uint32. | 75 // Pop a uint32. |
74 value.reset(dbus::PopDataAsValue(&reader)); | 76 value.reset(PopDataAsValue(&reader)); |
75 ASSERT_TRUE(value.get() != NULL); | 77 ASSERT_TRUE(value.get() != NULL); |
76 expected_value.reset( | 78 expected_value.reset( |
77 new base::FundamentalValue(static_cast<double>(kUint32Value))); | 79 new base::FundamentalValue(static_cast<double>(kUint32Value))); |
78 EXPECT_TRUE(value->Equals(expected_value.get())); | 80 EXPECT_TRUE(value->Equals(expected_value.get())); |
79 // Pop an int64. | 81 // Pop an int64. |
80 value.reset(dbus::PopDataAsValue(&reader)); | 82 value.reset(PopDataAsValue(&reader)); |
81 ASSERT_TRUE(value.get() != NULL); | 83 ASSERT_TRUE(value.get() != NULL); |
82 expected_value.reset( | 84 expected_value.reset( |
83 new base::FundamentalValue(static_cast<double>(kInt64Value))); | 85 new base::FundamentalValue(static_cast<double>(kInt64Value))); |
84 EXPECT_TRUE(value->Equals(expected_value.get())); | 86 EXPECT_TRUE(value->Equals(expected_value.get())); |
85 // Pop a uint64. | 87 // Pop a uint64. |
86 value.reset(dbus::PopDataAsValue(&reader)); | 88 value.reset(PopDataAsValue(&reader)); |
87 ASSERT_TRUE(value.get() != NULL); | 89 ASSERT_TRUE(value.get() != NULL); |
88 expected_value.reset( | 90 expected_value.reset( |
89 new base::FundamentalValue(static_cast<double>(kUint64Value))); | 91 new base::FundamentalValue(static_cast<double>(kUint64Value))); |
90 EXPECT_TRUE(value->Equals(expected_value.get())); | 92 EXPECT_TRUE(value->Equals(expected_value.get())); |
91 // Pop a double. | 93 // Pop a double. |
92 value.reset(dbus::PopDataAsValue(&reader)); | 94 value.reset(PopDataAsValue(&reader)); |
93 ASSERT_TRUE(value.get() != NULL); | 95 ASSERT_TRUE(value.get() != NULL); |
94 expected_value.reset(new base::FundamentalValue(kDoubleValue)); | 96 expected_value.reset(new base::FundamentalValue(kDoubleValue)); |
95 EXPECT_TRUE(value->Equals(expected_value.get())); | 97 EXPECT_TRUE(value->Equals(expected_value.get())); |
96 // Pop a string. | 98 // Pop a string. |
97 value.reset(dbus::PopDataAsValue(&reader)); | 99 value.reset(PopDataAsValue(&reader)); |
98 ASSERT_TRUE(value.get() != NULL); | 100 ASSERT_TRUE(value.get() != NULL); |
99 expected_value.reset(new base::StringValue(kStringValue)); | 101 expected_value.reset(new base::StringValue(kStringValue)); |
100 EXPECT_TRUE(value->Equals(expected_value.get())); | 102 EXPECT_TRUE(value->Equals(expected_value.get())); |
101 // Pop an empty string. | 103 // Pop an empty string. |
102 value.reset(dbus::PopDataAsValue(&reader)); | 104 value.reset(PopDataAsValue(&reader)); |
103 ASSERT_TRUE(value.get() != NULL); | 105 ASSERT_TRUE(value.get() != NULL); |
104 expected_value.reset(new base::StringValue(kEmptyStringValue)); | 106 expected_value.reset(new base::StringValue(kEmptyStringValue)); |
105 EXPECT_TRUE(value->Equals(expected_value.get())); | 107 EXPECT_TRUE(value->Equals(expected_value.get())); |
106 // Pop an object path. | 108 // Pop an object path. |
107 value.reset(dbus::PopDataAsValue(&reader)); | 109 value.reset(PopDataAsValue(&reader)); |
108 ASSERT_TRUE(value.get() != NULL); | 110 ASSERT_TRUE(value.get() != NULL); |
109 expected_value.reset(new base::StringValue(kObjectPathValue.value())); | 111 expected_value.reset(new base::StringValue(kObjectPathValue.value())); |
110 EXPECT_TRUE(value->Equals(expected_value.get())); | 112 EXPECT_TRUE(value->Equals(expected_value.get())); |
111 } | 113 } |
112 | 114 |
113 TEST(ValuesUtilTest, PopVariant) { | 115 TEST(ValuesUtilTest, PopVariant) { |
114 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 116 scoped_ptr<Response> response(Response::CreateEmpty()); |
115 // Append variant values. | 117 // Append variant values. |
116 dbus::MessageWriter writer(response.get()); | 118 MessageWriter writer(response.get()); |
117 const bool kBoolValue = true; | 119 const bool kBoolValue = true; |
118 writer.AppendVariantOfBool(kBoolValue); | 120 writer.AppendVariantOfBool(kBoolValue); |
119 const int32 kInt32Value = -45; | 121 const int32 kInt32Value = -45; |
120 writer.AppendVariantOfInt32(kInt32Value); | 122 writer.AppendVariantOfInt32(kInt32Value); |
121 const double kDoubleValue = 4.9; | 123 const double kDoubleValue = 4.9; |
122 writer.AppendVariantOfDouble(kDoubleValue); | 124 writer.AppendVariantOfDouble(kDoubleValue); |
123 const std::string kStringValue = "fifty"; | 125 const std::string kStringValue = "fifty"; |
124 writer.AppendVariantOfString(kStringValue); | 126 writer.AppendVariantOfString(kStringValue); |
125 | 127 |
126 dbus::MessageReader reader(response.get()); | 128 MessageReader reader(response.get()); |
127 scoped_ptr<base::Value> value; | 129 scoped_ptr<base::Value> value; |
128 scoped_ptr<base::Value> expected_value; | 130 scoped_ptr<base::Value> expected_value; |
129 // Pop a bool. | 131 // Pop a bool. |
130 value.reset(dbus::PopDataAsValue(&reader)); | 132 value.reset(PopDataAsValue(&reader)); |
131 ASSERT_TRUE(value.get() != NULL); | 133 ASSERT_TRUE(value.get() != NULL); |
132 expected_value.reset(new base::FundamentalValue(kBoolValue)); | 134 expected_value.reset(new base::FundamentalValue(kBoolValue)); |
133 EXPECT_TRUE(value->Equals(expected_value.get())); | 135 EXPECT_TRUE(value->Equals(expected_value.get())); |
134 // Pop an int32. | 136 // Pop an int32. |
135 value.reset(dbus::PopDataAsValue(&reader)); | 137 value.reset(PopDataAsValue(&reader)); |
136 ASSERT_TRUE(value.get() != NULL); | 138 ASSERT_TRUE(value.get() != NULL); |
137 expected_value.reset(new base::FundamentalValue(kInt32Value)); | 139 expected_value.reset(new base::FundamentalValue(kInt32Value)); |
138 EXPECT_TRUE(value->Equals(expected_value.get())); | 140 EXPECT_TRUE(value->Equals(expected_value.get())); |
139 // Pop a double. | 141 // Pop a double. |
140 value.reset(dbus::PopDataAsValue(&reader)); | 142 value.reset(PopDataAsValue(&reader)); |
141 ASSERT_TRUE(value.get() != NULL); | 143 ASSERT_TRUE(value.get() != NULL); |
142 expected_value.reset(new base::FundamentalValue(kDoubleValue)); | 144 expected_value.reset(new base::FundamentalValue(kDoubleValue)); |
143 EXPECT_TRUE(value->Equals(expected_value.get())); | 145 EXPECT_TRUE(value->Equals(expected_value.get())); |
144 // Pop a string. | 146 // Pop a string. |
145 value.reset(dbus::PopDataAsValue(&reader)); | 147 value.reset(PopDataAsValue(&reader)); |
146 ASSERT_TRUE(value.get() != NULL); | 148 ASSERT_TRUE(value.get() != NULL); |
147 expected_value.reset(new base::StringValue(kStringValue)); | 149 expected_value.reset(new base::StringValue(kStringValue)); |
148 EXPECT_TRUE(value->Equals(expected_value.get())); | 150 EXPECT_TRUE(value->Equals(expected_value.get())); |
149 } | 151 } |
150 | 152 |
151 // Pop extremely large integers which cannot be precisely represented in | 153 // Pop extremely large integers which cannot be precisely represented in |
152 // double. | 154 // double. |
153 TEST(ValuesUtilTest, PopExtremelyLargeIntegers) { | 155 TEST(ValuesUtilTest, PopExtremelyLargeIntegers) { |
154 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 156 scoped_ptr<Response> response(Response::CreateEmpty()); |
155 // Append large integers. | 157 // Append large integers. |
156 dbus::MessageWriter writer(response.get()); | 158 MessageWriter writer(response.get()); |
157 const int64 kInt64Value = -123456789012345689LL; | 159 const int64 kInt64Value = -123456789012345689LL; |
158 writer.AppendInt64(kInt64Value); | 160 writer.AppendInt64(kInt64Value); |
159 const uint64 kUint64Value = 9876543210987654321ULL; | 161 const uint64 kUint64Value = 9876543210987654321ULL; |
160 writer.AppendUint64(kUint64Value); | 162 writer.AppendUint64(kUint64Value); |
161 | 163 |
162 dbus::MessageReader reader(response.get()); | 164 MessageReader reader(response.get()); |
163 scoped_ptr<base::Value> value; | 165 scoped_ptr<base::Value> value; |
164 scoped_ptr<base::Value> expected_value; | 166 scoped_ptr<base::Value> expected_value; |
165 double double_value = 0; | 167 double double_value = 0; |
166 // Pop an int64. | 168 // Pop an int64. |
167 value.reset(dbus::PopDataAsValue(&reader)); | 169 value.reset(PopDataAsValue(&reader)); |
168 ASSERT_TRUE(value.get() != NULL); | 170 ASSERT_TRUE(value.get() != NULL); |
169 expected_value.reset( | 171 expected_value.reset( |
170 new base::FundamentalValue(static_cast<double>(kInt64Value))); | 172 new base::FundamentalValue(static_cast<double>(kInt64Value))); |
171 EXPECT_TRUE(value->Equals(expected_value.get())); | 173 EXPECT_TRUE(value->Equals(expected_value.get())); |
172 ASSERT_TRUE(value->GetAsDouble(&double_value)); | 174 ASSERT_TRUE(value->GetAsDouble(&double_value)); |
173 EXPECT_NE(kInt64Value, static_cast<int64>(double_value)); | 175 EXPECT_NE(kInt64Value, static_cast<int64>(double_value)); |
174 // Pop a uint64. | 176 // Pop a uint64. |
175 value.reset(dbus::PopDataAsValue(&reader)); | 177 value.reset(PopDataAsValue(&reader)); |
176 ASSERT_TRUE(value.get() != NULL); | 178 ASSERT_TRUE(value.get() != NULL); |
177 expected_value.reset( | 179 expected_value.reset( |
178 new base::FundamentalValue(static_cast<double>(kUint64Value))); | 180 new base::FundamentalValue(static_cast<double>(kUint64Value))); |
179 EXPECT_TRUE(value->Equals(expected_value.get())); | 181 EXPECT_TRUE(value->Equals(expected_value.get())); |
180 ASSERT_TRUE(value->GetAsDouble(&double_value)); | 182 ASSERT_TRUE(value->GetAsDouble(&double_value)); |
181 EXPECT_NE(kUint64Value, static_cast<uint64>(double_value)); | 183 EXPECT_NE(kUint64Value, static_cast<uint64>(double_value)); |
182 } | 184 } |
183 | 185 |
184 TEST(ValuesUtilTest, PopIntArray) { | 186 TEST(ValuesUtilTest, PopIntArray) { |
185 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 187 scoped_ptr<Response> response(Response::CreateEmpty()); |
186 // Append an int32 array. | 188 // Append an int32 array. |
187 dbus::MessageWriter writer(response.get()); | 189 MessageWriter writer(response.get()); |
188 dbus::MessageWriter sub_writer(NULL); | 190 MessageWriter sub_writer(NULL); |
189 std::vector<int32> data; | 191 std::vector<int32> data; |
190 data.push_back(0); | 192 data.push_back(0); |
191 data.push_back(1); | 193 data.push_back(1); |
192 data.push_back(2); | 194 data.push_back(2); |
193 writer.OpenArray("i", &sub_writer); | 195 writer.OpenArray("i", &sub_writer); |
194 for (size_t i = 0; i != data.size(); ++i) | 196 for (size_t i = 0; i != data.size(); ++i) |
195 sub_writer.AppendInt32(data[i]); | 197 sub_writer.AppendInt32(data[i]); |
196 writer.CloseContainer(&sub_writer); | 198 writer.CloseContainer(&sub_writer); |
197 | 199 |
198 // Create the expected value. | 200 // Create the expected value. |
199 scoped_ptr<base::ListValue> list_value(new base::ListValue); | 201 scoped_ptr<base::ListValue> list_value(new base::ListValue); |
200 for (size_t i = 0; i != data.size(); ++i) | 202 for (size_t i = 0; i != data.size(); ++i) |
201 list_value->Append(new base::FundamentalValue(data[i])); | 203 list_value->Append(new base::FundamentalValue(data[i])); |
202 | 204 |
203 // Pop an int32 array. | 205 // Pop an int32 array. |
204 dbus::MessageReader reader(response.get()); | 206 MessageReader reader(response.get()); |
205 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 207 scoped_ptr<base::Value> value(PopDataAsValue(&reader)); |
206 ASSERT_TRUE(value.get() != NULL); | 208 ASSERT_TRUE(value.get() != NULL); |
207 EXPECT_TRUE(value->Equals(list_value.get())); | 209 EXPECT_TRUE(value->Equals(list_value.get())); |
208 } | 210 } |
209 | 211 |
210 TEST(ValuesUtilTest, PopStringArray) { | 212 TEST(ValuesUtilTest, PopStringArray) { |
211 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 213 scoped_ptr<Response> response(Response::CreateEmpty()); |
212 // Append a string array. | 214 // Append a string array. |
213 dbus::MessageWriter writer(response.get()); | 215 MessageWriter writer(response.get()); |
214 dbus::MessageWriter sub_writer(NULL); | 216 MessageWriter sub_writer(NULL); |
215 std::vector<std::string> data; | 217 std::vector<std::string> data; |
216 data.push_back("Dreamlifter"); | 218 data.push_back("Dreamlifter"); |
217 data.push_back("Beluga"); | 219 data.push_back("Beluga"); |
218 data.push_back("Mriya"); | 220 data.push_back("Mriya"); |
219 writer.AppendArrayOfStrings(data); | 221 writer.AppendArrayOfStrings(data); |
220 | 222 |
221 // Create the expected value. | 223 // Create the expected value. |
222 scoped_ptr<base::ListValue> list_value(new base::ListValue); | 224 scoped_ptr<base::ListValue> list_value(new base::ListValue); |
223 for (size_t i = 0; i != data.size(); ++i) | 225 for (size_t i = 0; i != data.size(); ++i) |
224 list_value->Append(new base::StringValue(data[i])); | 226 list_value->Append(new base::StringValue(data[i])); |
225 | 227 |
226 // Pop a string array. | 228 // Pop a string array. |
227 dbus::MessageReader reader(response.get()); | 229 MessageReader reader(response.get()); |
228 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 230 scoped_ptr<base::Value> value(PopDataAsValue(&reader)); |
229 ASSERT_TRUE(value.get() != NULL); | 231 ASSERT_TRUE(value.get() != NULL); |
230 EXPECT_TRUE(value->Equals(list_value.get())); | 232 EXPECT_TRUE(value->Equals(list_value.get())); |
231 } | 233 } |
232 | 234 |
233 TEST(ValuesUtilTest, PopStruct) { | 235 TEST(ValuesUtilTest, PopStruct) { |
234 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 236 scoped_ptr<Response> response(Response::CreateEmpty()); |
235 // Append a struct. | 237 // Append a struct. |
236 dbus::MessageWriter writer(response.get()); | 238 MessageWriter writer(response.get()); |
237 dbus::MessageWriter sub_writer(NULL); | 239 MessageWriter sub_writer(NULL); |
238 writer.OpenStruct(&sub_writer); | 240 writer.OpenStruct(&sub_writer); |
239 const bool kBoolValue = true; | 241 const bool kBoolValue = true; |
240 sub_writer.AppendBool(kBoolValue); | 242 sub_writer.AppendBool(kBoolValue); |
241 const int32 kInt32Value = -123; | 243 const int32 kInt32Value = -123; |
242 sub_writer.AppendInt32(kInt32Value); | 244 sub_writer.AppendInt32(kInt32Value); |
243 const double kDoubleValue = 1.23; | 245 const double kDoubleValue = 1.23; |
244 sub_writer.AppendDouble(kDoubleValue); | 246 sub_writer.AppendDouble(kDoubleValue); |
245 const std::string kStringValue = "one two three"; | 247 const std::string kStringValue = "one two three"; |
246 sub_writer.AppendString(kStringValue); | 248 sub_writer.AppendString(kStringValue); |
247 writer.CloseContainer(&sub_writer); | 249 writer.CloseContainer(&sub_writer); |
248 | 250 |
249 // Create the expected value. | 251 // Create the expected value. |
250 base::ListValue list_value; | 252 base::ListValue list_value; |
251 list_value.Append(new base::FundamentalValue(kBoolValue)); | 253 list_value.Append(new base::FundamentalValue(kBoolValue)); |
252 list_value.Append(new base::FundamentalValue(kInt32Value)); | 254 list_value.Append(new base::FundamentalValue(kInt32Value)); |
253 list_value.Append(new base::FundamentalValue(kDoubleValue)); | 255 list_value.Append(new base::FundamentalValue(kDoubleValue)); |
254 list_value.Append(new base::StringValue(kStringValue)); | 256 list_value.Append(new base::StringValue(kStringValue)); |
255 | 257 |
256 // Pop a struct. | 258 // Pop a struct. |
257 dbus::MessageReader reader(response.get()); | 259 MessageReader reader(response.get()); |
258 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 260 scoped_ptr<base::Value> value(PopDataAsValue(&reader)); |
259 ASSERT_TRUE(value.get() != NULL); | 261 ASSERT_TRUE(value.get() != NULL); |
260 EXPECT_TRUE(value->Equals(&list_value)); | 262 EXPECT_TRUE(value->Equals(&list_value)); |
261 } | 263 } |
262 | 264 |
263 TEST(ValuesUtilTest, PopStringToVariantDictionary) { | 265 TEST(ValuesUtilTest, PopStringToVariantDictionary) { |
264 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 266 scoped_ptr<Response> response(Response::CreateEmpty()); |
265 // Append a dictionary. | 267 // Append a dictionary. |
266 dbus::MessageWriter writer(response.get()); | 268 MessageWriter writer(response.get()); |
267 dbus::MessageWriter sub_writer(NULL); | 269 MessageWriter sub_writer(NULL); |
268 dbus::MessageWriter entry_writer(NULL); | 270 MessageWriter entry_writer(NULL); |
269 writer.OpenArray("{sv}", &sub_writer); | 271 writer.OpenArray("{sv}", &sub_writer); |
270 sub_writer.OpenDictEntry(&entry_writer); | 272 sub_writer.OpenDictEntry(&entry_writer); |
271 const std::string kKey1 = "one"; | 273 const std::string kKey1 = "one"; |
272 entry_writer.AppendString(kKey1); | 274 entry_writer.AppendString(kKey1); |
273 const bool kBoolValue = true; | 275 const bool kBoolValue = true; |
274 entry_writer.AppendVariantOfBool(kBoolValue); | 276 entry_writer.AppendVariantOfBool(kBoolValue); |
275 sub_writer.CloseContainer(&entry_writer); | 277 sub_writer.CloseContainer(&entry_writer); |
276 sub_writer.OpenDictEntry(&entry_writer); | 278 sub_writer.OpenDictEntry(&entry_writer); |
277 const std::string kKey2 = "two"; | 279 const std::string kKey2 = "two"; |
278 entry_writer.AppendString(kKey2); | 280 entry_writer.AppendString(kKey2); |
(...skipping 15 matching lines...) Expand all Loading... |
294 writer.CloseContainer(&sub_writer); | 296 writer.CloseContainer(&sub_writer); |
295 | 297 |
296 // Create the expected value. | 298 // Create the expected value. |
297 base::DictionaryValue dictionary_value; | 299 base::DictionaryValue dictionary_value; |
298 dictionary_value.SetBoolean(kKey1, kBoolValue); | 300 dictionary_value.SetBoolean(kKey1, kBoolValue); |
299 dictionary_value.SetInteger(kKey2, kInt32Value); | 301 dictionary_value.SetInteger(kKey2, kInt32Value); |
300 dictionary_value.SetDouble(kKey3, kDoubleValue); | 302 dictionary_value.SetDouble(kKey3, kDoubleValue); |
301 dictionary_value.SetString(kKey4, kStringValue); | 303 dictionary_value.SetString(kKey4, kStringValue); |
302 | 304 |
303 // Pop a dictinoary. | 305 // Pop a dictinoary. |
304 dbus::MessageReader reader(response.get()); | 306 MessageReader reader(response.get()); |
305 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 307 scoped_ptr<base::Value> value(PopDataAsValue(&reader)); |
306 ASSERT_TRUE(value.get() != NULL); | 308 ASSERT_TRUE(value.get() != NULL); |
307 EXPECT_TRUE(value->Equals(&dictionary_value)); | 309 EXPECT_TRUE(value->Equals(&dictionary_value)); |
308 } | 310 } |
309 | 311 |
310 TEST(ValuesUtilTest, PopDictionaryWithDottedStringKey) { | 312 TEST(ValuesUtilTest, PopDictionaryWithDottedStringKey) { |
311 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 313 scoped_ptr<Response> response(Response::CreateEmpty()); |
312 // Append a dictionary. | 314 // Append a dictionary. |
313 dbus::MessageWriter writer(response.get()); | 315 MessageWriter writer(response.get()); |
314 dbus::MessageWriter sub_writer(NULL); | 316 MessageWriter sub_writer(NULL); |
315 dbus::MessageWriter entry_writer(NULL); | 317 MessageWriter entry_writer(NULL); |
316 writer.OpenArray("{sv}", &sub_writer); | 318 writer.OpenArray("{sv}", &sub_writer); |
317 sub_writer.OpenDictEntry(&entry_writer); | 319 sub_writer.OpenDictEntry(&entry_writer); |
318 const std::string kKey1 = "www.example.com"; // String including dots. | 320 const std::string kKey1 = "www.example.com"; // String including dots. |
319 entry_writer.AppendString(kKey1); | 321 entry_writer.AppendString(kKey1); |
320 const bool kBoolValue = true; | 322 const bool kBoolValue = true; |
321 entry_writer.AppendVariantOfBool(kBoolValue); | 323 entry_writer.AppendVariantOfBool(kBoolValue); |
322 sub_writer.CloseContainer(&entry_writer); | 324 sub_writer.CloseContainer(&entry_writer); |
323 sub_writer.OpenDictEntry(&entry_writer); | 325 sub_writer.OpenDictEntry(&entry_writer); |
324 const std::string kKey2 = ".example"; // String starting with a dot. | 326 const std::string kKey2 = ".example"; // String starting with a dot. |
325 entry_writer.AppendString(kKey2); | 327 entry_writer.AppendString(kKey2); |
(...skipping 11 matching lines...) Expand all Loading... |
337 // Create the expected value. | 339 // Create the expected value. |
338 base::DictionaryValue dictionary_value; | 340 base::DictionaryValue dictionary_value; |
339 dictionary_value.SetWithoutPathExpansion( | 341 dictionary_value.SetWithoutPathExpansion( |
340 kKey1, new base::FundamentalValue(kBoolValue)); | 342 kKey1, new base::FundamentalValue(kBoolValue)); |
341 dictionary_value.SetWithoutPathExpansion( | 343 dictionary_value.SetWithoutPathExpansion( |
342 kKey2, new base::FundamentalValue(kInt32Value)); | 344 kKey2, new base::FundamentalValue(kInt32Value)); |
343 dictionary_value.SetWithoutPathExpansion( | 345 dictionary_value.SetWithoutPathExpansion( |
344 kKey3, new base::FundamentalValue(kDoubleValue)); | 346 kKey3, new base::FundamentalValue(kDoubleValue)); |
345 | 347 |
346 // Pop a dictinoary. | 348 // Pop a dictinoary. |
347 dbus::MessageReader reader(response.get()); | 349 MessageReader reader(response.get()); |
348 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 350 scoped_ptr<base::Value> value(PopDataAsValue(&reader)); |
349 ASSERT_TRUE(value.get() != NULL); | 351 ASSERT_TRUE(value.get() != NULL); |
350 EXPECT_TRUE(value->Equals(&dictionary_value)); | 352 EXPECT_TRUE(value->Equals(&dictionary_value)); |
351 } | 353 } |
352 | 354 |
353 TEST(ValuesUtilTest, PopDoubleToIntDictionary) { | 355 TEST(ValuesUtilTest, PopDoubleToIntDictionary) { |
354 // Create test data. | 356 // Create test data. |
355 const int32 kValues[] = {0, 1, 1, 2, 3, 5, 8, 13, 21}; | 357 const int32 kValues[] = {0, 1, 1, 2, 3, 5, 8, 13, 21}; |
356 const std::vector<int32> values(kValues, kValues + arraysize(kValues)); | 358 const std::vector<int32> values(kValues, kValues + arraysize(kValues)); |
357 std::vector<double> keys(values.size()); | 359 std::vector<double> keys(values.size()); |
358 for (size_t i = 0; i != values.size(); ++i) | 360 for (size_t i = 0; i != values.size(); ++i) |
359 keys[i] = sqrt(values[i]); | 361 keys[i] = sqrt(values[i]); |
360 | 362 |
361 // Append a dictionary. | 363 // Append a dictionary. |
362 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 364 scoped_ptr<Response> response(Response::CreateEmpty()); |
363 dbus::MessageWriter writer(response.get()); | 365 MessageWriter writer(response.get()); |
364 dbus::MessageWriter sub_writer(NULL); | 366 MessageWriter sub_writer(NULL); |
365 writer.OpenArray("{di}", &sub_writer); | 367 writer.OpenArray("{di}", &sub_writer); |
366 for (size_t i = 0; i != values.size(); ++i) { | 368 for (size_t i = 0; i != values.size(); ++i) { |
367 dbus::MessageWriter entry_writer(NULL); | 369 MessageWriter entry_writer(NULL); |
368 sub_writer.OpenDictEntry(&entry_writer); | 370 sub_writer.OpenDictEntry(&entry_writer); |
369 entry_writer.AppendDouble(keys[i]); | 371 entry_writer.AppendDouble(keys[i]); |
370 entry_writer.AppendInt32(values[i]); | 372 entry_writer.AppendInt32(values[i]); |
371 sub_writer.CloseContainer(&entry_writer); | 373 sub_writer.CloseContainer(&entry_writer); |
372 } | 374 } |
373 writer.CloseContainer(&sub_writer); | 375 writer.CloseContainer(&sub_writer); |
374 | 376 |
375 // Create the expected value. | 377 // Create the expected value. |
376 base::DictionaryValue dictionary_value; | 378 base::DictionaryValue dictionary_value; |
377 for (size_t i = 0; i != values.size(); ++i) { | 379 for (size_t i = 0; i != values.size(); ++i) { |
378 scoped_ptr<base::Value> key_value(new base::FundamentalValue(keys[i])); | 380 scoped_ptr<base::Value> key_value(new base::FundamentalValue(keys[i])); |
379 std::string key_string; | 381 std::string key_string; |
380 base::JSONWriter::Write(key_value.get(), &key_string); | 382 base::JSONWriter::Write(key_value.get(), &key_string); |
381 dictionary_value.SetWithoutPathExpansion( | 383 dictionary_value.SetWithoutPathExpansion( |
382 key_string, new base::FundamentalValue(values[i])); | 384 key_string, new base::FundamentalValue(values[i])); |
383 } | 385 } |
384 | 386 |
385 // Pop a dictionary. | 387 // Pop a dictionary. |
386 dbus::MessageReader reader(response.get()); | 388 MessageReader reader(response.get()); |
387 scoped_ptr<base::Value> value(dbus::PopDataAsValue(&reader)); | 389 scoped_ptr<base::Value> value(PopDataAsValue(&reader)); |
388 ASSERT_TRUE(value.get() != NULL); | 390 ASSERT_TRUE(value.get() != NULL); |
389 EXPECT_TRUE(value->Equals(&dictionary_value)); | 391 EXPECT_TRUE(value->Equals(&dictionary_value)); |
390 } | 392 } |
391 | 393 |
392 TEST(ValuesUtilTest, AppendBasicTypes) { | 394 TEST(ValuesUtilTest, AppendBasicTypes) { |
393 const base::FundamentalValue kBoolValue(false); | 395 const base::FundamentalValue kBoolValue(false); |
394 const base::FundamentalValue kIntegerValue(42); | 396 const base::FundamentalValue kIntegerValue(42); |
395 const base::FundamentalValue kDoubleValue(4.2); | 397 const base::FundamentalValue kDoubleValue(4.2); |
396 const base::StringValue kStringValue("string"); | 398 const base::StringValue kStringValue("string"); |
397 | 399 |
398 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 400 scoped_ptr<Response> response(Response::CreateEmpty()); |
399 dbus::MessageWriter writer(response.get()); | 401 MessageWriter writer(response.get()); |
400 AppendBasicTypeValueData(&writer, kBoolValue); | 402 AppendBasicTypeValueData(&writer, kBoolValue); |
401 AppendBasicTypeValueData(&writer, kIntegerValue); | 403 AppendBasicTypeValueData(&writer, kIntegerValue); |
402 AppendBasicTypeValueData(&writer, kDoubleValue); | 404 AppendBasicTypeValueData(&writer, kDoubleValue); |
403 AppendBasicTypeValueData(&writer, kStringValue); | 405 AppendBasicTypeValueData(&writer, kStringValue); |
404 | 406 |
405 dbus::MessageReader reader(response.get()); | 407 MessageReader reader(response.get()); |
406 scoped_ptr<base::Value> value; | 408 scoped_ptr<base::Value> value; |
407 value.reset(dbus::PopDataAsValue(&reader)); | 409 value.reset(PopDataAsValue(&reader)); |
408 ASSERT_TRUE(value.get() != NULL); | 410 ASSERT_TRUE(value.get() != NULL); |
409 EXPECT_TRUE(value->Equals(&kBoolValue)); | 411 EXPECT_TRUE(value->Equals(&kBoolValue)); |
410 value.reset(dbus::PopDataAsValue(&reader)); | 412 value.reset(PopDataAsValue(&reader)); |
411 ASSERT_TRUE(value.get() != NULL); | 413 ASSERT_TRUE(value.get() != NULL); |
412 EXPECT_TRUE(value->Equals(&kIntegerValue)); | 414 EXPECT_TRUE(value->Equals(&kIntegerValue)); |
413 value.reset(dbus::PopDataAsValue(&reader)); | 415 value.reset(PopDataAsValue(&reader)); |
414 ASSERT_TRUE(value.get() != NULL); | 416 ASSERT_TRUE(value.get() != NULL); |
415 EXPECT_TRUE(value->Equals(&kDoubleValue)); | 417 EXPECT_TRUE(value->Equals(&kDoubleValue)); |
416 value.reset(dbus::PopDataAsValue(&reader)); | 418 value.reset(PopDataAsValue(&reader)); |
417 ASSERT_TRUE(value.get() != NULL); | 419 ASSERT_TRUE(value.get() != NULL); |
418 EXPECT_TRUE(value->Equals(&kStringValue)); | 420 EXPECT_TRUE(value->Equals(&kStringValue)); |
419 } | 421 } |
420 | 422 |
421 TEST(ValuesUtilTest, AppendBasicTypesAsVariant) { | 423 TEST(ValuesUtilTest, AppendBasicTypesAsVariant) { |
422 const base::FundamentalValue kBoolValue(false); | 424 const base::FundamentalValue kBoolValue(false); |
423 const base::FundamentalValue kIntegerValue(42); | 425 const base::FundamentalValue kIntegerValue(42); |
424 const base::FundamentalValue kDoubleValue(4.2); | 426 const base::FundamentalValue kDoubleValue(4.2); |
425 const base::StringValue kStringValue("string"); | 427 const base::StringValue kStringValue("string"); |
426 | 428 |
427 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 429 scoped_ptr<Response> response(Response::CreateEmpty()); |
428 dbus::MessageWriter writer(response.get()); | 430 MessageWriter writer(response.get()); |
429 AppendBasicTypeValueDataAsVariant(&writer, kBoolValue); | 431 AppendBasicTypeValueDataAsVariant(&writer, kBoolValue); |
430 AppendBasicTypeValueDataAsVariant(&writer, kIntegerValue); | 432 AppendBasicTypeValueDataAsVariant(&writer, kIntegerValue); |
431 AppendBasicTypeValueDataAsVariant(&writer, kDoubleValue); | 433 AppendBasicTypeValueDataAsVariant(&writer, kDoubleValue); |
432 AppendBasicTypeValueDataAsVariant(&writer, kStringValue); | 434 AppendBasicTypeValueDataAsVariant(&writer, kStringValue); |
433 | 435 |
434 dbus::MessageReader reader(response.get()); | 436 MessageReader reader(response.get()); |
435 scoped_ptr<base::Value> value; | 437 scoped_ptr<base::Value> value; |
436 value.reset(dbus::PopDataAsValue(&reader)); | 438 value.reset(PopDataAsValue(&reader)); |
437 ASSERT_TRUE(value.get() != NULL); | 439 ASSERT_TRUE(value.get() != NULL); |
438 EXPECT_TRUE(value->Equals(&kBoolValue)); | 440 EXPECT_TRUE(value->Equals(&kBoolValue)); |
439 value.reset(dbus::PopDataAsValue(&reader)); | 441 value.reset(PopDataAsValue(&reader)); |
440 ASSERT_TRUE(value.get() != NULL); | 442 ASSERT_TRUE(value.get() != NULL); |
441 EXPECT_TRUE(value->Equals(&kIntegerValue)); | 443 EXPECT_TRUE(value->Equals(&kIntegerValue)); |
442 value.reset(dbus::PopDataAsValue(&reader)); | 444 value.reset(PopDataAsValue(&reader)); |
443 ASSERT_TRUE(value.get() != NULL); | 445 ASSERT_TRUE(value.get() != NULL); |
444 EXPECT_TRUE(value->Equals(&kDoubleValue)); | 446 EXPECT_TRUE(value->Equals(&kDoubleValue)); |
445 value.reset(dbus::PopDataAsValue(&reader)); | 447 value.reset(PopDataAsValue(&reader)); |
446 ASSERT_TRUE(value.get() != NULL); | 448 ASSERT_TRUE(value.get() != NULL); |
447 EXPECT_TRUE(value->Equals(&kStringValue)); | 449 EXPECT_TRUE(value->Equals(&kStringValue)); |
448 } | 450 } |
| 451 |
| 452 } // namespace dbus |
OLD | NEW |