package net.shadowfacts.shadowui.view import net.shadowfacts.kiwidsl.dsl import net.shadowfacts.shadowui.Window import net.shadowfacts.shadowui.geometry.Axis import net.shadowfacts.shadowui.geometry.Size import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test import kotlin.math.abs /** * @author shadowfacts */ class StackViewTests { lateinit var window: Window @BeforeEach fun setup() { window = Window() } @Test fun testVerticalLayout() { val stack = window.addView(StackView(Axis.VERTICAL)) val one = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(50.0, 50.0) }) val two = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(75.0, 75.0) }) val three = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(50.0, 50.0) }) window.solver.dsl { stack.topAnchor equalTo 0 } window.layout() assertEquals(0.0, abs(one.topAnchor.value)) // sometimes -0.0, which fails the assertion but is actually ok assertEquals(50.0, one.bottomAnchor.value) assertEquals(50.0, two.topAnchor.value) assertEquals(125.0, two.bottomAnchor.value) assertEquals(125.0, three.topAnchor.value) assertEquals(175.0, three.bottomAnchor.value) assertEquals(175.0, stack.heightAnchor.value) } @Test fun testHorizontalLayout() { val stack = window.addView(StackView(Axis.HORIZONTAL)) val one = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(50.0, 50.0) }) val two = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(75.0, 75.0) }) val three = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(50.0, 50.0) }) window.solver.dsl { stack.leftAnchor equalTo 0 } window.layout() assertEquals(0.0, abs(one.leftAnchor.value)) // sometimes -0.0, which fails the assertion but is actually ok assertEquals(50.0, one.rightAnchor.value) assertEquals(50.0, two.leftAnchor.value) assertEquals(125.0, two.rightAnchor.value) assertEquals(125.0, three.leftAnchor.value) assertEquals(175.0, three.rightAnchor.value) assertEquals(175.0, stack.widthAnchor.value) } @Test fun testVerticalLayoutWithLeading() { val stack = window.addView(StackView(Axis.VERTICAL, StackView.Distribution.LEADING)) val one = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(50.0, 50.0) }) val two = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(75.0, 75.0) }) val three = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(100.0, 100.0) }) window.solver.dsl { stack.topAnchor equalTo 0 stack.leftAnchor equalTo 0 stack.rightAnchor equalTo 100 } window.layout() assertEquals(0.0, abs(one.leftAnchor.value)) assertEquals(50.0, one.rightAnchor.value) assertEquals(0.0, abs(two.leftAnchor.value)) assertEquals(75.0, two.rightAnchor.value) assertEquals(0.0, abs(three.leftAnchor.value)) assertEquals(100.0, three.rightAnchor.value) } @Test fun testVerticalLayoutWithTrailing() { val stack = window.addView(StackView(Axis.VERTICAL, StackView.Distribution.TRAILING)) val one = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(50.0, 50.0) }) val two = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(75.0, 75.0) }) val three = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(100.0, 100.0) }) window.solver.dsl { stack.topAnchor equalTo 0 stack.leftAnchor equalTo 0 stack.rightAnchor equalTo 100 } window.layout() assertEquals(50.0, one.leftAnchor.value) assertEquals(100.0, one.rightAnchor.value) assertEquals(25.0, two.leftAnchor.value) assertEquals(100.0, two.rightAnchor.value) assertEquals(0.0, abs(three.leftAnchor.value)) assertEquals(100.0, three.rightAnchor.value) } @Test fun testVerticalLayoutWithCenter() { val stack = window.addView(StackView(Axis.VERTICAL, StackView.Distribution.CENTER)) val one = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(50.0, 50.0) }) val two = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(75.0, 75.0) }) val three = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(100.0, 100.0) }) window.solver.dsl { stack.topAnchor equalTo 0 stack.leftAnchor equalTo 0 stack.rightAnchor equalTo 100 } window.layout() assertEquals(25.0, one.leftAnchor.value) assertEquals(75.0, one.rightAnchor.value) assertEquals(12.5, two.leftAnchor.value) assertEquals(87.5, two.rightAnchor.value) assertEquals(0.0, abs(three.leftAnchor.value)) assertEquals(100.0, three.rightAnchor.value) } @Test fun testVerticalLayoutWithFill() { val stack = window.addView(StackView(Axis.VERTICAL, StackView.Distribution.FILL)) val one = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(50.0, 50.0) }) val two = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(75.0, 75.0) }) val three = stack.addArrangedSubview(View().apply { intrinsicContentSize = Size(100.0, 100.0) }) window.solver.dsl { stack.topAnchor equalTo 0 stack.leftAnchor equalTo 0 stack.rightAnchor equalTo 100 } window.layout() assertEquals(0.0, abs(one.leftAnchor.value)) assertEquals(100.0, one.rightAnchor.value) assertEquals(0.0, abs(two.leftAnchor.value)) assertEquals(100.0, two.rightAnchor.value) assertEquals(0.0, abs(three.leftAnchor.value)) assertEquals(100.0, three.rightAnchor.value) } }