package com.cottondb.story import android.app.Application import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.offset import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyListState import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.material3.Text import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.geometry.Offset import androidx.compose.ui.platform.testTag import androidx.compose.ui.test.junit4.createComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.performTouchInput import androidx.compose.ui.test.swipeUp import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.dp import androidx.compose.ui.zIndex import com.cottondb.shell.control import com.cottondb.story.ink.Home import com.cottondb.story.ink.SceneDrag import com.cottondb.story.ink.ScrollWhileHeld import com.cottondb.story.ink.holdToMoveLatest import org.junit.Assert.assertEquals import org.junit.Assert.assertNotEquals import org.junit.Assert.assertNull 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 import kotlin.math.roundToInt /** * The home's scenes, sixty invented, rowed as the home rows them (the row's control, then its hold to move): a swipe on * a row scrolls at once and lifts nothing; a held row lifts only after the hold and moves; held near the foot, the list * scrolls under it. (The iPhone's home stopped scrolling after hold-and-drag went in, 21 Nov 2026.) */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], qualifiers = "w400dp-h700dp-mdpi", application = Application::class) class StoryHomeScrollTest { @get:Rule val compose = createComposeRule() private lateinit var list: LazyListState private lateinit var drag: SceneDrag private var tapped = 0 private var moved: Pair? = null private fun home() { val scenes = (1..60).map { Home.SceneRow("s$it", it, "Scene $it", null, null) } compose.setContent { list = androidx.compose.foundation.lazy.rememberLazyListState() drag = remember { SceneDrag(list).also { it.order = scenes } } ScrollWhileHeld(drag) LazyColumn(Modifier.fillMaxSize().testTag("home"), state = list) { itemsIndexed(drag.order, key = { _, row -> row.id }) { _, row -> val held = drag.moving == row.id Box( (if (held) Modifier.zIndex(1f) else Modifier).offset { IntOffset(0, if (held) drag.offset.roundToInt() else 0) } .fillMaxWidth().height(64.dp).testTag("row-${row.id}") .control(row.title, onTap = { tapped++ }) .holdToMoveLatest(row.id, onLift = { drag.lift(row.id) }, onDrag = drag::by, onDrop = { moved = drag.drop() }, onStill = { drag.still() }), ) { Text(row.title) } } } } } @Test fun a_swipe_on_a_row_scrolls_at_once_and_lifts_nothing() { home() compose.onNodeWithTag("row-s3").performTouchInput { swipeUp(startY = bottom - 2f, endY = top - 400f, durationMillis = 200) } compose.waitForIdle() assertTrue("the list scrolled: ${list.firstVisibleItemIndex}", list.firstVisibleItemIndex >= 4) assertNull(drag.moving) assertNull(moved) assertEquals(0, tapped) } @Test fun a_row_lifts_only_after_a_hold_and_moves_where_it_is_let_go() { home() compose.mainClock.autoAdvance = false compose.onNodeWithTag("row-s2").performTouchInput { down(center) } compose.mainClock.advanceTimeBy(200) assertNull("no lift before the hold's time", drag.moving) compose.mainClock.advanceTimeBy(600) assertEquals("s2", drag.moving) compose.onNodeWithTag("row-s2").performTouchInput { repeat(10) { moveBy(Offset(0f, 20f)) } } compose.mainClock.advanceTimeBy(100) compose.onNodeWithTag("row-s2").performTouchInput { up() } compose.mainClock.advanceTimeBy(500) assertNull(drag.moving) val (id, to) = moved ?: error("no move") assertEquals("s2", id) assertEquals(5, to) assertEquals(0, list.firstVisibleItemIndex) } @Test fun a_row_held_near_the_foot_scrolls_the_list_under_it() { home() compose.mainClock.autoAdvance = false compose.onNodeWithTag("row-s2").performTouchInput { down(center) } compose.mainClock.advanceTimeBy(800) assertEquals("s2", drag.moving) // Down to the foot of a 700 dp screen, and held there. compose.onNodeWithTag("home").performTouchInput { repeat(30) { moveBy(Offset(0f, 20f)) } } compose.mainClock.advanceTimeBy(1500) assertNotEquals(0, list.firstVisibleItemIndex) compose.onNodeWithTag("home").performTouchInput { up() } compose.mainClock.advanceTimeBy(500) val (_, to) = moved ?: error("no move") assertTrue("carried past the first screen: $to", to > 11) } }