Files
beatglow/app/widget/menu_test.go
T

58 lines
1.5 KiB
Go

package widget
import (
"image"
"image/color"
"testing"
"github.com/BeatGlow/beatglow/app"
"github.com/BeatGlow/beatglow/app/menu"
"github.com/BeatGlow/display"
"github.com/BeatGlow/display/pixel"
)
type menuTestDisplay struct {
display.Display
image *image.RGBA
}
func (d *menuTestDisplay) Bounds() image.Rectangle { return d.image.Bounds() }
func (d *menuTestDisplay) ColorModel() color.Model { return pixel.Gray4Model }
func (d *menuTestDisplay) At(x, y int) color.Color { return d.image.At(x, y) }
func (d *menuTestDisplay) Set(x, y int, c color.Color) { d.image.Set(x, y, c) }
type menuTestContext struct {
app.Context
display display.Display
}
func (c *menuTestContext) Display() display.Display { return c.display }
func TestMenuRefreshOnlyOnChanges(t *testing.T) {
data := struct{ Enabled bool }{}
root, err := menu.New(&data, 4, nil)
if err != nil {
t.Fatal(err)
}
ctx := &menuTestContext{display: &menuTestDisplay{image: image.NewRGBA(image.Rect(0, 0, 256, 48))}}
w := NewMenu(image.Pt(256, 48), root)
w.SetStyles(*app.StylesFor(pixel.Gray4Model))
w.Show(ctx)
if !w.Refresh(ctx) {
t.Fatal("initial menu was not drawn")
}
if w.Refresh(ctx) {
t.Fatal("unchanged menu requested another hardware refresh")
}
root.Change(1)
w.Redraw()
if !w.Refresh(ctx) || w.Refresh(ctx) {
t.Fatal("menu change should trigger exactly one refresh")
}
w.Hide(ctx)
w.Show(ctx)
if !w.Refresh(ctx) {
t.Fatal("reopened menu was not drawn")
}
}