From d223859cbc049301e263aebdc4620842b95b7af0 Mon Sep 17 00:00:00 2001 From: johan Date: Wed, 28 Feb 2024 13:10:07 +0100 Subject: [PATCH 1/4] Finally solved issue #1734 --- .../ui/lao/event/AbstractEventCreationFragment.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/event/AbstractEventCreationFragment.kt b/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/event/AbstractEventCreationFragment.kt index fa2a748b3e..2addc8f8cb 100644 --- a/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/event/AbstractEventCreationFragment.kt +++ b/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/event/AbstractEventCreationFragment.kt @@ -202,7 +202,9 @@ abstract class AbstractEventCreationFragment : Fragment() { startDateEditText?.setText("") startDate = null - if (compareWithNowByDay(newDate) < 0) { + // let the comparison go to -1 for cases where the time is just after midnight + // this is handled just fine by computeTimeInSeconds() as an Instant also contains the date + if (compareWithNowByDay(newDate) < -1) { showToast(R.string.past_date_not_allowed) return } @@ -219,7 +221,7 @@ abstract class AbstractEventCreationFragment : Fragment() { endTimeEditText?.setText("") } - if (compareWithNowByDay(newDate) == 0) { + if (compareWithNowByDay(newDate) <= 0) { computeTimesInSeconds() } } @@ -251,7 +253,6 @@ abstract class AbstractEventCreationFragment : Fragment() { private fun onStartTime(bundle: Bundle) { startTime = getSelection(bundle) startTimeEditText?.setText(timeFormat.format(startTime!!.time)) - if (startDate != null && endDate != null && startDate == endDate && @@ -319,7 +320,6 @@ abstract class AbstractEventCreationFragment : Fragment() { if (startDate == null || startTime == null) { return false } - completeStartTime[ startDate!![Calendar.YEAR], startDate!![Calendar.MONTH], From 9089d47d0768bf70876457643b918bbfc1a1356b Mon Sep 17 00:00:00 2001 From: johan Date: Mon, 4 Mar 2024 11:32:21 +0100 Subject: [PATCH 2/4] Added a test for start date being in day -1 --- .../event/AbstractEventCreationFragment.kt | 2 ++ .../election/ElectionSetupFragmentTest.kt | 22 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/event/AbstractEventCreationFragment.kt b/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/event/AbstractEventCreationFragment.kt index 2addc8f8cb..947bf97a4c 100644 --- a/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/event/AbstractEventCreationFragment.kt +++ b/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/event/AbstractEventCreationFragment.kt @@ -253,6 +253,7 @@ abstract class AbstractEventCreationFragment : Fragment() { private fun onStartTime(bundle: Bundle) { startTime = getSelection(bundle) startTimeEditText?.setText(timeFormat.format(startTime!!.time)) + if (startDate != null && endDate != null && startDate == endDate && @@ -320,6 +321,7 @@ abstract class AbstractEventCreationFragment : Fragment() { if (startDate == null || startTime == null) { return false } + completeStartTime[ startDate!![Calendar.YEAR], startDate!![Calendar.MONTH], diff --git a/fe2-android/app/src/test/ui/robolectric/com/github/dedis/popstellar/ui/lao/event/election/ElectionSetupFragmentTest.kt b/fe2-android/app/src/test/ui/robolectric/com/github/dedis/popstellar/ui/lao/event/election/ElectionSetupFragmentTest.kt index c1d87e5076..c6131b95ea 100644 --- a/fe2-android/app/src/test/ui/robolectric/com/github/dedis/popstellar/ui/lao/event/election/ElectionSetupFragmentTest.kt +++ b/fe2-android/app/src/test/ui/robolectric/com/github/dedis/popstellar/ui/lao/event/election/ElectionSetupFragmentTest.kt @@ -325,7 +325,6 @@ class ElectionSetupFragmentTest { ) } - // @matteosz: This test will fail if executed between 00:00 and 00:10! TO-FIX @Test fun cannotChooseStartTimeTooFarInPast() { val today = Calendar.getInstance() @@ -347,6 +346,27 @@ class ElectionSetupFragmentTest { EventCreationPageObject.startTimeView().check(ViewAssertions.matches(ViewMatchers.withText(""))) } + @Test + fun cannotChooseStartTimeInPastDay() { + val today = Calendar.getInstance() + today.add(Calendar.MINUTE, -1430) + val year = today[Calendar.YEAR] + val monthOfYear = today[Calendar.MONTH] + val dayOfMonth = today[Calendar.DAY_OF_MONTH] + val hourOfDay = today[Calendar.HOUR_OF_DAY] + val minutes = today[Calendar.MINUTE] + + EventCreationPageObject.startDateView().perform(ViewActions.click()) + getLastDialog(DatePickerDialog::class.java).updateDate(year, monthOfYear, dayOfMonth) + dialogPositiveButton().performClick() + + EventCreationPageObject.startTimeView().perform(ViewActions.click()) + getLastDialog(TimePickerDialog::class.java).updateTime(hourOfDay, minutes) + dialogPositiveButton().performClick() + + EventCreationPageObject.startTimeView().check(ViewAssertions.matches(ViewMatchers.withText(""))) + } + @Test @Ignore("Not implemented") fun choosingStartDateInvalidateAStartTimeInPast() { From 5f79276d34baa20be10476b42e3bd8591a7aecfb Mon Sep 17 00:00:00 2001 From: johan Date: Mon, 4 Mar 2024 18:55:30 +0100 Subject: [PATCH 3/4] Formatting (ktfmt) --- .../election/ElectionSetupFragmentTest.kt | 40 ++++++++++++------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/fe2-android/app/src/test/ui/robolectric/com/github/dedis/popstellar/ui/lao/event/election/ElectionSetupFragmentTest.kt b/fe2-android/app/src/test/ui/robolectric/com/github/dedis/popstellar/ui/lao/event/election/ElectionSetupFragmentTest.kt index c6131b95ea..907ebe1f32 100644 --- a/fe2-android/app/src/test/ui/robolectric/com/github/dedis/popstellar/ui/lao/event/election/ElectionSetupFragmentTest.kt +++ b/fe2-android/app/src/test/ui/robolectric/com/github/dedis/popstellar/ui/lao/event/election/ElectionSetupFragmentTest.kt @@ -36,12 +36,6 @@ import dagger.hilt.android.testing.HiltAndroidRule import dagger.hilt.android.testing.HiltAndroidTest import io.reactivex.Completable import io.reactivex.subjects.BehaviorSubject -import java.text.DateFormat -import java.text.SimpleDateFormat -import java.time.Instant -import java.util.Calendar -import java.util.Locale -import javax.inject.Inject import org.hamcrest.MatcherAssert import org.hamcrest.Matchers import org.junit.Assert @@ -54,26 +48,44 @@ import org.mockito.Mock import org.mockito.Mockito import org.mockito.junit.MockitoJUnit import org.mockito.junit.MockitoTestRule +import java.text.DateFormat +import java.text.SimpleDateFormat +import java.time.Instant +import java.util.Calendar +import java.util.Locale +import javax.inject.Inject @LargeTest @HiltAndroidTest @RunWith(AndroidJUnit4::class) class ElectionSetupFragmentTest { - @Inject lateinit var keyManager: KeyManager + @Inject + lateinit var keyManager: KeyManager - @Inject lateinit var messageHandler: MessageHandler + @Inject + lateinit var messageHandler: MessageHandler - @Inject lateinit var gson: Gson + @Inject + lateinit var gson: Gson - @BindValue @Mock lateinit var repository: LAORepository + @BindValue + @Mock + lateinit var repository: LAORepository - @BindValue @Mock lateinit var globalNetworkManager: GlobalNetworkManager + @BindValue + @Mock + lateinit var globalNetworkManager: GlobalNetworkManager - @Mock lateinit var messageSender: MessageSender + @Mock + lateinit var messageSender: MessageSender - @JvmField @Rule(order = 0) val mockitoRule: MockitoTestRule = MockitoJUnit.testRule(this) + @JvmField + @Rule(order = 0) + val mockitoRule: MockitoTestRule = MockitoJUnit.testRule(this) - @JvmField @Rule(order = 1) val hiltRule = HiltAndroidRule(this) + @JvmField + @Rule(order = 1) + val hiltRule = HiltAndroidRule(this) @JvmField @Rule(order = 2) From ef88b57f9896a4b7baac92dc52ae08b058ee5fc7 Mon Sep 17 00:00:00 2001 From: johan Date: Mon, 4 Mar 2024 22:43:40 +0100 Subject: [PATCH 4/4] Refactored variable --- .../popstellar/ui/lao/event/AbstractEventCreationFragment.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/event/AbstractEventCreationFragment.kt b/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/event/AbstractEventCreationFragment.kt index 947bf97a4c..fa619b12cf 100644 --- a/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/event/AbstractEventCreationFragment.kt +++ b/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/event/AbstractEventCreationFragment.kt @@ -199,12 +199,13 @@ abstract class AbstractEventCreationFragment : Fragment() { private fun onStartDate(bundle: Bundle) { val newDate = getSelection(bundle) + val daysFromNow = compareWithNowByDay(newDate) startDateEditText?.setText("") startDate = null // let the comparison go to -1 for cases where the time is just after midnight // this is handled just fine by computeTimeInSeconds() as an Instant also contains the date - if (compareWithNowByDay(newDate) < -1) { + if (daysFromNow < -1) { showToast(R.string.past_date_not_allowed) return } @@ -221,7 +222,7 @@ abstract class AbstractEventCreationFragment : Fragment() { endTimeEditText?.setText("") } - if (compareWithNowByDay(newDate) <= 0) { + if (daysFromNow <= 0) { computeTimesInSeconds() } }