1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17 #include "light_refbase.h"
18 #include "base_def.h"
19 #include "base_object.h"
20 #include "bool_wrapper.h"
21 #include "zchar_wrapper.h"
22 #include "byte_wrapper.h"
23 #include "user_object_wrapper.h"
24 #include "short_wrapper.h"
25 #include "int_wrapper.h"
26 #include "long_wrapper.h"
27 #include "float_wrapper.h"
28 #include "double_wrapper.h"
29 #include "string_wrapper.h"
30 #include "refbase.h"
31
32 using namespace OHOS;
33 using namespace OHOS::AAFwk;
34 using testing::ext::TestSize;
35
36 namespace OHOS {
37 namespace AAFwk {
38 class AAFwkBaseObjectTest : public testing::Test {
39 public:
40 static void SetUpTestCase(void);
41 static void TearDownTestCase(void);
42 void SetUp();
43 void TearDown();
44 };
45
SetUpTestCase(void)46 void AAFwkBaseObjectTest::SetUpTestCase(void)
47 {}
48
TearDownTestCase(void)49 void AAFwkBaseObjectTest::TearDownTestCase(void)
50 {}
51
SetUp(void)52 void AAFwkBaseObjectTest::SetUp(void)
53 {}
54
TearDown(void)55 void AAFwkBaseObjectTest::TearDown(void)
56 {}
57
58 /**
59 * @tc.number: BaseObject_test_001
60 * @tc.name: Query
61 * @tc.desc: test Query function and iid == g_IID_IWeakReferenceSource.
62 */
63 HWTEST_F(AAFwkBaseObjectTest, BaseObject_test_001, TestSize.Level1)
64 {
65 Object object;
66 InterfaceID iid = {
67 0xbc3f5250, 0x34d7, 0x42d2, 0x9b40, {0xf, 0xf, 0xc, 0xe, 0x8, 0x3, 0xf, 0xd, 0x4, 0x0, 0x6, 0x1}
68 };
69 EXPECT_TRUE(object.Query(iid));
70 }
71
72 /**
73 * @tc.number: BaseObject_test_002
74 * @tc.name: Query
75 * @tc.desc: test Query function and iid == g_IID_IInterface.
76 */
77 HWTEST_F(AAFwkBaseObjectTest, BaseObject_test_002, TestSize.Level1)
78 {
79 Object object;
80 InterfaceID iid = {
81 0x00000000, 0x0000, 0x0000, 0x0000, {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1}
82 };
83 EXPECT_TRUE(object.Query(iid));
84 }
85
86 /**
87 * @tc.number: BaseObject_test_003
88 * @tc.name: GetInterfaceID
89 * @tc.desc: test GetInterfaceID function and object == static_cast<IObject *>(this).
90 */
91 HWTEST_F(AAFwkBaseObjectTest, BaseObject_test_003, TestSize.Level1)
92 {
93 Object object;
94 InterfaceID iid = {
95 0x8321f710, 0xa0c0, 0x4cbe, 0xbfbc, {0x5, 0xa, 0x7, 0x8, 0xf, 0x1, 0x3, 0x1, 0x2, 0xb, 0x1, 0xb}
96 };
97 EXPECT_EQ(iid, object.GetInterfaceID(object.Query(iid)));
98 }
99
100 /**
101 * @tc.number: BaseObject_test_004
102 * @tc.name: GetInterfaceID
103 * @tc.desc: test GetInterfaceID function and object == static_cast<IWeakReferenceSource *>(this).
104 */
105 HWTEST_F(AAFwkBaseObjectTest, BaseObject_test_004, TestSize.Level1)
106 {
107 Object object;
108 InterfaceID iid = {
109 0xbc3f5250, 0x34d7, 0x42d2, 0x9b40, {0xf, 0xf, 0xc, 0xe, 0x8, 0x3, 0xf, 0xd, 0x4, 0x0, 0x6, 0x1}
110 };
111 EXPECT_EQ(iid, object.GetInterfaceID(object.Query(iid)));
112 }
113
114 /**
115 * @tc.number: BaseObject_test_005
116 * @tc.name: GetInterfaceID
117 * @tc.desc: test GetInterfaceID function and object is Any value.
118 */
119 HWTEST_F(AAFwkBaseObjectTest, BaseObject_test_005, TestSize.Level1)
120 {
121 Object object;
122 InterfaceID iid = {
123 0x26ab1978, 0x1d11, 0x4a4f, 0x826d, {0x6, 0x1, 0x7, 0x8, 0x5, 0xc, 0x0, 0x4, 0x8, 0xc, 0xc, 0xa}
124 };
125 EXPECT_EQ(InterfaceID::Empty, object.GetInterfaceID(object.Query(iid)));
126 }
127
128
129 /**
130 * @tc.number: BaseObject_test_006
131 * @tc.name: GetWeakReference
132 * @tc.desc: test GetWeakReference function and addr is not nullptr.
133 */
134 HWTEST_F(AAFwkBaseObjectTest, BaseObject_test_006, TestSize.Level1)
135 {
136 Object object;
137 sptr<IWeakReference> weakRef = nullptr;
138 EXPECT_EQ(ERR_OK, object.GetWeakReference(weakRef));
139 }
140
141 /**
142 * @tc.number: BaseObject_test_007
143 * @tc.name: ToString
144 * @tc.desc: test ToString function.
145 */
146 HWTEST_F(AAFwkBaseObjectTest, BaseObject_test_007, TestSize.Level1)
147 {
148 Object object;
149 object.ToString();
150 }
151
152 /**
153 * @tc.number: UserObject_test_001
154 * @tc.name: ToString
155 * @tc.desc: test ToString function and value_ is nullptr.
156 */
157 HWTEST_F(AAFwkBaseObjectTest, UserObject_test_001, TestSize.Level1)
158 {
159 UserObject userObject(nullptr);
160 EXPECT_EQ("", userObject.ToString());
161 }
162
163 /**
164 * @tc.number: UserObject_test_002
165 * @tc.name: Box
166 * @tc.desc: test Box function and value is nullptr.
167 */
168 HWTEST_F(AAFwkBaseObjectTest, UserObject_test_002, TestSize.Level1)
169 {
170 UserObject userObject(nullptr);
171 EXPECT_EQ(nullptr, userObject.Box(nullptr));
172 }
173
174 /**
175 * @tc.number: UserObject_test_003
176 * @tc.name: Unbox
177 * @tc.desc: test Unbox function and object is nullptr.
178 */
179 HWTEST_F(AAFwkBaseObjectTest, UserObject_test_003, TestSize.Level1)
180 {
181 UserObject userObject(nullptr);
182 EXPECT_EQ(nullptr, userObject.Unbox(nullptr));
183 }
184
185 /**
186 * @tc.number: UserObject_test_004
187 * @tc.name: Parse
188 * @tc.desc: test Parse function and str is empty.
189 */
190 HWTEST_F(AAFwkBaseObjectTest, UserObject_test_004, TestSize.Level1)
191 {
192 UserObject userObject(nullptr);
193 std::string str = "";
194 EXPECT_EQ(nullptr, userObject.Parse(str));
195 }
196
197 /**
198 * @tc.number: UserObject_test_005
199 * @tc.name: Parse
200 * @tc.desc: test Parse function and SPLIT is does not exist.
201 */
202 HWTEST_F(AAFwkBaseObjectTest, UserObject_test_005, TestSize.Level1)
203 {
204 UserObject userObject(nullptr);
205 std::string str = "acd";
206 EXPECT_EQ(nullptr, userObject.Parse(str));
207 }
208
209 /**
210 * @tc.number: UserObject_test_006
211 * @tc.name: Parse
212 * @tc.desc: test Parse function and SPLIT is exist.
213 */
214 HWTEST_F(AAFwkBaseObjectTest, UserObject_test_006, TestSize.Level1)
215 {
216 UserObject userObject(nullptr);
217 std::string str = "a#cd";
218 EXPECT_EQ(nullptr, userObject.Parse(str));
219 }
220
221 /**
222 * @tc.number: UserObject_test_007
223 * @tc.name: Parse
224 * @tc.desc: test Parse function and str == "true".
225 */
226 HWTEST_F(AAFwkBaseObjectTest, UserObject_test_007, TestSize.Level1)
227 {
228 Boolean boolean(true);
229 std::string str = "true";
230 boolean.Parse(str);
231 EXPECT_EQ("true", boolean.ToString());
232 }
233 } // namespace AAFwk
234 } // namespace OHOS
235