-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement calculated-expression extension (#1380)
* Implement calculated-expression extension * Fix form value update bug * Detect cyclic dependency | fix on init value loading * Fix merge conflict * Make birthdate age dependent | Handle and fix quantity values * Fix failing test * quantity viewholder delegate test covergae * Test coverage for update flow * spotless fix * spotless fix | re-run ci * Test covergae for questionnaire fragment * test coverage for quantity types * questionnaire fragment test with launchInFragmentContainer * Update datacapture/src/main/java/com/google/android/fhir/datacapture/QuestionnaireViewModel.kt Co-authored-by: aditya-07 <[email protected]> * Move widget to LayoutList | Run Calculation after state-change * Revert the run-expression after state-flow * remove empty line changes * spotless fix * Esperesso test | Fix failing test * Remove unnessary changes * Fix espresso tests * Ignore Failing tests * Revert ignore test | merge main | refactor * spotless fix * Rename tests * Add tests and docs * Move catalog calculation to behavior tab * spotless fix * Update datacapture/src/main/java/com/google/android/fhir/datacapture/MoreQuestionnaireItemComponents.kt Co-authored-by: Jing Tang <[email protected]> * Resolve feedback for naming * Resolve feedback for doc and naming * Refactor the update answer handling logic * Resolve feedback and merge master * Fix failing test Co-authored-by: aditya-07 <[email protected]> Co-authored-by: Benjamin Mwalimu <[email protected]> Co-authored-by: Jing Tang <[email protected]>
- Loading branch information
1 parent
da1b5b3
commit 08357ef
Showing
21 changed files
with
1,513 additions
and
300 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
catalog/src/main/assets/calculated_expression_questionnaire.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"resourceType": "Questionnaire", | ||
"item": [ | ||
{ | ||
"linkId": "a-birthdate", | ||
"text": "Birth Date", | ||
"type": "date", | ||
"extension": [ | ||
{ | ||
"url": "http://hl7.org/fhir/uv/sdc/StructureDefinition/sdc-questionnaire-calculatedExpression", | ||
"valueExpression": { | ||
"language": "text/fhirpath", | ||
"expression": "%resource.repeat(item).where(linkId='a-age-years' and answer.empty().not()).select(today() - answer.value)" | ||
} | ||
} | ||
] | ||
}, | ||
{ | ||
"linkId": "a-age-years", | ||
"text": "Age years", | ||
"type": "quantity", | ||
"initial": [{ | ||
"valueQuantity": { | ||
"unit": "years", | ||
"system": "http://unitsofmeasure.org", | ||
"code": "years" | ||
} | ||
}] | ||
}, | ||
{ | ||
"linkId": "a-age-acknowledge", | ||
"text": "Input age to automatically calculate birthdate until birthdate is updated manually", | ||
"type": "display" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
.../fhir/datacapture/views/QuestionnaireItemEditTextQuantityViewHolderFactoryEspressoTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright 2022 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.click | ||
import androidx.test.espresso.action.ViewActions.typeText | ||
import androidx.test.espresso.matcher.ViewMatchers.withId | ||
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.validation.NotValidated | ||
import com.google.common.truth.Truth.assertThat | ||
import java.math.BigDecimal | ||
import org.hl7.fhir.r4.model.Quantity | ||
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 QuestionnaireItemEditTextQuantityViewHolderFactoryEspressoTest { | ||
@Rule | ||
@JvmField | ||
var activityScenarioRule: ActivityScenarioRule<TestActivity> = | ||
ActivityScenarioRule(TestActivity::class.java) | ||
|
||
private lateinit var parent: FrameLayout | ||
private lateinit var viewHolder: QuestionnaireItemViewHolder | ||
|
||
@Before | ||
fun setup() { | ||
activityScenarioRule.scenario.onActivity { activity -> parent = FrameLayout(activity) } | ||
viewHolder = QuestionnaireItemEditTextQuantityViewHolderFactory.create(parent) | ||
setTestLayout(viewHolder.itemView) | ||
} | ||
|
||
@Test | ||
fun getValue_WithInitial_shouldReturn_Quantity_With_UnitAndSystem() { | ||
val questionnaireItemViewItem = | ||
QuestionnaireItemViewItem( | ||
Questionnaire.QuestionnaireItemComponent().apply { | ||
required = true | ||
addInitial( | ||
Questionnaire.QuestionnaireItemInitialComponent( | ||
Quantity().apply { | ||
code = "months" | ||
system = "http://unitofmeasure.com" | ||
} | ||
) | ||
) | ||
}, | ||
QuestionnaireResponse.QuestionnaireResponseItemComponent(), | ||
validationResult = NotValidated, | ||
answersChangedCallback = { _, _, _ -> }, | ||
) | ||
runOnUI { viewHolder.bind(questionnaireItemViewItem) } | ||
|
||
onView(withId(R.id.text_input_edit_text)).perform(click()) | ||
onView(withId(R.id.text_input_edit_text)).perform(typeText("22")) | ||
assertThat( | ||
viewHolder.itemView.findViewById<TextView>(R.id.text_input_edit_text).text.toString() | ||
) | ||
.isEqualTo("22") | ||
|
||
val responseValue = questionnaireItemViewItem.answers.first().valueQuantity | ||
assertThat(responseValue.code).isEqualTo("months") | ||
assertThat(responseValue.system).isEqualTo("http://unitofmeasure.com") | ||
assertThat(responseValue.value).isEqualTo(BigDecimal(22)) | ||
} | ||
|
||
@Test | ||
fun getValue_WithoutInitial_shouldReturn_Quantity_Without_UnitAndSystem() { | ||
val questionnaireItemViewItem = | ||
QuestionnaireItemViewItem( | ||
Questionnaire.QuestionnaireItemComponent().apply { required = true }, | ||
QuestionnaireResponse.QuestionnaireResponseItemComponent(), | ||
validationResult = NotValidated, | ||
answersChangedCallback = { _, _, _ -> }, | ||
) | ||
runOnUI { viewHolder.bind(questionnaireItemViewItem) } | ||
|
||
onView(withId(R.id.text_input_edit_text)).perform(click()) | ||
onView(withId(R.id.text_input_edit_text)).perform(typeText("22")) | ||
assertThat( | ||
viewHolder.itemView.findViewById<TextView>(R.id.text_input_edit_text).text.toString() | ||
) | ||
.isEqualTo("22") | ||
|
||
val responseValue = questionnaireItemViewItem.answers.first().valueQuantity | ||
assertThat(responseValue.code).isNull() | ||
assertThat(responseValue.system).isNull() | ||
assertThat(responseValue.value).isEqualTo(BigDecimal(22)) | ||
} | ||
|
||
/** Method to run code snippet on UI/main thread */ | ||
private fun runOnUI(action: () -> Unit) { | ||
activityScenarioRule.scenario.onActivity { action() } | ||
} | ||
|
||
/** Method to set content view for test activity */ | ||
private fun setTestLayout(view: View) { | ||
activityScenarioRule.scenario.onActivity { activity -> activity.setContentView(view) } | ||
InstrumentationRegistry.getInstrumentation().waitForIdleSync() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.