• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
17 #include <sys/time.h>
18 
19 #include <cstdint>
20 #include <functional>
21 #include <string>
22 #include <thread>
23 #include <vector>
24 
25 #include "global.h"
26 #include "im_common_event_manager.h"
27 #include "input_manager.h"
28 #include "input_method_controller.h"
29 #include "key_event.h"
30 #include "pointer_event.h"
31 
32 namespace OHOS {
33 namespace MiscServices {
34 using namespace testing::ext;
35 using namespace MMI;
36 namespace {
37 constexpr int32_t TIME_WAIT_FOR_STATUS_OK = 50;
38 constexpr int32_t TIME_WAIT_FOR_HANDLE_KEY_EVENT = 10000;
39 constexpr int32_t SEC_TO_NANOSEC = 1000000000;
40 constexpr int32_t NANOSECOND_TO_MILLISECOND = 1000000;
41 } // namespace
42 
43 class InputMethodServiceTest : public testing::Test {
44 public:
45     static void SetUpTestCase(void);
46     static void TearDownTestCase(void);
47     void SetUp();
48     void TearDown();
49 
50     static std::shared_ptr<MMI::KeyEvent> SetKeyEvent(int32_t keyCode, int32_t keyAction);
51     static bool SimulateKeyEvent(int32_t keyCode);
52     static bool SimulateCombinationKeyEvent(int32_t preKey, int32_t finalKey);
53 };
54 
55 class TextListener : public OnTextChangedListener {
56 public:
TextListener()57     TextListener()
58     {
59     }
~TextListener()60     ~TextListener()
61     {
62     }
InsertText(const std::u16string & text)63     void InsertText(const std::u16string &text)
64     {
65         IMSA_HILOGI("SERVICE TEST TextListener InsertText");
66     }
67 
DeleteBackward(int32_t length)68     void DeleteBackward(int32_t length)
69     {
70         IMSA_HILOGI("SERVICE TEST TextListener DeleteBackward length: %{public}d", length);
71     }
72 
SetKeyboardStatus(bool status)73     void SetKeyboardStatus(bool status)
74     {
75         IMSA_HILOGI("SERVICE TEST TextListener SetKeyboardStatus %{public}d", status);
76     }
DeleteForward(int32_t length)77     void DeleteForward(int32_t length)
78     {
79         IMSA_HILOGI("SERVICE TEST TextListener DeleteForward length: %{public}d", length);
80     }
SendKeyEventFromInputMethod(const KeyEvent & event)81     void SendKeyEventFromInputMethod(const KeyEvent &event)
82     {
83         IMSA_HILOGI("SERVICE TEST TextListener sendKeyEventFromInputMethod");
84     }
SendKeyboardInfo(const KeyboardInfo & status)85     void SendKeyboardInfo(const KeyboardInfo &status)
86     {
87         IMSA_HILOGI("SERVICE TEST TextListener SendKeyboardInfo");
88     }
MoveCursor(const Direction direction)89     void MoveCursor(const Direction direction)
90     {
91         IMSA_HILOGI("SERVICE TEST TextListener MoveCursor");
92     }
93 };
94 
GetNanoTime()95 int64_t GetNanoTime()
96 {
97     struct timespec time = { 0 };
98     clock_gettime(CLOCK_MONOTONIC, &time);
99     return static_cast<int64_t>(time.tv_sec) * SEC_TO_NANOSEC + time.tv_nsec;
100 }
101 
SetUpTestCase(void)102 void InputMethodServiceTest::SetUpTestCase(void)
103 {
104     IMSA_HILOGI("InputMethodServiceTest::SetUpTestCase");
105 }
106 
TearDownTestCase(void)107 void InputMethodServiceTest::TearDownTestCase(void)
108 {
109     IMSA_HILOGI("InputMethodServiceTest::TearDownTestCase");
110 }
111 
SetUp(void)112 void InputMethodServiceTest::SetUp(void)
113 {
114     IMSA_HILOGI("InputMethodServiceTest::SetUp");
115 }
116 
TearDown(void)117 void InputMethodServiceTest::TearDown(void)
118 {
119     IMSA_HILOGI("InputMethodServiceTest::TearDown");
120 }
121 
SetKeyEvent(int32_t keyCode,int32_t keyAction)122 std::shared_ptr<MMI::KeyEvent> InputMethodServiceTest::SetKeyEvent(int32_t keyCode, int32_t keyAction)
123 {
124     std::shared_ptr<MMI::KeyEvent> keyEvent = MMI::KeyEvent::Create();
125     int64_t downTime = GetNanoTime() / NANOSECOND_TO_MILLISECOND;
126     MMI::KeyEvent::KeyItem keyItem;
127     keyItem.SetKeyCode(keyCode);
128     keyItem.SetPressed(true);
129     keyItem.SetDownTime(downTime);
130     if (keyEvent != nullptr) {
131         keyEvent->SetKeyCode(keyCode);
132         keyEvent->SetKeyAction(keyAction);
133         keyEvent->AddPressedKeyItems(keyItem);
134     }
135     return keyEvent;
136 }
137 
SimulateKeyEvent(int32_t keyCode)138 bool InputMethodServiceTest::SimulateKeyEvent(int32_t keyCode)
139 {
140     auto keyDown = InputMethodServiceTest::SetKeyEvent(keyCode, MMI::KeyEvent::KEY_ACTION_DOWN);
141     auto keyUp = InputMethodServiceTest::SetKeyEvent(keyCode, MMI::KeyEvent::KEY_ACTION_UP);
142     bool result = keyDown != nullptr && keyUp != nullptr;
143     InputManager::GetInstance()->SimulateInputEvent(keyDown);
144     usleep(TIME_WAIT_FOR_STATUS_OK);
145     InputManager::GetInstance()->SimulateInputEvent(keyUp);
146     return result;
147 }
148 
SimulateCombinationKeyEvent(int32_t preKey,int32_t finalKey)149 bool InputMethodServiceTest::SimulateCombinationKeyEvent(int32_t preKey, int32_t finalKey)
150 {
151     auto preDown = InputMethodServiceTest::SetKeyEvent(preKey, MMI::KeyEvent::KEY_ACTION_DOWN);
152     auto finalDown = InputMethodServiceTest::SetKeyEvent(finalKey, MMI::KeyEvent::KEY_ACTION_DOWN);
153     auto preUp = InputMethodServiceTest::SetKeyEvent(finalKey, MMI::KeyEvent::KEY_ACTION_UP);
154     auto finalUp = InputMethodServiceTest::SetKeyEvent(preKey, MMI::KeyEvent::KEY_ACTION_UP);
155     InputManager::GetInstance()->SimulateInputEvent(preDown);
156     usleep(TIME_WAIT_FOR_STATUS_OK);
157     InputManager::GetInstance()->SimulateInputEvent(finalDown);
158     usleep(TIME_WAIT_FOR_STATUS_OK);
159     InputManager::GetInstance()->SimulateInputEvent(preUp);
160     usleep(TIME_WAIT_FOR_STATUS_OK);
161     InputManager::GetInstance()->SimulateInputEvent(finalUp);
162     bool result = preDown != nullptr && finalDown != nullptr && preUp != nullptr && finalUp != nullptr;
163     return result;
164 }
165 
166 /**
167  * @tc.name: test_KeyEvent_UNKNOWN_001
168  * @tc.desc: test KeyEvent Callback.
169  * @tc.type: FUNC
170  */
171 HWTEST_F(InputMethodServiceTest, test_KeyEvent_UNKNOWN_001, TestSize.Level0)
172 {
173     IMSA_HILOGI("test_KeyEvent_UNKNOWN_001 TEST START");
174     bool result = InputMethodServiceTest::SimulateKeyEvent(MMI::KeyEvent::KEYCODE_0);
175     EXPECT_TRUE(result);
176     usleep(TIME_WAIT_FOR_HANDLE_KEY_EVENT);
177 }
178 
179 /**
180  * @tc.name: test_KeyEvent_UNKNOWN_002
181  * @tc.desc: test KeyEvent Callback.
182  * @tc.type: FUNC
183  */
184 HWTEST_F(InputMethodServiceTest, test_KeyEvent_UNKNOWN_002, TestSize.Level0)
185 {
186     IMSA_HILOGI("test_KeyEvent_UNKNOWN_002 TEST START");
187     bool result =
188         InputMethodServiceTest::SimulateCombinationKeyEvent(MMI::KeyEvent::KEYCODE_0, MMI::KeyEvent::KEYCODE_1);
189     EXPECT_TRUE(result);
190     usleep(TIME_WAIT_FOR_HANDLE_KEY_EVENT);
191 }
192 
193 /**
194  * @tc.name: test_KeyEvent_CAPS_001
195  * @tc.desc: test KeyEvent Callback.
196  * @tc.type: FUNC
197  */
198 HWTEST_F(InputMethodServiceTest, test_KeyEvent_CAPS_001, TestSize.Level0)
199 {
200     IMSA_HILOGI("test_KeyEvent_CAPS_001 TEST START");
201     bool result = InputMethodServiceTest::SimulateKeyEvent(MMI::KeyEvent::KEYCODE_CAPS_LOCK);
202     EXPECT_TRUE(result);
203     usleep(TIME_WAIT_FOR_HANDLE_KEY_EVENT);
204 }
205 
206 /**
207  * @tc.name: test_KeyEvent_CTRL_001
208  * @tc.desc: test KeyEvent Callback.
209  * @tc.type: FUNC
210  */
211 HWTEST_F(InputMethodServiceTest, test_KeyEvent_CTRL_001, TestSize.Level0)
212 {
213     IMSA_HILOGI("test_KeyEvent_CTRL_001 TEST START");
214     bool result = InputMethodServiceTest::SimulateKeyEvent(MMI::KeyEvent::KEYCODE_CTRL_LEFT);
215     EXPECT_TRUE(result);
216     usleep(TIME_WAIT_FOR_HANDLE_KEY_EVENT);
217 }
218 
219 /**
220  * @tc.name: test_KeyEvent_CTRL_002
221  * @tc.desc: test KeyEvent Callback.
222  * @tc.type: FUNC
223  */
224 HWTEST_F(InputMethodServiceTest, test_KeyEvent_CTRL_002, TestSize.Level0)
225 {
226     IMSA_HILOGI("test_KeyEvent_CTRL_002 TEST START");
227     bool result = InputMethodServiceTest::SimulateKeyEvent(MMI::KeyEvent::KEYCODE_CTRL_RIGHT);
228     EXPECT_TRUE(result);
229     usleep(TIME_WAIT_FOR_HANDLE_KEY_EVENT);
230 }
231 
232 /**
233  * @tc.name: test_KeyEvent_SHIFT_001
234  * @tc.desc: test KeyEvent Callback.
235  * @tc.type: FUNC
236  */
237 HWTEST_F(InputMethodServiceTest, test_KeyEvent_SHIFT_001, TestSize.Level0)
238 {
239     IMSA_HILOGI("test_KeyEvent_SHIFT_001 TEST START");
240     bool result = InputMethodServiceTest::SimulateKeyEvent(MMI::KeyEvent::KEYCODE_SHIFT_LEFT);
241     EXPECT_TRUE(result);
242     usleep(TIME_WAIT_FOR_HANDLE_KEY_EVENT);
243 }
244 
245 /**
246  * @tc.name: test_KeyEvent_SHIFT_002
247  * @tc.desc: test KeyEvent Callback.
248  * @tc.type: FUNC
249  */
250 HWTEST_F(InputMethodServiceTest, test_KeyEvent_SHIFT_002, TestSize.Level0)
251 {
252     IMSA_HILOGI("test_KeyEvent_SHIFT_002 TEST START");
253     bool result = InputMethodServiceTest::SimulateKeyEvent(MMI::KeyEvent::KEYCODE_SHIFT_RIGHT);
254     EXPECT_TRUE(result);
255     usleep(TIME_WAIT_FOR_HANDLE_KEY_EVENT);
256 }
257 
258 /**
259  * @tc.name: test_KeyEvent_CTRL_SHIFT_001
260  * @tc.desc: test KeyEvent Callback.
261  * @tc.type: FUNC
262  */
263 HWTEST_F(InputMethodServiceTest, test_KeyEvent_CTRL_SHIFT_001, TestSize.Level0)
264 {
265     IMSA_HILOGI("test_KeyEvent_CTRL_SHIFT_001 TEST START");
266     bool result = InputMethodServiceTest::SimulateCombinationKeyEvent(
267         MMI::KeyEvent::KEYCODE_CTRL_LEFT, MMI::KeyEvent::KEYCODE_SHIFT_LEFT);
268     EXPECT_TRUE(result);
269     usleep(TIME_WAIT_FOR_HANDLE_KEY_EVENT);
270 }
271 
272 /**
273  * @tc.name: test_KeyEvent_CTRL_SHIFT_002
274  * @tc.desc: test KeyEvent Callback.
275  * @tc.type: FUNC
276  */
277 HWTEST_F(InputMethodServiceTest, test_KeyEvent_CTRL_SHIFT_002, TestSize.Level0)
278 {
279     IMSA_HILOGI("test_KeyEvent_CTRL_SHIFT_002 TEST START");
280     bool result = InputMethodServiceTest::SimulateCombinationKeyEvent(
281         MMI::KeyEvent::KEYCODE_CTRL_LEFT, MMI::KeyEvent::KEYCODE_SHIFT_RIGHT);
282     EXPECT_TRUE(result);
283     usleep(TIME_WAIT_FOR_HANDLE_KEY_EVENT);
284 }
285 
286 /**
287  * @tc.name: test_KeyEvent_CTRL_SHIFT_003
288  * @tc.desc: test KeyEvent Callback.
289  * @tc.type: FUNC
290  */
291 HWTEST_F(InputMethodServiceTest, test_KeyEvent_CTRL_SHIFT_003, TestSize.Level0)
292 {
293     IMSA_HILOGI("test_KeyEvent_CTRL_SHIFT_003 TEST START");
294     bool result = InputMethodServiceTest::SimulateCombinationKeyEvent(
295         MMI::KeyEvent::KEYCODE_CTRL_RIGHT, MMI::KeyEvent::KEYCODE_SHIFT_LEFT);
296     EXPECT_TRUE(result);
297     usleep(TIME_WAIT_FOR_HANDLE_KEY_EVENT);
298 }
299 
300 /**
301  * @tc.name: test_KeyEvent_CTRL_SHIFT_004
302  * @tc.desc: test KeyEvent Callback.
303  * @tc.type: FUNC
304  */
305 HWTEST_F(InputMethodServiceTest, test_KeyEvent_CTRL_SHIFT_004, TestSize.Level0)
306 {
307     IMSA_HILOGI("SubscribeKeyboardEvent007 TEST START");
308     bool result = InputMethodServiceTest::SimulateCombinationKeyEvent(
309         MMI::KeyEvent::KEYCODE_CTRL_RIGHT, MMI::KeyEvent::KEYCODE_SHIFT_RIGHT);
310     EXPECT_TRUE(result);
311 }
312 } // namespace MiscServices
313 } // namespace OHOS