1 /*
2 * Copyright (c) 2024 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 <cstdio>
17 #include <gtest/gtest.h>
18
19 #include "define_multimodal.h"
20 #include "general_touchscreen.h"
21 #include "input_device_manager.h"
22 #include "i_input_windows_manager.h"
23 #include "libinput-private.h"
24 #include "libinput_wrapper.h"
25 #include "touch_transform_processor.h"
26
27 namespace OHOS {
28 namespace MMI {
29 namespace {
30 using namespace testing::ext;
31 } // namespace
32
33 class TouchTransformProcessorTest : public testing::Test {
34 public:
35 static void SetUpTestCase(void);
36 static void TearDownTestCase(void);
37 void SetUp();
38 void TearDown();
39
40 private:
41 static void SetupTouchscreen();
42 static void CloseTouchscreen();
43 static GeneralTouchscreen vTouchscreen_;
44 static LibinputWrapper libinput_;
45 };
46
47 GeneralTouchscreen TouchTransformProcessorTest::vTouchscreen_;
48 LibinputWrapper TouchTransformProcessorTest::libinput_;
49
SetUpTestCase(void)50 void TouchTransformProcessorTest::SetUpTestCase(void)
51 {
52 ASSERT_TRUE(libinput_.Init());
53 SetupTouchscreen();
54 }
55
TearDownTestCase(void)56 void TouchTransformProcessorTest::TearDownTestCase(void)
57 {
58 CloseTouchscreen();
59 }
60
SetupTouchscreen()61 void TouchTransformProcessorTest::SetupTouchscreen()
62 {
63 ASSERT_TRUE(vTouchscreen_.SetUp());
64 std::cout << "device node name: " << vTouchscreen_.GetDevPath() << std::endl;
65 ASSERT_TRUE(libinput_.AddPath(vTouchscreen_.GetDevPath()));
66 libinput_event *event = libinput_.Dispatch();
67 ASSERT_TRUE(event != nullptr);
68 ASSERT_EQ(libinput_event_get_type(event), LIBINPUT_EVENT_DEVICE_ADDED);
69 struct libinput_device *device = libinput_event_get_device(event);
70 ASSERT_TRUE(device != nullptr);
71 }
72
CloseTouchscreen()73 void TouchTransformProcessorTest::CloseTouchscreen()
74 {
75 libinput_.RemovePath(vTouchscreen_.GetDevPath());
76 vTouchscreen_.Close();
77 }
78
SetUp()79 void TouchTransformProcessorTest::SetUp()
80 {
81 }
82
TearDown()83 void TouchTransformProcessorTest::TearDown()
84 {
85 }
86
87 /**
88 * @tc.name: TouchTransformProcessorTest_OnEventTouchDown_001
89 * @tc.desc: Test the funcation OnEventTouchDown
90 * @tc.type: FUNC
91 * @tc.require:
92 */
93 HWTEST_F(TouchTransformProcessorTest, OnEventTouchDown_001, TestSize.Level1)
94 {
95 int32_t deviceId = 6;
96 TouchTransformProcessor processor(deviceId);
97 libinput_event* event = nullptr;
98 bool ret = processor.OnEventTouchDown(event);
99 ASSERT_FALSE(ret);
100 }
101
102 /**
103 * @tc.name: TouchTransformProcessorTest_UpdatePointerItemProperties_001
104 * @tc.desc: Test the funcation UpdatePointerItemProperties
105 * @tc.type: FUNC
106 * @tc.require:
107 */
108 HWTEST_F(TouchTransformProcessorTest, UpdatePointerItemProperties_001, TestSize.Level1)
109 {
110 PointerEvent::PointerItem item;
111 EventTouch touchInfo;
112 touchInfo.point.x = 10;
113 touchInfo.point.y = 20;
114 touchInfo.toolRect.point.x = 30;
115 touchInfo.toolRect.point.y = 40;
116 touchInfo.toolRect.width = 50;
117 touchInfo.toolRect.height = 60;
118 int32_t deviceId = 6;
119 TouchTransformProcessor processor(deviceId);
120 processor.UpdatePointerItemProperties(item, touchInfo);
121 ASSERT_EQ(item.GetDisplayX(), touchInfo.point.x);
122 ASSERT_EQ(item.GetDisplayY(), touchInfo.point.y);
123 ASSERT_EQ(item.GetDisplayXPos(), touchInfo.point.x);
124 ASSERT_EQ(item.GetDisplayYPos(), touchInfo.point.y);
125 ASSERT_EQ(item.GetRawDisplayX(), touchInfo.point.x);
126 ASSERT_EQ(item.GetRawDisplayY(), touchInfo.point.y);
127 ASSERT_EQ(item.GetToolDisplayX(), touchInfo.toolRect.point.x);
128 ASSERT_EQ(item.GetToolDisplayY(), touchInfo.toolRect.point.y);
129 ASSERT_EQ(item.GetToolWidth(), touchInfo.toolRect.width);
130 ASSERT_EQ(item.GetToolHeight(), touchInfo.toolRect.height);
131 }
132
133 /**
134 * @tc.name: TouchTransformProcessorTest_OnEventTouchMotion_001
135 * @tc.desc: Test the funcation OnEventTouchMotion
136 * @tc.type: FUNC
137 * @tc.require:
138 */
139 HWTEST_F(TouchTransformProcessorTest, OnEventTouchMotion_001, TestSize.Level1)
140 {
141 int32_t deviceId = 6;
142 TouchTransformProcessor processor(deviceId);
143 libinput_event* event = nullptr;
144 bool ret = processor.OnEventTouchMotion(event);
145 ASSERT_FALSE(ret);
146 }
147
148 /**
149 * @tc.name: TouchTransformProcessorTest_OnEventTouchUp_001
150 * @tc.desc: Test the funcation OnEventTouchUp
151 * @tc.type: FUNC
152 * @tc.require:
153 */
154 HWTEST_F(TouchTransformProcessorTest, OnEventTouchUp_001, TestSize.Level1)
155 {
156 int32_t deviceId = 6;
157 TouchTransformProcessor processor(deviceId);
158 libinput_event* event = nullptr;
159 bool ret = processor.OnEventTouchUp(event);
160 ASSERT_FALSE(ret);
161 }
162
163 /**
164 * @tc.name: TouchTransformProcessorTest_GetTouchToolType_001
165 * @tc.desc: Test the funcation GetTouchToolType
166 * @tc.type: FUNC
167 * @tc.require:
168 */
169 HWTEST_F(TouchTransformProcessorTest, GetTouchToolType_001, TestSize.Level1)
170 {
171 int32_t deviceId = 6;
172 TouchTransformProcessor processor(deviceId);
173 struct libinput_device *device = nullptr;
174 int32_t toolType = processor.GetTouchToolType(device);
175 ASSERT_EQ(toolType, PointerEvent::TOOL_TYPE_FINGER);
176 }
177
178 /**
179 * @tc.name: TouchTransformProcessorTest_InitToolTypes_001
180 * @tc.desc: Test the funcation InitToolTypes
181 * @tc.type: FUNC
182 * @tc.require:
183 */
184 HWTEST_F(TouchTransformProcessorTest, InitToolTypes_001, TestSize.Level1)
185 {
186 int32_t deviceId = 6;
187 TouchTransformProcessor processor(deviceId);
188 processor.InitToolTypes();
189 ASSERT_EQ(processor.vecToolType_.size(), 16);
190 ASSERT_EQ(processor.vecToolType_[0].first, BTN_TOOL_PEN);
191 ASSERT_EQ(processor.vecToolType_[0].second, PointerEvent::TOOL_TYPE_PEN);
192 ASSERT_EQ(processor.vecToolType_[1].first, BTN_TOOL_RUBBER);
193 ASSERT_EQ(processor.vecToolType_[1].second, PointerEvent::TOOL_TYPE_RUBBER);
194 ASSERT_EQ(processor.vecToolType_[2].first, BTN_TOOL_BRUSH);
195 ASSERT_EQ(processor.vecToolType_[2].second, PointerEvent::TOOL_TYPE_BRUSH);
196 ASSERT_EQ(processor.vecToolType_[3].first, BTN_TOOL_PENCIL);
197 ASSERT_EQ(processor.vecToolType_[3].second, PointerEvent::TOOL_TYPE_PENCIL);
198 ASSERT_EQ(processor.vecToolType_[4].first, BTN_TOOL_AIRBRUSH);
199 ASSERT_EQ(processor.vecToolType_[4].second, PointerEvent::TOOL_TYPE_AIRBRUSH);
200 ASSERT_EQ(processor.vecToolType_[5].first, BTN_TOOL_FINGER);
201 ASSERT_EQ(processor.vecToolType_[5].second, PointerEvent::TOOL_TYPE_FINGER);
202 ASSERT_EQ(processor.vecToolType_[6].first, BTN_TOOL_MOUSE);
203 ASSERT_EQ(processor.vecToolType_[6].second, PointerEvent::TOOL_TYPE_MOUSE);
204 ASSERT_EQ(processor.vecToolType_[7].first, BTN_TOOL_LENS);
205 ASSERT_EQ(processor.vecToolType_[7].second, PointerEvent::TOOL_TYPE_LENS);
206 }
207
208 /**
209 * @tc.name: TouchTransformProcessorTest_OnEventTouchDown_002
210 * @tc.desc: Test the funcation OnEventTouchDown
211 * @tc.type: FUNC
212 * @tc.require:
213 */
214 HWTEST_F(TouchTransformProcessorTest, OnEventTouchDown_002, TestSize.Level1)
215 {
216 CALL_TEST_DEBUG;
217 int32_t deviceId = 6;
218 TouchTransformProcessor processor(deviceId);
219 libinput_event* event = nullptr;
220 bool ret = processor.OnEventTouchDown(event);
221 ASSERT_FALSE(ret);
222 processor.pointerEvent_ = PointerEvent::Create();
223 ASSERT_TRUE(processor.pointerEvent_ != nullptr);
224 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TRACKING_ID, 0);
225 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_POSITION_X, 5190);
226 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_POSITION_Y, 8306);
227 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_PRESSURE, 321);
228 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TOUCH_MAJOR, 198);
229 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TOUCH_MINOR, 180);
230 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_ORIENTATION, -64);
231 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_BLOB_ID, 2);
232 vTouchscreen_.SendEvent(EV_SYN, SYN_MT_REPORT, 0);
233 vTouchscreen_.SendEvent(EV_KEY, BTN_TOUCH, 1);
234 vTouchscreen_.SendEvent(EV_SYN, SYN_REPORT, 0);
235 event = libinput_.Dispatch();
236 ASSERT_TRUE(event != nullptr);
237 auto touch = libinput_event_get_touch_event(event);
238 ASSERT_TRUE(touch != nullptr);
239 auto device = libinput_event_get_device(event);
240 ASSERT_TRUE(device != nullptr);
241 ret = processor.OnEventTouchDown(event);
242 ASSERT_FALSE(ret);
243 }
244
245 /**
246 * @tc.name: TouchTransformProcessorTest_NotifyFingersenseProcess_001
247 * @tc.desc: Test the funcation NotifyFingersenseProcess
248 * @tc.type: FUNC
249 * @tc.require:
250 */
251 HWTEST_F(TouchTransformProcessorTest, NotifyFingersenseProcess_001, TestSize.Level1)
252 {
253 CALL_TEST_DEBUG;
254 int32_t deviceId = 6;
255 TouchTransformProcessor processor(deviceId);
256 PointerEvent::PointerItem item;
257 int32_t toolType = 0;
258 EXPECT_NO_FATAL_FAILURE(processor.NotifyFingersenseProcess(item, toolType));
259 }
260
261 /**
262 * @tc.name: TouchTransformProcessorTest_OnEventTouchMotion_002
263 * @tc.desc: Test the funcation OnEventTouchMotion
264 * @tc.type: FUNC
265 * @tc.require:
266 */
267 HWTEST_F(TouchTransformProcessorTest, OnEventTouchMotion_002, TestSize.Level1)
268 {
269 CALL_TEST_DEBUG;
270 int32_t deviceId = 6;
271 TouchTransformProcessor processor(deviceId);
272 libinput_event* event = nullptr;
273 bool ret = processor.OnEventTouchMotion(event);
274 ASSERT_FALSE(ret);
275 processor.pointerEvent_ = PointerEvent::Create();
276 ASSERT_TRUE(processor.pointerEvent_ != nullptr);
277 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_POSITION_X, 5199);
278 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_POSITION_Y, 8297);
279 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_PRESSURE, 356);
280 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TRACKING_ID, 0);
281 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TOUCH_MAJOR, 216);
282 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TOUCH_MINOR, 162);
283 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_ORIENTATION, -79);
284 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_BLOB_ID, 2);
285 vTouchscreen_.SendEvent(EV_SYN, SYN_MT_REPORT, 0);
286 vTouchscreen_.SendEvent(EV_SYN, SYN_REPORT, 0);
287 event = libinput_.Dispatch();
288 ASSERT_TRUE(event != nullptr);
289 auto touch = libinput_event_get_touch_event(event);
290 ASSERT_TRUE(touch != nullptr);
291 EXPECT_NO_FATAL_FAILURE(processor.OnEventTouchMotion(event));
292 }
293
294 /**
295 * @tc.name: TouchTransformProcessorTest_OnEventTouchUp_002
296 * @tc.desc: Test the funcation OnEventTouchUp
297 * @tc.type: FUNC
298 * @tc.require:
299 */
300 HWTEST_F(TouchTransformProcessorTest, OnEventTouchUp_002, TestSize.Level1)
301 {
302 CALL_TEST_DEBUG;
303 int32_t deviceId = 6;
304 TouchTransformProcessor processor(deviceId);
305 libinput_event* event = nullptr;
306 bool ret = processor.OnEventTouchUp(event);
307 ASSERT_FALSE(ret);
308 processor.pointerEvent_ = PointerEvent::Create();
309 ASSERT_TRUE(processor.pointerEvent_ != nullptr);
310 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_POSITION_X, 6486);
311 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_POSITION_Y, 7289);
312 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_PRESSURE, 313);
313 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TRACKING_ID, 0);
314 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TOUCH_MAJOR, 198);
315 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TOUCH_MINOR, 180);
316 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_ORIENTATION, -58);
317 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_BLOB_ID, 2);
318 vTouchscreen_.SendEvent(EV_SYN, SYN_MT_REPORT, 0);
319 vTouchscreen_.SendEvent(EV_KEY, BTN_TOUCH, 0);
320 vTouchscreen_.SendEvent(EV_SYN, SYN_REPORT, 0);
321 event = libinput_.Dispatch();
322 ASSERT_TRUE(event != nullptr);
323 auto touch = libinput_event_get_touch_event(event);
324 ASSERT_TRUE(touch != nullptr);
325 EXPECT_NO_FATAL_FAILURE(processor.OnEventTouchUp(event));
326 }
327
328 /**
329 * @tc.name: TouchTransformProcessorTest_OnEvent_001
330 * @tc.desc: Test OnEvent
331 * @tc.type: FUNC
332 * @tc.require:
333 */
334 HWTEST_F(TouchTransformProcessorTest, TouchTransformProcessorTest_OnEvent_001, TestSize.Level1)
335 {
336 CALL_TEST_DEBUG;
337 int32_t deviceId = 6;
338 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TRACKING_ID, 0);
339 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_POSITION_X, 5190);
340 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_POSITION_Y, 8306);
341 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_PRESSURE, 321);
342 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TOUCH_MAJOR, 198);
343 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TOUCH_MINOR, 180);
344 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_ORIENTATION, -64);
345 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_BLOB_ID, 2);
346 vTouchscreen_.SendEvent(EV_SYN, SYN_MT_REPORT, 0);
347 vTouchscreen_.SendEvent(EV_KEY, BTN_TOUCH, 1);
348 vTouchscreen_.SendEvent(EV_SYN, SYN_REPORT, 0);
349 libinput_event *event = libinput_.Dispatch();
350 ASSERT_TRUE(event != nullptr);
351 struct libinput_device *dev = libinput_event_get_device(event);
352 ASSERT_TRUE(dev != nullptr);
353 std::cout << "pointer device: " << libinput_device_get_name(dev) << std::endl;
354 TouchTransformProcessor processor(deviceId);
355 processor.pointerEvent_ = nullptr;
356 auto ret = processor.OnEvent(event);
357 ASSERT_FALSE(ret);
358 processor.pointerEvent_ = PointerEvent::Create();
359 ASSERT_TRUE(processor.pointerEvent_ != nullptr);
360 EXPECT_NO_FATAL_FAILURE(processor.OnEvent(event));
361 }
362
363 /**
364 * @tc.name: TouchTransformProcessorTest_OnEvent_002
365 * @tc.desc: Test OnEvent
366 * @tc.type: FUNC
367 * @tc.require:
368 */
369 HWTEST_F(TouchTransformProcessorTest, TouchTransformProcessorTest_OnEvent_002, TestSize.Level1)
370 {
371 CALL_TEST_DEBUG;
372 int32_t deviceId = 6;
373 TouchTransformProcessor processor(deviceId);
374 processor.pointerEvent_ = PointerEvent::Create();
375 ASSERT_TRUE(processor.pointerEvent_ != nullptr);
376 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_POSITION_X, 5199);
377 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_POSITION_Y, 8297);
378 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_PRESSURE, 356);
379 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TRACKING_ID, 0);
380 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TOUCH_MAJOR, 216);
381 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TOUCH_MINOR, 162);
382 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_ORIENTATION, -79);
383 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_BLOB_ID, 2);
384 vTouchscreen_.SendEvent(EV_SYN, SYN_MT_REPORT, 0);
385 vTouchscreen_.SendEvent(EV_SYN, SYN_REPORT, 0);
386 libinput_event *event = libinput_.Dispatch();
387 ASSERT_TRUE(event != nullptr);
388 struct libinput_device *dev = libinput_event_get_device(event);
389 ASSERT_TRUE(dev != nullptr);
390 std::cout << "pointer device: " << libinput_device_get_name(dev) << std::endl;
391 EXPECT_NO_FATAL_FAILURE(processor.OnEvent(event));
392 }
393
394 /**
395 * @tc.name: TouchTransformProcessorTest_OnEvent_003
396 * @tc.desc: Test OnEvent
397 * @tc.type: FUNC
398 * @tc.require:
399 */
400 HWTEST_F(TouchTransformProcessorTest, TouchTransformProcessorTest_OnEvent_003, TestSize.Level1)
401 {
402 CALL_TEST_DEBUG;
403 int32_t deviceId = 6;
404 TouchTransformProcessor processor(deviceId);
405 processor.pointerEvent_ = PointerEvent::Create();
406 ASSERT_TRUE(processor.pointerEvent_ != nullptr);
407 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_POSITION_X, 6486);
408 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_POSITION_Y, 7289);
409 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_PRESSURE, 313);
410 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TRACKING_ID, 0);
411 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TOUCH_MAJOR, 198);
412 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_TOUCH_MINOR, 180);
413 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_ORIENTATION, -58);
414 vTouchscreen_.SendEvent(EV_ABS, ABS_MT_BLOB_ID, 2);
415 vTouchscreen_.SendEvent(EV_SYN, SYN_MT_REPORT, 0);
416 vTouchscreen_.SendEvent(EV_KEY, BTN_TOUCH, 0);
417 vTouchscreen_.SendEvent(EV_SYN, SYN_REPORT, 0);
418 libinput_event *event = libinput_.Dispatch();
419 ASSERT_TRUE(event != nullptr);
420 struct libinput_device *dev = libinput_event_get_device(event);
421 ASSERT_TRUE(dev != nullptr);
422 std::cout << "pointer device: " << libinput_device_get_name(dev) << std::endl;
423 EXPECT_NO_FATAL_FAILURE(processor.OnEvent(event));
424 }
425 } // namespace MMI
426 } // namespace OHOS