817 lines
24 KiB
Go
817 lines
24 KiB
Go
package ui
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"testing"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/egoist/mygo/internal/platform"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestTabs(t *testing.T) {
|
||
|
|
tab := 0
|
||
|
|
changes := 0
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreColumn(c).Padding(10).Children(func() {
|
||
|
|
if coreTabs(c, &tab, "One", "Two", "Three").Changed() {
|
||
|
|
changes++
|
||
|
|
}
|
||
|
|
coreTextf(c, "page %d", tab)
|
||
|
|
})
|
||
|
|
}, 400, 200)
|
||
|
|
if err := tt.Click("Two"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if tab != 1 || !tt.HasText("page 1") || changes != 1 {
|
||
|
|
t.Fatalf("a click on Two: tab %d, %d changes, texts %q", tab, changes, tt.Texts())
|
||
|
|
}
|
||
|
|
// The arrows move the choice, and the focus with it.
|
||
|
|
tt.Key(0, KeyRight)
|
||
|
|
if tab != 2 || !tt.Focused("Three") {
|
||
|
|
t.Errorf("Right: tab %d, Three focused %v", tab, tt.Focused("Three"))
|
||
|
|
}
|
||
|
|
tt.Key(0, KeyRight)
|
||
|
|
if tab != 0 || !tt.Focused("One") {
|
||
|
|
t.Errorf("Right past the last tab: tab %d", tab)
|
||
|
|
}
|
||
|
|
tt.Key(0, KeyEnd)
|
||
|
|
if tab != 2 {
|
||
|
|
t.Errorf("End: tab %d", tab)
|
||
|
|
}
|
||
|
|
tt.Key(0, KeyHome)
|
||
|
|
if tab != 0 {
|
||
|
|
t.Errorf("Home: tab %d", tab)
|
||
|
|
}
|
||
|
|
|
||
|
|
tt.rt.accessibilityOn()
|
||
|
|
tt.Frame()
|
||
|
|
var tabs, selected int
|
||
|
|
for _, n := range tt.h.access.Nodes {
|
||
|
|
if n.Role == platform.RoleTab {
|
||
|
|
tabs++
|
||
|
|
if n.States&platform.AccessChecked != 0 {
|
||
|
|
selected++
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if tabs != 3 || selected != 1 {
|
||
|
|
t.Errorf("assistive technology sees %d tabs, %d selected", tabs, selected)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestSplit(t *testing.T) {
|
||
|
|
size := float32(120)
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreSplit(c, &size, func() { coreText(c, "left") }, func() { coreText(c, "right") }).Fill()
|
||
|
|
}, 400, 200)
|
||
|
|
if r, _ := tt.Find("right"); r.X < 120+1-0.5 || r.X > 120+1+0.5 {
|
||
|
|
t.Fatalf("the second pane starts at %v", r.X)
|
||
|
|
}
|
||
|
|
// The divider moves with the pointer, within the window.
|
||
|
|
x, y := float32(123), float32(100)
|
||
|
|
tt.Press(x, y)
|
||
|
|
tt.Move(x+50, y)
|
||
|
|
tt.Release(x+50, y)
|
||
|
|
if size != 170 {
|
||
|
|
t.Errorf("dragged 50 DIPs right, the first pane is %v wide", size)
|
||
|
|
}
|
||
|
|
tt.Press(x+50, y)
|
||
|
|
tt.Move(0, y)
|
||
|
|
tt.Release(0, y)
|
||
|
|
if size != 40 {
|
||
|
|
t.Errorf("dragged to the edge, the first pane is %v wide", size)
|
||
|
|
}
|
||
|
|
// The arrows move it once it has the focus.
|
||
|
|
tt.ClickAt(43, y)
|
||
|
|
tt.Key(0, KeyRight)
|
||
|
|
if size != 50 {
|
||
|
|
t.Errorf("Right on the divider: %v", size)
|
||
|
|
}
|
||
|
|
if r, _ := tt.Find("right"); r.X != 51 {
|
||
|
|
t.Errorf("the second pane follows to %v", r.X)
|
||
|
|
}
|
||
|
|
// The handle reaches over the first pane as over the second.
|
||
|
|
tt.Press(48, y)
|
||
|
|
tt.Move(68, y)
|
||
|
|
tt.Release(68, y)
|
||
|
|
if size != 70 {
|
||
|
|
t.Errorf("dragged 20 DIPs right from over the first pane, it is %v wide", size)
|
||
|
|
}
|
||
|
|
// The release's own move moves it too.
|
||
|
|
tt.Press(73, y)
|
||
|
|
tt.Move(103, y)
|
||
|
|
tt.Release(133, y)
|
||
|
|
if size != 130 {
|
||
|
|
t.Errorf("let go 60 DIPs right, the first pane is %v wide", size)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestSplitDragKeepsOvershoot checks that a drag past a pane's limit
|
||
|
|
// keeps how far it went: coming back moves the divider only once the
|
||
|
|
// pointer is over it again (#1474).
|
||
|
|
func TestSplitDragKeepsOvershoot(t *testing.T) {
|
||
|
|
size := float32(120)
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreSplit(c, &size, func() { coreText(c, "left") }, func() { coreText(c, "right") }).Fill()
|
||
|
|
}, 400, 200)
|
||
|
|
x, y := float32(123), float32(100)
|
||
|
|
tt.Press(x, y)
|
||
|
|
// 80 DIPs to the first pane's least, 40, and 200 past it.
|
||
|
|
tt.Move(x-280, y)
|
||
|
|
if size != 40 {
|
||
|
|
t.Fatalf("dragged past the least, the first pane is %v wide", size)
|
||
|
|
}
|
||
|
|
tt.Move(x-130, y)
|
||
|
|
if size != 40 {
|
||
|
|
t.Errorf("150 DIPs back, still 50 past the divider, the first pane is %v wide", size)
|
||
|
|
}
|
||
|
|
tt.Move(x-30, y)
|
||
|
|
if size != 90 {
|
||
|
|
t.Errorf("100 more back, 50 over the divider, the first pane is %v wide", size)
|
||
|
|
}
|
||
|
|
tt.Release(x-30, y)
|
||
|
|
// The next drag starts afresh.
|
||
|
|
tt.Press(93, y)
|
||
|
|
tt.Move(113, y)
|
||
|
|
tt.Release(113, y)
|
||
|
|
if size != 110 {
|
||
|
|
t.Errorf("a new drag of 20 DIPs right, the first pane is %v wide", size)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestSplitVertical(t *testing.T) {
|
||
|
|
size := float32(60)
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreSplitVertical(c, &size, func() { coreText(c, "top") }, func() { coreText(c, "bottom") }).Fill()
|
||
|
|
}, 300, 200)
|
||
|
|
// The second pane starts below the divider and fits in the window.
|
||
|
|
if r, _ := tt.Find("bottom"); r.Y < 60+1-0.5 || r.Y > 60+1+0.5 {
|
||
|
|
t.Fatalf("the second pane starts at %v", r.Y)
|
||
|
|
}
|
||
|
|
n := 0
|
||
|
|
for e := tt.rt.c.root.first; e != nil; e = e.next {
|
||
|
|
n++
|
||
|
|
}
|
||
|
|
if n != 1 {
|
||
|
|
t.Errorf("the split built %d elements in the root", n)
|
||
|
|
}
|
||
|
|
tt.Press(100, 61)
|
||
|
|
tt.Move(100, 101)
|
||
|
|
tt.Release(100, 101)
|
||
|
|
if size != 100 {
|
||
|
|
t.Errorf("dragged 40 DIPs down, the first pane is %v high", size)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestSplitResetOnDoubleClick restores the size SplitReset chose, while a
|
||
|
|
// single click keeps the one the user dragged to.
|
||
|
|
func TestSplitResetOnDoubleClick(t *testing.T) {
|
||
|
|
size := float32(120)
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreSplit(c, &size, func() { coreText(c, "left") }, func() { coreText(c, "right") }).SplitReset(220).Fill()
|
||
|
|
}, 400, 200)
|
||
|
|
drag(tt, 121, 100, 171, 100)
|
||
|
|
if size != 170 {
|
||
|
|
t.Fatalf("dragged to 170, the first pane is %v", size)
|
||
|
|
}
|
||
|
|
tt.ClickAt(171, 100)
|
||
|
|
if size != 170 {
|
||
|
|
t.Errorf("a click reset the first pane to %v", size)
|
||
|
|
}
|
||
|
|
tt.ClickAt(171, 100)
|
||
|
|
if size != 220 {
|
||
|
|
t.Errorf("double click on the divider: first pane %v, want 220", size)
|
||
|
|
}
|
||
|
|
if r, _ := tt.Find("right"); r.X != 221 {
|
||
|
|
t.Errorf("the second pane starts at %v after the reset, want 221", r.X)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestSplitResetThroughTheFacade is TestSplitResetOnDoubleClick through
|
||
|
|
// the public API, whose constructor applies input before the chained
|
||
|
|
// SplitReset of that build runs: the value the build before set is the one
|
||
|
|
// the input applies by.
|
||
|
|
func TestSplitResetThroughTheFacade(t *testing.T) {
|
||
|
|
size := float32(120)
|
||
|
|
tt := NewTester(func(c *Context) {
|
||
|
|
Split(c, &size, func() { Text(c, "left") }, func() { Text(c, "right") }).SplitReset(220).Fill()
|
||
|
|
}, 400, 200)
|
||
|
|
drag(tt, 121, 100, 171, 100)
|
||
|
|
if size != 170 {
|
||
|
|
t.Fatalf("dragged to 170, the first pane is %v", size)
|
||
|
|
}
|
||
|
|
tt.ClickAt(171, 100)
|
||
|
|
tt.ClickAt(171, 100)
|
||
|
|
if size != 220 {
|
||
|
|
t.Errorf("double click on the divider: first pane %v, want 220", size)
|
||
|
|
}
|
||
|
|
if r, _ := tt.Find("right"); r.X != 221 {
|
||
|
|
t.Errorf("the second pane starts at %v after the reset, want 221", r.X)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestSplitPercentResetOnDoubleClick resets a percent split in percent,
|
||
|
|
// not DIPs.
|
||
|
|
func TestSplitPercentResetOnDoubleClick(t *testing.T) {
|
||
|
|
firstPercent := float32(75)
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreSplitVerticalPercent(c, &firstPercent, func() { coreText(c, "top") }, func() { coreText(c, "bottom") }).SplitReset(50).Fill()
|
||
|
|
}, 300, 401)
|
||
|
|
drag(tt, 100, 301, 100, 101)
|
||
|
|
if firstPercent != 25 {
|
||
|
|
t.Fatalf("dragged to a quarter, the first pane takes %v%%", firstPercent)
|
||
|
|
}
|
||
|
|
tt.ClickAt(100, 101)
|
||
|
|
tt.ClickAt(100, 101)
|
||
|
|
if firstPercent != 50 {
|
||
|
|
t.Errorf("double click on the divider: first pane takes %v%%, want 50", firstPercent)
|
||
|
|
}
|
||
|
|
if r := at(t, tt, "bottom"); r.Y != 201 {
|
||
|
|
t.Errorf("the second pane starts at %v after the reset, want 201", r.Y)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestSplitResetKeepsSizeForLater keeps a size the window is too small to
|
||
|
|
// show, as a press on the divider does, so the pane takes it back when the
|
||
|
|
// window grows.
|
||
|
|
func TestSplitResetKeepsSizeForLater(t *testing.T) {
|
||
|
|
size := float32(120)
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreSplit(c, &size, func() { coreText(c, "left") }, func() { coreText(c, "right") }).SplitReset(220).Fill()
|
||
|
|
}, 200, 200)
|
||
|
|
tt.ClickAt(121, 100)
|
||
|
|
tt.ClickAt(121, 100)
|
||
|
|
if size != 220 {
|
||
|
|
t.Fatalf("double click in a window 200 wide: first pane %v, want 220", size)
|
||
|
|
}
|
||
|
|
if r := at(t, tt, "right"); r.X != 160 {
|
||
|
|
t.Errorf("the second pane starts at %v in a window 200 wide, want 160", r.X)
|
||
|
|
}
|
||
|
|
tt.SetSize(400, 200)
|
||
|
|
if r := at(t, tt, "right"); r.X != 221 {
|
||
|
|
t.Errorf("made wide again, the second pane starts at %v, want 221", r.X)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestSplitWithoutResetKeepsSizeOnDoubleClick leaves a split without
|
||
|
|
// SplitReset as it was.
|
||
|
|
func TestSplitWithoutResetKeepsSizeOnDoubleClick(t *testing.T) {
|
||
|
|
size := float32(120)
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreSplit(c, &size, func() { coreText(c, "left") }, func() { coreText(c, "right") }).Fill()
|
||
|
|
}, 400, 200)
|
||
|
|
drag(tt, 121, 100, 171, 100)
|
||
|
|
tt.ClickAt(171, 100)
|
||
|
|
tt.ClickAt(171, 100)
|
||
|
|
if size != 170 {
|
||
|
|
t.Errorf("a double click without SplitReset changed the first pane to %v", size)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestSplitShrinks keeps both panes in a window made smaller than the
|
||
|
|
// first pane: the first gives way, and takes its size back when there is
|
||
|
|
// room again; the divider moves from where it shows.
|
||
|
|
func TestSplitShrinks(t *testing.T) {
|
||
|
|
size := float32(450)
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreSplitVertical(c, &size, func() { coreText(c, "top") }, func() { coreText(c, "bottom") }).Fill()
|
||
|
|
}, 300, 600)
|
||
|
|
tt.SetSize(300, 300)
|
||
|
|
if r := at(t, tt, "bottom"); r.Y != 300-40 {
|
||
|
|
t.Fatalf("in a window 300 high, the second pane starts at %v, want %v", r.Y, 300-40)
|
||
|
|
}
|
||
|
|
if size != 450 {
|
||
|
|
t.Errorf("shrinking the window changed the size to %v", size)
|
||
|
|
}
|
||
|
|
// Assistive technology hears the size that shows.
|
||
|
|
tt.rt.accessibilityOn()
|
||
|
|
tt.Frame()
|
||
|
|
dividers := 0
|
||
|
|
for _, n := range tt.h.access.Nodes {
|
||
|
|
if n.Role == platform.RoleSplitter {
|
||
|
|
dividers++
|
||
|
|
if n.Now != 259 || n.Max != 259 {
|
||
|
|
t.Errorf("the divider says %v of at most %v, want 259 of 259", n.Now, n.Max)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if dividers != 1 {
|
||
|
|
t.Errorf("assistive technology sees %d dividers", dividers)
|
||
|
|
}
|
||
|
|
tt.SetSize(300, 600)
|
||
|
|
if r := at(t, tt, "bottom"); r.Y != 451 {
|
||
|
|
t.Errorf("made high again, the second pane starts at %v, want 451", r.Y)
|
||
|
|
}
|
||
|
|
tt.SetSize(300, 300)
|
||
|
|
// The divider shows at 259.
|
||
|
|
drag(tt, 100, 259, 100, 219)
|
||
|
|
if size != 219 {
|
||
|
|
t.Errorf("dragged 40 DIPs up from where it shows, the first pane is %v high, want 219", size)
|
||
|
|
}
|
||
|
|
size = 450
|
||
|
|
tt.Frame()
|
||
|
|
tt.Key(0, KeyUp)
|
||
|
|
if size != 249 {
|
||
|
|
t.Errorf("Up from where it shows: %v, want 249", size)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestSplitShrinksSideways is TestSplitShrinks for a split side by side,
|
||
|
|
// left to right and right to left.
|
||
|
|
func TestSplitShrinksSideways(t *testing.T) {
|
||
|
|
for _, rtl := range []bool{false, true} {
|
||
|
|
size := float32(350)
|
||
|
|
view := func(c *context) {
|
||
|
|
coreSplit(c, &size, func() { coreText(c, "first") }, func() { coreText(c, "second") }).Fill()
|
||
|
|
}
|
||
|
|
tt := coreNewTester(view, 400, 200)
|
||
|
|
if rtl {
|
||
|
|
tt = rtlTester(view, 400, 200)
|
||
|
|
}
|
||
|
|
tt.SetSize(200, 200)
|
||
|
|
r := at(t, tt, "second")
|
||
|
|
if want := float32(200 - 40); !rtl && r.X != want || rtl && r.X >= 40 {
|
||
|
|
t.Errorf("rtl %v: in a window 200 wide, the second pane's text is at %v", rtl, r.X)
|
||
|
|
}
|
||
|
|
// The divider shows 159 from the start.
|
||
|
|
x := float32(159)
|
||
|
|
if rtl {
|
||
|
|
x = 200 - 160
|
||
|
|
}
|
||
|
|
// Toward the second pane, which narrows the first.
|
||
|
|
to := x - 20
|
||
|
|
if rtl {
|
||
|
|
to = x + 20
|
||
|
|
}
|
||
|
|
drag(tt, x, 100, to, 100)
|
||
|
|
if size != 139 {
|
||
|
|
t.Errorf("rtl %v: dragged 20 DIPs from where the divider shows, the first pane is %v, want 139", rtl, size)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestSplitPercent shares the room in proportion: both panes follow the
|
||
|
|
// window, and the divider moves the percentage.
|
||
|
|
func TestSplitPercent(t *testing.T) {
|
||
|
|
firstPercent := float32(75)
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreSplitVerticalPercent(c, &firstPercent, func() { coreText(c, "top") }, func() { coreText(c, "bottom") }).Fill()
|
||
|
|
}, 300, 401)
|
||
|
|
if r := at(t, tt, "bottom"); r.Y != 301 {
|
||
|
|
t.Fatalf("three quarters of 400: the second pane starts at %v, want 301", r.Y)
|
||
|
|
}
|
||
|
|
tt.SetSize(300, 201)
|
||
|
|
if r := at(t, tt, "bottom"); r.Y != 151 {
|
||
|
|
t.Errorf("three quarters of 200: the second pane starts at %v, want 151", r.Y)
|
||
|
|
}
|
||
|
|
if firstPercent != 75 {
|
||
|
|
t.Errorf("resizing the window changed the first pane's percentage to %v", firstPercent)
|
||
|
|
}
|
||
|
|
drag(tt, 100, 150, 100, 100)
|
||
|
|
if firstPercent != 50 {
|
||
|
|
t.Errorf("dragged to the middle, the first pane takes %v%%, want 50", firstPercent)
|
||
|
|
}
|
||
|
|
tt.Key(0, KeyUp)
|
||
|
|
if firstPercent != 45 {
|
||
|
|
t.Errorf("Up from the middle: %v, want 45", firstPercent)
|
||
|
|
}
|
||
|
|
// Each pane keeps 40 DIPs.
|
||
|
|
firstPercent = 100
|
||
|
|
tt.Frame()
|
||
|
|
if r := at(t, tt, "bottom"); r.Y != 201-40 {
|
||
|
|
t.Errorf("100%%: the second pane starts at %v, want %v", r.Y, 201-40)
|
||
|
|
}
|
||
|
|
firstPercent = 0
|
||
|
|
tt.Frame()
|
||
|
|
if r := at(t, tt, "bottom"); r.Y != 41 {
|
||
|
|
t.Errorf("0%%: the second pane starts at %v, want 41", r.Y)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// TestSplitPercentSideways is TestSplitPercent side by side, left to right and
|
||
|
|
// right to left.
|
||
|
|
func TestSplitPercentSideways(t *testing.T) {
|
||
|
|
for _, rtl := range []bool{false, true} {
|
||
|
|
firstPercent := float32(25)
|
||
|
|
view := func(c *context) {
|
||
|
|
coreSplitPercent(c, &firstPercent, func() { coreText(c, "first") }, func() { coreText(c, "second") }).Fill()
|
||
|
|
}
|
||
|
|
tt := coreNewTester(view, 401, 200)
|
||
|
|
if rtl {
|
||
|
|
tt = rtlTester(view, 401, 200)
|
||
|
|
}
|
||
|
|
// The divider at 100 from the start.
|
||
|
|
x := float32(100)
|
||
|
|
if rtl {
|
||
|
|
x = 401 - 101
|
||
|
|
}
|
||
|
|
if r := at(t, tt, "second"); !rtl && r.X != 101 || rtl && r.X >= 300 {
|
||
|
|
t.Errorf("rtl %v: a quarter of 400: the second pane's text is at %v", rtl, r.X)
|
||
|
|
}
|
||
|
|
// Toward the second pane, which widens the first.
|
||
|
|
to := x + 100
|
||
|
|
if rtl {
|
||
|
|
to = x - 100
|
||
|
|
}
|
||
|
|
drag(tt, x, 100, to, 100)
|
||
|
|
if firstPercent != 50 {
|
||
|
|
t.Errorf("rtl %v: dragged 100 DIPs, the first pane takes %v%%, want 50", rtl, firstPercent)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestNumberInput(t *testing.T) {
|
||
|
|
v := 5.0
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreColumn(c).Padding(10).AlignItems(Start).Children(func() {
|
||
|
|
coreNumberInput(c, &v, 0, 10, 0.5).Label("Count")
|
||
|
|
})
|
||
|
|
}, 400, 100)
|
||
|
|
if err := tt.Click("Increase"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
tt.Click("Decrease")
|
||
|
|
tt.Click("Decrease")
|
||
|
|
if v != 4.5 {
|
||
|
|
t.Errorf("one step up and two down from 5: %v", v)
|
||
|
|
}
|
||
|
|
r, _ := tt.Find("Count")
|
||
|
|
tt.ClickAt(r.X+20, r.Y+r.H/2)
|
||
|
|
tt.Key(Cmd, KeyA)
|
||
|
|
tt.Type("7.5")
|
||
|
|
if v != 7.5 {
|
||
|
|
t.Errorf("typed 7.5: %v", v)
|
||
|
|
}
|
||
|
|
tt.Key(Cmd, KeyA)
|
||
|
|
tt.Type("12")
|
||
|
|
if v != 7.5 {
|
||
|
|
t.Errorf("12 is out of range, yet the value is %v", v)
|
||
|
|
}
|
||
|
|
tt.Key(0, KeyUp)
|
||
|
|
if v != 8 {
|
||
|
|
t.Errorf("Up from 7.5: %v", v)
|
||
|
|
}
|
||
|
|
for range 5 {
|
||
|
|
tt.Key(0, KeyUp)
|
||
|
|
}
|
||
|
|
if v != 10 {
|
||
|
|
t.Errorf("Up past the top: %v", v)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// A width given to a NumberInput or DecimalInput sizes the whole input,
|
||
|
|
// buttons and all: the field gives up the room the buttons need rather
|
||
|
|
// than pushing them over what comes after.
|
||
|
|
func TestNumberInputsKeepToTheirWidth(t *testing.T) {
|
||
|
|
n, d := 600.0, "600"
|
||
|
|
for _, input := range []func(c *context) *node{
|
||
|
|
func(c *context) *node { return coreNumberInput(c, &n, 100, 2000, 100) },
|
||
|
|
func(c *context) *node { return coreDecimalInput(c, &d, "100", "2000", "100") },
|
||
|
|
} {
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreRow(c).Padding(10).AlignItems(Center).Children(func() {
|
||
|
|
input(c).Width(90).Label("Wait")
|
||
|
|
coreText(c, "after")
|
||
|
|
})
|
||
|
|
}, 400, 100)
|
||
|
|
field, _ := tt.Find("Wait")
|
||
|
|
plus, _ := tt.Find("Increase")
|
||
|
|
after, _ := tt.Find("after")
|
||
|
|
if right := plus.X + plus.W; right > field.X+90 || right > after.X {
|
||
|
|
t.Errorf("the + button ends at x %.0f, past the input's 90 (from x %.0f) and the text after it (from x %.0f)", right, field.X, after.X)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestToast(t *testing.T) {
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
if coreButton(c, "Save").Clicked() {
|
||
|
|
c.Toast("Saved")
|
||
|
|
}
|
||
|
|
}, 400, 300)
|
||
|
|
if err := tt.Click("Save"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if !tt.HasText("Saved") {
|
||
|
|
t.Fatalf("no toast after the click: %q", tt.Texts())
|
||
|
|
}
|
||
|
|
tt.Click("Save")
|
||
|
|
if len(tt.rt.toasts) != 1 {
|
||
|
|
t.Errorf("the same message shows %d times", len(tt.rt.toasts))
|
||
|
|
}
|
||
|
|
tt.Advance(toastTime)
|
||
|
|
if tt.HasText("Saved") {
|
||
|
|
t.Error("the toast stays once its time is over")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestTable(t *testing.T) {
|
||
|
|
names := make([]string, 100)
|
||
|
|
for i := range names {
|
||
|
|
names[i] = "file" + string(rune('A'+i%26)) + string(rune('0'+i/26))
|
||
|
|
}
|
||
|
|
sel, opened := -1, -1
|
||
|
|
s := ListState{Selected: &sel}
|
||
|
|
cols := []TableColumn{{Title: "Name"}, {Title: "Size", Width: 80, Align: End}}
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreColumn(c).Fill().Padding(10).Children(func() {
|
||
|
|
if coreTable(c, &s, cols, len(names), func(row, col int) {
|
||
|
|
if col == 0 {
|
||
|
|
coreText(c, names[row])
|
||
|
|
} else {
|
||
|
|
coreTextf(c, "%d KB", row)
|
||
|
|
}
|
||
|
|
}).Grow(1).Submitted() {
|
||
|
|
opened = sel
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}, 400, 300)
|
||
|
|
if !tt.HasText("Name") || !tt.HasText("fileA0") || tt.HasText(names[99]) {
|
||
|
|
t.Fatalf("texts %q: the header and the first rows, not the last", tt.Texts())
|
||
|
|
}
|
||
|
|
if err := tt.Click("fileC0"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if sel != 2 {
|
||
|
|
t.Errorf("clicked the third row: selected %d", sel)
|
||
|
|
}
|
||
|
|
tt.Key(0, KeyDown)
|
||
|
|
tt.Key(0, KeyDown)
|
||
|
|
if sel != 4 {
|
||
|
|
t.Errorf("Down twice from the third row: %d", sel)
|
||
|
|
}
|
||
|
|
tt.Key(0, KeyEnd)
|
||
|
|
if sel != 99 || !tt.HasText(names[99]) {
|
||
|
|
t.Errorf("End: selected %d, last row shown %v", sel, tt.HasText(names[99]))
|
||
|
|
}
|
||
|
|
tt.Key(0, KeyEnter)
|
||
|
|
if opened != 99 {
|
||
|
|
t.Errorf("Enter opened %d", opened)
|
||
|
|
}
|
||
|
|
tt.Key(0, KeyHome)
|
||
|
|
r, _ := tt.Find("fileA0")
|
||
|
|
tt.ClickAt(r.X+r.W/2, r.Y+r.H/2)
|
||
|
|
tt.ClickAt(r.X+r.W/2, r.Y+r.H/2)
|
||
|
|
if sel != 0 || opened != 0 {
|
||
|
|
t.Errorf("a double click on the first row: selected %d, opened %d", sel, opened)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestTableRows(t *testing.T) {
|
||
|
|
// Sections of a header and 9 rows; every seventh row says a lot.
|
||
|
|
header := func(i int) bool { return i%10 == 0 }
|
||
|
|
s := ListState{Header: header}
|
||
|
|
headerCols := 0
|
||
|
|
cols := []TableColumn{{Title: "Name"}, {Title: "Notes", Width: 120}}
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreTable(c, &s, cols, 1000, func(row, col int) {
|
||
|
|
switch {
|
||
|
|
case header(row):
|
||
|
|
if col != 0 {
|
||
|
|
headerCols++
|
||
|
|
}
|
||
|
|
coreTextf(c, "Section %d", row/10)
|
||
|
|
case col == 0:
|
||
|
|
coreTextf(c, "Row %d", row)
|
||
|
|
case row%7 == 0:
|
||
|
|
coreText(c, "A note long enough to wrap over a few lines of its column")
|
||
|
|
default:
|
||
|
|
coreText(c, "Short")
|
||
|
|
}
|
||
|
|
}).Grow(1)
|
||
|
|
}, 400, 600)
|
||
|
|
row1, _ := rowBox(tt, &s, 1)
|
||
|
|
if row1.H != 32 {
|
||
|
|
t.Errorf("a row of one line is %v high", row1.H)
|
||
|
|
}
|
||
|
|
row7, _ := rowBox(tt, &s, 7)
|
||
|
|
if row7.H < 3*16 {
|
||
|
|
t.Errorf("a row of a cell wrapping over lines is %v high", row7.H)
|
||
|
|
}
|
||
|
|
if r, ok := tt.Find("Row 7"); !ok || r.Y+r.H/2 < row7.Y+row7.H/2-1 || r.Y+r.H/2 > row7.Y+row7.H/2+1 {
|
||
|
|
t.Errorf("the short cell of a tall row is at %v, not in the middle of %v", r, row7)
|
||
|
|
}
|
||
|
|
if top, _ := rowBox(tt, &s, 0); top.W != 400 || headerCols != 0 {
|
||
|
|
t.Errorf("a header row is %v wide and built %d other columns", top.W, headerCols)
|
||
|
|
}
|
||
|
|
// The state scrolls the rows, below the column headers; the header of
|
||
|
|
// the section they are in stays at the top.
|
||
|
|
s.ScrollTo(505, Start)
|
||
|
|
tt.Frame()
|
||
|
|
head, _ := tt.Find("Name")
|
||
|
|
r505, _ := rowBox(tt, &s, 505)
|
||
|
|
r500, ok := rowBox(tt, &s, 500)
|
||
|
|
if !ok || r500.Y < head.Y+head.H || r500.Y+r500.H > r505.Y {
|
||
|
|
t.Errorf("row 505 at %v under the column headers ending at %v; its section's header at %v (%v)", r505, head.Y+head.H, r500, ok)
|
||
|
|
}
|
||
|
|
if first, _ := s.Visible(); first > 505 || first < 503 {
|
||
|
|
t.Errorf("scrolled to row 505, the first row in view is %d", first)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestTree(t *testing.T) {
|
||
|
|
srcOpen, cmdOpen := false, true
|
||
|
|
var clicked string
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreTree(c, func() {
|
||
|
|
item := func(label string, open *bool, children func()) {
|
||
|
|
if coreTreeItem(c, label, open, children).Selected(clicked == label).Clicked() {
|
||
|
|
clicked = label
|
||
|
|
}
|
||
|
|
}
|
||
|
|
item("src", &srcOpen, func() {
|
||
|
|
item("main.go", nil, nil)
|
||
|
|
item("cmd", &cmdOpen, func() {
|
||
|
|
item("tool.go", nil, nil)
|
||
|
|
})
|
||
|
|
})
|
||
|
|
item("go.mod", nil, nil)
|
||
|
|
})
|
||
|
|
}, 400, 300)
|
||
|
|
if tt.HasText("main.go") || !tt.HasText("go.mod") {
|
||
|
|
t.Fatalf("a closed folder shows its items: %q", tt.Texts())
|
||
|
|
}
|
||
|
|
// Its arrow opens it.
|
||
|
|
r, _ := tt.Find("src")
|
||
|
|
tt.ClickAt(r.X-10, r.Y+r.H/2)
|
||
|
|
if !srcOpen || !tt.HasText("main.go") || !tt.HasText("tool.go") || clicked != "" {
|
||
|
|
t.Fatalf("after a click on the arrow: open %v, clicked %q, texts %q", srcOpen, clicked, tt.Texts())
|
||
|
|
}
|
||
|
|
// Nested items are indented.
|
||
|
|
if m, _ := tt.Find("main.go"); m.X <= r.X {
|
||
|
|
t.Errorf("main.go at %v, src at %v", m.X, r.X)
|
||
|
|
}
|
||
|
|
tt.Click("src")
|
||
|
|
if clicked != "src" {
|
||
|
|
t.Errorf("clicked %q", clicked)
|
||
|
|
}
|
||
|
|
// The arrows move the focus through the items in view.
|
||
|
|
tt.Key(0, KeyDown)
|
||
|
|
if !tt.Focused("main.go") {
|
||
|
|
t.Error("Down from src does not focus main.go")
|
||
|
|
}
|
||
|
|
tt.Key(0, KeyDown)
|
||
|
|
tt.Key(0, KeyLeft) // closes cmd
|
||
|
|
if cmdOpen || tt.HasText("tool.go") {
|
||
|
|
t.Errorf("Left on the open cmd leaves it open %v", cmdOpen)
|
||
|
|
}
|
||
|
|
tt.Key(0, KeyLeft) // to its parent
|
||
|
|
if !tt.Focused("src") {
|
||
|
|
t.Error("Left on the closed cmd does not focus src")
|
||
|
|
}
|
||
|
|
tt.Key(0, KeyEnter)
|
||
|
|
if clicked != "src" {
|
||
|
|
t.Errorf("Enter chose %q", clicked)
|
||
|
|
}
|
||
|
|
tt.Key(0, KeyLeft)
|
||
|
|
if srcOpen || tt.HasText("main.go") {
|
||
|
|
t.Error("Left on src does not close it")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDateInput(t *testing.T) {
|
||
|
|
date := time.Date(2026, 10, 3, 9, 30, 0, 0, time.UTC)
|
||
|
|
changes := 0
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreColumn(c).Padding(10).AlignItems(Start).Children(func() {
|
||
|
|
if coreDateInput(c, &date).Label("Due").Changed() {
|
||
|
|
changes++
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}, 400, 420)
|
||
|
|
if !tt.HasText("10/3/2026") {
|
||
|
|
t.Fatalf("texts %q", tt.Texts())
|
||
|
|
}
|
||
|
|
if err := tt.Click("Due"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if !tt.HasText("October 2026") {
|
||
|
|
t.Fatalf("no calendar after a click: %q", tt.Texts())
|
||
|
|
}
|
||
|
|
if err := tt.Click("October 15, 2026"); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if want := time.Date(2026, 10, 15, 9, 30, 0, 0, time.UTC); !date.Equal(want) || changes != 1 || tt.HasText("October 2026") {
|
||
|
|
t.Fatalf("a click on the 15th: %v, %d changes, calendar shown %v", date, changes, tt.HasText("October 2026"))
|
||
|
|
}
|
||
|
|
// From the keyboard: Enter opens it, the arrows and Page Down move,
|
||
|
|
// Enter chooses.
|
||
|
|
tt.Key(0, KeyEnter)
|
||
|
|
tt.Key(0, KeyRight)
|
||
|
|
tt.Key(0, KeyDown)
|
||
|
|
tt.Key(0, KeyPageDown)
|
||
|
|
tt.Key(0, KeyEnter)
|
||
|
|
if want := time.Date(2026, 11, 23, 9, 30, 0, 0, time.UTC); !date.Equal(want) {
|
||
|
|
t.Errorf("Right, Down and Page Down from the 15th chose %v", date)
|
||
|
|
}
|
||
|
|
tt.Click("Due")
|
||
|
|
tt.Click("Next month")
|
||
|
|
if !tt.HasText("December 2026") {
|
||
|
|
t.Errorf("Next month shows %q", tt.Texts())
|
||
|
|
}
|
||
|
|
tt.Key(0, KeyEscape)
|
||
|
|
if tt.HasText("December 2026") {
|
||
|
|
t.Error("Escape leaves the calendar open")
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestDateInputWide(t *testing.T) {
|
||
|
|
date := time.Date(2026, 10, 3, 0, 0, 0, 0, time.UTC)
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreColumn(c).Padding(10).Children(func() { coreDateInput(c, &date).Label("Due") })
|
||
|
|
}, 800, 420)
|
||
|
|
tt.Click("Due")
|
||
|
|
field, _ := tt.Find("Due")
|
||
|
|
next, ok := tt.Find("Next month")
|
||
|
|
if !ok {
|
||
|
|
t.Fatalf("no calendar after a click: %q", tt.Texts())
|
||
|
|
}
|
||
|
|
if next.X+next.W > field.X+field.W/2 {
|
||
|
|
t.Errorf("the calendar under a field %v wide reaches %v", field.W, next.X+next.W)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestProgressReverse(t *testing.T) {
|
||
|
|
for _, reverse := range []bool{false, true} {
|
||
|
|
img := coreRender(func(c *context) {
|
||
|
|
coreColumn(c).Padding(10).Children(func() {
|
||
|
|
bar := coreProgress(c, 0.25).Height(10)
|
||
|
|
if reverse {
|
||
|
|
bar.Reverse()
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}, 220, 30, 1)
|
||
|
|
accent := LightTheme().Accent
|
||
|
|
filled := func(x int) bool {
|
||
|
|
c := img.RGBAAt(x, 15)
|
||
|
|
return c.R == accent.R && c.G == accent.G && c.B == accent.B
|
||
|
|
}
|
||
|
|
// The fill takes a quarter of the 200 DIPs of the bar, from the
|
||
|
|
// left, or from the right when reversed.
|
||
|
|
if filled(20) == reverse || filled(200) != reverse || filled(110) {
|
||
|
|
t.Errorf("reverse %v: filled at 20 %v, at 110 %v, at 200 %v", reverse, filled(20), filled(110), filled(200))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestTooltipHidesOnPress(t *testing.T) {
|
||
|
|
tt := coreNewTester(func(c *context) {
|
||
|
|
coreRow(c).Gap(40).Padding(40).Children(func() {
|
||
|
|
coreButton(c, "Save").Tooltip("Save the file")
|
||
|
|
coreMenuButton(c, "More", func(m *Menu) { m.Item("Duplicate") }).Tooltip("More actions")
|
||
|
|
})
|
||
|
|
}, 400, 200)
|
||
|
|
rest := func(x, y float32) {
|
||
|
|
tt.Move(x, y)
|
||
|
|
tt.rt.tips.hoverSince = tt.now.Add(-time.Second)
|
||
|
|
tt.Frame()
|
||
|
|
}
|
||
|
|
r, _ := tt.Find("Save")
|
||
|
|
x, y := center(r)
|
||
|
|
rest(x, y)
|
||
|
|
if !tt.HasText("Save the file") {
|
||
|
|
t.Fatal("no tooltip over the button")
|
||
|
|
}
|
||
|
|
// A click hides it, and it stays hidden while the pointer rests on
|
||
|
|
// the button.
|
||
|
|
tt.ClickAt(x, y)
|
||
|
|
rest(x+2, y)
|
||
|
|
if tt.HasText("Save the file") {
|
||
|
|
t.Error("the tooltip shows again after a click")
|
||
|
|
}
|
||
|
|
// It shows again as the pointer comes back.
|
||
|
|
rest(5, 5)
|
||
|
|
rest(x, y)
|
||
|
|
if !tt.HasText("Save the file") {
|
||
|
|
t.Error("no tooltip as the pointer comes back")
|
||
|
|
}
|
||
|
|
// A click by the keys closes it too.
|
||
|
|
tt.Key(0, KeyEnter)
|
||
|
|
if tt.HasText("Save the file") {
|
||
|
|
t.Error("the tooltip shows after Enter clicked the button")
|
||
|
|
}
|
||
|
|
// A press before it shows keeps it from showing.
|
||
|
|
rest(5, 5)
|
||
|
|
tt.Move(x, y)
|
||
|
|
tt.ClickAt(x, y)
|
||
|
|
rest(x, y)
|
||
|
|
if tt.HasText("Save the file") {
|
||
|
|
t.Error("the tooltip shows after a click before the delay")
|
||
|
|
}
|
||
|
|
// The press opening a menu button's menu closes it.
|
||
|
|
r, _ = tt.Find("More")
|
||
|
|
x, y = center(r)
|
||
|
|
rest(x, y)
|
||
|
|
if !tt.HasText("More actions") {
|
||
|
|
t.Fatal("no tooltip over the menu button")
|
||
|
|
}
|
||
|
|
tt.Press(x, y)
|
||
|
|
if tt.Menu() == nil || tt.HasText("More actions") {
|
||
|
|
t.Errorf("menu %q; the tooltip shows with the menu", tt.Menu())
|
||
|
|
}
|
||
|
|
tt.Release(x, y)
|
||
|
|
tt.CloseMenu()
|
||
|
|
rest(x, y)
|
||
|
|
if tt.HasText("More actions") {
|
||
|
|
t.Error("the tooltip shows again after the menu")
|
||
|
|
}
|
||
|
|
}
|