257 lines
7.5 KiB
Kotlin
257 lines
7.5 KiB
Kotlin
package net.shadowfacts.cacao.view
|
|
|
|
import net.shadowfacts.kiwidsl.dsl
|
|
import net.shadowfacts.cacao.Window
|
|
import net.shadowfacts.cacao.geometry.Axis
|
|
import net.shadowfacts.cacao.geometry.Size
|
|
import net.shadowfacts.cacao.viewcontroller.ViewController
|
|
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 StackViewLayoutTests {
|
|
|
|
lateinit var viewController: ViewController
|
|
lateinit var window: Window
|
|
|
|
val view: View
|
|
get() = viewController.view
|
|
|
|
@BeforeEach
|
|
fun setup() {
|
|
viewController = object: ViewController() {
|
|
override fun loadView() {
|
|
view = View()
|
|
}
|
|
}
|
|
window = Window(viewController)
|
|
}
|
|
|
|
@Test
|
|
fun testVerticalLayout() {
|
|
val stack = view.addSubview(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 = view.addSubview(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 = view.addSubview(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 = view.addSubview(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 = view.addSubview(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 = view.addSubview(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)
|
|
}
|
|
|
|
@Test
|
|
fun testVerticalLayoutWithSpacing() {
|
|
val stack = view.addSubview(StackView(Axis.VERTICAL, spacing = 10.0))
|
|
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(60.0, two.topAnchor.value)
|
|
assertEquals(135.0, two.bottomAnchor.value)
|
|
assertEquals(145.0, three.topAnchor.value)
|
|
assertEquals(195.0, three.bottomAnchor.value)
|
|
|
|
assertEquals(195.0, stack.heightAnchor.value)
|
|
}
|
|
|
|
@Test
|
|
fun testHorizontalLayoutWithSpacing() {
|
|
val stack = view.addSubview(StackView(Axis.HORIZONTAL, spacing = 10.0))
|
|
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(60.0, two.leftAnchor.value)
|
|
assertEquals(135.0, two.rightAnchor.value)
|
|
assertEquals(145.0, three.leftAnchor.value)
|
|
assertEquals(195.0, three.rightAnchor.value)
|
|
|
|
assertEquals(195.0, stack.widthAnchor.value)
|
|
}
|
|
} |