Cotton code
226 lines · 10.5 KB · KotlinRaw
package com.cottondb.notesimport android.app.Applicationimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.fillMaxHeightimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.gestures.Orientationimport androidx.compose.foundation.gestures.draggableimport androidx.compose.foundation.gestures.rememberDraggableStateimport androidx.compose.material3.Textimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.setValueimport androidx.compose.ui.Modifierimport androidx.compose.ui.geometry.Offsetimport androidx.compose.ui.graphics.Colorimport androidx.compose.ui.platform.testTagimport androidx.compose.ui.semantics.SemanticsActionsimport androidx.compose.ui.test.assertIsDisplayedimport androidx.compose.ui.test.getUnclippedBoundsInRootimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithContentDescriptionimport androidx.compose.ui.test.onAllNodesWithContentDescriptionimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onRootimport androidx.compose.ui.test.performClickimport androidx.compose.ui.test.performSemanticsActionimport androidx.compose.ui.test.performTouchInputimport androidx.compose.ui.test.clickimport androidx.compose.ui.test.swipeimport androidx.compose.ui.text.font.FontFamilyimport androidx.compose.ui.unit.dpimport androidx.compose.ui.unit.heightimport androidx.compose.ui.unit.widthimport 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.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/** * The drawer pushes, it does not cover (the owner, 22 Nov 2026): Words' side page slid over the note list and dimmed * it. It now slides in from the left while the list moves right with it and stays in sight at the right edge, a fifth * of the width, barely dimmed; a tap or a drag on that edge closes it, and it follows the finger. The header gains the * side page's own mark at its left; the title with its arrow still opens the page. Invented notes, a 400dp phone. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h700dp-mdpi", application = Application::class)class NotesDrawerTest {    @get:Rule val compose = createComposeRule()    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 open by mutableStateOf(false)    private lateinit var drawer: SideDrawer    private var listComposed = 0    private var rowSwiped = 0f    /** The surface's arrangement: the list pushed by the drawer, its edge over it, the side page beside it. */    private fun show() {        compose.setContent {            Writing(theme, type) {                drawer = rememberSideDrawer()                DrawerRoom(drawer, open, enabled = true, onOpen = { open = it }) {                    Column(Modifier.fillMaxSize().pushedBy(drawer)) {                        listComposed++                        Text("Invented note about lanterns")                        // A row with its own swipe (Pin to the right, Bin to the left), as the list's rows have.                        Box(Modifier.fillMaxWidth().height(200.dp).testTag("row").draggable(rememberDraggableState { rowSwiped += it }, Orientation.Horizontal))                    }                    DrawerEdge(drawer, CLOSE, Color.Black.copy(alpha = EDGE_VEIL), theme.rule) { open = false }                    if (drawer.shown) Box(Modifier.fillMaxHeight().sideOf(drawer).testTag("side-page"))                }            }        }        compose.waitForIdle()    }    private fun listLeft() = compose.onNodeWithTag("note-list-room").getUnclippedBoundsInRoot().left.value    @Test fun the_list_stays_composed_and_in_sight_at_the_right_edge_when_open() {        show()        compose.runOnIdle { open = true }        compose.waitForIdle()        val side = compose.onNodeWithTag("side-page").getUnclippedBoundsInRoot()        assertEquals("the side page stands at the left", 0f, side.left.value, 0.5f)        assertEquals("four fifths of the width", 320f, side.width.value, 0.5f)        val list = compose.onNodeWithTag("note-list-room")        list.assertIsDisplayed()        assertEquals("the list moved right with the page, its left edge in sight", 320f, listLeft(), 0.5f)        assertEquals("the list keeps its own width (pushed, not squeezed)", 400f, list.getUnclippedBoundsInRoot().width.value, 0.5f)        val edge = compose.onNodeWithContentDescription(CLOSE)        edge.assertIsDisplayed()        assertEquals(320f, edge.getUnclippedBoundsInRoot().left.value, 0.5f)        assertTrue("the list was composed", listComposed > 0)        assertTrue("barely dimmed", EDGE_VEIL <= 0.1f)    }    @Test fun a_tap_on_the_edge_closes() {        open = true        show()        compose.onNodeWithContentDescription(CLOSE).performTouchInput { click(Offset(20f, centerY)) }        compose.waitForIdle()        assertFalse(open)        assertEquals(0f, listLeft(), 0.5f)        compose.onNodeWithTag("side-page").assertDoesNotExist()    }    @Test fun talkback_closes_by_the_edge() {        open = true        show()        compose.onNodeWithContentDescription(CLOSE).performSemanticsAction(SemanticsActions.OnClick)        compose.waitForIdle()        assertFalse(open)    }    @Test fun a_drag_on_the_edge_closes() {        open = true        show()        compose.onRoot().performTouchInput { swipe(Offset(360f, 300f), Offset(60f, 300f), 300) }        compose.waitForIdle()        assertFalse(open)        assertEquals(0f, listLeft(), 0.5f)    }    @Test fun it_follows_the_finger_from_the_left_edge_and_opens_when_let_go_past_half() {        show()        compose.onRoot().performTouchInput { down(Offset(10f, 300f)); moveBy(Offset(40f, 0f)); moveBy(Offset(80f, 0f)) }        compose.waitForIdle()        val held = listLeft()        assertTrue("held part way, the list stands where the finger is ($held)", held in 90f..125f)        assertFalse("not open until let go", open)        compose.onRoot().performTouchInput { moveBy(Offset(100f, 0f)); up() }        compose.waitForIdle()        assertTrue(open)        assertEquals(320f, listLeft(), 0.5f)    }    /** On the emulator a drag in from the left edge over a note was the note's Pin swipe, and the page never came. */    @Test fun a_drag_in_from_the_edge_over_a_row_opens_the_page_not_the_rows_swipe() {        show()        val row = compose.onNodeWithTag("row").getUnclippedBoundsInRoot()        val y = (row.top.value + row.bottom.value) / 2        compose.onRoot().performTouchInput { swipe(Offset(10f, y), Offset(300f, y), 300) }        compose.waitForIdle()        assertTrue(open)        assertEquals("the row did not swipe", 0f, rowSwiped, 0.01f)        open = false        compose.waitForIdle()        compose.onRoot().performTouchInput { swipe(Offset(200f, y), Offset(60f, y), 300) }        compose.waitForIdle()        assertFalse("further in, the row keeps its swipes", open)        assertTrue(rowSwiped < 0f)    }    /** On the emulator a note held just after the side mark's tap, before the page moved, opened its menu under the page. */    @Test fun the_list_is_covered_from_the_ask_not_from_the_first_move() {        show()        compose.mainClock.autoAdvance = false        compose.runOnIdle { open = true }        var frames = 0        while (compose.onAllNodesWithContentDescription(CLOSE).fetchSemanticsNodes().isEmpty() && frames < 5) {            compose.mainClock.advanceTimeByFrame()            compose.waitForIdle()            frames++        }        compose.onNodeWithContentDescription(CLOSE).assertExists()        assertEquals("the edge stands before the page has moved", 0f, drawer.offset.value, 0.01f)        compose.onNodeWithTag("row").assertDoesNotExist()        compose.mainClock.autoAdvance = true    }    @Test fun a_touch_while_it_moves_stops_it_under_the_finger() {        show()        compose.mainClock.autoAdvance = false        compose.runOnIdle { open = true }        repeat(7) { compose.mainClock.advanceTimeByFrame(); compose.waitForIdle() }        val moving = listLeft()        assertTrue("part way in ($moving)", moving in 1f..319f)        compose.onRoot().performTouchInput { down(Offset(moving + 40f, 300f)) }        compose.mainClock.advanceTimeBy(400)        assertEquals("held where it was caught", moving, listLeft(), 24f)        compose.onRoot().performTouchInput { moveBy(Offset(-150f, 0f)); moveBy(Offset(-100f, 0f)); up() }        compose.mainClock.autoAdvance = true        compose.waitForIdle()        assertFalse("dragged back, it closes", open)        assertEquals(0f, listLeft(), 0.5f)    }    @Test fun the_side_mark_opens_the_side_page() {        var opened = 0        compose.setContent { Writing(theme, type) { ListHead("Todo", OPEN, "More", onSide = { opened++ }) {} } }        val mark = compose.onNodeWithTag("list-side")        val bounds = mark.getUnclippedBoundsInRoot()        assertTrue("48 dp to touch (${bounds.width} × ${bounds.height})", bounds.width >= 48.dp && bounds.height >= 48.dp)        assertTrue("it stands at the left", bounds.left < 16.dp)        compose.onNodeWithContentDescription(OPEN).assertIsDisplayed()        mark.performClick()        assertEquals(1, opened)    }    @Test fun the_title_still_opens_the_side_page() {        var opened = 0        compose.setContent { Writing(theme, type) { ListHead("Todo", OPEN, "More", onSide = { opened++ }) {} } }        compose.onNodeWithContentDescription("$OPEN, Todo").assertIsDisplayed()        compose.onNodeWithTag("list-title", useUnmergedTree = true).assertIsDisplayed()        compose.onNodeWithTag("list-place").performClick()        assertEquals(1, opened)    }    private companion object {        const val CLOSE = "Close the side page"        const val OPEN = "Open the side page"    }}