android / android/architecture-samples
[dev-hilt] How test Fragment which configures its Toolbar with NavigationUI
- Dominant language
- Kotlin
- Stars
- 45.8k
- Forks
- 11.9k
- PR merge metrics
- No merged PRs in 30d
Description
We are able provide `NavHostController` to `FragmentScenario` for the Fragments which configures `Toolbar` by using `NavController`.
We have `HiltExt.kt` workaround instead of `FragmentScenario` at the moment and it does not support this.
So, we need to provide `NavHostController` order to be able test the fragments which configure its Toolbar by using NavController.
https://developer.android.com/guide/navigation/navigation-testing#test_fragment_navigation
```
val scenario = launchFragmentInContainer {
TitleScreen().also { fragment ->
// In addition to returning a new instance of our Fragment,
// get a callback whenever the fragment’s view is created
// or destroyed so that we can set the NavController
fragment.viewLifecycleOwnerLiveData.observeForever { viewLifecycleOwner ->
if (viewLifecycleOwner != null) {
// The fragment’s view has just been created
Navigation.setViewNavController(fragment.requireView(), navController)
}
}
}
}
```
`HiltExt.kt` may cover this.
Contributor guide
Research direction
Start by reading HiltExt.kt and the linked Navigation testing guidance, then trace how the current workaround launches fragments. Confirm how FragmentScenario supplies a NavHostController for fragments whose Toolbar uses NavigationUI. Done means the helper supports this setup and a fragment test demonstrates it.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, kotlin
- Domain
- mobile-dev, testing
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100