package com.cottondb.story 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.foundation.layout.Column import androidx.compose.ui.platform.testTag import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.performTouchInput import androidx.compose.ui.test.swipeDown import androidx.compose.ui.test.swipeUp import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.performScrollTo import com.cottondb.story.ink.InkSheet 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 /** * 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() 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) } }