Skip to content
Merged
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
@@ -0,0 +1,74 @@
package com.mobilerun.portal.core

import android.app.UiAutomation
import android.os.SystemClock
import android.view.accessibility.AccessibilityNodeInfo
import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class AccessibilityTreeBuilderInstrumentedTest {

@Test
fun buildFullAccessibilityTreeJson_serializesRealUnboundedRangeInfo() {
val instrumentation = InstrumentationRegistry.getInstrumentation()
val uiAutomation = instrumentation.uiAutomation

ActivityScenario.launch(UnboundedRangeFixtureActivity::class.java).use {
instrumentation.waitForIdleSync()

val node = waitForUnboundedRangeNode(uiAutomation)
val json = AccessibilityTreeBuilder.buildFullAccessibilityTreeJson(node)

assertNotNull(json)
val rangeJson = JSONObject(json!!.toString()).getJSONObject("rangeInfo")
assertEquals(AccessibilityNodeInfo.RangeInfo.RANGE_TYPE_FLOAT, rangeJson.getInt("type"))
assertTrue(rangeJson.isNull("min"))
assertTrue(rangeJson.isNull("max"))
assertEquals(0.0, rangeJson.getDouble("current"), 0.0)
}
}

@Suppress("DEPRECATION")
private fun waitForUnboundedRangeNode(uiAutomation: UiAutomation): AccessibilityNodeInfo {
val deadline = SystemClock.uptimeMillis() + NODE_TIMEOUT_MS

do {
val root = uiAutomation.rootInActiveWindow
val candidates = root
?.findAccessibilityNodeInfosByText(UnboundedRangeFixtureActivity.NODE_MARKER)
.orEmpty()
var match: AccessibilityNodeInfo? = null
candidates.forEach { candidate ->
val range = candidate.rangeInfo
if (
match == null &&
range?.min == Float.NEGATIVE_INFINITY &&
range.max == Float.POSITIVE_INFINITY
) {
match = candidate
} else {
candidate.recycle()
}
}
root?.recycle()

match?.let { return it }
SystemClock.sleep(NODE_POLL_INTERVAL_MS)
} while (SystemClock.uptimeMillis() < deadline)

throw AssertionError("Timed out waiting for a sealed accessibility node with an unbounded range")
}

private companion object {
const val NODE_TIMEOUT_MS = 5_000L
const val NODE_POLL_INTERVAL_MS = 50L
}
}
11 changes: 11 additions & 0 deletions app/src/debug/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application>
<activity
android:name=".core.UnboundedRangeFixtureActivity"
android:exported="false"
android:theme="@android:style/Theme.Material.Light.NoActionBar" />
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.mobilerun.portal.core

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.view.accessibility.AccessibilityNodeInfo

/**
* Minimal debug-only host for accessibility instrumentation fixtures.
*/
class UnboundedRangeFixtureActivity : Activity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(
UnboundedRangeView(this),
ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT,
),
)
}

@Suppress("DEPRECATION")
private class UnboundedRangeView(context: Context) : View(context) {
init {
importantForAccessibility = IMPORTANT_FOR_ACCESSIBILITY_YES
isFocusable = true
isFocusableInTouchMode = true
contentDescription = NODE_MARKER
}

override fun onInitializeAccessibilityNodeInfo(info: AccessibilityNodeInfo) {
super.onInitializeAccessibilityNodeInfo(info)
info.className = "android.widget.SeekBar"
info.text = NODE_MARKER
info.rangeInfo = AccessibilityNodeInfo.RangeInfo.obtain(
AccessibilityNodeInfo.RangeInfo.RANGE_TYPE_FLOAT,
Float.NEGATIVE_INFINITY,
Float.POSITIVE_INFINITY,
0f,
)
}
}

companion object {
const val NODE_MARKER = "mobilerun-unbounded-range-fixture"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,9 @@ object AccessibilityTreeBuilder {
node.rangeInfo?.let { range ->
put("rangeInfo", JSONObject().apply {
put("type", range.type)
put("min", range.min.toDouble())
put("max", range.max.toDouble())
put("current", range.current.toDouble())
put("min", jsonFiniteFloatOrNull(range.min))
put("max", jsonFiniteFloatOrNull(range.max))
put("current", jsonFiniteFloatOrNull(range.current))
})
}

Expand Down Expand Up @@ -367,6 +367,10 @@ object AccessibilityTreeBuilder {
}
}

private fun jsonFiniteFloatOrNull(value: Float): Any {
return if (value.isFinite()) value.toDouble() else JSONObject.NULL
}

private fun getVisiblePercentage(rect: Rect, screenBounds: Rect): Float {
val width = rect.right - rect.left
val height = rect.bottom - rect.top
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,48 @@ class AccessibilityTreeBuilderTest {
verify(exactly = 1) { root.recycle() }
}

@Test
fun buildFullAccessibilityTreeJson_mapsNonFiniteRangeValuesToJsonNull() {
val root = node("root")
val range = mockk<AccessibilityNodeInfo.RangeInfo>()
configureNode(root)
every { range.type } returns AccessibilityNodeInfo.RangeInfo.RANGE_TYPE_FLOAT
every { range.min } returns Float.NEGATIVE_INFINITY
every { range.max } returns Float.POSITIVE_INFINITY
every { range.current } returns Float.NaN
every { root.rangeInfo } returns range

val json = AccessibilityTreeBuilder.buildFullAccessibilityTreeJson(root)

assertNotNull(json)
val rangeJson = JSONObject(json!!.toString()).getJSONObject("rangeInfo")
assertEquals(AccessibilityNodeInfo.RangeInfo.RANGE_TYPE_FLOAT, rangeJson.getInt("type"))
assertTrue(rangeJson.isNull("min"))
assertTrue(rangeJson.isNull("max"))
assertTrue(rangeJson.isNull("current"))
}

@Test
fun buildFullAccessibilityTreeJson_preservesFiniteRangeValues() {
val root = node("root")
val range = mockk<AccessibilityNodeInfo.RangeInfo>()
configureNode(root)
every { range.type } returns AccessibilityNodeInfo.RangeInfo.RANGE_TYPE_FLOAT
every { range.min } returns -10.5f
every { range.max } returns 42.25f
every { range.current } returns 7.5f
every { root.rangeInfo } returns range

val json = AccessibilityTreeBuilder.buildFullAccessibilityTreeJson(root)

assertNotNull(json)
val rangeJson = JSONObject(json!!.toString()).getJSONObject("rangeInfo")
assertEquals(AccessibilityNodeInfo.RangeInfo.RANGE_TYPE_FLOAT, rangeJson.getInt("type"))
assertEquals(-10.5, rangeJson.getDouble("min"), 0.0)
assertEquals(42.25, rangeJson.getDouble("max"), 0.0)
assertEquals(7.5, rangeJson.getDouble("current"), 0.0)
}

private fun node(name: String): AccessibilityNodeInfo {
return mockk(name = name, relaxed = true)
}
Expand Down
Loading