Skip to content

Commit

Permalink
android/foundation: Finish the activity selectively on completion
Browse files Browse the repository at this point in the history
Finishing an activity does not make much sense if it's in the process
of recreation or has already been finished.
  • Loading branch information
MrHadiSatrio committed Jul 14, 2023
1 parent c8ca194 commit 5163575
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ActivityCompletionEventSink(

override fun sink(event: Event) {
if (event !is CompletionEvent) return
if (activity.isFinishing || activity.isChangingConfigurations) return
activity.finish()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ import com.hadisatrio.libs.kotlin.foundation.event.CompletionEvent
import com.hadisatrio.libs.kotlin.foundation.event.SelectionEvent
import com.hadisatrio.libs.kotlin.foundation.event.TextInputEvent
import com.hadisatrio.libs.kotlin.foundation.modal.ModalApprovalEvent
import io.kotest.matchers.booleans.shouldBeFalse
import io.kotest.matchers.booleans.shouldBeTrue
import io.mockk.every
import io.mockk.spyk
import io.mockk.verify
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
Expand All @@ -35,7 +36,8 @@ import org.robolectric.Robolectric
class ActivityCompletionEventSinkTest {

private val activityController = Robolectric.buildActivity(ComponentActivity::class.java)
private val eventSink = ActivityCompletionEventSink(activityController.get())
private val activity = spyk(activityController.get())
private val eventSink = ActivityCompletionEventSink(activity)

@Before
fun `Starts activity`() {
Expand All @@ -45,7 +47,21 @@ class ActivityCompletionEventSinkTest {
@Test
fun `Finishes the activity upon receiving a completion event`() {
eventSink.sink(CompletionEvent())
activityController.get().isFinishing.shouldBeTrue()
verify(exactly = 1) { activity.finish() }
}

@Test
fun `Does not try to finish if the activity is already finishing`() {
activity.finish()
eventSink.sink(CompletionEvent())
verify(exactly = 1) { activity.finish() }
}

@Test
fun `Does not try to finish if the activity is changing configurations`() {
every { activity.isChangingConfigurations }.returns(true)
eventSink.sink(CompletionEvent())
verify(inverse = true) { activity.finish() }
}

@Test
Expand All @@ -56,6 +72,6 @@ class ActivityCompletionEventSinkTest {
ModalApprovalEvent("lorem"),
CancellationEvent("system"),
).forEach { event -> eventSink.sink(event) }
activityController.get().isFinishing.shouldBeFalse()
verify(inverse = true) { activity.finish() }
}
}

0 comments on commit 5163575

Please sign in to comment.