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