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