1 /*
2  * Copyright (c) 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 <cstdio>
17 #include <gtest/gtest.h>
18 
19 #include "define_multimodal.h"
20 #include "joystick_transform_processor.h"
21 #include "touch_transform_processor.h"
22 #include "touch_event_normalize.h"
23 
24 #undef MMI_LOG_TAG
25 #define MMI_LOG_TAG "TouchEventNormalizeTest"
26 
27 namespace OHOS {
28 namespace MMI {
29 namespace {
30 using namespace testing::ext;
31 constexpr int32_t LIBINPUT_HOMEPAGE_BUTTON_CODE = 172;
32 constexpr int32_t LIBINPUT_BUTTON_NONE = -1;
33 } // namespace
34 class TouchEventNormalizeTest : public testing::Test {
35 public:
36     void SetUp();
37     void TearDown();
38 
39 private:
40     bool prePinchSwitch_ { true };
41     bool preSwipeSwitch_ { true };
42     bool preRotateSwitch_ { true };
43     bool preDoubleTapDragSwitch_ { true };
44 };
45 
46 class JoystickTransformProcessorTest : public testing::Test {
47 public:
SetUpTestCase(void)48     static void SetUpTestCase(void) {}
TearDownTestCase(void)49     static void TearDownTestCase(void) {}
50 };
51 
52 class TouchTransformProcessorTest : public testing::Test {
53 public:
SetUpTestCase(void)54     static void SetUpTestCase(void) {}
TearDownTestCase(void)55     static void TearDownTestCase(void) {}
56 };
57 
SetUp()58 void TouchEventNormalizeTest::SetUp()
59 {
60     TOUCH_EVENT_HDR->GetTouchpadPinchSwitch(prePinchSwitch_);
61     TOUCH_EVENT_HDR->GetTouchpadSwipeSwitch(preSwipeSwitch_);
62     TOUCH_EVENT_HDR->GetTouchpadRotateSwitch(preRotateSwitch_);
63     TOUCH_EVENT_HDR->GetTouchpadDoubleTapAndDragState(preDoubleTapDragSwitch_);
64 }
65 
TearDown()66 void TouchEventNormalizeTest::TearDown()
67 {
68     TOUCH_EVENT_HDR->SetTouchpadPinchSwitch(prePinchSwitch_);
69     TOUCH_EVENT_HDR->SetTouchpadSwipeSwitch(preSwipeSwitch_);
70     TOUCH_EVENT_HDR->SetTouchpadRotateSwitch(preRotateSwitch_);
71     TOUCH_EVENT_HDR->SetTouchpadDoubleTapAndDragState(preDoubleTapDragSwitch_);
72 }
73 
74 /**
75  * @tc.name: JoystickTransformProcessorTest_LibinputButtonToPointer
76  * @tc.desc: Test LibinputButtonToPointer
77  * @tc.type: FUNC
78  * @tc.require:
79  */
80 HWTEST_F(JoystickTransformProcessorTest, JoystickTransformProcessorTest_LibinputButtonToPointer, TestSize.Level1)
81 {
82     int32_t deviceId = 123;
83     JoystickTransformProcessor joystickTransformProcessor(deviceId);
84     uint32_t button = LIBINPUT_HOMEPAGE_BUTTON_CODE;
85     ASSERT_EQ(joystickTransformProcessor.LibinputButtonToPointer(button), PointerEvent::JOYSTICK_BUTTON_HOMEPAGE);
86 
87     button = LIBINPUT_BUTTON_NONE;
88     ASSERT_EQ(joystickTransformProcessor.LibinputButtonToPointer(button), PointerEvent::BUTTON_NONE);
89 }
90 
91 /**
92  * @tc.name: TouchTransformProcessorTest_UpdatePointerItemProperties
93  * @tc.desc: Test UpdatePointerItemProperties
94  * @tc.type: FUNC
95  * @tc.require:
96  */
97 HWTEST_F(TouchTransformProcessorTest, TouchTransformProcessorTest_UpdatePointerItemProperties, TestSize.Level1)
98 {
99     CALL_TEST_DEBUG;
100     int32_t deviceId = 6;
101     TouchTransformProcessor touchTransformProcessor(deviceId);
102     PointerEvent::PointerItem item;
103     EventTouch touchInfo;
104     touchInfo.point.x = 125;
105     touchInfo.point.y = 300;
106     touchInfo.toolRect.point.x = 300;
107     touchInfo.toolRect.point.y = 600;
108     touchInfo.toolRect.width = 720;
109     touchInfo.toolRect.height = 1000;
110     ASSERT_NO_FATAL_FAILURE(touchTransformProcessor.UpdatePointerItemProperties(item, touchInfo));
111 }
112 
113 /**
114  * @tc.name: TouchEventNormalizeTest_MakeTransformProcessor
115  * @tc.desc: Test Gets the TransformProcessor pointer based on the device type
116  * @tc.type: FUNC
117  * @tc.require:
118  */
119 HWTEST_F(TouchEventNormalizeTest, TouchEventNormalizeTest_MakeTransformProcessor, TestSize.Level1)
120 {
121     CALL_TEST_DEBUG;
122     int32_t deviceId = 123456;
123     ASSERT_NE(TOUCH_EVENT_HDR->MakeTransformProcessor(deviceId, TouchEventNormalize::DeviceType::TOUCH), nullptr);
124     ASSERT_NE(TOUCH_EVENT_HDR->MakeTransformProcessor(deviceId, TouchEventNormalize::DeviceType::TABLET_TOOL), nullptr);
125     ASSERT_NE(TOUCH_EVENT_HDR->MakeTransformProcessor(deviceId, TouchEventNormalize::DeviceType::TOUCH_PAD), nullptr);
126     ASSERT_NE(TOUCH_EVENT_HDR->MakeTransformProcessor(deviceId, TouchEventNormalize::DeviceType::GESTURE), nullptr);
127     ASSERT_NE(TOUCH_EVENT_HDR->MakeTransformProcessor(deviceId, TouchEventNormalize::DeviceType::JOYSTICK), nullptr);
128     ASSERT_EQ(TOUCH_EVENT_HDR->MakeTransformProcessor(deviceId, TouchEventNormalize::DeviceType::KNUCKLE), nullptr);
129 }
130 
131 /**
132  * @tc.name: TouchEventNormalizeTest_SetTouchpadPinchSwitch_01
133  * @tc.desc: Test SetTouchpadPinchSwitch
134  * @tc.type: FUNC
135  * @tc.require:
136  */
137 HWTEST_F(TouchEventNormalizeTest, TouchEventNormalizeTest_SetTouchpadPinchSwitch_01, TestSize.Level1)
138 {
139     CALL_TEST_DEBUG;
140     bool flag = false;
141     ASSERT_TRUE(TOUCH_EVENT_HDR->SetTouchpadPinchSwitch(flag) == RET_OK);
142 }
143 
144 /**
145  * @tc.name: TouchEventNormalizeTest_GetTouchpadPinchSwitch_02
146  * @tc.desc: Test GetTouchpadPinchSwitch
147  * @tc.type: FUNC
148  * @tc.require:
149  */
150 HWTEST_F(TouchEventNormalizeTest, TouchEventNormalizeTest_GetTouchpadPinchSwitch_02, TestSize.Level1)
151 {
152     CALL_TEST_DEBUG;
153     bool flag = true;
154     TOUCH_EVENT_HDR->SetTouchpadPinchSwitch(flag);
155     bool newFlag = true;
156     TOUCH_EVENT_HDR->GetTouchpadPinchSwitch(flag);
157     ASSERT_TRUE(flag == newFlag);
158 }
159 
160 /**
161  * @tc.name: TouchEventNormalizeTest_SetTouchpadSwipeSwitch_03
162  * @tc.desc: Test SetTouchpadSwipeSwitch
163  * @tc.type: FUNC
164  * @tc.require:
165  */
166 HWTEST_F(TouchEventNormalizeTest, TouchEventNormalizeTest_SetTouchpadSwipeSwitch_03, TestSize.Level1)
167 {
168     CALL_TEST_DEBUG;
169     bool flag = false;
170     ASSERT_TRUE(TOUCH_EVENT_HDR->SetTouchpadSwipeSwitch(flag) == RET_OK);
171 }
172 
173 /**
174  * @tc.name: TouchEventNormalizeTest_GetTouchpadSwipeSwitch_04
175  * @tc.desc: Test GetTouchpadSwipeSwitch
176  * @tc.type: FUNC
177  * @tc.require:
178  */
179 HWTEST_F(TouchEventNormalizeTest, TouchEventNormalizeTest_GetTouchpadSwipeSwitch_04, TestSize.Level1)
180 {
181     CALL_TEST_DEBUG;
182     bool flag = true;
183     TOUCH_EVENT_HDR->SetTouchpadSwipeSwitch(flag);
184     bool newFlag = true;
185     TOUCH_EVENT_HDR->GetTouchpadSwipeSwitch(flag);
186     ASSERT_TRUE(flag == newFlag);
187 }
188 
189 /**
190  * @tc.name: TouchEventNormalizeTest_SetTouchpadRotateSwitch_05
191  * @tc.desc: Test SetTouchpadRotateSwitch
192  * @tc.type: FUNC
193  * @tc.require:
194  */
195 HWTEST_F(TouchEventNormalizeTest, TouchEventNormalizeTest_SetTouchpadRotateSwitch_05, TestSize.Level1)
196 {
197     CALL_TEST_DEBUG;
198     bool rotateSwitch = false;
199     ASSERT_TRUE(TOUCH_EVENT_HDR->SetTouchpadRotateSwitch(rotateSwitch) == RET_OK);
200 }
201 
202 /**
203  * @tc.name: TouchEventNormalizeTest_GetTouchpadRotateSwitch_06
204  * @tc.desc: Test GetTouchpadRotateSwitch
205  * @tc.type: FUNC
206  * @tc.require:
207  */
208 HWTEST_F(TouchEventNormalizeTest, TouchEventNormalizeTest_GetTouchpadRotateSwitch_06, TestSize.Level1)
209 {
210     CALL_TEST_DEBUG;
211     bool rotateSwitch = true;
212     TOUCH_EVENT_HDR->SetTouchpadRotateSwitch(rotateSwitch);
213     bool newRotateSwitch = true;
214     TOUCH_EVENT_HDR->GetTouchpadRotateSwitch(rotateSwitch);
215     ASSERT_TRUE(rotateSwitch == newRotateSwitch);
216 }
217 } // namespace MMI
218 } // namespace OHOS