OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 <memory> |
| 6 #include <string> |
| 7 #include <unordered_map> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/message_loop/message_loop.h" |
| 11 #include "mojo/public/cpp/bindings/binding.h" |
| 12 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" |
| 13 #include "mojo/public/cpp/bindings/lib/serialization.h" |
| 14 #include "mojo/public/interfaces/bindings/tests/test_data_view.mojom.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 |
| 17 namespace mojo { |
| 18 namespace test { |
| 19 namespace data_view { |
| 20 namespace { |
| 21 |
| 22 class DataViewTest : public testing::Test { |
| 23 private: |
| 24 base::MessageLoop message_loop_; |
| 25 }; |
| 26 |
| 27 struct DataViewHolder { |
| 28 std::unique_ptr<TestStructDataView> data_view; |
| 29 std::unique_ptr<mojo::internal::FixedBufferForTesting> buf; |
| 30 mojo::internal::SerializationContext context; |
| 31 }; |
| 32 |
| 33 std::unique_ptr<DataViewHolder> SerializeTestStruct(TestStructPtr input) { |
| 34 std::unique_ptr<DataViewHolder> result(new DataViewHolder); |
| 35 |
| 36 size_t size = mojo::internal::PrepareToSerialize<TestStructPtr>( |
| 37 input, &result->context); |
| 38 |
| 39 result->buf.reset(new mojo::internal::FixedBufferForTesting(size)); |
| 40 internal::TestStruct_Data* data = nullptr; |
| 41 mojo::internal::Serialize<TestStructPtr>(input, result->buf.get(), &data, |
| 42 &result->context); |
| 43 |
| 44 result->data_view.reset(new TestStructDataView(data, &result->context)); |
| 45 return result; |
| 46 } |
| 47 |
| 48 class TestInterfaceImpl : public TestInterface { |
| 49 public: |
| 50 explicit TestInterfaceImpl(TestInterfaceRequest request) |
| 51 : binding_(this, std::move(request)) {} |
| 52 ~TestInterfaceImpl() override {} |
| 53 |
| 54 // TestInterface implementation: |
| 55 void Echo(int32_t value, const EchoCallback& callback) override { |
| 56 callback.Run(value); |
| 57 } |
| 58 |
| 59 private: |
| 60 Binding<TestInterface> binding_; |
| 61 }; |
| 62 |
| 63 } // namespace |
| 64 |
| 65 TEST_F(DataViewTest, String) { |
| 66 TestStructPtr obj(TestStruct::New()); |
| 67 obj->f_string = "hello"; |
| 68 |
| 69 auto data_view_holder = SerializeTestStruct(std::move(obj)); |
| 70 auto& data_view = *data_view_holder->data_view; |
| 71 |
| 72 StringDataView string_data_view; |
| 73 data_view.GetFStringDataView(&string_data_view); |
| 74 |
| 75 ASSERT_FALSE(string_data_view.is_null()); |
| 76 EXPECT_EQ(std::string("hello"), |
| 77 std::string(string_data_view.storage(), string_data_view.size())); |
| 78 } |
| 79 |
| 80 TEST_F(DataViewTest, NestedStruct) { |
| 81 TestStructPtr obj(TestStruct::New()); |
| 82 obj->f_struct = NestedStruct::New(); |
| 83 obj->f_struct->f_int32 = 42; |
| 84 |
| 85 auto data_view_holder = SerializeTestStruct(std::move(obj)); |
| 86 auto& data_view = *data_view_holder->data_view; |
| 87 |
| 88 NestedStructDataView struct_data_view; |
| 89 data_view.GetFStructDataView(&struct_data_view); |
| 90 |
| 91 ASSERT_FALSE(struct_data_view.is_null()); |
| 92 EXPECT_EQ(42, struct_data_view.f_int32()); |
| 93 } |
| 94 |
| 95 TEST_F(DataViewTest, NativeStruct) { |
| 96 TestStructPtr obj(TestStruct::New()); |
| 97 obj->f_native_struct = NativeStruct::New(); |
| 98 obj->f_native_struct->data = std::vector<uint8_t>({3, 2, 1}); |
| 99 |
| 100 auto data_view_holder = SerializeTestStruct(std::move(obj)); |
| 101 auto& data_view = *data_view_holder->data_view; |
| 102 |
| 103 NativeStructDataView struct_data_view; |
| 104 data_view.GetFNativeStructDataView(&struct_data_view); |
| 105 |
| 106 ASSERT_FALSE(struct_data_view.is_null()); |
| 107 ASSERT_EQ(3u, struct_data_view.size()); |
| 108 EXPECT_EQ(3, struct_data_view[0]); |
| 109 EXPECT_EQ(2, struct_data_view[1]); |
| 110 EXPECT_EQ(1, struct_data_view[2]); |
| 111 EXPECT_EQ(3, *struct_data_view.data()); |
| 112 } |
| 113 |
| 114 TEST_F(DataViewTest, BoolArray) { |
| 115 TestStructPtr obj(TestStruct::New()); |
| 116 obj->f_bool_array = {true, false}; |
| 117 |
| 118 auto data_view_holder = SerializeTestStruct(std::move(obj)); |
| 119 auto& data_view = *data_view_holder->data_view; |
| 120 |
| 121 ArrayDataView<bool> array_data_view; |
| 122 data_view.GetFBoolArrayDataView(&array_data_view); |
| 123 |
| 124 ASSERT_FALSE(array_data_view.is_null()); |
| 125 ASSERT_EQ(2u, array_data_view.size()); |
| 126 EXPECT_TRUE(array_data_view[0]); |
| 127 EXPECT_FALSE(array_data_view[1]); |
| 128 } |
| 129 |
| 130 TEST_F(DataViewTest, IntegerArray) { |
| 131 TestStructPtr obj(TestStruct::New()); |
| 132 obj->f_int32_array = {1024, 128}; |
| 133 |
| 134 auto data_view_holder = SerializeTestStruct(std::move(obj)); |
| 135 auto& data_view = *data_view_holder->data_view; |
| 136 |
| 137 ArrayDataView<int32_t> array_data_view; |
| 138 data_view.GetFInt32ArrayDataView(&array_data_view); |
| 139 |
| 140 ASSERT_FALSE(array_data_view.is_null()); |
| 141 ASSERT_EQ(2u, array_data_view.size()); |
| 142 EXPECT_EQ(1024, array_data_view[0]); |
| 143 EXPECT_EQ(128, array_data_view[1]); |
| 144 EXPECT_EQ(1024, *array_data_view.data()); |
| 145 } |
| 146 |
| 147 TEST_F(DataViewTest, EnumArray) { |
| 148 TestStructPtr obj(TestStruct::New()); |
| 149 obj->f_enum_array = {TestEnum::VALUE_1, TestEnum::VALUE_0}; |
| 150 |
| 151 auto data_view_holder = SerializeTestStruct(std::move(obj)); |
| 152 auto& data_view = *data_view_holder->data_view; |
| 153 |
| 154 ArrayDataView<TestEnum> array_data_view; |
| 155 data_view.GetFEnumArrayDataView(&array_data_view); |
| 156 |
| 157 ASSERT_FALSE(array_data_view.is_null()); |
| 158 ASSERT_EQ(2u, array_data_view.size()); |
| 159 EXPECT_EQ(TestEnum::VALUE_1, array_data_view[0]); |
| 160 EXPECT_EQ(TestEnum::VALUE_0, array_data_view[1]); |
| 161 EXPECT_EQ(TestEnum::VALUE_0, *(array_data_view.data() + 1)); |
| 162 |
| 163 TestEnum output; |
| 164 ASSERT_TRUE(array_data_view.Read(0, &output)); |
| 165 EXPECT_EQ(TestEnum::VALUE_1, output); |
| 166 } |
| 167 |
| 168 TEST_F(DataViewTest, InterfaceArray) { |
| 169 TestInterfacePtr ptr; |
| 170 TestInterfaceImpl impl(GetProxy(&ptr)); |
| 171 |
| 172 TestStructPtr obj(TestStruct::New()); |
| 173 obj->f_interface_array.push_back(std::move(ptr)); |
| 174 |
| 175 auto data_view_holder = SerializeTestStruct(std::move(obj)); |
| 176 auto& data_view = *data_view_holder->data_view; |
| 177 |
| 178 ArrayDataView<TestInterfacePtr> array_data_view; |
| 179 data_view.GetFInterfaceArrayDataView(&array_data_view); |
| 180 |
| 181 ASSERT_FALSE(array_data_view.is_null()); |
| 182 ASSERT_EQ(1u, array_data_view.size()); |
| 183 |
| 184 TestInterfacePtr ptr2 = array_data_view.Take(0); |
| 185 ASSERT_TRUE(ptr2); |
| 186 int32_t result = 0; |
| 187 ASSERT_TRUE(ptr2->Echo(42, &result)); |
| 188 EXPECT_EQ(42, result); |
| 189 } |
| 190 |
| 191 TEST_F(DataViewTest, NestedArray) { |
| 192 TestStructPtr obj(TestStruct::New()); |
| 193 obj->f_nested_array = {{3, 4}, {2}}; |
| 194 |
| 195 auto data_view_holder = SerializeTestStruct(std::move(obj)); |
| 196 auto& data_view = *data_view_holder->data_view; |
| 197 |
| 198 ArrayDataView<ArrayDataView<int32_t>> array_data_view; |
| 199 data_view.GetFNestedArrayDataView(&array_data_view); |
| 200 |
| 201 ASSERT_FALSE(array_data_view.is_null()); |
| 202 ASSERT_EQ(2u, array_data_view.size()); |
| 203 |
| 204 ArrayDataView<int32_t> nested_array_data_view; |
| 205 array_data_view.GetDataView(0, &nested_array_data_view); |
| 206 ASSERT_FALSE(nested_array_data_view.is_null()); |
| 207 ASSERT_EQ(2u, nested_array_data_view.size()); |
| 208 EXPECT_EQ(4, nested_array_data_view[1]); |
| 209 |
| 210 std::vector<int32_t> vec; |
| 211 ASSERT_TRUE(array_data_view.Read(1, &vec)); |
| 212 ASSERT_EQ(1u, vec.size()); |
| 213 EXPECT_EQ(2, vec[0]); |
| 214 } |
| 215 |
| 216 TEST_F(DataViewTest, StructArray) { |
| 217 NestedStructPtr nested_struct(NestedStruct::New()); |
| 218 nested_struct->f_int32 = 42; |
| 219 |
| 220 TestStructPtr obj(TestStruct::New()); |
| 221 obj->f_struct_array.push_back(std::move(nested_struct)); |
| 222 |
| 223 auto data_view_holder = SerializeTestStruct(std::move(obj)); |
| 224 auto& data_view = *data_view_holder->data_view; |
| 225 |
| 226 ArrayDataView<NestedStructDataView> array_data_view; |
| 227 data_view.GetFStructArrayDataView(&array_data_view); |
| 228 |
| 229 ASSERT_FALSE(array_data_view.is_null()); |
| 230 ASSERT_EQ(1u, array_data_view.size()); |
| 231 |
| 232 NestedStructDataView struct_data_view; |
| 233 array_data_view.GetDataView(0, &struct_data_view); |
| 234 ASSERT_FALSE(struct_data_view.is_null()); |
| 235 EXPECT_EQ(42, struct_data_view.f_int32()); |
| 236 |
| 237 NestedStructPtr nested_struct2; |
| 238 ASSERT_TRUE(array_data_view.Read(0, &nested_struct2)); |
| 239 ASSERT_TRUE(nested_struct2); |
| 240 EXPECT_EQ(42, nested_struct2->f_int32); |
| 241 } |
| 242 |
| 243 TEST_F(DataViewTest, Map) { |
| 244 TestStructPtr obj(TestStruct::New()); |
| 245 obj->f_map["1"] = 1; |
| 246 obj->f_map["2"] = 2; |
| 247 |
| 248 auto data_view_holder = SerializeTestStruct(std::move(obj)); |
| 249 auto& data_view = *data_view_holder->data_view; |
| 250 |
| 251 MapDataView<StringDataView, int32_t> map_data_view; |
| 252 data_view.GetFMapDataView(&map_data_view); |
| 253 |
| 254 ASSERT_FALSE(map_data_view.is_null()); |
| 255 ASSERT_EQ(2u, map_data_view.size()); |
| 256 |
| 257 ASSERT_FALSE(map_data_view.keys().is_null()); |
| 258 ASSERT_EQ(2u, map_data_view.keys().size()); |
| 259 |
| 260 ASSERT_FALSE(map_data_view.values().is_null()); |
| 261 ASSERT_EQ(2u, map_data_view.values().size()); |
| 262 |
| 263 std::vector<std::string> keys; |
| 264 ASSERT_TRUE(map_data_view.ReadKeys(&keys)); |
| 265 std::vector<int32_t> values; |
| 266 ASSERT_TRUE(map_data_view.ReadValues(&values)); |
| 267 |
| 268 std::unordered_map<std::string, int32_t> map; |
| 269 for (size_t i = 0; i < 2; ++i) |
| 270 map[keys[i]] = values[i]; |
| 271 |
| 272 EXPECT_EQ(1, map["1"]); |
| 273 EXPECT_EQ(2, map["2"]); |
| 274 } |
| 275 |
| 276 TEST_F(DataViewTest, UnionArray) { |
| 277 TestUnionPtr union_ptr(TestUnion::New()); |
| 278 union_ptr->set_f_int32(1024); |
| 279 |
| 280 TestStructPtr obj(TestStruct::New()); |
| 281 obj->f_union_array.push_back(std::move(union_ptr)); |
| 282 |
| 283 auto data_view_holder = SerializeTestStruct(std::move(obj)); |
| 284 auto& data_view = *data_view_holder->data_view; |
| 285 |
| 286 ArrayDataView<TestUnionDataView> array_data_view; |
| 287 data_view.GetFUnionArrayDataView(&array_data_view); |
| 288 ASSERT_FALSE(array_data_view.is_null()); |
| 289 ASSERT_EQ(1u, array_data_view.size()); |
| 290 |
| 291 TestUnionDataView union_data_view; |
| 292 array_data_view.GetDataView(0, &union_data_view); |
| 293 ASSERT_FALSE(union_data_view.is_null()); |
| 294 |
| 295 TestUnionPtr union_ptr2; |
| 296 ASSERT_TRUE(array_data_view.Read(0, &union_ptr2)); |
| 297 ASSERT_TRUE(union_ptr2->is_f_int32()); |
| 298 EXPECT_EQ(1024, union_ptr2->get_f_int32()); |
| 299 } |
| 300 |
| 301 } // namespace data_view |
| 302 } // namespace test |
| 303 } // namespace mojo |
OLD | NEW |