Skip to content

Commit

Permalink
Merge pull request #229 from google/kj/add-radio-button-to-item-control
Browse files Browse the repository at this point in the history
Add radio button to item control
  • Loading branch information
joiskash authored Feb 10, 2021
2 parents 6db3dc5 + 9a03eb5 commit 3a15a9c
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package com.google.android.fhir.datacapture
import com.google.fhir.r4.core.Questionnaire

internal const val ITEM_CONTROL_DROP_DOWN = "drop-down"
internal const val ITEM_CONTROL_RADIO_BUTTON = "radio-button"

internal const val EXTENSION_ITEM_CONTROL_URL =
"http://hl7.org/fhir/StructureDefinition/questionnaire-itemControl"
internal const val EXTENSION_ITEM_CONTROL_SYSTEM =
Expand All @@ -35,6 +37,7 @@ internal val Questionnaire.Item.itemControl: String?
}?.code?.value
) {
ITEM_CONTROL_DROP_DOWN -> ITEM_CONTROL_DROP_DOWN
ITEM_CONTROL_RADIO_BUTTON -> ITEM_CONTROL_RADIO_BUTTON
else -> null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ internal class QuestionnaireItemAdapter :
QuestionnaireItemViewHolderType {
if (questionnaireItem.itemControl == ITEM_CONTROL_DROP_DOWN) {
return QuestionnaireItemViewHolderType.DROP_DOWN
} else if (questionnaireItem.itemControl == ITEM_CONTROL_RADIO_BUTTON) {
return QuestionnaireItemViewHolderType.RADIO_GROUP
} else if (
questionnaireItem.answerOptionCount >=
MINIMUM_NUMBER_OF_ANSWER_OPTIONS_FOR_DROP_DOWN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<com.google.android.material.textfield.TextInputLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:id="@+id/dropdown_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import org.robolectric.annotation.Config
class MoreQuestionnaireItemExtensionsTest {

@Test
fun itemControl_shouldReturnItemControlCode() {
fun itemControl_shouldReturnItemControlCodeDropDown() {

val questionnaireItem = Questionnaire.Item.newBuilder()
.setType(
Expand Down Expand Up @@ -77,6 +77,48 @@ class MoreQuestionnaireItemExtensionsTest {
assertThat(questionnaireItem.itemControl).isEqualTo(ITEM_CONTROL_DROP_DOWN)
}

@Test
fun itemControl_shouldReturnItemControlCodeRadioButton() {

val questionnaireItem = Questionnaire.Item.newBuilder()
.setType(
Questionnaire.Item.TypeCode.newBuilder()
.setValue(QuestionnaireItemTypeCode.Value.CHOICE)
)
.addExtension(
Extension.newBuilder()
.setUrl(
Uri.newBuilder()
.setValue(EXTENSION_ITEM_CONTROL_URL)
)
.setValue(
Extension.ValueX.newBuilder()
.setCodeableConcept(
CodeableConcept.newBuilder()
.addCoding(
Coding.newBuilder()
.setCode(
Code.newBuilder()
.setValue(ITEM_CONTROL_RADIO_BUTTON)
)
.setDisplay(
String.newBuilder()
.setValue("Radio Group")
)
.setSystem(
Uri.newBuilder()
.setValue(EXTENSION_ITEM_CONTROL_SYSTEM)
)
)
)

)
)
.build()

assertThat(questionnaireItem.itemControl).isEqualTo(ITEM_CONTROL_RADIO_BUTTON)
}

@Test
fun itemControl_wrongExtensionUrl_shouldReturnNull() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class QuestionnaireItemAdapterTest {
}

@Test
fun getItemViewType_choiceItemType_shouldReturnRadioGroupViewHolderType() {
fun getItemViewType_choiceItemType_lessAnswerOptions_shouldReturnRadioGroupViewHolderType() {
val questionnaireItemAdapter = QuestionnaireItemAdapter()
questionnaireItemAdapter.submitList(
listOf(
Expand All @@ -235,7 +235,7 @@ class QuestionnaireItemAdapterTest {
}

@Test
fun getItemViewType_choiceItemType_shouldReturnDropDownViewHolderTypeWithoutExtension() {
fun getItemViewType_choiceItemType_moreAnswerOptions_shouldReturnDropDownViewHolderType() {
val answerOptions = Iterable {
iterator<Questionnaire.Item.AnswerOption> {
repeat(QuestionnaireItemAdapter.MINIMUM_NUMBER_OF_ANSWER_OPTIONS_FOR_DROP_DOWN) {
Expand Down Expand Up @@ -281,7 +281,31 @@ class QuestionnaireItemAdapterTest {
}

@Test
fun getItemViewType_choiceItemType_shouldReturnDropDownViewHolderTypeWithExtension() {
fun getItemViewType_choiceItemType_itemControlExtensionWithRadioButton_shouldReturnRadioGroupViewHolder() { // ktlint-disable max-line-length
val answerOptions = Iterable {
iterator<Questionnaire.Item.AnswerOption> {
repeat(QuestionnaireItemAdapter.MINIMUM_NUMBER_OF_ANSWER_OPTIONS_FOR_DROP_DOWN) {
yield(
Questionnaire.Item.AnswerOption.newBuilder()
.setValue(
Questionnaire.Item.AnswerOption.ValueX.newBuilder()
.setCoding(
Coding.newBuilder()
.setCode(
Code.newBuilder()
.setValue("test-code")
)
.setDisplay(
String.newBuilder()
.setValue("Test Code")
)
)
)
.build()
)
}
}
}
val questionnaireItemAdapter = QuestionnaireItemAdapter()
questionnaireItemAdapter.submitList(
listOf(
Expand All @@ -291,6 +315,7 @@ class QuestionnaireItemAdapterTest {
Questionnaire.Item.TypeCode.newBuilder()
.setValue(QuestionnaireItemTypeCode.Value.CHOICE)
)
.addAllAnswerOption(answerOptions)
.addExtension(
Extension.newBuilder()
.setUrl(
Expand All @@ -305,7 +330,59 @@ class QuestionnaireItemAdapterTest {
Coding.newBuilder()
.setCode(
Code.newBuilder()
.setValue("drop-down")
.setValue(ITEM_CONTROL_RADIO_BUTTON)
)
.setDisplay(
String.newBuilder()
.setValue("Radio Button")
)
.setSystem(
Uri.newBuilder()
.setValue(
EXTENSION_ITEM_CONTROL_SYSTEM
)
)
)
)
)
)
.build(),
QuestionnaireResponse.Item.newBuilder()
) {}
)
)

assertThat(questionnaireItemAdapter.getItemViewType(0))
.isEqualTo(QuestionnaireItemViewHolderType.RADIO_GROUP.value
)
}

@Test
fun getItemViewType_choiceItemType_itemControlExtensionWithDropDown_shouldReturnDropDownViewHolderType() { // ktlint-disable max-line-length
val questionnaireItemAdapter = QuestionnaireItemAdapter()
questionnaireItemAdapter.submitList(
listOf(
QuestionnaireItemViewItem(
Questionnaire.Item.newBuilder()
.setType(
Questionnaire.Item.TypeCode.newBuilder()
.setValue(QuestionnaireItemTypeCode.Value.CHOICE)
)
.addExtension(
Extension.newBuilder()
.setUrl(
Uri.newBuilder()
.setValue(EXTENSION_ITEM_CONTROL_URL)
)
.setValue(
Extension.ValueX.newBuilder()
.setCodeableConcept(
CodeableConcept.newBuilder()
.addCoding(
Coding.newBuilder()
.setCode(
Code.newBuilder()
.setValue(ITEM_CONTROL_DROP_DOWN)
)
.setDisplay(
String.newBuilder()
Expand All @@ -329,7 +406,7 @@ class QuestionnaireItemAdapterTest {

assertThat(questionnaireItemAdapter.getItemViewType(0))
.isEqualTo(QuestionnaireItemViewHolderType.DROP_DOWN.value
)
)
}

// TODO: test errors thrown for unsupported types
Expand Down

0 comments on commit 3a15a9c

Please sign in to comment.