Skip to content

Commit 1825619

Browse files
authored
feat: [ANDROAPP-5952] Update design system and adapt InputAge to new … (#3531)
* feat: [ANDROAPP-5952] Update design system and adapt InputAge to new implementation, add tests for component * fix: [ANDROAPP-5952] take timezone into account for date setting * fix: [ANDROAPP-5952] code clean up * fix: [ANDROAPP-5952] update design system * fix: [ANDROAPP-5952] fix test
1 parent 09192f4 commit 1825619

File tree

5 files changed

+275
-73
lines changed

5 files changed

+275
-73
lines changed

app/src/main/java/org/dhis2/usescases/eventsWithoutRegistration/eventDetails/ui/EventDetailsViewModel.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ import org.hisp.dhis.mobile.ui.designsystem.component.SelectableDates
3535
import java.text.SimpleDateFormat
3636
import java.util.Calendar
3737
import java.util.Date
38+
import java.util.GregorianCalendar
3839
import java.util.Locale
40+
import java.util.TimeZone
3941

4042
class EventDetailsViewModel(
4143
private val configureEventDetails: ConfigureEventDetails,
@@ -266,7 +268,22 @@ class EventDetailsViewModel(
266268
val calendar = Calendar.getInstance()
267269
calendar[year, month, day, 0, 0] = 0
268270
calendar[Calendar.MILLISECOND] = 0
271+
272+
val currentTimeZone: TimeZone = calendar.getTimeZone()
273+
val currentDt: Calendar = GregorianCalendar(currentTimeZone, Locale.getDefault())
274+
275+
var gmtOffset: Int = currentTimeZone.getOffset(
276+
currentDt[Calendar.ERA],
277+
currentDt[Calendar.YEAR],
278+
currentDt[Calendar.MONTH],
279+
currentDt[Calendar.DAY_OF_MONTH],
280+
currentDt[Calendar.DAY_OF_WEEK],
281+
currentDt[Calendar.MILLISECOND],
282+
)
283+
gmtOffset /= (60 * 60 * 1000)
284+
calendar.add(Calendar.HOUR_OF_DAY, +gmtOffset)
269285
val selectedDate = calendar.time
286+
270287
setUpEventReportDate(selectedDate)
271288
}
272289

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
package org.dhis2.form.ui.provider.inputfield
2+
3+
import androidx.compose.ui.Modifier
4+
import androidx.compose.ui.platform.testTag
5+
import androidx.compose.ui.test.assertIsDisplayed
6+
import androidx.compose.ui.test.assertTextEquals
7+
import androidx.compose.ui.test.junit4.createComposeRule
8+
import androidx.compose.ui.test.onNodeWithTag
9+
import androidx.compose.ui.test.onNodeWithText
10+
import androidx.compose.ui.test.performClick
11+
import androidx.compose.ui.test.performTextInput
12+
import androidx.compose.ui.test.printToLog
13+
import androidx.test.platform.app.InstrumentationRegistry
14+
import org.dhis2.form.di.Injector
15+
import org.hisp.dhis.android.core.common.ValueType
16+
import org.hisp.dhis.mobile.ui.designsystem.component.InputStyle
17+
import org.junit.Rule
18+
import org.junit.Test
19+
20+
21+
class AgeProviderTest {
22+
val resourceManager = Injector.provideResourcesManager(InstrumentationRegistry.getInstrumentation().getContext())
23+
companion object {
24+
25+
const val AGE_VALUE = "2023-01-19"
26+
const val INPUT_AGE_RESET_BUTTON = "INPUT_AGE_RESET_BUTTON"
27+
const val INPUT_AGE = "INPUT_AGE"
28+
const val INPUT_AGE_MODE_SELECTOR = "INPUT_AGE_MODE_SELECTOR"
29+
const val DATE_OF_BIRTH = "DATE OF BIRTH"
30+
const val AGE_BUTTON_TEXT = "AGE"
31+
const val INPUT_AGE_OPEN_CALENDAR_BUTTON = "INPUT_AGE_OPEN_CALENDAR_BUTTON"
32+
const val INPUT_AGE_TIME_UNIT_SELECTOR = "INPUT_AGE_TIME_UNIT_SELECTOR"
33+
const val INPUT_AGE_TEXT_FIELD = "INPUT_AGE_TEXT_FIELD"
34+
const val RADIO_BUTTON_months = "RADIO_BUTTON_months"
35+
const val RADIO_BUTTON_days = "RADIO_BUTTON_days"
36+
const val RADIO_BUTTON_years = "RADIO_BUTTON_years"
37+
const val AGE_SELECTOR_TEXT = "6 years"
38+
const val INPUT_AGE_TEST_TAG = "INPUT_AGE"
39+
const val FIELD_UI_MODEL_UID = "FieldUIModelUid"
40+
41+
}
42+
43+
@get:Rule
44+
val composeTestRule = createComposeRule()
45+
46+
@Test
47+
fun shouldDisplayInputAgeCorrectlyWhenModelHasValue() {
48+
49+
val dateValueTypeFieldUiModel =
50+
generateFieldUiModel(FIELD_UI_MODEL_UID, AGE_VALUE, AGE_VALUE, ValueType.DATE)
51+
composeTestRule.setContent {
52+
ProvideInputAge(
53+
inputStyle = InputStyle.DataInputStyle(),
54+
modifier = Modifier.testTag(INPUT_AGE_TEST_TAG),
55+
fieldUiModel = dateValueTypeFieldUiModel,
56+
intentHandler = {},
57+
resources = resourceManager,
58+
)
59+
}
60+
composeTestRule.onNodeWithTag(INPUT_AGE_TEST_TAG).assertIsDisplayed()
61+
62+
}
63+
64+
@Test
65+
fun shouldDisplayTextButtonSelectorWhenValueIsEmptyString() {
66+
67+
val dateValueTypeFieldUiModel =
68+
generateFieldUiModel(FIELD_UI_MODEL_UID, "", AGE_VALUE, ValueType.DATE)
69+
composeTestRule.setContent {
70+
ProvideInputAge(
71+
inputStyle = InputStyle.DataInputStyle(),
72+
modifier = Modifier.testTag(INPUT_AGE_TEST_TAG),
73+
fieldUiModel = dateValueTypeFieldUiModel,
74+
intentHandler = {},
75+
resources = resourceManager,
76+
)
77+
}
78+
composeTestRule.onNodeWithTag(INPUT_AGE)
79+
composeTestRule.onNodeWithTag(INPUT_AGE_MODE_SELECTOR)
80+
81+
}
82+
83+
@Test
84+
fun shouldDisplayInputDateWhenClickingOnDateButton() {
85+
86+
val dateValueTypeFieldUiModel =
87+
generateFieldUiModel(FIELD_UI_MODEL_UID, "", AGE_VALUE, ValueType.DATE)
88+
composeTestRule.setContent {
89+
ProvideInputAge(
90+
inputStyle = InputStyle.DataInputStyle(),
91+
92+
modifier = Modifier.testTag(INPUT_AGE_TEST_TAG),
93+
fieldUiModel = dateValueTypeFieldUiModel,
94+
intentHandler = {},
95+
resources = resourceManager,
96+
)
97+
}
98+
composeTestRule.onNodeWithTag(INPUT_AGE)
99+
composeTestRule.onNodeWithTag(INPUT_AGE_MODE_SELECTOR)
100+
composeTestRule.onNodeWithText(DATE_OF_BIRTH).performClick()
101+
composeTestRule.onNodeWithTag(INPUT_AGE_OPEN_CALENDAR_BUTTON).assertIsDisplayed()
102+
103+
}
104+
105+
106+
@Test
107+
fun shouldDisplayYearMonthDaySelector() {
108+
109+
val dateValueTypeFieldUiModel =
110+
generateFieldUiModel(FIELD_UI_MODEL_UID, "", AGE_VALUE, ValueType.DATE)
111+
composeTestRule.setContent {
112+
ProvideInputAge(
113+
inputStyle = InputStyle.DataInputStyle(),
114+
modifier = Modifier.testTag(INPUT_AGE_TEST_TAG),
115+
fieldUiModel = dateValueTypeFieldUiModel,
116+
intentHandler = {},
117+
resources = resourceManager,
118+
)
119+
120+
}
121+
composeTestRule.onNodeWithTag(INPUT_AGE)
122+
composeTestRule.onNodeWithTag(INPUT_AGE_MODE_SELECTOR)
123+
composeTestRule.onNodeWithText(AGE_BUTTON_TEXT).performClick()
124+
composeTestRule.onNodeWithTag(INPUT_AGE_TIME_UNIT_SELECTOR).assertIsDisplayed()
125+
}
126+
127+
@Test
128+
fun shouldNotChangeValueWhenUsingAgeSelectorRadioButtons() {
129+
130+
val dateValueTypeFieldUiModel =
131+
generateFieldUiModel(FIELD_UI_MODEL_UID, "", AGE_VALUE, ValueType.DATE)
132+
composeTestRule.setContent {
133+
ProvideInputAge(
134+
inputStyle = InputStyle.DataInputStyle(),
135+
modifier = Modifier.testTag(INPUT_AGE_TEST_TAG),
136+
fieldUiModel = dateValueTypeFieldUiModel,
137+
intentHandler = {},
138+
resources = resourceManager,
139+
)
140+
}
141+
composeTestRule.onNodeWithTag(INPUT_AGE)
142+
composeTestRule.onNodeWithTag(INPUT_AGE_MODE_SELECTOR)
143+
composeTestRule.onNodeWithText(AGE_BUTTON_TEXT).performClick()
144+
composeTestRule.onNodeWithTag(INPUT_AGE_TIME_UNIT_SELECTOR).assertIsDisplayed()
145+
composeTestRule.onNodeWithTag(INPUT_AGE_TEXT_FIELD).performTextInput("6")
146+
composeTestRule.onNodeWithTag(RADIO_BUTTON_months).performClick()
147+
composeTestRule.onNodeWithTag(RADIO_BUTTON_days).performClick()
148+
composeTestRule.onNodeWithTag(RADIO_BUTTON_years).performClick()
149+
composeTestRule.onNodeWithTag(INPUT_AGE_TEXT_FIELD).printToLog("AGE_SELECTOR_TEXT")
150+
151+
composeTestRule.onNodeWithTag(INPUT_AGE_TEXT_FIELD).assertTextEquals(AGE_SELECTOR_TEXT)
152+
153+
}
154+
@Test
155+
fun shouldDisplayTextButtonSelectorWhenTappingResetButton() {
156+
157+
val dateValueTypeFieldUiModel =
158+
generateFieldUiModel(FIELD_UI_MODEL_UID, "", AGE_VALUE, ValueType.DATE)
159+
composeTestRule.setContent {
160+
ProvideInputAge(
161+
inputStyle = InputStyle.DataInputStyle(),
162+
modifier = Modifier.testTag(INPUT_AGE_TEST_TAG),
163+
fieldUiModel = dateValueTypeFieldUiModel,
164+
intentHandler = {},
165+
resources = resourceManager,
166+
)
167+
}
168+
composeTestRule.onNodeWithText(AGE_BUTTON_TEXT).performClick()
169+
composeTestRule.onNodeWithTag(INPUT_AGE_RESET_BUTTON).assertIsDisplayed().performClick()
170+
composeTestRule.onNodeWithTag(INPUT_AGE_MODE_SELECTOR).assertIsDisplayed()
171+
172+
}
173+
174+
@Test
175+
fun shouldDisplayDatePickerWhenTappingOnCalendarButton() {
176+
177+
val dateValueTypeFieldUiModel =
178+
generateFieldUiModel(FIELD_UI_MODEL_UID, AGE_VALUE, AGE_VALUE, ValueType.DATE)
179+
composeTestRule.setContent {
180+
ProvideInputAge(
181+
inputStyle = InputStyle.DataInputStyle(),
182+
modifier = Modifier.testTag(INPUT_AGE_TEST_TAG),
183+
fieldUiModel = dateValueTypeFieldUiModel,
184+
intentHandler = {},
185+
resources = resourceManager,
186+
)
187+
}
188+
composeTestRule.onNodeWithTag(INPUT_AGE_OPEN_CALENDAR_BUTTON).assertIsDisplayed().performClick()
189+
composeTestRule.waitForIdle()
190+
composeTestRule.onNodeWithTag("DATE_PICKER").assertIsDisplayed()
191+
192+
}
193+
194+
}

0 commit comments

Comments
 (0)