The app supports 2 locales. During instrumentation or UI test, I want to test with a specific language (such as EN
). Referring 6629458, I made a function for changing the locale in kotlin:
private fun setLocale(language: String, country: String) {
val context = InstrumentationRegistry.getInstrumentation().context
val locale = Locale(language, country)
// here we update locale for date formatters
Locale.setDefault(locale)
// here we update locale for app resources
val res: Resources = context.getResources()
val config: Configuration = res.configuration
config.locale = locale
res.updateConfiguration(config, res.displayMetrics)
}
It can be called inside a UT test function body:
@Test
fun uiTest () {
composeTestRule.setContent {
//...
}
setLocale("en", "EN")
//...
}
However, the locale is not set to English but depending on the default setting of my testing Android device. Is there any clue for this (function is call without error but the locale is NOT changed as expected)?
Another question was also posted but it is not for UI test.
composeTestRule.setContent {
DeviceConfigurationOverride(
DeviceConfigurationOverride.Locales(LocaleList("es-ES"))
) {
//...
}