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());
  }
Exemple #2
0
  public void testBehaviourWithoutBricks() {
    Project project = ProjectManager.getInstance().getCurrentProject();
    assertNotNull("current project was null", project);

    Sprite blueSprite = project.getSpriteList().get(4);
    while (blueSprite.getNumberOfScripts() > 0) {
      blueSprite.removeScript(blueSprite.getScript(0));
    }

    assertEquals("there shouldn't be any script left", 0, blueSprite.getNumberOfScripts());
    assertEquals("there shouldn't be any script left", 0, blueSprite.getNumberOfBricks());
    StorageHandler.getInstance().loadProject(project.getName());

    solo.waitForActivity(StageActivity.class.getSimpleName());
    solo.sleep(1400);

    byte[] screenArray = StageActivity.stageListener.getPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    UiTestUtils.comparePixelArrayWithPixelScreenArray(
        BLUE_PIXEL, screenArray, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    UiTestUtils.comparePixelArrayWithPixelScreenArray(
        BLUE_PIXEL, screenArray, -19, -19, SCREEN_WIDTH, SCREEN_HEIGHT);
    UiTestUtils.comparePixelArrayWithPixelScreenArray(
        BLUE_PIXEL, screenArray, -19, 19, SCREEN_WIDTH, SCREEN_HEIGHT);
    UiTestUtils.comparePixelArrayWithPixelScreenArray(
        BLUE_PIXEL, screenArray, 19, -19, SCREEN_WIDTH, SCREEN_HEIGHT);
    UiTestUtils.comparePixelArrayWithPixelScreenArray(
        BLUE_PIXEL, screenArray, 19, 19, SCREEN_WIDTH, SCREEN_HEIGHT);

    solo.sleep(2000);
  }
Exemple #3
0
  @Override
  public void create() {
    font = new BitmapFont();
    font.setColor(1f, 0f, 0.05f, 1f);
    font.setScale(1.2f);

    project = ProjectManager.getInstance().getCurrentProject();
    pathForScreenshot = Utils.buildProjectPath(project.getName()) + "/";

    virtualWidth = project.getXmlHeader().virtualScreenWidth;
    virtualHeight = project.getXmlHeader().virtualScreenHeight;

    virtualWidthHalf = virtualWidth / 2;
    virtualHeightHalf = virtualHeight / 2;

    stage = new Stage(virtualWidth, virtualHeight, true);
    batch = stage.getSpriteBatch();

    Gdx.gl.glViewport(0, 0, ScreenValues.SCREEN_WIDTH, ScreenValues.SCREEN_HEIGHT);
    initScreenMode();

    sprites = project.getSpriteList();
    for (Sprite sprite : sprites) {
      sprite.resetSprite();
      sprite.look.createBrightnessContrastShader();
      stage.addActor(sprite.look);
      sprite.resume();
    }

    passepartout =
        new Passepartout(
            ScreenValues.SCREEN_WIDTH,
            ScreenValues.SCREEN_HEIGHT,
            maximizeViewPortWidth,
            maximizeViewPortHeight,
            virtualWidth,
            virtualHeight);
    stage.addActor(passepartout);

    if (DEBUG) {
      OrthoCamController camController = new OrthoCamController(camera);
      InputMultiplexer multiplexer = new InputMultiplexer();
      multiplexer.addProcessor(camController);
      multiplexer.addProcessor(stage);
      Gdx.input.setInputProcessor(multiplexer);
      fpsLogger = new FPSLogger();
    } else {
      Gdx.input.setInputProcessor(stage);
    }

    axes = new Texture(Gdx.files.internal("stage/red_pixel.bmp"));
    skipFirstFrameForAutomaticScreenshot = true;
    if (checkIfAutomaticScreenshotShouldBeTaken) {
      makeAutomaticScreenshot = project.manualScreenshotExists(SCREENSHOT_MANUAL_FILE_NAME);
    }
  }
  public void testWritePermissionFile() throws IOException {
    Project project = generateMultiplePermissionsProject();
    ProjectManager.getInstance().setProject(project);
    StorageHandler.getInstance().saveProject(project);

    File permissionsFile = new File(buildProjectPath(project.getName()), PROJECTPERMISSIONS_NAME);
    assertTrue("File containing the permissions could not be written", permissionsFile.exists());

    // only for assertions. Add future permission; Vibration and LED not activated
    Set<String> permissions = new HashSet<String>();
    permissions.add(Constants.ARDRONE_SUPPORT);
    permissions.add(Constants.BLUETOOTH_LEGO_NXT);
    permissions.add(Constants.TEXT_TO_SPEECH);
    permissions.add(Constants.FACE_DETECTION);

    BufferedReader reader = new BufferedReader(new FileReader(permissionsFile));
    String line;
    while ((line = reader.readLine()) != null) {
      assertTrue("Wrong permission in File found", permissions.contains(line));
    }
  }
Exemple #5
0
  public void testUploadingProjectDescriptionDefaultValue() throws Throwable {
    UiTestUtils.createValidUser(getActivity());
    String testDescription = "Test description";
    String actionSetDescriptionText = solo.getString(R.string.set_description);

    solo.clickOnButton(solo.getString(R.string.main_menu_programs));
    solo.waitForActivity(MyProjectsActivity.class.getSimpleName());
    solo.waitForFragmentById(R.id.fragment_projects_list);
    solo.clickLongOnText(uploadProject.getName());
    assertTrue(
        "context menu not loaded in 5 seconds",
        solo.waitForText(actionSetDescriptionText, 0, 5000));
    solo.clickOnText(actionSetDescriptionText);
    assertTrue(
        "dialog not loaded in 5 seconds", solo.waitForText(actionSetDescriptionText, 0, 5000));
    solo.clearEditText(0);
    solo.enterText(0, testDescription);
    assertTrue(
        "dialog not loaded in 5 seconds", solo.waitForText(actionSetDescriptionText, 0, 5000));
    solo.sleep(300);

    solo.clickOnText(solo.getString(R.string.ok));

    solo.waitForDialogToClose(500);
    solo.goBack();
    solo.waitForActivity(MainMenuActivity.class.getSimpleName());

    solo.clickOnText(solo.getString(R.string.main_menu_upload));
    boolean uploadDialogShown = solo.waitForText(uploadDialogTitle);

    assertTrue("upload project dialog not shown", uploadDialogShown);
    EditText uploadDescriptionView = (EditText) solo.getView(R.id.project_description_upload);
    String uploadDescription = uploadDescriptionView.getText().toString();
    solo.sleep(500);
    assertEquals("Project description was not set or is wrong", testDescription, uploadDescription);
  }
  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);
  }
Exemple #7
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);
  }
  public void testSerializeSettings()
      throws CompatibilityProjectException, OutdatedVersionProjectException,
          LoadingProjectException {

    NXTSensor.Sensor[] sensorMapping =
        new NXTSensor.Sensor[] {
          NXTSensor.Sensor.TOUCH, NXTSensor.Sensor.SOUND,
          NXTSensor.Sensor.LIGHT_INACTIVE, NXTSensor.Sensor.ULTRASONIC
        };

    Reflection.setPrivateField(ProjectManager.getInstance(), "asynchronousTask", false);

    Project project = generateMultiplePermissionsProject();
    ProjectManager.getInstance().setProject(project);

    String projectName = project.getName();
    SettingsActivity.setLegoMindstormsNXTSensorMapping(
        getInstrumentation().getTargetContext(), sensorMapping);

    ProjectManager.getInstance().saveProject(getInstrumentation().getTargetContext());
    Setting setting = project.getSettings().get(0);

    assertTrue("Wrong setting type, LegoNXT setting expected", setting instanceof LegoNXTSetting);

    LegoNXTSetting nxtSetting = (LegoNXTSetting) setting;
    NXTSensor.Sensor[] actualSensorMapping = nxtSetting.getSensorMapping();

    assertEquals("Wrong numer of sensors", 4, actualSensorMapping.length);

    assertEquals("Wrong sensor mapping for touch sensor", sensorMapping[0], actualSensorMapping[0]);
    assertEquals("Wrong sensor mapping for sound sensor", sensorMapping[1], actualSensorMapping[1]);
    assertEquals("Wrong sensor mapping for light sensor", sensorMapping[2], actualSensorMapping[2]);
    assertEquals(
        "Wrong sensor mapping for ultrasonic sensor", sensorMapping[3], actualSensorMapping[3]);

    NXTSensor.Sensor[] changedSensorMapping = sensorMapping.clone();
    changedSensorMapping[0] = NXTSensor.Sensor.LIGHT_ACTIVE;

    SettingsActivity.setLegoMindstormsNXTSensorMapping(
        getInstrumentation().getTargetContext(), changedSensorMapping);

    ProjectManager.getInstance().setProject(null);
    ProjectManager.getInstance().loadProject(projectName, getInstrumentation().getTargetContext());

    actualSensorMapping =
        SettingsActivity.getLegoMindstormsNXTSensorMapping(getInstrumentation().getTargetContext());

    assertEquals("Wrong numer of sensors", 4, actualSensorMapping.length);

    assertEquals(
        "Wrong sensor mapping for touch sensor, settings not correctly loaded from project",
        sensorMapping[0],
        actualSensorMapping[0]);
    assertEquals("Wrong sensor mapping for sound sensor", sensorMapping[1], actualSensorMapping[1]);
    assertEquals("Wrong sensor mapping for light sensor", sensorMapping[2], actualSensorMapping[2]);
    assertEquals(
        "Wrong sensor mapping for ultrasonic sensor", sensorMapping[3], actualSensorMapping[3]);

    project = ProjectManager.getInstance().getCurrentProject();

    setting = project.getSettings().get(0);
    nxtSetting = (LegoNXTSetting) setting;

    assertTrue("Wrong setting type, LegoNXT setting expected", setting instanceof LegoNXTSetting);

    actualSensorMapping = nxtSetting.getSensorMapping();

    assertEquals("Wrong numer of sensors", 4, actualSensorMapping.length);

    assertEquals("Wrong sensor mapping for touch sensor", sensorMapping[0], actualSensorMapping[0]);
    assertEquals("Wrong sensor mapping for sound sensor", sensorMapping[1], actualSensorMapping[1]);
    assertEquals("Wrong sensor mapping for light sensor", sensorMapping[2], actualSensorMapping[2]);
    assertEquals(
        "Wrong sensor mapping for ultrasonic sensor", sensorMapping[3], actualSensorMapping[3]);
  }
  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);
  }