From c7f7b543dbfb25fa13a4d260083e44d5039a48c7 Mon Sep 17 00:00:00 2001 From: Dorian Percic Date: Sun, 30 Jul 2023 18:59:46 +0200 Subject: [PATCH] IDE-130 Fix spriteEqualBackgroundTest Remove spriteEqualBackgroundTest as an uiespresso test, instead implement as an Unit Test. --- .../test/content/sprite/SpriteTest.java | 23 ++++++++ .../ui/fragment/RenameSpriteTest.kt | 53 ------------------- .../org/catrobat/catroid/content/Project.java | 7 ++- 3 files changed, 26 insertions(+), 57 deletions(-) diff --git a/catroid/src/androidTest/java/org/catrobat/catroid/test/content/sprite/SpriteTest.java b/catroid/src/androidTest/java/org/catrobat/catroid/test/content/sprite/SpriteTest.java index 3412f91bf4e..d10d9dd7531 100644 --- a/catroid/src/androidTest/java/org/catrobat/catroid/test/content/sprite/SpriteTest.java +++ b/catroid/src/androidTest/java/org/catrobat/catroid/test/content/sprite/SpriteTest.java @@ -39,16 +39,22 @@ import org.catrobat.catroid.formulaeditor.UserVariable; import org.catrobat.catroid.test.utils.TestUtils; import org.catrobat.catroid.utils.ShowTextUtils.AndroidStringProvider; +import org.junit.After; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import java.util.Locale; + import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertTrue; +import static org.catrobat.catroid.ui.settingsfragments.SettingsFragment.updateLocale; + @RunWith(AndroidJUnit4.class) public class SpriteTest { @@ -60,6 +66,7 @@ public class SpriteTest { private Project project; private Sprite sprite; + private Locale locale; private AndroidStringProvider androidStringProvider = new AndroidStringProvider(ApplicationProvider.getApplicationContext()); @@ -76,6 +83,13 @@ public void setUp() throws Exception { project.addUserVariable(globalVariable); ProjectManager.getInstance().setCurrentProject(project); + locale = ApplicationProvider.getApplicationContext() + .getResources().getConfiguration().locale; + } + + @After + public void tearDown() { + updateLocale(ApplicationProvider.getApplicationContext(), locale); } @Test @@ -114,4 +128,13 @@ public void testUserVariableVisibilityOfLocalVariablesInDifferentScenes() { userVariable = sprite2.getUserVariable(variableName); assertTrue(userVariable.getVisible()); } + + @Test + public void spriteEqualBackgroundTest() { + sprite.setName("Hintergrund"); + updateLocale(ApplicationProvider.getApplicationContext(), new Locale("de")); + project.checkIfSpriteNameEqualBackground(ApplicationProvider.getApplicationContext()); + Assert.assertNotEquals(sprite.getName(), "Hintergrund"); + Assert.assertEquals(sprite.getName(), "Hintergrund (1)"); + } } diff --git a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/RenameSpriteTest.kt b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/RenameSpriteTest.kt index 691994a7570..9d8c9ca7d43 100644 --- a/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/RenameSpriteTest.kt +++ b/catroid/src/androidTest/java/org/catrobat/catroid/uiespresso/ui/fragment/RenameSpriteTest.kt @@ -47,7 +47,6 @@ import org.catrobat.catroid.testsuites.annotations.Level.Smoke import org.catrobat.catroid.ui.ProjectActivity import org.catrobat.catroid.ui.settingsfragments.SettingsFragment.setLanguageSharedPreference import org.catrobat.catroid.uiespresso.ui.fragment.rvutils.RecyclerViewInteractionWrapper -import org.catrobat.catroid.uiespresso.ui.fragment.rvutils.RecyclerViewInteractionWrapper.onRecyclerView import org.catrobat.catroid.uiespresso.util.UiTestUtils import org.catrobat.catroid.uiespresso.util.rules.FragmentActivityTestRule import org.hamcrest.Matchers.allOf @@ -217,58 +216,6 @@ class RenameSpriteTest { .check(doesNotExist()) } - @Test - fun spriteEqualBackgroundTest() { - UiTestUtils.openActionBarMenu() - onView(withText(R.string.rename)).perform(click()) - onRecyclerView().atPosition(0) - .check(matches(not(isDisplayed()))) - onRecyclerView().atPosition(2) - .perform(click()) - onView(withText(R.string.rename_sprite_dialog)) - .inRoot(isDialog()) - .check(matches(isDisplayed())) - val backgroundString = "Background" - onView(allOf(withText(secondSpriteName), isDisplayed())) - .perform(replaceText(backgroundString)) - closeSoftKeyboard() - onView(allOf(withId(android.R.id.button1), withText(R.string.ok))) - .check(matches(not(isEnabled()))) - } - - // This test only works when the OS language of the emulator or device is set to English. - // Somehow the project gets created in the OS language whilst menus are translated according - // to setLanguageSharedPreference(...). This test is used to test functionality after switching - // language, this means if OS language is not set to english it probably wont work. - @Test - fun spriteEqualsBackgroundNameAfterLanguageChangeTest() { - baseActivityTestRule.finishActivity() - setLanguageSharedPreference(ApplicationProvider.getApplicationContext(), "en-GB") - baseActivityTestRule.launchActivity() - - UiTestUtils.openActionBarMenu() - onView(withText(R.string.rename)).perform(click()) - onRecyclerView().atPosition(0) - .check(matches(not(isDisplayed()))) - onRecyclerView().atPosition(2) - .perform(click()) - onView(withText(R.string.rename_sprite_dialog)) - .inRoot(isDialog()) - .check(matches(isDisplayed())) - val backgroundString = "Hintergrund" - onView(allOf(withText(secondSpriteName), isDisplayed())) - .perform(replaceText(backgroundString)) - closeSoftKeyboard() - onView(allOf(withId(android.R.id.button1), withText(R.string.ok))) - .perform(click()) - - baseActivityTestRule.finishActivity() - setLanguageSharedPreference(ApplicationProvider.getApplicationContext(), "de") - baseActivityTestRule.launchActivity() - onView(withText("$backgroundString (1)")) - .check(matches(isDisplayed())) - } - private fun createProject(projectName: String) { val project = Project(ApplicationProvider.getApplicationContext(), projectName) project.defaultScene.addSprite(Sprite(firstSpriteName)) diff --git a/catroid/src/main/java/org/catrobat/catroid/content/Project.java b/catroid/src/main/java/org/catrobat/catroid/content/Project.java index fff8720e765..cc5bca4e860 100644 --- a/catroid/src/main/java/org/catrobat/catroid/content/Project.java +++ b/catroid/src/main/java/org/catrobat/catroid/content/Project.java @@ -22,7 +22,6 @@ */ package org.catrobat.catroid.content; -import android.app.Activity; import android.content.Context; import android.os.Build; @@ -537,15 +536,15 @@ public List getSpriteNames(List spriteList) { return spriteNames; } - public void checkIfSpriteNameEqualBackground(Activity activity) { + public void checkIfSpriteNameEqualBackground(Context context) { List spriteList = new ArrayList<>(this.getSpriteListWithClones()); List spriteNames = getSpriteNames(spriteList); for (int sprite = 1; sprite < spriteList.size(); ++sprite) { - if (spriteList.get(sprite).getName().matches("[\\s]*" + activity.getString(R.string.background) + if (spriteList.get(sprite).getName().matches("[\\s]*" + context.getString(R.string.background) + "[\\s]*")) { UniqueNameProvider name = new UniqueNameProvider(); - String newSpriteName = name.getUniqueName(activity.getString(R.string.background), spriteNames); + String newSpriteName = name.getUniqueName(context.getString(R.string.background), spriteNames); spriteList.get(sprite).setName(newSpriteName); return; }