package com.cottondb.messages.warm import android.app.Application import android.view.ViewGroup import androidx.activity.ComponentActivity import androidx.compose.foundation.lazy.LazyListState import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onRoot import androidx.compose.ui.test.performScrollToKey import androidx.core.graphics.Insets import androidx.core.view.ViewCompat import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsCompat import com.cottondb.messages.chat.ChatList import com.cottondb.messages.chat.Words import com.cottondb.shell.core.CottonCore import kotlinx.serialization.json.jsonObject 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 /** 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() 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(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) } }