package net.shadowfacts.cacao import net.shadowfacts.cacao.geometry.Point import net.shadowfacts.cacao.geometry.Rect import net.shadowfacts.cacao.view.View 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 window: Window @BeforeEach fun setup() { window = Window() } @Test fun testConvertToParent() { val a = window.addView(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 = window.addView(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 = window.addView(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 = 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))) 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 = 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 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)) } }