Add a test for clicks that wait in the event queue

If the main loop is busy, clicks wait in the event queue and are then
handled right after each other. gocui decides whether two clicks form a
double click by the time it handles them, so two clicks read more than
500ms apart count as a double click.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Stefan Haller
2026-10-14 14:15:25 +02:00
co-authored by Claude Opus 5.5
parent 0855e981cf
commit 5bc855415a
+40
View File
@@ -2,6 +2,7 @@ package gocui
import (
"testing"
"time"
"github.com/gdamore/tcell/v3"
"github.com/stretchr/testify/assert"
@@ -66,3 +67,42 @@ func TestASwallowedClickIsNoHalfOfADoubleClick(t *testing.T) {
assert.Equal(t, []bool{false}, doubleClicks)
}
func TestDoubleClicksAreTimedByWhenTheClicksWereRead(t *testing.T) {
t.Cleanup(resetMouseState)
resetMouseState()
g := newTestGui(t)
view, _ := g.SetView("list", 0, 0, 20, 10, 0)
doubleClicks := []bool{}
g.SetViewClickBinding(&ViewMouseBinding{
ViewName: "list",
Key: MouseLeft,
Handler: func(opts ViewMouseBindingOpts) error {
doubleClicks = append(doubleClicks, opts.IsDoubleClick)
return nil
},
})
click := func(readAt time.Time) {
for _, buttons := range []tcell.ButtonMask{tcell.ButtonPrimary, tcell.ButtonNone} {
tev := tcell.NewEventMouse(view.x0+1, view.y0+1, buttons, tcell.ModNone)
tev.SetEventTime(readAt)
event := gocuiEventFromTcellEvent(tev)
assert.NoError(t, g.onKey(&event))
}
}
// The clicks are handled right after each other, long after they were read,
// as they are when they wait in the queue while the main loop is busy.
readAt := time.Now().Add(-time.Minute)
click(readAt)
click(readAt.Add(DOUBLE_CLICK_THRESHOLD + time.Millisecond))
click(readAt.Add(DOUBLE_CLICK_THRESHOLD + 100*time.Millisecond))
assert.Equal(t,
/* EXPECTED:
[]bool{false, false, true},
ACTUAL: */
[]bool{false, true, true},
doubleClicks)
}