119 lines · 5.3 KB · KotlinRaw
package com.cottondb.storyimport android.app.Applicationimport androidx.compose.foundation.layout.Boximport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.layout.offsetimport androidx.compose.foundation.lazy.LazyColumnimport androidx.compose.foundation.lazy.LazyListStateimport androidx.compose.foundation.lazy.itemsIndexedimport androidx.compose.material3.Textimport androidx.compose.runtime.rememberimport androidx.compose.ui.Modifierimport androidx.compose.ui.geometry.Offsetimport androidx.compose.ui.platform.testTagimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.performTouchInputimport androidx.compose.ui.test.swipeUpimport androidx.compose.ui.unit.IntOffsetimport androidx.compose.ui.unit.dpimport androidx.compose.ui.zIndeximport com.cottondb.shell.controlimport com.cottondb.story.ink.Homeimport com.cottondb.story.ink.SceneDragimport com.cottondb.story.ink.ScrollWhileHeldimport com.cottondb.story.ink.holdToMoveLatestimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertNotEqualsimport org.junit.Assert.assertNullimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Configimport 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<String, Int>? = 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) }}