2

This is a two part question.

1 - Does it add value to the project to check every string matches the correct widget across multiple languages for an Activity's associated UI test or should I take a different approach or just not do this at all?

2 - Assuming this is the right approach I have tried to implement the suggestion from the following post. Unfortunately the locale is not updating for my UI tests and fails one of the two tests depending on the system language that I have already set on my device.

@RunWith(AndroidJUnit4ClassRunner.class)
public class RegisterActivityTest {

    @Test
    public void test_onScreenLoadStringsDisplayedInEnglish() {
        testEnglishLocale();
        onView(withId(R.id.welcome_text_view)).check(matches(
            withText(WELCOME_EN)));
    }

    @Test
    public void test_onScreenLoadStringsDisplayedInSpanish() {
        testSpanishLocale();
        onView(withId(R.id.welcome_text_view)).check(matches(
            withText(WELCOME_ES)));
    }


    private void testEnglishLocale() {
        setLocale("en", "US");
    }

    private void testSpanishLocale() {
        setLocale("es", "ES");
    }

    private void setLocale(String language, String country) {
        Locale locale = new Locale(language, country);
        // here we update locale for date formatters
        Locale.setDefault(locale);
        // update locale for app resources
        Resources res = InstrumentationRegistry.getInstrumentation().getTargetContext().getResources();
        Configuration config = res.getConfiguration();
        config.locale = locale;
        res.updateConfiguration(config, res.getDisplayMetrics());
    }

}
smoljak
  • 37
  • 4

1 Answers1

0

To answer the first part of your question, It does add value to test around different languages. A common issue we run in to is that UI's are designed for a local language, but often a translated language has longer verbiage than the local version. This can cause unexpected bad UI behavior, such as buttons expanding beyond the edge of a screen, or content getting pushed down. It's these rendering issues that are good to test around.

I can't comment on the second question yet. I've just started digging in to how to implement the locale change in espresso.

notmystyle
  • 1,056
  • 9
  • 7