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