diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 83c03500f..265ecb738 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -147,6 +147,12 @@ dependencies { implementation(libs.androidx.print) implementation(libs.bundles.room) implementation(libs.androidx.work.runtime.ktx) + testImplementation(libs.junit) + androidTestImplementation(libs.androidx.test.core) + androidTestImplementation(libs.androidx.test.runner) + androidTestImplementation(libs.androidx.test.rules) + androidTestImplementation(libs.androidx.test.ext.junit) + androidTestImplementation(libs.androidx.test.ext.truth) ksp(libs.androidx.room.compiler) detektPlugins(libs.compose.detekt) } diff --git a/app/src/androidTest/kotlin/org/fossify/calendar/interfaces/EventsDaoGetOneTimeEventsFromToWithCalendarIdsTest.kt b/app/src/androidTest/kotlin/org/fossify/calendar/interfaces/EventsDaoGetOneTimeEventsFromToWithCalendarIdsTest.kt new file mode 100644 index 000000000..e11e1234f --- /dev/null +++ b/app/src/androidTest/kotlin/org/fossify/calendar/interfaces/EventsDaoGetOneTimeEventsFromToWithCalendarIdsTest.kt @@ -0,0 +1,85 @@ +package org.fossify.calendar.interfaces + +import android.content.Context +import androidx.room.Room +import androidx.test.core.app.ApplicationProvider +import androidx.test.ext.junit.runners.AndroidJUnit4 +import org.fossify.calendar.databases.EventsDatabase +import org.fossify.calendar.extensions.seconds +import org.fossify.calendar.models.Event +import org.fossify.calendar.testing.expectedFailure +import org.joda.time.DateTime +import org.junit.After +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import java.io.IOException + +@RunWith(AndroidJUnit4::class) +class EventsDaoGetOneTimeEventsFromToWithCalendarIdsTest { + + private lateinit var eventsDao: EventsDao + private lateinit var db: EventsDatabase + + private val calendarId = 1L + + @Before + fun createDb() { + val context = ApplicationProvider.getApplicationContext() + db = Room.inMemoryDatabaseBuilder( + context, EventsDatabase::class.java).build() + eventsDao = db.EventsDao() + } + + @After + @Throws(IOException::class) + fun closeDb() { + db.close() + } + + @Test + @Throws(Exception::class) + fun bug255_EventEndingAtMidnightShouldNotShowOnNextDay() { + expectedFailure("https://github.com/FossifyOrg/Calendar/issues/255") { + val startDay = DateTime(2026, 1, 1, 0, 0) + val startEvent = startDay.plusHours(23) + val event = Event(id = 0, startTS = startEvent.seconds(), endTS = startEvent.plusHours(1).seconds(), calendarId = calendarId) + eventsDao.insertOrUpdate(event) + + val eventsDayOne = eventsDao.getOneTimeEventsFromToWithCalendarIds(startDay.plusDays(1).seconds(), startDay.seconds(), listOf(calendarId)) + val eventsDayTwo = eventsDao.getOneTimeEventsFromToWithCalendarIds(startDay.plusDays(2).seconds(), startDay.plusDays(1).seconds(), listOf(calendarId)) + + Assert.assertEquals(1, eventsDayOne.count()) + Assert.assertEquals(emptyList(), eventsDayTwo) + } + } + + @Test + @Throws(Exception::class) + fun bug255_EventStartingAtMidnightWithoutDurationShouldOnlyShowOnSingleDay() { + expectedFailure("https://github.com/FossifyOrg/Calendar/issues/255") { + val startDay = DateTime(2026, 1, 10, 0, 0) + val event = Event(id = 0, startTS = startDay.seconds(), endTS = startDay.seconds(), calendarId = calendarId) + eventsDao.insertOrUpdate(event) + + val eventsDayOne = eventsDao.getOneTimeEventsFromToWithCalendarIds(startDay.seconds(), startDay.plusDays(-1).seconds(), listOf(calendarId)) + val eventsDayTwo = eventsDao.getOneTimeEventsFromToWithCalendarIds(startDay.plusDays(1).seconds(), startDay.seconds(), listOf(calendarId)) + + Assert.assertEquals(1, eventsDayTwo.count()) + Assert.assertEquals(emptyList(), eventsDayOne) + } + } + + @Test + @Throws(Exception::class) + fun bug440_EventAtMidnightOnFirstJan1970() { + expectedFailure("https://github.com/FossifyOrg/Calendar/issues/440") { + eventsDao.insertOrUpdate(Event(id = 0, startTS = 0, endTS = 3600, calendarId = calendarId)) + + val eventsOnFirstJan1970 = eventsDao.getOneTimeEventsFromToWithCalendarIds(86400, 0, listOf(calendarId)) + + Assert.assertEquals(1, eventsOnFirstJan1970.count()) + } + } +} diff --git a/app/src/androidTest/kotlin/org/fossify/calendar/testing/ExpectedFailureUtils.kt b/app/src/androidTest/kotlin/org/fossify/calendar/testing/ExpectedFailureUtils.kt new file mode 100644 index 000000000..94de4fc7a --- /dev/null +++ b/app/src/androidTest/kotlin/org/fossify/calendar/testing/ExpectedFailureUtils.kt @@ -0,0 +1,13 @@ +package org.fossify.calendar.testing + +fun expectedFailure(testName: String = "", block: () -> Unit) { + try { + block() + println("WARNING: Expected failure test '$testName' passed unexpectedly.") + } catch (e: Throwable) { + println("Expected failure in test '$testName': ${e.javaClass.simpleName}: ${e.message}") + return + } + + throw AssertionError("Expected failure test '$testName' passed unexpectedly. Investigate: bug may be fixed.") +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index e93f7ccfb..721b115e7 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -13,6 +13,13 @@ swiperefreshlayout = "1.2.0" work = "2.11.1" #Room room = "2.8.4" +#junit +junit = "4.13.2" +androidx-test-core = "1.7.0" +androidx-test-runner = "1.7.0" +androidx-test-rules = "1.7.0" +androidx-test-ext-junit = "1.3.0" +androidx-test-ext-truth = "1.7.0" #Fossify commons = "6.1.5" #Gradle @@ -35,6 +42,13 @@ androidx-room-ktx = { module = "androidx.room:room-ktx", version.ref = "room" } androidx-room-compiler = { module = "androidx.room:room-compiler", version.ref = "room" } #Compose compose-detekt = { module = "io.nlopez.compose.rules:detekt", version.ref = "detektCompose" } +#junit +junit = { group = "junit", name = "junit", version.ref = "junit" } +androidx-test-core = { group = "androidx.test", name = "core", version.ref = "androidx-test-core" } +androidx-test-runner = { group = "androidx.test", name = "runner", version.ref = "androidx-test-runner" } +androidx-test-rules = { group = "androidx.test", name = "rules", version.ref = "androidx-test-rules" } +androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-ext-junit" } +androidx-test-ext-truth = { group = "androidx.test.ext", name = "truth", version.ref = "androidx-test-ext-truth" } #Fossify fossify-commons = { module = "org.fossify:commons", version.ref = "commons" } [bundles]