1 /*
2 * Copyright (C) 2023 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 #include <gtest/gtest.h>
16 #include <sys/time.h>
17 #include <unistd.h>
18
19 #include <string>
20 #include <vector>
21
22 #include "global.h"
23 #include "ime_info_inquirer.h"
24 #include "input_method_controller.h"
25 #include "input_method_property.h"
26 #include "tdd_util.h"
27
28 using namespace testing::ext;
29 namespace OHOS {
30 namespace MiscServices {
31 class NewImeSwitchTest : public testing::Test {
32 public:
33 static void SetUpTestCase(void);
34 static void TearDownTestCase(void);
35 void SetUp();
36 void TearDown();
37 static void CheckCurrentProp();
38 static void CheckCurrentSubProp(const std::string &subName);
39 static void CheckCurrentSubProps();
40 static bool imeChangeFlag;
41 static sptr<InputMethodController> imc_;
42 static std::string bundleName;
43 static std::string extName;
44 static std::vector<std::string> subName;
45 static std::vector<std::string> locale;
46 static std::vector<std::string> language;
47 static bool enableOn;
48 static std::string beforeValue;
49 static std::string allEnableIme;
50 };
51 bool NewImeSwitchTest::imeChangeFlag = false;
52 sptr<InputMethodController> NewImeSwitchTest::imc_;
53 std::string NewImeSwitchTest::bundleName = "com.example.newTestIme";
54 std::string NewImeSwitchTest::extName = "InputMethodExtAbility";
55 std::vector<std::string> NewImeSwitchTest::subName{ "lowerInput", "upperInput", "chineseInput" };
56 std::vector<std::string> NewImeSwitchTest::locale{ "en-US", "en-US", "zh-CN" };
57 std::vector<std::string> NewImeSwitchTest::language{ "english", "english", "chinese" };
58 bool NewImeSwitchTest::enableOn = false;
59 std::string NewImeSwitchTest::beforeValue;
60 std::string NewImeSwitchTest::allEnableIme = "{\"enableImeList\" : {\"100\" : [ \"com.example.newTestIme\"]}}";
61 constexpr uint32_t IME_SUBTYPE_NUM = 3;
62 constexpr uint32_t WAIT_IME_READY_TIME = 1;
63 constexpr const char *ENABLE_IME_KEYWORD = "settings.inputmethod.enable_ime";
64 class InputMethodSettingListenerImpl : public InputMethodSettingListener {
65 public:
66 InputMethodSettingListenerImpl() = default;
67 ~InputMethodSettingListenerImpl() = default;
OnImeChange(const Property & property,const SubProperty & subProperty)68 void OnImeChange(const Property &property, const SubProperty &subProperty)
69 {
70 NewImeSwitchTest::imeChangeFlag = true;
71 IMSA_HILOGI("InputMethodSettingListenerImpl OnImeChange");
72 }
OnPanelStatusChange(const InputWindowStatus & status,const std::vector<InputWindowInfo> & windowInfo)73 void OnPanelStatusChange(const InputWindowStatus &status, const std::vector<InputWindowInfo> &windowInfo)
74 {
75 }
76 };
SetUpTestCase(void)77 void NewImeSwitchTest::SetUpTestCase(void)
78 {
79 IMSA_HILOGI("NewImeSwitchTest::SetUpTestCase");
80 ImeInfoInquirer::GetInstance().InitConfig();
81 enableOn = ImeInfoInquirer::GetInstance().IsEnableInputMethod();
82 TddUtil::GrantNativePermission();
83 if (enableOn == true) {
84 IMSA_HILOGI("Enable ime switch test.");
85 int32_t ret = TddUtil::GetEnableData(beforeValue);
86 if (ret == ErrorCode::NO_ERROR) {
87 TddUtil::PushEnableImeValue(ENABLE_IME_KEYWORD, allEnableIme);
88 }
89 }
90 TddUtil::StorageSelfTokenID();
91 TddUtil::SetTestTokenID(
92 TddUtil::AllocTestTokenID(true, "ohos.inputMethod.test", { "ohos.permission.CONNECT_IME_ABILITY" }));
93 imc_ = InputMethodController::GetInstance();
94 imc_->SetSettingListener(std::make_shared<InputMethodSettingListenerImpl>());
95 imc_->UpdateListenEventFlag("imeChange", true);
96 }
97
TearDownTestCase(void)98 void NewImeSwitchTest::TearDownTestCase(void)
99 {
100 IMSA_HILOGI("NewImeSwitchTest::TearDownTestCase");
101 if (enableOn) {
102 TddUtil::GrantNativePermission();
103 TddUtil::PushEnableImeValue(ENABLE_IME_KEYWORD, beforeValue);
104 }
105 InputMethodController::GetInstance()->Close();
106 TddUtil::RestoreSelfTokenID();
107 }
108
SetUp(void)109 void NewImeSwitchTest::SetUp(void)
110 {
111 IMSA_HILOGI("NewImeSwitchTest::SetUp");
112 }
113
TearDown(void)114 void NewImeSwitchTest::TearDown(void)
115 {
116 IMSA_HILOGI("NewImeSwitchTest::TearDown");
117 }
118
CheckCurrentProp()119 void NewImeSwitchTest::CheckCurrentProp()
120 {
121 std::shared_ptr<Property> property = imc_->GetCurrentInputMethod();
122 ASSERT_TRUE(property != nullptr);
123 EXPECT_EQ(property->name, bundleName);
124 EXPECT_EQ(property->id, extName);
125 }
126
CheckCurrentSubProp(const std::string & subName)127 void NewImeSwitchTest::CheckCurrentSubProp(const std::string &subName)
128 {
129 auto subProperty = imc_->GetCurrentInputMethodSubtype();
130 ASSERT_TRUE(subProperty != nullptr);
131 EXPECT_EQ(subProperty->id, subName);
132 EXPECT_EQ(subProperty->name, bundleName);
133 }
134
CheckCurrentSubProps()135 void NewImeSwitchTest::CheckCurrentSubProps()
136 {
137 std::vector<SubProperty> subProps;
138 auto ret = imc_->ListCurrentInputMethodSubtype(subProps);
139 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
140 ASSERT_EQ(subProps.size(), IME_SUBTYPE_NUM);
141 for (uint32_t i = 0; i < IME_SUBTYPE_NUM; i++) {
142 EXPECT_EQ(subProps[i].id, subName[i]);
143 EXPECT_EQ(subProps[i].name, bundleName);
144 EXPECT_EQ(subProps[i].language, language[i]);
145 EXPECT_EQ(subProps[i].locale, locale[i]);
146 }
147 }
148
149 /**
150 * @tc.name: testNewImeSwitch
151 * @tc.desc: switch ime to newTestIme.
152 * @tc.type: FUNC
153 * @tc.require:
154 * @tc.author: chenyu
155 */
156 HWTEST_F(NewImeSwitchTest, testNewImeSwitch, TestSize.Level0)
157 {
158 IMSA_HILOGI("newIme testNewImeSwitch Test START");
159 imeChangeFlag = false;
160 // switch to newTestIme
161 auto ret = imc_->SwitchInputMethod(SwitchTrigger::CURRENT_IME, bundleName);
162 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
163 EXPECT_TRUE(imeChangeFlag);
164 CheckCurrentProp();
165 CheckCurrentSubProp(subName[0]);
166 CheckCurrentSubProps();
167 sleep(WAIT_IME_READY_TIME);
168 }
169
170 /**
171 * @tc.name: testSubTypeSwitch_001
172 * @tc.desc: switch subtype with subName1.
173 * @tc.type: FUNC
174 * @tc.require:
175 * @tc.author: chenyu
176 */
177 HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_001, TestSize.Level0)
178 {
179 IMSA_HILOGI("newIme testSubTypeSwitch_001 Test START");
180 imeChangeFlag = false;
181 int32_t ret = imc_->SwitchInputMethod(SwitchTrigger::CURRENT_IME, bundleName, subName[0]);
182 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
183 EXPECT_FALSE(imeChangeFlag);
184 CheckCurrentProp();
185 CheckCurrentSubProp(subName[0]);
186 CheckCurrentSubProps();
187 }
188
189 /**
190 * @tc.name: testSubTypeSwitch_002
191 * @tc.desc: switch subtype with subName2.
192 * @tc.type: FUNC
193 * @tc.require:
194 * @tc.author: chenyu
195 */
196 HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_002, TestSize.Level0)
197 {
198 IMSA_HILOGI("newIme testSubTypeSwitch_002 Test START");
199 imeChangeFlag = false;
200 int32_t ret = imc_->SwitchInputMethod(SwitchTrigger::CURRENT_IME, bundleName, subName[1]);
201 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
202 EXPECT_TRUE(imeChangeFlag);
203 CheckCurrentProp();
204 CheckCurrentSubProp(subName[1]);
205 CheckCurrentSubProps();
206 }
207
208 /**
209 * @tc.name: testSubTypeSwitch_003
210 * @tc.desc: switch subtype with subName3.
211 * @tc.type: FUNC
212 * @tc.require:
213 * @tc.author: chenyu
214 */
215 HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_003, TestSize.Level0)
216 {
217 IMSA_HILOGI("newIme testSubTypeSwitch_003 Test START");
218 imeChangeFlag = false;
219 int32_t ret = imc_->SwitchInputMethod(SwitchTrigger::CURRENT_IME, bundleName, subName[2]);
220 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
221 EXPECT_TRUE(imeChangeFlag);
222 CheckCurrentProp();
223 CheckCurrentSubProp(subName[2]);
224 CheckCurrentSubProps();
225 }
226
227 /**
228 * @tc.name: testSubTypeSwitch_004
229 * @tc.desc: switch subtype witch subName1.
230 * @tc.type: FUNC
231 * @tc.require:
232 * @tc.author: chenyu
233 */
234 HWTEST_F(NewImeSwitchTest, testSubTypeSwitch_004, TestSize.Level0)
235 {
236 IMSA_HILOGI("newIme testSubTypeSwitch_004 Test START");
237 imeChangeFlag = false;
238 int32_t ret = imc_->SwitchInputMethod(SwitchTrigger::CURRENT_IME, bundleName, subName[0]);
239 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
240 EXPECT_TRUE(imeChangeFlag);
241 CheckCurrentProp();
242 CheckCurrentSubProp(subName[0]);
243 CheckCurrentSubProps();
244 }
245
246 /**
247 * @tc.name: testSubTypeSwitchWithErrorSubName
248 * @tc.desc: switch subtype with error subName.
249 * @tc.type: FUNC
250 * @tc.require:
251 * @tc.author: chenyu
252 */
253 HWTEST_F(NewImeSwitchTest, testSubTypeSwitchWithErrorSubName, TestSize.Level0)
254 {
255 IMSA_HILOGI("newIme testSubTypeSwitchWithErrorSubName Test START");
256 int32_t ret = imc_->SwitchInputMethod(SwitchTrigger::CURRENT_IME, bundleName, "errorSubName");
257 EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
258 CheckCurrentProp();
259 CheckCurrentSubProp(subName[0]);
260 CheckCurrentSubProps();
261 }
262
263 /**
264 * @tc.name: testSwitchToCurrentImeWithEmptySubName
265 * @tc.desc: switch to currentIme witch empty subName.
266 * @tc.type: FUNC
267 * @tc.require:
268 * @tc.author: chenyu
269 */
270 HWTEST_F(NewImeSwitchTest, testSwitchToCurrentImeWithEmptySubName, TestSize.Level0)
271 {
272 IMSA_HILOGI("newIme testSwitchToCurrentImeWithEmptySubName Test START");
273 imeChangeFlag = false;
274 int32_t ret = imc_->SwitchInputMethod(SwitchTrigger::CURRENT_IME, bundleName);
275 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
276 EXPECT_FALSE(imeChangeFlag);
277 CheckCurrentProp();
278 CheckCurrentSubProp(subName[0]);
279 CheckCurrentSubProps();
280 }
281
282 /**
283 * @tc.name: testSwitchInputMethod_001
284 * @tc.desc: switch ime to newTestIme and switch the subtype to subName1.
285 * @tc.type: FUNC
286 * @tc.require:
287 * @tc.author: weishaoxiong
288 */
289 HWTEST_F(NewImeSwitchTest, testSwitchInputMethod_001, TestSize.Level0)
290 {
291 IMSA_HILOGI("newIme testSwitchInputMethod_001 Test START");
292 imeChangeFlag = false;
293 auto ret = imc_->SwitchInputMethod(SwitchTrigger::SYSTEM_APP, bundleName, subName[1]);
294 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
295 EXPECT_TRUE(imeChangeFlag);
296 CheckCurrentProp();
297 CheckCurrentSubProp(subName[1]);
298 CheckCurrentSubProps();
299 }
300
301 /**
302 * @tc.name: testSwitchInputMethod_002
303 * @tc.desc: switch the subtype to subName0.
304 * @tc.type: FUNC
305 * @tc.require:
306 * @tc.author: weishaoxiong
307 */
308 HWTEST_F(NewImeSwitchTest, testSwitchInputMethod_002, TestSize.Level0)
309 {
310 IMSA_HILOGI("newIme testSwitchInputMethod_002 Test START");
311 imeChangeFlag = false;
312 auto ret = imc_->SwitchInputMethod(SwitchTrigger::SYSTEM_APP, bundleName, subName[0]);
313 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
314 EXPECT_TRUE(imeChangeFlag);
315 CheckCurrentProp();
316 CheckCurrentSubProp(subName[0]);
317 CheckCurrentSubProps();
318 }
319
320 /**
321 * @tc.name: testSwitchInputMethod_003
322 * @tc.desc: switch ime to newTestIme.
323 * @tc.type: FUNC
324 * @tc.require:
325 * @tc.author: weishaoxiong
326 */
327 HWTEST_F(NewImeSwitchTest, testSwitchInputMethod_003, TestSize.Level0)
328 {
329 IMSA_HILOGI("newIme testSwitchInputMethod_003 Test START");
330 auto ret = imc_->SwitchInputMethod(SwitchTrigger::SYSTEM_APP, bundleName);
331 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
332 CheckCurrentProp();
333 CheckCurrentSubProp(subName[0]);
334 CheckCurrentSubProps();
335 }
336
337 /**
338 * @tc.name: testSwitchInputMethod_004
339 * @tc.desc: The caller is not a system app.
340 * @tc.type: FUNC
341 * @tc.require:
342 * @tc.author: weishaoxiong
343 */
344 HWTEST_F(NewImeSwitchTest, testSwitchInputMethod_004, TestSize.Level0)
345 {
346 TddUtil::SetTestTokenID(
347 TddUtil::AllocTestTokenID(false, "ohos.inputMethod.test", { "ohos.permission.CONNECT_IME_ABILITY" }));
348 IMSA_HILOGI("newIme testSwitchInputMethod_004 Test START");
349 auto ret = imc_->SwitchInputMethod(SwitchTrigger::SYSTEM_APP, bundleName);
350 EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_SYSTEM_PERMISSION);
351 }
352
353 /**
354 * @tc.name: testSwitchInputMethod_005
355 * @tc.desc: The caller has no permissions.
356 * @tc.type: FUNC
357 * @tc.require:
358 * @tc.author: weishaoxiong
359 */
360 HWTEST_F(NewImeSwitchTest, testSwitchInputMethod_005, TestSize.Level0)
361 {
362 TddUtil::SetTestTokenID(TddUtil::AllocTestTokenID(true, "ohos.inputMethod.test", {}));
363 IMSA_HILOGI("newIme testSwitchInputMethod_005 Test START");
364 auto ret = imc_->SwitchInputMethod(SwitchTrigger::SYSTEM_APP, bundleName);
365 EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
366 }
367 } // namespace MiscServices
368 } // namespace OHOS
369