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