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_uwb_remote_control.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 "remote_control_transform_processor.h"
26
27 namespace OHOS {
28 namespace MMI {
29 namespace {
30 using namespace testing::ext;
31 constexpr int32_t POINTER_MOVEFLAG = { 7 };
32 } // namespace
33
34 class RemoteControlTransformProcessorTest : public testing::Test {
35 public:
36 static void SetUpTestCase(void);
37 static void TearDownTestCase(void);
38 void SetUp();
39 void TearDown();
40
41 private:
42 static void SetupUwbRemoteControl();
43 static void CloseUwbRemoteControl();
44 static GeneralUwbRemoteControl vUwbRemoteControl_;
45 static LibinputWrapper libinput_;
46 };
47
48 GeneralUwbRemoteControl RemoteControlTransformProcessorTest::vUwbRemoteControl_;
49 LibinputWrapper RemoteControlTransformProcessorTest::libinput_;
50
SetUpTestCase(void)51 void RemoteControlTransformProcessorTest::SetUpTestCase(void)
52 {
53 ASSERT_TRUE(libinput_.Init());
54 SetupUwbRemoteControl();
55 }
56
TearDownTestCase(void)57 void RemoteControlTransformProcessorTest::TearDownTestCase(void)
58 {
59 CloseUwbRemoteControl();
60 }
61
62
SetupUwbRemoteControl()63 void RemoteControlTransformProcessorTest::SetupUwbRemoteControl()
64 {
65 ASSERT_TRUE(vUwbRemoteControl_.SetUp());
66 std::cout << "device node name: " << vUwbRemoteControl_.GetDevPath() << std::endl;
67 ASSERT_FALSE(libinput_.AddPath(vUwbRemoteControl_.GetDevPath()));
68 libinput_event *event = libinput_.Dispatch();
69 ASSERT_TRUE(event != nullptr);
70 ASSERT_EQ(libinput_event_get_type(event), LIBINPUT_EVENT_DEVICE_ADDED);
71 struct libinput_device *device = libinput_event_get_device(event);
72 ASSERT_TRUE(device != nullptr);
73 }
74
75
CloseUwbRemoteControl()76 void RemoteControlTransformProcessorTest::CloseUwbRemoteControl()
77 {
78 libinput_.RemovePath(vUwbRemoteControl_.GetDevPath());
79 vUwbRemoteControl_.Close();
80 }
81
SetUp()82 void RemoteControlTransformProcessorTest::SetUp()
83 {
84 }
85
TearDown()86 void RemoteControlTransformProcessorTest::TearDown()
87 {
88 }
89
90 /**
91 * @tc.name: Remote_ControlTransformProcessorTest_InitToolTypes_001
92 * @tc.desc: Test the funcation InitToolTypes
93 * @tc.type: FUNC
94 * @tc.require:
95 */
96 HWTEST_F(RemoteControlTransformProcessorTest, InitToolTypes_001, TestSize.Level1)
97 {
98 int32_t deviceId = 7;
99 Remote_ControlTransformProcessor processor(deviceId);
100 processor.InitToolTypes();
101 ASSERT_EQ(processor.vecToolType_.size(), 16);
102 ASSERT_EQ(processor.vecToolType_[0].first, BTN_TOOL_PEN);
103 ASSERT_EQ(processor.vecToolType_[0].second, PointerEvent::TOOL_TYPE_PEN);
104 ASSERT_EQ(processor.vecToolType_[1].first, BTN_TOOL_RUBBER);
105 ASSERT_EQ(processor.vecToolType_[1].second, PointerEvent::TOOL_TYPE_RUBBER);
106 ASSERT_EQ(processor.vecToolType_[2].first, BTN_TOOL_BRUSH);
107 ASSERT_EQ(processor.vecToolType_[2].second, PointerEvent::TOOL_TYPE_BRUSH);
108 ASSERT_EQ(processor.vecToolType_[3].first, BTN_TOOL_PENCIL);
109 ASSERT_EQ(processor.vecToolType_[3].second, PointerEvent::TOOL_TYPE_PENCIL);
110 ASSERT_EQ(processor.vecToolType_[4].first, BTN_TOOL_AIRBRUSH);
111 ASSERT_EQ(processor.vecToolType_[4].second, PointerEvent::TOOL_TYPE_AIRBRUSH);
112 ASSERT_EQ(processor.vecToolType_[5].first, BTN_TOOL_FINGER);
113 ASSERT_EQ(processor.vecToolType_[5].second, PointerEvent::TOOL_TYPE_FINGER);
114 ASSERT_EQ(processor.vecToolType_[6].first, BTN_TOOL_MOUSE);
115 ASSERT_EQ(processor.vecToolType_[6].second, PointerEvent::TOOL_TYPE_MOUSE);
116 ASSERT_EQ(processor.vecToolType_[7].first, BTN_TOOL_LENS);
117 ASSERT_EQ(processor.vecToolType_[7].second, PointerEvent::TOOL_TYPE_LENS);
118 }
119
120 /**
121 * @tc.name: Remote_ControlTransformProcessorTest_OnEvent_001
122 * @tc.desc: Test OnEvent
123 * @tc.type: FUNC
124 * @tc.require:
125 */
126 HWTEST_F(RemoteControlTransformProcessorTest, Remote_ControlTransformProcessorTest_OnEvent_001,
127 TestSize.Level1)
128 {
129 CALL_TEST_DEBUG;
130 libinput_.DrainEvents();
131 int32_t deviceId = 7;
132 Remote_ControlTransformProcessor processor(deviceId);
133 processor.pointerEvent_ = PointerEvent::Create();
134 ASSERT_TRUE(processor.pointerEvent_ != nullptr);
135 int32_t varMoveFlag = POINTER_MOVEFLAG;
136 std::cout << "varMoveFlag: " << POINTER_MOVEFLAG << std::endl;
137 for (int32_t index = 1; index < POINTER_MOVEFLAG; ++index) {
138 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_TRACKING_ID, 0);
139 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_POSITION_X, 5190 + index*30);
140 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_POSITION_Y, 8306);
141 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_PRESSURE, 321);
142 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_MOVEFLAG, varMoveFlag);
143 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_TOUCH_MAJOR, 198);
144 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_TOUCH_MINOR, 180);
145 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_ORIENTATION, -64);
146 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_BLOB_ID, 2);
147 vUwbRemoteControl_.SendEvent(EV_SYN, SYN_MT_REPORT, 0);
148 vUwbRemoteControl_.SendEvent(EV_KEY, BTN_TOUCH, 0);
149 vUwbRemoteControl_.SendEvent(EV_SYN, SYN_REPORT, 0);
150 }
151 libinput_event* event = libinput_.Dispatch();
152 ASSERT_TRUE(event != nullptr);
153 while (event != nullptr) {
154 auto type = libinput_event_get_type(event);
155 if (type == LIBINPUT_EVENT_TOUCH_CANCEL || type == LIBINPUT_EVENT_TOUCH_FRAME) {
156 event = libinput_.Dispatch();
157 continue;
158 }
159 std::cout << "type: " << type << std::endl;
160 auto touch = libinput_event_get_touch_event(event);
161 ASSERT_TRUE(touch != nullptr);
162 int32_t moveFlag = libinput_event_touch_get_move_flag(touch);
163 std::cout << "moveFlag: " << moveFlag << std::endl;
164 auto dev = libinput_event_get_device(event);
165 ASSERT_TRUE(dev != nullptr);
166 std::cout << "touch device: " << libinput_device_get_name(dev) << std::endl;
167 EXPECT_NO_FATAL_FAILURE(processor.OnEvent(event));
168 event = libinput_.Dispatch();
169 }
170 }
171
172 /**
173 * @tc.name: Remote_ControlTransformProcessorTest_OnEventTouchMotion_001
174 * @tc.desc: Test the funcation OnEventTouchMotion
175 * @tc.type: FUNC
176 * @tc.require:
177 */
178 HWTEST_F(RemoteControlTransformProcessorTest, Remote_ControlTransformProcessorTest_OnEventTouchMotion_001,
179 TestSize.Level1)
180 {
181 int32_t deviceId = 7;
182 Remote_ControlTransformProcessor processor(deviceId);
183 libinput_event* event = nullptr;
184 bool ret = processor.OnEventTouchMotion(event);
185 ASSERT_FALSE(ret);
186 }
187
188 /**
189 * @tc.name: Remote_ControlTransformProcessorTest_OnEventTouchMotion_002
190 * @tc.desc: Test the funcation OnEventTouchMotion
191 * @tc.type: FUNC
192 * @tc.require:
193 */
194 HWTEST_F(RemoteControlTransformProcessorTest, Remote_ControlTransformProcessorTest_OnEventTouchMotion_002,
195 TestSize.Level1)
196 {
197 CALL_TEST_DEBUG;
198 libinput_.DrainEvents();
199 int32_t deviceId = 7;
200 Remote_ControlTransformProcessor processor(deviceId);
201 processor.pointerEvent_ = PointerEvent::Create();
202 ASSERT_TRUE(processor.pointerEvent_ != nullptr);
203 int32_t varMoveFlag = POINTER_MOVEFLAG;
204 std::cout << "moveflag: " << varMoveFlag << std::endl;
205 for (int32_t index = 1; index < POINTER_MOVEFLAG; ++index) {
206 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_TRACKING_ID, 0);
207 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_POSITION_X, 5190);
208 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_POSITION_Y, 8306);
209 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_PRESSURE, 321);
210 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_MOVEFLAG, varMoveFlag);
211 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_TOUCH_MAJOR, 198);
212 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_TOUCH_MINOR, 180);
213 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_ORIENTATION, -64);
214 vUwbRemoteControl_.SendEvent(EV_ABS, ABS_MT_BLOB_ID, 2);
215 vUwbRemoteControl_.SendEvent(EV_SYN, SYN_MT_REPORT, 0);
216 vUwbRemoteControl_.SendEvent(EV_KEY, BTN_TOUCH, 0);
217 vUwbRemoteControl_.SendEvent(EV_SYN, SYN_REPORT, 0);
218 }
219 libinput_event* event = libinput_.Dispatch();
220 ASSERT_TRUE(event != nullptr);
221 while (event != nullptr) {
222 auto type = libinput_event_get_type(event);
223 if (type == LIBINPUT_EVENT_TOUCH_CANCEL || type == LIBINPUT_EVENT_TOUCH_FRAME) {
224 event = libinput_.Dispatch();
225 continue;
226 }
227 std::cout << "type: " << type << std::endl;
228 auto touch = libinput_event_get_touch_event(event);
229 ASSERT_TRUE(touch != nullptr);
230 int32_t moveFlag = libinput_event_touch_get_move_flag(touch);
231 std::cout << "moveflag: " << moveFlag << std::endl;
232 ASSERT_EQ(moveFlag, varMoveFlag);
233 struct libinput_device *dev = libinput_event_get_device(event);
234 ASSERT_TRUE(dev != nullptr);
235 std::cout << "pointer device: " << libinput_device_get_name(dev) << std::endl;
236 EXPECT_NO_FATAL_FAILURE(processor.OnEventTouchMotion(event));
237 event = libinput_.Dispatch();
238 }
239 }
240 } // namespace MMI
241 } // namespace OHOS