Skip to content

Commit e1d8905

Browse files
vRallevJessalynWang
authored andcommitted
Add test for activity cleanup
Add a test that verifies `ViewRenderers` are properly cleaned up when the activity is destroyed without crashing the app.
1 parent c607e03 commit e1d8905

File tree

1 file changed

+43
-0
lines changed
  • renderer-android-view/public/src/androidInstrumentedTest/kotlin/software/amazon/app/platform/renderer

1 file changed

+43
-0
lines changed

renderer-android-view/public/src/androidInstrumentedTest/kotlin/software/amazon/app/platform/renderer/ViewRendererTest.kt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import android.view.View
66
import android.view.ViewGroup
77
import android.widget.FrameLayout
88
import android.widget.TextView
9+
import androidx.core.view.children
910
import androidx.lifecycle.Lifecycle
1011
import androidx.test.ext.junit.rules.ActivityScenarioRule
12+
import androidx.test.platform.app.InstrumentationRegistry
1113
import assertk.assertFailure
1214
import assertk.assertThat
1315
import 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

Comments
 (0)