Skip to content

Commit

Permalink
Merge pull request #4021 from Walcho1125/CATROID-509
Browse files Browse the repository at this point in the history
CATROID-509 Upload of someone else´s project without any changes is n…
  • Loading branch information
wslany authored Apr 20, 2021
2 parents 0387044 + 7b994d1 commit 02051e9
Show file tree
Hide file tree
Showing 14 changed files with 356 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2021 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* An additional term exception under section 7 of the GNU Affero
* General Public License, version 3, is available at
* http://developer.catrobat.org/license_additional_term
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.catrobat.catroid.uiespresso.ui.dialog

import android.content.Intent
import androidx.test.core.app.ApplicationProvider
import androidx.test.espresso.Espresso
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.matcher.ViewMatchers
import org.catrobat.catroid.ProjectManager
import org.catrobat.catroid.R
import org.catrobat.catroid.content.Project
import org.catrobat.catroid.content.Scene
import org.catrobat.catroid.content.Script
import org.catrobat.catroid.content.Sprite
import org.catrobat.catroid.content.StartScript
import org.catrobat.catroid.formulaeditor.UserVariable
import org.catrobat.catroid.io.XstreamSerializer
import org.catrobat.catroid.io.asynctask.ProjectSaveTask
import org.catrobat.catroid.ui.ProjectUploadActivity
import org.catrobat.catroid.uiespresso.ui.activity.ProjectUploadRatingDialogTest.ProjectUploadTestActivity
import org.catrobat.catroid.uiespresso.util.rules.BaseActivityTestRule
import org.junit.After
import org.junit.Rule
import org.junit.Test

class ReuploadProjectDialogTest {
@get:Rule
var activityTestRule = BaseActivityTestRule(
ProjectUploadTestActivity::class.java, false, false
)

var dummyProject: Project? = null
var projectName = "reuploadedProject"

fun createDownloadedProject(name: String?) {
dummyProject = Project(
ApplicationProvider.getApplicationContext(),
name
)
val dummyScene = Scene("scene", dummyProject!!)
ProjectManager.getInstance().currentProject = dummyProject
val sprite = Sprite("sprite")
val firstScript: Script = StartScript()
dummyScene.addSprite(sprite)
sprite.addScript(firstScript)
dummyProject!!.addScene(dummyScene)
ProjectSaveTask.task(dummyProject, ApplicationProvider.getApplicationContext())
val intent = Intent()
intent.putExtra(ProjectUploadActivity.PROJECT_DIR, dummyProject!!.directory)
activityTestRule.launchActivity(intent)
}

@After
@Throws(Exception::class)
fun tearDown() {
ProjectManager.getInstance().deleteDownloadedProjectInformation(projectName)
}

@Test
fun showUploadWarningForUnchangedProjectTest() {
ProjectManager.getInstance().deleteDownloadedProjectInformation(projectName)
ProjectManager.getInstance().addNewDownloadedProject(projectName)
createDownloadedProject(projectName)
Espresso.onView(ViewMatchers.withText(R.string.warning))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
Espresso.onView(ViewMatchers.withText(R.string.ok)).perform(ViewActions.click())
}

@Test
fun notShowUploadWarningForChangedProjectTest() {
ProjectManager.getInstance().loadDownloadedProjects()
ProjectManager.getInstance().deleteDownloadedProjectInformation(projectName)
ProjectManager.getInstance().addNewDownloadedProject(projectName)
createDownloadedProject(projectName)
Espresso.onView(ViewMatchers.withText(R.string.warning))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
Espresso.onView(ViewMatchers.withText(R.string.ok)).perform(ViewActions.click())
val currentProject = ProjectManager.getInstance().currentProject
val newScene = Scene("scene", currentProject)
currentProject.addScene(newScene)
XstreamSerializer.getInstance().saveProject(currentProject)
ProjectSaveTask.task(currentProject, ApplicationProvider.getApplicationContext())
val intent = Intent()
intent.putExtra(ProjectUploadActivity.PROJECT_DIR, currentProject.directory)
activityTestRule.launchActivity(intent)
Espresso.onView(ViewMatchers.withText(R.string.main_menu_upload))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
}

@Test
fun notShowUploadWarningForAddedVariableProjectTest() {
ProjectManager.getInstance().loadDownloadedProjects()
ProjectManager.getInstance().deleteDownloadedProjectInformation(projectName)
ProjectManager.getInstance().addNewDownloadedProject(projectName)
createDownloadedProject(projectName)
Espresso.onView(ViewMatchers.withText(R.string.warning))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
Espresso.onView(ViewMatchers.withText(R.string.ok)).perform(ViewActions.click())
val currentProject = ProjectManager.getInstance().currentProject
val userVariable = UserVariable("uservariable")
currentProject.addUserVariable(userVariable)
XstreamSerializer.getInstance().saveProject(currentProject)
ProjectSaveTask.task(currentProject, ApplicationProvider.getApplicationContext())
val intent = Intent()
intent.putExtra(ProjectUploadActivity.PROJECT_DIR, currentProject.directory)
activityTestRule.launchActivity(intent)
Espresso.onView(ViewMatchers.withText(R.string.main_menu_upload))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
}
}
82 changes: 82 additions & 0 deletions catroid/src/main/java/org/catrobat/catroid/ProjectManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@
package org.catrobat.catroid;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.catrobat.catroid.common.DefaultProjectHandler;
import org.catrobat.catroid.common.LookData;
import org.catrobat.catroid.common.ScreenModes;
Expand Down Expand Up @@ -58,7 +63,9 @@

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

Expand All @@ -77,6 +84,8 @@ public final class ProjectManager {
private Scene currentlyPlayingScene;
private Scene startScene;
private Sprite currentSprite;
private HashMap<String, Boolean> downloadedProjects;
private final String downloadedProjectsName = "downloaded_projects";

private Context applicationContext;

Expand All @@ -88,6 +97,7 @@ public ProjectManager(Context applicationContext) {
this.applicationContext = applicationContext;
if (instance == null) {
instance = this;
loadDownloadedProjects();
}
}

Expand Down Expand Up @@ -556,4 +566,76 @@ public void setCurrentlyEditedScene(Scene scene) {
currentlyEditedScene = scene;
currentlyPlayingScene = scene;
}

public void addNewDownloadedProject(String projectName) {
Boolean flag = downloadedProjects.get(projectName);
if (flag == null || flag) {
downloadedProjects.put(projectName, false);
saveDownloadedProjects();
}
}

public void resetChangedFlag(Project project) {
String projectName = project.getName();
Boolean isChanged = downloadedProjects.get(projectName);
if (isChanged != null && isChanged) {
downloadedProjects.put(projectName, false);
saveDownloadedProjects();
}
}

public boolean isChangedProject(Project project) {
String projectName = project.getName();
Boolean isChanged = downloadedProjects.get(projectName);
if (isChanged == null) {
return true;
}
return isChanged;
}

public void deleteDownloadedProjectInformation(String projectName) {
downloadedProjects.remove(projectName);
}

public void changedProject(String projectName) {
Boolean isChanged = downloadedProjects.get(projectName);
if (isChanged != null && !isChanged) {
downloadedProjects.put(projectName, true);
saveDownloadedProjects();
}
}

public void saveDownloadedProjects() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
SharedPreferences.Editor editor = sharedPreferences.edit();
Gson gson = new Gson();
String json = gson.toJson(downloadedProjects);
editor.putString(downloadedProjectsName, json);
editor.apply();
}

public void loadDownloadedProjects() {
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(applicationContext);
Gson gson = new Gson();
String json = null;
if (sharedPreferences != null) {
json = sharedPreferences.getString(downloadedProjectsName, null);
if (json != null) {
Type type = new TypeToken<HashMap<String, Boolean>>() {
}.getType();
downloadedProjects = gson.fromJson(json, type);
} else {
downloadedProjects = new HashMap<>();
}
}
}

public void moveChangedFlag(String source, String destination) {
Boolean value = downloadedProjects.get(source);
if (null != value) {
downloadedProjects.remove(source);
downloadedProjects.put(destination, true);
saveDownloadedProjects();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;

import org.catrobat.catroid.BuildConfig;
import org.catrobat.catroid.ProjectManager;
import org.catrobat.catroid.common.LookData;
import org.catrobat.catroid.common.NfcTagData;
import org.catrobat.catroid.common.ProjectData;
Expand Down Expand Up @@ -279,8 +280,11 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Objects;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import androidx.annotation.VisibleForTesting;

Expand Down Expand Up @@ -740,6 +744,20 @@ private static void setFileReferences(Project project) {
}
}
}
private boolean unnecessaryChanges(String currentXml, String previousXml) {
String formulaYRegex = "<formula category=\".*Y.*\">";
String formulaXRegex = "<formula category=\".*X.*\">";
Pattern formulaYPattern = Pattern.compile(formulaYRegex, Pattern.CASE_INSENSITIVE);
Pattern formulaXPattern = Pattern.compile(formulaXRegex, Pattern.CASE_INSENSITIVE);
Matcher currentFormulaYMatcher = formulaYPattern.matcher(currentXml);
Matcher previousFormulaXMatcher = formulaXPattern.matcher(previousXml);
currentFormulaYMatcher.find();
previousFormulaXMatcher.find();
if (previousFormulaXMatcher.matches() && currentFormulaYMatcher.matches() && (currentXml.indexOf(previousFormulaXMatcher.group(0)) == previousXml.indexOf(currentFormulaYMatcher.group(0)))) {
return true;
}
return false;
}

public boolean saveProject(Project project) {
if (project == null) {
Expand Down Expand Up @@ -771,6 +789,17 @@ public boolean saveProject(Project project) {
if (previousXml.equals(currentXml)) {
Log.d(TAG, "Project version is the same. Do not update " + currentCodeFile.getName());
return false;
} else {
String languageRegex = "<catrobatLanguageVersion>.*</catrobatLanguageVersion>";
Pattern languagePattern = Pattern.compile(languageRegex, Pattern.CASE_INSENSITIVE);
Matcher currentLanguageMatcher = languagePattern.matcher(currentXml);
Matcher previousLanguageMatcher = languagePattern.matcher(previousXml);
currentLanguageMatcher.find();
previousLanguageMatcher.find();
if (Objects.equals(currentLanguageMatcher.group(0),
previousLanguageMatcher.group(0)) && (!unnecessaryChanges(currentXml, previousXml))) {
ProjectManager.getInstance().changedProject(project.getName());
}
}
} catch (Exception e) {
Log.e(TAG, "Opening project at " + currentCodeFile.getAbsolutePath() + " failed.", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2018 The Catrobat Team
* Copyright (C) 2010-2021 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -26,6 +26,7 @@
import android.os.AsyncTask;
import android.util.Log;

import org.catrobat.catroid.ProjectManager;
import org.catrobat.catroid.io.StorageOperations;
import org.catrobat.catroid.io.XstreamSerializer;
import org.catrobat.catroid.utils.FileMetaDataExtractor;
Expand Down Expand Up @@ -61,6 +62,7 @@ public static boolean task(File srcDir, String dstName) {
try {
StorageOperations.copyDir(srcDir, dstDir);
XstreamSerializer.renameProject(new File(dstDir, CODE_XML_FILE_NAME), dstName);
ProjectManager.getInstance().addNewDownloadedProject(dstName);
return true;
} catch (IOException e) {
Log.e(TAG, "Something went wrong while copying " + srcDir.getAbsolutePath() + " to " + dstName, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2018 The Catrobat Team
* Copyright (C) 2010-2021 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand All @@ -26,6 +26,7 @@
import android.os.AsyncTask;
import android.util.Log;

import org.catrobat.catroid.ProjectManager;
import org.catrobat.catroid.io.XstreamSerializer;
import org.catrobat.catroid.utils.FileMetaDataExtractor;

Expand Down Expand Up @@ -59,6 +60,7 @@ public static File task(File projectDir, String dstName) throws IOException {

if (projectDir.renameTo(dstDir)
&& XstreamSerializer.renameProject(new File(dstDir, CODE_XML_FILE_NAME), dstName)) {
ProjectManager.getInstance().moveChangedFlag(projectDir.getName(), dstName);
return dstDir;
} else {
throw new IOException("Cannot rename project directory " + projectDir.getAbsolutePath() + " to " + dstName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Catroid: An on-device visual programming system for Android devices
* Copyright (C) 2010-2019 The Catrobat Team
* Copyright (C) 2010-2021 The Catrobat Team
* (<http://developer.catrobat.org/credits>)
*
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -30,6 +30,7 @@ import android.content.Intent
import android.os.Bundle
import android.os.ResultReceiver
import android.util.Log
import org.catrobat.catroid.ProjectManager
import org.catrobat.catroid.R
import org.catrobat.catroid.common.Constants
import org.catrobat.catroid.common.Constants.CACHE_DIR
Expand Down Expand Up @@ -139,6 +140,7 @@ class ProjectDownloadService : IntentService("ProjectDownloadService") {
ZipArchiver().unzip(destinationFile, projectDir)

XstreamSerializer.renameProject(File(projectDir, Constants.CODE_XML_FILE_NAME), projectName)
ProjectManager.getInstance().addNewDownloadedProject(projectName)

val downloadIntent = Intent(context, MainMenuActivity::class.java)
downloadIntent.setAction(Intent.ACTION_MAIN)
Expand Down
Loading

0 comments on commit 02051e9

Please sign in to comment.