OLD | NEW |
(Empty) | |
| 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)); |
| 49 EXPECT_TRUE(value.get() != NULL); |
| 50 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(kByteValue))); |
| 51 // Pop a bool. |
| 52 value.reset(dbus::PopDataAsValue(&reader)); |
| 53 EXPECT_TRUE(value.get() != NULL); |
| 54 EXPECT_TRUE(value->Equals(Value::CreateBooleanValue(kBoolValue))); |
| 55 // Pop an int16. |
| 56 value.reset(dbus::PopDataAsValue(&reader)); |
| 57 EXPECT_TRUE(value.get() != NULL); |
| 58 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(kInt16Value))); |
| 59 // Pop a uint16. |
| 60 value.reset(dbus::PopDataAsValue(&reader)); |
| 61 EXPECT_TRUE(value.get() != NULL); |
| 62 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(kUint16Value))); |
| 63 // Pop an int32. |
| 64 value.reset(dbus::PopDataAsValue(&reader)); |
| 65 EXPECT_TRUE(value.get() != NULL); |
| 66 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(kInt32Value))); |
| 67 // Pop a uint32. |
| 68 value.reset(dbus::PopDataAsValue(&reader)); |
| 69 EXPECT_TRUE(value.get() != NULL); |
| 70 EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kUint32Value))); |
| 71 // Pop an int64. |
| 72 value.reset(dbus::PopDataAsValue(&reader)); |
| 73 EXPECT_TRUE(value.get() != NULL); |
| 74 EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kInt64Value))); |
| 75 // Pop a uint64. |
| 76 value.reset(dbus::PopDataAsValue(&reader)); |
| 77 EXPECT_TRUE(value.get() != NULL); |
| 78 EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kUint64Value))); |
| 79 // Pop a double. |
| 80 value.reset(dbus::PopDataAsValue(&reader)); |
| 81 EXPECT_TRUE(value.get() != NULL); |
| 82 EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kDoubleValue))); |
| 83 // Pop a string. |
| 84 value.reset(dbus::PopDataAsValue(&reader)); |
| 85 EXPECT_TRUE(value.get() != NULL); |
| 86 EXPECT_TRUE(value->Equals(Value::CreateStringValue(kStringValue))); |
| 87 // Pop an empty string. |
| 88 value.reset(dbus::PopDataAsValue(&reader)); |
| 89 EXPECT_TRUE(value.get() != NULL); |
| 90 EXPECT_TRUE(value->Equals(Value::CreateStringValue(kEmptyStringValue))); |
| 91 // Pop an object path. |
| 92 value.reset(dbus::PopDataAsValue(&reader)); |
| 93 EXPECT_TRUE(value.get() != NULL); |
| 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)); |
| 115 EXPECT_TRUE(value.get() != NULL); |
| 116 EXPECT_TRUE(value->Equals(Value::CreateBooleanValue(kBoolValue))); |
| 117 // Pop an int32. |
| 118 value.reset(dbus::PopDataAsValue(&reader)); |
| 119 EXPECT_TRUE(value.get() != NULL); |
| 120 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(kInt32Value))); |
| 121 // Pop a double. |
| 122 value.reset(dbus::PopDataAsValue(&reader)); |
| 123 EXPECT_TRUE(value.get() != NULL); |
| 124 EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kDoubleValue))); |
| 125 // Pop a string. |
| 126 value.reset(dbus::PopDataAsValue(&reader)); |
| 127 EXPECT_TRUE(value.get() != NULL); |
| 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)); |
| 147 EXPECT_TRUE(value.get() != NULL); |
| 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)); |
| 153 EXPECT_TRUE(value.get() != NULL); |
| 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)); |
| 181 EXPECT_TRUE(value.get() != NULL); |
| 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)); |
| 204 EXPECT_TRUE(value.get() != NULL); |
| 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)); |
| 234 EXPECT_TRUE(value.get() != NULL); |
| 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)); |
| 281 EXPECT_TRUE(value.get() != NULL); |
| 282 EXPECT_TRUE(value->Equals(&dictionary_value)); |
| 283 } |
| 284 |
| 285 TEST(ValuesUtilTest, PopDoubleToIntDictionary) { |
| 286 // Create test data. |
| 287 const int32 kValues[] = {0, 1, 1, 2, 3, 5, 8, 13, 21}; |
| 288 const std::vector<int32> values(kValues, kValues + arraysize(kValues)); |
| 289 std::vector<double> keys(values.size()); |
| 290 for (size_t i = 0; i != values.size(); ++i) |
| 291 keys[i] = sqrt(values[i]); |
| 292 |
| 293 // Append a dictionary. |
| 294 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
| 295 dbus::MessageWriter writer(response.get()); |
| 296 dbus::MessageWriter sub_writer(NULL); |
| 297 writer.OpenArray("{di}", &sub_writer); |
| 298 for (size_t i = 0; i != values.size(); ++i) { |
| 299 dbus::MessageWriter entry_writer(NULL); |
| 300 sub_writer.OpenDictEntry(&entry_writer); |
| 301 entry_writer.AppendDouble(keys[i]); |
| 302 entry_writer.AppendInt32(values[i]); |
| 303 sub_writer.CloseContainer(&entry_writer); |
| 304 } |
| 305 writer.CloseContainer(&sub_writer); |
| 306 |
| 307 // Create the expected value. |
| 308 DictionaryValue dictionary_value; |
| 309 for (size_t i = 0; i != values.size(); ++i) { |
| 310 scoped_ptr<Value> key_value(Value::CreateDoubleValue(keys[i])); |
| 311 std::string key_string; |
| 312 base::JSONWriter::Write(key_value.get(), &key_string); |
| 313 dictionary_value.SetInteger(key_string, values[i]); |
| 314 } |
| 315 |
| 316 // Pop a dictionary. |
| 317 dbus::MessageReader reader(response.get()); |
| 318 scoped_ptr<Value> value(dbus::PopDataAsValue(&reader)); |
| 319 EXPECT_TRUE(value.get() != NULL); |
| 320 EXPECT_TRUE(value->Equals(&dictionary_value)); |
| 321 } |
OLD | NEW |