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..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,8 +27,10 @@ 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 import org.junit.After import org.junit.Before import org.junit.Test @@ -36,6 +38,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 +61,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 +69,35 @@ class IDEEditorAccessibilityTest { editor.release() } + /** + * 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 = + AbstractLayout::class.java.getDeclaredField("executor").let { + it.isAccessible = true + it.get(null) as ThreadPoolExecutor + } + 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 $timeoutSeconds s: queued=${executor.queue.size}, " + + "active=${executor.activeCount}, isEditable=${editor.isEditable}, isFormatting=${editor.isFormatting}" + } + Thread.sleep(5) + } + } + @Test fun `node reports as EditText with click and movement actions`() { val info = editor.createAccessibilityNodeInfo() @@ -138,6 +171,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. + 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()