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