Skip to content

Commit 0619668

Browse files
kirich1409claude
andcommitted
fix: correct @JvmName in ViewGroupBindings, fix nullable in findRootView, add tests
- Fix @JvmName("viewBindingFragment") → "viewBindingViewGroup"/"viewBindingViewGroupInflate" in reflection ViewGroupBindings (copy-paste error from FragmentViewBindings) - Fix DialogFragment.findRootView() using findViewById (nullable) instead of requireViewByIdCompat (non-null) when showsDialog=false - Add EagerViewBindingProperty.clear() test verifying no-op behavior - Add ActivityViewBindingProperty test verifying binding recreation after clear() Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 029c786 commit 0619668

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

vbpd-core/src/test/kotlin/dev/androidbroadcast/vbpd/EagerViewBindingPropertyTest.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,15 @@ class EagerViewBindingPropertyTest {
4040
val second = property.getValue(thisRef, mockProperty)
4141
assertEquals(first, second)
4242
}
43+
44+
@Test
45+
fun `clear does not affect getValue`() {
46+
val binding = createMockBinding()
47+
val property = EagerViewBindingProperty<Any, ViewBinding>(binding)
48+
val thisRef = Any()
49+
50+
property.clear()
51+
val result = property.getValue(thisRef, mockProperty)
52+
assertEquals(binding, result)
53+
}
4354
}

vbpd-reflection/src/main/kotlin/dev/androidbroadcast/vbpd/ViewGroupBindings.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import androidx.viewbinding.ViewBinding
1414
*
1515
* @return [ViewBindingProperty] that holds [ViewBinding] instance
1616
*/
17-
@JvmName("viewBindingFragment")
17+
@JvmName("viewBindingViewGroup")
1818
public inline fun <reified T : ViewBinding> ViewGroup.viewBinding(
1919
createMethod: CreateMethod = CreateMethod.BIND,
2020
): ViewBindingProperty<ViewGroup, T> = viewBinding(T::class.java, createMethod)
@@ -27,7 +27,7 @@ public inline fun <reified T : ViewBinding> ViewGroup.viewBinding(
2727
*
2828
* @return [ViewBindingProperty] that holds [ViewBinding] instance
2929
*/
30-
@JvmName("viewBindingFragment")
30+
@JvmName("viewBindingViewGroup")
3131
@JvmOverloads
3232
public fun <T : ViewBinding> ViewGroup.viewBinding(
3333
viewBindingClass: Class<T>,
@@ -50,7 +50,7 @@ public fun <T : ViewBinding> ViewGroup.viewBinding(
5050
*
5151
* @return [ViewBindingProperty] that holds [ViewBinding] instance
5252
*/
53-
@JvmName("viewBindingFragment")
53+
@JvmName("viewBindingViewGroupInflate")
5454
public inline fun <reified T : ViewBinding> ViewGroup.viewBinding(attachToRoot: Boolean = false): ViewBindingProperty<ViewGroup, T> =
5555
viewBinding(T::class.java, attachToRoot)
5656

@@ -62,7 +62,7 @@ public inline fun <reified T : ViewBinding> ViewGroup.viewBinding(attachToRoot:
6262
*
6363
* @return [ViewBindingProperty] that holds [ViewBinding] instance
6464
*/
65-
@JvmName("viewBindingFragment")
65+
@JvmName("viewBindingViewGroupInflate")
6666
public fun <T : ViewBinding> ViewGroup.viewBinding(
6767
viewBindingClass: Class<T>,
6868
attachToRoot: Boolean = false,

vbpd/src/main/kotlin/dev/androidbroadcast/vbpd/internal/VbpdUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public fun DialogFragment.findRootView(
5252
if (viewBindingRootId != 0) requireViewByIdCompat(viewBindingRootId) else this
5353
}
5454
} else {
55-
return requireView().findViewById(viewBindingRootId)
55+
return requireView().requireViewByIdCompat(viewBindingRootId)
5656
}
5757
}
5858

vbpd/src/test/kotlin/dev/androidbroadcast/vbpd/ActivityViewBindingPropertyTest.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import org.junit.Test
1111
import org.junit.runner.RunWith
1212
import org.robolectric.Robolectric
1313
import org.robolectric.RobolectricTestRunner
14+
import kotlin.reflect.KProperty
15+
import kotlin.test.assertEquals
16+
import kotlin.test.assertNotEquals
1417
import kotlin.test.assertNotNull
1518

1619
@RunWith(RobolectricTestRunner::class)
@@ -55,6 +58,37 @@ class ActivityViewBindingPropertyTest {
5558
val activity = controller.get()
5659
assertNotNull(activity.binding)
5760

61+
// Verify full lifecycle completes without crash
62+
// onDestroy triggers clear() which nulls binding and unregisters callbacks
5863
controller.pause().stop().destroy()
5964
}
65+
66+
@Test
67+
fun `clear resets binding and allows recreation`() {
68+
val mockProperty = mockk<KProperty<*>>(relaxed = true)
69+
var callCount = 0
70+
val delegate =
71+
ActivityViewBindingProperty<Activity, ViewBinding> { activity ->
72+
callCount++
73+
mockk<ViewBinding> {
74+
every { root } returns View(activity)
75+
}
76+
}
77+
78+
val controller =
79+
Robolectric
80+
.buildActivity(TestActivity::class.java)
81+
.create()
82+
.start()
83+
.resume()
84+
val activity = controller.get()
85+
86+
val binding1 = delegate.getValue(activity, mockProperty)
87+
assertEquals(1, callCount)
88+
89+
delegate.clear()
90+
val binding2 = delegate.getValue(activity, mockProperty)
91+
assertEquals(2, callCount)
92+
assertNotEquals(binding1, binding2, "After clear(), a new binding should be created")
93+
}
6094
}

0 commit comments

Comments
 (0)