Skip to content

Commit c3eef16

Browse files
committed
CHeckbox Crashes google#2661
1 parent 2cff3e3 commit c3eef16

File tree

1 file changed

+27
-278
lines changed

1 file changed

+27
-278
lines changed

datacapture/src/test/java/com/google/android/fhir/datacapture/views/factories/CheckBoxGroupViewHolderFactoryTest.kt

+27-278
Original file line numberDiff line numberDiff line change
@@ -617,326 +617,75 @@ class CheckBoxGroupViewHolderFactoryTest {
617617
)
618618
}
619619

620-
@Test
621-
fun bind_shouldSetQuestionHeader_whenRepeatFalse() {
622-
viewHolder.bind(
623-
QuestionnaireViewItem(
624-
Questionnaire.QuestionnaireItemComponent().apply {
625-
repeats = false
626-
text = "Question?"
627-
},
628-
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
629-
validationResult = NotValidated,
630-
answersChangedCallback = { _, _, _, _ -> },
631-
),
632-
)
633-
assertThat(viewHolder.itemView.findViewById<TextView>(R.id.question).text.toString())
634-
.isEqualTo("Question?")
635-
}
636-
637-
@Test
638-
fun bind_horizontal_shouldCreateCheckBoxButtons_whenRepeatFalse() {
639-
val questionnaire =
640-
Questionnaire.QuestionnaireItemComponent().apply {
641-
repeats = false
642-
addExtension(
643-
EXTENSION_CHOICE_ORIENTATION_URL,
644-
CodeType(ChoiceOrientationTypes.HORIZONTAL.extensionCode),
645-
)
646-
addAnswerOption(
647-
Questionnaire.QuestionnaireItemAnswerOptionComponent().apply {
648-
value = Coding().apply { display = "Coding 1" }
649-
},
650-
)
651-
addAnswerOption(
652-
Questionnaire.QuestionnaireItemAnswerOptionComponent().apply {
653-
value = Coding().apply { display = "Coding 2" }
654-
},
655-
)
656-
}
657-
viewHolder.bind(
658-
QuestionnaireViewItem(
659-
questionnaire,
660-
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
661-
validationResult = NotValidated,
662-
answersChangedCallback = { _, _, _, _ -> },
663-
),
664-
)
665-
666-
val checkBoxGroup = viewHolder.itemView.findViewById<ConstraintLayout>(R.id.checkbox_group)
667-
val children = checkBoxGroup.children.asIterable().filterIsInstance<CheckBox>()
668-
children.forEachIndexed { index, view ->
669-
assertThat(view.text.toString())
670-
.isEqualTo(questionnaire.answerOption[index].valueCoding.display)
671-
assertThat(view.layoutParams.width).isEqualTo(ViewGroup.LayoutParams.WRAP_CONTENT)
672-
}
673-
}
674620

675621
@Test
676-
fun bind_noAnswer_shouldLeaveCheckButtonsUnchecked_whenRepeatFalse() {
622+
fun click_should_Select_Other_CheckboxButton() {
677623
viewHolder.bind(
678624
QuestionnaireViewItem(
679625
Questionnaire.QuestionnaireItemComponent().apply {
680626
repeats = false
681627
addAnswerOption(
682628
Questionnaire.QuestionnaireItemAnswerOptionComponent().apply {
683-
value = Coding().apply { display = "Coding 1" }
684-
},
685-
)
686-
},
687-
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
688-
validationResult = NotValidated,
689-
answersChangedCallback = { _, _, _, _ -> },
690-
),
691-
)
692-
693-
val checkBoxGroup = viewHolder.itemView.findViewById<ConstraintLayout>(R.id.checkbox_group)
694-
val checkBox = checkBoxGroup.getChildAt(1) as CheckBox
695-
assertThat(checkBox.isChecked)
696-
.isFalse() // Ensure checkbox is unchecked when no answer is selected
697-
}
698-
699-
@Test
700-
fun bind_withImageInItemAnswerMediaExtension_shouldShowImageAfterCheckBox_whenRepeatFalse() {
701-
viewHolder.bind(
702-
QuestionnaireViewItem(
703-
Questionnaire.QuestionnaireItemComponent().apply {
704-
repeats = false
705-
addAnswerOption(
706-
Questionnaire.QuestionnaireItemAnswerOptionComponent().apply {
707-
extension = listOf(itemAnswerMediaExtension)
708-
value = StringType("Test Code")
629+
value = Coding().apply {
630+
code = "code-1"
631+
display = "display-1"
632+
}
709633
},
710634
)
711-
},
712-
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
713-
validationResult = NotValidated,
714-
answersChangedCallback = { _, _, _, _ -> },
715-
),
716-
)
717-
718-
val checkBoxGroup = viewHolder.itemView.findViewById<ConstraintLayout>(R.id.checkbox_group)
719-
val checkBox = checkBoxGroup.getChildAt(1) as CheckBox
720-
assertThat(checkBox.compoundDrawablesRelative[0]).isNotNull()
721-
assertThat(checkBox.compoundDrawablesRelative[1]).isNull()
722-
assertThat(checkBox.compoundDrawablesRelative[2]).isNull()
723-
assertThat(checkBox.compoundDrawablesRelative[3]).isNull()
724-
}
725-
726-
@Test
727-
fun bind_answer_shouldSetCheckBoxButton_whenRepeatFalse() {
728-
viewHolder.bind(
729-
QuestionnaireViewItem(
730-
Questionnaire.QuestionnaireItemComponent().apply {
731-
repeats = false
732635
addAnswerOption(
733636
Questionnaire.QuestionnaireItemAnswerOptionComponent().apply {
734-
value =
735-
Coding().apply {
736-
code = "code 1"
737-
display = "Coding 1"
738-
}
637+
value = Coding().apply {
638+
code = "code-2"
639+
display = "display-2"
640+
}
739641
},
740642
)
741643
},
742644
QuestionnaireResponse.QuestionnaireResponseItemComponent().apply {
743645
addAnswer(
744646
QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent().apply {
745-
value =
746-
Coding().apply {
747-
code = "code 1"
748-
display = "Coding 1"
749-
}
647+
value = Coding().apply {
648+
code = "code-1"
649+
display = "display-1"
650+
}
750651
},
751652
)
752653
},
753654
validationResult = NotValidated,
754655
answersChangedCallback = { _, _, _, _ -> },
755656
),
756657
)
757-
val checkBoxGroup = viewHolder.itemView.findViewById<ConstraintLayout>(R.id.checkbox_group)
758-
val checkBox = checkBoxGroup.getChildAt(1) as CheckBox
759658

760-
assertThat(checkBox.isChecked).isTrue()
761-
}
762-
763-
@Test
764-
fun click_shouldAddQuestionnaireResponseItemAnswer_whenRepeatFalse() {
765-
var answerHolder: List<QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent>? = null
766-
val questionnaireViewItem =
767-
QuestionnaireViewItem(
768-
Questionnaire.QuestionnaireItemComponent().apply {
769-
repeats = false
770-
addAnswerOption(
771-
Questionnaire.QuestionnaireItemAnswerOptionComponent().apply {
772-
value =
773-
Coding().apply {
774-
code = "code 1"
775-
display = "Coding 1"
776-
}
777-
},
778-
)
779-
},
780-
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
781-
validationResult = NotValidated,
782-
answersChangedCallback = { _, _, answers, _ -> answerHolder = answers },
783-
)
784-
viewHolder.bind(questionnaireViewItem)
785659
val checkBoxGroup = viewHolder.itemView.findViewById<ConstraintLayout>(R.id.checkbox_group)
786-
val checkBox = checkBoxGroup.getChildAt(1) as CheckBox
787-
checkBox.performClick()
788-
789-
assertThat(answerHolder!!.single().valueCoding.display).isEqualTo("Coding 1")
790-
}
791-
792-
@Test
793-
fun optionExclusiveAnswerOption_click_deselectsOtherAnswerOptions_whenRepeatFalse() {
794-
var answerHolder: List<QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent>? = null
795-
val questionnaireViewItem =
796-
QuestionnaireViewItem(
797-
Questionnaire.QuestionnaireItemComponent().apply {
798-
repeats = false
799-
addAnswerOption(
800-
Questionnaire.QuestionnaireItemAnswerOptionComponent().apply {
801-
value =
802-
Coding().apply {
803-
code = "code-1"
804-
display = "display-1"
805-
}
806-
},
807-
)
808-
addAnswerOption(
809-
Questionnaire.QuestionnaireItemAnswerOptionComponent().apply {
810-
value =
811-
Coding().apply {
812-
code = "code-2"
813-
display = "display-2"
814-
}
815-
},
816-
)
817-
},
818-
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
819-
validationResult = NotValidated,
820-
answersChangedCallback = { _, _, answers, _ -> answerHolder = answers },
821-
)
822-
viewHolder.bind(questionnaireViewItem)
823-
val checkBoxGroup = viewHolder.itemView.findViewById<ConstraintLayout>(R.id.checkbox_group)
824-
(checkBoxGroup.getChildAt(2) as CheckBox).performClick()
825-
(checkBoxGroup.getChildAt(1) as CheckBox).performClick()
826-
827-
assertThat(answerHolder!!.single().valueCoding.display).isEqualTo("display-1")
828-
}
829-
830-
@Test
831-
fun answerOption_click_deselectsOptionExclusiveAnswerOption_whenRepeatFalse() {
832-
var answerHolder: List<QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent>? = null
833-
val questionnaireViewItem =
834-
QuestionnaireViewItem(
835-
Questionnaire.QuestionnaireItemComponent().apply {
836-
repeats = false
837-
addAnswerOption(
838-
Questionnaire.QuestionnaireItemAnswerOptionComponent().apply {
839-
value =
840-
Coding().apply {
841-
code = "code-1"
842-
display = "display-1"
843-
}
844-
},
845-
)
846-
addAnswerOption(
847-
Questionnaire.QuestionnaireItemAnswerOptionComponent().apply {
848-
value =
849-
Coding().apply {
850-
code = "code-2"
851-
display = "display-2"
852-
}
853-
},
854-
)
855-
},
856-
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
857-
validationResult = NotValidated,
858-
answersChangedCallback = { _, _, answers, _ -> answerHolder = answers },
859-
)
860-
861-
viewHolder.bind(questionnaireViewItem)
862-
val checkBoxGroup = viewHolder.itemView.findViewById<ConstraintLayout>(R.id.checkbox_group)
863-
(checkBoxGroup.getChildAt(1) as CheckBox).performClick()
864-
(checkBoxGroup.getChildAt(2) as CheckBox).performClick()
865-
866-
assertThat(answerHolder!!.single().valueCoding.display).isEqualTo("display-2")
867-
}
868-
869-
@Test
870-
fun displayValidationResult_error_shouldShowErrorMessage_whenRepeatFalse() {
871-
viewHolder.bind(
872-
QuestionnaireViewItem(
873-
Questionnaire.QuestionnaireItemComponent().apply {
874-
repeats = false
875-
required = true
876-
},
877-
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
878-
validationResult = Invalid(listOf("Missing answer for required field.")),
879-
answersChangedCallback = { _, _, _, _ -> },
880-
),
881-
)
882-
883-
assertThat(viewHolder.itemView.findViewById<TextView>(R.id.error_text_at_header).text)
884-
.isEqualTo("Missing answer for required field.")
660+
assertThat((checkBoxGroup.getChildAt(1) as CheckBox).isChecked,).isTrue()
661+
checkBoxGroup.getChildAt(2).performClick()
662+
assertThat((checkBoxGroup.getChildAt(2) as CheckBox).isChecked,).isTrue()
885663
}
886664

887665
@Test
888-
fun displayValidationResult_noError_shouldShowNoErrorMessage_whenRepeatFalse() {
666+
fun click_Selected_CheckboxButton_Should_Uncheck_CheckboxButton() {
889667
viewHolder.bind(
890668
QuestionnaireViewItem(
891669
Questionnaire.QuestionnaireItemComponent().apply {
892670
repeats = false
893-
required = true
894671
addAnswerOption(
895672
Questionnaire.QuestionnaireItemAnswerOptionComponent().apply {
896-
value = Coding().apply { display = "display" }
897-
},
898-
)
899-
},
900-
QuestionnaireResponse.QuestionnaireResponseItemComponent().apply {
901-
addAnswer(
902-
QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent().apply {
903-
value = Coding().apply { display = "display" }
904-
},
905-
)
906-
},
907-
validationResult = NotValidated,
908-
answersChangedCallback = { _, _, _, _ -> },
909-
),
910-
)
911-
912-
assertThat(viewHolder.itemView.findViewById<TextView>(R.id.error_text_at_header).text.isEmpty())
913-
.isTrue()
914-
}
915-
916-
@Test
917-
fun bind_readOnly_shouldDisableView_whenRepeatFalse() {
918-
viewHolder.bind(
919-
QuestionnaireViewItem(
920-
Questionnaire.QuestionnaireItemComponent().apply {
921-
repeats = false
922-
readOnly = true
923-
addAnswerOption(
924-
Questionnaire.QuestionnaireItemAnswerOptionComponent().apply {
925-
value = Coding().apply { display = "Coding 1" }
673+
value = Coding().apply {
674+
code = "code-1"
675+
display = "display-1"
676+
}
926677
},
927678
)
928679
},
929680
QuestionnaireResponse.QuestionnaireResponseItemComponent(),
930681
validationResult = NotValidated,
931682
answersChangedCallback = { _, _, _, _ -> },
932-
),
933-
)
683+
))
934684

935-
assertThat(
936-
(viewHolder.itemView.findViewById<ConstraintLayout>(R.id.checkbox_group).getChildAt(1)
937-
as CheckBox)
938-
.isEnabled,
939-
)
940-
.isFalse()
685+
val checkBoxGroup = viewHolder.itemView.findViewById<ConstraintLayout>(R.id.checkbox_group)
686+
checkBoxGroup.getChildAt(1).performClick()
687+
assertThat((checkBoxGroup.getChildAt(1) as CheckBox).isChecked).isTrue()
688+
checkBoxGroup.getChildAt(1).performClick()
689+
assertThat((checkBoxGroup.getChildAt(1) as CheckBox).isChecked).isFalse()
941690
}
942691
}

0 commit comments

Comments
 (0)