Пример #1
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);
    }
  }
Пример #2
0
  @Override
  protected void setUp() throws Exception {

    TestUtils.clearProject(currentProjectName);
    storageHandler = StorageHandler.getInstance();
    Project testCopyFile = new Project(null, currentProjectName);
    testCopyFile.getXmlHeader().virtualScreenHeight = 1000;
    testCopyFile.getXmlHeader().virtualScreenWidth = 1000;
    projectManager = ProjectManager.getInstance();
    storageHandler.saveProject(testCopyFile);
    projectManager.setProject(testCopyFile);

    final String imagePath = Constants.DEFAULT_ROOT + "/testImage.png";
    testImage = new File(imagePath);
    if (!testImage.exists()) {
      testImage.createNewFile();
    }
    InputStream in =
        getInstrumentation().getContext().getResources().openRawResource(IMAGE_FILE_ID);
    OutputStream out =
        new BufferedOutputStream(new FileOutputStream(testImage), Constants.BUFFER_8K);

    byte[] buffer = new byte[Constants.BUFFER_8K];
    int length = 0;
    while ((length = in.read(buffer)) > 0) {
      out.write(buffer, 0, length);
    }

    in.close();
    out.flush();
    out.close();

    final String soundPath = Constants.DEFAULT_ROOT + "/testsound.mp3";
    testSound = new File(soundPath);
    if (!testSound.exists()) {
      testSound.createNewFile();
    }
    in = getInstrumentation().getContext().getResources().openRawResource(R.raw.testsound);
    out = new BufferedOutputStream(new FileOutputStream(testSound), Constants.BUFFER_8K);
    buffer = new byte[Constants.BUFFER_8K];
    length = 0;
    while ((length = in.read(buffer)) > 0) {
      out.write(buffer, 0, length);
    }

    in.close();
    out.flush();
    out.close();
  }
Пример #3
0
  public void createTestProject(String projectName) {
    StorageHandler storageHandler = StorageHandler.getInstance();
    Project project = new Project(getActivity(), projectName);
    Sprite firstSprite = new Sprite("cat");
    Sprite secondSprite = new Sprite("dog");
    Sprite thirdSprite = new Sprite("horse");
    Sprite fourthSprite = new Sprite("pig");

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

    storageHandler.saveProject(project);
  }
Пример #4
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);
  }
Пример #5
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());
  }
Пример #6
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);
  }