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