Status Update
Comments
ba...@gmail.com <ba...@gmail.com> #2
ma...@gmail.com <ma...@gmail.com> #3
Hoping for an update, just migrated over to type safe navigation and its great but having to convert all of our view model tests to instrumented tests is rough. If this can't be resolved is there any other solutions that don't require going to instrumented tests?
se...@google.com <se...@google.com> #4
I use backStackEntry.toRoute<T>() for now to get the destination and then pass the arguments to the viewModel via DI.
@Serializable data class Home(val argument1: String, val argument2: String)
@Composable fun HomeScreen( destination: Home, viewModel: HomeViewModel = koinViewModel { parametersOf(destination.argument1, destination.argument2) } ) { // content() }
ma...@gmail.com <ma...@gmail.com> #5
Temporary solution can be mocking the actual extension, this way it will never get to the moment of using bundle
@Before
fun before() {
mockkStatic("androidx.navigation.SavedStateHandleKt")
every { savedStateHandle.toRoute<SomeArgument>() } returns SomeArgument("some value")
}
@After
fun after() {
unmockkStatic("androidx.navigation.SavedStateHandleKt")
}
jo...@olavstoppen.no <jo...@olavstoppen.no> #6
This works great, thank you!
ka...@gmail.com <ka...@gmail.com> #7
Based on the previous temporary solution suggested here I created this Rule:
class SavedStateHandleRule(
private val route: Any,
) : TestWatcher() {
val savedStateHandleMock: SavedStateHandle = mockk()
override fun starting(description: Description?) {
mockkStatic("androidx.navigation.SavedStateHandleKt")
every { savedStateHandleMock.internalToRoute<Any>(any(), any()) } returns route
super.starting(description)
}
override fun finished(description: Description?) {
unmockkStatic("androidx.navigation.SavedStateHandleKt")
super.finished(description)
}
}
Usage
class ViewModelTest {
private val route = Route("foo")
@get:Rule
val savedStateHandleRule = SavedStateHandleRule(route)
private val viewModel: ViewModel get() = ViewModel(savedStateHandleRule.savedStateHandleMock)
dp...@phunware.com <dp...@phunware.com> #8
pa...@gmail.com <pa...@gmail.com> #9
dp...@phunware.com <dp...@phunware.com> #10
This issue is due to the need to call NavType.put
and NavType.get
internally when handling SavedState. When SavedState was commonized recently in 2.9.0, its Android implementation was delegated to Bundle. So in the end, SavedState in android unit test is still dealing with Bundle. This means there is still no viable solution to completely Android dependency.
However, this does mean that we can make the SavedStateHandle
test api common-friendly.
ae...@google.com <ae...@google.com> #11
Duplicate bug
"I managed to fix it by adding following Window flag to the createLayoutParams
function
flags = flags or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM
"
Description
Jetpack Compose version: 1.3.1
Jetpack Compose component used: Popup
Android Studio Build:#AI-222.4345.14.2221.9228443
Kotlin version: 1.8
Steps to Reproduce or Code Sample to Reproduce: