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