Files
MujicaUI/data/carousel_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

173 lines
4.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package data
import (
"fmt"
"testing"
"time"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/MujicaUI/core"
)
type carouselHarness struct {
index int
opts CarouselOptions
changes int
}
func (h *carouselHarness) view(c *ui.Context) {
core.Use(c, core.Settings{})
ui.Column(c).Padding(10).Size(400, 260).Children(func() {
v := Carousel(c, &h.index, 4, h.opts, func(i int) {
ui.Text(c, fmt.Sprintf("Window %d", i+1)).Padding(20)
})
v.Element.Grow(1)
if v.Changed() {
h.changes++
}
})
}
func TestCarouselButtonsAndDots(t *testing.T) {
h := &carouselHarness{}
tt := ui.NewTester(h.view, 420, 280)
_ = tt.Click("Previous slide")
if h.index != 0 || h.changes != 0 {
t.Fatal("previous moved past the first slide")
}
_ = tt.Click("Next slide")
if h.index != 1 || !tt.HasText("Window 2") || h.changes != 1 {
t.Fatalf("next: %d changes %d %q", h.index, h.changes, tt.Texts())
}
_ = tt.Click("Slide 4 of 4")
if h.index != 3 {
t.Fatalf("dot: %d", h.index)
}
_ = tt.Click("Next slide")
if h.index != 3 {
t.Fatal("next moved past the last slide without Loop")
}
h.opts.Loop = true
tt.Frame()
_ = tt.Click("Next slide")
if h.index != 0 {
t.Fatalf("loop: %d", h.index)
}
}
func TestCarouselKeyboard(t *testing.T) {
h := &carouselHarness{}
tt := ui.NewTester(h.view, 420, 280)
tt.Key(0, ui.KeyTab) // previous is disabled: focus lands on next
tt.Key(0, ui.KeyRight)
tt.Key(0, ui.KeyRight)
if h.index != 2 {
t.Fatalf("Right×2: %d", h.index)
}
tt.Key(0, ui.KeyEnd)
if h.index != 3 {
t.Fatalf("End: %d", h.index)
}
tt.Key(0, ui.KeyHome)
tt.Key(0, ui.KeyLeft)
if h.index != 0 {
t.Fatalf("Home, Left: %d", h.index)
}
if got := tt.Announcements(); len(got) == 0 || got[len(got)-1] != "Slide 1 of 4" {
t.Fatalf("announcements %q", got)
}
}
func TestCarouselNoAutoplayByDefault(t *testing.T) {
h := &carouselHarness{opts: CarouselOptions{Interval: 10 * time.Millisecond}}
tt := ui.NewTester(h.view, 420, 280)
tt.Advance(30 * time.Millisecond)
if h.index != 0 || tt.HasText("Pause autoplay") {
t.Fatalf("advanced without Autoplay: %d", h.index)
}
}
func TestCarouselAutoplayPause(t *testing.T) {
h := &carouselHarness{opts: CarouselOptions{Autoplay: true, Interval: 20 * time.Millisecond}}
tt := ui.NewTester(h.view, 420, 280)
tt.Advance(30 * time.Millisecond)
if h.index != 1 {
t.Fatalf("autoplay did not advance: %d", h.index)
}
_ = tt.Click("Pause autoplay")
tt.Move(1, 1) // away from the carousel
tt.SetFocused(false)
tt.SetFocused(true)
at := h.index
tt.Advance(50 * time.Millisecond)
if h.index != at || !tt.HasText("") {
t.Fatalf("paused carousel advanced: %d -> %d", at, h.index)
}
if _, ok := tt.Find("Resume autoplay"); !ok {
t.Fatal("no resume button")
}
r, _ := tt.Find("Window " + fmt.Sprint(at+1))
_ = tt.Click("Resume autoplay")
tt.Move(r.X+2, r.Y+2) // hover pauses too
tt.Advance(50 * time.Millisecond)
if h.index != at {
t.Fatalf("hovered carousel advanced: %d -> %d", at, h.index)
}
}
func TestCarouselSnapshot(t *testing.T) {
snapshot(t, "carousel", 440, 240, func(c *ui.Context) {
i := ui.Local(c.Root(), "i", func() *int { v := 1; return &v })
ui.Column(c).Padding(16).Size(440, 240).Children(func() {
Carousel(c, *i, 4, CarouselOptions{Autoplay: true, Label: "Windows"}, func(n int) {
ui.Column(c).Grow(1).Center().Children(func() {
ui.Text(c, fmt.Sprintf("Rose window %d", n+1)).FontSize(core.FontSize(c, 18))
})
}).Element.Grow(1)
})
})
}
func TestCarouselEmptyAutoplayKeys(t *testing.T) {
index := 0
tt := ui.NewTester(func(c *ui.Context) {
core.Use(c, core.Settings{})
Carousel(c, &index, 0, CarouselOptions{Autoplay: true}, func(int) {}).Element.Size(300, 200)
}, 320, 240)
for range 4 {
tt.Key(0, ui.KeyTab)
tt.Key(0, ui.KeyRight)
tt.Key(0, ui.KeyLeft)
}
if index != 0 {
t.Fatalf("index %d", index)
}
}
func TestCarouselDotsFollowArrows(t *testing.T) {
h := &carouselHarness{}
tt := ui.NewTester(h.view, 420, 280)
for range 10 {
if tt.Focused("Slide 1 of 4") {
break
}
tt.Key(0, ui.KeyTab)
}
if !tt.Focused("Slide 1 of 4") {
t.Fatal("Tab never reached the first dot")
}
tt.Key(0, ui.KeyRight)
if h.index != 1 || !tt.Focused("Slide 2 of 4") {
t.Fatalf("Right: index %d, focus on dot 2 %v", h.index, tt.Focused("Slide 2 of 4"))
}
tt.Key(0, ui.KeyEnter)
if h.index != 1 {
t.Fatalf("Enter on the focused dot moved to %d", h.index)
}
tt.Key(0, ui.KeyEnd)
if h.index != 3 || !tt.Focused("Slide 4 of 4") {
t.Fatalf("End: index %d", h.index)
}
}