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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Google LLC
* Copyright 2023-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 @@ -16,26 +16,30 @@

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

import android.view.View
import android.widget.FrameLayout
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.isDialog
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.semantics.SemanticsProperties
import androidx.compose.ui.test.SemanticsMatcher
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.filterToOne
import androidx.compose.ui.test.hasAnyChild
import androidx.compose.ui.test.hasContentDescription
import androidx.compose.ui.test.isEditable
import androidx.compose.ui.test.junit4.createEmptyComposeRule
import androidx.compose.ui.test.onChildren
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
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.test.utilities.clickIcon
import com.google.android.fhir.datacapture.validation.NotValidated
import com.google.android.fhir.datacapture.views.QuestionnaireViewItem
import com.google.android.fhir.datacapture.views.compose.DATE_TEXT_INPUT_FIELD
import com.google.android.fhir.datacapture.views.compose.TIME_PICKER_INPUT_FIELD
import com.google.android.fhir.datacapture.views.factories.DateTimePickerViewHolderFactory
import com.google.android.fhir.datacapture.views.factories.QuestionnaireItemViewHolder
import org.hamcrest.CoreMatchers.allOf
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.QuestionnaireResponse
import org.junit.Before
Expand All @@ -51,14 +55,17 @@ class DateTimePickerViewHolderFactoryEspressoTest {
var activityScenarioRule: ActivityScenarioRule<TestActivity> =
ActivityScenarioRule(TestActivity::class.java)

private lateinit var parent: FrameLayout
@get:Rule val composeTestRule = createEmptyComposeRule()

private lateinit var viewHolder: QuestionnaireItemViewHolder

@Before
fun setup() {
activityScenarioRule.scenario.onActivity { activity -> parent = FrameLayout(activity) }
viewHolder = DateTimePickerViewHolderFactory.create(parent)
setTestLayout(viewHolder.itemView)
activityScenarioRule.scenario.onActivity { activity ->
viewHolder = DateTimePickerViewHolderFactory.create(FrameLayout(activity))
activity.setContentView(viewHolder.itemView)
}
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
}

@Test
Expand All @@ -71,17 +78,29 @@ class DateTimePickerViewHolderFactoryEspressoTest {
answersChangedCallback = { _, _, _, _ -> },
)

runOnUI { viewHolder.bind(questionnaireItemView) }
onView(withId(R.id.date_input_layout)).perform(clickIcon(true))
onView(allOf(withText("OK")))
.inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(ViewActions.click())
onView(withId(R.id.time_input_edit_text)).perform(ViewActions.click())
// R.id.material_textinput_timepicker is the id for the text input in the time picker.
onView(allOf(withId(com.google.android.material.R.id.material_textinput_timepicker)))
.inRoot(isDialog())
.check(matches(isDisplayed()))
viewHolder.bind(questionnaireItemView)
composeTestRule
.onNodeWithTag(DATE_TEXT_INPUT_FIELD)
.onChildren()
.filterToOne(
SemanticsMatcher.expectValue(SemanticsProperties.Role, Role.Button),
)
.performClick()
composeTestRule.onNodeWithText("OK").performClick()
composeTestRule.onNodeWithTag(TIME_PICKER_INPUT_FIELD).performClick()

composeTestRule
.onNode(
hasContentDescription("Switch to clock input", substring = true) and
SemanticsMatcher.expectValue(SemanticsProperties.Role, Role.Button),
)
.assertIsDisplayed()
composeTestRule
.onNode(hasContentDescription("for hour", substring = true) and isEditable())
.assertIsDisplayed()
composeTestRule
.onNode(hasContentDescription("for minutes", substring = true) and isEditable())
.assertExists()
}

@Test
Expand All @@ -94,27 +113,34 @@ class DateTimePickerViewHolderFactoryEspressoTest {
answersChangedCallback = { _, _, _, _ -> },
)

runOnUI { viewHolder.bind(questionnaireItemView) }
onView(withId(R.id.date_input_layout)).perform(clickIcon(true))
onView(allOf(withText("OK")))
.inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(ViewActions.click())
onView(withId(R.id.time_input_layout)).perform(clickIcon(true))
// R.id.material_clock_face is the id for the clock input in the time picker.
onView(allOf(withId(com.google.android.material.R.id.material_clock_face)))
.inRoot(isDialog())
.check(matches(isDisplayed()))
}

/** Method to run code snippet on UI/main thread */
private fun runOnUI(action: () -> Unit) {
activityScenarioRule.scenario.onActivity { activity -> action() }
}
viewHolder.bind(questionnaireItemView)
composeTestRule
.onNodeWithTag(DATE_TEXT_INPUT_FIELD)
.onChildren()
.filterToOne(
SemanticsMatcher.expectValue(SemanticsProperties.Role, Role.Button),
)
.performClick()
composeTestRule.onNodeWithText("OK").performClick()
composeTestRule
.onNodeWithTag(TIME_PICKER_INPUT_FIELD)
.onChildren()
.filterToOne(
SemanticsMatcher.expectValue(SemanticsProperties.Role, Role.Button),
)
.performClick()

/** Method to set content view for test activity */
private fun setTestLayout(view: View) {
activityScenarioRule.scenario.onActivity { activity -> activity.setContentView(view) }
InstrumentationRegistry.getInstrumentation().waitForIdleSync()
composeTestRule
.onNode(
hasContentDescription("Switch to text input", substring = true) and
SemanticsMatcher.expectValue(SemanticsProperties.Role, Role.Button),
)
.assertIsDisplayed()
composeTestRule
.onNode(
hasAnyChild(hasContentDescription("12 o'clock", substring = true)) and
SemanticsMatcher.keyIsDefined(SemanticsProperties.SelectableGroup),
)
.assertIsDisplayed()
}
}
Loading
Loading