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 
23 #include <gtest/gtest.h>
24 #include <string_ex.h>
25 
26 #include "global.h"
27 #include "identity_checker_mock.h"
28 #include "input_attribute.h"
29 #include "input_method_engine_listener_impl.h"
30 #include "input_method_system_ability_proxy.h"
31 #include "input_method_system_ability_stub.h"
32 #include "keyboard_listener_test_impl.h"
33 #include "tdd_util.h"
34 #include "text_listener.h"
35 
36 using namespace testing::ext;
37 namespace OHOS {
38 namespace MiscServices {
39 class InputMethodAttachTest : public testing::Test {
40 public:
41     static sptr<InputMethodController> inputMethodController_;
42     static sptr<InputMethodAbility> inputMethodAbility_;
43     static sptr<InputMethodSystemAbilityProxy> imsaProxy_;
44     static sptr<InputMethodSystemAbility> imsa_;
SetUpTestCase(void)45     static void SetUpTestCase(void)
46     {
47         IMSA_HILOGI("InputMethodAttachTest::SetUpTestCase");
48         IdentityCheckerMock::ResetParam();
49         imsa_ = new (std::nothrow) InputMethodSystemAbility();
50         if (imsa_ == nullptr) {
51             return;
52         }
53         imsa_->OnStart();
54         imsa_->userId_ = TddUtil::GetCurrentUserId();
55         imsa_->identityChecker_ = std::make_shared<IdentityCheckerMock>();
56         sptr<InputMethodSystemAbilityStub> serviceStub = imsa_;
57         imsaProxy_ = new InputMethodSystemAbilityProxy(serviceStub->AsObject());
58         if (imsaProxy_ == nullptr) {
59             return;
60         }
61         IdentityCheckerMock::SetFocused(true);
62 
63         inputMethodAbility_ = InputMethodAbility::GetInstance();
64         inputMethodAbility_->abilityManager_ = imsaProxy_;
65         TddUtil::InitCurrentImePermissionInfo();
66         IdentityCheckerMock::SetBundleName(TddUtil::currentBundleNameMock_);
67         inputMethodAbility_->SetCoreAndAgent();
68         inputMethodAbility_->SetImeListener(std::make_shared<InputMethodEngineListenerImpl>());
69 
70         inputMethodController_ = InputMethodController::GetInstance();
71         inputMethodController_->abilityManager_ = imsaProxy_;
72     }
TearDownTestCase(void)73     static void TearDownTestCase(void)
74     {
75         IMSA_HILOGI("InputMethodAttachTest::TearDownTestCase");
76         IdentityCheckerMock::ResetParam();
77         imsa_->OnStop();
78     }
SetUp()79     void SetUp()
80     {
81         IMSA_HILOGI("InputMethodAttachTest::SetUp");
82     }
TearDown()83     void TearDown()
84     {
85         IMSA_HILOGI("InputMethodAttachTest::TearDown");
86         inputMethodController_->Close();
87     }
88 };
89 sptr<InputMethodController> InputMethodAttachTest::inputMethodController_;
90 sptr<InputMethodAbility> InputMethodAttachTest::inputMethodAbility_;
91 sptr<InputMethodSystemAbilityProxy> InputMethodAttachTest::imsaProxy_;
92 sptr<InputMethodSystemAbility> InputMethodAttachTest::imsa_;
93 
94 /**
95  * @tc.name: testAttach001
96  * @tc.desc: test Attach
97  * @tc.type: FUNC
98  */
99 HWTEST_F(InputMethodAttachTest, testAttach001, TestSize.Level0)
100 {
101     IMSA_HILOGI("test testAttach001 after attach.");
102     sptr<OnTextChangedListener> textListener = new TextListener();
103     auto ret = inputMethodController_->Attach(textListener);
104     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
105 
106     int32_t keyType = -1;
107     ret = inputMethodAbility_->GetEnterKeyType(keyType);
108     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
109     EXPECT_EQ(keyType, 0);
110     int32_t inputPattern = -1;
111     ret = inputMethodAbility_->GetInputPattern(inputPattern);
112     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
113     auto pattern = InputAttribute::PATTERN_TEXT;
114     EXPECT_EQ(inputPattern, pattern);
115 }
116 
117 /**
118  * @tc.name: testAttach002
119  * @tc.desc: test Attach
120  * @tc.type: FUNC
121  */
122 HWTEST_F(InputMethodAttachTest, testAttach002, TestSize.Level0)
123 {
124     IMSA_HILOGI("test testAttach002 after attach.");
125     sptr<OnTextChangedListener> textListener = new TextListener();
126     auto ret = inputMethodController_->Attach(textListener, false);
127     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
128 
129     int32_t keyType = -1;
130     ret = inputMethodAbility_->GetEnterKeyType(keyType);
131     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
132     EXPECT_EQ(keyType, 0);
133     int32_t inputPattern = -1;
134     ret = inputMethodAbility_->GetInputPattern(inputPattern);
135     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
136     auto pattern = InputAttribute::PATTERN_TEXT;
137     EXPECT_EQ(inputPattern, pattern);
138 }
139 
140 /**
141  * @tc.name: testAttach003
142  * @tc.desc: test Attach
143  * @tc.type: FUNC
144  */
145 HWTEST_F(InputMethodAttachTest, testAttach003, TestSize.Level0)
146 {
147     IMSA_HILOGI("test testAttach003 after attach.");
148     sptr<OnTextChangedListener> textListener = new TextListener();
149     InputAttribute attribute;
150     attribute.inputPattern = 2;
151     attribute.enterKeyType = 1;
152     auto ret = inputMethodController_->Attach(textListener, true, attribute);
153     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
154 
155     int32_t keyType = -1;
156     ret = inputMethodAbility_->GetEnterKeyType(keyType);
157     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
158     EXPECT_EQ(keyType, attribute.enterKeyType);
159     int32_t inputPattern = -1;
160     ret = inputMethodAbility_->GetInputPattern(inputPattern);
161     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
162     EXPECT_EQ(inputPattern, attribute.inputPattern);
163 }
164 
165 /**
166  * @tc.name: testAttach004
167  * @tc.desc: test Attach
168  * @tc.type: FUNC
169  */
170 HWTEST_F(InputMethodAttachTest, testAttach004, TestSize.Level0)
171 {
172     IMSA_HILOGI("test testAttach004 after attach.");
173     sptr<OnTextChangedListener> textListener = new TextListener();
174     InputAttribute attribute;
175     attribute.inputPattern = 3;
176     attribute.enterKeyType = 2;
177     TextConfig config;
178     config.inputAttribute = attribute;
179     auto ret = inputMethodController_->Attach(textListener, false, config);
180     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
181 
182     int32_t keyType = -1;
183     ret = inputMethodAbility_->GetEnterKeyType(keyType);
184     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
185     EXPECT_EQ(keyType, config.inputAttribute.enterKeyType);
186     int32_t inputPattern = -1;
187     ret = inputMethodAbility_->GetInputPattern(inputPattern);
188     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
189     EXPECT_EQ(inputPattern, config.inputAttribute.inputPattern);
190 }
191 
192 /**
193  * @tc.name: testAttach005
194  * @tc.desc: test Attach, test optional param in TextConfig
195  * @tc.type: FUNC
196  */
197 HWTEST_F(InputMethodAttachTest, testAttach005, TestSize.Level0)
198 {
199     IMSA_HILOGI("test testAttach005 after attach.");
200     sptr<OnTextChangedListener> textListener = new TextListener();
201     InputAttribute attribute;
202     attribute.inputPattern = 3;
203     attribute.enterKeyType = 2;
204     TextConfig config;
205     config.inputAttribute = attribute;
206     CursorInfo cursorInfo;
207     cursorInfo.left = 0;
208     cursorInfo.top = 1;
209     cursorInfo.width = 0.5;
210     cursorInfo.height = 1.2;
211     config.cursorInfo = cursorInfo;
212     Range selectionRange;
213     selectionRange.start = 0;
214     selectionRange.end = 2;
215     config.range = selectionRange;
216     config.windowId = 10;
217     inputMethodController_->Close();
218     auto ret = inputMethodController_->Attach(textListener, true, config);
219     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
220 
221     int32_t keyType = -1;
222     ret = inputMethodAbility_->GetEnterKeyType(keyType);
223     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
224     EXPECT_EQ(keyType, config.inputAttribute.enterKeyType);
225     int32_t inputPattern = -1;
226     ret = inputMethodAbility_->GetInputPattern(inputPattern);
227     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
228     EXPECT_EQ(inputPattern, config.inputAttribute.inputPattern);
229 
230     TextTotalConfig textConfig;
231     ret = inputMethodAbility_->GetTextConfig(textConfig);
232     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
233     EXPECT_EQ(textConfig.inputAttribute, config.inputAttribute);
234     EXPECT_EQ(textConfig.windowId, config.windowId);
235     EXPECT_EQ(textConfig.cursorInfo, config.cursorInfo);
236     EXPECT_EQ(textConfig.textSelection.newBegin, config.range.start);
237     EXPECT_EQ(textConfig.textSelection.newEnd, config.range.end);
238     EXPECT_EQ(textConfig.textSelection.oldBegin, INVALID_VALUE);
239     EXPECT_EQ(textConfig.textSelection.oldEnd, INVALID_VALUE);
240 }
241 
242 /**
243  * @tc.name: testAttach006
244  * @tc.desc: test Attach
245  * @tc.type: FUNC
246  */
247 HWTEST_F(InputMethodAttachTest, testAttach006, TestSize.Level0)
248 {
249     IMSA_HILOGI("test testAttach006 attach.");
250     InputMethodAttachTest::inputMethodController_->Close();
251     TextListener::ResetParam();
252     sptr<OnTextChangedListener> textListener = new TextListener();
253     auto ret = InputMethodAttachTest::inputMethodController_->Attach(textListener, false);
254     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
255     EXPECT_TRUE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::NONE));
256 
257     InputMethodAttachTest::inputMethodController_->Close();
258     TextListener::ResetParam();
259     ret = InputMethodAttachTest::inputMethodController_->Attach(textListener, true);
260     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
261     EXPECT_TRUE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::SHOW));
262 }
263 
264 /**
265  * @tc.name: testOnConfigurationChangeWithoutAttach
266  * @tc.desc: test OnConfigurationChange without attach
267  * @tc.type: FUNC
268  */
269 HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeWithoutAttach, TestSize.Level0)
270 {
271     IMSA_HILOGI("InputMethodAttachTest testOnConfigurationChangeWithoutAttach in.");
272     Configuration config;
273     EnterKeyType keyType = EnterKeyType::NEXT;
274     config.SetEnterKeyType(keyType);
275     TextInputType textInputType = TextInputType::DATETIME;
276     config.SetTextInputType(textInputType);
277     auto ret = inputMethodController_->OnConfigurationChange(config);
278     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_BOUND);
279 }
280 
281 /**
282  * @tc.name: testOnConfigurationChange
283  * @tc.desc: test OnConfigurationChange after attach
284  * @tc.type: FUNC
285  */
286 HWTEST_F(InputMethodAttachTest, testOnConfigurationChange, TestSize.Level0)
287 {
288     IMSA_HILOGI("test OnConfigurationChange after attach.");
289     sptr<OnTextChangedListener> textListener = new TextListener();
290     auto ret = inputMethodController_->Attach(textListener);
291     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
292 
293     Configuration config;
294     EnterKeyType keyType = EnterKeyType::NEXT;
295     config.SetEnterKeyType(keyType);
296     TextInputType textInputType = TextInputType::DATETIME;
297     config.SetTextInputType(textInputType);
298     ret = inputMethodController_->OnConfigurationChange(config);
299     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
300     int32_t keyType2;
301     ret = inputMethodAbility_->GetEnterKeyType(keyType2);
302     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
303     EXPECT_EQ(keyType2, (int)keyType);
304     int32_t inputPattern;
305     ret = inputMethodAbility_->GetInputPattern(inputPattern);
306     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
307     EXPECT_EQ(inputPattern, (int)textInputType);
308 }
309 
310 /**
311  * @tc.name: testGetTextConfig
312  * @tc.desc: test GetTextConfig of InputMethodAbility
313  * @tc.type: FUNC
314  */
315 HWTEST_F(InputMethodAttachTest, testGetTextConfig, TestSize.Level0)
316 {
317     IMSA_HILOGI("test OnConfigurationChange001 after attach.");
318     sptr<OnTextChangedListener> textListener = new TextListener();
319     InputAttribute attribute;
320     attribute.inputPattern = 3;
321     attribute.enterKeyType = 2;
322     TextConfig config;
323     config.inputAttribute = attribute;
324     CursorInfo cursorInfo;
325     cursorInfo.left = 0;
326     cursorInfo.top = 1;
327     cursorInfo.width = 0.5;
328     cursorInfo.height = 1.2;
329     config.cursorInfo = cursorInfo;
330     Range selectionRange;
331     selectionRange.start = 0;
332     selectionRange.end = 2;
333     config.range = selectionRange;
334     config.windowId = 10;
335     inputMethodController_->Close();
336     auto ret = inputMethodController_->Attach(textListener, false, config);
337     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
338     TextTotalConfig totalConfig;
339     ret = inputMethodAbility_->GetTextConfig(totalConfig);
340     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
341 
342     EXPECT_EQ(totalConfig.inputAttribute, attribute);
343     EXPECT_EQ(totalConfig.cursorInfo, cursorInfo);
344     EXPECT_EQ(totalConfig.textSelection.newBegin, selectionRange.start);
345     EXPECT_EQ(totalConfig.textSelection.newEnd, selectionRange.end);
346     EXPECT_EQ(totalConfig.textSelection.oldBegin, INVALID_VALUE);
347     EXPECT_EQ(totalConfig.textSelection.oldEnd, INVALID_VALUE);
348     EXPECT_EQ(totalConfig.windowId, config.windowId);
349 }
350 
351 /**
352  * @tc.name: testOnCursorUpdateAfterAttach001
353  * @tc.desc: test OnCursorUpdate after attach
354  * @tc.type: FUNC
355  */
356 HWTEST_F(InputMethodAttachTest, testOnCursorUpdateAfterAttach001, TestSize.Level0)
357 {
358     IMSA_HILOGI("test testOnCursorUpdateAfterAttach001.");
359     sptr<OnTextChangedListener> textListener = new TextListener();
360     auto ret = inputMethodController_->Attach(textListener);
361     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
362     CursorInfo cursorInfo = { .top = 5, .left = 5, .height = 5, .width = 0.8 };
363     ret = inputMethodController_->OnCursorUpdate(cursorInfo);
364     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
365     TextTotalConfig totalConfig;
366     ret = inputMethodAbility_->GetTextConfig(totalConfig);
367     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
368     EXPECT_EQ(totalConfig.cursorInfo, cursorInfo);
369 }
370 
371 /**
372  * @tc.name: testOnCursorUpdateAfterAttach002
373  * @tc.desc: test OnCursorUpdate after attach
374  * @tc.type: FUNC
375  */
376 HWTEST_F(InputMethodAttachTest, testOnCursorUpdateAfterAttach002, TestSize.Level0)
377 {
378     IMSA_HILOGI("test testOnCursorUpdateAfterAttach002.");
379     sptr<OnTextChangedListener> textListener = new TextListener();
380     InputAttribute attribute;
381     attribute.inputPattern = 3;
382     attribute.enterKeyType = 2;
383     TextConfig config;
384     config.inputAttribute = attribute;
385     config.cursorInfo = { .top = 1, .left = 1, .height = 1, .width = 0.4 };
386     auto ret = inputMethodController_->Attach(textListener, true, config);
387     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
388     CursorInfo cursorInfo = { .top = 5, .left = 5, .height = 5, .width = 0.8 };
389     ret = inputMethodController_->OnCursorUpdate(cursorInfo);
390     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
391     TextTotalConfig totalConfig;
392     ret = inputMethodAbility_->GetTextConfig(totalConfig);
393     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
394     EXPECT_EQ(totalConfig.cursorInfo, cursorInfo);
395 }
396 
397 /**
398  * @tc.name: testOnSelectionChangeAfterAttach002
399  * @tc.desc: test OnSelectionChange after attach
400  * @tc.type: FUNC
401  */
402 HWTEST_F(InputMethodAttachTest, testOnSelectionChangeAfterAttach002, TestSize.Level0)
403 {
404     IMSA_HILOGI("test testOnSelectionChangeAfterAttach002.");
405     sptr<OnTextChangedListener> textListener = new TextListener();
406     InputAttribute attribute;
407     attribute.inputPattern = 3;
408     attribute.enterKeyType = 2;
409     TextConfig config;
410     config.inputAttribute = attribute;
411     config.range = { .start = 1, .end = 2 };
412     inputMethodController_->Close();
413     auto ret = inputMethodController_->Attach(textListener, false, config);
414     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
415     int start = 0;
416     int end = 1;
417     ret = inputMethodController_->OnSelectionChange(Str8ToStr16("aaa"), start, end);
418     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
419 
420     TextTotalConfig totalConfig;
421     ret = inputMethodAbility_->GetTextConfig(totalConfig);
422     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
423     EXPECT_EQ(totalConfig.textSelection.newBegin, start);
424     EXPECT_EQ(totalConfig.textSelection.newEnd, end);
425     EXPECT_EQ(totalConfig.textSelection.oldBegin, config.range.start);
426     EXPECT_EQ(totalConfig.textSelection.oldEnd, config.range.end);
427 }
428 
429 /**
430  * @tc.name: testOnConfigurationChangeAfterAttach001
431  * @tc.desc: test OnConfigurationChange after attach
432  * @tc.type: FUNC
433  */
434 HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeAfterAttach001, TestSize.Level0)
435 {
436     IMSA_HILOGI("test testOnConfigurationChangeAfterAttach001.");
437     sptr<OnTextChangedListener> textListener = new TextListener();
438     auto ret = inputMethodController_->Attach(textListener);
439     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
440 
441     Configuration config;
442     config.SetTextInputType(TextInputType::DATETIME);
443     config.SetEnterKeyType(EnterKeyType::NEXT);
444     ret = inputMethodController_->OnConfigurationChange(config);
445     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
446 
447     TextTotalConfig totalConfig;
448     ret = inputMethodAbility_->GetTextConfig(totalConfig);
449     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
450     EXPECT_EQ(totalConfig.inputAttribute.inputPattern, static_cast<int32_t>(TextInputType::DATETIME));
451     EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, static_cast<int32_t>(EnterKeyType::NEXT));
452 }
453 
454 /**
455  * @tc.name: testOnConfigurationChangeAfterAttach002
456  * @tc.desc: test OnConfigurationChange after attach
457  * @tc.type: FUNC
458  */
459 HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeAfterAttach002, TestSize.Level0)
460 {
461     IMSA_HILOGI("test testOnConfigurationChangeAfterAttach002.");
462     sptr<OnTextChangedListener> textListener = new TextListener();
463     InputAttribute attribute;
464     attribute.inputPattern = 3;
465     attribute.enterKeyType = 2;
466     TextConfig config;
467     config.inputAttribute = attribute;
468     auto ret = inputMethodController_->Attach(textListener, false, config);
469     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
470 
471     Configuration configuration;
472     configuration.SetTextInputType(TextInputType::DATETIME);
473     configuration.SetEnterKeyType(EnterKeyType::NEXT);
474     ret = inputMethodController_->OnConfigurationChange(configuration);
475     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
476 
477     TextTotalConfig totalConfig;
478     ret = inputMethodAbility_->GetTextConfig(totalConfig);
479     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
480     EXPECT_EQ(totalConfig.inputAttribute.inputPattern, static_cast<int32_t>(configuration.GetTextInputType()));
481     EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, static_cast<int32_t>(configuration.GetEnterKeyType()));
482 }
483 
484 /**
485  * @tc.name: testSetCallingWindowAfterAttach002
486  * @tc.desc: test SetCallingWindow after attach
487  * @tc.type: FUNC
488  */
489 HWTEST_F(InputMethodAttachTest, testSetCallingWindowAfterAttach002, TestSize.Level0)
490 {
491     IMSA_HILOGI("test testSetCallingWindowAfterAttach002.");
492     sptr<OnTextChangedListener> textListener = new TextListener();
493     InputAttribute attribute;
494     attribute.inputPattern = 3;
495     attribute.enterKeyType = 2;
496     TextConfig config;
497     config.inputAttribute = attribute;
498     config.windowId = 88;
499     auto ret = inputMethodController_->Attach(textListener, false, config);
500     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
501 
502     uint32_t windowId = 99;
503     ret = inputMethodController_->SetCallingWindow(windowId);
504     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
505 
506     TextTotalConfig totalConfig;
507     ret = inputMethodAbility_->GetTextConfig(totalConfig);
508     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
509     EXPECT_EQ(totalConfig.windowId, windowId);
510 }
511 
512 /**
513  * @tc.name: testOnCursorUpdate001
514  * @tc.desc: test OnCursorUpdate after attach
515  * @tc.type: FUNC
516  */
517 HWTEST_F(InputMethodAttachTest, testOnCursorUpdate001, TestSize.Level0)
518 {
519     IMSA_HILOGI("test testOnCursorUpdate001.");
520     sptr<OnTextChangedListener> textListener = new TextListener();
521     auto ret = inputMethodController_->Attach(textListener);
522     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
523     CursorInfo cursorInfo = { .top = 5, .left = 5, .height = 5, .width = 0.8 };
524     ret = inputMethodController_->OnCursorUpdate(cursorInfo);
525     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
526 
527     InputAttribute attribute;
528     attribute.inputPattern = 3;
529     attribute.enterKeyType = 2;
530     TextConfig config;
531     config.inputAttribute = attribute;
532     CursorInfo cursorInfo2 = { .top = 10, .left = 9, .width = 8, .height = 7 };
533     config.cursorInfo = cursorInfo2;
534     ret = inputMethodController_->Attach(textListener, false, config);
535     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
536 
537     TextTotalConfig totalConfig;
538     ret = inputMethodAbility_->GetTextConfig(totalConfig);
539     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
540     EXPECT_EQ(totalConfig.cursorInfo, cursorInfo2);
541 }
542 
543 /**
544  * @tc.name: testOnSelectionChange
545  * @tc.desc: test OnSelectionChange after attach
546  * @tc.type: FUNC
547  */
548 HWTEST_F(InputMethodAttachTest, testOnSelectionChange, TestSize.Level0)
549 {
550     IMSA_HILOGI("test testOnSelectionChange.");
551     sptr<OnTextChangedListener> textListener = new TextListener();
552     auto ret = inputMethodController_->Attach(textListener);
553     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
554     int start = 0;
555     int end = 1;
556     ret = inputMethodController_->OnSelectionChange(Str8ToStr16("bbb"), start, end);
557     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
558 
559     InputAttribute attribute;
560     attribute.inputPattern = 3;
561     attribute.enterKeyType = 2;
562     TextConfig config;
563     config.inputAttribute = attribute;
564     config.range.start = 10;
565     config.range.end = 20;
566     ret = inputMethodController_->Attach(textListener, false, config);
567     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
568 
569     TextTotalConfig totalConfig;
570     ret = inputMethodAbility_->GetTextConfig(totalConfig);
571     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
572     EXPECT_EQ(totalConfig.textSelection.newBegin, config.range.start);
573     EXPECT_EQ(totalConfig.textSelection.newEnd, config.range.end);
574     EXPECT_EQ(totalConfig.textSelection.oldBegin, start);
575     EXPECT_EQ(totalConfig.textSelection.oldEnd, end);
576 }
577 
578 /**
579  * @tc.name: testOnConfigurationChange002
580  * @tc.desc: test OnConfigurationChange after attach
581  * @tc.type: FUNC
582  */
583 HWTEST_F(InputMethodAttachTest, testOnConfigurationChange002, TestSize.Level0)
584 {
585     IMSA_HILOGI("test testOnConfigurationChange002.");
586     sptr<OnTextChangedListener> textListener = new TextListener();
587     auto ret = inputMethodController_->Attach(textListener);
588     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
589 
590     Configuration configuration;
591     configuration.SetTextInputType(TextInputType::DATETIME);
592     configuration.SetEnterKeyType(EnterKeyType::NEXT);
593     ret = inputMethodController_->OnConfigurationChange(configuration);
594     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
595 
596     InputAttribute attribute;
597     attribute.inputPattern = 3;
598     attribute.enterKeyType = 2;
599     TextConfig config;
600     config.inputAttribute = attribute;
601     config.inputAttribute.enterKeyType = 5;
602     config.inputAttribute.inputPattern = 5;
603     ret = inputMethodController_->Attach(textListener, false, config);
604     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
605 
606     TextTotalConfig totalConfig;
607     ret = inputMethodAbility_->GetTextConfig(totalConfig);
608     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
609 
610     EXPECT_EQ(totalConfig.inputAttribute, config.inputAttribute);
611 }
612 
613 /**
614  * @tc.name: testSetCallingWindow
615  * @tc.desc: test SetCallingWindow after attach
616  * @tc.type: FUNC
617  */
618 HWTEST_F(InputMethodAttachTest, testSetCallingWindow, TestSize.Level0)
619 {
620     IMSA_HILOGI("test testSetCallingWindow.");
621     sptr<OnTextChangedListener> textListener = new TextListener();
622     auto ret = inputMethodController_->Attach(textListener);
623     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
624 
625     uint32_t windowId = 88;
626     ret = inputMethodController_->SetCallingWindow(windowId);
627     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
628 
629     InputAttribute attribute;
630     attribute.inputPattern = 3;
631     attribute.enterKeyType = 2;
632     TextConfig config;
633     config.windowId = 77;
634     ret = inputMethodController_->Attach(textListener, false, config);
635     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
636 
637     TextTotalConfig totalConfig;
638     ret = inputMethodAbility_->GetTextConfig(totalConfig);
639     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
640 
641     EXPECT_EQ(totalConfig.windowId, config.windowId);
642 }
643 /**
644  * @tc.name: testImeCallbackInAttach
645  * @tc.desc: test ime can receive callback in Attach
646  * @tc.type: FUNC
647  */
648 HWTEST_F(InputMethodAttachTest, testImeCallbackInAttach, TestSize.Level0)
649 {
650     IMSA_HILOGI("test testImeCallbackInAttach.");
651     inputMethodAbility_->SetImeListener(std::make_shared<InputMethodEngineListenerImpl>());
652     inputMethodAbility_->SetKdListener(std::make_shared<KeyboardListenerTestImpl>());
653     sptr<OnTextChangedListener> textListener = new TextListener();
654     InputMethodEngineListenerImpl::ResetParam();
655     KeyboardListenerTestImpl::ResetParam();
656     InputAttribute attribute;
657     attribute.inputPattern = 3;
658     attribute.enterKeyType = 2;
659     TextConfig config;
660     config.inputAttribute = attribute;
661     CursorInfo cursorInfo;
662     cursorInfo.left = 0;
663     cursorInfo.top = 1;
664     cursorInfo.width = 0.5;
665     cursorInfo.height = 1.2;
666     config.cursorInfo = cursorInfo;
667     Range selectionRange;
668     selectionRange.start = 5;
669     selectionRange.end = 2;
670     config.range = selectionRange;
671     config.windowId = 10;
672     auto ret = inputMethodController_->Attach(textListener, true, config);
673     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
674 
675     EXPECT_TRUE(KeyboardListenerTestImpl::WaitSelectionChange(selectionRange.start));
676     EXPECT_TRUE(KeyboardListenerTestImpl::WaitCursorUpdate());
677     EXPECT_TRUE(KeyboardListenerTestImpl::WaitEditorAttributeChange(attribute));
678     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitSetCallingWindow(config.windowId));
679 }
680 } // namespace MiscServices
681 } // namespace OHOS
682