Resync keyboard insets after a back-gesture dismissal (#177)

This commit is contained in:
makiolaj committed 2026-11-25 12:09:15 +01:00
1 parent 8cdf3ad709
commit 61324a0072
4 files changed
+87 -1

No files matched your search

@@ -0,0 +1,79 @@
package de.jeanlucmakiola.agendula.ui
import android.graphics.Insets
import android.view.View
import android.view.WindowInsets
import android.view.WindowInsetsAnimation
import androidx.compose.foundation.layout.WindowInsets as ComposeInsets
import androidx.compose.foundation.layout.ime
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.test.junit4.v2.createComposeRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SdkSuppress
import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.floret.components.ImeInsetsResync
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
/**
* A keyboard hidden by a predictive back gesture runs an insets animation without a
* duration; Compose then keeps the last keyboard height it saw (#177).
*/
@RunWith(AndroidJUnit4::class)
@SdkSuppress(minSdkVersion = 30)
class ImeInsetsResyncTest {
@get:Rule
val rule = createComposeRule()
private var imeBottom = -1
private lateinit var view: View
@Test
fun keyboardInsetsSettleAfterUserDrivenAnimation() {
setContent(resync = true)
runUserDrivenKeyboardAnimation()
rule.waitUntil(TIMEOUT_MS) { imeBottom == 0 }
assertThat(imeBottom).isEqualTo(0)
}
/** Shows the Compose behaviour [ImeInsetsResync] works around; drop both once this fails. */
@Test
fun withoutResyncKeyboardInsetsStayStale() {
setContent(resync = false)
runUserDrivenKeyboardAnimation()
rule.waitForIdle()
assertThat(imeBottom).isEqualTo(KEYBOARD_PX)
}
private fun setContent(resync: Boolean) {
rule.setContent {
if (resync) ImeInsetsResync()
view = LocalView.current
imeBottom = ComposeInsets.ime.getBottom(LocalDensity.current)
}
rule.waitForIdle()
}
private fun runUserDrivenKeyboardAnimation() {
rule.runOnUiThread {
val root = view.rootView
val ime = WindowInsets.Type.ime()
val animation = WindowInsetsAnimation(ime, null, -1)
val keyboard = Insets.of(0, 0, 0, KEYBOARD_PX)
val midway = WindowInsets.Builder().setInsets(ime, keyboard).setVisible(ime, true).build()
root.dispatchWindowInsetsAnimationPrepare(animation)
root.dispatchWindowInsetsAnimationStart(animation, WindowInsetsAnimation.Bounds(Insets.NONE, keyboard))
root.dispatchWindowInsetsAnimationProgress(midway, listOf(animation))
root.dispatchWindowInsetsAnimationEnd(animation)
}
rule.waitForIdle()
}
private companion object {
const val KEYBOARD_PX = 500
const val TIMEOUT_MS = 2_000L
}
}