zntg/ui/spacing_test.go
Sander Schobers dbc017507c Added Parent to Controls.
Fixed issue where events of partially and invisible controls where incorrectly handled.
2019-04-11 23:38:32 +02:00

80 lines
2.0 KiB
Go

package ui
import (
"testing"
"github.com/stretchr/testify/assert"
"opslag.de/schobers/geom"
)
var _ Control = &Mock{}
type Mock struct {
ControlBase
Size *geom.PointF32
}
func (m *Mock) DesiredSize(ctx Context) geom.PointF32 {
if m.Size != nil {
return *m.Size
}
return m.ControlBase.DesiredSize(ctx)
}
func TestNoStretchFillsAvailableSpace(t *testing.T) {
m := &Mock{}
s := BuildSpacing(m, func(s *Spacing) {
s.SetSize(0, 0)
})
s.Arrange(nil, geom.RectF32(31, 37, 73, 79), geom.ZeroPtF32, nil)
assert.Equal(t, geom.RectF32(31, 37, 73, 79), m.bounds)
}
func TestStretch(t *testing.T) {
m := &Mock{}
s := BuildSpacing(m, func(s *Spacing) {
s.SetSize(geom.NaN32(), geom.NaN32())
})
s.Arrange(nil, geom.RectF32(31, 37, 73, 79), geom.ZeroPtF32, nil)
assert.Equal(t, geom.RectF32(31, 37, 73, 79), m.bounds)
}
func TestCenter(t *testing.T) {
m := &Mock{Size: &geom.PointF32{X: 2, Y: 3}}
s := BuildSpacing(m, func(s *Spacing) {
s.Center()
})
s.Arrange(nil, geom.RectF32(31, 37, 73, 79), geom.ZeroPtF32, nil)
assert.Equal(t, geom.RectF32(51, 56.5, 53, 59.5), m.bounds)
}
func TestFixedMargin(t *testing.T) {
m := &Mock{Size: &geom.PointF32{X: geom.NaN32(), Y: geom.NaN32()}}
s := BuildSpacing(m, func(s *Spacing) {
s.Margin.Left = Fixed(7)
s.Margin.Top = Fixed(2)
s.Margin.Right = Fixed(3)
s.Margin.Bottom = Fixed(5)
})
s.Arrange(nil, geom.RectF32(31, 37, 73, 79), geom.ZeroPtF32, nil)
assert.Equal(t, geom.RectF32(38, 39, 70, 74), m.bounds)
}
func TestRightAlign(t *testing.T) {
m := &Mock{Size: &geom.PointF32{X: 2, Y: 3}}
s := Margins(m, geom.NaN32(), 0, 0, 0)
s.Arrange(nil, geom.RectF32(31, 37, 73, 79), geom.ZeroPtF32, nil)
assert.Equal(t, geom.RectF32(71, 37, 73, 79), m.bounds)
}
func TestRightAlignFixedWidth(t *testing.T) {
m := &Mock{Size: &geom.PointF32{X: 2, Y: 3}}
s := BuildSpacing(m, func(s *Spacing) {
s.Margin.Left = Infinite()
s.Width = Fixed(5)
})
s.Arrange(nil, geom.RectF32(31, 37, 73, 79), geom.ZeroPtF32, nil)
assert.Equal(t, geom.RectF32(68, 37, 73, 79), m.bounds)
}