Skip to content

Commit

Permalink
IDE-130 Fix spriteEqualBackgroundTest (#4847)
Browse files Browse the repository at this point in the history
Remove spriteEqualBackgroundTest as an uiespresso test, instead implement as an Unit Test.
  • Loading branch information
dorianpercic authored Dec 1, 2023
1 parent fc53544 commit 6647541
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -60,6 +66,7 @@ public class SpriteTest {

private Project project;
private Sprite sprite;
private Locale locale;

private AndroidStringProvider androidStringProvider =
new AndroidStringProvider(ApplicationProvider.getApplicationContext());
Expand All @@ -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
Expand Down Expand Up @@ -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)");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
*/
package org.catrobat.catroid.content;

import android.app.Activity;
import android.content.Context;
import android.os.Build;

Expand Down Expand Up @@ -537,15 +536,15 @@ public List<String> getSpriteNames(List<Sprite> spriteList) {
return spriteNames;
}

public void checkIfSpriteNameEqualBackground(Activity activity) {
public void checkIfSpriteNameEqualBackground(Context context) {
List<Sprite> spriteList =
new ArrayList<>(this.getSpriteListWithClones());
List<String> 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;
}
Expand Down

0 comments on commit 6647541

Please sign in to comment.