Skip to content

Commit

Permalink
Create the widget for date time question type
Browse files Browse the repository at this point in the history
  • Loading branch information
jingtang10 committed Feb 1, 2021
1 parent f33fbf3 commit b87fb22
Show file tree
Hide file tree
Showing 13 changed files with 494 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import androidx.test.annotation.UiThreadTest
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.views.QuestionnaireItemDatePickerViewHolderFactory.NUMBER_OF_MICROSECONDS_PER_SECOND
import com.google.android.material.textfield.TextInputLayout
import com.google.common.truth.Truth.assertThat
import com.google.fhir.r4.core.Date
Expand All @@ -44,7 +43,7 @@ class QuestionnaireItemDatePickerViewHolderFactoryInstrumentedTest {
@Before
fun setUp() {
context = ContextThemeWrapper(
InstrumentationRegistry.getInstrumentation().getTargetContext(),
InstrumentationRegistry.getInstrumentation().targetContext,
R.style.Theme_MaterialComponents
)
parent = FrameLayout(context)
Expand All @@ -62,8 +61,9 @@ class QuestionnaireItemDatePickerViewHolderFactoryInstrumentedTest {
)
)

assertThat(viewHolder.itemView.findViewById<TextInputLayout>(R.id.textInputLayout).hint)
.isEqualTo("Question?")
assertThat(
viewHolder.itemView.findViewById<TextInputLayout>(R.id.textInputLayout).hint
).isEqualTo("Question?")
}

@Test
Expand All @@ -79,8 +79,8 @@ class QuestionnaireItemDatePickerViewHolderFactoryInstrumentedTest {
)

assertThat(
viewHolder.itemView.findViewById<TextView>(R.id.textInputEditText).text.toString())
.isEqualTo("")
viewHolder.itemView.findViewById<TextView>(R.id.textInputEditText).text.toString()
).isEqualTo("")
}

@Test
Expand Down Expand Up @@ -111,7 +111,7 @@ class QuestionnaireItemDatePickerViewHolderFactoryInstrumentedTest {
)

assertThat(
viewHolder.itemView.findViewById<TextView>(R.id.textInputEditText).text.toString())
.isEqualTo("2020-01-01")
viewHolder.itemView.findViewById<TextView>(R.id.textInputEditText).text.toString()
).isEqualTo("2020-01-01")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* Copyright 2020 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.widget.FrameLayout
import android.widget.TextView
import androidx.appcompat.view.ContextThemeWrapper
import androidx.test.annotation.UiThreadTest
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.google.android.fhir.datacapture.R
import com.google.android.material.textfield.TextInputLayout
import com.google.common.truth.Truth.assertThat
import com.google.fhir.r4.core.DateTime
import com.google.fhir.r4.core.Questionnaire
import com.google.fhir.r4.core.QuestionnaireResponse
import java.time.LocalDate
import java.time.LocalTime
import java.time.ZoneId
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class QuestionnaireItemDateTimePickerViewHolderFactoryInstrumentedTest {
private lateinit var context: ContextThemeWrapper
private lateinit var parent: FrameLayout
private lateinit var viewHolder: QuestionnaireItemViewHolder

@Before
fun setUp() {
context = ContextThemeWrapper(
InstrumentationRegistry.getInstrumentation().targetContext,
R.style.Theme_MaterialComponents
)
parent = FrameLayout(context)
assertThat(parent).isNotNull()
viewHolder = QuestionnaireItemDateTimePickerViewHolderFactory.create(parent)
}

@Test
fun shouldSetTextViewText() {
viewHolder.bind(
QuestionnaireItemViewItem(
Questionnaire.Item.newBuilder().apply {
text = com.google.fhir.r4.core.String.newBuilder().setValue("Question?").build()
}.build(),
QuestionnaireResponse.Item.newBuilder()
)
)

assertThat(
viewHolder.itemView.findViewById<TextInputLayout>(R.id.dateInputLayout).hint
).isEqualTo("Question?")
assertThat(
viewHolder.itemView.findViewById<TextInputLayout>(R.id.timeInputLayout).hint
).isEqualTo("Question?")
}

@Test
@UiThreadTest
fun shouldSetEmptyDateTimeInput() {
viewHolder.bind(
QuestionnaireItemViewItem(
Questionnaire.Item.newBuilder().apply {
text = com.google.fhir.r4.core.String.newBuilder().setValue("Question?").build()
}.build(),
QuestionnaireResponse.Item.newBuilder()
)
)

assertThat(
viewHolder.itemView.findViewById<TextView>(R.id.dateInputEditText).text.toString()
).isEqualTo("")
assertThat(
viewHolder.itemView.findViewById<TextView>(R.id.timeInputEditText).text.toString()
).isEqualTo("")
}

@Test
@UiThreadTest
fun shouldSetDateTimeInput() {
viewHolder.bind(
QuestionnaireItemViewItem(
Questionnaire.Item.newBuilder().apply {
text = com.google.fhir.r4.core.String.newBuilder().setValue("Question?").build()
}.build(),
QuestionnaireResponse.Item.newBuilder().addAnswer(
QuestionnaireResponse.Item.Answer.newBuilder().apply {
value = QuestionnaireResponse.Item.Answer.ValueX.newBuilder()
.setDateTime(
DateTime.newBuilder()
.setValueUs(
LocalDate
.of(2020, 1, 5)
.atTime(LocalTime.of(1, 30))
.atZone(ZoneId.systemDefault())
.toEpochSecond() * NUMBER_OF_MICROSECONDS_PER_SECOND)
.setPrecision(DateTime.Precision.SECOND)
.setTimezone(ZoneId.systemDefault().id)
).build()
}
)
)
)

assertThat(
viewHolder.itemView.findViewById<TextView>(R.id.dateInputEditText).text.toString()
).isEqualTo("2020-01-05")
assertThat(
viewHolder.itemView.findViewById<TextView>(R.id.timeInputEditText).text.toString()
).isEqualTo("01:30:00")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class QuestionnaireItemEditTextDecimalViewHolderFactoryInstrumentedTest {
@Before
fun setUp() {
context = ContextThemeWrapper(
InstrumentationRegistry.getInstrumentation().getTargetContext(),
InstrumentationRegistry.getInstrumentation().targetContext,
R.style.Theme_MaterialComponents
)
parent = FrameLayout(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.google.android.fhir.datacapture.views.QuestionnaireItemCheckBoxViewHolderFactory
import com.google.android.fhir.datacapture.views.QuestionnaireItemDatePickerViewHolderFactory
import com.google.android.fhir.datacapture.views.QuestionnaireItemDateTimePickerViewHolderFactory
import com.google.android.fhir.datacapture.views.QuestionnaireItemEditTextDecimalViewHolderFactory
import com.google.android.fhir.datacapture.views.QuestionnaireItemEditTextIntegerViewHolderFactory
import com.google.android.fhir.datacapture.views.QuestionnaireItemEditTextMultiLineViewHolderFactory
Expand All @@ -43,6 +44,8 @@ internal class QuestionnaireItemAdapter(
QuestionnaireItemViewHolderType.CHECK_BOX -> QuestionnaireItemCheckBoxViewHolderFactory
QuestionnaireItemViewHolderType.DATE_PICKER ->
QuestionnaireItemDatePickerViewHolderFactory
QuestionnaireItemViewHolderType.DATE_TIME_PICKER ->
QuestionnaireItemDateTimePickerViewHolderFactory
QuestionnaireItemViewHolderType.EDIT_TEXT_SINGLE_LINE ->
QuestionnaireItemEditTextSingleLineViewHolderFactory
QuestionnaireItemViewHolderType.EDIT_TEXT_MULTI_LINE ->
Expand Down Expand Up @@ -73,6 +76,8 @@ internal class QuestionnaireItemAdapter(
QuestionnaireItemTypeCode.Value.GROUP -> QuestionnaireItemViewHolderType.GROUP
QuestionnaireItemTypeCode.Value.BOOLEAN -> QuestionnaireItemViewHolderType.CHECK_BOX
QuestionnaireItemTypeCode.Value.DATE -> QuestionnaireItemViewHolderType.DATE_PICKER
QuestionnaireItemTypeCode.Value.DATE_TIME ->
QuestionnaireItemViewHolderType.DATE_TIME_PICKER
QuestionnaireItemTypeCode.Value.STRING ->
QuestionnaireItemViewHolderType.EDIT_TEXT_SINGLE_LINE
QuestionnaireItemTypeCode.Value.TEXT ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ enum class QuestionnaireItemViewHolderType(val value: Int) {
GROUP(0),
CHECK_BOX(1),
DATE_PICKER(2),
EDIT_TEXT_SINGLE_LINE(3),
EDIT_TEXT_MULTI_LINE(4),
EDIT_TEXT_INTEGER(5),
EDIT_TEXT_DECIMAL(6),
RADIO_GROUP(7);
DATE_TIME_PICKER(3),
EDIT_TEXT_SINGLE_LINE(4),
EDIT_TEXT_MULTI_LINE(5),
EDIT_TEXT_INTEGER(6),
EDIT_TEXT_DECIMAL(7),
RADIO_GROUP(8);

companion object {
private val VALUES = values()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ object QuestionnaireItemDatePickerViewHolderFactory : QuestionnaireItemViewHolde
}

@SuppressLint("NewApi") // java.time APIs can be used due to desugaring
val LOCAL_DATE_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE

const val NUMBER_OF_MICROSECONDS_PER_SECOND = 1000000
const val NUMBER_OF_MICROSECONDS_PER_MILLISECOND = 1000
val LOCAL_DATE_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE!!
}

const val NUMBER_OF_MICROSECONDS_PER_SECOND = 1000000
const val NUMBER_OF_MICROSECONDS_PER_MILLISECOND = 1000

/**
* Returns the [AppCompatActivity] if there exists one wrapped inside [ContextThemeWrapper]s, or
* `null` otherwise.
Expand All @@ -136,7 +136,7 @@ object QuestionnaireItemDatePickerViewHolderFactory : QuestionnaireItemViewHolde
* TODO: find a more robust way to do this as it is not guaranteed that the activity is
* an AppCompatActivity.
*/
private fun Context.tryUnwrapContext(): AppCompatActivity? {
internal fun Context.tryUnwrapContext(): AppCompatActivity? {
var context = this
while (true) {
when (context) {
Expand Down
Loading

0 comments on commit b87fb22

Please sign in to comment.