From 5bc86ccc95c5f0770e394ec644f095911d865776 Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 22 Nov 2026 18:41:56 -0800 Subject: [PATCH 1/2] ADFA-7258: Wait for the editor's async layout in IDEEditorAccessibilityTest "accessibility click requests soft keyboard when editable" failed since it was added in d0bdfa808. The editor is right; the test raced a background thread. IDEEditor's a11y click calls sora's CodeEditor.showSoftInput(), which only reaches InputMethodManager.showSoftInput() when isEditable() is true. isEditable() is false while layoutBusy: sora measures text on a background executor and clears the flag in a callback it posts to the main looper. Robolectric runs that callback only when the test idles the looper, and setUp() idled once, right after setText(), before the layout finished. The click then hit a busy editor and the IMM was never called. setUp() now waits until sora's layout executor is idle and the editor is editable, and the keyboard test asserts that precondition. A temporary 100-iteration stress test failed 99/100 with the old setUp and 0/100 with the new one. The wait reads sora's private static AbstractLayout.executor by reflection; if sora renames it, setUp fails loudly rather than flakily. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw --- .../editor/ui/IDEEditorAccessibilityTest.kt | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/editor/src/test/java/com/itsaky/androidide/editor/ui/IDEEditorAccessibilityTest.kt b/editor/src/test/java/com/itsaky/androidide/editor/ui/IDEEditorAccessibilityTest.kt index 37ec41ad23..6f918caaf2 100644 --- a/editor/src/test/java/com/itsaky/androidide/editor/ui/IDEEditorAccessibilityTest.kt +++ b/editor/src/test/java/com/itsaky/androidide/editor/ui/IDEEditorAccessibilityTest.kt @@ -29,6 +29,7 @@ import androidx.test.core.app.ApplicationProvider import com.google.common.truth.Truth.assertThat import com.itsaky.androidide.app.BaseApplication import com.itsaky.androidide.editor.utils.EditorAccessibilitySegments +import io.github.rosemoe.sora.widget.layout.AbstractLayout import org.junit.After import org.junit.Before import org.junit.Test @@ -36,6 +37,8 @@ import org.junit.runner.RunWith import org.robolectric.RobolectricTestRunner import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config +import java.util.concurrent.ThreadPoolExecutor +import java.util.concurrent.TimeUnit @RunWith(RobolectricTestRunner::class) @Config(application = BaseApplication::class) @@ -57,7 +60,7 @@ class IDEEditorAccessibilityTest { editor = TestIDEEditor(context) editor.isWordwrap = false editor.setText("Line 1: first\nLine 2: second\nLine 3: third") - shadowOf(android.os.Looper.getMainLooper()).idle() + awaitEditorLayout() } @After @@ -65,6 +68,31 @@ class IDEEditorAccessibilityTest { editor.release() } + /** + * CodeEditor measures text on a background executor, and isEditable() stays false (layoutBusy) + * until a callback posted to the main looper clears it; showSoftInput() does nothing until then. + * Robolectric runs that callback only when the test idles the main looper, so one idle() after + * setText() races the background thread. Wait for the executor to drain and the callback to run. + * Two settled polls in a row cover a task caught between leaving the queue and starting. + */ + private fun awaitEditorLayout() { + val executor = + AbstractLayout::class.java.getDeclaredField("executor").let { + it.isAccessible = true + it.get(null) as ThreadPoolExecutor + } + val deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10) + var settledPolls = 0 + while (true) { + val executorIdle = executor.queue.isEmpty() && executor.activeCount == 0 + shadowOf(android.os.Looper.getMainLooper()).idle() + settledPolls = if (executorIdle && editor.isEditable) settledPolls + 1 else 0 + if (settledPolls == 2) return + check(System.nanoTime() < deadline) { "Editor layout did not settle within 10 s" } + Thread.sleep(5) + } + } + @Test fun `node reports as EditText with click and movement actions`() { val info = editor.createAccessibilityNodeInfo() @@ -138,6 +166,8 @@ class IDEEditorAccessibilityTest { fun `accessibility click requests soft keyboard when editable`() { editor.isDisableSoftKbdIfHardKbdAvailable = false val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager + // "Editable" as CodeEditor.showSoftInput() checks it: not just the flag, also no layout in flight. + assertThat(editor.isEditable).isTrue() val clickHandled = editor.performAccessibilityAction(AccessibilityNodeInfo.ACTION_CLICK, null) assertThat(clickHandled).isTrue() From 965184c9f3c5ba3be49652201a7ab2c86fd6d1bb Mon Sep 17 00:00:00 2001 From: David Schachter Date: Sun, 22 Nov 2026 22:09:54 -0800 Subject: [PATCH 2/2] ADFA-7258: Explain why awaitEditorLayout() checks the executor From the independent review of #2301: - The KDoc said isEditable() stays false until the layout callback runs. In fact sora's layout task sets layoutBusy itself when it starts, so isEditable() can read true before then. That is why the helper also waits for the executor to be idle. The second settled poll makes a missed task unlikely, not impossible. - The timeout message now gives the executor's queue size and active count, isEditable and isFormatting, and names the limit once. - The isEditable assertion before the click now says what failed. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vh2DAXGyHbMzjSsi2zbrkw --- .../editor/ui/IDEEditorAccessibilityTest.kt | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/editor/src/test/java/com/itsaky/androidide/editor/ui/IDEEditorAccessibilityTest.kt b/editor/src/test/java/com/itsaky/androidide/editor/ui/IDEEditorAccessibilityTest.kt index 6f918caaf2..0fc225b027 100644 --- a/editor/src/test/java/com/itsaky/androidide/editor/ui/IDEEditorAccessibilityTest.kt +++ b/editor/src/test/java/com/itsaky/androidide/editor/ui/IDEEditorAccessibilityTest.kt @@ -27,6 +27,7 @@ import android.view.inputmethod.InputMethodManager import android.widget.EditText import androidx.test.core.app.ApplicationProvider import com.google.common.truth.Truth.assertThat +import com.google.common.truth.Truth.assertWithMessage import com.itsaky.androidide.app.BaseApplication import com.itsaky.androidide.editor.utils.EditorAccessibilitySegments import io.github.rosemoe.sora.widget.layout.AbstractLayout @@ -69,11 +70,11 @@ class IDEEditorAccessibilityTest { } /** - * CodeEditor measures text on a background executor, and isEditable() stays false (layoutBusy) - * until a callback posted to the main looper clears it; showSoftInput() does nothing until then. - * Robolectric runs that callback only when the test idles the main looper, so one idle() after - * setText() races the background thread. Wait for the executor to drain and the callback to run. - * Two settled polls in a row cover a task caught between leaving the queue and starting. + * Sora's layout task sets layoutBusy when it starts, on a background executor, and clears it in a + * callback posted to the main looper. showSoftInput() does nothing while it is set. So isEditable() + * alone can read true before the task starts: wait until the executor is idle, then idle the main + * looper so the callback runs. Robolectric runs that callback only when the test idles the looper. + * A second settled poll makes it unlikely to miss a task that has left the queue but not yet started. */ private fun awaitEditorLayout() { val executor = @@ -81,14 +82,18 @@ class IDEEditorAccessibilityTest { it.isAccessible = true it.get(null) as ThreadPoolExecutor } - val deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(10) + val timeoutSeconds = 10L + val deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(timeoutSeconds) var settledPolls = 0 while (true) { val executorIdle = executor.queue.isEmpty() && executor.activeCount == 0 shadowOf(android.os.Looper.getMainLooper()).idle() settledPolls = if (executorIdle && editor.isEditable) settledPolls + 1 else 0 if (settledPolls == 2) return - check(System.nanoTime() < deadline) { "Editor layout did not settle within 10 s" } + check(System.nanoTime() < deadline) { + "Editor layout did not settle within $timeoutSeconds s: queued=${executor.queue.size}, " + + "active=${executor.activeCount}, isEditable=${editor.isEditable}, isFormatting=${editor.isFormatting}" + } Thread.sleep(5) } } @@ -167,7 +172,7 @@ class IDEEditorAccessibilityTest { editor.isDisableSoftKbdIfHardKbdAvailable = false val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager // "Editable" as CodeEditor.showSoftInput() checks it: not just the flag, also no layout in flight. - assertThat(editor.isEditable).isTrue() + assertWithMessage("editor not editable before the click (layout still busy?)").that(editor.isEditable).isTrue() val clickHandled = editor.performAccessibilityAction(AccessibilityNodeInfo.ACTION_CLICK, null) assertThat(clickHandled).isTrue()