Skip to content

Commit

Permalink
Empty Option added to DropDown and test cases added (#1298)
Browse files Browse the repository at this point in the history
* Empty Option added to DropDown and test cases added

* merged dropdown change with PR 995

* Review points addressed
  • Loading branch information
PallaviGanorkar authored Apr 20, 2022
1 parent ae4c26a commit ac93433
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 91 deletions.
1 change: 0 additions & 1 deletion datacapture/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ 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
@@ -0,0 +1,44 @@
/*
* 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://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.utilities

import android.view.View
import android.widget.AutoCompleteTextView
import androidx.test.espresso.UiController
import androidx.test.espresso.ViewAction
import androidx.test.espresso.matcher.ViewMatchers.isAssignableFrom
import androidx.test.espresso.matcher.ViewMatchers.isEnabled
import org.hamcrest.Matcher
import org.hamcrest.Matchers.allOf

/** Show Drop Down view for AutoCompleteTextView widget. */
fun showDropDown(): ViewAction {
return object : ViewAction {
override fun getConstraints(): Matcher<View> =
allOf(isEnabled(), isAssignableFrom(AutoCompleteTextView::class.java))

override fun getDescription(): String {
return "show DropDown"
}

override fun perform(uiController: UiController, view: View?) {
val autoCompleteTextView = view as AutoCompleteTextView
autoCompleteTextView.showDropDown()
uiController.loopMainThreadUntilIdle()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* 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://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.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.RootMatchers
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.platform.app.InstrumentationRegistry
import com.google.android.fhir.datacapture.R
import com.google.android.fhir.datacapture.TestActivity
import com.google.android.fhir.datacapture.utilities.showDropDown
import com.google.common.truth.Truth.assertThat
import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.QuestionnaireResponse
import org.junit.Before
import org.junit.Rule
import org.junit.Test

class QuestionnaireItemDropDownViewHolderFactoryEspressoTest {
@Rule
@JvmField
var activityScenarioRule: ActivityScenarioRule<TestActivity> =
ActivityScenarioRule<TestActivity>(TestActivity::class.java)

private lateinit var parent: FrameLayout
private lateinit var viewHolder: QuestionnaireItemViewHolder

@Before
fun setup() {
activityScenarioRule.getScenario().onActivity { activity -> parent = FrameLayout(activity) }
viewHolder = QuestionnaireItemDropDownViewHolderFactory.create(parent)
setTestLayout(viewHolder.itemView)
}

@Test
fun shouldClearAutoCompleteTextView() {
val questionnaireItemViewItem =
QuestionnaireItemViewItem(
answerOptions("Coding 1", "Coding 2", "Coding 3", "Coding 4", "Coding 5"),
responseOptions()
) {}
runOnUI { viewHolder.bind(questionnaireItemViewItem) }

onView(withId(R.id.auto_complete)).perform(showDropDown())
onView(withText("-"))
.inRoot(RootMatchers.isPlatformPopup())
.check(matches(isDisplayed()))
.perform(ViewActions.click())
assertThat(viewHolder.itemView.findViewById<TextView>(R.id.auto_complete).text.toString())
.isEqualTo("-")
assertThat(questionnaireItemViewItem.questionnaireResponseItem.answer).isEmpty()
}

@Test
fun shouldSetDropDownValueToAutoCompleteTextView() {
val questionnaireItemViewItem =
QuestionnaireItemViewItem(
answerOptions("Coding 1", "Coding 2", "Coding 3", "Coding 4", "Coding 5"),
responseOptions()
) {}
runOnUI { viewHolder.bind(questionnaireItemViewItem) }

onView(withId(R.id.auto_complete)).perform(showDropDown())
onView(withText("Coding 3"))
.inRoot(RootMatchers.isPlatformPopup())
.check(matches(isDisplayed()))
.perform(ViewActions.click())
assertThat(viewHolder.itemView.findViewById<TextView>(R.id.auto_complete).text.toString())
.isEqualTo("Coding 3")
assertThat(
(questionnaireItemViewItem.questionnaireResponseItem.answer[0].value as Coding).display
)
.isEqualTo("Coding 3")
}

/** Method to run code snippet on UI/main thread */
private fun runOnUI(action: () -> Unit) {
activityScenarioRule.getScenario().onActivity { activity -> action() }
}

/** Method to set content view for test activity */
private fun setTestLayout(view: View) {
activityScenarioRule.getScenario().onActivity { activity -> activity.setContentView(view) }
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
}

private fun answerOptions(vararg options: String) =
Questionnaire.QuestionnaireItemComponent().apply {
options.forEach { option ->
addAnswerOption(
Questionnaire.QuestionnaireItemAnswerOptionComponent().apply {
value =
Coding().apply {
code = option.replace(" ", "_")
display = option
}
}
)
}
}

private fun responseOptions(vararg responses: String) =
QuestionnaireResponse.QuestionnaireResponseItemComponent().apply {
responses.forEach { response ->
addAnswer(
QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent().apply {
value = Coding().apply { display = response }
}
)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal object QuestionnaireItemDropDownViewHolderFactory :
textInputLayout.hint = questionnaireItemViewItem.questionnaireItem.localizedFlyoverSpanned
val answerOptionString =
this.questionnaireItemViewItem.answerOption.map { it.displayString }.toMutableList()
answerOptionString.add(0, "-")
answerOptionString.add(0, context.getString(R.string.hyphen))
val adapter =
ArrayAdapter(context, R.layout.questionnaire_item_drop_down_list, answerOptionString)
autoCompleteTextView.setText(
Expand Down
1 change: 1 addition & 0 deletions datacapture/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,5 @@
<string name="date">Date</string>
<string name="time">Time</string>
<string name="select_date">Select date</string>
<string name="hyphen">-</string>
</resources>

0 comments on commit ac93433

Please sign in to comment.