[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "dbus/values_util.h" |
| 6 | |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
Hans Wennborg | 7219349 | 2015-04-24 21:38:04 | [diff] [blame] | 10 | #include <cmath> |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 11 | #include <memory> |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 12 | #include <utility> |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 15 | #include "base/json/json_writer.h" |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 16 | #include "base/macros.h" |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 17 | #include "base/values.h" |
| 18 | #include "dbus/message.h" |
| 19 | #include "testing/gtest/include/gtest/gtest.h" |
| 20 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 21 | namespace dbus { |
| 22 | |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 23 | TEST(ValuesUtilTest, PopBasicTypes) { |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 24 | std::unique_ptr<Response> response(Response::CreateEmpty()); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 25 | // Append basic type values. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 26 | MessageWriter writer(response.get()); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 27 | const uint8_t kByteValue = 42; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 28 | writer.AppendByte(kByteValue); |
| 29 | const bool kBoolValue = true; |
| 30 | writer.AppendBool(kBoolValue); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 31 | const int16_t kInt16Value = -43; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 32 | writer.AppendInt16(kInt16Value); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 33 | const uint16_t kUint16Value = 44; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 34 | writer.AppendUint16(kUint16Value); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 35 | const int32_t kInt32Value = -45; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 36 | writer.AppendInt32(kInt32Value); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 37 | const uint32_t kUint32Value = 46; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 38 | writer.AppendUint32(kUint32Value); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 39 | const int64_t kInt64Value = -47; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 40 | writer.AppendInt64(kInt64Value); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 41 | const uint64_t kUint64Value = 48; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 42 | 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] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 49 | const ObjectPath kObjectPathValue("/ObjectPath"); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 50 | writer.AppendObjectPath(kObjectPathValue); |
| 51 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 52 | MessageReader reader(response.get()); |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 53 | std::unique_ptr<base::Value> value; |
| 54 | std::unique_ptr<base::Value> expected_value; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 55 | // Pop a byte. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 56 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 57 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 58 | expected_value.reset(new base::FundamentalValue(kByteValue)); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 59 | EXPECT_TRUE(value->Equals(expected_value.get())); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 60 | // Pop a bool. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 61 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 62 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 63 | expected_value.reset(new base::FundamentalValue(kBoolValue)); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 64 | EXPECT_TRUE(value->Equals(expected_value.get())); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 65 | // Pop an int16_t. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 66 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 67 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 68 | expected_value.reset(new base::FundamentalValue(kInt16Value)); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 69 | EXPECT_TRUE(value->Equals(expected_value.get())); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 70 | // Pop a uint16_t. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 71 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 72 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 73 | expected_value.reset(new base::FundamentalValue(kUint16Value)); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 74 | EXPECT_TRUE(value->Equals(expected_value.get())); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 75 | // Pop an int32_t. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 76 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 77 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 78 | expected_value.reset(new base::FundamentalValue(kInt32Value)); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 79 | EXPECT_TRUE(value->Equals(expected_value.get())); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 80 | // Pop a uint32_t. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 81 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 82 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 83 | expected_value.reset( |
| 84 | new base::FundamentalValue(static_cast<double>(kUint32Value))); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 85 | EXPECT_TRUE(value->Equals(expected_value.get())); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 86 | // Pop an int64_t. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 87 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 88 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 89 | expected_value.reset( |
| 90 | new base::FundamentalValue(static_cast<double>(kInt64Value))); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 91 | EXPECT_TRUE(value->Equals(expected_value.get())); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 92 | // Pop a uint64_t. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 93 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 94 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 95 | expected_value.reset( |
| 96 | new base::FundamentalValue(static_cast<double>(kUint64Value))); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 97 | EXPECT_TRUE(value->Equals(expected_value.get())); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 98 | // Pop a double. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 99 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 100 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 101 | expected_value.reset(new base::FundamentalValue(kDoubleValue)); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 102 | EXPECT_TRUE(value->Equals(expected_value.get())); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 103 | // Pop a string. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 104 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 105 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 106 | expected_value.reset(new base::StringValue(kStringValue)); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 107 | EXPECT_TRUE(value->Equals(expected_value.get())); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 108 | // Pop an empty string. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 109 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 110 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 111 | expected_value.reset(new base::StringValue(kEmptyStringValue)); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 112 | EXPECT_TRUE(value->Equals(expected_value.get())); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 113 | // Pop an object path. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 114 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 115 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 116 | expected_value.reset(new base::StringValue(kObjectPathValue.value())); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 117 | EXPECT_TRUE(value->Equals(expected_value.get())); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | TEST(ValuesUtilTest, PopVariant) { |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 121 | std::unique_ptr<Response> response(Response::CreateEmpty()); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 122 | // Append variant values. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 123 | MessageWriter writer(response.get()); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 124 | const bool kBoolValue = true; |
| 125 | writer.AppendVariantOfBool(kBoolValue); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 126 | const int32_t kInt32Value = -45; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 127 | 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] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 133 | MessageReader reader(response.get()); |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 134 | std::unique_ptr<base::Value> value; |
| 135 | std::unique_ptr<base::Value> expected_value; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 136 | // Pop a bool. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 137 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 138 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 139 | expected_value.reset(new base::FundamentalValue(kBoolValue)); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 140 | EXPECT_TRUE(value->Equals(expected_value.get())); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 141 | // Pop an int32_t. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 142 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 143 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 144 | expected_value.reset(new base::FundamentalValue(kInt32Value)); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 145 | EXPECT_TRUE(value->Equals(expected_value.get())); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 146 | // Pop a double. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 147 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 148 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 149 | expected_value.reset(new base::FundamentalValue(kDoubleValue)); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 150 | EXPECT_TRUE(value->Equals(expected_value.get())); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 151 | // Pop a string. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 152 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 153 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 154 | expected_value.reset(new base::StringValue(kStringValue)); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 155 | EXPECT_TRUE(value->Equals(expected_value.get())); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | // Pop extremely large integers which cannot be precisely represented in |
| 159 | // double. |
| 160 | TEST(ValuesUtilTest, PopExtremelyLargeIntegers) { |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 161 | std::unique_ptr<Response> response(Response::CreateEmpty()); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 162 | // Append large integers. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 163 | MessageWriter writer(response.get()); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 164 | const int64_t kInt64Value = -123456789012345689LL; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 165 | writer.AppendInt64(kInt64Value); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 166 | const uint64_t kUint64Value = 9876543210987654321ULL; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 167 | writer.AppendUint64(kUint64Value); |
| 168 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 169 | MessageReader reader(response.get()); |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 170 | std::unique_ptr<base::Value> value; |
| 171 | std::unique_ptr<base::Value> expected_value; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 172 | double double_value = 0; |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 173 | // Pop an int64_t. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 174 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 175 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 176 | expected_value.reset( |
| 177 | new base::FundamentalValue(static_cast<double>(kInt64Value))); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 178 | EXPECT_TRUE(value->Equals(expected_value.get())); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 179 | ASSERT_TRUE(value->GetAsDouble(&double_value)); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 180 | EXPECT_NE(kInt64Value, static_cast<int64_t>(double_value)); |
| 181 | // Pop a uint64_t. |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 182 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 183 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 184 | expected_value.reset( |
| 185 | new base::FundamentalValue(static_cast<double>(kUint64Value))); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 186 | EXPECT_TRUE(value->Equals(expected_value.get())); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 187 | ASSERT_TRUE(value->GetAsDouble(&double_value)); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 188 | EXPECT_NE(kUint64Value, static_cast<uint64_t>(double_value)); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | TEST(ValuesUtilTest, PopIntArray) { |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 192 | std::unique_ptr<Response> response(Response::CreateEmpty()); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 193 | // Append an int32_t array. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 194 | MessageWriter writer(response.get()); |
| 195 | MessageWriter sub_writer(NULL); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 196 | std::vector<int32_t> data; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 197 | 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. |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 206 | std::unique_ptr<base::ListValue> list_value(new base::ListValue); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 207 | for (size_t i = 0; i != data.size(); ++i) |
dcheng | 4f046e85 | 2016-06-07 05:05:14 | [diff] [blame] | 208 | list_value->AppendInteger(data[i]); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 209 | |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 210 | // Pop an int32_t array. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 211 | MessageReader reader(response.get()); |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 212 | std::unique_ptr<base::Value> value(PopDataAsValue(&reader)); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 213 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 214 | EXPECT_TRUE(value->Equals(list_value.get())); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | TEST(ValuesUtilTest, PopStringArray) { |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 218 | std::unique_ptr<Response> response(Response::CreateEmpty()); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 219 | // Append a string array. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 220 | MessageWriter writer(response.get()); |
| 221 | MessageWriter sub_writer(NULL); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 222 | 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. |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 229 | std::unique_ptr<base::ListValue> list_value(new base::ListValue); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 230 | for (size_t i = 0; i != data.size(); ++i) |
dcheng | 4f046e85 | 2016-06-07 05:05:14 | [diff] [blame] | 231 | list_value->AppendString(data[i]); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 232 | |
| 233 | // Pop a string array. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 234 | MessageReader reader(response.get()); |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 235 | std::unique_ptr<base::Value> value(PopDataAsValue(&reader)); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 236 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | ba4ce1c | 2012-05-31 06:55:53 | [diff] [blame] | 237 | EXPECT_TRUE(value->Equals(list_value.get())); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | TEST(ValuesUtilTest, PopStruct) { |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 241 | std::unique_ptr<Response> response(Response::CreateEmpty()); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 242 | // Append a struct. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 243 | MessageWriter writer(response.get()); |
| 244 | MessageWriter sub_writer(NULL); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 245 | writer.OpenStruct(&sub_writer); |
| 246 | const bool kBoolValue = true; |
| 247 | sub_writer.AppendBool(kBoolValue); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 248 | const int32_t kInt32Value = -123; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 249 | 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] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 257 | base::ListValue list_value; |
dcheng | 4f046e85 | 2016-06-07 05:05:14 | [diff] [blame] | 258 | list_value.AppendBoolean(kBoolValue); |
| 259 | list_value.AppendInteger(kInt32Value); |
| 260 | list_value.AppendDouble(kDoubleValue); |
| 261 | list_value.AppendString(kStringValue); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 262 | |
| 263 | // Pop a struct. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 264 | MessageReader reader(response.get()); |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 265 | std::unique_ptr<base::Value> value(PopDataAsValue(&reader)); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 266 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 267 | EXPECT_TRUE(value->Equals(&list_value)); |
| 268 | } |
| 269 | |
| 270 | TEST(ValuesUtilTest, PopStringToVariantDictionary) { |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 271 | std::unique_ptr<Response> response(Response::CreateEmpty()); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 272 | // Append a dictionary. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 273 | MessageWriter writer(response.get()); |
| 274 | MessageWriter sub_writer(NULL); |
| 275 | MessageWriter entry_writer(NULL); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 276 | 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); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 286 | const int32_t kInt32Value = -45; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 287 | 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] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 304 | base::DictionaryValue dictionary_value; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 305 | 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] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 311 | MessageReader reader(response.get()); |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 312 | std::unique_ptr<base::Value> value(PopDataAsValue(&reader)); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 313 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 314 | EXPECT_TRUE(value->Equals(&dictionary_value)); |
| 315 | } |
| 316 | |
[email protected] | 0f0117b24 | 2012-03-20 18:18:57 | [diff] [blame] | 317 | TEST(ValuesUtilTest, PopDictionaryWithDottedStringKey) { |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 318 | std::unique_ptr<Response> response(Response::CreateEmpty()); |
[email protected] | 0f0117b24 | 2012-03-20 18:18:57 | [diff] [blame] | 319 | // Append a dictionary. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 320 | MessageWriter writer(response.get()); |
| 321 | MessageWriter sub_writer(NULL); |
| 322 | MessageWriter entry_writer(NULL); |
[email protected] | 0f0117b24 | 2012-03-20 18:18:57 | [diff] [blame] | 323 | 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); |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 333 | const int32_t kInt32Value = -45; |
[email protected] | 0f0117b24 | 2012-03-20 18:18:57 | [diff] [blame] | 334 | 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] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 345 | base::DictionaryValue dictionary_value; |
[email protected] | 0f0117b24 | 2012-03-20 18:18:57 | [diff] [blame] | 346 | dictionary_value.SetWithoutPathExpansion( |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 347 | kKey1, new base::FundamentalValue(kBoolValue)); |
[email protected] | 0f0117b24 | 2012-03-20 18:18:57 | [diff] [blame] | 348 | dictionary_value.SetWithoutPathExpansion( |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 349 | kKey2, new base::FundamentalValue(kInt32Value)); |
[email protected] | 0f0117b24 | 2012-03-20 18:18:57 | [diff] [blame] | 350 | dictionary_value.SetWithoutPathExpansion( |
[email protected] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 351 | kKey3, new base::FundamentalValue(kDoubleValue)); |
[email protected] | 0f0117b24 | 2012-03-20 18:18:57 | [diff] [blame] | 352 | |
| 353 | // Pop a dictinoary. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 354 | MessageReader reader(response.get()); |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 355 | std::unique_ptr<base::Value> value(PopDataAsValue(&reader)); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 356 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | 0f0117b24 | 2012-03-20 18:18:57 | [diff] [blame] | 357 | EXPECT_TRUE(value->Equals(&dictionary_value)); |
| 358 | } |
| 359 | |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 360 | TEST(ValuesUtilTest, PopDoubleToIntDictionary) { |
| 361 | // Create test data. |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 362 | 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] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 364 | std::vector<double> keys(values.size()); |
| 365 | for (size_t i = 0; i != values.size(); ++i) |
Hans Wennborg | 7219349 | 2015-04-24 21:38:04 | [diff] [blame] | 366 | keys[i] = std::sqrt(values[i]); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 367 | |
| 368 | // Append a dictionary. |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 369 | std::unique_ptr<Response> response(Response::CreateEmpty()); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 370 | MessageWriter writer(response.get()); |
| 371 | MessageWriter sub_writer(NULL); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 372 | writer.OpenArray("{di}", &sub_writer); |
| 373 | for (size_t i = 0; i != values.size(); ++i) { |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 374 | MessageWriter entry_writer(NULL); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 375 | 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] | f40b736 | 2013-02-12 20:06:15 | [diff] [blame] | 383 | base::DictionaryValue dictionary_value; |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 384 | for (size_t i = 0; i != values.size(); ++i) { |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 385 | std::string key_string; |
estade | 8d04646 | 2015-05-16 01:02:34 | [diff] [blame] | 386 | base::JSONWriter::Write(base::FundamentalValue(keys[i]), &key_string); |
| 387 | dictionary_value.SetIntegerWithoutPathExpansion(key_string, values[i]); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // Pop a dictionary. |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 391 | MessageReader reader(response.get()); |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 392 | std::unique_ptr<base::Value> value(PopDataAsValue(&reader)); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 393 | ASSERT_TRUE(value.get() != NULL); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 394 | EXPECT_TRUE(value->Equals(&dictionary_value)); |
| 395 | } |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 396 | |
| 397 | TEST(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 | |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 403 | std::unique_ptr<Response> response(Response::CreateEmpty()); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 404 | MessageWriter writer(response.get()); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 405 | AppendBasicTypeValueData(&writer, kBoolValue); |
| 406 | AppendBasicTypeValueData(&writer, kIntegerValue); |
| 407 | AppendBasicTypeValueData(&writer, kDoubleValue); |
| 408 | AppendBasicTypeValueData(&writer, kStringValue); |
| 409 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 410 | MessageReader reader(response.get()); |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 411 | std::unique_ptr<base::Value> value; |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 412 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 413 | ASSERT_TRUE(value.get() != NULL); |
| 414 | EXPECT_TRUE(value->Equals(&kBoolValue)); |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 415 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 416 | ASSERT_TRUE(value.get() != NULL); |
| 417 | EXPECT_TRUE(value->Equals(&kIntegerValue)); |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 418 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 419 | ASSERT_TRUE(value.get() != NULL); |
| 420 | EXPECT_TRUE(value->Equals(&kDoubleValue)); |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 421 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 422 | ASSERT_TRUE(value.get() != NULL); |
| 423 | EXPECT_TRUE(value->Equals(&kStringValue)); |
| 424 | } |
| 425 | |
| 426 | TEST(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 | |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 432 | std::unique_ptr<Response> response(Response::CreateEmpty()); |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 433 | MessageWriter writer(response.get()); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 434 | AppendBasicTypeValueDataAsVariant(&writer, kBoolValue); |
| 435 | AppendBasicTypeValueDataAsVariant(&writer, kIntegerValue); |
| 436 | AppendBasicTypeValueDataAsVariant(&writer, kDoubleValue); |
| 437 | AppendBasicTypeValueDataAsVariant(&writer, kStringValue); |
| 438 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 439 | MessageReader reader(response.get()); |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 440 | std::unique_ptr<base::Value> value; |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 441 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 442 | ASSERT_TRUE(value.get() != NULL); |
| 443 | EXPECT_TRUE(value->Equals(&kBoolValue)); |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 444 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 445 | ASSERT_TRUE(value.get() != NULL); |
| 446 | EXPECT_TRUE(value->Equals(&kIntegerValue)); |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 447 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 448 | ASSERT_TRUE(value.get() != NULL); |
| 449 | EXPECT_TRUE(value->Equals(&kDoubleValue)); |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 450 | value = PopDataAsValue(&reader); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 451 | ASSERT_TRUE(value.get() != NULL); |
| 452 | EXPECT_TRUE(value->Equals(&kStringValue)); |
| 453 | } |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 454 | |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 455 | TEST(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 | |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 461 | std::unique_ptr<Response> response(Response::CreateEmpty()); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 462 | 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()); |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 469 | std::unique_ptr<base::Value> value; |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 470 | value = PopDataAsValue(&reader); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 471 | ASSERT_TRUE(value.get() != NULL); |
| 472 | EXPECT_TRUE(value->Equals(&kBoolValue)); |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 473 | value = PopDataAsValue(&reader); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 474 | ASSERT_TRUE(value.get() != NULL); |
| 475 | EXPECT_TRUE(value->Equals(&kIntegerValue)); |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 476 | value = PopDataAsValue(&reader); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 477 | ASSERT_TRUE(value.get() != NULL); |
| 478 | EXPECT_TRUE(value->Equals(&kDoubleValue)); |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 479 | value = PopDataAsValue(&reader); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 480 | ASSERT_TRUE(value.get() != NULL); |
| 481 | EXPECT_TRUE(value->Equals(&kStringValue)); |
| 482 | } |
| 483 | |
| 484 | TEST(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 | |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 490 | std::unique_ptr<Response> response(Response::CreateEmpty()); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 491 | 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()); |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 498 | std::unique_ptr<base::Value> value; |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 499 | value = PopDataAsValue(&reader); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 500 | ASSERT_TRUE(value.get() != NULL); |
| 501 | EXPECT_TRUE(value->Equals(&kBoolValue)); |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 502 | value = PopDataAsValue(&reader); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 503 | ASSERT_TRUE(value.get() != NULL); |
| 504 | EXPECT_TRUE(value->Equals(&kIntegerValue)); |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 505 | value = PopDataAsValue(&reader); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 506 | ASSERT_TRUE(value.get() != NULL); |
| 507 | EXPECT_TRUE(value->Equals(&kDoubleValue)); |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 508 | value = PopDataAsValue(&reader); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 509 | ASSERT_TRUE(value.get() != NULL); |
| 510 | EXPECT_TRUE(value->Equals(&kStringValue)); |
| 511 | } |
| 512 | |
| 513 | TEST(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; |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 523 | const int32_t kInt32Value = -45; |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 524 | 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 | |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 543 | std::unique_ptr<Response> response(Response::CreateEmpty()); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 544 | 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()); |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 551 | std::unique_ptr<base::Value> value; |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 552 | value = PopDataAsValue(&reader); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 553 | ASSERT_TRUE(value.get() != NULL); |
| 554 | EXPECT_TRUE(value->Equals(&test_dictionary)); |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 555 | value = PopDataAsValue(&reader); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 556 | ASSERT_TRUE(value.get() != NULL); |
| 557 | EXPECT_TRUE(value->Equals(&int_value)); |
| 558 | } |
| 559 | |
| 560 | TEST(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; |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 570 | const int32_t kInt32Value = -45; |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 571 | 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 | |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 590 | std::unique_ptr<Response> response(Response::CreateEmpty()); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 591 | 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()); |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 598 | std::unique_ptr<base::Value> value; |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 599 | value = PopDataAsValue(&reader); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 600 | ASSERT_TRUE(value.get() != NULL); |
| 601 | EXPECT_TRUE(value->Equals(&test_dictionary)); |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 602 | value = PopDataAsValue(&reader); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 603 | ASSERT_TRUE(value.get() != NULL); |
| 604 | EXPECT_TRUE(value->Equals(&int_value)); |
| 605 | } |
| 606 | |
| 607 | TEST(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; |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 613 | const int32_t kInt32Value = -45; |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 614 | const double kDoubleValue = 4.9; |
| 615 | const std::string kStringValue = "fifty"; |
| 616 | |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 617 | std::unique_ptr<base::ListValue> list_value(new base::ListValue()); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 618 | list_value->AppendBoolean(kBoolValue); |
| 619 | list_value->AppendInteger(kInt32Value); |
| 620 | |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 621 | std::unique_ptr<base::DictionaryValue> dictionary_value( |
| 622 | new base::DictionaryValue()); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 623 | 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); |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 631 | test_list.Append(std::move(list_value)); |
| 632 | test_list.Append(std::move(dictionary_value)); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 633 | |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 634 | std::unique_ptr<Response> response(Response::CreateEmpty()); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 635 | 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()); |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 642 | std::unique_ptr<base::Value> value; |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 643 | value = PopDataAsValue(&reader); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 644 | ASSERT_TRUE(value.get() != NULL); |
| 645 | EXPECT_TRUE(value->Equals(&test_list)); |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 646 | value = PopDataAsValue(&reader); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 647 | ASSERT_TRUE(value.get() != NULL); |
| 648 | EXPECT_TRUE(value->Equals(&int_value)); |
| 649 | } |
| 650 | |
| 651 | TEST(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; |
avi | 22437c69 | 2015-12-22 18:12:45 | [diff] [blame] | 657 | const int32_t kInt32Value = -45; |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 658 | const double kDoubleValue = 4.9; |
| 659 | const std::string kStringValue = "fifty"; |
| 660 | |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 661 | std::unique_ptr<base::ListValue> list_value(new base::ListValue()); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 662 | list_value->AppendBoolean(kBoolValue); |
| 663 | list_value->AppendInteger(kInt32Value); |
| 664 | |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 665 | std::unique_ptr<base::DictionaryValue> dictionary_value( |
| 666 | new base::DictionaryValue()); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 667 | 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); |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 675 | test_list.Append(std::move(list_value)); |
| 676 | test_list.Append(std::move(dictionary_value)); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 677 | |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 678 | std::unique_ptr<Response> response(Response::CreateEmpty()); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 679 | 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()); |
dcheng | 2a19328 | 2016-04-08 22:55:04 | [diff] [blame] | 686 | std::unique_ptr<base::Value> value; |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 687 | value = PopDataAsValue(&reader); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 688 | ASSERT_TRUE(value.get() != NULL); |
| 689 | EXPECT_TRUE(value->Equals(&test_list)); |
dcheng | cef03546 | 2016-05-30 06:33:58 | [diff] [blame] | 690 | value = PopDataAsValue(&reader); |
[email protected] | a68242f | 2014-05-22 11:52:55 | [diff] [blame] | 691 | ASSERT_TRUE(value.get() != NULL); |
| 692 | EXPECT_TRUE(value->Equals(&int_value)); |
| 693 | } |
| 694 | |
[email protected] | 2a57ca64 | 2013-06-13 06:37:19 | [diff] [blame] | 695 | } // namespace dbus |