• 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 
16 #include "input_method_ability.h"
17 
18 #include <gtest/gtest.h>
19 #include <string_ex.h>
20 #include <unistd.h>
21 
22 #include <cstdint>
23 #include <functional>
24 #include <string>
25 #include <thread>
26 #include <vector>
27 
28 #include "global.h"
29 #include "i_input_data_channel.h"
30 #include "input_attribute.h"
31 #include "input_control_channel_stub.h"
32 #include "input_data_channel_proxy.h"
33 #include "input_data_channel_stub.h"
34 #include "input_method_agent_stub.h"
35 #include "input_method_controller.h"
36 #include "input_method_core_proxy.h"
37 #include "input_method_core_stub.h"
38 #include "input_method_panel.h"
39 #include "message_handler.h"
40 #include "tdd_util.h"
41 #include "text_listener.h"
42 
43 using namespace testing::ext;
44 namespace OHOS {
45 namespace MiscServices {
46 using WindowMgr = TddUtil::WindowManager;
47 constexpr uint32_t DEALY_TIME = 1;
48 class InputMethodAbilityTest : public testing::Test {
49 public:
50     static std::string imeIdStopped_;
51     static std::mutex imeListenerCallbackLock_;
52     static std::condition_variable imeListenerCv_;
53     static bool showKeyboard_;
54     static constexpr int CURSOR_DIRECTION_BASE_VALUE = 2011;
55     static sptr<InputMethodController> imc_;
56     static sptr<InputMethodAbility> inputMethodAbility_;
57     static uint32_t windowId_;
58 
59     class InputMethodEngineListenerImpl : public InputMethodEngineListener {
60     public:
61         InputMethodEngineListenerImpl() = default;
62         ~InputMethodEngineListenerImpl() = default;
63 
OnKeyboardStatus(bool isShow)64         void OnKeyboardStatus(bool isShow)
65         {
66             showKeyboard_ = isShow;
67             InputMethodAbilityTest::imeListenerCv_.notify_one();
68             IMSA_HILOGI("InputMethodEngineListenerImpl OnKeyboardStatus");
69         }
70 
OnInputStart()71         void OnInputStart()
72         {
73             IMSA_HILOGI("InputMethodEngineListenerImpl OnInputStart");
74         }
75 
OnInputStop(const std::string & imeId)76         void OnInputStop(const std::string &imeId)
77         {
78             imeIdStopped_ = imeId;
79             IMSA_HILOGI("InputMethodEngineListenerImpl OnInputStop");
80         }
81 
OnSetCallingWindow(uint32_t windowId)82         void OnSetCallingWindow(uint32_t windowId)
83         {
84             windowId_ = windowId;
85             IMSA_HILOGI("InputMethodEngineListenerImpl OnSetCallingWindow");
86         }
87 
OnSetSubtype(const SubProperty & property)88         void OnSetSubtype(const SubProperty &property)
89         {
90             IMSA_HILOGI("InputMethodEngineListenerImpl OnSetSubtype");
91         }
92     };
SetUpTestCase(void)93     static void SetUpTestCase(void)
94     {
95         // Set the tokenID to the tokenID of the current ime
96         TddUtil::StorageSelfTokenID();
97         std::shared_ptr<Property> property = InputMethodController::GetInstance()->GetCurrentInputMethod();
98         std::string bundleName = property != nullptr ? property->name : "default.inputmethod.unittest";
99         TddUtil::SetTestTokenID(TddUtil::GetTestTokenID(bundleName));
100         inputMethodAbility_ = InputMethodAbility::GetInstance();
101         inputMethodAbility_->OnImeReady();
102         inputMethodAbility_->SetCoreAndAgent();
103         TddUtil::RestoreSelfTokenID();
104         TextListener::ResetParam();
105         TddUtil::WindowManager::RegisterFocusChangeListener();
106     }
TearDownTestCase(void)107     static void TearDownTestCase(void)
108     {
109         IMSA_HILOGI("InputMethodAbilityTest::TearDownTestCase");
110         imc_->Close();
111         TextListener::ResetParam();
112         WindowMgr::HideWindow();
113         WindowMgr::DestroyWindow();
114     }
SetUp()115     void SetUp()
116     {
117         IMSA_HILOGI("InputMethodAbilityTest::SetUp");
118     }
TearDown()119     void TearDown()
120     {
121         IMSA_HILOGI("InputMethodAbilityTest::TearDown");
122     }
123 };
124 
125 std::string InputMethodAbilityTest::imeIdStopped_;
126 std::mutex InputMethodAbilityTest::imeListenerCallbackLock_;
127 std::condition_variable InputMethodAbilityTest::imeListenerCv_;
128 bool InputMethodAbilityTest::showKeyboard_ = true;
129 sptr<InputMethodController> InputMethodAbilityTest::imc_;
130 sptr<InputMethodAbility> InputMethodAbilityTest::inputMethodAbility_;
131 uint32_t InputMethodAbilityTest::windowId_ = 0;
132 
133 /**
134 * @tc.name: testSerializedInputAttribute
135 * @tc.desc: Checkout the serialization of InputAttribute.
136 * @tc.type: FUNC
137 */
138 HWTEST_F(InputMethodAbilityTest, testSerializedInputAttribute, TestSize.Level0)
139 {
140     InputAttribute inAttribute;
141     inAttribute.inputPattern = InputAttribute::PATTERN_PASSWORD;
142     MessageParcel data;
143     EXPECT_TRUE(InputAttribute::Marshalling(inAttribute, data));
144     InputAttribute outAttribute;
145     EXPECT_TRUE(InputAttribute::Unmarshalling(outAttribute, data));
146     EXPECT_TRUE(outAttribute.GetSecurityFlag());
147 }
148 
149 /**
150 * @tc.name: testShowKeyboardInputMethodCoreProxy
151 * @tc.desc: Test InputMethodCoreProxy ShowKeyboard
152 * @tc.type: FUNC
153 * @tc.require: issueI5NXHK
154 */
155 HWTEST_F(InputMethodAbilityTest, testShowKeyboardInputMethodCoreProxy, TestSize.Level0)
156 {
157     IMSA_HILOGI("testShowKeyboardInputMethodCoreProxy start.");
158     sptr<InputMethodCoreStub> coreStub = new InputMethodCoreStub(0);
159     sptr<IInputMethodCore> core = coreStub;
160     auto msgHandler = new (std::nothrow) MessageHandler();
161     coreStub->SetMessageHandler(msgHandler);
162     sptr<InputDataChannelStub> channelStub = new InputDataChannelStub();
163 
164     MessageParcel data;
165     data.WriteRemoteObject(core->AsObject());
166     data.WriteRemoteObject(channelStub->AsObject());
167     sptr<IRemoteObject> coreObject = data.ReadRemoteObject();
168     sptr<IRemoteObject> channelObject = data.ReadRemoteObject();
169 
170     sptr<InputMethodCoreProxy> coreProxy = new InputMethodCoreProxy(coreObject);
171     sptr<InputDataChannelProxy> channelProxy = new InputDataChannelProxy(channelObject);
172     auto ret = coreProxy->ShowKeyboard(channelProxy, false, false);
173     EXPECT_EQ(ret, ErrorCode::ERROR_IME);
174     delete msgHandler;
175 }
176 
177 /**
178 * @tc.name: testShowKeyboardException
179 * @tc.desc: InputMethodAbility ShowKeyboard without imeListener
180 * @tc.type: FUNC
181 * @tc.require:
182 */
183 HWTEST_F(InputMethodAbilityTest, testShowKeyboardException, TestSize.Level0)
184 {
185     IMSA_HILOGI("InputMethodAbilityTest testShowKeyboardException start.");
186     sptr<InputDataChannelStub> channelStub = new InputDataChannelStub();
187     auto ret = inputMethodAbility_->ShowKeyboard(channelStub->AsObject(), false, false);
188     EXPECT_EQ(ret, ErrorCode::ERROR_IME);
189 }
190 
191 /**
192 * @tc.name: testHideKeyboard
193 * @tc.desc: InputMethodAbility HideKeyboard
194 * @tc.type: FUNC
195 * @tc.require:
196 */
197 HWTEST_F(InputMethodAbilityTest, testHideKeyboard, TestSize.Level0)
198 {
199     IMSA_HILOGI("InputMethodAbilityTest testHideKeyboard start.");
200     auto ret = inputMethodAbility_->HideKeyboard();
201     EXPECT_EQ(ret, ErrorCode::ERROR_IME);
202 }
203 
204 /**
205 * @tc.name: testHideKeyboardSelfWithoutImeListener
206 * @tc.desc: InputMethodAbility HideKeyboardSelf Without ImeListener
207 * @tc.type: FUNC
208 * @tc.require:
209 * @tc.author: Hollokin
210 */
211 HWTEST_F(InputMethodAbilityTest, testHideKeyboardSelfWithoutImeListener, TestSize.Level0)
212 {
213     IMSA_HILOGI("InputMethodAbility testHideKeyboardSelfWithoutImeListener START");
214     std::unique_lock<std::mutex> lock(InputMethodAbilityTest::imeListenerCallbackLock_);
215     InputMethodAbilityTest::showKeyboard_ = true;
216     auto ret = inputMethodAbility_->HideKeyboardSelf();
217     auto cvStatus = imeListenerCv_.wait_for(lock, std::chrono::seconds(DEALY_TIME));
218     EXPECT_EQ(cvStatus, std::cv_status::timeout);
219     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
220     EXPECT_TRUE(InputMethodAbilityTest::showKeyboard_);
221 }
222 
223 /**
224 * @tc.name: testShowKeyboard
225 * @tc.desc: InputMethodAbility ShowKeyboard
226 * @tc.type: FUNC
227 * @tc.require:
228 */
229 HWTEST_F(InputMethodAbilityTest, testShowKeyboard, TestSize.Level0)
230 {
231     IMSA_HILOGI("InputMethodAbilityTest testShowKeyboard start.");
232     inputMethodAbility_->SetImeListener(std::make_shared<InputMethodEngineListenerImpl>());
233     sptr<InputDataChannelStub> channelStub = new InputDataChannelStub();
234     auto ret = inputMethodAbility_->ShowKeyboard(channelStub->AsObject(), false, false);
235     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
236     ret = inputMethodAbility_->HideKeyboard();
237     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
238 }
239 
240 /**
241 * @tc.name: testHideKeyboardSelf
242 * @tc.desc: InputMethodAbility HideKeyboardSelf
243 * @tc.type: FUNC
244 * @tc.require:
245 * @tc.author: Hollokin
246 */
247 HWTEST_F(InputMethodAbilityTest, testHideKeyboardSelf, TestSize.Level0)
248 {
249     IMSA_HILOGI("InputMethodAbility testHideKeyboardSelf START");
250     WindowMgr::CreateWindow();
251     WindowMgr::ShowWindow();
252     bool isFocused = FocusChangedListenerTestImpl::isFocused_->GetValue();
253     IMSA_HILOGI("testHideKeyboardSelf getFocus end, isFocused = %{public}d", isFocused);
254     sptr<OnTextChangedListener> textListener = new TextListener();
255     imc_ = InputMethodController::GetInstance();
256     imc_->Attach(textListener);
257     std::unique_lock<std::mutex> lock(InputMethodAbilityTest::imeListenerCallbackLock_);
258     InputMethodAbilityTest::showKeyboard_ = true;
259     inputMethodAbility_->SetImeListener(std::make_shared<InputMethodEngineListenerImpl>());
260     auto ret = inputMethodAbility_->HideKeyboardSelf();
261     InputMethodAbilityTest::imeListenerCv_.wait_for(
__anonbc4090d10102null262         lock, std::chrono::seconds(DEALY_TIME), [] { return InputMethodAbilityTest::showKeyboard_ == false; });
263     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
264     EXPECT_FALSE(InputMethodAbilityTest::showKeyboard_);
265 }
266 
267 /**
268 * @tc.name: testMoveCursor
269 * @tc.desc: InputMethodAbility MoveCursor
270 * @tc.type: FUNC
271 * @tc.require:
272 * @tc.author: Hollokin
273 */
274 HWTEST_F(InputMethodAbilityTest, testMoveCursor, TestSize.Level0)
275 {
276     IMSA_HILOGI("InputMethodAbility MoveCursor Test START");
277     constexpr int32_t keyCode = 4;
278     auto ret = inputMethodAbility_->MoveCursor(keyCode); // move cursor right
279     std::unique_lock<std::mutex> lock(TextListener::textListenerCallbackLock_);
280     TextListener::textListenerCv_.wait_for(
__anonbc4090d10202null281         lock, std::chrono::seconds(DEALY_TIME), [] { return TextListener::direction_ == keyCode; });
282     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
283     EXPECT_EQ(TextListener::direction_, keyCode);
284 }
285 
286 /**
287 * @tc.name: testInsertText
288 * @tc.desc: InputMethodAbility InsertText
289 * @tc.type: FUNC
290 * @tc.require:
291 * @tc.author: Hollokin
292 */
293 HWTEST_F(InputMethodAbilityTest, testInsertText, TestSize.Level0)
294 {
295     IMSA_HILOGI("InputMethodAbility InsertText Test START");
296     std::string text = "text";
297     std::u16string u16Text = Str8ToStr16(text);
298     auto ret = inputMethodAbility_->InsertText(text);
299     std::unique_lock<std::mutex> lock(TextListener::textListenerCallbackLock_);
300     TextListener::textListenerCv_.wait_for(
__anonbc4090d10302null301         lock, std::chrono::seconds(DEALY_TIME), [u16Text] { return TextListener::insertText_ == u16Text; });
302     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
303     EXPECT_EQ(TextListener::insertText_, u16Text);
304 }
305 
306 /**
307 * @tc.name: testSendFunctionKey
308 * @tc.desc: InputMethodAbility SendFunctionKey
309 * @tc.type: FUNC
310 * @tc.require:
311 * @tc.author: Hollokin
312 */
313 HWTEST_F(InputMethodAbilityTest, testSendFunctionKey, TestSize.Level0)
314 {
315     IMSA_HILOGI("InputMethodAbility SendFunctionKey Test START");
316     constexpr int32_t funcKey = 1;
317     auto ret = inputMethodAbility_->SendFunctionKey(funcKey);
318     std::unique_lock<std::mutex> lock(TextListener::textListenerCallbackLock_);
319     TextListener::textListenerCv_.wait_for(
__anonbc4090d10402null320         lock, std::chrono::seconds(DEALY_TIME), [] { return TextListener::key_ == funcKey; });
321     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
322     EXPECT_EQ(TextListener::key_, funcKey);
323 }
324 
325 /**
326 * @tc.name: testSendExtendAction
327 * @tc.desc: InputMethodAbility SendExtendAction
328 * @tc.type: FUNC
329 * @tc.require:
330 * @tc.author: chenyu
331 */
332 HWTEST_F(InputMethodAbilityTest, testSendExtendAction, TestSize.Level0)
333 {
334     IMSA_HILOGI("InputMethodAbility SendExtendAction Test START");
335     constexpr int32_t action = 1;
336     auto ret = inputMethodAbility_->SendExtendAction(action);
337     std::unique_lock<std::mutex> lock(TextListener::textListenerCallbackLock_);
338     TextListener::textListenerCv_.wait_for(
__anonbc4090d10502null339         lock, std::chrono::seconds(DEALY_TIME), [] { return TextListener::action_ == action; });
340     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
341     EXPECT_EQ(TextListener::action_, action);
342 }
343 
344 /**
345 * @tc.name: testDeleteText
346 * @tc.desc: InputMethodAbility DeleteForward & DeleteBackward
347 * @tc.type: FUNC
348 * @tc.require:
349 * @tc.author: Hollokin
350 */
351 HWTEST_F(InputMethodAbilityTest, testDeleteText, TestSize.Level0)
352 {
353     IMSA_HILOGI("InputMethodAbility testDelete Test START");
354     int32_t deleteForwardLenth = 1;
355     auto ret = inputMethodAbility_->DeleteForward(deleteForwardLenth);
356     std::unique_lock<std::mutex> lock(TextListener::textListenerCallbackLock_);
357     TextListener::textListenerCv_.wait_for(lock, std::chrono::seconds(DEALY_TIME),
__anonbc4090d10602null358         [deleteForwardLenth] { return TextListener::deleteBackwardLength_ == deleteForwardLenth; });
359     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
360     EXPECT_EQ(TextListener::deleteBackwardLength_, deleteForwardLenth);
361 
362     int32_t deleteBackwardLenth = 2;
363     ret = inputMethodAbility_->DeleteBackward(deleteBackwardLenth);
364     TextListener::textListenerCv_.wait_for(lock, std::chrono::seconds(DEALY_TIME),
__anonbc4090d10702null365         [deleteBackwardLenth] { return TextListener::deleteForwardLength_ == deleteBackwardLenth; });
366     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
367     EXPECT_EQ(TextListener::deleteForwardLength_, deleteBackwardLenth);
368 }
369 
370 /**
371 * @tc.name: testGetEnterKeyType
372 * @tc.desc: InputMethodAbility GetEnterKeyType & GetInputPattern
373 * @tc.type: FUNC
374 * @tc.require:
375 * @tc.author: Hollokin
376 */
377 HWTEST_F(InputMethodAbilityTest, testGetEnterKeyType, TestSize.Level0)
378 {
379     IMSA_HILOGI("InputMethodAbility testGetEnterKeyType START");
380     Configuration config;
381     EnterKeyType keyType = EnterKeyType::NEXT;
382     config.SetEnterKeyType(keyType);
383     TextInputType textInputType = TextInputType::DATETIME;
384     config.SetTextInputType(textInputType);
385     imc_->OnConfigurationChange(config);
386     int32_t keyType2;
387     auto ret = inputMethodAbility_->GetEnterKeyType(keyType2);
388     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
389     EXPECT_EQ(keyType2, (int)keyType);
390     int32_t inputPattern;
391     ret = inputMethodAbility_->GetInputPattern(inputPattern);
392     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
393     EXPECT_EQ(inputPattern, (int)textInputType);
394 }
395 
396 /**
397 * @tc.name: testGetTextConfig
398 * @tc.desc: InputMethodAbility GetTextConfig
399 * @tc.type: FUNC
400 * @tc.require:
401 * @tc.author: Hollokin
402 */
403 HWTEST_F(InputMethodAbilityTest, testGetTextConfig, TestSize.Level0)
404 {
405     IMSA_HILOGI("InputMethodAbility testGetTextConfig START");
406     sptr<OnTextChangedListener> textListener = new TextListener();
407     TextConfig textConfig;
408     textConfig.inputAttribute = { .inputPattern = 0, .enterKeyType = 1 };
409     auto ret = imc_->Attach(textListener, false, textConfig);
410     TextTotalConfig textTotalConfig;
411     ret = inputMethodAbility_->GetTextConfig(textTotalConfig);
412     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
413     EXPECT_EQ(textTotalConfig.inputAttribute.inputPattern, textConfig.inputAttribute.inputPattern);
414     EXPECT_EQ(textTotalConfig.inputAttribute.enterKeyType, textConfig.inputAttribute.enterKeyType);
415     textListener = nullptr;
416 }
417 
418 /**
419 * @tc.name: testSelectByRange_001
420 * @tc.desc: InputMethodAbility SelectByRange
421 * @tc.type: FUNC
422 * @tc.require:
423 * @tc.author: Zhaolinglan
424 */
425 HWTEST_F(InputMethodAbilityTest, testSelectByRange_001, TestSize.Level0)
426 {
427     IMSA_HILOGI("InputMethodAbility testSelectByRange_001 START");
428     constexpr int32_t start = 1;
429     constexpr int32_t end = 2;
430     auto ret = inputMethodAbility_->SelectByRange(start, end);
431     std::unique_lock<std::mutex> lock(TextListener::textListenerCallbackLock_);
432     TextListener::textListenerCv_.wait_for(lock, std::chrono::seconds(DEALY_TIME),
__anonbc4090d10802null433         [] { return TextListener::selectionStart_ == start && TextListener::selectionEnd_ == end; });
434     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
435     EXPECT_EQ(TextListener::selectionStart_, start);
436     EXPECT_EQ(TextListener::selectionEnd_, end);
437 }
438 
439 /**
440 * @tc.name: testSelectByRange_002
441 * @tc.desc: InputMethodAbility SelectByRange
442 * @tc.type: FUNC
443 * @tc.require:
444 * @tc.author: chenyu
445 */
446 HWTEST_F(InputMethodAbilityTest, testSelectByRange_002, TestSize.Level0)
447 {
448     IMSA_HILOGI("InputMethodAbility testSelectByRange_002 START");
449     int32_t start = -2;
450     int32_t end = 2;
451     auto ret = inputMethodAbility_->SelectByRange(start, end);
452     EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
453 
454     start = 2;
455     end = -2;
456     ret = inputMethodAbility_->SelectByRange(start, end);
457     EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
458 }
459 
460 /**
461 * @tc.name: testSelectByMovement
462 * @tc.desc: InputMethodAbility SelectByMovement
463 * @tc.type: FUNC
464 * @tc.require:
465 * @tc.author: Zhaolinglan
466 */
467 HWTEST_F(InputMethodAbilityTest, testSelectByMovement, TestSize.Level0)
468 {
469     IMSA_HILOGI("InputMethodAbility testSelectByMovement START");
470     constexpr int32_t direction = 1;
471     auto ret = inputMethodAbility_->SelectByMovement(direction);
472     std::unique_lock<std::mutex> lock(TextListener::textListenerCallbackLock_);
__anonbc4090d10902null473     TextListener::textListenerCv_.wait_for(lock, std::chrono::seconds(DEALY_TIME), [] {
474         return TextListener::selectionDirection_ == direction + InputMethodAbilityTest::CURSOR_DIRECTION_BASE_VALUE;
475     });
476     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
477     EXPECT_EQ(TextListener::selectionDirection_, direction + InputMethodAbilityTest::CURSOR_DIRECTION_BASE_VALUE);
478 }
479 
480 /**
481 * @tc.name: testGetTextAfterCursor
482 * @tc.desc:
483 * @tc.type: FUNC
484 * @tc.require:
485 */
486 HWTEST_F(InputMethodAbilityTest, testGetTextAfterCursor, TestSize.Level0)
487 {
488     IMSA_HILOGI("InputMethodAbility testGetTextAfterCursor START");
489     int32_t number = 3;
490     std::u16string text;
491     auto ret = inputMethodAbility_->GetTextAfterCursor(number, text);
492     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
493     EXPECT_EQ(text, Str8ToStr16(TextListener::TEXT_AFTER_CURSOR));
494 }
495 
496 /**
497 * @tc.name: testGetTextAfterCursor_timeout
498 * @tc.desc:
499 * @tc.type: FUNC
500 * @tc.require:
501 */
502 HWTEST_F(InputMethodAbilityTest, testGetTextAfterCursor_timeout, TestSize.Level0)
503 {
504     IMSA_HILOGI("InputMethodAbility testGetTextAfterCursor_timeout START");
505     TextListener::setTimeout(true);
506     int32_t number = 3;
507     std::u16string text;
508     auto ret = inputMethodAbility_->GetTextAfterCursor(number, text);
509     EXPECT_EQ(ret, ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED);
510     TextListener::setTimeout(false);
511 }
512 
513 /**
514 * @tc.name: testGetTextBeforeCursor
515 * @tc.desc:
516 * @tc.type: FUNC
517 * @tc.require:
518 */
519 HWTEST_F(InputMethodAbilityTest, testGetTextBeforeCursor, TestSize.Level0)
520 {
521     IMSA_HILOGI("InputMethodAbility testGetTextBeforeCursor START");
522     int32_t number = 5;
523     std::u16string text;
524     auto ret = inputMethodAbility_->GetTextBeforeCursor(number, text);
525     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
526     EXPECT_EQ(text, Str8ToStr16(TextListener::TEXT_BEFORE_CURSOR));
527 }
528 
529 /**
530 * @tc.name: testGetTextBeforeCursor_timeout
531 * @tc.desc:
532 * @tc.type: FUNC
533 * @tc.require:
534 */
535 HWTEST_F(InputMethodAbilityTest, testGetTextBeforeCursor_timeout, TestSize.Level0)
536 {
537     IMSA_HILOGI("InputMethodAbility testGetTextBeforeCursor_timeout START");
538     TextListener::setTimeout(true);
539     int32_t number = 5;
540     std::u16string text;
541     auto ret = inputMethodAbility_->GetTextBeforeCursor(number, text);
542     EXPECT_EQ(ret, ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED);
543     TextListener::setTimeout(false);
544 }
545 
546 /**
547 * @tc.name: testGetTextIndexAtCursor
548 * @tc.desc:
549 * @tc.type: FUNC
550 * @tc.require:
551 */
552 HWTEST_F(InputMethodAbilityTest, testGetTextIndexAtCursor, TestSize.Level0)
553 {
554     IMSA_HILOGI("InputMethodAbility testGetTextIndexAtCursor START");
555     int32_t index;
556     auto ret = inputMethodAbility_->GetTextIndexAtCursor(index);
557     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
558     EXPECT_EQ(index, TextListener::TEXT_INDEX);
559 }
560 
561 /**
562 * @tc.name: testGetTextIndexAtCursor_timeout
563 * @tc.desc:
564 * @tc.type: FUNC
565 * @tc.require:
566 */
567 HWTEST_F(InputMethodAbilityTest, testGetTextIndexAtCursor_timeout, TestSize.Level0)
568 {
569     IMSA_HILOGI("InputMethodAbility testGetTextIndexAtCursor_timeout START");
570     TextListener::setTimeout(true);
571     int32_t index;
572     auto ret = inputMethodAbility_->GetTextIndexAtCursor(index);
573     EXPECT_EQ(ret, ErrorCode::ERROR_CONTROLLER_INVOKING_FAILED);
574     TextListener::setTimeout(false);
575 }
576 
577 /**
578 * @tc.name: testCreatePanel001
579 * @tc.desc: It's allowed to create one SOFT_KEYBOARD panel, but two is denied.
580 * @tc.type: FUNC
581 * @tc.require:
582 */
583 HWTEST_F(InputMethodAbilityTest, testCreatePanel001, TestSize.Level0)
584 {
585     IMSA_HILOGI("InputMethodAbilityTest testCreatePanel001 START. You can not create two SOFT_KEYBOARD panel.");
586     TddUtil::SetTestTokenID(TddUtil::AllocTestTokenID(true, false, "undefine"));
587     std::shared_ptr<InputMethodPanel> softKeyboardPanel1 = nullptr;
588     PanelInfo panelInfo = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
589     auto ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo, softKeyboardPanel1);
590     EXPECT_TRUE(softKeyboardPanel1 != nullptr);
591     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
592 
593     std::shared_ptr<InputMethodPanel> softKeyboardPanel2 = nullptr;
594     ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo, softKeyboardPanel2);
595     EXPECT_TRUE(softKeyboardPanel2 == nullptr);
596     EXPECT_EQ(ret, ErrorCode::ERROR_OPERATE_PANEL);
597 
598     ret = inputMethodAbility_->DestroyPanel(softKeyboardPanel1);
599     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
600 
601     ret = inputMethodAbility_->DestroyPanel(softKeyboardPanel2);
602     EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
603 }
604 
605 /**
606 * @tc.name: testCreatePanel002
607 * @tc.desc: It's allowed to create one STATUS_BAR panel, but two is denied.
608 * @tc.type: FUNC
609 * @tc.require:
610 */
611 HWTEST_F(InputMethodAbilityTest, testCreatePanel002, TestSize.Level0)
612 {
613     IMSA_HILOGI("InputMethodAbilityTest testCreatePanel002 START. You can not create two STATUS_BAR panel.");
614     std::shared_ptr<InputMethodPanel> statusBar1 = nullptr;
615     PanelInfo panelInfo = { .panelType = STATUS_BAR };
616     auto ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo, statusBar1);
617     EXPECT_TRUE(statusBar1 != nullptr);
618     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
619 
620     std::shared_ptr<InputMethodPanel> statusBar2 = nullptr;
621     ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo, statusBar2);
622     EXPECT_TRUE(statusBar2 == nullptr);
623     EXPECT_EQ(ret, ErrorCode::ERROR_OPERATE_PANEL);
624 
625     ret = inputMethodAbility_->DestroyPanel(statusBar1);
626     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
627 
628     ret = inputMethodAbility_->DestroyPanel(statusBar2);
629     EXPECT_EQ(ret, ErrorCode::ERROR_BAD_PARAMETERS);
630 }
631 
632 /**
633 * @tc.name: testCreatePanel003
634 * @tc.desc: It's allowed to create one STATUS_BAR panel and one SOFT_KEYBOARD panel.
635 * @tc.type: FUNC
636 * @tc.require:
637 */
638 HWTEST_F(InputMethodAbilityTest, testCreatePanel003, TestSize.Level0)
639 {
640     IMSA_HILOGI("InputMethodAbilityTest testCreatePanel006 START. Allowed to create one SOFT_KEYBOARD panel and "
641                 "one STATUS_BAR panel.");
642     std::shared_ptr<InputMethodPanel> softKeyboardPanel = nullptr;
643     PanelInfo panelInfo1 = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
644     auto ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo1, softKeyboardPanel);
645     EXPECT_TRUE(softKeyboardPanel != nullptr);
646     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
647 
648     PanelInfo panelInfo2 = { .panelType = STATUS_BAR };
649     std::shared_ptr<InputMethodPanel> statusBar = nullptr;
650     ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo2, statusBar);
651     EXPECT_TRUE(statusBar != nullptr);
652     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
653 
654     ret = inputMethodAbility_->DestroyPanel(softKeyboardPanel);
655     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
656 
657     ret = inputMethodAbility_->DestroyPanel(statusBar);
658     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
659 }
660 
661 /**
662 * @tc.name: testCreatePanel004
663 * @tc.desc: It's allowed to create one STATUS_BAR panel and one SOFT_KEYBOARD panel.
664 * @tc.type: FUNC
665 * @tc.require:
666 */
667 HWTEST_F(InputMethodAbilityTest, testCreatePanel004, TestSize.Level0)
668 {
669     IMSA_HILOGI("InputMethodAbilityTest testCreatePanel006 START. Allowed to create one SOFT_KEYBOARD panel and "
670                 "one STATUS_BAR panel.");
671     std::shared_ptr<InputMethodPanel> inputMethodPanel = nullptr;
672     PanelInfo panelInfo1 = { .panelType = SOFT_KEYBOARD, .panelFlag = FLG_FIXED };
673     auto ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo1, inputMethodPanel);
674     EXPECT_TRUE(inputMethodPanel != nullptr);
675     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
676 
677     ret = inputMethodAbility_->DestroyPanel(inputMethodPanel);
678     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
679 
680     PanelInfo panelInfo2 = { .panelType = STATUS_BAR };
681     ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo2, inputMethodPanel);
682     EXPECT_TRUE(inputMethodPanel != nullptr);
683     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
684 
685     ret = inputMethodAbility_->DestroyPanel(inputMethodPanel);
686     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
687 
688     panelInfo1.panelFlag = FLG_FLOATING;
689     ret = inputMethodAbility_->CreatePanel(nullptr, panelInfo1, inputMethodPanel);
690     EXPECT_TRUE(inputMethodPanel != nullptr);
691     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
692 
693     ret = inputMethodAbility_->DestroyPanel(inputMethodPanel);
694     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
695 }
696 
697 /**
698 * @tc.name: testSetCallingWindow001
699 * @tc.desc: InputMethodAbility SetCallingWindow
700 * @tc.type: FUNC
701 * @tc.require:
702 * @tc.author: Hollokin
703 */
704 HWTEST_F(InputMethodAbilityTest, testSetCallingWindow001, TestSize.Level0)
705 {
706     IMSA_HILOGI("InputMethodAbility testSetCallingWindow001 START");
707     std::unique_lock<std::mutex> lock(InputMethodAbilityTest::imeListenerCallbackLock_);
708     InputMethodAbilityTest::showKeyboard_ = true;
709     inputMethodAbility_->SetImeListener(std::make_shared<InputMethodEngineListenerImpl>());
710     uint32_t windowId = 10;
711     inputMethodAbility_->SetCallingWindow(windowId);
__anonbc4090d10a02null712     InputMethodAbilityTest::imeListenerCv_.wait_for(lock, std::chrono::seconds(DEALY_TIME), [windowId] {
713         return InputMethodAbilityTest::windowId_ == windowId;
714     });
715     EXPECT_EQ(InputMethodAbilityTest::windowId_, windowId);
716 }
717 } // namespace MiscServices
718 } // namespace OHOS
719