Skip to content

Commit e0bed35

Browse files
committed
Test gesture emission and drag payload parsing
1 parent b179786 commit e0bed35

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

AndroidSwiftUICore/Tests/AndroidSwiftUICoreTests/EvaluatorTests.swift

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,68 @@ struct ModifierTests {
187187
#expect(tapped)
188188
}
189189

190+
@Test("onLongPressGesture registers its own callback distinct from a tap")
191+
func longPress() {
192+
var tapped = false
193+
var pressed = false
194+
let host = ViewHost(
195+
Text("x")
196+
.onTapGesture { tapped = true }
197+
.onLongPressGesture { pressed = true }
198+
)
199+
let node = host.evaluate()
200+
guard case .int(let tapID)? = node.modifiers.first(where: { $0.kind == "onTapGesture" })?.args["action"],
201+
case .int(let pressID)? = node.modifiers.first(where: { $0.kind == "longPress" })?.args["action"] else {
202+
Issue.record("missing gesture callbacks"); return
203+
}
204+
#expect(tapID != pressID)
205+
host.callbacks.invokeVoid(Int64(pressID))
206+
#expect(pressed)
207+
#expect(!tapped)
208+
}
209+
210+
@Test("A drag reports translation to onChanged, then a final value to onEnded")
211+
func dragGesture() {
212+
var changes: [CGSize] = []
213+
var ended: CGSize?
214+
let host = ViewHost(
215+
Text("x").gesture(
216+
DragGesture()
217+
.onChanged { changes.append($0.translation) }
218+
.onEnded { ended = $0.translation }
219+
)
220+
)
221+
let node = host.evaluate()
222+
let drag = node.modifiers.first { $0.kind == "drag" }
223+
#expect(drag?.args["minimumDistance"] == .double(10))
224+
guard case .int(let id)? = drag?.args["action"] else {
225+
Issue.record("missing drag callback"); return
226+
}
227+
// the interpreter's payload: phase, start point, current point (in points)
228+
host.callbacks.invokeString(Int64(id), "changed;10.0,20.0;40.0,25.0")
229+
host.callbacks.invokeString(Int64(id), "ended;10.0,20.0;60.0,20.0")
230+
#expect(changes.count == 1)
231+
#expect(changes.first?.width == 30) // 40 − 10
232+
#expect(changes.first?.height == 5) // 25 − 20
233+
#expect(ended?.width == 50)
234+
#expect(ended?.height == 0)
235+
}
236+
237+
@Test("A malformed drag payload is ignored rather than crashing")
238+
func dragGestureMalformed() {
239+
var changes = 0
240+
let host = ViewHost(
241+
Text("x").gesture(DragGesture().onChanged { _ in changes += 1 })
242+
)
243+
let node = host.evaluate()
244+
guard case .int(let id)? = node.modifiers.first(where: { $0.kind == "drag" })?.args["action"] else {
245+
Issue.record("missing drag callback"); return
246+
}
247+
host.callbacks.invokeString(Int64(id), "changed;garbage")
248+
host.callbacks.invokeString(Int64(id), "")
249+
#expect(changes == 0)
250+
}
251+
190252
@Test("onAppear and onDisappear emit distinct callback kinds")
191253
func appearDisappear() {
192254
let node = ViewHost(Text("x").onAppear {}.onDisappear {}).evaluate()

0 commit comments

Comments
 (0)