• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #define private public
16 #define protected public
17 #include "screenlock_system_ability.h"
18 #undef private
19 #undef protected
20 
21 #include <cstdint>
22 #include <list>
23 #include <string>
24 #include <sys/time.h>
25 
26 #include "accesstoken_kit.h"
27 #include "sclock_log.h"
28 #include "screenlock_callback_test.h"
29 #include "screenlock_common.h"
30 #include "screenlock_event_list_test.h"
31 #include "screenlock_notify_test_instance.h"
32 #include "screenlock_service_test.h"
33 #include "screenlock_system_ability.h"
34 #include "screenlock_system_ability_stub.h"
35 #include "securec.h"
36 #include "token_setproc.h"
37 
38 
39 namespace OHOS {
40 namespace ScreenLock {
41 using namespace testing::ext;
42 using namespace OHOS::Rosen;
43 using namespace OHOS::Security::AccessToken;
44 constexpr const uint16_t EACH_LINE_LENGTH = 100;
45 constexpr const uint16_t TOTAL_LENGTH = 1000;
46 constexpr const char *CMD1 = "hidumper -s 3704";
47 constexpr const char *CMD2 = "hidumper -s 3704 -a -h";
48 constexpr const char *CMD3 = "hidumper -s 3704 -a -all";
49 uint64_t g_selfTokenID = 0;
50 static EventListenerTest g_unlockTestListener;
51 
52 static HapPolicyParams g_policyParams = { .apl = APL_SYSTEM_CORE,
53     .domain = "test.domain",
54     .permList = { { .permissionName = "ohos.permission.ACCESS_SCREEN_LOCK_INNER",
55                       .bundleName = "ohos.screenlock_test.demo",
56                       .grantMode = 1,
57                       .availableLevel = APL_NORMAL,
58                       .label = "label",
59                       .labelId = 1,
60                       .description = "test",
61                       .descriptionId = 1 },
62         { .permissionName = "ohos.permission.DUMP",
63             .bundleName = "ohos.screenlock_test.demo",
64             .grantMode = 1,
65             .availableLevel = APL_SYSTEM_CORE,
66             .label = "label",
67             .labelId = 1,
68             .description = "test",
69             .descriptionId = 1 } },
70     .permStateList = { { .permissionName = "ohos.permission.ACCESS_SCREEN_LOCK_INNER",
71                            .isGeneral = true,
72                            .resDeviceID = { "local" },
73                            .grantStatus = { PermissionState::PERMISSION_GRANTED },
74                            .grantFlags = { 1 } },
75         { .permissionName = "ohos.permission.DUMP",
76             .isGeneral = true,
77             .resDeviceID = { "local" },
78             .grantStatus = { PermissionState::PERMISSION_GRANTED },
79             .grantFlags = { 1 } } } };
80 
81 HapInfoParams g_infoParams = { .userID = 1,
82     .bundleName = "screenlock_service",
83     .instIndex = 0,
84     .appIDDesc = "test",
85     .apiVersion = 9,
86     .isSystemApp = true };
87 
GrantNativePermission()88 void GrantNativePermission()
89 {
90     g_selfTokenID = GetSelfTokenID();
91     AccessTokenIDEx tokenIdEx = { 0 };
92     tokenIdEx = AccessTokenKit::AllocHapToken(g_infoParams, g_policyParams);
93     int32_t ret = SetSelfTokenID(tokenIdEx.tokenIDEx);
94     if (ret == 0) {
95         SCLOCK_HILOGI("SetSelfTokenID success!");
96     } else {
97         SCLOCK_HILOGE("SetSelfTokenID fail!");
98     }
99 }
100 
SetUpTestCase()101 void ScreenLockServiceTest::SetUpTestCase()
102 {
103     GrantNativePermission();
104 }
105 
TearDownTestCase()106 void ScreenLockServiceTest::TearDownTestCase()
107 {
108     ScreenLockSystemAbility::GetInstance()->ResetFfrtQueue();
109     SetSelfTokenID(g_selfTokenID);
110 }
111 
SetUp()112 void ScreenLockServiceTest::SetUp()
113 {
114 }
115 
TearDown()116 void ScreenLockServiceTest::TearDown()
117 {
118 }
119 
ExecuteCmd(const std::string & cmd,std::string & result)120 bool ScreenLockServiceTest::ExecuteCmd(const std::string &cmd, std::string &result)
121 {
122     char buff[EACH_LINE_LENGTH] = { 0x00 };
123     char output[TOTAL_LENGTH] = { 0x00 };
124     FILE *ptr = popen(cmd.c_str(), "r");
125     if (ptr != nullptr) {
126         while (fgets(buff, sizeof(buff), ptr) != nullptr) {
127             if (strcat_s(output, sizeof(output), buff) != 0) {
128                 pclose(ptr);
129                 ptr = nullptr;
130                 return false;
131             }
132         }
133         pclose(ptr);
134         ptr = nullptr;
135     } else {
136         return false;
137     }
138     result = std::string(output);
139     return true;
140 }
141 
142 /**
143 * @tc.name: ScreenLockTest001
144 * @tc.desc: beginWakeUp event.
145 * @tc.type: FUNC
146 * @tc.require:
147 * @tc.author:
148 */
149 HWTEST_F(ScreenLockServiceTest, ScreenLockTest001, TestSize.Level0)
150 {
151     SCLOCK_HILOGD("Test event of beginWakeUp");
152     ScreenLockSystemAbility::GetInstance();
153     DisplayPowerEvent event = DisplayPowerEvent::WAKE_UP;
154     EventStatus status = EventStatus::BEGIN;
155     sptr<ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener> displayPowerEventListener = new (std::nothrow)
156         ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener();
157     ASSERT_NE(displayPowerEventListener, nullptr);
158     displayPowerEventListener->OnDisplayPowerEvent(event, status);
159     int retVal = ScreenLockSystemAbility::GetInstance()->GetState().GetInteractiveState();
160     SCLOCK_HILOGD("Test_BeginWakeUp retVal=%{public}d", retVal);
161     EXPECT_EQ(retVal, static_cast<int>(InteractiveState::INTERACTIVE_STATE_BEGIN_WAKEUP));
162 }
163 
164 /**
165 * @tc.name: ScreenLockTest003
166 * @tc.desc: beginSleep event.
167 * @tc.type: FUNC
168 * @tc.require:
169 * @tc.author:
170 */
171 HWTEST_F(ScreenLockServiceTest, ScreenLockTest003, TestSize.Level0)
172 {
173     SCLOCK_HILOGD("Test event of beginsleep");
174     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
175     ScreenLockSystemAbility::GetInstance()->OnStart();
176     DisplayPowerEvent event = DisplayPowerEvent::SLEEP;
177     EventStatus status = EventStatus::BEGIN;
178     sptr<ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener> displayPowerEventListener = new (std::nothrow)
179         ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener();
180     ASSERT_NE(displayPowerEventListener, nullptr);
181     displayPowerEventListener->OnDisplayPowerEvent(event, status);
182     int retVal = ScreenLockSystemAbility::GetInstance()->GetState().GetInteractiveState();
183     SCLOCK_HILOGD("Test_BeginSleep retVal=%{public}d", retVal);
184     EXPECT_EQ(retVal, static_cast<int>(InteractiveState::INTERACTIVE_STATE_BEGIN_SLEEP));
185 }
186 
187 /**
188 * @tc.name: ScreenLockTest004
189 * @tc.desc: beginScreenOn event.
190 * @tc.type: FUNC
191 * @tc.require:
192 * @tc.author:
193 */
194 HWTEST_F(ScreenLockServiceTest, ScreenLockTest004, TestSize.Level0)
195 {
196     SCLOCK_HILOGD("Test event of beginscreenon");
197     ScreenLockSystemAbility::GetInstance();
198     DisplayPowerEvent event = DisplayPowerEvent::DISPLAY_ON;
199     EventStatus status = EventStatus::BEGIN;
200     sptr<ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener> displayPowerEventListener = new (std::nothrow)
201         ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener();
202     ASSERT_NE(displayPowerEventListener, nullptr);
203     displayPowerEventListener->OnDisplayPowerEvent(event, status);
204     int retVal = ScreenLockSystemAbility::GetInstance()->GetState().GetScreenState();
205     SCLOCK_HILOGD("Test_BeginScreenOn retVal=%{public}d", retVal);
206     EXPECT_EQ(retVal, static_cast<int>(ScreenState::SCREEN_STATE_BEGIN_ON));
207 }
208 
209 /**
210 * @tc.name: ScreenLockTest005
211 * @tc.desc: beginScreenOff event.
212 * @tc.type: FUNC
213 * @tc.require:
214 * @tc.author:
215 */
216 HWTEST_F(ScreenLockServiceTest, ScreenLockTest005, TestSize.Level0)
217 {
218     SCLOCK_HILOGD("Test event of beginscreenoff");
219     ScreenLockSystemAbility::GetInstance();
220     DisplayPowerEvent event = DisplayPowerEvent::DISPLAY_OFF;
221     EventStatus status = EventStatus::BEGIN;
222     sptr<ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener> displayPowerEventListener = new (std::nothrow)
223         ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener();
224     ASSERT_NE(displayPowerEventListener, nullptr);
225     displayPowerEventListener->OnDisplayPowerEvent(event, status);
226     int retVal = ScreenLockSystemAbility::GetInstance()->GetState().GetScreenState();
227     SCLOCK_HILOGD("Test_BeginScreenOff retVal=%{public}d", retVal);
228     EXPECT_EQ(retVal, static_cast<int>(ScreenState::SCREEN_STATE_BEGIN_OFF));
229 }
230 
231 /**
232 * @tc.name: ScreenLockTest006
233 * @tc.desc: endWakeUp event.
234 * @tc.type: FUNC
235 * @tc.require:
236 * @tc.author:
237 */
238 HWTEST_F(ScreenLockServiceTest, ScreenLockTest006, TestSize.Level0)
239 {
240     SCLOCK_HILOGD("Test event of endwakeup");
241     ScreenLockSystemAbility::GetInstance();
242     DisplayPowerEvent event = DisplayPowerEvent::WAKE_UP;
243     EventStatus status = EventStatus::END;
244     sptr<ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener> displayPowerEventListener = new (std::nothrow)
245         ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener();
246     ASSERT_NE(displayPowerEventListener, nullptr);
247     displayPowerEventListener->OnDisplayPowerEvent(event, status);
248     int retVal = ScreenLockSystemAbility::GetInstance()->GetState().GetInteractiveState();
249     SCLOCK_HILOGD("Test_EndWakeUp retVal=%{public}d", retVal);
250     EXPECT_EQ(retVal, static_cast<int>(InteractiveState::INTERACTIVE_STATE_END_WAKEUP));
251 }
252 
253 /**
254 * @tc.name: ScreenLockTest007
255 * @tc.desc: endSleep event.
256 * @tc.type: FUNC
257 * @tc.require:
258 * @tc.author:
259 */
260 HWTEST_F(ScreenLockServiceTest, ScreenLockTest007, TestSize.Level0)
261 {
262     SCLOCK_HILOGD("Test event of endsleep");
263     ScreenLockSystemAbility::GetInstance();
264     DisplayPowerEvent event = DisplayPowerEvent::SLEEP;
265     EventStatus status = EventStatus::END;
266     sptr<ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener> displayPowerEventListener = new (std::nothrow)
267         ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener();
268     ASSERT_NE(displayPowerEventListener, nullptr);
269     displayPowerEventListener->OnDisplayPowerEvent(event, status);
270     int retVal = ScreenLockSystemAbility::GetInstance()->GetState().GetInteractiveState();
271     SCLOCK_HILOGD("Test_EndSleep retVal=%{public}d", retVal);
272     EXPECT_EQ(retVal, static_cast<int>(InteractiveState::INTERACTIVE_STATE_END_SLEEP));
273 }
274 
275 /**
276 * @tc.name: ScreenLockTest008
277 * @tc.desc: endScreenOn event.
278 * @tc.type: FUNC
279 * @tc.require:
280 * @tc.author:
281 */
282 HWTEST_F(ScreenLockServiceTest, ScreenLockTest008, TestSize.Level0)
283 {
284     SCLOCK_HILOGD("Test event of endscreenon");
285     ScreenLockSystemAbility::GetInstance();
286     DisplayPowerEvent event = DisplayPowerEvent::DISPLAY_ON;
287     EventStatus status = EventStatus::END;
288     sptr<ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener> displayPowerEventListener = new (std::nothrow)
289         ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener();
290     ASSERT_NE(displayPowerEventListener, nullptr);
291     displayPowerEventListener->OnDisplayPowerEvent(event, status);
292     int retVal = ScreenLockSystemAbility::GetInstance()->GetState().GetScreenState();
293     SCLOCK_HILOGD("Test_EndScreenOn retVal=%{public}d", retVal);
294     EXPECT_EQ(retVal, static_cast<int>(ScreenState::SCREEN_STATE_END_ON));
295 }
296 
297 /**
298 * @tc.name: ScreenLockTest009
299 * @tc.desc: endScreenOff and begin desktopready event.
300 * @tc.type: FUNC
301 * @tc.require:
302 * @tc.author:
303 */
304 HWTEST_F(ScreenLockServiceTest, ScreenLockTest009, TestSize.Level0)
305 {
306     SCLOCK_HILOGD("Test event of endscreenoff");
307     ScreenLockSystemAbility::GetInstance();
308     DisplayPowerEvent event = DisplayPowerEvent::DISPLAY_OFF;
309     EventStatus status = EventStatus::END;
310     sptr<ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener> displayPowerEventListener = new (std::nothrow)
311         ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener();
312     ASSERT_NE(displayPowerEventListener, nullptr);
313     displayPowerEventListener->OnDisplayPowerEvent(event, status);
314     event = DisplayPowerEvent::DESKTOP_READY;
315     status = EventStatus::BEGIN;
316     displayPowerEventListener->OnDisplayPowerEvent(event, status);
317     int retVal = ScreenLockSystemAbility::GetInstance()->GetState().GetScreenState();
318     SCLOCK_HILOGD("Test_EndScreenOff retVal=%{public}d", retVal);
319     EXPECT_EQ(retVal, static_cast<int>(ScreenState::SCREEN_STATE_END_OFF));
320 }
321 
322 /**
323 * @tc.name: ScreenLockDumperTest013
324 * @tc.desc: dump showhelp.
325 * @tc.type: FUNC
326 * @tc.require:
327 * @tc.author:
328 */
329 HWTEST_F(ScreenLockServiceTest, ScreenLockDumperTest013, TestSize.Level0)
330 {
331     SCLOCK_HILOGD("Test hidumper of showhelp");
332     std::string result;
333     auto ret = ScreenLockServiceTest::ExecuteCmd(CMD1, result);
334     EXPECT_TRUE(ret);
335 }
336 
337 /**
338 * @tc.name: ScreenLockDumperTest014
339 * @tc.desc: dump showhelp.
340 * @tc.type: FUNC
341 * @tc.require:
342 * @tc.author:
343 */
344 HWTEST_F(ScreenLockServiceTest, ScreenLockDumperTest014, TestSize.Level0)
345 {
346     SCLOCK_HILOGD("Test hidumper of -h");
347     std::string result;
348     auto ret = ScreenLockServiceTest::ExecuteCmd(CMD2, result);
349     EXPECT_TRUE(ret);
350 }
351 
352 /**
353 * @tc.name: ScreenLockDumperTest015
354 * @tc.desc: dump screenlock information.
355 * @tc.type: FUNC
356 * @tc.require:
357 * @tc.author:
358 */
359 HWTEST_F(ScreenLockServiceTest, ScreenLockDumperTest015, TestSize.Level0)
360 {
361     SCLOCK_HILOGD("Test hidumper of -all");
362     std::string result;
363     auto ret = ScreenLockServiceTest::ExecuteCmd(CMD3, result);
364     EXPECT_TRUE(ret);
365 }
366 
367 /**
368 * @tc.name: ScreenLockTest016
369 * @tc.desc: Test Lock.
370 * @tc.type: FUNC
371 * @tc.require:
372 * @tc.author:
373 */
374 HWTEST_F(ScreenLockServiceTest, ScreenLockTest016, TestSize.Level0)
375 {
376     SCLOCK_HILOGD("Test RequestLock");
377     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
378     sptr<ScreenLockCallbackInterface> listener = new (std::nothrow) ScreenlockCallbackTest(g_unlockTestListener);
379     ASSERT_NE(listener, nullptr);
380 
381     int32_t userId = ScreenLockSystemAbility::GetInstance()->GetState().GetCurrentUser();
382     ScreenLockSystemAbility::GetInstance()->SetScreenlocked(true, userId);
383     bool isLocked = ScreenLockSystemAbility::GetInstance()->IsScreenLocked();
384     EXPECT_EQ(isLocked, true);
385     int32_t result = ScreenLockSystemAbility::GetInstance()->Lock(listener);
386     EXPECT_EQ(result, E_SCREENLOCK_OK);
387     ScreenLockSystemAbility::GetInstance()->SetScreenlocked(false, userId);
388     result = ScreenLockSystemAbility::GetInstance()->Lock(listener);
389     EXPECT_EQ(result, E_SCREENLOCK_OK);
390 }
391 
392 /**
393 * @tc.name: ScreenLockTest017
394 * @tc.desc: Test Unlock and UnlockScreen.
395 * @tc.type: FUNC
396 * @tc.require:
397 * @tc.author:
398 */
399 HWTEST_F(ScreenLockServiceTest, ScreenLockTest017, TestSize.Level0)
400 {
401     SCLOCK_HILOGD("Test RequestUnlock");
402     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
403     sptr<ScreenLockCallbackInterface> listener = new (std::nothrow) ScreenlockCallbackTest(g_unlockTestListener);
404     ASSERT_NE(listener, nullptr);
405     int32_t result = ScreenLockSystemAbility::GetInstance()->UnlockScreen(listener);
406     EXPECT_EQ(result, E_SCREENLOCK_NOT_FOCUS_APP);
407     result = ScreenLockSystemAbility::GetInstance()->Unlock(listener);
408     EXPECT_EQ(result, E_SCREENLOCK_NOT_FOCUS_APP);
409     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
410     result = ScreenLockSystemAbility::GetInstance()->Unlock(listener);
411     EXPECT_EQ(result, E_SCREENLOCK_NOT_FOCUS_APP);
412 }
413 
414 /**
415 * @tc.name: ScreenLockTest018
416 * @tc.desc: Test SendScreenLockEvent.
417 * @tc.type: FUNC
418 * @tc.require:
419 * @tc.author:
420 */
421 HWTEST_F(ScreenLockServiceTest, ScreenLockTest018, TestSize.Level0)
422 {
423     SCLOCK_HILOGD("Test SendScreenLockEvent");
424     ScreenLockSystemAbility::GetInstance()->SendScreenLockEvent(UNLOCK_SCREEN_RESULT, SCREEN_SUCC);
425     bool isLocked = ScreenLockSystemAbility::GetInstance()->IsScreenLocked();
426     EXPECT_EQ(isLocked, false);
427 }
428 
429 /**
430 * @tc.name: ScreenLockTest019
431 * @tc.desc: Test SendScreenLockEvent.
432 * @tc.type: FUNC
433 * @tc.require:
434 * @tc.author:
435 */
436 HWTEST_F(ScreenLockServiceTest, ScreenLockTest019, TestSize.Level0)
437 {
438     SCLOCK_HILOGD("Test SendScreenLockEvent");
439     ScreenLockSystemAbility::GetInstance()->SendScreenLockEvent(UNLOCK_SCREEN_RESULT, SCREEN_FAIL);
440     bool isLocked = ScreenLockSystemAbility::GetInstance()->IsScreenLocked();
441     EXPECT_EQ(isLocked, false);
442 }
443 
444 /**
445 * @tc.name: ScreenLockTest020
446 * @tc.desc: Test SendScreenLockEvent.
447 * @tc.type: FUNC
448 * @tc.require:
449 * @tc.author:
450 */
451 HWTEST_F(ScreenLockServiceTest, ScreenLockTest020, TestSize.Level0)
452 {
453     SCLOCK_HILOGD("Test SendScreenLockEvent");
454     ScreenLockSystemAbility::GetInstance()->SendScreenLockEvent(UNLOCK_SCREEN_RESULT, SCREEN_CANCEL);
455     bool isLocked = ScreenLockSystemAbility::GetInstance()->IsScreenLocked();
456     EXPECT_EQ(isLocked, false);
457 }
458 
459 /**
460 * @tc.name: ScreenLockTest021
461 * @tc.desc: Test SendScreenLockEvent.
462 * @tc.type: FUNC
463 * @tc.require:
464 * @tc.author:
465 */
466 HWTEST_F(ScreenLockServiceTest, ScreenLockTest021, TestSize.Level0)
467 {
468     SCLOCK_HILOGD("Test SendScreenLockEvent");
469     ScreenLockSystemAbility::GetInstance()->SendScreenLockEvent(LOCK_SCREEN_RESULT, SCREEN_SUCC);
470     bool isLocked;
471     ScreenLockSystemAbility::GetInstance()->IsLocked(isLocked);
472     EXPECT_EQ(isLocked, true);
473 }
474 
475 /**
476 * @tc.name: ScreenLockTest022
477 * @tc.desc: Test SendScreenLockEvent.
478 * @tc.type: FUNC
479 * @tc.require:
480 * @tc.author:
481 */
482 HWTEST_F(ScreenLockServiceTest, ScreenLockTest022, TestSize.Level0)
483 {
484     SCLOCK_HILOGD("Test SendScreenLockEvent");
485     ScreenLockSystemAbility::GetInstance()->SendScreenLockEvent(LOCK_SCREEN_RESULT, SCREEN_FAIL);
486     bool isLocked;
487     ScreenLockSystemAbility::GetInstance()->IsLocked(isLocked);
488     EXPECT_EQ(isLocked, true);
489 }
490 
491 /**
492 * @tc.name: ScreenLockTest023
493 * @tc.desc: Test SendScreenLockEvent.
494 * @tc.type: FUNC
495 * @tc.require:
496 * @tc.author:
497 */
498 HWTEST_F(ScreenLockServiceTest, ScreenLockTest023, TestSize.Level0)
499 {
500     SCLOCK_HILOGD("Test SendScreenLockEvent");
501     ScreenLockSystemAbility::GetInstance()->SendScreenLockEvent(SCREEN_DRAWDONE, SCREEN_SUCC);
502     ScreenLockSystemAbility::GetInstance()->SendScreenLockEvent(LOCK_SCREEN_RESULT, SCREEN_CANCEL);
503     bool isLocked;
504     ScreenLockSystemAbility::GetInstance()->IsLocked(isLocked);
505     EXPECT_EQ(isLocked, true);
506 }
507 
508 /**
509 * @tc.name: ScreenLockTest025
510 * @tc.desc: Test Onstop and OnStart.
511 * @tc.type: FUNC
512 * @tc.require:
513 * @tc.author:
514 */
515 HWTEST_F(ScreenLockServiceTest, ScreenLockTest025, TestSize.Level0)
516 {
517     SCLOCK_HILOGD("Test Onstop");
518     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
519     ScreenLockSystemAbility::GetInstance()->OnStart();
520     ScreenLockSystemAbility::GetInstance()->OnStop();
521     ScreenLockSystemAbility::GetInstance()->OnStart();
522     EXPECT_EQ(ScreenLockSystemAbility::GetInstance()->state_, ServiceRunningState::STATE_NOT_START);
523     int times = 0;
524     ScreenLockSystemAbility::GetInstance()->RegisterDisplayPowerEventListener(times);
525     bool isLocked;
526     ScreenLockSystemAbility::GetInstance()->IsLocked(isLocked);
527     SCLOCK_HILOGD("Test_SendScreenLockEvent of screendrawdone isLocked=%{public}d", isLocked);
528     EXPECT_EQ(isLocked, true);
529 }
530 
531 /**
532 * @tc.name: ScreenLockTest026
533 * @tc.desc: Test GetSecure.
534 * @tc.type: FUNC
535 * @tc.require:
536 * @tc.author:
537 */
538 HWTEST_F(ScreenLockServiceTest, ScreenLockTest026, TestSize.Level0)
539 {
540     SCLOCK_HILOGD("Test GetSecure.");
541     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
542     bool ret = ScreenLockSystemAbility::GetInstance()->GetSecure();
543     EXPECT_EQ(ret, false);
544 }
545 
546 /**
547 * @tc.name: ScreenLockTest027
548 * @tc.desc: Test UnlockScreenEvent.
549 * @tc.type: FUNC
550 * @tc.require:
551 * @tc.author:
552 */
553 HWTEST_F(ScreenLockServiceTest, ScreenLockTest027, TestSize.Level0)
554 {
555     SCLOCK_HILOGD("Test UnlockScreenEvent.");
556     ScreenLockSystemAbility::GetInstance()->unlockVecListeners_.clear();
557     ScreenLockSystemAbility::GetInstance()->UnlockScreenEvent(SCREEN_CANCEL);
558     bool isLocked;
559     ScreenLockSystemAbility::GetInstance()->IsLocked(isLocked);
560     EXPECT_EQ(isLocked, true);
561 }
562 
563 /**
564 * @tc.name: LockTest028
565 * @tc.desc: Test Lock Screen.
566 * @tc.type: FUNC
567 * @tc.require:
568 * @tc.author:
569 */
570 HWTEST_F(ScreenLockServiceTest, LockTest028, TestSize.Level0)
571 {
572     SCLOCK_HILOGD("Test RequestLock.");
573     int32_t userId = 0;
574     int32_t result = ScreenLockSystemAbility::GetInstance()->Lock(userId);
575     EXPECT_EQ(result, E_SCREENLOCK_OK);
576 }
577 
578 /**
579 * @tc.name: ScreenLockTest029
580 * @tc.desc: Test SetScreenLockDisabled.
581 * @tc.type: FUNC
582 * @tc.require:
583 * @tc.author:
584 */
585 HWTEST_F(ScreenLockServiceTest, ScreenLockTest029, TestSize.Level0)
586 {
587     SCLOCK_HILOGD("Test SetScreenLockDisabled.");
588     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
589     int userId = 0;
590     int32_t ret = ScreenLockSystemAbility::GetInstance()->SetScreenLockDisabled(false, userId);
591     bool disable = true;
592     int32_t result = ScreenLockSystemAbility::GetInstance()->IsScreenLockDisabled(userId, disable);
593     SCLOCK_HILOGD("SetScreenLockDisabled.[ret]:%{public}d, [disable]:%{public}d", ret, disable);
594     EXPECT_EQ(result, E_SCREENLOCK_NO_PERMISSION);
595 }
596 
597 /**
598 * @tc.name: ScreenLockTest030
599 * @tc.desc: Test SetScreenLockAuthState.
600 * @tc.type: FUNC
601 * @tc.require:
602 * @tc.author:
603 */
604 HWTEST_F(ScreenLockServiceTest, ScreenLockTest030, TestSize.Level0)
605 {
606     SCLOCK_HILOGD("Test SetScreenLockAuthState.");
607     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
608     int userId = 0;
609     std::string authtoken = "test";
610     int32_t ret = ScreenLockSystemAbility::GetInstance()->SetScreenLockAuthState(1, userId, authtoken);
611     SCLOCK_HILOGD("SetScreenLockAuthState.[ret]:%{public}d", ret);
612 
613     int32_t authState = 0;
614     int32_t result = ScreenLockSystemAbility::GetInstance()->GetScreenLockAuthState(userId, authState);
615     EXPECT_EQ(result, E_SCREENLOCK_NO_PERMISSION);
616 }
617 
618 /**
619 * @tc.name: ScreenLockTest031
620 * @tc.desc: Test RequestStrongAuth.
621 * @tc.type: FUNC
622 * @tc.require:
623 * @tc.author:
624 */
625 HWTEST_F(ScreenLockServiceTest, ScreenLockTest031, TestSize.Level0)
626 {
627     SCLOCK_HILOGD("Test RequestStrongAuth.");
628     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
629     int32_t userId = 0;
630     int reasonFlag = 1;
631     int32_t ret = ScreenLockSystemAbility::GetInstance()->RequestStrongAuth(reasonFlag, userId);
632 
633     ret = ScreenLockSystemAbility::GetInstance()->GetStrongAuth(userId, reasonFlag);
634 
635     EXPECT_EQ(ret, E_SCREENLOCK_OK);
636 }
637 
638 /**
639 * @tc.name: ScreenLockTest032
640 * @tc.desc: Test RequestStrongAuth.
641 * @tc.type: FUNC
642 * @tc.require:
643 * @tc.author:
644 */
645 HWTEST_F(ScreenLockServiceTest, ScreenLockTest032, TestSize.Level0)
646 {
647     SCLOCK_HILOGD("Test RequestStrongAuth.");
648     int fd = 1;
649     std::vector<std::u16string> args = { u"arg1", u"arg2" };
650 
651     int result = ScreenLockSystemAbility::GetInstance()->Dump(fd, args);
652     EXPECT_EQ(result, ERR_OK);
653 }
654 
655 } // namespace ScreenLock
656 } // namespace OHOS