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