• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "pointer_event.h"
16 
17 #include <gtest/gtest.h>
18 
19 #include "axis_event.h"
20 #include "define_multimodal.h"
21 #include "input_device.h"
22 #include "input_event.h"
23 #include "proto.h"
24 #include "util.h"
25 
26 #undef MMI_LOG_TAG
27 #define MMI_LOG_TAG "PointerEventExTest"
28 
29 namespace OHOS {
30 namespace MMI {
31 namespace {
32 using namespace testing::ext;
33 } // namespace
34 class PointerEventExTest : public testing::Test {
35 public:
36     static void SetUpTestCase(void);
37 };
38 
SetUpTestCase(void)39 void PointerEventExTest::SetUpTestCase(void)
40 {
41 }
42 /**
43  * @tc.name: PointerEventExTest_ToString_001
44  * @tc.desc: Test the funcation ToString
45  * @tc.type: FUNC
46  * @tc.require:
47  */
48 HWTEST_F(PointerEventExTest, PointerEventExTest_ToString_001, TestSize.Level2)
49 {
50     CALL_TEST_DEBUG;
51     auto pointerEvent = PointerEvent::Create();
52     ASSERT_NE(pointerEvent, nullptr);
53     pointerEvent->SetSourceType(PointerEvent::SOURCE_TYPE_TOUCHSCREEN);
54     pointerEvent->SetPointerId(0);
55     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_MOVE);
56     PointerEvent::PointerItem pointerItem1;
57     int32_t disPlayX1 = 100;
58     int32_t disPlayY1 = 110;
59     pointerItem1.SetFixedDisplayXPos(disPlayX1);
60     pointerItem1.SetFixedDisplayYPos(disPlayY1);
61     pointerItem1.SetPointerId(0);
62     pointerItem1.SetDownTime(0);
63     pointerItem1.SetPressed(true);
64     pointerItem1.SetPressure(30);
65     pointerItem1.SetToolType(PointerEvent::TOOL_TYPE_FINGER);
66     pointerEvent->AddPointerItem(pointerItem1);
67 
68     int32_t buttonId = 1;
69     pointerEvent->SetButtonPressed(buttonId);
70     buttonId = 2;
71     pointerEvent->SetButtonPressed(buttonId);
72     auto rlt = pointerEvent->ToString();
73     EXPECT_NE(rlt.find("displayX"), string::npos);
74 }
75 
76 /**
77  * @tc.name: PointerEventExTest_DumpPointerAction_005
78  * @tc.desc: Verify the funcation DumpPointerAction
79  * @tc.type: FUNC
80  * @tc.require:
81  * @tc.author:
82  */
83 HWTEST_F(PointerEventExTest, PointerEventExTest_DumpPointerAction_005, TestSize.Level2)
84 {
85     CALL_TEST_DEBUG;
86     auto pointerEvent = PointerEvent::Create();
87     ASSERT_NE(pointerEvent, nullptr);
88     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_BEGIN);
89     double axisValue = 0;
90     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_VERTICAL, axisValue);
91     std::string str = pointerEvent->DumpPointerAction();
92     EXPECT_EQ(str, "axis-begin");
93 
94     pointerEvent->ClearAxisValue();
95     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL, axisValue);
96     str = pointerEvent->DumpPointerAction();
97     EXPECT_EQ(str, "axis-begin");
98 
99     pointerEvent->ClearAxisValue();
100     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_PINCH, axisValue);
101     str = pointerEvent->DumpPointerAction();
102     EXPECT_EQ(str, "pinch-begin");
103 
104     pointerEvent->ClearAxisValue();
105     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_ROTATE, axisValue);
106     ASSERT_NO_FATAL_FAILURE(pointerEvent->DumpPointerAction());
107     str = pointerEvent->DumpPointerAction();
108     EXPECT_EQ(str, "axis-begin");
109 }
110 
111 /**
112  * @tc.name: PointerEventExTest_DumpPointerAction_006
113  * @tc.desc: Verify the funcation DumpPointerAction
114  * @tc.type: FUNC
115  * @tc.require:
116  * @tc.author:
117  */
118 HWTEST_F(PointerEventExTest, PointerEventExTest_DumpPointerAction_006, TestSize.Level2)
119 {
120     CALL_TEST_DEBUG;
121     auto pointerEvent = PointerEvent::Create();
122     ASSERT_NE(pointerEvent, nullptr);
123     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_UPDATE);
124     double axisValue = 0;
125     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_VERTICAL, axisValue);
126     std::string str = pointerEvent->DumpPointerAction();
127     EXPECT_EQ(str, "axis-update");
128 
129     pointerEvent->ClearAxisValue();
130     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL, axisValue);
131     str = pointerEvent->DumpPointerAction();
132     EXPECT_EQ(str, "axis-update");
133 
134     pointerEvent->ClearAxisValue();
135     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_PINCH, axisValue);
136     str = pointerEvent->DumpPointerAction();
137     EXPECT_EQ(str, "pinch-update");
138 
139     pointerEvent->ClearAxisValue();
140     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_ROTATE, axisValue);
141     str = pointerEvent->DumpPointerAction();
142     EXPECT_EQ(str, "axis-update");
143 }
144 
145 /**
146  * @tc.name: PointerEventExTest_DumpPointerAction_007
147  * @tc.desc: Verify the funcation DumpPointerAction
148  * @tc.type: FUNC
149  * @tc.require:
150  * @tc.author:
151  */
152 HWTEST_F(PointerEventExTest, PointerEventExTest_DumpPointerAction_007, TestSize.Level2)
153 {
154     CALL_TEST_DEBUG;
155     auto pointerEvent = PointerEvent::Create();
156     ASSERT_NE(pointerEvent, nullptr);
157     pointerEvent->SetPointerAction(PointerEvent::POINTER_ACTION_AXIS_END);
158     double axisValue = 0;
159     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_VERTICAL, axisValue);
160     std::string str = pointerEvent->DumpPointerAction();
161     EXPECT_EQ(str, "axis-end");
162 
163     pointerEvent->ClearAxisValue();
164     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_SCROLL_HORIZONTAL, axisValue);
165     str = pointerEvent->DumpPointerAction();
166     EXPECT_EQ(str, "axis-end");
167 
168     pointerEvent->ClearAxisValue();
169     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_PINCH, axisValue);
170     str = pointerEvent->DumpPointerAction();
171     EXPECT_EQ(str, "pinch-end");
172 
173     pointerEvent->ClearAxisValue();
174     pointerEvent->SetAxisValue(PointerEvent::AXIS_TYPE_ROTATE, axisValue);
175     str = pointerEvent->DumpPointerAction();
176     EXPECT_EQ(str, "axis-end");
177 }
178 
179 /**
180  * @tc.name: PointerEventExTest_DumpPointerAction_008
181  * @tc.desc: Verify the funcation DumpPointerAction
182  * @tc.type: FUNC
183  * @tc.require:
184  * @tc.author:
185  */
186 HWTEST_F(PointerEventExTest, PointerEventExTest_DumpPointerAction_008, TestSize.Level2)
187 {
188     CALL_TEST_DEBUG;
189     auto pointerEvent = PointerEvent::Create();
190     ASSERT_NE(pointerEvent, nullptr);
191 
192     int32_t invalidAction = 9999;
193     pointerEvent->SetPointerAction(invalidAction);
194     std::string str = pointerEvent->DumpPointerAction();
195     EXPECT_EQ(str, "unknown");
196 }
197 
198 /**
199  * @tc.name: AddPointerItemTest1
200  * @tc.desc: Test
201  * @tc.type: FUNC
202  * @tc.require:
203  */
204 HWTEST_F(PointerEventExTest, AddPointerItemTest1, TestSize.Level2)
205 {
206     CALL_TEST_DEBUG;
207     auto pointerEvent = PointerEvent::Create();
208     ASSERT_NE(pointerEvent, nullptr);
209     PointerEvent::PointerItem item1;
210     item1.SetPointerId(0);
211     pointerEvent->AddPointerItem(item1);
212     EXPECT_EQ(pointerEvent->pointers_.size(), 1);
213     PointerEvent::PointerItem item2;
214     item2.SetPointerId(1);
215     pointerEvent->AddPointerItem(item2);
216     EXPECT_EQ(pointerEvent->pointers_.size(), 2);
217     ASSERT_TRUE(!pointerEvent->IsValid());
218     PointerEvent::PointerItem item3;
219     item3.SetPointerId(2);
220     pointerEvent->AddPointerItem(item3);
221     EXPECT_EQ(pointerEvent->pointers_.size(), 3);
222     PointerEvent::PointerItem item4;
223     item4.SetPointerId(3);
224     pointerEvent->AddPointerItem(item4);
225     EXPECT_EQ(pointerEvent->pointers_.size(), 4);
226     PointerEvent::PointerItem item5;
227     item5.SetPointerId(4);
228     pointerEvent->AddPointerItem(item5);
229     EXPECT_EQ(pointerEvent->pointers_.size(), 5);
230     PointerEvent::PointerItem item6;
231     item6.SetPointerId(5);
232     pointerEvent->AddPointerItem(item6);
233     PointerEvent::PointerItem item7;
234     item7.SetPointerId(6);
235     pointerEvent->AddPointerItem(item7);
236     PointerEvent::PointerItem item8;
237     item8.SetPointerId(7);
238     pointerEvent->AddPointerItem(item8);
239     PointerEvent::PointerItem item9;
240     item9.SetPointerId(8);
241     pointerEvent->AddPointerItem(item9);
242     PointerEvent::PointerItem item10;
243     item10.SetPointerId(9);
244     pointerEvent->AddPointerItem(item10);
245     EXPECT_EQ(pointerEvent->pointers_.size(), 10);
246     PointerEvent::PointerItem item11;
247     item11.SetPointerId(10);
248     pointerEvent->AddPointerItem(item10);
249     EXPECT_EQ(pointerEvent->pointers_.size(), 10);
250 }
251 
252 /**
253  * @tc.name: AddPointerItemTest2
254  * @tc.desc: Test
255  * @tc.type: FUNC
256  * @tc.require:
257  */
258 HWTEST_F(PointerEventExTest, AddPointerItemTest2, TestSize.Level2)
259 {
260     CALL_TEST_DEBUG;
261     auto pointerEvent = PointerEvent::Create();
262     ASSERT_NE(pointerEvent, nullptr);
263     PointerEvent::PointerItem item1;
264     item1.SetPointerId(0);
265     pointerEvent->AddPointerItem(item1);
266     EXPECT_EQ(pointerEvent->pointers_.size(), 1);
267     PointerEvent::PointerItem item2;
268     item2.SetPointerId(0);
269     pointerEvent->AddPointerItem(item2);
270     EXPECT_EQ(pointerEvent->pointers_.size(), 1);
271 }
272 
273 /**
274  * @tc.name: SetButtonPressed1
275  * @tc.desc: Test SetButtonPressed
276  * @tc.type: FUNC
277  * @tc.require:
278  */
279 HWTEST_F(PointerEventExTest, SetButtonPressed1, TestSize.Level2)
280 {
281     CALL_TEST_DEBUG;
282     auto pointerEvent = PointerEvent::Create();
283     ASSERT_NE(pointerEvent, nullptr);
284 
285     int32_t buttonId = 1;
286     pointerEvent->SetButtonPressed(buttonId);
287 
288     buttonId = 2;
289     pointerEvent->SetButtonPressed(buttonId);
290 
291     buttonId = 3;
292     pointerEvent->SetButtonPressed(buttonId);
293 
294     buttonId = 4;
295     pointerEvent->SetButtonPressed(buttonId);
296 
297     buttonId = 5;
298     pointerEvent->SetButtonPressed(buttonId);
299 
300     buttonId = 6;
301     pointerEvent->SetButtonPressed(buttonId);
302 
303     buttonId = 7;
304     pointerEvent->SetButtonPressed(buttonId);
305 
306     buttonId = 8;
307     pointerEvent->SetButtonPressed(buttonId);
308 
309     buttonId = 9;
310     pointerEvent->SetButtonPressed(buttonId);
311     EXPECT_EQ(pointerEvent->pressedButtons_.size(), 9);
312 
313     pointerEvent->SetButtonPressed(buttonId);
314     EXPECT_EQ(pointerEvent->pressedButtons_.size(), 9);
315 
316     buttonId = 10;
317     pointerEvent->SetButtonPressed(buttonId);
318     EXPECT_TRUE(pointerEvent->IsButtonPressed(buttonId));
319 
320     buttonId = 11;
321     pointerEvent->SetButtonPressed(buttonId);
322     EXPECT_FALSE(pointerEvent->IsButtonPressed(buttonId));
323 }
324 } // namespace MMI
325 } // namespace OHOS
326