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