Cotton code
134 lines · 6.6 KB · KotlinRaw
package com.cottondb.notesimport 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.ui.test.assertIsDisplayedimport androidx.compose.ui.test.junit4.createAndroidComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onRootimport androidx.compose.ui.test.performScrollToimport androidx.compose.ui.test.onNodeWithTextimport androidx.compose.ui.test.performClickimport androidx.compose.ui.test.performTouchInputimport androidx.compose.ui.test.swipeDownimport androidx.compose.ui.test.clickimport androidx.compose.ui.geometry.Offsetimport androidx.compose.ui.text.font.FontFamilyimport com.cottondb.writing.theme.Writingimport com.cottondb.writing.theme.WritingThemeimport com.cottondb.writing.theme.WritingTypeimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.jsonObjectimport 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/** * Notes' sheets (a held row's menu, a note's More, the export formats, the asks) as a person meets them, with invented * acts. On the emulator (21 Nov 2026) Back on a held row's menu left the app. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h700dp-mdpi", application = Application::class)class NotesSheetTest {    @get:Rule val compose = createAndroidComposeRule<ComponentActivity>()    private val theme = WritingTheme.of("linen", "Linen", Json.parseToJsonElement("""{"ground":"#F7F5F0","text":"#2E2E31","heading":"#1C1C1E","secondary":"#8A8A8F","accent":"#D2453A"}""").jsonObject)    private val type = WritingType(FontFamily.Default, 17f, 1.5f, 600f, FontFamily.Monospace)    private var listStands by mutableStateOf(true)    private var menuOpen by mutableStateOf(true)    private val acts = listOf("pin" to "Pin", "tags" to "Tags", "find" to "Find in this note", "archive" to "Archive", "share" to "Share", "export" to "Export as a file", "bin" to "Move to the bin")        .map { (id, label) -> sheetAct(id, label, id, red = id == "bin") }    private fun back() {        compose.runOnUiThread { compose.activity.onBackPressedDispatcher.onBackPressed() }        compose.waitForIdle()    }    @Test fun back_closes_a_held_rows_menu_and_leaves_the_list() {        compose.setContent {            Writing(theme, type) {                // The list's own Back (as the list's, or the activity leaving), and a held row's menu over it.                BackHandler(listStands) { listStands = false }                if (listStands) Box(Modifier.fillMaxSize()) {                    Text("Notes")                    if (menuOpen) ActSheet(acts, "Close", title = "Amber note 01", onAct = {}) { menuOpen = false }                }            }        }        compose.waitForIdle()        back()        assertFalse("the menu stays after Back", menuOpen)        assertTrue("Back took the list away under the menu", listStands)    }    private fun menu() {        compose.setContent { Writing(theme, type) { if (menuOpen) ActSheet(acts, "Close", title = "Amber note 01", onAct = {}) { menuOpen = false } } }        compose.waitForIdle()    }    /** Pulled down from its title, the sheet closes; a short pull settles back. (On the emulator a pull did nothing.) */    @Test fun a_sheet_pulled_down_closes_and_a_short_pull_settles_back() {        menu()        compose.onNodeWithText("Amber note 01").performTouchInput { swipeDown(startY = centerY, endY = centerY + 40f, durationMillis = 400) }        compose.waitForIdle()        assertTrue("a short pull closed the sheet", menuOpen)        compose.onNodeWithText("Amber note 01").performTouchInput { swipeDown(startY = centerY, endY = centerY + 400f, durationMillis = 300) }        compose.waitForIdle()        assertFalse("the sheet stays after a long pull down", menuOpen)    }    /** A tap on the sheet itself (its title) closes nothing; a tap on the screen above it does. */    @Test fun a_tap_on_the_sheet_keeps_it_and_a_tap_outside_closes_it() {        menu()        compose.onNodeWithText("Amber note 01").performClick()        compose.waitForIdle()        assertTrue("a tap on the sheet's title closed it", menuOpen)        compose.onNodeWithTag("act-sheet-scrim").performTouchInput { click(Offset(centerX, 20f)) }        compose.waitForIdle()        assertFalse("a tap above the sheet left it standing", menuOpen)    }    /** A note's More in landscape at the largest text: every act whole, and the last reached by scrolling the sheet. */    @Test @Config(qualifiers = "w731dp-h411dp-land-mdpi", fontScale = 2.0f)    fun the_last_act_is_reachable_in_landscape_at_the_largest_text() {        compose.setContent { Writing(theme, type) { ActSheet(acts, "Close", onAct = {}) { menuOpen = false } } }        compose.waitForIdle()        val bin = compose.onNodeWithTag("act-bin")        bin.performScrollTo()        bin.assertIsDisplayed()        val root = compose.onRoot().fetchSemanticsNode().boundsInRoot        val node = bin.fetchSemanticsNode()        val top = node.positionInRoot.y        assertTrue("Move to the bin stands on the screen: $top..${top + node.size.height} in ${root.bottom}", top >= 0f && top + node.size.height <= root.bottom + 1f)        assertTrue("Move to the bin is whole, not crushed: ${node.size.height}", node.size.height >= 48)    }    /** The core's ask for deleting for good: its act in red and its Cancel under it, which closes the ask and deletes nothing. */    @Test @Config(fontScale = 2.0f)    fun the_ask_has_its_cancel_to_press() {        var deleted = false        val ask = Asked("Delete for good?", "Delete", "\u201cAmber note 01\u201d will be gone from every device. This can\u2019t be undone.", "Cancel") { deleted = true }        compose.setContent { Writing(theme, type) { if (menuOpen) AskSheet(ask, "Close") { menuOpen = false } } }        compose.waitForIdle()        compose.onNodeWithTag("act-cancel").performScrollTo().assertIsDisplayed()        compose.onNodeWithText("Cancel").assertIsDisplayed()        compose.onNodeWithTag("act-cancel").performClick()        compose.waitForIdle()        assertFalse("Cancel closed the ask", menuOpen)        assertFalse("Cancel deleted nothing", deleted)    }}