Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,19 @@ 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
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)
Expand All @@ -57,14 +61,43 @@ 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
fun tearDown() {
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()
Expand Down Expand Up @@ -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()
Expand Down
Loading