1 /*
2  * Copyright (c) 2022 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 UI_TEST_CUSTOM_INPUT_METHOD_H
17 #define UI_TEST_CUSTOM_INPUT_METHOD_H
18 
19 #include "common/input_method_manager.h"
20 #include "components/ui_edit_text.h"
21 #include "components/ui_label.h"
22 #include "components/ui_label_button.h"
23 #include "components/ui_scroll_view.h"
24 #include "layout/flex_layout.h"
25 #include "ui_test.h"
26 #include "graphic_timer.h"
27 
28 namespace OHOS {
29 constexpr char* UI_TEST_KEY_INPUT = "toggle";
30 enum class KeyboardType {
31     LOW_CASE,
32     UPPER_CASE,
33     NUMBER,
34     SYMBOL
35 };
36 class CustomInputMethod : public UIView::OnClickListener,
37                           public UIView::OnTouchListener,
38                           public UIView::OnLongPressListener,
39                           public InputMethodManager::InputMethodListener {
40 public:
41     class UIEditTextEx : public UIEditText {
42         // override the view type, so the FocusManager not invoke InputMethod onShow function
GetViewType()43         UIViewType GetViewType() const override
44         {
45             return UI_NUMBER_MAX;
46         }
47     };
48 
CustomInputMethod()49     explicit CustomInputMethod(): timer_(300, TimerMethod, this) {} // 300: time(ms) for add/del one letter
~CustomInputMethod()50     ~CustomInputMethod() {}
51 
TimerMethod(void * arg)52     static void TimerMethod(void* arg)
53     {
54         CustomInputMethod *customInputMethod  = reinterpret_cast<CustomInputMethod *>(arg);
55         customInputMethod->DealLongPressKeyEvent();
56     }
57 
58     bool OnRelease(UIView& view, const ReleaseEvent& event) override;
59     bool OnClick(UIView& view, const ClickEvent& event) override;
60     bool OnLongPress(UIView& view, const LongPressEvent& event) override;
61     void OnShow(InputMethodManager::InputMethodParam& param) override;
62     void OnHide() override;
63     void TimerMethod(UIView& view);
64     void DealKeyEvent(UIView& view);
65     void DealLongPressKeyEvent();
66 
67 private:
68     void SetupView(KeyboardType type);
69     void TearDownView();
70     UILabelButton* SetupButton(const char* title);
71     void SetupKeyboard(KeyboardType type);
72     void ChangeKeyboard(KeyboardType type);
73     FlexLayout* SetupKeyRow(const char* name, int16_t width, int16_t height);
74 
75     UIEditTextEx* editView_ = nullptr;
76     UILabelButton* inputTypeBtn_ = nullptr;
77     UIViewGroup* container_ = nullptr;
78     KeyboardType keyboardType_ = KeyboardType::LOW_CASE;
79     GraphicTimer timer_;
80     bool longPressed_ = false;
81     const char* key_ = nullptr;
82 };
83 } // namespace OHOS
84 #endif // UI_TEST_CUSTOM_INPUT_METHOD_H
85