• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "ime_cfg_manager.h"
19 #include "ime_info_inquirer.h"
20 #include "sys_cfg_parser.h"
21 #undef private
22 
23 #include <gtest/gtest.h>
24 #include <unistd.h>
25 
26 using namespace testing;
27 using namespace testing::ext;
28 namespace OHOS {
29 namespace MiscServices {
30 class JsonOperateTest : public testing::Test {
31 public:
32     static constexpr const char *IME_PERSIST_CFG = "{\"imeCfgList\":[{\"userId\":100,\"currentIme\":\"bundleName/"
33                                                    "extName\",\"currentSubName\":\"subName\",\"tempScreenLockIme\":"
34                                                    "\"\",\"isDefaultImeSet\":false},{\"userId\":104,\"currentIme\":"
35                                                    "\"bundleName1/"
36                                                    "extName1\",\"currentSubName\":\"subName1\",\"tempScreenLockIme\":"
37                                                    "\"\",\"isDefaultImeSet\":false}]}";
38     static constexpr const char *IME_PERSIST_CFG_NULL = "{\"imeCfgList\":[]}";
39     static constexpr const char *IME_PERSIST_CFG_VALUE_TYPE_ERROR = "{\"imeCfgList\":[{\"userId\":100,\"currentIme\":"
40                                                                     "\"bundleName/"
41                                                                     "extName\",\"currentSubName\":\"subName\"},{"
42                                                                     "\"userId\":"
43                                                                     "\"104\",\"currentIme\":\"bundleName1/"
44                                                                     "extName1\",\"currentSubName\":\"subName1\"}]}";
45     static constexpr const char *IME_PERSIST_CFG_NAME_LACK = "{\"imeCfgList\":[{\"userId\":100,\"currentSubName\":"
46                                                              "\"subName\"}]}";
47     static constexpr const char *IME_PERSIST_CFG_NAME_ERROR = "{\"imeCfgList\":[{\"userId\":100, \"bundle\": "
48                                                               "\"bundleName/extNme\",\"currentSubName\":"
49                                                               "\"subName\"}]}";
50 
51     static constexpr const char *ENABLE_IME = "{\"enableImeList\" : {\"100\" : [ \"testIme\", \"testIme1\", "
52                                               "\"testIme2\"],\"101\" : [\"testIme3\"], \"102\" : []}}";
53     static constexpr const char *ENABLE_KEYBOARD = "{\"enableKeyboardList\" : {\"100\" : [ \"testKeyboard\", "
54                                                    "\"testKeyboard1\"],\"101\" : "
55                                                    "[\"testKeyboard2\"], \"105\" : []}}";
56     static constexpr const char *SECURITY_MODE = "{\"fullExperienceList\" : {\"100\" : [\"testIme\", "
57                                                  "\"testIme3\"], \"102\" : []}}";
58     static constexpr const char *SUBTYPE = "{\"subtypes\": [{\"icon\": \"$media:icon\",\"id\": "
59                                            "\"subtypeId\",\"label\": \"$string:chinese\",\"locale\": "
60                                            "\"zh-CN\",\"mode\": \"lower\"},{\"icon\": \"$media:icon1\",\"id\": "
61                                            "\"subtypeId1\",\"label\": \"$string:english\",\"locale\": "
62                                            "\"en-US\",\"mode\": \"upper\"}]} ";
63     static constexpr const char *INPUT_SYS_CGF = "{\"systemConfig\":{\"enableInputMethodFeature\":true,"
64                                                  "\"enableFullExperienceFeature\":true,"
65                                                  "\"systemInputMethodConfigAbility\":\"setAbility\","
66                                                  "\"defaultInputMethod\":\"bundleName/extName\"}, "
67                                                  "\"supportedInputTypeList\":[{\"inputType\":0,\"bundleName\":"
68                                                  "\"testBundleName\", "
69                                                  "\"subtypeId\":\"testSubtypeId\"},{\"inputType\":1,\"bundleName\":"
70                                                  "\"\", \"subtypeId\":\"\"}]}";
71     static constexpr const char *SYS_PANEL_ADJUST = "{\"sysPanelAdjust\":"
72                                                     "[{\"style\": [\"fix\",\"default\",\"landscape\"],"
73                                                     "\"top\": 1,\"left\": 2,\"right\": 3,\"bottom\": 4}]}";
74     static constexpr const char *IGNORE_SYS_PANEL_ADJUST = "{\"ignoreSysPanelAdjust\":{\"inputType\": [0, 1, 3]}}";
75     static constexpr const char *INPUT_SYS_CGF_UID_LIST = "{\"systemConfig\": {\"proxyImeUidList\": [7101, 5521]}}";
SetUpTestCase()76     static void SetUpTestCase() { }
TearDownTestCase()77     static void TearDownTestCase() { }
SetUp()78     void SetUp() { }
TearDown()79     void TearDown() { }
80 };
81 
82 /**
83  * @tc.name: testParseImePersistCfg001
84  * @tc.desc: parse imePersistCfg
85  * @tc.type: FUNC
86  * @tc.require:
87  * @tc.author: chenyu
88  */
89 HWTEST_F(JsonOperateTest, testParseImePersistCfg001, TestSize.Level1)
90 {
91     IMSA_HILOGI("JsonOperateTest testParseImePersistCfg001 START");
92     ImeCfgManager::GetInstance().imeConfigs_.clear();
93     auto ret = ImeCfgManager::GetInstance().ParseImeCfg(JsonOperateTest::IME_PERSIST_CFG);
94     ASSERT_TRUE(ret);
95     ASSERT_EQ(ImeCfgManager::GetInstance().imeConfigs_.size(), 2);
96     auto cfg = ImeCfgManager::GetInstance().imeConfigs_;
97     EXPECT_EQ(cfg[0].userId, 100);
98     EXPECT_EQ(cfg[0].currentIme, "bundleName/extName");
99     EXPECT_EQ(cfg[0].currentSubName, "subName");
100     EXPECT_EQ(cfg[1].userId, 104);
101     EXPECT_EQ(cfg[1].currentIme, "bundleName1/extName1");
102     EXPECT_EQ(cfg[1].currentSubName, "subName1");
103 
104     ImeCfgManager::GetInstance().imeConfigs_.clear();
105     ret = ImeCfgManager::GetInstance().ParseImeCfg(JsonOperateTest::IME_PERSIST_CFG_NULL);
106     EXPECT_TRUE(ret);
107     EXPECT_TRUE(ImeCfgManager::GetInstance().imeConfigs_.empty());
108 
109     ImeCfgManager::GetInstance().imeConfigs_.clear();
110     ret = ImeCfgManager::GetInstance().ParseImeCfg(JsonOperateTest::IME_PERSIST_CFG_VALUE_TYPE_ERROR);
111     EXPECT_FALSE(ret);
112     EXPECT_TRUE(ImeCfgManager::GetInstance().imeConfigs_.empty());
113 
114     ImeCfgManager::GetInstance().imeConfigs_.clear();
115     ret = ImeCfgManager::GetInstance().ParseImeCfg(JsonOperateTest::IME_PERSIST_CFG_NAME_LACK);
116     EXPECT_FALSE(ret);
117     EXPECT_TRUE(ImeCfgManager::GetInstance().imeConfigs_.empty());
118 
119     ImeCfgManager::GetInstance().imeConfigs_.clear();
120     ret = ImeCfgManager::GetInstance().ParseImeCfg(JsonOperateTest::IME_PERSIST_CFG_NAME_ERROR);
121     EXPECT_FALSE(ret);
122     EXPECT_TRUE(ImeCfgManager::GetInstance().imeConfigs_.empty());
123 
124     ret = ImeCfgManager::GetInstance().ParseImeCfg(JsonOperateTest::ENABLE_KEYBOARD);
125     EXPECT_FALSE(ret);
126 }
127 
128 /**
129  * @tc.name: testPackageImePersistCfg001
130  * @tc.desc: package imePersistCfg
131  * @tc.type: FUNC
132  * @tc.require:
133  * @tc.author: chenyu
134  */
135 HWTEST_F(JsonOperateTest, testPackageImePersistCfg001, TestSize.Level1)
136 {
137     IMSA_HILOGI("JsonOperateTest testPackageImePersistCfg001 START");
138     ImeCfgManager::GetInstance().imeConfigs_.clear();
139     ImeCfgManager::GetInstance().imeConfigs_.emplace_back(100, "bundleName/extName", "subName", false);
140     ImeCfgManager::GetInstance().imeConfigs_.emplace_back(104, "bundleName1/extName1", "subName1", false);
141     auto str = ImeCfgManager::GetInstance().PackageImeCfg();
142     EXPECT_EQ(str, JsonOperateTest::IME_PERSIST_CFG);
143 }
144 
145 /**
146  * @tc.name: testParseSystemConfig001
147  * @tc.desc: parse systemConfig
148  * @tc.type: FUNC
149  * @tc.require:
150  * @tc.author: chenyu
151  */
152 HWTEST_F(JsonOperateTest, testParseSystemConfig001, TestSize.Level1)
153 {
154     IMSA_HILOGI("JsonOperateTest testParseSystemConfig001 START");
155     ImeSystemConfig imeSystemConfig;
156     auto ret = imeSystemConfig.Unmarshall(INPUT_SYS_CGF);
157     ASSERT_TRUE(ret);
158     auto systemConfig = imeSystemConfig.systemConfig;
159     EXPECT_EQ(systemConfig.systemInputMethodConfigAbility, "setAbility");
160     EXPECT_EQ(systemConfig.defaultInputMethod, "bundleName/extName");
161     EXPECT_TRUE(systemConfig.enableInputMethodFeature);
162     EXPECT_TRUE(systemConfig.enableFullExperienceFeature);
163 }
164 
165 /**
166  * @tc.name: testParseInputType001
167  * @tc.desc: parse inputType
168  * @tc.type: FUNC
169  * @tc.require:
170  * @tc.author: chenyu
171  */
172 HWTEST_F(JsonOperateTest, testParseInputType001, TestSize.Level1)
173 {
174     IMSA_HILOGI("JsonOperateTest testParseInputType001 START");
175     InputTypeCfg inputTypeCfg;
176     auto ret = inputTypeCfg.Unmarshall(INPUT_SYS_CGF);
177     ASSERT_TRUE(ret);
178     auto inputType = inputTypeCfg.inputType;
179     ASSERT_EQ(inputType.size(), 2);
180     EXPECT_EQ(inputType[0].type, InputType::CAMERA_INPUT);
181     EXPECT_EQ(inputType[0].subName, "testSubtypeId");
182     EXPECT_EQ(inputType[0].bundleName, "testBundleName");
183     EXPECT_EQ(inputType[1].type, InputType::SECURITY_INPUT);
184     EXPECT_EQ(inputType[1].subName, "");
185     EXPECT_EQ(inputType[1].bundleName, "");
186 }
187 
188 /**
189  * @tc.name: testParseSubtype001
190  * @tc.desc: parse subtype
191  * @tc.type: FUNC
192  * @tc.require:
193  * @tc.author: chenyu
194  */
195 HWTEST_F(JsonOperateTest, testParseSubtype001, TestSize.Level1)
196 {
197     IMSA_HILOGI("JsonOperateTest testParseSubtype001 START");
198     std::vector<std::string> profiles { { JsonOperateTest::SUBTYPE } };
199     SubtypeCfg subtype;
200     auto ret = ImeInfoInquirer::GetInstance().ParseSubtypeProfile(profiles, subtype);
201     ASSERT_TRUE(ret);
202     ASSERT_EQ(subtype.subtypes.size(), 2);
203     auto subtypes = subtype.subtypes;
204     EXPECT_EQ(subtypes[0].icon, "$media:icon");
205     EXPECT_EQ(subtypes[0].id, "subtypeId");
206     EXPECT_EQ(subtypes[0].label, "$string:chinese");
207     EXPECT_EQ(subtypes[0].locale, "zh-CN");
208     EXPECT_EQ(subtypes[0].mode, "lower");
209     EXPECT_EQ(subtypes[1].icon, "$media:icon1");
210     EXPECT_EQ(subtypes[1].id, "subtypeId1");
211     EXPECT_EQ(subtypes[1].label, "$string:english");
212     EXPECT_EQ(subtypes[1].locale, "en-US");
213     EXPECT_EQ(subtypes[1].mode, "upper");
214 
215     std::vector<std::string> profiles1 { { JsonOperateTest::SECURITY_MODE } };
216     SubtypeCfg subtype1;
217     ret = ImeInfoInquirer::GetInstance().ParseSubtypeProfile(profiles1, subtype1);
218     EXPECT_FALSE(ret);
219     EXPECT_TRUE(subtype1.subtypes.empty());
220 }
221 
222 /**
223  * @tc.name: testParseSysPanelAdjust001
224  * @tc.desc: parse SysPanelAdjust
225  * @tc.type: FUNC
226  * @tc.require:
227  */
228 HWTEST_F(JsonOperateTest, testParseSysPanelAdjust001, TestSize.Level1)
229 {
230     IMSA_HILOGI("JsonOperateTest testParseSysPanelAdjust001 START");
231     SysPanelAdjustCfg sysPanelAdjustCfg;
232     auto ret = sysPanelAdjustCfg.Unmarshall(SYS_PANEL_ADJUST);
233     ASSERT_TRUE(ret);
234     auto panelAdjust = sysPanelAdjustCfg.panelAdjust;
235     EXPECT_EQ(panelAdjust[0].style[0], "fix");
236     EXPECT_EQ(panelAdjust[0].style[1], "default");
237     EXPECT_EQ(panelAdjust[0].style[2], "landscape");
238     EXPECT_EQ(panelAdjust[0].top, 1);
239     EXPECT_EQ(panelAdjust[0].left, 2);
240     EXPECT_EQ(panelAdjust[0].right, 3);
241     EXPECT_EQ(panelAdjust[0].bottom, 4);
242 }
243 
244 /**
245 @tc.name: testParseIgnoreSysPanelAdjust001
246 @tc.desc: parse IgnoreSysPanelAdjust
247 @tc.type: FUNC
248 @tc.require:
249 */
250 HWTEST_F(JsonOperateTest, testParseIgnoreSysPanelAdjust001, TestSize.Level1)
251 {
252     IMSA_HILOGI("JsonOperateTest testParseIgnoreSysPanelAdjust001 START");
253     IgnoreSysPanelAdjustCfg ignoreSysPanelAdjustCfg;
254     auto ret = ignoreSysPanelAdjustCfg.Unmarshall("");
255     ASSERT_FALSE(ret);
256 }
257 
258 /**
259 @tc.name: testParseIgnoreSysPanelAdjust002
260 @tc.desc: parse IgnoreSysPanelAdjust
261 @tc.type: FUNC
262 @tc.require:
263 */
264 HWTEST_F(JsonOperateTest, testParseIgnoreSysPanelAdjust002, TestSize.Level1)
265 {
266     IMSA_HILOGI("JsonOperateTest testParseIgnoreSysPanelAdjust002 START");
267     IgnoreSysPanelAdjustCfg ignoreSysPanelAdjustCfg;
268     auto ret = ignoreSysPanelAdjustCfg.Unmarshall(IGNORE_SYS_PANEL_ADJUST);
269     ASSERT_TRUE(ret);
270     auto ignoreSysPanelAdjust = ignoreSysPanelAdjustCfg.ignoreSysPanelAdjust;
271     EXPECT_EQ(ignoreSysPanelAdjust.inputType[0], 0);
272     EXPECT_EQ(ignoreSysPanelAdjust.inputType[1], 1);
273     EXPECT_EQ(ignoreSysPanelAdjust.inputType[2], 3);
274 }
275 
276 /**
277  * @tc.name: testGetDumpInfo
278  * @tc.desc: parse GetDumpInfo
279  * @tc.type: FUNC
280  * @tc.require:
281  */
282 HWTEST_F(JsonOperateTest, testGetDumpInfo, TestSize.Level1)
283 {
284     IMSA_HILOGI("JsonOperateTest testGetDumpInfo START");
285     int32_t userId = 1234567890;
286     auto ret = ImeInfoInquirer::GetInstance().GetDumpInfo(userId);
287     ASSERT_EQ(ret, "");
288 }
289 
290 /**
291  * @tc.name: testListDisabledInputMethod
292  * @tc.desc: ListDisabledInputMethod
293  * @tc.type: FUNC
294  * @tc.require:
295  */
296 HWTEST_F(JsonOperateTest, testListDisabledInputMethod, TestSize.Level1)
297 {
298     IMSA_HILOGI("JsonOperateTest testListDisabledInputMethod START");
299     int32_t userId = 1234567890;
300     std::vector<Property> props;
301     auto ret = ImeInfoInquirer::GetInstance().ListDisabledInputMethod(userId, props);
302     ASSERT_NE(ret, ErrorCode::NO_ERROR);
303     ret = ImeInfoInquirer::GetInstance().ListDisabledInputMethod(userId, props);
304     ASSERT_NE(ret, ErrorCode::NO_ERROR);
305 }
306 
307 /**
308  * @tc.name: testGetSwitchInfoBySwitchCount
309  * @tc.desc: test GetSwitchInfoBySwitchCount
310  * @tc.type: FUNC
311  * @tc.require:
312  */
313 HWTEST_F(JsonOperateTest, testGetSwitchInfoBySwitchCount, TestSize.Level1)
314 {
315     IMSA_HILOGI("JsonOperateTest testGetSwitchInfoBySwitchCount START");
316     SwitchInfo switchInfo;
317     uint32_t cacheCount = 987654321;
318     int32_t userId = 1234567890;
319     auto ret = ImeInfoInquirer::GetInstance().GetSwitchInfoBySwitchCount(switchInfo, userId, cacheCount);
320     ASSERT_NE(ret, ErrorCode::NO_ERROR);
321 }
322 
323 /**
324  * @tc.name: testGetInputMethodConfig
325  * @tc.desc: test GetInputMethodConfig
326  * @tc.type: FUNC
327  * @tc.require:
328  */
329 HWTEST_F(JsonOperateTest, testGetInputMethodConfig, TestSize.Level1)
330 {
331     IMSA_HILOGI("JsonOperateTest testGetInputMethodConfig START");
332     AppExecFwk::ElementName inputMethodConfig;
333     int32_t userId = 100;
334     auto ret = ImeInfoInquirer::GetInstance().GetInputMethodConfig(userId, inputMethodConfig);
335     ASSERT_EQ(ret, ErrorCode::NO_ERROR);
336 }
337 
338 /**
339  * @tc.name: testFindTargetSubtypeByCondition
340  * @tc.desc: test FindTargetSubtypeByCondition
341  * @tc.type: FUNC
342  * @tc.require:
343  */
344 HWTEST_F(JsonOperateTest, testFindTargetSubtypeByCondition, TestSize.Level1)
345 {
346     IMSA_HILOGI("JsonOperateTest testFindTargetSubtypeByCondition START");
347     std::vector<SubProperty> subProps;
348     Condition condition = Condition::ENGLISH;
349     auto ret = ImeInfoInquirer::GetInstance().FindTargetSubtypeByCondition(subProps, condition);
350     ASSERT_EQ(ret, nullptr);
351     int32_t invalidNum = 5;
352     condition = static_cast<Condition>(invalidNum);
353     ret = ImeInfoInquirer::GetInstance().FindTargetSubtypeByCondition(subProps, condition);
354     ASSERT_EQ(ret, nullptr);
355 }
356 
357 /**
358  * @tc.name: testParseSubtypeProfile
359  * @tc.desc: test ParseSubtypeProfile
360  * @tc.type: FUNC
361  * @tc.require:
362  */
363 HWTEST_F(JsonOperateTest, testParseSubtypeProfile, TestSize.Level1)
364 {
365     IMSA_HILOGI("JsonOperateTest testParseSubtypeProfile START");
366     std::vector<std::string> profiles;
367     SubtypeCfg subtypeCfg;
368     auto ret = ImeInfoInquirer::GetInstance().ParseSubtypeProfile(profiles, subtypeCfg);
369     ASSERT_FALSE(ret);
370 }
371 
372 /**
373  * @tc.name: testGetResMgr
374  * @tc.desc: test GetResMgr
375  * @tc.type: FUNC
376  * @tc.require:
377  */
378 HWTEST_F(JsonOperateTest, testGetResMgr, TestSize.Level1)
379 {
380     IMSA_HILOGI("JsonOperateTest testGetResMgr START");
381     std::string resourcePath = "";
382     auto ret = ImeInfoInquirer::GetInstance().GetResMgr(resourcePath);
383     ASSERT_FALSE(ret);
384 }
385 
386 /**
387  * @tc.name: testIsDynamicStartIme
388  * @tc.desc: test IsDynamicStartIme
389  * @tc.type: FUNC
390  * @tc.require:
391  */
392 HWTEST_F(JsonOperateTest, testIsDynamicStartIme, TestSize.Level1)
393 {
394     IMSA_HILOGI("JsonOperateTest testIsDynamicStartIme START");
395     auto instance = ImeInfoInquirer::GetInstance();
396     // dynamicStartImeSysParam is empty
397     auto ret = instance.IsDynamicStartIme();
398     EXPECT_FALSE(false);
399 
400     // dynamicStartImeValue is empty
401     instance.systemConfig_.dynamicStartImeSysParam = "123";
402     ret = instance.IsDynamicStartIme();
403     EXPECT_TRUE(ret);
404 
405     // dynamicStartImeValue is default
406     instance.systemConfig_.dynamicStartImeValue = "default";
407     ret = instance.IsDynamicStartIme();
408     EXPECT_FALSE(ret);
409 
410     instance.systemConfig_.dynamicStartImeValue = "";
411     instance.systemConfig_.dynamicStartImeSysParam = "";
412 }
413 
414 /**
415  * @tc.name: testParseSystemConfigUidList
416  * @tc.desc: parse systemConfig
417  * @tc.type: FUNC
418  * @tc.require:
419  */
420 HWTEST_F(JsonOperateTest, testParseSystemConfigUidList, TestSize.Level1)
421 {
422     IMSA_HILOGI("JsonOperateTest testParseSystemConfigUidList START");
423     ImeSystemConfig imeSystemConfig;
424     auto ret = imeSystemConfig.Unmarshall(INPUT_SYS_CGF_UID_LIST);
425     ASSERT_TRUE(ret);
426     auto systemConfig = imeSystemConfig.systemConfig;
427     int32_t uid = 0;
428     EXPECT_FALSE(systemConfig.proxyImeUidList.find(uid) != systemConfig.proxyImeUidList.end());
429     uid = 7101;
430     EXPECT_TRUE(systemConfig.proxyImeUidList.find(uid) != systemConfig.proxyImeUidList.end());
431     uid = 5521;
432     EXPECT_TRUE(systemConfig.proxyImeUidList.find(uid) != systemConfig.proxyImeUidList.end());
433 }
434 } // namespace MiscServices
435 } // namespace OHOS