Skip to content

Commit

Permalink
add empty option (#995)
Browse files Browse the repository at this point in the history
* add empty option

* requested change

* dependencies update

* drop down tests

* Run spotless apply

Co-authored-by: Jing Tang <[email protected]>
  • Loading branch information
maanuanubhav999 and jingtang10 committed Apr 17, 2022
1 parent e2b40eb commit 4e21667
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 8 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ object Dependencies {
const val fragmentVersion = "1.3.6"
}

const val espresso = "3.3.0"
const val espresso = "3.4.0"
const val jacoco = "0.8.7"
const val junit = "4.12"
const val mockitoKotlin = "3.2.0"
Expand Down
1 change: 1 addition & 0 deletions datacapture/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ android {
configurations { all { exclude(module = "xpp3") } }

dependencies {
implementation("androidx.test.espresso:espresso-core:3.4.0")
androidTestImplementation(Dependencies.AndroidxTest.core)
androidTestImplementation(Dependencies.AndroidxTest.extJunit)
androidTestImplementation(Dependencies.AndroidxTest.extJunitKtx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class QuestionnaireItemDropDownViewHolderFactoryInstrumentedTest {
.itemView
.findViewById<AutoCompleteTextView>(R.id.auto_complete)
.adapter
.getItem(0)
.getItem(1)
.toString()
)
.isEqualTo("Test Code")
Expand All @@ -128,7 +128,7 @@ class QuestionnaireItemDropDownViewHolderFactoryInstrumentedTest {
fun shouldSetDropDownOptionToCodeIfValueCodingDisplayEmpty() {
val answerOption =
Questionnaire.QuestionnaireItemAnswerOptionComponent().apply {
value = Coding().apply { setCode("test-code") }
value = Coding().apply { code = "test-code" }
}
viewHolder.bind(
QuestionnaireItemViewItem(
Expand All @@ -142,7 +142,7 @@ class QuestionnaireItemDropDownViewHolderFactoryInstrumentedTest {
.itemView
.findViewById<AutoCompleteTextView>(R.id.auto_complete)
.adapter
.getItem(0)
.getItem(1)
.toString()
)
.isEqualTo("test-code")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright 2021 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.android.fhir.datacapture.views

import android.widget.AutoCompleteTextView
import android.widget.FrameLayout
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.activityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.google.android.fhir.datacapture.R
import com.google.android.fhir.datacapture.TestActivity
import com.google.common.truth.Truth
import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.QuestionnaireResponse
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class QuestionnaireItemDropDownViewHolderFactoryTest {
@get:Rule val rule = activityScenarioRule<TestActivity>()

@Test
@Ignore("To be fixed in https://github.com/google/android-fhir/pull/1298")
fun should_clearAutoCompleteTextView() = withViewHolder { holder ->
val answerOption =
Questionnaire.QuestionnaireItemAnswerOptionComponent().apply {
value =
Coding().apply {
code = "test-code"
display = "Test Code"
}
}
val questionnaireItemViewItem =
QuestionnaireItemViewItem(
Questionnaire.QuestionnaireItemComponent().apply {
answerValueSet = "http:https://coding-value-set-url"
},
QuestionnaireResponse.QuestionnaireResponseItemComponent().apply {
addAnswer(
QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent().apply {
value = answerOption.value
}
)
},
resolveAnswerValueSet = {
if (it == "http:https://coding-value-set-url") {
listOf(answerOption)
} else {
emptyList()
}
}
) {}
holder.bind(questionnaireItemViewItem)
var autoCompleteTextView =
holder.itemView.findViewById<AutoCompleteTextView>(R.id.auto_complete)

autoCompleteTextView.showDropDown()
onView(withText("-")).perform(click())

Truth.assertThat(autoCompleteTextView.text).isEqualTo("")
}

private inline fun withViewHolder(
crossinline block: TestActivity.(QuestionnaireItemViewHolder) -> Unit
) {
rule.scenario.onActivity {
block(it, QuestionnaireItemDropDownViewHolderFactory.create(FrameLayout(it)))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ internal object QuestionnaireItemDropDownViewHolderFactory :
questionSubtitleTextView.text = questionnaireItemViewItem.questionnaireItem.subtitleText
textInputLayout.hint = questionnaireItemViewItem.questionnaireItem.flyOverText
val answerOptionString =
this.questionnaireItemViewItem.answerOption.map { it.displayString }
this.questionnaireItemViewItem.answerOption.map { it.displayString }.toMutableList()
answerOptionString.add(0, "-")
val adapter =
ArrayAdapter(context, R.layout.questionnaire_item_drop_down_list, answerOptionString)
autoCompleteTextView.setText(
Expand All @@ -74,9 +75,13 @@ internal object QuestionnaireItemDropDownViewHolderFactory :
autoCompleteTextView.setAdapter(adapter)
autoCompleteTextView.onItemClickListener =
AdapterView.OnItemClickListener { _, _, position, _ ->
questionnaireItemViewItem.singleAnswerOrNull =
QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent()
.setValue(questionnaireItemViewItem.answerOption[position].valueCoding)
if (position == 0) {
questionnaireItemViewItem.singleAnswerOrNull = null
} else {
questionnaireItemViewItem.singleAnswerOrNull =
QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent()
.setValue(questionnaireItemViewItem.answerOption[position - 1].valueCoding)
}
onAnswerChanged(autoCompleteTextView.context)
}
}
Expand Down

0 comments on commit 4e21667

Please sign in to comment.