1 /*
2 * Copyright (C) 2022-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 <gtest/gtest.h>
17 #define private public
18 #define protected public
19 #include "input_method_ability.h"
20 #undef private
21
22 #include "key_event_util.h"
23
24 using namespace testing::ext;
25 namespace OHOS {
26 namespace MiscServices {
27 constexpr int32_t DEALY_TIME = 20;
28 class InputMethodAbilityExceptionTest : public testing::Test {
29 public:
SetUpTestCase(void)30 static void SetUpTestCase(void)
31 {
32 IMSA_HILOGI("InputMethodAbilityExceptionTest::SetUpTestCase");
33 inputMethodAbility_ = InputMethodAbility::GetInstance();
34 inputMethodAbility_->SetCoreAndAgent();
35 inputMethodAbility_->OnImeReady();
36 imeListener_ = std::make_shared<ImeListenerImpl>();
37 inputMethodAbility_->SetImeListener(imeListener_);
38 std::unique_lock<std::mutex> lock(lock_);
39 cv_.wait_for(lock, std::chrono::milliseconds(DEALY_TIME),
40 [] { return InputMethodAbilityExceptionTest::isInputStart_; });
41 inputMethodAbility_->dataChannelProxy_ = nullptr;
42 inputMethodAbility_->dataChannelObject_ = nullptr;
43 }
TearDownTestCase(void)44 static void TearDownTestCase(void)
45 {
46 IMSA_HILOGI("InputMethodAbilityExceptionTest::TearDownTestCase");
47 inputMethodAbility_->imeListener_ = nullptr;
48 }
SetUp()49 void SetUp()
50 {
51 }
TearDown()52 void TearDown()
53 {
54 }
55 class ImeListenerImpl : public InputMethodEngineListener {
56 public:
ImeListenerImpl()57 ImeListenerImpl(){};
~ImeListenerImpl()58 ~ImeListenerImpl(){};
59 void OnKeyboardStatus(bool isShow) override;
60 void OnInputStart() override;
61 void OnInputStop(const std::string &imeId) override;
62 void OnSetCallingWindow(uint32_t windowId) override;
63 void OnSetSubtype(const SubProperty &property) override;
64 };
65 static sptr<InputMethodAbility> inputMethodAbility_;
66
67 private:
68 static std::shared_ptr<ImeListenerImpl> imeListener_;
69 static std::mutex lock_;
70 static std::condition_variable cv_;
71 static bool isInputStart_;
72 };
OnKeyboardStatus(bool isShow)73 void InputMethodAbilityExceptionTest::ImeListenerImpl::OnKeyboardStatus(bool isShow)
74 {
75 }
OnInputStart()76 void InputMethodAbilityExceptionTest::ImeListenerImpl::OnInputStart()
77 {
78 std::unique_lock<std::mutex> lock(InputMethodAbilityExceptionTest::lock_);
79 InputMethodAbilityExceptionTest::isInputStart_ = true;
80 InputMethodAbilityExceptionTest::cv_.notify_one();
81 }
OnInputStop(const std::string & imeId)82 void InputMethodAbilityExceptionTest::ImeListenerImpl::OnInputStop(const std::string &imeId)
83 {
84 }
OnSetCallingWindow(uint32_t windowId)85 void InputMethodAbilityExceptionTest::ImeListenerImpl::OnSetCallingWindow(uint32_t windowId)
86 {
87 }
OnSetSubtype(const SubProperty & property)88 void InputMethodAbilityExceptionTest::ImeListenerImpl::OnSetSubtype(const SubProperty &property)
89 {
90 }
91 sptr<InputMethodAbility> InputMethodAbilityExceptionTest::inputMethodAbility_;
92 std::shared_ptr<InputMethodAbilityExceptionTest::ImeListenerImpl> InputMethodAbilityExceptionTest::imeListener_;
93 std::mutex InputMethodAbilityExceptionTest::lock_;
94 std::condition_variable InputMethodAbilityExceptionTest::cv_;
95 bool InputMethodAbilityExceptionTest::isInputStart_ = false;
96
97 /**
98 * @tc.name: testMoveCursorException
99 * @tc.desc: InputMethodAbility MoveCursor
100 * @tc.type: FUNC
101 * @tc.require:
102 * @tc.author: Hollokin
103 */
104 HWTEST_F(InputMethodAbilityExceptionTest, testMoveCursorException, TestSize.Level0)
105 {
106 IMSA_HILOGI("InputMethodAbilityExceptionTest MoveCursor Test START");
107 auto ret = inputMethodAbility_->MoveCursor(4); // move cursor right
108 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
109 }
110
111 /**
112 * @tc.name: testInsertTextException
113 * @tc.desc: InputMethodAbility InsertText
114 * @tc.type: FUNC
115 * @tc.require:
116 * @tc.author: Hollokin
117 */
118 HWTEST_F(InputMethodAbilityExceptionTest, testInsertTextException, TestSize.Level0)
119 {
120 IMSA_HILOGI("InputMethodAbilityExceptionTest InsertText Test START");
121 auto ret = inputMethodAbility_->InsertText("text");
122 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
123 }
124
125 /**
126 * @tc.name: testSendFunctionKeyException
127 * @tc.desc: InputMethodAbility SendFunctionKey
128 * @tc.type: FUNC
129 * @tc.require:
130 * @tc.author: Hollokin
131 */
132 HWTEST_F(InputMethodAbilityExceptionTest, testSendFunctionKeyException, TestSize.Level0)
133 {
134 IMSA_HILOGI("InputMethodAbilityExceptionTest SendFunctionKey Test START");
135 auto ret = inputMethodAbility_->SendFunctionKey(0);
136 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
137 }
138
139 /**
140 * @tc.name: testSendExtendActionException
141 * @tc.desc: InputMethodAbility SendExtendAction
142 * @tc.type: FUNC
143 * @tc.require:
144 * @tc.author: chenyu
145 */
146 HWTEST_F(InputMethodAbilityExceptionTest, testSendExtendActionException, TestSize.Level0)
147 {
148 IMSA_HILOGI("InputMethodAbilityExceptionTest SendExtendAction Test START");
149 constexpr int32_t action = 1;
150 auto ret = inputMethodAbility_->SendExtendAction(action);
151 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
152 }
153
154 /**
155 * @tc.name: testSelectByRangeException
156 * @tc.desc: InputMethodAbility SelectByRange
157 * @tc.type: FUNC
158 * @tc.require:
159 * @tc.author: chenyu
160 */
161 HWTEST_F(InputMethodAbilityExceptionTest, testSelectByRangeException, TestSize.Level0)
162 {
163 IMSA_HILOGI("InputMethodAbilityExceptionTest testSelectByRange START");
164 constexpr int32_t start = 1;
165 constexpr int32_t end = 2;
166 auto ret = inputMethodAbility_->SelectByRange(start, end);
167 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
168 }
169
170 /**
171 * @tc.name: testSelectByMovementException
172 * @tc.desc: InputMethodAbility SelectByMovement
173 * @tc.type: FUNC
174 * @tc.require:
175 * @tc.author: chenyu
176 */
177 HWTEST_F(InputMethodAbilityExceptionTest, testSelectByMovementException, TestSize.Level0)
178 {
179 IMSA_HILOGI("InputMethodAbilityExceptionTest testSelectByMovement START");
180 constexpr int32_t direction = 1;
181 auto ret = inputMethodAbility_->SelectByMovement(direction);
182 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
183 }
184
185 /**
186 * @tc.name: testDeleteExceptionText
187 * @tc.desc: InputMethodAbility DeleteForward & DeleteBackward
188 * @tc.type: FUNC
189 * @tc.require:
190 * @tc.author: Hollokin
191 */
192 HWTEST_F(InputMethodAbilityExceptionTest, testDeleteExceptionText, TestSize.Level0)
193 {
194 IMSA_HILOGI("InputMethodAbilityExceptionTest testDelete Test START");
195 int32_t deleteForwardLenth = 1;
196 auto ret = inputMethodAbility_->DeleteForward(deleteForwardLenth);
197 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
198 int32_t deleteBackwardLenth = 2;
199 ret = inputMethodAbility_->DeleteBackward(deleteBackwardLenth);
200 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
201 }
202
203 /**
204 * @tc.name: testGetTextException001
205 * @tc.desc: InputMethodAbility GetTextBeforeCursor & GetTextAfterCursor & GetTextIndexAtCursor
206 * @tc.type: FUNC
207 * @tc.require:
208 * @tc.author: Hollokin
209 */
210 HWTEST_F(InputMethodAbilityExceptionTest, testGetTextException001, TestSize.Level0)
211 {
212 IMSA_HILOGI("InputMethodAbilityExceptionTest testGetText001 START");
213 std::u16string text;
214 auto ret = inputMethodAbility_->GetTextAfterCursor(8, text);
215 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
216 ret = inputMethodAbility_->GetTextBeforeCursor(3, text);
217 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
218 int32_t index;
219 ret = inputMethodAbility_->GetTextIndexAtCursor(index);
220 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
221 }
222
223 /**
224 * @tc.name: testGetEnterKeyTypeException
225 * @tc.desc: InputMethodAbility GetEnterKeyType & GetInputPattern
226 * @tc.type: FUNC
227 * @tc.require:
228 * @tc.author: Hollokin
229 */
230 HWTEST_F(InputMethodAbilityExceptionTest, testGetEnterKeyTypeException, TestSize.Level0)
231 {
232 IMSA_HILOGI("InputMethodAbilityExceptionTest testGetEnterKeyType START");
233 int32_t keyType2;
234 auto ret = inputMethodAbility_->GetEnterKeyType(keyType2);
235 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
236 int32_t inputPattern;
237 ret = inputMethodAbility_->GetInputPattern(inputPattern);
238 EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
239 }
240
241 /**
242 * @tc.name: testDispatchKeyEventException
243 * @tc.desc: DispatchKeyEvent Exception
244 * @tc.type: FUNC
245 * @tc.require:
246 * @tc.author: chenyu
247 */
248 HWTEST_F(InputMethodAbilityExceptionTest, testDispatchKeyEventException, TestSize.Level0)
249 {
250 IMSA_HILOGI("InputMethodAbilityExceptionTest DispatchKeyEvent START");
251 auto keyEvent = KeyEventUtil::CreateKeyEvent(MMI::KeyEvent::KEYCODE_A, MMI::KeyEvent::KEY_ACTION_DOWN);
252 auto ret = inputMethodAbility_->DispatchKeyEvent(keyEvent);
253 EXPECT_FALSE(ret);
254 }
255 } // namespace MiscServices
256 } // namespace OHOS
257