public void testBroadcast() { Sprite sprite = new SingleSprite("testSprite"); Script script = new StartScript(); String message = "simpleTest"; BroadcastBrick broadcastBrick = new BroadcastBrick(message); script.addBrick(broadcastBrick); sprite.addScript(script); BroadcastScript broadcastScript = new BroadcastScript(message); int testPosition = 100; SetXBrick testBrick = new SetXBrick(testPosition); broadcastScript.addBrick(testBrick); sprite.addScript(broadcastScript); Project project = new Project(getContext(), UiTestUtils.DEFAULT_TEST_PROJECT_NAME); Scene scene = project.getDefaultScene(); scene.addSprite(sprite); ProjectManager.getInstance().setProject(project); sprite.createStartScriptActionSequenceAndPutToMap(new HashMap<String, List<String>>()); while (!allActionsOfAllSpritesAreFinished()) { for (Sprite spriteOfList : ProjectManager.getInstance().getCurrentScene().getSpriteList()) { spriteOfList.look.act(1.0f); } } assertEquals( "Simple broadcast failed", testPosition, (int) sprite.look.getXInUserInterfaceDimensionUnit()); }
@Override protected void setUp() { Project project = new Project(null, "testProject"); firstSprite = new SingleSprite("firstSprite"); StartScript startScript = new StartScript(); ChangeSizeByNBrick changeBrick = new ChangeSizeByNBrick(10); firstSprite.addScript(startScript); startScript.addBrick(changeBrick); project.getDefaultScene().addSprite(firstSprite); ProjectManager.getInstance().setProject(project); ProjectManager.getInstance().setCurrentSprite(firstSprite); dataContainer = ProjectManager.getInstance().getCurrentScene().getDataContainer(); dataContainer.addProjectUserList(PROJECT_USER_LIST_NAME); dataContainer.addSpriteUserListToSprite(firstSprite, SPRITE_USER_LIST_NAME); dataContainer.addProjectUserList(PROJECT_USER_LIST_NAME_2); }
public void testRestartingOfWhenScriptWithBroadcastWaitBrick() { String messageOne = "messageOne"; String messageTwo = "messageTwo"; final int xMovement = 1; Sprite sprite = new SingleSprite("cat"); Script startScript = new StartScript(); BroadcastBrick startBroadcastBrick = new BroadcastBrick(messageOne); startScript.addBrick(startBroadcastBrick); sprite.addScript(startScript); BroadcastScript broadcastScriptMessageOne = new BroadcastScript(messageOne); ChangeXByNBrick changeXByNBrickOne = new ChangeXByNBrick(xMovement); BroadcastWaitBrick broadcastWaitBrickOne = new BroadcastWaitBrick(messageTwo); broadcastScriptMessageOne.addBrick(changeXByNBrickOne); broadcastScriptMessageOne.addBrick(broadcastWaitBrickOne); sprite.addScript(broadcastScriptMessageOne); BroadcastScript broadcastScriptMessageTwo = new BroadcastScript(messageTwo); ChangeXByNBrick changeXByNBrickTwo = new ChangeXByNBrick(xMovement); BroadcastWaitBrick broadcastWaitBrickTwo = new BroadcastWaitBrick(messageOne); broadcastScriptMessageTwo.addBrick(changeXByNBrickTwo); broadcastScriptMessageTwo.addBrick(broadcastWaitBrickTwo); sprite.addScript(broadcastScriptMessageTwo); Project project = new Project(getContext(), UiTestUtils.DEFAULT_TEST_PROJECT_NAME); project.getDefaultScene().addSprite(sprite); ProjectManager.getInstance().setProject(project); sprite.createStartScriptActionSequenceAndPutToMap(new HashMap<String, List<String>>()); int loopCounter = 0; while (!allActionsOfAllSpritesAreFinished() && loopCounter++ < 20) { for (Sprite spriteOfList : ProjectManager.getInstance().getCurrentScene().getSpriteList()) { spriteOfList.look.act(1.0f); } } assertTrue( "When script does not restart itself when a BroadcastWait is sent! ", (int) sprite.look.getXInUserInterfaceDimensionUnit() > 5 * xMovement); }
public void testWhenScriptRestartingItself() { Sprite sprite = new SingleSprite("testSprite"); Script script = new StartScript(); String message = "simpleTest"; BroadcastBrick broadcastBrick = new BroadcastBrick(message); script.addBrick(broadcastBrick); sprite.addScript(script); BroadcastScript broadcastScript = new BroadcastScript(message); final int xMovement = 1; ChangeXByNBrick changeXByNBrick = new ChangeXByNBrick(xMovement); broadcastScript.addBrick(changeXByNBrick); BroadcastBrick broadcastBrickLoop = new BroadcastBrick(message); broadcastScript.addBrick(broadcastBrickLoop); WaitBrick wb = new WaitBrick(5); broadcastScript.addBrick(wb); sprite.addScript(broadcastScript); Project project = new Project(getContext(), UiTestUtils.DEFAULT_TEST_PROJECT_NAME); project.getDefaultScene().addSprite(sprite); ProjectManager.getInstance().setProject(project); sprite.createStartScriptActionSequenceAndPutToMap(new HashMap<String, List<String>>()); int loopCounter = 0; while (!allActionsOfAllSpritesAreFinished() && loopCounter++ < 20) { for (Sprite spriteOfList : ProjectManager.getInstance().getCurrentScene().getSpriteList()) { spriteOfList.look.act(1.0f); } } assertTrue( "When script does not restart itself!", (int) sprite.look.getXInUserInterfaceDimensionUnit() > xMovement); }
private Project generateMultiplePermissionsProject() { final Project project = new Project(getInstrumentation().getTargetContext(), projectName); Sprite firstSprite = new SingleSprite("first"); Sprite secondSprite = new SingleSprite("second"); Script testScript = new StartScript(); Script otherScript = new StartScript(); HideBrick hideBrick = new HideBrick(); ShowBrick showBrick = new ShowBrick(); SpeakBrick speakBrick = new SpeakBrick(""); LegoNxtMotorMoveBrick motorBrick = new LegoNxtMotorMoveBrick(LegoNxtMotorMoveBrick.Motor.MOTOR_A, SET_SPEED_INITIALLY); SetSizeToBrick setSizeToBrick = new SetSizeToBrick( new Formula( new FormulaElement( FormulaElement.ElementType.SENSOR, Sensors.FACE_SIZE.name(), null))); BrickBaseType moveBrick = DroneBrickFactory.getInstanceOfDroneBrick( DroneBrickFactory.DroneBricks.DRONE_MOVE_FORWARD_BRICK, DEFAULT_MOVE_TIME_IN_MILLISECONDS, DEFAULT_MOVE_POWER_IN_PERCENT); testScript.addBrick(hideBrick); testScript.addBrick(showBrick); testScript.addBrick(speakBrick); testScript.addBrick(motorBrick); otherScript.addBrick(setSizeToBrick); otherScript.addBrick(moveBrick); firstSprite.addScript(testScript); secondSprite.addScript(otherScript); project.getDefaultScene().addSprite(firstSprite); project.getDefaultScene().addSprite(secondSprite); return project; }
public void testSanityCheck() throws IOException { final int xPosition = 457; final int yPosition = 598; final float size = 0.8f; final Project project = new Project(getInstrumentation().getTargetContext(), projectName); Sprite firstSprite = new SingleSprite("first"); Sprite secondSprite = new SingleSprite("second"); Sprite thirdSprite = new SingleSprite("third"); Sprite fourthSprite = new SingleSprite("fourth"); Script testScript = new StartScript(); Script otherScript = new StartScript(); HideBrick hideBrick = new HideBrick(); ShowBrick showBrick = new ShowBrick(); SetSizeToBrick setSizeToBrick = new SetSizeToBrick(size); ComeToFrontBrick comeToFrontBrick = new ComeToFrontBrick(); PlaceAtBrick placeAtBrick = new PlaceAtBrick(xPosition, yPosition); testScript.addBrick(hideBrick); testScript.addBrick(showBrick); testScript.addBrick(setSizeToBrick); testScript.addBrick(comeToFrontBrick); otherScript.addBrick(placeAtBrick); otherScript.setPaused(true); firstSprite.addScript(testScript); secondSprite.addScript(otherScript); project.getDefaultScene().addSprite(firstSprite); project.getDefaultScene().addSprite(secondSprite); project.getDefaultScene().addSprite(thirdSprite); project.getDefaultScene().addSprite(fourthSprite); File tmpCodeFile = new File(buildProjectPath(project.getName()), PROJECTCODE_NAME_TMP); File currentCodeFile = new File(buildProjectPath(project.getName()), PROJECTCODE_NAME); assertFalse(tmpCodeFile.getName() + " exists!", tmpCodeFile.exists()); assertFalse(currentCodeFile.getName() + " exists!", currentCodeFile.exists()); storageHandler.saveProject(project); assertTrue(currentCodeFile.getName() + " was not created!", currentCodeFile.exists()); assertTrue(PROJECTCODE_NAME + " is empty!", currentCodeFile.length() > 0); // simulate 1st Option: tmp_code.xml exists but code.xml doesn't exist --> saveProject process // will restore from tmp_code.xml if (!tmpCodeFile.createNewFile()) { fail("Could not create tmp file"); } UtilFile.copyFile(tmpCodeFile, currentCodeFile); String currentCodeFileXml = Files.toString(currentCodeFile, Charsets.UTF_8); assertTrue("Could not delete " + currentCodeFile.getName(), currentCodeFile.delete()); storageHandler.saveProject(project); assertTrue(currentCodeFile.getName() + " was not created!", currentCodeFile.exists()); assertTrue(PROJECTCODE_NAME + " is empty!", currentCodeFile.length() > 0); assertTrue( "Sanity Check Failed. New Code File is not equal with tmp file.", currentCodeFileXml.equals(Files.toString(currentCodeFile, Charsets.UTF_8))); // simulate 2nd Option: tmp_code.xml and code.xml exist --> saveProject process will discard // tmp_code.xml and use code.xml if (!tmpCodeFile.createNewFile()) { fail("Could not create tmp file"); } storageHandler.saveProject(project); assertFalse("Sanity Check Failed. tmp file was not discarded.", tmpCodeFile.exists()); }
public void testSerializeProject() { final int xPosition = 457; final int yPosition = 598; final float size = 0.8f; Project project = new Project(getInstrumentation().getTargetContext(), projectName); Sprite firstSprite = new SingleSprite("first"); Sprite secondSprite = new SingleSprite("second"); Sprite thirdSprite = new SingleSprite("third"); Sprite fourthSprite = new SingleSprite("fourth"); Script testScript = new StartScript(); Script otherScript = new StartScript(); HideBrick hideBrick = new HideBrick(); ShowBrick showBrick = new ShowBrick(); SetSizeToBrick setSizeToBrick = new SetSizeToBrick(size); ComeToFrontBrick comeToFrontBrick = new ComeToFrontBrick(); PlaceAtBrick placeAtBrick = new PlaceAtBrick(xPosition, yPosition); testScript.addBrick(hideBrick); testScript.addBrick(showBrick); testScript.addBrick(setSizeToBrick); testScript.addBrick(comeToFrontBrick); otherScript.addBrick(placeAtBrick); otherScript.setPaused(true); firstSprite.addScript(testScript); secondSprite.addScript(otherScript); project.getDefaultScene().addSprite(firstSprite); project.getDefaultScene().addSprite(secondSprite); project.getDefaultScene().addSprite(thirdSprite); project.getDefaultScene().addSprite(fourthSprite); storageHandler.saveProject(project); Project loadedProject = storageHandler.loadProject(projectName, getInstrumentation().getContext()); Scene preScene = project.getDefaultScene(); Scene postScene = loadedProject.getDefaultScene(); ArrayList<Sprite> preSpriteList = (ArrayList<Sprite>) project.getDefaultScene().getSpriteList(); ArrayList<Sprite> postSpriteList = (ArrayList<Sprite>) loadedProject.getDefaultScene().getSpriteList(); // Test scene name: assertEquals( "Scene does not match after deserialization", preScene.getName(), postScene.getName()); // Test sprite names: assertEquals( "First sprite does not match after deserialization", preSpriteList.get(0).getName(), postSpriteList.get(0).getName()); assertEquals( "Second sprite does not match after deserialization", preSpriteList.get(1).getName(), postSpriteList.get(1).getName()); assertEquals( "Third sprite does not match after deserialization", preSpriteList.get(2).getName(), postSpriteList.get(2).getName()); assertEquals( "Fourth sprite does not match after deserialization", preSpriteList.get(3).getName(), postSpriteList.get(3).getName()); assertEquals( "Fifth sprite does not match after deserialization", preSpriteList.get(4).getName(), postSpriteList.get(4).getName()); // Test project name: assertEquals( "Title missmatch after deserialization", project.getName(), loadedProject.getName()); // Test random brick values Formula actualXPosition = ((FormulaBrick) postSpriteList.get(2).getScript(0).getBrickList().get(0)) .getFormulaWithBrickField(Brick.BrickField.X_POSITION); Formula actualYPosition = ((FormulaBrick) postSpriteList.get(2).getScript(0).getBrickList().get(0)) .getFormulaWithBrickField(Brick.BrickField.Y_POSITION); Formula actualSize = ((FormulaBrick) postSpriteList.get(1).getScript(0).getBrickList().get(2)) .getFormulaWithBrickField(Brick.BrickField.SIZE); assertEquals("Size was not deserialized right", size, interpretFormula(actualSize, null)); assertEquals( "XPosition was not deserialized right", xPosition, interpretFormula(actualXPosition, null).intValue()); assertEquals( "YPosition was not deserialized right", yPosition, interpretFormula(actualYPosition, null).intValue()); assertFalse("paused should not be set in script", preSpriteList.get(1).getScript(0).isPaused()); // Test version codes and names // final int preVersionCode = (Integer) TestUtils.getPrivateField("catroidVersionCode", // project, false); // final int postVersionCode = (Integer) TestUtils.getPrivateField("catroidVersionCode", // loadedProject, false); // assertEquals("Version codes are not equal", preVersionCode, postVersionCode); // // final String preVersionName = (String) TestUtils.getPrivateField("catroidVersionName", // project, false); // final String postVersionName = (String) TestUtils.getPrivateField("catroidVersionName", // loadedProject, false); // assertEquals("Version names are not equal", preVersionName, postVersionName); }