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 Init 002
72 * @tc.desc: Verify if can init fault.
73 * @tc.type: FUNC
74 * @tc.require: issueI8VZVN
75 * @tc.author:z30053169
76 */
77 HWTEST_F(PluginMgrTest, Init002, TestSize.Level1)
78 {
79 PluginMgr::GetInstance().pluginSwitch_ = nullptr;
80 pluginMgr_->Init();
81 SUCCEED();
82 }
83
84 /**
85 * @tc.name: Plugin mgr test Stop 001
86 * @tc.desc: Verify if can stop success.
87 * @tc.type: FUNC
88 * @tc.require: issueI798UT
89 * @tc.author:xukuan
90 */
91 HWTEST_F(PluginMgrTest, Stop001, TestSize.Level1)
92 {
93 pluginMgr_->Stop();
94 EXPECT_EQ(pluginMgr_->pluginLibMap_.size(), 0);
95 EXPECT_EQ(pluginMgr_->resTypeLibMap_.size(), 0);
96 EXPECT_TRUE(pluginMgr_->configReader_ == nullptr);
97 }
98
99 /**
100 * @tc.name: Plugin mgr test GetConfig 001
101 * @tc.desc: Verify if can get config with wrong env.
102 * @tc.type: FUNC
103 * @tc.require: issueI5WWV3
104 * @tc.author:lice
105 */
106 HWTEST_F(PluginMgrTest, GetConfig001, TestSize.Level1)
107 {
108 PluginConfig config = pluginMgr_->GetConfig("", "");
109 EXPECT_TRUE(config.itemList.empty());
110 }
111
112 /**
113 * @tc.name: Plugin mgr test GetConfig 002
114 * @tc.desc: Verify if can get config with wrong env.
115 * @tc.type: FUNC
116 * @tc.require: issueI8VZVN
117 * @tc.author:z30053169
118 */
119 HWTEST_F(PluginMgrTest, GetConfig002, TestSize.Level1)
120 {
121 PluginMgr::GetInstance().configReader_ = nullptr;
122 PluginConfig config = pluginMgr_->GetConfig("", "");
123 SUCCEED();
124 }
125
126 /**
127 * @tc.name: Plugin mgr test SubscribeResource 002
128 * @tc.desc: Verify if can SubscribeResource
129 * @tc.type: FUNC
130 * @tc.require: issueI8VZVN
131 * @tc.author:z30053169
132 */
133 HWTEST_F(PluginMgrTest, SubscribeResource002, TestSize.Level1)
134 {
135 PluginMgr::GetInstance().SubscribeResource("", 0);
136 SUCCEED();
137 PluginMgr::GetInstance().SubscribeResource("test", 1);
138 SUCCEED();
139 PluginMgr::GetInstance().UnSubscribeResource("test", 1);
140 SUCCEED();
141 }
142
143 /**
144 * @tc.name: Plugin mgr test UnSubscribeResource 003
145 * @tc.desc: Verify if can SubscribeResource
146 * @tc.type: FUNC
147 * @tc.require: issueI8VZVN
148 * @tc.author:z30053169
149 */
150 HWTEST_F(PluginMgrTest, UnSubscribeResource003, TestSize.Level1)
151 {
152 PluginMgr::GetInstance().UnSubscribeResource("", 0);
153 SUCCEED();
154 PluginMgr::GetInstance().UnSubscribeResource("test", 0);
155 SUCCEED();
156 PluginMgr::GetInstance().SubscribeResource("test1", 1);
157 PluginMgr::GetInstance().SubscribeResource("test2", 1);
158 PluginMgr::GetInstance().UnSubscribeResource("test1", 1);
159 SUCCEED();
160 PluginMgr::GetInstance().UnSubscribeResource("test2", 1);
161 SUCCEED();
162 }
163
164 /**
165 * @tc.name: Plugin mgr test DispatchResource 001
166 * @tc.desc: Verify if can DispatchResource
167 * @tc.type: FUNC
168 * @tc.require: issueI8VZVN
169 * @tc.author:z30053169
170 */
171 HWTEST_F(PluginMgrTest, DispatchResource001, TestSize.Level1)
172 {
173 pluginMgr_->Init();
174 nlohmann::json payload;
175 auto data = std::make_shared<ResData>(ResType::RES_TYPE_APP_ABILITY_START,
176 ResType::AppStartType::APP_COLD_START, payload);
177 pluginMgr_->DispatchResource(data);
178 pluginMgr_->DispatchResource(nullptr);
179 SUCCEED();
180 }
181
182 /**
183 * @tc.name: Plugin mgr test DispatchResource 002
184 * @tc.desc: Verify if can DispatchResource
185 * @tc.type: FUNC
186 * @tc.require: issueI8VZVN
187 * @tc.author:z30053169
188 */
189 HWTEST_F(PluginMgrTest, DispatchResource002, TestSize.Level1)
190 {
191 nlohmann::json payload;
192 auto data = std::make_shared<ResData>(ResType::RES_TYPE_APP_ABILITY_START,
193 ResType::AppStartType::APP_COLD_START, payload);
194 PluginMgr::GetInstance().SubscribeResource("", 0);
195 SUCCEED();
196 PluginMgr::GetInstance().SubscribeResource("test", ResType::RES_TYPE_APP_ABILITY_START);
197 SUCCEED();
198 PluginMgr::GetInstance().DispatchResource(data);
199 PluginMgr::GetInstance().UnSubscribeResource("", 0);
200 SUCCEED();
201 }
202
203 /**
204 * @tc.name: Plugin mgr test SubscribeResource 001
205 * @tc.desc: Verify if can stop success.
206 * @tc.type: FUNC
207 * @tc.require: issueI798UT
208 * @tc.author:xukuan
209 */
210 HWTEST_F(PluginMgrTest, SubscribeResource001, TestSize.Level1)
211 {
212 pluginMgr_->Init();
213 pluginMgr_->SubscribeResource(LIB_NAME, ResType::RES_TYPE_SCREEN_STATUS);
214 auto iter = pluginMgr_->resTypeLibMap_.find(ResType::RES_TYPE_SCREEN_STATUS);
215 string libName = iter->second.back();
216 EXPECT_EQ(libName.compare(LIB_NAME), 0);
217 }
218
219 /**
220 * @tc.name: Plugin mgr test Dump 001
221 * @tc.desc: Verify if dump commands is success.
222 * @tc.type: FUNC
223 * @tc.require: issueI5WWV3
224 * @tc.author:lice
225 */
226 HWTEST_F(PluginMgrTest, Dump001, TestSize.Level1)
227 {
228 std::string res;
229 pluginMgr_->Init();
230 pluginMgr_->DumpAllPlugin(res);
231 EXPECT_TRUE(!res.empty());
232 res = "";
233
234 pluginMgr_->LoadPlugin();
235 pluginMgr_->DumpHelpFromPlugin(res);
236 EXPECT_TRUE(res.empty());
237 res = "";
238
239 std::vector<std::string> args;
240 pluginMgr_->DumpOnePlugin(res, LIB_NAME, args);
241 EXPECT_TRUE(!res.empty());
242 res = "";
243
244 args.emplace_back("-h");
245 pluginMgr_->DumpOnePlugin(res, LIB_NAME, args);
246 EXPECT_TRUE(!res.empty());
247 }
248
249 /**
250 * @tc.name: Plugin mgr test RepairPlugin 001
251 * @tc.desc: Verify if RepairPlugin is success.
252 * @tc.type: FUNC
253 * @tc.require: issueI5WWV3
254 * @tc.author:lice
255 */
256 HWTEST_F(PluginMgrTest, RepairPlugin001, TestSize.Level1)
257 {
258 PluginLib libInfo = pluginMgr_->pluginLibMap_.find(LIB_NAME)->second;
259 pluginMgr_->RepairPlugin(Clock::now(), LIB_NAME, libInfo);
260 EXPECT_TRUE(true);
261 }
262
263 /**
264 * @tc.name: Plugin mgr test UnSubscribeResource 001
265 * @tc.desc: Verify if can stop success.
266 * @tc.type: FUNC
267 * @tc.require: issueI798UT
268 * @tc.author:xukuan
269 */
270 HWTEST_F(PluginMgrTest, UnSubscribeResource001, TestSize.Level1)
271 {
272 pluginMgr_->UnSubscribeResource(LIB_NAME, ResType::RES_TYPE_SCREEN_STATUS);
273 auto iter = pluginMgr_->resTypeLibMap_.find(ResType::RES_TYPE_SCREEN_STATUS);
274 EXPECT_TRUE(iter == pluginMgr_->resTypeLibMap_.end());
275 }
276
277 /*
278 * @tc.name: SocPerfSubTest_DispatchResource_001
279 * @tc.desc: DispatchResource Plugin
280 * @tc.type FUNC
281 * @tc.author:zoujie
282 * @tc.require: issueI5VWUI
283 */
284 HWTEST_F(PluginMgrTest, PluginMgrTest_DispatchResource_001, TestSize.Level1)
285 {
286 nlohmann::json payload;
287 auto data = std::make_shared<ResData>(ResType::RES_TYPE_APP_ABILITY_START,
288 ResType::AppStartType::APP_COLD_START, payload);
289 /* Init */
290 SocPerfPlugin::GetInstance().Init();
291 /* HandleAppAbilityStart */
292 SocPerfPlugin::GetInstance().DispatchResource(data);
293
294 data->value = ResType::AppStartType::APP_WARM_START;
295 SocPerfPlugin::GetInstance().DispatchResource(data);
296
297 /* HandleWindowFocus */
298 data->resType = ResType::RES_TYPE_WINDOW_FOCUS;
299 data->value = ResType::WindowFocusStatus::WINDOW_FOCUS;
300 SocPerfPlugin::GetInstance().DispatchResource(data);
301
302 /* HandleEventClick */
303 data->resType = ResType::RES_TYPE_CLICK_RECOGNIZE;
304 data->value = ResType::ClickEventType::TOUCH_EVENT;
305 SocPerfPlugin::GetInstance().DispatchResource(data);
306 data->value = ResType::ClickEventType::CLICK_EVENT;
307 SocPerfPlugin::GetInstance().DispatchResource(data);
308
309 /* HandlePushPage */
310 data->resType = ResType::RES_TYPE_PUSH_PAGE;
311 data->value = ResType::PushPageType::PUSH_PAGE_START;
312 SocPerfPlugin::GetInstance().DispatchResource(data);
313 data->value = ResType::PushPageType::PUSH_PAGE_COMPLETE;
314 SocPerfPlugin::GetInstance().DispatchResource(data);
315
316 /* HandlePopPage */
317 data->resType = ResType::RES_TYPE_POP_PAGE;
318 data->value = 0;
319 SocPerfPlugin::GetInstance().DispatchResource(data);
320
321 /* HandleEventSlide */
322 data->resType = ResType::RES_TYPE_SLIDE_RECOGNIZE;
323 data->value = ResType::SlideEventStatus::SLIDE_EVENT_ON;
324 SocPerfPlugin::GetInstance().DispatchResource(data);
325 data->value = ResType::SlideEventStatus::SLIDE_EVENT_OFF;
326 SocPerfPlugin::GetInstance().DispatchResource(data);
327
328 /* HandleEventWebGesture */
329 data->resType = ResType::RES_TYPE_WEB_GESTURE;
330 data->value = 0;
331 SocPerfPlugin::GetInstance().DispatchResource(data);
332
333 /* HandleResizeWindow */
334 data->resType = ResType::RES_TYPE_RESIZE_WINDOW;
335 data->value = ResType::WindowResizeType::WINDOW_RESIZING;
336 SocPerfPlugin::GetInstance().DispatchResource(data);
337 data->value = ResType::WindowResizeType::WINDOW_RESIZE_STOP;
338 SocPerfPlugin::GetInstance().DispatchResource(data);
339
340 /* HandleMoveWindow */
341 data->resType = ResType::RES_TYPE_MOVE_WINDOW;
342 data->value = ResType::WindowMoveType::WINDOW_MOVING;
343 SocPerfPlugin::GetInstance().DispatchResource(data);
344 data->value = ResType::WindowMoveType::WINDOW_MOVE_STOP;
345 SocPerfPlugin::GetInstance().DispatchResource(data);
346
347 /* DeInit */
348 SocPerfPlugin::GetInstance().Disable();
349 SUCCEED();
350 }
351
352 /*
353 * @tc.name: SocPerfSubTest_DispatchResource_002
354 * @tc.desc: DispatchResource Plugin
355 * @tc.type FUNC
356 * @tc.author:qiunaiguang
357 * @tc.require: issueI6I9QS
358 */
359 HWTEST_F(PluginMgrTest, PluginMgrTest_DispatchResource_002, Function | MediumTest | Level0)
360 {
361 /* Init */
362 SocPerfPlugin::GetInstance().Init();
363
364 nlohmann::json payload;
365 std::shared_ptr<ResData> resData =
366 std::make_shared<ResData>(ResType::RES_TYPE_LOAD_PAGE, ResType::LoadPageType::LOAD_PAGE_START, payload);
367 SocPerfPlugin::GetInstance().HandleLoadPage(resData);
368 resData->value = ResType::LoadPageType::LOAD_PAGE_COMPLETE;
369 SocPerfPlugin::GetInstance().HandleLoadPage(resData);
370 /* DeInit */
371 SocPerfPlugin::GetInstance().Disable();
372 SUCCEED();
373 }
374
375 /*
376 * @tc.name: SocPerfSubTest_DispatchResource_003
377 * @tc.desc: DispatchResource Plugin
378 * @tc.type FUNC
379 * @tc.author:qiunaiguang
380 * @tc.require: issueI6I9QS
381 */
382 HWTEST_F(PluginMgrTest, PluginMgrTest_DispatchResource_003, Function | MediumTest | Level0)
383 {
384 /* Init */
385 SocPerfPlugin::GetInstance().Init();
386 nlohmann::json payload;
387 std::shared_ptr<ResData> resData =
388 std::make_shared<ResData>(ResType::RES_TYPE_SHOW_REMOTE_ANIMATION,
389 ResType::ShowRemoteAnimationStatus::ANIMATION_BEGIN, payload);
390 SocPerfPlugin::GetInstance().HandleRemoteAnimation(resData);
391
392 resData->value = ResType::ShowRemoteAnimationStatus::ANIMATION_END;
393 SocPerfPlugin::GetInstance().HandleRemoteAnimation(resData);
394 /* DeInit */
395 SocPerfPlugin::GetInstance().Disable();
396 SUCCEED();
397 }
398
399 /*
400 * @tc.name: SocPerfSubTest_DispatchResource_004
401 * @tc.desc: DispatchResource Plugin
402 * @tc.type FUNC
403 * @tc.author:qiunaiguang
404 * @tc.require: issueI6I9QS
405 */
406 HWTEST_F(PluginMgrTest, PluginMgrTest_DispatchResource_004, Function | MediumTest | Level0)
407 {
408 /* Init */
409 SocPerfPlugin::GetInstance().Init();
410 SocPerfPlugin::GetInstance().InitFeatureSwitch("socperf_on_demand");
411 SocPerfPlugin::GetInstance().InitFeatureSwitch("test");
412 /* DeInit */
413 SocPerfPlugin::GetInstance().Disable();
414 SUCCEED();
415 }
416
417 /**
418 * @tc.name: Plugin mgr test DumPluginInfoAppend_001
419 * @tc.desc: test the interface DumpluginInfoAppend
420 * @tc.type: FUNC
421 * @tc.require: issueI8VZVN
422 * @tc.author:z30053169
423 */
424 HWTEST_F(PluginMgrTest, DumPluginInfoAppend_001, TestSize.Level1)
425 {
426 string result;
427 PluginInfo info;
428 info.switchOn = false;
429 PluginMgr::GetInstance().DumpPluginInfoAppend(result, info);
430 SUCCEED();
431 }
432
433 /**
434 * @tc.name: Plugin mgr test DispatchResource 003
435 * @tc.desc: test the interface DispatchResource
436 * @tc.type: FUNC
437 * @tc.require: issueI8VZVN
438 * @tc.author:z30053169
439 */
440 HWTEST_F(PluginMgrTest, DispatchResource003, TestSize.Level1)
441 {
442 nlohmann::json payload;
443 PluginMgr::GetInstance().dispatcher_ = nullptr;
444 auto data = std::make_shared<ResData>(ResType::RES_TYPE_APP_ABILITY_START,
445 ResType::AppStartType::APP_COLD_START, payload);
446 PluginMgr::GetInstance().UnSubscribeResource("test", ResType::RES_TYPE_APP_ABILITY_START);
447 PluginMgr::GetInstance().DispatchResource(data);
448 SUCCEED();
449 }
450
451 } // namespace ResourceSchedule
452 } // namespace OHOS
453