• 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 ResourceSchedule {
29 static constexpr int32_t APP_STATE_EXIT = 4;
30 
31 class TestNotifierSystemloadListener : public ResSchedSystemloadNotifierStub {
32 public:
33     TestNotifierSystemloadListener() = default;
34 
OnSystemloadLevel(int32_t level)35     void OnSystemloadLevel(int32_t level)
36     {
37         testSystemloadLevel = level;
38     }
39 
40     static int32_t testSystemloadLevel;
41 };
42 
43 int32_t TestNotifierSystemloadListener::testSystemloadLevel = 0;
44 
SetUpTestCase()45 void NotifierMgrTest::SetUpTestCase()
46 {
47     NotifierMgr::GetInstance().Init();
48 }
49 
TearDownTestCase()50 void NotifierMgrTest::TearDownTestCase() {}
51 
SetUp()52 void NotifierMgrTest::SetUp() {}
53 
TearDown()54 void NotifierMgrTest::TearDown()
55 {
56     NotifierMgr::GetInstance().UnRegisterNotifier(IPCSkeleton::GetCallingPid());
57     NotifierMgr::GetInstance().OnApplicationStateChange(APP_STATE_EXIT, IPCSkeleton::GetCallingPid());
58     TestNotifierSystemloadListener::testSystemloadLevel = 0;
59 }
60 
61 /**
62  * @tc.name: notifier manager TestNotifierSystemloadListener 001
63  * @tc.desc: test the interface TestNotifierSystemloadListener
64  * @tc.type: FUNC
65  * @tc.require: issueI97M6C
66  * @tc.author:shanhaiyang
67  */
68 HWTEST_F(NotifierMgrTest, TestNotifierSystemloadListener001, Function | MediumTest | Level0)
69 {
70     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
71     EXPECT_TRUE(notifier != nullptr);
72     NotifierMgr::GetInstance().RegisterNotifier(IPCSkeleton::GetCallingPid(), notifier);
73     NotifierMgr::GetInstance().OnApplicationStateChange(2, IPCSkeleton::GetCallingPid());
74     NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 2);
75     sleep(1);
76     EXPECT_TRUE(TestNotifierSystemloadListener::testSystemloadLevel == 2);
77 }
78 
79 /**
80  * @tc.name: notifier manager TestNotifierSystemloadListener 002
81  * @tc.desc: test the interface TestNotifierSystemloadListener
82  * @tc.type: FUNC
83  * @tc.require: issueI97M6C
84  * @tc.author:shanhaiyang
85  */
86 HWTEST_F(NotifierMgrTest, TestNotifierSystemloadListener002, Function | MediumTest | Level0)
87 {
88     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
89     EXPECT_TRUE(notifier != nullptr);
90     NotifierMgr::GetInstance().RegisterNotifier(IPCSkeleton::GetCallingPid(), notifier);
91     NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 2);
92     sleep(1);
93     EXPECT_TRUE(TestNotifierSystemloadListener::testSystemloadLevel == 2);
94 }
95 
96 /**
97  * @tc.name: notifier manager TestNotifierSystemloadListener 003
98  * @tc.desc: test the interface RegisterNotifier
99  * @tc.type: FUNC
100  * @tc.require: issueI97M6C
101  * @tc.author:shanhaiyang
102  */
103 HWTEST_F(NotifierMgrTest, TestNotifierSystemloadListener003, Function | MediumTest | Level0)
104 {
105     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
106     EXPECT_TRUE(notifier != nullptr);
107     NotifierMgr::GetInstance().RegisterNotifier(IPCSkeleton::GetCallingPid(), notifier);
108     NotifierMgr::GetInstance().OnApplicationStateChange(2, IPCSkeleton::GetCallingPid());
109     NotifierMgr::GetInstance().OnDeviceLevelChanged(1, 2);
110     sleep(1);
111     EXPECT_TRUE(TestNotifierSystemloadListener::testSystemloadLevel == 0);
112 }
113 
114 /**
115  * @tc.name: notifier manager TestNotifierSystemloadListener 004
116  * @tc.desc: test the interface TestNotifierSystemloadListener
117  * @tc.type: FUNC
118  * @tc.require: issueI97M6C
119  * @tc.author:shanhaiyang
120  */
121 HWTEST_F(NotifierMgrTest, TestNotifierSystemloadListener004, Function | MediumTest | Level0)
122 {
123     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
124     EXPECT_TRUE(notifier != nullptr);
125     NotifierMgr::GetInstance().RegisterNotifier(IPCSkeleton::GetCallingPid(), notifier);
126     NotifierMgr::GetInstance().OnApplicationStateChange(2, 111111);
127     NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 2);
128     sleep(1);
129     EXPECT_TRUE(TestNotifierSystemloadListener::testSystemloadLevel == 2);
130     NotifierMgr::GetInstance().OnApplicationStateChange(APP_STATE_EXIT, 111111);
131 }
132 
133 /**
134  * @tc.name: notifier manager TestNotifierSystemloadListener 005
135  * @tc.desc: test the interface TestNotifierSystemloadListener
136  * @tc.type: FUNC
137  * @tc.require: issueI97M6C
138  * @tc.author:shanhaiyang
139  */
140 HWTEST_F(NotifierMgrTest, TestNotifierSystemloadListener005, Function | MediumTest | Level0)
141 {
142     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
143     EXPECT_TRUE(notifier != nullptr);
144     NotifierMgr::GetInstance().RegisterNotifier(IPCSkeleton::GetCallingPid(), notifier);
145     NotifierMgr::GetInstance().OnApplicationStateChange(2, IPCSkeleton::GetCallingPid());
146     NotifierMgr::GetInstance().OnRemoteNotifierDied(notifier);
147     NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 2);
148     sleep(1);
149     EXPECT_TRUE(TestNotifierSystemloadListener::testSystemloadLevel == 0);
150 }
151 
152 /**
153  * @tc.name: notifier manager TestNotifierSystemloadListener 006
154  * @tc.desc: test the interface TestNotifierSystemloadListener
155  * @tc.type: FUNC
156  * @tc.require: issueI9G149
157  * @tc.author:shanhaiyang
158  */
159 HWTEST_F(NotifierMgrTest, TestNotifierSystemloadListener006, Function | MediumTest | Level0)
160 {
161     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
162     EXPECT_TRUE(notifier != nullptr);
163     auto callingPid = IPCSkeleton::GetCallingPid();
164     NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier);
165     auto& info = NotifierMgr::GetInstance().notifierMap_[callingPid];
166     info.hapApp = true;
167     NotifierMgr::GetInstance().OnApplicationStateChange(APP_STATE_EXIT, callingPid);
168     NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 2);
169     NotifierMgr::GetInstance().OnApplicationStateChange(2, callingPid);
170     sleep(1);
171     EXPECT_TRUE(TestNotifierSystemloadListener::testSystemloadLevel == 2);
172 }
173 
174 /**
175  * @tc.name: notifier manager RegisterNotifier 001
176  * @tc.desc: test the interface RegisterNotifier
177  * @tc.type: FUNC
178  * @tc.require: issueI97M6C
179  * @tc.author:shanhaiyang
180  */
181 HWTEST_F(NotifierMgrTest, RegisterNotifier001, Function | MediumTest | Level0)
182 {
183     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
184     EXPECT_TRUE(notifier != nullptr);
185     sptr<IRemoteObject> notifier1 = new (std::nothrow) TestNotifierSystemloadListener();
186     EXPECT_TRUE(notifier != nullptr);
187     auto callingPid = IPCSkeleton::GetCallingPid();
188     NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier);
189     NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier1);
190     auto realNotifier = NotifierMgr::GetInstance().notifierMap_[callingPid].notifier;
191     EXPECT_TRUE(notifier.GetRefPtr() == realNotifier.GetRefPtr());
192 }
193 
194 /**
195  * @tc.name: notifier manager RegisterNotifier 002
196  * @tc.desc: test the interface RegisterNotifier
197  * @tc.type: FUNC
198  * @tc.require: issueI97M6C
199  * @tc.author:shanhaiyang
200  */
201 HWTEST_F(NotifierMgrTest, RegisterNotifier002, Function | MediumTest | Level0)
202 {
203     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
204     EXPECT_TRUE(notifier != nullptr);
205     auto callingPid = IPCSkeleton::GetCallingPid();
206     NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier);
207     NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier);
208     auto size = NotifierMgr::GetInstance().notifierMap_.size();
209     EXPECT_TRUE(size == 1);
210 }
211 
212 /**
213  * @tc.name: notifier manager RegisterNotifier 003
214  * @tc.desc: test the interface RegisterNotifier
215  * @tc.type: FUNC
216  * @tc.require: issueI97M6C
217  * @tc.author:luolu
218  */
219 HWTEST_F(NotifierMgrTest, RegisterNotifier003, Function | MediumTest | Level0)
220 {
221     sptr<IRemoteObject> notifier;
222     auto callingPid = IPCSkeleton::GetCallingPid();
223     NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier);
224 }
225 
226 /**
227  * @tc.name: notifier manager RegisterNotifier 004
228  * @tc.desc: test the interface RegisterNotifier
229  * @tc.type: FUNC
230  * @tc.require: issueI97M6C
231  * @tc.author:luolu
232  */
233 HWTEST_F(NotifierMgrTest, RegisterNotifier004, Function | MediumTest | Level0)
234 {
235     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
236     EXPECT_TRUE(notifier != nullptr);
237     auto callingPid = IPCSkeleton::GetCallingPid();
238     NotifierMgr::GetInstance().notifierDeathRecipient_ = nullptr;
239     NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier);
240 }
241 
242 /**
243  * @tc.name: notifier manager UnRegisterNotifier 001
244  * @tc.desc: test the interface UnRegisterNotifier
245  * @tc.type: FUNC
246  * @tc.require: issueI97M6C
247  * @tc.author:shanhaiyang
248  */
249 HWTEST_F(NotifierMgrTest, UnRegisterNotifier001, Function | MediumTest | Level0)
250 {
251     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
252     EXPECT_TRUE(notifier != nullptr);
253     NotifierMgr::GetInstance().Init();
254     auto callingPid = IPCSkeleton::GetCallingPid();
255     NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier);
256     NotifierMgr::GetInstance().UnRegisterNotifier(callingPid);
257     auto size = NotifierMgr::GetInstance().notifierMap_.size();
258     EXPECT_TRUE(size == 0);
259 }
260 
261 /**
262  * @tc.name: notifier manager UnRegisterNotifier 002
263  * @tc.desc: test the interface UnRegisterNotifier
264  * @tc.type: FUNC
265  * @tc.require: issueI97M6C
266  * @tc.author:shanhaiyang
267  */
268 HWTEST_F(NotifierMgrTest, UnRegisterNotifier002, Function | MediumTest | Level0)
269 {
270     NotifierMgr::GetInstance().UnRegisterNotifier(IPCSkeleton::GetCallingPid());
271     auto size = NotifierMgr::GetInstance().notifierMap_.size();
272     EXPECT_TRUE(size == 0);
273 }
274 
275 /**
276  * @tc.name: notifier manager GetSystemloadLevel 001
277  * @tc.desc: test the interface GetSystemloadLevel
278  * @tc.type: FUNC
279  * @tc.require: issueI97M6C
280  * @tc.author:shanhaiyang
281  */
282 HWTEST_F(NotifierMgrTest, GetSystemloadLevel001, Function | MediumTest | Level0)
283 {
284     NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 0);
285     int32_t res = NotifierMgr::GetInstance().GetSystemloadLevel();
286     EXPECT_TRUE(res == 0);
287 }
288 
289 /**
290  * @tc.name: notifier manager GetSystemloadLevel 002
291  * @tc.desc: test the interface GetSystemloadLevel
292  * @tc.type: FUNC
293  * @tc.require: issueI97M6C
294  * @tc.author:shanhaiyang
295  */
296 HWTEST_F(NotifierMgrTest, GetSystemloadLevel002, Function | MediumTest | Level0)
297 {
298     NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 2);
299     NotifierMgr::GetInstance().OnDeviceLevelChanged(1, 5);
300     int32_t res = NotifierMgr::GetInstance().GetSystemloadLevel();
301     EXPECT_TRUE(res == 2);
302 }
303 
304 /**
305  * @tc.name: notifier manager dump 001
306  * @tc.desc: test the interface dump
307  * @tc.type: FUNC
308  * @tc.require: issueI97M6C
309  * @tc.author:shanhaiyang
310  */
311 HWTEST_F(NotifierMgrTest, Dump001, Function | MediumTest | Level0)
312 {
313     auto res = NotifierMgr::GetInstance().DumpRegisterInfo();
314     EXPECT_TRUE(res.size() == 0);
315 }
316 
317 /**
318  * @tc.name: notifier manager dump 002
319  * @tc.desc: test the interface dump
320  * @tc.type: FUNC
321  * @tc.require: issueI97M6C
322  * @tc.author:shanhaiyang
323  */
324 HWTEST_F(NotifierMgrTest, Dump002, Function | MediumTest | Level0)
325 {
326     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
327     EXPECT_TRUE(notifier != nullptr);
328     NotifierMgr::GetInstance().initialized_ = false;
329     NotifierMgr::GetInstance().Init();
330     NotifierMgr::GetInstance().RegisterNotifier(IPCSkeleton::GetCallingPid(), notifier);
331     auto res = NotifierMgr::GetInstance().DumpRegisterInfo();
332     EXPECT_TRUE(res.size() == 1);
333     NotifierMgr::GetInstance().UnRegisterNotifier(IPCSkeleton::GetCallingPid());
334     res = NotifierMgr::GetInstance().DumpRegisterInfo();
335     EXPECT_TRUE(res.size() == 0);
336 }
337 
338 /**
339  * @tc.name: notifier manager deinit 001
340  * @tc.desc: test the interface Deinit
341  * @tc.type: FUNC
342  * @tc.require: issueI97M6C
343  * @tc.author:luolu
344  */
345 HWTEST_F(NotifierMgrTest, Deinit001, Function | MediumTest | Level0)
346 {
347     NotifierMgr::GetInstance().Init();
348     NotifierMgr::GetInstance().Deinit();
349 }
350 
351 /**
352  * @tc.name: notifier manager OnRemoteNotifierDied 001
353  * @tc.desc: test the interface OnRemoteNotifierDied
354  * @tc.type: FUNC
355  * @tc.require: issueI97M6C
356  * @tc.author:luolu
357  */
358 HWTEST_F(NotifierMgrTest, OnRemoteNotifierDied001, Function | MediumTest | Level0)
359 {
360     sptr<IRemoteObject> notifier;
361     NotifierMgr::GetInstance().OnRemoteNotifierDied(notifier);
362 }
363 
364 /**
365  * @tc.name: notifier manager OnRemoteNotifierDied 002
366  * @tc.desc: test the interface OnRemoteNotifierDied
367  * @tc.type: FUNC
368  * @tc.require: issueI97M6C
369  * @tc.author:luolu
370  */
371 HWTEST_F(NotifierMgrTest, OnRemoteNotifierDied002, Function | MediumTest | Level0)
372 {
373     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
374     EXPECT_TRUE(notifier != nullptr);
375     NotifierMgr::GetInstance().OnRemoteNotifierDied(notifier);
376 }
377 
378 /**
379  * @tc.name: notifier manager OnDeviceLevelChanged 001
380  * @tc.desc: test the interface OnDeviceLevelChanged
381  * @tc.type: FUNC
382  * @tc.require: issueI97M6C
383  * @tc.author:luolu
384  */
385 HWTEST_F(NotifierMgrTest, OnDeviceLevelChanged001, Function | MediumTest | Level0)
386 {
387     sptr<IRemoteObject> notifier = nullptr;
388     NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 2);
389 }
390 
391 /**
392  * @tc.name: notifier manager OnApplicationStateChange 001
393  * @tc.desc: test the interface OnApplicationStateChange
394  * @tc.type: FUNC
395  * @tc.require: issueI97M6C
396  * @tc.author:luolu
397  */
398 HWTEST_F(NotifierMgrTest, OnApplicationStateChange001, Function | MediumTest | Level0)
399 {
400     NotifierMgr::GetInstance().OnApplicationStateChange(2, IPCSkeleton::GetCallingPid());
401 }
402 
403 /**
404  * @tc.name: notifier manager RemoveNotifierLock 001
405  * @tc.desc: test the interface RemoveNotifierLock
406  * @tc.type: FUNC
407  * @tc.require: issueI97M6C
408  * @tc.author:luolu
409  */
410 HWTEST_F(NotifierMgrTest, RemoveNotifierLock001, Function | MediumTest | Level0)
411 {
412     sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
413     EXPECT_TRUE(notifier != nullptr);
414     NotifierMgr::GetInstance().RemoveNotifierLock(notifier);
415 }
416 } // namespace ResourceSchedule
417 } // namespace OHOS
418