package com.cottondb.notes import android.app.Application import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.gestures.Orientation import androidx.compose.foundation.gestures.draggable import androidx.compose.foundation.gestures.rememberDraggableState import androidx.compose.material3.Text import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.testTag import androidx.compose.ui.semantics.SemanticsActions import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.getUnclippedBoundsInRoot import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onAllNodesWithContentDescription import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onRoot import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performSemanticsAction import androidx.compose.ui.test.performTouchInput import androidx.compose.ui.test.click import androidx.compose.ui.test.swipe import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.height import androidx.compose.ui.unit.width import com.cottondb.writing.theme.Writing import com.cottondb.writing.theme.WritingTheme import com.cottondb.writing.theme.WritingType import kotlinx.serialization.json.Json import kotlinx.serialization.json.jsonObject import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse 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 /** * 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" } }