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