| 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 <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 7 #include <cmath> | 10 #include <cmath> |
| 8 #include <vector> | 11 #include <vector> |
| 9 | 12 |
| 10 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
| 14 #include "base/macros.h" |
| 11 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/values.h" | 16 #include "base/values.h" |
| 13 #include "dbus/message.h" | 17 #include "dbus/message.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 19 |
| 16 namespace dbus { | 20 namespace dbus { |
| 17 | 21 |
| 18 TEST(ValuesUtilTest, PopBasicTypes) { | 22 TEST(ValuesUtilTest, PopBasicTypes) { |
| 19 scoped_ptr<Response> response(Response::CreateEmpty()); | 23 scoped_ptr<Response> response(Response::CreateEmpty()); |
| 20 // Append basic type values. | 24 // Append basic type values. |
| 21 MessageWriter writer(response.get()); | 25 MessageWriter writer(response.get()); |
| 22 const uint8 kByteValue = 42; | 26 const uint8_t kByteValue = 42; |
| 23 writer.AppendByte(kByteValue); | 27 writer.AppendByte(kByteValue); |
| 24 const bool kBoolValue = true; | 28 const bool kBoolValue = true; |
| 25 writer.AppendBool(kBoolValue); | 29 writer.AppendBool(kBoolValue); |
| 26 const int16 kInt16Value = -43; | 30 const int16_t kInt16Value = -43; |
| 27 writer.AppendInt16(kInt16Value); | 31 writer.AppendInt16(kInt16Value); |
| 28 const uint16 kUint16Value = 44; | 32 const uint16_t kUint16Value = 44; |
| 29 writer.AppendUint16(kUint16Value); | 33 writer.AppendUint16(kUint16Value); |
| 30 const int32 kInt32Value = -45; | 34 const int32_t kInt32Value = -45; |
| 31 writer.AppendInt32(kInt32Value); | 35 writer.AppendInt32(kInt32Value); |
| 32 const uint32 kUint32Value = 46; | 36 const uint32_t kUint32Value = 46; |
| 33 writer.AppendUint32(kUint32Value); | 37 writer.AppendUint32(kUint32Value); |
| 34 const int64 kInt64Value = -47; | 38 const int64_t kInt64Value = -47; |
| 35 writer.AppendInt64(kInt64Value); | 39 writer.AppendInt64(kInt64Value); |
| 36 const uint64 kUint64Value = 48; | 40 const uint64_t kUint64Value = 48; |
| 37 writer.AppendUint64(kUint64Value); | 41 writer.AppendUint64(kUint64Value); |
| 38 const double kDoubleValue = 4.9; | 42 const double kDoubleValue = 4.9; |
| 39 writer.AppendDouble(kDoubleValue); | 43 writer.AppendDouble(kDoubleValue); |
| 40 const std::string kStringValue = "fifty"; | 44 const std::string kStringValue = "fifty"; |
| 41 writer.AppendString(kStringValue); | 45 writer.AppendString(kStringValue); |
| 42 const std::string kEmptyStringValue; | 46 const std::string kEmptyStringValue; |
| 43 writer.AppendString(kEmptyStringValue); | 47 writer.AppendString(kEmptyStringValue); |
| 44 const ObjectPath kObjectPathValue("/ObjectPath"); | 48 const ObjectPath kObjectPathValue("/ObjectPath"); |
| 45 writer.AppendObjectPath(kObjectPathValue); | 49 writer.AppendObjectPath(kObjectPathValue); |
| 46 | 50 |
| 47 MessageReader reader(response.get()); | 51 MessageReader reader(response.get()); |
| 48 scoped_ptr<base::Value> value; | 52 scoped_ptr<base::Value> value; |
| 49 scoped_ptr<base::Value> expected_value; | 53 scoped_ptr<base::Value> expected_value; |
| 50 // Pop a byte. | 54 // Pop a byte. |
| 51 value.reset(PopDataAsValue(&reader)); | 55 value.reset(PopDataAsValue(&reader)); |
| 52 ASSERT_TRUE(value.get() != NULL); | 56 ASSERT_TRUE(value.get() != NULL); |
| 53 expected_value.reset(new base::FundamentalValue(kByteValue)); | 57 expected_value.reset(new base::FundamentalValue(kByteValue)); |
| 54 EXPECT_TRUE(value->Equals(expected_value.get())); | 58 EXPECT_TRUE(value->Equals(expected_value.get())); |
| 55 // Pop a bool. | 59 // Pop a bool. |
| 56 value.reset(PopDataAsValue(&reader)); | 60 value.reset(PopDataAsValue(&reader)); |
| 57 ASSERT_TRUE(value.get() != NULL); | 61 ASSERT_TRUE(value.get() != NULL); |
| 58 expected_value.reset(new base::FundamentalValue(kBoolValue)); | 62 expected_value.reset(new base::FundamentalValue(kBoolValue)); |
| 59 EXPECT_TRUE(value->Equals(expected_value.get())); | 63 EXPECT_TRUE(value->Equals(expected_value.get())); |
| 60 // Pop an int16. | 64 // Pop an int16_t. |
| 61 value.reset(PopDataAsValue(&reader)); | 65 value.reset(PopDataAsValue(&reader)); |
| 62 ASSERT_TRUE(value.get() != NULL); | 66 ASSERT_TRUE(value.get() != NULL); |
| 63 expected_value.reset(new base::FundamentalValue(kInt16Value)); | 67 expected_value.reset(new base::FundamentalValue(kInt16Value)); |
| 64 EXPECT_TRUE(value->Equals(expected_value.get())); | 68 EXPECT_TRUE(value->Equals(expected_value.get())); |
| 65 // Pop a uint16. | 69 // Pop a uint16_t. |
| 66 value.reset(PopDataAsValue(&reader)); | 70 value.reset(PopDataAsValue(&reader)); |
| 67 ASSERT_TRUE(value.get() != NULL); | 71 ASSERT_TRUE(value.get() != NULL); |
| 68 expected_value.reset(new base::FundamentalValue(kUint16Value)); | 72 expected_value.reset(new base::FundamentalValue(kUint16Value)); |
| 69 EXPECT_TRUE(value->Equals(expected_value.get())); | 73 EXPECT_TRUE(value->Equals(expected_value.get())); |
| 70 // Pop an int32. | 74 // Pop an int32_t. |
| 71 value.reset(PopDataAsValue(&reader)); | 75 value.reset(PopDataAsValue(&reader)); |
| 72 ASSERT_TRUE(value.get() != NULL); | 76 ASSERT_TRUE(value.get() != NULL); |
| 73 expected_value.reset(new base::FundamentalValue(kInt32Value)); | 77 expected_value.reset(new base::FundamentalValue(kInt32Value)); |
| 74 EXPECT_TRUE(value->Equals(expected_value.get())); | 78 EXPECT_TRUE(value->Equals(expected_value.get())); |
| 75 // Pop a uint32. | 79 // Pop a uint32_t. |
| 76 value.reset(PopDataAsValue(&reader)); | 80 value.reset(PopDataAsValue(&reader)); |
| 77 ASSERT_TRUE(value.get() != NULL); | 81 ASSERT_TRUE(value.get() != NULL); |
| 78 expected_value.reset( | 82 expected_value.reset( |
| 79 new base::FundamentalValue(static_cast<double>(kUint32Value))); | 83 new base::FundamentalValue(static_cast<double>(kUint32Value))); |
| 80 EXPECT_TRUE(value->Equals(expected_value.get())); | 84 EXPECT_TRUE(value->Equals(expected_value.get())); |
| 81 // Pop an int64. | 85 // Pop an int64_t. |
| 82 value.reset(PopDataAsValue(&reader)); | 86 value.reset(PopDataAsValue(&reader)); |
| 83 ASSERT_TRUE(value.get() != NULL); | 87 ASSERT_TRUE(value.get() != NULL); |
| 84 expected_value.reset( | 88 expected_value.reset( |
| 85 new base::FundamentalValue(static_cast<double>(kInt64Value))); | 89 new base::FundamentalValue(static_cast<double>(kInt64Value))); |
| 86 EXPECT_TRUE(value->Equals(expected_value.get())); | 90 EXPECT_TRUE(value->Equals(expected_value.get())); |
| 87 // Pop a uint64. | 91 // Pop a uint64_t. |
| 88 value.reset(PopDataAsValue(&reader)); | 92 value.reset(PopDataAsValue(&reader)); |
| 89 ASSERT_TRUE(value.get() != NULL); | 93 ASSERT_TRUE(value.get() != NULL); |
| 90 expected_value.reset( | 94 expected_value.reset( |
| 91 new base::FundamentalValue(static_cast<double>(kUint64Value))); | 95 new base::FundamentalValue(static_cast<double>(kUint64Value))); |
| 92 EXPECT_TRUE(value->Equals(expected_value.get())); | 96 EXPECT_TRUE(value->Equals(expected_value.get())); |
| 93 // Pop a double. | 97 // Pop a double. |
| 94 value.reset(PopDataAsValue(&reader)); | 98 value.reset(PopDataAsValue(&reader)); |
| 95 ASSERT_TRUE(value.get() != NULL); | 99 ASSERT_TRUE(value.get() != NULL); |
| 96 expected_value.reset(new base::FundamentalValue(kDoubleValue)); | 100 expected_value.reset(new base::FundamentalValue(kDoubleValue)); |
| 97 EXPECT_TRUE(value->Equals(expected_value.get())); | 101 EXPECT_TRUE(value->Equals(expected_value.get())); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 111 expected_value.reset(new base::StringValue(kObjectPathValue.value())); | 115 expected_value.reset(new base::StringValue(kObjectPathValue.value())); |
| 112 EXPECT_TRUE(value->Equals(expected_value.get())); | 116 EXPECT_TRUE(value->Equals(expected_value.get())); |
| 113 } | 117 } |
| 114 | 118 |
| 115 TEST(ValuesUtilTest, PopVariant) { | 119 TEST(ValuesUtilTest, PopVariant) { |
| 116 scoped_ptr<Response> response(Response::CreateEmpty()); | 120 scoped_ptr<Response> response(Response::CreateEmpty()); |
| 117 // Append variant values. | 121 // Append variant values. |
| 118 MessageWriter writer(response.get()); | 122 MessageWriter writer(response.get()); |
| 119 const bool kBoolValue = true; | 123 const bool kBoolValue = true; |
| 120 writer.AppendVariantOfBool(kBoolValue); | 124 writer.AppendVariantOfBool(kBoolValue); |
| 121 const int32 kInt32Value = -45; | 125 const int32_t kInt32Value = -45; |
| 122 writer.AppendVariantOfInt32(kInt32Value); | 126 writer.AppendVariantOfInt32(kInt32Value); |
| 123 const double kDoubleValue = 4.9; | 127 const double kDoubleValue = 4.9; |
| 124 writer.AppendVariantOfDouble(kDoubleValue); | 128 writer.AppendVariantOfDouble(kDoubleValue); |
| 125 const std::string kStringValue = "fifty"; | 129 const std::string kStringValue = "fifty"; |
| 126 writer.AppendVariantOfString(kStringValue); | 130 writer.AppendVariantOfString(kStringValue); |
| 127 | 131 |
| 128 MessageReader reader(response.get()); | 132 MessageReader reader(response.get()); |
| 129 scoped_ptr<base::Value> value; | 133 scoped_ptr<base::Value> value; |
| 130 scoped_ptr<base::Value> expected_value; | 134 scoped_ptr<base::Value> expected_value; |
| 131 // Pop a bool. | 135 // Pop a bool. |
| 132 value.reset(PopDataAsValue(&reader)); | 136 value.reset(PopDataAsValue(&reader)); |
| 133 ASSERT_TRUE(value.get() != NULL); | 137 ASSERT_TRUE(value.get() != NULL); |
| 134 expected_value.reset(new base::FundamentalValue(kBoolValue)); | 138 expected_value.reset(new base::FundamentalValue(kBoolValue)); |
| 135 EXPECT_TRUE(value->Equals(expected_value.get())); | 139 EXPECT_TRUE(value->Equals(expected_value.get())); |
| 136 // Pop an int32. | 140 // Pop an int32_t. |
| 137 value.reset(PopDataAsValue(&reader)); | 141 value.reset(PopDataAsValue(&reader)); |
| 138 ASSERT_TRUE(value.get() != NULL); | 142 ASSERT_TRUE(value.get() != NULL); |
| 139 expected_value.reset(new base::FundamentalValue(kInt32Value)); | 143 expected_value.reset(new base::FundamentalValue(kInt32Value)); |
| 140 EXPECT_TRUE(value->Equals(expected_value.get())); | 144 EXPECT_TRUE(value->Equals(expected_value.get())); |
| 141 // Pop a double. | 145 // Pop a double. |
| 142 value.reset(PopDataAsValue(&reader)); | 146 value.reset(PopDataAsValue(&reader)); |
| 143 ASSERT_TRUE(value.get() != NULL); | 147 ASSERT_TRUE(value.get() != NULL); |
| 144 expected_value.reset(new base::FundamentalValue(kDoubleValue)); | 148 expected_value.reset(new base::FundamentalValue(kDoubleValue)); |
| 145 EXPECT_TRUE(value->Equals(expected_value.get())); | 149 EXPECT_TRUE(value->Equals(expected_value.get())); |
| 146 // Pop a string. | 150 // Pop a string. |
| 147 value.reset(PopDataAsValue(&reader)); | 151 value.reset(PopDataAsValue(&reader)); |
| 148 ASSERT_TRUE(value.get() != NULL); | 152 ASSERT_TRUE(value.get() != NULL); |
| 149 expected_value.reset(new base::StringValue(kStringValue)); | 153 expected_value.reset(new base::StringValue(kStringValue)); |
| 150 EXPECT_TRUE(value->Equals(expected_value.get())); | 154 EXPECT_TRUE(value->Equals(expected_value.get())); |
| 151 } | 155 } |
| 152 | 156 |
| 153 // Pop extremely large integers which cannot be precisely represented in | 157 // Pop extremely large integers which cannot be precisely represented in |
| 154 // double. | 158 // double. |
| 155 TEST(ValuesUtilTest, PopExtremelyLargeIntegers) { | 159 TEST(ValuesUtilTest, PopExtremelyLargeIntegers) { |
| 156 scoped_ptr<Response> response(Response::CreateEmpty()); | 160 scoped_ptr<Response> response(Response::CreateEmpty()); |
| 157 // Append large integers. | 161 // Append large integers. |
| 158 MessageWriter writer(response.get()); | 162 MessageWriter writer(response.get()); |
| 159 const int64 kInt64Value = -123456789012345689LL; | 163 const int64_t kInt64Value = -123456789012345689LL; |
| 160 writer.AppendInt64(kInt64Value); | 164 writer.AppendInt64(kInt64Value); |
| 161 const uint64 kUint64Value = 9876543210987654321ULL; | 165 const uint64_t kUint64Value = 9876543210987654321ULL; |
| 162 writer.AppendUint64(kUint64Value); | 166 writer.AppendUint64(kUint64Value); |
| 163 | 167 |
| 164 MessageReader reader(response.get()); | 168 MessageReader reader(response.get()); |
| 165 scoped_ptr<base::Value> value; | 169 scoped_ptr<base::Value> value; |
| 166 scoped_ptr<base::Value> expected_value; | 170 scoped_ptr<base::Value> expected_value; |
| 167 double double_value = 0; | 171 double double_value = 0; |
| 168 // Pop an int64. | 172 // Pop an int64_t. |
| 169 value.reset(PopDataAsValue(&reader)); | 173 value.reset(PopDataAsValue(&reader)); |
| 170 ASSERT_TRUE(value.get() != NULL); | 174 ASSERT_TRUE(value.get() != NULL); |
| 171 expected_value.reset( | 175 expected_value.reset( |
| 172 new base::FundamentalValue(static_cast<double>(kInt64Value))); | 176 new base::FundamentalValue(static_cast<double>(kInt64Value))); |
| 173 EXPECT_TRUE(value->Equals(expected_value.get())); | 177 EXPECT_TRUE(value->Equals(expected_value.get())); |
| 174 ASSERT_TRUE(value->GetAsDouble(&double_value)); | 178 ASSERT_TRUE(value->GetAsDouble(&double_value)); |
| 175 EXPECT_NE(kInt64Value, static_cast<int64>(double_value)); | 179 EXPECT_NE(kInt64Value, static_cast<int64_t>(double_value)); |
| 176 // Pop a uint64. | 180 // Pop a uint64_t. |
| 177 value.reset(PopDataAsValue(&reader)); | 181 value.reset(PopDataAsValue(&reader)); |
| 178 ASSERT_TRUE(value.get() != NULL); | 182 ASSERT_TRUE(value.get() != NULL); |
| 179 expected_value.reset( | 183 expected_value.reset( |
| 180 new base::FundamentalValue(static_cast<double>(kUint64Value))); | 184 new base::FundamentalValue(static_cast<double>(kUint64Value))); |
| 181 EXPECT_TRUE(value->Equals(expected_value.get())); | 185 EXPECT_TRUE(value->Equals(expected_value.get())); |
| 182 ASSERT_TRUE(value->GetAsDouble(&double_value)); | 186 ASSERT_TRUE(value->GetAsDouble(&double_value)); |
| 183 EXPECT_NE(kUint64Value, static_cast<uint64>(double_value)); | 187 EXPECT_NE(kUint64Value, static_cast<uint64_t>(double_value)); |
| 184 } | 188 } |
| 185 | 189 |
| 186 TEST(ValuesUtilTest, PopIntArray) { | 190 TEST(ValuesUtilTest, PopIntArray) { |
| 187 scoped_ptr<Response> response(Response::CreateEmpty()); | 191 scoped_ptr<Response> response(Response::CreateEmpty()); |
| 188 // Append an int32 array. | 192 // Append an int32_t array. |
| 189 MessageWriter writer(response.get()); | 193 MessageWriter writer(response.get()); |
| 190 MessageWriter sub_writer(NULL); | 194 MessageWriter sub_writer(NULL); |
| 191 std::vector<int32> data; | 195 std::vector<int32_t> data; |
| 192 data.push_back(0); | 196 data.push_back(0); |
| 193 data.push_back(1); | 197 data.push_back(1); |
| 194 data.push_back(2); | 198 data.push_back(2); |
| 195 writer.OpenArray("i", &sub_writer); | 199 writer.OpenArray("i", &sub_writer); |
| 196 for (size_t i = 0; i != data.size(); ++i) | 200 for (size_t i = 0; i != data.size(); ++i) |
| 197 sub_writer.AppendInt32(data[i]); | 201 sub_writer.AppendInt32(data[i]); |
| 198 writer.CloseContainer(&sub_writer); | 202 writer.CloseContainer(&sub_writer); |
| 199 | 203 |
| 200 // Create the expected value. | 204 // Create the expected value. |
| 201 scoped_ptr<base::ListValue> list_value(new base::ListValue); | 205 scoped_ptr<base::ListValue> list_value(new base::ListValue); |
| 202 for (size_t i = 0; i != data.size(); ++i) | 206 for (size_t i = 0; i != data.size(); ++i) |
| 203 list_value->Append(new base::FundamentalValue(data[i])); | 207 list_value->Append(new base::FundamentalValue(data[i])); |
| 204 | 208 |
| 205 // Pop an int32 array. | 209 // Pop an int32_t array. |
| 206 MessageReader reader(response.get()); | 210 MessageReader reader(response.get()); |
| 207 scoped_ptr<base::Value> value(PopDataAsValue(&reader)); | 211 scoped_ptr<base::Value> value(PopDataAsValue(&reader)); |
| 208 ASSERT_TRUE(value.get() != NULL); | 212 ASSERT_TRUE(value.get() != NULL); |
| 209 EXPECT_TRUE(value->Equals(list_value.get())); | 213 EXPECT_TRUE(value->Equals(list_value.get())); |
| 210 } | 214 } |
| 211 | 215 |
| 212 TEST(ValuesUtilTest, PopStringArray) { | 216 TEST(ValuesUtilTest, PopStringArray) { |
| 213 scoped_ptr<Response> response(Response::CreateEmpty()); | 217 scoped_ptr<Response> response(Response::CreateEmpty()); |
| 214 // Append a string array. | 218 // Append a string array. |
| 215 MessageWriter writer(response.get()); | 219 MessageWriter writer(response.get()); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 233 } | 237 } |
| 234 | 238 |
| 235 TEST(ValuesUtilTest, PopStruct) { | 239 TEST(ValuesUtilTest, PopStruct) { |
| 236 scoped_ptr<Response> response(Response::CreateEmpty()); | 240 scoped_ptr<Response> response(Response::CreateEmpty()); |
| 237 // Append a struct. | 241 // Append a struct. |
| 238 MessageWriter writer(response.get()); | 242 MessageWriter writer(response.get()); |
| 239 MessageWriter sub_writer(NULL); | 243 MessageWriter sub_writer(NULL); |
| 240 writer.OpenStruct(&sub_writer); | 244 writer.OpenStruct(&sub_writer); |
| 241 const bool kBoolValue = true; | 245 const bool kBoolValue = true; |
| 242 sub_writer.AppendBool(kBoolValue); | 246 sub_writer.AppendBool(kBoolValue); |
| 243 const int32 kInt32Value = -123; | 247 const int32_t kInt32Value = -123; |
| 244 sub_writer.AppendInt32(kInt32Value); | 248 sub_writer.AppendInt32(kInt32Value); |
| 245 const double kDoubleValue = 1.23; | 249 const double kDoubleValue = 1.23; |
| 246 sub_writer.AppendDouble(kDoubleValue); | 250 sub_writer.AppendDouble(kDoubleValue); |
| 247 const std::string kStringValue = "one two three"; | 251 const std::string kStringValue = "one two three"; |
| 248 sub_writer.AppendString(kStringValue); | 252 sub_writer.AppendString(kStringValue); |
| 249 writer.CloseContainer(&sub_writer); | 253 writer.CloseContainer(&sub_writer); |
| 250 | 254 |
| 251 // Create the expected value. | 255 // Create the expected value. |
| 252 base::ListValue list_value; | 256 base::ListValue list_value; |
| 253 list_value.Append(new base::FundamentalValue(kBoolValue)); | 257 list_value.Append(new base::FundamentalValue(kBoolValue)); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 271 writer.OpenArray("{sv}", &sub_writer); | 275 writer.OpenArray("{sv}", &sub_writer); |
| 272 sub_writer.OpenDictEntry(&entry_writer); | 276 sub_writer.OpenDictEntry(&entry_writer); |
| 273 const std::string kKey1 = "one"; | 277 const std::string kKey1 = "one"; |
| 274 entry_writer.AppendString(kKey1); | 278 entry_writer.AppendString(kKey1); |
| 275 const bool kBoolValue = true; | 279 const bool kBoolValue = true; |
| 276 entry_writer.AppendVariantOfBool(kBoolValue); | 280 entry_writer.AppendVariantOfBool(kBoolValue); |
| 277 sub_writer.CloseContainer(&entry_writer); | 281 sub_writer.CloseContainer(&entry_writer); |
| 278 sub_writer.OpenDictEntry(&entry_writer); | 282 sub_writer.OpenDictEntry(&entry_writer); |
| 279 const std::string kKey2 = "two"; | 283 const std::string kKey2 = "two"; |
| 280 entry_writer.AppendString(kKey2); | 284 entry_writer.AppendString(kKey2); |
| 281 const int32 kInt32Value = -45; | 285 const int32_t kInt32Value = -45; |
| 282 entry_writer.AppendVariantOfInt32(kInt32Value); | 286 entry_writer.AppendVariantOfInt32(kInt32Value); |
| 283 sub_writer.CloseContainer(&entry_writer); | 287 sub_writer.CloseContainer(&entry_writer); |
| 284 sub_writer.OpenDictEntry(&entry_writer); | 288 sub_writer.OpenDictEntry(&entry_writer); |
| 285 const std::string kKey3 = "three"; | 289 const std::string kKey3 = "three"; |
| 286 entry_writer.AppendString(kKey3); | 290 entry_writer.AppendString(kKey3); |
| 287 const double kDoubleValue = 4.9; | 291 const double kDoubleValue = 4.9; |
| 288 entry_writer.AppendVariantOfDouble(kDoubleValue); | 292 entry_writer.AppendVariantOfDouble(kDoubleValue); |
| 289 sub_writer.CloseContainer(&entry_writer); | 293 sub_writer.CloseContainer(&entry_writer); |
| 290 sub_writer.OpenDictEntry(&entry_writer); | 294 sub_writer.OpenDictEntry(&entry_writer); |
| 291 const std::string kKey4 = "four"; | 295 const std::string kKey4 = "four"; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 318 writer.OpenArray("{sv}", &sub_writer); | 322 writer.OpenArray("{sv}", &sub_writer); |
| 319 sub_writer.OpenDictEntry(&entry_writer); | 323 sub_writer.OpenDictEntry(&entry_writer); |
| 320 const std::string kKey1 = "www.example.com"; // String including dots. | 324 const std::string kKey1 = "www.example.com"; // String including dots. |
| 321 entry_writer.AppendString(kKey1); | 325 entry_writer.AppendString(kKey1); |
| 322 const bool kBoolValue = true; | 326 const bool kBoolValue = true; |
| 323 entry_writer.AppendVariantOfBool(kBoolValue); | 327 entry_writer.AppendVariantOfBool(kBoolValue); |
| 324 sub_writer.CloseContainer(&entry_writer); | 328 sub_writer.CloseContainer(&entry_writer); |
| 325 sub_writer.OpenDictEntry(&entry_writer); | 329 sub_writer.OpenDictEntry(&entry_writer); |
| 326 const std::string kKey2 = ".example"; // String starting with a dot. | 330 const std::string kKey2 = ".example"; // String starting with a dot. |
| 327 entry_writer.AppendString(kKey2); | 331 entry_writer.AppendString(kKey2); |
| 328 const int32 kInt32Value = -45; | 332 const int32_t kInt32Value = -45; |
| 329 entry_writer.AppendVariantOfInt32(kInt32Value); | 333 entry_writer.AppendVariantOfInt32(kInt32Value); |
| 330 sub_writer.CloseContainer(&entry_writer); | 334 sub_writer.CloseContainer(&entry_writer); |
| 331 sub_writer.OpenDictEntry(&entry_writer); | 335 sub_writer.OpenDictEntry(&entry_writer); |
| 332 const std::string kKey3 = "example."; // String ending with a dot. | 336 const std::string kKey3 = "example."; // String ending with a dot. |
| 333 entry_writer.AppendString(kKey3); | 337 entry_writer.AppendString(kKey3); |
| 334 const double kDoubleValue = 4.9; | 338 const double kDoubleValue = 4.9; |
| 335 entry_writer.AppendVariantOfDouble(kDoubleValue); | 339 entry_writer.AppendVariantOfDouble(kDoubleValue); |
| 336 sub_writer.CloseContainer(&entry_writer); | 340 sub_writer.CloseContainer(&entry_writer); |
| 337 writer.CloseContainer(&sub_writer); | 341 writer.CloseContainer(&sub_writer); |
| 338 | 342 |
| 339 // Create the expected value. | 343 // Create the expected value. |
| 340 base::DictionaryValue dictionary_value; | 344 base::DictionaryValue dictionary_value; |
| 341 dictionary_value.SetWithoutPathExpansion( | 345 dictionary_value.SetWithoutPathExpansion( |
| 342 kKey1, new base::FundamentalValue(kBoolValue)); | 346 kKey1, new base::FundamentalValue(kBoolValue)); |
| 343 dictionary_value.SetWithoutPathExpansion( | 347 dictionary_value.SetWithoutPathExpansion( |
| 344 kKey2, new base::FundamentalValue(kInt32Value)); | 348 kKey2, new base::FundamentalValue(kInt32Value)); |
| 345 dictionary_value.SetWithoutPathExpansion( | 349 dictionary_value.SetWithoutPathExpansion( |
| 346 kKey3, new base::FundamentalValue(kDoubleValue)); | 350 kKey3, new base::FundamentalValue(kDoubleValue)); |
| 347 | 351 |
| 348 // Pop a dictinoary. | 352 // Pop a dictinoary. |
| 349 MessageReader reader(response.get()); | 353 MessageReader reader(response.get()); |
| 350 scoped_ptr<base::Value> value(PopDataAsValue(&reader)); | 354 scoped_ptr<base::Value> value(PopDataAsValue(&reader)); |
| 351 ASSERT_TRUE(value.get() != NULL); | 355 ASSERT_TRUE(value.get() != NULL); |
| 352 EXPECT_TRUE(value->Equals(&dictionary_value)); | 356 EXPECT_TRUE(value->Equals(&dictionary_value)); |
| 353 } | 357 } |
| 354 | 358 |
| 355 TEST(ValuesUtilTest, PopDoubleToIntDictionary) { | 359 TEST(ValuesUtilTest, PopDoubleToIntDictionary) { |
| 356 // Create test data. | 360 // Create test data. |
| 357 const int32 kValues[] = {0, 1, 1, 2, 3, 5, 8, 13, 21}; | 361 const int32_t kValues[] = {0, 1, 1, 2, 3, 5, 8, 13, 21}; |
| 358 const std::vector<int32> values(kValues, kValues + arraysize(kValues)); | 362 const std::vector<int32_t> values(kValues, kValues + arraysize(kValues)); |
| 359 std::vector<double> keys(values.size()); | 363 std::vector<double> keys(values.size()); |
| 360 for (size_t i = 0; i != values.size(); ++i) | 364 for (size_t i = 0; i != values.size(); ++i) |
| 361 keys[i] = std::sqrt(values[i]); | 365 keys[i] = std::sqrt(values[i]); |
| 362 | 366 |
| 363 // Append a dictionary. | 367 // Append a dictionary. |
| 364 scoped_ptr<Response> response(Response::CreateEmpty()); | 368 scoped_ptr<Response> response(Response::CreateEmpty()); |
| 365 MessageWriter writer(response.get()); | 369 MessageWriter writer(response.get()); |
| 366 MessageWriter sub_writer(NULL); | 370 MessageWriter sub_writer(NULL); |
| 367 writer.OpenArray("{di}", &sub_writer); | 371 writer.OpenArray("{di}", &sub_writer); |
| 368 for (size_t i = 0; i != values.size(); ++i) { | 372 for (size_t i = 0; i != values.size(); ++i) { |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 TEST(ValuesUtilTest, AppendDictionary) { | 512 TEST(ValuesUtilTest, AppendDictionary) { |
| 509 // Set up the input dictionary. | 513 // Set up the input dictionary. |
| 510 const std::string kKey1 = "one"; | 514 const std::string kKey1 = "one"; |
| 511 const std::string kKey2 = "two"; | 515 const std::string kKey2 = "two"; |
| 512 const std::string kKey3 = "three"; | 516 const std::string kKey3 = "three"; |
| 513 const std::string kKey4 = "four"; | 517 const std::string kKey4 = "four"; |
| 514 const std::string kKey5 = "five"; | 518 const std::string kKey5 = "five"; |
| 515 const std::string kKey6 = "six"; | 519 const std::string kKey6 = "six"; |
| 516 | 520 |
| 517 const bool kBoolValue = true; | 521 const bool kBoolValue = true; |
| 518 const int32 kInt32Value = -45; | 522 const int32_t kInt32Value = -45; |
| 519 const double kDoubleValue = 4.9; | 523 const double kDoubleValue = 4.9; |
| 520 const std::string kStringValue = "fifty"; | 524 const std::string kStringValue = "fifty"; |
| 521 | 525 |
| 522 base::ListValue* list_value = new base::ListValue(); | 526 base::ListValue* list_value = new base::ListValue(); |
| 523 list_value->AppendBoolean(kBoolValue); | 527 list_value->AppendBoolean(kBoolValue); |
| 524 list_value->AppendInteger(kInt32Value); | 528 list_value->AppendInteger(kInt32Value); |
| 525 | 529 |
| 526 base::DictionaryValue* dictionary_value = new base::DictionaryValue(); | 530 base::DictionaryValue* dictionary_value = new base::DictionaryValue(); |
| 527 dictionary_value->SetBoolean(kKey1, kBoolValue); | 531 dictionary_value->SetBoolean(kKey1, kBoolValue); |
| 528 dictionary_value->SetInteger(kKey2, kDoubleValue); | 532 dictionary_value->SetInteger(kKey2, kDoubleValue); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 555 TEST(ValuesUtilTest, AppendDictionaryAsVariant) { | 559 TEST(ValuesUtilTest, AppendDictionaryAsVariant) { |
| 556 // Set up the input dictionary. | 560 // Set up the input dictionary. |
| 557 const std::string kKey1 = "one"; | 561 const std::string kKey1 = "one"; |
| 558 const std::string kKey2 = "two"; | 562 const std::string kKey2 = "two"; |
| 559 const std::string kKey3 = "three"; | 563 const std::string kKey3 = "three"; |
| 560 const std::string kKey4 = "four"; | 564 const std::string kKey4 = "four"; |
| 561 const std::string kKey5 = "five"; | 565 const std::string kKey5 = "five"; |
| 562 const std::string kKey6 = "six"; | 566 const std::string kKey6 = "six"; |
| 563 | 567 |
| 564 const bool kBoolValue = true; | 568 const bool kBoolValue = true; |
| 565 const int32 kInt32Value = -45; | 569 const int32_t kInt32Value = -45; |
| 566 const double kDoubleValue = 4.9; | 570 const double kDoubleValue = 4.9; |
| 567 const std::string kStringValue = "fifty"; | 571 const std::string kStringValue = "fifty"; |
| 568 | 572 |
| 569 base::ListValue* list_value = new base::ListValue(); | 573 base::ListValue* list_value = new base::ListValue(); |
| 570 list_value->AppendBoolean(kBoolValue); | 574 list_value->AppendBoolean(kBoolValue); |
| 571 list_value->AppendInteger(kInt32Value); | 575 list_value->AppendInteger(kInt32Value); |
| 572 | 576 |
| 573 base::DictionaryValue* dictionary_value = new base::DictionaryValue(); | 577 base::DictionaryValue* dictionary_value = new base::DictionaryValue(); |
| 574 dictionary_value->SetBoolean(kKey1, kBoolValue); | 578 dictionary_value->SetBoolean(kKey1, kBoolValue); |
| 575 dictionary_value->SetInteger(kKey2, kDoubleValue); | 579 dictionary_value->SetInteger(kKey2, kDoubleValue); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 598 ASSERT_TRUE(value.get() != NULL); | 602 ASSERT_TRUE(value.get() != NULL); |
| 599 EXPECT_TRUE(value->Equals(&int_value)); | 603 EXPECT_TRUE(value->Equals(&int_value)); |
| 600 } | 604 } |
| 601 | 605 |
| 602 TEST(ValuesUtilTest, AppendList) { | 606 TEST(ValuesUtilTest, AppendList) { |
| 603 // Set up the input list. | 607 // Set up the input list. |
| 604 const std::string kKey1 = "one"; | 608 const std::string kKey1 = "one"; |
| 605 const std::string kKey2 = "two"; | 609 const std::string kKey2 = "two"; |
| 606 | 610 |
| 607 const bool kBoolValue = true; | 611 const bool kBoolValue = true; |
| 608 const int32 kInt32Value = -45; | 612 const int32_t kInt32Value = -45; |
| 609 const double kDoubleValue = 4.9; | 613 const double kDoubleValue = 4.9; |
| 610 const std::string kStringValue = "fifty"; | 614 const std::string kStringValue = "fifty"; |
| 611 | 615 |
| 612 base::ListValue* list_value = new base::ListValue(); | 616 base::ListValue* list_value = new base::ListValue(); |
| 613 list_value->AppendBoolean(kBoolValue); | 617 list_value->AppendBoolean(kBoolValue); |
| 614 list_value->AppendInteger(kInt32Value); | 618 list_value->AppendInteger(kInt32Value); |
| 615 | 619 |
| 616 base::DictionaryValue* dictionary_value = new base::DictionaryValue(); | 620 base::DictionaryValue* dictionary_value = new base::DictionaryValue(); |
| 617 dictionary_value->SetBoolean(kKey1, kBoolValue); | 621 dictionary_value->SetBoolean(kKey1, kBoolValue); |
| 618 dictionary_value->SetInteger(kKey2, kDoubleValue); | 622 dictionary_value->SetInteger(kKey2, kDoubleValue); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 641 ASSERT_TRUE(value.get() != NULL); | 645 ASSERT_TRUE(value.get() != NULL); |
| 642 EXPECT_TRUE(value->Equals(&int_value)); | 646 EXPECT_TRUE(value->Equals(&int_value)); |
| 643 } | 647 } |
| 644 | 648 |
| 645 TEST(ValuesUtilTest, AppendListAsVariant) { | 649 TEST(ValuesUtilTest, AppendListAsVariant) { |
| 646 // Set up the input list. | 650 // Set up the input list. |
| 647 const std::string kKey1 = "one"; | 651 const std::string kKey1 = "one"; |
| 648 const std::string kKey2 = "two"; | 652 const std::string kKey2 = "two"; |
| 649 | 653 |
| 650 const bool kBoolValue = true; | 654 const bool kBoolValue = true; |
| 651 const int32 kInt32Value = -45; | 655 const int32_t kInt32Value = -45; |
| 652 const double kDoubleValue = 4.9; | 656 const double kDoubleValue = 4.9; |
| 653 const std::string kStringValue = "fifty"; | 657 const std::string kStringValue = "fifty"; |
| 654 | 658 |
| 655 base::ListValue* list_value = new base::ListValue(); | 659 base::ListValue* list_value = new base::ListValue(); |
| 656 list_value->AppendBoolean(kBoolValue); | 660 list_value->AppendBoolean(kBoolValue); |
| 657 list_value->AppendInteger(kInt32Value); | 661 list_value->AppendInteger(kInt32Value); |
| 658 | 662 |
| 659 base::DictionaryValue* dictionary_value = new base::DictionaryValue(); | 663 base::DictionaryValue* dictionary_value = new base::DictionaryValue(); |
| 660 dictionary_value->SetBoolean(kKey1, kBoolValue); | 664 dictionary_value->SetBoolean(kKey1, kBoolValue); |
| 661 dictionary_value->SetInteger(kKey2, kDoubleValue); | 665 dictionary_value->SetInteger(kKey2, kDoubleValue); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 679 scoped_ptr<base::Value> value; | 683 scoped_ptr<base::Value> value; |
| 680 value.reset(PopDataAsValue(&reader)); | 684 value.reset(PopDataAsValue(&reader)); |
| 681 ASSERT_TRUE(value.get() != NULL); | 685 ASSERT_TRUE(value.get() != NULL); |
| 682 EXPECT_TRUE(value->Equals(&test_list)); | 686 EXPECT_TRUE(value->Equals(&test_list)); |
| 683 value.reset(PopDataAsValue(&reader)); | 687 value.reset(PopDataAsValue(&reader)); |
| 684 ASSERT_TRUE(value.get() != NULL); | 688 ASSERT_TRUE(value.get() != NULL); |
| 685 EXPECT_TRUE(value->Equals(&int_value)); | 689 EXPECT_TRUE(value->Equals(&int_value)); |
| 686 } | 690 } |
| 687 | 691 |
| 688 } // namespace dbus | 692 } // namespace dbus |
| OLD | NEW |