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 "crown_transform_processor.h"
20 #include "general_crown.h"
21 #include "i_input_windows_manager.h"
22 #include "input_device_manager.h"
23 #include "input_event_handler.h"
24 #include "libinput.h"
25 #include "libinput_wrapper.h"
26 #include "mmi_log.h"
27 #include "window_info.h"
28 
29 #undef MMI_LOG_TAG
30 #define MMI_LOG_TAG "CrownTransformProcessorTest"
31 
32 namespace OHOS {
33 namespace MMI {
34 namespace {
35 using namespace testing::ext;
36 }
37 class CrownTransformProcessorTest : public testing::Test {
38 public:
39     static void SetUpTestCase(void);
40     static void TearDownTestCase(void);
41     static void SetupCrown();
42     static void CloseCrown();
43     static void InitHandler();
44     libinput_event *GetEvent();
45 
46 private:
47     static GeneralCrown vCrown_;
48     static LibinputWrapper libinput_;
49 };
50 
51 GeneralCrown CrownTransformProcessorTest::vCrown_;
52 LibinputWrapper CrownTransformProcessorTest::libinput_;
53 
SetUpTestCase(void)54 void CrownTransformProcessorTest::SetUpTestCase(void)
55 {
56     ASSERT_TRUE(libinput_.Init());
57     SetupCrown();
58     InitHandler();
59 }
60 
TearDownTestCase(void)61 void CrownTransformProcessorTest::TearDownTestCase(void)
62 {
63     CloseCrown();
64 }
65 
SetupCrown()66 void CrownTransformProcessorTest::SetupCrown()
67 {
68     ASSERT_TRUE(vCrown_.SetUp());
69     MMI_HILOGD("device node name: %{public}s", vCrown_.GetDevPath().c_str());
70     ASSERT_TRUE(libinput_.AddPath(vCrown_.GetDevPath()));
71 
72     libinput_event *event = libinput_.Dispatch();
73     ASSERT_TRUE(event != nullptr);
74     ASSERT_EQ(libinput_event_get_type(event), LIBINPUT_EVENT_DEVICE_ADDED);
75     struct libinput_device *device = libinput_event_get_device(event);
76     ASSERT_TRUE(device != nullptr);
77     INPUT_DEV_MGR->OnInputDeviceAdded(device);
78 }
79 
CloseCrown()80 void CrownTransformProcessorTest::CloseCrown()
81 {
82     libinput_.RemovePath(vCrown_.GetDevPath());
83     vCrown_.Close();
84 }
85 
InitHandler()86 void CrownTransformProcessorTest::InitHandler()
87 {
88     UDSServer udsServer;
89     std::shared_ptr<OHOS::MMI::InputEventHandler> inputHandler = InputHandler;
90     ASSERT_NO_FATAL_FAILURE(inputHandler->Init(udsServer));
91 }
92 
GetEvent()93 libinput_event *CrownTransformProcessorTest::GetEvent()
94 {
95     libinput_event *event = nullptr;
96     libinput_event *evt = libinput_.Dispatch();
97     while (evt != nullptr) {
98         auto type = libinput_event_get_type(evt);
99         if (type == LIBINPUT_EVENT_POINTER_AXIS) {
100             event = evt;
101         }
102         evt = libinput_.Dispatch();
103     }
104     return event;
105 }
106 
107 /**
108  * @tc.name: CrownTransformProcessorTest_GetPointerEvent_001
109  * @tc.desc: Test GetPointerEvent
110  * @tc.type: FUNC
111  * @tc.require:
112  */
113 HWTEST_F(CrownTransformProcessorTest, CrownTransformProcessorTest_GetPointerEvent_001, TestSize.Level1)
114 {
115     CALL_TEST_DEBUG;
116     auto ret = CROWN_EVENT_HDR->GetPointerEvent();
117     ASSERT_NE(ret, nullptr);
118 }
119 
120 /**
121  * @tc.name: CrownTransformProcessorTest_IsCrownEvent_002
122  * @tc.desc: Test IsCrownEvent
123  * @tc.type: FUNC
124  * @tc.require:
125  */
126 HWTEST_F(CrownTransformProcessorTest, CrownTransformProcessorTest_IsCrownEvent_002, TestSize.Level1)
127 {
128     CALL_TEST_DEBUG;
129     vCrown_.SendEvent(EV_REL, REL_WHEEL, 5);
130     vCrown_.SendEvent(EV_SYN, SYN_REPORT, 0);
131     libinput_event *event = GetEvent();
132     ASSERT_TRUE(event != nullptr);
133     struct libinput_device *dev = libinput_event_get_device(event);
134     ASSERT_TRUE(dev != nullptr);
135     std::string name = libinput_device_get_name(dev);
136     MMI_HILOGD("pointer device: %{public}s", name.c_str());
137     ASSERT_TRUE(CROWN_EVENT_HDR->IsCrownEvent(event));
138 }
139 
140 
141 /**
142  * @tc.name: CrownTransformProcessorTest_NormalizeRotateEvent_003
143  * @tc.desc: Test NormalizeRotateEvent
144  * @tc.type: FUNC
145  * @tc.require:
146  */
147 HWTEST_F(CrownTransformProcessorTest, CrownTransformProcessorTest_NormalizeRotateEvent_003, TestSize.Level1)
148 {
149     CALL_TEST_DEBUG;
150     vCrown_.SendEvent(EV_REL, REL_WHEEL, 5);
151     vCrown_.SendEvent(EV_SYN, SYN_REPORT, 0);
152     libinput_event *event = GetEvent();
153     ASSERT_TRUE(event != nullptr);
154     struct libinput_device *dev = libinput_event_get_device(event);
155     ASSERT_TRUE(dev != nullptr);
156     std::string name = libinput_device_get_name(dev);
157     MMI_HILOGD("pointer device: %{public}s", name.c_str());
158     int32_t result = CROWN_EVENT_HDR->NormalizeRotateEvent(event);
159     ASSERT_TRUE(result > 0);
160 }
161 
162 /**
163  * @tc.name: CrownTransformProcessorTest_HandleCrownRotateBegin_004
164  * @tc.desc: Test HandleCrownRotateBegin
165  * @tc.type: FUNC
166  * @tc.require:
167  */
168 HWTEST_F(CrownTransformProcessorTest, CrownTransformProcessorTest_HandleCrownRotateBegin_004, TestSize.Level1)
169 {
170     CALL_TEST_DEBUG;
171     vCrown_.SendEvent(EV_REL, REL_WHEEL, 30);
172     vCrown_.SendEvent(EV_SYN, SYN_REPORT, 0);
173     libinput_event *event = GetEvent();
174     ASSERT_TRUE(event != nullptr);
175     struct libinput_device *dev = libinput_event_get_device(event);
176     ASSERT_TRUE(dev != nullptr);
177     std::string name = libinput_device_get_name(dev);
178     MMI_HILOGD("pointer device: %{public}s", name.c_str());
179     struct libinput_event_pointer *rawPointerEvent = libinput_event_get_pointer_event(event);
180     ASSERT_TRUE(rawPointerEvent != nullptr);
181     int32_t result = CROWN_EVENT_HDR->HandleCrownRotateBegin(rawPointerEvent);
182     EXPECT_EQ(result, RET_OK);
183 }
184 
185 /**
186  * @tc.name: CrownTransformProcessorTest_HandleCrownRotateUpdate_005
187  * @tc.desc: Test HandleCrownRotateUpdate
188  * @tc.type: FUNC
189  * @tc.require:
190  */
191 HWTEST_F(CrownTransformProcessorTest, CrownTransformProcessorTest_HandleCrownRotateUpdate_005, TestSize.Level1)
192 {
193     CALL_TEST_DEBUG;
194     vCrown_.SendEvent(EV_REL, REL_WHEEL, -30);
195     vCrown_.SendEvent(EV_SYN, SYN_REPORT, 0);
196     libinput_event *event = GetEvent();
197     ASSERT_TRUE(event != nullptr);
198     struct libinput_device *dev = libinput_event_get_device(event);
199     ASSERT_TRUE(dev != nullptr);
200     std::string name = libinput_device_get_name(dev);
201     MMI_HILOGD("pointer device: %{public}s", name.c_str());
202     struct libinput_event_pointer *rawPointerEvent = libinput_event_get_pointer_event(event);
203     ASSERT_TRUE(rawPointerEvent != nullptr);
204     int32_t result = CROWN_EVENT_HDR->HandleCrownRotateUpdate(rawPointerEvent);
205     EXPECT_EQ(result, RET_OK);
206 }
207 
208 /**
209  * @tc.name: CrownTransformProcessorTest_HandleCrownRotateEnd_006
210  * @tc.desc: Test HandleCrownRotateEnd
211  * @tc.type: FUNC
212  * @tc.require:
213  */
214 HWTEST_F(CrownTransformProcessorTest, CrownTransformProcessorTest_HandleCrownRotateEnd_006, TestSize.Level1)
215 {
216     CALL_TEST_DEBUG;
217     ASSERT_NO_FATAL_FAILURE(CROWN_EVENT_HDR->HandleCrownRotateEnd());
218 }
219 
220 /**
221  * @tc.name: CrownTransformProcessorTest_HandleCrownRotateBeginAndUpdate_007
222  * @tc.desc: Test HandleCrownRotateBeginAndUpdate
223  * @tc.type: FUNC
224  * @tc.require:
225  */
226 HWTEST_F(CrownTransformProcessorTest, CrownTransformProcessorTest_HandleCrownRotateBeginAndUpdate_007, TestSize.Level1)
227 {
228     CALL_TEST_DEBUG;
229     vCrown_.SendEvent(EV_REL, REL_WHEEL, 10);
230     vCrown_.SendEvent(EV_SYN, SYN_REPORT, 0);
231     libinput_event *event = GetEvent();
232     ASSERT_TRUE(event != nullptr);
233     struct libinput_device *dev = libinput_event_get_device(event);
234     ASSERT_TRUE(dev != nullptr);
235     std::string name = libinput_device_get_name(dev);
236     MMI_HILOGD("pointer device: %{public}s", name.c_str());
237     struct libinput_event_pointer *rawPointerEvent = libinput_event_get_pointer_event(event);
238     ASSERT_TRUE(rawPointerEvent != nullptr);
239     int32_t result = CROWN_EVENT_HDR->HandleCrownRotateBeginAndUpdate(rawPointerEvent,
240         PointerEvent::POINTER_ACTION_AXIS_BEGIN);
241     EXPECT_EQ(result, RET_OK);
242 }
243 
244 /**
245  * @tc.name: CrownTransformProcessorTest_HandleCrownRotatePostInner_008
246  * @tc.desc: Test HandleCrownRotatePostInner
247  * @tc.type: FUNC
248  * @tc.require:
249  */
250 HWTEST_F(CrownTransformProcessorTest, CrownTransformProcessorTest_HandleCrownRotatePostInner_008, TestSize.Level1)
251 {
252     CALL_TEST_DEBUG;
253     double angularVelocity = 67.0;
254     double degree = 11.0;
255     int32_t action = PointerEvent::POINTER_ACTION_AXIS_UPDATE;
256     ASSERT_NO_FATAL_FAILURE(CROWN_EVENT_HDR->HandleCrownRotatePostInner(angularVelocity, degree, action));
257 }
258 
259 /**
260  * @tc.name: CrownTransformProcessorTest_DumpInner_009
261  * @tc.desc: Test DumpInner
262  * @tc.type: FUNC
263  * @tc.require:
264  */
265 HWTEST_F(CrownTransformProcessorTest, CrownTransformProcessorTest_DumpInner_009, TestSize.Level1)
266 {
267     CALL_TEST_DEBUG;
268     ASSERT_NO_FATAL_FAILURE(CROWN_EVENT_HDR->DumpInner());
269 }
270 
271 /**
272  * @tc.name: CrownTransformProcessorTest_Dump_010
273  * @tc.desc: Test Dump
274  * @tc.type: FUNC
275  * @tc.require:
276  */
277 HWTEST_F(CrownTransformProcessorTest, CrownTransformProcessorTest_Dump_010, TestSize.Level1)
278 {
279     CALL_TEST_DEBUG;
280     std::vector<std::string> args;
281     std::vector<std::string> idNames;
282     int32_t fd = 0;
283     CROWN_EVENT_HDR->Dump(fd, args);
284     ASSERT_EQ(args, idNames);
285 }
286 }
287 }