• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #define private public
17 #define protected public
18 #include "input_method_ability.h"
19 #include "input_method_controller.h"
20 #include "input_method_system_ability.h"
21 #include "task_manager.h"
22 #undef private
23 #include <event_handler.h>
24 #include <gtest/gtest.h>
25 #include <string_ex.h>
26 #include <sys/time.h>
27 
28 #include <condition_variable>
29 #include <cstdint>
30 #include <functional>
31 #include <mutex>
32 #include <string>
33 #include <thread>
34 #include <vector>
35 
36 #include "global.h"
37 #include "iinput_method_agent.h"
38 #include "iinput_method_system_ability.h"
39 #include "identity_checker_mock.h"
40 #include "input_client_stub.h"
41 #include "input_data_channel_service_impl.h"
42 #include "input_method_ability.h"
43 #include "input_method_controller.h"
44 #include "input_method_engine_listener_impl.h"
45 #include "input_method_utils.h"
46 #include "keyboard_listener.h"
47 #include "message_parcel.h"
48 #include "tdd_util.h"
49 #include "text_listener.h"
50 
51 using namespace testing::ext;
52 namespace OHOS {
53 namespace MiscServices {
54 class KeyboardListenerImpl : public KeyboardListener {
55 public:
KeyboardListenerImpl()56     KeyboardListenerImpl() {};
~KeyboardListenerImpl()57     ~KeyboardListenerImpl() {};
58     static int32_t keyCode_;
59     static int32_t keyStatus_;
60     static CursorInfo cursorInfo_;
61     bool OnDealKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, uint64_t cbId,
62         const sptr<IRemoteObject> &channelObject) override;
63     bool OnKeyEvent(int32_t keyCode, int32_t keyStatus, sptr<KeyEventConsumerProxy> &consumer) override;
64     bool OnKeyEvent(const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer) override;
65     void OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height) override;
66     void OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) override;
67     void OnTextChange(const std::string &text) override;
68     void OnEditorAttributeChange(const InputAttribute &inputAttribute) override;
69 };
70 int32_t KeyboardListenerImpl::keyCode_ = 0;
71 int32_t KeyboardListenerImpl::keyStatus_ = 0;
72 CursorInfo KeyboardListenerImpl::cursorInfo_ = {};
OnKeyEvent(int32_t keyCode,int32_t keyStatus,sptr<KeyEventConsumerProxy> & consumer)73 bool KeyboardListenerImpl::OnKeyEvent(int32_t keyCode, int32_t keyStatus, sptr<KeyEventConsumerProxy> &consumer)
74 {
75     IMSA_HILOGD("KeyboardListenerImpl::OnKeyEvent %{public}d %{public}d", keyCode, keyStatus);
76     keyCode_ = keyCode;
77     keyStatus_ = keyStatus;
78     return true;
79 }
OnKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,sptr<KeyEventConsumerProxy> & consumer)80 bool KeyboardListenerImpl::OnKeyEvent(
81     const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer)
82 {
83     return true;
84 }
OnDealKeyEvent(const std::shared_ptr<MMI::KeyEvent> & keyEvent,uint64_t cbId,const sptr<IRemoteObject> & channelObject)85 bool KeyboardListenerImpl::OnDealKeyEvent(
86     const std::shared_ptr<MMI::KeyEvent> &keyEvent, uint64_t cbId, const sptr<IRemoteObject> &channelObject)
87 {
88     sptr<KeyEventConsumerProxy> consumer = new (std::nothrow) KeyEventConsumerProxy(nullptr);
89     bool isKeyCodeConsume = OnKeyEvent(keyEvent->GetKeyCode(), keyEvent->GetKeyAction(), consumer);
90     bool isKeyEventConsume = OnKeyEvent(keyEvent, consumer);
91     InputMethodAbility::GetInstance().HandleKeyEventResult(cbId, isKeyEventConsume | isKeyCodeConsume, channelObject);
92     return true;
93 }
OnCursorUpdate(int32_t positionX,int32_t positionY,int32_t height)94 void KeyboardListenerImpl::OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height)
95 {
96     IMSA_HILOGD("KeyboardListenerImpl::OnCursorUpdate %{public}d %{public}d %{public}d", positionX, positionY, height);
97     cursorInfo_ = { static_cast<double>(positionX), static_cast<double>(positionY), 0, static_cast<double>(height) };
98 }
OnSelectionChange(int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)99 void KeyboardListenerImpl::OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd) { }
OnTextChange(const std::string & text)100 void KeyboardListenerImpl::OnTextChange(const std::string &text) { }
OnEditorAttributeChange(const InputAttribute & inputAttribute)101 void KeyboardListenerImpl::OnEditorAttributeChange(const InputAttribute &inputAttribute) { }
102 
103 class InputMethodEditorTest : public testing::Test {
104 public:
105     static void SetUpTestCase(void);
106     static void TearDownTestCase(void);
107     void SetUp();
108     void TearDown();
109     static sptr<InputMethodController> inputMethodController_;
110     static InputMethodAbility &inputMethodAbility_;
111     static std::shared_ptr<MMI::KeyEvent> keyEvent_;
112     static std::shared_ptr<KeyboardListenerImpl> kbListener_;
113     static std::shared_ptr<InputMethodEngineListenerImpl> imeListener_;
114     static sptr<OnTextChangedListener> textListener_;
115     static sptr<InputMethodSystemAbility> imsa_;
116     static constexpr int32_t waitTaskEmptyTimes_ = 100;
117     static constexpr int32_t waitTaskEmptyinterval_ = 20;
118 
IsTaskEmpty()119     static bool IsTaskEmpty()
120     {
121         return TaskManager::GetInstance().curTask_ == nullptr &&
122                TaskManager::GetInstance().amsTasks_.empty() &&
123                TaskManager::GetInstance().imaTasks_.empty() &&
124                TaskManager::GetInstance().imsaTasks_.empty() &&
125                TaskManager::GetInstance().innerTasks_.empty();
126     }
127 };
128 sptr<InputMethodController> InputMethodEditorTest::inputMethodController_;
129 InputMethodAbility &InputMethodEditorTest::inputMethodAbility_ = InputMethodAbility::GetInstance();
130 std::shared_ptr<MMI::KeyEvent> InputMethodEditorTest::keyEvent_;
131 std::shared_ptr<KeyboardListenerImpl> InputMethodEditorTest::kbListener_;
132 std::shared_ptr<InputMethodEngineListenerImpl> InputMethodEditorTest::imeListener_;
133 sptr<OnTextChangedListener> InputMethodEditorTest::textListener_;
134 sptr<InputMethodSystemAbility> InputMethodEditorTest::imsa_;
135 
SetUpTestCase(void)136 void InputMethodEditorTest::SetUpTestCase(void)
137 {
138     IMSA_HILOGI("InputMethodEditorTest::SetUpTestCase");
139     IdentityCheckerMock::ResetParam();
140 
141     imsa_ = new (std::nothrow) InputMethodSystemAbility();
142     if (imsa_ == nullptr) {
143         return;
144     }
145     imsa_->OnStart();
146     imsa_->userId_ = TddUtil::GetCurrentUserId();
147     imsa_->identityChecker_ = std::make_shared<IdentityCheckerMock>();
148 
149     inputMethodAbility_.abilityManager_ = imsa_;
150     TddUtil::InitCurrentImePermissionInfo();
151     IdentityCheckerMock::SetBundleName(TddUtil::currentBundleNameMock_);
152     inputMethodAbility_.SetCoreAndAgent();
153     kbListener_ = std::make_shared<KeyboardListenerImpl>();
154     imeListener_ = std::make_shared<InputMethodEngineListenerImpl>();
155     inputMethodAbility_.SetKdListener(kbListener_);
156     inputMethodAbility_.SetImeListener(imeListener_);
157 
158     textListener_ = new TextListener();
159     inputMethodController_ = InputMethodController::GetInstance();
160     inputMethodController_->abilityManager_ = imsa_;
161 
162     keyEvent_ = MMI::KeyEvent::Create();
163     constexpr int32_t keyAction = 2;
164     constexpr int32_t keyCode = 2001;
165     keyEvent_->SetKeyAction(keyAction);
166     keyEvent_->SetKeyCode(keyCode);
167     IdentityCheckerMock::SetFocused(true);
168     int32_t ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, false);
169     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
170     ret = InputMethodEditorTest::inputMethodController_->Close();
171     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
172     IdentityCheckerMock::SetFocused(false);
173     TextListener::ResetParam();
174 }
175 
TearDownTestCase(void)176 void InputMethodEditorTest::TearDownTestCase(void)
177 {
178     IMSA_HILOGI("InputMethodEditorTest::TearDownTestCase");
179     TextListener::ResetParam();
180     IdentityCheckerMock::ResetParam();
181     imsa_->OnStop();
182 }
183 
SetUp(void)184 void InputMethodEditorTest::SetUp(void)
185 {
186     IMSA_HILOGI("InputMethodEditorTest::SetUp");
187     TaskManager::GetInstance().SetInited(true);
188 }
189 
TearDown(void)190 void InputMethodEditorTest::TearDown(void)
191 {
192     IMSA_HILOGI("InputMethodEditorTest::TearDown");
193     BlockRetry(waitTaskEmptyinterval_, waitTaskEmptyTimes_, IsTaskEmpty);
194     TaskManager::GetInstance().Reset();
195 }
196 
197 /**
198  * @tc.name: testIMCAttachUnfocused
199  * @tc.desc: InputMethodEditorTest Attach.
200  * @tc.type: FUNC
201  */
202 HWTEST_F(InputMethodEditorTest, testIMCAttachUnfocused, TestSize.Level1)
203 {
204     IMSA_HILOGI("InputMethodEditorTest Attach Unfocused Test START");
205     int32_t ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, false);
206     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOCUSED);
207     ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_);
208     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOCUSED);
209     ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, true);
210     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOCUSED);
211 }
212 
213 /**
214  * @tc.name: test Unfocused
215  * @tc.desc: InputMethodEditorTest Unfocused
216  * @tc.type: FUNC
217  */
218 HWTEST_F(InputMethodEditorTest, testUnfocused, TestSize.Level1)
219 {
220     IMSA_HILOGI("InputMethodEditorTest Unfocused Test START");
221     int32_t ret = InputMethodEditorTest::inputMethodController_->ShowTextInput();
222     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
223     ret = InputMethodEditorTest::inputMethodController_->DispatchKeyEvent(
__anonbfadf78c0102(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) 224         InputMethodEditorTest::keyEvent_, [](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) {});
225     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
226     ret = InputMethodEditorTest::inputMethodController_->ShowSoftKeyboard();
227     EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
228     ret = InputMethodEditorTest::inputMethodController_->HideSoftKeyboard();
229     EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
230     ret = InputMethodEditorTest::inputMethodController_->StopInputSession();
231     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOCUSED);
232     ret = InputMethodEditorTest::inputMethodController_->ShowCurrentInput();
233     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
234     ret = InputMethodEditorTest::inputMethodController_->HideCurrentInput();
235     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
236     ret = InputMethodEditorTest::inputMethodController_->OnCursorUpdate({});
237     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
238     ret = InputMethodEditorTest::inputMethodController_->OnSelectionChange(Str8ToStr16(""), 0, 0);
239     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
240     ret = InputMethodEditorTest::inputMethodController_->SetCallingWindow(1);
241     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
242     ret = InputMethodEditorTest::inputMethodController_->OnConfigurationChange({});
243     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
244 }
245 
246 /**
247  * @tc.name: testRequestInput001.
248  * @tc.desc: InputMethodEditorTest RequestShowInput/RequestHideInput neither permitted nor focused.
249  * @tc.type: FUNC
250  */
251 HWTEST_F(InputMethodEditorTest, testRequestInput001, TestSize.Level1)
252 {
253     IMSA_HILOGI("InputMethodEditorTest testRequestInput001 Test START");
254     int32_t ret = InputMethodEditorTest::inputMethodController_->RequestShowInput();
255     EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
256     ret = InputMethodEditorTest::inputMethodController_->RequestHideInput();
257     EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
258 }
259 
260 /**
261  * @tc.name: testRequestInput002.
262  * @tc.desc: InputMethodEditorTest RequestShowInput/RequestHideInput with permitted and not focused.
263  * @tc.type: FUNC
264  */
265 HWTEST_F(InputMethodEditorTest, testRequestInput002, TestSize.Level1)
266 {
267     IMSA_HILOGI("InputMethodEditorTest testRequestInput002 Test START");
268     IdentityCheckerMock::SetPermission(true);
269     int32_t ret = InputMethodEditorTest::inputMethodController_->RequestShowInput();
270     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
271     ret = InputMethodEditorTest::inputMethodController_->RequestHideInput();
272     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
273     IdentityCheckerMock::SetPermission(false);
274 }
275 
276 /**
277  * @tc.name: test AttachFocused
278  * @tc.desc: InputMethodEditorTest Attach Focused
279  * @tc.type: FUNC
280  */
281 HWTEST_F(InputMethodEditorTest, testAttachFocused, TestSize.Level1)
282 {
283     IMSA_HILOGI("InputMethodEditorTest Attach Focused Test START");
284     IdentityCheckerMock::SetFocused(true);
285     InputMethodEditorTest::imeListener_->isInputStart_ = false;
286     InputMethodEditorTest::imeListener_->keyboardState_ = false;
287     int32_t ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, false);
288     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
289     std::this_thread::sleep_for(std::chrono::seconds(2));
290 
291     InputMethodEditorTest::imeListener_->isInputStart_ = false;
292     InputMethodEditorTest::imeListener_->keyboardState_ = false;
293     ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_);
294     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
295     std::this_thread::sleep_for(std::chrono::seconds(2));
296     EXPECT_EQ(TextListener::keyboardStatus_, KeyboardStatus::SHOW);
297     EXPECT_TRUE(InputMethodEngineListenerImpl::keyboardState_);
298 
299     InputMethodEditorTest::imeListener_->isInputStart_ = false;
300     InputMethodEditorTest::imeListener_->keyboardState_ = false;
301     ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, true);
302     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
303     std::this_thread::sleep_for(std::chrono::seconds(2));
304     EXPECT_EQ(TextListener::keyboardStatus_, KeyboardStatus::SHOW);
305     EXPECT_TRUE(InputMethodEngineListenerImpl::keyboardState_);
306 
307     InputMethodEditorTest::inputMethodController_->Close();
308     IdentityCheckerMock::SetFocused(false);
309 }
310 
311 /**
312  * @tc.name: testShowSoftKeyboard
313  * @tc.desc: InputMethodEditorTest ShowSoftKeyboard
314  * @tc.type: FUNC
315  */
316 HWTEST_F(InputMethodEditorTest, testShowSoftKeyboard, TestSize.Level1)
317 {
318     IMSA_HILOGI("InputMethodEditorTest ShowSoftKeyboard Test START");
319     IdentityCheckerMock::SetFocused(true);
320     IdentityCheckerMock::SetPermission(true);
321     InputMethodEditorTest::imeListener_->keyboardState_ = false;
322     TextListener::ResetParam();
323     int32_t ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, false);
324     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
325 
326     ret = InputMethodEditorTest::inputMethodController_->ShowSoftKeyboard();
327     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
328     std::this_thread::sleep_for(std::chrono::seconds(2));
329     EXPECT_TRUE(imeListener_->keyboardState_ && TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::SHOW));
330     InputMethodEditorTest::inputMethodController_->Close();
331     IdentityCheckerMock::SetFocused(false);
332     IdentityCheckerMock::SetPermission(false);
333     BlockRetry(waitTaskEmptyinterval_, waitTaskEmptyTimes_, IsTaskEmpty);
334 }
335 
336 /**
337  * @tc.name: testIMCHideTextInput.
338  * @tc.desc: InputMethodEditorTest testHideTextInput.
339  * @tc.type: FUNC
340  */
341 HWTEST_F(InputMethodEditorTest, testIMCHideTextInput, TestSize.Level1)
342 {
343     IMSA_HILOGI("InputMethodEditorTest HideTextInputAndShowTextInput Test START");
344     IdentityCheckerMock::SetFocused(true);
345     IdentityCheckerMock::SetPermission(true);
346     int32_t ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, true);
347     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
348 
349     imeListener_->keyboardState_ = true;
350     InputMethodEditorTest::inputMethodController_->HideTextInput();
351     ret = InputMethodEditorTest::inputMethodController_->DispatchKeyEvent(
__anonbfadf78c0202(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) 352         InputMethodEditorTest::keyEvent_, [](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) {});
353     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
354     ret = InputMethodEditorTest::inputMethodController_->ShowSoftKeyboard();
355     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
356     ret = InputMethodEditorTest::inputMethodController_->HideSoftKeyboard();
357     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
358     ret = InputMethodEditorTest::inputMethodController_->ShowCurrentInput();
359     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
360     ret = InputMethodEditorTest::inputMethodController_->HideCurrentInput();
361     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
362     ret = InputMethodEditorTest::inputMethodController_->OnCursorUpdate({});
363     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
364     ret = InputMethodEditorTest::inputMethodController_->OnSelectionChange(Str8ToStr16(""), 0, 0);
365     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
366     ret = InputMethodEditorTest::inputMethodController_->SetCallingWindow(1);
367     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
368     ret = InputMethodEditorTest::inputMethodController_->OnConfigurationChange({});
369     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
370     InputMethodEditorTest::inputMethodController_->Close();
371     IdentityCheckerMock::SetFocused(false);
372     IdentityCheckerMock::SetPermission(false);
373     BlockRetry(waitTaskEmptyinterval_, waitTaskEmptyTimes_, IsTaskEmpty);
374 }
375 
376 /**
377  * @tc.name: testIMCDeactivateClient.
378  * @tc.desc: InputMethodEditorTest testIMCDeactivateClient.
379  * @tc.type: FUNC
380  */
381 HWTEST_F(InputMethodEditorTest, testIMCDeactivateClient, TestSize.Level1)
382 {
383     IMSA_HILOGI("InputMethodEditorTest testIMCDeactivateClient Test START");
384     IdentityCheckerMock::SetFocused(true);
385     IdentityCheckerMock::SetPermission(true);
386     int32_t ret = inputMethodController_->Attach(InputMethodEditorTest::textListener_, true);
387     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
388 
389     inputMethodController_->DeactivateClient();
390     ret = inputMethodController_->DispatchKeyEvent(
__anonbfadf78c0302(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) 391         InputMethodEditorTest::keyEvent_, [](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) {});
392     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
393     ret = inputMethodController_->ShowTextInput();
394     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
395     ret = inputMethodController_->HideTextInput();
396     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
397     ret = inputMethodController_->ShowSoftKeyboard();
398     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
399     ret = inputMethodController_->HideSoftKeyboard();
400     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
401     ret = inputMethodController_->ShowCurrentInput();
402     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
403     ret = inputMethodController_->HideCurrentInput();
404     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
405     ret = inputMethodController_->OnCursorUpdate({});
406     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
407     ret = inputMethodController_->OnSelectionChange(Str8ToStr16(""), 0, 0);
408     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
409     ret = inputMethodController_->SetCallingWindow(1);
410     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
411     ret = inputMethodController_->OnConfigurationChange({});
412     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
413 
414     ret = inputMethodController_->Attach(InputMethodEditorTest::textListener_, true);
415     ret = inputMethodController_->ShowTextInput();
416     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
417     InputMethodEditorTest::inputMethodController_->Close();
418     IdentityCheckerMock::SetFocused(false);
419     IdentityCheckerMock::SetPermission(false);
420     BlockRetry(waitTaskEmptyinterval_, waitTaskEmptyTimes_, IsTaskEmpty);
421 }
422 
423 /**
424  * @tc.name: testShowTextInput
425  * @tc.desc: InputMethodEditorTest ShowTextInput
426  * @tc.type: FUNC
427  */
428 HWTEST_F(InputMethodEditorTest, testShowTextInput, TestSize.Level1)
429 {
430     IMSA_HILOGI("InputMethodEditorTest ShowTextInput Test START");
431     IdentityCheckerMock::SetFocused(true);
432     int32_t ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, true);
433     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
434     InputMethodEditorTest::inputMethodController_->HideTextInput();
435 
436     ret = InputMethodEditorTest::inputMethodController_->ShowTextInput();
437     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
438     bool consumeResult = false;
439     ret = InputMethodEditorTest::inputMethodController_->DispatchKeyEvent(InputMethodEditorTest::keyEvent_,
__anonbfadf78c0402(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) 440         [&consumeResult](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) { consumeResult = isConsumed; });
441     usleep(1000);
442     ret =
443         ret && kbListener_->keyCode_ == keyEvent_->GetKeyCode() && kbListener_->keyStatus_ == keyEvent_->GetKeyAction();
444     EXPECT_TRUE(consumeResult);
445     InputMethodEditorTest::inputMethodController_->Close();
446     IdentityCheckerMock::SetFocused(false);
447     BlockRetry(waitTaskEmptyinterval_, waitTaskEmptyTimes_, IsTaskEmpty);
448 }
449 
450 /**
451  * @tc.name: testIMCClose.
452  * @tc.desc: InputMethodEditorTest Close.
453  * @tc.type: FUNC
454  */
455 HWTEST_F(InputMethodEditorTest, testIMCClose, TestSize.Level1)
456 {
457     IMSA_HILOGI("IMC Close Test START");
458     IdentityCheckerMock::SetFocused(true);
459     IdentityCheckerMock::SetPermission(true);
460     int32_t ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, true);
461     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
462     InputMethodEditorTest::inputMethodController_->Close();
463 
464     ret = InputMethodEditorTest::inputMethodController_->ShowTextInput();
465     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
466     ret = InputMethodEditorTest::inputMethodController_->DispatchKeyEvent(
__anonbfadf78c0502(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) 467         InputMethodEditorTest::keyEvent_, [](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) {});
468     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
469     ret = InputMethodEditorTest::inputMethodController_->ShowSoftKeyboard();
470     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOUND);
471     ret = InputMethodEditorTest::inputMethodController_->HideSoftKeyboard();
472     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOUND);
473     ret = InputMethodEditorTest::inputMethodController_->ShowCurrentInput();
474     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
475     ret = InputMethodEditorTest::inputMethodController_->HideCurrentInput();
476     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
477     ret = InputMethodEditorTest::inputMethodController_->OnCursorUpdate({});
478     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
479     ret = InputMethodEditorTest::inputMethodController_->OnSelectionChange(Str8ToStr16(""), 0, 0);
480     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
481     ret = InputMethodEditorTest::inputMethodController_->SetCallingWindow(1);
482     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
483     ret = InputMethodEditorTest::inputMethodController_->OnConfigurationChange({});
484     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
485     IdentityCheckerMock::SetFocused(false);
486     IdentityCheckerMock::SetPermission(false);
487     BlockRetry(waitTaskEmptyinterval_, waitTaskEmptyTimes_, IsTaskEmpty);
488 }
489 
490 /**
491  * @tc.name: testRequestShowInput.
492  * @tc.desc: InputMethodEditorTest testRequestShowInput with focused.
493  * @tc.type: FUNC
494  */
495 HWTEST_F(InputMethodEditorTest, testRequestShowInput, TestSize.Level1)
496 {
497     IMSA_HILOGI("InputMethodEditorTest testRequestShowInput Test START");
498     IdentityCheckerMock::SetFocused(true);
499     imeListener_->keyboardState_ = false;
500     int32_t ret = InputMethodEditorTest::inputMethodController_->RequestShowInput();
501     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
502     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitKeyboardStatus(true));
503     IdentityCheckerMock::SetFocused(false);
504 }
505 
506 /**
507  * @tc.name: testRequestHideInput_001.
508  * @tc.desc: InputMethodEditorTest testRequestHideInput with focused.
509  * @tc.type: FUNC
510  */
511 HWTEST_F(InputMethodEditorTest, testRequestHideInput_001, TestSize.Level1)
512 {
513     IMSA_HILOGI("InputMethodEditorTest testRequestHideInput_001 Test START");
514     IdentityCheckerMock::SetFocused(true);
515     imeListener_->keyboardState_ = true;
516     int32_t ret = InputMethodEditorTest::inputMethodController_->RequestHideInput();
517     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
518     IdentityCheckerMock::SetFocused(false);
519 }
520 
521 /**
522  * @tc.name: testRequestHideInput_002.
523  * @tc.desc: InputMethodEditorTest testRequestHideInput with focused.
524  * @tc.type: FUNC
525  */
526 HWTEST_F(InputMethodEditorTest, testRequestHideInput_002, TestSize.Level1)
527 {
528     IMSA_HILOGI("InputMethodEditorTest testRequestHideInput_002 Test START");
529     IdentityCheckerMock::SetFocused(true);
530     int32_t ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, false);
531     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
532     std::this_thread::sleep_for(std::chrono::seconds(2));
533 
534     imeListener_->keyboardState_ = true;
535     ret = InputMethodEditorTest::inputMethodController_->RequestHideInput();
536     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
537     std::this_thread::sleep_for(std::chrono::seconds(2));
538     EXPECT_FALSE(imeListener_->keyboardState_);
539     InputMethodEditorTest::inputMethodController_->Close();
540     IdentityCheckerMock::SetFocused(false);
541 }
542 } // namespace MiscServices
543 } // namespace OHOS
544