1 /*
2 * Copyright (c) 2021 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
18 #include "ohos/aafwk/content/skills.h"
19 #include "ohos/aafwk/base/string_wrapper.h"
20 #include "ohos/aafwk/base/bool_wrapper.h"
21 #include "ohos/aafwk/base/int_wrapper.h"
22 #include "ohos/aafwk/base/long_wrapper.h"
23 #include "utils/native/base/include/refbase.h"
24
25 using namespace testing::ext;
26 using namespace OHOS::AAFwk;
27 using OHOS::Parcel;
28
29 namespace OHOS {
30 namespace AAFwk {
31 static const int LARGE_STR_LEN = 65534;
32 static const int SET_COUNT = 20;
33 class SkillsBaseTest : public testing::Test {
34 public:
SkillsBaseTest()35 SkillsBaseTest()
36 {}
~SkillsBaseTest()37 ~SkillsBaseTest()
38 {}
39 static void SetUpTestCase(void);
40 static void TearDownTestCase(void);
41 void SetUp();
42 void TearDown();
43
44 std::shared_ptr<Skills> base_ = nullptr;
45 void CompareSkills(const std::shared_ptr<Skills> &skills1, const std::shared_ptr<Skills> &skills2) const;
46 };
47
SetUpTestCase(void)48 void SkillsBaseTest::SetUpTestCase(void)
49 {}
50
TearDownTestCase(void)51 void SkillsBaseTest::TearDownTestCase(void)
52 {}
53
SetUp(void)54 void SkillsBaseTest::SetUp(void)
55 {
56 base_ = std::make_shared<Skills>();
57 }
58
TearDown(void)59 void SkillsBaseTest::TearDown(void)
60 {}
61
62 /**
63 * @tc.number: AaFwk_Skills_Parcelable_0100
64 * @tc.name: Marshalling/Unmarshalling
65 * @tc.desc: marshalling Skills, and then check result.
66 */
67 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Parcelable_0100, Function | MediumTest | Level1)
68 {
69 std::shared_ptr<Skills> SkillsIn_ = std::make_shared<Skills>();
70 if (SkillsIn_ == nullptr) {
71 return;
72 }
73 SkillsIn_->AddEntity("12345");
74 SkillsIn_->AddAction("12345");
75 SkillsIn_->AddAuthority("12345");
76 SkillsIn_->AddScheme("12345");
77 SkillsIn_->AddPath("12345");
78 SkillsIn_->AddSchemeSpecificPart("12345");
79 SkillsIn_->AddType("12345");
80 WantParams wantParams;
81 std::string keyStr = "12345667";
82 bool valueBool = true;
83 wantParams.SetParam(keyStr, Boolean::Box(valueBool));
84 SkillsIn_->SetWantParams(wantParams);
85
86 Parcel in;
87 std::shared_ptr<Skills> SkillsOut_(Skills::Unmarshalling(in));
88 SkillsIn_->Marshalling(in);
89 if (SkillsOut_ != nullptr) {
90 CompareSkills(SkillsIn_, SkillsOut_);
91 EXPECT_EQ(valueBool, Boolean::Unbox(IBoolean::Query(SkillsOut_->GetWantParams().GetParam(keyStr))));
92 }
93 }
94
95 /**
96 * @tc.number: AaFwk_Skills_Parcelable_0200
97 * @tc.name: Marshalling/Unmarshalling
98 * @tc.desc: marshalling Skills, and then check result.
99 */
100 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Parcelable_0200, Function | MediumTest | Level1)
101 {
102 std::shared_ptr<Skills> SkillsIn_ = std::make_shared<Skills>();
103 if (SkillsIn_ == nullptr) {
104 return;
105 }
106
107 SkillsIn_->AddEntity("@#¥#3243adsafdf_中文");
108 SkillsIn_->AddAction("@#¥#3243adsafdf_中文");
109 SkillsIn_->AddAuthority("@#¥#3243adsafdf_中文");
110 SkillsIn_->AddScheme("@#¥#3243adsafdf_中文");
111 SkillsIn_->AddPath("@#¥#3243adsafdf_中文");
112 SkillsIn_->AddSchemeSpecificPart("@#¥#3243adsafdf_中文");
113 SkillsIn_->AddType("@#¥#3243adsafdf_中文");
114 WantParams wantParams;
115 std::string keyStr = "@#¥#3243adsafdf_中文";
116 long valueLong = 12345L;
117 wantParams.SetParam(keyStr, Long::Box(valueLong));
118 EXPECT_EQ(valueLong, Long::Unbox(ILong::Query(wantParams.GetParam(keyStr))));
119
120 SkillsIn_->SetWantParams(wantParams);
121
122 Parcel in;
123 SkillsIn_->Marshalling(in);
124 std::shared_ptr<Skills> SkillsOut_(Skills::Unmarshalling(in));
125
126 if (SkillsOut_ != nullptr) {
127 CompareSkills(SkillsIn_, SkillsOut_);
128 std::string result = String::Unbox(IString::Query(SkillsOut_->GetWantParams().GetParam(keyStr)));
129 EXPECT_STREQ(std::to_string(valueLong).c_str(), result.c_str());
130 }
131 }
132
133 /**
134 * @tc.number: AaFwk_Skills_Parcelable_0300
135 * @tc.name: Marshalling/Unmarshalling
136 * @tc.desc: marshalling Skills, and then check result.
137 */
138 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Parcelable_0300, Function | MediumTest | Level1)
139 {
140 std::shared_ptr<Skills> SkillsIn_ = std::make_shared<Skills>();
141 if (SkillsIn_ == nullptr) {
142 return;
143 }
144
145 SkillsIn_->AddEntity("");
146 SkillsIn_->AddAction("");
147 SkillsIn_->AddAuthority("");
148 SkillsIn_->AddScheme("");
149 SkillsIn_->AddPath("");
150 SkillsIn_->AddSchemeSpecificPart("");
151 SkillsIn_->AddType("");
152 WantParams wantParams;
153 std::string keyStr = "";
154 int valueInt = 123;
155 wantParams.SetParam(keyStr, Integer::Box(valueInt));
156
157 SkillsIn_->SetWantParams(wantParams);
158 EXPECT_EQ(valueInt, Integer::Unbox(IInteger::Query(wantParams.GetParam(keyStr))));
159
160 Parcel in;
161 SkillsIn_->Marshalling(in);
162 std::shared_ptr<Skills> SkillsOut_(Skills::Unmarshalling(in));
163
164 if (SkillsOut_ != nullptr) {
165 CompareSkills(SkillsIn_, SkillsOut_);
166 EXPECT_EQ(valueInt, Integer::Unbox(IInteger::Query(SkillsOut_->GetWantParams().GetParam(keyStr))));
167 }
168 }
169
170 /**
171 * @tc.number: AaFwk_Skills_Parcelable_0400
172 * @tc.name: Marshalling/Unmarshalling
173 * @tc.desc: marshalling Skills, and then check result.
174 */
175 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Parcelable_0400, Function | MediumTest | Level1)
176 {
177 std::shared_ptr<Skills> SkillsIn_ = std::make_shared<Skills>();
178 if (SkillsIn_ == nullptr) {
179 return;
180 }
181 SkillsIn_->AddEntity("12345");
182 SkillsIn_->AddAction("12345");
183 SkillsIn_->AddAuthority("12345");
184 SkillsIn_->AddScheme("12345");
185 SkillsIn_->AddPath("12345");
186 SkillsIn_->AddSchemeSpecificPart("12345");
187 SkillsIn_->AddType("12345");
188 SkillsIn_->AddEntity("@#¥#3243adsafdf_中文");
189 SkillsIn_->AddAction("@#¥#3243adsafdf_中文");
190 SkillsIn_->AddAuthority("@#¥#3243adsafdf_中文");
191 SkillsIn_->AddScheme("@#¥#3243adsafdf_中文");
192 SkillsIn_->AddPath("@#¥#3243adsafdf_中文");
193 SkillsIn_->AddSchemeSpecificPart("@#¥#3243adsafdf_中文");
194 SkillsIn_->AddType("@#¥#3243adsafdf_中文");
195 SkillsIn_->AddEntity("");
196 SkillsIn_->AddAction("");
197 SkillsIn_->AddAuthority("");
198 SkillsIn_->AddScheme("");
199 SkillsIn_->AddPath("");
200 SkillsIn_->AddSchemeSpecificPart("");
201 SkillsIn_->AddType("");
202 WantParams wantParams;
203 std::string keyStr = "12345667";
204 std::string valueString = "123";
205 wantParams.SetParam(keyStr, String::Box(valueString));
206 SkillsIn_->SetWantParams(wantParams);
207
208 Parcel in;
209 SkillsIn_->Marshalling(in);
210 std::shared_ptr<Skills> SkillsOut_(Skills::Unmarshalling(in));
211
212 if (SkillsOut_ != nullptr) {
213 CompareSkills(SkillsIn_, SkillsOut_);
214 EXPECT_EQ(valueString, String::Unbox(IString::Query(SkillsOut_->GetWantParams().GetParam(keyStr))));
215 }
216 }
217
CompareSkills(const std::shared_ptr<Skills> & skills1,const std::shared_ptr<Skills> & skills2) const218 void SkillsBaseTest::CompareSkills(const std::shared_ptr<Skills> &skills1, const std::shared_ptr<Skills> &skills2) const
219 {
220 EXPECT_EQ(skills1->CountEntities(), skills2->CountEntities());
221 EXPECT_EQ(skills1->CountActions(), skills2->CountActions());
222 EXPECT_EQ(skills1->CountAuthorities(), skills2->CountAuthorities());
223 EXPECT_EQ(skills1->CountSchemes(), skills2->CountSchemes());
224 EXPECT_EQ(skills1->CountPaths(), skills2->CountPaths());
225 EXPECT_EQ(skills1->CountSchemeSpecificParts(), skills2->CountSchemeSpecificParts());
226 EXPECT_EQ(skills1->CountTypes(), skills2->CountTypes());
227
228 int count = skills1->CountEntities();
229 for (int i = 0; i < count; i++) {
230 EXPECT_EQ(skills1->GetEntity(i), skills1->GetEntity(i));
231 }
232 count = skills1->CountActions();
233 for (int i = 0; i < count; i++) {
234 EXPECT_EQ(skills1->GetAction(i), skills1->GetAction(i));
235 }
236 count = skills1->CountAuthorities();
237 for (int i = 0; i < count; i++) {
238 EXPECT_EQ(skills1->GetAuthority(i), skills1->GetAuthority(i));
239 }
240 count = skills1->CountSchemes();
241 for (int i = 0; i < count; i++) {
242 EXPECT_EQ(skills1->GetScheme(i), skills1->GetScheme(i));
243 }
244 count = skills1->CountPaths();
245 for (int i = 0; i < count; i++) {
246 EXPECT_EQ(skills1->GetPath(i), skills1->GetPath(i));
247 }
248 count = skills1->CountSchemeSpecificParts();
249 for (int i = 0; i < count; i++) {
250 EXPECT_EQ(skills1->GetSchemeSpecificPart(i), skills1->GetSchemeSpecificPart(i));
251 }
252 count = skills1->CountTypes();
253 for (int i = 0; i < count; i++) {
254 EXPECT_EQ(skills1->GetType(i), skills1->GetType(i));
255 }
256
257 std::set<std::string> key1;
258 std::set<std::string> key2;
259 key1 = skills1->GetWantParams().KeySet();
260 key2 = skills2->GetWantParams().KeySet();
261 EXPECT_EQ(key1.size(), key2.size());
262
263 if (key1.size() > 0 && key2.size() > 0) {
264 std::set<std::string>::iterator iter1 = key1.begin();
265 std::set<std::string>::iterator iter2 = key2.begin();
266 for (; (iter1 != key1.end() && iter2 != key2.end()); iter1++, iter2++) {
267 EXPECT_EQ(*iter1, *iter2);
268 }
269 }
270 }
271
272 /**
273 * @tc.number: AaFwk_Skills_Entities_0100
274 * @tc.name: CountEntitie/HasEntity/GetEntity
275 * @tc.desc: Verify the function when the input string contains special characters.
276 */
277 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Entities_0100, Function | MediumTest | Level1)
278 {
279 std::string empty;
280 std::string entities = "entities.system.test";
281 EXPECT_EQ(0, base_->CountEntities());
282 EXPECT_EQ(false, base_->HasEntity(entities));
283 EXPECT_EQ(empty, base_->GetEntity(0));
284
285 base_->RemoveEntity(entities);
286 EXPECT_EQ(0, base_->CountEntities());
287 EXPECT_EQ(false, base_->HasEntity(entities));
288 }
289 /**
290 * @tc.number: AaFwk_Skills_GetEntities_0100
291 * @tc.name: AddEntity and GetEntities
292 * @tc.desc: Verify AddEntity and GetEntities.
293 */
294 HWTEST_F(SkillsBaseTest, AaFwk_Skills_GetEntities_0100, Function | MediumTest | Level1)
295 {
296
297 std::string entity = "12345667";
298 base_->AddEntity(entity);
299
300 size_t length = base_->GetEntities().size();
301
302 EXPECT_EQ((size_t)1, length);
303 EXPECT_EQ(entity, base_->GetEntities().at(0));
304 }
305
306 /**
307 * @tc.number: AaFwk_Skills_Authorities_0100
308 * @tc.name: CountEntitie/HasEntity/GetEntity
309 * @tc.desc: Verify the function when the input string has a long size.
310 */
311 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Authorities_0100, Function | MediumTest | Level1)
312 {
313 std::string empty;
314 std::string authorities = "authorities.system.test";
315 EXPECT_EQ(0, base_->CountAuthorities());
316 EXPECT_EQ(false, base_->HasAuthority(authorities));
317 EXPECT_EQ(empty, base_->GetAuthority(0));
318
319 base_->RemoveAuthority(authorities);
320 EXPECT_EQ(0, base_->CountAuthorities());
321 EXPECT_EQ(false, base_->HasAuthority(authorities));
322 }
323
324 /**
325 * @tc.number: AaFwk_Skills_Path_0300
326 * @tc.name: CountPaths/HasPath/GetPath
327 * @tc.desc: Verify the function when the input string is overrided.
328 */
329 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Path_0300, Function | MediumTest | Level1)
330 {
331 std::string empty;
332 std::string path = "paths.system.test";
333 PatternsMatcher pm(path, MatchType::DEFAULT);
334 base_->AddPath(pm);
335
336 EXPECT_EQ(1, base_->CountPaths());
337 EXPECT_EQ(true, base_->HasPath(path));
338 EXPECT_EQ(path, base_->GetPath(0));
339
340 base_->RemovePath(pm);
341 EXPECT_EQ(0, base_->CountPaths());
342 EXPECT_EQ(false, base_->HasPath(path));
343 }
344
345 /**
346 * @tc.number: AaFwk_Skills_Action_0100
347 * @tc.name: AddAction/CountActions/HasAction/GetAction
348 * @tc.desc: Verify the function when the input string is set 20 times.
349 */
350 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Action_0100, Function | MediumTest | Level1)
351 {
352 std::string empty;
353 std::string action = "action.system.test";
354 int actionCount = 1;
355
356 for (int i = 0; i < SET_COUNT; i++) {
357 base_->AddAction(action);
358 }
359
360 EXPECT_EQ(actionCount, base_->CountActions());
361 EXPECT_EQ(true, base_->HasAction(action));
362 EXPECT_EQ(action, base_->GetAction(0));
363
364 base_->RemoveAction(action);
365 EXPECT_EQ(0, base_->CountActions());
366 EXPECT_EQ(false, base_->HasAction(action));
367 }
368
369 /**
370 * @tc.number: AaFwk_Skills_Entity_0100
371 * @tc.name: CountEntities/HasEntity/CountEntities/GetEntity
372 * @tc.desc: Verify the function when the input string is default.
373 */
374 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Entity_0100, Function | MediumTest | Level1)
375 {
376 std::string empty;
377 std::string entity = "entity.system.test";
378 int entityCount = 1;
379
380 for (int i = 0; i < SET_COUNT; i++) {
381 base_->AddEntity(entity);
382 }
383
384 EXPECT_EQ(entityCount, base_->CountEntities());
385 EXPECT_EQ(true, base_->HasEntity(entity));
386 EXPECT_EQ(entity, base_->GetEntity(0));
387
388 base_->RemoveEntity(entity);
389 EXPECT_EQ(0, base_->CountEntities());
390 EXPECT_EQ(false, base_->HasEntity(entity));
391 }
392
393 /**
394 * @tc.number: AaFwk_Skills_Authority_0100
395 * @tc.name: CountAuthorities/HasAuthority/GetAuthority
396 * @tc.desc: Verify the function when the input string contains special characters.
397 */
398 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Authority_0100, Function | MediumTest | Level1)
399 {
400 std::string empty;
401 std::string authority = "Authority.system.test";
402 int authorityCount = 1;
403
404 for (int i = 0; i < SET_COUNT; i++) {
405 base_->AddAuthority(authority);
406 }
407
408 EXPECT_EQ(authorityCount, base_->CountAuthorities());
409 EXPECT_EQ(true, base_->HasAuthority(authority));
410 EXPECT_EQ(authority, base_->GetAuthority(0));
411
412 base_->RemoveAuthority(authority);
413 EXPECT_EQ(0, base_->CountAuthorities());
414 EXPECT_EQ(false, base_->HasAuthority(authority));
415 }
416
417 /**
418 * @tc.number: AaFwk_Skills_Path_0100
419 * @tc.name: CountPaths/HasPath/GetPath
420 * @tc.desc: Verify the function when the input string contains special characters.
421 */
422 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Path_0100, Function | MediumTest | Level1)
423 {
424 std::string empty;
425 std::string path = "Path.system.test";
426 int pathCount = 1;
427
428 for (int i = 0; i < SET_COUNT; i++) {
429 base_->AddPath(path);
430 }
431
432 EXPECT_EQ(pathCount, base_->CountPaths());
433 EXPECT_EQ(true, base_->HasPath(path));
434 EXPECT_EQ(path, base_->GetPath(0));
435
436 base_->RemovePath(path);
437 EXPECT_EQ(0, base_->CountPaths());
438 EXPECT_EQ(false, base_->HasPath(path));
439 }
440
441 /**
442 * @tc.number: AaFwk_Skills_Scheme_0100
443 * @tc.name: CountSchemes/HasScheme/GetScheme
444 * @tc.desc: Verify the function when the input string contains special characters.
445 */
446 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Scheme_0100, Function | MediumTest | Level1)
447 {
448 std::string empty;
449 std::string scheme = "scheme.system.test";
450 int schemeCount = 1;
451
452 for (int i = 0; i < SET_COUNT; i++) {
453 base_->AddScheme(scheme);
454 }
455
456 EXPECT_EQ(schemeCount, base_->CountSchemes());
457 EXPECT_EQ(true, base_->HasScheme(scheme));
458 EXPECT_EQ(scheme, base_->GetScheme(0));
459
460 base_->RemoveScheme(scheme);
461 EXPECT_EQ(0, base_->CountSchemes());
462 EXPECT_EQ(false, base_->HasScheme(scheme));
463 }
464
465 /**
466 * @tc.number: AaFwk_Skills_SchemeSpecificPart_0100
467 * @tc.name: CountSchemeSpecificParts/HasSchemeSpecificPart/GetSchemeSpecificPart
468 * @tc.desc: Verify the function when the input string contains special characters.
469 */
470 HWTEST_F(SkillsBaseTest, AaFwk_Skills_SchemeSpecificPart_0100, Function | MediumTest | Level1)
471 {
472 std::string empty;
473 std::string schemespecificpart = "schemespecificpart.system.test";
474 int schemespecificpartCount = 1;
475
476 for (int i = 0; i < SET_COUNT; i++) {
477 base_->AddSchemeSpecificPart(schemespecificpart);
478 }
479
480 EXPECT_EQ(schemespecificpartCount, base_->CountSchemeSpecificParts());
481 EXPECT_EQ(true, base_->HasSchemeSpecificPart(schemespecificpart));
482 EXPECT_EQ(schemespecificpart, base_->GetSchemeSpecificPart(0));
483
484 base_->RemoveSchemeSpecificPart(schemespecificpart);
485 EXPECT_EQ(0, base_->CountSchemeSpecificParts());
486 EXPECT_EQ(false, base_->HasSchemeSpecificPart(schemespecificpart));
487 }
488
489 /**
490 * @tc.number: AaFwk_Skills_Type_0100
491 * @tc.name: CountTypes/HasType/GetType
492 * @tc.desc: Verify the function when the input string contains special characters.
493 */
494 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Type_0100, Function | MediumTest | Level1)
495 {
496 std::string empty;
497 std::string type = "type/system.test";
498 int typeCount = 1;
499
500 for (int i = 0; i < SET_COUNT; i++) {
501 base_->AddType(type);
502 }
503
504 EXPECT_EQ(typeCount, base_->CountTypes());
505 EXPECT_EQ(true, base_->HasType(type));
506 EXPECT_EQ(type, base_->GetType(0));
507
508 base_->RemoveType(type);
509 EXPECT_EQ(0, base_->CountTypes());
510 EXPECT_EQ(false, base_->HasType(type));
511 }
512
513 /**
514 * @tc.number: AaFwk_Skills_Actions_0100
515 * @tc.name: CountActions/HasAuthority/GetAuthority
516 * @tc.desc: Verify the function when the input string contains special characters.
517 */
518 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Actions_0100, Function | MediumTest | Level1)
519 {
520 std::string empty;
521 std::string actions = "actions.system.test";
522 EXPECT_EQ(0, base_->CountActions());
523 EXPECT_EQ(false, base_->HasAuthority(actions));
524 EXPECT_EQ(empty, base_->GetAuthority(0));
525
526 base_->RemoveAuthority(actions);
527 EXPECT_EQ(0, base_->CountActions());
528 EXPECT_EQ(false, base_->HasAuthority(actions));
529 }
530
531 /**
532 * @tc.number: AaFwk_Skills_Schemes_0100
533 * @tc.name: CountSchemes/HasAuthority/GetAuthority
534 * @tc.desc: Verify the function when the input string contains special characters.
535 */
536 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Schemes_0100, Function | MediumTest | Level1)
537 {
538 std::string empty;
539 std::string schemes = "schemes.system.test";
540 EXPECT_EQ(0, base_->CountSchemes());
541 EXPECT_EQ(false, base_->HasAuthority(schemes));
542 EXPECT_EQ(empty, base_->GetAuthority(0));
543
544 base_->RemoveAuthority(schemes);
545 EXPECT_EQ(0, base_->CountSchemes());
546 EXPECT_EQ(false, base_->HasAuthority(schemes));
547 }
548
549 /**
550 * @tc.number: AaFwk_Skills_SchemeSpecificParts_0100
551 * @tc.name: CountSchemeSpecificParts/HasAuthority/GetAuthority
552 * @tc.desc: Verify the function when the input string contains special characters.
553 */
554 HWTEST_F(SkillsBaseTest, AaFwk_Skills_SchemeSpecificParts_0100, Function | MediumTest | Level1)
555 {
556 std::string empty;
557 std::string schemespecificparts = "schemespecificparts.system.test";
558 EXPECT_EQ(0, base_->CountSchemeSpecificParts());
559 EXPECT_EQ(false, base_->HasAuthority(schemespecificparts));
560 EXPECT_EQ(empty, base_->GetAuthority(0));
561
562 base_->RemoveAuthority(schemespecificparts);
563 EXPECT_EQ(0, base_->CountSchemeSpecificParts());
564 EXPECT_EQ(false, base_->HasAuthority(schemespecificparts));
565 }
566
567 /**
568 * @tc.number: AaFwk_Skills_Types_0100
569 * @tc.name: CountTypes/HasAuthority/GetAuthority
570 * @tc.desc: Verify the function when the input string contains special characters.
571 */
572 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Types_0100, Function | MediumTest | Level1)
573 {
574 std::string empty;
575 std::string types = "types.system.test";
576 GTEST_LOG_(INFO) << "---------------a ";
577 EXPECT_EQ(0, base_->CountTypes());
578 GTEST_LOG_(INFO) << "---------------b ";
579 EXPECT_EQ(false, base_->HasAuthority(types));
580 GTEST_LOG_(INFO) << "---------------1 ";
581 EXPECT_EQ(empty, base_->GetAuthority(0));
582 GTEST_LOG_(INFO) << "---------------2 ";
583
584 base_->RemoveAuthority(types);
585 EXPECT_EQ(0, base_->CountTypes());
586 EXPECT_EQ(false, base_->HasAuthority(types));
587 }
588
589 /**
590 * @tc.number: AaFwk_Skills_Action_0200
591 * @tc.name: CountActions/HasAction/GetAction
592 * @tc.desc: Verify the function when action is not exist.
593 */
594 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Action_0200, Function | MediumTest | Level1)
595 {
596 std::string empty;
597 std::string action = "action.system.test";
598 EXPECT_EQ(0, base_->CountActions());
599 EXPECT_EQ(false, base_->HasAction(action));
600 EXPECT_EQ(empty, base_->GetAction(0));
601
602 base_->RemoveAction(action);
603 EXPECT_EQ(0, base_->CountActions());
604 EXPECT_EQ(false, base_->HasAction(action));
605 }
606
607 /**
608 * @tc.number: AaFwk_Skills_Authority_0200
609 * @tc.name: CountAuthorities/HasAuthority/GetAuthority
610 * @tc.desc: Verify the function when action is not exist.
611 */
612 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Authority_0200, Function | MediumTest | Level1)
613 {
614 std::string empty;
615 std::string authority = "authority.system.test";
616 EXPECT_EQ(0, base_->CountAuthorities());
617 EXPECT_EQ(false, base_->HasAuthority(authority));
618 EXPECT_EQ(empty, base_->GetAuthority(0));
619
620 base_->RemoveAuthority(authority);
621 EXPECT_EQ(0, base_->CountAuthorities());
622 EXPECT_EQ(false, base_->HasAuthority(authority));
623 }
624
625 /**
626 * @tc.number: AaFwk_Skills_Path_0200
627 * @tc.name: CountPaths/HasPath/GetPath
628 * @tc.desc: Verify the function when action is not exist.
629 */
630 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Path_0200, Function | MediumTest | Level1)
631 {
632 std::string empty;
633 std::string path = "path.system.test";
634 base_->AddPath(path, MatchType::DEFAULT);
635
636 EXPECT_EQ(1, base_->CountPaths());
637 EXPECT_EQ(true, base_->HasPath(path));
638 EXPECT_EQ(path, base_->GetPath(0));
639
640 base_->RemovePath(path, MatchType::DEFAULT);
641 EXPECT_EQ(0, base_->CountPaths());
642 EXPECT_EQ(false, base_->HasPath(path));
643 }
644
645 /**
646 * @tc.number: AaFwk_Skills_Scheme_0200
647 * @tc.name: CountSchemes/HasScheme/GetScheme
648 * @tc.desc: Verify the function when action is not exist.
649 */
650 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Scheme_0200, Function | MediumTest | Level1)
651 {
652 std::string empty;
653 std::string scheme = "scheme.system.test";
654 EXPECT_EQ(0, base_->CountSchemes());
655 EXPECT_EQ(false, base_->HasScheme(scheme));
656 EXPECT_EQ(empty, base_->GetScheme(0));
657
658 base_->RemoveScheme(scheme);
659 EXPECT_EQ(0, base_->CountSchemes());
660 EXPECT_EQ(false, base_->HasScheme(scheme));
661 }
662
663 /**
664 * @tc.number: AaFwk_Skills_SchemeSpecificPart_0200
665 * @tc.name: CountSchemeSpecificParts/HasSchemeSpecificPart/GetSchemeSpecificPart
666 * @tc.desc: Verify the function when action is not exist.
667 */
668 HWTEST_F(SkillsBaseTest, AaFwk_Skills_SchemeSpecificPart_0200, Function | MediumTest | Level1)
669 {
670 std::string empty;
671 std::string schemespecificpart = "schemespecificpart.system.test";
672 EXPECT_EQ(0, base_->CountSchemeSpecificParts());
673 EXPECT_EQ(false, base_->HasSchemeSpecificPart(schemespecificpart));
674 EXPECT_EQ(empty, base_->GetSchemeSpecificPart(0));
675
676 base_->RemoveSchemeSpecificPart(schemespecificpart);
677 EXPECT_EQ(0, base_->CountSchemeSpecificParts());
678 EXPECT_EQ(false, base_->HasSchemeSpecificPart(schemespecificpart));
679 }
680
681 /**
682 * @tc.number: AaFwk_Skills_Type_0300
683 * @tc.name: CountTypes/HasType/GetType
684 * @tc.desc: Verify the function when action is not exist.
685 */
686 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Type_0300, Function | MediumTest | Level1)
687 {
688 std::string empty;
689 std::string type = "type.system.test";
690 EXPECT_EQ(0, base_->CountTypes());
691
692 EXPECT_EQ(false, base_->HasType(type));
693 EXPECT_EQ(empty, base_->GetType(0));
694
695 base_->RemoveType(type);
696 EXPECT_EQ(0, base_->CountTypes());
697 EXPECT_EQ(false, base_->HasType(type));
698 }
699
700 using SkillsMatchType = std::tuple<std::string, std::string, bool>;
701 class SkillsMatchTest : public testing::TestWithParam<SkillsMatchType> {
702 public:
SkillsMatchTest()703 SkillsMatchTest()
704 {}
~SkillsMatchTest()705 ~SkillsMatchTest()
706 {}
707 static void SetUpTestCase(void);
708 static void TearDownTestCase(void);
709 void SetUp();
710 void TearDown();
711 std::shared_ptr<Skills> skills_ = nullptr;
712 };
713
SetUpTestCase(void)714 void SkillsMatchTest::SetUpTestCase(void)
715 {}
716
TearDownTestCase(void)717 void SkillsMatchTest::TearDownTestCase(void)
718 {}
719
SetUp(void)720 void SkillsMatchTest::SetUp(void)
721 {
722 skills_ = std::make_shared<Skills>();
723 }
724
TearDown(void)725 void SkillsMatchTest::TearDown(void)
726 {}
727
728 /**
729 * @tc.number: AaFwk_Skills_match_0100
730 * @tc.name: CountTypes/HasType/GetType
731 * @tc.desc: Verify whether parameter change.
732 */
733 HWTEST_P(SkillsMatchTest, AaFwk_Skills_match_0100, Function | MediumTest | Level1)
734 {
735 std::string filterEntity = "entity.system.entity1";
736 std::string filterAction1 = "action.system.action1";
737 std::string filterAction2 = "action.system.action2";
738 std::string intentEntity = std::get<0>(GetParam());
739 std::string intentAction = std::get<1>(GetParam());
740 bool result = std::get<2>(GetParam());
741
742 skills_->AddEntity(filterEntity);
743 skills_->AddAction(filterAction1);
744 skills_->AddAction(filterAction2);
745
746 Want want;
747 want.AddEntity(intentEntity);
748 want.SetAction(intentAction);
749
750 EXPECT_EQ(result, skills_->Match(want));
751 }
752
753 INSTANTIATE_TEST_CASE_P(SkillsMatchTestP, SkillsMatchTest,
754 testing::Values(SkillsMatchType("entity.system.entityA", "action.system.actionA", false),
755 SkillsMatchType("entity.system.entity1", "action.system.actionA", false),
756 SkillsMatchType("entity.system.entityA", "action.system.action2", false),
757 SkillsMatchType("entity.system.entity1", "action.system.action1", true)));
758
759 /**
760 * @tc.number: AaFwk_Skills_Skills_0100
761 * @tc.name: Skills() and Skills(Skills)
762 * @tc.desc: Verify Skills().
763 */
764 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Skills_0100, Function | MediumTest | Level1)
765 {
766 Skills skills;
767
768 EXPECT_EQ(0, skills.CountEntities());
769 EXPECT_EQ(0, skills.CountActions());
770 EXPECT_EQ(0, skills.CountAuthorities());
771 EXPECT_EQ(0, skills.CountSchemes());
772
773 EXPECT_EQ(0, skills.CountPaths());
774 EXPECT_EQ(0, skills.CountSchemeSpecificParts());
775 EXPECT_EQ(0, skills.CountTypes());
776 EXPECT_EQ(0, skills.GetWantParams().Size());
777 }
778
779 /**
780 * @tc.number: AaFwk_Skills_Skills_0200
781 * @tc.name: Skills() and Skills(Skills)
782 * @tc.desc: Verify Skills().
783 */
784 HWTEST_F(SkillsBaseTest, AaFwk_Skills_Skills_0200, Function | MediumTest | Level1)
785 {
786 Skills skillsBase;
787 std::string entityString = "entity";
788 skillsBase.AddEntity(entityString);
789 std::string actionString = "action";
790 skillsBase.AddAction(actionString);
791 std::string authorityString = "authority";
792 skillsBase.AddAuthority(authorityString);
793 std::string schemeString = "scheme";
794 skillsBase.AddScheme(schemeString);
795 std::string pathString = "path";
796 skillsBase.AddPath(pathString);
797 std::string schemeSpecificPartsString = "schemeSpecificParts";
798 skillsBase.AddSchemeSpecificPart(schemeSpecificPartsString);
799 std::string typeString = "/type";
800 skillsBase.AddType(typeString);
801 Skills skills(skillsBase);
802
803 EXPECT_EQ(entityString, skills.GetEntity(0));
804 EXPECT_EQ(actionString, skills.GetAction(0));
805 EXPECT_EQ(authorityString, skills.GetAuthority(0));
806 EXPECT_EQ(schemeString, skills.GetScheme(0));
807
808 EXPECT_EQ(pathString, skills.GetPath(0));
809 EXPECT_EQ(schemeSpecificPartsString, skills.GetSchemeSpecificPart(0));
810 EXPECT_EQ(typeString, skills.GetType(0));
811 }
812
813 /**
814 * @tc.number: AaFwk_Skills_addremoveType_0100
815 * @tc.name: addType(PatternsMatcher)/ removeType(PatternsMatcher)
816 * @tc.desc: Verify addType/removeType result.
817 */
818 HWTEST_F(SkillsBaseTest, AaFwk_Skills_addremoveType_0100, Function | MediumTest | Level1)
819 {
820 std::string patternStr = std::string("systems/*t");
821
822 PatternsMatcher pattern(patternStr, MatchType::DEFAULT);
823
824 base_->AddType(pattern);
825 std::string type1 = base_->GetType(0);
826 EXPECT_EQ(patternStr, type1);
827
828 base_->RemoveType(patternStr);
829
830 EXPECT_EQ(0, base_->CountEntities());
831
832 base_->AddType(pattern);
833
834 std::string patternStr2 = std::string("systems/*test");
835 PatternsMatcher pattern2(patternStr2, MatchType::PREFIX);
836 base_->AddType(pattern2);
837 std::string type2 = base_->GetType(1);
838 EXPECT_EQ(patternStr2, type2);
839
840 base_->RemoveType(pattern2);
841 EXPECT_EQ(0, base_->CountEntities());
842
843 std::string patternStr3 = std::string("systems/*test3");
844 base_->AddType(patternStr3, MatchType::GLOBAL);
845
846 std::string type3 = base_->GetType(1);
847 EXPECT_EQ(patternStr3, type3);
848
849 base_->RemoveType(patternStr3, MatchType::GLOBAL);
850
851 EXPECT_EQ(0, base_->CountEntities());
852 }
853
854 using testParamsType = std::tuple<std::string, std::string>;
855 class SkillsParamsTest : public testing::TestWithParam<testParamsType> {
856 public:
SkillsParamsTest()857 SkillsParamsTest()
858 {}
~SkillsParamsTest()859 ~SkillsParamsTest()
860 {}
861 static void SetUpTestCase(void);
862 static void TearDownTestCase(void);
863 void SetUp();
864 void TearDown();
865 std::shared_ptr<Skills> skills_ = nullptr;
866 };
867
SetUpTestCase(void)868 void SkillsParamsTest::SetUpTestCase(void)
869 {}
870
TearDownTestCase(void)871 void SkillsParamsTest::TearDownTestCase(void)
872 {}
873
SetUp(void)874 void SkillsParamsTest::SetUp(void)
875 {
876 skills_ = std::make_shared<Skills>();
877 }
878
TearDown(void)879 void SkillsParamsTest::TearDown(void)
880 {}
881
882 /**
883 * @tc.number: AaFwk_Skills_Params_0100
884 * @tc.name: SetWantParams/GetWantParams
885 * @tc.desc: Verify addType/removeType result.
886 */
887 HWTEST_P(SkillsParamsTest, AaFwk_Skills_Params_0100, Function | MediumTest | Level1)
888 {
889 std::string keyStr = std::get<0>(GetParam());
890 std::string valueStr = std::get<1>(GetParam());
891 WantParams wantParams;
892 wantParams.SetParam(keyStr, String::Box(valueStr));
893 skills_->SetWantParams(wantParams);
894 EXPECT_EQ(valueStr, String::Unbox(IString::Query(skills_->GetWantParams().GetParam(keyStr))));
895 }
896
897 INSTANTIATE_TEST_CASE_P(SkillsParamsTestCaseP, SkillsParamsTest,
898 testing::Values(testParamsType("", "asdsdsdasa"), testParamsType(std::string(LARGE_STR_LEN + 1, 's'), "sadsdsdads"),
899 testParamsType("#$%^&*(!@\":<>{}", "asdsdsdasa"), testParamsType("3456677", ""),
900 testParamsType("1234", std::string(LARGE_STR_LEN + 1, 's')),
901 testParamsType("2323sdasdZ", "#$%^&*(!@\":<>{}sadsdasdsaf"), testParamsType("12345667", "sdasdfdsffdgfdg"),
902 testParamsType("", ""),
903 testParamsType(std::string(LARGE_STR_LEN + 1, 'k'), std::string(LARGE_STR_LEN + 1, 'k')),
904 testParamsType("#$%^&*(!@\":<>{},/", "#$%^&*(!@\":<>{},/")));
905
906 } // namespace AAFwk
907 } // namespace OHOS