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
Expand Up @@ -99,7 +99,7 @@ class ComposeUiTest {
Radiography.scan(viewStateRenderers = emptyList())
}

assertThat(hierarchy).contains("Box { }")
assertThat(hierarchy).contains("Box")
}

@Test fun semanticsAreReported() {
Expand All @@ -124,7 +124,7 @@ class ComposeUiTest {
assertThat(hierarchy).contains("Box { value:\"acc value\" }")
assertThat(hierarchy).contains("Box { DISABLED }")
assertThat(hierarchy).contains("Box { FOCUSED }")
assertThat(hierarchy).contains("Box { }")
assertThat(hierarchy).contains("Box")
assertThat(hierarchy).contains("Box { HIDDEN }")
assertThat(hierarchy).contains("Box { DIALOG }")
assertThat(hierarchy).contains("Box { POPUP }")
Expand Down Expand Up @@ -259,14 +259,14 @@ class ComposeUiTest {
assertThat(hierarchy).contains(
"""
Box:
  ${BLANK}Box { test-tag:"root" }
  ${BLANK}├─Box { }
  ${BLANK}├─Column { }
  ${BLANK}│ ├─Box { }
  ${BLANK}│ ╰─Box { }
  ${BLANK}╰─Row { }
  ${BLANK} ├─Box { }
  ${BLANK} ╰─Box { }
$BLANK ${BLANK}Box { test-tag:"root" }
$BLANK ${BLANK}├─Box
$BLANK ${BLANK}├─Column
$BLANK ${BLANK}│ ├─Box
$BLANK ${BLANK}│ ╰─Box
$BLANK ${BLANK}╰─Row
$BLANK ${BLANK} ├─Box
$BLANK ${BLANK} ╰─Box
""".trimIndent()
)
}
Expand All @@ -291,7 +291,7 @@ class ComposeUiTest {
"""
${BLANK} Box:
${BLANK}Box { test-tag:"root" }
${BLANK} ${BLANK}╰─AndroidView { }
${BLANK} ${BLANK}╰─AndroidView
""".trimIndent()
)
// But this view description should show up at some point.
Expand All @@ -315,7 +315,7 @@ class ComposeUiTest {
"""
|Providers:
|${BLANK}Providers { test-tag:"parent" }
|${BLANK}╰─Dialog { }
|${BLANK}╰─Dialog
|${BLANK} ╰─Providers { DIALOG }
|${BLANK} ╰─Box { test-tag:"child" }
|
Expand Down Expand Up @@ -344,7 +344,7 @@ class ComposeUiTest {
"""
|Providers:
|${BLANK}Providers { test-tag:"parent" }
|${BLANK}╰─CustomTestDialog { }
|${BLANK}╰─CustomTestDialog
|${BLANK} ╰─Providers { DIALOG }
|${BLANK} ╰─Box { test-tag:"child" }
|
Expand All @@ -370,7 +370,7 @@ class ComposeUiTest {
|Providers:
|${BLANK}Providers { test-tag:"parent" }
|${BLANK}╰─SingleSubcompositionLayout { test-tag:"subcompose-layout" }
|${BLANK} ╰─<subcomposition of SingleSubcompositionLayout> { }
|${BLANK} ╰─<subcomposition of SingleSubcompositionLayout>
|${BLANK} ╰─Box { test-tag:"child" }
|
""".trimMargin()
Expand All @@ -396,7 +396,7 @@ class ComposeUiTest {
|Providers:
|${BLANK}Providers { test-tag:"parent" }
|${BLANK}╰─SingleSubcompositionLayout { test-tag:"subcompose-layout" }
|${BLANK} ╰─<subcomposition of SingleSubcompositionLayout> { }
|${BLANK} ╰─<subcomposition of SingleSubcompositionLayout>
|${BLANK} ├─Box { test-tag:"child1" }
|${BLANK} ╰─Box { test-tag:"child2" }
|
Expand Down Expand Up @@ -428,10 +428,10 @@ class ComposeUiTest {
|Providers:
|${BLANK}Providers { test-tag:"parent" }
|${BLANK}╰─MultipleSubcompositionLayout { test-tag:"subcompose-layout" }
|${BLANK} ├─<subcomposition of MultipleSubcompositionLayout> { }
|${BLANK} ├─<subcomposition of MultipleSubcompositionLayout>
|${BLANK} │ ├─Box { test-tag:"child1.1" }
|${BLANK} │ ╰─Box { test-tag:"child1.2" }
|${BLANK} ╰─<subcomposition of MultipleSubcompositionLayout> { }
|${BLANK} ╰─<subcomposition of MultipleSubcompositionLayout>
|${BLANK} ├─Box { test-tag:"child2.1" }
|${BLANK} ╰─Box { test-tag:"child2.2" }
|
Expand Down Expand Up @@ -460,10 +460,10 @@ class ComposeUiTest {
|Providers:
|${BLANK}Providers { test-tag:"parent" }
|${BLANK}├─SingleSubcompositionLayout { test-tag:"subcompose-layout1" }
|${BLANK}│ ╰─<subcomposition of SingleSubcompositionLayout> { }
|${BLANK}│ ╰─<subcomposition of SingleSubcompositionLayout>
|${BLANK}│ ╰─Box { test-tag:"child1" }
|${BLANK}╰─SingleSubcompositionLayout { test-tag:"subcompose-layout2" }
|${BLANK} ╰─<subcomposition of SingleSubcompositionLayout> { }
|${BLANK} ╰─<subcomposition of SingleSubcompositionLayout>
|${BLANK} ╰─Box { test-tag:"child2" }
|
""".trimMargin()
Expand All @@ -488,7 +488,7 @@ class ComposeUiTest {
|Providers:
|${BLANK}Providers { test-tag:"parent" }
|${BLANK}╰─WithConstraints { test-tag:"with-constraints" }
|${BLANK} ╰─<subcomposition of WithConstraints> { }
|${BLANK} ╰─<subcomposition of WithConstraints>
|${BLANK} ╰─Box { test-tag:"child" }
|
""".trimMargin()
Expand Down Expand Up @@ -516,12 +516,12 @@ class ComposeUiTest {
|Providers:
|${BLANK}Providers { test-tag:"parent" }
|${BLANK}╰─LazyColumnFor { test-tag:"list" }
|${BLANK} ├─<subcomposition of LazyColumnFor> { }
|${BLANK} ├─<subcomposition of LazyColumnFor>
|${BLANK} │ ╰─Box { test-tag:"child:1" }
|${BLANK} ├─<subcomposition of LazyColumnFor> { }
|${BLANK} ├─<subcomposition of LazyColumnFor>
|${BLANK} │ ├─Box { test-tag:"child:2" }
|${BLANK} │ ╰─Box { test-tag:"child:2 (even)" }
|${BLANK} ╰─<subcomposition of LazyColumnFor> { }
|${BLANK} ╰─<subcomposition of LazyColumnFor>
|${BLANK} ╰─Box { test-tag:"child:3" }
|
""".trimMargin()
Expand Down Expand Up @@ -555,10 +555,10 @@ class ComposeUiTest {
|Providers:
|${BLANK}Providers { 10×30px, test-tag:"parent" }
|${BLANK}╰─MultipleSubcompositionLayout { 10×30px, test-tag:"subcompose-layout" }
|${BLANK} ├─<subcomposition of MultipleSubcompositionLayout> { }
|${BLANK} ├─<subcomposition of MultipleSubcompositionLayout>
|${BLANK} │ ├─Box { 10×10px, test-tag:"child1" }
|${BLANK} │ ╰─Box { 10×10px, test-tag:"child2" }
|${BLANK} ╰─<subcomposition of MultipleSubcompositionLayout> { }
|${BLANK} ╰─<subcomposition of MultipleSubcompositionLayout>
|${BLANK} ╰─Box { 10×10px, test-tag:"child3" }
|
""".trimMargin()
Expand Down
25 changes: 22 additions & 3 deletions radiography/src/main/java/radiography/Radiography.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Handler
import android.os.Looper
import android.view.View
import android.view.WindowManager
import androidx.annotation.VisibleForTesting
import radiography.Radiography.scan
import radiography.ScanScopes.AllWindowsScope
import radiography.ScannableView.AndroidView
Expand Down Expand Up @@ -102,21 +103,39 @@ public object Radiography {
}
}

private fun renderScannableViewTree(
@VisibleForTesting
@JvmSynthetic
internal fun renderScannableViewTree(
builder: StringBuilder,
rootView: ScannableView,
viewStateRenderers: List<ViewStateRenderer>,
viewFilter: ViewFilter
) {
renderTreeString(builder, rootView) {
append("${it.displayName} { ")
append(it.displayName)

// Surround attributes in curly braces.
// If no attributes get written (the length doesn't change), we'll remove the curly
// brace. We append the opening brace before attempting to write, and then delete it later if
// nothing was written, because most views have at least one attribute, so in most cases we
// need the prefix anyway.
append(" { ")
val attributesStartIndex = length

val appendable = AttributeAppendable(this)
for (renderer in viewStateRenderers) {
with(renderer) {
appendable.render(it)
}
}
append(" }")

if (length == attributesStartIndex) {
// No attributes were written, remove the opening curly brace.
delete(attributesStartIndex - 3, length)
} else {
// At least one attribute was written, so close the braces.
append(" }")
}

return@renderTreeString it.children.filter(viewFilter::matches).toList()
}
Expand Down
29 changes: 29 additions & 0 deletions radiography/src/test/java/radiography/RadiographyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config
import radiography.ScannableView.AndroidView
import radiography.ViewFilters.and
import radiography.ViewFilters.skipIdsViewFilter
import radiography.ViewStateRenderers.textViewRenderer
Expand Down Expand Up @@ -179,6 +180,34 @@ class RadiographyTest {
)
}

@Test fun `renderScannableViewTree includes output from renderers`() {
val builder = StringBuilder()
val rootView = View(context)
val scannableRoot = AndroidView(rootView)
val renderers = listOf(
ViewStateRenderer { append("render1") },
ViewStateRenderer { append("render2") },
)

Radiography.renderScannableViewTree(
builder, scannableRoot, renderers, ViewFilters.NoFilter
)

assertThat(builder.toString()).isEqualTo("${BLANK}View { render1, render2 }\n")
}

@Test fun `renderScannableViewTree doesn't render empty curly braces`() {
val builder = StringBuilder()
val rootView = View(context)
val scannableRoot = AndroidView(rootView)

Radiography.renderScannableViewTree(
builder, scannableRoot, viewStateRenderers = emptyList(), ViewFilters.NoFilter
)

assertThat(builder.toString()).isEqualTo("${BLANK}View\n")
}

companion object {
private const val BLANK = '\u00a0'
}
Expand Down