github.com/Seikaijyu/gio@v0.0.1/gesture/gesture_test.go (about) 1 // SPDX-License-Identifier: Unlicense OR MIT 2 3 package gesture 4 5 import ( 6 "image" 7 "testing" 8 "time" 9 10 "github.com/Seikaijyu/gio/f32" 11 "github.com/Seikaijyu/gio/io/event" 12 "github.com/Seikaijyu/gio/io/pointer" 13 "github.com/Seikaijyu/gio/io/router" 14 "github.com/Seikaijyu/gio/op" 15 "github.com/Seikaijyu/gio/op/clip" 16 ) 17 18 func TestHover(t *testing.T) { 19 ops := new(op.Ops) 20 var h Hover 21 rect := image.Rect(20, 20, 40, 40) 22 stack := clip.Rect(rect).Push(ops) 23 h.Add(ops) 24 stack.Pop() 25 r := new(router.Router) 26 r.Frame(ops) 27 28 r.Queue( 29 pointer.Event{Kind: pointer.Move, Position: f32.Pt(30, 30)}, 30 ) 31 if !h.Update(r) { 32 t.Fatal("expected hovered") 33 } 34 35 r.Queue( 36 pointer.Event{Kind: pointer.Move, Position: f32.Pt(50, 50)}, 37 ) 38 if h.Update(r) { 39 t.Fatal("expected not hovered") 40 } 41 } 42 43 func TestMouseClicks(t *testing.T) { 44 for _, tc := range []struct { 45 label string 46 events []event.Event 47 clicks []int // number of combined clicks per click (single, double...) 48 }{ 49 { 50 label: "single click", 51 events: mouseClickEvents(200 * time.Millisecond), 52 clicks: []int{1}, 53 }, 54 { 55 label: "double click", 56 events: mouseClickEvents( 57 100*time.Millisecond, 58 100*time.Millisecond+doubleClickDuration-1), 59 clicks: []int{1, 2}, 60 }, 61 { 62 label: "two single clicks", 63 events: mouseClickEvents( 64 100*time.Millisecond, 65 100*time.Millisecond+doubleClickDuration+1), 66 clicks: []int{1, 1}, 67 }, 68 } { 69 t.Run(tc.label, func(t *testing.T) { 70 var click Click 71 var ops op.Ops 72 click.Add(&ops) 73 74 var r router.Router 75 r.Frame(&ops) 76 r.Queue(tc.events...) 77 78 events := click.Update(&r) 79 clicks := filterMouseClicks(events) 80 if got, want := len(clicks), len(tc.clicks); got != want { 81 t.Fatalf("got %d mouse clicks, expected %d", got, want) 82 } 83 84 for i, click := range clicks { 85 if got, want := click.NumClicks, tc.clicks[i]; got != want { 86 t.Errorf("got %d combined mouse clicks, expected %d", got, want) 87 } 88 } 89 }) 90 } 91 } 92 93 func mouseClickEvents(times ...time.Duration) []event.Event { 94 press := pointer.Event{ 95 Kind: pointer.Press, 96 Source: pointer.Mouse, 97 Buttons: pointer.ButtonPrimary, 98 } 99 events := make([]event.Event, 0, 2*len(times)) 100 for _, t := range times { 101 press := press 102 press.Time = t 103 release := press 104 release.Kind = pointer.Release 105 events = append(events, press, release) 106 } 107 return events 108 } 109 110 func filterMouseClicks(events []ClickEvent) []ClickEvent { 111 var clicks []ClickEvent 112 for _, ev := range events { 113 if ev.Kind == KindClick { 114 clicks = append(clicks, ev) 115 } 116 } 117 return clicks 118 }