OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "dbus/values_util.h" | 5 #include "dbus/values_util.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/float_util.h" | 9 #include "base/float_util.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 writer.AppendDouble(kDoubleValue); | 37 writer.AppendDouble(kDoubleValue); |
38 const std::string kStringValue = "fifty"; | 38 const std::string kStringValue = "fifty"; |
39 writer.AppendString(kStringValue); | 39 writer.AppendString(kStringValue); |
40 const std::string kEmptyStringValue; | 40 const std::string kEmptyStringValue; |
41 writer.AppendString(kEmptyStringValue); | 41 writer.AppendString(kEmptyStringValue); |
42 const dbus::ObjectPath kObjectPathValue("ObjectPath"); | 42 const dbus::ObjectPath kObjectPathValue("ObjectPath"); |
43 writer.AppendObjectPath(kObjectPathValue); | 43 writer.AppendObjectPath(kObjectPathValue); |
44 | 44 |
45 dbus::MessageReader reader(response.get()); | 45 dbus::MessageReader reader(response.get()); |
46 scoped_ptr<Value> value; | 46 scoped_ptr<Value> value; |
| 47 scoped_ptr<Value> expected_value; |
47 // Pop a byte. | 48 // Pop a byte. |
48 value.reset(dbus::PopDataAsValue(&reader)); | 49 value.reset(dbus::PopDataAsValue(&reader)); |
49 ASSERT_TRUE(value.get() != NULL); | 50 ASSERT_TRUE(value.get() != NULL); |
50 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(kByteValue))); | 51 expected_value.reset(Value::CreateIntegerValue(kByteValue)); |
| 52 EXPECT_TRUE(value->Equals(expected_value.get())); |
51 // Pop a bool. | 53 // Pop a bool. |
52 value.reset(dbus::PopDataAsValue(&reader)); | 54 value.reset(dbus::PopDataAsValue(&reader)); |
53 ASSERT_TRUE(value.get() != NULL); | 55 ASSERT_TRUE(value.get() != NULL); |
54 EXPECT_TRUE(value->Equals(Value::CreateBooleanValue(kBoolValue))); | 56 expected_value.reset(Value::CreateBooleanValue(kBoolValue)); |
| 57 EXPECT_TRUE(value->Equals(expected_value.get())); |
55 // Pop an int16. | 58 // Pop an int16. |
56 value.reset(dbus::PopDataAsValue(&reader)); | 59 value.reset(dbus::PopDataAsValue(&reader)); |
57 ASSERT_TRUE(value.get() != NULL); | 60 ASSERT_TRUE(value.get() != NULL); |
58 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(kInt16Value))); | 61 expected_value.reset(Value::CreateIntegerValue(kInt16Value)); |
| 62 EXPECT_TRUE(value->Equals(expected_value.get())); |
59 // Pop a uint16. | 63 // Pop a uint16. |
60 value.reset(dbus::PopDataAsValue(&reader)); | 64 value.reset(dbus::PopDataAsValue(&reader)); |
61 ASSERT_TRUE(value.get() != NULL); | 65 ASSERT_TRUE(value.get() != NULL); |
62 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(kUint16Value))); | 66 expected_value.reset(Value::CreateIntegerValue(kUint16Value)); |
| 67 EXPECT_TRUE(value->Equals(expected_value.get())); |
63 // Pop an int32. | 68 // Pop an int32. |
64 value.reset(dbus::PopDataAsValue(&reader)); | 69 value.reset(dbus::PopDataAsValue(&reader)); |
65 ASSERT_TRUE(value.get() != NULL); | 70 ASSERT_TRUE(value.get() != NULL); |
66 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(kInt32Value))); | 71 expected_value.reset(Value::CreateIntegerValue(kInt32Value)); |
| 72 EXPECT_TRUE(value->Equals(expected_value.get())); |
67 // Pop a uint32. | 73 // Pop a uint32. |
68 value.reset(dbus::PopDataAsValue(&reader)); | 74 value.reset(dbus::PopDataAsValue(&reader)); |
69 ASSERT_TRUE(value.get() != NULL); | 75 ASSERT_TRUE(value.get() != NULL); |
70 EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kUint32Value))); | 76 expected_value.reset(Value::CreateDoubleValue(kUint32Value)); |
| 77 EXPECT_TRUE(value->Equals(expected_value.get())); |
71 // Pop an int64. | 78 // Pop an int64. |
72 value.reset(dbus::PopDataAsValue(&reader)); | 79 value.reset(dbus::PopDataAsValue(&reader)); |
73 ASSERT_TRUE(value.get() != NULL); | 80 ASSERT_TRUE(value.get() != NULL); |
74 EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kInt64Value))); | 81 expected_value.reset(Value::CreateDoubleValue(kInt64Value)); |
| 82 EXPECT_TRUE(value->Equals(expected_value.get())); |
75 // Pop a uint64. | 83 // Pop a uint64. |
76 value.reset(dbus::PopDataAsValue(&reader)); | 84 value.reset(dbus::PopDataAsValue(&reader)); |
77 ASSERT_TRUE(value.get() != NULL); | 85 ASSERT_TRUE(value.get() != NULL); |
78 EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kUint64Value))); | 86 expected_value.reset(Value::CreateDoubleValue(kUint64Value)); |
| 87 EXPECT_TRUE(value->Equals(expected_value.get())); |
79 // Pop a double. | 88 // Pop a double. |
80 value.reset(dbus::PopDataAsValue(&reader)); | 89 value.reset(dbus::PopDataAsValue(&reader)); |
81 ASSERT_TRUE(value.get() != NULL); | 90 ASSERT_TRUE(value.get() != NULL); |
82 EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kDoubleValue))); | 91 expected_value.reset(Value::CreateDoubleValue(kDoubleValue)); |
| 92 EXPECT_TRUE(value->Equals(expected_value.get())); |
83 // Pop a string. | 93 // Pop a string. |
84 value.reset(dbus::PopDataAsValue(&reader)); | 94 value.reset(dbus::PopDataAsValue(&reader)); |
85 ASSERT_TRUE(value.get() != NULL); | 95 ASSERT_TRUE(value.get() != NULL); |
86 EXPECT_TRUE(value->Equals(Value::CreateStringValue(kStringValue))); | 96 expected_value.reset(Value::CreateStringValue(kStringValue)); |
| 97 EXPECT_TRUE(value->Equals(expected_value.get())); |
87 // Pop an empty string. | 98 // Pop an empty string. |
88 value.reset(dbus::PopDataAsValue(&reader)); | 99 value.reset(dbus::PopDataAsValue(&reader)); |
89 ASSERT_TRUE(value.get() != NULL); | 100 ASSERT_TRUE(value.get() != NULL); |
90 EXPECT_TRUE(value->Equals(Value::CreateStringValue(kEmptyStringValue))); | 101 expected_value.reset(Value::CreateStringValue(kEmptyStringValue)); |
| 102 EXPECT_TRUE(value->Equals(expected_value.get())); |
91 // Pop an object path. | 103 // Pop an object path. |
92 value.reset(dbus::PopDataAsValue(&reader)); | 104 value.reset(dbus::PopDataAsValue(&reader)); |
93 ASSERT_TRUE(value.get() != NULL); | 105 ASSERT_TRUE(value.get() != NULL); |
94 EXPECT_TRUE( | 106 expected_value.reset(Value::CreateStringValue( |
95 value->Equals(Value::CreateStringValue(kObjectPathValue.value()))); | 107 kObjectPathValue.value())); |
| 108 EXPECT_TRUE(value->Equals(expected_value.get())); |
96 } | 109 } |
97 | 110 |
98 TEST(ValuesUtilTest, PopVariant) { | 111 TEST(ValuesUtilTest, PopVariant) { |
99 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 112 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
100 // Append variant values. | 113 // Append variant values. |
101 dbus::MessageWriter writer(response.get()); | 114 dbus::MessageWriter writer(response.get()); |
102 const bool kBoolValue = true; | 115 const bool kBoolValue = true; |
103 writer.AppendVariantOfBool(kBoolValue); | 116 writer.AppendVariantOfBool(kBoolValue); |
104 const int32 kInt32Value = -45; | 117 const int32 kInt32Value = -45; |
105 writer.AppendVariantOfInt32(kInt32Value); | 118 writer.AppendVariantOfInt32(kInt32Value); |
106 const double kDoubleValue = 4.9; | 119 const double kDoubleValue = 4.9; |
107 writer.AppendVariantOfDouble(kDoubleValue); | 120 writer.AppendVariantOfDouble(kDoubleValue); |
108 const std::string kStringValue = "fifty"; | 121 const std::string kStringValue = "fifty"; |
109 writer.AppendVariantOfString(kStringValue); | 122 writer.AppendVariantOfString(kStringValue); |
110 | 123 |
111 dbus::MessageReader reader(response.get()); | 124 dbus::MessageReader reader(response.get()); |
112 scoped_ptr<Value> value; | 125 scoped_ptr<Value> value; |
| 126 scoped_ptr<Value> expected_value; |
113 // Pop a bool. | 127 // Pop a bool. |
114 value.reset(dbus::PopDataAsValue(&reader)); | 128 value.reset(dbus::PopDataAsValue(&reader)); |
115 ASSERT_TRUE(value.get() != NULL); | 129 ASSERT_TRUE(value.get() != NULL); |
116 EXPECT_TRUE(value->Equals(Value::CreateBooleanValue(kBoolValue))); | 130 expected_value.reset(Value::CreateBooleanValue(kBoolValue)); |
| 131 EXPECT_TRUE(value->Equals(expected_value.get())); |
117 // Pop an int32. | 132 // Pop an int32. |
118 value.reset(dbus::PopDataAsValue(&reader)); | 133 value.reset(dbus::PopDataAsValue(&reader)); |
119 ASSERT_TRUE(value.get() != NULL); | 134 ASSERT_TRUE(value.get() != NULL); |
120 EXPECT_TRUE(value->Equals(Value::CreateIntegerValue(kInt32Value))); | 135 expected_value.reset(Value::CreateIntegerValue(kInt32Value)); |
| 136 EXPECT_TRUE(value->Equals(expected_value.get())); |
121 // Pop a double. | 137 // Pop a double. |
122 value.reset(dbus::PopDataAsValue(&reader)); | 138 value.reset(dbus::PopDataAsValue(&reader)); |
123 ASSERT_TRUE(value.get() != NULL); | 139 ASSERT_TRUE(value.get() != NULL); |
124 EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kDoubleValue))); | 140 expected_value.reset(Value::CreateDoubleValue(kDoubleValue)); |
| 141 EXPECT_TRUE(value->Equals(expected_value.get())); |
125 // Pop a string. | 142 // Pop a string. |
126 value.reset(dbus::PopDataAsValue(&reader)); | 143 value.reset(dbus::PopDataAsValue(&reader)); |
127 ASSERT_TRUE(value.get() != NULL); | 144 ASSERT_TRUE(value.get() != NULL); |
128 EXPECT_TRUE(value->Equals(Value::CreateStringValue(kStringValue))); | 145 expected_value.reset(Value::CreateStringValue(kStringValue)); |
| 146 EXPECT_TRUE(value->Equals(expected_value.get())); |
129 } | 147 } |
130 | 148 |
131 // Pop extremely large integers which cannot be precisely represented in | 149 // Pop extremely large integers which cannot be precisely represented in |
132 // double. | 150 // double. |
133 TEST(ValuesUtilTest, PopExtremelyLargeIntegers) { | 151 TEST(ValuesUtilTest, PopExtremelyLargeIntegers) { |
134 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 152 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
135 // Append large integers. | 153 // Append large integers. |
136 dbus::MessageWriter writer(response.get()); | 154 dbus::MessageWriter writer(response.get()); |
137 const int64 kInt64Value = -123456789012345689LL; | 155 const int64 kInt64Value = -123456789012345689LL; |
138 writer.AppendInt64(kInt64Value); | 156 writer.AppendInt64(kInt64Value); |
139 const uint64 kUint64Value = 9876543210987654321ULL; | 157 const uint64 kUint64Value = 9876543210987654321ULL; |
140 writer.AppendUint64(kUint64Value); | 158 writer.AppendUint64(kUint64Value); |
141 | 159 |
142 dbus::MessageReader reader(response.get()); | 160 dbus::MessageReader reader(response.get()); |
143 scoped_ptr<Value> value; | 161 scoped_ptr<Value> value; |
| 162 scoped_ptr<Value> expected_value; |
144 double double_value = 0; | 163 double double_value = 0; |
145 // Pop an int64. | 164 // Pop an int64. |
146 value.reset(dbus::PopDataAsValue(&reader)); | 165 value.reset(dbus::PopDataAsValue(&reader)); |
147 ASSERT_TRUE(value.get() != NULL); | 166 ASSERT_TRUE(value.get() != NULL); |
148 EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kInt64Value))); | 167 expected_value.reset(Value::CreateDoubleValue(kInt64Value)); |
| 168 EXPECT_TRUE(value->Equals(expected_value.get())); |
149 ASSERT_TRUE(value->GetAsDouble(&double_value)); | 169 ASSERT_TRUE(value->GetAsDouble(&double_value)); |
150 EXPECT_NE(kInt64Value, static_cast<int64>(double_value)); | 170 EXPECT_NE(kInt64Value, static_cast<int64>(double_value)); |
151 // Pop a uint64. | 171 // Pop a uint64. |
152 value.reset(dbus::PopDataAsValue(&reader)); | 172 value.reset(dbus::PopDataAsValue(&reader)); |
153 ASSERT_TRUE(value.get() != NULL); | 173 ASSERT_TRUE(value.get() != NULL); |
154 EXPECT_TRUE(value->Equals(Value::CreateDoubleValue(kUint64Value))); | 174 expected_value.reset(Value::CreateDoubleValue(kUint64Value)); |
| 175 EXPECT_TRUE(value->Equals(expected_value.get())); |
155 ASSERT_TRUE(value->GetAsDouble(&double_value)); | 176 ASSERT_TRUE(value->GetAsDouble(&double_value)); |
156 EXPECT_NE(kUint64Value, static_cast<uint64>(double_value)); | 177 EXPECT_NE(kUint64Value, static_cast<uint64>(double_value)); |
157 } | 178 } |
158 | 179 |
159 TEST(ValuesUtilTest, PopIntArray) { | 180 TEST(ValuesUtilTest, PopIntArray) { |
160 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 181 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
161 // Append an int32 array. | 182 // Append an int32 array. |
162 dbus::MessageWriter writer(response.get()); | 183 dbus::MessageWriter writer(response.get()); |
163 dbus::MessageWriter sub_writer(NULL); | 184 dbus::MessageWriter sub_writer(NULL); |
164 std::vector<int32> data; | 185 std::vector<int32> data; |
165 data.push_back(0); | 186 data.push_back(0); |
166 data.push_back(1); | 187 data.push_back(1); |
167 data.push_back(2); | 188 data.push_back(2); |
168 writer.OpenArray("i", &sub_writer); | 189 writer.OpenArray("i", &sub_writer); |
169 for (size_t i = 0; i != data.size(); ++i) | 190 for (size_t i = 0; i != data.size(); ++i) |
170 sub_writer.AppendInt32(data[i]); | 191 sub_writer.AppendInt32(data[i]); |
171 writer.CloseContainer(&sub_writer); | 192 writer.CloseContainer(&sub_writer); |
172 | 193 |
173 // Create the expected value. | 194 // Create the expected value. |
174 ListValue* list_value = new ListValue; | 195 scoped_ptr<ListValue> list_value(new ListValue); |
175 for (size_t i = 0; i != data.size(); ++i) | 196 for (size_t i = 0; i != data.size(); ++i) |
176 list_value->Append(Value::CreateIntegerValue(data[i])); | 197 list_value->Append(Value::CreateIntegerValue(data[i])); |
177 | 198 |
178 // Pop an int32 array. | 199 // Pop an int32 array. |
179 dbus::MessageReader reader(response.get()); | 200 dbus::MessageReader reader(response.get()); |
180 scoped_ptr<Value> value(dbus::PopDataAsValue(&reader)); | 201 scoped_ptr<Value> value(dbus::PopDataAsValue(&reader)); |
181 ASSERT_TRUE(value.get() != NULL); | 202 ASSERT_TRUE(value.get() != NULL); |
182 EXPECT_TRUE(value->Equals(list_value)); | 203 EXPECT_TRUE(value->Equals(list_value.get())); |
183 } | 204 } |
184 | 205 |
185 TEST(ValuesUtilTest, PopStringArray) { | 206 TEST(ValuesUtilTest, PopStringArray) { |
186 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 207 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
187 // Append a string array. | 208 // Append a string array. |
188 dbus::MessageWriter writer(response.get()); | 209 dbus::MessageWriter writer(response.get()); |
189 dbus::MessageWriter sub_writer(NULL); | 210 dbus::MessageWriter sub_writer(NULL); |
190 std::vector<std::string> data; | 211 std::vector<std::string> data; |
191 data.push_back("Dreamlifter"); | 212 data.push_back("Dreamlifter"); |
192 data.push_back("Beluga"); | 213 data.push_back("Beluga"); |
193 data.push_back("Mriya"); | 214 data.push_back("Mriya"); |
194 writer.AppendArrayOfStrings(data); | 215 writer.AppendArrayOfStrings(data); |
195 | 216 |
196 // Create the expected value. | 217 // Create the expected value. |
197 ListValue* list_value = new ListValue; | 218 scoped_ptr<ListValue> list_value(new ListValue); |
198 for (size_t i = 0; i != data.size(); ++i) | 219 for (size_t i = 0; i != data.size(); ++i) |
199 list_value->Append(Value::CreateStringValue(data[i])); | 220 list_value->Append(Value::CreateStringValue(data[i])); |
200 | 221 |
201 // Pop a string array. | 222 // Pop a string array. |
202 dbus::MessageReader reader(response.get()); | 223 dbus::MessageReader reader(response.get()); |
203 scoped_ptr<Value> value(dbus::PopDataAsValue(&reader)); | 224 scoped_ptr<Value> value(dbus::PopDataAsValue(&reader)); |
204 ASSERT_TRUE(value.get() != NULL); | 225 ASSERT_TRUE(value.get() != NULL); |
205 EXPECT_TRUE(value->Equals(list_value)); | 226 EXPECT_TRUE(value->Equals(list_value.get())); |
206 } | 227 } |
207 | 228 |
208 TEST(ValuesUtilTest, PopStruct) { | 229 TEST(ValuesUtilTest, PopStruct) { |
209 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); | 230 scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty()); |
210 // Append a struct. | 231 // Append a struct. |
211 dbus::MessageWriter writer(response.get()); | 232 dbus::MessageWriter writer(response.get()); |
212 dbus::MessageWriter sub_writer(NULL); | 233 dbus::MessageWriter sub_writer(NULL); |
213 writer.OpenStruct(&sub_writer); | 234 writer.OpenStruct(&sub_writer); |
214 const bool kBoolValue = true; | 235 const bool kBoolValue = true; |
215 sub_writer.AppendBool(kBoolValue); | 236 sub_writer.AppendBool(kBoolValue); |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
414 value.reset(dbus::PopDataAsValue(&reader)); | 435 value.reset(dbus::PopDataAsValue(&reader)); |
415 ASSERT_TRUE(value.get() != NULL); | 436 ASSERT_TRUE(value.get() != NULL); |
416 EXPECT_TRUE(value->Equals(&kIntegerValue)); | 437 EXPECT_TRUE(value->Equals(&kIntegerValue)); |
417 value.reset(dbus::PopDataAsValue(&reader)); | 438 value.reset(dbus::PopDataAsValue(&reader)); |
418 ASSERT_TRUE(value.get() != NULL); | 439 ASSERT_TRUE(value.get() != NULL); |
419 EXPECT_TRUE(value->Equals(&kDoubleValue)); | 440 EXPECT_TRUE(value->Equals(&kDoubleValue)); |
420 value.reset(dbus::PopDataAsValue(&reader)); | 441 value.reset(dbus::PopDataAsValue(&reader)); |
421 ASSERT_TRUE(value.get() != NULL); | 442 ASSERT_TRUE(value.get() != NULL); |
422 EXPECT_TRUE(value->Equals(&kStringValue)); | 443 EXPECT_TRUE(value->Equals(&kStringValue)); |
423 } | 444 } |
OLD | NEW |