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