• 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 
16 #include <cstdint>
17 
18 #include "../ui_input_event_test.h"
19 #include "arkoala_api.h"
20 #include "gtest/gtest.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 namespace OHOS::Ace {
25 namespace {
26 constexpr float HISTORY_ID_ONE = 1;
27 constexpr float HISTORY_ID_TWO = 2;
28 constexpr int32_t EVENT_SIZE = 2;
29 constexpr int32_t POINT_SIZE = 2;
30 
31 struct HistoryTestInputType {
HistoryTestInputTypeOHOS::Ace::__anon8938fea20111::HistoryTestInputType32     HistoryTestInputType(const ArkUI_UIInputEvent event, uint32_t pointerIndex, uint32_t historyIndex,
33         ArkUIHistoryTouchEvent* events = nullptr, uint32_t historySize = 0)
34         : event(event), pointerIndex(pointerIndex), historyIndex(historyIndex), historyEvents(events),
35           historySize(historySize) {};
36     ArkUI_UIInputEvent event;
37     uint32_t pointerIndex;
38     uint32_t historyIndex;
39     ArkUIHistoryTouchEvent* historyEvents = nullptr;
40     uint32_t historySize = 0;
41 };
42 } // namespace
43 
44 HWTEST_F(UIInputEventTest, OH_ArkUI_PointerEvent_GetHistoryPointerId_001, TestSize.Level0)
45 {
46     auto result = OH_ArkUI_PointerEvent_GetHistoryPointerId(nullptr, 0, 0);
47     EXPECT_EQ(result, 0);
48 }
49 
50 HWTEST_F(UIInputEventTest, OH_ArkUI_PointerEvent_GetHistoryPointerId_002, TestSize.Level0)
51 {
52     ArkUITouchEvent touchEvent;
53     int32_t pointerIndex = 1;
54     int32_t historyIndex = 1;
55     ArkUIAxisEvent axisEvent;
56     ArkUINodeEvent nodeEvent;
57     std::vector<std::pair<HistoryTestInputType, int32_t>> testCases = {
58         { HistoryTestInputType(ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, AXIS_EVENT_ID, &axisEvent },
59               pointerIndex, historyIndex),
60             0 },
61         { HistoryTestInputType(ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, TOUCH_EVENT_ID, &touchEvent },
62               pointerIndex, historyIndex),
63             0 },
64         { HistoryTestInputType(
65               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_MOUSE_EVENT_ID, &nodeEvent.mouseEvent },
66               pointerIndex, historyIndex),
67             0 },
68         { HistoryTestInputType(
69               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_AXIS_EVENT_ID, &nodeEvent.axisEvent },
70               pointerIndex, historyIndex),
71             0 },
72         { HistoryTestInputType(
73               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_KEY_EVENT_ID, &nodeEvent.keyEvent }, pointerIndex,
74               historyIndex),
75             0 },
76         { HistoryTestInputType(
77               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_FOCUS_AXIS_EVENT_ID, &nodeEvent.focusAxisEvent },
78               pointerIndex, historyIndex),
79             0 },
80         { HistoryTestInputType(
81               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_CLICK_EVENT_ID, &nodeEvent.clickEvent },
82               pointerIndex, historyIndex),
83             0 },
84         { HistoryTestInputType(
85               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_HOVER_EVENT_ID, &nodeEvent.hoverEvent },
86               pointerIndex, historyIndex),
87             0 },
88     };
89 
90     auto count = 0;
91     for (auto testCase : testCases) {
92         auto input = testCase.first;
93         auto expect = testCase.second;
94         auto result = OH_ArkUI_PointerEvent_GetHistoryPointerId(&input.event, input.pointerIndex, input.historyIndex);
95         EXPECT_EQ(result, expect) << "index = " << count << ",result = " << result << ",expect = " << expect;
96         count++;
97     }
98 }
99 
100 HWTEST_F(UIInputEventTest, OH_ArkUI_PointerEvent_GetHistoryPointerId_003, TestSize.Level0)
101 {
102     ArkUIHistoryTouchEvent events[EVENT_SIZE];
103     ArkUIHistoryTouchEvent emptyEvents[EVENT_SIZE];
104     ArkUITouchPoint pointes[POINT_SIZE];
105     pointes[0].id = HISTORY_ID_ONE;
106     pointes[1].id = HISTORY_ID_TWO;
107     events[0].touchPointSize = POINT_SIZE;
108     events[1].touchPointSize = POINT_SIZE;
109     events[0].touchPointes = pointes;
110     events[1].touchPointes = pointes;
111     emptyEvents[0].touchPointSize = POINT_SIZE;
112     emptyEvents[1].touchPointSize = POINT_SIZE;
113     ArkUINodeEvent nodeEvent;
114     std::vector<std::pair<HistoryTestInputType, float>> testCases = {
115         { HistoryTestInputType(ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, nullptr }, 0, 0),
116             0 }, // test when touchEvent is nullptr
117         { HistoryTestInputType(
118               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, -1, 0),
119             0 }, // test when historyIndex < 0
120         { HistoryTestInputType(
121               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 10, 0),
122             0 }, // test when historyIndex > touchEvent->historySize
123         { HistoryTestInputType(
124               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 2, 0),
125             0 }, // test when historyIndex = touchEvent->historySize
126 
127         { HistoryTestInputType(
128               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 1, 0),
129             0 }, // test when touchEvent->historyEvents = nullptr
130         { HistoryTestInputType(
131               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 1, -1,
132               emptyEvents, EVENT_SIZE),
133             0 }, // test when pointerIndex < 0
134         { HistoryTestInputType(
135               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 1, 10,
136               emptyEvents, EVENT_SIZE),
137             0 }, // test when pointerIndex > touchEvent->historyEvents[historyIndex].touchPointSize
138         { HistoryTestInputType(
139               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 1, 2,
140               emptyEvents, EVENT_SIZE),
141             0 }, // test when pointerIndex = touchEvent->historyEvents[historyIndex].touchPointSize
142         { HistoryTestInputType(
143               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 1, 1,
144               emptyEvents, EVENT_SIZE),
145             0 }, // test when (touchEvent->historyEvents[historyIndex].touchPointes) = nullptr
146         { HistoryTestInputType(
147               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 1, 1,
148               events, EVENT_SIZE),
149             HISTORY_ID_TWO }, // test when valid input with pointerIndex valid
150         { HistoryTestInputType(
151               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 1, 0,
152               emptyEvents, EVENT_SIZE),
153             0 }, // test when pointerIndex = Lower boundary,
154                  // (touchEvent->historyEvents[historyIndex].touchPointes) = nullptr
155         { HistoryTestInputType(
156               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 1, 0,
157               events, EVENT_SIZE),
158             HISTORY_ID_TWO }, // test when valid input with pointerIndex = Lower boundary
159 
160         { HistoryTestInputType(
161               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 0, 0),
162             0 }, // test when historyIndex = Lower boundary, touchEvent->historyEvents = nullptr
163         { HistoryTestInputType(
164               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 0, -1,
165               emptyEvents, EVENT_SIZE),
166             0 }, // test when historyIndex = Lower boundary, pointerIndex < 0
167         { HistoryTestInputType(
168               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 0, 10,
169               emptyEvents, EVENT_SIZE),
170             0 }, // test when historyIndex = Lower boundary, pointerIndex >
171                  // touchEvent->historyEvents[historyIndex].touchPointSize
172         { HistoryTestInputType(
173               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 0, 2,
174               emptyEvents, EVENT_SIZE),
175             0 }, // test when historyIndex = Lower boundary, pointerIndex =
176                  // touchEvent->historyEvents[historyIndex].touchPointSize
177         { HistoryTestInputType(
178               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 0, 1,
179               emptyEvents, EVENT_SIZE),
180             0 }, // test when historyIndex = Lower boundary,
181                  // (touchEvent->historyEvents[historyIndex].touchPointes) = nullptr
182         { HistoryTestInputType(
183               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 0, 1,
184               events, EVENT_SIZE),
185             HISTORY_ID_ONE }, // test when historyIndex = Lower boundary, input valid
186         { HistoryTestInputType(
187               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 0, 0,
188               emptyEvents, EVENT_SIZE),
189             0 }, // test when historyIndex = Lower boundary, pointerIndex = Lower boundary,
190                  // (touchEvent->historyEvents[historyIndex].touchPointes) = nullptr
191         { HistoryTestInputType(
192               ArkUI_UIInputEvent { ARKUI_UIINPUTEVENT_TYPE_UNKNOWN, C_TOUCH_EVENT_ID, &nodeEvent.touchEvent }, 0, 0,
193               events, EVENT_SIZE),
194             HISTORY_ID_ONE }, // test when historyIndex = Lower boundary, pointerIndex = Lower boundary, input valid
195     };
196 
197     auto count = 0;
198     for (auto testCase : testCases) {
199         auto input = testCase.first;
200         auto* touchEvent = reinterpret_cast<ArkUITouchEvent*>(input.event.inputEvent);
201         if (touchEvent) {
202             touchEvent->historyEvents = input.historyEvents;
203             touchEvent->historySize = input.historySize;
204         }
205         auto expect = testCase.second;
206         auto result = OH_ArkUI_PointerEvent_GetHistoryPointerId(&input.event, input.pointerIndex, input.historyIndex);
207         EXPECT_EQ(result, expect) << "index = " << count << ",result = " << result << ",expect = " << expect;
208         count++;
209     }
210 }
211 } // namespace OHOS::Ace