test-butler
test-butler copied to clipboard
Global locale changer
Could you please implement possibility of changing global locale like it is done in Custom Locale app
It may be used in tests which use locale-dependent WebView
I believe the library already does application-scoped locale changing already. Does this help your use-case or does it require device-scoped locale?
https://github.com/linkedin/test-butler/blob/master/test-butler-library/src/main/java/com/linkedin/android/testbutler/TestButler.java#L221
WebView does not use application-scoped locale, it uses device-scoped. I have tried to use method you have mentioned, but it does not take affect
Have you tried doing the locale change in setUp()
? Could be that the locale needs to be changed before the activity is loaded.
@Kisty Yep - tried in custom Runner
That's strange. How are you getting the locale in the WebView document?
@Kisty
url[0] = webView.getUrl();
And it returns url with device-scoped locale
Also, it looks like the way WebView
behaves on Android 7.0 has changed. To test this out, can you try running the app on a API 23 emulator?
The Solution:
The only way to solve this problem is to reset the default locale manually in every Activity, or at least every Activity that has a WebView.
public static void setLocale(Locale locale){ Locale.setDefault(locale); Context context = MusafirApplication.getInstance(); final Resources resources = context.getResources(); final Configuration config = resources.getConfiguration(); config.setLocale(locale); context.getResources().updateConfiguration(config, resources.getDisplayMetrics()); }
Call the above method before calling setContentView(...) in the onCreate() method of all your Activities.
As an addendum, Chrome custom tabs are now the preferred way of rendering in-app web pages.
http://stackoverflow.com/a/40675539/818821
Before open sourcing Test Butler, we had tried setting the global locale and found it to be extremely flaky. That said, I don't remember the details of how we were setting it and this was before Test Butler was signed as a system-level app (so there may be alternate approaches that are now available to us).
I'm open to trying to add this again. Not sure when I'll have time to get to it, so if someone is interested in picking this up, let me know.