Cotton code
107 lines · 4.3 KB · KotlinRaw
package com.cottondb.storyimport android.app.Applicationimport androidx.activity.ComponentActivityimport androidx.activity.compose.BackHandlerimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.material3.Textimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.setValueimport androidx.compose.ui.Modifierimport androidx.compose.foundation.layout.Columnimport androidx.compose.ui.platform.testTagimport androidx.compose.ui.test.junit4.createAndroidComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.performTouchInputimport androidx.compose.ui.test.swipeDownimport androidx.compose.ui.test.swipeUpimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.performScrollToimport com.cottondb.story.ink.InkSheetimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * Story's sheets (InkSheet) as a person meets them on a page: Back closes the sheet and leaves the page standing (About's * Delete this script? went with About at one Back on the emulator, 21 Nov 2026); pulled down, a sheet closes, as the * speaker panel and the iPhone's sheets do (none of them did on the emulator), and a long sheet's rows scroll first. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h700dp-mdpi", application = Application::class)class StorySheetTest {    @get:Rule val compose = createAndroidComposeRule<ComponentActivity>()    private var pageOpen by mutableStateOf(true)    private var asking by mutableStateOf(true)    /** A page with its own back (as StorySurface's), and a sheet of the page's own over it (as About's). */    private fun pageWithSheet() {        compose.setContent {            BackHandler(pageOpen) { pageOpen = false }            if (pageOpen) Box(Modifier.fillMaxSize()) {                Text("About this script")                if (asking) InkSheet("Delete this script?", "Close", "ask", onClose = { asking = false }) { Text("Delete") }            }        }    }    private fun back() {        compose.runOnUiThread { compose.activity.onBackPressedDispatcher.onBackPressed() }        compose.waitForIdle()    }    @Test fun back_closes_the_sheet_and_leaves_its_page() {        pageWithSheet()        back()        assertFalse("the sheet stays after Back", asking)        assertTrue("Back closed the page under the sheet", pageOpen)        back()        assertFalse(pageOpen)        assertEquals(false, asking)    }    private var closed = 0    /** A sheet of [rows] invented rows, as Your scripts or Merge with another stand with many. */    private fun sheet(rows: Int) {        compose.setContent {            if (closed == 0) InkSheet("Your scripts", "Close", "sheet", onClose = { closed++ }) {                Column { (1..rows).forEach { Text("Script $it", Modifier.testTag("script-$it")) } }            }        }    }    @Test fun a_sheet_pulled_down_closes() {        sheet(rows = 3)        compose.onNodeWithTag("sheet").performTouchInput { swipeDown(startY = top + 10f, endY = bottom, durationMillis = 300) }        compose.waitForIdle()        assertEquals(1, closed)    }    @Test fun a_little_pull_settles_back() {        sheet(rows = 3)        compose.onNodeWithTag("sheet").performTouchInput { swipeDown(startY = top + 10f, endY = top + 40f, durationMillis = 600) }        compose.waitForIdle()        assertEquals(0, closed)    }    @Test fun a_long_sheet_scrolls_its_rows_before_it_moves() {        sheet(rows = 80)        compose.onNodeWithTag("script-80").performScrollTo().assertIsDisplayed()        // Scrolled to its foot, a pull down brings the rows back first: the sheet stays.        compose.onNodeWithTag("sheet").performTouchInput { swipeDown(startY = top + 40f, endY = top + 300f, durationMillis = 400) }        compose.waitForIdle()        assertEquals(0, closed)        compose.onNodeWithTag("sheet").performTouchInput { swipeUp(startY = bottom - 40f, endY = top + 40f, durationMillis = 400) }        compose.waitForIdle()        assertEquals(0, closed)    }}