android - 执行 Espresso 测试时更改语言环境

标签 android localization internationalization android-espresso

我正在创建一个应该支持阿拉伯语和 RTL 布局的简单布局。一切正常。现在我想编写一个 Espresso 测试并断言它是否实际显示翻译文本。例如对于阿拉伯语,它应该显示来自阿拉伯语 strings.xml 的文本。

到目前为止,我尝试将以下代码作为 TestRule。

public void setLocale(Locale locale) {
        Resources resources = InstrumentationRegistry.getTargetContext().getResources();
        Locale.setDefault(locale);
        Configuration config = resources.getConfiguration();
        config.locale = locale;
        resources.updateConfiguration(config, resources.getDisplayMetrics());
    }

上面的代码改变了布局方向,但没有从本地化目录加载资源。

我没有做任何额外的事情,而是像 http://www.andreamaglie.com/2016/a-test-rule-for-setting-device-locale/ 这样的事情

我错过了什么吗?

最佳答案

我创建了一个 small test project使用 link you provided对于美国和英国,主要类(class)在答案的下方,但这是一个公共(public)项目,因此您可以 download it .
对于“AE”,您需要在 values-ar-rAE ( see this link ) 下创建一个 strings.xml
编辑:为每种语言和 MyActions 类添加了另一个测试。
致谢:MyActions 来自 here ,测试示例来自here ,您可能需要从开发人员设置中停止动画(从第二个链接和 here )

ForceLocaleRule:

import android.content.res.Configuration;
import android.content.res.Resources;
import android.support.test.InstrumentationRegistry;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import java.util.Locale;

public class ForceLocaleRule implements TestRule {

    private final Locale testLocale;
    private Locale deviceLocale;

    public ForceLocaleRule(Locale testLocale) {
        this.testLocale = testLocale;
    }

    @Override
    public Statement apply(final Statement base, Description description) {
        return new Statement() {
            public void evaluate() throws Throwable {
                try {
                    if (testLocale != null) {
                        deviceLocale = Locale.getDefault();
                        setLocale(testLocale);
                    }

                    base.evaluate();
                } finally {
                    if (deviceLocale != null) {
                        setLocale(deviceLocale);
                    }
                }
            }
        };
    }

    public void setLocale(Locale locale) {
        Resources resources = InstrumentationRegistry.getInstrumentation().getTargetContext().getResources();
        Locale.setDefault(locale);
        Configuration config = resources.getConfiguration();
        config.locale = locale;
        resources.updateConfiguration(config, resources.getDisplayMetrics());
    }

}

美国测试:

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Locale;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static example.com.testlocale.MyActions.setTextInTextView;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;

@RunWith(AndroidJUnit4.class)
public class MainActivityUsTest {

    @ClassRule
    public static final ForceLocaleRule localeTestRule = new ForceLocaleRule(Locale.US);

    @Rule
    public ActivityTestRule<MainActivity> myActivityRule = new ActivityTestRule<>(MainActivity.class);

    private Context context;

    @Before
    public void setUp() {
        context = InstrumentationRegistry.getTargetContext();
    }

    @Test
    public void testAirplaneEn() {
        assertEquals("airplane", context.getString(R.string.airplane));
    }

    @Test
    public void testAirplaneEnOnView() {
        onView(withId(R.id.text_view))
                .perform(setTextInTextView(context.getString(R.string.airplane)),
                        closeSoftKeyboard());
        onView(withId(R.id.text_view))
                .check(matches(withText(containsString("airplane"))));
    }

}

英国测试:

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Locale;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static example.com.testlocale.MyActions.setTextInTextView;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;

@RunWith(AndroidJUnit4.class)
public class MainActivityGbTest {

    @ClassRule
    public static final ForceLocaleRule localeTestRule = new ForceLocaleRule(Locale.UK);

    @Rule
    public ActivityTestRule<MainActivity> myActivityRule = new ActivityTestRule<>(MainActivity.class);

    private Context context;

    @Before
    public void setUp() {
        context = InstrumentationRegistry.getTargetContext();
    }

    @Test
    public void testAirplaneEnGB() {
        assertEquals("aeroplane", context.getString(R.string.airplane));
    }

    @Test
    public void testAirplaneEnOnView() {
        onView(withId(R.id.text_view))
                .perform(setTextInTextView(context.getString(R.string.airplane)),
                        closeSoftKeyboard());
        onView(withId(R.id.text_view))
                .check(matches(withText(containsString("aeroplane"))));
    }

}

我的 Action :

import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.view.View;
import android.widget.TextView;

import org.hamcrest.Matcher;

import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static org.hamcrest.CoreMatchers.allOf;

public class MyActions {

    public static ViewAction setTextInTextView(final String value){
        return new ViewAction() {
            @SuppressWarnings("unchecked")
            @Override
            public Matcher<View> getConstraints() {
                return allOf(isDisplayed(), isAssignableFrom(TextView.class));
            }

            @Override
            public void perform(UiController uiController, View view) {
                ((TextView) view).setText(value);
            }

            @Override
            public String getDescription() {
                return "set text to TextView";
            }
        };
    }

}

values-en-rUS\strings.xml

<resources>
    <string name="app_name">TestLocale</string>
    <string name="airplane">airplane</string>
</resources>

values-en-rGB\strings.xml

<resources>
    <string name="app_name">TestLocale</string>
    <string name="airplane">aeroplane</string>
</resources>

关于android - 执行 Espresso 测试时更改语言环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53417037/

相关文章:

c# - C#中资源文件的动态引用

ios - 在运行时更改 iOS 模拟器的当前语言环境

java - 如何在Qt中使用JNI创建字符串数组

Beta 和 Alpha channel 中的 Android AdMob 真实广告单元 ID

php - 在 cakephp 中使用本地化

iphone - 支持 NSString 的国际化

java - 如何用Java构建短语语言翻译引擎

Android:如何找到 Assets 文件夹的绝对路径?

android - Qt Creator 5.8 Android开发NDK x64位错误 "No Rule to make target.."

email - 我如何大写电子邮件地址?