// 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.parser.modules; import java.io.IOException; import java.io.StringReader; import java.util.Iterator; import org.xml.sax.SAXException; import xaal.objects.Animation; import xaal.objects.Xaal; import xaal.objects.animation.AnimationOperation; import xaal.objects.animation.CreateOperation; import xaal.objects.animation.DeleteOperation; import xaal.objects.animation.InsertOperation; import xaal.objects.animation.SwapOperation; import xaal.objects.animation.graphical.ChangePropertyOperation; import xaal.objects.animation.graphical.MoveOperation; import xaal.objects.animation.timing.Timing; import xaal.objects.graphical.GraphicalPrimitive; import xaal.objects.structures.Array; import xaal.objects.structures.Index; import xaal.objects.structures.Key; import xaal.objects.structures.Marker; import xaal.objects.structures.Structure; import xaal.objects.structures.Tree; import xaal.parser.ParserModule; import xaal.parser.ParserModuleException; import xaal.parser.XmlParser; import xaal.parser.modules.XaalAnimParserModule; import xaal.parser.modules.XaalDSAnimParserModule; import xaal.parser.modules.XaalDSParserModule; import xaal.parser.modules.XaalGPAnimParserModule; import xaal.parser.modules.XaalGPParserModule; import xaal.parser.modules.XaalMainParserModule; import junit.framework.TestCase; public class XaalDSAnimParserModuleTest extends TestCase { private XmlParser xp; public XaalDSAnimParserModuleTest(String name) { super(name); } protected void setUp() throws Exception { super.setUp(); } protected void tearDown() throws Exception { super.tearDown(); } private void initializeXP() throws ParserModuleException { xp = new XmlParser(); xp.registerParserModule(XaalDSAnimParserModule.class.getName()); xp.registerParserModule(XaalDSParserModule.class.getName()); xp.registerParserModule(XaalGPParserModule.class.getName()); xp.registerParserModule(XaalGPAnimParserModule.class.getName()); xp.registerParserModule(XaalMainParserModule.class.getName()); xp.registerParserModule(XaalAnimParserModule.class.getName()); } private Animation commonTests() { Object o = ParserModule.getProperty(XaalMainParserModule.XAAL_ROOT_OBJECT); assertEquals("XAAL_ROOT_OBJECT is null", true, o != null); assertEquals("XAAL_ROOT_OBJECT not instance of " + Xaal.class.getName(), true, o instanceof Xaal); Xaal x = (Xaal) o; assertEquals("Xaal.getAnimation() returned null", true, x.getAnimation() != null); return x.getAnimation(); } public void testElementCreate() throws IOException, SAXException, ParserModuleException { initializeXP(); xp.parse(new StringReader("" + "narration" + "" + ""+ ""+ "C")); Animation anim = commonTests(); Iterator i = anim.getOperations(); assertEquals("Animation operation create not created", true, i.hasNext()); Object o = i.next(); assertEquals("Animation operation not instance of " + CreateOperation.class.getName(), true, o instanceof CreateOperation); CreateOperation co = (CreateOperation) o; assertEquals("Id of the create operation not set correctly", "cre", co.getId()); assertEquals("Created additional operations", false, i.hasNext()); assertEquals("Narrative not set", "narration", co.getNarrative()); Timing t = co.getTiming(); assertEquals("Timing not set", false, t == null); assertEquals("Delay not correct", 13, t.getDelay()); assertEquals("Delay type not correct", Timing.FRAMES_TYPE, t.getDelayType()); assertEquals("Duration not correct", 20, t.getDuration()); assertEquals("Duration type not correct", Timing.MS_TYPE, t.getDurationType()); i = co.getStructures(); assertEquals("Should have created a structure", true, i.hasNext()); o = i.next(); assertEquals("The structure should be an instance of " + Array.class.getName(), true, (o instanceof Array)); Array a = (Array) o; assertEquals(false, a.isIndexed()); assertEquals("Size should be 7 while it is " + a.getSize(), 7, a.getSize()); assertEquals("Array orientation not set correctly", "vertical", a.getOrientation()); assertEquals("Id not initialized properly", "array", a.getId()); Index i0 = a.getIndex(0); assertEquals("Index 0 not intialized properly", true, i0 != null); Index i2 = a.getIndex(2); assertEquals("Index 2 should not be intialized", false, i2 != null); o = i0.getContent(); assertEquals("Contents of i0 not an instance of " + Key.class.getName(), true, o instanceof Key); Key k = (Key) o; assertEquals("Contents of i0 not initialized properly", true, k != null); assertEquals("Label of i0 not set correctly", "test", i0.getLabel()); assertEquals("Id of i0 not set correctly", "i0", i0.getId()); assertEquals("Index of i0 not set correctly", 0, i0.getIndex()); assertEquals("Value of key of index 0 not set correctly", "A", k.getValue()); assertEquals("Id of key of index 0 not set correctly", "keyA", k.getId()); Index i4 = a.getIndex(4); k = (Key)i4.getContent(); assertEquals("Value of key of index 4 not set correctly", "C", k.getValue()); assertEquals("Should have created only one structure", false, i.hasNext()); } public void testElementDelete() throws IOException, SAXException, ParserModuleException { initializeXP(); xp.parse(new StringReader("" + "" + "narration" + "" + "")); Animation anim = commonTests(); Iterator i = anim.getOperations(); assertEquals("Animation operation delete not created", true, i.hasNext()); Object o = i.next(); assertEquals("Animation operation not instance of " + DeleteOperation.class.getName(), true, o instanceof DeleteOperation); DeleteOperation doper = (DeleteOperation) o; assertEquals("Id of the delete operation not set correctly", "del", doper.getId()); assertEquals("Created additional operations", false, i.hasNext()); assertEquals("Narrative not set", "narration", doper.getNarrative()); Timing t = doper.getTiming(); assertEquals("Timing not set", false, t == null); assertEquals("Delay not correct", 13, t.getDelay()); assertEquals("Delay type not correct", Timing.FRAMES_TYPE, t.getDelayType()); assertEquals("Duration not correct", 20, t.getDuration()); assertEquals("Duration type not correct", Timing.MS_TYPE, t.getDurationType()); Xaal x = (Xaal)ParserModule.getProperty(XaalMainParserModule.XAAL_ROOT_OBJECT); Tree init = (Tree) x.getInitial().getStructures().next(); assertEquals("Target of delete not correctly set.", init, doper.getTarget()); assertEquals("Key to be deleted not correctly set", "A", doper.getKey()); } public void testElementInsert() throws IOException, SAXException, ParserModuleException { initializeXP(); xp.parse(new StringReader("" + "" + "" + "narration" + "" + "" + "")); Animation anim = commonTests(); Iterator i = anim.getOperations(); assertEquals("Animation operation insert not created", true, i.hasNext()); Object o = i.next(); assertEquals("Animation operation not instance of " + InsertOperation.class.getName(), true, o instanceof InsertOperation); InsertOperation ioper = (InsertOperation) o; assertEquals("Id of the insert operation not set correctly", "ins", ioper.getId()); assertEquals("Created additional operations", false, i.hasNext()); assertEquals("Narrative not set", "narration", ioper.getNarrative()); Timing t = ioper.getTiming(); assertEquals("Timing not set", false, t == null); assertEquals("Delay not correct", 13, t.getDelay()); assertEquals("Delay type not correct", Timing.S_TYPE, t.getDelayType()); assertEquals("Duration not correct", 20, t.getDuration()); assertEquals("Duration type not correct", Timing.FRAMES_TYPE, t.getDurationType()); Xaal x = (Xaal)ParserModule.getProperty(XaalMainParserModule.XAAL_ROOT_OBJECT); Tree init = (Tree) x.getInitial().getStructures().next(); assertEquals("Target of insert not correctly set.", init, ioper.getTarget()); i = ioper.getStructures(); Key k = (Key) i.next(); assertEquals("Source of insert not correctly set", "C", k.getValue()); i = ioper.getGraphicals(); assertEquals(true, i.hasNext()); MoveOperation mo = (MoveOperation) i.next(); assertEquals(false, i.hasNext()); assertEquals("c0", ((GraphicalPrimitive) mo.getGraphicals().next()).getId()); } public void testElementSwap() throws ParserModuleException, IOException, SAXException { initializeXP(); xp.parse(new StringReader(""+ ""+ "C" + "" + "narration" + "" + "")); Animation anim = commonTests(); Iterator i = anim.getOperations(); assertEquals("Animation operation swap not created", true, i.hasNext()); Object o = i.next(); assertEquals("Animation operation not instance of " + SwapOperation.class.getName(), true, o instanceof SwapOperation); SwapOperation so = (SwapOperation) o; assertEquals("Id of the swap operation not set correctly", "swapOper", so.getId()); assertEquals("Created additional operations", false, i.hasNext()); assertEquals("Narrative not set", "narration", so.getNarrative()); Timing t = so.getTiming(); assertEquals("Timing not set", false, t == null); assertEquals("Delay not correct", 13, t.getDelay()); assertEquals("Delay type not correct", Timing.MS_TYPE, t.getDelayType()); assertEquals("Duration not correct", 20, t.getDuration()); assertEquals("Duration type not correct", Timing.S_TYPE, t.getDurationType()); Structure swapStr = so.getSwap(); assertNotNull(swapStr); assertEquals("keyC", swapStr.getId()); Structure withStr = so.getWith(); assertNotNull(withStr); assertEquals("keyA", withStr.getId()); } public void testElementChangeProperty() throws ParserModuleException, IOException, SAXException { initializeXP(); xp.parse(new StringReader("" + "" + "" + "" + "")); Animation anim = commonTests(); Iterator i = anim.getOperations(); assertTrue("Expected an animation operation", i.hasNext()); ChangePropertyOperation cpo = (ChangePropertyOperation) i.next(); assertNotNull(cpo); Marker m = (Marker) cpo.getStructures().next(); assertNotNull(m); } }