• 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_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: get unlockstate, IsScreenLocked state.
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: RequestLockTest003
90 * @tc.desc: Test RequestLock and RequestUnlock
91 * @tc.type: FUNC
92 * @tc.require:
93 * @tc.author:
94 */
95 HWTEST_F(ScreenLockClientTest, RequestLockTest003, TestSize.Level0)
96 {
97     SCLOCK_HILOGD("Test RequestLock and RequestUnlock");
98     sptr<ScreenLockSystemAbilityInterface> 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 
106     listener = new (std::nothrow) ScreenlockCallbackTest(g_unlockTestListener);
107     if (listener == nullptr) {
108         SCLOCK_HILOGE("listener object is nullptr");
109         EXPECT_EQ(false, true);
110         return;
111     }
112     result = ScreenLockManager::GetInstance()->Lock(listener);
113     EXPECT_EQ(result, E_SCREENLOCK_NO_PERMISSION);
114     result = ScreenLockManager::GetInstance()->Unlock(Action::UNLOCK, listener);
115     EXPECT_EQ(result, E_SCREENLOCK_NO_PERMISSION);
116     result = ScreenLockManager::GetInstance()->Unlock(Action::UNLOCKSCREEN, listener);
117     EXPECT_EQ(result, E_SCREENLOCK_NO_PERMISSION);
118 }
119 
120 /**
121 * @tc.name: OnSystemEventTest004
122 * @tc.desc: Test OnSystemEvent.
123 * @tc.type: FUNC
124 * @tc.require:
125 * @tc.author:
126 */
127 HWTEST_F(ScreenLockClientTest, OnSystemEventTest004, TestSize.Level0)
128 {
129     SCLOCK_HILOGD("Test OnSystemEvent");
130     sptr<ScreenLockSystemAbilityInterface> listener = new (std::nothrow) ScreenlockCallbackTest(g_unlockTestListener);
131     int32_t result = ScreenLockAppManager::GetInstance()->OnSystemEvent(listener);
132     EXPECT_EQ(result, E_SCREENLOCK_NO_PERMISSION);
133 }
134 
135 /**
136 * @tc.name: SendScreenLockEventTest005
137 * @tc.desc: Test SendScreenLockEvent.
138 * @tc.type: FUNC
139 * @tc.require:
140 * @tc.author:
141 */
142 HWTEST_F(ScreenLockClientTest, SendScreenLockEventTest005, TestSize.Level0)
143 {
144     SCLOCK_HILOGD("Test SendScreenLockEvent");
145     int testNum = 0;
146     int32_t result = ScreenLockAppManager::GetInstance()->SendScreenLockEvent("test", testNum);
147     EXPECT_EQ(result, E_SCREENLOCK_NO_PERMISSION);
148 }
149 
150 /**
151 * @tc.name: OnSystemEventTest006
152 * @tc.desc: Test OnSystemEvent.
153 * @tc.type: FUNC
154 * @tc.require:
155 * @tc.author:
156 */
157 HWTEST_F(ScreenLockClientTest, OnSystemEventTest006, TestSize.Level0)
158 {
159     SCLOCK_HILOGD("Test OnSystemEvent");
160     sptr<ScreenLockSystemAbilityInterface> listener = nullptr;
161     int32_t result = ScreenLockAppManager::GetInstance()->OnSystemEvent(listener);
162     EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
163 }
164 
165 /**
166 * @tc.name: GetProxyTest007
167 * @tc.desc: Test GetProxy.
168 * @tc.type: FUNC
169 * @tc.require:
170 * @tc.author:
171 */
172 HWTEST_F(ScreenLockClientTest, GetProxyTest007, TestSize.Level0)
173 {
174     SCLOCK_HILOGD("Test GetProxy");
175     ScreenLockAppManager::GetInstance()->screenlockManagerProxy_ = nullptr;
176     sptr<ScreenLockManagerInterface> proxy = ScreenLockAppManager::GetInstance()->GetProxy();
177     EXPECT_NE(proxy, nullptr);
178     ScreenLockManager::GetInstance()->screenlockManagerProxy_ = nullptr;
179     proxy = nullptr;
180     proxy = ScreenLockManager::GetInstance()->GetProxy();
181     EXPECT_NE(proxy, nullptr);
182 }
183 
184 /**
185 * @tc.name: ProxyTest008
186 * @tc.desc: Test RequestLock, RequestUnLock and OnSystemEvent.
187 * @tc.type: FUNC
188 * @tc.require:
189 * @tc.author:
190 */
191 HWTEST_F(ScreenLockClientTest, ProxyTest008, TestSize.Level0)
192 {
193     SCLOCK_HILOGD("Test RequestLock, RequestUnLock and OnSystemEvent.");
194     auto proxy = ScreenLockAppManager::GetInstance()->GetProxy();
195     sptr<ScreenLockSystemAbilityInterface> listener = nullptr;
196     int32_t result = proxy->OnSystemEvent(listener);
197     EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
198     result = proxy->Lock(listener);
199     EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
200     result = proxy->Unlock(listener);
201     EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
202 }
203 } // namespace ScreenLock
204 } // namespace OHOS