Skip to content
Open
Show file tree
Hide file tree
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,12 +27,12 @@ class AccessibilityEventWrapper(event: AccessibilityEvent) {
}

private fun getIndexInParent(node: AccessibilityNodeInfo): Int {
var index = 0
val parent = node.parent ?: return 0
while (parent.getChild(index) != node) {
index++
return try {
val parent = node.parent ?: return -1
findIndexInParent(parent.childCount, parent::getChild, node)
} catch (_: RuntimeException) {
-1
}
return index
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.autojs.autojs.core.automator

internal fun <T> findIndexInParent(
childCount: Int,
childAt: (Int) -> T?,
target: T,
): Int {
return try {
for (index in 0 until childCount) {
if (childAt(index) == target) {
return index
}
}
-1
} catch (_: RuntimeException) {
-1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.autojs.autojs.core.automator

import org.junit.Assert.assertEquals
import org.junit.Test

class AccessibilityNodeIndexTest {

@Test
fun `returns sentinel without reading past child count when target disappears`() {
val children = listOf("remaining-child")

val index = findIndexInParent(children.size, children::get, "detached-child")

assertEquals(-1, index)
}

@Test
fun `returns sentinel when child access fails`() {
val index = findIndexInParent(1, { throw IllegalStateException("stale node") }, "target")

assertEquals(-1, index)
}

@Test
fun `returns target index when child is present`() {
val children = listOf("first", "target", "last")

val index = findIndexInParent(children.size, children::get, "target")

assertEquals(1, index)
}
}