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