Cotton code
70 lines · 2.7 KB · KotlinRaw
package com.cottondb.notesimport android.app.Applicationimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.lazy.LazyColumnimport androidx.compose.foundation.lazy.LazyListStateimport androidx.compose.foundation.lazy.itemsimport androidx.compose.material3.Textimport androidx.compose.runtime.getValueimport androidx.compose.runtime.mutableStateOfimport androidx.compose.runtime.setValueimport androidx.compose.ui.Modifierimport androidx.compose.ui.test.junit4.createComposeRuleimport androidx.compose.ui.unit.dpimport org.junit.Assert.assertEqualsimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** * The list's top, with seventy invented notes: a note pinned from the top of the list comes to the top and is seen * there (on the emulator it went above the list's top edge and the list stood one row down); a list scrolled down * keeps its place when a note is pinned. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h700dp-mdpi", application = Application::class)class NotesListTopTest {    @get:Rule val compose = createComposeRule()    private var rows by mutableStateOf((1..70).map { "Invented note $it" })    private lateinit var list: LazyListState    private fun show() {        compose.setContent {            list = androidx.compose.foundation.lazy.rememberLazyListState()            KeepTop(list, rows)            LazyColumn(Modifier.fillMaxSize(), state = list) {                items(rows, key = { it }) { Text(it, Modifier.fillMaxWidth().height(80.dp)) }            }        }        compose.waitForIdle()    }    /** [title] pinned: it moves to the top of the rows. */    private fun pin(title: String) {        compose.runOnIdle { rows = listOf(title) + (rows - title) }        compose.waitForIdle()    }    @Test fun a_note_pinned_at_the_top_of_the_list_is_seen_at_the_top() {        show()        pin("Invented note 3")        compose.runOnIdle {            assertEquals(0, list.firstVisibleItemIndex)            assertEquals("Invented note 3", list.layoutInfo.visibleItemsInfo.first().key)        }    }    @Test fun a_list_scrolled_down_keeps_its_place_when_a_note_is_pinned() {        show()        compose.runOnIdle { kotlinx.coroutines.runBlocking { list.scrollToItem(20) } }        compose.waitForIdle()        pin("Invented note 25")        compose.runOnIdle { assertEquals("Invented note 21", list.layoutInfo.visibleItemsInfo.first().key) }    }}