Skip to content

Commit

Permalink
add ViewNode.sibling
Browse files Browse the repository at this point in the history
  • Loading branch information
Krosxx committed Apr 26, 2023
1 parent 549c4ba commit ade0ea8
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 12 deletions.
3 changes: 0 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
<instrumentation
android:name=".MyInstrumentation"
android:targetPackage="${applicationId}" />
<instrumentation
android:name="cn.vove7.accessibility.uiauto.AutoInstrumentation"
android:targetPackage="${applicationId}" />
<application
android:name=".DemoApp"
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class MainActivity : AppCompatActivity() {
val actions = mutableListOf(
BaseNavigatorAction(),
PickScreenText(),
SiblingTestAction(),
DrawableAction(),
WaitAppAction(),
SelectTextAction(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import cn.vove7.auto.core.utils.AdapterRectF
import cn.vove7.auto.core.utils.AutoGestureDescription
import cn.vove7.auto.core.utils.GestureResultCallback
import cn.vove7.auto.core.viewfinder.*
import cn.vove7.auto.core.viewnode.ViewNode
import kotlinx.coroutines.*
import timber.log.Timber
import kotlin.coroutines.coroutineContext
Expand Down Expand Up @@ -85,6 +86,20 @@ class PickScreenText : Action() {
}
}

class SiblingTestAction :Action() {
override val name: String = "SiblingTest"

override suspend fun run(act: ComponentActivity) {
printLayoutInfo()
val view = withText(name).requireFirst()
Timber.i("view: $view")
Timber.i("view.previousSibling: ${view.previousSibling}")
Timber.i("view.nextSibling: ${view.nextSibling}")
check(view.previousSibling?.nextSibling == view)
toast("assert success")
}
}

class DrawableAction : Action() {
override val name: String
get() = "手势画图 - Rect - Circle - Oval"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cn.vove7.auto.core.viewfinder
import android.os.Build
import android.view.accessibility.AccessibilityNodeInfo
import androidx.annotation.RequiresApi
import cn.vove7.auto.core.viewnode.ViewNode
import cn.vove7.auto.core.viewnode.ViewOperation
import kotlinx.coroutines.runBlocking

Expand Down Expand Up @@ -143,4 +144,9 @@ interface FinderBuilderWithOperation : ViewOperation {

@RequiresApi(Build.VERSION_CODES.R)
override fun sendImeAction() = node.sendImeAction()

override val previousSibling: ViewNode?
get() = node.previousSibling
override val nextSibling: ViewNode?
get() = node.nextSibling
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cn.vove7.auto.core.viewfinder

import android.view.accessibility.AccessibilityNodeInfo
import cn.vove7.auto.core.viewnode.ViewNode
import cn.vove7.auto.core.viewnode.ViewOperation

/**
* # SmartFinder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import androidx.annotation.RequiresApi
import cn.vove7.auto.core.utils.times
import cn.vove7.auto.core.viewfinder.*
import cn.vove7.auto.core.viewnode.ViewNode
import timber.log.Timber

/**
* # apis
Expand Down Expand Up @@ -105,17 +106,17 @@ private fun ViewNode?.printWithChild(
includeInvisible: Boolean
) {
if (this == null) {
Log.d("ViewNode", "*" * dep + "[$index] null")
Timber.tag("ViewNode").d("*" * dep + "[" + index + "] null")
return
}
if (!includeInvisible && !isVisibleToUser) {
Log.w("ViewNode", "*" * dep + "[$index] " + "InVisible")
Timber.tag("ViewNode").w("*" * dep + "[" + index + "] " + "InVisible")
return
}
if (isVisibleToUser) {
Log.d("ViewNode", "*" * dep + "[$index] " + toString())
Timber.tag("ViewNode").d("*" * dep + "[" + index + "] " + toString())
} else {
Log.w("ViewNode", "*" * dep + "[$index] " + toString())
Timber.tag("ViewNode").w("*" * dep + "[" + index + "] " + toString())
}
children.forEachIndexed { i, it ->
it.printWithChild(i, dep + 1, includeInvisible)
Expand Down
19 changes: 19 additions & 0 deletions core/src/main/java/cn/vove7/auto/core/viewnode/ViewNode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,16 @@ class ViewNode(
override val parent: ViewNode?
get() = if (buildWithChildren) null else node.parent?.let { ViewNode(it) }

override val previousSibling: ViewNode?
get() = parent?.children?.let { parChildren ->
parChildren.getOrNull(parChildren.indexOf(this) - 1)
}

override val nextSibling: ViewNode?
get() = parent?.children?.let { parChildren ->
parChildren.getOrNull(parChildren.indexOf(this) + 1)
}

override fun tryClick(): Boolean {
return tryOp(AccessibilityNodeInfo.ACTION_CLICK)
}
Expand Down Expand Up @@ -410,4 +420,13 @@ class ViewNode(
override fun sendImeAction(): Boolean {
return node.performAction(android.R.id.accessibilityActionImeEnter)
}

override fun equals(other: Any?): Boolean {
return if (other is ViewNode) {
node == other.node
} else false
}

override fun hashCode() = node.hashCode()

}
12 changes: 7 additions & 5 deletions core/src/main/java/cn/vove7/auto/core/viewnode/ViewOperation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package cn.vove7.auto.core.viewnode

import android.graphics.Point
import android.graphics.Rect
import android.os.Build
import android.view.accessibility.AccessibilityNodeInfo
import android.view.accessibility.AccessibilityNodeInfo.RangeInfo
import androidx.annotation.RequiresApi
import cn.vove7.auto.core.utils.ViewChildList
import cn.vove7.auto.core.viewfinder.SmartFinder

Expand Down Expand Up @@ -57,6 +55,10 @@ interface ViewOperation {
*/
val parent: ViewNode?

// 兄弟结点
val previousSibling: ViewNode?
val nextSibling: ViewNode?

val requireParent: ViewNode
get() {
return parent ?: throw NullPointerException("parent is null of $this")
Expand Down Expand Up @@ -151,22 +153,22 @@ interface ViewOperation {

fun desc(): CharSequence?

//选择
// 选择
fun select(): Boolean

fun setSelection(start: Int, end: Int): Boolean
fun clearSelection(): Boolean

fun trySelect(): Boolean

//获得焦点
// 获得焦点
fun focus(): Boolean

fun clearFocus(): Boolean

/***以下不常用***/

//一般
// 一般
fun scrollUp(): Boolean

fun scrollDown(): Boolean
Expand Down

0 comments on commit ade0ea8

Please sign in to comment.