1 /*
2 * Copyright (C) 2025 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
16 #define private public
17 #define protected public
18 #include "numkey_apps_manager.h"
19
20 #include "ime_info_inquirer.h"
21
22 #undef private
23
24 #include <gtest/gtest.h>
25
26 using namespace testing::ext;
27 namespace OHOS {
28 namespace MiscServices {
29 class NumKeyAppsManagerTest : public testing::Test {
30 public:
31 static void SetUpTestCase(void);
32 static void TearDownTestCase(void);
33 void SetUp();
34 void TearDown();
35
36 private:
37 };
38
39 constexpr std::int32_t MAIN_USER_ID = 100;
40 constexpr std::int32_t INVALID_USER_ID = 10001;
41 static constexpr const char *WHITE_LIST_APP_NAME = "WHITE_LIST_APP_NAME";
42 static constexpr const char *BLOCK_LIST_APP_NAME = "BLOCK_LIST_APP_NAME";
43 static constexpr const char *DEFAULT_DEVICETYPE = "default";
44
SetUpTestCase(void)45 void NumKeyAppsManagerTest::SetUpTestCase(void)
46 {
47 IMSA_HILOGI("NumKeyAppsManagerTest::SetUpTestCase");
48 }
49
TearDownTestCase(void)50 void NumKeyAppsManagerTest::TearDownTestCase(void)
51 {
52 IMSA_HILOGI("NumKeyAppsManagerTest::TearDownTestCase");
53 }
54
SetUp()55 void NumKeyAppsManagerTest::SetUp()
56 {
57 IMSA_HILOGI("NumKeyAppsManagerTest::SetUp");
58 }
59
TearDown()60 void NumKeyAppsManagerTest::TearDown()
61 {
62 IMSA_HILOGI("NumKeyAppsManagerTest::TearDown");
63 }
64
65 /**
66 * @tc.name: testFeatureNotEnabled_001
67 * @tc.desc: when feature not enabled
68 * @tc.type: FUNC
69 */
70 HWTEST_F(NumKeyAppsManagerTest, testFeatureNotEnabled_001, TestSize.Level1)
71 {
72 IMSA_HILOGI("NumKeyAppsManagerTest testFeatureNotEnabled_001 START");
73 NumkeyAppsManager::GetInstance().isFeatureEnabled_ = false;
74 ImeInfoInquirer::GetInstance().systemConfig_.enableNumKeyFeature = false;
75 int32_t ret = NumkeyAppsManager::GetInstance().Init(MAIN_USER_ID);
76 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
77 EXPECT_EQ(NumkeyAppsManager::GetInstance().usersBlockList_.find(MAIN_USER_ID),
78 NumkeyAppsManager::GetInstance().usersBlockList_.end());
79 ret = NumkeyAppsManager::GetInstance().NeedAutoNumKeyInput(MAIN_USER_ID, WHITE_LIST_APP_NAME);
80 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
81 ret = NumkeyAppsManager::GetInstance().OnUserSwitched(MAIN_USER_ID);
82 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
83 ret = NumkeyAppsManager::GetInstance().OnUserRemoved(MAIN_USER_ID);
84 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
85 }
86
87 /**
88 * @tc.name: testInit_001
89 * @tc.desc: test init when feature enabled
90 * @tc.type: FUNC
91 */
92 HWTEST_F(NumKeyAppsManagerTest, testInit_001, TestSize.Level1)
93 {
94 IMSA_HILOGI("NumKeyAppsManagerTest testInit_001 START");
95 NumkeyAppsManager::GetInstance().isFeatureEnabled_ = true;
96 ImeInfoInquirer::GetInstance().systemConfig_.enableNumKeyFeature = true;
97 NumkeyAppsManager::GetInstance().disableNumKeyAppDeviceTypes_.clear();
98 ImeInfoInquirer::GetInstance().systemConfig_.disableNumKeyAppDeviceTypes.clear();
99 ImeInfoInquirer::GetInstance().systemConfig_.disableNumKeyAppDeviceTypes.insert(DEFAULT_DEVICETYPE);
100 int32_t ret = NumkeyAppsManager::GetInstance().Init(MAIN_USER_ID);
101 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
102 EXPECT_EQ(NumkeyAppsManager::GetInstance().disableNumKeyAppDeviceTypes_.count(DEFAULT_DEVICETYPE), 1);
103 EXPECT_NE(NumkeyAppsManager::GetInstance().usersBlockList_.find(MAIN_USER_ID),
104 NumkeyAppsManager::GetInstance().usersBlockList_.end());
105 }
106
107 /**
108 * @tc.name: testNeedAutoNumKeyInput_001
109 * @tc.desc: test NeedAutoNumKeyInput when app in usersBlockList_, or app not in usersBlockList_ but in numKeyAppList_
110 * @tc.type: FUNC
111 */
112 HWTEST_F(NumKeyAppsManagerTest, testNeedAutoNumKeyInput_001, TestSize.Level1)
113 {
114 IMSA_HILOGI("NumKeyAppsManagerTest testNeedAutoNumKeyInput_001 START");
115 NumkeyAppsManager::GetInstance().isFeatureEnabled_ = true;
116 NumkeyAppsManager::GetInstance().disableNumKeyAppDeviceTypes_.clear();
117 NumkeyAppsManager::GetInstance().disableNumKeyAppDeviceTypes_.insert(DEFAULT_DEVICETYPE);
118
119 NumkeyAppsManager::GetInstance().numKeyAppList_.clear();
120 NumkeyAppsManager::GetInstance().usersBlockList_.clear();
121 NumkeyAppsManager::GetInstance().usersBlockList_[MAIN_USER_ID] = { WHITE_LIST_APP_NAME };
122 bool ret = NumkeyAppsManager::GetInstance().NeedAutoNumKeyInput(MAIN_USER_ID, WHITE_LIST_APP_NAME);
123 EXPECT_FALSE(ret);
124
125 NumkeyAppsManager::GetInstance().numKeyAppList_.insert(WHITE_LIST_APP_NAME);
126 ret = NumkeyAppsManager::GetInstance().NeedAutoNumKeyInput(MAIN_USER_ID, WHITE_LIST_APP_NAME);
127 EXPECT_FALSE(ret);
128
129 NumkeyAppsManager::GetInstance().usersBlockList_.clear();
130 ret = NumkeyAppsManager::GetInstance().NeedAutoNumKeyInput(MAIN_USER_ID, WHITE_LIST_APP_NAME);
131 EXPECT_TRUE(ret);
132
133 NumkeyAppsManager::GetInstance().numKeyAppList_.clear();
134 NumkeyAppsManager::GetInstance().disableNumKeyAppDeviceTypes_.clear();
135 }
136
137 /**
138 * @tc.name: testNeedAutoNumKeyInput_002
139 * @tc.desc: test NeedAutoNumKeyInput when app not in numKeyAppList_ and usersBlockList_
140 * @tc.type: FUNC
141 */
142 HWTEST_F(NumKeyAppsManagerTest, testNeedAutoNumKeyInput_002, TestSize.Level1)
143 {
144 IMSA_HILOGI("NumKeyAppsManagerTest testNeedAutoNumKeyInput_002 START");
145 NumkeyAppsManager::GetInstance().isFeatureEnabled_ = true;
146 NumkeyAppsManager::GetInstance().numKeyAppList_.clear();
147 NumkeyAppsManager::GetInstance().usersBlockList_.clear();
148
149 NumkeyAppsManager::GetInstance().disableNumKeyAppDeviceTypes_.clear();
150 bool ret = NumkeyAppsManager::GetInstance().NeedAutoNumKeyInput(MAIN_USER_ID, WHITE_LIST_APP_NAME);
151 EXPECT_FALSE(ret);
152
153 NumkeyAppsManager::GetInstance().disableNumKeyAppDeviceTypes_.insert(DEFAULT_DEVICETYPE);
154 ret = NumkeyAppsManager::GetInstance().NeedAutoNumKeyInput(MAIN_USER_ID, WHITE_LIST_APP_NAME);
155 EXPECT_FALSE(ret);
156
157 NumkeyAppsManager::GetInstance().disableNumKeyAppDeviceTypes_.clear();
158 }
159
160 /**
161 * @tc.name: testOnUserSwitched_001
162 * @tc.desc: already inited, no need to update when user switch
163 * @tc.type: FUNC
164 */
165 HWTEST_F(NumKeyAppsManagerTest, testOnUserSwitched_001, TestSize.Level1)
166 {
167 IMSA_HILOGI("NumKeyAppsManagerTest testOnUserSwitched_001 START");
168 NumkeyAppsManager::GetInstance().isFeatureEnabled_ = true;
169 NumkeyAppsManager::GetInstance().usersBlockList_[INVALID_USER_ID] = { BLOCK_LIST_APP_NAME };
170 auto ret = NumkeyAppsManager::GetInstance().OnUserSwitched(INVALID_USER_ID);
171 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
172 EXPECT_EQ(NumkeyAppsManager::GetInstance().usersBlockList_[INVALID_USER_ID].size(), 0);
173 EXPECT_EQ(NumkeyAppsManager::GetInstance().usersBlockList_[INVALID_USER_ID].count(BLOCK_LIST_APP_NAME), 0);
174 }
175
176 /**
177 * @tc.name: testOnUserSwitched_002
178 * @tc.desc: update usersblocklist with user not inited when user switched
179 * @tc.type: FUNC
180 */
181 HWTEST_F(NumKeyAppsManagerTest, testOnUserSwitched_002, TestSize.Level1)
182 {
183 IMSA_HILOGI("NumKeyAppsManagerTest testOnUserSwitched_002 START");
184 NumkeyAppsManager::GetInstance().isFeatureEnabled_ = true;
185 NumkeyAppsManager::GetInstance().usersBlockList_.clear();
186 auto ret = NumkeyAppsManager::GetInstance().OnUserSwitched(MAIN_USER_ID);
187 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
188 EXPECT_NE(NumkeyAppsManager::GetInstance().usersBlockList_.find(MAIN_USER_ID),
189 NumkeyAppsManager::GetInstance().usersBlockList_.end());
190 }
191
192 /**
193 * @tc.name: testOnUserSwitched_003
194 * @tc.desc: test usersBlockList_ not empty after OnUserSwitched
195 * @tc.type: FUNC
196 */
197 HWTEST_F(NumKeyAppsManagerTest, testOnUserSwitched_003, TestSize.Level1)
198 {
199 IMSA_HILOGI("NumKeyAppsManagerTest testOnUserSwitched_003 START");
200 NumkeyAppsManager::GetInstance().usersBlockList_.clear();
201 auto ret = NumkeyAppsManager::GetInstance().OnUserSwitched(MAIN_USER_ID);
202 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
203 bool result = NumkeyAppsManager::GetInstance().usersBlockList_.find(MAIN_USER_ID)
204 != NumkeyAppsManager::GetInstance().usersBlockList_.end();
205 EXPECT_TRUE(result);
206 }
207
208 /**
209 * @tc.name: testOnUserRemoved_001
210 * @tc.desc: user removed when observers empty
211 * @tc.type: FUNC
212 */
213 HWTEST_F(NumKeyAppsManagerTest, testOnUserRemoved_001, TestSize.Level1)
214 {
215 IMSA_HILOGI("NumKeyAppsManagerTest testOnUserRemoved_001 START");
216 NumkeyAppsManager::GetInstance().isFeatureEnabled_ = true;
217 NumkeyAppsManager::GetInstance().observers_.clear();
218 auto ret = NumkeyAppsManager::GetInstance().OnUserRemoved(MAIN_USER_ID);
219 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
220 }
221
222 /**
223 * @tc.name: testOnUserRemoved_002
224 * @tc.desc: observers_ not empty, remove valid user
225 * @tc.type: FUNC
226 */
227 HWTEST_F(NumKeyAppsManagerTest, testOnUserRemoved_002, TestSize.Level1)
228 {
229 IMSA_HILOGI("NumKeyAppsManagerTest testOnUserRemoved_002 START");
230 NumkeyAppsManager::GetInstance().isFeatureEnabled_ = true;
231 sptr<SettingsDataObserver> observer = new (std::nothrow) SettingsDataObserver("", "", nullptr);
232 ASSERT_TRUE(observer != nullptr);
233 NumkeyAppsManager::GetInstance().observers_.clear();
234 NumkeyAppsManager::GetInstance().observers_[MAIN_USER_ID] = observer;
235 NumkeyAppsManager::GetInstance().OnUserRemoved(MAIN_USER_ID);
236 EXPECT_EQ(NumkeyAppsManager::GetInstance().observers_.find(MAIN_USER_ID),
237 NumkeyAppsManager::GetInstance().observers_.end());
238 NumkeyAppsManager::GetInstance().observers_.clear();
239 }
240
241 /**
242 * @tc.name: testOnUserRemoved_003
243 * @tc.desc: observers_ not empty, remove invalid user
244 * @tc.type: FUNC
245 */
246 HWTEST_F(NumKeyAppsManagerTest, testOnUserRemoved_003, TestSize.Level1)
247 {
248 IMSA_HILOGI("NumKeyAppsManagerTest testOnUserRemoved_003 START");
249 NumkeyAppsManager::GetInstance().isFeatureEnabled_ = true;
250 sptr<SettingsDataObserver> observer = new (std::nothrow) SettingsDataObserver("", "", nullptr);
251 ASSERT_TRUE(observer != nullptr);
252 NumkeyAppsManager::GetInstance().observers_.clear();
253 NumkeyAppsManager::GetInstance().observers_[MAIN_USER_ID] = observer;
254 NumkeyAppsManager::GetInstance().OnUserRemoved(INVALID_USER_ID);
255 EXPECT_NE(NumkeyAppsManager::GetInstance().observers_.find(MAIN_USER_ID),
256 NumkeyAppsManager::GetInstance().observers_.end());
257 NumkeyAppsManager::GetInstance().observers_.clear();
258 }
259
260 /**
261 * @tc.name: testInitWhiteList_001
262 * @tc.desc: InitWhiteList when already inited
263 * @tc.type: FUNC
264 */
265 HWTEST_F(NumKeyAppsManagerTest, testInitWhiteList_001, TestSize.Level1)
266 {
267 IMSA_HILOGI("NumKeyAppsManagerTest testInitWhiteList_001 START");
268 bool inited = NumkeyAppsManager::GetInstance().isListInited_.load();
269 NumkeyAppsManager::GetInstance().isListInited_.store(true);
270 auto ret = NumkeyAppsManager::GetInstance().InitWhiteList();
271 NumkeyAppsManager::GetInstance().isListInited_.store(inited);
272 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
273 }
274
275 /**
276 * @tc.name: testInitWhiteList_002
277 * @tc.desc: InitWhiteList when not inited
278 * @tc.type: FUNC
279 */
280 HWTEST_F(NumKeyAppsManagerTest, testInitWhiteList_002, TestSize.Level1)
281 {
282 IMSA_HILOGI("NumKeyAppsManagerTest testInitWhiteList_002 START");
283 bool inited = NumkeyAppsManager::GetInstance().isListInited_.load();
284 NumkeyAppsManager::GetInstance().isListInited_.store(false);
285 auto ret = NumkeyAppsManager::GetInstance().InitWhiteList();
286 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
287 NumkeyAppsManager::GetInstance().isListInited_.store(inited);
288 }
289
290 /**
291 * @tc.name: testUpdateUserBlockList_001
292 * @tc.desc: usersBlockList_ not empty update user block list
293 * @tc.type: FUNC
294 */
295 HWTEST_F(NumKeyAppsManagerTest, testUpdateUserBlockList_001, TestSize.Level1)
296 {
297 IMSA_HILOGI("NumKeyAppsManagerTest testUpdateUserBlockList_001 START");
298 NumkeyAppsManager::GetInstance().usersBlockList_.clear();
299 auto ret = NumkeyAppsManager::GetInstance().UpdateUserBlockList(MAIN_USER_ID);
300 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
301 EXPECT_NE(NumkeyAppsManager::GetInstance().usersBlockList_.find(MAIN_USER_ID),
302 NumkeyAppsManager::GetInstance().usersBlockList_.end());
303 NumkeyAppsManager::GetInstance().usersBlockList_.clear();
304 }
305
306 /**
307 * @tc.name: testParseWhiteList_001
308 * @tc.desc: test ParseWhiteList
309 * @tc.type: FUNC
310 */
311 HWTEST_F(NumKeyAppsManagerTest, testParseWhiteList_001, TestSize.Level1)
312 {
313 IMSA_HILOGI("NumKeyAppsManagerTest testParseWhiteList_001 START");
314 std::unordered_set<std::string> list;
315 auto ret = NumkeyAppsManager::GetInstance().ParseWhiteList(list);
316 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
317 }
318
319 /**
320 * @tc.name: testParseBlockList_001
321 * @tc.desc: test ParseBlockList
322 * @tc.type: FUNC
323 */
324 HWTEST_F(NumKeyAppsManagerTest, testParseBlockList_001, TestSize.Level1)
325 {
326 IMSA_HILOGI("NumKeyAppsManagerTest testParseBlockList_001 START");
327 std::unordered_set<std::string> list;
328 auto ret = NumkeyAppsManager::GetInstance().ParseBlockList(MAIN_USER_ID, list);
329 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
330 }
331
332 /**
333 * @tc.name: testRegisterUserBlockListData_001
334 * @tc.desc: test RegisterUserBlockListData
335 * @tc.type: FUNC
336 */
337 HWTEST_F(NumKeyAppsManagerTest, testRegisterUserBlockListData_001, TestSize.Level1)
338 {
339 IMSA_HILOGI("NumKeyAppsManagerTest testRegisterUserBlockListData_001 START");
340 NumkeyAppsManager::GetInstance().observers_.clear();
341 auto ret = NumkeyAppsManager::GetInstance().RegisterUserBlockListData(MAIN_USER_ID);
342 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
343 }
344
345 /**
346 * @tc.name: testRegisterUserBlockListData_002
347 * @tc.desc: test RegisterUserBlockListData
348 * @tc.type: FUNC
349 */
350 HWTEST_F(NumKeyAppsManagerTest, testRegisterUserBlockListData_002, TestSize.Level1)
351 {
352 IMSA_HILOGI("NumKeyAppsManagerTest testRegisterUserBlockListData_002 START");
353 NumkeyAppsManager::GetInstance().observers_.clear();
354 sptr<SettingsDataObserver> observer = new (std::nothrow) SettingsDataObserver("", "", nullptr);
355 ASSERT_TRUE(observer != nullptr);
356 NumkeyAppsManager::GetInstance().observers_[MAIN_USER_ID] = observer;
357 auto ret = NumkeyAppsManager::GetInstance().RegisterUserBlockListData(MAIN_USER_ID);
358 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
359 NumkeyAppsManager::GetInstance().observers_.clear();
360 }
361
362 /**
363 * @tc.name: testIsInNumKeyWhiteList_001
364 * @tc.desc: test IsInNumKeyWhiteList
365 * @tc.type: FUNC
366 */
367 HWTEST_F(NumKeyAppsManagerTest, testIsInNumKeyWhiteList_001, TestSize.Level1)
368 {
369 IMSA_HILOGI("NumKeyAppsManagerTest testIsInNumKeyWhiteList_001 START");
370
371 NumkeyAppsManager::GetInstance().numKeyAppList_.clear();
372 bool ret = NumkeyAppsManager::GetInstance().IsInNumKeyWhiteList(WHITE_LIST_APP_NAME);
373 EXPECT_FALSE(ret);
374
375 NumkeyAppsManager::GetInstance().numKeyAppList_.insert(WHITE_LIST_APP_NAME);
376 ret = NumkeyAppsManager::GetInstance().IsInNumKeyWhiteList(WHITE_LIST_APP_NAME);
377 EXPECT_TRUE(ret);
378
379 NumkeyAppsManager::GetInstance().numKeyAppList_.clear();
380 }
381
382 /**
383 * @tc.name: testIsInNumkeyBlockList_001
384 * @tc.desc: test IsInNumkeyBlockList
385 * @tc.type: FUNC
386 */
387 HWTEST_F(NumKeyAppsManagerTest, testIsInNumkeyBlockList_001, TestSize.Level1)
388 {
389 IMSA_HILOGI("NumKeyAppsManagerTest testIsInNumkeyBlockList_001 START");
390
391 NumkeyAppsManager::GetInstance().usersBlockList_.clear();
392 bool ret = NumkeyAppsManager::GetInstance().IsInNumkeyBlockList(MAIN_USER_ID, BLOCK_LIST_APP_NAME);
393 EXPECT_FALSE(ret);
394
395 NumkeyAppsManager::GetInstance().usersBlockList_[MAIN_USER_ID] = {};
396 ret = NumkeyAppsManager::GetInstance().IsInNumkeyBlockList(MAIN_USER_ID, BLOCK_LIST_APP_NAME);
397 EXPECT_FALSE(ret);
398
399 NumkeyAppsManager::GetInstance().usersBlockList_[MAIN_USER_ID] = { BLOCK_LIST_APP_NAME };
400 ret = NumkeyAppsManager::GetInstance().IsInNumkeyBlockList(MAIN_USER_ID, BLOCK_LIST_APP_NAME);
401 EXPECT_TRUE(ret);
402
403 NumkeyAppsManager::GetInstance().usersBlockList_.clear();
404 }
405 } // namespace MiscServices
406 } // namespace OHOS
407