-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces
SavedStateHandle.removeWorkflowState()
If you're juggling multiple workflow runtimes per `Activity` (if you're the kind of person who needs to call `Job.cancel` on the `Job` returned from `WorkflowLayout.take`) then you might also need to throw away the state captured by an AndroidX `SavedStateHandler` passed to the Android flavor of `renderWorkflowIn()` to prevent memory leaks.
- Loading branch information
Showing
4 changed files
with
99 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...ore-android/src/androidTest/java/com/squareup/workflow1/ui/AndroidRenderWorkflowInTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.squareup.workflow1.ui | ||
|
||
import android.widget.FrameLayout | ||
import androidx.activity.ComponentActivity | ||
import androidx.activity.viewModels | ||
import androidx.lifecycle.Lifecycle.State.CREATED | ||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import androidx.test.ext.junit.rules.ActivityScenarioRule | ||
import com.google.common.truth.Truth.assertThat | ||
import com.squareup.workflow1.StatelessWorkflow | ||
import com.squareup.workflow1.ui.internal.test.IdlingDispatcherRule | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.flow.StateFlow | ||
import leakcanary.DetectLeaksAfterTestSuccess | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.rules.RuleChain | ||
|
||
@OptIn(WorkflowUiExperimentalApi::class) | ||
internal class AndroidRenderWorkflowInTest { | ||
@get:Rule val scenarioRule = ActivityScenarioRule(ComponentActivity::class.java) | ||
private val scenario get() = scenarioRule.scenario | ||
|
||
@get:Rule val rules: RuleChain = RuleChain.outerRule(DetectLeaksAfterTestSuccess()) | ||
.around(scenarioRule) | ||
.around(IdlingDispatcherRule) | ||
|
||
@Test fun removeWorkflowStateDoesWhatItSaysOnTheTin() { | ||
var job: Job? = null | ||
scenario.onActivity { activity -> | ||
val model: SomeViewModel by activity.viewModels() | ||
val renderings: StateFlow<Screen> = renderWorkflowIn( | ||
workflow = SomeWorkflow, | ||
scope = model.viewModelScope, | ||
savedStateHandle = model.savedStateHandle | ||
) | ||
|
||
val layout = WorkflowLayout(activity) | ||
activity.setContentView(layout) | ||
|
||
assertThat(model.savedStateHandle.contains(KEY)).isFalse() | ||
|
||
job = layout.take(activity.lifecycle, renderings) | ||
assertThat(model.savedStateHandle.contains(KEY)).isFalse() | ||
} | ||
|
||
scenario.moveToState(CREATED) | ||
scenario.onActivity { activity -> | ||
val model: SomeViewModel by activity.viewModels() | ||
assertThat(model.savedStateHandle.contains(KEY)).isTrue() | ||
|
||
job?.cancel() | ||
assertThat(model.savedStateHandle.contains(KEY)).isTrue() | ||
|
||
model.savedStateHandle.removeWorkflowState() | ||
assertThat(model.savedStateHandle.contains(KEY)).isFalse() | ||
} | ||
} | ||
|
||
object SomeScreen : AndroidScreen<SomeScreen> { | ||
override val viewFactory: ScreenViewFactory<SomeScreen> = | ||
ScreenViewFactory.fromCode { _, initialEnvironment, context, _ -> | ||
ScreenViewHolder( | ||
initialEnvironment, | ||
FrameLayout(context) | ||
) { _, _ -> } | ||
} | ||
} | ||
|
||
object SomeWorkflow : StatelessWorkflow<Unit, Nothing, Screen>() { | ||
override fun render( | ||
renderProps: Unit, | ||
context: RenderContext | ||
): Screen { | ||
return SomeScreen | ||
} | ||
} | ||
|
||
class SomeViewModel(val savedStateHandle: SavedStateHandle) : ViewModel() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters