@@ -6,8 +6,10 @@ import android.view.View
66import android.view.ViewGroup
77import android.widget.FrameLayout
88import android.widget.TextView
9+ import androidx.core.view.children
910import androidx.lifecycle.Lifecycle
1011import androidx.test.ext.junit.rules.ActivityScenarioRule
12+ import androidx.test.platform.app.InstrumentationRegistry
1113import assertk.assertFailure
1214import assertk.assertThat
1315import assertk.assertions.isEqualTo
@@ -303,6 +305,47 @@ class ViewRendererTest {
303305 }
304306 }
305307
308+ @Test
309+ fun multiple_renderers_with_the_same_parent_can_be_detached_when_the_activity_is_destroyed () {
310+ val renderers = List (10 ) { TestViewRenderer () }
311+
312+ activityRule.scenario.onActivity { activity ->
313+ val parent = FrameLayout (activity).also { activity.contentView.addView(it) }
314+
315+ renderers.forEachIndexed { index, renderer ->
316+ renderer.init (activity, parent)
317+
318+ // This creates and attaches the view.
319+ renderer.render(TestModel (index))
320+ }
321+ }
322+
323+ activityRule.scenario.moveToState(Lifecycle .State .RESUMED )
324+
325+ activityRule.scenario.onActivity { activity ->
326+ val childViews =
327+ activity.contentView.children.filterIsInstance<ViewGroup >().single().children.toList()
328+
329+ childViews.forEach { assertThat(it.isAttachedToWindow).isTrue() }
330+ renderers.forEach {
331+ assertThat(it.inflateCalled).isEqualTo(1 )
332+ assertThat(it.renderCalled).isEqualTo(1 )
333+ assertThat(it.onDetachCalled).isEqualTo(0 )
334+ }
335+ }
336+
337+ activityRule.scenario.moveToState(Lifecycle .State .DESTROYED )
338+
339+ // Wait for the idle sync, otherwise not all onDetach callbacks may have been invoked.
340+ InstrumentationRegistry .getInstrumentation().waitForIdleSync()
341+
342+ renderers.forEach {
343+ assertThat(it.inflateCalled).isEqualTo(1 )
344+ assertThat(it.renderCalled).isEqualTo(1 )
345+ assertThat(it.onDetachCalled).isEqualTo(1 )
346+ }
347+ }
348+
306349 private fun renderer (
307350 activity : Activity ,
308351 parent : ViewGroup = activity.contentView,
0 commit comments