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 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
Hans Wennborg | acb4c7e | 2015-04-25 06:38:04 +0900 | [diff] [blame] | 10 | #include <cmath> |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 13 | #include "base/json/json_writer.h" |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 14 | #include "base/macros.h" |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 15 | #include "base/memory/scoped_ptr.h" |
| 16 | #include "base/values.h" |
| 17 | #include "dbus/message.h" |
| 18 | #include "testing/gtest/include/gtest/gtest.h" |
| 19 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 20 | namespace dbus { |
| 21 | |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 22 | TEST(ValuesUtilTest, PopBasicTypes) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 23 | scoped_ptr<Response> response(Response::CreateEmpty()); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 24 | // Append basic type values. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 25 | MessageWriter writer(response.get()); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 26 | const uint8_t kByteValue = 42; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 27 | writer.AppendByte(kByteValue); |
| 28 | const bool kBoolValue = true; |
| 29 | writer.AppendBool(kBoolValue); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 30 | const int16_t kInt16Value = -43; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 31 | writer.AppendInt16(kInt16Value); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 32 | const uint16_t kUint16Value = 44; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 33 | writer.AppendUint16(kUint16Value); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 34 | const int32_t kInt32Value = -45; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 35 | writer.AppendInt32(kInt32Value); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 36 | const uint32_t kUint32Value = 46; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 37 | writer.AppendUint32(kUint32Value); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 38 | const int64_t kInt64Value = -47; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 39 | writer.AppendInt64(kInt64Value); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 40 | const uint64_t kUint64Value = 48; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 41 | writer.AppendUint64(kUint64Value); |
| 42 | const double kDoubleValue = 4.9; |
| 43 | writer.AppendDouble(kDoubleValue); |
| 44 | const std::string kStringValue = "fifty"; |
| 45 | writer.AppendString(kStringValue); |
| 46 | const std::string kEmptyStringValue; |
| 47 | writer.AppendString(kEmptyStringValue); |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 48 | const ObjectPath kObjectPathValue("/ObjectPath"); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 49 | writer.AppendObjectPath(kObjectPathValue); |
| 50 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 51 | MessageReader reader(response.get()); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 52 | scoped_ptr<base::Value> value; |
| 53 | scoped_ptr<base::Value> expected_value; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 54 | // Pop a byte. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 55 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 56 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 57 | expected_value.reset(new base::FundamentalValue(kByteValue)); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 58 | EXPECT_TRUE(value->Equals(expected_value.get())); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 59 | // Pop a bool. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 60 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 61 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 62 | expected_value.reset(new base::FundamentalValue(kBoolValue)); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 63 | EXPECT_TRUE(value->Equals(expected_value.get())); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 64 | // Pop an int16_t. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 65 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 66 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 67 | expected_value.reset(new base::FundamentalValue(kInt16Value)); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 68 | EXPECT_TRUE(value->Equals(expected_value.get())); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 69 | // Pop a uint16_t. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 70 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 71 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 72 | expected_value.reset(new base::FundamentalValue(kUint16Value)); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 73 | EXPECT_TRUE(value->Equals(expected_value.get())); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 74 | // Pop an int32_t. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 75 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 76 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 77 | expected_value.reset(new base::FundamentalValue(kInt32Value)); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 78 | EXPECT_TRUE(value->Equals(expected_value.get())); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 79 | // Pop a uint32_t. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 80 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 81 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 82 | expected_value.reset( |
| 83 | new base::FundamentalValue(static_cast<double>(kUint32Value))); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 84 | EXPECT_TRUE(value->Equals(expected_value.get())); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 85 | // Pop an int64_t. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 86 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 87 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 88 | expected_value.reset( |
| 89 | new base::FundamentalValue(static_cast<double>(kInt64Value))); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 90 | EXPECT_TRUE(value->Equals(expected_value.get())); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 91 | // Pop a uint64_t. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 92 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 93 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 94 | expected_value.reset( |
| 95 | new base::FundamentalValue(static_cast<double>(kUint64Value))); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 96 | EXPECT_TRUE(value->Equals(expected_value.get())); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 97 | // Pop a double. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 98 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 99 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 100 | expected_value.reset(new base::FundamentalValue(kDoubleValue)); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 101 | EXPECT_TRUE(value->Equals(expected_value.get())); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 102 | // Pop a string. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 103 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 104 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 105 | expected_value.reset(new base::StringValue(kStringValue)); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 106 | EXPECT_TRUE(value->Equals(expected_value.get())); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 107 | // Pop an empty string. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 108 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 109 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 110 | expected_value.reset(new base::StringValue(kEmptyStringValue)); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 111 | EXPECT_TRUE(value->Equals(expected_value.get())); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 112 | // Pop an object path. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 113 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 114 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 115 | expected_value.reset(new base::StringValue(kObjectPathValue.value())); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 116 | EXPECT_TRUE(value->Equals(expected_value.get())); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | TEST(ValuesUtilTest, PopVariant) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 120 | scoped_ptr<Response> response(Response::CreateEmpty()); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 121 | // Append variant values. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 122 | MessageWriter writer(response.get()); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 123 | const bool kBoolValue = true; |
| 124 | writer.AppendVariantOfBool(kBoolValue); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 125 | const int32_t kInt32Value = -45; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 126 | writer.AppendVariantOfInt32(kInt32Value); |
| 127 | const double kDoubleValue = 4.9; |
| 128 | writer.AppendVariantOfDouble(kDoubleValue); |
| 129 | const std::string kStringValue = "fifty"; |
| 130 | writer.AppendVariantOfString(kStringValue); |
| 131 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 132 | MessageReader reader(response.get()); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 133 | scoped_ptr<base::Value> value; |
| 134 | scoped_ptr<base::Value> expected_value; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 135 | // Pop a bool. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 136 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 137 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 138 | expected_value.reset(new base::FundamentalValue(kBoolValue)); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 139 | EXPECT_TRUE(value->Equals(expected_value.get())); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 140 | // Pop an int32_t. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 141 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 142 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 143 | expected_value.reset(new base::FundamentalValue(kInt32Value)); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 144 | EXPECT_TRUE(value->Equals(expected_value.get())); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 145 | // Pop a double. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 146 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 147 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 148 | expected_value.reset(new base::FundamentalValue(kDoubleValue)); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 149 | EXPECT_TRUE(value->Equals(expected_value.get())); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 150 | // Pop a string. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 151 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 152 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 153 | expected_value.reset(new base::StringValue(kStringValue)); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 154 | EXPECT_TRUE(value->Equals(expected_value.get())); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Pop extremely large integers which cannot be precisely represented in |
| 158 | // double. |
| 159 | TEST(ValuesUtilTest, PopExtremelyLargeIntegers) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 160 | scoped_ptr<Response> response(Response::CreateEmpty()); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 161 | // Append large integers. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 162 | MessageWriter writer(response.get()); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 163 | const int64_t kInt64Value = -123456789012345689LL; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 164 | writer.AppendInt64(kInt64Value); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 165 | const uint64_t kUint64Value = 9876543210987654321ULL; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 166 | writer.AppendUint64(kUint64Value); |
| 167 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 168 | MessageReader reader(response.get()); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 169 | scoped_ptr<base::Value> value; |
| 170 | scoped_ptr<base::Value> expected_value; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 171 | double double_value = 0; |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 172 | // Pop an int64_t. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 173 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 174 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 175 | expected_value.reset( |
| 176 | new base::FundamentalValue(static_cast<double>(kInt64Value))); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 177 | EXPECT_TRUE(value->Equals(expected_value.get())); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 178 | ASSERT_TRUE(value->GetAsDouble(&double_value)); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 179 | EXPECT_NE(kInt64Value, static_cast<int64_t>(double_value)); |
| 180 | // Pop a uint64_t. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 181 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 182 | ASSERT_TRUE(value.get() != NULL); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 183 | expected_value.reset( |
| 184 | new base::FundamentalValue(static_cast<double>(kUint64Value))); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 185 | EXPECT_TRUE(value->Equals(expected_value.get())); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 186 | ASSERT_TRUE(value->GetAsDouble(&double_value)); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 187 | EXPECT_NE(kUint64Value, static_cast<uint64_t>(double_value)); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | TEST(ValuesUtilTest, PopIntArray) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 191 | scoped_ptr<Response> response(Response::CreateEmpty()); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 192 | // Append an int32_t array. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 193 | MessageWriter writer(response.get()); |
| 194 | MessageWriter sub_writer(NULL); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 195 | std::vector<int32_t> data; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 196 | data.push_back(0); |
| 197 | data.push_back(1); |
| 198 | data.push_back(2); |
| 199 | writer.OpenArray("i", &sub_writer); |
| 200 | for (size_t i = 0; i != data.size(); ++i) |
| 201 | sub_writer.AppendInt32(data[i]); |
| 202 | writer.CloseContainer(&sub_writer); |
| 203 | |
| 204 | // Create the expected value. |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 205 | scoped_ptr<base::ListValue> list_value(new base::ListValue); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 206 | for (size_t i = 0; i != data.size(); ++i) |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 207 | list_value->Append(new base::FundamentalValue(data[i])); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 208 | |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 209 | // Pop an int32_t array. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 210 | MessageReader reader(response.get()); |
| 211 | scoped_ptr<base::Value> value(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 212 | ASSERT_TRUE(value.get() != NULL); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 213 | EXPECT_TRUE(value->Equals(list_value.get())); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | TEST(ValuesUtilTest, PopStringArray) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 217 | scoped_ptr<Response> response(Response::CreateEmpty()); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 218 | // Append a string array. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 219 | MessageWriter writer(response.get()); |
| 220 | MessageWriter sub_writer(NULL); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 221 | std::vector<std::string> data; |
| 222 | data.push_back("Dreamlifter"); |
| 223 | data.push_back("Beluga"); |
| 224 | data.push_back("Mriya"); |
| 225 | writer.AppendArrayOfStrings(data); |
| 226 | |
| 227 | // Create the expected value. |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 228 | scoped_ptr<base::ListValue> list_value(new base::ListValue); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 229 | for (size_t i = 0; i != data.size(); ++i) |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 230 | list_value->Append(new base::StringValue(data[i])); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 231 | |
| 232 | // Pop a string array. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 233 | MessageReader reader(response.get()); |
| 234 | scoped_ptr<base::Value> value(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 235 | ASSERT_TRUE(value.get() != NULL); |
satorux@chromium.org | 5e612da | 2012-05-31 15:55:53 +0900 | [diff] [blame] | 236 | EXPECT_TRUE(value->Equals(list_value.get())); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | TEST(ValuesUtilTest, PopStruct) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 240 | scoped_ptr<Response> response(Response::CreateEmpty()); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 241 | // Append a struct. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 242 | MessageWriter writer(response.get()); |
| 243 | MessageWriter sub_writer(NULL); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 244 | writer.OpenStruct(&sub_writer); |
| 245 | const bool kBoolValue = true; |
| 246 | sub_writer.AppendBool(kBoolValue); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 247 | const int32_t kInt32Value = -123; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 248 | sub_writer.AppendInt32(kInt32Value); |
| 249 | const double kDoubleValue = 1.23; |
| 250 | sub_writer.AppendDouble(kDoubleValue); |
| 251 | const std::string kStringValue = "one two three"; |
| 252 | sub_writer.AppendString(kStringValue); |
| 253 | writer.CloseContainer(&sub_writer); |
| 254 | |
| 255 | // Create the expected value. |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 256 | base::ListValue list_value; |
| 257 | list_value.Append(new base::FundamentalValue(kBoolValue)); |
| 258 | list_value.Append(new base::FundamentalValue(kInt32Value)); |
| 259 | list_value.Append(new base::FundamentalValue(kDoubleValue)); |
| 260 | list_value.Append(new base::StringValue(kStringValue)); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 261 | |
| 262 | // Pop a struct. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 263 | MessageReader reader(response.get()); |
| 264 | scoped_ptr<base::Value> value(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 265 | ASSERT_TRUE(value.get() != NULL); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 266 | EXPECT_TRUE(value->Equals(&list_value)); |
| 267 | } |
| 268 | |
| 269 | TEST(ValuesUtilTest, PopStringToVariantDictionary) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 270 | scoped_ptr<Response> response(Response::CreateEmpty()); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 271 | // Append a dictionary. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 272 | MessageWriter writer(response.get()); |
| 273 | MessageWriter sub_writer(NULL); |
| 274 | MessageWriter entry_writer(NULL); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 275 | writer.OpenArray("{sv}", &sub_writer); |
| 276 | sub_writer.OpenDictEntry(&entry_writer); |
| 277 | const std::string kKey1 = "one"; |
| 278 | entry_writer.AppendString(kKey1); |
| 279 | const bool kBoolValue = true; |
| 280 | entry_writer.AppendVariantOfBool(kBoolValue); |
| 281 | sub_writer.CloseContainer(&entry_writer); |
| 282 | sub_writer.OpenDictEntry(&entry_writer); |
| 283 | const std::string kKey2 = "two"; |
| 284 | entry_writer.AppendString(kKey2); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 285 | const int32_t kInt32Value = -45; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 286 | entry_writer.AppendVariantOfInt32(kInt32Value); |
| 287 | sub_writer.CloseContainer(&entry_writer); |
| 288 | sub_writer.OpenDictEntry(&entry_writer); |
| 289 | const std::string kKey3 = "three"; |
| 290 | entry_writer.AppendString(kKey3); |
| 291 | const double kDoubleValue = 4.9; |
| 292 | entry_writer.AppendVariantOfDouble(kDoubleValue); |
| 293 | sub_writer.CloseContainer(&entry_writer); |
| 294 | sub_writer.OpenDictEntry(&entry_writer); |
| 295 | const std::string kKey4 = "four"; |
| 296 | entry_writer.AppendString(kKey4); |
| 297 | const std::string kStringValue = "fifty"; |
| 298 | entry_writer.AppendVariantOfString(kStringValue); |
| 299 | sub_writer.CloseContainer(&entry_writer); |
| 300 | writer.CloseContainer(&sub_writer); |
| 301 | |
| 302 | // Create the expected value. |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 303 | base::DictionaryValue dictionary_value; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 304 | dictionary_value.SetBoolean(kKey1, kBoolValue); |
| 305 | dictionary_value.SetInteger(kKey2, kInt32Value); |
| 306 | dictionary_value.SetDouble(kKey3, kDoubleValue); |
| 307 | dictionary_value.SetString(kKey4, kStringValue); |
| 308 | |
| 309 | // Pop a dictinoary. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 310 | MessageReader reader(response.get()); |
| 311 | scoped_ptr<base::Value> value(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 312 | ASSERT_TRUE(value.get() != NULL); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 313 | EXPECT_TRUE(value->Equals(&dictionary_value)); |
| 314 | } |
| 315 | |
hashimoto@chromium.org | 9a576b0 | 2012-03-21 03:18:57 +0900 | [diff] [blame] | 316 | TEST(ValuesUtilTest, PopDictionaryWithDottedStringKey) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 317 | scoped_ptr<Response> response(Response::CreateEmpty()); |
hashimoto@chromium.org | 9a576b0 | 2012-03-21 03:18:57 +0900 | [diff] [blame] | 318 | // Append a dictionary. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 319 | MessageWriter writer(response.get()); |
| 320 | MessageWriter sub_writer(NULL); |
| 321 | MessageWriter entry_writer(NULL); |
hashimoto@chromium.org | 9a576b0 | 2012-03-21 03:18:57 +0900 | [diff] [blame] | 322 | writer.OpenArray("{sv}", &sub_writer); |
| 323 | sub_writer.OpenDictEntry(&entry_writer); |
| 324 | const std::string kKey1 = "www.example.com"; // String including dots. |
| 325 | entry_writer.AppendString(kKey1); |
| 326 | const bool kBoolValue = true; |
| 327 | entry_writer.AppendVariantOfBool(kBoolValue); |
| 328 | sub_writer.CloseContainer(&entry_writer); |
| 329 | sub_writer.OpenDictEntry(&entry_writer); |
| 330 | const std::string kKey2 = ".example"; // String starting with a dot. |
| 331 | entry_writer.AppendString(kKey2); |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 332 | const int32_t kInt32Value = -45; |
hashimoto@chromium.org | 9a576b0 | 2012-03-21 03:18:57 +0900 | [diff] [blame] | 333 | entry_writer.AppendVariantOfInt32(kInt32Value); |
| 334 | sub_writer.CloseContainer(&entry_writer); |
| 335 | sub_writer.OpenDictEntry(&entry_writer); |
| 336 | const std::string kKey3 = "example."; // String ending with a dot. |
| 337 | entry_writer.AppendString(kKey3); |
| 338 | const double kDoubleValue = 4.9; |
| 339 | entry_writer.AppendVariantOfDouble(kDoubleValue); |
| 340 | sub_writer.CloseContainer(&entry_writer); |
| 341 | writer.CloseContainer(&sub_writer); |
| 342 | |
| 343 | // Create the expected value. |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 344 | base::DictionaryValue dictionary_value; |
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 | kKey1, new base::FundamentalValue(kBoolValue)); |
hashimoto@chromium.org | 9a576b0 | 2012-03-21 03:18:57 +0900 | [diff] [blame] | 347 | dictionary_value.SetWithoutPathExpansion( |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 348 | kKey2, new base::FundamentalValue(kInt32Value)); |
hashimoto@chromium.org | 9a576b0 | 2012-03-21 03:18:57 +0900 | [diff] [blame] | 349 | dictionary_value.SetWithoutPathExpansion( |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 350 | kKey3, new base::FundamentalValue(kDoubleValue)); |
hashimoto@chromium.org | 9a576b0 | 2012-03-21 03:18:57 +0900 | [diff] [blame] | 351 | |
| 352 | // Pop a dictinoary. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 353 | MessageReader reader(response.get()); |
| 354 | scoped_ptr<base::Value> value(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 355 | ASSERT_TRUE(value.get() != NULL); |
hashimoto@chromium.org | 9a576b0 | 2012-03-21 03:18:57 +0900 | [diff] [blame] | 356 | EXPECT_TRUE(value->Equals(&dictionary_value)); |
| 357 | } |
| 358 | |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 359 | TEST(ValuesUtilTest, PopDoubleToIntDictionary) { |
| 360 | // Create test data. |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 361 | const int32_t kValues[] = {0, 1, 1, 2, 3, 5, 8, 13, 21}; |
| 362 | const std::vector<int32_t> values(kValues, kValues + arraysize(kValues)); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 363 | std::vector<double> keys(values.size()); |
| 364 | for (size_t i = 0; i != values.size(); ++i) |
Hans Wennborg | acb4c7e | 2015-04-25 06:38:04 +0900 | [diff] [blame] | 365 | keys[i] = std::sqrt(values[i]); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 366 | |
| 367 | // Append a dictionary. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 368 | scoped_ptr<Response> response(Response::CreateEmpty()); |
| 369 | MessageWriter writer(response.get()); |
| 370 | MessageWriter sub_writer(NULL); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 371 | writer.OpenArray("{di}", &sub_writer); |
| 372 | for (size_t i = 0; i != values.size(); ++i) { |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 373 | MessageWriter entry_writer(NULL); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 374 | sub_writer.OpenDictEntry(&entry_writer); |
| 375 | entry_writer.AppendDouble(keys[i]); |
| 376 | entry_writer.AppendInt32(values[i]); |
| 377 | sub_writer.CloseContainer(&entry_writer); |
| 378 | } |
| 379 | writer.CloseContainer(&sub_writer); |
| 380 | |
| 381 | // Create the expected value. |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 382 | base::DictionaryValue dictionary_value; |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 383 | for (size_t i = 0; i != values.size(); ++i) { |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 384 | std::string key_string; |
estade | b5f30dd | 2015-05-16 10:02:34 +0900 | [diff] [blame] | 385 | base::JSONWriter::Write(base::FundamentalValue(keys[i]), &key_string); |
| 386 | dictionary_value.SetIntegerWithoutPathExpansion(key_string, values[i]); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | // Pop a dictionary. |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 390 | MessageReader reader(response.get()); |
| 391 | scoped_ptr<base::Value> value(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 392 | ASSERT_TRUE(value.get() != NULL); |
hashimoto@chromium.org | 8fb5a2b | 2012-03-17 08:08:42 +0900 | [diff] [blame] | 393 | EXPECT_TRUE(value->Equals(&dictionary_value)); |
| 394 | } |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 395 | |
| 396 | TEST(ValuesUtilTest, AppendBasicTypes) { |
| 397 | const base::FundamentalValue kBoolValue(false); |
| 398 | const base::FundamentalValue kIntegerValue(42); |
| 399 | const base::FundamentalValue kDoubleValue(4.2); |
| 400 | const base::StringValue kStringValue("string"); |
| 401 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 402 | scoped_ptr<Response> response(Response::CreateEmpty()); |
| 403 | MessageWriter writer(response.get()); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 404 | AppendBasicTypeValueData(&writer, kBoolValue); |
| 405 | AppendBasicTypeValueData(&writer, kIntegerValue); |
| 406 | AppendBasicTypeValueData(&writer, kDoubleValue); |
| 407 | AppendBasicTypeValueData(&writer, kStringValue); |
| 408 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 409 | MessageReader reader(response.get()); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 410 | scoped_ptr<base::Value> value; |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 411 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 412 | ASSERT_TRUE(value.get() != NULL); |
| 413 | EXPECT_TRUE(value->Equals(&kBoolValue)); |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 414 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 415 | ASSERT_TRUE(value.get() != NULL); |
| 416 | EXPECT_TRUE(value->Equals(&kIntegerValue)); |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 417 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 418 | ASSERT_TRUE(value.get() != NULL); |
| 419 | EXPECT_TRUE(value->Equals(&kDoubleValue)); |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 420 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 421 | ASSERT_TRUE(value.get() != NULL); |
| 422 | EXPECT_TRUE(value->Equals(&kStringValue)); |
| 423 | } |
| 424 | |
| 425 | TEST(ValuesUtilTest, AppendBasicTypesAsVariant) { |
| 426 | const base::FundamentalValue kBoolValue(false); |
| 427 | const base::FundamentalValue kIntegerValue(42); |
| 428 | const base::FundamentalValue kDoubleValue(4.2); |
| 429 | const base::StringValue kStringValue("string"); |
| 430 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 431 | scoped_ptr<Response> response(Response::CreateEmpty()); |
| 432 | MessageWriter writer(response.get()); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 433 | AppendBasicTypeValueDataAsVariant(&writer, kBoolValue); |
| 434 | AppendBasicTypeValueDataAsVariant(&writer, kIntegerValue); |
| 435 | AppendBasicTypeValueDataAsVariant(&writer, kDoubleValue); |
| 436 | AppendBasicTypeValueDataAsVariant(&writer, kStringValue); |
| 437 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 438 | MessageReader reader(response.get()); |
thestig@chromium.org | e1acdf8 | 2013-02-13 05:06:15 +0900 | [diff] [blame] | 439 | scoped_ptr<base::Value> value; |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 440 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 441 | ASSERT_TRUE(value.get() != NULL); |
| 442 | EXPECT_TRUE(value->Equals(&kBoolValue)); |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 443 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 444 | ASSERT_TRUE(value.get() != NULL); |
| 445 | EXPECT_TRUE(value->Equals(&kIntegerValue)); |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 446 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 447 | ASSERT_TRUE(value.get() != NULL); |
| 448 | EXPECT_TRUE(value->Equals(&kDoubleValue)); |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 449 | value.reset(PopDataAsValue(&reader)); |
hashimoto@chromium.org | ea59982 | 2012-03-25 05:37:18 +0900 | [diff] [blame] | 450 | ASSERT_TRUE(value.get() != NULL); |
| 451 | EXPECT_TRUE(value->Equals(&kStringValue)); |
| 452 | } |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 453 | |
armansito@chromium.org | 39d51b9 | 2014-05-22 20:52:55 +0900 | [diff] [blame] | 454 | TEST(ValuesUtilTest, AppendValueDataBasicTypes) { |
| 455 | const base::FundamentalValue kBoolValue(false); |
| 456 | const base::FundamentalValue kIntegerValue(42); |
| 457 | const base::FundamentalValue kDoubleValue(4.2); |
| 458 | const base::StringValue kStringValue("string"); |
| 459 | |
| 460 | scoped_ptr<Response> response(Response::CreateEmpty()); |
| 461 | MessageWriter writer(response.get()); |
| 462 | AppendValueData(&writer, kBoolValue); |
| 463 | AppendValueData(&writer, kIntegerValue); |
| 464 | AppendValueData(&writer, kDoubleValue); |
| 465 | AppendValueData(&writer, kStringValue); |
| 466 | |
| 467 | MessageReader reader(response.get()); |
| 468 | scoped_ptr<base::Value> value; |
| 469 | value.reset(PopDataAsValue(&reader)); |
| 470 | ASSERT_TRUE(value.get() != NULL); |
| 471 | EXPECT_TRUE(value->Equals(&kBoolValue)); |
| 472 | value.reset(PopDataAsValue(&reader)); |
| 473 | ASSERT_TRUE(value.get() != NULL); |
| 474 | EXPECT_TRUE(value->Equals(&kIntegerValue)); |
| 475 | value.reset(PopDataAsValue(&reader)); |
| 476 | ASSERT_TRUE(value.get() != NULL); |
| 477 | EXPECT_TRUE(value->Equals(&kDoubleValue)); |
| 478 | value.reset(PopDataAsValue(&reader)); |
| 479 | ASSERT_TRUE(value.get() != NULL); |
| 480 | EXPECT_TRUE(value->Equals(&kStringValue)); |
| 481 | } |
| 482 | |
| 483 | TEST(ValuesUtilTest, AppendValueDataAsVariantBasicTypes) { |
| 484 | const base::FundamentalValue kBoolValue(false); |
| 485 | const base::FundamentalValue kIntegerValue(42); |
| 486 | const base::FundamentalValue kDoubleValue(4.2); |
| 487 | const base::StringValue kStringValue("string"); |
| 488 | |
| 489 | scoped_ptr<Response> response(Response::CreateEmpty()); |
| 490 | MessageWriter writer(response.get()); |
| 491 | AppendValueDataAsVariant(&writer, kBoolValue); |
| 492 | AppendValueDataAsVariant(&writer, kIntegerValue); |
| 493 | AppendValueDataAsVariant(&writer, kDoubleValue); |
| 494 | AppendValueDataAsVariant(&writer, kStringValue); |
| 495 | |
| 496 | MessageReader reader(response.get()); |
| 497 | scoped_ptr<base::Value> value; |
| 498 | value.reset(PopDataAsValue(&reader)); |
| 499 | ASSERT_TRUE(value.get() != NULL); |
| 500 | EXPECT_TRUE(value->Equals(&kBoolValue)); |
| 501 | value.reset(PopDataAsValue(&reader)); |
| 502 | ASSERT_TRUE(value.get() != NULL); |
| 503 | EXPECT_TRUE(value->Equals(&kIntegerValue)); |
| 504 | value.reset(PopDataAsValue(&reader)); |
| 505 | ASSERT_TRUE(value.get() != NULL); |
| 506 | EXPECT_TRUE(value->Equals(&kDoubleValue)); |
| 507 | value.reset(PopDataAsValue(&reader)); |
| 508 | ASSERT_TRUE(value.get() != NULL); |
| 509 | EXPECT_TRUE(value->Equals(&kStringValue)); |
| 510 | } |
| 511 | |
| 512 | TEST(ValuesUtilTest, AppendDictionary) { |
| 513 | // Set up the input dictionary. |
| 514 | const std::string kKey1 = "one"; |
| 515 | const std::string kKey2 = "two"; |
| 516 | const std::string kKey3 = "three"; |
| 517 | const std::string kKey4 = "four"; |
| 518 | const std::string kKey5 = "five"; |
| 519 | const std::string kKey6 = "six"; |
| 520 | |
| 521 | const bool kBoolValue = true; |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 522 | const int32_t kInt32Value = -45; |
armansito@chromium.org | 39d51b9 | 2014-05-22 20:52:55 +0900 | [diff] [blame] | 523 | const double kDoubleValue = 4.9; |
| 524 | const std::string kStringValue = "fifty"; |
| 525 | |
| 526 | base::ListValue* list_value = new base::ListValue(); |
| 527 | list_value->AppendBoolean(kBoolValue); |
| 528 | list_value->AppendInteger(kInt32Value); |
| 529 | |
| 530 | base::DictionaryValue* dictionary_value = new base::DictionaryValue(); |
| 531 | dictionary_value->SetBoolean(kKey1, kBoolValue); |
| 532 | dictionary_value->SetInteger(kKey2, kDoubleValue); |
| 533 | |
| 534 | base::DictionaryValue test_dictionary; |
| 535 | test_dictionary.SetBoolean(kKey1, kBoolValue); |
| 536 | test_dictionary.SetInteger(kKey2, kInt32Value); |
| 537 | test_dictionary.SetDouble(kKey3, kDoubleValue); |
| 538 | test_dictionary.SetString(kKey4, kStringValue); |
| 539 | test_dictionary.Set(kKey5, list_value); // takes ownership |
| 540 | test_dictionary.Set(kKey6, dictionary_value); // takes ownership |
| 541 | |
| 542 | scoped_ptr<Response> response(Response::CreateEmpty()); |
| 543 | MessageWriter writer(response.get()); |
| 544 | AppendValueData(&writer, test_dictionary); |
| 545 | base::FundamentalValue int_value(kInt32Value); |
| 546 | AppendValueData(&writer, int_value); |
| 547 | |
| 548 | // Read the data. |
| 549 | MessageReader reader(response.get()); |
| 550 | scoped_ptr<base::Value> value; |
| 551 | value.reset(PopDataAsValue(&reader)); |
| 552 | ASSERT_TRUE(value.get() != NULL); |
| 553 | EXPECT_TRUE(value->Equals(&test_dictionary)); |
| 554 | value.reset(PopDataAsValue(&reader)); |
| 555 | ASSERT_TRUE(value.get() != NULL); |
| 556 | EXPECT_TRUE(value->Equals(&int_value)); |
| 557 | } |
| 558 | |
| 559 | TEST(ValuesUtilTest, AppendDictionaryAsVariant) { |
| 560 | // Set up the input dictionary. |
| 561 | const std::string kKey1 = "one"; |
| 562 | const std::string kKey2 = "two"; |
| 563 | const std::string kKey3 = "three"; |
| 564 | const std::string kKey4 = "four"; |
| 565 | const std::string kKey5 = "five"; |
| 566 | const std::string kKey6 = "six"; |
| 567 | |
| 568 | const bool kBoolValue = true; |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 569 | const int32_t kInt32Value = -45; |
armansito@chromium.org | 39d51b9 | 2014-05-22 20:52:55 +0900 | [diff] [blame] | 570 | const double kDoubleValue = 4.9; |
| 571 | const std::string kStringValue = "fifty"; |
| 572 | |
| 573 | base::ListValue* list_value = new base::ListValue(); |
| 574 | list_value->AppendBoolean(kBoolValue); |
| 575 | list_value->AppendInteger(kInt32Value); |
| 576 | |
| 577 | base::DictionaryValue* dictionary_value = new base::DictionaryValue(); |
| 578 | dictionary_value->SetBoolean(kKey1, kBoolValue); |
| 579 | dictionary_value->SetInteger(kKey2, kDoubleValue); |
| 580 | |
| 581 | base::DictionaryValue test_dictionary; |
| 582 | test_dictionary.SetBoolean(kKey1, kBoolValue); |
| 583 | test_dictionary.SetInteger(kKey2, kInt32Value); |
| 584 | test_dictionary.SetDouble(kKey3, kDoubleValue); |
| 585 | test_dictionary.SetString(kKey4, kStringValue); |
| 586 | test_dictionary.Set(kKey5, list_value); // takes ownership |
| 587 | test_dictionary.Set(kKey6, dictionary_value); // takes ownership |
| 588 | |
| 589 | scoped_ptr<Response> response(Response::CreateEmpty()); |
| 590 | MessageWriter writer(response.get()); |
| 591 | AppendValueDataAsVariant(&writer, test_dictionary); |
| 592 | base::FundamentalValue int_value(kInt32Value); |
| 593 | AppendValueData(&writer, int_value); |
| 594 | |
| 595 | // Read the data. |
| 596 | MessageReader reader(response.get()); |
| 597 | scoped_ptr<base::Value> value; |
| 598 | value.reset(PopDataAsValue(&reader)); |
| 599 | ASSERT_TRUE(value.get() != NULL); |
| 600 | EXPECT_TRUE(value->Equals(&test_dictionary)); |
| 601 | value.reset(PopDataAsValue(&reader)); |
| 602 | ASSERT_TRUE(value.get() != NULL); |
| 603 | EXPECT_TRUE(value->Equals(&int_value)); |
| 604 | } |
| 605 | |
| 606 | TEST(ValuesUtilTest, AppendList) { |
| 607 | // Set up the input list. |
| 608 | const std::string kKey1 = "one"; |
| 609 | const std::string kKey2 = "two"; |
| 610 | |
| 611 | const bool kBoolValue = true; |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 612 | const int32_t kInt32Value = -45; |
armansito@chromium.org | 39d51b9 | 2014-05-22 20:52:55 +0900 | [diff] [blame] | 613 | const double kDoubleValue = 4.9; |
| 614 | const std::string kStringValue = "fifty"; |
| 615 | |
| 616 | base::ListValue* list_value = new base::ListValue(); |
| 617 | list_value->AppendBoolean(kBoolValue); |
| 618 | list_value->AppendInteger(kInt32Value); |
| 619 | |
| 620 | base::DictionaryValue* dictionary_value = new base::DictionaryValue(); |
| 621 | dictionary_value->SetBoolean(kKey1, kBoolValue); |
| 622 | dictionary_value->SetInteger(kKey2, kDoubleValue); |
| 623 | |
| 624 | base::ListValue test_list; |
| 625 | test_list.AppendBoolean(kBoolValue); |
| 626 | test_list.AppendInteger(kInt32Value); |
| 627 | test_list.AppendDouble(kDoubleValue); |
| 628 | test_list.AppendString(kStringValue); |
| 629 | test_list.Append(list_value); // takes ownership |
| 630 | test_list.Append(dictionary_value); // takes ownership |
| 631 | |
| 632 | scoped_ptr<Response> response(Response::CreateEmpty()); |
| 633 | MessageWriter writer(response.get()); |
| 634 | AppendValueData(&writer, test_list); |
| 635 | base::FundamentalValue int_value(kInt32Value); |
| 636 | AppendValueData(&writer, int_value); |
| 637 | |
| 638 | // Read the data. |
| 639 | MessageReader reader(response.get()); |
| 640 | scoped_ptr<base::Value> value; |
| 641 | value.reset(PopDataAsValue(&reader)); |
| 642 | ASSERT_TRUE(value.get() != NULL); |
| 643 | EXPECT_TRUE(value->Equals(&test_list)); |
| 644 | value.reset(PopDataAsValue(&reader)); |
| 645 | ASSERT_TRUE(value.get() != NULL); |
| 646 | EXPECT_TRUE(value->Equals(&int_value)); |
| 647 | } |
| 648 | |
| 649 | TEST(ValuesUtilTest, AppendListAsVariant) { |
| 650 | // Set up the input list. |
| 651 | const std::string kKey1 = "one"; |
| 652 | const std::string kKey2 = "two"; |
| 653 | |
| 654 | const bool kBoolValue = true; |
avi | 0ad0ce0 | 2015-12-23 03:12:45 +0900 | [diff] [blame] | 655 | const int32_t kInt32Value = -45; |
armansito@chromium.org | 39d51b9 | 2014-05-22 20:52:55 +0900 | [diff] [blame] | 656 | const double kDoubleValue = 4.9; |
| 657 | const std::string kStringValue = "fifty"; |
| 658 | |
| 659 | base::ListValue* list_value = new base::ListValue(); |
| 660 | list_value->AppendBoolean(kBoolValue); |
| 661 | list_value->AppendInteger(kInt32Value); |
| 662 | |
| 663 | base::DictionaryValue* dictionary_value = new base::DictionaryValue(); |
| 664 | dictionary_value->SetBoolean(kKey1, kBoolValue); |
| 665 | dictionary_value->SetInteger(kKey2, kDoubleValue); |
| 666 | |
| 667 | base::ListValue test_list; |
| 668 | test_list.AppendBoolean(kBoolValue); |
| 669 | test_list.AppendInteger(kInt32Value); |
| 670 | test_list.AppendDouble(kDoubleValue); |
| 671 | test_list.AppendString(kStringValue); |
| 672 | test_list.Append(list_value); // takes ownership |
| 673 | test_list.Append(dictionary_value); // takes ownership |
| 674 | |
| 675 | scoped_ptr<Response> response(Response::CreateEmpty()); |
| 676 | MessageWriter writer(response.get()); |
| 677 | AppendValueDataAsVariant(&writer, test_list); |
| 678 | base::FundamentalValue int_value(kInt32Value); |
| 679 | AppendValueData(&writer, int_value); |
| 680 | |
| 681 | // Read the data. |
| 682 | MessageReader reader(response.get()); |
| 683 | scoped_ptr<base::Value> value; |
| 684 | value.reset(PopDataAsValue(&reader)); |
| 685 | ASSERT_TRUE(value.get() != NULL); |
| 686 | EXPECT_TRUE(value->Equals(&test_list)); |
| 687 | value.reset(PopDataAsValue(&reader)); |
| 688 | ASSERT_TRUE(value.get() != NULL); |
| 689 | EXPECT_TRUE(value->Equals(&int_value)); |
| 690 | } |
| 691 | |
thestig@chromium.org | f0b7eac | 2013-06-13 15:37:19 +0900 | [diff] [blame] | 692 | } // namespace dbus |