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: ScreenLockTest002
166 * @tc.desc: OnsystemEvent
167 * @tc.type: FUNC
168 * @tc.require:
169 * @tc.author:
170 */
171 HWTEST_F(ScreenLockServiceTest, ScreenLockTest002, TestSize.Level0)
172 {
173     SCLOCK_HILOGD("Test OnsystemEvent");
174     sptr<ScreenLockSystemAbilityInterface> listener = new (std::nothrow)
175         ScreenlockNotifyTestInstance(g_unlockTestListener);
176     ASSERT_NE(listener, nullptr);
177     ScreenLockSystemAbility::GetInstance()->OnSystemEvent(listener);
178     EXPECT_NE(ScreenLockSystemAbility::GetInstance()->systemEventListener_, nullptr);
179 }
180 
181 /**
182 * @tc.name: ScreenLockTest003
183 * @tc.desc: beginSleep event.
184 * @tc.type: FUNC
185 * @tc.require:
186 * @tc.author:
187 */
188 HWTEST_F(ScreenLockServiceTest, ScreenLockTest003, TestSize.Level0)
189 {
190     SCLOCK_HILOGD("Test event of beginsleep");
191     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
192     ScreenLockSystemAbility::GetInstance()->OnStart();
193     DisplayPowerEvent event = DisplayPowerEvent::SLEEP;
194     EventStatus status = EventStatus::BEGIN;
195     sptr<ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener> displayPowerEventListener = new (std::nothrow)
196         ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener();
197     ASSERT_NE(displayPowerEventListener, nullptr);
198     displayPowerEventListener->OnDisplayPowerEvent(event, status);
199     int retVal = ScreenLockSystemAbility::GetInstance()->GetState().GetInteractiveState();
200     SCLOCK_HILOGD("Test_BeginSleep retVal=%{public}d", retVal);
201     EXPECT_EQ(retVal, static_cast<int>(InteractiveState::INTERACTIVE_STATE_BEGIN_SLEEP));
202 }
203 
204 /**
205 * @tc.name: ScreenLockTest004
206 * @tc.desc: beginScreenOn event.
207 * @tc.type: FUNC
208 * @tc.require:
209 * @tc.author:
210 */
211 HWTEST_F(ScreenLockServiceTest, ScreenLockTest004, TestSize.Level0)
212 {
213     SCLOCK_HILOGD("Test event of beginscreenon");
214     ScreenLockSystemAbility::GetInstance();
215     DisplayPowerEvent event = DisplayPowerEvent::DISPLAY_ON;
216     EventStatus status = EventStatus::BEGIN;
217     sptr<ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener> displayPowerEventListener = new (std::nothrow)
218         ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener();
219     ASSERT_NE(displayPowerEventListener, nullptr);
220     displayPowerEventListener->OnDisplayPowerEvent(event, status);
221     int retVal = ScreenLockSystemAbility::GetInstance()->GetState().GetScreenState();
222     SCLOCK_HILOGD("Test_BeginScreenOn retVal=%{public}d", retVal);
223     EXPECT_EQ(retVal, static_cast<int>(ScreenState::SCREEN_STATE_BEGIN_ON));
224 }
225 
226 /**
227 * @tc.name: ScreenLockTest005
228 * @tc.desc: beginScreenOff event.
229 * @tc.type: FUNC
230 * @tc.require:
231 * @tc.author:
232 */
233 HWTEST_F(ScreenLockServiceTest, ScreenLockTest005, TestSize.Level0)
234 {
235     SCLOCK_HILOGD("Test event of beginscreenoff");
236     ScreenLockSystemAbility::GetInstance();
237     DisplayPowerEvent event = DisplayPowerEvent::DISPLAY_OFF;
238     EventStatus status = EventStatus::BEGIN;
239     sptr<ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener> displayPowerEventListener = new (std::nothrow)
240         ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener();
241     ASSERT_NE(displayPowerEventListener, nullptr);
242     displayPowerEventListener->OnDisplayPowerEvent(event, status);
243     int retVal = ScreenLockSystemAbility::GetInstance()->GetState().GetScreenState();
244     SCLOCK_HILOGD("Test_BeginScreenOff retVal=%{public}d", retVal);
245     EXPECT_EQ(retVal, static_cast<int>(ScreenState::SCREEN_STATE_BEGIN_OFF));
246 }
247 
248 /**
249 * @tc.name: ScreenLockTest006
250 * @tc.desc: endWakeUp event.
251 * @tc.type: FUNC
252 * @tc.require:
253 * @tc.author:
254 */
255 HWTEST_F(ScreenLockServiceTest, ScreenLockTest006, TestSize.Level0)
256 {
257     SCLOCK_HILOGD("Test event of endwakeup");
258     ScreenLockSystemAbility::GetInstance();
259     DisplayPowerEvent event = DisplayPowerEvent::WAKE_UP;
260     EventStatus status = EventStatus::END;
261     sptr<ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener> displayPowerEventListener = new (std::nothrow)
262         ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener();
263     ASSERT_NE(displayPowerEventListener, nullptr);
264     displayPowerEventListener->OnDisplayPowerEvent(event, status);
265     int retVal = ScreenLockSystemAbility::GetInstance()->GetState().GetInteractiveState();
266     SCLOCK_HILOGD("Test_EndWakeUp retVal=%{public}d", retVal);
267     EXPECT_EQ(retVal, static_cast<int>(InteractiveState::INTERACTIVE_STATE_END_WAKEUP));
268 }
269 
270 /**
271 * @tc.name: ScreenLockTest007
272 * @tc.desc: endSleep event.
273 * @tc.type: FUNC
274 * @tc.require:
275 * @tc.author:
276 */
277 HWTEST_F(ScreenLockServiceTest, ScreenLockTest007, TestSize.Level0)
278 {
279     SCLOCK_HILOGD("Test event of endsleep");
280     ScreenLockSystemAbility::GetInstance();
281     DisplayPowerEvent event = DisplayPowerEvent::SLEEP;
282     EventStatus status = EventStatus::END;
283     sptr<ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener> displayPowerEventListener = new (std::nothrow)
284         ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener();
285     ASSERT_NE(displayPowerEventListener, nullptr);
286     displayPowerEventListener->OnDisplayPowerEvent(event, status);
287     int retVal = ScreenLockSystemAbility::GetInstance()->GetState().GetInteractiveState();
288     SCLOCK_HILOGD("Test_EndSleep retVal=%{public}d", retVal);
289     EXPECT_EQ(retVal, static_cast<int>(InteractiveState::INTERACTIVE_STATE_END_SLEEP));
290 }
291 
292 /**
293 * @tc.name: ScreenLockTest008
294 * @tc.desc: endScreenOn event.
295 * @tc.type: FUNC
296 * @tc.require:
297 * @tc.author:
298 */
299 HWTEST_F(ScreenLockServiceTest, ScreenLockTest008, TestSize.Level0)
300 {
301     SCLOCK_HILOGD("Test event of endscreenon");
302     ScreenLockSystemAbility::GetInstance();
303     DisplayPowerEvent event = DisplayPowerEvent::DISPLAY_ON;
304     EventStatus status = EventStatus::END;
305     sptr<ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener> displayPowerEventListener = new (std::nothrow)
306         ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener();
307     ASSERT_NE(displayPowerEventListener, nullptr);
308     displayPowerEventListener->OnDisplayPowerEvent(event, status);
309     int retVal = ScreenLockSystemAbility::GetInstance()->GetState().GetScreenState();
310     SCLOCK_HILOGD("Test_EndScreenOn retVal=%{public}d", retVal);
311     EXPECT_EQ(retVal, static_cast<int>(ScreenState::SCREEN_STATE_END_ON));
312 }
313 
314 /**
315 * @tc.name: ScreenLockTest009
316 * @tc.desc: endScreenOff and begin desktopready event.
317 * @tc.type: FUNC
318 * @tc.require:
319 * @tc.author:
320 */
321 HWTEST_F(ScreenLockServiceTest, ScreenLockTest009, TestSize.Level0)
322 {
323     SCLOCK_HILOGD("Test event of endscreenoff");
324     ScreenLockSystemAbility::GetInstance();
325     DisplayPowerEvent event = DisplayPowerEvent::DISPLAY_OFF;
326     EventStatus status = EventStatus::END;
327     sptr<ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener> displayPowerEventListener = new (std::nothrow)
328         ScreenLockSystemAbility::ScreenLockDisplayPowerEventListener();
329     ASSERT_NE(displayPowerEventListener, nullptr);
330     displayPowerEventListener->OnDisplayPowerEvent(event, status);
331     event = DisplayPowerEvent::DESKTOP_READY;
332     status = EventStatus::BEGIN;
333     displayPowerEventListener->OnDisplayPowerEvent(event, status);
334     int retVal = ScreenLockSystemAbility::GetInstance()->GetState().GetScreenState();
335     SCLOCK_HILOGD("Test_EndScreenOff retVal=%{public}d", retVal);
336     EXPECT_EQ(retVal, static_cast<int>(ScreenState::SCREEN_STATE_END_OFF));
337 }
338 
339 /**
340 * @tc.name: ScreenLockDumperTest013
341 * @tc.desc: dump showhelp.
342 * @tc.type: FUNC
343 * @tc.require:
344 * @tc.author:
345 */
346 HWTEST_F(ScreenLockServiceTest, ScreenLockDumperTest013, TestSize.Level0)
347 {
348     SCLOCK_HILOGD("Test hidumper of showhelp");
349     std::string result;
350     auto ret = ScreenLockServiceTest::ExecuteCmd(CMD1, result);
351     EXPECT_TRUE(ret);
352     EXPECT_NE(result.find("Option"), std::string::npos);
353     EXPECT_NE(result.find("-all"), std::string::npos);
354 }
355 
356 /**
357 * @tc.name: ScreenLockDumperTest014
358 * @tc.desc: dump showhelp.
359 * @tc.type: FUNC
360 * @tc.require:
361 * @tc.author:
362 */
363 HWTEST_F(ScreenLockServiceTest, ScreenLockDumperTest014, TestSize.Level0)
364 {
365     SCLOCK_HILOGD("Test hidumper of -h");
366     std::string result;
367     auto ret = ScreenLockServiceTest::ExecuteCmd(CMD2, result);
368     EXPECT_TRUE(ret);
369     EXPECT_NE(result.find("Description"), std::string::npos);
370     EXPECT_NE(result.find("dump all screenlock information"), std::string::npos);
371 }
372 
373 /**
374 * @tc.name: ScreenLockDumperTest015
375 * @tc.desc: dump screenlock information.
376 * @tc.type: FUNC
377 * @tc.require:
378 * @tc.author:
379 */
380 HWTEST_F(ScreenLockServiceTest, ScreenLockDumperTest015, TestSize.Level0)
381 {
382     SCLOCK_HILOGD("Test hidumper of -all");
383     std::string result;
384     auto ret = ScreenLockServiceTest::ExecuteCmd(CMD3, result);
385     EXPECT_TRUE(ret);
386     EXPECT_NE(result.find("screenLocked"), std::string::npos);
387     EXPECT_NE(result.find("screenState"), std::string::npos);
388     EXPECT_NE(result.find("offReason"), std::string::npos);
389     EXPECT_NE(result.find("interactiveState"), std::string::npos);
390 }
391 
392 /**
393 * @tc.name: ScreenLockTest016
394 * @tc.desc: Test Lock.
395 * @tc.type: FUNC
396 * @tc.require:
397 * @tc.author:
398 */
399 HWTEST_F(ScreenLockServiceTest, ScreenLockTest016, TestSize.Level0)
400 {
401     SCLOCK_HILOGD("Test RequestLock");
402     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
403     sptr<ScreenLockCallbackInterface> listener = new (std::nothrow) ScreenlockCallbackTest(g_unlockTestListener);
404     ASSERT_NE(listener, nullptr);
405 
406     ScreenLockSystemAbility::GetInstance()->stateValue_.SetScreenlocked(true);
407     bool isLocked = ScreenLockSystemAbility::GetInstance()->IsScreenLocked();
408     EXPECT_EQ(isLocked, true);
409     int32_t result = ScreenLockSystemAbility::GetInstance()->Lock(listener);
410     EXPECT_EQ(result, E_SCREENLOCK_OK);
411     ScreenLockSystemAbility::GetInstance()->stateValue_.SetScreenlocked(false);
412     result = ScreenLockSystemAbility::GetInstance()->Lock(listener);
413     EXPECT_EQ(result, E_SCREENLOCK_OK);
414 }
415 
416 /**
417 * @tc.name: ScreenLockTest017
418 * @tc.desc: Test Unlock and UnlockScreen.
419 * @tc.type: FUNC
420 * @tc.require:
421 * @tc.author:
422 */
423 HWTEST_F(ScreenLockServiceTest, ScreenLockTest017, TestSize.Level0)
424 {
425     SCLOCK_HILOGD("Test RequestUnlock");
426     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
427     sptr<ScreenLockCallbackInterface> listener = new (std::nothrow) ScreenlockCallbackTest(g_unlockTestListener);
428     ASSERT_NE(listener, nullptr);
429     int32_t result = ScreenLockSystemAbility::GetInstance()->UnlockScreen(listener);
430     EXPECT_EQ(result, E_SCREENLOCK_NOT_FOCUS_APP);
431     result = ScreenLockSystemAbility::GetInstance()->Unlock(listener);
432     EXPECT_EQ(result, E_SCREENLOCK_NOT_FOCUS_APP);
433     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
434     result = ScreenLockSystemAbility::GetInstance()->Unlock(listener);
435     EXPECT_EQ(result, E_SCREENLOCK_NOT_FOCUS_APP);
436 }
437 
438 /**
439 * @tc.name: ScreenLockTest018
440 * @tc.desc: Test SendScreenLockEvent.
441 * @tc.type: FUNC
442 * @tc.require:
443 * @tc.author:
444 */
445 HWTEST_F(ScreenLockServiceTest, ScreenLockTest018, TestSize.Level0)
446 {
447     SCLOCK_HILOGD("Test SendScreenLockEvent");
448     ScreenLockSystemAbility::GetInstance()->SendScreenLockEvent(UNLOCK_SCREEN_RESULT, SCREEN_SUCC);
449     bool isLocked = ScreenLockSystemAbility::GetInstance()->IsScreenLocked();
450     EXPECT_EQ(isLocked, false);
451 }
452 
453 /**
454 * @tc.name: ScreenLockTest019
455 * @tc.desc: Test SendScreenLockEvent.
456 * @tc.type: FUNC
457 * @tc.require:
458 * @tc.author:
459 */
460 HWTEST_F(ScreenLockServiceTest, ScreenLockTest019, TestSize.Level0)
461 {
462     SCLOCK_HILOGD("Test SendScreenLockEvent");
463     ScreenLockSystemAbility::GetInstance()->SendScreenLockEvent(UNLOCK_SCREEN_RESULT, SCREEN_FAIL);
464     bool isLocked = ScreenLockSystemAbility::GetInstance()->IsScreenLocked();
465     EXPECT_EQ(isLocked, false);
466 }
467 
468 /**
469 * @tc.name: ScreenLockTest020
470 * @tc.desc: Test SendScreenLockEvent.
471 * @tc.type: FUNC
472 * @tc.require:
473 * @tc.author:
474 */
475 HWTEST_F(ScreenLockServiceTest, ScreenLockTest020, TestSize.Level0)
476 {
477     SCLOCK_HILOGD("Test SendScreenLockEvent");
478     ScreenLockSystemAbility::GetInstance()->SendScreenLockEvent(UNLOCK_SCREEN_RESULT, SCREEN_CANCEL);
479     bool isLocked = ScreenLockSystemAbility::GetInstance()->IsScreenLocked();
480     EXPECT_EQ(isLocked, false);
481 }
482 
483 /**
484 * @tc.name: ScreenLockTest021
485 * @tc.desc: Test SendScreenLockEvent.
486 * @tc.type: FUNC
487 * @tc.require:
488 * @tc.author:
489 */
490 HWTEST_F(ScreenLockServiceTest, ScreenLockTest021, TestSize.Level0)
491 {
492     SCLOCK_HILOGD("Test SendScreenLockEvent");
493     ScreenLockSystemAbility::GetInstance()->SendScreenLockEvent(LOCK_SCREEN_RESULT, SCREEN_SUCC);
494     bool isLocked;
495     ScreenLockSystemAbility::GetInstance()->IsLocked(isLocked);
496     EXPECT_EQ(isLocked, true);
497 }
498 
499 /**
500 * @tc.name: ScreenLockTest022
501 * @tc.desc: Test SendScreenLockEvent.
502 * @tc.type: FUNC
503 * @tc.require:
504 * @tc.author:
505 */
506 HWTEST_F(ScreenLockServiceTest, ScreenLockTest022, TestSize.Level0)
507 {
508     SCLOCK_HILOGD("Test SendScreenLockEvent");
509     ScreenLockSystemAbility::GetInstance()->SendScreenLockEvent(LOCK_SCREEN_RESULT, SCREEN_FAIL);
510     bool isLocked;
511     ScreenLockSystemAbility::GetInstance()->IsLocked(isLocked);
512     EXPECT_EQ(isLocked, true);
513 }
514 
515 /**
516 * @tc.name: ScreenLockTest023
517 * @tc.desc: Test SendScreenLockEvent.
518 * @tc.type: FUNC
519 * @tc.require:
520 * @tc.author:
521 */
522 HWTEST_F(ScreenLockServiceTest, ScreenLockTest023, TestSize.Level0)
523 {
524     SCLOCK_HILOGD("Test SendScreenLockEvent");
525     ScreenLockSystemAbility::GetInstance()->SendScreenLockEvent(SCREEN_DRAWDONE, SCREEN_SUCC);
526     ScreenLockSystemAbility::GetInstance()->SendScreenLockEvent(LOCK_SCREEN_RESULT, SCREEN_CANCEL);
527     bool isLocked;
528     ScreenLockSystemAbility::GetInstance()->IsLocked(isLocked);
529     EXPECT_EQ(isLocked, true);
530 }
531 
532 /**
533 * @tc.name: ScreenLockTest025
534 * @tc.desc: Test Onstop and OnStart.
535 * @tc.type: FUNC
536 * @tc.require:
537 * @tc.author:
538 */
539 HWTEST_F(ScreenLockServiceTest, ScreenLockTest025, TestSize.Level0)
540 {
541     SCLOCK_HILOGD("Test Onstop");
542     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
543     ScreenLockSystemAbility::GetInstance()->OnStart();
544     ScreenLockSystemAbility::GetInstance()->OnStop();
545     ScreenLockSystemAbility::GetInstance()->OnStart();
546     EXPECT_EQ(ScreenLockSystemAbility::GetInstance()->state_, ServiceRunningState::STATE_NOT_START);
547     int times = 0;
548     ScreenLockSystemAbility::GetInstance()->RegisterDisplayPowerEventListener(times);
549     bool isLocked;
550     ScreenLockSystemAbility::GetInstance()->IsLocked(isLocked);
551     SCLOCK_HILOGD("Test_SendScreenLockEvent of screendrawdone isLocked=%{public}d", isLocked);
552     EXPECT_EQ(isLocked, false);
553 }
554 
555 /**
556 * @tc.name: ScreenLockTest026
557 * @tc.desc: Test GetSecure.
558 * @tc.type: FUNC
559 * @tc.require:
560 * @tc.author:
561 */
562 HWTEST_F(ScreenLockServiceTest, ScreenLockTest026, TestSize.Level0)
563 {
564     SCLOCK_HILOGD("Test GetSecure.");
565     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
566     bool ret = ScreenLockSystemAbility::GetInstance()->GetSecure();
567     EXPECT_EQ(ret, false);
568 }
569 
570 /**
571 * @tc.name: ScreenLockTest027
572 * @tc.desc: Test UnlockScreenEvent.
573 * @tc.type: FUNC
574 * @tc.require:
575 * @tc.author:
576 */
577 HWTEST_F(ScreenLockServiceTest, ScreenLockTest027, TestSize.Level0)
578 {
579     SCLOCK_HILOGD("Test UnlockScreenEvent.");
580     ScreenLockSystemAbility::GetInstance()->unlockVecListeners_.clear();
581     ScreenLockSystemAbility::GetInstance()->UnlockScreenEvent(SCREEN_CANCEL);
582     bool isLocked;
583     ScreenLockSystemAbility::GetInstance()->IsLocked(isLocked);
584     EXPECT_EQ(isLocked, false);
585 }
586 
587 /**
588 * @tc.name: LockTest028
589 * @tc.desc: Test Lock Screen.
590 * @tc.type: FUNC
591 * @tc.require:
592 * @tc.author:
593 */
594 HWTEST_F(ScreenLockServiceTest, LockTest028, TestSize.Level0)
595 {
596     SCLOCK_HILOGD("Test RequestLock.");
597     int32_t userId = 0;
598     int32_t result = ScreenLockSystemAbility::GetInstance()->Lock(userId);
599     EXPECT_EQ(result, E_SCREENLOCK_OK);
600 }
601 
602 /**
603 * @tc.name: ScreenLockTest029
604 * @tc.desc: Test SetScreenLockDisabled.
605 * @tc.type: FUNC
606 * @tc.require:
607 * @tc.author:
608 */
609 HWTEST_F(ScreenLockServiceTest, ScreenLockTest029, TestSize.Level0)
610 {
611     SCLOCK_HILOGD("Test SetScreenLockDisabled.");
612     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
613     int userId = 0;
614     int32_t ret = ScreenLockSystemAbility::GetInstance()->SetScreenLockDisabled(false, userId);
615     bool disable = true;
616     ScreenLockSystemAbility::GetInstance()->IsScreenLockDisabled(userId, disable);
617     SCLOCK_HILOGD("SetScreenLockDisabled.[ret]:%{public}d, [disable]:%{public}d", ret, disable);
618 }
619 
620 /**
621 * @tc.name: ScreenLockTest030
622 * @tc.desc: Test SetScreenLockAuthState.
623 * @tc.type: FUNC
624 * @tc.require:
625 * @tc.author:
626 */
627 HWTEST_F(ScreenLockServiceTest, ScreenLockTest030, TestSize.Level0)
628 {
629     SCLOCK_HILOGD("Test SetScreenLockAuthState.");
630     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
631     int userId = 0;
632     std::string authtoken = "test";
633     int32_t ret = ScreenLockSystemAbility::GetInstance()->SetScreenLockAuthState(1, userId, authtoken);
634     SCLOCK_HILOGD("SetScreenLockAuthState.[ret]:%{public}d", ret);
635 
636     int32_t authState = 0;
637     ScreenLockSystemAbility::GetInstance()->GetScreenLockAuthState(userId, authState);
638 }
639 
640 /**
641 * @tc.name: ScreenLockTest031
642 * @tc.desc: Test RequestStrongAuth.
643 * @tc.type: FUNC
644 * @tc.require:
645 * @tc.author:
646 */
647 HWTEST_F(ScreenLockServiceTest, ScreenLockTest031, TestSize.Level0)
648 {
649     SCLOCK_HILOGD("Test RequestStrongAuth.");
650     ScreenLockSystemAbility::GetInstance()->state_ = ServiceRunningState::STATE_NOT_START;
651     int32_t userId = 0;
652     int reasonFlag = 1;
653     int32_t ret = ScreenLockSystemAbility::GetInstance()->RequestStrongAuth(reasonFlag, userId);
654 
655     ret = ScreenLockSystemAbility::GetInstance()->GetStrongAuth(userId, reasonFlag);
656 
657     EXPECT_EQ(ret, E_SCREENLOCK_OK);
658     EXPECT_EQ(reasonFlag, 1);
659 }
660 
661 } // namespace ScreenLock
662 } // namespace OHOS