// XAAL toolkit // Copyright (C) 2009 Ville Karavirta // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . /** * */ package tests.xaal.objects.graphical; import junit.framework.Assert; import junit.framework.TestCase; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import xaal.objects.Xaal; import xaal.objects.graphical.Coordinate; import xaal.objects.graphical.Offset; import xaal.objects.graphical.Rectangle; import xaal.objects.graphical.Style; import xaal.objects.graphical.Text; /** * @author vkaravir * */ public class OffsetTest extends TestCase { /** * @throws java.lang.Exception */ @BeforeClass public static void setUpBeforeClass() throws Exception { } /** * @throws java.lang.Exception */ @AfterClass public static void tearDownAfterClass() throws Exception { } /** * @throws java.lang.Exception */ @Before public void setUp() throws Exception { } /** * @throws java.lang.Exception */ @After public void tearDown() throws Exception { } @Test public void testOffsetsFromText() { Xaal x = new Xaal(); Text t = new Text(x, "T1"); t.addCoordinate(100, 100); t.setContents("TestText"); t.setStyle(new Style()); Coordinate c1 = new Coordinate(x, 0, 0); Offset o1 = new Offset(); o1.setX(-10); o1.setY(-10); o1.setAnchor(Offset.NW); o1.setBaseObj(t); c1.setOffset(o1); Coordinate c2 = new Coordinate(x, 20,-30); Offset o2 = new Offset(); o2.setX(10); o2.setY(10); o2.setBaseObj(t); o2.setAnchor(Offset.NW); c2.setOffset(o2); Assert.assertEquals(90, c1.getAbsoluteX()); Assert.assertEquals(90, c1.getAbsoluteY()); Assert.assertEquals(130, c2.getAbsoluteX()); Assert.assertEquals(80, c2.getAbsoluteY()); } @Test public void testRectangleOffsets() { Xaal x = new Xaal(); Text t = new Text(x, "T1"); t.addCoordinate(100, 100); t.setContents("TestText"); t.setStyle(new Style()); Rectangle r = new Rectangle(x, "R1"); Coordinate c1 = new Coordinate(x, 0, 0); Offset o1 = new Offset(); o1.setX(-10); o1.setY(-10); o1.setAnchor(Offset.NE); o1.setBaseObj(t); c1.setOffset(o1); r.setStartCoordinate(c1); Coordinate c2 = new Coordinate(x, 20,-30); Offset o2 = new Offset(); o2.setX(10); o2.setY(10); o2.setBaseObj(t); o2.setAnchor(Offset.SE); c2.setOffset(o2); r.setEndCoordinate(c2); java.awt.Rectangle bounds = t.getBounds(); Assert.assertEquals(bounds.x + bounds.width - 10, r.getStartCoordinate().getAbsoluteX()); Assert.assertEquals(bounds.y - 10, r.getStartCoordinate().getAbsoluteY()); Assert.assertEquals(bounds.x + bounds.width + 20 + 10, r.getEndCoordinate().getAbsoluteX()); Assert.assertEquals(bounds.y + bounds.height -30 + 10, r.getEndCoordinate().getAbsoluteY()); } @Test public void testChainedOffsets() { Xaal x = new Xaal(); Text t = new Text(x, "T1"); t.addCoordinate(100, 100); t.setContents("TestText"); t.setStyle(new Style()); Rectangle r = new Rectangle(x, "R1"); Coordinate c1 = new Coordinate(x, 0, 0); Offset o1 = new Offset(); o1.setX(-10); o1.setY(-10); o1.setAnchor(Offset.NW); o1.setBaseObj(t); c1.setOffset(o1); r.setStartCoordinate(c1); r.setEndCoordinate(new Coordinate(x, 200, 200)); Coordinate c2 = new Coordinate(x, 20,-30); Offset o2 = new Offset(); o2.setX(10); o2.setY(10); o2.setBaseObj(r); o2.setAnchor(Offset.NW); c2.setOffset(o2); Assert.assertEquals(120, c2.getAbsoluteX()); Assert.assertEquals(70, c2.getAbsoluteY()); Coordinate c3 = new Coordinate(x, -20, 20); Offset o3 = new Offset(); o3.setX(15); o3.setBaseObj(r); o3.setAnchor(Offset.S); c3.setOffset(o3); Assert.assertEquals(90 + 55 -20 + 15, c3.getAbsoluteX()); Assert.assertEquals(200 + 20, c3.getAbsoluteY()); } @Test public void testMy() { Xaal x = new Xaal(); Rectangle r = new Rectangle(x, "R1"); r.setStartCoordinate(new Coordinate(x, 100, 100)); r.setEndCoordinate(new Coordinate(x, 200, 200)); System.out.println(r.getBounds()); java.awt.Rectangle bounds = new java.awt.Rectangle(r.getBounds()); System.out.println(bounds); bounds.add(r.getBounds()); System.out.println(bounds); } }