|
| 1 | +package com.github.donglua.layoutx2c.codegen |
| 2 | + |
| 3 | +import com.github.donglua.layoutx2c.analyzer.LayoutAnalyzerV2 |
| 4 | +import com.github.donglua.layoutx2c.parser.XmlLayoutParser |
| 5 | +import com.google.common.truth.Truth.assertThat |
| 6 | +import org.junit.Test |
| 7 | + |
| 8 | +class TwoWayBindingAdapterBoundaryTest { |
| 9 | + |
| 10 | + @Test |
| 11 | + fun `two-way binding adapter is unsupported without an inverse adapter contract`() { |
| 12 | + val bindingAdapters = listOf( |
| 13 | + BindingAdapterDescriptor( |
| 14 | + attrs = listOf("app:stateColorRes"), |
| 15 | + methodClassName = "com.example.databinding.SampleBindingAdapters", |
| 16 | + methodName = "setStateColor" |
| 17 | + ) |
| 18 | + ) |
| 19 | + val tree = XmlLayoutParser().parse( |
| 20 | + """ |
| 21 | + <layout xmlns:android="http://schemas.android.com/apk/res/android" |
| 22 | + xmlns:app="http://schemas.android.com/apk/res-auto"> |
| 23 | + <data> |
| 24 | + <variable name="vm" type="com.example.StateViewModel" /> |
| 25 | + </data> |
| 26 | + <View |
| 27 | + android:id="@+id/state_indicator" |
| 28 | + android:layout_width="6dp" |
| 29 | + android:layout_height="6dp" |
| 30 | + app:stateColorRes="@={vm.colorRes}" /> |
| 31 | + </layout> |
| 32 | + """.trimIndent(), |
| 33 | + "item_two_way_binding_adapter" |
| 34 | + ) |
| 35 | + val analyzed = LayoutAnalyzerV2(bindingAdapters = bindingAdapters).analyze(tree.root) |
| 36 | + |
| 37 | + val generated = BindingFacadeGeneratorV2( |
| 38 | + packageName = "com.example.generated", |
| 39 | + rPackageName = "com.example", |
| 40 | + bindingAdapters = bindingAdapters |
| 41 | + ).generate( |
| 42 | + analyzedRoot = analyzed, |
| 43 | + layoutName = "item_two_way_binding_adapter", |
| 44 | + layoutResId = "R.layout.item_two_way_binding_adapter", |
| 45 | + useFastPath = true, |
| 46 | + dataBindingVariables = tree.rootMetadata.dataBindingVariables |
| 47 | + ).toString() |
| 48 | + |
| 49 | + assertThat(analyzed.unsupportedAttributes).contains("app:stateColorRes") |
| 50 | + assertThat(analyzed.twoWayBindingAttributes).doesNotContain("app:stateColorRes") |
| 51 | + assertThat(generated).doesNotContain("SampleBindingAdapters.setStateColor") |
| 52 | + } |
| 53 | +} |
0 commit comments