package ui import ( "math" "slices" "testing" "github.com/egoist/mygo/internal/platform" ) // focusedOf returns which of labels has the focus, "" for none. func focusedOf(tt *Tester, labels ...string) string { for _, l := range labels { if tt.Focused(l) { return l } } return "" } // tabs presses Tab (or Shift+Tab) n times and returns where the focus // went each time. func tabs(tt *Tester, mods Modifiers, n int, labels ...string) []string { var got []string for range n { tt.Key(mods, KeyTab) got = append(got, focusedOf(tt, labels...)) } return got } func TestFocusGroupIsOneTabStop(t *testing.T) { bold := false labels := []string{"Before", "Cut", "Bold", "Paste", "After"} tt := coreNewTester(func(c *context) { coreColumn(c).Padding(10).Gap(8).Children(func() { coreButton(c, "Before") coreToolbar(c, func() { coreButton(c, "Cut") coreToggle(c, &bold, "Bold") coreButton(c, "Paste") }).Label("Edit") coreButton(c, "After") }) }, 500, 300) if got := tabs(tt, 0, 4, labels...); !slices.Equal(got, []string{"Before", "Cut", "After", "Before"}) { t.Fatalf("Tab went %q", got) } tt.Key(0, KeyTab) // into the toolbar var arrows []string for _, k := range []Key{KeyRight, KeyRight, KeyRight, KeyLeft, KeyEnd, KeyHome, KeyEnd} { tt.Key(0, k) arrows = append(arrows, focusedOf(tt, labels...)) } if want := []string{"Bold", "Paste", "Cut", "Paste", "Paste", "Cut", "Paste"}; !slices.Equal(arrows, want) { t.Fatalf("the arrows went %q, not %q", arrows, want) } // Tab leaves the toolbar, and comes back where it left it. if got := tabs(tt, 0, 1, labels...); got[0] != "After" { t.Fatalf("Tab from the toolbar went to %q", got[0]) } if got := tabs(tt, Shift, 1, labels...); got[0] != "Paste" { t.Errorf("Shift+Tab back into the toolbar went to %q", got[0]) } if got := tabs(tt, Shift, 1, labels...); got[0] != "Before" { t.Errorf("Shift+Tab out of the toolbar went to %q", got[0]) } } // TestTabStopFalse checks that an element of TabStop(false) takes the // focus from a click and from Focus while Tab passes over it, as HTML's // tabindex="-1" (#1308). func TestTabStopFalse(t *testing.T) { var card *node focus := false labels := []string{"Before", "Inside", "After"} tt := coreNewTester(func(c *context) { coreColumn(c).Padding(10).Gap(8).Children(func() { coreButton(c, "Before") card = coreColumn(c).Padding(20).TabStop(false).Label("Card") card.Children(func() { coreButton(c, "Inside") }) if focus { card.Focus() } coreButton(c, "After") }) }, 400, 300) if got := tabs(tt, 0, 4, labels...); !slices.Equal(got, []string{"Before", "Inside", "After", "Before"}) { t.Fatalf("Tab went %q", got) } if got := tabs(tt, Shift, 3, labels...); !slices.Equal(got, []string{"After", "Inside", "Before"}) { t.Fatalf("Shift+Tab went %q", got) } // A press on it gives it the focus, and Tab goes on into it. r, ok := tt.Find("Card") if !ok { t.Fatal("no card") } tt.ClickAt(r.X+5, r.Y+5) if tt.rt.focused != card.id { t.Fatal("a click did not focus the card") } if got := tabs(tt, 0, 1, labels...); got[0] != "Inside" { t.Errorf("Tab from the card went to %q", got[0]) } focus = true tt.Frame() focus = false if tt.rt.focused != card.id { t.Fatal("Focus did not focus the card") } if got := tabs(tt, Shift, 1, labels...); got[0] != "Before" { t.Errorf("Shift+Tab from the card went to %q", got[0]) } tt.send(platform.SurfaceEvent{Kind: platform.AccessibilityOn}) if n := accessNode(t, tt.h.access, platform.RoleGroup, "Card"); n.States&platform.AccessFocusable == 0 { t.Errorf("the card is not focusable to assistive technology: %+v", n) } // TabStop(true) is a tab stop again. tt = coreNewTester(func(c *context) { coreColumn(c).Children(func() { coreButton(c, "Before") card = coreColumn(c).Padding(20).TabStop(false).TabStop(true).Label("Card") card.Children(func() { coreButton(c, "Inside") }) }) }, 400, 300) tabs(tt, 0, 2) if tt.rt.focused != card.id { t.Error("Tab did not stop at TabStop(true)") } } // TestFocusGroupEntersPastTabStopFalse checks that Tab enters a focus // group at a member it stops at, not at one of TabStop(false). func TestFocusGroupEntersPastTabStopFalse(t *testing.T) { labels := []string{"Before", "Cut", "Copy", "Paste", "After"} tt := coreNewTester(func(c *context) { coreColumn(c).Padding(10).Gap(8).Children(func() { coreButton(c, "Before") coreToolbar(c, func() { coreButton(c, "Cut").TabStop(false) coreButton(c, "Copy") coreButton(c, "Paste") }).Label("Edit") coreButton(c, "After") }) }, 500, 300) if got := tabs(tt, 0, 3, labels...); !slices.Equal(got, []string{"Before", "Copy", "After"}) { t.Fatalf("Tab went %q", got) } // The arrows still reach it, and Tab comes back into the group where // it stops. if got := tabs(tt, Shift, 1, labels...); got[0] != "Copy" { t.Fatalf("Shift+Tab went to %q", got[0]) } tt.Key(0, KeyLeft) if !tt.Focused("Cut") { t.Fatal("Left did not go to Cut") } tabs(tt, 0, 1) if got := tabs(tt, Shift, 1, labels...); got[0] != "Copy" { t.Errorf("Shift+Tab back into the toolbar went to %q", got[0]) } } func TestFocusGroupLeavesKeysToWhatTakesThem(t *testing.T) { name, volume := "abc", 0.5 tt := coreNewTester(func(c *context) { coreToolbar(c, func() { coreButton(c, "Cut") coreTextInput(c, &name).Label("Name").Width(100) coreSlider(c, &volume, 0, 1).Label("Volume").Width(100) coreButton(c, "Paste") }) }, 600, 200) tt.Key(0, KeyTab) tt.Key(0, KeyRight) if !tt.Focused("Name") { t.Fatal("Right from Cut did not go to the input") } tt.Key(0, KeyLeft) // the caret moves if !tt.Focused("Name") { t.Error("Left in the input left it") } tt.send(platform.SurfaceEvent{Kind: platform.AccessibilityOn}) tt.rt.focused = accessNode(t, tt.h.access, platform.RoleSlider, "Volume").ID tt.Frame() tt.Key(0, KeyRight) if !tt.Focused("Volume") || volume <= 0.5 { t.Errorf("Right on the slider moved the focus, or not the slider (%v)", volume) } } func TestRadioGroupArrowsChoose(t *testing.T) { size := "Medium" labels := []string{"Before", "Small", "Medium", "Large", "After"} tt := coreNewTester(func(c *context) { coreColumn(c).Padding(10).Gap(8).Children(func() { coreButton(c, "Before") coreRadioGroup(c, func() { for _, s := range []string{"Small", "Medium", "Large"} { coreRadio(c, &size, s, s) } }).Label("Size") coreButton(c, "After") }) }, 400, 300) // Tab goes to the one chosen. if got := tabs(tt, 0, 2, labels...); !slices.Equal(got, []string{"Before", "Medium"}) { t.Fatalf("Tab went %q", got) } tt.Key(0, KeyDown) if size != "Large" || !tt.Focused("Large") { t.Fatalf("Down chose %q", size) } tt.Key(0, KeyDown) // round to the first tt.Key(0, KeyRight) if size != "Medium" { t.Errorf("Down, Right chose %q", size) } if got := tabs(tt, 0, 1, labels...); got[0] != "After" { t.Errorf("Tab went to %q", got[0]) } tt.send(platform.SurfaceEvent{Kind: platform.AccessibilityOn}) g := accessNode(t, tt.h.access, platform.RoleRadioGroup, "Size") n := 0 for _, x := range tt.h.access.Nodes { if x.Role == platform.RoleRadio && tt.h.access.Nodes[x.Parent].ID == g.ID { n++ } } if n != 3 { t.Errorf("the radio group holds %d radio buttons", n) } } func TestRadioGroupEntersAtTheOneChosen(t *testing.T) { value := "" labels := []string{"a", "b", "c", "After"} tt := coreNewTester(func(c *context) { coreColumn(c).Padding(10).Gap(8).Children(func() { coreRadioGroup(c, func() { for _, v := range []string{"a", "b", "c"} { coreRadioBase(c, &value, v).Children(func() { coreText(c, v) }) } }).Label("Letters") coreButton(c, "After") }) }, 400, 300) tt.Click("a") if got := tabs(tt, 0, 1, labels...); value != "a" || got[0] != "After" { t.Fatalf("chose %q, then Tab went to %q", value, got[0]) } value = "c" // as another control would tt.Frame() if got := tabs(tt, Shift, 1, labels...); got[0] != "c" { t.Errorf("Shift+Tab back into the group went to %q, not to the one chosen", got[0]) } } func TestToggle(t *testing.T) { bold, changes := false, 0 tt := coreNewTester(func(c *context) { if coreToggle(c, &bold, "Bold").Changed() { changes++ } }, 300, 200) tt.Click("Bold") if !bold || changes != 1 { t.Fatalf("a click: bold %v, %d changes", bold, changes) } tt.send(platform.SurfaceEvent{Kind: platform.AccessibilityOn}) n := accessNode(t, tt.h.access, platform.RoleToggleButton, "Bold") if n.States&platform.AccessChecked == 0 { t.Errorf("pressed, it reads %+v", n) } tt.Key(0, KeySpace) if bold { t.Error("Space did not let it go") } } func TestSegmented(t *testing.T) { view, changes := 0, 0 tt := coreNewTester(func(c *context) { if coreSegmented(c, &view, "List", "Grid", "Columns").Label("View").Changed() { changes++ } }, 400, 200) tt.Click("Grid") if view != 1 || changes != 1 { t.Fatalf("a click on Grid: view %d, %d changes", view, changes) } tt.Key(0, KeyRight) if view != 2 || !tt.Focused("Columns") { t.Errorf("Right chose %d", view) } tt.send(platform.SurfaceEvent{Kind: platform.AccessibilityOn}) accessNode(t, tt.h.access, platform.RoleRadioGroup, "View") if n := accessNode(t, tt.h.access, platform.RoleRadio, "Columns"); n.States&platform.AccessChecked == 0 { t.Errorf("Columns reads %+v", n) } } // FillEqually stretches a segmented control across its parent and shares // the width equally among its segments, which choose, take the keys and // read as they do otherwise. func TestSegmentedFillEqually(t *testing.T) { view, changes, even := 0, 0, true off := false tt := coreNewTester(func(c *context) { coreColumn(c).Fill().Padding(20).Children(func() { seg := coreSegmented(c, &view, "Light", "Dark", "System settings") if even { seg.FillEqually() } if seg.Label("Appearance").Changed() { changes++ } g := coreToggleGroup(c, func() { coreToggle(c, &off, "Off") coreToggle(c, &off, "On, always") }).Label("Mode") if even { g.FillEqually() } }) }, 520, 200) bounds := func(role platform.AccessRole, labels ...string) []platform.RectF { t.Helper() tt.send(platform.SurfaceEvent{Kind: platform.AccessibilityOn}) var rs []platform.RectF for _, l := range labels { rs = append(rs, accessNode(t, tt.h.access, role, l).Bounds) } return rs } segs := bounds(platform.RoleRadio, "Light", "Dark", "System settings") track := bounds(platform.RoleRadioGroup, "Appearance")[0] if track.X != 20 || track.W != 480 { t.Fatalf("the track is %+v, not across the column", track) } for i, r := range segs { if math.Abs(r.W-segs[0].W) > 0.5 { t.Errorf("segment %d is %v wide, the first %v", i, r.W, segs[0].W) } } if end := segs[2].X + segs[2].W; track.X+track.W-end > 4 { t.Errorf("the last segment ends at %v, the track at %v", end, track.X+track.W) } // The label sits in the middle of its share. dark, _ := tt.Find("Dark") if mid := float64(dark.X + dark.W/2); math.Abs(mid-(segs[1].X+segs[1].W/2)) > 1 { t.Errorf("Dark's label is centered at %v in %+v", mid, segs[1]) } toggles := bounds(platform.RoleToggleButton, "Off", "On, always") if math.Abs(toggles[0].W-toggles[1].W) > 0.5 || toggles[0].W < 200 { t.Errorf("the toggles are %v and %v wide", toggles[0].W, toggles[1].W) } // Clicks, keys and assistive technology as without it. tt.ClickAt(float32(segs[1].X+segs[1].W-6), float32(segs[1].Y+segs[1].H/2)) if view != 1 || changes != 1 { t.Fatalf("a click near the end of Dark's share: view %d, %d changes", view, changes) } tt.Key(0, KeyRight) if view != 2 || !tt.Focused("System settings") { t.Errorf("Right chose %d", view) } if n := accessNode(t, tt.h.access, platform.RoleRadio, "System settings"); n.States&platform.AccessChecked == 0 { t.Errorf("System settings reads %+v", n) } // Without it, the segments are as wide as their labels. even = false tt.Frame() segs = bounds(platform.RoleRadio, "Light", "Dark", "System settings") if segs[2].W <= segs[1].W+20 { t.Errorf("without FillEqually, the segments are %v and %v wide", segs[1].W, segs[2].W) } // In a narrow parent the shares stay equal, and a label longer than // its share ends with an ellipsis, within its segment. even = true tt.SetSize(180, 200) segs = bounds(platform.RoleRadio, "Light", "Dark", "System settings") label, _ := tt.Find("System settings") if math.Abs(segs[0].W-segs[2].W) > 0.5 || float64(label.X+label.W) > segs[2].X+segs[2].W { t.Errorf("in a narrow track the segments are %v, %v and %v wide, the long label %+v", segs[0].W, segs[1].W, segs[2].W, label) } } // FillEqually on SegmentedBase's Track, after its segments, shares the // width among them, a disabled one too, which still takes no click. func TestSegmentedBaseFillEqually(t *testing.T) { view := 0 tt := coreNewTester(func(c *context) { coreColumn(c).Fill().Padding(20).Children(func() { seg := coreSegmentedBase(c, &view, 3) seg.Track.Padding(3).Children(func() { for i, label := range []string{"Grid", "List", "Columns view"} { s := seg.Segment(i).Padding(4, 10).Disabled(i == 2) s.Children(func() { coreText(c, label).SingleLine() }) } }).FillEqually().Label("View") }) }, 420, 200) tt.send(platform.SurfaceEvent{Kind: platform.AccessibilityOn}) var segs []platform.RectF for _, l := range []string{"Grid", "List", "Columns view"} { segs = append(segs, accessNode(t, tt.h.access, platform.RoleRadio, l).Bounds) } if math.Abs(segs[0].W-segs[2].W) > 0.5 || math.Abs(segs[0].W-125) > 1 { t.Fatalf("the segments are %v, %v and %v wide", segs[0].W, segs[1].W, segs[2].W) } tt.ClickAt(float32(segs[2].X+segs[2].W/2), float32(segs[2].Y+segs[2].H/2)) if view != 0 { t.Errorf("a click on the disabled segment chose %d", view) } tt.ClickAt(float32(segs[1].X+segs[1].W-4), float32(segs[1].Y+segs[1].H/2)) if view != 1 { t.Errorf("a click near the end of List's share chose %d", view) } } func TestTabsAreOneTabStop(t *testing.T) { tab := 1 labels := []string{"Before", "One", "Two", "Three", "After"} tt := coreNewTester(func(c *context) { coreColumn(c).Gap(8).Children(func() { coreButton(c, "Before") coreTabs(c, &tab, "One", "Two", "Three") coreButton(c, "After") }) }, 400, 200) if got := tabs(tt, 0, 3, labels...); !slices.Equal(got, []string{"Before", "Two", "After"}) { t.Errorf("Tab went %q", got) } tab = 2 // as another control would tt.Frame() if got := tabs(tt, Shift, 1, labels...); got[0] != "Three" { t.Errorf("Shift+Tab back into the tabs went to %q, not to the one chosen", got[0]) } } func TestToolbarOverflows(t *testing.T) { clicked := "" italic := false names := []string{"Alpha", "Bravo", "Charlie", "Delta", "Echo"} tt := coreNewTester(func(c *context) { coreToolbar(c, func() { for _, n := range names { if coreButton(c, n).Clicked() { clicked = n } } coreToggle(c, &italic, "Italic") }).Fill() }, 260, 100) // What does not fit goes into the menu, from the last. shown := []string{} for _, n := range append(names, "Italic") { if tt.HasText(n) { shown = append(shown, n) } } if len(shown) == 0 || len(shown) == 6 || shown[0] != "Alpha" { t.Fatalf("shows %q", shown) } if _, ok := tt.Find("More"); !ok { t.Fatal("no overflow button") } tt.Click("More") menu := tt.Menu() if len(menu) == 0 || menu[len(menu)-1] != "Italic" || slices.Contains(menu, shown[0]) { t.Fatalf("the menu has %q", menu) } if err := tt.ChooseMenuItem("Echo"); err != nil { t.Fatal(err) } if clicked != "Echo" { t.Errorf("choosing Echo clicked %q", clicked) } tt.Click("More") tt.ChooseMenuItem("Italic") if !italic { t.Error("choosing Italic did not press it") } tt.Click("More") if !tt.h.menu.Items[len(tt.h.menu.Items)-1].Checked { t.Error("Italic is not checked in the menu once pressed") } tt.CloseMenu() // With room, all show and the button goes. tt.SetSize(900, 100) for _, n := range names { if !tt.HasText(n) { t.Errorf("%s does not show with room", n) } } if r, ok := tt.Find("More"); ok && r.W > 0 { t.Error("the overflow button shows with room") } } func TestToolbarAccessibility(t *testing.T) { bold := true tt := coreNewTester(func(c *context) { coreToolbar(c, func() { coreToggleGroup(c, func() { coreToggle(c, &bold, "Bold") }).Label("Style") }).Label("Format") }, 400, 100) tt.send(platform.SurfaceEvent{Kind: platform.AccessibilityOn}) tb := accessNode(t, tt.h.access, platform.RoleToolbar, "Format") b := accessNode(t, tt.h.access, platform.RoleToggleButton, "Bold") if b.States&platform.AccessChecked == 0 { t.Errorf("Bold reads %+v", b) } // The toolbar holds the group, which holds the toggle. group := tt.h.access.Nodes[b.Parent] if group.Label != "Style" || tt.h.access.Nodes[group.Parent].ID != tb.ID { t.Errorf("the toggle is in %+v", group) } } func TestToolbarEntersAtItsFirst(t *testing.T) { bold := true tt := coreNewTester(func(c *context) { coreToolbar(c, func() { coreButton(c, "Cut") coreToggle(c, &bold, "Bold") }) }, 400, 100) tt.Key(0, KeyTab) if !tt.Focused("Cut") { t.Error("Tab went to the toggle that is on, not to the first control") } } func TestToolbarEntersWhereItWasLeft(t *testing.T) { view := 0 labels := []string{"Cut", "List", "Grid", "After"} tt := coreNewTester(func(c *context) { coreColumn(c).Gap(8).Children(func() { coreToolbar(c, func() { coreButton(c, "Cut") coreSegmented(c, &view, "List", "Grid").Label("View") }) coreButton(c, "After") }) }, 400, 200) tt.Click("Cut") tabs(tt, 0, 1, labels...) view = 1 tt.Frame() // A radio group inside a toolbar is part of it: the toolbar keeps // where it was left. if got := tabs(tt, Shift, 1, labels...); got[0] != "Cut" { t.Errorf("Shift+Tab back into the toolbar went to %q", got[0]) } }