Cotton code
100 lines · 4.5 KB · KotlinRaw
package com.cottondb.daysimport android.app.Applicationimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.material3.Textimport androidx.compose.ui.Modifierimport androidx.compose.ui.geometry.Offsetimport androidx.compose.ui.platform.testTagimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.clickimport androidx.compose.ui.test.getBoundsInRootimport androidx.compose.ui.test.performScrollToimport androidx.compose.ui.test.swipeDownimport androidx.compose.ui.test.swipeUpimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.performTouchInputimport androidx.compose.ui.unit.dpimport com.cottondb.days.ui.SheetHostimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport 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)    }}