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 #include "input_method_ability.h"
18 #undef private
19 
20 #include <gtest/gtest.h>
21 
22 #include "ability_manager_client.h"
23 #include "global.h"
24 #include "ime_event_monitor_manager_impl.h"
25 #include "ime_setting_listener_test_impl.h"
26 #include "input_method_ability_interface.h"
27 #include "input_method_controller.h"
28 #include "input_method_engine_listener_impl.h"
29 #include "keyboard_listener_test_impl.h"
30 #include "tdd_util.h"
31 #include "text_listener.h"
32 #include "input_method_types.h"
33 using namespace testing::ext;
34 namespace OHOS {
35 namespace MiscServices {
36 constexpr int32_t RETRY_INTERVAL = 100;
37 constexpr int32_t RETRY_TIME = 30;
38 constexpr int32_t WAIT_APP_START_COMPLETE = 1;
39 constexpr int32_t WAIT_BIND_COMPLETE = 1;
40 constexpr int32_t WAIT_CLICK_COMPLETE = 100;
41 constexpr const char *BUNDLENAME = "com.example.editorbox";
42 class ImeProxyTest : public testing::Test {
43 public:
44     static sptr<InputMethodController> imc_;
SetUpTestCase(void)45     static void SetUpTestCase(void)
46     {
47         TddUtil::StorageSelfTokenID();
48         TddUtil::InitWindow(false);
49         imc_ = InputMethodController::GetInstance();
50         RegisterImeSettingListener();
51         SwitchToTestIme();
52         InputMethodAbilityInterface::GetInstance().SetImeListener(std::make_shared<InputMethodEngineListenerImpl>());
53         InputMethodAbilityInterface::GetInstance().SetKdListener(std::make_shared<KeyboardListenerTestImpl>());
54         // native sa permission
55         TddUtil::GrantNativePermission();
56     }
TearDownTestCase(void)57     static void TearDownTestCase(void)
58     {
59         TddUtil::DestroyWindow();
60         TddUtil::RestoreSelfTokenID();
61     }
SetUp()62     void SetUp()
63     {
64         IMSA_HILOGI("InputMethodAbilityTest::SetUp");
65     }
TearDown()66     void TearDown()
67     {
68         IMSA_HILOGI("InputMethodAbilityTest::TearDown");
69     }
70 
Attach(bool isPc)71     static int32_t Attach(bool isPc)
72     {
73         isPc ? InputMethodEngineListenerImpl::isEnable_ = true : InputMethodEngineListenerImpl::isEnable_ = false;
74         TextConfig config;
75         config.cursorInfo = { .left = 0, .top = 1, .width = 0.5, .height = 1.2 };
76         sptr<OnTextChangedListener> testListener = new TextListener();
77         auto ret = imc_->Attach(testListener, true, config);
78         return ret;
79     }
Close(bool isPc)80     static void Close(bool isPc)
81     {
82         isPc ? InputMethodEngineListenerImpl::isEnable_ = true : InputMethodEngineListenerImpl::isEnable_ = false;
83         imc_->Close();
84     }
85 
StartApp()86     static void StartApp() // bind client default, not show keyboard
87     {
88         static std::string cmd = "aa start ability -a EntryAbility -b com.example.editorbox";
89         std::string result;
90         auto ret = TddUtil::ExecuteCmd(cmd, result);
91         EXPECT_TRUE(ret);
92         BlockRetry(RETRY_INTERVAL, RETRY_TIME,
93             []() { return AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility().GetBundleName() == BUNDLENAME; });
94         IMSA_HILOGI("start app success");
95         sleep(WAIT_APP_START_COMPLETE); // ensure app start complete
96     }
97 
ClickEditor(bool isPc)98     static void ClickEditor(bool isPc)
99     {
100         isPc ? InputMethodEngineListenerImpl::isEnable_ = true : InputMethodEngineListenerImpl::isEnable_ = false;
101         static std::string cmd = "uinput -T -d 200 200 -u 200 200";
102         std::string result;
103         auto ret = TddUtil::ExecuteCmd(cmd, result);
104         EXPECT_TRUE(ret);
105         usleep(WAIT_CLICK_COMPLETE); // ensure click complete
106     }
107 
StopApp()108     static void StopApp()
109     {
110         static std::string cmd = "aa force-stop com.example.editorbox";
111         std::string result;
112         auto ret = TddUtil::ExecuteCmd(cmd, result);
113         EXPECT_TRUE(ret);
114         BlockRetry(RETRY_INTERVAL, RETRY_TIME,
115             []() { return AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility().GetBundleName() != BUNDLENAME; });
116         IMSA_HILOGI("stop app success");
117     }
118 
EnsureBindComplete()119     static void EnsureBindComplete()
120     {
121         sleep(WAIT_BIND_COMPLETE);
122     }
123 
SwitchToTestIme()124     static void SwitchToTestIme()
125     {
126         ImeSettingListenerTestImpl::ResetParam();
127         // ohos.permission.MANAGE_SECURE_SETTINGS ohos.permission.CONNECT_IME_ABILITY
128         TddUtil::GrantNativePermission();
129         std::string beforeValue;
130         TddUtil::GetEnableData(beforeValue);
131         std::string allEnableIme = "{\"enableImeList\" : {\"100\" : [ \"com.example.testIme\"]}}";
132         TddUtil::PushEnableImeValue("settings.inputmethod.enable_ime", allEnableIme);
133         SubProperty subProp = {
134             .name = "com.example.testIme",
135             .id = "InputMethodExtAbility",
136         };
137         auto ret = imc_->SwitchInputMethod(SwitchTrigger::CURRENT_IME, subProp.name, subProp.id);
138         EXPECT_EQ(ret, ErrorCode::NO_ERROR);
139         EXPECT_TRUE(ImeSettingListenerTestImpl::WaitImeChange(subProp));
140         TddUtil::RestoreSelfTokenID();
141         TddUtil::PushEnableImeValue("settings.inputmethod.enable_ime", beforeValue);
142     }
143 
144 private:
RegisterImeSettingListener()145     static void RegisterImeSettingListener()
146     {
147         TddUtil::StorageSelfTokenID();
148         TddUtil::SetTestTokenID(TddUtil::AllocTestTokenID(true, "ImeProxyTest"));
149         auto listener = std::make_shared<ImeSettingListenerTestImpl>();
150         ImeEventMonitorManagerImpl::GetInstance().RegisterImeEventListener(
151             EVENT_IME_HIDE_MASK | EVENT_IME_SHOW_MASK | EVENT_IME_CHANGE_MASK, listener);
152         TddUtil::RestoreSelfTokenID();
153     }
154 };
155 sptr<InputMethodController> ImeProxyTest::imc_;
156 
157 /**
158 * @tc.name: RegisteredProxyNotInEditor_001
159 * @tc.desc: not in editor
160 * @tc.type: FUNC
161 */
162 HWTEST_F(ImeProxyTest, RegisteredProxyNotInEditor_001, TestSize.Level0)
163 {
164     IMSA_HILOGI("ImeProxyTest::RegisteredProxyNotInEditor_001");
165     // RegisteredProxy not in ima bind
166     InputMethodEngineListenerImpl::ResetParam();
167     InputMethodEngineListenerImpl::isEnable_ = true;
168     auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy();
169     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
170     EXPECT_FALSE(InputMethodEngineListenerImpl::WaitInputStart());
171 
172     InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME);
173 }
174 
175 /**
176 * @tc.name: AttachInPcAfterRegisteredProxyNotInEditor_002
177 * @tc.desc: not in editor
178 * @tc.type: FUNC
179 */
180 HWTEST_F(ImeProxyTest, AttachInPcAfterRegisteredProxyNotInEditor_002, TestSize.Level0)
181 {
182     IMSA_HILOGI("ImeProxyTest::AttachInPcAfterRegisteredProxyNotInEditor_002");
183     TddUtil::GetFocused();
184     // RegisteredProxy not in ima bind
185     InputMethodEngineListenerImpl::isEnable_ = true;
186     auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy();
187     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
188 
189     // mock click the edit box in pc, bind proxy
190     ImeSettingListenerTestImpl::ResetParam();
191     InputMethodEngineListenerImpl::ResetParam();
192     ret = Attach(true);
193     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
194     EXPECT_FALSE(ImeSettingListenerTestImpl::WaitPanelShow());
195     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart());
196     Close(false);
197     InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME);
198     TddUtil::GetUnfocused();
199 }
200 
201 /**
202 * @tc.name: AttachInPeAfterRegisteredProxyNotInEditor_003
203 * @tc.desc: not in editor
204 * @tc.type: FUNC
205 */
206 HWTEST_F(ImeProxyTest, AttachInPeAfterRegisteredProxyNotInEditor_003, TestSize.Level0)
207 {
208     IMSA_HILOGI("ImeProxyTest::AttachInPeAfterRegisteredProxyNotInEditor_003");
209     TddUtil::GetFocused();
210     // RegisteredProxy not in ima bind
211     InputMethodEngineListenerImpl::isEnable_ = true;
212     auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy();
213     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
214 
215     // mock click the edit box in pe, bind ima
216     ImeSettingListenerTestImpl::ResetParam();
217     InputMethodEngineListenerImpl::ResetParam();
218     ret = Attach(false);
219     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
220     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelShow());
221     EXPECT_FALSE(InputMethodEngineListenerImpl::WaitInputStart());
222     Close(false);
223     InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME);
224     TddUtil::GetUnfocused();
225 }
226 
227 /**
228 * @tc.name: RegisteredProxyInImaEditor_004
229 * @tc.desc:
230 * @tc.type: FUNC
231 */
232 HWTEST_F(ImeProxyTest, RegisteredProxyInImaEditor_004, TestSize.Level0)
233 {
234     IMSA_HILOGI("ImeProxyTest::RegisteredProxyInImaEditor_004");
235     TddUtil::GetFocused();
236     // mock click the edit box in pe, bind ima
237     ImeSettingListenerTestImpl::ResetParam();
238     auto ret = Attach(false);
239     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
240     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelShow());
241 
242     // RegisteredProxy in ima bind, unbind ima, bind proxy
243     InputMethodEngineListenerImpl::ResetParam();
244     ImeSettingListenerTestImpl::ResetParam();
245     KeyboardListenerTestImpl::ResetParam();
246     InputMethodEngineListenerImpl::isEnable_ = true;
247     ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy();
248     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
249     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelHide());
250     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart());
251     EXPECT_TRUE(KeyboardListenerTestImpl::WaitCursorUpdate());
252     Close(false);
253     InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME);
254     TddUtil::GetUnfocused();
255 }
256 
257 /**
258 * @tc.name: UnRegisteredAndRegisteredProxyInProxyBind_005
259 * @tc.desc:
260 * @tc.type: FUNC
261 */
262 HWTEST_F(ImeProxyTest, UnRegisteredAndRegisteredProxyInProxyBind_005, TestSize.Level0)
263 {
264     IMSA_HILOGI("ImeProxyTest::UnRegisteredAndRegisteredProxyInProxyBind_005");
265     TddUtil::GetFocused();
266     auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy();
267     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
268 
269     // mock click the edit box in pc, bind proxy
270     InputMethodEngineListenerImpl::ResetParam();
271     ret = Attach(true);
272     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
273     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart());
274 
275     // UnRegisteredProxy in proxy bind
276     InputMethodEngineListenerImpl::ResetParam();
277     ret = InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME);
278     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
279     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputFinish());
280     ret = InputMethodAbilityInterface::GetInstance().InsertText("b");
281     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
282 
283     // RegisteredProxy proxy, rebind proxy
284     InputMethodEngineListenerImpl::ResetParam();
285     InputMethodEngineListenerImpl::isEnable_ = true;
286     ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy();
287     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
288     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart());
289     Close(false);
290     InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME);
291     TddUtil::GetUnfocused();
292 }
293 
294 /**
295 * @tc.name: UnRegisteredProxyNotInBind_stop_006
296 * @tc.desc:
297 * @tc.type: FUNC
298 */
299 HWTEST_F(ImeProxyTest, UnRegisteredProxyNotInBind_stop_006, TestSize.Level0)
300 {
301     IMSA_HILOGI("ImeProxyTest::UnRegisteredProxyNotInBind_stop_006");
302     InputMethodEngineListenerImpl::ResetParam();
303     InputMethodEngineListenerImpl::isEnable_ = true;
304     auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy();
305     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
306     ret = InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME);
307     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
308     EXPECT_FALSE(InputMethodEngineListenerImpl::WaitInputFinish());
309 }
310 
311 /**
312 * @tc.name: UnRegisteredProxyInProxyBind_stop_007
313 * @tc.desc:
314 * @tc.type: FUNC
315 */
316 HWTEST_F(ImeProxyTest, UnRegisteredProxyInProxyBind_stop_007, TestSize.Level0)
317 {
318     IMSA_HILOGI("ImeProxyTest::UnRegisteredProxyInProxyBind_stop_007");
319     TddUtil::GetFocused();
320     InputMethodEngineListenerImpl::isEnable_ = true;
321     auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy();
322     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
323 
324     // mock click the edit box in pc, bind proxy
325     InputMethodEngineListenerImpl::ResetParam();
326     ret = Attach(true);
327     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
328     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart());
329 
330     InputMethodEngineListenerImpl::ResetParam();
331     ImeSettingListenerTestImpl::ResetParam();
332     ret = InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME);
333     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
334     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputFinish());
335     EXPECT_FALSE(ImeSettingListenerTestImpl::WaitPanelShow());
336     Close(false);
337     TddUtil::GetUnfocused();
338 }
339 
340 /**
341 * @tc.name: UnRegisteredProxyInImaBind_stop_008
342 * @tc.desc:
343 * @tc.type: FUNC
344 */
345 HWTEST_F(ImeProxyTest, UnRegisteredProxyInImaBind_stop_008, TestSize.Level0)
346 {
347     IMSA_HILOGI("ImeProxyTest::UnRegisteredProxyInImaBind_stop_008");
348     TddUtil::GetFocused();
349     InputMethodEngineListenerImpl::isEnable_ = true;
350     auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy();
351     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
352 
353     // mock click the edit box in pe, bind ima
354     ImeSettingListenerTestImpl::ResetParam();
355     ret = Attach(false);
356     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
357     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelShow());
358 
359     InputMethodEngineListenerImpl::ResetParam();
360     ImeSettingListenerTestImpl::ResetParam();
361     ret = InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME);
362     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
363     EXPECT_FALSE(ImeSettingListenerTestImpl::WaitPanelHide());
364     EXPECT_FALSE(InputMethodEngineListenerImpl::WaitInputFinish());
365     Close(false);
366     TddUtil::GetUnfocused();
367 }
368 
369 /**
370 * @tc.name: UnRegisteredProxyNotInBind_switch_009
371 * @tc.desc:
372 * @tc.type: FUNC
373 */
374 HWTEST_F(ImeProxyTest, UnRegisteredProxyNotInBind_switch_009, TestSize.Level0)
375 {
376     IMSA_HILOGI("ImeProxyTest::UnRegisteredProxyNotInBind_switch_009");
377     InputMethodEngineListenerImpl::isEnable_ = true;
378     auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy();
379     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
380     ret = InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::SWITCH_PROXY_IME_TO_IME);
381     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
382 }
383 
384 /**
385 * @tc.name: UnRegisteredProxyInProxyBind_switch_010
386 * @tc.desc:
387 * @tc.type: FUNC
388 */
389 HWTEST_F(ImeProxyTest, UnRegisteredProxyInProxyBind_switch_010, TestSize.Level0)
390 {
391     IMSA_HILOGI("ImeProxyTest::UnRegisteredProxyInProxyBind_switch_010");
392     TddUtil::GetFocused();
393     InputMethodEngineListenerImpl::isEnable_ = true;
394     auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy();
395     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
396 
397     // mock click the edit box in pc, bind proxy
398     InputMethodEngineListenerImpl::ResetParam();
399     ret = Attach(true);
400     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
401     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart());
402 
403     ImeSettingListenerTestImpl::ResetParam();
404     InputMethodEngineListenerImpl::ResetParam();
405     ret = InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::SWITCH_PROXY_IME_TO_IME);
406     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
407     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputFinish());
408     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelShow());
409     Close(false);
410     TddUtil::GetUnfocused();
411 }
412 
413 /**
414 * @tc.name: UnRegisteredProxyWithErrorType_011
415 * @tc.desc:
416 * @tc.type: FUNC
417 */
418 HWTEST_F(ImeProxyTest, UnRegisteredProxyWithErrorType_011, TestSize.Level0)
419 {
420     IMSA_HILOGI("ImeProxyTest::UnRegisteredProxyWithErrorType_011");
421     InputMethodEngineListenerImpl::isEnable_ = true;
422     auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy();
423     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
424     ret = InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(static_cast<UnRegisteredType>(3));
425     EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
426 }
427 
428 /**
429 * @tc.name: AppUnFocusInProxyBindInPe_012
430 * @tc.desc:
431 * @tc.type: FUNC
432 */
433 HWTEST_F(ImeProxyTest, AppUnFocusInProxyBindInPe_012, TestSize.Level0)
434 {
435     IMSA_HILOGI("ImeProxyTest::AppUnFocusInProxyBindInPe_012");
436     TddUtil::GetFocused();
437     auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy();
438     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
439 
440     // mock click the edit box in pc, bind proxy
441     InputMethodEngineListenerImpl::ResetParam();
442     ret = Attach(true);
443     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
444     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart());
445 
446     // mock app unFocus in proxy bind in pe, unbind proxy
447     Close(false);
448     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputFinish());
449     ret = InputMethodAbilityInterface::GetInstance().InsertText("d");
450     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
451 
452     ret = InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME);
453     TddUtil::GetUnfocused();
454 }
455 
456 /**
457 * @tc.name: AppUnFocusInProxyBindInPc_013
458 * @tc.desc:
459 * @tc.type: FUNC
460 */
461 HWTEST_F(ImeProxyTest, AppUnFocusInProxyBindInPc_013, TestSize.Level0)
462 {
463     IMSA_HILOGI("ImeProxyTest::AppUnFocusInProxyBindInPc_013");
464     TddUtil::GetFocused();
465     auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy();
466     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
467 
468     // open the app, click the edit box in pc, bind proxy
469     InputMethodEngineListenerImpl::ResetParam();
470     ret = Attach(true);
471     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
472     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart());
473 
474     // mock app unFocus in proxy bind in pc, unbind proxy
475     Close(true);
476     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputFinish());
477     ret = InputMethodAbilityInterface::GetInstance().InsertText("d");
478     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
479 
480     InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME);
481     TddUtil::GetUnfocused();
482 }
483 
484 /**
485 * @tc.name: ProxyAndImaSwitchTest_014
486 * @tc.desc:
487 * @tc.type: FUNC
488 */
489 HWTEST_F(ImeProxyTest, ProxyAndImaSwitchTest_014, TestSize.Level0)
490 {
491     IMSA_HILOGI("ImeProxyTest::ProxyAndImaSwitchTest_014");
492     TddUtil::GetFocused();
493     auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy();
494     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
495 
496     // mock click the edit box in pe, bind ima
497     ImeSettingListenerTestImpl::ResetParam();
498     ret = Attach(false);
499     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
500     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelShow());
501 
502     // mock click the edit box in pc, unbind ima, bind proxy
503     ImeSettingListenerTestImpl::ResetParam();
504     InputMethodEngineListenerImpl::ResetParam();
505     ret = Attach(true);
506     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
507     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart());
508     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelHide());
509 
510     // mock click the edit box in pe, unbind proxy, bind ima
511     ImeSettingListenerTestImpl::ResetParam();
512     InputMethodEngineListenerImpl::ResetParam();
513     ret = Attach(false);
514     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
515     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelShow());
516     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputFinish());
517 
518     InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME);
519     Close(false);
520     TddUtil::GetUnfocused();
521 }
522 
523 /**
524 * @tc.name: KeyboardListenerTest_015
525 * @tc.desc:
526 * @tc.type: FUNC
527 */
528 HWTEST_F(ImeProxyTest, KeyboardListenerTest_015, TestSize.Level0)
529 {
530     IMSA_HILOGI("ImeProxyTest::KeyboardListenerTest_015");
531     MessageParcel *data = new MessageParcel();
532     // 1:positionX, 2: positionY, 5: height
533     data->WriteInt32(1);
534     data->WriteInt32(2);
535     data->WriteInt32(5);
536     Message *message = new Message(MessageID::MSG_ID_ON_CURSOR_UPDATE, data);
537     InputMethodAbility::GetInstance()->OnCursorUpdate(message);
538     EXPECT_TRUE(KeyboardListenerTestImpl::WaitCursorUpdate());
539     delete message;
540 
541     MessageParcel *data1 = new MessageParcel();
542     // u"text": text, 1: oldBegin, 2: oldEnd, 4: newBegin, 6: newEnd
543     data1->WriteString16(u"text");
544     data1->WriteInt32(1);
545     data1->WriteInt32(2);
546     data1->WriteInt32(4);
547     data1->WriteInt32(6);
548     Message *message1 = new Message(MessageID::MSG_ID_ON_SELECTION_CHANGE, data1);
549     InputMethodAbility::GetInstance()->OnSelectionChange(message1);
550     EXPECT_TRUE(KeyboardListenerTestImpl::WaitSelectionChange(4));
551     EXPECT_TRUE(KeyboardListenerTestImpl::WaitTextChange("text"));
552     delete message1;
553 }
554 
555 /**
556 * @tc.name: TextEditTest
557 * @tc.desc:
558 * @tc.type: FUNC
559 */
560 HWTEST_F(ImeProxyTest, TextEditTest, TestSize.Level0)
561 {
562     IMSA_HILOGI("ImeProxyTest::TextEditTest");
563     TddUtil::GetFocused();
564     auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy();
565     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
566     InputMethodEngineListenerImpl::ResetParam();
567     ret = Attach(true);
568     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
569     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart());
570 
571     TextListener::ResetParam();
572     ret = InputMethodAbilityInterface::GetInstance().InsertText("b");
573     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
574     EXPECT_TRUE(TextListener::WaitInsertText(u"b"));
575     ret = InputMethodAbilityInterface::GetInstance().MoveCursor(1);
576     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
577     EXPECT_TRUE(TextListener::WaitMoveCursor(1));
578     ret = InputMethodAbilityInterface::GetInstance().DeleteBackward(1);
579     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
580     EXPECT_TRUE(TextListener::WaitDeleteForward(1));
581     ret = InputMethodAbilityInterface::GetInstance().DeleteForward(2);
582     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
583     EXPECT_TRUE(TextListener::WaitDeleteBackward(2));
584 
585     Close(true);
586     InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME);
587     TddUtil::GetUnfocused();
588 }
589 
590 /**
591 * @tc.name: ClientDiedInImaBind_016
592 * @tc.desc:
593 * @tc.type: FUNC
594 */
595 HWTEST_F(ImeProxyTest, ClientDiedInImaBind_016, TestSize.Level0)
596 {
597     IMSA_HILOGI("ImeProxyTest::ClientDiedInImaBind_016");
598     // open the app, click the edit box in pe, bind ima
599     StartApp();
600     ImeSettingListenerTestImpl::ResetParam();
601     ClickEditor(false);
602     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelShow());
603     EnsureBindComplete();
604 
605     ImeSettingListenerTestImpl::ResetParam();
606     StopApp();
607     EXPECT_TRUE(ImeSettingListenerTestImpl::WaitPanelHide());
608 }
609 
610 /**
611 * @tc.name: ClientDiedInProxyBind_017
612 * @tc.desc:
613 * @tc.type: FUNC
614 */
615 HWTEST_F(ImeProxyTest, ClientDiedInProxyBind_017, TestSize.Level0)
616 {
617     IMSA_HILOGI("ImeProxyTest::ClientDiedInProxyBind_017");
618     // RegisteredProxy not in ima bind
619     InputMethodEngineListenerImpl::isEnable_ = true;
620     auto ret = InputMethodAbilityInterface::GetInstance().RegisteredProxy();
621     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
622 
623     StartApp();
624     InputMethodEngineListenerImpl::ResetParam();
625     ClickEditor(true);
626     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputStart());
627     EnsureBindComplete();
628 
629     InputMethodEngineListenerImpl::ResetParam();
630     StopApp();
631     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputFinish());
632     InputMethodAbilityInterface::GetInstance().UnRegisteredProxy(UnRegisteredType::REMOVE_PROXY_IME);
633 }
634 
635 /**
636 * @tc.name: onInputFinishTest_StopInput
637 * @tc.desc: close
638 * @tc.type: FUNC
639 */
640 HWTEST_F(ImeProxyTest, onInputFinishTest_StopInput, TestSize.Level0)
641 {
642     IMSA_HILOGI("ImeProxyTest::onInputFinishTest_StopInput");
643     InputMethodAbility::GetInstance()->StopInput(nullptr);
644     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputFinish());
645 }
646 /**
647 * @tc.name: onInputFinishTest_OnClientInactive
648 * @tc.desc: OnClientInactive
649 * @tc.type: FUNC
650 */
651 HWTEST_F(ImeProxyTest, onInputFinishTest_OnClientInactive, TestSize.Level0)
652 {
653     IMSA_HILOGI("ImeProxyTest::onInputFinishTest_OnClientInactive");
654     InputMethodAbility::GetInstance()->OnClientInactive(nullptr);
655     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitInputFinish());
656 }
657 } // namespace MiscServices
658 } // namespace OHOS
659