package com.cottondb.notes import android.app.Application import android.view.ViewGroup import androidx.activity.ComponentActivity import androidx.compose.foundation.layout.Column 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.items import androidx.compose.material3.Text import androidx.compose.ui.Modifier import androidx.compose.ui.platform.testTag import androidx.compose.ui.test.junit4.createAndroidComposeRule import androidx.compose.ui.test.onNodeWithTag import androidx.compose.ui.test.onRoot import androidx.compose.ui.test.performScrollToIndex import androidx.compose.ui.unit.dp import androidx.core.graphics.Insets import androidx.core.view.ViewCompat import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsCompat 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 /** * The list searched with the keyboard up, seventy invented notes found: the last can be scrolled into view above the * keyboard. (On the emulator the last results stayed behind it.) */ @RunWith(RobolectricTestRunner::class) @Config(sdk = [35], qualifiers = "w400dp-h700dp-mdpi", application = Application::class) class NotesListKeyboardTest { @get:Rule val compose = createAndroidComposeRule() @Test fun the_last_note_found_stands_above_the_keyboard() { compose.runOnUiThread { WindowCompat.setDecorFitsSystemWindows(compose.activity.window, false) } compose.setContent { Column(Modifier.fillMaxSize().listRoom()) { LazyColumn(Modifier.weight(1f).fillMaxWidth().testTag("note-list")) { items((1..70).map { "Invented note $it" }, key = { it }) { Text(it, Modifier.fillMaxWidth().height(80.dp).testTag(it)) } } } } compose.waitForIdle() val keyboard = 300 compose.runOnUiThread { val view = (compose.activity.findViewById(android.R.id.content)).getChildAt(0) val insets = WindowInsetsCompat.Builder() .setInsets(WindowInsetsCompat.Type.ime(), Insets.of(0, 0, 0, keyboard)) .setVisible(WindowInsetsCompat.Type.ime(), true) .build() ViewCompat.dispatchApplyWindowInsets(view, insets) } compose.waitForIdle() compose.onNodeWithTag("note-list").performScrollToIndex(69) compose.waitForIdle() val screen = compose.onRoot().fetchSemanticsNode().size.height val last = compose.onNodeWithTag("Invented note 70").fetchSemanticsNode() val foot = last.positionInRoot.y + last.size.height assertTrue("the last note's foot $foot stands above the keyboard at ${screen - keyboard}", foot <= screen - keyboard + 1f) } /** Turned sideways, the camera's cutout at the left edge: the list's rows start clear of it. */ @Test @Config(qualifiers = "w731dp-h411dp-land-mdpi") fun the_list_stands_clear_of_the_cutout_sideways() { compose.runOnUiThread { WindowCompat.setDecorFitsSystemWindows(compose.activity.window, false) } compose.setContent { Column(Modifier.fillMaxSize().listRoom()) { LazyColumn(Modifier.weight(1f).fillMaxWidth()) { items(listOf("Invented note 1"), key = { it }) { Text(it, Modifier.testTag(it)) } } } } compose.waitForIdle() val cutout = 52 compose.runOnUiThread { val view = (compose.activity.findViewById(android.R.id.content)).getChildAt(0) val insets = WindowInsetsCompat.Builder() .setInsets(WindowInsetsCompat.Type.displayCutout(), Insets.of(cutout, 0, 0, 0)) .build() ViewCompat.dispatchApplyWindowInsets(view, insets) } compose.waitForIdle() val start = compose.onNodeWithTag("Invented note 1").fetchSemanticsNode().positionInRoot.x assertTrue("the row starts at $start, under the cutout's $cutout", start >= cutout) } }