2019-06-23 03:03:23 +00:00
|
|
|
package net.shadowfacts.cacao
|
|
|
|
|
|
|
|
import net.shadowfacts.cacao.geometry.Point
|
|
|
|
import net.shadowfacts.cacao.geometry.Rect
|
|
|
|
import net.shadowfacts.cacao.view.View
|
2019-08-08 23:17:14 +00:00
|
|
|
import net.shadowfacts.cacao.viewcontroller.ViewController
|
2019-06-23 03:03:23 +00:00
|
|
|
import org.junit.jupiter.api.Assertions.assertEquals
|
|
|
|
import org.junit.jupiter.api.BeforeEach
|
|
|
|
import org.junit.jupiter.api.Test
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author shadowfacts
|
|
|
|
*/
|
|
|
|
class CoordinateConversionTests {
|
|
|
|
|
2019-08-08 23:17:14 +00:00
|
|
|
lateinit var viewController: ViewController
|
2019-06-23 03:03:23 +00:00
|
|
|
lateinit var window: Window
|
2019-08-08 23:17:14 +00:00
|
|
|
|
|
|
|
val view: View
|
|
|
|
get() = viewController.view
|
2019-06-23 03:03:23 +00:00
|
|
|
|
|
|
|
@BeforeEach
|
|
|
|
fun setup() {
|
2019-08-08 23:17:14 +00:00
|
|
|
viewController = object: ViewController() {
|
|
|
|
override fun loadView() {
|
|
|
|
view = View()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window = Window(viewController)
|
2019-06-23 03:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun testConvertToParent() {
|
2019-08-08 23:17:14 +00:00
|
|
|
val a = view.addSubview(View(Rect(0.0, 0.0, 100.0, 100.0)))
|
2019-06-23 20:53:25 +00:00
|
|
|
val b = a.addSubview(View(Rect(25.0, 25.0, 50.0, 50.0)))
|
2019-06-23 03:03:23 +00:00
|
|
|
|
|
|
|
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() {
|
2019-08-08 23:17:14 +00:00
|
|
|
val root = view.addSubview(View(Rect(0.0, 0.0, 200.0, 200.0)))
|
2019-06-23 20:53:25 +00:00
|
|
|
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)))
|
2019-06-23 03:03:23 +00:00
|
|
|
|
|
|
|
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() {
|
2019-08-08 23:17:14 +00:00
|
|
|
val root = view.addSubview(View(Rect(0.0, 0.0, 200.0, 100.0)))
|
2019-06-23 20:53:25 +00:00
|
|
|
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)))
|
2019-06-23 03:03:23 +00:00
|
|
|
|
|
|
|
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() {
|
2019-08-08 23:17:14 +00:00
|
|
|
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)))
|
2019-06-23 03:03:23 +00:00
|
|
|
|
|
|
|
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() {
|
2019-08-08 23:17:14 +00:00
|
|
|
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)))
|
2019-06-23 20:53:25 +00:00
|
|
|
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)))
|
2019-06-23 03:03:23 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|