1 /*
2 * Copyright (c) 2025 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 <chrono>
17 #include <gtest/gtest.h>
18 #include <thread>
19
20 #define private public
21 #define protected public
22 #include "oh_commonevent.h"
23 #include "oh_commonevent_parameters_parse.h"
24 #include "oh_commonevent_wrapper.h"
25 #undef private
26 #undef protected
27
28 using namespace testing::ext;
29 using namespace OHOS;
30 using namespace OHOS::EventFwk;
31
32 namespace {
33 const char* EVENT = "com.ces.test.event";
34 const char* EVENT2 = "com.ces.test.event2";
35 const char* PUBLISHER_PERMISSION = "ohos.permission.test";
36 const char* PUBLISHER_PERMISSION2 = "ohos.permission.test2";
37 const char* TEST_BUNDLENAME = "com.example.test";
38 const char* TEST_DATA = "initial data";
39 const char* TEST_DATA2 = "test data";
40 const char* INT_KEY = "intKey";
41 const char* INT_ARRAY_KEY = "intArrayKey";
42 const char* LONG_KEY = "longKey";
43 const char* LONG_ARRAY_KEY = "longArrayKey";
44 const char* DOUBLE_KEY = "doubleKey";
45 const char* DOUBLE_ARRAY_KEY = "doubleArrayKey";
46 const char* BOOL_KEY = "boolKey";
47 const char* BOOL_ARRAY_KEY = "boolArrayKey";
48 const char* CHAR_KEY = "charKey";
49 const char* CHAR_ARRAY_KEY = "charArrayKey";
50 const char* INVALID_KEY = "invalidKey";
51 constexpr int INT_VALUE = 10;
52 constexpr int INT_ARRAY_VALUE[] = { 1, 2, 3 };
53 constexpr long LONG_VALUE = 100;
54 constexpr long LONG_ARRAY_VALUE[] = { 10, 20, 30 };
55 constexpr double DOUBLE_VALUE = 10.1;
56 constexpr double DOUBLE_ARRAY_VALUE[] = { 1.1, 2.2, 3.3 };
57 constexpr bool BOOL_VALUE = true;
58 constexpr bool BOOL_ARRAY_VALUE[] = { true, false, true };
59 constexpr char CHAR_VALUE = 'a';
60 const char* CHAR_ARRAY_VALUE = "abc";
61 constexpr int32_t TEST_CODE = 1;
62 constexpr int32_t TEST_CODE2 = 2;
63 constexpr int32_t TEST_NUM = 2;
64 constexpr int32_t TEST_VALUE_NUM = 3;
65 constexpr int32_t SLEEP_TIME = 3;
66 } // namespace
67
68 class CapiCommonEventTest : public testing::Test {
69 public:
CapiCommonEventTest()70 CapiCommonEventTest() {}
~CapiCommonEventTest()71 virtual ~CapiCommonEventTest() {}
72
73 static void SetUpTestCase();
74 static void TearDownTestCase();
75 void SetUp();
76 void TearDown();
77 };
78
SetUpTestCase()79 void CapiCommonEventTest::SetUpTestCase() {}
80
TearDownTestCase()81 void CapiCommonEventTest::TearDownTestCase() {}
82
SetUp()83 void CapiCommonEventTest::SetUp() {}
84
TearDown()85 void CapiCommonEventTest::TearDown() {}
86
OnReceive(const CommonEvent_RcvData * data)87 static void OnReceive(const CommonEvent_RcvData* data)
88 {
89 const char* event = OH_CommonEvent_GetEventFromRcvData(data);
90 ASSERT_STREQ(event, EVENT);
91 int code = OH_CommonEvent_GetCodeFromRcvData(data);
92 ASSERT_EQ(code, TEST_CODE);
93 const char* retData = OH_CommonEvent_GetDataStrFromRcvData(data);
94 ASSERT_STREQ(retData, TEST_DATA);
95 const CommonEvent_Parameters* parameters = OH_CommonEvent_GetParametersFromRcvData(data);
96 ASSERT_NE(parameters, nullptr);
97 }
98
OnReceiveWithoutData(const CommonEvent_RcvData * data)99 static void OnReceiveWithoutData(const CommonEvent_RcvData* data)
100 {
101 const char* event = OH_CommonEvent_GetEventFromRcvData(data);
102 ASSERT_STREQ(event, EVENT);
103 int code = OH_CommonEvent_GetCodeFromRcvData(data);
104 ASSERT_EQ(code, 0);
105 const char* retData = OH_CommonEvent_GetDataStrFromRcvData(data);
106 ASSERT_STREQ(retData, "");
107 }
108
109 /*
110 * @tc.name: CapiCommonEventTest_0100
111 * @tc.desc: Test create and set empty subscribeInfo.
112 * @tc.type: FUNC
113 */
114 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_0100, Function | MediumTest | Level0)
115 {
116 const char* events[] = {};
117 auto subscribeInfo = OH_CommonEvent_CreateSubscribeInfo(events, 0);
118 ASSERT_EQ(subscribeInfo, nullptr);
119
120 int32_t ret = OH_CommonEvent_SetPublisherPermission(subscribeInfo, "");
121 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
122
123 ret = OH_CommonEvent_SetPublisherBundleName(subscribeInfo, "");
124 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
125
126 OH_CommonEvent_DestroySubscribeInfo(subscribeInfo);
127 }
128
129 /*
130 * @tc.name: CapiCommonEventTest_0200
131 * @tc.desc: Test create and set subscribeInfo.
132 * @tc.type: FUNC
133 */
134 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_0200, Function | MediumTest | Level0)
135 {
136 const char* events[] = { EVENT, EVENT2 };
137 auto subscribeInfo = OH_CommonEvent_CreateSubscribeInfo(events, TEST_NUM);
138 ASSERT_NE(subscribeInfo, nullptr);
139
140 int32_t ret = OH_CommonEvent_SetPublisherPermission(subscribeInfo, nullptr);
141 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
142 ASSERT_EQ(subscribeInfo->permission, std::string());
143
144 ret = OH_CommonEvent_SetPublisherPermission(subscribeInfo, PUBLISHER_PERMISSION);
145 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
146 ASSERT_STREQ(subscribeInfo->permission.c_str(), PUBLISHER_PERMISSION);
147
148 ret = OH_CommonEvent_SetPublisherBundleName(subscribeInfo, nullptr);
149 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
150 ASSERT_EQ(subscribeInfo->bundleName, std::string());
151
152 ret = OH_CommonEvent_SetPublisherBundleName(subscribeInfo, TEST_BUNDLENAME);
153 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
154 ASSERT_STREQ(subscribeInfo->bundleName.c_str(), TEST_BUNDLENAME);
155
156 OH_CommonEvent_DestroySubscribeInfo(subscribeInfo);
157 }
158
159 /*
160 * @tc.name: CapiCommonEventTest_0300
161 * @tc.desc: Test create subscriber.
162 * @tc.type: FUNC
163 */
164 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_0300, Function | MediumTest | Level0)
165 {
166 auto subscriber = OH_CommonEvent_CreateSubscriber(nullptr, OnReceive);
167 ASSERT_EQ(subscriber, nullptr);
168
169 const char* events[] = { EVENT, EVENT2 };
170 auto subscribeInfo = OH_CommonEvent_CreateSubscribeInfo(events, TEST_NUM);
171 ASSERT_NE(subscribeInfo, nullptr);
172
173 auto subscriber2 = OH_CommonEvent_CreateSubscriber(subscribeInfo, OnReceive);
174 ASSERT_NE(subscriber2, nullptr);
175
176 OH_CommonEvent_DestroySubscriber(subscriber2);
177 }
178
179 /*
180 * @tc.name: CapiCommonEventTest_0400
181 * @tc.desc: Test subscribe and unsubscribe event.
182 * @tc.type: FUNC
183 */
184 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_0400, Function | MediumTest | Level0)
185 {
186 int32_t ret = OH_CommonEvent_Subscribe(nullptr);
187 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
188
189 const char* events[] = { EVENT, EVENT2 };
190 auto subscribeInfo = OH_CommonEvent_CreateSubscribeInfo(events, TEST_NUM);
191 ASSERT_NE(subscribeInfo, nullptr);
192
193 auto subscriber = OH_CommonEvent_CreateSubscriber(subscribeInfo, OnReceive);
194 ASSERT_NE(subscriber, nullptr);
195
196 ret = OH_CommonEvent_Subscribe(subscriber);
197 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
198 ret = OH_CommonEvent_UnSubscribe(subscriber);
199 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
200
201 OH_CommonEvent_DestroySubscriber(subscriber);
202 }
203
204 /*
205 * @tc.name: CapiCommonEventTest_0500
206 * @tc.desc: Test create and set publishInfo.
207 * @tc.type: FUNC
208 */
209 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_0500, Function | MediumTest | Level0)
210 {
211 auto publishInfo = OH_CommonEvent_CreatePublishInfo(true);
212 ASSERT_NE(publishInfo, nullptr);
213
214 int32_t ret = OH_CommonEvent_SetPublishInfoBundleName(publishInfo, nullptr);
215 ASSERT_EQ(publishInfo->bundleName, std::string());
216 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
217
218 ret = OH_CommonEvent_SetPublishInfoBundleName(publishInfo, TEST_BUNDLENAME);
219 ASSERT_STREQ(publishInfo->bundleName.c_str(), TEST_BUNDLENAME);
220 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
221
222 const char* permissions[] = { PUBLISHER_PERMISSION, PUBLISHER_PERMISSION2 };
223 ret = OH_CommonEvent_SetPublishInfoPermissions(publishInfo, permissions, TEST_NUM);
224 ASSERT_FALSE(publishInfo->subscriberPermissions.empty());
225 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
226
227 const char* permissions2[] = {};
228 ret = OH_CommonEvent_SetPublishInfoPermissions(publishInfo, permissions2, 0);
229 ASSERT_TRUE(publishInfo->subscriberPermissions.empty());
230 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
231
232 ret = OH_CommonEvent_SetPublishInfoCode(publishInfo, TEST_CODE);
233 ASSERT_EQ(publishInfo->code, TEST_CODE);
234 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
235
236 ret = OH_CommonEvent_SetPublishInfoData(publishInfo, nullptr, 0);
237 ASSERT_EQ(publishInfo->data, std::string());
238 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
239
240 ret = OH_CommonEvent_SetPublishInfoData(publishInfo, TEST_DATA, strlen(TEST_DATA));
241 ASSERT_STREQ(publishInfo->data.c_str(), TEST_DATA);
242 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
243
244 OH_CommonEvent_DestroyPublishInfo(publishInfo);
245 }
246
247 /*
248 * @tc.name: CapiCommonEventTest_0600
249 * @tc.desc: Test create empty publishInfo.
250 * @tc.type: FUNC
251 */
252 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_0600, Function | MediumTest | Level0)
253 {
254 CommonEvent_PublishInfo* publishInfo = nullptr;
255 CommonEvent_Parameters* param = nullptr;
256
257 int32_t ret = OH_CommonEvent_SetPublishInfoBundleName(publishInfo, nullptr);
258 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
259
260 const char* permissions[] = {};
261 ret = OH_CommonEvent_SetPublishInfoPermissions(publishInfo, permissions, 0);
262 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
263
264 ret = OH_CommonEvent_SetPublishInfoCode(publishInfo, 0);
265 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
266
267 ret = OH_CommonEvent_SetPublishInfoData(publishInfo, nullptr, 0);
268 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
269
270 ret = OH_CommonEvent_SetPublishInfoParameters(publishInfo, param);
271 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
272 }
273
274 /*
275 * @tc.name: CapiCommonEventTest_0700
276 * @tc.desc: Test create and set int parameters.
277 * @tc.type: FUNC
278 */
279 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_0700, Function | MediumTest | Level0)
280 {
281 auto param = OH_CommonEvent_CreateParameters();
282
283 int32_t ret = OH_CommonEvent_SetIntToParameters(nullptr, nullptr, 0);
284 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
285 bool exist = OH_CommonEvent_HasKeyInParameters(nullptr, nullptr);
286 ASSERT_FALSE(exist);
287 ret = OH_CommonEvent_GetIntFromParameters(nullptr, nullptr, 0);
288 ASSERT_EQ(ret, 0);
289
290 ret = OH_CommonEvent_SetIntToParameters(param, nullptr, 0);
291 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
292 exist = OH_CommonEvent_HasKeyInParameters(param, nullptr);
293 ASSERT_FALSE(exist);
294 ret = OH_CommonEvent_GetIntFromParameters(param, nullptr, 0);
295 ASSERT_EQ(ret, 0);
296
297 ret = OH_CommonEvent_SetIntToParameters(param, INT_KEY, INT_VALUE);
298 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
299 exist = OH_CommonEvent_HasKeyInParameters(param, INT_KEY);
300 ASSERT_TRUE(exist);
301 ret = OH_CommonEvent_GetIntFromParameters(param, INVALID_KEY, 0);
302 ASSERT_EQ(ret, 0);
303 ret = OH_CommonEvent_GetIntFromParameters(param, INT_KEY, 0);
304 ASSERT_EQ(ret, INT_VALUE);
305
306 OH_CommonEvent_DestroyParameters(param);
307 }
308
309 /*
310 * @tc.name: CapiCommonEventTest_0800
311 * @tc.desc: Test create and set int array parameters.
312 * @tc.type: FUNC
313 */
314 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_0800, Function | MediumTest | Level0)
315 {
316 auto param = OH_CommonEvent_CreateParameters();
317
318 int32_t ret = OH_CommonEvent_SetIntArrayToParameters(nullptr, nullptr, nullptr, 0);
319 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
320 ret = OH_CommonEvent_GetIntArrayFromParameters(nullptr, nullptr, nullptr);
321 ASSERT_EQ(ret, 0);
322
323 ret = OH_CommonEvent_SetIntArrayToParameters(param, nullptr, nullptr, 0);
324 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
325 ret = OH_CommonEvent_GetIntArrayFromParameters(param, nullptr, nullptr);
326 ASSERT_EQ(ret, 0);
327
328 ret = OH_CommonEvent_SetIntArrayToParameters(param, INT_ARRAY_KEY, INT_ARRAY_VALUE, TEST_VALUE_NUM);
329 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
330 bool exist = OH_CommonEvent_HasKeyInParameters(param, INT_ARRAY_KEY);
331 ASSERT_TRUE(exist);
332 int** arr = new int*;
333 ret = OH_CommonEvent_GetIntArrayFromParameters(param, INVALID_KEY, arr);
334 ASSERT_EQ(ret, 0);
335 ret = OH_CommonEvent_GetIntArrayFromParameters(param, INT_ARRAY_KEY, arr);
336 ASSERT_EQ(ret, TEST_VALUE_NUM);
337
338 OH_CommonEvent_DestroyParameters(param);
339 }
340
341 /*
342 * @tc.name: CapiCommonEventTest_0900
343 * @tc.desc: Test create and set long parameters.
344 * @tc.type: FUNC
345 */
346 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_0900, Function | MediumTest | Level0)
347 {
348 auto param = OH_CommonEvent_CreateParameters();
349
350 int32_t ret = OH_CommonEvent_SetLongToParameters(nullptr, nullptr, 0);
351 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
352 bool exist = OH_CommonEvent_HasKeyInParameters(nullptr, nullptr);
353 ASSERT_FALSE(exist);
354 long retLong = OH_CommonEvent_GetLongFromParameters(nullptr, nullptr, 0);
355 ASSERT_EQ(retLong, 0);
356
357 ret = OH_CommonEvent_SetLongToParameters(param, nullptr, 0);
358 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
359 exist = OH_CommonEvent_HasKeyInParameters(param, nullptr);
360 ASSERT_FALSE(exist);
361 retLong = OH_CommonEvent_GetLongFromParameters(param, nullptr, 0);
362 ASSERT_EQ(retLong, 0);
363
364 ret = OH_CommonEvent_SetLongToParameters(param, LONG_KEY, LONG_VALUE);
365 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
366 exist = OH_CommonEvent_HasKeyInParameters(param, LONG_KEY);
367 ASSERT_TRUE(exist);
368 retLong = OH_CommonEvent_GetLongFromParameters(param, INVALID_KEY, 0);
369 ASSERT_EQ(retLong, 0);
370 retLong = OH_CommonEvent_GetLongFromParameters(param, LONG_KEY, 0);
371 ASSERT_EQ(retLong, LONG_VALUE);
372
373 OH_CommonEvent_DestroyParameters(param);
374 }
375
376 /*
377 * @tc.name: CapiCommonEventTest_1000
378 * @tc.desc: Test create and set long array parameters.
379 * @tc.type: FUNC
380 */
381 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_1000, Function | MediumTest | Level0)
382 {
383 auto param = OH_CommonEvent_CreateParameters();
384
385 int32_t ret = OH_CommonEvent_SetLongArrayToParameters(nullptr, nullptr, nullptr, 0);
386 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
387 ret = OH_CommonEvent_GetLongArrayFromParameters(nullptr, nullptr, nullptr);
388 ASSERT_EQ(ret, 0);
389
390 ret = OH_CommonEvent_SetLongArrayToParameters(param, nullptr, nullptr, 0);
391 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
392 ret = OH_CommonEvent_GetLongArrayFromParameters(param, nullptr, nullptr);
393 ASSERT_EQ(ret, 0);
394
395 ret = OH_CommonEvent_SetLongArrayToParameters(param, LONG_ARRAY_KEY, LONG_ARRAY_VALUE, TEST_VALUE_NUM);
396 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
397 bool exist = OH_CommonEvent_HasKeyInParameters(param, LONG_ARRAY_KEY);
398 ASSERT_TRUE(exist);
399 long** arr = new long*;
400 ret = OH_CommonEvent_GetLongArrayFromParameters(param, INVALID_KEY, arr);
401 ASSERT_EQ(ret, 0);
402 ret = OH_CommonEvent_GetLongArrayFromParameters(param, LONG_ARRAY_KEY, arr);
403 ASSERT_EQ(ret, TEST_VALUE_NUM);
404
405 OH_CommonEvent_DestroyParameters(param);
406 }
407
408 /*
409 * @tc.name: CapiCommonEventTest_1100
410 * @tc.desc: Test create and set double parameters.
411 * @tc.type: FUNC
412 */
413 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_1100, Function | MediumTest | Level0)
414 {
415 auto param = OH_CommonEvent_CreateParameters();
416
417 int32_t ret = OH_CommonEvent_SetDoubleToParameters(nullptr, nullptr, 0.0);
418 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
419 bool exist = OH_CommonEvent_HasKeyInParameters(nullptr, nullptr);
420 ASSERT_FALSE(exist);
421 double retDouble = OH_CommonEvent_GetDoubleFromParameters(nullptr, nullptr, 0.0);
422 ASSERT_EQ(retDouble, 0.0);
423
424 ret = OH_CommonEvent_SetDoubleToParameters(param, nullptr, 0.0);
425 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
426 exist = OH_CommonEvent_HasKeyInParameters(param, nullptr);
427 ASSERT_FALSE(exist);
428 retDouble = OH_CommonEvent_GetDoubleFromParameters(param, nullptr, 0.0);
429 ASSERT_EQ(retDouble, 0.0);
430
431 ret = OH_CommonEvent_SetDoubleToParameters(param, DOUBLE_KEY, DOUBLE_VALUE);
432 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
433 exist = OH_CommonEvent_HasKeyInParameters(param, DOUBLE_KEY);
434 ASSERT_TRUE(exist);
435 retDouble = OH_CommonEvent_GetDoubleFromParameters(param, INVALID_KEY, 0.0);
436 ASSERT_EQ(retDouble, 0.0);
437 retDouble = OH_CommonEvent_GetDoubleFromParameters(param, DOUBLE_KEY, 0.0);
438 ASSERT_EQ(retDouble, DOUBLE_VALUE);
439
440 OH_CommonEvent_DestroyParameters(param);
441 }
442
443 /*
444 * @tc.name: CapiCommonEventTest_1200
445 * @tc.desc: Test create and set double array parameters.
446 * @tc.type: FUNC
447 */
448 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_1200, Function | MediumTest | Level0)
449 {
450 auto param = OH_CommonEvent_CreateParameters();
451
452 int32_t ret = OH_CommonEvent_SetDoubleArrayToParameters(nullptr, nullptr, nullptr, 0);
453 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
454 ret = OH_CommonEvent_GetDoubleArrayFromParameters(nullptr, nullptr, nullptr);
455 ASSERT_EQ(ret, 0);
456
457 ret = OH_CommonEvent_SetDoubleArrayToParameters(param, nullptr, nullptr, 0);
458 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
459 ret = OH_CommonEvent_GetDoubleArrayFromParameters(param, nullptr, nullptr);
460 ASSERT_EQ(ret, 0);
461
462 ret = OH_CommonEvent_SetDoubleArrayToParameters(param, DOUBLE_ARRAY_KEY, DOUBLE_ARRAY_VALUE, TEST_VALUE_NUM);
463 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
464 bool exist = OH_CommonEvent_HasKeyInParameters(param, DOUBLE_ARRAY_KEY);
465 ASSERT_TRUE(exist);
466 double** arr = new double*;
467 ret = OH_CommonEvent_GetDoubleArrayFromParameters(param, INVALID_KEY, arr);
468 ASSERT_EQ(ret, 0);
469 ret = OH_CommonEvent_GetDoubleArrayFromParameters(param, DOUBLE_ARRAY_KEY, arr);
470 ASSERT_EQ(ret, TEST_VALUE_NUM);
471
472 OH_CommonEvent_DestroyParameters(param);
473 }
474
475 /*
476 * @tc.name: CapiCommonEventTest_1300
477 * @tc.desc: Test create and set bool parameters.
478 * @tc.type: FUNC
479 */
480 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_1300, Function | MediumTest | Level0)
481 {
482 auto param = OH_CommonEvent_CreateParameters();
483
484 int32_t ret = OH_CommonEvent_SetBoolToParameters(nullptr, nullptr, false);
485 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
486 bool exist = OH_CommonEvent_HasKeyInParameters(nullptr, nullptr);
487 ASSERT_FALSE(exist);
488 bool retBool = OH_CommonEvent_GetBoolFromParameters(nullptr, nullptr, false);
489 ASSERT_EQ(retBool, false);
490
491 ret = OH_CommonEvent_SetBoolToParameters(param, nullptr, false);
492 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
493 exist = OH_CommonEvent_HasKeyInParameters(param, nullptr);
494 ASSERT_FALSE(exist);
495 retBool = OH_CommonEvent_GetBoolFromParameters(param, nullptr, false);
496 ASSERT_EQ(retBool, false);
497
498 ret = OH_CommonEvent_SetBoolToParameters(param, BOOL_KEY, BOOL_VALUE);
499 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
500 exist = OH_CommonEvent_HasKeyInParameters(param, BOOL_KEY);
501 ASSERT_TRUE(exist);
502 retBool = OH_CommonEvent_GetBoolFromParameters(param, INVALID_KEY, false);
503 ASSERT_EQ(retBool, false);
504 retBool = OH_CommonEvent_GetBoolFromParameters(param, BOOL_KEY, false);
505 ASSERT_EQ(retBool, BOOL_VALUE);
506
507 OH_CommonEvent_DestroyParameters(param);
508 }
509
510 /*
511 * @tc.name: CapiCommonEventTest_1400
512 * @tc.desc: Test create and set bool array parameters.
513 * @tc.type: FUNC
514 */
515 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_1400, Function | MediumTest | Level0)
516 {
517 auto param = OH_CommonEvent_CreateParameters();
518
519 int32_t ret = OH_CommonEvent_SetBoolArrayToParameters(nullptr, nullptr, nullptr, 0);
520 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
521 ret = OH_CommonEvent_GetBoolArrayFromParameters(nullptr, nullptr, nullptr);
522 ASSERT_EQ(ret, 0);
523
524 ret = OH_CommonEvent_SetBoolArrayToParameters(param, nullptr, nullptr, 0);
525 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
526 ret = OH_CommonEvent_GetBoolArrayFromParameters(param, nullptr, nullptr);
527 ASSERT_EQ(ret, 0);
528
529 ret = OH_CommonEvent_SetBoolArrayToParameters(param, BOOL_ARRAY_KEY, BOOL_ARRAY_VALUE, TEST_VALUE_NUM);
530 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
531 bool exist = OH_CommonEvent_HasKeyInParameters(param, BOOL_ARRAY_KEY);
532 ASSERT_TRUE(exist);
533 bool** arr = new bool*;
534 ret = OH_CommonEvent_GetBoolArrayFromParameters(param, INVALID_KEY, arr);
535 ASSERT_EQ(ret, 0);
536 ret = OH_CommonEvent_GetBoolArrayFromParameters(param, BOOL_ARRAY_KEY, arr);
537 ASSERT_EQ(ret, TEST_VALUE_NUM);
538
539 OH_CommonEvent_DestroyParameters(param);
540 }
541
542 /*
543 * @tc.name: CapiCommonEventTest_1500
544 * @tc.desc: Test create and set char parameters.
545 * @tc.type: FUNC
546 */
547 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_1500, Function | MediumTest | Level0)
548 {
549 auto param = OH_CommonEvent_CreateParameters();
550
551 int32_t ret = OH_CommonEvent_SetCharToParameters(nullptr, nullptr, ' ');
552 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
553 bool exist = OH_CommonEvent_HasKeyInParameters(nullptr, nullptr);
554 ASSERT_FALSE(exist);
555 char retChar = OH_CommonEvent_GetCharFromParameters(nullptr, nullptr, ' ');
556 ASSERT_EQ(retChar, ' ');
557
558 ret = OH_CommonEvent_SetCharToParameters(param, nullptr, ' ');
559 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
560 exist = OH_CommonEvent_HasKeyInParameters(param, nullptr);
561 ASSERT_FALSE(exist);
562 retChar = OH_CommonEvent_GetCharFromParameters(param, nullptr, ' ');
563 ASSERT_EQ(retChar, ' ');
564
565 ret = OH_CommonEvent_SetCharToParameters(param, CHAR_KEY, CHAR_VALUE);
566 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
567 exist = OH_CommonEvent_HasKeyInParameters(param, CHAR_KEY);
568 ASSERT_TRUE(exist);
569 retChar = OH_CommonEvent_GetCharFromParameters(param, INVALID_KEY, ' ');
570 ASSERT_EQ(retChar, ' ');
571 retChar = OH_CommonEvent_GetCharFromParameters(param, CHAR_KEY, ' ');
572 ASSERT_EQ(retChar, CHAR_VALUE);
573
574 OH_CommonEvent_DestroyParameters(param);
575 }
576
577 /*
578 * @tc.name: CapiCommonEventTest_1600
579 * @tc.desc: Test create and set char array parameters.
580 * @tc.type: FUNC
581 */
582 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_1600, Function | MediumTest | Level0)
583 {
584 auto param = OH_CommonEvent_CreateParameters();
585
586 int32_t ret = OH_CommonEvent_SetCharArrayToParameters(nullptr, nullptr, nullptr, 0);
587 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
588 ret = OH_CommonEvent_GetCharArrayFromParameters(nullptr, nullptr, nullptr);
589 ASSERT_EQ(ret, 0);
590
591 ret = OH_CommonEvent_SetCharArrayToParameters(param, nullptr, nullptr, 0);
592 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
593 ret = OH_CommonEvent_GetCharArrayFromParameters(param, nullptr, nullptr);
594 ASSERT_EQ(ret, 0);
595
596 ret = OH_CommonEvent_SetCharArrayToParameters(param, CHAR_ARRAY_KEY, CHAR_ARRAY_VALUE, TEST_VALUE_NUM);
597 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
598 bool exist = OH_CommonEvent_HasKeyInParameters(param, CHAR_ARRAY_KEY);
599 ASSERT_TRUE(exist);
600 char** arr = new char*;
601 ret = OH_CommonEvent_GetCharArrayFromParameters(param, INVALID_KEY, arr);
602 ASSERT_EQ(ret, 0);
603 ret = OH_CommonEvent_GetCharArrayFromParameters(param, CHAR_ARRAY_KEY, arr);
604 ASSERT_EQ(ret, TEST_VALUE_NUM + 1);
605
606 OH_CommonEvent_DestroyParameters(param);
607 }
608
609 /*
610 * @tc.name: CapiCommonEventTest_1700
611 * @tc.desc: Test publish commonEvent.
612 * @tc.type: FUNC
613 */
614 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_1700, Function | MediumTest | Level0)
615 {
616 int32_t ret = OH_CommonEvent_Publish(nullptr);
617 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
618
619 ret = OH_CommonEvent_Publish(EVENT);
620 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
621 }
622
623 /*
624 * @tc.name: CapiCommonEventTest_1800
625 * @tc.desc: Test publish commonEvent with publishInfo.
626 * @tc.type: FUNC
627 */
628 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_1800, Function | MediumTest | Level0)
629 {
630 auto publishInfo = OH_CommonEvent_CreatePublishInfo(true);
631 ASSERT_NE(publishInfo, nullptr);
632
633 int32_t ret = OH_CommonEvent_SetPublishInfoBundleName(publishInfo, TEST_BUNDLENAME);
634 ASSERT_STREQ(publishInfo->bundleName.c_str(), TEST_BUNDLENAME);
635 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
636
637 ret = OH_CommonEvent_SetPublishInfoCode(publishInfo, TEST_CODE);
638 ASSERT_EQ(publishInfo->code, TEST_CODE);
639 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
640
641 ret = OH_CommonEvent_SetPublishInfoData(publishInfo, TEST_DATA, strlen(TEST_DATA));
642 ASSERT_STREQ(publishInfo->data.c_str(), TEST_DATA);
643 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
644
645 auto param = OH_CommonEvent_CreateParameters();
646 ret = OH_CommonEvent_SetIntToParameters(param, INT_KEY, INT_VALUE);
647 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
648 ret = OH_CommonEvent_SetPublishInfoParameters(publishInfo, param);
649 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
650
651 const char* events[] = { EVENT, EVENT2 };
652 auto subscribeInfo = OH_CommonEvent_CreateSubscribeInfo(events, TEST_NUM);
653 ASSERT_NE(subscribeInfo, nullptr);
654
655 auto subscriber = OH_CommonEvent_CreateSubscriber(subscribeInfo, OnReceive);
656 ASSERT_NE(subscriber, nullptr);
657
658 ret = OH_CommonEvent_Subscribe(subscriber);
659 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
660
661 ret = OH_CommonEvent_PublishWithInfo(nullptr, nullptr);
662 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
663 ret = OH_CommonEvent_PublishWithInfo(EVENT, nullptr);
664 ASSERT_EQ(ret, COMMONEVENT_ERR_INVALID_PARAMETER);
665 ret = OH_CommonEvent_PublishWithInfo(EVENT, publishInfo);
666 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
667
668 std::this_thread::sleep_for(std::chrono::seconds(SLEEP_TIME));
669
670 ret = OH_CommonEvent_UnSubscribe(subscriber);
671 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
672
673 OH_CommonEvent_DestroySubscriber(subscriber);
674 OH_CommonEvent_DestroyParameters(param);
675 OH_CommonEvent_DestroyPublishInfo(publishInfo);
676 }
677
678 /*
679 * @tc.name: CapiCommonEventTest_1900
680 * @tc.desc: Test handle ordered common event.
681 * @tc.type: FUNC
682 */
683 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_1900, Function | MediumTest | Level0)
684 {
685 auto publishInfo = OH_CommonEvent_CreatePublishInfo(true);
686 ASSERT_NE(publishInfo, nullptr);
687
688 const char* events[] = { EVENT, EVENT2 };
689 auto subscribeInfo = OH_CommonEvent_CreateSubscribeInfo(events, TEST_NUM);
690 ASSERT_NE(subscribeInfo, nullptr);
691
692 auto subscriber = OH_CommonEvent_CreateSubscriber(subscribeInfo, OnReceiveWithoutData);
693 ASSERT_NE(subscriber, nullptr);
694
695 int32_t ret = OH_CommonEvent_Subscribe(subscriber);
696 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
697
698 ret = OH_CommonEvent_PublishWithInfo(EVENT, publishInfo);
699 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
700
701 std::this_thread::sleep_for(std::chrono::seconds(SLEEP_TIME));
702
703 bool retBool = OH_CommonEvent_IsOrderedCommonEvent(subscriber);
704 ASSERT_TRUE(retBool);
705 retBool = OH_CommonEvent_AbortCommonEvent(subscriber);
706 ASSERT_TRUE(retBool);
707 retBool = OH_CommonEvent_GetAbortCommonEvent(subscriber);
708 ASSERT_TRUE(retBool);
709 retBool = OH_CommonEvent_ClearAbortCommonEvent(subscriber);
710 ASSERT_TRUE(retBool);
711 retBool = OH_CommonEvent_GetAbortCommonEvent(subscriber);
712 ASSERT_FALSE(retBool);
713 retBool = OH_CommonEvent_FinishCommonEvent(subscriber);
714 ASSERT_TRUE(retBool);
715
716 ret = OH_CommonEvent_UnSubscribe(subscriber);
717 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
718
719 OH_CommonEvent_DestroySubscriber(subscriber);
720 OH_CommonEvent_DestroyPublishInfo(publishInfo);
721 }
722
723 /*
724 * @tc.name: CapiCommonEventTest_2000
725 * @tc.desc: Test handle ordered common event.
726 * @tc.type: FUNC
727 */
728 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_2000, Function | MediumTest | Level0)
729 {
730 auto publishInfo = OH_CommonEvent_CreatePublishInfo(true);
731 ASSERT_NE(publishInfo, nullptr);
732
733 const char* events[] = { EVENT, EVENT2 };
734 auto subscribeInfo = OH_CommonEvent_CreateSubscribeInfo(events, TEST_NUM);
735 ASSERT_NE(subscribeInfo, nullptr);
736
737 int32_t ret = OH_CommonEvent_SetPublishInfoCode(publishInfo, TEST_CODE);
738 ASSERT_EQ(publishInfo->code, TEST_CODE);
739 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
740
741 ret = OH_CommonEvent_SetPublishInfoData(publishInfo, TEST_DATA, strlen(TEST_DATA));
742 ASSERT_STREQ(publishInfo->data.c_str(), TEST_DATA);
743 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
744
745 auto param = OH_CommonEvent_CreateParameters();
746 ret = OH_CommonEvent_SetPublishInfoParameters(publishInfo, param);
747 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
748
749 auto subscriber = OH_CommonEvent_CreateSubscriber(subscribeInfo, OnReceive);
750 ASSERT_NE(subscriber, nullptr);
751
752 ret = OH_CommonEvent_Subscribe(subscriber);
753 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
754
755 ret = OH_CommonEvent_PublishWithInfo(EVENT, publishInfo);
756 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
757
758 std::this_thread::sleep_for(std::chrono::seconds(SLEEP_TIME));
759
760 bool retBool = OH_CommonEvent_SetCodeToSubscriber(subscriber, TEST_CODE2);
761 ASSERT_TRUE(retBool);
762 retBool = OH_CommonEvent_SetDataToSubscriber(subscriber, TEST_DATA2, strlen(TEST_DATA2));
763 ASSERT_TRUE(retBool);
764 int32_t code = OH_CommonEvent_GetCodeFromSubscriber(subscriber);
765 ASSERT_EQ(code, TEST_CODE2);
766 const char* data = OH_CommonEvent_GetDataFromSubscriber(subscriber);
767 ASSERT_STREQ(data, TEST_DATA2);
768
769 ret = OH_CommonEvent_UnSubscribe(subscriber);
770 ASSERT_EQ(ret, COMMONEVENT_ERR_OK);
771
772 OH_CommonEvent_DestroySubscriber(subscriber);
773 OH_CommonEvent_DestroyParameters(param);
774 OH_CommonEvent_DestroyPublishInfo(publishInfo);
775 }
776
777 /*
778 * @tc.name: CapiCommonEventTest_2100
779 * @tc.desc: Test handle ordered common event with empty subscriber.
780 * @tc.type: FUNC
781 */
782 HWTEST_F(CapiCommonEventTest, CapiCommonEventTest_2100, Function | MediumTest | Level0)
783 {
784 bool ret = OH_CommonEvent_IsOrderedCommonEvent(nullptr);
785 ASSERT_FALSE(ret);
786 ret = OH_CommonEvent_AbortCommonEvent(nullptr);
787 ASSERT_FALSE(ret);
788 ret = OH_CommonEvent_GetAbortCommonEvent(nullptr);
789 ASSERT_FALSE(ret);
790 ret = OH_CommonEvent_ClearAbortCommonEvent(nullptr);
791 ASSERT_FALSE(ret);
792 ret = OH_CommonEvent_FinishCommonEvent(nullptr);
793 ASSERT_FALSE(ret);
794 ret = OH_CommonEvent_SetCodeToSubscriber(nullptr, 0);
795 ASSERT_FALSE(ret);
796 ret = OH_CommonEvent_SetDataToSubscriber(nullptr, nullptr, 0);
797 ASSERT_FALSE(ret);
798 int32_t code = OH_CommonEvent_GetCodeFromSubscriber(nullptr);
799 ASSERT_EQ(code, 0);
800 const char* data = OH_CommonEvent_GetDataFromSubscriber(nullptr);
801 ASSERT_EQ(data, nullptr);
802 }