layout: add test that Flex doesn't allocate

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2020-06-02 11:37:45 +02:00
parent 2bdf8c3851
commit ce0cc706ad
+67
View File
@@ -0,0 +1,67 @@
// SPDX-License-Identifier: Unlicense OR MIT
package layout
import (
"image"
"testing"
"gioui.org/op"
)
func TestStack(t *testing.T) {
gtx := Context{
Ops: new(op.Ops),
Constraints: Constraints{
Max: image.Pt(100, 100),
},
}
exp := image.Point{X: 60, Y: 70}
dims := Stack{Alignment: Center}.Layout(gtx,
Expanded(func(gtx Context) Dimensions {
return Dimensions{Size: exp}
}),
Stacked(func(gtx Context) Dimensions {
return Dimensions{Size: image.Point{X: 50, Y: 50}}
}),
)
if got := dims.Size; got != exp {
t.Errorf("Stack ignored Expanded size, got %v expected %v", got, exp)
}
}
func TestStackAllocs(t *testing.T) {
var ops op.Ops
allocs := testing.AllocsPerRun(1, func() {
ops.Reset()
gtx := Context{
Ops: &ops,
}
Stack{}.Layout(gtx,
Stacked(func(gtx Context) Dimensions {
return Dimensions{Size: image.Point{X: 50, Y: 50}}
}),
)
})
if allocs != 0 {
t.Errorf("expected no allocs, got %f", allocs)
}
}
func TestFlexAllocs(t *testing.T) {
var ops op.Ops
allocs := testing.AllocsPerRun(1, func() {
ops.Reset()
gtx := Context{
Ops: &ops,
}
Flex{}.Layout(gtx,
Rigid(func(gtx Context) Dimensions {
return Dimensions{Size: image.Point{X: 50, Y: 50}}
}),
)
})
if allocs != 0 {
t.Errorf("expected no allocs, got %f", allocs)
}
}