Files
gio/gesture/gesture_test.go
T
Elias Naur ef8171b971 io: [API] introduce event filters; convert pointer input to use them
Instead of having to supply the predicates for event filtering at the
time of layout, the new Filter type allows widgets to filter at the time
of calling Source.Events. There is then only the need for a single input
op type, in package event.

Filters most importantly allow the use of one tag for several event types,
and we can define that a widget w has &w as its primary tag, by convention.
This allows the replacement of per-widget Focus methods with direct uses
of FocusCmd{&w}, and the later addition of Source.Focused(&w) queries.

Note that the TestCursor test needed restructuring to avoid its use of
InputOps.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2024-02-05 10:59:51 +00:00

121 lines
2.5 KiB
Go

// SPDX-License-Identifier: Unlicense OR MIT
package gesture
import (
"image"
"testing"
"time"
"gioui.org/f32"
"gioui.org/io/event"
"gioui.org/io/input"
"gioui.org/io/pointer"
"gioui.org/op"
"gioui.org/op/clip"
)
func TestHover(t *testing.T) {
ops := new(op.Ops)
var h Hover
rect := image.Rect(20, 20, 40, 40)
stack := clip.Rect(rect).Push(ops)
h.Add(ops)
stack.Pop()
r := new(input.Router)
h.Update(r.Source())
r.Frame(ops)
r.Queue(
pointer.Event{Kind: pointer.Move, Position: f32.Pt(30, 30)},
)
if !h.Update(r.Source()) {
t.Fatal("expected hovered")
}
r.Queue(
pointer.Event{Kind: pointer.Move, Position: f32.Pt(50, 50)},
)
if h.Update(r.Source()) {
t.Fatal("expected not hovered")
}
}
func TestMouseClicks(t *testing.T) {
for _, tc := range []struct {
label string
events []event.Event
clicks []int // number of combined clicks per click (single, double...)
}{
{
label: "single click",
events: mouseClickEvents(200 * time.Millisecond),
clicks: []int{1},
},
{
label: "double click",
events: mouseClickEvents(
100*time.Millisecond,
100*time.Millisecond+doubleClickDuration-1),
clicks: []int{1, 2},
},
{
label: "two single clicks",
events: mouseClickEvents(
100*time.Millisecond,
100*time.Millisecond+doubleClickDuration+1),
clicks: []int{1, 1},
},
} {
t.Run(tc.label, func(t *testing.T) {
var click Click
var ops op.Ops
click.Add(&ops)
var r input.Router
click.Update(r.Source())
r.Frame(&ops)
r.Queue(tc.events...)
events := click.Update(r.Source())
clicks := filterMouseClicks(events)
if got, want := len(clicks), len(tc.clicks); got != want {
t.Fatalf("got %d mouse clicks, expected %d", got, want)
}
for i, click := range clicks {
if got, want := click.NumClicks, tc.clicks[i]; got != want {
t.Errorf("got %d combined mouse clicks, expected %d", got, want)
}
}
})
}
}
func mouseClickEvents(times ...time.Duration) []event.Event {
press := pointer.Event{
Kind: pointer.Press,
Source: pointer.Mouse,
Buttons: pointer.ButtonPrimary,
}
events := make([]event.Event, 0, 2*len(times))
for _, t := range times {
press := press
press.Time = t
release := press
release.Kind = pointer.Release
events = append(events, press, release)
}
return events
}
func filterMouseClicks(events []ClickEvent) []ClickEvent {
var clicks []ClickEvent
for _, ev := range events {
if ev.Kind == KindClick {
clicks = append(clicks, ev)
}
}
return clicks
}