diff --git a/android/src/main/java/me/sectiontn/codepad/view/CodeEditorView.kt b/android/src/main/java/me/sectiontn/codepad/view/CodeEditorView.kt index cdffd96..19d6d8f 100644 --- a/android/src/main/java/me/sectiontn/codepad/view/CodeEditorView.kt +++ b/android/src/main/java/me/sectiontn/codepad/view/CodeEditorView.kt @@ -18,6 +18,7 @@ import android.view.KeyEvent import android.view.MotionEvent import android.view.VelocityTracker import android.view.View +import android.view.ViewTreeObserver import android.view.ViewConfiguration import android.view.inputmethod.EditorInfo import android.view.inputmethod.InputConnection @@ -227,6 +228,27 @@ class CodeEditorView(context: Context) : View(context) { // Keyboard avoidance: the tracker turns the window's IME overlap into a second bottom inset in px. private var avoidsKeyboard = false private var keyboardInset = 0 + private var keyboardTop = Int.MAX_VALUE + private val windowSpot = IntArray(2) + // Page mode under avoidKeyboard: a reveal due before the next draw, and the scroller height the layout watch saw. + private var pageRevealPending = false + private var watchingPage = false + private var pageHeight = -1 + private val onPageLayout = ViewTreeObserver.OnGlobalLayoutListener { + val h = pageScroller()?.height ?: -1 + if (h != pageHeight) { + pageHeight = h + revealInPage() + } + } + // Before the draw, so the page moves in the frame that shows the caret's new row. + private val onPagePreDraw = ViewTreeObserver.OnPreDrawListener { + if (revealPending || pageRevealPending) { + pageRevealPending = false + revealInPage() + } + true + } private var keyboardOffset = 0 private var keyboardTracker: KeyboardInsetTracker? = null private var contentInsetTop = 0f @@ -360,7 +382,7 @@ class CodeEditorView(context: Context) : View(context) { } fun focus() { - requestFocus() + focusKeepingPage() showKeyboard() } @@ -408,17 +430,87 @@ class CodeEditorView(context: Context) : View(context) { if (isFocused) revealPending = true } - /** Pixels of the view under the keyboard, from KeyboardInsetTracker; applied like a bottom inset. */ - fun applyKeyboardInset(px: Int) { - if (px == keyboardInset) return - keyboardInset = px + /** The keyboard's top in window pixels from KeyboardInsetTracker, Int.MAX_VALUE with none. Tests inject one here. */ + fun applyKeyboardTop(px: Int) { + keyboardTop = px + updateKeyboardInset() + revealInPage() + } + + // A scrolling editor takes its overlap with the keyboard's top raised by keyboardOffset as a bottom inset. + private fun updateKeyboardInset() { + var inset = 0 + if (isScrollEnabled && keyboardTop != Int.MAX_VALUE) { + getLocationInWindow(windowSpot) + inset = KeyboardInset.overlap(windowSpot[1] + height, keyboardTop - keyboardOffset) + } + if (inset == keyboardInset) return + keyboardInset = inset applyInsets() } + /** + * Page mode under avoidKeyboard: scrolls the nearest vertical scroller the least that shows the caret row inside it + * and above the keyboard's top raised by keyboardOffset. + */ + private fun revealInPage() { + if (isScrollEnabled || !avoidsKeyboard || !isFocused) return + val page = pageScroller() ?: return + // From the layout, as revealCaret reads it, so a reveal before the draw sees the caret's new row. + val m = rowMetrics() + val head = core.selection.head + val line = core.document.lineAt(head) + val entry = cache[line] ?: layoutLine(line) + getLocationInWindow(windowSpot) + val rowTop = windowSpot[1] + viewport.lineTop(line) + entry.layout.rowOf(head - core.document.lineStart(line)) * m.rowHeight + page.getLocationInWindow(windowSpot) + // The scroller clamps its own scrollBy, so a direction it can move in is open-ended here. + val delta = CoreNative.pageRevealDelta( + rowTop, rowTop + m.rowHeight, windowSpot[1], windowSpot[1] + page.height, keyboardTop, keyboardOffset, + if (page.canScrollVertically(-1)) Int.MAX_VALUE else 0, if (page.canScrollVertically(1)) Int.MAX_VALUE else 0, + ) + if (delta != 0) page.scrollBy(0, delta) + } + + // The nearest parent that scrolls vertically: a horizontal scroller in between is skipped. + private fun pageScroller(): View? { + var p = parent as? View + while (p != null && !(p.canScrollVertically(1) || p.canScrollVertically(-1))) p = p.parent as? View + return p + } + + // While a page-mode editor under avoidKeyboard has the focus, any layout (a scroller shrinking with an avoiding + // view, the editor growing a line) shows the caret again. + private fun watchPage() { + val on = isFocused && isAttachedToWindow && !isScrollEnabled && avoidsKeyboard + if (on == watchingPage) return + watchingPage = on + val observer = viewTreeObserver + if (on) { + pageHeight = pageScroller()?.height ?: -1 + observer.addOnGlobalLayoutListener(onPageLayout) + observer.addOnPreDrawListener(onPagePreDraw) + } else { + pageRevealPending = false + observer.removeOnGlobalLayoutListener(onPageLayout) + observer.removeOnPreDrawListener(onPagePreDraw) + } + } + + // A ScrollView scrolls a focused child's whole rect into view before onFocusChanged, which moves the page under a + // tall page-mode editor before the keyboard opens. The page reveal shows the caret instead. + private fun focusKeepingPage() { + val page = if (isFocused || isScrollEnabled || !avoidsKeyboard) null else pageScroller() + val y = page?.scrollY ?: 0 + requestFocus() + if (page != null && page.scrollY != y) page.scrollTo(page.scrollX, y) + } + /** Follows the window's IME insets while attached; off by default. */ fun setAvoidKeyboard(on: Boolean) { avoidsKeyboard = on if (on && isAttachedToWindow) startKeyboardTracker() else stopKeyboardTracker() + watchPage() } val isAvoidingKeyboard: Boolean get() = keyboardTracker != null @@ -428,7 +520,8 @@ class CodeEditorView(context: Context) : View(context) { val px = dp(value.coerceAtLeast(0f)) if (px == keyboardOffset) return keyboardOffset = px - applyInsets() + updateKeyboardInset() + revealInPage() } private fun applyInsets() { @@ -442,7 +535,7 @@ class CodeEditorView(context: Context) : View(context) { private fun startKeyboardTracker() { if (keyboardTracker != null) return - keyboardTracker = KeyboardInsetTracker(this, ::applyKeyboardInset).also { it.start() } + keyboardTracker = KeyboardInsetTracker(this, ::applyKeyboardTop).also { it.start() } } private fun stopKeyboardTracker() { @@ -454,6 +547,12 @@ class CodeEditorView(context: Context) : View(context) { if (enabled == isScrollEnabled) return isScrollEnabled = enabled scroller.abort() + updateKeyboardInset() + watchPage() + if (watchingPage) { + pageRevealPending = true + invalidate() + } } fun scrollToLine(line: Int, animated: Boolean) { @@ -468,8 +567,16 @@ class CodeEditorView(context: Context) : View(context) { private fun lineTop(line: Int): Int = viewport.rowStart(line.coerceIn(0, core.document.lineCount - 1)) * viewport.rowHeight - /** Scrolls the least amount that shows the caret with the text padding around it. */ + /** Scrolls the least amount that shows the caret with the text padding around it, the page too in page mode. */ fun revealSelection() { + revealCaret() + if (watchingPage) { + pageRevealPending = true + invalidate() + } + } + + private fun revealCaret() { val m = rowMetrics() val head = core.selection.head val line = core.document.lineAt(head) @@ -976,14 +1083,14 @@ class CodeEditorView(context: Context) : View(context) { override fun placeCaret(offset: Int) { menu.hide() - requestFocus() + focusKeepingPage() showKeyboard() core.setSelection(offset, offset) showInsertionHandle() } override fun selectWord(offset: Int) { - requestFocus() + focusKeepingPage() showKeyboard() core.selectWordAt(offset) wordAnchor = core.selection.range @@ -1205,6 +1312,7 @@ class CodeEditorView(context: Context) : View(context) { override fun onFocusChanged(gainFocus: Boolean, direction: Int, previouslyFocusedRect: Rect?) { super.onFocusChanged(gainFocus, direction, previouslyFocusedRect) focused = gainFocus + watchPage() if (gainFocus) { restartBlink() } else { @@ -1220,12 +1328,18 @@ class CodeEditorView(context: Context) : View(context) { if (dropped) return engine() if (avoidsKeyboard) startKeyboardTracker() + watchPage() if (autoFocusPending) runAutoFocus() } override fun onDetachedFromWindow() { super.onDetachedFromWindow() stopKeyboardTracker() + if (watchingPage) { + watchingPage = false + viewTreeObserver.removeOnGlobalLayoutListener(onPageLayout) + viewTreeObserver.removeOnPreDrawListener(onPagePreDraw) + } removeCallbacks(longPressRunnable) removeCallbacks(blink) removeCallbacks(hideInsertion) @@ -1281,7 +1395,7 @@ class CodeEditorView(context: Context) : View(context) { viewport.beginDraw(checksCalls = debugSwitch()) if (revealPending) { revealPending = false - revealSelection() + revealCaret() } // One core call for the document. Until publish returns, every document read comes from it. var f = readFrame() diff --git a/android/src/main/java/me/sectiontn/codepad/view/KeyboardInset.kt b/android/src/main/java/me/sectiontn/codepad/view/KeyboardInset.kt index 6fb95ed..5de2348 100644 --- a/android/src/main/java/me/sectiontn/codepad/view/KeyboardInset.kt +++ b/android/src/main/java/me/sectiontn/codepad/view/KeyboardInset.kt @@ -4,9 +4,12 @@ import android.view.WindowManager /** The arithmetic behind KeyboardInsetTracker, kept pure so JUnit covers it. */ internal object KeyboardInset { - /** Pixels of the view covered by the keyboard, both measured in window coordinates. */ - fun overlap(viewBottomInWindow: Int, windowHeight: Int, imeBottom: Int): Int = - maxOf(0, viewBottomInWindow - (windowHeight - imeBottom)) + /** The keyboard's top in window pixels, Int.MAX_VALUE with no keyboard up. */ + fun keyboardTop(windowHeight: Int, imeBottom: Int): Int = if (imeBottom <= 0) Int.MAX_VALUE else windowHeight - imeBottom + + /** Pixels of the view under a keyboard whose top is at keyboardTop, both in window coordinates. */ + fun overlap(viewBottomInWindow: Int, keyboardTop: Int): Int = + if (keyboardTop == Int.MAX_VALUE) 0 else maxOf(0, viewBottomInWindow - keyboardTop) /** adjustPan moves the whole window instead, so an inset on top of it would double the avoidance. */ fun applies(softInputMode: Int): Boolean = diff --git a/android/src/main/java/me/sectiontn/codepad/view/KeyboardInsetTracker.kt b/android/src/main/java/me/sectiontn/codepad/view/KeyboardInsetTracker.kt index 70d0918..f376a55 100644 --- a/android/src/main/java/me/sectiontn/codepad/view/KeyboardInsetTracker.kt +++ b/android/src/main/java/me/sectiontn/codepad/view/KeyboardInsetTracker.kt @@ -8,18 +8,24 @@ import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsAnimationCompat import androidx.core.view.WindowInsetsCompat -/** Listens to the window's IME insets on behalf of one view and reports its overlap with the keyboard in pixels. */ -internal class KeyboardInsetTracker(private val view: View, private val apply: (Int) -> Unit) { - private val location = IntArray(2) +/** Listens to the window's IME insets on behalf of one view and reports the keyboard's top in window pixels. */ +internal class KeyboardInsetTracker(private val view: View, private val apply: (keyboardTop: Int) -> Unit) { + // The window applies the end state before an IME animation's first frame. Skipping it lets a page ride the frames. + private var animating = false // Progress frames arrive on every view with a callback; androidx emulates them below API 30. private val callback = object : WindowInsetsAnimationCompat.Callback(DISPATCH_MODE_CONTINUE_ON_SUBTREE) { + override fun onPrepare(animation: WindowInsetsAnimationCompat) { + if (animation.typeMask and WindowInsetsCompat.Type.ime() != 0) animating = true + } + override fun onProgress(insets: WindowInsetsCompat, running: List): WindowInsetsCompat { if (running.any { it.typeMask and WindowInsetsCompat.Type.ime() != 0 }) update(insets) return insets } override fun onEnd(animation: WindowInsetsAnimationCompat) { + if (animation.typeMask and WindowInsetsCompat.Type.ime() != 0) animating = false ViewCompat.getRootWindowInsets(view)?.let(::update) } } @@ -27,7 +33,7 @@ internal class KeyboardInsetTracker(private val view: View, private val apply: ( fun start() { ViewCompat.setWindowInsetsAnimationCallback(view, callback) ViewCompat.setOnApplyWindowInsetsListener(view) { _, insets -> - update(insets) + if (!animating) update(insets) insets } ViewCompat.getRootWindowInsets(view)?.let(::update) @@ -36,19 +42,18 @@ internal class KeyboardInsetTracker(private val view: View, private val apply: ( fun stop() { ViewCompat.setWindowInsetsAnimationCallback(view, null) ViewCompat.setOnApplyWindowInsetsListener(view, null) - apply(0) + apply(Int.MAX_VALUE) } private fun update(insets: WindowInsetsCompat) { val mode = activity(view.context)?.window?.attributes?.softInputMode ?: 0 if (!KeyboardInset.applies(mode)) { - apply(0) + apply(Int.MAX_VALUE) return } val root = view.rootView ?: return - view.getLocationInWindow(location) val ime = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom - apply(KeyboardInset.overlap(location[1] + view.height, root.height, ime)) + apply(KeyboardInset.keyboardTop(root.height, ime)) } private fun activity(context: Context): Activity? { diff --git a/android/src/test/java/me/sectiontn/codepad/view/KeyboardInsetTest.kt b/android/src/test/java/me/sectiontn/codepad/view/KeyboardInsetTest.kt index 99b14b9..1d3fd68 100644 --- a/android/src/test/java/me/sectiontn/codepad/view/KeyboardInsetTest.kt +++ b/android/src/test/java/me/sectiontn/codepad/view/KeyboardInsetTest.kt @@ -8,11 +8,17 @@ import org.junit.Test class KeyboardInsetTest { @Test - fun overlapIsTheViewBandUnderTheKeyboard() { - assertEquals(0, KeyboardInset.overlap(viewBottomInWindow = 1000, windowHeight = 2000, imeBottom = 0)) - assertEquals(0, KeyboardInset.overlap(1000, 2000, 900)) - assertEquals(300, KeyboardInset.overlap(1500, 2000, 800)) - assertEquals(800, KeyboardInset.overlap(2000, 2000, 800)) + fun theKeyboardTopIsTheWindowLessTheImeAndOutOfReachWithNone() { + assertEquals(Int.MAX_VALUE, KeyboardInset.keyboardTop(windowHeight = 2000, imeBottom = 0)) + assertEquals(1200, KeyboardInset.keyboardTop(2000, 800)) + } + + @Test + fun overlapIsTheViewBandUnderTheKeyboardTop() { + assertEquals(0, KeyboardInset.overlap(viewBottomInWindow = 1000, keyboardTop = Int.MAX_VALUE)) + assertEquals(0, KeyboardInset.overlap(1000, 1100)) + assertEquals(300, KeyboardInset.overlap(1500, 1200)) + assertEquals(800, KeyboardInset.overlap(2000, 1200)) } @Test diff --git a/example/android/app/src/androidTest/java/codepad/example/AvoidKeyboardTest.kt b/example/android/app/src/androidTest/java/codepad/example/AvoidKeyboardTest.kt index 5af5c66..189840b 100644 --- a/example/android/app/src/androidTest/java/codepad/example/AvoidKeyboardTest.kt +++ b/example/android/app/src/androidTest/java/codepad/example/AvoidKeyboardTest.kt @@ -38,19 +38,23 @@ class AvoidKeyboardTest { val sizes = ArrayList() editor.onContentSizeListener = { _, h -> sizes.add(h) } editor.setAvoidKeyboard(true) - editor.setInitialText(List(50) { "line $it" }.joinToString("\n")) + val text = List(50) { "line $it" }.joinToString("\n") + editor.setInitialText(text) + // The window may give the editor focus, and then an inset change reveals the caret: keep it on line 49. + editor.core.setSelection(text.length, text.length) layout(editor) draw(editor) editor.scrollToLine(49, false) draw(editor) val before = editor.frame.scrollY val density = editor.resources.displayMetrics.density - editor.applyKeyboardInset(300) + val bottom = IntArray(2).also { editor.getLocationInWindow(it) }[1] + editor.height + editor.applyKeyboardTop(bottom - 300) editor.scrollToLine(49, false) draw(editor) assertEquals(300 / density, editor.frame.scrollY - before, 1f) assertEquals("content size excludes the keyboard inset", 1, sizes.size) - editor.applyKeyboardInset(0) + editor.applyKeyboardTop(Int.MAX_VALUE) editor.scrollToLine(49, false) draw(editor) assertEquals(before, editor.frame.scrollY, 1f) diff --git a/example/android/app/src/androidTest/java/codepad/example/PageKeyboardTest.kt b/example/android/app/src/androidTest/java/codepad/example/PageKeyboardTest.kt new file mode 100644 index 0000000..21f7399 --- /dev/null +++ b/example/android/app/src/androidTest/java/codepad/example/PageKeyboardTest.kt @@ -0,0 +1,235 @@ +package codepad.example + +import android.os.SystemClock +import android.view.View +import android.view.ViewGroup +import android.widget.FrameLayout +import android.widget.HorizontalScrollView +import android.widget.LinearLayout +import android.widget.ScrollView +import androidx.test.core.app.ActivityScenario +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.core.view.ViewCompat +import androidx.core.view.WindowInsetsCompat +import androidx.test.platform.app.InstrumentationRegistry +import me.sectiontn.codepad.view.CodeEditorView +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith + +/** A page-mode editor under avoidKeyboard in a ScrollView, with keyboard tops injected through the tracker's path. */ +@RunWith(AndroidJUnit4::class) +class PageKeyboardTest { + private val text = List(60) { "line $it" }.joinToString("\n") + + // The editor 600 px wide and 3200 px tall between spacers of above and below px, in a ScrollView. + private fun page(s: ActivityScenario, above: Int = 0, sideways: Boolean = false, below: Int = 4000): ScrollView { + var page: ScrollView? = null + s.onActivity { a -> + val editor = a.editor + (editor.parent as ViewGroup).removeView(editor) + val column = LinearLayout(a).apply { orientation = LinearLayout.VERTICAL } + if (above > 0) column.addView(View(a), LinearLayout.LayoutParams(600, above)) + if (sideways) { + column.addView(HorizontalScrollView(a).apply { + isFillViewport = true + addView(editor, FrameLayout.LayoutParams(600, 3200)) + }) + } else { + column.addView(editor, LinearLayout.LayoutParams(600, 3200)) + } + if (below > 0) column.addView(View(a), LinearLayout.LayoutParams(600, below)) + page = ScrollView(a).apply { addView(column) } + a.setContentView(page) + editor.setScrollEnabled(false) + editor.setAvoidKeyboard(true) + editor.setInitialText(text) + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + return page!! + } + + private fun imeShown(view: View): Boolean = + ViewCompat.getRootWindowInsets(view)?.isVisible(WindowInsetsCompat.Type.ime()) == true + + private fun windowTop(view: View): Int = IntArray(2).also { view.getLocationInWindow(it) }[1] + + private fun density(editor: CodeEditorView): Float = editor.resources.displayMetrics.density + + // The caret row's window top and bottom in px, from the last published frame. + private fun caretRow(editor: CodeEditorView): Pair { + val caret = editor.frame.caret ?: error("no caret in the frame") + val top = windowTop(editor) + return top + caret.top * density(editor) to top + caret.bottom * density(editor) + } + + // Puts the caret on the line whose row ends about 250 px above the page's bottom, and draws. + private fun caretLow(page: ScrollView, editor: CodeEditorView) { + editor.core.setSelection(0, 0) + draw(editor) + val (rowTop, rowBottom) = caretRow(editor) + val line = ((windowTop(page) + page.height - 250 - rowTop) / (rowBottom - rowTop)).toInt() + editor.core.setSelection(text.indexOf("line $line\n"), text.indexOf("line $line\n")) + draw(editor) + } + + @Test + fun aKeyboardOverTheCaretScrollsThePageUntilTheRowClearsIt() { + launch().use { s -> + val page = page(s) + val editor = s.editor() + onMain { + editor.requestFocus() + caretLow(page, editor) + val (rowTop, rowBottom) = caretRow(editor) + val keyboard = (rowBottom - 150).toInt() + editor.applyKeyboardTop(keyboard) + val (_, after) = caretRow(editor) + assertTrue("row ends at $after over a keyboard at $keyboard", after <= keyboard && after > keyboard - (rowBottom - rowTop)) + assertTrue("the page scrolled", page.scrollY > 0) + } + } + } + + @Test + fun pageModeLeavesTheKeyboardOutOfTheEditorsInset() { + launch().use { s -> + val page = page(s) + val editor = s.editor() + onMain { + editor.requestFocus() + caretLow(page, editor) + editor.applyKeyboardTop(caretRow(editor).second.toInt() - 150) + draw(editor) + assertEquals(0f, editor.frame.scrollY, 0f) + assertEquals(0, viewportOf(editor).insetBottom) + } + } + } + + @Test + fun keyboardOffsetKeepsTheRowThatFarAboveTheKeyboard() { + launch().use { s -> + val page = page(s) + val editor = s.editor() + onMain { + editor.requestFocus() + editor.setKeyboardOffset(52f) + caretLow(page, editor) + val keyboard = caretRow(editor).second.toInt() - 150 + editor.applyKeyboardTop(keyboard) + val raised = keyboard - Math.round(52 * density(editor)) + val (_, after) = caretRow(editor) + assertTrue("row ends at $after over a raised line at $raised", after <= raised) + } + } + } + + @Test + fun anUnfocusedEditorLeavesThePageAlone() { + launch().use { s -> + val page = page(s) + val editor = s.editor() + onMain { + caretLow(page, editor) + val before = page.scrollY + editor.applyKeyboardTop(caretRow(editor).second.toInt() - 150) + assertEquals(before, page.scrollY) + } + } + } + + @Test + fun aHorizontalScrollerBetweenIsSkipped() { + launch().use { s -> + val page = page(s, sideways = true) + val editor = s.editor() + onMain { + editor.requestFocus() + caretLow(page, editor) + val keyboard = caretRow(editor).second.toInt() - 150 + editor.applyKeyboardTop(keyboard) + assertTrue("the page scrolled", page.scrollY > 0) + assertTrue(caretRow(editor).second <= keyboard) + } + } + } + + @Test + fun keyboardOffsetCoversAScrollingEditorThatEndsJustAboveTheKeyboard() { + launch().use { s -> + val editor = s.editor() + onMain { + editor.setAvoidKeyboard(true) + editor.setInitialText(text) + editor.setKeyboardOffset(52f) + editor.requestFocus() + draw(editor) + val before = viewportOf(editor).insetBottom + editor.applyKeyboardTop(windowTop(editor) + editor.height + 20) + assertEquals(Math.round(52 * density(editor)) - 20, viewportOf(editor).insetBottom - before) + } + } + } + + @Test + fun theEditorsOwnFocusLeavesThePageWhereItWas() { + launch().use { s -> + // A ScrollView scrolls a focused child taller than itself to its top, here 300 px. + val page = page(s, above = 300) + val editor = s.editor() + onMain { + assertEquals(0, page.scrollY) + editor.focus() + val after = page.scrollY + editor.blur() + assertEquals(0, after) + } + // The keyboard focus() opened is closed before the next test reads the window's insets. + val end = SystemClock.uptimeMillis() + 3000 + while (SystemClock.uptimeMillis() < end && onMain { imeShown(editor) }) Thread.sleep(50) + } + } + + @Test + fun theScrollerShrinkingWhileTheKeyboardIsUpRevealsAgain() { + launch().use { s -> + // The editor ends the page and the page sits at its end, so only room from a smaller scroller lifts the row. + val page = page(s, below = 0) + val editor = s.editor() + val keyboard = onMain { + editor.requestFocus() + page.scrollTo(0, (page.getChildAt(0).height - page.height).coerceAtLeast(0)) + caretLow(page, editor) + val keyboard = caretRow(editor).second.toInt() - 150 + editor.applyKeyboardTop(keyboard) + assertTrue("the row starts covered", caretRow(editor).second > keyboard) + page.layoutParams = page.layoutParams.apply { height = page.height - 600 } + keyboard + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + onMain { assertTrue("row ends at ${caretRow(editor).second} over $keyboard", caretRow(editor).second <= keyboard) } + } + } + + @Test + fun aLayoutThatLeavesTheScrollerAloneKeepsAPageScrolledByHand() { + launch().use { s -> + val page = page(s) + val editor = s.editor() + val scrolled = onMain { + editor.requestFocus() + caretLow(page, editor) + editor.applyKeyboardTop(caretRow(editor).second.toInt() - 150) + page.scrollBy(0, -100) + // A sibling grows by a pixel: the window lays out, the scroller keeps its size. + val spacer = (page.getChildAt(0) as ViewGroup).getChildAt(1) + spacer.layoutParams = spacer.layoutParams.apply { height += 1 } + page.scrollY + } + InstrumentationRegistry.getInstrumentation().waitForIdleSync() + onMain { assertEquals(scrolled, page.scrollY) } + } + } +}