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.
5 #include "dbus/values_util.h"
10 #include "base/json/json_writer.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/values.h"
13 #include "dbus/message.h"
14 #include "testing/gtest/include/gtest/gtest.h"
18 TEST(ValuesUtilTest
, PopBasicTypes
) {
19 scoped_ptr
<Response
> response(Response::CreateEmpty());
20 // Append basic type values.
21 MessageWriter
writer(response
.get());
22 const uint8 kByteValue
= 42;
23 writer
.AppendByte(kByteValue
);
24 const bool kBoolValue
= true;
25 writer
.AppendBool(kBoolValue
);
26 const int16 kInt16Value
= -43;
27 writer
.AppendInt16(kInt16Value
);
28 const uint16 kUint16Value
= 44;
29 writer
.AppendUint16(kUint16Value
);
30 const int32 kInt32Value
= -45;
31 writer
.AppendInt32(kInt32Value
);
32 const uint32 kUint32Value
= 46;
33 writer
.AppendUint32(kUint32Value
);
34 const int64 kInt64Value
= -47;
35 writer
.AppendInt64(kInt64Value
);
36 const uint64 kUint64Value
= 48;
37 writer
.AppendUint64(kUint64Value
);
38 const double kDoubleValue
= 4.9;
39 writer
.AppendDouble(kDoubleValue
);
40 const std::string kStringValue
= "fifty";
41 writer
.AppendString(kStringValue
);
42 const std::string kEmptyStringValue
;
43 writer
.AppendString(kEmptyStringValue
);
44 const ObjectPath
kObjectPathValue("/ObjectPath");
45 writer
.AppendObjectPath(kObjectPathValue
);
47 MessageReader
reader(response
.get());
48 scoped_ptr
<base::Value
> value
;
49 scoped_ptr
<base::Value
> expected_value
;
51 value
.reset(PopDataAsValue(&reader
));
52 ASSERT_TRUE(value
.get() != NULL
);
53 expected_value
.reset(new base::FundamentalValue(kByteValue
));
54 EXPECT_TRUE(value
->Equals(expected_value
.get()));
56 value
.reset(PopDataAsValue(&reader
));
57 ASSERT_TRUE(value
.get() != NULL
);
58 expected_value
.reset(new base::FundamentalValue(kBoolValue
));
59 EXPECT_TRUE(value
->Equals(expected_value
.get()));
61 value
.reset(PopDataAsValue(&reader
));
62 ASSERT_TRUE(value
.get() != NULL
);
63 expected_value
.reset(new base::FundamentalValue(kInt16Value
));
64 EXPECT_TRUE(value
->Equals(expected_value
.get()));
66 value
.reset(PopDataAsValue(&reader
));
67 ASSERT_TRUE(value
.get() != NULL
);
68 expected_value
.reset(new base::FundamentalValue(kUint16Value
));
69 EXPECT_TRUE(value
->Equals(expected_value
.get()));
71 value
.reset(PopDataAsValue(&reader
));
72 ASSERT_TRUE(value
.get() != NULL
);
73 expected_value
.reset(new base::FundamentalValue(kInt32Value
));
74 EXPECT_TRUE(value
->Equals(expected_value
.get()));
76 value
.reset(PopDataAsValue(&reader
));
77 ASSERT_TRUE(value
.get() != NULL
);
79 new base::FundamentalValue(static_cast<double>(kUint32Value
)));
80 EXPECT_TRUE(value
->Equals(expected_value
.get()));
82 value
.reset(PopDataAsValue(&reader
));
83 ASSERT_TRUE(value
.get() != NULL
);
85 new base::FundamentalValue(static_cast<double>(kInt64Value
)));
86 EXPECT_TRUE(value
->Equals(expected_value
.get()));
88 value
.reset(PopDataAsValue(&reader
));
89 ASSERT_TRUE(value
.get() != NULL
);
91 new base::FundamentalValue(static_cast<double>(kUint64Value
)));
92 EXPECT_TRUE(value
->Equals(expected_value
.get()));
94 value
.reset(PopDataAsValue(&reader
));
95 ASSERT_TRUE(value
.get() != NULL
);
96 expected_value
.reset(new base::FundamentalValue(kDoubleValue
));
97 EXPECT_TRUE(value
->Equals(expected_value
.get()));
99 value
.reset(PopDataAsValue(&reader
));
100 ASSERT_TRUE(value
.get() != NULL
);
101 expected_value
.reset(new base::StringValue(kStringValue
));
102 EXPECT_TRUE(value
->Equals(expected_value
.get()));
103 // Pop an empty string.
104 value
.reset(PopDataAsValue(&reader
));
105 ASSERT_TRUE(value
.get() != NULL
);
106 expected_value
.reset(new base::StringValue(kEmptyStringValue
));
107 EXPECT_TRUE(value
->Equals(expected_value
.get()));
108 // Pop an object path.
109 value
.reset(PopDataAsValue(&reader
));
110 ASSERT_TRUE(value
.get() != NULL
);
111 expected_value
.reset(new base::StringValue(kObjectPathValue
.value()));
112 EXPECT_TRUE(value
->Equals(expected_value
.get()));
115 TEST(ValuesUtilTest
, PopVariant
) {
116 scoped_ptr
<Response
> response(Response::CreateEmpty());
117 // Append variant values.
118 MessageWriter
writer(response
.get());
119 const bool kBoolValue
= true;
120 writer
.AppendVariantOfBool(kBoolValue
);
121 const int32 kInt32Value
= -45;
122 writer
.AppendVariantOfInt32(kInt32Value
);
123 const double kDoubleValue
= 4.9;
124 writer
.AppendVariantOfDouble(kDoubleValue
);
125 const std::string kStringValue
= "fifty";
126 writer
.AppendVariantOfString(kStringValue
);
128 MessageReader
reader(response
.get());
129 scoped_ptr
<base::Value
> value
;
130 scoped_ptr
<base::Value
> expected_value
;
132 value
.reset(PopDataAsValue(&reader
));
133 ASSERT_TRUE(value
.get() != NULL
);
134 expected_value
.reset(new base::FundamentalValue(kBoolValue
));
135 EXPECT_TRUE(value
->Equals(expected_value
.get()));
137 value
.reset(PopDataAsValue(&reader
));
138 ASSERT_TRUE(value
.get() != NULL
);
139 expected_value
.reset(new base::FundamentalValue(kInt32Value
));
140 EXPECT_TRUE(value
->Equals(expected_value
.get()));
142 value
.reset(PopDataAsValue(&reader
));
143 ASSERT_TRUE(value
.get() != NULL
);
144 expected_value
.reset(new base::FundamentalValue(kDoubleValue
));
145 EXPECT_TRUE(value
->Equals(expected_value
.get()));
147 value
.reset(PopDataAsValue(&reader
));
148 ASSERT_TRUE(value
.get() != NULL
);
149 expected_value
.reset(new base::StringValue(kStringValue
));
150 EXPECT_TRUE(value
->Equals(expected_value
.get()));
153 // Pop extremely large integers which cannot be precisely represented in
155 TEST(ValuesUtilTest
, PopExtremelyLargeIntegers
) {
156 scoped_ptr
<Response
> response(Response::CreateEmpty());
157 // Append large integers.
158 MessageWriter
writer(response
.get());
159 const int64 kInt64Value
= -123456789012345689LL;
160 writer
.AppendInt64(kInt64Value
);
161 const uint64 kUint64Value
= 9876543210987654321ULL;
162 writer
.AppendUint64(kUint64Value
);
164 MessageReader
reader(response
.get());
165 scoped_ptr
<base::Value
> value
;
166 scoped_ptr
<base::Value
> expected_value
;
167 double double_value
= 0;
169 value
.reset(PopDataAsValue(&reader
));
170 ASSERT_TRUE(value
.get() != NULL
);
171 expected_value
.reset(
172 new base::FundamentalValue(static_cast<double>(kInt64Value
)));
173 EXPECT_TRUE(value
->Equals(expected_value
.get()));
174 ASSERT_TRUE(value
->GetAsDouble(&double_value
));
175 EXPECT_NE(kInt64Value
, static_cast<int64
>(double_value
));
177 value
.reset(PopDataAsValue(&reader
));
178 ASSERT_TRUE(value
.get() != NULL
);
179 expected_value
.reset(
180 new base::FundamentalValue(static_cast<double>(kUint64Value
)));
181 EXPECT_TRUE(value
->Equals(expected_value
.get()));
182 ASSERT_TRUE(value
->GetAsDouble(&double_value
));
183 EXPECT_NE(kUint64Value
, static_cast<uint64
>(double_value
));
186 TEST(ValuesUtilTest
, PopIntArray
) {
187 scoped_ptr
<Response
> response(Response::CreateEmpty());
188 // Append an int32 array.
189 MessageWriter
writer(response
.get());
190 MessageWriter
sub_writer(NULL
);
191 std::vector
<int32
> data
;
195 writer
.OpenArray("i", &sub_writer
);
196 for (size_t i
= 0; i
!= data
.size(); ++i
)
197 sub_writer
.AppendInt32(data
[i
]);
198 writer
.CloseContainer(&sub_writer
);
200 // Create the expected value.
201 scoped_ptr
<base::ListValue
> list_value(new base::ListValue
);
202 for (size_t i
= 0; i
!= data
.size(); ++i
)
203 list_value
->Append(new base::FundamentalValue(data
[i
]));
205 // Pop an int32 array.
206 MessageReader
reader(response
.get());
207 scoped_ptr
<base::Value
> value(PopDataAsValue(&reader
));
208 ASSERT_TRUE(value
.get() != NULL
);
209 EXPECT_TRUE(value
->Equals(list_value
.get()));
212 TEST(ValuesUtilTest
, PopStringArray
) {
213 scoped_ptr
<Response
> response(Response::CreateEmpty());
214 // Append a string array.
215 MessageWriter
writer(response
.get());
216 MessageWriter
sub_writer(NULL
);
217 std::vector
<std::string
> data
;
218 data
.push_back("Dreamlifter");
219 data
.push_back("Beluga");
220 data
.push_back("Mriya");
221 writer
.AppendArrayOfStrings(data
);
223 // Create the expected value.
224 scoped_ptr
<base::ListValue
> list_value(new base::ListValue
);
225 for (size_t i
= 0; i
!= data
.size(); ++i
)
226 list_value
->Append(new base::StringValue(data
[i
]));
228 // Pop a string array.
229 MessageReader
reader(response
.get());
230 scoped_ptr
<base::Value
> value(PopDataAsValue(&reader
));
231 ASSERT_TRUE(value
.get() != NULL
);
232 EXPECT_TRUE(value
->Equals(list_value
.get()));
235 TEST(ValuesUtilTest
, PopStruct
) {
236 scoped_ptr
<Response
> response(Response::CreateEmpty());
238 MessageWriter
writer(response
.get());
239 MessageWriter
sub_writer(NULL
);
240 writer
.OpenStruct(&sub_writer
);
241 const bool kBoolValue
= true;
242 sub_writer
.AppendBool(kBoolValue
);
243 const int32 kInt32Value
= -123;
244 sub_writer
.AppendInt32(kInt32Value
);
245 const double kDoubleValue
= 1.23;
246 sub_writer
.AppendDouble(kDoubleValue
);
247 const std::string kStringValue
= "one two three";
248 sub_writer
.AppendString(kStringValue
);
249 writer
.CloseContainer(&sub_writer
);
251 // Create the expected value.
252 base::ListValue list_value
;
253 list_value
.Append(new base::FundamentalValue(kBoolValue
));
254 list_value
.Append(new base::FundamentalValue(kInt32Value
));
255 list_value
.Append(new base::FundamentalValue(kDoubleValue
));
256 list_value
.Append(new base::StringValue(kStringValue
));
259 MessageReader
reader(response
.get());
260 scoped_ptr
<base::Value
> value(PopDataAsValue(&reader
));
261 ASSERT_TRUE(value
.get() != NULL
);
262 EXPECT_TRUE(value
->Equals(&list_value
));
265 TEST(ValuesUtilTest
, PopStringToVariantDictionary
) {
266 scoped_ptr
<Response
> response(Response::CreateEmpty());
267 // Append a dictionary.
268 MessageWriter
writer(response
.get());
269 MessageWriter
sub_writer(NULL
);
270 MessageWriter
entry_writer(NULL
);
271 writer
.OpenArray("{sv}", &sub_writer
);
272 sub_writer
.OpenDictEntry(&entry_writer
);
273 const std::string kKey1
= "one";
274 entry_writer
.AppendString(kKey1
);
275 const bool kBoolValue
= true;
276 entry_writer
.AppendVariantOfBool(kBoolValue
);
277 sub_writer
.CloseContainer(&entry_writer
);
278 sub_writer
.OpenDictEntry(&entry_writer
);
279 const std::string kKey2
= "two";
280 entry_writer
.AppendString(kKey2
);
281 const int32 kInt32Value
= -45;
282 entry_writer
.AppendVariantOfInt32(kInt32Value
);
283 sub_writer
.CloseContainer(&entry_writer
);
284 sub_writer
.OpenDictEntry(&entry_writer
);
285 const std::string kKey3
= "three";
286 entry_writer
.AppendString(kKey3
);
287 const double kDoubleValue
= 4.9;
288 entry_writer
.AppendVariantOfDouble(kDoubleValue
);
289 sub_writer
.CloseContainer(&entry_writer
);
290 sub_writer
.OpenDictEntry(&entry_writer
);
291 const std::string kKey4
= "four";
292 entry_writer
.AppendString(kKey4
);
293 const std::string kStringValue
= "fifty";
294 entry_writer
.AppendVariantOfString(kStringValue
);
295 sub_writer
.CloseContainer(&entry_writer
);
296 writer
.CloseContainer(&sub_writer
);
298 // Create the expected value.
299 base::DictionaryValue dictionary_value
;
300 dictionary_value
.SetBoolean(kKey1
, kBoolValue
);
301 dictionary_value
.SetInteger(kKey2
, kInt32Value
);
302 dictionary_value
.SetDouble(kKey3
, kDoubleValue
);
303 dictionary_value
.SetString(kKey4
, kStringValue
);
306 MessageReader
reader(response
.get());
307 scoped_ptr
<base::Value
> value(PopDataAsValue(&reader
));
308 ASSERT_TRUE(value
.get() != NULL
);
309 EXPECT_TRUE(value
->Equals(&dictionary_value
));
312 TEST(ValuesUtilTest
, PopDictionaryWithDottedStringKey
) {
313 scoped_ptr
<Response
> response(Response::CreateEmpty());
314 // Append a dictionary.
315 MessageWriter
writer(response
.get());
316 MessageWriter
sub_writer(NULL
);
317 MessageWriter
entry_writer(NULL
);
318 writer
.OpenArray("{sv}", &sub_writer
);
319 sub_writer
.OpenDictEntry(&entry_writer
);
320 const std::string kKey1
= "www.example.com"; // String including dots.
321 entry_writer
.AppendString(kKey1
);
322 const bool kBoolValue
= true;
323 entry_writer
.AppendVariantOfBool(kBoolValue
);
324 sub_writer
.CloseContainer(&entry_writer
);
325 sub_writer
.OpenDictEntry(&entry_writer
);
326 const std::string kKey2
= ".example"; // String starting with a dot.
327 entry_writer
.AppendString(kKey2
);
328 const int32 kInt32Value
= -45;
329 entry_writer
.AppendVariantOfInt32(kInt32Value
);
330 sub_writer
.CloseContainer(&entry_writer
);
331 sub_writer
.OpenDictEntry(&entry_writer
);
332 const std::string kKey3
= "example."; // String ending with a dot.
333 entry_writer
.AppendString(kKey3
);
334 const double kDoubleValue
= 4.9;
335 entry_writer
.AppendVariantOfDouble(kDoubleValue
);
336 sub_writer
.CloseContainer(&entry_writer
);
337 writer
.CloseContainer(&sub_writer
);
339 // Create the expected value.
340 base::DictionaryValue dictionary_value
;
341 dictionary_value
.SetWithoutPathExpansion(
342 kKey1
, new base::FundamentalValue(kBoolValue
));
343 dictionary_value
.SetWithoutPathExpansion(
344 kKey2
, new base::FundamentalValue(kInt32Value
));
345 dictionary_value
.SetWithoutPathExpansion(
346 kKey3
, new base::FundamentalValue(kDoubleValue
));
349 MessageReader
reader(response
.get());
350 scoped_ptr
<base::Value
> value(PopDataAsValue(&reader
));
351 ASSERT_TRUE(value
.get() != NULL
);
352 EXPECT_TRUE(value
->Equals(&dictionary_value
));
355 TEST(ValuesUtilTest
, PopDoubleToIntDictionary
) {
357 const int32 kValues
[] = {0, 1, 1, 2, 3, 5, 8, 13, 21};
358 const std::vector
<int32
> values(kValues
, kValues
+ arraysize(kValues
));
359 std::vector
<double> keys(values
.size());
360 for (size_t i
= 0; i
!= values
.size(); ++i
)
361 keys
[i
] = std::sqrt(values
[i
]);
363 // Append a dictionary.
364 scoped_ptr
<Response
> response(Response::CreateEmpty());
365 MessageWriter
writer(response
.get());
366 MessageWriter
sub_writer(NULL
);
367 writer
.OpenArray("{di}", &sub_writer
);
368 for (size_t i
= 0; i
!= values
.size(); ++i
) {
369 MessageWriter
entry_writer(NULL
);
370 sub_writer
.OpenDictEntry(&entry_writer
);
371 entry_writer
.AppendDouble(keys
[i
]);
372 entry_writer
.AppendInt32(values
[i
]);
373 sub_writer
.CloseContainer(&entry_writer
);
375 writer
.CloseContainer(&sub_writer
);
377 // Create the expected value.
378 base::DictionaryValue dictionary_value
;
379 for (size_t i
= 0; i
!= values
.size(); ++i
) {
380 std::string key_string
;
381 base::JSONWriter::Write(base::FundamentalValue(keys
[i
]), &key_string
);
382 dictionary_value
.SetIntegerWithoutPathExpansion(key_string
, values
[i
]);
386 MessageReader
reader(response
.get());
387 scoped_ptr
<base::Value
> value(PopDataAsValue(&reader
));
388 ASSERT_TRUE(value
.get() != NULL
);
389 EXPECT_TRUE(value
->Equals(&dictionary_value
));
392 TEST(ValuesUtilTest
, AppendBasicTypes
) {
393 const base::FundamentalValue
kBoolValue(false);
394 const base::FundamentalValue
kIntegerValue(42);
395 const base::FundamentalValue
kDoubleValue(4.2);
396 const base::StringValue
kStringValue("string");
398 scoped_ptr
<Response
> response(Response::CreateEmpty());
399 MessageWriter
writer(response
.get());
400 AppendBasicTypeValueData(&writer
, kBoolValue
);
401 AppendBasicTypeValueData(&writer
, kIntegerValue
);
402 AppendBasicTypeValueData(&writer
, kDoubleValue
);
403 AppendBasicTypeValueData(&writer
, kStringValue
);
405 MessageReader
reader(response
.get());
406 scoped_ptr
<base::Value
> value
;
407 value
.reset(PopDataAsValue(&reader
));
408 ASSERT_TRUE(value
.get() != NULL
);
409 EXPECT_TRUE(value
->Equals(&kBoolValue
));
410 value
.reset(PopDataAsValue(&reader
));
411 ASSERT_TRUE(value
.get() != NULL
);
412 EXPECT_TRUE(value
->Equals(&kIntegerValue
));
413 value
.reset(PopDataAsValue(&reader
));
414 ASSERT_TRUE(value
.get() != NULL
);
415 EXPECT_TRUE(value
->Equals(&kDoubleValue
));
416 value
.reset(PopDataAsValue(&reader
));
417 ASSERT_TRUE(value
.get() != NULL
);
418 EXPECT_TRUE(value
->Equals(&kStringValue
));
421 TEST(ValuesUtilTest
, AppendBasicTypesAsVariant
) {
422 const base::FundamentalValue
kBoolValue(false);
423 const base::FundamentalValue
kIntegerValue(42);
424 const base::FundamentalValue
kDoubleValue(4.2);
425 const base::StringValue
kStringValue("string");
427 scoped_ptr
<Response
> response(Response::CreateEmpty());
428 MessageWriter
writer(response
.get());
429 AppendBasicTypeValueDataAsVariant(&writer
, kBoolValue
);
430 AppendBasicTypeValueDataAsVariant(&writer
, kIntegerValue
);
431 AppendBasicTypeValueDataAsVariant(&writer
, kDoubleValue
);
432 AppendBasicTypeValueDataAsVariant(&writer
, kStringValue
);
434 MessageReader
reader(response
.get());
435 scoped_ptr
<base::Value
> value
;
436 value
.reset(PopDataAsValue(&reader
));
437 ASSERT_TRUE(value
.get() != NULL
);
438 EXPECT_TRUE(value
->Equals(&kBoolValue
));
439 value
.reset(PopDataAsValue(&reader
));
440 ASSERT_TRUE(value
.get() != NULL
);
441 EXPECT_TRUE(value
->Equals(&kIntegerValue
));
442 value
.reset(PopDataAsValue(&reader
));
443 ASSERT_TRUE(value
.get() != NULL
);
444 EXPECT_TRUE(value
->Equals(&kDoubleValue
));
445 value
.reset(PopDataAsValue(&reader
));
446 ASSERT_TRUE(value
.get() != NULL
);
447 EXPECT_TRUE(value
->Equals(&kStringValue
));
450 TEST(ValuesUtilTest
, AppendValueDataBasicTypes
) {
451 const base::FundamentalValue
kBoolValue(false);
452 const base::FundamentalValue
kIntegerValue(42);
453 const base::FundamentalValue
kDoubleValue(4.2);
454 const base::StringValue
kStringValue("string");
456 scoped_ptr
<Response
> response(Response::CreateEmpty());
457 MessageWriter
writer(response
.get());
458 AppendValueData(&writer
, kBoolValue
);
459 AppendValueData(&writer
, kIntegerValue
);
460 AppendValueData(&writer
, kDoubleValue
);
461 AppendValueData(&writer
, kStringValue
);
463 MessageReader
reader(response
.get());
464 scoped_ptr
<base::Value
> value
;
465 value
.reset(PopDataAsValue(&reader
));
466 ASSERT_TRUE(value
.get() != NULL
);
467 EXPECT_TRUE(value
->Equals(&kBoolValue
));
468 value
.reset(PopDataAsValue(&reader
));
469 ASSERT_TRUE(value
.get() != NULL
);
470 EXPECT_TRUE(value
->Equals(&kIntegerValue
));
471 value
.reset(PopDataAsValue(&reader
));
472 ASSERT_TRUE(value
.get() != NULL
);
473 EXPECT_TRUE(value
->Equals(&kDoubleValue
));
474 value
.reset(PopDataAsValue(&reader
));
475 ASSERT_TRUE(value
.get() != NULL
);
476 EXPECT_TRUE(value
->Equals(&kStringValue
));
479 TEST(ValuesUtilTest
, AppendValueDataAsVariantBasicTypes
) {
480 const base::FundamentalValue
kBoolValue(false);
481 const base::FundamentalValue
kIntegerValue(42);
482 const base::FundamentalValue
kDoubleValue(4.2);
483 const base::StringValue
kStringValue("string");
485 scoped_ptr
<Response
> response(Response::CreateEmpty());
486 MessageWriter
writer(response
.get());
487 AppendValueDataAsVariant(&writer
, kBoolValue
);
488 AppendValueDataAsVariant(&writer
, kIntegerValue
);
489 AppendValueDataAsVariant(&writer
, kDoubleValue
);
490 AppendValueDataAsVariant(&writer
, kStringValue
);
492 MessageReader
reader(response
.get());
493 scoped_ptr
<base::Value
> value
;
494 value
.reset(PopDataAsValue(&reader
));
495 ASSERT_TRUE(value
.get() != NULL
);
496 EXPECT_TRUE(value
->Equals(&kBoolValue
));
497 value
.reset(PopDataAsValue(&reader
));
498 ASSERT_TRUE(value
.get() != NULL
);
499 EXPECT_TRUE(value
->Equals(&kIntegerValue
));
500 value
.reset(PopDataAsValue(&reader
));
501 ASSERT_TRUE(value
.get() != NULL
);
502 EXPECT_TRUE(value
->Equals(&kDoubleValue
));
503 value
.reset(PopDataAsValue(&reader
));
504 ASSERT_TRUE(value
.get() != NULL
);
505 EXPECT_TRUE(value
->Equals(&kStringValue
));
508 TEST(ValuesUtilTest
, AppendDictionary
) {
509 // Set up the input dictionary.
510 const std::string kKey1
= "one";
511 const std::string kKey2
= "two";
512 const std::string kKey3
= "three";
513 const std::string kKey4
= "four";
514 const std::string kKey5
= "five";
515 const std::string kKey6
= "six";
517 const bool kBoolValue
= true;
518 const int32 kInt32Value
= -45;
519 const double kDoubleValue
= 4.9;
520 const std::string kStringValue
= "fifty";
522 base::ListValue
* list_value
= new base::ListValue();
523 list_value
->AppendBoolean(kBoolValue
);
524 list_value
->AppendInteger(kInt32Value
);
526 base::DictionaryValue
* dictionary_value
= new base::DictionaryValue();
527 dictionary_value
->SetBoolean(kKey1
, kBoolValue
);
528 dictionary_value
->SetInteger(kKey2
, kDoubleValue
);
530 base::DictionaryValue test_dictionary
;
531 test_dictionary
.SetBoolean(kKey1
, kBoolValue
);
532 test_dictionary
.SetInteger(kKey2
, kInt32Value
);
533 test_dictionary
.SetDouble(kKey3
, kDoubleValue
);
534 test_dictionary
.SetString(kKey4
, kStringValue
);
535 test_dictionary
.Set(kKey5
, list_value
); // takes ownership
536 test_dictionary
.Set(kKey6
, dictionary_value
); // takes ownership
538 scoped_ptr
<Response
> response(Response::CreateEmpty());
539 MessageWriter
writer(response
.get());
540 AppendValueData(&writer
, test_dictionary
);
541 base::FundamentalValue
int_value(kInt32Value
);
542 AppendValueData(&writer
, int_value
);
545 MessageReader
reader(response
.get());
546 scoped_ptr
<base::Value
> value
;
547 value
.reset(PopDataAsValue(&reader
));
548 ASSERT_TRUE(value
.get() != NULL
);
549 EXPECT_TRUE(value
->Equals(&test_dictionary
));
550 value
.reset(PopDataAsValue(&reader
));
551 ASSERT_TRUE(value
.get() != NULL
);
552 EXPECT_TRUE(value
->Equals(&int_value
));
555 TEST(ValuesUtilTest
, AppendDictionaryAsVariant
) {
556 // Set up the input dictionary.
557 const std::string kKey1
= "one";
558 const std::string kKey2
= "two";
559 const std::string kKey3
= "three";
560 const std::string kKey4
= "four";
561 const std::string kKey5
= "five";
562 const std::string kKey6
= "six";
564 const bool kBoolValue
= true;
565 const int32 kInt32Value
= -45;
566 const double kDoubleValue
= 4.9;
567 const std::string kStringValue
= "fifty";
569 base::ListValue
* list_value
= new base::ListValue();
570 list_value
->AppendBoolean(kBoolValue
);
571 list_value
->AppendInteger(kInt32Value
);
573 base::DictionaryValue
* dictionary_value
= new base::DictionaryValue();
574 dictionary_value
->SetBoolean(kKey1
, kBoolValue
);
575 dictionary_value
->SetInteger(kKey2
, kDoubleValue
);
577 base::DictionaryValue test_dictionary
;
578 test_dictionary
.SetBoolean(kKey1
, kBoolValue
);
579 test_dictionary
.SetInteger(kKey2
, kInt32Value
);
580 test_dictionary
.SetDouble(kKey3
, kDoubleValue
);
581 test_dictionary
.SetString(kKey4
, kStringValue
);
582 test_dictionary
.Set(kKey5
, list_value
); // takes ownership
583 test_dictionary
.Set(kKey6
, dictionary_value
); // takes ownership
585 scoped_ptr
<Response
> response(Response::CreateEmpty());
586 MessageWriter
writer(response
.get());
587 AppendValueDataAsVariant(&writer
, test_dictionary
);
588 base::FundamentalValue
int_value(kInt32Value
);
589 AppendValueData(&writer
, int_value
);
592 MessageReader
reader(response
.get());
593 scoped_ptr
<base::Value
> value
;
594 value
.reset(PopDataAsValue(&reader
));
595 ASSERT_TRUE(value
.get() != NULL
);
596 EXPECT_TRUE(value
->Equals(&test_dictionary
));
597 value
.reset(PopDataAsValue(&reader
));
598 ASSERT_TRUE(value
.get() != NULL
);
599 EXPECT_TRUE(value
->Equals(&int_value
));
602 TEST(ValuesUtilTest
, AppendList
) {
603 // Set up the input list.
604 const std::string kKey1
= "one";
605 const std::string kKey2
= "two";
607 const bool kBoolValue
= true;
608 const int32 kInt32Value
= -45;
609 const double kDoubleValue
= 4.9;
610 const std::string kStringValue
= "fifty";
612 base::ListValue
* list_value
= new base::ListValue();
613 list_value
->AppendBoolean(kBoolValue
);
614 list_value
->AppendInteger(kInt32Value
);
616 base::DictionaryValue
* dictionary_value
= new base::DictionaryValue();
617 dictionary_value
->SetBoolean(kKey1
, kBoolValue
);
618 dictionary_value
->SetInteger(kKey2
, kDoubleValue
);
620 base::ListValue test_list
;
621 test_list
.AppendBoolean(kBoolValue
);
622 test_list
.AppendInteger(kInt32Value
);
623 test_list
.AppendDouble(kDoubleValue
);
624 test_list
.AppendString(kStringValue
);
625 test_list
.Append(list_value
); // takes ownership
626 test_list
.Append(dictionary_value
); // takes ownership
628 scoped_ptr
<Response
> response(Response::CreateEmpty());
629 MessageWriter
writer(response
.get());
630 AppendValueData(&writer
, test_list
);
631 base::FundamentalValue
int_value(kInt32Value
);
632 AppendValueData(&writer
, int_value
);
635 MessageReader
reader(response
.get());
636 scoped_ptr
<base::Value
> value
;
637 value
.reset(PopDataAsValue(&reader
));
638 ASSERT_TRUE(value
.get() != NULL
);
639 EXPECT_TRUE(value
->Equals(&test_list
));
640 value
.reset(PopDataAsValue(&reader
));
641 ASSERT_TRUE(value
.get() != NULL
);
642 EXPECT_TRUE(value
->Equals(&int_value
));
645 TEST(ValuesUtilTest
, AppendListAsVariant
) {
646 // Set up the input list.
647 const std::string kKey1
= "one";
648 const std::string kKey2
= "two";
650 const bool kBoolValue
= true;
651 const int32 kInt32Value
= -45;
652 const double kDoubleValue
= 4.9;
653 const std::string kStringValue
= "fifty";
655 base::ListValue
* list_value
= new base::ListValue();
656 list_value
->AppendBoolean(kBoolValue
);
657 list_value
->AppendInteger(kInt32Value
);
659 base::DictionaryValue
* dictionary_value
= new base::DictionaryValue();
660 dictionary_value
->SetBoolean(kKey1
, kBoolValue
);
661 dictionary_value
->SetInteger(kKey2
, kDoubleValue
);
663 base::ListValue test_list
;
664 test_list
.AppendBoolean(kBoolValue
);
665 test_list
.AppendInteger(kInt32Value
);
666 test_list
.AppendDouble(kDoubleValue
);
667 test_list
.AppendString(kStringValue
);
668 test_list
.Append(list_value
); // takes ownership
669 test_list
.Append(dictionary_value
); // takes ownership
671 scoped_ptr
<Response
> response(Response::CreateEmpty());
672 MessageWriter
writer(response
.get());
673 AppendValueDataAsVariant(&writer
, test_list
);
674 base::FundamentalValue
int_value(kInt32Value
);
675 AppendValueData(&writer
, int_value
);
678 MessageReader
reader(response
.get());
679 scoped_ptr
<base::Value
> value
;
680 value
.reset(PopDataAsValue(&reader
));
681 ASSERT_TRUE(value
.get() != NULL
);
682 EXPECT_TRUE(value
->Equals(&test_list
));
683 value
.reset(PopDataAsValue(&reader
));
684 ASSERT_TRUE(value
.get() != NULL
);
685 EXPECT_TRUE(value
->Equals(&int_value
));