diff options
author | armansito@chromium.org <armansito@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-22 11:52:55 +0000 |
---|---|---|
committer | armansito@chromium.org <armansito@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-22 11:52:55 +0000 |
commit | a68242f419e2ef9a5167dc59c2caf64c28aee530 (patch) | |
tree | a47aae5ddcdd0bc91205f1c19fa991a5d9db45fd /dbus/values_util_unittest.cc | |
parent | 9c497f14121c6ee2aed0ea9edfbf73e7faa455be (diff) | |
download | chromium_src-a68242f419e2ef9a5167dc59c2caf64c28aee530.zip chromium_src-a68242f419e2ef9a5167dc59c2caf64c28aee530.tar.gz chromium_src-a68242f419e2ef9a5167dc59c2caf64c28aee530.tar.bz2 |
dbus/values_util.h: Add functions to append collection type values to message.
Added functions AppendValueData and AppendValueDataAsVariant to values_util.h.
These functions are useful for writing both basic types and variant lists and
dictionaries to a message writer. Lists and dictionaries are written with type
"av" and "a{sv}", respectively.
BUG=359413
TEST=dbus_unittests,chromeos_unittests
Review URL: https://codereview.chromium.org/221393004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@272167 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'dbus/values_util_unittest.cc')
-rw-r--r-- | dbus/values_util_unittest.cc | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/dbus/values_util_unittest.cc b/dbus/values_util_unittest.cc index 2de12d7..336d771 100644 --- a/dbus/values_util_unittest.cc +++ b/dbus/values_util_unittest.cc @@ -449,4 +449,242 @@ TEST(ValuesUtilTest, AppendBasicTypesAsVariant) { EXPECT_TRUE(value->Equals(&kStringValue)); } +TEST(ValuesUtilTest, AppendValueDataBasicTypes) { + const base::FundamentalValue kBoolValue(false); + const base::FundamentalValue kIntegerValue(42); + const base::FundamentalValue kDoubleValue(4.2); + const base::StringValue kStringValue("string"); + + scoped_ptr<Response> response(Response::CreateEmpty()); + MessageWriter writer(response.get()); + AppendValueData(&writer, kBoolValue); + AppendValueData(&writer, kIntegerValue); + AppendValueData(&writer, kDoubleValue); + AppendValueData(&writer, kStringValue); + + MessageReader reader(response.get()); + scoped_ptr<base::Value> value; + value.reset(PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&kBoolValue)); + value.reset(PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&kIntegerValue)); + value.reset(PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&kDoubleValue)); + value.reset(PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&kStringValue)); +} + +TEST(ValuesUtilTest, AppendValueDataAsVariantBasicTypes) { + const base::FundamentalValue kBoolValue(false); + const base::FundamentalValue kIntegerValue(42); + const base::FundamentalValue kDoubleValue(4.2); + const base::StringValue kStringValue("string"); + + scoped_ptr<Response> response(Response::CreateEmpty()); + MessageWriter writer(response.get()); + AppendValueDataAsVariant(&writer, kBoolValue); + AppendValueDataAsVariant(&writer, kIntegerValue); + AppendValueDataAsVariant(&writer, kDoubleValue); + AppendValueDataAsVariant(&writer, kStringValue); + + MessageReader reader(response.get()); + scoped_ptr<base::Value> value; + value.reset(PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&kBoolValue)); + value.reset(PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&kIntegerValue)); + value.reset(PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&kDoubleValue)); + value.reset(PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&kStringValue)); +} + +TEST(ValuesUtilTest, AppendDictionary) { + // Set up the input dictionary. + const std::string kKey1 = "one"; + const std::string kKey2 = "two"; + const std::string kKey3 = "three"; + const std::string kKey4 = "four"; + const std::string kKey5 = "five"; + const std::string kKey6 = "six"; + + const bool kBoolValue = true; + const int32 kInt32Value = -45; + const double kDoubleValue = 4.9; + const std::string kStringValue = "fifty"; + + base::ListValue* list_value = new base::ListValue(); + list_value->AppendBoolean(kBoolValue); + list_value->AppendInteger(kInt32Value); + + base::DictionaryValue* dictionary_value = new base::DictionaryValue(); + dictionary_value->SetBoolean(kKey1, kBoolValue); + dictionary_value->SetInteger(kKey2, kDoubleValue); + + base::DictionaryValue test_dictionary; + test_dictionary.SetBoolean(kKey1, kBoolValue); + test_dictionary.SetInteger(kKey2, kInt32Value); + test_dictionary.SetDouble(kKey3, kDoubleValue); + test_dictionary.SetString(kKey4, kStringValue); + test_dictionary.Set(kKey5, list_value); // takes ownership + test_dictionary.Set(kKey6, dictionary_value); // takes ownership + + scoped_ptr<Response> response(Response::CreateEmpty()); + MessageWriter writer(response.get()); + AppendValueData(&writer, test_dictionary); + base::FundamentalValue int_value(kInt32Value); + AppendValueData(&writer, int_value); + + // Read the data. + MessageReader reader(response.get()); + scoped_ptr<base::Value> value; + value.reset(PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&test_dictionary)); + value.reset(PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&int_value)); +} + +TEST(ValuesUtilTest, AppendDictionaryAsVariant) { + // Set up the input dictionary. + const std::string kKey1 = "one"; + const std::string kKey2 = "two"; + const std::string kKey3 = "three"; + const std::string kKey4 = "four"; + const std::string kKey5 = "five"; + const std::string kKey6 = "six"; + + const bool kBoolValue = true; + const int32 kInt32Value = -45; + const double kDoubleValue = 4.9; + const std::string kStringValue = "fifty"; + + base::ListValue* list_value = new base::ListValue(); + list_value->AppendBoolean(kBoolValue); + list_value->AppendInteger(kInt32Value); + + base::DictionaryValue* dictionary_value = new base::DictionaryValue(); + dictionary_value->SetBoolean(kKey1, kBoolValue); + dictionary_value->SetInteger(kKey2, kDoubleValue); + + base::DictionaryValue test_dictionary; + test_dictionary.SetBoolean(kKey1, kBoolValue); + test_dictionary.SetInteger(kKey2, kInt32Value); + test_dictionary.SetDouble(kKey3, kDoubleValue); + test_dictionary.SetString(kKey4, kStringValue); + test_dictionary.Set(kKey5, list_value); // takes ownership + test_dictionary.Set(kKey6, dictionary_value); // takes ownership + + scoped_ptr<Response> response(Response::CreateEmpty()); + MessageWriter writer(response.get()); + AppendValueDataAsVariant(&writer, test_dictionary); + base::FundamentalValue int_value(kInt32Value); + AppendValueData(&writer, int_value); + + // Read the data. + MessageReader reader(response.get()); + scoped_ptr<base::Value> value; + value.reset(PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&test_dictionary)); + value.reset(PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&int_value)); +} + +TEST(ValuesUtilTest, AppendList) { + // Set up the input list. + const std::string kKey1 = "one"; + const std::string kKey2 = "two"; + + const bool kBoolValue = true; + const int32 kInt32Value = -45; + const double kDoubleValue = 4.9; + const std::string kStringValue = "fifty"; + + base::ListValue* list_value = new base::ListValue(); + list_value->AppendBoolean(kBoolValue); + list_value->AppendInteger(kInt32Value); + + base::DictionaryValue* dictionary_value = new base::DictionaryValue(); + dictionary_value->SetBoolean(kKey1, kBoolValue); + dictionary_value->SetInteger(kKey2, kDoubleValue); + + base::ListValue test_list; + test_list.AppendBoolean(kBoolValue); + test_list.AppendInteger(kInt32Value); + test_list.AppendDouble(kDoubleValue); + test_list.AppendString(kStringValue); + test_list.Append(list_value); // takes ownership + test_list.Append(dictionary_value); // takes ownership + + scoped_ptr<Response> response(Response::CreateEmpty()); + MessageWriter writer(response.get()); + AppendValueData(&writer, test_list); + base::FundamentalValue int_value(kInt32Value); + AppendValueData(&writer, int_value); + + // Read the data. + MessageReader reader(response.get()); + scoped_ptr<base::Value> value; + value.reset(PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&test_list)); + value.reset(PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&int_value)); +} + +TEST(ValuesUtilTest, AppendListAsVariant) { + // Set up the input list. + const std::string kKey1 = "one"; + const std::string kKey2 = "two"; + + const bool kBoolValue = true; + const int32 kInt32Value = -45; + const double kDoubleValue = 4.9; + const std::string kStringValue = "fifty"; + + base::ListValue* list_value = new base::ListValue(); + list_value->AppendBoolean(kBoolValue); + list_value->AppendInteger(kInt32Value); + + base::DictionaryValue* dictionary_value = new base::DictionaryValue(); + dictionary_value->SetBoolean(kKey1, kBoolValue); + dictionary_value->SetInteger(kKey2, kDoubleValue); + + base::ListValue test_list; + test_list.AppendBoolean(kBoolValue); + test_list.AppendInteger(kInt32Value); + test_list.AppendDouble(kDoubleValue); + test_list.AppendString(kStringValue); + test_list.Append(list_value); // takes ownership + test_list.Append(dictionary_value); // takes ownership + + scoped_ptr<Response> response(Response::CreateEmpty()); + MessageWriter writer(response.get()); + AppendValueDataAsVariant(&writer, test_list); + base::FundamentalValue int_value(kInt32Value); + AppendValueData(&writer, int_value); + + // Read the data. + MessageReader reader(response.get()); + scoped_ptr<base::Value> value; + value.reset(PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&test_list)); + value.reset(PopDataAsValue(&reader)); + ASSERT_TRUE(value.get() != NULL); + EXPECT_TRUE(value->Equals(&int_value)); +} + } // namespace dbus |