|
| 1 | +package com.squareup.sample.container.overviewdetail |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.view.View |
| 5 | +import androidx.activity.ComponentActivity |
| 6 | +import androidx.test.ext.junit.rules.ActivityScenarioRule |
| 7 | +import com.google.common.truth.Truth.assertThat |
| 8 | +import com.squareup.workflow1.ui.AndroidViewRendering |
| 9 | +import com.squareup.workflow1.ui.BuilderViewFactory |
| 10 | +import com.squareup.workflow1.ui.Compatible |
| 11 | +import com.squareup.workflow1.ui.Named |
| 12 | +import com.squareup.workflow1.ui.ViewEnvironment |
| 13 | +import com.squareup.workflow1.ui.ViewFactory |
| 14 | +import com.squareup.workflow1.ui.WorkflowUiExperimentalApi |
| 15 | +import com.squareup.workflow1.ui.backstack.BackStackContainer |
| 16 | +import com.squareup.workflow1.ui.backstack.BackStackScreen |
| 17 | +import com.squareup.workflow1.ui.bindShowRendering |
| 18 | +import com.squareup.workflow1.ui.getRendering |
| 19 | +import org.junit.Rule |
| 20 | +import org.junit.Test |
| 21 | + |
| 22 | +@OptIn(WorkflowUiExperimentalApi::class) |
| 23 | +internal class BackStackContainerTest { |
| 24 | + @get:Rule val scenarioRule = ActivityScenarioRule(ComponentActivity::class.java) |
| 25 | + private val scenario get() = scenarioRule.scenario |
| 26 | + |
| 27 | + private data class Rendering(val name: String) : Compatible, AndroidViewRendering<Rendering> { |
| 28 | + override val compatibilityKey = name |
| 29 | + override val viewFactory: ViewFactory<Rendering> |
| 30 | + get() = BuilderViewFactory(Rendering::class) { r, e, ctx, _ -> |
| 31 | + View(ctx).also { it.bindShowRendering(r, e) { _, _ -> /* Noop */ } } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + @Test fun firstScreenIsRendered() { |
| 36 | + scenario.onActivity { activity -> |
| 37 | + val c = VisibleBackStackContainer(activity) |
| 38 | + |
| 39 | + c.show(BackStackScreen(Rendering("able"))) |
| 40 | + val showing = c.visibleRendering as Rendering |
| 41 | + assertThat(showing).isEqualTo(Rendering("able")) |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Test fun secondScreenIsRendered() { |
| 46 | + scenario.onActivity { activity -> |
| 47 | + val c = VisibleBackStackContainer(activity) |
| 48 | + |
| 49 | + c.show(BackStackScreen(Rendering("able"))) |
| 50 | + c.show(BackStackScreen(Rendering("baker"))) |
| 51 | + val showing = c.visibleRendering as Rendering |
| 52 | + assertThat(showing).isEqualTo(Rendering("baker")) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @Test fun thirdScreenIsRendered() { |
| 57 | + scenario.onActivity { activity -> |
| 58 | + val c = VisibleBackStackContainer(activity) |
| 59 | + |
| 60 | + c.show(BackStackScreen(Rendering("able"))) |
| 61 | + c.show(BackStackScreen(Rendering("baker"))) |
| 62 | + c.show(BackStackScreen(Rendering("charlie"))) |
| 63 | + val showing = c.visibleRendering as Rendering |
| 64 | + assertThat(showing).isEqualTo(Rendering("charlie")) |
| 65 | + |
| 66 | + // This used to fail because of our naive use of TransitionManager. The |
| 67 | + // transition from baker view to charlie view was dropped because the |
| 68 | + // transition from able view to baker view was still in progress. |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test fun isDebounced() { |
| 73 | + scenario.onActivity { activity -> |
| 74 | + val c = VisibleBackStackContainer(activity) |
| 75 | + |
| 76 | + c.show(BackStackScreen(Rendering("able"))) |
| 77 | + c.show(BackStackScreen(Rendering("able"))) |
| 78 | + c.show(BackStackScreen(Rendering("able"))) |
| 79 | + c.show(BackStackScreen(Rendering("able"))) |
| 80 | + |
| 81 | + assertThat(c.transitionCount).isEqualTo(1) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private class VisibleBackStackContainer(context: Context) : BackStackContainer(context) { |
| 86 | + var transitionCount = 0 |
| 87 | + val visibleRendering: Any? get() = getChildAt(0)?.getRendering<Named<*>>()?.wrapped |
| 88 | + |
| 89 | + fun show(rendering: BackStackScreen<*>) { |
| 90 | + update(rendering, ViewEnvironment()) |
| 91 | + } |
| 92 | + |
| 93 | + override fun performTransition( |
| 94 | + oldViewMaybe: View?, |
| 95 | + newView: View, |
| 96 | + popped: Boolean |
| 97 | + ) { |
| 98 | + transitionCount++ |
| 99 | + super.performTransition(oldViewMaybe, newView, popped) |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments