package com.cottondb.notes import android.app.Application import androidx.activity.ComponentActivity import androidx.activity.compose.BackHandler import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize 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.test.assertIsDisplayed import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onRoot import androidx.compose.ui.test.performScrollTo import androidx.compose.ui.test.onNodeWithText import androidx.compose.ui.test.performClick import androidx.compose.ui.test.performTouchInput import androidx.compose.ui.test.swipeDown import androidx.compose.ui.test.click import androidx.compose.ui.geometry.Offset import androidx.compose.ui.text.font.FontFamily 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.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 /** * 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() 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) } }