我正在创建一个简单的布局,该布局应支持阿拉伯语和RTL布局。一切正常。现在,我想编写一个Espresso测试,并断定它实际上是否在显示翻译的文本。例如对于阿拉伯语言,应显示阿拉伯字符串.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/
以外,我没有做任何其他事情。我错过了什么吗?
答案 0 :(得分:3)
我已经使用small test project为美国和英国创建了一个link you provided,主要类别的答案很低,但这是一个公共项目,因此您只需download it。 br />
对于“ AE”,您需要在strings.xml
(see this link)下创建一个values-ar-rAE
。
编辑:为每种语言和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.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";
}
};
}
}
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>
答案 1 :(得分:0)
对于在切换到androidx之后遇到Locale
更改问题的任何人:androidx.appcompat:appcompat:1.1.0
目前存在错误,但可以通过降级为1.0.2
或添加{{3}来解决}在您的AppCompatActivity
中的解决方案。刚刚在Espresso单元测试中进行了测试,一切都很好。
@Override
public void applyOverrideConfiguration(Configuration overrideConfiguration) {
if (overrideConfiguration != null) {
int uiMode = overrideConfiguration.uiMode;
overrideConfiguration.setTo(getBaseContext().getResources().getConfiguration());
overrideConfiguration.uiMode = uiMode;
}
super.applyOverrideConfiguration(overrideConfiguration);
}