1 /*
2 * Copyright (C) 2021-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 #define private public
16 #define protected public
17 #include "input_method_controller.h"
18
19 #include "ime_enabled_info_manager.h"
20 #include "ime_info_inquirer.h"
21 #include "input_data_channel_stub.h"
22 #include "input_method_ability.h"
23 #include "input_method_system_ability.h"
24 #include "key_event_result_handler.h"
25 #include "task_manager.h"
26 #undef private
27
28 #include <event_handler.h>
29 #include <gtest/gtest.h>
30 #include <string_ex.h>
31 #include <sys/time.h>
32
33 #include <condition_variable>
34 #include <csignal>
35 #include <cstdint>
36 #include <functional>
37 #include <gtest/hwext/gtest-multithread.h>
38 #include <mutex>
39 #include <string>
40 #include <thread>
41 #include <unordered_map>
42 #include <vector>
43
44 #include "ability_manager_client.h"
45 #include "block_data.h"
46 #include "global.h"
47 #include "iinput_method_agent.h"
48 #include "iinput_method_system_ability.h"
49 #include "identity_checker_mock.h"
50 #include "if_system_ability_manager.h"
51 #include "input_client_stub.h"
52 #include "input_client_service_impl.h"
53 #include "input_death_recipient.h"
54 #include "input_event_callback.h"
55 #include "input_method_ability.h"
56 #include "input_method_engine_listener_impl.h"
57 #include "input_data_channel_service_impl.h"
58 #include "input_method_system_ability_proxy.h"
59 #include "input_method_utils.h"
60 #include "iservice_registry.h"
61 #include "key_event_util.h"
62 #include "keyboard_listener.h"
63 #include "message_parcel.h"
64 #include "scope_utils.h"
65 #include "system_ability.h"
66 #include "system_ability_definition.h"
67 #include "tdd_util.h"
68 #include "text_listener.h"
69 using namespace testing;
70 using namespace testing::ext;
71 using namespace testing::mt;
72 namespace OHOS {
73 namespace MiscServices {
74 constexpr uint32_t RETRY_TIME = 200 * 1000;
75 constexpr uint32_t RETRY_TIMES = 5;
76 constexpr uint32_t WAIT_INTERVAL = 500;
77 constexpr size_t PRIVATE_COMMAND_SIZE_MAX = 32 * 1024;
78 constexpr uint32_t WAIT_SA_DIE_TIME_OUT = 3;
79 const std::string IME_KEY = "settings.inputmethod.enable_ime";
80
81 class SelectListenerMock : public ControllerListener {
82 public:
83 SelectListenerMock() = default;
84 ~SelectListenerMock() override = default;
85
OnSelectByRange(int32_t start,int32_t end)86 void OnSelectByRange(int32_t start, int32_t end) override
87 {
88 start_ = start;
89 end_ = end;
90 selectListenerCv_.notify_all();
91 }
92
OnSelectByMovement(int32_t direction)93 void OnSelectByMovement(int32_t direction) override
94 {
95 direction_ = direction;
96 selectListenerCv_.notify_all();
97 }
98 static void WaitSelectListenerCallback();
99 static int32_t start_;
100 static int32_t end_;
101 static int32_t direction_;
102 static std::mutex selectListenerMutex_;
103 static std::condition_variable selectListenerCv_;
104 };
105
106 int32_t SelectListenerMock::start_ = 0;
107 int32_t SelectListenerMock::end_ = 0;
108 int32_t SelectListenerMock::direction_ = 0;
109 std::mutex SelectListenerMock::selectListenerMutex_;
110 std::condition_variable SelectListenerMock::selectListenerCv_;
WaitSelectListenerCallback()111 void SelectListenerMock::WaitSelectListenerCallback()
112 {
113 std::unique_lock<std::mutex> lock(selectListenerMutex_);
114 selectListenerCv_.wait_for(lock, std::chrono::milliseconds(WAIT_INTERVAL));
115 }
116
117 class InputMethodControllerTest : public testing::Test {
118 public:
119 static void SetUpTestCase(void);
120 static void TearDownTestCase(void);
121 void SetUp();
122 void TearDown();
123 static void SetInputDeathRecipient();
124 static void OnRemoteSaDied(const wptr<IRemoteObject> &remote);
125 static void CheckKeyEvent(std::shared_ptr<MMI::KeyEvent> keyEvent);
126 static bool WaitRemoteDiedCallback();
127 static bool WaitKeyboardStatus(bool keyboardState);
128 static void TriggerConfigurationChangeCallback(Configuration &info);
129 static void TriggerCursorUpdateCallback(CursorInfo &info);
130 static void TriggerSelectionChangeCallback(std::u16string &text, int start, int end);
131 static void CheckProxyObject();
132 static void DispatchKeyEventCallback(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed);
133 static bool WaitKeyEventCallback();
134 static void CheckTextConfig(const TextConfig &config);
135 static void ResetKeyboardListenerTextConfig();
136 static void EditorContentMultiTest();
137 static sptr<InputMethodController> GetController();
138 static sptr<OnTextChangedListener> GetTextListener();
139 static std::mutex controllerMutex_;
140 static sptr<InputMethodController> inputMethodController_;
141 static InputMethodAbility &inputMethodAbility_;
142 static sptr<InputMethodSystemAbility> imsa_;
143 static sptr<InputMethodSystemAbilityProxy> imsaProxy_;
144 static std::shared_ptr<MMI::KeyEvent> keyEvent_;
145 static std::shared_ptr<InputMethodEngineListenerImpl> imeListener_;
146 static std::shared_ptr<SelectListenerMock> controllerListener_;
147 static std::mutex textListenerMutex_;
148 static sptr<OnTextChangedListener> textListener_;
149 static std::mutex keyboardListenerMutex_;
150 static std::condition_variable keyboardListenerCv_;
151 static BlockData<std::shared_ptr<MMI::KeyEvent>> blockKeyEvent_;
152 static BlockData<std::shared_ptr<MMI::KeyEvent>> blockFullKeyEvent_;
153 static BlockData<bool> isNotifyFinished_;
154 static std::mutex onRemoteSaDiedMutex_;
155 static std::condition_variable onRemoteSaDiedCv_;
156 static sptr<InputDeathRecipient> deathRecipient_;
157 static CursorInfo cursorInfo_;
158 static int32_t oldBegin_;
159 static int32_t oldEnd_;
160 static int32_t newBegin_;
161 static int32_t newEnd_;
162 static std::string text_;
163 static bool doesKeyEventConsume_;
164 static bool doesFUllKeyEventConsume_;
165 static std::condition_variable keyEventCv_;
166 static std::mutex keyEventLock_;
167 static bool consumeResult_;
168 static InputAttribute inputAttribute_;
169 static std::shared_ptr<AppExecFwk::EventHandler> textConfigHandler_;
170 static constexpr uint32_t DELAY_TIME = 1;
171 static constexpr uint32_t KEY_EVENT_DELAY_TIME = 100;
172 static constexpr int32_t TASK_DELAY_TIME = 10;
173 static constexpr int32_t EACH_THREAD_CIRCULATION_TIME = 100;
174 static constexpr int32_t THREAD_NUM = 5;
175 static std::atomic<int32_t> multiThreadExecTotalNum_;
176
177 class KeyboardListenerImpl : public KeyboardListener {
178 public:
KeyboardListenerImpl()179 KeyboardListenerImpl()
180 {
181 std::shared_ptr<AppExecFwk::EventRunner> runner = AppExecFwk::EventRunner::Create("InputMethodControllerTe"
182 "st");
183 textConfigHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
184 };
~KeyboardListenerImpl()185 ~KeyboardListenerImpl() {};
OnKeyEvent(int32_t keyCode,int32_t keyStatus,sptr<KeyEventConsumerProxy> & consumer)186 bool OnKeyEvent(int32_t keyCode, int32_t keyStatus, sptr<KeyEventConsumerProxy> &consumer) override
187 {
188 if (!doesKeyEventConsume_) {
189 IMSA_HILOGI("KeyboardListenerImpl doesKeyEventConsume_ false");
190 return false;
191 }
192 IMSA_HILOGI("KeyboardListenerImpl::OnKeyEvent %{public}d %{public}d", keyCode, keyStatus);
193 auto keyEvent = KeyEventUtil::CreateKeyEvent(keyCode, keyStatus);
194 blockKeyEvent_.SetValue(keyEvent);
195 return true;
196 }
OnKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,sptr<KeyEventConsumerProxy> & consumer)197 bool OnKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer) override
198 {
199 if (!doesFUllKeyEventConsume_) {
200 IMSA_HILOGI("KeyboardListenerImpl doesFUllKeyEventConsume_ false");
201 return false;
202 }
203 IMSA_HILOGI("KeyboardListenerImpl::OnKeyEvent %{public}d %{public}d", keyEvent->GetKeyCode(),
204 keyEvent->GetKeyAction());
205 auto fullKey = keyEvent;
206 blockFullKeyEvent_.SetValue(fullKey);
207 return true;
208 }
OnDealKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,uint64_t cbId,const sptr<IRemoteObject> & channelObject)209 bool OnDealKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, uint64_t cbId,
210 const sptr<IRemoteObject> &channelObject) override
211 {
212 IMSA_HILOGI("KeyboardListenerImpl run in");
213 sptr<KeyEventConsumerProxy> consumer = new (std::nothrow) KeyEventConsumerProxy(nullptr);
214 bool isKeyCodeConsume = OnKeyEvent(keyEvent->GetKeyCode(), keyEvent->GetKeyAction(), consumer);
215 bool isKeyEventConsume = OnKeyEvent(keyEvent, consumer);
216 InputMethodAbility::GetInstance().HandleKeyEventResult(
217 cbId, isKeyEventConsume | isKeyCodeConsume, channelObject);
218 return true;
219 }
OnCursorUpdate(int32_t positionX,int32_t positionY,int32_t height)220 void OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) override
221 {
222 IMSA_HILOGI("KeyboardListenerImpl %{public}d %{public}d %{public}d", positionX, positionY, height);
223 cursorInfo_ = { static_cast<double>(positionX), static_cast<double>(positionY), 0,
224 static_cast<double>(height) };
225 InputMethodControllerTest::keyboardListenerCv_.notify_one();
226 }
OnSelectionChange(int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)227 void OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) override
228 {
229 IMSA_HILOGI(
230 "KeyboardListenerImpl %{public}d %{public}d %{public}d %{public}d", oldBegin, oldEnd, newBegin, newEnd);
231 oldBegin_ = oldBegin;
232 oldEnd_ = oldEnd;
233 newBegin_ = newBegin;
234 newEnd_ = newEnd;
235 InputMethodControllerTest::keyboardListenerCv_.notify_one();
236 }
OnTextChange(const std::string & text)237 void OnTextChange(const std::string &text) override
238 {
239 IMSA_HILOGI("KeyboardListenerImpl text: %{public}s", text.c_str());
240 text_ = text;
241 InputMethodControllerTest::keyboardListenerCv_.notify_one();
242 }
OnEditorAttributeChange(const InputAttribute & inputAttribute)243 void OnEditorAttributeChange(const InputAttribute &inputAttribute) override
244 {
245 IMSA_HILOGI("KeyboardListenerImpl inputPattern: %{public}d, enterKeyType: %{public}d, "
246 "isTextPreviewSupported: %{public}d",
247 inputAttribute.inputPattern, inputAttribute.enterKeyType, inputAttribute.isTextPreviewSupported);
248 inputAttribute_ = inputAttribute;
249 InputMethodControllerTest::keyboardListenerCv_.notify_one();
250 }
251 };
252 };
253 std::mutex InputMethodControllerTest::controllerMutex_;
254 sptr<InputMethodController> InputMethodControllerTest::inputMethodController_;
255 InputMethodAbility &InputMethodControllerTest::inputMethodAbility_ = InputMethodAbility::GetInstance();
256 sptr<InputMethodSystemAbility> InputMethodControllerTest::imsa_;
257 sptr<InputMethodSystemAbilityProxy> InputMethodControllerTest::imsaProxy_;
258 std::shared_ptr<MMI::KeyEvent> InputMethodControllerTest::keyEvent_;
259 std::shared_ptr<InputMethodEngineListenerImpl> InputMethodControllerTest::imeListener_;
260 std::shared_ptr<SelectListenerMock> InputMethodControllerTest::controllerListener_;
261 std::mutex InputMethodControllerTest::textListenerMutex_;
262 sptr<OnTextChangedListener> InputMethodControllerTest::textListener_;
263 CursorInfo InputMethodControllerTest::cursorInfo_ = {};
264 int32_t InputMethodControllerTest::oldBegin_ = 0;
265 int32_t InputMethodControllerTest::oldEnd_ = 0;
266 int32_t InputMethodControllerTest::newBegin_ = 0;
267 int32_t InputMethodControllerTest::newEnd_ = 0;
268 std::string InputMethodControllerTest::text_;
269 InputAttribute InputMethodControllerTest::inputAttribute_;
270 std::mutex InputMethodControllerTest::keyboardListenerMutex_;
271 std::condition_variable InputMethodControllerTest::keyboardListenerCv_;
272 sptr<InputDeathRecipient> InputMethodControllerTest::deathRecipient_;
273 std::mutex InputMethodControllerTest::onRemoteSaDiedMutex_;
274 std::condition_variable InputMethodControllerTest::onRemoteSaDiedCv_;
275 BlockData<std::shared_ptr<MMI::KeyEvent>> InputMethodControllerTest::blockKeyEvent_ {
276 InputMethodControllerTest::KEY_EVENT_DELAY_TIME, nullptr
277 };
278 BlockData<std::shared_ptr<MMI::KeyEvent>> InputMethodControllerTest::blockFullKeyEvent_ {
279 InputMethodControllerTest::KEY_EVENT_DELAY_TIME, nullptr
280 };
281 BlockData<bool> InputMethodControllerTest::isNotifyFinished_ {
282 false, PerUserSession::MAX_NOTIFY_TIME
283 };
284 bool InputMethodControllerTest::doesKeyEventConsume_ { false };
285 bool InputMethodControllerTest::doesFUllKeyEventConsume_ { false };
286 std::condition_variable InputMethodControllerTest::keyEventCv_;
287 std::mutex InputMethodControllerTest::keyEventLock_;
288 bool InputMethodControllerTest::consumeResult_ { false };
289 std::shared_ptr<AppExecFwk::EventHandler> InputMethodControllerTest::textConfigHandler_ { nullptr };
290 std::atomic<int32_t> InputMethodControllerTest::multiThreadExecTotalNum_{ 0 };
291
SetUpTestCase(void)292 void InputMethodControllerTest::SetUpTestCase(void)
293 {
294 IMSA_HILOGI("InputMethodControllerTest::SetUpTestCase");
295 IdentityCheckerMock::ResetParam();
296 imsa_ = new (std::nothrow) InputMethodSystemAbility();
297 if (imsa_ == nullptr) {
298 return;
299 }
300 imsa_->OnStart();
301 imsa_->userId_ = TddUtil::GetCurrentUserId();
302 imsa_->identityChecker_ = std::make_shared<IdentityCheckerMock>();
303 sptr<InputMethodSystemAbilityStub> serviceStub = imsa_;
304 imsaProxy_ = new (std::nothrow) InputMethodSystemAbilityProxy(serviceStub->AsObject());
305 if (imsaProxy_ == nullptr) {
306 return;
307 }
308 IdentityCheckerMock::SetFocused(true);
309
310 inputMethodAbility_.abilityManager_ = imsaProxy_;
311 TddUtil::InitCurrentImePermissionInfo();
312 IdentityCheckerMock::SetBundleName(TddUtil::currentBundleNameMock_);
313 inputMethodAbility_.SetCoreAndAgent();
314 controllerListener_ = std::make_shared<SelectListenerMock>();
315 textListener_ = new TextListener();
316 inputMethodAbility_.SetKdListener(std::make_shared<KeyboardListenerImpl>());
317 imeListener_ = std::make_shared<InputMethodEngineListenerImpl>(textConfigHandler_);
318 inputMethodAbility_.SetImeListener(imeListener_);
319
320 inputMethodController_ = InputMethodController::GetInstance();
321 inputMethodController_->abilityManager_ = imsaProxy_;
322
323 keyEvent_ = KeyEventUtil::CreateKeyEvent(MMI::KeyEvent::KEYCODE_A, MMI::KeyEvent::KEY_ACTION_DOWN);
324 keyEvent_->SetFunctionKey(MMI::KeyEvent::NUM_LOCK_FUNCTION_KEY, 0);
325 keyEvent_->SetFunctionKey(MMI::KeyEvent::CAPS_LOCK_FUNCTION_KEY, 1);
326 keyEvent_->SetFunctionKey(MMI::KeyEvent::SCROLL_LOCK_FUNCTION_KEY, 1);
327
328 SetInputDeathRecipient();
329 TextListener::ResetParam();
330 }
331
TearDownTestCase(void)332 void InputMethodControllerTest::TearDownTestCase(void)
333 {
334 IMSA_HILOGI("InputMethodControllerTest::TearDownTestCase");
335 TextListener::ResetParam();
336 inputMethodController_->SetControllerListener(nullptr);
337 IdentityCheckerMock::ResetParam();
338 imsa_->OnStop();
339 }
340
SetUp(void)341 void InputMethodControllerTest::SetUp(void)
342 {
343 IMSA_HILOGI("InputMethodControllerTest::SetUp");
344 TaskManager::GetInstance().SetInited(true);
345 }
346
TearDown(void)347 void InputMethodControllerTest::TearDown(void)
348 {
349 IMSA_HILOGI("InputMethodControllerTest::TearDown");
350 std::this_thread::sleep_for(std::chrono::seconds(1));
351 TaskManager::GetInstance().Reset();
352 }
353
SetInputDeathRecipient()354 void InputMethodControllerTest::SetInputDeathRecipient()
355 {
356 IMSA_HILOGI("InputMethodControllerTest::SetInputDeathRecipient");
357 sptr<ISystemAbilityManager> systemAbilityManager =
358 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
359 if (systemAbilityManager == nullptr) {
360 IMSA_HILOGI("InputMethodControllerTest, system ability manager is nullptr");
361 return;
362 }
363 auto systemAbility = systemAbilityManager->GetSystemAbility(INPUT_METHOD_SYSTEM_ABILITY_ID, "");
364 if (systemAbility == nullptr) {
365 IMSA_HILOGI("InputMethodControllerTest, system ability is nullptr");
366 return;
367 }
368 deathRecipient_ = new (std::nothrow) InputDeathRecipient();
369 if (deathRecipient_ == nullptr) {
370 IMSA_HILOGE("InputMethodControllerTest, new death recipient failed");
371 return;
372 }
373 deathRecipient_->SetDeathRecipient([](const wptr<IRemoteObject> &remote) {
374 OnRemoteSaDied(remote);
375 });
376 if ((systemAbility->IsProxyObject()) && (!systemAbility->AddDeathRecipient(deathRecipient_))) {
377 IMSA_HILOGE("InputMethodControllerTest, failed to add death recipient.");
378 return;
379 }
380 }
381
OnRemoteSaDied(const wptr<IRemoteObject> & remote)382 void InputMethodControllerTest::OnRemoteSaDied(const wptr<IRemoteObject> &remote)
383 {
384 IMSA_HILOGI("InputMethodControllerTest::OnRemoteSaDied");
385 onRemoteSaDiedCv_.notify_one();
386 }
387
WaitRemoteDiedCallback()388 bool InputMethodControllerTest::WaitRemoteDiedCallback()
389 {
390 IMSA_HILOGI("InputMethodControllerTest::WaitRemoteDiedCallback");
391 std::unique_lock<std::mutex> lock(onRemoteSaDiedMutex_);
392 return onRemoteSaDiedCv_.wait_for(lock, std::chrono::seconds(WAIT_SA_DIE_TIME_OUT)) != std::cv_status::timeout;
393 }
394
CheckProxyObject()395 void InputMethodControllerTest::CheckProxyObject()
396 {
397 for (uint32_t retryTimes = 0; retryTimes < RETRY_TIMES; ++retryTimes) {
398 IMSA_HILOGI("times = %{public}d", retryTimes);
399 sptr<ISystemAbilityManager> systemAbilityManager =
400 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
401 if (systemAbilityManager == nullptr) {
402 IMSA_HILOGI("system ability manager is nullptr");
403 continue;
404 }
405 auto systemAbility = systemAbilityManager->CheckSystemAbility(INPUT_METHOD_SYSTEM_ABILITY_ID);
406 if (systemAbility != nullptr) {
407 IMSA_HILOGI("CheckProxyObject success!");
408 break;
409 }
410 usleep(RETRY_TIME);
411 }
412 }
413
CheckKeyEvent(std::shared_ptr<MMI::KeyEvent> keyEvent)414 void InputMethodControllerTest::CheckKeyEvent(std::shared_ptr<MMI::KeyEvent> keyEvent)
415 {
416 ASSERT_NE(keyEvent, nullptr);
417 bool ret = keyEvent->GetKeyCode() == keyEvent_->GetKeyCode();
418 EXPECT_TRUE(ret);
419 ret = keyEvent->GetKeyAction() == keyEvent_->GetKeyAction();
420 EXPECT_TRUE(ret);
421 ret = keyEvent->GetKeyIntention() == keyEvent_->GetKeyIntention();
422 EXPECT_TRUE(ret);
423 // check function key state
424 ret = keyEvent->GetFunctionKey(MMI::KeyEvent::NUM_LOCK_FUNCTION_KEY) ==
425 keyEvent_->GetFunctionKey(MMI::KeyEvent::NUM_LOCK_FUNCTION_KEY);
426 EXPECT_TRUE(ret);
427 ret = keyEvent->GetFunctionKey(MMI::KeyEvent::CAPS_LOCK_FUNCTION_KEY) ==
428 keyEvent_->GetFunctionKey(MMI::KeyEvent::CAPS_LOCK_FUNCTION_KEY);
429 EXPECT_TRUE(ret);
430 ret = keyEvent->GetFunctionKey(MMI::KeyEvent::SCROLL_LOCK_FUNCTION_KEY) ==
431 keyEvent_->GetFunctionKey(MMI::KeyEvent::SCROLL_LOCK_FUNCTION_KEY);
432 EXPECT_TRUE(ret);
433 // check KeyItem
434 ret = keyEvent->GetKeyItems().size() == keyEvent_->GetKeyItems().size();
435 EXPECT_TRUE(ret);
436 ret = keyEvent->GetKeyItem()->GetKeyCode() == keyEvent_->GetKeyItem()->GetKeyCode();
437 EXPECT_TRUE(ret);
438 ret = keyEvent->GetKeyItem()->GetDownTime() == keyEvent_->GetKeyItem()->GetDownTime();
439 EXPECT_TRUE(ret);
440 ret = keyEvent->GetKeyItem()->GetDeviceId() == keyEvent_->GetKeyItem()->GetDeviceId();
441 EXPECT_TRUE(ret);
442 ret = keyEvent->GetKeyItem()->IsPressed() == keyEvent_->GetKeyItem()->IsPressed();
443 EXPECT_TRUE(ret);
444 ret = keyEvent->GetKeyItem()->GetUnicode() == keyEvent_->GetKeyItem()->GetUnicode();
445 EXPECT_TRUE(ret);
446 }
447
WaitKeyboardStatus(bool keyboardState)448 bool InputMethodControllerTest::WaitKeyboardStatus(bool keyboardState)
449 {
450 return InputMethodEngineListenerImpl::WaitKeyboardStatus(keyboardState);
451 }
452
TriggerConfigurationChangeCallback(Configuration & info)453 void InputMethodControllerTest::TriggerConfigurationChangeCallback(Configuration &info)
454 {
455 textConfigHandler_->PostTask(
456 [info]() {
457 inputMethodController_->OnConfigurationChange(info);
458 },
459 InputMethodControllerTest::TASK_DELAY_TIME, AppExecFwk::EventQueue::Priority::VIP);
460 {
461 std::unique_lock<std::mutex> lock(InputMethodControllerTest::keyboardListenerMutex_);
462 InputMethodControllerTest::keyboardListenerCv_.wait_for(
463 lock, std::chrono::seconds(InputMethodControllerTest::DELAY_TIME), [&info] {
464 return (static_cast<OHOS::MiscServices::TextInputType>(
465 InputMethodControllerTest::inputAttribute_.inputPattern) == info.GetTextInputType()) &&
466 (static_cast<OHOS::MiscServices::EnterKeyType>(
467 InputMethodControllerTest::inputAttribute_.enterKeyType) == info.GetEnterKeyType());
468 });
469 }
470 }
471
TriggerCursorUpdateCallback(CursorInfo & info)472 void InputMethodControllerTest::TriggerCursorUpdateCallback(CursorInfo &info)
473 {
474 textConfigHandler_->PostTask(
475 [info]() {
476 inputMethodController_->OnCursorUpdate(info);
477 },
478 InputMethodControllerTest::TASK_DELAY_TIME, AppExecFwk::EventQueue::Priority::VIP);
479 {
480 std::unique_lock<std::mutex> lock(InputMethodControllerTest::keyboardListenerMutex_);
481 InputMethodControllerTest::keyboardListenerCv_.wait_for(
482 lock, std::chrono::seconds(InputMethodControllerTest::DELAY_TIME), [&info] {
483 return InputMethodControllerTest::cursorInfo_ == info;
484 });
485 }
486 }
487
TriggerSelectionChangeCallback(std::u16string & text,int start,int end)488 void InputMethodControllerTest::TriggerSelectionChangeCallback(std::u16string &text, int start, int end)
489 {
490 IMSA_HILOGI("InputMethodControllerTest run in");
491 textConfigHandler_->PostTask(
492 [text, start, end]() {
493 inputMethodController_->OnSelectionChange(text, start, end);
494 },
495 InputMethodControllerTest::TASK_DELAY_TIME, AppExecFwk::EventQueue::Priority::VIP);
496 {
497 std::unique_lock<std::mutex> lock(InputMethodControllerTest::keyboardListenerMutex_);
498 InputMethodControllerTest::keyboardListenerCv_.wait_for(
499 lock, std::chrono::seconds(InputMethodControllerTest::DELAY_TIME), [&text, start, end] {
500 return InputMethodControllerTest::text_ == Str16ToStr8(text) &&
501 InputMethodControllerTest::newBegin_ == start && InputMethodControllerTest::newEnd_ == end;
502 });
503 }
504 IMSA_HILOGI("InputMethodControllerTest end");
505 }
506
CheckTextConfig(const TextConfig & config)507 void InputMethodControllerTest::CheckTextConfig(const TextConfig &config)
508 {
509 std::this_thread::sleep_for(std::chrono::seconds(1));
510 EXPECT_EQ(imeListener_->windowId_, config.windowId);
511 EXPECT_EQ(cursorInfo_.left, config.cursorInfo.left);
512 EXPECT_EQ(cursorInfo_.top, config.cursorInfo.top);
513 EXPECT_EQ(cursorInfo_.height, config.cursorInfo.height);
514 EXPECT_EQ(newBegin_, config.range.start);
515 EXPECT_EQ(newEnd_, config.range.end);
516 EXPECT_EQ(inputAttribute_.inputPattern, config.inputAttribute.inputPattern);
517 EXPECT_EQ(inputAttribute_.enterKeyType, config.inputAttribute.enterKeyType);
518 }
519
DispatchKeyEventCallback(std::shared_ptr<MMI::KeyEvent> & keyEvent,bool isConsumed)520 void InputMethodControllerTest::DispatchKeyEventCallback(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed)
521 {
522 IMSA_HILOGI("InputMethodControllerTest isConsumed: %{public}d", isConsumed);
523 consumeResult_ = isConsumed;
524 keyEventCv_.notify_one();
525 }
526
WaitKeyEventCallback()527 bool InputMethodControllerTest::WaitKeyEventCallback()
528 {
529 std::unique_lock<std::mutex> lock(keyEventLock_);
530 keyEventCv_.wait_for(lock, std::chrono::seconds(DELAY_TIME), [] {
531 return consumeResult_;
532 });
533 return consumeResult_;
534 }
535
ResetKeyboardListenerTextConfig()536 void InputMethodControllerTest::ResetKeyboardListenerTextConfig()
537 {
538 cursorInfo_ = {};
539 oldBegin_ = INVALID_VALUE;
540 oldEnd_ = INVALID_VALUE;
541 newBegin_ = INVALID_VALUE;
542 newEnd_ = INVALID_VALUE;
543 text_ = "";
544 inputAttribute_ = {};
545 }
546
EditorContentMultiTest()547 void InputMethodControllerTest::EditorContentMultiTest()
548 {
549 for (int32_t i = 0; i < EACH_THREAD_CIRCULATION_TIME; i++) {
550 InputAttribute inputAttribute = { .isTextPreviewSupported = true };
551 auto controller = GetController();
552 ASSERT_NE(controller, nullptr);
553 auto listener = GetTextListener();
554 ASSERT_NE(listener, nullptr);
555 controller->Attach(listener, false, inputAttribute);
556 std::u16string text = Str8ToStr16("testSelect");
557 int start = 1;
558 int end = 2;
559 controller->OnSelectionChange(text, start, end);
560 controller->OnInputStop();
561 multiThreadExecTotalNum_.fetch_add(1, std::memory_order_relaxed);
562 }
563 }
564
GetController()565 sptr<InputMethodController> InputMethodControllerTest::GetController()
566 {
567 std::lock_guard<std::mutex> lock(controllerMutex_);
568 return inputMethodController_;
569 }
570
GetTextListener()571 sptr<OnTextChangedListener> InputMethodControllerTest::GetTextListener()
572 {
573 std::lock_guard<std::mutex> lock(textListenerMutex_);
574 return textListener_;
575 }
576
577 /**
578 * @tc.name: testIMCAttach001
579 * @tc.desc: IMC Attach.
580 * @tc.type: FUNC
581 * @tc.require:
582 */
583 HWTEST_F(InputMethodControllerTest, testIMCAttach001, TestSize.Level0)
584 {
585 IMSA_HILOGI("IMC testIMCAttach001 Test START");
586 imeListener_->isInputStart_ = false;
587 TextListener::ResetParam();
588 inputMethodController_->Attach(textListener_, false);
589 inputMethodController_->Attach(textListener_);
590 inputMethodController_->Attach(textListener_, true);
591 std::this_thread::sleep_for(std::chrono::seconds(2));
592 EXPECT_EQ(TextListener::keyboardStatus_, KeyboardStatus::SHOW);
593 EXPECT_TRUE(imeListener_->isInputStart_ && imeListener_->keyboardState_);
594 }
595
596 /**
597 * @tc.name: testIMCAttach002
598 * @tc.desc: IMC Attach.
599 * @tc.type: FUNC
600 * @tc.require:
601 */
602 HWTEST_F(InputMethodControllerTest, testIMCAttach002, TestSize.Level0)
603 {
604 IMSA_HILOGI("IMC testIMCAttach002 Test START");
605 TextListener::ResetParam();
606 CursorInfo cursorInfo = { 1, 1, 1, 1 };
607 Range selectionRange = { 1, 2 };
608 InputAttribute attribute = { 1, 1 };
609 uint32_t windowId = 10;
610 TextConfig textConfig = {
611 .inputAttribute = attribute, .cursorInfo = cursorInfo, .range = selectionRange, .windowId = windowId
612 };
613
614 inputMethodController_->Attach(textListener_, true, textConfig);
615 InputMethodControllerTest::CheckTextConfig(textConfig);
616
617 TextListener::ResetParam();
618 cursorInfo = { 2, 2, 2, 2 };
619 selectionRange = { 3, 4 };
620 attribute = { 2, 2 };
621 windowId = 11;
622 textConfig = {
623 .inputAttribute = attribute, .cursorInfo = cursorInfo, .range = selectionRange, .windowId = windowId
624 };
625 inputMethodController_->Attach(textListener_, true, textConfig);
626 InputMethodControllerTest::CheckTextConfig(textConfig);
627 }
628
629 /**
630 * @tc.name: testIMCSetCallingWindow
631 * @tc.desc: IMC SetCallingWindow.
632 * @tc.type: FUNC
633 * @tc.require:
634 */
635 HWTEST_F(InputMethodControllerTest, testIMCSetCallingWindow, TestSize.Level0)
636 {
637 IMSA_HILOGI("IMC SetCallingWindow Test START");
638 auto ret = inputMethodController_->Attach(textListener_);
639 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
640 uint32_t windowId = 3;
641 ret = inputMethodController_->SetCallingWindow(windowId);
642 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
643 EXPECT_EQ(windowId, imeListener_->windowId_);
644 }
645
646 /**
647 * @tc.name: testIMCDiscardTypingText
648 * @tc.desc: IMC DiscardTypingText.
649 * @tc.type: FUNC
650 * @tc.require:
651 */
652 HWTEST_F(InputMethodControllerTest, testIMCDiscardTypingText, TestSize.Level0)
653 {
654 IMSA_HILOGI("IMC DiscardTypingText Test START");
655 InputMethodControllerTest::inputMethodController_->Close();
656 auto ret = inputMethodController_->DiscardTypingText();
657 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
658 ret = inputMethodController_->Attach(textListener_);
659 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
660 ret = inputMethodController_->DiscardTypingText();
661 EXPECT_GE(ret, ErrorCode::NO_ERROR);
662 }
663
664 /**
665 * @tc.name: testGetIMSAProxy
666 * @tc.desc: Get Imsa Proxy.
667 * @tc.type: FUNC
668 */
669 HWTEST_F(InputMethodControllerTest, testGetIMSAProxy, TestSize.Level0)
670 {
671 auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
672 ASSERT_NE(systemAbilityManager, nullptr);
673 auto systemAbility = systemAbilityManager->GetSystemAbility(INPUT_METHOD_SYSTEM_ABILITY_ID, "");
674 EXPECT_TRUE(systemAbility != nullptr);
675 }
676
677 /**
678 * @tc.name: testWriteReadIInputDataChannel
679 * @tc.desc: Checkout IInputDataChannel.
680 * @tc.type: FUNC
681 */
682 HWTEST_F(InputMethodControllerTest, testWriteReadIInputDataChannel, TestSize.Level0)
683 {
684 sptr<InputDataChannelStub> mInputDataChannel = new InputDataChannelServiceImpl();
685 MessageParcel data;
686 auto ret = data.WriteRemoteObject(mInputDataChannel->AsObject());
687 EXPECT_TRUE(ret);
688 auto remoteObject = data.ReadRemoteObject();
689 sptr<IInputDataChannel> iface = iface_cast<IInputDataChannel>(remoteObject);
690 EXPECT_TRUE(iface != nullptr);
691 }
692
693 /**
694 * @tc.name: testIMCBindToIMSA
695 * @tc.desc: Bind IMSA.
696 * @tc.type: FUNC
697 */
698 HWTEST_F(InputMethodControllerTest, testIMCBindToIMSA, TestSize.Level0)
699 {
700 sptr<InputClientStub> mClient = new InputClientServiceImpl();
701 MessageParcel data;
702 auto ret = data.WriteRemoteObject(mClient->AsObject());
703 EXPECT_TRUE(ret);
704 auto remoteObject = data.ReadRemoteObject();
705 sptr<IInputClient> iface = iface_cast<IInputClient>(remoteObject);
706 EXPECT_TRUE(iface != nullptr);
707 }
708
709 /**
710 * @tc.name: testIMCDispatchKeyEvent001
711 * @tc.desc: test IMC DispatchKeyEvent with 'keyDown/KeyUP'.
712 * @tc.type: FUNC
713 * @tc.require:
714 */
715 HWTEST_F(InputMethodControllerTest, testIMCDispatchKeyEvent001, TestSize.Level0)
716 {
717 IMSA_HILOGI("IMC testIMCDispatchKeyEvent001 Test START");
718 doesKeyEventConsume_ = true;
719 doesFUllKeyEventConsume_ = false;
720 blockKeyEvent_.Clear(nullptr);
721 auto res = inputMethodController_->Attach(textListener_);
722 EXPECT_EQ(res, ErrorCode::NO_ERROR);
723
724 bool ret = inputMethodController_->DispatchKeyEvent(keyEvent_, DispatchKeyEventCallback);
725 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
726
727 EXPECT_TRUE(WaitKeyEventCallback());
728
729 auto keyEvent = blockKeyEvent_.GetValue();
730 ASSERT_NE(keyEvent, nullptr);
731 EXPECT_EQ(keyEvent->GetKeyCode(), keyEvent_->GetKeyCode());
732 EXPECT_EQ(keyEvent->GetKeyAction(), keyEvent_->GetKeyAction());
733
734 doesKeyEventConsume_ = false;
735 ret = inputMethodController_->DispatchKeyEvent(keyEvent_, DispatchKeyEventCallback);
736 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
737
738 EXPECT_TRUE(!WaitKeyEventCallback());
739 }
740
741 /**
742 * @tc.name: testIMCDispatchKeyEvent002
743 * @tc.desc: test IMC DispatchKeyEvent with 'keyEvent'.
744 * @tc.type: FUNC
745 * @tc.require:
746 */
747 HWTEST_F(InputMethodControllerTest, testIMCDispatchKeyEvent002, TestSize.Level0)
748 {
749 IMSA_HILOGI("IMC testIMCDispatchKeyEvent002 Test START");
750 doesKeyEventConsume_ = false;
751 doesFUllKeyEventConsume_ = true;
752 blockFullKeyEvent_.Clear(nullptr);
753 int32_t ret = inputMethodController_->DispatchKeyEvent(
__anon669cc1890902(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) 754 keyEvent_, [](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) {});
755 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
756 auto keyEvent = blockFullKeyEvent_.GetValue();
757 ASSERT_NE(keyEvent, nullptr);
758 CheckKeyEvent(keyEvent);
759 }
760
761 /**
762 * @tc.name: testIMCDispatchKeyEvent003
763 * @tc.desc: test IMC DispatchKeyEvent with 'keyDown/KeyUP' and 'keyEvent'.
764 * @tc.type: FUNC
765 * @tc.require:
766 */
767 HWTEST_F(InputMethodControllerTest, testIMCDispatchKeyEvent003, TestSize.Level0)
768 {
769 IMSA_HILOGI("IMC testIMCDispatchKeyEvent003 Test START");
770 doesKeyEventConsume_ = true;
771 doesFUllKeyEventConsume_ = true;
772 blockKeyEvent_.Clear(nullptr);
773 blockFullKeyEvent_.Clear(nullptr);
774 int32_t ret = inputMethodController_->DispatchKeyEvent(
__anon669cc1890a02(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) 775 keyEvent_, [](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) {});
776 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
777 auto keyEvent = blockKeyEvent_.GetValue();
778 auto keyFullEvent = blockFullKeyEvent_.GetValue();
779 ASSERT_NE(keyEvent, nullptr);
780 EXPECT_EQ(keyEvent->GetKeyCode(), keyEvent_->GetKeyCode());
781 EXPECT_EQ(keyEvent->GetKeyAction(), keyEvent_->GetKeyAction());
782 ASSERT_NE(keyFullEvent, nullptr);
783 CheckKeyEvent(keyFullEvent);
784 }
785
786 /**
787 * @tc.name: testIMCOnCursorUpdate01
788 * @tc.desc: Test update cursorInfo, call 'OnCursorUpdate' twice, if cursorInfo is the same,
789 * the second time will not get callback.
790 * @tc.type: FUNC
791 * @tc.require:
792 * @tc.author: Zhaolinglan
793 */
794 HWTEST_F(InputMethodControllerTest, testIMCOnCursorUpdate01, TestSize.Level0)
795 {
796 IMSA_HILOGI("IMC testIMCOnCursorUpdate01 Test START");
797 auto ret = inputMethodController_->Attach(textListener_, false);
798 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
799 CursorInfo info = { 1, 3, 0, 5 };
800 InputMethodControllerTest::TriggerCursorUpdateCallback(info);
801 EXPECT_EQ(InputMethodControllerTest::cursorInfo_, info);
802
803 InputMethodControllerTest::cursorInfo_ = {};
804 InputMethodControllerTest::TriggerCursorUpdateCallback(info);
805 EXPECT_FALSE(InputMethodControllerTest::cursorInfo_ == info);
806 }
807
808 /**
809 * @tc.name: testIMCOnCursorUpdate02
810 * @tc.desc: Test update cursorInfo, 'Attach'->'OnCursorUpdate'->'Close'->'Attach'->'OnCursorUpdate',
811 * it will get callback two time.
812 * @tc.type: FUNC
813 * @tc.require:
814 * @tc.author: Zhaolinglan
815 */
816 HWTEST_F(InputMethodControllerTest, testIMCOnCursorUpdate02, TestSize.Level0)
817 {
818 IMSA_HILOGI("IMC testIMCOnCursorUpdate02 Test START");
819 auto ret = inputMethodController_->Attach(textListener_, false);
820 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
821 CursorInfo info = { 2, 4, 0, 6 };
822 InputMethodControllerTest::TriggerCursorUpdateCallback(info);
823 EXPECT_EQ(InputMethodControllerTest::cursorInfo_, info);
824
825 InputMethodControllerTest::cursorInfo_ = {};
826 ret = InputMethodControllerTest::inputMethodController_->Close();
827 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
828 ret = inputMethodController_->Attach(textListener_, false);
829 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
830 InputMethodControllerTest::TriggerCursorUpdateCallback(info);
831 EXPECT_EQ(InputMethodControllerTest::cursorInfo_, info);
832 }
833
834 /**
835 * @tc.name: testIMCOnSelectionChange01
836 * @tc.desc: Test change selection, call 'OnSelectionChange' twice, if selection is the same,
837 * the second time will not get callback.
838 * @tc.type: FUNC
839 * @tc.require:
840 * @tc.author: Zhaolinglan
841 */
842 HWTEST_F(InputMethodControllerTest, testIMCOnSelectionChange01, TestSize.Level0)
843 {
844 IMSA_HILOGI("IMC testIMCOnSelectionChange01 Test START");
845 auto ret = inputMethodController_->Attach(textListener_, false);
846 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
847 std::u16string text = Str8ToStr16("testSelect");
848 int start = 1;
849 int end = 2;
850 InputMethodControllerTest::TriggerSelectionChangeCallback(text, start, end);
851 EXPECT_EQ(InputMethodControllerTest::text_, Str16ToStr8(text));
852
853 InputMethodControllerTest::text_ = "";
854 InputMethodControllerTest::TriggerSelectionChangeCallback(text, start, end);
855 EXPECT_NE(InputMethodControllerTest::text_, Str16ToStr8(text));
856 }
857
858 /**
859 * @tc.name: testIMCOnSelectionChange02
860 * @tc.desc: Test change selection, 'Attach'->'OnSelectionChange'->'Close'->'Attach'->'OnSelectionChange',
861 * it will get callback two time.
862 * @tc.type: FUNC
863 * @tc.require:
864 * @tc.author: Zhaolinglan
865 */
866 HWTEST_F(InputMethodControllerTest, testIMCOnSelectionChange02, TestSize.Level0)
867 {
868 IMSA_HILOGI("IMC testIMCOnSelectionChange02 Test START");
869 auto ret = inputMethodController_->Attach(textListener_, false);
870 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
871 std::u16string text = Str8ToStr16("testSelect2");
872 int start = 1;
873 int end = 2;
874 InputMethodControllerTest::TriggerSelectionChangeCallback(text, start, end);
875 EXPECT_EQ(InputMethodControllerTest::text_, Str16ToStr8(text));
876
877 InputMethodControllerTest::text_ = "";
878 InputMethodControllerTest::inputMethodController_->Close();
879 inputMethodController_->Attach(textListener_, false);
880 InputMethodControllerTest::TriggerSelectionChangeCallback(text, start, end);
881 EXPECT_EQ(InputMethodControllerTest::text_, Str16ToStr8(text));
882 }
883
884 /**
885 * @tc.name: testIMCOnSelectionChange03
886 * @tc.desc: Test change selection, 'Attach without config'->'OnSelectionChange(0, 0)', it will get callback.
887 * @tc.type: FUNC
888 * @tc.require:
889 * @tc.author: Zhaolinglan
890 */
891 HWTEST_F(InputMethodControllerTest, testIMCOnSelectionChange03, TestSize.Level0)
892 {
893 IMSA_HILOGI("IMC testIMCOnSelectionChange03 Test START");
894 InputMethodControllerTest::ResetKeyboardListenerTextConfig();
895 InputMethodControllerTest::inputMethodController_->Close();
896 auto ret = inputMethodController_->Attach(textListener_, false);
897 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
898 std::u16string text = Str8ToStr16("");
899 int start = 0;
900 int end = 0;
901 InputMethodControllerTest::TriggerSelectionChangeCallback(text, start, end);
902 EXPECT_EQ(InputMethodControllerTest::text_, Str16ToStr8(text));
903 EXPECT_EQ(InputMethodControllerTest::oldBegin_, INVALID_VALUE);
904 EXPECT_EQ(InputMethodControllerTest::oldEnd_, INVALID_VALUE);
905 EXPECT_EQ(InputMethodControllerTest::newBegin_, start);
906 EXPECT_EQ(InputMethodControllerTest::newEnd_, end);
907 }
908
909 /**
910 * @tc.name: testIMCOnSelectionChange04
911 * @tc.desc: Test change selection, 'Attach with range(1, 1)'->'Attach witch range(2, 2)', it will get (1, 1, 2, 2).
912 * @tc.type: FUNC
913 * @tc.require:
914 * @tc.author: Zhaolinglan
915 */
916 HWTEST_F(InputMethodControllerTest, testIMCOnSelectionChange04, TestSize.Level0)
917 {
918 IMSA_HILOGI("IMC testIMCOnSelectionChange04 Test START");
919 InputMethodControllerTest::ResetKeyboardListenerTextConfig();
920 InputMethodControllerTest::inputMethodController_->Close();
921 TextConfig textConfig;
922 textConfig.range = { 1, 1 };
923 auto ret = inputMethodController_->Attach(textListener_, false, textConfig);
924 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
925 textConfig.range = { 2, 2 };
926 ret = inputMethodController_->Attach(textListener_, false, textConfig);
927 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
928 std::this_thread::sleep_for(std::chrono::seconds(2));
929 EXPECT_EQ(InputMethodControllerTest::oldBegin_, 1);
930 EXPECT_EQ(InputMethodControllerTest::oldEnd_, 1);
931 EXPECT_EQ(InputMethodControllerTest::newBegin_, 2);
932 EXPECT_EQ(InputMethodControllerTest::newEnd_, 2);
933 }
934
935 /**
936 * @tc.name: testIMCOnSelectionChange05
937 * @tc.desc: 'Attach without range'-> it will get no selectionChange callback.
938 * @tc.type: FUNC
939 * @tc.require:
940 * @tc.author: Zhaolinglan
941 */
942 HWTEST_F(InputMethodControllerTest, testIMCOnSelectionChange05, TestSize.Level0)
943 {
944 IMSA_HILOGI("IMC testIMCOnSelectionChange05 Test START");
945 InputMethodControllerTest::ResetKeyboardListenerTextConfig();
946 InputMethodControllerTest::inputMethodController_->Close();
947 inputMethodController_->Attach(textListener_, false);
948 EXPECT_EQ(InputMethodControllerTest::oldBegin_, INVALID_VALUE);
949 EXPECT_EQ(InputMethodControllerTest::oldEnd_, INVALID_VALUE);
950 EXPECT_EQ(InputMethodControllerTest::newBegin_, INVALID_VALUE);
951 EXPECT_EQ(InputMethodControllerTest::newEnd_, INVALID_VALUE);
952 }
953
954 /**
955 * @tc.name: testIMCOnSelectionChange06
956 * @tc.desc: 'Update(1, 2)'->'Attach without range', it will get no selectionChange callback.
957 * @tc.type: FUNC
958 * @tc.require:
959 * @tc.author: Zhaolinglan
960 */
961 HWTEST_F(InputMethodControllerTest, testIMCOnSelectionChange06, TestSize.Level0)
962 {
963 IMSA_HILOGI("IMC testIMCOnSelectionChange06 Test START");
964 InputMethodControllerTest::inputMethodController_->Close();
965 inputMethodController_->Attach(textListener_, false);
966 std::u16string text = Str8ToStr16("");
967 InputMethodControllerTest::TriggerSelectionChangeCallback(text, 1, 2);
968 InputMethodControllerTest::ResetKeyboardListenerTextConfig();
969 inputMethodController_->Attach(textListener_, false);
970 EXPECT_EQ(InputMethodControllerTest::oldBegin_, INVALID_VALUE);
971 EXPECT_EQ(InputMethodControllerTest::oldEnd_, INVALID_VALUE);
972 EXPECT_EQ(InputMethodControllerTest::newBegin_, INVALID_VALUE);
973 EXPECT_EQ(InputMethodControllerTest::newEnd_, INVALID_VALUE);
974 }
975
976 /**
977 * @tc.name: testIMCOnSelectionChange07
978 * @tc.desc: 'Attach with range -> Attach without range', it will get no selectionChange callback.
979 * @tc.type: FUNC
980 * @tc.require:
981 * @tc.author: Zhaolinglan
982 */
983 HWTEST_F(InputMethodControllerTest, testIMCOnSelectionChange07, TestSize.Level0)
984 {
985 IMSA_HILOGI("IMC testIMCOnSelectionChange07 Test START");
986 InputMethodControllerTest::inputMethodController_->Close();
987 TextConfig textConfig;
988 textConfig.range = { 1, 1 };
989 inputMethodController_->Attach(textListener_, false, textConfig);
990 std::this_thread::sleep_for(std::chrono::seconds(2));
991
992 InputMethodControllerTest::ResetKeyboardListenerTextConfig();
993 inputMethodController_->Attach(textListener_, false);
994 std::this_thread::sleep_for(std::chrono::seconds(2));
995 EXPECT_EQ(InputMethodControllerTest::oldBegin_, INVALID_VALUE);
996 EXPECT_EQ(InputMethodControllerTest::oldEnd_, INVALID_VALUE);
997 EXPECT_EQ(InputMethodControllerTest::newBegin_, INVALID_VALUE);
998 EXPECT_EQ(InputMethodControllerTest::newEnd_, INVALID_VALUE);
999 }
1000
1001 /**
1002 * @tc.name: testIMCOnSelectionChange08
1003 * @tc.desc: 'OnSelectionChange(1, 2) -> Attach without range -> OnSelectionChange(3, 4)', it will get (1,2,3,4)
1004 * @tc.type: FUNC
1005 * @tc.require:
1006 * @tc.author: Zhaolinglan
1007 */
1008 HWTEST_F(InputMethodControllerTest, testIMCOnSelectionChange08, TestSize.Level0)
1009 {
1010 IMSA_HILOGI("IMC testIMCOnSelectionChange08 Test START");
1011 InputMethodControllerTest::inputMethodController_->Close();
1012 inputMethodController_->Attach(textListener_, false);
1013 std::u16string text = Str8ToStr16("");
1014 InputMethodControllerTest::TriggerSelectionChangeCallback(text, 1, 2);
1015 InputMethodControllerTest::ResetKeyboardListenerTextConfig();
1016 inputMethodController_->Attach(textListener_, false);
1017 InputMethodControllerTest::TriggerSelectionChangeCallback(text, 3, 4);
1018 EXPECT_EQ(InputMethodControllerTest::oldBegin_, 1);
1019 EXPECT_EQ(InputMethodControllerTest::oldEnd_, 2);
1020 EXPECT_EQ(InputMethodControllerTest::newBegin_, 3);
1021 EXPECT_EQ(InputMethodControllerTest::newEnd_, 4);
1022 }
1023
1024 /**
1025 * @tc.name: testIMCOnSelectionChange09
1026 * @tc.desc: Attach with range(0, 0) -> OnSelectionChange("", 0, 0) -> Get 'textChange' and 'selectionChange' Callback
1027 * @tc.type: FUNC
1028 * @tc.require:
1029 * @tc.author: Zhaolinglan
1030 */
1031 HWTEST_F(InputMethodControllerTest, testIMCOnSelectionChange09, TestSize.Level0)
1032 {
1033 IMSA_HILOGI("IMC testIMCOnSelectionChange09 Test START");
1034 InputMethodControllerTest::inputMethodController_->Close();
1035 TextConfig textConfig;
1036 textConfig.range = { 0, 0 };
1037 auto ret = inputMethodController_->Attach(textListener_, false, textConfig);
1038 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1039 InputMethodControllerTest::ResetKeyboardListenerTextConfig();
1040 InputMethodControllerTest::text_ = "test";
1041 std::u16string text = Str8ToStr16("test1");
1042 InputMethodControllerTest::TriggerSelectionChangeCallback(text, 1, 6);
1043 EXPECT_EQ(InputMethodControllerTest::text_, "test1");
1044 EXPECT_EQ(InputMethodControllerTest::newBegin_, 1);
1045 EXPECT_EQ(InputMethodControllerTest::newEnd_, 6);
1046 }
1047
1048 /**
1049 * @tc.name: testShowTextInput
1050 * @tc.desc: IMC ShowTextInput
1051 * @tc.type: FUNC
1052 */
1053 HWTEST_F(InputMethodControllerTest, testShowTextInput, TestSize.Level0)
1054 {
1055 IMSA_HILOGI("IMC ShowTextInput Test START");
1056 TextListener::ResetParam();
1057 inputMethodController_->ShowTextInput();
1058 EXPECT_TRUE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::SHOW));
1059 }
1060
1061 /**
1062 * @tc.name: testShowSoftKeyboard
1063 * @tc.desc: IMC ShowSoftKeyboard
1064 * @tc.type: FUNC
1065 */
1066 HWTEST_F(InputMethodControllerTest, testShowSoftKeyboard, TestSize.Level0)
1067 {
1068 IMSA_HILOGI("IMC ShowSoftKeyboard Test START");
1069 IdentityCheckerMock::SetPermission(true);
1070 imeListener_->keyboardState_ = false;
1071 TextListener::ResetParam();
1072 int32_t ret = inputMethodController_->ShowSoftKeyboard();
1073 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1074 EXPECT_TRUE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::SHOW) && WaitKeyboardStatus(true));
1075 IdentityCheckerMock::SetPermission(false);
1076 }
1077
1078 /**
1079 * @tc.name: testShowCurrentInput
1080 * @tc.desc: IMC ShowCurrentInput
1081 * @tc.type: FUNC
1082 */
1083 HWTEST_F(InputMethodControllerTest, testShowCurrentInput, TestSize.Level0)
1084 {
1085 IMSA_HILOGI("IMC ShowCurrentInput Test START");
1086 imeListener_->keyboardState_ = false;
1087 TextListener::ResetParam();
1088 int32_t ret = inputMethodController_->ShowCurrentInput();
1089 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1090 EXPECT_TRUE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::SHOW) && WaitKeyboardStatus(true));
1091 }
1092
1093 /**
1094 * @tc.name: testIMCGetEnterKeyType
1095 * @tc.desc: IMC testGetEnterKeyType.
1096 * @tc.type: FUNC
1097 * @tc.require:
1098 */
1099 HWTEST_F(InputMethodControllerTest, testIMCGetEnterKeyType, TestSize.Level0)
1100 {
1101 IMSA_HILOGI("IMC GetEnterKeyType Test START");
1102 int32_t keyType;
1103 inputMethodController_->GetEnterKeyType(keyType);
1104 EXPECT_TRUE(keyType >= static_cast<int32_t>(EnterKeyType::UNSPECIFIED) &&
1105 keyType <= static_cast<int32_t>(EnterKeyType::PREVIOUS));
1106 }
1107
1108 /**
1109 * @tc.name: testIMCGetInputPattern
1110 * @tc.desc: IMC testGetInputPattern.
1111 * @tc.type: FUNC
1112 * @tc.require:
1113 */
1114 HWTEST_F(InputMethodControllerTest, testIMCGetInputPattern, TestSize.Level0)
1115 {
1116 IMSA_HILOGI("IMC GetInputPattern Test START");
1117 int32_t inputPattern;
1118 inputMethodController_->GetInputPattern(inputPattern);
1119 EXPECT_TRUE(inputPattern >= static_cast<int32_t>(TextInputType::NONE) &&
1120 inputPattern <= static_cast<int32_t>(TextInputType::VISIBLE_PASSWORD));
1121 }
1122
1123 /**
1124 * @tc.name: testOnEditorAttributeChanged01
1125 * @tc.desc: IMC testOnEditorAttributeChanged01.
1126 * @tc.type: FUNC
1127 * @tc.require:
1128 */
1129 HWTEST_F(InputMethodControllerTest, testOnEditorAttributeChanged01, TestSize.Level0)
1130 {
1131 IMSA_HILOGI("IMC testOnEditorAttributeChanged01 Test START");
1132 auto ret = inputMethodController_->Attach(textListener_, false);
1133 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1134 Configuration info;
1135 info.SetEnterKeyType(EnterKeyType::GO);
1136 info.SetTextInputType(TextInputType::NUMBER);
1137 InputMethodControllerTest::TriggerConfigurationChangeCallback(info);
1138 EXPECT_EQ(InputMethodControllerTest::inputAttribute_.inputPattern, static_cast<int32_t>(info.GetTextInputType()));
1139 EXPECT_EQ(InputMethodControllerTest::inputAttribute_.enterKeyType, static_cast<int32_t>(info.GetEnterKeyType()));
1140 }
1141
1142 /**
1143 * @tc.name: testOnEditorAttributeChanged02
1144 * @tc.desc: Attach(isPreviewSupport) -> OnConfigurationChange -> editorChange callback (isPreviewSupport).
1145 * @tc.type: FUNC
1146 * @tc.require:
1147 */
1148 HWTEST_F(InputMethodControllerTest, testOnEditorAttributeChanged02, TestSize.Level0)
1149 {
1150 IMSA_HILOGI("IMC testOnEditorAttributeChanged02 Test START");
1151 InputAttribute attribute = { .inputPattern = static_cast<int32_t>(TextInputType::DATETIME),
1152 .enterKeyType = static_cast<int32_t>(EnterKeyType::GO),
1153 .isTextPreviewSupported = true };
1154 auto ret = inputMethodController_->Attach(textListener_, false, attribute);
1155 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1156 Configuration info;
1157 info.SetEnterKeyType(EnterKeyType::NEW_LINE);
1158 info.SetTextInputType(TextInputType::NUMBER);
1159 InputMethodControllerTest::ResetKeyboardListenerTextConfig();
1160 InputMethodControllerTest::TriggerConfigurationChangeCallback(info);
1161 EXPECT_EQ(InputMethodControllerTest::inputAttribute_.inputPattern, static_cast<int32_t>(info.GetTextInputType()));
1162 EXPECT_EQ(InputMethodControllerTest::inputAttribute_.enterKeyType, static_cast<int32_t>(info.GetEnterKeyType()));
1163 EXPECT_EQ(InputMethodControllerTest::inputAttribute_.isTextPreviewSupported, attribute.isTextPreviewSupported);
1164 }
1165
1166 /**
1167 * @tc.name: testHideSoftKeyboard
1168 * @tc.desc: IMC HideSoftKeyboard
1169 * @tc.type: FUNC
1170 */
1171 HWTEST_F(InputMethodControllerTest, testHideSoftKeyboard, TestSize.Level0)
1172 {
1173 IMSA_HILOGI("IMC HideSoftKeyboard Test START");
1174 IdentityCheckerMock::SetPermission(true);
1175 imeListener_->keyboardState_ = true;
1176 TextListener::ResetParam();
1177 int32_t ret = inputMethodController_->HideSoftKeyboard();
1178 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1179 EXPECT_TRUE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::HIDE) && WaitKeyboardStatus(false));
1180 IdentityCheckerMock::SetPermission(false);
1181 }
1182
1183 /**
1184 * @tc.name: testIMCHideCurrentInput
1185 * @tc.desc: IMC HideCurrentInput.
1186 * @tc.type: FUNC
1187 * @tc.require:
1188 */
1189 HWTEST_F(InputMethodControllerTest, testIMCHideCurrentInput, TestSize.Level0)
1190 {
1191 IMSA_HILOGI("IMC HideCurrentInput Test START");
1192 imeListener_->keyboardState_ = true;
1193 TextListener::ResetParam();
1194 int32_t ret = inputMethodController_->HideCurrentInput();
1195 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1196 EXPECT_TRUE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::HIDE) && WaitKeyboardStatus(false));
1197 }
1198
1199 /**
1200 * @tc.name: testIMCInputStopSession
1201 * @tc.desc: IMC testInputStopSession.
1202 * @tc.type: FUNC
1203 * @tc.require: issueI5U8FZ
1204 * @tc.author: Hollokin
1205 */
1206 HWTEST_F(InputMethodControllerTest, testIMCInputStopSession, TestSize.Level0)
1207 {
1208 IMSA_HILOGI("IMC StopInputSession Test START");
1209 imeListener_->keyboardState_ = true;
1210 int32_t ret = inputMethodController_->StopInputSession();
1211 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1212 EXPECT_TRUE(WaitKeyboardStatus(false));
1213 }
1214
1215 /**
1216 * @tc.name: testIMCHideTextInput.
1217 * @tc.desc: IMC testHideTextInput.
1218 * @tc.type: FUNC
1219 */
1220 HWTEST_F(InputMethodControllerTest, testIMCHideTextInput, TestSize.Level0)
1221 {
1222 IMSA_HILOGI("IMC HideTextInput Test START");
1223 imeListener_->keyboardState_ = true;
1224 inputMethodController_->HideTextInput();
1225 EXPECT_TRUE(WaitKeyboardStatus(false));
1226 }
1227
1228 /**
1229 * @tc.name: testIMCRequestShowInput.
1230 * @tc.desc: IMC testIMCRequestShowInput.
1231 * @tc.type: FUNC
1232 */
1233 HWTEST_F(InputMethodControllerTest, testIMCRequestShowInput, TestSize.Level0)
1234 {
1235 IMSA_HILOGI("IMC testIMCRequestShowInput Test START");
1236 imeListener_->keyboardState_ = false;
1237 int32_t ret = InputMethodControllerTest::inputMethodController_->RequestShowInput();
1238 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1239 EXPECT_TRUE(WaitKeyboardStatus(true));
1240 }
1241
1242 /**
1243 * @tc.name: testIMCRequestHideInput.
1244 * @tc.desc: IMC testIMCRequestHideInput.
1245 * @tc.type: FUNC
1246 */
1247 HWTEST_F(InputMethodControllerTest, testIMCRequestHideInput, TestSize.Level0)
1248 {
1249 IMSA_HILOGI("IMC testIMCRequestHideInput Test START");
1250 imeListener_->keyboardState_ = true;
1251 int32_t ret = InputMethodControllerTest::inputMethodController_->RequestHideInput();
1252 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1253 EXPECT_TRUE(WaitKeyboardStatus(false));
1254 }
1255
1256 /**
1257 * @tc.name: testSetControllerListener
1258 * @tc.desc: IMC SetControllerListener
1259 * @tc.type: FUNC
1260 */
1261 HWTEST_F(InputMethodControllerTest, testSetControllerListener, TestSize.Level0)
1262 {
1263 IMSA_HILOGI("IMC SetControllerListener Test START");
1264 inputMethodController_->SetControllerListener(controllerListener_);
1265
1266 int32_t ret = inputMethodController_->Attach(textListener_, false);
1267 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1268 SelectListenerMock::start_ = 0;
1269 SelectListenerMock::end_ = 0;
1270 inputMethodAbility_.SelectByRange(1, 2);
1271 SelectListenerMock::WaitSelectListenerCallback();
1272 EXPECT_EQ(SelectListenerMock::start_, 1);
1273 EXPECT_EQ(SelectListenerMock::end_, 2);
1274
1275 SelectListenerMock::direction_ = 0;
1276 inputMethodAbility_.SelectByMovement(static_cast<int32_t>(Direction::UP));
1277 SelectListenerMock::WaitSelectListenerCallback();
1278 EXPECT_EQ(SelectListenerMock::direction_, static_cast<int32_t>(Direction::UP));
1279
1280 SelectListenerMock::direction_ = 0;
1281 inputMethodAbility_.SelectByMovement(static_cast<int32_t>(Direction::DOWN));
1282 SelectListenerMock::WaitSelectListenerCallback();
1283 EXPECT_EQ(SelectListenerMock::direction_, static_cast<int32_t>(Direction::DOWN));
1284
1285 SelectListenerMock::direction_ = 0;
1286 inputMethodAbility_.SelectByMovement(static_cast<int32_t>(Direction::LEFT));
1287 SelectListenerMock::WaitSelectListenerCallback();
1288 EXPECT_EQ(SelectListenerMock::direction_, static_cast<int32_t>(Direction::LEFT));
1289
1290 SelectListenerMock::direction_ = 0;
1291 inputMethodAbility_.SelectByMovement(static_cast<int32_t>(Direction::RIGHT));
1292 SelectListenerMock::WaitSelectListenerCallback();
1293 EXPECT_EQ(SelectListenerMock::direction_, static_cast<int32_t>(Direction::RIGHT));
1294 }
1295
1296 /**
1297 * @tc.name: testWasAttached
1298 * @tc.desc: IMC WasAttached
1299 * @tc.type: FUNC
1300 */
1301 HWTEST_F(InputMethodControllerTest, testWasAttached, TestSize.Level0)
1302 {
1303 IMSA_HILOGI("IMC WasAttached Test START");
1304 inputMethodController_->Close();
1305 bool result = inputMethodController_->WasAttached();
1306 EXPECT_FALSE(result);
1307 int32_t ret = inputMethodController_->Attach(textListener_, false);
1308 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1309 result = inputMethodController_->WasAttached();
1310 EXPECT_TRUE(result);
1311 inputMethodController_->Close();
1312 }
1313
1314 /**
1315 * @tc.name: testGetDefaultInputMethod
1316 * @tc.desc: IMC GetDefaultInputMethod
1317 * @tc.type: FUNC
1318 */
1319 HWTEST_F(InputMethodControllerTest, testGetDefaultInputMethod, TestSize.Level0)
1320 {
1321 IMSA_HILOGI("IMC testGetDefaultInputMethod Test START");
1322 std::shared_ptr<Property> property = nullptr;
1323 int32_t ret = inputMethodController_->GetDefaultInputMethod(property);
1324 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1325 ASSERT_NE(property, nullptr);
1326 EXPECT_FALSE(property->name.empty());
1327 }
1328
1329 /**
1330 * @tc.name: testGetSystemInputMethodConfig
1331 * @tc.desc: IMC GetSystemInputMethodConfig
1332 * @tc.type: FUNC
1333 */
1334 HWTEST_F(InputMethodControllerTest, GetSystemInputMethodConfig, TestSize.Level0)
1335 {
1336 IMSA_HILOGI("IMC GetSystemInputMethodConfig Test START");
1337 OHOS::AppExecFwk::ElementName inputMethodConfig;
1338 int32_t ret = inputMethodController_->GetInputMethodConfig(inputMethodConfig);
1339 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1340 EXPECT_GE(inputMethodConfig.GetBundleName().length(), 0);
1341 EXPECT_GE(inputMethodConfig.GetAbilityName().length(), 0);
1342 }
1343
1344 /**
1345 * @tc.name: testWithoutEditableState
1346 * @tc.desc: IMC testWithoutEditableState
1347 * @tc.type: FUNC
1348 * @tc.require:
1349 */
1350 HWTEST_F(InputMethodControllerTest, testWithoutEditableState, TestSize.Level0)
1351 {
1352 IMSA_HILOGI("IMC WithouteEditableState Test START");
1353 auto ret = inputMethodController_->Attach(textListener_, false);
1354 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1355 ret = inputMethodController_->HideTextInput();
1356 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1357
1358 int32_t deleteForwardLength = 1;
1359 ret = inputMethodAbility_.DeleteForward(deleteForwardLength);
1360 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
1361 EXPECT_NE(TextListener::deleteForwardLength_, deleteForwardLength);
1362
1363 int32_t deleteBackwardLength = 2;
1364 ret = inputMethodAbility_.DeleteBackward(deleteBackwardLength);
1365 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
1366 EXPECT_NE(TextListener::deleteBackwardLength_, deleteBackwardLength);
1367
1368 std::string insertText = "t";
1369 ret = inputMethodAbility_.InsertText(insertText);
1370 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
1371 EXPECT_NE(TextListener::insertText_, Str8ToStr16(insertText));
1372
1373 constexpr int32_t funcKey = 1;
1374 ret = inputMethodAbility_.SendFunctionKey(funcKey);
1375 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
1376 EXPECT_NE(TextListener::key_, funcKey);
1377
1378 constexpr int32_t keyCode = 4;
1379 ret = inputMethodAbility_.MoveCursor(keyCode);
1380 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
1381 EXPECT_NE(TextListener::direction_, keyCode);
1382 }
1383
1384 /**
1385 * @tc.name: testIsInputTypeSupported
1386 * @tc.desc: IsInputTypeSupported
1387 * @tc.type: FUNC
1388 * @tc.require:
1389 * @tc.author: chenyu
1390 */
1391 HWTEST_F(InputMethodControllerTest, testIsInputTypeSupported, TestSize.Level0)
1392 {
1393 IMSA_HILOGI("IMC testIsInputTypeSupported Test START");
1394 auto ret = inputMethodController_->IsInputTypeSupported(InputType::NONE);
1395 EXPECT_FALSE(ret);
1396 }
1397
1398 /**
1399 * @tc.name: testStartInputType
1400 * @tc.desc: StartInputType
1401 * @tc.type: FUNC
1402 * @tc.require:
1403 * @tc.author: chenyu
1404 */
1405 HWTEST_F(InputMethodControllerTest, testStartInputType, TestSize.Level0)
1406 {
1407 IMSA_HILOGI("IMC testStartInputType Test START");
1408 auto ret = inputMethodController_->StartInputType(InputType::NONE);
1409 EXPECT_NE(ret, ErrorCode::NO_ERROR);
1410 }
1411
1412 /**
1413 * @tc.name: testStartInputTypeAsync
1414 * @tc.desc: StartInputTypeAsync
1415 * @tc.type: FUNC
1416 * @tc.require:
1417 * @tc.author: chenyu
1418 */
1419 HWTEST_F(InputMethodControllerTest, testStartInputTypeAsync, TestSize.Level0)
1420 {
1421 IMSA_HILOGI("IMC testStartInputTypeAsync Test START");
1422 auto ret = inputMethodController_->StartInputTypeAsync(InputType::NONE);
1423 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1424 }
1425
1426 /**
1427 * @tc.name: testSendPrivateCommand_001
1428 * @tc.desc: IMC SendPrivateCommand without default ime
1429 * @tc.type: FUNC
1430 * @tc.require:
1431 * @tc.author: mashaoyin
1432 */
1433 HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_001, TestSize.Level0)
1434 {
1435 IMSA_HILOGI("IMC testSendPrivateCommand_001 Test START");
1436 InputMethodEngineListenerImpl::ResetParam();
1437 auto ret = inputMethodController_->Attach(textListener_, false);
1438 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1439 std::unordered_map<std::string, PrivateDataValue> privateCommand;
1440 PrivateDataValue privateDataValue1 = std::string("stringValue");
1441 privateCommand.emplace("value1", privateDataValue1);
1442 IdentityCheckerMock::SetBundleNameValid(false);
1443 ret = inputMethodController_->SendPrivateCommand(privateCommand);
1444 EXPECT_EQ(ret, ErrorCode::ERROR_NOT_DEFAULT_IME);
1445 inputMethodController_->Close();
1446 IdentityCheckerMock::SetBundleNameValid(true);
1447 }
1448
1449 /**
1450 * @tc.name: testSendPrivateCommand_002
1451 * @tc.desc: SendPrivateCommand not bound, and empty privateCommand.
1452 * @tc.type: FUNC
1453 * @tc.require:
1454 * @tc.author: mashaoyin
1455 */
1456 HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_002, TestSize.Level0)
1457 {
1458 IMSA_HILOGI("IMC testSendPrivateCommand_002 Test START");
1459 InputMethodEngineListenerImpl::ResetParam();
1460 IdentityCheckerMock::SetBundleNameValid(true);
1461 inputMethodController_->Close();
1462 std::unordered_map<std::string, PrivateDataValue> privateCommand;
1463 auto ret = inputMethodController_->SendPrivateCommand(privateCommand);
1464 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
1465
1466 ret = inputMethodController_->Attach(textListener_, false);
1467 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1468 ret = inputMethodController_->SendPrivateCommand(privateCommand);
1469 EXPECT_EQ(ret, ErrorCode::ERROR_INVALID_PRIVATE_COMMAND_SIZE);
1470 inputMethodController_->Close();
1471 IdentityCheckerMock::SetBundleNameValid(false);
1472 }
1473
1474 /**
1475 * @tc.name: testSendPrivateCommand_003
1476 * @tc.desc: IMC SendPrivateCommand with normal private command.
1477 * @tc.type: FUNC
1478 * @tc.require:
1479 * @tc.author: mashaoyin
1480 */
1481 HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_003, TestSize.Level0)
1482 {
1483 IMSA_HILOGI("IMC testSendPrivateCommand_003 Test START");
1484 IdentityCheckerMock::SetBundleNameValid(true);
1485 InputMethodEngineListenerImpl::ResetParam();
1486 auto ret = inputMethodController_->Attach(textListener_, false);
1487 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1488 std::unordered_map<std::string, PrivateDataValue> privateCommand;
1489 PrivateDataValue privateDataValue1 = std::string("stringValue");
1490 privateCommand.emplace("value1", privateDataValue1);
1491 ret = inputMethodController_->SendPrivateCommand(privateCommand);
1492 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1493 inputMethodController_->Close();
1494 IdentityCheckerMock::SetBundleNameValid(false);
1495 }
1496
1497 /**
1498 * @tc.name: testSendPrivateCommand_004
1499 * @tc.desc: IMC SendPrivateCommand with correct data format and all data type.
1500 * @tc.type: FUNC
1501 * @tc.require:
1502 * @tc.author: mashaoyin
1503 */
1504 HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_004, TestSize.Level0)
1505 {
1506 IMSA_HILOGI("IMC testSendPrivateCommand_004 Test START");
1507 IdentityCheckerMock::SetBundleNameValid(true);
1508 InputMethodEngineListenerImpl::ResetParam();
1509 auto ret = inputMethodController_->Attach(textListener_, false);
1510 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1511 std::unordered_map<std::string, PrivateDataValue> privateCommand;
1512 PrivateDataValue privateDataValue1 = std::string("stringValue");
1513 PrivateDataValue privateDataValue2 = true;
1514 PrivateDataValue privateDataValue3 = 100;
1515 privateCommand.emplace("value1", privateDataValue1);
1516 privateCommand.emplace("value2", privateDataValue2);
1517 privateCommand.emplace("value3", privateDataValue3);
1518 ret = inputMethodController_->SendPrivateCommand(privateCommand);
1519 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1520 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitSendPrivateCommand(privateCommand));
1521 inputMethodController_->Close();
1522 IdentityCheckerMock::SetBundleNameValid(false);
1523 }
1524
1525 /**
1526 * @tc.name: testSendPrivateCommand_005
1527 * @tc.desc: IMC SendPrivateCommand with more than 5 private command.
1528 * @tc.type: FUNC
1529 * @tc.require:
1530 * @tc.author: mashaoyin
1531 */
1532 HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_005, TestSize.Level0)
1533 {
1534 IMSA_HILOGI("IMC testSendPrivateCommand_005 Test START");
1535 IdentityCheckerMock::SetBundleNameValid(true);
1536 InputMethodEngineListenerImpl::ResetParam();
1537 auto ret = inputMethodController_->Attach(textListener_, false);
1538 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1539 std::unordered_map<std::string, PrivateDataValue> privateCommand;
1540 PrivateDataValue privateDataValue1 = std::string("stringValue");
1541 privateCommand.emplace("value1", privateDataValue1);
1542 privateCommand.emplace("value2", privateDataValue1);
1543 privateCommand.emplace("value3", privateDataValue1);
1544 privateCommand.emplace("value4", privateDataValue1);
1545 privateCommand.emplace("value5", privateDataValue1);
1546 privateCommand.emplace("value6", privateDataValue1);
1547 ret = inputMethodController_->SendPrivateCommand(privateCommand);
1548 EXPECT_EQ(ret, ErrorCode::ERROR_INVALID_PRIVATE_COMMAND_SIZE);
1549 inputMethodController_->Close();
1550 IdentityCheckerMock::SetBundleNameValid(false);
1551 }
1552
1553 /**
1554 * @tc.name: testSendPrivateCommand_006
1555 * @tc.desc: IMC SendPrivateCommand size is more than 32KB.
1556 * @tc.type: FUNC
1557 * @tc.require:
1558 * @tc.author: mashaoyin
1559 */
1560 HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_006, TestSize.Level0)
1561 {
1562 IMSA_HILOGI("IMC testSendPrivateCommand_006 Test START");
1563 IdentityCheckerMock::SetBundleNameValid(true);
1564 InputMethodEngineListenerImpl::ResetParam();
1565 std::unordered_map<std::string, PrivateDataValue> privateCommand;
1566 PrivateDataValue privateDataValue1 = std::string("stringValue");
1567 PrivateDataValue privateDataValue2 = true;
1568 PrivateDataValue privateDataValue3 = 100;
1569 PrivateDataValue privateDataValue4 = 0;
1570 privateCommand.emplace("value1", privateDataValue1);
1571 privateCommand.emplace("value2", privateDataValue2);
1572 privateCommand.emplace("value3", privateDataValue3);
1573 privateCommand.emplace("displayId", privateDataValue4);
1574 TextConfig textConfig;
1575 textConfig.privateCommand = privateCommand;
1576 textConfig.windowId = 1;
1577 auto ret = inputMethodController_->Attach(textListener_, false, textConfig);
1578 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1579 EXPECT_TRUE(InputMethodEngineListenerImpl::WaitSendPrivateCommand(privateCommand));
1580 inputMethodController_->Close();
1581 IdentityCheckerMock::SetBundleNameValid(false);
1582 }
1583
1584 /**
1585 * @tc.name: testSendPrivateCommand_007
1586 * @tc.desc: IMC SendPrivateCommand with Attach.
1587 * @tc.type: FUNC
1588 * @tc.require:
1589 * @tc.author: mashaoyin
1590 */
1591 HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_007, TestSize.Level0)
1592 {
1593 IMSA_HILOGI("IMC testSendPrivateCommand_007 Test START");
1594 IdentityCheckerMock::SetBundleNameValid(true);
1595 TextListener::ResetParam();
1596 auto ret = inputMethodController_->Attach(textListener_, false);
1597 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1598 string str(32768, 'a');
1599 std::unordered_map<std::string, PrivateDataValue> privateCommand;
1600 PrivateDataValue privateDataValue1 = str;
1601 privateCommand.emplace("value1", privateDataValue1);
1602 ret = inputMethodController_->SendPrivateCommand(privateCommand);
1603 EXPECT_EQ(ret, ErrorCode::ERROR_INVALID_PRIVATE_COMMAND_SIZE);
1604 inputMethodController_->Close();
1605 IdentityCheckerMock::SetBundleNameValid(false);
1606 }
1607
1608 /**
1609 * @tc.name: testSendPrivateCommand_008
1610 * @tc.desc: IMA SendPrivateCommand total size is 32KB, 32KB - 1, 32KB + 1.
1611 * @tc.type: IMC
1612 * @tc.require:
1613 * @tc.author: mashaoyin
1614 */
1615 HWTEST_F(InputMethodControllerTest, testSendPrivateCommand_008, TestSize.Level0)
1616 {
1617 IMSA_HILOGI("IMC testSendPrivateCommand_008 Test START");
1618 IdentityCheckerMock::SetBundleNameValid(true);
1619 auto ret = inputMethodController_->Attach(textListener_, false);
1620 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1621 TextListener::ResetParam();
1622 std::unordered_map<std::string, PrivateDataValue> privateCommand1 {
1623 { "v", string(PRIVATE_COMMAND_SIZE_MAX - 2, 'a') }
1624 };
1625 std::unordered_map<std::string, PrivateDataValue> privateCommand2 {
1626 { "v", string(PRIVATE_COMMAND_SIZE_MAX - 1, 'a') }
1627 };
1628 std::unordered_map<std::string, PrivateDataValue> privateCommand3 {
1629 { "v", string(PRIVATE_COMMAND_SIZE_MAX, 'a') }
1630 };
1631 ret = inputMethodController_->SendPrivateCommand(privateCommand1);
1632 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1633 ret = inputMethodController_->SendPrivateCommand(privateCommand2);
1634 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1635 ret = inputMethodController_->SendPrivateCommand(privateCommand3);
1636 EXPECT_EQ(ret, ErrorCode::ERROR_INVALID_PRIVATE_COMMAND_SIZE);
1637 inputMethodController_->Close();
1638 IdentityCheckerMock::SetBundleNameValid(false);
1639 }
1640
1641 /**
1642 * @tc.name: testFinishTextPreviewAfterDetach_001
1643 * @tc.desc: IMC testFinishTextPreviewAfterDetach.
1644 * @tc.type: IMC
1645 * @tc.require:
1646 * @tc.author: zhaolinglan
1647 */
1648 HWTEST_F(InputMethodControllerTest, testFinishTextPreviewAfterDetach_001, TestSize.Level0)
1649 {
1650 IMSA_HILOGI("IMC testFinishTextPreviewAfterDetach_001 Test START");
1651 InputAttribute inputAttribute = { .isTextPreviewSupported = true };
1652 inputMethodController_->Attach(textListener_, false, inputAttribute);
1653 TextListener::ResetParam();
1654 inputMethodController_->Close();
1655 EXPECT_TRUE(TextListener::isFinishTextPreviewCalled_);
1656 }
1657
1658 /**
1659 * @tc.name: testFinishTextPreviewAfterDetach_002
1660 * @tc.desc: IMC testFinishTextPreviewAfterDetach_002.
1661 * @tc.type: IMC
1662 * @tc.require:
1663 * @tc.author: zhaolinglan
1664 */
1665 HWTEST_F(InputMethodControllerTest, testFinishTextPreviewAfterDetach_002, TestSize.Level0)
1666 {
1667 IMSA_HILOGI("IMC testFinishTextPreviewAfterDetach_002 Test START");
1668 InputAttribute inputAttribute = { .isTextPreviewSupported = true };
1669 inputMethodController_->Attach(textListener_, false, inputAttribute);
1670 TextListener::ResetParam();
1671 inputMethodController_->DeactivateClient();
1672 EXPECT_FALSE(TextListener::isFinishTextPreviewCalled_);
1673 }
1674
1675 /**
1676 * @tc.name: testOnInputReady
1677 * @tc.desc: IMC testOnInputReady
1678 * @tc.type: IMC
1679 * @tc.require:
1680 */
1681 HWTEST_F(InputMethodControllerTest, testOnInputReady, TestSize.Level0)
1682 {
1683 IMSA_HILOGI("IMC OnInputReady Test START");
1684 InputAttribute inputAttribute = { .isTextPreviewSupported = true };
1685 inputMethodController_->Attach(textListener_, false, inputAttribute);
1686 sptr<IRemoteObject> agentObject = nullptr;
1687 BindImeInfo imeInfo;
1688 imeInfo.pid = 0;
1689 imeInfo.bundleName = "";
1690 inputMethodController_->OnInputReady(agentObject, imeInfo);
1691 TextListener::ResetParam();
1692 inputMethodController_->DeactivateClient();
1693 EXPECT_FALSE(TextListener::isFinishTextPreviewCalled_);
1694 }
1695
1696 /**
1697 * @tc.name: testOnInputStop
1698 * @tc.desc: IMC testOnInputStop
1699 * @tc.type: IMC
1700 * @tc.require:
1701 */
1702 HWTEST_F(InputMethodControllerTest, testOnInputStop, TestSize.Level0)
1703 {
1704 IMSA_HILOGI("IMC OnInputStop Test START");
1705 InputAttribute inputAttribute = { .isTextPreviewSupported = true };
1706 inputMethodController_->Attach(textListener_, false, inputAttribute);
1707 int32_t invalidValue = -1;
1708 inputMethodController_->isBound_.store(true);
1709 inputMethodController_->isEditable_.store(true);
1710 inputMethodController_->isTextNotified_.store(true);
1711 inputMethodController_->selectOldBegin_ = 0;
1712 inputMethodController_->selectOldEnd_ = 0;
1713 inputMethodController_->selectNewBegin_ = 1;
1714 inputMethodController_->selectNewEnd_ = 1;
1715 inputMethodController_->OnInputStop();
1716 EXPECT_EQ(inputMethodController_->isBound_, false);
1717 EXPECT_EQ(inputMethodController_->isEditable_, false);
1718 EXPECT_EQ(inputMethodController_->isTextNotified_, false);
1719 EXPECT_EQ(inputMethodController_->textString_, Str8ToStr16(""));
1720 EXPECT_EQ(inputMethodController_->selectOldBegin_, invalidValue);
1721 EXPECT_EQ(inputMethodController_->selectOldEnd_, invalidValue);
1722 EXPECT_EQ(inputMethodController_->selectNewBegin_, invalidValue);
1723 EXPECT_EQ(inputMethodController_->selectNewEnd_, invalidValue);
1724 inputMethodController_->DeactivateClient();
1725 }
1726
1727 /**
1728 * @tc.name: testIsPanelShown
1729 * @tc.desc: IMC testIsPanelShown
1730 * @tc.type: IMC
1731 * @tc.require:
1732 */
1733 HWTEST_F(InputMethodControllerTest, testIsPanelShown, TestSize.Level0)
1734 {
1735 IMSA_HILOGI("IMC testIsPanelShown Test START");
1736 InputAttribute inputAttribute = { .isTextPreviewSupported = true };
1737 inputMethodController_->Attach(textListener_, false, inputAttribute);
1738 const PanelInfo panelInfo;
1739 bool isShown = false;
1740 auto ret = inputMethodController_->IsPanelShown(panelInfo, isShown);
1741 EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_SYSTEM_PERMISSION);
1742 }
1743
1744 /**
1745 * @tc.name: testIMCDispatchKeyEvent_null
1746 * @tc.desc: test IMC DispatchKeyEvent with keyEvent null
1747 * @tc.type: FUNC
1748 * @tc.require:
1749 */
1750 HWTEST_F(InputMethodControllerTest, testIMCDispatchKeyEvent_null, TestSize.Level0)
1751 {
1752 IMSA_HILOGI("IMC testIMCDispatchKeyEvent_null Test START");
1753 std::shared_ptr<MMI::KeyEvent> keyEvent = nullptr;
1754 KeyEventCallback callback;
1755 auto ret = inputMethodController_->DispatchKeyEvent(keyEvent, callback);
1756 EXPECT_EQ(ret, ErrorCode::ERROR_EX_NULL_POINTER);
1757 std::this_thread::sleep_for(std::chrono::seconds(2)); // avoid EventHandler crash
1758 }
1759
1760 /**
1761 * @tc.name: testIMCReset
1762 * @tc.desc: test IMC Reset
1763 * @tc.type: FUNC
1764 * @tc.require:
1765 */
1766 HWTEST_F(InputMethodControllerTest, testIMCReset, TestSize.Level0)
1767 {
1768 IMSA_HILOGI("IMC testIMCReset Test START");
1769 auto ret = inputMethodController_->Attach(textListener_, false);
1770 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1771 EXPECT_NE(inputMethodController_->abilityManager_, nullptr);
1772 EXPECT_NE(inputMethodController_->textListener_, nullptr);
1773 inputMethodController_->Reset();
1774 EXPECT_EQ(inputMethodController_->textListener_, nullptr);
1775 EXPECT_EQ(inputMethodController_->abilityManager_, nullptr);
1776 inputMethodController_->abilityManager_ = imsaProxy_;
1777 }
1778 /**
1779 * @tc.name: testGetInputMethodState_001
1780 * @tc.desc: IMA
1781 * @tc.type: FUNC
1782 * @tc.require:
1783 */
1784 HWTEST_F(InputMethodControllerTest, testGetInputMethodState_001, TestSize.Level0)
1785 {
1786 IMSA_HILOGI("InputMethodControllerTest GetInputMethodState_001 Test START");
1787 ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.clear();
1788 ImeInfoInquirer::GetInstance().systemConfig_.enableFullExperienceFeature = true;
1789 ImeInfoInquirer::GetInstance().systemConfig_.enableInputMethodFeature = true;
1790 ImeEnabledInfo info;
1791 info.bundleName = TddUtil::currentBundleNameMock_;
1792 info.enabledStatus = EnabledStatus::FULL_EXPERIENCE_MODE;
1793 ImeEnabledCfg cfg;
1794 cfg.enabledInfos.push_back(info);
1795 ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.insert({ imsa_->userId_, cfg });
1796 EnabledStatus status = EnabledStatus::DISABLED;
1797 auto ret = inputMethodController_->GetInputMethodState(status);
1798 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1799 EXPECT_TRUE(status == EnabledStatus::FULL_EXPERIENCE_MODE);
1800 }
1801
1802 /**
1803 * @tc.name: testGetInputMethodState_002
1804 * @tc.desc: IMA
1805 * @tc.type: FUNC
1806 * @tc.require:
1807 */
1808 HWTEST_F(InputMethodControllerTest, testGetInputMethodState_002, TestSize.Level0)
1809 {
1810 IMSA_HILOGI("InputMethodControllerTest GetInputMethodState_002 Test START");
1811 ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.clear();
1812 ImeInfoInquirer::GetInstance().systemConfig_.enableFullExperienceFeature = false;
1813 ImeInfoInquirer::GetInstance().systemConfig_.enableInputMethodFeature = false;
1814 ImeEnabledInfo info;
1815 info.bundleName = TddUtil::currentBundleNameMock_;
1816 info.enabledStatus = EnabledStatus::BASIC_MODE;
1817 ImeEnabledCfg cfg;
1818 cfg.enabledInfos.push_back(info);
1819 ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.insert({ imsa_->userId_, cfg });
1820 EnabledStatus status = EnabledStatus::DISABLED;
1821 auto ret = inputMethodController_->GetInputMethodState(status);
1822 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1823 EXPECT_TRUE(status == EnabledStatus::BASIC_MODE);
1824 }
1825
1826 /**
1827 * @tc.name: testGetInputMethodState_003
1828 * @tc.desc: IMA
1829 * @tc.type: FUNC
1830 * @tc.require:
1831 */
1832 HWTEST_F(InputMethodControllerTest, testGetInputMethodState_003, TestSize.Level0)
1833 {
1834 IMSA_HILOGI("InputMethodControllerTest GetInputMethodState_003 Test START");
1835 ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.clear();
1836 ImeInfoInquirer::GetInstance().systemConfig_.enableFullExperienceFeature = true;
1837 ImeInfoInquirer::GetInstance().systemConfig_.enableInputMethodFeature = false;
1838 ImeEnabledInfo info;
1839 info.bundleName = TddUtil::currentBundleNameMock_;
1840 info.enabledStatus = EnabledStatus::BASIC_MODE;
1841 ImeEnabledCfg cfg;
1842 cfg.enabledInfos.push_back(info);
1843 ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.insert({ imsa_->userId_, cfg });
1844 EnabledStatus status = EnabledStatus::DISABLED;
1845 auto ret = inputMethodController_->GetInputMethodState(status);
1846 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1847 EXPECT_TRUE(status == EnabledStatus::BASIC_MODE);
1848 }
1849
1850 /**
1851 * @tc.name: testGetInputMethodState_004
1852 * @tc.desc: IMA
1853 * @tc.type: FUNC
1854 * @tc.require:
1855 */
1856 HWTEST_F(InputMethodControllerTest, testGetInputMethodState_004, TestSize.Level0)
1857 {
1858 IMSA_HILOGI("InputMethodControllerTest GetInputMethodState_004 Test START");
1859 ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.clear();
1860 ImeInfoInquirer::GetInstance().systemConfig_.enableFullExperienceFeature = false;
1861 ImeInfoInquirer::GetInstance().systemConfig_.enableInputMethodFeature = true;
1862 ImeEnabledInfo info;
1863 info.bundleName = TddUtil::currentBundleNameMock_;
1864 info.enabledStatus = EnabledStatus::BASIC_MODE;
1865 ImeEnabledCfg cfg;
1866 cfg.enabledInfos.push_back(info);
1867 ImeEnabledInfoManager::GetInstance().imeEnabledCfg_.insert({ imsa_->userId_, cfg });
1868 EnabledStatus status = EnabledStatus::DISABLED;
1869 auto ret = inputMethodController_->GetInputMethodState(status);
1870 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1871 EXPECT_TRUE(status == EnabledStatus::BASIC_MODE);
1872 }
1873
1874 /**
1875 * @tc.name: testIsDefaultImeSetAndEnableIme
1876 * @tc.desc: test IMC Reset
1877 * @tc.type: FUNC
1878 * @tc.require:
1879 */
1880 HWTEST_F(InputMethodControllerTest, testIsDefaultImeSetAndEnableIme, TestSize.Level0)
1881 {
1882 IMSA_HILOGI("IMC testIsDefaultImeSetAndEnableIme Test START");
1883 auto ret = inputMethodController_->IsDefaultImeSet();
1884 EXPECT_FALSE(ret);
1885 const std::string bundleName;
1886 auto enableRet = inputMethodController_->EnableIme(bundleName);
1887 EXPECT_NE(enableRet, ErrorCode::NO_ERROR);
1888 }
1889
1890 /**
1891 * @tc.name: testSendPrivateData_001
1892 * @tc.desc: IMC
1893 * @tc.type: FUNC
1894 * @tc.require:
1895 */
1896 HWTEST_F(InputMethodControllerTest, testSendPrivateData_001, TestSize.Level0)
1897 {
1898 IMSA_HILOGI("IMC testSendPrivateData_001 Test START");
1899 InputMethodEngineListenerImpl::ResetParam();
1900 auto ret = inputMethodController_->Attach(textListener_, false);
1901 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1902 std::unordered_map<std::string, PrivateDataValue> privateCommand;
1903 ret = inputMethodController_->SendPrivateData(privateCommand);
1904 EXPECT_EQ(ret, ErrorCode::ERROR_PRIVATE_COMMAND_IS_EMPTY);
1905 inputMethodController_->Close();
1906 }
1907
1908 /**
1909 * @tc.name: testSendPrivateData_002
1910 * @tc.desc: IMC
1911 * @tc.type: FUNC
1912 * @tc.require:
1913 */
1914 HWTEST_F(InputMethodControllerTest, testSendPrivateData_002, TestSize.Level0)
1915 {
1916 IMSA_HILOGI("IMC testSendPrivateData_002 Test START");
1917 InputMethodEngineListenerImpl::ResetParam();
1918 auto ret = inputMethodController_->Attach(textListener_, false);
1919 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1920 std::unordered_map<std::string, PrivateDataValue> privateCommand;
1921 PrivateDataValue privateDataValue = std::string("stringValue");
1922 privateCommand.emplace("value", privateDataValue);
1923 ret = inputMethodController_->SendPrivateData(privateCommand);
1924 EXPECT_NE(ret, ErrorCode::NO_ERROR);
1925 inputMethodController_->Close();
1926 }
1927
1928 /**
1929 * @tc.name: testSendPrivateData_003
1930 * @tc.desc: IMC
1931 * @tc.type: FUNC
1932 * @tc.require:
1933 */
1934 HWTEST_F(InputMethodControllerTest, testSendPrivateData_003, TestSize.Level0)
1935 {
1936 IMSA_HILOGI("IMC testSendPrivateData_003 Test START");
1937 InputMethodEngineListenerImpl::ResetParam();
1938 IdentityCheckerMock::SetSpecialSaUid(true);
1939 auto ret = inputMethodController_->Attach(textListener_, false);
1940 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1941 std::unordered_map<std::string, PrivateDataValue> privateCommand;
1942 PrivateDataValue privateDataValue = std::string("stringValue");
1943 privateCommand.emplace("value", privateDataValue);
1944 ret = inputMethodController_->SendPrivateData(privateCommand);
1945 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1946 inputMethodController_->Close();
1947 }
1948
1949 /**
1950 * @tc.name: testSendPrivateData_004
1951 * @tc.desc: IMC
1952 * @tc.type: FUNC
1953 * @tc.require:
1954 */
1955 HWTEST_F(InputMethodControllerTest, testSendPrivateData_004, TestSize.Level0)
1956 {
1957 IMSA_HILOGI("IMC testSendPrivateData_004 Test START");
1958 InputMethodEngineListenerImpl::ResetParam();
1959 IdentityCheckerMock::SetSpecialSaUid(true);
1960 auto ret = inputMethodController_->Attach(textListener_, false);
1961 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1962 std::unordered_map<std::string, PrivateDataValue> privateCommand;
1963 PrivateDataValue privateDataValue1 = std::string("stringValue");
1964 PrivateDataValue privateDataValue2 = true;
1965 PrivateDataValue privateDataValue3 = 0;
1966 privateCommand.emplace("value1", privateDataValue1);
1967 privateCommand.emplace("value2", privateDataValue2);
1968 privateCommand.emplace("value3", privateDataValue3);
1969 ret = inputMethodController_->SendPrivateData(privateCommand);
1970 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1971 inputMethodController_->Close();
1972 }
1973
1974 /**
1975 * @tc.name: testSendPrivateData_005
1976 * @tc.desc: IMC
1977 * @tc.type: FUNC
1978 * @tc.require:
1979 */
1980 HWTEST_F(InputMethodControllerTest, testSendPrivateData_005, TestSize.Level0)
1981 {
1982 IMSA_HILOGI("IMC testSendPrivateData_005 Test START");
1983 InputMethodEngineListenerImpl::ResetParam();
1984 IdentityCheckerMock::SetSpecialSaUid(true);
1985 std::unordered_map<std::string, PrivateDataValue> privateCommand;
1986 PrivateDataValue privateDataValue = std::string("stringValue");
1987 privateCommand.emplace("value", privateDataValue);
1988 auto ret = inputMethodController_->SendPrivateData(privateCommand);
1989 EXPECT_EQ(ret, ErrorCode::ERROR_SCENE_UNSUPPORTED);
1990 inputMethodController_->Close();
1991 }
1992
1993 /**
1994 * @tc.name: testSendPrivateData_006
1995 * @tc.desc: IMC
1996 * @tc.type: FUNC
1997 * @tc.require:
1998 */
1999 HWTEST_F(InputMethodControllerTest, testSendPrivateData_006, TestSize.Level0)
2000 {
2001 IMSA_HILOGI("IMC testSendPrivateData_006 Test START");
2002 InputMethodEngineListenerImpl::ResetParam();
2003 IdentityCheckerMock::SetSpecialSaUid(false);
2004 auto ret = inputMethodController_->Attach(textListener_, false);
2005 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
2006 std::unordered_map<std::string, PrivateDataValue> privateCommand;
2007 PrivateDataValue privateDataValue = std::string("stringValue");
2008 privateCommand.emplace("value", privateDataValue);
2009 ret = inputMethodController_->SendPrivateData(privateCommand);
2010 EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
2011 inputMethodController_->Close();
2012 }
2013
2014 /**
2015 * @tc.name: testSendPrivateData_007
2016 * @tc.desc: IMC
2017 * @tc.type: FUNC
2018 * @tc.require:
2019 */
2020 HWTEST_F(InputMethodControllerTest, testSendPrivateData_007, TestSize.Level0)
2021 {
2022 IMSA_HILOGI("IMC testSendPrivateData_007 Test START");
2023 InputMethodEngineListenerImpl::ResetParam();
2024 IdentityCheckerMock::SetSpecialSaUid(true);
2025 auto ret = inputMethodController_->Attach(textListener_, false);
2026 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
2027 std::unordered_map<std::string, PrivateDataValue> privateCommand;
2028 PrivateDataValue privateDataValue = std::string("stringValue");
2029 privateCommand.emplace("value", privateDataValue);
2030 inputMethodController_->clientInfo_.config.isSimpleKeyboardEnabled = true;
2031 ret = inputMethodController_->SendPrivateData(privateCommand);
2032 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
2033 inputMethodController_->clientInfo_.config.isSimpleKeyboardEnabled = false;
2034 inputMethodController_->Close();
2035 }
2036
2037 /**
2038 * @tc.name: testUpdateTextPreviewState
2039 * @tc.desc: test IMC Reset
2040 * @tc.type: FUNC
2041 * @tc.require:
2042 */
2043 HWTEST_F(InputMethodControllerTest, testUpdateTextPreviewState, TestSize.Level0)
2044 {
2045 IMSA_HILOGI("IMC testUpdateTextPreviewState Test START");
2046 ASSERT_NE(inputMethodController_, nullptr);
2047 inputMethodController_->textConfig_.inputAttribute.isTextPreviewSupported = false;
2048 inputMethodController_->UpdateTextPreviewState(true);
2049 EXPECT_TRUE(inputMethodController_->textConfig_.inputAttribute.isTextPreviewSupported);
2050 inputMethodController_->UpdateTextPreviewState(true);
2051 EXPECT_TRUE(inputMethodController_->textConfig_.inputAttribute.isTextPreviewSupported);
2052 }
2053
2054 /**
2055 * @tc.name: RegisterWindowScaleCallbackHandler
2056 * @tc.desc: test IMC
2057 * @tc.type: FUNC
2058 * @tc.require:
2059 */
2060 HWTEST_F(InputMethodControllerTest, RegisterWindowScaleCallbackHandler, TestSize.Level0)
2061 {
2062 IMSA_HILOGI("IMC RegisterWindowScaleCallbackHandler Test START");
2063 ASSERT_NE(inputMethodController_, nullptr);
2064 EXPECT_EQ(inputMethodController_->windowScaleCallback_, nullptr);
__anon669cc1890b02(uint32_t windowId, CursorInfo &cursorInfo) 2065 auto callback = [] (uint32_t windowId, CursorInfo &cursorInfo) {
2066 return 0;
2067 };
2068 auto res = inputMethodController_->RegisterWindowScaleCallbackHandler(std::move(callback));
2069 EXPECT_EQ(res, 0);
2070 EXPECT_NE(inputMethodController_->windowScaleCallback_, nullptr);
2071 }
2072
2073 /**
2074 * @tc.name: GetWindowScaleCoordinate
2075 * @tc.desc: test IMC
2076 * @tc.type: FUNC
2077 * @tc.require:
2078 */
2079 HWTEST_F(InputMethodControllerTest, GetWindowScaleCoordinate, TestSize.Level0)
2080 {
2081 IMSA_HILOGI("IMC GetWindowScaleCoordinate Test START");
2082 ASSERT_NE(inputMethodController_, nullptr);
2083 CursorInfo cursorInfo;
2084 cursorInfo.left = 100;
2085 cursorInfo.top = 100;
2086 cursorInfo.width = 100;
2087 cursorInfo.height = 100;
2088 uint32_t windowId = 100;
2089 inputMethodController_->GetWindowScaleCoordinate(windowId, cursorInfo);
2090 EXPECT_NEAR(cursorInfo.left, 100, 0.00001f);
2091 EXPECT_NEAR(cursorInfo.top, 100, 0.00001f);
2092 EXPECT_NEAR(cursorInfo.width, 100, 0.00001f);
2093 EXPECT_NEAR(cursorInfo.height, 100, 0.00001f);
__anon669cc1890c02(uint32_t windowId, CursorInfo &cursorInfo) 2094 auto callback = [] (uint32_t windowId, CursorInfo &cursorInfo) {
2095 cursorInfo.left++;
2096 cursorInfo.top++;
2097 cursorInfo.width++;
2098 cursorInfo.height++;
2099 return 0;
2100 };
2101 auto res = inputMethodController_->RegisterWindowScaleCallbackHandler(std::move(callback));
2102 EXPECT_EQ(res, 0);
2103 inputMethodController_->GetWindowScaleCoordinate(windowId, cursorInfo);
2104 EXPECT_NEAR(cursorInfo.left, 101, 0.00001f);
2105 EXPECT_NEAR(cursorInfo.top, 101, 0.00001f);
2106 EXPECT_NEAR(cursorInfo.width, 101, 0.00001f);
2107 EXPECT_NEAR(cursorInfo.height, 101, 0.00001f);
2108 }
2109
2110 /**
2111 * @tc.name: TestImcOptionalInputMethod
2112 * @tc.desc: Test ImcOptionalInputMethod
2113 * @tc.type: FUNC
2114 */
2115 HWTEST_F(InputMethodControllerTest, TestImcOptionalInputMethod, TestSize.Level0)
2116 {
2117 IMSA_HILOGI("ImeProxyTest::TestImcOptionalInputMethod");
2118 Property property;
2119 std::vector<SubProperty> subProps;
2120 auto ret = inputMethodController_->ListInputMethodSubtype(property, subProps);
2121 EXPECT_NE(ret, ErrorCode::NO_ERROR);
2122 ret = inputMethodController_->ShowOptionalInputMethod();
2123 EXPECT_NE(ret, ErrorCode::NO_ERROR);
2124 ret = inputMethodController_->DisplayOptionalInputMethod();
2125 EXPECT_NE(ret, ErrorCode::NO_ERROR);
2126 }
2127
2128 /**
2129 * @tc.name: TestSetSimpleKeyboardEnabled
2130 * @tc.desc: Test SetSimpleKeyboardEnabled
2131 * @tc.type: FUNC
2132 */
2133 HWTEST_F(InputMethodControllerTest, TestSetSimpleKeyboardEnabled, TestSize.Level0)
2134 {
2135 auto ret = inputMethodController_->SetSimpleKeyboardEnabled(true);
2136 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
2137 ret = inputMethodController_->SetSimpleKeyboardEnabled(false);
2138 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
2139 }
2140
2141 /**
2142 * @tc.name: TestNotifyOnInputStopFinished001
2143 * @tc.desc: Test NotifyOnInputStopFinished in 20ms
2144 * @tc.type: FUNC
2145 */
2146 HWTEST_F(InputMethodControllerTest, TestNotifyOnInputStopFinished001, TestSize.Level0)
2147 {
2148 IMSA_HILOGI("TestNotifyOnInputStopFinished001 START");
2149 sptr<OnInputStopNotifyStub> proxy = new (std::nothrow) OnInputStopNotifyServiceImpl(getpid());
2150 ASSERT_NE(proxy, nullptr);
2151 std::shared_ptr<OnInputStopNotifyProxy> channelProxy = std::make_shared<OnInputStopNotifyProxy>(proxy);
2152 auto sessionTemp = std::make_shared<PerUserSession>(0, nullptr);
2153 UserSessionManager::GetInstance().userSessions_.insert({0, sessionTemp});
2154 auto ret = channelProxy->NotifyOnInputStopFinished();
2155 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
2156 UserSessionManager::GetInstance().userSessions_.clear();
2157 }
2158
2159 /**
2160 * @tc.name: testOnInputStopAsync
2161 * @tc.desc: Bind IMSA.
2162 * @tc.type: FUNC
2163 */
2164 HWTEST_F(InputMethodControllerTest, TestOnInputStopAsync, TestSize.Level0)
2165 {
2166 IMSA_HILOGI("TestOnInputStopAsync START");
2167 auto sessionTemp = std::make_shared<PerUserSession>(0, nullptr);
2168 sptr<IInputClient> client = new (std::nothrow) InputClientServiceImpl();
2169 InputClientInfo clientInfo = { .client = client };
2170 std::shared_ptr<InputClientInfo> sharedClientInfo = std::make_shared<InputClientInfo>(clientInfo);
2171 isNotifyFinished_.SetValue(false);
2172 sessionTemp->StopClientInput(sharedClientInfo, false, false);
2173 isNotifyFinished_.SetValue(true);
2174 sessionTemp->StopClientInput(sharedClientInfo, false, false);
2175 EXPECT_TRUE(isNotifyFinished_.GetValue());
2176 }
2177
2178 /**
2179 * @tc.name: TestResponseDataChannel
2180 * @tc.desc: Test ResponseDataChannel: agent channel isnullptr
2181 * @tc.type: FUNC
2182 */
2183 HWTEST_F(InputMethodControllerTest, TestResponseDataChannel, TestSize.Level0)
2184 {
2185 IMSA_HILOGI("TestResponseDataChannel START");
2186 uint64_t msgId = 10;
2187 int32_t code = 5;
2188 ResponseData data = std::monostate{};
2189 auto ret = inputMethodController_->ResponseDataChannel(nullptr, msgId, code, data);
2190 EXPECT_NE(ret, ErrorCode::NO_ERROR);
2191 }
2192
2193 /**
2194 * @tc.name: TestEditorContentLock
2195 * @tc.desc: Test editorContentLock_
2196 * @tc.type: FUNC
2197 */
2198 HWTEST_F(InputMethodControllerTest, TestEditorContentLock, TestSize.Level0)
2199 {
2200 IMSA_HILOGI("InputMethodControllerTest::TestEditorContentLock START");
2201 multiThreadExecTotalNum_.store(0);
2202 SET_THREAD_NUM(InputMethodControllerTest::THREAD_NUM);
2203 GTEST_RUN_TASK(InputMethodControllerTest::EditorContentMultiTest);
2204 EXPECT_EQ(multiThreadExecTotalNum_.load(), THREAD_NUM * EACH_THREAD_CIRCULATION_TIME);
2205 }
2206
2207 /**
2208 * @tc.name: TestClientNullptr
2209 * @tc.desc: Test clientInfo.client is nullptr
2210 * @tc.type: FUNC
2211 */
2212 HWTEST_F(InputMethodControllerTest, TestClientNullptr, TestSize.Level0)
2213 {
2214 IMSA_HILOGI("TestClientNullptr START");
2215 auto sessionTemp = std::make_shared<PerUserSession>(0, nullptr);
2216 sptr<IInputClient> client = new (std::nothrow) InputClientServiceImpl();
2217 InputClientInfo clientInfo = { .client = nullptr };
2218
2219 sessionTemp->GetWant(nullptr);
2220 auto ret = sessionTemp->OnUpdateListenEventFlag(clientInfo);
2221 EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
2222
2223 sessionTemp->HandleBindImeChanged(clientInfo, nullptr);
2224 std::shared_ptr<InputClientInfo> ptr = nullptr;
2225 sessionTemp->ClearRequestKeyboardReason(ptr);
2226 auto info = std::make_shared<InputClientInfo>();
2227 EXPECT_NE(info, nullptr);
2228 sessionTemp->ClearRequestKeyboardReason(info);
2229 }
2230
2231 /**
2232 * @tc.name: TestEventCallback
2233 * @tc.desc: Test TestEventCallback
2234 * @tc.type: FUNC
2235 */
2236 HWTEST_F(InputMethodControllerTest, TestEventCallback, TestSize.Level0)
2237 {
2238 IMSA_HILOGI("TestEventCallback START");
2239 auto eventcallback = std::make_shared<InputEventCallback>();
2240 std::shared_ptr<MMI::KeyEvent> keyevent = nullptr;
2241 eventcallback->OnInputEvent(keyevent);
2242 EXPECT_EQ(keyevent, nullptr);
2243 eventcallback->OnInputEvent(keyEvent_);
2244 }
2245
2246 /**
2247 * @tc.name: TestGetKeyEventCbInfo
2248 * @tc.desc: Test GetKeyEventCbInfo
2249 * @tc.type: FUNC
2250 */
2251 HWTEST_F(InputMethodControllerTest, TestGetKeyEventCbInfo, TestSize.Level0)
2252 {
2253 IMSA_HILOGI("TestGetKeyEventCbInfo START");
2254 KeyEventResultHandler keyEventRetHandler;
2255 keyEventRetHandler.keyEventCbHandlers_.clear();
2256 uint64_t cbId = 13;
2257 KeyEventCbInfo info;
2258 auto ret = keyEventRetHandler.GetKeyEventCbInfo(cbId, info);
2259 EXPECT_NE(ret, ErrorCode::NO_ERROR);
2260
2261 KeyEventCbInfo cbInfo;
2262 keyEventRetHandler.keyEventCbHandlers_.insert_or_assign(cbId, cbInfo);
2263 ret = keyEventRetHandler.GetKeyEventCbInfo(cbId, info);
2264 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
2265 EXPECT_EQ(info.callback, nullptr);
2266
__anon669cc1890d02(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) 2267 auto cb = [](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) {};
2268 cbInfo.callback = cb;
2269 keyEventRetHandler.keyEventCbHandlers_.insert_or_assign(cbId, cbInfo);
2270 ret = keyEventRetHandler.GetKeyEventCbInfo(cbId, info);
2271 EXPECT_EQ(ret, ErrorCode::NO_ERROR);
2272 EXPECT_NE(info.callback, nullptr);
2273 }
2274
2275 /**
2276 * @tc.name: TestRemoveKeyEventCbInfo
2277 * @tc.desc: Test RemoveKeyEventCbInfo
2278 * @tc.type: FUNC
2279 */
2280 HWTEST_F(InputMethodControllerTest, TestRemoveKeyEventCbInfo, TestSize.Level0)
2281 {
2282 IMSA_HILOGI("TestRemoveKeyEventCbInfo START");
2283 KeyEventResultHandler keyEventRetHandler;
2284 keyEventRetHandler.keyEventCbHandlers_.clear();
2285 uint64_t cbId = 13;
__anon669cc1890e02(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) 2286 auto cb = [](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) {};
2287 KeyEventCbInfo cbInfo{ nullptr, cb };
2288 keyEventRetHandler.keyEventCbHandlers_.insert_or_assign(cbId, cbInfo);
2289 EXPECT_EQ(keyEventRetHandler.keyEventCbHandlers_.size(), 1);
2290
2291 uint64_t cbId1 = 14;
2292 keyEventRetHandler.RemoveKeyEventCbInfo(cbId1);
2293 EXPECT_EQ(keyEventRetHandler.keyEventCbHandlers_.size(), 1);
2294
2295 keyEventRetHandler.RemoveKeyEventCbInfo(cbId);
2296 EXPECT_TRUE(keyEventRetHandler.keyEventCbHandlers_.empty());
2297 }
2298
2299 /**
2300 * @tc.name: TestHandleKeyEventResult
2301 * @tc.desc: Test HandleKeyEventResult
2302 * @tc.type: FUNC
2303 */
2304 HWTEST_F(InputMethodControllerTest, TestHandleKeyEventResult, TestSize.Level0)
2305 {
2306 IMSA_HILOGI("TestHandleKeyEventResult START");
2307 KeyEventResultHandler keyEventRetHandler;
2308 keyEventRetHandler.keyEventCbHandlers_.clear();
2309 uint64_t cbId = 13;
2310 KeyEventCbInfo cbInfo;
2311 keyEventRetHandler.keyEventCbHandlers_.insert_or_assign(cbId, cbInfo);
2312
2313 uint64_t cbId1 = 15;
2314 keyEventRetHandler.HandleKeyEventResult(cbId1, true);
2315 EXPECT_EQ(keyEventRetHandler.keyEventCbHandlers_.size(), 1);
2316
2317 keyEventRetHandler.HandleKeyEventResult(cbId, true);
2318 EXPECT_TRUE(keyEventRetHandler.keyEventCbHandlers_.empty());
2319
__anon669cc1890f02(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) 2320 auto cb = [](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) {};
2321 cbInfo.callback = cb;
2322 keyEventRetHandler.keyEventCbHandlers_.insert_or_assign(cbId, cbInfo);
2323 keyEventRetHandler.HandleKeyEventResult(cbId, true);
2324 EXPECT_TRUE(keyEventRetHandler.keyEventCbHandlers_.empty());
2325 }
2326
2327 /**
2328 * @tc.name: TestClearKeyEventCbInfo
2329 * @tc.desc: Test ClearKeyEventCbInfo
2330 * @tc.type: FUNC
2331 */
2332 HWTEST_F(InputMethodControllerTest, TestClearKeyEventCbInfo, TestSize.Level0)
2333 {
2334 IMSA_HILOGI("TestClearKeyEventCbInfo START");
2335 KeyEventResultHandler keyEventRetHandler;
2336 keyEventRetHandler.keyEventCbHandlers_.clear();
2337 uint64_t cbId = 13;
2338 KeyEventCbInfo cbInfo;
2339 keyEventRetHandler.keyEventCbHandlers_.insert_or_assign(cbId, cbInfo);
2340 uint64_t cbId1 = 15;
__anon669cc1891002(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) 2341 auto cb = [](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) {};
2342 cbInfo.callback = cb;
2343 keyEventRetHandler.keyEventCbHandlers_.insert_or_assign(cbId1, cbInfo);
2344 keyEventRetHandler.ClearKeyEventCbInfo();
2345 EXPECT_TRUE(keyEventRetHandler.keyEventCbHandlers_.empty());
2346 }
2347 } // namespace MiscServices
2348 } // namespace OHOS
2349