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