diff options
author | hashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-24 20:37:18 +0000 |
---|---|---|
committer | hashimoto@chromium.org <hashimoto@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-24 20:37:18 +0000 |
commit | 6e04d17161b8d53770979fb5a451da8174e255c6 (patch) | |
tree | 3ca941138578159e4059e52f94a105c1242ed609 /dbus | |
parent | ffd60e434c0dff91d8cb9251c08d901b12346b2a (diff) | |
download | chromium_src-6e04d17161b8d53770979fb5a451da8174e255c6.zip chromium_src-6e04d17161b8d53770979fb5a451da8174e255c6.tar.gz chromium_src-6e04d17161b8d53770979fb5a451da8174e255c6.tar.bz2 |
Add dbus::AppendBasicTypeValueData
BUG=chromium-os:16557
TEST=dbus_unittests --gtest_filter="ValuesUtilTest.Append*"
Review URL: http://codereview.chromium.org/9826044
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@128771 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'dbus')
-rw-r--r-- | dbus/values_util.cc | 61 | ||||
-rw-r--r-- | dbus/values_util.h | 8 | ||||
-rw-r--r-- | dbus/values_util_unittest.cc | 106 |
3 files changed, 151 insertions, 24 deletions
diff --git a/dbus/values_util.cc b/dbus/values_util.cc index b78638d..e341ac2 100644 --- a/dbus/values_util.cc +++ b/dbus/values_util.cc @@ -62,6 +62,27 @@ bool PopDictionaryEntries(MessageReader* reader, return true; } +// Gets the D-Bus type signature for the value. +std::string GetTypeSignature(const base::Value& value) { + switch (value.GetType()) { + case base::Value::TYPE_BOOLEAN: + return "b"; + case base::Value::TYPE_INTEGER: + return "i"; + case base::Value::TYPE_DOUBLE: + return "d"; + case base::Value::TYPE_STRING: + return "s"; + case base::Value::TYPE_BINARY: + return "ay"; + case base::Value::TYPE_DICTIONARY: + return "a{sv}"; + default: + DLOG(ERROR) << "Unexpected type " << value.GetType(); + return ""; + } +} + } // namespace Value* PopDataAsValue(MessageReader* reader) { @@ -182,4 +203,44 @@ Value* PopDataAsValue(MessageReader* reader) { return result; } +void AppendBasicTypeValueData(MessageWriter* writer, const base::Value& value) { + switch (value.GetType()) { + case base::Value::TYPE_BOOLEAN: { + bool bool_value = false; + value.GetAsBoolean(&bool_value); + writer->AppendBool(bool_value); + break; + } + case base::Value::TYPE_INTEGER: { + int int_value = 0; + value.GetAsInteger(&int_value); + writer->AppendInt32(int_value); + break; + } + case base::Value::TYPE_DOUBLE: { + double double_value = 0; + value.GetAsDouble(&double_value); + writer->AppendDouble(double_value); + break; + } + case base::Value::TYPE_STRING: { + std::string string_value; + value.GetAsString(&string_value); + writer->AppendString(string_value); + break; + } + default: + DLOG(ERROR) << "Unexpected type " << value.GetType(); + break; + } +} + +void AppendBasicTypeValueDataAsVariant(MessageWriter* writer, + const base::Value& value) { + MessageWriter sub_writer(NULL); + writer->OpenVariant(GetTypeSignature(value), &sub_writer); + AppendBasicTypeValueData(&sub_writer, value); + writer->CloseContainer(&sub_writer); +} + } // namespace dbus diff --git a/dbus/values_util.h b/dbus/values_util.h index cfb31a5..535a402 100644 --- a/dbus/values_util.h +++ b/dbus/values_util.h @@ -13,6 +13,7 @@ class Value; namespace dbus { class MessageReader; +class MessageWriter; // Pops a value from |reader| as a base::Value. // Returns NULL if an error occurs. @@ -20,6 +21,13 @@ class MessageReader; // double. Non-string diciontary keys are converted to strings. base::Value* PopDataAsValue(MessageReader* reader); +// Appends a basic type value to |writer|. +void AppendBasicTypeValueData(MessageWriter* writer, const base::Value& value); + +// Appends a basic type value to |writer| as a variant. +void AppendBasicTypeValueDataAsVariant(MessageWriter* writer, + const base::Value& value); + } // namespace dbus #endif // DBUS_VALUES_UTIL_H_ diff --git a/dbus/values_util_unittest.cc b/dbus/values_util_unittest.cc index f97c55a..9dca3ee 100644 --- a/dbus/values_util_unittest.cc +++ b/dbus/values_util_unittest.cc @@ -46,51 +46,51 @@ TEST(ValuesUtilTest, PopBasicTypes) { scoped_ptr<Value> value; // Pop a byte. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(kByteValue))); // Pop a bool. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(Value::CreateBooleanValue(kBoolValue))); // Pop an int16. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(kInt16Value))); // Pop a uint16. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(kUint16Value))); // Pop an int32. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(kInt32Value))); // Pop a uint32. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kUint32Value))); // Pop an int64. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kInt64Value))); // Pop a uint64. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kUint64Value))); // Pop a double. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kDoubleValue))); // Pop a string. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(Value::CreateStringValue(kStringValue))); // Pop an empty string. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(Value::CreateStringValue(kEmptyStringValue))); // Pop an object path. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE( value->Equals(Value::CreateStringValue(kObjectPathValue.value()))); } @@ -112,19 +112,19 @@ TEST(ValuesUtilTest, PopVariant) { scoped_ptr<Value> value; // Pop a bool. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(Value::CreateBooleanValue(kBoolValue))); // Pop an int32. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(kInt32Value))); // Pop a double. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kDoubleValue))); // Pop a string. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(Value::CreateStringValue(kStringValue))); } @@ -144,13 +144,13 @@ TEST(ValuesUtilTest, PopExtremelyLargeIntegers) { double double_value = 0; // Pop an int64. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kInt64Value))); ASSERT_TRUE(value->GetAsDouble(&double_value)); EXPECT_NE(kInt64Value, static_cast<int64>(double_value)); // Pop a uint64. value.reset(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kUint64Value))); ASSERT_TRUE(value->GetAsDouble(&double_value)); EXPECT_NE(kUint64Value, static_cast<uint64>(double_value)); @@ -178,7 +178,7 @@ TEST(ValuesUtilTest, PopIntArray) { // Pop an int32 array. dbus::MessageReader reader(response.get()); scoped_ptr<Value> value(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(list_value)); } @@ -201,7 +201,7 @@ TEST(ValuesUtilTest, PopStringArray) { // Pop a string array. dbus::MessageReader reader(response.get()); scoped_ptr<Value> value(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(list_value)); } @@ -231,7 +231,7 @@ TEST(ValuesUtilTest, PopStruct) { // Pop a struct. dbus::MessageReader reader(response.get()); scoped_ptr<Value> value(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(&list_value)); } @@ -278,7 +278,7 @@ TEST(ValuesUtilTest, PopStringToVariantDictionary) { // Pop a dictinoary. dbus::MessageReader reader(response.get()); scoped_ptr<Value> value(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(&dictionary_value)); } @@ -321,7 +321,7 @@ TEST(ValuesUtilTest, PopDictionaryWithDottedStringKey) { // Pop a dictinoary. dbus::MessageReader reader(response.get()); scoped_ptr<Value> value(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(&dictionary_value)); } @@ -360,6 +360,64 @@ TEST(ValuesUtilTest, PopDoubleToIntDictionary) { // Pop a dictionary. dbus::MessageReader reader(response.get()); scoped_ptr<Value> value(dbus::PopDataAsValue(&reader)); - EXPECT_TRUE(value.get() != NULL); + ASSERT_TRUE(value.get() != NULL); EXPECT_TRUE(value->Equals(&dictionary_value)); } + +TEST(ValuesUtilTest, AppendBasicTypes) { + const base::FundamentalValue kBoolValue(false); + const base::FundamentalValue kIntegerValue(42); + const base::FundamentalValue kDoubleValue(4.2); + const base::StringValue kStringValue("string"); + + scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); + dbus::MessageWriter writer(response.get()); + AppendBasicTypeValueData(&writer, kBoolValue); + AppendBasicTypeValueData(&writer, kIntegerValue); + AppendBasicTypeValueData(&writer, kDoubleValue); + AppendBasicTypeValueData(&writer, kStringValue); + + dbus::MessageReader reader(response.get()); + scoped_ptr<Value> value; + value.reset(dbus::PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&kBoolValue)); + value.reset(dbus::PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&kIntegerValue)); + value.reset(dbus::PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&kDoubleValue)); + value.reset(dbus::PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&kStringValue)); +} + +TEST(ValuesUtilTest, AppendBasicTypesAsVariant) { + const base::FundamentalValue kBoolValue(false); + const base::FundamentalValue kIntegerValue(42); + const base::FundamentalValue kDoubleValue(4.2); + const base::StringValue kStringValue("string"); + + scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); + dbus::MessageWriter writer(response.get()); + AppendBasicTypeValueDataAsVariant(&writer, kBoolValue); + AppendBasicTypeValueDataAsVariant(&writer, kIntegerValue); + AppendBasicTypeValueDataAsVariant(&writer, kDoubleValue); + AppendBasicTypeValueDataAsVariant(&writer, kStringValue); + + dbus::MessageReader reader(response.get()); + scoped_ptr<Value> value; + value.reset(dbus::PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&kBoolValue)); + value.reset(dbus::PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&kIntegerValue)); + value.reset(dbus::PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&kDoubleValue)); + value.reset(dbus::PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&kStringValue)); +} |