test-butler icon indicating copy to clipboard operation
test-butler copied to clipboard

Global locale changer

Open SKART1 opened this issue 7 years ago • 8 comments

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

SKART1 avatar Mar 13 '17 09:03 SKART1

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

Kisty avatar Mar 13 '17 12:03 Kisty

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

SKART1 avatar Mar 13 '17 12:03 SKART1

Have you tried doing the locale change in setUp()? Could be that the locale needs to be changed before the activity is loaded.

Kisty avatar Mar 13 '17 13:03 Kisty

@Kisty Yep - tried in custom Runner

SKART1 avatar Mar 13 '17 13:03 SKART1

That's strange. How are you getting the locale in the WebView document?

Kisty avatar Mar 13 '17 13:03 Kisty

@Kisty url[0] = webView.getUrl();

And it returns url with device-scoped locale

SKART1 avatar Mar 13 '17 13:03 SKART1

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

Kisty avatar Mar 13 '17 13:03 Kisty

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.

drewhannay avatar Mar 13 '17 15:03 drewhannay