Har*_*edi 5 android localization internationalization android-espresso
我正在创建一个应该支持阿拉伯语言和RTL布局的简单布局.一切正常.现在我想写一个Espresso测试并断言天气它实际上是否显示翻译的文本.例如,对于阿拉伯语言,它应该显示来自arabic 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());
}
Run Code Online (Sandbox Code Playgroud)
上面的代码更改了布局方向,但不从本地化目录加载资源.
我没有做任何额外的事情,但像http://www.andreamaglie.com/2016/a-test-rule-for-setting-device-locale/
我错过了什么吗?
我使用您为美国和英国提供的链接创建了一个小型测试项目,主要课程在答案的后面,但它是一个公共项目,因此您只需下载即可。
对于“AE”,您需要创建一个底部(请参阅此链接)。编辑:为每种语言和 MyActions 类添加了另一个测试。
积分:MyActions 来自此处,测试示例来自此处,您可能需要从开发人员设置中停止动画(来自第二个链接和此处)strings.xml
values-ar-rAE
强制区域规则:
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());
}
}
Run Code Online (Sandbox Code Playgroud)
美国测试:
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"))));
}
}
Run Code Online (Sandbox Code Playgroud)
英国测试:
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"))));
}
}
Run Code Online (Sandbox Code Playgroud)
我的行动:
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";
}
};
}
}
Run Code Online (Sandbox Code Playgroud)
values-en-rUS\strings.xml
<resources>
<string name="app_name">TestLocale</string>
<string name="airplane">airplane</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
值-en-rGB\strings.xml
<resources>
<string name="app_name">TestLocale</string>
<string name="airplane">aeroplane</string>
</resources>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
995 次 |
最近记录: |