Files
MujicaUI/overlay/hover_card_test.go
ZacharyZhang-NYandClaude Opus 5.5 bcce34cda1 Upgrade MyGo to v0.5.5
MyGo v0.5.5 returns ui.Element as a checked value handle instead of a
pointer. Migrated with `mygo migrate-ui`, then fixed what the codemod
leaves to review:

- Element nil checks become .Valid(); absent elements are ui.Element{}.
- Input handlers and paint callbacks run outside the build pass, where
  elements expire and Context methods are inert: read IsDisabled into a
  local during the build, keep a ui.Handle for Focus, use c.Services()
  for clipboard access, create ui.Local state during the build.
- Add core.AutoFocus: v0.5.5 records an overlay's opener after its
  content builds, so same-frame autofocus lost focus restore on close.
  It focuses one frame later; cascader rows defer their focus likewise.
- core.OverlayLayer identifies build passes by the root element, not
  c.Now(), which repeats across passes and under the Tester clock.
- Context menus survive the opening press counted as PressedOutside;
  split button tooltip no longer reorders its open menu; disabled
  menubar rows pass presses to the panel; Form defers blur-started
  async checks so the growing message no longer moves Submit away
  from the release; Combobox drops its disabled copy now that MyGo
  blocks typing under a disabled wrapper (canary flipped to a guard).
- The Tester clock is virtual: tests use Advance/Now instead of
  time.Sleep/time.Now and read element state inside the view. The web
  mirror advances the clock to wall time before each frame and input.
- AGENTS.md and docs describe the value Element API and the new pin.

Snapshot goldens are darwin-only and were not re-verified here.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 01:37:54 -04:00

178 lines
5.2 KiB
Go

package overlay
import (
"testing"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/input"
)
const hoverCardTestDelay = 150 * time.Millisecond
type hoverCardApp struct {
follows, next int
inDialog bool
}
func (a *hoverCardApp) card(c *ui.Context) {
b := input.Button(c, "Lady Sakiko", input.ButtonOptions{Variant: input.Ghost})
HoverCard(c, b, HoverCardOptions{OpenDelay: hoverCardTestDelay, CloseDelay: hoverCardTestDelay}, func() {
ui.Text(c, "Keyboardist of the court")
if input.Button(c, "Follow", input.ButtonOptions{}).Clicked() {
a.follows++
}
})
if input.Button(c, "Next", input.ButtonOptions{}).Clicked() {
a.next++
}
}
func (a *hoverCardApp) view(c *ui.Context) {
core.Use(c, core.Settings{})
if a.inDialog {
open := true
Dialog(c, &open, DialogOptions{Title: "Court", HideClose: true}, func() {
ui.Column(c).Gap(80).AlignItems(ui.Start).Children(func() { a.card(c) })
})
return
}
ui.Column(c).Padding(40).Gap(80).AlignItems(ui.Start).Children(func() { a.card(c) })
}
func hoverCardShown(tt *ui.Tester) func() bool {
return func() bool { return tt.HasText("Keyboardist of the court") }
}
// hoverCardSettle waits past both delays so any pending open or close has happened.
func hoverCardSettle(tt *ui.Tester) {
tt.Advance(hoverCardTestDelay + 30*time.Millisecond)
}
func TestHoverCardPointerCanEnterCard(t *testing.T) {
a := &hoverCardApp{}
tt := ui.NewTester(a.view, 500, 400)
r, _ := tt.Find("Lady Sakiko")
start := time.Now()
tt.Move(r.X+5, r.Y+5)
if time.Since(start) < hoverCardTestDelay && tt.HasText("Keyboardist of the court") {
t.Fatal("opened before the delay")
}
if !overlayUntil(tt, hoverCardShown(tt)) {
t.Fatalf("card did not open: %q", tt.Texts())
}
// The pointer inside the card keeps it open past the close delay.
k, _ := tt.Find("Keyboardist of the court")
tt.Move(k.X+5, k.Y+5)
hoverCardSettle(tt)
if !tt.HasText("Follow") {
t.Fatal("card closed with the pointer inside it")
}
if err := tt.Click("Follow"); err != nil {
t.Fatal(err)
}
if a.follows != 1 || !tt.Focused("Follow") {
t.Fatalf("follows %d, Follow focused %v", a.follows, tt.Focused("Follow"))
}
// With the focus inside, leaving keeps it; a click outside closes it.
tt.Move(490, 390)
hoverCardSettle(tt)
if !tt.HasText("Follow") {
t.Fatal("card closed with the focus inside it")
}
tt.ClickAt(490, 390)
if tt.HasText("Follow") {
t.Fatal("outside click did not close the card")
}
}
func TestHoverCardCloseDelay(t *testing.T) {
a := &hoverCardApp{}
tt := ui.NewTester(a.view, 500, 400)
r, _ := tt.Find("Lady Sakiko")
tt.Move(r.X+5, r.Y+5)
if !overlayUntil(tt, hoverCardShown(tt)) {
t.Fatal("card did not open")
}
start := time.Now()
tt.Move(490, 390)
if time.Since(start) < hoverCardTestDelay && !tt.HasText("Follow") {
t.Fatal("closed without the close delay")
}
if !overlayUntil(tt, func() bool { return !tt.HasText("Follow") }) {
t.Fatal("card stayed open after the close delay")
}
}
func TestHoverCardKeyboardEntersAndEscapes(t *testing.T) {
a := &hoverCardApp{}
tt := ui.NewTester(a.view, 500, 400)
tt.Key(0, ui.KeyTab)
if !overlayUntil(tt, hoverCardShown(tt)) {
t.Fatal("no card on keyboard focus")
}
tt.Key(0, ui.KeyTab)
if !tt.Focused("Follow") {
t.Fatal("Tab did not enter the card")
}
tt.Key(0, ui.KeyEnter)
if a.follows != 1 {
t.Fatalf("follows %d", a.follows)
}
tt.Key(0, ui.KeyEscape)
if tt.HasText("Follow") || !tt.Focused("Lady Sakiko") {
t.Fatalf("Escape: card shown %v, anchor focused %v", tt.HasText("Follow"), tt.Focused("Lady Sakiko"))
}
hoverCardSettle(tt)
if tt.HasText("Follow") {
t.Fatal("dismissed card came back while focused")
}
tt.Key(0, ui.KeyTab)
if !tt.Focused("Next") || a.next != 0 {
t.Fatal("Tab after the card did not reach Next")
}
}
func TestHoverCardInsideDialog(t *testing.T) {
a := &hoverCardApp{inDialog: true}
tt := ui.NewTester(a.view, 600, 500)
r, _ := tt.Find("Lady Sakiko")
tt.Move(r.X+5, r.Y+5)
if !overlayUntil(tt, hoverCardShown(tt)) {
t.Fatal("card did not open in the dialog")
}
if err := tt.Click("Follow"); err != nil {
t.Fatal(err)
}
if a.follows != 1 || !tt.Focused("Follow") {
t.Fatalf("follows %d, focus kept on Follow %v", a.follows, tt.Focused("Follow"))
}
tt.Key(ui.Shift, ui.KeyTab)
if !tt.Focused("Lady Sakiko") {
t.Fatal("Shift+Tab from the card did not reach its anchor in the dialog")
}
}
func TestHoverCardSnapshot(t *testing.T) {
snapshotAfter(t, "hover_card", 420, 240, func(c *ui.Context) {
ui.Column(c).Padding(24).AlignItems(ui.Start).Children(func() {
b := input.Button(c, "Lady Uika", input.ButtonOptions{Variant: input.Ghost})
HoverCard(c, b, HoverCardOptions{OpenDelay: time.Millisecond}, func() {
ui.Column(c).Gap(8).AlignItems(ui.Start).Children(func() {
ui.Text(c, "Lady Uika").FontWeight(600)
ui.Text(c, "Vocalist and guitarist; keeps the ledger of the masquerade.").TextColor(core.Tokens(c).TextMuted)
input.Button(c, "Follow", input.ButtonOptions{Variant: input.Outline})
})
})
})
}, func(tt *ui.Tester) {
r, _ := tt.Find("Lady Uika")
tt.Move(r.X+5, r.Y+5)
if !overlayUntil(tt, func() bool { return tt.HasText("Follow") }) {
t.Fatal("card did not open")
}
})
}