Esempio n. 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);
    }
  }
Esempio n. 2
0
  @Override
  public void setUp() {
    File projectFile =
        new File(
            Constants.DEFAULT_ROOT + "/" + getContext().getString(R.string.default_project_name));

    if (projectFile.exists()) {
      UtilFile.deleteDirectory(projectFile);
    }
  }
Esempio n. 3
0
 public void testDeleteDirectory() {
   UtilFile.deleteDirectory(testDirectory);
   assertFalse("File in subdirectory still exists after call to deleteDirectory", file2.exists());
   assertFalse(
       "Subdirectory in test directory still exists after call to deleteDirectory",
       subDirectory.exists());
   assertFalse(
       "File in test directory still exists after call to deleteDirectory", file1.exists());
   assertFalse(
       "Test directory still exists after call to deleteDirectory", testDirectory.exists());
 }
Esempio n. 4
0
  private void createTestProject() {
    File directory = new File(Constants.DEFAULT_ROOT + "/" + testProject);
    if (directory.exists()) {
      UtilFile.deleteDirectory(directory);
    }
    assertFalse("testProject was not deleted!", directory.exists());

    uploadProject = new Project(getActivity(), testProject);
    ProjectManager.getInstance().setProject(uploadProject);
    StorageHandler.getInstance().saveProject(uploadProject);

    File file =
        new File(Constants.DEFAULT_ROOT + "/" + testProject + "/" + Constants.PROJECTCODE_NAME);
    assertTrue(testProject + " was not created!", file.exists());
  }
Esempio n. 5
0
  @Override
  protected void setUp() throws Exception {
    super.setUp();

    UtilFile.deleteDirectory(new File(CATROID_DIRECTORY + "/testDirectory"));
    TestUtils.deleteTestProjects(projectName);

    testDirectory = new File(CATROID_DIRECTORY + "/testDirectory");
    testDirectory.mkdir();
    file1 = new File(testDirectory.getAbsolutePath() + "/file1");
    file1.createNewFile();
    subDirectory = new File(testDirectory.getAbsolutePath() + "/subDirectory");
    subDirectory.mkdir();
    file2 = new File(subDirectory.getAbsolutePath() + "/file2");
    file2.createNewFile();
  }
Esempio n. 6
0
  public void testGetProjectNames() {
    Project project = new Project(null, projectName);
    ProjectManager.getInstance().setProject(project);
    Sprite sprite = new Sprite("new sprite");
    project.addSprite(sprite);
    StorageHandler.getInstance().saveProject(project);

    File catroidDirectoryFile = new File(CATROID_DIRECTORY);
    File project1Directory = new File(catroidDirectoryFile + "/" + projectName);

    List<String> projectList = UtilFile.getProjectNames(catroidDirectoryFile);

    assertTrue(
        "project1 should be in Projectlist - is a valid Catroid project",
        projectList.contains(project1Directory.getName()));
    assertFalse(
        "testDirectory should not be in Projectlist - not a Catroid project",
        projectList.contains(testDirectory.getName()));
  }
Esempio n. 7
0
  public void testFileSize() throws IOException {
    for (int i = 0; i < 2; i++) {
      UtilFile.saveFileToProject(
          "testDirectory",
          i + "testsound.mp3",
          org.catrobat.catroid.test.R.raw.longtestsound,
          getInstrumentation().getContext(),
          UtilFile.FileType.TYPE_SOUND_FILE);
    }

    double expectedSizeInKilobytes = 84.2;
    assertEquals(
        "Unexpected file size String",
        String.format("%.1f KB", expectedSizeInKilobytes),
        UtilFile.getSizeAsString(testDirectory));

    for (int i = 2; i < 48; i++) {
      UtilFile.saveFileToProject(
          "testDirectory",
          i + "testsound.mp3",
          org.catrobat.catroid.test.R.raw.longtestsound,
          getInstrumentation().getContext(),
          UtilFile.FileType.TYPE_SOUND_FILE);
    }
    DecimalFormat decimalFormat = new DecimalFormat("#.0");
    String expected = decimalFormat.format(2.0) + " MB";
    assertEquals("Unexpected file size String", expected, UtilFile.getSizeAsString(testDirectory));

    PrintWriter printWriter = null;

    File testFile = new File(Utils.buildPath(testDirectory.getAbsolutePath(), "catroid.txt"));

    try {
      testFile.createNewFile();

      printWriter = new PrintWriter(testFile);
      printWriter.print("catroid");
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (printWriter != null) {
        printWriter.close();
      }
    }

    assertEquals("Unexpected Filesize!", "7 Byte", UtilFile.getSizeAsString(testFile));

    UtilFile.deleteDirectory(testDirectory);
  }
Esempio n. 8
0
 @Override
 protected void tearDown() throws Exception {
   UtilFile.deleteDirectory(testDirectory);
   TestUtils.deleteTestProjects(projectName);
   super.tearDown();
 }
Esempio n. 9
0
  public void testEncodeAndDecodeSpecialCharsForFileSystem() {
    String projectName1 = ".*\"/:<>?\\|%";
    String projectName1Encoded = UtilFile.encodeSpecialCharsForFileSystem(projectName1);
    assertEquals(
        "String projectName1 encoded not equal!",
        ".%2A%22%2F%3A%3C%3E%3F%5C%7C%25",
        projectName1Encoded);
    assertEquals(
        "String projectName1 decoded not equal!",
        projectName1,
        UtilFile.decodeSpecialCharsForFileSystem(projectName1Encoded));

    String projectName2 = "../*\"/:<>?\\|";
    String projectName2Encoded = UtilFile.encodeSpecialCharsForFileSystem(projectName2);
    assertEquals(
        "String projectName2 encoded not equal!",
        "..%2F%2A%22%2F%3A%3C%3E%3F%5C%7C",
        projectName2Encoded);
    assertEquals(
        "String projectName2 decoded not equal!",
        projectName2,
        UtilFile.decodeSpecialCharsForFileSystem(projectName2Encoded));

    String projectName3 = "./*T?E\"S/T:T<E>S?T\\T\\E|S%";
    String projectName3Encoded = UtilFile.encodeSpecialCharsForFileSystem(projectName3);
    assertEquals(
        "String projectName3 encoded not equal!",
        ".%2F%2AT%3FE%22S%2FT%3AT%3CE%3ES%3FT%5CT%5CE%7CS%25",
        projectName3Encoded);
    assertEquals(
        "String projectName3 decoded not equal!",
        projectName3,
        UtilFile.decodeSpecialCharsForFileSystem(projectName3Encoded));

    String projectName4 = ".";
    String projectName4Encoded = UtilFile.encodeSpecialCharsForFileSystem(projectName4);
    assertEquals("String projectName4 encoded not equal!", "%2E", projectName4Encoded);
    assertEquals(
        "String projectName4 decoded not equal!",
        projectName4,
        UtilFile.decodeSpecialCharsForFileSystem(projectName4Encoded));

    String projectName5 = "..";
    String projectName5Encoded = UtilFile.encodeSpecialCharsForFileSystem(projectName5);
    assertEquals("String projectName5 encoded not equal!", "%2E%2E", projectName5Encoded);
    assertEquals(
        "String projectName5 decoded not equal!",
        projectName5,
        UtilFile.decodeSpecialCharsForFileSystem(projectName5Encoded));

    String projectName6 = "../*T?E\"S/T:%22T<E>S?T\\T\\E|S%äö|üß";
    String projectName6Encoded = UtilFile.encodeSpecialCharsForFileSystem(projectName6);
    assertEquals(
        "String projectName6 encoded not equal!",
        "..%2F%2AT%3FE%22S%2FT%3A%2522T%3CE%3ES%3FT%5CT%5CE%7CS%25äö%7Cüß",
        projectName6Encoded);
    assertEquals(
        "String projectName6 decoded not equal!",
        projectName6,
        UtilFile.decodeSpecialCharsForFileSystem(projectName6Encoded));
  }
Esempio n. 10
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());
  }
Esempio n. 11
0
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
      convertView = View.inflate(context, R.layout.fragment_costume_costumelist_item, null);
    }

    convertView.findViewById(R.id.btn_costume_copy).setTag(position);
    convertView.findViewById(R.id.btn_costume_delete).setTag(position);
    convertView.findViewById(R.id.btn_costume_edit).setTag(position);
    convertView.findViewById(R.id.costume_name).setTag(position);
    convertView.findViewById(R.id.costume_image).setTag(position);

    CostumeData costumeData = costumeDataItems.get(position);

    if (costumeData != null) {
      ImageView costumeImage = (ImageView) convertView.findViewById(R.id.costume_image);
      TextView costumeNameTextField = (TextView) convertView.findViewById(R.id.costume_name);
      TextView costumeResolution = (TextView) convertView.findViewById(R.id.costume_res);
      TextView costumeSize = (TextView) convertView.findViewById(R.id.costume_size);
      Button costumeEditButton = (Button) convertView.findViewById(R.id.btn_costume_edit);
      Button costumeCopyButton = (Button) convertView.findViewById(R.id.btn_costume_copy);
      Button costumeDeleteButton = (Button) convertView.findViewById(R.id.btn_costume_delete);

      costumeImage.setImageBitmap(costumeData.getThumbnailBitmap());
      costumeNameTextField.setText(costumeData.getCostumeName());

      // setting resolution and costume size:
      {
        int[] resolution = costumeData.getResolution();
        costumeResolution.setText(resolution[0] + " x " + resolution[1]);

        // setting size
        if (costumeData.getAbsolutePath() != null) {
          costumeSize.setText(UtilFile.getSizeAsString(new File(costumeData.getAbsolutePath())));
        }
      }

      costumeImage.setOnClickListener(
          new OnClickListener() {
            @Override
            public void onClick(View v) {
              if (onCostumeEditListener != null) {
                onCostumeEditListener.onCostumeEdit(v);
              }
            }
          });

      costumeNameTextField.setOnClickListener(
          new OnClickListener() {
            @Override
            public void onClick(View v) {
              if (onCostumeEditListener != null) {
                onCostumeEditListener.onCostumeRename(v);
              }
            }
          });

      costumeEditButton.setOnClickListener(
          new OnClickListener() {
            @Override
            public void onClick(View v) {
              if (onCostumeEditListener != null) {
                onCostumeEditListener.onCostumeEdit(v);
              }
            }
          });

      costumeCopyButton.setOnClickListener(
          new OnClickListener() {
            @Override
            public void onClick(View v) {
              if (onCostumeEditListener != null) {
                onCostumeEditListener.onCostumeCopy(v);
              }
            }
          });

      costumeDeleteButton.setOnClickListener(
          new OnClickListener() {
            @Override
            public void onClick(View v) {
              if (onCostumeEditListener != null) {
                onCostumeEditListener.onCostumeDelete(v);
              }
            }
          });
    }

    return convertView;
  }