Currently duration is simply returned or changed without checking whether the stream...
[chromium-blink-merge.git] / dbus / values_util_unittest.cc
blob2de12d793ad45e7de75bbee835af1d3405c57099
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "dbus/values_util.h"
7 #include <vector>
9 #include "base/float_util.h"
10 #include "base/json/json_writer.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/values.h"
13 #include "dbus/message.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace dbus {
18 TEST(ValuesUtilTest, PopBasicTypes) {
19 scoped_ptr<Response> response(Response::CreateEmpty());
20 // Append basic type values.
21 MessageWriter writer(response.get());
22 const uint8 kByteValue = 42;
23 writer.AppendByte(kByteValue);
24 const bool kBoolValue = true;
25 writer.AppendBool(kBoolValue);
26 const int16 kInt16Value = -43;
27 writer.AppendInt16(kInt16Value);
28 const uint16 kUint16Value = 44;
29 writer.AppendUint16(kUint16Value);
30 const int32 kInt32Value = -45;
31 writer.AppendInt32(kInt32Value);
32 const uint32 kUint32Value = 46;
33 writer.AppendUint32(kUint32Value);
34 const int64 kInt64Value = -47;
35 writer.AppendInt64(kInt64Value);
36 const uint64 kUint64Value = 48;
37 writer.AppendUint64(kUint64Value);
38 const double kDoubleValue = 4.9;
39 writer.AppendDouble(kDoubleValue);
40 const std::string kStringValue = "fifty";
41 writer.AppendString(kStringValue);
42 const std::string kEmptyStringValue;
43 writer.AppendString(kEmptyStringValue);
44 const ObjectPath kObjectPathValue("/ObjectPath");
45 writer.AppendObjectPath(kObjectPathValue);
47 MessageReader reader(response.get());
48 scoped_ptr<base::Value> value;
49 scoped_ptr<base::Value> expected_value;
50 // Pop a byte.
51 value.reset(PopDataAsValue(&reader));
52 ASSERT_TRUE(value.get() != NULL);
53 expected_value.reset(new base::FundamentalValue(kByteValue));
54 EXPECT_TRUE(value->Equals(expected_value.get()));
55 // Pop a bool.
56 value.reset(PopDataAsValue(&reader));
57 ASSERT_TRUE(value.get() != NULL);
58 expected_value.reset(new base::FundamentalValue(kBoolValue));
59 EXPECT_TRUE(value->Equals(expected_value.get()));
60 // Pop an int16.
61 value.reset(PopDataAsValue(&reader));
62 ASSERT_TRUE(value.get() != NULL);
63 expected_value.reset(new base::FundamentalValue(kInt16Value));
64 EXPECT_TRUE(value->Equals(expected_value.get()));
65 // Pop a uint16.
66 value.reset(PopDataAsValue(&reader));
67 ASSERT_TRUE(value.get() != NULL);
68 expected_value.reset(new base::FundamentalValue(kUint16Value));
69 EXPECT_TRUE(value->Equals(expected_value.get()));
70 // Pop an int32.
71 value.reset(PopDataAsValue(&reader));
72 ASSERT_TRUE(value.get() != NULL);
73 expected_value.reset(new base::FundamentalValue(kInt32Value));
74 EXPECT_TRUE(value->Equals(expected_value.get()));
75 // Pop a uint32.
76 value.reset(PopDataAsValue(&reader));
77 ASSERT_TRUE(value.get() != NULL);
78 expected_value.reset(
79 new base::FundamentalValue(static_cast<double>(kUint32Value)));
80 EXPECT_TRUE(value->Equals(expected_value.get()));
81 // Pop an int64.
82 value.reset(PopDataAsValue(&reader));
83 ASSERT_TRUE(value.get() != NULL);
84 expected_value.reset(
85 new base::FundamentalValue(static_cast<double>(kInt64Value)));
86 EXPECT_TRUE(value->Equals(expected_value.get()));
87 // Pop a uint64.
88 value.reset(PopDataAsValue(&reader));
89 ASSERT_TRUE(value.get() != NULL);
90 expected_value.reset(
91 new base::FundamentalValue(static_cast<double>(kUint64Value)));
92 EXPECT_TRUE(value->Equals(expected_value.get()));
93 // Pop a double.
94 value.reset(PopDataAsValue(&reader));
95 ASSERT_TRUE(value.get() != NULL);
96 expected_value.reset(new base::FundamentalValue(kDoubleValue));
97 EXPECT_TRUE(value->Equals(expected_value.get()));
98 // Pop a string.
99 value.reset(PopDataAsValue(&reader));
100 ASSERT_TRUE(value.get() != NULL);
101 expected_value.reset(new base::StringValue(kStringValue));
102 EXPECT_TRUE(value->Equals(expected_value.get()));
103 // Pop an empty string.
104 value.reset(PopDataAsValue(&reader));
105 ASSERT_TRUE(value.get() != NULL);
106 expected_value.reset(new base::StringValue(kEmptyStringValue));
107 EXPECT_TRUE(value->Equals(expected_value.get()));
108 // Pop an object path.
109 value.reset(PopDataAsValue(&reader));
110 ASSERT_TRUE(value.get() != NULL);
111 expected_value.reset(new base::StringValue(kObjectPathValue.value()));
112 EXPECT_TRUE(value->Equals(expected_value.get()));
115 TEST(ValuesUtilTest, PopVariant) {
116 scoped_ptr<Response> response(Response::CreateEmpty());
117 // Append variant values.
118 MessageWriter writer(response.get());
119 const bool kBoolValue = true;
120 writer.AppendVariantOfBool(kBoolValue);
121 const int32 kInt32Value = -45;
122 writer.AppendVariantOfInt32(kInt32Value);
123 const double kDoubleValue = 4.9;
124 writer.AppendVariantOfDouble(kDoubleValue);
125 const std::string kStringValue = "fifty";
126 writer.AppendVariantOfString(kStringValue);
128 MessageReader reader(response.get());
129 scoped_ptr<base::Value> value;
130 scoped_ptr<base::Value> expected_value;
131 // Pop a bool.
132 value.reset(PopDataAsValue(&reader));
133 ASSERT_TRUE(value.get() != NULL);
134 expected_value.reset(new base::FundamentalValue(kBoolValue));
135 EXPECT_TRUE(value->Equals(expected_value.get()));
136 // Pop an int32.
137 value.reset(PopDataAsValue(&reader));
138 ASSERT_TRUE(value.get() != NULL);
139 expected_value.reset(new base::FundamentalValue(kInt32Value));
140 EXPECT_TRUE(value->Equals(expected_value.get()));
141 // Pop a double.
142 value.reset(PopDataAsValue(&reader));
143 ASSERT_TRUE(value.get() != NULL);
144 expected_value.reset(new base::FundamentalValue(kDoubleValue));
145 EXPECT_TRUE(value->Equals(expected_value.get()));
146 // Pop a string.
147 value.reset(PopDataAsValue(&reader));
148 ASSERT_TRUE(value.get() != NULL);
149 expected_value.reset(new base::StringValue(kStringValue));
150 EXPECT_TRUE(value->Equals(expected_value.get()));
153 // Pop extremely large integers which cannot be precisely represented in
154 // double.
155 TEST(ValuesUtilTest, PopExtremelyLargeIntegers) {
156 scoped_ptr<Response> response(Response::CreateEmpty());
157 // Append large integers.
158 MessageWriter writer(response.get());
159 const int64 kInt64Value = -123456789012345689LL;
160 writer.AppendInt64(kInt64Value);
161 const uint64 kUint64Value = 9876543210987654321ULL;
162 writer.AppendUint64(kUint64Value);
164 MessageReader reader(response.get());
165 scoped_ptr<base::Value> value;
166 scoped_ptr<base::Value> expected_value;
167 double double_value = 0;
168 // Pop an int64.
169 value.reset(PopDataAsValue(&reader));
170 ASSERT_TRUE(value.get() != NULL);
171 expected_value.reset(
172 new base::FundamentalValue(static_cast<double>(kInt64Value)));
173 EXPECT_TRUE(value->Equals(expected_value.get()));
174 ASSERT_TRUE(value->GetAsDouble(&double_value));
175 EXPECT_NE(kInt64Value, static_cast<int64>(double_value));
176 // Pop a uint64.
177 value.reset(PopDataAsValue(&reader));
178 ASSERT_TRUE(value.get() != NULL);
179 expected_value.reset(
180 new base::FundamentalValue(static_cast<double>(kUint64Value)));
181 EXPECT_TRUE(value->Equals(expected_value.get()));
182 ASSERT_TRUE(value->GetAsDouble(&double_value));
183 EXPECT_NE(kUint64Value, static_cast<uint64>(double_value));
186 TEST(ValuesUtilTest, PopIntArray) {
187 scoped_ptr<Response> response(Response::CreateEmpty());
188 // Append an int32 array.
189 MessageWriter writer(response.get());
190 MessageWriter sub_writer(NULL);
191 std::vector<int32> data;
192 data.push_back(0);
193 data.push_back(1);
194 data.push_back(2);
195 writer.OpenArray("i", &sub_writer);
196 for (size_t i = 0; i != data.size(); ++i)
197 sub_writer.AppendInt32(data[i]);
198 writer.CloseContainer(&sub_writer);
200 // Create the expected value.
201 scoped_ptr<base::ListValue> list_value(new base::ListValue);
202 for (size_t i = 0; i != data.size(); ++i)
203 list_value->Append(new base::FundamentalValue(data[i]));
205 // Pop an int32 array.
206 MessageReader reader(response.get());
207 scoped_ptr<base::Value> value(PopDataAsValue(&reader));
208 ASSERT_TRUE(value.get() != NULL);
209 EXPECT_TRUE(value->Equals(list_value.get()));
212 TEST(ValuesUtilTest, PopStringArray) {
213 scoped_ptr<Response> response(Response::CreateEmpty());
214 // Append a string array.
215 MessageWriter writer(response.get());
216 MessageWriter sub_writer(NULL);
217 std::vector<std::string> data;
218 data.push_back("Dreamlifter");
219 data.push_back("Beluga");
220 data.push_back("Mriya");
221 writer.AppendArrayOfStrings(data);
223 // Create the expected value.
224 scoped_ptr<base::ListValue> list_value(new base::ListValue);
225 for (size_t i = 0; i != data.size(); ++i)
226 list_value->Append(new base::StringValue(data[i]));
228 // Pop a string array.
229 MessageReader reader(response.get());
230 scoped_ptr<base::Value> value(PopDataAsValue(&reader));
231 ASSERT_TRUE(value.get() != NULL);
232 EXPECT_TRUE(value->Equals(list_value.get()));
235 TEST(ValuesUtilTest, PopStruct) {
236 scoped_ptr<Response> response(Response::CreateEmpty());
237 // Append a struct.
238 MessageWriter writer(response.get());
239 MessageWriter sub_writer(NULL);
240 writer.OpenStruct(&sub_writer);
241 const bool kBoolValue = true;
242 sub_writer.AppendBool(kBoolValue);
243 const int32 kInt32Value = -123;
244 sub_writer.AppendInt32(kInt32Value);
245 const double kDoubleValue = 1.23;
246 sub_writer.AppendDouble(kDoubleValue);
247 const std::string kStringValue = "one two three";
248 sub_writer.AppendString(kStringValue);
249 writer.CloseContainer(&sub_writer);
251 // Create the expected value.
252 base::ListValue list_value;
253 list_value.Append(new base::FundamentalValue(kBoolValue));
254 list_value.Append(new base::FundamentalValue(kInt32Value));
255 list_value.Append(new base::FundamentalValue(kDoubleValue));
256 list_value.Append(new base::StringValue(kStringValue));
258 // Pop a struct.
259 MessageReader reader(response.get());
260 scoped_ptr<base::Value> value(PopDataAsValue(&reader));
261 ASSERT_TRUE(value.get() != NULL);
262 EXPECT_TRUE(value->Equals(&list_value));
265 TEST(ValuesUtilTest, PopStringToVariantDictionary) {
266 scoped_ptr<Response> response(Response::CreateEmpty());
267 // Append a dictionary.
268 MessageWriter writer(response.get());
269 MessageWriter sub_writer(NULL);
270 MessageWriter entry_writer(NULL);
271 writer.OpenArray("{sv}", &sub_writer);
272 sub_writer.OpenDictEntry(&entry_writer);
273 const std::string kKey1 = "one";
274 entry_writer.AppendString(kKey1);
275 const bool kBoolValue = true;
276 entry_writer.AppendVariantOfBool(kBoolValue);
277 sub_writer.CloseContainer(&entry_writer);
278 sub_writer.OpenDictEntry(&entry_writer);
279 const std::string kKey2 = "two";
280 entry_writer.AppendString(kKey2);
281 const int32 kInt32Value = -45;
282 entry_writer.AppendVariantOfInt32(kInt32Value);
283 sub_writer.CloseContainer(&entry_writer);
284 sub_writer.OpenDictEntry(&entry_writer);
285 const std::string kKey3 = "three";
286 entry_writer.AppendString(kKey3);
287 const double kDoubleValue = 4.9;
288 entry_writer.AppendVariantOfDouble(kDoubleValue);
289 sub_writer.CloseContainer(&entry_writer);
290 sub_writer.OpenDictEntry(&entry_writer);
291 const std::string kKey4 = "four";
292 entry_writer.AppendString(kKey4);
293 const std::string kStringValue = "fifty";
294 entry_writer.AppendVariantOfString(kStringValue);
295 sub_writer.CloseContainer(&entry_writer);
296 writer.CloseContainer(&sub_writer);
298 // Create the expected value.
299 base::DictionaryValue dictionary_value;
300 dictionary_value.SetBoolean(kKey1, kBoolValue);
301 dictionary_value.SetInteger(kKey2, kInt32Value);
302 dictionary_value.SetDouble(kKey3, kDoubleValue);
303 dictionary_value.SetString(kKey4, kStringValue);
305 // Pop a dictinoary.
306 MessageReader reader(response.get());
307 scoped_ptr<base::Value> value(PopDataAsValue(&reader));
308 ASSERT_TRUE(value.get() != NULL);
309 EXPECT_TRUE(value->Equals(&dictionary_value));
312 TEST(ValuesUtilTest, PopDictionaryWithDottedStringKey) {
313 scoped_ptr<Response> response(Response::CreateEmpty());
314 // Append a dictionary.
315 MessageWriter writer(response.get());
316 MessageWriter sub_writer(NULL);
317 MessageWriter entry_writer(NULL);
318 writer.OpenArray("{sv}", &sub_writer);
319 sub_writer.OpenDictEntry(&entry_writer);
320 const std::string kKey1 = "www.example.com"; // String including dots.
321 entry_writer.AppendString(kKey1);
322 const bool kBoolValue = true;
323 entry_writer.AppendVariantOfBool(kBoolValue);
324 sub_writer.CloseContainer(&entry_writer);
325 sub_writer.OpenDictEntry(&entry_writer);
326 const std::string kKey2 = ".example"; // String starting with a dot.
327 entry_writer.AppendString(kKey2);
328 const int32 kInt32Value = -45;
329 entry_writer.AppendVariantOfInt32(kInt32Value);
330 sub_writer.CloseContainer(&entry_writer);
331 sub_writer.OpenDictEntry(&entry_writer);
332 const std::string kKey3 = "example."; // String ending with a dot.
333 entry_writer.AppendString(kKey3);
334 const double kDoubleValue = 4.9;
335 entry_writer.AppendVariantOfDouble(kDoubleValue);
336 sub_writer.CloseContainer(&entry_writer);
337 writer.CloseContainer(&sub_writer);
339 // Create the expected value.
340 base::DictionaryValue dictionary_value;
341 dictionary_value.SetWithoutPathExpansion(
342 kKey1, new base::FundamentalValue(kBoolValue));
343 dictionary_value.SetWithoutPathExpansion(
344 kKey2, new base::FundamentalValue(kInt32Value));
345 dictionary_value.SetWithoutPathExpansion(
346 kKey3, new base::FundamentalValue(kDoubleValue));
348 // Pop a dictinoary.
349 MessageReader reader(response.get());
350 scoped_ptr<base::Value> value(PopDataAsValue(&reader));
351 ASSERT_TRUE(value.get() != NULL);
352 EXPECT_TRUE(value->Equals(&dictionary_value));
355 TEST(ValuesUtilTest, PopDoubleToIntDictionary) {
356 // Create test data.
357 const int32 kValues[] = {0, 1, 1, 2, 3, 5, 8, 13, 21};
358 const std::vector<int32> values(kValues, kValues + arraysize(kValues));
359 std::vector<double> keys(values.size());
360 for (size_t i = 0; i != values.size(); ++i)
361 keys[i] = sqrt(values[i]);
363 // Append a dictionary.
364 scoped_ptr<Response> response(Response::CreateEmpty());
365 MessageWriter writer(response.get());
366 MessageWriter sub_writer(NULL);
367 writer.OpenArray("{di}", &sub_writer);
368 for (size_t i = 0; i != values.size(); ++i) {
369 MessageWriter entry_writer(NULL);
370 sub_writer.OpenDictEntry(&entry_writer);
371 entry_writer.AppendDouble(keys[i]);
372 entry_writer.AppendInt32(values[i]);
373 sub_writer.CloseContainer(&entry_writer);
375 writer.CloseContainer(&sub_writer);
377 // Create the expected value.
378 base::DictionaryValue dictionary_value;
379 for (size_t i = 0; i != values.size(); ++i) {
380 scoped_ptr<base::Value> key_value(new base::FundamentalValue(keys[i]));
381 std::string key_string;
382 base::JSONWriter::Write(key_value.get(), &key_string);
383 dictionary_value.SetWithoutPathExpansion(
384 key_string, new base::FundamentalValue(values[i]));
387 // Pop a dictionary.
388 MessageReader reader(response.get());
389 scoped_ptr<base::Value> value(PopDataAsValue(&reader));
390 ASSERT_TRUE(value.get() != NULL);
391 EXPECT_TRUE(value->Equals(&dictionary_value));
394 TEST(ValuesUtilTest, AppendBasicTypes) {
395 const base::FundamentalValue kBoolValue(false);
396 const base::FundamentalValue kIntegerValue(42);
397 const base::FundamentalValue kDoubleValue(4.2);
398 const base::StringValue kStringValue("string");
400 scoped_ptr<Response> response(Response::CreateEmpty());
401 MessageWriter writer(response.get());
402 AppendBasicTypeValueData(&writer, kBoolValue);
403 AppendBasicTypeValueData(&writer, kIntegerValue);
404 AppendBasicTypeValueData(&writer, kDoubleValue);
405 AppendBasicTypeValueData(&writer, kStringValue);
407 MessageReader reader(response.get());
408 scoped_ptr<base::Value> value;
409 value.reset(PopDataAsValue(&reader));
410 ASSERT_TRUE(value.get() != NULL);
411 EXPECT_TRUE(value->Equals(&kBoolValue));
412 value.reset(PopDataAsValue(&reader));
413 ASSERT_TRUE(value.get() != NULL);
414 EXPECT_TRUE(value->Equals(&kIntegerValue));
415 value.reset(PopDataAsValue(&reader));
416 ASSERT_TRUE(value.get() != NULL);
417 EXPECT_TRUE(value->Equals(&kDoubleValue));
418 value.reset(PopDataAsValue(&reader));
419 ASSERT_TRUE(value.get() != NULL);
420 EXPECT_TRUE(value->Equals(&kStringValue));
423 TEST(ValuesUtilTest, AppendBasicTypesAsVariant) {
424 const base::FundamentalValue kBoolValue(false);
425 const base::FundamentalValue kIntegerValue(42);
426 const base::FundamentalValue kDoubleValue(4.2);
427 const base::StringValue kStringValue("string");
429 scoped_ptr<Response> response(Response::CreateEmpty());
430 MessageWriter writer(response.get());
431 AppendBasicTypeValueDataAsVariant(&writer, kBoolValue);
432 AppendBasicTypeValueDataAsVariant(&writer, kIntegerValue);
433 AppendBasicTypeValueDataAsVariant(&writer, kDoubleValue);
434 AppendBasicTypeValueDataAsVariant(&writer, kStringValue);
436 MessageReader reader(response.get());
437 scoped_ptr<base::Value> value;
438 value.reset(PopDataAsValue(&reader));
439 ASSERT_TRUE(value.get() != NULL);
440 EXPECT_TRUE(value->Equals(&kBoolValue));
441 value.reset(PopDataAsValue(&reader));
442 ASSERT_TRUE(value.get() != NULL);
443 EXPECT_TRUE(value->Equals(&kIntegerValue));
444 value.reset(PopDataAsValue(&reader));
445 ASSERT_TRUE(value.get() != NULL);
446 EXPECT_TRUE(value->Equals(&kDoubleValue));
447 value.reset(PopDataAsValue(&reader));
448 ASSERT_TRUE(value.get() != NULL);
449 EXPECT_TRUE(value->Equals(&kStringValue));
452 } // namespace dbus