Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update FakeFormActivityLauncher and FakeManageActivityLauncher to use turbines #9957

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ internal class DefaultEmbeddedSheetLauncherTest {
val paymentMethodMetadata = PaymentMethodMetadataFactory.create()
val expectedArgs = FormContract.Args(code, paymentMethodMetadata)
launcher.launchForm(code, paymentMethodMetadata)
assertThat(formActivityLauncher.didLaunch).isTrue()
assertThat(formActivityLauncher.launchArgs).isEqualTo(expectedArgs)
val launchState = formActivityLauncher.launchTurbine.awaitItem()
assertThat(launchState.didLaunch).isTrue()
assertThat(launchState.launchArgs).isEqualTo(expectedArgs)
}

@Test
Expand All @@ -53,20 +54,15 @@ internal class DefaultEmbeddedSheetLauncherTest {
assertThat(selectionHolder.selection.value).isNull()
}

@Test
fun `formActivityLauncher unregisters onDestroy`() = testScenario {
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
assertThat(formActivityLauncher.didUnregister).isTrue()
}

@Test
fun `launchManage launches activity with correct parameters`() = testScenario {
val paymentMethodMetadata = PaymentMethodMetadataFactory.create()
val customerState = PaymentSheetFixtures.EMPTY_CUSTOMER_STATE
val expectedArgs = ManageContract.Args(paymentMethodMetadata, customerState, PaymentSelection.GooglePay)
launcher.launchManage(paymentMethodMetadata, customerState, PaymentSelection.GooglePay)
assertThat(manageActivityLauncher.didLaunch).isTrue()
assertThat(manageActivityLauncher.launchArgs).isEqualTo(expectedArgs)
val launchState = manageActivityLauncher.launchTurbine.awaitItem()
assertThat(launchState.didLaunch).isTrue()
assertThat(launchState.launchArgs).isEqualTo(expectedArgs)
}

@Test
Expand Down Expand Up @@ -98,9 +94,10 @@ internal class DefaultEmbeddedSheetLauncherTest {
}

@Test
fun `manageActivityLauncher unregisters onDestroy`() = testScenario {
fun `onDestroy unregisters launchers`() = testScenario {
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
assertThat(formActivityLauncher.didUnregister).isTrue()
assertThat(formActivityLauncher.unregisterTurbine.awaitItem()).isTrue()
assertThat(manageActivityLauncher.unregisterTurbine.awaitItem()).isTrue()
}

private fun testScenario(
Expand Down Expand Up @@ -154,6 +151,9 @@ internal class DefaultEmbeddedSheetLauncherTest {
launcher = embeddedActivityLauncher,
customerStateHolder = customerStateHolder,
).block()

formActivityLauncher.validate()
manageActivityLauncher.validate()
}

private class Scenario(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,39 @@ package com.stripe.android.paymentelement.embedded
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContract
import androidx.core.app.ActivityOptionsCompat
import app.cash.turbine.ReceiveTurbine
import app.cash.turbine.Turbine

internal class FakeFormActivityLauncher : ActivityResultLauncher<FormContract.Args>() {
var didLaunch = false
private set
var launchArgs: FormContract.Args? = null
private set
var didUnregister = false
private set
private val _launchStateTurbine = Turbine<LaunchState>()
val launchTurbine: ReceiveTurbine<LaunchState> = _launchStateTurbine
private val _unregisterTurbine = Turbine<Boolean>()
val unregisterTurbine: ReceiveTurbine<Boolean> = _unregisterTurbine
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably even just replace this with Unit instead of Boolean. We don't really care about the value. Just that a value was emitted.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to Unit


override fun launch(input: FormContract.Args?, options: ActivityOptionsCompat?) {
didLaunch = true
launchArgs = input
_launchStateTurbine.add(
LaunchState(
didLaunch = true,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can remove this from the launch state. It'll only be emitted if the launch was called.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should still be possible!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed

launchArgs = input
)
)
}

override fun unregister() {
didUnregister = true
_unregisterTurbine.add(true)
}

override fun getContract(): ActivityResultContract<FormContract.Args, FormResult> {
return FormContract
}

fun validate() {
launchTurbine.ensureAllEventsConsumed()
unregisterTurbine.ensureAllEventsConsumed()
}

data class LaunchState(
val didLaunch: Boolean,
val launchArgs: FormContract.Args?
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,39 @@ package com.stripe.android.paymentelement.embedded.manage
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContract
import androidx.core.app.ActivityOptionsCompat
import app.cash.turbine.ReceiveTurbine
import app.cash.turbine.Turbine

internal class FakeManageActivityLauncher : ActivityResultLauncher<ManageContract.Args>() {
var didLaunch = false
private set
var launchArgs: ManageContract.Args? = null
private set
var didUnregister = false
private set
private val _launchStateTurbine = Turbine<LaunchState>()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's probably a not terrible way to make these generic, and not need 2 of basically the same implementations.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to pair on it if you want!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added generic FakeEmbeddedActivityLauncher

val launchTurbine: ReceiveTurbine<LaunchState> = _launchStateTurbine
private val _unregisterTurbine = Turbine<Boolean>()
val unregisterTurbine: ReceiveTurbine<Boolean> = _unregisterTurbine

override fun launch(input: ManageContract.Args?, options: ActivityOptionsCompat?) {
didLaunch = true
launchArgs = input
_launchStateTurbine.add(
LaunchState(
didLaunch = true,
launchArgs = input
)
)
}

override fun unregister() {
didUnregister = true
_unregisterTurbine.add(true)
}

override fun getContract(): ActivityResultContract<ManageContract.Args, ManageResult> {
return ManageContract
}

fun validate() {
launchTurbine.ensureAllEventsConsumed()
unregisterTurbine.ensureAllEventsConsumed()
}

data class LaunchState(
val didLaunch: Boolean,
val launchArgs: ManageContract.Args?
)
}
Loading