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/
我错过了什么吗?
解决方案
我使用您为美国和英国提供的链接创建了一个小型测试项目,主要课程在答案下方,但它是一个公共项目,因此您可以下载它。
对于“AE”,您需要在下面创建一个(请参阅此链接)。编辑:为每种语言和 MyActions 类添加了另一个测试。
致谢:MyActions 来自这里,测试示例来自这里,您可能需要从开发人员设置中停止动画(来自第二个链接和这里)strings.xml
values-ar-rAE
ForceLocale规则:
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.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"))));
}
}
我的行动:
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";
}
};
}
}
值-en-rUS\strings.xml
<resources>
<string name="app_name">TestLocale</string>
<string name="airplane">airplane</string>
</resources>
值-en-rGB\strings.xml
<resources>
<string name="app_name">TestLocale</string>
<string name="airplane">aeroplane</string>
</resources>
推荐阅读
- c# - 我在 Visual Studio 中收到一个 SqlException,将数字数据转换为数字 //ASP.NET 项目(第二次上传)
- qt - 如何防止伽马射线?
- ios - 如何从 iOS 将视频投射到三星 SmartTV
- java - OpenAPI 3 中是否已弃用模式验证?
- javascript - 反应只听第一个状态变化
- javascript - 如何从计算属性中使用 v-for?
- c# - 第一次调用后,对不同数据库上下文的 EF 查询似乎会缓存
- apache-nifi - JOLT - 转换成输出数组
- reactjs - React Router 与 Spring Boot 路由冲突
- python - 使用 QDesktopServices.openUrl() 打开由 QWebEnginePage::createWindow() 创建的窗口