90 lines · 4.2 KB · KotlinRaw
package com.cottondb.notesimport android.app.Applicationimport android.view.ViewGroupimport androidx.activity.ComponentActivityimport androidx.compose.foundation.layout.Columnimport androidx.compose.foundation.layout.fillMaxSizeimport androidx.compose.foundation.layout.fillMaxWidthimport androidx.compose.foundation.layout.heightimport androidx.compose.foundation.lazy.LazyColumnimport androidx.compose.foundation.lazy.itemsimport androidx.compose.material3.Textimport androidx.compose.ui.Modifierimport androidx.compose.ui.platform.testTagimport androidx.compose.ui.test.junit4.createAndroidComposeRuleimport androidx.compose.ui.test.onNodeWithTagimport androidx.compose.ui.test.onRootimport androidx.compose.ui.test.performScrollToIndeximport androidx.compose.ui.unit.dpimport androidx.core.graphics.Insetsimport androidx.core.view.ViewCompatimport androidx.core.view.WindowCompatimport androidx.core.view.WindowInsetsCompatimport org.junit.Assert.assertTrueimport org.junit.Ruleimport org.junit.Testimport org.junit.runner.RunWithimport org.robolectric.RobolectricTestRunnerimport 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<ComponentActivity>() @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<ViewGroup>(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<ViewGroup>(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) }}