diff --git a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/content/brick/utils/BrickTestUtils.java b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/content/brick/utils/BrickTestUtils.java index 84642c52be8..96735c0f081 100644 --- a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/content/brick/utils/BrickTestUtils.java +++ b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/content/brick/utils/BrickTestUtils.java @@ -31,20 +31,23 @@ import androidx.test.core.app.ApplicationProvider; +import static org.koin.java.KoinJavaComponent.inject; + public final class BrickTestUtils { private BrickTestUtils() { throw new AssertionError(); } public static Script createEmptyCastProjectAndGetStartScript(String projectName) { + ProjectManager projectManager = inject(ProjectManager.class).getValue(); Project project = new Project(ApplicationProvider.getApplicationContext(), projectName, false, true); Sprite sprite = new Sprite("testSprite"); Script script = new StartScript(); sprite.addScript(script); project.getDefaultScene().addSprite(sprite); - ProjectManager.getInstance().setCurrentProject(project); - ProjectManager.getInstance().setCurrentSprite(sprite); - ProjectManager.getInstance().setCurrentlyEditedScene(project.getDefaultScene()); + projectManager.setCurrentProject(project); + projectManager.setCurrentSprite(sprite); + projectManager.setCurrentlyEditedScene(project.getDefaultScene()); return script; } } diff --git a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/CopyLookTest.java b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/CopyLookTest.java index 5b58b33030a..b70d212f5b8 100644 --- a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/CopyLookTest.java +++ b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/CopyLookTest.java @@ -25,31 +25,29 @@ import org.catrobat.catroid.ProjectManager; import org.catrobat.catroid.R; -import org.catrobat.catroid.common.LookData; import org.catrobat.catroid.content.Project; import org.catrobat.catroid.content.Sprite; -import org.catrobat.catroid.io.ResourceImporter; import org.catrobat.catroid.io.XstreamSerializer; import org.catrobat.catroid.testsuites.annotations.Cat; import org.catrobat.catroid.testsuites.annotations.Level; import org.catrobat.catroid.ui.SpriteActivity; +import org.catrobat.catroid.uiespresso.ui.fragment.actionutils.ActionUtils; import org.catrobat.catroid.uiespresso.util.rules.FragmentActivityTestRule; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; -import java.io.File; import java.io.IOException; -import java.util.List; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.platform.app.InstrumentationRegistry; -import static org.catrobat.catroid.common.Constants.IMAGE_DIRECTORY_NAME; import static org.catrobat.catroid.uiespresso.ui.fragment.rvutils.RecyclerViewInteractionWrapper.onRecyclerView; +import static org.koin.java.KoinJavaComponent.inject; import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu; @@ -62,34 +60,56 @@ @RunWith(AndroidJUnit4.class) public class CopyLookTest { + final ProjectManager projectManager = inject(ProjectManager.class).getValue(); + @Rule public FragmentActivityTestRule baseActivityTestRule = new FragmentActivityTestRule<>(SpriteActivity.class, SpriteActivity.EXTRA_FRAGMENT_POSITION, SpriteActivity.FRAGMENT_LOOKS); - private String toBeCopiedLookName = "testLook"; + private String testLookName1 = "testLook"; + private String testLookName2 = "testLook2"; + private String toBeCopiedLookName1 = "testLook (1)"; @Before - public void setUp() throws Exception { + public void setUp() { createProject(); - baseActivityTestRule.launchActivity(); + } + + @After + public void tearDown() { + baseActivityTestRule.getActivity().finish(); } @Category({Cat.AppUi.class, Level.Smoke.class}) @Test - public void copyLookTest() { + public void copyLookOneElementListTest() throws IOException { + ActionUtils.addLook(projectManager, testLookName1); + baseActivityTestRule.launchActivity(); + openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getInstrumentation().getTargetContext()); onView(withText(R.string.copy)).perform(click()); + onView(withText(toBeCopiedLookName1)) + .check(matches(isDisplayed())); + } + + @Category({Cat.AppUi.class, Level.Smoke.class}) + @Test + public void copyLookMultipleElementsListTest() throws IOException { + ActionUtils.addLook(projectManager, testLookName1); + ActionUtils.addLook(projectManager, testLookName2); + baseActivityTestRule.launchActivity(); + + openActionBarOverflowOrOptionsMenu(ApplicationProvider.getApplicationContext()); + onView(withText(R.string.copy)).perform(click()); + onRecyclerView().atPosition(0) - .performCheckItemClick(); + .performCheckItemClick(); onView(withId(R.id.confirm)).perform(click()); - onView(withText(toBeCopiedLookName)) - .check(matches(isDisplayed())); - - onView(withText(toBeCopiedLookName + " (1)")) + onView(withText(toBeCopiedLookName1)) .check(matches(isDisplayed())); } @@ -103,28 +123,15 @@ public void selectFragmentToCopyTest() { onRecyclerView().atPosition(0).performCheckItemCheck(); } - private void createProject() throws IOException { + private void createProject() { String projectName = "copyLookFragmentTest"; Project project = new Project(ApplicationProvider.getApplicationContext(), projectName); Sprite sprite = new Sprite("testSprite"); project.getDefaultScene().addSprite(sprite); - ProjectManager.getInstance().setCurrentProject(project); - ProjectManager.getInstance().setCurrentSprite(sprite); + projectManager.setCurrentProject(project); + projectManager.setCurrentSprite(sprite); XstreamSerializer.getInstance().saveProject(project); - - File imageFile = ResourceImporter.createImageFileFromResourcesInDirectory( - InstrumentationRegistry.getInstrumentation().getContext().getResources(), - org.catrobat.catroid.test.R.drawable.catroid_banzai, - new File(project.getDefaultScene().getDirectory(), IMAGE_DIRECTORY_NAME), - "catroid_sunglasses.png", - 1); - - List lookDataList = ProjectManager.getInstance().getCurrentSprite().getLookList(); - LookData lookData = new LookData(); - lookData.setFile(imageFile); - lookData.setName(toBeCopiedLookName); - lookDataList.add(lookData); } } diff --git a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/CopySoundTest.java b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/CopySoundTest.java index 4bc0b15fbb9..318951f1836 100644 --- a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/CopySoundTest.java +++ b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/CopySoundTest.java @@ -25,31 +25,29 @@ import org.catrobat.catroid.ProjectManager; import org.catrobat.catroid.R; -import org.catrobat.catroid.common.SoundInfo; import org.catrobat.catroid.content.Project; import org.catrobat.catroid.content.Sprite; -import org.catrobat.catroid.io.ResourceImporter; import org.catrobat.catroid.io.XstreamSerializer; import org.catrobat.catroid.testsuites.annotations.Cat; import org.catrobat.catroid.testsuites.annotations.Level; import org.catrobat.catroid.ui.SpriteActivity; +import org.catrobat.catroid.uiespresso.ui.fragment.actionutils.ActionUtils; import org.catrobat.catroid.uiespresso.util.rules.FragmentActivityTestRule; +import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; -import java.io.File; import java.io.IOException; -import java.util.List; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.platform.app.InstrumentationRegistry; -import static org.catrobat.catroid.common.Constants.SOUND_DIRECTORY_NAME; import static org.catrobat.catroid.uiespresso.ui.fragment.rvutils.RecyclerViewInteractionWrapper.onRecyclerView; +import static org.koin.java.KoinJavaComponent.inject; import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu; @@ -62,22 +60,47 @@ @RunWith(AndroidJUnit4.class) public class CopySoundTest { + final ProjectManager projectManager = inject(ProjectManager.class).getValue(); + @Rule public FragmentActivityTestRule baseActivityTestRule = new FragmentActivityTestRule<>(SpriteActivity.class, SpriteActivity.EXTRA_FRAGMENT_POSITION, SpriteActivity.FRAGMENT_SOUNDS); - private String toBeCopiedSoundName = "testSound"; + private String testSoundName1 = "testSound"; + private String testSoundName2 = "testSound2"; + private String toBeCopiedSoundName1 = "testSound (1)"; @Before - public void setUp() throws Exception { + public void setUp() { createProject(); + } + + @After + public void tearDown() { + baseActivityTestRule.getActivity().finish(); + } + + @Category({Cat.AppUi.class, Level.Smoke.class}) + @Test + public void copyLookOneElementListTest() throws IOException { + ActionUtils.addSound(projectManager, testSoundName1); baseActivityTestRule.launchActivity(); + + openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getInstrumentation().getTargetContext()); + onView(withText(R.string.copy)).perform(click()); + + onView(withText(toBeCopiedSoundName1)) + .check(matches(isDisplayed())); } @Category({Cat.AppUi.class, Level.Smoke.class}) @Test - public void copySoundTest() { + public void copySoundMultipleElementsListTest() throws IOException { + ActionUtils.addSound(projectManager, testSoundName1); + ActionUtils.addSound(projectManager, testSoundName2); + baseActivityTestRule.launchActivity(); + openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getInstrumentation().getTargetContext()); onView(withText(R.string.copy)).perform(click()); @@ -86,10 +109,10 @@ public void copySoundTest() { onView(withId(R.id.confirm)).perform(click()); - onView(withText(toBeCopiedSoundName)) + onView(withText(testSoundName1)) .check(matches(isDisplayed())); - onView(withText(toBeCopiedSoundName + " (1)")) + onView(withText(toBeCopiedSoundName1)) .check(matches(isDisplayed())); } @@ -103,27 +126,15 @@ public void selectFragmentToCopyTest() { onRecyclerView().atPosition(0).performCheckItemCheck(); } - private void createProject() throws IOException { + private void createProject() { String projectName = "copySoundFragmentTest"; Project project = new Project(ApplicationProvider.getApplicationContext(), projectName); Sprite sprite = new Sprite("testSprite"); project.getDefaultScene().addSprite(sprite); - ProjectManager.getInstance().setCurrentProject(project); - ProjectManager.getInstance().setCurrentSprite(sprite); + projectManager.setCurrentProject(project); + projectManager.setCurrentSprite(sprite); XstreamSerializer.getInstance().saveProject(project); - - File soundFile = ResourceImporter.createSoundFileFromResourcesInDirectory( - InstrumentationRegistry.getInstrumentation().getContext().getResources(), - org.catrobat.catroid.test.R.raw.longsound, - new File(project.getDefaultScene().getDirectory(), SOUND_DIRECTORY_NAME), - "longsound.mp3"); - - List soundInfoList = ProjectManager.getInstance().getCurrentSprite().getSoundList(); - SoundInfo soundInfo = new SoundInfo(); - soundInfo.setFile(soundFile); - soundInfo.setName(toBeCopiedSoundName); - soundInfoList.add(soundInfo); } } diff --git a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/DeleteLookTest.java b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/DeleteLookTest.java index acebd2690d1..146ef925eba 100644 --- a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/DeleteLookTest.java +++ b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/DeleteLookTest.java @@ -25,14 +25,13 @@ import org.catrobat.catroid.ProjectManager; import org.catrobat.catroid.R; -import org.catrobat.catroid.common.LookData; import org.catrobat.catroid.content.Project; import org.catrobat.catroid.content.Sprite; -import org.catrobat.catroid.io.ResourceImporter; import org.catrobat.catroid.io.XstreamSerializer; import org.catrobat.catroid.testsuites.annotations.Cat; import org.catrobat.catroid.testsuites.annotations.Level; import org.catrobat.catroid.ui.SpriteActivity; +import org.catrobat.catroid.uiespresso.ui.fragment.actionutils.ActionUtils; import org.catrobat.catroid.uiespresso.util.UiTestUtils; import org.catrobat.catroid.uiespresso.util.rules.FragmentActivityTestRule; import org.junit.Before; @@ -41,17 +40,15 @@ import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; -import java.io.File; import java.io.IOException; -import java.util.List; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.platform.app.InstrumentationRegistry; -import static org.catrobat.catroid.common.Constants.IMAGE_DIRECTORY_NAME; import static org.catrobat.catroid.uiespresso.ui.fragment.rvutils.RecyclerViewInteractionWrapper.onRecyclerView; import static org.hamcrest.Matchers.allOf; +import static org.koin.java.KoinJavaComponent.inject; import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu; @@ -66,22 +63,27 @@ @RunWith(AndroidJUnit4.class) public class DeleteLookTest { + final ProjectManager projectManager = inject(ProjectManager.class).getValue(); + @Rule public FragmentActivityTestRule baseActivityTestRule = new FragmentActivityTestRule<>(SpriteActivity.class, SpriteActivity.EXTRA_FRAGMENT_POSITION, SpriteActivity.FRAGMENT_LOOKS); - private String toBeDeletedLookName = "testLook2"; + private final String toBeDeletedLookName = "testLook2"; @Before - public void setUp() throws Exception { + public void setUp() throws IOException { createProject(); baseActivityTestRule.launchActivity(); } @Category({Cat.AppUi.class, Level.Smoke.class}) @Test - public void deleteLookTest() { + public void deleteLookMultipleElementsListTest() throws IOException { + ActionUtils.addLook(projectManager, "testLook1"); + ActionUtils.addLook(projectManager, toBeDeletedLookName); + openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getInstrumentation().getTargetContext()); onView(withText(R.string.delete)).perform(click()); @@ -107,6 +109,17 @@ public void deleteLookTest() { .check(doesNotExist()); } + @Test + public void deleteLookSingleElementListTest() throws IOException { + ActionUtils.addLook(projectManager, toBeDeletedLookName); + + openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getInstrumentation().getTargetContext()); + onView(withText(R.string.delete)).perform(click()); + + onView(withText(toBeDeletedLookName)) + .check(doesNotExist()); + } + @Category({Cat.AppUi.class, Level.Smoke.class}) @Test public void selectFragmentToDeleteTest() { @@ -119,7 +132,10 @@ public void selectFragmentToDeleteTest() { @Category({Cat.AppUi.class, Level.Smoke.class}) @Test - public void cancelDeleteLookTest() { + public void cancelDeleteLookTest() throws IOException { + ActionUtils.addLook(projectManager, "testLook1"); + ActionUtils.addLook(projectManager, toBeDeletedLookName); + openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getInstrumentation().getTargetContext()); onView(withText(R.string.delete)).perform(click()); @@ -145,40 +161,15 @@ public void cancelDeleteLookTest() { .check(matches(isDisplayed())); } - private void createProject() throws IOException { + private void createProject() { String projectName = "deleteLookFragmentTest"; Project project = new Project(ApplicationProvider.getApplicationContext(), projectName); Sprite sprite = new Sprite("testSprite"); project.getDefaultScene().addSprite(sprite); - ProjectManager.getInstance().setCurrentProject(project); - ProjectManager.getInstance().setCurrentSprite(sprite); + projectManager.setCurrentProject(project); + projectManager.setCurrentSprite(sprite); XstreamSerializer.getInstance().saveProject(project); - - File imageFile = ResourceImporter.createImageFileFromResourcesInDirectory( - InstrumentationRegistry.getInstrumentation().getContext().getResources(), - org.catrobat.catroid.test.R.drawable.catroid_banzai, - new File(project.getDefaultScene().getDirectory(), IMAGE_DIRECTORY_NAME), - "catroid_sunglasses.png", - 1); - - File imageFile2 = ResourceImporter.createImageFileFromResourcesInDirectory( - InstrumentationRegistry.getInstrumentation().getContext().getResources(), - org.catrobat.catroid.test.R.drawable.catroid_banzai, - new File(project.getDefaultScene().getDirectory(), IMAGE_DIRECTORY_NAME), - "catroid_banzai.png", - 1); - - List lookDataList = ProjectManager.getInstance().getCurrentSprite().getLookList(); - LookData lookData = new LookData(); - lookData.setFile(imageFile); - lookData.setName("testLook1"); - lookDataList.add(lookData); - - LookData lookData2 = new LookData(); - lookData2.setFile(imageFile2); - lookData2.setName(toBeDeletedLookName); - lookDataList.add(lookData2); } } diff --git a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/DeleteProjectTest.java b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/DeleteProjectTest.java index 0be37f0f43e..e8f14258497 100644 --- a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/DeleteProjectTest.java +++ b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/DeleteProjectTest.java @@ -72,14 +72,14 @@ public class DeleteProjectTest { @Before public void setUp() throws Exception { createProject(projectToDelete); - createProject("secondProject"); - - baseActivityTestRule.launchActivity(null); } @Category({Cat.AppUi.class, Level.Smoke.class}) @Test - public void deleteProjectTest() { + public void deleteProjectMultipleElementsListTest() { + createProject("secondProject"); + baseActivityTestRule.launchActivity(null); + openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getInstrumentation().getTargetContext()); onView(withText(R.string.delete)).perform(click()); @@ -105,6 +105,21 @@ public void deleteProjectTest() { .check(doesNotExist()); } + @Category({Cat.AppUi.class, Level.Smoke.class}) + @Test + public void deleteProjectSingleElementListTest() { + baseActivityTestRule.launchActivity(null); + + openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getInstrumentation().getTargetContext()); + onView(withText(R.string.delete)).perform(click()); + + onView(withText(projectToDelete)) + .check(doesNotExist()); + + onView(withText("My project")) + .check(matches(isDisplayed())); // default project + } + @Category({Cat.AppUi.class, Level.Smoke.class}) @Test public void selectFragmentToDeleteTest() { @@ -118,6 +133,9 @@ public void selectFragmentToDeleteTest() { @Category({Cat.AppUi.class, Level.Smoke.class}) @Test public void cancelDeleteProjectTest() { + createProject("secondProject"); + baseActivityTestRule.launchActivity(null); + openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getInstrumentation().getTargetContext()); onView(withText(R.string.delete)).perform(click()); diff --git a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/DeleteSoundTest.java b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/DeleteSoundTest.java index 2b90c6dc145..67ad4b8e270 100644 --- a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/DeleteSoundTest.java +++ b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/DeleteSoundTest.java @@ -25,14 +25,13 @@ import org.catrobat.catroid.ProjectManager; import org.catrobat.catroid.R; -import org.catrobat.catroid.common.SoundInfo; import org.catrobat.catroid.content.Project; import org.catrobat.catroid.content.Sprite; -import org.catrobat.catroid.io.ResourceImporter; import org.catrobat.catroid.io.XstreamSerializer; import org.catrobat.catroid.testsuites.annotations.Cat; import org.catrobat.catroid.testsuites.annotations.Level; import org.catrobat.catroid.ui.SpriteActivity; +import org.catrobat.catroid.uiespresso.ui.fragment.actionutils.ActionUtils; import org.catrobat.catroid.uiespresso.util.UiTestUtils; import org.catrobat.catroid.uiespresso.util.rules.FragmentActivityTestRule; import org.junit.Before; @@ -41,17 +40,15 @@ import org.junit.experimental.categories.Category; import org.junit.runner.RunWith; -import java.io.File; import java.io.IOException; -import java.util.List; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.platform.app.InstrumentationRegistry; -import static org.catrobat.catroid.common.Constants.SOUND_DIRECTORY_NAME; import static org.catrobat.catroid.uiespresso.ui.fragment.rvutils.RecyclerViewInteractionWrapper.onRecyclerView; import static org.hamcrest.Matchers.allOf; +import static org.koin.java.KoinJavaComponent.inject; import static androidx.test.espresso.Espresso.onView; import static androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu; @@ -66,6 +63,8 @@ @RunWith(AndroidJUnit4.class) public class DeleteSoundTest { + final ProjectManager projectManager = inject(ProjectManager.class).getValue(); + @Rule public FragmentActivityTestRule baseActivityTestRule = new FragmentActivityTestRule<>(SpriteActivity.class, SpriteActivity.EXTRA_FRAGMENT_POSITION, @@ -76,12 +75,15 @@ public class DeleteSoundTest { @Before public void setUp() throws Exception { createProject(); - baseActivityTestRule.launchActivity(); } @Category({Cat.AppUi.class, Level.Smoke.class}) @Test - public void deleteSoundTest() { + public void deleteSoundMultipleElementsListTest() throws IOException { + ActionUtils.addSound(projectManager, "testSound1"); + ActionUtils.addSound(projectManager, toBeDeletedSoundName); + baseActivityTestRule.launchActivity(); + openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getInstrumentation().getTargetContext()); onView(withText(R.string.delete)).perform(click()); @@ -109,7 +111,24 @@ public void deleteSoundTest() { @Category({Cat.AppUi.class, Level.Smoke.class}) @Test - public void cancelDeleteSoundTest() { + public void deleteSoundSingleElementListTest() throws IOException { + ActionUtils.addSound(projectManager, toBeDeletedSoundName); + baseActivityTestRule.launchActivity(); + + openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getInstrumentation().getTargetContext()); + onView(withText(R.string.delete)).perform(click()); + + onView(withText(toBeDeletedSoundName)) + .check(doesNotExist()); + } + + @Category({Cat.AppUi.class, Level.Smoke.class}) + @Test + public void cancelDeleteSoundTest() throws IOException { + ActionUtils.addSound(projectManager, "testSound1"); + ActionUtils.addSound(projectManager, toBeDeletedSoundName); + baseActivityTestRule.launchActivity(); + openActionBarOverflowOrOptionsMenu(InstrumentationRegistry.getInstrumentation().getTargetContext()); onView(withText(R.string.delete)).perform(click()); @@ -145,38 +164,15 @@ public void selectFragmentToDeleteTest() { onRecyclerView().atPosition(0).performCheckItemCheck(); } - private void createProject() throws IOException { + private void createProject() { String projectName = "deleteSoundFragmentTest"; Project project = new Project(ApplicationProvider.getApplicationContext(), projectName); Sprite sprite = new Sprite("testSprite"); project.getDefaultScene().addSprite(sprite); - ProjectManager.getInstance().setCurrentProject(project); - ProjectManager.getInstance().setCurrentSprite(sprite); + projectManager.setCurrentProject(project); + projectManager.setCurrentSprite(sprite); XstreamSerializer.getInstance().saveProject(project); - - File soundFile = ResourceImporter.createSoundFileFromResourcesInDirectory( - InstrumentationRegistry.getInstrumentation().getContext().getResources(), - org.catrobat.catroid.test.R.raw.longsound, - new File(project.getDefaultScene().getDirectory(), SOUND_DIRECTORY_NAME), - "longsound.mp3"); - - File soundFile2 = ResourceImporter.createSoundFileFromResourcesInDirectory( - InstrumentationRegistry.getInstrumentation().getContext().getResources(), - org.catrobat.catroid.test.R.raw.testsoundui, - new File(project.getDefaultScene().getDirectory(), SOUND_DIRECTORY_NAME), - "testsoundui.mp3"); - - List soundInfoList = ProjectManager.getInstance().getCurrentSprite().getSoundList(); - SoundInfo soundInfo = new SoundInfo(); - soundInfo.setFile(soundFile); - soundInfo.setName("testSound1"); - soundInfoList.add(soundInfo); - - SoundInfo soundInfo2 = new SoundInfo(); - soundInfo2.setFile(soundFile2); - soundInfo2.setName(toBeDeletedSoundName); - soundInfoList.add(soundInfo2); } } diff --git a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/ProjectCopierTest.kt b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/ProjectCopierTest.kt index 62c50d3b9e0..250192afe4c 100644 --- a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/ProjectCopierTest.kt +++ b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/ProjectCopierTest.kt @@ -57,6 +57,7 @@ import org.junit.Rule import org.junit.Test import org.junit.experimental.categories.Category import org.junit.runner.RunWith +import org.koin.java.KoinJavaComponent.inject import java.io.File import java.io.IOException @@ -68,6 +69,7 @@ class ProjectCopierTest { ProjectListActivity::class.java, true, false ) private val toBeCopiedProjectName = "testProject" + private val projectManager: ProjectManager = inject(ProjectManager::class.java).value @Before @Throws(Exception::class) @@ -129,8 +131,8 @@ class ProjectCopierTest { script.addBrick(SetXBrick(Formula(BrickValues.X_POSITION))) sprite.addScript(script) project.defaultScene.addSprite(sprite) - ProjectManager.getInstance().currentProject = project - ProjectManager.getInstance().currentSprite = sprite + projectManager.currentProject = project + projectManager.currentSprite = sprite XstreamSerializer.getInstance().saveProject(project) val soundFile = ResourceImporter.createSoundFileFromResourcesInDirectory( getInstrumentation().context.resources, diff --git a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/SpriteListFragmentWithObjectTest.kt b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/SpriteListFragmentWithObjectTest.kt index dfa4858c01a..ef3b5be41aa 100644 --- a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/SpriteListFragmentWithObjectTest.kt +++ b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/SpriteListFragmentWithObjectTest.kt @@ -30,7 +30,9 @@ import androidx.test.espresso.matcher.ViewMatchers.isDisplayed import androidx.test.espresso.matcher.ViewMatchers.withId import androidx.test.espresso.matcher.ViewMatchers.withText import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.catrobat.catroid.ProjectManager import org.catrobat.catroid.R +import org.catrobat.catroid.content.Sprite import org.catrobat.catroid.ui.ProjectActivity import org.catrobat.catroid.ui.ProjectActivity.Companion.EXTRA_FRAGMENT_POSITION import org.catrobat.catroid.ui.ProjectActivity.Companion.FRAGMENT_SPRITES @@ -43,9 +45,12 @@ import org.junit.Before import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith +import org.koin.java.KoinJavaComponent @RunWith(AndroidJUnit4::class) class SpriteListFragmentWithObjectTest { + val projectManager: ProjectManager by KoinJavaComponent.inject(ProjectManager::class.java) + @get:Rule var baseActivityTestRule = FragmentActivityTestRule( ProjectActivity::class.java, @@ -91,8 +96,8 @@ class SpriteListFragmentWithObjectTest { @Test fun testBackpackInOverflow() { + projectManager.currentProject.defaultScene.addSprite(Sprite("testSprite2")) openActionBarOverflowOrOptionsMenu(baseActivityTestRule.activity) - onView(withText(R.string.backpack)) .perform(click()) @@ -103,7 +108,10 @@ class SpriteListFragmentWithObjectTest { @Test fun testCopyInOverflow() { openActionBarOverflowOrOptionsMenu(baseActivityTestRule.activity) + onView(withText(R.string.copy)) + .perform(click()) + openActionBarOverflowOrOptionsMenu(baseActivityTestRule.activity) onView(withText(R.string.copy)) .perform(click()) @@ -113,8 +121,8 @@ class SpriteListFragmentWithObjectTest { @Test fun testDeleteInOverflow() { + projectManager.currentProject.defaultScene.addSprite(Sprite("testSprite2")) openActionBarOverflowOrOptionsMenu(baseActivityTestRule.activity) - onView(withText(R.string.delete)) .perform(click()) diff --git a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/actionutils/ActionUtils.java b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/actionutils/ActionUtils.java new file mode 100644 index 00000000000..3f256b69cfb --- /dev/null +++ b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/actionutils/ActionUtils.java @@ -0,0 +1,80 @@ +/* + * Catroid: An on-device visual programming system for Android devices + * Copyright (C) 2010-2022 The Catrobat Team + * () + * + * 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 . + */ + +package org.catrobat.catroid.uiespresso.ui.fragment.actionutils; + +import org.catrobat.catroid.ProjectManager; +import org.catrobat.catroid.common.LookData; +import org.catrobat.catroid.common.SoundInfo; +import org.catrobat.catroid.io.ResourceImporter; +import org.catrobat.catroid.io.XstreamSerializer; + +import java.io.File; +import java.io.IOException; +import java.util.List; + +import androidx.test.platform.app.InstrumentationRegistry; + +import static org.catrobat.catroid.common.Constants.IMAGE_DIRECTORY_NAME; +import static org.catrobat.catroid.common.Constants.SOUND_DIRECTORY_NAME; + +public class ActionUtils { + + protected ActionUtils() { + throw new UnsupportedOperationException(); + } + + public static void addLook(ProjectManager projectManager, String lookName) throws IOException { + File imageFile = ResourceImporter.createImageFileFromResourcesInDirectory( + InstrumentationRegistry.getInstrumentation().getContext().getResources(), + org.catrobat.catroid.test.R.drawable.catroid_banzai, + new File(projectManager.getCurrentProject().getDefaultScene().getDirectory(), + IMAGE_DIRECTORY_NAME), + "catroid_sunglasses.png", + 1); + + List lookDataList = projectManager.getCurrentSprite().getLookList(); + + LookData lookData1 = new LookData(); + lookData1.setFile(imageFile); + lookData1.setName(lookName); + lookDataList.add(lookData1); + XstreamSerializer.getInstance().saveProject(projectManager.getCurrentProject()); + } + + public static void addSound(ProjectManager projectManager, String soundName) throws IOException { + File soundFile = ResourceImporter.createSoundFileFromResourcesInDirectory( + InstrumentationRegistry.getInstrumentation().getContext().getResources(), + org.catrobat.catroid.test.R.raw.longsound, + new File(projectManager.getCurrentProject().getDefaultScene().getDirectory(), SOUND_DIRECTORY_NAME), + "longsound.mp3"); + + List soundInfoList = projectManager.getCurrentSprite().getSoundList(); + SoundInfo soundInfo = new SoundInfo(); + + soundInfo.setFile(soundFile); + soundInfo.setName(soundName); + soundInfoList.add(soundInfo); + XstreamSerializer.getInstance().saveProject(projectManager.getCurrentProject()); + } +} diff --git a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/tabs/TabLayoutActionModeTest.java b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/tabs/TabLayoutActionModeTest.java index 4275a3772e7..5b8f7b2b387 100644 --- a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/tabs/TabLayoutActionModeTest.java +++ b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/tabs/TabLayoutActionModeTest.java @@ -62,6 +62,7 @@ import static org.junit.Assert.assertNull; import static org.junit.runners.Parameterized.Parameter; import static org.junit.runners.Parameterized.Parameters; +import static org.koin.java.KoinJavaComponent.inject; import static androidx.test.espresso.Espresso.onIdle; import static androidx.test.espresso.Espresso.onView; @@ -90,6 +91,7 @@ public static Iterable data() { public Integer fragment; private Project project; + final ProjectManager projectManager = inject(ProjectManager.class).getValue(); @Before public void setUp() throws IOException { @@ -98,7 +100,7 @@ public void setUp() throws IOException { } @Test - public void testFragment() { + public void testFragmentAbortDelete() { onView(withId(tab_layout)).perform(selectTabAtPosition(fragment)); assertTabLayoutIsShown(fragment); openActionBarOverflowOrOptionsMenu(baseActivityTestRule.getActivity()); @@ -123,17 +125,19 @@ private void assertTabLayoutIsNotShown() { private void createProject() throws IOException { project = new Project(ApplicationProvider.getApplicationContext(), "TabLayoutActionModeTest"); Sprite sprite = new Sprite("testSprite"); - project.getDefaultScene().addSprite(sprite); - ProjectManager.getInstance().setCurrentProject(project); - ProjectManager.getInstance().setCurrentSprite(sprite); - ProjectManager.getInstance().setCurrentlyEditedScene(project.getDefaultScene()); + projectManager.setCurrentProject(project); + projectManager.setCurrentSprite(sprite); + projectManager.setCurrentlyEditedScene(project.getDefaultScene()); XstreamSerializer.getInstance().saveProject(project); Script script = new StartScript(); sprite.addScript(script); + sprite.addScript(new StartScript()); + sprite.getLookList().add(createLookData()); sprite.getLookList().add(createLookData()); sprite.getSoundList().add(createSoundInfo()); + sprite.getSoundList().add(createSoundInfo()); } private LookData createLookData() { diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/LookListFragment.kt b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/LookListFragment.kt index 5975c49166f..30fad68d384 100644 --- a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/LookListFragment.kt +++ b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/LookListFragment.kt @@ -163,6 +163,7 @@ class LookListFragment : RecyclerViewFragment() { override fun deleteItems(selectedItems: List) { setShowProgressBar(true) + var deletedItemCount = 0 for (item in selectedItems) { try { lookController.delete(item) @@ -170,12 +171,13 @@ class LookListFragment : RecyclerViewFragment() { Log.e(TAG, Log.getStackTraceString(e)) } adapter.remove(item) + deletedItemCount++ } ToastUtil.showSuccess( requireContext(), resources.getQuantityString( R.plurals.deleted_looks, - selectedItems.size, - selectedItems.size + deletedItemCount, + deletedItemCount ) ) finishActionMode() diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/ProjectListFragment.kt b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/ProjectListFragment.kt index f24f31da273..823063a9e40 100644 --- a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/ProjectListFragment.kt +++ b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/ProjectListFragment.kt @@ -339,6 +339,7 @@ class ProjectListFragment : RecyclerViewFragment(), ProjectLoadLis override fun deleteItems(selectedItems: MutableList?) { setShowProgressBar(true) + var deletedItemCount = 0 selectedItems ?: return for (item in selectedItems) { item ?: continue @@ -349,12 +350,13 @@ class ProjectListFragment : RecyclerViewFragment(), ProjectLoadLis Log.e(TAG, Log.getStackTraceString(e)) } adapter.remove(item) + deletedItemCount++ } ToastUtil.showSuccess( requireContext(), resources.getQuantityString( R.plurals.deleted_projects, - selectedItems.size, - selectedItems.size + deletedItemCount, + deletedItemCount ) ) finishActionMode() diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/RecyclerViewFragment.java b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/RecyclerViewFragment.java index 7ed39bbbe9d..8b4a895de33 100644 --- a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/RecyclerViewFragment.java +++ b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/RecyclerViewFragment.java @@ -97,6 +97,7 @@ public abstract class RecyclerViewFragment extends Fragment protected UniqueNameProvider uniqueNameProvider = new UniqueNameProvider(); protected ItemTouchHelper touchHelper; + protected int itemCountThreshold = 1; protected RecyclerView.AdapterDataObserver observer = new RecyclerView.AdapterDataObserver() { @@ -133,7 +134,7 @@ public boolean onCreateActionMode(ActionMode mode, Menu menu) { onRename(menu); return true; case MERGE: - adapter.selectionMode = adapter.PAIRS; + adapter.selectionMode = RVAdapter.PAIRS; mode.setTitle(R.string.am_merge); break; case IMPORT_LOCAL: @@ -148,7 +149,7 @@ public boolean onCreateActionMode(ActionMode mode, Menu menu) { } private void onRename(Menu menu) { - adapter.selectionMode = adapter.SINGLE; + adapter.selectionMode = RVAdapter.SINGLE; adapter.showSettings = false; adapter.showRipples = false; menu.findItem(R.id.confirm).setVisible(false); @@ -232,7 +233,7 @@ protected void resetActionModeParameters() { adapter.showCheckBoxes = false; adapter.showSettings = true; adapter.showRipples = true; - adapter.selectionMode = adapter.MULTIPLE; + adapter.selectionMode = RVAdapter.MULTIPLE; } @Override @@ -321,7 +322,7 @@ public void onPrepareOptionsMenu(@NotNull Menu menu) { public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.backpack: - prepareActionMode(BACKPACK); + prepareBackpackActionMode(); break; case R.id.copy: prepareActionMode(COPY); @@ -330,10 +331,10 @@ public boolean onOptionsItemSelected(MenuItem item) { prepareActionMode(DELETE); break; case R.id.rename: - prepareActionMode(RENAME); + startActionMode(RENAME); break; case R.id.merge: - prepareActionMode(MERGE); + startActionMode(MERGE); break; case R.id.show_details: adapter.showDetails = !adapter.showDetails; @@ -349,22 +350,42 @@ public boolean onOptionsItemSelected(MenuItem item) { return true; } - protected void prepareActionMode(@ActionModeType int type) { - if (type == BACKPACK) { - if (isBackpackEmpty()) { - startActionMode(BACKPACK); - } else if (adapter.getItems().isEmpty()) { - switchToBackpack(); - } else { - showBackpackModeChooser(); + protected void prepareActionMode(int type) { + if (adapter.getItemCount() == itemCountThreshold) { + switch (type) { + case COPY: + copyItems(adapter.getItems() + .subList(itemCountThreshold - 1, adapter.getItemCount())); + break; + case DELETE: + deleteItems(adapter.getItems() + .subList(itemCountThreshold - 1, adapter.getItemCount())); + break; + default: + break; } } else { startActionMode(type); } } + private void prepareBackpackActionMode() { + if (adapter.getItemCount() == itemCountThreshold) { + packItems(adapter.getItems().subList(itemCountThreshold - 1, adapter.getItemCount())); + return; + } + + if (isBackpackEmpty()) { + startActionMode(BACKPACK); + } else if (adapter.getItems().isEmpty()) { + switchToBackpack(); + } else { + showBackpackModeChooser(); + } + } + private void startActionMode(@ActionModeType int type) { - if (adapter.getItems().isEmpty() || (this instanceof SpriteListFragment && adapter.getItems().size() == 1)) { + if (adapter.getItems().isEmpty()) { ToastUtil.showError(getActivity(), R.string.am_empty_list); resetActionModeParameters(); } else { @@ -399,7 +420,7 @@ public void onSelectionChanged(int selectedItemCnt) { } protected void updateSelectionToggle(Menu menu) { - if (adapter.selectionMode == adapter.MULTIPLE) { + if (adapter.selectionMode == RVAdapter.MULTIPLE) { MenuItem selectionToggle = menu.findItem(R.id.toggle_selection); selectionToggle.setVisible(true); menu.findItem(R.id.overflow).setVisible(true); @@ -457,7 +478,7 @@ public void notifyDataSetChanged() { protected void showBackpackModeChooser() { CharSequence[] items = new CharSequence[] {getString(R.string.pack), getString(R.string.unpack)}; - new AlertDialog.Builder(getContext()) + new AlertDialog.Builder(requireContext()) .setTitle(R.string.backpack_title) .setItems(items, (dialog, which) -> { switch (which) { @@ -481,7 +502,7 @@ protected void showBackpackModeChooser() { protected abstract int getDeleteAlertTitleId(); protected void showDeleteAlert(final List selectedItems) { - new AlertDialog.Builder(getContext()) + new AlertDialog.Builder(requireContext()) .setTitle(getResources().getQuantityString(getDeleteAlertTitleId(), selectedItems.size())) .setMessage(R.string.dialog_confirm_delete) .setPositiveButton(R.string.delete, (dialog, id) -> deleteItems(selectedItems)) @@ -493,7 +514,7 @@ protected void showDeleteAlert(final List selectedItems) { protected abstract void deleteItems(List selectedItems); protected void showRenameDialog(T selectedItem) { - TextInputDialog.Builder builder = new TextInputDialog.Builder(getContext()); + TextInputDialog.Builder builder = new TextInputDialog.Builder(requireContext()); builder.setHint(getString(getRenameDialogHint())) .setText(selectedItem.getName()) .setTextWatcher(new DuplicateInputTextWatcher(adapter.getItems())) @@ -516,14 +537,12 @@ protected void showMergeDialog(List selectedItems) { if (adapter.getSelectedItems().size() <= 1) { ToastUtil.showError(getContext(), R.string.am_merge_error); } else { - TextInputDialog.Builder builder = new TextInputDialog.Builder(getContext()); + TextInputDialog.Builder builder = new TextInputDialog.Builder(requireContext()); builder.setHint(getString(R.string.project_name_label)) .setTextWatcher(new NewProjectNameTextWatcher<>()) .setPositiveButton(getString(R.string.ok), (TextInputDialog.OnClickListener) (dialog, textInput) - -> { - mergeProjects(selectedItems, textInput); - }); + -> mergeProjects(selectedItems, textInput)); builder.setTitle(R.string.new_merge_project_dialog_title) .setNegativeButton(R.string.cancel, null) diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/SceneListFragment.kt b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/SceneListFragment.kt index ff40b88b89c..84469674a92 100644 --- a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/SceneListFragment.kt +++ b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/SceneListFragment.kt @@ -144,6 +144,7 @@ class SceneListFragment : RecyclerViewFragment(), override fun deleteItems(selectedItems: List) { setShowProgressBar(true) + var deletedItemsCount = 0 for (item in selectedItems) { try { sceneController.delete(item) @@ -151,12 +152,13 @@ class SceneListFragment : RecyclerViewFragment(), Log.e(TAG, Log.getStackTraceString(e)) } adapter.remove(item) + deletedItemsCount++ } ToastUtil.showSuccess( activity, resources.getQuantityString( R.plurals.deleted_scenes, - selectedItems.size, - selectedItems.size + deletedItemsCount, + deletedItemsCount ) ) finishActionMode() diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/ScriptFragment.java b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/ScriptFragment.java index a8689709ae0..e29918c8280 100644 --- a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/ScriptFragment.java +++ b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/ScriptFragment.java @@ -452,7 +452,7 @@ public boolean onOptionsItemSelected(MenuItem item) { loadProjectAfterUndoOption(); break; case R.id.backpack: - prepareActionMode(BACKPACK); + prepareBackpackActionMode(); break; case R.id.copy: prepareActionMode(COPY); @@ -461,7 +461,7 @@ public boolean onOptionsItemSelected(MenuItem item) { prepareActionMode(DELETE); break; case R.id.comment_in_out: - prepareActionMode(COMMENT); + startActionMode(COMMENT); break; case R.id.catblocks: switchToCatblocks(); @@ -536,20 +536,39 @@ public void onCategorySelected(String category) { .commit(); } - protected void prepareActionMode(@ActionModeType int type) { - if (type == BACKPACK) { - if (BackpackListManager.getInstance().getBackpackedScriptGroups().isEmpty()) { - startActionMode(BACKPACK); - } else if (adapter.isEmpty()) { - switchToBackpack(); - } else { - showBackpackModeChooser(); + protected void prepareActionMode(int type) { + if (adapter.getCount() == 1) { + switch (type) { + case COPY: + copy(adapter.getItems()); + break; + case DELETE: + delete(adapter.getItems()); + break; + default: + startActionMode(type); + break; } } else { startActionMode(type); } } + private void prepareBackpackActionMode() { + if (adapter.getItems().size() == 1) { + showNewScriptGroupAlert(adapter.getItems()); + return; + } + + if (BackpackListManager.getInstance().getBackpackedScriptGroups().isEmpty()) { + startActionMode(BACKPACK); + } else if (adapter.isEmpty()) { + switchToBackpack(); + } else { + showBackpackModeChooser(); + } + } + private void startActionMode(@ActionModeType int type) { if (adapter.isEmpty()) { ToastUtil.showError(getActivity(), R.string.am_empty_list); diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/SoundListFragment.kt b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/SoundListFragment.kt index 34fe2278fe9..b1b9f317b53 100644 --- a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/SoundListFragment.kt +++ b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/SoundListFragment.kt @@ -146,7 +146,7 @@ class SoundListFragment : RecyclerViewFragment() { override fun deleteItems(selectedItems: List) { setShowProgressBar(true) - + var deletedItemsCount = 0 for (item in selectedItems) { try { soundController.delete(item) @@ -154,13 +154,14 @@ class SoundListFragment : RecyclerViewFragment() { Log.e(TAG, Log.getStackTraceString(e)) } adapter.remove(item) + deletedItemsCount++ } ToastUtil.showSuccess( requireContext(), resources.getQuantityString( R.plurals.deleted_sounds, - selectedItems.size, - selectedItems.size + deletedItemsCount, + deletedItemsCount ) ) finishActionMode() diff --git a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/SpriteListFragment.kt b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/SpriteListFragment.kt index 8632cb9a319..0b0df92a676 100644 --- a/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/SpriteListFragment.kt +++ b/catroid/src/main/java/org/catrobat/catroid/ui/recyclerview/fragment/SpriteListFragment.kt @@ -134,13 +134,23 @@ class SpriteListFragment : RecyclerViewFragment() { } override fun onOptionsItemSelected(item: MenuItem): Boolean { + this.itemCountThreshold = 2 when (item.itemId) { R.id.new_group -> showNewGroupDialog() - else -> super.onOptionsItemSelected(item) + else -> handleSelectedOptionItem(item) } return true } + private fun handleSelectedOptionItem(item: MenuItem) { + if (adapter.items.size == 1) { + ToastUtil.showError(activity, R.string.am_empty_list) + resetActionModeParameters() + } else { + super.onOptionsItemSelected(item) + } + } + private fun showNewGroupDialog() { val builder = TextInputDialog.Builder(requireContext()) val uniqueNameProvider = UniqueNameProvider() @@ -231,6 +241,7 @@ class SpriteListFragment : RecyclerViewFragment() { override fun deleteItems(selectedItems: List) { setShowProgressBar(true) + var deletedItemsCount = 0 for (item in selectedItems) { if (item is GroupSprite) { for (sprite in item.groupItems) { @@ -242,12 +253,13 @@ class SpriteListFragment : RecyclerViewFragment() { } spriteController.delete(item) adapter.remove(item) + deletedItemsCount++ } ToastUtil.showSuccess( requireContext(), resources.getQuantityString( R.plurals.deleted_sprites, - selectedItems.size, - selectedItems.size + deletedItemsCount, + deletedItemsCount ) ) finishActionMode() @@ -368,7 +380,6 @@ class SpriteListFragment : RecyclerViewFragment() { R.id.rename -> showRenameDialog(item) R.id.from_library -> addFromLibrary(item) R.id.from_local -> addFromLocalProject(item) - else -> {} } true }