Skip to content

Instantly share code, notes, and snippets.

@AfzalivE
Last active August 3, 2022 22:30
Show Gist options
  • Save AfzalivE/8a4122870ae06d7d41a9ca4e366d8707 to your computer and use it in GitHub Desktop.
Save AfzalivE/8a4122870ae06d7d41a9ca4e366d8707 to your computer and use it in GitHub Desktop.
Disable soft-keyboards for tests
import androidx.test.runner.AndroidJUnitRunner
class CustomJunitRunner : AndroidJUnitRunner() {
private var enabledImes = emptyList<String>()
override fun callApplicationOnCreate(app: Application?) {
super.callApplicationOnCreate(app)
enabledImes = getEnabledImes()
disableSoftKeyboards(enabledImes)
}
override fun finish(resultCode: Int, results: Bundle?) {
enableSoftKeyboards(enabledImes)
super.finish(resultCode, results)
}
}
import android.app.Instrumentation
import android.os.ParcelFileDescriptor
import androidx.test.platform.app.InstrumentationRegistry
val instrumentation: Instrumentation
get() = InstrumentationRegistry.getInstrumentation()
fun disableSoftKeyboards(enabledImes: List<String>) {
enabledImes.forEach {
instrumentation.uiAutomation.executeShellCommand("ime disable $it")
}
}
fun enableSoftKeyboards(enabledImes: List<String>) {
enabledImes.forEach {
instrumentation.uiAutomation.executeShellCommand("ime enable $it")
}
}
fun getEnabledImes(): List<String> {
val fileDescriptor = instrumentation.uiAutomation.executeShellCommand("ime list -s")
val inputStream = ParcelFileDescriptor.AutoCloseInputStream(fileDescriptor)
return inputStream.bufferedReader().lineSequence().toList()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment