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