Fix unit tests

This commit is contained in:
Shadowfacts 2019-08-08 19:17:14 -04:00
parent ca3f2328f0
commit 7cbcb267dd
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
8 changed files with 122 additions and 39 deletions

View File

@ -3,6 +3,7 @@ package net.shadowfacts.cacao
import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.view.View
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
@ -12,16 +13,25 @@ import org.junit.jupiter.api.Test
*/
class CoordinateConversionTests {
lateinit var viewController: ViewController
lateinit var window: Window
val view: View
get() = viewController.view
@BeforeEach
fun setup() {
window = Window()
viewController = object: ViewController() {
override fun loadView() {
view = View()
}
}
window = Window(viewController)
}
@Test
fun testConvertToParent() {
val a = window.addView(View(Rect(0.0, 0.0, 100.0, 100.0)))
val a = view.addSubview(View(Rect(0.0, 0.0, 100.0, 100.0)))
val b = a.addSubview(View(Rect(25.0, 25.0, 50.0, 50.0)))
assertEquals(Point(25.0, 25.0), b.convert(Point(0.0, 0.0), to = a))
@ -31,7 +41,7 @@ class CoordinateConversionTests {
@Test
fun testConvertToSibling() {
val root = window.addView(View(Rect(0.0, 0.0, 200.0, 200.0)))
val root = view.addSubview(View(Rect(0.0, 0.0, 200.0, 200.0)))
val a = root.addSubview(View(Rect(25.0, 25.0, 50.0, 50.0)))
val b = root.addSubview(View(Rect(75.0, 75.0, 50.0, 50.0)))
@ -42,7 +52,7 @@ class CoordinateConversionTests {
@Test
fun testConvertBetweenSubtrees() {
val root = window.addView(View(Rect(0.0, 0.0, 200.0, 100.0)))
val root = view.addSubview(View(Rect(0.0, 0.0, 200.0, 100.0)))
val a = root.addSubview(View(Rect(0.0, 0.0, 100.0, 100.0)))
val b = root.addSubview(View(Rect(100.0, 0.0, 100.0, 100.0)))
val c = a.addSubview(View(Rect(0.0, 0.0, 50.0, 50.0)))
@ -55,8 +65,8 @@ class CoordinateConversionTests {
@Test
fun testConvertBetweenTopLevelViews() {
val a = window.addView(View(Rect(0.0, 0.0, 100.0, 100.0)))
val b = window.addView(View(Rect(100.0, 100.0, 100.0, 100.0)))
val a = view.addSubview(View(Rect(0.0, 0.0, 100.0, 100.0)))
val b = view.addSubview(View(Rect(100.0, 100.0, 100.0, 100.0)))
assertEquals(Point(0.0, 0.0), a.convert(Point(100.0, 100.0), to = b))
assertEquals(Point(200.0, 200.0), b.convert(Point(100.0, 100.0), to = a))
@ -65,8 +75,8 @@ class CoordinateConversionTests {
@Test
fun testConvertBetweenTopLevelSubtrees() {
val a = window.addView(View(Rect(0.0, 0.0, 100.0, 100.0)))
val b = window.addView(View(Rect(100.0, 100.0, 100.0, 100.0)))
val a = view.addSubview(View(Rect(0.0, 0.0, 100.0, 100.0)))
val b = view.addSubview(View(Rect(100.0, 100.0, 100.0, 100.0)))
val c = a.addSubview(View(Rect(25.0, 25.0, 50.0, 50.0)))
val d = b.addSubview(View(Rect(25.0, 25.0, 50.0, 50.0)))

View File

@ -3,6 +3,7 @@ package net.shadowfacts.cacao
import net.shadowfacts.kiwidsl.dsl
import net.shadowfacts.cacao.geometry.Size
import net.shadowfacts.cacao.view.View
import net.shadowfacts.cacao.viewcontroller.ViewController
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
@ -12,16 +13,25 @@ import org.junit.jupiter.api.Test
*/
class WindowLayoutTests {
lateinit var viewController: ViewController
lateinit var window: Window
val view: View
get() = viewController.view
@BeforeEach
fun setup() {
window = Window()
viewController = object: ViewController() {
override fun loadView() {
view = View()
}
}
window = Window(viewController)
}
@Test
fun testConstraintToConstant() {
val view = window.addView(View())
val view = view.addSubview(View())
window.solver.dsl {
view.leftAnchor equalTo 100
view.rightAnchor equalTo 200
@ -37,8 +47,8 @@ class WindowLayoutTests {
@Test
fun testConstraintToView() {
val one = window.addView(View())
val two = window.addView(View())
val one = view.addSubview(View())
val two = view.addSubview(View())
window.solver.dsl {
one.leftAnchor equalTo 0
one.widthAnchor equalTo 100
@ -63,7 +73,7 @@ class WindowLayoutTests {
@Test
fun testIntrinsicContentSize() {
val view = window.addView(View()).apply {
val view = view.addSubview(View()).apply {
intrinsicContentSize = Size(100.0, 200.0)
}
window.solver.dsl {

View File

@ -4,6 +4,7 @@ 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
@ -14,16 +15,25 @@ import kotlin.math.abs
*/
class StackViewLayoutTests {
lateinit var viewController: ViewController
lateinit var window: Window
val view: View
get() = viewController.view
@BeforeEach
fun setup() {
window = Window()
viewController = object: ViewController() {
override fun loadView() {
view = View()
}
}
window = Window(viewController)
}
@Test
fun testVerticalLayout() {
val stack = window.addView(StackView(Axis.VERTICAL))
val stack = view.addSubview(StackView(Axis.VERTICAL))
val one = stack.addArrangedSubview(View().apply {
intrinsicContentSize = Size(50.0, 50.0)
})
@ -50,7 +60,7 @@ class StackViewLayoutTests {
@Test
fun testHorizontalLayout() {
val stack = window.addView(StackView(Axis.HORIZONTAL))
val stack = view.addSubview(StackView(Axis.HORIZONTAL))
val one = stack.addArrangedSubview(View().apply {
intrinsicContentSize = Size(50.0, 50.0)
})
@ -77,7 +87,7 @@ class StackViewLayoutTests {
@Test
fun testVerticalLayoutWithLeading() {
val stack = window.addView(StackView(Axis.VERTICAL, StackView.Distribution.LEADING))
val stack = view.addSubview(StackView(Axis.VERTICAL, StackView.Distribution.LEADING))
val one = stack.addArrangedSubview(View().apply {
intrinsicContentSize = Size(50.0, 50.0)
})
@ -106,7 +116,7 @@ class StackViewLayoutTests {
@Test
fun testVerticalLayoutWithTrailing() {
val stack = window.addView(StackView(Axis.VERTICAL, StackView.Distribution.TRAILING))
val stack = view.addSubview(StackView(Axis.VERTICAL, StackView.Distribution.TRAILING))
val one = stack.addArrangedSubview(View().apply {
intrinsicContentSize = Size(50.0, 50.0)
})
@ -135,7 +145,7 @@ class StackViewLayoutTests {
@Test
fun testVerticalLayoutWithCenter() {
val stack = window.addView(StackView(Axis.VERTICAL, StackView.Distribution.CENTER))
val stack = view.addSubview(StackView(Axis.VERTICAL, StackView.Distribution.CENTER))
val one = stack.addArrangedSubview(View().apply {
intrinsicContentSize = Size(50.0, 50.0)
})
@ -164,7 +174,7 @@ class StackViewLayoutTests {
@Test
fun testVerticalLayoutWithFill() {
val stack = window.addView(StackView(Axis.VERTICAL, StackView.Distribution.FILL))
val stack = view.addSubview(StackView(Axis.VERTICAL, StackView.Distribution.FILL))
val one = stack.addArrangedSubview(View().apply {
intrinsicContentSize = Size(50.0, 50.0)
})
@ -193,7 +203,7 @@ class StackViewLayoutTests {
@Test
fun testVerticalLayoutWithSpacing() {
val stack = window.addView(StackView(Axis.VERTICAL, spacing = 10.0))
val stack = view.addSubview(StackView(Axis.VERTICAL, spacing = 10.0))
val one = stack.addArrangedSubview(View().apply {
intrinsicContentSize = Size(50.0, 50.0)
})
@ -220,7 +230,7 @@ class StackViewLayoutTests {
@Test
fun testHorizontalLayoutWithSpacing() {
val stack = window.addView(StackView(Axis.HORIZONTAL, spacing = 10.0))
val stack = view.addSubview(StackView(Axis.HORIZONTAL, spacing = 10.0))
val one = stack.addArrangedSubview(View().apply {
intrinsicContentSize = Size(50.0, 50.0)
})

View File

@ -5,6 +5,7 @@ import net.shadowfacts.cacao.Window
import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.util.MouseButton
import net.shadowfacts.cacao.viewcontroller.ViewController
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
@ -17,17 +18,26 @@ class ViewClickTests {
lateinit var screen: CacaoScreen
lateinit var window: Window
lateinit var viewController: ViewController
val view: View
get() = viewController.view
@BeforeEach
fun setup() {
screen = CacaoScreen()
window = screen.addWindow(Window())
viewController = object: ViewController() {
override fun loadView() {
view = View(Rect(0.0, 0.0, 1000.0, 1000.0))
}
}
window = screen.addWindow(Window(viewController))
}
@Test
fun testClickInsideRootView() {
val mouse = CompletableFuture<Point>()
window.addView(object: View(Rect(50.0, 50.0, 100.0, 100.0)) {
view.addSubview(object: View(Rect(50.0, 50.0, 100.0, 100.0)) {
override fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
mouse.complete(point)
return true
@ -41,7 +51,7 @@ class ViewClickTests {
@Test
fun testClickOutsideRootView() {
val clicked = CompletableFuture<Boolean>()
window.addView(object: View(Rect(50.0, 50.0, 100.0, 100.0)) {
view.addSubview(object: View(Rect(50.0, 50.0, 100.0, 100.0)) {
override fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
clicked.complete(true)
return true
@ -55,7 +65,7 @@ class ViewClickTests {
@Test
fun testClickInsideNestedView() {
val mouse = CompletableFuture<Point>()
val root = window.addView(View(Rect(50.0, 50.0, 100.0, 100.0)))
val root = view.addSubview(View(Rect(50.0, 50.0, 100.0, 100.0)))
root.addSubview(object: View(Rect(25.0, 25.0, 50.0, 50.0)) {
override fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
mouse.complete(point)
@ -70,7 +80,7 @@ class ViewClickTests {
@Test
fun testClickOutsideNestedView() {
val clicked = CompletableFuture<Boolean>()
val root = window.addView(View(Rect(50.0, 50.0, 100.0, 100.0)))
val root = view.addSubview(View(Rect(50.0, 50.0, 100.0, 100.0)))
root.addSubview(object: View(Rect(25.0, 25.0, 50.0, 50.0)) {
override fun mouseClicked(point: Point, mouseButton: MouseButton): Boolean {
clicked.complete(true)

View File

@ -3,6 +3,7 @@ package net.shadowfacts.cacao.view
import net.shadowfacts.cacao.Window
import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.viewcontroller.ViewController
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
@ -14,8 +15,12 @@ import java.util.concurrent.CompletableFuture
*/
class ViewHoverTests {
lateinit var viewController: ViewController
lateinit var window: Window
val view: View
get() = viewController.view
companion object {
@BeforeAll
@JvmStatic
@ -26,13 +31,18 @@ class ViewHoverTests {
@BeforeEach
fun setup() {
window = Window()
viewController = object: ViewController() {
override fun loadView() {
view = View()
}
}
window = Window(viewController)
}
@Test
fun testHoverRootView() {
val point = CompletableFuture<Point>()
window.addView(object: View(Rect(50.0, 50.0, 100.0, 100.0)) {
view.addSubview(object: View(Rect(50.0, 50.0, 100.0, 100.0)) {
override fun drawContent(mouse: Point, delta: Float) {
point.complete(mouse)
}
@ -45,7 +55,7 @@ class ViewHoverTests {
@Test
fun testHoverNestedView() {
val point = CompletableFuture<Point>()
val root = window.addView(View(Rect(50.0, 50.0, 100.0, 100.0)))
val root = view.addSubview(View(Rect(50.0, 50.0, 100.0, 100.0)))
root.addSubview(object: View(Rect(25.0, 25.0, 50.0, 50.0)) {
override fun drawContent(mouse: Point, delta: Float) {
point.complete(mouse)

View File

@ -3,9 +3,11 @@ package net.shadowfacts.cacao.view.button
import net.shadowfacts.cacao.CacaoScreen
import net.shadowfacts.cacao.Window
import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.geometry.Size
import net.shadowfacts.cacao.util.MouseButton
import net.shadowfacts.cacao.view.View
import net.shadowfacts.cacao.viewcontroller.ViewController
import net.shadowfacts.kiwidsl.dsl
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
@ -20,11 +22,20 @@ class ButtonClickTests {
lateinit var screen: CacaoScreen
lateinit var window: Window
lateinit var viewController: ViewController
val view: View
get() = viewController.view
@BeforeEach
fun setup() {
screen = CacaoScreen()
window = screen.addWindow(Window())
viewController = object: ViewController() {
override fun loadView() {
view = View(Rect(0.0, 0.0, 1000.0, 1000.0))
}
}
window = screen.addWindow(Window(viewController))
}
@Test
@ -33,7 +44,7 @@ class ButtonClickTests {
val content = View().apply {
intrinsicContentSize = Size(25.0, 25.0)
}
val button = window.addView(Button(content).apply {
val button = view.addSubview(Button(content).apply {
handler = {
clicked.complete(true)
}
@ -54,7 +65,7 @@ class ButtonClickTests {
val content = View().apply {
intrinsicContentSize = Size(25.0, 25.0)
}
val button = window.addView(Button(content).apply {
val button = view.addSubview(Button(content).apply {
handler = {
clicked.complete(true)
}

View File

@ -5,6 +5,8 @@ import net.shadowfacts.cacao.Window
import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.util.MouseButton
import net.shadowfacts.cacao.view.View
import net.shadowfacts.cacao.viewcontroller.ViewController
import net.shadowfacts.kiwidsl.dsl
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
@ -32,13 +34,19 @@ class EnumButtonTests {
lateinit var screen: CacaoScreen
lateinit var window: Window
lateinit var viewController: ViewController
lateinit var button: EnumButton<MyEnum>
@BeforeEach
fun setup() {
screen = CacaoScreen()
window = screen.addWindow(Window())
button = window.addView(EnumButton(MyEnum.ONE, MyEnum::name))
viewController = object: ViewController() {
override fun loadView() {
view = View(Rect(0.0, 0.0, 1000.0, 1000.0))
}
}
window = screen.addWindow(Window(viewController))
button = viewController.view.addSubview(EnumButton(MyEnum.ONE, MyEnum::name))
window.solver.dsl {
button.leftAnchor equalTo 0
button.topAnchor equalTo 0

View File

@ -1,9 +1,12 @@
package net.shadowfacts.cacao.view.button
import net.shadowfacts.cacao.CacaoScreen
import net.shadowfacts.cacao.Window
import net.shadowfacts.cacao.geometry.Point
import net.shadowfacts.cacao.geometry.Rect
import net.shadowfacts.cacao.util.MouseButton
import net.shadowfacts.cacao.view.View
import net.shadowfacts.cacao.viewcontroller.ViewController
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
@ -23,17 +26,28 @@ class ToggleButtonTests {
}
}
lateinit var screen: CacaoScreen
lateinit var window: Window
lateinit var viewController: ViewController
val view: View
get() = viewController.view
@BeforeEach
fun setup() {
window = Window()
screen = CacaoScreen()
viewController = object: ViewController() {
override fun loadView() {
view = View(Rect(0.0, 0.0, 1000.0, 1000.0))
}
}
window = screen.addWindow(Window(viewController))
}
@Test
fun testHandlerCalled() {
val called = CompletableFuture<Boolean>()
val button = window.addView(ToggleButton(false).apply {
val button = view.addSubview(ToggleButton(false).apply {
frame = Rect(0.0, 0.0, 25.0, 25.0)
content.frame = bounds
handler = {
@ -47,7 +61,7 @@ class ToggleButtonTests {
@Test
fun testTogglesValues() {
val button = window.addView(ToggleButton(false).apply {
val button = view.addSubview(ToggleButton(false).apply {
frame = Rect(0.0, 0.0, 25.0, 25.0)
content.frame = bounds
})
@ -60,7 +74,7 @@ class ToggleButtonTests {
@Test
fun testMiddleClickDoesNotChangeValue() {
val button = window.addView(ToggleButton(false).apply {
val button = view.addSubview(ToggleButton(false).apply {
frame = Rect(0.0, 0.0, 25.0, 25.0)
content.frame = bounds
})