Change locale while executing Espresso test
I am creating a simple layout that should support arabic language and RTL layout. Everything works fine. Now I want to write an Espresso test and assert weather it's actually showing the translated text or not. e.g. For arabic language it should display text from arabic strings.xml.
So far I tried below code as a 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());
}
The above code changes the layout direction but doesn't load resources from the localised directory.
I am not doing anything extra but something like http://www.andreamaglie.com/2016/a-test-rule-for-setting-device-locale/
Am I missing anything?
android localization internationalization android-espresso
add a comment |
I am creating a simple layout that should support arabic language and RTL layout. Everything works fine. Now I want to write an Espresso test and assert weather it's actually showing the translated text or not. e.g. For arabic language it should display text from arabic strings.xml.
So far I tried below code as a 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());
}
The above code changes the layout direction but doesn't load resources from the localised directory.
I am not doing anything extra but something like http://www.andreamaglie.com/2016/a-test-rule-for-setting-device-locale/
Am I missing anything?
android localization internationalization android-espresso
How do you construct thelocale
?
– azizbekian
Dec 12 '18 at 10:36
@azizbekian like this Locale("ar", "AE")
– Hardik Trivedi
Dec 12 '18 at 11:03
add a comment |
I am creating a simple layout that should support arabic language and RTL layout. Everything works fine. Now I want to write an Espresso test and assert weather it's actually showing the translated text or not. e.g. For arabic language it should display text from arabic strings.xml.
So far I tried below code as a 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());
}
The above code changes the layout direction but doesn't load resources from the localised directory.
I am not doing anything extra but something like http://www.andreamaglie.com/2016/a-test-rule-for-setting-device-locale/
Am I missing anything?
android localization internationalization android-espresso
I am creating a simple layout that should support arabic language and RTL layout. Everything works fine. Now I want to write an Espresso test and assert weather it's actually showing the translated text or not. e.g. For arabic language it should display text from arabic strings.xml.
So far I tried below code as a 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());
}
The above code changes the layout direction but doesn't load resources from the localised directory.
I am not doing anything extra but something like http://www.andreamaglie.com/2016/a-test-rule-for-setting-device-locale/
Am I missing anything?
android localization internationalization android-espresso
android localization internationalization android-espresso
asked Nov 21 '18 at 16:55
Hardik TrivediHardik Trivedi
3,66312145
3,66312145
How do you construct thelocale
?
– azizbekian
Dec 12 '18 at 10:36
@azizbekian like this Locale("ar", "AE")
– Hardik Trivedi
Dec 12 '18 at 11:03
add a comment |
How do you construct thelocale
?
– azizbekian
Dec 12 '18 at 10:36
@azizbekian like this Locale("ar", "AE")
– Hardik Trivedi
Dec 12 '18 at 11:03
How do you construct the
locale
?– azizbekian
Dec 12 '18 at 10:36
How do you construct the
locale
?– azizbekian
Dec 12 '18 at 10:36
@azizbekian like this Locale("ar", "AE")
– Hardik Trivedi
Dec 12 '18 at 11:03
@azizbekian like this Locale("ar", "AE")
– Hardik Trivedi
Dec 12 '18 at 11:03
add a comment |
1 Answer
1
active
oldest
votes
I've create a small test project using the link you provided for US and UK, the main classes are further down the answer, but it's a public project so you can just download it.
For 'AE' you will need to create a strings.xml
under values-ar-rAE
(see this link).
Edit: added another test for each language and MyActions class.
Credits: MyActions is from here, tests examples are from here, you might need to stop animation from the developer settings (from second link and 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());
}
}
US test:
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"))));
}
}
UK test:
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"))));
}
}
MyActions:
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-rUSstrings.xml
<resources>
<string name="app_name">TestLocale</string>
<string name="airplane">airplane</string>
</resources>
values-en-rGBstrings.xml
<resources>
<string name="app_name">TestLocale</string>
<string name="airplane">aeroplane</string>
</resources>
Thanks for checking this. Everything looks fine. But does this work same when I do assertion on view which shows value from strings.xml. e.g. Let's say there is a fragment/activity which displays @string/airplane on some TextView. If I do view assertion using espresso does it fetch correct locale through this force locale?
– Hardik Trivedi
Dec 18 '18 at 17:08
@HardikTrivedi I've edited my answer with more tests, it does not seem to load the text originally, but once you set the text it is correct, hope it's what you are looking for
– MikeL
Dec 18 '18 at 17:56
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417037%2fchange-locale-while-executing-espresso-test%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I've create a small test project using the link you provided for US and UK, the main classes are further down the answer, but it's a public project so you can just download it.
For 'AE' you will need to create a strings.xml
under values-ar-rAE
(see this link).
Edit: added another test for each language and MyActions class.
Credits: MyActions is from here, tests examples are from here, you might need to stop animation from the developer settings (from second link and 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());
}
}
US test:
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"))));
}
}
UK test:
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"))));
}
}
MyActions:
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-rUSstrings.xml
<resources>
<string name="app_name">TestLocale</string>
<string name="airplane">airplane</string>
</resources>
values-en-rGBstrings.xml
<resources>
<string name="app_name">TestLocale</string>
<string name="airplane">aeroplane</string>
</resources>
Thanks for checking this. Everything looks fine. But does this work same when I do assertion on view which shows value from strings.xml. e.g. Let's say there is a fragment/activity which displays @string/airplane on some TextView. If I do view assertion using espresso does it fetch correct locale through this force locale?
– Hardik Trivedi
Dec 18 '18 at 17:08
@HardikTrivedi I've edited my answer with more tests, it does not seem to load the text originally, but once you set the text it is correct, hope it's what you are looking for
– MikeL
Dec 18 '18 at 17:56
add a comment |
I've create a small test project using the link you provided for US and UK, the main classes are further down the answer, but it's a public project so you can just download it.
For 'AE' you will need to create a strings.xml
under values-ar-rAE
(see this link).
Edit: added another test for each language and MyActions class.
Credits: MyActions is from here, tests examples are from here, you might need to stop animation from the developer settings (from second link and 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());
}
}
US test:
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"))));
}
}
UK test:
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"))));
}
}
MyActions:
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-rUSstrings.xml
<resources>
<string name="app_name">TestLocale</string>
<string name="airplane">airplane</string>
</resources>
values-en-rGBstrings.xml
<resources>
<string name="app_name">TestLocale</string>
<string name="airplane">aeroplane</string>
</resources>
Thanks for checking this. Everything looks fine. But does this work same when I do assertion on view which shows value from strings.xml. e.g. Let's say there is a fragment/activity which displays @string/airplane on some TextView. If I do view assertion using espresso does it fetch correct locale through this force locale?
– Hardik Trivedi
Dec 18 '18 at 17:08
@HardikTrivedi I've edited my answer with more tests, it does not seem to load the text originally, but once you set the text it is correct, hope it's what you are looking for
– MikeL
Dec 18 '18 at 17:56
add a comment |
I've create a small test project using the link you provided for US and UK, the main classes are further down the answer, but it's a public project so you can just download it.
For 'AE' you will need to create a strings.xml
under values-ar-rAE
(see this link).
Edit: added another test for each language and MyActions class.
Credits: MyActions is from here, tests examples are from here, you might need to stop animation from the developer settings (from second link and 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());
}
}
US test:
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"))));
}
}
UK test:
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"))));
}
}
MyActions:
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-rUSstrings.xml
<resources>
<string name="app_name">TestLocale</string>
<string name="airplane">airplane</string>
</resources>
values-en-rGBstrings.xml
<resources>
<string name="app_name">TestLocale</string>
<string name="airplane">aeroplane</string>
</resources>
I've create a small test project using the link you provided for US and UK, the main classes are further down the answer, but it's a public project so you can just download it.
For 'AE' you will need to create a strings.xml
under values-ar-rAE
(see this link).
Edit: added another test for each language and MyActions class.
Credits: MyActions is from here, tests examples are from here, you might need to stop animation from the developer settings (from second link and 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());
}
}
US test:
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"))));
}
}
UK test:
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"))));
}
}
MyActions:
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-rUSstrings.xml
<resources>
<string name="app_name">TestLocale</string>
<string name="airplane">airplane</string>
</resources>
values-en-rGBstrings.xml
<resources>
<string name="app_name">TestLocale</string>
<string name="airplane">aeroplane</string>
</resources>
edited Dec 18 '18 at 18:03
answered Dec 16 '18 at 14:38
MikeLMikeL
7551722
7551722
Thanks for checking this. Everything looks fine. But does this work same when I do assertion on view which shows value from strings.xml. e.g. Let's say there is a fragment/activity which displays @string/airplane on some TextView. If I do view assertion using espresso does it fetch correct locale through this force locale?
– Hardik Trivedi
Dec 18 '18 at 17:08
@HardikTrivedi I've edited my answer with more tests, it does not seem to load the text originally, but once you set the text it is correct, hope it's what you are looking for
– MikeL
Dec 18 '18 at 17:56
add a comment |
Thanks for checking this. Everything looks fine. But does this work same when I do assertion on view which shows value from strings.xml. e.g. Let's say there is a fragment/activity which displays @string/airplane on some TextView. If I do view assertion using espresso does it fetch correct locale through this force locale?
– Hardik Trivedi
Dec 18 '18 at 17:08
@HardikTrivedi I've edited my answer with more tests, it does not seem to load the text originally, but once you set the text it is correct, hope it's what you are looking for
– MikeL
Dec 18 '18 at 17:56
Thanks for checking this. Everything looks fine. But does this work same when I do assertion on view which shows value from strings.xml. e.g. Let's say there is a fragment/activity which displays @string/airplane on some TextView. If I do view assertion using espresso does it fetch correct locale through this force locale?
– Hardik Trivedi
Dec 18 '18 at 17:08
Thanks for checking this. Everything looks fine. But does this work same when I do assertion on view which shows value from strings.xml. e.g. Let's say there is a fragment/activity which displays @string/airplane on some TextView. If I do view assertion using espresso does it fetch correct locale through this force locale?
– Hardik Trivedi
Dec 18 '18 at 17:08
@HardikTrivedi I've edited my answer with more tests, it does not seem to load the text originally, but once you set the text it is correct, hope it's what you are looking for
– MikeL
Dec 18 '18 at 17:56
@HardikTrivedi I've edited my answer with more tests, it does not seem to load the text originally, but once you set the text it is correct, hope it's what you are looking for
– MikeL
Dec 18 '18 at 17:56
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417037%2fchange-locale-while-executing-espresso-test%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How do you construct the
locale
?– azizbekian
Dec 12 '18 at 10:36
@azizbekian like this Locale("ar", "AE")
– Hardik Trivedi
Dec 12 '18 at 11:03