Files
MujicaUI/chart/chart_tooltip_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

105 lines
2.9 KiB
Go

package chart
import (
"image/color"
"testing"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/internal"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
func TestChartTooltipFlipsAtEdges(t *testing.T) {
var at [2]float32
var tip ui.Rect
tt := ui.NewTester(func(c *ui.Context) {
core.Use(c, core.Settings{Mode: core.Light})
tip = ChartTooltip(c, at[0], at[1], []ChartTooltipLine{{Text: "Lauds"}, {Text: "Pews 5", Color: theme.Light().Info, Shape: 1}}).Bounds()
}, 400, 300)
at = [2]float32{50, 50}
tt.Frame()
tt.Frame()
if tip.X != 62 || tip.Y != 62 || tip.W < 40 {
t.Fatalf("tooltip at %v", tip)
}
at = [2]float32{395, 295}
tt.Frame()
tt.Frame()
if tip.X+tip.W > 395-12+0.5 || tip.Y+tip.H > 295-12+0.5 {
t.Fatalf("tooltip did not flip: %v", tip)
}
defer func() {
if recover() == nil {
t.Fatal("no panic without lines")
}
}()
ui.NewTester(func(c *ui.Context) { core.Use(c, core.Settings{}); ChartTooltip(c, 0, 0, nil) }, 100, 100)
}
func TestChartTooltipFollowsFocusAndPointer(t *testing.T) {
h := &chartHarness{build: func(c *ui.Context) ChartResult {
return LineChart(c, lineSeries(), LineChartOptions{Frame: ChartFrame{Label: "Chapel"}})
}}
tt := ui.NewTester(h.view, 520, 420)
tt.SetPreferences(ui.Preferences{ReduceMotion: true, TextScale: 1})
tabTo(t, tt, "Chapel")
tt.Key(0, ui.KeyEnd)
if !tooltipShown(tt) {
t.Fatal("no tooltip for the keyboard point")
}
tt.Key(0, ui.KeyEscape)
if tooltipShown(tt) {
t.Fatal("tooltip stayed after Escape")
}
b := h.box(tt)
tt.Move(b.X+b.W/2, b.Y+b.H/2)
if !tooltipShown(tt) {
t.Fatal("no tooltip under the pointer")
}
}
// tooltipShown finds Surface runs longer than marker rings.
func tooltipShown(tt *ui.Tester) bool {
img := tt.Image()
want := toRGBA(theme.Light().Surface)
r := img.Bounds()
for y := r.Min.Y; y < r.Max.Y; y += 2 {
run := 0
for x := r.Min.X; x < r.Max.X; x++ {
if color.RGBAModel.Convert(img.At(x, y)).(color.RGBA) == want {
if run++; run >= 30 {
return true
}
} else {
run = 0
}
}
}
return false
}
func TestChartTooltipSnapshot(t *testing.T) {
snapshot(t, "chart_tooltip", 320, 140, func(c *ui.Context) {
pal := internal.Palette(core.Tokens(c))
ChartTooltip(c, 20, 20, []ChartTooltipLine{{Text: "Lauds"}, {Text: "Pews 5", Color: pal[0]}, {Text: "Candles 2", Color: pal[1], Shape: 1}})
})
}
func TestChartTooltipFitsLongContent(t *testing.T) {
var b ui.Rect
tt := ui.NewTester(func(c *ui.Context) {
core.Use(c, core.Settings{})
lines := make([]ChartTooltipLine, 40)
for i := range lines {
lines[i].Text = "A very long series name that wraps across the tooltip panel"
}
b = ChartTooltip(c, 159, 99, lines).Bounds()
}, 160, 100)
tt.Frame()
if b.X < 4 || b.Y < 4 || b.X+b.W > 156 || b.Y+b.H > 96 {
t.Fatalf("tooltip outside window: %v", b)
}
}