ASMR/src/test/kotlin/net/shadowfacts/cacao/view/StackViewLayoutTests.kt

194 lines
5.5 KiB
Kotlin
Raw Normal View History

2019-06-22 19:02:17 +00:00
package net.shadowfacts.cacao.view
2019-06-22 18:59:37 +00:00
import net.shadowfacts.kiwidsl.dsl
2019-06-22 19:02:17 +00:00
import net.shadowfacts.cacao.Window
import net.shadowfacts.cacao.geometry.Axis
import net.shadowfacts.cacao.geometry.Size
2019-06-22 18:59:37 +00:00
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
*/
2019-06-23 20:32:51 +00:00
class StackViewLayoutTests {
2019-06-22 18:59:37 +00:00
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)
}
}