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 EXPECT_EQ(NotifierMgr::GetInstance().notifierMap_.size(), 0);
225 }
226
227 /**
228 * @tc.name: notifier manager RegisterNotifier 004
229 * @tc.desc: test the interface RegisterNotifier
230 * @tc.type: FUNC
231 * @tc.require: issueI97M6C
232 * @tc.author:luolu
233 */
234 HWTEST_F(NotifierMgrTest, RegisterNotifier004, Function | MediumTest | Level0)
235 {
236 sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
237 EXPECT_TRUE(notifier != nullptr);
238 auto callingPid = IPCSkeleton::GetCallingPid();
239 NotifierMgr::GetInstance().notifierDeathRecipient_ = nullptr;
240 NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier);
241 }
242
243 /**
244 * @tc.name: notifier manager UnRegisterNotifier 001
245 * @tc.desc: test the interface UnRegisterNotifier
246 * @tc.type: FUNC
247 * @tc.require: issueI97M6C
248 * @tc.author:shanhaiyang
249 */
250 HWTEST_F(NotifierMgrTest, UnRegisterNotifier001, Function | MediumTest | Level0)
251 {
252 sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
253 EXPECT_TRUE(notifier != nullptr);
254 NotifierMgr::GetInstance().Init();
255 auto callingPid = IPCSkeleton::GetCallingPid();
256 NotifierMgr::GetInstance().RegisterNotifier(callingPid, notifier);
257 NotifierMgr::GetInstance().UnRegisterNotifier(callingPid);
258 auto size = NotifierMgr::GetInstance().notifierMap_.size();
259 EXPECT_TRUE(size == 0);
260 }
261
262 /**
263 * @tc.name: notifier manager UnRegisterNotifier 002
264 * @tc.desc: test the interface UnRegisterNotifier
265 * @tc.type: FUNC
266 * @tc.require: issueI97M6C
267 * @tc.author:shanhaiyang
268 */
269 HWTEST_F(NotifierMgrTest, UnRegisterNotifier002, Function | MediumTest | Level0)
270 {
271 NotifierMgr::GetInstance().UnRegisterNotifier(IPCSkeleton::GetCallingPid());
272 auto size = NotifierMgr::GetInstance().notifierMap_.size();
273 EXPECT_TRUE(size == 0);
274 }
275
276 /**
277 * @tc.name: notifier manager GetSystemloadLevel 001
278 * @tc.desc: test the interface GetSystemloadLevel
279 * @tc.type: FUNC
280 * @tc.require: issueI97M6C
281 * @tc.author:shanhaiyang
282 */
283 HWTEST_F(NotifierMgrTest, GetSystemloadLevel001, Function | MediumTest | Level0)
284 {
285 NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 0);
286 int32_t res = NotifierMgr::GetInstance().GetSystemloadLevel();
287 EXPECT_TRUE(res == 0);
288 }
289
290 /**
291 * @tc.name: notifier manager GetSystemloadLevel 002
292 * @tc.desc: test the interface GetSystemloadLevel
293 * @tc.type: FUNC
294 * @tc.require: issueI97M6C
295 * @tc.author:shanhaiyang
296 */
297 HWTEST_F(NotifierMgrTest, GetSystemloadLevel002, Function | MediumTest | Level0)
298 {
299 NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 2);
300 NotifierMgr::GetInstance().OnDeviceLevelChanged(1, 5);
301 int32_t res = NotifierMgr::GetInstance().GetSystemloadLevel();
302 EXPECT_TRUE(res == 2);
303 }
304
305 /**
306 * @tc.name: notifier manager dump 001
307 * @tc.desc: test the interface dump
308 * @tc.type: FUNC
309 * @tc.require: issueI97M6C
310 * @tc.author:shanhaiyang
311 */
312 HWTEST_F(NotifierMgrTest, Dump001, Function | MediumTest | Level0)
313 {
314 auto res = NotifierMgr::GetInstance().DumpRegisterInfo();
315 EXPECT_TRUE(res.size() == 0);
316 }
317
318 /**
319 * @tc.name: notifier manager dump 002
320 * @tc.desc: test the interface dump
321 * @tc.type: FUNC
322 * @tc.require: issueI97M6C
323 * @tc.author:shanhaiyang
324 */
325 HWTEST_F(NotifierMgrTest, Dump002, Function | MediumTest | Level0)
326 {
327 sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
328 EXPECT_TRUE(notifier != nullptr);
329 NotifierMgr::GetInstance().initialized_ = false;
330 NotifierMgr::GetInstance().Init();
331 NotifierMgr::GetInstance().RegisterNotifier(IPCSkeleton::GetCallingPid(), notifier);
332 auto res = NotifierMgr::GetInstance().DumpRegisterInfo();
333 EXPECT_TRUE(res.size() == 1);
334 NotifierMgr::GetInstance().UnRegisterNotifier(IPCSkeleton::GetCallingPid());
335 res = NotifierMgr::GetInstance().DumpRegisterInfo();
336 EXPECT_TRUE(res.size() == 0);
337 }
338
339 /**
340 * @tc.name: notifier manager deinit 001
341 * @tc.desc: test the interface Deinit
342 * @tc.type: FUNC
343 * @tc.require: issueI97M6C
344 * @tc.author:luolu
345 */
346 HWTEST_F(NotifierMgrTest, Deinit001, Function | MediumTest | Level0)
347 {
348 NotifierMgr::GetInstance().Init();
349 EXPECT_TRUE(NotifierMgr::GetInstance().initialized_);
350 NotifierMgr::GetInstance().Deinit();
351 }
352
353 /**
354 * @tc.name: notifier manager OnRemoteNotifierDied 001
355 * @tc.desc: test the interface OnRemoteNotifierDied
356 * @tc.type: FUNC
357 * @tc.require: issueI97M6C
358 * @tc.author:luolu
359 */
360 HWTEST_F(NotifierMgrTest, OnRemoteNotifierDied001, Function | MediumTest | Level0)
361 {
362 sptr<IRemoteObject> notifier;
363 NotifierMgr::GetInstance().OnRemoteNotifierDied(notifier);
364 EXPECT_TRUE(NotifierMgr::GetInstance().notifierMap_.size() == 0);
365 }
366
367 /**
368 * @tc.name: notifier manager OnRemoteNotifierDied 002
369 * @tc.desc: test the interface OnRemoteNotifierDied
370 * @tc.type: FUNC
371 * @tc.require: issueI97M6C
372 * @tc.author:luolu
373 */
374 HWTEST_F(NotifierMgrTest, OnRemoteNotifierDied002, Function | MediumTest | Level0)
375 {
376 sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
377 EXPECT_TRUE(notifier != nullptr);
378 NotifierMgr::GetInstance().OnRemoteNotifierDied(notifier);
379 }
380
381 /**
382 * @tc.name: notifier manager OnDeviceLevelChanged 001
383 * @tc.desc: test the interface OnDeviceLevelChanged
384 * @tc.type: FUNC
385 * @tc.require: issueI97M6C
386 * @tc.author:luolu
387 */
388 HWTEST_F(NotifierMgrTest, OnDeviceLevelChanged001, Function | MediumTest | Level0)
389 {
390 sptr<IRemoteObject> notifier = nullptr;
391 NotifierMgr::GetInstance().OnDeviceLevelChanged(0, 2);
392 EXPECT_EQ(NotifierMgr::GetInstance().systemloadLevel_, 2);
393 }
394
395 /**
396 * @tc.name: notifier manager OnApplicationStateChange 001
397 * @tc.desc: test the interface OnApplicationStateChange
398 * @tc.type: FUNC
399 * @tc.require: issueI97M6C
400 * @tc.author:luolu
401 */
402 HWTEST_F(NotifierMgrTest, OnApplicationStateChange001, Function | MediumTest | Level0)
403 {
404 NotifierMgr::GetInstance().Init();
405 NotifierMgr::GetInstance().OnApplicationStateChange(2, IPCSkeleton::GetCallingPid());
406 EXPECT_TRUE(NotifierMgr::GetInstance().initialized_);
407 }
408
409 /**
410 * @tc.name: notifier manager RemoveNotifierLock 001
411 * @tc.desc: test the interface RemoveNotifierLock
412 * @tc.type: FUNC
413 * @tc.require: issueI97M6C
414 * @tc.author:luolu
415 */
416 HWTEST_F(NotifierMgrTest, RemoveNotifierLock001, Function | MediumTest | Level0)
417 {
418 sptr<IRemoteObject> notifier = new (std::nothrow) TestNotifierSystemloadListener();
419 EXPECT_TRUE(notifier != nullptr);
420 NotifierMgr::GetInstance().RemoveNotifierLock(notifier);
421 }
422 } // namespace ResourceSchedule
423 } // namespace OHOS
424