ASMR/src/test/kotlin/net/shadowfacts/cacao/CoordinateConversionTests.kt

88 lines
3.1 KiB
Kotlin

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
/**
* @author shadowfacts
*/
class CoordinateConversionTests {
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 testConvertToParent() {
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))
assertEquals(Point(75.0, 75.0), b.convert(Point(50.0, 50.0), to = a))
assertEquals(Rect(25.0, 25.0, 50.0, 50.0), b.convert(Rect(0.0, 0.0, 50.0, 50.0), to = a))
}
@Test
fun testConvertToSibling() {
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)))
assertEquals(Point(-50.0, -50.0), a.convert(Point(0.0, 0.0), to = b))
assertEquals(Point(100.0, 100.0), b.convert(Point(50.0, 50.0), to = a))
assertEquals(Rect(50.0, 50.0, 50.0, 50.0), b.convert(Rect(0.0, 0.0, 50.0, 50.0), to = a))
}
@Test
fun testConvertBetweenSubtrees() {
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)))
val d = b.addSubview(View(Rect(0.0, 0.0, 50.0, 50.0)))
assertEquals(Point(-100.0, 0.0), c.convert(Point(0.0, 0.0), to = b))
assertEquals(Point(-50.0, 50.0), c.convert(Point(50.0, 50.0), to = d))
assertEquals(Rect(100.0, 0.0, 50.0, 50.0), d.convert(Rect(0.0, 0.0, 50.0, 50.0), to = c))
}
@Test
fun testConvertBetweenTopLevelViews() {
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))
assertEquals(Rect(100.0, 100.0, 100.0, 100.0), b.convert(Rect(0.0, 0.0, 100.0, 100.0), to = a))
}
@Test
fun testConvertBetweenTopLevelSubtrees() {
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)))
assertEquals(Point(-50.0, -50.0), c.convert(Point(50.0, 50.0), to = d))
assertEquals(Point(100.0, 100.0), d.convert(Point(0.0, 0.0), to = c))
assertEquals(Rect(100.0, 100.0, 50.0, 50.0), d.convert(Rect(0.0, 0.0, 50.0, 50.0), to = c))
}
}