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