98 lines
2.2 KiB
Kotlin
98 lines
2.2 KiB
Kotlin
|
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.util.MouseButton
|
||
|
import org.junit.jupiter.api.Assertions.*
|
||
|
import org.junit.jupiter.api.BeforeEach
|
||
|
import org.junit.jupiter.api.Test
|
||
|
import java.util.concurrent.CompletableFuture
|
||
|
|
||
|
/**
|
||
|
* @author shadowfacts
|
||
|
*/
|
||
|
class ViewClickTests {
|
||
|
|
||
|
lateinit var window: Window
|
||
|
|
||
|
@BeforeEach
|
||
|
fun setup() {
|
||
|
window = Window()
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
fun testClickInsideRootView() {
|
||
|
val mouse = CompletableFuture<Point>()
|
||
|
window.addView(object: View() {
|
||
|
init {
|
||
|
frame = Rect(50.0, 50.0, 100.0, 100.0)
|
||
|
}
|
||
|
|
||
|
override fun mouseClicked(point: Point, mouseButton: MouseButton) {
|
||
|
mouse.complete(point)
|
||
|
}
|
||
|
})
|
||
|
window.mouseClicked(Point(75.0, 75.0), MouseButton.LEFT)
|
||
|
|
||
|
assertEquals(Point(25.0, 25.0), mouse.getNow(null))
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
fun testClickOutsideRootView() {
|
||
|
val clicked = CompletableFuture<Boolean>()
|
||
|
window.addView(object: View() {
|
||
|
init {
|
||
|
frame = Rect(50.0, 50.0, 100.0, 100.0)
|
||
|
}
|
||
|
|
||
|
override fun mouseClicked(point: Point, mouseButton: MouseButton) {
|
||
|
clicked.complete(true)
|
||
|
}
|
||
|
})
|
||
|
window.mouseClicked(Point(25.0, 25.0), MouseButton.LEFT)
|
||
|
|
||
|
assertFalse(clicked.getNow(false))
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
fun testClickInsideNestedView() {
|
||
|
val mouse = CompletableFuture<Point>()
|
||
|
val root = window.addView(View().apply {
|
||
|
frame = Rect(50.0, 50.0, 100.0, 100.0)
|
||
|
})
|
||
|
root.addSubview(object: View() {
|
||
|
init {
|
||
|
frame = Rect(25.0, 25.0, 50.0, 50.0)
|
||
|
}
|
||
|
|
||
|
override fun mouseClicked(point: Point, mouseButton: MouseButton) {
|
||
|
mouse.complete(point)
|
||
|
}
|
||
|
})
|
||
|
window.mouseClicked(Point(100.0, 100.0), MouseButton.LEFT)
|
||
|
|
||
|
assertEquals(Point(25.0, 25.0), mouse.getNow(null))
|
||
|
}
|
||
|
|
||
|
@Test
|
||
|
fun testClickOutsideNestedView() {
|
||
|
val clicked = CompletableFuture<Boolean>()
|
||
|
val root = window.addView(View().apply {
|
||
|
frame = Rect(50.0, 50.0, 100.0, 100.0)
|
||
|
})
|
||
|
root.addSubview(object: View() {
|
||
|
init {
|
||
|
frame = Rect(25.0, 25.0, 50.0, 50.0)
|
||
|
}
|
||
|
|
||
|
override fun mouseClicked(point: Point, mouseButton: MouseButton) {
|
||
|
clicked.complete(true)
|
||
|
}
|
||
|
})
|
||
|
window.mouseClicked(Point(0.0, 0.0), MouseButton.LEFT)
|
||
|
|
||
|
assertFalse(clicked.getNow(false))
|
||
|
}
|
||
|
|
||
|
}
|