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_app_manager.h"
18 #include "screenlock_manager.h"
19 #undef private
20 #undef protected
21
22 #include <cstdint>
23 #include <list>
24 #include <string>
25 #include <sys/time.h>
26
27 #include "sclock_log.h"
28 #include "screenlock_callback_test.h"
29 #include "screenlock_client_test.h"
30 #include "screenlock_common.h"
31 #include "screenlock_event_list_test.h"
32 #include "screenlock_notify_test_instance.h"
33 #include "screenlock_system_ability.h"
34 #include "securec.h"
35
36 namespace OHOS {
37 namespace ScreenLock {
38 using namespace testing::ext;
39
40 static EventListenerTest g_unlockTestListener;
41
SetUpTestCase()42 void ScreenLockClientTest::SetUpTestCase()
43 {
44 }
45
TearDownTestCase()46 void ScreenLockClientTest::TearDownTestCase()
47 {
48 }
49
SetUp()50 void ScreenLockClientTest::SetUp()
51 {
52 }
53
TearDown()54 void ScreenLockClientTest::TearDown()
55 {
56 }
57
58 /**
59 * @tc.name: SetScreenLockTest001
60 * @tc.desc: set screen state and get state of the screen.
61 * @tc.type: FUNC
62 * @tc.require:
63 * @tc.author:
64 */
65 HWTEST_F(ScreenLockClientTest, SetScreenLockTest001, TestSize.Level0)
66 {
67 SCLOCK_HILOGD("Test IsScreenLocked state");
68 ScreenLockSystemAbility::GetInstance()->SetScreenlocked(true);
69 bool isLocked = ScreenLockSystemAbility::GetInstance()->IsScreenLocked();
70 EXPECT_EQ(isLocked, true);
71 }
72
73 /**
74 * @tc.name: GetSecureTest002
75 * @tc.desc: get secure.
76 * @tc.type: FUNC
77 * @tc.require:
78 * @tc.author:
79 */
80 HWTEST_F(ScreenLockClientTest, GetSecureTest002, TestSize.Level0)
81 {
82 SCLOCK_HILOGD("Test secure");
83 bool result = ScreenLockManager::GetInstance()->GetSecure();
84 SCLOCK_HILOGD(" result is-------->%{public}d", result);
85 EXPECT_EQ(result, false);
86 }
87
88 /**
89 * @tc.name: LockTest003
90 * @tc.desc: Test Lock and Unlock
91 * @tc.type: FUNC
92 * @tc.require:
93 * @tc.author:
94 */
95 HWTEST_F(ScreenLockClientTest, LockTest003, TestSize.Level0)
96 {
97 SCLOCK_HILOGD("Test RequestLock and RequestUnlock");
98 sptr<ScreenLockCallbackInterface> listener = nullptr;
99 int32_t result = ScreenLockManager::GetInstance()->Lock(listener);
100 EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
101 result = ScreenLockManager::GetInstance()->Unlock(Action::UNLOCK, listener);
102 EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
103 result = ScreenLockManager::GetInstance()->Unlock(Action::UNLOCKSCREEN, listener);
104 EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
105 listener = new (std::nothrow) ScreenlockCallbackTest(g_unlockTestListener);
106 ASSERT_NE(listener, nullptr);
107 result = ScreenLockManager::GetInstance()->Lock(listener);
108 EXPECT_EQ(result, E_SCREENLOCK_NOT_SYSTEM_APP);
109 result = ScreenLockManager::GetInstance()->Unlock(Action::UNLOCK, listener);
110 EXPECT_EQ(result, E_SCREENLOCK_NOT_SYSTEM_APP);
111 result = ScreenLockManager::GetInstance()->Unlock(Action::UNLOCKSCREEN, listener);
112 EXPECT_EQ(result, E_SCREENLOCK_NO_PERMISSION);
113 }
114
115 /**
116 * @tc.name: OnSystemEventTest004
117 * @tc.desc: Test OnSystemEvent.
118 * @tc.type: FUNC
119 * @tc.require:
120 * @tc.author:
121 */
122 HWTEST_F(ScreenLockClientTest, OnSystemEventTest004, TestSize.Level0)
123 {
124 SCLOCK_HILOGD("Test OnSystemEvent");
125 sptr<ScreenLockSystemAbilityInterface> listener = new (std::nothrow)
126 ScreenLockSystemAbilityTest(g_unlockTestListener);
127 ASSERT_NE(listener, nullptr);
128 int32_t result = ScreenLockAppManager::GetInstance()->OnSystemEvent(listener);
129 EXPECT_EQ(result, E_SCREENLOCK_NOT_SYSTEM_APP);
130 }
131
132 /**
133 * @tc.name: SendScreenLockEventTest005
134 * @tc.desc: Test SendScreenLockEvent.
135 * @tc.type: FUNC
136 * @tc.require:
137 * @tc.author:
138 */
139 HWTEST_F(ScreenLockClientTest, SendScreenLockEventTest005, TestSize.Level0)
140 {
141 SCLOCK_HILOGD("Test SendScreenLockEvent");
142 int testNum = 0;
143 int32_t result = ScreenLockAppManager::GetInstance()->SendScreenLockEvent("test", testNum);
144 EXPECT_EQ(result, E_SCREENLOCK_NOT_SYSTEM_APP);
145 }
146
147 /**
148 * @tc.name: OnSystemEventTest006
149 * @tc.desc: Test OnSystemEvent.
150 * @tc.type: FUNC
151 * @tc.require:
152 * @tc.author:
153 */
154 HWTEST_F(ScreenLockClientTest, OnSystemEventTest006, TestSize.Level0)
155 {
156 SCLOCK_HILOGD("Test OnSystemEvent");
157 sptr<ScreenLockSystemAbilityInterface> listener = nullptr;
158 int32_t result = ScreenLockAppManager::GetInstance()->OnSystemEvent(listener);
159 EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
160 }
161
162 /**
163 * @tc.name: GetProxyTest007
164 * @tc.desc: Test GetProxy.
165 * @tc.type: FUNC
166 * @tc.require:
167 * @tc.author:
168 */
169 HWTEST_F(ScreenLockClientTest, GetProxyTest007, TestSize.Level0)
170 {
171 SCLOCK_HILOGD("Test GetProxy");
172 ScreenLockAppManager::GetInstance()->screenlockManagerProxy_ = nullptr;
173 sptr<ScreenLockManagerInterface> proxy = ScreenLockAppManager::GetInstance()->GetProxy();
174 EXPECT_NE(proxy, nullptr);
175 ScreenLockManager::GetInstance()->screenlockManagerProxy_ = nullptr;
176 proxy = nullptr;
177 proxy = ScreenLockManager::GetInstance()->GetProxy();
178 EXPECT_NE(proxy, nullptr);
179 }
180
181 /**
182 * @tc.name: ProxyTest008
183 * @tc.desc: Test Lock, UnLock and OnSystemEvent.
184 * @tc.type: FUNC
185 * @tc.require:
186 * @tc.author:
187 */
188 HWTEST_F(ScreenLockClientTest, ProxyTest008, TestSize.Level0)
189 {
190 SCLOCK_HILOGD("Test RequestLock, RequestUnLock and OnSystemEvent.");
191 auto proxy = ScreenLockAppManager::GetInstance()->GetProxy();
192 sptr<ScreenLockSystemAbilityInterface> listener = nullptr;
193 int32_t result = proxy->OnSystemEvent(listener);
194 EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
195 sptr<ScreenLockCallbackInterface> callback = nullptr;
196 result = proxy->Lock(callback);
197 EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
198 result = proxy->Unlock(callback);
199 EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
200 result = proxy->Unlock(callback);
201 EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
202 }
203 } // namespace ScreenLock
204 } // namespace OHOS