package com.cottondb.notes import android.app.Application import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.LazyListState import androidx.compose.foundation.lazy.items 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.junit4.createComposeRule import androidx.compose.ui.unit.dp import org.junit.Assert.assertEquals import org.junit.Rule import org.junit.Test import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import 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) } } }