Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android更改测试的语言环境_Android_Kotlin - Fatal编程技术网

Android更改测试的语言环境

Android更改测试的语言环境,android,kotlin,Android,Kotlin,我想测试当用户更改语言时字符串是否正确更新。我正在使用Espresso测试字符串是否与正确的语言环境匹配,目前我正在这样更改它: private fun changeLocale(language: String, country: String) { val locale = Locale(language, country) Locale.setDefault(locale) val configuration = Configuration() config

我想测试当用户更改语言时字符串是否正确更新。我正在使用
Espresso
测试字符串是否与正确的语言环境匹配,目前我正在这样更改它:

private fun changeLocale(language: String, country: String) {
    val locale = Locale(language, country)
    Locale.setDefault(locale)
    val configuration = Configuration()
    configuration.locale = locale
    context.activity.baseContext.createConfigurationContext(configuration)


    getInstrumentation().runOnMainSync {
        context.activity.recreate()
    }

}
问题是,在View(withText(预期)).check(matches(isDisplayed())上的浓缩咖啡测试
断言为false,因此我想知道在运行测试之前设置默认区域设置的正确方法是什么?

根据答案,您可以通过编程方式更改区域设置:

public class ResourcesTestCase extends AndroidTestCase {

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

    public void testEnglishLocale() {
        setLocale("en", "EN");
        String cancelString = getContext().getString(R.string.cancel);
        assertEquals("Cancel", cancelString);
    }

    public void testGermanLocale() {
        setLocale("de", "DE");
        String cancelString = getContext().getString(R.string.cancel);
        assertEquals("Abbrechen", cancelString);
    }

    public void testSpanishLocale() {
        setLocale("es", "ES");
        String cancelString = getContext().getString(R.string.cancel);
        assertEquals("Cancelar", cancelString);
    }

}

您可以轻松地将代码转换为Kotlin。

根据我的经验,在运行时设置语言环境是不可靠的。关于这个话题,这家伙还有很多话要说:


您应该尝试使用Firebase Test Lab或类似服务,并在不同的设备(设置了不同的区域设置)上运行测试。

Kotlin版本,基于@Adib Faramazi的帖子

    private fun setLocale(language: String, country: String) {
       val locale = Locale(language, country)
       // here we update locale for date formatters
       Locale.setDefault(locale)
       // here we update locale for app resources
       val context: Context = getApplicationContext()
       val res: Resources = context.resources
       val config: Configuration = res.configuration
       config.setLocales(LocaleList(locale))
       res.updateConfiguration(config, res.displayMetrics)
}