• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "notifier_mgr_test.h"
17 
18 #include "ipc_skeleton.h"
19 #include "notifier_mgr.h"
20 #include "res_sched_common_death_recipient.h"
21 #include "res_sched_systemload_notifier_proxy.h"
22 #include "res_sched_systemload_notifier_stub.h"
23 
24 using namespace std;
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 namespace AppExecFwk {
GetBundleNameByPid(const int pid,std::string & bundleName,int32_t & uid)29     int32_t AppMgrClient::GetBundleNameByPid(const int pid, std::string &bundleName, int32_t &uid)
30     {
31         if (pid == 1) {
32             bundleName = "";
33         } else {
34             bundleName ="com.example.myapp";
35         }
36         return ERR_OK;
37     }
38 }
39 namespace ResourceSchedule {
40 static constexpr int32_t APP_STATE_EXIT = 4;
41 
42 class TestNotifierSystemloadListener : public ResSchedSystemloadNotifierStub {
43 public:
44     TestNotifierSystemloadListener() = default;
45 
OnSystemloadLevel(int32_t level)46     ErrCode OnSystemloadLevel(int32_t level)
47     {
48         testSystemloadLevel = level;
49         return ERR_OK;
50     }
51 
52     static int32_t testSystemloadLevel;
53 };
54 
55 int32_t TestNotifierSystemloadListener::testSystemloadLevel = 0;
56 
SetUpTestCase()57 void NotifierMgrTest::SetUpTestCase()
58 {
59     NotifierMgr::GetInstance().Init();
60 }
61 
TearDownTestCase()62 void NotifierMgrTest::TearDownTestCase() {}
63 
SetUp()64 void NotifierMgrTest::SetUp() {}
65 
TearDown()66 void NotifierMgrTest::TearDown()
67 {
68     NotifierMgr::GetInstance().UnRegisterNotifier(IPCSkeleton::GetCallingPid());
69     NotifierMgr::GetInstance().OnApplicationStateChange(APP_STATE_EXIT, IPCSkeleton::GetCallingPid());
70     TestNotifierSystemloadListener::testSystemloadLevel = 0;
71 }
72 
73 /**
74  * @tc.name: notifier manager TestNotifierSystemloadListener 001
75  * @tc.desc: test the interface TestNotifierSystemloadListener
76  * @tc.type: FUNC
77  * @tc.require: issueI97M6C
78  * @tc.author:shanhaiyang
79  */
80 HWTEST_F(NotifierMgrTest, TestNotifierSystemloadListener001, Function | MediumTest | Level0)
81 {
82     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
83     EXPECT_TRUE(notifier != nullptr);
84     NotifierMgr::GetInstance().RegisterNotifier(IPCSkeleton::GetCallingPid(), notifier);
85     NotifierMgr::GetInstance().OnApplicationStateChange(2, IPCSkeleton::GetCallingPid());
86     NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 2);
87     sleep(1);
88     EXPECT_TRUE(TestNotifierSystemloadListener::testSystemloadLevel == 2);
89 }
90 
91 /**
92  * @tc.name: notifier manager TestNotifierSystemloadListener 002
93  * @tc.desc: test the interface TestNotifierSystemloadListener
94  * @tc.type: FUNC
95  * @tc.require: issueI97M6C
96  * @tc.author:shanhaiyang
97  */
98 HWTEST_F(NotifierMgrTest, TestNotifierSystemloadListener002, Function | MediumTest | Level0)
99 {
100     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
101     EXPECT_TRUE(notifier != nullptr);
102     NotifierMgr::GetInstance().RegisterNotifier(IPCSkeleton::GetCallingPid(), notifier);
103     NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 2);
104     sleep(1);
105     EXPECT_TRUE(TestNotifierSystemloadListener::testSystemloadLevel == 2);
106 }
107 
108 /**
109  * @tc.name: notifier manager TestNotifierSystemloadListener 003
110  * @tc.desc: test the interface RegisterNotifier
111  * @tc.type: FUNC
112  * @tc.require: issueI97M6C
113  * @tc.author:shanhaiyang
114  */
115 HWTEST_F(NotifierMgrTest, TestNotifierSystemloadListener003, Function | MediumTest | Level0)
116 {
117     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
118     EXPECT_TRUE(notifier != nullptr);
119     NotifierMgr::GetInstance().RegisterNotifier(IPCSkeleton::GetCallingPid(), notifier);
120     NotifierMgr::GetInstance().OnApplicationStateChange(2, IPCSkeleton::GetCallingPid());
121     NotifierMgr::GetInstance().OnDeviceLevelChanged(1, 2);
122     sleep(1);
123     EXPECT_TRUE(TestNotifierSystemloadListener::testSystemloadLevel == 0);
124 }
125 
126 /**
127  * @tc.name: notifier manager TestNotifierSystemloadListener 004
128  * @tc.desc: test the interface TestNotifierSystemloadListener
129  * @tc.type: FUNC
130  * @tc.require: issueI97M6C
131  * @tc.author:shanhaiyang
132  */
133 HWTEST_F(NotifierMgrTest, TestNotifierSystemloadListener004, Function | MediumTest | Level0)
134 {
135     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
136     EXPECT_TRUE(notifier != nullptr);
137     NotifierMgr::GetInstance().RegisterNotifier(IPCSkeleton::GetCallingPid(), notifier);
138     NotifierMgr::GetInstance().OnApplicationStateChange(2, 111111);
139     NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 2);
140     sleep(1);
141     EXPECT_TRUE(TestNotifierSystemloadListener::testSystemloadLevel == 2);
142     NotifierMgr::GetInstance().OnApplicationStateChange(APP_STATE_EXIT, 111111);
143 }
144 
145 /**
146  * @tc.name: notifier manager TestNotifierSystemloadListener 005
147  * @tc.desc: test the interface TestNotifierSystemloadListener
148  * @tc.type: FUNC
149  * @tc.require: issueI97M6C
150  * @tc.author:shanhaiyang
151  */
152 HWTEST_F(NotifierMgrTest, TestNotifierSystemloadListener005, Function | MediumTest | Level0)
153 {
154     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
155     EXPECT_TRUE(notifier != nullptr);
156     NotifierMgr::GetInstance().RegisterNotifier(IPCSkeleton::GetCallingPid(), notifier);
157     NotifierMgr::GetInstance().OnApplicationStateChange(2, IPCSkeleton::GetCallingPid());
158     NotifierMgr::GetInstance().OnRemoteNotifierDied(notifier);
159     NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 2);
160     sleep(1);
161     EXPECT_TRUE(TestNotifierSystemloadListener::testSystemloadLevel == 0);
162 }
163 
164 /**
165  * @tc.name: notifier manager TestNotifierSystemloadListener 006
166  * @tc.desc: test the interface TestNotifierSystemloadListener
167  * @tc.type: FUNC
168  * @tc.require: issueI9G149
169  * @tc.author:shanhaiyang
170  */
171 HWTEST_F(NotifierMgrTest, TestNotifierSystemloadListener006, Function | MediumTest | Level0)
172 {
173     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
174     EXPECT_TRUE(notifier != nullptr);
175     auto callingPid = IPCSkeleton::GetCallingPid();
176     NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier);
177     auto& info = NotifierMgr::GetInstance().notifierMap_[callingPid];
178     info.hapApp = true;
179     NotifierMgr::GetInstance().OnApplicationStateChange(APP_STATE_EXIT, callingPid);
180     NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 2);
181     NotifierMgr::GetInstance().OnApplicationStateChange(2, callingPid);
182     sleep(1);
183     EXPECT_TRUE(TestNotifierSystemloadListener::testSystemloadLevel == 2);
184 }
185 
186 /**
187  * @tc.name: notifier manager RegisterNotifier 001
188  * @tc.desc: test the interface RegisterNotifier
189  * @tc.type: FUNC
190  * @tc.require: issueI97M6C
191  * @tc.author:shanhaiyang
192  */
193 HWTEST_F(NotifierMgrTest, RegisterNotifier001, Function | MediumTest | Level0)
194 {
195     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
196     EXPECT_TRUE(notifier != nullptr);
197     sptr<IRemoteObject> notifier1 = new (std::nothrow) TestNotifierSystemloadListener();
198     EXPECT_TRUE(notifier != nullptr);
199     auto callingPid = IPCSkeleton::GetCallingPid();
200     NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier);
201     NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier1);
202     auto realNotifier = NotifierMgr::GetInstance().notifierMap_[callingPid].notifier;
203     EXPECT_TRUE(notifier.GetRefPtr() == realNotifier.GetRefPtr());
204 }
205 
206 /**
207  * @tc.name: notifier manager RegisterNotifier 002
208  * @tc.desc: test the interface RegisterNotifier
209  * @tc.type: FUNC
210  * @tc.require: issueI97M6C
211  * @tc.author:shanhaiyang
212  */
213 HWTEST_F(NotifierMgrTest, RegisterNotifier002, Function | MediumTest | Level0)
214 {
215     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
216     EXPECT_TRUE(notifier != nullptr);
217     auto callingPid = IPCSkeleton::GetCallingPid();
218     NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier);
219     NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier);
220     auto size = NotifierMgr::GetInstance().notifierMap_.size();
221     EXPECT_TRUE(size == 1);
222 }
223 
224 /**
225  * @tc.name: notifier manager RegisterNotifier 003
226  * @tc.desc: test the interface RegisterNotifier
227  * @tc.type: FUNC
228  * @tc.require: issueI97M6C
229  * @tc.author:luolu
230  */
231 HWTEST_F(NotifierMgrTest, RegisterNotifier003, Function | MediumTest | Level0)
232 {
233     sptr<IRemoteObject> notifier;
234     auto callingPid = IPCSkeleton::GetCallingPid();
235     NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier);
236     EXPECT_EQ(NotifierMgr::GetInstance().notifierMap_.size(), 0);
237 }
238 
239 /**
240  * @tc.name: notifier manager RegisterNotifier 004
241  * @tc.desc: test the interface RegisterNotifier
242  * @tc.type: FUNC
243  * @tc.require: issueI97M6C
244  * @tc.author:luolu
245  */
246 HWTEST_F(NotifierMgrTest, RegisterNotifier004, Function | MediumTest | Level0)
247 {
248     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
249     EXPECT_TRUE(notifier != nullptr);
250     auto callingPid = IPCSkeleton::GetCallingPid();
251     NotifierMgr::GetInstance().notifierDeathRecipient_ = nullptr;
252     NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier);
253 }
254 
255 /**
256  * @tc.name: notifier manager RegisterNotifier 005
257  * @tc.desc: test the interface RegisterNotifier
258  * @tc.type: FUNC
259  * @tc.require: issueI97M6C
260  * @tc.author:zhuxiaofei
261  */
262 HWTEST_F(NotifierMgrTest, RegisterNotifier005, Function | MediumTest | Level0)
263 {
264     NotifierMgr::GetInstance().isTaskSubmit_ = false;
265     NotifierMgr::GetInstance().ReportPidToHisysevent(1);
266     EXPECT_TRUE(!NotifierMgr::GetInstance().isTaskSubmit_);
267 }
268 
269 /**
270  * @tc.name: notifier manager RegisterNotifier 006
271  * @tc.desc: test the interface RegisterNotifier
272  * @tc.type: FUNC
273  * @tc.require: issueI97M6C
274  * @tc.author:zhuxiaofei
275  */
276 HWTEST_F(NotifierMgrTest, RegisterNotifier006, Function | MediumTest | Level0)
277 {
278     NotifierMgr::GetInstance().isTaskSubmit_ = true;
279     NotifierMgr::GetInstance().hisyseventBundleNames_.clear();
280     for (int i = 1; i < 100; i++) {
281         NotifierMgr::GetInstance().hisyseventBundleNames_.insert(std::to_string(i));
282     }
283     NotifierMgr::GetInstance().ReportPidToHisysevent(100);
284     EXPECT_TRUE(NotifierMgr::GetInstance().hisyseventBundleNames_.size() == 0);
285 }
286 
287 /**
288  * @tc.name: notifier manager RegisterNotifier 007
289  * @tc.desc: test the interface RegisterNotifier
290  * @tc.type: FUNC
291  * @tc.require: issueI97M6C
292  * @tc.author:zhuxiaofei
293  */
294 HWTEST_F(NotifierMgrTest, RegisterNotifier007, Function | MediumTest | Level0)
295 {
296     NotifierMgr::GetInstance().isTaskSubmit_ = false;
297     NotifierMgr::GetInstance().hisyseventBundleNames_.clear();
298     NotifierMgr::GetInstance().hisyseventBundleNames_.insert("1");
299     NotifierMgr::GetInstance().NotifierEventReportPeriod();
300     EXPECT_TRUE(NotifierMgr::GetInstance().hisyseventBundleNames_.size() == 0);
301 }
302 
303 /**
304  * @tc.name: notifier manager RegisterNotifier 008
305  * @tc.desc: test the interface RegisterNotifier
306  * @tc.type: FUNC
307  * @tc.require: issueI97M6C
308  * @tc.author:zhuxiaofei
309  */
310 HWTEST_F(NotifierMgrTest, RegisterNotifier008, Function | MediumTest | Level0)
311 {
312     NotifierMgr::GetInstance().isTaskSubmit_ = false;
313     NotifierMgr::GetInstance().hisyseventBundleNames_.clear();
314     NotifierMgr::GetInstance().ReportPidToHisysevent(100);
315     EXPECT_TRUE(NotifierMgr::GetInstance().isTaskSubmit_);
316 }
317 
318 /**
319  * @tc.name: notifier manager UnRegisterNotifier 001
320  * @tc.desc: test the interface UnRegisterNotifier
321  * @tc.type: FUNC
322  * @tc.require: issueI97M6C
323  * @tc.author:shanhaiyang
324  */
325 HWTEST_F(NotifierMgrTest, UnRegisterNotifier001, Function | MediumTest | Level0)
326 {
327     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
328     EXPECT_TRUE(notifier != nullptr);
329     NotifierMgr::GetInstance().Init();
330     auto callingPid = IPCSkeleton::GetCallingPid();
331     NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier);
332     NotifierMgr::GetInstance().UnRegisterNotifier(callingPid);
333     auto size = NotifierMgr::GetInstance().notifierMap_.size();
334     EXPECT_TRUE(size == 0);
335 }
336 
337 /**
338  * @tc.name: notifier manager UnRegisterNotifier 002
339  * @tc.desc: test the interface UnRegisterNotifier
340  * @tc.type: FUNC
341  * @tc.require: issueI97M6C
342  * @tc.author:shanhaiyang
343  */
344 HWTEST_F(NotifierMgrTest, UnRegisterNotifier002, Function | MediumTest | Level0)
345 {
346     NotifierMgr::GetInstance().UnRegisterNotifier(IPCSkeleton::GetCallingPid());
347     auto size = NotifierMgr::GetInstance().notifierMap_.size();
348     EXPECT_TRUE(size == 0);
349 }
350 
351 /**
352  * @tc.name: notifier manager GetSystemloadLevel 001
353  * @tc.desc: test the interface GetSystemloadLevel
354  * @tc.type: FUNC
355  * @tc.require: issueI97M6C
356  * @tc.author:shanhaiyang
357  */
358 HWTEST_F(NotifierMgrTest, GetSystemloadLevel001, Function | MediumTest | Level0)
359 {
360     NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 0);
361     int32_t res = NotifierMgr::GetInstance().GetSystemloadLevel();
362     EXPECT_TRUE(res == 0);
363 }
364 
365 /**
366  * @tc.name: notifier manager GetSystemloadLevel 002
367  * @tc.desc: test the interface GetSystemloadLevel
368  * @tc.type: FUNC
369  * @tc.require: issueI97M6C
370  * @tc.author:shanhaiyang
371  */
372 HWTEST_F(NotifierMgrTest, GetSystemloadLevel002, Function | MediumTest | Level0)
373 {
374     NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 2);
375     NotifierMgr::GetInstance().OnDeviceLevelChanged(1, 5);
376     int32_t res = NotifierMgr::GetInstance().GetSystemloadLevel();
377     EXPECT_TRUE(res == 2);
378 }
379 
380 /**
381  * @tc.name: notifier manager dump 001
382  * @tc.desc: test the interface dump
383  * @tc.type: FUNC
384  * @tc.require: issueI97M6C
385  * @tc.author:shanhaiyang
386  */
387 HWTEST_F(NotifierMgrTest, Dump001, Function | MediumTest | Level0)
388 {
389     auto res = NotifierMgr::GetInstance().DumpRegisterInfo();
390     EXPECT_TRUE(res.size() == 0);
391 }
392 
393 /**
394  * @tc.name: notifier manager dump 002
395  * @tc.desc: test the interface dump
396  * @tc.type: FUNC
397  * @tc.require: issueI97M6C
398  * @tc.author:shanhaiyang
399  */
400 HWTEST_F(NotifierMgrTest, Dump002, Function | MediumTest | Level0)
401 {
402     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
403     EXPECT_TRUE(notifier != nullptr);
404     NotifierMgr::GetInstance().initialized_ = false;
405     NotifierMgr::GetInstance().Init();
406     NotifierMgr::GetInstance().RegisterNotifier(IPCSkeleton::GetCallingPid(), notifier);
407     auto res = NotifierMgr::GetInstance().DumpRegisterInfo();
408     EXPECT_TRUE(res.size() == 1);
409     NotifierMgr::GetInstance().UnRegisterNotifier(IPCSkeleton::GetCallingPid());
410     res = NotifierMgr::GetInstance().DumpRegisterInfo();
411     EXPECT_TRUE(res.size() == 0);
412 }
413 
414 /**
415  * @tc.name: notifier manager deinit 001
416  * @tc.desc: test the interface Deinit
417  * @tc.type: FUNC
418  * @tc.require: issueI97M6C
419  * @tc.author:luolu
420  */
421 HWTEST_F(NotifierMgrTest, Deinit001, Function | MediumTest | Level0)
422 {
423     NotifierMgr::GetInstance().Init();
424     EXPECT_TRUE(NotifierMgr::GetInstance().initialized_);
425     NotifierMgr::GetInstance().Deinit();
426 }
427 
428 /**
429  * @tc.name: notifier manager OnRemoteNotifierDied 001
430  * @tc.desc: test the interface OnRemoteNotifierDied
431  * @tc.type: FUNC
432  * @tc.require: issueI97M6C
433  * @tc.author:luolu
434  */
435 HWTEST_F(NotifierMgrTest, OnRemoteNotifierDied001, Function | MediumTest | Level0)
436 {
437     sptr<IRemoteObject> notifier;
438     NotifierMgr::GetInstance().OnRemoteNotifierDied(notifier);
439     EXPECT_TRUE(NotifierMgr::GetInstance().notifierMap_.size() == 0);
440 }
441 
442 /**
443  * @tc.name: notifier manager OnRemoteNotifierDied 002
444  * @tc.desc: test the interface OnRemoteNotifierDied
445  * @tc.type: FUNC
446  * @tc.require: issueI97M6C
447  * @tc.author:luolu
448  */
449 HWTEST_F(NotifierMgrTest, OnRemoteNotifierDied002, Function | MediumTest | Level0)
450 {
451     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
452     EXPECT_TRUE(notifier != nullptr);
453     NotifierMgr::GetInstance().OnRemoteNotifierDied(notifier);
454 }
455 
456 /**
457  * @tc.name: notifier manager OnDeviceLevelChanged 001
458  * @tc.desc: test the interface OnDeviceLevelChanged
459  * @tc.type: FUNC
460  * @tc.require: issueI97M6C
461  * @tc.author:luolu
462  */
463 HWTEST_F(NotifierMgrTest, OnDeviceLevelChanged001, Function | MediumTest | Level0)
464 {
465     sptr<IRemoteObject> notifier = nullptr;
466     NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 2);
467     EXPECT_EQ(NotifierMgr::GetInstance().systemloadLevel_, 2);
468 }
469 
470 /**
471  * @tc.name: notifier manager OnApplicationStateChange 001
472  * @tc.desc: test the interface OnApplicationStateChange
473  * @tc.type: FUNC
474  * @tc.require: issueI97M6C
475  * @tc.author:luolu
476  */
477 HWTEST_F(NotifierMgrTest, OnApplicationStateChange001, Function | MediumTest | Level0)
478 {
479     NotifierMgr::GetInstance().Init();
480     NotifierMgr::GetInstance().OnApplicationStateChange(2, IPCSkeleton::GetCallingPid());
481     EXPECT_TRUE(NotifierMgr::GetInstance().initialized_);
482 }
483 
484 /**
485  * @tc.name: notifier manager RemoveNotifierLock 001
486  * @tc.desc: test the interface RemoveNotifierLock
487  * @tc.type: FUNC
488  * @tc.require: issueI97M6C
489  * @tc.author:luolu
490  */
491 HWTEST_F(NotifierMgrTest, RemoveNotifierLock001, Function | MediumTest | Level0)
492 {
493     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
494     EXPECT_TRUE(notifier != nullptr);
495     NotifierMgr::GetInstance().RemoveNotifierLock(notifier);
496 }
497 } // namespace ResourceSchedule
498 } // namespace OHOS
499