private void copyImageToCatroid(String originalImagePath) { int[] imageDimensions = ImageEditing.getImageDimensions(originalImagePath); if (imageDimensions[0] < 0 || imageDimensions[1] < 0) { Utils.showErrorDialog(getActivity(), getString(R.string.error_load_image)); return; } File oldFile = new File(originalImagePath); try { if (originalImagePath.equals("")) { throw new IOException(); } String projectName = ProjectManager.getInstance().getCurrentProject().getName(); File imageFile = StorageHandler.getInstance().copyImage(projectName, originalImagePath, null); String imageName; int extensionDotIndex = oldFile.getName().lastIndexOf('.'); if (extensionDotIndex > 0) { imageName = oldFile.getName().substring(0, extensionDotIndex); } else { imageName = oldFile.getName(); } String imageFileName = imageFile.getName(); // if pixmap cannot be created, image would throw an Exception in stage // so has to be loaded again with other Config Pixmap pixmap = null; pixmap = Utils.getPixmapFromFile(imageFile); if (pixmap == null) { ImageEditing.overwriteImageFileWithNewBitmap(imageFile); pixmap = Utils.getPixmapFromFile(imageFile); if (pixmap == null) { Utils.showErrorDialog(getActivity(), getString(R.string.error_load_image)); StorageHandler.getInstance().deleteFile(imageFile.getAbsolutePath()); return; } } pixmap = null; updateLookAdapter(imageName, imageFileName); } catch (IOException e) { Utils.showErrorDialog(getActivity(), getString(R.string.error_load_image)); } getLoaderManager().destroyLoader(ID_LOADER_MEDIA_IMAGE); getActivity().sendBroadcast(new Intent(ScriptActivity.ACTION_BRICK_LIST_CHANGED)); }
@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(); }
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); }
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); }
public void testContainer() throws IOException, InterruptedException { storageHandler.copyImage(currentProjectName, testImage.getAbsolutePath(), null); String checksumImage = Utils.md5Checksum(testImage); FileChecksumContainer fileChecksumContainer = projectManager.getFileChecksumContainer(); assertTrue( "Checksum isn't in container", fileChecksumContainer.containsChecksum(checksumImage)); // wait to get a different timestamp on next file Thread.sleep(2000); File newTestImage = storageHandler.copyImage(currentProjectName, testImage.getAbsolutePath(), null); File imageDirectory = new File( Constants.DEFAULT_ROOT + "/" + currentProjectName + "/" + Constants.IMAGE_DIRECTORY + "/"); File[] filesImage = imageDirectory.listFiles(); // nomedia file is also in images folder assertEquals("Wrong amount of files in folder", 2, filesImage.length); File newTestSound = storageHandler.copySoundFile(testSound.getAbsolutePath()); String checksumSound = Utils.md5Checksum(testSound); assertTrue( "Checksum isn't in container", fileChecksumContainer.containsChecksum(checksumSound)); File soundDirectory = new File( Constants.DEFAULT_ROOT + "/" + currentProjectName + "/" + Constants.SOUND_DIRECTORY); File[] filesSound = soundDirectory.listFiles(); // nomedia file is also in sounds folder assertEquals("Wrong amount of files in folder", 2, filesSound.length); fileChecksumContainer.decrementUsage(newTestImage.getAbsolutePath()); assertTrue("Checksum was deleted", fileChecksumContainer.containsChecksum(checksumImage)); fileChecksumContainer.decrementUsage(newTestImage.getAbsolutePath()); assertFalse("Checksum wasn't deleted", fileChecksumContainer.containsChecksum(checksumImage)); fileChecksumContainer.decrementUsage(newTestSound.getAbsolutePath()); assertFalse("Checksum wasn't deleted", fileChecksumContainer.containsChecksum(checksumSound)); }
private void handleDeleteSound(int position) { ArrayList<SoundInfo> soundInfoList = ProjectManager.getInstance().getCurrentSprite().getSoundList(); StorageHandler.getInstance().deleteFile(soundInfoList.get(position).getAbsolutePath()); soundInfoList.remove(position); getActivity().sendBroadcast(new Intent(ScriptTabActivity.ACTION_SOUND_DELETED)); }
private void deleteLook(int position) { StorageHandler.getInstance().deleteFile(lookDataList.get(position).getAbsolutePath()); lookDataList.remove(position); ProjectManager.getInstance().getCurrentSprite().setLookDataList(lookDataList); getActivity().sendBroadcast(new Intent(ScriptActivity.ACTION_LOOK_DELETED)); }
private void loadPocketPaintImageIntoCatroid(Intent intent) { Bundle bundle = intent.getExtras(); String pathOfPocketPaintImage = bundle.getString(Constants.EXTRA_PICTURE_PATH_POCKET_PAINT); int[] imageDimensions = ImageEditing.getImageDimensions(pathOfPocketPaintImage); if (imageDimensions[0] < 0 || imageDimensions[1] < 0) { Utils.showErrorDialog(getActivity(), this.getString(R.string.error_load_image)); return; } String actualChecksum = Utils.md5Checksum(new File(pathOfPocketPaintImage)); // If look changed --> saving new image with new checksum and changing lookData if (!selectedLookData.getChecksum().equalsIgnoreCase(actualChecksum)) { String oldFileName = selectedLookData.getLookFileName(); String newFileName = oldFileName.substring(oldFileName.indexOf('_') + 1); // HACK for https://github.com/Catrobat/Catroid/issues/81 if (!newFileName.endsWith(".png")) { newFileName = newFileName + ".png"; } String projectName = ProjectManager.getInstance().getCurrentProject().getName(); try { File newLookFile = StorageHandler.getInstance() .copyImage(projectName, pathOfPocketPaintImage, newFileName); File temporaryPictureFileInPocketPaint = new File(pathOfPocketPaintImage); temporaryPictureFileInPocketPaint.delete(); // delete temp file in paintroid StorageHandler.getInstance() .deleteFile( selectedLookData.getAbsolutePath()); // reduce usage in container or delete it selectedLookData.setLookFilename(newLookFile.getName()); selectedLookData.resetThumbnailBitmap(); } catch (IOException e) { e.printStackTrace(); } } }
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); } }
public void testDeleteFile() throws IOException, InterruptedException { File newTestImage1 = storageHandler.copyImage(currentProjectName, testImage.getAbsolutePath(), null); // wait to get a different timestamp on next file Thread.sleep(2000); storageHandler.deleteFile(newTestImage1.getAbsolutePath()); File imageDirectory = new File( Constants.DEFAULT_ROOT + "/" + currentProjectName + "/" + Constants.IMAGE_DIRECTORY); File[] filesImage = imageDirectory.listFiles(); assertEquals("Wrong amount of files in folder", 1, filesImage.length); File newTestSound = storageHandler.copySoundFile(testSound.getAbsolutePath()); storageHandler.deleteFile(newTestSound.getAbsolutePath()); File soundDirectory = new File( Constants.DEFAULT_ROOT + "/" + currentProjectName + "/" + Constants.SOUND_DIRECTORY); File[] filesSound = soundDirectory.listFiles(); assertEquals("Wrong amount of files in folder", 1, filesSound.length); }
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()); }
private void copyLook(int position) { LookData lookData = lookDataList.get(position); try { String projectName = ProjectManager.getInstance().getCurrentProject().getName(); StorageHandler.getInstance().copyImage(projectName, lookData.getAbsolutePath(), null); String imageName = lookData.getLookName() + "_" + getString(R.string.copy_look_addition); String imageFileName = lookData.getLookFileName(); updateLookAdapter(imageName, imageFileName); } catch (IOException e) { Utils.showErrorDialog(getActivity(), getString(R.string.error_load_image)); e.printStackTrace(); } getActivity().sendBroadcast(new Intent(ScriptActivity.ACTION_BRICK_LIST_CHANGED)); }
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())); }
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)); } }
public SoundInfo copySoundInfoForSprite(Sprite sprite) { SoundInfo cloneSoundInfo = new SoundInfo(); cloneSoundInfo.name = this.name; try { cloneSoundInfo.fileName = StorageHandler.getInstance() .copySoundFile( Utils.buildPath( Utils.buildProjectPath( ProjectManager.getInstance().getCurrentProject().getName()), Constants.SOUND_DIRECTORY, fileName)) .getName(); } catch (IOException ioException) { Log.e(TAG, Log.getStackTraceString(ioException)); } return cloneSoundInfo; }
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); }
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); }
private boolean handleOkButton() { EditText editText = (EditText) dialogView.findViewById(R.id.dialog_new_object_name_edit_text); String newSpriteName; if (editText.length() == 0) { newSpriteName = editText.getHint().toString(); } else { newSpriteName = editText.getText().toString().trim(); } if (newSpriteName.contains(".")) { int fileExtensionPosition = newSpriteName.indexOf("."); newSpriteName = newSpriteName.substring(0, fileExtensionPosition); } ProjectManager projectManager = ProjectManager.getInstance(); if (newSpriteName.equalsIgnoreCase("")) { Utils.showErrorDialog(getActivity(), R.string.spritename_invalid); return false; } if (projectManager.spriteExists(newSpriteName)) { Utils.showErrorDialog(getActivity(), R.string.spritename_already_exists); return false; } Sprite sprite = new Sprite(newSpriteName); projectManager.addSprite(sprite); LookData lookData; try { File newLookFile = StorageHandler.getInstance() .copyImage(projectManager.getCurrentProject().getName(), lookUri.getPath(), null); String imageFileName = newLookFile.getName(); Utils.rewriteImageFileForStage(getActivity(), newLookFile); lookData = new LookData(); lookData.setLookFilename(imageFileName); lookData.setLookName(newSpriteName); } catch (IOException exception) { Utils.showErrorDialog(getActivity(), R.string.error_load_image); exception.printStackTrace(); return false; } sprite.getLookDataList().add(lookData); if (requestedAction == ActionAfterFinished.ACTION_UPDATE_SPINNER && spinnerAdapter != null) { Intent broadcastIntent; broadcastIntent = new Intent(ScriptActivity.ACTION_BRICK_LIST_CHANGED); getActivity().sendBroadcast(broadcastIntent); spinnerAdapter.refreshSpinnerAfterNewSprite(getActivity(), newSpriteName); } else { Intent broadcastIntent; broadcastIntent = new Intent(ScriptActivity.ACTION_SPRITES_LIST_CHANGED); getActivity().sendBroadcast(broadcastIntent); } if (requestedAction == ActionAfterFinished.ACTION_FORWARD_TO_NEW_OBJECT) { projectManager.setCurrentSprite(sprite); Intent intent = new Intent(getActivity(), ProgramMenuActivity.class); intent.putExtra( ProgramMenuActivity.FORWARD_TO_SCRIPT_ACTIVITY, ScriptActivity.FRAGMENT_SCRIPTS); startActivity(intent); } dismiss(); return true; }
@Override protected void onHandleIntent(Intent intent) { StorageHandler.getInstance().saveProject(ProjectManager.getInstance().getCurrentProject()); receiver = (ResultReceiver) intent.getParcelableExtra("receiver"); try { if (projectPath == null) { result = false; Log.e(TAG, "project path is null"); return; } File directoryPath = new File(projectPath); String[] paths = directoryPath.list(); if (paths == null) { result = false; Log.e(TAG, "project path is not valid"); return; } for (int i = 0; i < paths.length; i++) { paths[i] = Utils.buildPath(directoryPath.getAbsolutePath(), paths[i]); } String zipFileString = Utils.buildPath(Constants.TMP_PATH, UPLOAD_FILE_NAME); File zipFile = new File(zipFileString); if (!zipFile.exists()) { zipFile.getParentFile().mkdirs(); zipFile.createNewFile(); } if (!UtilZip.writeToZipFile(paths, zipFileString)) { zipFile.delete(); result = false; return; } // String deviceIMEI = UtilDeviceInfo.getDeviceIMEI(context); String userEmail = UtilDeviceInfo.getUserEmail(this); String language = UtilDeviceInfo.getUserLanguageCode(this); Context context = getApplicationContext(); ServerCalls.getInstance() .uploadProject( projectName, projectDescription, zipFileString, userEmail, language, token, username, receiver, notificationId, context); zipFile.delete(); } catch (IOException e) { e.printStackTrace(); result = false; } catch (WebconnectionException webException) { serverAnswer = webException.getMessage(); Log.e(TAG, serverAnswer); result = false; } }
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); }
public StorageHandlerTest() throws IOException { storageHandler = StorageHandler.getInstance(); }
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()); }