Skip to content

Commit 02c3ed9

Browse files
authored
Merge pull request #4126 from dandrawes/CATROID-1043
CATROID-1043 CRITICAL/HOTFIX-REQUIRED: Severe loss of all edits after renaming project via "Project properties" from object overview interface
2 parents 94eefed + bd4606a commit 02c3ed9

File tree

2 files changed

+97
-3
lines changed

2 files changed

+97
-3
lines changed

catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/ProjectOptionsTest.java

+93-3
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,26 @@
2323

2424
package org.catrobat.catroid.uiespresso.ui.fragment;
2525

26+
import android.app.Activity;
27+
import android.app.Instrumentation;
28+
import android.content.Intent;
29+
import android.net.Uri;
30+
import android.os.Environment;
2631
import android.widget.EditText;
2732

2833
import org.catrobat.catroid.ProjectManager;
2934
import org.catrobat.catroid.R;
35+
import org.catrobat.catroid.common.Constants;
3036
import org.catrobat.catroid.common.ScreenModes;
3137
import org.catrobat.catroid.content.Project;
38+
import org.catrobat.catroid.io.ResourceImporter;
3239
import org.catrobat.catroid.io.StorageOperations;
3340
import org.catrobat.catroid.io.asynctask.ProjectSaveTask;
3441
import org.catrobat.catroid.ui.ProjectActivity;
3542
import org.catrobat.catroid.uiespresso.util.UiTestUtils;
3643
import org.catrobat.catroid.uiespresso.util.rules.FragmentActivityTestRule;
44+
import org.hamcrest.Matcher;
45+
import org.hamcrest.core.AllOf;
3746
import org.junit.After;
3847
import org.junit.Assert;
3948
import org.junit.Before;
@@ -49,13 +58,21 @@
4958

5059
import androidx.test.core.app.ApplicationProvider;
5160
import androidx.test.espresso.action.ViewActions;
61+
import androidx.test.espresso.intent.Intents;
5262
import androidx.test.ext.junit.runners.AndroidJUnit4;
63+
import androidx.test.platform.app.InstrumentationRegistry;
5364

65+
import static org.catrobat.catroid.R.id.tab_layout;
5466
import static org.catrobat.catroid.common.Constants.CATROBAT_EXTENSION;
55-
import static org.catrobat.catroid.common.FlavoredConstants.DEFAULT_ROOT_DIRECTORY;
5667
import static org.catrobat.catroid.common.Constants.EXTERNAL_STORAGE_ROOT_EXPORT_DIRECTORY;
68+
import static org.catrobat.catroid.common.FlavoredConstants.DEFAULT_ROOT_DIRECTORY;
69+
import static org.catrobat.catroid.uiespresso.util.actions.TabActionsKt.selectTabAtPosition;
70+
import static org.catrobat.catroid.uiespresso.util.matchers.BundleMatchers.bundleHasExtraIntent;
71+
import static org.catrobat.catroid.uiespresso.util.matchers.BundleMatchers.bundleHasMatchingString;
5772
import static org.hamcrest.Matchers.allOf;
5873
import static org.hamcrest.Matchers.anyOf;
74+
import static org.hamcrest.Matchers.equalTo;
75+
import static org.hamcrest.Matchers.hasItem;
5976
import static org.hamcrest.Matchers.instanceOf;
6077

6178
import static androidx.test.espresso.Espresso.closeSoftKeyboard;
@@ -66,6 +83,13 @@
6683
import static androidx.test.espresso.action.ViewActions.replaceText;
6784
import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
6885
import static androidx.test.espresso.assertion.ViewAssertions.matches;
86+
import static androidx.test.espresso.intent.Intents.intended;
87+
import static androidx.test.espresso.intent.Intents.intending;
88+
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasAction;
89+
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasCategories;
90+
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent;
91+
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasExtras;
92+
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasType;
6993
import static androidx.test.espresso.matcher.RootMatchers.isDialog;
7094
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
7195
import static androidx.test.espresso.matcher.ViewMatchers.isNotChecked;
@@ -85,7 +109,7 @@ public class ProjectOptionsTest {
85109
@Rule
86110
public FragmentActivityTestRule<ProjectActivity> baseActivityTestRule =
87111
new FragmentActivityTestRule<>(ProjectActivity.class, ProjectActivity.EXTRA_FRAGMENT_POSITION,
88-
ProjectActivity.FRAGMENT_SPRITES);
112+
ProjectActivity.FRAGMENT_SPRITES);
89113

90114
@Before
91115
public void setUp() throws Exception {
@@ -108,7 +132,7 @@ public void tearDown() {
108132
}
109133

110134
@Test
111-
public void changeProjectName() {
135+
public void changeProjectName() throws IOException {
112136
onView(allOf(withText(PROJECT_NAME), isDisplayed(), instanceOf(EditText.class)))
113137
.perform(replaceText(EXISTING_PROJECT_NAME));
114138

@@ -131,6 +155,72 @@ public void changeProjectName() {
131155
onView(withText(NEW_PROJECT_NAME))
132156
.check(matches(isDisplayed()));
133157
Assert.assertEquals(NEW_PROJECT_NAME, project.getName());
158+
159+
onView(withText(R.string.default_project_background_name))
160+
.perform(click());
161+
162+
onView(withId(tab_layout)).perform(selectTabAtPosition(1));
163+
164+
onView(withId(R.id.button_add))
165+
.perform(click());
166+
167+
Matcher<Intent> expectedPaintNewLookIntent = createLookFromPaintroid();
168+
169+
onView(withId(R.id.dialog_new_look_paintroid))
170+
.perform(click());
171+
172+
intended(expectedPaintNewLookIntent);
173+
174+
onView(withText("Background (1)"))
175+
.check(matches(isDisplayed()));
176+
}
177+
178+
private Matcher<Intent> createLookFromPaintroid() throws IOException {
179+
File tmpDir = new File(
180+
Environment.getExternalStorageDirectory().getAbsolutePath(), "Pocket Code Test Temp");
181+
String lookFileName = "catroid_sunglasses.png";
182+
183+
Intents.init();
184+
185+
Matcher<Intent> expectedGetContentIntent = AllOf.allOf(
186+
hasAction("android.intent.action.GET_CONTENT"),
187+
hasType("image/*"));
188+
189+
String chooserTitle = UiTestUtils.getResourcesString(R.string.select_look_from_gallery);
190+
Matcher<Intent> expectedChooserIntent = AllOf.allOf(
191+
hasAction("android.intent.action.CHOOSER"),
192+
hasExtras(bundleHasMatchingString("android.intent.extra.TITLE", chooserTitle)),
193+
hasExtras(bundleHasExtraIntent(expectedGetContentIntent)));
194+
195+
if (!tmpDir.exists()) {
196+
tmpDir.mkdirs();
197+
}
198+
199+
File imageFile = ResourceImporter.createImageFileFromResourcesInDirectory(
200+
InstrumentationRegistry.getInstrumentation().getContext().getResources(),
201+
org.catrobat.catroid.test.R.drawable.catroid_banzai,
202+
tmpDir,
203+
lookFileName,
204+
1);
205+
206+
Intent resultData = new Intent();
207+
resultData.setData(Uri.fromFile(imageFile));
208+
209+
Instrumentation.ActivityResult result =
210+
new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);
211+
212+
intending(expectedChooserIntent).respondWith(result);
213+
214+
Matcher<Intent> expectedPaintNewLookIntent = AllOf.allOf(
215+
hasComponent(Constants.POCKET_PAINT_INTENT_ACTIVITY_NAME),
216+
hasAction("android.intent.action.MAIN"),
217+
hasCategories(hasItem(equalTo("android.intent.category.LAUNCHER"))));
218+
219+
Instrumentation.ActivityResult resultPaintroid = new Instrumentation.ActivityResult(Activity.RESULT_OK, null);
220+
221+
intending(expectedPaintNewLookIntent).respondWith(resultPaintroid);
222+
223+
return expectedPaintNewLookIntent;
134224
}
135225

136226
@Test

catroid/src/main/java/org/catrobat/catroid/ui/fragment/ProjectOptionsFragment.java

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ public class ProjectOptionsFragment extends Fragment implements
8181
private View view;
8282

8383
private Project project;
84+
private String sceneName;
8485

8586
private TextInputLayout nameInputLayout;
8687
private TextInputLayout descriptionInputLayout;
@@ -102,6 +103,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
102103
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(R.string.project_options);
103104

104105
project = ProjectManager.getInstance().getCurrentProject();
106+
sceneName = ProjectManager.getInstance().getCurrentlyEditedScene().getName();
105107

106108
nameInputLayout = view.findViewById(R.id.project_options_name_layout);
107109
nameInputLayout.getEditText().setText(project.getName());
@@ -233,9 +235,11 @@ private void setProjectName() {
233235

234236
if (!project.getName().equals(name)) {
235237
try {
238+
XstreamSerializer.getInstance().saveProject(project);
236239
File renamedDirectory = ProjectRenameTask.task(project.getDirectory(), name);
237240
ProjectLoadTask.task(renamedDirectory, getActivity().getApplicationContext());
238241
project = ProjectManager.getInstance().getCurrentProject();
242+
ProjectManager.getInstance().setCurrentlyEditedScene(project.getSceneByName(sceneName));
239243
} catch (IOException e) {
240244
Log.e(TAG, "Creating renamed directory failed!", e);
241245
}

0 commit comments

Comments
 (0)