2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <gtest/gtest.h>
18 #include <gmock/gmock.h>
22 #include "parcel/parcel.hh"
24 using ::testing::AtLeast;
25 using namespace tizen_base;
27 class ParcelCppTest : public ::testing::Test {
29 virtual void SetUp() {
32 virtual void TearDown() {
44 class Student : public Parcelable {
46 Student(std::string name, uint32_t age)
47 : name_(std::move(name)), age_(age) {
51 virtual ~Student() = default;
53 const std::string& GetName() const {
57 uint32_t GetAge() const {
61 virtual void WriteToParcel(Parcel* parcel) const {
62 parcel->WriteString(name_);
63 parcel->WriteUInt32(age_);
66 virtual void ReadFromParcel(Parcel* parcel) {
67 name_ = parcel->ReadString();
68 parcel->ReadUInt32(&age_);
76 TEST_F(ParcelCppTest, Ctor_AND_Dtor) {
80 TEST_F(ParcelCppTest, Write_AND_Read) {
81 char buf[1024] = { 128, };
82 GetHandle().Write(buf, sizeof(buf));
83 char read_buf[1024] = { 0, };
84 int ret = GetHandle().Read(read_buf, sizeof(read_buf));
85 ASSERT_EQ(ret, Parcel::Error::None);
86 ASSERT_EQ(buf[0], read_buf[0]);
89 TEST_F(ParcelCppTest, WriteBool_AND_ReadBool) {
90 GetHandle().WriteBool(true);
92 int ret = GetHandle().ReadBool(&val);
93 ASSERT_EQ(ret, Parcel::Error::None);
97 TEST_F(ParcelCppTest, WriteByte_AND_ReadByte) {
98 GetHandle().WriteByte(1);
100 int ret = GetHandle().ReadByte(&val);
101 ASSERT_EQ(ret, Parcel::Error::None);
105 TEST_F(ParcelCppTest, WriteUInt16_AND_ReadUInt16) {
106 GetHandle().WriteUInt16(1);
108 int ret = GetHandle().ReadUInt16(&val);
109 ASSERT_EQ(ret, Parcel::Error::None);
113 TEST_F(ParcelCppTest, WriteUInt32_AND_ReadUInt32) {
114 GetHandle().WriteUInt32(1);
116 int ret = GetHandle().ReadUInt32(&val);
117 ASSERT_EQ(ret, Parcel::Error::None);
121 TEST_F(ParcelCppTest, WriteUInt64_AND_ReadUInt64) {
122 GetHandle().WriteUInt64(1);
124 int ret = GetHandle().ReadUInt64(&val);
125 ASSERT_EQ(ret, Parcel::Error::None);
129 TEST_F(ParcelCppTest, WriteInt16_AND_ReadInt16) {
130 GetHandle().WriteInt16(1);
132 int ret = GetHandle().ReadInt16(&val);
133 ASSERT_EQ(ret, Parcel::Error::None);
137 TEST_F(ParcelCppTest, WriteInt32_AND_ReadInt32) {
138 GetHandle().WriteInt32(1);
140 int ret = GetHandle().ReadInt32(&val);
141 ASSERT_EQ(ret, Parcel::Error::None);
145 TEST_F(ParcelCppTest, WriteInt64_AND_ReadInt64) {
146 GetHandle().WriteInt64(1);
148 int ret = GetHandle().ReadInt64(&val);
149 ASSERT_EQ(ret, Parcel::Error::None);
153 TEST_F(ParcelCppTest, WriteFloat_AND_ReadFloat) {
154 GetHandle().WriteFloat(0.1f);
156 int ret = GetHandle().ReadFloat(&val);
157 ASSERT_EQ(ret, Parcel::Error::None);
158 ASSERT_EQ(val, 0.1f);
161 TEST_F(ParcelCppTest, WriteDouble_AND_ReadDouble) {
162 GetHandle().WriteDouble(0.1f);
164 int ret = GetHandle().ReadDouble(&val);
165 ASSERT_EQ(ret, Parcel::Error::None);
166 ASSERT_EQ(val, 0.1f);
169 TEST_F(ParcelCppTest, WriteString_AND_ReadString) {
170 GetHandle().WriteString("TestString");
171 auto val = GetHandle().ReadString();
172 ASSERT_EQ(get_last_result(), Parcel::Error::None);
173 ASSERT_EQ(val, "TestString");
176 TEST_F(ParcelCppTest, WriteCString_AND_ReadCString) {
177 GetHandle().WriteCString("TestCString");
179 int ret = GetHandle().ReadCString(&str);
180 auto ptr = std::unique_ptr<char, decltype(std::free)*>(str, std::free);
181 ASSERT_EQ(ret, Parcel::Error::None);
182 ASSERT_NE(str, nullptr);
183 ASSERT_EQ(std::string(str), "TestCString");
186 TEST_F(ParcelCppTest, WriteCBundle_AND_ReadCBundle) {
187 bundle* b = bundle_create();
188 bundle_add_str(b, "Key", "Value");
189 GetHandle().WriteCBundle(b);
191 bundle* val = nullptr;
192 int ret = GetHandle().ReadCBundle(&val);
193 auto ptr = std::unique_ptr<bundle, decltype(bundle_free)*>(val, bundle_free);
194 ASSERT_EQ(ret, Parcel::Error::None);
195 ASSERT_NE(val, nullptr);
197 bundle_get_str(b, "Key", &str);
198 ASSERT_EQ(std::string(str), "Value");
201 TEST_F(ParcelCppTest, WriteBundle_AND_ReadBundle) {
203 b.Add("Key", "Value");
204 GetHandle().WriteBundle(b);
205 auto val = GetHandle().ReadBundle();
206 ASSERT_EQ(get_last_result(), Parcel::Error::None);
207 auto str = val.GetString("Key");
208 ASSERT_EQ(str, "Value");
211 TEST_F(ParcelCppTest, GetRaw) {
212 GetHandle().WriteInt32(0);
213 auto& raw = GetHandle().GetRaw();
214 ASSERT_EQ(raw.size(), sizeof(uint32_t) + sizeof(int32_t));
217 TEST_F(ParcelCppTest, ResetReader) {
218 GetHandle().WriteInt32(1);
219 GetHandle().WriteInt32(2);
221 int ret = GetHandle().ReadInt32(&val);
222 ASSERT_EQ(ret, Parcel::Error::None);
224 GetHandle().ResetReader();
226 ret = GetHandle().ReadInt32(&val);
227 ASSERT_EQ(ret, Parcel::Error::None);
231 TEST_F(ParcelCppTest, Clear) {
232 GetHandle().WriteInt32(1);
235 int ret = GetHandle().ReadInt32(&val);
236 ASSERT_NE(ret, Parcel::Error::None);
240 TEST_F(ParcelCppTest, Reset) {
241 GetHandle().WriteByte(1);
242 char buf[1024] = { 128, };
243 GetHandle().Reset(buf, sizeof(buf));
244 char read_buf[1024] = { 0, };
245 int ret = GetHandle().Read(read_buf, sizeof(read_buf));
246 ASSERT_EQ(ret, Parcel::Error::None);
247 ASSERT_EQ(buf[0], read_buf[0]);
250 TEST_F(ParcelCppTest, WriteParcelable_AND_ReadParcelable) {
252 GetHandle().WriteParcelable(s1);
254 GetHandle().ReadParcelable(&s2);
255 ASSERT_EQ(s1.GetName(), s2.GetName());
256 ASSERT_EQ(s1.GetAge(), s2.GetAge());
259 TEST_F(ParcelCppTest, IsEmpty) {
260 ASSERT_EQ(GetHandle().IsEmpty(), true);
261 GetHandle().WriteInt32(1);
262 ASSERT_EQ(GetHandle().IsEmpty(), false);