1 /*
2 * Copyright (c) 2022-2023 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 #define private public
17 #include <dlfcn.h>
18 #include "plugin_mgr.h"
19 #include "res_type.h"
20 #include "plugin_mgr_test.h"
21 #include "socperf_plugin.h"
22 #undef private
23 #include "mock_plugin_mgr.h"
24 #include "res_data.h"
25 #include "res_type.h"
26
27 using namespace std;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace ResourceSchedule {
32 namespace {
33 const string LIB_NAME = "libunittest_plugin.z.so";
34 }
35
SetUpTestCase()36 void PluginMgrTest::SetUpTestCase() {}
37
TearDownTestCase()38 void PluginMgrTest::TearDownTestCase() {}
39
SetUp()40 void PluginMgrTest::SetUp()
41 {
42 /**
43 * @tc.setup: initialize the member variable pluginMgr_
44 */
45 pluginMgr_ = make_shared<MockPluginMgr>();
46 }
47
TearDown()48 void PluginMgrTest::TearDown()
49 {
50 /**
51 * @tc.teardown: clear pluginMgr_
52 */
53 pluginMgr_ = nullptr;
54 }
55
56 /**
57 * @tc.name: Plugin mgr test Init 001
58 * @tc.desc: Verify if can init success.
59 * @tc.type: FUNC
60 * @tc.require: issueI798UT
61 * @tc.author:xukuan
62 */
63 HWTEST_F(PluginMgrTest, Init001, TestSize.Level1)
64 {
65 pluginMgr_->Init();
66 EXPECT_TRUE(pluginMgr_->initStatus == pluginMgr_->INIT_SUCCESS);
67 EXPECT_EQ(pluginMgr_->pluginLibMap_.size(), 0);
68 }
69
70 /**
71 * @tc.name: Plugin mgr test Stop 001
72 * @tc.desc: Verify if can stop success.
73 * @tc.type: FUNC
74 * @tc.require: issueI798UT
75 * @tc.author:xukuan
76 */
77 HWTEST_F(PluginMgrTest, Stop001, TestSize.Level1)
78 {
79 pluginMgr_->Stop();
80 EXPECT_EQ(pluginMgr_->pluginLibMap_.size(), 0);
81 EXPECT_EQ(pluginMgr_->resTypeLibMap_.size(), 0);
82 EXPECT_TRUE(pluginMgr_->configReader_ == nullptr);
83 }
84
85 /**
86 * @tc.name: Plugin mgr test GetConfig 001
87 * @tc.desc: Verify if can get config with wrong env.
88 * @tc.type: FUNC
89 * @tc.require: issueI5WWV3
90 * @tc.author:lice
91 */
92 HWTEST_F(PluginMgrTest, GetConfig001, TestSize.Level1)
93 {
94 PluginConfig config = pluginMgr_->GetConfig("", "");
95 EXPECT_TRUE(config.itemList.empty());
96 }
97
98 /**
99 * @tc.name: Plugin mgr test GetRunner 001
100 * @tc.desc: Verify if can get runner.
101 * @tc.type: FUNC
102 * @tc.require: issueI6NPEV
103 * @tc.author:liyi
104 */
105 HWTEST_F(PluginMgrTest, GetRunner001, TestSize.Level1)
106 {
107 EXPECT_TRUE(pluginMgr_->GetRunner() != nullptr);
108 }
109
110 /**
111 * @tc.name: Plugin mgr test SubscribeResource 001
112 * @tc.desc: Verify if can stop success.
113 * @tc.type: FUNC
114 * @tc.require: issueI798UT
115 * @tc.author:xukuan
116 */
117 HWTEST_F(PluginMgrTest, SubscribeResource001, TestSize.Level1)
118 {
119 pluginMgr_->Init();
120 pluginMgr_->SubscribeResource(LIB_NAME, ResType::RES_TYPE_SCREEN_STATUS);
121 auto iter = pluginMgr_->resTypeLibMap_.find(ResType::RES_TYPE_SCREEN_STATUS);
122 string libName = iter->second.back();
123 EXPECT_EQ(libName.compare(LIB_NAME), 0);
124 }
125
126 /**
127 * @tc.name: Plugin mgr test Dump 001
128 * @tc.desc: Verify if dump commands is success.
129 * @tc.type: FUNC
130 * @tc.require: issueI5WWV3
131 * @tc.author:lice
132 */
133 HWTEST_F(PluginMgrTest, Dump001, TestSize.Level1)
134 {
135 std::string res;
136 pluginMgr_->Init();
137 pluginMgr_->DumpAllPlugin(res);
138 EXPECT_TRUE(!res.empty());
139 res = "";
140
141 pluginMgr_->LoadPlugin();
142 pluginMgr_->DumpHelpFromPlugin(res);
143 EXPECT_TRUE(res.empty());
144 res = "";
145
146 std::vector<std::string> args;
147 pluginMgr_->DumpOnePlugin(res, LIB_NAME, args);
148 EXPECT_TRUE(!res.empty());
149 res = "";
150
151 args.emplace_back("-h");
152 pluginMgr_->DumpOnePlugin(res, LIB_NAME, args);
153 EXPECT_TRUE(!res.empty());
154 }
155
156 /**
157 * @tc.name: Plugin mgr test RepairPlugin 001
158 * @tc.desc: Verify if RepairPlugin is success.
159 * @tc.type: FUNC
160 * @tc.require: issueI5WWV3
161 * @tc.author:lice
162 */
163 HWTEST_F(PluginMgrTest, RepairPlugin001, TestSize.Level1)
164 {
165 PluginLib libInfo = pluginMgr_->pluginLibMap_.find(LIB_NAME)->second;
166 pluginMgr_->RepairPlugin(Clock::now(), LIB_NAME, libInfo);
167 EXPECT_TRUE(true);
168 }
169
170 /**
171 * @tc.name: Plugin mgr test UnSubscribeResource 001
172 * @tc.desc: Verify if can stop success.
173 * @tc.type: FUNC
174 * @tc.require: issueI798UT
175 * @tc.author:xukuan
176 */
177 HWTEST_F(PluginMgrTest, UnSubscribeResource001, TestSize.Level1)
178 {
179 pluginMgr_->UnSubscribeResource(LIB_NAME, ResType::RES_TYPE_SCREEN_STATUS);
180 auto iter = pluginMgr_->resTypeLibMap_.find(ResType::RES_TYPE_SCREEN_STATUS);
181 EXPECT_TRUE(iter == pluginMgr_->resTypeLibMap_.end());
182 }
183
184 /*
185 * @tc.name: SocPerfSubTest_DispatchResource_001
186 * @tc.desc: DispatchResource Plugin
187 * @tc.type FUNC
188 * @tc.author:zoujie
189 * @tc.require: issueI5VWUI
190 */
191 HWTEST_F(PluginMgrTest, PluginMgrTest_DispatchResource_001, TestSize.Level1)
192 {
193 nlohmann::json payload;
194 auto data = std::make_shared<ResData>(ResType::RES_TYPE_APP_ABILITY_START,
195 ResType::AppStartType::APP_COLD_START, payload);
196 /* Init */
197 SocPerfPlugin::GetInstance().Init();
198 /* HandleAppAbilityStart */
199 SocPerfPlugin::GetInstance().socperfOnDemandSwitch_ = false;
200 SocPerfPlugin::GetInstance().DispatchResource(data);
201 SocPerfPlugin::GetInstance().socperfOnDemandSwitch_ = true;
202 SocPerfPlugin::GetInstance().DispatchResource(data);
203
204 data->value = ResType::AppStartType::APP_WARM_START;
205 SocPerfPlugin::GetInstance().DispatchResource(data);
206
207 /* HandleWindowFocus */
208 data->resType = ResType::RES_TYPE_WINDOW_FOCUS;
209 data->value = ResType::WindowFocusStatus::WINDOW_FOCUS;
210 SocPerfPlugin::GetInstance().DispatchResource(data);
211
212 /* HandleEventClick */
213 data->resType = ResType::RES_TYPE_CLICK_RECOGNIZE;
214 data->value = ResType::ClickEventType::TOUCH_EVENT;
215 SocPerfPlugin::GetInstance().DispatchResource(data);
216 data->value = ResType::ClickEventType::CLICK_EVENT;
217 SocPerfPlugin::GetInstance().DispatchResource(data);
218
219 /* HandlePushPage */
220 data->resType = ResType::RES_TYPE_PUSH_PAGE;
221 data->value = ResType::PushPageType::PUSH_PAGE_START;
222 SocPerfPlugin::GetInstance().DispatchResource(data);
223 data->value = ResType::PushPageType::PUSH_PAGE_COMPLETE;
224 SocPerfPlugin::GetInstance().DispatchResource(data);
225
226 /* HandlePopPage */
227 data->resType = ResType::RES_TYPE_POP_PAGE;
228 data->value = 0;
229 SocPerfPlugin::GetInstance().DispatchResource(data);
230
231 /* HandleEventSlide */
232 data->resType = ResType::RES_TYPE_SLIDE_RECOGNIZE;
233 data->value = ResType::SlideEventStatus::SLIDE_EVENT_ON;
234 SocPerfPlugin::GetInstance().DispatchResource(data);
235 data->value = ResType::SlideEventStatus::SLIDE_EVENT_OFF;
236 SocPerfPlugin::GetInstance().DispatchResource(data);
237
238 /* HandleEventWebGesture */
239 data->resType = ResType::RES_TYPE_WEB_GESTURE;
240 data->value = 0;
241 SocPerfPlugin::GetInstance().DispatchResource(data);
242
243 /* HandleResizeWindow */
244 data->resType = ResType::RES_TYPE_RESIZE_WINDOW;
245 data->value = ResType::WindowResizeType::WINDOW_RESIZING;
246 SocPerfPlugin::GetInstance().DispatchResource(data);
247 data->value = ResType::WindowResizeType::WINDOW_RESIZE_STOP;
248 SocPerfPlugin::GetInstance().DispatchResource(data);
249
250 /* HandleMoveWindow */
251 data->resType = ResType::RES_TYPE_MOVE_WINDOW;
252 data->value = ResType::WindowMoveType::WINDOW_MOVING;
253 SocPerfPlugin::GetInstance().DispatchResource(data);
254 data->value = ResType::WindowMoveType::WINDOW_MOVE_STOP;
255 SocPerfPlugin::GetInstance().DispatchResource(data);
256
257 /* DeInit */
258 SocPerfPlugin::GetInstance().Disable();
259 }
260
261 /*
262 * @tc.name: SocPerfSubTest_DispatchResource_002
263 * @tc.desc: DispatchResource Plugin
264 * @tc.type FUNC
265 * @tc.author:qiunaiguang
266 * @tc.require: issueI6I9QS
267 */
268 HWTEST_F(PluginMgrTest, PluginMgrTest_DispatchResource_002, Function | MediumTest | Level0)
269 {
270 /* Init */
271 SocPerfPlugin::GetInstance().Init();
272
273 SocPerfPlugin::GetInstance().socperfOnDemandSwitch_ = false;
274 nlohmann::json payload;
275 std::shared_ptr<ResData> resData =
276 std::make_shared<ResData>(ResType::RES_TYPE_LOAD_PAGE, ResType::LoadPageType::LOAD_PAGE_START, payload);
277 SocPerfPlugin::GetInstance().HandleLoadPage(resData);
278
279 SocPerfPlugin::GetInstance().socperfOnDemandSwitch_ = true;
280 SocPerfPlugin::GetInstance().HandleLoadPage(resData);
281
282 resData->value = ResType::LoadPageType::LOAD_PAGE_COMPLETE;
283 SocPerfPlugin::GetInstance().HandleLoadPage(resData);
284 /* DeInit */
285 SocPerfPlugin::GetInstance().Disable();
286 }
287
288 /*
289 * @tc.name: SocPerfSubTest_DispatchResource_003
290 * @tc.desc: DispatchResource Plugin
291 * @tc.type FUNC
292 * @tc.author:qiunaiguang
293 * @tc.require: issueI6I9QS
294 */
295 HWTEST_F(PluginMgrTest, PluginMgrTest_DispatchResource_003, Function | MediumTest | Level0)
296 {
297 /* Init */
298 SocPerfPlugin::GetInstance().Init();
299 nlohmann::json payload;
300 std::shared_ptr<ResData> resData =
301 std::make_shared<ResData>(ResType::RES_TYPE_SHOW_REMOTE_ANIMATION,
302 ResType::ShowRemoteAnimationStatus::ANIMATION_BEGIN, payload);
303
304 SocPerfPlugin::GetInstance().socperfOnDemandSwitch_ = false;
305 SocPerfPlugin::GetInstance().HandleRemoteAnimation(resData);
306
307 SocPerfPlugin::GetInstance().socperfOnDemandSwitch_ = true;
308 SocPerfPlugin::GetInstance().HandleRemoteAnimation(resData);
309
310 resData->value = ResType::ShowRemoteAnimationStatus::ANIMATION_END;
311 SocPerfPlugin::GetInstance().HandleRemoteAnimation(resData);
312 /* DeInit */
313 SocPerfPlugin::GetInstance().Disable();
314 }
315
316 /*
317 * @tc.name: SocPerfSubTest_DispatchResource_004
318 * @tc.desc: DispatchResource Plugin
319 * @tc.type FUNC
320 * @tc.author:qiunaiguang
321 * @tc.require: issueI6I9QS
322 */
323 HWTEST_F(PluginMgrTest, PluginMgrTest_DispatchResource_004, Function | MediumTest | Level0)
324 {
325 /* Init */
326 SocPerfPlugin::GetInstance().Init();
327 SocPerfPlugin::GetInstance().InitFeatureSwitch("socperf_on_demand");
328 SocPerfPlugin::GetInstance().InitFeatureSwitch("test");
329 /* DeInit */
330 SocPerfPlugin::GetInstance().Disable();
331 }
332 } // namespace ResourceSchedule
333 } // namespace OHOS
334