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 #ifndef INPUTMETHOD_TEST_TEXT_LISTENER_H
17 #define INPUTMETHOD_TEST_TEXT_LISTENER_H
18 
19 #include <unistd.h>
20 
21 #include <condition_variable>
22 
23 #include "input_method_controller.h"
24 #include "input_method_utils.h"
25 #include "key_event.h"
26 #include "string_ex.h"
27 
28 namespace OHOS {
29 namespace MiscServices {
30 class TextListener : public OnTextChangedListener {
31 public:
32     TextListener();
33     ~TextListener();
34     void InsertText(const std::u16string &text) override;
35     void DeleteForward(int32_t length) override;
36     void DeleteBackward(int32_t length) override;
37     void SendKeyEventFromInputMethod(const KeyEvent &event) override;
38     void SendKeyboardStatus(const KeyboardStatus &keyboardStatus) override;
39     void SendFunctionKey(const FunctionKey &functionKey) override;
40     void SetKeyboardStatus(bool status) override;
41     void MoveCursor(const Direction direction) override;
42     void HandleSetSelection(int32_t start, int32_t end) override;
43     void HandleExtendAction(int32_t action) override;
44     void HandleSelect(int32_t keyCode, int32_t cursorMoveSkip) override;
45     void NotifyPanelStatusInfo(const PanelStatusInfo &info) override;
46     void NotifyKeyboardHeight(uint32_t height) override;
47     std::u16string GetLeftTextOfCursor(int32_t number) override;
48     std::u16string GetRightTextOfCursor(int32_t number) override;
49     int32_t GetTextIndexAtCursor() override;
50     int32_t ReceivePrivateCommand(const std::unordered_map<std::string, PrivateDataValue> &privateCommand) override;
51     int32_t SetPreviewText(const std::u16string &text, const Range &range) override;
52     void FinishTextPreview() override;
53     static void ResetParam();
54     static bool WaitSendKeyboardStatusCallback(const KeyboardStatus &keyboardStatus);
55     static bool WaitNotifyPanelStatusInfoCallback(const PanelStatusInfo &info);
56     static bool WaitNotifyKeyboardHeightCallback(uint32_t height);
57     static bool WaitSendPrivateCommandCallback(std::unordered_map<std::string, PrivateDataValue> &privateCommand);
58     static bool WaitInsertText(const std::u16string &insertText);
59     static bool WaitMoveCursor(int32_t direction);
60     static bool WaitDeleteForward(int32_t length);
61     static bool WaitDeleteBackward(int32_t length);
62     static bool WaitSendFunctionKey(int32_t functionKey);
63     static bool WaitHandleExtendAction(int32_t action);
64     static bool WaitHandleSetSelection(int32_t start, int32_t end);
65     static bool WaitHandleSelect(int32_t keyCode, int32_t cursorMoveSkip);
66     static std::mutex textListenerCallbackLock_;
67     static std::condition_variable textListenerCv_;
68     static int32_t direction_;
69     static int32_t deleteForwardLength_;
70     static int32_t deleteBackwardLength_;
71     static std::u16string insertText_;
72     static int32_t key_;
73     static bool status_;
74     static int32_t selectionStart_;
75     static int32_t selectionEnd_;
76     static int32_t selectionDirection_;
77     static int32_t selectionSkip_;
78     static int32_t action_;
79     static uint32_t height_;
80     static KeyboardStatus keyboardStatus_;
81     static PanelStatusInfo info_;
82     std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
83     static constexpr int32_t TEXT_INDEX = 455;
84     static constexpr const char *TEXT_BEFORE_CURSOR = "before";
85     static constexpr const char *TEXT_AFTER_CURSOR = "after";
86     static std::unordered_map<std::string, PrivateDataValue> privateCommand_;
87     static std::string previewText_;
88     static Range previewRange_;
89     static bool isFinishTextPreviewCalled_;
90 };
91 } // namespace MiscServices
92 } // namespace OHOS
93 
94 #endif // INPUTMETHOD_TEST_TEXT_LISTENER_H
95