• 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 <cstring>
17 #include <unistd.h>
18 
19 #include "gtest/gtest.h"
20 
21 #include "plugin_manager/include/aie_plugin_info.h"
22 #include "plugin_manager/include/i_plugin_manager.h"
23 #include "utils/log/aie_log.h"
24 
25 using namespace OHOS::AI;
26 using namespace testing::ext;
27 
28 namespace {
29     const std::string AID_DEMO_PLUGIN_SYNC = "sample_plugin_1";
30     const std::string AID_DEMO_PLUGIN_ASYNC = "sample_plugin_2";
31     const std::string AID_PLUGIN_INVALID = "invalid_plugin";
32     const int ALGORITHM_VERSION_VALID = 1;
33 }
34 
35 class PluginManagerTest : public testing::Test {
36 public:
37     // SetUpTestCase:The preset action of the test suite is executed before the first TestCase
SetUpTestCase()38     static void SetUpTestCase() {};
39 
40     // TearDownTestCase:The test suite cleanup action is executed after the last TestCase
TearDownTestCase()41     static void TearDownTestCase() {};
42 
43     // SetUp:Execute before each test case
SetUp()44     void SetUp() {};
45 
46     // TearDown:Execute after each test case
TearDown()47     void TearDown() {};
48 };
49 
TestPluginManager(std::string aid,bool isExceptedLoadPluginSuccess)50 static void TestPluginManager(std::string aid, bool isExceptedLoadPluginSuccess)
51 {
52     IPluginManager *pluginManager = IPluginManager::GetPluginManager();
53     ASSERT_NE(pluginManager, nullptr) << "GetPluginManager test failed.";
54 
55     long long version = ALGORITHM_VERSION_VALID;
56     std::shared_ptr<Plugin> plugin = nullptr;
57     pluginManager->GetPlugin(aid, version, plugin);
58     ASSERT_EQ(isExceptedLoadPluginSuccess, plugin != nullptr) << "pluginManager->GetPlugin test failed.";
59 
60     if (plugin != nullptr) {
61         const char *name = plugin->GetPluginAlgorithm()->GetName();
62         HILOGD("[Test]The plugin name [%s].", name);
63         ASSERT_EQ(isExceptedLoadPluginSuccess, name != nullptr) << "Get plugin name failed.";
64     }
65 
66     pluginManager->UnloadPlugin(aid, version);
67 
68     pluginManager->Destroy();
69 }
70 
TestPluginManagerUnloadPlugin(std::string aid)71 static void TestPluginManagerUnloadPlugin(std::string aid)
72 {
73     long long version = ALGORITHM_VERSION_VALID;
74     IPluginManager *pluginManager2 = IPluginManager::GetPluginManager();
75     ASSERT_NE(pluginManager2, nullptr) << "GetPluginManager test failed.";
76 
77     ASSERT_NO_THROW(pluginManager2->UnloadPlugin(aid, version)) << "UnloadPlugin test failed.";
78 }
79 
80 /**
81  * @tc.name: testPluginManager001
82  * @tc.desc: Test the process of loading and unloading synchronous plugin.
83  * @tc.type: FUNC
84  * @tc.require: AR000F77ON
85  */
86 HWTEST_F(PluginManagerTest, testPluginManager001, TestSize.Level0)
87 {
88     HILOGI("[Test]testPluginManager001.");
89     TestPluginManager(AID_DEMO_PLUGIN_SYNC, true);
90 }
91 
92 /**
93  * @tc.name: testPluginManager002
94  * @tc.desc: Test the process of loading and unloading asynchronous plugin.
95  * @tc.type: FUNC
96  * @tc.require: AR000F77ON
97  */
98 HWTEST_F(PluginManagerTest, testPluginManager002, TestSize.Level0)
99 {
100     HILOGI("[Test]testPluginManager002.");
101     TestPluginManager(AID_DEMO_PLUGIN_ASYNC, true);
102 }
103 
104 /**
105  * @tc.name: testPluginManager003
106  * @tc.desc: Test loading plugin whose plugin id is invalid.
107  * @tc.type: FUNC
108  * @tc.require: AR000F77ON
109  */
110 HWTEST_F(PluginManagerTest, testPluginManager003, TestSize.Level0)
111 {
112     HILOGI("[Test]testPluginManager003.");
113     TestPluginManager(AID_PLUGIN_INVALID, false);
114 }
115 
116 /**
117  * @tc.name: testPluginManager004
118  * @tc.desc: Test the process of unloading synchronous plugin without loading the plugin.
119  * @tc.type: FUNC
120  * @tc.require: AR000F77ON
121  */
122 HWTEST_F(PluginManagerTest, testPluginManager004, TestSize.Level0)
123 {
124     HILOGI("[Test]testPluginManager004.");
125     TestPluginManagerUnloadPlugin(AID_DEMO_PLUGIN_SYNC);
126 }
127 
128 /**
129  * @tc.name: testPluginManager005
130  * @tc.desc: Test the process of unloading asynchronous plugin without loading the plugin.
131  * @tc.type: FUNC
132  * @tc.require: AR000F77ON
133  */
134 HWTEST_F(PluginManagerTest, testPluginManager005, TestSize.Level0)
135 {
136     HILOGI("[Test]testPluginManager005.");
137     TestPluginManagerUnloadPlugin(AID_DEMO_PLUGIN_ASYNC);
138 }
139 
140 /**
141  * @tc.name: testPluginManager006
142  * @tc.desc: Test unloading plugin whose plugin id is invalid.
143  * @tc.type: FUNC
144  * @tc.require: AR000F77ON
145  */
146 HWTEST_F(PluginManagerTest, testPluginManager006, TestSize.Level0)
147 {
148     HILOGI("[Test]testPluginManager006.");
149     TestPluginManagerUnloadPlugin(AID_PLUGIN_INVALID);
150 }
151