| 1 |
package boxlayout |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/stretchr/testify/assert" |
| 7 |
) |
| 8 |
|
| 9 |
func TestArrangeWindows(t *testing.T) { |
| 10 |
type scenario struct { |
| 11 |
testName string |
| 12 |
root *Box |
| 13 |
x0 int |
| 14 |
y0 int |
| 15 |
width int |
| 16 |
height int |
| 17 |
test func(result map[string]Dimensions) |
| 18 |
} |
| 19 |
|
| 20 |
scenarios := []scenario{ |
| 21 |
{ |
| 22 |
testName: "Empty box", |
| 23 |
root: &Box{}, |
| 24 |
x0: 0, |
| 25 |
y0: 0, |
| 26 |
width: 10, |
| 27 |
height: 10, |
| 28 |
test: func(result map[string]Dimensions) { |
| 29 |
assert.EqualValues(t, result, map[string]Dimensions{}) |
| 30 |
}, |
| 31 |
}, |
| 32 |
{ |
| 33 |
testName: "Box with static and dynamic panel", |
| 34 |
root: &Box{Children: []*Box{{Size: 1, Window: "static"}, {Weight: 1, Window: "dynamic"}}}, |
| 35 |
x0: 0, |
| 36 |
y0: 0, |
| 37 |
width: 10, |
| 38 |
height: 10, |
| 39 |
test: func(result map[string]Dimensions) { |
| 40 |
assert.EqualValues( |
| 41 |
t, |
| 42 |
result, |
| 43 |
map[string]Dimensions{ |
| 44 |
"dynamic": {X0: 0, X1: 9, Y0: 1, Y1: 9}, |
| 45 |
"static": {X0: 0, X1: 9, Y0: 0, Y1: 0}, |
| 46 |
}, |
| 47 |
) |
| 48 |
}, |
| 49 |
}, |
| 50 |
{ |
| 51 |
testName: "Box with static and two dynamic panels", |
| 52 |
root: &Box{Children: []*Box{{Size: 1, Window: "static"}, {Weight: 1, Window: "dynamic1"}, {Weight: 2, Window: "dynamic2"}}}, |
| 53 |
x0: 0, |
| 54 |
y0: 0, |
| 55 |
width: 10, |
| 56 |
height: 10, |
| 57 |
test: func(result map[string]Dimensions) { |
| 58 |
assert.EqualValues( |
| 59 |
t, |
| 60 |
result, |
| 61 |
map[string]Dimensions{ |
| 62 |
"static": {X0: 0, X1: 9, Y0: 0, Y1: 0}, |
| 63 |
"dynamic1": {X0: 0, X1: 9, Y0: 1, Y1: 3}, |
| 64 |
"dynamic2": {X0: 0, X1: 9, Y0: 4, Y1: 9}, |
| 65 |
}, |
| 66 |
) |
| 67 |
}, |
| 68 |
}, |
| 69 |
{ |
| 70 |
testName: "Box with COLUMN direction", |
| 71 |
root: &Box{Direction: COLUMN, Children: []*Box{{Size: 1, Window: "static"}, {Weight: 1, Window: "dynamic1"}, {Weight: 2, Window: "dynamic2"}}}, |
| 72 |
x0: 0, |
| 73 |
y0: 0, |
| 74 |
width: 10, |
| 75 |
height: 10, |
| 76 |
test: func(result map[string]Dimensions) { |
| 77 |
assert.EqualValues( |
| 78 |
t, |
| 79 |
result, |
| 80 |
map[string]Dimensions{ |
| 81 |
"static": {X0: 0, X1: 0, Y0: 0, Y1: 9}, |
| 82 |
"dynamic1": {X0: 1, X1: 3, Y0: 0, Y1: 9}, |
| 83 |
"dynamic2": {X0: 4, X1: 9, Y0: 0, Y1: 9}, |
| 84 |
}, |
| 85 |
) |
| 86 |
}, |
| 87 |
}, |
| 88 |
{ |
| 89 |
testName: "Box with COLUMN direction only on wide boxes with narrow box", |
| 90 |
root: &Box{ConditionalDirection: func(width int, height int) Direction { |
| 91 |
if width > 4 { |
| 92 |
return COLUMN |
| 93 |
} else { |
| 94 |
return ROW |
| 95 |
} |
| 96 |
}, Children: []*Box{{Weight: 1, Window: "dynamic1"}, {Weight: 1, Window: "dynamic2"}}}, |
| 97 |
x0: 0, |
| 98 |
y0: 0, |
| 99 |
width: 4, |
| 100 |
height: 4, |
| 101 |
test: func(result map[string]Dimensions) { |
| 102 |
assert.EqualValues( |
| 103 |
t, |
| 104 |
result, |
| 105 |
map[string]Dimensions{ |
| 106 |
"dynamic1": {X0: 0, X1: 3, Y0: 0, Y1: 1}, |
| 107 |
"dynamic2": {X0: 0, X1: 3, Y0: 2, Y1: 3}, |
| 108 |
}, |
| 109 |
) |
| 110 |
}, |
| 111 |
}, |
| 112 |
{ |
| 113 |
testName: "Box with COLUMN direction only on wide boxes with wide box", |
| 114 |
root: &Box{ConditionalDirection: func(width int, height int) Direction { |
| 115 |
if width > 4 { |
| 116 |
return COLUMN |
| 117 |
} else { |
| 118 |
return ROW |
| 119 |
} |
| 120 |
}, Children: []*Box{{Weight: 1, Window: "dynamic1"}, {Weight: 1, Window: "dynamic2"}}}, |
| 121 |
|
| 122 |
x0: 0, |
| 123 |
y0: 0, |
| 124 |
width: 5, |
| 125 |
height: 5, |
| 126 |
test: func(result map[string]Dimensions) { |
| 127 |
assert.EqualValues( |
| 128 |
t, |
| 129 |
result, |
| 130 |
map[string]Dimensions{ |
| 131 |
"dynamic1": {X0: 0, X1: 2, Y0: 0, Y1: 4}, |
| 132 |
"dynamic2": {X0: 3, X1: 4, Y0: 0, Y1: 4}, |
| 133 |
}, |
| 134 |
) |
| 135 |
}, |
| 136 |
}, |
| 137 |
{ |
| 138 |
testName: "Box with conditional children where box is wide", |
| 139 |
root: &Box{ConditionalChildren: func(width int, height int) []*Box { |
| 140 |
if width > 4 { |
| 141 |
return []*Box{{Window: "wide", Weight: 1}} |
| 142 |
} else { |
| 143 |
return []*Box{{Window: "narrow", Weight: 1}} |
| 144 |
} |
| 145 |
}}, |
| 146 |
x0: 0, |
| 147 |
y0: 0, |
| 148 |
width: 5, |
| 149 |
height: 5, |
| 150 |
test: func(result map[string]Dimensions) { |
| 151 |
assert.EqualValues( |
| 152 |
t, |
| 153 |
result, |
| 154 |
map[string]Dimensions{ |
| 155 |
"wide": {X0: 0, X1: 4, Y0: 0, Y1: 4}, |
| 156 |
}, |
| 157 |
) |
| 158 |
}, |
| 159 |
}, |
| 160 |
{ |
| 161 |
testName: "Box with conditional children where box is narrow", |
| 162 |
root: &Box{ConditionalChildren: func(width int, height int) []*Box { |
| 163 |
if width > 4 { |
| 164 |
return []*Box{{Window: "wide", Weight: 1}} |
| 165 |
} else { |
| 166 |
return []*Box{{Window: "narrow", Weight: 1}} |
| 167 |
} |
| 168 |
}}, |
| 169 |
x0: 0, |
| 170 |
y0: 0, |
| 171 |
width: 4, |
| 172 |
height: 4, |
| 173 |
test: func(result map[string]Dimensions) { |
| 174 |
assert.EqualValues( |
| 175 |
t, |
| 176 |
result, |
| 177 |
map[string]Dimensions{ |
| 178 |
"narrow": {X0: 0, X1: 3, Y0: 0, Y1: 3}, |
| 179 |
}, |
| 180 |
) |
| 181 |
}, |
| 182 |
}, |
| 183 |
{ |
| 184 |
testName: "Box with static child with size too large", |
| 185 |
root: &Box{Direction: COLUMN, Children: []*Box{{Size: 11, Window: "static"}, {Weight: 1, Window: "dynamic1"}, {Weight: 2, Window: "dynamic2"}}}, |
| 186 |
x0: 0, |
| 187 |
y0: 0, |
| 188 |
width: 10, |
| 189 |
height: 10, |
| 190 |
test: func(result map[string]Dimensions) { |
| 191 |
assert.EqualValues( |
| 192 |
t, |
| 193 |
result, |
| 194 |
map[string]Dimensions{ |
| 195 |
"static": {X0: 0, X1: 9, Y0: 0, Y1: 9}, |
| 196 |
|
| 197 |
|
| 198 |
"dynamic1": {X0: 10, X1: 9, Y0: 0, Y1: 9}, |
| 199 |
"dynamic2": {X0: 10, X1: 9, Y0: 0, Y1: 9}, |
| 200 |
}, |
| 201 |
) |
| 202 |
}, |
| 203 |
}, |
| 204 |
{ |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
testName: "Distributing remainder across weighted boxes", |
| 209 |
root: &Box{Direction: COLUMN, Children: []*Box{{Size: 2, Window: "static"}, {Weight: 1, Window: "dynamic1"}, {Weight: 2, Window: "dynamic2"}}}, |
| 210 |
x0: 0, |
| 211 |
y0: 0, |
| 212 |
width: 10, |
| 213 |
height: 10, |
| 214 |
test: func(result map[string]Dimensions) { |
| 215 |
assert.EqualValues( |
| 216 |
t, |
| 217 |
result, |
| 218 |
map[string]Dimensions{ |
| 219 |
"static": {X0: 0, X1: 1, Y0: 0, Y1: 9}, |
| 220 |
"dynamic1": {X0: 2, X1: 4, Y0: 0, Y1: 9}, |
| 221 |
"dynamic2": {X0: 5, X1: 9, Y0: 0, Y1: 9}, |
| 222 |
}, |
| 223 |
) |
| 224 |
}, |
| 225 |
}, |
| 226 |
{ |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
testName: "Distributing remainder across weighted boxes 2", |
| 232 |
root: &Box{Direction: COLUMN, Children: []*Box{{Weight: 2, Window: "dynamic1"}, {Weight: 2, Window: "dynamic2"}, {Weight: 1, Window: "dynamic3"}}}, |
| 233 |
x0: 0, |
| 234 |
y0: 0, |
| 235 |
width: 9, |
| 236 |
height: 10, |
| 237 |
test: func(result map[string]Dimensions) { |
| 238 |
assert.EqualValues( |
| 239 |
t, |
| 240 |
result, |
| 241 |
map[string]Dimensions{ |
| 242 |
"dynamic1": {X0: 0, X1: 3, Y0: 0, Y1: 9}, |
| 243 |
"dynamic2": {X0: 4, X1: 6, Y0: 0, Y1: 9}, |
| 244 |
"dynamic3": {X0: 7, X1: 8, Y0: 0, Y1: 9}, |
| 245 |
}, |
| 246 |
) |
| 247 |
}, |
| 248 |
}, |
| 249 |
{ |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
testName: "Distributing remainder across weighted boxes with unnormalized weights", |
| 255 |
root: &Box{Direction: COLUMN, Children: []*Box{{Weight: 4, Window: "dynamic1"}, {Weight: 4, Window: "dynamic2"}, {Weight: 2, Window: "dynamic3"}}}, |
| 256 |
x0: 0, |
| 257 |
y0: 0, |
| 258 |
width: 9, |
| 259 |
height: 10, |
| 260 |
test: func(result map[string]Dimensions) { |
| 261 |
assert.EqualValues( |
| 262 |
t, |
| 263 |
result, |
| 264 |
map[string]Dimensions{ |
| 265 |
"dynamic1": {X0: 0, X1: 3, Y0: 0, Y1: 9}, |
| 266 |
"dynamic2": {X0: 4, X1: 6, Y0: 0, Y1: 9}, |
| 267 |
"dynamic3": {X0: 7, X1: 8, Y0: 0, Y1: 9}, |
| 268 |
}, |
| 269 |
) |
| 270 |
}, |
| 271 |
}, |
| 272 |
{ |
| 273 |
testName: "Another distribution test", |
| 274 |
root: &Box{Direction: COLUMN, Children: []*Box{ |
| 275 |
{Weight: 3, Window: "dynamic1"}, |
| 276 |
{Weight: 1, Window: "dynamic2"}, |
| 277 |
{Weight: 1, Window: "dynamic3"}, |
| 278 |
}}, |
| 279 |
x0: 0, |
| 280 |
y0: 0, |
| 281 |
width: 9, |
| 282 |
height: 10, |
| 283 |
test: func(result map[string]Dimensions) { |
| 284 |
assert.EqualValues( |
| 285 |
t, |
| 286 |
result, |
| 287 |
map[string]Dimensions{ |
| 288 |
"dynamic1": {X0: 0, X1: 4, Y0: 0, Y1: 9}, |
| 289 |
"dynamic2": {X0: 5, X1: 6, Y0: 0, Y1: 9}, |
| 290 |
"dynamic3": {X0: 7, X1: 8, Y0: 0, Y1: 9}, |
| 291 |
}, |
| 292 |
) |
| 293 |
}, |
| 294 |
}, |
| 295 |
{ |
| 296 |
testName: "Box with zero weight", |
| 297 |
root: &Box{Direction: COLUMN, Children: []*Box{ |
| 298 |
{Weight: 1, Window: "dynamic1"}, |
| 299 |
{Weight: 0, Window: "dynamic2"}, |
| 300 |
}}, |
| 301 |
x0: 0, |
| 302 |
y0: 0, |
| 303 |
width: 10, |
| 304 |
height: 10, |
| 305 |
test: func(result map[string]Dimensions) { |
| 306 |
assert.EqualValues( |
| 307 |
t, |
| 308 |
result, |
| 309 |
map[string]Dimensions{ |
| 310 |
"dynamic1": {X0: 0, X1: 9, Y0: 0, Y1: 9}, |
| 311 |
"dynamic2": {X0: 10, X1: 9, Y0: 0, Y1: 9}, |
| 312 |
}, |
| 313 |
) |
| 314 |
}, |
| 315 |
}, |
| 316 |
} |
| 317 |
|
| 318 |
for _, s := range scenarios { |
| 319 |
s := s |
| 320 |
t.Run(s.testName, func(t *testing.T) { |
| 321 |
s.test(ArrangeWindows(s.root, s.x0, s.y0, s.width, s.height)) |
| 322 |
}) |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
func TestNormalizeWeights(t *testing.T) { |
| 327 |
scenarios := []struct { |
| 328 |
testName string |
| 329 |
input []int |
| 330 |
expected []int |
| 331 |
}{ |
| 332 |
{ |
| 333 |
testName: "empty", |
| 334 |
input: []int{}, |
| 335 |
expected: []int{}, |
| 336 |
}, |
| 337 |
{ |
| 338 |
testName: "one item of value 1", |
| 339 |
input: []int{1}, |
| 340 |
expected: []int{1}, |
| 341 |
}, |
| 342 |
{ |
| 343 |
testName: "one item of value greater than 1", |
| 344 |
input: []int{2}, |
| 345 |
expected: []int{1}, |
| 346 |
}, |
| 347 |
{ |
| 348 |
testName: "slice contains 1", |
| 349 |
input: []int{2, 1}, |
| 350 |
expected: []int{2, 1}, |
| 351 |
}, |
| 352 |
{ |
| 353 |
testName: "slice contains 2 and 2", |
| 354 |
input: []int{2, 2}, |
| 355 |
expected: []int{1, 1}, |
| 356 |
}, |
| 357 |
{ |
| 358 |
testName: "no common multiple", |
| 359 |
input: []int{2, 3}, |
| 360 |
expected: []int{2, 3}, |
| 361 |
}, |
| 362 |
{ |
| 363 |
testName: "complex case", |
| 364 |
input: []int{10, 10, 20}, |
| 365 |
expected: []int{1, 1, 2}, |
| 366 |
}, |
| 367 |
{ |
| 368 |
testName: "when a zero weight is included it is ignored", |
| 369 |
input: []int{10, 10, 20, 0}, |
| 370 |
expected: []int{1, 1, 2, 0}, |
| 371 |
}, |
| 372 |
} |
| 373 |
|
| 374 |
for _, s := range scenarios { |
| 375 |
s := s |
| 376 |
t.Run(s.testName, func(t *testing.T) { |
| 377 |
assert.EqualValues(t, s.expected, normalizeWeights(s.input)) |
| 378 |
}) |
| 379 |
} |
| 380 |
} |