| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package flex |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"fmt" |
| 10 |
"image" |
| 11 |
"image/color" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"golang.org/x/exp/shiny/unit" |
| 15 |
"golang.org/x/exp/shiny/widget" |
| 16 |
"golang.org/x/exp/shiny/widget/node" |
| 17 |
"golang.org/x/exp/shiny/widget/theme" |
| 18 |
) |
| 19 |
|
| 20 |
type layoutTest struct { |
| 21 |
desc string |
| 22 |
direction Direction |
| 23 |
wrap FlexWrap |
| 24 |
alignContent AlignContent |
| 25 |
justify Justify |
| 26 |
size image.Point |
| 27 |
measured [][2]float64 |
| 28 |
layoutData []LayoutData |
| 29 |
want []image.Rectangle |
| 30 |
} |
| 31 |
|
| 32 |
func (t *layoutTest) html() string { |
| 33 |
buf := new(bytes.Buffer) |
| 34 |
fmt.Fprintf(buf, `<style> |
| 35 |
#container { |
| 36 |
display: flex; |
| 37 |
width: %dpx; |
| 38 |
height: %dpx; |
| 39 |
`, t.size.X, t.size.Y) |
| 40 |
|
| 41 |
switch t.direction { |
| 42 |
case Row: |
| 43 |
case RowReverse: |
| 44 |
fmt.Fprintf(buf, "\tflex-direction: row-reverse;\n") |
| 45 |
case Column: |
| 46 |
fmt.Fprintf(buf, "\tflex-direction: column;\n") |
| 47 |
case ColumnReverse: |
| 48 |
fmt.Fprintf(buf, "\tflex-direction: column-reverse;\n") |
| 49 |
} |
| 50 |
switch t.wrap { |
| 51 |
case NoWrap: |
| 52 |
case Wrap: |
| 53 |
fmt.Fprintf(buf, "\tflex-wrap: wrap;\n") |
| 54 |
case WrapReverse: |
| 55 |
fmt.Fprintf(buf, "\tflex-wrap: wrap-reverse;\n") |
| 56 |
} |
| 57 |
switch t.alignContent { |
| 58 |
case AlignContentStart: |
| 59 |
case AlignContentEnd: |
| 60 |
fmt.Fprintf(buf, "\talign-content: flex-end;\n") |
| 61 |
case AlignContentCenter: |
| 62 |
fmt.Fprintf(buf, "\talign-content: center;\n") |
| 63 |
case AlignContentSpaceBetween: |
| 64 |
fmt.Fprintf(buf, "\talign-content: space-between;\n") |
| 65 |
case AlignContentSpaceAround: |
| 66 |
fmt.Fprintf(buf, "\talign-content: space-around;\n") |
| 67 |
case AlignContentStretch: |
| 68 |
fmt.Fprintf(buf, "\talign-content: stretch;\n") |
| 69 |
} |
| 70 |
switch t.justify { |
| 71 |
case JustifyStart: |
| 72 |
case JustifyEnd: |
| 73 |
fmt.Fprintf(buf, "\tjustify-content: flex-end;\n") |
| 74 |
case JustifyCenter: |
| 75 |
fmt.Fprintf(buf, "\tjustify-content: center;\n") |
| 76 |
case JustifySpaceBetween: |
| 77 |
fmt.Fprintf(buf, "\tjustify-content: space-between;\n") |
| 78 |
case JustifySpaceAround: |
| 79 |
fmt.Fprintf(buf, "\tjustify-content: space-around;\n") |
| 80 |
} |
| 81 |
fmt.Fprintf(buf, "}\n") |
| 82 |
|
| 83 |
for i, m := range t.measured { |
| 84 |
fmt.Fprintf(buf, `#child%d { |
| 85 |
width: %.2fpx; |
| 86 |
height: %.2fpx; |
| 87 |
`, i, m[0], m[1]) |
| 88 |
c := colors[i%len(colors)] |
| 89 |
fmt.Fprintf(buf, "\tbackground-color: rgb(%d, %d, %d);\n", c.R, c.G, c.B) |
| 90 |
if t.layoutData != nil { |
| 91 |
d := t.layoutData[i] |
| 92 |
if d.MinSize.X != 0 { |
| 93 |
fmt.Fprintf(buf, "\tmin-width: %dpx;\n", d.MinSize.X) |
| 94 |
} |
| 95 |
if d.MinSize.Y != 0 { |
| 96 |
fmt.Fprintf(buf, "\tmin-height: %dpx;\n", d.MinSize.Y) |
| 97 |
} |
| 98 |
if d.MaxSize != nil { |
| 99 |
fmt.Fprintf(buf, "\tmax-width: %dpx;\n", d.MaxSize.X) |
| 100 |
fmt.Fprintf(buf, "\tmax-height: %dpx;\n", d.MaxSize.Y) |
| 101 |
} |
| 102 |
if d.Grow != 0 { |
| 103 |
fmt.Fprintf(buf, "\tflex-grow: %f;\n", d.Grow) |
| 104 |
} |
| 105 |
if d.Shrink != nil { |
| 106 |
fmt.Fprintf(buf, "\tflex-shrink: %f;\n", *d.Shrink) |
| 107 |
} |
| 108 |
|
| 109 |
} |
| 110 |
fmt.Fprintf(buf, "}\n") |
| 111 |
} |
| 112 |
fmt.Fprintf(buf, `</style> |
| 113 |
<div id="container"> |
| 114 |
`) |
| 115 |
for i := range t.measured { |
| 116 |
fmt.Fprintf(buf, "\t<div id=\"child%d\"></div>\n", i) |
| 117 |
} |
| 118 |
fmt.Fprintf(buf, `</div> |
| 119 |
<pre id="out"></pre> |
| 120 |
<script> |
| 121 |
var out = document.getElementById("out"); |
| 122 |
var container = document.getElementById("container"); |
| 123 |
for (var i = 0; i < container.children.length; i++) { |
| 124 |
var c = container.children[i]; |
| 125 |
var ctop = c.offsetTop - container.offsetTop; |
| 126 |
var cleft = c.offsetLeft - container.offsetLeft; |
| 127 |
var cbottom = ctop + c.offsetHeight; |
| 128 |
var cright = cleft + c.offsetWidth; |
| 129 |
|
| 130 |
out.innerHTML += "\timage.Rect(" + cleft + ", " + ctop + ", " + cright + ", " + cbottom + "),\n"; |
| 131 |
} |
| 132 |
</script> |
| 133 |
`) |
| 134 |
|
| 135 |
return buf.String() |
| 136 |
} |
| 137 |
|
| 138 |
var colors = []color.RGBA{ |
| 139 |
{0x00, 0x7f, 0x7f, 0xff}, |
| 140 |
{0x7f, 0x00, 0x7f, 0xff}, |
| 141 |
{0x7f, 0x7f, 0x00, 0xff}, |
| 142 |
{0xff, 0x00, 0x00, 0xff}, |
| 143 |
{0x00, 0xff, 0x00, 0xff}, |
| 144 |
{0x00, 0x00, 0xff, 0xff}, |
| 145 |
} |
| 146 |
|
| 147 |
var layoutTests = []layoutTest{{ |
| 148 |
desc: "no children", |
| 149 |
}, { |
| 150 |
desc: "no children wrapped", |
| 151 |
wrap: Wrap, |
| 152 |
}, { |
| 153 |
desc: "single unflexed child", |
| 154 |
size: image.Point{100, 100}, |
| 155 |
measured: [][2]float64{{100, 100}}, |
| 156 |
want: []image.Rectangle{ |
| 157 |
image.Rect(0, 0, 100, 100), |
| 158 |
}, |
| 159 |
}, { |
| 160 |
desc: "unflexed children", |
| 161 |
size: image.Point{350, 100}, |
| 162 |
measured: [][2]float64{{100, 100}, {100, 100}, {100, 100}}, |
| 163 |
want: []image.Rectangle{ |
| 164 |
image.Rect(0, 0, 100, 100), |
| 165 |
image.Rect(100, 0, 200, 100), |
| 166 |
image.Rect(200, 0, 300, 100), |
| 167 |
}, |
| 168 |
}, { |
| 169 |
desc: "final child that grows", |
| 170 |
size: image.Point{300, 100}, |
| 171 |
measured: [][2]float64{{100, 100}, {100, 100}}, |
| 172 |
want: []image.Rectangle{ |
| 173 |
image.Rect(0, 0, 100, 100), |
| 174 |
image.Rect(100, 0, 300, 100), |
| 175 |
}, |
| 176 |
layoutData: []LayoutData{{}, {Grow: 1}}, |
| 177 |
}, { |
| 178 |
desc: "share growth equally", |
| 179 |
size: image.Point{300, 100}, |
| 180 |
measured: [][2]float64{{50, 50}, {100, 100}, {100, 100}}, |
| 181 |
want: []image.Rectangle{ |
| 182 |
image.Rect(0, 0, 50, 50), |
| 183 |
image.Rect(50, 0, 175, 100), |
| 184 |
image.Rect(175, 0, 300, 100), |
| 185 |
}, |
| 186 |
layoutData: []LayoutData{{}, {Grow: 1}, {Grow: 1}}, |
| 187 |
}, { |
| 188 |
desc: "share growth inequally", |
| 189 |
size: image.Point{300, 100}, |
| 190 |
measured: [][2]float64{{20, 100}, {20, 100}, {20, 100}}, |
| 191 |
want: []image.Rectangle{ |
| 192 |
image.Rect(0, 0, 30, 100), |
| 193 |
image.Rect(30, 0, 130, 100), |
| 194 |
image.Rect(130, 0, 300, 100), |
| 195 |
}, |
| 196 |
layoutData: []LayoutData{ |
| 197 |
{MaxSize: &image.Point{30, 100}, Grow: 1}, |
| 198 |
{MinSize: image.Point{100, 0}, Grow: 1}, |
| 199 |
{Grow: 4}, |
| 200 |
}, |
| 201 |
}, { |
| 202 |
desc: "wrap", |
| 203 |
size: image.Point{300, 200}, |
| 204 |
wrap: Wrap, |
| 205 |
measured: [][2]float64{{150, 100}, {280, 100}, {20, 100}}, |
| 206 |
want: []image.Rectangle{ |
| 207 |
image.Rect(0, 0, 30, 100), |
| 208 |
image.Rect(0, 100, 280, 200), |
| 209 |
image.Rect(280, 100, 300, 200), |
| 210 |
}, |
| 211 |
layoutData: []LayoutData{ |
| 212 |
{MaxSize: &image.Point{30, 100}, Grow: 1}, |
| 213 |
{MinSize: image.Point{100, 0}, Grow: 1}, |
| 214 |
{Grow: 1}, |
| 215 |
}, |
| 216 |
}, { |
| 217 |
desc: "align-content default", |
| 218 |
size: image.Point{300, 200}, |
| 219 |
direction: Column, |
| 220 |
wrap: Wrap, |
| 221 |
measured: [][2]float64{{150, 100}, {160, 100}, {20, 100}, {300, 300}}, |
| 222 |
want: []image.Rectangle{ |
| 223 |
image.Rect(0, 0, 30, 100), |
| 224 |
image.Rect(0, 100, 160, 200), |
| 225 |
image.Rect(220, 0, 240, 195), |
| 226 |
image.Rect(220, 195, 225, 200), |
| 227 |
}, |
| 228 |
layoutData: []LayoutData{ |
| 229 |
{MaxSize: &image.Point{30, 100}, Grow: 1}, |
| 230 |
{MinSize: image.Point{100, 0}, Grow: 1}, |
| 231 |
{Grow: 1}, |
| 232 |
{MaxSize: &image.Point{5, 5}}, |
| 233 |
}, |
| 234 |
}, { |
| 235 |
desc: "align-content: space-around", |
| 236 |
size: image.Point{300, 200}, |
| 237 |
direction: Column, |
| 238 |
wrap: Wrap, |
| 239 |
alignContent: AlignContentSpaceAround, |
| 240 |
measured: [][2]float64{{150, 100}, {160, 100}, {20, 100}, {300, 300}}, |
| 241 |
want: []image.Rectangle{ |
| 242 |
image.Rect(30, 0, 60, 100), |
| 243 |
image.Rect(30, 100, 190, 200), |
| 244 |
image.Rect(250, 0, 270, 195), |
| 245 |
image.Rect(250, 195, 255, 200), |
| 246 |
}, |
| 247 |
layoutData: []LayoutData{ |
| 248 |
{MaxSize: &image.Point{30, 100}, Grow: 1}, |
| 249 |
{MinSize: image.Point{100, 0}, Grow: 1}, |
| 250 |
{Grow: 1}, |
| 251 |
{MaxSize: &image.Point{5, 5}}, |
| 252 |
}, |
| 253 |
}, { |
| 254 |
desc: "align-content: space-between", |
| 255 |
size: image.Point{300, 200}, |
| 256 |
direction: Column, |
| 257 |
wrap: Wrap, |
| 258 |
alignContent: AlignContentSpaceBetween, |
| 259 |
measured: [][2]float64{{150, 100}, {160, 100}, {20, 100}, {300, 300}}, |
| 260 |
want: []image.Rectangle{ |
| 261 |
image.Rect(0, 0, 30, 100), |
| 262 |
image.Rect(0, 100, 160, 200), |
| 263 |
image.Rect(280, 0, 300, 195), |
| 264 |
image.Rect(280, 195, 285, 200), |
| 265 |
}, |
| 266 |
layoutData: []LayoutData{ |
| 267 |
{MaxSize: &image.Point{30, 100}, Grow: 1}, |
| 268 |
{MinSize: image.Point{100, 0}, Grow: 1}, |
| 269 |
{Grow: 1}, |
| 270 |
{MaxSize: &image.Point{5, 5}}, |
| 271 |
}, |
| 272 |
}, { |
| 273 |
desc: "align-content: end", |
| 274 |
size: image.Point{300, 200}, |
| 275 |
direction: Column, |
| 276 |
wrap: Wrap, |
| 277 |
alignContent: AlignContentEnd, |
| 278 |
measured: [][2]float64{{150, 100}, {160, 100}, {20, 100}, {300, 300}}, |
| 279 |
want: []image.Rectangle{ |
| 280 |
image.Rect(120, 0, 150, 100), |
| 281 |
image.Rect(120, 100, 280, 200), |
| 282 |
image.Rect(280, 0, 300, 195), |
| 283 |
image.Rect(280, 195, 285, 200), |
| 284 |
}, |
| 285 |
layoutData: []LayoutData{ |
| 286 |
{MaxSize: &image.Point{30, 100}, Grow: 1}, |
| 287 |
{MinSize: image.Point{100, 0}, Grow: 1}, |
| 288 |
{Grow: 1}, |
| 289 |
{MaxSize: &image.Point{5, 5}}, |
| 290 |
}, |
| 291 |
}, { |
| 292 |
desc: "align-content: center", |
| 293 |
size: image.Point{300, 200}, |
| 294 |
direction: Column, |
| 295 |
wrap: Wrap, |
| 296 |
alignContent: AlignContentCenter, |
| 297 |
measured: [][2]float64{{150, 100}, {160, 100}, {20, 100}, {300, 300}}, |
| 298 |
want: []image.Rectangle{ |
| 299 |
image.Rect(60, 0, 90, 100), |
| 300 |
image.Rect(60, 100, 220, 200), |
| 301 |
image.Rect(220, 0, 240, 195), |
| 302 |
image.Rect(220, 195, 225, 200), |
| 303 |
}, |
| 304 |
layoutData: []LayoutData{ |
| 305 |
{MaxSize: &image.Point{30, 100}, Grow: 1}, |
| 306 |
{MinSize: image.Point{100, 0}, Grow: 1}, |
| 307 |
{Grow: 1}, |
| 308 |
{MaxSize: &image.Point{5, 5}}, |
| 309 |
}, |
| 310 |
}, { |
| 311 |
desc: "column-reverse", |
| 312 |
size: image.Point{300, 60}, |
| 313 |
direction: ColumnReverse, |
| 314 |
wrap: Wrap, |
| 315 |
measured: [][2]float64{{25, 25}, {25, 25}, {25, 25}, {25, 25}, {25, 25}}, |
| 316 |
want: []image.Rectangle{ |
| 317 |
image.Rect(0, 35, 25, 60), |
| 318 |
image.Rect(0, 0, 25, 35), |
| 319 |
image.Rect(100, 35, 125, 60), |
| 320 |
image.Rect(100, 10, 125, 35), |
| 321 |
image.Rect(200, 0, 225, 60), |
| 322 |
}, |
| 323 |
layoutData: []LayoutData{ |
| 324 |
{}, |
| 325 |
{Grow: 1}, |
| 326 |
{}, |
| 327 |
{}, |
| 328 |
{Grow: 1}, |
| 329 |
}, |
| 330 |
}, { |
| 331 |
desc: "justify-content: flex-start", |
| 332 |
size: image.Point{90, 90}, |
| 333 |
measured: [][2]float64{{5, 10}, {5, 10}, {10, 10}}, |
| 334 |
justify: JustifyStart, |
| 335 |
want: []image.Rectangle{ |
| 336 |
image.Rect(0, 0, 5, 10), |
| 337 |
image.Rect(5, 0, 10, 10), |
| 338 |
image.Rect(10, 0, 20, 10), |
| 339 |
}, |
| 340 |
}, { |
| 341 |
desc: "justify-content: flex-end", |
| 342 |
size: image.Point{90, 90}, |
| 343 |
measured: [][2]float64{{5, 10}, {5, 10}, {10, 10}}, |
| 344 |
justify: JustifyEnd, |
| 345 |
want: []image.Rectangle{ |
| 346 |
image.Rect(70, 0, 75, 10), |
| 347 |
image.Rect(75, 0, 80, 10), |
| 348 |
image.Rect(80, 0, 90, 10), |
| 349 |
}, |
| 350 |
}, { |
| 351 |
desc: "justify-content: center", |
| 352 |
size: image.Point{90, 90}, |
| 353 |
measured: [][2]float64{{5, 10}, {5, 10}, {10, 10}}, |
| 354 |
justify: JustifyCenter, |
| 355 |
want: []image.Rectangle{ |
| 356 |
image.Rect(35, 0, 40, 10), |
| 357 |
image.Rect(40, 0, 45, 10), |
| 358 |
image.Rect(45, 0, 55, 10), |
| 359 |
}, |
| 360 |
}, { |
| 361 |
desc: "justify-content: space-between", |
| 362 |
size: image.Point{90, 90}, |
| 363 |
measured: [][2]float64{{5, 10}, {5, 10}, {10, 10}}, |
| 364 |
justify: JustifySpaceBetween, |
| 365 |
want: []image.Rectangle{ |
| 366 |
image.Rect(0, 0, 5, 10), |
| 367 |
image.Rect(40, 0, 45, 10), |
| 368 |
image.Rect(80, 0, 90, 10), |
| 369 |
}, |
| 370 |
}, { |
| 371 |
desc: "justify-content: space-around", |
| 372 |
size: image.Point{90, 90}, |
| 373 |
measured: [][2]float64{{5, 10}, {5, 10}, {10, 10}}, |
| 374 |
justify: JustifySpaceAround, |
| 375 |
want: []image.Rectangle{ |
| 376 |
image.Rect(12, 0, 17, 10), |
| 377 |
image.Rect(40, 0, 45, 10), |
| 378 |
image.Rect(68, 0, 78, 10), |
| 379 |
}, |
| 380 |
}} |
| 381 |
|
| 382 |
func TestLayout(t *testing.T) { |
| 383 |
for testNum, test := range layoutTests { |
| 384 |
var children []node.Node |
| 385 |
for i, sz := range test.measured { |
| 386 |
u := widget.NewUniform(theme.StaticColor(colors[i]), nil) |
| 387 |
n := widget.NewSizer(unit.Pixels(sz[0]), unit.Pixels(sz[1]), u) |
| 388 |
if test.layoutData != nil { |
| 389 |
n.LayoutData = test.layoutData[i] |
| 390 |
} |
| 391 |
children = append(children, n) |
| 392 |
} |
| 393 |
|
| 394 |
w := NewFlex(children...) |
| 395 |
w.Direction = test.direction |
| 396 |
w.Wrap = test.wrap |
| 397 |
w.AlignContent = test.alignContent |
| 398 |
w.Justify = test.justify |
| 399 |
|
| 400 |
w.Measure(nil, node.NoHint, node.NoHint) |
| 401 |
w.Rect = image.Rectangle{Max: test.size} |
| 402 |
w.Layout(nil) |
| 403 |
|
| 404 |
bad := false |
| 405 |
for i, n := range children { |
| 406 |
if n.Wrappee().Rect != test.want[i] { |
| 407 |
bad = true |
| 408 |
break |
| 409 |
} |
| 410 |
} |
| 411 |
if bad { |
| 412 |
t.Logf("Bad test %d, %q:\n%s", testNum, test.desc, test.html()) |
| 413 |
} |
| 414 |
for i, n := range children { |
| 415 |
if got, want := n.Wrappee().Rect, test.want[i]; got != want { |
| 416 |
t.Errorf("[%d].Rect=%v, want %v", i, got, want) |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
} |