• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-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 #define private public
16 #define protected public
17 #include "input_method_ability.h"
18 
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 #include <unistd.h>
26 
27 #include <cstdint>
28 #include <functional>
29 #include <string>
30 #include <thread>
31 #include <vector>
32 
33 #include "global.h"
34 #include "i_input_data_channel.h"
35 #include "identity_checker_mock.h"
36 #include "input_attribute.h"
37 #include "input_control_channel_stub.h"
38 #include "input_data_channel_proxy.h"
39 #include "input_data_channel_stub.h"
40 #include "input_method_agent_stub.h"
41 #include "input_method_core_proxy.h"
42 #include "input_method_core_stub.h"
43 #include "input_method_panel.h"
44 #include "input_method_system_ability_proxy.h"
45 #include "message_handler.h"
46 #include "scope_utils.h"
47 #include "tdd_util.h"
48 #include "text_listener.h"
49 
50 using namespace testing::ext;
51 namespace OHOS {
52 namespace MiscServices {
53 constexpr uint32_t DEALY_TIME = 1;
54 class InputMethodAbilityTest : public testing::Test {
55 public:
56     static std::mutex imeListenerCallbackLock_;
57     static std::condition_variable imeListenerCv_;
58     static bool showKeyboard_;
59     static constexpr int CURSOR_DIRECTION_BASE_VALUE = 2011;
60     static sptr<InputMethodController> imc_;
61     static sptr<OnTextChangedListener> textListener_;
62     static sptr<InputMethodAbility> inputMethodAbility_;
63     static uint32_t windowId_;
64     static int32_t security_;
65     static uint64_t currentImeTokenId_;
66     static int32_t currentImeUid_;
67     static sptr<InputMethodSystemAbility> imsa_;
68     static sptr<InputMethodSystemAbilityProxy> imsaProxy_;
69 
70     class InputMethodEngineListenerImpl : public InputMethodEngineListener {
71     public:
72         InputMethodEngineListenerImpl() = default;
73         ~InputMethodEngineListenerImpl() = default;
74 
OnKeyboardStatus(bool isShow)75         void OnKeyboardStatus(bool isShow)
76         {
77             showKeyboard_ = isShow;
78             InputMethodAbilityTest::imeListenerCv_.notify_one();
79             IMSA_HILOGI("InputMethodEngineListenerImpl OnKeyboardStatus");
80         }
81 
OnInputStart()82         void OnInputStart()
83         {
84             IMSA_HILOGI("InputMethodEngineListenerImpl OnInputStart");
85         }
86 
OnInputStop()87         int32_t OnInputStop()
88         {
89             IMSA_HILOGI("InputMethodEngineListenerImpl OnInputStop");
90             return ErrorCode::NO_ERROR;
91         }
92 
OnSetCallingWindow(uint32_t windowId)93         void OnSetCallingWindow(uint32_t windowId)
94         {
95             windowId_ = windowId;
96             IMSA_HILOGI("InputMethodEngineListenerImpl OnSetCallingWindow");
97         }
98 
OnSetSubtype(const SubProperty & property)99         void OnSetSubtype(const SubProperty &property)
100         {
101             IMSA_HILOGI("InputMethodEngineListenerImpl OnSetSubtype");
102         }
103 
OnSecurityChange(int32_t security)104         void OnSecurityChange(int32_t security)
105         {
106             security_ = security;
107             IMSA_HILOGI("InputMethodEngineListenerImpl OnSecurityChange");
108         }
109 
ReceivePrivateCommand(const std::unordered_map<std::string,PrivateDataValue> & privateCommand)110         void ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand)
111         {
112             IMSA_HILOGI("InputMethodEngineListenerImpl ReceivePrivateCommand");
113         }
114     };
115 
SetUpTestCase(void)116     static void SetUpTestCase(void)
117     {
118         IdentityCheckerMock::ResetParam();
119         // Set the tokenID to the tokenID of the current ime
120         TddUtil::StorageSelfTokenID();
121         imsa_ = new (std::nothrow) InputMethodSystemAbility();
122         if (imsa_ == nullptr) {
123             return;
124         }
125         imsa_->OnStart();
126         imsa_->userId_ = TddUtil::GetCurrentUserId();
127         imsa_->identityChecker_ = std::make_shared<IdentityCheckerMock>();
128         sptr<InputMethodSystemAbilityStub> serviceStub = imsa_;
129         imsaProxy_ = new (std::nothrow) InputMethodSystemAbilityProxy(serviceStub->AsObject());
130         if (imsaProxy_ == nullptr) {
131             return;
132         }
133         IdentityCheckerMock::SetFocused(true);
134 
135         std::shared_ptr<Property> property = InputMethodController::GetInstance()->GetCurrentInputMethod();
136         auto currentIme = property != nullptr ? property->name : "default.inputmethod.unittest";
137         currentImeTokenId_ = TddUtil::GetTestTokenID(currentIme);
138         currentImeUid_ = TddUtil::GetUid(currentIme);
139 
140         inputMethodAbility_ = InputMethodAbility::GetInstance();
141         inputMethodAbility_->abilityManager_ = imsaProxy_;
142         TddUtil::InitCurrentImePermissionInfo();
143         IdentityCheckerMock::SetBundleName(TddUtil::currentBundleNameMock_);
144         inputMethodAbility_->SetCoreAndAgent();
145 
146         TextListener::ResetParam();
147         imc_ = InputMethodController::GetInstance();
148         imc_->abilityManager_ = imsaProxy_;
149         textListener_ = new TextListener();
150     }
TearDownTestCase(void)151     static void TearDownTestCase(void)
152     {
153         IMSA_HILOGI("InputMethodAbilityTest::TearDownTestCase");
154         imc_->Close();
155         TextListener::ResetParam();
156         TddUtil::RestoreSelfTokenID();
157         IdentityCheckerMock::ResetParam();
158         imsa_->OnStop();
159     }
GetIMCAttachIMA()160     static void GetIMCAttachIMA()
161     {
162         imc_->SetTextListener(textListener_);
163         imc_->clientInfo_.state = ClientState::ACTIVE;
164         imc_->isBound_.store(true);
165         imc_->isEditable_.store(true);
166         auto agent = inputMethodAbility_->agentStub_->AsObject();
167         imc_->SetAgent(agent);
168 
169         sptr<IInputDataChannel> channel = iface_cast<IInputDataChannel>(imc_->clientInfo_.channel);
170         inputMethodAbility_->SetInputDataChannel(channel->AsObject());
171         IMSA_HILOGI("end");
172     }
GetIMCDetachIMA()173     static void GetIMCDetachIMA()
174     {
175         imc_->OnInputStop();
176         inputMethodAbility_->ClearDataChannel(inputMethodAbility_->dataChannelObject_);
177         IMSA_HILOGI("end");
178     }
SetUp()179     void SetUp()
180     {
181         IMSA_HILOGI("InputMethodAbilityTest::SetUp");
182         TextListener::ResetParam();
183     }
TearDown()184     void TearDown()
185     {
186         IMSA_HILOGI("InputMethodAbilityTest::TearDown");
187     }
CheckPanelStatusInfo(const std::shared_ptr<InputMethodPanel> & panel,const PanelStatusInfo & info)188     void CheckPanelStatusInfo(const std::shared_ptr<InputMethodPanel> &panel, const PanelStatusInfo &info)
189     {
190         TextListener::ResetParam();
191         info.visible ? CheckPanelInfoInShow(panel, info) : CheckPanelInfoInHide(panel, info);
192     }
CheckPanelInfoInShow(const std::shared_ptr<InputMethodPanel> & panel,const PanelStatusInfo & info)193     void CheckPanelInfoInShow(const std::shared_ptr<InputMethodPanel> &panel, const PanelStatusInfo &info)
194     {
195         auto ret = inputMethodAbility_->ShowPanel(panel);
196         EXPECT_EQ(ret, ErrorCode::NO_ERROR);
197         if (info.panelInfo.panelFlag != FLG_CANDIDATE_COLUMN) {
198             if (info.panelInfo.panelType == SOFT_KEYBOARD) {
199                 EXPECT_TRUE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::SHOW));
200             } else {
201                 EXPECT_FALSE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::SHOW));
202             }
203             EXPECT_TRUE(TextListener::WaitNotifyPanelStatusInfoCallback(
204                 { { info.panelInfo.panelType, info.panelInfo.panelFlag }, info.visible, info.trigger }));
205             return;
206         }
207         EXPECT_FALSE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::SHOW));
208         EXPECT_FALSE(TextListener::WaitNotifyPanelStatusInfoCallback(
209             { { info.panelInfo.panelType, info.panelInfo.panelFlag }, info.visible, info.trigger }));
210     }
CheckPanelInfoInHide(const std::shared_ptr<InputMethodPanel> & panel,const PanelStatusInfo & info)211     void CheckPanelInfoInHide(const std::shared_ptr<InputMethodPanel> &panel, const PanelStatusInfo &info)
212     {
213         AccessScope scope(currentImeTokenId_, currentImeUid_);
214         auto ret = inputMethodAbility_->HidePanel(panel);
215         EXPECT_EQ(ret, ErrorCode::NO_ERROR);
216         if (info.panelInfo.panelFlag != FLG_CANDIDATE_COLUMN) {
217             if (info.panelInfo.panelType == SOFT_KEYBOARD) {
218                 EXPECT_TRUE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::HIDE));
219             } else {
220                 EXPECT_FALSE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::HIDE));
221             };
222             EXPECT_TRUE(TextListener::WaitNotifyPanelStatusInfoCallback(
223                 { { info.panelInfo.panelType, info.panelInfo.panelFlag }, info.visible, info.trigger }));
224             return;
225         }
226         EXPECT_FALSE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::HIDE));
227         EXPECT_FALSE(TextListener::WaitNotifyPanelStatusInfoCallback(
228             { { info.panelInfo.panelType, info.panelInfo.panelFlag }, info.visible, info.trigger }));
229     }
230 };
231 
232 std::mutex InputMethodAbilityTest::imeListenerCallbackLock_;
233 std::condition_variable InputMethodAbilityTest::imeListenerCv_;
234 bool InputMethodAbilityTest::showKeyboard_ = true;
235 sptr<InputMethodController> InputMethodAbilityTest::imc_;
236 sptr<OnTextChangedListener> InputMethodAbilityTest::textListener_;
237 sptr<InputMethodAbility> InputMethodAbilityTest::inputMethodAbility_;
238 uint32_t InputMethodAbilityTest::windowId_ = 0;
239 int32_t InputMethodAbilityTest::security_ = -1;
240 uint64_t InputMethodAbilityTest::currentImeTokenId_ = 0;
241 int32_t InputMethodAbilityTest::currentImeUid_ = 0;
242 sptr<InputMethodSystemAbility> InputMethodAbilityTest::imsa_;
243 sptr<InputMethodSystemAbilityProxy> InputMethodAbilityTest::imsaProxy_;
244 
245 /**
246 * @tc.name: testSerializedInputAttribute
247 * @tc.desc: Checkout the serialization of InputAttribute.
248 * @tc.type: FUNC
249 */
250 HWTEST_F(InputMethodAbilityTest, testSerializedInputAttribute, TestSize.Level0)
251 {
252     InputAttribute inAttribute;
253     inAttribute.inputPattern = InputAttribute::PATTERN_PASSWORD;
254     MessageParcel data;
255     EXPECT_TRUE(InputAttribute::Marshalling(inAttribute, data));
256     InputAttribute outAttribute;
257     EXPECT_TRUE(InputAttribute::Unmarshalling(outAttribute, data));
258     EXPECT_TRUE(outAttribute.GetSecurityFlag());
259 }
260 
261 /**
262 * @tc.name: testShowKeyboardInputMethodCoreProxy
263 * @tc.desc: Test InputMethodCoreProxy ShowKeyboard
264 * @tc.type: FUNC
265 * @tc.require: issueI5NXHK
266 */
267 HWTEST_F(InputMethodAbilityTest, testShowKeyboardInputMethodCoreProxy, TestSize.Level0)
268 {
269     IMSA_HILOGI("testShowKeyboardInputMethodCoreProxy start.");
270     sptr<InputMethodCoreStub> coreStub = new InputMethodCoreStub();
271     sptr<IInputMethodCore> core = coreStub;
272     auto msgHandler = new (std::nothrow) MessageHandler();
273     coreStub->SetMessageHandler(msgHandler);
274     sptr<InputDataChannelStub> channelStub = new InputDataChannelStub();
275 
276     MessageParcel data;
277     data.WriteRemoteObject(core->AsObject());
278     data.WriteRemoteObject(channelStub->AsObject());
279     sptr<IRemoteObject> coreObject = data.ReadRemoteObject();
280     sptr<IRemoteObject> channelObject = data.ReadRemoteObject();
281 
282     sptr<InputMethodCoreProxy> coreProxy = new InputMethodCoreProxy(coreObject);
283     sptr<InputDataChannelProxy> channelProxy = new InputDataChannelProxy(channelObject);
284     auto ret = coreProxy->ShowKeyboard();
285     EXPECT_EQ(ret, ErrorCode::ERROR_IME);
286     delete msgHandler;
287 }
288 
289 /**
290 * @tc.name: testShowKeyboardWithoutImeListener
291 * @tc.desc: InputMethodAbility ShowKeyboard without imeListener
292 * @tc.type: FUNC
293 * @tc.require:
294 */
295 HWTEST_F(InputMethodAbilityTest, testShowKeyboardWithoutImeListener, TestSize.Level0)
296 {
297     IMSA_HILOGI("InputMethodAbilityTest testShowKeyboardWithoutImeListener start.");
298     auto ret = inputMethodAbility_->ShowKeyboard();
299     EXPECT_EQ(ret, ErrorCode::ERROR_IME);
300 }
301 
302 /**
303 * @tc.name: testHideKeyboardWithoutImeListener
304 * @tc.desc: InputMethodAbility HideKeyboard without imeListener
305 * @tc.type: FUNC
306 * @tc.require:
307 */
308 HWTEST_F(InputMethodAbilityTest, testHideKeyboardWithoutImeListener, TestSize.Level0)
309 {
310     IMSA_HILOGI("InputMethodAbilityTest testHideKeyboardWithoutImeListener start.");
311     auto ret = inputMethodAbility_->HideKeyboard();
312     EXPECT_EQ(ret, ErrorCode::ERROR_IME);
313 }
314 
315 /**
316 * @tc.name: testStartInputWithoutPanel
317 * @tc.desc: InputMethodAbility StartInput Without Panel
318 * @tc.type: FUNC
319 * @tc.require:
320 */
321 HWTEST_F(InputMethodAbilityTest, testStartInputWithoutPanel, TestSize.Level0)
322 {
323     IMSA_HILOGI("InputMethodAbilityTest testStartInputWithoutAttach start.");
324     inputMethodAbility_->SetImeListener(std::make_shared<InputMethodEngineListenerImpl>());
325     sptr<InputDataChannelStub> channelStub = new InputDataChannelStub();
326     InputClientInfo clientInfo;
327     clientInfo.channel = channelStub;
328     auto ret = inputMethodAbility_->StartInput(clientInfo, false);
329     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
330     clientInfo.isShowKeyboard = true;
331     ret = inputMethodAbility_->StartInput(clientInfo, false);
332     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
333 }
334 
335 /**
336 * @tc.name: testHideKeyboardSelf
337 * @tc.desc: InputMethodAbility HideKeyboardSelf
338 * @tc.type: FUNC
339 * @tc.require:
340 * @tc.author: Hollokin
341 */
342 HWTEST_F(InputMethodAbilityTest, testHideKeyboardSelf, TestSize.Level0)
343 {
344     IMSA_HILOGI("InputMethodAbility testHideKeyboardSelf START");
345     imc_->Attach(textListener_);
346     std::unique_lock<std::mutex> lock(InputMethodAbilityTest::imeListenerCallbackLock_);
347     InputMethodAbilityTest::showKeyboard_ = true;
348     inputMethodAbility_->SetImeListener(std::make_shared<InputMethodEngineListenerImpl>());
349     auto ret = inputMethodAbility_->HideKeyboardSelf();
350     InputMethodAbilityTest::imeListenerCv_.wait_for(
__anon0131c2710102null351         lock, std::chrono::seconds(DEALY_TIME), [] { return InputMethodAbilityTest::showKeyboard_ == false; });
352     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
353     EXPECT_FALSE(InputMethodAbilityTest::showKeyboard_);
354 }
355 
356 /**
357 * @tc.name: testMoveCursor
358 * @tc.desc: InputMethodAbility MoveCursor
359 * @tc.type: FUNC
360 * @tc.require:
361 * @tc.author: Hollokin
362 */
363 HWTEST_F(InputMethodAbilityTest, testMoveCursor, TestSize.Level0)
364 {
365     IMSA_HILOGI("InputMethodAbility MoveCursor Test START");
366     constexpr int32_t keyCode = 4;
367     auto ret = inputMethodAbility_->MoveCursor(keyCode); // move cursor right });
368     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
369     EXPECT_TRUE(TextListener::WaitMoveCursor(keyCode));
370 }
371 
372 /**
373 * @tc.name: testInsertText
374 * @tc.desc: InputMethodAbility InsertText
375 * @tc.type: FUNC
376 * @tc.require:
377 * @tc.author: Hollokin
378 */
379 HWTEST_F(InputMethodAbilityTest, testInsertText, TestSize.Level0)
380 {
381     IMSA_HILOGI("InputMethodAbility InsertText Test START");
382     std::string text = "text";
383     std::u16string u16Text = Str8ToStr16(text);
384     auto ret = inputMethodAbility_->InsertText(text);
385     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
386     EXPECT_TRUE(TextListener::WaitInsertText(u16Text));
387 }
388 
389 /**
390 * @tc.name: testSendFunctionKey
391 * @tc.desc: InputMethodAbility SendFunctionKey
392 * @tc.type: FUNC
393 * @tc.require:
394 * @tc.author: Hollokin
395 */
396 HWTEST_F(InputMethodAbilityTest, testSendFunctionKey, TestSize.Level0)
397 {
398     IMSA_HILOGI("InputMethodAbility SendFunctionKey Test START");
399     constexpr int32_t funcKey = 1;
400     auto ret = inputMethodAbility_->SendFunctionKey(funcKey);
401     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
402     EXPECT_TRUE(TextListener::WaitSendFunctionKey(funcKey));
403 }
404 
405 /**
406 * @tc.name: testSendExtendAction
407 * @tc.desc: InputMethodAbility SendExtendAction
408 * @tc.type: FUNC
409 * @tc.require:
410 * @tc.author: chenyu
411 */
412 HWTEST_F(InputMethodAbilityTest, testSendExtendAction, TestSize.Level0)
413 {
414     IMSA_HILOGI("InputMethodAbility SendExtendAction Test START");
415     constexpr int32_t action = 1;
416     auto ret = inputMethodAbility_->SendExtendAction(action);
417     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
418     EXPECT_TRUE(TextListener::WaitHandleExtendAction(action));
419 }
420 
421 /**
422 * @tc.name: testDeleteText
423 * @tc.desc: InputMethodAbility DeleteForward & DeleteBackward
424 * @tc.type: FUNC
425 * @tc.require:
426 * @tc.author: Hollokin
427 */
428 HWTEST_F(InputMethodAbilityTest, testDeleteText, TestSize.Level0)
429 {
430     IMSA_HILOGI("InputMethodAbility testDelete Test START");
431     int32_t deleteForwardLenth = 1;
432     auto ret = inputMethodAbility_->DeleteForward(deleteForwardLenth);
433     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
434     EXPECT_TRUE(TextListener::WaitDeleteBackward(deleteForwardLenth));
435 
436     int32_t deleteBackwardLenth = 2;
437     ret = inputMethodAbility_->DeleteBackward(deleteBackwardLenth);
438     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
439     EXPECT_TRUE(TextListener::WaitDeleteForward(deleteBackwardLenth));
440 }
441 
442 /**
443 * @tc.name: testGetEnterKeyType
444 * @tc.desc: InputMethodAbility GetEnterKeyType & GetInputPattern
445 * @tc.type: FUNC
446 * @tc.require:
447 * @tc.author: Hollokin
448 */
449 HWTEST_F(InputMethodAbilityTest, testGetEnterKeyType, TestSize.Level0)
450 {
451     IMSA_HILOGI("InputMethodAbility testGetEnterKeyType START");
452     Configuration config;
453     EnterKeyType keyType = EnterKeyType::NEXT;
454     config.SetEnterKeyType(keyType);
455     TextInputType textInputType = TextInputType::DATETIME;
456     config.SetTextInputType(textInputType);
457     imc_->OnConfigurationChange(config);
458     int32_t keyType2;
459     auto ret = inputMethodAbility_->GetEnterKeyType(keyType2);
460     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
461     EXPECT_EQ(keyType2, (int)keyType);
462     int32_t inputPattern;
463     ret = inputMethodAbility_->GetInputPattern(inputPattern);
464     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
465     EXPECT_EQ(inputPattern, (int)textInputType);
466 }
467 
468 /**
469 * @tc.name: testGetTextConfig
470 * @tc.desc: InputMethodAbility GetTextConfig
471 * @tc.type: FUNC
472 * @tc.require:
473 * @tc.author: Hollokin
474 */
475 HWTEST_F(InputMethodAbilityTest, testGetTextConfig, TestSize.Level0)
476 {
477     IMSA_HILOGI("InputMethodAbility testGetTextConfig START");
478     TextConfig textConfig;
479     textConfig.inputAttribute = { .inputPattern = 0, .enterKeyType = 1 };
480     auto ret = imc_->Attach(textListener_, false, textConfig);
481     TextTotalConfig textTotalConfig;
482     ret = inputMethodAbility_->GetTextConfig(textTotalConfig);
483     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
484     EXPECT_EQ(textTotalConfig.inputAttribute.inputPattern, textConfig.inputAttribute.inputPattern);
485     EXPECT_EQ(textTotalConfig.inputAttribute.enterKeyType, textConfig.inputAttribute.enterKeyType);
486 }
487 
488 /**
489 * @tc.name: testSelectByRange_001
490 * @tc.desc: InputMethodAbility SelectByRange
491 * @tc.type: FUNC
492 * @tc.require:
493 * @tc.author: Zhaolinglan
494 */
495 HWTEST_F(InputMethodAbilityTest, testSelectByRange_001, TestSize.Level0)
496 {
497     IMSA_HILOGI("InputMethodAbility testSelectByRange_001 START");
498     constexpr int32_t start = 1;
499     constexpr int32_t end = 2;
500     auto ret = inputMethodAbility_->SelectByRange(start, end);
501     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
502     EXPECT_TRUE(TextListener::WaitHandleSetSelection(start, end));
503 }
504 
505 /**
506 * @tc.name: testSelectByRange_002
507 * @tc.desc: InputMethodAbility SelectByRange
508 * @tc.type: FUNC
509 * @tc.require:
510 * @tc.author: chenyu
511 */
512 HWTEST_F(InputMethodAbilityTest, testSelectByRange_002, TestSize.Level0)
513 {
514     IMSA_HILOGI("InputMethodAbility testSelectByRange_002 START");
515     int32_t start = -2;
516     int32_t end = 2;
517     auto ret = inputMethodAbility_->SelectByRange(start, end);
518     EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED);
519 
520     start = 2;
521     end = -2;
522     ret = inputMethodAbility_->SelectByRange(start, end);
523     EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED);
524 }
525 
526 /**
527 * @tc.name: testSelectByMovement
528 * @tc.desc: InputMethodAbility SelectByMovement
529 * @tc.type: FUNC
530 * @tc.require:
531 * @tc.author: Zhaolinglan
532 */
533 HWTEST_F(InputMethodAbilityTest, testSelectByMovement, TestSize.Level0)
534 {
535     IMSA_HILOGI("InputMethodAbility testSelectByMovement START");
536     constexpr int32_t direction = 1;
537     auto ret = inputMethodAbility_->SelectByMovement(direction);
538     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
539     EXPECT_TRUE(TextListener::WaitHandleSelect(direction + InputMethodAbilityTest::CURSOR_DIRECTION_BASE_VALUE, 0));
540 }
541 
542 /**
543 * @tc.name: testGetTextAfterCursor
544 * @tc.desc:
545 * @tc.type: FUNC
546 * @tc.require:
547 */
548 HWTEST_F(InputMethodAbilityTest, testGetTextAfterCursor, TestSize.Level0)
549 {
550     IMSA_HILOGI("InputMethodAbility testGetTextAfterCursor START");
551     int32_t number = 3;
552     std::u16string text;
553     auto ret = inputMethodAbility_->GetTextAfterCursor(number, text);
554     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
555     EXPECT_EQ(text, Str8ToStr16(TextListener::TEXT_AFTER_CURSOR));
556 }
557 
558 /**
559 * @tc.name: testGetTextBeforeCursor
560 * @tc.desc:
561 * @tc.type: FUNC
562 * @tc.require:
563 */
564 HWTEST_F(InputMethodAbilityTest, testGetTextBeforeCursor, TestSize.Level0)
565 {
566     IMSA_HILOGI("InputMethodAbility testGetTextBeforeCursor START");
567     int32_t number = 5;
568     std::u16string text;
569     auto ret = inputMethodAbility_->GetTextBeforeCursor(number, text);
570     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
571     EXPECT_EQ(text, Str8ToStr16(TextListener::TEXT_BEFORE_CURSOR));
572 }
573 
574 /**
575 * @tc.name: testGetTextIndexAtCursor
576 * @tc.desc:
577 * @tc.type: FUNC
578 * @tc.require:
579 */
580 HWTEST_F(InputMethodAbilityTest, testGetTextIndexAtCursor, TestSize.Level0)
581 {
582     IMSA_HILOGI("InputMethodAbility testGetTextIndexAtCursor START");
583     int32_t index;
584     auto ret = inputMethodAbility_->GetTextIndexAtCursor(index);
585     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
586     EXPECT_EQ(index, TextListener::TEXT_INDEX);
587 }
588 
589 /**
590 * @tc.name: testCreatePanel001
591 * @tc.desc: It's allowed to create one SOFT_KEYBOARD panel, but two is denied.
592 * @tc.type: FUNC
593 * @tc.require:
594 */
595 HWTEST_F(InputMethodAbilityTest, testCreatePanel001, TestSize.Level0)
596 {
597     IMSA_HILOGI("InputMethodAbilityTest testCreatePanel001 START. You can not create two SOFT_KEYBOARD panel.");
598     AccessScope scope(currentImeTokenId_, currentImeUid_);
599     std::shared_ptr<InputMethodPanel> softKeyboardPanel1 = nullptr;
600     PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
601     auto ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo, softKeyboardPanel1);
602     EXPECT_TRUE(softKeyboardPanel1 != nullptr);
603     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
604 
605     std::shared_ptr<InputMethodPanel> softKeyboardPanel2 = nullptr;
606     ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo, softKeyboardPanel2);
607     EXPECT_TRUE(softKeyboardPanel2 == nullptr);
608     EXPECT_EQ(ret, ErrorCode::ERROR_OPERATE_PANEL);
609 
610     ret = inputMethodAbility_->DestroyPanel(softKeyboardPanel1);
611     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
612 
613     ret = inputMethodAbility_->DestroyPanel(softKeyboardPanel2);
614     EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
615 }
616 
617 /**
618 * @tc.name: testCreatePanel002
619 * @tc.desc: It's allowed to create one STATUS_BAR panel, but two is denied.
620 * @tc.type: FUNC
621 * @tc.require:
622 */
623 HWTEST_F(InputMethodAbilityTest, testCreatePanel002, TestSize.Level0)
624 {
625     IMSA_HILOGI("InputMethodAbilityTest testCreatePanel002 START. You can not create two STATUS_BAR panel.");
626     AccessScope scope(currentImeTokenId_, currentImeUid_);
627     std::shared_ptr<InputMethodPanel> statusBar1 = nullptr;
628     PanelInfo panelInfo = { .panelType = STATUS_BAR };
629     auto ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo, statusBar1);
630     EXPECT_TRUE(statusBar1 != nullptr);
631     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
632 
633     std::shared_ptr<InputMethodPanel> statusBar2 = nullptr;
634     ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo, statusBar2);
635     EXPECT_TRUE(statusBar2 == nullptr);
636     EXPECT_EQ(ret, ErrorCode::ERROR_OPERATE_PANEL);
637 
638     ret = inputMethodAbility_->DestroyPanel(statusBar1);
639     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
640 
641     ret = inputMethodAbility_->DestroyPanel(statusBar2);
642     EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
643 }
644 
645 /**
646 * @tc.name: testCreatePanel003
647 * @tc.desc: It's allowed to create one STATUS_BAR panel and one SOFT_KEYBOARD panel.
648 * @tc.type: FUNC
649 * @tc.require:
650 */
651 HWTEST_F(InputMethodAbilityTest, testCreatePanel003, TestSize.Level0)
652 {
653     IMSA_HILOGI("InputMethodAbilityTest testCreatePanel006 START. Allowed to create one SOFT_KEYBOARD panel and "
654                 "one STATUS_BAR panel.");
655     AccessScope scope(currentImeTokenId_, currentImeUid_);
656     std::shared_ptr<InputMethodPanel> softKeyboardPanel = nullptr;
657     PanelInfo panelInfo1 = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
658     auto ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo1, softKeyboardPanel);
659     EXPECT_TRUE(softKeyboardPanel != nullptr);
660     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
661 
662     PanelInfo panelInfo2 = { .panelType = STATUS_BAR };
663     std::shared_ptr<InputMethodPanel> statusBar = nullptr;
664     ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo2, statusBar);
665     EXPECT_TRUE(statusBar != nullptr);
666     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
667 
668     ret = inputMethodAbility_->DestroyPanel(softKeyboardPanel);
669     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
670 
671     ret = inputMethodAbility_->DestroyPanel(statusBar);
672     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
673 }
674 
675 /**
676 * @tc.name: testCreatePanel004
677 * @tc.desc: It's allowed to create one STATUS_BAR panel and one SOFT_KEYBOARD panel.
678 * @tc.type: FUNC
679 * @tc.require:
680 */
681 HWTEST_F(InputMethodAbilityTest, testCreatePanel004, TestSize.Level0)
682 {
683     IMSA_HILOGI("InputMethodAbilityTest testCreatePanel006 START. Allowed to create one SOFT_KEYBOARD panel and "
684                 "one STATUS_BAR panel.");
685     AccessScope scope(currentImeTokenId_, currentImeUid_);
686     std::shared_ptr<InputMethodPanel> inputMethodPanel = nullptr;
687     PanelInfo panelInfo1 = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
688     auto ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo1, inputMethodPanel);
689     EXPECT_TRUE(inputMethodPanel != nullptr);
690     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
691 
692     ret = inputMethodAbility_->DestroyPanel(inputMethodPanel);
693     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
694 
695     PanelInfo panelInfo2 = { .panelType = STATUS_BAR };
696     ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo2, inputMethodPanel);
697     EXPECT_TRUE(inputMethodPanel != nullptr);
698     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
699 
700     ret = inputMethodAbility_->DestroyPanel(inputMethodPanel);
701     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
702 
703     panelInfo1.panelFlag = FLG_FLOATING;
704     ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo1, inputMethodPanel);
705     EXPECT_TRUE(inputMethodPanel != nullptr);
706     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
707 
708     ret = inputMethodAbility_->DestroyPanel(inputMethodPanel);
709     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
710 }
711 
712 /**
713 * @tc.name: testCreatePanel005
714 * @tc.desc: It's allowed to create one SOFT_KEYBOARD panel, but two is denied.
715 * @tc.type: FUNC
716 * @tc.require:
717 */
718 HWTEST_F(InputMethodAbilityTest, testCreatePanel005, TestSize.Level0)
719 {
720     IMSA_HILOGI("InputMethodAbilityTest testCreatePanel005 START.");
721     AccessScope scope(currentImeTokenId_, currentImeUid_);
722     std::shared_ptr<InputMethodPanel> softKeyboardPanel1 = nullptr;
723     PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
724     auto ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo, softKeyboardPanel1);
725     EXPECT_TRUE(softKeyboardPanel1 != nullptr);
726     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
727 
728     ret = inputMethodAbility_->DestroyPanel(softKeyboardPanel1);
729     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
730 
731     std::shared_ptr<InputMethodPanel> softKeyboardPanel2 = nullptr;
732     ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo, softKeyboardPanel2);
733     EXPECT_TRUE(softKeyboardPanel2 != nullptr);
734     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
735 
736     ret = inputMethodAbility_->DestroyPanel(softKeyboardPanel2);
737     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
738 }
739 
740 /**
741 * @tc.name: testCreatePanel006
742 * @tc.desc: It's allowed to create one SOFT_KEYBOARD panel, but two is denied.
743 * @tc.type: FUNC
744 * @tc.require:
745 */
746 HWTEST_F(InputMethodAbilityTest, testCreatePanel006, TestSize.Level0)
747 {
748     IMSA_HILOGI("InputMethodAbilityTest testCreatePanel006 START.");
749     AccessScope scope(currentImeTokenId_, currentImeUid_);
750     std::shared_ptr<InputMethodPanel> softKeyboardPanel1 = nullptr;
751     PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
752     auto ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo, softKeyboardPanel1);
753     EXPECT_TRUE(softKeyboardPanel1 != nullptr);
754     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
755 
756     std::shared_ptr<InputMethodPanel> softKeyboardPanel2 = nullptr;
757     ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo, softKeyboardPanel2);
758     EXPECT_TRUE(softKeyboardPanel2 == nullptr);
759     EXPECT_EQ(ret, ErrorCode::ERROR_OPERATE_PANEL);
760 
761     ret = inputMethodAbility_->DestroyPanel(softKeyboardPanel1);
762     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
763 
764     std::shared_ptr<InputMethodPanel> softKeyboardPanel3 = nullptr;
765     ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo, softKeyboardPanel3);
766     EXPECT_TRUE(softKeyboardPanel3 != nullptr);
767     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
768 
769     ret = inputMethodAbility_->DestroyPanel(softKeyboardPanel2);
770     EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
771 
772     ret = inputMethodAbility_->DestroyPanel(softKeyboardPanel3);
773     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
774 }
775 
776 /**
777 * @tc.name: testSetCallingWindow001
778 * @tc.desc: InputMethodAbility SetCallingWindow
779 * @tc.type: FUNC
780 * @tc.require:
781 * @tc.author: Hollokin
782 */
783 HWTEST_F(InputMethodAbilityTest, testSetCallingWindow001, TestSize.Level0)
784 {
785     IMSA_HILOGI("InputMethodAbility testSetCallingWindow001 START");
786     std::unique_lock<std::mutex> lock(InputMethodAbilityTest::imeListenerCallbackLock_);
787     InputMethodAbilityTest::showKeyboard_ = true;
788     inputMethodAbility_->SetImeListener(std::make_shared<InputMethodEngineListenerImpl>());
789     uint32_t windowId = 10;
790     inputMethodAbility_->SetCallingWindow(windowId);
791     InputMethodAbilityTest::imeListenerCv_.wait_for(
__anon0131c2710202null792         lock, std::chrono::seconds(DEALY_TIME), [windowId] { return InputMethodAbilityTest::windowId_ == windowId; });
793     EXPECT_EQ(InputMethodAbilityTest::windowId_, windowId);
794 }
795 
796 /**
797 * @tc.name: testNotifyPanelStatusInfo_001
798 * @tc.desc: ShowKeyboard HideKeyboard SOFT_KEYBOARD FLG_FIXED
799 * @tc.type: FUNC
800 * @tc.require:
801 * @tc.author: chenyu
802 */
803 HWTEST_F(InputMethodAbilityTest, testNotifyPanelStatusInfo_001, TestSize.Level0)
804 {
805     IMSA_HILOGI("InputMethodAbility testNotifyPanelStatusInfo_001 START");
806     imc_->Attach(textListener_);
807     PanelInfo info = { .panelType = STATUS_BAR };
808     auto panel = std::make_shared<InputMethodPanel>();
809     AccessScope scope(currentImeTokenId_, currentImeUid_);
810     auto ret = inputMethodAbility_->CreatePanel(nullptr, info, panel);
811     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
812     auto panel1 = std::make_shared<InputMethodPanel>();
813     PanelInfo info1 = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
814     ret = inputMethodAbility_->CreatePanel(nullptr, info1, panel1);
815     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
816 
817     TextListener::ResetParam();
818     ret = inputMethodAbility_->ShowKeyboard();
819     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
820     EXPECT_TRUE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::SHOW));
821     EXPECT_TRUE(TextListener::WaitNotifyPanelStatusInfoCallback({ info1, true, Trigger::IMF }));
822 
823     TextListener::ResetParam();
824     ret = inputMethodAbility_->HideKeyboard();
825     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
826     EXPECT_TRUE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::HIDE));
827     EXPECT_TRUE(TextListener::WaitNotifyPanelStatusInfoCallback({ info1, false, Trigger::IMF }));
828 
829     ret = inputMethodAbility_->DestroyPanel(panel);
830     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
831     ret = inputMethodAbility_->DestroyPanel(panel1);
832     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
833 }
834 
835 /**
836 * @tc.name: testNotifyPanelStatusInfo_002
837 * @tc.desc: ShowPanel HidePanel SOFT_KEYBOARD  FLG_FLOATING
838 * @tc.type: FUNC
839 * @tc.require:
840 * @tc.author: chenyu
841 */
842 HWTEST_F(InputMethodAbilityTest, testNotifyPanelStatusInfo_002, TestSize.Level0)
843 {
844     IMSA_HILOGI("InputMethodAbility testNotifyPanelStatusInfo_002 START");
845     imc_->Attach(textListener_);
846     PanelInfo info = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FLOATING };
847     AccessScope scope(currentImeTokenId_, currentImeUid_);
848     auto panel = std::make_shared<InputMethodPanel>();
849     auto ret = inputMethodAbility_->CreatePanel(nullptr, info, panel);
850     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
851 
852     // ShowPanel
853     CheckPanelStatusInfo(panel, { info, true, Trigger::IME_APP });
854     // HidePanel
855     CheckPanelStatusInfo(panel, { info, false, Trigger::IME_APP });
856 
857     ret = inputMethodAbility_->DestroyPanel(panel);
858     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
859 }
860 
861 /**
862 * @tc.name: testNotifyPanelStatusInfo_003
863 * @tc.desc: ShowPanel HidePanel STATUS_BAR
864 * @tc.type: FUNC
865 * @tc.require:
866 * @tc.author: chenyu
867 */
868 HWTEST_F(InputMethodAbilityTest, testNotifyPanelStatusInfo_003, TestSize.Level0)
869 {
870     IMSA_HILOGI("InputMethodAbility testNotifyPanelStatusInfo_003 START");
871     imc_->Attach(textListener_);
872     PanelInfo info = { .panelType = STATUS_BAR };
873     auto panel = std::make_shared<InputMethodPanel>();
874     AccessScope scope(currentImeTokenId_, currentImeUid_);
875     auto ret = inputMethodAbility_->CreatePanel(nullptr, info, panel);
876     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
877 
878     // ShowPanel
879     CheckPanelStatusInfo(panel, { info, true, Trigger::IME_APP });
880     // HidePanel
881     CheckPanelStatusInfo(panel, { info, false, Trigger::IME_APP });
882 
883     ret = inputMethodAbility_->DestroyPanel(panel);
884     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
885 }
886 
887 /**
888 * @tc.name: testNotifyPanelStatusInfo_004
889 * @tc.desc: ShowPanel HidePanel SOFT_KEYBOARD  FLG_CANDIDATE_COLUMN
890 * @tc.type: FUNC
891 * @tc.require:
892 * @tc.author: chenyu
893 */
894 HWTEST_F(InputMethodAbilityTest, testNotifyPanelStatusInfo_004, TestSize.Level0)
895 {
896     IMSA_HILOGI("InputMethodAbility testNotifyPanelStatusInfo_004 START");
897     imc_->Attach(textListener_);
898     PanelInfo info = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_CANDIDATE_COLUMN };
899     auto panel = std::make_shared<InputMethodPanel>();
900     AccessScope scope(currentImeTokenId_, currentImeUid_);
901     auto ret = inputMethodAbility_->CreatePanel(nullptr, info, panel);
902     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
903 
904     // ShowPanel
905     CheckPanelStatusInfo(panel, { info, true, Trigger::IME_APP });
906     // HidePanel
907     CheckPanelStatusInfo(panel, { info, false, Trigger::IME_APP });
908 
909     ret = inputMethodAbility_->DestroyPanel(panel);
910     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
911 }
912 
913 /**
914 * @tc.name: testNotifyPanelStatusInfo_005
915 * @tc.desc: HideKeyboardSelf
916 * @tc.type: FUNC
917 * @tc.require:
918 * @tc.author: chenyu
919 */
920 HWTEST_F(InputMethodAbilityTest, testNotifyPanelStatusInfo_005, TestSize.Level0)
921 {
922     IMSA_HILOGI("InputMethodAbility testNotifyPanelStatusInfo_005 START");
923     PanelInfo info = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FLOATING };
924     imc_->Attach(textListener_);
925 
926     // has no panel
927     TextListener::ResetParam();
928     auto ret = inputMethodAbility_->HideKeyboardSelf();
929     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
930     EXPECT_TRUE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::HIDE));
931     EXPECT_FALSE(TextListener::WaitNotifyPanelStatusInfoCallback({ info, false, Trigger::IME_APP }));
932 
933     AccessScope scope(currentImeTokenId_, currentImeUid_);
934     auto panel = std::make_shared<InputMethodPanel>();
935     ret = inputMethodAbility_->CreatePanel(nullptr, info, panel);
936     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
937     ret = inputMethodAbility_->ShowPanel(panel);
938     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
939     // has panel
940     TextListener::ResetParam();
941     ret = inputMethodAbility_->HideKeyboardSelf();
942     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
943     EXPECT_TRUE(TextListener::WaitSendKeyboardStatusCallback(KeyboardStatus::HIDE));
944     EXPECT_TRUE(TextListener::WaitNotifyPanelStatusInfoCallback({ info, false, Trigger::IME_APP }));
945 
946     ret = inputMethodAbility_->DestroyPanel(panel);
947     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
948 }
949 
950 /**
951 * @tc.name: testNotifyKeyboardHeight_001
952 * @tc.desc: NotifyKeyboardHeight SOFT_KEYBOARD  FLG_FIXED
953 * @tc.type: FUNC
954 * @tc.require:
955 * @tc.author: mashaoyin
956 */
957 HWTEST_F(InputMethodAbilityTest, testNotifyKeyboardHeight_001, TestSize.Level0)
958 {
959     IMSA_HILOGI("InputMethodAbility testNotifyKeyboardHeight_001 START");
960     imc_->Attach(textListener_);
961     AccessScope scope(currentImeTokenId_, currentImeUid_);
962     TextListener::ResetParam();
963     inputMethodAbility_->NotifyKeyboardHeight(1, FLG_FIXED);
964     EXPECT_TRUE(TextListener::WaitNotifyKeyboardHeightCallback(1));
965 }
966 
967 /**
968 * @tc.name: testNotifyKeyboardHeight_002
969 * @tc.desc: NotifyKeyboardHeight SOFT_KEYBOARD  FLG_CANDIDATE_COLUMN
970 * @tc.type: FUNC
971 * @tc.require:
972 * @tc.author: mashaoyin
973 */
974 HWTEST_F(InputMethodAbilityTest, testNotifyKeyboardHeight_002, TestSize.Level0)
975 {
976     IMSA_HILOGI("InputMethodAbility testNotifyKeyboardHeight_002 START");
977     imc_->Attach(textListener_);
978     AccessScope scope(currentImeTokenId_, currentImeUid_);
979     TextListener::ResetParam();
980     inputMethodAbility_->NotifyKeyboardHeight(1, FLG_CANDIDATE_COLUMN);
981     EXPECT_TRUE(TextListener::WaitNotifyKeyboardHeightCallback(0));
982 }
983 
984 /**
985 * @tc.name: testNotifyKeyboardHeight_003
986 * @tc.desc: NotifyKeyboardHeight Attach with hard keyboard
987 * @tc.type: FUNC
988 * @tc.require:
989 * @tc.author: mashaoyin
990 */
991 HWTEST_F(InputMethodAbilityTest, testNotifyKeyboardHeight_003, TestSize.Level0)
992 {
993     IMSA_HILOGI("InputMethodAbility testNotifyKeyboardHeight_003 START");
994     TextListener::ResetParam();
995     AccessScope scope(currentImeTokenId_, currentImeUid_);
996     PanelInfo info = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_CANDIDATE_COLUMN };
997     auto panel = std::make_shared<InputMethodPanel>();
998     auto ret = inputMethodAbility_->CreatePanel(nullptr, info, panel);
999     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1000     panel->Resize(1, 1);
1001     imc_->Attach(textListener_);
1002     EXPECT_TRUE(TextListener::WaitNotifyKeyboardHeightCallback(0));
1003     ret = inputMethodAbility_->DestroyPanel(panel);
1004     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1005 }
1006 
1007 /**
1008 * @tc.name: testOnSecurityChange
1009 * @tc.desc: OnSecurityChange
1010 * @tc.type: FUNC
1011 * @tc.require:
1012 * @tc.author: chenyu
1013 */
1014 HWTEST_F(InputMethodAbilityTest, testOnSecurityChange, TestSize.Level0)
1015 {
1016     IMSA_HILOGI("InputMethodAbility testOnSecurityChange START");
1017     int32_t security = 32;
1018     inputMethodAbility_->SetImeListener(std::make_shared<InputMethodEngineListenerImpl>());
1019     auto ret = inputMethodAbility_->OnSecurityChange(security);
1020     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1021     EXPECT_EQ(InputMethodAbilityTest::security_, security);
1022 }
1023 
1024 /**
1025  * @tc.name: testSendPrivateCommand_001
1026  * @tc.desc: IMA SendPrivateCommand current is not default ime.
1027  * @tc.type: FUNC
1028  * @tc.require:
1029  * @tc.author: mashaoyin
1030  */
1031 HWTEST_F(InputMethodAbilityTest, testSendPrivateCommand_001, TestSize.Level0)
1032 {
1033     IMSA_HILOGI("InputMethodAbility testSendPrivateCommand_001 Test START");
1034     IdentityCheckerMock::SetBundleNameValid(false);
1035     TextListener::ResetParam();
1036     InputMethodAbilityTest::GetIMCDetachIMA();
1037     TddUtil::RestoreSelfTokenID();
1038     std::unordered_map<std::string, PrivateDataValue> privateCommand;
1039     auto ret = inputMethodAbility_->SendPrivateCommand(privateCommand);
1040     EXPECT_EQ(ret, ErrorCode::ERROR_NOT_DEFAULT_IME);
1041     IdentityCheckerMock::SetBundleNameValid(true);
1042 }
1043 
1044 /**
1045  * @tc.name: testSendPrivateCommand_002
1046  * @tc.desc: IMA SendPrivateCommand current data specification, default ime, not bound.
1047  * @tc.type: FUNC
1048  * @tc.require:
1049  * @tc.author: mashaoyin
1050  */
1051 HWTEST_F(InputMethodAbilityTest, testSendPrivateCommand_002, TestSize.Level0)
1052 {
1053     IMSA_HILOGI("InputMethodAbility testSendPrivateCommand_002 Test START");
1054     InputMethodAbilityTest::GetIMCDetachIMA();
1055     IdentityCheckerMock::SetBundleNameValid(true);
1056     std::unordered_map<std::string, PrivateDataValue> privateCommand;
1057     PrivateDataValue privateDataValue1 = std::string("stringValue");
1058     privateCommand.insert({ "value1", privateDataValue1 });
1059     auto ret = inputMethodAbility_->SendPrivateCommand(privateCommand);
1060     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
1061     IdentityCheckerMock::SetBundleNameValid(false);
1062 }
1063 
1064 /**
1065  * @tc.name: testSendPrivateCommand_003
1066  * @tc.desc: IMA SendPrivateCommand with correct data specification and all data type.
1067  * @tc.type: FUNC
1068  * @tc.require:
1069  * @tc.author: mashaoyin
1070  */
1071 HWTEST_F(InputMethodAbilityTest, testSendPrivateCommand_003, TestSize.Level0)
1072 {
1073     IMSA_HILOGI("InputMethodAbility testSendPrivateCommand_003 Test START");
1074     TextListener::ResetParam();
1075     InputMethodAbilityTest::GetIMCAttachIMA();
1076     IdentityCheckerMock::SetBundleNameValid(true);
1077     std::unordered_map<std::string, PrivateDataValue> privateCommand;
1078     PrivateDataValue privateDataValue1 = std::string("stringValue");
1079     PrivateDataValue privateDataValue2 = true;
1080     PrivateDataValue privateDataValue3 = 100;
1081     privateCommand.emplace("value1", privateDataValue1);
1082     privateCommand.emplace("value2", privateDataValue2);
1083     privateCommand.emplace("value3", privateDataValue3);
1084     auto ret = inputMethodAbility_->SendPrivateCommand(privateCommand);
1085     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1086     EXPECT_TRUE(TextListener::WaitSendPrivateCommandCallback(privateCommand));
1087     InputMethodAbilityTest::GetIMCDetachIMA();
1088     IdentityCheckerMock::SetBundleNameValid(false);
1089 }
1090 
1091 /**
1092  * @tc.name: testGetCallingWindowInfo_001
1093  * @tc.desc: GetCallingWindowInfo with IMC not bound
1094  * @tc.type: FUNC
1095  * @tc.require:
1096  * @tc.author: zhaolinglan
1097  */
1098 HWTEST_F(InputMethodAbilityTest, testGetCallingWindowInfo_001, TestSize.Level0)
1099 {
1100     IMSA_HILOGI("InputMethodAbility testGetCallingWindowInfo_001 Test START");
1101     InputMethodAbilityTest::GetIMCDetachIMA();
1102     CallingWindowInfo windowInfo;
1103     int32_t ret = InputMethodAbilityTest::inputMethodAbility_->GetCallingWindowInfo(windowInfo);
1104     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NOT_FOUND);
1105 }
1106 
1107 /**
1108  * @tc.name: testGetCallingWindowInfo_002
1109  * @tc.desc: GetCallingWindowInfo with panel not created
1110  * @tc.type: FUNC
1111  * @tc.require:
1112  * @tc.author: zhaolinglan
1113  */
1114 HWTEST_F(InputMethodAbilityTest, testGetCallingWindowInfo_002, TestSize.Level0)
1115 {
1116     IMSA_HILOGI("InputMethodAbility testGetCallingWindowInfo_002 Test START");
1117     AccessScope accessScope(InputMethodAbilityTest::currentImeTokenId_, InputMethodAbilityTest::currentImeUid_);
1118     // bind IMC
1119     InputMethodAbilityTest::GetIMCAttachIMA();
1120     // no panel is created
1121     InputMethodAbilityTest::inputMethodAbility_->panels_.Clear();
1122     CallingWindowInfo windowInfo;
1123     int32_t ret = InputMethodAbilityTest::inputMethodAbility_->GetCallingWindowInfo(windowInfo);
1124     EXPECT_EQ(ret, ErrorCode::ERROR_PANEL_NOT_FOUND);
1125     InputMethodAbilityTest::GetIMCDetachIMA();
1126 }
1127 
1128 /**
1129  * @tc.name: testGetCallingWindowInfo_003
1130  * @tc.desc: GetCallingWindowInfo with only status_bar created
1131  * @tc.type: FUNC
1132  * @tc.require:
1133  * @tc.author: zhaolinglan
1134  */
1135 HWTEST_F(InputMethodAbilityTest, testGetCallingWindowInfo_003, TestSize.Level0)
1136 {
1137     IMSA_HILOGI("InputMethodAbility testGetCallingWindowInfo_003 Test START");
1138     AccessScope accessScope(InputMethodAbilityTest::currentImeTokenId_, InputMethodAbilityTest::currentImeUid_);
1139     // bind IMC
1140     InputMethodAbilityTest::GetIMCAttachIMA();
1141     // only STATUS_BAR panel in IMA
1142     auto inputMethodPanel = std::make_shared<InputMethodPanel>();
1143     PanelInfo info = { PanelType::STATUS_BAR };
1144     InputMethodAbilityTest::inputMethodAbility_->CreatePanel(nullptr, info, inputMethodPanel);
1145     CallingWindowInfo windowInfo;
1146     int32_t ret = InputMethodAbilityTest::inputMethodAbility_->GetCallingWindowInfo(windowInfo);
1147     EXPECT_EQ(ret, ErrorCode::ERROR_PANEL_NOT_FOUND);
1148     InputMethodAbilityTest::inputMethodAbility_->DestroyPanel(inputMethodPanel);
1149     InputMethodAbilityTest::GetIMCDetachIMA();
1150 }
1151 
1152 /**
1153  * @tc.name: testGetCallingWindowInfo_004
1154  * @tc.desc: GetCallingWindowInfo with invalid windowid
1155  * @tc.type: FUNC
1156  * @tc.require:
1157  * @tc.author: zhaolinglan
1158  */
1159 HWTEST_F(InputMethodAbilityTest, testGetCallingWindowInfo_004, TestSize.Level0)
1160 {
1161     IMSA_HILOGI("InputMethodAbility testGetCallingWindowInfo_004 Test START");
1162     AccessScope accessScope(InputMethodAbilityTest::currentImeTokenId_, InputMethodAbilityTest::currentImeUid_);
1163     // bind imc
1164     InputMethodAbilityTest::GetIMCAttachIMA();
1165     // SOFT_KEYBOARD panel exists
1166     auto inputMethodPanel = std::make_shared<InputMethodPanel>();
1167     PanelInfo info = { PanelType::SOFT_KEYBOARD, PanelFlag::FLG_FIXED };
1168     InputMethodAbilityTest::inputMethodAbility_->CreatePanel(nullptr, info, inputMethodPanel);
1169     // invalid window id
1170     InputMethodAbilityTest::imc_->clientInfo_.config.windowId = INVALID_WINDOW_ID;
1171     CallingWindowInfo windowInfo;
1172     int32_t ret = InputMethodAbilityTest::inputMethodAbility_->GetCallingWindowInfo(windowInfo);
1173     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1174     InputMethodAbilityTest::inputMethodAbility_->DestroyPanel(inputMethodPanel);
1175     InputMethodAbilityTest::GetIMCDetachIMA();
1176 }
1177 
1178 /**
1179  * @tc.name: testGetCallingWindowInfo_005
1180  * @tc.desc: GetCallingWindowInfo success
1181  * @tc.type: FUNC
1182  * @tc.require:
1183  * @tc.author: zhaolinglan
1184  */
1185 HWTEST_F(InputMethodAbilityTest, testGetCallingWindowInfo_005, TestSize.Level0)
1186 {
1187     IMSA_HILOGI("InputMethodAbility testGetCallingWindowInfo_005 Test START");
1188     AccessScope accessScope(InputMethodAbilityTest::currentImeTokenId_, InputMethodAbilityTest::currentImeUid_);
1189     // SOFT_KEYBOARD window is created
1190     InputMethodAbilityTest::inputMethodAbility_->panels_.Clear();
1191     auto inputMethodPanel = std::make_shared<InputMethodPanel>();
1192     PanelInfo info = { PanelType::SOFT_KEYBOARD, PanelFlag::FLG_FIXED };
1193     InputMethodAbilityTest::inputMethodAbility_->CreatePanel(nullptr, info, inputMethodPanel);
1194     // bind IMC
1195     InputMethodAbilityTest::GetIMCAttachIMA();
1196     InputMethodAbilityTest::imc_->textConfig_.windowId = TddUtil::WindowManager::currentWindowId_;
1197     // get window info success
1198     CallingWindowInfo windowInfo;
1199     int32_t ret = InputMethodAbilityTest::inputMethodAbility_->GetCallingWindowInfo(windowInfo);
1200     EXPECT_TRUE(ret == ErrorCode::NO_ERROR || ret == ErrorCode::ERROR_WINDOW_MANAGER);
1201     InputMethodAbilityTest::GetIMCDetachIMA();
1202     InputMethodAbilityTest::inputMethodAbility_->DestroyPanel(inputMethodPanel);
1203 }
1204 
1205 /**
1206  * @tc.name: testSetPreviewText_001
1207  * @tc.desc: IMA
1208  * @tc.type: FUNC
1209  * @tc.require:
1210  * @tc.author: zhaolinglan
1211  */
1212 HWTEST_F(InputMethodAbilityTest, testSetPreviewText_001, TestSize.Level0)
1213 {
1214     IMSA_HILOGI("InputMethodAbilityTest testSetPreviewText_001 Test START");
1215     TextListener::ResetParam();
1216     std::string text = "test";
1217     Range range = { 1, 2 };
1218     InputMethodAbilityTest::GetIMCAttachIMA();
1219     InputMethodAbilityTest::imc_->textConfig_.inputAttribute.isTextPreviewSupported = true;
1220     auto ret = InputMethodAbilityTest::inputMethodAbility_->SetPreviewText(text, range);
1221     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1222     EXPECT_EQ(TextListener::previewText_, text);
1223     EXPECT_EQ(TextListener::previewRange_, range);
1224     InputMethodAbilityTest::GetIMCDetachIMA();
1225 }
1226 
1227 /**
1228  * @tc.name: testSetPreviewText_002
1229  * @tc.desc: IMA
1230  * @tc.type: FUNC
1231  * @tc.require:
1232  * @tc.author: zhaolinglan
1233  */
1234 HWTEST_F(InputMethodAbilityTest, testSetPreviewText_002, TestSize.Level0)
1235 {
1236     IMSA_HILOGI("InputMethodAbilityTest testSetPreviewText_002 Test START");
1237     TextListener::ResetParam();
1238     std::string text = "test";
1239     Range range = { 1, 2 };
1240     InputMethodAbilityTest::inputMethodAbility_->ClearDataChannel(
1241         InputMethodAbilityTest::inputMethodAbility_->dataChannelObject_);
1242     auto ret = InputMethodAbilityTest::inputMethodAbility_->SetPreviewText(text, range);
1243     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
1244     EXPECT_NE(TextListener::previewText_, text);
1245     EXPECT_FALSE(TextListener::previewRange_ == range);
1246 }
1247 
1248 /**
1249  * @tc.name: testSetPreviewText_003
1250  * @tc.desc: IMA
1251  * @tc.type: FUNC
1252  * @tc.require:
1253  * @tc.author: zhaolinglan
1254  */
1255 HWTEST_F(InputMethodAbilityTest, testSetPreviewText_003, TestSize.Level0)
1256 {
1257     IMSA_HILOGI("InputMethodAbilityTest testSetPreviewText_003 Test START");
1258     TextListener::ResetParam();
1259     std::string text = "test";
1260     Range range = { 1, 2 };
1261     InputMethodAbilityTest::GetIMCAttachIMA();
1262     InputMethodAbilityTest::imc_->textConfig_.inputAttribute.isTextPreviewSupported = false;
1263     auto ret = InputMethodAbilityTest::inputMethodAbility_->SetPreviewText(text, range);
1264     EXPECT_EQ(ret, ErrorCode::ERROR_TEXT_PREVIEW_NOT_SUPPORTED);
1265     EXPECT_NE(TextListener::previewText_, text);
1266     EXPECT_FALSE(TextListener::previewRange_ == range);
1267     InputMethodAbilityTest::GetIMCDetachIMA();
1268 }
1269 
1270 /**
1271  * @tc.name: testFinishTextPreview_001
1272  * @tc.desc: IMA
1273  * @tc.type: FUNC
1274  * @tc.require:
1275  * @tc.author: zhaolinglan
1276  */
1277 HWTEST_F(InputMethodAbilityTest, testFinishTextPreview_001, TestSize.Level0)
1278 {
1279     IMSA_HILOGI("InputMethodAbilityTest testFinishTextPreview_001 Test START");
1280     TextListener::ResetParam();
1281     InputMethodAbilityTest::GetIMCAttachIMA();
1282     InputMethodAbilityTest::imc_->textConfig_.inputAttribute.isTextPreviewSupported = true;
1283     auto ret = InputMethodAbilityTest::inputMethodAbility_->FinishTextPreview(false);
1284     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
1285     EXPECT_TRUE(TextListener::isFinishTextPreviewCalled_);
1286     InputMethodAbilityTest::GetIMCDetachIMA();
1287 }
1288 
1289 /**
1290  * @tc.name: testFinishTextPreview_002
1291  * @tc.desc: IMA
1292  * @tc.type: FUNC
1293  * @tc.require:
1294  * @tc.author: zhaolinglan
1295  */
1296 HWTEST_F(InputMethodAbilityTest, testFinishTextPreview_002, TestSize.Level0)
1297 {
1298     IMSA_HILOGI("InputMethodAbilityTest testFinishTextPreview_002 Test START");
1299     TextListener::ResetParam();
1300     InputMethodAbilityTest::inputMethodAbility_->ClearDataChannel(
1301         InputMethodAbilityTest::inputMethodAbility_->dataChannelObject_);
1302     auto ret = InputMethodAbilityTest::inputMethodAbility_->FinishTextPreview(false);
1303     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
1304     EXPECT_FALSE(TextListener::isFinishTextPreviewCalled_);
1305 }
1306 
1307 /**
1308  * @tc.name: testFinishTextPreview_003
1309  * @tc.desc: IMA
1310  * @tc.type: FUNC
1311  * @tc.require:
1312  * @tc.author: zhaolinglan
1313  */
1314 HWTEST_F(InputMethodAbilityTest, testFinishTextPreview_003, TestSize.Level0)
1315 {
1316     IMSA_HILOGI("InputMethodAbilityTest testFinishTextPreview_003 Test START");
1317     TextListener::ResetParam();
1318     InputMethodAbilityTest::GetIMCAttachIMA();
1319     InputMethodAbilityTest::imc_->textConfig_.inputAttribute.isTextPreviewSupported = false;
1320     auto ret = InputMethodAbilityTest::inputMethodAbility_->FinishTextPreview(false);
1321     EXPECT_EQ(ret, ErrorCode::ERROR_TEXT_PREVIEW_NOT_SUPPORTED);
1322     EXPECT_FALSE(TextListener::isFinishTextPreviewCalled_);
1323     InputMethodAbilityTest::GetIMCDetachIMA();
1324 }
1325 } // namespace MiscServices
1326 } // namespace OHOS
1327