Last active
August 3, 2022 22:30
-
-
Save AfzalivE/8a4122870ae06d7d41a9ca4e366d8707 to your computer and use it in GitHub Desktop.
Disable soft-keyboards for tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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