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 #define private public
17 #define protected public
18 #include "async_common_event_result.h"
19 #include "matching_skills.h"
20 #undef private
21 #undef protected
22
23 #include <gtest/gtest.h>
24
25 using namespace testing::ext;
26 using namespace OHOS;
27 using namespace OHOS::EventFwk;
28 using OHOS::Parcel;
29
30 static const size_t SET_COUNT = 1;
31 static const size_t MAX_COUNT = 100;
32
33 class MatchingSkillsTest : public testing::Test {
34 public:
35 static void SetUpTestCase(void);
36 static void TearDownTestCase(void);
37 void SetUp();
38 void TearDown();
39 };
40
SetUpTestCase(void)41 void MatchingSkillsTest::SetUpTestCase(void)
42 {}
43
TearDownTestCase(void)44 void MatchingSkillsTest::TearDownTestCase(void)
45 {}
46
SetUp(void)47 void MatchingSkillsTest::SetUp(void)
48 {}
49
TearDown(void)50 void MatchingSkillsTest::TearDown(void)
51 {}
52
53 /*
54 * Feature: MatchingSkills
55 * Function: AddEntity/GetEntity/CountEntities/HasEntity/RemoveEntity
56 * SubFunction: NA
57 * FunctionPoints: AddEntity/GetEntity
58 * EnvConditions: NA
59 * CaseDescription: Verify the function when after add entity can get entity
60 * and count entity is right,has entity and can success remove entity
61 */
62 HWTEST_F(MatchingSkillsTest, MatchingSkills_Entity_001, TestSize.Level1)
63 {
64 MatchingSkills matchSkills;
65 std::string empty;
66 std::string entity = "event.unit.test";
67 matchSkills.AddEntity(entity);
68 matchSkills.AddEntity(entity);
69 EXPECT_EQ(SET_COUNT, matchSkills.CountEntities());
70
71 EXPECT_EQ(true, matchSkills.HasEntity(entity));
72
73 EXPECT_EQ(entity, matchSkills.GetEntity(0));
74
75 EXPECT_EQ(false, entity == matchSkills.GetEntity(MAX_COUNT));
76
77 matchSkills.RemoveEntity(entity);
78
79 matchSkills.RemoveEntity(entity);
80
81 EXPECT_EQ(0, matchSkills.CountEntities());
82 }
83
84 /*
85 * Feature: MatchingSkills
86 * Function: event
87 * SubFunction: NA
88 * FunctionPoints: AddEvent/GetEvent/CountEvent/HasEvent/RemoveEvent
89 * EnvConditions: NA
90 * CaseDescription: Verify the function when after add event can get event
91 * and count event is right,has event and can success remove event
92 */
93 HWTEST_F(MatchingSkillsTest, MatchingSkills_Event_001, TestSize.Level1)
94 {
95 MatchingSkills matchSkills;
96 std::string empty;
97 std::string event = "event.unit.test";
98 matchSkills.AddEvent(event);
99 matchSkills.AddEvent(event);
100
101 EXPECT_EQ(true, matchSkills.HasEvent(event));
102
103 EXPECT_EQ(event, matchSkills.GetEvent(0));
104
105 EXPECT_EQ(false, event == matchSkills.GetEvent(SET_COUNT));
106 matchSkills.RemoveEvent(event);
107
108 matchSkills.RemoveEvent(event);
109
110 EXPECT_EQ(0, matchSkills.CountEvent());
111 }
112
113 /*
114 * Feature: MatchingSkills
115 * Function: scheme
116 * SubFunction: NA
117 * FunctionPoints: AddScheme/GetScheme/CountScheme/HasScheme/RemoveScheme
118 * EnvConditions: NA
119 * CaseDescription: Verify the function when after add scheme can get scheme
120 * and count scheme is right,has scheme and can success remove scheme
121 */
122 HWTEST_F(MatchingSkillsTest, MatchingSkills_Scheme_001, TestSize.Level1)
123 {
124 MatchingSkills matchSkills;
125 std::string empty;
126 std::string shceme = "event.unit.test";
127 matchSkills.AddScheme(shceme);
128 matchSkills.AddScheme(shceme);
129
130 EXPECT_EQ(true, matchSkills.HasScheme(shceme));
131
132 EXPECT_EQ(shceme, matchSkills.GetScheme(0));
133
134 EXPECT_EQ(false, shceme == matchSkills.GetScheme(SET_COUNT));
135
136 matchSkills.RemoveScheme(shceme);
137
138 matchSkills.RemoveScheme(shceme);
139
140 EXPECT_EQ(0, matchSkills.CountSchemes());
141 }
142
143 /*
144 * Feature: MatchingSkills
145 * Function: Match Event
146 * SubFunction: NA
147 * FunctionPoints: Match Event
148 * EnvConditions: NA
149 * CaseDescription: Verify match event
150 */
151 HWTEST_F(MatchingSkillsTest, MatchingSkills_MatchEvent_001, TestSize.Level1)
152 {
153 MatchingSkills matchSkills;
154 EXPECT_EQ(false, matchSkills.MatchEvent(""));
155
156 std::string event = "event.unit.test";
157 matchSkills.AddEvent(event);
158 EXPECT_EQ(true, matchSkills.MatchEvent(event));
159 }
160
161 /*
162 * Feature: MatchingSkills
163 * Function: Match entity
164 * SubFunction: NA
165 * FunctionPoints: Match entity
166 * EnvConditions: NA
167 * CaseDescription: Verify match entity
168 */
169
170 HWTEST_F(MatchingSkillsTest, MatchingSkills_MatchEntity_001, TestSize.Level1)
171 {
172 MatchingSkills matchSkills;
173 std::vector<std::string> entities;
174 EXPECT_EQ(true, matchSkills.MatchEntity(entities));
175
176 std::string entity = "event.unit.test";
177 matchSkills.AddEntity(entity);
178 entities.emplace_back(entity);
179 EXPECT_EQ(true, matchSkills.MatchEntity(entities));
180 entities.emplace_back("entitydiffer");
181 EXPECT_EQ(false, matchSkills.MatchEntity(entities));
182 }
183
184 /*
185 * Feature: MatchingSkills
186 * Function: Match scheme
187 * SubFunction: NA
188 * FunctionPoints: Match scheme
189 * EnvConditions: NA
190 * CaseDescription: Verify match scheme
191 */
192
193 HWTEST_F(MatchingSkillsTest, MatchingSkills_MatchScheme_001, TestSize.Level1)
194 {
195 MatchingSkills matchSkills;
196 EXPECT_EQ(true, matchSkills.MatchScheme(""));
197 std::string scheme = "action.system.event";
198 matchSkills.AddScheme(scheme);
199 EXPECT_EQ(true, matchSkills.MatchScheme(scheme));
200 EXPECT_EQ(false, matchSkills.MatchScheme("schemediffer"));
201 }
202
203 /*
204 * Feature: Match
205 * Function: Match
206 * SubFunction: NA
207 * FunctionPoints: Match
208 * EnvConditions: NA
209 * CaseDescription: Verify match
210 */
211
212 HWTEST_F(MatchingSkillsTest, MatchingSkills_Match_001, TestSize.Level1)
213 {
214 MatchingSkills matchSkills;
215 std::string event = "event.unit.test";
216 matchSkills.AddEvent(event);
217 std::string entity;
218 matchSkills.AddEntity(entity);
219 Want want;
220 want.AddEntity(entity);
221 want.SetAction(event);
222 EXPECT_EQ(true, matchSkills.Match(want));
223 }
224
225 /*
226 * Feature: GetEntity
227 * Function: GetEntity
228 * SubFunction: NA
229 * FunctionPoints: GetEntity
230 * EnvConditions: NA
231 * CaseDescription: test GetEntity function
232 */
233
234 HWTEST_F(MatchingSkillsTest, MatchingSkills_001, TestSize.Level1)
235 {
236 MatchingSkills matchSkills;
237 std::string event = "event.unit.test";
238 matchSkills.entities_.emplace_back(event);
239 size_t index = 1;
240 EXPECT_EQ("", matchSkills.GetEntity(index));
241 }
242
243 /*
244 * Feature: GetEntity
245 * Function: GetEntity
246 * SubFunction: NA
247 * FunctionPoints: GetEntity
248 * EnvConditions: NA
249 * CaseDescription: test GetEntity function
250 */
251
252 HWTEST_F(MatchingSkillsTest, MatchingSkills_002, TestSize.Level1)
253 {
254 MatchingSkills matchSkills;
255 std::string event = "event.unit.test";
256 matchSkills.entities_.emplace_back(event);
257 size_t index = -1;
258 EXPECT_EQ("", matchSkills.GetEntity(index));
259 }
260
261 /*
262 * Feature: GetEntity
263 * Function: GetEntity
264 * SubFunction: NA
265 * FunctionPoints: GetEntity
266 * EnvConditions: NA
267 * CaseDescription: test GetEntity function
268 */
269
270 HWTEST_F(MatchingSkillsTest, MatchingSkills_003, TestSize.Level1)
271 {
272 MatchingSkills matchSkills;
273 std::string event = "event.unit.test";
274 matchSkills.entities_.emplace_back(event);
275 size_t index = 0;
276 EXPECT_EQ("event.unit.test", matchSkills.GetEntity(index));
277 }
278
279 /*
280 * Feature: GetEvent
281 * Function: GetEvent
282 * SubFunction: NA
283 * FunctionPoints: GetEvent
284 * EnvConditions: NA
285 * CaseDescription: test GetEvent function
286 */
287
288 HWTEST_F(MatchingSkillsTest, MatchingSkills_004, TestSize.Level1)
289 {
290 MatchingSkills matchSkills;
291 std::string event = "event.unit.test";
292 matchSkills.events_.emplace_back(event);
293 size_t index = 1;
294 EXPECT_EQ("", matchSkills.GetEvent(index));
295 }
296
297 /*
298 * Feature: GetEvent
299 * Function: GetEvent
300 * SubFunction: NA
301 * FunctionPoints: GetEvent
302 * EnvConditions: NA
303 * CaseDescription: test GetEvent function
304 */
305
306 HWTEST_F(MatchingSkillsTest, MatchingSkills_005, TestSize.Level1)
307 {
308 MatchingSkills matchSkills;
309 std::string event = "event.unit.test";
310 matchSkills.events_.emplace_back(event);
311 size_t index = -1;
312 EXPECT_EQ("", matchSkills.GetEvent(index));
313 }
314
315 /*
316 * Feature: GetEvent
317 * Function: GetEvent
318 * SubFunction: NA
319 * FunctionPoints: GetEvent
320 * EnvConditions: NA
321 * CaseDescription: test GetEvent function
322 */
323
324 HWTEST_F(MatchingSkillsTest, MatchingSkills_006, TestSize.Level1)
325 {
326 MatchingSkills matchSkills;
327 std::string event = "event.unit.test";
328 matchSkills.events_.emplace_back(event);
329 size_t index = 0;
330 EXPECT_EQ("event.unit.test", matchSkills.GetEvent(index));
331 }
332
333 /*
334 * Feature: GetScheme
335 * Function: GetScheme
336 * SubFunction: NA
337 * FunctionPoints: GetScheme
338 * EnvConditions: NA
339 * CaseDescription: test GetScheme function
340 */
341
342 HWTEST_F(MatchingSkillsTest, MatchingSkills_007, TestSize.Level1)
343 {
344 MatchingSkills matchSkills;
345 std::string event = "event.unit.test";
346 matchSkills.schemes_.emplace_back(event);
347 size_t index = 1;
348 EXPECT_EQ("", matchSkills.GetScheme(index));
349 }
350
351 /*
352 * Feature: GetScheme
353 * Function: GetScheme
354 * SubFunction: NA
355 * FunctionPoints: GetScheme
356 * EnvConditions: NA
357 * CaseDescription: test GetScheme function
358 */
359
360 HWTEST_F(MatchingSkillsTest, MatchingSkills_008, TestSize.Level1)
361 {
362 MatchingSkills matchSkills;
363 std::string event = "event.unit.test";
364 matchSkills.schemes_.emplace_back(event);
365 size_t index = -1;
366 EXPECT_EQ("", matchSkills.GetScheme(index));
367 }
368
369 /*
370 * Feature: GetScheme
371 * Function: GetScheme
372 * SubFunction: NA
373 * FunctionPoints: GetScheme
374 * EnvConditions: NA
375 * CaseDescription: test GetScheme function
376 */
377
378 HWTEST_F(MatchingSkillsTest, MatchingSkills_009, TestSize.Level1)
379 {
380 MatchingSkills matchSkills;
381 std::string event = "event.unit.test";
382 matchSkills.schemes_.emplace_back(event);
383 size_t index = 0;
384 EXPECT_EQ("event.unit.test", matchSkills.GetScheme(index));
385 }
386
387 /*
388 * Feature: FinishCommonEvent
389 * Function: FinishCommonEvent
390 * SubFunction: NA
391 * FunctionPoints: FinishCommonEvent
392 * EnvConditions: NA
393 * CaseDescription: test FinishCommonEvent function
394 */
395
396 HWTEST_F(MatchingSkillsTest, AsyncCommonEventResult_001, TestSize.Level1)
397 {
398 int32_t resultCode = 1;
399 std::string resultData = "aa";
400 bool ordered = true;
401 bool sticky = true;
402 sptr<IRemoteObject> token = nullptr;
403 AsyncCommonEventResult asyncCommonEventResult(resultCode, resultData, ordered, sticky, token);
404 asyncCommonEventResult.finished_ = true;
405 EXPECT_EQ(false, asyncCommonEventResult.FinishCommonEvent());
406 }
407
408 /*
409 * @tc.number: AsyncCommonEventResult_002
410 * @tc.name: AsyncCommonEventResult CheckSynchronous
411 * @tc.desc: test SetCode function
412 */
413
414 HWTEST_F(MatchingSkillsTest, AsyncCommonEventResult_002, TestSize.Level1)
415 {
416 int32_t resultCode = 1;
417 std::string resultData = "aa";
418 bool ordered = false;
419 bool sticky = true;
420 sptr<IRemoteObject> token = nullptr;
421 AsyncCommonEventResult asyncCommonEventResult(resultCode, resultData, ordered, sticky, token);
422 EXPECT_EQ(asyncCommonEventResult.CheckSynchronous(), false);
423 int32_t code = 1;
424 bool result = asyncCommonEventResult.SetCode(code);
425 EXPECT_EQ(result, false);
426 }
427
428 /*
429 * @tc.number: AsyncCommonEventResult_003
430 * @tc.name: AsyncCommonEventResult SetData
431 * @tc.desc: test SetData function
432 */
433
434 HWTEST_F(MatchingSkillsTest, AsyncCommonEventResult_003, TestSize.Level1)
435 {
436 int32_t resultCode = 1;
437 std::string resultData = "aa";
438 bool ordered = false;
439 bool sticky = true;
440 sptr<IRemoteObject> token = nullptr;
441 AsyncCommonEventResult asyncCommonEventResult(resultCode, resultData, ordered, sticky, token);
442 EXPECT_EQ(asyncCommonEventResult.CheckSynchronous(), false);
443 std::string data = "this is data";
444 bool result = asyncCommonEventResult.SetData(data);
445 EXPECT_EQ(result, false);
446 }
447
448 /*
449 * @tc.number: AsyncCommonEventResult_004
450 * @tc.name: AsyncCommonEventResult SetCodeAndData
451 * @tc.desc: test SetCodeAndData function
452 */
453
454 HWTEST_F(MatchingSkillsTest, AsyncCommonEventResult_004, TestSize.Level1)
455 {
456 int32_t resultCode = 1;
457 std::string resultData = "aa";
458 bool ordered = false;
459 bool sticky = true;
460 sptr<IRemoteObject> token = nullptr;
461 AsyncCommonEventResult asyncCommonEventResult(resultCode, resultData, ordered, sticky, token);
462 EXPECT_EQ(asyncCommonEventResult.CheckSynchronous(), false);
463 int32_t code = 1;
464 std::string data = "this is data";
465 bool result = asyncCommonEventResult.SetCodeAndData(code, data);
466 EXPECT_EQ(result, false);
467 }
468
469 /*
470 * @tc.number: AsyncCommonEventResult_005
471 * @tc.name: AsyncCommonEventResult AbortCommonEvent
472 * @tc.desc: test AbortCommonEvent function
473 */
474
475 HWTEST_F(MatchingSkillsTest, AsyncCommonEventResult_005, TestSize.Level1)
476 {
477 int32_t resultCode = 1;
478 std::string resultData = "aa";
479 bool ordered = false;
480 bool sticky = true;
481 sptr<IRemoteObject> token = nullptr;
482 AsyncCommonEventResult asyncCommonEventResult(resultCode, resultData, ordered, sticky, token);
483 EXPECT_EQ(asyncCommonEventResult.CheckSynchronous(), false);
484 bool result = asyncCommonEventResult.AbortCommonEvent();
485 EXPECT_EQ(result, false);
486 }
487
488 /*
489 * @tc.number: AsyncCommonEventResult_006
490 * @tc.name: AsyncCommonEventResult ClearAbortCommonEvent
491 * @tc.desc: test ClearAbortCommonEvent function
492 */
493
494 HWTEST_F(MatchingSkillsTest, AsyncCommonEventResult_006, TestSize.Level1)
495 {
496 int32_t resultCode = 1;
497 std::string resultData = "aa";
498 bool ordered = false;
499 bool sticky = true;
500 sptr<IRemoteObject> token = nullptr;
501 AsyncCommonEventResult asyncCommonEventResult(resultCode, resultData, ordered, sticky, token);
502 EXPECT_EQ(asyncCommonEventResult.CheckSynchronous(), false);
503 bool result = asyncCommonEventResult.ClearAbortCommonEvent();
504 EXPECT_EQ(result, false);
505 }
506
507 /*
508 * @tc.number: AsyncCommonEventResult_007
509 * @tc.name: AsyncCommonEventResult FinishCommonEvent
510 * @tc.desc: test FinishCommonEvent function
511 */
512
513 HWTEST_F(MatchingSkillsTest, AsyncCommonEventResult_007, TestSize.Level1)
514 {
515 int32_t resultCode = 1;
516 std::string resultData = "aa";
517 bool ordered = false;
518 bool sticky = true;
519 sptr<IRemoteObject> token = nullptr;
520 AsyncCommonEventResult asyncCommonEventResult(resultCode, resultData, ordered, sticky, token);
521 EXPECT_EQ(asyncCommonEventResult.CheckSynchronous(), false);
522 bool result = asyncCommonEventResult.FinishCommonEvent();
523 EXPECT_EQ(result, false);
524 }