|
| 1 | +package com.flipcash.shared.chat.ui |
| 2 | + |
| 3 | +import androidx.compose.foundation.clickable |
| 4 | +import androidx.compose.foundation.layout.Row |
| 5 | +import androidx.compose.material3.Text |
| 6 | +import androidx.compose.ui.Modifier |
| 7 | +import androidx.compose.ui.test.assertIsDisplayed |
| 8 | +import androidx.compose.ui.test.hasClickAction |
| 9 | +import androidx.compose.ui.test.hasContentDescription |
| 10 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 11 | +import androidx.compose.ui.test.onNodeWithContentDescription |
| 12 | +import com.flipcash.services.models.chat.MuteState |
| 13 | +import com.flipcash.services.models.chat.ViewerState |
| 14 | +import com.getcode.theme.DesignSystem |
| 15 | +import org.junit.Rule |
| 16 | +import org.junit.Test |
| 17 | +import org.junit.runner.RunWith |
| 18 | +import org.robolectric.RobolectricTestRunner |
| 19 | +import kotlin.time.Clock |
| 20 | +import kotlin.time.Duration.Companion.hours |
| 21 | + |
| 22 | +/** |
| 23 | + * The indicator's two questions: whether it draws, and whether a screen reader hears it. |
| 24 | + * |
| 25 | + * The second is the reason this is a Compose test rather than a unit test of [rememberIsMuted]. |
| 26 | + * Both surfaces that draw the bell sit inside a merging semantics node — a clickable row, a |
| 27 | + * tappable title — and what such a node does with a child's description is the platform's choice, |
| 28 | + * not ours. Compose concatenates rather than discarding, which is why the bell carries its own |
| 29 | + * description instead of the containers appending to theirs; if that ever stopped being true the |
| 30 | + * indicator would go silent with nothing else failing. |
| 31 | + */ |
| 32 | +@RunWith(RobolectricTestRunner::class) |
| 33 | +class MutedIndicatorTest { |
| 34 | + |
| 35 | + @get:Rule |
| 36 | + val composeTestRule = createComposeRule() |
| 37 | + |
| 38 | + private val mutedLabel = "Muted" |
| 39 | + |
| 40 | + private fun setIndicator(viewerState: ViewerState?) { |
| 41 | + composeTestRule.setContent { |
| 42 | + DesignSystem { |
| 43 | + MutedIndicator(viewerState = viewerState) |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // --------------------------------------------------------------- |
| 49 | + // Whether it draws |
| 50 | + // --------------------------------------------------------------- |
| 51 | + |
| 52 | + @Test |
| 53 | + fun `drawn while muted forever`() { |
| 54 | + setIndicator(ViewerState(mute = MuteState.Forever)) |
| 55 | + composeTestRule.onNodeWithContentDescription(mutedLabel).assertIsDisplayed() |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun `drawn while a timed mute is still running`() { |
| 60 | + setIndicator(ViewerState(mute = MuteState.Until(Clock.System.now() + 1.hours))) |
| 61 | + composeTestRule.onNodeWithContentDescription(mutedLabel).assertIsDisplayed() |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun `not drawn once a timed mute has lapsed`() { |
| 66 | + // The state the server last sent, read after its deadline. Nothing arrives to clear it, so |
| 67 | + // this is what a chat that has gone audible on its own actually looks like in the store. |
| 68 | + setIndicator(ViewerState(mute = MuteState.Until(Clock.System.now() - 1.hours))) |
| 69 | + composeTestRule.onNodeWithContentDescription(mutedLabel).assertDoesNotExist() |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun `not drawn when the chat is not muted`() { |
| 74 | + setIndicator(ViewerState(mute = null)) |
| 75 | + composeTestRule.onNodeWithContentDescription(mutedLabel).assertDoesNotExist() |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + fun `not drawn when there is no viewer state`() { |
| 80 | + setIndicator(null) |
| 81 | + composeTestRule.onNodeWithContentDescription(mutedLabel).assertDoesNotExist() |
| 82 | + } |
| 83 | + |
| 84 | + // --------------------------------------------------------------- |
| 85 | + // Whether a screen reader hears it |
| 86 | + // --------------------------------------------------------------- |
| 87 | + |
| 88 | + @Test |
| 89 | + fun `description survives a merging container`() { |
| 90 | + composeTestRule.setContent { |
| 91 | + DesignSystem { |
| 92 | + Row(modifier = Modifier.clickable {}) { |
| 93 | + Text("Alice") |
| 94 | + MutedIndicator(viewerState = ViewerState(mute = MuteState.Forever)) |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // The clickable is the merged node; finding the description on it is what says the bell |
| 100 | + // reached the label the row is read out as, rather than only existing under it. |
| 101 | + composeTestRule |
| 102 | + .onNode(hasClickAction() and hasContentDescription(mutedLabel)) |
| 103 | + .assertExists() |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + fun `merging container says nothing about muting when the chat is audible`() { |
| 108 | + composeTestRule.setContent { |
| 109 | + DesignSystem { |
| 110 | + Row(modifier = Modifier.clickable {}) { |
| 111 | + Text("Alice") |
| 112 | + MutedIndicator(viewerState = null) |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + composeTestRule.onNodeWithContentDescription(mutedLabel).assertDoesNotExist() |
| 118 | + } |
| 119 | +} |
0 commit comments