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 #undef private
22 #include <event_handler.h>
23 #include <gtest/gtest.h>
24 #include <string_ex.h>
25 #include <sys/time.h>
26 
27 #include <condition_variable>
28 #include <cstdint>
29 #include <functional>
30 #include <mutex>
31 #include <string>
32 #include <thread>
33 #include <vector>
34 
35 #include "global.h"
36 #include "i_input_method_agent.h"
37 #include "i_input_method_system_ability.h"
38 #include "identity_checker_mock.h"
39 #include "input_client_stub.h"
40 #include "input_data_channel_stub.h"
41 #include "input_method_ability.h"
42 #include "input_method_controller.h"
43 #include "input_method_engine_listener_impl.h"
44 #include "input_method_system_ability_proxy.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(
62         const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer) 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,sptr<KeyEventConsumerProxy> & consumer)85 bool KeyboardListenerImpl::OnDealKeyEvent(
86     const std::shared_ptr<MMI::KeyEvent> &keyEvent, sptr<KeyEventConsumerProxy> &consumer)
87 {
88     bool isKeyCodeConsume = OnKeyEvent(keyEvent->GetKeyCode(), keyEvent->GetKeyAction(), consumer);
89     bool isKeyEventConsume = OnKeyEvent(keyEvent, consumer);
90     if (consumer != nullptr) {
91         consumer->OnKeyEventResult(isKeyEventConsume | isKeyCodeConsume);
92     }
93     return true;
94 }
OnCursorUpdate(int32_t positionX,int32_t positionY,int32_t height)95 void KeyboardListenerImpl::OnCursorUpdate(int32_t positionX, int32_t positionY, int32_t height)
96 {
97     IMSA_HILOGD("KeyboardListenerImpl::OnCursorUpdate %{public}d %{public}d %{public}d", positionX, positionY, height);
98     cursorInfo_ = { static_cast<double>(positionX), static_cast<double>(positionY), 0, static_cast<double>(height) };
99 }
OnSelectionChange(int32_t oldBegin,int32_t oldEnd,int32_t newBegin,int32_t newEnd)100 void KeyboardListenerImpl::OnSelectionChange(int32_t oldBegin, int32_t oldEnd, int32_t newBegin, int32_t newEnd)
101 {
102 }
OnTextChange(const std::string & text)103 void KeyboardListenerImpl::OnTextChange(const std::string &text)
104 {
105 }
OnEditorAttributeChange(const InputAttribute & inputAttribute)106 void KeyboardListenerImpl::OnEditorAttributeChange(const InputAttribute &inputAttribute)
107 {
108 }
109 
110 class InputMethodEditorTest : public testing::Test {
111 public:
112     static void SetUpTestCase(void);
113     static void TearDownTestCase(void);
114     void SetUp();
115     void TearDown();
116     static sptr<InputMethodController> inputMethodController_;
117     static sptr<InputMethodAbility> inputMethodAbility_;
118     static std::shared_ptr<MMI::KeyEvent> keyEvent_;
119     static std::shared_ptr<KeyboardListenerImpl> kbListener_;
120     static std::shared_ptr<InputMethodEngineListenerImpl> imeListener_;
121     static sptr<OnTextChangedListener> textListener_;
122     static sptr<InputMethodSystemAbility> imsa_;
123 };
124 sptr<InputMethodController> InputMethodEditorTest::inputMethodController_;
125 sptr<InputMethodAbility> InputMethodEditorTest::inputMethodAbility_;
126 std::shared_ptr<MMI::KeyEvent> InputMethodEditorTest::keyEvent_;
127 std::shared_ptr<KeyboardListenerImpl> InputMethodEditorTest::kbListener_;
128 std::shared_ptr<InputMethodEngineListenerImpl> InputMethodEditorTest::imeListener_;
129 sptr<OnTextChangedListener> InputMethodEditorTest::textListener_;
130 sptr<InputMethodSystemAbility> InputMethodEditorTest::imsa_;
131 
SetUpTestCase(void)132 void InputMethodEditorTest::SetUpTestCase(void)
133 {
134     IMSA_HILOGI("InputMethodEditorTest::SetUpTestCase");
135     IdentityCheckerMock::ResetParam();
136 
137     imsa_ = new (std::nothrow) InputMethodSystemAbility();
138     if (imsa_ == nullptr) {
139         return;
140     }
141     imsa_->OnStart();
142     imsa_->userId_ = TddUtil::GetCurrentUserId();
143     imsa_->identityChecker_ = std::make_shared<IdentityCheckerMock>();
144 
145     inputMethodAbility_ = InputMethodAbility::GetInstance();
146     inputMethodAbility_->abilityManager_ = imsa_;
147     TddUtil::InitCurrentImePermissionInfo();
148     IdentityCheckerMock::SetBundleName(TddUtil::currentBundleNameMock_);
149     inputMethodAbility_->SetCoreAndAgent();
150     kbListener_ = std::make_shared<KeyboardListenerImpl>();
151     imeListener_ = std::make_shared<InputMethodEngineListenerImpl>();
152     inputMethodAbility_->SetKdListener(kbListener_);
153     inputMethodAbility_->SetImeListener(imeListener_);
154 
155     textListener_ = new TextListener();
156     inputMethodController_ = InputMethodController::GetInstance();
157     inputMethodController_->abilityManager_ = imsa_;
158 
159     keyEvent_ = MMI::KeyEvent::Create();
160     constexpr int32_t keyAction = 2;
161     constexpr int32_t keyCode = 2001;
162     keyEvent_->SetKeyAction(keyAction);
163     keyEvent_->SetKeyCode(keyCode);
164     IdentityCheckerMock::SetFocused(true);
165     int32_t ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, false);
166     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
167     ret = InputMethodEditorTest::inputMethodController_->Close();
168     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
169     IdentityCheckerMock::SetFocused(false);
170     TextListener::ResetParam();
171 }
172 
TearDownTestCase(void)173 void InputMethodEditorTest::TearDownTestCase(void)
174 {
175     IMSA_HILOGI("InputMethodEditorTest::TearDownTestCase");
176     TextListener::ResetParam();
177     IdentityCheckerMock::ResetParam();
178     imsa_->OnStop();
179 }
180 
SetUp(void)181 void InputMethodEditorTest::SetUp(void)
182 {
183     IMSA_HILOGI("InputMethodEditorTest::SetUp");
184 }
185 
TearDown(void)186 void InputMethodEditorTest::TearDown(void)
187 {
188     IMSA_HILOGI("InputMethodEditorTest::TearDown");
189 }
190 
191 /**
192  * @tc.name: testIMCAttachUnfocused
193  * @tc.desc: InputMethodEditorTest Attach.
194  * @tc.type: FUNC
195  */
196 HWTEST_F(InputMethodEditorTest, testIMCAttachUnfocused, TestSize.Level0)
197 {
198     IMSA_HILOGI("InputMethodEditorTest Attach Unfocused Test START");
199     int32_t ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, false);
200     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOCUSED);
201     ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_);
202     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOCUSED);
203     ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, true);
204     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOCUSED);
205 }
206 
207 /**
208  * @tc.name: test Unfocused
209  * @tc.desc: InputMethodEditorTest Unfocused
210  * @tc.type: FUNC
211  */
212 HWTEST_F(InputMethodEditorTest, testUnfocused, TestSize.Level0)
213 {
214     IMSA_HILOGI("InputMethodEditorTest Unfocused Test START");
215     int32_t ret = InputMethodEditorTest::inputMethodController_->ShowTextInput();
216     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
217     ret = InputMethodEditorTest::inputMethodController_->DispatchKeyEvent(
__anon7a70cba20102(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) 218         InputMethodEditorTest::keyEvent_, [](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) {});
219     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
220     ret = InputMethodEditorTest::inputMethodController_->ShowSoftKeyboard();
221     EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
222     ret = InputMethodEditorTest::inputMethodController_->HideSoftKeyboard();
223     EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
224     ret = InputMethodEditorTest::inputMethodController_->StopInputSession();
225     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOCUSED);
226     ret = InputMethodEditorTest::inputMethodController_->ShowCurrentInput();
227     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
228     ret = InputMethodEditorTest::inputMethodController_->HideCurrentInput();
229     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
230     ret = InputMethodEditorTest::inputMethodController_->OnCursorUpdate({});
231     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
232     ret = InputMethodEditorTest::inputMethodController_->OnSelectionChange(Str8ToStr16(""), 0, 0);
233     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
234     ret = InputMethodEditorTest::inputMethodController_->SetCallingWindow(1);
235     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
236     ret = InputMethodEditorTest::inputMethodController_->OnConfigurationChange({});
237     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
238 }
239 
240 /**
241  * @tc.name: testRequestInput001.
242  * @tc.desc: InputMethodEditorTest RequestShowInput/RequestHideInput neither permitted nor focused.
243  * @tc.type: FUNC
244  */
245 HWTEST_F(InputMethodEditorTest, testRequestInput001, TestSize.Level0)
246 {
247     IMSA_HILOGI("InputMethodEditorTest testRequestInput001 Test START");
248     int32_t ret = InputMethodEditorTest::inputMethodController_->RequestShowInput();
249     EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
250     ret = InputMethodEditorTest::inputMethodController_->RequestHideInput();
251     EXPECT_EQ(ret, ErrorCode::ERROR_STATUS_PERMISSION_DENIED);
252 }
253 
254 /**
255  * @tc.name: testRequestInput002.
256  * @tc.desc: InputMethodEditorTest RequestShowInput/RequestHideInput with permitted and not focused.
257  * @tc.type: FUNC
258  */
259 HWTEST_F(InputMethodEditorTest, testRequestInput002, TestSize.Level0)
260 {
261     IMSA_HILOGI("InputMethodEditorTest testRequestInput002 Test START");
262     IdentityCheckerMock::SetPermission(true);
263     int32_t ret = InputMethodEditorTest::inputMethodController_->RequestShowInput();
264     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
265     ret = InputMethodEditorTest::inputMethodController_->RequestHideInput();
266     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
267     IdentityCheckerMock::SetPermission(false);
268 }
269 
270 /**
271  * @tc.name: test AttachFocused
272  * @tc.desc: InputMethodEditorTest Attach Focused
273  * @tc.type: FUNC
274  */
275 HWTEST_F(InputMethodEditorTest, testAttachFocused, TestSize.Level0)
276 {
277     IMSA_HILOGI("InputMethodEditorTest Attach Focused Test START");
278     IdentityCheckerMock::SetFocused(true);
279     InputMethodEditorTest::imeListener_->isInputStart_ = false;
280     InputMethodEditorTest::imeListener_->keyboardState_ = false;
281     int32_t ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, false);
282     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
283 
284     InputMethodEditorTest::imeListener_->isInputStart_ = false;
285     InputMethodEditorTest::imeListener_->keyboardState_ = false;
286     ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_);
287     EXPECT_TRUE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::SHOW));
288     EXPECT_TRUE(imeListener_->keyboardState_);
289     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
290 
291     InputMethodEditorTest::imeListener_->isInputStart_ = false;
292     InputMethodEditorTest::imeListener_->keyboardState_ = false;
293     ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, true);
294     EXPECT_TRUE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::SHOW));
295     EXPECT_TRUE(imeListener_->keyboardState_);
296     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
297     InputMethodEditorTest::inputMethodController_->Close();
298     IdentityCheckerMock::SetFocused(false);
299 }
300 
301 /**
302  * @tc.name: testShowSoftKeyboard
303  * @tc.desc: InputMethodEditorTest ShowSoftKeyboard
304  * @tc.type: FUNC
305  */
306 HWTEST_F(InputMethodEditorTest, testShowSoftKeyboard, TestSize.Level0)
307 {
308     IMSA_HILOGI("InputMethodEditorTest ShowSoftKeyboard Test START");
309     IdentityCheckerMock::SetFocused(true);
310     IdentityCheckerMock::SetPermission(true);
311     InputMethodEditorTest::imeListener_->keyboardState_ = false;
312     TextListener::ResetParam();
313     int32_t ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, false);
314     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
315     ret = InputMethodEditorTest::inputMethodController_->ShowSoftKeyboard();
316     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
317     EXPECT_TRUE(imeListener_->keyboardState_ && TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::SHOW));
318     InputMethodEditorTest::inputMethodController_->Close();
319     IdentityCheckerMock::SetFocused(false);
320     IdentityCheckerMock::SetPermission(false);
321 }
322 
323 /**
324  * @tc.name: testIMCHideTextInput.
325  * @tc.desc: InputMethodEditorTest testHideTextInput.
326  * @tc.type: FUNC
327  */
328 HWTEST_F(InputMethodEditorTest, testIMCHideTextInput, TestSize.Level0)
329 {
330     IMSA_HILOGI("InputMethodEditorTest HideTextInputAndShowTextInput Test START");
331     IdentityCheckerMock::SetFocused(true);
332     IdentityCheckerMock::SetPermission(true);
333     int32_t ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, true);
334     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
335 
336     imeListener_->keyboardState_ = true;
337     InputMethodEditorTest::inputMethodController_->HideTextInput();
338     ret = InputMethodEditorTest::inputMethodController_->DispatchKeyEvent(
__anon7a70cba20202(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) 339         InputMethodEditorTest::keyEvent_, [](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) {});
340     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
341     ret = InputMethodEditorTest::inputMethodController_->ShowSoftKeyboard();
342     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
343     ret = InputMethodEditorTest::inputMethodController_->HideSoftKeyboard();
344     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
345     ret = InputMethodEditorTest::inputMethodController_->ShowCurrentInput();
346     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
347     ret = InputMethodEditorTest::inputMethodController_->HideCurrentInput();
348     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
349     ret = InputMethodEditorTest::inputMethodController_->OnCursorUpdate({});
350     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
351     ret = InputMethodEditorTest::inputMethodController_->OnSelectionChange(Str8ToStr16(""), 0, 0);
352     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
353     ret = InputMethodEditorTest::inputMethodController_->SetCallingWindow(1);
354     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
355     ret = InputMethodEditorTest::inputMethodController_->OnConfigurationChange({});
356     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
357     InputMethodEditorTest::inputMethodController_->Close();
358     IdentityCheckerMock::SetFocused(false);
359     IdentityCheckerMock::SetPermission(false);
360 }
361 
362 /**
363  * @tc.name: testIMCDeactivateClient.
364  * @tc.desc: InputMethodEditorTest testIMCDeactivateClient.
365  * @tc.type: FUNC
366  */
367 HWTEST_F(InputMethodEditorTest, testIMCDeactivateClient, TestSize.Level0)
368 {
369     IMSA_HILOGI("InputMethodEditorTest testIMCDeactivateClient Test START");
370     IdentityCheckerMock::SetFocused(true);
371     IdentityCheckerMock::SetPermission(true);
372     int32_t ret = inputMethodController_->Attach(InputMethodEditorTest::textListener_, true);
373     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
374 
375     inputMethodController_->DeactivateClient();
376     ret = inputMethodController_->DispatchKeyEvent(
__anon7a70cba20302(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) 377         InputMethodEditorTest::keyEvent_, [](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) {});
378     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
379     ret = inputMethodController_->ShowTextInput();
380     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
381     ret = inputMethodController_->HideTextInput();
382     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
383     ret = inputMethodController_->ShowSoftKeyboard();
384     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
385     ret = inputMethodController_->HideSoftKeyboard();
386     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
387     ret = inputMethodController_->ShowCurrentInput();
388     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
389     ret = inputMethodController_->HideCurrentInput();
390     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
391     ret = inputMethodController_->OnCursorUpdate({});
392     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
393     ret = inputMethodController_->OnSelectionChange(Str8ToStr16(""), 0, 0);
394     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
395     ret = inputMethodController_->SetCallingWindow(1);
396     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
397     ret = inputMethodController_->OnConfigurationChange({});
398     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
399 
400     ret = inputMethodController_->Attach(InputMethodEditorTest::textListener_, true);
401     ret = inputMethodController_->ShowTextInput();
402     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
403     InputMethodEditorTest::inputMethodController_->Close();
404     IdentityCheckerMock::SetFocused(false);
405     IdentityCheckerMock::SetPermission(false);
406 }
407 
408 /**
409  * @tc.name: testShowTextInput
410  * @tc.desc: InputMethodEditorTest ShowTextInput
411  * @tc.type: FUNC
412  */
413 HWTEST_F(InputMethodEditorTest, testShowTextInput, TestSize.Level0)
414 {
415     IMSA_HILOGI("InputMethodEditorTest ShowTextInput Test START");
416     IdentityCheckerMock::SetFocused(true);
417     int32_t ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, true);
418     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
419     InputMethodEditorTest::inputMethodController_->HideTextInput();
420 
421     ret = InputMethodEditorTest::inputMethodController_->ShowTextInput();
422     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
423     bool consumeResult = false;
424     ret = InputMethodEditorTest::inputMethodController_->DispatchKeyEvent(InputMethodEditorTest::keyEvent_,
__anon7a70cba20402(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) 425         [&consumeResult](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) { consumeResult = isConsumed; });
426     usleep(1000);
427     ret = ret && kbListener_->keyCode_ == keyEvent_->GetKeyCode()
428           && kbListener_->keyStatus_ == keyEvent_->GetKeyAction();
429     EXPECT_TRUE(consumeResult);
430     InputMethodEditorTest::inputMethodController_->Close();
431     IdentityCheckerMock::SetFocused(false);
432 }
433 
434 /**
435  * @tc.name: testIMCClose.
436  * @tc.desc: InputMethodEditorTest Close.
437  * @tc.type: FUNC
438  */
439 HWTEST_F(InputMethodEditorTest, testIMCClose, TestSize.Level0)
440 {
441     IMSA_HILOGI("IMC Close Test START");
442     IdentityCheckerMock::SetFocused(true);
443     IdentityCheckerMock::SetPermission(true);
444     int32_t ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, true);
445     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
446     InputMethodEditorTest::inputMethodController_->Close();
447 
448     ret = InputMethodEditorTest::inputMethodController_->ShowTextInput();
449     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
450     ret = InputMethodEditorTest::inputMethodController_->DispatchKeyEvent(
__anon7a70cba20502(std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) 451         InputMethodEditorTest::keyEvent_, [](std::shared_ptr<MMI::KeyEvent> &keyEvent, bool isConsumed) {});
452     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
453     ret = InputMethodEditorTest::inputMethodController_->ShowSoftKeyboard();
454     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOUND);
455     ret = InputMethodEditorTest::inputMethodController_->HideSoftKeyboard();
456     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOUND);
457     ret = InputMethodEditorTest::inputMethodController_->ShowCurrentInput();
458     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
459     ret = InputMethodEditorTest::inputMethodController_->HideCurrentInput();
460     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_EDITABLE);
461     ret = InputMethodEditorTest::inputMethodController_->OnCursorUpdate({});
462     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
463     ret = InputMethodEditorTest::inputMethodController_->OnSelectionChange(Str8ToStr16(""), 0, 0);
464     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
465     ret = InputMethodEditorTest::inputMethodController_->SetCallingWindow(1);
466     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
467     ret = InputMethodEditorTest::inputMethodController_->OnConfigurationChange({});
468     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
469     IdentityCheckerMock::SetFocused(false);
470     IdentityCheckerMock::SetPermission(false);
471 }
472 
473 /**
474  * @tc.name: testRequestShowInput.
475  * @tc.desc: InputMethodEditorTest testRequestShowInput with focused.
476  * @tc.type: FUNC
477  */
478 HWTEST_F(InputMethodEditorTest, testRequestShowInput, TestSize.Level0)
479 {
480     IMSA_HILOGI("InputMethodEditorTest testRequestShowInput Test START");
481     IdentityCheckerMock::SetFocused(true);
482     imeListener_->keyboardState_ = false;
483     int32_t ret = InputMethodEditorTest::inputMethodController_->RequestShowInput();
484     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
485     EXPECT_TRUE(imeListener_->keyboardState_);
486     IdentityCheckerMock::SetFocused(false);
487 }
488 
489 /**
490  * @tc.name: testRequestHideInput_001.
491  * @tc.desc: InputMethodEditorTest testRequestHideInput with focused.
492  * @tc.type: FUNC
493  */
494 HWTEST_F(InputMethodEditorTest, testRequestHideInput_001, TestSize.Level0)
495 {
496     IMSA_HILOGI("InputMethodEditorTest testRequestHideInput_001 Test START");
497     IdentityCheckerMock::SetFocused(true);
498     imeListener_->keyboardState_ = true;
499     int32_t ret = InputMethodEditorTest::inputMethodController_->RequestHideInput();
500     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
501     IdentityCheckerMock::SetFocused(false);
502 }
503 
504 /**
505  * @tc.name: testRequestHideInput_002.
506  * @tc.desc: InputMethodEditorTest testRequestHideInput with focused.
507  * @tc.type: FUNC
508  */
509 HWTEST_F(InputMethodEditorTest, testRequestHideInput_002, TestSize.Level0)
510 {
511     IMSA_HILOGI("InputMethodEditorTest testRequestHideInput_002 Test START");
512     IdentityCheckerMock::SetFocused(true);
513     int32_t ret = InputMethodEditorTest::inputMethodController_->Attach(InputMethodEditorTest::textListener_, false);
514     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
515     imeListener_->keyboardState_ = true;
516     ret = InputMethodEditorTest::inputMethodController_->RequestHideInput();
517     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
518     EXPECT_FALSE(imeListener_->keyboardState_);
519     InputMethodEditorTest::inputMethodController_->Close();
520     IdentityCheckerMock::SetFocused(false);
521 }
522 } // namespace MiscServices
523 } // namespace OHOS
524