public void testBroadcastWait() {
    Sprite sprite = new Sprite("spriteOne");
    Script scriptWait = new StartScript(sprite);
    BroadcastWaitBrick broadcastWaitBrick = new BroadcastWaitBrick(sprite);
    String message = "waitTest";
    broadcastWaitBrick.setSelectedMessage(message);
    int testPosition = 100;
    SetXBrick setXBrick = new SetXBrick(sprite, testPosition);
    scriptWait.addBrick(broadcastWaitBrick);
    scriptWait.addBrick(setXBrick);
    sprite.addScript(scriptWait);

    BroadcastScript broadcastScript = new BroadcastScript(sprite);
    WaitBrick waitBrick = new WaitBrick(sprite, 500);
    int setTestPosition = 20;
    SetXBrick setXBrick2 = new SetXBrick(sprite, setTestPosition);
    broadcastScript.setBroadcastMessage(message);
    broadcastScript.addBrick(waitBrick);
    broadcastScript.addBrick(setXBrick2);
    sprite.addScript(broadcastScript);

    sprite.startStartScripts();

    try {
      Thread.sleep(1000);
    } catch (InterruptedException ignored) {
    }

    assertEquals("Broadcast and wait failed", testPosition, (int) sprite.costume.getXPosition());
  }
示例#2
0
  private void addScriptToProject(int position, ScriptBrick scriptBrick) {
    Sprite currentSprite = ProjectManager.getInstance().getCurrentSprite();

    int[] temp = getScriptAndBrickIndexFromProject(position);

    int scriptPosition = temp[0];
    int brickPosition = temp[1];

    Script newScript = scriptBrick.getScriptSafe();
    if (currentSprite.getNumberOfBricks() > 0) {
      int addScriptTo = position == 0 ? 0 : scriptPosition + 1;
      currentSprite.addScript(addScriptTo, newScript);
    } else {
      currentSprite.addScript(newScript);
    }

    Script previousScript = currentSprite.getScript(scriptPosition);
    if (previousScript != null) {
      Brick brick;
      int size = previousScript.getBrickList().size();
      for (int i = brickPosition; i < size; i++) {
        brick = previousScript.getBrick(brickPosition);
        previousScript.removeBrick(brick);
        newScript.addBrick(brick);
      }
    }
    ProjectManager.getInstance().setCurrentScript(newScript);
  }
  public void testBroadcastWait() {
    Sprite sprite = new Sprite("spriteOne");
    Script scriptWait = new StartScript(sprite);
    String message = "waitTest";
    BroadcastWaitBrick broadcastWaitBrick = new BroadcastWaitBrick(sprite, message);
    int testPosition = 100;
    SetXBrick setXBrick = new SetXBrick(sprite, testPosition);
    scriptWait.addBrick(broadcastWaitBrick);
    scriptWait.addBrick(setXBrick);
    sprite.addScript(scriptWait);

    BroadcastScript broadcastScript = new BroadcastScript(sprite, message);
    WaitBrick waitBrick = new WaitBrick(sprite, 500);
    int setTestPosition = 20;
    SetXBrick setXBrick2 = new SetXBrick(sprite, setTestPosition);
    broadcastScript.addBrick(waitBrick);
    broadcastScript.addBrick(setXBrick2);
    sprite.addScript(broadcastScript);

    Project project = new Project(getContext(), "testProject");
    project.addSprite(sprite);
    ProjectManager.getInstance().setProject(project);

    sprite.createStartScriptActionSequence();

    while (!sprite.look.getAllActionsAreFinished()) {
      sprite.look.act(1.0f);
    }

    assertEquals(
        "Broadcast and wait failed",
        testPosition,
        (int) sprite.look.getXInUserInterfaceDimensionUnit());
  }
  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());
  }
  private void createProject() {
    Project project = new Project(null, UiTestUtils.PROJECTNAME1);

    Sprite spriteCat = new Sprite(backgroundName);
    Script startScriptCat = new StartScript();
    Script scriptTappedCat = new WhenScript();
    Brick setXBrick = new SetXBrick(50);
    Brick setYBrick = new SetYBrick(50);
    Brick changeXBrick = new ChangeXByNBrick(50);
    startScriptCat.addBrick(setYBrick);
    startScriptCat.addBrick(setXBrick);
    scriptTappedCat.addBrick(changeXBrick);

    spriteCat.addScript(startScriptCat);
    spriteCat.addScript(scriptTappedCat);
    project.addSprite(spriteCat);

    ProjectManager.getInstance().setProject(project);
    ProjectManager.getInstance().setCurrentSprite(spriteCat);
    ProjectManager.getInstance().setCurrentScript(startScriptCat);

    File imageFile =
        UiTestUtils.saveFileToProject(
            project.getName(),
            "catroid_sunglasses.png",
            org.catrobat.catroid.test.R.drawable.catroid_sunglasses,
            getInstrumentation().getContext(),
            UiTestUtils.FileTypes.IMAGE);

    ProjectManager projectManager = ProjectManager.getInstance();
    List<LookData> lookDataList = projectManager.getCurrentSprite().getLookDataList();
    LookData lookData = new LookData();
    lookData.setLookFilename(imageFile.getName());
    lookData.setLookName("Catroid sun");
    lookDataList.add(lookData);
    projectManager
        .getFileChecksumContainer()
        .addChecksum(lookData.getChecksum(), lookData.getAbsolutePath());

    File soundFile =
        UiTestUtils.saveFileToProject(
            project.getName(),
            "longsound.mp3",
            org.catrobat.catroid.test.R.raw.longsound,
            getInstrumentation().getContext(),
            UiTestUtils.FileTypes.SOUND);
    SoundInfo soundInfo = new SoundInfo();
    soundInfo.setSoundFileName(soundFile.getName());
    soundInfo.setTitle("longsound");

    List<SoundInfo> soundInfoList = ProjectManager.getInstance().getCurrentSprite().getSoundList();
    soundInfoList.add(soundInfo);
    ProjectManager.getInstance()
        .getFileChecksumContainer()
        .addChecksum(soundInfo.getChecksum(), soundInfo.getAbsolutePath());
  }
示例#6
0
  public void testAliasesAndXmlHeader() {

    String projectName = "myProject";

    File projectFile = new File(Constants.DEFAULT_ROOT + "/" + projectName);
    if (projectFile.exists()) {
      UtilFile.deleteDirectory(projectFile);
    }

    Project project = new Project(getContext(), projectName);
    Sprite sprite = new Sprite("testSprite");
    Script startScript = new StartScript(sprite);
    Script whenScript = new WhenScript(sprite);
    sprite.addScript(startScript);
    sprite.addScript(whenScript);
    project.addSprite(sprite);

    ArrayList<Brick> startScriptBrickList = new ArrayList<Brick>();
    ArrayList<Brick> whenScriptBrickList = new ArrayList<Brick>();
    startScriptBrickList.add(new ChangeXByNBrick(sprite, 4));
    startScriptBrickList.add(new ChangeYByNBrick(sprite, 5));
    startScriptBrickList.add(new ComeToFrontBrick(sprite));
    startScriptBrickList.add(new GoNStepsBackBrick(sprite, 5));
    startScriptBrickList.add(new HideBrick(sprite));
    startScriptBrickList.add(new WhenStartedBrick(sprite, startScript));

    whenScriptBrickList.add(new PlaySoundBrick(sprite));
    whenScriptBrickList.add(new SetSizeToBrick(sprite, 50));
    whenScriptBrickList.add(new SetCostumeBrick(sprite));
    whenScriptBrickList.add(new SetXBrick(sprite, 50));
    whenScriptBrickList.add(new SetYBrick(sprite, 50));
    whenScriptBrickList.add(new ShowBrick(sprite));
    whenScriptBrickList.add(new WaitBrick(sprite, 1000));

    for (Brick b : startScriptBrickList) {
      startScript.addBrick(b);
    }
    for (Brick b : whenScriptBrickList) {
      whenScript.addBrick(b);
    }

    storageHandler.saveProject(project);
    String projectString = TestUtils.getProjectfileAsString(projectName);
    assertFalse("project contains package information", projectString.contains("org.catrobat"));

    String xmlHeader = (String) TestUtils.getPrivateField("XML_HEADER", new XmlSerializer(), false);
    assertTrue(
        "Project file did not contain correct XML header.", projectString.startsWith(xmlHeader));

    projectFile = new File(Constants.DEFAULT_ROOT + "/" + projectName);
    if (projectFile.exists()) {
      UtilFile.deleteDirectory(projectFile);
    }
  }
示例#7
0
  public void addNewBrick(int position, Brick brickToBeAdded, boolean initInsertedBrick) {

    if (draggedBrick != null) {
      Log.w(TAG, "Want to add Brick while there is another one currently dragged.");
      return;
    }

    Sprite currentSprite = ProjectManager.getInstance().getCurrentSprite();
    int scriptCount = currentSprite.getNumberOfScripts();
    if (scriptCount == 0 && brickToBeAdded instanceof ScriptBrick) {
      currentSprite.addScript(((ScriptBrick) brickToBeAdded).getScriptSafe());
      initBrickList();
      notifyDataSetChanged();
      return;
    }

    if (position < 0) {
      position = 0;
    } else if (position > brickList.size()) {
      position = brickList.size();
    }

    if (brickToBeAdded instanceof ScriptBrick) {

      brickList.add(position, brickToBeAdded);
      position = getNewPositionForScriptBrick(position, brickToBeAdded);
      brickList.remove(brickToBeAdded);
      brickList.add(position, brickToBeAdded);
      scrollToPosition(position);

    } else {

      position = getNewPositionIfEndingBrickIsThere(position, brickToBeAdded);
      position = position <= 0 ? 1 : position;
      position = position > brickList.size() ? brickList.size() : position;
      brickList.add(position, brickToBeAdded);
    }

    this.initInsertedBrick = initInsertedBrick;
    this.positionOfInsertedBrick = position;

    if (scriptCount == 0 && userBrick == null) {
      Script script = new StartScript();
      currentSprite.addScript(script);
      brickList.add(0, script.getScriptBrick());
      ProjectManager.getInstance().setCurrentScript(script);
      clearCheckedItems();
      positionOfInsertedBrick = 1;
    }

    notifyDataSetChanged();
  }
示例#8
0
  @Given("^'(\\w+)' has a Start script$")
  public void object_has_start_script(String object) {
    programWaitLockPermits -= 1;
    Project project = ProjectManager.getInstance().getCurrentProject();
    Sprite sprite = Util.findSprite(project, object);
    StartScript script = new StartScript(sprite);

    script.addBrick(
        new CallbackBrick(
            sprite,
            new CallbackBrick.BrickCallback() {
              @Override
              public void onCallback() {
                synchronized (programStartWaitLock) {
                  if (!programHasStarted) {
                    programHasStarted = true;
                    programStartWaitLock.notify();
                  }
                }
              }
            }));

    sprite.addScript(script);
    Cucumber.put(Cucumber.KEY_CURRENT_SCRIPT, script);
  }
示例#9
0
  private void createTestProject(String projectName) {
    double size = 0.8;

    Project project = new Project(null, projectName);
    Sprite firstSprite = new Sprite("cat");

    Script testScript = new StartScript();

    brickListToCheck = new ArrayList<Brick>();
    brickListToCheck.add(new HideBrick());
    brickListToCheck.add(new ShowBrick());
    brickListToCheck.add(new SetSizeToBrick(size));

    for (Brick brick : brickListToCheck) {
      testScript.addBrick(brick);
    }

    firstSprite.addScript(testScript);

    project.addSprite(firstSprite);

    ProjectManager.getInstance().setProject(project);
    ProjectManager.getInstance().setCurrentSprite(firstSprite);
    ProjectManager.getInstance().setCurrentScript(testScript);
  }
示例#10
0
  @Given("^'(\\w+)' has a When '(\\w+)' script$")
  public void object_has_a_when_script(String object, String message) {
    programWaitLockPermits -= 1;
    Project project = ProjectManager.getInstance().getCurrentProject();
    Sprite sprite = Util.findSprite(project, object);
    BroadcastScript script = new BroadcastScript(sprite, message);

    sprite.addScript(script);
    Cucumber.put(Cucumber.KEY_CURRENT_SCRIPT, script);
  }
  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);
  }
示例#13
0
  private void createProject() {
    project = new Project(null, UiTestUtils.DEFAULT_TEST_PROJECT_NAME);
    Sprite sprite = new Sprite("cat");
    Script script = new StartScript(sprite);
    script.addBrick(new StopAllSoundsBrick(sprite));

    sprite.addScript(script);
    project.addSprite(sprite);

    ProjectManager.getInstance().setProject(project);
    ProjectManager.getInstance().setCurrentSprite(sprite);
    ProjectManager.getInstance().setCurrentScript(script);
  }
示例#14
0
  private void createProject() {
    project = new Project(null, UiTestUtils.DEFAULT_TEST_PROJECT_NAME);
    Sprite sprite = new Sprite("cat");
    Script script = new StartScript(sprite);
    ifBrick = new IfLogicBeginBrick(sprite, 0);
    IfLogicElseBrick ifElseBrick = new IfLogicElseBrick(sprite, ifBrick);
    IfLogicEndBrick ifEndBrick = new IfLogicEndBrick(sprite, ifElseBrick, ifBrick);
    ifBrick.setElseBrick(ifElseBrick);
    ifBrick.setEndBrick(ifEndBrick);

    script.addBrick(ifBrick);
    script.addBrick(new ChangeYByNBrick(sprite, -10));
    script.addBrick(ifElseBrick);
    script.addBrick(ifEndBrick);

    sprite.addScript(script);
    sprite.addScript(new StartScript(sprite));
    project.addSprite(sprite);

    ProjectManager.getInstance().setProject(project);
    ProjectManager.getInstance().setCurrentSprite(sprite);
    ProjectManager.getInstance().setCurrentScript(script);
  }
  private void createProject() {
    project = new Project(null, UiTestUtils.DEFAULT_TEST_PROJECT_NAME);
    sprite = new Sprite("cat");
    Script script = new StartScript(sprite);
    placeAtBrick = new PlaceAtBrick(sprite, INITIAL_X, INITIAL_Y);
    script.addBrick(placeAtBrick);

    sprite.addScript(script);
    project.addSprite(sprite);

    ProjectManager.getInstance().setProject(project);
    ProjectManager.getInstance().setCurrentSprite(sprite);
    ProjectManager.getInstance().setCurrentScript(script);
  }
示例#16
0
  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;
  }
示例#17
0
  public void testBroadcast() {
    Sprite sprite = new Sprite("testSprite");
    Script script = new StartScript(sprite);
    BroadcastBrick broadcastBrick = new BroadcastBrick(sprite);
    String message = "simpleTest";
    broadcastBrick.setSelectedMessage(message);
    script.addBrick(broadcastBrick);
    sprite.addScript(script);

    BroadcastScript broadcastScript = new BroadcastScript(sprite);
    int testPosition = 100;
    SetXBrick testBrick = new SetXBrick(sprite, testPosition);
    broadcastScript.setBroadcastMessage(message);
    broadcastScript.addBrick(testBrick);
    sprite.addScript(broadcastScript);

    sprite.startStartScripts();
    try {
      Thread.sleep(1000);
    } catch (InterruptedException ignored) {
    }

    assertEquals("Simple broadcast failed", testPosition, (int) sprite.costume.getXPosition());
  }
示例#18
0
  private void createProject() {
    ProjectManager projectManager = ProjectManager.getInstance();
    Project project = new Project(null, UiTestUtils.DEFAULT_TEST_PROJECT_NAME);
    Sprite firstSprite = new Sprite("cat");
    Script testScript = new StartScript(firstSprite);

    SetLookBrick setLookBrick = new SetLookBrick(firstSprite);
    testScript.addBrick(setLookBrick);

    firstSprite.addScript(testScript);
    project.addSprite(firstSprite);

    projectManager.setProject(project);
    projectManager.setCurrentSprite(firstSprite);
    projectManager.setCurrentScript(testScript);
    lookDataList = projectManager.getCurrentSprite().getLookDataList();

    lookFile =
        UiTestUtils.saveFileToProject(
            UiTestUtils.DEFAULT_TEST_PROJECT_NAME,
            "image.png",
            RESOURCE_LOOK,
            getInstrumentation().getContext(),
            UiTestUtils.FileTypes.IMAGE);
    LookData lookData = new LookData();
    lookData.setLookFilename(lookFile.getName());
    lookData.setLookName(lookName);

    lookFile2 =
        UiTestUtils.saveFileToProject(
            UiTestUtils.DEFAULT_TEST_PROJECT_NAME,
            "image2.png",
            RESOURCE_LOOK2,
            getInstrumentation().getContext(),
            UiTestUtils.FileTypes.IMAGE);
    LookData lookData2 = new LookData();
    lookData2.setLookFilename(lookFile2.getName());
    lookData2.setLookName(lookName2);

    lookDataList.add(lookData);
    lookDataList.add(lookData2);
    ProjectManager.getInstance()
        .getFileChecksumContainer()
        .addChecksum(lookData.getChecksum(), lookData.getAbsolutePath());
    ProjectManager.getInstance()
        .getFileChecksumContainer()
        .addChecksum(lookData2.getChecksum(), lookData2.getAbsolutePath());
  }
  private void createProject() {
    project = new Project(null, UiTestUtils.DEFAULT_TEST_PROJECT_NAME);
    Sprite sprite = new Sprite("cat");
    Script script = new StartScript();

    motorBrick =
        new LegoNxtMotorMoveBrick(LegoNxtMotorMoveBrick.Motor.MOTOR_A, SET_SPEED_INITIALLY);

    script.addBrick(motorBrick);

    sprite.addScript(script);
    project.addSprite(sprite);

    ProjectManager.getInstance().setProject(project);
    ProjectManager.getInstance().setCurrentSprite(sprite);
    ProjectManager.getInstance().setCurrentScript(script);
  }
  @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);
  }
示例#21
0
  private void createProject() {
    project = new Project(null, UiTestUtils.DEFAULT_TEST_PROJECT_NAME);
    sprite = new Sprite("cat");
    Script script = new StartScript(sprite);
    ProjectManager.getInstance().setProject(project);
    ProjectManager.getInstance().setCurrentSprite(sprite);
    ProjectManager.getInstance().setCurrentScript(script);

    userVariablesContainer = project.getUserVariables();
    userVariablesContainer.addProjectUserVariable("p1");
    userVariablesContainer.addProjectUserVariable("p2");
    userVariablesContainer.addSpriteUserVariable("sprite_var1");
    userVariablesContainer.addSpriteUserVariable("sprite_var2");

    setVariableBrick = new SetVariableBrick(sprite, 0.0);
    script.addBrick(setVariableBrick);
    changeVariableBrick = new ChangeVariableBrick(sprite, 1.1);
    script.addBrick(changeVariableBrick);

    sprite.addScript(script);
    project.addSprite(sprite);
  }
  @Override
  protected void setUp() {
    this.project = new Project(null, "testProject");
    firstSprite = new Sprite("firstSprite");
    startScript = new StartScript();
    changeBrick = new ChangeSizeByNBrick(10);
    firstSprite.addScript(startScript);
    startScript.addBrick(changeBrick);
    project.addSprite(firstSprite);
    ProjectManager.getInstance().setProject(project);
    ProjectManager.getInstance().setCurrentSprite(firstSprite);

    UserVariablesContainer userVariableContainer =
        ProjectManager.getInstance().getCurrentProject().getUserVariables();
    userVariableContainer
        .addProjectUserVariable(PROJECT_USER_VARIABLE)
        .setValue(USER_VARIABLE_1_VALUE_TYPE_DOUBLE);
    userVariableContainer
        .addSpriteUserVariableToSprite(firstSprite, SPRITE_USER_VARIABLE)
        .setValue(USER_VARIABLE_2_VALUE_TYPE_DOUBLE);
    userVariableContainer
        .addProjectUserVariable(PROJECT_USER_VARIABLE_2)
        .setValue(USER_VARIABLE_3_VALUE_TYPE_STRING);
  }
示例#23
0
  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);
  }
示例#24
0
  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());
  }
示例#25
0
  public void testSerializeProject() {

    int xPosition = 457;
    int yPosition = 598;
    double size = 0.8;

    Project project = new Project(getContext(), "testProject");
    Sprite firstSprite = new Sprite("first");
    Sprite secondSprite = new Sprite("second");
    Sprite thirdSprite = new Sprite("third");
    Sprite fourthSprite = new Sprite("fourth");
    Script testScript = new StartScript(firstSprite);
    Script otherScript = new StartScript(secondSprite);
    HideBrick hideBrick = new HideBrick(firstSprite);
    ShowBrick showBrick = new ShowBrick(firstSprite);
    SetSizeToBrick setSizeToBrick = new SetSizeToBrick(secondSprite, size);
    ComeToFrontBrick comeToFrontBrick = new ComeToFrontBrick(firstSprite);
    PlaceAtBrick placeAtBrick = new PlaceAtBrick(secondSprite, xPosition, yPosition);

    // adding Bricks: ----------------
    testScript.addBrick(hideBrick);
    testScript.addBrick(showBrick);
    testScript.addBrick(setSizeToBrick);
    testScript.addBrick(comeToFrontBrick);

    otherScript.addBrick(placeAtBrick); // secondSprite
    otherScript.setPaused(true);
    // -------------------------------

    firstSprite.addScript(testScript);
    secondSprite.addScript(otherScript);

    project.addSprite(firstSprite);
    project.addSprite(secondSprite);
    project.addSprite(thirdSprite);
    project.addSprite(fourthSprite);

    storageHandler.saveProject(project);

    Project loadedProject = storageHandler.loadProject("testProject");

    ArrayList<Sprite> preSpriteList = (ArrayList<Sprite>) project.getSpriteList();
    ArrayList<Sprite> postSpriteList = (ArrayList<Sprite>) loadedProject.getSpriteList();

    // 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
    int actualXPosition =
        (Integer)
            TestUtils.getPrivateField(
                "xPosition", (postSpriteList.get(2).getScript(0).getBrickList().get(0)), false);
    int actualYPosition =
        (Integer)
            TestUtils.getPrivateField(
                "yPosition", (postSpriteList.get(2).getScript(0).getBrickList().get(0)), false);

    double actualSize =
        (Double)
            TestUtils.getPrivateField(
                "size", (postSpriteList.get(1).getScript(0).getBrickList().get(2)), false);

    assertEquals("Size was not deserialized right", size, actualSize);
    assertEquals("XPosition was not deserialized right", xPosition, actualXPosition);
    assertEquals("YPosition was not deserialized right", yPosition, actualYPosition);

    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);
  }
示例#26
0
  private void createProject() {
    ScreenValues.SCREEN_HEIGHT = SCREEN_HEIGHT;
    ScreenValues.SCREEN_WIDTH = SCREEN_WIDTH;

    project = new Project(null, UiTestUtils.DEFAULT_TEST_PROJECT_NAME);

    // yellow Sprite
    Sprite yellowSprite = new Sprite("yellowSprite");
    StartScript yellowStartScript = new StartScript();
    LookData yellowLookData = new LookData();
    String yellowImageName = "yellow_image.bmp";

    yellowLookData.setLookName(yellowImageName);

    yellowSprite.getLookDataList().add(yellowLookData);

    yellowStartScript.addBrick(new PlaceAtBrick(-21, 21));

    yellowSprite.addScript(yellowStartScript);

    WhenScript yellowWhenScript = new WhenScript();
    SetGhostEffectBrick yellowSetGhostEffectBrick = new SetGhostEffectBrick(100d);
    yellowWhenScript.addBrick(yellowSetGhostEffectBrick);

    yellowSprite.addScript(yellowWhenScript);

    // blue Sprite
    Sprite blueSprite = new Sprite("blueSprite");
    StartScript blueStartScript = new StartScript();
    LookData blueLookData = new LookData();
    String blueImageName = "blue_image.bmp";

    blueLookData.setLookName(blueImageName);

    blueSprite.getLookDataList().add(blueLookData);

    blueStartScript.addBrick(new PlaceAtBrick(21, 21));

    blueSprite.addScript(blueStartScript);

    WhenScript blueWhenScript = new WhenScript();
    SetSizeToBrick blueSetSizeToBrick = new SetSizeToBrick(200d);
    blueWhenScript.addBrick(blueSetSizeToBrick);

    blueSprite.addScript(blueWhenScript);

    // green Sprite
    Sprite greenSprite = new Sprite("greenSprite");
    StartScript greenStartScript = new StartScript();
    LookData greenLookData = new LookData();
    String greenImageName = "green_image.bmp";

    greenLookData.setLookName(greenImageName);

    greenSprite.getLookDataList().add(greenLookData);

    greenStartScript.addBrick(new PlaceAtBrick(21, -21));

    greenSprite.addScript(greenStartScript);

    WhenScript greenWhenScript = new WhenScript();
    ComeToFrontBrick greenComeToFrontBrick = new ComeToFrontBrick();
    greenWhenScript.addBrick(greenComeToFrontBrick);

    greenSprite.addScript(greenWhenScript);

    // red Sprite
    Sprite redSprite = new Sprite("redSprite");
    StartScript redStartScript = new StartScript();
    LookData redLookData = new LookData();
    String redImageName = "red_image.bmp";

    redLookData.setLookName(redImageName);

    redSprite.getLookDataList().add(redLookData);

    redStartScript.addBrick(new PlaceAtBrick(-21, -21));

    redSprite.addScript(redStartScript);

    WhenScript redWhenScript = new WhenScript();
    ComeToFrontBrick redComeToFrontBrick = new ComeToFrontBrick();
    SetBrightnessBrick redSetBrightnessBrick = new SetBrightnessBrick(50d);
    TurnLeftBrick redTurnLeftBrick = new TurnLeftBrick(45d);
    redWhenScript.addBrick(redComeToFrontBrick);
    redWhenScript.addBrick(redSetBrightnessBrick);
    redWhenScript.addBrick(redTurnLeftBrick);

    redSprite.addScript(redWhenScript);

    // black Sprite
    Sprite blackSprite = new Sprite("blackSprite");
    StartScript blackStartScript = new StartScript();
    LookData blackLookData = new LookData();
    String blackImageName = "black_image.bmp";

    blackLookData.setLookName(blackImageName);

    blackSprite.getLookDataList().add(blackLookData);

    blackStartScript.addBrick(new PlaceAtBrick(-50, 50));

    blackSprite.addScript(blackStartScript);

    WhenScript blackWhenScript = new WhenScript();
    ComeToFrontBrick blackComeToFrontBrick = new ComeToFrontBrick();
    SetBrightnessBrick blackSetBrightnessBrick = new SetBrightnessBrick(150d);
    blackWhenScript.addBrick(blackComeToFrontBrick);
    blackWhenScript.addBrick(blackSetBrightnessBrick);

    blackSprite.addScript(blackWhenScript);

    project.addSprite(blackSprite);
    project.addSprite(yellowSprite);
    project.addSprite(redSprite);
    project.addSprite(greenSprite);
    project.addSprite(blueSprite);

    StorageHandler.getInstance().saveProject(project);

    File yellowImageFile =
        UiTestUtils.saveFileToProject(
            project.getName(),
            yellowImageName,
            org.catrobat.catroid.test.R.raw.yellow_image,
            getInstrumentation().getContext(),
            UiTestUtils.FileTypes.IMAGE);
    File greenImageFile =
        UiTestUtils.saveFileToProject(
            project.getName(),
            greenImageName,
            org.catrobat.catroid.test.R.raw.green_image,
            getInstrumentation().getContext(),
            UiTestUtils.FileTypes.IMAGE);
    File blueImageFile =
        UiTestUtils.saveFileToProject(
            project.getName(),
            blueImageName,
            org.catrobat.catroid.test.R.raw.blue_image,
            getInstrumentation().getContext(),
            UiTestUtils.FileTypes.IMAGE);
    File redImageFile =
        UiTestUtils.saveFileToProject(
            project.getName(),
            redImageName,
            org.catrobat.catroid.test.R.raw.red_image,
            getInstrumentation().getContext(),
            UiTestUtils.FileTypes.IMAGE);
    File blackImageFile =
        UiTestUtils.saveFileToProject(
            project.getName(),
            blackImageName,
            org.catrobat.catroid.test.R.raw.black_image,
            getInstrumentation().getContext(),
            UiTestUtils.FileTypes.IMAGE);
    yellowLookData.setLookFilename(yellowImageFile.getName());
    greenLookData.setLookFilename(greenImageFile.getName());
    blueLookData.setLookFilename(blueImageFile.getName());
    redLookData.setLookFilename(redImageFile.getName());
    blackLookData.setLookFilename(blackImageFile.getName());

    StorageHandler.getInstance().saveProject(project);
    ProjectManager.getInstance().setProject(project);
  }