1 /*
2 * Copyright (C) 2022 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 #define private public
16 #define protected public
17 #include "full_ime_info_manager.h"
18 #include "ime_cfg_manager.h"
19 #include "ime_info_inquirer.h"
20 #include "input_method_controller.h"
21 #include "input_method_system_ability.h"
22 #include "peruser_session.h"
23 #include "wms_connection_observer.h"
24 #undef private
25 #include <gtest/gtest.h>
26 #include <sys/time.h>
27 #include <unistd.h>
28
29 #include <string>
30 #include <vector>
31
32 #include "application_info.h"
33 #include "global.h"
34 #include "i_input_method_agent.h"
35 #include "i_input_method_core.h"
36 #include "ime_cfg_manager.h"
37 #include "input_method_agent_proxy.h"
38 #include "input_method_agent_stub.h"
39 #include "input_method_core_stub.h"
40 #include "keyboard_event.h"
41 #include "os_account_manager.h"
42 #include "tdd_util.h"
43 #include "user_session_manager.h"
44
45 using namespace testing::ext;
46 namespace OHOS {
47 namespace MiscServices {
48 using namespace AppExecFwk;
49 class InputMethodPrivateMemberTest : public testing::Test {
50 public:
51 static void SetUpTestCase(void);
52 static void TearDownTestCase(void);
53 void SetUp();
54 void TearDown();
55 static sptr<InputMethodSystemAbility> service_;
56 };
57 constexpr std::int32_t MAIN_USER_ID = 100;
SetUpTestCase(void)58 void InputMethodPrivateMemberTest::SetUpTestCase(void)
59 {
60 IMSA_HILOGI("InputMethodPrivateMemberTest::SetUpTestCase");
61 service_ = new (std::nothrow) InputMethodSystemAbility();
62 if (service_ == nullptr) {
63 return;
64 }
65 service_->OnStart();
66 }
67
TearDownTestCase(void)68 void InputMethodPrivateMemberTest::TearDownTestCase(void)
69 {
70 service_->OnStop();
71 IMSA_HILOGI("InputMethodPrivateMemberTest::TearDownTestCase");
72 }
73
SetUp(void)74 void InputMethodPrivateMemberTest::SetUp(void)
75 {
76 IMSA_HILOGI("InputMethodPrivateMemberTest::SetUp");
77 ImeCfgManager::GetInstance().imeConfigs_.clear();
78 FullImeInfoManager::GetInstance().fullImeInfos_.clear();
79 service_->userId_ = MAIN_USER_ID;
80 }
81
TearDown(void)82 void InputMethodPrivateMemberTest::TearDown(void)
83 {
84 IMSA_HILOGI("InputMethodPrivateMemberTest::TearDown");
85 ImeCfgManager::GetInstance().imeConfigs_.clear();
86 FullImeInfoManager::GetInstance().fullImeInfos_.clear();
87 }
88 sptr<InputMethodSystemAbility> InputMethodPrivateMemberTest::service_;
89
90 /**
91 * @tc.name: SA_TestOnStart
92 * @tc.desc: SA OnStart.
93 * @tc.type: FUNC
94 * @tc.require:
95 */
96 HWTEST_F(InputMethodPrivateMemberTest, SA_TestOnStart, TestSize.Level0)
97 {
98 InputMethodSystemAbility ability;
99 ability.state_ = ServiceRunningState::STATE_RUNNING;
100 ability.OnStart();
101 EXPECT_EQ(ability.identityChecker_, nullptr);
102 }
103
104 /**
105 * @tc.name: SA_GetExtends
106 * @tc.desc: SA GetExtends.
107 * @tc.type: FUNC
108 * @tc.require: issuesI640YZ
109 */
110 HWTEST_F(InputMethodPrivateMemberTest, SA_GetExtends, TestSize.Level0)
111 {
112 constexpr int32_t metaDataNums = 5;
113 ImeInfoInquirer inquirer;
114 std::vector<Metadata> metaData;
115 Metadata metadata[metaDataNums] = { { "language", "english", "" }, { "mode", "mode", "" },
116 { "locale", "local", "" }, { "icon", "icon", "" }, { "", "", "" } };
117 for (auto const &data : metadata) {
118 metaData.emplace_back(data);
119 }
120 auto subProperty = inquirer.GetExtends(metaData);
121 EXPECT_EQ(subProperty.language, "english");
122 EXPECT_EQ(subProperty.mode, "mode");
123 EXPECT_EQ(subProperty.locale, "local");
124 EXPECT_EQ(subProperty.icon, "icon");
125 }
126
127 /**
128 * @tc.name: SA_TestOnUserStarted
129 * @tc.desc: SA_TestOnUserStarted.
130 * @tc.type: FUNC
131 * @tc.require:
132 */
133 HWTEST_F(InputMethodPrivateMemberTest, SA_TestOnUserStarted, TestSize.Level0)
134 {
135 // isScbEnable_ is true
136 service_->isScbEnable_ = true;
137 MessageParcel *parcel = nullptr;
138 auto msg = std::make_shared<Message>(MessageID::MSG_ID_USER_START, parcel);
139 auto ret = service_->OnUserStarted(msg.get());
140 EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
141
142 // msg is nullptr
143 service_->isScbEnable_ = false;
144 ret = service_->OnUserStarted(msg.get());
145 EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
146
147 // userId is same
148 service_->userId_ = 50;
149 MessageParcel *parcel1 = new MessageParcel();
150 parcel1->WriteInt32(50);
151 auto msg1 = std::make_shared<Message>(MessageID::MSG_ID_USER_START, parcel1);
152 ret = service_->OnUserStarted(msg1.get());
153 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
154
155 // start ime
156 WmsConnectionObserver observer(nullptr);
157 observer.OnConnected(60, 0);
158 // imeStarting_ is true
159 IMSA_HILOGI("InputMethodPrivateMemberTest::imeStarting_ is true");
160 service_->userId_ = 50;
161 MessageParcel *parcel2 = new MessageParcel();
162 parcel2->WriteInt32(60);
163 auto msg2 = std::make_shared<Message>(MessageID::MSG_ID_USER_START, parcel2);
164 ret = service_->OnUserStarted(msg2.get());
165 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
166 // imeStarting_ is false
167 IMSA_HILOGI("InputMethodPrivateMemberTest::imeStarting_ is false");
168 service_->userId_ = 50;
169 MessageParcel *parcel3 = new MessageParcel();
170 observer.OnConnected(333, 0);
171 parcel3->WriteInt32(333);
172 auto msg3 = std::make_shared<Message>(MessageID::MSG_ID_USER_START, parcel3);
173 ret = service_->OnUserStarted(msg3.get());
174 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
175 }
176
177 /**
178 * @tc.name: SA_TestOnUserRemoved
179 * @tc.desc: SA_TestOnUserRemoved.
180 * @tc.type: FUNC
181 * @tc.require:
182 */
183 HWTEST_F(InputMethodPrivateMemberTest, SA_TestOnUserRemoved, TestSize.Level0)
184 {
185 // msg is nullptr
186 auto *msg = new Message(MessageID::MSG_ID_USER_REMOVED, nullptr);
187 auto ret = service_->OnUserRemoved(msg);
188 EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
189 MessageHandler::Instance()->SendMessage(msg);
190
191 // move userId
192 MessageParcel *parcel1 = new MessageParcel();
193 parcel1->WriteInt32(60);
194 auto msg1 = std::make_shared<Message>(MessageID::MSG_ID_USER_REMOVED, parcel1);
195 auto ret1 = service_->OnUserRemoved(msg1.get());
196 EXPECT_EQ(ret1, ErrorCode::NO_ERROR);
197 }
198
199 /**
200 * @tc.name: SA_ListInputMethodInfoWithInexistentUserId
201 * @tc.desc: SA ListInputMethodInfo With Inexistent UserId.
202 * @tc.type: FUNC
203 * @tc.require: issuesI669E8
204 */
205 HWTEST_F(InputMethodPrivateMemberTest, SA_ListInputMethodInfoWithInexistentUserId, TestSize.Level0)
206 {
207 ImeInfoInquirer inquirer;
208 constexpr int32_t userId = 1;
209 auto inputMethodInfos = inquirer.ListInputMethodInfo(userId);
210 EXPECT_TRUE(inputMethodInfos.empty());
211 }
212
213 /**
214 * @tc.name: IMC_ListInputMethodCommonWithErrorStatus
215 * @tc.desc: IMC ListInputMethodCommon With Error Status.
216 * @tc.type: FUNC
217 * @tc.require: issuesI669E8
218 */
219 HWTEST_F(InputMethodPrivateMemberTest, IMC_ListInputMethodCommonWithErrorStatus, TestSize.Level0)
220 {
221 std::vector<Property> props;
222 auto ret = InputMethodController::GetInstance()->ListInputMethodCommon(static_cast<InputMethodStatus>(5), props);
223 EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
224 EXPECT_TRUE(props.empty());
225 }
226
227 /**
228 * @tc.name: PerUserSessionCoreOrAgentNullptr
229 * @tc.desc: Test PerUserSession with core nullptr.
230 * @tc.type: FUNC
231 * @tc.require: issuesI794QF
232 * @tc.author: Zhaolinglan
233 */
234 HWTEST_F(InputMethodPrivateMemberTest, PerUserSessionCoreOrAgentNullptr, TestSize.Level0)
235 {
236 IMSA_HILOGI("InputMethodPrivateMemberTest PerUserSessionCoreOrAgentNullptr TEST START");
237 auto userSession = std::make_shared<PerUserSession>(MAIN_USER_ID);
238 auto imc = InputMethodController::GetInstance();
239 int32_t ret = userSession->ShowKeyboard(imc->clientInfo_.client);
240 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOUND);
241 ret = userSession->HideKeyboard(imc->clientInfo_.client);
242 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOUND);
243 ret = userSession->InitInputControlChannel();
244 EXPECT_EQ(ret, ErrorCode::ERROR_IME_NOT_STARTED);
245 userSession->StopCurrentIme();
246 ret = userSession->SwitchSubtype({});
247 EXPECT_EQ(ret, ErrorCode::ERROR_IME_NOT_STARTED);
248 }
249
250 /**
251 * @tc.name: PerUserSessionClientError
252 * @tc.desc: Test PerUserSession with client error.
253 * @tc.type: FUNC
254 * @tc.require: issuesI794QF
255 * @tc.author: Zhaolinglan
256 */
257 HWTEST_F(InputMethodPrivateMemberTest, PerUserSessionClientError, TestSize.Level0)
258 {
259 IMSA_HILOGI("InputMethodPrivateMemberTest PerUserSessionClientError TEST START");
260 auto userSession = std::make_shared<PerUserSession>(MAIN_USER_ID);
261 auto imc = InputMethodController::GetInstance();
262 sptr<InputMethodCoreStub> core = new InputMethodCoreStub();
263
264 auto clientInfo = userSession->GetClientInfo(imc->clientInfo_.client->AsObject());
265 EXPECT_EQ(clientInfo, nullptr);
266
267 userSession->SetCurrentClient(nullptr);
268 userSession->OnUnfocused(0, 0);
269 int32_t ret = userSession->OnHideCurrentInput();
270 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOUND);
271 ret = userSession->OnShowCurrentInput();
272 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOUND);
273
274 userSession->SetCurrentClient(imc->clientInfo_.client);
275 ret = userSession->OnShowCurrentInput();
276 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOUND);
277 }
278
279 /**
280 * @tc.name: PerUserSessionParameterNullptr001
281 * @tc.desc: Test PerUserSession with parameter client nullptr.
282 * @tc.type: FUNC
283 * @tc.require: issuesI794QF
284 * @tc.author: Zhaolinglan
285 */
286 HWTEST_F(InputMethodPrivateMemberTest, PerUserSessionParameterNullptr001, TestSize.Level0)
287 {
288 IMSA_HILOGI("InputMethodPrivateMemberTest PerUserSessionParameterNullptr001 TEST START");
289 auto userSession = std::make_shared<PerUserSession>(MAIN_USER_ID);
290 sptr<IRemoteObject> agent = nullptr;
291 InputClientInfo clientInfo;
292 int32_t ret = userSession->OnStartInput(clientInfo, agent);
293 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
294 ret = userSession->OnReleaseInput(nullptr);
295 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
296 auto client = userSession->GetClientInfo(nullptr);
297 EXPECT_EQ(client, nullptr);
298 }
299
300 /**
301 * @tc.name: PerUserSessionParameterNullptr003
302 * @tc.desc: Test PerUserSession with parameter nullptr.
303 * @tc.type: FUNC
304 * @tc.require: issuesI794QF
305 * @tc.author: Zhaolinglan
306 */
307 HWTEST_F(InputMethodPrivateMemberTest, PerUserSessionParameterNullptr003, TestSize.Level0)
308 {
309 IMSA_HILOGI("InputMethodPrivateMemberTest PerUserSessionParameterNullptr003 TEST START");
310 auto userSession = std::make_shared<PerUserSession>(MAIN_USER_ID);
311 sptr<InputMethodCoreStub> core = new InputMethodCoreStub();
312 userSession->OnClientDied(nullptr);
313 userSession->OnImeDied(nullptr, ImeType::IME);
314 bool isShowKeyboard = false;
315 userSession->UpdateClientInfo(nullptr, { { UpdateFlag::ISSHOWKEYBOARD, isShowKeyboard } });
316 int32_t ret = userSession->RemoveIme(nullptr, ImeType::IME);
317 EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
318 }
319
320 /**
321 * @tc.name: SA_ImeCfgManagerTest_002
322 * @tc.desc: getImeCfg failed
323 * @tc.type: FUNC
324 * @tc.require:
325 * @tc.author: chenyu
326 */
327 HWTEST_F(InputMethodPrivateMemberTest, SA_ImeCfgManagerTest_002, TestSize.Level0)
328 {
329 IMSA_HILOGI("InputMethodPrivateMemberTest SA_ImeCfgManagerTest_002 TEST START");
330 ImeCfgManager cfgManager;
331 auto cfg = cfgManager.GetImeCfg(100);
332 EXPECT_TRUE(cfg.currentSubName.empty());
333 EXPECT_TRUE(cfg.currentIme.empty());
334 }
335
336 /**
337 * @tc.name: SA_SwitchByCombinationKey_001
338 * @tc.desc: keycode = MMI::KeyEvent::KEYCODE_0
339 * @tc.type: FUNC
340 * @tc.require:
341 * @tc.author: chenyu
342 */
343 HWTEST_F(InputMethodPrivateMemberTest, SA_SwitchByCombinationKey_001, TestSize.Level0)
344 {
345 IMSA_HILOGI("InputMethodPrivateMemberTest SA_SwitchByCombinationKey_001 TEST START");
346 ImeCfgManager cfgManager;
347 auto ret = service_->SwitchByCombinationKey(0);
348 EXPECT_EQ(ret, ErrorCode::ERROR_EX_UNSUPPORTED_OPERATION);
349 }
350
351 /**
352 * @tc.name: SA_SwitchByCombinationKey_002
353 * @tc.desc: SwitchLanguage()/SwitchMode():GetImeInfo failed
354 * @tc.type: FUNC
355 * @tc.require:
356 * @tc.author: chenyu
357 */
358 HWTEST_F(InputMethodPrivateMemberTest, SA_SwitchByCombinationKey_002, TestSize.Level0)
359 {
360 IMSA_HILOGI("InputMethodPrivateMemberTest SA_SwitchByCombinationKey_002 TEST START");
361 ImeCfgManager cfgManager;
362 auto ret = service_->SwitchByCombinationKey(KeyboardEvent::SHIFT_RIGHT_MASK);
363 EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
364 ret = service_->SwitchByCombinationKey(KeyboardEvent::CAPS_MASK);
365 EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
366 }
367
368 /**
369 * @tc.name: SA_SwitchByCombinationKey_003
370 * @tc.desc: SwitchLanguage()/SwitchMode():is newIme
371 * @tc.type: FUNC
372 * @tc.require:
373 * @tc.author: chenyu
374 */
375 HWTEST_F(InputMethodPrivateMemberTest, SA_SwitchByCombinationKey_003, TestSize.Level0)
376 {
377 IMSA_HILOGI("InputMethodPrivateMemberTest SA_SwitchByCombinationKey_003 TEST START");
378 ImeCfgManager cfgManager;
379 FullImeInfo info;
380 info.isNewIme = true;
381 info.prop = { .name = "testBundleName" };
382 info.subProps = { { .id = "testSubName" } };
383 FullImeInfoManager::GetInstance().fullImeInfos_.insert({ MAIN_USER_ID, { info } });
384 ImeCfgManager::GetInstance().imeConfigs_.push_back({ MAIN_USER_ID, "testBundleName/testExtName", "testSubName" });
385 auto ret = service_->SwitchByCombinationKey(KeyboardEvent::SHIFT_RIGHT_MASK);
386 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
387 ret = service_->SwitchByCombinationKey(KeyboardEvent::CAPS_MASK);
388 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
389 }
390
391 /**
392 * @tc.name: SA_SwitchByCombinationKey_004
393 * @tc.desc: SwitchLanguage():info.subProp.language == "French"
394 * @tc.type: FUNC
395 * @tc.require:
396 * @tc.author: chenyu
397 */
398 HWTEST_F(InputMethodPrivateMemberTest, SA_SwitchByCombinationKey_004, TestSize.Level0)
399 {
400 IMSA_HILOGI("InputMethodPrivateMemberTest SA_SwitchByCombinationKey_004 TEST START");
401 FullImeInfo info;
402 info.prop = { .name = "testBundleName", .id = "testExtName" };
403 info.subProps = { { .name = "testBundleName", .id = "testSubName", .language = "French" } };
404 FullImeInfoManager::GetInstance().fullImeInfos_.insert({ MAIN_USER_ID, { info } });
405 ImeCfgManager::GetInstance().imeConfigs_.push_back({ MAIN_USER_ID, "testBundleName/testExtName", "testSubName" });
406 auto ret = service_->SwitchByCombinationKey(KeyboardEvent::SHIFT_RIGHT_MASK);
407 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
408 }
409
410 /**
411 * @tc.name: SA_SwitchByCombinationKey_005
412 * @tc.desc: SwitchLanguage()/SwitchMode():FindTargetSubtypeByCondition failed
413 * @tc.type: FUNC
414 * @tc.require:
415 * @tc.author: chenyu
416 */
417 HWTEST_F(InputMethodPrivateMemberTest, SA_SwitchByCombinationKey_005, TestSize.Level0)
418 {
419 IMSA_HILOGI("InputMethodPrivateMemberTest SA_SwitchByCombinationKey_005 TEST START");
420 FullImeInfo info;
421 info.prop = { .name = "testBundleName", .id = "testExtName" };
422 info.subProps = { { .name = "testBundleName", .id = "testSubName", .mode = "upper", .language = "english" } };
423 FullImeInfoManager::GetInstance().fullImeInfos_.insert({ MAIN_USER_ID, { info } });
424 ImeCfgManager::GetInstance().imeConfigs_.push_back({ MAIN_USER_ID, "testBundleName/testExtName", "testSubName" });
425 auto ret = service_->SwitchByCombinationKey(KeyboardEvent::SHIFT_RIGHT_MASK);
426 EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
427 ret = service_->SwitchByCombinationKey(KeyboardEvent::CAPS_MASK);
428 EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
429 }
430
431 /**
432 * @tc.name: SA_SwitchByCombinationKey_006
433 * @tc.desc: SwitchLanguage()/SwitchMode():StartInputService() failed
434 * @tc.type: FUNC
435 * @tc.require:
436 * @tc.author: chenyu
437 */
438 HWTEST_F(InputMethodPrivateMemberTest, SA_SwitchByCombinationKey_006, TestSize.Level0)
439 {
440 IMSA_HILOGI("InputMethodPrivateMemberTest SA_SwitchByCombinationKey_006 TEST START");
441 FullImeInfo info;
442 info.prop = { .name = "testBundleName", .id = "testExtName" };
443 info.subProps = { { .name = "testBundleName", .id = "testSubName", .mode = "upper", .language = "english" },
444 { .name = "testBundleName", .id = "testSubName1", .mode = "lower", .language = "chinese" } };
445 FullImeInfoManager::GetInstance().fullImeInfos_.insert({ MAIN_USER_ID, { info } });
446
447 ImeCfgManager::GetInstance().imeConfigs_.push_back({ MAIN_USER_ID, "testBundleName/testExtName", "testSubName" });
448 // english->chinese
449 auto ret = service_->SwitchByCombinationKey(KeyboardEvent::SHIFT_RIGHT_MASK);
450 EXPECT_EQ(ret, ErrorCode::ERROR_IME_START_FAILED);
451 // lower->upper
452 ret = service_->SwitchByCombinationKey(KeyboardEvent::CAPS_MASK);
453 EXPECT_EQ(ret, ErrorCode::ERROR_IME_START_FAILED);
454 }
455
456 /**
457 * @tc.name: SA_SwitchByCombinationKey_007
458 * @tc.desc: SwitchType():StartInputService() failed
459 * @tc.type: FUNC
460 * @tc.require:
461 * @tc.author: chenyu
462 */
463 HWTEST_F(InputMethodPrivateMemberTest, SA_SwitchByCombinationKey_007, TestSize.Level0)
464 {
465 IMSA_HILOGI("InputMethodPrivateMemberTest SA_SwitchByCombinationKey_007 TEST START");
466 auto userId = TddUtil::GetCurrentUserId();
467 service_->userId_ = userId;
468 std::vector<Property> props;
469 InputMethodController::GetInstance()->ListInputMethod(props);
470 if (props.size() == 1) {
471 auto ret = service_->SwitchByCombinationKey(KeyboardEvent::SHIFT_RIGHT_MASK | KeyboardEvent::CTRL_RIGHT_MASK);
472 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
473 }
474 }
475
476 /**
477 * @tc.name: SA_SwitchByCombinationKey_008
478 * @tc.desc: SwitchType():find_if failed
479 * @tc.type: FUNC
480 * @tc.require:
481 * @tc.author: chenyu
482 */
483 HWTEST_F(InputMethodPrivateMemberTest, SA_SwitchByCombinationKey_008, TestSize.Level0)
484 {
485 IMSA_HILOGI("InputMethodPrivateMemberTest SA_SwitchByCombinationKey_008 TEST START");
486 auto userId = TddUtil::GetCurrentUserId();
487 service_->userId_ = userId;
488 auto prop = InputMethodController::GetInstance()->GetCurrentInputMethod();
489 auto subProp = InputMethodController::GetInstance()->GetCurrentInputMethodSubtype();
490 ImeCfgManager::GetInstance().imeConfigs_.push_back({ userId, prop->name + "/" + prop->id, subProp->id });
491 std::vector<Property> props;
492 InputMethodController::GetInstance()->ListInputMethod(props);
493 if (props.size() == 1) {
494 auto ret = service_->SwitchByCombinationKey(KeyboardEvent::SHIFT_RIGHT_MASK | KeyboardEvent::CTRL_RIGHT_MASK);
495 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
496 }
497 }
498
499 /**
500 * @tc.name: SA_testReleaseInput_001
501 * @tc.desc: client is nullptr
502 * @tc.type: FUNC
503 * @tc.require:
504 * @tc.author: chenyu
505 */
506 HWTEST_F(InputMethodPrivateMemberTest, SA_testReleaseInput_001, TestSize.Level0)
507 {
508 IMSA_HILOGI("InputMethodPrivateMemberTest SA_testReleaseInput_001 TEST START");
509 auto ret = service_->ReleaseInput(nullptr);
510 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
511 }
512
513 /**
514 * @tc.name: III_TestGetCurrentInputMethodSubtype_001
515 * @tc.desc:
516 * @tc.type: FUNC
517 * @tc.require:
518 * @tc.author: chenyu
519 */
520 HWTEST_F(InputMethodPrivateMemberTest, III_TestGetCurrentSubtype_001, TestSize.Level0)
521 {
522 IMSA_HILOGI("InputMethodPrivateMemberTest III_TestGetCurrentInputMethodSubtype_001 TEST START");
523 // currentIme is empty
524 auto currentUserId = TddUtil::GetCurrentUserId();
525 auto subProp = ImeInfoInquirer::GetInstance().GetCurrentSubtype(currentUserId);
526 EXPECT_TRUE(subProp == nullptr);
527
528 // subName is not find
529 auto currentProp = InputMethodController::GetInstance()->GetCurrentInputMethod();
530 ImeCfgManager::GetInstance().imeConfigs_.push_back(
531 { currentUserId, currentProp->name + "/" + currentProp->id, "tt" });
532 subProp = ImeInfoInquirer::GetInstance().GetCurrentSubtype(currentUserId);
533 ASSERT_TRUE(subProp != nullptr);
534 EXPECT_TRUE(subProp->name == currentProp->name);
535
536 // get correct subProp
537 auto currentSubProp = InputMethodController::GetInstance()->GetCurrentInputMethodSubtype();
538 ImeCfgManager::GetInstance().imeConfigs_.push_back(
539 { currentUserId, currentProp->name + "/" + currentProp->id, currentSubProp->id });
540 subProp = ImeInfoInquirer::GetInstance().GetCurrentSubtype(currentUserId);
541 ASSERT_TRUE(subProp != nullptr);
542 EXPECT_TRUE(subProp->id == currentSubProp->id);
543 }
544
545 /**
546 * @tc.name: III_TestGetCurrentInputMethod_001
547 * @tc.desc:
548 * @tc.type: FUNC
549 * @tc.require:
550 * @tc.author: chenyu
551 */
552 HWTEST_F(InputMethodPrivateMemberTest, III_TestGetCurrentIme_001, TestSize.Level0)
553 {
554 IMSA_HILOGI("InputMethodPrivateMemberTest III_TestGetCurrentInputMethod_001 TEST START");
555 // currentIme is empty
556 auto currentUserId = TddUtil::GetCurrentUserId();
557 auto prop = ImeInfoInquirer::GetInstance().GetCurrentInputMethod(currentUserId);
558 EXPECT_TRUE(prop == nullptr);
559
560 // get correct prop
561 auto currentProp = InputMethodController::GetInstance()->GetCurrentInputMethod();
562 ImeCfgManager::GetInstance().imeConfigs_.push_back(
563 { currentUserId, currentProp->name + "/" + currentProp->id, "test" });
564 prop = ImeInfoInquirer::GetInstance().GetCurrentInputMethod(currentUserId);
565 ASSERT_TRUE(prop != nullptr);
566 EXPECT_TRUE(prop->id == currentProp->id);
567 }
568
569 /**
570 * @tc.name: III_TestListEnabledInputMethod_001
571 * @tc.desc:
572 * @tc.type: FUNC
573 * @tc.require:
574 * @tc.author: chenyu
575 */
576 HWTEST_F(InputMethodPrivateMemberTest, III_TestListEnabledInputMethod_001, TestSize.Level0)
577 {
578 IMSA_HILOGI("InputMethodPrivateMemberTest III_TestListEnabledInputMethod_001 TEST START");
579 // currentIme is empty
580 std::vector<Property> props;
581 auto currentUserId = TddUtil::GetCurrentUserId();
582 auto ret = ImeInfoInquirer::GetInstance().ListEnabledInputMethod(currentUserId, props, false);
583 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
584 }
585
586 /**
587 * @tc.name: III_TestListCurrentInputMethodSubtype_001
588 * @tc.desc:
589 * @tc.type: FUNC
590 * @tc.require:
591 * @tc.author: chenyu
592 */
593 HWTEST_F(InputMethodPrivateMemberTest, III_TestListCurrentInputMethodSubtype_001, TestSize.Level0)
594 {
595 IMSA_HILOGI("InputMethodPrivateMemberTest III_TestListCurrentInputMethodSubtype_001 TEST START");
596 // currentIme is empty
597 std::vector<SubProperty> subProps;
598 auto currentUserId = TddUtil::GetCurrentUserId();
599 auto ret = ImeInfoInquirer::GetInstance().ListCurrentInputMethodSubtype(currentUserId, subProps);
600 EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
601 }
602
603 /**
604 * @tc.name: III_TestListInputMethod_001
605 * @tc.desc: status is error
606 * @tc.type: FUNC
607 * @tc.require:
608 * @tc.author: chenyu
609 */
610 HWTEST_F(InputMethodPrivateMemberTest, III_TestListInputMethod_001, TestSize.Level0)
611 {
612 IMSA_HILOGI("InputMethodPrivateMemberTest III_TestListInputMethod_001 TEST START");
613 std::vector<Property> props;
614 auto ret = ImeInfoInquirer::GetInstance().ListInputMethod(60, InputMethodStatus(10), props, false);
615 EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
616 }
617
618 /**
619 * @tc.name: III_TestIsNewExtInfos_001
620 * @tc.desc: has no metadata name = SUBTYPE_PROFILE_METADATA_NAME
621 * @tc.type: FUNC
622 * @tc.require:
623 * @tc.author: chenyu
624 */
625 HWTEST_F(InputMethodPrivateMemberTest, III_TestIsNewExtInfos_001, TestSize.Level0)
626 {
627 IMSA_HILOGI("InputMethodPrivateMemberTest III_TestIsNewExtInfos_001 TEST START");
628 ExtensionAbilityInfo extInfo;
629 std::vector<SubProperty> subProps;
630 auto ret = ImeInfoInquirer::GetInstance().ListInputMethodSubtype(50, extInfo, subProps);
631 EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
632 }
633
634 /**
635 * @tc.name: ICM_TestDeleteImeCfg_001
636 * @tc.desc: delete ime cfg correctly
637 * @tc.type: FUNC
638 * @tc.require:
639 * @tc.author: chenyu
640 */
641 HWTEST_F(InputMethodPrivateMemberTest, ICM_TestDeleteImeCfg_001, TestSize.Level0)
642 {
643 IMSA_HILOGI("InputMethodPrivateMemberTest ICM_TestDeleteImeCfg_001 TEST START");
644 ImeCfgManager::GetInstance().imeConfigs_.push_back({ 100, "testBundleName", "testSubName" });
645 ImeCfgManager::GetInstance().DeleteImeCfg(100);
646 EXPECT_TRUE(ImeCfgManager::GetInstance().imeConfigs_.empty());
647 }
648
649 /**
650 * @tc.name: WMSConnectObserver_001
651 * @tc.desc:
652 * @tc.type: FUNC
653 * @tc.require:
654 * @tc.author: chenyu
655 */
656 HWTEST_F(InputMethodPrivateMemberTest, WMSConnectObserver_001, TestSize.Level0)
657 {
658 IMSA_HILOGI("InputMethodPrivateMemberTest WMSConnectObserver_001 TEST START");
659 WmsConnectionObserver observer(nullptr);
660 WmsConnectionObserver::connectedUserId_.clear();
661 int32_t userId = 100;
662 int32_t screenId = 0;
663
664 observer.OnConnected(userId, screenId);
665 EXPECT_EQ(WmsConnectionObserver::connectedUserId_.size(), 1);
666 EXPECT_TRUE(WmsConnectionObserver::IsWmsConnected(userId));
667
668 int32_t userId1 = 102;
669 observer.OnConnected(userId1, screenId);
670 EXPECT_EQ(WmsConnectionObserver::connectedUserId_.size(), 2);
671 EXPECT_TRUE(WmsConnectionObserver::IsWmsConnected(userId1));
672
673 observer.OnConnected(userId, screenId);
674 EXPECT_EQ(WmsConnectionObserver::connectedUserId_.size(), 2);
675
676 observer.OnDisconnected(userId, screenId);
677 EXPECT_EQ(WmsConnectionObserver::connectedUserId_.size(), 1);
678 EXPECT_FALSE(WmsConnectionObserver::IsWmsConnected(userId));
679 }
680
681 /**
682 * @tc.name: IMC_testDeactivateClient
683 * @tc.desc: DeactivateClient
684 * @tc.type: FUNC
685 * @tc.require:
686 * @tc.author: chenyu
687 */
688 HWTEST_F(InputMethodPrivateMemberTest, IMC_testDeactivateClient, TestSize.Level0)
689 {
690 IMSA_HILOGI("InputMethodPrivateMemberTest IMC_testDeactivateClient Test START");
691 auto imc = InputMethodController::GetInstance();
692 imc->agent_ = std::make_shared<InputMethodAgentStub>();
693 MessageParcel data;
694 data.WriteRemoteObject(imc->agent_->AsObject());
695 imc->agentObject_ = data.ReadRemoteObject();
696 imc->clientInfo_.state = ClientState::ACTIVE;
697 imc->DeactivateClient();
698 EXPECT_EQ(imc->clientInfo_.state, ClientState::INACTIVE);
699 EXPECT_NE(imc->agent_, nullptr);
700 EXPECT_NE(imc->agentObject_, nullptr);
701 }
702
703 /**
704 * @tc.name: testIsPanelShown
705 * @tc.desc: Test Panel Shown.
706 * @tc.type: FUNC
707 */
708 HWTEST_F(InputMethodPrivateMemberTest, testIsPanelShown, TestSize.Level0)
709 {
710 IMSA_HILOGI("InputMethodPrivateMemberTest testIsPanelShown TEST START");
711 auto userSession = std::make_shared<PerUserSession>(MAIN_USER_ID);
712 PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
713 bool flag = true;
714 auto ret = userSession->IsPanelShown(panelInfo, flag);
715 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
716 }
717
718 /**
719 * @tc.name: TestGetDefaultInputMethod_001
720 * @tc.desc: TestGetDefaultInputMethod
721 * @tc.type: FUNC
722 * @tc.require:
723 */
724 HWTEST_F(InputMethodPrivateMemberTest, TestGetDefaultInputMethod_001, TestSize.Level0)
725 {
726 IMSA_HILOGI("InputMethodPrivateMemberTest TestGetDefaultInputMethod_001 TEST START");
727 // currentIme is empty
728 std::shared_ptr<Property> prop;
729 auto currentUserId = TddUtil::GetCurrentUserId();
730 auto ret = ImeInfoInquirer::GetInstance().GetDefaultInputMethod(currentUserId, prop, false);
731 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
732 }
733
734 /**
735 * @tc.name: TestGetDefaultInputMethod_002
736 * @tc.desc: TestGetDefaultInputMethod
737 * @tc.type: FUNC
738 * @tc.require:
739 */
740 HWTEST_F(InputMethodPrivateMemberTest, TestGetDefaultInputMethod_002, TestSize.Level0)
741 {
742 IMSA_HILOGI("InputMethodPrivateMemberTest TestGetDefaultInputMethod_002 TEST START");
743 // currentIme is empty
744 std::shared_ptr<Property> prop;
745 auto currentUserId = TddUtil::GetCurrentUserId();
746 auto ret = ImeInfoInquirer::GetInstance().GetDefaultInputMethod(currentUserId, prop, true);
747 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
748 }
749
750 /**
751 * @tc.name: TestGetResMgr
752 * @tc.desc: GetResMgr
753 * @tc.type: FUNC
754 * @tc.require:
755 */
756 HWTEST_F(InputMethodPrivateMemberTest, TestGetResMgr, TestSize.Level0)
757 {
758 IMSA_HILOGI("InputMethodPrivateMemberTest TestGetResMgr TEST START");
759 // currentIme is empty
760 auto ret = ImeInfoInquirer::GetInstance().GetResMgr("/test");
761 EXPECT_TRUE(ret != nullptr);
762 }
763
764 /**
765 * @tc.name: TestQueryFullImeInfo
766 * @tc.desc: QueryFullImeInfo
767 * @tc.type: FUNC
768 * @tc.require:
769 */
770 HWTEST_F(InputMethodPrivateMemberTest, TestQueryFullImeInfo, TestSize.Level0)
771 {
772 IMSA_HILOGI("InputMethodPrivateMemberTest TestQueryFullImeInfo TEST START");
773 auto currentUserId = TddUtil::GetCurrentUserId();
774 std::vector<FullImeInfo> infos;
775 auto ret = ImeInfoInquirer::GetInstance().QueryFullImeInfo(currentUserId, infos);
776 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
777 }
778
779 /**
780 * @tc.name: TestIsInputMethod
781 * @tc.desc: IsInputMethod
782 * @tc.type: FUNC
783 * @tc.require:
784 */
785 HWTEST_F(InputMethodPrivateMemberTest, TestIsInputMethod, TestSize.Level0)
786 {
787 IMSA_HILOGI("InputMethodPrivateMemberTest TestIsInputMethod TEST START");
788 auto currentUserId = TddUtil::GetCurrentUserId();
789 auto bundleName = "testBundleName1";
790 auto ret = ImeInfoInquirer::GetInstance().IsInputMethod(currentUserId, bundleName);
791 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
792 }
793
794 /**
795 @tc.name: TestHandlePackageEvent
796 @tc.desc: TestHandlePackageEvent
797 @tc.type: FUNC
798 @tc.require:
799 */
800 HWTEST_F(InputMethodPrivateMemberTest, TestHandlePackageEvent, TestSize.Level0)
801 {
802 // msg is nullptr
803 auto *msg = new Message(MessageID::MSG_ID_PACKAGE_REMOVED, nullptr);
804 auto ret = service_->HandlePackageEvent(msg);
805 EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
806 MessageHandler::Instance()->SendMessage(msg);
807
808 // PARCELABLE failed
809 MessageParcel *parcel1 = new (std::nothrow) MessageParcel();
810 auto bundleName = "testBundleName1";
811 parcel1->WriteString(bundleName);
812 auto msg1 = std::make_shared<Message>(MessageID::MSG_ID_PACKAGE_REMOVED, parcel1);
813 auto ret1 = service_->HandlePackageEvent(msg1.get());
814 EXPECT_EQ(ret1, ErrorCode::ERROR_EX_PARCELABLE);
815
816 // userId is not same
817 auto parcel2 = new (std::nothrow) MessageParcel();
818 auto userId = 50;
819 service_->userId_ = 60;
820 parcel2->WriteInt32(userId);
821 parcel2->WriteString(bundleName);
822 auto msg2 = std::make_shared<Message>(MessageID::MSG_ID_PACKAGE_REMOVED, parcel2);
823 auto ret2 = service_->HandlePackageEvent(msg2.get());
824 EXPECT_EQ(ret2, ErrorCode::NO_ERROR);
825
826 //remove bundle not current ime
827 auto parcel3 = new (std::nothrow) MessageParcel();
828 service_->userId_ = userId;
829 ImeCfgManager::GetInstance().imeConfigs_.push_back({ 60, "testBundleName/testExtName", "testSubName" });
830 parcel3->WriteInt32(userId);
831 parcel3->WriteString(bundleName);
832 auto msg3 = std::make_shared<Message>(MessageID::MSG_ID_PACKAGE_REMOVED, parcel3);
833 auto ret3 = service_->HandlePackageEvent(msg3.get());
834 EXPECT_EQ(ret3, ErrorCode::NO_ERROR);
835 }
836 } // namespace MiscServices
837 } // namespace OHOS