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 scoped_ptr
<base::Value
> key_value(new base::FundamentalValue(keys
[i
]));
381 std::string key_string
;
382 base::JSONWriter::Write(key_value
.get(), &key_string
);
383 dictionary_value
.SetWithoutPathExpansion(
384 key_string
, new base::FundamentalValue(values
[i
]));
388 MessageReader
reader(response
.get());
389 scoped_ptr
<base::Value
> value(PopDataAsValue(&reader
));
390 ASSERT_TRUE(value
.get() != NULL
);
391 EXPECT_TRUE(value
->Equals(&dictionary_value
));
394 TEST(ValuesUtilTest
, AppendBasicTypes
) {
395 const base::FundamentalValue
kBoolValue(false);
396 const base::FundamentalValue
kIntegerValue(42);
397 const base::FundamentalValue
kDoubleValue(4.2);
398 const base::StringValue
kStringValue("string");
400 scoped_ptr
<Response
> response(Response::CreateEmpty());
401 MessageWriter
writer(response
.get());
402 AppendBasicTypeValueData(&writer
, kBoolValue
);
403 AppendBasicTypeValueData(&writer
, kIntegerValue
);
404 AppendBasicTypeValueData(&writer
, kDoubleValue
);
405 AppendBasicTypeValueData(&writer
, kStringValue
);
407 MessageReader
reader(response
.get());
408 scoped_ptr
<base::Value
> value
;
409 value
.reset(PopDataAsValue(&reader
));
410 ASSERT_TRUE(value
.get() != NULL
);
411 EXPECT_TRUE(value
->Equals(&kBoolValue
));
412 value
.reset(PopDataAsValue(&reader
));
413 ASSERT_TRUE(value
.get() != NULL
);
414 EXPECT_TRUE(value
->Equals(&kIntegerValue
));
415 value
.reset(PopDataAsValue(&reader
));
416 ASSERT_TRUE(value
.get() != NULL
);
417 EXPECT_TRUE(value
->Equals(&kDoubleValue
));
418 value
.reset(PopDataAsValue(&reader
));
419 ASSERT_TRUE(value
.get() != NULL
);
420 EXPECT_TRUE(value
->Equals(&kStringValue
));
423 TEST(ValuesUtilTest
, AppendBasicTypesAsVariant
) {
424 const base::FundamentalValue
kBoolValue(false);
425 const base::FundamentalValue
kIntegerValue(42);
426 const base::FundamentalValue
kDoubleValue(4.2);
427 const base::StringValue
kStringValue("string");
429 scoped_ptr
<Response
> response(Response::CreateEmpty());
430 MessageWriter
writer(response
.get());
431 AppendBasicTypeValueDataAsVariant(&writer
, kBoolValue
);
432 AppendBasicTypeValueDataAsVariant(&writer
, kIntegerValue
);
433 AppendBasicTypeValueDataAsVariant(&writer
, kDoubleValue
);
434 AppendBasicTypeValueDataAsVariant(&writer
, kStringValue
);
436 MessageReader
reader(response
.get());
437 scoped_ptr
<base::Value
> value
;
438 value
.reset(PopDataAsValue(&reader
));
439 ASSERT_TRUE(value
.get() != NULL
);
440 EXPECT_TRUE(value
->Equals(&kBoolValue
));
441 value
.reset(PopDataAsValue(&reader
));
442 ASSERT_TRUE(value
.get() != NULL
);
443 EXPECT_TRUE(value
->Equals(&kIntegerValue
));
444 value
.reset(PopDataAsValue(&reader
));
445 ASSERT_TRUE(value
.get() != NULL
);
446 EXPECT_TRUE(value
->Equals(&kDoubleValue
));
447 value
.reset(PopDataAsValue(&reader
));
448 ASSERT_TRUE(value
.get() != NULL
);
449 EXPECT_TRUE(value
->Equals(&kStringValue
));
452 TEST(ValuesUtilTest
, AppendValueDataBasicTypes
) {
453 const base::FundamentalValue
kBoolValue(false);
454 const base::FundamentalValue
kIntegerValue(42);
455 const base::FundamentalValue
kDoubleValue(4.2);
456 const base::StringValue
kStringValue("string");
458 scoped_ptr
<Response
> response(Response::CreateEmpty());
459 MessageWriter
writer(response
.get());
460 AppendValueData(&writer
, kBoolValue
);
461 AppendValueData(&writer
, kIntegerValue
);
462 AppendValueData(&writer
, kDoubleValue
);
463 AppendValueData(&writer
, kStringValue
);
465 MessageReader
reader(response
.get());
466 scoped_ptr
<base::Value
> value
;
467 value
.reset(PopDataAsValue(&reader
));
468 ASSERT_TRUE(value
.get() != NULL
);
469 EXPECT_TRUE(value
->Equals(&kBoolValue
));
470 value
.reset(PopDataAsValue(&reader
));
471 ASSERT_TRUE(value
.get() != NULL
);
472 EXPECT_TRUE(value
->Equals(&kIntegerValue
));
473 value
.reset(PopDataAsValue(&reader
));
474 ASSERT_TRUE(value
.get() != NULL
);
475 EXPECT_TRUE(value
->Equals(&kDoubleValue
));
476 value
.reset(PopDataAsValue(&reader
));
477 ASSERT_TRUE(value
.get() != NULL
);
478 EXPECT_TRUE(value
->Equals(&kStringValue
));
481 TEST(ValuesUtilTest
, AppendValueDataAsVariantBasicTypes
) {
482 const base::FundamentalValue
kBoolValue(false);
483 const base::FundamentalValue
kIntegerValue(42);
484 const base::FundamentalValue
kDoubleValue(4.2);
485 const base::StringValue
kStringValue("string");
487 scoped_ptr
<Response
> response(Response::CreateEmpty());
488 MessageWriter
writer(response
.get());
489 AppendValueDataAsVariant(&writer
, kBoolValue
);
490 AppendValueDataAsVariant(&writer
, kIntegerValue
);
491 AppendValueDataAsVariant(&writer
, kDoubleValue
);
492 AppendValueDataAsVariant(&writer
, kStringValue
);
494 MessageReader
reader(response
.get());
495 scoped_ptr
<base::Value
> value
;
496 value
.reset(PopDataAsValue(&reader
));
497 ASSERT_TRUE(value
.get() != NULL
);
498 EXPECT_TRUE(value
->Equals(&kBoolValue
));
499 value
.reset(PopDataAsValue(&reader
));
500 ASSERT_TRUE(value
.get() != NULL
);
501 EXPECT_TRUE(value
->Equals(&kIntegerValue
));
502 value
.reset(PopDataAsValue(&reader
));
503 ASSERT_TRUE(value
.get() != NULL
);
504 EXPECT_TRUE(value
->Equals(&kDoubleValue
));
505 value
.reset(PopDataAsValue(&reader
));
506 ASSERT_TRUE(value
.get() != NULL
);
507 EXPECT_TRUE(value
->Equals(&kStringValue
));
510 TEST(ValuesUtilTest
, AppendDictionary
) {
511 // Set up the input dictionary.
512 const std::string kKey1
= "one";
513 const std::string kKey2
= "two";
514 const std::string kKey3
= "three";
515 const std::string kKey4
= "four";
516 const std::string kKey5
= "five";
517 const std::string kKey6
= "six";
519 const bool kBoolValue
= true;
520 const int32 kInt32Value
= -45;
521 const double kDoubleValue
= 4.9;
522 const std::string kStringValue
= "fifty";
524 base::ListValue
* list_value
= new base::ListValue();
525 list_value
->AppendBoolean(kBoolValue
);
526 list_value
->AppendInteger(kInt32Value
);
528 base::DictionaryValue
* dictionary_value
= new base::DictionaryValue();
529 dictionary_value
->SetBoolean(kKey1
, kBoolValue
);
530 dictionary_value
->SetInteger(kKey2
, kDoubleValue
);
532 base::DictionaryValue test_dictionary
;
533 test_dictionary
.SetBoolean(kKey1
, kBoolValue
);
534 test_dictionary
.SetInteger(kKey2
, kInt32Value
);
535 test_dictionary
.SetDouble(kKey3
, kDoubleValue
);
536 test_dictionary
.SetString(kKey4
, kStringValue
);
537 test_dictionary
.Set(kKey5
, list_value
); // takes ownership
538 test_dictionary
.Set(kKey6
, dictionary_value
); // takes ownership
540 scoped_ptr
<Response
> response(Response::CreateEmpty());
541 MessageWriter
writer(response
.get());
542 AppendValueData(&writer
, test_dictionary
);
543 base::FundamentalValue
int_value(kInt32Value
);
544 AppendValueData(&writer
, int_value
);
547 MessageReader
reader(response
.get());
548 scoped_ptr
<base::Value
> value
;
549 value
.reset(PopDataAsValue(&reader
));
550 ASSERT_TRUE(value
.get() != NULL
);
551 EXPECT_TRUE(value
->Equals(&test_dictionary
));
552 value
.reset(PopDataAsValue(&reader
));
553 ASSERT_TRUE(value
.get() != NULL
);
554 EXPECT_TRUE(value
->Equals(&int_value
));
557 TEST(ValuesUtilTest
, AppendDictionaryAsVariant
) {
558 // Set up the input dictionary.
559 const std::string kKey1
= "one";
560 const std::string kKey2
= "two";
561 const std::string kKey3
= "three";
562 const std::string kKey4
= "four";
563 const std::string kKey5
= "five";
564 const std::string kKey6
= "six";
566 const bool kBoolValue
= true;
567 const int32 kInt32Value
= -45;
568 const double kDoubleValue
= 4.9;
569 const std::string kStringValue
= "fifty";
571 base::ListValue
* list_value
= new base::ListValue();
572 list_value
->AppendBoolean(kBoolValue
);
573 list_value
->AppendInteger(kInt32Value
);
575 base::DictionaryValue
* dictionary_value
= new base::DictionaryValue();
576 dictionary_value
->SetBoolean(kKey1
, kBoolValue
);
577 dictionary_value
->SetInteger(kKey2
, kDoubleValue
);
579 base::DictionaryValue test_dictionary
;
580 test_dictionary
.SetBoolean(kKey1
, kBoolValue
);
581 test_dictionary
.SetInteger(kKey2
, kInt32Value
);
582 test_dictionary
.SetDouble(kKey3
, kDoubleValue
);
583 test_dictionary
.SetString(kKey4
, kStringValue
);
584 test_dictionary
.Set(kKey5
, list_value
); // takes ownership
585 test_dictionary
.Set(kKey6
, dictionary_value
); // takes ownership
587 scoped_ptr
<Response
> response(Response::CreateEmpty());
588 MessageWriter
writer(response
.get());
589 AppendValueDataAsVariant(&writer
, test_dictionary
);
590 base::FundamentalValue
int_value(kInt32Value
);
591 AppendValueData(&writer
, int_value
);
594 MessageReader
reader(response
.get());
595 scoped_ptr
<base::Value
> value
;
596 value
.reset(PopDataAsValue(&reader
));
597 ASSERT_TRUE(value
.get() != NULL
);
598 EXPECT_TRUE(value
->Equals(&test_dictionary
));
599 value
.reset(PopDataAsValue(&reader
));
600 ASSERT_TRUE(value
.get() != NULL
);
601 EXPECT_TRUE(value
->Equals(&int_value
));
604 TEST(ValuesUtilTest
, AppendList
) {
605 // Set up the input list.
606 const std::string kKey1
= "one";
607 const std::string kKey2
= "two";
609 const bool kBoolValue
= true;
610 const int32 kInt32Value
= -45;
611 const double kDoubleValue
= 4.9;
612 const std::string kStringValue
= "fifty";
614 base::ListValue
* list_value
= new base::ListValue();
615 list_value
->AppendBoolean(kBoolValue
);
616 list_value
->AppendInteger(kInt32Value
);
618 base::DictionaryValue
* dictionary_value
= new base::DictionaryValue();
619 dictionary_value
->SetBoolean(kKey1
, kBoolValue
);
620 dictionary_value
->SetInteger(kKey2
, kDoubleValue
);
622 base::ListValue test_list
;
623 test_list
.AppendBoolean(kBoolValue
);
624 test_list
.AppendInteger(kInt32Value
);
625 test_list
.AppendDouble(kDoubleValue
);
626 test_list
.AppendString(kStringValue
);
627 test_list
.Append(list_value
); // takes ownership
628 test_list
.Append(dictionary_value
); // takes ownership
630 scoped_ptr
<Response
> response(Response::CreateEmpty());
631 MessageWriter
writer(response
.get());
632 AppendValueData(&writer
, test_list
);
633 base::FundamentalValue
int_value(kInt32Value
);
634 AppendValueData(&writer
, int_value
);
637 MessageReader
reader(response
.get());
638 scoped_ptr
<base::Value
> value
;
639 value
.reset(PopDataAsValue(&reader
));
640 ASSERT_TRUE(value
.get() != NULL
);
641 EXPECT_TRUE(value
->Equals(&test_list
));
642 value
.reset(PopDataAsValue(&reader
));
643 ASSERT_TRUE(value
.get() != NULL
);
644 EXPECT_TRUE(value
->Equals(&int_value
));
647 TEST(ValuesUtilTest
, AppendListAsVariant
) {
648 // Set up the input list.
649 const std::string kKey1
= "one";
650 const std::string kKey2
= "two";
652 const bool kBoolValue
= true;
653 const int32 kInt32Value
= -45;
654 const double kDoubleValue
= 4.9;
655 const std::string kStringValue
= "fifty";
657 base::ListValue
* list_value
= new base::ListValue();
658 list_value
->AppendBoolean(kBoolValue
);
659 list_value
->AppendInteger(kInt32Value
);
661 base::DictionaryValue
* dictionary_value
= new base::DictionaryValue();
662 dictionary_value
->SetBoolean(kKey1
, kBoolValue
);
663 dictionary_value
->SetInteger(kKey2
, kDoubleValue
);
665 base::ListValue test_list
;
666 test_list
.AppendBoolean(kBoolValue
);
667 test_list
.AppendInteger(kInt32Value
);
668 test_list
.AppendDouble(kDoubleValue
);
669 test_list
.AppendString(kStringValue
);
670 test_list
.Append(list_value
); // takes ownership
671 test_list
.Append(dictionary_value
); // takes ownership
673 scoped_ptr
<Response
> response(Response::CreateEmpty());
674 MessageWriter
writer(response
.get());
675 AppendValueDataAsVariant(&writer
, test_list
);
676 base::FundamentalValue
int_value(kInt32Value
);
677 AppendValueData(&writer
, int_value
);
680 MessageReader
reader(response
.get());
681 scoped_ptr
<base::Value
> value
;
682 value
.reset(PopDataAsValue(&reader
));
683 ASSERT_TRUE(value
.get() != NULL
);
684 EXPECT_TRUE(value
->Equals(&test_list
));
685 value
.reset(PopDataAsValue(&reader
));
686 ASSERT_TRUE(value
.get() != NULL
);
687 EXPECT_TRUE(value
->Equals(&int_value
));