Skip to content

Commit

Permalink
Bind to bottomNavContainerFrame only if bottomNavItems not empty (#2589)
Browse files Browse the repository at this point in the history
* Bind to bottomNavContainerFrame only if bottomNavItems not empty

* Add tests for setShowNavigationInDefaultLongScroll
  • Loading branch information
LZRS authored Jun 27, 2024
1 parent e192970 commit be4221c
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,13 @@ class QuestionnaireFragment : Fragment() {
}

// Set bottom navigation
bottomNavContainerFrame.visibility = View.VISIBLE
NavigationViewHolder(bottomNavContainerFrame)
.bind(state.bottomNavItems.single().questionnaireNavigationUIState)
if (state.bottomNavItems.isNotEmpty()) {
bottomNavContainerFrame.visibility = View.VISIBLE
NavigationViewHolder(bottomNavContainerFrame)
.bind(state.bottomNavItems.single().questionnaireNavigationUIState)
} else {
bottomNavContainerFrame.visibility = View.GONE
}

// Hide progress indicator
questionnaireProgressIndicator.visibility = View.GONE
Expand All @@ -175,9 +179,13 @@ class QuestionnaireFragment : Fragment() {
reviewModeEditButton.visibility = View.GONE

// Set bottom navigation
bottomNavContainerFrame.visibility = View.VISIBLE
NavigationViewHolder(bottomNavContainerFrame)
.bind(state.bottomNavItems.single().questionnaireNavigationUIState)
if (state.bottomNavItems.isNotEmpty()) {
bottomNavContainerFrame.visibility = View.VISIBLE
NavigationViewHolder(bottomNavContainerFrame)
.bind(state.bottomNavItems.single().questionnaireNavigationUIState)
} else {
bottomNavContainerFrame.visibility = View.GONE
}

// Set progress indicator
questionnaireProgressIndicator.visibility = View.VISIBLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,61 @@ class QuestionnaireFragmentTest {
}
}

fun `show bottom navigation container when setShowNavigationInDefaultLongScroll is set to false`() {
val questionnaire =
Questionnaire().apply {
id = "a-questionnaire"
addItem(
Questionnaire.QuestionnaireItemComponent().apply {
linkId = "a-link-id"
type = Questionnaire.QuestionnaireItemType.BOOLEAN
},
)
}
val questionnaireJson = parser.encodeResourceToString(questionnaire)
val scenario =
launchFragment<QuestionnaireFragment>(
QuestionnaireFragment.builder()
.setQuestionnaire(questionnaireJson)
.setShowNavigationInDefaultLongScroll(false)
.buildArgs(),
)
scenario.moveToState(Lifecycle.State.RESUMED)
scenario.withFragment {
val bottomNavContainer = requireView().findViewById<View>(R.id.bottom_nav_container_frame)
assertThat(bottomNavContainer.visibility).isEqualTo(View.VISIBLE)
assertThat(bottomNavContainer.findViewById<View>(R.id.submit_questionnaire).isEnabled)
.isTrue()
}
}

@Test
fun `hide the bottom navigation container when setShowNavigationInDefaultLongScroll is set to true`() {
val questionnaire =
Questionnaire().apply {
id = "a-questionnaire"
addItem(
Questionnaire.QuestionnaireItemComponent().apply {
linkId = "a-link-id"
type = Questionnaire.QuestionnaireItemType.BOOLEAN
},
)
}
val questionnaireJson = parser.encodeResourceToString(questionnaire)
val scenario =
launchFragment<QuestionnaireFragment>(
QuestionnaireFragment.builder()
.setQuestionnaire(questionnaireJson)
.setShowNavigationInDefaultLongScroll(true)
.buildArgs(),
)
scenario.moveToState(Lifecycle.State.RESUMED)
scenario.withFragment {
val bottomNavContainer = requireView().findViewById<View>(R.id.bottom_nav_container_frame)
assertThat(bottomNavContainer.visibility).isEqualTo(View.GONE)
}
}

object QuestionnaireItemViewHolderFactoryMatchersProviderFactoryTestImpl :
QuestionnaireItemViewHolderFactoryMatchersProviderFactory {
override fun get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3193,9 +3193,38 @@ class QuestionnaireViewModelTest {
}
val viewModel = createQuestionnaireViewModel(questionnaire, showNavigationInLongScroll = true)
val questionnaireState = viewModel.questionnaireStateFlow.first()
println(questionnaireState.items)
assertThat(questionnaireState.bottomNavItems.isEmpty()).isTrue()
assertThat(questionnaireState.items.last())
.isInstanceOf(QuestionnaireAdapterItem.Navigation::class.java)
val navigationItem = questionnaireState.items.last() as QuestionnaireAdapterItem.Navigation
assertThat(navigationItem.questionnaireNavigationUIState.navSubmit.isEnabled).isTrue()
}

fun `EXTRA_SHOW_NAVIGATION_IN_DEFAULT_LONG_SCROLL not setting should not add navigation item to questionnaireState items`() =
runTest {
val questionnaire =
Questionnaire().apply {
id = "a-questionnaire"
addItem(
Questionnaire.QuestionnaireItemComponent().apply {
linkId = "a-linkId"
type = Questionnaire.QuestionnaireItemType.BOOLEAN
},
)
}
val viewModel = createQuestionnaireViewModel(questionnaire)
val questionnaireState = viewModel.questionnaireStateFlow.first()
assertThat(questionnaireState.items.map { it::class.java })
.doesNotContain(QuestionnaireAdapterItem.Navigation::class.java)
assertThat(questionnaireState.bottomNavItems.isNotEmpty()).isTrue()
assertThat(
questionnaireState.bottomNavItems
.single()
.questionnaireNavigationUIState
.navSubmit
.isEnabled,
)
.isTrue()
}

// ==================================================================== //
Expand Down

0 comments on commit be4221c

Please sign in to comment.