|
|
|
@ -356,64 +356,60 @@ object AppAndSystemHelper {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Changes the default language of the entire device, not just the app.
|
|
|
|
|
* Runs on Debug variant as we don't want to adjust Release permission manifests
|
|
|
|
|
* Runs the test in its testBlock.
|
|
|
|
|
* Cleans up and sets the default locale after it's done.
|
|
|
|
|
* As a safety measure, always add the resetSystemLocaleToEnUS() method in the tearDown method of your Class.
|
|
|
|
|
*/
|
|
|
|
|
fun runWithSystemLocaleChanged(locale: Locale, testRule: ActivityTestRule<HomeActivity>, testBlock: () -> Unit) {
|
|
|
|
|
val defaultLocale = Locale.getDefault()
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
setSystemLocale(locale)
|
|
|
|
|
testBlock()
|
|
|
|
|
ThreadUtils.runOnUiThread { testRule.activity.recreate() }
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
} finally {
|
|
|
|
|
setSystemLocale(defaultLocale)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Changes the default language of the entire device, not just the app.
|
|
|
|
|
* We can only use this if we're running on a debug build, otherwise it will change the permission manifests in release builds.
|
|
|
|
|
*/
|
|
|
|
|
fun setSystemLocale(locale: Locale) {
|
|
|
|
|
if (Config.channel.isDebug) {
|
|
|
|
|
/* Sets permission to change device language */
|
|
|
|
|
Log.i(
|
|
|
|
|
TAG,
|
|
|
|
|
"setSystemLocale: Requesting permission to change system locale to $locale.",
|
|
|
|
|
)
|
|
|
|
|
PermissionRequester().apply {
|
|
|
|
|
addPermissions(
|
|
|
|
|
Manifest.permission.CHANGE_CONFIGURATION,
|
|
|
|
|
)
|
|
|
|
|
requestPermissions()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val defaultLocale = Locale.getDefault()
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
setSystemLocale(locale)
|
|
|
|
|
testBlock()
|
|
|
|
|
ThreadUtils.runOnUiThread { testRule.activity.recreate() }
|
|
|
|
|
} catch (e: Exception) {
|
|
|
|
|
e.printStackTrace()
|
|
|
|
|
} finally {
|
|
|
|
|
setSystemLocale(defaultLocale)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resets the default language of the entire device back to EN-US.
|
|
|
|
|
* In case of a test instrumentation crash, the finally statement in the
|
|
|
|
|
* runWithSystemLocaleChanged(locale: Locale) method, will not be reached.
|
|
|
|
|
* Add this method inside the tearDown method of your test class, where the above method is used.
|
|
|
|
|
* Note: If set inside the ActivityTestRule's afterActivityFinished() method, this also won't work,
|
|
|
|
|
* as the methods inside it are not always executed: https://github.com/android/android-test/issues/498
|
|
|
|
|
*/
|
|
|
|
|
fun resetSystemLocaleToEnUS() {
|
|
|
|
|
if (Locale.getDefault() != Locale.US) {
|
|
|
|
|
Log.i(TAG, "Resetting system locale to EN US")
|
|
|
|
|
setSystemLocale(Locale.US)
|
|
|
|
|
Log.i(
|
|
|
|
|
TAG,
|
|
|
|
|
"setSystemLocale: Received permission to change system locale to $locale.",
|
|
|
|
|
)
|
|
|
|
|
val activityManagerNative = Class.forName("android.app.ActivityManagerNative")
|
|
|
|
|
val am = activityManagerNative.getMethod("getDefault", *arrayOfNulls(0))
|
|
|
|
|
.invoke(activityManagerNative, *arrayOfNulls(0))
|
|
|
|
|
val config =
|
|
|
|
|
InstrumentationRegistry.getInstrumentation().context.resources.configuration
|
|
|
|
|
config.javaClass.getDeclaredField("locale")[config] = locale
|
|
|
|
|
config.javaClass.getDeclaredField("userSetLocale").setBoolean(config, true)
|
|
|
|
|
am.javaClass.getMethod(
|
|
|
|
|
"updateConfiguration",
|
|
|
|
|
Configuration::class.java,
|
|
|
|
|
).invoke(am, config)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Changes the default language of the entire device, not just the app.
|
|
|
|
|
*/
|
|
|
|
|
fun setSystemLocale(locale: Locale) {
|
|
|
|
|
val activityManagerNative = Class.forName("android.app.ActivityManagerNative")
|
|
|
|
|
val am = activityManagerNative.getMethod("getDefault", *arrayOfNulls(0))
|
|
|
|
|
.invoke(activityManagerNative, *arrayOfNulls(0))
|
|
|
|
|
val config = InstrumentationRegistry.getInstrumentation().context.resources.configuration
|
|
|
|
|
config.javaClass.getDeclaredField("locale")[config] = locale
|
|
|
|
|
config.javaClass.getDeclaredField("userSetLocale").setBoolean(config, true)
|
|
|
|
|
am.javaClass.getMethod(
|
|
|
|
|
"updateConfiguration",
|
|
|
|
|
Configuration::class.java,
|
|
|
|
|
).invoke(am, config)
|
|
|
|
|
Log.i(
|
|
|
|
|
TAG,
|
|
|
|
|
"setSystemLocale: Changed system locale to $locale.",
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun putAppToBackground() {
|
|
|
|
|