blob: d310d68a48534f2a16ed892e20950b162d17fdd3 [file] [log] [blame]
[email protected]a5aae122012-03-16 23:08:421// 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.
4
5#include "dbus/values_util.h"
6
avi22437c692015-12-22 18:12:457#include <stddef.h>
8#include <stdint.h>
9
Hans Wennborg72193492015-04-24 21:38:0410#include <cmath>
dcheng2a193282016-04-08 22:55:0411#include <memory>
dcheng98e96a72016-06-11 03:41:4812#include <utility>
[email protected]a5aae122012-03-16 23:08:4213#include <vector>
14
[email protected]a5aae122012-03-16 23:08:4215#include "base/json/json_writer.h"
avi22437c692015-12-22 18:12:4516#include "base/macros.h"
[email protected]a5aae122012-03-16 23:08:4217#include "base/values.h"
18#include "dbus/message.h"
19#include "testing/gtest/include/gtest/gtest.h"
20
[email protected]2a57ca642013-06-13 06:37:1921namespace dbus {
22
[email protected]a5aae122012-03-16 23:08:4223TEST(ValuesUtilTest, PopBasicTypes) {
dcheng2a193282016-04-08 22:55:0424 std::unique_ptr<Response> response(Response::CreateEmpty());
[email protected]a5aae122012-03-16 23:08:4225 // Append basic type values.
[email protected]2a57ca642013-06-13 06:37:1926 MessageWriter writer(response.get());
avi22437c692015-12-22 18:12:4527 const uint8_t kByteValue = 42;
[email protected]a5aae122012-03-16 23:08:4228 writer.AppendByte(kByteValue);
29 const bool kBoolValue = true;
30 writer.AppendBool(kBoolValue);
avi22437c692015-12-22 18:12:4531 const int16_t kInt16Value = -43;
[email protected]a5aae122012-03-16 23:08:4232 writer.AppendInt16(kInt16Value);
avi22437c692015-12-22 18:12:4533 const uint16_t kUint16Value = 44;
[email protected]a5aae122012-03-16 23:08:4234 writer.AppendUint16(kUint16Value);
avi22437c692015-12-22 18:12:4535 const int32_t kInt32Value = -45;
[email protected]a5aae122012-03-16 23:08:4236 writer.AppendInt32(kInt32Value);
avi22437c692015-12-22 18:12:4537 const uint32_t kUint32Value = 46;
[email protected]a5aae122012-03-16 23:08:4238 writer.AppendUint32(kUint32Value);
avi22437c692015-12-22 18:12:4539 const int64_t kInt64Value = -47;
[email protected]a5aae122012-03-16 23:08:4240 writer.AppendInt64(kInt64Value);
avi22437c692015-12-22 18:12:4541 const uint64_t kUint64Value = 48;
[email protected]a5aae122012-03-16 23:08:4242 writer.AppendUint64(kUint64Value);
43 const double kDoubleValue = 4.9;
44 writer.AppendDouble(kDoubleValue);
45 const std::string kStringValue = "fifty";
46 writer.AppendString(kStringValue);
47 const std::string kEmptyStringValue;
48 writer.AppendString(kEmptyStringValue);
[email protected]2a57ca642013-06-13 06:37:1949 const ObjectPath kObjectPathValue("/ObjectPath");
[email protected]a5aae122012-03-16 23:08:4250 writer.AppendObjectPath(kObjectPathValue);
51
[email protected]2a57ca642013-06-13 06:37:1952 MessageReader reader(response.get());
dcheng2a193282016-04-08 22:55:0453 std::unique_ptr<base::Value> value;
54 std::unique_ptr<base::Value> expected_value;
[email protected]a5aae122012-03-16 23:08:4255 // Pop a byte.
dchengcef035462016-05-30 06:33:5856 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:1857 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:1558 expected_value.reset(new base::FundamentalValue(kByteValue));
[email protected]ba4ce1c2012-05-31 06:55:5359 EXPECT_TRUE(value->Equals(expected_value.get()));
[email protected]a5aae122012-03-16 23:08:4260 // Pop a bool.
dchengcef035462016-05-30 06:33:5861 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:1862 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:1563 expected_value.reset(new base::FundamentalValue(kBoolValue));
[email protected]ba4ce1c2012-05-31 06:55:5364 EXPECT_TRUE(value->Equals(expected_value.get()));
avi22437c692015-12-22 18:12:4565 // Pop an int16_t.
dchengcef035462016-05-30 06:33:5866 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:1867 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:1568 expected_value.reset(new base::FundamentalValue(kInt16Value));
[email protected]ba4ce1c2012-05-31 06:55:5369 EXPECT_TRUE(value->Equals(expected_value.get()));
avi22437c692015-12-22 18:12:4570 // Pop a uint16_t.
dchengcef035462016-05-30 06:33:5871 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:1872 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:1573 expected_value.reset(new base::FundamentalValue(kUint16Value));
[email protected]ba4ce1c2012-05-31 06:55:5374 EXPECT_TRUE(value->Equals(expected_value.get()));
avi22437c692015-12-22 18:12:4575 // Pop an int32_t.
dchengcef035462016-05-30 06:33:5876 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:1877 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:1578 expected_value.reset(new base::FundamentalValue(kInt32Value));
[email protected]ba4ce1c2012-05-31 06:55:5379 EXPECT_TRUE(value->Equals(expected_value.get()));
avi22437c692015-12-22 18:12:4580 // Pop a uint32_t.
dchengcef035462016-05-30 06:33:5881 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:1882 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:1583 expected_value.reset(
84 new base::FundamentalValue(static_cast<double>(kUint32Value)));
[email protected]ba4ce1c2012-05-31 06:55:5385 EXPECT_TRUE(value->Equals(expected_value.get()));
avi22437c692015-12-22 18:12:4586 // Pop an int64_t.
dchengcef035462016-05-30 06:33:5887 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:1888 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:1589 expected_value.reset(
90 new base::FundamentalValue(static_cast<double>(kInt64Value)));
[email protected]ba4ce1c2012-05-31 06:55:5391 EXPECT_TRUE(value->Equals(expected_value.get()));
avi22437c692015-12-22 18:12:4592 // Pop a uint64_t.
dchengcef035462016-05-30 06:33:5893 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:1894 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:1595 expected_value.reset(
96 new base::FundamentalValue(static_cast<double>(kUint64Value)));
[email protected]ba4ce1c2012-05-31 06:55:5397 EXPECT_TRUE(value->Equals(expected_value.get()));
[email protected]a5aae122012-03-16 23:08:4298 // Pop a double.
dchengcef035462016-05-30 06:33:5899 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18100 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:15101 expected_value.reset(new base::FundamentalValue(kDoubleValue));
[email protected]ba4ce1c2012-05-31 06:55:53102 EXPECT_TRUE(value->Equals(expected_value.get()));
[email protected]a5aae122012-03-16 23:08:42103 // Pop a string.
dchengcef035462016-05-30 06:33:58104 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18105 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:15106 expected_value.reset(new base::StringValue(kStringValue));
[email protected]ba4ce1c2012-05-31 06:55:53107 EXPECT_TRUE(value->Equals(expected_value.get()));
[email protected]a5aae122012-03-16 23:08:42108 // Pop an empty string.
dchengcef035462016-05-30 06:33:58109 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18110 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:15111 expected_value.reset(new base::StringValue(kEmptyStringValue));
[email protected]ba4ce1c2012-05-31 06:55:53112 EXPECT_TRUE(value->Equals(expected_value.get()));
[email protected]a5aae122012-03-16 23:08:42113 // Pop an object path.
dchengcef035462016-05-30 06:33:58114 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18115 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:15116 expected_value.reset(new base::StringValue(kObjectPathValue.value()));
[email protected]ba4ce1c2012-05-31 06:55:53117 EXPECT_TRUE(value->Equals(expected_value.get()));
[email protected]a5aae122012-03-16 23:08:42118}
119
120TEST(ValuesUtilTest, PopVariant) {
dcheng2a193282016-04-08 22:55:04121 std::unique_ptr<Response> response(Response::CreateEmpty());
[email protected]a5aae122012-03-16 23:08:42122 // Append variant values.
[email protected]2a57ca642013-06-13 06:37:19123 MessageWriter writer(response.get());
[email protected]a5aae122012-03-16 23:08:42124 const bool kBoolValue = true;
125 writer.AppendVariantOfBool(kBoolValue);
avi22437c692015-12-22 18:12:45126 const int32_t kInt32Value = -45;
[email protected]a5aae122012-03-16 23:08:42127 writer.AppendVariantOfInt32(kInt32Value);
128 const double kDoubleValue = 4.9;
129 writer.AppendVariantOfDouble(kDoubleValue);
130 const std::string kStringValue = "fifty";
131 writer.AppendVariantOfString(kStringValue);
132
[email protected]2a57ca642013-06-13 06:37:19133 MessageReader reader(response.get());
dcheng2a193282016-04-08 22:55:04134 std::unique_ptr<base::Value> value;
135 std::unique_ptr<base::Value> expected_value;
[email protected]a5aae122012-03-16 23:08:42136 // Pop a bool.
dchengcef035462016-05-30 06:33:58137 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18138 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:15139 expected_value.reset(new base::FundamentalValue(kBoolValue));
[email protected]ba4ce1c2012-05-31 06:55:53140 EXPECT_TRUE(value->Equals(expected_value.get()));
avi22437c692015-12-22 18:12:45141 // Pop an int32_t.
dchengcef035462016-05-30 06:33:58142 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18143 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:15144 expected_value.reset(new base::FundamentalValue(kInt32Value));
[email protected]ba4ce1c2012-05-31 06:55:53145 EXPECT_TRUE(value->Equals(expected_value.get()));
[email protected]a5aae122012-03-16 23:08:42146 // Pop a double.
dchengcef035462016-05-30 06:33:58147 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18148 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:15149 expected_value.reset(new base::FundamentalValue(kDoubleValue));
[email protected]ba4ce1c2012-05-31 06:55:53150 EXPECT_TRUE(value->Equals(expected_value.get()));
[email protected]a5aae122012-03-16 23:08:42151 // Pop a string.
dchengcef035462016-05-30 06:33:58152 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18153 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:15154 expected_value.reset(new base::StringValue(kStringValue));
[email protected]ba4ce1c2012-05-31 06:55:53155 EXPECT_TRUE(value->Equals(expected_value.get()));
[email protected]a5aae122012-03-16 23:08:42156}
157
158// Pop extremely large integers which cannot be precisely represented in
159// double.
160TEST(ValuesUtilTest, PopExtremelyLargeIntegers) {
dcheng2a193282016-04-08 22:55:04161 std::unique_ptr<Response> response(Response::CreateEmpty());
[email protected]a5aae122012-03-16 23:08:42162 // Append large integers.
[email protected]2a57ca642013-06-13 06:37:19163 MessageWriter writer(response.get());
avi22437c692015-12-22 18:12:45164 const int64_t kInt64Value = -123456789012345689LL;
[email protected]a5aae122012-03-16 23:08:42165 writer.AppendInt64(kInt64Value);
avi22437c692015-12-22 18:12:45166 const uint64_t kUint64Value = 9876543210987654321ULL;
[email protected]a5aae122012-03-16 23:08:42167 writer.AppendUint64(kUint64Value);
168
[email protected]2a57ca642013-06-13 06:37:19169 MessageReader reader(response.get());
dcheng2a193282016-04-08 22:55:04170 std::unique_ptr<base::Value> value;
171 std::unique_ptr<base::Value> expected_value;
[email protected]a5aae122012-03-16 23:08:42172 double double_value = 0;
avi22437c692015-12-22 18:12:45173 // Pop an int64_t.
dchengcef035462016-05-30 06:33:58174 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18175 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:15176 expected_value.reset(
177 new base::FundamentalValue(static_cast<double>(kInt64Value)));
[email protected]ba4ce1c2012-05-31 06:55:53178 EXPECT_TRUE(value->Equals(expected_value.get()));
[email protected]a5aae122012-03-16 23:08:42179 ASSERT_TRUE(value->GetAsDouble(&double_value));
avi22437c692015-12-22 18:12:45180 EXPECT_NE(kInt64Value, static_cast<int64_t>(double_value));
181 // Pop a uint64_t.
dchengcef035462016-05-30 06:33:58182 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18183 ASSERT_TRUE(value.get() != NULL);
[email protected]f40b7362013-02-12 20:06:15184 expected_value.reset(
185 new base::FundamentalValue(static_cast<double>(kUint64Value)));
[email protected]ba4ce1c2012-05-31 06:55:53186 EXPECT_TRUE(value->Equals(expected_value.get()));
[email protected]a5aae122012-03-16 23:08:42187 ASSERT_TRUE(value->GetAsDouble(&double_value));
avi22437c692015-12-22 18:12:45188 EXPECT_NE(kUint64Value, static_cast<uint64_t>(double_value));
[email protected]a5aae122012-03-16 23:08:42189}
190
191TEST(ValuesUtilTest, PopIntArray) {
dcheng2a193282016-04-08 22:55:04192 std::unique_ptr<Response> response(Response::CreateEmpty());
avi22437c692015-12-22 18:12:45193 // Append an int32_t array.
[email protected]2a57ca642013-06-13 06:37:19194 MessageWriter writer(response.get());
195 MessageWriter sub_writer(NULL);
avi22437c692015-12-22 18:12:45196 std::vector<int32_t> data;
[email protected]a5aae122012-03-16 23:08:42197 data.push_back(0);
198 data.push_back(1);
199 data.push_back(2);
200 writer.OpenArray("i", &sub_writer);
201 for (size_t i = 0; i != data.size(); ++i)
202 sub_writer.AppendInt32(data[i]);
203 writer.CloseContainer(&sub_writer);
204
205 // Create the expected value.
dcheng2a193282016-04-08 22:55:04206 std::unique_ptr<base::ListValue> list_value(new base::ListValue);
[email protected]a5aae122012-03-16 23:08:42207 for (size_t i = 0; i != data.size(); ++i)
dcheng4f046e852016-06-07 05:05:14208 list_value->AppendInteger(data[i]);
[email protected]a5aae122012-03-16 23:08:42209
avi22437c692015-12-22 18:12:45210 // Pop an int32_t array.
[email protected]2a57ca642013-06-13 06:37:19211 MessageReader reader(response.get());
dcheng2a193282016-04-08 22:55:04212 std::unique_ptr<base::Value> value(PopDataAsValue(&reader));
[email protected]6e04d172012-03-24 20:37:18213 ASSERT_TRUE(value.get() != NULL);
[email protected]ba4ce1c2012-05-31 06:55:53214 EXPECT_TRUE(value->Equals(list_value.get()));
[email protected]a5aae122012-03-16 23:08:42215}
216
217TEST(ValuesUtilTest, PopStringArray) {
dcheng2a193282016-04-08 22:55:04218 std::unique_ptr<Response> response(Response::CreateEmpty());
[email protected]a5aae122012-03-16 23:08:42219 // Append a string array.
[email protected]2a57ca642013-06-13 06:37:19220 MessageWriter writer(response.get());
221 MessageWriter sub_writer(NULL);
[email protected]a5aae122012-03-16 23:08:42222 std::vector<std::string> data;
223 data.push_back("Dreamlifter");
224 data.push_back("Beluga");
225 data.push_back("Mriya");
226 writer.AppendArrayOfStrings(data);
227
228 // Create the expected value.
dcheng2a193282016-04-08 22:55:04229 std::unique_ptr<base::ListValue> list_value(new base::ListValue);
[email protected]a5aae122012-03-16 23:08:42230 for (size_t i = 0; i != data.size(); ++i)
dcheng4f046e852016-06-07 05:05:14231 list_value->AppendString(data[i]);
[email protected]a5aae122012-03-16 23:08:42232
233 // Pop a string array.
[email protected]2a57ca642013-06-13 06:37:19234 MessageReader reader(response.get());
dcheng2a193282016-04-08 22:55:04235 std::unique_ptr<base::Value> value(PopDataAsValue(&reader));
[email protected]6e04d172012-03-24 20:37:18236 ASSERT_TRUE(value.get() != NULL);
[email protected]ba4ce1c2012-05-31 06:55:53237 EXPECT_TRUE(value->Equals(list_value.get()));
[email protected]a5aae122012-03-16 23:08:42238}
239
240TEST(ValuesUtilTest, PopStruct) {
dcheng2a193282016-04-08 22:55:04241 std::unique_ptr<Response> response(Response::CreateEmpty());
[email protected]a5aae122012-03-16 23:08:42242 // Append a struct.
[email protected]2a57ca642013-06-13 06:37:19243 MessageWriter writer(response.get());
244 MessageWriter sub_writer(NULL);
[email protected]a5aae122012-03-16 23:08:42245 writer.OpenStruct(&sub_writer);
246 const bool kBoolValue = true;
247 sub_writer.AppendBool(kBoolValue);
avi22437c692015-12-22 18:12:45248 const int32_t kInt32Value = -123;
[email protected]a5aae122012-03-16 23:08:42249 sub_writer.AppendInt32(kInt32Value);
250 const double kDoubleValue = 1.23;
251 sub_writer.AppendDouble(kDoubleValue);
252 const std::string kStringValue = "one two three";
253 sub_writer.AppendString(kStringValue);
254 writer.CloseContainer(&sub_writer);
255
256 // Create the expected value.
[email protected]f40b7362013-02-12 20:06:15257 base::ListValue list_value;
dcheng4f046e852016-06-07 05:05:14258 list_value.AppendBoolean(kBoolValue);
259 list_value.AppendInteger(kInt32Value);
260 list_value.AppendDouble(kDoubleValue);
261 list_value.AppendString(kStringValue);
[email protected]a5aae122012-03-16 23:08:42262
263 // Pop a struct.
[email protected]2a57ca642013-06-13 06:37:19264 MessageReader reader(response.get());
dcheng2a193282016-04-08 22:55:04265 std::unique_ptr<base::Value> value(PopDataAsValue(&reader));
[email protected]6e04d172012-03-24 20:37:18266 ASSERT_TRUE(value.get() != NULL);
[email protected]a5aae122012-03-16 23:08:42267 EXPECT_TRUE(value->Equals(&list_value));
268}
269
270TEST(ValuesUtilTest, PopStringToVariantDictionary) {
dcheng2a193282016-04-08 22:55:04271 std::unique_ptr<Response> response(Response::CreateEmpty());
[email protected]a5aae122012-03-16 23:08:42272 // Append a dictionary.
[email protected]2a57ca642013-06-13 06:37:19273 MessageWriter writer(response.get());
274 MessageWriter sub_writer(NULL);
275 MessageWriter entry_writer(NULL);
[email protected]a5aae122012-03-16 23:08:42276 writer.OpenArray("{sv}", &sub_writer);
277 sub_writer.OpenDictEntry(&entry_writer);
278 const std::string kKey1 = "one";
279 entry_writer.AppendString(kKey1);
280 const bool kBoolValue = true;
281 entry_writer.AppendVariantOfBool(kBoolValue);
282 sub_writer.CloseContainer(&entry_writer);
283 sub_writer.OpenDictEntry(&entry_writer);
284 const std::string kKey2 = "two";
285 entry_writer.AppendString(kKey2);
avi22437c692015-12-22 18:12:45286 const int32_t kInt32Value = -45;
[email protected]a5aae122012-03-16 23:08:42287 entry_writer.AppendVariantOfInt32(kInt32Value);
288 sub_writer.CloseContainer(&entry_writer);
289 sub_writer.OpenDictEntry(&entry_writer);
290 const std::string kKey3 = "three";
291 entry_writer.AppendString(kKey3);
292 const double kDoubleValue = 4.9;
293 entry_writer.AppendVariantOfDouble(kDoubleValue);
294 sub_writer.CloseContainer(&entry_writer);
295 sub_writer.OpenDictEntry(&entry_writer);
296 const std::string kKey4 = "four";
297 entry_writer.AppendString(kKey4);
298 const std::string kStringValue = "fifty";
299 entry_writer.AppendVariantOfString(kStringValue);
300 sub_writer.CloseContainer(&entry_writer);
301 writer.CloseContainer(&sub_writer);
302
303 // Create the expected value.
[email protected]f40b7362013-02-12 20:06:15304 base::DictionaryValue dictionary_value;
[email protected]a5aae122012-03-16 23:08:42305 dictionary_value.SetBoolean(kKey1, kBoolValue);
306 dictionary_value.SetInteger(kKey2, kInt32Value);
307 dictionary_value.SetDouble(kKey3, kDoubleValue);
308 dictionary_value.SetString(kKey4, kStringValue);
309
310 // Pop a dictinoary.
[email protected]2a57ca642013-06-13 06:37:19311 MessageReader reader(response.get());
dcheng2a193282016-04-08 22:55:04312 std::unique_ptr<base::Value> value(PopDataAsValue(&reader));
[email protected]6e04d172012-03-24 20:37:18313 ASSERT_TRUE(value.get() != NULL);
[email protected]a5aae122012-03-16 23:08:42314 EXPECT_TRUE(value->Equals(&dictionary_value));
315}
316
[email protected]0f0117b242012-03-20 18:18:57317TEST(ValuesUtilTest, PopDictionaryWithDottedStringKey) {
dcheng2a193282016-04-08 22:55:04318 std::unique_ptr<Response> response(Response::CreateEmpty());
[email protected]0f0117b242012-03-20 18:18:57319 // Append a dictionary.
[email protected]2a57ca642013-06-13 06:37:19320 MessageWriter writer(response.get());
321 MessageWriter sub_writer(NULL);
322 MessageWriter entry_writer(NULL);
[email protected]0f0117b242012-03-20 18:18:57323 writer.OpenArray("{sv}", &sub_writer);
324 sub_writer.OpenDictEntry(&entry_writer);
325 const std::string kKey1 = "www.example.com"; // String including dots.
326 entry_writer.AppendString(kKey1);
327 const bool kBoolValue = true;
328 entry_writer.AppendVariantOfBool(kBoolValue);
329 sub_writer.CloseContainer(&entry_writer);
330 sub_writer.OpenDictEntry(&entry_writer);
331 const std::string kKey2 = ".example"; // String starting with a dot.
332 entry_writer.AppendString(kKey2);
avi22437c692015-12-22 18:12:45333 const int32_t kInt32Value = -45;
[email protected]0f0117b242012-03-20 18:18:57334 entry_writer.AppendVariantOfInt32(kInt32Value);
335 sub_writer.CloseContainer(&entry_writer);
336 sub_writer.OpenDictEntry(&entry_writer);
337 const std::string kKey3 = "example."; // String ending with a dot.
338 entry_writer.AppendString(kKey3);
339 const double kDoubleValue = 4.9;
340 entry_writer.AppendVariantOfDouble(kDoubleValue);
341 sub_writer.CloseContainer(&entry_writer);
342 writer.CloseContainer(&sub_writer);
343
344 // Create the expected value.
[email protected]f40b7362013-02-12 20:06:15345 base::DictionaryValue dictionary_value;
[email protected]0f0117b242012-03-20 18:18:57346 dictionary_value.SetWithoutPathExpansion(
[email protected]f40b7362013-02-12 20:06:15347 kKey1, new base::FundamentalValue(kBoolValue));
[email protected]0f0117b242012-03-20 18:18:57348 dictionary_value.SetWithoutPathExpansion(
[email protected]f40b7362013-02-12 20:06:15349 kKey2, new base::FundamentalValue(kInt32Value));
[email protected]0f0117b242012-03-20 18:18:57350 dictionary_value.SetWithoutPathExpansion(
[email protected]f40b7362013-02-12 20:06:15351 kKey3, new base::FundamentalValue(kDoubleValue));
[email protected]0f0117b242012-03-20 18:18:57352
353 // Pop a dictinoary.
[email protected]2a57ca642013-06-13 06:37:19354 MessageReader reader(response.get());
dcheng2a193282016-04-08 22:55:04355 std::unique_ptr<base::Value> value(PopDataAsValue(&reader));
[email protected]6e04d172012-03-24 20:37:18356 ASSERT_TRUE(value.get() != NULL);
[email protected]0f0117b242012-03-20 18:18:57357 EXPECT_TRUE(value->Equals(&dictionary_value));
358}
359
[email protected]a5aae122012-03-16 23:08:42360TEST(ValuesUtilTest, PopDoubleToIntDictionary) {
361 // Create test data.
avi22437c692015-12-22 18:12:45362 const int32_t kValues[] = {0, 1, 1, 2, 3, 5, 8, 13, 21};
363 const std::vector<int32_t> values(kValues, kValues + arraysize(kValues));
[email protected]a5aae122012-03-16 23:08:42364 std::vector<double> keys(values.size());
365 for (size_t i = 0; i != values.size(); ++i)
Hans Wennborg72193492015-04-24 21:38:04366 keys[i] = std::sqrt(values[i]);
[email protected]a5aae122012-03-16 23:08:42367
368 // Append a dictionary.
dcheng2a193282016-04-08 22:55:04369 std::unique_ptr<Response> response(Response::CreateEmpty());
[email protected]2a57ca642013-06-13 06:37:19370 MessageWriter writer(response.get());
371 MessageWriter sub_writer(NULL);
[email protected]a5aae122012-03-16 23:08:42372 writer.OpenArray("{di}", &sub_writer);
373 for (size_t i = 0; i != values.size(); ++i) {
[email protected]2a57ca642013-06-13 06:37:19374 MessageWriter entry_writer(NULL);
[email protected]a5aae122012-03-16 23:08:42375 sub_writer.OpenDictEntry(&entry_writer);
376 entry_writer.AppendDouble(keys[i]);
377 entry_writer.AppendInt32(values[i]);
378 sub_writer.CloseContainer(&entry_writer);
379 }
380 writer.CloseContainer(&sub_writer);
381
382 // Create the expected value.
[email protected]f40b7362013-02-12 20:06:15383 base::DictionaryValue dictionary_value;
[email protected]a5aae122012-03-16 23:08:42384 for (size_t i = 0; i != values.size(); ++i) {
[email protected]a5aae122012-03-16 23:08:42385 std::string key_string;
estade8d046462015-05-16 01:02:34386 base::JSONWriter::Write(base::FundamentalValue(keys[i]), &key_string);
387 dictionary_value.SetIntegerWithoutPathExpansion(key_string, values[i]);
[email protected]a5aae122012-03-16 23:08:42388 }
389
390 // Pop a dictionary.
[email protected]2a57ca642013-06-13 06:37:19391 MessageReader reader(response.get());
dcheng2a193282016-04-08 22:55:04392 std::unique_ptr<base::Value> value(PopDataAsValue(&reader));
[email protected]6e04d172012-03-24 20:37:18393 ASSERT_TRUE(value.get() != NULL);
[email protected]a5aae122012-03-16 23:08:42394 EXPECT_TRUE(value->Equals(&dictionary_value));
395}
[email protected]6e04d172012-03-24 20:37:18396
397TEST(ValuesUtilTest, AppendBasicTypes) {
398 const base::FundamentalValue kBoolValue(false);
399 const base::FundamentalValue kIntegerValue(42);
400 const base::FundamentalValue kDoubleValue(4.2);
401 const base::StringValue kStringValue("string");
402
dcheng2a193282016-04-08 22:55:04403 std::unique_ptr<Response> response(Response::CreateEmpty());
[email protected]2a57ca642013-06-13 06:37:19404 MessageWriter writer(response.get());
[email protected]6e04d172012-03-24 20:37:18405 AppendBasicTypeValueData(&writer, kBoolValue);
406 AppendBasicTypeValueData(&writer, kIntegerValue);
407 AppendBasicTypeValueData(&writer, kDoubleValue);
408 AppendBasicTypeValueData(&writer, kStringValue);
409
[email protected]2a57ca642013-06-13 06:37:19410 MessageReader reader(response.get());
dcheng2a193282016-04-08 22:55:04411 std::unique_ptr<base::Value> value;
dchengcef035462016-05-30 06:33:58412 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18413 ASSERT_TRUE(value.get() != NULL);
414 EXPECT_TRUE(value->Equals(&kBoolValue));
dchengcef035462016-05-30 06:33:58415 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18416 ASSERT_TRUE(value.get() != NULL);
417 EXPECT_TRUE(value->Equals(&kIntegerValue));
dchengcef035462016-05-30 06:33:58418 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18419 ASSERT_TRUE(value.get() != NULL);
420 EXPECT_TRUE(value->Equals(&kDoubleValue));
dchengcef035462016-05-30 06:33:58421 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18422 ASSERT_TRUE(value.get() != NULL);
423 EXPECT_TRUE(value->Equals(&kStringValue));
424}
425
426TEST(ValuesUtilTest, AppendBasicTypesAsVariant) {
427 const base::FundamentalValue kBoolValue(false);
428 const base::FundamentalValue kIntegerValue(42);
429 const base::FundamentalValue kDoubleValue(4.2);
430 const base::StringValue kStringValue("string");
431
dcheng2a193282016-04-08 22:55:04432 std::unique_ptr<Response> response(Response::CreateEmpty());
[email protected]2a57ca642013-06-13 06:37:19433 MessageWriter writer(response.get());
[email protected]6e04d172012-03-24 20:37:18434 AppendBasicTypeValueDataAsVariant(&writer, kBoolValue);
435 AppendBasicTypeValueDataAsVariant(&writer, kIntegerValue);
436 AppendBasicTypeValueDataAsVariant(&writer, kDoubleValue);
437 AppendBasicTypeValueDataAsVariant(&writer, kStringValue);
438
[email protected]2a57ca642013-06-13 06:37:19439 MessageReader reader(response.get());
dcheng2a193282016-04-08 22:55:04440 std::unique_ptr<base::Value> value;
dchengcef035462016-05-30 06:33:58441 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18442 ASSERT_TRUE(value.get() != NULL);
443 EXPECT_TRUE(value->Equals(&kBoolValue));
dchengcef035462016-05-30 06:33:58444 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18445 ASSERT_TRUE(value.get() != NULL);
446 EXPECT_TRUE(value->Equals(&kIntegerValue));
dchengcef035462016-05-30 06:33:58447 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18448 ASSERT_TRUE(value.get() != NULL);
449 EXPECT_TRUE(value->Equals(&kDoubleValue));
dchengcef035462016-05-30 06:33:58450 value = PopDataAsValue(&reader);
[email protected]6e04d172012-03-24 20:37:18451 ASSERT_TRUE(value.get() != NULL);
452 EXPECT_TRUE(value->Equals(&kStringValue));
453}
[email protected]2a57ca642013-06-13 06:37:19454
[email protected]a68242f2014-05-22 11:52:55455TEST(ValuesUtilTest, AppendValueDataBasicTypes) {
456 const base::FundamentalValue kBoolValue(false);
457 const base::FundamentalValue kIntegerValue(42);
458 const base::FundamentalValue kDoubleValue(4.2);
459 const base::StringValue kStringValue("string");
460
dcheng2a193282016-04-08 22:55:04461 std::unique_ptr<Response> response(Response::CreateEmpty());
[email protected]a68242f2014-05-22 11:52:55462 MessageWriter writer(response.get());
463 AppendValueData(&writer, kBoolValue);
464 AppendValueData(&writer, kIntegerValue);
465 AppendValueData(&writer, kDoubleValue);
466 AppendValueData(&writer, kStringValue);
467
468 MessageReader reader(response.get());
dcheng2a193282016-04-08 22:55:04469 std::unique_ptr<base::Value> value;
dchengcef035462016-05-30 06:33:58470 value = PopDataAsValue(&reader);
[email protected]a68242f2014-05-22 11:52:55471 ASSERT_TRUE(value.get() != NULL);
472 EXPECT_TRUE(value->Equals(&kBoolValue));
dchengcef035462016-05-30 06:33:58473 value = PopDataAsValue(&reader);
[email protected]a68242f2014-05-22 11:52:55474 ASSERT_TRUE(value.get() != NULL);
475 EXPECT_TRUE(value->Equals(&kIntegerValue));
dchengcef035462016-05-30 06:33:58476 value = PopDataAsValue(&reader);
[email protected]a68242f2014-05-22 11:52:55477 ASSERT_TRUE(value.get() != NULL);
478 EXPECT_TRUE(value->Equals(&kDoubleValue));
dchengcef035462016-05-30 06:33:58479 value = PopDataAsValue(&reader);
[email protected]a68242f2014-05-22 11:52:55480 ASSERT_TRUE(value.get() != NULL);
481 EXPECT_TRUE(value->Equals(&kStringValue));
482}
483
484TEST(ValuesUtilTest, AppendValueDataAsVariantBasicTypes) {
485 const base::FundamentalValue kBoolValue(false);
486 const base::FundamentalValue kIntegerValue(42);
487 const base::FundamentalValue kDoubleValue(4.2);
488 const base::StringValue kStringValue("string");
489
dcheng2a193282016-04-08 22:55:04490 std::unique_ptr<Response> response(Response::CreateEmpty());
[email protected]a68242f2014-05-22 11:52:55491 MessageWriter writer(response.get());
492 AppendValueDataAsVariant(&writer, kBoolValue);
493 AppendValueDataAsVariant(&writer, kIntegerValue);
494 AppendValueDataAsVariant(&writer, kDoubleValue);
495 AppendValueDataAsVariant(&writer, kStringValue);
496
497 MessageReader reader(response.get());
dcheng2a193282016-04-08 22:55:04498 std::unique_ptr<base::Value> value;
dchengcef035462016-05-30 06:33:58499 value = PopDataAsValue(&reader);
[email protected]a68242f2014-05-22 11:52:55500 ASSERT_TRUE(value.get() != NULL);
501 EXPECT_TRUE(value->Equals(&kBoolValue));
dchengcef035462016-05-30 06:33:58502 value = PopDataAsValue(&reader);
[email protected]a68242f2014-05-22 11:52:55503 ASSERT_TRUE(value.get() != NULL);
504 EXPECT_TRUE(value->Equals(&kIntegerValue));
dchengcef035462016-05-30 06:33:58505 value = PopDataAsValue(&reader);
[email protected]a68242f2014-05-22 11:52:55506 ASSERT_TRUE(value.get() != NULL);
507 EXPECT_TRUE(value->Equals(&kDoubleValue));
dchengcef035462016-05-30 06:33:58508 value = PopDataAsValue(&reader);
[email protected]a68242f2014-05-22 11:52:55509 ASSERT_TRUE(value.get() != NULL);
510 EXPECT_TRUE(value->Equals(&kStringValue));
511}
512
513TEST(ValuesUtilTest, AppendDictionary) {
514 // Set up the input dictionary.
515 const std::string kKey1 = "one";
516 const std::string kKey2 = "two";
517 const std::string kKey3 = "three";
518 const std::string kKey4 = "four";
519 const std::string kKey5 = "five";
520 const std::string kKey6 = "six";
521
522 const bool kBoolValue = true;
avi22437c692015-12-22 18:12:45523 const int32_t kInt32Value = -45;
[email protected]a68242f2014-05-22 11:52:55524 const double kDoubleValue = 4.9;
525 const std::string kStringValue = "fifty";
526
527 base::ListValue* list_value = new base::ListValue();
528 list_value->AppendBoolean(kBoolValue);
529 list_value->AppendInteger(kInt32Value);
530
531 base::DictionaryValue* dictionary_value = new base::DictionaryValue();
532 dictionary_value->SetBoolean(kKey1, kBoolValue);
533 dictionary_value->SetInteger(kKey2, kDoubleValue);
534
535 base::DictionaryValue test_dictionary;
536 test_dictionary.SetBoolean(kKey1, kBoolValue);
537 test_dictionary.SetInteger(kKey2, kInt32Value);
538 test_dictionary.SetDouble(kKey3, kDoubleValue);
539 test_dictionary.SetString(kKey4, kStringValue);
540 test_dictionary.Set(kKey5, list_value); // takes ownership
541 test_dictionary.Set(kKey6, dictionary_value); // takes ownership
542
dcheng2a193282016-04-08 22:55:04543 std::unique_ptr<Response> response(Response::CreateEmpty());
[email protected]a68242f2014-05-22 11:52:55544 MessageWriter writer(response.get());
545 AppendValueData(&writer, test_dictionary);
546 base::FundamentalValue int_value(kInt32Value);
547 AppendValueData(&writer, int_value);
548
549 // Read the data.
550 MessageReader reader(response.get());
dcheng2a193282016-04-08 22:55:04551 std::unique_ptr<base::Value> value;
dchengcef035462016-05-30 06:33:58552 value = PopDataAsValue(&reader);
[email protected]a68242f2014-05-22 11:52:55553 ASSERT_TRUE(value.get() != NULL);
554 EXPECT_TRUE(value->Equals(&test_dictionary));
dchengcef035462016-05-30 06:33:58555 value = PopDataAsValue(&reader);
[email protected]a68242f2014-05-22 11:52:55556 ASSERT_TRUE(value.get() != NULL);
557 EXPECT_TRUE(value->Equals(&int_value));
558}
559
560TEST(ValuesUtilTest, AppendDictionaryAsVariant) {
561 // Set up the input dictionary.
562 const std::string kKey1 = "one";
563 const std::string kKey2 = "two";
564 const std::string kKey3 = "three";
565 const std::string kKey4 = "four";
566 const std::string kKey5 = "five";
567 const std::string kKey6 = "six";
568
569 const bool kBoolValue = true;
avi22437c692015-12-22 18:12:45570 const int32_t kInt32Value = -45;
[email protected]a68242f2014-05-22 11:52:55571 const double kDoubleValue = 4.9;
572 const std::string kStringValue = "fifty";
573
574 base::ListValue* list_value = new base::ListValue();
575 list_value->AppendBoolean(kBoolValue);
576 list_value->AppendInteger(kInt32Value);
577
578 base::DictionaryValue* dictionary_value = new base::DictionaryValue();
579 dictionary_value->SetBoolean(kKey1, kBoolValue);
580 dictionary_value->SetInteger(kKey2, kDoubleValue);
581
582 base::DictionaryValue test_dictionary;
583 test_dictionary.SetBoolean(kKey1, kBoolValue);
584 test_dictionary.SetInteger(kKey2, kInt32Value);
585 test_dictionary.SetDouble(kKey3, kDoubleValue);
586 test_dictionary.SetString(kKey4, kStringValue);
587 test_dictionary.Set(kKey5, list_value); // takes ownership
588 test_dictionary.Set(kKey6, dictionary_value); // takes ownership
589
dcheng2a193282016-04-08 22:55:04590 std::unique_ptr<Response> response(Response::CreateEmpty());
[email protected]a68242f2014-05-22 11:52:55591 MessageWriter writer(response.get());
592 AppendValueDataAsVariant(&writer, test_dictionary);
593 base::FundamentalValue int_value(kInt32Value);
594 AppendValueData(&writer, int_value);
595
596 // Read the data.
597 MessageReader reader(response.get());
dcheng2a193282016-04-08 22:55:04598 std::unique_ptr<base::Value> value;
dchengcef035462016-05-30 06:33:58599 value = PopDataAsValue(&reader);
[email protected]a68242f2014-05-22 11:52:55600 ASSERT_TRUE(value.get() != NULL);
601 EXPECT_TRUE(value->Equals(&test_dictionary));
dchengcef035462016-05-30 06:33:58602 value = PopDataAsValue(&reader);
[email protected]a68242f2014-05-22 11:52:55603 ASSERT_TRUE(value.get() != NULL);
604 EXPECT_TRUE(value->Equals(&int_value));
605}
606
607TEST(ValuesUtilTest, AppendList) {
608 // Set up the input list.
609 const std::string kKey1 = "one";
610 const std::string kKey2 = "two";
611
612 const bool kBoolValue = true;
avi22437c692015-12-22 18:12:45613 const int32_t kInt32Value = -45;
[email protected]a68242f2014-05-22 11:52:55614 const double kDoubleValue = 4.9;
615 const std::string kStringValue = "fifty";
616
dcheng98e96a72016-06-11 03:41:48617 std::unique_ptr<base::ListValue> list_value(new base::ListValue());
[email protected]a68242f2014-05-22 11:52:55618 list_value->AppendBoolean(kBoolValue);
619 list_value->AppendInteger(kInt32Value);
620
dcheng98e96a72016-06-11 03:41:48621 std::unique_ptr<base::DictionaryValue> dictionary_value(
622 new base::DictionaryValue());
[email protected]a68242f2014-05-22 11:52:55623 dictionary_value->SetBoolean(kKey1, kBoolValue);
624 dictionary_value->SetInteger(kKey2, kDoubleValue);
625
626 base::ListValue test_list;
627 test_list.AppendBoolean(kBoolValue);
628 test_list.AppendInteger(kInt32Value);
629 test_list.AppendDouble(kDoubleValue);
630 test_list.AppendString(kStringValue);
dcheng98e96a72016-06-11 03:41:48631 test_list.Append(std::move(list_value));
632 test_list.Append(std::move(dictionary_value));
[email protected]a68242f2014-05-22 11:52:55633
dcheng2a193282016-04-08 22:55:04634 std::unique_ptr<Response> response(Response::CreateEmpty());
[email protected]a68242f2014-05-22 11:52:55635 MessageWriter writer(response.get());
636 AppendValueData(&writer, test_list);
637 base::FundamentalValue int_value(kInt32Value);
638 AppendValueData(&writer, int_value);
639
640 // Read the data.
641 MessageReader reader(response.get());
dcheng2a193282016-04-08 22:55:04642 std::unique_ptr<base::Value> value;
dchengcef035462016-05-30 06:33:58643 value = PopDataAsValue(&reader);
[email protected]a68242f2014-05-22 11:52:55644 ASSERT_TRUE(value.get() != NULL);
645 EXPECT_TRUE(value->Equals(&test_list));
dchengcef035462016-05-30 06:33:58646 value = PopDataAsValue(&reader);
[email protected]a68242f2014-05-22 11:52:55647 ASSERT_TRUE(value.get() != NULL);
648 EXPECT_TRUE(value->Equals(&int_value));
649}
650
651TEST(ValuesUtilTest, AppendListAsVariant) {
652 // Set up the input list.
653 const std::string kKey1 = "one";
654 const std::string kKey2 = "two";
655
656 const bool kBoolValue = true;
avi22437c692015-12-22 18:12:45657 const int32_t kInt32Value = -45;
[email protected]a68242f2014-05-22 11:52:55658 const double kDoubleValue = 4.9;
659 const std::string kStringValue = "fifty";
660
dcheng98e96a72016-06-11 03:41:48661 std::unique_ptr<base::ListValue> list_value(new base::ListValue());
[email protected]a68242f2014-05-22 11:52:55662 list_value->AppendBoolean(kBoolValue);
663 list_value->AppendInteger(kInt32Value);
664
dcheng98e96a72016-06-11 03:41:48665 std::unique_ptr<base::DictionaryValue> dictionary_value(
666 new base::DictionaryValue());
[email protected]a68242f2014-05-22 11:52:55667 dictionary_value->SetBoolean(kKey1, kBoolValue);
668 dictionary_value->SetInteger(kKey2, kDoubleValue);
669
670 base::ListValue test_list;
671 test_list.AppendBoolean(kBoolValue);
672 test_list.AppendInteger(kInt32Value);
673 test_list.AppendDouble(kDoubleValue);
674 test_list.AppendString(kStringValue);
dcheng98e96a72016-06-11 03:41:48675 test_list.Append(std::move(list_value));
676 test_list.Append(std::move(dictionary_value));
[email protected]a68242f2014-05-22 11:52:55677
dcheng2a193282016-04-08 22:55:04678 std::unique_ptr<Response> response(Response::CreateEmpty());
[email protected]a68242f2014-05-22 11:52:55679 MessageWriter writer(response.get());
680 AppendValueDataAsVariant(&writer, test_list);
681 base::FundamentalValue int_value(kInt32Value);
682 AppendValueData(&writer, int_value);
683
684 // Read the data.
685 MessageReader reader(response.get());
dcheng2a193282016-04-08 22:55:04686 std::unique_ptr<base::Value> value;
dchengcef035462016-05-30 06:33:58687 value = PopDataAsValue(&reader);
[email protected]a68242f2014-05-22 11:52:55688 ASSERT_TRUE(value.get() != NULL);
689 EXPECT_TRUE(value->Equals(&test_list));
dchengcef035462016-05-30 06:33:58690 value = PopDataAsValue(&reader);
[email protected]a68242f2014-05-22 11:52:55691 ASSERT_TRUE(value.get() != NULL);
692 EXPECT_TRUE(value->Equals(&int_value));
693}
694
[email protected]2a57ca642013-06-13 06:37:19695} // namespace dbus