Created
August 20, 2023 01:10
-
-
Save AfzalivE/bb61447abb986697db8dca5d85d1bb13 to your computer and use it in GitHub Desktop.
AndroidView NumberPicker type in test
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
java.lang.IllegalStateException: No input session started. Missing a focus? | |
at androidx.compose.ui.test.AndroidComposeUiTestEnvironment$AndroidTestOwner$performTextInput$1.invoke(ComposeUiTest.android.kt:551) | |
at androidx.compose.ui.test.AndroidComposeUiTestEnvironment$AndroidTestOwner$performTextInput$1.invoke(ComposeUiTest.android.kt:549) | |
at androidx.compose.ui.test.junit4.AndroidSynchronization_androidKt.runOnUiThread$lambda$0(AndroidSynchronization.android.kt:37) | |
at androidx.compose.ui.test.junit4.AndroidSynchronization_androidKt.$r8$lambda$SUUsAclwsAzFrTMk57EPm0y6pV0(Unknown Source:0) | |
at androidx.compose.ui.test.junit4.AndroidSynchronization_androidKt$$ExternalSyntheticLambda0.call(Unknown Source:2) | |
at java.util.concurrent.FutureTask.run(FutureTask.java:266) | |
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462) | |
at java.util.concurrent.FutureTask.run(FutureTask.java:266) | |
at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:2296) | |
at android.os.Handler.handleCallback(Handler.java:938) | |
at android.os.Handler.dispatchMessage(Handler.java:99) | |
at android.os.Looper.loopOnce(Looper.java:201) | |
at android.os.Looper.loop(Looper.java:288) | |
at android.app.ActivityThread.main(ActivityThread.java:7870) | |
at java.lang.reflect.Method.invoke(Native Method) | |
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) | |
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) |
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.widget.NumberPicker | |
import android.widget.Toast | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.platform.LocalContext | |
import androidx.compose.ui.semantics.semantics | |
import androidx.compose.ui.semantics.setText | |
import androidx.compose.ui.tooling.preview.Preview | |
import androidx.compose.ui.viewinterop.AndroidView | |
@Composable | |
fun NumberPicker( | |
value: Int, | |
values: IntRange, | |
onValueChange: (Int) -> Unit, | |
modifier: Modifier = Modifier, | |
) { | |
AndroidView( | |
modifier = modifier | |
.testTag("number_picker") | |
.semantics { | |
setText { | |
val textValue = it.text.toIntOrNull() ?: return@setText false | |
onValueChange(textValue) | |
true | |
} | |
}, | |
factory = { context -> | |
NumberPicker(context).apply { | |
setOnValueChangedListener { picker, _, _ -> | |
onValueChange(picker.value) | |
} | |
minValue = values.first | |
maxValue = values.last | |
} | |
}, | |
update = { numberPicker -> | |
numberPicker.value = value | |
}, | |
) | |
} | |
@Suppress("MagicNumber") | |
@Preview | |
@Composable | |
private fun NumberPickerPreview() { | |
val context = LocalContext.current | |
NumberPicker( | |
value = 5, | |
values = (0..23), | |
onValueChange = { Toast.makeText(context, "$it", Toast.LENGTH_SHORT).show() }, | |
) | |
} |
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.compose.ui.test.hasTestTag | |
import androidx.compose.ui.test.junit4.ComposeTestRule | |
import androidx.compose.ui.test.performClick | |
import androidx.compose.ui.test.performTextInput | |
fun ComposeTestRule.setMinutes(minutes: Int) { | |
onNode(hasTestTag(TEST_TAG_HOUR_NUMBER_PICKER)).performClick() | |
.performTextInput(minutes.toString()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment