Skip to content

Commit

Permalink
Configurable drop down answer option.
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaKBhadragond14 committed Apr 23, 2024
1 parent 6ff0643 commit 9cad51f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Google LLC
* Copyright 2023-2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -349,6 +349,33 @@ class DropDownViewHolderFactoryEspressoTest {
assertThat((answerHolder!!.single().value as Reference).id).isEqualTo("ref_2")
}

@Test
fun shouldSetDefaultValueFromInitialProperty() {
val questionnaireViewItem =
QuestionnaireViewItem(
answerOptions("Coding 1", "Coding 2", "Coding 3", "Coding 4", "Coding 5"),
responseOptions(),
validationResult = NotValidated,
answersChangedCallback = { _, _, _, _ -> },
)
questionnaireViewItem.questionnaireItem.addInitial(
Questionnaire.QuestionnaireItemInitialComponent().apply {
this.value = StringType("Select Answer")
},
)
runOnUI {
viewHolder.bind(questionnaireViewItem)
viewHolder.itemView.findViewById<AutoCompleteTextView>(R.id.auto_complete).showDropDown()
}
onView(withId(R.id.auto_complete)).perform(delayMainThread())
onView(withText("Select Answer"))
.inRoot(isPlatformPopup())
.check(matches(isDisplayed()))
.perform(click())
assertThat(viewHolder.itemView.findViewById<TextView>(R.id.auto_complete).text.toString())
.isEqualTo("Select Answer")
}

/** Method to run code snippet on UI/main thread */
private fun runOnUI(action: () -> Unit) {
activityScenarioRule.scenario.onActivity { action() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ internal object DropDownViewHolderFactory :
hint = questionnaireViewItem.enabledDisplayItems.localizedFlyoverSpanned
helperText = getRequiredOrOptionalText(questionnaireViewItem, context)
}
val initialAnswerString =
if (questionnaireViewItem.questionnaireItem.hasInitial()) {
questionnaireViewItem.questionnaireItem.initial.first().valueStringType.valueAsString
} else {
context.getString(R.string.hyphen)
}
val answerOptionList =
this.questionnaireViewItem.enabledAnswerOptions
.map {
Expand All @@ -80,8 +86,8 @@ internal object DropDownViewHolderFactory :
answerOptionList.add(
0,
DropDownAnswerOption(
context.getString(R.string.hyphen),
context.getString(R.string.hyphen),
initialAnswerString,
initialAnswerString,
null,
),
)
Expand Down

0 comments on commit 9cad51f

Please sign in to comment.