|
| 1 | +package com.squareup.workflow1.ui |
| 2 | + |
| 3 | +import android.widget.FrameLayout |
| 4 | +import androidx.activity.ComponentActivity |
| 5 | +import androidx.activity.viewModels |
| 6 | +import androidx.lifecycle.Lifecycle.State.CREATED |
| 7 | +import androidx.lifecycle.SavedStateHandle |
| 8 | +import androidx.lifecycle.ViewModel |
| 9 | +import androidx.lifecycle.viewModelScope |
| 10 | +import androidx.test.ext.junit.rules.ActivityScenarioRule |
| 11 | +import com.google.common.truth.Truth.assertThat |
| 12 | +import com.squareup.workflow1.StatelessWorkflow |
| 13 | +import com.squareup.workflow1.ui.internal.test.IdlingDispatcherRule |
| 14 | +import kotlinx.coroutines.Job |
| 15 | +import kotlinx.coroutines.flow.StateFlow |
| 16 | +import leakcanary.DetectLeaksAfterTestSuccess |
| 17 | +import org.junit.Rule |
| 18 | +import org.junit.Test |
| 19 | +import org.junit.rules.RuleChain |
| 20 | + |
| 21 | +@OptIn(WorkflowUiExperimentalApi::class) |
| 22 | +internal class AndroidRenderWorkflowInTest { |
| 23 | + @get:Rule val scenarioRule = ActivityScenarioRule(ComponentActivity::class.java) |
| 24 | + private val scenario get() = scenarioRule.scenario |
| 25 | + |
| 26 | + @get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess()) |
| 27 | + .around(scenarioRule) |
| 28 | + .around(IdlingDispatcherRule) |
| 29 | + |
| 30 | + @Test fun removeWorkflowStateDoesWhatItSaysOnTheTin() { |
| 31 | + var job: Job? = null |
| 32 | + |
| 33 | + // Activity.onCreate(), the take() call won't start pulling yet. |
| 34 | + scenario.onActivity { activity -> |
| 35 | + val model: SomeViewModel by activity.viewModels() |
| 36 | + val renderings: StateFlow<Screen> = renderWorkflowIn( |
| 37 | + workflow = SomeWorkflow, |
| 38 | + scope = model.viewModelScope, |
| 39 | + savedStateHandle = model.savedStateHandle |
| 40 | + ) |
| 41 | + |
| 42 | + val layout = WorkflowLayout(activity) |
| 43 | + activity.setContentView(layout) |
| 44 | + |
| 45 | + assertThat(model.savedStateHandle.contains(KEY)).isFalse() |
| 46 | + |
| 47 | + job = layout.take(activity.lifecycle, renderings) |
| 48 | + assertThat(model.savedStateHandle.contains(KEY)).isFalse() |
| 49 | + } |
| 50 | + |
| 51 | + // Exit onCreate() and move to CREATED status. take() starts to draw |
| 52 | + // and the renderWorkflowIn() call above starts pushing TreeSnapshots |
| 53 | + // (lazy serialization functions) to the SavedStateHandle. |
| 54 | + scenario.moveToState(CREATED) |
| 55 | + scenario.onActivity { activity -> |
| 56 | + val model: SomeViewModel by activity.viewModels() |
| 57 | + assertThat(model.savedStateHandle.contains(KEY)).isTrue() |
| 58 | + |
| 59 | + // The Job returned from take() is canceled. There is still a |
| 60 | + // TreeSnapshot and whatever pointer it captured in the SavedStateHandle. |
| 61 | + job?.cancel() |
| 62 | + assertThat(model.savedStateHandle.contains(KEY)).isTrue() |
| 63 | + |
| 64 | + // We can remove it. |
| 65 | + model.savedStateHandle.removeWorkflowState() |
| 66 | + assertThat(model.savedStateHandle.contains(KEY)).isFalse() |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + object SomeScreen : AndroidScreen<SomeScreen> { |
| 71 | + override val viewFactory: ScreenViewFactory<SomeScreen> = |
| 72 | + ScreenViewFactory.fromCode { _, initialEnvironment, context, _ -> |
| 73 | + ScreenViewHolder( |
| 74 | + initialEnvironment, |
| 75 | + FrameLayout(context) |
| 76 | + ) { _, _ -> } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + object SomeWorkflow : StatelessWorkflow<Unit, Nothing, Screen>() { |
| 81 | + override fun render( |
| 82 | + renderProps: Unit, |
| 83 | + context: RenderContext |
| 84 | + ): Screen { |
| 85 | + return SomeScreen |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + class SomeViewModel(val savedStateHandle: SavedStateHandle) : ViewModel() |
| 90 | +} |
0 commit comments