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 #include <iostream>
18 #include "abs_image_detector.h"
19 #include "hilog/log.h"
20 #include "log_tags.h"
21 #include "plugin_server.h"
22
23 using OHOS::DelayedRefSingleton;
24 using std::string;
25 using std::vector;
26 using namespace testing::ext;
27 using namespace OHOS::HiviewDFX;
28 using namespace OHOS::MultimediaPlugin;
29 using namespace OHOS::PluginExample;
30
31 static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_TAG_DOMAIN_ID_PLUGIN, "PluginManagerTest" };
32
33 class PluginManagerTest : public testing::Test {
34 public:
35 static void SetUpTestCase(void);
36 static void TearDownTestCase(void);
37 void SetUp();
38 void TearDown();
39
40 static uint32_t DoTestRegister003(OHOS::MultimediaPlugin::PluginServer &pluginServer);
41 static uint32_t DoTestRegister004(OHOS::MultimediaPlugin::PluginServer &pluginServer);
42 static uint32_t DoTestInstanceLimit001(OHOS::MultimediaPlugin::PluginServer &pluginServer);
43 static uint32_t DoTestInstanceLimit003(OHOS::MultimediaPlugin::PluginServer &pluginServer);
44 };
45
SetUpTestCase(void)46 void PluginManagerTest::SetUpTestCase(void)
47 {}
48
TearDownTestCase(void)49 void PluginManagerTest::TearDownTestCase(void)
50 {}
51
SetUp(void)52 void PluginManagerTest::SetUp(void)
53 {}
54
TearDown(void)55 void PluginManagerTest::TearDown(void)
56 {}
57
58 /**
59 * @tc.name: TestRegister001
60 * @tc.desc: Verify that the plugin management module supports the basic scenario of
61 * registering and managing one plugin package in one directory.
62 * @tc.type: FUNC
63 */
64 HWTEST_F(PluginManagerTest, TestRegister001, TestSize.Level3)
65 {
66 /**
67 * @tc.steps: step1. Register one directory with one plugin package.
68 * @tc.expected: step1. The directory was registered successfully.
69 */
70 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
71 vector<string> pluginPaths = { "/system/etc/multimediaplugin/testplugins" };
72 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
73 ASSERT_EQ(ret, SUCCESS);
74
75 /**
76 * @tc.steps: step2. Create a plugin object by class name from the plugin package.
77 * @tc.expected: step2. The plugin object was created successfully.
78 */
79 uint32_t errorCode;
80 string implClassName = "OHOS::PluginExample::CloudLabelDetector";
81 AbsImageDetector *cLabelDetector = pluginServer.CreateObject<AbsImageDetector>(implClassName, errorCode);
82 ASSERT_NE(cLabelDetector, nullptr);
83
84 /**
85 * @tc.steps: step3. Call the member function of the plugin object.
86 * @tc.expected: step3. The member function of the plugin object can be called normally,
87 * and the execution result is correct.
88 */
89 cLabelDetector->Prepare();
90 string result = cLabelDetector->Process();
91 delete cLabelDetector;
92 EXPECT_EQ(result, "CloudLabelDetector");
93 }
94
95 /**
96 * @tc.name: TestRegister002
97 * @tc.desc: Verify that the plugin management module supports the basic scenario of
98 * registering and managing multiple plugins in one directory.
99 * @tc.type: FUNC
100 */
101 HWTEST_F(PluginManagerTest, TestRegister002, TestSize.Level3)
102 {
103 /**
104 * @tc.steps: step1. Register one directory with two plugin packages.
105 * @tc.expected: step1. The directory was registered successfully.
106 */
107 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
108 vector<string> pluginPaths = { "/system/etc/multimediaplugin/testplugins2" };
109 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
110 ASSERT_EQ(ret, SUCCESS);
111
112 /**
113 * @tc.steps: step2. Create a plugin object by class name from the first plugin package.
114 * @tc.expected: step2. The plugin object was created successfully.
115 */
116 string implClassName = "OHOS::PluginExample::CloudLabelDetector2";
117 uint32_t errorCode;
118 AbsImageDetector *cLabelDetector2 = pluginServer.CreateObject<AbsImageDetector>(implClassName, errorCode);
119 ASSERT_NE(cLabelDetector2, nullptr);
120
121 /**
122 * @tc.steps: step3. Call the member function of the plugin object from the first plugin package.
123 * @tc.expected: step3. The member function of the plugin object can be called normally,
124 * and the execution result is correct.
125 */
126 cLabelDetector2->Prepare();
127 string result = cLabelDetector2->Process();
128 delete cLabelDetector2;
129 EXPECT_EQ(result, "CloudLabelDetector2");
130
131 /**
132 * @tc.steps: step4. Create a plugin object by class name from the second plugin package.
133 * @tc.expected: step4. The plugin object was created successfully.
134 */
135 implClassName = "OHOS::PluginExample::LabelDetector3";
136 AbsImageDetector *labelDetector3 = pluginServer.CreateObject<AbsImageDetector>(implClassName, errorCode);
137 ASSERT_NE(labelDetector3, nullptr);
138
139 /**
140 * @tc.steps: step5. Call the member function of the plugin object from the second plugin package.
141 * @tc.expected: step5. The member function of the plugin object can be called normally,
142 * and the execution result is correct.
143 */
144 labelDetector3->Prepare();
145 result = labelDetector3->Process();
146 delete labelDetector3;
147 EXPECT_EQ(result, "LabelDetector3");
148 }
149
150 /**
151 * @tc.name: TestRegister003
152 * @tc.desc: Verify that the plugin management module supports registration of
153 * multiple directories not contain each other.
154 * @tc.type: FUNC
155 */
156 HWTEST_F(PluginManagerTest, TestRegister003, TestSize.Level3)
157 {
158 /**
159 * @tc.steps: step1. Register two non-inclusive directories that contain a total of three plugin packages.
160 * @tc.expected: step1. The directories were registered successfully.
161 */
162 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
163 vector<string> pluginPaths = { "/system/etc/multimediaplugin/testplugins",
164 "/system/etc/multimediaplugin/testplugins2" };
165 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
166 ASSERT_EQ(ret, SUCCESS);
167
168 /**
169 * @tc.steps: step2. Test registered plugin packages can be used normally.
170 * @tc.expected: step2. Plugin objects can be created correctly from the three registered plugin packages.
171 */
172 ASSERT_EQ(DoTestRegister003(pluginServer), SUCCESS);
173 }
174
175 /**
176 * @tc.name: TestRegister004
177 * @tc.desc: Verify that the plugin management module supports the registration of
178 * multiple directories with duplicate relationships.
179 * @tc.type: FUNC
180 */
181 HWTEST_F(PluginManagerTest, TestRegister004, TestSize.Level3)
182 {
183 /**
184 * @tc.steps: step1. Register three directories with duplicate relationships.
185 * @tc.expected: step1. The directories were registered successfully.
186 */
187 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
188 vector<string> pluginPaths = { "/system/etc/multimediaplugin/testplugins2", "/system/etc/multimediaplugin",
189 "/system/etc/multimediaplugin/testplugins2" };
190 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
191 ASSERT_EQ(ret, SUCCESS);
192
193 /**
194 * @tc.steps: step2. Test registered plugin packages can be used normally.
195 * @tc.expected: step2. Plugin objects can be created correctly from registered plugin packages.
196 */
197 ASSERT_EQ(DoTestRegister004(pluginServer), SUCCESS);
198 }
199
200 /**
201 * @tc.name: TestRegister005
202 * @tc.desc: Verify that the plugin management module supports managing
203 * multiple plugin classes in a plugin package.
204 * @tc.type: FUNC
205 */
206 HWTEST_F(PluginManagerTest, TestRegister005, TestSize.Level3)
207 {
208 /**
209 * @tc.steps: step1. Register one plugin packages with two plugin classes.
210 * @tc.expected: step1. The directory was registered successfully.
211 */
212 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
213 vector<string> pluginPaths = { "/system/etc/multimediaplugin/testplugins" };
214 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
215 ASSERT_EQ(ret, SUCCESS);
216
217 /**
218 * @tc.steps: step2. Create a plugin object by class name from the first plugin class.
219 * @tc.expected: step2. The plugin object was created successfully.
220 */
221 uint32_t errorCode;
222 string implClassName = "OHOS::PluginExample::LabelDetector";
223 AbsImageDetector *labelDetector = pluginServer.CreateObject<AbsImageDetector>(implClassName, errorCode);
224 ASSERT_NE(labelDetector, nullptr);
225
226 /**
227 * @tc.steps: step3. Call the member function of the plugin object from the first plugin class.
228 * @tc.expected: step3. The member function of the plugin object can be called normally,
229 * and the execution result is correct.
230 */
231 labelDetector->Prepare();
232 string result = labelDetector->Process();
233 delete labelDetector;
234 EXPECT_EQ(result, "LabelDetector");
235
236 /**
237 * @tc.steps: step4. Create a plugin object by class name from the second plugin class.
238 * @tc.expected: step4. The plugin object was created successfully.
239 */
240 implClassName = "OHOS::PluginExample::CloudLabelDetector";
241 AbsImageDetector *cLabelDetector = pluginServer.CreateObject<AbsImageDetector>(implClassName, errorCode);
242 ASSERT_NE(cLabelDetector, nullptr);
243
244 /**
245 * @tc.steps: step5. Call the member function of the plugin object from the second plugin class.
246 * @tc.expected: step5. The member function of the plugin object can be called normally,
247 * and the execution result is correct.
248 */
249 cLabelDetector->Prepare();
250 result = cLabelDetector->Process();
251 delete cLabelDetector;
252 EXPECT_EQ(result, "CloudLabelDetector");
253 }
254
255 /**
256 * @tc.name: TestCreateByName001
257 * @tc.desc: Verify that the object is not able to be created and
258 * returns the correct error code when the class is not found by class name.
259 * @tc.type: FUNC
260 */
261 HWTEST_F(PluginManagerTest, TestCreateByName001, TestSize.Level3)
262 {
263 /**
264 * @tc.steps: step1. Register a directory with some valid plugin packages.
265 * @tc.expected: step1. The directory was registered successfully.
266 */
267 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
268 vector<string> pluginPaths = { "/system/etc/multimediaplugin/testplugins" };
269 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
270 ASSERT_EQ(ret, SUCCESS);
271
272 /**
273 * @tc.steps: step2. Create a plugin object with a non-existing class name parameter.
274 * @tc.expected: step2. Creation failed and correctly returned error code indicating the reason.
275 */
276 uint32_t errorCode;
277 // "UnknownDetector" means non-existing detector object.
278 AbsImageDetector *unknownDetector = pluginServer.CreateObject<AbsImageDetector>("UnknownDetector", errorCode);
279 EXPECT_EQ(unknownDetector, nullptr);
280
281 delete unknownDetector;
282 EXPECT_EQ(errorCode, ERR_MATCHING_PLUGIN);
283 }
284
285 /**
286 * @tc.name: TestCreateByService001
287 * @tc.desc: Verify that the plugin object can be found and created correctly by service
288 * type id.
289 * @tc.type: FUNC
290 */
291 HWTEST_F(PluginManagerTest, TestCreateByService001, TestSize.Level3)
292 {
293 /**
294 * @tc.steps: step1. Register a directory with some valid plugin packages.
295 * @tc.expected: step1. The directory was registered successfully.
296 */
297 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
298 vector<string> pluginPaths = { "/system/etc/multimediaplugin/testplugins2" };
299 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
300 ASSERT_EQ(ret, SUCCESS);
301
302 /**
303 * @tc.steps: step2. Create a plugin object by servicer type id of face detector.
304 * @tc.expected: step2. The plugin object was created successfully.
305 */
306 uint32_t errorCode;
307 AbsImageDetector *labelDetector =
308 pluginServer.CreateObject<AbsImageDetector>(AbsImageDetector::SERVICE_FACE, errorCode);
309 ASSERT_NE(labelDetector, nullptr);
310
311 /**
312 * @tc.steps: step3. Call the member function of the plugin object of face detector.
313 * @tc.expected: step3. The member function of the plugin object can be called normally,
314 * and the execution result is correct.
315 */
316 labelDetector->Prepare();
317 string result = labelDetector->Process();
318 delete labelDetector;
319 ASSERT_EQ(result, "LabelDetector3");
320
321 /**
322 * @tc.steps: step4. Create a plugin object by servicer type id of text detector.
323 * @tc.expected: step4. The plugin object was created successfully.
324 */
325 AbsImageDetector *cLabelDetector =
326 pluginServer.CreateObject<AbsImageDetector>(AbsImageDetector::SERVICE_TEXT, errorCode);
327 ASSERT_NE(cLabelDetector, nullptr);
328
329 /**
330 * @tc.steps: step5. Call the member function of the plugin object of text detector.
331 * @tc.expected: step5. The member function of the plugin object can be called normally,
332 * and the execution result is correct.
333 */
334 cLabelDetector->Prepare();
335 result = cLabelDetector->Process();
336 delete cLabelDetector;
337 ASSERT_EQ(result, "CloudLabelDetector3");
338 }
339
340 /**
341 * @tc.name: TestCreateByService002
342 * @tc.desc: Verify that the object is not able to be created and return the correct error code
343 * when the matching class is not found by the service type id parameter.
344 * @tc.type: FUNC
345 */
346 HWTEST_F(PluginManagerTest, TestCreateByService002, TestSize.Level3)
347 {
348 AbsImageDetector *unknownDetector = nullptr;
349 HiLog::Debug(LABEL, "[PluginManager_TestCreateByService_002] Start.");
350 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
351
352 /**
353 * @tc.steps: step1. Register a directory with some valid plugin packages.
354 * @tc.expected: step1. The directory was registered successfully.
355 */
356 vector<string> pluginPaths = { "/system/etc/multimediaplugin/testplugins2" };
357 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
358 ASSERT_EQ(ret, SUCCESS);
359
360 /**
361 * @tc.steps: step2. Create a plugin object with a non-existing service type id parameter.
362 * @tc.expected: step2. Creation failed and correctly returned error code indicating the reason.
363 */
364 uint32_t errorCode;
365 unknownDetector = pluginServer.CreateObject<AbsImageDetector>(AbsImageDetector::SERVICE_FLOWER, errorCode);
366 EXPECT_EQ(unknownDetector, nullptr);
367
368 delete unknownDetector;
369 EXPECT_EQ(errorCode, ERR_MATCHING_PLUGIN);
370 }
371
372 /**
373 * @tc.name: TestCreateByCapabilities001
374 * @tc.desc: Verify that the plugin object can be found and created correctly by capabilities.
375 * @tc.type: FUNC
376 */
377 HWTEST_F(PluginManagerTest, TestCreateByCapabilities001, TestSize.Level3)
378 {
379 /**
380 * @tc.steps: step1. Register multiple plugin directories with multiple valid plugin packages.
381 * @tc.expected: step1. The directories were registered successfully.
382 */
383 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
384 vector<string> pluginPaths = { "/system/etc/multimediaplugin", "/system/etc/multimediaplugin/testplugins2" };
385 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
386 ASSERT_EQ(ret, SUCCESS);
387
388 /**
389 * @tc.steps: step2. Create a plugin object by servicer type id and capabilities.
390 * @tc.expected: step2. The plugin object was created successfully.
391 */
392 uint32_t errorCode;
393 // "labelNum" means capability name, 10000 means capability value, exist in metadata.
394 map<string, AttrData> capabilities = { { "labelNum", AttrData(static_cast<uint32_t>(10000)) } };
395 AbsImageDetector *labelDetector =
396 pluginServer.CreateObject<AbsImageDetector>(AbsImageDetector::SERVICE_LABEL, capabilities, errorCode);
397 ASSERT_NE(labelDetector, nullptr);
398
399 /**
400 * @tc.steps: step3. Call the member function of the plugin object.
401 * @tc.expected: step4. The member function of the plugin object can be called normally,
402 * and the execution result is correct.
403 */
404 labelDetector->Prepare();
405 string result = labelDetector->Process();
406 delete labelDetector;
407 ASSERT_EQ(result, "CloudLabelDetector");
408 }
409
410 /**
411 * @tc.name: TestCreateByCapabilities002
412 * @tc.desc: Verify that the object is not able to be created and return the correct error code
413 * when the matching class is not found by the capabilities.
414 * @tc.type: FUNC
415 */
416 HWTEST_F(PluginManagerTest, TestCreateByCapabilities002, TestSize.Level3)
417 {
418 /**
419 * @tc.steps: step1. Register multiple plugin directories with multiple valid plugin packages.
420 * @tc.expected: step1. The directories were registered successfully.
421 */
422 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
423 vector<string> pluginPaths = { "/system/etc/multimediaplugin/testplugins2", "/system/etc/multimediaplugin",
424 "/system/etc/multimediaplugin/testplugins2" };
425 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
426 ASSERT_EQ(ret, SUCCESS);
427
428 /**
429 * @tc.steps: step2. Create a plugin object with a non-existing service type id and capabilities parameter.
430 * @tc.expected: step2. Creation failed and correctly returned error code indicating the reason.
431 */
432 uint32_t errorCode;
433 // "labelNum" means capability name, 128 means capability value, not exist in metadata.
434 map<string, AttrData> capabilities = { { "labelNum", AttrData(static_cast<uint32_t>(128)) } };
435 AbsImageDetector *unknownDetector =
436 pluginServer.CreateObject<AbsImageDetector>(AbsImageDetector::SERVICE_LABEL, capabilities, errorCode);
437 EXPECT_EQ(unknownDetector, nullptr);
438 delete unknownDetector;
439 EXPECT_EQ(errorCode, ERR_MATCHING_PLUGIN);
440 }
441
442 /**
443 * @tc.name: TestPluginPriority001
444 * @tc.desc: Verify that the plugin class static priority function is correct.
445 * @tc.type: FUNC
446 */
447 HWTEST_F(PluginManagerTest, TestPluginPriority001, TestSize.Level3)
448 {
449 /**
450 * @tc.steps: step1. Register multiple plugin directories with multiple valid plugin packages.
451 * @tc.expected: step1. The directories were registered successfully.
452 */
453 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
454 vector<string> pluginPaths = { "/system/etc/multimediaplugin", "/system/etc/multimediaplugin/testplugins2" };
455 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
456 ASSERT_EQ(ret, SUCCESS);
457
458 /**
459 * @tc.steps: step2. Create a plugin object by servicer type id and there are multiple classes
460 * that can match the id.
461 * @tc.expected: step2. The highest priority matching plugin object was created successfully.
462 */
463 uint32_t errorCode;
464 AbsImageDetector *labelDetector =
465 pluginServer.CreateObject<AbsImageDetector>(AbsImageDetector::SERVICE_LABEL, errorCode);
466 ASSERT_NE(labelDetector, nullptr);
467
468 /**
469 * @tc.steps: step3. Call the member function of the plugin object.
470 * @tc.expected: step4. The member function of the plugin object can be called normally,
471 * and the execution result is correct.
472 */
473 labelDetector->Prepare();
474 string result = labelDetector->Process();
475 delete labelDetector;
476 // here, the higher target is LabelDetector and is not CloudLabelDetector.
477 ASSERT_EQ(result, "LabelDetector");
478 }
479
480 /**
481 * @tc.name: TestPluginPriority002
482 * @tc.desc: Verify that the plugin class dynamic priority is correct and
483 * takes precedence over static priority.
484 * @tc.type: FUNC
485 */
486 HWTEST_F(PluginManagerTest, TestPluginPriority002, TestSize.Level3)
487 {
488 /**
489 * @tc.steps: step1. Register multiple plugin directories with multiple valid plugin packages.
490 * @tc.expected: step1. The directories were registered successfully.
491 */
492 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
493 vector<string> pluginPaths = { "/system/etc/multimediaplugin", "/system/etc/multimediaplugin/testplugins2" };
494 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
495 ASSERT_EQ(ret, SUCCESS);
496
497 /**
498 * @tc.steps: step2. Create a plugin object by servicer type id and priorityScheme, and there are multiple classes
499 * that can match the id.
500 * @tc.expected: step2. The highest priority matching plugin object was created successfully.
501 */
502 uint32_t errorCode;
503 // "labelNum" means attrdata key, exist in metedata.
504 PriorityScheme priorityScheme = { PriorityType::PRIORITY_ORDER_BY_ATTR_DESCENDING, "labelNum" };
505 AbsImageDetector *labelDetector =
506 pluginServer.CreateObject<AbsImageDetector>(AbsImageDetector::SERVICE_LABEL, priorityScheme, errorCode);
507 ASSERT_NE(labelDetector, nullptr);
508
509 /**
510 * @tc.steps: step3. Call the member function of the plugin object.
511 * @tc.expected: step4. The member function of the plugin object can be called normally,
512 * and the execution result is correct.
513 */
514 labelDetector->Prepare();
515 string result = labelDetector->Process();
516 delete labelDetector;
517 // here, the higher target is CloudLabelDetector and is not LabelDetector.
518 ASSERT_EQ(result, "CloudLabelDetector");
519 }
520
521 /**
522 * @tc.name: TestGetClassByService001
523 * @tc.desc: Verify that the plugin object can be found and get classes info correctly by service
524 * type id.
525 * @tc.type: FUNC
526 */
527 HWTEST_F(PluginManagerTest, TestGetClassByService001, TestSize.Level3)
528 {
529 /**
530 * @tc.steps: step1. Register a directory with some valid plugin packages.
531 * @tc.expected: step1. The directory was registered successfully.
532 */
533 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
534 vector<string> pluginPaths = { "/system/etc/multimediaplugin", "/system/etc/multimediaplugin/testplugins2" };
535 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
536 ASSERT_EQ(ret, SUCCESS);
537
538 /**
539 * @tc.steps: step2. get classes information list by servicer type id of face detector.
540 * @tc.expected: step2. The getclass info result successfully.
541 */
542 vector<ClassInfo> classInfo;
543 uint32_t errorCode =
544 pluginServer.PluginServerGetClassInfo<AbsImageDetector>(AbsImageDetector::SERVICE_FACE, classInfo);
545 EXPECT_NE(classInfo.size(), 0UL); // existing service type id, get class success, size should not be zero.
546 EXPECT_EQ(errorCode, SUCCESS);
547
548 /**
549 * @tc.steps: step3. get classes information list by servicer type id of text detector.
550 * @tc.expected: step3. The getclass info result successfully.
551 */
552 errorCode = pluginServer.PluginServerGetClassInfo<AbsImageDetector>(AbsImageDetector::SERVICE_TEXT, classInfo);
553 EXPECT_NE(classInfo.size(), 0UL); // existing service type id, get class success, size should not be zero.
554 EXPECT_EQ(errorCode, SUCCESS);
555 }
556
557 /**
558 * @tc.name: TestGetClassByService002
559 * @tc.desc: Verify that the plugin classes can not be found by non-existing service type id.
560 * @tc.type: FUNC
561 */
562 HWTEST_F(PluginManagerTest, TestGetClassByService002, TestSize.Level3)
563 {
564 /**
565 * @tc.steps: step1. Register a directory with some valid plugin packages.
566 * @tc.expected: step1. The directory was registered successfully.
567 */
568 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
569 vector<string> pluginPaths = { "/system/etc/multimediaplugin", "/system/etc/multimediaplugin/testplugins2" };
570 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
571 ASSERT_EQ(ret, SUCCESS);
572
573 /**
574 * @tc.steps: step2. get classes information with non-existing service type id parameter.
575 * @tc.expected: step2. The getclass info result fail.
576 */
577 vector<ClassInfo> classInfo;
578 uint32_t errorCode =
579 pluginServer.PluginServerGetClassInfo<AbsImageDetector>(AbsImageDetector::SERVICE_FLOWER, classInfo);
580 ASSERT_EQ(classInfo.size(), 0UL); // non-existing service type id, get class success, size should be zero.
581 ASSERT_NE(errorCode, SUCCESS);
582 }
583
584 /**
585 * @tc.name: TestGetClassByCapbility001
586 * @tc.desc: Verify that the plugin classes can be found and get classes info correctly by service
587 * type id.
588 * @tc.type: FUNC
589 */
590 HWTEST_F(PluginManagerTest, TestGetClassByCapbility001, TestSize.Level3)
591 {
592 /**
593 * @tc.steps: step1. Register a directory with some valid plugin packages.
594 * @tc.expected: step1. The directory was registered successfully.
595 */
596 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
597 vector<string> pluginPaths = { "/system/etc/multimediaplugin", "/system/etc/multimediaplugin/testplugins2" };
598 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
599 ASSERT_EQ(ret, SUCCESS);
600
601 /**
602 * @tc.steps: step2. get classes information list by servicer type id of face detector and capbilities.
603 * @tc.expected: step2. The getclass info result successfully.
604 */
605 vector<ClassInfo> classInfo;
606 // "labelNum" means capability name, 256 means capability value, exist in metedata.
607 map<string, AttrData> capabilities = { { "labelNum", AttrData(static_cast<uint32_t>(256)) } };
608 uint32_t errorCode =
609 pluginServer.PluginServerGetClassInfo<AbsImageDetector>(AbsImageDetector::SERVICE_FACE, capabilities, classInfo);
610 ASSERT_NE(classInfo.size(), 0UL); // existing service type id, get class success, size should not be zero.
611 ASSERT_EQ(errorCode, SUCCESS);
612 }
613
614 /**
615 * @tc.name: TestGetClassByCapbility002
616 * @tc.desc: Verify that the plugin classes can not be found by the correct service type id
617 * but the non-existing capbility.
618 * @tc.type: FUNC
619 */
620 HWTEST_F(PluginManagerTest, TestGetClassByCapbility002, TestSize.Level3)
621 {
622 /**
623 * @tc.steps: step1. Register a directory with some valid plugin packages.
624 * @tc.expected: step1. The directory was registered successfully.
625 */
626 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
627 vector<string> pluginPaths = { "/system/etc/multimediaplugin", "/system/etc/multimediaplugin/testplugins2" };
628 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
629 ASSERT_EQ(ret, SUCCESS);
630
631 /**
632 * @tc.steps: step2. get classes information list by servicer type id of face detector and capbilities.
633 * @tc.expected: step2. The getclass info result successfully.
634 */
635 vector<ClassInfo> classInfo;
636 // "labelNum1" means capability name, 1000 means capability value, not exist in metedata.
637 map<string, AttrData> capabilities = { { "labelNum1", AttrData(static_cast<uint32_t>(1000)) } };
638 uint32_t errorCode =
639 pluginServer.PluginServerGetClassInfo<AbsImageDetector>(AbsImageDetector::SERVICE_FACE, capabilities, classInfo);
640 ASSERT_EQ(classInfo.size(), 0UL); // non-existing service type id, get class success, size should be zero.
641 ASSERT_NE(errorCode, SUCCESS);
642 }
643
644 /**
645 * @tc.name: TestInstanceLimit001
646 * @tc.desc: Verify cross-create multiple plugin objects within the limit of the number of instances.
647 * @tc.type: FUNC
648 */
649 HWTEST_F(PluginManagerTest, TestInstanceLimit001, TestSize.Level3)
650 {
651 /**
652 * @tc.steps: step1. Register a directory with some valid plugin packages.
653 * @tc.expected: step1. The directory was registered successfully.
654 */
655 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
656 vector<string> pluginPaths = { "/system/etc/multimediaplugin/testplugins" };
657 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
658 ASSERT_EQ(ret, SUCCESS);
659
660 /**
661 * @tc.steps: step2. Cross-create multiple plugin objects within the limit of the number of instances.
662 * @tc.expected: step2. The plugin objects were created successfully.
663 */
664 ASSERT_EQ(DoTestInstanceLimit001(pluginServer), SUCCESS);
665 }
666
667 /**
668 * @tc.name: TestInstanceLimit002
669 * @tc.desc: Verify create multiple plugin objects belonging to the same class, up to the
670 * maximum number of instances.
671 * @tc.type: FUNC
672 */
673 HWTEST_F(PluginManagerTest, TestInstanceLimit002, TestSize.Level3)
674 {
675 /**
676 * @tc.steps: step1. Register a directory with some valid plugin packages.
677 * @tc.expected: step1. The directory was registered successfully.
678 */
679 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
680 vector<string> pluginPaths = { "/system/etc/multimediaplugin/testplugins2" };
681 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
682 ASSERT_EQ(ret, SUCCESS);
683
684 /**
685 * @tc.steps: step2. Create multiple plugin objects belonging to the same class,
686 * up to the maximum number of instances.
687 * @tc.expected: step2. The plugin objects were created successfully.
688 */
689 uint32_t errorCode;
690 AbsImageDetector *labelDetectorIns1 =
691 pluginServer.CreateObject<AbsImageDetector>(AbsImageDetector::SERVICE_FACE, errorCode);
692 EXPECT_NE(labelDetectorIns1, nullptr);
693 delete labelDetectorIns1;
694
695 AbsImageDetector *labelDetectorIns2 =
696 pluginServer.CreateObject<AbsImageDetector>(AbsImageDetector::SERVICE_FACE, errorCode);
697 EXPECT_NE(labelDetectorIns2, nullptr);
698 delete labelDetectorIns2;
699
700 AbsImageDetector *labelDetectorIns3 =
701 pluginServer.CreateObject<AbsImageDetector>(AbsImageDetector::SERVICE_FACE, errorCode);
702 EXPECT_NE(labelDetectorIns3, nullptr);
703 delete labelDetectorIns3;
704 }
705
706 /**
707 * @tc.name: TestInstanceLimit003
708 * @tc.desc: Verify that the number of instances limit mechanism is correct.
709 * @tc.type: FUNC
710 */
711 HWTEST_F(PluginManagerTest, TestInstanceLimit003, TestSize.Level3)
712 {
713 /**
714 * @tc.steps: step1. Register a directory with some valid plugin packages.
715 * @tc.expected: step1. The directory was registered successfully.
716 */
717 PluginServer &pluginServer = DelayedRefSingleton<PluginServer>::GetInstance();
718 vector<string> pluginPaths = { "/system/etc/multimediaplugin/testplugins2" };
719 uint32_t ret = pluginServer.Register(std::move(pluginPaths));
720 ASSERT_EQ(ret, SUCCESS);
721
722 ASSERT_EQ(DoTestInstanceLimit003(pluginServer), SUCCESS);
723 }
724
725 // ------------------------------- private method -------------------------------
726 /*
727 * Feature: MultiMedia
728 * Function: Plugins
729 * SubFunction: Plugins Manager
730 * FunctionPoints: Registering and managing multiple Plugins
731 * EnvConditions: NA
732 * CaseDescription: Verify that the plugin management module supports registration of
733 * multiple directories not contain each other.
734 */
DoTestRegister003(PluginServer & pluginServer)735 uint32_t PluginManagerTest::DoTestRegister003(PluginServer &pluginServer)
736 {
737 uint32_t testRet = ERR_GENERAL;
738 uint32_t errorCode;
739 AbsImageDetector *labelDetector3 = nullptr;
740 AbsImageDetector *cLabelDetector = nullptr;
741 string result;
742 string implClassName = "OHOS::PluginExample::CloudLabelDetector2";
743 AbsImageDetector *cLabelDetector2 = pluginServer.CreateObject<AbsImageDetector>(implClassName, errorCode);
744 if (cLabelDetector2 == nullptr) {
745 HiLog::Error(LABEL, "[DoTestRegister003] cLabelDetector2 null. ERRNO: %{public}u.", errorCode);
746 goto TEST_END;
747 }
748
749 implClassName = "OHOS::PluginExample::LabelDetector3";
750 labelDetector3 = pluginServer.CreateObject<AbsImageDetector>(implClassName, errorCode);
751 if (labelDetector3 == nullptr) {
752 HiLog::Error(LABEL, "[DoTestRegister003] labelDetector3 null. ERRNO: %{public}u.", errorCode);
753 goto TEST_END;
754 }
755
756 implClassName = "OHOS::PluginExample::CloudLabelDetector";
757 cLabelDetector = pluginServer.CreateObject<AbsImageDetector>(implClassName, errorCode);
758 if (cLabelDetector == nullptr) {
759 HiLog::Error(LABEL, "[DoTestRegister003] cLabelDetector null. ERRNO: %{public}u.", errorCode);
760 goto TEST_END;
761 }
762
763 result = cLabelDetector2->Process();
764 if (result != "CloudLabelDetector2") {
765 HiLog::Error(LABEL, "[DoTestRegister003] result1 check fail, result: %{public}s.", result.c_str());
766 goto TEST_END;
767 }
768
769 result = labelDetector3->Process();
770 if (result != "LabelDetector3") {
771 HiLog::Error(LABEL, "[DoTestRegister003] result2 check fail, result: %{public}s.", result.c_str());
772 goto TEST_END;
773 }
774
775 result = cLabelDetector->Process();
776 if (result != "CloudLabelDetector") {
777 HiLog::Error(LABEL, "[DoTestRegister003] result3 check fail, result: %{public}s.", result.c_str());
778 goto TEST_END;
779 }
780 testRet = SUCCESS;
781
782 TEST_END:
783 delete cLabelDetector;
784 delete cLabelDetector2;
785 delete labelDetector3;
786
787 return testRet;
788 }
789
790 /*
791 * Feature: MultiMedia
792 * Function: Plugins
793 * SubFunction: Plugins Manager
794 * FunctionPoints: Registering and managing multiple Plugins
795 * EnvConditions: NA
796 * CaseDescription: Verify that the plugin management module supports the registration of
797 * multiple directories with duplicate relationships.
798 */
DoTestRegister004(PluginServer & pluginServer)799 uint32_t PluginManagerTest::DoTestRegister004(PluginServer &pluginServer)
800 {
801 uint32_t testRet = ERR_GENERAL;
802 uint32_t errorCode;
803 AbsImageDetector *cLabelDetector2 = nullptr;
804 AbsImageDetector *labelDetector3 = nullptr;
805 string result;
806 string implClassName = "OHOS::PluginExample::CloudLabelDetector";
807 AbsImageDetector *cLabelDetector = pluginServer.CreateObject<AbsImageDetector>(implClassName, errorCode);
808 if (cLabelDetector == nullptr) {
809 HiLog::Error(LABEL, "[DoTestRegister004] cLabelDetector null. ERRNO: %{public}u.", errorCode);
810 goto TEST_END;
811 }
812
813 implClassName = "OHOS::PluginExample::CloudLabelDetector2";
814 cLabelDetector2 = pluginServer.CreateObject<AbsImageDetector>(implClassName, errorCode);
815 if (cLabelDetector2 == nullptr) {
816 HiLog::Error(LABEL, "[DoTestRegister004] cLabelDetector2 null. ERRNO: %{public}u.", errorCode);
817 goto TEST_END;
818 }
819
820 implClassName = "OHOS::PluginExample::LabelDetector3";
821 labelDetector3 = pluginServer.CreateObject<AbsImageDetector>(implClassName, errorCode);
822 if (labelDetector3 == nullptr) {
823 HiLog::Error(LABEL, "[DoTestRegister004] labelDetector3 null. ERRNO: %{public}u.", errorCode);
824 goto TEST_END;
825 }
826
827 result = cLabelDetector->Process();
828 if (result != "CloudLabelDetector") {
829 HiLog::Error(LABEL, "[DoTestRegister004] result1 check fail, result: %{public}s.", result.c_str());
830 goto TEST_END;
831 }
832
833 result = cLabelDetector2->Process();
834 if (result != "CloudLabelDetector2") {
835 HiLog::Error(LABEL, "[DoTestRegister004] result2 check fail, result: %{public}s.", result.c_str());
836 goto TEST_END;
837 }
838
839 result = labelDetector3->Process();
840 if (result != "LabelDetector3") {
841 HiLog::Error(LABEL, "[DoTestRegister004] result3 check fail, result: %{public}s.", result.c_str());
842 return ERR_GENERAL;
843 }
844 testRet = SUCCESS;
845
846 TEST_END:
847 delete cLabelDetector;
848 delete cLabelDetector2;
849 delete labelDetector3;
850
851 return testRet;
852 }
853
854 /*
855 * Feature: MultiMedia
856 * Function: Plugins
857 * SubFunction: Reference count
858 * FunctionPoints: Registering and managing multiple Plugins
859 * EnvConditions: NA
860 * CaseDescription: Verify cross-create multiple plugin objects within the limit of the number of instances.
861 */
DoTestInstanceLimit001(PluginServer & pluginServer)862 uint32_t PluginManagerTest::DoTestInstanceLimit001(PluginServer &pluginServer)
863 {
864 uint32_t testRet = ERR_GENERAL;
865 uint32_t errorCode;
866 AbsImageDetector *labelDetectorIns1 = nullptr;
867 AbsImageDetector *cLabelDetectorIns1 = nullptr;
868 AbsImageDetector *labelDetectorIns2 = nullptr;
869 AbsImageDetector *cLabelDetectorIns2 = nullptr;
870 string implClassName = "OHOS::PluginExample::LabelDetector";
871 labelDetectorIns1 = pluginServer.CreateObject<AbsImageDetector>(implClassName, errorCode);
872 if (labelDetectorIns1 == nullptr) {
873 HiLog::Error(LABEL, "[PluginManager_TestInstanceLimit_001] labelDetectorIns1 null. ERRNO: %{public}u.",
874 errorCode);
875 goto TEST_END;
876 }
877
878 implClassName = "OHOS::PluginExample::CloudLabelDetector";
879 cLabelDetectorIns1 = pluginServer.CreateObject<AbsImageDetector>(implClassName, errorCode);
880 if (cLabelDetectorIns1 == nullptr) {
881 HiLog::Error(LABEL, "[PluginManager_TestInstanceLimit_001] cLabelDetectorIns1 null. ERRNO: %{public}u.",
882 errorCode);
883 goto TEST_END;
884 }
885
886 implClassName = "OHOS::PluginExample::LabelDetector";
887 labelDetectorIns2 = pluginServer.CreateObject<AbsImageDetector>(implClassName, errorCode);
888 if (labelDetectorIns2 == nullptr) {
889 HiLog::Error(LABEL, "[PluginManager_TestInstanceLimit_001] labelDetectorIns2 null. ERRNO: %{public}u.",
890 errorCode);
891 goto TEST_END;
892 }
893
894 implClassName = "OHOS::PluginExample::CloudLabelDetector";
895 cLabelDetectorIns2 = pluginServer.CreateObject<AbsImageDetector>(implClassName, errorCode);
896 if (cLabelDetectorIns2 == nullptr) {
897 HiLog::Error(LABEL, "[PluginManager_TestInstanceLimit_001] cLabelDetectorIns2 null. ERRNO: %{public}u.",
898 errorCode);
899 goto TEST_END;
900 }
901 testRet = SUCCESS;
902
903 TEST_END:
904 delete labelDetectorIns1;
905 delete cLabelDetectorIns1;
906 delete labelDetectorIns2;
907 delete cLabelDetectorIns2;
908
909 return testRet;
910 }
911
912 /*
913 * Feature: MultiMedia
914 * Function: Plugins
915 * SubFunction: Plugins Manager
916 * FunctionPoints: Instance number
917 * EnvConditions: NA
918 * CaseDescription: Verify that the number of instances limit mechanism is correct.
919 */
DoTestInstanceLimit003(PluginServer & pluginServer)920 uint32_t PluginManagerTest::DoTestInstanceLimit003(PluginServer &pluginServer)
921 {
922 uint32_t testRet = ERR_GENERAL;
923 uint32_t errorCode;
924 AbsImageDetector *labelDetectorIns2 = nullptr;
925 AbsImageDetector *labelDetectorIns3 = nullptr;
926 AbsImageDetector *labelDetectorIns4 = nullptr;
927 AbsImageDetector *labelDetectorIns5 = nullptr;
928
929 /**
930 * @tc.steps: step2. Create multiple plugin objects belonging to the same class,
931 * the number of which exceeds the maximum number of instances.
932 * @tc.expected: step2. The part that did not exceed the number of instances was created successfully,
933 * the excess part was created failed and an error code indicating the reason for
934 * the name was returned.
935 */
936 AbsImageDetector *labelDetectorIns1 =
937 pluginServer.CreateObject<AbsImageDetector>(AbsImageDetector::SERVICE_FACE, errorCode);
938 if (labelDetectorIns1 == nullptr) {
939 HiLog::Error(LABEL, "[DoTestInstanceLimit003] labelDetectorIns1 null. ERRNO: %{public}u.", errorCode);
940 goto TEST_END;
941 }
942
943 labelDetectorIns2 = pluginServer.CreateObject<AbsImageDetector>(AbsImageDetector::SERVICE_FACE, errorCode);
944 if (labelDetectorIns1 == nullptr) {
945 HiLog::Error(LABEL, "[DoTestInstanceLimit003] labelDetectorIns2 null. ERRNO: %{public}u.", errorCode);
946 goto TEST_END;
947 }
948
949 labelDetectorIns3 = pluginServer.CreateObject<AbsImageDetector>(AbsImageDetector::SERVICE_FACE, errorCode);
950 if (labelDetectorIns1 == nullptr) {
951 HiLog::Error(LABEL, "[DoTestInstanceLimit003] labelDetectorIns3 null. ERRNO: %{public}u.", errorCode);
952 goto TEST_END;
953 }
954
955 labelDetectorIns4 = pluginServer.CreateObject<AbsImageDetector>(AbsImageDetector::SERVICE_FACE, errorCode);
956 if (labelDetectorIns4 != nullptr) {
957 HiLog::Error(LABEL, "[DoTestInstanceLimit003] labelDetectorIns4 not null. ERRNO: %{public}u.", errorCode);
958 goto TEST_END;
959 }
960
961 if (errorCode != ERR_INSTANCE_LIMIT) {
962 HiLog::Error(LABEL, "[DoTestInstanceLimit003] unexpected errorCode: %{public}u.", errorCode);
963 goto TEST_END;
964 }
965
966 /**
967 * @tc.steps: step3. Release a plugin object, making the number of instances below the limit,
968 * then request to create a new plugin object.
969 * @tc.expected: step3. The new plugin object was created successfully.
970 */
971 delete labelDetectorIns2;
972 labelDetectorIns2 = nullptr;
973
974 labelDetectorIns5 = pluginServer.CreateObject<AbsImageDetector>(AbsImageDetector::SERVICE_FACE, errorCode);
975 if (labelDetectorIns1 == nullptr) {
976 HiLog::Error(LABEL, "[DoTestInstanceLimit003] labelDetectorIns5 null. ERRNO: %{public}u.", errorCode);
977 goto TEST_END;
978 }
979 testRet = SUCCESS;
980
981 TEST_END:
982 delete labelDetectorIns1;
983 delete labelDetectorIns2;
984 delete labelDetectorIns3;
985 delete labelDetectorIns4;
986 delete labelDetectorIns5;
987
988 return testRet;
989 }
990