Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package com.google.android.fhir.datacapture.contrib.views.locationwidget
import android.view.View
import com.google.android.fhir.datacapture.extensions.itemControlCode
import com.google.android.fhir.datacapture.extensions.tryUnwrapContext
import com.google.android.fhir.datacapture.views.GroupHeaderView
import com.google.android.fhir.datacapture.views.HeaderView
import com.google.android.fhir.datacapture.views.QuestionnaireViewItem
import com.google.android.fhir.datacapture.views.factories.QuestionnaireItemAndroidViewHolderDelegate
import com.google.android.fhir.datacapture.views.factories.QuestionnaireItemAndroidViewHolderFactory
Expand All @@ -30,7 +30,7 @@ object LocationWidgetViewHolderFactory :
QuestionnaireItemAndroidViewHolderFactory(R.layout.location_widget_view) {
override fun getQuestionnaireItemViewHolderDelegate() =
object : QuestionnaireItemAndroidViewHolderDelegate {
private lateinit var headerView: GroupHeaderView
private lateinit var headerView: HeaderView
private lateinit var locationWidgetButton: MaterialButton

override lateinit var questionnaireViewItem: QuestionnaireViewItem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
>
<com.google.android.fhir.datacapture.views.GroupHeaderView
<com.google.android.fhir.datacapture.views.HeaderView
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ package com.google.android.fhir.datacapture.test
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.compose.ui.test.assertCountEquals
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.hasTestTag
import androidx.compose.ui.test.hasText
import androidx.compose.ui.test.junit4.createAndroidComposeRule
import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.compose.ui.test.performTextInput
import androidx.fragment.app.commitNow
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.espresso.assertion.ViewAssertions.doesNotExist
import androidx.test.espresso.matcher.RootMatchers
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.withId
Expand All @@ -47,8 +50,11 @@ import com.google.android.fhir.datacapture.test.utilities.clickOnText
import com.google.android.fhir.datacapture.validation.Invalid
import com.google.android.fhir.datacapture.validation.QuestionnaireResponseValidator
import com.google.android.fhir.datacapture.validation.Valid
import com.google.android.fhir.datacapture.views.compose.ADD_REPEATED_GROUP_BUTTON_TAG
import com.google.android.fhir.datacapture.views.compose.DELETE_REPEATED_GROUP_ITEM_BUTTON_TAG
import com.google.android.fhir.datacapture.views.compose.EDIT_TEXT_FIELD_TEST_TAG
import com.google.android.fhir.datacapture.views.compose.HANDLE_INPUT_DEBOUNCE_TIME
import com.google.android.fhir.datacapture.views.compose.REPEATED_GROUP_INSTANCE_HEADER_TITLE_TAG
import com.google.android.fhir.datacapture.views.factories.localDate
import com.google.android.fhir.datacapture.views.factories.localDateTime
import com.google.android.material.progressindicator.LinearProgressIndicator
Expand Down Expand Up @@ -639,42 +645,42 @@ class QuestionnaireUiEspressoTest {
@Test
fun test_add_item_button_does_not_exist_for_non_repeated_groups() {
buildFragmentFromQuestionnaire("/component_non_repeated_group.json")
onView(withId(com.google.android.fhir.datacapture.R.id.add_item_to_repeated_group))
.check(doesNotExist())
composeTestRule.onNodeWithTag(ADD_REPEATED_GROUP_BUTTON_TAG).assertDoesNotExist()
}

@Test
fun test_repeated_group_is_added() {
buildFragmentFromQuestionnaire("/component_repeated_group.json")
onView(withId(com.google.android.fhir.datacapture.R.id.add_item_to_repeated_group))
.perform(ViewActions.click())
composeTestRule.onNodeWithTag(ADD_REPEATED_GROUP_BUTTON_TAG).performClick()

composeTestRule
.onNodeWithTag(QuestionnaireFragment.QUESTIONNAIRE_EDIT_LIST)
.assertExists()
.assertIsDisplayed()

onView(withId(com.google.android.fhir.datacapture.R.id.repeated_group_instance_header_title))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
composeTestRule.onNodeWithTag(REPEATED_GROUP_INSTANCE_HEADER_TITLE_TAG).assertIsDisplayed()

onView(withText(com.google.android.fhir.datacapture.R.string.delete))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
composeTestRule.onNodeWithTag(DELETE_REPEATED_GROUP_ITEM_BUTTON_TAG).assertIsDisplayed()
}

@Test
fun test_repeated_group_adds_multiple_items() {
buildFragmentFromQuestionnaire("/component_multiple_repeated_group.json")
onView(allOf(withText("Add Repeated Group"))).perform(ViewActions.click())

onView(allOf(withText(com.google.android.fhir.datacapture.R.string.delete)))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
composeTestRule
.onNode(hasTestTag(ADD_REPEATED_GROUP_BUTTON_TAG) and hasText("Add Repeated Group"))
.performClick()
composeTestRule.onNodeWithTag(DELETE_REPEATED_GROUP_ITEM_BUTTON_TAG).assertIsDisplayed()
composeTestRule.onNodeWithTag(REPEATED_GROUP_INSTANCE_HEADER_TITLE_TAG).assertIsDisplayed()

onView(
allOf(
withId(com.google.android.fhir.datacapture.R.id.repeated_group_instance_header_title),
),
)
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
composeTestRule
.onNode(hasTestTag(ADD_REPEATED_GROUP_BUTTON_TAG) and hasText("Add Decimal Repeated Group"))
.performClick()
composeTestRule
.onAllNodes(hasTestTag(DELETE_REPEATED_GROUP_ITEM_BUTTON_TAG))
.assertCountEquals(2)
composeTestRule
.onAllNodes(hasTestTag(REPEATED_GROUP_INSTANCE_HEADER_TITLE_TAG))
.assertCountEquals(2)
}

@Test
Expand All @@ -689,14 +695,9 @@ class QuestionnaireUiEspressoTest {
.assertExists()
.assertIsDisplayed()

onView(withId(com.google.android.fhir.datacapture.R.id.repeated_group_instance_header_title))
.check(ViewAssertions.matches(ViewMatchers.isDisplayed()))

onView(withText(com.google.android.fhir.datacapture.R.string.delete))
.perform(ViewActions.click())

onView(withText(com.google.android.fhir.datacapture.R.id.repeated_group_instance_header_title))
.check(doesNotExist())
composeTestRule.onNodeWithTag(REPEATED_GROUP_INSTANCE_HEADER_TITLE_TAG).assertIsDisplayed()
composeTestRule.onNodeWithTag(DELETE_REPEATED_GROUP_ITEM_BUTTON_TAG).performClick()
composeTestRule.onNodeWithTag(REPEATED_GROUP_INSTANCE_HEADER_TITLE_TAG).assertDoesNotExist()
}

private fun buildFragmentFromQuestionnaire(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2025 Google LLC
* Copyright 2025 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 All @@ -14,35 +14,55 @@
* limitations under the License.
*/

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

import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsNotDisplayed
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.junit4.createEmptyComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.google.android.fhir.datacapture.R
import com.google.android.fhir.datacapture.test.TestActivity
import com.google.android.fhir.datacapture.validation.Invalid
import com.google.android.fhir.datacapture.validation.NotValidated
import com.google.android.fhir.datacapture.views.GroupHeaderView
import com.google.android.fhir.datacapture.views.QuestionnaireViewItem
import com.google.android.fhir.datacapture.views.compose.ERROR_TEXT_TAG
import com.google.android.fhir.datacapture.views.compose.HEADER_TAG
import com.google.android.fhir.datacapture.views.factories.GroupViewHolderFactory
import com.google.android.fhir.datacapture.views.factories.QuestionnaireItemViewHolder
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
import org.junit.runner.RunWith
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
@RunWith(AndroidJUnit4::class)
class GroupViewHolderFactoryTest {
private val parent =
FrameLayout(
Robolectric.buildActivity(AppCompatActivity::class.java).create().get().apply {
setTheme(com.google.android.material.R.style.Theme_Material3_DayNight)
},
)
private val viewHolder = GroupViewHolderFactory.create(parent)
@get:Rule
val activityScenarioRule: ActivityScenarioRule<TestActivity> =
ActivityScenarioRule(TestActivity::class.java)

@get:Rule val composeTestRule = createEmptyComposeRule()

private lateinit var viewHolder: QuestionnaireItemViewHolder

@Before
fun setUp() {
activityScenarioRule.scenario.onActivity { activity ->
viewHolder = GroupViewHolderFactory.create(FrameLayout(activity))
activity.setContentView(viewHolder.itemView)
}

InstrumentationRegistry.getInstrumentation().waitForIdleSync()
}

@Test
fun shouldSetQuestionHeader() {
Expand All @@ -55,6 +75,8 @@ class GroupViewHolderFactoryTest {
),
)

composeTestRule.waitForIdle()

assertThat(viewHolder.itemView.findViewById<TextView>(R.id.question).text.toString())
.isEqualTo("Group header")
}
Expand All @@ -69,9 +91,9 @@ class GroupViewHolderFactoryTest {
answersChangedCallback = { _, _, _, _ -> },
),
)

assertThat(viewHolder.itemView.findViewById<TextView>(R.id.error).text)
.isEqualTo("Missing answer for required field.")
composeTestRule
.onNodeWithTag(ERROR_TEXT_TAG)
.assertTextEquals("Missing answer for required field.")
}

@Test
Expand All @@ -96,8 +118,7 @@ class GroupViewHolderFactoryTest {
answersChangedCallback = { _, _, _, _ -> },
),
)

assertThat(viewHolder.itemView.findViewById<TextView>(R.id.error).text).isEqualTo("")
composeTestRule.onNodeWithTag(ERROR_TEXT_TAG).assertDoesNotExist()
}

@Test
Expand All @@ -121,21 +142,12 @@ class GroupViewHolderFactoryTest {
),
)

composeTestRule.waitForIdle()

assertThat(
viewHolder.itemView
.findViewById<GroupHeaderView>(R.id.header)
.findViewById<TextView>(R.id.hint)
.text
.isNullOrEmpty(),
)
.isTrue()
assertThat(
viewHolder.itemView
.findViewById<GroupHeaderView>(R.id.header)
.findViewById<TextView>(R.id.hint)
.visibility,
viewHolder.itemView.findViewById<TextView?>(R.id.hint),
)
.isEqualTo(View.GONE)
.isNull()
}

@Test
Expand All @@ -148,8 +160,7 @@ class GroupViewHolderFactoryTest {
answersChangedCallback = { _, _, _, _ -> },
),
)
assertThat(viewHolder.itemView.findViewById<GroupHeaderView>(R.id.header).visibility)
.isEqualTo(View.VISIBLE)
composeTestRule.onNodeWithTag(HEADER_TAG).assertIsDisplayed()
}

@Test
Expand All @@ -163,7 +174,6 @@ class GroupViewHolderFactoryTest {
),
)

assertThat(viewHolder.itemView.findViewById<GroupHeaderView>(R.id.header).visibility)
.isEqualTo(View.GONE)
composeTestRule.onNodeWithTag(HEADER_TAG).assertIsNotDisplayed()
}
}
Loading
Loading