1 /*
2 * Copyright (c) 2023-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 "running_lock_mock_test.h"
17
18 #include "mock_lock_action.h"
19 #include "mock_power_action.h"
20 #include "mock_state_action.h"
21 #include "power_common.h"
22 #include "power_log.h"
23 #include "power_mgr_service.h"
24
25 using namespace testing::ext;
26 using namespace OHOS::PowerMgr;
27 using namespace OHOS;
28 using namespace std;
29 using ::testing::_;
30
31 namespace {
32 const std::string RUNNINGLOCK_BACKGROUND_NAME = "OHOS.RunningLock.Background";
33 constexpr int32_t RUNNINGLOCKPARAM_TIMEOUTMS_DEF = -1;
34 } // namespace
35 static sptr<PowerMgrService> g_powerService;
36 static MockStateAction* g_powerStateAction;
37 static MockStateAction* g_shutdownStateAction;
38 static MockPowerAction* g_powerAction;
39 static MockLockAction* g_lockAction;
40
ResetMockAction()41 static void ResetMockAction()
42 {
43 g_powerStateAction = new MockStateAction();
44 g_shutdownStateAction = new MockStateAction();
45 g_powerAction = new MockPowerAction();
46 g_lockAction = new MockLockAction();
47 g_powerService->EnableMock(g_powerStateAction, g_shutdownStateAction, g_powerAction, g_lockAction);
48 }
49
SetUpTestCase(void)50 void RunningLockMockTest::SetUpTestCase(void)
51 {
52 // create singleton service object at the beginning
53 g_powerService = DelayedSpSingleton<PowerMgrService>::GetInstance();
54 g_powerService->OnStart();
55 }
56
TearDownTestCase(void)57 void RunningLockMockTest::TearDownTestCase(void)
58 {
59 g_powerService->OnStop();
60 DelayedSpSingleton<PowerMgrService>::DestroyInstance();
61 }
62
SetUp(void)63 void RunningLockMockTest::SetUp(void)
64 {
65 ResetMockAction();
66 }
67
68 namespace {
69 /**
70 * @tc.name: RunningLockMockTest001
71 * @tc.desc: test proximity screen control runninglock by mock
72 * @tc.type: FUNC
73 * @tc.require: issueI6LPK9
74 */
75 HWTEST_F (RunningLockMockTest, RunningLockMockTest001, TestSize.Level2)
76 {
77 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest001 start.");
78 #ifdef HAS_SENSORS_SENSOR_PART
79 ASSERT_NE(g_powerService, nullptr);
80 ASSERT_NE(g_lockAction, nullptr);
81
82 RunningLockInfo runninglockInfo(
83 "RunningLockMockProximity1.1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
84 RunningLockInfo runninglockInfo2(
85 "RunningLockMockProximity1.2", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
86 int32_t timeoutMs = 100;
87 auto runningLockMgr = g_powerService->GetRunningLockMgr();
88 uint32_t lockActionCount = 0;
89 uint32_t unlockActionCount = 0;
90
__anon4b4103a20302(const RunningLockParam& param) 91 EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
92 EXPECT_EQ(param.name, RUNNINGLOCK_BACKGROUND_NAME);
93 EXPECT_EQ(param.type, RunningLockType::RUNNINGLOCK_BACKGROUND);
94 EXPECT_EQ(param.timeoutMs, RUNNINGLOCKPARAM_TIMEOUTMS_DEF);
95 lockActionCount++;
96 return RUNNINGLOCK_SUCCESS;
97 });
__anon4b4103a20402(const RunningLockParam& param) 98 EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
99 EXPECT_EQ(param.name, RUNNINGLOCK_BACKGROUND_NAME);
100 EXPECT_EQ(param.type, RunningLockType::RUNNINGLOCK_BACKGROUND);
101 unlockActionCount++;
102 return RUNNINGLOCK_SUCCESS;
103 });
104
105 sptr<IRemoteObject> runninglockToken = new RunningLockTokenStub();
106 sptr<IRemoteObject> runninglockToken2 = new RunningLockTokenStub();
107 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(runninglockToken, runninglockInfo));
108 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(runninglockToken2, runninglockInfo2));
109
110 g_powerService->Lock(runninglockToken);
111 EXPECT_EQ(1, runningLockMgr->GetValidRunningLockNum(runninglockInfo.type));
112 g_powerService->Lock(runninglockToken2);
113 EXPECT_EQ(2, runningLockMgr->GetValidRunningLockNum(runninglockInfo2.type));
114
115 g_powerService->UnLock(runninglockToken);
116 EXPECT_EQ(1, runningLockMgr->GetValidRunningLockNum(runninglockInfo.type));
117 g_powerService->UnLock(runninglockToken2);
118 EXPECT_EQ(0, runningLockMgr->GetValidRunningLockNum(runninglockInfo2.type));
119
120 g_powerService->ReleaseRunningLock(runninglockToken);
121 g_powerService->ReleaseRunningLock(runninglockToken2);
122
123 EXPECT_EQ(lockActionCount, 0);
124 EXPECT_EQ(unlockActionCount, 0);
125 #endif
126 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest001 end.");
127 }
128
129 /**
130 * @tc.name: RunningLockMockTest002
131 * @tc.desc: test proximity screen control runninglock release function by mock
132 * @tc.type: FUNC
133 * @tc.require: issueI6LPK9
134 */
135 HWTEST_F (RunningLockMockTest, RunningLockMockTest002, TestSize.Level2)
136 {
137 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest002 start.");
138 #ifdef HAS_SENSORS_SENSOR_PART
139 ASSERT_NE(g_powerService, nullptr);
140 ASSERT_NE(g_lockAction, nullptr);
141
142 RunningLockInfo runninglockInfo(
143 "RunningLockMockProximity2.1", RunningLockType::RUNNINGLOCK_PROXIMITY_SCREEN_CONTROL);
144 int32_t timeoutMs = 100;
145 auto runningLockMgr = g_powerService->GetRunningLockMgr();
146 uint32_t lockActionCount = 0;
147 uint32_t unlockActionCount = 0;
148
__anon4b4103a20502(const RunningLockParam& param) 149 EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
150 EXPECT_EQ(param.name, RUNNINGLOCK_BACKGROUND_NAME);
151 EXPECT_EQ(param.type, RunningLockType::RUNNINGLOCK_BACKGROUND);
152 EXPECT_EQ(param.timeoutMs, RUNNINGLOCKPARAM_TIMEOUTMS_DEF);
153 lockActionCount++;
154 return RUNNINGLOCK_SUCCESS;
155 });
__anon4b4103a20602(const RunningLockParam& param) 156 EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
157 EXPECT_EQ(param.name, RUNNINGLOCK_BACKGROUND_NAME);
158 EXPECT_EQ(param.type, RunningLockType::RUNNINGLOCK_BACKGROUND);
159 unlockActionCount++;
160 return RUNNINGLOCK_SUCCESS;
161 });
162
163 sptr<IRemoteObject> runninglockToken = new RunningLockTokenStub();
164 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(runninglockToken, runninglockInfo));
165
166 g_powerService->Lock(runninglockToken);
167 EXPECT_EQ(1, runningLockMgr->GetValidRunningLockNum(runninglockInfo.type));
168 g_powerService->UnLock(runninglockToken);
169 g_powerService->ReleaseRunningLock(runninglockToken);
170
171 EXPECT_EQ(lockActionCount, 0);
172 EXPECT_EQ(unlockActionCount, 0);
173 #endif
174 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest002 end.");
175 }
176
177 /**
178 * @tc.name: RunningLockMockTest003
179 * @tc.desc: test scene runninglock by mock
180 * @tc.type: FUNC
181 * @tc.require: issueI6LPK9
182 */
183 HWTEST_F (RunningLockMockTest, RunningLockMockTest003, TestSize.Level2)
184 {
185 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest003 start.");
186 ASSERT_NE(g_powerService, nullptr);
187 ASSERT_NE(g_lockAction, nullptr);
188
189 RunningLockInfo runninglockPhone("RunningLockMockPhone3.1", RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
190 RunningLockInfo runninglockNotify("RunningLockMockNotify3.1", RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION);
191 int32_t timeoutMs = -1;
192 uint32_t lockActionCount = 0;
193 uint32_t unlockActionCount = 0;
194
__anon4b4103a20702(RunningLockType type) 195 auto GetRunningLockInfo = [&](RunningLockType type) {
196 if (type == RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE) {
197 return runninglockPhone;
198 }
199 return runninglockNotify;
200 };
201
__anon4b4103a20802(const RunningLockParam& param) 202 EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
203 RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
204 EXPECT_EQ(param.name, runninglockInfo.name);
205 EXPECT_EQ(param.type, runninglockInfo.type);
206 EXPECT_EQ(param.timeoutMs, timeoutMs);
207 lockActionCount++;
208 return RUNNINGLOCK_SUCCESS;
209 });
__anon4b4103a20902(const RunningLockParam& param) 210 EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
211 RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
212 EXPECT_EQ(param.name, runninglockInfo.name);
213 EXPECT_EQ(param.type, runninglockInfo.type);
214 unlockActionCount++;
215 return RUNNINGLOCK_SUCCESS;
216 });
217
218 sptr<IRemoteObject> phoneToken = new RunningLockTokenStub();
219 sptr<IRemoteObject> notifyToken = new RunningLockTokenStub();
220 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(phoneToken, runninglockPhone));
221 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(notifyToken, runninglockNotify));
222
223 g_powerService->Lock(phoneToken);
224 g_powerService->Lock(notifyToken);
225 g_powerService->UnLock(phoneToken);
226 g_powerService->UnLock(notifyToken);
227
228 g_powerService->ReleaseRunningLock(phoneToken);
229 g_powerService->ReleaseRunningLock(notifyToken);
230
231 EXPECT_EQ(lockActionCount, 2);
232 EXPECT_EQ(unlockActionCount, 2);
233 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest003 end.");
234 }
235
236 /**
237 * @tc.name: RunningLockMockTest004
238 * @tc.desc: test scene runninglock by mock
239 * @tc.type: FUNC
240 * @tc.require: issueI6LPK9
241 */
242 HWTEST_F (RunningLockMockTest, RunningLockMockTest004, TestSize.Level2)
243 {
244 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest004 start.");
245 ASSERT_NE(g_powerService, nullptr);
246 ASSERT_NE(g_lockAction, nullptr);
247
248 RunningLockInfo runninglockAudio("RunningLockMockAudio4.1", RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO);
249 RunningLockInfo runninglockSport("RunningLockMockSport4.1", RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT);
250 int32_t timeoutMs = -1;
251 uint32_t lockActionCount = 0;
252 uint32_t unlockActionCount = 0;
253
__anon4b4103a20a02(RunningLockType type) 254 auto GetRunningLockInfo = [&](RunningLockType type) {
255 if (type == RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO) {
256 return runninglockAudio;
257 }
258 return runninglockSport;
259 };
260
__anon4b4103a20b02(const RunningLockParam& param) 261 EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
262 RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
263 EXPECT_EQ(param.name, runninglockInfo.name);
264 EXPECT_EQ(param.type, runninglockInfo.type);
265 EXPECT_EQ(param.timeoutMs, timeoutMs);
266 lockActionCount++;
267 return RUNNINGLOCK_SUCCESS;
268 });
__anon4b4103a20c02(const RunningLockParam& param) 269 EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
270 RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
271 EXPECT_EQ(param.name, runninglockInfo.name);
272 EXPECT_EQ(param.type, runninglockInfo.type);
273 unlockActionCount++;
274 return RUNNINGLOCK_SUCCESS;
275 });
276
277 sptr<IRemoteObject> audioToken = new RunningLockTokenStub();
278 sptr<IRemoteObject> sportToken = new RunningLockTokenStub();
279 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(audioToken, runninglockAudio));
280 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(sportToken, runninglockSport));
281
282 g_powerService->Lock(audioToken);
283 g_powerService->Lock(sportToken);
284 g_powerService->UnLock(audioToken);
285 g_powerService->UnLock(sportToken);
286
287 g_powerService->ReleaseRunningLock(audioToken);
288 g_powerService->ReleaseRunningLock(sportToken);
289
290 EXPECT_EQ(lockActionCount, 2);
291 EXPECT_EQ(unlockActionCount, 2);
292 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest004 end.");
293 }
294
295 /**
296 * @tc.name: RunningLockMockTest005
297 * @tc.desc: test scene runninglock by mock
298 * @tc.type: FUNC
299 * @tc.require: issueI6LPK9
300 */
301 HWTEST_F (RunningLockMockTest, RunningLockMockTest005, TestSize.Level2)
302 {
303 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest005 start.");
304 ASSERT_NE(g_powerService, nullptr);
305 ASSERT_NE(g_lockAction, nullptr);
306
307 RunningLockInfo runninglockNavi("RunningLockMockNavi5.1", RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION);
308 RunningLockInfo runninglockTask("RunningLockMockTask5.1", RunningLockType::RUNNINGLOCK_BACKGROUND_TASK);
309 int32_t timeoutMs = -1;
310 uint32_t lockActionCount = 0;
311 uint32_t unlockActionCount = 0;
312
__anon4b4103a20d02(RunningLockType type) 313 auto GetRunningLockInfo = [&](RunningLockType type) {
314 if (type == RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION) {
315 return runninglockNavi;
316 }
317 return runninglockTask;
318 };
319
__anon4b4103a20e02(const RunningLockParam& param) 320 EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
321 auto runninglockInfo = GetRunningLockInfo(param.type);
322 EXPECT_EQ(param.name, runninglockInfo.name);
323 EXPECT_EQ(param.type, runninglockInfo.type);
324 EXPECT_EQ(param.timeoutMs, timeoutMs);
325 lockActionCount++;
326 return RUNNINGLOCK_SUCCESS;
327 });
__anon4b4103a20f02(const RunningLockParam& param) 328 EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
329 auto runninglockInfo = GetRunningLockInfo(param.type);
330 EXPECT_EQ(param.name, runninglockInfo.name);
331 EXPECT_EQ(param.type, runninglockInfo.type);
332 unlockActionCount++;
333 return RUNNINGLOCK_SUCCESS;
334 });
335
336 sptr<IRemoteObject> naviToken = new RunningLockTokenStub();
337 sptr<IRemoteObject> taskToken = new RunningLockTokenStub();
338
339 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(naviToken, runninglockNavi));
340 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(taskToken, runninglockTask));
341
342 g_powerService->Lock(naviToken);
343 g_powerService->Lock(taskToken);
344 g_powerService->UnLock(naviToken);
345 g_powerService->UnLock(taskToken);
346
347 g_powerService->ReleaseRunningLock(naviToken);
348 g_powerService->ReleaseRunningLock(taskToken);
349
350 EXPECT_EQ(lockActionCount, 2);
351 EXPECT_EQ(unlockActionCount, 2);
352 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest005 end.");
353 }
354
355 /**
356 * @tc.name: RunningLockMockTest006
357 * @tc.desc: test scene runninglock release function by mock
358 * @tc.type: FUNC
359 * @tc.require: issueI6LPK9
360 */
361 HWTEST_F (RunningLockMockTest, RunningLockMockTest006, TestSize.Level2)
362 {
363 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest006 start.");
364 ASSERT_NE(g_powerService, nullptr);
365 ASSERT_NE(g_lockAction, nullptr);
366
367 RunningLockInfo runninglockPhone("RunningLockMockPhone6.1", RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
368 RunningLockInfo runninglockNotify("RunningLockMockNotify6.1", RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION);
369 RunningLockInfo runninglockAudio("RunningLockMockAudio6.1", RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO);
370 int32_t timeoutMs = 100;
371 uint32_t lockActionCount = 0;
372 uint32_t unlockActionCount = 0;
373
__anon4b4103a21002(RunningLockType type) 374 auto GetRunningLockInfo = [&](RunningLockType type) {
375 RunningLockInfo lockInfo {};
376 switch (type) {
377 case RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE:
378 return runninglockPhone;
379 case RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION:
380 return runninglockNotify;
381 case RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO:
382 return runninglockAudio;
383 default:
384 return lockInfo;
385 }
386 };
387
__anon4b4103a21102(const RunningLockParam& param) 388 EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
389 lockActionCount++;
390 return RUNNINGLOCK_SUCCESS;
391 });
__anon4b4103a21202(const RunningLockParam& param) 392 EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
393 RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
394 EXPECT_EQ(param.name, runninglockInfo.name);
395 EXPECT_EQ(param.type, runninglockInfo.type);
396 unlockActionCount++;
397 return RUNNINGLOCK_SUCCESS;
398 });
399
400 sptr<IRemoteObject> phoneToken = new RunningLockTokenStub();
401 sptr<IRemoteObject> notifyToken = new RunningLockTokenStub();
402 sptr<IRemoteObject> audioToken = new RunningLockTokenStub();
403
404 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(phoneToken, runninglockPhone));
405 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(notifyToken, runninglockNotify));
406 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(audioToken, runninglockAudio));
407
408 g_powerService->Lock(phoneToken);
409 g_powerService->Lock(notifyToken);
410 g_powerService->Lock(audioToken);
411
412 g_powerService->UnLock(phoneToken);
413 g_powerService->UnLock(notifyToken);
414 g_powerService->UnLock(audioToken);
415
416 g_powerService->ReleaseRunningLock(phoneToken);
417 g_powerService->ReleaseRunningLock(notifyToken);
418 g_powerService->ReleaseRunningLock(audioToken);
419
420 EXPECT_EQ(lockActionCount, 3);
421 EXPECT_EQ(unlockActionCount, 3);
422 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest006 end.");
423 }
424
425 /**
426 * @tc.name: RunningLockMockTest007
427 * @tc.desc: test scene runninglock release function by mock
428 * @tc.type: FUNC
429 * @tc.require: issueI6LPK9
430 */
431 HWTEST_F (RunningLockMockTest, RunningLockMockTest007, TestSize.Level2)
432 {
433 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest007 start.");
434 ASSERT_NE(g_powerService, nullptr);
435 ASSERT_NE(g_lockAction, nullptr);
436
437 RunningLockInfo runninglockSport("RunningLockMockSport7.1", RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT);
438 RunningLockInfo runninglockNavi("RunningLockMockNavi7.1", RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION);
439 RunningLockInfo runninglockTask("RunningLockMockTask7.1", RunningLockType::RUNNINGLOCK_BACKGROUND_TASK);
440 int32_t timeoutMs = 100;
441 uint32_t lockActionCount = 0;
442 uint32_t unlockActionCount = 0;
443
__anon4b4103a21302(RunningLockType type) 444 auto GetRunningLockInfo = [&](RunningLockType type) {
445 RunningLockInfo lockInfo {};
446 switch (type) {
447 case RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT:
448 return runninglockSport;
449 case RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION:
450 return runninglockNavi;
451 case RunningLockType::RUNNINGLOCK_BACKGROUND_TASK:
452 return runninglockTask;
453 default:
454 return lockInfo;
455 }
456 };
457
__anon4b4103a21402(const RunningLockParam& param) 458 EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
459 lockActionCount++;
460 return RUNNINGLOCK_SUCCESS;
461 });
__anon4b4103a21502(const RunningLockParam& param) 462 EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
463 RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
464 EXPECT_EQ(param.name, runninglockInfo.name);
465 EXPECT_EQ(param.type, runninglockInfo.type);
466 unlockActionCount++;
467 return RUNNINGLOCK_SUCCESS;
468 });
469
470 sptr<IRemoteObject> sportToken = new RunningLockTokenStub();
471 sptr<IRemoteObject> naviToken = new RunningLockTokenStub();
472 sptr<IRemoteObject> taskToken = new RunningLockTokenStub();
473
474 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(sportToken, runninglockSport));
475 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(naviToken, runninglockNavi));
476 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(taskToken, runninglockTask));
477
478 g_powerService->Lock(sportToken);
479 g_powerService->Lock(naviToken);
480 g_powerService->Lock(taskToken);
481
482 g_powerService->UnLock(sportToken);
483 g_powerService->UnLock(naviToken);
484 g_powerService->UnLock(taskToken);
485
486 g_powerService->ReleaseRunningLock(sportToken);
487 g_powerService->ReleaseRunningLock(naviToken);
488 g_powerService->ReleaseRunningLock(taskToken);
489
490 EXPECT_EQ(lockActionCount, 3);
491 EXPECT_EQ(unlockActionCount, 3);
492 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest007 end.");
493 }
494
495 /**
496 * @tc.name: RunningLockMockTest008
497 * @tc.desc: Test ProxyRunningLock function, test Background runninglock
498 * @tc.type: FUNC
499 * @tc.require: issueI6S0YY
500 */
501 HWTEST_F (RunningLockMockTest, RunningLockMockTest008, TestSize.Level2)
502 {
503 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest008 start.");
504 ASSERT_NE(g_powerService, nullptr);
505 ASSERT_NE(g_lockAction, nullptr);
506
507 RunningLockInfo runninglockInfo("RunningLockMockBackground8.1", RunningLockType::RUNNINGLOCK_BACKGROUND);
508 auto runningLockMgr = g_powerService->GetRunningLockMgr();
509 uint32_t lockActionCount = 0;
510 uint32_t unlockActionCount = 0;
511 pid_t curUid = getuid();
512 pid_t curPid = getpid();
513
__anon4b4103a21602(const RunningLockParam& param) 514 EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
515 EXPECT_EQ(param.name, runninglockInfo.name);
516 EXPECT_EQ(param.type, RunningLockType::RUNNINGLOCK_BACKGROUND_TASK);
517 EXPECT_EQ(param.timeoutMs, RUNNINGLOCKPARAM_TIMEOUTMS_DEF);
518 lockActionCount++;
519 return RUNNINGLOCK_SUCCESS;
520 });
__anon4b4103a21702(const RunningLockParam& param) 521 EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
522 EXPECT_EQ(param.name, runninglockInfo.name);
523 EXPECT_EQ(param.type, RunningLockType::RUNNINGLOCK_BACKGROUND_TASK);
524 unlockActionCount++;
525 return RUNNINGLOCK_SUCCESS;
526 });
527
528 sptr<IRemoteObject> runninglockToken = new RunningLockTokenStub();
529 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(runninglockToken, runninglockInfo));
530 auto backgroundLock = runningLockMgr->GetRunningLockInner(runninglockToken);
531 ASSERT_NE(backgroundLock, nullptr);
532 g_powerService->Lock(runninglockToken);
533 EXPECT_TRUE(backgroundLock->GetState() == RunningLockState::RUNNINGLOCK_STATE_ENABLE);
534 EXPECT_EQ(lockActionCount, 1);
535
536 EXPECT_TRUE(g_powerService->ProxyRunningLock(true, curPid, curUid));
537
538 EXPECT_TRUE(backgroundLock->GetState() == RunningLockState::RUNNINGLOCK_STATE_PROXIED);
539 EXPECT_EQ(unlockActionCount, 1);
540
541 EXPECT_TRUE(g_powerService->ProxyRunningLock(false, curPid, curUid));
542
543 EXPECT_TRUE(backgroundLock->GetState() == RunningLockState::RUNNINGLOCK_STATE_ENABLE);
544 EXPECT_EQ(lockActionCount, 2);
545
546 g_powerService->UnLock(runninglockToken);
547 g_powerService->ReleaseRunningLock(runninglockToken);
548 EXPECT_EQ(unlockActionCount, 2);
549 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest008 end.");
550 }
551
552 /**
553 * @tc.name: RunningLockMockTest009
554 * @tc.desc: Test ProxyRunningLock function, test Scene runninglock
555 * @tc.type: FUNC
556 * @tc.require: issueI6S0YY
557 */
558 HWTEST_F (RunningLockMockTest, RunningLockMockTest009, TestSize.Level2)
559 {
560 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest009 start.");
561 ASSERT_NE(g_powerService, nullptr);
562 ASSERT_NE(g_lockAction, nullptr);
563
564 RunningLockInfo runninglockPhone("RunningLockMockPhone9.1", RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
565 RunningLockInfo runninglockNotify("RunningLockMockNotify9.1", RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION);
566 uint32_t lockActionCount = 0;
567 uint32_t unlockActionCount = 0;
568 pid_t curUid = getuid();
569 pid_t curPid = getpid();
570
__anon4b4103a21802(RunningLockType type) 571 auto GetRunningLockInfo = [&](RunningLockType type) {
572 if (type == RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE) {
573 return runninglockPhone;
574 }
575 return runninglockNotify;
576 };
577
__anon4b4103a21902(const RunningLockParam& param) 578 EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
579 RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
580 EXPECT_EQ(param.name, runninglockInfo.name);
581 EXPECT_EQ(param.type, runninglockInfo.type);
582 EXPECT_EQ(param.timeoutMs, RUNNINGLOCKPARAM_TIMEOUTMS_DEF);
583 lockActionCount++;
584 return RUNNINGLOCK_SUCCESS;
585 });
__anon4b4103a21a02(const RunningLockParam& param) 586 EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
587 RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
588 EXPECT_EQ(param.name, runninglockInfo.name);
589 EXPECT_EQ(param.type, runninglockInfo.type);
590 unlockActionCount++;
591 return RUNNINGLOCK_NOT_SUPPORT;
592 });
593
594 sptr<IRemoteObject> phoneToken = new RunningLockTokenStub();
595 sptr<IRemoteObject> notifyToken = new RunningLockTokenStub();
596 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(phoneToken, runninglockPhone));
597 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(notifyToken, runninglockNotify));
598
599
600 EXPECT_TRUE(g_powerService->ProxyRunningLocks(true, {std::make_pair(curPid, curUid)}));
601 EXPECT_EQ(unlockActionCount, 0);
602
603 g_powerService->Lock(phoneToken);
604 g_powerService->Lock(notifyToken);
605 g_powerService->UnLock(phoneToken);
606 g_powerService->UnLock(notifyToken);
607
608 EXPECT_EQ(lockActionCount, 2);
609 EXPECT_EQ(unlockActionCount, 2);
610
611
612 EXPECT_TRUE(g_powerService->ProxyRunningLocks(false, {std::make_pair(curPid, curUid)}));
613 EXPECT_EQ(lockActionCount, 2);
614
615 g_powerService->ReleaseRunningLock(phoneToken);
616 g_powerService->ReleaseRunningLock(notifyToken);
617 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest009 end.");
618 }
619
620 /**
621 * @tc.name: RunningLockMockTest010
622 * @tc.desc: Test ProxyRunningLock function, test Scene runninglock
623 * @tc.type: FUNC
624 * @tc.require: issueI6S0YY
625 */
626 HWTEST_F (RunningLockMockTest, RunningLockMockTest010, TestSize.Level2)
627 {
628 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest010 start.");
629 ASSERT_NE(g_powerService, nullptr);
630 ASSERT_NE(g_lockAction, nullptr);
631
632 RunningLockInfo runninglockAudio("RunningLockMockAudio10.1", RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO);
633 RunningLockInfo runninglockSport("RunningLockMockSport10.1", RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT);
634 auto runningLockMgr = g_powerService->GetRunningLockMgr();
635 uint32_t lockActionCount = 0;
636 uint32_t unlockActionCount = 0;
637
__anon4b4103a21b02(RunningLockType type) 638 auto GetRunningLockInfo = [&](RunningLockType type) {
639 return type == RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO ? runninglockAudio : runninglockSport;
640 };
641
__anon4b4103a21c02(const RunningLockParam& param) 642 EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
643 RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
644 EXPECT_EQ(param.name, runninglockInfo.name);
645 EXPECT_EQ(param.type, runninglockInfo.type);
646 EXPECT_EQ(param.timeoutMs, RUNNINGLOCKPARAM_TIMEOUTMS_DEF);
647 lockActionCount++;
648 return RUNNINGLOCK_SUCCESS;
649 });
__anon4b4103a21d02(const RunningLockParam& param) 650 EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
651 RunningLockInfo runninglockInfo = GetRunningLockInfo(param.type);
652 EXPECT_EQ(param.name, runninglockInfo.name);
653 EXPECT_EQ(param.type, runninglockInfo.type);
654 unlockActionCount++;
655 return RUNNINGLOCK_SUCCESS;
656 });
657
658 sptr<IRemoteObject> audioToken = new RunningLockTokenStub();
659 sptr<IRemoteObject> sportToken = new RunningLockTokenStub();
660 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(audioToken, runninglockAudio));
661 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(sportToken, runninglockSport));
662 auto audioLock = runningLockMgr->GetRunningLockInner(audioToken);
663 auto sportLock = runningLockMgr->GetRunningLockInner(sportToken);
664
665 g_powerService->Lock(audioToken);
666 g_powerService->Lock(sportToken);
667 EXPECT_EQ(lockActionCount, 2);
668 EXPECT_TRUE(audioLock->GetState() == RunningLockState::RUNNINGLOCK_STATE_ENABLE);
669 EXPECT_TRUE(sportLock->GetState() == RunningLockState::RUNNINGLOCK_STATE_ENABLE);
670
671 EXPECT_TRUE(g_powerService->ProxyRunningLock(true, getpid(), getuid()));
672
673 EXPECT_EQ(unlockActionCount, 2);
674 EXPECT_TRUE(audioLock->GetState() == RunningLockState::RUNNINGLOCK_STATE_PROXIED);
675 EXPECT_TRUE(sportLock->GetState() == RunningLockState::RUNNINGLOCK_STATE_PROXIED);
676
677 EXPECT_TRUE(g_powerService->ProxyRunningLock(false, getpid(), getuid()));
678
679 EXPECT_EQ(lockActionCount, 4);
680 EXPECT_TRUE(audioLock->GetState() == RunningLockState::RUNNINGLOCK_STATE_ENABLE);
681 EXPECT_TRUE(sportLock->GetState() == RunningLockState::RUNNINGLOCK_STATE_ENABLE);
682
683 g_powerService->UnLock(audioToken);
684 g_powerService->UnLock(sportToken);
685 g_powerService->ReleaseRunningLock(audioToken);
686 g_powerService->ReleaseRunningLock(sportToken);
687 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest010 end.");
688 }
689
690 /**
691 * @tc.name: RunningLockMockTest011
692 * @tc.desc: Test ProxyRunningLock function, test Scene runninglock, HDI unlock() return RUNNINGLOCK_NOT_SUPPORT
693 * @tc.type: FUNC
694 * @tc.require: issueI6S0YY
695 */
696 HWTEST_F (RunningLockMockTest, RunningLockMockTest011, TestSize.Level2)
697 {
698 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest011 start.");
699 ASSERT_NE(g_powerService, nullptr);
700 ASSERT_NE(g_lockAction, nullptr);
701
702 RunningLockInfo runninglockNavi("RunningLockMockNavi11.1", RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION);
703 auto runningLockMgr = g_powerService->GetRunningLockMgr();
704 uint32_t lockActionCount = 0;
705 uint32_t unlockActionCount = 0;
706 int32_t timeoutMs = -1;
707 pid_t curUid = getuid();
708 pid_t curPid = getpid();
709
__anon4b4103a21e02(const RunningLockParam& param) 710 EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
711 EXPECT_EQ(param.name, runninglockNavi.name);
712 EXPECT_EQ(param.type, runninglockNavi.type);
713 EXPECT_EQ(param.timeoutMs, timeoutMs);
714 lockActionCount++;
715 return RUNNINGLOCK_SUCCESS;
716 });
__anon4b4103a21f02(const RunningLockParam& param) 717 EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
718 EXPECT_EQ(param.name, runninglockNavi.name);
719 EXPECT_EQ(param.type, runninglockNavi.type);
720 unlockActionCount++;
721 return RUNNINGLOCK_NOT_SUPPORT;
722 });
723
724 sptr<IRemoteObject> naviToken = new RunningLockTokenStub();
725 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(naviToken, runninglockNavi));
726 auto naviLock = runningLockMgr->GetRunningLockInner(naviToken);
727
728 g_powerService->Lock(naviToken);
729 EXPECT_EQ(lockActionCount, 1);
730 EXPECT_TRUE(naviLock->GetState() == RunningLockState::RUNNINGLOCK_STATE_ENABLE);
731
732 EXPECT_TRUE(g_powerService->ProxyRunningLock(true, curPid, curUid));
733
734 EXPECT_EQ(unlockActionCount, 1);
735 EXPECT_TRUE(naviLock->GetState() == RunningLockState::RUNNINGLOCK_STATE_PROXIED);
736
737 EXPECT_TRUE(g_powerService->ProxyRunningLock(false, curPid, curUid));
738
739 EXPECT_EQ(lockActionCount, 1);
740 EXPECT_TRUE(naviLock->GetState() == RunningLockState::RUNNINGLOCK_STATE_ENABLE);
741
742 g_powerService->UnLock(naviToken);
743 g_powerService->ReleaseRunningLock(naviToken);
744 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest011 end.");
745 }
746
747 /**
748 * @tc.name: RunningLockMockTest012
749 * @tc.desc: Test ProxyRunningLock function, test Scene runninglock, HDI unlock() return RUNNINGLOCK_SUCCESS
750 * @tc.type: FUNC
751 * @tc.require: issueI6S0YY
752 */
753 HWTEST_F (RunningLockMockTest, RunningLockMockTest012, TestSize.Level2)
754 {
755 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest012 start.");
756 ASSERT_NE(g_powerService, nullptr);
757 ASSERT_NE(g_lockAction, nullptr);
758
759 RunningLockInfo runninglockTask("RunningLockMockTask12.1", RunningLockType::RUNNINGLOCK_BACKGROUND_TASK);
760 auto runningLockMgr = g_powerService->GetRunningLockMgr();
761 uint32_t lockActionCount = 0;
762 uint32_t unlockActionCount = 0;
763 int32_t timeoutMs = -1;
764 pid_t curUid = getuid();
765 pid_t curPid = getpid();
766
__anon4b4103a22002(const RunningLockParam& param) 767 EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
768 EXPECT_EQ(param.name, runninglockTask.name);
769 EXPECT_EQ(param.type, runninglockTask.type);
770 EXPECT_EQ(param.timeoutMs, timeoutMs);
771 lockActionCount++;
772 return RUNNINGLOCK_SUCCESS;
773 });
__anon4b4103a22102(const RunningLockParam& param) 774 EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
775 EXPECT_EQ(param.name, runninglockTask.name);
776 EXPECT_EQ(param.type, runninglockTask.type);
777 unlockActionCount++;
778 return RUNNINGLOCK_SUCCESS;
779 });
780
781 sptr<IRemoteObject> taskToken = new RunningLockTokenStub();
782 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(taskToken, runninglockTask));
783 auto taskLock = runningLockMgr->GetRunningLockInner(taskToken);
784
785 g_powerService->Lock(taskToken);
786 EXPECT_EQ(lockActionCount, 1);
787 EXPECT_TRUE(taskLock->GetState() == RunningLockState::RUNNINGLOCK_STATE_ENABLE);
788
789
790 EXPECT_TRUE(g_powerService->ProxyRunningLocks(true, {std::make_pair(curPid, curUid)}));
791 EXPECT_EQ(unlockActionCount, 1);
792 EXPECT_TRUE(taskLock->GetState() == RunningLockState::RUNNINGLOCK_STATE_PROXIED);
793
794
795 EXPECT_TRUE(g_powerService->ProxyRunningLocks(false, {std::make_pair(curPid, curUid)}));
796 EXPECT_EQ(lockActionCount, 2);
797 EXPECT_TRUE(taskLock->GetState() == RunningLockState::RUNNINGLOCK_STATE_ENABLE);
798
799 g_powerService->UnLock(taskToken);
800 g_powerService->ReleaseRunningLock(taskToken);
801 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest012 end.");
802 }
803
804 /**
805 * @tc.name: RunningLockMockTest013
806 * @tc.desc: Test ProxyRunningLock function, runninglock release first, then cancel the proxy
807 * @tc.type: FUNC
808 * @tc.require: issueI6TW2R
809 */
810 HWTEST_F (RunningLockMockTest, RunningLockMockTest013, TestSize.Level2)
811 {
812 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest013 start.");
813 ASSERT_NE(g_powerService, nullptr);
814 ASSERT_NE(g_lockAction, nullptr);
815
816 RunningLockInfo runninglockTask("RunningLockMockTask13.1", RunningLockType::RUNNINGLOCK_BACKGROUND_TASK);
817 auto runningLockMgr = g_powerService->GetRunningLockMgr();
818 uint32_t lockActionCount = 0;
819 uint32_t unlockActionCount = 0;
820 int32_t timeoutMs = -1;
821 pid_t curUid = getuid();
822 pid_t curPid = getpid();
823
__anon4b4103a22202(const RunningLockParam& param) 824 EXPECT_CALL(*g_lockAction, Lock(_)).WillRepeatedly([&](const RunningLockParam& param) {
825 EXPECT_EQ(param.name, runninglockTask.name);
826 EXPECT_EQ(param.type, runninglockTask.type);
827 EXPECT_EQ(param.timeoutMs, timeoutMs);
828 lockActionCount++;
829 return RUNNINGLOCK_SUCCESS;
830 });
__anon4b4103a22302(const RunningLockParam& param) 831 EXPECT_CALL(*g_lockAction, Unlock(_)).WillRepeatedly([&](const RunningLockParam& param) {
832 EXPECT_EQ(param.name, runninglockTask.name);
833 EXPECT_EQ(param.type, runninglockTask.type);
834 unlockActionCount++;
835 return RUNNINGLOCK_SUCCESS;
836 });
837
838 sptr<IRemoteObject> taskToken = new RunningLockTokenStub();
839 EXPECT_EQ(PowerErrors::ERR_OK, g_powerService->CreateRunningLock(taskToken, runninglockTask));
840 auto taskLock = runningLockMgr->GetRunningLockInner(taskToken);
841
842 g_powerService->Lock(taskToken);
843 EXPECT_EQ(lockActionCount, 1);
844 EXPECT_TRUE(taskLock->GetState() == RunningLockState::RUNNINGLOCK_STATE_ENABLE);
845
846 EXPECT_TRUE(g_powerService->ProxyRunningLock(true, curPid, curUid));
847
848 EXPECT_EQ(unlockActionCount, 1);
849 EXPECT_TRUE(taskLock->GetState() == RunningLockState::RUNNINGLOCK_STATE_PROXIED);
850
851 g_powerService->UnLock(taskToken);
852 g_powerService->ReleaseRunningLock(taskToken);
853 EXPECT_EQ(runningLockMgr->GetRunningLockInner(taskToken), nullptr);
854
855 EXPECT_TRUE(g_powerService->ProxyRunningLock(false, curPid, curUid));
856
857 EXPECT_EQ(lockActionCount, 1);
858 POWER_HILOGI(LABEL_TEST, "RunningLockMockTest013 end.");
859 }
860 }
861