Pad/internal/test/e2e/cursor_interaction_test.go
Greg Pomerantz bef3929f40 fix: update tests and fix SetCursorFromPoint panic
- Rewrite cursor_test.go to use the current GlyphLayout-based API
  instead of the deleted byteOffsetToLineCol function
- Add guard for empty/missing layout slices in SetCursorFromPoint
  to prevent index-out-of-range panic on empty documents
- Add missing Advance field to e2e test GlyphLayout initialization
- Add cmd/pad/pad binary to .gitignore
2026-06-04 07:24:44 -04:00

65 lines
1.9 KiB
Go

package e2e_test
import (
"testing"
"time"
"pad/internal/editor"
"pad/internal/test/e2e"
"pad/internal/ui"
)
// TestEditorClickToMoveCursor tests that clicking/tapping in the editor moves the cursor.
func TestEditorClickToMoveCursor(t *testing.T) {
h := e2e.NewHarnessWithDefaults()
defer h.Cleanup()
// Switch to editor page and load some text
editor.GoToEditor(nil)
h.SendConfig(780, 1688)
// Wait for frame to ensure page switch
_, err := e2e.WaitForNewFrame(h, h.FrameCount(), 5*time.Second)
if err != nil {
t.Fatalf("timeout waiting for frame: %v", err)
}
// Initialize GlyphLayout for the test
editor.TheState.Editor.GlyphLayout = ui.GlyphLayout{
ByteOffsets: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
X: []ui.Dp{10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120},
Y: []ui.Dp{70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70, 70},
Advance: []ui.Dp{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
}
// Initial cursor position should be 0 (or end of text, depending on implementation)
// For this test, let's assume it starts at 0.
if editor.TheState.Editor.CursorPosition != 0 {
t.Errorf("expected initial cursor 0, got %d", editor.TheState.Editor.CursorPosition)
}
// Simulate tap at a position that should move the cursor.
// We use SendInput to simulate the tap event handler for the editor's text field.
h.SendInput([]ui.InputEvent{
{
Handler: func(data any) {
if pt, ok := data.(ui.Point); ok {
editor.SetCursorFromPoint(float64(pt.X), float64(pt.Y))
}
},
Data: ui.Point{X: 30, Y: 70},
},
})
// Wait for potential re-render
_, err = e2e.WaitForNewFrame(h, h.FrameCount(), 1*time.Second)
if err != nil {
t.Logf("no new frame after tap, checking state anyway")
}
// Assert cursor moved
if editor.TheState.Editor.CursorPosition == 0 {
t.Errorf("expected cursor to move from 0, but it remained at 0")
}
}