android / android/architecture-samples

[dev-hilt] Example instrumentation test using navGraphViewModels, FragmentScenario and launchFragmentInHiltContainer

Open
#759 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Kotlin
Stars
45.8k
Forks
11.9k
PR merge metrics
No merged PRs in 30d

Description

Hello,

Could you add an example instrumentation tests which uses:

* navGraphViewModels
* FragmentScenario
* launchFragmentInHiltContainer
* databinding where the nav graph scoped viewmodel is set into the viewbinding

I cannot get this combination to work in an instrumentation test because I cannot set the navcontroller early enough. It is still null when onViewCreated is called. When viewBinding.viewModel = myViewModel is done in the code snippet below, findNavController will eventually be called but I have not been able to set it via Navigation.setViewNavController(...)

Fragment contains
`private val myViewModel: MyViewModel by navGraphViewModels(R.id.my_nav_graph) { defaultViewModelProviderFactory }`

`
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
/* Sets the {@link LifecycleOwner} that should be used for observing changes of
* LiveData in this binding. If a {@link LiveData} is in one of the binding expressions
* and no LifecycleOwner is set, the LiveData will not be observed and updates to it
* will not be propagated to the UI. */
viewBinding.lifecycleOwner = this
viewBinding.viewModel = myViewModel // pass in the view model
}
`

Contributor guide

Open the contributing guide

Research direction

Start by locating the dev-hilt sample and its instrumentation-test setup, then reproduce the lifecycle problem described in the issue with navGraphViewModels, FragmentScenario, launchFragmentInHiltContainer, and databinding. Done means the sample test demonstrates this combination with the nav-graph-scoped ViewModel available to the view binding.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
mobile, testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.