Cotton code
59 lines · 3.1 KB · KotlinRaw
package com.cottondb.messages.warmimport android.app.Applicationimport android.view.ViewGroupimport androidx.activity.ComponentActivityimport androidx.compose.foundation.lazy.LazyListStateimport androidx.compose.ui.test.junit4.createAndroidComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onRootimport androidx.compose.ui.test.performScrollToKeyimport androidx.core.graphics.Insetsimport androidx.core.view.ViewCompatimport androidx.core.view.WindowCompatimport androidx.core.view.WindowInsetsCompatimport com.cottondb.messages.chat.ChatListimport com.cottondb.messages.chat.Wordsimport com.cottondb.shell.core.CottonCoreimport kotlinx.serialization.json.jsonObjectimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport org.robolectric.annotation.Config/** Searching Chats with the keyboard up: the last chat found scrolls to above the keyboard, never under it. Invented chats. */@RunWith(RobolectricTestRunner::class)@Config(sdk = [35], qualifiers = "w400dp-h860dp-mdpi", application = Application::class)class ChatsKeyboardTest {    @get:Rule val compose = createAndroidComposeRule<ComponentActivity>()    private fun row(i: Int) = """{"thread":"t$i","name":"Invented Example $i","group":false,"source":"whatsapp","picture":{"initials":"IE","tint":1},        "lastLine":{"text":"See you at seven","mine":false},"when":"Tue","unread":0,"markedUnread":false,"pinned":false,"muted":false,        "archived":false,"leading":[],"trailing":[]}"""    @Test    fun the_last_chat_found_stands_above_the_keyboard() {        val list = ChatList.of(CottonCore.json.parseToJsonElement("""{"words":{"title":"Chats","search":"Search"},"filters":[],            "rows":[${(1..40).joinToString(",") { row(it) }}]}""").jsonObject, Words { set, key -> "$set:$key" })        compose.runOnUiThread { WindowCompat.setDecorFitsSystemWindows(compose.activity.window, false) }        compose.setContent { WarmTheme(night = false) { ChatsPlace(list, LazyListState(), "Example", ChatsWords("New chat", "You", "Muted", "Pinned", "Unread", "More"), {}, {}, {}, { _, _ -> }, {}, {}, {}) } }        compose.waitForIdle()        val keyboard = 300        compose.runOnUiThread {            val view = compose.activity.findViewById<ViewGroup>(android.R.id.content).getChildAt(0)            ViewCompat.dispatchApplyWindowInsets(                view,                WindowInsetsCompat.Builder().setInsets(WindowInsetsCompat.Type.ime(), Insets.of(0, 0, 0, keyboard)).setVisible(WindowInsetsCompat.Type.ime(), true).build(),            )        }        compose.waitForIdle()        compose.onNodeWithTag("chat-list").performScrollToKey("t40")        compose.waitForIdle()        val screen = compose.onRoot().fetchSemanticsNode().size.height        val last = compose.onNodeWithTag("chat-row-t40").fetchSemanticsNode()        val foot = last.positionInRoot.y + last.size.height        assertTrue("the last chat's foot $foot stands above the keyboard at ${screen - keyboard}", foot <= screen - keyboard + 1f)    }}