A cross-cutting pass, each fix with a test that failed first. Veils: MyGo still hands a key to a base that turned off this frame, so every element key now goes through pixel.Shortcut, which an archtest enforces. Input handlers record and the build applies them only while enabled; the editor turns away a veil-frame letter or copy; value bases get a copy and keep only enabled turns. Popups give a held focus back to their opener when the veil lifts. Motion: S5 is enforced at the clock. Every Tick, Since, Step, Toward and Shot runs at 4-15 fps; blinks keep 1 Hz on 4 fps steps, fast animations step at 15, and drawn time is quantized to frames. Clocks stepping back: Since holds at zero without overflow, and k.Elapsed moves a stored start to now, so nothing goes negative, runs backward or indexes out of range. Also: logs count typed text instead of holding it, gallery names use glyphs the font has, removable rows are keyed, components take their own Options (archtest), and sweeps over all 508 demos check veiled keys, clicks, wheels, the clock stepping back and names.
100 lines
3.1 KiB
Go
100 lines
3.1 KiB
Go
package data_test
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/egoist/mygo/ui"
|
|
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/data"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
|
|
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
|
|
)
|
|
|
|
// underVeil builds fn where the test raises a veil.
|
|
func underVeil(inert *bool, fn func(c *ui.Context)) *ui.Tester {
|
|
return view(400, 300, newClock(), false, func(c *ui.Context) {
|
|
s := pixel.Current(c)
|
|
s.Inert = *inert
|
|
pixel.Scope(c, s, func() { fn(c) })
|
|
})
|
|
}
|
|
|
|
var tree = []data.TreeNode{{Label: "Maps", Children: []data.TreeNode{{Label: "Cave"}}}, {Label: "Saves"}}
|
|
|
|
func TestVeilsStopKeysInTheirFirstFrame(t *testing.T) {
|
|
for name, tc := range map[string]struct {
|
|
build func(c *ui.Context, v *string)
|
|
mods ui.Modifiers
|
|
key ui.Key
|
|
}{
|
|
"Sortable": {func(c *ui.Context, v *string) {
|
|
items := []string{"A", "B", "C"}
|
|
if r := data.Sortable(c, "List", &items, data.SortableOptions{}); r.Changed {
|
|
*v = fmt.Sprint(items)
|
|
}
|
|
}, ui.Alt, ui.KeyDown},
|
|
"Tree": {func(c *ui.Context, v *string) { data.Tree(c, "Files", tree, v, data.TreeOptions{}) }, 0, ui.KeyDown},
|
|
"Inventory": {func(c *ui.Context, v *string) {
|
|
at := 0
|
|
fmt.Sscan(*v, &at)
|
|
bag := []data.Item{{Sprite: icon.Sword, Name: "Sword"}, {Sprite: icon.Gem, Name: "Gem"}}
|
|
data.Inventory(c, "Bag", bag, &at, data.InventoryOptions{})
|
|
*v = fmt.Sprint(at)
|
|
}, 0, ui.KeyRight},
|
|
} {
|
|
v, inert := "0", false
|
|
tt := underVeil(&inert, func(c *ui.Context) { tc.build(c, &v) })
|
|
tt.Key(0, ui.KeyTab)
|
|
inert = true
|
|
tt.Key(tc.mods, tc.key)
|
|
if v != "0" {
|
|
t.Errorf("%s: a key in the veil's first frame made %s", name, v)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestDragsStopUnderAVeil(t *testing.T) {
|
|
items, inert := []string{"A", "B", "C"}, false
|
|
tt := underVeil(&inert, func(c *ui.Context) { data.Sortable(c, "List", &items, data.SortableOptions{}) })
|
|
r := find(t, tt, "List")
|
|
row := r.H / 3
|
|
tt.Press(r.X+10, r.Y+row/2)
|
|
tt.Move(r.X+10, r.Y+row*3/2)
|
|
moved := fmt.Sprint(items)
|
|
inert = true
|
|
tt.Move(r.X+10, r.Y+row*5/2)
|
|
tt.Release(r.X+10, r.Y+row*5/2)
|
|
if moved != "[B A C]" || fmt.Sprint(items) != moved {
|
|
t.Errorf("Sortable: %s before the veil, %v under it", moved, items)
|
|
}
|
|
bag, at := []data.Item{{Sprite: icon.Sword, Name: "Sword"}, {Sprite: icon.Gem, Name: "Gem"}}, 0
|
|
inert = false
|
|
tt = underVeil(&inert, func(c *ui.Context) { data.Inventory(c, "Bag", bag, &at, data.InventoryOptions{}) })
|
|
r = find(t, tt, "Bag")
|
|
tt.Press(r.X+20, r.Y+20)
|
|
tt.Move(r.X+64, r.Y+20)
|
|
inert = true
|
|
tt.Release(r.X+64, r.Y+20)
|
|
if bag[0].Name != "Sword" {
|
|
t.Error("Inventory swapped under the veil")
|
|
}
|
|
}
|
|
|
|
func TestTreeLeavesAnUnknownChoiceAlone(t *testing.T) {
|
|
chosen, changed := "9/9", 0
|
|
tt := view(400, 300, newClock(), false, func(c *ui.Context) {
|
|
if data.Tree(c, "Files", tree, &chosen, data.TreeOptions{}).Changed {
|
|
changed++
|
|
}
|
|
})
|
|
if chosen != "9/9" || changed != 0 {
|
|
t.Fatalf("an unknown choice became %q, %d changes", chosen, changed)
|
|
}
|
|
tt.Key(0, ui.KeyTab)
|
|
tt.Key(0, ui.KeyDown)
|
|
if chosen != "0" || changed != 1 {
|
|
t.Fatalf("Down chose %q, %d changes", chosen, changed)
|
|
}
|
|
}
|