|
| 1 | +package com.squareup.workflow1.ui.container |
| 2 | + |
| 3 | +import android.app.Dialog |
| 4 | +import android.content.Context |
| 5 | +import android.view.View |
| 6 | +import android.widget.EditText |
| 7 | +import androidx.activity.ComponentActivity |
| 8 | +import androidx.test.ext.junit.rules.ActivityScenarioRule |
| 9 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 10 | +import com.google.common.truth.Truth.assertThat |
| 11 | +import com.squareup.workflow1.ui.AndroidScreen |
| 12 | +import com.squareup.workflow1.ui.Compatible |
| 13 | +import com.squareup.workflow1.ui.ScreenViewFactory |
| 14 | +import com.squareup.workflow1.ui.ScreenViewHolder |
| 15 | +import com.squareup.workflow1.ui.ViewEnvironment |
| 16 | +import com.squareup.workflow1.ui.WorkflowLayout |
| 17 | +import com.squareup.workflow1.ui.WorkflowUiExperimentalApi |
| 18 | +import com.squareup.workflow1.ui.container.OverlayDialogHolder.Companion.InOverlay |
| 19 | +import com.squareup.workflow1.ui.environmentOrNull |
| 20 | +import org.junit.Rule |
| 21 | +import org.junit.Test |
| 22 | +import org.junit.runner.RunWith |
| 23 | + |
| 24 | +@OptIn(WorkflowUiExperimentalApi::class) |
| 25 | +@RunWith(AndroidJUnit4::class) |
| 26 | +internal class DialogIntegrationTest { |
| 27 | + @get:Rule val scenarioRule = ActivityScenarioRule(ComponentActivity::class.java) |
| 28 | + private val scenario get() = scenarioRule.scenario |
| 29 | + |
| 30 | + private data class ContentRendering(val name: String) : |
| 31 | + Compatible, AndroidScreen<ContentRendering> { |
| 32 | + override val compatibilityKey = name |
| 33 | + override val viewFactory: ScreenViewFactory<ContentRendering> |
| 34 | + get() = ScreenViewFactory.fromCode { _, initialRendering, context, _ -> |
| 35 | + ScreenViewHolder( |
| 36 | + initialRendering, |
| 37 | + EditText(context).apply { |
| 38 | + // Must have an id to participate in view persistence. |
| 39 | + id = 65 |
| 40 | + } |
| 41 | + ) { _, _ -> /* Noop */ } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + private var latestContentView: View? = null |
| 46 | + private var latestDialog: Dialog? = null |
| 47 | + |
| 48 | + private inner class DialogRendering( |
| 49 | + name: String, |
| 50 | + override val content: ContentRendering |
| 51 | + ) : |
| 52 | + Compatible, AndroidOverlay<DialogRendering>, ScreenOverlay<ContentRendering> { |
| 53 | + override val compatibilityKey = name |
| 54 | + override val dialogFactory = |
| 55 | + object : ScreenOverlayDialogFactory<ContentRendering, DialogRendering>( |
| 56 | + type = DialogRendering::class |
| 57 | + ) { |
| 58 | + override fun buildContent( |
| 59 | + viewFactory: ScreenViewFactory<ContentRendering>, |
| 60 | + initialContent: ContentRendering, |
| 61 | + initialEnvironment: ViewEnvironment, |
| 62 | + context: Context |
| 63 | + ): ScreenViewHolder<ContentRendering> = |
| 64 | + super.buildContent(viewFactory, initialContent, initialEnvironment, context).also { |
| 65 | + latestContentView = it.view |
| 66 | + } |
| 67 | + |
| 68 | + override fun buildDialogWithContent( |
| 69 | + content: ScreenViewHolder<ContentRendering> |
| 70 | + ) = super.buildDialogWithContent(content).also { latestDialog = it } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test fun showOne() { |
| 75 | + val screen = BodyAndOverlaysScreen( |
| 76 | + ContentRendering("body"), DialogRendering("dialog", ContentRendering("content")) |
| 77 | + ) |
| 78 | + |
| 79 | + scenario.onActivity { activity -> |
| 80 | + val root = WorkflowLayout(activity) |
| 81 | + root.show(screen) |
| 82 | + |
| 83 | + assertThat(latestContentView).isNotNull() |
| 84 | + assertThat(latestDialog).isNotNull() |
| 85 | + assertThat(latestDialog!!.isShowing).isTrue() |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** https://github.com/square/workflow-kotlin/issues/825 */ |
| 90 | + @Test fun showASecondDialog() { |
| 91 | + val oneDialog = BodyAndOverlaysScreen( |
| 92 | + ContentRendering("body"), DialogRendering("dialog", ContentRendering("content")) |
| 93 | + ) |
| 94 | + lateinit var root: WorkflowLayout |
| 95 | + |
| 96 | + scenario.onActivity { activity -> |
| 97 | + root = WorkflowLayout(activity) |
| 98 | + root.show(oneDialog) |
| 99 | + } |
| 100 | + |
| 101 | + val dialog2 = DialogRendering("dialog2", ContentRendering("content2")) |
| 102 | + val twoDialogs = BodyAndOverlaysScreen( |
| 103 | + ContentRendering("body"), |
| 104 | + DialogRendering("dialog1", ContentRendering("content1")), |
| 105 | + dialog2 |
| 106 | + ) |
| 107 | + |
| 108 | + scenario.onActivity { |
| 109 | + root.show(twoDialogs) |
| 110 | + val lastOverlay = latestContentView?.environmentOrNull?.get(InOverlay)!! |
| 111 | + assertThat(lastOverlay).isEqualTo(dialog2) |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments