• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #define private public
17 #define protected public
18 #include "input_method_ability.h"
19 #include "input_method_controller.h"
20 #include "input_method_system_ability.h"
21 #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: testAttachWithInvalidImmersiveMode
399  * @tc.desc: test Attach with invalid immersive mode.
400  * @tc.type: FUNC
401  */
402 HWTEST_F(InputMethodAttachTest, testAttachWithInvalidImmersiveMode, TestSize.Level0)
403 {
404     IMSA_HILOGI("test testAttachWithInvalidImmersiveMode.");
405     sptr<OnTextChangedListener> textListener = new TextListener();
406     InputAttribute attribute;
407     attribute.immersiveMode = static_cast<int32_t>(ImmersiveMode::END);
408     TextConfig config;
409     config.inputAttribute = attribute;
410     auto ret = inputMethodController_->Attach(textListener, true, config);
411     EXPECT_EQ(ErrorCode::ERROR_PARAMETER_CHECK_FAILED, ret);
412 }
413 
414 /**
415  * @tc.name: testOnSelectionChangeAfterAttach002
416  * @tc.desc: test OnSelectionChange after attach
417  * @tc.type: FUNC
418  */
419 HWTEST_F(InputMethodAttachTest, testOnSelectionChangeAfterAttach002, TestSize.Level0)
420 {
421     IMSA_HILOGI("test testOnSelectionChangeAfterAttach002.");
422     sptr<OnTextChangedListener> textListener = new TextListener();
423     InputAttribute attribute;
424     attribute.inputPattern = 3;
425     attribute.enterKeyType = 2;
426     TextConfig config;
427     config.inputAttribute = attribute;
428     config.range = { .start = 1, .end = 2 };
429     inputMethodController_->Close();
430     auto ret = inputMethodController_->Attach(textListener, false, config);
431     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
432     int start = 0;
433     int end = 1;
434     ret = inputMethodController_->OnSelectionChange(Str8ToStr16("aaa"), start, end);
435     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
436 
437     TextTotalConfig totalConfig;
438     ret = inputMethodAbility_->GetTextConfig(totalConfig);
439     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
440     EXPECT_EQ(totalConfig.textSelection.newBegin, start);
441     EXPECT_EQ(totalConfig.textSelection.newEnd, end);
442     EXPECT_EQ(totalConfig.textSelection.oldBegin, config.range.start);
443     EXPECT_EQ(totalConfig.textSelection.oldEnd, config.range.end);
444 }
445 
446 /**
447  * @tc.name: testOnConfigurationChangeAfterAttach001
448  * @tc.desc: test OnConfigurationChange after attach
449  * @tc.type: FUNC
450  */
451 HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeAfterAttach001, TestSize.Level0)
452 {
453     IMSA_HILOGI("test testOnConfigurationChangeAfterAttach001.");
454     sptr<OnTextChangedListener> textListener = new TextListener();
455     auto ret = inputMethodController_->Attach(textListener);
456     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
457 
458     Configuration config;
459     config.SetTextInputType(TextInputType::DATETIME);
460     config.SetEnterKeyType(EnterKeyType::NEXT);
461     ret = inputMethodController_->OnConfigurationChange(config);
462     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
463 
464     TextTotalConfig totalConfig;
465     ret = inputMethodAbility_->GetTextConfig(totalConfig);
466     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
467     EXPECT_EQ(totalConfig.inputAttribute.inputPattern, static_cast<int32_t>(TextInputType::DATETIME));
468     EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, static_cast<int32_t>(EnterKeyType::NEXT));
469 }
470 
471 /**
472  * @tc.name: testOnConfigurationChangeAfterAttach002
473  * @tc.desc: test OnConfigurationChange after attach
474  * @tc.type: FUNC
475  */
476 HWTEST_F(InputMethodAttachTest, testOnConfigurationChangeAfterAttach002, TestSize.Level0)
477 {
478     IMSA_HILOGI("test testOnConfigurationChangeAfterAttach002.");
479     sptr<OnTextChangedListener> textListener = new TextListener();
480     InputAttribute attribute;
481     attribute.inputPattern = 3;
482     attribute.enterKeyType = 2;
483     attribute.immersiveMode = static_cast<int32_t>(ImmersiveMode::DARK_IMMERSIVE);
484     TextConfig config;
485     config.inputAttribute = attribute;
486     auto ret = inputMethodController_->Attach(textListener, false, config);
487     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
488 
489     Configuration configuration;
490     configuration.SetTextInputType(TextInputType::DATETIME);
491     configuration.SetEnterKeyType(EnterKeyType::NEXT);
492     ret = inputMethodController_->OnConfigurationChange(configuration);
493     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
494 
495     TextTotalConfig totalConfig;
496     ret = inputMethodAbility_->GetTextConfig(totalConfig);
497     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
498     EXPECT_EQ(totalConfig.inputAttribute.inputPattern, static_cast<int32_t>(configuration.GetTextInputType()));
499     EXPECT_EQ(totalConfig.inputAttribute.enterKeyType, static_cast<int32_t>(configuration.GetEnterKeyType()));
500     EXPECT_EQ(totalConfig.inputAttribute.immersiveMode, static_cast<int32_t>(ImmersiveMode::DARK_IMMERSIVE));
501 }
502 
503 /**
504  * @tc.name: testSetCallingWindowAfterAttach002
505  * @tc.desc: test SetCallingWindow after attach
506  * @tc.type: FUNC
507  */
508 HWTEST_F(InputMethodAttachTest, testSetCallingWindowAfterAttach002, TestSize.Level0)
509 {
510     IMSA_HILOGI("test testSetCallingWindowAfterAttach002.");
511     sptr<OnTextChangedListener> textListener = new TextListener();
512     InputAttribute attribute;
513     attribute.inputPattern = 3;
514     attribute.enterKeyType = 2;
515     TextConfig config;
516     config.inputAttribute = attribute;
517     config.windowId = 88;
518     auto ret = inputMethodController_->Attach(textListener, false, config);
519     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
520 
521     uint32_t windowId = 99;
522     ret = inputMethodController_->SetCallingWindow(windowId);
523     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
524 
525     TextTotalConfig totalConfig;
526     ret = inputMethodAbility_->GetTextConfig(totalConfig);
527     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
528     EXPECT_EQ(totalConfig.windowId, windowId);
529 }
530 
531 /**
532  * @tc.name: testOnCursorUpdate001
533  * @tc.desc: test OnCursorUpdate after attach
534  * @tc.type: FUNC
535  */
536 HWTEST_F(InputMethodAttachTest, testOnCursorUpdate001, TestSize.Level0)
537 {
538     IMSA_HILOGI("test testOnCursorUpdate001.");
539     sptr<OnTextChangedListener> textListener = new TextListener();
540     auto ret = inputMethodController_->Attach(textListener);
541     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
542     CursorInfo cursorInfo = { .top = 5, .left = 5, .height = 5, .width = 0.8 };
543     ret = inputMethodController_->OnCursorUpdate(cursorInfo);
544     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
545 
546     InputAttribute attribute;
547     attribute.inputPattern = 3;
548     attribute.enterKeyType = 2;
549     TextConfig config;
550     config.inputAttribute = attribute;
551     CursorInfo cursorInfo2 = { .top = 10, .left = 9, .width = 8, .height = 7 };
552     config.cursorInfo = cursorInfo2;
553     ret = inputMethodController_->Attach(textListener, false, config);
554     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
555 
556     TextTotalConfig totalConfig;
557     ret = inputMethodAbility_->GetTextConfig(totalConfig);
558     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
559     EXPECT_EQ(totalConfig.cursorInfo, cursorInfo2);
560 }
561 
562 /**
563  * @tc.name: testOnSelectionChange
564  * @tc.desc: test OnSelectionChange after attach
565  * @tc.type: FUNC
566  */
567 HWTEST_F(InputMethodAttachTest, testOnSelectionChange, TestSize.Level0)
568 {
569     IMSA_HILOGI("test testOnSelectionChange.");
570     sptr<OnTextChangedListener> textListener = new TextListener();
571     auto ret = inputMethodController_->Attach(textListener);
572     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
573     int start = 0;
574     int end = 1;
575     ret = inputMethodController_->OnSelectionChange(Str8ToStr16("bbb"), start, end);
576     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
577 
578     InputAttribute attribute;
579     attribute.inputPattern = 3;
580     attribute.enterKeyType = 2;
581     TextConfig config;
582     config.inputAttribute = attribute;
583     config.range.start = 10;
584     config.range.end = 20;
585     ret = inputMethodController_->Attach(textListener, false, config);
586     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
587 
588     TextTotalConfig totalConfig;
589     ret = inputMethodAbility_->GetTextConfig(totalConfig);
590     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
591     EXPECT_EQ(totalConfig.textSelection.newBegin, config.range.start);
592     EXPECT_EQ(totalConfig.textSelection.newEnd, config.range.end);
593     EXPECT_EQ(totalConfig.textSelection.oldBegin, start);
594     EXPECT_EQ(totalConfig.textSelection.oldEnd, end);
595 }
596 
597 /**
598  * @tc.name: testOnConfigurationChange002
599  * @tc.desc: test OnConfigurationChange after attach
600  * @tc.type: FUNC
601  */
602 HWTEST_F(InputMethodAttachTest, testOnConfigurationChange002, TestSize.Level0)
603 {
604     IMSA_HILOGI("test testOnConfigurationChange002.");
605     sptr<OnTextChangedListener> textListener = new TextListener();
606     auto ret = inputMethodController_->Attach(textListener);
607     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
608 
609     Configuration configuration;
610     configuration.SetTextInputType(TextInputType::DATETIME);
611     configuration.SetEnterKeyType(EnterKeyType::NEXT);
612     ret = inputMethodController_->OnConfigurationChange(configuration);
613     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
614 
615     InputAttribute attribute;
616     attribute.inputPattern = 3;
617     attribute.enterKeyType = 2;
618     TextConfig config;
619     config.inputAttribute = attribute;
620     config.inputAttribute.enterKeyType = 5;
621     config.inputAttribute.inputPattern = 5;
622     config.inputAttribute.immersiveMode = static_cast<int32_t>(ImmersiveMode::LIGHT_IMMERSIVE);
623     ret = inputMethodController_->Attach(textListener, false, config);
624     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
625 
626     TextTotalConfig totalConfig;
627     ret = inputMethodAbility_->GetTextConfig(totalConfig);
628     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
629 
630     EXPECT_EQ(totalConfig.inputAttribute, config.inputAttribute);
631 }
632 
633 /**
634  * @tc.name: testSetCallingWindow
635  * @tc.desc: test SetCallingWindow after attach
636  * @tc.type: FUNC
637  */
638 HWTEST_F(InputMethodAttachTest, testSetCallingWindow, TestSize.Level0)
639 {
640     IMSA_HILOGI("test testSetCallingWindow.");
641     sptr<OnTextChangedListener> textListener = new TextListener();
642     auto ret = inputMethodController_->Attach(textListener);
643     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
644 
645     uint32_t windowId = 88;
646     ret = inputMethodController_->SetCallingWindow(windowId);
647     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
648 
649     InputAttribute attribute;
650     attribute.inputPattern = 3;
651     attribute.enterKeyType = 2;
652     TextConfig config;
653     config.windowId = 77;
654     ret = inputMethodController_->Attach(textListener, false, config);
655     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
656 
657     TextTotalConfig totalConfig;
658     ret = inputMethodAbility_->GetTextConfig(totalConfig);
659     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
660 
661     EXPECT_EQ(totalConfig.windowId, config.windowId);
662 }
663 /**
664  * @tc.name: testImeCallbackInAttach
665  * @tc.desc: test ime can receive callback in Attach
666  * @tc.type: FUNC
667  */
668 HWTEST_F(InputMethodAttachTest, testImeCallbackInAttach, TestSize.Level0)
669 {
670     IMSA_HILOGI("test testImeCallbackInAttach.");
671     inputMethodAbility_->SetImeListener(std::make_shared<InputMethodEngineListenerImpl>());
672     inputMethodAbility_->SetKdListener(std::make_shared<KeyboardListenerTestImpl>());
673     sptr<OnTextChangedListener> textListener = new TextListener();
674     InputMethodEngineListenerImpl::ResetParam();
675     KeyboardListenerTestImpl::ResetParam();
676     InputAttribute attribute;
677     attribute.inputPattern = 3;
678     attribute.enterKeyType = 2;
679     TextConfig config;
680     config.inputAttribute = attribute;
681     CursorInfo cursorInfo;
682     cursorInfo.left = 0;
683     cursorInfo.top = 1;
684     cursorInfo.width = 0.5;
685     cursorInfo.height = 1.2;
686     config.cursorInfo = cursorInfo;
687     Range selectionRange;
688     selectionRange.start = 5;
689     selectionRange.end = 2;
690     config.range = selectionRange;
691     config.windowId = 10;
692     auto ret = inputMethodController_->Attach(textListener, true, config);
693     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
694 
695     EXPECT_TRUE(KeyboardListenerTestImpl::WaitSelectionChange(selectionRange.start));
696     EXPECT_TRUE(KeyboardListenerTestImpl::WaitCursorUpdate());
697     EXPECT_TRUE(KeyboardListenerTestImpl::WaitEditorAttributeChange(attribute));
698     EXPECT_TRUE(InputMethodEngineListenerImpl::WaitSetCallingWindow(config.windowId));
699 }
700 /**
701  * @tc.name: testAttach007
702  * @tc.desc: test Attach
703  * @tc.type: FUNC
704  */
705 HWTEST_F(InputMethodAttachTest, testAttach007, TestSize.Level0)
706 {
707     IMSA_HILOGI("test testAttach007 after attach.");
708     sptr<OnTextChangedListener> textListener = new TextListener();
709     InputAttribute attribute;
710     attribute.inputPattern = 3;
711     attribute.enterKeyType = 2;
712     TextConfig config;
713     config.inputAttribute = attribute;
714     AttachOptions attachOptions;
715     attachOptions.isShowKeyboard = false;
716     attachOptions.requestKeyboardReason = RequestKeyboardReason::NONE;
717     auto ret = inputMethodController_->Attach(textListener, attachOptions, config);
718     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
719 
720     int32_t keyType = -1;
721     ret = inputMethodAbility_->GetEnterKeyType(keyType);
722     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
723     EXPECT_EQ(keyType, config.inputAttribute.enterKeyType);
724     int32_t inputPattern = -1;
725     ret = inputMethodAbility_->GetInputPattern(inputPattern);
726     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
727     EXPECT_EQ(inputPattern, config.inputAttribute.inputPattern);
728 }
729 } // namespace MiscServices
730 } // namespace OHOS
731