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 #include "i_input_method_agent.h"
22 #include "input_data_channel_stub.h"
23 #include "input_method_agent_stub.h"
24 #include "input_method_engine_listener_impl.h"
25 #include "key_event_util.h"
26 
27 using namespace testing::ext;
28 namespace OHOS {
29 namespace MiscServices {
30 const std::u16string AGENTSTUB_INTERFACE_TOKEN = u"ohos.miscservices.inputmethod.IInputMethodAgent";
31 class InputMethodAbilityExceptionTest : public testing::Test {
32 public:
SetUpTestCase(void)33     static void SetUpTestCase(void)
34     {
35         IMSA_HILOGI("InputMethodAbilityExceptionTest::SetUpTestCase");
36         inputMethodAbility_ = InputMethodAbility::GetInstance();
37     }
TearDownTestCase(void)38     static void TearDownTestCase(void)
39     {
40         IMSA_HILOGI("InputMethodAbilityExceptionTest::TearDownTestCase");
41     }
SetUp()42     void SetUp()
43     {
44     }
TearDown()45     void TearDown()
46     {
47     }
ResetMemberVar()48     static void ResetMemberVar()
49     {
50         inputMethodAbility_->dataChannelProxy_ = nullptr;
51         inputMethodAbility_->dataChannelObject_ = nullptr;
52         inputMethodAbility_->imeListener_ = nullptr;
53         inputMethodAbility_->panels_.Clear();
54     }
55     static sptr<InputMethodAbility> inputMethodAbility_;
56 };
57 sptr<InputMethodAbility> InputMethodAbilityExceptionTest::inputMethodAbility_;
58 
59 /**
60  * @tc.name: testMoveCursorException
61  * @tc.desc: InputMethodAbility MoveCursor
62  * @tc.type: FUNC
63  * @tc.require:
64  * @tc.author: Hollokin
65  */
66 HWTEST_F(InputMethodAbilityExceptionTest, testMoveCursorException, TestSize.Level0)
67 {
68     IMSA_HILOGI("InputMethodAbilityExceptionTest MoveCursor Test START");
69     auto ret = inputMethodAbility_->MoveCursor(4); // move cursor right
70     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
71 }
72 
73 /**
74  * @tc.name: testInsertTextException
75  * @tc.desc: InputMethodAbility InsertText
76  * @tc.type: FUNC
77  * @tc.require:
78  * @tc.author: Hollokin
79  */
80 HWTEST_F(InputMethodAbilityExceptionTest, testInsertTextException, TestSize.Level0)
81 {
82     IMSA_HILOGI("InputMethodAbilityExceptionTest InsertText Test START");
83     auto ret = inputMethodAbility_->InsertText("text");
84     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
85 }
86 
87 /**
88  * @tc.name: testSendFunctionKeyException
89  * @tc.desc: InputMethodAbility SendFunctionKey
90  * @tc.type: FUNC
91  * @tc.require:
92  * @tc.author: Hollokin
93  */
94 HWTEST_F(InputMethodAbilityExceptionTest, testSendFunctionKeyException, TestSize.Level0)
95 {
96     IMSA_HILOGI("InputMethodAbilityExceptionTest SendFunctionKey Test START");
97     auto ret = inputMethodAbility_->SendFunctionKey(0);
98     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
99 }
100 
101 /**
102  * @tc.name: testSendExtendActionException
103  * @tc.desc: InputMethodAbility SendExtendAction
104  * @tc.type: FUNC
105  * @tc.require:
106  * @tc.author: chenyu
107  */
108 HWTEST_F(InputMethodAbilityExceptionTest, testSendExtendActionException, TestSize.Level0)
109 {
110     IMSA_HILOGI("InputMethodAbilityExceptionTest SendExtendAction Test START");
111     constexpr int32_t action = 1;
112     auto ret = inputMethodAbility_->SendExtendAction(action);
113     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
114 }
115 
116 /**
117  * @tc.name: testSelectByRangeException
118  * @tc.desc: InputMethodAbility SelectByRange
119  * @tc.type: FUNC
120  * @tc.require:
121  * @tc.author: chenyu
122  */
123 HWTEST_F(InputMethodAbilityExceptionTest, testSelectByRangeException, TestSize.Level0)
124 {
125     IMSA_HILOGI("InputMethodAbilityExceptionTest testSelectByRange START");
126     // start < 0, end < 0
127     int32_t start = -1;
128     int32_t end = -2;
129     auto ret = inputMethodAbility_->SelectByRange(start, end);
130     EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED);
131     // start < 0, end >0
132     start = -1;
133     end = 2;
134     ret = inputMethodAbility_->SelectByRange(start, end);
135     EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED);
136     //end < 0, start > 0
137     start = 1;
138     end = -2;
139     ret = inputMethodAbility_->SelectByRange(start, end);
140     EXPECT_EQ(ret, ErrorCode::ERROR_PARAMETER_CHECK_FAILED);
141     // dataChannel == nullptr
142     start = 1;
143     end = 2;
144     ret = inputMethodAbility_->SelectByRange(start, end);
145     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
146 }
147 
148 /**
149  * @tc.name: testSelectByMovementException
150  * @tc.desc: InputMethodAbility SelectByMovement
151  * @tc.type: FUNC
152  * @tc.require:
153  * @tc.author: chenyu
154  */
155 HWTEST_F(InputMethodAbilityExceptionTest, testSelectByMovementException, TestSize.Level0)
156 {
157     IMSA_HILOGI("InputMethodAbilityExceptionTest testSelectByMovement START");
158     constexpr int32_t direction = 1;
159     auto ret = inputMethodAbility_->SelectByMovement(direction);
160     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
161 }
162 
163 /**
164  * @tc.name: testDeleteExceptionText
165  * @tc.desc: InputMethodAbility DeleteForward & DeleteBackward
166  * @tc.type: FUNC
167  * @tc.require:
168  * @tc.author: Hollokin
169  */
170 HWTEST_F(InputMethodAbilityExceptionTest, testDeleteExceptionText, TestSize.Level0)
171 {
172     IMSA_HILOGI("InputMethodAbilityExceptionTest testDelete Test START");
173     int32_t deleteForwardLenth = 1;
174     auto ret = inputMethodAbility_->DeleteForward(deleteForwardLenth);
175     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
176     int32_t deleteBackwardLenth = 2;
177     ret = inputMethodAbility_->DeleteBackward(deleteBackwardLenth);
178     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
179 }
180 
181 /**
182  * @tc.name: testGetTextException001
183  * @tc.desc: InputMethodAbility GetTextBeforeCursor & GetTextAfterCursor & GetTextIndexAtCursor
184  * @tc.type: FUNC
185  * @tc.require:
186  * @tc.author: Hollokin
187  */
188 HWTEST_F(InputMethodAbilityExceptionTest, testGetTextException001, TestSize.Level0)
189 {
190     IMSA_HILOGI("InputMethodAbilityExceptionTest testGetText001 START");
191     std::u16string text;
192     auto ret = inputMethodAbility_->GetTextAfterCursor(8, text);
193     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
194     ret = inputMethodAbility_->GetTextBeforeCursor(3, text);
195     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
196     int32_t index;
197     ret = inputMethodAbility_->GetTextIndexAtCursor(index);
198     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
199 }
200 
201 /**
202  * @tc.name: testGetEnterKeyTypeException
203  * @tc.desc: InputMethodAbility GetEnterKeyType & GetInputPattern
204  * @tc.type: FUNC
205  * @tc.require:
206  * @tc.author: Hollokin
207  */
208 HWTEST_F(InputMethodAbilityExceptionTest, testGetEnterKeyTypeException, TestSize.Level0)
209 {
210     IMSA_HILOGI("InputMethodAbilityExceptionTest testGetEnterKeyType START");
211     int32_t keyType2;
212     auto ret = inputMethodAbility_->GetEnterKeyType(keyType2);
213     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
214     int32_t inputPattern;
215     ret = inputMethodAbility_->GetInputPattern(inputPattern);
216     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
217 }
218 
219 /**
220  * @tc.name: testDispatchKeyEventException
221  * @tc.desc: DispatchKeyEvent Exception
222  * @tc.type: FUNC
223  * @tc.require:
224  * @tc.author: chenyu
225  */
226 HWTEST_F(InputMethodAbilityExceptionTest, testDispatchKeyEventException, TestSize.Level0)
227 {
228     IMSA_HILOGI("InputMethodAbilityExceptionTest DispatchKeyEvent START");
229     // keyEvent == nullptr;
230     std::shared_ptr<MMI::KeyEvent> keyEvent = nullptr;
231     sptr<KeyEventConsumerProxy> consumer = new (std::nothrow) KeyEventConsumerProxy(nullptr);
232     auto ret = inputMethodAbility_->DispatchKeyEvent(keyEvent, consumer);
233     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
234 
235     // kdListener_ == nullptr
236     keyEvent = KeyEventUtil::CreateKeyEvent(MMI::KeyEvent::KEYCODE_A, MMI::KeyEvent::KEY_ACTION_DOWN);
237     ret = inputMethodAbility_->DispatchKeyEvent(keyEvent, consumer);
238     EXPECT_EQ(ret, ErrorCode::ERROR_CLIENT_NULL_POINTER);
239 }
240 
241 /**
242  * @tc.name: testShowKeyboard_001
243  * @tc.desc: ShowKeyboard Exception
244  * @tc.type: FUNC
245  * @tc.require:
246  * @tc.author: chenyu
247  */
248 HWTEST_F(InputMethodAbilityExceptionTest, testShowKeyboard_001, TestSize.Level0)
249 {
250     IMSA_HILOGI("InputMethodAbilityExceptionTest testShowKeyboard_001 START");
251     // channelObject == nullptr
252     auto ret = inputMethodAbility_->ShowKeyboard();
253     EXPECT_EQ(ret, ErrorCode::ERROR_IME);
254 
255     ResetMemberVar();
256 }
257 
258 /**
259  * @tc.name: testShowKeyboard_002
260  * @tc.desc: ShowKeyBoard Exception
261  * @tc.type: FUNC
262  * @tc.require:
263  * @tc.author: chenyu
264  */
265 HWTEST_F(InputMethodAbilityExceptionTest, testShowKeyboard_002, TestSize.Level0)
266 {
267     IMSA_HILOGI("InputMethodAbilityExceptionTest testShowKeyboard_002 START");
268     // imeListener_ == nullptr
269     auto ret = inputMethodAbility_->ShowKeyboard();
270     EXPECT_EQ(ret, ErrorCode::ERROR_IME);
271 
272     auto imeListener = std::make_shared<InputMethodEngineListenerImpl>();
273     inputMethodAbility_->SetImeListener(imeListener);
274     sptr<InputDataChannelStub> channelObject = new InputDataChannelStub();
275     inputMethodAbility_->SetInputDataChannel(channelObject->AsObject());
276     // panel exist, PanelFlag == FLG_CANDIDATE_COLUMN
277     auto panel = std::make_shared<InputMethodPanel>();
278     panel->panelFlag_ = FLG_CANDIDATE_COLUMN;
279     panel->windowId_ = 2;
280     inputMethodAbility_->panels_.Insert(SOFT_KEYBOARD, panel);
281     ret = inputMethodAbility_->ShowKeyboard();
282     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
283     // panel not exist
284     inputMethodAbility_->panels_.Clear();
285     ret = inputMethodAbility_->ShowKeyboard();
286     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
287 
288     ResetMemberVar();
289 }
290 
291 /**
292  * @tc.name: testHideKeyboard_001
293  * @tc.desc: HideKeyboard Exception
294  * @tc.type: FUNC
295  * @tc.require:
296  * @tc.author: chenyu
297  */
298 HWTEST_F(InputMethodAbilityExceptionTest, testHideKeyboard_001, TestSize.Level0)
299 {
300     IMSA_HILOGI("InputMethodAbilityExceptionTest testHideKeyboard_001 START");
301     // imeListener_ == nullptr
302     auto ret = inputMethodAbility_->HideKeyboard();
303     EXPECT_EQ(ret, ErrorCode::ERROR_IME);
304 
305     // panel exist, PanelFlag == FLG_CANDIDATE_COLUMN
306     auto imeListener = std::make_shared<InputMethodEngineListenerImpl>();
307     inputMethodAbility_->SetImeListener(imeListener);
308     sptr<InputDataChannelStub> channelObject = new InputDataChannelStub();
309     inputMethodAbility_->SetInputDataChannel(channelObject->AsObject());
310     auto panel = std::make_shared<InputMethodPanel>();
311     panel->panelFlag_ = FLG_CANDIDATE_COLUMN;
312     panel->windowId_ = 2;
313     inputMethodAbility_->panels_.Insert(SOFT_KEYBOARD, panel);
314     ret = inputMethodAbility_->HideKeyboard();
315     EXPECT_EQ(ret, ErrorCode::NO_ERROR);
316 
317     // ShowPanel failed
318     inputMethodAbility_->panels_.Clear();
319     panel->panelFlag_ = FLG_FIXED;
320     inputMethodAbility_->panels_.Insert(SOFT_KEYBOARD, panel);
321     ret = inputMethodAbility_->HideKeyboard();
322     EXPECT_EQ(ret, ErrorCode::ERROR_NULL_POINTER);
323 
324     ResetMemberVar();
325 }
326 
327 /**
328  * @tc.name: testDispatchKeyEvent_001
329  * @tc.desc: DispatchKeyEvent Exception
330  * @tc.type: FUNC
331  * @tc.require:
332  * @tc.author: mashaoyin
333  */
334 HWTEST_F(InputMethodAbilityExceptionTest, testDispatchKeyEvent_001, TestSize.Level0)
335 {
336     IMSA_HILOGI("InputMethodAbilityExceptionTest testDispatchKeyEvent_001 START");
337     sptr<InputMethodAgentStub> agentStub = new InputMethodAgentStub();
338     MessageParcel data;
339     data.WriteInterfaceToken(AGENTSTUB_INTERFACE_TOKEN);
340     MessageParcel reply;
341     MessageOption option;
342     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
343     keyEvent->WriteToParcel(data);
344     data.WriteRemoteObject(nullptr);
345     auto ret =
346         agentStub->OnRemoteRequest(static_cast<uint32_t>(IInputMethodAgent::DISPATCH_KEY_EVENT), data, reply, option);
347     EXPECT_EQ(ret, ErrorCode::ERROR_EX_PARCELABLE);
348 }
349 } // namespace MiscServices
350 } // namespace OHOS
351