-
Notifications
You must be signed in to change notification settings - Fork 1
/
align_test.go
148 lines (132 loc) · 4.15 KB
/
align_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package goey
import (
"errors"
"testing"
"bitbucket.org/rj/goey/base"
"bitbucket.org/rj/goey/mock"
)
func (w *alignElement) Props() base.Widget {
child := base.Widget(nil)
if w.child != nil {
child = w.child.(Proper).Props()
}
return &Align{
HAlign: w.hAlign,
VAlign: w.vAlign,
WidthFactor: w.widthFactor,
HeightFactor: w.heightFactor,
Child: child,
}
}
func TestAlignMount(t *testing.T) {
// These should all be able to mount without error.
testingMountWidgets(t,
&Align{Child: &Button{Text: "A"}},
&Align{HAlign: AlignStart, Child: &Button{Text: "B"}},
&Align{HAlign: AlignEnd, Child: &Button{Text: "C"}},
&Align{HAlign: AlignCenter, Child: &Button{Text: "C"}},
&Align{HeightFactor: 2, WidthFactor: 2.5, Child: &Button{Text: "C"}},
&Align{},
)
// These should mount with an error.
err := errors.New("Mock error 1")
testingMountWidgetsFail(t, err,
&Align{Child: &mock.Widget{Err: err}},
)
}
func TestAlignClose(t *testing.T) {
testingCloseWidgets(t,
&Align{Child: &Button{Text: "A"}},
&Align{},
)
}
func TestAlignUpdateProps(t *testing.T) {
testingUpdateWidgets(t, []base.Widget{
&Align{Child: &Button{Text: "A"}},
&Align{HAlign: AlignStart, Child: &Button{Text: "B"}},
&Align{HAlign: AlignEnd, Child: &Button{Text: "C"}},
&Align{HAlign: AlignCenter, Child: &Button{Text: "C"}},
&Align{HeightFactor: 2, WidthFactor: 2.5, Child: &Button{Text: "C"}},
&Align{},
}, []base.Widget{
&Align{Child: &Button{Text: "AB"}},
&Align{HAlign: AlignCenter, Child: &Button{Text: "BC"}},
&Align{HAlign: AlignStart, Child: &Button{Text: "CD"}},
&Align{HAlign: AlignEnd, Child: &Button{Text: "CE"}},
&Align{HeightFactor: 4, WidthFactor: 3},
&Align{Child: &Label{Text: "CF"}},
})
}
func TestAlignLayout(t *testing.T) {
size1 := base.Size{10 * DIP, 20 * DIP}
size2 := base.Size{35 * DIP, 30 * DIP}
sizeZ := base.Size{}
cases := []struct {
in base.Size
widthFactor float64
heightFactor float64
bc base.Constraints
out base.Size
}{
{size1, 0, 0, base.Loose(size1), size1},
{size2, 0, 0, base.Loose(size2), size2},
{size1, 0, 0, base.Loose(size2), size1},
{sizeZ, 0, 0, base.Loose(size1), size1},
{sizeZ, 0, 0, base.Loose(size2), size2},
{size1, 2, 2, base.Loose(size1), size1},
{size2, 2, 2, base.Loose(size2), size2},
{size1, 2, 2, base.Loose(size2), base.Size{20 * DIP, 30 * DIP}},
{size1, 0, 0, base.Tight(size1), size1},
{size2, 0, 0, base.Tight(size2), size2},
{sizeZ, 0, 0, base.Tight(size1), size1},
{size1, 0, 0, base.Expand().Loosen(), size1},
{size2, 0, 0, base.Expand().Loosen(), size2},
{sizeZ, 0, 0, base.Expand().Loosen(), sizeZ},
{size1, 1, 1, base.Expand().Loosen(), size1},
{size2, 1, 1, base.Expand().Loosen(), size2},
{size1, 2, 3, base.Expand().Loosen(), base.Size{20 * DIP, 60 * DIP}},
{size2, 4, 5, base.Expand().Loosen(), base.Size{4 * 35 * DIP, 150 * DIP}},
}
for i, v := range cases {
elem := alignElement{
child: mock.NewIfNotZero(v.in),
widthFactor: v.widthFactor,
heightFactor: v.heightFactor,
}
if out := elem.Layout(v.bc); out != v.out {
t.Errorf("Case %d: Returned size does not match, got %v, want %v", i, out, v.out)
}
}
}
func TestAlignMinIntrinsicSize(t *testing.T) {
size1 := base.Size{10 * DIP, 20 * DIP}
size2 := base.Size{35 * DIP, 30 * DIP}
sizeZ := base.Size{}
cases := []struct {
in base.Size
widthFactor float64
heightFactor float64
out base.Size
}{
{size1, 0, 0, size1},
{size2, 0, 0, size2},
{sizeZ, 0, 0, sizeZ},
{size1, 1, 1, size1},
{size2, 1, 1, size2},
{sizeZ, 1, 1, sizeZ},
{size1, 2, 3, base.Size{20 * DIP, 60 * DIP}},
}
for i, v := range cases {
elem := alignElement{
child: mock.NewIfNotZero(v.in),
widthFactor: v.widthFactor,
heightFactor: v.heightFactor,
}
if out := elem.MinIntrinsicWidth(base.Inf); out != v.out.Width {
t.Errorf("Case %d: Returned min intrinsic width does not match, got %v, want %v", i, out, v.out.Width)
}
if out := elem.MinIntrinsicHeight(base.Inf); out != v.out.Height {
t.Errorf("Case %d: Returned min intrinsic width does not match, got %v, want %v", i, out, v.out.Height)
}
}
}