From 48e2de76cb39b41d91f129ad785c23643d4edafd Mon Sep 17 00:00:00 2001 From: "DeanT.Maxim" <22721535+DeanTMaxim@users.noreply.github.com> Date: Fri, 21 Aug 2026 23:29:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=98=B2=E6=AD=A2=E6=97=A0=E9=9A=9C?= =?UTF-8?q?=E7=A2=8D=E4=BA=8B=E4=BB=B6=E6=BA=90=E8=8A=82=E7=82=B9=E7=B4=A2?= =?UTF-8?q?=E5=BC=95=E8=B6=8A=E7=95=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../automator/AccessibilityEventWrapper.kt | 10 +++--- .../core/automator/AccessibilityNodeIndex.kt | 18 +++++++++++ .../automator/AccessibilityNodeIndexTest.kt | 32 +++++++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/org/autojs/autojs/core/automator/AccessibilityNodeIndex.kt create mode 100644 app/src/test/java/org/autojs/autojs/core/automator/AccessibilityNodeIndexTest.kt diff --git a/app/src/main/java/org/autojs/autojs/core/automator/AccessibilityEventWrapper.kt b/app/src/main/java/org/autojs/autojs/core/automator/AccessibilityEventWrapper.kt index f72a42d44..d08e5d266 100644 --- a/app/src/main/java/org/autojs/autojs/core/automator/AccessibilityEventWrapper.kt +++ b/app/src/main/java/org/autojs/autojs/core/automator/AccessibilityEventWrapper.kt @@ -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 } } diff --git a/app/src/main/java/org/autojs/autojs/core/automator/AccessibilityNodeIndex.kt b/app/src/main/java/org/autojs/autojs/core/automator/AccessibilityNodeIndex.kt new file mode 100644 index 000000000..e39e6d363 --- /dev/null +++ b/app/src/main/java/org/autojs/autojs/core/automator/AccessibilityNodeIndex.kt @@ -0,0 +1,18 @@ +package org.autojs.autojs.core.automator + +internal fun 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 + } +} diff --git a/app/src/test/java/org/autojs/autojs/core/automator/AccessibilityNodeIndexTest.kt b/app/src/test/java/org/autojs/autojs/core/automator/AccessibilityNodeIndexTest.kt new file mode 100644 index 000000000..41e001f7a --- /dev/null +++ b/app/src/test/java/org/autojs/autojs/core/automator/AccessibilityNodeIndexTest.kt @@ -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) + } +}