Files
MujicaUI/navigation/navigation_review_test.go
T
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

126 lines
4.1 KiB
Go

package navigation
import (
"fmt"
"testing"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/icons"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/theme"
)
// TestNavigationPopupsFadeIn checks each popup fades in over PopupDuration instead of appearing at once.
func TestNavigationPopupsFadeIn(t *testing.T) {
for _, tc := range []struct {
name, open, sample string
view func(c *ui.Context)
}{
{"breadcrumb", "Show hidden path", "Alpha", func(c *ui.Context) {
Breadcrumb(c, breadcrumbPath("Root", "Alpha", "Beta", "Gamma", "Omega", "Leaf"), BreadcrumbOptions{})
}},
{"pagination", "10 / page", "50 / page", func(c *ui.Context) {
page, size := 1, 10
Pagination(c, &page, &size, PaginationOptions{Total: 100, PageSizes: []int{10, 50}})
}},
{"navigation menu", "Library", "Letters", func(c *ui.Context) {
NavigationMenu(c, ui.NewRouter("/"), navigationMenuSample(), NavigationMenuOptions{})
}},
{"menubar", "File", "Quit", func(c *ui.Context) {
Menubar(c, (&menubarApp{}).menus(), MenubarOptions{})
}},
{"command palette", "", "Esc close", func(c *ui.Context) {
open := true
CommandPalette(c, &open, commandPaletteSample(), CommandPaletteOptions{})
}},
} {
tt := ui.NewTester(func(c *ui.Context) {
core.Use(c, core.Settings{Mode: core.Light})
ui.Column(c).Fill().Padding(16).AlignItems(ui.Start).Children(func() { tc.view(c) })
}, 700, 500)
if tc.open != "" {
if err := tt.Click(tc.open); err != nil {
t.Fatal(err)
}
}
r, ok := tt.Find(tc.sample)
if !ok {
t.Fatalf("%s: no %q", tc.name, tc.sample)
}
x, y := int(r.X-3), int(r.Y+r.H/2)
// The Tester clock stands still, so a quarter of the way in is exact.
tt.Advance(theme.PopupDuration / 4)
early := tt.Image().RGBAAt(x, y)
tt.Advance(200 * time.Millisecond)
if late := tt.Image().RGBAAt(x, y); early == late {
t.Errorf("%s: popup appeared at once (%v)", tc.name, late)
}
}
}
// TestNavigationMenuLongPanelScrolls checks a tall group stays in the window and follows the focus.
func TestNavigationMenuLongPanelScrolls(t *testing.T) {
var links []NavigationLink
for i := range 30 {
links = append(links, NavigationLink{Label: fmt.Sprintf("Link %02d", i), Path: fmt.Sprintf("/l/%d", i)})
}
tt := ui.NewTester(func(c *ui.Context) {
core.Use(c, core.Settings{})
NavigationMenu(c, ui.NewRouter("/"), []NavigationLink{{Label: "Group", Items: links}}, NavigationMenuOptions{})
}, 400, 300)
tt.Key(0, ui.KeyTab)
tt.Key(0, ui.KeyDown)
tt.Key(0, ui.KeyEnd)
tt.Frame()
if !tt.Focused("Link 29") {
t.Fatal("End did not focus the last link")
}
r, _ := tt.Find("Link 29")
if r.H == 0 || r.Y+r.H > 300 {
t.Fatalf("focused link off screen at %+v", r)
}
}
// TestNavigationCaptionsFollowTextScale checks small text grows with the desktop text scale.
func TestNavigationCaptionsFollowTextScale(t *testing.T) {
for _, tc := range []struct {
text string
view func(c *ui.Context)
}{
{"Name and seal", func(c *ui.Context) {
cur := 0
Steps(c, &cur, []Step{{Title: "Account", Description: "Name and seal"}}, StepsOptions{})
}},
{"12", func(c *ui.Context) {
sel := "a"
Sidebar(c, &sel, []SidebarSection{{Title: "Mail", Items: []SidebarItem{{ID: "a", Label: "Inbox", Icon: icons.Must("inbox"), Count: 12}}}}, SidebarOptions{})
}},
{"Verse of the court", func(c *ui.Context) {
navigationMenuItem(c, NavigationLink{Label: "Poems", Path: "/p", Description: "Verse of the court"}, false, false)
}},
{"Esc close", func(c *ui.Context) {
open := true
CommandPalette(c, &open, []Command{{ID: "a", Label: "A"}}, CommandPaletteOptions{})
}},
} {
height := func(scale float32) float32 {
tt := ui.NewTester(func(c *ui.Context) {
core.Use(c, core.Settings{})
ui.Column(c).Fill().AlignItems(ui.Start).Children(func() { tc.view(c) })
}, 900, 700)
tt.SetPreferences(ui.Preferences{TextScale: scale})
tt.Frame()
r, ok := tt.Find(tc.text)
if !ok {
t.Fatalf("no %q", tc.text)
}
return r.H
}
if h1, h2 := height(1), height(2); h2 < h1*1.8 {
t.Errorf("%q: height %v at 2x text, %v at 1x", tc.text, h2, h1)
}
}
}