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 #include "components/ui_edit_text.h"
17 #include "common/input_method_manager.h"
18 
19 #include <climits>
20 #include <gtest/gtest.h>
21 
22 using namespace testing::ext;
23 namespace OHOS {
24 class InputMethodManagerTest : public testing::Test {
25 public:
26     static void SetUpTestCase(void);
27     static void TearDownTestCase(void);
28 };
29 
30 class TestInputMethodListener : public InputMethodManager::InputMethodListener {
31 public:
OnShow(InputMethodManager::InputMethodParam & param)32     void OnShow(InputMethodManager::InputMethodParam& param) override
33     {
34         showStatus = true;
35     }
36 
OnHide()37     void OnHide() override
38     {
39         showStatus = false;
40     }
41 
GetShowStatus()42     bool GetShowStatus()
43     {
44         return showStatus;
45     }
46 
47 private:
48     bool showStatus = false;
49 };
50 
SetUpTestCase(void)51 void InputMethodManagerTest::SetUpTestCase(void)
52 {
53 }
54 
TearDownTestCase(void)55 void InputMethodManagerTest::TearDownTestCase(void)
56 {
57     InputMethodManager& inputMethodManager =  InputMethodManager::GetInstance();
58     inputMethodManager.SetInputMethodListener(nullptr);
59 }
60 
61 /**
62  * @tc.name: ShowInputMethod_001
63  * @tc.desc: Verify ShowInputMethod function, equal.
64  * @tc.type: FUNC
65  * @tc.require: issueI5AD4A
66  */
67 HWTEST_F(InputMethodManagerTest, ShowInputMethod_001, TestSize.Level0)
68 {
69     TestInputMethodListener* listener = new TestInputMethodListener();
70     InputMethodManager& inputMethodManager =  InputMethodManager::GetInstance();
71     inputMethodManager.SetInputMethodListener(listener);
72     UIEditText* editView = new UIEditText();
73 
74     inputMethodManager.ShowInputMethod(editView);
75     EXPECT_EQ(listener->GetShowStatus(), true);
76 
77     inputMethodManager.HideInputMethod();
78     EXPECT_EQ(listener->GetShowStatus(), false);
79 
80     delete listener;
81     listener = nullptr;
82     delete editView;
83     editView = nullptr;
84 }
85 
86 /**
87  * @tc.name: InsertText_001
88  * @tc.desc: Verify InsertText function, equal.
89  * @tc.type: FUNC
90  * @tc.require: issueI5AD4A
91  */
92 HWTEST_F(InputMethodManagerTest, InsertText_001, TestSize.Level0)
93 {
94     TestInputMethodListener* listener = new TestInputMethodListener();
95     InputMethodManager& inputMethodManager =  InputMethodManager::GetInstance();
96     inputMethodManager.SetInputMethodListener(listener);
97     UIEditText* editView = new UIEditText();
98     inputMethodManager.ShowInputMethod(editView);
99     std::string text = "hello";
100     inputMethodManager.InsertText(text);
101     int16_t ret = text.compare(editView->GetText());
102     EXPECT_EQ(ret, 0);
103 
104     delete listener;
105     listener = nullptr;
106     delete editView;
107     editView = nullptr;
108 }
109 
110 /**
111  * @tc.name: DeleteBackward_001
112  * @tc.desc: Verify DeleteBackward function, equal.
113  * @tc.type: FUNC
114  * @tc.require: issueI5AD4A
115  */
116 HWTEST_F(InputMethodManagerTest, DeleteBackward_001, TestSize.Level0)
117 {
118     TestInputMethodListener* listener = new TestInputMethodListener();
119     InputMethodManager& inputMethodManager =  InputMethodManager::GetInstance();
120     inputMethodManager.SetInputMethodListener(listener);
121     UIEditText* editView = new UIEditText();
122     inputMethodManager.ShowInputMethod(editView);
123     std::string text = "hello world";
124     inputMethodManager.InsertText(text);
125     int16_t ret = text.compare(editView->GetText());
126     EXPECT_EQ(ret, 0);
127 
128     uint16_t length = 0;
129     inputMethodManager.DeleteBackward(length);
130     EXPECT_EQ(editView->GetText(), text);
131 
132     uint16_t length1 = 1;
133     inputMethodManager.DeleteBackward(length1);
134     std::string tmp = text.substr(0, text.length() - length1);
135     ret = tmp.compare(editView->GetText());
136     EXPECT_EQ(ret, 0);
137 
138     uint16_t length2 = 2; // 2: length
139     inputMethodManager.DeleteBackward(length2);
140     tmp = text.substr(0, text.length() - length1 - length2);
141     ret = tmp.compare(editView->GetText());
142     EXPECT_EQ(ret, 0);
143 
144     uint16_t length3 = 200; // 200: length
145     inputMethodManager.DeleteBackward(length3);
146     tmp = "";
147     ret = tmp.compare(editView->GetText());
148     EXPECT_EQ(ret, 0);
149 
150     delete listener;
151     listener = nullptr;
152     delete editView;
153     editView = nullptr;
154 }
155 
156 /**
157  * @tc.name: SetInputType_001
158  * @tc.desc: Verify SetInputType function, equal.
159  * @tc.type: FUNC
160  * @tc.require: issueI5AD4A
161  */
162 HWTEST_F(InputMethodManagerTest, SetInputType_001, TestSize.Level0)
163 {
164     TestInputMethodListener* listener = new TestInputMethodListener();
165     InputMethodManager& inputMethodManager =  InputMethodManager::GetInstance();
166     inputMethodManager.SetInputMethodListener(listener);
167     UIEditText* editView = new UIEditText();
168     inputMethodManager.ShowInputMethod(editView);
169     inputMethodManager.SetInputType(InputType::TEXT_TYPE);
170     EXPECT_EQ(editView->GetInputType(), InputType::TEXT_TYPE);
171 
172     inputMethodManager.SetInputType(InputType::PASSWORD_TYPE);
173     EXPECT_EQ(editView->GetInputType(), InputType::PASSWORD_TYPE);
174 
175     delete listener;
176     listener = nullptr;
177     delete editView;
178     editView = nullptr;
179 }
180 } // namespace OHOS