package com.cottondb.days import android.app.Application import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.material3.Text import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset import androidx.compose.ui.platform.testTag import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.click import androidx.compose.ui.test.getBoundsInRoot import androidx.compose.ui.test.performScrollTo import androidx.compose.ui.test.swipeDown import androidx.compose.ui.test.swipeUp import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.performTouchInput import androidx.compose.ui.unit.dp import com.cottondb.days.ui.SheetHost import org.junit.Assert.assertEquals import org.junit.Assert.assertTrue import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.annotation.Config /** * A sheet over Days (the month, Another day, the filter, When, the item's sheet). On the emulator a tap on the month * sheet's blank paper (beside the grip, left of Today) closed it (21 Nov 2026): the tap went through to the veil; and * on its side the month and Another day did not scroll, so their last rows and Today were out of reach. */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], qualifiers = "w400dp-h700dp-mdpi", application = Application::class) class DaysSheetTest { @get:Rule val compose = createComposeRule() private var closed = 0 private fun sheet(lines: Int = 8) { compose.setContent { Box(Modifier.fillMaxSize()) { Box(Modifier.fillMaxWidth().height(4.dp).testTag("veil-spot")) SheetHost(onClose = { closed++ }, tag = "sheet") { (1..lines).forEach { Text("Invented line $it", Modifier.fillMaxWidth().height(48.dp).testTag("line-$it")) } } } } } @Test fun a_tap_on_the_sheets_blank_paper_keeps_it_open() { sheet() compose.onNodeWithTag("sheet").performTouchInput { click(Offset(width * 0.85f, 6f)) } compose.onNodeWithTag("line-3").performTouchInput { click(Offset(width - 4f, centerY)) } compose.onNodeWithTag("sheet").performTouchInput { click(Offset(width - 2f, bottom - 2f)) } compose.waitForIdle() assertEquals(0, closed) compose.onNodeWithTag("veil-spot").performTouchInput { click() } compose.waitForIdle() assertEquals("a tap outside still closes it", 1, closed) } /** The grip showed, but a pull down did nothing. */ @Test fun a_pull_down_on_the_grip_closes_the_sheet() { sheet() compose.onNodeWithTag("sheet-grip").performTouchInput { swipeDown(startY = top, endY = top + 400f, durationMillis = 300) } compose.waitForIdle() assertEquals(1, closed) } @Test fun a_pull_down_on_the_lines_at_their_top_closes_the_sheet() { sheet(lines = 60) compose.onNodeWithTag("sheet-scroll").performTouchInput { swipeDown(startY = top + 20f, endY = top + 420f, durationMillis = 300) } compose.waitForIdle() assertEquals(1, closed) } @Test fun a_short_pull_springs_back_and_closes_nothing() { sheet() compose.onNodeWithTag("sheet-grip").performTouchInput { swipeDown(startY = top, endY = top + 40f, durationMillis = 600) } compose.waitForIdle() assertEquals(0, closed) } /** On its side the month and Another day were cut off below their fourth row, Today out of reach. */ @Test fun a_tall_sheet_scrolls_and_its_last_line_is_reachable() { sheet(lines = 60) val before = compose.onNodeWithTag("line-1").getBoundsInRoot().top compose.onNodeWithTag("sheet-scroll").performTouchInput { swipeUp(startY = bottom - 10f, endY = top + 10f, durationMillis = 300) } compose.waitForIdle() assertEquals(0, closed) assertTrue("the lines scrolled", compose.onNodeWithTag("line-1").getBoundsInRoot().top < before) compose.onNodeWithTag("line-60").performScrollTo().assertIsDisplayed() val sheet = compose.onNodeWithTag("sheet").getBoundsInRoot() assertTrue("the last line stands inside the sheet", compose.onNodeWithTag("line-60").getBoundsInRoot().bottom <= sheet.bottom) } }