zntg/ui/spacing_test.go
Sander Schobers 75fce53716 Added Paragraph control.
Extended DesiredSize method with extra arguments that tells the control how much space is available for the parent control (note: this might not be the actual given size when Arrange is called on the control).
2020-05-16 15:37:53 +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, size geom.PointF32) geom.PointF32 {
if m.Size != nil {
return *m.Size
}
return m.ControlBase.DesiredSize(ctx, size)
}
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)
}