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());
}
}